1 # This file implements common functions for all boot scripts
3 # Rerun with sudo if needed
4 [ $(id -u) = 0 ] || exec sudo $0 $@
6 # Function to write a message and exit with error code
12 # Function to setup subhost name and log file
15 [ -r "$CONFIG" ] || die "Cannot use $CONFIG"
16 config NAME "$(basename $CONFIG .conf)"
17 config LOG /tmp/oly-$NAME.log
20 # Function to set up all subhost configuration
24 BASE="$(cd $(dirname $CONFIG); realpath $BASE)"
25 [ -z "$BASE" ] && die "BASE is unset; bogus $CONFIG ?"
26 [ -d "$BASE" ] || die "$BASE is not a directory; bogus $CONFIG ?"
27 cd "$BASE" || die "$BASE is inaccessible"
30 config LIVE "$BASE/live"
31 config UPPER "$BASE/root"
32 config WORK "$BASE/work"
34 config START "!networking ssh"
35 config PREMOUNT "$PROGRAMDIR/overlay-premount"
36 config POSTMOUNT "$PROGRAMDIR/overlay-postmount"
37 config INIT "$PROGRAMDIR/overlay-init"
41 # function to reverse the $* words
44 for w in $* ; do OUT="$w $OUT" ; done
48 # grab and set a configuration variable
49 # $1 = variable, [ $2 = default .. error otherwise ]
53 $(sed "/^$1=.*/{s|^$1=||;s|^\\s*||;s|\\s*\$||;b};d" $CONFIG)
56 [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
57 V="$2" # use the given default
58 elif [ -z "${V##!*}" ] ; then
62 [ -z "$W" ] && die "bad $1 config: $V"
69 # Install a default $1/etc/network/interfaces on the subhost root $1
71 [ -r $1/etc/network/interfaces ] && return 0
72 mkdir -p $1/etc/network
73 cat <<EOF >> $1/etc/network/interfaces
74 # Generated for $NAME subhost
76 iface lo inet loopback
78 for IF in $(ip netns exec $NAME ip link show | grep "^eth") ; do
79 cat <<EOF >> $1/etc/network/interfaces
82 iface eth$i inet manual
87 # Setup the network namespace for the given $CABLES
88 # $1=netns ( $2="br=mac" .. )
90 local NETNS BR IF MAC C i ADD
97 [ -z "$MAC" ] || MAC="address $MAC"
98 ip link add $IF type veth peer name eth$i $MAC netns $NETNS
101 if [ -z "$BR" ] ; then
111 # Set up an overlay for $name on $live, with a new tmpfs on its /run,
112 # and "install" a "reaper" as the upcoming pid 1
114 local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
116 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
118 if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
119 die "$LIVE already has a mount"
122 [ -d "$UPPER" ] || die "UPPER=$UPPER is not a directory"
123 [ -d "$LOWER" ] || die "LOWER=LOWPER is not a directory"
124 [ -d "$LIVE" ] || die "LOWER=LOWPER is not a directory"
125 [ -x "${PREMOUNT%% *}" ] || die "PREMOUNT=${PREMOUNT%% *} not executable"
126 [ -f "${PREMOUNT%% *}" ] || die "PREMOUNT='$PREMOUNT' is not a command"
127 [ -x "${POSTMOUNT%% *}" ] || \
128 die "POSTMOUNT=${POSTMOUNT%% *} not executable"
129 [ -f "${POSTMOUNT%% *}" ] || \
130 die "POSTMOUNT='$POSTMOUNT' is not a command"
132 # UPPER is the same as LOWER then skip the overlay mount
133 if [ "$UPPER" != "$LOWER" ] ; then
135 [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
137 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
139 OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
140 if ! mount -t overlay "$OLY" $1 $2 ; then
141 umount -R "$UPPER/dev"
143 die "Cannot set up the overlay mount $2"
145 elif [ "$LIVE" != "$UPPER" ] ; then
146 # With UPPER = LOWER we rather make a bind mount to LIVE
147 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
148 mount --bind $UPPER $LOWER
151 env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER"
154 # Find the "unshare" process for $1 and echo the its pid and the pids
155 # of its child processes.
158 local USPID="$(pgrep -f "unshare.* $NAME ")"
159 [ -z "$USPID" ] && return 1
160 echo "$USPID $(ps -hopid --ppid=$USPID)"
163 # Find all overlay-boot processes and list their config files
165 pgrep -a overlay-boot | awk '{print $4}'