On 01/24/2014 03:55 PM, Martin Kletzander wrote:
On Fri, Jan 24, 2014 at 10:37:20AM +0800, Sheldon wrote:
> Now I working on a new KVM management tool base on html5 and libvirt
>
> we will support a REST API for detach and attach guest interface.
>
> we want to use mac as the identifier of the network interface.
>
> That meas we should get the mac after the user create a interface.
> also other people may be doing the exact same thing at the exact same
> time to create a interface.
>
>
> Here are the demo codes:
>
> dom = self._get_vm(vm)
> xml = """
> <interface type='network'>
> <source network='default'/>
> </interface>
> """
> dom.attachDeviceFlags(xml, libvirt.VIR_DOMAIN_AFFECT_CURRENT)
> # now I want to get the mac, how to get it?
>
You have to parse the XML back and find it.
> I have check libvirt use this code to generate a mac address.
> does libvirt can gurantee that the MAC address generated is unique?
>
No, as mentioned in the second subthread, the XML you provided is
incomplete and can cause address clashing.
So te best answer to your question in $SUBJ would be "generate it
yourself and add it to the XML before attaching".
now I define a mac address generator in our management tool.
it will generate a mac and assign it xml before create the interface.
+ def randomMAC():
+ mac = [0x52, 0x54, 0x00,
+ random.randint(0x00, 0x7f),
+ random.randint(0x00, 0xff),
+ random.randint(0x00, 0xff)]
+ mac = randomMAC()
+ xml = """
+ <interface type='network'>
+ <source network='default'/>
+ <mac address='%s' >
+ </interface>
+ """ % mac
should I gurantee that the MAC address generated is unique by myself?
check every interface mac of every guest, and make sure no address clashing?
I wonder can libvirt support a API to generate mac address?
Martin
> void virMacAddrGenerate(const unsigned char prefix[VIR_MAC_PREFIX_BUFLEN],
> virMacAddrPtr addr)
> {
> addr->addr[0] = prefix[0];
> addr->addr[1] = prefix[1];
> addr->addr[2] = prefix[2];
> addr->addr[3] = virRandomBits(8);
> addr->addr[4] = virRandomBits(8);
> addr->addr[5] = virRandomBits(8);
> }
> libvirt also use the KVM prefix as default as it's in the privately
> administered range:
> 52:54:00:XX:XX:XX
>
>
>
> --
> Thanks and best regards!
>
> Sheldon Feng(冯少合)<shaohef(a)linux.vnet.ibm.com>
> IBM Linux Technology Center
>
> --
> libvir-list mailing list
> libvir-list(a)redhat.com
>
https://www.redhat.com/mailman/listinfo/libvir-list
--
Thanks and best regards!
Sheldon Feng(冯少合)<shaohef(a)linux.vnet.ibm.com>
IBM Linux Technology Center