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