3 # Boot up subhost as if a vitual machine.
4 # Allow CONFIG given as environment
11 : ${SUBHOST=/opt/subhost/$NAME}
12 : ${TARGET=$SUBHOST/live}
13 : ${CONFIG=$SUBHOST/config}
15 # sets DISKS=( ... ) and BRIDGES=( ... )
16 [ -r "$CONFIG" ] && . $CONFIG
18 if [ -z "$DISKS" ] ; then
19 echo "Missing CONFIG" >&2
23 # Generate a mac address for given arguments pass through 40-bit b2sum
24 # and with 02: prefix.
26 local V="$(b2sum -l 40 <<< "$*" )"
27 V="$( sed 's/\(..\)/\1:/g' <<< "${V}aaaaaaaaaa")"
31 # Setup the subhost network namespace and link up the host side
34 [ -e /var/run/netns/$NSNAME ] || ip netns add $NSNAME
35 for BRIDGE in ${BRIDGES[@]} ; do
40 [ -z "$MAC" ] && MAC="$(macaddr "$(hostname)" "$NAME" "$IF")"
41 brctl show $BRIDGE >& /dev/null || brctl addbr $BRIDGE
42 ip link add $IF type veth peer name eth$E address $MAC netns $NSNAME
44 [ -n "$BRIDGE" ] && brctl addif $BRIDGE $IF
49 # loopdev path -- Make a named device node for a loop device
51 local MKNOD="mknod $(basename $2) b $(stat -c "%t %T" $1)"
55 mkdir -p $(dirname $2)
57 ( cd $(dirname $2) && $MKNOD )
60 # diskid image partition# -- set up parition device node
62 local LOOP="$(losetup -j "$3" | sed 's/:.*//')"
63 [ -z "$LOOP" ] && LOOP="$(losetup -f --show "$3")"
64 grep -q $TARGET /proc/mounts || \
65 mount ${LOOP}p$2 $TARGET
66 copyloop $LOOP $TARGET/dev/$1
67 copyloop ${LOOP}p$2 $TARGET/dev/$1$2
70 # Setup device nodes for VM loopbacks according to $DISKS
72 for DISK in ${DISKS[@]} ; do
73 D=( $(echo $DISK | tr : ' ') )
79 echo "Unknown disk type ${D[0]}" >&2
86 # Change $TARGET/dev/console
89 CONSOLE=$TARGET/dev/console
101 ######################################################################
102 # Run the subhost init
103 ip netns exec $NSNAME unshare --fork --pid --mount-proc --kill-child \
104 --uts --ipc --mount --cgroup chroot $TARGET /sbin/init
105 ######################################################################