This patch adds the 'cache' parameter for KVM; currently this is only
customisable at the hypervisor level, so it's the same for all drives
(except any CDROM image, which gets the default).
---
 lib/constants.py         |   12 ++++++++++++
 lib/hypervisor/hv_kvm.py |   11 ++++++++++-
 man/gnt-instance.sgml    |   21 +++++++++++++++++++++
 3 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/lib/constants.py b/lib/constants.py
index 0f035ef..99c669b 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -391,6 +391,7 @@ HV_DEVICE_MODEL = "device_model"
 HV_INIT_SCRIPT = "init_script"
 HV_MIGRATION_PORT = "migration_port"
 HV_USE_LOCALTIME = "use_localtime"
+HV_DISK_CACHE = "disk_cache"
 
 HVS_PARAMETER_TYPES = {
   HV_BOOT_ORDER: VTYPE_STRING,
@@ -417,6 +418,7 @@ HVS_PARAMETER_TYPES = {
   HV_INIT_SCRIPT: VTYPE_STRING,
   HV_MIGRATION_PORT: VTYPE_INT,
   HV_USE_LOCALTIME: VTYPE_BOOL,
+  HV_DISK_CACHE: VTYPE_STRING,
   }
 
 HVS_PARAMETERS = frozenset(HVS_PARAMETER_TYPES.keys())
@@ -491,6 +493,15 @@ HT_DISK_SD = "sd"
 HT_DISK_MTD = "mtd"
 HT_DISK_PFLASH = "pflash"
 
+HT_CACHE_DEFAULT = "default"
+HT_CACHE_NONE = "none"
+HT_CACHE_WTHROUGH = "writethrough"
+HT_CACHE_WBACK = "writeback"
+HT_VALID_CACHE_TYPES = frozenset([HT_CACHE_DEFAULT,
+                                  HT_CACHE_NONE,
+                                  HT_CACHE_WTHROUGH,
+                                  HT_CACHE_WBACK])
+
 HT_HVM_VALID_DISK_TYPES = frozenset([HT_DISK_PARAVIRTUAL, HT_DISK_IOEMU])
 HT_KVM_VALID_DISK_TYPES = frozenset([HT_DISK_PARAVIRTUAL, HT_DISK_IDE,
                                      HT_DISK_SCSI, HT_DISK_SD, HT_DISK_MTD,
@@ -645,6 +656,7 @@ HVC_DEFAULTS = {
     HV_USB_MOUSE: '',
     HV_MIGRATION_PORT: 8102,
     HV_USE_LOCALTIME: False,
+    HV_DISK_CACHE: HT_CACHE_DEFAULT,
     },
   HT_FAKE: {
     },
diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index a153d36..94e5651 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -74,6 +74,8 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       hv_base.ParamInSet(False, constants.HT_KVM_VALID_MOUSE_TYPES),
     constants.HV_MIGRATION_PORT: hv_base.NET_PORT_CHECK,
     constants.HV_USE_LOCALTIME: hv_base.NO_CHECK,
+    constants.HV_DISK_CACHE:
+      hv_base.ParamInSet(True, constants.HT_VALID_CACHE_TYPES),
     }
 
   _MIGRATION_STATUS_RE = re.compile('Migration\s+status:\s+(\w+)',
@@ -310,6 +312,12 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       if_val = ',if=virtio'
     else:
       if_val = ',if=%s' % disk_type
+    # Cache mode
+    disk_cache = hvp[constants.HV_DISK_CACHE]
+    if disk_cache != constants.HT_CACHE_DEFAULT:
+      cache_val = ",cache=%s" % disk_cache
+    else:
+      cache_val = ""
     for cfdev, dev_path in block_devices:
       if cfdev.mode != constants.DISK_RDWR:
         raise errors.HypervisorError("Instance has read-only disks which"
@@ -323,7 +331,8 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       else:
         boot_val = ''
 
-      drive_val = 'file=%s,format=raw%s%s' % (dev_path, if_val, boot_val)
+      drive_val = 'file=%s,format=raw%s%s%s' % (dev_path, if_val, boot_val,
+                                                cache_val)
       kvm_cmd.extend(['-drive', drive_val])
 
     iso_image = hvp[constants.HV_CDROM_IMAGE_PATH]
diff --git a/man/gnt-instance.sgml b/man/gnt-instance.sgml
index 14f6c51..adaec32 100644
--- a/man/gnt-instance.sgml
+++ b/man/gnt-instance.sgml
@@ -521,6 +521,27 @@
               </listitem>
             </varlistentry>
 
+            <varlistentry>
+              <term>disk_cache</term>
+              <listitem>
+                <simpara>Valid for the KVM hypervisor.</simpara>
+
+                <simpara>The disk cache mode. It can be either
+                <userinput>default</userinput> to not pass any cache
+                option to KVM, or one of the KVM cache modes: none
+                (for direct I/O), writethrough (to use the host cache
+                but report completion to the guest only when the host
+                has commited the changes to disk) or writeback (to use
+                the host cache and report completion as soon as the
+                data is in the host cache). Note that there are
+                special considerations for the cache mode depending on
+                version of KVM used and disk type (always raw file
+                under Ganeti), please refer to the KVM documentation
+                for more details.
+                </simpara>
+              </listitem>
+            </varlistentry>
+
           </variablelist>
 
         </para>
-- 
1.6.5.3

Reply via email to