Commit | Line | Data |
---|---|---|
0ded70f3 | 1 | ;;; GNU Guix --- Functional package management for GNU |
4d0a3d8e | 2 | ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org> |
1ef8b72a | 3 | ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com> |
e0b2e930 | 4 | ;;; Copyright © 2017 Leo Famulari <leo@famulari.name> |
b09a8da4 | 5 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> |
0ded70f3 LC |
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 | ||
b09a8da4 | 22 | (define-module (gnu bootloader grub) |
0ded70f3 LC |
23 | #:use-module (guix store) |
24 | #:use-module (guix packages) | |
25 | #:use-module (guix derivations) | |
26 | #:use-module (guix records) | |
d9f0a237 | 27 | #:use-module (guix monads) |
f6a7b21d | 28 | #:use-module (guix gexp) |
99ae9ceb | 29 | #:use-module (guix download) |
84dfb458 | 30 | #:use-module (gnu artwork) |
2e58e05b | 31 | #:use-module (gnu system) |
b09a8da4 | 32 | #:use-module (gnu bootloader) |
9b336338 | 33 | #:use-module (gnu system uuid) |
862e38d5 | 34 | #:autoload (gnu packages bootloaders) (grub) |
99ae9ceb | 35 | #:autoload (gnu packages compression) (gzip) |
ffde82c9 | 36 | #:autoload (gnu packages gtk) (guile-cairo guile-rsvg) |
1b0f266e | 37 | #:autoload (gnu packages guile) (guile-2.2) |
0ded70f3 | 38 | #:use-module (ice-9 match) |
6b173ac0 | 39 | #:use-module (ice-9 regex) |
0ded70f3 | 40 | #:use-module (srfi srfi-1) |
1ef8b72a | 41 | #:use-module (rnrs bytevectors) |
99ae9ceb LC |
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 | ||
b09a8da4 MO |
56 | grub-bootloader |
57 | grub-efi-bootloader | |
cf189709 | 58 | grub-mkrescue-bootloader |
d5b429ab | 59 | |
b09a8da4 | 60 | grub-configuration)) |
0ded70f3 LC |
61 | |
62 | ;;; Commentary: | |
63 | ;;; | |
64 | ;;; Configuration of GNU GRUB. | |
65 | ;;; | |
66 | ;;; Code: | |
67 | ||
1ef8b72a CM |
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." | |
8b22107e MO |
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))) | |
0f65f54e | 80 | |
99ae9ceb LC |
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 | ||
99ae9ceb LC |
98 | (define %background-image |
99 | (grub-image | |
100 | (aspect-ratio 4/3) | |
357db1f9 LC |
101 | (file (file-append %artwork-repository |
102 | "/grub/GuixSD-fully-black-4-3.svg")))) | |
99ae9ceb LC |
103 | |
104 | (define %default-theme | |
105 | ;; Default theme contributed by Felipe López. | |
106 | (grub-theme | |
107 | (images (list %background-image)) | |
9c09760a | 108 | (color-highlight '((fg . yellow) (bg . black))) |
99ae9ceb LC |
109 | (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030 |
110 | ||
99ae9ceb LC |
111 | \f |
112 | ;;; | |
113 | ;;; Background image & themes. | |
114 | ;;; | |
115 | ||
b09a8da4 MO |
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 | ||
ffde82c9 LC |
121 | (define* (svg->png svg #:key width height) |
122 | "Build a PNG of HEIGHT x WIDTH from SVG." | |
1b0f266e LC |
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))) | |
99ae9ceb | 142 | |
6394fe65 | 143 | (define* (grub-background-image config #:key (width 1024) (height 768)) |
99ae9ceb LC |
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)) | |
b09a8da4 MO |
149 | (grub-theme-images |
150 | (bootloader-theme config))))) | |
99ae9ceb | 151 | (if image |
ffde82c9 LC |
152 | (svg->png (grub-image-file image) |
153 | #:width width #:height height) | |
99ae9ceb LC |
154 | (with-monad %store-monad |
155 | (return #f))))) | |
156 | ||
1ef8b72a CM |
157 | (define* (eye-candy config store-device store-mount-point |
158 | #:key system port) | |
99ae9ceb LC |
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 | |
1ef8b72a CM |
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\"." | |
6b173ac0 | 165 | (define setup-gfxterm-body |
4d0a3d8e LC |
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. | |
6b173ac0 MW |
169 | (if (string-match "^(x86_64|i[3-6]86)-" system) |
170 | " | |
122c3a1d | 171 | # Leave 'gfxmode' to 'auto'. |
6b173ac0 MW |
172 | insmod video_bochs |
173 | insmod video_cirrus | |
174 | insmod gfxterm | |
4d0a3d8e LC |
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 | |
5f19fb6a LC |
182 | else |
183 | # These are specific to non-EFI Intel machines. | |
184 | insmod vbe | |
185 | insmod vga | |
4d0a3d8e | 186 | fi |
6b173ac0 MW |
187 | " |
188 | "")) | |
189 | ||
e0b2e930 | 190 | (define (setup-gfxterm config font-file) |
b09a8da4 MO |
191 | (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config)) |
192 | #~(format #f "if loadfont ~a; then | |
e0b2e930 LF |
193 | setup_gfxterm |
194 | fi~%" #$font-file) | |
b09a8da4 | 195 | "")) |
e0b2e930 | 196 | |
99ae9ceb | 197 | (define (theme-colors type) |
b09a8da4 | 198 | (let* ((theme (bootloader-theme config)) |
99ae9ceb LC |
199 | (colors (type theme))) |
200 | (string-append (symbol->string (assoc-ref colors 'fg)) "/" | |
201 | (symbol->string (assoc-ref colors 'bg))))) | |
202 | ||
6b779207 | 203 | (define font-file |
1ef8b72a | 204 | (strip-mount-point store-mount-point |
0f65f54e | 205 | (file-append grub "/share/grub/unicode.pf2"))) |
6b779207 | 206 | |
99ae9ceb | 207 | (mlet* %store-monad ((image (grub-background-image config))) |
6b173ac0 MW |
208 | (return (and image |
209 | #~(format #$port " | |
210 | function setup_gfxterm {~a} | |
99ae9ceb | 211 | |
ccc2678b | 212 | # Set 'root' to the partition that contains /gnu/store. |
6b779207 | 213 | ~a |
ccc2678b | 214 | |
e0b2e930 LF |
215 | ~a |
216 | ~a | |
99ae9ceb LC |
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~%" | |
6b173ac0 | 226 | #$setup-gfxterm-body |
1ef8b72a | 227 | #$(grub-root-search store-device font-file) |
e0b2e930 | 228 | #$(setup-gfxterm config font-file) |
c48a145f | 229 | #$(grub-setup-io config) |
6b779207 | 230 | |
1ef8b72a | 231 | #$(strip-mount-point store-mount-point image) |
6b173ac0 MW |
232 | #$(theme-colors grub-theme-color-normal) |
233 | #$(theme-colors grub-theme-color-highlight)))))) | |
99ae9ceb LC |
234 | |
235 | \f | |
236 | ;;; | |
237 | ;;; Configuration file. | |
238 | ;;; | |
239 | ||
e0b2e930 LF |
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) " "))) | |
b09a8da4 MO |
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)) | |
e0b2e930 LF |
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 | ||
1ef8b72a CM |
291 | (define (grub-root-search device file) |
292 | "Return the GRUB 'search' command to look for DEVICE, which contains FILE, | |
6b779207 LC |
293 | a gexp. The result is a gexp that can be inserted in the grub.cfg-generation |
294 | code." | |
5babe521 LC |
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 | "" | |
1ef8b72a CM |
300 | (match device |
301 | ;; Preferably refer to DEVICE by its UUID or label. This is more | |
ecc4324f | 302 | ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>. |
9b336338 | 303 | ((? uuid? uuid) |
5babe521 | 304 | (format #f "search --fs-uuid --set ~a" |
1ef8b72a CM |
305 | (uuid->string device))) |
306 | ((? string? label) | |
307 | (format #f "search --label --set ~a" label)) | |
308 | (#f | |
5babe521 | 309 | #~(format #f "search --file --set ~a" #$file))))) |
6b779207 | 310 | |
1ef8b72a | 311 | (define* (grub-configuration-file config entries |
fe6e3fe2 LC |
312 | #:key |
313 | (system (%current-system)) | |
314 | (old-entries '())) | |
d5b429ab | 315 | "Return the GRUB configuration file corresponding to CONFIG, a |
b09a8da4 MO |
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." | |
d5b429ab | 319 | (define all-entries |
1975c754 DM |
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))) | |
1ef8b72a | 328 | ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point. |
8b22107e | 329 | ;; Use the right file names for KERNEL and INITRD in case |
1ef8b72a CM |
330 | ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a |
331 | ;; separate partition. | |
8b22107e MO |
332 | (let ((kernel (strip-mount-point device-mount-point kernel)) |
333 | (initrd (strip-mount-point device-mount-point initrd))) | |
0f65f54e | 334 | #~(format port "menuentry ~s { |
6b779207 | 335 | ~a |
44d5f54e | 336 | linux ~a ~a |
d9f0a237 | 337 | initrd ~a |
0ded70f3 | 338 | }~%" |
0f65f54e | 339 | #$label |
8b22107e MO |
340 | #$(grub-root-search device kernel) |
341 | #$kernel (string-join (list #$@arguments)) | |
342 | #$initrd)))) | |
1ef8b72a | 343 | (mlet %store-monad ((sugar (eye-candy config |
1975c754 | 344 | (menu-entry-device |
8b22107e | 345 | (first all-entries)) |
1975c754 | 346 | (menu-entry-device-mount-point |
0ab1e8ae | 347 | (first all-entries)) |
1ef8b72a CM |
348 | #:system system |
349 | #:port #~port))) | |
99ae9ceb LC |
350 | (define builder |
351 | #~(call-with-output-file #$output | |
352 | (lambda (port) | |
fdf14c64 JD |
353 | (format port |
354 | "# This file was generated from your GuixSD configuration. Any changes | |
355 | # will be lost upon reconfiguration. | |
356 | ") | |
99ae9ceb LC |
357 | #$sugar |
358 | (format port " | |
f6a7b21d | 359 | set default=~a |
6c777cf8 | 360 | set timeout=~a~%" |
b09a8da4 MO |
361 | #$(bootloader-configuration-default-entry config) |
362 | #$(bootloader-configuration-timeout config)) | |
1975c754 | 363 | #$@(map menu-entry->gexp all-entries) |
99ae9ceb LC |
364 | |
365 | #$@(if (pair? old-entries) | |
366 | #~((format port " | |
fe6e3fe2 | 367 | submenu \"GNU system, old configurations...\" {~%") |
1975c754 | 368 | #$@(map menu-entry->gexp old-entries) |
99ae9ceb LC |
369 | (format port "}~%")) |
370 | #~())))) | |
0ded70f3 | 371 | |
99ae9ceb | 372 | (gexp->derivation "grub.cfg" builder))) |
0ded70f3 | 373 | |
b09a8da4 MO |
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 | ||
46fc9a7e | 389 | (unless (zero? (system* grub "--no-floppy" "--target=i386-pc" |
b09a8da4 MO |
390 | "--boot-directory" install-dir |
391 | device)) | |
2941b347 AW |
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)"))))) | |
b09a8da4 MO |
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) | |
2941b347 | 424 | (installer install-grub-efi) |
b09a8da4 MO |
425 | (name 'grub-efi) |
426 | (package grub-efi))) | |
427 | ||
cf189709 DM |
428 | (define* grub-mkrescue-bootloader |
429 | (bootloader | |
430 | (inherit grub-efi-bootloader) | |
431 | (package grub-hybrid))) | |
432 | ||
b09a8da4 MO |
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 | ||
0ded70f3 | 453 | ;;; grub.scm ends here |