Skip to contentSkip to conversation

Provide additional info through qemu-img info

PATCH Applied Max Reitz 2 participants 8 messages Oct 9, 2013 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

v8 cover Provide additional info through qemu-img info

qemu-img info provides only pretty general information about an image. For any image format, there might be specific options which cannot be represented in a universal way; for instance, qcow2 provides the compatibility and lazy_refcount options whose values are certainly interesting but currently cannot be output by qemu-img info.

Therefore, this series adds a new ImageInfoSpecific union type to ImageInfo which may be used by block drivers as a template for new types dedicated to the specific information they can provide, as well as a function bdrv_get_specific_info for retrieving this information. It also adds support to qemu-img info and qemu-io -c info to print the content of these specific structures.

v8:
- implemented Kevin's remark:
- patch 4: use BDRVQcowState.compatible_features instead of BDRVQCowState.use_lazy_refcounts to determine the lazy-refcounts value
- patch 4: employ compound initializers
- patch 6: rewrote test case in Python; now also tests JSON output and QMP output with qcow2 runtime options differing from the ones specified by the image

v7:
- patch 5: corrected JSON object ending regex from '^ *\}, *$' to '^ *\},? *$' (comma is optional); added description of what is being stripped to commit message

v6:
- implemented Eric's remarks:
- corrected description of ImageInfo in qapi-schema.json (patch 1)
- added root indentation in bdrv_image_info_specific_dump (patch 3); Although this is a very minor change, I dropped Eric's Reviewed-by because of it, since I'm not sure myself whether the place I've added the indentation is actually the right one (maybe the caller of bdrv_image_info_specific_dump should control the indentation instead of hardcoding it into the function). Furthermore, this (his) suggestion was not part of his review for this patch, but for patch 6.
- changed patch 5 from using some non-portable expressions to fully bash-specific ones
- use $() instead of `` and $PWD instead of pwd in patch 6 and indent format specific information in reference output (according to change in patch 3)
- rebased on Kevin's block branch (changed some line numbers and the diff environment in patch 5)

v5:
- In consideration of Stefan's argument about bdrv_round_to_cluster being called in the I/O path, removed ImageInfoSpecific from BlockDriverInfo and introduced a new function bdrv_get_specific_info instead; this also removes the need for bdrv_put_info. Resulting changes in regard to the single patches:
- patch 2: completely different
- patch 3: qemu-io-cmds.c:info_f now needs to call bdrv_get_specific_info in order to receive the new information
- patch 4: moved the creation of ImageInfoSpecific from qcow2_get_info (which now remains unmodified) to the new qcow2_get_specific_info function; additionally, register this function as bdrv_qcow2.bdrv_get_specific_info
- rebased on Kevin's block branch, renamed test from 064 to 065 (to avoid conflict with his blockdev-add series; affects patch 6)
- patches 1 and 5 remain unmodified

v4:
- changed dirty "grep -A 42" for grepping all lines until EOF in test 064 to cleaner "sed -n '//,$p'" (patch 6)
- rebased on Kevin's block branch (affects line numbers in patches 2, 3 and 4 as well as the group file change in patch 6)

v3:
- implemented Fam's remarks:
- bdrv_get_info already initializes all fields to NULL, no need to do this manually (patch 2)
- implemented bdrv_put_info as a wrapper to qapi_free_ImageInfoSpecific, though this may change with further extensions to BlockDriverInfo (patch 2)
- changed one occurence of puts("foo") to printf("foo\n") in order to be consistent with the surrounding code (patch 3)
- other patches (1, 4, 5, 6) remain unmodified

v2:
- following Eric's recommendation: changed the representation of the format specific information from an uninterpreted blobbed string to a union of format specific types

Max Reitz (6):
qapi: Add ImageInfoSpecific type
block: Add bdrv_get_specific_info block/qapi: Human-readable ImageInfoSpecific dump
qcow2: Add support for ImageInfoSpecific
qemu-iotests: Discard specific info in _img_info
qemu-iotests: Additional info from qemu-img info

 block.c                       |   9 +++
 block/qapi.c                  | 124 +++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c                 |  26 +++++++++
 include/block/block.h         |   1 +
 include/block/block_int.h     |   1 +
 include/block/qapi.h          |   2 +
 qapi-schema.json              |  34 +++++++++++-
 qemu-io-cmds.c                |   9 +++
 tests/qemu-iotests/065        | 125 ++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/065.out    |   5 ++
 tests/qemu-iotests/common.rc  |  20 ++++++-
 tests/qemu-iotests/group      |   1 +
 tests/qemu-iotests/iotests.py |   4 ++
 13 files changed, 359 insertions(+), 2 deletions(-)
 create mode 100755 tests/qemu-iotests/065
 create mode 100644 tests/qemu-iotests/065.out
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v8 1/6 qapi: Add ImageInfoSpecific type

