[Kimchi-devel] [PATCH 6/8] Get disk type according to file path on get_disk_xml()

Aline Manera alinefm at linux.vnet.ibm.com
Thu Oct 30 12:26:18 UTC 2014


The disk type can be also be identified by the file path.
So move it to the common place to handle guest disk XML manipulation.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/model/vmstorages.py | 33 ++++-----------------------------
 src/kimchi/xmlutils/disk.py    | 33 ++++++++++++++++++++++++++++-----
 2 files changed, 32 insertions(+), 34 deletions(-)

diff --git a/src/kimchi/model/vmstorages.py b/src/kimchi/model/vmstorages.py
index 4c4682c..70240b0 100644
--- a/src/kimchi/model/vmstorages.py
+++ b/src/kimchi/model/vmstorages.py
@@ -17,8 +17,6 @@
 # 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 os
-import stat
 import string
 
 from lxml import etree
@@ -28,7 +26,6 @@ from kimchi.exception import OperationFailed
 from kimchi.model.vms import DOM_STATE_MAP, VMModel
 from kimchi.model.storagevolumes import StorageVolumeModel
 from kimchi.model.utils import get_vm_config_flag
-from kimchi.utils import check_url_path
 from kimchi.osinfo import lookup
 from kimchi.xmlutils.disk import get_device_node, get_disk_xml
 from kimchi.xmlutils.disk import get_vm_disk_info, get_vm_disks
@@ -44,24 +41,6 @@ def _get_device_bus(dev_type, dom):
     return lookup(distro, version)[dev_type+'_bus']
 
 
-def _check_path(path):
-    if check_url_path(path):
-        src_type = 'network'
-    # Check if path is a valid local path
-    elif os.path.exists(path):
-        if os.path.isfile(path):
-            src_type = 'file'
-        else:
-            r_path = os.path.realpath(path)
-            if not stat.S_ISBLK(os.stat(r_path).st_mode):
-                raise InvalidParameter("KCHVMSTOR0003E", {'value': path})
-
-            src_type = 'block'
-    else:
-        raise InvalidParameter("KCHVMSTOR0003E", {'value': path})
-    return src_type
-
-
 class VMStoragesModel(object):
     def __init__(self, **kargs):
         self.conn = kargs['conn']
@@ -144,7 +123,7 @@ class VMStoragesModel(object):
 
         params.update(self._get_available_bus_address(params['bus'], vm_name))
         # Add device to VM
-        dev, xml = get_disk_xml(_check_path(params['path']), params)
+        dev, xml = get_disk_xml(params)
         try:
             conn = self.conn.get()
             dom = conn.lookupByName(vm_name)
@@ -190,20 +169,16 @@ class VMStorageModel(object):
             raise OperationFailed("KCHVMSTOR0010E", {'error': e.message})
 
     def update(self, vm_name, dev_name, params):
-        path = params.get('path', '')
-        params['path'] = path
-        if len(path) != 0:
-            src_type = _check_path(path)
-        else:
-            src_type = 'file'
         dom = VMModel.get_vm(vm_name, self.conn)
 
         dev_info = self.lookup(vm_name, dev_name)
         if dev_info['type'] != 'cdrom':
             raise InvalidOperation("KCHVMSTOR0006E")
 
+        params['path'] = params.get('path', '')
         dev_info.update(params)
-        dev, xml = get_disk_xml(src_type, dev_info, ignore_source)
+
+        dev, xml = get_disk_xml(dev_info)
         try:
             dom.updateDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
         except Exception as e:
diff --git a/src/kimchi/xmlutils/disk.py b/src/kimchi/xmlutils/disk.py
index f40f34f..f066dbe 100644
--- a/src/kimchi/xmlutils/disk.py
+++ b/src/kimchi/xmlutils/disk.py
@@ -18,20 +18,23 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
 import lxml.etree as ET
+import os
 import socket
+import stat
 import string
 import urlparse
 
 from lxml import objectify
 from lxml.builder import E
 
-from kimchi.exception import NotFoundError
+from kimchi.exception import InvalidParameter, NotFoundError
+from kimchi.utils import check_url_path
 
 BUS_TO_DEV_MAP = {'ide': 'hd', 'virtio': 'vd', 'scsi': 'sd'}
 DEV_TYPE_SRC_ATTR_MAP = {'file': 'file', 'block': 'dev'}
 
 
-def get_disk_xml(src_type, params):
+def get_disk_xml(params):
     """
     <disk type='file' device='cdrom'>
       <driver name='qemu' type='raw'/>
@@ -42,7 +45,9 @@ def get_disk_xml(src_type, params):
       <readonly/>
     </disk>
     """
-    disk = E.disk(type=src_type, device=params['type'])
+    path = params['path']
+    disk_type = _get_disk_type(path) if len(path) > 0 else 'file'
+    disk = E.disk(type=disk_type, device=params['type'])
     disk.append(E.driver(name='qemu', type=params['format']))
 
     # Get device name according to bus and index values
@@ -60,7 +65,7 @@ def get_disk_xml(src_type, params):
     if len(params['path']) == 0:
         return (dev, ET.tostring(disk, encoding='utf-8', pretty_print=True))
 
-    if src_type == 'network':
+    if disk_type == 'network':
         """
         <source protocol='%(protocol)s' name='%(url_path)s'>
           <host name='%(hostname)s' port='%(port)s'/>
@@ -76,12 +81,30 @@ def get_disk_xml(src_type, params):
         <source file='%(src)s' />
         """
         source = E.source()
-        source.set(DEV_TYPE_SRC_ATTR_MAP[src_type], params['path'])
+        source.set(DEV_TYPE_SRC_ATTR_MAP[disk_type], params['path'])
 
     disk.append(source)
     return (dev, ET.tostring(disk, encoding='utf-8', pretty_print=True))
 
 
+def _get_disk_type(path):
+    if check_url_path(path):
+        return 'network'
+
+    if not os.path.exists(path):
+        raise InvalidParameter("KCHVMSTOR0003E", {'value': path})
+
+    # Check if path is a valid local path
+    if os.path.isfile(path):
+        return 'file'
+
+    r_path = os.path.realpath(path)
+    if stat.S_ISBLK(os.stat(r_path).st_mode):
+        return 'block'
+
+    raise InvalidParameter("KCHVMSTOR0003E", {'value': path})
+
+
 def get_device_node(dom, dev_name):
     xml = dom.XMLDesc(0)
     devices = objectify.fromstring(xml).devices
-- 
1.9.3




More information about the Kimchi-devel mailing list