From patchwork Mon Aug 22 07:55:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reda Sallahi X-Patchwork-Id: 9293085 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2A57A60574 for ; Mon, 22 Aug 2016 08:00:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18BF3287DA for ; Mon, 22 Aug 2016 08:00:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0C38D287EA; Mon, 22 Aug 2016 08:00:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BA45C287DA for ; Mon, 22 Aug 2016 08:00:34 +0000 (UTC) Received: from localhost ([::1]:40132 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bbk9p-0008Kp-S3 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 22 Aug 2016 04:00:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44124) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bbk5U-0004VQ-43 for qemu-devel@nongnu.org; Mon, 22 Aug 2016 03:56:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bbk5R-0004gK-LC for qemu-devel@nongnu.org; Mon, 22 Aug 2016 03:56:04 -0400 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:33612) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bbk5R-0004ft-BQ; Mon, 22 Aug 2016 03:56:01 -0400 Received: by mail-wm0-x241.google.com with SMTP id o80so12209836wme.0; Mon, 22 Aug 2016 00:56:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=dMREaEAJ5OCoRF81dFLjWetSZfSWj3mqBdrufhoi9QA=; b=cUlcb4EfmvlWikmqY4yV7g+k/0PYYuFuwcNBBGyZJLDtXDvdk0t39rNBxYCEwHrccO l0MRs3RLmJdsBChKetBJFi1Lkibu3aPaPr3NrZo/1CRiIqGSZT1/h/bIOUjJJw54EhHU j3JwFyavhz7ep++V/vzo80OpXq28NtvBx6itKevHbenQw+NAAEH1sI5WUmMgaL2lJoqJ nOPy45gsgFENQVjzIlsnDwr996vtRpsyRMb3uonZcBxYMtbPdfFnySk0/zDkbIdb84eh IeKY2UdcWA4FeOTqB/QZnAG50D+iTbPDlmXXmobTF6BlOXrkNrTYcstLGyt/qWuNoMuS vTiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dMREaEAJ5OCoRF81dFLjWetSZfSWj3mqBdrufhoi9QA=; b=dtaV/yNrUGTsKC8+Tn/rXn5szy7dkATemGhOsLjSw5jucDPdn/mf8O/iP2naDEAaZa isiDrO2UWjD03Pm0tP6PZG3JX+VxOmjglLyO2GNbhEYlOmHZbaKych0Hu5eLZq2ounTp vnfxZsW14NixTaNhfka4uy97Rj6OGh/bansMKepcfngfGnYlyg71bie0gu9FrVlXhlEL /BsgmaHtzz0Rr8/cpAwkEcjnZ+4KgoOoAO97jJ4OLSLjfP0PaqLXSpa8WpU/MmHpMNfk KGCQdghoHk/T1n8IDfqx8hQXloEFywe24ey64lzOf7U3JdAoOaM/dHsYYS8qCwGbEge+ vrrA== X-Gm-Message-State: AEkoouuEcJFMDpPEIXF7OhmOUrHgHKi9fKWuJ24aXg7q1Q9Tzo6NTY9A2DkbREfAAe5IhA== X-Received: by 10.194.106.200 with SMTP id gw8mr16182461wjb.147.1471852560378; Mon, 22 Aug 2016 00:56:00 -0700 (PDT) Received: from cinque_stelle.sm14.sm.cri.epita.net ([163.5.208.89]) by smtp.gmail.com with ESMTPSA id uo4sm22329922wjc.36.2016.08.22.00.55.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Aug 2016 00:55:59 -0700 (PDT) From: Reda Sallahi To: qemu-devel@nongnu.org Date: Mon, 22 Aug 2016 09:55:11 +0200 Message-Id: <20160822075517.5859-2-fullmanet@gmail.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20160822075517.5859-1-fullmanet@gmail.com> References: <20160822075517.5859-1-fullmanet@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH 1/7] qemu-img: add seek option to dd 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 , Reda Sallahi , Stefan Hajnoczi Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch adds the seek option which allows qemu-img dd to skip a number of blocks on the output before copying the input. A test case was added to test the seek option. Signed-off-by: Reda Sallahi Reviewed-by: Stefan Hajnoczi --- qemu-img-cmds.hx | 4 +-- qemu-img.c | 45 +++++++++++++++++++++++----- qemu-img.texi | 4 ++- tests/qemu-iotests/161 | 73 ++++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/161.out | 51 ++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 6 files changed, 167 insertions(+), 11 deletions(-) create mode 100755 tests/qemu-iotests/161 create mode 100644 tests/qemu-iotests/161.out diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index 18685ac..e79a577 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -46,9 +46,9 @@ STEXI ETEXI DEF("dd", img_dd, - "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [conv=notrunc] if=input of=output") + "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [seek=blocks] [conv=notrunc] if=input of=output") STEXI -@item dd [--image-opts] [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] [skip=@var{blocks}] [conv=notrunc] if=@var{input} of=@var{output} + @item dd [--image-opts] [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] [skip=@var{blocks}] [seek=@var{blocks}] [conv=notrunc] if=@var{input} of=@var{output} ETEXI DEF("info", img_info, diff --git a/qemu-img.c b/qemu-img.c index 57b99d8..a1c0381 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -175,6 +175,7 @@ static void QEMU_NORETURN help(void) " 'if=FILE' read from FILE\n" " 'of=FILE' write to FILE\n" " 'skip=N' skip N bs-sized blocks at the start of input\n" + " 'seek=N' seek N bs-sized blocks at the start of output\n" " 'conv=notrunc' do not truncate the output file\n"; printf("%s\nSupported formats:", help_msg); @@ -3808,7 +3809,8 @@ out: #define C_IF 04 #define C_OF 010 #define C_SKIP 020 -#define C_CONV 040 +#define C_SEEK 040 +#define C_CONV 0100 struct DdInfo { unsigned int flags; @@ -3897,6 +3899,22 @@ static int img_dd_skip(const char *arg, return 0; } +static int img_dd_seek(const char *arg, + struct DdIo *in, struct DdIo *out, + struct DdInfo *dd) +{ + char *end; + + out->offset = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B); + + if (out->offset < 0 || *end) { + error_report("invalid number: '%s'", arg); + return 1; + } + + return 0; +} + #define C_NOTRUNC 01 static int img_dd_conv(const char *arg, @@ -3927,7 +3945,7 @@ static int img_dd(int argc, char **argv) int c, i; const char *out_fmt = "raw"; const char *fmt = NULL; - int64_t size = 0, out_size; + int64_t size = 0, out_size = 0; int64_t block_count = 0, out_pos, in_pos; struct DdInfo dd = { .flags = 0, @@ -3953,6 +3971,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 }, { "conv", img_dd_conv, C_CONV }, { NULL, NULL, 0 } }; @@ -4019,6 +4038,14 @@ static int img_dd(int argc, char **argv) arg = NULL; } + /* Overflow check for seek */ + if (out.offset > INT64_MAX / out.bsz) { + error_report("seek with the block size specified is too large " + "for data type used"); + ret = -1; + goto out; + } + if (!(dd.flags & C_IF && dd.flags & C_OF)) { error_report("Must specify both input and output files"); ret = -1; @@ -4044,9 +4071,9 @@ static int img_dd(int argc, char **argv) } /* Overflow means the specified offset is beyond input image's size */ if (in.offset > INT64_MAX / in.bsz || size < in.offset * in.bsz) { - out_size = 0; + out_size = out.offset * out.bsz; } else { - out_size = size - in.offset * in.bsz; + out_size = size - in.offset * in.bsz + out.offset * out.bsz; } if (image_opts) { @@ -4131,10 +4158,12 @@ static int img_dd(int argc, char **argv) goto out; } - if (in.offset <= INT64_MAX / in.bsz && size >= in.offset * in.bsz) { - if (blk2sz < out_size) { - blk_truncate(blk2, out_size); + if (in.offset > INT64_MAX / in.bsz || size < in.offset * in.bsz) { + if (blk2sz < out.offset * out.bsz) { + blk_truncate(blk2, out.offset * out.bsz); } + } else if (blk2sz < out_size) { + blk_truncate(blk2, out_size); } } @@ -4151,7 +4180,7 @@ static int img_dd(int argc, char **argv) in.buf = g_new(uint8_t, in.bsz); - for (out_pos = 0; in_pos < size; block_count++) { + for (out_pos = out.offset * out.bsz; in_pos < size; block_count++) { int in_ret, out_ret; if (in_pos + in.bsz > size) { diff --git a/qemu-img.texi b/qemu-img.texi index 002dde2..95f603b 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -154,6 +154,8 @@ sets the output file. dd truncates the output file to zero if 'conv=notrunc' is not specified. @item skip=@var{blocks} sets the number of input blocks to skip +@item seek=@var{blocks} +sets the number of output blocks to skip @item conv=notrunc makes dd not truncate output file to zero @end table @@ -329,7 +331,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. -@item dd [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] [skip=@var{blocks}] [conv=notrunc] if=@var{input} of=@var{output} +@item dd [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] [skip=@var{blocks}] [seek=@var{blocks}] [conv=notrunc] if=@var{input} of=@var{output} Dd copies from @var{input} file to @var{output} file converting it from @var{fmt} format to @var{output_fmt} format. diff --git a/tests/qemu-iotests/161 b/tests/qemu-iotests/161 new file mode 100755 index 0000000..bc99102 --- /dev/null +++ b/tests/qemu-iotests/161 @@ -0,0 +1,73 @@ +#! /bin/bash +# +# qemu-img dd test for the seek option +# +# Copyright (C) 2016 Reda Sallahi +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +owner=fullmanet@gmail.com + +seq="$(basename $0)" +echo "QA output created by $seq" + +here="$PWD" +status=1 + +_cleanup() +{ + _cleanup_test_img + rm -f "$TEST_IMG.out" "$TEST_IMG.out.dd" +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +. ./common.rc +. ./common.filter +. ./common.pattern + +_supported_fmt raw +_supported_proto file +_supported_os Linux + +TEST_SEEK_BLOCKS="1 2 30 30K" + +for seek in $TEST_SEEK_BLOCKS; do + echo + echo "== Creating image ==" + + size=1M + _make_test_img $size + _check_test_img + $QEMU_IO -c "write -P 0xa 13k 512k" "$TEST_IMG" | _filter_qemu_io + + echo + echo "== Converting the image with dd with seek=$seek ==" + + $QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" seek="$seek" conv=notrunc \ + -O "$IMGFMT" 2> /dev/null + TEST_IMG="$TEST_IMG.out" _check_test_img + dd if="$TEST_IMG" of="$TEST_IMG.out.dd" seek="$seek" conv=notrunc \ + status=none + + echo + echo "== Compare the images with qemu-img compare ==" + + $QEMU_IMG compare "$TEST_IMG.out.dd" "$TEST_IMG.out" +done + +echo +echo "*** done" +rm -f "$seq.full" +status=0 diff --git a/tests/qemu-iotests/161.out b/tests/qemu-iotests/161.out new file mode 100644 index 0000000..5dfc748 --- /dev/null +++ b/tests/qemu-iotests/161.out @@ -0,0 +1,51 @@ +QA output created by 161 + +== Creating image == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 13312 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with seek=1 == +No errors were found on the image. + +== Compare the images with qemu-img compare == +Images are identical. + +== Creating image == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 13312 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with seek=2 == +No errors were found on the image. + +== Compare the images with qemu-img compare == +Images are identical. + +== Creating image == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 13312 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with seek=30 == +No errors were found on the image. + +== Compare the images with qemu-img compare == +Images are identical. + +== Creating image == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 +No errors were found on the image. +wrote 524288/524288 bytes at offset 13312 +512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with seek=30K == +No errors were found on the image. + +== Compare the images with qemu-img compare == +Images are identical. + +*** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index 503eb27..28833ee 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -160,4 +160,5 @@ 158 rw auto quick 159 rw auto quick 160 rw auto quick +161 rw auto quick 162 auto quick