Add a new type ImageInfoSpecific as a union for image format specific information in ImageInfo.

---
 qapi-schema.json | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
qapi-schema.json +17 -1
index 381ffbf..246789b 100644
@@ -210,6 +210,18 @@
             'vm-clock-sec': 'int', 'vm-clock-nsec': 'int' } }
 
 ##
+# @ImageInfoSpecific:
+#
+# A discriminated record of image format specific information structures.
+#
+# Since: 1.7
+##
+
+{ 'union': 'ImageInfoSpecific',
+  'data': {
+  } }
+
+##
 # @ImageInfo:
 #
 # Information about a QEMU image file
@@ -238,6 +250,9 @@
 #
 # @backing-image: #optional info of the backing image (since 1.6)
 #
+# @format-specific: #optional structure supplying additional format-specific
+# information (since 1.7)
+#
 # Since: 1.3
 #
 ##
@@ -248,7 +263,8 @@
            '*cluster-size': 'int', '*encrypted': 'bool',
            '*backing-filename': 'str', '*full-backing-filename': 'str',
            '*backing-filename-format': 'str', '*snapshots': ['SnapshotInfo'],
-           '*backing-image': 'ImageInfo' } }
+           '*backing-image': 'ImageInfo',
+           '*format-specific': 'ImageInfoSpecific' } }
 
 ##
 # @ImageCheck:
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH v8 3/6 block/qapi: Human-readable ImageInfoSpecific dump

In reply to Max Reitz

Add a function for generically dumping the ImageInfoSpecific information in a human-readable format to block/qapi.c.

Use this function in bdrv_image_info_dump and qemu-io-cmds.c:info_f to allow qemu-img info resp. qemu-io -c info to print that format specific information.

---
 block/qapi.c         | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/block/qapi.h |   2 +
 qemu-io-cmds.c       |   9 ++++
 3 files changed, 132 insertions(+)
block/qapi.c +121 -0
index ab1dd24..5880b3e 100644
@@ -25,6 +25,9 @@
 #include "block/qapi.h"
 #include "block/block_int.h"
 #include "qmp-commands.h"
+#include "qapi-visit.h"
+#include "qapi/qmp-output-visitor.h"
+#include "qapi/qmp/types.h"
 
 /*
  * Returns 0 on success, with *p_list either set to describe snapshot
@@ -426,6 +429,119 @@ void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
     }
 }
 
+static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
+                       QDict *dict);
+static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
+                       QList *list);
+
+static void dump_qobject(fprintf_function func_fprintf, void *f,
+                         int comp_indent, QObject *obj)
+{
+    switch (qobject_type(obj)) {
+        case QTYPE_QINT: {
+            QInt *value = qobject_to_qint(obj);
+            func_fprintf(f, "%" PRId64, qint_get_int(value));
+            break;
+        }
+        case QTYPE_QSTRING: {
+            QString *value = qobject_to_qstring(obj);
+            func_fprintf(f, "%s", qstring_get_str(value));
+            break;
+        }
+        case QTYPE_QDICT: {
+            QDict *value = qobject_to_qdict(obj);
+            dump_qdict(func_fprintf, f, comp_indent, value);
+            break;
+        }
+        case QTYPE_QLIST: {
+            QList *value = qobject_to_qlist(obj);
+            dump_qlist(func_fprintf, f, comp_indent, value);
+            break;
+        }
+        case QTYPE_QFLOAT: {
+            QFloat *value = qobject_to_qfloat(obj);
+            func_fprintf(f, "%g", qfloat_get_double(value));
+            break;
+        }
+        case QTYPE_QBOOL: {
+            QBool *value = qobject_to_qbool(obj);
+            func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
+            break;
+        }
+        case QTYPE_QERROR: {
+            QString *value = qerror_human((QError *)obj);
+            func_fprintf(f, "%s", qstring_get_str(value));
+            break;
+        }
+        case QTYPE_NONE:
+            break;
+        case QTYPE_MAX:
+        default:
+            abort();
+    }
+}
+
+static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
+                       QList *list)
+{
+    const QListEntry *entry;
+    int i = 0;
+
+    for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
+        qtype_code type = qobject_type(entry->value);
+        bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
+        const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
+
+        func_fprintf(f, format, indentation * 4, "", i);
+        dump_qobject(func_fprintf, f, indentation + 1, entry->value);
+        if (!composite) {
+            func_fprintf(f, "\n");
+        }
+    }
+}
+
+static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
+                       QDict *dict)
+{
+    const QDictEntry *entry;
+
+    for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
+        qtype_code type = qobject_type(entry->value);
+        bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
+        const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
+        char key[strlen(entry->key) + 1];
+        int i;
+
+        /* replace dashes with spaces in key (variable) names */
+        for (i = 0; entry->key[i]; i++) {
+            key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
+        }
+        key[i] = 0;
+
+        func_fprintf(f, format, indentation * 4, "", key);
+        dump_qobject(func_fprintf, f, indentation + 1, entry->value);
+        if (!composite) {
+            func_fprintf(f, "\n");
+        }
+    }
+}
+
+void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
+                                   ImageInfoSpecific *info_spec)
+{
+    Error *local_err = NULL;
+    QmpOutputVisitor *ov = qmp_output_visitor_new();
+    QObject *obj, *data;
+
+    visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
+                                 &local_err);
+    obj = qmp_output_get_qobject(ov);
+    assert(qobject_type(obj) == QTYPE_QDICT);
+    data = qdict_get(qobject_to_qdict(obj), "data");
+    dump_qobject(func_fprintf, f, 1, data);
+    qmp_output_visitor_cleanup(ov);
+}
+
 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
                           ImageInfo *info)
 {
@@ -496,4 +612,9 @@ void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
             func_fprintf(f, "\n");
         }
     }
