2013-08-29 00:04:04 +02:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2014-01-18 15:08:33 +01:00
|
|
|
|
;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
|
2013-08-29 00:04:04 +02:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (guix build linux-initrd)
|
|
|
|
|
#:use-module (rnrs io ports)
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
2013-08-29 00:04:04 +02:00
|
|
|
|
#:use-module (system foreign)
|
2014-07-03 22:44:14 +02:00
|
|
|
|
#:use-module (system repl error-handling)
|
2014-01-29 21:57:56 +01:00
|
|
|
|
#:autoload (system repl repl) (start-repl)
|
|
|
|
|
#:autoload (system base compile) (compile-file)
|
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
|
#:use-module (ice-9 match)
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
#:use-module (ice-9 rdelim)
|
2014-01-31 14:26:30 +01:00
|
|
|
|
#:use-module (ice-9 ftw)
|
2014-01-29 21:57:56 +01:00
|
|
|
|
#:use-module (guix build utils)
|
2013-08-29 00:04:04 +02:00
|
|
|
|
#:export (mount-essential-file-systems
|
|
|
|
|
linux-command-line
|
2014-05-16 23:31:48 +02:00
|
|
|
|
find-long-option
|
2013-09-01 23:13:56 +02:00
|
|
|
|
make-essential-device-nodes
|
2013-08-29 00:04:04 +02:00
|
|
|
|
configure-qemu-networking
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
|
|
|
|
|
disk-partitions
|
|
|
|
|
partition-label-predicate
|
|
|
|
|
find-partition-by-label
|
2014-06-02 23:58:50 +02:00
|
|
|
|
canonicalize-device-spec
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
|
2014-07-23 00:44:27 +02:00
|
|
|
|
mount-flags->bit-mask
|
2014-05-10 23:33:52 +02:00
|
|
|
|
check-file-system
|
2014-05-03 00:26:07 +02:00
|
|
|
|
mount-file-system
|
2013-08-31 14:52:12 +02:00
|
|
|
|
bind-mount
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
|
2013-08-29 00:04:04 +02:00
|
|
|
|
load-linux-module*
|
2014-01-29 21:57:56 +01:00
|
|
|
|
device-number
|
|
|
|
|
boot-system))
|
2013-08-29 00:04:04 +02:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
|
|
|
|
;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that
|
|
|
|
|
;;; many of these use procedures not yet available in vanilla Guile (`mount',
|
|
|
|
|
;;; `load-linux-module', etc.); these are provided by a Guile patch used in
|
|
|
|
|
;;; the GNU distribution.
|
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(define* (mount-essential-file-systems #:key (root "/"))
|
|
|
|
|
"Mount /proc and /sys under ROOT."
|
|
|
|
|
(define (scope dir)
|
|
|
|
|
(string-append root
|
|
|
|
|
(if (string-suffix? "/" root)
|
|
|
|
|
""
|
|
|
|
|
"/")
|
|
|
|
|
dir))
|
|
|
|
|
|
|
|
|
|
(unless (file-exists? (scope "proc"))
|
|
|
|
|
(mkdir (scope "proc")))
|
|
|
|
|
(mount "none" (scope "proc") "proc")
|
|
|
|
|
|
|
|
|
|
(unless (file-exists? (scope "sys"))
|
|
|
|
|
(mkdir (scope "sys")))
|
|
|
|
|
(mount "none" (scope "sys") "sysfs"))
|
|
|
|
|
|
2014-05-04 22:24:47 +02:00
|
|
|
|
(define (move-essential-file-systems root)
|
|
|
|
|
"Move currently mounted essential file systems to ROOT."
|
|
|
|
|
(for-each (lambda (dir)
|
|
|
|
|
(let ((target (string-append root dir)))
|
|
|
|
|
(unless (file-exists? target)
|
|
|
|
|
(mkdir target))
|
|
|
|
|
(mount dir target "" MS_MOVE)))
|
|
|
|
|
'("/proc" "/sys")))
|
|
|
|
|
|
2013-08-29 00:04:04 +02:00
|
|
|
|
(define (linux-command-line)
|
|
|
|
|
"Return the Linux kernel command line as a list of strings."
|
|
|
|
|
(string-tokenize
|
|
|
|
|
(call-with-input-file "/proc/cmdline"
|
|
|
|
|
get-string-all)))
|
|
|
|
|
|
2014-05-16 23:31:48 +02:00
|
|
|
|
(define (find-long-option option arguments)
|
|
|
|
|
"Find OPTION among ARGUMENTS, where OPTION is something like \"--load\".
|
|
|
|
|
Return the value associated with OPTION, or #f on failure."
|
|
|
|
|
(let ((opt (string-append option "=")))
|
|
|
|
|
(and=> (find (cut string-prefix? opt <>)
|
|
|
|
|
arguments)
|
|
|
|
|
(lambda (arg)
|
|
|
|
|
(substring arg (+ 1 (string-index arg #\=)))))))
|
|
|
|
|
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
(define-syntax %ext2-endianness
|
|
|
|
|
;; Endianness of ext2 file systems.
|
|
|
|
|
(identifier-syntax (endianness little)))
|
|
|
|
|
|
|
|
|
|
;; Offset in bytes of interesting parts of an ext2 superblock. See
|
|
|
|
|
;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
|
|
|
|
|
;; TODO: Use "packed structs" from Guile-OpenGL or similar.
|
|
|
|
|
(define-syntax %ext2-sblock-magic (identifier-syntax 56))
|
|
|
|
|
(define-syntax %ext2-sblock-creator-os (identifier-syntax 72))
|
|
|
|
|
(define-syntax %ext2-sblock-uuid (identifier-syntax 104))
|
|
|
|
|
(define-syntax %ext2-sblock-volume-name (identifier-syntax 120))
|
|
|
|
|
|
|
|
|
|
(define (read-ext2-superblock device)
|
|
|
|
|
"Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f
|
|
|
|
|
if DEVICE does not contain an ext2 file system."
|
|
|
|
|
(define %ext2-magic
|
|
|
|
|
;; The magic bytes that identify an ext2 file system.
|
|
|
|
|
#xef53)
|
|
|
|
|
|
2014-07-02 12:18:36 +02:00
|
|
|
|
(define superblock-size
|
|
|
|
|
;; Size of the interesting part of an ext2 superblock.
|
|
|
|
|
264)
|
|
|
|
|
|
|
|
|
|
(define block
|
|
|
|
|
;; The superblock contents.
|
|
|
|
|
(make-bytevector superblock-size))
|
|
|
|
|
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
(call-with-input-file device
|
|
|
|
|
(lambda (port)
|
|
|
|
|
(seek port 1024 SEEK_SET)
|
2014-07-02 12:18:36 +02:00
|
|
|
|
|
|
|
|
|
;; Note: work around <http://bugs.gnu.org/17466>.
|
|
|
|
|
(and (eqv? superblock-size (get-bytevector-n! port block 0
|
|
|
|
|
superblock-size))
|
|
|
|
|
(let ((magic (bytevector-u16-ref block %ext2-sblock-magic
|
|
|
|
|
%ext2-endianness)))
|
|
|
|
|
(and (= magic %ext2-magic)
|
|
|
|
|
block))))))
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
|
|
|
|
|
(define (ext2-superblock-uuid sblock)
|
|
|
|
|
"Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector."
|
|
|
|
|
(let ((uuid (make-bytevector 16)))
|
|
|
|
|
(bytevector-copy! sblock %ext2-sblock-uuid uuid 0 16)
|
|
|
|
|
uuid))
|
|
|
|
|
|
|
|
|
|
(define (ext2-superblock-volume-name sblock)
|
|
|
|
|
"Return the volume name of SBLOCK as a string of at most 16 characters, or
|
|
|
|
|
#f if SBLOCK has no volume name."
|
|
|
|
|
(let ((bv (make-bytevector 16)))
|
|
|
|
|
(bytevector-copy! sblock %ext2-sblock-volume-name bv 0 16)
|
|
|
|
|
|
|
|
|
|
;; This is a Latin-1, nul-terminated string.
|
|
|
|
|
(let ((bytes (take-while (negate zero?) (bytevector->u8-list bv))))
|
|
|
|
|
(if (null? bytes)
|
|
|
|
|
#f
|
|
|
|
|
(list->string (map integer->char bytes))))))
|
|
|
|
|
|
|
|
|
|
(define (disk-partitions)
|
|
|
|
|
"Return the list of device names corresponding to valid disk partitions."
|
|
|
|
|
(define (partition? major minor)
|
|
|
|
|
(let ((marker (format #f "/sys/dev/block/~a:~a/partition" major minor)))
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(not (zero? (call-with-input-file marker read))))
|
|
|
|
|
(lambda args
|
|
|
|
|
(if (= ENOENT (system-error-errno args))
|
|
|
|
|
#f
|
|
|
|
|
(apply throw args))))))
|
|
|
|
|
|
|
|
|
|
(call-with-input-file "/proc/partitions"
|
|
|
|
|
(lambda (port)
|
|
|
|
|
;; Skip the two header lines.
|
|
|
|
|
(read-line port)
|
|
|
|
|
(read-line port)
|
|
|
|
|
|
|
|
|
|
;; Read each subsequent line, and extract the last space-separated
|
|
|
|
|
;; field.
|
|
|
|
|
(let loop ((parts '()))
|
|
|
|
|
(let ((line (read-line port)))
|
|
|
|
|
(if (eof-object? line)
|
|
|
|
|
(reverse parts)
|
|
|
|
|
(match (string-tokenize line)
|
|
|
|
|
(((= string->number major) (= string->number minor)
|
|
|
|
|
blocks name)
|
|
|
|
|
(if (partition? major minor)
|
|
|
|
|
(loop (cons name parts))
|
|
|
|
|
(loop parts))))))))))
|
|
|
|
|
|
|
|
|
|
(define (partition-label-predicate label)
|
|
|
|
|
"Return a procedure that, when applied to a partition name such as \"sda1\",
|
|
|
|
|
return #t if that partition's volume name is LABEL."
|
|
|
|
|
(lambda (part)
|
|
|
|
|
(let* ((device (string-append "/dev/" part))
|
2014-06-01 21:20:54 +02:00
|
|
|
|
(sblock (catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(read-ext2-superblock device))
|
|
|
|
|
(lambda args
|
|
|
|
|
;; When running on the hand-made /dev,
|
|
|
|
|
;; 'disk-partitions' could return partitions for which
|
|
|
|
|
;; we have no /dev node. Handle that gracefully.
|
|
|
|
|
(if (= ENOENT (system-error-errno args))
|
|
|
|
|
(begin
|
|
|
|
|
(format (current-error-port)
|
|
|
|
|
"warning: device '~a' not found~%"
|
|
|
|
|
device)
|
|
|
|
|
#f)
|
|
|
|
|
(apply throw args))))))
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
(and sblock
|
2014-05-31 15:55:38 +02:00
|
|
|
|
(let ((volume (ext2-superblock-volume-name sblock)))
|
|
|
|
|
(and volume
|
|
|
|
|
(string=? volume label)))))))
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
|
|
|
|
|
(define (find-partition-by-label label)
|
|
|
|
|
"Return the first partition found whose volume name is LABEL, or #f if none
|
|
|
|
|
were found."
|
|
|
|
|
(and=> (find (partition-label-predicate label)
|
|
|
|
|
(disk-partitions))
|
|
|
|
|
(cut string-append "/dev/" <>)))
|
|
|
|
|
|
2014-06-02 23:48:55 +02:00
|
|
|
|
(define* (canonicalize-device-spec spec #:optional (title 'any))
|
|
|
|
|
"Return the device name corresponding to SPEC. TITLE is a symbol, one of
|
|
|
|
|
the following:
|
|
|
|
|
|
|
|
|
|
• 'device', in which case SPEC is known to designate a device node--e.g.,
|
|
|
|
|
\"/dev/sda1\";
|
|
|
|
|
• 'label', in which case SPEC is known to designate a partition label--e.g.,
|
|
|
|
|
\"my-root-part\";
|
|
|
|
|
• 'any', in which case SPEC can be anything.
|
|
|
|
|
"
|
|
|
|
|
(define max-trials
|
|
|
|
|
;; Number of times we retry partition label resolution.
|
|
|
|
|
7)
|
|
|
|
|
|
|
|
|
|
(define canonical-title
|
|
|
|
|
;; The realm of canonicalization.
|
|
|
|
|
(if (eq? title 'any)
|
|
|
|
|
(if (string-prefix? "/" spec)
|
|
|
|
|
'device
|
|
|
|
|
'label)
|
|
|
|
|
title))
|
|
|
|
|
|
|
|
|
|
(case canonical-title
|
|
|
|
|
((device)
|
|
|
|
|
;; Nothing to do.
|
|
|
|
|
spec)
|
|
|
|
|
((label)
|
|
|
|
|
;; Resolve the label.
|
|
|
|
|
(let loop ((count 0))
|
|
|
|
|
(let ((device (find-partition-by-label spec)))
|
|
|
|
|
(or device
|
|
|
|
|
;; Some devices take a bit of time to appear, most notably USB
|
|
|
|
|
;; storage devices. Thus, wait for the device to appear.
|
|
|
|
|
(if (> count max-trials)
|
2014-07-03 22:44:14 +02:00
|
|
|
|
(error "failed to resolve partition label" spec)
|
2014-06-02 23:48:55 +02:00
|
|
|
|
(begin
|
|
|
|
|
(sleep 1)
|
|
|
|
|
(loop (+ 1 count))))))))
|
|
|
|
|
;; TODO: Add support for UUIDs.
|
|
|
|
|
(else
|
|
|
|
|
(error "unknown device title" title))))
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
|
2014-06-01 21:48:57 +02:00
|
|
|
|
(define* (make-disk-device-nodes base major #:optional (minor 0))
|
|
|
|
|
"Make the block device nodes around BASE (something like \"/root/dev/sda\")
|
|
|
|
|
with the given MAJOR number, starting with MINOR."
|
|
|
|
|
(mknod base 'block-special #o644 (device-number major minor))
|
|
|
|
|
(let loop ((i 1))
|
|
|
|
|
(when (< i 6)
|
|
|
|
|
(mknod (string-append base (number->string i))
|
|
|
|
|
'block-special #o644 (device-number major (+ minor i)))
|
|
|
|
|
(loop (+ i 1)))))
|
|
|
|
|
|
2013-09-01 23:13:56 +02:00
|
|
|
|
(define* (make-essential-device-nodes #:key (root "/"))
|
|
|
|
|
"Make essential device nodes under ROOT/dev."
|
|
|
|
|
;; The hand-made udev!
|
|
|
|
|
|
|
|
|
|
(define (scope dir)
|
|
|
|
|
(string-append root
|
|
|
|
|
(if (string-suffix? "/" root)
|
|
|
|
|
""
|
|
|
|
|
"/")
|
|
|
|
|
dir))
|
|
|
|
|
|
|
|
|
|
(unless (file-exists? (scope "dev"))
|
|
|
|
|
(mkdir (scope "dev")))
|
|
|
|
|
|
2014-01-31 01:43:16 +01:00
|
|
|
|
;; Make the device nodes for SCSI disks.
|
2014-06-01 21:48:57 +02:00
|
|
|
|
(make-disk-device-nodes (scope "dev/sda") 8)
|
|
|
|
|
(make-disk-device-nodes (scope "dev/sdb") 8 16)
|
|
|
|
|
(make-disk-device-nodes (scope "dev/sdc") 8 32)
|
|
|
|
|
(make-disk-device-nodes (scope "dev/sdd") 8 48)
|
|
|
|
|
|
|
|
|
|
;; SCSI CD-ROM devices (aka. "/dev/sr0" etc.).
|
|
|
|
|
(mknod (scope "dev/scd0") 'block-special #o644 (device-number 11 0))
|
|
|
|
|
(mknod (scope "dev/scd1") 'block-special #o644 (device-number 11 1))
|
2014-01-31 01:43:16 +01:00
|
|
|
|
|
|
|
|
|
;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
|
2014-06-01 21:48:57 +02:00
|
|
|
|
(make-disk-device-nodes (scope "dev/vda") 252)
|
2013-09-01 23:13:56 +02:00
|
|
|
|
|
2014-02-01 01:07:14 +01:00
|
|
|
|
;; Memory (used by Xorg's VESA driver.)
|
|
|
|
|
(mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
|
|
|
|
|
(mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
|
|
|
|
|
|
2014-02-01 02:22:23 +01:00
|
|
|
|
;; Inputs (used by Xorg.)
|
|
|
|
|
(unless (file-exists? (scope "dev/input"))
|
|
|
|
|
(mkdir (scope "dev/input")))
|
|
|
|
|
(mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
|
|
|
|
|
(mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
|
|
|
|
|
(mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
|
|
|
|
|
|
2014-06-01 23:51:12 +02:00
|
|
|
|
;; System console. This node is magically created by the kernel on the
|
|
|
|
|
;; initrd's root, so don't try to create it in that case.
|
|
|
|
|
(unless (string=? root "/")
|
|
|
|
|
(mknod (scope "dev/console") 'char-special #o600
|
|
|
|
|
(device-number 5 1)))
|
|
|
|
|
|
2013-09-01 23:13:56 +02:00
|
|
|
|
;; TTYs.
|
2013-09-04 23:22:34 +02:00
|
|
|
|
(mknod (scope "dev/tty") 'char-special #o600
|
|
|
|
|
(device-number 5 0))
|
2014-04-09 17:50:37 +02:00
|
|
|
|
(chmod (scope "dev/tty") #o666)
|
2013-09-01 23:13:56 +02:00
|
|
|
|
(let loop ((n 0))
|
|
|
|
|
(and (< n 50)
|
|
|
|
|
(let ((name (format #f "dev/tty~a" n)))
|
2013-09-04 23:22:34 +02:00
|
|
|
|
(mknod (scope name) 'char-special #o600
|
2013-09-01 23:13:56 +02:00
|
|
|
|
(device-number 4 n))
|
|
|
|
|
(loop (+ 1 n)))))
|
|
|
|
|
|
2014-05-14 16:38:47 +02:00
|
|
|
|
;; Serial line.
|
|
|
|
|
(mknod (scope "dev/ttyS0") 'char-special #o660
|
|
|
|
|
(device-number 4 64))
|
|
|
|
|
|
2014-03-10 23:16:02 +01:00
|
|
|
|
;; Pseudo ttys.
|
|
|
|
|
(mknod (scope "dev/ptmx") 'char-special #o666
|
|
|
|
|
(device-number 5 2))
|
2014-04-09 17:50:37 +02:00
|
|
|
|
(chmod (scope "dev/ptmx") #o666)
|
2014-03-10 23:16:02 +01:00
|
|
|
|
|
2014-04-09 19:10:07 +02:00
|
|
|
|
;; Create /dev/pts; it will be mounted later, at boot time.
|
2014-03-10 23:16:02 +01:00
|
|
|
|
(unless (file-exists? (scope "dev/pts"))
|
|
|
|
|
(mkdir (scope "dev/pts")))
|
|
|
|
|
|
2013-09-11 00:52:36 +02:00
|
|
|
|
;; Rendez-vous point for syslogd.
|
|
|
|
|
(mknod (scope "dev/log") 'socket #o666 0)
|
|
|
|
|
(mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
|
|
|
|
|
|
2014-04-09 17:50:37 +02:00
|
|
|
|
;; Other useful nodes, notably relied on by guix-daemon.
|
|
|
|
|
(for-each (match-lambda
|
|
|
|
|
((file major minor)
|
|
|
|
|
(mknod (scope file) 'char-special #o666
|
|
|
|
|
(device-number major minor))
|
|
|
|
|
(chmod (scope file) #o666)))
|
|
|
|
|
'(("dev/null" 1 3)
|
|
|
|
|
("dev/zero" 1 5)
|
|
|
|
|
("dev/full" 1 7)
|
|
|
|
|
("dev/random" 1 8)
|
|
|
|
|
("dev/urandom" 1 9)))
|
|
|
|
|
|
|
|
|
|
(symlink "/proc/self/fd" (scope "dev/fd"))
|
|
|
|
|
(symlink "/proc/self/fd/0" (scope "dev/stdin"))
|
|
|
|
|
(symlink "/proc/self/fd/1" (scope "dev/stdout"))
|
2014-04-14 23:59:08 +02:00
|
|
|
|
(symlink "/proc/self/fd/2" (scope "dev/stderr"))
|
|
|
|
|
|
2014-05-22 21:57:39 +02:00
|
|
|
|
;; Loopback devices.
|
|
|
|
|
(let loop ((i 0))
|
|
|
|
|
(when (< i 8)
|
|
|
|
|
(mknod (scope (string-append "dev/loop" (number->string i)))
|
|
|
|
|
'block-special #o660
|
|
|
|
|
(device-number 7 i))
|
|
|
|
|
(loop (+ 1 i))))
|
|
|
|
|
|
2014-04-14 23:59:08 +02:00
|
|
|
|
;; File systems in user space (FUSE).
|
|
|
|
|
(mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
|
2013-09-01 23:13:56 +02:00
|
|
|
|
|
2013-08-29 00:04:04 +02:00
|
|
|
|
(define %host-qemu-ipv4-address
|
|
|
|
|
(inet-pton AF_INET "10.0.2.10"))
|
|
|
|
|
|
|
|
|
|
(define* (configure-qemu-networking #:optional (interface "eth0"))
|
|
|
|
|
"Setup the INTERFACE network interface and /etc/resolv.conf according to
|
|
|
|
|
QEMU's default networking settings (see net/slirp.c in QEMU for default
|
|
|
|
|
networking values.) Return #t if INTERFACE is up, #f otherwise."
|
|
|
|
|
(display "configuring QEMU networking...\n")
|
|
|
|
|
(let* ((sock (socket AF_INET SOCK_STREAM 0))
|
|
|
|
|
(address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
|
|
|
|
|
(flags (network-interface-flags sock interface)))
|
|
|
|
|
(set-network-interface-address sock interface address)
|
|
|
|
|
(set-network-interface-flags sock interface (logior flags IFF_UP))
|
|
|
|
|
|
2014-08-15 16:06:21 +02:00
|
|
|
|
;; Hello! We used to create /etc/resolv.conf here, with "nameserver
|
|
|
|
|
;; 10.0.2.3\n". However, with Linux-libre 3.16, we're getting ENOSPC.
|
|
|
|
|
;; And since it's actually unnecessary, it's gone.
|
2013-08-29 00:04:04 +02:00
|
|
|
|
|
|
|
|
|
(logand (network-interface-flags sock interface) IFF_UP)))
|
|
|
|
|
|
2014-05-03 00:26:07 +02:00
|
|
|
|
;; Linux mount flags, from libc's <sys/mount.h>.
|
|
|
|
|
(define MS_RDONLY 1)
|
2014-07-23 00:44:27 +02:00
|
|
|
|
(define MS_NOSUID 2)
|
|
|
|
|
(define MS_NODEV 4)
|
|
|
|
|
(define MS_NOEXEC 8)
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(define MS_BIND 4096)
|
2014-05-04 22:24:47 +02:00
|
|
|
|
(define MS_MOVE 8192)
|
2014-01-31 12:21:10 +01:00
|
|
|
|
|
2013-08-31 14:52:12 +02:00
|
|
|
|
(define (bind-mount source target)
|
|
|
|
|
"Bind-mount SOURCE at TARGET."
|
|
|
|
|
(mount source target "" MS_BIND))
|
|
|
|
|
|
2013-08-29 00:04:04 +02:00
|
|
|
|
(define (load-linux-module* file)
|
|
|
|
|
"Load Linux module from FILE, the name of a `.ko' file."
|
|
|
|
|
(define (slurp module)
|
|
|
|
|
(call-with-input-file file get-bytevector-all))
|
|
|
|
|
|
|
|
|
|
(load-linux-module (slurp file)))
|
|
|
|
|
|
|
|
|
|
(define (device-number major minor)
|
|
|
|
|
"Return the device number for the device with MAJOR and MINOR, for use as
|
|
|
|
|
the last argument of `mknod'."
|
|
|
|
|
(+ (* major 256) minor))
|
|
|
|
|
|
2014-05-14 16:38:21 +02:00
|
|
|
|
(define (pidof program)
|
|
|
|
|
"Return the PID of the first presumed instance of PROGRAM."
|
|
|
|
|
(let ((program (basename program)))
|
|
|
|
|
(find (lambda (pid)
|
|
|
|
|
(let ((exe (format #f "/proc/~a/exe" pid)))
|
|
|
|
|
(and=> (false-if-exception (readlink exe))
|
|
|
|
|
(compose (cut string=? program <>) basename))))
|
|
|
|
|
(filter-map string->number (scandir "/proc")))))
|
|
|
|
|
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(define* (mount-root-file-system root type
|
2014-05-04 00:30:39 +02:00
|
|
|
|
#:key volatile-root? (unionfs "unionfs"))
|
2014-05-03 00:26:07 +02:00
|
|
|
|
"Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT?
|
|
|
|
|
is true, mount ROOT read-only and make it a union with a writable tmpfs using
|
|
|
|
|
UNIONFS."
|
2014-05-14 16:38:21 +02:00
|
|
|
|
(define (mark-as-not-killable pid)
|
|
|
|
|
;; Tell the 'user-processes' dmd service that PID must be kept alive when
|
|
|
|
|
;; shutting down.
|
|
|
|
|
(mkdir-p "/root/etc/dmd")
|
|
|
|
|
(let ((port (open-file "/root/etc/dmd/do-not-kill" "a")))
|
|
|
|
|
(chmod port #o600)
|
|
|
|
|
(write pid port)
|
|
|
|
|
(newline port)
|
|
|
|
|
(close-port port)))
|
|
|
|
|
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(catch #t
|
|
|
|
|
(lambda ()
|
|
|
|
|
(if volatile-root?
|
|
|
|
|
(begin
|
|
|
|
|
(mkdir-p "/real-root")
|
|
|
|
|
(mount root "/real-root" type MS_RDONLY)
|
|
|
|
|
(mkdir-p "/rw-root")
|
|
|
|
|
(mount "none" "/rw-root" "tmpfs")
|
|
|
|
|
|
|
|
|
|
;; We want read-write /dev nodes.
|
|
|
|
|
(make-essential-device-nodes #:root "/rw-root")
|
|
|
|
|
|
2014-06-21 17:35:56 +02:00
|
|
|
|
;; Make /root a union of the tmpfs and the actual root. Use
|
|
|
|
|
;; 'max_files' to set a high RLIMIT_NOFILE for the unionfs process
|
|
|
|
|
;; itself. Failing to do that, we quickly run out of file
|
|
|
|
|
;; descriptors; see <http://bugs.gnu.org/17827>.
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(unless (zero? (system* unionfs "-o"
|
2014-06-21 17:35:56 +02:00
|
|
|
|
"cow,allow_other,use_ino,suid,dev,max_files=65536"
|
2014-05-03 00:26:07 +02:00
|
|
|
|
"/rw-root=RW:/real-root=RO"
|
|
|
|
|
"/root"))
|
2014-05-14 16:38:21 +02:00
|
|
|
|
(error "unionfs failed"))
|
|
|
|
|
|
|
|
|
|
;; Make sure unionfs remains alive till the end. Because
|
|
|
|
|
;; 'fuse_daemonize' doesn't tell the PID of the forked daemon, we
|
|
|
|
|
;; have to resort to 'pidof' here.
|
|
|
|
|
(mark-as-not-killable (pidof unionfs)))
|
2014-05-04 00:30:39 +02:00
|
|
|
|
(begin
|
|
|
|
|
(check-file-system root type)
|
|
|
|
|
(mount root "/root" type))))
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(lambda args
|
|
|
|
|
(format (current-error-port) "exception while mounting '~a': ~s~%"
|
|
|
|
|
root args)
|
2014-05-08 00:22:26 +02:00
|
|
|
|
(start-repl)))
|
|
|
|
|
|
|
|
|
|
(copy-file "/proc/mounts" "/root/etc/mtab"))
|
2014-05-03 00:26:07 +02:00
|
|
|
|
|
2014-05-04 00:30:39 +02:00
|
|
|
|
(define (check-file-system device type)
|
|
|
|
|
"Run a file system check of TYPE on DEVICE."
|
|
|
|
|
(define fsck
|
|
|
|
|
(string-append "fsck." type))
|
|
|
|
|
|
|
|
|
|
(let ((status (system* fsck "-v" "-p" device)))
|
|
|
|
|
(match (status:exit-val status)
|
|
|
|
|
(0
|
|
|
|
|
#t)
|
|
|
|
|
(1
|
|
|
|
|
(format (current-error-port) "'~a' corrected errors on ~a; continuing~%"
|
|
|
|
|
fsck device))
|
|
|
|
|
(2
|
|
|
|
|
(format (current-error-port) "'~a' corrected errors on ~a; rebooting~%"
|
|
|
|
|
fsck device)
|
|
|
|
|
(sleep 3)
|
|
|
|
|
(reboot))
|
|
|
|
|
(code
|
|
|
|
|
(format (current-error-port) "'~a' exited with code ~a on ~a; spawning REPL~%"
|
|
|
|
|
fsck code device)
|
|
|
|
|
(start-repl)))))
|
|
|
|
|
|
2014-07-23 00:44:27 +02:00
|
|
|
|
(define (mount-flags->bit-mask flags)
|
|
|
|
|
"Return the number suitable for the 'flags' argument of 'mount' that
|
|
|
|
|
corresponds to the symbols listed in FLAGS."
|
|
|
|
|
(let loop ((flags flags))
|
|
|
|
|
(match flags
|
|
|
|
|
(('read-only rest ...)
|
|
|
|
|
(logior MS_RDONLY (loop rest)))
|
|
|
|
|
(('bind-mount rest ...)
|
|
|
|
|
(logior MS_BIND (loop rest)))
|
|
|
|
|
(('no-suid rest ...)
|
|
|
|
|
(logior MS_NOSUID (loop rest)))
|
|
|
|
|
(('no-dev rest ...)
|
|
|
|
|
(logior MS_NODEV (loop rest)))
|
|
|
|
|
(('no-exec rest ...)
|
|
|
|
|
(logior MS_NOEXEC (loop rest)))
|
|
|
|
|
(()
|
|
|
|
|
0))))
|
|
|
|
|
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(define* (mount-file-system spec #:key (root "/root"))
|
|
|
|
|
"Mount the file system described by SPEC under ROOT. SPEC must have the
|
|
|
|
|
form:
|
|
|
|
|
|
2014-06-02 23:58:50 +02:00
|
|
|
|
(DEVICE TITLE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?)
|
2014-05-03 00:26:07 +02:00
|
|
|
|
|
|
|
|
|
DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f;
|
2014-05-04 00:30:39 +02:00
|
|
|
|
FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to
|
|
|
|
|
run a file system check."
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(match spec
|
2014-06-02 23:58:50 +02:00
|
|
|
|
((source title mount-point type (flags ...) options check?)
|
|
|
|
|
(let ((source (canonicalize-device-spec source title))
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
(mount-point (string-append root "/" mount-point)))
|
2014-05-04 00:30:39 +02:00
|
|
|
|
(when check?
|
|
|
|
|
(check-file-system source type))
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(mkdir-p mount-point)
|
2014-07-23 00:44:27 +02:00
|
|
|
|
(mount source mount-point type (mount-flags->bit-mask flags)
|
2014-05-03 00:26:07 +02:00
|
|
|
|
(if options
|
|
|
|
|
(string->pointer options)
|
2014-05-08 00:22:26 +02:00
|
|
|
|
%null-pointer))
|
|
|
|
|
|
|
|
|
|
;; Update /etc/mtab.
|
|
|
|
|
(mkdir-p (string-append root "/etc"))
|
2014-05-10 21:47:05 +02:00
|
|
|
|
(let ((port (open-file (string-append root "/etc/mtab") "a")))
|
2014-05-08 00:22:26 +02:00
|
|
|
|
(format port "~a ~a ~a ~a 0 0~%"
|
2014-07-23 00:44:27 +02:00
|
|
|
|
source mount-point type (or options ""))
|
2014-05-08 00:22:26 +02:00
|
|
|
|
(close-port port))))))
|
2014-05-03 00:26:07 +02:00
|
|
|
|
|
2014-05-04 22:24:47 +02:00
|
|
|
|
(define (switch-root root)
|
|
|
|
|
"Switch to ROOT as the root file system, in a way similar to what
|
|
|
|
|
util-linux' switch_root(8) does."
|
|
|
|
|
(move-essential-file-systems root)
|
|
|
|
|
(chdir root)
|
2014-05-06 18:09:25 +02:00
|
|
|
|
|
|
|
|
|
;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
|
|
|
|
|
;; TODO: Use 'statfs' to check the fs type, like klibc does.
|
|
|
|
|
(when (or (not (file-exists? "/init")) (directory-exists? "/home"))
|
|
|
|
|
(format (current-error-port)
|
|
|
|
|
"The root file system is probably not an initrd; \
|
|
|
|
|
bailing out.~%root contents: ~s~%" (scandir "/"))
|
|
|
|
|
(force-output (current-error-port))
|
|
|
|
|
(exit 1))
|
|
|
|
|
|
|
|
|
|
;; Delete files from the old root, without crossing mount points (assuming
|
|
|
|
|
;; there are no mount points in sub-directories.) That means we're leaving
|
|
|
|
|
;; the empty ROOT directory behind us, but that's OK.
|
|
|
|
|
(let ((root-device (stat:dev (stat "/"))))
|
|
|
|
|
(for-each (lambda (file)
|
|
|
|
|
(unless (member file '("." ".."))
|
|
|
|
|
(let* ((file (string-append "/" file))
|
|
|
|
|
(device (stat:dev (lstat file))))
|
|
|
|
|
(when (= device root-device)
|
|
|
|
|
(delete-file-recursively file)))))
|
|
|
|
|
(scandir "/")))
|
|
|
|
|
|
|
|
|
|
;; Make ROOT the new root.
|
2014-05-04 22:24:47 +02:00
|
|
|
|
(mount root "/" "" MS_MOVE)
|
2014-05-06 18:09:25 +02:00
|
|
|
|
(chroot ".")
|
|
|
|
|
(chdir "/")
|
|
|
|
|
|
|
|
|
|
(when (file-exists? "/dev/console")
|
|
|
|
|
;; Close the standard file descriptors since they refer to the old
|
2014-05-08 23:21:45 +02:00
|
|
|
|
;; /dev/console, and reopen them.
|
|
|
|
|
(let ((console (open-file "/dev/console" "r+b0")))
|
|
|
|
|
(for-each close-fdes '(0 1 2))
|
|
|
|
|
|
|
|
|
|
(dup2 (fileno console) 0)
|
|
|
|
|
(dup2 (fileno console) 1)
|
|
|
|
|
(dup2 (fileno console) 2)
|
|
|
|
|
|
|
|
|
|
(close-port console))))
|
2014-05-04 22:24:47 +02:00
|
|
|
|
|
linux-initrd: Allow use of volume labels in 'file-system' declarations.
* guix/build/linux-initrd.scm (%ext2-endianness, %ext2-sblock-magic,
%ext2-sblock-creator-os, %ext2-sblock-uuid, %ext2-sblock-volume-name):
New macros.
(read-ext2-superblock, ext2-superblock-uuid,
ext2-superblock-volume-name, disk-partitions,
partition-label-predicate, find-partition-by-label,
canonicalize-device-spec): New procedures.
(mount-file-system): Use 'canonicalize-device-spec' on SOURCE.
(boot-system): Likewise for ROOT.
* doc/guix.texi (Using the Configuration System): Adjust 'file-system'
declaration accordingly.
2014-05-30 23:44:28 +02:00
|
|
|
|
|
2014-01-29 21:57:56 +01:00
|
|
|
|
(define* (boot-system #:key
|
|
|
|
|
(linux-modules '())
|
|
|
|
|
qemu-guest-networking?
|
|
|
|
|
guile-modules-in-chroot?
|
2014-05-04 00:30:39 +02:00
|
|
|
|
volatile-root?
|
2014-01-29 21:57:56 +01:00
|
|
|
|
(mounts '()))
|
|
|
|
|
"This procedure is meant to be called from an initrd. Boot a system by
|
|
|
|
|
first loading LINUX-MODULES, then setting up QEMU guest networking if
|
|
|
|
|
QEMU-GUEST-NETWORKING? is true, mounting the file systems specified in MOUNTS,
|
|
|
|
|
and finally booting into the new root if any. The initrd supports kernel
|
|
|
|
|
command-line options '--load', '--root', and '--repl'.
|
|
|
|
|
|
2014-05-04 00:30:39 +02:00
|
|
|
|
Mount the root file system, specified by the '--root' command-line argument,
|
|
|
|
|
if any.
|
2014-05-03 12:16:10 +02:00
|
|
|
|
|
2014-05-03 00:26:07 +02:00
|
|
|
|
MOUNTS must be a list suitable for 'mount-file-system'.
|
2014-01-29 21:57:56 +01:00
|
|
|
|
|
|
|
|
|
When GUILE-MODULES-IN-CHROOT? is true, make core Guile modules available in
|
2014-01-31 14:26:30 +01:00
|
|
|
|
the new root.
|
|
|
|
|
|
|
|
|
|
When VOLATILE-ROOT? is true, the root file system is writable but any changes
|
|
|
|
|
to it are lost."
|
2014-05-04 00:30:39 +02:00
|
|
|
|
(define root-mount-point?
|
|
|
|
|
(match-lambda
|
2014-06-02 23:58:50 +02:00
|
|
|
|
((device _ "/" _ ...) #t)
|
2014-05-04 00:30:39 +02:00
|
|
|
|
(_ #f)))
|
|
|
|
|
|
|
|
|
|
(define root-fs-type
|
|
|
|
|
(or (any (match-lambda
|
2014-06-02 23:58:50 +02:00
|
|
|
|
((device _ "/" type _ ...) type)
|
2014-05-04 00:30:39 +02:00
|
|
|
|
(_ #f))
|
|
|
|
|
mounts)
|
|
|
|
|
"ext4"))
|
|
|
|
|
|
2014-01-29 21:57:56 +01:00
|
|
|
|
(display "Welcome, this is GNU's early boot Guile.\n")
|
|
|
|
|
(display "Use '--repl' for an initrd REPL.\n\n")
|
|
|
|
|
|
2014-07-03 22:44:14 +02:00
|
|
|
|
(call-with-error-handling
|
|
|
|
|
(lambda ()
|
|
|
|
|
(mount-essential-file-systems)
|
|
|
|
|
(let* ((args (linux-command-line))
|
|
|
|
|
(to-load (find-long-option "--load" args))
|
|
|
|
|
(root (find-long-option "--root" args)))
|
|
|
|
|
|
|
|
|
|
(when (member "--repl" args)
|
|
|
|
|
(start-repl))
|
|
|
|
|
|
|
|
|
|
(display "loading kernel modules...\n")
|
|
|
|
|
(for-each (compose load-linux-module*
|
|
|
|
|
(cut string-append "/modules/" <>))
|
|
|
|
|
linux-modules)
|
|
|
|
|
|
|
|
|
|
(when qemu-guest-networking?
|
|
|
|
|
(unless (configure-qemu-networking)
|
|
|
|
|
(display "network interface is DOWN\n")))
|
|
|
|
|
|
|
|
|
|
;; Make /dev nodes.
|
|
|
|
|
(make-essential-device-nodes)
|
|
|
|
|
|
|
|
|
|
;; Prepare the real root file system under /root.
|
|
|
|
|
(unless (file-exists? "/root")
|
|
|
|
|
(mkdir "/root"))
|
|
|
|
|
(if root
|
|
|
|
|
(mount-root-file-system (canonicalize-device-spec root)
|
|
|
|
|
root-fs-type
|
|
|
|
|
#:volatile-root? volatile-root?)
|
|
|
|
|
(mount "none" "/root" "tmpfs"))
|
|
|
|
|
|
|
|
|
|
(unless (file-exists? "/root/dev")
|
|
|
|
|
(mkdir "/root/dev")
|
|
|
|
|
(make-essential-device-nodes #:root "/root"))
|
|
|
|
|
|
|
|
|
|
;; Mount the specified file systems.
|
|
|
|
|
(for-each mount-file-system
|
|
|
|
|
(remove root-mount-point? mounts))
|
|
|
|
|
|
|
|
|
|
(when guile-modules-in-chroot?
|
|
|
|
|
;; Copy the directories that contain .scm and .go files so that the
|
|
|
|
|
;; child process in the chroot can load modules (we would bind-mount
|
|
|
|
|
;; them but for some reason that fails with EINVAL -- XXX).
|
|
|
|
|
(mkdir-p "/root/share")
|
|
|
|
|
(mkdir-p "/root/lib")
|
|
|
|
|
(mount "none" "/root/share" "tmpfs")
|
|
|
|
|
(mount "none" "/root/lib" "tmpfs")
|
|
|
|
|
(copy-recursively "/share" "/root/share"
|
|
|
|
|
#:log (%make-void-port "w"))
|
|
|
|
|
(copy-recursively "/lib" "/root/lib"
|
|
|
|
|
#:log (%make-void-port "w")))
|
|
|
|
|
|
|
|
|
|
(if to-load
|
|
|
|
|
(begin
|
|
|
|
|
(switch-root "/root")
|
|
|
|
|
(format #t "loading '~a'...\n" to-load)
|
|
|
|
|
|
|
|
|
|
;; TODO: Remove /lib, /share, and /loader.go.
|
|
|
|
|
(primitive-load to-load)
|
|
|
|
|
|
|
|
|
|
(format (current-error-port)
|
|
|
|
|
"boot program '~a' terminated, rebooting~%"
|
|
|
|
|
to-load)
|
|
|
|
|
(sleep 2)
|
|
|
|
|
(reboot))
|
|
|
|
|
(begin
|
|
|
|
|
(display "no boot file passed via '--load'\n")
|
|
|
|
|
(display "entering a warm and cozy REPL\n")
|
|
|
|
|
(start-repl)))))))
|
2014-01-29 21:57:56 +01:00
|
|
|
|
|
2013-08-29 00:04:04 +02:00
|
|
|
|
;;; linux-initrd.scm ends here
|