linux-boot.scm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016, 2017, 2019–2021 Tobias Geerinckx-Rice <me@tobias.gr>
  4. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  5. ;;; Copyright © 2019 Guillaume Le Vaillant <glv@posteo.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu build linux-boot)
  22. #:use-module (rnrs io ports)
  23. #:use-module (system repl error-handling)
  24. #:autoload (system repl repl) (start-repl)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-9)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (ice-9 match)
  30. #:use-module (ice-9 rdelim)
  31. #:use-module (ice-9 regex)
  32. #:use-module (ice-9 ftw)
  33. #:use-module (guix build utils)
  34. #:use-module ((guix build syscalls)
  35. #:hide (file-system-type))
  36. #:use-module (gnu build linux-modules)
  37. #:use-module (gnu build file-systems)
  38. #:use-module (gnu system file-systems)
  39. #:export (mount-essential-file-systems
  40. linux-command-line
  41. find-long-option
  42. find-long-options
  43. make-essential-device-nodes
  44. make-static-device-nodes
  45. configure-qemu-networking
  46. boot-system))
  47. ;;; Commentary:
  48. ;;;
  49. ;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that
  50. ;;; many of these use procedures not yet available in vanilla Guile (`mount',
  51. ;;; `load-linux-module', etc.); these are provided by a Guile patch used in
  52. ;;; the GNU distribution.
  53. ;;;
  54. ;;; Code:
  55. (define* (mount-essential-file-systems #:key (root "/"))
  56. "Mount /dev, /proc, and /sys under ROOT."
  57. (define (scope dir)
  58. (string-append root
  59. (if (string-suffix? "/" root)
  60. ""
  61. "/")
  62. dir))
  63. (unless (file-exists? (scope "proc"))
  64. (mkdir (scope "proc")))
  65. (mount "none" (scope "proc") "proc")
  66. (unless (file-exists? (scope "dev"))
  67. (mkdir (scope "dev")))
  68. (mount "none" (scope "dev") "devtmpfs")
  69. (unless (file-exists? (scope "sys"))
  70. (mkdir (scope "sys")))
  71. (mount "none" (scope "sys") "sysfs"))
  72. (define (move-essential-file-systems root)
  73. "Move currently mounted essential file systems to ROOT."
  74. (for-each (lambda (dir)
  75. (let ((target (string-append root dir)))
  76. (unless (file-exists? target)
  77. (mkdir target))
  78. (mount dir target "" MS_MOVE)))
  79. '("/dev" "/proc" "/sys")))
  80. (define (linux-command-line)
  81. "Return the Linux kernel command line as a list of strings."
  82. (string-tokenize
  83. (call-with-input-file "/proc/cmdline"
  84. get-string-all)))
  85. (define (find-long-option option arguments)
  86. "Find OPTION among ARGUMENTS, where OPTION is something like \"--load\".
  87. Return the value associated with OPTION, or #f on failure."
  88. (let ((opt (string-append option "=")))
  89. (and=> (find (cut string-prefix? opt <>)
  90. arguments)
  91. (lambda (arg)
  92. (substring arg (+ 1 (string-index arg #\=)))))))
  93. (define (find-long-options option arguments)
  94. "Find OPTIONs among ARGUMENTS, where OPTION is something like \"console\".
  95. Return the values associated with OPTIONs as a list, or the empty list if
  96. OPTION doesn't appear in ARGUMENTS."
  97. (let ((opt (string-append option "=")))
  98. (filter-map (lambda (arg)
  99. (and (string-prefix? opt arg)
  100. (substring arg (+ 1 (string-index arg #\=)))))
  101. arguments)))
  102. (define (resume-if-hibernated device)
  103. "Resume from hibernation if possible. This is safe ONLY if no on-disk file
  104. systems have been mounted; calling it later risks severe file system corruption!
  105. See <Documentation/swsusp.txt> in the kernel source directory. This is the
  106. caller's responsibility, as is catching exceptions if resumption was supposed to
  107. happen but didn't.
  108. Resume only from DEVICE if it's a string. If it's #f, use the kernel's default
  109. hibernation device (CONFIG_PM_STD_PARTITION). Never return if resumption
  110. succeeds. Return nothing otherwise. The kernel logs any details to dmesg."
  111. (define (string->major:minor string)
  112. "Return a string with MAJOR:MINOR numbers of the device specified by STRING"
  113. ;; The "resume=" kernel command-line option always provides a string, which
  114. ;; can represent a device, a UUID, or a label. Check for all three.
  115. (let* ((spec (cond ((string-prefix? "/" string) string)
  116. ((uuid string) => identity)
  117. (else (file-system-label string))))
  118. ;; XXX The kernel's swsusp_resume_can_resume() waits if ‘resumewait’
  119. ;; is found on the command line; our canonicalize-device-spec gives
  120. ;; up after 20 seconds. We could emulate the former by looping…
  121. (device (canonicalize-device-spec spec))
  122. (rdev (stat:rdev (stat device))))
  123. (let-values (((major minor) (device-number->major+minor rdev)))
  124. (format #f "~a:~a" major minor))))
  125. ;; Write the resume DEVICE to this magic file, using the MAJOR:MINOR device
  126. ;; numbers if possible. The kernel will immediately try to resume from it.
  127. (let ((resume "/sys/power/resume"))
  128. (when (file-exists? resume) ; this kernel supports hibernation
  129. ;; Honour the kernel's default device (only) if none other was given.
  130. (let ((major:minor (if device
  131. (or (false-if-exception (string->major:minor
  132. device))
  133. ;; We can't parse it. Maybe the kernel can.
  134. device)
  135. (let ((default (call-with-input-file resume
  136. read-line)))
  137. ;; Don't waste time echoing 0:0 to /sys.
  138. (if (string=? "0:0" default)
  139. #f
  140. default)))))
  141. (when major:minor
  142. (call-with-output-file resume ; may throw an ‘Invalid argument’
  143. (cut display major:minor <>))))))) ; may never return
  144. (define* (make-disk-device-nodes base major #:optional (minor 0))
  145. "Make the block device nodes around BASE (something like \"/root/dev/sda\")
  146. with the given MAJOR number, starting with MINOR."
  147. (mknod base 'block-special #o644 (device-number major minor))
  148. (let loop ((i 1))
  149. (when (< i 16)
  150. (mknod (string-append base (number->string i))
  151. 'block-special #o644 (device-number major (+ minor i)))
  152. (loop (+ i 1)))))
  153. ;; Representation of a /dev node.
  154. (define-record-type <device-node>
  155. (device-node name type major minor module)
  156. device-node?
  157. (name device-node-name)
  158. (type device-node-type)
  159. (major device-node-major)
  160. (minor device-node-minor)
  161. (module device-node-module))
  162. (define (read-static-device-nodes port)
  163. "Read from PORT a list of <device-node> written in the format used by
  164. /lib/modules/*/*.devname files."
  165. (let loop ((line (read-line port)))
  166. (if (eof-object? line)
  167. '()
  168. (match (string-split line #\space)
  169. (((? (cut string-prefix? "#" <>)) _ ...)
  170. (loop (read-line port)))
  171. ((module-name device-name device-spec)
  172. (let* ((device-parts
  173. (string-match "([bc])([0-9][0-9]*):([0-9][0-9]*)"
  174. device-spec))
  175. (type-string (match:substring device-parts 1))
  176. (type (match type-string
  177. ("c" 'char-special)
  178. ("b" 'block-special)))
  179. (major-string (match:substring device-parts 2))
  180. (major (string->number major-string 10))
  181. (minor-string (match:substring device-parts 3))
  182. (minor (string->number minor-string 10)))
  183. (cons (device-node device-name type major minor module-name)
  184. (loop (read-line port)))))
  185. (_
  186. (begin
  187. (format (current-error-port)
  188. "read-static-device-nodes: ignored devname line '~a'~%" line)
  189. (loop (read-line port))))))))
  190. (define* (mkdir-p* dir #:optional (mode #o755))
  191. "This is a variant of 'mkdir-p' that works around
  192. <http://bugs.gnu.org/24659> by passing MODE explicitly in each 'mkdir' call."
  193. (define absolute?
  194. (string-prefix? "/" dir))
  195. (define not-slash
  196. (char-set-complement (char-set #\/)))
  197. (let loop ((components (string-tokenize dir not-slash))
  198. (root (if absolute?
  199. ""
  200. ".")))
  201. (match components
  202. ((head tail ...)
  203. (let ((path (string-append root "/" head)))
  204. (catch 'system-error
  205. (lambda ()
  206. (mkdir path mode)
  207. (loop tail path))
  208. (lambda args
  209. (if (= EEXIST (system-error-errno args))
  210. (loop tail path)
  211. (apply throw args))))))
  212. (() #t))))
  213. (define (report-system-error name . args)
  214. "Report a system error for the file NAME."
  215. (let ((errno (system-error-errno args)))
  216. (format (current-error-port) "could not create '~a': ~a~%" name
  217. (strerror errno))))
  218. ;; Catch a system-error, log it and don't die from it.
  219. (define-syntax-rule (catch-system-error name exp)
  220. (catch 'system-error
  221. (lambda ()
  222. exp)
  223. (lambda args
  224. (apply report-system-error name args))))
  225. ;; Create a device node like the <device-node> passed here on the file system.
  226. (define create-device-node
  227. (match-lambda
  228. (($ <device-node> xname type major minor module)
  229. (let ((name (string-append "/dev/" xname)))
  230. (mkdir-p* (dirname name))
  231. (catch-system-error name
  232. (mknod name type #o600 (device-number major minor)))))))
  233. (define* (make-static-device-nodes linux-release-module-directory)
  234. "Create static device nodes required by the given Linux release.
  235. This is required in order to solve a chicken-or-egg problem:
  236. The Linux kernel has a feature to autoload modules when a device is first
  237. accessed.
  238. And udev has a feature to set the permissions of static nodes correctly
  239. when it is starting up and also to automatically create nodes when hardware
  240. is hotplugged. That leaves universal device files which are not linked to
  241. one specific hardware device. These we have to create."
  242. (let ((devname-name (string-append linux-release-module-directory "/"
  243. "modules.devname")))
  244. (for-each create-device-node
  245. (call-with-input-file devname-name
  246. read-static-device-nodes))))
  247. (define* (make-essential-device-nodes #:optional (root "/"))
  248. "Make essential device nodes under ROOT/dev."
  249. ;; The hand-made devtmpfs/udev!
  250. (define (scope dir)
  251. (string-append root
  252. (if (string-suffix? "/" root)
  253. ""
  254. "/")
  255. dir))
  256. (unless (file-exists? (scope "dev"))
  257. (mkdir (scope "dev")))
  258. ;; Make the device nodes for SCSI disks.
  259. (make-disk-device-nodes (scope "dev/sda") 8)
  260. (make-disk-device-nodes (scope "dev/sdb") 8 16)
  261. (make-disk-device-nodes (scope "dev/sdc") 8 32)
  262. (make-disk-device-nodes (scope "dev/sdd") 8 48)
  263. ;; SCSI CD-ROM devices (aka. "/dev/sr0" etc.).
  264. (mknod (scope "dev/scd0") 'block-special #o644 (device-number 11 0))
  265. (mknod (scope "dev/scd1") 'block-special #o644 (device-number 11 1))
  266. ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
  267. (make-disk-device-nodes (scope "dev/vda") 252)
  268. ;; Memory (used by Xorg's VESA driver.)
  269. (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
  270. (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
  271. ;; Inputs (used by Xorg.)
  272. (unless (file-exists? (scope "dev/input"))
  273. (mkdir (scope "dev/input")))
  274. (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
  275. (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
  276. (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
  277. ;; System console. This node is magically created by the kernel on the
  278. ;; initrd's root, so don't try to create it in that case.
  279. (unless (string=? root "/")
  280. (mknod (scope "dev/console") 'char-special #o600
  281. (device-number 5 1)))
  282. ;; TTYs.
  283. (mknod (scope "dev/tty") 'char-special #o600
  284. (device-number 5 0))
  285. (chmod (scope "dev/tty") #o666)
  286. (let loop ((n 0))
  287. (and (< n 50)
  288. (let ((name (format #f "dev/tty~a" n)))
  289. (mknod (scope name) 'char-special #o600
  290. (device-number 4 n))
  291. (loop (+ 1 n)))))
  292. ;; Serial line.
  293. (mknod (scope "dev/ttyS0") 'char-special #o660
  294. (device-number 4 64))
  295. ;; Pseudo ttys.
  296. (mknod (scope "dev/ptmx") 'char-special #o666
  297. (device-number 5 2))
  298. (chmod (scope "dev/ptmx") #o666)
  299. ;; Create /dev/pts; it will be mounted later, at boot time.
  300. (unless (file-exists? (scope "dev/pts"))
  301. (mkdir (scope "dev/pts")))
  302. ;; Rendez-vous point for syslogd.
  303. (mknod (scope "dev/log") 'socket #o666 0)
  304. (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
  305. ;; Other useful nodes, notably relied on by guix-daemon.
  306. (for-each (match-lambda
  307. ((file major minor)
  308. (mknod (scope file) 'char-special #o666
  309. (device-number major minor))
  310. (chmod (scope file) #o666)))
  311. '(("dev/null" 1 3)
  312. ("dev/zero" 1 5)
  313. ("dev/full" 1 7)
  314. ("dev/random" 1 8)
  315. ("dev/urandom" 1 9)))
  316. (symlink "/proc/self/fd" (scope "dev/fd"))
  317. (symlink "/proc/self/fd/0" (scope "dev/stdin"))
  318. (symlink "/proc/self/fd/1" (scope "dev/stdout"))
  319. (symlink "/proc/self/fd/2" (scope "dev/stderr"))
  320. ;; Loopback devices.
  321. (let loop ((i 0))
  322. (when (< i 8)
  323. (mknod (scope (string-append "dev/loop" (number->string i)))
  324. 'block-special #o660
  325. (device-number 7 i))
  326. (loop (+ 1 i))))
  327. ;; File systems in user space (FUSE).
  328. (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
  329. (define %host-qemu-ipv4-address
  330. (inet-pton AF_INET "10.0.2.10"))
  331. (define* (configure-qemu-networking #:optional (interface "eth0"))
  332. "Setup the INTERFACE network interface and /etc/resolv.conf according to
  333. QEMU's default networking settings (see net/slirp.c in QEMU for default
  334. networking values.) Return #t if INTERFACE is up, #f otherwise."
  335. (display "configuring QEMU networking...\n")
  336. (let* ((sock (socket AF_INET SOCK_STREAM 0))
  337. (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
  338. (flags (network-interface-flags sock interface)))
  339. (set-network-interface-address sock interface address)
  340. (set-network-interface-flags sock interface (logior flags IFF_UP))
  341. (logand (network-interface-flags sock interface) IFF_UP)))
  342. (define (pidof program)
  343. "Return the PID of the first presumed instance of PROGRAM."
  344. (let ((program (basename program)))
  345. (find (lambda (pid)
  346. (let ((exe (format #f "/proc/~a/exe" pid)))
  347. (and=> (false-if-exception (readlink exe))
  348. (compose (cut string=? program <>) basename))))
  349. (filter-map string->number (scandir "/proc")))))
  350. (define* (mount-root-file-system root type
  351. #:key volatile-root? (flags 0) options
  352. check? skip-check-if-clean? repair)
  353. "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT? is
  354. true, mount ROOT read-only and make it an overlay with a writable tmpfs using
  355. the kernel built-in overlayfs. FLAGS and OPTIONS indicates the options to use
  356. to mount ROOT, and behave the same as for the `mount' procedure.
  357. If CHECK? is true, first run ROOT's fsck tool (if any) non-interactively.
  358. If SKIP-CHECK-IF-CLEAN? is true, ask fsck to return immediately if ROOT is
  359. marked as clean. If REPAIR is true, fsck may write to ROOT to perform repairs.
  360. If REPAIR is also 'PREEN, ask fsck to perform only those repairs that it
  361. considers safe."
  362. (if volatile-root?
  363. (begin
  364. (mkdir-p "/real-root")
  365. (mount root "/real-root" type (logior MS_RDONLY flags) options)
  366. (mkdir-p "/rw-root")
  367. (mount "none" "/rw-root" "tmpfs")
  368. ;; Create the upperdir and the workdir of the overlayfs
  369. (mkdir-p "/rw-root/upper")
  370. (mkdir-p "/rw-root/work")
  371. ;; We want read-write /dev nodes.
  372. (mkdir-p "/rw-root/upper/dev")
  373. (mount "none" "/rw-root/upper/dev" "devtmpfs")
  374. ;; Make /root an overlay of the tmpfs and the actual root.
  375. (mount "none" "/root" "overlay" 0
  376. "lowerdir=/real-root,upperdir=/rw-root/upper,workdir=/rw-root/work"))
  377. (begin
  378. (when check?
  379. (check-file-system root type (not skip-check-if-clean?) repair))
  380. (mount root "/root" type flags options)))
  381. ;; Make sure /root/etc/mtab is a symlink to /proc/self/mounts.
  382. (false-if-exception
  383. (delete-file "/root/etc/mtab"))
  384. (mkdir-p "/root/etc")
  385. (symlink "/proc/self/mounts" "/root/etc/mtab"))
  386. (define (switch-root root)
  387. "Switch to ROOT as the root file system, in a way similar to what
  388. util-linux' switch_root(8) does."
  389. (move-essential-file-systems root)
  390. (chdir root)
  391. ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
  392. ;; TODO: Use 'statfs' to check the fs type, like klibc does.
  393. (when (or (not (file-exists? "/init")) (directory-exists? "/home"))
  394. (format (current-error-port)
  395. "The root file system is probably not an initrd; \
  396. bailing out.~%root contents: ~s~%" (scandir "/"))
  397. (force-output (current-error-port))
  398. (exit 1))
  399. ;; Delete files from the old root, without crossing mount points (assuming
  400. ;; there are no mount points in sub-directories.) That means we're leaving
  401. ;; the empty ROOT directory behind us, but that's OK.
  402. (let ((root-device (stat:dev (stat "/"))))
  403. (for-each (lambda (file)
  404. (unless (member file '("." ".."))
  405. (let* ((file (string-append "/" file))
  406. (device (stat:dev (lstat file))))
  407. (when (= device root-device)
  408. (delete-file-recursively file)))))
  409. (scandir "/")))
  410. ;; Make ROOT the new root.
  411. (mount root "/" "" MS_MOVE)
  412. (chroot ".")
  413. (chdir "/")
  414. (when (file-exists? "/dev/console")
  415. ;; Close the standard file descriptors since they refer to the old
  416. ;; /dev/console, and reopen them.
  417. (let ((console (open-file "/dev/console" "r+b0")))
  418. (for-each close-fdes '(0 1 2))
  419. (dup2 (fileno console) 0)
  420. (dup2 (fileno console) 1)
  421. (dup2 (fileno console) 2)
  422. (close-port console))))
  423. (define* (boot-system #:key
  424. (linux-modules '())
  425. linux-module-directory
  426. keymap-file
  427. qemu-guest-networking?
  428. volatile-root?
  429. pre-mount
  430. (mounts '())
  431. (on-error 'debug))
  432. "This procedure is meant to be called from an initrd. Boot a system by
  433. first loading LINUX-MODULES (a list of module names) from
  434. LINUX-MODULE-DIRECTORY, then installing KEYMAP-FILE with 'loadkeys' (if
  435. KEYMAP-FILE is true), then setting up QEMU guest networking if
  436. QEMU-GUEST-NETWORKING? is true, calling PRE-MOUNT, mounting the file systems
  437. specified in MOUNTS, and finally booting into the new root if any. The initrd
  438. supports kernel command-line options '--load', '--root', and '--repl'.
  439. Mount the root file system, specified by the '--root' command-line argument,
  440. if any.
  441. MOUNTS must be a list of <file-system> objects.
  442. When VOLATILE-ROOT? is true, the root file system is writable but any changes
  443. to it are lost.
  444. ON-ERROR is passed to 'call-with-error-handling'; it determines what happens
  445. upon error."
  446. (define (root-mount-point? fs)
  447. (string=? (file-system-mount-point fs) "/"))
  448. (define (device-string->file-system-device device-string)
  449. ;; The "--root=SPEC" kernel command-line option always provides a
  450. ;; string, but the string can represent a device, an nfs-root, a UUID, or a
  451. ;; label. So check for all four.
  452. (cond ((string-prefix? "/" device-string) device-string)
  453. ((string-contains device-string ":/") device-string) ; nfs-root
  454. ((uuid device-string) => identity)
  455. (else (file-system-label device-string))))
  456. (display "Welcome, this is GNU's early boot Guile.\n")
  457. (display "Use '--repl' for an initrd REPL.\n\n")
  458. (call-with-error-handling
  459. (lambda ()
  460. (mount-essential-file-systems)
  461. (let* ((args (linux-command-line))
  462. (to-load (find-long-option "--load" args))
  463. ;; If present, ‘--root’ on the kernel command line takes precedence
  464. ;; over the ‘device’ field of the root <file-system> record.
  465. (root-device (and=> (find-long-option "--root" args)
  466. device-string->file-system-device))
  467. (root-fs (or (find root-mount-point? mounts)
  468. ;; Fall back to fictitious defaults.
  469. (file-system (device (or root-device "/dev/root"))
  470. (mount-point "/")
  471. (type "ext4"))))
  472. (fsck.mode (find-long-option "fsck.mode" args)))
  473. (define (check? fs)
  474. (match fsck.mode
  475. ("skip" #f)
  476. ("force" #t)
  477. (_ (file-system-check? fs)))) ; assume "auto"
  478. (define (skip-check-if-clean? fs)
  479. (match fsck.mode
  480. ("force" #f)
  481. (_ (file-system-skip-check-if-clean? fs))))
  482. (define (repair fs)
  483. (let ((arg (find-long-option "fsck.repair" args)))
  484. (if arg
  485. (match arg
  486. ("no" #f)
  487. ("yes" #t)
  488. (_ 'preen))
  489. (file-system-repair fs))))
  490. (when (member "--repl" args)
  491. (start-repl))
  492. (display "loading kernel modules...\n")
  493. (load-linux-modules-from-directory linux-modules
  494. linux-module-directory)
  495. (unless (or (member "hibernate=noresume" args)
  496. ;; Also handle the equivalent old-style argument.
  497. ;; See Documentation/admin-guide/kernel-parameters.txt.
  498. (member "noresume" args))
  499. ;; Try to resume immediately after loading (storage) modules
  500. ;; but before any on-disk file systems have been mounted.
  501. (false-if-exception ; failure is not fatal
  502. (resume-if-hibernated (find-long-option "resume" args))))
  503. (when keymap-file
  504. (let ((status (system* "loadkeys" keymap-file)))
  505. (unless (zero? status)
  506. ;; Emit a warning rather than abort when we cannot load
  507. ;; KEYMAP-FILE.
  508. (format (current-error-port)
  509. "warning: 'loadkeys' exited with status ~a~%"
  510. status))))
  511. (when qemu-guest-networking?
  512. (unless (configure-qemu-networking)
  513. (display "network interface is DOWN\n")))
  514. ;; A big ugly hammer, to be used only for debugging and in desperate
  515. ;; situations where no proper device synchonisation is possible.
  516. (let ((root-delay (and=> (find-long-option "rootdelay" args)
  517. string->number)))
  518. (when root-delay
  519. (format #t
  520. "Pausing for rootdelay=~a seconds before mounting the root file system...\n"
  521. root-delay)
  522. (sleep root-delay)))
  523. ;; Prepare the real root file system under /root.
  524. (unless (file-exists? "/root")
  525. (mkdir "/root"))
  526. (when (procedure? pre-mount)
  527. ;; Do whatever actions are needed before mounting the root file
  528. ;; system--e.g., installing device mappings. Error out when the
  529. ;; return value is false.
  530. (unless (pre-mount)
  531. (error "pre-mount actions failed")))
  532. (setenv "EXT2FS_NO_MTAB_OK" "1")
  533. (if root-device
  534. (mount-root-file-system (canonicalize-device-spec root-device)
  535. (file-system-type root-fs)
  536. #:volatile-root? volatile-root?
  537. #:flags (mount-flags->bit-mask
  538. (file-system-flags root-fs))
  539. #:options (file-system-options root-fs)
  540. #:check? (check? root-fs)
  541. #:skip-check-if-clean?
  542. (skip-check-if-clean? root-fs)
  543. #:repair (repair root-fs))
  544. (mount "none" "/root" "tmpfs"))
  545. ;; Mount the specified non-root file systems.
  546. (for-each (lambda (fs)
  547. (mount-file-system fs
  548. #:check? (check? fs)
  549. #:skip-check-if-clean?
  550. (skip-check-if-clean? fs)
  551. #:repair (repair fs)))
  552. (remove root-mount-point? mounts))
  553. (setenv "EXT2FS_NO_MTAB_OK" #f)
  554. (if to-load
  555. (begin
  556. (switch-root "/root")
  557. (format #t "loading '~a'...\n" to-load)
  558. (primitive-load to-load)
  559. (format (current-error-port)
  560. "boot program '~a' terminated, rebooting~%"
  561. to-load)
  562. (sleep 2)
  563. (reboot))
  564. (begin
  565. (display "no boot file passed via '--load'\n")
  566. (display "entering a warm and cozy REPL\n")
  567. (start-repl)))))
  568. #:on-error on-error))
  569. ;;; linux-boot.scm ends here