1 # This file implements common functions for all boot methods
8 # grab and set a configuration variable
9 # $1 = variable, [ $2 = default .. error otherwise ]
11 eval $1="$(sed "/^$1=.*/{s|^$1=||;b};d" $CONFIG)"
12 [ -z "$(eval echo "\$$1")" ] || return 0
13 [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
18 # Unless the subhost already has a private /etc/network/interfaces,
19 # install an "empty" one
21 [ -r $UPPER/etc/network/interfaces ] && return 0
22 [ "$UPPER/etc/network" = "/etc/network" ] && exit 1
23 mkdir -p $UPPER/etc/network
24 cat <<EOF > $UPPER/etc/network/interfaces
25 # Generated for $NAME subhost
27 iface lo inet loopback
32 # Setup the network namespace for the given $CABLES
33 # $1=netns ( $2="br=mac" .. )
35 local NETNS BR IF MAC C i ADD
40 setup_networking || ADD=true
44 [ -z "$MAC" ] || MAC="address $MAC"
45 ip link add $IF type veth peer name eth$i $MAC netns $NETNS
47 $ADD && cat <<EOF >> $UPPER/etc/network/interfaces
50 iface eth$i inet manual
53 if [ -z "$BR" ] ; then
63 # (name live system root work)
64 # Set up an overlay fmr $name on $live, with a new tmpfs on its /run,
65 # and "install" a "reaper" as the upcoming pid 1
67 local LIVE="$2" LOWER="$3" UPPER="$4" ROOT
69 if grep -q "$1 $2" /proc/mounts ; then
70 die "$1 is already mounted"
73 if [ -f "${UPPER%% *}" ] ; then
74 if [ -x "${UPPER%% *}" ] ; then
75 echo "${UPPER%% *} appears to be executable" >&2
76 # Giving a program/script as UPPER= asks for running this
77 # first, to make a root filesystem available. The script takes
78 # ACTION "setup" and "teardown", and on "setup" it must tell
79 # where the ROOT is set up.
80 ROOT="$(env ACTION=setup $UPPER)"
81 if [ ! -d "$ROOT" ] ; then
83 die "root setup failed: $UPPER"
86 ## Now falling down to "normal overlay" setup
88 die "${UPPER%% *} (root setup program/script) is not executable"
92 # LIVE is the same as LOWER then skip the overlay; just assume
93 # a proper chroot system exists at LIVE.
94 if [ "$LIVE" != "$LOWER" ] ; then
97 mount -t tmpfs -osize=100M tmpfs "$UPPER/run"
98 mkdir -p "$UPPER/run/lock"
100 mkdir -p "$UPPER/dev"
101 mount -t tmpfs -osize=100M tmpfs "$UPPER/dev"
102 mknod -m 622 "$UPPER/dev/console" c 5 1
103 mknod -m 666 "$UPPER/dev/null" c 1 3
104 mknod -m 666 "$UPPER/dev/zero" c 1 5
105 mknod -m 666 "$UPPER/dev/ptmx" c 5 2
106 mknod -m 666 "$UPPER/dev/tty" c 5 0
107 mknod -m 444 "$UPPER/dev/random" c 1 8
108 mknod -m 444 "$UPPER/dev/urandom" c 1 9
109 chown root:tty "$UPPER/dev/console"
110 chown root:tty "$UPPER/dev/ptmx"
111 chown root:tty "$UPPER/dev/tty"
112 ln -sTf /proc/self/fd "$UPPER/dev/fd"
113 ln -sTf /proc/self/fd/0 "$UPPER/dev/stdin"
114 ln -sTf /proc/self/fd/1 "$UPPER/dev/stdout"
115 ln -sTf /proc/self/fd/2 "$UPPER/dev/stderr"
116 ln -sTf /proc/kcore "$UPPER/dev/core"
117 mkdir "$UPPER/dev/shm"
118 mkdir "$UPPER/dev/pts"
119 chmod 1777 "$UPPER/dev/shm"
121 OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
122 if mount -t overlay "$OLY" $1 $2 ; then
125 umount -R "$UPPER/dev"
127 die "Cannot set up the overlay mount $2"
131 echo "Installing $OVERLAYDIR/reaper to $LIVE/.reaper"
132 cp -p $OVERLAYDIR/reaper $LIVE/.reaper
141 # find the upperdir option for an overlay mount line
143 sed 's/.*upperdir=\([^,]*\).*/\1/'
146 # Check if $1 is "live" and echo the
147 # unshare and reaper process pids
150 local USPID="$(pgrep -f "unshare.* $NAME ")"
151 [ -z "$USPID" ] && return 1
152 echo $USPID $(pgrep -f ".reaper $NAME")
156 for C in $(pgrep -a overlay-boot | awk '{print $4}') ; do
157 eval NAME="$(sed "/^NAME=.*/{s|^NAME=||;b};d" $C)"
158 [ -z "$NAME" ] && NAME=$(basename $C .conf)