[Kimchi-devel] [PATCH 2/8] Move vmdisks.py functions to xmlutils/disk.py

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


vmdisks.py also had some functions to manipulate the guest disk XML.
So move them to xmlutils/disk.py
Also update the references to it on Kimchi code.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/model/storagevolumes.py |  4 +-
 src/kimchi/model/vmstorages.py     | 10 ++---
 src/kimchi/vmdisks.py              | 75 --------------------------------------
 src/kimchi/xmlutils/disk.py        | 53 +++++++++++++++++++++++++++
 4 files changed, 60 insertions(+), 82 deletions(-)
 delete mode 100644 src/kimchi/vmdisks.py

diff --git a/src/kimchi/model/storagevolumes.py b/src/kimchi/model/storagevolumes.py
index 1ee8d0a..db596c3 100644
--- a/src/kimchi/model/storagevolumes.py
+++ b/src/kimchi/model/storagevolumes.py
@@ -32,7 +32,7 @@ from kimchi.model.storagepools import StoragePoolModel
 from kimchi.model.tasks import TaskModel
 from kimchi.model.vms import VMsModel, VMModel
 from kimchi.utils import add_task, kimchi_log
-from kimchi.vmdisks import get_vm_disk, get_vm_disk_list
+from kimchi.xmlutils.disk import get_vm_disk_info, get_vm_disk_list
 from kimchi.xmlutils.utils import xpath_get_text
 
 
@@ -267,7 +267,7 @@ class StorageVolumeModel(object):
                         dom = VMModel.get_vm(vm, self.conn)
                         storages = get_vm_disk_list(dom)
                         for disk in storages:
