[Kimchi-devel] [PATCH] [Kimchi 2/4] Allow disks to update cache and io flags

Ramon Medeiros posted 4 patches 5 years, 8 months ago
[Kimchi-devel] [PATCH] [Kimchi 2/4] Allow disks to update cache and io flags
Posted by Ramon Medeiros 5 years, 8 months ago
This allows VMs to update disks flags (io and cache).

Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com>
---
 API.json            | 11 ++++++++++-
 docs/API.md         |  9 +++++++++
 i18n.py             |  1 -
 model/vmstorages.py | 51 +++++++++++++++++++++++++++++++++------------------
 xmlutils/disk.py    | 10 ++++++++++
 5 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/API.json b/API.json
index 5729e5d..abe980c 100644
--- a/API.json
+++ b/API.json
@@ -761,8 +761,17 @@
                     "description": "Path of iso image file or disk mount point",
                     "type": "string",
                     "pattern": "^(|(/)|(http)[s]?:|[t]?(ftp)[s]?:)+.*$",
-                    "required": true,
                     "error": "KCHVMSTOR0003E"
+                },
+                "cache": {
+                    "description": "Cache options",
+                    "type": "string",
+                    "pattern": "^(none|writethrough|writeback|directsync|unsafe|default)$"
+                },
+                "io": {
+                    "description": "I/O options",
+                    "type": "string",
+                    "pattern": "^(native|threads|default)$"
                 }
             },
             "additionalProperties": false
diff --git a/docs/API.md b/docs/API.md
index 3ecc7a0..1aa7308 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -243,6 +243,10 @@ Represents a snapshot of the Virtual Machine's primary monitor.
     * dir_path: s390x specific attribute to attach direct storage without libvirt
     * format: s390x specific attribute specify the format of direct storage
     * size: s390x specific attribute to specify the size of direct storage
