| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 |
|
| 23 | (define-module (gnu system linux-initrd)
|
| 24 | #:use-module (guix gexp)
|
| 25 | #:use-module (guix utils)
|
| 26 | #:use-module ((guix store)
|
| 27 | #:select (%store-prefix))
|
| 28 | #:use-module ((guix derivations)
|
| 29 | #:select (derivation->output-path))
|
| 30 | #:use-module (guix modules)
|
| 31 | #:use-module (gnu packages compression)
|
| 32 | #:use-module (gnu packages disk)
|
| 33 | #:use-module (gnu packages linux)
|
| 34 | #:use-module (gnu packages file-systems)
|
| 35 | #:use-module (gnu packages guile)
|
| 36 | #:use-module ((gnu packages xorg)
|
| 37 | #:select (console-setup xkeyboard-config))
|
| 38 | #:use-module ((gnu packages make-bootstrap)
|
| 39 | #:select (%guile-3.0-static-stripped))
|
| 40 | #:use-module (gnu system file-systems)
|
| 41 | #:use-module (gnu system mapped-devices)
|
| 42 | #:use-module (gnu system keyboard)
|
| 43 | #:use-module (ice-9 match)
|
| 44 | #:use-module (ice-9 regex)
|
| 45 | #:use-module (ice-9 vlist)
|
| 46 | #:use-module (srfi srfi-1)
|
| 47 | #:use-module (srfi srfi-26)
|
| 48 | #:export (expression->initrd
|
| 49 | %base-initrd-modules
|
| 50 | raw-initrd
|
| 51 | file-system-packages
|
| 52 | base-initrd))
|
| 53 |
|
| 54 |
|
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 |
|
| 62 |
|
| 63 | (define* (expression->initrd exp
|
| 64 | #:key
|
| 65 | (guile %guile-3.0-static-stripped)
|
| 66 | (gzip gzip)
|
| 67 | (name "guile-initrd")
|
| 68 | (system (%current-system)))
|
| 69 | "Return as a file-like object a Linux initrd (a gzipped cpio archive)
|
| 70 | containing GUILE and that evaluates EXP, a G-expression, upon booting. All
|
| 71 | the derivations referenced by EXP are automatically copied to the initrd."
|
| 72 |
|
| 73 | |
| 74 | |
| 75 |
|
| 76 | (define init
|
| 77 | (program-file "init" exp #:guile guile))
|
| 78 |
|
| 79 | (define (import-module? module)
|
| 80 | |
| 81 | |
| 82 | |
| 83 | (and (guix-module-name? module)
|
| 84 | (not (equal? module '(guix store deduplication)))))
|
| 85 |
|
| 86 | (define builder
|
| 87 | |
| 88 | |
| 89 | |
| 90 | (with-imported-modules (source-module-closure
|
| 91 | '((gnu build linux-initrd))
|
| 92 | #:select? import-module?)
|
| 93 | #~(begin
|
| 94 | (use-modules (gnu build linux-initrd))
|
| 95 |
|
| 96 | (mkdir #$output)
|
| 97 |
|
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | (call-with-output-file (string-append #$ output "/references")
|
| 107 | (lambda (port)
|
| 108 | (simple-format port "~A\n" #$guile)))
|
| 109 |
|
| 110 | (build-initrd (string-append #$output "/initrd.cpio.gz")
|
| 111 | #:guile #$guile
|
| 112 | #:init #$init
|
| 113 | |
| 114 | #:references-graphs '("closure")
|
| 115 | #:gzip (string-append #+gzip "/bin/gzip")))))
|
| 116 |
|
| 117 | (file-append (computed-file name builder
|
| 118 | #:options
|
| 119 | `(#:references-graphs (("closure" ,init))))
|
| 120 | "/initrd.cpio.gz"))
|
| 121 |
|
| 122 | (define (flat-linux-module-directory linux modules)
|
| 123 | "Return a flat directory containing the Linux kernel modules listed in
|
| 124 | MODULES and taken from LINUX."
|
| 125 | (define imported-modules
|
| 126 | (source-module-closure '((gnu build linux-modules)
|
| 127 | (guix build utils))))
|
| 128 |
|
| 129 | (define build-exp
|
| 130 | (with-imported-modules imported-modules
|
| 131 | (with-extensions (list guile-zlib)
|
| 132 | #~(begin
|
| 133 | (use-modules (gnu build linux-modules)
|
| 134 | (guix build utils)
|
| 135 | (srfi srfi-1)
|
| 136 | (srfi srfi-26))
|
| 137 |
|
| 138 | (define module-dir
|
| 139 | (string-append #$linux "/lib/modules"))
|
| 140 |
|
| 141 | (define modules
|
| 142 | (let* ((lookup (cut find-module-file module-dir <>))
|
| 143 | (modules (map lookup '#$modules)))
|
| 144 | (append modules
|
| 145 | (recursive-module-dependencies
|
| 146 | modules
|
| 147 | #:lookup-module lookup))))
|
| 148 |
|
| 149 | (define (maybe-uncompress file)
|
| 150 | |
| 151 | |
| 152 | (cond
|
| 153 | ((string-contains file ".ko.gz")
|
| 154 | (invoke #+(file-append gzip "/bin/gunzip") file))))
|
| 155 |
|
| 156 | (mkdir #$output)
|
| 157 | (for-each (lambda (module)
|
| 158 | (let ((out-module
|
| 159 | (string-append #$output "/"
|
| 160 | (basename module))))
|
| 161 | (format #t "copying '~a'...~%" module)
|
| 162 | (copy-file module out-module)
|
| 163 | (maybe-uncompress out-module)))
|
| 164 | (delete-duplicates modules))
|
| 165 |
|
| 166 | |
| 167 | (write-module-name-database #$output)))))
|
| 168 |
|
| 169 | (computed-file "linux-modules" build-exp))
|
| 170 |
|
| 171 | (define* (raw-initrd file-systems
|
| 172 | #:key
|
| 173 | (linux linux-libre)
|
| 174 | (linux-modules '())
|
| 175 | (mapped-devices '())
|
| 176 | (keyboard-layout #f)
|
| 177 | (helper-packages '())
|
| 178 | qemu-networking?
|
| 179 | volatile-root?
|
| 180 | (on-error 'debug))
|
| 181 | "Return as a file-like object a raw initrd, with kernel
|
| 182 | modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
|
| 183 | mounted by the initrd, possibly in addition to the root file system specified
|
| 184 | on the kernel command line via '--root'. LINUX-MODULES is a list of kernel
|
| 185 | modules to be loaded at boot time. MAPPED-DEVICES is a list of device
|
| 186 | mappings to realize before FILE-SYSTEMS are mounted.
|
| 187 | HELPER-PACKAGES is a list of packages to be copied in the initrd. It may include
|
| 188 | e2fsck/static or other packages needed by the initrd to check root partition.
|
| 189 |
|
| 190 | When true, KEYBOARD-LAYOUT is a <keyboard-layout> record denoting the desired
|
| 191 | console keyboard layout. This is done before MAPPED-DEVICES are set up and
|
| 192 | before FILE-SYSTEMS are mounted such that, should the user need to enter a
|
| 193 | passphrase or use the REPL, this happens using the intended keyboard layout.
|
| 194 |
|
| 195 | When QEMU-NETWORKING? is true, set up networking with the standard QEMU
|
| 196 | parameters.
|
| 197 |
|
| 198 | When VOLATILE-ROOT? is true, the root file system is writable but any changes
|
| 199 | to it are lost.
|
| 200 |
|
| 201 | ON-ERROR is passed to 'call-with-error-handling'; it determines what happens
|
| 202 | upon error."
|
| 203 | (define device-mapping-commands
|
| 204 | |
| 205 | (map (lambda (md)
|
| 206 | (let* ((source (mapped-device-source md))
|
| 207 | (targets (mapped-device-targets md))
|
| 208 | (type (mapped-device-type md))
|
| 209 | (open (mapped-device-kind-open type)))
|
| 210 | (open source targets)))
|
| 211 | mapped-devices))
|
| 212 |
|
| 213 | (define kodir
|
| 214 | (flat-linux-module-directory linux linux-modules))
|
| 215 |
|
| 216 | (expression->initrd
|
| 217 | (with-imported-modules (source-module-closure
|
| 218 | '((gnu build linux-boot)
|
| 219 | (guix build utils)
|
| 220 | (guix build bournish)
|
| 221 | (gnu system file-systems)
|
| 222 | (gnu build file-systems)))
|
| 223 | #~(begin
|
| 224 | (use-modules (gnu build linux-boot)
|
| 225 | (gnu system file-systems)
|
| 226 | ((guix build utils) #:hide (delete))
|
| 227 | (guix build bournish) |
| 228 | (srfi srfi-1) |
| 229 | (srfi srfi-26)
|
| 230 |
|
| 231 | |
| 232 | |
| 233 | |
| 234 | ((gnu build file-systems)
|
| 235 | #:select (find-partition-by-luks-uuid))
|
| 236 | (rnrs bytevectors))
|
| 237 |
|
| 238 | (with-output-to-port (%make-void-port "w")
|
| 239 | (lambda ()
|
| 240 | (set-path-environment-variable "PATH" '("bin" "sbin")
|
| 241 | '#$helper-packages)))
|
| 242 |
|
| 243 | (parameterize ((current-warning-port (%make-void-port "w")))
|
| 244 | (boot-system #:mounts
|
| 245 | (map spec->file-system
|
| 246 | '#$(map file-system->spec file-systems))
|
| 247 | #:pre-mount (lambda ()
|
| 248 | (and #$@device-mapping-commands))
|
| 249 | #:linux-modules '#$linux-modules
|
| 250 | #:linux-module-directory '#$kodir
|
| 251 | #:keymap-file #+(and=> keyboard-layout
|
| 252 | keyboard-layout->console-keymap)
|
| 253 | #:qemu-guest-networking? #$qemu-networking?
|
| 254 | #:volatile-root? '#$volatile-root?
|
| 255 | #:on-error '#$on-error))))
|
| 256 | #:name "raw-initrd"))
|
| 257 |
|
| 258 | (define* (file-system-packages file-systems #:key (volatile-root? #f))
|
| 259 | "Return the list of statically-linked, stripped packages to check
|
| 260 | FILE-SYSTEMS."
|
| 261 | `(,@(if (find (lambda (fs)
|
| 262 | (string-prefix? "ext" (file-system-type fs)))
|
| 263 | file-systems)
|
| 264 | (list e2fsck/static)
|
| 265 | '())
|
| 266 | ,@(if (find (lambda (fs)
|
| 267 | (string-suffix? "fat" (file-system-type fs)))
|
| 268 | file-systems)
|
| 269 | (list fatfsck/static)
|
| 270 | '())
|
| 271 | ,@(if (find (file-system-type-predicate "bcachefs") file-systems)
|
| 272 | (list bcachefs-tools/static)
|
| 273 | '())
|
| 274 | ,@(if (find (file-system-type-predicate "btrfs") file-systems)
|
| 275 | (list btrfs-progs/static)
|
| 276 | '())
|
| 277 | ,@(if (find (file-system-type-predicate "jfs") file-systems)
|
| 278 | (list jfs_fsck/static)
|
| 279 | '())
|
| 280 | ,@(if (find (file-system-type-predicate "f2fs") file-systems)
|
| 281 | (list f2fs-fsck/static)
|
| 282 | '())))
|
| 283 |
|
| 284 | (define-syntax vhash |
| 285 | (syntax-rules (=>)
|
| 286 | "Build a vhash with the given key/value mappings."
|
| 287 | ((_)
|
| 288 | vlist-null)
|
| 289 | ((_ (key others ... => value) rest ...)
|
| 290 | (vhash-cons key value
|
| 291 | (vhash (others ... => value) rest ...)))
|
| 292 | ((_ (=> value) rest ...)
|
| 293 | (vhash rest ...))))
|
| 294 |
|
| 295 | (define-syntax lookup-procedure
|
| 296 | (syntax-rules (else)
|
| 297 | "Return a procedure that lookups keys in the given dictionary."
|
| 298 | ((_ mapping ... (else default))
|
| 299 | (let ((table (vhash mapping ...)))
|
| 300 | (lambda (key)
|
| 301 | (match (vhash-assoc key table)
|
| 302 | (#f default)
|
| 303 | ((key . value) value)))))))
|
| 304 |
|
| 305 | (define file-system-type-modules
|
| 306 | |
| 307 | (lookup-procedure ("cifs" => '("md4" "ecb" "cifs"))
|
| 308 | ("9p" => '("9p" "9pnet_virtio"))
|
| 309 | ("bcachefs" => '("bcachefs"))
|
| 310 | ("btrfs" => '("btrfs"))
|
| 311 | ("iso9660" => '("isofs"))
|
| 312 | ("jfs" => '("jfs"))
|
| 313 | ("f2fs" => '("f2fs" "crc32_generic"))
|
| 314 | (else '())))
|
| 315 |
|
| 316 | (define (file-system-modules file-systems)
|
| 317 | "Return the list of Linux modules needed to mount FILE-SYSTEMS."
|
| 318 | (append-map (compose file-system-type-modules file-system-type)
|
| 319 | file-systems))
|
| 320 |
|
| 321 | (define* (default-initrd-modules
|
| 322 | #:optional
|
| 323 | (system (or (%current-target-system)
|
| 324 | (%current-system))))
|
| 325 | "Return the list of modules included in the initrd by default."
|
| 326 | (define virtio-modules
|
| 327 | |
| 328 | '("virtio_pci" "virtio_balloon" "virtio_blk" "virtio_net"
|
| 329 | "virtio_console" "virtio-rng"))
|
| 330 |
|
| 331 | `("ahci" |
| 332 | "usb-storage" "uas" |
| 333 | "usbhid" "hid-generic" "hid-apple" |
| 334 | "dm-crypt" "xts" "serpent_generic" "wp512" |
| 335 | "nls_iso8859-1" |
| 336 | ,@(if (string-match "^(x86_64|i[3-6]86)-" system)
|
| 337 | '("pata_acpi" "pata_atiixp" |
| 338 | "isci") |
| 339 | '())
|
| 340 |
|
| 341 | ,@virtio-modules))
|
| 342 |
|
| 343 | (define-syntax %base-initrd-modules
|
| 344 | |
| 345 | (identifier-syntax (default-initrd-modules)))
|
| 346 |
|
| 347 | (define* (base-initrd file-systems
|
| 348 | #:key
|
| 349 | (linux linux-libre)
|
| 350 | (linux-modules '())
|
| 351 | (mapped-devices '())
|
| 352 | (keyboard-layout #f)
|
| 353 | qemu-networking?
|
| 354 | volatile-root?
|
| 355 | (extra-modules '()) |
| 356 | (on-error 'debug))
|
| 357 | "Return as a file-like object a generic initrd, with kernel
|
| 358 | modules taken from LINUX. FILE-SYSTEMS is a list of file-systems to be
|
| 359 | mounted by the initrd, possibly in addition to the root file system specified
|
| 360 | on the kernel command line via '--root'. MAPPED-DEVICES is a list of device
|
| 361 | mappings to realize before FILE-SYSTEMS are mounted.
|
| 362 |
|
| 363 | When true, KEYBOARD-LAYOUT is a <keyboard-layout> record denoting the desired
|
| 364 | console keyboard layout. This is done before MAPPED-DEVICES are set up and
|
| 365 | before FILE-SYSTEMS are mounted such that, should the user need to enter a
|
| 366 | passphrase or use the REPL, this happens using the intended keyboard layout.
|
| 367 |
|
| 368 | QEMU-NETWORKING? and VOLATILE-ROOT? behaves as in raw-initrd.
|
| 369 |
|
| 370 | The initrd is automatically populated with all the kernel modules necessary
|
| 371 | for FILE-SYSTEMS and for the given options. Additional kernel
|
| 372 | modules can be listed in LINUX-MODULES. They will be added to the initrd, and
|
| 373 | loaded at boot time in the order in which they appear."
|
| 374 | (define linux-modules*
|
| 375 | |
| 376 | `(,@linux-modules
|
| 377 | ,@(file-system-modules file-systems)
|
| 378 | ,@(if volatile-root?
|
| 379 | '("overlay")
|
| 380 | '())
|
| 381 | ,@extra-modules))
|
| 382 |
|
| 383 | (define helper-packages
|
| 384 | (append (file-system-packages file-systems
|
| 385 | #:volatile-root? volatile-root?)
|
| 386 | (if keyboard-layout
|
| 387 | (list loadkeys-static)
|
| 388 | '())))
|
| 389 |
|
| 390 | (raw-initrd file-systems
|
| 391 | #:linux linux
|
| 392 | #:linux-modules linux-modules*
|
| 393 | #:mapped-devices mapped-devices
|
| 394 | #:helper-packages helper-packages
|
| 395 | #:keyboard-layout keyboard-layout
|
| 396 | #:qemu-networking? qemu-networking?
|
| 397 | #:volatile-root? volatile-root?
|
| 398 | #:on-error on-error))
|
| 399 |
|
| 400 | |