storage: Clean up volume wiping

Let's cleanly differentiate what wiping a volume does for ploop and
other volumes so it's more readable what is done for each one instead of
branching out multiple times in different parts of the same function.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Martin Kletzander 2016-07-14 07:27:02 +02:00
parent 430c4ca771
commit 068fde5fcf

View File

@ -2401,47 +2401,28 @@ virStorageBackendWipeLocal(const char *path,
} }
/* In here just for a clean patch series, will be removed in future patch */ static int
static int virStorageBackendVolWipePloop(virStorageVolDefPtr vol); virStorageBackendVolWipeLocalFile(const char *path,
unsigned int algorithm,
unsigned long long allocation)
int
virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
virStorageVolDefPtr vol,
unsigned int algorithm,
unsigned int flags)
{ {
int ret = -1, fd = -1; int ret = -1, fd = -1;
const char *alg_char = NULL; const char *alg_char = NULL;
struct stat st; struct stat st;
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
char *path = NULL;
char *target_path = vol->target.path;
virCheckFlags(0, -1); fd = open(path, O_RDWR);
VIR_DEBUG("Wiping volume with path '%s' and algorithm %u",
vol->target.path, algorithm);
if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0)
goto cleanup;
target_path = path;
}
fd = open(target_path, O_RDWR);
if (fd == -1) { if (fd == -1) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to open storage volume with path '%s'"), _("Failed to open storage volume with path '%s'"),
vol->target.path); path);
goto cleanup; goto cleanup;
} }
if (fstat(fd, &st) == -1) { if (fstat(fd, &st) == -1) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to stat storage volume with path '%s'"), _("Failed to stat storage volume with path '%s'"),
vol->target.path); path);
goto cleanup; goto cleanup;
} }
@ -2484,10 +2465,11 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
goto cleanup; goto cleanup;
} }
VIR_DEBUG("Wiping file '%s' with algorithm '%s'", path, alg_char);
if (algorithm != VIR_STORAGE_VOL_WIPE_ALG_ZERO) { if (algorithm != VIR_STORAGE_VOL_WIPE_ALG_ZERO) {
cmd = virCommandNew(SCRUB); cmd = virCommandNew(SCRUB);
virCommandAddArgList(cmd, "-f", "-p", alg_char, virCommandAddArgList(cmd, "-f", "-p", alg_char, path, NULL);
target_path, NULL);
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto cleanup;
@ -2499,26 +2481,23 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
} else { } else {
ret = virStorageBackendWipeLocal(path, ret = virStorageBackendWipeLocal(path,
fd, fd,
vol->target.allocation, allocation,
st.st_blksize); st.st_blksize);
} }
if (ret < 0) if (ret < 0)
goto cleanup; goto cleanup;
} }
if (vol->target.format == VIR_STORAGE_FILE_PLOOP)
ret = virStorageBackendVolWipePloop(vol);
cleanup: cleanup:
virCommandFree(cmd); virCommandFree(cmd);
VIR_FREE(path);
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
return ret; return ret;
} }
static int static int
virStorageBackendVolWipePloop(virStorageVolDefPtr vol) virStorageBackendVolWipePloop(virStorageVolDefPtr vol,
unsigned int algorithm)
{ {
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
char *target_path = NULL; char *target_path = NULL;
@ -2540,6 +2519,11 @@ virStorageBackendVolWipePloop(virStorageVolDefPtr vol)
if (virAsprintf(&disk_desc, "%s/DiskDescriptor.xml", vol->target.path) < 0) if (virAsprintf(&disk_desc, "%s/DiskDescriptor.xml", vol->target.path) < 0)
goto cleanup; goto cleanup;
if (virStorageBackendVolWipeLocalFile(target_path,
algorithm,
vol->target.allocation) < 0)
goto cleanup;
if (virFileRemove(disk_desc, 0, 0) < 0) { if (virFileRemove(disk_desc, 0, 0) < 0) {
virReportError(errno, _("Failed to delete DiskDescriptor.xml of volume '%s'"), virReportError(errno, _("Failed to delete DiskDescriptor.xml of volume '%s'"),
vol->target.path); vol->target.path);
@ -2554,7 +2538,7 @@ virStorageBackendVolWipePloop(virStorageVolDefPtr vol)
cmd = virCommandNewArgList(create_tool, "init", "-s", NULL); cmd = virCommandNewArgList(create_tool, "init", "-s", NULL);
virCommandAddArgFormat(cmd, "%lluM", VIR_DIV_UP(vol->target.capacity, virCommandAddArgFormat(cmd, "%lluM", VIR_DIV_UP(vol->target.capacity,
(1024 * 1024))); (1024 * 1024)));
virCommandAddArgList(cmd, "-t", "ext4", NULL); virCommandAddArgList(cmd, "-t", "ext4", NULL);
virCommandAddArg(cmd, target_path); virCommandAddArg(cmd, target_path);
ret = virCommandRun(cmd, NULL); ret = virCommandRun(cmd, NULL);
@ -2568,6 +2552,32 @@ virStorageBackendVolWipePloop(virStorageVolDefPtr vol)
} }
int
virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
virStorageVolDefPtr vol,
unsigned int algorithm,
unsigned int flags)
{
int ret = -1;
virCheckFlags(0, -1);
VIR_DEBUG("Wiping volume with path '%s' and algorithm %u",
vol->target.path, algorithm);
if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
ret = virStorageBackendVolWipePloop(vol, algorithm);
} else {
ret = virStorageBackendVolWipeLocalFile(vol->target.path,
algorithm,
vol->target.allocation);
}
return ret;
}
#ifdef GLUSTER_CLI #ifdef GLUSTER_CLI
int int
virStorageBackendFindGlusterPoolSources(const char *host, virStorageBackendFindGlusterPoolSources(const char *host,