d635da4347739db34020cd6856382d5c6f7827f9
[rrq/overlay-boot.git] / overlay-boot
1 #!/bin/sh
2 #
3 # This boot scripts runs a service subhost as defined by the
4 # configuration file named on the command line.
5 # See "man overlay-boot" for details.
6
7 PROGRAMDIR="$(dirname $(realpath $0))"
8 . $PROGRAMDIR/functions
9
10 subhost_name $1
11
12 if [ -z "$UNSHARED" ] ; then
13     if [ ! -r /run/netns/$NAME ] ; then
14         ip netns add $NAME
15         ip netns exec $NAME ip link set lo up || exit 1
16     fi
17     exec env UNSHARED=yes unshare -m $0 $@ > $LOG 2>&1 &
18     echo "Logging to $LOG" >&2
19     exit 0
20 fi
21
22 subhost_config
23 setup_veth_cables $NAME $CABLES
24 setup_overlay "$NAME" "$LIVE" "$LOWER" "$UPPER" "$WORK"
25
26 exithandler() {
27     ip netns del $NAME
28     [ "$UPPER" != "$LIVE" ] && umount -R "$LIVE"
29 }
30 trap "exithandler" 0
31
32 CMD="unshare -fp --mount-proc -i -u ip netns exec $NAME chroot $LIVE /bin/sh"
33 echo "$CMD"
34 env CONFIG="$CONFIG" $INIT | $CMD
35 echo "EXITED $CMD"