diff mbox

qemu-img: add seek option to dd

Message ID 20160810150344.2337-1-fullmanet@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Reda Sallahi Aug. 10, 2016, 3:03 p.m. UTC
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 <fullmanet@gmail.com>
---
Depends on:
[PATCH v5] qemu-img: add skip option to dd

 qemu-img-cmds.hx           |   4 +-
 qemu-img.c                 | 116 ++++++++++++++++++++++++++++++++++++++-------
 qemu-img.texi              |   4 +-
 tests/qemu-iotests/161     |  72 ++++++++++++++++++++++++++++
 tests/qemu-iotests/161.out |  51 ++++++++++++++++++++
 tests/qemu-iotests/group   |   1 +
 6 files changed, 228 insertions(+), 20 deletions(-)
 create mode 100755 tests/qemu-iotests/161
 create mode 100644 tests/qemu-iotests/161.out

Comments

Max Reitz Aug. 10, 2016, 5:42 p.m. UTC | #1
On 10.08.2016 17:03, Reda Sallahi wrote:
> 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 <fullmanet@gmail.com>
> ---
> Depends on:
> [PATCH v5] qemu-img: add skip option to dd
> 
>  qemu-img-cmds.hx           |   4 +-
>  qemu-img.c                 | 116 ++++++++++++++++++++++++++++++++++++++-------
>  qemu-img.texi              |   4 +-
>  tests/qemu-iotests/161     |  72 ++++++++++++++++++++++++++++
>  tests/qemu-iotests/161.out |  51 ++++++++++++++++++++
>  tests/qemu-iotests/group   |   1 +
>  6 files changed, 228 insertions(+), 20 deletions(-)
>  create mode 100755 tests/qemu-iotests/161
>  create mode 100644 tests/qemu-iotests/161.out

On IRC, Kevin has raised concern that qemu-img dd should never truncate
the output image, or at least not until told to do so.

While I do not share his opinion, I do share his concern that people may
fall into this trap. Therefore, I propose doing two things:

First, we should merge a patch which implements conv=notrunc. Without
qemu-img dd actually truncating anything yet, that will be rather
trivial. However, I think that until we have come to a conclusion on how
to handle the case where conv=notrunc has not been passed, we should
actually make conv=notrunc mandatory. That is, the patch implementing
conv=notrunc should also make qemu-img dd error out if it has not been
specified.

I admit this is a rather stupid idea but I think it will do the trick
for now.

Second, this patch should be separated into two parts: The first of
which make qemu-img dd no longer create the output image if it exists
already, and the second of which then implements the seek option. Those
are two different things so I think they should be done in two different
patches.

Max
diff mbox

Patch

diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
index f054599..826ce3f 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] if=input of=output")
+    "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [seek=blocks] 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}] 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}] if=@var{input} of=@var{output}
 ETEXI
 
 DEF("info", img_info,
diff --git a/qemu-img.c b/qemu-img.c
index 3adec86..600c866 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -174,7 +174,8 @@  static void QEMU_NORETURN help(void)
            "  'count=N' copy only N input blocks\n"
            "  '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";
+           "  '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";
 
     printf("%s\nSupported formats:", help_msg);
     bdrv_iterate_format(format_print, NULL);
@@ -3807,6 +3808,7 @@  out:
 #define C_IF      04
 #define C_OF      010
 #define C_SKIP    020
+#define C_SEEK    040
 
 struct DdInfo {
     unsigned int flags;
@@ -3894,6 +3896,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;
+}
+
 static int img_dd(int argc, char **argv)
 {
     int ret = 0;
@@ -3901,6 +3919,8 @@  static int img_dd(int argc, char **argv)
     char *tmp;
     BlockDriver *drv = NULL, *proto_drv = NULL;
     BlockBackend *blk1 = NULL, *blk2 = NULL;
+    BlockDriverState *bs = NULL;
+    QDict *qoptions = NULL;
     QemuOpts *opts = NULL;
     QemuOptsList *create_opts = NULL;
     Error *local_err = NULL;
@@ -3933,6 +3953,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[] = {
@@ -3998,6 +4019,14 @@  static int img_dd(int argc, char **argv)
         arg = NULL;
     }
 
+    /* Overflow check for seek */
+    if (dd.flags & C_SEEK && 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;
@@ -4052,22 +4081,61 @@  static int img_dd(int argc, char **argv)
         size = dd.count * in.bsz;
     }
 
-    /* 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);
-    }
+    qoptions = qdict_new();
+    qdict_put(qoptions, "driver", qstring_from_str(out_fmt));
 
-    ret = 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 = -1;
-        goto out;
+    bs = bdrv_open(out.filename, NULL, qoptions, BDRV_O_RDWR, &local_err);
+
+    if (!bs) {
+        drv = bdrv_find_format(out_fmt);
+        if (!drv) {
+            error_report("Unknown file format");
+            ret = -1;
+            goto out;
+        }
+        proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
+
+        if (!proto_drv) {
+            error_report_err(local_err);
+            ret = -1;
+            goto out;
+        }
+        if (!drv->create_opts) {
+            error_report("Format driver '%s' does not support image creation",
+                         drv->format_name);
+            ret = -1;
+            goto out;
+        }
+        if (!proto_drv->create_opts) {
+            error_report("Protocol driver '%s' does not support image creation",
+                         proto_drv->format_name);
+            ret = -1;
+            goto out;
+        }
+        create_opts = qemu_opts_append(create_opts, drv->create_opts);
+        create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
+
+        opts = qemu_opts_create(create_opts, NULL, 0, &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.offset * in.bsz)) {
+            qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
+                                out.offset * out.bsz, &error_abort);
+        } else {
+            qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size
+                                - in.offset * in.bsz + out.offset * out.bsz,
+                                &error_abort);
+        }
+
+        ret = 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 = -1;
+            goto out;
+        }
     }
 
     blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
@@ -4089,9 +4157,22 @@  static int img_dd(int argc, char **argv)
         in_pos = in.offset * in.bsz;
     }
 
+    if (bs) {
+        if (!(dd.flags & C_SEEK)) {
+            blk_truncate(blk2, 0);
+        }
+        if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
+                                  size < in.offset * in.bsz)) {
+            blk_truncate(blk2, out.offset * out.bsz);
+        } else {
+            blk_truncate(blk2,
+                         size - in.offset * in.bsz + out.offset * out.bsz);
+        }
+    }
+
     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) {
@@ -4124,6 +4205,7 @@  out:
     qemu_opts_free(create_opts);
     blk_unref(blk1);
     blk_unref(blk2);
+    bdrv_unref(bs);
     g_free(in.filename);
     g_free(out.filename);
     g_free(in.buf);
diff --git a/qemu-img.texi b/qemu-img.texi
index 174aae3..4dfda19 100644
--- a/qemu-img.texi
+++ b/qemu-img.texi
@@ -153,6 +153,8 @@  sets the input file
 sets the output file
 @item skip=@var{blocks}
 sets the number of input blocks to skip
+@item seek=@var{blocks}
+sets the number of output blocks to skip
 @end table
 
 Command description:
@@ -326,7 +328,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}] 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}] 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..fe8de62
--- /dev/null
+++ b/tests/qemu-iotests/161
@@ -0,0 +1,72 @@ 
+#! /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 <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_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" -O "$IMGFMT" \
+        2> /dev/null
+    TEST_IMG="$TEST_IMG.out" _check_test_img
+    dd if="$TEST_IMG" of="$TEST_IMG.out.dd" seek="$seek" 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 a042f48..757e002 100644
--- a/tests/qemu-iotests/group
+++ b/tests/qemu-iotests/group
@@ -160,3 +160,4 @@ 
 158 rw auto quick
 159 rw auto quick
 160 rw auto quick
+161 rw auto quick