Xen 
 
Home About Xen.org Xen Xen Summit Wiki Mailing List Bug Tracker Xen Downloads
 
   
 

xen-users

[Xen-users] slightly less random random macs

To: xen-users@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-users] slightly less random random macs
From: Tim Post <tim.post@xxxxxxxxxxxxxxx>
Date: Wed, 29 Nov 2006 17:52:31 +0800
Delivery-date: Wed, 29 Nov 2006 01:52:59 -0800
Envelope-to: www-data@xxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-users-request@lists.xensource.com?subject=help>
List-id: Xen user discussion <xen-users.lists.xensource.com>
List-post: <mailto:xen-users@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-users>, <mailto:xen-users-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-users>, <mailto:xen-users-request@lists.xensource.com?subject=unsubscribe>
Organization: Net Kinetics
Reply-to: tim.post@xxxxxxxxxxxxxxx
Sender: xen-users-bounces@xxxxxxxxxxxxxxxxxxx
Hello all, 

There's been many posts lately about problems that stem from either
duplicate macs, non static macs and other networking issues that would
be averted if a mac was specified upon vm creation.

I've been using this little function I wrote in my shell scripts that
setup dom-u's from a csv file and its worked rather well, lets you
control the prefix and characters used in making the mac. 

As far as I know there is no "standard" mac prefix to denote a virtual
nic. 

Anyway here's the snippet / function which is very self explanatory and
should work with bash / dash / etc. This can be accomplished with python
using only a few lines of code, but bash seems more widely used when
creating back end helpers. 

Hope someone finds it of use.

Best,
-Tim

# creates a random mac address and sets a global _MAC variable
# to contain it.

makemac()
{
        # edit prefix to suit, ideally matches the manufacturer
        # prefix of the type of nic your using.
        local prefix="00:00:6d"
        local hextet[3]=""
        local hextet[4]=""
        local hextet[5]=""
        local tmp=""
        local i=3;
        # these characters are used in making the mac
        local digi='0a1b2c3d4e5f6789h'
        local offset=$((${#digi} - 1))
        _MAC="$prefix"

        while [ "$i" -le 5 ]; do
                tmp[1]="${digi:$(($RANDOM%${offset})):1}"
                tmp[2]="${digi:$(($RANDOM%${offset})):1}"
                hextet[$i]="${tmp[1]}${tmp[2]}"
                _MAC="${_MAC}:${hextet[i]}"
                let "i += 1"
        done

        unset octet
        unset tmp
        unset prefix
}

# When done, the variable $_MAC will contain the generated mac address.

Sample use :

#!/bin/sh

echo "I will make a mac address."
makemac
echo "The MAC address is ${_MAC}, Have a nice day."
unset _MAC

exit 0



_______________________________________________
Xen-users mailing list
Xen-users@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-users

<Prev in Thread] Current Thread [Next in Thread>