grub.scm 35 KB

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