Skip to contentSkip to conversation

Add JSON output to qemu-img info

PATCH Superseded Benoît Canet 3 participants 10 messages Aug 22, 2012 – Aug 23, 2012 Kbuild

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add JSON output to qemu-img info

This patchset add a JSON output mode to the qemu-img info command. It's a rewrite from scratch of the original patchset by Wenchao Xia following Anthony Liguori advices on JSON formating.

the --output=(json|human) option is now mandatory on the command line.

Benoît Canet (3):
qapi: Add SnapshotInfo.
qapi: Add ImageInfo.
qemu-img: Add json output option to the info command.

in v2:

eblake: make some field optionals
squash the two qapi patchs together
fix a typo on vm_clock_nsec

bcanet: fix a potential memory leak

in v3:

lcapitulino:
remove unneeded test
put '\n' at the end of json in printf statement drop the uneeded head pointer in collect_snapshots

in v4:

Wenchao Xia && Kevin Wolf: -Refactor to separate rate ImageInfo
collection from human printing.

Kevin Wolf: -Use --output=(json|human).
-make the two choice exclusive and print a message if none is specified.
-cosmetic '=' alignement in collect snapshots.

Benoît Canet: -add full-backing-filename to the ImageInfo structure
(needed for human printing)
-make ImageInfo->actual_size optional depending on the context.

Benoît Canet (2):
qapi: Add SnapshotInfo and ImageInfo.
qemu-img: Add json output option to the info command.

 Makefile         |    3 +-
 qapi-schema.json |   62 ++++++++++++++
 qemu-img.c       |  252 ++++++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 272 insertions(+), 45 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v4 1/2 qapi: Add SnapshotInfo and ImageInfo.

---
 qapi-schema.json |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
qapi-schema.json +62 -0
index a92adb1..4c4b9b9 100644
@@ -126,6 +126,68 @@
             'running', 'save-vm', 'shutdown', 'suspended', 'watchdog' ] }
 
 ##
+# @SnapshotInfo
+#
+# @id: unique snapshot id
+#
+# @name: user choosen name
+#
+# @vm-state-size: size of the VM state
+#
+# @date-sec: UTC date of the snapshot
+#
+# @date-nsec: date in nano seconds
+#
+# @vm-clock-nsec: VM clock relative to boot in nano seconds
+#
+# Since: 1.2
+#
+##
+
+{ 'type': 'SnapshotInfo',
+  'data': { 'id': 'str', 'name': 'str', 'vm-state-size': 'int',
+            'date-sec': 'int', 'date-nsec': 'int',
+            'vm-clock-nsec': 'int' } }
+
+##
+# @ImageInfo:
+#
+# Information about a QEMU image file
+#
+# @filename: name of the image file
+#
+# @format: format of the image file
+#
+# @virtual-size: maximum capacity in bytes of the image
+#
+# @actual-size: #optional actual size on disk in bytes of the image
+#
+# @dirty-flag: #optional true if image is not cleanly closed
+#
+# @cluster-size: #optional size of a cluster in bytes
+#
+# @encrypted: #optional true if the image is encrypted
+#
+# @backing-filename: #optional name of the backing file
+#
+# @full-backing-filename: #optional full path of the backing file
+#
+# @backing-filename-format: #optional the format of the backing file
+#
+# @snapshots: #optional list of VM snapshots
+#
+# Since: 1.2
+#
+##
+
+{ 'type': 'ImageInfo',
+  'data': {'filename': 'str', 'format': 'str', '*dirty-flag': 'bool',
+           '*actual-size': 'int', 'virtual-size': 'int',
+           '*cluster-size': 'int', '*encrypted': 'bool',
+           '*backing-filename': 'str', '*full-backing-filename': 'str',
+           '*backing-filename-format': 'str', '*snapshots': ['SnapshotInfo'] } }
+
+##
 # @StatusInfo:
 #
 # Information about VCPU run state
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [Qemu-devel] [PATCH V4 1/2] qapi: Add SnapshotInfo and ImageInfo.

Quoted 10 lines · Benoît Canet
---
 qapi-schema.json |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
qapi-schema.json +0 -0
index a92adb1..4c4b9b9 100644

qapi-schema.json line 133

Quoted 8 lines
qapi-schema.json +5 -0
@@ -126,6 +126,68 @@
             'running', 'save-vm', 'shutdown', 'suspended', 'watchdog' ] }
 
 ##
+# @SnapshotInfo
+#
+# @id: unique snapshot id
+#
+# @name: user choosen name

s/choosen/chosen/

qapi-schema.json line 141

