Minor correction, with explicit chroot (as required in beowulf)
[rrq/subhost.git] / control
1 #!/bin/bash
2 #
3 # Control program for bespoke arbitrary services through unshared sub-hosts
4 #
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)
8
9 set -x
10
11 SCRIPT=$0
12 CMD=$1
13 NAME=$2
14 shift 2
15
16 usage() {
17     echo "$SCRIPT (start|stop) <subhost>" >&2
18     exit 1
19 }
20
21 [ -z "$NAME" ] && usage
22
23 : ${SUBHOST=/opt/subhost}
24 : ${OSROOT=}
25 : ${TOP=$SUBHOST/$NAME}
26 : ${TARGET=$TOP/live}
27 : ${IMAGE=$TOP/$NAME.img}
28 : ${UPPER=$TOP/root}
29 : ${WORK=$TOP/work}
30 : ${MOUNT=$TOP/mnt}
31 : ${NSNAME=$NAME}
32 : ${BRIDGES=lan_br}
33 : ${CONFIG=$TOP/config}
34
35 [ -e "$CONFIG" ] && . "$CONFIG"
36
37 cd "$SUBHOST" || exit 1
38
39 # Create a simple overlay subhost without its own image file
40 create_subhost() {
41     mkdir -p $TARGET $MOUNT $UPPER $WORK
42     [ -d "$OSROOT" ] || OSROOT=$SUBHOST/chimaera/base
43     [ -e $CONFIG ] || cat <<EOF > "$CONFIG"
44 # Subhost $NAME is an autogenerated overlay subhost with shared filesystem
45 OSROOT="$OSROOT"
46 BRIDGES="$BRIDGES"
47 EOF
48 }
49
50 # map a string into a hexadecimal number with 10-digits by folding the given string into 9-digit ascii-code numbers and adding them
51 hexfoldsum() {
52     local V
53     V="$(b2sum -l 40 <<< "$1" | sed 's/\(..\)/\1:/g')"
54     echo "02:${V:0:14}"
55 }
56
57 # Generate a mac address for given interface by passing hostname,
58 # subhost name and interface through 40-bit b2sum and with 02: prefix.
59 macaddr() {
60     local V="$(b2sum -l 40 <<< "$(hostname)$NAME$1" | sed 's/\(..\)/\1:/g')"
61     echo "02:${V:0:14}"
62 }
63
64 # setup the subhost network namespace and link up the host side
65 setup_network() {
66     E=0
67     ip netns add $NSNAME
68     for BRIDGE in ${BRIDGES[@]} ; do
69         brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
70         IF=$NAME$E
71         MAC=$(macaddr $IF)
72         ip link add $IF type veth peer name eth$E address $MAC netns $NSNAME 
73         ip link set $IF up
74         [ -n "$BRIDGE" ] && brctl addif $BRIDGE $IF
75         E=$((E+1))
76     done
77 }
78
79 # check if $1 is mounted
80 is_mounted() {
81     grep -qE "^[^ ]+ $1 " /proc/mounts
82 }
83
84 # Setup or re-setup the subhost filesystem
85 setup_rootfs() {
86     if is_mounted $TARGET ; then
87         mount -oremount $TARGET
88     else
89         if [ -e "$IMAGE" ] ; then
90             # $IMAGE is either an image file or a link to a partition,
91             # with /root and /work in it.
92             is_mounted $MOUNT || mount $IMAGE $MOUNT || exit 1
93             UPPER=$MOUNT/root
94             WORK=$MOUNT/work
95         fi
96         if [ -z "$OSROOT" ] ; then
97             # No overlay
98             mount --rbind $UPPER $TARGET
99         else
100             # overlay of $UPPER (+$WORK) over $OSROOT onto $TARGET
101             mount -t overlay $NAME \
102                   -olowerdir=$OSROOT,upperdir=$UPPER,workdir=$WORK \
103                   $TARGET
104         fi
105     fi
106 }
107
108 case "$CMD" in
109     start)
110         [ -e "/run/netns/$NSNAME" ] || setup_network
111         [ -d "$MOUNT" ] || create_subhost
112         setup_rootfs
113         START=/bin/bash
114         [ -x $TARGET/startup ] && START=/startup
115         exec ip netns exec $NSNAME unshare \
116              --fork --pid --mount-proc --kill-child \
117              --uts --ipc --mount --cgroup \
118              chroot $TARGET $START
119         ;;
120     stop)
121         umount $TARGET
122         [ -e $IMAGE ] && umount $MOUNT
123         ip netns del $NSNAME
124         ;;
125     *)
126         usage
127         ;;
128 esac