use ifup for bridge-less cable; allow UPPER scripting for image file root; allow...
[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 OVERLAYDIR="$(realpath $(dirname $0))"
8
9 [ $(id -u) = 0 ] || exec sudo $0 $@
10 . $(dirname $(realpath $0))/functions $*
11
12 CONFIG="$1"
13 [ -r "$CONFIG" ] || die "Missing configuration $CONFIG"
14
15 config NAME $(basename $1 .${1##*.})
16 config LOG /tmp/oly-$NAME.log
17
18 if [ -z "$UNSHARED" ] ; then
19     # Pre-unsharing:
20     #
21     # Create the network namespace for the subhost, then trigger
22     # detached re-run with unshared mount namespace
23     [ -r /run/netns/$NAME ] || {
24         ip netns add $NAME
25         ip netns exec $NAME ip link set lo up
26     }
27     exec env UNSHARED=yes unshare -m $0 $@ > $LOG 2>&1 &
28     echo "Logging to $LOG" >&2
29     exit 0
30 fi
31
32 config BASE
33 BASE="$(cd $(dirname $CONFIG); realpath $BASE)"
34
35 [ -z "$BASE" ] && die "BASE is unset; bogus $CONFIG ?"
36 [ -d "$BASE" ] || die "$BASE is not a directory; bogus $CONFIG ?"
37 cd "$BASE" || die "$BASE is inaccessible"
38
39 config LIVE "$BASE/live"
40 config UPPER "$BASE/root"
41 config WORK "$BASE/work"
42 config LOWER "/"
43 config CABLES ""
44 config START "networking ssh"
45 config SUBSHELL /bin/sh
46
47 # Setup virtual cabling and subhost's /etc/network/interfaces
48 setup_veth_cables $NAME $CABLES
49
50 # Set up the mount for this subhost, including a new tmpfs on its /run
51 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
52 setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
53
54 exithandler() {
55     ip netns del $NAME
56     [ "$LOWER" != "$LIVE" ] && umount -R "$LIVE"
57     [ -f "${UPPER%% *}" ] && [ -x "${UPPER%% *}" ] && \
58         env ACTION=teardown $UPPER
59 }
60 trap "exithandler" 0
61
62 CMD="unshare -fp --mount-proc ip netns exec $NAME chroot $LIVE /bin/sh"
63 echo "$CMD"
64
65 config RAM_SIZE 50M
66
67 cat <<EOF | $CMD
68 set -x
69 mount -t proc proc /proc
70 mount -t devpts devpts /dev/pts
71 mount -t sysfs sysfs /sys
72 if [ "$RAM_SIZE" != "none" ] && ! grep -q '/run tmpfs' /proc/mounts ; then
73     mount -t tmpfs -osize=$RAM_SIZE,mode=755 tmpfs /run
74 fi
75 for srv in $START ; do service \$srv start ; done
76 dummy_service() {
77     [ \$# -gt 3 ] && return 0
78     echo "Starting dummy service" >&2
79     set +x
80     [ -p /run/dummy_service ] || mkfifo /run/dummy_service
81     ( printf dummy_service > /proc/self/comm ; read X < /run/dummy_service ) &
82     set -x
83 }
84 dummy_service /proc/*/comm
85 exec /.reaper $NAME
86 EOF
87 echo "EXITED $CMD"