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
14 # Function to setup subhost name and log file
17 [ -r "$CONFIG" ] || die "Cannot use $CONFIG"
18 config NAME "$(basename $CONFIG .conf)"
19 config LOG /tmp/oly-$NAME.log
22 # Function to set up all subhost configuration
26 BASE="$(cd $(dirname $CONFIG); realpath $BASE)"
27 [ -z "$BASE" ] && die "BASE is unset; bogus $CONFIG ?"
28 [ -d "$BASE" ] || die "$BASE is not a directory; bogus $CONFIG ?"
29 cd "$BASE" || die "$BASE is inaccessible"
32 config LIVE "$BASE/live"
33 config UPPER "$BASE/root"
34 config WORK "$BASE/work"
36 config START "networking ssh"
37 config PREMOUNT "$PROGRAMDIR/overlay-premount"
38 config POSTMOUNT "$PROGRAMDIR/overlay-postmount"
39 config INIT "$PROGRAMDIR/overlay-init"
43 # function to reverse the $* words
46 for w in $* ; do OUT="$w $OUT" ; done
50 # grab and set a configuration variable
51 # $1 = variable, [ $2 = default .. error otherwise ]
55 $(sed "/^$1=.*/{s|^$1=||;s|^\\s*||;s|\\s*\$||;b};d" $CONFIG)
58 [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
59 V="$2" # use the given default
60 elif [ -z "${V##!*}" ] ; then
64 [ -z "$W" ] && die "bad $1 config: $V"
71 # Install a default $1/etc/network/interfaces on the subhost root $1
73 [ -r $1/etc/network/interfaces ] && return 0
74 mkdir -p $1/etc/network
75 cat <<EOF >> $1/etc/network/interfaces
76 # Generated for $NAME subhost
78 iface lo inet loopback
80 for IF in $(ip netns exec $NAME ip link show | grep "^eth") ; do
81 cat <<EOF >> $1/etc/network/interfaces
84 iface eth$i inet manual
89 # Setup the network namespace for the given $CABLES
90 # $1=netns ( $2="br=mac" .. )
92 local NETNS BR IF MAC C i ADD
99 [ -z "$MAC" ] || MAC="address $MAC"
100 ip link add $IF type veth peer name eth$i $MAC netns $NETNS
103 if [ -z "$BR" ] ; then
113 # Set up an overlay for $name on $live, with a new tmpfs on its /run,
114 # and "install" a "reaper" as the upcoming pid 1
116 local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
118 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
120 if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
121 die "$LIVE already has a mount"
124 [ -d "$UPPER" ] || die "UPPER=$UPPER is not a directory"
125 [ -d "$LOWER" ] || die "LOWER=LOWPER is not a directory"
126 [ -d "$LIVE" ] || die "LOWER=LOWPER is not a directory"
127 [ -x "${PREMOUNT%% *}" ] || die "PREMOUNT=${PREMOUNT%% *} not executable"
128 [ -f "${PREMOUNT%% *}" ] || die "PREMOUNT='$PREMOUNT' is not a command"
129 [ -x "${POSTMOUNT%% *}" ] || \
130 die "POSTMOUNT=${POSTMOUNT%% *} not executable"
131 [ -f "${POSTMOUNT%% *}" ] || \
132 die "POSTMOUNT='$POSTMOUNT' is not a command"
134 # UPPER is the same as LOWER then skip the overlay mount
135 if [ "$UPPER" != "$LOWER" ] ; then
137 [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
139 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
141 OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
142 if ! mount -t overlay "$OLY" $1 $2 ; then
143 umount -R "$UPPER/dev"
145 die "Cannot set up the overlay mount $2"
147 elif [ "$LIVE" != "$UPPER" ] ; then
148 # With UPPER = LOWER we rather make a bind mount to LIVE
149 env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
150 mount --bind $UPPER $LOWER
153 grep ^SHARE= "$CONFIG" | while read A ; do
154 B="$(echo ${A#SHARE=})"
155 D="$(realpath "$LOWER$B")"
156 [ "$D" = "$LOWER" ] && continue
157 if [ -d "$D" ] ; then
158 echo bind mount $D onto $LIVE$D
160 mount --bind $D $LIVE$D
164 env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER"
167 # Find the "unshare" process for $1 and echo the its pid and the pids
168 # of its child processes.
171 local USPID="$(pgrep -f "unshare.* $NAME ")"
172 [ -z "$USPID" ] && return 1
173 echo "$USPID $(ps -hopid --ppid=$USPID)"
176 # Find all overlay-boot processes and list their config files
178 pgrep -a overlay-boot | awk '{print $4}'
181 # Start cgroup v2 cpuset accounting if enabled.
182 # Needs manual enabling, with:
183 # mount -t cgroup2 cgroup2 /sys/fs/cgroup
184 setup_cgroup2_accounting() {
185 local NAME="$1" ME="$2"
186 local ACCDIR="$(awk '$3 == "cgroup2" {print $2; exit}' /proc/mounts)"
187 [ -z "$ACCDIR" ] && return 0
188 mkdir -p "$ACCDIR/$NAME"
189 echo "$ME" > $ACCDIR/$NAME/cgroup.procs