gnu: linux-initrd: Better populate /dev.
[jackhill/guix/guix.git] / guix / build / linux-initrd.scm
CommitLineData
88840f02 1;;; GNU Guix --- Functional package management for GNU
b97c95eb 2;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
88840f02
LC
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)
d4254711
LC
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)
44ddf33e 27 #:use-module (ice-9 ftw)
d4254711 28 #:use-module (guix build utils)
88840f02
LC
29 #:export (mount-essential-file-systems
30 linux-command-line
d91712ee 31 make-essential-device-nodes
88840f02
LC
32 configure-qemu-networking
33 mount-qemu-smb-share
4919d684 34 mount-qemu-9p
89bf140b 35 bind-mount
88840f02 36 load-linux-module*
d4254711
LC
37 device-number
38 boot-system))
88840f02
LC
39
40;;; Commentary:
41;;;
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.
46;;;
47;;; Code:
48
49(define* (mount-essential-file-systems #:key (root "/"))
50 "Mount /proc and /sys under ROOT."
51 (define (scope dir)
52 (string-append root
53 (if (string-suffix? "/" root)
54 ""
55 "/")
56 dir))
57
58 (unless (file-exists? (scope "proc"))
59 (mkdir (scope "proc")))
60 (mount "none" (scope "proc") "proc")
61
62 (unless (file-exists? (scope "sys"))
63 (mkdir (scope "sys")))
64 (mount "none" (scope "sys") "sysfs"))
65
66(define (linux-command-line)
67 "Return the Linux kernel command line as a list of strings."
68 (string-tokenize
69 (call-with-input-file "/proc/cmdline"
70 get-string-all)))
71
d91712ee
LC
72(define* (make-essential-device-nodes #:key (root "/"))
73 "Make essential device nodes under ROOT/dev."
74 ;; The hand-made udev!
75
76 (define (scope dir)
77 (string-append root
78 (if (string-suffix? "/" root)
79 ""
80 "/")
81 dir))
82
83 (unless (file-exists? (scope "dev"))
84 (mkdir (scope "dev")))
85
fc4bc4b6
LC
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))
90
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))
d91712ee 95
c04c6ff6
LC
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))
99
1c221510
LC
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))
106
d91712ee 107 ;; TTYs.
29804e6e
LC
108 (mknod (scope "dev/tty") 'char-special #o600
109 (device-number 5 0))
289773c1 110 (chmod (scope "dev/tty") #o666)
d91712ee
LC
111 (let loop ((n 0))
112 (and (< n 50)
113 (let ((name (format #f "dev/tty~a" n)))
29804e6e 114 (mknod (scope name) 'char-special #o600
d91712ee
LC
115 (device-number 4 n))
116 (loop (+ 1 n)))))
117
c9c88118
LC
118 ;; Pseudo ttys.
119 (mknod (scope "dev/ptmx") 'char-special #o666
120 (device-number 5 2))
289773c1 121 (chmod (scope "dev/ptmx") #o666)
c9c88118
LC
122
123 (unless (file-exists? (scope "dev/pts"))
124 (mkdir (scope "dev/pts")))
125 (mount "none" (scope "dev/pts") "devpts")
126
37c825eb
LC
127 ;; Rendez-vous point for syslogd.
128 (mknod (scope "dev/log") 'socket #o666 0)
129 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
130
289773c1
LC
131 ;; Other useful nodes, notably relied on by guix-daemon.
132 (for-each (match-lambda
133 ((file major minor)
134 (mknod (scope file) 'char-special #o666
135 (device-number major minor))
136 (chmod (scope file) #o666)))
137 '(("dev/null" 1 3)
138 ("dev/zero" 1 5)
139 ("dev/full" 1 7)
140 ("dev/random" 1 8)
141 ("dev/urandom" 1 9)))
142
143 (symlink "/proc/self/fd" (scope "dev/fd"))
144 (symlink "/proc/self/fd/0" (scope "dev/stdin"))
145 (symlink "/proc/self/fd/1" (scope "dev/stdout"))
146 (symlink "/proc/self/fd/2" (scope "dev/stderr")))
d91712ee 147
88840f02
LC
148(define %host-qemu-ipv4-address
149 (inet-pton AF_INET "10.0.2.10"))
150
151(define* (configure-qemu-networking #:optional (interface "eth0"))
152 "Setup the INTERFACE network interface and /etc/resolv.conf according to
153QEMU's default networking settings (see net/slirp.c in QEMU for default
154networking values.) Return #t if INTERFACE is up, #f otherwise."
155 (display "configuring QEMU networking...\n")
156 (let* ((sock (socket AF_INET SOCK_STREAM 0))
157 (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
158 (flags (network-interface-flags sock interface)))
159 (set-network-interface-address sock interface address)
160 (set-network-interface-flags sock interface (logior flags IFF_UP))
161
162 (unless (file-exists? "/etc")
163 (mkdir "/etc"))
164 (call-with-output-file "/etc/resolv.conf"
165 (lambda (p)
166 (display "nameserver 10.0.2.3\n" p)))
167
168 (logand (network-interface-flags sock interface) IFF_UP)))
169
170(define (mount-qemu-smb-share share mount-point)
171 "Mount QEMU's CIFS/SMB SHARE at MOUNT-POINT.
172
173Vanilla QEMU's `-smb' option just exports a /qemu share, whereas our
174`qemu-with-multiple-smb-shares' package exports the /xchg and /store shares
175 (the latter allows the store to be shared between the host and guest.)"
176
177 (format #t "mounting QEMU's SMB share `~a'...\n" share)
178 (let ((server "10.0.2.4"))
179 (mount (string-append "//" server share) mount-point "cifs" 0
180 (string->pointer "guest,sec=none"))))
181
4919d684
LC
182(define (mount-qemu-9p source mount-point)
183 "Mount QEMU's 9p file system from SOURCE at MOUNT-POINT.
184
185This uses the 'virtio' transport, which requires the various virtio Linux
186modules to be loaded."
187
188 (format #t "mounting QEMU's 9p share '~a'...\n" source)
189 (let ((server "10.0.2.4"))
190 (mount source mount-point "9p" 0
191 (string->pointer "trans=virtio"))))
192
89bf140b
LC
193(define (bind-mount source target)
194 "Bind-mount SOURCE at TARGET."
195 (define MS_BIND 4096) ; from libc's <sys/mount.h>
196
197 (mount source target "" MS_BIND))
198
88840f02
LC
199(define (load-linux-module* file)
200 "Load Linux module from FILE, the name of a `.ko' file."
201 (define (slurp module)
202 (call-with-input-file file get-bytevector-all))
203
204 (load-linux-module (slurp file)))
205
206(define (device-number major minor)
207 "Return the device number for the device with MAJOR and MINOR, for use as
208the last argument of `mknod'."
209 (+ (* major 256) minor))
210
d4254711
LC
211(define* (boot-system #:key
212 (linux-modules '())
213 qemu-guest-networking?
214 guile-modules-in-chroot?
44ddf33e 215 volatile-root?
d4254711
LC
216 (mounts '()))
217 "This procedure is meant to be called from an initrd. Boot a system by
218first loading LINUX-MODULES, then setting up QEMU guest networking if
219QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
220and finally booting into the new root if any. The initrd supports kernel
221command-line options '--load', '--root', and '--repl'.
222
223MOUNTS must be a list of elements of the form:
224
225 (FILE-SYSTEM-TYPE SOURCE TARGET)
226
227When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
44ddf33e
LC
228the new root.
229
230When VOLATILE-ROOT? is true, the root file system is writable but any changes
231to it are lost."
d4254711
LC
232 (define (resolve file)
233 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
234 ;; under /root.
235 (let ((st (lstat file)))
236 (if (eq? 'symlink (stat:type st))
237 (let ((target (readlink file)))
238 (resolve (string-append "/root" target)))
239 file)))
240
44ddf33e
LC
241 (define MS_RDONLY 1)
242
d4254711
LC
243 (display "Welcome, this is GNU's early boot Guile.\n")
244 (display "Use '--repl' for an initrd REPL.\n\n")
245
246 (mount-essential-file-systems)
247 (let* ((args (linux-command-line))
248 (option (lambda (opt)
249 (let ((opt (string-append opt "=")))
250 (and=> (find (cut string-prefix? opt <>)
251 args)
252 (lambda (arg)
253 (substring arg (+ 1 (string-index arg #\=))))))))
254 (to-load (option "--load"))
255 (root (option "--root")))
256
257 (when (member "--repl" args)
258 (start-repl))
259
260 (display "loading kernel modules...\n")
261 (for-each (compose load-linux-module*
262 (cut string-append "/modules/" <>))
263 linux-modules)
264
265 (when qemu-guest-networking?
266 (unless (configure-qemu-networking)
267 (display "network interface is DOWN\n")))
268
269 ;; Make /dev nodes.
270 (make-essential-device-nodes)
271
272 ;; Prepare the real root file system under /root.
273 (unless (file-exists? "/root")
274 (mkdir "/root"))
275 (if root
83b9e6a1
LC
276 (catch #t
277 (lambda ()
44ddf33e
LC
278 (if volatile-root?
279 (begin
280 ;; XXX: For lack of a union file system...
281 (mkdir-p "/real-root")
282 (mount root "/real-root" "ext3" MS_RDONLY)
283 (mount "none" "/root" "tmpfs")
284
285 ;; XXX: 'copy-recursively' cannot deal with device nodes, so
286 ;; explicitly avoid /dev.
287 (for-each (lambda (file)
288 (unless (string=? "dev" file)
289 (copy-recursively (string-append "/real-root/"
290 file)
291 (string-append "/root/"
292 file)
293 #:log (%make-void-port
294 "w"))))
295 (scandir "/real-root"
296 (lambda (file)
297 (not (member file '("." ".."))))))
298
299 ;; TODO: Unmount /real-root.
300 )
301 (mount root "/root" "ext3")))
83b9e6a1
LC
302 (lambda args
303 (format (current-error-port) "exception while mounting '~a': ~s~%"
304 root args)
305 (start-repl)))
d4254711 306 (mount "none" "/root" "tmpfs"))
44ddf33e 307
d4254711
LC
308 (mount-essential-file-systems #:root "/root")
309
310 (unless (file-exists? "/root/dev")
311 (mkdir "/root/dev")
312 (make-essential-device-nodes #:root "/root"))
313
314 ;; Mount the specified file systems.
315 (for-each (match-lambda
316 (('cifs source target)
317 (let ((target (string-append "/root/" target)))
318 (mkdir-p target)
319 (mount-qemu-smb-share source target)))
4919d684
LC
320 (('9p source target)
321 (let ((target (string-append "/root/" target)))
322 (mkdir-p target)
323 (mount-qemu-9p source target))))
d4254711
LC
324 mounts)
325
326 (when guile-modules-in-chroot?
327 ;; Copy the directories that contain .scm and .go files so that the
328 ;; child process in the chroot can load modules (we would bind-mount
329 ;; them but for some reason that fails with EINVAL -- XXX).
330 (mkdir-p "/root/share")
331 (mkdir-p "/root/lib")
332 (mount "none" "/root/share" "tmpfs")
333 (mount "none" "/root/lib" "tmpfs")
334 (copy-recursively "/share" "/root/share"
335 #:log (%make-void-port "w"))
336 (copy-recursively "/lib" "/root/lib"
337 #:log (%make-void-port "w")))
338
339 (if to-load
340 (begin
341 (format #t "loading '~a'...\n" to-load)
26fc862a 342 (chdir "/root")
d4254711
LC
343 (chroot "/root")
344 ;; TODO: Remove /lib, /share, and /loader.go.
345 (catch #t
346 (lambda ()
347 (primitive-load to-load))
348 (lambda args
349 (format (current-error-port) "'~a' raised an exception: ~s~%"
350 to-load args)
351 (start-repl)))
352 (format (current-error-port)
353 "boot program '~a' terminated, rebooting~%"
354 to-load)
355 (sleep 2)
356 (reboot))
357 (begin
358 (display "no boot file passed via '--load'\n")
359 (display "entering a warm and cozy REPL\n")
360 (start-repl)))))
361
88840f02 362;;; linux-initrd.scm ends here