Revised pretend-boot description
[rrq/subhost.git] / control
diff --git a/control b/control
index b63bc74011edb82a44f6d9a8905b741664ef869c..304c93bd1400b6a310f17c8e90288d5594b3cc83 100755 (executable)
--- a/control
+++ b/control
@@ -29,7 +29,7 @@ usage() {
 : ${WORK=$TOP/work}
 : ${MOUNT=$TOP/mnt}
 : ${NSNAME=$NAME}
-: ${BRIDGES=homenet}
+: ${BRIDGES=lan_br}
 : ${CONFIG=$TOP/config}
 
 [ -e "$CONFIG" ] && . "$CONFIG"
@@ -39,7 +39,7 @@ cd "$SUBHOST" || exit 1
 # Create a simple overlay subhost without its own image file
 create_subhost() {
     mkdir -p $TARGET $MOUNT $UPPER $WORK
-    [ -d "$OSROOT" ] || OSROOT=$SUBHOST/daedalus/root
+    [ -d "$OSROOT" ] || OSROOT=$SUBHOST/chimaera/base
     [ -e $CONFIG ] || cat <<EOF > "$CONFIG"
 # Subhost $NAME is an autogenerated overlay subhost with shared filesystem
 OSROOT="$OSROOT"
@@ -47,10 +47,12 @@ BRIDGES="$BRIDGES"
 EOF
 }
 
-# generate a mac for given $1 (interface) using the last 5 characters
+# Generate a mac address for given arguments pass through 40-bit b2sum
+# and with 02: prefix.
 macaddr() {
-    local M="$(xxd -p <<< "${1:$(( ${#1} - 5)):5}")66666666"
-    echo "0a:${M:0:2}:${M:2:2}:${M:4:2}:${M:6:2}:${M:8:2}"
+    local V="$(b2sum -l 40 <<< "$*" )"
+    V="$( sed 's/\(..\)/\1:/g' <<< "${V}aaaaaaaaaa")"
+    echo "02:${V:0:14}"
 }
 
 # setup the subhost network namespace and link up the host side
@@ -58,9 +60,12 @@ setup_network() {
     E=0
     ip netns add $NSNAME
     for BRIDGE in ${BRIDGES[@]} ; do
-       brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
        IF=$NAME$E
-       MAC=$(macaddr $IF)
+       B=( ${BRIDGE/:/ } )
+       BRIDGE="${B[0]}"
+       MAC="${B[1]}"
+       [ -z "$MAC" ] && MAC="$(macaddr "$(hostname)" "$NAME" "$IF")"
+       brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
        ip link add $IF type veth peer name eth$E address $MAC netns $NSNAME 
        ip link set $IF up
        [ -n "$BRIDGE" ] && brctl addif $BRIDGE $IF
@@ -78,8 +83,9 @@ setup_rootfs() {
     if is_mounted $TARGET ; then
        mount -oremount $TARGET
     else
-       if [ -f "$IMAGE" ] ; then
-           # The subhost has an image file with /root and /work in it
+       if [ -e "$IMAGE" ] ; then
+           # $IMAGE is either an image file or a link to a partition,
+           # with /root and /work in it.
            is_mounted $MOUNT || mount $IMAGE $MOUNT || exit 1
            UPPER=$MOUNT/root
            WORK=$MOUNT/work
@@ -96,6 +102,26 @@ setup_rootfs() {
     fi
 }
 
+# Set up cgroup CPU accounting (cpuacct)
+accounting() {
+        # 1-- once, system wide
+        grep -q "/sys/fs/cgroup cgroup" /proc/mounts || \
+            mount -t cgroup -ocpuacct none /sys/fs/cgroup
+        if [ -d /sys/fs/cgroup/$NAME ] ; then
+            : # reset?
+        else
+            mkdir /sys/fs/cgroup/$NAME
+           # the following fixes things that sometimes are broken?
+           for f in cpuset.mems cpuset.cpus ; do
+               [ -z "$(cat /sys/fs/cgroups/$NAME/$F)" ] && \
+                   echo 0 > /sys/fs/cgroups/$NAME/$F
+           done
+
+        fi
+       # Register this task for subhost accounting
+        echo $$ > /sys/fs/cgroup/$NAME/tasks
+}
+
 case "$CMD" in
     start)
        [ -e "/run/netns/$NSNAME" ] || setup_network
@@ -103,15 +129,17 @@ case "$CMD" in
        setup_rootfs
        START=/bin/bash
        [ -x $TARGET/startup ] && START=/startup
+       accounting
        exec ip netns exec $NSNAME unshare \
             --fork --pid --mount-proc --kill-child \
             --uts --ipc --mount --cgroup \
-            "--root=$TARGET" $START
+            chroot $TARGET $START
        ;;
     stop)
        umount $TARGET
        [ -e $IMAGE ] && umount $MOUNT
        ip netns del $NSNAME
+       rmdir /sys/fs/cgroup/$NAME
        ;;
     *)
        usage