EOF
}
-# map a string into a hexadecimal number with 10-digits by folding the given string into 9-digit ascii-code numbers and adding them
-hexfoldsum() {
- local V
- V="$(b2sum -l 40 <<< "$1" | sed 's/\(..\)/\1:/g')"
- echo "02:${V:0:14}"
-}
-
-# Generate a mac address for given interface by passing hostname,
-# subhost name and interface through 40-bit b2sum and with 02: prefix.
+# Generate a mac address for given arguments pass through 40-bit b2sum
+# and with 02: prefix.
macaddr() {
- local V="$(b2sum -l 40 <<< "$(hostname)$NAME$1" | sed 's/\(..\)/\1:/g')"
+ local V="$(b2sum -l 40 <<< "$*" )"
+ V="$( sed 's/\(..\)/\1:/g' <<< "${V}aaaaaaaaaa")"
echo "02:${V:0:14}"
}
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
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
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 \
umount $TARGET
[ -e $IMAGE ] && umount $MOUNT
ip netns del $NSNAME
+ rmdir /sys/fs/cgroup/$NAME
;;
*)
usage