Skip to contentSkip to conversation

qemu-img: add the 'dd' subcommand

PATCH Superseded Reda Sallahi 2 participants 2 messages Aug 9, 2016

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 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 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                       | 302 ++++++++++++++++++++++++++++++++++++++-
 qemu-img.texi                    |  25 ++++
 tests/qemu-iotests/158           |  68 +++++++++
 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/group         |   2 +
 9 files changed, 583 insertions(+), 1 deletion(-)
 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 +301 -1

314 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 +68 -0

68 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..cef5222 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/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
#1 lore raw
Quoted 31 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 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 14 lines
 qemu-img-cmds.hx                 |   6 +
 qemu-img.c                       | 302 ++++++++++++++++++++++++++++++++++++++-
 qemu-img.texi                    |  25 ++++
 tests/qemu-iotests/158           |  68 +++++++++
 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/group         |   2 +
 9 files changed, 583 insertions(+), 1 deletion(-)
 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

Looks good overall, just some minor comments below (and maybe you still want to replace {in,out}count by {in,out}_position anyway).

[...]

Quoted 4 lines
qemu-img.c +0 -0
index d2865a5..10aaf0e 100644

[...]

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

[...]

qemu-img.c out: · line 4028

Quoted 8 lines
qemu-img.c +8 -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 * in.bsz < size) {

dd.count * in.bsz can overflow, there should probably be a check for that before this point.

Quoted 2 lines
qemu-img.c +2 -0
+        size = dd.count * in.bsz;
+    }

[...]

Quoted 6 lines
tests/qemu-iotests/158 new file +0 -0
new file mode 100755
index 0000000..48972c8
@@ -0,0 +1,68 @@

[...]

tests/qemu-iotests/158 line 58

Quoted 6 lines
tests/qemu-iotests/158 new file +6 -0
+echo
+echo "== Converting the image with dd =="
+
+$QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" -O "$IMGFMT"
+$QEMU_IMG check "$TEST_IMG.out" -f "$IMGFMT" 2>&1  | _filter_testdir | \
+    _filter_qemu_img_check

A simpler way of doing this would be:

TEST_IMG="$TEST_IMG.out" _check_test_img

[...]

Quoted 6 lines
tests/qemu-iotests/159 new file +0 -0
new file mode 100755
index 0000000..e55d942
@@ -0,0 +1,70 @@

[...]

Quoted 5 lines
tests/qemu-iotests/159 new file +5 -0
+for bs in $TEST_SIZES; do
+    echo
+    echo "== Creating image =="
+
+    size=1M

tests/qemu-iotests/159 line 60

Quoted 10 lines
tests/qemu-iotests/159 new file +10 -0
+    _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 a block size of $bs =="
+
+    $QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" bs=$bs -O "$IMGFMT"
+    $QEMU_IMG check "$TEST_IMG.out" -f "$IMGFMT" 2>&1  | _filter_testdir | \
+        _filter_qemu_img_check

Again, TEST_IMG="$TEST_IMG.out" _check_test_img would be simpler.

Quoted 10 lines
tests/qemu-iotests/159 new file +10 -0
+    echo
+    echo "== Compare the images with qemu-img compare =="
+
+    $QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.out"
+done
+
+echo
+echo "*** done"
+rm -f "$seq.full"
+status=0

[...]

Quoted 7 lines
tests/qemu-iotests/common.filter +0 -0
index 3ab6e4d..cef5222 100644
@@ -44,6 +44,15 @@ _filter_imgfmt()
     sed -e "s#$IMGFMT#IMGFMT#g"
 }

tests/qemu-iotests/common.filter _filter_imgfmt() · line 55

Quoted 10 lines
tests/qemu-iotests/common.filter +9 -0
 
+# 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'
+}
+

If you use the TEST_IMG="$TEST_IMG.out" _check_test_img shorthand I've proposed above, then this function will no longer be necessary.

If you don't, then please make use of this function in _check_test_img. That function contains exactly the same code (probably not by chance), so you should replace that code by a call to this function.

Max