From 6c6265186c60c79b243b170fa7bdeddb47d1df32 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 19 Jan 2009 11:10:01 +0000
Subject: [PATCH] Block device creation cleanup

Currently when creation LVM-based instances, we always get the
extremely-confusing message "ERROR Can't find LV /dev/xenvg/..." which
is actually expected. This behaviour was introduced before we had
UUID-style LV names, since at that point it was not a unexpected to have
such volumes laying around after a failed creation.

Today, it's much more of an error to see existing volumes, and it's
better to abort with a failure. Since bdev.LogicalVolume.Create() method
will raise an error in case it exists, we can remove this check in
backend before creating the device.

The Create methods for DRBD and FileStorage currently don't raise
exception, as behaviour is not very well defined here.

We also change some exception types raised in bdev so that all
exceptions raised by device creation are a subclass of GenericError.

Reviewed-by: ultrotter
---
 lib/backend.py | 14 ++------------
 lib/bdev.py    | 14 ++++++++------
 2 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index 75c4bdbdf..c9e9af5ba 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -1016,19 +1016,9 @@ def CreateBlockDevice(disk, size, owner, on_primary, info):
         # be assembled
         crdev.Open()
       clist.append(crdev)
-  try:
-    device = bdev.FindDevice(disk.dev_type, disk.physical_id, clist)
-    if device is not None:
-      logging.info("removing existing device %s", disk)
-      device.Remove()
-  except errors.BlockDeviceError, err:
-    pass
 
-  device = bdev.Create(disk.dev_type, disk.physical_id,
-                       clist, size)
-  if device is None:
-    raise ValueError("Can't create child device for %s, %s" %
-                     (disk, size))
+  device = bdev.Create(disk.dev_type, disk.physical_id, clist, size)
+
   if on_primary or disk.AssembleOnSecondary():
     if not device.Assemble():
       errorstring = "Can't assemble device after creation"
diff --git a/lib/bdev.py b/lib/bdev.py
index 282464830..f1ade2eaa 100644
--- a/lib/bdev.py
+++ b/lib/bdev.py
@@ -275,7 +275,8 @@ class LogicalVolume(BlockDev):
 
     """
     if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2:
-      raise ValueError("Invalid configuration data %s" % str(unique_id))
+      raise errors.ProgrammerError("Invalid configuration data %s" %
+                                   str(unique_id))
     vg_name, lv_name = unique_id
     pvs_info = cls.GetPVInfo(vg_name)
     if not pvs_info:
@@ -295,8 +296,8 @@ class LogicalVolume(BlockDev):
     result = utils.RunCmd(["lvcreate", "-L%dm" % size, "-n%s" % lv_name,
                            vg_name] + pvlist)
     if result.failed:
-      raise errors.BlockDeviceError("%s - %s" % (result.fail_reason,
-                                                result.output))
+      raise errors.BlockDeviceError("LV create failed (%s): %s" %
+                                    (result.fail_reason, result.output))
     return LogicalVolume(unique_id, children)
 
   @staticmethod
@@ -1646,16 +1647,17 @@ class FileStorage(BlockDev):
     @return: an instance of FileStorage
 
     """
+    # TODO: decide whether we should check for existing files and
+    # abort or not
     if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2:
       raise ValueError("Invalid configuration data %s" % str(unique_id))
     dev_path = unique_id[1]
     try:
       f = open(dev_path, 'w')
-    except IOError, err:
-      raise errors.BlockDeviceError("Could not create '%'" % err)
-    else:
       f.truncate(size * 1024 * 1024)
       f.close()
+    except IOError, err:
+      raise errors.BlockDeviceError("Error in file creation: %" % str(err))
 
     return FileStorage(unique_id, children)
 
-- 
GitLab