Skip to contentSkip to conversation

Let 'qemu-img convert --bitmaps' skip inconsistent bitmaps

PATCH Superseded Eric Blake 2 participants 9 messages Jul 8, 2021 – Jul 9, 2021 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

cover Let 'qemu-img convert --bitmaps' skip inconsistent bitmaps

This is mostly a convenience factor as one could already use 'qemu-img info' to learn which bitmaps are broken and then 'qemu-img bitmap --remove' to nuke them before calling 'qemu-img convert --bitmaps', but it does have the advantage that the copied file is usable without extra efforts and the broken bitmap is not deleted from the source file.

Eric Blake (2):
iotests: Improve and rename test 291 to qemu-img-bitmap
qemu-img: Add --skip-broken for 'convert --bitmaps'

 docs/tools/qemu-img.rst                       |  8 +++-
 block/dirty-bitmap.c                          |  2 +-
 qemu-img.c                                    | 20 +++++++-
 .../{291 => tests/qemu-img-bitmaps}           | 17 ++++++-
 .../{291.out => tests/qemu-img-bitmaps.out}   | 46 ++++++++++++++++++-
 5 files changed, 87 insertions(+), 6 deletions(-)
 rename tests/qemu-iotests/{291 => tests/qemu-img-bitmaps} (88%)
 rename tests/qemu-iotests/{291.out => tests/qemu-img-bitmaps.out} (76%)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/2 iotests: Improve and rename test 291 to qemu-img-bitmap

Enhance the test to demonstrate behavior of qemu-img with a qcow2 image containing an inconsistent bitmap, and rename it now that we support useful iotest names.

While at it, fix a missing newline in the error message thus exposed.

---
 block/dirty-bitmap.c                          |  2 +-
 .../{291 => tests/qemu-img-bitmaps}           | 13 +++++++-
 .../{291.out => tests/qemu-img-bitmaps.out}   | 32 ++++++++++++++++++-
 3 files changed, 44 insertions(+), 3 deletions(-)
 rename tests/qemu-iotests/{291 => tests/qemu-img-bitmaps} (92%)
 rename tests/qemu-iotests/{291.out => tests/qemu-img-bitmaps.out} (82%)
block/dirty-bitmap.c +1 -1
index 68d295d6e3ed..0ef46163e3ea 100644
@@ -193,7 +193,7 @@ int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
         error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
                    bitmap->name);
         error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
-                          " this bitmap from disk");
+                          " this bitmap from disk\n");
         return -1;
     }
 
tests/qemu-iotests/tests/qemu-img-bitmaps renamed from tests/qemu-iotests/291 +12 -1
similarity index 92%
rename from tests/qemu-iotests/291
rename to tests/qemu-iotests/tests/qemu-img-bitmaps
index 20efb080a6c0..76cd9e31e850 100755
@@ -3,7 +3,7 @@
 #
 # Test qemu-img bitmap handling
 #
-# Copyright (C) 2018-2020 Red Hat, Inc.
+# Copyright (C) 2018-2021 Red Hat, Inc.
 #
 # 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
@@ -32,6 +32,7 @@ _cleanup()
 trap "_cleanup; exit \$status" 0 1 2 3 15
 
 # get standard environment, filters and checks
+cd ..
 . ./common.rc
 . ./common.filter
 . ./common.nbd
@@ -129,6 +130,16 @@ $QEMU_IMG map --output=json --image-opts \
 
 nbd_server_stop
 
+echo
+echo "=== Check handling of inconsistent bitmap ==="
+echo
+
+$QEMU_IO -c abort "$TEST_IMG" 2>/dev/null
+$QEMU_IMG bitmap --add "$TEST_IMG" b4
+$QEMU_IMG bitmap --remove "$TEST_IMG" b1
+_img_info --format-specific | _filter_irrelevant_img_info
+$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"
+
 # success, all done
 echo '*** done'
 rm -f $seq.full
tests/qemu-iotests/tests/qemu-img-bitmaps.out renamed from tests/qemu-iotests/291.out +31 -1
similarity index 82%
rename from tests/qemu-iotests/291.out
rename to tests/qemu-iotests/tests/qemu-img-bitmaps.out
index 018d6b103f87..17b34eaed30f 100644
@@ -1,4 +1,4 @@
-QA output created by 291
+QA output created by qemu-img-bitmaps
 
 === Initial image setup ===
 
