Modify 'CreateDisks' function to accept the disks of an instance as an
extra parameter. Since the instance may not have been saved to the
config file yet, 'CreateDisks' can not query the config file for the
instance's disks; so they need to be passed as an argument.

Signed-off-by: Ilias Tsitsimpis <[email protected]>
---
 lib/cmdlib/instance.py         |  2 +-
 lib/cmdlib/instance_storage.py | 20 +++++++++++++++-----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/cmdlib/instance.py b/lib/cmdlib/instance.py
index 04f71e1..95b40e3 100644
--- a/lib/cmdlib/instance.py
+++ b/lib/cmdlib/instance.py
@@ -1442,7 +1442,7 @@ class LUInstanceCreate(LogicalUnit):
     else:
       feedback_fn("* creating instance disks...")
       try:
-        CreateDisks(self, iobj)
+        CreateDisks(self, iobj, instance_disks=iobj.disks)
       except errors.OpExecError:
         self.LogWarning("Device creation failed")
         self.cfg.ReleaseDRBDMinors(self.op.instance_name)
diff --git a/lib/cmdlib/instance_storage.py b/lib/cmdlib/instance_storage.py
index 91372c7..3cf9447 100644
--- a/lib/cmdlib/instance_storage.py
+++ b/lib/cmdlib/instance_storage.py
@@ -202,15 +202,23 @@ def _UndoCreateDisks(lu, disks_created, instance):
                 (disk, lu.cfg.GetNodeName(node_uuid)), logging.warning)
 
 
-def CreateDisks(lu, instance, to_skip=None, target_node_uuid=None, disks=None):
+def CreateDisks(lu, instance, instance_disks=None,
+                to_skip=None, target_node_uuid=None, disks=None):
   """Create all disks for an instance.
 
   This abstracts away some work from AddInstance.
 
+  Since the instance may not have been saved to the config file yet, this
+  function can not query the config file for the instance's disks; in that
+  case they need to be passed as an argument.
+
   @type lu: L{LogicalUnit}
   @param lu: the logical unit on whose behalf we execute
   @type instance: L{objects.Instance}
   @param instance: the instance whose disks we should create
+  @type instance_disks: list of L{objects.Disk}
+  @param instance_disks: the disks that belong to the instance; if not
+      specified, retrieve them from config file
   @type to_skip: list
   @param to_skip: list of indices to skip
   @type target_node_uuid: string
@@ -224,29 +232,31 @@ def CreateDisks(lu, instance, to_skip=None, 
target_node_uuid=None, disks=None):
 
   """
   info = GetInstanceInfoText(instance)
+  if instance_disks is None:
+    instance_disks = instance.disks
   if target_node_uuid is None:
     pnode_uuid = instance.primary_node
     # We cannot use config's 'GetInstanceNodes' here as 'CreateDisks'
     # is used by 'LUInstanceCreate' and the instance object is not
     # stored in the config yet.
     all_node_uuids = []
-    for disk in instance.disks:
+    for disk in instance_disks:
       all_node_uuids.extend(disk.all_nodes)
     all_node_uuids = set(all_node_uuids)
     # ensure that primary node is always the first
-    all_node_uuids.discard(instance.primary_node)
+    all_node_uuids.discard(pnode_uuid)
     all_node_uuids = [pnode_uuid] + list(all_node_uuids)
   else:
     pnode_uuid = target_node_uuid
     all_node_uuids = [pnode_uuid]
 
   if disks is None:
-    disks = instance.disks
+    disks = instance_disks
 
   CheckDiskTemplateEnabled(lu.cfg.GetClusterInfo(), instance.disk_template)
 
   if instance.disk_template in constants.DTS_FILEBASED:
-    file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1])
+    file_storage_dir = os.path.dirname(instance_disks[0].logical_id[1])
     result = lu.rpc.call_file_storage_dir_create(pnode_uuid, file_storage_dir)
 
     result.Raise("Failed to create directory '%s' on"
-- 
1.9.1

Reply via email to