diff --git a/lib/backend.py b/lib/backend.py index 8abe5a676a363a0969aa955e546c8595542dc28e..3df63960cce381e73203dd9fb92ca22f18951793 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 b66b6eb4d1d19b4bd9ef53b43417fa3d0146e29d..b4e9a37ff4b9f723c01e4a93812fe1aa2cf74b79 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"