[Vserver] Dummy device creation without race states

2004-03-30 Thread Liam Helmer





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' 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
  

Re: [Vserver] Dummy device creation without race states

2004-03-30 Thread Herbert Poetzl
On Wed, Mar 31, 2004 at 01:41:14AM +, Liam Helmer wrote:
> 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' 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

impressive, but 
 
 printf "%s:%02x" eth0 12345

is a good solution too (for bash)

best,
Herbert

___
Vserver mailing list
[EMAIL PROTECTED]
http://list.linux-vserver.org/mailman/listinfo/vserver


Re: [Vserver] Dummy device creation without race states

2004-03-30 Thread Liam Helmer




On Wed, 2004-03-31 at 05:11, Herbert Poetzl wrote:

On Wed, Mar 31, 2004 at 01:41:14AM +, Liam Helmer wrote:
 printf "%s:%02x" eth0 12345



Ooh, cool. That's much nicer. So, after fixing a couple of other bugs too, it should look more like this:

vstohex () {
    [ -n "$1" ] || return 1
	local oct1=$((${1} / 256))
	local oct2=$((${1} % 256))
	printf "%02:%02" ${oct1} ${oct2} 
    }

if [ -n "${BUILD_VSIF}" ] && ! /sbin/ip link ls ${VSERVER_NAME} > /dev/null 2>&1 ; then
	olddummylist=($(/sbin/ip link ls 2> /dev/null | gawk '$2 ~ /^dummy/ {print $2}' | sed 's/:$//g' | sort))
	insmod -o dummy-${VSERVER_NAME} dummy
	newdummylist=($(/sbin/ip link ls 2> /dev/null | 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/run/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} "Couldn't set up interface ${VSERVER_NAME} correctly!"
			rm -f ${dummylock}
			break
		done
	fi
fi
if [ "$BUILD_VSIF" ] ; then
	if ! /sbin/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



is a good solution too (for bash)

best,
Herbert

___
Vserver mailing list
[EMAIL PROTECTED]
http://list.linux-vserver.org/mailman/listinfo/vserver