grub.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2019, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  7. ;;; Copyright © 2019, 2020 Miguel Ángel Arruga Vivas <rosen644835@gmail.com>
  8. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  9. ;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
  10. ;;;
  11. ;;; This file is part of GNU Guix.
  12. ;;;
  13. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  14. ;;; under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 3 of the License, or (at
  16. ;;; your option) any later version.
  17. ;;;
  18. ;;; GNU Guix is distributed in the hope that it will be useful, but
  19. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  25. (define-module (gnu bootloader grub)
  26. #:use-module (guix build union)
  27. #:use-module (guix records)
  28. #:use-module (guix store)
  29. #:use-module (guix utils)
  30. #:use-module (guix gexp)
  31. #:use-module (gnu artwork)
  32. #:use-module (gnu bootloader)
  33. #:use-module (gnu system uuid)
  34. #:use-module (gnu system file-systems)
  35. #:use-module (gnu system keyboard)
  36. #:use-module (gnu system locale)
  37. #:use-module (gnu packages bootloaders)
  38. #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
  39. #:autoload (gnu packages xorg) (xkeyboard-config)
  40. #:use-module (ice-9 match)
  41. #:use-module (ice-9 regex)
  42. #:use-module (srfi srfi-1)
  43. #:use-module (srfi srfi-2)
  44. #:export (grub-theme
  45. grub-theme?
  46. grub-theme-image
  47. grub-theme-resolution
  48. grub-theme-color-normal
  49. grub-theme-color-highlight
  50. grub-theme-gfxmode
  51. install-grub-efi-netboot
  52. grub-bootloader
  53. grub-efi-bootloader
  54. grub-efi-netboot-bootloader
  55. grub-mkrescue-bootloader
  56. grub-minimal-bootloader
  57. grub-configuration))
  58. ;;; Commentary:
  59. ;;;
  60. ;;; Configuration of GNU GRUB.
  61. ;;;
  62. ;;; Code:
  63. (define* (normalize-file file mount-point store-directory-prefix)
  64. "Strip MOUNT-POINT and prepend STORE-DIRECTORY-PREFIX, if any, to FILE, a
  65. G-expression or other lowerable object denoting a file name."
  66. (define (strip-mount-point mount-point file)
  67. (if mount-point
  68. (if (string=? mount-point "/")
  69. file
  70. #~(let ((file #$file))
  71. (if (string-prefix? #$mount-point file)
  72. (substring #$file #$(string-length mount-point))
  73. file)))
  74. file))
  75. (define (prepend-store-directory-prefix store-directory-prefix file)
  76. (if store-directory-prefix
  77. #~(string-append #$store-directory-prefix #$file)
  78. file))
  79. (prepend-store-directory-prefix store-directory-prefix
  80. (strip-mount-point mount-point file)))
  81. (define-record-type* <grub-theme>
  82. ;; Default theme contributed by Felipe López.
  83. grub-theme make-grub-theme
  84. grub-theme?
  85. (image grub-theme-image
  86. (default (file-append %artwork-repository
  87. "/grub/GuixSD-fully-black-4-3.svg")))
  88. (resolution grub-theme-resolution
  89. (default '(1024 . 768)))
  90. (color-normal grub-theme-color-normal
  91. (default '((fg . light-gray) (bg . black))))
  92. (color-highlight grub-theme-color-highlight
  93. (default '((fg . yellow) (bg . black))))
  94. (gfxmode grub-theme-gfxmode
  95. (default '("auto")))) ;list of string
  96. ;;;
  97. ;;; Background image & themes.
  98. ;;;
  99. (define (bootloader-theme config)
  100. "Return user defined theme in CONFIG if defined or a default theme
  101. otherwise."
  102. (or (bootloader-configuration-theme config) (grub-theme)))
  103. (define* (image->png image #:key width height)
  104. "Build a PNG of HEIGHT x WIDTH from IMAGE if its file suffix is \".svg\".
  105. Otherwise the picture in IMAGE is just copied."
  106. (computed-file "grub-image.png"
  107. (with-imported-modules '((gnu build svg))
  108. (with-extensions (list guile-rsvg guile-cairo)
  109. #~(if (string-suffix? ".svg" #+image)
  110. (begin
  111. (use-modules (gnu build svg))
  112. (svg->png #+image #$output
  113. #:width #$width
  114. #:height #$height))
  115. (copy-file #+image #$output))))))
  116. (define* (grub-background-image config)
  117. "Return the GRUB background image defined in CONFIG or #f if none was found.
  118. If the suffix of the image file is \".svg\", then it is converted into a PNG
  119. file with the resolution provided in CONFIG."
  120. (let* ((theme (bootloader-theme config))
  121. (image (grub-theme-image theme)))
  122. (and image
  123. (match (grub-theme-resolution theme)
  124. (((? number? width) . (? number? height))
  125. (image->png image #:width width #:height height))
  126. (_ #f)))))
  127. (define (grub-locale-directory grub)
  128. "Generate a directory with the locales from GRUB."
  129. (define builder
  130. #~(begin
  131. (use-modules (ice-9 ftw))
  132. (let ((locale (string-append #$grub "/share/locale"))
  133. (out #$output))
  134. (mkdir out)
  135. (chdir out)
  136. (for-each (lambda (lang)
  137. (let ((file (string-append locale "/" lang
  138. "/LC_MESSAGES/grub.mo"))
  139. (dest (string-append lang ".mo")))
  140. (when (file-exists? file)
  141. (copy-file file dest))))
  142. (scandir locale)))))
  143. (computed-file "grub-locales" builder))
  144. (define* (eye-candy config store-device store-mount-point
  145. #:key store-directory-prefix port)
  146. "Return a gexp that writes to PORT (a port-valued gexp) the 'grub.cfg' part
  147. concerned with graphics mode, background images, colors, and all that.
  148. STORE-DEVICE designates the device holding the store, and STORE-MOUNT-POINT is
  149. its mount point; these are used to determine where the background image and
  150. fonts must be searched for. STORE-DIRECTORY-PREFIX is a directory prefix to
  151. prepend to any store file name."
  152. (define (setup-gfxterm config)
  153. (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
  154. #~(format #f "
  155. if loadfont unicode; then
  156. set gfxmode=~a
  157. insmod all_video
  158. insmod gfxterm
  159. fi~%"
  160. #$(string-join
  161. (grub-theme-gfxmode (bootloader-theme config))
  162. ";"))
  163. ""))
  164. (define (theme-colors type)
  165. (let* ((theme (bootloader-theme config))
  166. (colors (type theme)))
  167. (string-append (symbol->string (assoc-ref colors 'fg)) "/"
  168. (symbol->string (assoc-ref colors 'bg)))))
  169. (define image
  170. (normalize-file (grub-background-image config)
  171. store-mount-point
  172. store-directory-prefix))
  173. (and image
  174. #~(format #$port "
  175. # Set 'root' to the partition that contains /gnu/store.
  176. ~a
  177. ~a
  178. ~a
  179. insmod png
  180. if background_image ~a; then
  181. set color_normal=~a
  182. set color_highlight=~a
  183. else
  184. set menu_color_normal=cyan/blue
  185. set menu_color_highlight=white/blue
  186. fi~%"
  187. #$(grub-root-search store-device image)
  188. #$(setup-gfxterm config)
  189. #$(grub-setup-io config)
  190. #$image
  191. #$(theme-colors grub-theme-color-normal)
  192. #$(theme-colors grub-theme-color-highlight))))
  193. ;;;
  194. ;;; Configuration file.
  195. ;;;
  196. (define* (keyboard-layout-file layout
  197. #:key
  198. (grub grub))
  199. "Process the X keyboard layout description LAYOUT, a <keyboard-layout> record,
  200. and return a file in the format for GRUB keymaps. LAYOUT must be present in
  201. the 'share/X11/xkb/symbols/' directory of 'xkeyboard-config'."
  202. (define builder
  203. (with-imported-modules '((guix build utils))
  204. #~(begin
  205. (use-modules (guix build utils))
  206. ;; 'grub-kbdcomp' passes all its arguments but '-o' to 'ckbcomp'
  207. ;; (from the 'console-setup' package).
  208. (invoke #+(file-append grub "/bin/grub-mklayout")
  209. "-i" #+(keyboard-layout->console-keymap layout)
  210. "-o" #$output))))
  211. (computed-file (string-append "grub-keymap."
  212. (string-map (match-lambda
  213. (#\, #\-)
  214. (chr chr))
  215. (keyboard-layout-name layout)))
  216. builder))
  217. (define (grub-setup-io config)
  218. "Return GRUB commands to configure the input / output interfaces. The result
  219. is a string that can be inserted in grub.cfg."
  220. (let* ((symbols->string (lambda (list)
  221. (string-join (map symbol->string list) " ")))
  222. (outputs (bootloader-configuration-terminal-outputs config))
  223. (inputs (bootloader-configuration-terminal-inputs config))
  224. (unit (bootloader-configuration-serial-unit config))
  225. (speed (bootloader-configuration-serial-speed config))
  226. ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
  227. ;; as documented in GRUB manual section "Simple Configuration
  228. ;; Handling".
  229. (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
  230. gfxterm vga_text mda_text morse spkmodem))
  231. (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
  232. at_keyboard usb_keyboard))
  233. (io (string-append
  234. ;; UNIT and SPEED are arguments to the same GRUB command
  235. ;; ("serial"), so we process them together.
  236. (if (or unit speed)
  237. (string-append
  238. "serial"
  239. (if unit
  240. ;; COM ports 1 through 4
  241. (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
  242. (string-append " --unit=" (number->string unit))
  243. #f)
  244. "")
  245. (if speed
  246. (if (exact-integer? speed)
  247. (string-append " --speed=" (number->string speed))
  248. #f)
  249. "")
  250. "\n")
  251. "")
  252. (if (null? inputs)
  253. ""
  254. (string-append
  255. "terminal_input "
  256. (symbols->string
  257. (map
  258. (lambda (input)
  259. (if (memq input valid-inputs) input #f)) inputs))
  260. "\n"))
  261. "terminal_output "
  262. (symbols->string
  263. (map
  264. (lambda (output)
  265. (if (memq output valid-outputs) output #f)) outputs)))))
  266. (format #f "~a" io)))
  267. (define (grub-root-search device file)
  268. "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
  269. a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
  270. code."
  271. ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
  272. ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
  273. ;; custom menu entries. In the latter case, don't emit a 'search' command.
  274. (if (and (string? file) (not (string-prefix? "/" file)))
  275. ""
  276. (match device
  277. ;; Preferably refer to DEVICE by its UUID or label. This is more
  278. ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
  279. ((? uuid? uuid)
  280. (format #f "search --fs-uuid --set ~a"
  281. (uuid->string device)))
  282. ((? file-system-label? label)
  283. (format #f "search --label --set ~a"
  284. (file-system-label->string label)))
  285. ((? (lambda (device)
  286. (and (string? device) (string-contains device ":/"))) nfs-uri)
  287. ;; If the device is an NFS share, then we assume that the expected
  288. ;; file on that device (e.g. the GRUB background image or the kernel)
  289. ;; has to be loaded over the network. Otherwise we would need an
  290. ;; additional device information for some local disk to look for that
  291. ;; file, which we do not have.
  292. ;;
  293. ;; We explicitly set "root=(tftp)" here even though if grub.cfg
  294. ;; had been loaded via TFTP, Grub would have set "root=(tftp)"
  295. ;; automatically anyway. The reason is if you have a system that
  296. ;; used to be on NFS but now is local, root would be set to local
  297. ;; disk. If you then selected an older system generation that is
  298. ;; supposed to boot from network in the Grub boot menu, Grub still
  299. ;; wouldn't load those files from network otherwise.
  300. ;;
  301. ;; TFTP is preferred to HTTP because it is used more widely and
  302. ;; specified in standards more widely--especially BOOTP/DHCPv4
  303. ;; defines a TFTP server for DHCP option 66, but not HTTP.
  304. ;;
  305. ;; Note: DHCPv6 specifies option 59 to contain a boot-file-url,
  306. ;; which can contain a HTTP or TFTP URL.
  307. ;;
  308. ;; Note: It is assumed that the file paths are of a similar
  309. ;; setup on both the TFTP server and the NFS server (it is
  310. ;; not possible to search for files on TFTP).
  311. ;;
  312. ;; TODO: Allow HTTP.
  313. "set root=(tftp)")
  314. ((or #f (? string?))
  315. #~(format #f "search --file --set ~a" #$file)))))
  316. (define* (grub-configuration-file config entries
  317. #:key
  318. (locale #f)
  319. (system (%current-system))
  320. (old-entries '())
  321. (store-crypto-devices '())
  322. store-directory-prefix)
  323. "Return the GRUB configuration file corresponding to CONFIG, a
  324. <bootloader-configuration> object, and where the store is available at
  325. STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
  326. entries corresponding to old generations of the system.
  327. STORE-CRYPTO-DEVICES contain the UUIDs of the encrypted units that must
  328. be unlocked to access the store contents.
  329. STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required
  330. when booting a root file system on a Btrfs subvolume."
  331. (define all-entries
  332. (append entries (bootloader-configuration-menu-entries config)))
  333. (define (menu-entry->gexp entry)
  334. (let ((label (menu-entry-label entry))
  335. (linux (menu-entry-linux entry))
  336. (device (menu-entry-device entry))
  337. (device-mount-point (menu-entry-device-mount-point entry)))
  338. (if linux
  339. (let ((arguments (menu-entry-linux-arguments entry))
  340. (linux (normalize-file linux
  341. device-mount-point
  342. store-directory-prefix))
  343. (initrd (normalize-file (menu-entry-initrd entry)
  344. device-mount-point
  345. store-directory-prefix)))
  346. ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
  347. ;; Use the right file names for LINUX and INITRD in case
  348. ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
  349. ;; separate partition.
  350. ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the linux and
  351. ;; initrd paths, to allow booting from a Btrfs subvolume.
  352. #~(format port "menuentry ~s {
  353. ~a
  354. linux ~a ~a
  355. initrd ~a
  356. }~%"
  357. #$label
  358. #$(grub-root-search device linux)
  359. #$linux (string-join (list #$@arguments))
  360. #$initrd))
  361. (let ((kernel (menu-entry-multiboot-kernel entry))
  362. (arguments (menu-entry-multiboot-arguments entry))
  363. (modules (menu-entry-multiboot-modules entry))
  364. (root-index 1)) ; XXX EFI will need root-index 2
  365. #~(format port "
  366. menuentry ~s {
  367. multiboot ~a root=device:hd0s~a~a~a
  368. }~%"
  369. #$label
  370. #$kernel
  371. #$root-index (string-join (list #$@arguments) " " 'prefix)
  372. (string-join (map string-join '#$modules)
  373. "\n module " 'prefix))))))
  374. (define (crypto-devices)
  375. (define (crypto-device->cryptomount dev)
  376. (if (uuid? dev)
  377. #~(format port "cryptomount -u ~a~%"
  378. ;; cryptomount only accepts UUID without the hypen.
  379. #$(string-delete #\- (uuid->string dev)))
  380. ;; Other type of devices aren't implemented.
  381. #~()))
  382. (let ((devices (map crypto-device->cryptomount store-crypto-devices))
  383. (modules #~(format port "insmod luks~%insmod luks2~%")))
  384. (if (null? devices)
  385. devices
  386. (cons modules devices))))
  387. (define (sugar)
  388. (let* ((entry (first all-entries))
  389. (device (menu-entry-device entry))
  390. (mount-point (menu-entry-device-mount-point entry)))
  391. (eye-candy config
  392. device
  393. mount-point
  394. #:store-directory-prefix store-directory-prefix
  395. #:port #~port)))
  396. (define locale-config
  397. (let* ((entry (first all-entries))
  398. (device (menu-entry-device entry))
  399. (mount-point (menu-entry-device-mount-point entry))
  400. (bootloader (bootloader-configuration-bootloader config))
  401. (grub (bootloader-package bootloader)))
  402. #~(let ((locale #$(and locale
  403. (locale-definition-source
  404. (locale-name->definition locale))))
  405. (locales #$(and locale
  406. (normalize-file (grub-locale-directory grub)
  407. mount-point
  408. store-directory-prefix))))
  409. (when locale
  410. (format port "\
  411. # Localization configuration.
  412. ~asearch --file --set ~a/en@quot.mo
  413. set locale_dir=~a
  414. set lang=~a~%"
  415. ;; Skip the search if there is an image, as it has already
  416. ;; been performed by eye-candy and traversing the store is
  417. ;; an expensive operation.
  418. #$(if (grub-theme-image (bootloader-theme config))
  419. "# "
  420. "")
  421. locales
  422. locales
  423. locale)))))
  424. (define keyboard-layout-config
  425. (let* ((layout (bootloader-configuration-keyboard-layout config))
  426. (grub (bootloader-package
  427. (bootloader-configuration-bootloader config)))
  428. (keymap* (and layout
  429. (keyboard-layout-file layout #:grub grub)))
  430. (entry (first all-entries))
  431. (device (menu-entry-device entry))
  432. (mount-point (menu-entry-device-mount-point entry))
  433. (keymap (and keymap*
  434. (normalize-file keymap* mount-point
  435. store-directory-prefix))))
  436. #~(when #$keymap
  437. (format port "\
  438. insmod keylayouts
  439. keymap ~a~%" #$keymap))))
  440. (define builder
  441. #~(call-with-output-file #$output
  442. (lambda (port)
  443. (format port
  444. "# This file was generated from your Guix configuration. Any changes
  445. # will be lost upon reconfiguration.
  446. ")
  447. #$@(crypto-devices)
  448. #$(sugar)
  449. #$locale-config
  450. #$keyboard-layout-config
  451. (format port "
  452. set default=~a
  453. set timeout=~a~%"
  454. #$(bootloader-configuration-default-entry config)
  455. #$(bootloader-configuration-timeout config))
  456. #$@(map menu-entry->gexp all-entries)
  457. #$@(if (pair? old-entries)
  458. #~((format port "
  459. submenu \"GNU system, old configurations...\" {~%")
  460. #$@(map menu-entry->gexp old-entries)
  461. (format port "}~%"))
  462. #~())
  463. (format port "
  464. if [ \"${grub_platform}\" == efi ]; then
  465. menuentry \"Firmware setup\" {
  466. fwsetup
  467. }
  468. fi~%"))))
  469. ;; Since this file is rather unique, there's no point in trying to
  470. ;; substitute it.
  471. (computed-file "grub.cfg" builder
  472. #:options '(#:local-build? #t
  473. #:substitutable? #f)))
  474. ;;;
  475. ;;; Install procedures.
  476. ;;;
  477. (define install-grub
  478. #~(lambda (bootloader device mount-point)
  479. (let ((grub (string-append bootloader "/sbin/grub-install"))
  480. (install-dir (string-append mount-point "/boot")))
  481. ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE
  482. ;; is #f, then we populate the disk-image rooted at MOUNT-POINT.
  483. (if device
  484. (begin
  485. ;; Tell 'grub-install' that there might be a LUKS-encrypted
  486. ;; /boot or root partition.
  487. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  488. ;; Hide potentially confusing messages from the user, such as
  489. ;; "Installing for i386-pc platform."
  490. (invoke/quiet grub "--no-floppy" "--target=i386-pc"
  491. "--boot-directory" install-dir
  492. device))
  493. ;; When creating a disk-image, only install a font and GRUB modules.
  494. (let* ((fonts (string-append install-dir "/grub/fonts")))
  495. (mkdir-p fonts)
  496. (copy-file (string-append bootloader "/share/grub/unicode.pf2")
  497. (string-append fonts "/unicode.pf2"))
  498. (copy-recursively (string-append bootloader "/lib/")
  499. install-dir))))))
  500. (define install-grub-disk-image
  501. #~(lambda (bootloader root-index image)
  502. ;; Install GRUB on the given IMAGE. The root partition index is
  503. ;; ROOT-INDEX.
  504. (let ((grub-mkimage
  505. (string-append bootloader "/bin/grub-mkimage"))
  506. (modules '("biosdisk" "part_msdos" "fat" "ext2"))
  507. (grub-bios-setup
  508. (string-append bootloader "/sbin/grub-bios-setup"))
  509. (root-device (format #f "hd0,msdos~a" root-index))
  510. (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img"))
  511. (device-map "device.map"))
  512. ;; Create a minimal, standalone GRUB image that will be written
  513. ;; directly in the MBR-GAP (space between the end of the MBR and the
  514. ;; first partition).
  515. (apply invoke grub-mkimage
  516. "-O" "i386-pc"
  517. "-o" "core.img"
  518. "-p" (format #f "(~a)/boot/grub" root-device)
  519. modules)
  520. ;; Create a device mapping file.
  521. (call-with-output-file device-map
  522. (lambda (port)
  523. (format port "(hd0) ~a~%" image)))
  524. ;; Copy the default boot.img, that will be written on the MBR sector
  525. ;; by GRUB-BIOS-SETUP.
  526. (copy-file boot-img "boot.img")
  527. ;; Install both the "boot.img" and the "core.img" files on the given
  528. ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB
  529. ;; written in the MBR-GAP. GRUB configuration and missing modules will
  530. ;; be read from ROOT-DEVICE.
  531. (invoke grub-bios-setup
  532. "-m" device-map
  533. "-r" root-device
  534. "-d" "."
  535. image))))
  536. (define install-grub-efi
  537. #~(lambda (bootloader efi-dir mount-point)
  538. ;; There is nothing useful to do when called in the context of a disk
  539. ;; image generation.
  540. (when efi-dir
  541. ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
  542. ;; system whose root is mounted at MOUNT-POINT.
  543. (let ((grub-install (string-append bootloader "/sbin/grub-install"))
  544. (install-dir (string-append mount-point "/boot"))
  545. ;; When installing Guix, it's common to mount EFI-DIR below
  546. ;; MOUNT-POINT rather than /boot/efi on the live image.
  547. (target-esp (if (file-exists? (string-append mount-point efi-dir))
  548. (string-append mount-point efi-dir)
  549. efi-dir)))
  550. ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
  551. ;; root partition.
  552. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  553. (invoke/quiet grub-install "--boot-directory" install-dir
  554. "--bootloader-id=Guix"
  555. "--efi-directory" target-esp)))))
  556. (define (install-grub-efi-netboot subdir)
  557. "Define a grub-efi-netboot bootloader installer for installation in SUBDIR,
  558. which is usually efi/Guix or efi/boot."
  559. (let* ((system (string-split (nix-system->gnu-triplet
  560. (or (%current-target-system)
  561. (%current-system)))
  562. #\-))
  563. (arch (first system))
  564. (boot-efi-link (match system
  565. ;; These are the supportend systems and the names
  566. ;; defined by the UEFI standard for removable media.
  567. (("i686" _ ...) "/bootia32.efi")
  568. (("x86_64" _ ...) "/bootx64.efi")
  569. (("arm" _ ...) "/bootarm.efi")
  570. (("aarch64" _ ...) "/bootaa64.efi")
  571. (("riscv" _ ...) "/bootriscv32.efi")
  572. (("riscv64" _ ...) "/bootriscv64.efi")
  573. ;; Other systems are not supported, although defined.
  574. ;; (("riscv128" _ ...) "/bootriscv128.efi")
  575. ;; (("ia64" _ ...) "/bootia64.efi")
  576. ((_ ...) #f)))
  577. (core-efi (string-append
  578. ;; This is the arch dependent file name of GRUB, e.g.
  579. ;; i368-efi/core.efi or arm64-efi/core.efi.
  580. (match arch
  581. ("i686" "i386")
  582. ("aarch64" "arm64")
  583. ("riscv" "riscv32")
  584. (_ arch))
  585. "-efi/core.efi")))
  586. (with-imported-modules
  587. '((guix build union))
  588. #~(lambda (bootloader target mount-point)
  589. "Install the BOOTLOADER, which must be the package grub, as e.g.
  590. bootx64.efi or bootaa64.efi into SUBDIR, which is usually efi/Guix or efi/boot,
  591. below the directory TARGET for the system whose root is mounted at MOUNT-POINT.
  592. MOUNT-POINT is the last argument in 'guix system init /etc/config.scm mnt/point'
  593. or '/' for other 'guix system' commands.
  594. Where TARGET comes from the targets argument given to the
  595. bootloader-configuration in:
  596. (operating-system
  597. (bootloader (bootloader-configuration
  598. (targets '(\"/boot\"))
  599. …))
  600. …)
  601. TARGET is required to be an absolute directory name, usually mounted via NFS,
  602. and finally needs to be provided by a TFTP server as the TFTP root directory.
  603. GRUB will load tftp://server/SUBDIR/grub.cfg and this file will instruct it to
  604. load more files from the store like tftp://server/gnu/store/…-linux…/Image.
  605. To make this possible two symlinks will be created. The first symlink points
  606. relatively form MOUNT-POINT/TARGET/SUBDIR/grub.cfg to
  607. MOUNT-POINT/boot/grub/grub.cfg, and the second symlink points relatively from
  608. MOUNT-POINT/TARGET/%store-prefix to MOUNT-POINT/%store-prefix.
  609. It is important to note that these symlinks need to be relative, as the absolute
  610. paths on the TFTP server side are unknown.
  611. It is also important to note that both symlinks will point outside the TFTP root
  612. directory and that the TARGET/%store-prefix symlink makes the whole store
  613. accessible via TFTP. Possibly the TFTP server must be configured
  614. to allow accesses outside its TFTP root directory. This may need to be
  615. considered for security aspects."
  616. (use-modules ((guix build union) #:select (symlink-relative)))
  617. (let* ((net-dir (string-append mount-point target "/"))
  618. (sub-dir (string-append net-dir #$subdir "/"))
  619. (store (string-append mount-point (%store-prefix)))
  620. (store-link (string-append net-dir (%store-prefix)))
  621. (grub-cfg (string-append mount-point "/boot/grub/grub.cfg"))
  622. (grub-cfg-link (string-append sub-dir (basename grub-cfg)))
  623. (boot-efi-link (string-append sub-dir #$boot-efi-link)))
  624. ;; Prepare the symlink to the store.
  625. (mkdir-p (dirname store-link))
  626. (false-if-exception (delete-file store-link))
  627. (symlink-relative store store-link)
  628. ;; Prepare the symlink to the grub.cfg, which points into the store.
  629. (mkdir-p (dirname grub-cfg-link))
  630. (false-if-exception (delete-file grub-cfg-link))
  631. (symlink-relative grub-cfg grub-cfg-link)
  632. ;; Install GRUB, which refers to the grub.cfg, with support for
  633. ;; encrypted partitions,
  634. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  635. (invoke/quiet (string-append bootloader "/bin/grub-mknetdir")
  636. (string-append "--net-directory=" net-dir)
  637. (string-append "--subdir=" #$subdir))
  638. ;; Prepare the bootloader symlink, which points to core.efi of GRUB.
  639. (false-if-exception (delete-file boot-efi-link))
  640. (symlink #$core-efi boot-efi-link))))))
  641. ;;;
  642. ;;; Bootloader definitions.
  643. ;;;
  644. ;;; For all these grub-bootloader variables the path to /boot/grub/grub.cfg
  645. ;;; is fixed. Inheriting and overwriting the field 'configuration-file' will
  646. ;;; break 'guix system delete-generations', 'guix system switch-generation',
  647. ;;; and 'guix system roll-back'.
  648. (define grub-bootloader
  649. (bootloader
  650. (name 'grub)
  651. (package grub)
  652. (installer install-grub)
  653. (disk-image-installer install-grub-disk-image)
  654. (configuration-file "/boot/grub/grub.cfg")
  655. (configuration-file-generator grub-configuration-file)))
  656. (define grub-minimal-bootloader
  657. (bootloader
  658. (inherit grub-bootloader)
  659. (package grub-minimal)))
  660. (define grub-efi-bootloader
  661. (bootloader
  662. (inherit grub-bootloader)
  663. (installer install-grub-efi)
  664. (disk-image-installer #f)
  665. (name 'grub-efi)
  666. (package grub-efi)))
  667. (define grub-efi-netboot-bootloader
  668. (bootloader
  669. (inherit grub-efi-bootloader)
  670. (name 'grub-efi-netboot-bootloader)
  671. (installer (install-grub-efi-netboot "efi/Guix"))))
  672. (define grub-mkrescue-bootloader
  673. (bootloader
  674. (inherit grub-efi-bootloader)
  675. (package grub-hybrid)))
  676. ;;;
  677. ;;; Compatibility macros.
  678. ;;;
  679. (define-syntax grub-configuration
  680. (syntax-rules (grub)
  681. ((_ (grub package) fields ...)
  682. (if (eq? package grub)
  683. (bootloader-configuration
  684. (bootloader grub-bootloader)
  685. fields ...)
  686. (bootloader-configuration
  687. (bootloader grub-efi-bootloader)
  688. fields ...)))
  689. ((_ fields ...)
  690. (bootloader-configuration
  691. (bootloader grub-bootloader)
  692. fields ...))))
  693. ;;; grub.scm ends here