linux-boot: Load modules and their dependencies, à la 'modprobe'.
[jackhill/guix/guix.git] / gnu / system / linux-initrd.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
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
19 (define-module (gnu system linux-initrd)
20 #:use-module (guix monads)
21 #:use-module (guix gexp)
22 #:use-module (guix utils)
23 #:use-module ((guix store)
24 #:select (%store-prefix))
25 #:use-module ((guix derivations)
26 #:select (derivation->output-path))
27 #:use-module (gnu packages cpio)
28 #:use-module (gnu packages compression)
29 #:use-module (gnu packages linux)
30 #:use-module (gnu packages guile)
31 #:use-module ((gnu packages make-bootstrap)
32 #:select (%guile-static-stripped))
33 #:use-module (gnu system file-systems)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 regex)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-26)
38 #:export (expression->initrd
39 base-initrd))
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))
57 (modules '()))
58 "Return a derivation that builds a Linux initrd (a gzipped cpio archive)
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.
61
62 MODULES is a list of Guile module names to be embedded in the initrd."
63
64 ;; General Linux overview in `Documentation/early-userspace/README' and
65 ;; `Documentation/filesystems/ramfs-rootfs-initramfs.txt'.
66
67 (mlet %store-monad ((init (gexp->script "init" exp
68 #:modules modules
69 #:guile guile)))
70 (define builder
71 #~(begin
72 (use-modules (gnu build linux-initrd))
73
74 (mkdir #$output)
75 (build-initrd (string-append #$output "/initrd")
76 #:guile #$guile
77 #:init #$init
78 ;; Copy everything INIT refers to into the initrd.
79 #:references-graphs '("closure")
80 #:cpio (string-append #$cpio "/bin/cpio")
81 #:gzip (string-append #$gzip "/bin/gzip"))))
82
83 (gexp->derivation name builder
84 #:modules '((guix build utils)
85 (guix build store-copy)
86 (gnu build linux-initrd))
87 #:references-graphs `(("closure" ,init)))))
88
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)
95 (srfi srfi-1)
96 (guix build utils)
97 (gnu build linux-modules))
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
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
123 (mkdir #$output)
124 (for-each (lambda (module)
125 (format #t "copying '~a'...~%" module)
126 (copy-file module
127 (string-append #$output "/"
128 (basename module))))
129 (delete-duplicates modules))))
130
131 (gexp->derivation "linux-modules" build-exp
132 #:modules '((guix build utils)
133 (guix elf)
134 (gnu build linux-modules))))
135
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
140 (($ <file-system> device title mount-point type flags options _ check?)
141 (list device title mount-point type flags options check?))))
142
143 (define* (base-initrd file-systems
144 #:key
145 (mapped-devices '())
146 qemu-networking?
147 virtio?
148 volatile-root?
149 (extra-modules '()))
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'.
153 MAPPED-DEVICES is a list of device mappings to realize before FILE-SYSTEMS are
154 mounted.
155
156 When QEMU-NETWORKING? is true, set up networking with the standard QEMU
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.
159
160 When VOLATILE-ROOT? is true, the root file system is writable but any changes
161 to it are lost.
162
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
166 loaded at boot time in the order in which they appear."
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
172 (define cifs-modules
173 ;; Modules needed to mount CIFS file systems.
174 '("md4.ko" "ecb.ko" "cifs.ko"))
175
176 (define virtio-9p-modules
177 ;; Modules for the 9p paravirtualized file system.
178 '("fscache.ko" "9pnet.ko" "9p.ko" "9pnet_virtio.ko"))
179
180 (define (file-system-type-predicate type)
181 (lambda (fs)
182 (string=? (file-system-type fs) type)))
183
184 (define linux-modules
185 ;; Modules added to the initrd and loaded from the initrd.
186 `("libahci.ko" "ahci.ko" ;for SATA controllers
187 "pata_acpi.ko" "pata_atiixp.ko" ;for ATA controllers
188 ,@(if (or virtio? qemu-networking?)
189 virtio-modules
190 '())
191 ,@(if (find (file-system-type-predicate "cifs") file-systems)
192 cifs-modules
193 '())
194 ,@(if (find (file-system-type-predicate "9p") file-systems)
195 virtio-9p-modules
196 '())
197 ,@(if volatile-root?
198 '("fuse.ko")
199 '())
200 ,@extra-modules))
201
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
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
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)
237 #:pre-mount (lambda ()
238 (and #$@device-mapping-commands))
239 #:linux-modules '#$linux-modules
240 #:linux-module-directory '#$kodir
241 #:qemu-guest-networking? #$qemu-networking?
242 #:volatile-root? '#$volatile-root?))
243 #:name "base-initrd"
244 #:modules '((guix build utils)
245 (gnu build linux-boot)
246 (gnu build linux-modules)
247 (gnu build file-systems)
248 (guix elf)))))
249
250 ;;; linux-initrd.scm ends here