yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
26ba25
From a4029c787c5741de154d9d1ede7e70c7deaba416 Mon Sep 17 00:00:00 2001
26ba25
From: Max Reitz <mreitz@redhat.com>
26ba25
Date: Wed, 16 May 2018 12:00:18 +0200
26ba25
Subject: [PATCH 003/268] qemu-img: Check post-truncation size
26ba25
26ba25
RH-Author: Max Reitz <mreitz@redhat.com>
26ba25
Message-id: <20180516120018.27565-2-mreitz@redhat.com>
26ba25
Patchwork-id: 80281
26ba25
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 1/1] qemu-img: Check post-truncation size
26ba25
Bugzilla: 1523065
26ba25
RH-Acked-by: John Snow <jsnow@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
26ba25
26ba25
Some block drivers (iscsi and file-posix when dealing with device files)
26ba25
do not actually support truncation, even though they provide a
26ba25
.bdrv_truncate() method and will happily return success when providing a
26ba25
new size that does not exceed the current size.  This is because these
26ba25
drivers expect the user to resize the image outside of qemu and then
26ba25
provide qemu with that information through the block_resize command
26ba25
(compare cb1b83e740384b4e0d950f3d7c81c02b8ce86c2e).
26ba25
26ba25
Of course, anyone using qemu-img resize will find that behavior useless.
26ba25
So we should check the actual size of the image after the supposedly
26ba25
successful truncation took place, emit an error if nothing changed and
26ba25
emit a warning if the target size was not met.
26ba25
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
Message-id: 20180421163957.29872-1-mreitz@redhat.com
26ba25
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit 5279b30392da7a3248b320c75f20c61e3a95863c)
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 qemu-img.c | 39 +++++++++++++++++++++++++++++++++++----
26ba25
 1 file changed, 35 insertions(+), 4 deletions(-)
26ba25
26ba25
diff --git a/qemu-img.c b/qemu-img.c
26ba25
index 855fa52..8320887 100644
26ba25
--- a/qemu-img.c
26ba25
+++ b/qemu-img.c
26ba25
@@ -3381,7 +3381,7 @@ static int img_resize(int argc, char **argv)
26ba25
     Error *err = NULL;
26ba25
     int c, ret, relative;
26ba25
     const char *filename, *fmt, *size;
26ba25
-    int64_t n, total_size, current_size;
26ba25
+    int64_t n, total_size, current_size, new_size;
26ba25
     bool quiet = false;
26ba25
     BlockBackend *blk = NULL;
26ba25
     PreallocMode prealloc = PREALLOC_MODE_OFF;
26ba25
@@ -3557,11 +3557,42 @@ static int img_resize(int argc, char **argv)
26ba25
     }
26ba25
 
26ba25
     ret = blk_truncate(blk, total_size, prealloc, &err;;
26ba25
-    if (!ret) {
26ba25
-        qprintf(quiet, "Image resized.\n");
26ba25
-    } else {
26ba25
+    if (ret < 0) {
26ba25
         error_report_err(err);
26ba25
+        goto out;
26ba25
+    }
26ba25
+
26ba25
+    new_size = blk_getlength(blk);
26ba25
+    if (new_size < 0) {
26ba25
+        error_report("Failed to verify truncated image length: %s",
26ba25
+                     strerror(-new_size));
26ba25
+        ret = -1;
26ba25
+        goto out;
26ba25
     }
26ba25
+
26ba25
+    /* Some block drivers implement a truncation method, but only so
26ba25
+     * the user can cause qemu to refresh the image's size from disk.
26ba25
+     * The idea is that the user resizes the image outside of qemu and
26ba25
+     * then invokes block_resize to inform qemu about it.
26ba25
+     * (This includes iscsi and file-posix for device files.)
26ba25
+     * Of course, that is not the behavior someone invoking
26ba25
+     * qemu-img resize would find useful, so we catch that behavior
26ba25
+     * here and tell the user. */
26ba25
+    if (new_size != total_size && new_size == current_size) {
26ba25
+        error_report("Image was not resized; resizing may not be supported "
26ba25
+                     "for this image");
26ba25
+        ret = -1;
26ba25
+        goto out;
26ba25
+    }
26ba25
+
26ba25
+    if (new_size != total_size) {
26ba25
+        warn_report("Image should have been resized to %" PRIi64
26ba25
+                    " bytes, but was resized to %" PRIi64 " bytes",
26ba25
+                    total_size, new_size);
26ba25
+    }
26ba25
+
26ba25
+    qprintf(quiet, "Image resized.\n");
26ba25
+
26ba25
 out:
26ba25
     blk_unref(blk);
26ba25
     if (ret) {
26ba25
-- 
26ba25
1.8.3.1
26ba25