From patchwork Thu Aug 17 09:15:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9905371 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 72BE660244 for ; Thu, 17 Aug 2017 09:17:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6DFBE28AD4 for ; Thu, 17 Aug 2017 09:17:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6304228AD6; Thu, 17 Aug 2017 09:17:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CA6AC28AD4 for ; Thu, 17 Aug 2017 09:17:44 +0000 (UTC) Received: from localhost ([::1]:40478 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1diGvw-0000Pn-27 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 17 Aug 2017 05:17:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1diGuY-0000Dc-Ua for qemu-devel@nongnu.org; Thu, 17 Aug 2017 05:16:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1diGuX-0004VR-Nj for qemu-devel@nongnu.org; Thu, 17 Aug 2017 05:16:18 -0400 Received: from mail-db5eur01on0099.outbound.protection.outlook.com ([104.47.2.99]:34144 helo=EUR01-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1diGuT-0004EF-7f; Thu, 17 Aug 2017 05:16:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=QR7A2QvXZ6BmEeC+98vt1CfEjIOx6RorGZlCVIZVGOA=; b=dist2UX08CKGjZYgP7ZBeS9uccQrE0SJnSXTScv1XEUj/kSLw+Mbk3ioJ0pnOeVnfyAJTYTJVTxrgTQhoc0a82zQE7mAVRavlBL7Dek5VgXHwRjp8qEF6cej0+CMtSh75g126emtrhOYG9OGgtRFSNZ425qfLt8lH7oSN1GbqKE= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.6) by HE1PR0802MB2556.eurprd08.prod.outlook.com (2603:10a6:3:e1::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1341.21; Thu, 17 Aug 2017 09:16:09 +0000 From: Pavel Butsykin To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Thu, 17 Aug 2017 12:15:39 +0300 Message-Id: <20170817091542.9403-2-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170817091542.9403-1-pbutsykin@virtuozzo.com> References: <20170817091542.9403-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: AM5PR0602CA0016.eurprd06.prod.outlook.com (2603:10a6:203:a3::26) To HE1PR0802MB2556.eurprd08.prod.outlook.com (2603:10a6:3:e1::21) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 51a28c22-f1e0-43c4-001a-08d4e55099b8 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:HE1PR0802MB2556; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2556; 3:X0BU8UQdSfA+CYl33n1caGwDatqOqeSMZVQ7ZIDtE9JyGioxXyhDp8iiLQsZxTsquJTAoSIeLo2NREV9c8+ojt/Nuf8A9bzy1xY7WGcsgXTRi9wDfljQsJDoZP3VRk72PjBixL43ScMFr/AfNn2QRamyRY9CsZTMDCNAn3PKS2mKHKhzEZv/35ig2b3UEHebcAdiPnwWPMiHn3UAgj0tE2j0GtcOrkmck8gQ/jZ2CR4RDHTMS+5MeKe4kQoUnGY6; 25:FUhnmgIdVcxvlcpBaA7ZPM4VhM+BL3ixvipMPIqVq6/2U8vFezIBnaNbYYjhPQs1o48jYYOxCLJvj+To5rE5zGVI6IWaRFsn7bTlxE1uRXYIUnoTuWaleX8BVZRxlkYU3oEMOpFM+qVB77iEsogNZsvJ1wyuPtrb9eA0DxCzvDuKSkiiuMF4jmxOBMrwJju13VBPZ8L9YAvDmw0clU+Gjtx4k1iooxwKrfoj2ovoWmMzp7MuD48irGmYKqmajm1DlBCGKFGOJ0m43zsXcWaUQD418x7RAD2o2NkRBd2cyZB76aflZR2xHB1Nbu7WKULiA1UZkmNa3IzBA/798UbYMw==; 31:aGexP0WJuVo1SdIz1zQ03b+VQyT5ihfX0hBBUV3+WwMceBc9kXhC2pDLDet9+rCK3sqKYux8itHoOXIb3VVlBLitrSA7VmCPTu6ceN6clLtNAA8Ea15GlptbSutjjhtThGJPtVPY42PEX+XJNCqg4EJl+LvEWky8exayQQePlhCGZOBaqcGekS5DWtIBWRcHNjU/kll8NoyBYiercD2gpBHJmZpjJfBWNUQbN6hw+sg= X-MS-TrafficTypeDiagnostic: HE1PR0802MB2556: X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2556; 20:tFLTfjJx4gezZQIr8l4Y80Q2W1s2LvtiDL6r1Ppflf1iJDs1/q51eIelmocYg2jVaWhcAgNRdDHkF88665n4ILhaIuPVowN2B01kDv6GzAWZwPS7x9uTNBuLI9Xx/iFnTyLnv729zQ5PGCXRYa8xlxAyuuXQDhUkiuspKJacLTmEcjg8U4G4pFHZdaywWBdaoYT9RZffGdNXmaNmWPsdt9ePe15SI1e4S9i3t+Ai8a/Y9uJKSKUPOMPMuZCqHl+oJ/2Xl5QDO7BrmP7r0jW9Nhqa2bPy4uRobt1T+IevWLYu9lw0BQD3Vc6DnadrQpU4KJrk/k13dE2mcbNnEWHl5sCNVsaR5GEHXbtJfN7+ANdhQkSYJs/ffny5u2yDlMFGFoH0zEvMr0/MSixHKTm86LAUUuT3NtXcYwdPujcXLYw=; 4:ZeFoeDiG04I+hj/kXVrsrWnE031DcUC8dOT/g7dvYBZG8KxMUWazBxbQNjDggVsumXNwg9qxB6mDjypAlLEb0TFFxYd6UyDtgaqqqC109z3V2GKHuYl1w8Gw+Z3Ju8GnPWbhbdWahSuKBXaGIvJoH2mlybzeHjGijfdc6dkVoRM735OmmRbbhuupAUoGPCzZHheYKQe93W52NGB+cIUj4/uRAVydLwU6aQ9I3fj9LBTkkgzEZmKXQYhnbYZi+bsA7oG/9VS39QcxFhxKef6HsNd9aYfFDLk+IWwOBHZQmDM= X-Exchange-Antispam-Report-Test: UriScan:(17755550239193); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(100000703101)(100105400095)(93006095)(93001095)(10201501046)(3002001)(6041248)(20161123562025)(20161123558100)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123564025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:HE1PR0802MB2556; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:HE1PR0802MB2556; X-Forefront-PRVS: 0402872DA1 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(7370300001)(4630300001)(6009001)(39830400002)(199003)(189002)(86362001)(105586002)(36756003)(81166006)(50226002)(7736002)(66066001)(189998001)(6116002)(1076002)(97736004)(3846002)(81156014)(8676002)(33646002)(53936002)(107886003)(110136004)(478600001)(6506006)(5003940100001)(6486002)(69596002)(48376002)(4326008)(2906002)(50466002)(47776003)(25786009)(6512007)(7350300001)(10056002)(42186005)(53416004)(6666003)(2950100002)(5660300001)(305945005)(68736007)(50986999)(76176999)(106356001)(101416001); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0802MB2556; H:pavelb-Z68P-DS3.sw.ru; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0802MB2556; 23:zIuSugkkyXuGhNqMi1nsDCB3ue5ONoERBM+jslR?= =?us-ascii?Q?+dGzM8nNmts8kRMAY0IOkTxcbWSWYFK8mjep09grL2qRsxcRMO75eVtshIjG?= =?us-ascii?Q?RMi7Im++M/3j9Jo4GLmkUblEgxhPWaFr6sja7EMHB479/3BIabZYxjXLrtDR?= =?us-ascii?Q?QGsLnd0kFSnDGDJ0PCV9Y5UWAGB8fbtyX9xA0JkhMTgUmRk/BxuaYXRwc6Q8?= =?us-ascii?Q?h5BSihIcQ4n+YboDPir9zMEwWvZqux/pn+WjS7PTLPhbJh3wztowRReHIiir?= =?us-ascii?Q?qNQgYsCl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TjkdaaPPX7QCFnPxYQn4QNhiK3WlrrPIKi7L+eClwlpju0H0PYiGG7B21dxTqzUP42K29/wgm6iHPGJ5h6TjmLeBxVYy7CEFFh8l5o56BSVSZar3B17NOlH32awuZNKJ7ri17o4= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Aug 2017 09:16:09.6762 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2556 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.99 Subject: [Qemu-devel] [PATCH v7 1/4] 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, pbutsykin@virtuozzo.com, jsnow@redhat.com, armbru@redhat.com, mreitz@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- qemu-img-cmds.hx | 4 ++-- qemu-img.c | 23 +++++++++++++++++++++++ qemu-img.texi | 6 +++++- tests/qemu-iotests/102 | 4 ++-- 4 files changed, 32 insertions(+), 5 deletions(-) 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 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] @var{filename} [+ | -]@var{size} ETEXI STEXI diff --git a/qemu-img.c b/qemu-img.c index 56ef49e214..b7b2386cbd 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -65,6 +65,7 @@ enum { OPTION_TARGET_IMAGE_OPTS = 263, OPTION_SIZE = 264, OPTION_PREALLOCATION = 265, + OPTION_SHRINK = 266, }; typedef enum OutputFormat { @@ -3437,6 +3438,7 @@ static int img_resize(int argc, char **argv) }, }; bool image_opts = false; + bool shrink = false; /* Remove size from argv manually so that negative numbers are not treated * as options by getopt. */ @@ -3455,6 +3457,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 = getopt_long(argc, argv, ":f:hq", @@ -3498,6 +3501,9 @@ static int img_resize(int argc, char **argv) return 1; } break; + case OPTION_SHRINK: + shrink = true; + break; } } if (optind != argc - 1) { @@ -3571,6 +3577,23 @@ static int img_resize(int argc, char **argv) goto out; } + 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") != 0) { + error_report( + "Use the --shrink option to perform a shrink operation."); + ret = -1; + goto out; + } else { + warn_report("Using the --shrink option will suppress this message." + "Note that future versions of qemu-img may refuse to " + "shrink images without this option."); + } + } + ret = blk_truncate(blk, total_size, prealloc, &err); if (!ret) { qprintf(quiet, "Image resized.\n"); diff --git a/qemu-img.texi b/qemu-img.texi index 72dabd6b3e..ea5d04b873 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -536,7 +536,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. -@item resize [--preallocation=@var{prealloc}] @var{filename} [+ | -]@var{size} +@item resize [--shrink] [--preallocation=@var{prealloc}] @var{filename} [+ | -]@var{size} Change the disk image as if it had been created with @var{size}. @@ -544,6 +544,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 partition sizes accordingly. Failure to do so will result in data loss! +When shrinking images, the @code{--shrink} option must be given. This informs +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: reftable, # 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)) $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 qemu_comm_method=monitor _launch_qemu -drive if=none,file="$TEST_IMG",id=drv0 -$QEMU_IMG resize -f raw "$TEST_IMG" $((5 * 64 * 1024)) +$QEMU_IMG resize -f raw --shrink "$TEST_IMG" $((5 * 64 * 1024)) _send_qemu_cmd $QEMU_HANDLE 'qemu-io drv0 map' 'allocated' \ | sed -e 's/^(qemu).*qemu-io drv0 map...$/(qemu) qemu-io drv0 map/'