Patchwork [PATCHv4,1/2] qemu-img: find the image end offset during check

login
register
mail settings
Submitter Federico Simoncelli
Date Jan. 28, 2013, 11:59 a.m.
Message ID <1359374387-11202-1-git-send-email-fsimonce@redhat.com>
Download mbox | patch
Permalink /patch/216181/
State New
Headers show

Comments

Federico Simoncelli - Jan. 28, 2013, 11:59 a.m.
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>
---
 block/qcow2-refcount.c       |   10 ++++++++--
 include/block/block.h        |    1 +
 qemu-img.c                   |    4 ++++
 tests/qemu-iotests/026       |    6 +++---
 tests/qemu-iotests/036       |    3 ++-
 tests/qemu-iotests/039       |    2 +-
 tests/qemu-iotests/044.out   |    1 +
 tests/qemu-iotests/common.rc |    5 +++--
 8 files changed, 23 insertions(+), 9 deletions(-)
Eric Blake - Jan. 28, 2013, 5:53 p.m.
On 01/28/2013 04:59 AM, Federico Simoncelli wrote:
> 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>
> ---
>  block/qcow2-refcount.c       |   10 ++++++++--
>  include/block/block.h        |    1 +
>  qemu-img.c                   |    4 ++++
>  tests/qemu-iotests/026       |    6 +++---
>  tests/qemu-iotests/036       |    3 ++-
>  tests/qemu-iotests/039       |    2 +-
>  tests/qemu-iotests/044.out   |    1 +
>  tests/qemu-iotests/common.rc |    5 +++--
>  8 files changed, 23 insertions(+), 9 deletions(-)

> +++ b/tests/qemu-iotests/common.rc
> @@ -161,9 +161,10 @@ _cleanup_test_img()
>  
>  _check_test_img()
>  {
> -    $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
> +    $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./'
> +    	sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./' | \

Pre-existing, but '\:' is not portable in sed; better is using ':' directly.

> +    	sed -e '/Image end offset: [0-9]\+/d'
>  }

'sed -e | sed -e' is a waste of a process.  Why not use a single sed for
both operations?  For that matter, since we are already using sed, why
not use it in place of 'grep -v'?

Use of '[0-9]\+' is not portable in all versions of sed. Better would
be: '[0-9][0-9]*'.  Then again, since you aren't anchoring the
expression, you don't really care whether there was more than one digit;
you still end up deleting the entire line.

Thus, a simpler version would be:

