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