[Kimchi-devel] [PATCH 8/8] Update vmtemplate.py to use get_disk_xml() while generating CDROM XML

Aline Manera alinefm at linux.vnet.ibm.com
Mon Oct 27 15:40:27 UTC 2014


It is the first step to have vmtemplate.py using the same functions used
by model/vmstorages.py
The disk generation will also use get_disk_xml() but it will be done in
a separated patch.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/model/vms.py  |  1 -
 src/kimchi/vmtemplate.py | 78 +++++++++++++-----------------------------------
 2 files changed, 20 insertions(+), 59 deletions(-)

diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index 1089464..51d2348 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -209,7 +209,6 @@ class VMsModel(object):
         stream_protocols = self.caps.libvirt_stream_protocols
         xml = t.to_vm_xml(name, vm_uuid,
                           libvirt_stream_protocols=stream_protocols,
-                          qemu_stream_dns=self.caps.qemu_stream_dns,
                           graphics=graphics,
                           volumes=vol_list)
 
diff --git a/src/kimchi/vmtemplate.py b/src/kimchi/vmtemplate.py
index 5f22db9..aede24f 100644
--- a/src/kimchi/vmtemplate.py
+++ b/src/kimchi/vmtemplate.py
@@ -19,23 +19,20 @@
 
 import os
 import string
-import socket
 import time
 import urlparse
 import uuid
 
-
 from distutils.version import LooseVersion
-
+from lxml import etree
+from lxml.builder import E
 
 from kimchi import osinfo
 from kimchi.exception import InvalidParameter, IsoFormatError, MissingParameter
 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 lxml import etree
-from lxml.builder import E
-
+from kimchi.xmlutils.disk import get_disk_xml
 
 QEMU_NAMESPACE = "xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'"
 
@@ -122,69 +119,36 @@ class VMTemplate(object):
         except IsoFormatError:
             raise InvalidParameter("KCHISO0001E", {'filename': iso})
 
-    def _get_cdrom_xml(self, libvirt_stream_protocols, qemu_stream_dns):
+    def _get_cdrom_xml(self, libvirt_stream_protocols):
         if 'cdrom' not in self.info:
             return ''
-        bus = self.info['cdrom_bus']
-        dev = "%s%s" % (self._bus_to_dev[bus],
-                        string.lowercase[self.info['cdrom_index']])
 
-        local_file = """
-            <disk type='file' device='cdrom'>
-              <driver name='qemu' type='raw'/>
-              <source file='%(src)s' />
-              <target dev='%(dev)s' bus='%(bus)s'/>
-              <readonly/>
-            </disk>
-        """
-
-        network_file = """
-            <disk type='network' device='cdrom'>
-              <driver name='qemu' type='raw'/>
-              <source protocol='%(protocol)s' name='%(url_path)s'>
-                <host name='%(hostname)s' port='%(port)s'/>
-              </source>
-              <target dev='%(dev)s' bus='%(bus)s'/>
-              <readonly/>
-            </disk>
-        """
+        params = {}
+        params['type'] = 'cdrom'
+        params['format'] = 'raw'
+        params['bus'] = self.info['cdrom_bus']
+        params['index'] = self.info['cdrom_index']
+        params['path'] = self.info['cdrom']
 
         qemu_stream_cmdline = """
             <qemu:commandline>
               <qemu:arg value='-drive'/>
-              <qemu:arg value='file=%(url)s,if=none,id=drive-%(bus)s0-1-0,\
-readonly=on,format=raw'/>
+              <qemu:arg value='file=%(path)s,if=none,id=drive-%(bus)s0-1-0,\
+readonly=on,format=%(format)s'/>
               <qemu:arg value='-device'/>
               <qemu:arg value='%(bus)s-cd,bus=%(bus)s.1,unit=0,\
 drive=drive-%(bus)s0-1-0,id=%(bus)s0-1-0'/>
             </qemu:commandline>
         """
 
-        if not self.info.get('iso_stream', False):
-            params = {'src': self.info['cdrom'], 'dev': dev, 'bus': bus}
-            return local_file % (params)
-
-        output = urlparse.urlparse(self.info['cdrom'])
-        port = output.port
-        protocol = output.scheme
-        hostname = output.hostname
-        url_path = output.path
-
-        if port is None:
-            port = socket.getservbyname(protocol)
+        if self.info.get('iso_stream', False):
+            protocol = urlparse.urlparse(params['path']).scheme
+            if protocol not in libvirt_stream_protocols:
+                # return qemucmdline XML
+                return qemu_stream_cmdline % params
 
-        url = self.info['cdrom']
-        if not qemu_stream_dns:
-            hostname = socket.gethostbyname(hostname)
-            url = protocol + "://" + hostname + ":" + str(port) + url_path
-
-        if protocol not in libvirt_stream_protocols:
-            return qemu_stream_cmdline % {'url': url, 'bus': bus}
-
-        params = {'protocol': protocol, 'url_path': url_path,
-                  'hostname': hostname, 'port': port, 'dev': dev, 'bus': bus}
-
-        return network_file % (params)
+        dev, xml = get_disk_xml(params)
+        return xml
 
     def _get_disks_xml(self, vm_uuid):
         storage_path = self._get_storage_path()
@@ -380,10 +344,8 @@ drive=drive-%(bus)s0-1-0,id=%(bus)s0-1-0'/>
         else:
             params['disks'] = self._get_disks_xml(vm_uuid)
 
-        qemu_stream_dns = kwargs.get('qemu_stream_dns', False)
         libvirt_stream_protocols = kwargs.get('libvirt_stream_protocols', [])
-        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols,
-                                        qemu_stream_dns)
+        cdrom_xml = self._get_cdrom_xml(libvirt_stream_protocols)
 
         if not urlparse.urlparse(self.info.get('cdrom', "")).scheme in \
                 libvirt_stream_protocols and \
-- 
1.9.3




More information about the Kimchi-devel mailing list