@@ -115,4 +115,34 @@ Format specific information:
 [{ "start": 0, "length": 2097152, "depth": 0, "present": true, "zero": false, "data": true, "offset": OFFSET},
 { "start": 2097152, "length": 1048576, "depth": 0, "present": false, "zero": false, "data": false},
 { "start": 3145728, "length": 7340032, "depth": 0, "present": true, "zero": false, "data": true, "offset": OFFSET}]
+
+=== Check handling of inconsistent bitmap ===
+
+image: TEST_DIR/t.IMGFMT
+file format: IMGFMT
+virtual size: 10 MiB (10485760 bytes)
+cluster_size: 65536
+backing file: TEST_DIR/t.IMGFMT.base
+backing file format: IMGFMT
+Format specific information:
+    bitmaps:
+        [0]:
+            flags:
+                [0]: in-use
+                [1]: auto
+            name: b2
+            granularity: 65536
+        [1]:
+            flags:
+                [0]: in-use
+            name: b0
+            granularity: 65536
+        [2]:
+            flags:
+                [0]: auto
+            name: b4
+            granularity: 65536
+    corrupt: false
+qemu-img: Failed to populate bitmap b0: Bitmap 'b0' is inconsistent and cannot be used
+Try block-dirty-bitmap-remove to delete this bitmap from disk
 *** done
Signature · 1 line
2.31.1
Tested-by Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] iotests: Improve and rename test 291 to qemu-img-bitmap

Quoted 55 lines · Eric Blake

Enhance the test to demonstrate behavior of qemu-img with a qcow2 image containing an inconsistent bitmap, and rename it now that we support useful iotest names.

While at it, fix a missing newline in the error message thus exposed.

---
  block/dirty-bitmap.c                          |  2 +-
  .../{291 => tests/qemu-img-bitmaps}           | 13 +++++++-
  .../{291.out => tests/qemu-img-bitmaps.out}   | 32 ++++++++++++++++++-
  3 files changed, 44 insertions(+), 3 deletions(-)

rename tests/qemu-iotests/{291 => tests/qemu-img-bitmaps} (92%) rename tests/qemu-iotests/{291.out => tests/qemu-img-bitmaps.out} (82%)

block/dirty-bitmap.c +1 -1
index 68d295d6e3ed..0ef46163e3ea 100644
@@ -193,7 +193,7 @@ int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
          error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
                     bitmap->name);
          error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
-                          " this bitmap from disk");
+                          " this bitmap from disk\n");
          return -1;
      }
 
tests/qemu-iotests/tests/qemu-img-bitmaps renamed from tests/qemu-iotests/291 +2 -1
similarity index 92%
rename from tests/qemu-iotests/291
rename to tests/qemu-iotests/tests/qemu-img-bitmaps
index 20efb080a6c0..76cd9e31e850 100755
@@ -3,7 +3,7 @@
  #
  # Test qemu-img bitmap handling
  #
-# Copyright (C) 2018-2020 Red Hat, Inc.
+# Copyright (C) 2018-2021 Red Hat, Inc.
  #
  # 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
@@ -32,6 +32,7 @@ _cleanup()
  trap "_cleanup; exit \$status" 0 1 2 3 15
 
  # get standard environment, filters and checks
+cd ..
  . ./common.rc
  . ./common.filter
  . ./common.nbd
@@ -129,6 +130,16 @@ $QEMU_IMG map --output=json --image-opts \
 
  nbd_server_stop

tests/qemu-iotests/tests/qemu-img-bitmaps $QEMU_IMG map --output=json --image-opts \ · line 141

Quoted 10 lines
tests/qemu-iotests/tests/qemu-img-bitmaps renamed from tests/qemu-iotests/291 +9 -0
 
+echo
+echo "=== Check handling of inconsistent bitmap ==="
+echo
+
+$QEMU_IO -c abort "$TEST_IMG" 2>/dev/null
+$QEMU_IMG bitmap --add "$TEST_IMG" b4
+$QEMU_IMG bitmap --remove "$TEST_IMG" b1
+_img_info --format-specific | _filter_irrelevant_img_info
+$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"

Worth then removing remaining inconsistent bitmaps and try again?

I think you should now remove $TEST_IMG.copy in _cleanup

with squashed in

tests/qemu-iotests/tests/qemu-img-bitmaps +1 -0
@@ -27,6 +27,7 @@ status=1 # failure is the default!
 _cleanup()
 {
     _cleanup_test_img
+    _rm_test_img "$TEST_IMG.copy"
     nbd_server_stop
 }
 trap "_cl
Quoted 53 lines
tests/qemu-iotests/tests/qemu-img-bitmaps renamed from tests/qemu-iotests/291 +1 -0
+
  # success, all done
  echo '*** done'
  rm -f $seq.full
