Add disk cache control parameter for KVM
authorIustin Pop <iustin@google.com>
Wed, 25 Nov 2009 10:04:56 +0000 (11:04 +0100)
committerGuido Trotter <ultrotter@google.com>
Thu, 10 Dec 2009 14:45:50 +0000 (14:45 +0000)
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).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>

lib/constants.py
lib/hypervisor/hv_kvm.py
man/gnt-instance.sgml

index 54a8a01..e8f8ae7 100644 (file)
@@ -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,
@@ -646,6 +657,7 @@ HVC_DEFAULTS = {
     HV_USB_MOUSE: '',
     HV_MIGRATION_PORT: 8102,
     HV_USE_LOCALTIME: False,
+    HV_DISK_CACHE: HT_CACHE_DEFAULT,
     },
   HT_FAKE: {
     },
index a478a53..7caae96 100644 (file)
@@ -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+)',
@@ -312,6 +314,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"
@@ -325,7 +333,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]
index 81d75b2..ea92bf1 100644 (file)
               </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>