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
41 ;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that
42 ;;; many of these use procedures not yet available in vanilla Guile (`mount',
43 ;;; `load-linux-module', etc.); these are provided by a Guile patch used in
44 ;;; the GNU distribution.
48 (define* (mount-essential-file-systems #:key (root "/"))
49 "Mount /proc and /sys under ROOT."
52 (if (string-suffix? "/" root)
57 (unless (file-exists? (scope "proc"))
58 (mkdir (scope "proc")))
59 (mount "none" (scope "proc") "proc")
61 (unless (file-exists? (scope "sys"))
62 (mkdir (scope "sys")))
63 (mount "none" (scope "sys") "sysfs"))
65 (define (move-essential-file-systems root)
66 "Move currently mounted essential file systems to ROOT."
67 (for-each (lambda (dir)
68 (let ((target (string-append root dir)))
69 (unless (file-exists? target)
71 (mount dir target "" MS_MOVE)))
74 (define (linux-command-line)
75 "Return the Linux kernel command line as a list of strings."
77 (call-with-input-file "/proc/cmdline"
80 (define* (make-essential-device-nodes #:key (root "/"))
81 "Make essential device nodes under ROOT/dev."
82 ;; The hand-made udev!
86 (if (string-suffix? "/" root)
91 (unless (file-exists? (scope "dev"))
92 (mkdir (scope "dev")))
94 ;; Make the device nodes for SCSI disks.
95 (mknod (scope "dev/sda") 'block-special #o644 (device-number 8 0))
96 (mknod (scope "dev/sda1") 'block-special #o644 (device-number 8 1))
97 (mknod (scope "dev/sda2") 'block-special #o644 (device-number 8 2))
99 ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
100 (mknod (scope "dev/vda") 'block-special #o644 (device-number 252 0))
101 (mknod (scope "dev/vda1") 'block-special #o644 (device-number 252 1))
102 (mknod (scope "dev/vda2") 'block-special #o644 (device-number 252 2))
104 ;; Memory (used by Xorg's VESA driver.)
105 (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
106 (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
108 ;; Inputs (used by Xorg.)
109 (unless (file-exists? (scope "dev/input"))
110 (mkdir (scope "dev/input")))
111 (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
112 (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
113 (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
116 (mknod (scope "dev/tty") 'char-special #o600
118 (chmod (scope "dev/tty") #o666)
121 (let ((name (format #f "dev/tty~a" n)))
122 (mknod (scope name) 'char-special #o600
127 (mknod (scope "dev/ptmx") 'char-special #o666
129 (chmod (scope "dev/ptmx") #o666)
131 ;; Create /dev/pts; it will be mounted later, at boot time.
132 (unless (file-exists? (scope "dev/pts"))
133 (mkdir (scope "dev/pts")))
135 ;; Rendez-vous point for syslogd.
136 (mknod (scope "dev/log") 'socket #o666 0)
137 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
139 ;; Other useful nodes, notably relied on by guix-daemon.
140 (for-each (match-lambda
142 (mknod (scope file) 'char-special #o666
143 (device-number major minor))
144 (chmod (scope file) #o666)))
149 ("dev/urandom" 1 9)))
151 (symlink "/proc/self/fd" (scope "dev/fd"))
152 (symlink "/proc/self/fd/0" (scope "dev/stdin"))
153 (symlink "/proc/self/fd/1" (scope "dev/stdout"))
154 (symlink "/proc/self/fd/2" (scope "dev/stderr"))
156 ;; File systems in user space (FUSE).
157 (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
159 (define %host-qemu-ipv4-address
160 (inet-pton AF_INET "10.0.2.10"))
162 (define* (configure-qemu-networking #:optional (interface "eth0"))
163 "Setup the INTERFACE network interface and /etc/resolv.conf according to
164 QEMU's default networking settings (see net/slirp.c in QEMU for default
165 networking values.) Return #t if INTERFACE is up, #f otherwise."
166 (display "configuring QEMU networking...\n")
167 (let* ((sock (socket AF_INET SOCK_STREAM 0))
168 (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
169 (flags (network-interface-flags sock interface)))
170 (set-network-interface-address sock interface address)
171 (set-network-interface-flags sock interface (logior flags IFF_UP))
173 (unless (file-exists? "/etc")
175 (call-with-output-file "/etc/resolv.conf"
177 (display "nameserver 10.0.2.3\n" p)))
179 (logand (network-interface-flags sock interface) IFF_UP)))
181 ;; Linux mount flags, from libc's <sys/mount.h>.
183 (define MS_BIND 4096)
184 (define MS_MOVE 8192)
186 (define (bind-mount source target)
187 "Bind-mount SOURCE at TARGET."
188 (mount source target "" MS_BIND))
190 (define (load-linux-module* file)
191 "Load Linux module from FILE, the name of a `.ko' file."
192 (define (slurp module)
193 (call-with-input-file file get-bytevector-all))
195 (load-linux-module (slurp file)))
197 (define (device-number major minor)
198 "Return the device number for the device with MAJOR and MINOR, for use as
199 the last argument of `mknod'."
200 (+ (* major 256) minor))
202 (define* (mount-root-file-system root type
203 #:key volatile-root? (unionfs "unionfs"))
204 "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT?
205 is true, mount ROOT read-only and make it a union with a writable tmpfs using
211 (mkdir-p "/real-root")
212 (mount root "/real-root" type MS_RDONLY)
214 (mount "none" "/rw-root" "tmpfs")
216 ;; We want read-write /dev nodes.
217 (make-essential-device-nodes #:root "/rw-root")
219 ;; Make /root a union of the tmpfs and the actual root.
220 (unless (zero? (system* unionfs "-o"
221 "cow,allow_other,use_ino,suid,dev"
222 "/rw-root=RW:/real-root=RO"
224 (error "unionfs failed")))
226 (check-file-system root type)
227 (mount root "/root" type))))
229 (format (current-error-port) "exception while mounting '~a': ~s~%"
233 (define (check-file-system device type)
234 "Run a file system check of TYPE on DEVICE."
236 (string-append "fsck." type))
238 (let ((status (system* fsck "-v" "-p" device)))
239 (match (status:exit-val status)
243 (format (current-error-port) "'~a' corrected errors on ~a; continuing~%"
246 (format (current-error-port) "'~a' corrected errors on ~a; rebooting~%"
251 (format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%"
255 (define* (mount-file-system spec #:key (root "/root"))
256 "Mount the file system described by SPEC under ROOT. SPEC must have the
259 (DEVICE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?)
261 DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f;
262 FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to
263 run a file system check."
264 (define flags->bit-mask
266 (('read-only rest ...)
267 (or MS_RDONLY (flags->bit-mask rest)))
268 (('bind-mount rest ...)
269 (or MS_BIND (flags->bit-mask rest)))
274 ((source mount-point type (flags ...) options check?)
275 (let ((mount-point (string-append root "/" mount-point)))
277 (check-file-system source type))
278 (mkdir-p mount-point)
279 (mount source mount-point type (flags->bit-mask flags)
281 (string->pointer options)
284 (define (switch-root root)
285 "Switch to ROOT as the root file system, in a way similar to what
286 util-linux' switch_root(8) does."
287 (move-essential-file-systems root)
290 ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
291 ;; TODO: Use 'statfs' to check the fs type, like klibc does.
292 (when (or (not (file-exists? "/init")) (directory-exists? "/home"))
293 (format (current-error-port)
294 "The root file system is probably not an initrd; \
295 bailing out.~%root contents: ~s~%" (scandir "/"))
296 (force-output (current-error-port))
299 ;; Delete files from the old root, without crossing mount points (assuming
300 ;; there are no mount points in sub-directories.) That means we're leaving
301 ;; the empty ROOT directory behind us, but that's OK.
302 (let ((root-device (stat:dev (stat "/"))))
303 (for-each (lambda (file)
304 (unless (member file '("." ".."))
305 (let* ((file (string-append "/" file))
306 (device (stat:dev (lstat file))))
307 (when (= device root-device)
308 (delete-file-recursively file)))))
311 ;; Make ROOT the new root.
312 (mount root "/" "" MS_MOVE)
316 (when (file-exists? "/dev/console")
317 ;; Close the standard file descriptors since they refer to the old
319 (for-each close-fdes '(0 1 2))
322 (let ((in (open-file "/dev/console" "rbl"))
323 (out (open-file "/dev/console" "wbl")))
325 (dup2 (fileno out) 1)
326 (dup2 (fileno out) 2)
328 ;; Safely close IN and OUT.
329 (for-each (lambda (port)
330 (if (memv (fileno port) '(0 1 2))
331 (set-port-revealed! port 1)
335 (define* (boot-system #:key
337 qemu-guest-networking?
338 guile-modules-in-chroot?
341 "This procedure is meant to be called from an initrd. Boot a system by
342 first loading LINUX-MODULES, then setting up QEMU guest networking if
343 QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
344 and finally booting into the new root if any. The initrd supports kernel
345 command-line options '--load', '--root', and '--repl'.
347 Mount the root file system, specified by the '--root' command-line argument,
350 MOUNTS must be a list suitable for 'mount-file-system'.
352 When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
355 When VOLATILE-ROOT? is true, the root file system is writable but any changes
357 (define (resolve file)
358 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
360 (let ((st (lstat file)))
361 (if (eq? 'symlink (stat:type st))
362 (let ((target (readlink file)))
363 (resolve (string-append "/root" target)))
366 (define root-mount-point?
368 ((device "/" _ ...) #t)
372 (or (any (match-lambda
373 ((device "/" type _ ...) type)
378 (display "Welcome, this is GNU's early boot Guile.\n")
379 (display "Use '--repl' for an initrd REPL.\n\n")
381 (mount-essential-file-systems)
382 (let* ((args (linux-command-line))
383 (option (lambda (opt)
384 (let ((opt (string-append opt "=")))
385 (and=> (find (cut string-prefix? opt <>)
388 (substring arg (+ 1 (string-index arg #\=))))))))
389 (to-load (option "--load"))
390 (root (option "--root")))
392 (when (member "--repl" args)
395 (display "loading kernel modules...\n")
396 (for-each (compose load-linux-module*
397 (cut string-append "/modules/" <>))
400 (when qemu-guest-networking?
401 (unless (configure-qemu-networking)
402 (display "network interface is DOWN\n")))
405 (make-essential-device-nodes)
407 ;; Prepare the real root file system under /root.
408 (unless (file-exists? "/root")
411 (mount-root-file-system root root-fs-type
412 #:volatile-root? volatile-root?)
413 (mount "none" "/root" "tmpfs"))
415 (unless (file-exists? "/root/dev")
417 (make-essential-device-nodes #:root "/root"))
419 ;; Mount the specified file systems.
420 (for-each mount-file-system
421 (remove root-mount-point? mounts))
423 (when guile-modules-in-chroot?
424 ;; Copy the directories that contain .scm and .go files so that the
425 ;; child process in the chroot can load modules (we would bind-mount
426 ;; them but for some reason that fails with EINVAL -- XXX).
427 (mkdir-p "/root/share")
428 (mkdir-p "/root/lib")
429 (mount "none" "/root/share" "tmpfs")
430 (mount "none" "/root/lib" "tmpfs")
431 (copy-recursively "/share" "/root/share"
432 #:log (%make-void-port "w"))
433 (copy-recursively "/lib" "/root/lib"
434 #:log (%make-void-port "w")))
438 (switch-root "/root")
439 (format #t "loading '~a'...\n" to-load)
441 ;; Obviously this has to be done each time we boot. Do it from here
442 ;; so that statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3)
443 ;; expects (and thus openpty(3) and its users, such as xterm.)
444 (mount "none" "/dev/pts" "devpts")
446 ;; TODO: Remove /lib, /share, and /loader.go.
449 (primitive-load to-load))
451 (format (current-error-port) "'~a' raised an exception: ~s~%"
454 (format (current-error-port)
455 "boot program '~a' terminated, rebooting~%"
460 (display "no boot file passed via '--load'\n")
461 (display "entering a warm and cozy REPL\n")
464 ;;; linux-initrd.scm ends here