tests/qemu-iotests/tests/qemu-img-bitmaps.out renamed from tests/qemu-iotests/291.out +31 -1
similarity index 82%
rename from tests/qemu-iotests/291.out
rename to tests/qemu-iotests/tests/qemu-img-bitmaps.out
index 018d6b103f87..17b34eaed30f 100644
@@ -1,4 +1,4 @@
-QA output created by 291
+QA output created by qemu-img-bitmaps
 
  === Initial image setup ===
 
@@ -115,4 +115,34 @@ Format specific information:
  [{ "start": 0, "length": 2097152, "depth": 0, "present": true, "zero": false, "data": true, "offset": OFFSET},
  { "start": 2097152, "length": 1048576, "depth": 0, "present": false, "zero": false, "data": false},
  { "start": 3145728, "length": 7340032, "depth": 0, "present": true, "zero": false, "data": true, "offset": OFFSET}]
+
+=== Check handling of inconsistent bitmap ===
+
+image: TEST_DIR/t.IMGFMT
+file format: IMGFMT
+virtual size: 10 MiB (10485760 bytes)
+cluster_size: 65536
+backing file: TEST_DIR/t.IMGFMT.base
+backing file format: IMGFMT
+Format specific information:
+    bitmaps:
+        [0]:
+            flags:
+                [0]: in-use
+                [1]: auto
+            name: b2
+            granularity: 65536
+        [1]:
+            flags:
+                [0]: in-use
+            name: b0
+            granularity: 65536
+        [2]:
+            flags:
+                [0]: auto
+            name: b4
+            granularity: 65536
+    corrupt: false
+qemu-img: Failed to populate bitmap b0: Bitmap 'b0' is inconsistent and cannot be used
+Try block-dirty-bitmap-remove to delete this bitmap from disk
  *** done
Signature · 2 lines
Best regards,
Vladimir
author
#3 lore raw

Re: [PATCH 1/2] iotests: Improve and rename test 291 to qemu-img-bitmap

Quoted 14 lines · Vladimir Sementsov-Ogievskiy

Review

Quoted 11 lines

+++ b/tests/qemu-iotests/tests/qemu-img-bitmaps

(quoted diff) +9 -0
+echo
+echo "=== Check handling of inconsistent bitmap ==="
+echo
+
+$QEMU_IO -c abort "$TEST_IMG" 2>/dev/null
+$QEMU_IMG bitmap --add "$TEST_IMG" b4
+$QEMU_IMG bitmap --remove "$TEST_IMG" b1
+_img_info --format-specific | _filter_irrelevant_img_info
+$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"

Worth then removing remaining inconsistent bitmaps and try again?

Quoted 1 line

I think you should now remove $TEST_IMG.copy in _cleanup

$TEST_IMG.copy isn't created on failure (or if it is, that in itself is a problem we should be avoiding), so as written, there was nothing that should have needed cleaning up until patch 2. But your idea (here and in patch 2) of demonstrating manual cleanup for recovery (in addition to the goal of patch 2 of skipping broken bitmaps in the first place) is reasonable, so I'll incorporate that into v2.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#4 lore raw

Re: [PATCH 1/2] iotests: Improve and rename test 291 to qemu-img-bitmap

Quoted 15 lines · Eric Blake
Quoted 14 lines · Vladimir Sementsov-Ogievskiy

Review

Quoted 11 lines

+++ b/tests/qemu-iotests/tests/qemu-img-bitmaps

(quoted diff) +9 -0
+echo
+echo "=== Check handling of inconsistent bitmap ==="
+echo
+
+$QEMU_IO -c abort "$TEST_IMG" 2>/dev/null
+$QEMU_IMG bitmap --add "$TEST_IMG" b4
+$QEMU_IMG bitmap --remove "$TEST_IMG" b1
+_img_info --format-specific | _filter_irrelevant_img_info
+$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"

Worth then removing remaining inconsistent bitmaps and try again?

Quoted 4 lines
Quoted 1 line

I think you should now remove $TEST_IMG.copy in _cleanup

$TEST_IMG.copy isn't created on failure (or if it is, that in itself is a problem we should be avoiding),

Seems that's the case:
./build/qemu-img create -f qcow2 x 1M
./build/qemu-img bitmap --add x b1
./build/qemu-io x
qemu-io> abort
Aborted (core dumped)
./build/qemu-img info x
image: x
file format: qcow2
virtual size: 1 MiB (1048576 bytes)
disk size: 204 KiB
cluster_size: 65536
Format specific information:

    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    bitmaps:
        [0]:
            flags:
                [0]: in-use
                [1]: auto
            name: b1
            granularity: 65536
    refcount bits: 16
    corrupt: false
    extended l2: false

