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