gnu: linux-initrd: Recognize 9p file systems.
[jackhill/guix/guix.git] / guix / build / linux-initrd.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
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)
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 (guix build utils)
28 #:export (mount-essential-file-systems
29 linux-command-line
30 make-essential-device-nodes
31 configure-qemu-networking
32 mount-qemu-smb-share
33 mount-qemu-9p
34 bind-mount
35 load-linux-module*
36 device-number
37 boot-system))
38
39 ;;; Commentary:
40 ;;;
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.
45 ;;;
46 ;;; Code:
47
48 (define* (mount-essential-file-systems #:key (root "/"))
49 "Mount /proc and /sys under ROOT."
50 (define (scope dir)
51 (string-append root
52 (if (string-suffix? "/" root)
53 ""
54 "/")
55 dir))
56
57 (unless (file-exists? (scope "proc"))
58 (mkdir (scope "proc")))
59 (mount "none" (scope "proc") "proc")
60
61 (unless (file-exists? (scope "sys"))
62 (mkdir (scope "sys")))
63 (mount "none" (scope "sys") "sysfs"))
64
65 (define (linux-command-line)
66 "Return the Linux kernel command line as a list of strings."
67 (string-tokenize
68 (call-with-input-file "/proc/cmdline"
69 get-string-all)))
70
71 (define* (make-essential-device-nodes #:key (root "/"))
72 "Make essential device nodes under ROOT/dev."
73 ;; The hand-made udev!
74
75 (define (scope dir)
76 (string-append root
77 (if (string-suffix? "/" root)
78 ""
79 "/")
80 dir))
81
82 (unless (file-exists? (scope "dev"))
83 (mkdir (scope "dev")))
84
85 ;; Make the device nodes for SCSI disks.
86 (mknod (scope "dev/sda") 'block-special #o644 (device-number 8 0))
87 (mknod (scope "dev/sda1") 'block-special #o644 (device-number 8 1))
88 (mknod (scope "dev/sda2") 'block-special #o644 (device-number 8 2))
89
90 ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
91 (mknod (scope "dev/vda") 'block-special #o644 (device-number 252 0))
92 (mknod (scope "dev/vda1") 'block-special #o644 (device-number 252 1))
93 (mknod (scope "dev/vda2") 'block-special #o644 (device-number 252 2))
94
95 ;; TTYs.
96 (mknod (scope "dev/tty") 'char-special #o600
97 (device-number 5 0))
98 (let loop ((n 0))
99 (and (< n 50)
100 (let ((name (format #f "dev/tty~a" n)))
101 (mknod (scope name) 'char-special #o600
102 (device-number 4 n))
103 (loop (+ 1 n)))))
104
105 ;; Rendez-vous point for syslogd.
106 (mknod (scope "dev/log") 'socket #o666 0)
107 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
108
109 ;; Other useful nodes.
110 (mknod (scope "dev/null") 'char-special #o666 (device-number 1 3))
111 (mknod (scope "dev/zero") 'char-special #o666 (device-number 1 5))
112 (chmod (scope "dev/null") #o666)
113 (chmod (scope "dev/zero") #o666))
114
115 (define %host-qemu-ipv4-address
116 (inet-pton AF_INET "10.0.2.10"))
117
118 (define* (configure-qemu-networking #:optional (interface "eth0"))
119 "Setup the INTERFACE network interface and /etc/resolv.conf according to
120 QEMU's default networking settings (see net/slirp.c in QEMU for default
121 networking values.) Return #t if INTERFACE is up, #f otherwise."
122 (display "configuring QEMU networking...\n")
123 (let* ((sock (socket AF_INET SOCK_STREAM 0))
124 (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
125 (flags (network-interface-flags sock interface)))
126 (set-network-interface-address sock interface address)
127 (set-network-interface-flags sock interface (logior flags IFF_UP))
128
129 (unless (file-exists? "/etc")
130 (mkdir "/etc"))
131 (call-with-output-file "/etc/resolv.conf"
132 (lambda (p)
133 (display "nameserver 10.0.2.3\n" p)))
134
135 (logand (network-interface-flags sock interface) IFF_UP)))
136
137 (define (mount-qemu-smb-share share mount-point)
138 "Mount QEMU's CIFS/SMB SHARE at MOUNT-POINT.
139
140 Vanilla QEMU's `-smb' option just exports a /qemu share, whereas our
141 `qemu-with-multiple-smb-shares' package exports the /xchg and /store shares
142 (the latter allows the store to be shared between the host and guest.)"
143
144 (format #t "mounting QEMU's SMB share `~a'...\n" share)
145 (let ((server "10.0.2.4"))
146 (mount (string-append "//" server share) mount-point "cifs" 0
147 (string->pointer "guest,sec=none"))))
148
149 (define (mount-qemu-9p source mount-point)
150 "Mount QEMU's 9p file system from SOURCE at MOUNT-POINT.
151
152 This uses the 'virtio' transport, which requires the various virtio Linux
153 modules to be loaded."
154
155 (format #t "mounting QEMU's 9p share '~a'...\n" source)
156 (let ((server "10.0.2.4"))
157 (mount source mount-point "9p" 0
158 (string->pointer "trans=virtio"))))
159
160 (define (bind-mount source target)
161 "Bind-mount SOURCE at TARGET."
162 (define MS_BIND 4096) ; from libc's <sys/mount.h>
163
164 (mount source target "" MS_BIND))
165
166 (define (load-linux-module* file)
167 "Load Linux module from FILE, the name of a `.ko' file."
168 (define (slurp module)
169 (call-with-input-file file get-bytevector-all))
170
171 (load-linux-module (slurp file)))
172
173 (define (device-number major minor)
174 "Return the device number for the device with MAJOR and MINOR, for use as
175 the last argument of `mknod'."
176 (+ (* major 256) minor))
177
178 (define* (boot-system #:key
179 (linux-modules '())
180 qemu-guest-networking?
181 guile-modules-in-chroot?
182 (mounts '()))
183 "This procedure is meant to be called from an initrd. Boot a system by
184 first loading LINUX-MODULES, then setting up QEMU guest networking if
185 QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
186 and finally booting into the new root if any. The initrd supports kernel
187 command-line options '--load', '--root', and '--repl'.
188
189 MOUNTS must be a list of elements of the form:
190
191 (FILE-SYSTEM-TYPE SOURCE TARGET)
192
193 When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
194 the new root."
195 (define (resolve file)
196 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
197 ;; under /root.
198 (let ((st (lstat file)))
199 (if (eq? 'symlink (stat:type st))
200 (let ((target (readlink file)))
201 (resolve (string-append "/root" target)))
202 file)))
203
204 (display "Welcome, this is GNU's early boot Guile.\n")
205 (display "Use '--repl' for an initrd REPL.\n\n")
206
207 (mount-essential-file-systems)
208 (let* ((args (linux-command-line))
209 (option (lambda (opt)
210 (let ((opt (string-append opt "=")))
211 (and=> (find (cut string-prefix? opt <>)
212 args)
213 (lambda (arg)
214 (substring arg (+ 1 (string-index arg #\=))))))))
215 (to-load (option "--load"))
216 (root (option "--root")))
217
218 (when (member "--repl" args)
219 (start-repl))
220
221 (display "loading kernel modules...\n")
222 (for-each (compose load-linux-module*
223 (cut string-append "/modules/" <>))
224 linux-modules)
225
226 (when qemu-guest-networking?
227 (unless (configure-qemu-networking)
228 (display "network interface is DOWN\n")))
229
230 ;; Make /dev nodes.
231 (make-essential-device-nodes)
232
233 ;; Prepare the real root file system under /root.
234 (unless (file-exists? "/root")
235 (mkdir "/root"))
236 (if root
237 (catch #t
238 (lambda ()
239 (mount root "/root" "ext3"))
240 (lambda args
241 (format (current-error-port) "exception while mounting '~a': ~s~%"
242 root args)
243 (start-repl)))
244 (mount "none" "/root" "tmpfs"))
245 (mount-essential-file-systems #:root "/root")
246
247 (unless (file-exists? "/root/dev")
248 (mkdir "/root/dev")
249 (make-essential-device-nodes #:root "/root"))
250
251 ;; Mount the specified file systems.
252 (for-each (match-lambda
253 (('cifs source target)
254 (let ((target (string-append "/root/" target)))
255 (mkdir-p target)
256 (mount-qemu-smb-share source target)))
257 (('9p source target)
258 (let ((target (string-append "/root/" target)))
259 (mkdir-p target)
260 (mount-qemu-9p source target))))
261 mounts)
262
263 (when guile-modules-in-chroot?
264 ;; Copy the directories that contain .scm and .go files so that the
265 ;; child process in the chroot can load modules (we would bind-mount
266 ;; them but for some reason that fails with EINVAL -- XXX).
267 (mkdir-p "/root/share")
268 (mkdir-p "/root/lib")
269 (mount "none" "/root/share" "tmpfs")
270 (mount "none" "/root/lib" "tmpfs")
271 (copy-recursively "/share" "/root/share"
272 #:log (%make-void-port "w"))
273 (copy-recursively "/lib" "/root/lib"
274 #:log (%make-void-port "w")))
275
276 (if to-load
277 (begin
278 (format #t "loading '~a'...\n" to-load)
279 (chroot "/root")
280 ;; TODO: Remove /lib, /share, and /loader.go.
281 (catch #t
282 (lambda ()
283 (primitive-load to-load))
284 (lambda args
285 (format (current-error-port) "'~a' raised an exception: ~s~%"
286 to-load args)
287 (start-repl)))
288 (format (current-error-port)
289 "boot program '~a' terminated, rebooting~%"
290 to-load)
291 (sleep 2)
292 (reboot))
293 (begin
294 (display "no boot file passed via '--load'\n")
295 (display "entering a warm and cozy REPL\n")
296 (start-repl)))))
297
298 ;;; linux-initrd.scm ends here