linux-initrd: Create /dev/klog and /dev/kmsg.
[jackhill/guix/guix.git] / guix / build / linux-initrd.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
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)
22 #:export (mount-essential-file-systems
23 linux-command-line
24 make-essential-device-nodes
25 configure-qemu-networking
26 mount-qemu-smb-share
27 bind-mount
28 load-linux-module*
29 device-number))
30
31 ;;; Commentary:
32 ;;;
33 ;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that
34 ;;; many of these use procedures not yet available in vanilla Guile (`mount',
35 ;;; `load-linux-module', etc.); these are provided by a Guile patch used in
36 ;;; the GNU distribution.
37 ;;;
38 ;;; Code:
39
40 (define* (mount-essential-file-systems #:key (root "/"))
41 "Mount /proc and /sys under ROOT."
42 (define (scope dir)
43 (string-append root
44 (if (string-suffix? "/" root)
45 ""
46 "/")
47 dir))
48
49 (unless (file-exists? (scope "proc"))
50 (mkdir (scope "proc")))
51 (mount "none" (scope "proc") "proc")
52
53 (unless (file-exists? (scope "sys"))
54 (mkdir (scope "sys")))
55 (mount "none" (scope "sys") "sysfs"))
56
57 (define (linux-command-line)
58 "Return the Linux kernel command line as a list of strings."
59 (string-tokenize
60 (call-with-input-file "/proc/cmdline"
61 get-string-all)))
62
63 (define* (make-essential-device-nodes #:key (root "/"))
64 "Make essential device nodes under ROOT/dev."
65 ;; The hand-made udev!
66
67 (define (scope dir)
68 (string-append root
69 (if (string-suffix? "/" root)
70 ""
71 "/")
72 dir))
73
74 (unless (file-exists? (scope "dev"))
75 (mkdir (scope "dev")))
76
77 ;; Make the device nodes for QEMU's hard disk and partitions.
78 (mknod (scope "dev/vda") 'block-special #o644 (device-number 8 0))
79 (mknod (scope "dev/vda1") 'block-special #o644 (device-number 8 1))
80 (mknod (scope "dev/vda2") 'block-special #o644 (device-number 8 2))
81
82 ;; TTYs.
83 (mknod (scope "dev/tty") 'char-special #o600
84 (device-number 5 0))
85 (let loop ((n 0))
86 (and (< n 50)
87 (let ((name (format #f "dev/tty~a" n)))
88 (mknod (scope name) 'char-special #o600
89 (device-number 4 n))
90 (loop (+ 1 n)))))
91
92 ;; Rendez-vous point for syslogd.
93 (mknod (scope "dev/log") 'socket #o666 0)
94 (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
95
96 ;; Other useful nodes.
97 (mknod (scope "dev/null") 'char-special #o666 (device-number 1 3))
98 (mknod (scope "dev/zero") 'char-special #o666 (device-number 1 5)))
99
100 (define %host-qemu-ipv4-address
101 (inet-pton AF_INET "10.0.2.10"))
102
103 (define* (configure-qemu-networking #:optional (interface "eth0"))
104 "Setup the INTERFACE network interface and /etc/resolv.conf according to
105 QEMU's default networking settings (see net/slirp.c in QEMU for default
106 networking values.) Return #t if INTERFACE is up, #f otherwise."
107 (display "configuring QEMU networking...\n")
108 (let* ((sock (socket AF_INET SOCK_STREAM 0))
109 (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
110 (flags (network-interface-flags sock interface)))
111 (set-network-interface-address sock interface address)
112 (set-network-interface-flags sock interface (logior flags IFF_UP))
113
114 (unless (file-exists? "/etc")
115 (mkdir "/etc"))
116 (call-with-output-file "/etc/resolv.conf"
117 (lambda (p)
118 (display "nameserver 10.0.2.3\n" p)))
119
120 (logand (network-interface-flags sock interface) IFF_UP)))
121
122 (define (mount-qemu-smb-share share mount-point)
123 "Mount QEMU's CIFS/SMB SHARE at MOUNT-POINT.
124
125 Vanilla QEMU's `-smb' option just exports a /qemu share, whereas our
126 `qemu-with-multiple-smb-shares' package exports the /xchg and /store shares
127 (the latter allows the store to be shared between the host and guest.)"
128
129 (format #t "mounting QEMU's SMB share `~a'...\n" share)
130 (let ((server "10.0.2.4"))
131 (mount (string-append "//" server share) mount-point "cifs" 0
132 (string->pointer "guest,sec=none"))))
133
134 (define (bind-mount source target)
135 "Bind-mount SOURCE at TARGET."
136 (define MS_BIND 4096) ; from libc's <sys/mount.h>
137
138 (mount source target "" MS_BIND))
139
140 (define (load-linux-module* file)
141 "Load Linux module from FILE, the name of a `.ko' file."
142 (define (slurp module)
143 (call-with-input-file file get-bytevector-all))
144
145 (load-linux-module (slurp file)))
146
147 (define (device-number major minor)
148 "Return the device number for the device with MAJOR and MINOR, for use as
149 the last argument of `mknod'."
150 (+ (* major 256) minor))
151
152 ;;; linux-initrd.scm ends here