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