linux-initrd.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 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 monads)
  23. #:use-module (guix store)
  24. #:use-module (guix gexp)
  25. #:use-module (guix utils)
  26. #:use-module ((guix store)
  27. #:select (%store-prefix))
  28. #:use-module ((guix derivations)
  29. #:select (derivation->output-path))
  30. #:use-module (guix modules)
  31. #:use-module (gnu packages compression)
  32. #:use-module (gnu packages disk)
  33. #:use-module (gnu packages linux)
  34. #:use-module (gnu packages guile)
  35. #:use-module ((gnu packages make-bootstrap)
  36. #:select (%guile-static-stripped))
  37. #:use-module (gnu system file-systems)
  38. #:use-module (gnu system mapped-devices)
  39. #:use-module (ice-9 match)
  40. #:use-module (ice-9 regex)
  41. #:use-module (srfi srfi-1)
  42. #:use-module (srfi srfi-26)
  43. #:export (expression->initrd
  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 a derivation that builds 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. (build-initrd (string-append #$output "/initrd")
  73. #:guile #$guile
  74. #:init #$init
  75. ;; Copy everything INIT refers to into the initrd.
  76. #:references-graphs '("closure")
  77. #:gzip (string-append #$gzip "/bin/gzip")))))
  78. (gexp->derivation name builder
  79. #:references-graphs `(("closure" ,init))))
  80. (define (flat-linux-module-directory linux modules)
  81. "Return a flat directory containing the Linux kernel modules listed in
  82. MODULES and taken from LINUX."
  83. (define build-exp
  84. (with-imported-modules (source-module-closure
  85. '((guix build utils)
  86. (gnu build linux-modules)))
  87. #~(begin
  88. (use-modules (ice-9 match) (ice-9 regex)
  89. (srfi srfi-1)
  90. (guix build utils)
  91. (gnu build linux-modules))
  92. (define (string->regexp str)
  93. ;; Return a regexp that matches STR exactly.
  94. (string-append "^" (regexp-quote str) "$"))
  95. (define module-dir
  96. (string-append #$linux "/lib/modules"))
  97. (define (lookup module)
  98. (let ((name (ensure-dot-ko module)))
  99. (match (find-files module-dir (string->regexp name))
  100. ((file)
  101. file)
  102. (()
  103. (error "module not found" name module-dir))
  104. ((_ ...)
  105. (error "several modules by that name"
  106. name module-dir)))))
  107. (define modules
  108. (let ((modules (map lookup '#$modules)))
  109. (append modules
  110. (recursive-module-dependencies modules
  111. #:lookup-module lookup))))
  112. (mkdir #$output)
  113. (for-each (lambda (module)
  114. (format #t "copying '~a'...~%" module)
  115. (copy-file module
  116. (string-append #$output "/"
  117. (basename module))))
  118. (delete-duplicates modules)))))
  119. (computed-file "linux-modules" build-exp))
  120. (define* (raw-initrd file-systems
  121. #:key
  122. (linux linux-libre)
  123. (linux-modules '())
  124. (mapped-devices '())
  125. (helper-packages '())
  126. qemu-networking?
  127. volatile-root?)
  128. "Return a monadic derivation that builds 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. (define device-mapping-commands
  141. ;; List of gexps to open the mapped devices.
  142. (map (lambda (md)
  143. (let* ((source (mapped-device-source md))
  144. (target (mapped-device-target md))
  145. (type (mapped-device-type md))
  146. (open (mapped-device-kind-open type)))
  147. (open source target)))
  148. mapped-devices))
  149. (define kodir
  150. (flat-linux-module-directory linux linux-modules))
  151. (expression->initrd
  152. (with-imported-modules (source-module-closure
  153. '((gnu build linux-boot)
  154. (guix build utils)
  155. (guix build bournish)
  156. (gnu build file-systems)))
  157. #~(begin
  158. (use-modules (gnu build linux-boot)
  159. (guix build utils)
  160. (guix build bournish) ;add the 'bournish' meta-command
  161. (srfi srfi-26)
  162. ;; FIXME: The following modules are for
  163. ;; LUKS-DEVICE-MAPPING. We should instead propagate
  164. ;; this info via gexps.
  165. ((gnu build file-systems)
  166. #:select (find-partition-by-luks-uuid))
  167. (rnrs bytevectors))
  168. (with-output-to-port (%make-void-port "w")
  169. (lambda ()
  170. (set-path-environment-variable "PATH" '("bin" "sbin")
  171. '#$helper-packages)))
  172. (boot-system #:mounts '#$(map file-system->spec file-systems)
  173. #:pre-mount (lambda ()
  174. (and #$@device-mapping-commands))
  175. #:linux-modules '#$linux-modules
  176. #:linux-module-directory '#$kodir
  177. #:qemu-guest-networking? #$qemu-networking?
  178. #:volatile-root? '#$volatile-root?)))
  179. #:name "raw-initrd"))
  180. (define* (file-system-packages file-systems #:key (volatile-root? #f))
  181. "Return the list of statically-linked, stripped packages to check
  182. FILE-SYSTEMS."
  183. `(,@(if (find (lambda (fs)
  184. (string-prefix? "ext" (file-system-type fs)))
  185. file-systems)
  186. (list e2fsck/static)
  187. '())
  188. ,@(if (find (lambda (fs)
  189. (string-suffix? "fat" (file-system-type fs)))
  190. file-systems)
  191. (list fatfsck/static)
  192. '())
  193. ,@(if (find (file-system-type-predicate "btrfs") file-systems)
  194. (list btrfs-progs/static)
  195. '())
  196. ,@(if volatile-root?
  197. (list unionfs-fuse/static)
  198. '())))
  199. (define* (base-initrd file-systems
  200. #:key
  201. (linux linux-libre)
  202. (mapped-devices '())
  203. qemu-networking?
  204. volatile-root?
  205. (virtio? #t)
  206. (extra-modules '()))
  207. "Return a monadic derivation that builds a generic initrd, with kernel
  208. modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
  209. mounted by the initrd, possibly in addition to the root file system specified
  210. on the kernel command line via '--root'. MAPPED-DEVICES is a list of device
  211. mappings to realize before FILE-SYSTEMS are mounted.
  212. QEMU-NETWORKING? and VOLATILE-ROOT? behaves as in raw-initrd.
  213. When VIRTIO? is true, load additional modules so the initrd can
  214. be used as a QEMU guest with the root file system on a para-virtualized block
  215. device.
  216. The initrd is automatically populated with all the kernel modules necessary
  217. for FILE-SYSTEMS and for the given options. However, additional kernel
  218. modules can be listed in EXTRA-MODULES. They will be added to the initrd, and
  219. loaded at boot time in the order in which they appear."
  220. (define virtio-modules
  221. ;; Modules for Linux para-virtualized devices, for use in QEMU guests.
  222. '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
  223. "virtio_console"))
  224. (define cifs-modules
  225. ;; Modules needed to mount CIFS file systems.
  226. '("md4" "ecb" "cifs"))
  227. (define virtio-9p-modules
  228. ;; Modules for the 9p paravirtualized file system.
  229. '("9p" "9pnet_virtio"))
  230. (define (file-system-type-predicate type)
  231. (lambda (fs)
  232. (string=? (file-system-type fs) type)))
  233. (define linux-modules
  234. ;; Modules added to the initrd and loaded from the initrd.
  235. `("ahci" ;for SATA controllers
  236. "usb-storage" "uas" ;for the installation image etc.
  237. "usbhid" "hid-generic" "hid-apple" ;keyboards during early boot
  238. "dm-crypt" "xts" "serpent_generic" "wp512" ;for encrypted root partitions
  239. "nvme" ;for new SSD NVMe devices
  240. "nls_iso8859-1" ;for `mkfs.fat`, et.al
  241. ,@(if (string-match "^(x86_64|i[3-6]86)-" (%current-system))
  242. '("pata_acpi" "pata_atiixp" ;for ATA controllers
  243. "isci") ;for SAS controllers like Intel C602
  244. '())
  245. ,@(if (or virtio? qemu-networking?)
  246. virtio-modules
  247. '())
  248. ,@(if (find (file-system-type-predicate "cifs") file-systems)
  249. cifs-modules
  250. '())
  251. ,@(if (find (file-system-type-predicate "9p") file-systems)
  252. virtio-9p-modules
  253. '())
  254. ,@(if (find (file-system-type-predicate "btrfs") file-systems)
  255. '("btrfs")
  256. '())
  257. ,@(if (find (file-system-type-predicate "iso9660") file-systems)
  258. '("isofs")
  259. '())
  260. ,@(if volatile-root?
  261. '("fuse")
  262. '())
  263. ,@extra-modules))
  264. (define helper-packages
  265. (file-system-packages file-systems #:volatile-root? volatile-root?))
  266. (raw-initrd file-systems
  267. #:linux linux
  268. #:linux-modules linux-modules
  269. #:mapped-devices mapped-devices
  270. #:helper-packages helper-packages
  271. #:qemu-networking? qemu-networking?
  272. #:volatile-root? volatile-root?))
  273. ;;; linux-initrd.scm ends here