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 [ -z "$MAC" ] ; then
107 # set up veth with "random" mac address
108 ip link add $IF type veth peer name eth$i netns $NETNS
109 elif [ -z "${MAC%%.*}" ] ; then
110 # set up a host vlan with specified tag on previous eth
115 # set up veth with specified mac address
116 ip link add $IF type veth peer name eth$i address $MAC netns $NETNS
119 if [ -z "$BR" ] ; then
120 ifup $IF || ip link set $IF up
129 # Set up an overlay for $name on $live, with a new tmpfs on its /run,
130 # and "install" a "reaper" as the upcoming pid 1
132 local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
134 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
136 if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
137 die "$LIVE already has a mount"
140 [ -d "$UPPER" ] || die "UPPER=$UPPER is not a directory"
141 [ -d "$LOWER" ] || die "LOWER=LOWPER is not a directory"
142 [ -d "$LIVE" ] || die "LOWER=LOWPER is not a directory"
143 [ -x "${PREMOUNT%% *}" ] || die "PREMOUNT=${PREMOUNT%% *} not executable"
144 [ -f "${PREMOUNT%% *}" ] || die "PREMOUNT='$PREMOUNT' is not a command"
145 [ -x "${POSTMOUNT%% *}" ] || \
146 die "POSTMOUNT=${POSTMOUNT%% *} not executable"
147 [ -f "${POSTMOUNT%% *}" ] || \
148 die "POSTMOUNT='$POSTMOUNT' is not a command"
150 # UPPER is the same as LOWER then skip the overlay mount
151 if [ "$UPPER" != "$LOWER" ] ; then
153 [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
155 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
157 OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
158 if ! mount -t overlay "$OLY" $1 $2 ; then
159 umount -R "$UPPER/dev"
161 die "Cannot set up the overlay mount $2"
163 elif [ "$LIVE" != "$UPPER" ] ; then
164 # With UPPER = LOWER we rather make a bind mount to LIVE
165 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
166 mount --bind $UPPER $LIVE
169 grep ^SHARE= "$CONFIG" | while read A ; do
170 B="$(echo ${A#SHARE=})"
172 [ "$D" = "$LOWER" ] && continue
173 if [ -d "$D" ] ; then
174 echo bind mount $D onto $LIVE$B
176 mount --bind $D $LIVE$B
180 env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER"
183 # Find the "unshare" process for $1 and echo the its pid and the pids
184 # of its child processes.
187 local USPID="$(pgrep -f "unshare.* $NAME ")"
188 [ -z "$USPID" ] && return 1
189 echo "$USPID $(ps -hopid --ppid=$USPID)"
192 # Find all overlay-boot processes and list their config files
194 pgrep -a overlay-boot | awk '{print $4}'
197 # Start cgroup v2 cpuset accounting if enabled.
198 # Needs manual enabling, with:
199 # mount -t cgroup2 cgroup2 /sys/fs/cgroup
200 setup_cgroup2_accounting() {
201 local NAME="$1" ME="$2"
202 local ACCDIR="$(awk '$3 == "cgroup2" {print $2; exit}' /proc/mounts)"
203 [ -z "$ACCDIR" ] && return 0
204 mkdir -p "$ACCDIR/$NAME"
205 echo "$ME" > $ACCDIR/$NAME/cgroup.procs