[PATCH 0/2] virsh: qemu-monitor-command: Improve docs and retrun value handling

2/2 was already reviewed, but it depends on the docs patch. Peter Krempa (2): docs: virsh: Modernize docs for qemu-monitor-command virsh: Allow extracting 'return' section of QMP command in 'qemu-monitor-command' docs/manpages/virsh.rst | 24 ++++++++++++++-------- tools/virsh-domain.c | 44 ++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 17 deletions(-) -- 2.24.1

Mention that we actually by default use QMP and clarify the rest of the documentation. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- docs/manpages/virsh.rst | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 105b0d8f03..e10e5463a3 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -7485,13 +7485,18 @@ qemu-monitor-command qemu-monitor-command domain { [--hmp] | [--pretty] } command... Send an arbitrary monitor command *command* to domain *domain* through the -QEMU monitor. The results of the command will be printed on stdout. If -*--hmp* is passed, the command is considered to be a human monitor command -and libvirt will automatically convert it into QMP if needed. In that case -the result will also be converted back from QMP. If *--pretty* is given, -and the monitor uses QMP, then the output will be pretty-printed. If more -than one argument is provided for *command*, they are concatenated with a -space in between before passing the single command to the monitor. +QEMU monitor. The results of the command will be printed on stdout. + +If more than one argument is provided for *command*, they are concatenated with +a space in between before passing the single command to the monitor. + +Note that libvirt uses the QMP to talk to qemu so *command* must be valid JSON +in QMP format to work properly. + +If *--pretty* is given the QMP reply is pretty-printed. + +If *--hmp* is passed, the command is considered to be a human monitor command +and libvirt will automatically convert it into QMP and convert the result back. qemu-agent-command -- 2.24.1

On 2/7/20 3:07 AM, Peter Krempa wrote:
Mention that we actually by default use QMP and clarify the rest of the documentation.
Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- docs/manpages/virsh.rst | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 105b0d8f03..e10e5463a3 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -7485,13 +7485,18 @@ qemu-monitor-command qemu-monitor-command domain { [--hmp] | [--pretty] } command...
Send an arbitrary monitor command *command* to domain *domain* through the -QEMU monitor. The results of the command will be printed on stdout. If -*--hmp* is passed, the command is considered to be a human monitor command -and libvirt will automatically convert it into QMP if needed. In that case -the result will also be converted back from QMP. If *--pretty* is given, -and the monitor uses QMP, then the output will be pretty-printed. If more -than one argument is provided for *command*, they are concatenated with a -space in between before passing the single command to the monitor. +QEMU monitor. The results of the command will be printed on stdout. + +If more than one argument is provided for *command*, they are concatenated with +a space in between before passing the single command to the monitor. + +Note that libvirt uses the QMP to talk to qemu so *command* must be valid JSON +in QMP format to work properly.
Do we want to call any additional attention to the fact that libvirt will NOT accept qemu's extensions beyond JSON (such as 'string' in place of "string")? Otherwise, Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

Simplify gathering the actual return value from a passed-through QMP command when using 'qemu-monitor-command' by adding '--return-value' switch which just extracts the 'return' section and alternatively reports an error if the section is not present. This simplifies gathering of some test data where the full reply would need to be trimmed just for the actual return value. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> --- docs/manpages/virsh.rst | 5 ++++- tools/virsh-domain.c | 44 ++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index e10e5463a3..dc404ddfe8 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -7482,7 +7482,7 @@ qemu-monitor-command .. code-block:: - qemu-monitor-command domain { [--hmp] | [--pretty] } command... + qemu-monitor-command domain { [--hmp] | [--pretty] [--return-value] } command... Send an arbitrary monitor command *command* to domain *domain* through the QEMU monitor. The results of the command will be printed on stdout. @@ -7495,6 +7495,9 @@ in QMP format to work properly. If *--pretty* is given the QMP reply is pretty-printed. +If *--return-value* is given the 'return' key of the QMP response object is +extracted rather than passing through the full reply from QEMU. + If *--hmp* is passed, the command is considered to be a human monitor command and libvirt will automatically convert it into QMP and convert the result back. diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 781463f0e2..409aa22ca6 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -9544,6 +9544,10 @@ static const vshCmdOptDef opts_qemu_monitor_command[] = { .type = VSH_OT_BOOL, .help = N_("pretty-print any qemu monitor protocol output") }, + {.name = "return-value", + .type = VSH_OT_BOOL, + .help = N_("extract the value of the 'return' key from the returned string") + }, {.name = "cmd", .type = VSH_OT_ARGV, .flags = VSH_OFLAG_REQ, @@ -9558,11 +9562,17 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd) g_autoptr(virshDomain) dom = NULL; g_autofree char *monitor_cmd = NULL; g_autofree char *result = NULL; + g_autoptr(virJSONValue) resultjson = NULL; unsigned int flags = 0; const vshCmdOpt *opt = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; + bool pretty = vshCommandOptBool(cmd, "pretty"); + bool returnval = vshCommandOptBool(cmd, "return-value"); + virJSONValuePtr formatjson; + g_autofree char *jsonstr = NULL; VSH_EXCLUSIVE_OPTIONS("hmp", "pretty"); + VSH_EXCLUSIVE_OPTIONS("hmp", "return-value"); if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) return false; @@ -9580,17 +9590,33 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd) if (virDomainQemuMonitorCommand(dom, monitor_cmd, &result, flags) < 0) return false; - if (vshCommandOptBool(cmd, "pretty")) { - char *tmp; - if ((tmp = virJSONStringReformat(result, true))) { - VIR_FREE(result); - result = tmp; - virTrimSpaces(result, NULL); - } else { - vshResetLibvirtError(); + if (returnval || pretty) { + resultjson = virJSONValueFromString(result); + + if (returnval && !resultjson) { + vshError(ctl, "failed to parse JSON returned by qemu"); + return false; } } - vshPrint(ctl, "%s\n", result); + + /* print raw non-prettified result */ + if (!resultjson) { + vshPrint(ctl, "%s\n", result); + return true; + } + + if (returnval) { + if (!(formatjson = virJSONValueObjectGet(resultjson, "return"))) { + vshError(ctl, "'return' member missing"); + return false; + } + } else { + formatjson = resultjson; + } + + jsonstr = virJSONValueToString(formatjson, pretty); + virTrimSpaces(jsonstr, NULL); + vshPrint(ctl, "%s", jsonstr); return true; } -- 2.24.1
participants (2)
-
Eric Blake
-
Peter Krempa