ls y
ls: cannot access 'y': No such file or directory
./build/qemu-img convert --bitmaps -O qcow2 x y
qemu-img: Failed to populate bitmap b1: Bitmap 'b1' is inconsistent and cannot be used Try block-dirty-bitmap-remove to delete this bitmap from disk[root@kvm master]#

# ls y
y
./build/qemu-img info y
image: y
file format: qcow2
virtual size: 1 MiB (1048576 bytes)
disk size: 204 KiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    bitmaps:
        [0]:
            flags:
            name: b1
            granularity: 65536
    refcount bits: 16
    corrupt: false
    extended l2: false

WOW! It even contains the bitmap not marked in-use. That's a real bug.

Quoted 6 lines

so as written, there was nothing
that should have needed cleaning up until patch 2. But your idea (here and in patch 2) of demonstrating manual cleanup for recovery (in addition to the goal of patch 2 of skipping broken bitmaps in the first place) is reasonable, so I'll incorporate that into v2.

Signature · 2 lines
Best regards,
Vladimir
author
#5 lore raw

Re: [PATCH 1/2] iotests: Improve and rename test 291 to qemu-img-bitmap

Quoted 16 lines · Vladimir Sementsov-Ogievskiy
Quoted 15 lines · Eric Blake
Quoted 14 lines · Vladimir Sementsov-Ogievskiy

Review

Quoted 11 lines

+++ b/tests/qemu-iotests/tests/qemu-img-bitmaps

(quoted diff) +9 -0
+echo
+echo "=== Check handling of inconsistent bitmap ==="
+echo
+
+$QEMU_IO -c abort "$TEST_IMG" 2>/dev/null
+$QEMU_IMG bitmap --add "$TEST_IMG" b4
+$QEMU_IMG bitmap --remove "$TEST_IMG" b1
+_img_info --format-specific | _filter_irrelevant_img_info
+$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"

Worth then removing remaining inconsistent bitmaps and try again?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I think you should now remove $TEST_IMG.copy in _cleanup

$TEST_IMG.copy isn't created on failure (or if it is, that in itself is a problem we should be avoiding),

Seems that's the case:

Oh. Thanks for checking. Yeah, re-reading the code, we don't attempt to copy the bitmaps until after the rest of the convert operation is done. Which means a fix to pre-validate bitmaps before starting the convert requires more code, but may be the right thing (failing at the very end of a long convert attempt, but leaving the file around, is not ideal compared to failing fast).

Looks like my v2 will be a bit more beefy.

Quoted 25 lines

./build/qemu-img convert --bitmaps -O qcow2 x y
qemu-img: Failed to populate bitmap b1: Bitmap 'b1' is inconsistent and cannot be used Try block-dirty-bitmap-remove to delete this bitmap from disk[root@kvm master]#

# ls y
y
./build/qemu-img info y
image: y
file format: qcow2
virtual size: 1 MiB (1048576 bytes)
disk size: 204 KiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    bitmaps:
        [0]:
            flags:
            name: b1
            granularity: 65536
    refcount bits: 16
    corrupt: false
    extended l2: false
Quoted 1 line

WOW! It even contains the bitmap not marked in-use. That's a real bug.

So we left things dangling with a bitmap that has completely different contents than in the original file. Yeah, that looks like an issue worth improving.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#6 lore raw

PATCH 2/2 qemu-img: Add --skip-broken for 'convert --bitmaps'

In reply to Eric Blake

The point of 'qemu-img convert --bitmaps' is to be a convenience for actions that are already possible through a string of smaller 'qemu-img bitmap' sub-commands. One situation not accounted for already is that if a source image contains an inconsistent bitmap (for example, because a qemu process died abruptly before flushing bitmap state), the user MUST delete those inconsistent bitmaps before anything else useful can be done with the image.

We don't want to delete inconsistent bitmaps by default: although a corrupt bitmap is only a loss of optimization rather than a corruption of user-visible data, it is still nice to require the user to opt in to the fact that they are aware of the loss of the bitmap. Still, requiring the user to check 'qemu-img info' to see whether bitmaps are consistent, then use 'qemu-img bitmap --remove' to remove offenders, all before using 'qemu-img convert', is a lot more work than just adding a knob 'qemu-img convert --bitmaps --skip-broken' which opts in to skipping the broken bitmaps.

