[Kimchi-devel] [PATCH] Create common function to generate interface guest XML

Aline Manera alinefm at linux.vnet.ibm.com
Tue Nov 4 16:39:54 UTC 2014


And use it on vmtemplate.py and model/vmifaces.py to make sure in both
cases the same XML structure will be used.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/model/vmifaces.py     | 19 +++++-----------
 src/kimchi/vmtemplate.py         | 29 ++++++------------------
 src/kimchi/xmlutils/interface.py | 49 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+), 35 deletions(-)
 create mode 100644 src/kimchi/xmlutils/interface.py

diff --git a/src/kimchi/model/vmifaces.py b/src/kimchi/model/vmifaces.py
index 721bf14..87ada6d 100644
--- a/src/kimchi/model/vmifaces.py
+++ b/src/kimchi/model/vmifaces.py
@@ -21,10 +21,10 @@ import random
 
 import libvirt
 from lxml import etree, objectify
-from lxml.builder import E
 
 from kimchi.exception import InvalidOperation, InvalidParameter, NotFoundError
 from kimchi.model.vms import DOM_STATE_MAP, VMModel
+from kimchi.xmlutils.interface import get_iface_xml
 
 
 class VMIfacesModel(object):
@@ -53,22 +53,15 @@ class VMIfacesModel(object):
                 for iface in self.get_vmifaces(vm, self.conn))
 
         while True:
-            mac = VMIfacesModel.random_mac()
-            if mac not in macs:
+            params['mac'] = VMIfacesModel.random_mac()
+            if params['mac'] not in macs:
                 break
 
-        children = [E.mac(address=mac)]
-        ("network" in params.keys() and
-         children.append(E.source(network=params['network'])))
-        ("model" in params.keys() and
-         children.append(E.model(type=params['model'])))
-        attrib = {"type": params["type"]}
-
-        xml = etree.tostring(E.interface(*children, **attrib))
-
+        os_distro, os_version = VMModel.vm_get_os_metadata(dom)
+        xml = get_iface_xml(params, conn.getInfo()[0], os_distro, os_version)
         dom.attachDeviceFlags(xml, libvirt.VIR_DOMAIN_AFFECT_CURRENT)
 
-        return mac
+        return params['mac']
 
     @staticmethod
     def get_vmifaces(vm, conn):
diff --git a/src/kimchi/vmtemplate.py b/src/kimchi/vmtemplate.py
index 4e5806f..25f1994 100644
--- a/src/kimchi/vmtemplate.py
+++ b/src/kimchi/vmtemplate.py
@@ -23,7 +23,6 @@ import time
 import urlparse
 import uuid
 
-from distutils.version import LooseVersion
 from lxml import etree
 from lxml.builder import E
 
@@ -33,6 +32,7 @@ from kimchi.imageinfo import probe_image, probe_img_info
 from kimchi.isoinfo import IsoImage
 from kimchi.utils import check_url_path, pool_name_from_uri
 from kimchi.xmlutils.disk import get_disk_xml
+from kimchi.xmlutils.interface import get_iface_xml
 from kimchi.xmlutils.qemucmdline import get_qemucmdline_xml
 
 
@@ -239,30 +239,15 @@ class VMTemplate(object):
             ret.append(info)
         return ret
 
-    def _disable_vhost(self):
-        # Hack to disable vhost feature in Ubuntu LE and SLES LE (PPC)
-        driver = ""
-        if self.info['arch'] == 'ppc64' and \
-            ((self.info['os_distro'] == 'ubuntu' and LooseVersion(
-             self.info['os_version']) >= LooseVersion('14.04')) or
-             (self.info['os_distro'] == 'sles' and LooseVersion(
-              self.info['os_version']) >= LooseVersion('12'))):
-            driver = "  <driver name='qemu'/>\n            "
-        return driver
-
     def _get_networks_xml(self):
-        network = """
-            <interface type='network'>
-              <source network='%(network)s'/>
-              <model type='%(nic_model)s'/>
-            %(driver)s</interface>
-        """
         networks = ""
-        net_info = {"nic_model": self.info['nic_model'],
-                    "driver": self._disable_vhost()}
+        params = {'type': 'network',
+                  'model': self.info['nic_model']}
         for nw in self.info['networks']:
-            net_info['network'] = nw
-            networks += network % net_info
+            params['network'] = nw
+            networks += get_iface_xml(params, self.info['arch'],
+                                      self.info['os_distro'],
+                                      self.info['os_version'])
         return networks
 
     def _get_input_output_xml(self):
diff --git a/src/kimchi/xmlutils/interface.py b/src/kimchi/xmlutils/interface.py
new file mode 100644
index 0000000..c76131a
--- /dev/null
+++ b/src/kimchi/xmlutils/interface.py
@@ -0,0 +1,49 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2014
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import lxml.etree as ET
+
+from distutils.version import LooseVersion
+from lxml.builder import E
+
+
+def get_iface_xml(params, arch=None, os_distro=None, os_version=None):
+    """
+    <interface type='network'>
+      <source network='default'/>
+      <model type='virtio'/>
+    </interface>
+    """
+    interface = E.interface(type=params['type'])
+    interface.append(E.source(network=params['network']))
+    interface.append(E.model(type=params['model']))
+
+    mac = params.get('mac', None)
+    if mac is not None:
+        interface.append(E.mac(address=mac))
+
+    # Hack to disable vhost feature in Ubuntu LE and SLES LE (PPC)
+    if arch == 'ppc64' and \
+       ((os_distro == 'ubuntu' and
+         LooseVersion(os_version) >= LooseVersion('14.04')) or
+        (os_distro == 'sles' and
+         LooseVersion(os_version >= LooseVersion('12')))):
+        interface.append(E.driver(name='qemu'))
+
+    return ET.tostring(interface, encoding='utf-8', pretty_print=True)
-- 
1.9.3




More information about the Kimchi-devel mailing list