Implement dryrun mode for BlockDev.Grow()
authorIustin Pop <iustin@google.com>
Mon, 9 May 2011 14:50:23 +0000 (16:50 +0200)
committerIustin Pop <iustin@google.com>
Tue, 10 May 2011 14:37:46 +0000 (16:37 +0200)
This is always called with False from backend for now.

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

lib/backend.py
lib/bdev.py

index 0c05299..b598af6 100644 (file)
@@ -2245,7 +2245,7 @@ def BlockdevGrow(disk, amount):
     _Fail("Cannot find block device %s", disk)
 
   try:
     _Fail("Cannot find block device %s", disk)
 
   try:
-    r_dev.Grow(amount)
+    r_dev.Grow(amount, False)
   except errors.BlockDeviceError, err:
     _Fail("Failed to grow block device: %s", err, exc=True)
 
   except errors.BlockDeviceError, err:
     _Fail("Failed to grow block device: %s", err, exc=True)
 
index 2430b1d..10dba32 100644 (file)
@@ -331,10 +331,14 @@ class BlockDev(object):
     for child in self._children:
       child.SetInfo(text)
 
     for child in self._children:
       child.SetInfo(text)
 
-  def Grow(self, amount):
+  def Grow(self, amount, dryrun):
     """Grow the block device.
 
     """Grow the block device.
 
+    @type amount: integer
     @param amount: the amount (in mebibytes) to grow with
     @param amount: the amount (in mebibytes) to grow with
+    @type dryrun: boolean
+    @param dryrun: whether to execute the operation in simulation mode
+        only, without actually increasing the size
 
     """
     raise NotImplementedError
 
     """
     raise NotImplementedError
@@ -753,7 +757,7 @@ class LogicalVolume(BlockDev):
       _ThrowError("Command: %s error: %s - %s", result.cmd, result.fail_reason,
                   result.output)
 
       _ThrowError("Command: %s error: %s - %s", result.cmd, result.fail_reason,
                   result.output)
 
-  def Grow(self, amount):
+  def Grow(self, amount, dryrun):
     """Grow the logical volume.
 
     """
     """Grow the logical volume.
 
     """
@@ -764,13 +768,15 @@ class LogicalVolume(BlockDev):
     rest = amount % full_stripe_size
     if rest != 0:
       amount += full_stripe_size - rest
     rest = amount % full_stripe_size
     if rest != 0:
       amount += full_stripe_size - rest
+    cmd = ["lvextend", "-L", "+%dm" % amount]
+    if dryrun:
+      cmd.append("--test")
     # we try multiple algorithms since the 'best' ones might not have
     # space available in the right place, but later ones might (since
     # they have less constraints); also note that only recent LVM
     # supports 'cling'
     for alloc_policy in "contiguous", "cling", "normal":
     # we try multiple algorithms since the 'best' ones might not have
     # space available in the right place, but later ones might (since
     # they have less constraints); also note that only recent LVM
     # supports 'cling'
     for alloc_policy in "contiguous", "cling", "normal":
-      result = utils.RunCmd(["lvextend", "--alloc", alloc_policy,
-                             "-L", "+%dm" % amount, self.dev_path])
+      result = utils.RunCmd(cmd + ["--alloc", alloc_policy, self.dev_path])
       if not result.failed:
         return
     _ThrowError("Can't grow LV %s: %s", self.dev_path, result.output)
       if not result.failed:
         return
     _ThrowError("Can't grow LV %s: %s", self.dev_path, result.output)
@@ -1911,7 +1917,7 @@ class DRBD8(BaseDRBD):
     cls._InitMeta(aminor, meta.dev_path)
     return cls(unique_id, children, size)
 
     cls._InitMeta(aminor, meta.dev_path)
     return cls(unique_id, children, size)
 
-  def Grow(self, amount):
+  def Grow(self, amount, dryrun):
     """Resize the DRBD device and its backing storage.
 
     """
     """Resize the DRBD device and its backing storage.
 
     """
@@ -1919,7 +1925,10 @@ class DRBD8(BaseDRBD):
       _ThrowError("drbd%d: Grow called while not attached", self._aminor)
     if len(self._children) != 2 or None in self._children:
       _ThrowError("drbd%d: cannot grow diskless device", self.minor)
       _ThrowError("drbd%d: Grow called while not attached", self._aminor)
     if len(self._children) != 2 or None in self._children:
       _ThrowError("drbd%d: cannot grow diskless device", self.minor)
-    self._children[0].Grow(amount)
+    self._children[0].Grow(amount, dryrun)
+    if dryrun:
+      # DRBD does not support dry-run mode, so we'll return here
+      return
     result = utils.RunCmd(["drbdsetup", self.dev_path, "resize", "-s",
                            "%dm" % (self.size + amount)])
     if result.failed:
     result = utils.RunCmd(["drbdsetup", self.dev_path, "resize", "-s",
                            "%dm" % (self.size + amount)])
     if result.failed:
@@ -2001,7 +2010,7 @@ class FileStorage(BlockDev):
     # TODO: implement rename for file-based storage
     _ThrowError("Rename is not supported for file-based storage")
 
     # TODO: implement rename for file-based storage
     _ThrowError("Rename is not supported for file-based storage")
 
-  def Grow(self, amount):
+  def Grow(self, amount, dryrun):
     """Grow the file
 
     @param amount: the amount (in mebibytes) to grow with
     """Grow the file
 
     @param amount: the amount (in mebibytes) to grow with
@@ -2012,6 +2021,9 @@ class FileStorage(BlockDev):
     current_size = self.GetActualSize()
     new_size = current_size + amount * 1024 * 1024
     assert new_size > current_size, "Cannot Grow with a negative amount"
     current_size = self.GetActualSize()
     new_size = current_size + amount * 1024 * 1024
     assert new_size > current_size, "Cannot Grow with a negative amount"
+    # We can't really simulate the growth
+    if dryrun:
+      return
     try:
       f = open(self.dev_path, "a+")
       f.truncate(new_size)
     try:
       f = open(self.dev_path, "a+")
       f.truncate(new_size)
@@ -2173,7 +2185,7 @@ class PersistentBlockDevice(BlockDev):
     """
     pass
 
     """
     pass
 
-  def Grow(self, amount):
+  def Grow(self, amount, dryrun):
     """Grow the logical volume.
 
     """
     """Grow the logical volume.
 
     """