gnu: r-imager: Update to 0.42.8.
[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 © 2019, 2020 Miguel Ángel Arruga Vivas <rosen644835@gmail.com>
8 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
9 ;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
10 ;;;
11 ;;; This file is part of GNU Guix.
12 ;;;
13 ;;; GNU Guix is free software; you can redistribute it and/or modify it
14 ;;; under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 3 of the License, or (at
16 ;;; your option) any later version.
17 ;;;
18 ;;; GNU Guix is distributed in the hope that it will be useful, but
19 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25
26 (define-module (gnu bootloader grub)
27 #:use-module (guix build union)
28 #:use-module (guix records)
29 #:use-module (guix store)
30 #:use-module (guix utils)
31 #:use-module (guix gexp)
32 #:use-module (gnu artwork)
33 #:use-module (gnu bootloader)
34 #:use-module (gnu system uuid)
35 #:use-module (gnu system file-systems)
36 #:use-module (gnu system keyboard)
37 #:use-module (gnu system locale)
38 #:use-module (gnu packages bootloaders)
39 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
40 #:autoload (gnu packages xorg) (xkeyboard-config)
41 #:use-module (ice-9 match)
42 #:use-module (ice-9 regex)
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-2)
45 #:export (grub-theme
46 grub-theme?
47 grub-theme-image
48 grub-theme-resolution
49 grub-theme-color-normal
50 grub-theme-color-highlight
51 grub-theme-gfxmode
52
53 install-grub-efi-netboot
54
55 grub-bootloader
56 grub-efi-bootloader
57 grub-efi-netboot-bootloader
58 grub-mkrescue-bootloader
59 grub-minimal-bootloader
60
61 grub-configuration))
62
63 ;;; Commentary:
64 ;;;
65 ;;; Configuration of GNU GRUB.
66 ;;;
67 ;;; Code:
68
69 (define* (normalize-file file mount-point store-directory-prefix)
70 "Strip MOUNT-POINT and prepend STORE-DIRECTORY-PREFIX, if any, to FILE, a
71 G-expression or other lowerable object denoting a file name."
72
73 (define (strip-mount-point mount-point file)
74 (if mount-point
75 (if (string=? mount-point "/")
76 file
77 #~(let ((file #$file))
78 (if (string-prefix? #$mount-point file)
79 (substring #$file #$(string-length mount-point))
80 file)))
81 file))
82
83 (define (prepend-store-directory-prefix store-directory-prefix file)
84 (if store-directory-prefix
85 #~(string-append #$store-directory-prefix #$file)
86 file))
87
88 (prepend-store-directory-prefix store-directory-prefix
89 (strip-mount-point mount-point file)))
90
91
92
93 (define-record-type* <grub-theme>
94 ;; Default theme contributed by Felipe López.
95 grub-theme make-grub-theme
96 grub-theme?
97 (image grub-theme-image
98 (default (file-append %artwork-repository
99 "/grub/GuixSD-fully-black-4-3.svg")))
100 (resolution grub-theme-resolution
101 (default '(1024 . 768)))
102 (color-normal grub-theme-color-normal
103 (default '((fg . light-gray) (bg . black))))
104 (color-highlight grub-theme-color-highlight
105 (default '((fg . yellow) (bg . black))))
106 (gfxmode grub-theme-gfxmode
107 (default '("auto")))) ;list of string
108
109 \f
110 ;;;
111 ;;; Background image & themes.
112 ;;;
113
114 (define (bootloader-theme config)
115 "Return user defined theme in CONFIG if defined or a default theme
116 otherwise."
117 (or (bootloader-configuration-theme config) (grub-theme)))
118
119 (define* (image->png image #:key width height)
120 "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\".
121 Otherwise the picture in IMAGE is just copied."
122 (computed-file "grub-image.png"
123 (with-imported-modules '((gnu build svg))
124 (with-extensions (list guile-rsvg guile-cairo)
125 #~(if (string-suffix? ".svg" #+image)
126 (begin
127 (use-modules (gnu build svg))
128 (svg->png #+image #$output
129 #:width #$width
130 #:height #$height))
131 (copy-file #+image #$output))))))
132
133 (define* (grub-background-image config)
134 "Return the GRUB background image defined in CONFIG or #f if none was found.
135 If the suffix of the image file is \".svg\", then it is converted into a PNG
136 file with the resolution provided in CONFIG."
137 (let* ((theme (bootloader-theme config))
138 (image (grub-theme-image theme)))
139 (and image
140 (match (grub-theme-resolution theme)
141 (((? number? width) . (? number? height))
142 (image->png image #:width width #:height height))
143 (_ #f)))))
144
145 (define (grub-locale-directory grub)
146 "Generate a directory with the locales from GRUB."
147 (define builder
148 #~(begin
149 (use-modules (ice-9 ftw))
150 (let ((locale (string-append #$grub "/share/locale"))
151 (out #$output))
152 (mkdir out)
153 (chdir out)
154 (for-each (lambda (lang)
155 (let ((file (string-append locale "/" lang
156 "/LC_MESSAGES/grub.mo"))
157 (dest (string-append lang ".mo")))
158 (when (file-exists? file)
159 (copy-file file dest))))
160 (scandir locale)))))
161 (computed-file "grub-locales" builder))
162
163 (define* (eye-candy config store-device store-mount-point
164 #:key store-directory-prefix port)
165 "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part
166 concerned with graphics mode, background images, colors, and all that.
167 STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is
168 its mount point; these are used to determine where the background image and
169 fonts must be searched for. STORE-DIRECTORY-PREFIX is a directory prefix to
170 prepend to any store file name."
171 (define (setup-gfxterm config)
172 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
173 #~(format #f "
174 if loadfont unicode; then
175 set gfxmode=~a
176 insmod all_video
177 insmod gfxterm
178 fi~%"
179 #$(string-join
180 (grub-theme-gfxmode (bootloader-theme config))
181 ";"))
182 ""))
183
184 (define (theme-colors type)
185 (let* ((theme (bootloader-theme config))
186 (colors (type theme)))
187 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
188 (symbol->string (assoc-ref colors 'bg)))))
189
190 (define image
191 (normalize-file (grub-background-image config)
192 store-mount-point
193 store-directory-prefix))
194
195 (and image
196 #~(format #$port "
197 # Set 'root' to the partition that contains /gnu/store.
198 ~a
199
200 ~a
201 ~a
202
203 insmod png
204 if background_image ~a; then
205 set color_normal=~a
206 set color_highlight=~a
207 else
208 set menu_color_normal=cyan/blue
209 set menu_color_highlight=white/blue
210 fi~%"
211 #$(grub-root-search store-device image)
212 #$(setup-gfxterm config)
213 #$(grub-setup-io config)
214
215 #$image
216 #$(theme-colors grub-theme-color-normal)
217 #$(theme-colors grub-theme-color-highlight))))
218
219 \f
220 ;;;
221 ;;; Configuration file.
222 ;;;
223
224 (define* (keyboard-layout-file layout
225 #:key
226 (grub grub))
227 "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
228 and return a file in the format for GRUB keymaps. LAYOUT must be present in
229 the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'."
230 (define builder
231 (with-imported-modules '((guix build utils))
232 #~(begin
233 (use-modules (guix build utils))
234
235 ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp'
236 ;; (from the 'console-setup' package).
237 (invoke #+(file-append grub "/bin/grub-mklayout")
238 "-i" #+(keyboard-layout->console-keymap layout)
239 "-o" #$output))))
240
241 (computed-file (string-append "grub-keymap."
242 (string-map (match-lambda
243 (#\, #\-)
244 (chr chr))
245 (keyboard-layout-name layout)))
246 builder))
247
248 (define (grub-setup-io config)
249 "Return GRUB commands to configure the input / output interfaces. The result
250 is a string that can be inserted in grub.cfg."
251 (let* ((symbols->string (lambda (list)
252 (string-join (map symbol->string list) " ")))
253 (outputs (bootloader-configuration-terminal-outputs config))
254 (inputs (bootloader-configuration-terminal-inputs config))
255 (unit (bootloader-configuration-serial-unit config))
256 (speed (bootloader-configuration-serial-speed config))
257
258 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
259 ;; as documented in GRUB manual section "Simple Configuration
260 ;; Handling".
261 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
262 gfxterm vga_text mda_text morse spkmodem))
263 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
264 at_keyboard usb_keyboard))
265
266 (io (string-append
267 "terminal_output "
268 (symbols->string
269 (map
270 (lambda (output)
271 (if (memq output valid-outputs) output #f)) outputs)) "\n"
272 (if (null? inputs)
273 ""
274 (string-append
275 "terminal_input "
276 (symbols->string
277 (map
278 (lambda (input)
279 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
280 ;; UNIT and SPEED are arguments to the same GRUB command
281 ;; ("serial"), so we process them together.
282 (if (or unit speed)
283 (string-append
284 "serial"
285 (if unit
286 ;; COM ports 1 through 4
287 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
288 (string-append " --unit=" (number->string unit))
289 #f)
290 "")
291 (if speed
292 (if (exact-integer? speed)
293 (string-append " --speed=" (number->string speed))
294 #f)
295 ""))
296 ""))))
297 (format #f "~a" io)))
298
299 (define (grub-root-search device file)
300 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
301 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
302 code."
303 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
304 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
305 ;; custom menu entries. In the latter case, don't emit a 'search' command.
306 (if (and (string? file) (not (string-prefix? "/" file)))
307 ""
308 (match device
309 ;; Preferably refer to DEVICE by its UUID or label. This is more
310 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
311 ((? uuid? uuid)
312 (format #f "search --fs-uuid --set ~a"
313 (uuid->string device)))
314 ((? file-system-label? label)
315 (format #f "search --label --set ~a"
316 (file-system-label->string label)))
317 ((? (lambda (device)
318 (and (string? device) (string-contains device ":/"))) nfs-uri)
319 ;; If the device is an NFS share, then we assume that the expected
320 ;; file on that device (e.g. the GRUB background image or the kernel)
321 ;; has to be loaded over the network. Otherwise we would need an
322 ;; additional device information for some local disk to look for that
323 ;; file, which we do not have.
324 ;;
325 ;; We explicitly set "root=(tftp)" here even though if grub.cfg
326 ;; had been loaded via TFTP, Grub would have set "root=(tftp)"
327 ;; automatically anyway. The reason is if you have a system that
328 ;; used to be on NFS but now is local, root would be set to local
329 ;; disk. If you then selected an older system generation that is
330 ;; supposed to boot from network in the Grub boot menu, Grub still
331 ;; wouldn't load those files from network otherwise.
332 ;;
333 ;; TFTP is preferred to HTTP because it is used more widely and
334 ;; specified in standards more widely--especially BOOTP/DHCPv4
335 ;; defines a TFTP server for DHCP option 66, but not HTTP.
336 ;;
337 ;; Note: DHCPv6 specifies option 59 to contain a boot-file-url,
338 ;; which can contain a HTTP or TFTP URL.
339 ;;
340 ;; Note: It is assumed that the file paths are of a similar
341 ;; setup on both the TFTP server and the NFS server (it is
342 ;; not possible to search for files on TFTP).
343 ;;
344 ;; TODO: Allow HTTP.
345 "set root=(tftp)")
346 ((or #f (? string?))
347 #~(format #f "search --file --set ~a" #$file)))))
348
349 (define* (grub-configuration-file config entries
350 #:key
351 (locale #f)
352 (system (%current-system))
353 (old-entries '())
354 (store-crypto-devices '())
355 store-directory-prefix)
356 "Return the GRUB configuration file corresponding to CONFIG, a
357 <bootloader-configuration> object, and where the store is available at
358 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
359 entries corresponding to old generations of the system.
360 STORE-CRYPTO-DEVICES contain the UUIDs of the encrypted units that must
361 be unlocked to access the store contents.
362 STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required
363 when booting a root file system on a Btrfs subvolume."
364 (define all-entries
365 (append entries (bootloader-configuration-menu-entries config)))
366 (define (menu-entry->gexp entry)
367 (let ((label (menu-entry-label entry))
368 (linux (menu-entry-linux entry))
369 (device (menu-entry-device entry))
370 (device-mount-point (menu-entry-device-mount-point entry)))
371 (if linux
372 (let ((arguments (menu-entry-linux-arguments entry))
373 (linux (normalize-file linux
374 device-mount-point
375 store-directory-prefix))
376 (initrd (normalize-file (menu-entry-initrd entry)
377 device-mount-point
378 store-directory-prefix)))
379 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
380 ;; Use the right file names for LINUX and INITRD in case
381 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
382 ;; separate partition.
383
384 ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the linux and
385 ;; initrd paths, to allow booting from a Btrfs subvolume.
386 #~(format port "menuentry ~s {
387 ~a
388 linux ~a ~a
389 initrd ~a
390 }~%"
391 #$label
392 #$(grub-root-search device linux)
393 #$linux (string-join (list #$@arguments))
394 #$initrd))
395 (let ((kernel (menu-entry-multiboot-kernel entry))
396 (arguments (menu-entry-multiboot-arguments entry))
397 (modules (menu-entry-multiboot-modules entry))
398 (root-index 1)) ; XXX EFI will need root-index 2
399 #~(format port "
400 menuentry ~s {
401 multiboot ~a root=device:hd0s~a~a~a
402 }~%"
403 #$label
404 #$kernel
405 #$root-index (string-join (list #$@arguments) " " 'prefix)
406 (string-join (map string-join '#$modules)
407 "\n module " 'prefix))))))
408
409 (define (crypto-devices)
410 (define (crypto-device->cryptomount dev)
411 (if (uuid? dev)
412 #~(format port "cryptomount -u ~a~%"
413 ;; cryptomount only accepts UUID without the hypen.
414 #$(string-delete #\- (uuid->string dev)))
415 ;; Other type of devices aren't implemented.
416 #~()))
417 (let ((devices (map crypto-device->cryptomount store-crypto-devices))
418 ;; XXX: Add luks2 when grub 2.06 is packaged.
419 (modules #~(format port "insmod luks~%")))
420 (if (null? devices)
421 devices
422 (cons modules devices))))
423
424 (define (sugar)
425 (let* ((entry (first all-entries))
426 (device (menu-entry-device entry))
427 (mount-point (menu-entry-device-mount-point entry)))
428 (eye-candy config
429 device
430 mount-point
431 #:store-directory-prefix store-directory-prefix
432 #:port #~port)))
433
434 (define locale-config
435 (let* ((entry (first all-entries))
436 (device (menu-entry-device entry))
437 (mount-point (menu-entry-device-mount-point entry))
438 (bootloader (bootloader-configuration-bootloader config))
439 (grub (bootloader-package bootloader)))
440 #~(let ((locale #$(and locale
441 (locale-definition-source
442 (locale-name->definition locale))))
443 (locales #$(and locale
444 (normalize-file (grub-locale-directory grub)
445 mount-point
446 store-directory-prefix))))
447 (when locale
448 (format port "\
449 # Localization configuration.
450 ~asearch --file --set ~a/en@quot.mo
451 set locale_dir=~a
452 set lang=~a~%"
453 ;; Skip the search if there is an image, as it has already
454 ;; been performed by eye-candy and traversing the store is
455 ;; an expensive operation.
456 #$(if (grub-theme-image (bootloader-theme config))
457 "# "
458 "")
459 locales
460 locales
461 locale)))))
462
463 (define keyboard-layout-config
464 (let* ((layout (bootloader-configuration-keyboard-layout config))
465 (grub (bootloader-package
466 (bootloader-configuration-bootloader config)))
467 (keymap* (and layout
468 (keyboard-layout-file layout #:grub grub)))
469 (entry (first all-entries))
470 (device (menu-entry-device entry))
471 (mount-point (menu-entry-device-mount-point entry))
472 (keymap (and keymap*
473 (normalize-file keymap* mount-point
474 store-directory-prefix))))
475 #~(when #$keymap
476 (format port "\
477 insmod keylayouts
478 keymap ~a~%" #$keymap))))
479
480 (define builder
481 #~(call-with-output-file #$output
482 (lambda (port)
483 (format port
484 "# This file was generated from your Guix configuration. Any changes
485 # will be lost upon reconfiguration.
486 ")
487 #$@(crypto-devices)
488 #$(sugar)
489 #$locale-config
490 #$keyboard-layout-config
491 (format port "
492 set default=~a
493 set timeout=~a~%"
494 #$(bootloader-configuration-default-entry config)
495 #$(bootloader-configuration-timeout config))
496 #$@(map menu-entry->gexp all-entries)
497
498 #$@(if (pair? old-entries)
499 #~((format port "
500 submenu \"GNU system, old configurations...\" {~%")
501 #$@(map menu-entry->gexp old-entries)
502 (format port "}~%"))
503 #~())
504 (format port "
505 if [ \"${grub_platform}\" == efi ]; then
506 menuentry \"Firmware setup\" {
507 fwsetup
508 }
509 fi~%"))))
510
511 ;; Since this file is rather unique, there's no point in trying to
512 ;; substitute it.
513 (computed-file "grub.cfg" builder
514 #:options '(#:local-build? #t
515 #:substitutable? #f)))
516
517 \f
518
519 ;;;
520 ;;; Install procedures.
521 ;;;
522
523 (define install-grub
524 #~(lambda (bootloader device mount-point)
525 (let ((grub (string-append bootloader "/sbin/grub-install"))
526 (install-dir (string-append mount-point "/boot")))
527 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE
528 ;; is #f, then we populate the disk-image rooted at MOUNT-POINT.
529 (if device
530 (begin
531 ;; Tell 'grub-install' that there might be a LUKS-encrypted
532 ;; /boot or root partition.
533 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
534
535 ;; Hide potentially confusing messages from the user, such as
536 ;; "Installing for i386-pc platform."
537 (invoke/quiet grub "--no-floppy" "--target=i386-pc"
538 "--boot-directory" install-dir
539 device))
540 ;; When creating a disk-image, only install a font and GRUB modules.
541 (let* ((fonts (string-append install-dir "/grub/fonts")))
542 (mkdir-p fonts)
543 (copy-file (string-append bootloader "/share/grub/unicode.pf2")
544 (string-append fonts "/unicode.pf2"))
545 (copy-recursively (string-append bootloader "/lib/")
546 install-dir))))))
547
548 (define install-grub-disk-image
549 #~(lambda (bootloader root-index image)
550 ;; Install GRUB on the given IMAGE. The root partition index is
551 ;; ROOT-INDEX.
552 (let ((grub-mkimage
553 (string-append bootloader "/bin/grub-mkimage"))
554 (modules '("biosdisk" "part_msdos" "fat" "ext2"))
555 (grub-bios-setup
556 (string-append bootloader "/sbin/grub-bios-setup"))
557 (root-device (format #f "hd0,msdos~a" root-index))
558 (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img"))
559 (device-map "device.map"))
560
561 ;; Create a minimal, standalone GRUB image that will be written
562 ;; directly in the MBR-GAP (space between the end of the MBR and the
563 ;; first partition).
564 (apply invoke grub-mkimage
565 "-O" "i386-pc"
566 "-o" "core.img"
567 "-p" (format #f "(~a)/boot/grub" root-device)
568 modules)
569
570 ;; Create a device mapping file.
571 (call-with-output-file device-map
572 (lambda (port)
573 (format port "(hd0) ~a~%" image)))
574
575 ;; Copy the default boot.img, that will be written on the MBR sector
576 ;; by GRUB-BIOS-SETUP.
577 (copy-file boot-img "boot.img")
578
579 ;; Install both the "boot.img" and the "core.img" files on the given
580 ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB
581 ;; written in the MBR-GAP. GRUB configuration and missing modules will
582 ;; be read from ROOT-DEVICE.
583 (invoke grub-bios-setup
584 "-m" device-map
585 "-r" root-device
586 "-d" "."
587 image))))
588
589 (define install-grub-efi
590 #~(lambda (bootloader efi-dir mount-point)
591 ;; There is nothing useful to do when called in the context of a disk
592 ;; image generation.
593 (when efi-dir
594 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
595 ;; system whose root is mounted at MOUNT-POINT.
596 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
597 (install-dir (string-append mount-point "/boot"))
598 ;; When installing Guix, it's common to mount EFI-DIR below
599 ;; MOUNT-POINT rather than /boot/efi on the live image.
600 (target-esp (if (file-exists? (string-append mount-point efi-dir))
601 (string-append mount-point efi-dir)
602 efi-dir)))
603 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
604 ;; root partition.
605 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
606 (invoke/quiet grub-install "--boot-directory" install-dir
607 "--bootloader-id=Guix"
608 "--efi-directory" target-esp)))))
609
610 (define (install-grub-efi-netboot subdir)
611 "Define a grub-efi-netboot bootloader installer for installation in SUBDIR,
612 which is usually efi/Guix or efi/boot."
613 (let* ((system (string-split (nix-system->gnu-triplet
614 (or (%current-target-system)
615 (%current-system)))
616 #\-))
617 (arch (first system))
618 (boot-efi-link (match system
619 ;; These are the supportend systems and the names
620 ;; defined by the UEFI standard for removable media.
621 (("i686" _ ...) "/bootia32.efi")
622 (("x86_64" _ ...) "/bootx64.efi")
623 (("arm" _ ...) "/bootarm.efi")
624 (("aarch64" _ ...) "/bootaa64.efi")
625 (("riscv" _ ...) "/bootriscv32.efi")
626 (("riscv64" _ ...) "/bootriscv64.efi")
627 ;; Other systems are not supported, although defined.
628 ;; (("riscv128" _ ...) "/bootriscv128.efi")
629 ;; (("ia64" _ ...) "/bootia64.efi")
630 ((_ ...) #f)))
631 (core-efi (string-append
632 ;; This is the arch dependent file name of GRUB, e.g.
633 ;; i368-efi/core.efi or arm64-efi/core.efi.
634 (match arch
635 ("i686" "i386")
636 ("aarch64" "arm64")
637 ("riscv" "riscv32")
638 (_ arch))
639 "-efi/core.efi")))
640 (with-imported-modules
641 '((guix build union))
642 #~(lambda (bootloader target mount-point)
643 "Install the BOOTLOADER, which must be the package grub, as e.g.
644 bootx64.efi or bootaa64.efi into SUBDIR, which is usually efi/Guix or efi/boot,
645 below the directory TARGET for the system whose root is mounted at MOUNT-POINT.
646
647 MOUNT-POINT is the last argument in 'guix system init /etc/config.scm mnt/point'
648 or '/' for other 'guix system' commands.
649
650 TARGET is the target argument given to the bootloader-configuration in
651
652 (operating-system
653 (bootloader (bootloader-configuration
654 (target \"/boot\")
655 …))
656 …)
657
658 TARGET is required to be an absolute directory name, usually mounted via NFS,
659 and finally needs to be provided by a TFTP server as the TFTP root directory.
660
661 GRUB will load tftp://server/SUBDIR/grub.cfg and this file will instruct it to
662 load more files from the store like tftp://server/gnu/store/…-linux…/Image.
663
664 To make this possible two symlinks will be created. The first symlink points
665 relatively form MOUNT-POINT/TARGET/SUBDIR/grub.cfg to
666 MOUNT-POINT/boot/grub/grub.cfg, and the second symlink points relatively from
667 MOUNT-POINT/TARGET/%store-prefix to MOUNT-POINT/%store-prefix.
668
669 It is important to note that these symlinks need to be relativ, as the absolute
670 paths on the TFTP server side are unknown.
671
672 It is also important to note that both symlinks will point outside the TFTP root
673 directory and that the TARGET/%store-prefix symlink makes the whole store
674 accessible via TFTP. Possibly the TFTP server must be configured
675 to allow accesses outside its TFTP root directory. This may need to be
676 considered for security aspects."
677 (use-modules ((guix build union) #:select (symlink-relative)))
678 (let* ((net-dir (string-append mount-point target "/"))
679 (sub-dir (string-append net-dir #$subdir "/"))
680 (store (string-append mount-point (%store-prefix)))
681 (store-link (string-append net-dir (%store-prefix)))
682 (grub-cfg (string-append mount-point "/boot/grub/grub.cfg"))
683 (grub-cfg-link (string-append sub-dir (basename grub-cfg)))
684 (boot-efi-link (string-append sub-dir #$boot-efi-link)))
685 ;; Prepare the symlink to the store.
686 (mkdir-p (dirname store-link))
687 (false-if-exception (delete-file store-link))
688 (symlink-relative store store-link)
689 ;; Prepare the symlink to the grub.cfg, which points into the store.
690 (mkdir-p (dirname grub-cfg-link))
691 (false-if-exception (delete-file grub-cfg-link))
692 (symlink-relative grub-cfg grub-cfg-link)
693 ;; Install GRUB, which refers to the grub.cfg, with support for
694 ;; encrypted partitions,
695 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
696 (invoke/quiet (string-append bootloader "/bin/grub-mknetdir")
697 (string-append "--net-directory=" net-dir)
698 (string-append "--subdir=" #$subdir))
699 ;; Prepare the bootloader symlink, which points to core.efi of GRUB.
700 (false-if-exception (delete-file boot-efi-link))
701 (symlink #$core-efi boot-efi-link))))))
702
703 \f
704
705 ;;;
706 ;;; Bootloader definitions.
707 ;;;
708 ;;; For all these grub-bootloader variables the path to /boot/grub/grub.cfg
709 ;;; is fixed. Inheriting and overwriting the field 'configuration-file' will
710 ;;; break 'guix system delete-generations', 'guix system switch-generation',
711 ;;; and 'guix system roll-back'.
712
713 (define grub-bootloader
714 (bootloader
715 (name 'grub)
716 (package grub)
717 (installer install-grub)
718 (disk-image-installer install-grub-disk-image)
719 (configuration-file "/boot/grub/grub.cfg")
720 (configuration-file-generator grub-configuration-file)))
721
722 (define grub-minimal-bootloader
723 (bootloader
724 (inherit grub-bootloader)
725 (package grub-minimal)))
726
727 (define grub-efi-bootloader
728 (bootloader
729 (inherit grub-bootloader)
730 (installer install-grub-efi)
731 (disk-image-installer #f)
732 (name 'grub-efi)
733 (package grub-efi)))
734
735 (define grub-efi-netboot-bootloader
736 (bootloader
737 (inherit grub-efi-bootloader)
738 (name 'grub-efi-netboot-bootloader)
739 (installer (install-grub-efi-netboot "efi/Guix"))))
740
741 (define grub-mkrescue-bootloader
742 (bootloader
743 (inherit grub-efi-bootloader)
744 (package grub-hybrid)))
745
746 \f
747 ;;;
748 ;;; Compatibility macros.
749 ;;;
750
751 (define-syntax grub-configuration
752 (syntax-rules (grub)
753 ((_ (grub package) fields ...)
754 (if (eq? package grub)
755 (bootloader-configuration
756 (bootloader grub-bootloader)
757 fields ...)
758 (bootloader-configuration
759 (bootloader grub-efi-bootloader)
760 fields ...)))
761 ((_ fields ...)
762 (bootloader-configuration
763 (bootloader grub-bootloader)
764 fields ...))))
765
766 ;;; grub.scm ends here