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> |
cfe9c7ec | 7 | ;;; Copyright © 2019 Miguel Ángel Arruga Vivas <rosen644835@gmail.com> |
aaffde38 | 8 | ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com> |
536c53d3 | 9 | ;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de> |
0ded70f3 LC |
10 | ;;; |
11 | ;;; This file is part of GNU Guix. | |
12 | ;;; | |
13 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
14 | ;;; under the terms of the GNU General Public License as published by | |
15 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
16 | ;;; your option) any later version. | |
17 | ;;; | |
18 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
19 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;;; GNU General Public License for more details. | |
22 | ;;; | |
23 | ;;; You should have received a copy of the GNU General Public License | |
24 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
25 | ||
b09a8da4 | 26 | (define-module (gnu bootloader grub) |
c85f316a | 27 | #:use-module (guix build union) |
0ded70f3 | 28 | #:use-module (guix records) |
c85f316a S |
29 | #:use-module (guix store) |
30 | #:use-module (guix utils) | |
f6a7b21d | 31 | #:use-module (guix gexp) |
84dfb458 | 32 | #:use-module (gnu artwork) |
b09a8da4 | 33 | #:use-module (gnu bootloader) |
9b336338 | 34 | #:use-module (gnu system uuid) |
a5acc17a | 35 | #:use-module (gnu system file-systems) |
8d058e7b | 36 | #:use-module (gnu system keyboard) |
cfe9c7ec | 37 | #:use-module (gnu system locale) |
6a7c4636 | 38 | #:use-module (gnu packages bootloaders) |
ffde82c9 | 39 | #:autoload (gnu packages gtk) (guile-cairo guile-rsvg) |
8d058e7b | 40 | #:autoload (gnu packages xorg) (xkeyboard-config) |
0ded70f3 | 41 | #:use-module (ice-9 match) |
6b173ac0 | 42 | #:use-module (ice-9 regex) |
0ded70f3 | 43 | #:use-module (srfi srfi-1) |
6794653e | 44 | #:use-module (srfi srfi-2) |
9cdb10d5 | 45 | #:export (grub-theme |
99ae9ceb | 46 | grub-theme? |
9cdb10d5 S |
47 | grub-theme-image |
48 | grub-theme-resolution | |
99ae9ceb LC |
49 | grub-theme-color-normal |
50 | grub-theme-color-highlight | |
9cdb10d5 | 51 | grub-theme-gfxmode |
99ae9ceb | 52 | |
c85f316a S |
53 | install-grub-efi-netboot |
54 | ||
b09a8da4 MO |
55 | grub-bootloader |
56 | grub-efi-bootloader | |
c85f316a | 57 | grub-efi-netboot-bootloader |
cf189709 | 58 | grub-mkrescue-bootloader |
6a790fe3 | 59 | grub-minimal-bootloader |
d5b429ab | 60 | |
b09a8da4 | 61 | grub-configuration)) |
0ded70f3 LC |
62 | |
63 | ;;; Commentary: | |
64 | ;;; | |
65 | ;;; Configuration of GNU GRUB. | |
66 | ;;; | |
67 | ;;; Code: | |
68 | ||
e7b86a0d MC |
69 | (define* (normalize-file file mount-point store-directory-prefix) |
70 | "Strip MOUNT-POINT and prepend STORE-DIRECTORY-PREFIX, if any, to FILE, a | |
b460ba79 MC |
71 | G-expression or other lowerable object denoting a file name." |
72 | ||
73 | (define (strip-mount-point mount-point file) | |
74 | (if mount-point | |
75 | (if (string=? mount-point "/") | |
76 | file | |
77 | #~(let ((file #$file)) | |
78 | (if (string-prefix? #$mount-point file) | |
79 | (substring #$file #$(string-length mount-point)) | |
80 | file))) | |
81 | file)) | |
82 | ||
e7b86a0d MC |
83 | (define (prepend-store-directory-prefix store-directory-prefix file) |
84 | (if store-directory-prefix | |
85 | #~(string-append #$store-directory-prefix #$file) | |
b460ba79 MC |
86 | file)) |
87 | ||
e7b86a0d MC |
88 | (prepend-store-directory-prefix store-directory-prefix |
89 | (strip-mount-point mount-point file))) | |
b460ba79 MC |
90 | |
91 | ||
0f65f54e | 92 | |
99ae9ceb | 93 | (define-record-type* <grub-theme> |
9cdb10d5 | 94 | ;; Default theme contributed by Felipe López. |
99ae9ceb LC |
95 | grub-theme make-grub-theme |
96 | grub-theme? | |
9cdb10d5 S |
97 | (image grub-theme-image |
98 | (default (file-append %artwork-repository | |
99 | "/grub/GuixSD-fully-black-4-3.svg"))) | |
100 | (resolution grub-theme-resolution | |
101 | (default '(1024 . 768))) | |
99ae9ceb | 102 | (color-normal grub-theme-color-normal |
9cdb10d5 | 103 | (default '((fg . light-gray) (bg . black)))) |
99ae9ceb | 104 | (color-highlight grub-theme-color-highlight |
9cdb10d5 S |
105 | (default '((fg . yellow) (bg . black)))) |
106 | (gfxmode grub-theme-gfxmode | |
f52fe7c3 | 107 | (default '("auto")))) ;list of string |
99ae9ceb | 108 | |
99ae9ceb LC |
109 | \f |
110 | ;;; | |
111 | ;;; Background image & themes. | |
112 | ;;; | |
113 | ||
b09a8da4 | 114 | (define (bootloader-theme config) |
9cdb10d5 | 115 | "Return user defined theme in CONFIG if defined or a default theme |
b09a8da4 | 116 | otherwise." |
9cdb10d5 | 117 | (or (bootloader-configuration-theme config) (grub-theme))) |
b09a8da4 | 118 | |
9cdb10d5 S |
119 | (define* (image->png image #:key width height) |
120 | "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\". | |
121 | Otherwise the picture in IMAGE is just copied." | |
46c296dc LC |
122 | (computed-file "grub-image.png" |
123 | (with-imported-modules '((gnu build svg)) | |
124 | (with-extensions (list guile-rsvg guile-cairo) | |
9cdb10d5 S |
125 | #~(if (string-suffix? ".svg" #+image) |
126 | (begin | |
127 | (use-modules (gnu build svg)) | |
128 | (svg->png #+image #$output | |
129 | #:width #$width | |
130 | #:height #$height)) | |
131 | (copy-file #+image #$output)))))) | |
132 | ||
133 | (define* (grub-background-image config) | |
134 | "Return the GRUB background image defined in CONFIG or #f if none was found. | |
135 | If the suffix of the image file is \".svg\", then it is converted into a PNG | |
136 | file with the resolution provided in CONFIG." | |
137 | (let* ((theme (bootloader-theme config)) | |
138 | (image (grub-theme-image theme))) | |
46c296dc | 139 | (and image |
9cdb10d5 S |
140 | (match (grub-theme-resolution theme) |
141 | (((? number? width) . (? number? height)) | |
142 | (image->png image #:width width #:height height)) | |
143 | (_ #f))))) | |
99ae9ceb | 144 | |
f445bc65 MÁAV |
145 | (define (grub-locale-directory grub) |
146 | "Generate a directory with the locales from GRUB." | |
147 | (define builder | |
148 | #~(begin | |
149 | (use-modules (ice-9 ftw)) | |
150 | (let ((locale (string-append #$grub "/share/locale")) | |
151 | (out #$output)) | |
152 | (mkdir out) | |
153 | (chdir out) | |
154 | (for-each (lambda (lang) | |
155 | (let ((file (string-append locale "/" lang | |
156 | "/LC_MESSAGES/grub.mo")) | |
157 | (dest (string-append lang ".mo"))) | |
158 | (when (file-exists? file) | |
159 | (copy-file file dest)))) | |
160 | (scandir locale))))) | |
161 | (computed-file "grub-locales" builder)) | |
162 | ||
1ef8b72a | 163 | (define* (eye-candy config store-device store-mount-point |
536c53d3 | 164 | #:key store-directory-prefix port) |
b460ba79 MC |
165 | "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part |
166 | concerned with graphics mode, background images, colors, and all that. | |
167 | STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is | |
168 | its mount point; these are used to determine where the background image and | |
536c53d3 S |
169 | fonts must be searched for. STORE-DIRECTORY-PREFIX is a directory prefix to |
170 | prepend to any store file name." | |
e0b2e930 | 171 | (define (setup-gfxterm config font-file) |
b09a8da4 | 172 | (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config)) |
536c53d3 S |
173 | #~(format #f " |
174 | if loadfont ~a; then | |
175 | set gfxmode=~a | |
176 | insmod all_video | |
177 | insmod gfxterm | |
178 | fi~%" | |
80352a2f | 179 | #+font-file |
536c53d3 S |
180 | #$(string-join |
181 | (grub-theme-gfxmode (bootloader-theme config)) | |
182 | ";")) | |
b09a8da4 | 183 | "")) |
e0b2e930 | 184 | |
99ae9ceb | 185 | (define (theme-colors type) |
b09a8da4 | 186 | (let* ((theme (bootloader-theme config)) |
99ae9ceb LC |
187 | (colors (type theme))) |
188 | (string-append (symbol->string (assoc-ref colors 'fg)) "/" | |
189 | (symbol->string (assoc-ref colors 'bg))))) | |
190 | ||
6b779207 | 191 | (define font-file |
222a630e MÁAV |
192 | (let* ((bootloader (bootloader-configuration-bootloader config)) |
193 | (grub (bootloader-package bootloader))) | |
194 | (normalize-file (file-append grub "/share/grub/unicode.pf2") | |
195 | store-mount-point | |
196 | store-directory-prefix))) | |
6b779207 | 197 | |
46c296dc | 198 | (define image |
b460ba79 MC |
199 | (normalize-file (grub-background-image config) |
200 | store-mount-point | |
e7b86a0d | 201 | store-directory-prefix)) |
46c296dc LC |
202 | |
203 | (and image | |
204 | #~(format #$port " | |
ccc2678b | 205 | # Set 'root' to the partition that contains /gnu/store. |
6b779207 | 206 | ~a |
ccc2678b | 207 | |
e0b2e930 LF |
208 | ~a |
209 | ~a | |
99ae9ceb LC |
210 | |
211 | insmod png | |
212 | if background_image ~a; then | |
213 | set color_normal=~a | |
214 | set color_highlight=~a | |
215 | else | |
216 | set menu_color_normal=cyan/blue | |
217 | set menu_color_highlight=white/blue | |
218 | fi~%" | |
46c296dc LC |
219 | #$(grub-root-search store-device font-file) |
220 | #$(setup-gfxterm config font-file) | |
221 | #$(grub-setup-io config) | |
6b779207 | 222 | |
b460ba79 | 223 | #$image |
46c296dc LC |
224 | #$(theme-colors grub-theme-color-normal) |
225 | #$(theme-colors grub-theme-color-highlight)))) | |
99ae9ceb LC |
226 | |
227 | \f | |
228 | ;;; | |
229 | ;;; Configuration file. | |
230 | ;;; | |
231 | ||
8d058e7b LC |
232 | (define* (keyboard-layout-file layout |
233 | #:key | |
234 | (grub grub)) | |
235 | "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record, | |
236 | and return a file in the format for GRUB keymaps. LAYOUT must be present in | |
237 | the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'." | |
238 | (define builder | |
239 | (with-imported-modules '((guix build utils)) | |
240 | #~(begin | |
241 | (use-modules (guix build utils)) | |
242 | ||
243 | ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp' | |
244 | ;; (from the 'console-setup' package). | |
8cf7dd24 | 245 | (invoke #+(file-append grub "/bin/grub-mklayout") |
8d058e7b LC |
246 | "-i" #+(keyboard-layout->console-keymap layout) |
247 | "-o" #$output)))) | |
248 | ||
2729cb40 LC |
249 | (computed-file (string-append "grub-keymap." |
250 | (string-map (match-lambda | |
251 | (#\, #\-) | |
252 | (chr chr)) | |
253 | (keyboard-layout-name layout))) | |
8d058e7b LC |
254 | builder)) |
255 | ||
e0b2e930 LF |
256 | (define (grub-setup-io config) |
257 | "Return GRUB commands to configure the input / output interfaces. The result | |
258 | is a string that can be inserted in grub.cfg." | |
259 | (let* ((symbols->string (lambda (list) | |
260 | (string-join (map symbol->string list) " "))) | |
b09a8da4 MO |
261 | (outputs (bootloader-configuration-terminal-outputs config)) |
262 | (inputs (bootloader-configuration-terminal-inputs config)) | |
263 | (unit (bootloader-configuration-serial-unit config)) | |
264 | (speed (bootloader-configuration-serial-speed config)) | |
e0b2e930 LF |
265 | |
266 | ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT, | |
267 | ;; as documented in GRUB manual section "Simple Configuration | |
268 | ;; Handling". | |
269 | (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3 | |
270 | gfxterm vga_text mda_text morse spkmodem)) | |
271 | (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3 | |
272 | at_keyboard usb_keyboard)) | |
273 | ||
274 | (io (string-append | |
275 | "terminal_output " | |
276 | (symbols->string | |
277 | (map | |
278 | (lambda (output) | |
279 | (if (memq output valid-outputs) output #f)) outputs)) "\n" | |
280 | (if (null? inputs) | |
281 | "" | |
282 | (string-append | |
283 | "terminal_input " | |
284 | (symbols->string | |
285 | (map | |
286 | (lambda (input) | |
287 | (if (memq input valid-inputs) input #f)) inputs)) "\n")) | |
288 | ;; UNIT and SPEED are arguments to the same GRUB command | |
289 | ;; ("serial"), so we process them together. | |
290 | (if (or unit speed) | |
291 | (string-append | |
292 | "serial" | |
293 | (if unit | |
294 | ;; COM ports 1 through 4 | |
295 | (if (and (exact-integer? unit) (<= unit 3) (>= unit 0)) | |
296 | (string-append " --unit=" (number->string unit)) | |
297 | #f) | |
298 | "") | |
299 | (if speed | |
300 | (if (exact-integer? speed) | |
301 | (string-append " --speed=" (number->string speed)) | |
302 | #f) | |
303 | "")) | |
304 | "")))) | |
305 | (format #f "~a" io))) | |
306 | ||
1ef8b72a CM |
307 | (define (grub-root-search device file) |
308 | "Return the GRUB 'search' command to look for DEVICE, which contains FILE, | |
6b779207 LC |
309 | a gexp. The result is a gexp that can be inserted in the grub.cfg-generation |
310 | code." | |
5babe521 LC |
311 | ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but |
312 | ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of | |
313 | ;; custom menu entries. In the latter case, don't emit a 'search' command. | |
314 | (if (and (string? file) (not (string-prefix? "/" file))) | |
315 | "" | |
1ef8b72a CM |
316 | (match device |
317 | ;; Preferably refer to DEVICE by its UUID or label. This is more | |
ecc4324f | 318 | ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>. |
9b336338 | 319 | ((? uuid? uuid) |
5babe521 | 320 | (format #f "search --fs-uuid --set ~a" |
1ef8b72a | 321 | (uuid->string device))) |
a5acc17a LC |
322 | ((? file-system-label? label) |
323 | (format #f "search --label --set ~a" | |
324 | (file-system-label->string label))) | |
8c4f1aa8 S |
325 | ((? (lambda (device) |
326 | (and (string? device) (string-contains device ":/"))) nfs-uri) | |
c85f316a S |
327 | ;; If the device is an NFS share, then we assume that the expected |
328 | ;; file on that device (e.g. the GRUB background image or the kernel) | |
329 | ;; has to be loaded over the network. Otherwise we would need an | |
330 | ;; additional device information for some local disk to look for that | |
331 | ;; file, which we do not have. | |
8c4f1aa8 S |
332 | ;; |
333 | ;; We explicitly set "root=(tftp)" here even though if grub.cfg | |
334 | ;; had been loaded via TFTP, Grub would have set "root=(tftp)" | |
335 | ;; automatically anyway. The reason is if you have a system that | |
336 | ;; used to be on NFS but now is local, root would be set to local | |
337 | ;; disk. If you then selected an older system generation that is | |
338 | ;; supposed to boot from network in the Grub boot menu, Grub still | |
339 | ;; wouldn't load those files from network otherwise. | |
340 | ;; | |
341 | ;; TFTP is preferred to HTTP because it is used more widely and | |
342 | ;; specified in standards more widely--especially BOOTP/DHCPv4 | |
343 | ;; defines a TFTP server for DHCP option 66, but not HTTP. | |
344 | ;; | |
345 | ;; Note: DHCPv6 specifies option 59 to contain a boot-file-url, | |
346 | ;; which can contain a HTTP or TFTP URL. | |
347 | ;; | |
348 | ;; Note: It is assumed that the file paths are of a similar | |
349 | ;; setup on both the TFTP server and the NFS server (it is | |
350 | ;; not possible to search for files on TFTP). | |
351 | ;; | |
352 | ;; TODO: Allow HTTP. | |
353 | "set root=(tftp)") | |
a5acc17a | 354 | ((or #f (? string?)) |
5babe521 | 355 | #~(format #f "search --file --set ~a" #$file))))) |
6b779207 | 356 | |
1ef8b72a | 357 | (define* (grub-configuration-file config entries |
fe6e3fe2 | 358 | #:key |
cfe9c7ec | 359 | (locale #f) |
fe6e3fe2 | 360 | (system (%current-system)) |
b460ba79 | 361 | (old-entries '()) |
e7b86a0d | 362 | store-directory-prefix) |
d5b429ab | 363 | "Return the GRUB configuration file corresponding to CONFIG, a |
b09a8da4 | 364 | <bootloader-configuration> object, and where the store is available at |
e7b86a0d MC |
365 | STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu |
366 | entries corresponding to old generations of the system. | |
367 | STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required | |
368 | when booting a root file system on a Btrfs subvolume." | |
d5b429ab | 369 | (define all-entries |
1975c754 DM |
370 | (append entries (bootloader-configuration-menu-entries config))) |
371 | (define (menu-entry->gexp entry) | |
1244491a JN |
372 | (let ((label (menu-entry-label entry)) |
373 | (linux (menu-entry-linux entry)) | |
374 | (device (menu-entry-device entry)) | |
375 | (device-mount-point (menu-entry-device-mount-point entry))) | |
376 | (if linux | |
377 | (let ((arguments (menu-entry-linux-arguments entry)) | |
378 | (linux (normalize-file linux | |
379 | device-mount-point | |
380 | store-directory-prefix)) | |
381 | (initrd (normalize-file (menu-entry-initrd entry) | |
382 | device-mount-point | |
383 | store-directory-prefix))) | |
384 | ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point. | |
385 | ;; Use the right file names for LINUX and INITRD in case | |
386 | ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a | |
387 | ;; separate partition. | |
388 | ||
389 | ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the linux and | |
390 | ;; initrd paths, to allow booting from a Btrfs subvolume. | |
391 | #~(format port "menuentry ~s { | |
6b779207 | 392 | ~a |
44d5f54e | 393 | linux ~a ~a |
d9f0a237 | 394 | initrd ~a |
0ded70f3 | 395 | }~%" |
1244491a JN |
396 | #$label |
397 | #$(grub-root-search device linux) | |
398 | #$linux (string-join (list #$@arguments)) | |
399 | #$initrd)) | |
400 | (let ((kernel (menu-entry-multiboot-kernel entry)) | |
401 | (arguments (menu-entry-multiboot-arguments entry)) | |
402 | (modules (menu-entry-multiboot-modules entry)) | |
403 | (root-index 1)) ; XXX EFI will need root-index 2 | |
404 | #~(format port " | |
405 | menuentry ~s { | |
406 | multiboot ~a root=device:hd0s~a~a~a | |
407 | }~%" | |
408 | #$label | |
409 | #$kernel | |
410 | #$root-index (string-join (list #$@arguments) " " 'prefix) | |
411 | (string-join (map string-join '#$modules) | |
412 | "\n module " 'prefix)))))) | |
413 | ||
414 | (define (sugar) | |
415 | (let* ((entry (first all-entries)) | |
416 | (device (menu-entry-device entry)) | |
417 | (mount-point (menu-entry-device-mount-point entry))) | |
418 | (eye-candy config | |
419 | device | |
420 | mount-point | |
421 | #:store-directory-prefix store-directory-prefix | |
1244491a | 422 | #:port #~port))) |
46c296dc | 423 | |
cfe9c7ec | 424 | (define locale-config |
f445bc65 MÁAV |
425 | (let* ((entry (first all-entries)) |
426 | (device (menu-entry-device entry)) | |
427 | (mount-point (menu-entry-device-mount-point entry)) | |
428 | (bootloader (bootloader-configuration-bootloader config)) | |
429 | (grub (bootloader-package bootloader))) | |
430 | #~(let ((locale #$(and locale | |
431 | (locale-definition-source | |
432 | (locale-name->definition locale)))) | |
433 | (locales #$(and locale | |
434 | (normalize-file (grub-locale-directory grub) | |
435 | mount-point | |
436 | store-directory-prefix)))) | |
437 | (when locale | |
438 | (format port "\ | |
cfe9c7ec | 439 | # Localization configuration. |
f445bc65 MÁAV |
440 | ~asearch --file --set ~a/en@quot.mo |
441 | set locale_dir=~a | |
442 | set lang=~a~%" | |
443 | ;; Skip the search if there is an image, as it has already | |
444 | ;; been performed by eye-candy and traversing the store is | |
445 | ;; an expensive operation. | |
446 | #$(if (grub-theme-image (bootloader-theme config)) | |
447 | "# " | |
448 | "") | |
449 | locales | |
450 | locales | |
451 | locale))))) | |
cfe9c7ec | 452 | |
8d058e7b | 453 | (define keyboard-layout-config |
b460ba79 MC |
454 | (let* ((layout (bootloader-configuration-keyboard-layout config)) |
455 | (grub (bootloader-package | |
456 | (bootloader-configuration-bootloader config))) | |
457 | (keymap* (and layout | |
458 | (keyboard-layout-file layout #:grub grub))) | |
c69a1c27 MÁAV |
459 | (entry (first all-entries)) |
460 | (device (menu-entry-device entry)) | |
461 | (mount-point (menu-entry-device-mount-point entry)) | |
b460ba79 | 462 | (keymap (and keymap* |
c69a1c27 MÁAV |
463 | (normalize-file keymap* mount-point |
464 | store-directory-prefix)))) | |
b460ba79 MC |
465 | #~(when #$keymap |
466 | (format port "\ | |
8d058e7b | 467 | insmod keylayouts |
b460ba79 | 468 | keymap ~a~%" #$keymap)))) |
8d058e7b | 469 | |
46c296dc LC |
470 | (define builder |
471 | #~(call-with-output-file #$output | |
472 | (lambda (port) | |
473 | (format port | |
59e80445 | 474 | "# This file was generated from your Guix configuration. Any changes |
fdf14c64 JD |
475 | # will be lost upon reconfiguration. |
476 | ") | |
1244491a | 477 | #$(sugar) |
cfe9c7ec | 478 | #$locale-config |
8d058e7b | 479 | #$keyboard-layout-config |
46c296dc | 480 | (format port " |
f6a7b21d | 481 | set default=~a |
6c777cf8 | 482 | set timeout=~a~%" |
46c296dc LC |
483 | #$(bootloader-configuration-default-entry config) |
484 | #$(bootloader-configuration-timeout config)) | |
485 | #$@(map menu-entry->gexp all-entries) | |
99ae9ceb | 486 | |
46c296dc LC |
487 | #$@(if (pair? old-entries) |
488 | #~((format port " | |
fe6e3fe2 | 489 | submenu \"GNU system, old configurations...\" {~%") |
46c296dc LC |
490 | #$@(map menu-entry->gexp old-entries) |
491 | (format port "}~%")) | |
b0d09586 BW |
492 | #~()) |
493 | (format port " | |
494 | if [ \"${grub_platform}\" == efi ]; then | |
495 | menuentry \"Firmware setup\" { | |
496 | fwsetup | |
497 | } | |
498 | fi~%")))) | |
0ded70f3 | 499 | |
9512ba6b LC |
500 | ;; Since this file is rather unique, there's no point in trying to |
501 | ;; substitute it. | |
502 | (computed-file "grub.cfg" builder | |
503 | #:options '(#:local-build? #t | |
504 | #:substitutable? #f))) | |
0ded70f3 | 505 | |
b09a8da4 MO |
506 | \f |
507 | ||
508 | ;;; | |
509 | ;;; Install procedures. | |
510 | ;;; | |
511 | ||
512 | (define install-grub | |
513 | #~(lambda (bootloader device mount-point) | |
b09a8da4 MO |
514 | (let ((grub (string-append bootloader "/sbin/grub-install")) |
515 | (install-dir (string-append mount-point "/boot"))) | |
7e6a42f2 MO |
516 | ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE |
517 | ;; is #f, then we populate the disk-image rooted at MOUNT-POINT. | |
518 | (if device | |
519 | (begin | |
520 | ;; Tell 'grub-install' that there might be a LUKS-encrypted | |
521 | ;; /boot or root partition. | |
522 | (setenv "GRUB_ENABLE_CRYPTODISK" "y") | |
523 | ||
524 | ;; Hide potentially confusing messages from the user, such as | |
525 | ;; "Installing for i386-pc platform." | |
526 | (invoke/quiet grub "--no-floppy" "--target=i386-pc" | |
527 | "--boot-directory" install-dir | |
528 | device)) | |
529 | ;; When creating a disk-image, only install GRUB modules. | |
530 | (copy-recursively (string-append bootloader "/lib/") | |
531 | install-dir))))) | |
2941b347 | 532 | |
7feefb3b MO |
533 | (define install-grub-disk-image |
534 | #~(lambda (bootloader root-index image) | |
535 | ;; Install GRUB on the given IMAGE. The root partition index is | |
536 | ;; ROOT-INDEX. | |
537 | (let ((grub-mkimage | |
538 | (string-append bootloader "/bin/grub-mkimage")) | |
539 | (modules '("biosdisk" "part_msdos" "fat" "ext2")) | |
540 | (grub-bios-setup | |
541 | (string-append bootloader "/sbin/grub-bios-setup")) | |
542 | (root-device (format #f "hd0,msdos~a" root-index)) | |
543 | (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img")) | |
544 | (device-map "device.map")) | |
545 | ||
546 | ;; Create a minimal, standalone GRUB image that will be written | |
547 | ;; directly in the MBR-GAP (space between the end of the MBR and the | |
548 | ;; first partition). | |
549 | (apply invoke grub-mkimage | |
550 | "-O" "i386-pc" | |
551 | "-o" "core.img" | |
552 | "-p" (format #f "(~a)/boot/grub" root-device) | |
553 | modules) | |
554 | ||
555 | ;; Create a device mapping file. | |
556 | (call-with-output-file device-map | |
557 | (lambda (port) | |
558 | (format port "(hd0) ~a~%" image))) | |
559 | ||
560 | ;; Copy the default boot.img, that will be written on the MBR sector | |
561 | ;; by GRUB-BIOS-SETUP. | |
562 | (copy-file boot-img "boot.img") | |
563 | ||
564 | ;; Install both the "boot.img" and the "core.img" files on the given | |
565 | ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB | |
566 | ;; written in the MBR-GAP. GRUB configuration and missing modules will | |
567 | ;; be read from ROOT-DEVICE. | |
568 | (invoke grub-bios-setup | |
569 | "-m" device-map | |
570 | "-r" root-device | |
571 | "-d" "." | |
572 | image)))) | |
573 | ||
2941b347 AW |
574 | (define install-grub-efi |
575 | #~(lambda (bootloader efi-dir mount-point) | |
576 | ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the | |
577 | ;; system whose root is mounted at MOUNT-POINT. | |
578 | (let ((grub-install (string-append bootloader "/sbin/grub-install")) | |
aa5a549c | 579 | (install-dir (string-append mount-point "/boot")) |
59e80445 | 580 | ;; When installing Guix, it's common to mount EFI-DIR below |
aa5a549c MB |
581 | ;; MOUNT-POINT rather than /boot/efi on the live image. |
582 | (target-esp (if (file-exists? (string-append mount-point efi-dir)) | |
583 | (string-append mount-point efi-dir) | |
584 | efi-dir))) | |
2941b347 AW |
585 | ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or |
586 | ;; root partition. | |
587 | (setenv "GRUB_ENABLE_CRYPTODISK" "y") | |
21fcfe1e LC |
588 | (invoke/quiet grub-install "--boot-directory" install-dir |
589 | "--bootloader-id=Guix" | |
590 | "--efi-directory" target-esp)))) | |
b09a8da4 | 591 | |
c85f316a S |
592 | (define (install-grub-efi-netboot subdir) |
593 | "Define a grub-efi-netboot bootloader installer for installation in SUBDIR, | |
594 | which is usually efi/Guix or efi/boot." | |
595 | (let* ((system (string-split (nix-system->gnu-triplet | |
596 | (or (%current-target-system) | |
597 | (%current-system))) | |
598 | #\-)) | |
599 | (arch (first system)) | |
600 | (boot-efi-link (match system | |
601 | ;; These are the supportend systems and the names | |
602 | ;; defined by the UEFI standard for removable media. | |
603 | (("i686" _ ...) "/bootia32.efi") | |
604 | (("x86_64" _ ...) "/bootx64.efi") | |
605 | (("arm" _ ...) "/bootarm.efi") | |
606 | (("aarch64" _ ...) "/bootaa64.efi") | |
607 | (("riscv" _ ...) "/bootriscv32.efi") | |
608 | (("riscv64" _ ...) "/bootriscv64.efi") | |
609 | ;; Other systems are not supported, although defined. | |
610 | ;; (("riscv128" _ ...) "/bootriscv128.efi") | |
611 | ;; (("ia64" _ ...) "/bootia64.efi") | |
612 | ((_ ...) #f))) | |
613 | (core-efi (string-append | |
614 | ;; This is the arch dependent file name of GRUB, e.g. | |
615 | ;; i368-efi/core.efi or arm64-efi/core.efi. | |
616 | (match arch | |
617 | ("i686" "i386") | |
618 | ("aarch64" "arm64") | |
619 | ("riscv" "riscv32") | |
620 | (_ arch)) | |
621 | "-efi/core.efi"))) | |
622 | (with-imported-modules | |
623 | '((guix build union)) | |
624 | #~(lambda (bootloader target mount-point) | |
625 | "Install the BOOTLOADER, which must be the package grub, as e.g. | |
626 | bootx64.efi or bootaa64.efi into SUBDIR, which is usually efi/Guix or efi/boot, | |
627 | below the directory TARGET for the system whose root is mounted at MOUNT-POINT. | |
628 | ||
629 | MOUNT-POINT is the last argument in 'guix system init /etc/config.scm mnt/point' | |
630 | or '/' for other 'guix system' commands. | |
631 | ||
632 | TARGET is the target argument given to the bootloader-configuration in | |
633 | ||
634 | (operating-system | |
635 | (bootloader (bootloader-configuration | |
636 | (target \"/boot\") | |
637 | …)) | |
638 | …) | |
639 | ||
640 | TARGET is required to be an absolute directory name, usually mounted via NFS, | |
641 | and finally needs to be provided by a TFTP server as the TFTP root directory. | |
642 | ||
643 | GRUB will load tftp://server/SUBDIR/grub.cfg and this file will instruct it to | |
644 | load more files from the store like tftp://server/gnu/store/…-linux…/Image. | |
645 | ||
646 | To make this possible two symlinks will be created. The first symlink points | |
647 | relatively form MOUNT-POINT/TARGET/SUBDIR/grub.cfg to | |
648 | MOUNT-POINT/boot/grub/grub.cfg, and the second symlink points relatively from | |
649 | MOUNT-POINT/TARGET/%store-prefix to MOUNT-POINT/%store-prefix. | |
650 | ||
651 | It is important to note that these symlinks need to be relativ, as the absolute | |
652 | paths on the TFTP server side are unknown. | |
653 | ||
654 | It is also important to note that both symlinks will point outside the TFTP root | |
655 | directory and that the TARGET/%store-prefix symlink makes the whole store | |
656 | accessible via TFTP. Possibly the TFTP server must be configured | |
657 | to allow accesses outside its TFTP root directory. This may need to be | |
658 | considered for security aspects." | |
659 | (use-modules ((guix build union) #:select (symlink-relative))) | |
660 | (let* ((net-dir (string-append mount-point target "/")) | |
661 | (sub-dir (string-append net-dir #$subdir "/")) | |
662 | (store (string-append mount-point (%store-prefix))) | |
663 | (store-link (string-append net-dir (%store-prefix))) | |
664 | (grub-cfg (string-append mount-point "/boot/grub/grub.cfg")) | |
665 | (grub-cfg-link (string-append sub-dir (basename grub-cfg))) | |
666 | (boot-efi-link (string-append sub-dir #$boot-efi-link))) | |
667 | ;; Prepare the symlink to the store. | |
668 | (mkdir-p (dirname store-link)) | |
669 | (false-if-exception (delete-file store-link)) | |
670 | (symlink-relative store store-link) | |
671 | ;; Prepare the symlink to the grub.cfg, which points into the store. | |
672 | (mkdir-p (dirname grub-cfg-link)) | |
673 | (false-if-exception (delete-file grub-cfg-link)) | |
674 | (symlink-relative grub-cfg grub-cfg-link) | |
675 | ;; Install GRUB, which refers to the grub.cfg, with support for | |
676 | ;; encrypted partitions, | |
677 | (setenv "GRUB_ENABLE_CRYPTODISK" "y") | |
678 | (invoke/quiet (string-append bootloader "/bin/grub-mknetdir") | |
679 | (string-append "--net-directory=" net-dir) | |
680 | (string-append "--subdir=" #$subdir)) | |
681 | ;; Prepare the bootloader symlink, which points to core.efi of GRUB. | |
682 | (false-if-exception (delete-file boot-efi-link)) | |
683 | (symlink #$core-efi boot-efi-link)))))) | |
684 | ||
b09a8da4 MO |
685 | \f |
686 | ||
687 | ;;; | |
688 | ;;; Bootloader definitions. | |
689 | ;;; | |
3f2bd9df S |
690 | ;;; For all these grub-bootloader variables the path to /boot/grub/grub.cfg |
691 | ;;; is fixed. Inheriting and overwriting the field 'configuration-file' will | |
692 | ;;; break 'guix system delete-generations', 'guix system switch-generation', | |
693 | ;;; and 'guix system roll-back'. | |
b09a8da4 MO |
694 | |
695 | (define grub-bootloader | |
696 | (bootloader | |
697 | (name 'grub) | |
698 | (package grub) | |
699 | (installer install-grub) | |
7feefb3b | 700 | (disk-image-installer install-grub-disk-image) |
b09a8da4 MO |
701 | (configuration-file "/boot/grub/grub.cfg") |
702 | (configuration-file-generator grub-configuration-file))) | |
703 | ||
f70c2bd9 | 704 | (define grub-minimal-bootloader |
6a790fe3 | 705 | (bootloader |
7202895e MO |
706 | (inherit grub-bootloader) |
707 | (package grub-minimal))) | |
6a790fe3 | 708 | |
f70c2bd9 | 709 | (define grub-efi-bootloader |
b09a8da4 MO |
710 | (bootloader |
711 | (inherit grub-bootloader) | |
2941b347 | 712 | (installer install-grub-efi) |
7feefb3b | 713 | (disk-image-installer #f) |
b09a8da4 MO |
714 | (name 'grub-efi) |
715 | (package grub-efi))) | |
716 | ||
c85f316a S |
717 | (define grub-efi-netboot-bootloader |
718 | (bootloader | |
719 | (inherit grub-efi-bootloader) | |
720 | (name 'grub-efi-netboot-bootloader) | |
721 | (installer (install-grub-efi-netboot "efi/Guix")))) | |
722 | ||
f70c2bd9 | 723 | (define grub-mkrescue-bootloader |
cf189709 DM |
724 | (bootloader |
725 | (inherit grub-efi-bootloader) | |
726 | (package grub-hybrid))) | |
727 | ||
b09a8da4 MO |
728 | \f |
729 | ;;; | |
730 | ;;; Compatibility macros. | |
731 | ;;; | |
732 | ||
733 | (define-syntax grub-configuration | |
734 | (syntax-rules (grub) | |
735 | ((_ (grub package) fields ...) | |
736 | (if (eq? package grub) | |
737 | (bootloader-configuration | |
738 | (bootloader grub-bootloader) | |
739 | fields ...) | |
740 | (bootloader-configuration | |
741 | (bootloader grub-efi-bootloader) | |
742 | fields ...))) | |
743 | ((_ fields ...) | |
744 | (bootloader-configuration | |
745 | (bootloader grub-bootloader) | |
746 | fields ...)))) | |
747 | ||
0ded70f3 | 748 | ;;; grub.scm ends here |