fixup of networking
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Sat, 26 Feb 2022 00:24:38 +0000 (11:24 +1100)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Sat, 26 Feb 2022 00:24:38 +0000 (11:24 +1100)
control

diff --git a/control b/control
index 28a4d2c0d99d9fb2653b4c49d8e1d78d4100595a..b63bc74011edb82a44f6d9a8905b741664ef869c 100755 (executable)
--- a/control
+++ b/control
@@ -6,6 +6,8 @@
 # $2 = the sub-host to start or stop
 # $3... = the optional chroot-ed command (/startup by default)
 
+set -x
+
 SCRIPT=$0
 CMD=$1
 NAME=$2
@@ -18,53 +20,87 @@ usage() {
 
 [ -z "$NAME" ] && usage
 
-: ${ATTIC=/opt/srv}
-: ${BRIDGE=srv_br}
-: ${TARGET=/srv/$NAME}
-: ${LOWER=$ATTIC/daedalus.fs}
-: ${IMAGE=$ATTIC/$NAME/$NAME.img}
-: ${UPPER=$ATTIC/$NAME/root}
-: ${WORK=$ATTIC/$NAME/work}
-: ${MOUNT=$ATTIC/$NAME/mnt}
+: ${SUBHOST=/opt/subhost}
+: ${OSROOT=}
+: ${TOP=$SUBHOST/$NAME}
+: ${TARGET=$TOP/live}
+: ${IMAGE=$TOP/$NAME.img}
+: ${UPPER=$TOP/root}
+: ${WORK=$TOP/work}
+: ${MOUNT=$TOP/mnt}
 : ${NSNAME=$NAME}
-: ${NETH=1}
+: ${BRIDGES=homenet}
+: ${CONFIG=$TOP/config}
+
+[ -e "$CONFIG" ] && . "$CONFIG"
+
+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
+    [ -e $CONFIG ] || cat <<EOF > "$CONFIG"
+# Subhost $NAME is an autogenerated overlay subhost with shared filesystem
+OSROOT="$OSROOT"
+BRIDGES="$BRIDGES"
+EOF
 }
 
-setup_network() {
-    if [ -n "$BRIDGE" ] ; then
-       brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
-    fi
+# generate a mac for given $1 (interface) using the last 5 characters
+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}"
+}
 
+# setup the subhost network namespace and link up the host side
+setup_network() {
     E=0
     ip netns add $NSNAME
-    for I in $(eval echo "{1..$NETH}") ; do
+    for BRIDGE in ${BRIDGES[@]} ; do
+       brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
        IF=$NAME$E
-       ip link add $IF type veth peer name eth$E netns $NSNAME
+       MAC=$(macaddr $IF)
+       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
        E=$((E+1))
     done
 }
 
+# check if $1 is mounted
+is_mounted() {
+    grep -qE "^[^ ]+ $1 " /proc/mounts
+}
+
+# Setup or re-setup the subhost filesystem
 setup_rootfs() {
-    if [ -e "$IMAGE" ] ; then
-       mount $IMAGE $MOUNT || exit 1
-       LOWER=$MOUNT/root
-       WORK=$MOUNT/work
+    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
+           is_mounted $MOUNT || mount $IMAGE $MOUNT || exit 1
+           UPPER=$MOUNT/root
+           WORK=$MOUNT/work
+       fi
+       if [ -z "$OSROOT" ] ; then
+           # No overlay
+           mount --rbind $UPPER $TARGET
+       else
+           # overlay of $UPPER (+$WORK) over $OSROOT onto $TARGET
+           mount -t overlay $NAME \
+                 -olowerdir=$OSROOT,upperdir=$UPPER,workdir=$WORK \
+                 $TARGET
+       fi
     fi
-    mount -t overlay $NAME -olowerdir=$LOWER,upperdir=$UPPER,workdir=$WORK \
-         $TARGET
 }
 
 case "$CMD" in
     start)
-       cd "$ATTIC" || exit 1
        [ -e "/run/netns/$NSNAME" ] || setup_network
        [ -d "$MOUNT" ] || create_subhost
-       grep -q "^$NAME $TARGET overlay" /proc/mounts || setup_rootfs
+       setup_rootfs
        START=/bin/bash
        [ -x $TARGET/startup ] && START=/startup
        exec ip netns exec $NSNAME unshare \