various correction
[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 ] || ip netns add $NAME
20     exec env UNSHARED=yes unshare -m $0 $@ > $LOG 2>&1 &
21     echo "Logging to $LOG" >&2
22     exit 0
23 fi
24
25 config BASE
26 BASE="$(cd $(dirname $CONFIG); realpath $BASE)"
27
28 config LIVE "$BASE/live"
29 config UPPER "$BASE/root"
30 config WORK "$BASE/work"
31 config LOWER "/"
32 config CABLES ""
33 config START "networking ssh"
34 config SUBSHELL /bin/sh
35 config STOP ""
36
37 # Setup virtual cabling and subhost's /etc/network/interfaces
38 setup_veth_cables $NAME $CABLES
39
40 # Set up the mount for this subhost, including a new tmpfs on its /run
41 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
42 setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
43
44 exithandler() {
45     ip netns del $NAME
46     umount -R "$LIVE"
47 }
48 trap "exithandler" 0
49
50 CMD="unshare -fp --mount-proc ip netns exec $NAME chroot $LIVE /bin/sh"
51 echo "$CMD"
52
53 cat <<EOF | $CMD
54 set -x
55 mount -t proc proc /proc
56 for srv in $START ; do service \$srv start ; done
57 exec /.reaper $NAME
58 EOF
59 echo "EXITED $CMD"