7f1c5b
From 2f5369f0effaa23be746f9b5d9f6a0bfc346fb7d Mon Sep 17 00:00:00 2001
7f1c5b
From: Kevin Wolf <kwolf@redhat.com>
7f1c5b
Date: Thu, 12 Jan 2023 20:14:52 +0100
7f1c5b
Subject: [PATCH 02/20] qemu-img commit: Report errors while closing the image
7f1c5b
7f1c5b
RH-Author: Kevin Wolf <kwolf@redhat.com>
7f1c5b
RH-MergeRequest: 143: qemu-img: Fix exit code for errors closing the image
7f1c5b
RH-Bugzilla: 2150180
7f1c5b
RH-Acked-by: Thomas Huth <thuth@redhat.com>
7f1c5b
RH-Acked-by: Hanna Czenczek <hreitz@redhat.com>
7f1c5b
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
7f1c5b
RH-Commit: [2/4] faedd43355463b1210a3f21ecd430f478bd06f5a (kmwolf/centos-qemu-kvm)
7f1c5b
7f1c5b
blk_unref() can't report any errors that happen while closing the image.
7f1c5b
For example, if qcow2 hits an -ENOSPC error while writing out dirty
7f1c5b
bitmaps when it's closed, it prints error messages to stderr, but
7f1c5b
'qemu-img commit' won't see any error return value and will therefore
7f1c5b
look successful with exit code 0.
7f1c5b
7f1c5b
In order to fix this, manually inactivate the image first before calling
7f1c5b
blk_unref(). This already performs the operations that would be most
7f1c5b
likely to fail while closing the image, but it can still return errors.
7f1c5b
7f1c5b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7f1c5b
Message-Id: <20230112191454.169353-3-kwolf@redhat.com>
7f1c5b
Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
7f1c5b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7f1c5b
(cherry picked from commit 44efba2d713aca076c411594d0c1a2b99155eeb3)
7f1c5b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7f1c5b
---
7f1c5b
 qemu-img.c | 13 +++++++++++++
7f1c5b
 1 file changed, 13 insertions(+)
7f1c5b
7f1c5b
diff --git a/qemu-img.c b/qemu-img.c
7f1c5b
index a9b3a8103c..3cbdda9f76 100644
7f1c5b
--- a/qemu-img.c
7f1c5b
+++ b/qemu-img.c
7f1c5b
@@ -449,6 +449,11 @@ static BlockBackend *img_open(bool image_opts,
7f1c5b
         blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
7f1c5b
                             force_share);
7f1c5b
     }
7f1c5b
+
7f1c5b
+    if (blk) {
7f1c5b
+        blk_set_force_allow_inactivate(blk);
7f1c5b
+    }
7f1c5b
+
7f1c5b
     return blk;
7f1c5b
 }
7f1c5b
 
7f1c5b
@@ -1119,6 +1124,14 @@ unref_backing:
7f1c5b
 done:
7f1c5b
     qemu_progress_end();
7f1c5b
 
7f1c5b
+    /*
7f1c5b
+     * Manually inactivate the image first because this way we can know whether
7f1c5b
+     * an error occurred. blk_unref() doesn't tell us about failures.
7f1c5b
+     */
7f1c5b
+    ret = bdrv_inactivate_all();
7f1c5b
+    if (ret < 0 && !local_err) {
7f1c5b
+        error_setg_errno(&local_err, -ret, "Error while closing the image");
7f1c5b
+    }
7f1c5b
     blk_unref(blk);
7f1c5b
 
7f1c5b
     if (local_err) {
7f1c5b
-- 
7f1c5b
2.31.1
7f1c5b