SHARE to mount host dir onto LIVE
[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 setup_veth_cables() {
96     local NETNS BR IF MAC C i ADD
97     NETNS="$1"
98     shift 1
99     i=0
100     for C in "$@" ; do
101         IF=$NETNS$i
102         MAC="${C#*=}"
103         [ -z "$MAC" ] || MAC="address $MAC"
104         ip link add $IF type veth peer name eth$i $MAC netns $NETNS
105         ip link set $IF up
106         BR="${C%=*}"
107         if [ -z "$BR" ] ; then
108             ip link set $IF
109             ifup $IF
110         else
111             brctl addif $BR $IF
112         fi
113         i=$((i+1))
114     done
115 }
116
117 # Set up an overlay for $name on $live, with a new tmpfs on its /run,
118 # and "install" a "reaper" as the upcoming pid 1
119 setup_overlay() {
120     local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
121
122     echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
123
124     if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
125         die "$LIVE already has a mount"
126     fi
127
128     [ -d "$UPPER" ] || die "UPPER=$UPPER is not a directory"
129     [ -d "$LOWER" ] || die "LOWER=LOWPER is not a directory"
130     [ -d "$LIVE" ] || die "LOWER=LOWPER is not a directory"
131     [ -x "${PREMOUNT%% *}" ] || die "PREMOUNT=${PREMOUNT%% *} not executable"
132     [ -f "${PREMOUNT%% *}" ] || die "PREMOUNT='$PREMOUNT' is not a command"
133     [ -x "${POSTMOUNT%% *}" ] || \
134         die "POSTMOUNT=${POSTMOUNT%% *} not executable"
135     [ -f "${POSTMOUNT%% *}" ] || \
136         die "POSTMOUNT='$POSTMOUNT' is not a command"
137
138     # UPPER is the same as LOWER then skip the overlay mount
139     if [ "$UPPER" != "$LOWER" ] ; then
140         # sanity check
141         [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
142
143         env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
144
145         OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
146         if ! mount -t overlay "$OLY" $1 $2 ; then
147             umount -R "$UPPER/dev"
148             umount "$UPPER/run"
149             die "Cannot set up the overlay mount $2"
150         fi
151     elif [ "$LIVE" != "$UPPER" ] ; then
152         # With UPPER = LOWER we rather make a bind mount to LIVE
153         env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
154         mount --bind $UPPER $LOWER
155     fi
156
157     grep ^SHARE= "$CONFIG" | while read A ; do
158         B="$(echo ${A#SHARE=})"
159         D="$(realpath "$B")"
160         [ "$D" = "$LOWER" ] && continue
161         if [ -d "$D" ] ; then
162             echo bind mount $D onto $LIVE$B
163             mkdir -p $LIVE$D
164             mount --bind $D $LIVE$B
165         fi
166     done
167
168     env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER" 
169 }
170
171 # Find the "unshare" process for $1 and echo the its pid and the pids
172 # of its child processes.
173 is_live() {
174     local NAME=$1
175     local USPID="$(pgrep -f "unshare.* $NAME ")"
176     [ -z "$USPID" ] && return 1
177     echo "$USPID $(ps -hopid --ppid=$USPID)"
178 }
179
180 # Find all overlay-boot processes and list their config files
181 list_running() {
182     pgrep -a overlay-boot | awk '{print $4}'
183 }
184
185 # Start cgroup v2 cpuset accounting if enabled.
186 # Needs manual enabling, with:
187 #    mount -t cgroup2 cgroup2 /sys/fs/cgroup
188 setup_cgroup2_accounting() {
189     local NAME="$1" ME="$2"
190     local ACCDIR="$(awk '$3 == "cgroup2" {print $2; exit}' /proc/mounts)"
191     [ -z "$ACCDIR" ] && return 0
192     mkdir -p "$ACCDIR/$NAME"
193     echo "$ME" > $ACCDIR/$NAME/cgroup.procs
194 }