Skip to contentSkip to conversation

qemu-img: add the 'dd' subcommand

PATCH In review Reda Sallahi 3 participants 3 messages Aug 10, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/1 qemu-img: add the 'dd' subcommand

This patch adds a basic dd subcommand analogous to dd(1) to qemu-img.

For the start, this implements the bs, if, of and count options and requires both if and of to be specified (no stdin/stdout if not specified) and doesn't support tty, pipes, etc.

The image format must be specified with -O for the output if the raw format is not the intended one.

Two tests are added to test qemu-img dd.

---
Changes from v8:
* Add an overflow check.
Changes from v7:
* Remove a C99-style for loop.
Changes from v6:
* Remove get_size() to use qemu_strtosz_suffix() instead.
* Type changes for some fields in DdIo and DdInfo. Changes from v5:
* Add dd sections on qemu-img.texi.
Changes from v4:
* Fix the exit status.
Changes from v3:
* Delete an unused (so far) field in DdIo. Changes from v2:
* Add copyright headers to new files.
Changes from v1:
* Removal of dead code.
* Fix a memory leak.
* Complete the cleanup function in the test cases.

 qemu-img-cmds.hx                 |   6 +
 qemu-img.c                       | 303 ++++++++++++++++++++++++++++++++++++++-
 qemu-img.texi                    |  25 ++++
 tests/qemu-iotests/158           |  67 +++++++++
 tests/qemu-iotests/158.out       |  15 ++
 tests/qemu-iotests/159           |  70 +++++++++
 tests/qemu-iotests/159.out       |  87 +++++++++++
 tests/qemu-iotests/common.filter |   9 ++
 tests/qemu-iotests/common.rc     |   5 +-
 tests/qemu-iotests/group         |   2 +
 10 files changed, 584 insertions(+), 5 deletions(-)
 create mode 100755 tests/qemu-iotests/158
 create mode 100644 tests/qemu-iotests/158.out
 create mode 100755 tests/qemu-iotests/159
 create mode 100644 tests/qemu-iotests/159.out
qemu-img-cmds.hx +6 -0
index 7e95b2d..03bdd7a 100644
@@ -45,6 +45,12 @@ STEXI
 @item convert [--object @var{objectdef}] [--image-opts] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
+DEF("dd", img_dd,
+    "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=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}
+ETEXI
+
 DEF("info", img_info,
     "info [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] [--backing-chain] filename")
 STEXI
qemu-img.c +302 -1

315 lines not on this page · raw message

qemu-img.texi +25 -0
index 449a19c..880293a 100644
@@ -139,6 +139,20 @@ Parameters to convert subcommand:
 Skip the creation of the target volume
 @end table
 
+Parameters to dd subcommand:
+
+@table @option
+
+@item bs=@var{block_size}
+defines the block size
+@item count=@var{blocks}
+sets the number of input blocks to copy
+@item if=@var{input}
+sets the input file
+@item of=@var{output}
+sets the output file
+@end table
+
 Command description:
 
 @table @option
@@ -310,6 +324,17 @@ 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}
+
+Dd copies from @var{input} file to @var{output} file converting it from
+@var{fmt} format to @var{output_fmt} format.
+
+The data is by default read and written using blocks of 512 bytes but can be
+modified by specifying @var{block_size}. If count=@var{blocks} is specified
+dd will stop reading input after reading @var{blocks} input blocks.
+
+The size syntax is similar to dd(1)'s size syntax.
+
 @item info [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename}
 
 Give information about the disk image @var{filename}. Use it in
tests/qemu-iotests/158 new file +67 -0

67 lines not on this page · raw message

tests/qemu-iotests/158.out new file +15 -0
new file mode 100644
index 0000000..58bfd85
@@ -0,0 +1,15 @@
+QA output created by 158
+
+== 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 ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+*** done
tests/qemu-iotests/159 new file +70 -0

70 lines not on this page · raw message

tests/qemu-iotests/159.out new file +87 -0

87 lines not on this page · raw message

tests/qemu-iotests/common.filter +9 -0
index 3ab6e4d..240ed06 100644
@@ -44,6 +44,15 @@ _filter_imgfmt()
     sed -e "s#$IMGFMT#IMGFMT#g"
 }
 
+# Replace error message when the format is not supported and delete
+# the output lines after the first one
+_filter_qemu_img_check()
+{
+    sed -e '/allocated.*fragmented.*compressed clusters/d' \
+        -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
+        -e '/Image end offset: [0-9]\+/d'
+}
+
 # Removes \r from messages
 _filter_win32()
 {
tests/qemu-iotests/common.rc +1 -4
index 306b00c..126bd67 100644
@@ -234,10 +234,7 @@ _check_test_img()
         else
             $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
         fi
-    ) | _filter_testdir | \
-        sed -e '/allocated.*fragmented.*compressed clusters/d' \
-            -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
-            -e '/Image end offset: [0-9]\+/d'
+    ) | _filter_testdir | _filter_qemu_img_check
 }
 
 _img_info()
