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