linux-boot.scm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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-26)
  28. #:use-module (ice-9 match)
  29. #:use-module (ice-9 rdelim)
  30. #:use-module (ice-9 regex)
  31. #:use-module (ice-9 ftw)
  32. #:use-module (guix build utils)
  33. #:use-module ((guix build syscalls)
  34. #:hide (file-system-type))
  35. #:use-module (gnu build linux-modules)
  36. #:use-module (gnu build file-systems)
  37. #:use-module (gnu system file-systems)
  38. #:export (mount-essential-file-systems
  39. linux-command-line
  40. find-long-option
  41. find-long-options
  42. make-essential-device-nodes
  43. make-static-device-nodes
  44. configure-qemu-networking
  45. device-number
  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. ;; For backwards compatibility, device numbering is a baroque affair.
  124. ;; This is the full 64-bit scheme used by glibc's <sys/sysmacros.h>.
  125. (major (logior (ash (logand #x00000000000fff00 rdev) -8)
  126. (ash (logand #xfffff00000000000 rdev) -32)))
  127. (minor (logior (logand #x00000000000000ff rdev)
  128. (ash (logand #x00000ffffff00000 rdev) -12))))
  129. (format #f "~a:~a" major minor)))
  130. ;; Write the resume DEVICE to this magic file, using the MAJOR:MINOR device
  131. ;; numbers if possible. The kernel will immediately try to resume from it.
  132. (let ((resume "/sys/power/resume"))
  133. (when (file-exists? resume) ; this kernel supports hibernation
  134. ;; Honour the kernel's default device (only) if none other was given.
  135. (let ((major:minor (if device
  136. (or (false-if-exception (string->major:minor
  137. device))
  138. ;; We can't parse it. Maybe the kernel can.
  139. device)
  140. (let ((default (call-with-input-file resume
  141. read-line)))
  142. ;; Don't waste time echoing 0:0 to /sys.
  143. (if (string=? "0:0" default)
  144. #f
  145. default)))))
  146. (when major:minor
  147. (call-with-output-file resume ; may throw an ‘Invalid argument’
  148. (cut display major:minor <>))))))) ; may never return
  149. (define* (make-disk-device-nodes base major #:optional (minor 0))
  150. "Make the block device nodes around BASE (something like \"/root/dev/sda\")
  151. with the given MAJOR number, starting with MINOR."
  152. (mknod base 'block-special #o644 (device-number major minor))
  153. (let loop ((i 1))
  154. (when (< i 16)
  155. (mknod (string-append base (number->string i))
  156. 'block-special #o644 (device-number major (+ minor i)))
  157. (loop (+ i 1)))))
  158. ;; Representation of a /dev node.
  159. (define-record-type <device-node>
  160. (device-node name type major minor module)
  161. device-node?
  162. (name device-node-name)
  163. (type device-node-type)
  164. (major device-node-major)
  165. (minor device-node-minor)
  166. (module device-node-module))
  167. (define (read-static-device-nodes port)
  168. "Read from PORT a list of <device-node> written in the format used by
  169. /lib/modules/*/*.devname files."
  170. (let loop ((line (read-line port)))
  171. (if (eof-object? line)
  172. '()
  173. (match (string-split line #\space)
  174. (((? (cut string-prefix? "#" <>)) _ ...)
  175. (loop (read-line port)))
  176. ((module-name device-name device-spec)
  177. (let* ((device-parts
  178. (string-match "([bc])([0-9][0-9]*):([0-9][0-9]*)"
  179. device-spec))
  180. (type-string (match:substring device-parts 1))
  181. (type (match type-string
  182. ("c" 'char-special)
  183. ("b" 'block-special)))
  184. (major-string (match:substring device-parts 2))
  185. (major (string->number major-string 10))
  186. (minor-string (match:substring device-parts 3))
  187. (minor (string->number minor-string 10)))
  188. (cons (device-node device-name type major minor module-name)
  189. (loop (read-line port)))))
  190. (_
  191. (begin
  192. (format (current-error-port)
  193. "read-static-device-nodes: ignored devname line '~a'~%" line)
  194. (loop (read-line port))))))))
  195. (define* (mkdir-p* dir #:optional (mode #o755))
  196. "This is a variant of 'mkdir-p' that works around
  197. <http://bugs.gnu.org/24659> by passing MODE explicitly in each 'mkdir' call."
  198. (define absolute?
  199. (string-prefix? "/" dir))
  200. (define not-slash
  201. (char-set-complement (char-set #\/)))
  202. (let loop ((components (string-tokenize dir not-slash))
  203. (root (if absolute?
  204. ""
  205. ".")))
  206. (match components
  207. ((head tail ...)
  208. (let ((path (string-append root "/" head)))
  209. (catch 'system-error
  210. (lambda ()
  211. (mkdir path mode)
  212. (loop tail path))
  213. (lambda args
  214. (if (= EEXIST (system-error-errno args))
  215. (loop tail path)
  216. (apply throw args))))))
  217. (() #t))))
  218. (define (report-system-error name . args)
  219. "Report a system error for the file NAME."
  220. (let ((errno (system-error-errno args)))
  221. (format (current-error-port) "could not create '~a': ~a~%" name
  222. (strerror errno))))
  223. ;; Catch a system-error, log it and don't die from it.
  224. (define-syntax-rule (catch-system-error name exp)
  225. (catch 'system-error
  226. (lambda ()
  227. exp)
  228. (lambda args
  229. (apply report-system-error name args))))
  230. ;; Create a device node like the <device-node> passed here on the file system.
  231. (define create-device-node
  232. (match-lambda
  233. (($ <device-node> xname type major minor module)
  234. (let ((name (string-append "/dev/" xname)))
  235. (mkdir-p* (dirname name))
  236. (catch-system-error name
  237. (mknod name type #o600 (device-number major minor)))))))
  238. (define* (make-static-device-nodes linux-release-module-directory)
  239. "Create static device nodes required by the given Linux release.
  240. This is required in order to solve a chicken-or-egg problem:
  241. The Linux kernel has a feature to autoload modules when a device is first
  242. accessed.
  243. And udev has a feature to set the permissions of static nodes correctly
  244. when it is starting up and also to automatically create nodes when hardware
  245. is hotplugged. That leaves universal device files which are not linked to
  246. one specific hardware device. These we have to create."
  247. (let ((devname-name (string-append linux-release-module-directory "/"
  248. "modules.devname")))
  249. (for-each create-device-node
  250. (call-with-input-file devname-name
  251. read-static-device-nodes))))
  252. (define* (make-essential-device-nodes #:optional (root "/"))
  253. "Make essential device nodes under ROOT/dev."
  254. ;; The hand-made devtmpfs/udev!
  255. (define (scope dir)
  256. (string-append root
  257. (if (string-suffix? "/" root)
  258. ""
  259. "/")
  260. dir))
  261. (unless (file-exists? (scope "dev"))
  262. (mkdir (scope "dev")))
  263. ;; Make the device nodes for SCSI disks.
  264. (make-disk-device-nodes (scope "dev/sda") 8)
  265. (make-disk-device-nodes (scope "dev/sdb") 8 16)
  266. (make-disk-device-nodes (scope "dev/sdc") 8 32)
  267. (make-disk-device-nodes (scope "dev/sdd") 8 48)
  268. ;; SCSI CD-ROM devices (aka. "/dev/sr0" etc.).
  269. (mknod (scope "dev/scd0") 'block-special #o644 (device-number 11 0))
  270. (mknod (scope "dev/scd1") 'block-special #o644 (device-number 11 1))
  271. ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
  272. (make-disk-device-nodes (scope "dev/vda") 252)
  273. ;; Memory (used by Xorg's VESA driver.)
  274. (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
  275. (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
  276. ;; Inputs (used by Xorg.)
  277. (unless (file-exists? (scope "dev/input"))
  278. (mkdir (scope "dev/input")))
  279. (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
  280. (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
  281. (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
  282. ;; System console. This node is magically created by the kernel on the
  283. ;; initrd's root, so don't try to create it in that case.
  284. (unless (string=? root "/")
  285. (mknod (scope "dev/console") 'char-special #o600
  286. (device-number 5 1)))
  287. ;; TTYs.
  288. (mknod (scope "dev/tty") 'char-special #o600
  289. (device-number 5 0))
  290. (chmod (scope "dev/tty") #o666)
  291. (let loop ((n 0))
  292. (and (< n 50)
  293. (let ((name (format #f "dev/tty~a" n)))
  294. (mknod (scope name) 'char-special #o600
  295. (device-number 4 n))
  296. (loop (+ 1 n)))))
  297. ;; Serial line.
  298. (mknod (scope "dev/ttyS0") 'char-special #o660
  299. (device-number 4 64))
  300. ;; Pseudo ttys.
  301. (mknod (scope "dev/ptmx") 'char-special #o666
  302. (device-number 5 2))
  303. (chmod (scope "dev/ptmx") #o666)
  304. ;; Create /dev/pts; it will be mounted later, at boot time.
  305. (unless (file-exists? (scope "dev/pts"))
  306. (mkdir (scope "dev/pts")))
  307. ;; Rendez-vous point for syslogd.
  308. (mknod (scope "dev/log") 'socket #o666 0)
  309. (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
  310. ;; Other useful nodes, notably relied on by guix-daemon.
  311. (for-each (match-lambda
  312. ((file major minor)
  313. (mknod (scope file) 'char-special #o666
  314. (device-number major minor))
  315. (chmod (scope file) #o666)))
  316. '(("dev/null" 1 3)
  317. ("dev/zero" 1 5)
  318. ("dev/full" 1 7)
  319. ("dev/random" 1 8)
  320. ("dev/urandom" 1 9)))
  321. (symlink "/proc/self/fd" (scope "dev/fd"))
  322. (symlink "/proc/self/fd/0" (scope "dev/stdin"))
  323. (symlink "/proc/self/fd/1" (scope "dev/stdout"))
  324. (symlink "/proc/self/fd/2" (scope "dev/stderr"))
  325. ;; Loopback devices.
  326. (let loop ((i 0))
  327. (when (< i 8)
  328. (mknod (scope (string-append "dev/loop" (number->string i)))
  329. 'block-special #o660
  330. (device-number 7 i))
  331. (loop (+ 1 i))))
  332. ;; File systems in user space (FUSE).
  333. (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
  334. (define %host-qemu-ipv4-address
  335. (inet-pton AF_INET "10.0.2.10"))
  336. (define* (configure-qemu-networking #:optional (interface "eth0"))
  337. "Setup the INTERFACE network interface and /etc/resolv.conf according to
  338. QEMU's default networking settings (see net/slirp.c in QEMU for default
  339. networking values.) Return #t if INTERFACE is up, #f otherwise."
  340. (display "configuring QEMU networking...\n")
  341. (let* ((sock (socket AF_INET SOCK_STREAM 0))
  342. (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
  343. (flags (network-interface-flags sock interface)))
  344. (set-network-interface-address sock interface address)
  345. (set-network-interface-flags sock interface (logior flags IFF_UP))
  346. ;; Hello! We used to create /etc/resolv.conf here, with "nameserver
  347. ;; 10.0.2.3\n". However, with Linux-libre 3.16, we're getting ENOSPC.
  348. ;; And since it's actually unnecessary, it's gone.
  349. (logand (network-interface-flags sock interface) IFF_UP)))
  350. (define (device-number major minor)
  351. "Return the device number for the device with MAJOR and MINOR, for use as
  352. the last argument of `mknod'."
  353. (+ (* major 256) minor))
  354. (define (pidof program)
  355. "Return the PID of the first presumed instance of PROGRAM."
  356. (let ((program (basename program)))
  357. (find (lambda (pid)
  358. (let ((exe (format #f "/proc/~a/exe" pid)))
  359. (and=> (false-if-exception (readlink exe))
  360. (compose (cut string=? program <>) basename))))
  361. (filter-map string->number (scandir "/proc")))))
  362. (define* (mount-root-file-system root type
  363. #:key volatile-root? (flags 0) options
  364. check?)
  365. "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT? is
  366. true, mount ROOT read-only and make it an overlay with a writable tmpfs using
  367. the kernel built-in overlayfs. FLAGS and OPTIONS indicates the options to use
  368. to mount ROOT, and behave the same as for the `mount' procedure.
  369. If CHECK? is true, first run ROOT's fsck tool (if any) non-interactively."
  370. (if volatile-root?
  371. (begin
  372. (mkdir-p "/real-root")
  373. (mount root "/real-root" type (logior MS_RDONLY flags) options)
  374. (mkdir-p "/rw-root")
  375. (mount "none" "/rw-root" "tmpfs")
  376. ;; Create the upperdir and the workdir of the overlayfs
  377. (mkdir-p "/rw-root/upper")
  378. (mkdir-p "/rw-root/work")
  379. ;; We want read-write /dev nodes.
  380. (mkdir-p "/rw-root/upper/dev")
  381. (mount "none" "/rw-root/upper/dev" "devtmpfs")
  382. ;; Make /root an overlay of the tmpfs and the actual root.
  383. (mount "none" "/root" "overlay" 0
  384. "lowerdir=/real-root,upperdir=/rw-root/upper,workdir=/rw-root/work"))
  385. (begin
  386. (when check?
  387. (check-file-system root type))
  388. (mount root "/root" type flags options)))
  389. ;; Make sure /root/etc/mtab is a symlink to /proc/self/mounts.
  390. (false-if-exception
  391. (delete-file "/root/etc/mtab"))
  392. (mkdir-p "/root/etc")
  393. (symlink "/proc/self/mounts" "/root/etc/mtab"))
  394. (define (switch-root root)
  395. "Switch to ROOT as the root file system, in a way similar to what
  396. util-linux' switch_root(8) does."
  397. (move-essential-file-systems root)
  398. (chdir root)
  399. ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
  400. ;; TODO: Use 'statfs' to check the fs type, like klibc does.
  401. (when (or (not (file-exists? "/init")) (directory-exists? "/home"))
  402. (format (current-error-port)
  403. "The root file system is probably not an initrd; \
  404. bailing out.~%root contents: ~s~%" (scandir "/"))
  405. (force-output (current-error-port))
  406. (exit 1))
  407. ;; Delete files from the old root, without crossing mount points (assuming
  408. ;; there are no mount points in sub-directories.) That means we're leaving
  409. ;; the empty ROOT directory behind us, but that's OK.
  410. (let ((root-device (stat:dev (stat "/"))))
  411. (for-each (lambda (file)
  412. (unless (member file '("." ".."))
  413. (let* ((file (string-append "/" file))
  414. (device (stat:dev (lstat file))))
  415. (when (= device root-device)
  416. (delete-file-recursively file)))))
  417. (scandir "/")))
  418. ;; Make ROOT the new root.
  419. (mount root "/" "" MS_MOVE)
  420. (chroot ".")
  421. (chdir "/")
  422. (when (file-exists? "/dev/console")
  423. ;; Close the standard file descriptors since they refer to the old
  424. ;; /dev/console, and reopen them.
  425. (let ((console (open-file "/dev/console" "r+b0")))
  426. (for-each close-fdes '(0 1 2))
  427. (dup2 (fileno console) 0)
  428. (dup2 (fileno console) 1)
  429. (dup2 (fileno console) 2)
  430. (close-port console))))
  431. (define* (boot-system #:key
  432. (linux-modules '())
  433. linux-module-directory
  434. keymap-file
  435. qemu-guest-networking?
  436. volatile-root?
  437. pre-mount
  438. (mounts '())
  439. (on-error 'debug))
  440. "This procedure is meant to be called from an initrd. Boot a system by
  441. first loading LINUX-MODULES (a list of module names) from
  442. LINUX-MODULE-DIRECTORY, then installing KEYMAP-FILE with 'loadkeys' (if
  443. KEYMAP-FILE is true), then setting up QEMU guest networking if
  444. QEMU-GUEST-NETWORKING? is true, calling PRE-MOUNT, mounting the file systems
  445. specified in MOUNTS, and finally booting into the new root if any. The initrd
  446. supports kernel command-line options '--load', '--root', and '--repl'.
  447. Mount the root file system, specified by the '--root' command-line argument,
  448. if any.
  449. MOUNTS must be a list of <file-system> objects.
  450. When VOLATILE-ROOT? is true, the root file system is writable but any changes
  451. to it are lost.
  452. ON-ERROR is passed to 'call-with-error-handling'; it determines what happens
  453. upon error."
  454. (define (root-mount-point? fs)
  455. (string=? (file-system-mount-point fs) "/"))
  456. (define (device-string->file-system-device device-string)
  457. ;; The "--root=SPEC" kernel command-line option always provides a
  458. ;; string, but the string can represent a device, an nfs-root, a UUID, or a
  459. ;; label. So check for all four.
  460. (cond ((string-prefix? "/" device-string) device-string)
  461. ((string-contains device-string ":/") device-string) ; nfs-root
  462. ((uuid device-string) => identity)
  463. (else (file-system-label device-string))))
  464. (display "Welcome, this is GNU's early boot Guile.\n")
  465. (display "Use '--repl' for an initrd REPL.\n\n")
  466. (call-with-error-handling
  467. (lambda ()
  468. (mount-essential-file-systems)
  469. (let* ((args (linux-command-line))
  470. (to-load (find-long-option "--load" args))
  471. (root-fs (find root-mount-point? mounts))
  472. (root-fs-type (or (and=> root-fs file-system-type)
  473. "ext4"))
  474. (root-fs-device (and=> root-fs file-system-device))
  475. (root-fs-flags (mount-flags->bit-mask
  476. (or (and=> root-fs file-system-flags)
  477. '())))
  478. (root-options (if root-fs
  479. (file-system-options root-fs)
  480. #f))
  481. ;; --root takes precedence over the 'device' field of the root
  482. ;; <file-system> record.
  483. (root-device (or (and=> (find-long-option "--root" args)
  484. device-string->file-system-device)
  485. root-fs-device)))
  486. (when (member "--repl" args)
  487. (start-repl))
  488. (display "loading kernel modules...\n")
  489. (load-linux-modules-from-directory linux-modules
  490. linux-module-directory)
  491. (unless (or (member "hibernate=noresume" args)
  492. ;; Also handle the equivalent old-style argument.
  493. ;; See Documentation/admin-guide/kernel-parameters.txt.
  494. (member "noresume" args))
  495. ;; Try to resume immediately after loading (storage) modules
  496. ;; but before any on-disk file systems have been mounted.
  497. (false-if-exception ; failure is not fatal
  498. (resume-if-hibernated (find-long-option "resume" args))))
  499. (when keymap-file
  500. (let ((status (system* "loadkeys" keymap-file)))
  501. (unless (zero? status)
  502. ;; Emit a warning rather than abort when we cannot load
  503. ;; KEYMAP-FILE.
  504. (format (current-error-port)
  505. "warning: 'loadkeys' exited with status ~a~%"
  506. status))))
  507. (when qemu-guest-networking?
  508. (unless (configure-qemu-networking)
  509. (display "network interface is DOWN\n")))
  510. ;; A big ugly hammer, to be used only for debugging and in desperate
  511. ;; situations where no proper device synchonisation is possible.
  512. (let ((root-delay (and=> (find-long-option "rootdelay" args)
  513. string->number)))
  514. (when root-delay
  515. (format #t
  516. "Pausing for rootdelay=~a seconds before mounting the root file system...\n"
  517. root-delay)
  518. (sleep root-delay)))
  519. ;; Prepare the real root file system under /root.
  520. (unless (file-exists? "/root")
  521. (mkdir "/root"))
  522. (when (procedure? pre-mount)
  523. ;; Do whatever actions are needed before mounting the root file
  524. ;; system--e.g., installing device mappings. Error out when the
  525. ;; return value is false.
  526. (unless (pre-mount)
  527. (error "pre-mount actions failed")))
  528. (setenv "EXT2FS_NO_MTAB_OK" "1")
  529. (if root-device
  530. (mount-root-file-system (canonicalize-device-spec root-device)
  531. root-fs-type
  532. #:volatile-root? volatile-root?
  533. #:flags root-fs-flags
  534. #:options root-options
  535. #:check? (if root-fs
  536. (file-system-check? root-fs)
  537. #t))
  538. (mount "none" "/root" "tmpfs"))
  539. ;; Mount the specified file systems.
  540. (for-each mount-file-system
  541. (remove root-mount-point? mounts))
  542. (setenv "EXT2FS_NO_MTAB_OK" #f)
  543. (if to-load
  544. (begin
  545. (switch-root "/root")
  546. (format #t "loading '~a'...\n" to-load)
  547. (primitive-load to-load)
  548. (format (current-error-port)
  549. "boot program '~a' terminated, rebooting~%"
  550. to-load)
  551. (sleep 2)
  552. (reboot))
  553. (begin
  554. (display "no boot file passed via '--load'\n")
  555. (display "entering a warm and cozy REPL\n")
  556. (start-repl)))))
  557. #:on-error on-error))
  558. ;;; linux-boot.scm ends here