grub.scm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. "terminal_output "
  235. (symbols->string
  236. (map
  237. (lambda (output)
  238. (if (memq output valid-outputs) output #f)) outputs)) "\n"
  239. (if (null? inputs)
  240. ""
  241. (string-append
  242. "terminal_input "
  243. (symbols->string
  244. (map
  245. (lambda (input)
  246. (if (memq input valid-inputs) input #f)) inputs)) "\n"))
  247. ;; UNIT and SPEED are arguments to the same GRUB command
  248. ;; ("serial"), so we process them together.
  249. (if (or unit speed)
  250. (string-append
  251. "serial"
  252. (if unit
  253. ;; COM ports 1 through 4
  254. (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
  255. (string-append " --unit=" (number->string unit))
  256. #f)
  257. "")
  258. (if speed
  259. (if (exact-integer? speed)
  260. (string-append " --speed=" (number->string speed))
  261. #f)
  262. ""))
  263. ""))))
  264. (format #f "~a" io)))
  265. (define (grub-root-search device file)
  266. "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
  267. a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
  268. code."
  269. ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
  270. ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
  271. ;; custom menu entries. In the latter case, don't emit a 'search' command.
  272. (if (and (string? file) (not (string-prefix? "/" file)))
  273. ""
  274. (match device
  275. ;; Preferably refer to DEVICE by its UUID or label. This is more
  276. ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
  277. ((? uuid? uuid)
  278. (format #f "search --fs-uuid --set ~a"
  279. (uuid->string device)))
  280. ((? file-system-label? label)
  281. (format #f "search --label --set ~a"
  282. (file-system-label->string label)))
  283. ((? (lambda (device)
  284. (and (string? device) (string-contains device ":/"))) nfs-uri)
  285. ;; If the device is an NFS share, then we assume that the expected
  286. ;; file on that device (e.g. the GRUB background image or the kernel)
  287. ;; has to be loaded over the network. Otherwise we would need an
  288. ;; additional device information for some local disk to look for that
  289. ;; file, which we do not have.
  290. ;;
  291. ;; We explicitly set "root=(tftp)" here even though if grub.cfg
  292. ;; had been loaded via TFTP, Grub would have set "root=(tftp)"
  293. ;; automatically anyway. The reason is if you have a system that
  294. ;; used to be on NFS but now is local, root would be set to local
  295. ;; disk. If you then selected an older system generation that is
  296. ;; supposed to boot from network in the Grub boot menu, Grub still
  297. ;; wouldn't load those files from network otherwise.
  298. ;;
  299. ;; TFTP is preferred to HTTP because it is used more widely and
  300. ;; specified in standards more widely--especially BOOTP/DHCPv4
  301. ;; defines a TFTP server for DHCP option 66, but not HTTP.
  302. ;;
  303. ;; Note: DHCPv6 specifies option 59 to contain a boot-file-url,
  304. ;; which can contain a HTTP or TFTP URL.
  305. ;;
  306. ;; Note: It is assumed that the file paths are of a similar
  307. ;; setup on both the TFTP server and the NFS server (it is
  308. ;; not possible to search for files on TFTP).
  309. ;;
  310. ;; TODO: Allow HTTP.
  311. "set root=(tftp)")
  312. ((or #f (? string?))
  313. #~(format #f "search --file --set ~a" #$file)))))
  314. (define* (grub-configuration-file config entries
  315. #:key
  316. (locale #f)
  317. (system (%current-system))
  318. (old-entries '())
  319. (store-crypto-devices '())
  320. store-directory-prefix)
  321. "Return the GRUB configuration file corresponding to CONFIG, a
  322. <bootloader-configuration> object, and where the store is available at
  323. STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
  324. entries corresponding to old generations of the system.
  325. STORE-CRYPTO-DEVICES contain the UUIDs of the encrypted units that must
  326. be unlocked to access the store contents.
  327. STORE-DIRECTORY-PREFIX may be used to specify a store prefix, as is required
  328. when booting a root file system on a Btrfs subvolume."
  329. (define all-entries
  330. (append entries (bootloader-configuration-menu-entries config)))
  331. (define (menu-entry->gexp entry)
  332. (let ((label (menu-entry-label entry))
  333. (linux (menu-entry-linux entry))
  334. (device (menu-entry-device entry))
  335. (device-mount-point (menu-entry-device-mount-point entry)))
  336. (if linux
  337. (let ((arguments (menu-entry-linux-arguments entry))
  338. (linux (normalize-file linux
  339. device-mount-point
  340. store-directory-prefix))
  341. (initrd (normalize-file (menu-entry-initrd entry)
  342. device-mount-point
  343. store-directory-prefix)))
  344. ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
  345. ;; Use the right file names for LINUX and INITRD in case
  346. ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
  347. ;; separate partition.
  348. ;; When BTRFS-SUBVOLUME-FILE-NAME is defined, prepend it the linux and
  349. ;; initrd paths, to allow booting from a Btrfs subvolume.
  350. #~(format port "menuentry ~s {
  351. ~a
  352. linux ~a ~a
  353. initrd ~a
  354. }~%"
  355. #$label
  356. #$(grub-root-search device linux)
  357. #$linux (string-join (list #$@arguments))
  358. #$initrd))
  359. (let ((kernel (menu-entry-multiboot-kernel entry))
  360. (arguments (menu-entry-multiboot-arguments entry))
  361. (modules (menu-entry-multiboot-modules entry))
  362. (root-index 1)) ; XXX EFI will need root-index 2
  363. #~(format port "
  364. menuentry ~s {
  365. multiboot ~a root=device:hd0s~a~a~a
  366. }~%"
  367. #$label
  368. #$kernel
  369. #$root-index (string-join (list #$@arguments) " " 'prefix)
  370. (string-join (map string-join '#$modules)
  371. "\n module " 'prefix))))))
  372. (define (crypto-devices)
  373. (define (crypto-device->cryptomount dev)
  374. (if (uuid? dev)
  375. #~(format port "cryptomount -u ~a~%"
  376. ;; cryptomount only accepts UUID without the hypen.
  377. #$(string-delete #\- (uuid->string dev)))
  378. ;; Other type of devices aren't implemented.
  379. #~()))
  380. (let ((devices (map crypto-device->cryptomount store-crypto-devices))
  381. ;; XXX: Add luks2 when grub 2.06 is packaged.
  382. (modules #~(format port "insmod luks~%")))
  383. (if (null? devices)
  384. devices
  385. (cons modules devices))))
  386. (define (sugar)
  387. (let* ((entry (first all-entries))
  388. (device (menu-entry-device entry))
  389. (mount-point (menu-entry-device-mount-point entry)))
  390. (eye-candy config
  391. device
  392. mount-point
  393. #:store-directory-prefix store-directory-prefix
  394. #:port #~port)))
  395. (define locale-config
  396. (let* ((entry (first all-entries))
  397. (device (menu-entry-device entry))
  398. (mount-point (menu-entry-device-mount-point entry))
  399. (bootloader (bootloader-configuration-bootloader config))
  400. (grub (bootloader-package bootloader)))
  401. #~(let ((locale #$(and locale
  402. (locale-definition-source
  403. (locale-name->definition locale))))
  404. (locales #$(and locale
  405. (normalize-file (grub-locale-directory grub)
  406. mount-point
  407. store-directory-prefix))))
  408. (when locale
  409. (format port "\
  410. # Localization configuration.
  411. ~asearch --file --set ~a/en@quot.mo
  412. set locale_dir=~a
  413. set lang=~a~%"
  414. ;; Skip the search if there is an image, as it has already
  415. ;; been performed by eye-candy and traversing the store is
  416. ;; an expensive operation.
  417. #$(if (grub-theme-image (bootloader-theme config))
  418. "# "
  419. "")
  420. locales
  421. locales
  422. locale)))))
  423. (define keyboard-layout-config
  424. (let* ((layout (bootloader-configuration-keyboard-layout config))
  425. (grub (bootloader-package
  426. (bootloader-configuration-bootloader config)))
  427. (keymap* (and layout
  428. (keyboard-layout-file layout #:grub grub)))
  429. (entry (first all-entries))
  430. (device (menu-entry-device entry))
  431. (mount-point (menu-entry-device-mount-point entry))
  432. (keymap (and keymap*
  433. (normalize-file keymap* mount-point
  434. store-directory-prefix))))
  435. #~(when #$keymap
  436. (format port "\
  437. insmod keylayouts
  438. keymap ~a~%" #$keymap))))
  439. (define builder
  440. #~(call-with-output-file #$output
  441. (lambda (port)
  442. (format port
  443. "# This file was generated from your Guix configuration. Any changes
  444. # will be lost upon reconfiguration.
  445. ")
  446. #$@(crypto-devices)
  447. #$(sugar)
  448. #$locale-config
  449. #$keyboard-layout-config
  450. (format port "
  451. set default=~a
  452. set timeout=~a~%"
  453. #$(bootloader-configuration-default-entry config)
  454. #$(bootloader-configuration-timeout config))
  455. #$@(map menu-entry->gexp all-entries)
  456. #$@(if (pair? old-entries)
  457. #~((format port "
  458. submenu \"GNU system, old configurations...\" {~%")
  459. #$@(map menu-entry->gexp old-entries)
  460. (format port "}~%"))
  461. #~())
  462. (format port "
  463. if [ \"${grub_platform}\" == efi ]; then
  464. menuentry \"Firmware setup\" {
  465. fwsetup
  466. }
  467. fi~%"))))
  468. ;; Since this file is rather unique, there's no point in trying to
  469. ;; substitute it.
  470. (computed-file "grub.cfg" builder
  471. #:options '(#:local-build? #t
  472. #:substitutable? #f)))
  473. ;;;
  474. ;;; Install procedures.
  475. ;;;
  476. (define install-grub
  477. #~(lambda (bootloader device mount-point)
  478. (let ((grub (string-append bootloader "/sbin/grub-install"))
  479. (install-dir (string-append mount-point "/boot")))
  480. ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT. If DEVICE
  481. ;; is #f, then we populate the disk-image rooted at MOUNT-POINT.
  482. (if device
  483. (begin
  484. ;; Tell 'grub-install' that there might be a LUKS-encrypted
  485. ;; /boot or root partition.
  486. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  487. ;; Hide potentially confusing messages from the user, such as
  488. ;; "Installing for i386-pc platform."
  489. (invoke/quiet grub "--no-floppy" "--target=i386-pc"
  490. "--boot-directory" install-dir
  491. device))
  492. ;; When creating a disk-image, only install a font and GRUB modules.
  493. (let* ((fonts (string-append install-dir "/grub/fonts")))
  494. (mkdir-p fonts)
  495. (copy-file (string-append bootloader "/share/grub/unicode.pf2")
  496. (string-append fonts "/unicode.pf2"))
  497. (copy-recursively (string-append bootloader "/lib/")
  498. install-dir))))))
  499. (define install-grub-disk-image
  500. #~(lambda (bootloader root-index image)
  501. ;; Install GRUB on the given IMAGE. The root partition index is
  502. ;; ROOT-INDEX.
  503. (let ((grub-mkimage
  504. (string-append bootloader "/bin/grub-mkimage"))
  505. (modules '("biosdisk" "part_msdos" "fat" "ext2"))
  506. (grub-bios-setup
  507. (string-append bootloader "/sbin/grub-bios-setup"))
  508. (root-device (format #f "hd0,msdos~a" root-index))
  509. (boot-img (string-append bootloader "/lib/grub/i386-pc/boot.img"))
  510. (device-map "device.map"))
  511. ;; Create a minimal, standalone GRUB image that will be written
  512. ;; directly in the MBR-GAP (space between the end of the MBR and the
  513. ;; first partition).
  514. (apply invoke grub-mkimage
  515. "-O" "i386-pc"
  516. "-o" "core.img"
  517. "-p" (format #f "(~a)/boot/grub" root-device)
  518. modules)
  519. ;; Create a device mapping file.
  520. (call-with-output-file device-map
  521. (lambda (port)
  522. (format port "(hd0) ~a~%" image)))
  523. ;; Copy the default boot.img, that will be written on the MBR sector
  524. ;; by GRUB-BIOS-SETUP.
  525. (copy-file boot-img "boot.img")
  526. ;; Install both the "boot.img" and the "core.img" files on the given
  527. ;; IMAGE. On boot, the MBR sector will execute the minimal GRUB
  528. ;; written in the MBR-GAP. GRUB configuration and missing modules will
  529. ;; be read from ROOT-DEVICE.
  530. (invoke grub-bios-setup
  531. "-m" device-map
  532. "-r" root-device
  533. "-d" "."
  534. image))))
  535. (define install-grub-efi
  536. #~(lambda (bootloader efi-dir mount-point)
  537. ;; There is nothing useful to do when called in the context of a disk
  538. ;; image generation.
  539. (when efi-dir
  540. ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
  541. ;; system whose root is mounted at MOUNT-POINT.
  542. (let ((grub-install (string-append bootloader "/sbin/grub-install"))
  543. (install-dir (string-append mount-point "/boot"))
  544. ;; When installing Guix, it's common to mount EFI-DIR below
  545. ;; MOUNT-POINT rather than /boot/efi on the live image.
  546. (target-esp (if (file-exists? (string-append mount-point efi-dir))
  547. (string-append mount-point efi-dir)
  548. efi-dir)))
  549. ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
  550. ;; root partition.
  551. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  552. (invoke/quiet grub-install "--boot-directory" install-dir
  553. "--bootloader-id=Guix"
  554. "--efi-directory" target-esp)))))
  555. (define (install-grub-efi-netboot subdir)
  556. "Define a grub-efi-netboot bootloader installer for installation in SUBDIR,
  557. which is usually efi/Guix or efi/boot."
  558. (let* ((system (string-split (nix-system->gnu-triplet
  559. (or (%current-target-system)
  560. (%current-system)))
  561. #\-))
  562. (arch (first system))
  563. (boot-efi-link (match system
  564. ;; These are the supportend systems and the names
  565. ;; defined by the UEFI standard for removable media.
  566. (("i686" _ ...) "/bootia32.efi")
  567. (("x86_64" _ ...) "/bootx64.efi")
  568. (("arm" _ ...) "/bootarm.efi")
  569. (("aarch64" _ ...) "/bootaa64.efi")
  570. (("riscv" _ ...) "/bootriscv32.efi")
  571. (("riscv64" _ ...) "/bootriscv64.efi")
  572. ;; Other systems are not supported, although defined.
  573. ;; (("riscv128" _ ...) "/bootriscv128.efi")
  574. ;; (("ia64" _ ...) "/bootia64.efi")
  575. ((_ ...) #f)))
  576. (core-efi (string-append
  577. ;; This is the arch dependent file name of GRUB, e.g.
  578. ;; i368-efi/core.efi or arm64-efi/core.efi.
  579. (match arch
  580. ("i686" "i386")
  581. ("aarch64" "arm64")
  582. ("riscv" "riscv32")
  583. (_ arch))
  584. "-efi/core.efi")))
  585. (with-imported-modules
  586. '((guix build union))
  587. #~(lambda (bootloader target mount-point)
  588. "Install the BOOTLOADER, which must be the package grub, as e.g.
  589. bootx64.efi or bootaa64.efi into SUBDIR, which is usually efi/Guix or efi/boot,
  590. below the directory TARGET for the system whose root is mounted at MOUNT-POINT.
  591. MOUNT-POINT is the last argument in 'guix system init /etc/config.scm mnt/point'
  592. or '/' for other 'guix system' commands.
  593. Where TARGET comes from the targets argument given to the
  594. bootloader-configuration in:
  595. (operating-system
  596. (bootloader (bootloader-configuration
  597. (targets '(\"/boot\"))
  598. …))
  599. …)
  600. TARGET is required to be an absolute directory name, usually mounted via NFS,
  601. and finally needs to be provided by a TFTP server as the TFTP root directory.
  602. GRUB will load tftp://server/SUBDIR/grub.cfg and this file will instruct it to
  603. load more files from the store like tftp://server/gnu/store/…-linux…/Image.
  604. To make this possible two symlinks will be created. The first symlink points
  605. relatively form MOUNT-POINT/TARGET/SUBDIR/grub.cfg to
  606. MOUNT-POINT/boot/grub/grub.cfg, and the second symlink points relatively from
  607. MOUNT-POINT/TARGET/%store-prefix to MOUNT-POINT/%store-prefix.
  608. It is important to note that these symlinks need to be relative, as the absolute
  609. paths on the TFTP server side are unknown.
  610. It is also important to note that both symlinks will point outside the TFTP root
  611. directory and that the TARGET/%store-prefix symlink makes the whole store
  612. accessible via TFTP. Possibly the TFTP server must be configured
  613. to allow accesses outside its TFTP root directory. This may need to be
  614. considered for security aspects."
  615. (use-modules ((guix build union) #:select (symlink-relative)))
  616. (let* ((net-dir (string-append mount-point target "/"))
  617. (sub-dir (string-append net-dir #$subdir "/"))
  618. (store (string-append mount-point (%store-prefix)))
  619. (store-link (string-append net-dir (%store-prefix)))
  620. (grub-cfg (string-append mount-point "/boot/grub/grub.cfg"))
  621. (grub-cfg-link (string-append sub-dir (basename grub-cfg)))
  622. (boot-efi-link (string-append sub-dir #$boot-efi-link)))
  623. ;; Prepare the symlink to the store.
  624. (mkdir-p (dirname store-link))
  625. (false-if-exception (delete-file store-link))
  626. (symlink-relative store store-link)
  627. ;; Prepare the symlink to the grub.cfg, which points into the store.
  628. (mkdir-p (dirname grub-cfg-link))
  629. (false-if-exception (delete-file grub-cfg-link))
  630. (symlink-relative grub-cfg grub-cfg-link)
  631. ;; Install GRUB, which refers to the grub.cfg, with support for
  632. ;; encrypted partitions,
  633. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  634. (invoke/quiet (string-append bootloader "/bin/grub-mknetdir")
  635. (string-append "--net-directory=" net-dir)
  636. (string-append "--subdir=" #$subdir))
  637. ;; Prepare the bootloader symlink, which points to core.efi of GRUB.
  638. (false-if-exception (delete-file boot-efi-link))
  639. (symlink #$core-efi boot-efi-link))))))
  640. ;;;
  641. ;;; Bootloader definitions.
  642. ;;;
  643. ;;; For all these grub-bootloader variables the path to /boot/grub/grub.cfg
  644. ;;; is fixed. Inheriting and overwriting the field 'configuration-file' will
  645. ;;; break 'guix system delete-generations', 'guix system switch-generation',
  646. ;;; and 'guix system roll-back'.
  647. (define grub-bootloader
  648. (bootloader
  649. (name 'grub)
  650. (package grub)
  651. (installer install-grub)
  652. (disk-image-installer install-grub-disk-image)
  653. (configuration-file "/boot/grub/grub.cfg")
  654. (configuration-file-generator grub-configuration-file)))
  655. (define grub-minimal-bootloader
  656. (bootloader
  657. (inherit grub-bootloader)
  658. (package grub-minimal)))
  659. (define grub-efi-bootloader
  660. (bootloader
  661. (inherit grub-bootloader)
  662. (installer install-grub-efi)
  663. (disk-image-installer #f)
  664. (name 'grub-efi)
  665. (package grub-efi)))
  666. (define grub-efi-netboot-bootloader
  667. (bootloader
  668. (inherit grub-efi-bootloader)
  669. (name 'grub-efi-netboot-bootloader)
  670. (installer (install-grub-efi-netboot "efi/Guix"))))
  671. (define grub-mkrescue-bootloader
  672. (bootloader
  673. (inherit grub-efi-bootloader)
  674. (package grub-hybrid)))
  675. ;;;
  676. ;;; Compatibility macros.
  677. ;;;
  678. (define-syntax grub-configuration
  679. (syntax-rules (grub)
  680. ((_ (grub package) fields ...)
  681. (if (eq? package grub)
  682. (bootloader-configuration
  683. (bootloader grub-bootloader)
  684. fields ...)
  685. (bootloader-configuration
  686. (bootloader grub-efi-bootloader)
  687. fields ...)))
  688. ((_ fields ...)
  689. (bootloader-configuration
  690. (bootloader grub-bootloader)
  691. fields ...))))
  692. ;;; grub.scm ends here