1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (guix build linux-initrd)
20 #:use-module (rnrs io ports)
21 #:use-module (system foreign)
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)
27 #:use-module (ice-9 ftw)
28 #:use-module (guix build utils)
29 #:export (mount-essential-file-systems
31 make-essential-device-nodes
32 configure-qemu-networking
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.
49 (define* (mount-essential-file-systems #:key (root "/"))
50 "Mount /proc and /sys under ROOT."
53 (if (string-suffix? "/" root)
58 (unless (file-exists? (scope "proc"))
59 (mkdir (scope "proc")))
60 (mount "none" (scope "proc") "proc")
62 (unless (file-exists? (scope "sys"))
63 (mkdir (scope "sys")))
64 (mount "none" (scope "sys") "sysfs"))
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)
72 (mount dir target "" MS_MOVE)))
75 (define (linux-command-line)
76 "Return the Linux kernel command line as a list of strings."
78 (call-with-input-file "/proc/cmdline"
81 (define* (make-essential-device-nodes #:key (root "/"))
82 "Make essential device nodes under ROOT/dev."
83 ;; The hand-made udev!
87 (if (string-suffix? "/" root)
92 (unless (file-exists? (scope "dev"))
93 (mkdir (scope "dev")))
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))
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))
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))
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))
117 (mknod (scope "dev/tty") 'char-special #o600
119 (chmod (scope "dev/tty") #o666)
122 (let ((name (format #f "dev/tty~a" n)))
123 (mknod (scope name) 'char-special #o600
128 (mknod (scope "dev/ptmx") 'char-special #o666
130 (chmod (scope "dev/ptmx") #o666)
132 ;; Create /dev/pts; it will be mounted later, at boot time.
133 (unless (file-exists? (scope "dev/pts"))
134 (mkdir (scope "dev/pts")))
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))
140 ;; Other useful nodes, notably relied on by guix-daemon.
141 (for-each (match-lambda
143 (mknod (scope file) 'char-special #o666
144 (device-number major minor))
145 (chmod (scope file) #o666)))
150 ("dev/urandom" 1 9)))
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"))
155 (symlink "/proc/self/fd/2" (scope "dev/stderr"))
157 ;; File systems in user space (FUSE).
158 (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
160 (define %host-qemu-ipv4-address
161 (inet-pton AF_INET "10.0.2.10"))
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))
174 (unless (file-exists? "/etc")
176 (call-with-output-file "/etc/resolv.conf"
178 (display "nameserver 10.0.2.3\n" p)))
180 (logand (network-interface-flags sock interface) IFF_UP)))
182 ;; Linux mount flags, from libc's <sys/mount.h>.
184 (define MS_BIND 4096)
185 (define MS_MOVE 8192)
187 (define (bind-mount source target)
188 "Bind-mount SOURCE at TARGET."
189 (mount source target "" MS_BIND))
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))
196 (load-linux-module (slurp file)))
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))
203 (define* (mount-root-file-system root type
204 #:key volatile-root? (unionfs "unionfs"))
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
212 (mkdir-p "/real-root")
213 (mount root "/real-root" type MS_RDONLY)
215 (mount "none" "/rw-root" "tmpfs")
217 ;; We want read-write /dev nodes.
218 (make-essential-device-nodes #:root "/rw-root")
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"
225 (error "unionfs failed")))
227 (check-file-system root type)
228 (mount root "/root" type))))
230 (format (current-error-port) "exception while mounting '~a': ~s~%"
234 (copy-file "/proc/mounts" "/root/etc/mtab"))
236 (define (check-file-system device type)
237 "Run a file system check of TYPE on DEVICE."
239 (string-append "fsck." type))
241 (let ((status (system* fsck "-v" "-p" device)))
242 (match (status:exit-val status)
246 (format (current-error-port) "'~a' corrected errors on ~a; continuing~%"
249 (format (current-error-port) "'~a' corrected errors on ~a; rebooting~%"
254 (format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%"
258 (define* (mount-file-system spec #:key (root "/root"))
259 "Mount the file system described by SPEC under ROOT. SPEC must have the
262 (DEVICE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?)
264 DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f;
265 FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to
266 run a file system check."
267 (define flags->bit-mask
269 (('read-only rest ...)
270 (or MS_RDONLY (flags->bit-mask rest)))
271 (('bind-mount rest ...)
272 (or MS_BIND (flags->bit-mask rest)))
277 ((source mount-point type (flags ...) options check?)
278 (let ((mount-point (string-append root "/" mount-point)))
280 (check-file-system source type))
281 (mkdir-p mount-point)
282 (mount source mount-point type (flags->bit-mask flags)
284 (string->pointer options)
288 (mkdir-p (string-append root "/etc"))
289 (let ((port (open-file (string-append root "/etc/mtab") "a")))
290 (format port "~a ~a ~a ~a 0 0~%"
291 source mount-point type options)
292 (close-port port))))))
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)
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))
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)))))
321 ;; Make ROOT the new root.
322 (mount root "/" "" MS_MOVE)
326 (when (file-exists? "/dev/console")
327 ;; Close the standard file descriptors since they refer to the old
328 ;; /dev/console, and reopen them.
329 (let ((console (open-file "/dev/console" "r+b0")))
330 (for-each close-fdes '(0 1 2))
332 (dup2 (fileno console) 0)
333 (dup2 (fileno console) 1)
334 (dup2 (fileno console) 2)
336 (close-port console))))
338 (define* (boot-system #:key
340 qemu-guest-networking?
341 guile-modules-in-chroot?
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'.
350 Mount the root file system, specified by the '--root' command-line argument,
353 MOUNTS must be a list suitable for 'mount-file-system'.
355 When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
358 When VOLATILE-ROOT? is true, the root file system is writable but any changes
360 (define (resolve file)
361 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
363 (let ((st (lstat file)))
364 (if (eq? 'symlink (stat:type st))
365 (let ((target (readlink file)))
366 (resolve (string-append "/root" target)))
369 (define root-mount-point?
371 ((device "/" _ ...) #t)
375 (or (any (match-lambda
376 ((device "/" type _ ...) type)
381 (display "Welcome, this is GNU's early boot Guile.\n")
382 (display "Use '--repl' for an initrd REPL.\n\n")
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 <>)
391 (substring arg (+ 1 (string-index arg #\=))))))))
392 (to-load (option "--load"))
393 (root (option "--root")))
395 (when (member "--repl" args)
398 (display "loading kernel modules...\n")
399 (for-each (compose load-linux-module*
400 (cut string-append "/modules/" <>))
403 (when qemu-guest-networking?
404 (unless (configure-qemu-networking)
405 (display "network interface is DOWN\n")))
408 (make-essential-device-nodes)
410 ;; Prepare the real root file system under /root.
411 (unless (file-exists? "/root")
414 (mount-root-file-system root root-fs-type
415 #:volatile-root? volatile-root?)
416 (mount "none" "/root" "tmpfs"))
418 (unless (file-exists? "/root/dev")
420 (make-essential-device-nodes #:root "/root"))
422 ;; Mount the specified file systems.
423 (for-each mount-file-system
424 (remove root-mount-point? mounts))
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")))
441 (switch-root "/root")
442 (format #t "loading '~a'...\n" to-load)
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")
449 ;; TODO: Remove /lib, /share, and /loader.go.
452 (primitive-load to-load))
454 (format (current-error-port) "'~a' raised an exception: ~s~%"
457 (format (current-error-port)
458 "boot program '~a' terminated, rebooting~%"
463 (display "no boot file passed via '--load'\n")
464 (display "entering a warm and cozy REPL\n")
467 ;;; linux-initrd.scm ends here