linux-initrd: Delete files from the initrd ramfs when switching roots.
[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)
231 (start-repl))))
232
3c05b4bc
LC
233(define (check-file-system device type)
234 "Run a file system check of TYPE on DEVICE."
235 (define fsck
236 (string-append "fsck." type))
237
238 (let ((status (system* fsck "-v" "-p" device)))
239 (match (status:exit-val status)
240 (0
241 #t)
242 (1
243 (format (current-error-port) "'~a' corrected errors on ~a; continuing~%"
244 fsck device))
245 (2
246 (format (current-error-port) "'~a' corrected errors on ~a; rebooting~%"
247 fsck device)
248 (sleep 3)
249 (reboot))
250 (code
251 (format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%"
252 fsck code device)
253 (start-repl)))))
254
83bcd0b8
LC
255(define* (mount-file-system spec #:key (root "/root"))
256 "Mount the file system described by SPEC under ROOT. SPEC must have the
257form:
258
3c05b4bc 259 (DEVICE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?)
83bcd0b8
LC
260
261DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f;
3c05b4bc
LC
262FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to
263run a file system check."
83bcd0b8
LC
264 (define flags->bit-mask
265 (match-lambda
266 (('read-only rest ...)
267 (or MS_RDONLY (flags->bit-mask rest)))
268 (('bind-mount rest ...)
269 (or MS_BIND (flags->bit-mask rest)))
270 (()
271 0)))
272
273 (match spec
3c05b4bc 274 ((source mount-point type (flags ...) options check?)
83bcd0b8 275 (let ((mount-point (string-append root "/" mount-point)))
3c05b4bc
LC
276 (when check?
277 (check-file-system source type))
83bcd0b8
LC
278 (mkdir-p mount-point)
279 (mount source mount-point type (flags->bit-mask flags)
280 (if options
281 (string->pointer options)
282 %null-pointer))))))
283
1d462832
LC
284(define (switch-root root)
285 "Switch to ROOT as the root file system, in a way similar to what
286util-linux' switch_root(8) does."
287 (move-essential-file-systems root)
288 (chdir root)
26a728eb
LC
289
290 ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
291 ;; TODO: Use 'statfs' to check the fs type, like klibc does.
292 (when (or (not (file-exists? "/init")) (directory-exists? "/home"))
293 (format (current-error-port)
294 "The root file system is probably not an initrd; \
295bailing out.~%root contents: ~s~%" (scandir "/"))
296 (force-output (current-error-port))
297 (exit 1))
298
299 ;; Delete files from the old root, without crossing mount points (assuming
300 ;; there are no mount points in sub-directories.) That means we're leaving
301 ;; the empty ROOT directory behind us, but that's OK.
302 (let ((root-device (stat:dev (stat "/"))))
303 (for-each (lambda (file)
304 (unless (member file '("." ".."))
305 (let* ((file (string-append "/" file))
306 (device (stat:dev (lstat file))))
307 (when (= device root-device)
308 (delete-file-recursively file)))))
309 (scandir "/")))
310
311 ;; Make ROOT the new root.
1d462832 312 (mount root "/" "" MS_MOVE)
26a728eb
LC
313 (chroot ".")
314 (chdir "/")
315
316 (when (file-exists? "/dev/console")
317 ;; Close the standard file descriptors since they refer to the old
318 ;; /dev/console.
319 (for-each close-fdes '(0 1 2))
320
321 ;; Reopen them.
322 (let ((in (open-file "/dev/console" "rbl"))
323 (out (open-file "/dev/console" "wbl")))
324 (dup2 (fileno in) 0)
325 (dup2 (fileno out) 1)
326 (dup2 (fileno out) 2)
327
328 ;; Safely close IN and OUT.
329 (for-each (lambda (port)
330 (if (memv (fileno port) '(0 1 2))
331 (set-port-revealed! port 1)
332 (close-port port)))
333 (list in out)))))
1d462832 334
d4254711
LC
335(define* (boot-system #:key
336 (linux-modules '())
337 qemu-guest-networking?
338 guile-modules-in-chroot?
3c05b4bc 339 volatile-root?
d4254711
LC
340 (mounts '()))
341 "This procedure is meant to be called from an initrd. Boot a system by
342first loading LINUX-MODULES, then setting up QEMU guest networking if
343QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
344and finally booting into the new root if any. The initrd supports kernel
345command-line options '--load', '--root', and '--repl'.
346
3c05b4bc
LC
347Mount the root file system, specified by the '--root' command-line argument,
348if any.
03ddfaf5 349
83bcd0b8 350MOUNTS must be a list suitable for 'mount-file-system'.
d4254711
LC
351
352When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
44ddf33e
LC
353the new root.
354
355When VOLATILE-ROOT? is true, the root file system is writable but any changes
356to it are lost."
d4254711
LC
357 (define (resolve file)
358 ;; If FILE is a symlink to an absolute file name, resolve it as if we were
359 ;; under /root.
360 (let ((st (lstat file)))
361 (if (eq? 'symlink (stat:type st))
362 (let ((target (readlink file)))
363 (resolve (string-append "/root" target)))
364 file)))
365
3c05b4bc
LC
366 (define root-mount-point?
367 (match-lambda
368 ((device "/" _ ...) #t)
369 (_ #f)))
370
371 (define root-fs-type
372 (or (any (match-lambda
373 ((device "/" type _ ...) type)
374 (_ #f))
375 mounts)
376 "ext4"))
377
d4254711
LC
378 (display "Welcome, this is GNU's early boot Guile.\n")
379 (display "Use '--repl' for an initrd REPL.\n\n")
380
381 (mount-essential-file-systems)
382 (let* ((args (linux-command-line))
383 (option (lambda (opt)
384 (let ((opt (string-append opt "=")))
385 (and=> (find (cut string-prefix? opt <>)
386 args)
387 (lambda (arg)
388 (substring arg (+ 1 (string-index arg #\=))))))))
389 (to-load (option "--load"))
390 (root (option "--root")))
391
392 (when (member "--repl" args)
393 (start-repl))
394
395 (display "loading kernel modules...\n")
396 (for-each (compose load-linux-module*
397 (cut string-append "/modules/" <>))
398 linux-modules)
399
400 (when qemu-guest-networking?
401 (unless (configure-qemu-networking)
402 (display "network interface is DOWN\n")))
403
404 ;; Make /dev nodes.
405 (make-essential-device-nodes)
406
407 ;; Prepare the real root file system under /root.
408 (unless (file-exists? "/root")
409 (mkdir "/root"))
410 (if root
83bcd0b8 411 (mount-root-file-system root root-fs-type
3c05b4bc 412 #:volatile-root? volatile-root?)
d4254711 413 (mount "none" "/root" "tmpfs"))
44ddf33e 414
d4254711
LC
415 (unless (file-exists? "/root/dev")
416 (mkdir "/root/dev")
417 (make-essential-device-nodes #:root "/root"))
418
419 ;; Mount the specified file systems.
3c05b4bc
LC
420 (for-each mount-file-system
421 (remove root-mount-point? mounts))
d4254711
LC
422
423 (when guile-modules-in-chroot?
424 ;; Copy the directories that contain .scm and .go files so that the
425 ;; child process in the chroot can load modules (we would bind-mount
426 ;; them but for some reason that fails with EINVAL -- XXX).
427 (mkdir-p "/root/share")
428 (mkdir-p "/root/lib")
429 (mount "none" "/root/share" "tmpfs")
430 (mount "none" "/root/lib" "tmpfs")
431 (copy-recursively "/share" "/root/share"
432 #:log (%make-void-port "w"))
433 (copy-recursively "/lib" "/root/lib"
434 #:log (%make-void-port "w")))
435
436 (if to-load
437 (begin
1d462832 438 (switch-root "/root")
26a728eb 439 (format #t "loading '~a'...\n" to-load)
c865a878
LC
440
441 ;; Obviously this has to be done each time we boot. Do it from here
442 ;; so that statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3)
443 ;; expects (and thus openpty(3) and its users, such as xterm.)
444 (mount "none" "/dev/pts" "devpts")
445
d4254711
LC
446 ;; TODO: Remove /lib, /share, and /loader.go.
447 (catch #t
448 (lambda ()
449 (primitive-load to-load))
450 (lambda args
451 (format (current-error-port) "'~a' raised an exception: ~s~%"
452 to-load args)
453 (start-repl)))
454 (format (current-error-port)
455 "boot program '~a' terminated, rebooting~%"
456 to-load)
457 (sleep 2)
458 (reboot))
459 (begin
460 (display "no boot file passed via '--load'\n")
461 (display "entering a warm and cozy REPL\n")
462 (start-repl)))))
463
88840f02 464;;; linux-initrd.scm ends here