<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Test with network and bridge:<br>
<br>
ramonn@jarvis:~/GitTrees/KIMCHI (clone_macadress *)$ sudo virsh
dumpxml fedora | grep interface -n2<br>
55- <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x1'/><br>
56- </controller><br>
<b>57: <interface type='bridge'></b><b><br>
</b><b>58- <mac address='52:54:00:f5:e2:19'/></b><br>
59- <source bridge='kbenp0s25'/><br>
60- <model type='rtl8139'/><br>
61- <address type='pci' domain='0x0000' bus='0x00'
slot='0x03' function='0x0'/><br>
62: </interface><br>
<b>63: <interface type='network'></b><b><br>
</b><b>64- <mac address='52:54:00:07:48:ea'/></b><br>
65- <source network='default'/><br>
66- <model type='virtio'/><br>
67- <address type='pci' domain='0x0000' bus='0x00'
slot='0x07' function='0x0'/><br>
68: </interface><br>
69- <serial type='pty'><br>
70- <target port='0'/><br>
ramonn@jarvis:~/GitTrees/KIMCHI (clone_macadress *)$ sudo virsh edit
fedora^C<br>
ramonn@jarvis:~/GitTrees/KIMCHI (clone_macadress *)$ sudo virsh
dumpxml fedora-clone-1466116532715061 | grep interface -n2<br>
55- <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x1'/><br>
56- </controller><br>
<b>57: <interface type='bridge'></b><b><br>
</b><b>58- <mac address='52:54:00:52:df:37'/></b><br>
59- <source bridge='kbenp0s25'/><br>
60- <model type='rtl8139'/><br>
61- <address type='pci' domain='0x0000' bus='0x00'
slot='0x03' function='0x0'/><br>
62: </interface><br>
<b>63: <interface type='network'></b><b><br>
</b><b>64- <mac address='52:54:00:31:5a:82'/></b><br>
65- <source network='default'/><br>
66- <model type='virtio'/><br>
67- <address type='pci' domain='0x0000' bus='0x00'
slot='0x07' function='0x0'/><br>
68: </interface><br>
69- <serial type='pty'><br>
70- <target port='0'/><br>
<br>
<br>
<div class="moz-cite-prefix">On 06/16/2016 07:26 PM, Ramon Medeiros
wrote:<br>
</div>
<blockquote
cite="mid:1466116003-21039-1-git-send-email-ramonn@linux.vnet.ibm.com"
type="cite">
<pre wrap="">When cloning a VM, it's needed to add a new mac address. model/vms.py
was selection <interface type=network /> only. Using correct xpath
framework, now all <interface> tag with type as attribute and <mac
address> as a subelement, is selected. After all, they will received new
mac address. This way, it's not needed to check if the interfaces has it
own mac address.
Signed-off-by: Ramon Medeiros <a class="moz-txt-link-rfc2396E" href="mailto:ramonn@linux.vnet.ibm.com"><ramonn@linux.vnet.ibm.com></a>
---
model/vms.py | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/model/vms.py b/model/vms.py
index f069c4e..9753a10 100644
--- a/model/vms.py
+++ b/model/vms.py
@@ -83,12 +83,10 @@ VM_ONLINE_UPDATE_PARAMS = ['graphics', 'groups', 'memory', 'users']
VM_OFFLINE_UPDATE_PARAMS = ['cpu_info', 'graphics', 'groups', 'memory',
'name', 'users']
+XPATH_DOMAIN_CHANGE_MAC = "./devices/interface[@type='%s']/mac[@address='%s']"
XPATH_DOMAIN_DISK = "/domain/devices/disk[@device='disk']/source/@file"
XPATH_DOMAIN_DISK_BY_FILE = "./devices/disk[@device='disk']/source[@file='%s']"
XPATH_DOMAIN_NAME = '/domain/name'
-XPATH_DOMAIN_MAC = "/domain/devices/interface[@type='network']/mac/@address"
-XPATH_DOMAIN_MAC_BY_ADDRESS = "./devices/interface[@type='network']/"\
- "mac[@address='%s']<a class="moz-txt-link-rfc2396E" href="mailto:XPATH_DOMAIN_MEMORY='/domain/memory'XPATH_DOMAIN_MEMORY_UNIT='/domain/memory/@unit'XPATH_DOMAIN_UUID='/domain/uuid'@@-97,6+95,7@@XPATH_DOMAIN_DEV_CPU_ID='/domain/devices/spapr-cpu-socket/@id'XPATH_CPU='./cpu'XPATH_NAME='./name'XPATH_NUMA_CELL='./cpu/numa/cell'+XPATH_MAC=">"
XPATH_DOMAIN_MEMORY = '/domain/memory'
XPATH_DOMAIN_MEMORY_UNIT = '/domain/memory/@unit'
XPATH_DOMAIN_UUID = '/domain/uuid'
@@ -97,6 +95,7 @@ XPATH_DOMAIN_DEV_CPU_ID = '/domain/devices/spapr-cpu-socket/@id'
XPATH_CPU = './cpu'
XPATH_NAME = './name'
XPATH_NUMA_CELL = './cpu/numa/cell'
+XPATH_MAC = "</a>./devices/interface[@type]/mac/[@address]"
XPATH_TOPOLOGY = './cpu/topology'
XPATH_VCPU = './vcpu'
XPATH_MAX_MEMORY = './maxMemory'
@@ -417,19 +416,25 @@ class VMModel(object):
The XML descriptor <xml> with the new MAC addresses instead of the
old ones.
"""
- old_macs = xpath_get_text(xml, XPATH_DOMAIN_MAC)
- new_macs = []
+ # get dict with macs and interfaces
+ old_macs_int = {}
+ for node in ET.fromstring(xml).findall(XPATH_MAC):
+ old_macs_int[node.get("address")] = node.getparent().get("type")
- for mac in old_macs:
+ # update mac address
+ new_macs = []
+ for mac_addr in old_macs_int:
while True:
new_mac = model.vmifaces.VMIfacesModel.random_mac()
# make sure the new MAC doesn't conflict with the original VM
# and with the new values on the new VM.
- if new_mac not in (old_macs + new_macs):
+ if new_mac not in (old_macs_int.keys() + new_macs):
new_macs.append(new_mac)
break
- xml = xml_item_update(xml, XPATH_DOMAIN_MAC_BY_ADDRESS % mac,
+ dev_type = old_macs_int[mac_addr]
+ xml = xml_item_update(xml, XPATH_DOMAIN_CHANGE_MAC % (dev_type,
+ mac_addr),
new_mac, 'address')
return xml
</pre>
</blockquote>
<br>
<pre class="moz-signature" cols="72">--
Ramon Nunes Medeiros
Kimchi Developer
Linux Technology Center Brazil
IBM Systems & Technology Group
Phone : +55 19 2132 7878
<a class="moz-txt-link-abbreviated" href="mailto:ramonn@br.ibm.com">ramonn@br.ibm.com</a> </pre>
</body>
</html>