Quoted 8 lines
qapi-schema.json +8 -0
+#
+# @vm-state-size: size of the VM state
+#
+# @date-sec: UTC date of the snapshot
+#
+# @date-nsec: date in nano seconds
+#
+# @vm-clock-nsec: VM clock relative to boot in nano seconds

Since we have two fields named *-nsec, it might be worth clarifying that date-nsec is merely the fractional portion to be combined with date-sec (always less than 1000000000), while vm-clock-nsec includes seconds if the drift is that large.

For that matter, should we even be exposing things in this manner? I know the internal struct has seconds and nanos separate for date, because it maps to struct timespec; but why can't we combine them into one giant number for JSON? Or are we worried about exceeding int64_t if we take seconds * 1000000000 when dealing with the timestamp the snapshot was created, even though we aren't worried about the VM clock being that far away from boot?

qapi-schema.json line 143

Quoted 2 lines
qapi-schema.json +2 -0
+#
+# Since: 1.2

Probably too late for 1.2, so this should be 1.3.

qapi-schema.json line 163

Quoted 12 lines
qapi-schema.json +12 -0
+##
+# @ImageInfo:
+#
+# Information about a QEMU image file
+#
+# @filename: name of the image file
+#
+# @format: format of the image file
+#
+# @virtual-size: maximum capacity in bytes of the image
+#
+# @actual-size: #optional actual size on disk in bytes of the image

That seems backwards - with a raw file, don't you know the actual size, but have no idea what it can further grow to?

Quoted 6 lines
qapi-schema.json +6 -0
+#
+# @dirty-flag: #optional true if image is not cleanly closed
+#
+# @cluster-size: #optional size of a cluster in bytes
+#
+# @encrypted: #optional true if the image is encrypted

qapi-schema.json line 179

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# @backing-filename: #optional name of the backing file
+#
+# @full-backing-filename: #optional full path of the backing file
+#
+# @backing-filename-format: #optional the format of the backing file
+#
+# @snapshots: #optional list of VM snapshots
+#
+# Since: 1.2

Again, 1.3.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH V4 1/2] qapi: Add SnapshotInfo and ImageInfo.

Quoted 36 lines · Eric Blake
Quoted 10 lines · Benoît Canet
---
 qapi-schema.json |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
qapi-schema.json +0 -0
index a92adb1..4c4b9b9 100644

qapi-schema.json line 133

Quoted 8 lines
qapi-schema.json +5 -0
@@ -126,6 +126,68 @@
             'running', 'save-vm', 'shutdown', 'suspended', 'watchdog' ] }
 
 ##
+# @SnapshotInfo
+#
+# @id: unique snapshot id
+#
+# @name: user choosen name

s/choosen/chosen/

qapi-schema.json line 141

Quoted 8 lines
qapi-schema.json +8 -0
+#
+# @vm-state-size: size of the VM state
+#
+# @date-sec: UTC date of the snapshot
+#
+# @date-nsec: date in nano seconds
+#
+# @vm-clock-nsec: VM clock relative to boot in nano seconds

Since we have two fields named *-nsec, it might be worth clarifying that date-nsec is merely the fractional portion to be combined with date-sec (always less than 1000000000), while vm-clock-nsec includes seconds if the drift is that large.

Quoted 4 lines

For that matter, should we even be exposing things in this manner? I know the internal struct has seconds and nanos separate for date, because it maps to struct timespec; but why can't we combine them into one giant number for JSON?

Wouldn't people working with low level language be annoyed after parsing this JSON to have to split this combined number in two parts to fit them back into struct timespec ?

Quoted 49 lines

Or are we worried about exceeding int64_t if
we take seconds * 1000000000 when dealing with the timestamp the snapshot was created, even though we aren't worried about the VM clock being that far away from boot?

qapi-schema.json line 143

Quoted 2 lines
qapi-schema.json +2 -0
+#
+# Since: 1.2

Probably too late for 1.2, so this should be 1.3.

qapi-schema.json line 163

Quoted 12 lines
qapi-schema.json +12 -0
+##
+# @ImageInfo:
+#
+# Information about a QEMU image file
+#
+# @filename: name of the image file
+#
+# @format: format of the image file
+#
+# @virtual-size: maximum capacity in bytes of the image
+#
+# @actual-size: #optional actual size on disk in bytes of the image

That seems backwards - with a raw file, don't you know the actual size, but have no idea what it can further grow to?

Quoted 6 lines
qapi-schema.json +6 -0
+#
+# @dirty-flag: #optional true if image is not cleanly closed
+#
+# @cluster-size: #optional size of a cluster in bytes
+#
+# @encrypted: #optional true if the image is encrypted

