Commit | Line | Data |
---|---|---|
0ded70f3 | 1 | ;;; GNU Guix --- Functional package management for GNU |
9512ba6b | 2 | ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
1ef8b72a | 3 | ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com> |
e0b2e930 | 4 | ;;; Copyright © 2017 Leo Famulari <leo@famulari.name> |
7feefb3b | 5 | ;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com> |
6a790fe3 | 6 | ;;; Copyright © 2019, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> |
aaffde38 | 7 | ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com> |
0ded70f3 LC |
8 | ;;; |
9 | ;;; This file is part of GNU Guix. | |
10 | ;;; | |
11 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
12 | ;;; under the terms of the GNU General Public License as published by | |
13 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
14 | ;;; your option) any later version. | |
15 | ;;; | |
16 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
17 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;;; GNU General Public License for more details. | |
20 | ;;; | |
21 | ;;; You should have received a copy of the GNU General Public License | |
22 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
b09a8da4 | 24 | (define-module (gnu bootloader grub) |
0ded70f3 | 25 | #:use-module (guix records) |
46c296dc | 26 | #:use-module ((guix utils) #:select (%current-system)) |
f6a7b21d | 27 | #:use-module (guix gexp) |
84dfb458 | 28 | #:use-module (gnu artwork) |
b09a8da4 | 29 | #:use-module (gnu bootloader) |
9b336338 | 30 | #:use-module (gnu system uuid) |
a5acc17a | 31 | #:use-module (gnu system file-systems) |
8d058e7b | 32 | #:use-module (gnu system keyboard) |
6a7c4636 | 33 | #:use-module (gnu packages bootloaders) |
ffde82c9 | 34 | #:autoload (gnu packages gtk) (guile-cairo guile-rsvg) |
8d058e7b | 35 | #:autoload (gnu packages xorg) (xkeyboard-config) |
0ded70f3 | 36 | #:use-module (ice-9 match) |
6b173ac0 | 37 | #:use-module (ice-9 regex) |
0ded70f3 | 38 | #:use-module (srfi srfi-1) |
6794653e | 39 | #:use-module (srfi srfi-2) |
9cdb10d5 | 40 | #:export (grub-theme |
99ae9ceb | 41 | grub-theme? |
9cdb10d5 S |
42 | grub-theme-image |
43 | grub-theme-resolution | |
99ae9ceb LC |
44 | grub-theme-color-normal |
45 | grub-theme-color-highlight | |
9cdb10d5 | 46 | grub-theme-gfxmode |
99ae9ceb | 47 | |
b09a8da4 MO |
48 | grub-bootloader |
49 | grub-efi-bootloader | |
cf189709 | 50 | grub-mkrescue-bootloader |
6a790fe3 | 51 | grub-minimal-bootloader |
d5b429ab | 52 | |
b09a8da4 | 53 | grub-configuration)) |
0ded70f3 LC |
54 | |
55 | ;;; Commentary: | |
56 | ;;; | |
57 | ;;; Configuration of GNU GRUB. | |
58 | ;;; | |
59 | ;;; Code: | |
60 | ||
e7b86a0d MC |
61 | (define* (normalize-file file mount-point store-directory-prefix) |
62 | "Strip MOUNT-POINT and prepend STORE-DIRECTORY-PREFIX, if any, to FILE, a | |
b460ba79 MC |
63 | G-expression or other lowerable object denoting a file name." |
64 | ||
65 | (define (strip-mount-point mount-point file) | |
66 | (if mount-point | |
67 | (if (string=? mount-point "/") | |
68 | file | |
69 | #~(let ((file #$file)) | |
70 | (if (string-prefix? #$mount-point file) | |
71 | (substring #$file #$(string-length mount-point)) | |
72 | file))) | |
73 | file)) | |
74 | ||
e7b86a0d MC |
75 | (define (prepend-store-directory-prefix store-directory-prefix file) |
76 | (if store-directory-prefix | |
77 | #~(string-append #$store-directory-prefix #$file) | |
b460ba79 MC |
78 | file)) |
79 | ||
e7b86a0d MC |
80 | (prepend-store-directory-prefix store-directory-prefix |
81 | (strip-mount-point mount-point file))) | |
b460ba79 MC |
82 | |
83 | ||
0f65f54e | 84 | |
99ae9ceb | 85 | (define-record-type* <grub-theme> |
9cdb10d5 | 86 | ;; Default theme contributed by Felipe López. |
99ae9ceb LC |
87 | grub-theme make-grub-theme |
88 | grub-theme? | |
9cdb10d5 S |
89 | (image grub-theme-image |
90 | (default (file-append %artwork-repository | |
91 | "/grub/GuixSD-fully-black-4-3.svg"))) | |
92 | (resolution grub-theme-resolution | |
93 | (default '(1024 . 768))) | |
99ae9ceb | 94 | (color-normal grub-theme-color-normal |
9cdb10d5 | 95 | (default '((fg . light-gray) (bg . black)))) |
99ae9ceb | 96 | (color-highlight grub-theme-color-highlight |
9cdb10d5 S |
97 | (default '((fg . yellow) (bg . black)))) |
98 | (gfxmode grub-theme-gfxmode | |
f52fe7c3 | 99 | (default '("auto")))) ;list of string |
99ae9ceb | 100 | |
99ae9ceb LC |
101 | \f |
102 | ;;; | |
103 | ;;; Background image & themes. | |
104 | ;;; | |
105 | ||
b09a8da4 | 106 | (define (bootloader-theme config) |
9cdb10d5 | 107 | "Return user defined theme in CONFIG if defined or a default theme |
b09a8da4 | 108 | otherwise." |
9cdb10d5 | 109 | (or (bootloader-configuration-theme config) (grub-theme))) |
b09a8da4 | 110 | |
9cdb10d5 S |
111 | (define* (image->png image #:key width height) |
112 | "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\". | |
113 | Otherwise the picture in IMAGE is just copied." | |
46c296dc LC |
114 | (computed-file "grub-image.png" |
115 | (with-imported-modules '((gnu build svg)) | |
116 | (with-extensions (list guile-rsvg guile-cairo) | |
9cdb10d5 S |
117 | #~(if (string-suffix? ".svg" #+image) |
118 | (begin | |
119 | (use-modules (gnu build svg)) | |
120 | (svg->png #+image #$output | |
121 | #:width #$width | |
122 | #:height #$height)) | |
123 | (copy-file #+image #$output)))))) | |
124 | ||
125 | (define* (grub-background-image config) | |
126 | "Return the GRUB background image defined in CONFIG or #f if none was found. | |
127 | If the suffix of the image file is \".svg\", then it is converted into a PNG | |
128 | file with the resolution provided in CONFIG." | |
129 | (let* ((theme (bootloader-theme config)) | |
130 | (image (grub-theme-image theme))) | |
46c296dc | 131 | (and image |
9cdb10d5 S |
132 | (match (grub-theme-resolution theme) |
133 | (((? number? width) . (? number? height)) | |
134 | (image->png image #:width width #:height height)) | |
135 | (_ #f))))) | |
99ae9ceb | 136 | |
1ef8b72a | 137 | (define* (eye-candy config store-device store-mount-point |
e7b86a0d | 138 | #:key store-directory-prefix system port) |
b460ba79 MC |
139 | "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part |
140 | concerned with graphics mode, background images, colors, and all that. | |
141 | STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is | |
142 | its mount point; these are used to determine where the background image and | |
143 | fonts must be searched for. SYSTEM must be the target system string---e.g., | |
e7b86a0d MC |
144 | \"x86_64-linux\". STORE-DIRECTORY-PREFIX is a directory prefix to prepend to |
145 | any store file name." | |
6b173ac0 | 146 | (define setup-gfxterm-body |
6794653e MC |
147 | (let ((gfxmode |
148 | (or (and-let* ((theme (bootloader-configuration-theme config)) | |
9cdb10d5 | 149 | (gfxmode (grub-theme-gfxmode theme))) |
6794653e MC |
150 | (string-join gfxmode ";")) |
151 | "auto"))) | |
152 | ||
153 | ;; Intel and EFI systems need to be switched into graphics mode, whereas | |
154 | ;; most other modern architectures have no other mode and therefore | |
155 | ;; don't need to be switched. | |
156 | ||
157 | ;; XXX: Do we really need to restrict to x86 systems? We could imitate | |
158 | ;; what the GRUB default configuration does and decide based on whether | |
159 | ;; a user provided 'gfxterm' in the terminal-outputs field of their | |
160 | ;; bootloader-configuration record. | |
161 | (if (string-match "^(x86_64|i[3-6]86)-" system) | |
162 | (format #f " | |
163 | set gfxmode=~a | |
aaffde38 | 164 | insmod all_video |
6794653e MC |
165 | insmod gfxterm~%" gfxmode) |
166 | ""))) | |
6b173ac0 | 167 | |
e0b2e930 | 168 | (define (setup-gfxterm config font-file) |
b09a8da4 MO |
169 | (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config)) |
170 | #~(format #f "if loadfont ~a; then | |
e0b2e930 | 171 | setup_gfxterm |
8cf7dd24 | 172 | fi~%" #+font-file) |
b09a8da4 | 173 | "")) |
e0b2e930 | 174 | |
99ae9ceb | 175 | (define (theme-colors type) |
b09a8da4 | 176 | (let* ((theme (bootloader-theme config)) |
99ae9ceb LC |
177 | (colors (type theme))) |
178 | (string-append (symbol->string (assoc-ref colors 'fg)) "/" | |
179 | (symbol->string (assoc-ref colors 'bg))))) | |
180 | ||
6b779207 | 181 | (define font-file |
b460ba79 MC |
182 | (normalize-file (file-append grub "/share/grub/unicode.pf2") |
183 | store-mount-point | |
e7b86a0d | 184 | store-directory-prefix)) |
6b779207 | 185 | |
46c296dc | 186 | (define image |
b460ba79 MC |
187 | (normalize-file (grub-background-image config) |
188 | store-mount-point | |
e7b86a0d | 189 | store-directory-prefix)) |
46c296dc LC |
190 | |
191 | (and image | |
192 | #~(format #$port " | |
6b173ac0 | 193 | function setup_gfxterm {~a} |
99ae9ceb | 194 | |
ccc2678b | 195 | # Set 'root' to the partition that contains /gnu/store. |
6b779207 | 196 | ~a |
ccc2678b | 197 | |
e0b2e930 LF |
198 | ~a |
199 | ~a | |
99ae9ceb LC |
200 | |
201 | insmod png | |
202 | if background_image ~a; then | |
203 | set color_normal=~a | |
204 | set color_highlight=~a | |
205 | else | |
206 | set menu_color_normal=cyan/blue | |
207 | set menu_color_highlight=white/blue | |
208 | fi~%" | |
46c296dc LC |
209 | #$setup-gfxterm-body |
210 | #$(grub-root-search store-device font-file) | |
211 | #$(setup-gfxterm config font-file) | |
212 | #$(grub-setup-io config) | |
6b779207 | 213 | |
b460ba79 | 214 | #$image |
46c296dc LC |
215 | #$(theme-colors grub-theme-color-normal) |
216 | #$(theme-colors grub-theme-color-highlight)))) | |
99ae9ceb LC |
217 | |
218 | \f | |
219 | ;;; | |
220 | ;;; Configuration file. | |
221 | ;;; | |
222 | ||
8d058e7b LC |
223 | (define* (keyboard-layout-file layout |
224 | #:key | |
225 | (grub grub)) | |
226 | "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record, | |
227 | and return a file in the format for GRUB keymaps. LAYOUT must be present in | |
228 | the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'." | |
229 | (define builder | |
230 | (with-imported-modules '((guix build utils)) | |
231 | #~(begin | |
232 | (use-modules (guix build utils)) | |
233 | ||
234 | ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp' | |
235 | ;; (from the 'console-setup' package). | |
8cf7dd24 | 236 | (invoke #+(file-append grub "/bin/grub-mklayout") |
8d058e7b LC |
237 | "-i" #+(keyboard-layout->console-keymap layout) |
238 | "-o" #$output)))) | |
239 | ||
2729cb40 LC |
240 | (computed-file (string-append "grub-keymap." |
241 | (string-map (match-lambda | |
242 | (#\, #\-) | |
243 | (chr chr)) | |
244 | (keyboard-layout-name layout))) | |
8d058e7b LC |
245 | builder)) |
246 | ||
e0b2e930 LF |
247 | (define (grub-setup-io config) |
248 | "Return GRUB commands to configure the input / output interfaces. The result | |
249 | is a string that can be inserted in grub.cfg." | |
250 | (let* ((symbols->string (lambda (list) | |
251 | (string-join (map symbol->string list) " "))) | |
b09a8da4 MO |
252 | (outputs (bootloader-configuration-terminal-outputs config)) |
253 | (inputs (bootloader-configuration-terminal-inputs config)) | |
254 | (unit (bootloader-configuration-serial-unit config)) | |
255 | (speed (bootloader-configuration-serial-speed config)) | |
e0b2e930 LF |
256 | |
257 | ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT, | |
258 | ;; as documented in GRUB manual section "Simple Configuration | |
259 | ;; Handling". | |
260 | (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3 | |
261 | gfxterm vga_text mda_text morse spkmodem)) | |
262 | (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3 | |
263 | at_keyboard usb_keyboard)) | |
264 | ||
265 | (io (string-append | |
266 | "terminal_output " | |
267 | (symbols->string | |
268 | (map | |
269 | (lambda (output) | |
270 | (if (memq output valid-outputs) output #f)) outputs)) "\n" | |
271 | (if (null? inputs) | |
272 | "" | |
273 | (string-append | |
274 | "terminal_input " | |
275 | (symbols->string | |
276 | (map | |
277 | (lambda (input) | |
278 | (if (memq input valid-inputs) input #f)) inputs)) "\n")) | |
279 | ;; UNIT and SPEED are arguments to the same GRUB command | |
280 | ;; ("serial"), so we process them together. | |
281 | (if (or unit speed) | |
282 | (string-append | |
283 | "serial" | |
284 | (if unit | |
285 | ;; COM ports 1 through 4 | |
286 | (if (and (exact-integer? unit) (<= unit 3) (>= unit 0)) | |
287 | (string-append " --unit=" (number->string unit)) | |
288 | #f) | |
289 | "") | |
290 | (if speed | |
291 | (if (exact-integer? speed) | |
292 | (string-append " --speed=" (number->string speed)) | |
293 | #f) | |
294 | "")) | |
295 | "")))) | |
296 | (format #f "~a" io))) | |
297 | ||
1ef8b72a CM |
298 | (define (grub-root-search device file) |
299 | "Return the GRUB 'search' command to look for DEVICE, which contains FILE, | |
6b779207 LC |
300 | a gexp. The result is a gexp that can be inserted in the grub.cfg-generation |
301 | code." | |
5babe521 LC |
302 | ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but |
303 | ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of | |
304 | ;; custom menu entries. In the latter case, don't emit a 'search' command. | |
305 | (if (and (string? file) (not (string-prefix? "/" file))) | |
306 | "" | |
1ef8b72a CM |
307 | (match device |
308 | ;; Preferably refer to DEVICE by its UUID or label. This is more | |
ecc4324f | 309 | ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>. |
9b336338 | 310 | ((? uuid? uuid) |
5babe521 | 311 | (format #f "search --fs-uuid --set ~a" |
1ef8b72a | 312 | (uuid->string device))) |
a5acc17a LC |
313 | ((? file-system-label? label) |
314 | (format #f "search --label --set ~a" | |
315 | (file-system-label->string label))) | |
316 | ((or #f (? string?)) | |
5babe521 | 317 | #~(format #f "search --file --set ~a" #$file))))) |
6b779207 | 318 | |
1ef8b72a | 319 | (define* (grub-configuration-file config entries |
fe6e3fe2 LC |
320 | #:key |
321 | (system (%current-system)) | |
b460ba79 | 322 | (old-entries '()) |
e7b86a0d | 323 | store-directory-prefix) |
d5b429ab | 324 | "Return the GRUB configuration file corresponding to CONFIG, a |
b09a8da4 | 325 | <bootloader-configuration> object, and where the store is available at |
e7b86a0d MC |
326 | STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu |
327 | entries corresponding to old generations of the system. | |
328 | STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required | |
329 | when booting a root file system on a Btrfs subvolume." | |
d5b429ab | 330 | (define all-entries |
1975c754 DM |
331 | (append entries (bootloader-configuration-menu-entries config))) |
332 | (define (menu-entry->gexp entry) | |
1244491a JN |
333 | (let ((label (menu-entry-label entry)) |
334 | (linux (menu-entry-linux entry)) | |
335 | (device (menu-entry-device entry)) | |
336 | (device-mount-point (menu-entry-device-mount-point entry))) | |
337 | (if linux | |
338 | (let ((arguments (menu-entry-linux-arguments entry)) | |
339 | (linux (normalize-file linux | |
340 | device-mount-point | |
341 | store-directory-prefix)) | |
342 | (initrd (normalize-file (menu-entry-initrd entry) | |
343 | device-mount-point | |
344 | store-directory-prefix))) | |
345 | ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point. | |
346 | ;; Use the right file names for LINUX and INITRD in case | |
347 | ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a | |
348 | ;; separate partition. | |
349 | ||
350 | ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the linux and | |
351 | ;; initrd paths, to allow booting from a Btrfs subvolume. | |
352 | #~(format port "menuentry ~s { | |
6b779207 | 353 | ~a |
44d5f54e | 354 | linux ~a ~a |
d9f0a237 | 355 | initrd ~a |
0ded70f3 | 356 | }~%" |
1244491a JN |
357 | #$label |
358 | #$(grub-root-search device linux) | |
359 | #$linux (string-join (list #$@arguments)) | |
360 | #$initrd)) | |
361 | (let ((kernel (menu-entry-multiboot-kernel entry)) | |
362 | (arguments (menu-entry-multiboot-arguments entry)) | |
363 | (modules (menu-entry-multiboot-modules entry)) | |
364 | (root-index 1)) ; XXX EFI will need root-index 2 | |
365 | #~(format port " | |
366 | menuentry ~s { | |
367 | multiboot ~a root=device:hd0s~a~a~a | |
368 | }~%" | |
369 | #$label | |
370 | #$kernel | |
371 | #$root-index (string-join (list #$@arguments) " " 'prefix) | |
372 | (string-join (map string-join '#$modules) | |
373 | "\n module " 'prefix)))))) | |
374 | ||
375 | (define (sugar) | |
376 | (let* ((entry (first all-entries)) | |
377 | (device (menu-entry-device entry)) | |
378 | (mount-point (menu-entry-device-mount-point entry))) | |
379 | (eye-candy config | |
380 | device | |
381 | mount-point | |
382 | #:store-directory-prefix store-directory-prefix | |
383 | #:system system | |
384 | #:port #~port))) | |
46c296dc | 385 | |
8d058e7b | 386 | (define keyboard-layout-config |
b460ba79 MC |
387 | (let* ((layout (bootloader-configuration-keyboard-layout config)) |
388 | (grub (bootloader-package | |
389 | (bootloader-configuration-bootloader config))) | |
390 | (keymap* (and layout | |
391 | (keyboard-layout-file layout #:grub grub))) | |
392 | (keymap (and keymap* | |
e7b86a0d MC |
393 | (if store-directory-prefix |
394 | #~(string-append #$store-directory-prefix | |
b460ba79 MC |
395 | #$keymap*) |
396 | keymap*)))) | |
397 | #~(when #$keymap | |
398 | (format port "\ | |
8d058e7b | 399 | insmod keylayouts |
b460ba79 | 400 | keymap ~a~%" #$keymap)))) |
8d058e7b | 401 | |
46c296dc LC |
402 | (define builder |
403 | #~(call-with-output-file #$output | |
404 | (lambda (port) | |
405 | (format port | |
59e80445 | 406 | "# This file was generated from your Guix configuration. Any changes |
fdf14c64 JD |
407 | # will be lost upon reconfiguration. |
408 | ") | |
1244491a | 409 | #$(sugar) |
8d058e7b | 410 | #$keyboard-layout-config |
46c296dc | 411 | (format port " |
f6a7b21d | 412 | set default=~a |
6c777cf8 | 413 | set timeout=~a~%" |
46c296dc LC |
414 | #$(bootloader-configuration-default-entry config) |
415 | #$(bootloader-configuration-timeout config)) | |
416 | #$@(map menu-entry->gexp all-entries) | |
99ae9ceb | 417 | |
46c296dc LC |
418 | #$@(if (pair? old-entries) |
419 | #~((format port " | |
fe6e3fe2 | 420 | submenu \"GNU system, old configurations...\" {~%") |
46c296dc LC |
421 | #$@(map menu-entry->gexp old-entries) |
422 | (format port "}~%")) | |
b0d09586 BW |
423 | #~()) |
424 | (format port " | |
425 | if [ \"${grub_platform}\" == efi ]; then | |
426 | menuentry \"Firmware setup\" { | |
427 | fwsetup | |
428 | } | |
429 | fi~%")))) | |
0ded70f3 | 430 | |
9512ba6b LC |
431 | ;; Since this file is rather unique, there's no point in trying to |
432 | ;; substitute it. | |
433 | (computed-file "grub.cfg" builder | |
434 | #:options '(#:local-build? #t | |
435 | #:substitutable? #f))) | |
0ded70f3 | 436 | |
b09a8da4 MO |
437 | \f |
438 | ||
439 | ;;; | |
440 | ;;; Install procedures. | |
441 | ;;; | |
442 | ||
443 | (define install-grub | |
444 | #~(lambda (bootloader device mount-point) | |
b09a8da4 MO |
445 | (let ((grub (string-append bootloader "/sbin/grub-install")) |
446 | (install-dir (string-append mount-point "/boot"))) | |
7e6a42f2 MO |
447 | ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE |
448 | ;; is #f, then we populate the disk-image rooted at MOUNT-POINT. | |
449 | (if device | |
450 | (begin | |
451 | ;; Tell 'grub-install' that there might be a LUKS-encrypted | |
452 | ;; /boot or root partition. | |
453 | (setenv "GRUB_ENABLE_CRYPTODISK" "y") | |
454 | ||
455 | ;; Hide potentially confusing messages from the user, such as | |
456 | ;; "Installing for i386-pc platform." | |
457 | (invoke/quiet grub "--no-floppy" "--target=i386-pc" | |
458 | "--boot-directory" install-dir | |
459 | device)) | |
460 | ;; When creating a disk-image, only install GRUB modules. | |
461 | (copy-recursively (string-append bootloader "/lib/") | |
462 | install-dir))))) | |
2941b347 | 463 | |
7feefb3b MO |
464 | (define install-grub-disk-image |
465 | #~(lambda (bootloader root-index image) | |
466 | ;; Install GRUB on the given IMAGE. The root partition index is | |
467 | ;; ROOT-INDEX. | |
468 | (let ((grub-mkimage | |
469 | (string-append bootloader "/bin/grub-mkimage")) | |
470 | (modules '("biosdisk" "part_msdos" "fat" "ext2")) | |
471 | (grub-bios-setup | |
472 | (string-append bootloader "/sbin/grub-bios-setup")) | |
473 | (root-device (format #f "hd0,msdos~a" root-index)) | |
474 | (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img")) | |
475 | (device-map "device.map")) | |
476 | ||
477 | ;; Create a minimal, standalone GRUB image that will be written | |
478 | ;; directly in the MBR-GAP (space between the end of the MBR and the | |
479 | ;; first partition). | |
480 | (apply invoke grub-mkimage | |
481 | "-O" "i386-pc" | |
482 | "-o" "core.img" | |
483 | "-p" (format #f "(~a)/boot/grub" root-device) | |
484 | modules) | |
485 | ||
486 | ;; Create a device mapping file. | |
487 | (call-with-output-file device-map | |
488 | (lambda (port) | |
489 | (format port "(hd0) ~a~%" image))) | |
490 | ||
491 | ;; Copy the default boot.img, that will be written on the MBR sector | |
492 | ;; by GRUB-BIOS-SETUP. | |
493 | (copy-file boot-img "boot.img") | |
494 | ||
495 | ;; Install both the "boot.img" and the "core.img" files on the given | |
496 | ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB | |
497 | ;; written in the MBR-GAP. GRUB configuration and missing modules will | |
498 | ;; be read from ROOT-DEVICE. | |
499 | (invoke grub-bios-setup | |
500 | "-m" device-map | |
501 | "-r" root-device | |
502 | "-d" "." | |
503 | image)))) | |
504 | ||
2941b347 AW |
505 | (define install-grub-efi |
506 | #~(lambda (bootloader efi-dir mount-point) | |
507 | ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the | |
508 | ;; system whose root is mounted at MOUNT-POINT. | |
509 | (let ((grub-install (string-append bootloader "/sbin/grub-install")) | |
aa5a549c | 510 | (install-dir (string-append mount-point "/boot")) |
59e80445 | 511 | ;; When installing Guix, it's common to mount EFI-DIR below |
aa5a549c MB |
512 | ;; MOUNT-POINT rather than /boot/efi on the live image. |
513 | (target-esp (if (file-exists? (string-append mount-point efi-dir)) | |
514 | (string-append mount-point efi-dir) | |
515 | efi-dir))) | |
2941b347 AW |
516 | ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or |
517 | ;; root partition. | |
518 | (setenv "GRUB_ENABLE_CRYPTODISK" "y") | |
21fcfe1e LC |
519 | (invoke/quiet grub-install "--boot-directory" install-dir |
520 | "--bootloader-id=Guix" | |
521 | "--efi-directory" target-esp)))) | |
b09a8da4 MO |
522 | |
523 | \f | |
524 | ||
525 | ;;; | |
526 | ;;; Bootloader definitions. | |
527 | ;;; | |
528 | ||
529 | (define grub-bootloader | |
530 | (bootloader | |
531 | (name 'grub) | |
532 | (package grub) | |
533 | (installer install-grub) | |
7feefb3b | 534 | (disk-image-installer install-grub-disk-image) |
b09a8da4 MO |
535 | (configuration-file "/boot/grub/grub.cfg") |
536 | (configuration-file-generator grub-configuration-file))) | |
537 | ||
7202895e | 538 | (define* grub-minimal-bootloader |
6a790fe3 | 539 | (bootloader |
7202895e MO |
540 | (inherit grub-bootloader) |
541 | (package grub-minimal))) | |
6a790fe3 | 542 | |
b09a8da4 MO |
543 | (define* grub-efi-bootloader |
544 | (bootloader | |
545 | (inherit grub-bootloader) | |
2941b347 | 546 | (installer install-grub-efi) |
7feefb3b | 547 | (disk-image-installer #f) |
b09a8da4 MO |
548 | (name 'grub-efi) |
549 | (package grub-efi))) | |
550 | ||
cf189709 DM |
551 | (define* grub-mkrescue-bootloader |
552 | (bootloader | |
553 | (inherit grub-efi-bootloader) | |
554 | (package grub-hybrid))) | |
555 | ||
b09a8da4 MO |
556 | \f |
557 | ;;; | |
558 | ;;; Compatibility macros. | |
559 | ;;; | |
560 | ||
561 | (define-syntax grub-configuration | |
562 | (syntax-rules (grub) | |
563 | ((_ (grub package) fields ...) | |
564 | (if (eq? package grub) | |
565 | (bootloader-configuration | |
566 | (bootloader grub-bootloader) | |
567 | fields ...) | |
568 | (bootloader-configuration | |
569 | (bootloader grub-efi-bootloader) | |
570 | fields ...))) | |
571 | ((_ fields ...) | |
572 | (bootloader-configuration | |
573 | (bootloader grub-bootloader) | |
574 | fields ...)))) | |
575 | ||
0ded70f3 | 576 | ;;; grub.scm ends here |