reaper in C (bbonev)
[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 # Function to write a message and exit with error code
7 die() {
8     echo "$*" >&2
9     exit 1
10 }
11
12 # Function to setup subhost name and log file
13 subhost_name() {
14     CONFIG="$1"
15     [ -r "$CONFIG" ] || die "Cannot use $CONFIG"
16     config NAME "$(basename $CONFIG .conf)"
17     config LOG /tmp/oly-$NAME.log
18 }
19
20 # Function to set up all subhost configuration
21 subhost_config() {
22
23     config BASE
24     BASE="$(cd $(dirname $CONFIG); realpath $BASE)"
25     [ -z "$BASE" ] && die "BASE is unset; bogus $CONFIG ?"
26     [ -d "$BASE" ] || die "$BASE is not a directory; bogus $CONFIG ?"
27     cd "$BASE" || die "$BASE is inaccessible"
28
29     config CABLES ""
30     config LIVE "$BASE/live"
31     config UPPER "$BASE/root"
32     config WORK "$BASE/work"
33     config LOWER "/"
34     config START "networking ssh"
35     config PREMOUNT "$PROGRAMDIR/overlay-premount"
36     config POSTMOUNT "$PROGRAMDIR/overlay-postmount"
37     config INIT "$PROGRAMDIR/overlay-init"
38     config RAM_SIZE 50M
39 }
40
41 # function to reverse the $* words
42 reverse() {
43     local OUT=""
44     for w in $* ; do OUT="$w $OUT" ; done
45     echo "${OUT% }"
46 }
47
48 # grab and set a configuration variable
49 # $1 = variable, [ $2 = default .. error otherwise ]
50 config() {
51     local V W
52     read V <<EOF
53 $(sed "/^$1=.*/{s|^$1=||;s|^\\s*||;s|\\s*\$||;b};d" $CONFIG)
54 EOF
55     if [ -z "$V" ] ; then
56         [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
57         V="$2" # use the given default
58     elif [ -z "${V##!*}" ] ; then
59         read W <<EOF
60 $(${V#!})
61 EOF
62         [ -z "$W" ] && die "bad $1 config: $V"
63         V="$W"
64     fi
65     eval $1="'$V'"
66     eval echo "$1=$V" >&2
67 }
68
69 # Install a default $1/etc/network/interfaces on the subhost root $1
70 setup_networking() {
71     [ -r $1/etc/network/interfaces ] && return 0
72     mkdir -p $1/etc/network
73     cat <<EOF >> $1/etc/network/interfaces
74 # Generated for $NAME subhost
75 auto lo
76 iface lo inet loopback
77 EOF
78     for IF in $(ip netns exec $NAME ip link show | grep "^eth") ; do
79         cat <<EOF >> $1/etc/network/interfaces
80
81 auto eth$i
82 iface eth$i inet manual
83 EOF
84     done
85 }
86
87 # Setup the network namespace for the given $CABLES
88 # $1=netns ( $2="br=mac" .. )
89 setup_veth_cables() {
90     local NETNS BR IF MAC C i ADD
91     NETNS="$1"
92     shift 1
93     i=0
94     for C in "$@" ; do
95         IF=$NETNS$i
96         MAC="${C#*=}"
97         [ -z "$MAC" ] || MAC="address $MAC"
98         ip link add $IF type veth peer name eth$i $MAC netns $NETNS
99         ip link set $IF up
100         BR="${C%=*}"
101         if [ -z "$BR" ] ; then
102             ip link set $IF
103             ifup $IF
104         else
105             brctl addif $BR $IF
106         fi
107         i=$((i+1))
108     done
109 }
110
111 # Set up an overlay for $name on $live, with a new tmpfs on its /run,
112 # and "install" a "reaper" as the upcoming pid 1
113 setup_overlay() {
114     local NAME="$1" LIVE="$2" LOWER="$3" UPPER="$4" WORK="$5"
115
116     echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
117
118     if grep -qE "^[^ ]+ $LIVE " /proc/mounts ; then
119         die "$LIVE already has a mount"
120     fi
121
122     [ -d "$UPPER" ] || die "UPPER=$UPPER is not a directory"
123     [ -d "$LOWER" ] || die "LOWER=LOWPER is not a directory"
124     [ -d "$LIVE" ] || die "LOWER=LOWPER is not a directory"
125     [ -x "${PREMOUNT%% *}" ] || die "PREMOUNT=${PREMOUNT%% *} not executable"
126     [ -f "${PREMOUNT%% *}" ] || die "PREMOUNT='$PREMOUNT' is not a command"
127     [ -x "${POSTMOUNT%% *}" ] || \
128         die "POSTMOUNT=${POSTMOUNT%% *} not executable"
129     [ -f "${POSTMOUNT%% *}" ] || \
130         die "POSTMOUNT='$POSTMOUNT' is not a command"
131
132     # UPPER is the same as LOWER then skip the overlay mount
133     if [ "$UPPER" != "$LOWER" ] ; then
134         # sanity check
135         [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
136
137         env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
138
139         OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
140         if ! mount -t overlay "$OLY" $1 $2 ; then
141             umount -R "$UPPER/dev"
142             umount "$UPPER/run"
143             die "Cannot set up the overlay mount $2"
144         fi
145     elif [ "$LIVE" != "$UPPER" ] ; then
146         # With UPPER = LOWER we rather make a bind mount to LIVE
147         env CONFIG="$CONFIG" $PREMOUNT "$UPPER"
148         mount --bind $UPPER $LOWER
149     fi
150
151     env CONFIG="$CONFIG" $POSTMOUNT "LIVE" "$UPPER" 
152 }
153
154 # Find the "unshare" process for $1 and echo the its pid and the pids
155 # of its child processes.
156 is_live() {
157     local NAME=$1
158     local USPID="$(pgrep -f "unshare.* $NAME ")"
159     [ -z "$USPID" ] && return 1
160     echo "$USPID $(ps -hopid --ppid=$USPID)"
161 }
162
163 # Find all overlay-boot processes and list their config files
164 list_running() {
165     pgrep -a overlay-boot | awk '{print $4}'
166 }