Add JSON output to qemu-img info
v7 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.
in v5:
Eric Blake: -use a constant for getopt parsing to avoid future
short options collision.
-make the command default to --output=human. -fix spurious whitespace change.
-split vm-clock-nsec in two fields vm-clock-sec and
vm-clock-nsec.
-declare JSON structure as "Since 1.3"
in v6:
Blue Swirl: -Add missing const in getopt structure declaration.
Eric Blake: -Remove spurious undef.
-Use an enum instead of two boolean.
in v7:
Kevin Wolf: -Add missing documentation.
-Change bogus comment about chained lists support in qapi. -Remove collect_backing_file_format and use bs->backing_format instead. -Remove uneeded if.
-Change bogus printing logic for backing file format. -Rename Format enum to OutputFormat.
-Use OFORMAT_HUMAN by default and get rid of uneeded else branch -Use error_report insted of fprintf.
-Use a switch instead of ifs.
Benoît Canet: Rename FORMAT_HUMAN and FORMAT_JSON to OFORMAT_JSON and OFORMAT_HUMAN
Benoît Canet (2):
qapi: Add SnapshotInfo and ImageInfo.
qemu-img: Add json output option to the info command.
Makefile | 3 +- qapi-schema.json | 64 +++++++++++++++ qemu-img-cmds.hx | 4 +- qemu-img.c | 232 +++++++++++++++++++++++++++++++++++++++++++----------- qemu-img.texi | 5 +- 5 files changed, 259 insertions(+), 49 deletions(-)
Signature · 1 line
1.7.9.5
PATCH v7 1/2 qapi: Add SnapshotInfo and ImageInfo.
--- qapi-schema.json | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
qapi-schema.json +64 -0
@@ -156,6 +156,70 @@ 'running', 'save-vm', 'shutdown', 'suspended', 'watchdog' ] } ## +# @SnapshotInfo +# +# @id: unique snapshot id +# +# @name: user chosen name +# +# @vm-state-size: size of the VM state +# +# @date-sec: UTC date of the snapshot in seconds +# +# @date-nsec: fractional part in nano seconds to be used with date-sec +# +# @vm-clock-sec: VM clock relative to boot in seconds +# +# @vm-clock-nsec: fractional part in nano seconds to be used with vm-clock-sec +# +# Since: 1.3 +# +## + +{ 'type': 'SnapshotInfo', + 'data': { 'id': 'str', 'name': 'str', 'vm-state-size': 'int', + 'date-sec': 'int', 'date-nsec': 'int', + 'vm-clock-sec': '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.3 +# +## + +{ '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
PATCH v7 2/2 qemu-img: Add json output option to the info command.
In reply to Benoît CanetThis 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": 637102488,
"name": "vm-20120821145509",
"date-sec": 1345553709,
"date-nsec": 220289000,
"vm-clock-sec": 20,
"id": "1",
"vm-state-size": 96522745
},
{
"vm-clock-nsec": 28210866,
"name": "vm-20120821154059",
"date-sec": 1345556459,
"date-nsec": 171392000,
"vm-clock-sec": 46,
"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-cmds.hx | 4 +- qemu-img.c | 232 +++++++++++++++++++++++++++++++++++++++++++----------- qemu-img.texi | 5 +- 4 files changed, 195 insertions(+), 49 deletions(-)
Makefile +2 -1
@@ -157,7 +157,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-cmds.hx +2 -2
@@ -34,9 +34,9 @@ STEXI ETEXI DEF("info", img_info, - "info [-f fmt] filename") + "info [-f fmt] [--output=ofmt] filename") STEXI -@item info [-f @var{fmt}] @var{filename} +@item info [-f @var{fmt}] [--output=@var{ofmt}] @var{filename} ETEXI DEF("snapshot", img_snapshot,
qemu-img.c +188 -44
281 lines not on this page · raw message
qemu-img.texi +3 -2
@@ -129,12 +129,13 @@ created as a copy on write image of the specified base image; the @var{backing_file} should have the same content as the input's base image, however the path, image format, etc may differ. -@item info [-f @var{fmt}] @var{filename} +@item info [-f @var{fmt}] [--output=@var{ofmt}] @var{filename} Give information about the disk image @var{filename}. Use it in particular to know the size reserved on disk which can be different from the displayed size. If VM snapshots are stored in the disk image, -they are displayed too. +they are displayed too. The command can output in the format @var{ofmt} +which is either human or json. @item snapshot [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot} ] @var{filename}
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH V7 2/2] qemu-img: Add json output option to the info command.
Am 05.09.2012 11:25, schrieb Benoît Canet:
Quoted 33 lines
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": 637102488, "name": "vm-20120821145509", "date-sec": 1345553709, "date-nsec": 220289000, "vm-clock-sec": 20, "id": "1", "vm-state-size": 96522745 }, { "vm-clock-nsec": 28210866, "name": "vm-20120821154059", "date-sec": 1345556459, "date-nsec": 171392000, "vm-clock-sec": 46, "id": "2", "vm-state-size": 101208714 } ], "virtual-size": 1073741824, "filename": "snap.qcow2", "cluster-size": 65536, "format": "qcow2", "actual-size": 985587712, "dirty-flag": false }
Quoted 1 line
Signed-off-by Benoit Canet <[email protected]>
Looks much better, but I still have a few comments:
Quoted 5 lines
qemu-img.c +5
-0+bdrv_get_backing_filename (bs, backing_filename,sizeof (backing_filename)); +if (backing_filename[0 ] !='\0' ) { + info->backing_filename =g_strdup (backing_filename); + info->has_backing_filename = true; + bdrv_get_full_backing_filename(bs, backing_filename2,
qemu-img.c static void dump_snapshots(BlockDriverState *bs) · line 1203
Quoted 10 lines
qemu-img.c +10
-0+ sizeof(backing_filename2)); + +if (strcmp (backing_filename, backing_filename2) !=0 ) { + info->full_backing_filename = +g_strdup (backing_filename2); + info->has_full_backing_filename = true; + } + + info->backing_filename_format = bs->backing_format; + info->has_backing_filename_format = true;
I believe we should check bs->backing_format[0] first. If it's empty, don't include the format.
Quoted 28 lines
qemu-img.c +28
-0+ } +} + +static void dump_human_image_info (ImageInfo *info) +{ +char size_buf[128 ], dsize_buf[128 ]; +if (!info->has_actual_size) { +snprintf (dsize_buf,sizeof (dsize_buf),"unavailable" ); + }else { +get_human_readable_size (dsize_buf,sizeof (dsize_buf), + info->actual_size); + } +get_human_readable_size (size_buf,sizeof (size_buf), info->virtual_size); +printf ("image: %s\n" +"file format: %s\n" +"virtual size: %s (%" PRId64" bytes)\n" +"disk size: %s\n" , + info->filename, info->format, size_buf, + info->virtual_size, + dsize_buf); + +if (info->has_encrypted && info->encrypted) { +printf ("encrypted: yes\n" ); + } + +if (info->has_cluster_size) { +printf ("cluster_size: %" PRId64"\n" , info->cluster_size); + }
qemu-img.c static void dump_snapshots(BlockDriverState *bs) · line 1241
Quoted 10 lines
qemu-img.c +10
-0+ +if (info->has_dirty_flag && info->dirty_flag) { +printf ("cleanly shut down: no\n" ); + } + +if (info->has_backing_filename) { +printf ("backing file: %s" , info->backing_filename); +if (info->has_full_backing_filename) { +printf (" (actual path: %s)" , info->full_backing_filename); + }
Maybe add the backing file format here if it's available?
Quoted 9 lines
qemu-img.c +5
-0@@ -1135,48 +1291,36 @@ static int img_info(int argc, char **argv) } filename = argv[optind++]; +if (output && !strcmp (output,"json" )) { + output_format =OFORMAT_JSON ; + }else if (output && !strcmp (output,"human" )) { + output_format =OFORMAT_HUMAN ; + }else if (output) {
qemu-img.c static int img_info(int argc, char **argv) · line 1308
Quoted 10 lines
qemu-img.c +6
-0+error_report ("--output must be used with human or json as argument." ); +return 1 ; + } + + info =g_new0 (ImageInfo,1 ); bs =bdrv_new_open (filename, fmt,BDRV_O_FLAGS |BDRV_O_NO_BACKING );if (!bs) { +g_free (info);return 1 ; }
If you move thee g_new0 below, you don't have to free here.
Quoted 7 lines
qemu-img.texi +0
-0@@ -129,12 +129,13 @@ created as a copy on write image of the specified base image; the @var{backing_file} should have the same content as the input's base image, however the path, image format, etc may differ.
qemu-img.texi created as a copy on write image of the specified base image; the · line 138
Quoted 10 lines
qemu-img.texi +3
-2-@item info [-f @var{fmt}] @var{filename} +@item info [-f @var{fmt}] [--output=@var{ofmt}] @var{filename} Give information about the disk image @var{filename}. Use it in particular to know the size reserved on disk which can be different from the displayed size. If VM snapshots are stored in the disk image, -they are displayed too. +they are displayed too. The command can output in the format @var{ofmt} +which is either human or json.
"...@code{human} or @code{json}" is better, I think.
Kevin