1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
7 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
9 ;;; This file is part of GNU Guix.
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.
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.
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/>.
24 (define-module (gnu bootloader grub)
25 #:use-module (guix records)
26 #:use-module ((guix utils) #:select (%current-system))
27 #:use-module (guix gexp)
28 #:use-module (gnu artwork)
29 #:use-module (gnu bootloader)
30 #:use-module (gnu system uuid)
31 #:use-module (gnu system file-systems)
32 #:use-module (gnu system keyboard)
33 #:use-module (gnu packages bootloaders)
34 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
35 #:autoload (gnu packages xorg) (xkeyboard-config)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 regex)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-2)
42 grub-image-aspect-ratio
48 grub-theme-color-normal
49 grub-theme-color-highlight
56 grub-mkrescue-bootloader
62 ;;; Configuration of GNU GRUB.
66 (define (strip-mount-point mount-point file)
67 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
68 denoting a file name."
70 ((? string? mount-point)
71 (if (string=? mount-point "/")
73 #~(let ((file #$file))
74 (if (string-prefix? #$mount-point file)
75 (substring #$file #$(string-length mount-point))
79 (define-record-type* <grub-image>
80 grub-image make-grub-image
82 (aspect-ratio grub-image-aspect-ratio ;rational number
84 (file grub-image-file)) ;file-valued gexp (SVG)
86 (define-record-type* <grub-theme>
87 grub-theme make-grub-theme
89 (images grub-theme-images
90 (default '())) ;list of <grub-image>
91 (color-normal grub-theme-color-normal
92 (default '((fg . cyan) (bg . blue))))
93 (color-highlight grub-theme-color-highlight
94 (default '((fg . white) (bg . blue))))
96 (default '("auto")))) ;list of string
98 (define %background-image
101 (file (file-append %artwork-repository
102 "/grub/GuixSD-fully-black-4-3.svg"))))
104 (define %default-theme
105 ;; Default theme contributed by Felipe López.
107 (images (list %background-image))
108 (color-highlight '((fg . yellow) (bg . black)))
109 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
113 ;;; Background image & themes.
116 (define (bootloader-theme config)
117 "Return user defined theme in CONFIG if defined or %default-theme
119 (or (bootloader-configuration-theme config) %default-theme))
121 (define* (svg->png svg #:key width height)
122 "Build a PNG of HEIGHT x WIDTH from SVG."
123 (computed-file "grub-image.png"
124 (with-imported-modules '((gnu build svg))
125 (with-extensions (list guile-rsvg guile-cairo)
127 (use-modules (gnu build svg))
128 (svg->png #+svg #$output
130 #:height #$height))))))
132 (define* (grub-background-image config #:key (width 1024) (height 768))
133 "Return the GRUB background image defined in CONFIG with a ratio of
134 WIDTH/HEIGHT, or #f if none was found."
135 (let* ((ratio (/ width height))
136 (image (find (lambda (image)
137 (= (grub-image-aspect-ratio image) ratio))
139 (bootloader-theme config)))))
141 (svg->png (grub-image-file image)
142 #:width width #:height height))))
144 (define* (eye-candy config store-device store-mount-point
146 "Return a gexp that writes to PORT (a port-valued gexp) the
147 'grub.cfg' part concerned with graphics mode, background images, colors, and
148 all that. STORE-DEVICE designates the device holding the store, and
149 STORE-MOUNT-POINT is its mount point; these are used to determine where the
150 background image and fonts must be searched for. SYSTEM must be the target
151 system string---e.g., \"x86_64-linux\"."
152 (define setup-gfxterm-body
154 (or (and-let* ((theme (bootloader-configuration-theme config))
155 (gfxmode (grub-gfxmode theme)))
156 (string-join gfxmode ";"))
159 ;; Intel and EFI systems need to be switched into graphics mode, whereas
160 ;; most other modern architectures have no other mode and therefore
161 ;; don't need to be switched.
163 ;; XXX: Do we really need to restrict to x86 systems? We could imitate
164 ;; what the GRUB default configuration does and decide based on whether
165 ;; a user provided 'gfxterm' in the terminal-outputs field of their
166 ;; bootloader-configuration record.
167 (if (string-match "^(x86_64|i[3-6]86)-" system)
171 insmod gfxterm~%" gfxmode)
174 (define (setup-gfxterm config font-file)
175 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
176 #~(format #f "if loadfont ~a; then
181 (define (theme-colors type)
182 (let* ((theme (bootloader-theme config))
183 (colors (type theme)))
184 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
185 (symbol->string (assoc-ref colors 'bg)))))
188 (strip-mount-point store-mount-point
189 (file-append grub "/share/grub/unicode.pf2")))
192 (grub-background-image config))
196 function setup_gfxterm {~a}
198 # Set 'root' to the partition that contains /gnu/store.
205 if background_image ~a; then
207 set color_highlight=~a
209 set menu_color_normal=cyan/blue
210 set menu_color_highlight=white/blue
213 #$(grub-root-search store-device font-file)
214 #$(setup-gfxterm config font-file)
215 #$(grub-setup-io config)
217 #$(strip-mount-point store-mount-point image)
218 #$(theme-colors grub-theme-color-normal)
219 #$(theme-colors grub-theme-color-highlight))))
223 ;;; Configuration file.
226 (define* (keyboard-layout-file layout
229 "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
230 and return a file in the format for GRUB keymaps. LAYOUT must be present in
231 the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'."
233 (with-imported-modules '((guix build utils))
235 (use-modules (guix build utils))
237 ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp'
238 ;; (from the 'console-setup' package).
239 (invoke #$(file-append grub "/bin/grub-mklayout")
240 "-i" #+(keyboard-layout->console-keymap layout)
243 (computed-file (string-append "grub-keymap."
244 (string-map (match-lambda
247 (keyboard-layout-name layout)))
250 (define (grub-setup-io config)
251 "Return GRUB commands to configure the input / output interfaces. The result
252 is a string that can be inserted in grub.cfg."
253 (let* ((symbols->string (lambda (list)
254 (string-join (map symbol->string list) " ")))
255 (outputs (bootloader-configuration-terminal-outputs config))
256 (inputs (bootloader-configuration-terminal-inputs config))
257 (unit (bootloader-configuration-serial-unit config))
258 (speed (bootloader-configuration-serial-speed config))
260 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
261 ;; as documented in GRUB manual section "Simple Configuration
263 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
264 gfxterm vga_text mda_text morse spkmodem))
265 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
266 at_keyboard usb_keyboard))
273 (if (memq output valid-outputs) output #f)) outputs)) "\n"
281 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
282 ;; UNIT and SPEED are arguments to the same GRUB command
283 ;; ("serial"), so we process them together.
288 ;; COM ports 1 through 4
289 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
290 (string-append " --unit=" (number->string unit))
294 (if (exact-integer? speed)
295 (string-append " --speed=" (number->string speed))
299 (format #f "~a" io)))
301 (define (grub-root-search device file)
302 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
303 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
305 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
306 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
307 ;; custom menu entries. In the latter case, don't emit a 'search' command.
308 (if (and (string? file) (not (string-prefix? "/" file)))
311 ;; Preferably refer to DEVICE by its UUID or label. This is more
312 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
314 (format #f "search --fs-uuid --set ~a"
315 (uuid->string device)))
316 ((? file-system-label? label)
317 (format #f "search --label --set ~a"
318 (file-system-label->string label)))
320 #~(format #f "search --file --set ~a" #$file)))))
322 (define* (grub-configuration-file config entries
324 (system (%current-system))
326 "Return the GRUB configuration file corresponding to CONFIG, a
327 <bootloader-configuration> object, and where the store is available at
328 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
329 entries corresponding to old generations of the system."
331 (append entries (bootloader-configuration-menu-entries config)))
332 (define (menu-entry->gexp entry)
333 (let ((device (menu-entry-device entry))
334 (device-mount-point (menu-entry-device-mount-point entry))
335 (label (menu-entry-label entry))
336 (kernel (menu-entry-linux entry))
337 (arguments (menu-entry-linux-arguments entry))
338 (initrd (menu-entry-initrd entry)))
339 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
340 ;; Use the right file names for KERNEL and INITRD in case
341 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
342 ;; separate partition.
343 (let ((kernel (strip-mount-point device-mount-point kernel))
344 (initrd (strip-mount-point device-mount-point initrd)))
345 #~(format port "menuentry ~s {
351 #$(grub-root-search device kernel)
352 #$kernel (string-join (list #$@arguments))
356 (menu-entry-device (first all-entries))
357 (menu-entry-device-mount-point (first all-entries))
361 (define keyboard-layout-config
362 (let ((layout (bootloader-configuration-keyboard-layout config))
363 (grub (bootloader-package
364 (bootloader-configuration-bootloader config))))
365 #~(let ((keymap #$(and layout
366 (keyboard-layout-file layout #:grub grub))))
370 keymap ~a~%" keymap)))))
373 #~(call-with-output-file #$output
376 "# This file was generated from your Guix configuration. Any changes
377 # will be lost upon reconfiguration.
380 #$keyboard-layout-config
384 #$(bootloader-configuration-default-entry config)
385 #$(bootloader-configuration-timeout config))
386 #$@(map menu-entry->gexp all-entries)
388 #$@(if (pair? old-entries)
390 submenu \"GNU system, old configurations...\" {~%")
391 #$@(map menu-entry->gexp old-entries)
395 if [ \"${grub_platform}\" == efi ]; then
396 menuentry \"Firmware setup\" {
401 ;; Since this file is rather unique, there's no point in trying to
403 (computed-file "grub.cfg" builder
404 #:options '(#:local-build? #t
405 #:substitutable? #f)))
410 ;;; Install procedures.
414 #~(lambda (bootloader device mount-point)
415 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
416 (let ((grub (string-append bootloader "/sbin/grub-install"))
417 (install-dir (string-append mount-point "/boot")))
418 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
420 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
422 ;; Hide potentially confusing messages from the user, such as
423 ;; "Installing for i386-pc platform."
424 (invoke/quiet grub "--no-floppy" "--target=i386-pc"
425 "--boot-directory" install-dir
428 (define install-grub-efi
429 #~(lambda (bootloader efi-dir mount-point)
430 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
431 ;; system whose root is mounted at MOUNT-POINT.
432 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
433 (install-dir (string-append mount-point "/boot"))
434 ;; When installing Guix, it's common to mount EFI-DIR below
435 ;; MOUNT-POINT rather than /boot/efi on the live image.
436 (target-esp (if (file-exists? (string-append mount-point efi-dir))
437 (string-append mount-point efi-dir)
439 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
441 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
442 (invoke/quiet grub-install "--boot-directory" install-dir
443 "--bootloader-id=Guix"
444 "--efi-directory" target-esp))))
449 ;;; Bootloader definitions.
452 (define grub-bootloader
456 (installer install-grub)
457 (configuration-file "/boot/grub/grub.cfg")
458 (configuration-file-generator grub-configuration-file)))
460 (define* grub-efi-bootloader
462 (inherit grub-bootloader)
463 (installer install-grub-efi)
467 (define* grub-mkrescue-bootloader
469 (inherit grub-efi-bootloader)
470 (package grub-hybrid)))
474 ;;; Compatibility macros.
477 (define-syntax grub-configuration
479 ((_ (grub package) fields ...)
480 (if (eq? package grub)
481 (bootloader-configuration
482 (bootloader grub-bootloader)
484 (bootloader-configuration
485 (bootloader grub-efi-bootloader)
488 (bootloader-configuration
489 (bootloader grub-bootloader)
492 ;;; grub.scm ends here