Remove traces of "GuixSD".
[jackhill/guix/guix.git] / gnu / bootloader / grub.scm
CommitLineData
0ded70f3 1;;; GNU Guix --- Functional package management for GNU
59e80445 2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
1ef8b72a 3;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
e0b2e930 4;;; Copyright © 2017 Leo Famulari <leo@famulari.name>
b09a8da4 5;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
0ded70f3
LC
6;;;
7;;; This file is part of GNU Guix.
8;;;
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.
13;;;
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.
18;;;
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/>.
21
b09a8da4 22(define-module (gnu bootloader grub)
0ded70f3 23 #:use-module (guix records)
46c296dc 24 #:use-module ((guix utils) #:select (%current-system))
f6a7b21d 25 #:use-module (guix gexp)
84dfb458 26 #:use-module (gnu artwork)
b09a8da4 27 #:use-module (gnu bootloader)
9b336338 28 #:use-module (gnu system uuid)
a5acc17a 29 #:use-module (gnu system file-systems)
862e38d5 30 #:autoload (gnu packages bootloaders) (grub)
ffde82c9 31 #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
0ded70f3 32 #:use-module (ice-9 match)
6b173ac0 33 #:use-module (ice-9 regex)
0ded70f3 34 #:use-module (srfi srfi-1)
99ae9ceb
LC
35 #:export (grub-image
36 grub-image?
37 grub-image-aspect-ratio
38 grub-image-file
39
40 grub-theme
41 grub-theme?
42 grub-theme-images
43 grub-theme-color-normal
44 grub-theme-color-highlight
45
46 %background-image
47 %default-theme
48
b09a8da4
MO
49 grub-bootloader
50 grub-efi-bootloader
cf189709 51 grub-mkrescue-bootloader
d5b429ab 52
b09a8da4 53 grub-configuration))
0ded70f3
LC
54
55;;; Commentary:
56;;;
57;;; Configuration of GNU GRUB.
58;;;
59;;; Code:
60
1ef8b72a
CM
61(define (strip-mount-point mount-point file)
62 "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
63denoting a file name."
8b22107e
MO
64 (match mount-point
65 ((? string? mount-point)
66 (if (string=? mount-point "/")
67 file
68 #~(let ((file #$file))
69 (if (string-prefix? #$mount-point file)
70 (substring #$file #$(string-length mount-point))
71 file))))
72 (#f file)))
0f65f54e 73
99ae9ceb
LC
74(define-record-type* <grub-image>
75 grub-image make-grub-image
76 grub-image?
77 (aspect-ratio grub-image-aspect-ratio ;rational number
78 (default 4/3))
79 (file grub-image-file)) ;file-valued gexp (SVG)
80
81(define-record-type* <grub-theme>
82 grub-theme make-grub-theme
83 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)))))
90
99ae9ceb
LC
91(define %background-image
92 (grub-image
93 (aspect-ratio 4/3)
357db1f9
LC
94 (file (file-append %artwork-repository
95 "/grub/GuixSD-fully-black-4-3.svg"))))
99ae9ceb
LC
96
97(define %default-theme
98 ;; Default theme contributed by Felipe López.
99 (grub-theme
100 (images (list %background-image))
9c09760a 101 (color-highlight '((fg . yellow) (bg . black)))
99ae9ceb
LC
102 (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
103
99ae9ceb
LC
104\f
105;;;
106;;; Background image & themes.
107;;;
108
b09a8da4
MO
109(define (bootloader-theme config)
110 "Return user defined theme in CONFIG if defined or %default-theme
111otherwise."
112 (or (bootloader-configuration-theme config) %default-theme))
113
ffde82c9
LC
114(define* (svg->png svg #:key width height)
115 "Build a PNG of HEIGHT x WIDTH from SVG."
46c296dc
LC
116 (computed-file "grub-image.png"
117 (with-imported-modules '((gnu build svg))
118 (with-extensions (list guile-rsvg guile-cairo)
119 #~(begin
120 (use-modules (gnu build svg))
121 (svg->png #+svg #$output
122 #:width #$width
123 #:height #$height))))))
99ae9ceb 124
6394fe65 125(define* (grub-background-image config #:key (width 1024) (height 768))
99ae9ceb
LC
126 "Return the GRUB background image defined in CONFIG with a ratio of
127WIDTH/HEIGHT, or #f if none was found."
128 (let* ((ratio (/ width height))
129 (image (find (lambda (image)
130 (= (grub-image-aspect-ratio image) ratio))
b09a8da4
MO
131 (grub-theme-images
132 (bootloader-theme config)))))
46c296dc
LC
133 (and image
134 (svg->png (grub-image-file image)
135 #:width width #:height height))))
99ae9ceb 136
1ef8b72a
CM
137(define* (eye-candy config store-device store-mount-point
138 #:key system port)
46c296dc 139 "Return a gexp that writes to PORT (a port-valued gexp) the
99ae9ceb 140'grub.cfg' part concerned with graphics mode, background images, colors, and
1ef8b72a
CM
141all that. STORE-DEVICE designates the device holding the store, and
142STORE-MOUNT-POINT is its mount point; these are used to determine where the
143background image and fonts must be searched for. SYSTEM must be the target
144system string---e.g., \"x86_64-linux\"."
6b173ac0 145 (define setup-gfxterm-body
4d0a3d8e
LC
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.
6b173ac0
MW
149 (if (string-match "^(x86_64|i[3-6]86)-" system)
150 "
122c3a1d 151 # Leave 'gfxmode' to 'auto'.
6b173ac0
MW
152 insmod video_bochs
153 insmod video_cirrus
154 insmod gfxterm
4d0a3d8e
LC
155
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.
160 insmod efi_gop
161 insmod efi_uga
5f19fb6a
LC
162 else
163 # These are specific to non-EFI Intel machines.
164 insmod vbe
165 insmod vga
4d0a3d8e 166 fi
6b173ac0
MW
167"
168 ""))
169
e0b2e930 170 (define (setup-gfxterm config font-file)
b09a8da4
MO
171 (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
172 #~(format #f "if loadfont ~a; then
e0b2e930
LF
173 setup_gfxterm
174fi~%" #$font-file)
b09a8da4 175 ""))
e0b2e930 176
99ae9ceb 177 (define (theme-colors type)
b09a8da4 178 (let* ((theme (bootloader-theme config))
99ae9ceb
LC
179 (colors (type theme)))
180 (string-append (symbol->string (assoc-ref colors 'fg)) "/"
181 (symbol->string (assoc-ref colors 'bg)))))
182
6b779207 183 (define font-file
1ef8b72a 184 (strip-mount-point store-mount-point
0f65f54e 185 (file-append grub "/share/grub/unicode.pf2")))
6b779207 186
46c296dc
LC
187 (define image
188 (grub-background-image config))
189
190 (and image
191 #~(format #$port "
6b173ac0 192function setup_gfxterm {~a}
99ae9ceb 193
ccc2678b 194# Set 'root' to the partition that contains /gnu/store.
6b779207 195~a
ccc2678b 196
e0b2e930
LF
197~a
198~a
99ae9ceb
LC
199
200insmod png
201if background_image ~a; then
202 set color_normal=~a
203 set color_highlight=~a
204else
205 set menu_color_normal=cyan/blue
206 set menu_color_highlight=white/blue
207fi~%"
46c296dc
LC
208 #$setup-gfxterm-body
209 #$(grub-root-search store-device font-file)
210 #$(setup-gfxterm config font-file)
211 #$(grub-setup-io config)
6b779207 212
46c296dc
LC
213 #$(strip-mount-point store-mount-point image)
214 #$(theme-colors grub-theme-color-normal)
215 #$(theme-colors grub-theme-color-highlight))))
99ae9ceb
LC
216
217\f
218;;;
219;;; Configuration file.
220;;;
221
e0b2e930
LF
222(define (grub-setup-io config)
223 "Return GRUB commands to configure the input / output interfaces. The result
224is a string that can be inserted in grub.cfg."
225 (let* ((symbols->string (lambda (list)
226 (string-join (map symbol->string list) " ")))
b09a8da4
MO
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))
e0b2e930
LF
231
232 ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
233 ;; as documented in GRUB manual section "Simple Configuration
234 ;; Handling".
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))
239
240 (io (string-append
241 "terminal_output "
242 (symbols->string
243 (map
244 (lambda (output)
245 (if (memq output valid-outputs) output #f)) outputs)) "\n"
246 (if (null? inputs)
247 ""
248 (string-append
249 "terminal_input "
250 (symbols->string
251 (map
252 (lambda (input)
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.
256 (if (or unit speed)
257 (string-append
258 "serial"
259 (if unit
260 ;; COM ports 1 through 4
261 (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
262 (string-append " --unit=" (number->string unit))
263 #f)
264 "")
265 (if speed
266 (if (exact-integer? speed)
267 (string-append " --speed=" (number->string speed))
268 #f)
269 ""))
270 ""))))
271 (format #f "~a" io)))
272
1ef8b72a
CM
273(define (grub-root-search device file)
274 "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
6b779207
LC
275a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
276code."
5babe521
LC
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)))
281 ""
1ef8b72a
CM
282 (match device
283 ;; Preferably refer to DEVICE by its UUID or label. This is more
ecc4324f 284 ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
9b336338 285 ((? uuid? uuid)
5babe521 286 (format #f "search --fs-uuid --set ~a"
1ef8b72a 287 (uuid->string device)))
a5acc17a
LC
288 ((? file-system-label? label)
289 (format #f "search --label --set ~a"
290 (file-system-label->string label)))
291 ((or #f (? string?))
5babe521 292 #~(format #f "search --file --set ~a" #$file)))))
6b779207 293
1ef8b72a 294(define* (grub-configuration-file config entries
fe6e3fe2
LC
295 #:key
296 (system (%current-system))
297 (old-entries '()))
d5b429ab 298 "Return the GRUB configuration file corresponding to CONFIG, a
b09a8da4
MO
299<bootloader-configuration> object, and where the store is available at
300STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
301entries corresponding to old generations of the system."
d5b429ab 302 (define all-entries
1975c754
DM
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)))
1ef8b72a 311 ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
8b22107e 312 ;; Use the right file names for KERNEL and INITRD in case
1ef8b72a
CM
313 ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
314 ;; separate partition.
8b22107e
MO
315 (let ((kernel (strip-mount-point device-mount-point kernel))
316 (initrd (strip-mount-point device-mount-point initrd)))
0f65f54e 317 #~(format port "menuentry ~s {
6b779207 318 ~a
44d5f54e 319 linux ~a ~a
d9f0a237 320 initrd ~a
0ded70f3 321}~%"
0f65f54e 322 #$label
8b22107e
MO
323 #$(grub-root-search device kernel)
324 #$kernel (string-join (list #$@arguments))
325 #$initrd))))
46c296dc
LC
326 (define sugar
327 (eye-candy config
328 (menu-entry-device (first all-entries))
329 (menu-entry-device-mount-point (first all-entries))
330 #:system system
331 #:port #~port))
332
333 (define builder
334 #~(call-with-output-file #$output
335 (lambda (port)
336 (format port
59e80445 337 "# This file was generated from your Guix configuration. Any changes
fdf14c64
JD
338# will be lost upon reconfiguration.
339")
46c296dc
LC
340 #$sugar
341 (format port "
f6a7b21d 342set default=~a
6c777cf8 343set timeout=~a~%"
46c296dc
LC
344 #$(bootloader-configuration-default-entry config)
345 #$(bootloader-configuration-timeout config))
346 #$@(map menu-entry->gexp all-entries)
99ae9ceb 347
46c296dc
LC
348 #$@(if (pair? old-entries)
349 #~((format port "
fe6e3fe2 350submenu \"GNU system, old configurations...\" {~%")
46c296dc
LC
351 #$@(map menu-entry->gexp old-entries)
352 (format port "}~%"))
353 #~()))))
0ded70f3 354
46c296dc 355 (computed-file "grub.cfg" builder))
0ded70f3 356
b09a8da4
MO
357\f
358
359;;;
360;;; Install procedures.
361;;;
362
363(define install-grub
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
369 ;; root partition.
370 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
371
46fc9a7e 372 (unless (zero? (system* grub "--no-floppy" "--target=i386-pc"
b09a8da4
MO
373 "--boot-directory" install-dir
374 device))
2941b347
AW
375 (error "failed to install GRUB (BIOS)")))))
376
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"))
aa5a549c 382 (install-dir (string-append mount-point "/boot"))
59e80445 383 ;; When installing Guix, it's common to mount EFI-DIR below
aa5a549c
MB
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)
387 efi-dir)))
2941b347
AW
388 ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
389 ;; root partition.
390 (setenv "GRUB_ENABLE_CRYPTODISK" "y")
391 (unless (zero? (system* grub-install "--boot-directory" install-dir
59e80445 392 "--bootloader-id=Guix"
aa5a549c 393 "--efi-directory" target-esp))
2941b347 394 (error "failed to install GRUB (EFI)")))))
b09a8da4
MO
395
396\f
397
398;;;
399;;; Bootloader definitions.
400;;;
401
402(define grub-bootloader
403 (bootloader
404 (name 'grub)
405 (package grub)
406 (installer install-grub)
407 (configuration-file "/boot/grub/grub.cfg")
408 (configuration-file-generator grub-configuration-file)))
409
410(define* grub-efi-bootloader
411 (bootloader
412 (inherit grub-bootloader)
2941b347 413 (installer install-grub-efi)
b09a8da4
MO
414 (name 'grub-efi)
415 (package grub-efi)))
416
cf189709
DM
417(define* grub-mkrescue-bootloader
418 (bootloader
419 (inherit grub-efi-bootloader)
420 (package grub-hybrid)))
421
b09a8da4
MO
422\f
423;;;
424;;; Compatibility macros.
425;;;
426
427(define-syntax grub-configuration
428 (syntax-rules (grub)
429 ((_ (grub package) fields ...)
430 (if (eq? package grub)
431 (bootloader-configuration
432 (bootloader grub-bootloader)
433 fields ...)
434 (bootloader-configuration
435 (bootloader grub-efi-bootloader)
436 fields ...)))
437 ((_ fields ...)
438 (bootloader-configuration
439 (bootloader grub-bootloader)
440 fields ...))))
441
0ded70f3 442;;; grub.scm ends here