From 9c6f0b515bb1b5da0490c64547bbf1913e35ce3a Mon Sep 17 00:00:00 2001
From: Helga Velroyen <helgav@google.com>
Date: Thu, 21 Mar 2013 13:06:01 +0100
Subject: [PATCH] Check if instances use disk templates that get disabled

When disk templates get disabled via 'gnt-cluster modify', so far
we did not check if any instances still use a disk template of that
type. This patch adds the necessariy check.
It also includes adding a map of disk templates to storage types
and reordering of storage types and disk templates in alphabetical order.

Signed-off-by: Helga Velroyen <helgav@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/cmdlib.py | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 0bc1ace62..0e6fb066b 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -4408,9 +4408,7 @@ class LUClusterSetParams(LogicalUnit):
           hv_class.CheckParameterSyntax(hv_params)
           _CheckHVParams(self, node_list, hv_name, hv_params)
 
-    # FIXME: Regarding enabled_storage_types: If a method is removed
-    # which is actually currently used by an instance, should removing
-    # it be prevented?
+    self._CheckDiskTypeConsistency()
 
     if self.op.os_hvp:
       # no need to check any newly-enabled hypervisors, since the
@@ -4434,6 +4432,26 @@ class LUClusterSetParams(LogicalUnit):
                                    " specified" % self.op.default_iallocator,
                                    errors.ECODE_INVAL)
 
+  def _CheckDiskTypeConsistency(self):
+    """Check whether the storage types that are going to be disabled
+       are still in use by some instances.
+
+    """
+    cluster = self.cfg.GetClusterInfo()
+    instances = self.cfg.GetAllInstancesInfo()
+
+    storage_types_to_remove = set(cluster.enabled_storage_types) \
+      - set(self.op.enabled_storage_types)
+    for instance in instances.itervalues():
+      storage_type = constants.DISK_TEMPLATES_STORAGE_TYPE[
+                       instance.disk_template]
+      if storage_type in storage_types_to_remove:
+        raise errors.OpPrereqError("Cannot disable storage type '%s',"
+                                   " because instance '%s' is using disk"
+                                   " template '%s'." %
+                                   (storage_type, instance.name,
+                                    instance.disk_template))
+
   def Exec(self, feedback_fn):
     """Change the parameters of the cluster.
 
-- 
GitLab