It's useful to know how much space can be occupied by qcow2 persistent bitmaps, even though such metadata is unrelated to the guest-visible data. Report this value as an additional field.
Reported-by: Nir Soffer <nsof...@redhat.com> Signed-off-by: Eric Blake <ebl...@redhat.com> --- This is independent from my 'qemu-img convert --bitmaps' series, but highly related. As an example, if I create a 100M image, then 2 persistent bitmaps, all with default cluster/granularity sizing, I now see: $ ./qemu-img measure -f qcow2 -O qcow2 build/img.top required size: 52756480 fully allocated size: 105185280 bitmaps size: 327680 which argues that I should allocate 52756480 + 327680 bytes prior to attempting qemu-img convert --bitmaps to a pre-sized destination. If we like the idea, I probably need to submit a 2/1 patch adding iotest coverage of the new measurement. qapi/block-core.json | 15 ++++++++++----- block/qcow2.c | 25 +++++++++++++++++++++++++ qemu-img.c | 3 +++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 943df1926a91..b47c6d69ba27 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -633,18 +633,23 @@ # efficiently so file size may be smaller than virtual disk size. # # The values are upper bounds that are guaranteed to fit the new image file. -# Subsequent modification, such as internal snapshot or bitmap creation, may -# require additional space and is not covered here. +# Subsequent modification, such as internal snapshot or further bitmap +# creation, may require additional space and is not covered here. # -# @required: Size required for a new image file, in bytes. +# @required: Size required for a new image file, in bytes, when copying just +# guest-visible contents. # # @fully-allocated: Image file size, in bytes, once data has been written -# to all sectors. +# to all sectors, when copying just guest-visible contents. +# +# @bitmaps: Additional size required for bitmap metadata not directly used +# for guest contents, when that metadata can be copied in addition +# to guest contents. (since 5.1) # # Since: 2.10 ## { 'struct': 'BlockMeasureInfo', - 'data': {'required': 'int', 'fully-allocated': 'int'} } + 'data': {'required': 'int', 'fully-allocated': 'int', '*bitmaps': 'int'} } ## # @query-block: diff --git a/block/qcow2.c b/block/qcow2.c index b524b0c53f84..eba6c2511e60 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -4657,6 +4657,7 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs, PreallocMode prealloc; bool has_backing_file; bool has_luks; + uint64_t bitmaps_size = 0; /* size occupied by bitmaps in in_bs */ /* Parse image creation options */ cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err); @@ -4732,6 +4733,8 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs, /* Account for input image */ if (in_bs) { + BdrvDirtyBitmap *bm; + size_t bitmap_overhead = 0; int64_t ssize = bdrv_getlength(in_bs); if (ssize < 0) { error_setg_errno(&local_err, -ssize, @@ -4739,6 +4742,26 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs, goto err; } + FOR_EACH_DIRTY_BITMAP(in_bs, bm) { + if (bdrv_dirty_bitmap_get_persistence(bm)) { + uint64_t bmsize = bdrv_dirty_bitmap_size(bm); + uint32_t granularity = bdrv_dirty_bitmap_granularity(bm); + const char *name = bdrv_dirty_bitmap_name(bm); + uint64_t bmclusters = DIV_ROUND_UP(bmsize / granularity + / CHAR_BIT, cluster_size); + + /* Assume the entire bitmap is allocated */ + bitmaps_size += bmclusters * cluster_size; + /* Also reserve space for the bitmap table entries */ + bitmaps_size += ROUND_UP(bmclusters * sizeof(uint64_t), + cluster_size); + /* Guess at contribution to bitmap directory size */ + bitmap_overhead += ROUND_UP(strlen(name) + 24, + sizeof(uint64_t)); + } + } + bitmaps_size += ROUND_UP(bitmap_overhead, cluster_size); + virtual_size = ROUND_UP(ssize, cluster_size); if (has_backing_file) { @@ -4795,6 +4818,8 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs, * still counted. */ info->required = info->fully_allocated - virtual_size + required; + info->has_bitmaps = !!bitmaps_size; + info->bitmaps = bitmaps_size; return info; err: diff --git a/qemu-img.c b/qemu-img.c index 6541357179c2..d900bde89911 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -5084,6 +5084,9 @@ static int img_measure(int argc, char **argv) if (output_format == OFORMAT_HUMAN) { printf("required size: %" PRIu64 "\n", info->required); printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); + if (info->has_bitmaps) { + printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); + } } else { dump_json_block_measure_info(info); } -- 2.26.1