From nobody Tue Apr 15 03:57:26 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506436543739144.06077910408635; Tue, 26 Sep 2017 07:35:43 -0700 (PDT) Received: from localhost ([::1]:47844 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dwqxO-0004K7-ID for importer@patchew.org; Tue, 26 Sep 2017 10:35:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33213) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dwqks-0001uQ-Dw for qemu-devel@nongnu.org; Tue, 26 Sep 2017 10:22:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dwqko-0005eW-3M for qemu-devel@nongnu.org; Tue, 26 Sep 2017 10:22:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50536) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dwqkb-0005XX-I6; Tue, 26 Sep 2017 10:22:17 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A25A8C047B92; Tue, 26 Sep 2017 14:22:16 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-39.ams2.redhat.com [10.36.117.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4FA436A504; Tue, 26 Sep 2017 14:22:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A25A8C047B92 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=kwolf@redhat.com From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 26 Sep 2017 16:21:29 +0200 Message-Id: <20170926142133.2498-21-kwolf@redhat.com> In-Reply-To: <20170926142133.2498-1-kwolf@redhat.com> References: <20170926142133.2498-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 26 Sep 2017 14:22:16 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 20/24] qemu-img: add --shrink flag for resize X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Pavel Butsykin The flag is additional precaution against data loss. Perhaps in the future = the operation shrink without this flag will be blocked for all formats, but for= now we need to maintain compatibility with raw. Signed-off-by: Pavel Butsykin Reviewed-by: Max Reitz Reviewed-by: John Snow Message-id: 20170918124230.8152-2-pbutsykin@virtuozzo.com [mreitz: Added a missing space to a warning] Signed-off-by: Max Reitz --- qemu-img.c | 23 +++++++++++++++++++++++ qemu-img-cmds.hx | 4 ++-- qemu-img.texi | 6 +++++- tests/qemu-iotests/102 | 4 ++-- tests/qemu-iotests/106 | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index df984b11b9..d6007b2a6d 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -64,6 +64,7 @@ enum { OPTION_TARGET_IMAGE_OPTS =3D 263, OPTION_SIZE =3D 264, OPTION_PREALLOCATION =3D 265, + OPTION_SHRINK =3D 266, }; =20 typedef enum OutputFormat { @@ -3436,6 +3437,7 @@ static int img_resize(int argc, char **argv) }, }; bool image_opts =3D false; + bool shrink =3D false; =20 /* Remove size from argv manually so that negative numbers are not tre= ated * as options by getopt. */ @@ -3454,6 +3456,7 @@ static int img_resize(int argc, char **argv) {"object", required_argument, 0, OPTION_OBJECT}, {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, + {"shrink", no_argument, 0, OPTION_SHRINK}, {0, 0, 0, 0} }; c =3D getopt_long(argc, argv, ":f:hq", @@ -3496,6 +3499,9 @@ static int img_resize(int argc, char **argv) return 1; } break; + case OPTION_SHRINK: + shrink =3D true; + break; } } if (optind !=3D argc - 1) { @@ -3569,6 +3575,23 @@ static int img_resize(int argc, char **argv) goto out; } =20 + if (total_size < current_size && !shrink) { + warn_report("Shrinking an image will delete all data beyond the " + "shrunken image's end. Before performing such an " + "operation, make sure there is no important data there= ."); + + if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") !=3D 0) { + error_report( + "Use the --shrink option to perform a shrink operation."); + ret =3D -1; + goto out; + } else { + warn_report("Using the --shrink option will suppress this mess= age. " + "Note that future versions of qemu-img may refuse = to " + "shrink images without this option."); + } + } + ret =3D blk_truncate(blk, total_size, prealloc, &err); if (!ret) { qprintf(quiet, "Image resized.\n"); diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index b47d409665..2fe31893cf 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -89,9 +89,9 @@ STEXI ETEXI =20 DEF("resize", img_resize, - "resize [--object objectdef] [--image-opts] [-q] filename [+ | -]size") + "resize [--object objectdef] [--image-opts] [-q] [--shrink] filename [= + | -]size") STEXI -@item resize [--object @var{objectdef}] [--image-opts] [-q] @var{filename}= [+ | -]@var{size} +@item resize [--object @var{objectdef}] [--image-opts] [-q] [--shrink] @va= r{filename} [+ | -]@var{size} ETEXI =20 STEXI diff --git a/qemu-img.texi b/qemu-img.texi index 90c7eab4a8..ee5c5940d3 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -545,7 +545,7 @@ qemu-img rebase -b base.img diff.qcow2 At this point, @code{modified.img} can be discarded, since @code{base.img + diff.qcow2} contains the same information. =20 -@item resize [--preallocation=3D@var{prealloc}] @var{filename} [+ | -]@var= {size} +@item resize [--shrink] [--preallocation=3D@var{prealloc}] @var{filename} = [+ | -]@var{size} =20 Change the disk image as if it had been created with @var{size}. =20 @@ -553,6 +553,10 @@ Before using this command to shrink a disk image, you = MUST use file system and partitioning tools inside the VM to reduce allocated file systems and part= ition sizes accordingly. Failure to do so will result in data loss! =20 +When shrinking images, the @code{--shrink} option must be given. This info= rms +qemu-img that the user acknowledges all loss of data beyond the truncated +image's end. + After using this command to grow a disk image, you must use file system and partitioning tools inside the VM to actually begin using the new space on = the device. diff --git a/tests/qemu-iotests/102 b/tests/qemu-iotests/102 index 87db1bb1bf..d7ad8d9840 100755 --- a/tests/qemu-iotests/102 +++ b/tests/qemu-iotests/102 @@ -54,7 +54,7 @@ _make_test_img $IMG_SIZE $QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io # Remove data cluster from image (first cluster: image header, second: ref= table, # third: refblock, fourth: L1 table, fifth: L2 table) -$QEMU_IMG resize -f raw "$TEST_IMG" $((5 * 64 * 1024)) +$QEMU_IMG resize -f raw --shrink "$TEST_IMG" $((5 * 64 * 1024)) =20 $QEMU_IO -c map "$TEST_IMG" $QEMU_IMG map "$TEST_IMG" @@ -69,7 +69,7 @@ $QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io =20 qemu_comm_method=3Dmonitor _launch_qemu -drive if=3Dnone,file=3D"$TEST_IMG= ",id=3Ddrv0 =20 -$QEMU_IMG resize -f raw "$TEST_IMG" $((5 * 64 * 1024)) +$QEMU_IMG resize -f raw --shrink "$TEST_IMG" $((5 * 64 * 1024)) =20 _send_qemu_cmd $QEMU_HANDLE 'qemu-io drv0 map' 'allocated' \ | sed -e 's/^(qemu).*qemu-io drv0 map...$/(qemu) qemu-io drv0 map/' diff --git a/tests/qemu-iotests/106 b/tests/qemu-iotests/106 index 32649578fb..bfe71f4e60 100755 --- a/tests/qemu-iotests/106 +++ b/tests/qemu-iotests/106 @@ -83,7 +83,7 @@ echo '=3D=3D=3D Testing image shrinking =3D=3D=3D' for growth_mode in falloc full off; do echo echo "--- growth_mode=3D$growth_mode ---" - $QEMU_IMG resize -f "$IMGFMT" --preallocation=3D$growth_mode "$TEST_IM= G" -${GROWTH_SIZE}K + $QEMU_IMG resize -f "$IMGFMT" --shrink --preallocation=3D$growth_mode = "$TEST_IMG" -${GROWTH_SIZE}K done =20 # success, all done --=20 2.13.5