Commit | Line | Data |
---|---|---|
f09d925b | 1 | ;;; GNU Guix --- Functional package management for GNU |
b0dd47a8 | 2 | ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org> |
f09d925b LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
735c6dd7 LC |
19 | (define-module (gnu system linux-initrd) |
20 | #:use-module (guix monads) | |
0c21d92b | 21 | #:use-module (guix gexp) |
f09d925b | 22 | #:use-module (guix utils) |
d4254711 LC |
23 | #:use-module ((guix store) |
24 | #:select (%store-prefix)) | |
1c96c1bb LC |
25 | #:use-module ((guix derivations) |
26 | #:select (derivation->output-path)) | |
f09d925b LC |
27 | #:use-module (gnu packages cpio) |
28 | #:use-module (gnu packages compression) | |
29 | #:use-module (gnu packages linux) | |
f989fa39 | 30 | #:use-module (gnu packages guile) |
f09d925b LC |
31 | #:use-module ((gnu packages make-bootstrap) |
32 | #:select (%guile-static-stripped)) | |
c5df1839 | 33 | #:use-module (gnu system file-systems) |
1c96c1bb | 34 | #:use-module (ice-9 match) |
217b862f | 35 | #:use-module (ice-9 regex) |
83bcd0b8 | 36 | #:use-module (srfi srfi-1) |
42d10464 | 37 | #:use-module (srfi srfi-26) |
735c6dd7 | 38 | #:export (expression->initrd |
060238ae | 39 | base-initrd)) |
f09d925b LC |
40 | |
41 | \f | |
42 | ;;; Commentary: | |
43 | ;;; | |
44 | ;;; Tools to build initial RAM disks (initrd's) for Linux-Libre, and in | |
45 | ;;; particular initrd's that run Guile. | |
46 | ;;; | |
47 | ;;; Code: | |
48 | ||
49 | ||
50 | (define* (expression->initrd exp | |
51 | #:key | |
52 | (guile %guile-static-stripped) | |
53 | (cpio cpio) | |
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 |
59 | containing GUILE and that evaluates EXP, a G-expression, upon booting. All |
60 | the derivations referenced by EXP are automatically copied to the initrd. | |
fd1b1fa2 | 61 | |
42d10464 | 62 | MODULES 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 LC |
80 | #:cpio (string-append #$cpio "/bin/cpio") |
81 | #:gzip (string-append #$gzip "/bin/gzip")))) | |
f09d925b | 82 | |
0c21d92b | 83 | (gexp->derivation name builder |
fbb35558 | 84 | #:modules '((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 | |
91 | MODULES 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 | |
83bcd0b8 LC |
136 | (define (file-system->spec fs) |
137 | "Return a list corresponding to file-system FS that can be passed to the | |
138 | initrd code." | |
139 | (match fs | |
d4c87617 LC |
140 | (($ <file-system> device title mount-point type flags options _ check?) |
141 | (list device title mount-point type flags options check?)))) | |
83bcd0b8 | 142 | |
060238ae | 143 | (define* (base-initrd file-systems |
83bcd0b8 | 144 | #:key |
de1c158f | 145 | (mapped-devices '()) |
4fc96187 | 146 | qemu-networking? |
24e0160a LC |
147 | virtio? |
148 | volatile-root? | |
6c1df081 | 149 | (extra-modules '())) |
060238ae LC |
150 | "Return a monadic derivation that builds a generic initrd. FILE-SYSTEMS is |
151 | a list of file-systems to be mounted by the initrd, possibly in addition to | |
152 | the root file system specified on the kernel command line via '--root'. | |
de1c158f LC |
153 | MAPPED-DEVICES is a list of device mappings to realize before FILE-SYSTEMS are |
154 | mounted. | |
f09d925b | 155 | |
112440a7 | 156 | When QEMU-NETWORKING? is true, set up networking with the standard QEMU |
24e0160a LC |
157 | parameters. When VIRTIO? is true, load additional modules so the initrd can |
158 | be used as a QEMU guest with para-virtualized I/O drivers. | |
112440a7 | 159 | |
83bcd0b8 LC |
160 | When VOLATILE-ROOT? is true, the root file system is writable but any changes |
161 | to it are lost. | |
b48d21b2 | 162 | |
fa16f845 LC |
163 | The initrd is automatically populated with all the kernel modules necessary |
164 | for FILE-SYSTEMS and for the given options. However, additional kernel | |
165 | modules can be listed in EXTRA-MODULES. They will be added to the initrd, and | |
6c1df081 | 166 | loaded at boot time in the order in which they appear." |
24e0160a LC |
167 | (define virtio-modules |
168 | ;; Modules for Linux para-virtualized devices, for use in QEMU guests. | |
169 | '("virtio.ko" "virtio_ring.ko" "virtio_pci.ko" | |
170 | "virtio_balloon.ko" "virtio_blk.ko" "virtio_net.ko")) | |
171 | ||
d4254711 LC |
172 | (define cifs-modules |
173 | ;; Modules needed to mount CIFS file systems. | |
174 | '("md4.ko" "ecb.ko" "cifs.ko")) | |
88840f02 | 175 | |
4919d684 LC |
176 | (define virtio-9p-modules |
177 | ;; Modules for the 9p paravirtualized file system. | |
6f22f3c9 | 178 | '("fscache.ko" "9pnet.ko" "9p.ko" "9pnet_virtio.ko")) |
4919d684 | 179 | |
83bcd0b8 LC |
180 | (define (file-system-type-predicate type) |
181 | (lambda (fs) | |
182 | (string=? (file-system-type fs) type))) | |
183 | ||
d4254711 LC |
184 | (define linux-modules |
185 | ;; Modules added to the initrd and loaded from the initrd. | |
ceb6b4c2 LC |
186 | `("libahci.ko" "ahci.ko" ;for SATA controllers |
187 | "pata_acpi.ko" "pata_atiixp.ko" ;for ATA controllers | |
c299dffc | 188 | ,@(if (or virtio? qemu-networking?) |
24e0160a LC |
189 | virtio-modules |
190 | '()) | |
83bcd0b8 | 191 | ,@(if (find (file-system-type-predicate "cifs") file-systems) |
4919d684 LC |
192 | cifs-modules |
193 | '()) | |
83bcd0b8 | 194 | ,@(if (find (file-system-type-predicate "9p") file-systems) |
4919d684 | 195 | virtio-9p-modules |
1c96c1bb LC |
196 | '()) |
197 | ,@(if volatile-root? | |
198 | '("fuse.ko") | |
fa16f845 LC |
199 | '()) |
200 | ,@extra-modules)) | |
f09d925b | 201 | |
3c05b4bc LC |
202 | (define helper-packages |
203 | ;; Packages to be copied on the initrd. | |
204 | `(,@(if (find (lambda (fs) | |
205 | (string-prefix? "ext" (file-system-type fs))) | |
206 | file-systems) | |
207 | (list e2fsck/static) | |
208 | '()) | |
209 | ,@(if volatile-root? | |
210 | (list unionfs-fuse/static) | |
211 | '()))) | |
212 | ||
de1c158f LC |
213 | (define device-mapping-commands |
214 | ;; List of gexps to open the mapped devices. | |
215 | (map (lambda (md) | |
216 | (let* ((source (mapped-device-source md)) | |
217 | (target (mapped-device-target md)) | |
218 | (type (mapped-device-type md)) | |
219 | (open (mapped-device-kind-open type))) | |
220 | (open source target))) | |
221 | mapped-devices)) | |
222 | ||
42d10464 LC |
223 | (mlet %store-monad ((kodir (flat-linux-module-directory linux-libre |
224 | linux-modules))) | |
225 | (expression->initrd | |
226 | #~(begin | |
227 | (use-modules (gnu build linux-boot) | |
228 | (guix build utils) | |
229 | (srfi srfi-26)) | |
230 | ||
231 | (with-output-to-port (%make-void-port "w") | |
232 | (lambda () | |
233 | (set-path-environment-variable "PATH" '("bin" "sbin") | |
234 | '#$helper-packages))) | |
235 | ||
236 | (boot-system #:mounts '#$(map file-system->spec file-systems) | |
de1c158f LC |
237 | #:pre-mount (lambda () |
238 | (and #$@device-mapping-commands)) | |
0e704a2d LC |
239 | #:linux-modules '#$linux-modules |
240 | #:linux-module-directory '#$kodir | |
42d10464 | 241 | #:qemu-guest-networking? #$qemu-networking? |
42d10464 LC |
242 | #:volatile-root? '#$volatile-root?)) |
243 | #:name "base-initrd" | |
244 | #:modules '((guix build utils) | |
245 | (gnu build linux-boot) | |
0e704a2d LC |
246 | (gnu build linux-modules) |
247 | (gnu build file-systems) | |
248 | (guix elf))))) | |
f09d925b LC |
249 | |
250 | ;;; linux-initrd.scm ends here |