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