| 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 | configure-qemu-networking |
| 25 | mount-qemu-smb-share |
| 26 | load-linux-module* |
| 27 | device-number)) |
| 28 | |
| 29 | ;;; Commentary: |
| 30 | ;;; |
| 31 | ;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that |
| 32 | ;;; many of these use procedures not yet available in vanilla Guile (`mount', |
| 33 | ;;; `load-linux-module', etc.); these are provided by a Guile patch used in |
| 34 | ;;; the GNU distribution. |
| 35 | ;;; |
| 36 | ;;; Code: |
| 37 | |
| 38 | (define* (mount-essential-file-systems #:key (root "/")) |
| 39 | "Mount /proc and /sys under ROOT." |
| 40 | (define (scope dir) |
| 41 | (string-append root |
| 42 | (if (string-suffix? "/" root) |
| 43 | "" |
| 44 | "/") |
| 45 | dir)) |
| 46 | |
| 47 | (unless (file-exists? (scope "proc")) |
| 48 | (mkdir (scope "proc"))) |
| 49 | (mount "none" (scope "proc") "proc") |
| 50 | |
| 51 | (unless (file-exists? (scope "sys")) |
| 52 | (mkdir (scope "sys"))) |
| 53 | (mount "none" (scope "sys") "sysfs")) |
| 54 | |
| 55 | (define (linux-command-line) |
| 56 | "Return the Linux kernel command line as a list of strings." |
| 57 | (string-tokenize |
| 58 | (call-with-input-file "/proc/cmdline" |
| 59 | get-string-all))) |
| 60 | |
| 61 | (define %host-qemu-ipv4-address |
| 62 | (inet-pton AF_INET "10.0.2.10")) |
| 63 | |
| 64 | (define* (configure-qemu-networking #:optional (interface "eth0")) |
| 65 | "Setup the INTERFACE network interface and /etc/resolv.conf according to |
| 66 | QEMU's default networking settings (see net/slirp.c in QEMU for default |
| 67 | networking values.) Return #t if INTERFACE is up, #f otherwise." |
| 68 | (display "configuring QEMU networking...\n") |
| 69 | (let* ((sock (socket AF_INET SOCK_STREAM 0)) |
| 70 | (address (make-socket-address AF_INET %host-qemu-ipv4-address 0)) |
| 71 | (flags (network-interface-flags sock interface))) |
| 72 | (set-network-interface-address sock interface address) |
| 73 | (set-network-interface-flags sock interface (logior flags IFF_UP)) |
| 74 | |
| 75 | (unless (file-exists? "/etc") |
| 76 | (mkdir "/etc")) |
| 77 | (call-with-output-file "/etc/resolv.conf" |
| 78 | (lambda (p) |
| 79 | (display "nameserver 10.0.2.3\n" p))) |
| 80 | |
| 81 | (logand (network-interface-flags sock interface) IFF_UP))) |
| 82 | |
| 83 | (define (mount-qemu-smb-share share mount-point) |
| 84 | "Mount QEMU's CIFS/SMB SHARE at MOUNT-POINT. |
| 85 | |
| 86 | Vanilla QEMU's `-smb' option just exports a /qemu share, whereas our |
| 87 | `qemu-with-multiple-smb-shares' package exports the /xchg and /store shares |
| 88 | (the latter allows the store to be shared between the host and guest.)" |
| 89 | |
| 90 | (format #t "mounting QEMU's SMB share `~a'...\n" share) |
| 91 | (let ((server "10.0.2.4")) |
| 92 | (mount (string-append "//" server share) mount-point "cifs" 0 |
| 93 | (string->pointer "guest,sec=none")))) |
| 94 | |
| 95 | (define (load-linux-module* file) |
| 96 | "Load Linux module from FILE, the name of a `.ko' file." |
| 97 | (define (slurp module) |
| 98 | (call-with-input-file file get-bytevector-all)) |
| 99 | |
| 100 | (load-linux-module (slurp file))) |
| 101 | |
| 102 | (define (device-number major minor) |
| 103 | "Return the device number for the device with MAJOR and MINOR, for use as |
| 104 | the last argument of `mknod'." |
| 105 | (+ (* major 256) minor)) |
| 106 | |
| 107 | ;;; linux-initrd.scm ends here |