remove unused STOP variable
[rrq/overlay-boot.git] / overlay-boot
1 #!/bin/sh
2 #
3 # This boot method runs a service subhost with a root filesystem that
4 # is an overlay of the subhost's root and an OS root. The service
5 # subhost is defined by a configuration file named on teh command line
6
7 [ $(id -u) = 0 ] || exec sudo $0 $@
8 . $(dirname $(realpath $0))/functions $*
9 CONFIG="$1"
10 [ -r "$CONFIG" ] || die "Missing configuration $CONFIG"
11 config NAME $(basename $1 .${1##*.})
12 config LOG /tmp/oly-$NAME.log
13
14 if [ -z "$UNSHARED" ] ; then
15     # Pre-unsharing:
16     #
17     # Create the network namespace for the subhost, then trigger
18     # detached re-run with unshared mount namespace
19     [ -r /run/netns/$NAME ] || {
20         ip netns add $NAME
21         ip netns exec $NAME ip link set lo up
22     }
23     exec env UNSHARED=yes unshare -m $0 $@ > $LOG 2>&1 &
24     echo "Logging to $LOG" >&2
25     exit 0
26 fi
27
28 config BASE
29 BASE="$(cd $(dirname $CONFIG); realpath $BASE)"
30
31 config LIVE "$BASE/live"
32 config UPPER "$BASE/root"
33 config WORK "$BASE/work"
34 config LOWER "/"
35 config CABLES ""
36 config START "networking ssh"
37 config SUBSHELL /bin/sh
38
39 # Setup virtual cabling and subhost's /etc/network/interfaces
40 setup_veth_cables $NAME $CABLES
41
42 # Set up the mount for this subhost, including a new tmpfs on its /run
43 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
44 setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
45
46 exithandler() {
47     ip netns del $NAME
48     umount -R "$LIVE"
49 }
50 trap "exithandler" 0
51
52 CMD="unshare -fp --mount-proc ip netns exec $NAME chroot $LIVE /bin/sh"
53 echo "$CMD"
54
55 cat <<EOF | $CMD
56 set -x
57 mount -t proc proc /proc
58 for srv in $START ; do service \$srv start ; done
59 exec /.reaper $NAME
60 EOF
61 echo "EXITED $CMD"