linux-initrd.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 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 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 system linux-initrd)
  22. #:use-module (guix gexp)
  23. #:use-module (guix utils)
  24. #:use-module ((guix store)
  25. #:select (%store-prefix))
  26. #:use-module ((guix derivations)
  27. #:select (derivation->output-path))
  28. #:use-module (guix modules)
  29. #:use-module (gnu packages compression)
  30. #:use-module (gnu packages disk)
  31. #:use-module (gnu packages linux)
  32. #:use-module (gnu packages guile)
  33. #:use-module ((gnu packages make-bootstrap)
  34. #:select (%guile-static-stripped))
  35. #:use-module (gnu system file-systems)
  36. #:use-module (gnu system mapped-devices)
  37. #:use-module (ice-9 match)
  38. #:use-module (ice-9 regex)
  39. #:use-module (ice-9 vlist)
  40. #:use-module (srfi srfi-1)
  41. #:use-module (srfi srfi-26)
  42. #:export (expression->initrd
  43. %base-initrd-modules
  44. raw-initrd
  45. file-system-packages
  46. base-initrd))
  47. ;;; Commentary:
  48. ;;;
  49. ;;; Tools to build initial RAM disks (initrd's) for Linux-Libre, and in
  50. ;;; particular initrd's that run Guile.
  51. ;;;
  52. ;;; Code:
  53. (define* (expression->initrd exp
  54. #:key
  55. (guile %guile-static-stripped)
  56. (gzip gzip)
  57. (name "guile-initrd")
  58. (system (%current-system)))
  59. "Return as a file-like object a Linux initrd (a gzipped cpio archive)
  60. containing GUILE and that evaluates EXP, a G-expression, upon booting. All
  61. the derivations referenced by EXP are automatically copied to the initrd."
  62. ;; General Linux overview in `Documentation/early-userspace/README' and
  63. ;; `Documentation/filesystems/ramfs-rootfs-initramfs.txt'.
  64. (define init
  65. (program-file "init" exp #:guile guile))
  66. (define builder
  67. (with-imported-modules (source-module-closure
  68. '((gnu build linux-initrd)))
  69. #~(begin
  70. (use-modules (gnu build linux-initrd))
  71. (mkdir #$output)
  72. ;; The guile used in the initrd must be present in the store, so
  73. ;; that module loading works once the root is switched.
  74. ;;
  75. ;; To ensure that is the case, add an explicit reference to the
  76. ;; guile package used in the initrd to the output.
  77. ;;
  78. ;; This fixes guix-patches bug #28399, "Fix mysql activation, and
  79. ;; add a basic test".
  80. (call-with-output-file (string-append #$ output "/references")
  81. (lambda (port)
  82. (simple-format port "~A\n" #$guile)))
  83. (build-initrd (string-append #$output "/initrd.cpio.gz")
  84. #:guile #$guile
  85. #:init #$init
  86. ;; Copy everything INIT refers to into the initrd.
  87. #:references-graphs '("closure")
  88. #:gzip (string-append #$gzip "/bin/gzip")))))
  89. (file-append (computed-file name builder
  90. #:options
  91. `(#:references-graphs (("closure" ,init))))
  92. "/initrd.cpio.gz"))
  93. (define (flat-linux-module-directory linux modules)
  94. "Return a flat directory containing the Linux kernel modules listed in
  95. MODULES and taken from LINUX."
  96. (define build-exp
  97. (with-imported-modules (source-module-closure
  98. '((gnu build linux-modules)))
  99. #~(begin
  100. (use-modules (gnu build linux-modules)
  101. (srfi srfi-1)
  102. (srfi srfi-26))
  103. (define module-dir
  104. (string-append #$linux "/lib/modules"))
  105. (define modules
  106. (let* ((lookup (cut find-module-file module-dir <>))
  107. (modules (map lookup '#$modules)))
  108. (append modules
  109. (recursive-module-dependencies modules
  110. #:lookup-module lookup))))
  111. (mkdir #$output)
  112. (for-each (lambda (module)
  113. (format #t "copying '~a'...~%" module)
  114. (copy-file module
  115. (string-append #$output "/"
  116. (basename module))))
  117. (delete-duplicates modules)))))
  118. (computed-file "linux-modules" build-exp))
  119. (define* (raw-initrd file-systems
  120. #:key
  121. (linux linux-libre)
  122. (linux-modules '())
  123. (mapped-devices '())
  124. (helper-packages '())
  125. qemu-networking?
  126. volatile-root?
  127. (on-error 'debug))
  128. "Return as a file-like object a raw initrd, with kernel
  129. modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
  130. mounted by the initrd, possibly in addition to the root file system specified
  131. on the kernel command line via '--root'. LINUX-MODULES is a list of kernel
  132. modules to be loaded at boot time. MAPPED-DEVICES is a list of device
  133. mappings to realize before FILE-SYSTEMS are mounted.
  134. HELPER-PACKAGES is a list of packages to be copied in the initrd. It may include
  135. e2fsck/static or other packages needed by the initrd to check root partition.
  136. When QEMU-NETWORKING? is true, set up networking with the standard QEMU
  137. parameters.
  138. When VOLATILE-ROOT? is true, the root file system is writable but any changes
  139. to it are lost.
  140. ON-ERROR is passed to 'call-with-error-handling'; it determines what happens
  141. upon error."
  142. (define device-mapping-commands
  143. ;; List of gexps to open the mapped devices.
  144. (map (lambda (md)
  145. (let* ((source (mapped-device-source md))
  146. (target (mapped-device-target md))
  147. (type (mapped-device-type md))
  148. (open (mapped-device-kind-open type)))
  149. (open source target)))
  150. mapped-devices))
  151. (define kodir
  152. (flat-linux-module-directory linux linux-modules))
  153. (expression->initrd
  154. (with-imported-modules (source-module-closure
  155. '((gnu build linux-boot)
  156. (guix build utils)
  157. (guix build bournish)
  158. (gnu system file-systems)
  159. (gnu build file-systems)))
  160. #~(begin
  161. (use-modules (gnu build linux-boot)
  162. (gnu system file-systems)
  163. (guix build utils)
  164. (guix build bournish) ;add the 'bournish' meta-command
  165. (srfi srfi-26)
  166. ;; FIXME: The following modules are for
  167. ;; LUKS-DEVICE-MAPPING. We should instead propagate
  168. ;; this info via gexps.
  169. ((gnu build file-systems)
  170. #:select (find-partition-by-luks-uuid))
  171. (rnrs bytevectors))
  172. (with-output-to-port (%make-void-port "w")
  173. (lambda ()
  174. (set-path-environment-variable "PATH" '("bin" "sbin")
  175. '#$helper-packages)))
  176. (boot-system #:mounts
  177. (map spec->file-system
  178. '#$(map file-system->spec file-systems))
  179. #:pre-mount (lambda ()
  180. (and #$@device-mapping-commands))
  181. #:linux-modules '#$linux-modules
  182. #:linux-module-directory '#$kodir
  183. #:qemu-guest-networking? #$qemu-networking?
  184. #:volatile-root? '#$volatile-root?
  185. #:on-error '#$on-error)))
  186. #:name "raw-initrd"))
  187. (define* (file-system-packages file-systems #:key (volatile-root? #f))
  188. "Return the list of statically-linked, stripped packages to check
  189. FILE-SYSTEMS."
  190. `(,@(if (find (lambda (fs)
  191. (string-prefix? "ext" (file-system-type fs)))
  192. file-systems)
  193. (list e2fsck/static)
  194. '())
  195. ,@(if (find (lambda (fs)
  196. (string-suffix? "fat" (file-system-type fs)))
  197. file-systems)
  198. (list fatfsck/static)
  199. '())
  200. ,@(if (find (file-system-type-predicate "btrfs") file-systems)
  201. (list btrfs-progs/static)
  202. '())))
  203. (define-syntax vhash ;TODO: factorize
  204. (syntax-rules (=>)
  205. "Build a vhash with the given key/value mappings."
  206. ((_)
  207. vlist-null)
  208. ((_ (key others ... => value) rest ...)
  209. (vhash-cons key value
  210. (vhash (others ... => value) rest ...)))
  211. ((_ (=> value) rest ...)
  212. (vhash rest ...))))
  213. (define-syntax lookup-procedure
  214. (syntax-rules (else)
  215. "Return a procedure that lookups keys in the given dictionary."
  216. ((_ mapping ... (else default))
  217. (let ((table (vhash mapping ...)))
  218. (lambda (key)
  219. (match (vhash-assoc key table)
  220. (#f default)
  221. ((key . value) value)))))))
  222. (define file-system-type-modules
  223. ;; Given a file system type, return the list of modules it needs.
  224. (lookup-procedure ("cifs" => '("md4" "ecb" "cifs"))
  225. ("9p" => '("9p" "9pnet_virtio"))
  226. ("btrfs" => '("btrfs"))
  227. ("iso9660" => '("isofs"))
  228. (else '())))
  229. (define (file-system-modules file-systems)
  230. "Return the list of Linux modules needed to mount FILE-SYSTEMS."
  231. (append-map (compose file-system-type-modules file-system-type)
  232. file-systems))
  233. (define* (default-initrd-modules #:optional (system (%current-system)))
  234. "Return the list of modules included in the initrd by default."
  235. (define virtio-modules
  236. ;; Modules for Linux para-virtualized devices, for use in QEMU guests.
  237. '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
  238. "virtio_console" "virtio-rng"))
  239. `("ahci" ;for SATA controllers
  240. "usb-storage" "uas" ;for the installation image etc.
  241. "usbhid" "hid-generic" "hid-apple" ;keyboards during early boot
  242. "dm-crypt" "xts" "serpent_generic" "wp512" ;for encrypted root partitions
  243. "nls_iso8859-1" ;for `mkfs.fat`, et.al
  244. ,@(if (string-match "^(x86_64|i[3-6]86)-" system)
  245. '("pata_acpi" "pata_atiixp" ;for ATA controllers
  246. "isci") ;for SAS controllers like Intel C602
  247. '())
  248. ,@virtio-modules))
  249. (define-syntax %base-initrd-modules
  250. ;; This more closely matches our naming convention.
  251. (identifier-syntax (default-initrd-modules)))
  252. (define* (base-initrd file-systems
  253. #:key
  254. (linux linux-libre)
  255. (linux-modules '())
  256. (mapped-devices '())
  257. qemu-networking?
  258. volatile-root?
  259. (extra-modules '()) ;deprecated
  260. (on-error 'debug))
  261. "Return as a file-like object a generic initrd, with kernel
  262. modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
  263. mounted by the initrd, possibly in addition to the root file system specified
  264. on the kernel command line via '--root'. MAPPED-DEVICES is a list of device
  265. mappings to realize before FILE-SYSTEMS are mounted.
  266. QEMU-NETWORKING? and VOLATILE-ROOT? behaves as in raw-initrd.
  267. The initrd is automatically populated with all the kernel modules necessary
  268. for FILE-SYSTEMS and for the given options. Additional kernel
  269. modules can be listed in LINUX-MODULES. They will be added to the initrd, and
  270. loaded at boot time in the order in which they appear."
  271. (define linux-modules*
  272. ;; Modules added to the initrd and loaded from the initrd.
  273. `(,@linux-modules
  274. ,@(file-system-modules file-systems)
  275. ,@(if volatile-root?
  276. '("overlay")
  277. '())
  278. ,@extra-modules))
  279. (define helper-packages
  280. (file-system-packages file-systems #:volatile-root? volatile-root?))
  281. (raw-initrd file-systems
  282. #:linux linux
  283. #:linux-modules linux-modules*
  284. #:mapped-devices mapped-devices
  285. #:helper-packages helper-packages
  286. #:qemu-networking? qemu-networking?
  287. #:volatile-root? volatile-root?
  288. #:on-error on-error))
  289. ;;; linux-initrd.scm ends here