From nobody Tue Feb 10 08:41:24 2026 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.zoho.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 1486123866600800.4740243393005; Fri, 3 Feb 2017 04:11:06 -0800 (PST) Received: from localhost ([::1]:33726 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZchk-0008OD-KS for importer@patchew.org; Fri, 03 Feb 2017 07:11:04 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48428) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcbV-0003AN-Ic for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZcbS-0001pD-Pm for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:37 -0500 Received: from mx1.redhat.com ([209.132.183.28]:42248) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZca9-0000OV-IY; Fri, 03 Feb 2017 07:03:13 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 348158123E; Fri, 3 Feb 2017 12:03:13 +0000 (UTC) Received: from t460.redhat.com (ovpn-117-145.ams2.redhat.com [10.36.117.145]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v13C30q8002011; Fri, 3 Feb 2017 07:03:10 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 12:02:51 +0000 Message-Id: <20170203120254.15062-4-berrange@redhat.com> In-Reply-To: <20170203120254.15062-1-berrange@redhat.com> References: <20170203120254.15062-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 03 Feb 2017 12:03:13 +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] [PATCH v2 3/6] qemu-img: add support for conv=nocreat, notrunc args to dd command 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: Kevin Wolf , Fam Zheng , qemu-block@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" The -n arg to the convert command allows use of a pre-existing image, rather than creating a new image. This adds equivalent functionality to the dd command using the 'conv' arg. If 'conv=3Dnocreat' is used, then it will assume the image already exists. The existing image will be truncated to match the required output size. 'conv=3Dnotrunc' cna be used to preserve the existing image size. Signed-off-by: Daniel P. Berrange --- qemu-img-cmds.hx | 4 +- qemu-img.c | 137 +++++++++++++++++++++++++++++++++++++++++----------= ---- qemu-img.texi | 10 +++- 3 files changed, 115 insertions(+), 36 deletions(-) diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index f054599..b2c5424 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -46,9 +46,9 @@ STEXI ETEXI =20 DEF("dd", img_dd, - "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=3Dblock_size] [count= =3Dblocks] [skip=3Dblocks] if=3Dinput of=3Doutput") + "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=3Dblock_size] [count= =3Dblocks] [skip=3Dblocks] [conv=3Dnocreat,notrunc] if=3Dinput of=3Doutput") STEXI -@item dd [--image-opts] [-f @var{fmt}] [-O @var{output_fmt}] [bs=3D@var{bl= ock_size}] [count=3D@var{blocks}] [skip=3D@var{blocks}] if=3D@var{input} of= =3D@var{output} +@item dd [--image-opts] [-f @var{fmt}] [-O @var{output_fmt}] [bs=3D@var{bl= ock_size}] [count=3D@var{blocks}] [skip=3D@var{blocks}] [conv=3Dnocreat,not= runc] if=3D@var{input} of=3D@var{output} ETEXI =20 DEF("info", img_info, diff --git a/qemu-img.c b/qemu-img.c index 629f9e9..c9ab9e5 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -174,7 +174,9 @@ static void QEMU_NORETURN help(void) " 'count=3DN' copy only N input blocks\n" " 'if=3DFILE' read from FILE\n" " 'of=3DFILE' write to FILE\n" - " 'skip=3DN' skip N bs-sized blocks at the start of input\n"; + " 'skip=3DN' skip N bs-sized blocks at the start of input\n" + " 'conv=3Dnocreat' don't create the output file\n" + " 'conv=3Dnotrunc' don't truncate the output file\n"; =20 printf("%s\nSupported formats:", help_msg); bdrv_iterate_format(format_print, NULL); @@ -3814,11 +3816,13 @@ out: return 0; } =20 -#define C_BS 01 -#define C_COUNT 02 -#define C_IF 04 -#define C_OF 010 -#define C_SKIP 020 +#define C_BS (1 << 0) +#define C_COUNT (1 << 1) +#define C_IF (1 << 2) +#define C_OF (1 << 3) +#define C_SKIP (1 << 4) +#define C_NOCREAT (1 << 5) +#define C_NOTRUNC (1 << 6) =20 struct DdInfo { unsigned int flags; @@ -3906,6 +3910,31 @@ static int img_dd_skip(const char *arg, return 0; } =20 +static int img_dd_conv(const char *arg, + struct DdIo *in, struct DdIo *out, + struct DdInfo *dd) +{ + char **flags, **tmp; + + tmp =3D flags =3D g_strsplit(arg, ",", 0); + + while (tmp && *tmp) { + if (g_str_equal(*tmp, "noconv")) { + dd->flags |=3D C_NOCREAT; + } else if (g_str_equal(*tmp, "notrunc")) { + dd->flags |=3D C_NOTRUNC; + } else { + error_report("invalid conv argument: '%s'", *tmp); + g_strfreev(flags); + return 1; + } + tmp++; + } + + g_strfreev(flags); + return 0; +} + static int img_dd(int argc, char **argv) { int ret =3D 0; @@ -3920,7 +3949,7 @@ static int img_dd(int argc, char **argv) int c, i; const char *out_fmt =3D "raw"; const char *fmt =3D NULL; - int64_t size =3D 0; + int64_t size =3D 0, out_size; int64_t block_count =3D 0, out_pos, in_pos; struct DdInfo dd =3D { .flags =3D 0, @@ -3945,6 +3974,7 @@ static int img_dd(int argc, char **argv) { "if", img_dd_if, C_IF }, { "of", img_dd_of, C_OF }, { "skip", img_dd_skip, C_SKIP }, + { "conv", img_dd_conv, 0 }, { NULL, NULL, 0 } }; const struct option long_options[] =3D { @@ -3954,7 +3984,7 @@ static int img_dd(int argc, char **argv) { 0, 0, 0, 0 } }; =20 - while ((c =3D getopt_long(argc, argv, "hf:O:", long_options, NULL))) { + while ((c =3D getopt_long(argc, argv, "hnf:O:", long_options, NULL))) { if (c =3D=3D EOF) { break; } @@ -4051,22 +4081,25 @@ static int img_dd(int argc, char **argv) ret =3D -1; goto out; } - if (!drv->create_opts) { - error_report("Format driver '%s' does not support image creation", - drv->format_name); - ret =3D -1; - goto out; - } - if (!proto_drv->create_opts) { - error_report("Protocol driver '%s' does not support image creation= ", - proto_drv->format_name); - ret =3D -1; - goto out; - } - create_opts =3D qemu_opts_append(create_opts, drv->create_opts); - create_opts =3D qemu_opts_append(create_opts, proto_drv->create_opts); =20 - opts =3D qemu_opts_create(create_opts, NULL, 0, &error_abort); + if (!(dd.flags & C_NOCREAT)) { + if (!drv->create_opts) { + error_report("Format driver '%s' does not support image creati= on", + drv->format_name); + ret =3D -1; + goto out; + } + if (!proto_drv->create_opts) { + error_report("Protocol driver '%s' does not support image crea= tion", + proto_drv->format_name); + ret =3D -1; + goto out; + } + create_opts =3D qemu_opts_append(create_opts, drv->create_opts); + create_opts =3D qemu_opts_append(create_opts, proto_drv->create_op= ts); + + opts =3D qemu_opts_create(create_opts, NULL, 0, &error_abort); + } =20 size =3D blk_getlength(blk1); if (size < 0) { @@ -4083,19 +4116,22 @@ static int img_dd(int argc, char **argv) /* Overflow means the specified offset is beyond input image's size */ if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || size < in.bsz * in.offset)) { - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); + out_size =3D 0; } else { - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, - size - in.bsz * in.offset, &error_abort); + out_size =3D size - in.bsz * in.offset; } =20 - ret =3D bdrv_create(drv, out.filename, opts, &local_err); - if (ret < 0) { - error_reportf_err(local_err, - "%s: error while creating output image: ", - out.filename); - ret =3D -1; - goto out; + if (!(dd.flags & C_NOCREAT)) { + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, out_size, &error_abort); + + ret =3D bdrv_create(drv, out.filename, opts, &local_err); + if (ret < 0) { + error_reportf_err(local_err, + "%s: error while creating output image: ", + out.filename); + ret =3D -1; + goto out; + } } =20 /* TODO, we can't honour --image-opts for the target, @@ -4111,6 +4147,41 @@ static int img_dd(int argc, char **argv) goto out; } =20 + if (dd.flags & C_NOCREAT) { + if (dd.flags & C_NOTRUNC) { + int64_t existing_size =3D blk_getlength(blk2); + if (existing_size < 0) { + error_report("unable to get output image length: %s", + strerror(-existing_size)); + ret =3D -1; + goto out; + } else if (existing_size < out_size) { + /* Not large enough, so we must enlarge it */ + ret =3D blk_truncate(blk2, out_size); + if (ret < 0) { + error_reportf_err(local_err, + "%s: error while enlarging output im= age: ", + out.filename); + ret =3D -1; + goto out; + } + } + } else { + /* dd would truncate to 0 length, then append out_size + * worth of data. Our images don't grow on demand, so + * we just truncate to final output size straight away + */ + ret =3D blk_truncate(blk2, out_size); + if (ret < 0) { + error_reportf_err(local_err, + "%s: error while truncating output image= : ", + out.filename); + ret =3D -1; + goto out; + } + } + } + if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || size < in.offset * in.bsz)) { /* We give a warning if the skip option is bigger than the input diff --git a/qemu-img.texi b/qemu-img.texi index 174aae3..9f10562 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -326,7 +326,7 @@ skipped. This is useful for formats such as @code{rbd} = if the target volume has already been created with site specific options that cannot be supplied through qemu-img. =20 -@item dd [-f @var{fmt}] [-O @var{output_fmt}] [bs=3D@var{block_size}] [cou= nt=3D@var{blocks}] [skip=3D@var{blocks}] if=3D@var{input} of=3D@var{output} +@item dd [-f @var{fmt}] [-O @var{output_fmt}] [bs=3D@var{block_size}] [cou= nt=3D@var{blocks}] [skip=3D@var{blocks}] [conv=3Dnocreat,notrunc] if=3D@var= {input} of=3D@var{output} =20 Dd copies from @var{input} file to @var{output} file converting it from @var{fmt} format to @var{output_fmt} format. @@ -337,6 +337,14 @@ dd will stop reading input after reading @var{blocks} = input blocks. =20 The size syntax is similar to dd(1)'s size syntax. =20 +If the @code{conv=3Dnocreat} option is specified, the target volume creati= on +will be skipped. Its length will be truncated to match data length, if it +is longer than the required data size. If the @code{conv=3Dnotrunc} option +is specified, no file size shrinking will be done. If the existing output +file is too small it will be enlarged to fit. These options are useful for +formats such as @code{rbd} if the target volume has already been created +with site specific options that cannot be supplied through qemu-img. + @item info [-f @var{fmt}] [--output=3D@var{ofmt}] [--backing-chain] @var{f= ilename} =20 Give information about the disk image @var{filename}. Use it in --=20 2.9.3