From da63bb4e15cfbbc06b8b19df56d714f8f04f94f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Nussbaumer?= <rn@google.com>
Date: Mon, 25 Oct 2010 16:30:34 +0200
Subject: [PATCH] Second iteration over backend.BlockdevWipe
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch now uses dd entirely to wipe the disk, make it
much easier to wipe in blocks so we can give interactive feedback
about the status.

Signed-off-by: RenΓ© Nussbaumer <rn@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/backend.py   | 30 ++++++++++++++++++++++++------
 lib/constants.py |  3 ++-
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index 8abe5a676..3df63960c 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -1287,32 +1287,50 @@ def BlockdevCreate(disk, size, owner, on_primary, info):
   return device.unique_id
 
 
-def _WipeDevice(path):
+def _WipeDevice(path, offset, size):
   """This function actually wipes the device.
 
   @param path: The path to the device to wipe
+  @param offset: The offset in MiB in the file
+  @param size: The size in MiB to write
 
   """
-  result = utils.RunCmd("%s%s" % (constants.WIPE_CMD, utils.ShellQuote(path)))
+  cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
+         "bs=%d" % constants.WIPE_BLOCK_SIZE, "oflag=direct", "of=%s" % path,
+         "count=%d" % size]
+  result = utils.RunCmd(cmd)
 
   if result.failed:
     _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
           result.fail_reason, result.output)
 
 
-def BlockdevWipe(disk):
+def BlockdevWipe(disk, offset, size):
   """Wipes a block device.
 
   @type disk: L{objects.Disk}
   @param disk: the disk object we want to wipe
+  @type offset: int
+  @param offset: The offset in MiB in the file
+  @type size: int
+  @param size: The size in MiB to write
 
   """
   try:
     rdev = _RecursiveFindBD(disk)
-  except errors.BlockDeviceError, err:
-    _Fail("Cannot execute wipe for device %s: device not found", err)
+  except errors.BlockDeviceError:
+    rdev = None
+
+  if not rdev:
+    _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
+
+  # Do cross verify some of the parameters
+  if offset > rdev.size:
+    _Fail("Offset is bigger than device size")
+  if (offset + size) > rdev.size:
+    _Fail("The provided offset and size to wipe is bigger than device size")
 
-  _WipeDevice(rdev.dev_path)
+  _WipeDevice(rdev.dev_path, offset, size)
 
 
 def BlockdevRemove(disk):
diff --git a/lib/constants.py b/lib/constants.py
index b66b6eb4d..b4e9a37ff 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -96,7 +96,8 @@ CONFD_GROUP = _autoconf.CONFD_GROUP
 NODED_USER = _autoconf.NODED_USER
 
 # Wipe
-WIPE_CMD = _autoconf.WIPE_CMD
+DD_CMD = "dd"
+WIPE_BLOCK_SIZE = 1024**2
 
 # file paths
 DATA_DIR = _autoconf.LOCALSTATEDIR + "/lib/ganeti"
-- 
GitLab