1 # This file implements common functions for all boot scripts
3 # Rerun with sudo if needed
4 [ $(id -u) = 0 ] || exec sudo $0 $@
6 export ACTION="$(basename $0)"
8 # Function to write a message and exit with error code
18 # Function to setup subhost name and log file
21 [ -r "$CONFIG" ] || die "Cannot use $CONFIG"
22 config NAME "$(basename $CONFIG .conf)"
23 config LOG /tmp/oly-$NAME.log
26 # Function to set up all subhost configuration
30 BASE="$(cd $(dirname $CONFIG); realpath $BASE)"
31 [ -z "$BASE" ] && die "BASE is unset; bogus $CONFIG ?"
32 [ -d "$BASE" ] || die "$BASE is not a directory; bogus $CONFIG ?"
33 cd "$BASE" || die "$BASE is inaccessible"
36 config LIVE "$BASE/live"
37 config UPPER "$BASE/root"
38 config WORK "$BASE/work"
40 config START "networking ssh"
41 config PREMOUNT "$PROGRAMDIR/overlay-premount"
42 config POSTMOUNT "$PROGRAMDIR/overlay-postmount"
43 config INIT "$PROGRAMDIR/overlay-init"
47 # function to reverse the $* words
50 for w in $* ; do OUT="$w $OUT" ; done
54 # grab and set a configuration variable
55 # $1 = variable, [ $2 = default .. error otherwise ]
59 $(sed "/^$1=.*/{s|^$1=||;s|^\\s*||;s|\\s*\$||;b};d" $CONFIG)
62 [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
63 V="$2" # use the given default
64 elif [ -z "${V##!*}" ] ; then
68 [ -z "$W" ] && die "bad $1 config: $V"
75 # Install a default $1/etc/network/interfaces on the subhost root $1
77 [ -r $1/etc/network/interfaces ] && return 0
78 mkdir -p $1/etc/network
79 cat <<EOF >> $1/etc/network/interfaces
80 # Generated for $NAME subhost
82 iface lo inet loopback
84 for IF in $(ip netns exec $NAME ip link show | grep "^eth") ; do
85 cat <<EOF >> $1/etc/network/interfaces
88 iface eth$i inet manual
93 # Setup the network namespace for the given $CABLES
94 # $1=netns ( $2="br=mac" .. )
95 # br is optional, mac is optional.
96 # If mac is .N then it's taken as vlan tag on prior outer interface
97 # (with ifup configuration) and the inner interface is left alone.
99 local NETNS BR IF MAC C i ADD
106 if ip link show $IF > /dev/null 2>&1 ; then
107 : # The interface exists already (bad badness); let things fail
108 elif ifquery --state $IF >/dev/null 2>&1 ; then
109 # doesn't exist but has residue state; quiet cleanup
110 ifdown -f $IF > /dev/null 2>&1
112 if [ -z "$MAC" ] ; then
113 # set up veth with "random" mac address
114 ip link add $IF type veth peer name eth$i netns $NETNS
115 elif [ -z "${MAC%%.*}" ] ; then
116 # set up a host vlan with specified tag on previous eth
121 # set up veth with specified mac address
122 ip link add $IF type veth peer name eth$i address $MAC netns $NETNS
125 if [ -z "$BR" ] ; then
126 ifup $IF || ip link set $IF up
135 # Set up an overlay for $name on $live, with a new tmpfs on its /run,
136 # and "install" a "reaper" as the upcoming pid 1
138 local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
140 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
142 if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
143 die "$LIVE already has a mount"
146 [ -d "$UPPER" ] || die "UPPER=$UPPER is not a directory"
147 [ -d "$LOWER" ] || die "LOWER=LOWPER is not a directory"
148 [ -d "$LIVE" ] || die "LOWER=LOWPER is not a directory"
149 [ -x "${PREMOUNT%% *}" ] || die "PREMOUNT=${PREMOUNT%% *} not executable"
150 [ -f "${PREMOUNT%% *}" ] || die "PREMOUNT='$PREMOUNT' is not a command"
151 [ -x "${POSTMOUNT%% *}" ] || \
152 die "POSTMOUNT=${POSTMOUNT%% *} not executable"
153 [ -f "${POSTMOUNT%% *}" ] || \
154 die "POSTMOUNT='$POSTMOUNT' is not a command"
156 # UPPER is the same as LOWER then skip the overlay mount
157 if [ "$UPPER" != "$LOWER" ] ; then
159 [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
161 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
163 OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
164 if ! mount -t overlay "$OLY" $1 $2 ; then
165 umount -R "$UPPER/dev"
167 die "Cannot set up the overlay mount $2"
169 elif [ "$LIVE" != "$UPPER" ] ; then
170 # With UPPER = LOWER we rather make a bind mount to LIVE
171 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
172 mount --bind $UPPER $LIVE
175 grep ^SHARE= "$CONFIG" | while read A ; do
176 B="$(echo ${A#SHARE=})"
178 [ "$D" = "$LOWER" ] && continue
179 if [ -d "$D" ] ; then
180 echo bind mount $D onto $LIVE$B
182 mount --bind $D $LIVE$B
186 env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER"
189 # Find the "unshare" process for $1 and echo the its pid and the pids
190 # of its child processes.
193 local USPID="$(pgrep -f "unshare.* $NAME ")"
194 [ -z "$USPID" ] && return 1
195 echo "$USPID $(ps -hopid --ppid=$USPID)"
198 # Find all overlay-boot processes and list their config files
200 pgrep -a overlay-boot | awk '{print $4}'
203 # Start cgroup v2 cpuset accounting if enabled.
204 # Needs manual enabling, with:
205 # mount -t cgroup2 cgroup2 /sys/fs/cgroup
206 setup_cgroup2_accounting() {
207 local NAME="$1" ME="$2"
208 local ACCDIR="$(awk '$3 == "cgroup2" {print $2; exit}' /proc/mounts)"
209 [ -z "$ACCDIR" ] && return 0
210 mkdir -p "$ACCDIR/$NAME"
211 echo "$ME" > $ACCDIR/$NAME/cgroup.procs