qapi-schema.json line 179

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# @backing-filename: #optional name of the backing file
+#
+# @full-backing-filename: #optional full path of the backing file
+#
+# @backing-filename-format: #optional the format of the backing file
+#
+# @snapshots: #optional list of VM snapshots
+#
+# Since: 1.2

Again, 1.3.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH V4 1/2] qapi: Add SnapshotInfo and ImageInfo.

Quoted 5 lines · Benoît Canet
Quoted 5 lines

Since we have two fields named *-nsec, it might be worth clarifying that date-nsec is merely the fractional portion to be combined with date-sec (always less than 1000000000), while vm-clock-nsec includes seconds if the drift is that large.

Quoted 8 lines
Quoted 4 lines

For that matter, should we even be exposing things in this manner? I know the internal struct has seconds and nanos separate for date, because it maps to struct timespec; but why can't we combine them into one giant number for JSON?

Wouldn't people working with low level language be annoyed after parsing this JSON to have to split this combined number in two parts to fit them back into struct timespec ?

Perhaps, in which case, why don't we present vm-clock-nsec via two fields of seconds and fraction, for the same reasoning? My point is that we have two different bike shed colors showing in this one API, but I would prefer we be consistent and pick just one (as to _which_ color, I can be persuaded either way).

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH V4 1/2] qapi: Add SnapshotInfo and ImageInfo.

Quoted 11 lines · Eric Blake
Quoted 10 lines · Benoît Canet
Quoted 9 lines

Since we have two fields named *-nsec, it might be worth clarifying that date-nsec is merely the fractional portion to be combined with date-sec (always less than 1000000000), while vm-clock-nsec includes seconds if the drift is that large.

For that matter, should we even be exposing things in this manner? I know the internal struct has seconds and nanos separate for date, because it maps to struct timespec; but why can't we combine them into one giant number for JSON?

Quoted 6 lines
Quoted 3 lines

Wouldn't people working with low level language be annoyed after parsing this JSON to have to split this combined number in two parts to fit them back into struct timespec ?

Perhaps, in which case, why don't we present vm-clock-nsec via two fields of seconds and fraction, for the same reasoning?

I'll do that.

Quoted 9 lines

My point is
that we have two different bike shed colors showing in this one API, but I would prefer we be consistent and pick just one (as to _which_ color, I can be persuaded either way).

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH V4 1/2] qapi: Add SnapshotInfo and ImageInfo.

In reply to Eric Blake
Quoted 2 lines

That seems backwards - with a raw file, don't you know the actual size, but have no idea what it can further grow to?

In raw it commes from fstat() with the field st.st_blocks.

from man:
blkcnt_t st_blocks; /* number of 512B blocks allocated */

Benoît

#7 lore raw

Re: [Qemu-devel] [PATCH V4 1/2] qapi: Add SnapshotInfo and ImageInfo.

In reply to Eric Blake
Quoted 7 lines
Quoted 7 lines
qapi-schema.json +7 -0
+##
+# @ImageInfo:
+#
+# Information about a QEMU image file
+#
+# @filename: name of the image file
+#
Quoted 8 lines

qapi-schema.json line 163

Quoted 5 lines
qapi-schema.json +5 -0
+# @format: format of the image file
+#
+# @virtual-size: maximum capacity in bytes of the image
+#
+# @actual-size: #optional actual size on disk in bytes of the image

That seems backwards - with a raw file, don't you know the actual size, but have no idea what it can further grow to?

Try the current qemy-img info on a raw file you will be surprised of the results. This field also can have an "unavailable" value.

example result with unpatched qemu-img:

benoit@Laure:~/qemu$ qemu-img info truc1.raw 
image: truc1.raw
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 776M

disk size is actual-size.

Benoît

#8 lore raw

PATCH v4 2/2 qemu-img: Add json output option to the info command.

In reply to Benoît Canet

This option --output=[human|json] make qemu-img info output on human or JSON representation at the choice of the user.

example:

{
    "snapshots": [
        {
            "vm-clock-nsec": 20637102488,
            "name": "vm-20120821145509",
            "date-sec": 1345553709,
            "date-nsec": 220289000,
            "id": "1",
            "vm-state-size": 96522745
        },
        {
            "vm-clock-nsec": 46028210866,
            "name": "vm-20120821154059",
            "date-sec": 1345556459,
            "date-nsec": 171392000,
            "id": "2",
            "vm-state-size": 101208714
        }
    ],
    "virtual-size": 1073741824,
    "filename": "snap.qcow2",
    "cluster-size": 65536,
    "format": "qcow2",
    "actual-size": 985587712,
    "dirty-flag": false
}
---
 Makefile   |    3 +-
 qemu-img.c |  252 +++++++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 210 insertions(+), 45 deletions(-)
