| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 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 |
|
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 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 | |
| 64 | |
| 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 | |
| 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 | |
| 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 | |
| 163 | '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
|
| 164 | "virtio_console"))
|
| 165 |
|
| 166 | (define cifs-modules
|
| 167 | |
| 168 | '("md4" "ecb" "cifs"))
|
| 169 |
|
| 170 | (define virtio-9p-modules
|
| 171 | |
| 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 | |
| 180 | `("ahci" |
| 181 | "pata_acpi" "pata_atiixp" |
| 182 | "isci" |
| 183 | "usb-storage" "uas" |
| 184 | "usbkbd" "usbhid" |
| 185 | ,@(if (or virtio? qemu-networking?)
|
| 186 | virtio-modules
|
| 187 | '())
|
| 188 | ,@(if (find (file-system-type-predicate "cifs") file-systems)
|
| 189 | cifs-modules
|
| 190 | '())
|
| 191 | ,@(if (find (file-system-type-predicate "9p") file-systems)
|
| 192 | virtio-9p-modules
|
| 193 | '())
|
| 194 | ,@(if volatile-root?
|
| 195 | '("fuse")
|
| 196 | '())
|
| 197 | ,@extra-modules))
|
| 198 |
|
| 199 | (define helper-packages
|
| 200 | |
| 201 | `(,@(if (find (lambda (fs)
|
| 202 | (string-prefix? "ext" (file-system-type fs)))
|
| 203 | file-systems)
|
| 204 | (list e2fsck/static)
|
| 205 | '())
|
| 206 | ,@(if volatile-root?
|
| 207 | (list unionfs-fuse/static)
|
| 208 | '())))
|
| 209 |
|
| 210 | (define device-mapping-commands
|
| 211 | |
| 212 | (map (lambda (md)
|
| 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)))
|
| 218 | mapped-devices))
|
| 219 |
|
| 220 | (mlet %store-monad ((kodir (flat-linux-module-directory linux
|
| 221 | linux-modules)))
|
| 222 | (expression->initrd
|
| 223 | #~(begin
|
| 224 | (use-modules (gnu build linux-boot)
|
| 225 | (guix build utils)
|
| 226 | (srfi srfi-26))
|
| 227 |
|
| 228 | (with-output-to-port (%make-void-port "w")
|
| 229 | (lambda ()
|
| 230 | (set-path-environment-variable "PATH" '("bin" "sbin")
|
| 231 | '#$helper-packages)))
|
| 232 |
|
| 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?))
|
| 240 | #:name "base-initrd"
|
| 241 | #:modules '((guix build utils)
|
| 242 | (guix build syscalls)
|
| 243 | (gnu build linux-boot)
|
| 244 | (gnu build linux-modules)
|
| 245 | (gnu build file-systems)
|
| 246 | (guix elf)))))
|
| 247 |
|
| 248 | |