Skip to contentSkip to conversation

qemu-img: add measure sub-command

PATCH Superseded Stefan Hajnoczi 5 participants 17 messages Mar 22, 2017 – Mar 30, 2017 Block layer

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover qemu-img: add measure sub-command

v3:
* Drop RFC, this is ready to go for QEMU 2.10
* Use "required size" instead of "required bytes" in qemu-img output for consistency [Nir]
* Clarify BlockMeasureInfo semantics [Max]
* Clarify bdrv_measure() opts argument and error handling [Nir]
* Handle -o backing_file= for qcow2 [Max]
* Handle snapshot options in qemu-img measure
* Probe input image for allocated data clusters for qcow2. Didn't centralize this because there are format-specific aspects such as the cluster_size. It may make sense to centralize it later (with a bit more complexity) if support is added to more formats.
* Add qemu-img(1) man page section for 'measure' sub-command [Max]
* Extend test case to cover additional scenarios [Nir]

RFCv2:
* Publishing RFC again to discuss the new user-visible interfaces. Code has changed quite a bit, I have not kept any Reviewed-by tags.
* Rename qemu-img sub-command "measure" and API bdrv_measure() [Nir]
* Report both "required bytes" and "fully allocated bytes" to handle the empty image file and prealloc use cases [Nir and Dan]
* Use bdrv_getlength() instead of bdrv_nb_sectors() [Berto]
* Rename "err" label "out" in qemu-img-cmds.c [Nir]
* Add basic qcow2 support, doesn't support qemu-img convert from existing files yet

RFCv1:
* Publishing patch series with just raw support, no qcow2 yet. Please review the command-line interface and let me know if you are happy with this approach.

Users and management tools sometimes need to know the size required for a new disk image so that an LVM volume, SAN LUN, etc can be allocated ahead of time. Image formats like qcow2 have non-trivial metadata that makes it hard to estimate the exact size without knowledge of file format internals.

This patch series introduces a new qemu-img sub-command that calculates the required size for both image creation and conversion scenarios.

The conversion scenario is:

  $ qemu-img measure -f raw -O qcow2 input.img
  required size: 1327680
  fully allocated size: 1074069504

Here an existing image file is taken and the output includes the space required for data from the input image file.

The creation scenario is:

  $ qemu-img measure -O qcow2 --size 5G
  required size: 327680
  fully allocated size: 1074069504

Stefan Hajnoczi (8):
block: add bdrv_measure() API
raw-format: add bdrv_measure() support
qcow2: extract preallocation calculation function
qcow2: extract image creation option parsing
qcow2: add bdrv_measure() support
qemu-img: add measure subcommand
qemu-iotests: support per-format golden output files
iotests: add test 178 for qemu-img measure

 qapi/block-core.json             |  25 +++
 include/block/block.h            |   4 +
 include/block/block_int.h        |   2 +
 block.c                          |  35 ++++
 block/qcow2.c                    | 362 +++++++++++++++++++++++++++++----------
 block/raw-format.c               |  22 +++
 qemu-img.c                       | 227 ++++++++++++++++++++++++
 qemu-img-cmds.hx                 |   6 +
 qemu-img.texi                    |  25 +++
 tests/qemu-iotests/178           | 144 ++++++++++++++++
 tests/qemu-iotests/178.out.qcow2 | 242 ++++++++++++++++++++++++++
 tests/qemu-iotests/178.out.raw   | 130 ++++++++++++++
 tests/qemu-iotests/check         |   5 +
 tests/qemu-iotests/group         |   1 +
 14 files changed, 1136 insertions(+), 94 deletions(-)
 create mode 100755 tests/qemu-iotests/178
 create mode 100644 tests/qemu-iotests/178.out.qcow2
 create mode 100644 tests/qemu-iotests/178.out.raw
Signature · 1 line
2.9.3
#1 lore raw

PATCH v3 1/8 block: add bdrv_measure() API

bdrv_measure() provides a conservative maximum for the size of a new image. This information is handy if storage needs to be allocated (e.g. a SAN or an LVM volume) ahead of time.

---
 qapi/block-core.json      | 25 +++++++++++++++++++++++++
 include/block/block.h     |  4 ++++
 include/block/block_int.h |  2 ++
 block.c                   | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 66 insertions(+)
qapi/block-core.json +25 -0
index 0f132fc..42fb90e 100644
@@ -463,6 +463,31 @@
            '*dirty-bitmaps': ['BlockDirtyInfo'] } }
 
 ##
+# @BlockMeasureInfo:
+#
+# Image size calculation information.  This structure describes the size
+# requirements for creating a new image file.
+#
+# The size requirements depend on the new image file format.  File size always
+# equals virtual disk size for the 'raw' format.  Compact formats such as
+# 'qcow2' represent unallocated and zero regions 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.
+#
+# @required: Size required for a new image file, in bytes.
+#
+# @fully-allocated: Image file size, in bytes, once data has been written
+#                   to all sectors.
+#
+# Since: 2.10
+##
+{ 'struct': 'BlockMeasureInfo',
+  'data': {'required': 'int', 'fully-allocated': 'int'} }
+
+##
 # @query-block:
 #
 # Get a list of BlockInfo for all virtual block devices.
include/block/block.h +4 -0
index 5149260..43c789f 100644
@@ -298,6 +298,10 @@ int bdrv_truncate(BdrvChild *child, int64_t offset);
 int64_t bdrv_nb_sectors(BlockDriverState *bs);
 int64_t bdrv_getlength(BlockDriverState *bs);
 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
+void bdrv_measure(BlockDriver *drv, QemuOpts *opts,
+                  BlockDriverState *in_bs,
+                  BlockMeasureInfo *info,
+                  Error **errp);
 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
 void bdrv_refresh_limits(BlockDriverState *bs, Error **errp);
 int bdrv_commit(BlockDriverState *bs);
include/block/block_int.h +2 -0
index 59400bd..5099a58 100644
@@ -201,6 +201,8 @@ struct BlockDriver {
     int64_t (*bdrv_getlength)(BlockDriverState *bs);
     bool has_variable_length;
     int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
+    void (*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs,
+                         BlockMeasureInfo *info, Error **errp);
 
     int coroutine_fn (*bdrv_co_pwritev_compressed)(BlockDriverState *bs,
         uint64_t offset, uint64_t bytes, QEMUIOVector *qiov);
block.c +35 -0
index 6e906ec..a191b82 100644
@@ -3266,6 +3266,41 @@ int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
     return -ENOTSUP;
 }
 
+/*
+ * bdrv_measure:
+ * @drv: Format driver
+ * @opts: Creation options for new image
+ * @in_bs: Existing image containing data for new image (may be NULL)
+ * @info: Result object
+ * @errp: Error object
+ *
+ * Calculate file size required to create a new image.
+ *
+ * If @in_bs is given then space for allocated clusters and zero clusters
+ * from that image are included in the calculation.  If @opts contains a
+ * backing file that is shared by @in_bs then backing clusters are omitted
+ * from the calculation.
+ *
+ * If @in_bs is NULL then the calculation includes no allocated clusters
+ * unless a preallocation option is given in @opts.
+ *
+ * Note that @in_bs may use a different BlockDriver from @drv.
+ *
+ * If an error occurs the @errp pointer is set.
+ */
+void bdrv_measure(BlockDriver *drv, QemuOpts *opts,
+                  BlockDriverState *in_bs, BlockMeasureInfo *info,
+                  Error **errp)
+{
+    if (!drv->bdrv_measure) {
+        error_setg(errp, "Block driver '%s' does not support size measurement",
+                   drv->format_name);
+        return;
+    }
+
+    drv->bdrv_measure(opts, in_bs, info, errp);
+}
+
 /**
  * Return number of sectors on success, -errno on error.
  */
Signature · 1 line
2.9.3
#3 lore raw

PATCH v3 2/8 raw-format: add bdrv_measure() support

