3 # Boot up subhost as if a vitual machine.
5 # There is already an existing network namespace of the right name,
6 # and there are one or more disks at /opt/subhost/$NAME/disk$i
12 : ${SUBHOST=/opt/subhost/$NAME}
13 : ${TARGET=$SUBHOST/live}
14 : ${INITRD=$SUBHOST/initrd.gz}
15 : ${CONFIG=$SUBHOST/config}
17 [ -r "$CONFIG" ] && . $CONFIG
19 # Generate a mac address for given arguments pass through 40-bit b2sum
20 # and with 02: prefix.
22 local V="$(b2sum -l 40 <<< "$*" )"
23 V="$( sed 's/\(..\)/\1:/g' <<< "${V}aaaaaaaaaa")"
27 # Setup the subhost network namespace and link up the host side
30 [ -e /var/run/netns/$NSNAME ] || ip netns add $NSNAME
31 for BRIDGE in ${BRIDGES[@]} ; do
36 [ -z "$MAC" ] && MAC="$(macaddr "$(hostname)" "$NAME" "$IF")"
37 brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
38 ip link add $IF type veth peer name eth$E address $MAC netns $NSNAME
40 [ -n "$BRIDGE" ] && brctl addif $BRIDGE $IF
45 # loopdev path -- Make a named device node for a loop device
47 local MKNOD="mknod $(basename $2) b $(stat -c "%t %T" $1)"
51 mkdir -p $(dirname $2)
53 ( cd $(dirname $2) && $MKNOD )
56 # diskid image partition# -- set up parition device node
58 local LOOP="$(losetup -j "$3" | sed 's/:.*//')"
59 [ -z "$LOOP" ] && LOOP="$(losetup -f --show "$3")"
60 grep -q $TARGET /proc/mounts || \
61 mount ${LOOP}p$2 $TARGET
62 copyloop $LOOP $TARGET/dev/$1
63 copyloop ${LOOP}p$2 $TARGET/dev/$1$2
66 # Setup device nodes for VM loopbacks according to $DISKS
68 for DISK in ${DISKS[@]} ; do
69 D=( $(echo $DISK | tr : ' ') )
75 echo "Unknown disk type ${D[0]}" >&2
82 # Change $TARGET/dev/console
85 CONSOLE=$TARGET/dev/console
92 setup_console || exit 1
94 # Install the current subhost-init startup script
95 cp -p $(realpath $(dirname $0))/subhost-init startup $TARGET/
97 ######################################################################
99 ip netns exec $NSNAME unshare --fork --pid --mount-proc --kill-child \
100 --uts --ipc --mount --cgroup chroot $TARGET /startup
101 ######################################################################