---
 docs/tools/qemu-img.rst                       |  8 +++++++-
 qemu-img.c                                    | 20 +++++++++++++++++--
 tests/qemu-iotests/tests/qemu-img-bitmaps     |  4 ++++
 tests/qemu-iotests/tests/qemu-img-bitmaps.out | 14 +++++++++++++
 4 files changed, 43 insertions(+), 3 deletions(-)
docs/tools/qemu-img.rst +7 -1
index 1d8470eada0e..5cf1c764597b 100644
@@ -414,7 +414,7 @@ Command description:
   4
     Error on reading data
 
-.. option:: convert [--object OBJECTDEF] [--image-opts] [--target-image-opts] [--target-is-zero] [--bitmaps] [-U] [-C] [-c] [-p] [-q] [-n] [-f FMT] [-t CACHE] [-T SRC_CACHE] [-O OUTPUT_FMT] [-B BACKING_FILE] [-o OPTIONS] [-l SNAPSHOT_PARAM] [-S SPARSE_SIZE] [-r RATE_LIMIT] [-m NUM_COROUTINES] [-W] FILENAME [FILENAME2 [...]] OUTPUT_FILENAME
+.. option:: convert [--object OBJECTDEF] [--image-opts] [--target-image-opts] [--target-is-zero] [--bitmaps [--skip-broken]] [-U] [-C] [-c] [-p] [-q] [-n] [-f FMT] [-t CACHE] [-T SRC_CACHE] [-O OUTPUT_FMT] [-B BACKING_FILE] [-o OPTIONS] [-l SNAPSHOT_PARAM] [-S SPARSE_SIZE] [-r RATE_LIMIT] [-m NUM_COROUTINES] [-W] FILENAME [FILENAME2 [...]] OUTPUT_FILENAME
 
   Convert the disk image *FILENAME* or a snapshot *SNAPSHOT_PARAM*
   to disk image *OUTPUT_FILENAME* using format *OUTPUT_FMT*. It can
@@ -456,6 +456,12 @@ Command description:
   *NUM_COROUTINES* specifies how many coroutines work in parallel during
   the convert process (defaults to 8).
 
+  Use of ``--bitmaps`` requests that any persistent bitmaps present in
+  the original are also copied to the destination.  If any bitmap is
+  inconsistent in the source, the conversion will fail unless
+  ``--skip-broken`` is also specified to copy only the consistent
+  bitmaps.
+
 .. option:: create [--object OBJECTDEF] [-q] [-f FMT] [-b BACKING_FILE] [-F BACKING_FMT] [-u] [-o OPTIONS] FILENAME [SIZE]
 
   Create the new disk image *FILENAME* of size *SIZE* and format
qemu-img.c +18 -2
index 68a4d298098f..e8b012f39c0c 100644
@@ -82,6 +82,7 @@ enum {
     OPTION_MERGE = 274,
     OPTION_BITMAPS = 275,
     OPTION_FORCE = 276,
+    OPTION_SKIP_BROKEN = 277,
 };
 
 typedef enum OutputFormat {
@@ -2101,7 +2102,8 @@ static int convert_do_copy(ImgConvertState *s)
     return s->ret;
 }
 
-static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst)
+static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst,
+                                bool skip_broken)
 {
     BdrvDirtyBitmap *bm;
     Error *err = NULL;
@@ -2113,6 +2115,10 @@ static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst)
             continue;
         }
         name = bdrv_dirty_bitmap_name(bm);
+        if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
+            warn_report("Skipping inconsistent bitmap %s", name);
+            continue;
+        }
         qmp_block_dirty_bitmap_add(dst->node_name, name,
                                    true, bdrv_dirty_bitmap_granularity(bm),
                                    true, true,
@@ -2167,6 +2173,7 @@ static int img_convert(int argc, char **argv)
     bool force_share = false;
     bool explict_min_sparse = false;
     bool bitmaps = false;
+    bool skip_broken = false;
     int64_t rate_limit = 0;
 
     ImgConvertState s = (ImgConvertState) {
@@ -2188,6 +2195,7 @@ static int img_convert(int argc, char **argv)
             {"salvage", no_argument, 0, OPTION_SALVAGE},
             {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
             {"bitmaps", no_argument, 0, OPTION_BITMAPS},
+            {"skip-broken", no_argument, 0, OPTION_SKIP_BROKEN},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WUr:",
@@ -2316,6 +2324,9 @@ static int img_convert(int argc, char **argv)
         case OPTION_BITMAPS:
             bitmaps = true;
             break;
+        case OPTION_SKIP_BROKEN:
+            skip_broken = true;
+            break;
         }
     }
 
@@ -2323,6 +2334,11 @@ static int img_convert(int argc, char **argv)
         out_fmt = "raw";
     }
 
