Add bind-mount for /etc/adjtime to make subhost use host clock without ado
[rrq/overlay-boot.git] / functions
1 # This file implements common functions for all boot scripts
2
3 # Rerun with sudo if needed
4 [ $(id -u) = 0 ] || exec sudo $0 $@
5
6 export ACTION="$(basename $0)"
7
8 # Function to write a message and exit with error code
9 die() {
10     echo "$*" >&2
11     exit 1
12 }
13
14 beginswith() {
15     [ "$1" != "${1#$2}" ]
16 }
17
18 # Function to setup subhost name and log file
19 subhost_name() {
20     CONFIG="$1"
21     [ -r "$CONFIG" ] || die "Cannot use $CONFIG"
22     config NAME "$(basename $CONFIG .conf)"
23     config LOG /tmp/oly-$NAME.log
24 }
25
26 # Function to set up all subhost configuration
27 subhost_config() {
28
29     config BASE
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"
34
35     config CABLES ""
36     config LIVE "$BASE/live"
37     config UPPER "$BASE/root"
38     config WORK "$BASE/work"
39     config LOWER "/"
40     config START "networking ssh"
41     config PREMOUNT "$PROGRAMDIR/overlay-premount"
42     config POSTMOUNT "$PROGRAMDIR/overlay-postmount"
43     config INIT "$PROGRAMDIR/overlay-init"
44     config RAM_SIZE 50M
45 }
46
47 # function to reverse the $* words
48 reverse() {
49     local OUT=""
50     for w in $* ; do OUT="$w $OUT" ; done
51     echo "${OUT% }"
52 }
53
54 # grab and set a configuration variable
55 # $1 = variable, [ $2 = default .. error otherwise ]
56 config() {
57     local V W
58     read V <<EOF
59 $(sed "/^$1=.*/{s|^$1=||;s|^\\s*||;s|\\s*\$||;b};d" $CONFIG)
60 EOF
61     if [ -z "$V" ] ; then
62         [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
63         V="$2" # use the given default
64     elif [ -z "${V##!*}" ] ; then
65         read W <<EOF
66 $(${V#!})
67 EOF
68         [ -z "$W" ] && die "bad $1 config: $V"
69         V="$W"
70     fi
71     eval $1="'$V'"
72     eval echo "$1=$V" >&2
73 }
74
75 # Install a default $1/etc/network/interfaces on the subhost root $1
76 setup_networking() {
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
81 auto lo
82 iface lo inet loopback
83 EOF
84     for IF in $(ip netns exec $NAME ip link show | grep "^eth") ; do
85         cat <<EOF >> $1/etc/network/interfaces
86
87 auto eth$i
88 iface eth$i inet manual
89 EOF
90     done
91 }
92
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.
98 setup_veth_cables() {
99     local NETNS BR IF MAC C i ADD
100     NETNS="$1"
101     shift 1
102     i=0
103     for C in "$@" ; do
104         IF=$NETNS$i
105         MAC="${C#*=}"
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
111         fi
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 
117             i=$((i-1))
118             IF=$NETNS$i$MAC
119             ifup $IF
120         else
121             # set up veth with specified mac address
122             ip link add $IF type veth peer name eth$i address $MAC netns $NETNS
123         fi
124         BR="${C%=*}"
125         if [ -z "$BR" ] ; then
126             ifup $IF || ip link set $IF up
127         else
128             ip link set $IF up
129             brctl addif $BR $IF
130         fi
131         i=$((i+1))
132     done
133 }
134
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
137 setup_overlay() {
138     local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
139
140     echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
141
142     if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
143         die "$LIVE already has a mount"
144     fi
145
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"
155
156     # UPPER is the same as LOWER then skip the overlay mount
157     if [ "$UPPER" != "$LOWER" ] ; then
158         # sanity check
159         [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
160
161         env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
162
163         OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
164         if ! mount -t overlay "$OLY" $1 $2 ; then
165             umount -R "$UPPER/dev"
166             umount "$UPPER/run"
167             die "Cannot set up the overlay mount $2"
168         fi
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
173     fi
174
175     grep ^SHARE= "$CONFIG" | while read A ; do
176         B="$(echo ${A#SHARE=})"
177         D="$(realpath "$B")"
178         [ "$D" = "$LOWER" ] && continue
179         if [ -d "$D" ] ; then
180             echo bind mount $D onto $LIVE$B
181             mkdir -p $LIVE$D
182             mount --bind $D $LIVE$B
183         fi
184     done
185
186     env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER" 
187 }
188
189 # Find the "unshare" process for $1 and echo the its pid and the pids
190 # of its child processes.
191 is_live() {
192     local NAME=$1
193     local USPID="$(pgrep -f "unshare.* $NAME ")"
194     [ -z "$USPID" ] && return 1
195     echo "$USPID $(ps -hopid --ppid=$USPID)"
196 }
197
198 # Find all overlay-boot processes and list their config files
199 list_running() {
200     pgrep -a overlay-boot | awk '{print $4}'
201 }
202
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
212 }