<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-      &lt;address type='pci' domain='0x0000' bus='0x00'
    slot='0x01' function='0x1'/&gt;<br>
    56-    &lt;/controller&gt;<br>
    <b>57:    &lt;interface type='bridge'&gt;</b><b><br>
    </b><b>58-      &lt;mac address='52:54:00:f5:e2:19'/&gt;</b><br>
    59-      &lt;source bridge='kbenp0s25'/&gt;<br>
    60-      &lt;model type='rtl8139'/&gt;<br>
    61-      &lt;address type='pci' domain='0x0000' bus='0x00'
    slot='0x03' function='0x0'/&gt;<br>
    62:    &lt;/interface&gt;<br>
    <b>63:    &lt;interface type='network'&gt;</b><b><br>
    </b><b>64-      &lt;mac address='52:54:00:07:48:ea'/&gt;</b><br>
    65-      &lt;source network='default'/&gt;<br>
    66-      &lt;model type='virtio'/&gt;<br>
    67-      &lt;address type='pci' domain='0x0000' bus='0x00'
    slot='0x07' function='0x0'/&gt;<br>
    68:    &lt;/interface&gt;<br>
    69-    &lt;serial type='pty'&gt;<br>
    70-      &lt;target port='0'/&gt;<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-      &lt;address type='pci' domain='0x0000' bus='0x00'
    slot='0x01' function='0x1'/&gt;<br>
    56-    &lt;/controller&gt;<br>
    <b>57:    &lt;interface type='bridge'&gt;</b><b><br>
    </b><b>58-      &lt;mac address='52:54:00:52:df:37'/&gt;</b><br>
    59-      &lt;source bridge='kbenp0s25'/&gt;<br>
    60-      &lt;model type='rtl8139'/&gt;<br>
    61-      &lt;address type='pci' domain='0x0000' bus='0x00'
    slot='0x03' function='0x0'/&gt;<br>
    62:    &lt;/interface&gt;<br>
    <b>63:    &lt;interface type='network'&gt;</b><b><br>
    </b><b>64-      &lt;mac address='52:54:00:31:5a:82'/&gt;</b><br>
    65-      &lt;source network='default'/&gt;<br>
    66-      &lt;model type='virtio'/&gt;<br>
    67-      &lt;address type='pci' domain='0x0000' bus='0x00'
    slot='0x07' function='0x0'/&gt;<br>
    68:    &lt;/interface&gt;<br>
    69-    &lt;serial type='pty'&gt;<br>
    70-      &lt;target port='0'/&gt;<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 &lt;interface type=network /&gt; only. Using correct xpath
framework, now all &lt;interface&gt; tag with type as attribute and &lt;mac
address&gt; 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">&lt;ramonn@linux.vnet.ibm.com&gt;</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 &lt;xml&gt; 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 &amp; 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>