+    if (skip_broken && !bitmaps) {
+        error_report("Use of --skip-broken requires --bitmaps");
+        goto fail_getopt;
+    }
+
     if (s.compressed && s.copy_range) {
         error_report("Cannot enable copy offloading when -c is used");
         goto fail_getopt;
@@ -2678,7 +2694,7 @@ static int img_convert(int argc, char **argv)
 
     /* Now copy the bitmaps */
     if (bitmaps && ret == 0) {
-        ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs);
+        ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs, skip_broken);
     }
 
 out:
tests/qemu-iotests/tests/qemu-img-bitmaps +4 -0
index 76cd9e31e850..3e1a39bc81e4 100755
@@ -28,6 +28,7 @@ _cleanup()
 {
     _cleanup_test_img
     nbd_server_stop
+    _rm_test_img "$TEST_DIR/t.$IMGFMT.copy"
 }
 trap "_cleanup; exit \$status" 0 1 2 3 15
 
@@ -139,6 +140,9 @@ $QEMU_IMG bitmap --add "$TEST_IMG" b4
 $QEMU_IMG bitmap --remove "$TEST_IMG" b1
 _img_info --format-specific | _filter_irrelevant_img_info
 $QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"
+$QEMU_IMG convert --bitmaps --skip-broken -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"
+TEST_IMG="$TEST_IMG.copy" _img_info --format-specific \
+    | _filter_irrelevant_img_info
 
 # success, all done
 echo '*** done'
tests/qemu-iotests/tests/qemu-img-bitmaps.out +14 -0
index 17b34eaed30f..685bde6d1d63 100644
@@ -145,4 +145,18 @@ Format specific information:
     corrupt: false
 qemu-img: Failed to populate bitmap b0: Bitmap 'b0' is inconsistent and cannot be used
 Try block-dirty-bitmap-remove to delete this bitmap from disk
+qemu-img: warning: Skipping inconsistent bitmap b0
+qemu-img: warning: Skipping inconsistent bitmap b2
+image: TEST_DIR/t.IMGFMT.copy
+file format: IMGFMT
+virtual size: 10 MiB (10485760 bytes)
+cluster_size: 65536
+Format specific information:
+    bitmaps:
+        [0]:
+            flags:
+                [0]: auto
+            name: b4
+            granularity: 65536
+    corrupt: false
 *** done
Signature · 1 line
2.31.1
#7 lore raw

Re: [PATCH 2/2] qemu-img: Add --skip-broken for 'convert --bitmaps'

Quoted 32 lines · Eric Blake

The point of 'qemu-img convert --bitmaps' is to be a convenience for actions that are already possible through a string of smaller 'qemu-img bitmap' sub-commands. One situation not accounted for already is that if a source image contains an inconsistent bitmap (for example, because a qemu process died abruptly before flushing bitmap state), the user MUST delete those inconsistent bitmaps before anything else useful can be done with the image.

We don't want to delete inconsistent bitmaps by default: although a corrupt bitmap is only a loss of optimization rather than a corruption of user-visible data, it is still nice to require the user to opt in to the fact that they are aware of the loss of the bitmap. Still, requiring the user to check 'qemu-img info' to see whether bitmaps are consistent, then use 'qemu-img bitmap --remove' to remove offenders, all before using 'qemu-img convert', is a lot more work than just adding a knob 'qemu-img convert --bitmaps --skip-broken' which opts in to skipping the broken bitmaps.

---
  docs/tools/qemu-img.rst                       |  8 +++++++-
  qemu-img.c                                    | 20 +++++++++++++++++--
  tests/qemu-iotests/tests/qemu-img-bitmaps     |  4 ++++
  tests/qemu-iotests/tests/qemu-img-bitmaps.out | 14 +++++++++++++
  4 files changed, 43 insertions(+), 3 deletions(-)
docs/tools/qemu-img.rst +0 -0
index 1d8470eada0e..5cf1c764597b 100644

docs/tools/qemu-img.rst Command description: · line 417

Quoted 5 lines
docs/tools/qemu-img.rst +1 -1
@@ -414,7 +414,7 @@ Command description:
    4
      Error on reading data
 
