]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
storage: dir: adapts .wipeVol for ploop volumes
authorOlga Krishtal <okrishtal@virtuozzo.com>
Mon, 27 Jun 2016 15:03:38 +0000 (18:03 +0300)
committerJán Tomko <jtomko@redhat.com>
Tue, 12 Jul 2016 11:16:13 +0000 (13:16 +0200)
The modification of .volWipe callback wipes ploop volume using one of
given wiping algorithm: dod, nnsa, etc.
However, in case of ploop volume we need to reinitialize root.hds and DiskDescriptor.xml.

v2:
- added check on ploop tools presens
- virCommandAddArgFormat changed to virCommandAddArg

Signed-off-by: Olga Krishtal <okrishtal@virtuozzo.com>
src/storage/storage_backend.c

index 97f6ffecd6bdb2ec01543027592d38f21235c5bc..4b0b19c45ca5326a4688a052bc5cc2712e38b33b 100644 (file)
@@ -2195,6 +2195,55 @@ virStorageBackendWipeLocal(virStorageVolDefPtr vol,
     return ret;
 }
 
+static int
+virStorageBackendVolWipePloop(virStorageVolDefPtr vol)
+{
+    virCommandPtr cmd = NULL;
+    char *target_path = NULL;
+    char *disk_desc = NULL;
+    char *create_tool = NULL;
+
+    int ret = -1;
+
+    create_tool = virFindFileInPath("ploop");
+    if (!create_tool) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("unable to find ploop tools, please install them"));
+        return -1;
+    }
+
+    if (virAsprintf(&target_path, "%s/root.hds", vol->target.path) < 0)
+        goto cleanup;
+
+    if (virAsprintf(&disk_desc, "%s/DiskDescriptor.xml", vol->target.path) < 0)
+        goto cleanup;
+
+    if (virFileRemove(disk_desc, 0, 0) < 0) {
+        virReportError(errno, _("Failed to delete DiskDescriptor.xml of volume '%s'"),
+                       vol->target.path);
+        goto cleanup;
+    }
+    if (virFileRemove(target_path, 0, 0) < 0) {
+        virReportError(errno, _("failed to delete root.hds of volume '%s'"),
+                       vol->target.path);
+        goto cleanup;
+    }
+
+    cmd = virCommandNewArgList(create_tool, "init", "-s", NULL);
+
+    virCommandAddArgFormat(cmd, "%lluM", VIR_DIV_UP(vol->target.capacity,
+                                                        (1024 * 1024)));
+    virCommandAddArgList(cmd, "-t", "ext4", NULL);
+    virCommandAddArg(cmd, target_path);
+    ret = virCommandRun(cmd, NULL);
+
+ cleanup:
+    VIR_FREE(disk_desc);
+    VIR_FREE(target_path);
+    VIR_FREE(create_tool);
+    virCommandFree(cmd);
+    return ret;
+}
 
 int
 virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
@@ -2207,6 +2256,8 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
     const char *alg_char = NULL;
     struct stat st;
     virCommandPtr cmd = NULL;
+    char *path = NULL;
+    char *target_path = vol->target.path;
 
     virCheckFlags(0, -1);
 
@@ -2214,12 +2265,12 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
               vol->target.path, algorithm);
 
     if (vol->target.format == VIR_STORAGE_FILE_PLOOP) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("wiping for ploop volumes is not supported"));
-        goto cleanup;
+        if (virAsprintf(&path, "%s/root.hds", vol->target.path) < 0)
+            goto cleanup;
+        target_path = path;
     }
 
-    fd = open(vol->target.path, O_RDWR);
+    fd = open(target_path, O_RDWR);
     if (fd == -1) {
         virReportSystemError(errno,
                              _("Failed to open storage volume with path '%s'"),
@@ -2276,13 +2327,12 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
     if (algorithm != VIR_STORAGE_VOL_WIPE_ALG_ZERO) {
         cmd = virCommandNew(SCRUB);
         virCommandAddArgList(cmd, "-f", "-p", alg_char,
-                             vol->target.path, NULL);
+                             target_path, NULL);
 
         if (virCommandRun(cmd, NULL) < 0)
             goto cleanup;
 
         ret = 0;
-        goto cleanup;
     } else {
         if (S_ISREG(st.st_mode) && st.st_blocks < (st.st_size / DEV_BSIZE)) {
             ret = virStorageBackendVolZeroSparseFileLocal(vol, st.st_size, fd);
@@ -2292,10 +2342,16 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
                                              vol->target.allocation,
                                              st.st_blksize);
         }
+        if (ret < 0)
+            goto cleanup;
     }
 
+    if (vol->target.format == VIR_STORAGE_FILE_PLOOP)
+        ret = virStorageBackendVolWipePloop(vol);
+
  cleanup:
     virCommandFree(cmd);
+    VIR_FREE(path);
     VIR_FORCE_CLOSE(fd);
     return ret;
 }