services: Use (guix build syscalls) instead of util-linux.
[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 32 configure-qemu-networking
83bcd0b8 33 mount-file-system
89bf140b 34 bind-mount
88840f02 35 load-linux-module*
d4254711
LC
36 device-number
37 boot-system))
88840f02
LC
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
1d462832
LC
65(define (move-essential-file-systems root)
66 "Move currently mounted essential file systems to ROOT."
67 (for-each (lambda (dir)
68 (let ((target (string-append root dir)))
69 (unless (file-exists? target)
70 (mkdir target))
71 (mount dir target "" MS_MOVE)))
72 '("/proc" "/sys")))
73
88840f02
LC
74(define (linux-command-line)
75 "Return the Linux kernel command line as a list of strings."
76 (string-tokenize
77 (call-with-input-file "/proc/cmdline"
78 get-string-all)))
79
d91712ee
LC
80(define* (make-essential-device-nodes #:key (root "/"))
81 "Make essential device nodes under ROOT/dev."
82 ;; The hand-made udev!
83
84 (define (scope dir)
85 (string-append root
86 (if (string-suffix? "/" root)
87 ""
88 "/")
89 dir))
90
91 (unless (file-exists? (scope "dev"))
92 (mkdir (scope "dev")))
93
fc4bc4b6
LC
94 ;; Make the device nodes for SCSI disks.
95 (mknod (scope "dev/sda") 'block-special #o644 (device-number 8 0))
96 (mknod (scope "dev/sda1") 'block-special #o644 (device-number 8 1))
97 (mknod (scope "dev/sda2") 'block-special #o644 (device-number 8 2))
98
99 ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
100 (mknod (scope "dev/vda") 'block-special #o644 (device-number 252 0))
101 (mknod (scope "dev/vda1") 'block-special #o644 (device-number 252 1))
102 (mknod (scope "dev/vda2") 'block-special #o644 (device-number 252 2))
d91712ee 103
c04c6ff6
LC
104 ;; Memory (used by Xorg's VESA driver.)
105 (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
106 (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
107
1c221510
LC
108 ;; Inputs (used by Xorg.)
109 (unless (file-exists? (scope "dev/input"))
110 (mkdir (scope "dev/input")))
111 (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
112 (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
113 (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
114
d91712ee 115 ;; TTYs.
29804e6e
LC
116 (mknod (scope "dev/tty") 'char-special #o600
117 (device-number 5 0))
289773c1 118 (chmod (scope "dev/tty") #o666)
d91712ee
LC
119 (let loop ((n 0))
120 (and (< n 50)
121 (let ((name (format #f "dev/tty~a" n)))
29804e6e 122 (mknod (scope name) 'char-special #o600
d91712ee
LC
123 (device-number 4 n))
124 (loop (+ 1 n)))))
125
c9c88118
LC
126 ;; Pseudo ttys.
127 (mknod (scope "dev/ptmx") 'char-special #o666
128 (device-number 5 2))
289773c1 129 (chmod (scope "dev/ptmx") #o666)
c9c88118 130
c865a878 131 ;; Create /dev/pts; it will be mounted later, at boot time.
c9c88118
LC
132 (unless (file-exists? (scope "dev/pts"))
133 (mkdir (scope "dev/pts")))
c9c88118 134
37c825eb
LC
135 ;; Rendez-vous point for syslogd.
136 (mknod (scope "dev/log") 'socket #o666 0)
137 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
138
289773c1
LC
139 ;; Other useful nodes, notably relied on by guix-daemon.
140 (for-each (match-lambda
141 ((file major minor)
142 (mknod (scope file) 'char-special #o666
143 (device-number major minor))
144 (chmod (scope file) #o666)))
145 '(("dev/null" 1 3)
146 ("dev/zero" 1 5)
147 ("dev/full" 1 7)
148 ("dev/random" 1 8)
149 ("dev/urandom" 1 9)))
150
151 (symlink "/proc/self/fd" (scope "dev/fd"))
152 (symlink "/proc/self/fd/0" (scope "dev/stdin"))
153 (symlink "/proc/self/fd/1" (scope "dev/stdout"))
1c96c1bb
LC
154 (symlink "/proc/self/fd/2" (scope "dev/stderr"))
155
156 ;; File systems in user space (FUSE).
157 (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
d91712ee 158
88840f02
LC
159(define %host-qemu-ipv4-address
160 (inet-pton AF_INET "10.0.2.10"))
161
162(define* (configure-qemu-networking #:optional (interface "eth0"))
163 "Setup the INTERFACE network interface and /etc/resolv.conf according to
164QEMU's default networking settings (see net/slirp.c in QEMU for default
165networking values.) Return #t if INTERFACE is up, #f otherwise."
166 (display "configuring QEMU networking...\n")
167 (let* ((sock (socket AF_INET SOCK_STREAM 0))
168 (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
169 (flags (network-interface-flags sock interface)))
170 (set-network-interface-address sock interface address)
171 (set-network-interface-flags sock interface (logior flags IFF_UP))
172
173 (unless (file-exists? "/etc")
174 (mkdir "/etc"))
175 (call-with-output-file "/etc/resolv.conf"
176 (lambda (p)
177 (display "nameserver 10.0.2.3\n" p)))
178
179 (logand (network-interface-flags sock interface) IFF_UP)))
180
83bcd0b8
LC
181;; Linux mount flags, from libc's <sys/mount.h>.
182(define MS_RDONLY 1)
183(define MS_BIND 4096)
1d462832 184(define MS_MOVE 8192)
4919d684 185
89bf140b
LC
186(define (bind-mount source target)
187 "Bind-mount SOURCE at TARGET."
89bf140b
LC
188 (mount source target "" MS_BIND))
189
88840f02
LC
190(define (load-linux-module* file)
191 "Load Linux module from FILE, the name of a `.ko' file."
192 (define (slurp module)
193 (call-with-input-file file get-bytevector-all))
194
195 (load-linux-module (slurp file)))
196
197(define (device-number major minor)
198 "Return the device number for the device with MAJOR and MINOR, for use as
199the last argument of `mknod'."
200 (+ (* major 256) minor))
201
83bcd0b8 202(define* (mount-root-file-system root type
3c05b4bc 203 #:key volatile-root? (unionfs "unionfs"))
83bcd0b8
LC
204 "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT?
205is true, mount ROOT read-only and make it a union with a writable tmpfs using
206UNIONFS."
207 (catch #t
208 (lambda ()
209 (if volatile-root?
210 (begin
211 (mkdir-p "/real-root")
212 (mount root "/real-root" type MS_RDONLY)
213 (mkdir-p "/rw-root")
214 (mount "none" "/rw-root" "tmpfs")
215
216 ;; We want read-write /dev nodes.
217 (make-essential-device-nodes #:root "/rw-root")
218
219 ;; Make /root a union of the tmpfs and the actual root.
220 (unless (zero? (system* unionfs "-o"
221 "cow,allow_other,use_ino,suid,dev"
222 "/rw-root=RW:/real-root=RO"
223 "/root"))
224 (error "unionfs failed")))
3c05b4bc
LC
225 (begin
226 (check-file-system root type)
227 (mount root "/root" type))))
83bcd0b8
LC
228 (lambda args
229 (format (current-error-port) "exception while mounting '~a': ~s~%"
230 root args)
b1995341
LC
231 (start-repl)))
232
233 (copy-file "/proc/mounts" "/root/etc/mtab"))
83bcd0b8 234
3c05b4bc
LC
235(define (check-file-system device type)
236 "Run a file system check of TYPE on DEVICE."
237 (define fsck
238 (string-append "fsck." type))
239
240 (let ((status (system* fsck "-v" "-p" device)))
241 (match (status:exit-val status)
242 (0
243 #t)
244 (1
245 (format (current-error-port) "'~a' corrected errors on ~a; continuing~%"
246 fsck device))
247 (2
248 (format (current-error-port) "'~a' corrected errors on ~a; rebooting~%"
249 fsck device)
250 (sleep 3)
251 (reboot))
252 (code
253 (format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%"
254 fsck code device)
255 (start-repl)))))
256
83bcd0b8
LC
257(define* (mount-file-system spec #:key (root "/root"))
258 "Mount the file system described by SPEC under ROOT. SPEC must have the
259form:
260
3c05b4bc 261 (DEVICE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?)
83bcd0b8
LC
262
263DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f;
3c05b4bc
LC
264FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to
265run a file system check."
83bcd0b8
LC
266 (define flags->bit-mask
267 (match-lambda
268 (('read-only rest ...)
269 (or MS_RDONLY (flags->bit-mask rest)))
270 (('bind-mount rest ...)
271 (or MS_BIND (flags->bit-mask rest)))
272 (()
273 0)))
274
275 (match spec
3c05b4bc 276 ((source mount-point type (flags ...) options check?)
83bcd0b8 277 (let ((mount-point (string-append root "/" mount-point)))
3c05b4bc
LC
278 (when check?
279 (check-file-system source type))
83bcd0b8
LC
280 (mkdir-p mount-point)
281 (mount source mount-point type (flags->bit-mask flags)
282 (if options
283 (string->pointer options)
b1995341
LC
284 %null-pointer))
285
286 ;; Update /etc/mtab.
287 (mkdir-p (string-append root "/etc"))
02139eb9 288 (let ((port (open-file (string-append root "/etc/mtab") "a")))
b1995341
LC
289 (format port "~a ~a ~a ~a 0 0~%"
290 source mount-point type options)
291 (close-port port))))))
83bcd0b8 292
1d462832
LC
293(define (switch-root root)
294 "Switch to ROOT as the root file system, in a way similar to what
295util-linux' switch_root(8) does."
296 (move-essential-file-systems root)
297 (chdir root)
26a728eb
LC
298
299 ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
300 ;; TODO: Use 'statfs' to check the fs type, like klibc does.
301 (when (or (not (file-exists? "/init")) (directory-exists? "/home"))
302 (format (current-error-port)
303 "The root file system is probably not an initrd; \
304bailing out.~%root contents: ~s~%" (scandir "/"))
305 (force-output (current-error-port))
306 (exit 1))
307
308 ;; Delete files from the old root, without crossing mount points (assuming
309 ;; there are no mount points in sub-directories.) That means we're leaving
310 ;; the empty ROOT directory behind us, but that's OK.
311 (let ((root-device (stat:dev (stat "/"))))
312 (for-each (lambda (file)
313 (unless (member file '("." ".."))
314 (let* ((file (string-append "/" file))
315 (device (stat:dev (lstat file))))
316 (when (= device root-device)
317 (delete-file-recursively file)))))
318 (scandir "/")))
319
320 ;; Make ROOT the new root.
1d462832 321 (mount root "/" "" MS_MOVE)
26a728eb
LC
322 (chroot ".")
323 (chdir "/")
324
325 (when (file-exists? "/dev/console")
326 ;; Close the standard file descriptors since they refer to the old
474b832d
LC
327 ;; /dev/console, and reopen them.
328 (let ((console (open-file "/dev/console" "r+b0")))
329 (for-each close-fdes '(0 1 2))
330
331 (dup2 (fileno console) 0)
332 (dup2 (fileno console) 1)
333 (dup2 (fileno console) 2)
334
335 (close-port console))))
1d462832 336
d4254711
LC
337(define* (boot-system #:key
338 (linux-modules '())
339 qemu-guest-networking?
340 guile-modules-in-chroot?
3c05b4bc 341 volatile-root?
d4254711
LC
342 (mounts '()))
343 "This procedure is meant to be called from an initrd. Boot a system by
344first loading LINUX-MODULES, then setting up QEMU guest networking if
345QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
346and finally booting into the new root if any. The initrd supports kernel
347command-line options '--load', '--root', and '--repl'.
348
3c05b4bc
LC
349Mount the root file system, specified by the '--root' command-line argument,
350if any.
03ddfaf5 351
83bcd0b8 352MOUNTS must be a list suitable for 'mount-file-system'.
d4254711
LC
353
354When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
44ddf33e
LC
355the new root.
356
357When VOLATILE-ROOT? is true, the root file system is writable but any changes
358to it are lost."
d4254711
LC
359 (define (resolve file)
360 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
361 ;; under /root.
362 (let ((st (lstat file)))
363 (if (eq? 'symlink (stat:type st))
364 (let ((target (readlink file)))
365 (resolve (string-append "/root" target)))
366 file)))
367
3c05b4bc
LC
368 (define root-mount-point?
369 (match-lambda
370 ((device "/" _ ...) #t)
371 (_ #f)))
372
373 (define root-fs-type
374 (or (any (match-lambda
375 ((device "/" type _ ...) type)
376 (_ #f))
377 mounts)
378 "ext4"))
379
d4254711
LC
380 (display "Welcome, this is GNU's early boot Guile.\n")
381 (display "Use '--repl' for an initrd REPL.\n\n")
382
383 (mount-essential-file-systems)
384 (let* ((args (linux-command-line))
385 (option (lambda (opt)
386 (let ((opt (string-append opt "=")))
387 (and=> (find (cut string-prefix? opt <>)
388 args)
389 (lambda (arg)
390 (substring arg (+ 1 (string-index arg #\=))))))))
391 (to-load (option "--load"))
392 (root (option "--root")))
393
394 (when (member "--repl" args)
395 (start-repl))
396
397 (display "loading kernel modules...\n")
398 (for-each (compose load-linux-module*
399 (cut string-append "/modules/" <>))
400 linux-modules)
401
402 (when qemu-guest-networking?
403 (unless (configure-qemu-networking)
404 (display "network interface is DOWN\n")))
405
406 ;; Make /dev nodes.
407 (make-essential-device-nodes)
408
409 ;; Prepare the real root file system under /root.
410 (unless (file-exists? "/root")
411 (mkdir "/root"))
412 (if root
83bcd0b8 413 (mount-root-file-system root root-fs-type
3c05b4bc 414 #:volatile-root? volatile-root?)
d4254711 415 (mount "none" "/root" "tmpfs"))
44ddf33e 416
d4254711
LC
417 (unless (file-exists? "/root/dev")
418 (mkdir "/root/dev")
419 (make-essential-device-nodes #:root "/root"))
420
421 ;; Mount the specified file systems.
3c05b4bc
LC
422 (for-each mount-file-system
423 (remove root-mount-point? mounts))
d4254711
LC
424
425 (when guile-modules-in-chroot?
426 ;; Copy the directories that contain .scm and .go files so that the
427 ;; child process in the chroot can load modules (we would bind-mount
428 ;; them but for some reason that fails with EINVAL -- XXX).
429 (mkdir-p "/root/share")
430 (mkdir-p "/root/lib")
431 (mount "none" "/root/share" "tmpfs")
432 (mount "none" "/root/lib" "tmpfs")
433 (copy-recursively "/share" "/root/share"
434 #:log (%make-void-port "w"))
435 (copy-recursively "/lib" "/root/lib"
436 #:log (%make-void-port "w")))
437
438 (if to-load
439 (begin
1d462832 440 (switch-root "/root")
26a728eb 441 (format #t "loading '~a'...\n" to-load)
c865a878
LC
442
443 ;; Obviously this has to be done each time we boot. Do it from here
444 ;; so that statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3)
445 ;; expects (and thus openpty(3) and its users, such as xterm.)
446 (mount "none" "/dev/pts" "devpts")
447
d4254711
LC
448 ;; TODO: Remove /lib, /share, and /loader.go.
449 (catch #t
450 (lambda ()
451 (primitive-load to-load))
452 (lambda args
453 (format (current-error-port) "'~a' raised an exception: ~s~%"
454 to-load args)
455 (start-repl)))
456 (format (current-error-port)
457 "boot program '~a' terminated, rebooting~%"
458 to-load)
459 (sleep 2)
460 (reboot))
461 (begin
462 (display "no boot file passed via '--load'\n")
463 (display "entering a warm and cozy REPL\n")
464 (start-repl)))))
465
88840f02 466;;; linux-initrd.scm ends here