-.. option:: convert [--object OBJECTDEF] [--image-opts] [--target-image-opts] [--target-is-zero] [--bitmaps] [-U] [-C] [-c] [-p] [-q] [-n] [-f FMT] [-t CACHE] [-T SRC_CACHE] [-O OUTPUT_FMT] [-B BACKING_FILE] [-o OPTIONS] [-l SNAPSHOT_PARAM] [-S SPARSE_SIZE] [-r RATE_LIMIT] [-m NUM_COROUTINES] [-W] FILENAME [FILENAME2 [...]] OUTPUT_FILENAME
+.. option:: convert [--object OBJECTDEF] [--image-opts] [--target-image-opts] [--target-is-zero] [--bitmaps [--skip-broken]] [-U] [-C] [-c] [-p] [-q] [-n] [-f FMT] [-t CACHE] [-T SRC_CACHE] [-O OUTPUT_FMT] [-B BACKING_FILE] [-o OPTIONS] [-l SNAPSHOT_PARAM] [-S SPARSE_SIZE] [-r RATE_LIMIT] [-m NUM_COROUTINES] [-W] FILENAME [FILENAME2 [...]] OUTPUT_FILENAME

Of course, [--bitmaps [--skip-broken]] looks like --skip-broken is a suboption.. But actually it's not so. So, shouldn't it be named more explicit, like --skip-broken-bitmaps ? To be sure that we will not interfere in future with some other broken things we want to skip? And to avoid strange but correct command lines like

qemu-img convert --skip-broken <someother options> --bitmaps <some other options> src dst

Quoted 101 lines
docs/tools/qemu-img.rst +6 -0
    Convert the disk image *FILENAME* or a snapshot *SNAPSHOT_PARAM*
    to disk image *OUTPUT_FILENAME* using format *OUTPUT_FMT*. It can
@@ -456,6 +456,12 @@ Command description:
    *NUM_COROUTINES* specifies how many coroutines work in parallel during
    the convert process (defaults to 8).
 
+  Use of ``--bitmaps`` requests that any persistent bitmaps present in
+  the original are also copied to the destination.  If any bitmap is
+  inconsistent in the source, the conversion will fail unless
+  ``--skip-broken`` is also specified to copy only the consistent
+  bitmaps.
+
  .. option:: create [--object OBJECTDEF] [-q] [-f FMT] [-b BACKING_FILE] [-F BACKING_FMT] [-u] [-o OPTIONS] FILENAME [SIZE]
 
    Create the new disk image *FILENAME* of size *SIZE* and format
qemu-img.c +18 -2
index 68a4d298098f..e8b012f39c0c 100644
@@ -82,6 +82,7 @@ enum {
      OPTION_MERGE = 274,
      OPTION_BITMAPS = 275,
      OPTION_FORCE = 276,
+    OPTION_SKIP_BROKEN = 277,
  };
 
  typedef enum OutputFormat {
@@ -2101,7 +2102,8 @@ static int convert_do_copy(ImgConvertState *s)
      return s->ret;
  }
 
-static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst)
+static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst,
+                                bool skip_broken)
  {
      BdrvDirtyBitmap *bm;
      Error *err = NULL;
@@ -2113,6 +2115,10 @@ static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst)
              continue;
          }
          name = bdrv_dirty_bitmap_name(bm);
+        if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
+            warn_report("Skipping inconsistent bitmap %s", name);
+            continue;
+        }
          qmp_block_dirty_bitmap_add(dst->node_name, name,
                                     true, bdrv_dirty_bitmap_granularity(bm),
                                     true, true,
@@ -2167,6 +2173,7 @@ static int img_convert(int argc, char **argv)
      bool force_share = false;
      bool explict_min_sparse = false;
      bool bitmaps = false;
+    bool skip_broken = false;
      int64_t rate_limit = 0;
 
      ImgConvertState s = (ImgConvertState) {
@@ -2188,6 +2195,7 @@ static int img_convert(int argc, char **argv)
              {"salvage", no_argument, 0, OPTION_SALVAGE},
              {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
              {"bitmaps", no_argument, 0, OPTION_BITMAPS},
+            {"skip-broken", no_argument, 0, OPTION_SKIP_BROKEN},
              {0, 0, 0, 0}
          };
          c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WUr:",
@@ -2316,6 +2324,9 @@ static int img_convert(int argc, char **argv)
          case OPTION_BITMAPS:
              bitmaps = true;
              break;
+        case OPTION_SKIP_BROKEN:
+            skip_broken = true;
+            break;
          }
      }
 
@@ -2323,6 +2334,11 @@ static int img_convert(int argc, char **argv)
          out_fmt = "raw";
      }
 
