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 (linux-command-line)
67 "Return the Linux kernel command line as a list of strings."
69 (call-with-input-file "/proc/cmdline"
72 (define* (make-essential-device-nodes #:key (root "/"))
73 "Make essential device nodes under ROOT/dev."
74 ;; The hand-made udev!
78 (if (string-suffix? "/" root)
83 (unless (file-exists? (scope "dev"))
84 (mkdir (scope "dev")))
86 ;; Make the device nodes for SCSI disks.
87 (mknod (scope "dev/sda") 'block-special #o644 (device-number 8 0))
88 (mknod (scope "dev/sda1") 'block-special #o644 (device-number 8 1))
89 (mknod (scope "dev/sda2") 'block-special #o644 (device-number 8 2))
91 ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
92 (mknod (scope "dev/vda") 'block-special #o644 (device-number 252 0))
93 (mknod (scope "dev/vda1") 'block-special #o644 (device-number 252 1))
94 (mknod (scope "dev/vda2") 'block-special #o644 (device-number 252 2))
96 ;; Memory (used by Xorg's VESA driver.)
97 (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
98 (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
100 ;; Inputs (used by Xorg.)
101 (unless (file-exists? (scope "dev/input"))
102 (mkdir (scope "dev/input")))
103 (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
104 (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
105 (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
108 (mknod (scope "dev/tty") 'char-special #o600
112 (let ((name (format #f "dev/tty~a" n)))
113 (mknod (scope name) 'char-special #o600
118 (mknod (scope "dev/ptmx") 'char-special #o666
121 (unless (file-exists? (scope "dev/pts"))
122 (mkdir (scope "dev/pts")))
123 (mount "none" (scope "dev/pts") "devpts")
125 ;; Rendez-vous point for syslogd.
126 (mknod (scope "dev/log") 'socket #o666 0)
127 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
129 ;; Other useful nodes.
130 (mknod (scope "dev/null") 'char-special #o666 (device-number 1 3))
131 (mknod (scope "dev/zero") 'char-special #o666 (device-number 1 5))
132 (chmod (scope "dev/null") #o666)
133 (chmod (scope "dev/zero") #o666))
135 (define %host-qemu-ipv4-address
136 (inet-pton AF_INET "10.0.2.10"))
138 (define* (configure-qemu-networking #:optional (interface "eth0"))
139 "Setup the INTERFACE network interface and /etc/resolv.conf according to
140 QEMU's default networking settings (see net/slirp.c in QEMU for default
141 networking values.) Return #t if INTERFACE is up, #f otherwise."
142 (display "configuring QEMU networking...\n")
143 (let* ((sock (socket AF_INET SOCK_STREAM 0))
144 (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
145 (flags (network-interface-flags sock interface)))
146 (set-network-interface-address sock interface address)
147 (set-network-interface-flags sock interface (logior flags IFF_UP))
149 (unless (file-exists? "/etc")
151 (call-with-output-file "/etc/resolv.conf"
153 (display "nameserver 10.0.2.3\n" p)))
155 (logand (network-interface-flags sock interface) IFF_UP)))
157 (define (mount-qemu-smb-share share mount-point)
158 "Mount QEMU's CIFS/SMB SHARE at MOUNT-POINT.
160 Vanilla QEMU's `-smb' option just exports a /qemu share, whereas our
161 `qemu-with-multiple-smb-shares' package exports the /xchg and /store shares
162 (the latter allows the store to be shared between the host and guest.)"
164 (format #t "mounting QEMU's SMB share `~a'...\n" share)
165 (let ((server "10.0.2.4"))
166 (mount (string-append "//" server share) mount-point "cifs" 0
167 (string->pointer "guest,sec=none"))))
169 (define (mount-qemu-9p source mount-point)
170 "Mount QEMU's 9p file system from SOURCE at MOUNT-POINT.
172 This uses the 'virtio' transport, which requires the various virtio Linux
173 modules to be loaded."
175 (format #t "mounting QEMU's 9p share '~a'...\n" source)
176 (let ((server "10.0.2.4"))
177 (mount source mount-point "9p" 0
178 (string->pointer "trans=virtio"))))
180 (define (bind-mount source target)
181 "Bind-mount SOURCE at TARGET."
182 (define MS_BIND 4096) ; from libc's <sys/mount.h>
184 (mount source target "" MS_BIND))
186 (define (load-linux-module* file)
187 "Load Linux module from FILE, the name of a `.ko' file."
188 (define (slurp module)
189 (call-with-input-file file get-bytevector-all))
191 (load-linux-module (slurp file)))
193 (define (device-number major minor)
194 "Return the device number for the device with MAJOR and MINOR, for use as
195 the last argument of `mknod'."
196 (+ (* major 256) minor))
198 (define* (boot-system #:key
200 qemu-guest-networking?
201 guile-modules-in-chroot?
204 "This procedure is meant to be called from an initrd. Boot a system by
205 first loading LINUX-MODULES, then setting up QEMU guest networking if
206 QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
207 and finally booting into the new root if any. The initrd supports kernel
208 command-line options '--load', '--root', and '--repl'.
210 MOUNTS must be a list of elements of the form:
212 (FILE-SYSTEM-TYPE SOURCE TARGET)
214 When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
217 When VOLATILE-ROOT? is true, the root file system is writable but any changes
219 (define (resolve file)
220 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
222 (let ((st (lstat file)))
223 (if (eq? 'symlink (stat:type st))
224 (let ((target (readlink file)))
225 (resolve (string-append "/root" target)))
230 (display "Welcome, this is GNU's early boot Guile.\n")
231 (display "Use '--repl' for an initrd REPL.\n\n")
233 (mount-essential-file-systems)
234 (let* ((args (linux-command-line))
235 (option (lambda (opt)
236 (let ((opt (string-append opt "=")))
237 (and=> (find (cut string-prefix? opt <>)
240 (substring arg (+ 1 (string-index arg #\=))))))))
241 (to-load (option "--load"))
242 (root (option "--root")))
244 (when (member "--repl" args)
247 (display "loading kernel modules...\n")
248 (for-each (compose load-linux-module*
249 (cut string-append "/modules/" <>))
252 (when qemu-guest-networking?
253 (unless (configure-qemu-networking)
254 (display "network interface is DOWN\n")))
257 (make-essential-device-nodes)
259 ;; Prepare the real root file system under /root.
260 (unless (file-exists? "/root")
267 ;; XXX: For lack of a union file system...
268 (mkdir-p "/real-root")
269 (mount root "/real-root" "ext3" MS_RDONLY)
270 (mount "none" "/root" "tmpfs")
272 ;; XXX: 'copy-recursively' cannot deal with device nodes, so
273 ;; explicitly avoid /dev.
274 (for-each (lambda (file)
275 (unless (string=? "dev" file)
276 (copy-recursively (string-append "/real-root/"
278 (string-append "/root/"
280 #:log (%make-void-port
282 (scandir "/real-root"
284 (not (member file '("." ".."))))))
286 ;; TODO: Unmount /real-root.
288 (mount root "/root" "ext3")))
290 (format (current-error-port) "exception while mounting '~a': ~s~%"
293 (mount "none" "/root" "tmpfs"))
295 (mount-essential-file-systems #:root "/root")
297 (unless (file-exists? "/root/dev")
299 (make-essential-device-nodes #:root "/root"))
301 ;; Mount the specified file systems.
302 (for-each (match-lambda
303 (('cifs source target)
304 (let ((target (string-append "/root/" target)))
306 (mount-qemu-smb-share source target)))
308 (let ((target (string-append "/root/" target)))
310 (mount-qemu-9p source target))))
313 (when guile-modules-in-chroot?
314 ;; Copy the directories that contain .scm and .go files so that the
315 ;; child process in the chroot can load modules (we would bind-mount
316 ;; them but for some reason that fails with EINVAL -- XXX).
317 (mkdir-p "/root/share")
318 (mkdir-p "/root/lib")
319 (mount "none" "/root/share" "tmpfs")
320 (mount "none" "/root/lib" "tmpfs")
321 (copy-recursively "/share" "/root/share"
322 #:log (%make-void-port "w"))
323 (copy-recursively "/lib" "/root/lib"
324 #:log (%make-void-port "w")))
328 (format #t "loading '~a'...\n" to-load)
331 ;; TODO: Remove /lib, /share, and /loader.go.
334 (primitive-load to-load))
336 (format (current-error-port) "'~a' raised an exception: ~s~%"
339 (format (current-error-port)
340 "boot program '~a' terminated, rebooting~%"
345 (display "no boot file passed via '--load'\n")
346 (display "entering a warm and cozy REPL\n")
349 ;;; linux-initrd.scm ends here