The creation of the dummy devices is ugly and has races ('dummy0' is
used by every 'vserver ... start' instance which conflicts with the
parallel vserver startup). 'dummy<ctx>' would be ideally but is not
supported by the kernel.

I've attached what I was thinking, roughly. Yes, this is part of a much larger script... I'll share it with everyone when it's more complete.
One problem with this code is that there's no real way of dumping the vserver interfaces when you're done. I can't think of others off the top of my head, but they're probably there.


vstohex () {
        [ -n "$1" ] || return 1
local oct1=$((${1} / 256))
local oct2=$((${1} % 256))
local -a out=()
        for seg in $oct1 $oct2 ; do
                hex1=$(($seg / 16))
                [ "$hex1" = "15" ] && hex1=f
                [ "$hex1" = "14" ] && hex1=e
                [ "$hex1" = "13" ] && hex1=d
                [ "$hex1" = "12" ] && hex1=c
                [ "$hex1" = "11" ] && hex1=b
                [ "$hex1" = "10" ] && hex1=a
                hex2=$(($seg % 16))
                [ "$hex2" = "15" ] && hex2=f
                [ "$hex2" = "14" ] && hex2=e
                [ "$hex2" = "13" ] && hex2=d
                [ "$hex2" = "12" ] && hex2=c
                [ "$hex2" = "11" ] && hex2=b
                [ "$hex2" = "10" ] && hex2=a
                [EMAIL PROTECTED]
        done
echo "${out[0]}:${out[1]}"
        }

if [ - "${BUILD_VSIF}" ] && ! ip link ls ${VSERVER_NAME} 2> /dev/null ; then
olddummylist=($(ip link ls | gawk '$2 ~ /^dummy/ {print $2}' | sed 's/:$//g' | sort))
insmod -o new dummy
newdummylist=($(ip link ls | gawk '$2 ~ /^dummy/ {print $2}' | sed 's/:$//g' | sort))
if [ "[EMAIL PROTECTED]" = "[EMAIL PROTECTED]" ] ; then
${LOGERROR} "Sorry, no dummy support in kernel!"
else
for nd in [EMAIL PROTECTED] ; do
for od in [EMAIL PROTECTED] ; do
[ "$nd" = "$od" ] && continue 2
done
dummylock=/var/lock/vserver/.lock.${nd}
[ -f "$dummylock" ] && continue
echo $$ >> $dummylock
hexend=$(vstohex ${S_CONTEXT})
mac="ff:ff:ff:ff:${hexend}"
ip link set dev ${nd} address ${mac}
nameif ${VSERVER_NAME} ${mac} || ${LOGERROR} "Problem setting up the dummy interface"
rm -f ${dummylock}
break
done
fi
fi
if [ "$BUILD_VSIF" ] ; then
if ! ip link ls ${VSERVER_NAME} > /dev/null 2>&1 ; then
${LOGERROR} "Dummy interface ${VSERVER_NAME} doesn't exist!"
else
ip addr flush ${VSERVER_NAME}
for ipaddr in ${IPROOT} ; do
ip addr add dev ${VSERVER_NAME} $ipaddr
done
fi
fi

vstohex () {
        [ -n "$1" ] || return 1
        local oct1=$((${1} / 256))
        local oct2=$((${1} % 256))
        local -a out=()
        for seg in $oct1 $oct2 ; do
                hex1=$(($seg / 16))
                [ "$hex1" = "15" ] && hex1=f
                [ "$hex1" = "14" ] && hex1=e
                [ "$hex1" = "13" ] && hex1=d
                [ "$hex1" = "12" ] && hex1=c
                [ "$hex1" = "11" ] && hex1=b
                [ "$hex1" = "10" ] && hex1=a
                hex2=$(($seg % 16))
                [ "$hex2" = "15" ] && hex2=f
                [ "$hex2" = "14" ] && hex2=e
                [ "$hex2" = "13" ] && hex2=d
                [ "$hex2" = "12" ] && hex2=c
                [ "$hex2" = "11" ] && hex2=b
                [ "$hex2" = "10" ] && hex2=a
                [EMAIL PROTECTED]
        done
        echo "${out[0]}:${out[1]}"
        }

                if [ "${BUILD_VSIF}" != "" ] && ! ip addr ls ${VSERVER_NAME} 2> 
/dev/null ; then
                        dummylist=($(ip link ls | gawk '$2 ~ /^dummy/ {print $2}' | 
sed 's/:$//g' | sort))
                        insmod -o new dummy
                        newdummylist=($(ip link ls | gawk '$2 ~ /^dummy/ {print $2}' | 
sed 's/:$//g' | sort))
                        if [ "[EMAIL PROTECTED]" = "[EMAIL PROTECTED]" ] ; then
                                ${LOGERROR} "Sorry, no dummy support in kernel!"
                        else
                                for nd in [EMAIL PROTECTED] ; do
                                        for od in ${olddummylist} ; do
                                                [ "$nd" = "$od" ] && continue 2
                                        done
                                        dummylock=/var/lock/vserver/.lock.${nd}
                                        [ -f "$dummylock" ] && continue
                                        echo $$ >> $dummylock
                                        hexend=$(vstohex ${S_CONTEXT})
                                        mac="ff:ff:ff:ff:${hexend}"
                                        ip link set dev ${nd} address ${mac}
                                        nameif ${VSERVER_NAME} ${mac} || continue
                                        rm -f ${dummylock}
                                        break
                                done
                        fi
                fi
                if [ "$BUILD_VSIF" ] ; then
                        if ! ip link ls ${VSERVER_NAME} > /dev/null 2>&1 ; then
                                ${LOGERROR} "Dummy interface ${VSERVER_NAME} doesn't 
exist!"
                        else
                                ip addr flush ${VSERVER_NAME}
                                for ipaddr in ${IPROOT} ; do
                                        ip addr add dev ${VSERVER_NAME} $ipaddr
                                done
                        fi
                fi

Reply via email to