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