linux-initrd: Ensure that the guile used in the initrd is referenced.
[jackhill/guix/guix.git] / gnu / system / linux-initrd.scm
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
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 file-system-packages
47 base-initrd))
48
49 \f
50 ;;; Commentary:
51 ;;;
52 ;;; Tools to build initial RAM disks (initrd's) for Linux-Libre, and in
53 ;;; particular initrd's that run Guile.
54 ;;;
55 ;;; Code:
56
57
58 (define* (expression->initrd exp
59 #:key
60 (guile %guile-static-stripped)
61 (gzip gzip)
62 (name "guile-initrd")
63 (system (%current-system)))
64 "Return a derivation that builds 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
68 ;; General Linux overview in `Documentation/early-userspace/README' and
69 ;; `Documentation/filesystems/ramfs-rootfs-initramfs.txt'.
70
71 (define init
72 (program-file "init" exp #:guile guile))
73
74 (define builder
75 (with-imported-modules (source-module-closure
76 '((gnu build linux-initrd)))
77 #~(begin
78 (use-modules (gnu build linux-initrd))
79
80 (mkdir #$output)
81
82 ;; The guile used in the initrd must be present in the store, so
83 ;; that module loading works once the root is switched.
84 ;;
85 ;; To ensure that is the case, add an explicit reference to the
86 ;; guile package used in the initrd to the output.
87 ;;
88 ;; This fixes guix-patches bug #28399, "Fix mysql activation, and
89 ;; add a basic test".
90 (call-with-output-file (string-append #$ output "/references")
91 (lambda (port)
92 (simple-format port "~A\n" #$guile)))
93
94 (build-initrd (string-append #$output "/initrd")
95 #:guile #$guile
96 #:init #$init
97 ;; Copy everything INIT refers to into the initrd.
98 #:references-graphs '("closure")
99 #:gzip (string-append #$gzip "/bin/gzip")))))
100
101 (gexp->derivation name builder
102 #:references-graphs `(("closure" ,init))))
103
104 (define (flat-linux-module-directory linux modules)
105 "Return a flat directory containing the Linux kernel modules listed in
106 MODULES and taken from LINUX."
107 (define build-exp
108 (with-imported-modules (source-module-closure
109 '((guix build utils)
110 (gnu build linux-modules)))
111 #~(begin
112 (use-modules (ice-9 match) (ice-9 regex)
113 (srfi srfi-1)
114 (guix build utils)
115 (gnu build linux-modules))
116
117 (define (string->regexp str)
118 ;; Return a regexp that matches STR exactly.
119 (string-append "^" (regexp-quote str) "$"))
120
121 (define module-dir
122 (string-append #$linux "/lib/modules"))
123
124 (define (lookup module)
125 (let ((name (ensure-dot-ko module)))
126 (match (find-files module-dir (string->regexp name))
127 ((file)
128 file)
129 (()
130 (error "module not found" name module-dir))
131 ((_ ...)
132 (error "several modules by that name"
133 name module-dir)))))
134
135 (define modules
136 (let ((modules (map lookup '#$modules)))
137 (append modules
138 (recursive-module-dependencies modules
139 #:lookup-module lookup))))
140
141 (mkdir #$output)
142 (for-each (lambda (module)
143 (format #t "copying '~a'...~%" module)
144 (copy-file module
145 (string-append #$output "/"
146 (basename module))))
147 (delete-duplicates modules)))))
148
149 (computed-file "linux-modules" build-exp))
150
151 (define* (raw-initrd file-systems
152 #:key
153 (linux linux-libre)
154 (linux-modules '())
155 (mapped-devices '())
156 (helper-packages '())
157 qemu-networking?
158 volatile-root?)
159 "Return a monadic derivation that builds a raw initrd, with kernel
160 modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
161 mounted by the initrd, possibly in addition to the root file system specified
162 on the kernel command line via '--root'. LINUX-MODULES is a list of kernel
163 modules to be loaded at boot time. MAPPED-DEVICES is a list of device
164 mappings to realize before FILE-SYSTEMS are mounted.
165 HELPER-PACKAGES is a list of packages to be copied in the initrd. It may include
166 e2fsck/static or other packages needed by the initrd to check root partition.
167
168 When QEMU-NETWORKING? is true, set up networking with the standard QEMU
169 parameters.
170 When VOLATILE-ROOT? is true, the root file system is writable but any changes
171 to it are lost."
172 (define device-mapping-commands
173 ;; List of gexps to open the mapped devices.
174 (map (lambda (md)
175 (let* ((source (mapped-device-source md))
176 (target (mapped-device-target md))
177 (type (mapped-device-type md))
178 (open (mapped-device-kind-open type)))
179 (open source target)))
180 mapped-devices))
181
182 (define kodir
183 (flat-linux-module-directory linux linux-modules))
184
185 (expression->initrd
186 (with-imported-modules (source-module-closure
187 '((gnu build linux-boot)
188 (guix build utils)
189 (guix build bournish)
190 (gnu build file-systems)))
191 #~(begin
192 (use-modules (gnu build linux-boot)
193 (guix build utils)
194 (guix build bournish) ;add the 'bournish' meta-command
195 (srfi srfi-26)
196
197 ;; FIXME: The following modules are for
198 ;; LUKS-DEVICE-MAPPING. We should instead propagate
199 ;; this info via gexps.
200 ((gnu build file-systems)
201 #:select (find-partition-by-luks-uuid))
202 (rnrs bytevectors))
203
204 (with-output-to-port (%make-void-port "w")
205 (lambda ()
206 (set-path-environment-variable "PATH" '("bin" "sbin")
207 '#$helper-packages)))
208
209 (boot-system #:mounts '#$(map file-system->spec file-systems)
210 #:pre-mount (lambda ()
211 (and #$@device-mapping-commands))
212 #:linux-modules '#$linux-modules
213 #:linux-module-directory '#$kodir
214 #:qemu-guest-networking? #$qemu-networking?
215 #:volatile-root? '#$volatile-root?)))
216 #:name "raw-initrd"))
217
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 volatile-root?
235 (list unionfs-fuse/static)
236 '())))
237
238 (define* (base-initrd file-systems
239 #:key
240 (linux linux-libre)
241 (mapped-devices '())
242 qemu-networking?
243 volatile-root?
244 (virtio? #t)
245 (extra-modules '()))
246 "Return a monadic derivation that builds a generic initrd, with kernel
247 modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
248 mounted by the initrd, possibly in addition to the root file system specified
249 on the kernel command line via '--root'. MAPPED-DEVICES is a list of device
250 mappings to realize before FILE-SYSTEMS are mounted.
251
252 QEMU-NETWORKING? and VOLATILE-ROOT? behaves as in raw-initrd.
253
254 When VIRTIO? is true, load additional modules so the initrd can
255 be used as a QEMU guest with the root file system on a para-virtualized block
256 device.
257
258 The initrd is automatically populated with all the kernel modules necessary
259 for FILE-SYSTEMS and for the given options. However, additional kernel
260 modules can be listed in EXTRA-MODULES. They will be added to the initrd, and
261 loaded at boot time in the order in which they appear."
262 (define virtio-modules
263 ;; Modules for Linux para-virtualized devices, for use in QEMU guests.
264 '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
265 "virtio_console"))
266
267 (define cifs-modules
268 ;; Modules needed to mount CIFS file systems.
269 '("md4" "ecb" "cifs"))
270
271 (define virtio-9p-modules
272 ;; Modules for the 9p paravirtualized file system.
273 '("9p" "9pnet_virtio"))
274
275 (define (file-system-type-predicate type)
276 (lambda (fs)
277 (string=? (file-system-type fs) type)))
278
279 (define linux-modules
280 ;; Modules added to the initrd and loaded from the initrd.
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 "nvme" ;for new SSD NVMe devices
286 "nls_iso8859-1" ;for `mkfs.fat`, et.al
287 ,@(if (string-match "^(x86_64|i[3-6]86)-" (%current-system))
288 '("pata_acpi" "pata_atiixp" ;for ATA controllers
289 "isci") ;for SAS controllers like Intel C602
290 '())
291 ,@(if (or virtio? qemu-networking?)
292 virtio-modules
293 '())
294 ,@(if (find (file-system-type-predicate "cifs") file-systems)
295 cifs-modules
296 '())
297 ,@(if (find (file-system-type-predicate "9p") file-systems)
298 virtio-9p-modules
299 '())
300 ,@(if (find (file-system-type-predicate "btrfs") file-systems)
301 '("btrfs")
302 '())
303 ,@(if (find (file-system-type-predicate "iso9660") file-systems)
304 '("isofs")
305 '())
306 ,@(if volatile-root?
307 '("fuse")
308 '())
309 ,@extra-modules))
310
311 (define helper-packages
312 (file-system-packages file-systems #:volatile-root? volatile-root?))
313
314 (raw-initrd file-systems
315 #:linux linux
316 #:linux-modules linux-modules
317 #:mapped-devices mapped-devices
318 #:helper-packages helper-packages
319 #:qemu-networking? qemu-networking?
320 #:volatile-root? volatile-root?))
321
322 ;;; linux-initrd.scm ends here