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>
9 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
44 grub-theme-color-normal
45 grub-theme-color-highlight
50 grub-mkrescue-bootloader
51 grub-minimal-bootloader
57 ;;; Configuration of GNU GRUB.
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."
65 (define (strip-mount-point mount-point file)
67 (if (string=? mount-point "/")
69 #~(let ((file #$file))
70 (if (string-prefix? #$mount-point file)
71 (substring #$file #$(string-length mount-point))
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)
80 (prepend-btrfs-subvolume-file-name btrfs-subvolume-file-name
81 (strip-mount-point mount-point file)))
85 (define-record-type* <grub-theme>
86 ;; Default theme contributed by Felipe López.
87 grub-theme make-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
103 ;;; Background image & themes.
106 (define (bootloader-theme config)
107 "Return user defined theme in CONFIG if defined or a default theme
109 (or (bootloader-configuration-theme config) (grub-theme)))
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)
119 (use-modules (gnu build svg))
120 (svg->png #+image #$output
123 (copy-file #+image #$output))))))
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)))
132 (match (grub-theme-resolution theme)
133 (((? number? width) . (? number? height))
134 (image->png image #:width width #:height height))
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
148 (or (and-let* ((theme (bootloader-configuration-theme config))
149 (gfxmode (grub-theme-gfxmode theme)))
150 (string-join gfxmode ";"))
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.
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)
165 insmod gfxterm~%" gfxmode)
168 (define (setup-gfxterm config font-file)
169 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
170 #~(format #f "if loadfont ~a; then
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)))))
182 (normalize-file (file-append grub "/share/grub/unicode.pf2")
184 btrfs-store-subvolume-file-name))
187 (normalize-file (grub-background-image config)
189 btrfs-store-subvolume-file-name))
193 function setup_gfxterm {~a}
195 # Set 'root' to the partition that contains /gnu/store.
202 if background_image ~a; then
204 set color_highlight=~a
206 set menu_color_normal=cyan/blue
207 set menu_color_highlight=white/blue
210 #$(grub-root-search store-device font-file)
211 #$(setup-gfxterm config font-file)
212 #$(grub-setup-io config)
215 #$(theme-colors grub-theme-color-normal)
216 #$(theme-colors grub-theme-color-highlight))))
220 ;;; Configuration file.
223 (define* (keyboard-layout-file layout
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'."
230 (with-imported-modules '((guix build utils))
232 (use-modules (guix build utils))
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)
240 (computed-file (string-append "grub-keymap."
241 (string-map (match-lambda
244 (keyboard-layout-name layout)))
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))
257 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
258 ;; as documented in GRUB manual section "Simple Configuration
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))
270 (if (memq output valid-outputs) output #f)) outputs)) "\n"
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.
285 ;; COM ports 1 through 4
286 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
287 (string-append " --unit=" (number->string unit))
291 (if (exact-integer? speed)
292 (string-append " --speed=" (number->string speed))
296 (format #f "~a" io)))
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
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)))
308 ;; Preferably refer to DEVICE by its UUID or label. This is more
309 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
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)))
317 #~(format #f "search --file --set ~a" #$file)))))
319 (define* (grub-configuration-file config entries
321 (system (%current-system))
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."
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)
339 btrfs-subvolume-file-name))
340 (initrd (normalize-file (menu-entry-initrd entry)
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.
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 {
356 #$(grub-root-search device kernel)
357 #$kernel (string-join (list #$@arguments))
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
367 (define keyboard-layout-config
368 (let* ((layout (bootloader-configuration-keyboard-layout config))
369 (grub (bootloader-package
370 (bootloader-configuration-bootloader config)))
372 (keyboard-layout-file layout #:grub grub)))
374 (if btrfs-subvolume-file-name
375 #~(string-append #$btrfs-subvolume-file-name
381 keymap ~a~%" #$keymap))))
384 #~(call-with-output-file #$output
387 "# This file was generated from your Guix configuration. Any changes
388 # will be lost upon reconfiguration.
391 #$keyboard-layout-config
395 #$(bootloader-configuration-default-entry config)
396 #$(bootloader-configuration-timeout config))
397 #$@(map menu-entry->gexp all-entries)
399 #$@(if (pair? old-entries)
401 submenu \"GNU system, old configurations...\" {~%")
402 #$@(map menu-entry->gexp old-entries)
406 if [ \"${grub_platform}\" == efi ]; then
407 menuentry \"Firmware setup\" {
412 ;; Since this file is rather unique, there's no point in trying to
414 (computed-file "grub.cfg" builder
415 #:options '(#:local-build? #t
416 #:substitutable? #f)))
421 ;;; Install procedures.
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
431 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
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
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)
450 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
452 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
453 (invoke/quiet grub-install "--boot-directory" install-dir
454 "--bootloader-id=Guix"
455 "--efi-directory" target-esp))))
460 ;;; Bootloader definitions.
463 (define grub-bootloader
467 (installer install-grub)
468 (configuration-file "/boot/grub/grub.cfg")
469 (configuration-file-generator grub-configuration-file)))
471 (define grub-minimal-bootloader
474 (package grub-minimal)
475 (installer install-grub)
476 (configuration-file "/boot/grub/grub.cfg")
477 (configuration-file-generator grub-configuration-file)))
479 (define* grub-efi-bootloader
481 (inherit grub-bootloader)
482 (installer install-grub-efi)
486 (define* grub-mkrescue-bootloader
488 (inherit grub-efi-bootloader)
489 (package grub-hybrid)))
493 ;;; Compatibility macros.
496 (define-syntax grub-configuration
498 ((_ (grub package) fields ...)
499 (if (eq? package grub)
500 (bootloader-configuration
501 (bootloader grub-bootloader)
503 (bootloader-configuration
504 (bootloader grub-efi-bootloader)
507 (bootloader-configuration
508 (bootloader grub-bootloader)
511 ;;; grub.scm ends here