bb40c551a7491142c98f4a45eec990f59490b5d0
[jackhill/guix/guix.git] / gnu / bootloader / grub.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
4 ;;; Copyright © 2017 Leo Famulari <leo@famulari.name>
5 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
6 ;;; Copyright © 2019, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
7 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
8 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
15 ;;;
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24 (define-module (gnu bootloader grub)
25 #:use-module (guix records)
26 #:use-module ((guix utils) #:select (%current-system))
27 #:use-module (guix gexp)
28 #:use-module (gnu artwork)
29 #:use-module (gnu bootloader)
30 #:use-module (gnu system uuid)
31 #:use-module (gnu system file-systems)
32 #:use-module (gnu system keyboard)
33 #:use-module (gnu packages bootloaders)
34 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
35 #:autoload (gnu packages xorg) (xkeyboard-config)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 regex)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-2)
40 #:export (grub-theme
41 grub-theme?
42 grub-theme-image
43 grub-theme-resolution
44 grub-theme-color-normal
45 grub-theme-color-highlight
46 grub-theme-gfxmode
47
48 grub-bootloader
49 grub-efi-bootloader
50 grub-mkrescue-bootloader
51 grub-minimal-bootloader
52
53 grub-configuration))
54
55 ;;; Commentary:
56 ;;;
57 ;;; Configuration of GNU GRUB.
58 ;;;
59 ;;; Code:
60
61 (define* (normalize-file file mount-point btrfs-subvolume-file-name)
62 "Strip MOUNT-POINT and prepend BTRFS-SUBVOLUME-FILE-NAME to FILE, a
63 G-expression or other lowerable object denoting a file name."
64
65 (define (strip-mount-point mount-point file)
66 (if mount-point
67 (if (string=? mount-point "/")
68 file
69 #~(let ((file #$file))
70 (if (string-prefix? #$mount-point file)
71 (substring #$file #$(string-length mount-point))
72 file)))
73 file))
74
75 (define (prepend-btrfs-subvolume-file-name btrfs-subvolume-file-name file)
76 (if btrfs-subvolume-file-name
77 #~(string-append #$btrfs-subvolume-file-name #$file)
78 file))
79
80 (prepend-btrfs-subvolume-file-name btrfs-subvolume-file-name
81 (strip-mount-point mount-point file)))
82
83
84
85 (define-record-type* <grub-theme>
86 ;; Default theme contributed by Felipe López.
87 grub-theme make-grub-theme
88 grub-theme?
89 (image grub-theme-image
90 (default (file-append %artwork-repository
91 "/grub/GuixSD-fully-black-4-3.svg")))
92 (resolution grub-theme-resolution
93 (default '(1024 . 768)))
94 (color-normal grub-theme-color-normal
95 (default '((fg . light-gray) (bg . black))))
96 (color-highlight grub-theme-color-highlight
97 (default '((fg . yellow) (bg . black))))
98 (gfxmode grub-theme-gfxmode
99 (default '("auto")))) ;list of string
100
101 \f
102 ;;;
103 ;;; Background image & themes.
104 ;;;
105
106 (define (bootloader-theme config)
107 "Return user defined theme in CONFIG if defined or a default theme
108 otherwise."
109 (or (bootloader-configuration-theme config) (grub-theme)))
110
111 (define* (image->png image #:key width height)
112 "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\".
113 Otherwise the picture in IMAGE is just copied."
114 (computed-file "grub-image.png"
115 (with-imported-modules '((gnu build svg))
116 (with-extensions (list guile-rsvg guile-cairo)
117 #~(if (string-suffix? ".svg" #+image)
118 (begin
119 (use-modules (gnu build svg))
120 (svg->png #+image #$output
121 #:width #$width
122 #:height #$height))
123 (copy-file #+image #$output))))))
124
125 (define* (grub-background-image config)
126 "Return the GRUB background image defined in CONFIG or #f if none was found.
127 If the suffix of the image file is \".svg\", then it is converted into a PNG
128 file with the resolution provided in CONFIG."
129 (let* ((theme (bootloader-theme config))
130 (image (grub-theme-image theme)))
131 (and image
132 (match (grub-theme-resolution theme)
133 (((? number? width) . (? number? height))
134 (image->png image #:width width #:height height))
135 (_ #f)))))
136
137 (define* (eye-candy config store-device store-mount-point
138 #:key btrfs-store-subvolume-file-name system port)
139 "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part
140 concerned with graphics mode, background images, colors, and all that.
141 STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is
142 its mount point; these are used to determine where the background image and
143 fonts must be searched for. SYSTEM must be the target system string---e.g.,
144 \"x86_64-linux\". BTRFS-STORE-SUBVOLUME-FILE-NAME is the file name of the
145 Btrfs subvolume, to be prepended to any store path, if any."
146 (define setup-gfxterm-body
147 (let ((gfxmode
148 (or (and-let* ((theme (bootloader-configuration-theme config))
149 (gfxmode (grub-theme-gfxmode theme)))
150 (string-join gfxmode ";"))
151 "auto")))
152
153 ;; Intel and EFI systems need to be switched into graphics mode, whereas
154 ;; most other modern architectures have no other mode and therefore
155 ;; don't need to be switched.
156
157 ;; XXX: Do we really need to restrict to x86 systems? We could imitate
158 ;; what the GRUB default configuration does and decide based on whether
159 ;; a user provided 'gfxterm' in the terminal-outputs field of their
160 ;; bootloader-configuration record.
161 (if (string-match "^(x86_64|i[3-6]86)-" system)
162 (format #f "
163 set gfxmode=~a
164 insmod all_video
165 insmod gfxterm~%" gfxmode)
166 "")))
167
168 (define (setup-gfxterm config font-file)
169 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
170 #~(format #f "if loadfont ~a; then
171 setup_gfxterm
172 fi~%" #+font-file)
173 ""))
174
175 (define (theme-colors type)
176 (let* ((theme (bootloader-theme config))
177 (colors (type theme)))
178 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
179 (symbol->string (assoc-ref colors 'bg)))))
180
181 (define font-file
182 (normalize-file (file-append grub "/share/grub/unicode.pf2")
183 store-mount-point
184 btrfs-store-subvolume-file-name))
185
186 (define image
187 (normalize-file (grub-background-image config)
188 store-mount-point
189 btrfs-store-subvolume-file-name))
190
191 (and image
192 #~(format #$port "
193 function setup_gfxterm {~a}
194
195 # Set 'root' to the partition that contains /gnu/store.
196 ~a
197
198 ~a
199 ~a
200
201 insmod png
202 if background_image ~a; then
203 set color_normal=~a
204 set color_highlight=~a
205 else
206 set menu_color_normal=cyan/blue
207 set menu_color_highlight=white/blue
208 fi~%"
209 #$setup-gfxterm-body
210 #$(grub-root-search store-device font-file)
211 #$(setup-gfxterm config font-file)
212 #$(grub-setup-io config)
213
214 #$image
215 #$(theme-colors grub-theme-color-normal)
216 #$(theme-colors grub-theme-color-highlight))))
217
218 \f
219 ;;;
220 ;;; Configuration file.
221 ;;;
222
223 (define* (keyboard-layout-file layout
224 #:key
225 (grub grub))
226 "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
227 and return a file in the format for GRUB keymaps. LAYOUT must be present in
228 the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'."
229 (define builder
230 (with-imported-modules '((guix build utils))
231 #~(begin
232 (use-modules (guix build utils))
233
234 ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp'
235 ;; (from the 'console-setup' package).
236 (invoke #+(file-append grub "/bin/grub-mklayout")
237 "-i" #+(keyboard-layout->console-keymap layout)
238 "-o" #$output))))
239
240 (computed-file (string-append "grub-keymap."
241 (string-map (match-lambda
242 (#\, #\-)
243 (chr chr))
244 (keyboard-layout-name layout)))
245 builder))
246
247 (define (grub-setup-io config)
248 "Return GRUB commands to configure the input / output interfaces. The result
249 is a string that can be inserted in grub.cfg."
250 (let* ((symbols->string (lambda (list)
251 (string-join (map symbol->string list) " ")))
252 (outputs (bootloader-configuration-terminal-outputs config))
253 (inputs (bootloader-configuration-terminal-inputs config))
254 (unit (bootloader-configuration-serial-unit config))
255 (speed (bootloader-configuration-serial-speed config))
256
257 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
258 ;; as documented in GRUB manual section "Simple Configuration
259 ;; Handling".
260 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
261 gfxterm vga_text mda_text morse spkmodem))
262 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
263 at_keyboard usb_keyboard))
264
265 (io (string-append
266 "terminal_output "
267 (symbols->string
268 (map
269 (lambda (output)
270 (if (memq output valid-outputs) output #f)) outputs)) "\n"
271 (if (null? inputs)
272 ""
273 (string-append
274 "terminal_input "
275 (symbols->string
276 (map
277 (lambda (input)
278 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
279 ;; UNIT and SPEED are arguments to the same GRUB command
280 ;; ("serial"), so we process them together.
281 (if (or unit speed)
282 (string-append
283 "serial"
284 (if unit
285 ;; COM ports 1 through 4
286 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
287 (string-append " --unit=" (number->string unit))
288 #f)
289 "")
290 (if speed
291 (if (exact-integer? speed)
292 (string-append " --speed=" (number->string speed))
293 #f)
294 ""))
295 ""))))
296 (format #f "~a" io)))
297
298 (define (grub-root-search device file)
299 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
300 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
301 code."
302 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
303 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
304 ;; custom menu entries. In the latter case, don't emit a 'search' command.
305 (if (and (string? file) (not (string-prefix? "/" file)))
306 ""
307 (match device
308 ;; Preferably refer to DEVICE by its UUID or label. This is more
309 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
310 ((? uuid? uuid)
311 (format #f "search --fs-uuid --set ~a"
312 (uuid->string device)))
313 ((? file-system-label? label)
314 (format #f "search --label --set ~a"
315 (file-system-label->string label)))
316 ((or #f (? string?))
317 #~(format #f "search --file --set ~a" #$file)))))
318
319 (define* (grub-configuration-file config entries
320 #:key
321 (system (%current-system))
322 (old-entries '())
323 btrfs-subvolume-file-name)
324 "Return the GRUB configuration file corresponding to CONFIG, a
325 <bootloader-configuration> object, and where the store is available at
326 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list
327 of menu entries corresponding to old generations of the system.
328 BTRFS-SUBVOLUME-FILE-NAME may be used to specify on which subvolume a
329 Btrfs root file system resides."
330 (define all-entries
331 (append entries (bootloader-configuration-menu-entries config)))
332 (define (menu-entry->gexp entry)
333 (let* ((device (menu-entry-device entry))
334 (device-mount-point (menu-entry-device-mount-point entry))
335 (label (menu-entry-label entry))
336 (arguments (menu-entry-linux-arguments entry))
337 (kernel (normalize-file (menu-entry-linux entry)
338 device-mount-point
339 btrfs-subvolume-file-name))
340 (initrd (normalize-file (menu-entry-initrd entry)
341 device-mount-point
342 btrfs-subvolume-file-name)))
343 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
344 ;; Use the right file names for KERNEL and INITRD in case
345 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
346 ;; separate partition.
347
348 ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the kernel and
349 ;; initrd paths, to allow booting from a Btrfs subvolume.
350 #~(format port "menuentry ~s {
351 ~a
352 linux ~a ~a
353 initrd ~a
354 }~%"
355 #$label
356 #$(grub-root-search device kernel)
357 #$kernel (string-join (list #$@arguments))
358 #$initrd)))
359 (define sugar
360 (eye-candy config
361 (menu-entry-device (first all-entries))
362 (menu-entry-device-mount-point (first all-entries))
363 #:btrfs-store-subvolume-file-name btrfs-subvolume-file-name
364 #:system system
365 #:port #~port))
366
367 (define keyboard-layout-config
368 (let* ((layout (bootloader-configuration-keyboard-layout config))
369 (grub (bootloader-package
370 (bootloader-configuration-bootloader config)))
371 (keymap* (and layout
372 (keyboard-layout-file layout #:grub grub)))
373 (keymap (and keymap*
374 (if btrfs-subvolume-file-name
375 #~(string-append #$btrfs-subvolume-file-name
376 #$keymap*)
377 keymap*))))
378 #~(when #$keymap
379 (format port "\
380 insmod keylayouts
381 keymap ~a~%" #$keymap))))
382
383 (define builder
384 #~(call-with-output-file #$output
385 (lambda (port)
386 (format port
387 "# This file was generated from your Guix configuration. Any changes
388 # will be lost upon reconfiguration.
389 ")
390 #$sugar
391 #$keyboard-layout-config
392 (format port "
393 set default=~a
394 set timeout=~a~%"
395 #$(bootloader-configuration-default-entry config)
396 #$(bootloader-configuration-timeout config))
397 #$@(map menu-entry->gexp all-entries)
398
399 #$@(if (pair? old-entries)
400 #~((format port "
401 submenu \"GNU system, old configurations...\" {~%")
402 #$@(map menu-entry->gexp old-entries)
403 (format port "}~%"))
404 #~())
405 (format port "
406 if [ \"${grub_platform}\" == efi ]; then
407 menuentry \"Firmware setup\" {
408 fwsetup
409 }
410 fi~%"))))
411
412 ;; Since this file is rather unique, there's no point in trying to
413 ;; substitute it.
414 (computed-file "grub.cfg" builder
415 #:options '(#:local-build? #t
416 #:substitutable? #f)))
417
418 \f
419
420 ;;;
421 ;;; Install procedures.
422 ;;;
423
424 (define install-grub
425 #~(lambda (bootloader device mount-point)
426 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
427 (let ((grub (string-append bootloader "/sbin/grub-install"))
428 (install-dir (string-append mount-point "/boot")))
429 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
430 ;; root partition.
431 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
432
433 ;; Hide potentially confusing messages from the user, such as
434 ;; "Installing for i386-pc platform."
435 (invoke/quiet grub "--no-floppy" "--target=i386-pc"
436 "--boot-directory" install-dir
437 device))))
438
439 (define install-grub-efi
440 #~(lambda (bootloader efi-dir mount-point)
441 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
442 ;; system whose root is mounted at MOUNT-POINT.
443 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
444 (install-dir (string-append mount-point "/boot"))
445 ;; When installing Guix, it's common to mount EFI-DIR below
446 ;; MOUNT-POINT rather than /boot/efi on the live image.
447 (target-esp (if (file-exists? (string-append mount-point efi-dir))
448 (string-append mount-point efi-dir)
449 efi-dir)))
450 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
451 ;; root partition.
452 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
453 (invoke/quiet grub-install "--boot-directory" install-dir
454 "--bootloader-id=Guix"
455 "--efi-directory" target-esp))))
456
457 \f
458
459 ;;;
460 ;;; Bootloader definitions.
461 ;;;
462
463 (define grub-bootloader
464 (bootloader
465 (name 'grub)
466 (package grub)
467 (installer install-grub)
468 (configuration-file "/boot/grub/grub.cfg")
469 (configuration-file-generator grub-configuration-file)))
470
471 (define grub-minimal-bootloader
472 (bootloader
473 (name 'grub)
474 (package grub-minimal)
475 (installer install-grub)
476 (configuration-file "/boot/grub/grub.cfg")
477 (configuration-file-generator grub-configuration-file)))
478
479 (define* grub-efi-bootloader
480 (bootloader
481 (inherit grub-bootloader)
482 (installer install-grub-efi)
483 (name 'grub-efi)
484 (package grub-efi)))
485
486 (define* grub-mkrescue-bootloader
487 (bootloader
488 (inherit grub-efi-bootloader)
489 (package grub-hybrid)))
490
491 \f
492 ;;;
493 ;;; Compatibility macros.
494 ;;;
495
496 (define-syntax grub-configuration
497 (syntax-rules (grub)
498 ((_ (grub package) fields ...)
499 (if (eq? package grub)
500 (bootloader-configuration
501 (bootloader grub-bootloader)
502 fields ...)
503 (bootloader-configuration
504 (bootloader grub-efi-bootloader)
505 fields ...)))
506 ((_ fields ...)
507 (bootloader-configuration
508 (bootloader grub-bootloader)
509 fields ...))))
510
511 ;;; grub.scm ends here