projects/guix·blob mpd-fix-groups · refs

678 lines · 27.0 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <[email protected]>
3;;; Copyright © 2016, 2017, 2019–2021 Tobias Geerinckx-Rice <[email protected]>
4;;; Copyright © 2017 Mathieu Othacehe <[email protected]>
5;;; Copyright © 2019 Guillaume Le Vaillant <[email protected]>
6;;; Copyright © 2020, 2022 Maxim Cournoyer <[email protected]>
7;;;
8;;; This file is part of GNU Guix.
9;;;
10;;; GNU Guix is free software; you can redistribute it and/or modify it
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
15;;; GNU Guix is distributed in the hope that it will be useful, but
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
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;;; Commentary:
52;;;
53;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that
54;;; many of these use procedures not yet available in vanilla Guile (`mount',
55;;; `load-linux-module', etc.); these are provided by a Guile patch used in
56;;; the GNU distribution.
57;;;
58;;; Code:
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\".
98Return 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\".
107Return the values associated with OPTIONs as a list, or the empty list if
108OPTION 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
117systems have been mounted; calling it later risks severe file system corruption!
118See <Documentation/swsusp.txt> in the kernel source directory. This is the
119caller's responsibility, as is catching exceptions if resumption was supposed to
120happen but didn't.
121
122Resume only from DEVICE if it's a string. If it's #f, use the kernel's default
123hibernation device (CONFIG_PM_STD_PARTITION). Never return if resumption
124succeeds. 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 ;; The "resume=" kernel command-line option always provides a string, which
130 ;; can represent a device, a UUID, or a label. Check for all three.
131 (let* ((spec (cond ((string-prefix? "/" string) string)
132 ((uuid string) => identity)
133 (else (file-system-label string))))
134 ;; XXX The kernel's swsusp_resume_can_resume() waits if ‘resumewait’
135 ;; is found on the command line; our canonicalize-device-spec gives
136 ;; up after 20 seconds. We could emulate the former by looping…
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 ;; Write the resume DEVICE to this magic file, using the MAJOR:MINOR device
143 ;; numbers if possible. The kernel will immediately try to resume from it.
144 (let ((resume "/sys/power/resume"))
145 (when (file-exists? resume) ; this kernel supports hibernation
146 ;; Honour the kernel's default device (only) if none other was given.
147 (let ((major:minor (if device
148 (or (false-if-exception (string->major:minor
149 device))
150 ;; We can't parse it. Maybe the kernel can.
151 device)
152 (let ((default (call-with-input-file resume
153 read-line)))
154 ;; Don't waste time echoing 0:0 to /sys.
155 (if (string=? "0:0" default)
156 #f
157 default)))))
158 (when major:minor
159 (call-with-output-file resume ; may throw an ‘Invalid argument’
160 (cut display major:minor <>))))))) ; may never return
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\")
164with 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;; Representation of a /dev node.
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;; Catch a system-error, log it and don't die from it.
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;; Create a device node like the <device-node> passed here on the file system.
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.
262This is required in order to solve a chicken-or-egg problem:
263The Linux kernel has a feature to autoload modules when a device is first
264accessed.
265And udev has a feature to set the permissions of static nodes correctly
266when it is starting up and also to automatically create nodes when hardware
267is hotplugged. That leaves universal device files which are not linked to
268one 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 ;; The hand-made devtmpfs/udev!
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 ;; Make the device nodes for SCSI disks.
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 ;; SCSI CD-ROM devices (aka. "/dev/sr0" etc.).
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 ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
300 (make-disk-device-nodes (scope "dev/vda") 252)
301
302 ;; Memory (used by Xorg's VESA driver.)
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 ;; Inputs (used by Xorg.)
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 ;; System console. This node is magically created by the kernel on the
314 ;; initrd's root, so don't try to create it in that case.
315 (unless (string=? root "/")
316 (mknod (scope "dev/console") 'char-special #o600
317 (device-number 5 1)))
318
319 ;; TTYs.
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 ;; Serial line.
331 (mknod (scope "dev/ttyS0") 'char-special #o660
332 (device-number 4 64))
333
334 ;; Pseudo ttys.
335 (mknod (scope "dev/ptmx") 'char-special #o666
336 (device-number 5 2))
337 (chmod (scope "dev/ptmx") #o666)
338
339 ;; Create /dev/pts; it will be mounted later, at boot time.
340 (unless (file-exists? (scope "dev/pts"))
341 (mkdir (scope "dev/pts")))
342
343 ;; Rendez-vous point for syslogd.
344 (mknod (scope "dev/log") 'socket #o666 0)
345 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
346
347 ;; Other useful nodes, notably relied on by guix-daemon.
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 ;; Loopback devices.
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 ;; File systems in user space (FUSE).
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
380QEMU's default networking settings (see net/slirp.c in QEMU for default
381networking 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
404true, mount ROOT read-only and make it an overlay with a writable tmpfs using
405the kernel built-in overlayfs. FLAGS and OPTIONS indicates the options to use
406to mount ROOT, and behave the same as for the `mount' procedure.
407
408If CHECK? is true, first run ROOT's fsck tool (if any) non-interactively.
409If SKIP-CHECK-IF-CLEAN? is true, ask fsck to return immediately if ROOT is
410marked as clean. If REPAIR is true, fsck may write to ROOT to perform repairs.
411If REPAIR is also 'PREEN, ask fsck to perform only those repairs that it
412considers 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 ;; Create the upperdir and the workdir of the overlayfs
422 (mkdir-p "/rw-root/upper")
423 (mkdir-p "/rw-root/work")
424
425 ;; We want read-write /dev nodes.
426 (mkdir-p "/rw-root/upper/dev")
427 (mount "none" "/rw-root/upper/dev" "devtmpfs")
428
429 ;; Make /root an overlay of the tmpfs and the actual root.
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 ;; Make sure /root/etc/mtab is a symlink to /proc/self/mounts.
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
445util-linux' switch_root(8) does."
446 (move-essential-file-systems root)
447 (chdir root)
448
449 ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
450 ;; TODO: Use 'statfs' to check the fs type, like klibc does.
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; \
454bailing out.~%root contents: ~s~%" (scandir "/"))
455 (force-output (current-error-port))
456 (exit 1))
457
458 ;; Delete files from the old root, without crossing mount points (assuming
459 ;; there are no mount points in sub-directories.) That means we're leaving
460 ;; the empty ROOT directory behind us, but that's OK.
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 ;; Make ROOT the new root.
471 (mount root "/" "" MS_MOVE)
472 (chroot ".")
473 (chdir "/")
474
475 (when (file-exists? "/dev/console")
476 ;; Close the standard file descriptors since they refer to the old
477 ;; /dev/console, and reopen them.
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
498first loading LINUX-MODULES (a list of module names) from
499LINUX-MODULE-DIRECTORY, then installing KEYMAP-FILE with 'loadkeys' (if
500KEYMAP-FILE is true), then setting up QEMU guest networking if
501QEMU-GUEST-NETWORKING? is true, calling PRE-MOUNT, mounting the file systems
502specified in MOUNTS, and finally booting into the new root if any. The initrd
503supports the kernel command-line options 'gnu.load' and 'gnu.repl'. It also
504honors a subset of the Linux kernel command-line parameters such as
505'fsck.mode', 'resume', 'rootdelay', rootflags and rootfstype.
506
507Mount the root file system, specified by the 'root' command-line argument, if
508any.
509
510MOUNTS must be a list of <file-system> objects.
511
512When VOLATILE-ROOT? is true, the root file system is writable but any changes
513to it are lost.
514
515ON-ERROR is passed to 'call-with-error-handling'; it determines what happens
516upon 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 ;; The "root=SPEC" kernel command-line option always provides a string,
522 ;; but the string can represent a device, an nfs-root, a UUID, or a label.
523 ;; So check for all four.
524 (cond ((string-prefix? "/" device-string) device-string)
525 ((string-contains device-string ":/") device-string) ; nfs-root
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 ;; If present, ‘root’ on the kernel command line takes precedence
538 ;; over the ‘device’ field of the root <file-system> record.
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 ;; If present, ‘root’ on the kernel command line takes precedence over
550 ;; the ‘device’ field of the root <file-system> record; likewise for
551 ;; the 'rootfstype' and 'rootflags' arguments.
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)))) ; assume "auto"
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 ;; Emit a warning rather than abort when we cannot load
596 ;; KEYMAP-FILE.
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 ;; A big ugly hammer, to be used only for debugging and in desperate
606 ;; situations where no proper device synchonisation is possible.
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 \
611the root file system...\n" root-delay)
612 (sleep root-delay)))
613
614 (when (procedure? pre-mount)
615 ;; Do whatever actions are needed before mounting the root file
616 ;; system--e.g., installing device mappings. Error out when the
617 ;; return value is false.
618 (unless (pre-mount)
619 (error "pre-mount actions failed")))
620
621 (unless (or (member "hibernate=noresume" args)
622 ;; Also handle the equivalent old-style argument.
623 ;; See Documentation/admin-guide/kernel-parameters.txt.
624 (member "noresume" args))
625 ;; Try to resume immediately after loading (storage) modules
626 ;; but before any on-disk file systems have been mounted.
627 (false-if-exception ; failure is not fatal
628 (resume-if-hibernated (find-long-option "resume" args))))
629
630 ;; Prepare the real root file system under /root.
631 (unless (file-exists? "/root")
632 (mkdir "/root"))
633
634 (setenv "EXT2FS_NO_MTAB_OK" "1")
635
636 ;; Mount the root file system.
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 ;; Mount the specified non-root file systems.
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;;; linux-boot.scm ends here