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