linux-initrd: Use pata_acpi, pata_atiixp, and isci modules only on Intel.
[jackhill/guix/guix.git] / gnu / system / linux-initrd.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 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 store)
22 #:use-module (guix gexp)
23 #:use-module (guix utils)
24 #:use-module ((guix store)
25 #:select (%store-prefix))
26 #:use-module ((guix derivations)
27 #:select (derivation->output-path))
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 (gzip gzip)
54 (name "guile-initrd")
55 (system (%current-system))
56 (modules '()))
57 "Return a derivation that builds a Linux initrd (a gzipped cpio archive)
58 containing GUILE and that evaluates EXP, a G-expression, upon booting. All
59 the derivations referenced by EXP are automatically copied to the initrd.
60
61 MODULES is a list of Guile module names to be embedded in the initrd."
62
63 ;; General Linux overview in `Documentation/early-userspace/README' and
64 ;; `Documentation/filesystems/ramfs-rootfs-initramfs.txt'.
65
66 (mlet %store-monad ((init (gexp->script "init" exp
67 #:modules modules
68 #:guile guile)))
69 (define builder
70 #~(begin
71 (use-modules (gnu build linux-initrd))
72
73 (mkdir #$output)
74 (build-initrd (string-append #$output "/initrd")
75 #:guile #$guile
76 #:init #$init
77 ;; Copy everything INIT refers to into the initrd.
78 #:references-graphs '("closure")
79 #:gzip (string-append #$gzip "/bin/gzip"))))
80
81 (gexp->derivation name builder
82 #:modules '((guix cpio)
83 (guix build utils)
84 (guix build store-copy)
85 (gnu build linux-initrd))
86 #:references-graphs `(("closure" ,init)))))
87
88 (define (flat-linux-module-directory linux modules)
89 "Return a flat directory containing the Linux kernel modules listed in
90 MODULES and taken from LINUX."
91 (define build-exp
92 #~(begin
93 (use-modules (ice-9 match) (ice-9 regex)
94 (srfi srfi-1)
95 (guix build utils)
96 (gnu build linux-modules))
97
98 (define (string->regexp str)
99 ;; Return a regexp that matches STR exactly.
100 (string-append "^" (regexp-quote str) "$"))
101
102 (define module-dir
103 (string-append #$linux "/lib/modules"))
104
105 (define (lookup module)
106 (let ((name (ensure-dot-ko module)))
107 (match (find-files module-dir (string->regexp name))
108 ((file)
109 file)
110 (()
111 (error "module not found" name module-dir))
112 ((_ ...)
113 (error "several modules by that name"
114 name module-dir)))))
115
116 (define modules
117 (let ((modules (map lookup '#$modules)))
118 (append modules
119 (recursive-module-dependencies modules
120 #:lookup-module lookup))))
121
122 (mkdir #$output)
123 (for-each (lambda (module)
124 (format #t "copying '~a'...~%" module)
125 (copy-file module
126 (string-append #$output "/"
127 (basename module))))
128 (delete-duplicates modules))))
129
130 (gexp->derivation "linux-modules" build-exp
131 #:modules '((guix build utils)
132 (guix elf)
133 (gnu build linux-modules))))
134
135 (define* (base-initrd file-systems
136 #:key
137 (linux linux-libre)
138 (mapped-devices '())
139 qemu-networking?
140 (virtio? #t)
141 volatile-root?
142 (extra-modules '()))
143 "Return a monadic derivation that builds a generic initrd, with kernel
144 modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
145 mounted by the initrd, possibly in addition to the root file system specified
146 on the kernel command line via '--root'. MAPPED-DEVICES is a list of device
147 mappings to realize before FILE-SYSTEMS are mounted.
148
149 When QEMU-NETWORKING? is true, set up networking with the standard QEMU
150 parameters. When VIRTIO? is true, load additional modules so the initrd can
151 be used as a QEMU guest with the root file system on a para-virtualized block
152 device.
153
154 When VOLATILE-ROOT? is true, the root file system is writable but any changes
155 to it are lost.
156
157 The initrd is automatically populated with all the kernel modules necessary
158 for FILE-SYSTEMS and for the given options. However, additional kernel
159 modules can be listed in EXTRA-MODULES. They will be added to the initrd, and
160 loaded at boot time in the order in which they appear."
161 (define virtio-modules
162 ;; Modules for Linux para-virtualized devices, for use in QEMU guests.
163 '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
164 "virtio_console"))
165
166 (define cifs-modules
167 ;; Modules needed to mount CIFS file systems.
168 '("md4" "ecb" "cifs"))
169
170 (define virtio-9p-modules
171 ;; Modules for the 9p paravirtualized file system.
172 '("9p" "9pnet_virtio"))
173
174 (define (file-system-type-predicate type)
175 (lambda (fs)
176 (string=? (file-system-type fs) type)))
177
178 (define linux-modules
179 ;; Modules added to the initrd and loaded from the initrd.
180 `("ahci" ;for SATA controllers
181 "usb-storage" "uas" ;for the installation image etc.
182 "usbkbd" "usbhid" ;USB keyboards, for debugging
183 "dm-crypt" "xts" ;for encrypted root partitions
184 ,@(if (string-match "^(x86_64|i[3-6]86)-" (%current-system))
185 '("pata_acpi" "pata_atiixp" ;for ATA controllers
186 "isci") ;for SAS controllers like Intel C602
187 '())
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")
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
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 (guix build syscalls)
246 (gnu build linux-boot)
247 (gnu build linux-modules)
248 (gnu build file-systems)
249 (guix elf)))))
250
251 ;;; linux-initrd.scm ends here