In reply to Stefan Hajnoczi

Maximum size calculation is trivial for the raw format: it's just the requested image size (because there is no metadata).

---
 block/raw-format.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
block/raw-format.c +22 -0
index 86fbc65..e66ba00 100644
@@ -312,6 +312,27 @@ static int64_t raw_getlength(BlockDriverState *bs)
     return s->size;
 }
 
+static void raw_measure(QemuOpts *opts, BlockDriverState *in_bs,
+                        BlockMeasureInfo *info,
+                        Error **errp)
+{
+    if (in_bs) {
+        int64_t ssize = bdrv_getlength(in_bs);
+        if (ssize < 0) {
+            error_setg_errno(errp, -ssize, "Unable to get image size");
+            return;
+        }
+        info->required = ssize;
+    } else {
+        info->required =
+            ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
+                     BDRV_SECTOR_SIZE);
+    }
+
+    /* Unallocated sectors count towards the file size in raw images */
+    info->fully_allocated = info->required;
+}
+
 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
 {
     return bdrv_get_info(bs->file->bs, bdi);
@@ -477,6 +498,7 @@ BlockDriver bdrv_raw = {
     .bdrv_truncate        = &raw_truncate,
     .bdrv_getlength       = &raw_getlength,
     .has_variable_length  = true,
+    .bdrv_measure         = &raw_measure,
     .bdrv_get_info        = &raw_get_info,
     .bdrv_refresh_limits  = &raw_refresh_limits,
     .bdrv_probe_blocksizes = &raw_probe_blocksizes,
Signature · 1 line
2.9.3
#5 lore raw

PATCH v3 3/8 qcow2: extract preallocation calculation function

In reply to Stefan Hajnoczi

Calculating the preallocated image size will be needed to implement .bdrv_measure(). Extract the code out into a separate function.

---
 block/qcow2.c | 134 +++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 76 insertions(+), 58 deletions(-)
block/qcow2.c +76 -58
index 6a92d2e..7c702f4 100644
@@ -2095,6 +2095,79 @@ static int preallocate(BlockDriverState *bs)
     return 0;
 }
 
+/**
+ * qcow2_calc_prealloc_size:
+ * @total_size: virtual disk size in bytes
+ * @cluster_size: cluster size in bytes
+ * @refcount_order: refcount bits power-of-2 exponent
+ *
+ * Returns: Total number of bytes required for the fully allocated image
+ * (including metadata).
+ */
+static int64_t qcow2_calc_prealloc_size(int64_t total_size,
+                                        size_t cluster_size,
+                                        int refcount_order)
+{
+    /* Note: The following calculation does not need to be exact; if it is a
+     * bit off, either some bytes will be "leaked" (which is fine) or we
+     * will need to increase the file size by some bytes (which is fine,
+     * too, as long as the bulk is allocated here). Therefore, using
+     * floating point arithmetic is fine. */
+    int64_t meta_size = 0;
+    uint64_t nreftablee, nrefblocke, nl1e, nl2e;
+    int64_t aligned_total_size = align_offset(total_size, cluster_size);
+    int cluster_bits = ctz32(cluster_size);
+    int refblock_bits, refblock_size;
+    /* refcount entry size in bytes */
+    double rces = (1 << refcount_order) / 8.;
+
+    /* see qcow2_open() */
+    refblock_bits = cluster_bits - (refcount_order - 3);
+    refblock_size = 1 << refblock_bits;
+
+    /* header: 1 cluster */
+    meta_size += cluster_size;
+
+    /* total size of L2 tables */
+    nl2e = aligned_total_size / cluster_size;
+    nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t));
+    meta_size += nl2e * sizeof(uint64_t);
+
+    /* total size of L1 tables */
+    nl1e = nl2e * sizeof(uint64_t) / cluster_size;
+    nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t));
+    meta_size += nl1e * sizeof(uint64_t);
+
+    /* total size of refcount blocks
+     *
+     * note: every host cluster is reference-counted, including metadata
+     * (even refcount blocks are recursively included).
+     * Let:
+     *   a = total_size (this is the guest disk size)
+     *   m = meta size not including refcount blocks and refcount tables
+     *   c = cluster size
+     *   y1 = number of refcount blocks entries
+     *   y2 = meta size including everything
+     *   rces = refcount entry size in bytes
+     * then,
+     *   y1 = (y2 + a)/c
+     *   y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m
+     * we can get y1:
+     *   y1 = (a + m) / (c - rces - rces * sizeof(u64) / c)
+     */
+    nrefblocke = (aligned_total_size + meta_size + cluster_size)
+        / (cluster_size - rces - rces * sizeof(uint64_t)
+                / cluster_size);
+    meta_size += DIV_ROUND_UP(nrefblocke, refblock_size) * cluster_size;
+
+    /* total size of refcount tables */
+    nreftablee = nrefblocke / refblock_size;
+    nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t));
+    meta_size += nreftablee * sizeof(uint64_t);
+
+    return meta_size + aligned_total_size;
+}
+
 static int qcow2_create2(const char *filename, int64_t total_size,
                          const char *backing_file, const char *backing_format,
                          int flags, size_t cluster_size, PreallocMode prealloc,
@@ -2133,64 +2206,9 @@ static int qcow2_create2(const char *filename, int64_t total_size,
     int ret;
 
     if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
-        /* Note: The following calculation does not need to be exact; if it is a
-         * bit off, either some bytes will be "leaked" (which is fine) or we
-         * will need to increase the file size by some bytes (which is fine,
-         * too, as long as the bulk is allocated here). Therefore, using
-         * floating point arithmetic is fine. */
-        int64_t meta_size = 0;
-        uint64_t nreftablee, nrefblocke, nl1e, nl2e;
-        int64_t aligned_total_size = align_offset(total_size, cluster_size);
-        int refblock_bits, refblock_size;
-        /* refcount entry size in bytes */
-        double rces = (1 << refcount_order) / 8.;
-
-        /* see qcow2_open() */
-        refblock_bits = cluster_bits - (refcount_order - 3);
-        refblock_size = 1 << refblock_bits;
-
-        /* header: 1 cluster */
-        meta_size += cluster_size;
-
-        /* total size of L2 tables */
-        nl2e = aligned_total_size / cluster_size;
-        nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t));
-        meta_size += nl2e * sizeof(uint64_t);
-
-        /* total size of L1 tables */
-        nl1e = nl2e * sizeof(uint64_t) / cluster_size;
-        nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t));
-        meta_size += nl1e * sizeof(uint64_t);
-
-        /* total size of refcount blocks
-         *
-         * note: every host cluster is reference-counted, including metadata
-         * (even refcount blocks are recursively included).
-         * Let:
-         *   a = total_size (this is the guest disk size)
-         *   m = meta size not including refcount blocks and refcount tables
-         *   c = cluster size
-         *   y1 = number of refcount blocks entries
-         *   y2 = meta size including everything
-         *   rces = refcount entry size in bytes
-         * then,
-         *   y1 = (y2 + a)/c
-         *   y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m
-         * we can get y1:
-         *   y1 = (a + m) / (c - rces - rces * sizeof(u64) / c)
-         */
-        nrefblocke = (aligned_total_size + meta_size + cluster_size)
-                   / (cluster_size - rces - rces * sizeof(uint64_t)
-                                                 / cluster_size);
-        meta_size += DIV_ROUND_UP(nrefblocke, refblock_size) * cluster_size;
-
-        /* total size of refcount tables */
-        nreftablee = nrefblocke / refblock_size;
-        nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t));
-        meta_size += nreftablee * sizeof(uint64_t);
-
-        qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
-                            aligned_total_size + meta_size, &error_abort);
+        int64_t prealloc_size =
+            qcow2_calc_prealloc_size(total_size, cluster_size, refcount_order);
+        qemu_opt_set_number(opts, BLOCK_OPT_SIZE, prealloc_size, &error_abort);
         qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc],
                      &error_abort);
     }