-                            d_info = get_vm_disk(dom, disk)
+                            d_info = get_vm_disk_info(dom, disk)
                             if path == d_info['path']:
                                 ref_cnt = ref_cnt + 1
                     session.store('storagevolume', vol_id,
diff --git a/src/kimchi/model/vmstorages.py b/src/kimchi/model/vmstorages.py
index 808b3d7..3a8b6b3 100644
--- a/src/kimchi/model/vmstorages.py
+++ b/src/kimchi/model/vmstorages.py
@@ -30,8 +30,8 @@ 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.vmdisks import get_device_xml, get_vm_disk, get_vm_disk_list
-from kimchi.xmlutils.disk import get_disk_xml
+from kimchi.xmlutils.disk import get_device_node, get_disk_xml
+from kimchi.xmlutils.disk import get_vm_disk_info, get_vm_disk_list
 
 HOTPLUG_TYPE = ['scsi', 'virtio']
 PREFIX_MAP = {'ide': 'hd', 'virtio': 'vd', 'scsi': 'sd'}
@@ -78,7 +78,7 @@ class VMStoragesModel(object):
         valid_id = [('0', '0'), ('0', '1'), ('1', '0'), ('1', '1')]
         controller_id = '0'
         for dev_name in disks:
-            disk = get_device_xml(dom, dev_name)
+            disk = get_device_node(dom, dev_name)
             if disk.target.attrib['bus'] == 'ide':
                 controller_id = disk.address.attrib['controller']
                 bus_id = disk.address.attrib['bus']
@@ -170,7 +170,7 @@ class VMStorageModel(object):
     def lookup(self, vm_name, dev_name):
         # Retrieve disk xml and format return dict
         dom = VMModel.get_vm(vm_name, self.conn)
-        return get_vm_disk(dom, dev_name)
+        return get_vm_disk_info(dom, dev_name)
 
     def delete(self, vm_name, dev_name):
         # Get storage device xml
@@ -188,7 +188,7 @@ class VMStorageModel(object):
         try:
             conn = self.conn.get()
             dom = conn.lookupByName(vm_name)
-            disk = get_device_xml(dom, dev_name)
+            disk = get_device_node(dom, dev_name)
             dom.detachDeviceFlags(etree.tostring(disk),
                                   get_vm_config_flag(dom, 'all'))
         except Exception as e:
diff --git a/src/kimchi/vmdisks.py b/src/kimchi/vmdisks.py
deleted file mode 100644
index f1c3f02..0000000
--- a/src/kimchi/vmdisks.py
+++ /dev/null
@@ -1,75 +0,0 @@
-#
-# 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
-
-from lxml import objectify
-
-from kimchi.exception import NotFoundError
-
-DEV_TYPE_SRC_ATTR_MAP = {'file': 'file',
-                         'block': 'dev'}
-
-
-def get_device_xml(dom, dev_name):
-    # Get VM xml and then devices xml
-    xml = dom.XMLDesc(0)
-    devices = objectify.fromstring(xml).devices
-    disk = devices.xpath("./disk/target[@dev='%s']/.." % dev_name)
-    if not disk:
-        return None
-    return disk[0]
-
-
-def get_vm_disk(dom, dev_name):
-    # Retrieve disk xml and format return dict
-    disk = get_device_xml(dom, dev_name)
-    if disk is None:
-        raise NotFoundError("KCHVMSTOR0007E",
-                            {'dev_name': dev_name,
-                             'vm_name': dom.name()})
-    path = ""
-    dev_bus = disk.target.attrib['bus']
-    try:
-        source = disk.source
-        if source is not None:
-            src_type = disk.attrib['type']
-            if src_type == 'network':
-                host = source.host
-                path = (source.attrib['protocol'] + '://' +
-                        host.attrib['name'] + ':' +
-                        host.attrib['port'] + source.attrib['name'])
-            else:
-                path = source.attrib[DEV_TYPE_SRC_ATTR_MAP[src_type]]
-    except:
-        pass
-    dev_type = disk.attrib['device']
-    return {'dev': dev_name,
-            'type': dev_type,
-            'path': path,
-            'format': disk.driver.attrib['type'],
-            'bus': dev_bus}
-
-
-def get_vm_disk_list(dom):
-    xml = dom.XMLDesc(0)
-    devices = objectify.fromstring(xml).devices
-    storages = [disk.target.attrib['dev']
-                for disk in devices.xpath("./disk[@device='disk']")]
-    storages += [disk.target.attrib['dev']
-                 for disk in devices.xpath("./disk[@device='cdrom']")]
-    return storages
diff --git a/src/kimchi/xmlutils/disk.py b/src/kimchi/xmlutils/disk.py
index 8c64ff4..800a64c 100644
--- a/src/kimchi/xmlutils/disk.py
+++ b/src/kimchi/xmlutils/disk.py
@@ -21,8 +21,11 @@ import lxml.etree as ET
 import socket
 import urlparse
 
+from lxml import objectify
 from lxml.builder import E
 
+from kimchi.exception import NotFoundError
+
 DEV_TYPE_SRC_ATTR_MAP = {'file': 'file', 'block': 'dev'}
 
 
@@ -71,3 +74,53 @@ def get_disk_xml(src_type, params, ignore_src=False):
 
     disk.append(source)
     return ET.tostring(disk, encoding='utf-8', pretty_print=True)
+
+
+def get_device_node(dom, dev_name):
+    xml = dom.XMLDesc(0)
+    devices = objectify.fromstring(xml).devices
+    disk = devices.xpath("./disk/target[@dev='%s']/.." % dev_name)
+
+    if not disk:
+        raise NotFoundError("KCHVMSTOR0007E",
+                            {'dev_name': dev_name,
+                             'vm_name': dom.name()})
+
+    return disk[0]
+
+
+def get_vm_disk_info(dom, dev_name):
+    # Retrieve disk xml and format return dict
+    disk = get_device_node(dom, dev_name)
+    if disk is None:
+        return None
+
+    try:
+        source = disk.source
+        if source is not None:
+            src_type = disk.attrib['type']
+            if src_type == 'network':
+                host = source.host
+                path = (source.attrib['protocol'] + '://' +
+                        host.attrib['name'] + ':' +
+                        host.attrib['port'] + source.attrib['name'])
+            else:
+                path = source.attrib[DEV_TYPE_SRC_ATTR_MAP[src_type]]
+    except:
+        path = ""
+
+    return {'dev': dev_name,
+            'path': path,
+            'type': disk.attrib['device'],
+            'format': disk.driver.attrib['type'],
+            'bus': disk.target.attrib['bus']}
+
+
+def get_vm_disk_list(dom):
+    xml = dom.XMLDesc(0)
+    devices = objectify.fromstring(xml).devices
+    storages = [disk.target.attrib['dev']
+                for disk in devices.xpath("./disk[@device='disk']")]
+    storages += [disk.target.attrib['dev']
+                 for disk in devices.xpath("./disk[@device='cdrom']")]
+    return storages
-- 
1.9.3




More information about the Kimchi-devel mailing list