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