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>
10 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
45 grub-theme-color-normal
46 grub-theme-color-highlight
51 grub-mkrescue-bootloader
52 grub-minimal-bootloader
58 ;;; Configuration of GNU GRUB.
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."
66 (define (strip-mount-point mount-point file)
68 (if (string=? mount-point "/")
70 #~(let ((file #$file))
71 (if (string-prefix? #$mount-point file)
72 (substring #$file #$(string-length mount-point))
76 (define (prepend-store-directory-prefix store-directory-prefix file)
77 (if store-directory-prefix
78 #~(string-append #$store-directory-prefix #$file)
81 (prepend-store-directory-prefix store-directory-prefix
82 (strip-mount-point mount-point file)))
86 (define-record-type* <grub-theme>
87 ;; Default theme contributed by Felipe López.
88 grub-theme make-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
104 ;;; Background image & themes.
107 (define (bootloader-theme config)
108 "Return user defined theme in CONFIG if defined or a default theme
110 (or (bootloader-configuration-theme config) (grub-theme)))
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)
120 (use-modules (gnu build svg))
121 (svg->png #+image #$output
124 (copy-file #+image #$output))))))
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)))
133 (match (grub-theme-resolution theme)
134 (((? number? width) . (? number? height))
135 (image->png image #:width width #:height height))
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))
156 (grub-theme-gfxmode (bootloader-theme config))
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)))))
167 (normalize-file (file-append grub "/share/grub/unicode.pf2")
169 store-directory-prefix))
172 (normalize-file (grub-background-image config)
174 store-directory-prefix))
178 # Set 'root' to the partition that contains /gnu/store.
185 if background_image ~a; then
187 set color_highlight=~a
189 set menu_color_normal=cyan/blue
190 set menu_color_highlight=white/blue
192 #$(grub-root-search store-device font-file)
193 #$(setup-gfxterm config font-file)
194 #$(grub-setup-io config)
197 #$(theme-colors grub-theme-color-normal)
198 #$(theme-colors grub-theme-color-highlight))))
202 ;;; Configuration file.
205 (define* (keyboard-layout-file layout
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'."
212 (with-imported-modules '((guix build utils))
214 (use-modules (guix build utils))
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)
222 (computed-file (string-append "grub-keymap."
223 (string-map (match-lambda
226 (keyboard-layout-name layout)))
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))
239 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
240 ;; as documented in GRUB manual section "Simple Configuration
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))
252 (if (memq output valid-outputs) output #f)) outputs)) "\n"
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.
267 ;; COM ports 1 through 4
268 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
269 (string-append " --unit=" (number->string unit))
273 (if (exact-integer? speed)
274 (string-append " --speed=" (number->string speed))
278 (format #f "~a" io)))
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
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)))
290 ;; Preferably refer to DEVICE by its UUID or label. This is more
291 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
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)))
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
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.
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.
316 ;; Note: DHCPv6 specifies option 59 to contain a boot-file-url,
317 ;; which can contain a HTTP or TFTP URL.
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).
326 #~(format #f "search --file --set ~a" #$file)))))
328 (define* (grub-configuration-file config entries
330 (system (%current-system))
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."
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)))
347 (let ((arguments (menu-entry-linux-arguments entry))
348 (linux (normalize-file linux
350 store-directory-prefix))
351 (initrd (normalize-file (menu-entry-initrd entry)
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.
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 {
367 #$(grub-root-search device linux)
368 #$linux (string-join (list #$@arguments))
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
376 multiboot ~a root=device:hd0s~a~a~a
380 #$root-index (string-join (list #$@arguments) " " 'prefix)
381 (string-join (map string-join '#$modules)
382 "\n module " 'prefix))))))
385 (let* ((entry (first all-entries))
386 (device (menu-entry-device entry))
387 (mount-point (menu-entry-device-mount-point entry)))
391 #:store-directory-prefix store-directory-prefix
394 (define keyboard-layout-config
395 (let* ((layout (bootloader-configuration-keyboard-layout config))
396 (grub (bootloader-package
397 (bootloader-configuration-bootloader config)))
399 (keyboard-layout-file layout #:grub grub)))
401 (if store-directory-prefix
402 #~(string-append #$store-directory-prefix
408 keymap ~a~%" #$keymap))))
411 #~(call-with-output-file #$output
414 "# This file was generated from your Guix configuration. Any changes
415 # will be lost upon reconfiguration.
418 #$keyboard-layout-config
422 #$(bootloader-configuration-default-entry config)
423 #$(bootloader-configuration-timeout config))
424 #$@(map menu-entry->gexp all-entries)
426 #$@(if (pair? old-entries)
428 submenu \"GNU system, old configurations...\" {~%")
429 #$@(map menu-entry->gexp old-entries)
433 if [ \"${grub_platform}\" == efi ]; then
434 menuentry \"Firmware setup\" {
439 ;; Since this file is rather unique, there's no point in trying to
441 (computed-file "grub.cfg" builder
442 #:options '(#:local-build? #t
443 #:substitutable? #f)))
448 ;;; Install procedures.
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.
459 ;; Tell 'grub-install' that there might be a LUKS-encrypted
460 ;; /boot or root partition.
461 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
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
468 ;; When creating a disk-image, only install GRUB modules.
469 (copy-recursively (string-append bootloader "/lib/")
472 (define install-grub-disk-image
473 #~(lambda (bootloader root-index image)
474 ;; Install GRUB on the given IMAGE. The root partition index is
477 (string-append bootloader "/bin/grub-mkimage"))
478 (modules '("biosdisk" "part_msdos" "fat" "ext2"))
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"))
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
488 (apply invoke grub-mkimage
491 "-p" (format #f "(~a)/boot/grub" root-device)
494 ;; Create a device mapping file.
495 (call-with-output-file device-map
497 (format port "(hd0) ~a~%" image)))
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")
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
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)
524 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
526 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
527 (invoke/quiet grub-install "--boot-directory" install-dir
528 "--bootloader-id=Guix"
529 "--efi-directory" target-esp))))
534 ;;; Bootloader definitions.
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'.
541 (define grub-bootloader
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)))
550 (define grub-minimal-bootloader
552 (inherit grub-bootloader)
553 (package grub-minimal)))
555 (define grub-efi-bootloader
557 (inherit grub-bootloader)
558 (installer install-grub-efi)
559 (disk-image-installer #f)
563 (define grub-mkrescue-bootloader
565 (inherit grub-efi-bootloader)
566 (package grub-hybrid)))
570 ;;; Compatibility macros.
573 (define-syntax grub-configuration
575 ((_ (grub package) fields ...)
576 (if (eq? package grub)
577 (bootloader-configuration
578 (bootloader grub-bootloader)
580 (bootloader-configuration
581 (bootloader grub-efi-bootloader)
584 (bootloader-configuration
585 (bootloader grub-bootloader)
588 ;;; grub.scm ends here