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