Commit | Line | Data |
---|---|---|
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) | |
22 | #:export (mount-essential-file-systems | |
23 | linux-command-line | |
d91712ee | 24 | make-essential-device-nodes |
88840f02 LC |
25 | configure-qemu-networking |
26 | mount-qemu-smb-share | |
89bf140b | 27 | bind-mount |
88840f02 LC |
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 | ||
d91712ee LC |
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. | |
29804e6e LC |
83 | (mknod (scope "dev/tty") 'char-special #o600 |
84 | (device-number 5 0)) | |
d91712ee LC |
85 | (let loop ((n 0)) |
86 | (and (< n 50) | |
87 | (let ((name (format #f "dev/tty~a" n))) | |
29804e6e | 88 | (mknod (scope name) 'char-special #o600 |
d91712ee LC |
89 | (device-number 4 n)) |
90 | (loop (+ 1 n))))) | |
91 | ||
37c825eb LC |
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 | ||
d91712ee LC |
96 | ;; Other useful nodes. |
97 | (mknod (scope "dev/null") 'char-special #o666 (device-number 1 3)) | |
b97c95eb LC |
98 | (mknod (scope "dev/zero") 'char-special #o666 (device-number 1 5)) |
99 | (chmod (scope "dev/null") #o666) | |
100 | (chmod (scope "dev/zero") #o666)) | |
d91712ee | 101 | |
88840f02 LC |
102 | (define %host-qemu-ipv4-address |
103 | (inet-pton AF_INET "10.0.2.10")) | |
104 | ||
105 | (define* (configure-qemu-networking #:optional (interface "eth0")) | |
106 | "Setup the INTERFACE network interface and /etc/resolv.conf according to | |
107 | QEMU's default networking settings (see net/slirp.c in QEMU for default | |
108 | networking values.) Return #t if INTERFACE is up, #f otherwise." | |
109 | (display "configuring QEMU networking...\n") | |
110 | (let* ((sock (socket AF_INET SOCK_STREAM 0)) | |
111 | (address (make-socket-address AF_INET %host-qemu-ipv4-address 0)) | |
112 | (flags (network-interface-flags sock interface))) | |
113 | (set-network-interface-address sock interface address) | |
114 | (set-network-interface-flags sock interface (logior flags IFF_UP)) | |
115 | ||
116 | (unless (file-exists? "/etc") | |
117 | (mkdir "/etc")) | |
118 | (call-with-output-file "/etc/resolv.conf" | |
119 | (lambda (p) | |
120 | (display "nameserver 10.0.2.3\n" p))) | |
121 | ||
122 | (logand (network-interface-flags sock interface) IFF_UP))) | |
123 | ||
124 | (define (mount-qemu-smb-share share mount-point) | |
125 | "Mount QEMU's CIFS/SMB SHARE at MOUNT-POINT. | |
126 | ||
127 | Vanilla QEMU's `-smb' option just exports a /qemu share, whereas our | |
128 | `qemu-with-multiple-smb-shares' package exports the /xchg and /store shares | |
129 | (the latter allows the store to be shared between the host and guest.)" | |
130 | ||
131 | (format #t "mounting QEMU's SMB share `~a'...\n" share) | |
132 | (let ((server "10.0.2.4")) | |
133 | (mount (string-append "//" server share) mount-point "cifs" 0 | |
134 | (string->pointer "guest,sec=none")))) | |
135 | ||
89bf140b LC |
136 | (define (bind-mount source target) |
137 | "Bind-mount SOURCE at TARGET." | |
138 | (define MS_BIND 4096) ; from libc's <sys/mount.h> | |
139 | ||
140 | (mount source target "" MS_BIND)) | |
141 | ||
88840f02 LC |
142 | (define (load-linux-module* file) |
143 | "Load Linux module from FILE, the name of a `.ko' file." | |
144 | (define (slurp module) | |
145 | (call-with-input-file file get-bytevector-all)) | |
146 | ||
147 | (load-linux-module (slurp file))) | |
148 | ||
149 | (define (device-number major minor) | |
150 | "Return the device number for the device with MAJOR and MINOR, for use as | |
151 | the last argument of `mknod'." | |
152 | (+ (* major 256) minor)) | |
153 | ||
154 | ;;; linux-initrd.scm ends here |