+
+    if (info->has_format_specific) {
+        func_fprintf(f, "Format specific information:\n");
+        bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
+    }
 }
include/block/qapi.h +2 -0
index 0496cc9..9518ee4 100644
@@ -42,6 +42,8 @@ BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 
 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
                         QEMUSnapshotInfo *sn);
+void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
+                                   ImageInfoSpecific *info_spec);
 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
                           ImageInfo *info);
 #endif
qemu-io-cmds.c +9 -0
index 8565d49..667f4e4 100644
@@ -10,6 +10,7 @@
 
 #include "qemu-io.h"
 #include "block/block_int.h"
+#include "block/qapi.h"
 #include "qemu/main-loop.h"
 
 #define CMD_NOFILE_OK   0x01
@@ -1678,6 +1679,7 @@ static const cmdinfo_t length_cmd = {
 static int info_f(BlockDriverState *bs, int argc, char **argv)
 {
     BlockDriverInfo bdi;
+    ImageInfoSpecific *spec_info;
     char s1[64], s2[64];
     int ret;
 
@@ -1699,6 +1701,13 @@ static int info_f(BlockDriverState *bs, int argc, char **argv)
     printf("cluster size: %s\n", s1);
     printf("vm state offset: %s\n", s2);
 
+    spec_info = bdrv_get_specific_info(bs);
+    if (spec_info) {
+        printf("Format specific information:\n");
+        bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
+        qapi_free_ImageInfoSpecific(spec_info);
+    }
+
     return 0;
 }
 
Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH v8 4/6 qcow2: Add support for ImageInfoSpecific

In reply to Max Reitz

Add a new ImageInfoSpecificQCow2 type as a subtype of ImageInfoSpecific. This contains the compatibility level as a string and an optional lazy_refcounts boolean (optional means mandatory for compat >= 1.1 and not available for compat == 0.10).

Also, add qcow2_get_specific_info, which returns this information.

---
 block/qcow2.c    | 26 ++++++++++++++++++++++++++
 qapi-schema.json | 16 ++++++++++++++++
 2 files changed, 42 insertions(+)
block/qcow2.c +26 -0
index 4a9888c..d59ecbd 100644
@@ -1810,6 +1810,31 @@ static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
     return 0;
 }
 