+    * io: IO settings for disks. Values accepted: native, threads and default.
+    * cache: Cache settings for disks. Values accepted: none, writethrough,
+      writeback, directsync, unsafe and default.
+    * bus: Bus type of disk.
 
 ### Sub-resource: storage
 **URI:** /plugins/kimchi/vms/*:name*/storages/*:dev*
@@ -250,9 +254,14 @@ Represents a snapshot of the Virtual Machine's primary monitor.
     * dev: The name of the storage in the vm.
     * type: The type of the storage (currently support 'cdrom' and 'disk').
     * path: Path of cdrom iso or disk image file.
+    * io: IO settings for disks.
+    * cache: Cache settings for disks.
     * bus: Bus type of disk attached.
 * **PUT**: Update storage information
     * path: Path of cdrom iso. Can not be blank. Now just support cdrom type.
+    * io: IO settings for disks. Values accepted: native, threads and default.
+    * cache: Cache settings for disks. Values accepted: none, writethrough,
+        writeback, directsync, unsafe and default.
 * **DELETE**: Remove the storage.
 
 **Actions (POST):**
diff --git a/i18n.py b/i18n.py
index 4460ce5..4bb3a4f 100644
--- a/i18n.py
+++ b/i18n.py
@@ -327,7 +327,6 @@ messages = {
 
     "KCHVMSTOR0002E": _("Invalid storage type. Types supported: 'cdrom', 'disk'"),
     "KCHVMSTOR0003E": _("The path '%(value)s' is not a valid local/remote path for the device"),
-    "KCHVMSTOR0006E": _("Only CDROM path can be update."),
     "KCHVMSTOR0007E": _("The storage device %(dev_name)s does not exist in the virtual machine %(vm_name)s"),
     "KCHVMSTOR0008E": _("Error while creating new storage device: %(error)s"),
     "KCHVMSTOR0009E": _("Error while updating storage device: %(error)s"),
diff --git a/model/vmstorages.py b/model/vmstorages.py
index db68121..007e88c 100644
--- a/model/vmstorages.py
+++ b/model/vmstorages.py
@@ -1,7 +1,7 @@
 #
 # Project Kimchi
 #
-# Copyright IBM Corp, 2015-2016
+# Copyright IBM Corp, 2015-2017
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -35,7 +35,6 @@ from wok.plugins.kimchi.utils import create_disk_image, is_s390x
 from wok.plugins.kimchi.xmlutils.disk import get_device_node, get_disk_xml
 from wok.plugins.kimchi.xmlutils.disk import get_vm_disk_info, get_vm_disks
 
-
 HOTPLUG_TYPE = ['scsi', 'virtio']
 
 
@@ -232,26 +231,42 @@ class VMStorageModel(object):
         dom = VMModel.get_vm(vm_name, self.conn)
 
         dev_info = self.lookup(vm_name, dev_name)
+
+        # only change path to cdrom devices
         if dev_info['type'] != 'cdrom':
-            raise InvalidOperation("KCHVMSTOR0006E")
+            old_dev, old_xml = get_disk_xml(dev_info)
+            dev_info.update(params)
+            dev, xml = get_disk_xml(dev_info)
 
-        params['path'] = params.get('path', '')
-        old_disk_path = dev_info['path']
-        new_disk_path = params['path']
-        if new_disk_path != old_disk_path:
-            # An empty path means a CD-ROM was empty or ejected:
-            if old_disk_path is not '':
-                old_disk_used_by = get_disk_used_by(self.conn, old_disk_path)
-            if new_disk_path is not '':
-                new_disk_used_by = get_disk_used_by(self.conn, new_disk_path)
+            # remove
+            self.delete(vm_name, dev_name)
 
-        dev_info.update(params)
-        dev, xml = get_disk_xml(dev_info)
+            try:
+                dom.attachDeviceFlags(xml)
+            except Exception as e:
+                dom.attachDeviceFlags(old_xml)
+                raise OperationFailed("KCHVMSTOR0009E", {'error': e.message})
 
-        try:
-            dom.updateDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
-        except Exception as e:
-            raise OperationFailed("KCHVMSTOR0009E", {'error': e.message})
+        else:
+            dev_info.update(params)
+            dev, xml = get_disk_xml(dev_info)
+
+            params['path'] = params.get('path', '')
+            old_disk_path = dev_info['path']
+            new_disk_path = params['path']
+            if new_disk_path != old_disk_path:
+                # An empty path means a CD-ROM was empty or ejected:
+                if old_disk_path is not '':
+                    old_disk_used_by = get_disk_used_by(self.conn,
+                                                        old_disk_path)
+                if new_disk_path is not '':
+                    new_disk_used_by = get_disk_used_by(self.conn,
+                                                        new_disk_path)
+
+            try:
+                dom.updateDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
+            except Exception as e:
+                raise OperationFailed("KCHVMSTOR0009E", {'error': e.message})
 
         try:
             if old_disk_used_by is not None and \
diff --git a/xmlutils/disk.py b/xmlutils/disk.py
index 76700b9..ff01190 100644
--- a/xmlutils/disk.py
+++ b/xmlutils/disk.py
@@ -51,6 +51,8 @@ def get_disk_xml(params):
     if disk_type is None:
         disk_type = _get_disk_type(path) if len(path) > 0 else 'file'
     disk = E.disk(type=disk_type, device=params['type'])
+
+    # <driver />
     driver = E.driver(name='qemu', type=params['format'])
     try:
         fd = os.open(path, os.O_RDONLY | os.O_DIRECT)
@@ -69,6 +71,14 @@ def get_disk_xml(params):
     if params.get('pool_type') == "netfs":
         driver.set("io", "native")
 
+    # set io
+    if params.get("io") is not None:
+        driver.set("io", params.get("io"))
+
+    # set cache
+    if params.get("cache") is not None:
+        driver.set("cache", params.get("cache"))
+
     disk.append(driver)
 
     # Get device name according to bus and index values
-- 
2.9.3

_______________________________________________
Kimchi-devel mailing list
Kimchi-devel@ovirt.org
http://lists.ovirt.org/mailman/listinfo/kimchi-devel