Makefile +2 -1
index ab82ef3..9ba064b 100644
@@ -160,7 +160,8 @@ tools-obj-y = $(oslib-obj-y) $(trace-obj-y) qemu-tool.o qemu-timer.o \
 	iohandler.o cutils.o iov.o async.o
 tools-obj-$(CONFIG_POSIX) += compatfd.o
 
-qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y)
+qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y) $(qapi-obj-y) \
+                              qapi-visit.o qapi-types.o
 qemu-nbd$(EXESUF): qemu-nbd.o $(tools-obj-y) $(block-obj-y)
 qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
 
qemu-img.c +208 -44

312 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#9 lore raw

Re: [Qemu-devel] [PATCH V4 2/2] qemu-img: Add json output option to the info command.

qemu-img.c out: · line 1092

Quoted 11 lines · Benoît Canet

This option --output=[human|json] make qemu-img info output on human or JSON representation at the choice of the user.

qemu-img.c +0 -1
@@ -1083,7 +1088,6 @@ out:
     return 0;
 }
 
-
 static void dump_snapshots(BlockDriverState *bs)
 {

Spurious whitespace change.

Quoted 7 lines
qemu-img.c +7 -0
+static int img_info(int argc, char **argv)
+{
+    int c;
+    bool human = false, json = false;
+    const char *filename, *fmt, *output;
+    BlockDriverState *bs;
+    ImageInfo *info;

qemu-img.c static void dump_snapshots(BlockDriverState *bs) · line 1286

Quoted 10 lines
qemu-img.c +6 -1
 
     fmt = NULL;
+    output = NULL;
     for(;;) {
-        c = getopt(argc, argv, "f:h");
+        int option_index = 0;
+        static struct option long_options[] = {
+            {"help", no_argument, 0, 'h'},
+            {"format", required_argument, 0, 'f'},
+            {"output", required_argument, 0, 'm'},

I would define a constant, such as 'enum {OPTION_FORMAT=256};', so that you don't risk future confusion...

qemu-img.c static void dump_snapshots(BlockDriverState *bs) · line 1289

Quoted 3 lines
qemu-img.c +3 -0
+            {0, 0, 0, 0}
+        };
+        c = getopt_long(argc, argv, "f:h",

...if we later do add an 'm' short option.

qemu-img.c static int img_info(int argc, char **argv) · line 1302

Quoted 9 lines
qemu-img.c +2 -0
+                        long_options, &option_index);
         if (c == -1) {
             break;
         }
@@ -1128,6 +1299,9 @@ static int img_info(int argc, char **argv)
         case 'f':
             fmt = optarg;
             break;
+        case 'm':

Given the above, this would reuse your new named value.

qemu-img.c static int img_info(int argc, char **argv) · line 1312

Quoted 5 lines
qemu-img.c +1 -0
@@ -1135,52 +1309,42 @@ static int img_info(int argc, char **argv)
     }
     filename = argv[optind++];
 
+    if (output && !strncmp(output, "json", strlen("json"))) {

Why strncmp? It ignores trailing garbage (as in --output=jsonoops). Stick to strcmp.

qemu-img.c static int img_info(int argc, char **argv) · line 1314

Quoted 2 lines
qemu-img.c +2 -0
+        json = true;
+    } else if (output && !strncmp(output, "human", strlen("human"))) {

And again.

qemu-img.c static int img_info(int argc, char **argv) · line 1320

Quoted 6 lines
qemu-img.c +6 -0
+        human = true;
+    } else {
+        fprintf(stderr,
+            "Error: --output must be used with human or json as argument.\n");
+        return 1;
+    }

If we get here, and --output=... was not given, then both human and json are false. That's a problem, since...

Quoted 12 lines
qemu-img.c +4 -7
+
+    if (human) {
+        dump_human_image_info(info);
+        dump_snapshots(bs);
     }
-    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
-    if (backing_filename[0] != '\0') {
-        bdrv_get_full_backing_filename(bs, backing_filename2,
-                                       sizeof(backing_filename2));
-        printf("backing file: %s", backing_filename);
-        if (strcmp(backing_filename, backing_filename2) != 0) {
-            printf(" (actual path: %s)", backing_filename2);

qemu-img.c static int img_info(int argc, char **argv) · line 1341

Quoted 10 lines
qemu-img.c +6 -3
-        }
-        putchar('\n');
+
+    if (json) {
+        collect_snapshots(bs, info);
+        dump_json_image_info(info);
     }
-    dump_snapshots(bs);
+
+    qapi_free_ImageInfo(info);

...you will end up with no output. You want to default to human output for back-compat to older usage.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org