bring up lo upon adding netns
[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 config STOP ""
39
40 # Setup virtual cabling and subhost's /etc/network/interfaces
41 setup_veth_cables $NAME $CABLES
42
43 # Set up the mount for this subhost, including a new tmpfs on its /run
44 echo setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
45 setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
46
47 exithandler() {
48     ip netns del $NAME
49     umount -R "$LIVE"
50 }
51 trap "exithandler" 0
52
53 CMD="unshare -fp --mount-proc ip netns exec $NAME chroot $LIVE /bin/sh"
54 echo "$CMD"
55
56 cat <<EOF | $CMD
57 set -x
58 mount -t proc proc /proc
59 for srv in $START ; do service \$srv start ; done
60 exec /.reaper $NAME
61 EOF
62 echo "EXITED $CMD"