grub.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 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 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu bootloader grub)
  22. #:use-module (guix store)
  23. #:use-module (guix packages)
  24. #:use-module (guix derivations)
  25. #:use-module (guix records)
  26. #:use-module (guix monads)
  27. #:use-module (guix gexp)
  28. #:use-module (guix download)
  29. #:use-module (gnu artwork)
  30. #:use-module (gnu system)
  31. #:use-module (gnu bootloader)
  32. #:use-module (gnu system uuid)
  33. #:autoload (gnu packages bootloaders) (grub)
  34. #:autoload (gnu packages compression) (gzip)
  35. #:autoload (gnu packages gtk) (guile-cairo guile-rsvg)
  36. #:autoload (gnu packages guile) (guile-2.2)
  37. #:use-module (ice-9 match)
  38. #:use-module (ice-9 regex)
  39. #:use-module (srfi srfi-1)
  40. #:use-module (rnrs bytevectors)
  41. #:export (grub-image
  42. grub-image?
  43. grub-image-aspect-ratio
  44. grub-image-file
  45. grub-theme
  46. grub-theme?
  47. grub-theme-images
  48. grub-theme-color-normal
  49. grub-theme-color-highlight
  50. %background-image
  51. %default-theme
  52. grub-bootloader
  53. grub-efi-bootloader
  54. grub-mkrescue-bootloader
  55. grub-configuration))
  56. ;;; Commentary:
  57. ;;;
  58. ;;; Configuration of GNU GRUB.
  59. ;;;
  60. ;;; Code:
  61. (define (strip-mount-point mount-point file)
  62. "Strip MOUNT-POINT from FILE, which is a gexp or other lowerable object
  63. denoting a file name."
  64. (match mount-point
  65. ((? string? mount-point)
  66. (if (string=? mount-point "/")
  67. file
  68. #~(let ((file #$file))
  69. (if (string-prefix? #$mount-point file)
  70. (substring #$file #$(string-length mount-point))
  71. file))))
  72. (#f file)))
  73. (define-record-type* <grub-image>
  74. grub-image make-grub-image
  75. grub-image?
  76. (aspect-ratio grub-image-aspect-ratio ;rational number
  77. (default 4/3))
  78. (file grub-image-file)) ;file-valued gexp (SVG)
  79. (define-record-type* <grub-theme>
  80. grub-theme make-grub-theme
  81. grub-theme?
  82. (images grub-theme-images
  83. (default '())) ;list of <grub-image>
  84. (color-normal grub-theme-color-normal
  85. (default '((fg . cyan) (bg . blue))))
  86. (color-highlight grub-theme-color-highlight
  87. (default '((fg . white) (bg . blue)))))
  88. (define %background-image
  89. (grub-image
  90. (aspect-ratio 4/3)
  91. (file (file-append %artwork-repository
  92. "/grub/GuixSD-fully-black-4-3.svg"))))
  93. (define %default-theme
  94. ;; Default theme contributed by Felipe López.
  95. (grub-theme
  96. (images (list %background-image))
  97. (color-highlight '((fg . yellow) (bg . black)))
  98. (color-normal '((fg . light-gray) (bg . black))))) ;XXX: #x303030
  99. ;;;
  100. ;;; Background image & themes.
  101. ;;;
  102. (define (bootloader-theme config)
  103. "Return user defined theme in CONFIG if defined or %default-theme
  104. otherwise."
  105. (or (bootloader-configuration-theme config) %default-theme))
  106. (define* (svg->png svg #:key width height)
  107. "Build a PNG of HEIGHT x WIDTH from SVG."
  108. ;; Note: Guile-RSVG & co. are now built for Guile 2.2, so we use 2.2 here.
  109. ;; TODO: Remove #:guile-for-build when 2.2 has become the default.
  110. (mlet %store-monad ((guile (package->derivation guile-2.2 #:graft? #f)))
  111. (gexp->derivation "grub-image.png"
  112. (with-imported-modules '((gnu build svg))
  113. #~(begin
  114. ;; We need these two libraries.
  115. (add-to-load-path (string-append #+guile-rsvg
  116. "/share/guile/site/"
  117. (effective-version)))
  118. (add-to-load-path (string-append #+guile-cairo
  119. "/share/guile/site/"
  120. (effective-version)))
  121. (use-modules (gnu build svg))
  122. (svg->png #+svg #$output
  123. #:width #$width
  124. #:height #$height)))
  125. #:guile-for-build guile)))
  126. (define* (grub-background-image config #:key (width 1024) (height 768))
  127. "Return the GRUB background image defined in CONFIG with a ratio of
  128. WIDTH/HEIGHT, or #f if none was found."
  129. (let* ((ratio (/ width height))
  130. (image (find (lambda (image)
  131. (= (grub-image-aspect-ratio image) ratio))
  132. (grub-theme-images
  133. (bootloader-theme config)))))
  134. (if image
  135. (svg->png (grub-image-file image)
  136. #:width width #:height height)
  137. (with-monad %store-monad
  138. (return #f)))))
  139. (define* (eye-candy config store-device store-mount-point
  140. #:key system port)
  141. "Return in %STORE-MONAD a gexp that writes to PORT (a port-valued gexp) the
  142. 'grub.cfg' part concerned with graphics mode, background images, colors, and
  143. all that. STORE-DEVICE designates the device holding the store, and
  144. STORE-MOUNT-POINT is its mount point; these are used to determine where the
  145. background image and fonts must be searched for. SYSTEM must be the target
  146. system string---e.g., \"x86_64-linux\"."
  147. (define setup-gfxterm-body
  148. ;; Intel and EFI systems need to be switched into graphics mode, whereas
  149. ;; most other modern architectures have no other mode and therefore don't
  150. ;; need to be switched.
  151. (if (string-match "^(x86_64|i[3-6]86)-" system)
  152. "
  153. # Leave 'gfxmode' to 'auto'.
  154. insmod video_bochs
  155. insmod video_cirrus
  156. insmod gfxterm
  157. if [ \"${grub_platform}\" == efi ]; then
  158. # This is for (U)EFI systems (these modules are unavailable in the
  159. # non-EFI GRUB.) If we don't load them, GRUB boots in \"blind mode\",
  160. # which isn't convenient.
  161. insmod efi_gop
  162. insmod efi_uga
  163. else
  164. # These are specific to non-EFI Intel machines.
  165. insmod vbe
  166. insmod vga
  167. fi
  168. "
  169. ""))
  170. (define (setup-gfxterm config font-file)
  171. (if (memq 'gfxterm (bootloader-configuration-terminal-outputs config))
  172. #~(format #f "if loadfont ~a; then
  173. setup_gfxterm
  174. fi~%" #$font-file)
  175. ""))
  176. (define (theme-colors type)
  177. (let* ((theme (bootloader-theme config))
  178. (colors (type theme)))
  179. (string-append (symbol->string (assoc-ref colors 'fg)) "/"
  180. (symbol->string (assoc-ref colors 'bg)))))
  181. (define font-file
  182. (strip-mount-point store-mount-point
  183. (file-append grub "/share/grub/unicode.pf2")))
  184. (mlet* %store-monad ((image (grub-background-image config)))
  185. (return (and image
  186. #~(format #$port "
  187. function setup_gfxterm {~a}
  188. # Set 'root' to the partition that contains /gnu/store.
  189. ~a
  190. ~a
  191. ~a
  192. insmod png
  193. if background_image ~a; then
  194. set color_normal=~a
  195. set color_highlight=~a
  196. else
  197. set menu_color_normal=cyan/blue
  198. set menu_color_highlight=white/blue
  199. fi~%"
  200. #$setup-gfxterm-body
  201. #$(grub-root-search store-device font-file)
  202. #$(setup-gfxterm config font-file)
  203. #$(grub-setup-io config)
  204. #$(strip-mount-point store-mount-point image)
  205. #$(theme-colors grub-theme-color-normal)
  206. #$(theme-colors grub-theme-color-highlight))))))
  207. ;;;
  208. ;;; Configuration file.
  209. ;;;
  210. (define (grub-setup-io config)
  211. "Return GRUB commands to configure the input / output interfaces. The result
  212. is a string that can be inserted in grub.cfg."
  213. (let* ((symbols->string (lambda (list)
  214. (string-join (map symbol->string list) " ")))
  215. (outputs (bootloader-configuration-terminal-outputs config))
  216. (inputs (bootloader-configuration-terminal-inputs config))
  217. (unit (bootloader-configuration-serial-unit config))
  218. (speed (bootloader-configuration-serial-speed config))
  219. ;; Respectively, GRUB_TERMINAL_OUTPUT and GRUB_TERMINAL_INPUT,
  220. ;; as documented in GRUB manual section "Simple Configuration
  221. ;; Handling".
  222. (valid-outputs '(console serial serial_0 serial_1 serial_2 serial_3
  223. gfxterm vga_text mda_text morse spkmodem))
  224. (valid-inputs '(console serial serial_0 serial_1 serial_2 serial_3
  225. at_keyboard usb_keyboard))
  226. (io (string-append
  227. "terminal_output "
  228. (symbols->string
  229. (map
  230. (lambda (output)
  231. (if (memq output valid-outputs) output #f)) outputs)) "\n"
  232. (if (null? inputs)
  233. ""
  234. (string-append
  235. "terminal_input "
  236. (symbols->string
  237. (map
  238. (lambda (input)
  239. (if (memq input valid-inputs) input #f)) inputs)) "\n"))
  240. ;; UNIT and SPEED are arguments to the same GRUB command
  241. ;; ("serial"), so we process them together.
  242. (if (or unit speed)
  243. (string-append
  244. "serial"
  245. (if unit
  246. ;; COM ports 1 through 4
  247. (if (and (exact-integer? unit) (<= unit 3) (>= unit 0))
  248. (string-append " --unit=" (number->string unit))
  249. #f)
  250. "")
  251. (if speed
  252. (if (exact-integer? speed)
  253. (string-append " --speed=" (number->string speed))
  254. #f)
  255. ""))
  256. ""))))
  257. (format #f "~a" io)))
  258. (define (grub-root-search device file)
  259. "Return the GRUB 'search' command to look for DEVICE, which contains FILE,
  260. a gexp. The result is a gexp that can be inserted in the grub.cfg-generation
  261. code."
  262. ;; Usually FILE is a file name gexp like "/gnu/store/…-linux/vmlinuz", but
  263. ;; it can also be something like "(hd0,msdos1)/vmlinuz" in the case of
  264. ;; custom menu entries. In the latter case, don't emit a 'search' command.
  265. (if (and (string? file) (not (string-prefix? "/" file)))
  266. ""
  267. (match device
  268. ;; Preferably refer to DEVICE by its UUID or label. This is more
  269. ;; efficient and less ambiguous, see <http://bugs.gnu.org/22281>.
  270. ((? uuid? uuid)
  271. (format #f "search --fs-uuid --set ~a"
  272. (uuid->string device)))
  273. ((? string? label)
  274. (format #f "search --label --set ~a" label))
  275. (#f
  276. #~(format #f "search --file --set ~a" #$file)))))
  277. (define* (grub-configuration-file config entries
  278. #:key
  279. (system (%current-system))
  280. (old-entries '()))
  281. "Return the GRUB configuration file corresponding to CONFIG, a
  282. <bootloader-configuration> object, and where the store is available at
  283. STORE-FS, a <file-system> object. OLD-ENTRIES is taken to be a list of menu
  284. entries corresponding to old generations of the system."
  285. (define all-entries
  286. (append entries (bootloader-configuration-menu-entries config)))
  287. (define (menu-entry->gexp entry)
  288. (let ((device (menu-entry-device entry))
  289. (device-mount-point (menu-entry-device-mount-point entry))
  290. (label (menu-entry-label entry))
  291. (kernel (menu-entry-linux entry))
  292. (arguments (menu-entry-linux-arguments entry))
  293. (initrd (menu-entry-initrd entry)))
  294. ;; Here DEVICE is the store and DEVICE-MOUNT-POINT is its mount point.
  295. ;; Use the right file names for KERNEL and INITRD in case
  296. ;; DEVICE-MOUNT-POINT is not "/", meaning that the store is on a
  297. ;; separate partition.
  298. (let ((kernel (strip-mount-point device-mount-point kernel))
  299. (initrd (strip-mount-point device-mount-point initrd)))
  300. #~(format port "menuentry ~s {
  301. ~a
  302. linux ~a ~a
  303. initrd ~a
  304. }~%"
  305. #$label
  306. #$(grub-root-search device kernel)
  307. #$kernel (string-join (list #$@arguments))
  308. #$initrd))))
  309. (mlet %store-monad ((sugar (eye-candy config
  310. (menu-entry-device
  311. (first all-entries))
  312. (menu-entry-device-mount-point
  313. (first all-entries))
  314. #:system system
  315. #:port #~port)))
  316. (define builder
  317. #~(call-with-output-file #$output
  318. (lambda (port)
  319. (format port
  320. "# This file was generated from your GuixSD configuration. Any changes
  321. # will be lost upon reconfiguration.
  322. ")
  323. #$sugar
  324. (format port "
  325. set default=~a
  326. set timeout=~a~%"
  327. #$(bootloader-configuration-default-entry config)
  328. #$(bootloader-configuration-timeout config))
  329. #$@(map menu-entry->gexp all-entries)
  330. #$@(if (pair? old-entries)
  331. #~((format port "
  332. submenu \"GNU system, old configurations...\" {~%")
  333. #$@(map menu-entry->gexp old-entries)
  334. (format port "}~%"))
  335. #~()))))
  336. (gexp->derivation "grub.cfg" builder)))
  337. ;;;
  338. ;;; Install procedures.
  339. ;;;
  340. (define install-grub
  341. #~(lambda (bootloader device mount-point)
  342. ;; Install GRUB on DEVICE which is mounted at MOUNT-POINT.
  343. (let ((grub (string-append bootloader "/sbin/grub-install"))
  344. (install-dir (string-append mount-point "/boot")))
  345. ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
  346. ;; root partition.
  347. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  348. (unless (zero? (system* grub "--no-floppy"
  349. "--boot-directory" install-dir
  350. device))
  351. (error "failed to install GRUB (BIOS)")))))
  352. (define install-grub-efi
  353. #~(lambda (bootloader efi-dir mount-point)
  354. ;; Install GRUB onto the EFI partition mounted at EFI-DIR, for the
  355. ;; system whose root is mounted at MOUNT-POINT.
  356. (let ((grub-install (string-append bootloader "/sbin/grub-install"))
  357. (install-dir (string-append mount-point "/boot")))
  358. ;; Tell 'grub-install' that there might be a LUKS-encrypted /boot or
  359. ;; root partition.
  360. (setenv "GRUB_ENABLE_CRYPTODISK" "y")
  361. (unless (zero? (system* grub-install "--boot-directory" install-dir
  362. "--efi-directory" efi-dir))
  363. (error "failed to install GRUB (EFI)")))))
  364. ;;;
  365. ;;; Bootloader definitions.
  366. ;;;
  367. (define grub-bootloader
  368. (bootloader
  369. (name 'grub)
  370. (package grub)
  371. (installer install-grub)
  372. (configuration-file "/boot/grub/grub.cfg")
  373. (configuration-file-generator grub-configuration-file)))
  374. (define* grub-efi-bootloader
  375. (bootloader
  376. (inherit grub-bootloader)
  377. (installer install-grub-efi)
  378. (name 'grub-efi)
  379. (package grub-efi)))
  380. (define* grub-mkrescue-bootloader
  381. (bootloader
  382. (inherit grub-efi-bootloader)
  383. (package grub-hybrid)))
  384. ;;;
  385. ;;; Compatibility macros.
  386. ;;;
  387. (define-syntax grub-configuration
  388. (syntax-rules (grub)
  389. ((_ (grub package) fields ...)
  390. (if (eq? package grub)
  391. (bootloader-configuration
  392. (bootloader grub-bootloader)
  393. fields ...)
  394. (bootloader-configuration
  395. (bootloader grub-efi-bootloader)
  396. fields ...)))
  397. ((_ fields ...)
  398. (bootloader-configuration
  399. (bootloader grub-bootloader)
  400. fields ...))))
  401. ;;; grub.scm ends here