Signature · 1 line
2.9.3
#7 lore raw

PATCH v3 4/8 qcow2: extract image creation option parsing

In reply to Stefan Hajnoczi

The image creation options parsed by qcow2_create() are also needed to implement .bdrv_measure(). Extract the parsing code, including input validation.

---
 block/qcow2.c | 109 +++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 73 insertions(+), 36 deletions(-)
block/qcow2.c +73 -36
index 7c702f4..19be468 100644
@@ -2168,24 +2168,73 @@ static int64_t qcow2_calc_prealloc_size(int64_t total_size,
     return meta_size + aligned_total_size;
 }
 
-static int qcow2_create2(const char *filename, int64_t total_size,
-                         const char *backing_file, const char *backing_format,
-                         int flags, size_t cluster_size, PreallocMode prealloc,
-                         QemuOpts *opts, int version, int refcount_order,
-                         Error **errp)
+static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp)
 {
+    size_t cluster_size;
     int cluster_bits;
-    QDict *options;
 
-    /* Calculate cluster_bits */
+    cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
+                                         DEFAULT_CLUSTER_SIZE);
     cluster_bits = ctz32(cluster_size);
     if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
         (1 << cluster_bits) != cluster_size)
     {
         error_setg(errp, "Cluster size must be a power of two between %d and "
                    "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
-        return -EINVAL;
+        return 0;
     }
+    return cluster_size;
+}
+
+static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
+{
+    char *buf;
+    int ret;
+
+    buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
+    if (!buf) {
+        ret = 3; /* default */
+    } else if (!strcmp(buf, "0.10")) {
+        ret = 2;
+    } else if (!strcmp(buf, "1.1")) {
+        ret = 3;
+    } else {
+        error_setg(errp, "Invalid compatibility level: '%s'", buf);
+        ret = -EINVAL;
+    }
+    g_free(buf);
+    return ret;
+}
+
+static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
+                                                Error **errp)
+{
+    uint64_t refcount_bits;
+
+    refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
+    if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
+        error_setg(errp, "Refcount width must be a power of two and may not "
+                   "exceed 64 bits");
+        return 0;
+    }
+
+    if (version < 3 && refcount_bits != 16) {
+        error_setg(errp, "Different refcount widths than 16 bits require "
+                   "compatibility level 1.1 or above (use compat=1.1 or "
+                   "greater)");
+        return 0;
+    }
+
+    return refcount_bits;
+}
+
+static int qcow2_create2(const char *filename, int64_t total_size,
+                         const char *backing_file, const char *backing_format,
+                         int flags, size_t cluster_size, PreallocMode prealloc,
+                         QemuOpts *opts, int version, int refcount_order,
+                         Error **errp)
+{
+    QDict *options;
 
     /*
      * Open the image file and write a minimal qcow2 header.
@@ -2235,7 +2284,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
     *header = (QCowHeader) {
         .magic                      = cpu_to_be32(QCOW_MAGIC),
         .version                    = cpu_to_be32(version),
-        .cluster_bits               = cpu_to_be32(cluster_bits),
+        .cluster_bits               = cpu_to_be32(ctz32(cluster_size)),
         .size                       = cpu_to_be64(0),
         .l1_table_offset            = cpu_to_be64(0),
         .l1_size                    = cpu_to_be32(0),
@@ -2371,8 +2420,8 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
     int flags = 0;
     size_t cluster_size = DEFAULT_CLUSTER_SIZE;
     PreallocMode prealloc;
-    int version = 3;
-    uint64_t refcount_bits = 16;
+    int version;
+    uint64_t refcount_bits;
     int refcount_order;
     Error *local_err = NULL;
     int ret;
@@ -2385,8 +2434,12 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
     if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
         flags |= BLOCK_FLAG_ENCRYPT;
     }
-    cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
-                                         DEFAULT_CLUSTER_SIZE);
+    cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        ret = -EINVAL;
+        goto finish;
+    }
     buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
     prealloc = qapi_enum_parse(PreallocMode_lookup, buf,
                                PREALLOC_MODE__MAX, PREALLOC_MODE_OFF,
@@ -2396,16 +2449,10 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
         ret = -EINVAL;
         goto finish;
     }
-    g_free(buf);
-    buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
-    if (!buf) {
-        /* keep the default */
-    } else if (!strcmp(buf, "0.10")) {
-        version = 2;
-    } else if (!strcmp(buf, "1.1")) {
-        version = 3;
-    } else {
-        error_setg(errp, "Invalid compatibility level: '%s'", buf);
+
+    version = qcow2_opt_get_version_del(opts, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
         ret = -EINVAL;
         goto finish;
     }
@@ -2428,19 +2475,9 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
         goto finish;
     }
 
-    refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS,
-                                            refcount_bits);
-    if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
-        error_setg(errp, "Refcount width must be a power of two and may not "
-                   "exceed 64 bits");
-        ret = -EINVAL;
-        goto finish;
-    }
-
-    if (version < 3 && refcount_bits != 16) {
-        error_setg(errp, "Different refcount widths than 16 bits require "
-                   "compatibility level 1.1 or above (use compat=1.1 or "
-                   "greater)");
+    refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
         ret = -EINVAL;
         goto finish;
     }
Signature · 1 line
2.9.3
#9 lore raw

PATCH v3 5/8 qcow2: add bdrv_measure() support

In reply to Stefan Hajnoczi

Use qcow2_calc_prealloc_size() to get the required file size.

---
 block/qcow2.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 119 insertions(+)
block/qcow2.c +119 -0
index 19be468..ed898d3 100644
@@ -2940,6 +2940,124 @@ static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
     return 0;
 }
 
