1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 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>
7 ;;; This file is part of GNU Guix.
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.
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.
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/>.
22 (define-module (gnu bootloader grub)
23 #:use-module (guix records)
24 #:use-module ((guix utils) #:select (%current-system))
25 #:use-module (guix gexp)
26 #:use-module (gnu artwork)
27 #:use-module (gnu bootloader)
28 #:use-module (gnu system uuid)
29 #:use-module (gnu system file-systems)
30 #:autoload (gnu packages bootloaders) (grub)
31 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
32 #:use-module (ice-9 match)
33 #:use-module (ice-9 regex)
34 #:use-module (srfi srfi-1)
37 grub-image-aspect-ratio
43 grub-theme-color-normal
44 grub-theme-color-highlight
51 grub-mkrescue-bootloader
57 ;;; Configuration of GNU GRUB.
61 (define (strip-mount-point mount-point file)
62 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
63 denoting a file name."
65 ((? string? mount-point)
66 (if (string=? mount-point "/")
68 #~(let ((file #$file))
69 (if (string-prefix? #$mount-point file)
70 (substring #$file #$(string-length mount-point))
74 (define-record-type* <grub-image>
75 grub-image make-grub-image
77 (aspect-ratio grub-image-aspect-ratio ;rational number
79 (file grub-image-file)) ;file-valued gexp (SVG)
81 (define-record-type* <grub-theme>
82 grub-theme make-grub-theme
84 (images grub-theme-images
85 (default '())) ;list of <grub-image>
86 (color-normal grub-theme-color-normal
87 (default '((fg . cyan) (bg . blue))))
88 (color-highlight grub-theme-color-highlight
89 (default '((fg . white) (bg . blue)))))
91 (define %background-image
94 (file (file-append %artwork-repository
95 "/grub/GuixSD-fully-black-4-3.svg"))))
97 (define %default-theme
98 ;; Default theme contributed by Felipe López.
100 (images (list %background-image))
101 (color-highlight '((fg . yellow) (bg . black)))
102 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
106 ;;; Background image & themes.
109 (define (bootloader-theme config)
110 "Return user defined theme in CONFIG if defined or %default-theme
112 (or (bootloader-configuration-theme config) %default-theme))
114 (define* (svg->png svg #:key width height)
115 "Build a PNG of HEIGHT x WIDTH from SVG."
116 (computed-file "grub-image.png"
117 (with-imported-modules '((gnu build svg))
118 (with-extensions (list guile-rsvg guile-cairo)
120 (use-modules (gnu build svg))
121 (svg->png #+svg #$output
123 #:height #$height))))))
125 (define* (grub-background-image config #:key (width 1024) (height 768))
126 "Return the GRUB background image defined in CONFIG with a ratio of
127 WIDTH/HEIGHT, or #f if none was found."
128 (let* ((ratio (/ width height))
129 (image (find (lambda (image)
130 (= (grub-image-aspect-ratio image) ratio))
132 (bootloader-theme config)))))
134 (svg->png (grub-image-file image)
135 #:width width #:height height))))
137 (define* (eye-candy config store-device store-mount-point
139 "Return a gexp that writes to PORT (a port-valued gexp) the
140 'grub.cfg' part concerned with graphics mode, background images, colors, and
141 all that. STORE-DEVICE designates the device holding the store, and
142 STORE-MOUNT-POINT is its mount point; these are used to determine where the
143 background image and fonts must be searched for. SYSTEM must be the target
144 system string---e.g., \"x86_64-linux\"."
145 (define setup-gfxterm-body
146 ;; Intel and EFI systems need to be switched into graphics mode, whereas
147 ;; most other modern architectures have no other mode and therefore don't
148 ;; need to be switched.
149 (if (string-match "^(x86_64|i[3-6]86)-" system)
151 # Leave 'gfxmode' to 'auto'.
156 if [ \"${grub_platform}\" == efi ]; then
157 # This is for (U)EFI systems (these modules are unavailable in the
158 # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\",
159 # which isn't convenient.
163 # These are specific to non-EFI Intel machines.
170 (define (setup-gfxterm config font-file)
171 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
172 #~(format #f "if loadfont ~a; then
177 (define (theme-colors type)
178 (let* ((theme (bootloader-theme config))
179 (colors (type theme)))
180 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
181 (symbol->string (assoc-ref colors 'bg)))))
184 (strip-mount-point store-mount-point
185 (file-append grub "/share/grub/unicode.pf2")))
188 (grub-background-image config))
192 function setup_gfxterm {~a}
194 # Set 'root' to the partition that contains /gnu/store.
201 if background_image ~a; then
203 set color_highlight=~a
205 set menu_color_normal=cyan/blue
206 set menu_color_highlight=white/blue
209 #$(grub-root-search store-device font-file)
210 #$(setup-gfxterm config font-file)
211 #$(grub-setup-io config)
213 #$(strip-mount-point store-mount-point image)
214 #$(theme-colors grub-theme-color-normal)
215 #$(theme-colors grub-theme-color-highlight))))
219 ;;; Configuration file.
222 (define (grub-setup-io config)
223 "Return GRUB commands to configure the input / output interfaces. The result
224 is a string that can be inserted in grub.cfg."
225 (let* ((symbols->string (lambda (list)
226 (string-join (map symbol->string list) " ")))
227 (outputs (bootloader-configuration-terminal-outputs config))
228 (inputs (bootloader-configuration-terminal-inputs config))
229 (unit (bootloader-configuration-serial-unit config))
230 (speed (bootloader-configuration-serial-speed config))
232 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
233 ;; as documented in GRUB manual section "Simple Configuration
235 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
236 gfxterm vga_text mda_text morse spkmodem))
237 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
238 at_keyboard usb_keyboard))
245 (if (memq output valid-outputs) output #f)) outputs)) "\n"
253 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
254 ;; UNIT and SPEED are arguments to the same GRUB command
255 ;; ("serial"), so we process them together.
260 ;; COM ports 1 through 4
261 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
262 (string-append " --unit=" (number->string unit))
266 (if (exact-integer? speed)
267 (string-append " --speed=" (number->string speed))
271 (format #f "~a" io)))
273 (define (grub-root-search device file)
274 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
275 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
277 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
278 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
279 ;; custom menu entries. In the latter case, don't emit a 'search' command.
280 (if (and (string? file) (not (string-prefix? "/" file)))
283 ;; Preferably refer to DEVICE by its UUID or label. This is more
284 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
286 (format #f "search --fs-uuid --set ~a"
287 (uuid->string device)))
288 ((? file-system-label? label)
289 (format #f "search --label --set ~a"
290 (file-system-label->string label)))
292 #~(format #f "search --file --set ~a" #$file)))))
294 (define* (grub-configuration-file config entries
296 (system (%current-system))
298 "Return the GRUB configuration file corresponding to CONFIG, a
299 <bootloader-configuration> object, and where the store is available at
300 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
301 entries corresponding to old generations of the system."
303 (append entries (bootloader-configuration-menu-entries config)))
304 (define (menu-entry->gexp entry)
305 (let ((device (menu-entry-device entry))
306 (device-mount-point (menu-entry-device-mount-point entry))
307 (label (menu-entry-label entry))
308 (kernel (menu-entry-linux entry))
309 (arguments (menu-entry-linux-arguments entry))
310 (initrd (menu-entry-initrd entry)))
311 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
312 ;; Use the right file names for KERNEL and INITRD in case
313 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
314 ;; separate partition.
315 (let ((kernel (strip-mount-point device-mount-point kernel))
316 (initrd (strip-mount-point device-mount-point initrd)))
317 #~(format port "menuentry ~s {
323 #$(grub-root-search device kernel)
324 #$kernel (string-join (list #$@arguments))
328 (menu-entry-device (first all-entries))
329 (menu-entry-device-mount-point (first all-entries))
334 #~(call-with-output-file #$output
337 "# This file was generated from your GuixSD configuration. Any changes
338 # will be lost upon reconfiguration.
344 #$(bootloader-configuration-default-entry config)
345 #$(bootloader-configuration-timeout config))
346 #$@(map menu-entry->gexp all-entries)
348 #$@(if (pair? old-entries)
350 submenu \"GNU system, old configurations...\" {~%")
351 #$@(map menu-entry->gexp old-entries)
355 (computed-file "grub.cfg" builder))
360 ;;; Install procedures.
364 #~(lambda (bootloader device mount-point)
365 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
366 (let ((grub (string-append bootloader "/sbin/grub-install"))
367 (install-dir (string-append mount-point "/boot")))
368 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
370 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
372 (unless (zero? (system* grub "--no-floppy" "--target=i386-pc"
373 "--boot-directory" install-dir
375 (error "failed to install GRUB (BIOS)")))))
377 (define install-grub-efi
378 #~(lambda (bootloader efi-dir mount-point)
379 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
380 ;; system whose root is mounted at MOUNT-POINT.
381 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
382 (install-dir (string-append mount-point "/boot"))
383 ;; When installing GuixSD, it's common to mount EFI-DIR below
384 ;; MOUNT-POINT rather than /boot/efi on the live image.
385 (target-esp (if (file-exists? (string-append mount-point efi-dir))
386 (string-append mount-point efi-dir)
388 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
390 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
391 (unless (zero? (system* grub-install "--boot-directory" install-dir
392 "--bootloader-id=GuixSD"
393 "--efi-directory" target-esp))
394 (error "failed to install GRUB (EFI)")))))
399 ;;; Bootloader definitions.
402 (define grub-bootloader
406 (installer install-grub)
407 (configuration-file "/boot/grub/grub.cfg")
408 (configuration-file-generator grub-configuration-file)))
410 (define* grub-efi-bootloader
412 (inherit grub-bootloader)
413 (installer install-grub-efi)
417 (define* grub-mkrescue-bootloader
419 (inherit grub-efi-bootloader)
420 (package grub-hybrid)))
424 ;;; Compatibility macros.
427 (define-syntax grub-configuration
429 ((_ (grub package) fields ...)
430 (if (eq? package grub)
431 (bootloader-configuration
432 (bootloader grub-bootloader)
434 (bootloader-configuration
435 (bootloader grub-efi-bootloader)
438 (bootloader-configuration
439 (bootloader grub-bootloader)
442 ;;; grub.scm ends here