From nobody Mon Feb 9 17:24:36 2026 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1644513716723819.8052916533522; Thu, 10 Feb 2022 09:21:56 -0800 (PST) Received: from localhost ([::1]:54056 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nID8k-0003vf-MQ for importer@patchew.org; Thu, 10 Feb 2022 12:21:54 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45570) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nI9Y4-0003ow-BW; Thu, 10 Feb 2022 08:31:52 -0500 Received: from proxmox-new.maurer-it.com ([94.136.29.106]:51161) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nI9Xx-0004md-Jx; Thu, 10 Feb 2022 08:31:47 -0500 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 124F846DCD; Thu, 10 Feb 2022 14:31:31 +0100 (CET) From: Fabian Ebner To: qemu-devel@nongnu.org Subject: [PATCH 2/4] qemu-img: dd: add isize parameter Date: Thu, 10 Feb 2022 14:31:21 +0100 Message-Id: <20220210133123.347350-3-f.ebner@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220210133123.347350-1-f.ebner@proxmox.com> References: <20220210133123.347350-1-f.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=94.136.29.106; envelope-from=f.ebner@proxmox.com; helo=proxmox-new.maurer-it.com 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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, w.bumiller@proxmox.com, qemu-block@nongnu.org, aderumier@odiso.com, hreitz@redhat.com, t.lamprecht@proxmox.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1644513720308100001 Content-Type: text/plain; charset="utf-8" From: Wolfgang Bumiller for writing small images from stdin to bigger ones. In order to distinguish between an actually unexpected and an expected end of input. Signed-off-by: Wolfgang Bumiller Signed-off-by: Thomas Lamprecht [FE: override size earlier use flag to detect parameter add documenation] Signed-off-by: Fabian Ebner --- docs/tools/qemu-img.rst | 10 ++++++++-- qemu-img-cmds.hx | 4 ++-- qemu-img.c | 24 +++++++++++++++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/docs/tools/qemu-img.rst b/docs/tools/qemu-img.rst index 775eaf3097..43328fe108 100644 --- a/docs/tools/qemu-img.rst +++ b/docs/tools/qemu-img.rst @@ -230,6 +230,10 @@ Parameters to dd subcommand: =20 Sets the number of input blocks to skip =20 +.. option:: isize=3DINPUT_SIZE + + Treat the input image or stream as if it had this size + .. option:: osize=3DOUTPUT_SIZE =20 Sets the output image's size @@ -492,7 +496,7 @@ Command description: it doesn't need to be specified separately in this case. =20 =20 -.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=3DBLOCK_SI= ZE] [count=3DBLOCKS] [skip=3DBLOCKS] [osize=3DOUTPUT_SIZE] [if=3DINPUT] [of= =3DOUTPUT] +.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=3DBLOCK_SI= ZE] [count=3DBLOCKS] [skip=3DBLOCKS] [isize=3DINPUT_SIZE] [osize=3DOUTPUT_S= IZE] [if=3DINPUT] [of=3DOUTPUT] =20 dd copies from *INPUT* file (default: STDIN) to *OUTPUT* file (default: STDOUT) converting it from *FMT* format to *OUTPUT_FMT* format. @@ -504,7 +508,9 @@ Command description: The size syntax is similar to :manpage:`dd(1)`'s size syntax. =20 The output image will be created with size *OUTPUT_SIZE* and at most thi= s many - bytes will be copied. + bytes will be copied. When *INPUT_SIZE* is positive, it overrides the in= put + image's size for the copy operation. When *INPUT_SIZE* is zero and readi= ng + from STDIN, do not treat premature end of the input stream as an error. =20 .. option:: info [--object OBJECTDEF] [--image-opts] [-f FMT] [--output=3D= OFMT] [--backing-chain] [-U] FILENAME =20 diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index e4935365c9..50993e6c47 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -58,9 +58,9 @@ SRST ERST =20 DEF("dd", img_dd, - "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=3Dblock_size] [co= unt=3Dblocks] [skip=3Dblocks] [osize=3Doutput_size] [if=3Dinput] [of=3Doutp= ut]") + "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=3Dblock_size] [co= unt=3Dblocks] [skip=3Dblocks] [isize=3Dinput_size] [osize=3Doutput_size] [i= f=3Dinput] [of=3Doutput]") SRST -.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=3DBLOCK_SI= ZE] [count=3DBLOCKS] [skip=3DBLOCKS] [osize=3DOUTPUT_SIZE] [if=3DINPUT] [of= =3DOUTPUT] +.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=3DBLOCK_SI= ZE] [count=3DBLOCKS] [skip=3DBLOCKS] [isize=3DINPUT_SIZE] [osize=3DOUTPUT_S= IZE] [if=3DINPUT] [of=3DOUTPUT] ERST =20 DEF("info", img_info, diff --git a/qemu-img.c b/qemu-img.c index ea488fd190..630928773d 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4820,11 +4820,13 @@ static int img_bitmap(int argc, char **argv) #define C_OF 010 #define C_SKIP 020 #define C_OSIZE 040 +#define C_ISIZE 0100 =20 struct DdInfo { unsigned int flags; int64_t count; int64_t osize; + int64_t isize; }; =20 struct DdIo { @@ -4913,6 +4915,19 @@ static int img_dd_osize(const char *arg, return 0; } =20 +static int img_dd_isize(const char *arg, + struct DdIo *in, struct DdIo *out, + struct DdInfo *dd) +{ + dd->isize =3D cvtnum("isize", arg); + + if (dd->isize < 0) { + return 1; + } + + return 0; +} + static int img_dd(int argc, char **argv) { int ret =3D 0; @@ -4934,6 +4949,7 @@ static int img_dd(int argc, char **argv) .flags =3D 0, .count =3D 0, .osize =3D 0, + .isize =3D 0, }; struct DdIo in =3D { .bsz =3D 512, /* Block size is by default 512 bytes */ @@ -4955,6 +4971,7 @@ static int img_dd(int argc, char **argv) { "of", img_dd_of, C_OF }, { "skip", img_dd_skip, C_SKIP }, { "osize", img_dd_osize, C_OSIZE }, + { "isize", img_dd_isize, C_ISIZE }, { NULL, NULL, 0 } }; const struct option long_options[] =3D { @@ -5061,7 +5078,9 @@ static int img_dd(int argc, char **argv) } } =20 - if (dd.flags & C_IF) { + if (dd.flags & C_ISIZE && dd.isize > 0) { + size =3D dd.isize; + } else if (dd.flags & C_IF) { size =3D blk_getlength(blk1); if (size < 0) { error_report("Failed to get size for '%s'", in.filename); @@ -5174,6 +5193,9 @@ static int img_dd(int argc, char **argv) } else { in_ret =3D read(STDIN_FILENO, in.buf, in_bsz); if (in_ret =3D=3D 0) { + if (dd.flags & C_ISIZE && dd.isize =3D=3D 0) { + goto out; + } /* early EOF is considered an error */ error_report("Input ended unexpectedly"); ret =3D -1; --=20 2.30.2