1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 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 #:autoload (gnu packages bootloaders) (grub)
35 #:autoload (gnu packages compression) (gzip)
36 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
37 #:autoload (gnu packages guile) (guile-2.2)
38 #:use-module (ice-9 match)
39 #:use-module (ice-9 regex)
40 #:use-module (srfi srfi-1)
41 #:use-module (rnrs bytevectors)
44 grub-image-aspect-ratio
50 grub-theme-color-normal
51 grub-theme-color-highlight
58 grub-mkrescue-bootloader
64 ;;; Configuration of GNU GRUB.
68 (define (strip-mount-point mount-point file)
69 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
70 denoting a file name."
72 ((? string? mount-point)
73 (if (string=? mount-point "/")
75 #~(let ((file #$file))
76 (if (string-prefix? #$mount-point file)
77 (substring #$file #$(string-length mount-point))
81 (define-record-type* <grub-image>
82 grub-image make-grub-image
84 (aspect-ratio grub-image-aspect-ratio ;rational number
86 (file grub-image-file)) ;file-valued gexp (SVG)
88 (define-record-type* <grub-theme>
89 grub-theme make-grub-theme
91 (images grub-theme-images
92 (default '())) ;list of <grub-image>
93 (color-normal grub-theme-color-normal
94 (default '((fg . cyan) (bg . blue))))
95 (color-highlight grub-theme-color-highlight
96 (default '((fg . white) (bg . blue)))))
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 ;; Note: Guile-RSVG & co. are now built for Guile 2.2, so we use 2.2 here.
124 ;; TODO: Remove #:guile-for-build when 2.2 has become the default.
125 (mlet %store-monad ((guile (package->derivation guile-2.2 #:graft? #f)))
126 (gexp->derivation "grub-image.png"
127 (with-imported-modules '((gnu build svg))
129 ;; We need these two libraries.
130 (add-to-load-path (string-append #+guile-rsvg
132 (effective-version)))
133 (add-to-load-path (string-append #+guile-cairo
135 (effective-version)))
137 (use-modules (gnu build svg))
138 (svg->png #+svg #$output
141 #:guile-for-build guile)))
143 (define* (grub-background-image config #:key (width 1024) (height 768))
144 "Return the GRUB background image defined in CONFIG with a ratio of
145 WIDTH/HEIGHT, or #f if none was found."
146 (let* ((ratio (/ width height))
147 (image (find (lambda (image)
148 (= (grub-image-aspect-ratio image) ratio))
150 (bootloader-theme config)))))
152 (svg->png (grub-image-file image)
153 #:width width #:height height)
154 (with-monad %store-monad
157 (define* (eye-candy config store-device store-mount-point
159 "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the
160 'grub.cfg' part concerned with graphics mode, background images, colors, and
161 all that. STORE-DEVICE designates the device holding the store, and
162 STORE-MOUNT-POINT is its mount point; these are used to determine where the
163 background image and fonts must be searched for. SYSTEM must be the target
164 system string---e.g., \"x86_64-linux\"."
165 (define setup-gfxterm-body
166 ;; Intel and EFI systems need to be switched into graphics mode, whereas
167 ;; most other modern architectures have no other mode and therefore don't
168 ;; need to be switched.
169 (if (string-match "^(x86_64|i[3-6]86)-" system)
171 # Leave 'gfxmode' to 'auto'.
176 if [ \"${grub_platform}\" == efi ]; then
177 # This is for (U)EFI systems (these modules are unavailable in the
178 # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\",
179 # which isn't convenient.
183 # These are specific to non-EFI Intel machines.
190 (define (setup-gfxterm config font-file)
191 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
192 #~(format #f "if loadfont ~a; then
197 (define (theme-colors type)
198 (let* ((theme (bootloader-theme config))
199 (colors (type theme)))
200 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
201 (symbol->string (assoc-ref colors 'bg)))))
204 (strip-mount-point store-mount-point
205 (file-append grub "/share/grub/unicode.pf2")))
207 (mlet* %store-monad ((image (grub-background-image config)))
210 function setup_gfxterm {~a}
212 # Set 'root' to the partition that contains /gnu/store.
219 if background_image ~a; then
221 set color_highlight=~a
223 set menu_color_normal=cyan/blue
224 set menu_color_highlight=white/blue
227 #$(grub-root-search store-device font-file)
228 #$(setup-gfxterm config font-file)
229 #$(grub-setup-io config)
231 #$(strip-mount-point store-mount-point image)
232 #$(theme-colors grub-theme-color-normal)
233 #$(theme-colors grub-theme-color-highlight))))))
237 ;;; Configuration file.
240 (define (grub-setup-io config)
241 "Return GRUB commands to configure the input / output interfaces. The result
242 is a string that can be inserted in grub.cfg."
243 (let* ((symbols->string (lambda (list)
244 (string-join (map symbol->string list) " ")))
245 (outputs (bootloader-configuration-terminal-outputs config))
246 (inputs (bootloader-configuration-terminal-inputs config))
247 (unit (bootloader-configuration-serial-unit config))
248 (speed (bootloader-configuration-serial-speed config))
250 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
251 ;; as documented in GRUB manual section "Simple Configuration
253 (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
254 gfxterm vga_text mda_text morse spkmodem))
255 (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
256 at_keyboard usb_keyboard))
263 (if (memq output valid-outputs) output #f)) outputs)) "\n"
271 (if (memq input valid-inputs) input #f)) inputs)) "\n"))
272 ;; UNIT and SPEED are arguments to the same GRUB command
273 ;; ("serial"), so we process them together.
278 ;; COM ports 1 through 4
279 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
280 (string-append " --unit=" (number->string unit))
284 (if (exact-integer? speed)
285 (string-append " --speed=" (number->string speed))
289 (format #f "~a" io)))
291 (define (grub-root-search device file)
292 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
293 a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
295 ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
296 ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
297 ;; custom menu entries. In the latter case, don't emit a 'search' command.
298 (if (and (string? file) (not (string-prefix? "/" file)))
301 ;; Preferably refer to DEVICE by its UUID or label. This is more
302 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
304 (format #f "search --fs-uuid --set ~a"
305 (uuid->string device)))
307 (format #f "search --label --set ~a" label))
309 #~(format #f "search --file --set ~a" #$file)))))
311 (define* (grub-configuration-file config entries
313 (system (%current-system))
315 "Return the GRUB configuration file corresponding to CONFIG, a
316 <bootloader-configuration> object, and where the store is available at
317 STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
318 entries corresponding to old generations of the system."
320 (append entries (bootloader-configuration-menu-entries config)))
321 (define (menu-entry->gexp entry)
322 (let ((device (menu-entry-device entry))
323 (device-mount-point (menu-entry-device-mount-point entry))
324 (label (menu-entry-label entry))
325 (kernel (menu-entry-linux entry))
326 (arguments (menu-entry-linux-arguments entry))
327 (initrd (menu-entry-initrd entry)))
328 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
329 ;; Use the right file names for KERNEL and INITRD in case
330 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
331 ;; separate partition.
332 (let ((kernel (strip-mount-point device-mount-point kernel))
333 (initrd (strip-mount-point device-mount-point initrd)))
334 #~(format port "menuentry ~s {
340 #$(grub-root-search device kernel)
341 #$kernel (string-join (list #$@arguments))
343 (mlet %store-monad ((sugar (eye-candy config
346 (menu-entry-device-mount-point
351 #~(call-with-output-file #$output
354 "# This file was generated from your GuixSD configuration. Any changes
355 # will be lost upon reconfiguration.
361 #$(bootloader-configuration-default-entry config)
362 #$(bootloader-configuration-timeout config))
363 #$@(map menu-entry->gexp all-entries)
365 #$@(if (pair? old-entries)
367 submenu \"GNU system, old configurations...\" {~%")
368 #$@(map menu-entry->gexp old-entries)
372 (gexp->derivation "grub.cfg" builder)))
377 ;;; Install procedures.
381 #~(lambda (bootloader device mount-point)
382 ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
383 (let ((grub (string-append bootloader "/sbin/grub-install"))
384 (install-dir (string-append mount-point "/boot")))
385 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
387 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
389 (unless (zero? (system* grub "--no-floppy"
390 "--boot-directory" install-dir
392 (error "failed to install GRUB (BIOS)")))))
394 (define install-grub-efi
395 #~(lambda (bootloader efi-dir mount-point)
396 ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
397 ;; system whose root is mounted at MOUNT-POINT.
398 (let ((grub-install (string-append bootloader "/sbin/grub-install"))
399 (install-dir (string-append mount-point "/boot")))
400 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
402 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
403 (unless (zero? (system* grub-install "--boot-directory" install-dir
404 "--efi-directory" efi-dir))
405 (error "failed to install GRUB (EFI)")))))
410 ;;; Bootloader definitions.
413 (define grub-bootloader
417 (installer install-grub)
418 (configuration-file "/boot/grub/grub.cfg")
419 (configuration-file-generator grub-configuration-file)))
421 (define* grub-efi-bootloader
423 (inherit grub-bootloader)
424 (installer install-grub-efi)
428 (define* grub-mkrescue-bootloader
430 (inherit grub-efi-bootloader)
431 (package grub-hybrid)))
435 ;;; Compatibility macros.
438 (define-syntax grub-configuration
440 ((_ (grub package) fields ...)
441 (if (eq? package grub)
442 (bootloader-configuration
443 (bootloader grub-bootloader)
445 (bootloader-configuration
446 (bootloader grub-efi-bootloader)
449 (bootloader-configuration
450 (bootloader grub-bootloader)
453 ;;; grub.scm ends here