gnu: sbcl-trivial-features: Update to 20200403.
[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 Mathieu Othacehe <m.othacehe@gmail.com>
6 ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
7 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
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
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)
40 #:export (grub-image
41 grub-image?
42 grub-image-aspect-ratio
43 grub-image-file
44
45 grub-theme
46 grub-theme?
47 grub-theme-images
48 grub-theme-color-normal
49 grub-theme-color-highlight
50
51 %background-image
52 %default-theme
53
54 grub-bootloader
55 grub-efi-bootloader
56 grub-mkrescue-bootloader
57
58 grub-configuration))
59
60 ;;; Commentary:
61 ;;;
62 ;;; Configuration of GNU GRUB.
63 ;;;
64 ;;; Code:
65
66 (define (strip-mount-point mount-point file)
67 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
68 denoting a file name."
69 (match mount-point
70 ((? string? mount-point)
71 (if (string=? mount-point "/")
72 file
73 #~(let ((file #$file))
74 (if (string-prefix? #$mount-point file)
75 (substring #$file #$(string-length mount-point))
76 file))))
77 (#f file)))
78
79 (define-record-type* <grub-image>
80 grub-image make-grub-image
81 grub-image?
82 (aspect-ratio grub-image-aspect-ratio ;rational number
83 (default 4/3))
84 (file grub-image-file)) ;file-valued gexp (SVG)
85
86 (define-record-type* <grub-theme>
87 grub-theme make-grub-theme
88 grub-theme?
89 (images grub-theme-images
90 (default '())) ;list of <grub-image>
91 (color-normal grub-theme-color-normal
92 (default '((fg . cyan) (bg . blue))))
93 (color-highlight grub-theme-color-highlight
94 (default '((fg . white) (bg . blue))))
95 (gfxmode grub-gfxmode
96 (default '("auto")))) ;list of string
97
98 (define %background-image
99 (grub-image
100 (aspect-ratio 4/3)
101 (file (file-append %artwork-repository
102 "/grub/GuixSD-fully-black-4-3.svg"))))
103
104 (define %default-theme
105 ;; Default theme contributed by Felipe López.
106 (grub-theme
107 (images (list %background-image))
108 (color-highlight '((fg . yellow) (bg . black)))
109 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
110
111 \f
112 ;;;
113 ;;; Background image & themes.
114 ;;;
115
116 (define (bootloader-theme config)
117 "Return user defined theme in CONFIG if defined or %default-theme
118 otherwise."
119 (or (bootloader-configuration-theme config) %default-theme))
120
121 (define* (svg->png svg #:key width height)
122 "Build a PNG of HEIGHT x WIDTH from SVG."
123 (computed-file "grub-image.png"
124 (with-imported-modules '((gnu build svg))
125 (with-extensions (list guile-rsvg guile-cairo)
126 #~(begin
127 (use-modules (gnu build svg))
128 (svg->png #+svg #$output
129 #:width #$width
130 #:height #$height))))))
131
132 (define* (grub-background-image config #:key (width 1024) (height 768))
133 "Return the GRUB background image defined in CONFIG with a ratio of
134 WIDTH/HEIGHT, or #f if none was found."
135 (let* ((ratio (/ width height))
136 (image (find (lambda (image)
137 (= (grub-image-aspect-ratio image) ratio))
138 (grub-theme-images
139 (bootloader-theme config)))))
140 (and image
141 (svg->png (grub-image-file image)
142 #:width width #:height height))))
143
144 (define* (eye-candy config store-device store-mount-point
145 #:key system port)
146 "Return a gexp that writes to PORT (a port-valued gexp) the
147 'grub.cfg' part concerned with graphics mode, background images, colors, and
148 all that. STORE-DEVICE designates the device holding the store, and
149 STORE-MOUNT-POINT is its mount point; these are used to determine where the
150 background image and fonts must be searched for. SYSTEM must be the target
151 system string---e.g., \"x86_64-linux\"."
152 (define setup-gfxterm-body
153 (let ((gfxmode
154 (or (and-let* ((theme (bootloader-configuration-theme config))
155 (gfxmode (grub-gfxmode theme)))
156 (string-join gfxmode ";"))
157 "auto")))
158
159 ;; Intel and EFI systems need to be switched into graphics mode, whereas
160 ;; most other modern architectures have no other mode and therefore
161 ;; don't need to be switched.
162
163 ;; XXX: Do we really need to restrict to x86 systems? We could imitate
164 ;; what the GRUB default configuration does and decide based on whether
165 ;; a user provided 'gfxterm' in the terminal-outputs field of their
166 ;; bootloader-configuration record.
167 (if (string-match "^(x86_64|i[3-6]86)-" system)
168 (format #f "
169 set gfxmode=~a
170 insmod all_video
171 insmod gfxterm~%" gfxmode)
172 "")))
173
174 (define (setup-gfxterm config font-file)
175 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
176 #~(format #f "if loadfont ~a; then
177 setup_gfxterm
178 fi~%" #$font-file)
179 ""))
180
181 (define (theme-colors type)
182 (let* ((theme (bootloader-theme config))
183 (colors (type theme)))
184 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
185 (symbol->string (assoc-ref colors 'bg)))))
186
187 (define font-file
188 (strip-mount-point store-mount-point
189 (file-append grub "/share/grub/unicode.pf2")))
190
191 (define image
192 (grub-background-image config))
193
194 (and image
195 #~(format #$port "
196 function setup_gfxterm {~a}
197
198 # Set 'root' to the partition that contains /gnu/store.
199 ~a
200
201 ~a
202 ~a
203
204 insmod png
205 if background_image ~a; then
206 set color_normal=~a
207 set color_highlight=~a
208 else
209 set menu_color_normal=cyan/blue
210 set menu_color_highlight=white/blue
211 fi~%"
212 #$setup-gfxterm-body
213 #$(grub-root-search store-device font-file)
214 #$(setup-gfxterm config font-file)
215 #$(grub-setup-io config)
216
217 #$(strip-mount-point store-mount-point image)
218 #$(theme-colors grub-theme-color-normal)
219 #$(theme-colors grub-theme-color-highlight))))
220
221 \f
222 ;;;
223 ;;; Configuration file.
224 ;;;
225
226 (define* (keyboard-layout-file layout
227 #:key
228 (grub grub))
229 "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
230 and return a file in the format for GRUB keymaps. LAYOUT must be present in
231 the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'."
232 (define builder
233 (with-imported-modules '((guix build utils))
234 #~(begin
235 (use-modules (guix build utils))
236
237 ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp'
238 ;; (from the 'console-setup' package).
239 (invoke #$(file-append grub "/bin/grub-mklayout")
240 "-i" #+(keyboard-layout->console-keymap layout)
241 "-o" #$output))))
242
243 (computed-file (string-append "grub-keymap."
244 (string-map (match-lambda
245 (#\, #\-)
246 (chr chr))
247 (keyboard-layout-name layout)))
248 builder))
249
250 (define (grub-setup-io config)
251 "Return GRUB commands to configure the input / output interfaces. The result
252 is a string that can be inserted in grub.cfg."
253 (let* ((symbols->string (lambda (list)
254 (string-join (map symbol->string list) " ")))
255 (outputs (bootloader-configuration-terminal-outputs config))
256 (inputs (bootloader-configuration-terminal-inputs config))
257 (unit (bootloader-configuration-serial-unit config))
258 (speed (bootloader-configuration-serial-speed config))
259
260 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
261 ;; as documented in GRUB manual section "Simple Configuration
262 ;; Handling".
263 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
264 gfxterm vga_text mda_text morse spkmodem))
265 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
266 at_keyboard usb_keyboard))
267
268 (io (string-append
269 "terminal_output "
270 (symbols->string
271 (map
272 (lambda (output)
273 (if (memq output valid-outputs) output #f)) outputs)) "\n"
274 (if (null? inputs)
275 ""
276 (string-append
277 "terminal_input "
278 (symbols->string
279 (map
280 (lambda (input)
281 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
282 ;; UNIT and SPEED are arguments to the same GRUB command
283 ;; ("serial"), so we process them together.
284 (if (or unit speed)
285 (string-append
286 "serial"
287 (if unit
288 ;; COM ports 1 through 4
289 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
290 (string-append " --unit=" (number->string unit))
291 #f)
292 "")
293 (if speed
294 (if (exact-integer? speed)
295 (string-append " --speed=" (number->string speed))
296 #f)
297 ""))
298 ""))))
299 (format #f "~a" io)))
300
301 (define (grub-root-search device file)
302 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
303 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
304 code."
305 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
306 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
307 ;; custom menu entries. In the latter case, don't emit a 'search' command.
308 (if (and (string? file) (not (string-prefix? "/" file)))
309 ""
310 (match device
311 ;; Preferably refer to DEVICE by its UUID or label. This is more
312 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
313 ((? uuid? uuid)
314 (format #f "search --fs-uuid --set ~a"
315 (uuid->string device)))
316 ((? file-system-label? label)
317 (format #f "search --label --set ~a"
318 (file-system-label->string label)))
319 ((or #f (? string?))
320 #~(format #f "search --file --set ~a" #$file)))))
321
322 (define* (grub-configuration-file config entries
323 #:key
324 (system (%current-system))
325 (old-entries '()))
326 "Return the GRUB configuration file corresponding to CONFIG, a
327 <bootloader-configuration> object, and where the store is available at
328 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
329 entries corresponding to old generations of the system."
330 (define all-entries
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 (kernel (menu-entry-linux entry))
337 (arguments (menu-entry-linux-arguments entry))
338 (initrd (menu-entry-initrd entry)))
339 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
340 ;; Use the right file names for KERNEL and INITRD in case
341 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
342 ;; separate partition.
343 (let ((kernel (strip-mount-point device-mount-point kernel))
344 (initrd (strip-mount-point device-mount-point initrd)))
345 #~(format port "menuentry ~s {
346 ~a
347 linux ~a ~a
348 initrd ~a
349 }~%"
350 #$label
351 #$(grub-root-search device kernel)
352 #$kernel (string-join (list #$@arguments))
353 #$initrd))))
354 (define sugar
355 (eye-candy config
356 (menu-entry-device (first all-entries))
357 (menu-entry-device-mount-point (first all-entries))
358 #:system system
359 #:port #~port))
360
361 (define keyboard-layout-config
362 (let ((layout (bootloader-configuration-keyboard-layout config))
363 (grub (bootloader-package
364 (bootloader-configuration-bootloader config))))
365 #~(let ((keymap #$(and layout
366 (keyboard-layout-file layout #:grub grub))))
367 (when keymap
368 (format port "\
369 insmod keylayouts
370 keymap ~a~%" keymap)))))
371
372 (define builder
373 #~(call-with-output-file #$output
374 (lambda (port)
375 (format port
376 "# This file was generated from your Guix configuration. Any changes
377 # will be lost upon reconfiguration.
378 ")
379 #$sugar
380 #$keyboard-layout-config
381 (format port "
382 set default=~a
383 set timeout=~a~%"
384 #$(bootloader-configuration-default-entry config)
385 #$(bootloader-configuration-timeout config))
386 #$@(map menu-entry->gexp all-entries)
387
388 #$@(if (pair? old-entries)
389 #~((format port "
390 submenu \"GNU system, old configurations...\" {~%")
391 #$@(map menu-entry->gexp old-entries)
392 (format port "}~%"))
393 #~())
394 (format port "
395 if [ \"${grub_platform}\" == efi ]; then
396 menuentry \"Firmware setup\" {
397 fwsetup
398 }
399 fi~%"))))
400
401 ;; Since this file is rather unique, there's no point in trying to
402 ;; substitute it.
403 (computed-file "grub.cfg" builder
404 #:options '(#:local-build? #t
405 #:substitutable? #f)))
406
407 \f
408
409 ;;;
410 ;;; Install procedures.
411 ;;;
412
413 (define install-grub
414 #~(lambda (bootloader device mount-point)
415 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
416 (let ((grub (string-append bootloader "/sbin/grub-install"))
417 (install-dir (string-append mount-point "/boot")))
418 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
419 ;; root partition.
420 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
421
422 ;; Hide potentially confusing messages from the user, such as
423 ;; "Installing for i386-pc platform."
424 (invoke/quiet grub "--no-floppy" "--target=i386-pc"
425 "--boot-directory" install-dir
426 device))))
427
428 (define install-grub-efi
429 #~(lambda (bootloader efi-dir mount-point)
430 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
431 ;; system whose root is mounted at MOUNT-POINT.
432 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
433 (install-dir (string-append mount-point "/boot"))
434 ;; When installing Guix, it's common to mount EFI-DIR below
435 ;; MOUNT-POINT rather than /boot/efi on the live image.
436 (target-esp (if (file-exists? (string-append mount-point efi-dir))
437 (string-append mount-point efi-dir)
438 efi-dir)))
439 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
440 ;; root partition.
441 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
442 (invoke/quiet grub-install "--boot-directory" install-dir
443 "--bootloader-id=Guix"
444 "--efi-directory" target-esp))))
445
446 \f
447
448 ;;;
449 ;;; Bootloader definitions.
450 ;;;
451
452 (define grub-bootloader
453 (bootloader
454 (name 'grub)
455 (package grub)
456 (installer install-grub)
457 (configuration-file "/boot/grub/grub.cfg")
458 (configuration-file-generator grub-configuration-file)))
459
460 (define* grub-efi-bootloader
461 (bootloader
462 (inherit grub-bootloader)
463 (installer install-grub-efi)
464 (name 'grub-efi)
465 (package grub-efi)))
466
467 (define* grub-mkrescue-bootloader
468 (bootloader
469 (inherit grub-efi-bootloader)
470 (package grub-hybrid)))
471
472 \f
473 ;;;
474 ;;; Compatibility macros.
475 ;;;
476
477 (define-syntax grub-configuration
478 (syntax-rules (grub)
479 ((_ (grub package) fields ...)
480 (if (eq? package grub)
481 (bootloader-configuration
482 (bootloader grub-bootloader)
483 fields ...)
484 (bootloader-configuration
485 (bootloader grub-efi-bootloader)
486 fields ...)))
487 ((_ fields ...)
488 (bootloader-configuration
489 (bootloader grub-bootloader)
490 fields ...))))
491
492 ;;; grub.scm ends here