Discussion:
[libvirt] [question]Is there a good way to get the mac of guest interface after I attached it to a guest
Sheldon
2014-01-24 02:37:20 UTC
Permalink
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?


I have check libvirt use this code to generate a mac address.
does libvirt can gurantee that the MAC address generated is unique?


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(冯少合)<***@linux.vnet.ibm.com>
IBM Linux Technology Center
Jincheng Miao
2014-01-24 06:40:42 UTC
Permalink
----- Original Message -----
Post by Sheldon
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.
dom = self._get_vm(vm)
xml = """
<interface type='network'>
<source network='default'/>
</interface>
"""
This interface xml is partial, and it is not recommended according to 'man virsh':
"
Note: using of partial device definition XML files may lead to
unexpected results as some fields may be autogenerated and thus
match devices other than expected.
"

And there is a way to find out the mac address specified by libvirt,
we can check /var/lib/libvirt/dnsmasq/default.leases, newer one is on top
# cat /var/lib/libvirt/dnsmasq/default.leases
1390549166 52:54:00:ea:9f:58 192.168.122.106 * *
1390547957 52:54:00:9d:43:38 192.168.122.134 * *
1390547828 52:54:00:0b:2f:ec 192.168.122.76 localhost *

But seems there is some latency, :(
Post by Sheldon
dom.attachDeviceFlags(xml, libvirt.VIR_DOMAIN_AFFECT_CURRENT)
# now I want to get the mac, how to get it?
I have check libvirt use this code to generate a mac address.
does libvirt can gurantee that the MAC address generated is unique?
yes, I also have this question.
Post by Sheldon
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
52:54:00:XX:XX:XX
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
Sheldon
2014-01-24 07:00:31 UTC
Permalink
Post by Jincheng Miao
----- Original Message -----
Post by Sheldon
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.
dom = self._get_vm(vm)
xml = """
<interface type='network'>
<source network='default'/>
</interface>
"""
"
Note: using of partial device definition XML files may lead to
unexpected results as some fields may be autogenerated and thus
match devices other than expected.
"
And there is a way to find out the mac address specified by libvirt,
we can check /var/lib/libvirt/dnsmasq/default.leases, newer one is on top
# cat /var/lib/libvirt/dnsmasq/default.leases
1390549166 52:54:00:ea:9f:58 192.168.122.106 * *
1390547957 52:54:00:9d:43:38 192.168.122.134 * *
1390547828 52:54:00:0b:2f:ec 192.168.122.76 localhost *
But seems there is some latency, :(
yes, but only the vm is running, we can get the mac and IP.
Post by Jincheng Miao
Post by Sheldon
dom.attachDeviceFlags(xml, libvirt.VIR_DOMAIN_AFFECT_CURRENT)
# now I want to get the mac, how to get it?
I have check libvirt use this code to generate a mac address.
does libvirt can gurantee that the MAC address generated is unique?
yes, I also have this question.
Post by Sheldon
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
52:54:00:XX:XX:XX
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
--
Thanks and best regards!

Sheldon Feng(冯少合)<***@linux.vnet.ibm.com>
IBM Linux Technology Center
Martin Kletzander
2014-01-24 07:55:57 UTC
Permalink
Post by Sheldon
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.
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.
Post by Sheldon
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".

Martin
Post by Sheldon
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
52:54:00:XX:XX:XX
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
Sheldon
2014-01-24 08:34:12 UTC
Permalink
Post by Martin Kletzander
Post by Sheldon
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.
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.
Post by Sheldon
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?
Post by Martin Kletzander
Martin
Post by Sheldon
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
52:54:00:XX:XX:XX
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
--
Thanks and best regards!

Sheldon Feng(冯少合)<***@linux.vnet.ibm.com>
IBM Linux Technology Center
Martin Kletzander
2014-01-24 09:30:24 UTC
Permalink
Post by Sheldon
Post by Martin Kletzander
Post by Sheldon
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.
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.
Post by Sheldon
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.
+ 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?
Yes, exactly. You may be trying how the systems behave when MAC
addresses clash, you may have different subnets which are not
connected and you want have the same MAC address, but libvirt will
never know if you want to connect them together, you may have two
machines defined with slight differences (one with numa one without)
and run the one you want each time, etc. And libvirt can't possibly
know about this. IIRC, virt-manager can do the thing you want, but
that's probably not applicable for you, so you'll have to go through
the list of VMs you don't want the addresses to clash and check
whether the MAC is ok or not. Or assign a MAC using a function
according to some unique element of yours and you don't have to check
anything.
Post by Sheldon
I wonder can libvirt support a API to generate mac address?
Patches are welcome :-)

Martin
Post by Sheldon
Post by Martin Kletzander
Martin
Post by Sheldon
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
52:54:00:XX:XX:XX
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
Sheldon
2014-01-24 16:06:10 UTC
Permalink
Post by Martin Kletzander
Post by Sheldon
Post by Martin Kletzander
Post by Sheldon
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.
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.
Post by Sheldon
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.
+ 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?
Yes, exactly. You may be trying how the systems behave when MAC
addresses clash, you may have different subnets which are not
connected and you want have the same MAC address, but libvirt will
never know if you want to connect them together, you may have two
machines defined with slight differences (one with numa one without)
and run the one you want each time, etc. And libvirt can't possibly
know about this. IIRC, virt-manager can do the thing you want, but
that's probably not applicable for you, so you'll have to go through
the list of VMs you don't want the addresses to clash and check
whether the MAC is ok or not. Or assign a MAC using a function
according to some unique element of yours and you don't have to check
anything.
Post by Sheldon
I wonder can libvirt support a API to generate mac address?
Patches are welcome :-)
libvirt.virDomain.macAddrGenerate() or
libvirt.virConnect.macAddrGenerate() ?
Post by Martin Kletzander
Martin
Post by Sheldon
Post by Martin Kletzander
Martin
Post by Sheldon
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
52:54:00:XX:XX:XX
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
--
Thanks and best regards!
IBM Linux Technology Center
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
--
Thanks and best regards!

Sheldon Feng(冯少合)<***@linux.vnet.ibm.com>
IBM Linux Technology Center
Martin Kletzander
2014-01-24 16:19:42 UTC
Permalink
Post by Sheldon
Post by Martin Kletzander
Post by Sheldon
Post by Martin Kletzander
Post by Sheldon
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.
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.
Post by Sheldon
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.
+ 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?
Yes, exactly. You may be trying how the systems behave when MAC
addresses clash, you may have different subnets which are not
connected and you want have the same MAC address, but libvirt will
never know if you want to connect them together, you may have two
machines defined with slight differences (one with numa one without)
and run the one you want each time, etc. And libvirt can't possibly
know about this. IIRC, virt-manager can do the thing you want, but
that's probably not applicable for you, so you'll have to go through
the list of VMs you don't want the addresses to clash and check
whether the MAC is ok or not. Or assign a MAC using a function
according to some unique element of yours and you don't have to check
anything.
Post by Sheldon
I wonder can libvirt support a API to generate mac address?
Patches are welcome :-)
libvirt.virDomain.macAddrGenerate() or
libvirt.virConnect.macAddrGenerate() ?
I'd say it depends on what you want it to do. If you want it *only*
to generate the mac address, then I see no point in exposing it; that'd
be like exposing a STRPREFIX macro :)

Martin

Continue reading on narkive:
Search results for '[libvirt] [question]Is there a good way to get the mac of guest interface after I attached it to a guest' (Questions and Answers)
14
replies
Creating a "Why we should switch to Mac" Speech. Help Please?
started 2007-10-26 15:44:14 UTC
desktops
Loading...