Scripts/make-mac.sh

From Xen

The following, is a little script that can be used to assign unique MAC addresses based on the name of a VM and a Xen bridge.

#!/bin/bash
if [[ $# < 1 ]] ; then
    echo "Usage: $0 vmname [bridge]"
    exit 1
fi

name=$1
bridge="xenbr0"

if [[ $# > 1 ]] ; then
    bridge=$2
fi

hmac=$(cat /sys/class/net/${bridge}/address)

if [[ -z "$hmac" ]] ; then
    fail "Couldn't find address for device $bridge"
fi

# Get the host mac, and lop off the first two bytes
mac=$(echo $hmac | sed 's/^..:..:\(.*$\)/\1/;')

# Add an "extension" to the end based on the name
macext=$(echo "$name" | md5sum | sed 's/^\(..\).*$/\1/;')

# And pick a number for the first byte that has bit 2 set ("local")
mac="5a:${mac}:${macext}"

echo $mac