gnu: xorg-server: Use /var as $localstatedir.
[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
LC
95
96 ;; TTYs.
29804e6e
LC
97 (mknod (scope "dev/tty") 'char-special #o600
98 (device-number 5 0))
d91712ee
LC
99 (let loop ((n 0))
100 (and (< n 50)
101 (let ((name (format #f "dev/tty~a" n)))
29804e6e 102 (mknod (scope name) 'char-special #o600
d91712ee
LC
103 (device-number 4 n))
104 (loop (+ 1 n)))))
105
37c825eb
LC
106 ;; Rendez-vous point for syslogd.
107 (mknod (scope "dev/log") 'socket #o666 0)
108 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
109
d91712ee
LC
110 ;; Other useful nodes.
111 (mknod (scope "dev/null") 'char-special #o666 (device-number 1 3))
b97c95eb
LC
112 (mknod (scope "dev/zero") 'char-special #o666 (device-number 1 5))
113 (chmod (scope "dev/null") #o666)
114 (chmod (scope "dev/zero") #o666))
d91712ee 115
88840f02
LC
116(define %host-qemu-ipv4-address
117 (inet-pton AF_INET "10.0.2.10"))
118
119(define* (configure-qemu-networking #:optional (interface "eth0"))
120 "Setup the INTERFACE network interface and /etc/resolv.conf according to
121QEMU's default networking settings (see net/slirp.c in QEMU for default
122networking values.) Return #t if INTERFACE is up, #f otherwise."
123 (display "configuring QEMU networking...\n")
124 (let* ((sock (socket AF_INET SOCK_STREAM 0))
125 (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
126 (flags (network-interface-flags sock interface)))
127 (set-network-interface-address sock interface address)
128 (set-network-interface-flags sock interface (logior flags IFF_UP))
129
130 (unless (file-exists? "/etc")
131 (mkdir "/etc"))
132 (call-with-output-file "/etc/resolv.conf"
133 (lambda (p)
134 (display "nameserver 10.0.2.3\n" p)))
135
136 (logand (network-interface-flags sock interface) IFF_UP)))
137
138(define (mount-qemu-smb-share share mount-point)
139 "Mount QEMU's CIFS/SMB SHARE at MOUNT-POINT.
140
141Vanilla QEMU's `-smb' option just exports a /qemu share, whereas our
142`qemu-with-multiple-smb-shares' package exports the /xchg and /store shares
143 (the latter allows the store to be shared between the host and guest.)"
144
145 (format #t "mounting QEMU's SMB share `~a'...\n" share)
146 (let ((server "10.0.2.4"))
147 (mount (string-append "//" server share) mount-point "cifs" 0
148 (string->pointer "guest,sec=none"))))
149
4919d684
LC
150(define (mount-qemu-9p source mount-point)
151 "Mount QEMU's 9p file system from SOURCE at MOUNT-POINT.
152
153This uses the 'virtio' transport, which requires the various virtio Linux
154modules to be loaded."
155
156 (format #t "mounting QEMU's 9p share '~a'...\n" source)
157 (let ((server "10.0.2.4"))
158 (mount source mount-point "9p" 0
159 (string->pointer "trans=virtio"))))
160
89bf140b
LC
161(define (bind-mount source target)
162 "Bind-mount SOURCE at TARGET."
163 (define MS_BIND 4096) ; from libc's <sys/mount.h>
164
165 (mount source target "" MS_BIND))
166
88840f02
LC
167(define (load-linux-module* file)
168 "Load Linux module from FILE, the name of a `.ko' file."
169 (define (slurp module)
170 (call-with-input-file file get-bytevector-all))
171
172 (load-linux-module (slurp file)))
173
174(define (device-number major minor)
175 "Return the device number for the device with MAJOR and MINOR, for use as
176the last argument of `mknod'."
177 (+ (* major 256) minor))
178
d4254711
LC
179(define* (boot-system #:key
180 (linux-modules '())
181 qemu-guest-networking?
182 guile-modules-in-chroot?
44ddf33e 183 volatile-root?
d4254711
LC
184 (mounts '()))
185 "This procedure is meant to be called from an initrd. Boot a system by
186first loading LINUX-MODULES, then setting up QEMU guest networking if
187QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
188and finally booting into the new root if any. The initrd supports kernel
189command-line options '--load', '--root', and '--repl'.
190
191MOUNTS must be a list of elements of the form:
192
193 (FILE-SYSTEM-TYPE SOURCE TARGET)
194
195When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
44ddf33e
LC
196the new root.
197
198When VOLATILE-ROOT? is true, the root file system is writable but any changes
199to it are lost."
d4254711
LC
200 (define (resolve file)
201 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
202 ;; under /root.
203 (let ((st (lstat file)))
204 (if (eq? 'symlink (stat:type st))
205 (let ((target (readlink file)))
206 (resolve (string-append "/root" target)))
207 file)))
208
44ddf33e
LC
209 (define MS_RDONLY 1)
210
d4254711
LC
211 (display "Welcome, this is GNU's early boot Guile.\n")
212 (display "Use '--repl' for an initrd REPL.\n\n")
213
214 (mount-essential-file-systems)
215 (let* ((args (linux-command-line))
216 (option (lambda (opt)
217 (let ((opt (string-append opt "=")))
218 (and=> (find (cut string-prefix? opt <>)
219 args)
220 (lambda (arg)
221 (substring arg (+ 1 (string-index arg #\=))))))))
222 (to-load (option "--load"))
223 (root (option "--root")))
224
225 (when (member "--repl" args)
226 (start-repl))
227
228 (display "loading kernel modules...\n")
229 (for-each (compose load-linux-module*
230 (cut string-append "/modules/" <>))
231 linux-modules)
232
233 (when qemu-guest-networking?
234 (unless (configure-qemu-networking)
235 (display "network interface is DOWN\n")))
236
237 ;; Make /dev nodes.
238 (make-essential-device-nodes)
239
240 ;; Prepare the real root file system under /root.
241 (unless (file-exists? "/root")
242 (mkdir "/root"))
243 (if root
83b9e6a1
LC
244 (catch #t
245 (lambda ()
44ddf33e
LC
246 (if volatile-root?
247 (begin
248 ;; XXX: For lack of a union file system...
249 (mkdir-p "/real-root")
250 (mount root "/real-root" "ext3" MS_RDONLY)
251 (mount "none" "/root" "tmpfs")
252
253 ;; XXX: 'copy-recursively' cannot deal with device nodes, so
254 ;; explicitly avoid /dev.
255 (for-each (lambda (file)
256 (unless (string=? "dev" file)
257 (copy-recursively (string-append "/real-root/"
258 file)
259 (string-append "/root/"
260 file)
261 #:log (%make-void-port
262 "w"))))
263 (scandir "/real-root"
264 (lambda (file)
265 (not (member file '("." ".."))))))
266
267 ;; TODO: Unmount /real-root.
268 )
269 (mount root "/root" "ext3")))
83b9e6a1
LC
270 (lambda args
271 (format (current-error-port) "exception while mounting '~a': ~s~%"
272 root args)
273 (start-repl)))
d4254711 274 (mount "none" "/root" "tmpfs"))
44ddf33e 275
d4254711
LC
276 (mount-essential-file-systems #:root "/root")
277
278 (unless (file-exists? "/root/dev")
279 (mkdir "/root/dev")
280 (make-essential-device-nodes #:root "/root"))
281
282 ;; Mount the specified file systems.
283 (for-each (match-lambda
284 (('cifs source target)
285 (let ((target (string-append "/root/" target)))
286 (mkdir-p target)
287 (mount-qemu-smb-share source target)))
4919d684
LC
288 (('9p source target)
289 (let ((target (string-append "/root/" target)))
290 (mkdir-p target)
291 (mount-qemu-9p source target))))
d4254711
LC
292 mounts)
293
294 (when guile-modules-in-chroot?
295 ;; Copy the directories that contain .scm and .go files so that the
296 ;; child process in the chroot can load modules (we would bind-mount
297 ;; them but for some reason that fails with EINVAL -- XXX).
298 (mkdir-p "/root/share")
299 (mkdir-p "/root/lib")
300 (mount "none" "/root/share" "tmpfs")
301 (mount "none" "/root/lib" "tmpfs")
302 (copy-recursively "/share" "/root/share"
303 #:log (%make-void-port "w"))
304 (copy-recursively "/lib" "/root/lib"
305 #:log (%make-void-port "w")))
306
307 (if to-load
308 (begin
309 (format #t "loading '~a'...\n" to-load)
310 (chroot "/root")
311 ;; TODO: Remove /lib, /share, and /loader.go.
312 (catch #t
313 (lambda ()
314 (primitive-load to-load))
315 (lambda args
316 (format (current-error-port) "'~a' raised an exception: ~s~%"
317 to-load args)
318 (start-repl)))
319 (format (current-error-port)
320 "boot program '~a' terminated, rebooting~%"
321 to-load)
322 (sleep 2)
323 (reboot))
324 (begin
325 (display "no boot file passed via '--load'\n")
326 (display "entering a warm and cozy REPL\n")
327 (start-repl)))))
328
88840f02 329;;; linux-initrd.scm ends here