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