$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'
Blue Swirl - Jan. 28, 2013, 8:22 p.m.
On Mon, Jan 28, 2013 at 11:59 AM, Federico Simoncelli
<fsimonce@redhat.com> wrote:
> 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>
> ---
>  block/qcow2-refcount.c       |   10 ++++++++--
>  include/block/block.h        |    1 +
>  qemu-img.c                   |    4 ++++
>  tests/qemu-iotests/026       |    6 +++---
>  tests/qemu-iotests/036       |    3 ++-
>  tests/qemu-iotests/039       |    2 +-
>  tests/qemu-iotests/044.out   |    1 +
>  tests/qemu-iotests/common.rc |    5 +++--
>  8 files changed, 23 insertions(+), 9 deletions(-)
>
> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
> index 6a95aa6..45ad043 100644
> --- a/block/qcow2-refcount.c
> +++ b/block/qcow2-refcount.c
> @@ -1116,7 +1116,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;
> @@ -1187,7 +1187,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++) {

Please add a space between 'for' and '('.

>          refcount1 = get_refcount(bs, i);
>          if (refcount1 < 0) {
>              fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
> @@ -1197,6 +1197,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 */
> @@ -1231,6 +1236,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
>          }
>      }
>
> +    res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
>      ret = 0;
>
>  fail:
> diff --git a/include/block/block.h b/include/block/block.h
> index ffd1936..85eb3d9 100644
> --- a/include/block/block.h
> +++ b/include/block/block.h
> @@ -213,6 +213,7 @@ typedef struct BdrvCheckResult {
>      int check_errors;
>      int corruptions_fixed;
>      int leaks_fixed;
> +    int64_t image_end_offset;
>      BlockFragInfo bfi;
>  } BdrvCheckResult;
>
> diff --git a/qemu-img.c b/qemu-img.c
> index 85d3740..e80c1c5 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -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) {
> diff --git a/tests/qemu-iotests/026 b/tests/qemu-iotests/026
> index 1602ccd..107a3ff 100755
> --- a/tests/qemu-iotests/026
> +++ b/tests/qemu-iotests/026
> @@ -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
> diff --git a/tests/qemu-iotests/036 b/tests/qemu-iotests/036
> index 329533e..4dbfc57 100755
> --- a/tests/qemu-iotests/036
> +++ b/tests/qemu-iotests/036
> @@ -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
> diff --git a/tests/qemu-iotests/039 b/tests/qemu-iotests/039
> index c5ae806..ae35175 100755
> --- a/tests/qemu-iotests/039
> +++ b/tests/qemu-iotests/039
> @@ -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
> diff --git a/tests/qemu-iotests/044.out b/tests/qemu-iotests/044.out
> index 7a40071..9c48673 100644
> --- a/tests/qemu-iotests/044.out
> +++ b/tests/qemu-iotests/044.out
> @@ -1,4 +1,5 @@
>  No errors were found on the image.
> +Image end offset: 4296447488
>  .
>  ----------------------------------------------------------------------
>  Ran 1 tests
> diff --git a/tests/qemu-iotests/common.rc b/tests/qemu-iotests/common.rc
> index aef5f52..606d91e 100644
> --- a/tests/qemu-iotests/common.rc
> +++ b/tests/qemu-iotests/common.rc
> @@ -161,9 +161,10 @@ _cleanup_test_img()
>
>  _check_test_img()
>  {
> -    $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
> +    $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./'
> +       sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./' | \
> +       sed -e '/Image end offset: [0-9]\+/d'
>  }
>
>  _img_info()
> --
> 1.7.1
>
>
Kevin Wolf - Feb. 6, 2013, 3:27 p.m.
Am 28.01.2013 12:59, schrieb Federico Simoncelli:
> 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>

Thanks, applied both to the block-next branch for 1.5.

Kevin

Patch

diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 6a95aa6..45ad043 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -1116,7 +1116,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;
@@ -1187,7 +1187,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",
@@ -1197,6 +1197,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 */
@@ -1231,6 +1236,7 @@  int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
         }
     }
 
+    res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
     ret = 0;
 
 fail:
diff --git a/include/block/block.h b/include/block/block.h
index ffd1936..85eb3d9 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -213,6 +213,7 @@  typedef struct BdrvCheckResult {
     int check_errors;
     int corruptions_fixed;
     int leaks_fixed;
+    int64_t image_end_offset;
     BlockFragInfo bfi;
 } BdrvCheckResult;
 
diff --git a/qemu-img.c b/qemu-img.c
index 85d3740..e80c1c5 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -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) {
diff --git a/tests/qemu-iotests/026 b/tests/qemu-iotests/026
index 1602ccd..107a3ff 100755
--- a/tests/qemu-iotests/026
+++ b/tests/qemu-iotests/026
@@ -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
diff --git a/tests/qemu-iotests/036 b/tests/qemu-iotests/036
index 329533e..4dbfc57 100755
--- a/tests/qemu-iotests/036
+++ b/tests/qemu-iotests/036
@@ -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
diff --git a/tests/qemu-iotests/039 b/tests/qemu-iotests/039
index c5ae806..ae35175 100755
--- a/tests/qemu-iotests/039
+++ b/tests/qemu-iotests/039
@@ -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
diff --git a/tests/qemu-iotests/044.out b/tests/qemu-iotests/044.out
index 7a40071..9c48673 100644
--- a/tests/qemu-iotests/044.out
+++ b/tests/qemu-iotests/044.out
@@ -1,4 +1,5 @@ 
 No errors were found on the image.
+Image end offset: 4296447488
 .
 ----------------------------------------------------------------------
 Ran 1 tests
diff --git a/tests/qemu-iotests/common.rc b/tests/qemu-iotests/common.rc
index aef5f52..606d91e 100644
--- a/tests/qemu-iotests/common.rc
+++ b/tests/qemu-iotests/common.rc
@@ -161,9 +161,10 @@  _cleanup_test_img()
 
 _check_test_img()
 {
-    $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
+    $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./'
+    	sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./' | \
+    	sed -e '/Image end offset: [0-9]\+/d'
 }
 
 _img_info()