From patchwork Wed Jul 27 14:51:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reda Sallahi X-Patchwork-Id: 9249935 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 607F0607D8 for ; Wed, 27 Jul 2016 14:55:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F098237A5 for ; Wed, 27 Jul 2016 14:55:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 40FA4269B2; Wed, 27 Jul 2016 14:55:30 +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 617B0237A5 for ; Wed, 27 Jul 2016 14:55:29 +0000 (UTC) Received: from localhost ([::1]:46935 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bSQF6-0005Eg-Kj for patchwork-qemu-devel@patchwork.kernel.org; Wed, 27 Jul 2016 10:55:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59242) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bSQBv-0002VB-Ak for qemu-devel@nongnu.org; Wed, 27 Jul 2016 10:52:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bSQBq-0002ip-3o for qemu-devel@nongnu.org; Wed, 27 Jul 2016 10:52:10 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:35243) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bSQBp-0002if-Pr; Wed, 27 Jul 2016 10:52:06 -0400 Received: by mail-wm0-x243.google.com with SMTP id i5so6850795wmg.2; Wed, 27 Jul 2016 07:52:05 -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; bh=9m1KIts+PRI7QLRYpkR1FYRjCiGM11rLvGKR3Lwxu5U=; b=yNQAHFpRMdGK22+kezIifaVUyzDamdfMR2Sz7q63wUJaXgAd1TuP9wjP2cSCF4zg3G QKl4O0yQ0DbmqA4OhNy0pnOlCB5Q5jEItsqMq0LdRPqhVFa1Gooz1BX5A2k0FX4KB546 GbKp+rayI3ZFEXUti3wKz6YkFYgJFNzVW+PfkSGkMW/ekeuPo4DjEpxiJuIBNcLerHxI iajFY3oyx9Mg4DczilG13qI3opmIOrePgLnYD1HpyhO5+4wW+BovICY5/i03IZel7BK5 HrRPpQreI7XuGBll/eYp0OpcesYrgi5kjv/W5OtGUzMSPQVP7eSzsMHXFjAdgdJRMsu4 zNpA== 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; bh=9m1KIts+PRI7QLRYpkR1FYRjCiGM11rLvGKR3Lwxu5U=; b=cAtag3dDP12CTbbXaLb4AtH14qE4U0iYjRXNY+1/fjui7WDcxYlCq5hUeXkYU8aypi 08Hp1eNcdBBtGcdtaJH1TrFqJ9g2QQ14iJnl7KbCxwA83lzR7E8rk3gd86basdNSRR3H kDMoBGWDWcP5LTvAthmV/KUbp2xb4lR8oLo8pjEBCYF/20QzdD3jeaNpD28xyp7rHWl9 Sbj676lQLA0I5nPGsFsHLR2FjpHINvZTrA02t/bdQ4mMMnXu3TTk6/LPwYIAOauSm3OC xcPFRI1QYFQqBLQkK8bFGwBBdWSNNZ2Lv1wVIveiiJYbbxewbKTUPcgjd3OTKeuTZ+9H lYsg== X-Gm-Message-State: ALyK8tLh5IJ4jqcYEOFMf+FNnskQO2wYxixBwU/3cqTW3ttwHX7W2orR5wSkwBYIBHqMbQ== X-Received: by 10.28.125.5 with SMTP id y5mr53535743wmc.103.1469631124857; Wed, 27 Jul 2016 07:52:04 -0700 (PDT) Received: from localhost.localdomain (ram94-2-82-66-69-246.fbx.proxad.net. [82.66.69.246]) by smtp.gmail.com with ESMTPSA id yt6sm6788028wjc.23.2016.07.27.07.52.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Jul 2016 07:52:04 -0700 (PDT) From: Reda Sallahi To: qemu-devel@nongnu.org Date: Wed, 27 Jul 2016 16:51:46 +0200 Message-Id: <20160727145146.8864-1-fullmanet@gmail.com> X-Mailer: git-send-email 2.9.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PATCH v3] qemu-img: add skip 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 , Reda Sallahi , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This adds the skip option which allows qemu-img dd to skip a number of blocks before copying the input. A test case was added to test the skip option. Signed-off-by: Reda Sallahi --- Depends on: [PATCH v6] qemu-img: add the 'dd' subcommand Changes from v2: * Delete a tab that sneaked in (test case 160). Changes from v1: * Change test case output. qemu-img-cmds.hx | 4 +-- qemu-img.c | 37 +++++++++++++++++++++--- qemu-img.texi | 4 ++- tests/qemu-iotests/160 | 72 ++++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/160.out | 51 ++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 6 files changed, 162 insertions(+), 7 deletions(-) create mode 100755 tests/qemu-iotests/160 create mode 100644 tests/qemu-iotests/160.out diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index 03bdd7a..f054599 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] if=input of=output") + "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] if=input of=output") STEXI -@item dd [--image-opts] [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] 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}] if=@var{input} of=@var{output} ETEXI DEF("info", img_info, diff --git a/qemu-img.c b/qemu-img.c index 498626b..e83f838 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -173,7 +173,8 @@ static void QEMU_NORETURN help(void) "(default: 512)\n" " 'count=N' copy only N input blocks\n" " 'if=FILE' read from FILE\n" - " 'of=FILE' write to FILE\n"; + " 'of=FILE' write to FILE\n" + " 'skip=N' skip N bs-sized blocks at the start of input\n"; printf("%s\nSupported formats:", help_msg); bdrv_iterate_format(format_print, NULL); @@ -3805,6 +3806,7 @@ out: #define C_COUNT 02 #define C_IF 04 #define C_OF 010 +#define C_SKIP 020 struct DdInfo { unsigned int flags; @@ -3815,6 +3817,7 @@ struct DdIo { size_t bsz; /* Block size */ char *filename; uint8_t *buf; + int64_t offset; }; struct DdOpts { @@ -3940,6 +3943,19 @@ static int img_dd_of(const char *arg, return 0; } +static int img_dd_skip(const char *arg, + struct DdIo *in, struct DdIo *out, + struct DdInfo *dd) +{ + in->offset = get_size(arg); + + if (in->offset == 0 && (errno == EINVAL || errno == ERANGE)) { + return 1; + } + + return 0; +} + static int img_dd(int argc, char **argv) { int ret = 0; @@ -3963,12 +3979,14 @@ static int img_dd(int argc, char **argv) struct DdIo in = { .bsz = 512, /* Block size is by default 512 bytes */ .filename = NULL, - .buf = NULL + .buf = NULL, + .offset = 0 }; struct DdIo out = { .bsz = 512, .filename = NULL, - .buf = NULL + .buf = NULL, + .offset = 0 }; const struct DdOpts options[] = { @@ -3976,6 +3994,7 @@ static int img_dd(int argc, char **argv) { "count", img_dd_count, C_COUNT }, { "if", img_dd_if, C_IF }, { "of", img_dd_of, C_OF }, + { "skip", img_dd_skip, C_SKIP }, { NULL, NULL, 0 } }; const struct option long_options[] = { @@ -4090,7 +4109,13 @@ static int img_dd(int argc, char **argv) size = dd.count * in.bsz; } - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort); + if (dd.flags & C_SKIP && size < in.bsz * in.offset) { + error_report("%s: cannot skip to specified offset", in.filename); + 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); + } ret = bdrv_create(drv, out.filename, opts, &local_err); if (ret < 0) { @@ -4111,6 +4136,10 @@ static int img_dd(int argc, char **argv) in.buf = g_new(uint8_t, in.bsz); + if (dd.flags & C_SKIP) { + incount = in.offset * in.bsz; + } + for (; incount < size; block_count++) { int in_ret, out_ret; diff --git a/qemu-img.texi b/qemu-img.texi index 880293a..174aae3 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -151,6 +151,8 @@ sets the number of input blocks to copy sets the input file @item of=@var{output} sets the output file +@item skip=@var{blocks} +sets the number of input blocks to skip @end table Command description: @@ -324,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. -@item dd [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] 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}] 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/160 b/tests/qemu-iotests/160 new file mode 100755 index 0000000..16e2957 --- /dev/null +++ b/tests/qemu-iotests/160 @@ -0,0 +1,72 @@ +#! /bin/bash +# +# qemu-img dd test for the skip 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_SKIP_BLOCKS="1 2 30 30K" + +for skip in $TEST_SKIP_BLOCKS; do + echo + echo "== Creating image ==" + + size=1M + _make_test_img $size + _check_test_img + $QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io + + echo + echo "== Converting the image with dd with skip=$skip ==" + + $QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" skip="$skip" -O "$IMGFMT" \ + 2> /dev/null + $QEMU_IMG check "$TEST_IMG.out" -f "$IMGFMT" 2>&1 | _filter_testdir | \ + _filter_qemu_img_check + dd if="$TEST_IMG" of="$TEST_IMG.out.dd" skip="$skip" 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/160.out b/tests/qemu-iotests/160.out new file mode 100644 index 0000000..565b03f --- /dev/null +++ b/tests/qemu-iotests/160.out @@ -0,0 +1,51 @@ +QA output created by 160 + +== Creating image == +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 +No errors were found on the image. +wrote 1048576/1048576 bytes at offset 0 +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with skip=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 1048576/1048576 bytes at offset 0 +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with skip=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 1048576/1048576 bytes at offset 0 +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with skip=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 1048576/1048576 bytes at offset 0 +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +== Converting the image with dd with skip=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 ec712bc..a042f48 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -159,3 +159,4 @@ 157 auto 158 rw auto quick 159 rw auto quick +160 rw auto quick