All of lore.kernel.org
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
	Peter Maydell <peter.maydell@linaro.org>
Subject: [Qemu-devel] [PULL 12/18] qemu-img: add skip option to dd
Date: Wed, 14 Sep 2016 18:40:56 +0200	[thread overview]
Message-ID: <1473871262-409-13-git-send-email-mreitz@redhat.com> (raw)
In-Reply-To: <1473871262-409-1-git-send-email-mreitz@redhat.com>

From: Reda Sallahi <fullmanet@gmail.com>

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 <fullmanet@gmail.com>
Message-id: 20160810141609.32727-1-fullmanet@gmail.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 qemu-img-cmds.hx           |  4 +--
 qemu-img.c                 | 50 ++++++++++++++++++++++++++++----
 qemu-img.texi              |  4 ++-
 tests/qemu-iotests/160     | 72 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/160.out | 51 ++++++++++++++++++++++++++++++++
 tests/qemu-iotests/group   |  1 +
 6 files changed, 174 insertions(+), 8 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 5bdac8d..ceffefe 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);
@@ -3807,6 +3808,7 @@ out:
 #define C_COUNT   02
 #define C_IF      04
 #define C_OF      010
+#define C_SKIP    020
 
 struct DdInfo {
     unsigned int flags;
@@ -3817,6 +3819,7 @@ struct DdIo {
     int bsz;    /* Block size */
     char *filename;
     uint8_t *buf;
+    int64_t offset;
 };
 
 struct DdOpts {
@@ -3877,6 +3880,22 @@ 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)
+{
+    char *end;
+
+    in->offset = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
+
+    if (in->offset < 0 || *end) {
+        error_report("invalid number: '%s'", arg);
+        return 1;
+    }
+
+    return 0;
+}
+
 static int img_dd(int argc, char **argv)
 {
     int ret = 0;
@@ -3900,12 +3919,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[] = {
@@ -3913,6 +3934,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[] = {
@@ -4032,7 +4054,14 @@ static int img_dd(int argc, char **argv)
         size = dd.count * in.bsz;
     }
 
-    qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
+    /* 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);
+    }
 
     ret = bdrv_create(drv, out.filename, opts, &local_err);
     if (ret < 0) {
@@ -4051,9 +4080,20 @@ static int img_dd(int argc, char **argv)
         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
+         * size and create an empty output disk image (i.e. like dd(1)).
+         */
+        error_report("%s: cannot skip to specified offset", in.filename);
+        in_pos = size;
+    } else {
+        in_pos = in.offset * in.bsz;
+    }
+
     in.buf = g_new(uint8_t, in.bsz);
 
-    for (in_pos = 0, out_pos = 0; in_pos < size; block_count++) {
+    for (out_pos = 0; 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 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..5c910e5
--- /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 <http://www.gnu.org/licenses/>.
+#
+
+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 24 512k" "$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
+    TEST_IMG="$TEST_IMG.out" _check_test_img
+    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..9cedc80
--- /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 524288/524288 bytes at offset 24
+512 KiB, 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 524288/524288 bytes at offset 24
+512 KiB, 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 524288/524288 bytes at offset 24
+512 KiB, 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 524288/524288 bytes at offset 24
+512 KiB, 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 c46beb1..a57fc92 100644
--- a/tests/qemu-iotests/group
+++ b/tests/qemu-iotests/group
@@ -158,5 +158,6 @@
 156 rw auto quick
 157 auto
 159 rw auto quick
+160 rw auto quick
 162 auto quick
 170 rw auto quick
-- 
2.5.5

  parent reply	other threads:[~2016-09-14 16:41 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-14 16:40 [Qemu-devel] [PULL 00/18] Block layer patches Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 01/18] block: Hide HBitmap in block dirty bitmap interface Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 02/18] HBitmap: Introduce "meta" bitmap to track bit changes Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 03/18] tests: Add test code for meta bitmap Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 04/18] block: Support meta dirty bitmap Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 05/18] block: Add two dirty bitmap getters Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 06/18] block: Assert that bdrv_release_dirty_bitmap succeeded Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 07/18] hbitmap: serialization Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 08/18] block: BdrvDirtyBitmap serialization interface Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 09/18] tests: Add test code for hbitmap serialization Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 10/18] block: More operations for meta dirty bitmap Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 11/18] qemu-img: add the 'dd' subcommand Max Reitz
2016-09-14 16:40 ` Max Reitz [this message]
2016-09-14 16:40 ` [Qemu-devel] [PULL 13/18] blockdev: prepare iSCSI block driver for dynamic loading Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 14/18] blockdev: Add dynamic generation of module_block.h Max Reitz
2016-09-14 16:40 ` [Qemu-devel] [PULL 15/18] blockdev: Add dynamic module loading for block drivers Max Reitz
2016-09-14 16:41 ` [Qemu-devel] [PULL 16/18] blockdev: Modularize nfs block driver Max Reitz
2016-09-14 16:41 ` [Qemu-devel] [PULL 17/18] commit: get the overlay node before manipulating the backing chain Max Reitz
2016-09-14 16:41 ` [Qemu-devel] [PULL 18/18] iotest 055: refactor and speed up Max Reitz
2016-09-14 20:16 ` [Qemu-devel] [PULL 00/18] Block layer patches no-reply
2016-09-15 16:10 ` Peter Maydell
2016-09-17 21:32   ` Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1473871262-409-13-git-send-email-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.