gnu: grub: Support loading files from TFTP if the root filesystem is NFS.
[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, 2020 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 ;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
9 ;;;
10 ;;; This file is part of GNU Guix.
11 ;;;
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
16 ;;;
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24
25 (define-module (gnu bootloader grub)
26 #:use-module (guix records)
27 #:use-module ((guix utils) #:select (%current-system))
28 #:use-module (guix gexp)
29 #:use-module (gnu artwork)
30 #:use-module (gnu bootloader)
31 #:use-module (gnu system uuid)
32 #:use-module (gnu system file-systems)
33 #:use-module (gnu system keyboard)
34 #:use-module (gnu packages bootloaders)
35 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
36 #:autoload (gnu packages xorg) (xkeyboard-config)
37 #:use-module (ice-9 match)
38 #:use-module (ice-9 regex)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-2)
41 #:export (grub-theme
42 grub-theme?
43 grub-theme-image
44 grub-theme-resolution
45 grub-theme-color-normal
46 grub-theme-color-highlight
47 grub-theme-gfxmode
48
49 grub-bootloader
50 grub-efi-bootloader
51 grub-mkrescue-bootloader
52 grub-minimal-bootloader
53
54 grub-configuration))
55
56 ;;; Commentary:
57 ;;;
58 ;;; Configuration of GNU GRUB.
59 ;;;
60 ;;; Code:
61
62 (define* (normalize-file file mount-point store-directory-prefix)
63 "Strip MOUNT-POINT and prepend STORE-DIRECTORY-PREFIX, if any, to FILE, a
64 G-expression or other lowerable object denoting a file name."
65
66 (define (strip-mount-point mount-point file)
67 (if mount-point
68 (if (string=? mount-point "/")
69 file
70 #~(let ((file #$file))
71 (if (string-prefix? #$mount-point file)
72 (substring #$file #$(string-length mount-point))
73 file)))
74 file))
75
76 (define (prepend-store-directory-prefix store-directory-prefix file)
77 (if store-directory-prefix
78 #~(string-append #$store-directory-prefix #$file)
79 file))
80
81 (prepend-store-directory-prefix store-directory-prefix
82 (strip-mount-point mount-point file)))
83
84
85
86 (define-record-type* <grub-theme>
87 ;; Default theme contributed by Felipe López.
88 grub-theme make-grub-theme
89 grub-theme?
90 (image grub-theme-image
91 (default (file-append %artwork-repository
92 "/grub/GuixSD-fully-black-4-3.svg")))
93 (resolution grub-theme-resolution
94 (default '(1024 . 768)))
95 (color-normal grub-theme-color-normal
96 (default '((fg . light-gray) (bg . black))))
97 (color-highlight grub-theme-color-highlight
98 (default '((fg . yellow) (bg . black))))
99 (gfxmode grub-theme-gfxmode
100 (default '("auto")))) ;list of string
101
102 \f
103 ;;;
104 ;;; Background image & themes.
105 ;;;
106
107 (define (bootloader-theme config)
108 "Return user defined theme in CONFIG if defined or a default theme
109 otherwise."
110 (or (bootloader-configuration-theme config) (grub-theme)))
111
112 (define* (image->png image #:key width height)
113 "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\".
114 Otherwise the picture in IMAGE is just copied."
115 (computed-file "grub-image.png"
116 (with-imported-modules '((gnu build svg))
117 (with-extensions (list guile-rsvg guile-cairo)
118 #~(if (string-suffix? ".svg" #+image)
119 (begin
120 (use-modules (gnu build svg))
121 (svg->png #+image #$output
122 #:width #$width
123 #:height #$height))
124 (copy-file #+image #$output))))))
125
126 (define* (grub-background-image config)
127 "Return the GRUB background image defined in CONFIG or #f if none was found.
128 If the suffix of the image file is \".svg\", then it is converted into a PNG
129 file with the resolution provided in CONFIG."
130 (let* ((theme (bootloader-theme config))
131 (image (grub-theme-image theme)))
132 (and image
133 (match (grub-theme-resolution theme)
134 (((? number? width) . (? number? height))
135 (image->png image #:width width #:height height))
136 (_ #f)))))
137
138 (define* (eye-candy config store-device store-mount-point
139 #:key store-directory-prefix port)
140 "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part
141 concerned with graphics mode, background images, colors, and all that.
142 STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is
143 its mount point; these are used to determine where the background image and
144 fonts must be searched for. STORE-DIRECTORY-PREFIX is a directory prefix to
145 prepend to any store file name."
146 (define (setup-gfxterm config font-file)
147 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
148 #~(format #f "
149 if loadfont ~a; then
150 set gfxmode=~a
151 insmod all_video
152 insmod gfxterm
153 fi~%"
154 #+font-file
155 #$(string-join
156 (grub-theme-gfxmode (bootloader-theme config))
157 ";"))
158 ""))
159
160 (define (theme-colors type)
161 (let* ((theme (bootloader-theme config))
162 (colors (type theme)))
163 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
164 (symbol->string (assoc-ref colors 'bg)))))
165
166 (define font-file
167 (normalize-file (file-append grub "/share/grub/unicode.pf2")
168 store-mount-point
169 store-directory-prefix))
170
171 (define image
172 (normalize-file (grub-background-image config)
173 store-mount-point
174 store-directory-prefix))
175
176 (and image
177 #~(format #$port "
178 # Set 'root' to the partition that contains /gnu/store.
179 ~a
180
181 ~a
182 ~a
183
184 insmod png
185 if background_image ~a; then
186 set color_normal=~a
187 set color_highlight=~a
188 else
189 set menu_color_normal=cyan/blue
190 set menu_color_highlight=white/blue
191 fi~%"
192 #$(grub-root-search store-device font-file)
193 #$(setup-gfxterm config font-file)
194 #$(grub-setup-io config)
195
196 #$image
197 #$(theme-colors grub-theme-color-normal)
198 #$(theme-colors grub-theme-color-highlight))))
199
200 \f
201 ;;;
202 ;;; Configuration file.
203 ;;;
204
205 (define* (keyboard-layout-file layout
206 #:key
207 (grub grub))
208 "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
209 and return a file in the format for GRUB keymaps. LAYOUT must be present in
210 the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'."
211 (define builder
212 (with-imported-modules '((guix build utils))
213 #~(begin
214 (use-modules (guix build utils))
215
216 ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp'
217 ;; (from the 'console-setup' package).
218 (invoke #+(file-append grub "/bin/grub-mklayout")
219 "-i" #+(keyboard-layout->console-keymap layout)
220 "-o" #$output))))
221
222 (computed-file (string-append "grub-keymap."
223 (string-map (match-lambda
224 (#\, #\-)
225 (chr chr))
226 (keyboard-layout-name layout)))
227 builder))
228
229 (define (grub-setup-io config)
230 "Return GRUB commands to configure the input / output interfaces. The result
231 is a string that can be inserted in grub.cfg."
232 (let* ((symbols->string (lambda (list)
233 (string-join (map symbol->string list) " ")))
234 (outputs (bootloader-configuration-terminal-outputs config))
235 (inputs (bootloader-configuration-terminal-inputs config))
236 (unit (bootloader-configuration-serial-unit config))
237 (speed (bootloader-configuration-serial-speed config))
238
239 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
240 ;; as documented in GRUB manual section "Simple Configuration
241 ;; Handling".
242 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
243 gfxterm vga_text mda_text morse spkmodem))
244 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
245 at_keyboard usb_keyboard))
246
247 (io (string-append
248 "terminal_output "
249 (symbols->string
250 (map
251 (lambda (output)
252 (if (memq output valid-outputs) output #f)) outputs)) "\n"
253 (if (null? inputs)
254 ""
255 (string-append
256 "terminal_input "
257 (symbols->string
258 (map
259 (lambda (input)
260 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
261 ;; UNIT and SPEED are arguments to the same GRUB command
262 ;; ("serial"), so we process them together.
263 (if (or unit speed)
264 (string-append
265 "serial"
266 (if unit
267 ;; COM ports 1 through 4
268 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
269 (string-append " --unit=" (number->string unit))
270 #f)
271 "")
272 (if speed
273 (if (exact-integer? speed)
274 (string-append " --speed=" (number->string speed))
275 #f)
276 ""))
277 ""))))
278 (format #f "~a" io)))
279
280 (define (grub-root-search device file)
281 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
282 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
283 code."
284 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
285 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
286 ;; custom menu entries. In the latter case, don't emit a 'search' command.
287 (if (and (string? file) (not (string-prefix? "/" file)))
288 ""
289 (match device
290 ;; Preferably refer to DEVICE by its UUID or label. This is more
291 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
292 ((? uuid? uuid)
293 (format #f "search --fs-uuid --set ~a"
294 (uuid->string device)))
295 ((? file-system-label? label)
296 (format #f "search --label --set ~a"
297 (file-system-label->string label)))
298 ((? (lambda (device)
299 (and (string? device) (string-contains device ":/"))) nfs-uri)
300 ;; This assumes that if your root file system is on NFS, then
301 ;; you also want to load your grub extra files, kernel and initrd
302 ;; from there.
303 ;;
304 ;; We explicitly set "root=(tftp)" here even though if grub.cfg
305 ;; had been loaded via TFTP, Grub would have set "root=(tftp)"
306 ;; automatically anyway. The reason is if you have a system that
307 ;; used to be on NFS but now is local, root would be set to local
308 ;; disk. If you then selected an older system generation that is
309 ;; supposed to boot from network in the Grub boot menu, Grub still
310 ;; wouldn't load those files from network otherwise.
311 ;;
312 ;; TFTP is preferred to HTTP because it is used more widely and
313 ;; specified in standards more widely--especially BOOTP/DHCPv4
314 ;; defines a TFTP server for DHCP option 66, but not HTTP.
315 ;;
316 ;; Note: DHCPv6 specifies option 59 to contain a boot-file-url,
317 ;; which can contain a HTTP or TFTP URL.
318 ;;
319 ;; Note: It is assumed that the file paths are of a similar
320 ;; setup on both the TFTP server and the NFS server (it is
321 ;; not possible to search for files on TFTP).
322 ;;
323 ;; TODO: Allow HTTP.
324 "set root=(tftp)")
325 ((or #f (? string?))
326 #~(format #f "search --file --set ~a" #$file)))))
327
328 (define* (grub-configuration-file config entries
329 #:key
330 (system (%current-system))
331 (old-entries '())
332 store-directory-prefix)
333 "Return the GRUB configuration file corresponding to CONFIG, a
334 <bootloader-configuration> object, and where the store is available at
335 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
336 entries corresponding to old generations of the system.
337 STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required
338 when booting a root file system on a Btrfs subvolume."
339 (define all-entries
340 (append entries (bootloader-configuration-menu-entries config)))
341 (define (menu-entry->gexp entry)
342 (let ((label (menu-entry-label entry))
343 (linux (menu-entry-linux entry))
344 (device (menu-entry-device entry))
345 (device-mount-point (menu-entry-device-mount-point entry)))
346 (if linux
347 (let ((arguments (menu-entry-linux-arguments entry))
348 (linux (normalize-file linux
349 device-mount-point
350 store-directory-prefix))
351 (initrd (normalize-file (menu-entry-initrd entry)
352 device-mount-point
353 store-directory-prefix)))
354 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
355 ;; Use the right file names for LINUX and INITRD in case
356 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
357 ;; separate partition.
358
359 ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the linux and
360 ;; initrd paths, to allow booting from a Btrfs subvolume.
361 #~(format port "menuentry ~s {
362 ~a
363 linux ~a ~a
364 initrd ~a
365 }~%"
366 #$label
367 #$(grub-root-search device linux)
368 #$linux (string-join (list #$@arguments))
369 #$initrd))
370 (let ((kernel (menu-entry-multiboot-kernel entry))
371 (arguments (menu-entry-multiboot-arguments entry))
372 (modules (menu-entry-multiboot-modules entry))
373 (root-index 1)) ; XXX EFI will need root-index 2
374 #~(format port "
375 menuentry ~s {
376 multiboot ~a root=device:hd0s~a~a~a
377 }~%"
378 #$label
379 #$kernel
380 #$root-index (string-join (list #$@arguments) " " 'prefix)
381 (string-join (map string-join '#$modules)
382 "\n module " 'prefix))))))
383
384 (define (sugar)
385 (let* ((entry (first all-entries))
386 (device (menu-entry-device entry))
387 (mount-point (menu-entry-device-mount-point entry)))
388 (eye-candy config
389 device
390 mount-point
391 #:store-directory-prefix store-directory-prefix
392 #:port #~port)))
393
394 (define keyboard-layout-config
395 (let* ((layout (bootloader-configuration-keyboard-layout config))
396 (grub (bootloader-package
397 (bootloader-configuration-bootloader config)))
398 (keymap* (and layout
399 (keyboard-layout-file layout #:grub grub)))
400 (keymap (and keymap*
401 (if store-directory-prefix
402 #~(string-append #$store-directory-prefix
403 #$keymap*)
404 keymap*))))
405 #~(when #$keymap
406 (format port "\
407 insmod keylayouts
408 keymap ~a~%" #$keymap))))
409
410 (define builder
411 #~(call-with-output-file #$output
412 (lambda (port)
413 (format port
414 "# This file was generated from your Guix configuration. Any changes
415 # will be lost upon reconfiguration.
416 ")
417 #$(sugar)
418 #$keyboard-layout-config
419 (format port "
420 set default=~a
421 set timeout=~a~%"
422 #$(bootloader-configuration-default-entry config)
423 #$(bootloader-configuration-timeout config))
424 #$@(map menu-entry->gexp all-entries)
425
426 #$@(if (pair? old-entries)
427 #~((format port "
428 submenu \"GNU system, old configurations...\" {~%")
429 #$@(map menu-entry->gexp old-entries)
430 (format port "}~%"))
431 #~())
432 (format port "
433 if [ \"${grub_platform}\" == efi ]; then
434 menuentry \"Firmware setup\" {
435 fwsetup
436 }
437 fi~%"))))
438
439 ;; Since this file is rather unique, there's no point in trying to
440 ;; substitute it.
441 (computed-file "grub.cfg" builder
442 #:options '(#:local-build? #t
443 #:substitutable? #f)))
444
445 \f
446
447 ;;;
448 ;;; Install procedures.
449 ;;;
450
451 (define install-grub
452 #~(lambda (bootloader device mount-point)
453 (let ((grub (string-append bootloader "/sbin/grub-install"))
454 (install-dir (string-append mount-point "/boot")))
455 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE
456 ;; is #f, then we populate the disk-image rooted at MOUNT-POINT.
457 (if device
458 (begin
459 ;; Tell 'grub-install' that there might be a LUKS-encrypted
460 ;; /boot or root partition.
461 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
462
463 ;; Hide potentially confusing messages from the user, such as
464 ;; "Installing for i386-pc platform."
465 (invoke/quiet grub "--no-floppy" "--target=i386-pc"
466 "--boot-directory" install-dir
467 device))
468 ;; When creating a disk-image, only install GRUB modules.
469 (copy-recursively (string-append bootloader "/lib/")
470 install-dir)))))
471
472 (define install-grub-disk-image
473 #~(lambda (bootloader root-index image)
474 ;; Install GRUB on the given IMAGE. The root partition index is
475 ;; ROOT-INDEX.
476 (let ((grub-mkimage
477 (string-append bootloader "/bin/grub-mkimage"))
478 (modules '("biosdisk" "part_msdos" "fat" "ext2"))
479 (grub-bios-setup
480 (string-append bootloader "/sbin/grub-bios-setup"))
481 (root-device (format #f "hd0,msdos~a" root-index))
482 (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img"))
483 (device-map "device.map"))
484
485 ;; Create a minimal, standalone GRUB image that will be written
486 ;; directly in the MBR-GAP (space between the end of the MBR and the
487 ;; first partition).
488 (apply invoke grub-mkimage
489 "-O" "i386-pc"
490 "-o" "core.img"
491 "-p" (format #f "(~a)/boot/grub" root-device)
492 modules)
493
494 ;; Create a device mapping file.
495 (call-with-output-file device-map
496 (lambda (port)
497 (format port "(hd0) ~a~%" image)))
498
499 ;; Copy the default boot.img, that will be written on the MBR sector
500 ;; by GRUB-BIOS-SETUP.
501 (copy-file boot-img "boot.img")
502
503 ;; Install both the "boot.img" and the "core.img" files on the given
504 ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB
505 ;; written in the MBR-GAP. GRUB configuration and missing modules will
506 ;; be read from ROOT-DEVICE.
507 (invoke grub-bios-setup
508 "-m" device-map
509 "-r" root-device
510 "-d" "."
511 image))))
512
513 (define install-grub-efi
514 #~(lambda (bootloader efi-dir mount-point)
515 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
516 ;; system whose root is mounted at MOUNT-POINT.
517 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
518 (install-dir (string-append mount-point "/boot"))
519 ;; When installing Guix, it's common to mount EFI-DIR below
520 ;; MOUNT-POINT rather than /boot/efi on the live image.
521 (target-esp (if (file-exists? (string-append mount-point efi-dir))
522 (string-append mount-point efi-dir)
523 efi-dir)))
524 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
525 ;; root partition.
526 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
527 (invoke/quiet grub-install "--boot-directory" install-dir
528 "--bootloader-id=Guix"
529 "--efi-directory" target-esp))))
530
531 \f
532
533 ;;;
534 ;;; Bootloader definitions.
535 ;;;
536 ;;; For all these grub-bootloader variables the path to /boot/grub/grub.cfg
537 ;;; is fixed. Inheriting and overwriting the field 'configuration-file' will
538 ;;; break 'guix system delete-generations', 'guix system switch-generation',
539 ;;; and 'guix system roll-back'.
540
541 (define grub-bootloader
542 (bootloader
543 (name 'grub)
544 (package grub)
545 (installer install-grub)
546 (disk-image-installer install-grub-disk-image)
547 (configuration-file "/boot/grub/grub.cfg")
548 (configuration-file-generator grub-configuration-file)))
549
550 (define grub-minimal-bootloader
551 (bootloader
552 (inherit grub-bootloader)
553 (package grub-minimal)))
554
555 (define grub-efi-bootloader
556 (bootloader
557 (inherit grub-bootloader)
558 (installer install-grub-efi)
559 (disk-image-installer #f)
560 (name 'grub-efi)
561 (package grub-efi)))
562
563 (define grub-mkrescue-bootloader
564 (bootloader
565 (inherit grub-efi-bootloader)
566 (package grub-hybrid)))
567
568 \f
569 ;;;
570 ;;; Compatibility macros.
571 ;;;
572
573 (define-syntax grub-configuration
574 (syntax-rules (grub)
575 ((_ (grub package) fields ...)
576 (if (eq? package grub)
577 (bootloader-configuration
578 (bootloader grub-bootloader)
579 fields ...)
580 (bootloader-configuration
581 (bootloader grub-efi-bootloader)
582 fields ...)))
583 ((_ fields ...)
584 (bootloader-configuration
585 (bootloader grub-bootloader)
586 fields ...))))
587
588 ;;; grub.scm ends here