+static void qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
+                          BlockMeasureInfo *info, Error **errp)
+{
+    Error *local_err = NULL;
+    uint64_t required = 0; /* bytes that contribute to required size */
+    uint64_t virtual_size; /* disk size as seen by guest */
+    uint64_t refcount_bits;
+    size_t cluster_size;
+    int version;
+    char *optstr;
+    PreallocMode prealloc;
+    bool has_backing_file;
+
+    /* Parse image creation options */
+    cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
+    if (local_err) {
+        goto err;
+    }
+
+    version = qcow2_opt_get_version_del(opts, &local_err);
+    if (local_err) {
+        goto err;
+    }
+
+    refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
+    if (local_err) {
+        goto err;
+    }
+
+    optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
+    prealloc = qapi_enum_parse(PreallocMode_lookup, optstr,
+                               PREALLOC_MODE__MAX, PREALLOC_MODE_OFF,
+                               &local_err);
+    g_free(optstr);
+    if (local_err) {
+        goto err;
+    }
+
+    optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
+    has_backing_file = !!optstr;
+    g_free(optstr);
+
+    virtual_size = align_offset(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
+                                cluster_size);
+
+    /* Account for input image */
+    if (in_bs) {
+        int64_t ssize = bdrv_getlength(in_bs);
+        if (ssize < 0) {
+            error_setg_errno(errp, -ssize, "Unable to get image virtual_size");
+            return;
+        }
+
+        virtual_size = align_offset(ssize, cluster_size);
+
+        if (has_backing_file) {
+            /* We don't how much of the backing chain is shared by the input
+             * image and the new image file.  In the worst case the new image's
+             * backing file has nothing in common with the input image.  Be
+             * conservative and assume all clusters need to be written.
+             */
+            required = virtual_size;
+        } else {
+            int cluster_sectors = cluster_size / BDRV_SECTOR_SIZE;
+            int64_t sector_num;
+            int pnum = 0;
+
+            for (sector_num = 0;
+                 sector_num < ssize / BDRV_SECTOR_SIZE;
+                 sector_num += pnum) {
+                int nb_sectors = MAX(ssize / BDRV_SECTOR_SIZE - sector_num,
+                                     INT_MAX);
+                BlockDriverState *file;
+                int64_t ret;
+
+                ret = bdrv_get_block_status_above(in_bs, NULL,
+                                                  sector_num, nb_sectors,
+                                                  &pnum, &file);
+                if (ret < 0) {
+                    error_setg_errno(errp, -ret, "Unable to get block status");
+                    return;
+                }
+
+                if (ret & BDRV_BLOCK_ZERO) {
+                    /* Skip zero regions (safe with no backing file) */
+                } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
+                           (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
+                    /* Extend pnum to end of cluster for next iteration */
+                    pnum = ROUND_UP(sector_num + pnum, cluster_sectors) -
+                           sector_num;
+
+                    /* Count clusters we've seen */
+                    required += (sector_num % cluster_sectors + pnum) *
+                                BDRV_SECTOR_SIZE;
+                }
+            }
+        }
+    }
+
+    /* Take into account preallocation.  Nothing special is needed for
+     * PREALLOC_MODE_METADATA since metadata is always counted.
+     */
+    if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
+        required = virtual_size;
+    }
+
+    info->fully_allocated =
+        qcow2_calc_prealloc_size(virtual_size, cluster_size,
+                                 ctz32(refcount_bits));
+
+    /* Remove regions that are not required */
+    info->required = info->fully_allocated - virtual_size + required;
+    return;
+
+err:
+    error_propagate(errp, local_err);
+}
+
 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
 {
     BDRVQcow2State *s = bs->opaque;
@@ -3487,6 +3605,7 @@ BlockDriver bdrv_qcow2 = {
     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
     .bdrv_snapshot_list     = qcow2_snapshot_list,
     .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
+    .bdrv_measure           = qcow2_measure,
     .bdrv_get_info          = qcow2_get_info,
     .bdrv_get_specific_info = qcow2_get_specific_info,
 
Signature · 1 line
2.9.3
#10 lore raw

PATCH v3 6/8 qemu-img: add measure subcommand

In reply to Stefan Hajnoczi

The measure subcommand calculates the size required by a new image file. This can be used by users or management tools that need to allocate space on an LVM volume, SAN LUN, etc before creating or converting an image file.

---
 qemu-img.c       | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-img-cmds.hx |   6 ++
 qemu-img.texi    |  25 ++++++
 3 files changed, 258 insertions(+)
qemu-img.c +227 -0
index 98b836b..eb1fb62 100644
@@ -59,6 +59,7 @@ enum {
     OPTION_PATTERN = 260,
     OPTION_FLUSH_INTERVAL = 261,
     OPTION_NO_DRAIN = 262,
+    OPTION_SIZE = 263,
 };
 
 typedef enum OutputFormat {
@@ -4287,6 +4288,232 @@ out:
     return 0;
 }
 
+static void dump_json_block_measure_info(BlockMeasureInfo *info)
+{
+    QString *str;
+    QObject *obj;
+    Visitor *v = qobject_output_visitor_new(&obj);
+
+    visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
+    visit_complete(v, &obj);
+    str = qobject_to_json_pretty(obj);
+    assert(str != NULL);
+    printf("%s\n", qstring_get_str(str));
+    qobject_decref(obj);
+    visit_free(v);
+    QDECREF(str);
+}
+
+static int img_measure(int argc, char **argv)
+{
+    static const struct option long_options[] = {
+        {"help", no_argument, 0, 'h'},
+        {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
+        {"object", required_argument, 0, OPTION_OBJECT},
+        {"output", required_argument, 0, OPTION_OUTPUT},
+        {"size", required_argument, 0, OPTION_SIZE},
+        {0, 0, 0, 0}
+    };
+    OutputFormat output_format = OFORMAT_HUMAN;
+    BlockBackend *in_blk = NULL;
+    BlockDriver *drv;
+    const char *filename = NULL;
+    const char *fmt = NULL;
+    const char *out_fmt = "raw";
+    char *options = NULL;
+    char *snapshot_name = NULL;
+    QemuOpts *opts = NULL;
+    QemuOpts *object_opts = NULL;
+    QemuOpts *sn_opts = NULL;
+    QemuOptsList *create_opts = NULL;
+    bool image_opts = false;
+    uint64_t img_size = ~0ULL;
+    BlockMeasureInfo info;
+    Error *local_err = NULL;
+    int ret = 1;
+    int c;
+
+    while ((c = getopt_long(argc, argv, "hf:O:o:l:",
+                            long_options, NULL)) != -1) {
+        switch (c) {
+        case '?':
+        case 'h':
+            help();
+            break;
+        case 'f':
+            fmt = optarg;
+            break;
+        case 'O':
+            out_fmt = optarg;
+            break;
+        case 'o':
+            if (!is_valid_option_list(optarg)) {
+                error_report("Invalid option list: %s", optarg);
+                goto out;
+            }
+            if (!options) {
+                options = g_strdup(optarg);
+            } else {
+                char *old_options = options;
+                options = g_strdup_printf("%s,%s", options, optarg);
+                g_free(old_options);
+            }
+            break;
+        case 'l':
+            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
+                sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
+                                                  optarg, false);
+                if (!sn_opts) {
+                    error_report("Failed in parsing snapshot param '%s'",
+                                 optarg);
+                    goto out;
+                }
+            } else {
+                snapshot_name = optarg;
+            }
+            break;
+        case OPTION_OBJECT:
+            object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
+                                                  optarg, true);
+            if (!object_opts) {
+                goto out;
+            }
+            break;
+        case OPTION_IMAGE_OPTS:
+            image_opts = true;
+            break;
+        case OPTION_OUTPUT:
+            if (!strcmp(optarg, "json")) {
+                output_format = OFORMAT_JSON;
+            } else if (!strcmp(optarg, "human")) {
+                output_format = OFORMAT_HUMAN;
+            } else {
+                error_report("--output must be used with human or json "
+                             "as argument.");
+                goto out;
+            }
+            break;
+        case OPTION_SIZE:
+        {
+            int64_t sval;
+
+            sval = cvtnum(optarg);
+            if (sval < 0) {
+                if (sval == -ERANGE) {
+                    error_report("Image size must be less than 8 EiB!");
+                } else {
+                    error_report("Invalid image size specified! You may use "
+                                 "k, M, G, T, P or E suffixes for ");
+                    error_report("kilobytes, megabytes, gigabytes, terabytes, "
+                                 "petabytes and exabytes.");
+                }
+                goto out;
+            }
+            img_size = (uint64_t)sval;
+        }
+        break;
+        }
+    }
+
+    if (qemu_opts_foreach(&qemu_object_opts,
+                          user_creatable_add_opts_foreach,
+                          NULL, NULL)) {
+        goto out;
+    }
+
+    if (argc - optind > 1) {
+        error_report("At most one filename argument is allowed.");
+        goto out;
+    } else if (argc - optind == 1) {
+        filename = argv[optind];
+    }
+
+    if (!filename &&
+        (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
+        error_report("--object, --image-opts, -f, and -l "
+                     "require a filename argument.");
+        goto out;
+    }
+    if (filename && img_size != ~0ULL) {
+        error_report("--size N cannot be used together with a filename.");
+        goto out;
+    }
+    if (!filename && img_size == ~0ULL) {
+        error_report("Either --size N or one filename must be specified.");
+        goto out;
+    }
+
+    if (filename) {
+        in_blk = img_open(image_opts, filename, fmt, 0, false, false);
+        if (!in_blk) {
+            goto out;
+        }
+
+        if (sn_opts) {
+            bdrv_snapshot_load_tmp(blk_bs(in_blk),
+                    qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
+                    qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
+                    &local_err);
+        } else if (snapshot_name != NULL) {
+            bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
+                    snapshot_name, &local_err);
+        }
+        if (local_err) {
+            error_reportf_err(local_err, "Failed to load snapshot: ");
+            goto out;
+        }
+    }
+
+    drv = bdrv_find_format(out_fmt);
+    if (!drv) {
+        error_report("Unknown file format '%s'", out_fmt);
+        goto out;
+    }
+    if (!drv->create_opts) {
+        error_report("Format driver '%s' does not support image creation",
+                     drv->format_name);
+        goto out;
+    }
+
+    create_opts = qemu_opts_append(create_opts, drv->create_opts);
+    create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
+    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
+    if (options) {
+        qemu_opts_do_parse(opts, options, NULL, &local_err);
+        if (local_err) {
+            error_report_err(local_err);
+            error_report("Invalid options for file format '%s'", out_fmt);
+            goto out;
+        }
+    }
+    if (img_size != ~0ULL) {
+        qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
+    }
+
+    bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &info, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+        goto out;
+    }
+
+    if (output_format == OFORMAT_HUMAN) {
+        printf("required size: %" PRIu64 "\n", info.required);
+        printf("fully allocated size: %" PRIu64 "\n", info.fully_allocated);
+    } else {
+        dump_json_block_measure_info(&info);
+    }
+
+    ret = 0;
+
+out:
+    qemu_opts_del(object_opts);
+    qemu_opts_del(opts);
+    qemu_opts_del(sn_opts);
+    qemu_opts_free(create_opts);
+    g_free(options);
+    blk_unref(in_blk);
+    return ret;
+}
 
 static const img_cmd_t img_cmds[] = {
 #define DEF(option, callback, arg_string)        \
qemu-img-cmds.hx +6 -0
index 9c9702c..291bc2c 100644
@@ -63,6 +63,12 @@ STEXI
 @item map [--object @var{objectdef}] [--image-opts] [-f @var{fmt}] [--output=@var{ofmt}] @var{filename}
 ETEXI
 
+DEF("measure", img_measure,
+"measure [--output=ofmt] [-O output_fmt] [-o options] [--size N | [--object objectdef] [--image-opts] [-f fmt] [-l snapshot_param] filename]")
+STEXI
+@item measure [--output=@var{ofmt}] [-O @var{output_fmt}] [-o @var{options}] [--size @var{N} | [--object @var{objectdef}] [--image-opts] [-f @var{fmt}] [-l @var{snapshot_param}] @var{filename}]
+ETEXI
+
 DEF("snapshot", img_snapshot,
     "snapshot [--object objectdef] [--image-opts] [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename")
 STEXI
qemu-img.texi +25 -0
index c81db3e..030b074 100644
@@ -429,6 +429,31 @@ preallocated.
 For more information, consult @file{include/block/block.h} in QEMU's
 source code.
 
+@item measure [--output=@var{ofmt}] [-O @var{output_fmt}] [-o @var{options}] [--size @var{N} | [--object @var{objectdef}] [--image-opts] [-f @var{fmt}] [-l @var{snapshot_param}] @var{filename}]
+
+Calculate the file size required for a new image.  This information can be used
+to size logical volumes or SAN LUNs appropriately for the image that will be
+placed in them.  The values reported are guaranteed to be large enough to fit
+the image.
+
+If the size @var{N} is given then act as if creating a new empty image file
+using @command{qemu-img create}.  If @var{filename} is given then act as if
+converting an existing image file using @command{qemu-img convert}.
+
+The following fields are reported:
+@example
+required size: 524288
+fully allocated size: 1074069504
+@end example
+
+The @code{required size} is the file size of the new image.  It may be smaller
+than the virtual disk size if the image format supports compact representation.
+
+The @code{fully allocated size} is the file size of the new image once data has
+been written to all sectors.  This is the maximum size that the image file can
+occupy with the exception of internal snapshots, dirty bitmaps, vmstate data,
+and other advanced image format features.
+
 @item snapshot [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot} ] @var{filename}
 
 List, apply, create or delete snapshots in image @var{filename}.
