1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
44 ;;; Tools to build initial RAM disks (initrd's) for Linux-Libre, and in
45 ;;; particular initrd's that run Guile.
50 (define* (expression->initrd exp
52 (guile %guile-static-stripped)
55 (system (%current-system))
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.
61 MODULES is a list of Guile module names to be embedded in the initrd."
63 ;; General Linux overview in `Documentation/early-userspace/README' and
64 ;; `Documentation/filesystems/ramfs-rootfs-initramfs.txt'.
66 (mlet %store-monad ((init (gexp->script "init" exp
71 (use-modules (gnu build linux-initrd))
74 (build-initrd (string-append #$output "/initrd")
77 ;; Copy everything INIT refers to into the initrd.
78 #:references-graphs '("closure")
79 #:gzip (string-append #$gzip "/bin/gzip"))))
81 (gexp->derivation name builder
82 #:modules '((guix cpio)
84 (guix build store-copy)
85 (gnu build linux-initrd))
86 #:references-graphs `(("closure" ,init)))))
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."
93 (use-modules (ice-9 match) (ice-9 regex)
96 (gnu build linux-modules))
98 (define (string->regexp str)
99 ;; Return a regexp that matches STR exactly.
100 (string-append "^" (regexp-quote str) "$"))
103 (string-append #$linux "/lib/modules"))
105 (define (lookup module)
106 (let ((name (ensure-dot-ko module)))
107 (match (find-files module-dir (string->regexp name))
111 (error "module not found" name module-dir))
113 (error "several modules by that name"
117 (let ((modules (map lookup '#$modules)))
119 (recursive-module-dependencies modules
120 #:lookup-module lookup))))
123 (for-each (lambda (module)
124 (format #t "copying '~a'...~%" module)
126 (string-append #$output "/"
128 (delete-duplicates modules))))
130 (gexp->derivation "linux-modules" build-exp
131 #:modules '((guix build utils)
133 (gnu build linux-modules))))
135 (define* (base-initrd file-systems
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.
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
154 When VOLATILE-ROOT? is true, the root file system is writable but any changes
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"
167 ;; Modules needed to mount CIFS file systems.
168 '("md4" "ecb" "cifs"))
170 (define virtio-9p-modules
171 ;; Modules for the 9p paravirtualized file system.
172 '("9p" "9pnet_virtio"))
174 (define (file-system-type-predicate type)
176 (string=? (file-system-type fs) type)))
178 (define linux-modules
179 ;; Modules added to the initrd and loaded from the initrd.
180 `("ahci" ;for SATA controllers
181 "pata_acpi" "pata_atiixp" ;for ATA controllers
182 "isci" ;for SAS controllers like Intel C602
183 "usb-storage" "uas" ;for the installation image etc.
184 "usbkbd" "usbhid" ;USB keyboards, for debugging
185 ,@(if (or virtio? qemu-networking?)
188 ,@(if (find (file-system-type-predicate "cifs") file-systems)
191 ,@(if (find (file-system-type-predicate "9p") file-systems)
199 (define helper-packages
200 ;; Packages to be copied on the initrd.
201 `(,@(if (find (lambda (fs)
202 (string-prefix? "ext" (file-system-type fs)))
207 (list unionfs-fuse/static)
210 (define device-mapping-commands
211 ;; List of gexps to open the mapped devices.
213 (let* ((source (mapped-device-source md))
214 (target (mapped-device-target md))
215 (type (mapped-device-type md))
216 (open (mapped-device-kind-open type)))
217 (open source target)))
220 (mlet %store-monad ((kodir (flat-linux-module-directory linux
224 (use-modules (gnu build linux-boot)
228 (with-output-to-port (%make-void-port "w")
230 (set-path-environment-variable "PATH" '("bin" "sbin")
231 '#$helper-packages)))
233 (boot-system #:mounts '#$(map file-system->spec file-systems)
234 #:pre-mount (lambda ()
235 (and #$@device-mapping-commands))
236 #:linux-modules '#$linux-modules
237 #:linux-module-directory '#$kodir
238 #:qemu-guest-networking? #$qemu-networking?
239 #:volatile-root? '#$volatile-root?))
241 #:modules '((guix build utils)
242 (guix build syscalls)
243 (gnu build linux-boot)
244 (gnu build linux-modules)
245 (gnu build file-systems)
248 ;;; linux-initrd.scm ends here