gnu: Add python-pyusb.
[jackhill/guix/guix.git] / gnu / system / linux-initrd.scm
CommitLineData
f09d925b 1;;; GNU Guix --- Functional package management for GNU
b153f9f0 2;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
cc023e32 3;;; Copyright © 2016 Mark H Weaver <mhw@netris.org>
f09d925b
LC
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
735c6dd7
LC
20(define-module (gnu system linux-initrd)
21 #:use-module (guix monads)
e87f0591 22 #:use-module (guix store)
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))
f09d925b
LC
29 #:use-module (gnu packages compression)
30 #:use-module (gnu packages linux)
f989fa39 31 #:use-module (gnu packages guile)
f09d925b
LC
32 #:use-module ((gnu packages make-bootstrap)
33 #:select (%guile-static-stripped))
c5df1839 34 #:use-module (gnu system file-systems)
1c96c1bb 35 #:use-module (ice-9 match)
217b862f 36 #:use-module (ice-9 regex)
83bcd0b8 37 #:use-module (srfi srfi-1)
42d10464 38 #:use-module (srfi srfi-26)
735c6dd7 39 #:export (expression->initrd
060238ae 40 base-initrd))
f09d925b
LC
41
42\f
43;;; Commentary:
44;;;
45;;; Tools to build initial RAM disks (initrd's) for Linux-Libre, and in
46;;; particular initrd's that run Guile.
47;;;
48;;; Code:
49
50
51(define* (expression->initrd exp
52 #:key
53 (guile %guile-static-stripped)
f09d925b
LC
54 (gzip gzip)
55 (name "guile-initrd")
56 (system (%current-system))
42d10464 57 (modules '()))
fd1b1fa2 58 "Return a derivation that builds a Linux initrd (a gzipped cpio archive)
df650fa8
LC
59containing GUILE and that evaluates EXP, a G-expression, upon booting. All
60the derivations referenced by EXP are automatically copied to the initrd.
fd1b1fa2 61
42d10464 62MODULES is a list of Guile module names to be embedded in the initrd."
f09d925b
LC
63
64 ;; General Linux overview in `Documentation/early-userspace/README' and
65 ;; `Documentation/filesystems/ramfs-rootfs-initramfs.txt'.
66
42d10464
LC
67 (mlet %store-monad ((init (gexp->script "init" exp
68 #:modules modules
69 #:guile guile)))
0c21d92b 70 (define builder
0c21d92b 71 #~(begin
1621cf97 72 (use-modules (gnu build linux-initrd))
1c96c1bb 73
70608adb 74 (mkdir #$output)
1621cf97
LC
75 (build-initrd (string-append #$output "/initrd")
76 #:guile #$guile
77 #:init #$init
42d10464 78 ;; Copy everything INIT refers to into the initrd.
df650fa8 79 #:references-graphs '("closure")
1621cf97 80 #:gzip (string-append #$gzip "/bin/gzip"))))
f09d925b 81
0c21d92b 82 (gexp->derivation name builder
e8277f90
LC
83 #:modules '((guix cpio)
84 (guix build utils)
49fa9381
LC
85 (guix build store-copy)
86 (gnu build linux-initrd))
df650fa8 87 #:references-graphs `(("closure" ,init)))))
735c6dd7 88
b21a1c5a
LC
89(define (flat-linux-module-directory linux modules)
90 "Return a flat directory containing the Linux kernel modules listed in
91MODULES and taken from LINUX."
92 (define build-exp
93 #~(begin
94 (use-modules (ice-9 match) (ice-9 regex)
600c285b
LC
95 (srfi srfi-1)
96 (guix build utils)
97 (gnu build linux-modules))
b21a1c5a
LC
98
99 (define (string->regexp str)
100 ;; Return a regexp that matches STR exactly.
101 (string-append "^" (regexp-quote str) "$"))
102
103 (define module-dir
104 (string-append #$linux "/lib/modules"))
105
600c285b
LC
106 (define (lookup module)
107 (let ((name (ensure-dot-ko module)))
108 (match (find-files module-dir (string->regexp name))
109 ((file)
110 file)
111 (()
112 (error "module not found" name module-dir))
113 ((_ ...)
114 (error "several modules by that name"
115 name module-dir)))))
116
117 (define modules
118 (let ((modules (map lookup '#$modules)))
119 (append modules
120 (recursive-module-dependencies modules
121 #:lookup-module lookup))))
122
b21a1c5a
LC
123 (mkdir #$output)
124 (for-each (lambda (module)
600c285b
LC
125 (format #t "copying '~a'...~%" module)
126 (copy-file module
127 (string-append #$output "/"
128 (basename module))))
129 (delete-duplicates modules))))
b21a1c5a
LC
130
131 (gexp->derivation "linux-modules" build-exp
600c285b
LC
132 #:modules '((guix build utils)
133 (guix elf)
134 (gnu build linux-modules))))
b21a1c5a 135
060238ae 136(define* (base-initrd file-systems
83bcd0b8 137 #:key
0d275f4a 138 (linux linux-libre)
de1c158f 139 (mapped-devices '())
4fc96187 140 qemu-networking?
e26d5076 141 (virtio? #t)
24e0160a 142 volatile-root?
6c1df081 143 (extra-modules '()))
0d275f4a
AW
144 "Return a monadic derivation that builds a generic initrd, with kernel
145modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
146mounted by the initrd, possibly in addition to the root file system specified
147on the kernel command line via '--root'. MAPPED-DEVICES is a list of device
148mappings to realize before FILE-SYSTEMS are mounted.
f09d925b 149
112440a7 150When QEMU-NETWORKING? is true, set up networking with the standard QEMU
24e0160a 151parameters. When VIRTIO? is true, load additional modules so the initrd can
e26d5076
LC
152be used as a QEMU guest with the root file system on a para-virtualized block
153device.
112440a7 154
83bcd0b8
LC
155When VOLATILE-ROOT? is true, the root file system is writable but any changes
156to it are lost.
b48d21b2 157
fa16f845
LC
158The initrd is automatically populated with all the kernel modules necessary
159for FILE-SYSTEMS and for the given options. However, additional kernel
160modules can be listed in EXTRA-MODULES. They will be added to the initrd, and
6c1df081 161loaded at boot time in the order in which they appear."
24e0160a
LC
162 (define virtio-modules
163 ;; Modules for Linux para-virtualized devices, for use in QEMU guests.
a182e94e
LC
164 '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
165 "virtio_console"))
24e0160a 166
d4254711
LC
167 (define cifs-modules
168 ;; Modules needed to mount CIFS file systems.
08b1990a 169 '("md4" "ecb" "cifs"))
88840f02 170
4919d684
LC
171 (define virtio-9p-modules
172 ;; Modules for the 9p paravirtualized file system.
08b1990a 173 '("9p" "9pnet_virtio"))
4919d684 174
83bcd0b8
LC
175 (define (file-system-type-predicate type)
176 (lambda (fs)
177 (string=? (file-system-type fs) type)))
178
d4254711
LC
179 (define linux-modules
180 ;; Modules added to the initrd and loaded from the initrd.
493c245b 181 `("ahci" ;for SATA controllers
493c245b 182 "usb-storage" "uas" ;for the installation image etc.
cc023e32 183 "usbhid" "hid-generic" "hid-apple" ;keyboards during early boot
dfb9001a 184 "dm-crypt" "xts" "serpent_generic" "wp512" ;for encrypted root partitions
9f4a2496
MW
185 ,@(if (string-match "^(x86_64|i[3-6]86)-" (%current-system))
186 '("pata_acpi" "pata_atiixp" ;for ATA controllers
187 "isci") ;for SAS controllers like Intel C602
188 '())
c299dffc 189 ,@(if (or virtio? qemu-networking?)
24e0160a
LC
190 virtio-modules
191 '())
83bcd0b8 192 ,@(if (find (file-system-type-predicate "cifs") file-systems)
4919d684
LC
193 cifs-modules
194 '())
83bcd0b8 195 ,@(if (find (file-system-type-predicate "9p") file-systems)
4919d684 196 virtio-9p-modules
1c96c1bb
LC
197 '())
198 ,@(if volatile-root?
08b1990a 199 '("fuse")
fa16f845
LC
200 '())
201 ,@extra-modules))
f09d925b 202
3c05b4bc
LC
203 (define helper-packages
204 ;; Packages to be copied on the initrd.
205 `(,@(if (find (lambda (fs)
206 (string-prefix? "ext" (file-system-type fs)))
207 file-systems)
208 (list e2fsck/static)
209 '())
210 ,@(if volatile-root?
211 (list unionfs-fuse/static)
212 '())))
213
de1c158f
LC
214 (define device-mapping-commands
215 ;; List of gexps to open the mapped devices.
216 (map (lambda (md)
217 (let* ((source (mapped-device-source md))
218 (target (mapped-device-target md))
219 (type (mapped-device-type md))
220 (open (mapped-device-kind-open type)))
221 (open source target)))
222 mapped-devices))
223
0d275f4a 224 (mlet %store-monad ((kodir (flat-linux-module-directory linux
42d10464
LC
225 linux-modules)))
226 (expression->initrd
227 #~(begin
228 (use-modules (gnu build linux-boot)
229 (guix build utils)
f2e4805b 230 (guix build bournish) ;add the 'bournish' meta-command
42d10464
LC
231 (srfi srfi-26))
232
233 (with-output-to-port (%make-void-port "w")
234 (lambda ()
235 (set-path-environment-variable "PATH" '("bin" "sbin")
236 '#$helper-packages)))
237
238 (boot-system #:mounts '#$(map file-system->spec file-systems)
de1c158f
LC
239 #:pre-mount (lambda ()
240 (and #$@device-mapping-commands))
0e704a2d
LC
241 #:linux-modules '#$linux-modules
242 #:linux-module-directory '#$kodir
42d10464 243 #:qemu-guest-networking? #$qemu-networking?
42d10464
LC
244 #:volatile-root? '#$volatile-root?))
245 #:name "base-initrd"
f2e4805b
LC
246 #:modules '((guix build bournish)
247 (guix build utils)
1e49bcf9 248 (guix build syscalls)
42d10464 249 (gnu build linux-boot)
0e704a2d
LC
250 (gnu build linux-modules)
251 (gnu build file-systems)
252 (guix elf)))))
f09d925b
LC
253
254;;; linux-initrd.scm ends here