From nobody Fri May 3 11:49:23 2024 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 148612360550782.1859656317896; Fri, 3 Feb 2017 04:06:45 -0800 (PST) Received: from localhost ([::1]:33697 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcdX-0004Nz-G4 for importer@patchew.org; Fri, 03 Feb 2017 07:06:43 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48445) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcbX-0003Bx-4F for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZcbT-0001qh-NJ for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:39 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39316) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZca3-0000Eb-Is; Fri, 03 Feb 2017 07:03:07 -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 5D03480465; Fri, 3 Feb 2017 12:03:07 +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 v13C30q6002011; Fri, 3 Feb 2017 07:03:04 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 12:02:49 +0000 Message-Id: <20170203120254.15062-2-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.28]); Fri, 03 Feb 2017 12:03:07 +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 1/6] qemu-img: add support for --object with '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 qemu-img dd command added --image-opts support, but missed the corresponding --object support. This prevented passing secrets (eg auth passwords) needed by certain disk images. Reviewed-by: Eric Blake Signed-off-by: Daniel P. Berrange --- qemu-img.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/qemu-img.c b/qemu-img.c index 74e3362..391a141 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3949,6 +3949,7 @@ static int img_dd(int argc, char **argv) }; const struct option long_options[] =3D { { "help", no_argument, 0, 'h'}, + { "object", required_argument, 0, OPTION_OBJECT}, { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, { 0, 0, 0, 0 } }; @@ -3971,6 +3972,14 @@ static int img_dd(int argc, char **argv) case 'h': help(); break; + case OPTION_OBJECT: { + QemuOpts *opts; + opts =3D qemu_opts_parse_noisily(&qemu_object_opts, + optarg, true); + if (!opts) { + return 1; + } + } break; case OPTION_IMAGE_OPTS: image_opts =3D true; break; @@ -4015,6 +4024,13 @@ static int img_dd(int argc, char **argv) ret =3D -1; goto out; } + + if (qemu_opts_foreach(&qemu_object_opts, + user_creatable_add_opts_foreach, + NULL, NULL)) { + return 1; + } + blk1 =3D img_open(image_opts, in.filename, fmt, 0, false, false); =20 if (!blk1) { --=20 2.9.3 From nobody Fri May 3 11:49:23 2024 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 1486123881471555.4252287232591; Fri, 3 Feb 2017 04:11:21 -0800 (PST) Received: from localhost ([::1]:33728 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZchz-0000A3-NW for importer@patchew.org; Fri, 03 Feb 2017 07:11:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48338) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcbS-000379-MF for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZcbO-0001kC-Cz for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:34 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39332) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZca6-0000JQ-90; Fri, 03 Feb 2017 07:03:10 -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 214CF85540; Fri, 3 Feb 2017 12:03:10 +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 v13C30q7002011; Fri, 3 Feb 2017 07:03:07 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 12:02:50 +0000 Message-Id: <20170203120254.15062-3-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.28]); Fri, 03 Feb 2017 12:03:10 +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 2/6] qemu-img: fix --image-opts usage with 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 --image-opts flag can only be used to affect the parsing of the source image. The target image has to be specified in the traditional style regardless, since it needs to be passed to the bdrv_create() API which does not support the new style opts. Signed-off-by: Daniel P. Berrange Reviewed-by: Max Reitz --- qemu-img.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 391a141..629f9e9 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4098,8 +4098,13 @@ static int img_dd(int argc, char **argv) goto out; } =20 - blk2 =3D img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR, - false, false); + /* TODO, we can't honour --image-opts for the target, + * since it needs to be given in a format compatible + * with the bdrv_create() call above which does not + * support image-opts style. + */ + blk2 =3D img_open_file(out.filename, out_fmt, BDRV_O_RDWR, + false, false); =20 if (!blk2) { ret =3D -1; --=20 2.9.3 From nobody Fri May 3 11:49:23 2024 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 From nobody Fri May 3 11:49:23 2024 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 1486124127907468.0991755443762; Fri, 3 Feb 2017 04:15:27 -0800 (PST) Received: from localhost ([::1]:33747 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcly-0003rW-0i for importer@patchew.org; Fri, 03 Feb 2017 07:15:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48402) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcbU-00039V-O5 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 1cZcbR-0001nh-IM for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:36 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52014) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZcaC-0000SD-5Y; Fri, 03 Feb 2017 07:03:16 -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 F1EE94D6A5; Fri, 3 Feb 2017 12:03:15 +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 v13C30q9002011; Fri, 3 Feb 2017 07:03:13 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 12:02:52 +0000 Message-Id: <20170203120254.15062-5-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.26]); Fri, 03 Feb 2017 12:03: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] [PATCH v2 4/6] qemu-img: add support for -o arg 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 -o arg to the convert command allows specification of format/protocol options for the newly created image. This adds a -o arg to the dd command to get feature parity. Signed-off-by: Daniel P. Berrange Reviewed-by: Max Reitz --- qemu-img-cmds.hx | 2 +- qemu-img.c | 32 +++++++++++++++++++++++++++++++- qemu-img.texi | 6 ++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index b2c5424..2488cbe 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -46,7 +46,7 @@ STEXI ETEXI =20 DEF("dd", img_dd, - "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=3Dblock_size] [count= =3Dblocks] [skip=3Dblocks] [conv=3Dnocreat,notrunc] if=3Dinput of=3Doutput") + "dd [--image-opts] [-f fmt] [-O output_fmt] [-o options] [bs=3Dblock_s= ize] [count=3Dblocks] [skip=3Dblocks] [conv=3Dnocreat,notrunc] if=3Dinput o= f=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}] [conv=3Dnocreat,not= runc] if=3D@var{input} of=3D@var{output} ETEXI diff --git a/qemu-img.c b/qemu-img.c index c9ab9e5..39fcf09 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3949,6 +3949,7 @@ static int img_dd(int argc, char **argv) int c, i; const char *out_fmt =3D "raw"; const char *fmt =3D NULL; + char *optionstr =3D NULL; int64_t size =3D 0, out_size; int64_t block_count =3D 0, out_pos, in_pos; struct DdInfo dd =3D { @@ -3984,7 +3985,7 @@ static int img_dd(int argc, char **argv) { 0, 0, 0, 0 } }; =20 - while ((c =3D getopt_long(argc, argv, "hnf:O:", long_options, NULL))) { + while ((c =3D getopt_long(argc, argv, "hno:f:O:", long_options, NULL))= ) { if (c =3D=3D EOF) { break; } @@ -3995,6 +3996,20 @@ static int img_dd(int argc, char **argv) case 'f': fmt =3D optarg; break; + case 'o': + if (!is_valid_option_list(optarg)) { + error_report("Invalid option list: %s", optarg); + ret =3D -1; + goto out; + } + if (!optionstr) { + optionstr =3D g_strdup(optarg); + } else { + char *old_options =3D optionstr; + optionstr =3D g_strdup_printf("%s,%s", optionstr, optarg); + g_free(old_options); + } + break; case '?': error_report("Try 'qemu-img --help' for more information."); ret =3D -1; @@ -4055,6 +4070,11 @@ static int img_dd(int argc, char **argv) goto out; } =20 + if (optionstr && has_help_option(optionstr)) { + ret =3D print_block_option_help(out.filename, out_fmt); + goto out; + } + if (qemu_opts_foreach(&qemu_object_opts, user_creatable_add_opts_foreach, NULL, NULL)) { @@ -4099,6 +4119,15 @@ static int img_dd(int argc, char **argv) create_opts =3D qemu_opts_append(create_opts, proto_drv->create_op= ts); =20 opts =3D qemu_opts_create(create_opts, NULL, 0, &error_abort); + + if (optionstr) { + qemu_opts_do_parse(opts, optionstr, NULL, &local_err); + if (local_err) { + error_report_err(local_err); + ret =3D -1; + goto out; + } + } } =20 size =3D blk_getlength(blk1); @@ -4224,6 +4253,7 @@ static int img_dd(int argc, char **argv) =20 out: g_free(arg); + g_free(optionstr); qemu_opts_del(opts); qemu_opts_free(create_opts); blk_unref(blk1); diff --git a/qemu-img.texi b/qemu-img.texi index 9f10562..01acfb8 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -326,10 +326,12 @@ 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}] [conv=3Dnocreat,notrunc] if=3D@var= {input} of=3D@var{output} +@item dd [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] [bs=3D@va= r{block_size}] [count=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. +@var{fmt} format to @var{output_fmt} format. Depending on the output file +format, you can add one or more @var{options} that enable additional +features of this format. =20 The data is by default read and written using blocks of 512 bytes but can = be modified by specifying @var{block_size}. If count=3D@var{blocks} is specif= ied --=20 2.9.3 From nobody Fri May 3 11:49:23 2024 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 1486124127124997.4746062442065; Fri, 3 Feb 2017 04:15:27 -0800 (PST) Received: from localhost ([::1]:33746 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZclv-0003pV-K7 for importer@patchew.org; Fri, 03 Feb 2017 07:15:23 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48444) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcbX-0003Bv-3e for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZcbU-0001sZ-R1 for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:39 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60624) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZcaF-0000V3-3A; Fri, 03 Feb 2017 07:03:19 -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 BEDD161BB0; Fri, 3 Feb 2017 12:03:18 +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 v13C30qA002011; Fri, 3 Feb 2017 07:03:16 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 12:02:53 +0000 Message-Id: <20170203120254.15062-6-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.39]); Fri, 03 Feb 2017 12:03:18 +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 5/6] qemu-img: introduce --target-image-opts for 'convert' 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 '--image-opts' flags indicates whether the source filename includes options. The target filename has to remain in the plain filename format though, since it needs to be passed to bdrv_create(). When using --skip-create though, it would be possible to use image-opts syntax. This adds --target-image-opts to indicate that the target filename includes options. Currently this mandates use of the --skip-create flag too. Signed-off-by: Daniel P. Berrange --- qemu-img-cmds.hx | 6 +-- qemu-img.c | 131 ++++++++++++++++++++++++++++++++++++---------------= ---- qemu-img.texi | 12 ++++- 3 files changed, 98 insertions(+), 51 deletions(-) diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index 2488cbe..13fded9 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -40,13 +40,13 @@ STEXI ETEXI =20 DEF("convert", img_convert, - "convert [--object objectdef] [--image-opts] [-c] [-p] [-q] [-n] [-f f= mt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_id_= or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] ou= tput_filename") + "convert [--object objectdef] [--image-opts] [--target-image-opts] [-c= ] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o opt= ions] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filenam= e [filename2 [...]] output_filename") STEXI -@item convert [--object @var{objectdef}] [--image-opts] [-c] [-p] [-q] [-n= ] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}= ] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param= }] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{outpu= t_filename} +@item convert [--object @var{objectdef}] [--image-opts] [--target-image-op= ts] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}= ] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [= -l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filena= me2} [...]] @var{output_filename} ETEXI =20 DEF("dd", img_dd, - "dd [--image-opts] [-f fmt] [-O output_fmt] [-o options] [bs=3Dblock_s= ize] [count=3Dblocks] [skip=3Dblocks] [conv=3Dnocreat,notrunc] if=3Dinput o= f=3Doutput") + "dd [--image-opts] [--target-image-opts] [-f fmt] [-O output_fmt] [-o = options] [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}] [conv=3Dnocreat,not= runc] if=3D@var{input} of=3D@var{output} ETEXI diff --git a/qemu-img.c b/qemu-img.c index 39fcf09..dc4c6eb 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -59,6 +59,7 @@ enum { OPTION_PATTERN =3D 260, OPTION_FLUSH_INTERVAL =3D 261, OPTION_NO_DRAIN =3D 262, + OPTION_TARGET_IMAGE_OPTS =3D 263, }; =20 typedef enum OutputFormat { @@ -1765,7 +1766,7 @@ static int img_convert(int argc, char **argv) int progress =3D 0, flags, src_flags; bool writethrough, src_writethrough; const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_file= name; - BlockDriver *drv, *proto_drv; + BlockDriver *drv =3D NULL, *proto_drv =3D NULL; BlockBackend **blk =3D NULL, *out_blk =3D NULL; BlockDriverState **bs =3D NULL, *out_bs =3D NULL; int64_t total_sectors; @@ -1783,9 +1784,10 @@ static int img_convert(int argc, char **argv) QemuOpts *sn_opts =3D NULL; ImgConvertState state; bool image_opts =3D false; + bool tgt_image_opts =3D false; =20 + out_fmt =3D NULL; fmt =3D NULL; - out_fmt =3D "raw"; cache =3D "unsafe"; src_cache =3D BDRV_DEFAULT_CACHE; out_baseimg =3D NULL; @@ -1796,6 +1798,7 @@ static int img_convert(int argc, char **argv) {"help", no_argument, 0, 'h'}, {"object", required_argument, 0, OPTION_OBJECT}, {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, + {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS= }, {0, 0, 0, 0} }; c =3D getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn", @@ -1900,15 +1903,27 @@ static int img_convert(int argc, char **argv) case OPTION_IMAGE_OPTS: image_opts =3D true; break; + case OPTION_TARGET_IMAGE_OPTS: + tgt_image_opts =3D true; + break; } } =20 + if (!out_fmt && !tgt_image_opts) { + out_fmt =3D "raw"; + } + if (qemu_opts_foreach(&qemu_object_opts, user_creatable_add_opts_foreach, NULL, NULL)) { goto fail_getopt; } =20 + if (tgt_image_opts && !skip_create) { + error_report("--target-image-opts requires use of -n flag"); + goto fail_getopt; + } + /* Initialize before goto out */ if (quiet) { progress =3D 0; @@ -1918,7 +1933,7 @@ static int img_convert(int argc, char **argv) bs_n =3D argc - optind - 1; out_filename =3D bs_n >=3D 1 ? argv[argc - 1] : NULL; =20 - if (options && has_help_option(options)) { + if (out_fmt && options && has_help_option(options)) { ret =3D print_block_option_help(out_filename, out_fmt); goto out; } @@ -1987,22 +2002,22 @@ static int img_convert(int argc, char **argv) goto out; } =20 - /* Find driver and parse its options */ - drv =3D bdrv_find_format(out_fmt); - if (!drv) { - error_report("Unknown file format '%s'", out_fmt); - ret =3D -1; - goto out; - } + if (!skip_create) { + /* Find driver and parse its options */ + drv =3D bdrv_find_format(out_fmt); + if (!drv) { + error_report("Unknown file format '%s'", out_fmt); + ret =3D -1; + goto out; + } =20 - proto_drv =3D bdrv_find_protocol(out_filename, true, &local_err); - if (!proto_drv) { - error_report_err(local_err); - ret =3D -1; - goto out; - } + proto_drv =3D bdrv_find_protocol(out_filename, true, &local_err); + if (!proto_drv) { + error_report_err(local_err); + ret =3D -1; + goto out; + } =20 - if (!skip_create) { if (!drv->create_opts) { error_report("Format driver '%s' does not support image creati= on", drv->format_name); @@ -2091,12 +2106,18 @@ static int img_convert(int argc, char **argv) goto out; } =20 - /* XXX we should allow --image-opts to trigger use of - * img_open() here, but then we have trouble with - * the bdrv_create() call which takes different params. - * Not critical right now, so fix can wait... - */ - out_blk =3D img_open_file(out_filename, out_fmt, flags, writethrough, = quiet); + if (skip_create) { + out_blk =3D img_open(tgt_image_opts, out_filename, out_fmt, + flags, writethrough, quiet); + } else { + /* TODO ultimately we should allow --target-image-opts + * to be used even when -n is not given. + * That has to wait for bdrv_create to be improved + * to allow filenames in option syntax + */ + out_blk =3D img_open_file(out_filename, out_fmt, + flags, writethrough, quiet); + } if (!out_blk) { ret =3D -1; goto out; @@ -3946,8 +3967,9 @@ static int img_dd(int argc, char **argv) QemuOptsList *create_opts =3D NULL; Error *local_err =3D NULL; bool image_opts =3D false; + bool tgt_image_opts =3D false; int c, i; - const char *out_fmt =3D "raw"; + const char *out_fmt =3D NULL; const char *fmt =3D NULL; char *optionstr =3D NULL; int64_t size =3D 0, out_size; @@ -3982,6 +4004,7 @@ static int img_dd(int argc, char **argv) { "help", no_argument, 0, 'h'}, { "object", required_argument, 0, OPTION_OBJECT}, { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, + {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, { 0, 0, 0, 0 } }; =20 @@ -4028,6 +4051,9 @@ static int img_dd(int argc, char **argv) case OPTION_IMAGE_OPTS: image_opts =3D true; break; + case OPTION_TARGET_IMAGE_OPTS: + tgt_image_opts =3D true; + break; } } =20 @@ -4064,13 +4090,22 @@ static int img_dd(int argc, char **argv) arg =3D NULL; } =20 + if (tgt_image_opts && !(dd.flags & C_NOCREAT)) { + error_report("--target-image-opts requires use of -n flag"); + goto out; + } + + if (!out_fmt && !tgt_image_opts) { + out_fmt =3D "raw"; + } + if (!(dd.flags & C_IF && dd.flags & C_OF)) { error_report("Must specify both input and output files"); ret =3D -1; goto out; } =20 - if (optionstr && has_help_option(optionstr)) { + if (out_fmt && optionstr && has_help_option(optionstr)) { ret =3D print_block_option_help(out.filename, out_fmt); goto out; } @@ -4088,21 +4123,21 @@ static int img_dd(int argc, char **argv) goto out; } =20 - drv =3D bdrv_find_format(out_fmt); - if (!drv) { - error_report("Unknown file format"); - ret =3D -1; - goto out; - } - proto_drv =3D bdrv_find_protocol(out.filename, true, &local_err); + if (!(dd.flags & C_NOCREAT)) { + drv =3D bdrv_find_format(out_fmt); + if (!drv) { + error_report("Unknown file format"); + ret =3D -1; + goto out; + } + proto_drv =3D bdrv_find_protocol(out.filename, true, &local_err); =20 - if (!proto_drv) { - error_report_err(local_err); - ret =3D -1; - goto out; - } + if (!proto_drv) { + error_report_err(local_err); + ret =3D -1; + goto out; + } =20 - if (!(dd.flags & C_NOCREAT)) { if (!drv->create_opts) { error_report("Format driver '%s' does not support image creati= on", drv->format_name); @@ -4152,7 +4187,6 @@ static int img_dd(int argc, char **argv) =20 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, @@ -4163,13 +4197,18 @@ static int img_dd(int argc, char **argv) } } =20 - /* TODO, we can't honour --image-opts for the target, - * since it needs to be given in a format compatible - * with the bdrv_create() call above which does not - * support image-opts style. - */ - blk2 =3D img_open_file(out.filename, out_fmt, BDRV_O_RDWR, - false, false); + if (dd.flags & C_NOCREAT) { + blk2 =3D img_open(tgt_image_opts, out.filename, out_fmt, + BDRV_O_RDWR, false, false); + } else { + /* TODO ultimately we should allow --target-image-opts + * to be used even when -n is not given. + * That has to wait for bdrv_create to be improved + * to allow filenames in option syntax + */ + blk2 =3D img_open_file(out.filename, out_fmt, + BDRV_O_RDWR, false, false); + } =20 if (!blk2) { ret =3D -1; diff --git a/qemu-img.texi b/qemu-img.texi index 01acfb8..bda3cc3 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -45,9 +45,17 @@ keys. =20 @item --image-opts =20 -Indicates that the @var{filename} parameter is to be interpreted as a +Indicates that the source @var{filename} parameter is to be interpreted as= a full option string, not a plain filename. This parameter is mutually -exclusive with the @var{-f} and @var{-F} parameters. +exclusive with the @var{-f} parameter. + +@item --target-image-opts + +Indicates that the target @var{filename} parameter(s) are to be interprete= d a +a full option string, not a plain filename. This parameter is mutually +exclusive with the @var{-F} or @var{-O} parameters. It is currently requir= ed +to also use the @var{-n} parameter to skip image creation. This restriction +will be relaxed in a future release. =20 @item fmt is the disk image format. It is guessed automatically in most cases. See b= elow --=20 2.9.3 From nobody Fri May 3 11:49:23 2024 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 1486123872185821.9543882185917; Fri, 3 Feb 2017 04:11:12 -0800 (PST) Received: from localhost ([::1]:33727 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZchp-0008RU-6r for importer@patchew.org; Fri, 03 Feb 2017 07:11:09 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48354) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZcbT-00037g-6a for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZcbR-0001nM-99 for qemu-devel@nongnu.org; Fri, 03 Feb 2017 07:04:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40236) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZcaH-0000YG-NC; Fri, 03 Feb 2017 07:03:21 -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 812CB4E4E6; Fri, 3 Feb 2017 12:03:21 +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 v13C30qB002011; Fri, 3 Feb 2017 07:03:19 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 12:02:54 +0000 Message-Id: <20170203120254.15062-7-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.38]); Fri, 03 Feb 2017 12:03:21 +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 6/6] qemu-img: copy *key-secret opts when opening newly created files 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 qemu-img dd/convert commands will create a image file and then try to open it. Historically it has been possible to open new files without passing any options. With encrypted files though, the *key-secret options are mandatory, so we need to provide those options when opening the newly created file. Signed-off-by: Daniel P. Berrange Reviewed-by: Max Reitz --- qemu-img.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index dc4c6eb..98522dd 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -319,6 +319,49 @@ static BlockBackend *img_open_file(const char *filenam= e, } =20 =20 +static int img_add_key_secrets(void *opaque, + const char *name, const char *value, + Error **errp) +{ + QDict **options =3D opaque; + + if (g_str_has_suffix(name, "key-secret")) { + if (!*options) { + *options =3D qdict_new(); + } + qdict_put(*options, name, qstring_from_str(value)); + } + + return 0; +} + +static BlockBackend *img_open_new_file(const char *filename, + QemuOpts *create_opts, + const char *fmt, int flags, + bool writethrough, bool quiet) +{ + BlockBackend *blk; + Error *local_err =3D NULL; + QDict *options =3D NULL; + + if (fmt) { + options =3D qdict_new(); + qdict_put(options, "driver", qstring_from_str(fmt)); + } + + qemu_opt_foreach(create_opts, img_add_key_secrets, &options, NULL); + + blk =3D blk_new_open(filename, NULL, options, flags, &local_err); + if (!blk) { + error_reportf_err(local_err, "Could not open '%s': ", filename); + return NULL; + } + blk_set_enable_write_cache(blk, !writethrough); + + return blk; +} + + static BlockBackend *img_open(bool image_opts, const char *filename, const char *fmt, int flags, bool writethroug= h, @@ -2115,8 +2158,8 @@ static int img_convert(int argc, char **argv) * That has to wait for bdrv_create to be improved * to allow filenames in option syntax */ - out_blk =3D img_open_file(out_filename, out_fmt, - flags, writethrough, quiet); + out_blk =3D img_open_new_file(out_filename, opts, out_fmt, + flags, writethrough, quiet); } if (!out_blk) { ret =3D -1; @@ -4206,8 +4249,8 @@ static int img_dd(int argc, char **argv) * That has to wait for bdrv_create to be improved * to allow filenames in option syntax */ - blk2 =3D img_open_file(out.filename, out_fmt, - BDRV_O_RDWR, false, false); + blk2 =3D img_open_new_file(out.filename, opts, out_fmt, + BDRV_O_RDWR, false, false); } =20 if (!blk2) { --=20 2.9.3