+static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
+{
+    BDRVQcowState *s = bs->opaque;
+    ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
+
+    *spec_info = (ImageInfoSpecific){
+        .kind  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
+        .qcow2 = g_new(ImageInfoSpecificQCow2, 1),
+    };
+    if (s->qcow_version == 2) {
+        *spec_info->qcow2 = (ImageInfoSpecificQCow2){
+            .compat = g_strdup("0.10"),
+        };
+    } else if (s->qcow_version == 3) {
+        *spec_info->qcow2 = (ImageInfoSpecificQCow2){
+            .compat             = g_strdup("1.1"),
+            .lazy_refcounts     = s->compatible_features &
+                                  QCOW2_COMPAT_LAZY_REFCOUNTS,
+            .has_lazy_refcounts = true,
+        };
+    }
+
+    return spec_info;
+}
+
 #if 0
 static void dump_refcounts(BlockDriverState *bs)
 {
@@ -2130,6 +2155,7 @@ static BlockDriver bdrv_qcow2 = {
     .bdrv_snapshot_list     = qcow2_snapshot_list,
     .bdrv_snapshot_load_tmp     = qcow2_snapshot_load_tmp,
     .bdrv_get_info      = qcow2_get_info,
+    .bdrv_get_specific_info = qcow2_get_specific_info,
 
     .bdrv_save_vmstate    = qcow2_save_vmstate,
     .bdrv_load_vmstate    = qcow2_load_vmstate,
qapi-schema.json +16 -0
index 246789b..a1a81a4 100644
@@ -210,6 +210,21 @@
             'vm-clock-sec': 'int', 'vm-clock-nsec': 'int' } }
 
 ##
+# @ImageInfoSpecificQCow2:
+#
+# @compat: compatibility level
+#
+# @lazy-refcounts: #optional on or off; only valid for compat >= 1.1
+#
+# Since: 1.7
+##
+{ 'type': 'ImageInfoSpecificQCow2',
+  'data': {
+      'compat': 'str',
+      '*lazy-refcounts': 'bool'
+  } }
+
+##
 # @ImageInfoSpecific:
 #
 # A discriminated record of image format specific information structures.
@@ -219,6 +234,7 @@
 
 { 'union': 'ImageInfoSpecific',
   'data': {
+      'qcow2': 'ImageInfoSpecificQCow2'
   } }
 
 ##
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH v8 2/6 block: Add bdrv_get_specific_info

In reply to Max Reitz

Add a function for retrieving an ImageInfoSpecific object from a block driver.

---
 block.c                   | 9 +++++++++
 block/qapi.c              | 3 +++
 include/block/block.h     | 1 +
 include/block/block_int.h | 1 +
 4 files changed, 14 insertions(+)
block.c +9 -0
index d7ca37e..43acaad 100644
@@ -3322,6 +3322,15 @@ int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
     return drv->bdrv_get_info(bs, bdi);
 }
 
+ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    if (drv && drv->bdrv_get_specific_info) {
+        return drv->bdrv_get_specific_info(bs);
+    }
+    return NULL;
+}
+
 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
                       int64_t pos, int size)
 {
block/qapi.c +3 -0
index 782051c..ab1dd24 100644
@@ -134,6 +134,9 @@ void bdrv_query_image_info(BlockDriverState *bs,
         info->dirty_flag = bdi.is_dirty;
         info->has_dirty_flag = true;
     }
+    info->format_specific     = bdrv_get_specific_info(bs);
+    info->has_format_specific = info->format_specific != NULL;
+
     backing_filename = bs->backing_file;
     if (backing_filename[0] != '\0') {
         info->backing_filename = g_strdup(backing_filename);
include/block/block.h +1 -0
index f808550..e265124 100644
@@ -335,6 +335,7 @@ int bdrv_get_flags(BlockDriverState *bs);
 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
                           const uint8_t *buf, int nb_sectors);
 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
+ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs);
 void bdrv_round_to_clusters(BlockDriverState *bs,
                             int64_t sector_num, int nb_sectors,
                             int64_t *cluster_sector_num,
include/block/block_int.h +1 -0
index 211087a..17b26b2 100644
@@ -168,6 +168,7 @@ struct BlockDriver {
     int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
                                   const char *snapshot_name);
     int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
+    ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
 
     int (*bdrv_save_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov,
                              int64_t pos);
Signature · 1 line
1.8.3.1
author
#5 lore raw

PATCH v8 5/6 qemu-iotests: Discard specific info in _img_info

In reply to Max Reitz

In _img_info, filter out additional information specific to the image format provided by qemu-img info, since tests designed for multiple image formats would produce different outputs for every image format otherwise.

In a human-readable dump, that new information will always be last for each "image information block" (multiple blocks are emitted when inspecting the backing file chain). Every block is separated by an empty line. Therefore, in this case, everything starting with the line "Format specific information:" up to that empty line (or EOF, if it is the last block) has to be stripped.

The JSON dump will always emit pretty JSON data. Therefore, the opening and closing braces of every object will be on lines which are indented by exactly the same amount, and all lines in between will have more indentation. Thus, in this case, everything starting with a line matching the regular expression /^ *"format-specific": {/ until /^ *},?/ has to be stripped, where the number of spaces at the beginning of the respective lines is equal.

---
 tests/qemu-iotests/common.rc | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.rc +19 -1
index 1b22db0..227c003 100644
@@ -197,12 +197,30 @@ _check_test_img()
 
 _img_info()
 {
+    discard=0
+    regex_json_spec_start='^ *"format-specific": \{'
     $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
         sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
             -e "s#$TEST_DIR#TEST_DIR#g" \
             -e "s#$IMGFMT#IMGFMT#g" \
             -e "/^disk size:/ D" \
-            -e "/actual-size/ D"
+            -e "/actual-size/ D" | \
+        while IFS='' read line; do
+            if [[ $line == "Format specific information:" ]]; then
+                discard=1
+            elif [[ $line =~ $regex_json_spec_start ]]; then
+                discard=2
+                regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
+            fi
+            if [[ $discard == 0 ]]; then
+                echo "$line"
+            elif [[ $discard == 1 && ! $line ]]; then
+                echo
+                discard=0
+            elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
+                discard=0
+            fi
+        done
 }
 
 _get_pids_by_name()
Signature · 1 line
1.8.3.1
author
#6 lore raw

PATCH v8 6/6 qemu-iotests: Additional info from qemu-img info

In reply to Max Reitz

Add a test for the additional information now provided by qemu-img info when used on qcow2 images. It also tests the qemu QMP output from the query-block command when running qemu with different runtime options than specified in the image (ImageInfoSpecific should always refer to the image).

---
 tests/qemu-iotests/065        | 125 ++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/065.out    |   5 ++
 tests/qemu-iotests/group      |   1 +
 tests/qemu-iotests/iotests.py |   4 ++
 4 files changed, 135 insertions(+)
 create mode 100755 tests/qemu-iotests/065
 create mode 100644 tests/qemu-iotests/065.out
tests/qemu-iotests/065 new file +125 -0
new file mode 100755
index 0000000..a4db717
@@ -0,0 +1,125 @@
+#!/usr/bin/env python2
+#
+# Test for additional information emitted by qemu-img info on qcow2
+# images
+#
+# Copyright (C) 2013 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/>.
+#
+
+import os
+import re
+import json
+import iotests
+from iotests import qemu_img, qemu_img_pipe
+import unittest
+
+test_img = os.path.join(iotests.test_dir, 'test.img')
+
+class TestImageInfoSpecific(iotests.QMPTestCase):
+    '''Abstract base class for ImageInfoSpecific tests'''
+
+    def setUp(self):
+        if self.img_options is None:
+            self.skipTest('Skipping abstract test class')
+        qemu_img('create', '-f', iotests.imgfmt, '-o', self.img_options,
+                 test_img, '128K')
+
+    def tearDown(self):
+        os.remove(test_img)
+
+class TestQemuImgInfo(TestImageInfoSpecific):
+    '''Abstract base class for qemu-img info tests'''
+
+    img_options = None
+    json_compare = None
+    human_compare = None
+
+    def test_json(self):
+        data = json.loads(qemu_img_pipe('info', '--output=json', test_img))
+        data = data['format-specific']
+        self.assertEqual(data['type'], iotests.imgfmt)
+        self.assertEqual(data['data'], self.json_compare)
+
+    def test_human(self):
+        data = qemu_img_pipe('info', '--output=human', test_img).split('\n')
+        data = data[(data.index('Format specific information:') + 1)
+                    :data.index('')]
+        for field in data:
+            self.assertTrue(re.match('^ {4}[^ ]', field) is not None)
+        data = map(lambda line: line.strip(), data)
+        self.assertEqual(data, self.human_compare)
+
+class TestQMP(TestImageInfoSpecific):
+    '''Abstract base class for qemu QMP tests'''
+
+    img_options = None
+    qemu_options = ''
+
+    def setUp(self):
+        TestImageInfoSpecific.setUp(self)
+        self.vm = iotests.VM().add_drive(test_img, self.qemu_options)
+        self.vm.launch()
+
+    def tearDown(self):
+        self.vm.shutdown()
+        TestImageInfoSpecific.tearDown(self)
+
+    def test_qmp(self):
+        result = self.vm.qmp('query-block')['return']
+        drive = filter(lambda drive: drive['device'] == 'drive0', result)[0]
+        data = drive['inserted']['image']['format-specific']
+        self.assertEqual(data['type'], iotests.imgfmt)
+        self.assertEqual(data['data'], self.compare)
+
[email protected](iotests.imgfmt != 'qcow2', 'This test is qcow2-specific')
+class TestQCow2(TestQemuImgInfo):
+    '''Testing a qcow2 version 2 image'''
+    img_options = 'compat=0.10'
+    json_compare = { 'compat': '0.10' }
+    human_compare = [ 'compat: 0.10' ]
+
[email protected](iotests.imgfmt != 'qcow2', 'This test is qcow2-specific')
+class TestQCow3NotLazy(TestQemuImgInfo):
+    '''Testing a qcow2 version 3 image with lazy refcounts disabled'''
+    img_options = 'compat=1.1,lazy_refcounts=off'
+    json_compare = { 'compat': '1.1', 'lazy-refcounts': False }
+    human_compare = [ 'compat: 1.1', 'lazy refcounts: false' ]
+
[email protected](iotests.imgfmt != 'qcow2', 'This test is qcow2-specific')
+class TestQCow3Lazy(TestQemuImgInfo):
+    '''Testing a qcow2 version 3 image with lazy refcounts enabled'''
+    img_options = 'compat=1.1,lazy_refcounts=on'
+    json_compare = { 'compat': '1.1', 'lazy-refcounts': True }
+    human_compare = [ 'compat: 1.1', 'lazy refcounts: true' ]
+
[email protected](iotests.imgfmt != 'qcow2', 'This test is qcow2-specific')
+class TestQCow3NotLazyQMP(TestQMP):
+    '''Testing a qcow2 version 3 image with lazy refcounts disabled, opening
+       with lazy refcounts enabled'''
+    img_options = 'compat=1.1,lazy_refcounts=off'
+    qemu_options = 'lazy-refcounts=on'
+    compare = { 'compat': '1.1', 'lazy-refcounts': False }
+
[email protected](iotests.imgfmt != 'qcow2', 'This test is qcow2-specific')
+class TestQCow3LazyQMP(TestQMP):
+    '''Testing a qcow2 version 3 image with lazy refcounts enabled, opening
+       with lazy refcounts disabled'''
+    img_options = 'compat=1.1,lazy_refcounts=on'
+    qemu_options = 'lazy-refcounts=off'
+    compare = { 'compat': '1.1', 'lazy-refcounts': True }
+
+if __name__ == '__main__':
+    iotests.main(supported_fmts=['qcow2'])
tests/qemu-iotests/065.out new file +5 -0
new file mode 100644
index 0000000..1351234
@@ -0,0 +1,5 @@
+........sss
+----------------------------------------------------------------------
+Ran 11 tests
+
+OK (skipped=3)
tests/qemu-iotests/group +1 -0
index 1ad02e5..f1a68b0 100644
@@ -69,3 +69,4 @@
 061 rw auto
 062 rw auto
 063 rw auto
+065 rw auto
tests/qemu-iotests/iotests.py +4 -0
index 376d6e8..fb10ff4 100644
@@ -49,6 +49,10 @@ def qemu_img_verbose(*args):
     '''Run qemu-img without suppressing its output and return the exit code'''
     return subprocess.call(qemu_img_args + list(args))
 
+def qemu_img_pipe(*args):
+    '''Run qemu-img and return its output'''
+    return subprocess.Popen(qemu_img_args + list(args), stdout=subprocess.PIPE).communicate()[0]
+
 def qemu_io(*args):
     '''Run qemu-io and return the stdout data'''
     args = qemu_io_args + list(args)
Signature · 1 line
1.8.3.1
Applied
#7 lore raw
In reply to Max Reitz

Am 09.10.2013 um 10:46 hat Max Reitz geschrieben:

Quoted 6 lines

qemu-img info provides only pretty general information about an image. For any image format, there might be specific options which cannot be represented in a universal way; for instance, qcow2 provides the compatibility and lazy_refcount options whose values are certainly interesting but currently cannot be output by qemu-img info.

Quoted 6 lines

Therefore, this series adds a new ImageInfoSpecific union type to ImageInfo which may be used by block drivers as a template for new types dedicated to the specific information they can provide, as well as a function bdrv_get_specific_info for retrieving this information. It also adds support to qemu-img info and qemu-io -c info to print the content of these specific structures.

Thanks, applied to the block branch.

Kevin