initial
[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
9 . $(dirname $(realpath $0))/functions
10
11 CONFIG="$1"
12 [ -r "$CONFIG" ] || die "Missing configuration $CONFIG"
13
14 config NAME $(basename $1 .${1##*.})
15 config LOG /tmp/oly-$NAME.log
16
17 if [ -z "$UNSHARED" ] ; then
18     # Pre-unsharing:
19     #
20     # Create the network namespace for the subhost, then trigger
21     # detached re-run with unshared mount namespace
22     [ -r /run/netns/$NAME ] || ip netns add $NAME
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 config LIVE "$BASE/live"
30 config UPPER "$BASE/root"
31 config WORK "$BASE/work"
32 config LOWER "/"
33 config CABLES ""
34 config START "networking ssh"
35 config SUBSHELL /bin/sh
36 config STOP ""
37
38 # Setup virtual cabling and subhost's /etc/network/interfaces
39 setup_veth_cables $NAME $CABLES
40
41 # Set up the mount for this subhost, including a new tmpfs on its /run
42 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
43 setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
44
45 exithandler() {
46     ip netns del $NAME
47     umount -R "$LIVE"
48 }
49 trap "exithandler" 0
50
51 CMD="unshare -fp --mount-proc ip netns exec $NAME chroot $LIVE /bin/sh"
52 echo "$CMD"
53 cat <<EOF | $CMD
54 set -x
55 mount --bind $UPPER/run /run
56 for srv in $START ; do service \$srv start ; done
57 exec /.reaper $NAME
58 EOF
59 echo "EXITED $CMD"
60 #echo "$STOP" | ip netns exec $NAME chroot $LIVE $SUBSHELL