system: Add 'multiboot-modules' field to <boot-parameters>.
[jackhill/guix/guix.git] / gnu / bootloader / grub.scm
CommitLineData
0ded70f3 1;;; GNU Guix --- Functional package management for GNU
9512ba6b 2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
1ef8b72a 3;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
e0b2e930 4;;; Copyright © 2017 Leo Famulari <leo@famulari.name>
7feefb3b 5;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
6a790fe3 6;;; Copyright © 2019, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
aaffde38 7;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
0ded70f3
LC
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
b09a8da4 24(define-module (gnu bootloader grub)
0ded70f3 25 #:use-module (guix records)
46c296dc 26 #:use-module ((guix utils) #:select (%current-system))
f6a7b21d 27 #:use-module (guix gexp)
84dfb458 28 #:use-module (gnu artwork)
b09a8da4 29 #:use-module (gnu bootloader)
9b336338 30 #:use-module (gnu system uuid)
a5acc17a 31 #:use-module (gnu system file-systems)
8d058e7b 32 #:use-module (gnu system keyboard)
6a7c4636 33 #:use-module (gnu packages bootloaders)
ffde82c9 34 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
8d058e7b 35 #:autoload (gnu packages xorg) (xkeyboard-config)
0ded70f3 36 #:use-module (ice-9 match)
6b173ac0 37 #:use-module (ice-9 regex)
0ded70f3 38 #:use-module (srfi srfi-1)
6794653e 39 #:use-module (srfi srfi-2)
9cdb10d5 40 #:export (grub-theme
99ae9ceb 41 grub-theme?
9cdb10d5
S
42 grub-theme-image
43 grub-theme-resolution
99ae9ceb
LC
44 grub-theme-color-normal
45 grub-theme-color-highlight
9cdb10d5 46 grub-theme-gfxmode
99ae9ceb 47
b09a8da4
MO
48 grub-bootloader
49 grub-efi-bootloader
cf189709 50 grub-mkrescue-bootloader
6a790fe3 51 grub-minimal-bootloader
d5b429ab 52
b09a8da4 53 grub-configuration))
0ded70f3
LC
54
55;;; Commentary:
56;;;
57;;; Configuration of GNU GRUB.
58;;;
59;;; Code:
60
e7b86a0d
MC
61(define* (normalize-file file mount-point store-directory-prefix)
62 "Strip MOUNT-POINT and prepend STORE-DIRECTORY-PREFIX, if any, to FILE, a
b460ba79
MC
63G-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
e7b86a0d
MC
75 (define (prepend-store-directory-prefix store-directory-prefix file)
76 (if store-directory-prefix
77 #~(string-append #$store-directory-prefix #$file)
b460ba79
MC
78 file))
79
e7b86a0d
MC
80 (prepend-store-directory-prefix store-directory-prefix
81 (strip-mount-point mount-point file)))
b460ba79
MC
82
83
0f65f54e 84
99ae9ceb 85(define-record-type* <grub-theme>
9cdb10d5 86 ;; Default theme contributed by Felipe López.
99ae9ceb
LC
87 grub-theme make-grub-theme
88 grub-theme?
9cdb10d5
S
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)))
99ae9ceb 94 (color-normal grub-theme-color-normal
9cdb10d5 95 (default '((fg . light-gray) (bg . black))))
99ae9ceb 96 (color-highlight grub-theme-color-highlight
9cdb10d5
S
97 (default '((fg . yellow) (bg . black))))
98 (gfxmode grub-theme-gfxmode
f52fe7c3 99 (default '("auto")))) ;list of string
99ae9ceb 100
99ae9ceb
LC
101\f
102;;;
103;;; Background image & themes.
104;;;
105
b09a8da4 106(define (bootloader-theme config)
9cdb10d5 107 "Return user defined theme in CONFIG if defined or a default theme
b09a8da4 108otherwise."
9cdb10d5 109 (or (bootloader-configuration-theme config) (grub-theme)))
b09a8da4 110
9cdb10d5
S
111(define* (image->png image #:key width height)
112 "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\".
113Otherwise the picture in IMAGE is just copied."
46c296dc
LC
114 (computed-file "grub-image.png"
115 (with-imported-modules '((gnu build svg))
116 (with-extensions (list guile-rsvg guile-cairo)
9cdb10d5
S
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.
127If the suffix of the image file is \".svg\", then it is converted into a PNG
128file with the resolution provided in CONFIG."
129 (let* ((theme (bootloader-theme config))
130 (image (grub-theme-image theme)))
46c296dc 131 (and image
9cdb10d5
S
132 (match (grub-theme-resolution theme)
133 (((? number? width) . (? number? height))
134 (image->png image #:width width #:height height))
135 (_ #f)))))
99ae9ceb 136
1ef8b72a 137(define* (eye-candy config store-device store-mount-point
e7b86a0d 138 #:key store-directory-prefix system port)
b460ba79
MC
139 "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part
140concerned with graphics mode, background images, colors, and all that.
141STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is
142its mount point; these are used to determine where the background image and
143fonts must be searched for. SYSTEM must be the target system string---e.g.,
e7b86a0d
MC
144\"x86_64-linux\". STORE-DIRECTORY-PREFIX is a directory prefix to prepend to
145any store file name."
6b173ac0 146 (define setup-gfxterm-body
6794653e
MC
147 (let ((gfxmode
148 (or (and-let* ((theme (bootloader-configuration-theme config))
9cdb10d5 149 (gfxmode (grub-theme-gfxmode theme)))
6794653e
MC
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
aaffde38 164 insmod all_video
6794653e
MC
165 insmod gfxterm~%" gfxmode)
166 "")))
6b173ac0 167
e0b2e930 168 (define (setup-gfxterm config font-file)
b09a8da4
MO
169 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
170 #~(format #f "if loadfont ~a; then
e0b2e930 171 setup_gfxterm
8cf7dd24 172fi~%" #+font-file)
b09a8da4 173 ""))
e0b2e930 174
99ae9ceb 175 (define (theme-colors type)
b09a8da4 176 (let* ((theme (bootloader-theme config))
99ae9ceb
LC
177 (colors (type theme)))
178 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
179 (symbol->string (assoc-ref colors 'bg)))))
180
6b779207 181 (define font-file
b460ba79
MC
182 (normalize-file (file-append grub "/share/grub/unicode.pf2")
183 store-mount-point
e7b86a0d 184 store-directory-prefix))
6b779207 185
46c296dc 186 (define image
b460ba79
MC
187 (normalize-file (grub-background-image config)
188 store-mount-point
e7b86a0d 189 store-directory-prefix))
46c296dc
LC
190
191 (and image
192 #~(format #$port "
6b173ac0 193function setup_gfxterm {~a}
99ae9ceb 194
ccc2678b 195# Set 'root' to the partition that contains /gnu/store.
6b779207 196~a
ccc2678b 197
e0b2e930
LF
198~a
199~a
99ae9ceb
LC
200
201insmod png
202if background_image ~a; then
203 set color_normal=~a
204 set color_highlight=~a
205else
206 set menu_color_normal=cyan/blue
207 set menu_color_highlight=white/blue
208fi~%"
46c296dc
LC
209 #$setup-gfxterm-body
210 #$(grub-root-search store-device font-file)
211 #$(setup-gfxterm config font-file)
212 #$(grub-setup-io config)
6b779207 213
b460ba79 214 #$image
46c296dc
LC
215 #$(theme-colors grub-theme-color-normal)
216 #$(theme-colors grub-theme-color-highlight))))
99ae9ceb
LC
217
218\f
219;;;
220;;; Configuration file.
221;;;
222
8d058e7b
LC
223(define* (keyboard-layout-file layout
224 #:key
225 (grub grub))
226 "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
227and return a file in the format for GRUB keymaps. LAYOUT must be present in
228the '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).
8cf7dd24 236 (invoke #+(file-append grub "/bin/grub-mklayout")
8d058e7b
LC
237 "-i" #+(keyboard-layout->console-keymap layout)
238 "-o" #$output))))
239
2729cb40
LC
240 (computed-file (string-append "grub-keymap."
241 (string-map (match-lambda
242 (#\, #\-)
243 (chr chr))
244 (keyboard-layout-name layout)))
8d058e7b
LC
245 builder))
246
e0b2e930
LF
247(define (grub-setup-io config)
248 "Return GRUB commands to configure the input / output interfaces. The result
249is a string that can be inserted in grub.cfg."
250 (let* ((symbols->string (lambda (list)
251 (string-join (map symbol->string list) " ")))
b09a8da4
MO
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))
e0b2e930
LF
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
1ef8b72a
CM
298(define (grub-root-search device file)
299 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
6b779207
LC
300a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
301code."
5babe521
LC
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 ""
1ef8b72a
CM
307 (match device
308 ;; Preferably refer to DEVICE by its UUID or label. This is more
ecc4324f 309 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
9b336338 310 ((? uuid? uuid)
5babe521 311 (format #f "search --fs-uuid --set ~a"
1ef8b72a 312 (uuid->string device)))
a5acc17a
LC
313 ((? file-system-label? label)
314 (format #f "search --label --set ~a"
315 (file-system-label->string label)))
316 ((or #f (? string?))
5babe521 317 #~(format #f "search --file --set ~a" #$file)))))
6b779207 318
1ef8b72a 319(define* (grub-configuration-file config entries
fe6e3fe2
LC
320 #:key
321 (system (%current-system))
b460ba79 322 (old-entries '())
e7b86a0d 323 store-directory-prefix)
d5b429ab 324 "Return the GRUB configuration file corresponding to CONFIG, a
b09a8da4 325<bootloader-configuration> object, and where the store is available at
e7b86a0d
MC
326STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
327entries corresponding to old generations of the system.
328STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required
329when booting a root file system on a Btrfs subvolume."
d5b429ab 330 (define all-entries
1975c754
DM
331 (append entries (bootloader-configuration-menu-entries config)))
332 (define (menu-entry->gexp entry)
b460ba79
MC
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
e7b86a0d 339 store-directory-prefix))
b460ba79
MC
340 (initrd (normalize-file (menu-entry-initrd entry)
341 device-mount-point
e7b86a0d 342 store-directory-prefix)))
1ef8b72a 343 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
8b22107e 344 ;; Use the right file names for KERNEL and INITRD in case
1ef8b72a
CM
345 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
346 ;; separate partition.
b460ba79 347 #~(format port "menuentry ~s {
6b779207 348 ~a
44d5f54e 349 linux ~a ~a
d9f0a237 350 initrd ~a
0ded70f3 351}~%"
b460ba79
MC
352 #$label
353 #$(grub-root-search device kernel)
354 #$kernel (string-join (list #$@arguments))
355 #$initrd)))
46c296dc
LC
356 (define sugar
357 (eye-candy config
358 (menu-entry-device (first all-entries))
359 (menu-entry-device-mount-point (first all-entries))
e7b86a0d 360 #:store-directory-prefix store-directory-prefix
46c296dc
LC
361 #:system system
362 #:port #~port))
363
8d058e7b 364 (define keyboard-layout-config
b460ba79
MC
365 (let* ((layout (bootloader-configuration-keyboard-layout config))
366 (grub (bootloader-package
367 (bootloader-configuration-bootloader config)))
368 (keymap* (and layout
369 (keyboard-layout-file layout #:grub grub)))
370 (keymap (and keymap*
e7b86a0d
MC
371 (if store-directory-prefix
372 #~(string-append #$store-directory-prefix
b460ba79
MC
373 #$keymap*)
374 keymap*))))
375 #~(when #$keymap
376 (format port "\
8d058e7b 377insmod keylayouts
b460ba79 378keymap ~a~%" #$keymap))))
8d058e7b 379
46c296dc
LC
380 (define builder
381 #~(call-with-output-file #$output
382 (lambda (port)
383 (format port
59e80445 384 "# This file was generated from your Guix configuration. Any changes
fdf14c64
JD
385# will be lost upon reconfiguration.
386")
46c296dc 387 #$sugar
8d058e7b 388 #$keyboard-layout-config
46c296dc 389 (format port "
f6a7b21d 390set default=~a
6c777cf8 391set timeout=~a~%"
46c296dc
LC
392 #$(bootloader-configuration-default-entry config)
393 #$(bootloader-configuration-timeout config))
394 #$@(map menu-entry->gexp all-entries)
99ae9ceb 395
46c296dc
LC
396 #$@(if (pair? old-entries)
397 #~((format port "
fe6e3fe2 398submenu \"GNU system, old configurations...\" {~%")
46c296dc
LC
399 #$@(map menu-entry->gexp old-entries)
400 (format port "}~%"))
b0d09586
BW
401 #~())
402 (format port "
403if [ \"${grub_platform}\" == efi ]; then
404 menuentry \"Firmware setup\" {
405 fwsetup
406 }
407fi~%"))))
0ded70f3 408
9512ba6b
LC
409 ;; Since this file is rather unique, there's no point in trying to
410 ;; substitute it.
411 (computed-file "grub.cfg" builder
412 #:options '(#:local-build? #t
413 #:substitutable? #f)))
0ded70f3 414
b09a8da4
MO
415\f
416
417;;;
418;;; Install procedures.
419;;;
420
421(define install-grub
422 #~(lambda (bootloader device mount-point)
b09a8da4
MO
423 (let ((grub (string-append bootloader "/sbin/grub-install"))
424 (install-dir (string-append mount-point "/boot")))
7e6a42f2
MO
425 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE
426 ;; is #f, then we populate the disk-image rooted at MOUNT-POINT.
427 (if device
428 (begin
429 ;; Tell 'grub-install' that there might be a LUKS-encrypted
430 ;; /boot or 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 ;; When creating a disk-image, only install GRUB modules.
439 (copy-recursively (string-append bootloader "/lib/")
440 install-dir)))))
2941b347 441
7feefb3b
MO
442(define install-grub-disk-image
443 #~(lambda (bootloader root-index image)
444 ;; Install GRUB on the given IMAGE. The root partition index is
445 ;; ROOT-INDEX.
446 (let ((grub-mkimage
447 (string-append bootloader "/bin/grub-mkimage"))
448 (modules '("biosdisk" "part_msdos" "fat" "ext2"))
449 (grub-bios-setup
450 (string-append bootloader "/sbin/grub-bios-setup"))
451 (root-device (format #f "hd0,msdos~a" root-index))
452 (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img"))
453 (device-map "device.map"))
454
455 ;; Create a minimal, standalone GRUB image that will be written
456 ;; directly in the MBR-GAP (space between the end of the MBR and the
457 ;; first partition).
458 (apply invoke grub-mkimage
459 "-O" "i386-pc"
460 "-o" "core.img"
461 "-p" (format #f "(~a)/boot/grub" root-device)
462 modules)
463
464 ;; Create a device mapping file.
465 (call-with-output-file device-map
466 (lambda (port)
467 (format port "(hd0) ~a~%" image)))
468
469 ;; Copy the default boot.img, that will be written on the MBR sector
470 ;; by GRUB-BIOS-SETUP.
471 (copy-file boot-img "boot.img")
472
473 ;; Install both the "boot.img" and the "core.img" files on the given
474 ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB
475 ;; written in the MBR-GAP. GRUB configuration and missing modules will
476 ;; be read from ROOT-DEVICE.
477 (invoke grub-bios-setup
478 "-m" device-map
479 "-r" root-device
480 "-d" "."
481 image))))
482
2941b347
AW
483(define install-grub-efi
484 #~(lambda (bootloader efi-dir mount-point)
485 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
486 ;; system whose root is mounted at MOUNT-POINT.
487 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
aa5a549c 488 (install-dir (string-append mount-point "/boot"))
59e80445 489 ;; When installing Guix, it's common to mount EFI-DIR below
aa5a549c
MB
490 ;; MOUNT-POINT rather than /boot/efi on the live image.
491 (target-esp (if (file-exists? (string-append mount-point efi-dir))
492 (string-append mount-point efi-dir)
493 efi-dir)))
2941b347
AW
494 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
495 ;; root partition.
496 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
21fcfe1e
LC
497 (invoke/quiet grub-install "--boot-directory" install-dir
498 "--bootloader-id=Guix"
499 "--efi-directory" target-esp))))
b09a8da4
MO
500
501\f
502
503;;;
504;;; Bootloader definitions.
505;;;
506
507(define grub-bootloader
508 (bootloader
509 (name 'grub)
510 (package grub)
511 (installer install-grub)
7feefb3b 512 (disk-image-installer install-grub-disk-image)
b09a8da4
MO
513 (configuration-file "/boot/grub/grub.cfg")
514 (configuration-file-generator grub-configuration-file)))
515
7202895e 516(define* grub-minimal-bootloader
6a790fe3 517 (bootloader
7202895e
MO
518 (inherit grub-bootloader)
519 (package grub-minimal)))
6a790fe3 520
b09a8da4
MO
521(define* grub-efi-bootloader
522 (bootloader
523 (inherit grub-bootloader)
2941b347 524 (installer install-grub-efi)
7feefb3b 525 (disk-image-installer #f)
b09a8da4
MO
526 (name 'grub-efi)
527 (package grub-efi)))
528
cf189709
DM
529(define* grub-mkrescue-bootloader
530 (bootloader
531 (inherit grub-efi-bootloader)
532 (package grub-hybrid)))
533
b09a8da4
MO
534\f
535;;;
536;;; Compatibility macros.
537;;;
538
539(define-syntax grub-configuration
540 (syntax-rules (grub)
541 ((_ (grub package) fields ...)
542 (if (eq? package grub)
543 (bootloader-configuration
544 (bootloader grub-bootloader)
545 fields ...)
546 (bootloader-configuration
547 (bootloader grub-efi-bootloader)
548 fields ...)))
549 ((_ fields ...)
550 (bootloader-configuration
551 (bootloader grub-bootloader)
552 fields ...))))
553
0ded70f3 554;;; grub.scm ends here