distribute subhosts.conf
[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 # Function to setup subhost name and log file
15 subhost_name() {
16     CONFIG="$1"
17     [ -r "$CONFIG" ] || die "Cannot use $CONFIG"
18     config NAME "$(basename $CONFIG .conf)"
19     config LOG /tmp/oly-$NAME.log
20 }
21
22 # Function to set up all subhost configuration
23 subhost_config() {
24
25     config BASE
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"
30
31     config CABLES ""
32     config LIVE "$BASE/live"
33     config UPPER "$BASE/root"
34     config WORK "$BASE/work"
35     config LOWER "/"
36     config START "networking ssh"
37     config PREMOUNT "$PROGRAMDIR/overlay-premount"
38     config POSTMOUNT "$PROGRAMDIR/overlay-postmount"
39     config INIT "$PROGRAMDIR/overlay-init"
40     config RAM_SIZE 50M
41 }
42
43 # function to reverse the $* words
44 reverse() {
45     local OUT=""
46     for w in $* ; do OUT="$w $OUT" ; done
47     echo "${OUT% }"
48 }
49
50 # grab and set a configuration variable
51 # $1 = variable, [ $2 = default .. error otherwise ]
52 config() {
53     local V W
54     read V <<EOF
55 $(sed "/^$1=.*/{s|^$1=||;s|^\\s*||;s|\\s*\$||;b};d" $CONFIG)
56 EOF
57     if [ -z "$V" ] ; then
58         [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
59         V="$2" # use the given default
60     elif [ -z "${V##!*}" ] ; then
61         read W <<EOF
62 $(${V#!})
63 EOF
64         [ -z "$W" ] && die "bad $1 config: $V"
65         V="$W"
66     fi
67     eval $1="'$V'"
68     eval echo "$1=$V" >&2
69 }
70
71 # Install a default $1/etc/network/interfaces on the subhost root $1
72 setup_networking() {
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
77 auto lo
78 iface lo inet loopback
79 EOF
80     for IF in $(ip netns exec $NAME ip link show | grep "^eth") ; do
81         cat <<EOF >> $1/etc/network/interfaces
82
83 auto eth$i
84 iface eth$i inet manual
85 EOF
86     done
87 }
88
89 # Setup the network namespace for the given $CABLES
90 # $1=netns ( $2="br=mac" .. )
91 setup_veth_cables() {
92     local NETNS BR IF MAC C i ADD
93     NETNS="$1"
94     shift 1
95     i=0
96     for C in "$@" ; do
97         IF=$NETNS$i
98         MAC="${C#*=}"
99         [ -z "$MAC" ] || MAC="address $MAC"
100         ip link add $IF type veth peer name eth$i $MAC netns $NETNS
101         ip link set $IF up
102         BR="${C%=*}"
103         if [ -z "$BR" ] ; then
104             ip link set $IF
105             ifup $IF
106         else
107             brctl addif $BR $IF
108         fi
109         i=$((i+1))
110     done
111 }
112
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
115 setup_overlay() {
116     local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
117
118     echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
119
120     if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
121         die "$LIVE already has a mount"
122     fi
123
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"
133
134     # UPPER is the same as LOWER then skip the overlay mount
135     if [ "$UPPER" != "$LOWER" ] ; then
136         # sanity check
137         [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
138
139         env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
140
141         OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
142         if ! mount -t overlay "$OLY" $1 $2 ; then
143             umount -R "$UPPER/dev"
144             umount "$UPPER/run"
145             die "Cannot set up the overlay mount $2"
146         fi
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
151     fi
152
153     env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER" 
154 }
155
156 # Find the "unshare" process for $1 and echo the its pid and the pids
157 # of its child processes.
158 is_live() {
159     local NAME=$1
160     local USPID="$(pgrep -f "unshare.* $NAME ")"
161     [ -z "$USPID" ] && return 1
162     echo "$USPID $(ps -hopid --ppid=$USPID)"
163 }
164
165 # Find all overlay-boot processes and list their config files
166 list_running() {
167     pgrep -a overlay-boot | awk '{print $4}'
168 }
169
170 # Start cgroup v2 cpuset accounting if enabled.
171 # Needs manual enabling, with:
172 #    mount -t cgroup2 cgroup2 /sys/fs/cgroup
173 setup_cgroup2_accounting() {
174     local NAME="$1" ME="$2"
175     local ACCDIR="$(awk '$3 == "cgroup2" {print $2; exit}' /proc/mounts)"
176     [ -z "$ACCDIR" ] && return 0
177     mkdir -p "$ACCDIR/$NAME"
178     echo "$ME" > $ACCDIR/$NAME/cgroup.procs
179 }