My apologies, I saw the Signed-off-by requirement at first, but as I followed the docs I got to "git publish" and didn't realize "git publish" was actually able to send emails on my system (I don't remember setting up any SMTP settings). By that time, I forgot and thought this patch was short enough to not warrant much of a commit message.
The main practical advantage is for users that call "qemu-img resize" via scripts or other systems (like Packer in my case) that don't check the image size ahead of time. I can upstream this change into them (by using "qemu-img info —output=json ...") but I figured it would be useful to more users here.
This could trivially be added to block/io.c:bdrv_co_truncate(), as well as blockdev.c:qmp_block_resize() with a little more work. I'm not familiar with those workflows, but if needed I can do that as well.
Here's the new patch:
From 17f5c5f03d930c4816b92b97e0e54db0725d7b94 Mon Sep 17 00:00:00 2001
Date: Mon, 20 Jan 2025 01:56:24 -0800
Subject: [PATCH] Skip resizing image to the same size
Higher-level software, such as Packer, blindly call "qemu-img resize"
even when the size is the same. This change can be pushed to those
users, but it's very cheap to check and can save many other users more
time here.
---
qemu-img.c | 6 ++++++
tests/qemu-iotests/061 | 8 ++++++++
tests/qemu-iotests/061.out | 9 +++++++++
3 files changed, 23 insertions(+)
diff --git a/qemu-img.c b/qemu-img.c
index 2f2fac90e8..3345c4e63f 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -4184,6 +4184,12 @@ static int img_resize(int argc, char **argv)
goto out;
}
+ if (total_size == current_size) {
+ qprintf(quiet, "Image already has the desired size.\n");
+ ret = 0;
+ goto out;
+ }
+
/*
* The user expects the image to have the desired size after
* resizing, so pass @exact=true. It is of no use to report
diff --git a/tests/qemu-iotests/061 b/tests/qemu-iotests/061
index b71ac097d1..88aec4ebc6 100755
--- a/tests/qemu-iotests/061
+++ b/tests/qemu-iotests/061
@@ -150,6 +150,14 @@ _qcow2_dump_header | grep '^\(version\|size\|nb_snap\)'
_check_test_img
+echo
+echo "=== Testing resize to same size ==="
+echo
+_make_test_img -o "compat=0.10" 32M
+$QEMU_IMG resize "$TEST_IMG" 32M ||
+ echo "unexpected fail"
+_qcow2_dump_header | grep '^\(version\|size\|nb_snap\)'
+_check_test_img
echo
echo "=== Testing dirty lazy_refcounts=off ==="
diff --git a/tests/qemu-iotests/061.out b/tests/qemu-iotests/061.out
index 24c33add7c..d94eb9d513 100644
--- a/tests/qemu-iotests/061.out
+++ b/tests/qemu-iotests/061.out
@@ -299,6 +299,15 @@ size 33554432
nb_snapshots 1
No errors were found on the image.
+=== Testing resize to same size ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=33554432
+Image already has the desired size.
+version 2
+size 33554432
+nb_snapshots 0
+No errors were found on the image.
+
=== Testing dirty lazy_refcounts=off ===
Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
--
2.43.0