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