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 store)
24 #:use-module (guix packages)
25 #:use-module (guix derivations)
26 #:use-module (guix records)
27 #:use-module (guix monads)
28 #:use-module (guix gexp)
29 #:use-module (guix download)
30 #:use-module (gnu artwork)
31 #:use-module (gnu system)
32 #:use-module (gnu bootloader)
33 #:use-module (gnu system uuid)
34 #:use-module (gnu system file-systems)
35 #:autoload (gnu packages bootloaders) (grub)
36 #:autoload (gnu packages compression) (gzip)
37 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
38 #:autoload (gnu packages guile) (guile-2.2)
39 #:use-module (ice-9 match)
40 #:use-module (ice-9 regex)
41 #:use-module (srfi srfi-1)
42 #:use-module (rnrs bytevectors)
45 grub-image-aspect-ratio
51 grub-theme-color-normal
52 grub-theme-color-highlight
59 grub-mkrescue-bootloader
65 ;;; Configuration of GNU GRUB.
69 (define (strip-mount-point mount-point file)
70 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
71 denoting a file name."
73 ((? string? mount-point)
74 (if (string=? mount-point "/")
76 #~(let ((file #$file))
77 (if (string-prefix? #$mount-point file)
78 (substring #$file #$(string-length mount-point))
82 (define-record-type* <grub-image>
83 grub-image make-grub-image
85 (aspect-ratio grub-image-aspect-ratio ;rational number
87 (file grub-image-file)) ;file-valued gexp (SVG)
89 (define-record-type* <grub-theme>
90 grub-theme make-grub-theme
92 (images grub-theme-images
93 (default '())) ;list of <grub-image>
94 (color-normal grub-theme-color-normal
95 (default '((fg . cyan) (bg . blue))))
96 (color-highlight grub-theme-color-highlight
97 (default '((fg . white) (bg . blue)))))
99 (define %background-image
102 (file (file-append %artwork-repository
103 "/grub/GuixSD-fully-black-4-3.svg"))))
105 (define %default-theme
106 ;; Default theme contributed by Felipe López.
108 (images (list %background-image))
109 (color-highlight '((fg . yellow) (bg . black)))
110 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
114 ;;; Background image & themes.
117 (define (bootloader-theme config)
118 "Return user defined theme in CONFIG if defined or %default-theme
120 (or (bootloader-configuration-theme config) %default-theme))
122 (define* (svg->png svg #:key width height)
123 "Build a PNG of HEIGHT x WIDTH from SVG."
124 ;; Note: Guile-RSVG & co. are now built for Guile 2.2, so we use 2.2 here.
125 ;; TODO: Remove #:guile-for-build when 2.2 has become the default.
126 (mlet %store-monad ((guile (package->derivation guile-2.2 #:graft? #f)))
127 (gexp->derivation "grub-image.png"
128 (with-imported-modules '((gnu build svg))
130 ;; We need these two libraries.
131 (add-to-load-path (string-append #+guile-rsvg
133 (effective-version)))
134 (add-to-load-path (string-append #+guile-cairo
136 (effective-version)))
138 (use-modules (gnu build svg))
139 (svg->png #+svg #$output
142 #:guile-for-build guile)))
144 (define* (grub-background-image config #:key (width 1024) (height 768))
145 "Return the GRUB background image defined in CONFIG with a ratio of
146 WIDTH/HEIGHT, or #f if none was found."
147 (let* ((ratio (/ width height))
148 (image (find (lambda (image)
149 (= (grub-image-aspect-ratio image) ratio))
151 (bootloader-theme config)))))
153 (svg->png (grub-image-file image)
154 #:width width #:height height)
155 (with-monad %store-monad
158 (define* (eye-candy config store-device store-mount-point
160 "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the
161 'grub.cfg' part concerned with graphics mode, background images, colors, and
162 all that. STORE-DEVICE designates the device holding the store, and
163 STORE-MOUNT-POINT is its mount point; these are used to determine where the
164 background image and fonts must be searched for. SYSTEM must be the target
165 system string---e.g., \"x86_64-linux\"."
166 (define setup-gfxterm-body
167 ;; Intel and EFI systems need to be switched into graphics mode, whereas
168 ;; most other modern architectures have no other mode and therefore don't
169 ;; need to be switched.
170 (if (string-match "^(x86_64|i[3-6]86)-" system)
172 # Leave 'gfxmode' to 'auto'.
177 if [ \"${grub_platform}\" == efi ]; then
178 # This is for (U)EFI systems (these modules are unavailable in the
179 # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\",
180 # which isn't convenient.
184 # These are specific to non-EFI Intel machines.
191 (define (setup-gfxterm config font-file)
192 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
193 #~(format #f "if loadfont ~a; then
198 (define (theme-colors type)
199 (let* ((theme (bootloader-theme config))
200 (colors (type theme)))
201 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
202 (symbol->string (assoc-ref colors 'bg)))))
205 (strip-mount-point store-mount-point
206 (file-append grub "/share/grub/unicode.pf2")))
208 (mlet* %store-monad ((image (grub-background-image config)))
211 function setup_gfxterm {~a}
213 # Set 'root' to the partition that contains /gnu/store.
220 if background_image ~a; then
222 set color_highlight=~a
224 set menu_color_normal=cyan/blue
225 set menu_color_highlight=white/blue
228 #$(grub-root-search store-device font-file)
229 #$(setup-gfxterm config font-file)
230 #$(grub-setup-io config)
232 #$(strip-mount-point store-mount-point image)
233 #$(theme-colors grub-theme-color-normal)
234 #$(theme-colors grub-theme-color-highlight))))))
238 ;;; Configuration file.
241 (define (grub-setup-io config)
242 "Return GRUB commands to configure the input / output interfaces. The result
243 is a string that can be inserted in grub.cfg."
244 (let* ((symbols->string (lambda (list)
245 (string-join (map symbol->string list) " ")))
246 (outputs (bootloader-configuration-terminal-outputs config))
247 (inputs (bootloader-configuration-terminal-inputs config))
248 (unit (bootloader-configuration-serial-unit config))
249 (speed (bootloader-configuration-serial-speed config))
251 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
252 ;; as documented in GRUB manual section "Simple Configuration
254 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
255 gfxterm vga_text mda_text morse spkmodem))
256 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
257 at_keyboard usb_keyboard))
264 (if (memq output valid-outputs) output #f)) outputs)) "\n"
272 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
273 ;; UNIT and SPEED are arguments to the same GRUB command
274 ;; ("serial"), so we process them together.
279 ;; COM ports 1 through 4
280 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
281 (string-append " --unit=" (number->string unit))
285 (if (exact-integer? speed)
286 (string-append " --speed=" (number->string speed))
290 (format #f "~a" io)))
292 (define (grub-root-search device file)
293 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
294 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
296 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
297 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
298 ;; custom menu entries. In the latter case, don't emit a 'search' command.
299 (if (and (string? file) (not (string-prefix? "/" file)))
302 ;; Preferably refer to DEVICE by its UUID or label. This is more
303 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
305 (format #f "search --fs-uuid --set ~a"
306 (uuid->string device)))
307 ((? file-system-label? label)
308 (format #f "search --label --set ~a"
309 (file-system-label->string label)))
311 #~(format #f "search --file --set ~a" #$file)))))
313 (define* (grub-configuration-file config entries
315 (system (%current-system))
317 "Return the GRUB configuration file corresponding to CONFIG, a
318 <bootloader-configuration> object, and where the store is available at
319 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
320 entries corresponding to old generations of the system."
322 (append entries (bootloader-configuration-menu-entries config)))
323 (define (menu-entry->gexp entry)
324 (let ((device (menu-entry-device entry))
325 (device-mount-point (menu-entry-device-mount-point entry))
326 (label (menu-entry-label entry))
327 (kernel (menu-entry-linux entry))
328 (arguments (menu-entry-linux-arguments entry))
329 (initrd (menu-entry-initrd entry)))
330 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
331 ;; Use the right file names for KERNEL and INITRD in case
332 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
333 ;; separate partition.
334 (let ((kernel (strip-mount-point device-mount-point kernel))
335 (initrd (strip-mount-point device-mount-point initrd)))
336 #~(format port "menuentry ~s {
342 #$(grub-root-search device kernel)
343 #$kernel (string-join (list #$@arguments))
345 (mlet %store-monad ((sugar (eye-candy config
348 (menu-entry-device-mount-point
353 #~(call-with-output-file #$output
356 "# This file was generated from your GuixSD configuration. Any changes
357 # will be lost upon reconfiguration.
363 #$(bootloader-configuration-default-entry config)
364 #$(bootloader-configuration-timeout config))
365 #$@(map menu-entry->gexp all-entries)
367 #$@(if (pair? old-entries)
369 submenu \"GNU system, old configurations...\" {~%")
370 #$@(map menu-entry->gexp old-entries)
374 (gexp->derivation "grub.cfg" builder)))
379 ;;; Install procedures.
383 #~(lambda (bootloader device mount-point)
384 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
385 (let ((grub (string-append bootloader "/sbin/grub-install"))
386 (install-dir (string-append mount-point "/boot")))
387 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
389 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
391 (unless (zero? (system* grub "--no-floppy" "--target=i386-pc"
392 "--boot-directory" install-dir
394 (error "failed to install GRUB (BIOS)")))))
396 (define install-grub-efi
397 #~(lambda (bootloader efi-dir mount-point)
398 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
399 ;; system whose root is mounted at MOUNT-POINT.
400 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
401 (install-dir (string-append mount-point "/boot")))
402 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
404 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
405 (unless (zero? (system* grub-install "--boot-directory" install-dir
406 "--efi-directory" efi-dir))
407 (error "failed to install GRUB (EFI)")))))
412 ;;; Bootloader definitions.
415 (define grub-bootloader
419 (installer install-grub)
420 (configuration-file "/boot/grub/grub.cfg")
421 (configuration-file-generator grub-configuration-file)))
423 (define* grub-efi-bootloader
425 (inherit grub-bootloader)
426 (installer install-grub-efi)
430 (define* grub-mkrescue-bootloader
432 (inherit grub-efi-bootloader)
433 (package grub-hybrid)))
437 ;;; Compatibility macros.
440 (define-syntax grub-configuration
442 ((_ (grub package) fields ...)
443 (if (eq? package grub)
444 (bootloader-configuration
445 (bootloader grub-bootloader)
447 (bootloader-configuration
448 (bootloader grub-efi-bootloader)
451 (bootloader-configuration
452 (bootloader grub-bootloader)
455 ;;; grub.scm ends here