Signature · 1 line
2.9.3
#11 lore raw

PATCH v3 7/8 qemu-iotests: support per-format golden output files

In reply to Stefan Hajnoczi

Some tests produce format-dependent output. Either the difference is filtered out and ignored, or the test case is format-specific so we don't need to worry about per-format output differences.

There is a third case: the test script is the same for all image formats and the format-dependent output is relevant. An ugly workaround is to copy-paste the test into multiple per-format test cases. This duplicates code and is not maintainable.

This patch allows test cases to add per-format golden output files so a single test case can work correctly when format-dependent output must be checked:

123.out.qcow2
123.out.raw
123.out.vmdk
...

This naming scheme is not composable with 123.out.nocache or 123.pc.out, two other scenarios where output files are split. I don't think it matters since few test cases need these features.

---
 tests/qemu-iotests/check | 5 +++++
 1 file changed, 5 insertions(+)
tests/qemu-iotests/check +5 -0
index 4b1c674..29553cf 100755
@@ -338,6 +338,11 @@ do
                 reference="$reference_machine"
             fi
 
+            reference_format="$source_iotests/$seq.out.$IMGFMT"
+            if [ -f "$reference_format" ]; then
+                reference="$reference_format"
+            fi
+
             if [ "$CACHEMODE" = "none" ]; then
                 [ -f "$source_iotests/$seq.out.nocache" ] && reference="$source_iotests/$seq.out.nocache"
             fi
Signature · 1 line
2.9.3
#12 lore raw

PATCH v3 8/8 iotests: add test 178 for qemu-img measure

In reply to Stefan Hajnoczi
---
 tests/qemu-iotests/178           | 144 +++++++++++++++++++++++
 tests/qemu-iotests/178.out.qcow2 | 242 +++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/178.out.raw   | 130 +++++++++++++++++++++
 tests/qemu-iotests/group         |   1 +
 4 files changed, 517 insertions(+)
 create mode 100755 tests/qemu-iotests/178
 create mode 100644 tests/qemu-iotests/178.out.qcow2
 create mode 100644 tests/qemu-iotests/178.out.raw
