Change of email and signing key.
[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 [ -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 
111             i=$((i-1))
112             IF=$NETNS$i$MAC
113             ifup $IF
114         else
115             # set up veth with specified mac address
116             ip link add $IF type veth peer name eth$i address $MAC netns $NETNS
117         fi
118         ip link set $IF up
119         BR="${C%=*}"
120         if [ -z "$BR" ] ; then
121             ifup $IF
122         else
123             brctl addif $BR $IF
124         fi
125         i=$((i+1))
126     done
127 }
128
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
131 setup_overlay() {
132     local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
133
134     echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
135
136     if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
137         die "$LIVE already has a mount"
138     fi
139
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"
149
150     # UPPER is the same as LOWER then skip the overlay mount
151     if [ "$UPPER" != "$LOWER" ] ; then
152         # sanity check
153         [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
154
155         env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
156
157         OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
158         if ! mount -t overlay "$OLY" $1 $2 ; then
159             umount -R "$UPPER/dev"
160             umount "$UPPER/run"
161             die "Cannot set up the overlay mount $2"
162         fi
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 $LOWER
167     fi
168
169     grep ^SHARE= "$CONFIG" | while read A ; do
170         B="$(echo ${A#SHARE=})"
171         D="$(realpath "$B")"
172         [ "$D" = "$LOWER" ] && continue
173         if [ -d "$D" ] ; then
174             echo bind mount $D onto $LIVE$B
175             mkdir -p $LIVE$D
176             mount --bind $D $LIVE$B
177         fi
178     done
179
180     env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER" 
181 }
182
183 # Find the "unshare" process for $1 and echo the its pid and the pids
184 # of its child processes.
185 is_live() {
186     local NAME=$1
187     local USPID="$(pgrep -f "unshare.* $NAME ")"
188     [ -z "$USPID" ] && return 1
189     echo "$USPID $(ps -hopid --ppid=$USPID)"
190 }
191
192 # Find all overlay-boot processes and list their config files
193 list_running() {
194     pgrep -a overlay-boot | awk '{print $4}'
195 }
196
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
206 }