From nobody Wed Apr 16 03:29:37 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 (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499793077737106.05762124548426; Tue, 11 Jul 2017 10:11:17 -0700 (PDT) Received: from localhost ([::1]:47688 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUygs-0006c4-Cq for importer@patchew.org; Tue, 11 Jul 2017 13:11:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47062) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUxmL-00029x-Id for qemu-devel@nongnu.org; Tue, 11 Jul 2017 12:12:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dUxmH-0007G9-FJ for qemu-devel@nongnu.org; Tue, 11 Jul 2017 12:12:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58438) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dUxmD-0007AN-KX; Tue, 11 Jul 2017 12:12:41 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 44B6C80E72; Tue, 11 Jul 2017 16:12:40 +0000 (UTC) Received: from localhost (ovpn-204-123.brq.redhat.com [10.40.204.123]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A22E460617; Tue, 11 Jul 2017 16:12:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 44B6C80E72 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 44B6C80E72 From: Max Reitz To: qemu-block@nongnu.org Date: Tue, 11 Jul 2017 18:08:02 +0200 Message-Id: <20170711160814.20941-74-mreitz@redhat.com> In-Reply-To: <20170711160814.20941-1-mreitz@redhat.com> References: <20170711160814.20941-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 11 Jul 2017 16:12:40 +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 73/85] qemu-img: Expose PreallocMode for resizing 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: qemu-devel@nongnu.org, Max Reitz 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" Add a --preallocation command line option to qemu-img resize which can be used to set the PreallocMode parameter of blk_truncate(). While touching this code, fix the fact that we did not handle errors returned by blk_getlength(). Signed-off-by: Max Reitz Message-id: 20170613202107.10125-5-mreitz@redhat.com Reviewed-by: Stefan Hajnoczi Signed-off-by: Max Reitz --- qemu-img.c | 33 ++++++++++++++++++++++++++++++--- qemu-img.texi | 7 ++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 1b74195..2802214 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -24,6 +24,7 @@ #include "qemu/osdep.h" #include "qemu-version.h" #include "qapi/error.h" +#include "qapi/util.h" #include "qapi-visit.h" #include "qapi/qobject-output-visitor.h" #include "qapi/qmp/qerror.h" @@ -62,6 +63,7 @@ enum { OPTION_NO_DRAIN =3D 262, OPTION_TARGET_IMAGE_OPTS =3D 263, OPTION_SIZE =3D 264, + OPTION_PREALLOCATION =3D 265, }; =20 typedef enum OutputFormat { @@ -3408,9 +3410,10 @@ static int img_resize(int argc, char **argv) Error *err =3D NULL; int c, ret, relative; const char *filename, *fmt, *size; - int64_t n, total_size; + int64_t n, total_size, current_size; bool quiet =3D false; BlockBackend *blk =3D NULL; + PreallocMode prealloc =3D PREALLOC_MODE_OFF; QemuOpts *param; =20 static QemuOptsList resize_options =3D { @@ -3444,6 +3447,7 @@ static int img_resize(int argc, char **argv) {"help", no_argument, 0, 'h'}, {"object", required_argument, 0, OPTION_OBJECT}, {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, + {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, {0, 0, 0, 0} }; c =3D getopt_long(argc, argv, ":f:hq", @@ -3478,6 +3482,15 @@ static int img_resize(int argc, char **argv) case OPTION_IMAGE_OPTS: image_opts =3D true; break; + case OPTION_PREALLOCATION: + prealloc =3D qapi_enum_parse(PreallocMode_lookup, optarg, + PREALLOC_MODE__MAX, PREALLOC_MODE__= MAX, + NULL); + if (prealloc =3D=3D PREALLOC_MODE__MAX) { + error_report("Invalid preallocation mode '%s'", optarg); + return 1; + } + break; } } if (optind !=3D argc - 1) { @@ -3526,8 +3539,16 @@ static int img_resize(int argc, char **argv) goto out; } =20 + current_size =3D blk_getlength(blk); + if (current_size < 0) { + error_report("Failed to inquire current image length: %s", + strerror(-current_size)); + ret =3D -1; + goto out; + } + if (relative) { - total_size =3D blk_getlength(blk) + n * relative; + total_size =3D current_size + n * relative; } else { total_size =3D n; } @@ -3537,7 +3558,13 @@ static int img_resize(int argc, char **argv) goto out; } =20 - ret =3D blk_truncate(blk, total_size, PREALLOC_MODE_OFF, &err); + if (total_size <=3D current_size && prealloc !=3D PREALLOC_MODE_OFF) { + error_report("Preallocation can only be used for growing images"); + ret =3D -1; + goto out; + } + + ret =3D blk_truncate(blk, total_size, prealloc, &err); if (!ret) { qprintf(quiet, "Image resized.\n"); } else { diff --git a/qemu-img.texi b/qemu-img.texi index a2f4519..f11f603 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -529,7 +529,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 @var{filename} [+ | -]@var{size} +@item resize [--preallocation=3D@var{prealloc}] @var{filename} [+ | -]@var= {size} =20 Change the disk image as if it had been created with @var{size}. =20 @@ -541,6 +541,11 @@ After using this command to grow a disk image, you mus= t use file system and partitioning tools inside the VM to actually begin using the new space on = the device. =20 +When growing an image, the @code{--preallocation} option may be used to sp= ecify +how the additional image area should be allocated on the host. See the fo= rmat +description in the @code{NOTES} section which values are allowed. Using t= his +option may result in slightly more data being allocated than necessary. + @item amend [-p] [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{fil= ename} =20 Amends the image format specific @var{options} for the image file --=20 2.9.4