]> git.proxmox.com Git - mirror_qemu.git/commitdiff
qemu-img: find the image end offset during check
authorFederico Simoncelli <fsimonce@redhat.com>
Mon, 28 Jan 2013 11:59:46 +0000 (06:59 -0500)
committerKevin Wolf <kwolf@redhat.com>
Fri, 22 Feb 2013 20:21:08 +0000 (21:21 +0100)
This patch adds the support for reporting the image end offset (in
bytes). This is particularly useful after a conversion (or a rebase)
where the destination is a block device in order to find the first
unused byte at the end of the image.

Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
block/qcow2-refcount.c
include/block/block.h
qemu-img.c
tests/qemu-iotests/026
tests/qemu-iotests/036
tests/qemu-iotests/039
tests/qemu-iotests/044.out
tests/qemu-iotests/common.rc

index bc1784c30efe9e18b5ce1b8ac877efb15b1a16d7..d36cb4dd2fafc5c834674878d57d21b54f0238df 100644 (file)
@@ -1112,7 +1112,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                           BdrvCheckMode fix)
 {
     BDRVQcowState *s = bs->opaque;
-    int64_t size, i;
+    int64_t size, i, highest_cluster;
     int nb_clusters, refcount1, refcount2;
     QCowSnapshot *sn;
     uint16_t *refcount_table;
@@ -1183,7 +1183,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
     }
 
     /* compare ref counts */
-    for(i = 0; i < nb_clusters; i++) {
+    for (i = 0, highest_cluster = 0; i < nb_clusters; i++) {
         refcount1 = get_refcount(bs, i);
         if (refcount1 < 0) {
             fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
@@ -1193,6 +1193,11 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
         }
 
         refcount2 = refcount_table[i];
+
+        if (refcount1 > 0 || refcount2 > 0) {
+            highest_cluster = i;
+        }
+
         if (refcount1 != refcount2) {
 
             /* Check if we're allowed to fix the mismatch */
@@ -1227,6 +1232,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
         }
     }
 
+    res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
     ret = 0;
 
 fail:
index 5c3b911c1b688c35fa00e7402f6e02ca0d736913..ce61883fc9955773fb1c851f8f15fecb35087dd2 100644 (file)
@@ -213,6 +213,7 @@ typedef struct BdrvCheckResult {
     int check_errors;
     int corruptions_fixed;
     int leaks_fixed;
+    int64_t image_end_offset;
     BlockFragInfo bfi;
 } BdrvCheckResult;
 
index 85d3740b9ca9af1e536f06fef45835b670218bd0..e80c1c55fd436448d121afaae8405db290fae85f 100644 (file)
@@ -475,6 +475,10 @@ static int img_check(int argc, char **argv)
         result.bfi.fragmented_clusters * 100.0 / result.bfi.allocated_clusters);
     }
 
+    if (result.image_end_offset > 0) {
+        printf("Image end offset: %" PRId64 "\n", result.image_end_offset);
+    }
+
     bdrv_delete(bs);
 
     if (ret < 0 || result.check_errors) {
index 1602ccd2a5f780f29d49b6442cb5501e47cd7955..107a3ff2f6e3461c6b930cbe8a817eb761fc7786 100755 (executable)
@@ -102,7 +102,7 @@ if [ "$event" == "l2_load" ]; then
     $QEMU_IO -c "read $vmstate 0 128k " $BLKDBG_TEST_IMG | _filter_qemu_io
 fi
 
-$QEMU_IMG check $TEST_IMG 2>&1 | grep -v "refcount=1 reference=0"
+_check_test_img 2>&1 | grep -v "refcount=1 reference=0"
 
 done
 done
@@ -147,7 +147,7 @@ echo
 echo "Event: $event; errno: $errno; imm: $imm; once: $once; write $vmstate"
 $QEMU_IO -c "write $vmstate 0 64M" $BLKDBG_TEST_IMG | _filter_qemu_io
 
-$QEMU_IMG check $TEST_IMG 2>&1 | grep -v "refcount=1 reference=0"
+_check_test_img 2>&1 | grep -v "refcount=1 reference=0"
 
 done
 done
@@ -186,7 +186,7 @@ echo
 echo "Event: $event; errno: $errno; imm: $imm; once: $once"
 $QEMU_IO -c "write -b 0 64k" $BLKDBG_TEST_IMG | _filter_qemu_io
 
-$QEMU_IMG check $TEST_IMG 2>&1 | grep -v "refcount=1 reference=0"
+_check_test_img 2>&1 | grep -v "refcount=1 reference=0"
 
 done
 done
index 329533e9eab64098a4a41c5479b06c5b89465edc..4dbfc5724c174bd3661b50e2c73035adf3aff5ab 100755 (executable)
@@ -59,7 +59,8 @@ _make_test_img 64M
 echo
 echo === Repair image ===
 echo
-$QEMU_IMG check -r all $TEST_IMG
+_check_test_img -r all
+
 ./qcow2.py $TEST_IMG dump-header
 
 # success, all done
index c5ae806ecbc527f11fc1070a3fec862f3b1f1453..ae3517575c955cd69b55503e81b17b8371ce4dbf 100755 (executable)
@@ -86,7 +86,7 @@ $QEMU_IO -r -c "read -P 0x5a 0 512" $TEST_IMG | _filter_qemu_io
 echo
 echo "== Repairing the image file must succeed =="
 
-$QEMU_IMG check -r all $TEST_IMG
+_check_test_img -r all
 
 # The dirty bit must not be set
 ./qcow2.py $TEST_IMG dump-header | grep incompatible_features
index 7a4007137d81c3255e2814efec8650b001e8449f..9c4867329f8a97ae36dea5531cd43298a800ebaf 100644 (file)
@@ -1,4 +1,5 @@
 No errors were found on the image.
+Image end offset: 4296447488
 .
 ----------------------------------------------------------------------
 Ran 1 tests
index aef5f52b4f4ec3b87d5c210ac68b25ae952f71ba..5ba960b09fdabaf02bb01b64a031725a1669c872 100644 (file)
@@ -161,9 +161,10 @@ _cleanup_test_img()
 
 _check_test_img()
 {
-    $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
-        grep -v "fragmented$" | \
-       sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./'
+    $QEMU_IMG check "$@" -f $IMGFMT $TEST_IMG 2>&1 | \
+        sed -e "/fragmented$/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'
 }
 
 _img_info()