Skip to contentSkip to conversation

qemu-img: add conv=notrunc option to dd

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

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-img: add conv=notrunc option to dd

This adds the conv=notrunc option to dd which tells dd to not truncate the output.

For the time being we make it mandatory to specify conv=notrunc.

---
 qemu-img-cmds.hx       |  4 ++--
 qemu-img.c             | 31 ++++++++++++++++++++++++++++---
 qemu-img.texi          | 15 +++++++++------
 tests/qemu-iotests/158 |  2 +-
 tests/qemu-iotests/159 |  3 ++-
 tests/qemu-iotests/160 |  7 ++++---
 6 files changed, 46 insertions(+), 16 deletions(-)
qemu-img-cmds.hx +2 -2
index f054599..18685ac 100644
@@ -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] [conv=notrunc] 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}] [conv=notrunc] if=@var{input} of=@var{output}
 ETEXI
 
 DEF("info", img_info,
qemu-img.c +28 -3
index 3adec86..16b56b8 100644
@@ -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"
+           "  'conv=notrunc' do not truncate the output file\n";
 
     printf("%s\nSupported formats:", help_msg);
     bdrv_iterate_format(format_print, NULL);
@@ -3807,10 +3808,12 @@ out:
 #define C_IF      04
 #define C_OF      010
 #define C_SKIP    020
+#define C_CONV    040
 
 struct DdInfo {
     unsigned int flags;
     int64_t count;
+    unsigned int conv;
 };
 
 struct DdIo {
@@ -3894,6 +3897,21 @@ static int img_dd_skip(const char *arg,
     return 0;
 }
 
+#define C_NOTRUNC 01
+
+static int img_dd_conv(const char *arg,
+                       struct DdIo *in, struct DdIo *out,
+                       struct DdInfo *dd)
+{
+    if (!strcmp(arg, "notrunc")) {
+        dd->conv |= C_NOTRUNC;
+        return 0;
+    } else {
+        error_report("invalid conversion: '%s'", arg);
+        return 1;
+    }
+}
+
 static int img_dd(int argc, char **argv)
 {
     int ret = 0;
@@ -3909,10 +3927,11 @@ static int img_dd(int argc, char **argv)
     const char *out_fmt = "raw";
     const char *fmt = NULL;
     int64_t size = 0;
-    int64_t block_count = 0, out_pos, in_pos;
+    int64_t out_pos, in_pos;
     struct DdInfo dd = {
         .flags = 0,
         .count = 0,
+        .conv = 0
     };
     struct DdIo in = {
         .bsz = 512, /* Block size is by default 512 bytes */
@@ -3933,6 +3952,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 },
+        { "conv", img_dd_conv, C_CONV },
         { NULL, NULL, 0 }
     };
     const struct option long_options[] = {
@@ -3997,6 +4017,11 @@ static int img_dd(int argc, char **argv)
         g_free(arg);
         arg = NULL;
     }
+    if (!(dd.conv & C_NOTRUNC)) {
+        error_report("conv=notrunc not specified");
+        ret = -1;
+        goto out;
+    }
 
     if (!(dd.flags & C_IF && dd.flags & C_OF)) {
         error_report("Must specify both input and output files");
@@ -4091,7 +4116,7 @@ static int img_dd(int argc, char **argv)
 
     in.buf = g_new(uint8_t, in.bsz);
 
-    for (out_pos = 0; in_pos < size; block_count++) {
+    for (out_pos = 0; in_pos < size; ) {
         int in_ret, out_ret;
 
         if (in_pos + in.bsz > size) {
qemu-img.texi +9 -6
index 174aae3..891af14 100644
@@ -144,15 +144,18 @@ Parameters to dd subcommand:
 @table @option
 
 @item bs=@var{block_size}
-defines the block size
+Defines the block size.
 @item count=@var{blocks}
-sets the number of input blocks to copy
+Sets the number of input blocks to copy.
 @item if=@var{input}
-sets the input file
+Sets the input file.
 @item of=@var{output}
-sets the output file
+Sets the output file. dd truncates the output file to zero if 'conv=notrunc'
+is not specified.
 @item skip=@var{blocks}
-sets the number of input blocks to skip
+Sets the number of input blocks to skip.
+@item conv=notrunc
+Makes dd not truncate output file to zero.
 @end table
 
 Command description:
@@ -326,7 +329,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}] [conv=notrunc] 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.
tests/qemu-iotests/158 +1 -1
index 5b335db..63e2a10 100755
@@ -53,7 +53,7 @@ $QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io
 echo
 echo "== Converting the image with dd =="
 
-$QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" -O "$IMGFMT"
+$QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" conv=notrunc -O "$IMGFMT"
 TEST_IMG="$TEST_IMG.out" _check_test_img
 
 echo
tests/qemu-iotests/159 +2 -1
index 825f05f..d68a19f 100755
@@ -55,7 +55,8 @@ for bs in $TEST_SIZES; do
     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 dd if="$TEST_IMG" of="$TEST_IMG.out" bs=$bs conv=notrunc \
+        -O "$IMGFMT"
     TEST_IMG="$TEST_IMG.out" _check_test_img
 
     echo
tests/qemu-iotests/160 +4 -3
index 5c910e5..53b3c30 100755
@@ -55,10 +55,11 @@ for skip in $TEST_SKIP_BLOCKS; do
     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 dd if="$TEST_IMG" of="$TEST_IMG.out" skip="$skip" conv=notrunc  \
+        -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
+    dd if="$TEST_IMG" of="$TEST_IMG.out.dd" skip="$skip" conv=notrunc \
+        status=none
 
     echo
     echo "== Compare the images with qemu-img compare =="
Signature · 1 line
2.9.2
#1 lore raw
Quoted 81 lines · Reda Sallahi

This adds the conv=notrunc option to dd which tells dd to not truncate the output.

For the time being we make it mandatory to specify conv=notrunc.

---
 qemu-img-cmds.hx       |  4 ++--
 qemu-img.c             | 31 ++++++++++++++++++++++++++++---
 qemu-img.texi          | 15 +++++++++------
 tests/qemu-iotests/158 |  2 +-
 tests/qemu-iotests/159 |  3 ++-
 tests/qemu-iotests/160 |  7 ++++---
 6 files changed, 46 insertions(+), 16 deletions(-)
qemu-img-cmds.hx +2 -2
index f054599..18685ac 100644
@@ -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] [conv=notrunc] 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}] [conv=notrunc] if=@var{input} of=@var{output}
 ETEXI
 
 DEF("info", img_info,
qemu-img.c +19 -1
index 3adec86..16b56b8 100644
@@ -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"
+           "  'conv=notrunc' do not truncate the output file\n";
 
     printf("%s\nSupported formats:", help_msg);
     bdrv_iterate_format(format_print, NULL);
@@ -3807,10 +3808,12 @@ out:
 #define C_IF      04
 #define C_OF      010
 #define C_SKIP    020
+#define C_CONV    040
 
 struct DdInfo {
     unsigned int flags;
     int64_t count;
+    unsigned int conv;
 };
 
 struct DdIo {
@@ -3894,6 +3897,21 @@ static int img_dd_skip(const char *arg,
     return 0;
 }
 
+#define C_NOTRUNC 01
+
+static int img_dd_conv(const char *arg,
+                       struct DdIo *in, struct DdIo *out,
+                       struct DdInfo *dd)
+{
+    if (!strcmp(arg, "notrunc")) {
+        dd->conv |= C_NOTRUNC;
+        return 0;
+    } else {
+        error_report("invalid conversion: '%s'", arg);
+        return 1;
+    }
+}
+
 static int img_dd(int argc, char **argv)
 {
     int ret = 0;

qemu-img.c static int img_dd(int argc, char **argv) · line 3930

Quoted 5 lines
qemu-img.c +1 -1
@@ -3909,10 +3927,11 @@ static int img_dd(int argc, char **argv)
     const char *out_fmt = "raw";
     const char *fmt = NULL;
     int64_t size = 0;
-    int64_t block_count = 0, out_pos, in_pos;
+    int64_t out_pos, in_pos;

This change is not related to this patch. It should probably done by another patch, or removed.

Quoted 16 lines
qemu-img.c +2 -0
     struct DdInfo dd = {
         .flags = 0,
         .count = 0,
+        .conv = 0
     };
     struct DdIo in = {
         .bsz = 512, /* Block size is by default 512 bytes */
@@ -3933,6 +3952,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 },
+        { "conv", img_dd_conv, C_CONV },
         { NULL, NULL, 0 }
     };
     const struct option long_options[] = {

qemu-img.c static int img_dd(int argc, char **argv) · line 4021

Quoted 5 lines
qemu-img.c +2 -0
@@ -3997,6 +4017,11 @@ static int img_dd(int argc, char **argv)
         g_free(arg);
         arg = NULL;
     }
+    if (!(dd.conv & C_NOTRUNC)) {
+        error_report("conv=notrunc not specified");

A comment and/or a more detailed error message explaining the situation why we do this now would be necessary, so that in the future we'll remember to do the right thing once it's decided.

Quoted 30 lines
qemu-img.c +4 -1
+        ret = -1;
+        goto out;
+    }
 
     if (!(dd.flags & C_IF && dd.flags & C_OF)) {
         error_report("Must specify both input and output files");
@@ -4091,7 +4116,7 @@ static int img_dd(int argc, char **argv)
 
     in.buf = g_new(uint8_t, in.bsz);
 
-    for (out_pos = 0; in_pos < size; block_count++) {
+    for (out_pos = 0; in_pos < size; ) {
         int in_ret, out_ret;
 
         if (in_pos + in.bsz > size) {
qemu-img.texi +2 -3
index 174aae3..891af14 100644
@@ -144,15 +144,18 @@ Parameters to dd subcommand:
 @table @option
 
 @item bs=@var{block_size}
-defines the block size
+Defines the block size.
 @item count=@var{blocks}
-sets the number of input blocks to copy
+Sets the number of input blocks to copy.
 @item if=@var{input}
-sets the input file

qemu-img.texi Parameters to dd subcommand: · line 158

Quoted 10 lines
qemu-img.texi +6 -2
+Sets the input file.
 @item of=@var{output}
-sets the output file
+Sets the output file. dd truncates the output file to zero if 'conv=notrunc'
+is not specified.
 @item skip=@var{blocks}
-sets the number of input blocks to skip
+Sets the number of input blocks to skip.
+@item conv=notrunc
+Makes dd not truncate output file to zero.

Capitalizing other sentences should go as a sepearte patch.

Quoted 61 lines
qemu-img.texi +1 -1
 @end table
 
 Command description:
@@ -326,7 +329,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}] [conv=notrunc] 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.
tests/qemu-iotests/158 +1 -1
index 5b335db..63e2a10 100755
@@ -53,7 +53,7 @@ $QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io
 echo
 echo "== Converting the image with dd =="
 
-$QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" -O "$IMGFMT"
+$QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" conv=notrunc -O "$IMGFMT"
 TEST_IMG="$TEST_IMG.out" _check_test_img
 
 echo
tests/qemu-iotests/159 +2 -1
index 825f05f..d68a19f 100755
@@ -55,7 +55,8 @@ for bs in $TEST_SIZES; do
     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 dd if="$TEST_IMG" of="$TEST_IMG.out" bs=$bs conv=notrunc \
+        -O "$IMGFMT"
     TEST_IMG="$TEST_IMG.out" _check_test_img
 
     echo
tests/qemu-iotests/160 +4 -3
index 5c910e5..53b3c30 100755
@@ -55,10 +55,11 @@ for skip in $TEST_SKIP_BLOCKS; do
     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 dd if="$TEST_IMG" of="$TEST_IMG.out" skip="$skip" conv=notrunc  \
+        -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
+    dd if="$TEST_IMG" of="$TEST_IMG.out.dd" skip="$skip" conv=notrunc \
+        status=none
 
     echo
     echo "== Compare the images with qemu-img compare =="
Signature · 1 line
2.9.2

Apart from above, the conv=notrunc code looks good to me. Thanks!

Fam