render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
3e5111
From 73114abb7437db2e4625a64377005ddaf8f17840 Mon Sep 17 00:00:00 2001
3e5111
Message-Id: <73114abb7437db2e4625a64377005ddaf8f17840@dist-git>
3e5111
From: John Ferlan <jferlan@redhat.com>
3e5111
Date: Wed, 26 Apr 2017 08:41:10 -0400
3e5111
Subject: [PATCH] storage: Modify storageBackendWipeLocal to allow zero from
3e5111
 end of device
3e5111
3e5111
https://bugzilla.redhat.com/show_bug.cgi?id=1439132
3e5111
3e5111
Add bool 'zero_end' and logic that would allow a caller to wipe specific
3e5111
portions of a target device either from the beginning (the default) or
3e5111
from the end when zero_end is true.
3e5111
3e5111
This will allow for this code to wipe out partition table information
3e5111
from a device.
3e5111
3e5111
(cherry picked from commit 859a2d162ac6dd141539819cfc6157724d12bde6)
3e5111
Signed-off-by: John Ferlan <jferlan@redhat.com>
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
---
3e5111
 src/storage/storage_util.c | 40 +++++++++++++++++++++++++++-------------
3e5111
 1 file changed, 27 insertions(+), 13 deletions(-)
3e5111
3e5111
diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
3e5111
index adec9ab6f..8dbdc770e 100644
3e5111
--- a/src/storage/storage_util.c
3e5111
+++ b/src/storage/storage_util.c
3e5111
@@ -2516,26 +2516,38 @@ static int
3e5111
 storageBackendWipeLocal(const char *path,
3e5111
                         int fd,
3e5111
                         unsigned long long wipe_len,
3e5111
-                        size_t writebuf_length)
3e5111
+                        size_t writebuf_length,
3e5111
+                        bool zero_end)
3e5111
 {
3e5111
     int ret = -1, written = 0;
3e5111
     unsigned long long remaining = 0;
3e5111
+    off_t size;
3e5111
     size_t write_size = 0;
3e5111
     char *writebuf = NULL;
3e5111
 
3e5111
-    VIR_DEBUG("wiping start: 0 len: %llu", wipe_len);
3e5111
-
3e5111
     if (VIR_ALLOC_N(writebuf, writebuf_length) < 0)
3e5111
         goto cleanup;
3e5111
 
3e5111
-    if (lseek(fd, 0, SEEK_SET) < 0) {
3e5111
-        virReportSystemError(errno,
3e5111
-                             _("Failed to seek to the start in volume "
3e5111
-                               "with path '%s'"),
3e5111
-                             path);
3e5111
-        goto cleanup;
3e5111
+    if (!zero_end) {
3e5111
+        if ((size = lseek(fd, 0, SEEK_SET)) < 0) {
3e5111
+            virReportSystemError(errno,
3e5111
+                                 _("Failed to seek to the start in volume "
3e5111
+                                   "with path '%s'"),
3e5111
+                                 path);
3e5111
+            goto cleanup;
3e5111
+        }
3e5111
+    } else {
3e5111
+        if ((size = lseek(fd, -wipe_len, SEEK_END)) < 0) {
3e5111
+            virReportSystemError(errno,
3e5111
+                                 _("Failed to seek to %llu bytes to the end "
3e5111
+                                   "in volume with path '%s'"),
3e5111
+                                 wipe_len, path);
3e5111
+            goto cleanup;
3e5111
+        }
3e5111
     }
3e5111
 
3e5111
+    VIR_DEBUG("wiping start: %zd len: %llu", (ssize_t) size, wipe_len);
3e5111
+
3e5111
     remaining = wipe_len;
3e5111
     while (remaining > 0) {
3e5111
 
3e5111
@@ -2573,7 +2585,8 @@ storageBackendWipeLocal(const char *path,
3e5111
 static int
3e5111
 storageBackendVolWipeLocalFile(const char *path,
3e5111
                                unsigned int algorithm,
3e5111
-                               unsigned long long allocation)
3e5111
+                               unsigned long long allocation,
3e5111
+                               bool zero_end)
3e5111
 {
3e5111
     int ret = -1, fd = -1;
3e5111
     const char *alg_char = NULL;
3e5111
@@ -2648,7 +2661,8 @@ storageBackendVolWipeLocalFile(const char *path,
3e5111
         if (S_ISREG(st.st_mode) && st.st_blocks < (st.st_size / DEV_BSIZE)) {
3e5111
             ret = storageBackendVolZeroSparseFileLocal(path, st.st_size, fd);
3e5111
         } else {
3e5111
-            ret = storageBackendWipeLocal(path, fd, allocation, st.st_blksize);
3e5111
+            ret = storageBackendWipeLocal(path, fd, allocation, st.st_blksize,
3e5111
+                                          zero_end);
3e5111
         }
3e5111
         if (ret < 0)
3e5111
             goto cleanup;
3e5111
@@ -2686,7 +2700,7 @@ storageBackendVolWipePloop(virStorageVolDefPtr vol,
3e5111
         goto cleanup;
3e5111
 
3e5111
     if (storageBackendVolWipeLocalFile(target_path, algorithm,
3e5111
-                                       vol->target.allocation) < 0)
3e5111
+                                       vol->target.allocation, false) < 0)
3e5111
         goto cleanup;
3e5111
 
3e5111
     if (virFileRemove(disk_desc, 0, 0) < 0) {
3e5111
@@ -2735,7 +2749,7 @@ virStorageBackendVolWipeLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
3e5111
         ret = storageBackendVolWipePloop(vol, algorithm);
3e5111
     } else {
3e5111
         ret = storageBackendVolWipeLocalFile(vol->target.path, algorithm,
3e5111
-                                             vol->target.allocation);
3e5111
+                                             vol->target.allocation, false);
3e5111
     }
3e5111
 
3e5111
     return ret;
3e5111
-- 
3e5111
2.12.2
3e5111