+    if (skip_broken && !bitmaps) {
+        error_report("Use of --skip-broken requires --bitmaps");
+        goto fail_getopt;
+    }
+
      if (s.compressed && s.copy_range) {
          error_report("Cannot enable copy offloading when -c is used");
          goto fail_getopt;
@@ -2678,7 +2694,7 @@ static int img_convert(int argc, char **argv)
 
      /* Now copy the bitmaps */
      if (bitmaps && ret == 0) {
-        ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs);
+        ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs, skip_broken);
      }
 
  out:
tests/qemu-iotests/tests/qemu-img-bitmaps +0 -0
index 76cd9e31e850..3e1a39bc81e4 100755

tests/qemu-iotests/tests/qemu-img-bitmaps _cleanup() · line 31

Quoted 4 lines
tests/qemu-iotests/tests/qemu-img-bitmaps +1 -0
@@ -28,6 +28,7 @@ _cleanup()
  {
      _cleanup_test_img
      nbd_server_stop
+    _rm_test_img "$TEST_DIR/t.$IMGFMT.copy"

Aha here it is. It should appear in a previous patch..

Also, it may be simply

_rm_test_img "$TEST_IMG.copy"

, like in 110.

tests/qemu-iotests/tests/qemu-img-bitmaps $QEMU_IMG bitmap --add "$TEST_IMG" b4 · line 145

Quoted 10 lines
tests/qemu-iotests/tests/qemu-img-bitmaps +3 -0
  }
  trap "_cleanup; exit \$status" 0 1 2 3 15
 
@@ -139,6 +140,9 @@ $QEMU_IMG bitmap --add "$TEST_IMG" b4
  $QEMU_IMG bitmap --remove "$TEST_IMG" b1
  _img_info --format-specific | _filter_irrelevant_img_info
  $QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"
+$QEMU_IMG convert --bitmaps --skip-broken -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"
+TEST_IMG="$TEST_IMG.copy" _img_info --format-specific \
+    | _filter_irrelevant_img_info

We still can now remove remaining inconsistent bitmaps and retry convert without --skip-broken, just to cover more nearby cases.

Quoted 27 lines
tests/qemu-iotests/tests/qemu-img-bitmaps +0 -0
  # success, all done
  echo '*** done'
tests/qemu-iotests/tests/qemu-img-bitmaps.out +14 -0
index 17b34eaed30f..685bde6d1d63 100644
@@ -145,4 +145,18 @@ Format specific information:
      corrupt: false
  qemu-img: Failed to populate bitmap b0: Bitmap 'b0' is inconsistent and cannot be used
  Try block-dirty-bitmap-remove to delete this bitmap from disk
+qemu-img: warning: Skipping inconsistent bitmap b0
+qemu-img: warning: Skipping inconsistent bitmap b2
+image: TEST_DIR/t.IMGFMT.copy
+file format: IMGFMT
+virtual size: 10 MiB (10485760 bytes)
+cluster_size: 65536
+Format specific information:
+    bitmaps:
+        [0]:
+            flags:
+                [0]: auto
+            name: b4
+            granularity: 65536
+    corrupt: false
  *** done
Signature · 2 lines
Best regards,
Vladimir
#8 lore raw
In reply to Eric Blake
Quoted 11 lines · Eric Blake

This is mostly a convenience factor as one could already use 'qemu-img info' to learn which bitmaps are broken and then 'qemu-img bitmap --remove' to nuke them before calling 'qemu-img convert --bitmaps', but it does have the advantage that the copied file is usable without extra efforts and the broken bitmap is not deleted from the source file.

Eric Blake (2):
iotests: Improve and rename test 291 to qemu-img-bitmap
qemu-img: Add --skip-broken for 'convert --bitmaps'

Quoted 9 lines
  docs/tools/qemu-img.rst                       |  8 +++-
  block/dirty-bitmap.c                          |  2 +-
  qemu-img.c                                    | 20 +++++++-
  .../{291 => tests/qemu-img-bitmaps}           | 17 ++++++-
  .../{291.out => tests/qemu-img-bitmaps.out}   | 46 ++++++++++++++++++-
  5 files changed, 87 insertions(+), 6 deletions(-)

rename tests/qemu-iotests/{291 => tests/qemu-img-bitmaps} (88%) rename tests/qemu-iotests/{291.out => tests/qemu-img-bitmaps.out} (76%)

I've applied this onto your nbd branch. This doesn't apply on master.

Signature · 2 lines
Best regards,
Vladimir