| 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 build linux-boot)
|
| 24 | #:use-module (rnrs io ports)
|
| 25 | #:use-module (system repl error-handling)
|
| 26 | #:autoload (system repl repl) (start-repl)
|
| 27 | #:use-module (srfi srfi-1)
|
| 28 | #:use-module (srfi srfi-9)
|
| 29 | #:use-module (srfi srfi-11)
|
| 30 | #:use-module (srfi srfi-26)
|
| 31 | #:use-module (ice-9 match)
|
| 32 | #:use-module (ice-9 rdelim)
|
| 33 | #:use-module (ice-9 regex)
|
| 34 | #:use-module (ice-9 ftw)
|
| 35 | #:use-module (guix build utils)
|
| 36 | #:use-module ((guix build syscalls)
|
| 37 | #:hide (file-system-type))
|
| 38 | #:use-module (gnu build linux-modules)
|
| 39 | #:use-module (gnu build file-systems)
|
| 40 | #:use-module (gnu system file-systems)
|
| 41 | #:export (mount-essential-file-systems
|
| 42 | linux-command-line
|
| 43 | find-long-option
|
| 44 | find-long-options
|
| 45 | make-essential-device-nodes
|
| 46 | make-static-device-nodes
|
| 47 | configure-qemu-networking
|
| 48 |
|
| 49 | boot-system))
|
| 50 |
|
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 |
|
| 60 | (define* (mount-essential-file-systems #:key (root "/"))
|
| 61 | "Mount /dev, /proc, and /sys under ROOT."
|
| 62 | (define (scope dir)
|
| 63 | (string-append root
|
| 64 | (if (string-suffix? "/" root)
|
| 65 | ""
|
| 66 | "/")
|
| 67 | dir))
|
| 68 |
|
| 69 | (unless (file-exists? (scope "proc"))
|
| 70 | (mkdir (scope "proc")))
|
| 71 | (mount "none" (scope "proc") "proc")
|
| 72 |
|
| 73 | (unless (file-exists? (scope "dev"))
|
| 74 | (mkdir (scope "dev")))
|
| 75 | (mount "none" (scope "dev") "devtmpfs")
|
| 76 |
|
| 77 | (unless (file-exists? (scope "sys"))
|
| 78 | (mkdir (scope "sys")))
|
| 79 | (mount "none" (scope "sys") "sysfs"))
|
| 80 |
|
| 81 | (define (move-essential-file-systems root)
|
| 82 | "Move currently mounted essential file systems to ROOT."
|
| 83 | (for-each (lambda (dir)
|
| 84 | (let ((target (string-append root dir)))
|
| 85 | (unless (file-exists? target)
|
| 86 | (mkdir target))
|
| 87 | (mount dir target "" MS_MOVE)))
|
| 88 | '("/dev" "/proc" "/sys")))
|
| 89 |
|
| 90 | (define (linux-command-line)
|
| 91 | "Return the Linux kernel command line as a list of strings."
|
| 92 | (string-tokenize
|
| 93 | (call-with-input-file "/proc/cmdline"
|
| 94 | get-string-all)))
|
| 95 |
|
| 96 | (define (find-long-option option arguments)
|
| 97 | "Find OPTION among ARGUMENTS, where OPTION is something like \"gnu.load\".
|
| 98 | Return the value associated with OPTION, or #f on failure."
|
| 99 | (let ((opt (string-append option "=")))
|
| 100 | (and=> (find (cut string-prefix? opt <>)
|
| 101 | arguments)
|
| 102 | (lambda (arg)
|
| 103 | (substring arg (+ 1 (string-index arg #\=)))))))
|
| 104 |
|
| 105 | (define (find-long-options option arguments)
|
| 106 | "Find OPTIONs among ARGUMENTS, where OPTION is something like \"console\".
|
| 107 | Return the values associated with OPTIONs as a list, or the empty list if
|
| 108 | OPTION doesn't appear in ARGUMENTS."
|
| 109 | (let ((opt (string-append option "=")))
|
| 110 | (filter-map (lambda (arg)
|
| 111 | (and (string-prefix? opt arg)
|
| 112 | (substring arg (+ 1 (string-index arg #\=)))))
|
| 113 | arguments)))
|
| 114 |
|
| 115 | (define (resume-if-hibernated device)
|
| 116 | "Resume from hibernation if possible. This is safe ONLY if no on-disk file
|
| 117 | systems have been mounted; calling it later risks severe file system corruption!
|
| 118 | See <Documentation/swsusp.txt> in the kernel source directory. This is the
|
| 119 | caller's responsibility, as is catching exceptions if resumption was supposed to
|
| 120 | happen but didn't.
|
| 121 |
|
| 122 | Resume only from DEVICE if it's a string. If it's #f, use the kernel's default
|
| 123 | hibernation device (CONFIG_PM_STD_PARTITION). Never return if resumption
|
| 124 | succeeds. Return nothing otherwise. The kernel logs any details to dmesg."
|
| 125 |
|
| 126 | (define (string->major:minor string)
|
| 127 | "Return a string with MAJOR:MINOR numbers of the device specified by STRING"
|
| 128 |
|
| 129 | |
| 130 | |
| 131 | (let* ((spec (cond ((string-prefix? "/" string) string)
|
| 132 | ((uuid string) => identity)
|
| 133 | (else (file-system-label string))))
|
| 134 | |
| 135 | |
| 136 | |
| 137 | (device (canonicalize-device-spec spec))
|
| 138 | (rdev (stat:rdev (stat device))))
|
| 139 | (let-values (((major minor) (device-number->major+minor rdev)))
|
| 140 | (format #f "~a:~a" major minor))))
|
| 141 |
|
| 142 | |
| 143 | |
| 144 | (let ((resume "/sys/power/resume"))
|
| 145 | (when (file-exists? resume) |
| 146 | |
| 147 | (let ((major:minor (if device
|
| 148 | (or (false-if-exception (string->major:minor
|
| 149 | device))
|
| 150 | |
| 151 | device)
|
| 152 | (let ((default (call-with-input-file resume
|
| 153 | read-line)))
|
| 154 | |
| 155 | (if (string=? "0:0" default)
|
| 156 | #f
|
| 157 | default)))))
|
| 158 | (when major:minor
|
| 159 | (call-with-output-file resume |
| 160 | (cut display major:minor <>))))))) |
| 161 |
|
| 162 | (define* (make-disk-device-nodes base major #:optional (minor 0))
|
| 163 | "Make the block device nodes around BASE (something like \"/root/dev/sda\")
|
| 164 | with the given MAJOR number, starting with MINOR."
|
| 165 | (mknod base 'block-special #o644 (device-number major minor))
|
| 166 | (let loop ((i 1))
|
| 167 | (when (< i 16)
|
| 168 | (mknod (string-append base (number->string i))
|
| 169 | 'block-special #o644 (device-number major (+ minor i)))
|
| 170 | (loop (+ i 1)))))
|
| 171 |
|
| 172 | |
| 173 | (define-record-type <device-node>
|
| 174 | (device-node name type major minor module)
|
| 175 | device-node?
|
| 176 | (name device-node-name)
|
| 177 | (type device-node-type)
|
| 178 | (major device-node-major)
|
| 179 | (minor device-node-minor)
|
| 180 | (module device-node-module))
|
| 181 |
|
| 182 | (define (read-static-device-nodes port)
|
| 183 | "Read from PORT a list of <device-node> written in the format used by
|
| 184 | /lib/modules/*/*.devname files."
|
| 185 | (let loop ((line (read-line port)))
|
| 186 | (if (eof-object? line)
|
| 187 | '()
|
| 188 | (match (string-split line #\space)
|
| 189 | (((? (cut string-prefix? "#" <>)) _ ...)
|
| 190 | (loop (read-line port)))
|
| 191 | ((module-name device-name device-spec)
|
| 192 | (let* ((device-parts
|
| 193 | (string-match "([bc])([0-9][0-9]*):([0-9][0-9]*)"
|
| 194 | device-spec))
|
| 195 | (type-string (match:substring device-parts 1))
|
| 196 | (type (match type-string
|
| 197 | ("c" 'char-special)
|
| 198 | ("b" 'block-special)))
|
| 199 | (major-string (match:substring device-parts 2))
|
| 200 | (major (string->number major-string 10))
|
| 201 | (minor-string (match:substring device-parts 3))
|
| 202 | (minor (string->number minor-string 10)))
|
| 203 | (cons (device-node device-name type major minor module-name)
|
| 204 | (loop (read-line port)))))
|
| 205 | (_
|
| 206 | (begin
|
| 207 | (format (current-error-port)
|
| 208 | "read-static-device-nodes: ignored devname line '~a'~%" line)
|
| 209 | (loop (read-line port))))))))
|
| 210 |
|
| 211 | (define* (mkdir-p* dir #:optional (mode #o755))
|
| 212 | "This is a variant of 'mkdir-p' that works around
|
| 213 | <http://bugs.gnu.org/24659> by passing MODE explicitly in each 'mkdir' call."
|
| 214 | (define absolute?
|
| 215 | (string-prefix? "/" dir))
|
| 216 |
|
| 217 | (define not-slash
|
| 218 | (char-set-complement (char-set #\/)))
|
| 219 |
|
| 220 | (let loop ((components (string-tokenize dir not-slash))
|
| 221 | (root (if absolute?
|
| 222 | ""
|
| 223 | ".")))
|
| 224 | (match components
|
| 225 | ((head tail ...)
|
| 226 | (let ((path (string-append root "/" head)))
|
| 227 | (catch 'system-error
|
| 228 | (lambda ()
|
| 229 | (mkdir path mode)
|
| 230 | (loop tail path))
|
| 231 | (lambda args
|
| 232 | (if (= EEXIST (system-error-errno args))
|
| 233 | (loop tail path)
|
| 234 | (apply throw args))))))
|
| 235 | (() #t))))
|
| 236 |
|
| 237 | (define (report-system-error name . args)
|
| 238 | "Report a system error for the file NAME."
|
| 239 | (let ((errno (system-error-errno args)))
|
| 240 | (format (current-error-port) "could not create '~a': ~a~%" name
|
| 241 | (strerror errno))))
|
| 242 |
|
| 243 | |
| 244 | (define-syntax-rule (catch-system-error name exp)
|
| 245 | (catch 'system-error
|
| 246 | (lambda ()
|
| 247 | exp)
|
| 248 | (lambda args
|
| 249 | (apply report-system-error name args))))
|
| 250 |
|
| 251 | |
| 252 | (define create-device-node
|
| 253 | (match-lambda
|
| 254 | (($ <device-node> xname type major minor module)
|
| 255 | (let ((name (string-append "/dev/" xname)))
|
| 256 | (mkdir-p* (dirname name))
|
| 257 | (catch-system-error name
|
| 258 | (mknod name type #o600 (device-number major minor)))))))
|
| 259 |
|
| 260 | (define* (make-static-device-nodes linux-release-module-directory)
|
| 261 | "Create static device nodes required by the given Linux release.
|
| 262 | This is required in order to solve a chicken-or-egg problem:
|
| 263 | The Linux kernel has a feature to autoload modules when a device is first
|
| 264 | accessed.
|
| 265 | And udev has a feature to set the permissions of static nodes correctly
|
| 266 | when it is starting up and also to automatically create nodes when hardware
|
| 267 | is hotplugged. That leaves universal device files which are not linked to
|
| 268 | one specific hardware device. These we have to create."
|
| 269 | (let ((devname-name (string-append linux-release-module-directory "/"
|
| 270 | "modules.devname")))
|
| 271 | (for-each create-device-node
|
| 272 | (call-with-input-file devname-name
|
| 273 | read-static-device-nodes))))
|
| 274 |
|
| 275 | (define* (make-essential-device-nodes #:optional (root "/"))
|
| 276 | "Make essential device nodes under ROOT/dev."
|
| 277 | |
| 278 |
|
| 279 | (define (scope dir)
|
| 280 | (string-append root
|
| 281 | (if (string-suffix? "/" root)
|
| 282 | ""
|
| 283 | "/")
|
| 284 | dir))
|
| 285 |
|
| 286 | (unless (file-exists? (scope "dev"))
|
| 287 | (mkdir (scope "dev")))
|
| 288 |
|
| 289 | |
| 290 | (make-disk-device-nodes (scope "dev/sda") 8)
|
| 291 | (make-disk-device-nodes (scope "dev/sdb") 8 16)
|
| 292 | (make-disk-device-nodes (scope "dev/sdc") 8 32)
|
| 293 | (make-disk-device-nodes (scope "dev/sdd") 8 48)
|
| 294 |
|
| 295 | |
| 296 | (mknod (scope "dev/scd0") 'block-special #o644 (device-number 11 0))
|
| 297 | (mknod (scope "dev/scd1") 'block-special #o644 (device-number 11 1))
|
| 298 |
|
| 299 | |
| 300 | (make-disk-device-nodes (scope "dev/vda") 252)
|
| 301 |
|
| 302 | |
| 303 | (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
|
| 304 | (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
|
| 305 |
|
| 306 | |
| 307 | (unless (file-exists? (scope "dev/input"))
|
| 308 | (mkdir (scope "dev/input")))
|
| 309 | (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
|
| 310 | (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
|
| 311 | (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
|
| 312 |
|
| 313 | |
| 314 | |
| 315 | (unless (string=? root "/")
|
| 316 | (mknod (scope "dev/console") 'char-special #o600
|
| 317 | (device-number 5 1)))
|
| 318 |
|
| 319 | |
| 320 | (mknod (scope "dev/tty") 'char-special #o600
|
| 321 | (device-number 5 0))
|
| 322 | (chmod (scope "dev/tty") #o666)
|
| 323 | (let loop ((n 0))
|
| 324 | (and (< n 50)
|
| 325 | (let ((name (format #f "dev/tty~a" n)))
|
| 326 | (mknod (scope name) 'char-special #o600
|
| 327 | (device-number 4 n))
|
| 328 | (loop (+ 1 n)))))
|
| 329 |
|
| 330 | |
| 331 | (mknod (scope "dev/ttyS0") 'char-special #o660
|
| 332 | (device-number 4 64))
|
| 333 |
|
| 334 | |
| 335 | (mknod (scope "dev/ptmx") 'char-special #o666
|
| 336 | (device-number 5 2))
|
| 337 | (chmod (scope "dev/ptmx") #o666)
|
| 338 |
|
| 339 | |
| 340 | (unless (file-exists? (scope "dev/pts"))
|
| 341 | (mkdir (scope "dev/pts")))
|
| 342 |
|
| 343 | |
| 344 | (mknod (scope "dev/log") 'socket #o666 0)
|
| 345 | (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
|
| 346 |
|
| 347 | |
| 348 | (for-each (match-lambda
|
| 349 | ((file major minor)
|
| 350 | (mknod (scope file) 'char-special #o666
|
| 351 | (device-number major minor))
|
| 352 | (chmod (scope file) #o666)))
|
| 353 | '(("dev/null" 1 3)
|
| 354 | ("dev/zero" 1 5)
|
| 355 | ("dev/full" 1 7)
|
| 356 | ("dev/random" 1 8)
|
| 357 | ("dev/urandom" 1 9)))
|
| 358 |
|
| 359 | (symlink "/proc/self/fd" (scope "dev/fd"))
|
| 360 | (symlink "/proc/self/fd/0" (scope "dev/stdin"))
|
| 361 | (symlink "/proc/self/fd/1" (scope "dev/stdout"))
|
| 362 | (symlink "/proc/self/fd/2" (scope "dev/stderr"))
|
| 363 |
|
| 364 | |
| 365 | (let loop ((i 0))
|
| 366 | (when (< i 8)
|
| 367 | (mknod (scope (string-append "dev/loop" (number->string i)))
|
| 368 | 'block-special #o660
|
| 369 | (device-number 7 i))
|
| 370 | (loop (+ 1 i))))
|
| 371 |
|
| 372 | |
| 373 | (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
|
| 374 |
|
| 375 | (define %host-qemu-ipv4-address
|
| 376 | (inet-pton AF_INET "10.0.2.10"))
|
| 377 |
|
| 378 | (define* (configure-qemu-networking #:optional (interface "eth0"))
|
| 379 | "Setup the INTERFACE network interface and /etc/resolv.conf according to
|
| 380 | QEMU's default networking settings (see net/slirp.c in QEMU for default
|
| 381 | networking values.) Return #t if INTERFACE is up, #f otherwise."
|
| 382 | (display "configuring QEMU networking...\n")
|
| 383 | (let* ((sock (socket AF_INET SOCK_STREAM 0))
|
| 384 | (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
|
| 385 | (flags (network-interface-flags sock interface)))
|
| 386 | (set-network-interface-address sock interface address)
|
| 387 | (set-network-interface-flags sock interface (logior flags IFF_UP))
|
| 388 |
|
| 389 | (logand (network-interface-flags sock interface) IFF_UP)))
|
| 390 |
|
| 391 | (define (pidof program)
|
| 392 | "Return the PID of the first presumed instance of PROGRAM."
|
| 393 | (let ((program (basename program)))
|
| 394 | (find (lambda (pid)
|
| 395 | (let ((exe (format #f "/proc/~a/exe" pid)))
|
| 396 | (and=> (false-if-exception (readlink exe))
|
| 397 | (compose (cut string=? program <>) basename))))
|
| 398 | (filter-map string->number (scandir "/proc")))))
|
| 399 |
|
| 400 | (define* (mount-root-file-system root type
|
| 401 | #:key volatile-root? (flags 0) options
|
| 402 | check? skip-check-if-clean? repair)
|
| 403 | "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT? is
|
| 404 | true, mount ROOT read-only and make it an overlay with a writable tmpfs using
|
| 405 | the kernel built-in overlayfs. FLAGS and OPTIONS indicates the options to use
|
| 406 | to mount ROOT, and behave the same as for the `mount' procedure.
|
| 407 |
|
| 408 | If CHECK? is true, first run ROOT's fsck tool (if any) non-interactively.
|
| 409 | If SKIP-CHECK-IF-CLEAN? is true, ask fsck to return immediately if ROOT is
|
| 410 | marked as clean. If REPAIR is true, fsck may write to ROOT to perform repairs.
|
| 411 | If REPAIR is also 'PREEN, ask fsck to perform only those repairs that it
|
| 412 | considers safe."
|
| 413 |
|
| 414 | (if volatile-root?
|
| 415 | (begin
|
| 416 | (mkdir-p "/real-root")
|
| 417 | (mount root "/real-root" type (logior MS_RDONLY flags) options)
|
| 418 | (mkdir-p "/rw-root")
|
| 419 | (mount "none" "/rw-root" "tmpfs")
|
| 420 |
|
| 421 | |
| 422 | (mkdir-p "/rw-root/upper")
|
| 423 | (mkdir-p "/rw-root/work")
|
| 424 |
|
| 425 | |
| 426 | (mkdir-p "/rw-root/upper/dev")
|
| 427 | (mount "none" "/rw-root/upper/dev" "devtmpfs")
|
| 428 |
|
| 429 | |
| 430 | (mount "none" "/root" "overlay" 0
|
| 431 | "lowerdir=/real-root,upperdir=/rw-root/upper,workdir=/rw-root/work"))
|
| 432 | (begin
|
| 433 | (when check?
|
| 434 | (check-file-system root type (not skip-check-if-clean?) repair))
|
| 435 | (mount root "/root" type flags options)))
|
| 436 |
|
| 437 | |
| 438 | (false-if-exception
|
| 439 | (delete-file "/root/etc/mtab"))
|
| 440 | (mkdir-p "/root/etc")
|
| 441 | (symlink "/proc/self/mounts" "/root/etc/mtab"))
|
| 442 |
|
| 443 | (define (switch-root root)
|
| 444 | "Switch to ROOT as the root file system, in a way similar to what
|
| 445 | util-linux' switch_root(8) does."
|
| 446 | (move-essential-file-systems root)
|
| 447 | (chdir root)
|
| 448 |
|
| 449 | |
| 450 | |
| 451 | (when (or (not (file-exists? "/init")) (directory-exists? "/home"))
|
| 452 | (format (current-error-port)
|
| 453 | "The root file system is probably not an initrd; \
|
| 454 | bailing out.~%root contents: ~s~%" (scandir "/"))
|
| 455 | (force-output (current-error-port))
|
| 456 | (exit 1))
|
| 457 |
|
| 458 | |
| 459 | |
| 460 | |
| 461 | (let ((root-device (stat:dev (stat "/"))))
|
| 462 | (for-each (lambda (file)
|
| 463 | (unless (member file '("." ".."))
|
| 464 | (let* ((file (string-append "/" file))
|
| 465 | (device (stat:dev (lstat file))))
|
| 466 | (when (= device root-device)
|
| 467 | (delete-file-recursively file)))))
|
| 468 | (scandir "/")))
|
| 469 |
|
| 470 | |
| 471 | (mount root "/" "" MS_MOVE)
|
| 472 | (chroot ".")
|
| 473 | (chdir "/")
|
| 474 |
|
| 475 | (when (file-exists? "/dev/console")
|
| 476 | |
| 477 | |
| 478 | (let ((console (open-file "/dev/console" "r+b0")))
|
| 479 | (for-each close-fdes '(0 1 2))
|
| 480 |
|
| 481 | (dup2 (fileno console) 0)
|
| 482 | (dup2 (fileno console) 1)
|
| 483 | (dup2 (fileno console) 2)
|
| 484 |
|
| 485 | (close-port console))))
|
| 486 |
|
| 487 |
|
| 488 | (define* (boot-system #:key
|
| 489 | (linux-modules '())
|
| 490 | linux-module-directory
|
| 491 | keymap-file
|
| 492 | qemu-guest-networking?
|
| 493 | volatile-root?
|
| 494 | pre-mount
|
| 495 | (mounts '())
|
| 496 | (on-error 'debug))
|
| 497 | "This procedure is meant to be called from an initrd. Boot a system by
|
| 498 | first loading LINUX-MODULES (a list of module names) from
|
| 499 | LINUX-MODULE-DIRECTORY, then installing KEYMAP-FILE with 'loadkeys' (if
|
| 500 | KEYMAP-FILE is true), then setting up QEMU guest networking if
|
| 501 | QEMU-GUEST-NETWORKING? is true, calling PRE-MOUNT, mounting the file systems
|
| 502 | specified in MOUNTS, and finally booting into the new root if any. The initrd
|
| 503 | supports the kernel command-line options 'gnu.load' and 'gnu.repl'. It also
|
| 504 | honors a subset of the Linux kernel command-line parameters such as
|
| 505 | 'fsck.mode', 'resume', 'rootdelay', rootflags and rootfstype.
|
| 506 |
|
| 507 | Mount the root file system, specified by the 'root' command-line argument, if
|
| 508 | any.
|
| 509 |
|
| 510 | MOUNTS must be a list of <file-system> objects.
|
| 511 |
|
| 512 | When VOLATILE-ROOT? is true, the root file system is writable but any changes
|
| 513 | to it are lost.
|
| 514 |
|
| 515 | ON-ERROR is passed to 'call-with-error-handling'; it determines what happens
|
| 516 | upon error."
|
| 517 | (define (root-mount-point? fs)
|
| 518 | (string=? (file-system-mount-point fs) "/"))
|
| 519 |
|
| 520 | (define (device-string->file-system-device device-string)
|
| 521 | |
| 522 | |
| 523 | |
| 524 | (cond ((string-prefix? "/" device-string) device-string)
|
| 525 | ((string-contains device-string ":/") device-string) |
| 526 | ((uuid device-string) => identity)
|
| 527 | (else (file-system-label device-string))))
|
| 528 |
|
| 529 | (display "Welcome, this is GNU's early boot Guile.\n")
|
| 530 | (display "Use 'gnu.repl' for an initrd REPL.\n\n")
|
| 531 |
|
| 532 | (call-with-error-handling
|
| 533 | (lambda ()
|
| 534 | (mount-essential-file-systems)
|
| 535 | (let* ((args (linux-command-line))
|
| 536 | (to-load (find-long-option "gnu.load" args))
|
| 537 | |
| 538 | |
| 539 | (root-device (and=> (find-long-option "root" args)
|
| 540 | device-string->file-system-device))
|
| 541 | (rootfstype (find-long-option "rootfstype" args))
|
| 542 | (rootflags (find-long-option "rootflags" args))
|
| 543 | (root-fs* (find root-mount-point? mounts))
|
| 544 | (fsck.mode (find-long-option "fsck.mode" args)))
|
| 545 |
|
| 546 | (unless (or root-fs* (and root-device rootfstype))
|
| 547 | (error "no root file system or 'root' and 'rootfstype' parameters"))
|
| 548 |
|
| 549 | |
| 550 | |
| 551 | |
| 552 | (define root-fs
|
| 553 | (if root-fs*
|
| 554 | (file-system
|
| 555 | (inherit root-fs*)
|
| 556 | (device (or root-device (file-system-device root-fs*)))
|
| 557 | (type (or rootfstype (file-system-type root-fs*)))
|
| 558 | (options (or rootflags (file-system-options root-fs*))))
|
| 559 | (file-system
|
| 560 | (device root-device)
|
| 561 | (mount-point "/")
|
| 562 | (type rootfstype)
|
| 563 | (options rootflags))))
|
| 564 |
|
| 565 | (define (check? fs)
|
| 566 | (match fsck.mode
|
| 567 | ("skip" #f)
|
| 568 | ("force" #t)
|
| 569 | (_ (file-system-check? fs)))) |
| 570 |
|
| 571 | (define (skip-check-if-clean? fs)
|
| 572 | (match fsck.mode
|
| 573 | ("force" #f)
|
| 574 | (_ (file-system-skip-check-if-clean? fs))))
|
| 575 |
|
| 576 | (define (repair fs)
|
| 577 | (let ((arg (find-long-option "fsck.repair" args)))
|
| 578 | (if arg
|
| 579 | (match arg
|
| 580 | ("no" #f)
|
| 581 | ("yes" #t)
|
| 582 | (_ 'preen))
|
| 583 | (file-system-repair fs))))
|
| 584 |
|
| 585 | (when (member "gnu.repl" args)
|
| 586 | (start-repl))
|
| 587 |
|
| 588 | (display "loading kernel modules...\n")
|
| 589 | (load-linux-modules-from-directory linux-modules
|
| 590 | linux-module-directory)
|
| 591 |
|
| 592 | (when keymap-file
|
| 593 | (let ((status (system* "loadkeys" keymap-file)))
|
| 594 | (unless (zero? status)
|
| 595 | |
| 596 | |
| 597 | (format (current-error-port)
|
| 598 | "warning: 'loadkeys' exited with status ~a~%"
|
| 599 | status))))
|
| 600 |
|
| 601 | (when qemu-guest-networking?
|
| 602 | (unless (configure-qemu-networking)
|
| 603 | (display "network interface is DOWN\n")))
|
| 604 |
|
| 605 | |
| 606 | |
| 607 | (let ((root-delay (and=> (find-long-option "rootdelay" args)
|
| 608 | string->number)))
|
| 609 | (when root-delay
|
| 610 | (format #t "Pausing for rootdelay=~a seconds before mounting \
|
| 611 | the root file system...\n" root-delay)
|
| 612 | (sleep root-delay)))
|
| 613 |
|
| 614 | (when (procedure? pre-mount)
|
| 615 | |
| 616 | |
| 617 | |
| 618 | (unless (pre-mount)
|
| 619 | (error "pre-mount actions failed")))
|
| 620 |
|
| 621 | (unless (or (member "hibernate=noresume" args)
|
| 622 | |
| 623 | |
| 624 | (member "noresume" args))
|
| 625 | |
| 626 | |
| 627 | (false-if-exception |
| 628 | (resume-if-hibernated (find-long-option "resume" args))))
|
| 629 |
|
| 630 | |
| 631 | (unless (file-exists? "/root")
|
| 632 | (mkdir "/root"))
|
| 633 |
|
| 634 | (setenv "EXT2FS_NO_MTAB_OK" "1")
|
| 635 |
|
| 636 | |
| 637 | (mount-root-file-system (canonicalize-device-spec
|
| 638 | (file-system-device root-fs))
|
| 639 | (file-system-type root-fs)
|
| 640 | #:volatile-root? volatile-root?
|
| 641 | #:flags (mount-flags->bit-mask
|
| 642 | (file-system-flags root-fs))
|
| 643 | #:options (file-system-options root-fs)
|
| 644 | #:check? (check? root-fs)
|
| 645 | #:skip-check-if-clean?
|
| 646 | (skip-check-if-clean? root-fs)
|
| 647 | #:repair (repair root-fs))
|
| 648 |
|
| 649 | |
| 650 | (for-each (lambda (fs)
|
| 651 | (mount-file-system fs
|
| 652 | #:check? (check? fs)
|
| 653 | #:skip-check-if-clean?
|
| 654 | (skip-check-if-clean? fs)
|
| 655 | #:repair (repair fs)))
|
| 656 | (remove root-mount-point? mounts))
|
| 657 |
|
| 658 | (setenv "EXT2FS_NO_MTAB_OK" #f)
|
| 659 |
|
| 660 | (if to-load
|
| 661 | (begin
|
| 662 | (switch-root "/root")
|
| 663 | (format #t "loading '~a'...\n" to-load)
|
| 664 |
|
| 665 | (primitive-load to-load)
|
| 666 |
|
| 667 | (format (current-error-port)
|
| 668 | "boot program '~a' terminated, rebooting~%"
|
| 669 | to-load)
|
| 670 | (sleep 2)
|
| 671 | (reboot))
|
| 672 | (begin
|
| 673 | (display "no boot file passed via 'gnu.load'\n")
|
| 674 | (display "entering a warm and cozy REPL\n")
|
| 675 | (start-repl)))))
|
| 676 | #:on-error on-error))
|
| 677 |
|
| 678 | |