[libvirt] [PATCH] logical: Need to overwrite/clear more than just first 512 bytes

John Ferlan posted 1 patch 7 years, 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20170324174808.4920-1-jferlan@redhat.com
src/storage/storage_backend_logical.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
[libvirt] [PATCH] logical: Need to overwrite/clear more than just first 512 bytes
Posted by John Ferlan 7 years, 1 month ago
https://bugzilla.redhat.com/show_bug.cgi?id=1430679

As it turns out some file headers (e.g. ext4) may be larger/longer than
the 512 bytes of zeros being written prior to a pvcreate, so let's write
out 2048 bytes similar to how the pvcreate sources would peek at the first
4 sectors of the device.

Make sure there is at enough bytes on the device to clear before doing
doing the clear - just to be sure.

Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/storage/storage_backend_logical.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index 29d63b1..9ca6fd4 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -93,7 +93,8 @@ static int
 virStorageBackendLogicalInitializeDevice(const char *path)
 {
     int fd = -1;
-    char zeros[PV_BLANK_SECTOR_SIZE] = {0};
+    char zeros[4 * PV_BLANK_SECTOR_SIZE] = {0};
+    off_t size;
     int ret = -1;
     virCommandPtr pvcmd = NULL;
 
@@ -107,6 +108,25 @@ virStorageBackendLogicalInitializeDevice(const char *path)
         return -1;
     }
 
+    if ((size = lseek(fd, 0, SEEK_END)) == (off_t)-1) {
+        virReportSystemError(errno,
+                             _("failed to seek to end of %s"), path);
+        goto cleanup;
+    }
+
+    if (size < 4 * PV_BLANK_SECTOR_SIZE) {
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
+                       _("cannot initialize '%s' detected size='%lu' less "
+                         "than minimum required='%d"),
+                         path, size, 4 * PV_BLANK_SECTOR_SIZE);
+        goto cleanup;
+    }
+    if ((size = lseek(fd, 0, SEEK_SET)) == (off_t)-1) {
+        virReportSystemError(errno,
+                             _("failed to seek to start of %s"), path);
+        goto cleanup;
+    }
+
     if (safewrite(fd, zeros, sizeof(zeros)) < 0) {
         virReportSystemError(errno, _("cannot clear device header of '%s'"),
                              path);
-- 
2.9.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] logical: Need to overwrite/clear more than just first 512 bytes
Posted by Michal Privoznik 7 years, 1 month ago
On 24.03.2017 18:48, John Ferlan wrote:
> https://bugzilla.redhat.com/show_bug.cgi?id=1430679
>
> As it turns out some file headers (e.g. ext4) may be larger/longer than
> the 512 bytes of zeros being written prior to a pvcreate, so let's write
> out 2048 bytes similar to how the pvcreate sources would peek at the first
> 4 sectors of the device.
>
> Make sure there is at enough bytes on the device to clear before doing
> doing the clear - just to be sure.
>
> Signed-off-by: John Ferlan <jferlan@redhat.com>
> ---
>  src/storage/storage_backend_logical.c | 22 +++++++++++++++++++++-
>  1 file changed, 21 insertions(+), 1 deletion(-)
>

ACK

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list