linux-initrd.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2017, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu system linux-initrd)
  23. #:use-module (guix gexp)
  24. #:use-module (guix utils)
  25. #:use-module ((guix store)
  26. #:select (%store-prefix))
  27. #:use-module ((guix derivations)
  28. #:select (derivation->output-path))
  29. #:use-module (guix modules)
  30. #:use-module (gnu packages compression)
  31. #:use-module (gnu packages disk)
  32. #:use-module (gnu packages linux)
  33. #:use-module (gnu packages file-systems)
  34. #:use-module (gnu packages guile)
  35. #:use-module ((gnu packages xorg)
  36. #:select (console-setup xkeyboard-config))
  37. #:use-module ((gnu packages make-bootstrap)
  38. #:select (%guile-3.0-static-stripped))
  39. #:use-module (gnu system file-systems)
  40. #:use-module (gnu system mapped-devices)
  41. #:use-module (gnu system keyboard)
  42. #:use-module (ice-9 match)
  43. #:use-module (ice-9 regex)
  44. #:use-module (ice-9 vlist)
  45. #:use-module (srfi srfi-1)
  46. #:use-module (srfi srfi-26)
  47. #:export (expression->initrd
  48. %base-initrd-modules
  49. raw-initrd
  50. file-system-packages
  51. base-initrd))
  52. ;;; Commentary:
  53. ;;;
  54. ;;; Tools to build initial RAM disks (initrd's) for Linux-Libre, and in
  55. ;;; particular initrd's that run Guile.
  56. ;;;
  57. ;;; Code:
  58. (define* (expression->initrd exp
  59. #:key
  60. (guile %guile-3.0-static-stripped)
  61. (gzip gzip)
  62. (name "guile-initrd")
  63. (system (%current-system)))
  64. "Return as a file-like object a Linux initrd (a gzipped cpio archive)
  65. containing GUILE and that evaluates EXP, a G-expression, upon booting. All
  66. the derivations referenced by EXP are automatically copied to the initrd."
  67. ;; General Linux overview in `Documentation/early-userspace/README' and
  68. ;; `Documentation/filesystems/ramfs-rootfs-initramfs.txt'.
  69. (define init
  70. (program-file "init" exp #:guile guile))
  71. (define builder
  72. ;; Do not use "guile-zlib" extension here, otherwise it would drag the
  73. ;; non-static "zlib" package to the initrd closure. It is not needed
  74. ;; anyway because the modules are stored uncompressed within the initrd.
  75. (with-imported-modules (source-module-closure
  76. '((gnu build linux-initrd)))
  77. #~(begin
  78. (use-modules (gnu build linux-initrd))
  79. (mkdir #$output)
  80. ;; The guile used in the initrd must be present in the store, so
  81. ;; that module loading works once the root is switched.
  82. ;;
  83. ;; To ensure that is the case, add an explicit reference to the
  84. ;; guile package used in the initrd to the output.
  85. ;;
  86. ;; This fixes guix-patches bug #28399, "Fix mysql activation, and
  87. ;; add a basic test".
  88. (call-with-output-file (string-append #$ output "/references")
  89. (lambda (port)
  90. (simple-format port "~A\n" #$guile)))
  91. (build-initrd (string-append #$output "/initrd.cpio.gz")
  92. #:guile #$guile
  93. #:init #$init
  94. ;; Copy everything INIT refers to into the initrd.
  95. #:references-graphs '("closure")
  96. #:gzip (string-append #+gzip "/bin/gzip")))))
  97. (file-append (computed-file name builder
  98. #:options
  99. `(#:references-graphs (("closure" ,init))))
  100. "/initrd.cpio.gz"))
  101. (define (flat-linux-module-directory linux modules)
  102. "Return a flat directory containing the Linux kernel modules listed in
  103. MODULES and taken from LINUX."
  104. (define imported-modules
  105. (source-module-closure '((gnu build linux-modules)
  106. (guix build utils))))
  107. (define build-exp
  108. (with-imported-modules imported-modules
  109. (with-extensions (list guile-zlib)
  110. #~(begin
  111. (use-modules (gnu build linux-modules)
  112. (guix build utils)
  113. (srfi srfi-1)
  114. (srfi srfi-26))
  115. (define module-dir
  116. (string-append #$linux "/lib/modules"))
  117. (define modules
  118. (let* ((lookup (cut find-module-file module-dir <>))
  119. (modules (map lookup '#$modules)))
  120. (append modules
  121. (recursive-module-dependencies
  122. modules
  123. #:lookup-module lookup))))
  124. (define (maybe-uncompress file)
  125. ;; If FILE is a compressed module, uncompress it, as the initrd
  126. ;; is already gzipped as a whole.
  127. (cond
  128. ((string-contains file ".ko.gz")
  129. (invoke #+(file-append gzip "/bin/gunzip") file))))
  130. (mkdir #$output)
  131. (for-each (lambda (module)
  132. (let ((out-module
  133. (string-append #$output "/"
  134. (basename module))))
  135. (format #t "copying '~a'...~%" module)
  136. (copy-file module out-module)
  137. (maybe-uncompress out-module)))
  138. (delete-duplicates modules))
  139. ;; Hyphen or underscore? This database tells us.
  140. (write-module-name-database #$output)))))
  141. (computed-file "linux-modules" build-exp))
  142. (define* (raw-initrd file-systems
  143. #:key
  144. (linux linux-libre)
  145. (linux-modules '())
  146. (mapped-devices '())
  147. (keyboard-layout #f)
  148. (helper-packages '())
  149. qemu-networking?
  150. volatile-root?
  151. (on-error 'debug))
  152. "Return as a file-like object a raw initrd, with kernel
  153. modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
  154. mounted by the initrd, possibly in addition to the root file system specified
  155. on the kernel command line via '--root'. LINUX-MODULES is a list of kernel
  156. modules to be loaded at boot time. MAPPED-DEVICES is a list of device
  157. mappings to realize before FILE-SYSTEMS are mounted.
  158. HELPER-PACKAGES is a list of packages to be copied in the initrd. It may include
  159. e2fsck/static or other packages needed by the initrd to check root partition.
  160. When true, KEYBOARD-LAYOUT is a <keyboard-layout> record denoting the desired
  161. console keyboard layout. This is done before MAPPED-DEVICES are set up and
  162. before FILE-SYSTEMS are mounted such that, should the user need to enter a
  163. passphrase or use the REPL, this happens using the intended keyboard layout.
  164. When QEMU-NETWORKING? is true, set up networking with the standard QEMU
  165. parameters.
  166. When VOLATILE-ROOT? is true, the root file system is writable but any changes
  167. to it are lost.
  168. ON-ERROR is passed to 'call-with-error-handling'; it determines what happens
  169. upon error."
  170. (define device-mapping-commands
  171. ;; List of gexps to open the mapped devices.
  172. (map (lambda (md)
  173. (let* ((source (mapped-device-source md))
  174. (target (mapped-device-target md))
  175. (type (mapped-device-type md))
  176. (open (mapped-device-kind-open type)))
  177. (open source target)))
  178. mapped-devices))
  179. (define kodir
  180. (flat-linux-module-directory linux linux-modules))
  181. (expression->initrd
  182. (with-imported-modules (source-module-closure
  183. '((gnu build linux-boot)
  184. (guix build utils)
  185. (guix build bournish)
  186. (gnu system file-systems)
  187. (gnu build file-systems)))
  188. #~(begin
  189. (use-modules (gnu build linux-boot)
  190. (gnu system file-systems)
  191. ((guix build utils) #:hide (delete))
  192. (guix build bournish) ;add the 'bournish' meta-command
  193. (srfi srfi-26)
  194. ;; FIXME: The following modules are for
  195. ;; LUKS-DEVICE-MAPPING. We should instead propagate
  196. ;; this info via gexps.
  197. ((gnu build file-systems)
  198. #:select (find-partition-by-luks-uuid))
  199. (rnrs bytevectors))
  200. (with-output-to-port (%make-void-port "w")
  201. (lambda ()
  202. (set-path-environment-variable "PATH" '("bin" "sbin")
  203. '#$helper-packages)))
  204. (parameterize ((current-warning-port (%make-void-port "w")))
  205. (boot-system #:mounts
  206. (map spec->file-system
  207. '#$(map file-system->spec file-systems))
  208. #:pre-mount (lambda ()
  209. (and #$@device-mapping-commands))
  210. #:linux-modules '#$linux-modules
  211. #:linux-module-directory '#$kodir
  212. #:keymap-file #+(and=> keyboard-layout
  213. keyboard-layout->console-keymap)
  214. #:qemu-guest-networking? #$qemu-networking?
  215. #:volatile-root? '#$volatile-root?
  216. #:on-error '#$on-error))))
  217. #:name "raw-initrd"))
  218. (define* (file-system-packages file-systems #:key (volatile-root? #f))
  219. "Return the list of statically-linked, stripped packages to check
  220. FILE-SYSTEMS."
  221. `(,@(if (find (lambda (fs)
  222. (string-prefix? "ext" (file-system-type fs)))
  223. file-systems)
  224. (list e2fsck/static)
  225. '())
  226. ,@(if (find (lambda (fs)
  227. (string-suffix? "fat" (file-system-type fs)))
  228. file-systems)
  229. (list fatfsck/static)
  230. '())
  231. ,@(if (find (file-system-type-predicate "btrfs") file-systems)
  232. (list btrfs-progs/static)
  233. '())
  234. ,@(if (find (file-system-type-predicate "jfs") file-systems)
  235. (list jfs_fsck/static)
  236. '())
  237. ,@(if (find (file-system-type-predicate "f2fs") file-systems)
  238. (list f2fs-fsck/static)
  239. '())))
  240. (define-syntax vhash ;TODO: factorize
  241. (syntax-rules (=>)
  242. "Build a vhash with the given key/value mappings."
  243. ((_)
  244. vlist-null)
  245. ((_ (key others ... => value) rest ...)
  246. (vhash-cons key value
  247. (vhash (others ... => value) rest ...)))
  248. ((_ (=> value) rest ...)
  249. (vhash rest ...))))
  250. (define-syntax lookup-procedure
  251. (syntax-rules (else)
  252. "Return a procedure that lookups keys in the given dictionary."
  253. ((_ mapping ... (else default))
  254. (let ((table (vhash mapping ...)))
  255. (lambda (key)
  256. (match (vhash-assoc key table)
  257. (#f default)
  258. ((key . value) value)))))))
  259. (define file-system-type-modules
  260. ;; Given a file system type, return the list of modules it needs.
  261. (lookup-procedure ("cifs" => '("md4" "ecb" "cifs"))
  262. ("9p" => '("9p" "9pnet_virtio"))
  263. ("btrfs" => '("btrfs"))
  264. ("iso9660" => '("isofs"))
  265. ("jfs" => '("jfs"))
  266. ("f2fs" => '("f2fs" "crc32_generic"))
  267. (else '())))
  268. (define (file-system-modules file-systems)
  269. "Return the list of Linux modules needed to mount FILE-SYSTEMS."
  270. (append-map (compose file-system-type-modules file-system-type)
  271. file-systems))
  272. (define* (default-initrd-modules
  273. #:optional
  274. (system (or (%current-target-system)
  275. (%current-system))))
  276. "Return the list of modules included in the initrd by default."
  277. (define virtio-modules
  278. ;; Modules for Linux para-virtualized devices, for use in QEMU guests.
  279. '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
  280. "virtio_console" "virtio-rng"))
  281. `("ahci" ;for SATA controllers
  282. "usb-storage" "uas" ;for the installation image etc.
  283. "usbhid" "hid-generic" "hid-apple" ;keyboards during early boot
  284. "dm-crypt" "xts" "serpent_generic" "wp512" ;for encrypted root partitions
  285. "nls_iso8859-1" ;for `mkfs.fat`, et.al
  286. ,@(if (string-match "^(x86_64|i[3-6]86)-" system)
  287. '("pata_acpi" "pata_atiixp" ;for ATA controllers
  288. "isci") ;for SAS controllers like Intel C602
  289. '())
  290. ,@virtio-modules))
  291. (define-syntax %base-initrd-modules
  292. ;; This more closely matches our naming convention.
  293. (identifier-syntax (default-initrd-modules)))
  294. (define* (base-initrd file-systems
  295. #:key
  296. (linux linux-libre)
  297. (linux-modules '())
  298. (mapped-devices '())
  299. (keyboard-layout #f)
  300. qemu-networking?
  301. volatile-root?
  302. (extra-modules '()) ;deprecated
  303. (on-error 'debug))
  304. "Return as a file-like object a generic initrd, with kernel
  305. modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
  306. mounted by the initrd, possibly in addition to the root file system specified
  307. on the kernel command line via '--root'. MAPPED-DEVICES is a list of device
  308. mappings to realize before FILE-SYSTEMS are mounted.
  309. When true, KEYBOARD-LAYOUT is a <keyboard-layout> record denoting the desired
  310. console keyboard layout. This is done before MAPPED-DEVICES are set up and
  311. before FILE-SYSTEMS are mounted such that, should the user need to enter a
  312. passphrase or use the REPL, this happens using the intended keyboard layout.
  313. QEMU-NETWORKING? and VOLATILE-ROOT? behaves as in raw-initrd.
  314. The initrd is automatically populated with all the kernel modules necessary
  315. for FILE-SYSTEMS and for the given options. Additional kernel
  316. modules can be listed in LINUX-MODULES. They will be added to the initrd, and
  317. loaded at boot time in the order in which they appear."
  318. (define linux-modules*
  319. ;; Modules added to the initrd and loaded from the initrd.
  320. `(,@linux-modules
  321. ,@(file-system-modules file-systems)
  322. ,@(if volatile-root?
  323. '("overlay")
  324. '())
  325. ,@extra-modules))
  326. (define helper-packages
  327. (append (file-system-packages file-systems
  328. #:volatile-root? volatile-root?)
  329. (if keyboard-layout
  330. (list loadkeys-static)
  331. '())))
  332. (raw-initrd file-systems
  333. #:linux linux
  334. #:linux-modules linux-modules*
  335. #:mapped-devices mapped-devices
  336. #:helper-packages helper-packages
  337. #:keyboard-layout keyboard-layout
  338. #:qemu-networking? qemu-networking?
  339. #:volatile-root? volatile-root?
  340. #:on-error on-error))
  341. ;;; linux-initrd.scm ends here