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