From nobody Fri May 17 14:45:19 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1611842890; cv=none; d=zohomail.com; s=zohoarc; b=PDeUjwobRPajw+BZ5Igw4U0IGePtqDs4ZCPar2jGSuGKk71jggnIARNDYNU6T6x9ydo1EDBF3uBRCHI7qbUufisUXngvkKFjR1q49+jCSCCW8TVQ2tpQZ2pRwOxvgpOoImeD1IOkwIPbQV09zOu6dWjUnCq4ISyhFdkYhaUN/Pw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611842890; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=URxjgPCuj0Hsu6wakYYQKmz1Ix0bwRTCqA7VKsig+Oo=; b=YWTgXfJNQ3Y7llFl8HehEYwDYnD+BL79JsV0G8A2qh+sC5JWNX6z74PzJUPaCIjX4pMrShphst8XSfajuo31RDDrePSIeEDnYdqPrD8U2H6YUUfEi6PgEH2+puRgyaVH+XU/qNGL6MXPjfydsOYnU/Lh1yQ14jSL3BGTICxQKFg= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1611842890596732.5771025455277; Thu, 28 Jan 2021 06:08:10 -0800 (PST) Received: from localhost ([::1]:41266 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l57xx-0007gX-OE for importer@patchew.org; Thu, 28 Jan 2021 09:08:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35096) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l57xD-0007ET-9L for qemu-devel@nongnu.org; Thu, 28 Jan 2021 09:07:23 -0500 Received: from kerio.kamp.de ([195.62.97.192]:52547) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l57xA-0003NO-TD for qemu-devel@nongnu.org; Thu, 28 Jan 2021 09:07:23 -0500 Received: from submission.kamp.de ([195.62.97.28]) by kerio.kamp.de with ESMTPS (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)) for qemu-devel@nongnu.org; Thu, 28 Jan 2021 15:07:05 +0100 Received: (qmail 62107 invoked from network); 28 Jan 2021 14:07:10 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 28 Jan 2021 14:07:10 -0000 Received: by lieven-pc (Postfix, from userid 1060) id 6E75C13D87F; Thu, 28 Jan 2021 15:07:10 +0100 (CET) X-Footer: a2FtcC5kZQ== From: Peter Lieven To: qemu-block@nongnu.org Subject: [PATCH] qemu-img: add seek and -n option to dd command Date: Thu, 28 Jan 2021 15:07:04 +0100 Message-Id: <20210128140704.6547-1-pl@kamp.de> X-Mailer: git-send-email 2.17.1 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=195.62.97.192; envelope-from=pl@kamp.de; helo=kerio.kamp.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Peter Lieven , qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Peter Lieven diff --git a/docs/tools/qemu-img.rst b/docs/tools/qemu-img.rst index b615aa8419..7d4564c2b8 100644 --- a/docs/tools/qemu-img.rst +++ b/docs/tools/qemu-img.rst @@ -209,6 +209,10 @@ Parameters to dd subcommand: =20 .. program:: qemu-img-dd =20 +.. option:: -n + + Skip the creation of the output file + .. option:: bs=3DBLOCK_SIZE =20 Defines the block size @@ -229,6 +233,10 @@ Parameters to dd subcommand: =20 Sets the number of input blocks to skip =20 +.. option:: sseek=3DBLOCKS + + Sets the number of blocks to seek into the output + Parameters to snapshot subcommand: =20 .. program:: qemu-img-snapshot diff --git a/qemu-img.c b/qemu-img.c index 8597d069af..d7f390e382 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -213,12 +213,17 @@ static void QEMU_NORETURN help(void) " '-s' run in Strict mode - fail on different image size or se= ctor allocation\n" "\n" "Parameters to dd subcommand:\n" + " '-n' skips the target volume creation (useful if the volume = is created\n" + " prior to running qemu-img). Note that he behaviour is n= ot identical to\n" + " original dd option conv=3Dnocreat. The output is neithe= r truncated nor\n" + " is it possible to write past the end of an existing fil= e.\n" " 'bs=3DBYTES' read and write up to BYTES bytes at a time " "(default: 512)\n" " '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" + " 'seek=3DN' seek N bs-sized blocks into the output\n"; =20 printf("%s\nSupported formats:", help_msg); bdrv_iterate_format(format_print, NULL, false); @@ -4885,6 +4890,7 @@ static int img_bitmap(int argc, char **argv) #define C_IF 04 #define C_OF 010 #define C_SKIP 020 +#define C_SEEK 040 =20 struct DdInfo { unsigned int flags; @@ -4964,6 +4970,19 @@ static int img_dd_skip(const char *arg, return 0; } =20 +static int img_dd_seek(const char *arg, + struct DdIo *in, struct DdIo *out, + struct DdInfo *dd) +{ + out->offset =3D cvtnum("seek", arg); + + if (in->offset < 0) { + return 1; + } + + return 0; +} + static int img_dd(int argc, char **argv) { int ret =3D 0; @@ -4980,7 +4999,7 @@ static int img_dd(int argc, char **argv) const char *fmt =3D NULL; int64_t size =3D 0; int64_t block_count =3D 0, out_pos, in_pos; - bool force_share =3D false; + bool force_share =3D false, skip_create =3D false; struct DdInfo dd =3D { .flags =3D 0, .count =3D 0, @@ -5004,6 +5023,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 }, + { "seek", img_dd_seek, C_SEEK }, { NULL, NULL, 0 } }; const struct option long_options[] =3D { @@ -5014,7 +5034,7 @@ static int img_dd(int argc, char **argv) { 0, 0, 0, 0 } }; =20 - while ((c =3D getopt_long(argc, argv, ":hf:O:U", long_options, NULL)))= { + while ((c =3D getopt_long(argc, argv, ":hf:O:Un", long_options, NULL))= ) { if (c =3D=3D EOF) { break; } @@ -5037,6 +5057,9 @@ static int img_dd(int argc, char **argv) case 'U': force_share =3D true; break; + case 'n': + skip_create =3D true; + break; case OPTION_OBJECT: if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true))= { ret =3D -1; @@ -5116,22 +5139,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 (!skip_create) { + 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) { @@ -5145,22 +5171,25 @@ static int img_dd(int argc, char **argv) size =3D dd.count * in.bsz; } =20 - /* 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); - } else { - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, - size - in.bsz * in.offset, &error_abort); - } + if (!skip_create) { + /* Overflow means the specified offset is beyond input image's siz= e */ + 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); + } else { + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, + out.bsz * out.offset + size - + in.bsz * in.offset, &error_abort); + } =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; + 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, @@ -5189,7 +5218,7 @@ static int img_dd(int argc, char **argv) =20 in.buf =3D g_new(uint8_t, in.bsz); =20 - for (out_pos =3D 0; in_pos < size; block_count++) { + for (out_pos =3D out.offset * out.bsz; in_pos < size; block_count++) { int in_ret, out_ret; =20 if (in_pos + in.bsz > size) { --=20 2.17.1