Commit | Line | Data |
---|---|---|
88840f02 | 1 | ;;; GNU Guix --- Functional package management for GNU |
b97c95eb | 2 | ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org> |
88840f02 LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (guix build linux-initrd) | |
20 | #:use-module (rnrs io ports) | |
21 | #:use-module (system foreign) | |
d4254711 LC |
22 | #:autoload (system repl repl) (start-repl) |
23 | #:autoload (system base compile) (compile-file) | |
24 | #:use-module (srfi srfi-1) | |
25 | #:use-module (srfi srfi-26) | |
26 | #:use-module (ice-9 match) | |
44ddf33e | 27 | #:use-module (ice-9 ftw) |
d4254711 | 28 | #:use-module (guix build utils) |
88840f02 LC |
29 | #:export (mount-essential-file-systems |
30 | linux-command-line | |
d91712ee | 31 | make-essential-device-nodes |
88840f02 | 32 | configure-qemu-networking |
023f391c | 33 | check-file-system |
83bcd0b8 | 34 | mount-file-system |
89bf140b | 35 | bind-mount |
88840f02 | 36 | load-linux-module* |
d4254711 LC |
37 | device-number |
38 | boot-system)) | |
88840f02 LC |
39 | |
40 | ;;; Commentary: | |
41 | ;;; | |
42 | ;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that | |
43 | ;;; many of these use procedures not yet available in vanilla Guile (`mount', | |
44 | ;;; `load-linux-module', etc.); these are provided by a Guile patch used in | |
45 | ;;; the GNU distribution. | |
46 | ;;; | |
47 | ;;; Code: | |
48 | ||
49 | (define* (mount-essential-file-systems #:key (root "/")) | |
50 | "Mount /proc and /sys under ROOT." | |
51 | (define (scope dir) | |
52 | (string-append root | |
53 | (if (string-suffix? "/" root) | |
54 | "" | |
55 | "/") | |
56 | dir)) | |
57 | ||
58 | (unless (file-exists? (scope "proc")) | |
59 | (mkdir (scope "proc"))) | |
60 | (mount "none" (scope "proc") "proc") | |
61 | ||
62 | (unless (file-exists? (scope "sys")) | |
63 | (mkdir (scope "sys"))) | |
64 | (mount "none" (scope "sys") "sysfs")) | |
65 | ||
1d462832 LC |
66 | (define (move-essential-file-systems root) |
67 | "Move currently mounted essential file systems to ROOT." | |
68 | (for-each (lambda (dir) | |
69 | (let ((target (string-append root dir))) | |
70 | (unless (file-exists? target) | |
71 | (mkdir target)) | |
72 | (mount dir target "" MS_MOVE))) | |
73 | '("/proc" "/sys"))) | |
74 | ||
88840f02 LC |
75 | (define (linux-command-line) |
76 | "Return the Linux kernel command line as a list of strings." | |
77 | (string-tokenize | |
78 | (call-with-input-file "/proc/cmdline" | |
79 | get-string-all))) | |
80 | ||
d91712ee LC |
81 | (define* (make-essential-device-nodes #:key (root "/")) |
82 | "Make essential device nodes under ROOT/dev." | |
83 | ;; The hand-made udev! | |
84 | ||
85 | (define (scope dir) | |
86 | (string-append root | |
87 | (if (string-suffix? "/" root) | |
88 | "" | |
89 | "/") | |
90 | dir)) | |
91 | ||
92 | (unless (file-exists? (scope "dev")) | |
93 | (mkdir (scope "dev"))) | |
94 | ||
fc4bc4b6 LC |
95 | ;; Make the device nodes for SCSI disks. |
96 | (mknod (scope "dev/sda") 'block-special #o644 (device-number 8 0)) | |
97 | (mknod (scope "dev/sda1") 'block-special #o644 (device-number 8 1)) | |
98 | (mknod (scope "dev/sda2") 'block-special #o644 (device-number 8 2)) | |
99 | ||
100 | ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM. | |
101 | (mknod (scope "dev/vda") 'block-special #o644 (device-number 252 0)) | |
102 | (mknod (scope "dev/vda1") 'block-special #o644 (device-number 252 1)) | |
103 | (mknod (scope "dev/vda2") 'block-special #o644 (device-number 252 2)) | |
d91712ee | 104 | |
c04c6ff6 LC |
105 | ;; Memory (used by Xorg's VESA driver.) |
106 | (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1)) | |
107 | (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2)) | |
108 | ||
1c221510 LC |
109 | ;; Inputs (used by Xorg.) |
110 | (unless (file-exists? (scope "dev/input")) | |
111 | (mkdir (scope "dev/input"))) | |
112 | (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63)) | |
113 | (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32)) | |
114 | (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64)) | |
115 | ||
d91712ee | 116 | ;; TTYs. |
29804e6e LC |
117 | (mknod (scope "dev/tty") 'char-special #o600 |
118 | (device-number 5 0)) | |
289773c1 | 119 | (chmod (scope "dev/tty") #o666) |
d91712ee LC |
120 | (let loop ((n 0)) |
121 | (and (< n 50) | |
122 | (let ((name (format #f "dev/tty~a" n))) | |
29804e6e | 123 | (mknod (scope name) 'char-special #o600 |
d91712ee LC |
124 | (device-number 4 n)) |
125 | (loop (+ 1 n))))) | |
126 | ||
c9c88118 LC |
127 | ;; Pseudo ttys. |
128 | (mknod (scope "dev/ptmx") 'char-special #o666 | |
129 | (device-number 5 2)) | |
289773c1 | 130 | (chmod (scope "dev/ptmx") #o666) |
c9c88118 | 131 | |
c865a878 | 132 | ;; Create /dev/pts; it will be mounted later, at boot time. |
c9c88118 LC |
133 | (unless (file-exists? (scope "dev/pts")) |
134 | (mkdir (scope "dev/pts"))) | |
c9c88118 | 135 | |
37c825eb LC |
136 | ;; Rendez-vous point for syslogd. |
137 | (mknod (scope "dev/log") 'socket #o666 0) | |
138 | (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11)) | |
139 | ||
289773c1 LC |
140 | ;; Other useful nodes, notably relied on by guix-daemon. |
141 | (for-each (match-lambda | |
142 | ((file major minor) | |
143 | (mknod (scope file) 'char-special #o666 | |
144 | (device-number major minor)) | |
145 | (chmod (scope file) #o666))) | |
146 | '(("dev/null" 1 3) | |
147 | ("dev/zero" 1 5) | |
148 | ("dev/full" 1 7) | |
149 | ("dev/random" 1 8) | |
150 | ("dev/urandom" 1 9))) | |
151 | ||
152 | (symlink "/proc/self/fd" (scope "dev/fd")) | |
153 | (symlink "/proc/self/fd/0" (scope "dev/stdin")) | |
154 | (symlink "/proc/self/fd/1" (scope "dev/stdout")) | |
1c96c1bb LC |
155 | (symlink "/proc/self/fd/2" (scope "dev/stderr")) |
156 | ||
157 | ;; File systems in user space (FUSE). | |
158 | (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229))) | |
d91712ee | 159 | |
88840f02 LC |
160 | (define %host-qemu-ipv4-address |
161 | (inet-pton AF_INET "10.0.2.10")) | |
162 | ||
163 | (define* (configure-qemu-networking #:optional (interface "eth0")) | |
164 | "Setup the INTERFACE network interface and /etc/resolv.conf according to | |
165 | QEMU's default networking settings (see net/slirp.c in QEMU for default | |
166 | networking values.) Return #t if INTERFACE is up, #f otherwise." | |
167 | (display "configuring QEMU networking...\n") | |
168 | (let* ((sock (socket AF_INET SOCK_STREAM 0)) | |
169 | (address (make-socket-address AF_INET %host-qemu-ipv4-address 0)) | |
170 | (flags (network-interface-flags sock interface))) | |
171 | (set-network-interface-address sock interface address) | |
172 | (set-network-interface-flags sock interface (logior flags IFF_UP)) | |
173 | ||
174 | (unless (file-exists? "/etc") | |
175 | (mkdir "/etc")) | |
176 | (call-with-output-file "/etc/resolv.conf" | |
177 | (lambda (p) | |
178 | (display "nameserver 10.0.2.3\n" p))) | |
179 | ||
180 | (logand (network-interface-flags sock interface) IFF_UP))) | |
181 | ||
83bcd0b8 LC |
182 | ;; Linux mount flags, from libc's <sys/mount.h>. |
183 | (define MS_RDONLY 1) | |
184 | (define MS_BIND 4096) | |
1d462832 | 185 | (define MS_MOVE 8192) |
4919d684 | 186 | |
89bf140b LC |
187 | (define (bind-mount source target) |
188 | "Bind-mount SOURCE at TARGET." | |
89bf140b LC |
189 | (mount source target "" MS_BIND)) |
190 | ||
88840f02 LC |
191 | (define (load-linux-module* file) |
192 | "Load Linux module from FILE, the name of a `.ko' file." | |
193 | (define (slurp module) | |
194 | (call-with-input-file file get-bytevector-all)) | |
195 | ||
196 | (load-linux-module (slurp file))) | |
197 | ||
198 | (define (device-number major minor) | |
199 | "Return the device number for the device with MAJOR and MINOR, for use as | |
200 | the last argument of `mknod'." | |
201 | (+ (* major 256) minor)) | |
202 | ||
83bcd0b8 | 203 | (define* (mount-root-file-system root type |
3c05b4bc | 204 | #:key volatile-root? (unionfs "unionfs")) |
83bcd0b8 LC |
205 | "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT? |
206 | is true, mount ROOT read-only and make it a union with a writable tmpfs using | |
207 | UNIONFS." | |
208 | (catch #t | |
209 | (lambda () | |
210 | (if volatile-root? | |
211 | (begin | |
212 | (mkdir-p "/real-root") | |
213 | (mount root "/real-root" type MS_RDONLY) | |
214 | (mkdir-p "/rw-root") | |
215 | (mount "none" "/rw-root" "tmpfs") | |
216 | ||
217 | ;; We want read-write /dev nodes. | |
218 | (make-essential-device-nodes #:root "/rw-root") | |
219 | ||
220 | ;; Make /root a union of the tmpfs and the actual root. | |
221 | (unless (zero? (system* unionfs "-o" | |
222 | "cow,allow_other,use_ino,suid,dev" | |
223 | "/rw-root=RW:/real-root=RO" | |
224 | "/root")) | |
225 | (error "unionfs failed"))) | |
3c05b4bc LC |
226 | (begin |
227 | (check-file-system root type) | |
228 | (mount root "/root" type)))) | |
83bcd0b8 LC |
229 | (lambda args |
230 | (format (current-error-port) "exception while mounting '~a': ~s~%" | |
231 | root args) | |
b1995341 LC |
232 | (start-repl))) |
233 | ||
234 | (copy-file "/proc/mounts" "/root/etc/mtab")) | |
83bcd0b8 | 235 | |
3c05b4bc LC |
236 | (define (check-file-system device type) |
237 | "Run a file system check of TYPE on DEVICE." | |
238 | (define fsck | |
239 | (string-append "fsck." type)) | |
240 | ||
241 | (let ((status (system* fsck "-v" "-p" device))) | |
242 | (match (status:exit-val status) | |
243 | (0 | |
244 | #t) | |
245 | (1 | |
246 | (format (current-error-port) "'~a' corrected errors on ~a; continuing~%" | |
247 | fsck device)) | |
248 | (2 | |
249 | (format (current-error-port) "'~a' corrected errors on ~a; rebooting~%" | |
250 | fsck device) | |
251 | (sleep 3) | |
252 | (reboot)) | |
253 | (code | |
254 | (format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%" | |
255 | fsck code device) | |
256 | (start-repl))))) | |
257 | ||
83bcd0b8 LC |
258 | (define* (mount-file-system spec #:key (root "/root")) |
259 | "Mount the file system described by SPEC under ROOT. SPEC must have the | |
260 | form: | |
261 | ||
3c05b4bc | 262 | (DEVICE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?) |
83bcd0b8 LC |
263 | |
264 | DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f; | |
3c05b4bc LC |
265 | FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to |
266 | run a file system check." | |
83bcd0b8 LC |
267 | (define flags->bit-mask |
268 | (match-lambda | |
269 | (('read-only rest ...) | |
270 | (or MS_RDONLY (flags->bit-mask rest))) | |
271 | (('bind-mount rest ...) | |
272 | (or MS_BIND (flags->bit-mask rest))) | |
273 | (() | |
274 | 0))) | |
275 | ||
276 | (match spec | |
3c05b4bc | 277 | ((source mount-point type (flags ...) options check?) |
83bcd0b8 | 278 | (let ((mount-point (string-append root "/" mount-point))) |
3c05b4bc LC |
279 | (when check? |
280 | (check-file-system source type)) | |
83bcd0b8 LC |
281 | (mkdir-p mount-point) |
282 | (mount source mount-point type (flags->bit-mask flags) | |
283 | (if options | |
284 | (string->pointer options) | |
b1995341 LC |
285 | %null-pointer)) |
286 | ||
287 | ;; Update /etc/mtab. | |
288 | (mkdir-p (string-append root "/etc")) | |
02139eb9 | 289 | (let ((port (open-file (string-append root "/etc/mtab") "a"))) |
b1995341 LC |
290 | (format port "~a ~a ~a ~a 0 0~%" |
291 | source mount-point type options) | |
292 | (close-port port)))))) | |
83bcd0b8 | 293 | |
1d462832 LC |
294 | (define (switch-root root) |
295 | "Switch to ROOT as the root file system, in a way similar to what | |
296 | util-linux' switch_root(8) does." | |
297 | (move-essential-file-systems root) | |
298 | (chdir root) | |
26a728eb LC |
299 | |
300 | ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd. | |
301 | ;; TODO: Use 'statfs' to check the fs type, like klibc does. | |
302 | (when (or (not (file-exists? "/init")) (directory-exists? "/home")) | |
303 | (format (current-error-port) | |
304 | "The root file system is probably not an initrd; \ | |
305 | bailing out.~%root contents: ~s~%" (scandir "/")) | |
306 | (force-output (current-error-port)) | |
307 | (exit 1)) | |
308 | ||
309 | ;; Delete files from the old root, without crossing mount points (assuming | |
310 | ;; there are no mount points in sub-directories.) That means we're leaving | |
311 | ;; the empty ROOT directory behind us, but that's OK. | |
312 | (let ((root-device (stat:dev (stat "/")))) | |
313 | (for-each (lambda (file) | |
314 | (unless (member file '("." "..")) | |
315 | (let* ((file (string-append "/" file)) | |
316 | (device (stat:dev (lstat file)))) | |
317 | (when (= device root-device) | |
318 | (delete-file-recursively file))))) | |
319 | (scandir "/"))) | |
320 | ||
321 | ;; Make ROOT the new root. | |
1d462832 | 322 | (mount root "/" "" MS_MOVE) |
26a728eb LC |
323 | (chroot ".") |
324 | (chdir "/") | |
325 | ||
326 | (when (file-exists? "/dev/console") | |
327 | ;; Close the standard file descriptors since they refer to the old | |
474b832d LC |
328 | ;; /dev/console, and reopen them. |
329 | (let ((console (open-file "/dev/console" "r+b0"))) | |
330 | (for-each close-fdes '(0 1 2)) | |
331 | ||
332 | (dup2 (fileno console) 0) | |
333 | (dup2 (fileno console) 1) | |
334 | (dup2 (fileno console) 2) | |
335 | ||
336 | (close-port console)))) | |
1d462832 | 337 | |
d4254711 LC |
338 | (define* (boot-system #:key |
339 | (linux-modules '()) | |
340 | qemu-guest-networking? | |
341 | guile-modules-in-chroot? | |
3c05b4bc | 342 | volatile-root? |
d4254711 LC |
343 | (mounts '())) |
344 | "This procedure is meant to be called from an initrd. Boot a system by | |
345 | first loading LINUX-MODULES, then setting up QEMU guest networking if | |
346 | QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS, | |
347 | and finally booting into the new root if any. The initrd supports kernel | |
348 | command-line options '--load', '--root', and '--repl'. | |
349 | ||
3c05b4bc LC |
350 | Mount the root file system, specified by the '--root' command-line argument, |
351 | if any. | |
03ddfaf5 | 352 | |
83bcd0b8 | 353 | MOUNTS must be a list suitable for 'mount-file-system'. |
d4254711 LC |
354 | |
355 | When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in | |
44ddf33e LC |
356 | the new root. |
357 | ||
358 | When VOLATILE-ROOT? is true, the root file system is writable but any changes | |
359 | to it are lost." | |
d4254711 LC |
360 | (define (resolve file) |
361 | ;; If FILE is a symlink to an absolute file name, resolve it as if we were | |
362 | ;; under /root. | |
363 | (let ((st (lstat file))) | |
364 | (if (eq? 'symlink (stat:type st)) | |
365 | (let ((target (readlink file))) | |
366 | (resolve (string-append "/root" target))) | |
367 | file))) | |
368 | ||
3c05b4bc LC |
369 | (define root-mount-point? |
370 | (match-lambda | |
371 | ((device "/" _ ...) #t) | |
372 | (_ #f))) | |
373 | ||
374 | (define root-fs-type | |
375 | (or (any (match-lambda | |
376 | ((device "/" type _ ...) type) | |
377 | (_ #f)) | |
378 | mounts) | |
379 | "ext4")) | |
380 | ||
d4254711 LC |
381 | (display "Welcome, this is GNU's early boot Guile.\n") |
382 | (display "Use '--repl' for an initrd REPL.\n\n") | |
383 | ||
384 | (mount-essential-file-systems) | |
385 | (let* ((args (linux-command-line)) | |
386 | (option (lambda (opt) | |
387 | (let ((opt (string-append opt "="))) | |
388 | (and=> (find (cut string-prefix? opt <>) | |
389 | args) | |
390 | (lambda (arg) | |
391 | (substring arg (+ 1 (string-index arg #\=)))))))) | |
392 | (to-load (option "--load")) | |
393 | (root (option "--root"))) | |
394 | ||
395 | (when (member "--repl" args) | |
396 | (start-repl)) | |
397 | ||
398 | (display "loading kernel modules...\n") | |
399 | (for-each (compose load-linux-module* | |
400 | (cut string-append "/modules/" <>)) | |
401 | linux-modules) | |
402 | ||
403 | (when qemu-guest-networking? | |
404 | (unless (configure-qemu-networking) | |
405 | (display "network interface is DOWN\n"))) | |
406 | ||
407 | ;; Make /dev nodes. | |
408 | (make-essential-device-nodes) | |
409 | ||
410 | ;; Prepare the real root file system under /root. | |
411 | (unless (file-exists? "/root") | |
412 | (mkdir "/root")) | |
413 | (if root | |
83bcd0b8 | 414 | (mount-root-file-system root root-fs-type |
3c05b4bc | 415 | #:volatile-root? volatile-root?) |
d4254711 | 416 | (mount "none" "/root" "tmpfs")) |
44ddf33e | 417 | |
d4254711 LC |
418 | (unless (file-exists? "/root/dev") |
419 | (mkdir "/root/dev") | |
420 | (make-essential-device-nodes #:root "/root")) | |
421 | ||
422 | ;; Mount the specified file systems. | |
3c05b4bc LC |
423 | (for-each mount-file-system |
424 | (remove root-mount-point? mounts)) | |
d4254711 LC |
425 | |
426 | (when guile-modules-in-chroot? | |
427 | ;; Copy the directories that contain .scm and .go files so that the | |
428 | ;; child process in the chroot can load modules (we would bind-mount | |
429 | ;; them but for some reason that fails with EINVAL -- XXX). | |
430 | (mkdir-p "/root/share") | |
431 | (mkdir-p "/root/lib") | |
432 | (mount "none" "/root/share" "tmpfs") | |
433 | (mount "none" "/root/lib" "tmpfs") | |
434 | (copy-recursively "/share" "/root/share" | |
435 | #:log (%make-void-port "w")) | |
436 | (copy-recursively "/lib" "/root/lib" | |
437 | #:log (%make-void-port "w"))) | |
438 | ||
439 | (if to-load | |
440 | (begin | |
1d462832 | 441 | (switch-root "/root") |
26a728eb | 442 | (format #t "loading '~a'...\n" to-load) |
c865a878 LC |
443 | |
444 | ;; Obviously this has to be done each time we boot. Do it from here | |
445 | ;; so that statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) | |
446 | ;; expects (and thus openpty(3) and its users, such as xterm.) | |
447 | (mount "none" "/dev/pts" "devpts") | |
448 | ||
d4254711 LC |
449 | ;; TODO: Remove /lib, /share, and /loader.go. |
450 | (catch #t | |
451 | (lambda () | |
452 | (primitive-load to-load)) | |
453 | (lambda args | |
454 | (format (current-error-port) "'~a' raised an exception: ~s~%" | |
455 | to-load args) | |
456 | (start-repl))) | |
457 | (format (current-error-port) | |
458 | "boot program '~a' terminated, rebooting~%" | |
459 | to-load) | |
460 | (sleep 2) | |
461 | (reboot)) | |
462 | (begin | |
463 | (display "no boot file passed via '--load'\n") | |
464 | (display "entering a warm and cozy REPL\n") | |
465 | (start-repl))))) | |
466 | ||
88840f02 | 467 | ;;; linux-initrd.scm ends here |