tests/qemu-iotests/group +2 -0
index 3a3973e..ec712bc 100644
@@ -157,3 +157,5 @@
 155 rw auto
 156 rw auto quick
 157 auto
+158 rw auto quick
+159 rw auto quick
Signature · 1 line
2.9.2
Reviewed-by on v9 1/1
#1 lore raw
Quoted 33 lines · Reda Sallahi

This patch adds a basic dd subcommand analogous to dd(1) to qemu-img.

For the start, this implements the bs, if, of and count options and requires both if and of to be specified (no stdin/stdout if not specified) and doesn't support tty, pipes, etc.

The image format must be specified with -O for the output if the raw format is not the intended one.

Two tests are added to test qemu-img dd.

---
Changes from v8:
* Add an overflow check.
Changes from v7:
* Remove a C99-style for loop.
Changes from v6:
* Remove get_size() to use qemu_strtosz_suffix() instead.
* Type changes for some fields in DdIo and DdInfo. Changes from v5:
* Add dd sections on qemu-img.texi.
Changes from v4:
* Fix the exit status.
Changes from v3:
* Delete an unused (so far) field in DdIo. Changes from v2:
* Add copyright headers to new files.
Changes from v1:
* Removal of dead code.
* Fix a memory leak.
* Complete the cleanup function in the test cases.

Quoted 15 lines
 qemu-img-cmds.hx                 |   6 +
 qemu-img.c                       | 303 ++++++++++++++++++++++++++++++++++++++-
 qemu-img.texi                    |  25 ++++
 tests/qemu-iotests/158           |  67 +++++++++
 tests/qemu-iotests/158.out       |  15 ++
 tests/qemu-iotests/159           |  70 +++++++++
 tests/qemu-iotests/159.out       |  87 +++++++++++
 tests/qemu-iotests/common.filter |   9 ++
 tests/qemu-iotests/common.rc     |   5 +-
 tests/qemu-iotests/group         |   2 +
 10 files changed, 584 insertions(+), 5 deletions(-)
 create mode 100755 tests/qemu-iotests/158
 create mode 100644 tests/qemu-iotests/158.out
 create mode 100755 tests/qemu-iotests/159
 create mode 100644 tests/qemu-iotests/159.out
#2 lore raw
In reply to Reda Sallahi
Quoted 48 lines · Reda Sallahi

This patch adds a basic dd subcommand analogous to dd(1) to qemu-img.

For the start, this implements the bs, if, of and count options and requires both if and of to be specified (no stdin/stdout if not specified) and doesn't support tty, pipes, etc.

The image format must be specified with -O for the output if the raw format is not the intended one.

Two tests are added to test qemu-img dd.

---
Changes from v8:
* Add an overflow check.
Changes from v7:
* Remove a C99-style for loop.
Changes from v6:
* Remove get_size() to use qemu_strtosz_suffix() instead.
* Type changes for some fields in DdIo and DdInfo. Changes from v5:
* Add dd sections on qemu-img.texi.
Changes from v4:
* Fix the exit status.
Changes from v3:
* Delete an unused (so far) field in DdIo. Changes from v2:
* Add copyright headers to new files.
Changes from v1:
* Removal of dead code.
* Fix a memory leak.
* Complete the cleanup function in the test cases.

 qemu-img-cmds.hx                 |   6 +
 qemu-img.c                       | 303 ++++++++++++++++++++++++++++++++++++++-
 qemu-img.texi                    |  25 ++++
 tests/qemu-iotests/158           |  67 +++++++++
 tests/qemu-iotests/158.out       |  15 ++
 tests/qemu-iotests/159           |  70 +++++++++
 tests/qemu-iotests/159.out       |  87 +++++++++++
 tests/qemu-iotests/common.filter |   9 ++
 tests/qemu-iotests/common.rc     |   5 +-
 tests/qemu-iotests/group         |   2 +
 10 files changed, 584 insertions(+), 5 deletions(-)
 create mode 100755 tests/qemu-iotests/158
 create mode 100644 tests/qemu-iotests/158.out
 create mode 100755 tests/qemu-iotests/159
 create mode 100644 tests/qemu-iotests/159.out

[...]

Quoted 4 lines
qemu-img.c +0 -0
index d2865a5..034baf7 100644

[...]

Quoted 1 line
qemu-img.c +0 -0
@@ -3794,6 +3801,300 @@ out:

[...]

qemu-img.c out: · line 4030

Quoted 10 lines
qemu-img.c +10 -0
+    size = blk_getlength(blk1);
+    if (size < 0) {
+        error_report("Failed to get size for '%s'", in.filename);
+        ret = -1;
+        goto out;
+    }
+
+    if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
+        dd.count * in.bsz < size) {
+        size = dd.count * in.bsz;

At first I wanted to object and say that we should throw an error if dd.count * in.bsz overflows. But you're right, since all bs and count do is limit the length of the data to copy, we can just consider them "infinite" (i.e. no limit) if there product overflows. Clever.

Thanks, Reda, I've applied this patch to my block-next branch for the qemu 2.8 development window:

https://github.com/XanClic/qemu/commits/block-next

Max