tests/qemu-iotests/178 new file +144 -0
new file mode 100755
index 0000000..b777870
@@ -0,0 +1,144 @@
+#!/bin/bash
+#
+# qemu-img measure sub-command tests
+#
+# Copyright (C) 2017 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
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+status=1    # failure is the default!
+
+_cleanup()
+{
+    _cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+. ./common.pattern
+
+_supported_fmt raw qcow2
+_supported_proto file
+_supported_os Linux
+
+echo "== Input validation =="
+echo
+
+_make_test_img 1G
+
+$QEMU_IMG measure # missing arguments
+$QEMU_IMG measure --size 2G "$TEST_IMG" # only one allowed
+$QEMU_IMG measure "$TEST_IMG" a # only one filename allowed
+$QEMU_IMG measure --object secret,id=sec0,data=MTIzNDU2,format=base64 # missing filename
+$QEMU_IMG measure --image-opts # missing filename
+$QEMU_IMG measure -f qcow2 # missing filename
+$QEMU_IMG measure -l snap1 # missing filename
+$QEMU_IMG measure -o , # invalid option list
+$QEMU_IMG measure -l snapshot.foo # invalid snapshot option
+$QEMU_IMG measure --output foo # invalid output format
+$QEMU_IMG measure --size -1 # invalid image size
+$QEMU_IMG measure -O foo "$TEST_IMG" # unknown image file format
+
+make_test_img_with_fmt() {
+    # Shadow global variables within this function
+    local IMGFMT="$1" IMGOPTS=""
+    _make_test_img "$2"
+}
+
+qemu_io_with_fmt() {
+    # Shadow global variables within this function
+    local QEMU_IO_OPTIONS=$(echo "$QEMU_IO_OPTIONS" | sed "s/-f $IMGFMT/-f $1/")
+    shift
+    $QEMU_IO "$@"
+}
+
+for ofmt in human json; do
+    echo
+    echo "== Size calculation for a new file ($ofmt) =="
+    echo
+
+    # Try a few interesting sizes
+    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 0
+    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2G
+    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 64G
+    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 256G
+    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 1T
+
+    # Always test the raw input files but also IMGFMT
+    for fmt in $(echo -e "raw\n$IMGFMT\n" | sort -u); do
+        echo
+        echo "== Empty $fmt input image ($ofmt) =="
+        echo
+        make_test_img_with_fmt "$fmt" 0
+        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
+
+        echo
+        echo "== $fmt input image with data ($ofmt) =="
+        echo
+        make_test_img_with_fmt "$fmt" 1G
+        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
+        qemu_io_with_fmt "$fmt" -c "write 512 512" "$TEST_IMG" | _filter_qemu_io
+        qemu_io_with_fmt "$fmt" -c "write 64K 64K" "$TEST_IMG" | _filter_qemu_io
+        if [ "$fmt" = "qcow2" ]; then
+            $QEMU_IMG snapshot -c snapshot1 "$TEST_IMG"
+        fi
+        qemu_io_with_fmt "$fmt" -c "write 128M 63K" "$TEST_IMG" | _filter_qemu_io
+        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
+
+        if [ "$fmt" = "qcow2" ]; then
+            echo
+            echo "== $fmt input image with internal snapshot ($ofmt) =="
+            echo
+            $QEMU_IMG measure --output=$ofmt -f "$fmt" -l snapshot1 \
+                              -O "$IMGFMT" "$TEST_IMG"
+        fi
+
+        if [ "$IMGFMT" = "qcow2" ]; then
+            echo
+            echo "== $fmt input image and a backing file ($ofmt) =="
+            echo
+            # The backing file doesn't need to exist :)
+            $QEMU_IMG measure --output=$ofmt -o backing_file=x \
+                              -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
+        fi
+
+        echo
+        echo "== $fmt input image and preallocation ($ofmt) =="
+        echo
+        $QEMU_IMG measure --output=$ofmt -o preallocation=full \
+                          -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
+
+        echo
+        echo "== Fully-allocated $fmt input image ($ofmt) =="
+        echo
+        make_test_img_with_fmt "$fmt" 8M
+        qemu_io_with_fmt "$fmt" -c "write 0 8M" "$TEST_IMG" | _filter_qemu_io
+        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
+    done
+done
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
tests/qemu-iotests/178.out.qcow2 new file +242 -0

242 lines not on this page · raw message

tests/qemu-iotests/178.out.raw new file +130 -0

130 lines not on this page · raw message

tests/qemu-iotests/group +1 -0
index 1f4bf03..846f962 100644
@@ -168,3 +168,4 @@
 173 rw auto
 174 auto
 175 auto quick
+178 auto quick
Signature · 1 line
2.9.3
#13 lore raw
In reply to Stefan Hajnoczi
Quoted 92 lines · Stefan Hajnoczi

v3:
* Drop RFC, this is ready to go for QEMU 2.10
* Use "required size" instead of "required bytes" in qemu-img output for consistency [Nir]
* Clarify BlockMeasureInfo semantics [Max]
* Clarify bdrv_measure() opts argument and error handling [Nir]
* Handle -o backing_file= for qcow2 [Max]
* Handle snapshot options in qemu-img measure
* Probe input image for allocated data clusters for qcow2. Didn't
centralize
this because there are format-specific aspects such as the
cluster_size. It
may make sense to centralize it later (with a bit more complexity) if support is added to more formats.
* Add qemu-img(1) man page section for 'measure' sub-command [Max]
* Extend test case to cover additional scenarios [Nir]

RFCv2:
* Publishing RFC again to discuss the new user-visible interfaces. Code
has
changed quite a bit, I have not kept any Reviewed-by tags.
* Rename qemu-img sub-command "measure" and API bdrv_measure() [Nir]
* Report both "required bytes" and "fully allocated bytes" to handle the
empty
image file and prealloc use cases [Nir and Dan]
* Use bdrv_getlength() instead of bdrv_nb_sectors() [Berto]
* Rename "err" label "out" in qemu-img-cmds.c [Nir]
* Add basic qcow2 support, doesn't support qemu-img convert from existing
files yet

RFCv1:
* Publishing patch series with just raw support, no qcow2 yet. Please
review
the command-line interface and let me know if you are happy with this approach.

Users and management tools sometimes need to know the size required for a new
disk image so that an LVM volume, SAN LUN, etc can be allocated ahead of time.
Image formats like qcow2 have non-trivial metadata that makes it hard to estimate the exact size without knowledge of file format internals.

This patch series introduces a new qemu-img sub-command that calculates the required size for both image creation and conversion scenarios.

The conversion scenario is:

  $ qemu-img measure -f raw -O qcow2 input.img
  required size: 1327680
  fully allocated size: 1074069504

Here an existing image file is taken and the output includes the space required
for data from the input image file.

The creation scenario is:

  $ qemu-img measure -O qcow2 --size 5G
  required size: 327680
  fully allocated size: 1074069504

Stefan Hajnoczi (8):
block: add bdrv_measure() API
raw-format: add bdrv_measure() support
qcow2: extract preallocation calculation function
qcow2: extract image creation option parsing
qcow2: add bdrv_measure() support
qemu-img: add measure subcommand
qemu-iotests: support per-format golden output files
iotests: add test 178 for qemu-img measure

 qapi/block-core.json             |  25 +++
 include/block/block.h            |   4 +
 include/block/block_int.h        |   2 +
 block.c                          |  35 ++++
 block/qcow2.c                    | 362
+++++++++++++++++++++++++++++----------
 block/raw-format.c               |  22 +++
 qemu-img.c                       | 227 ++++++++++++++++++++++++
 qemu-img-cmds.hx                 |   6 +
 qemu-img.texi                    |  25 +++
 tests/qemu-iotests/178           | 144 ++++++++++++++++
 tests/qemu-iotests/178.out.qcow2 | 242 ++++++++++++++++++++++++++
 tests/qemu-iotests/178.out.raw   | 130 ++++++++++++++
 tests/qemu-iotests/check         |   5 +
 tests/qemu-iotests/group         |   1 +
 14 files changed, 1136 insertions(+), 94 deletions(-)
 create mode 100755 tests/qemu-iotests/178
 create mode 100644 tests/qemu-iotests/178.out.qcow2
 create mode 100644 tests/qemu-iotests/178.out.raw
Quoted 3 lines

--
2.9.3

Stefan, is this available in some public git repo?

I would like to test it, and downloading and applying 8 patches is lot of work.
(I'm probably not using the right tools for this).

Nir

#14 lore raw
Quoted 98 lines · Nir Soffer
Quoted 92 lines · Stefan Hajnoczi

v3:
* Drop RFC, this is ready to go for QEMU 2.10
* Use "required size" instead of "required bytes" in qemu-img output for consistency [Nir]
* Clarify BlockMeasureInfo semantics [Max]
* Clarify bdrv_measure() opts argument and error handling [Nir]
* Handle -o backing_file= for qcow2 [Max]
* Handle snapshot options in qemu-img measure
* Probe input image for allocated data clusters for qcow2. Didn't
centralize
this because there are format-specific aspects such as the
cluster_size. It
may make sense to centralize it later (with a bit more complexity) if support is added to more formats.
* Add qemu-img(1) man page section for 'measure' sub-command [Max]
* Extend test case to cover additional scenarios [Nir]

RFCv2:
* Publishing RFC again to discuss the new user-visible interfaces. Code
has
changed quite a bit, I have not kept any Reviewed-by tags.
* Rename qemu-img sub-command "measure" and API bdrv_measure() [Nir]
* Report both "required bytes" and "fully allocated bytes" to handle the
empty
image file and prealloc use cases [Nir and Dan]
* Use bdrv_getlength() instead of bdrv_nb_sectors() [Berto]
* Rename "err" label "out" in qemu-img-cmds.c [Nir]
* Add basic qcow2 support, doesn't support qemu-img convert from existing
files yet

RFCv1:
* Publishing patch series with just raw support, no qcow2 yet. Please
review
the command-line interface and let me know if you are happy with this approach.

Users and management tools sometimes need to know the size required for a new
disk image so that an LVM volume, SAN LUN, etc can be allocated ahead of time.
Image formats like qcow2 have non-trivial metadata that makes it hard to estimate the exact size without knowledge of file format internals.

This patch series introduces a new qemu-img sub-command that calculates the required size for both image creation and conversion scenarios.

The conversion scenario is:

  $ qemu-img measure -f raw -O qcow2 input.img
  required size: 1327680
  fully allocated size: 1074069504

Here an existing image file is taken and the output includes the space required
for data from the input image file.

The creation scenario is:

  $ qemu-img measure -O qcow2 --size 5G
  required size: 327680
  fully allocated size: 1074069504

Stefan Hajnoczi (8):
block: add bdrv_measure() API
raw-format: add bdrv_measure() support
qcow2: extract preallocation calculation function
qcow2: extract image creation option parsing
qcow2: add bdrv_measure() support
qemu-img: add measure subcommand
qemu-iotests: support per-format golden output files
iotests: add test 178 for qemu-img measure

 qapi/block-core.json             |  25 +++
 include/block/block.h            |   4 +
 include/block/block_int.h        |   2 +
 block.c                          |  35 ++++
 block/qcow2.c                    | 362
+++++++++++++++++++++++++++++----------
 block/raw-format.c               |  22 +++
 qemu-img.c                       | 227 ++++++++++++++++++++++++
 qemu-img-cmds.hx                 |   6 +
 qemu-img.texi                    |  25 +++
 tests/qemu-iotests/178           | 144 ++++++++++++++++
 tests/qemu-iotests/178.out.qcow2 | 242 ++++++++++++++++++++++++++
 tests/qemu-iotests/178.out.raw   | 130 ++++++++++++++
 tests/qemu-iotests/check         |   5 +
 tests/qemu-iotests/group         |   1 +
 14 files changed, 1136 insertions(+), 94 deletions(-)
 create mode 100755 tests/qemu-iotests/178
 create mode 100644 tests/qemu-iotests/178.out.qcow2
 create mode 100644 tests/qemu-iotests/178.out.raw
Quoted 3 lines

--
2.9.3

Quoted 5 lines

Stefan, is this available in some public git repo?

I would like to test it, and downloading and applying 8 patches is lot of work.
(I'm probably not using the right tools for this).

I've pushed the branch here:
https://github.com/stefanha/qemu/tree/qemu-img-query-max-size

In mutt you can tag multiple emails ('t') and then pipe them to git am in one go (';|git am'). Many other email clients offer no support for processing patches by default.

Stefan

#15 lore raw
Quoted 2 lines · Stefan Hajnoczi
Quoted 1 line · Nir Soffer

On Wed, Mar 22, 2017 at 2:28 PM Stefan Hajnoczi <[email protected]>

Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines

v3:
* Drop RFC, this is ready to go for QEMU 2.10
* Use "required size" instead of "required bytes" in qemu-img output

for

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

consistency [Nir]
* Clarify BlockMeasureInfo semantics [Max]
* Clarify bdrv_measure() opts argument and error handling [Nir]
* Handle -o backing_file= for qcow2 [Max]
* Handle snapshot options in qemu-img measure
* Probe input image for allocated data clusters for qcow2. Didn't
centralize
this because there are format-specific aspects such as the
cluster_size. It
may make sense to centralize it later (with a bit more complexity)

if

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

support is added to more formats.
* Add qemu-img(1) man page section for 'measure' sub-command [Max]
* Extend test case to cover additional scenarios [Nir]

RFCv2:
* Publishing RFC again to discuss the new user-visible interfaces.

Code

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

has
changed quite a bit, I have not kept any Reviewed-by tags.
* Rename qemu-img sub-command "measure" and API bdrv_measure() [Nir]
* Report both "required bytes" and "fully allocated bytes" to handle

the

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

empty
image file and prealloc use cases [Nir and Dan]
* Use bdrv_getlength() instead of bdrv_nb_sectors() [Berto]
* Rename "err" label "out" in qemu-img-cmds.c [Nir]
* Add basic qcow2 support, doesn't support qemu-img convert from

existing

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

files yet

RFCv1:
* Publishing patch series with just raw support, no qcow2 yet. Please
review
the command-line interface and let me know if you are happy with

this

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

approach.

Users and management tools sometimes need to know the size required

for a

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

new
disk image so that an LVM volume, SAN LUN, etc can be allocated ahead

of

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

time.
Image formats like qcow2 have non-trivial metadata that makes it hard

to

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

estimate the exact size without knowledge of file format internals.

This patch series introduces a new qemu-img sub-command that

calculates the

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines

required size for both image creation and conversion scenarios.

The conversion scenario is:

  $ qemu-img measure -f raw -O qcow2 input.img
  required size: 1327680
  fully allocated size: 1074069504

Here an existing image file is taken and the output includes the space required
for data from the input image file.

The creation scenario is:

  $ qemu-img measure -O qcow2 --size 5G
  required size: 327680
  fully allocated size: 1074069504

Stefan Hajnoczi (8):
block: add bdrv_measure() API
raw-format: add bdrv_measure() support
qcow2: extract preallocation calculation function
qcow2: extract image creation option parsing
qcow2: add bdrv_measure() support
qemu-img: add measure subcommand
qemu-iotests: support per-format golden output files
iotests: add test 178 for qemu-img measure

 qapi/block-core.json             |  25 +++
 include/block/block.h            |   4 +
 include/block/block_int.h        |   2 +
 block.c                          |  35 ++++
 block/qcow2.c                    | 362
+++++++++++++++++++++++++++++----------
 block/raw-format.c               |  22 +++
 qemu-img.c                       | 227 ++++++++++++++++++++++++
 qemu-img-cmds.hx                 |   6 +
 qemu-img.texi                    |  25 +++
 tests/qemu-iotests/178           | 144 ++++++++++++++++
 tests/qemu-iotests/178.out.qcow2 | 242 ++++++++++++++++++++++++++
 tests/qemu-iotests/178.out.raw   | 130 ++++++++++++++
 tests/qemu-iotests/check         |   5 +
 tests/qemu-iotests/group         |   1 +
 14 files changed, 1136 insertions(+), 94 deletions(-)
 create mode 100755 tests/qemu-iotests/178
 create mode 100644 tests/qemu-iotests/178.out.qcow2
 create mode 100644 tests/qemu-iotests/178.out.raw
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

--
2.9.3

Stefan, is this available in some public git repo?

I would like to test it, and downloading and applying 8 patches is lot

of

Quoted 11 lines
Quoted 2 lines

work.
(I'm probably not using the right tools for this).

I've pushed the branch here:
https://github.com/stefanha/qemu/tree/qemu-img-query-max-size

In mutt you can tag multiple emails ('t') and then pipe them to git am in one go (';|git am'). Many other email clients offer no support for processing patches by default.

Stefan

Hi Stefan,

Thank you for the branch, it was much helpful to verify and test the code that way :)

I ran a test and created several files using python.
Some of the files that were converted to qcow2 seems to have larger size than the measure size.

In the following example the converted file had an extra 65536 bits than the measured size (*):

Creating test_qcow2 using python:
"""
import os
import io
MB = 1024 ** 2
filename = os.path.join("/home_folder", 'test_qcow2') with io.open(filename, "wb") as f:

     f.truncate(1024 * MB)
     f.write("x" * MB)
     f.seek(512 * MB)
     f.write("x" * MB)

"""

Convert the file using qemu-img convert:

$ qemu-img convert -f raw -O qcow2 ~/test_qcow2 ~/test_qcow2_convert.qcow2

Validate the size:

Checking the size of the created file:

$ stat --printf="%s\n" ~/test_qcow2
1073741824

qemu-img measure returns required size of 2424832:

$ ./qemu-img measure -O qcow2 -f raw ~/test_qcow2
required size: 2424832
fully allocated size: 1074069504

The converted file has size of 2490368:

$ stat --printf="%s\n" ~/test_qcow2_convert.qcow
2490368

Looking at the following code:

file: block/qcow2.c
method: qcow2_calc_prealloc_size
lines: 2163 - 2166

    /* total size of refcount tables */
        nreftablee = nrefblocke / refblock_size;
        nreftablee = align_offset(nreftablee, cluster_size /
sizeof(uint64_t));
        meta_size += nreftablee * sizeof(uint64_t

it could be that when the result of nrefblocke / refblock_size is less than 1 the align_offset being done at line 2165 convert the float into int and therefore it is 0, I assume that there should always be at lease one cluster for nreftablee.
so maybe that might be the missing cluster?
I've tried to test this with the proposed fix and it seems to do the trick.

Please let me know what you think, if that is indeed the case maybe we will be glad to send a patch.

Thanks,
Maor

(*) The files can be found here:
https://www.dropbox.com/sh/nid7wtgt6qxdrcz/AABbjoJL4Jj0bzwOui2SD56ta?dl=0

#16 lore raw
Quoted 190 lines · Maor Lipchuk
Quoted 2 lines · Stefan Hajnoczi
Quoted 1 line · Nir Soffer

On Wed, Mar 22, 2017 at 2:28 PM Stefan Hajnoczi <[email protected]>

Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines

v3:
* Drop RFC, this is ready to go for QEMU 2.10
* Use "required size" instead of "required bytes" in qemu-img output

for

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

consistency [Nir]
* Clarify BlockMeasureInfo semantics [Max]
* Clarify bdrv_measure() opts argument and error handling [Nir]
* Handle -o backing_file= for qcow2 [Max]
* Handle snapshot options in qemu-img measure
* Probe input image for allocated data clusters for qcow2. Didn't
centralize
this because there are format-specific aspects such as the
cluster_size. It
may make sense to centralize it later (with a bit more complexity)

if

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

support is added to more formats.
* Add qemu-img(1) man page section for 'measure' sub-command [Max]
* Extend test case to cover additional scenarios [Nir]

RFCv2:
* Publishing RFC again to discuss the new user-visible interfaces.

Code

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

has
changed quite a bit, I have not kept any Reviewed-by tags.
* Rename qemu-img sub-command "measure" and API bdrv_measure() [Nir]
* Report both "required bytes" and "fully allocated bytes" to handle

the

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

empty
image file and prealloc use cases [Nir and Dan]
* Use bdrv_getlength() instead of bdrv_nb_sectors() [Berto]
* Rename "err" label "out" in qemu-img-cmds.c [Nir]
* Add basic qcow2 support, doesn't support qemu-img convert from

existing

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

files yet

RFCv1:
* Publishing patch series with just raw support, no qcow2 yet. Please
review
the command-line interface and let me know if you are happy with

this

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

approach.

Users and management tools sometimes need to know the size required

for a

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

new
disk image so that an LVM volume, SAN LUN, etc can be allocated ahead

of

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

time.
Image formats like qcow2 have non-trivial metadata that makes it hard

to

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

estimate the exact size without knowledge of file format internals.

This patch series introduces a new qemu-img sub-command that

calculates the

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines

required size for both image creation and conversion scenarios.

The conversion scenario is:

  $ qemu-img measure -f raw -O qcow2 input.img
  required size: 1327680
  fully allocated size: 1074069504

Here an existing image file is taken and the output includes the space required
for data from the input image file.

The creation scenario is:

  $ qemu-img measure -O qcow2 --size 5G
  required size: 327680
  fully allocated size: 1074069504

Stefan Hajnoczi (8):
block: add bdrv_measure() API
raw-format: add bdrv_measure() support
qcow2: extract preallocation calculation function
qcow2: extract image creation option parsing
qcow2: add bdrv_measure() support
qemu-img: add measure subcommand
qemu-iotests: support per-format golden output files
iotests: add test 178 for qemu-img measure

 qapi/block-core.json             |  25 +++
 include/block/block.h            |   4 +
 include/block/block_int.h        |   2 +
 block.c                          |  35 ++++
 block/qcow2.c                    | 362
+++++++++++++++++++++++++++++----------
 block/raw-format.c               |  22 +++
 qemu-img.c                       | 227 ++++++++++++++++++++++++
 qemu-img-cmds.hx                 |   6 +
 qemu-img.texi                    |  25 +++
 tests/qemu-iotests/178           | 144 ++++++++++++++++
 tests/qemu-iotests/178.out.qcow2 | 242 ++++++++++++++++++++++++++
 tests/qemu-iotests/178.out.raw   | 130 ++++++++++++++
 tests/qemu-iotests/check         |   5 +
 tests/qemu-iotests/group         |   1 +
 14 files changed, 1136 insertions(+), 94 deletions(-)
 create mode 100755 tests/qemu-iotests/178
 create mode 100644 tests/qemu-iotests/178.out.qcow2
 create mode 100644 tests/qemu-iotests/178.out.raw
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

--
2.9.3

Stefan, is this available in some public git repo?

I would like to test it, and downloading and applying 8 patches is lot

of

Quoted 11 lines
Quoted 2 lines

work.
(I'm probably not using the right tools for this).

I've pushed the branch here:
https://github.com/stefanha/qemu/tree/qemu-img-query-max-size

In mutt you can tag multiple emails ('t') and then pipe them to git am in one go (';|git am'). Many other email clients offer no support for processing patches by default.

Stefan

Hi Stefan,

Thank you for the branch, it was much helpful to verify and test the code that way :)

I ran a test and created several files using python.
Some of the files that were converted to qcow2 seems to have larger size than the measure size.

In the following example the converted file had an extra 65536 bits than the measured size (*):

Creating test_qcow2 using python:
"""
import os
import io
MB = 1024 ** 2
filename = os.path.join("/home_folder", 'test_qcow2') with io.open(filename, "wb") as f:

     f.truncate(1024 * MB)
     f.write("x" * MB)
     f.seek(512 * MB)
     f.write("x" * MB)

"""

Convert the file using qemu-img convert:

$ qemu-img convert -f raw -O qcow2 ~/test_qcow2 ~/test_qcow2_convert.qcow2

Validate the size:

Checking the size of the created file:

$ stat --printf="%s\n" ~/test_qcow2
1073741824

qemu-img measure returns required size of 2424832:

$ ./qemu-img measure -O qcow2 -f raw ~/test_qcow2
required size: 2424832
fully allocated size: 1074069504

The converted file has size of 2490368:

$ stat --printf="%s\n" ~/test_qcow2_convert.qcow
2490368

Looking at the following code:

file: block/qcow2.c
method: qcow2_calc_prealloc_size
lines: 2163 - 2166

    /* total size of refcount tables */
        nreftablee = nrefblocke / refblock_size;
        nreftablee = align_offset(nreftablee, cluster_size /
sizeof(uint64_t));
        meta_size += nreftablee * sizeof(uint64_t

it could be that when the result of nrefblocke / refblock_size is less than 1 the align_offset being done at line 2165 convert the float into int and therefore it is 0, I assume that there should always be at lease one cluster for nreftablee.
so maybe that might be the missing cluster?
I've tried to test this with the proposed fix and it seems to do the trick.

Quoted 2 lines

Please let me know what you think, if that is indeed the case maybe we will be glad to send a patch.

Thanks for pointing out this bug. There is a big warning about this in the qcow2 code:

/* Note: The following calculation does not need to be exact; if it is a
* bit off, either some bytes will be "leaked" (which is fine) or we
* will need to increase the file size by some bytes (which is fine,
* too, as long as the bulk is allocated here). Therefore, using
* floating point arithmetic is fine. */

I will fix it by auditing the code and ensuring we round up as necessary in v4.

Stefan