3 # Control program for bespoke arbitrary services through unshared sub-hosts
5 # $1 = the command: start or stop
6 # $2 = the sub-host to start or stop
7 # $3... = the optional chroot-ed command (/startup by default)
15 echo "$SCRIPT (start|stop) <subhost>" >&2
19 [ -z "$NAME" ] && usage
23 : ${TARGET=/srv/$NAME}
24 : ${LOWER=$ATTIC/daedalus.fs}
25 : ${IMAGE=$ATTIC/$NAME/$NAME.img}
26 : ${UPPER=$ATTIC/$NAME/root}
27 : ${WORK=$ATTIC/$NAME/work}
28 : ${MOUNT=$ATTIC/$NAME/mnt}
33 mkdir -p $TARGET $MOUNT $UPPER $WORK
37 if [ -n "$BRIDGE" ] ; then
38 brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
43 for I in $(eval echo "{1..$NETH}") ; do
45 ip link add $IF type veth peer name eth$E netns $NSNAME
47 [ -n "$BRIDGE" ] && brctl addif $BRIDGE $IF
53 if [ -e "$IMAGE" ] ; then
54 mount $IMAGE $MOUNT || exit 1
58 mount -t overlay $NAME -olowerdir=$LOWER,upperdir=$UPPER,workdir=$WORK \
65 [ -e "/run/netns/$NSNAME" ] || setup_network
66 [ -d "$MOUNT" ] || create_subhost
67 grep -q "^$NAME $TARGET overlay" /proc/mounts || setup_rootfs
69 [ -x $TARGET/startup ] && START=/startup
70 exec ip netns exec $NSNAME unshare \
71 --fork --pid --mount-proc --kill-child \
72 --uts --ipc --mount --cgroup \
73 "--root=$TARGET" $START
77 [ -e $IMAGE ] && umount $MOUNT