grub.scm 28 KB

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