[libvirt] [PATCH 01/19] virsh: Add QMP command wrapping for 'qemu-monitor-command'

Peter Krempa pkrempa at redhat.com
Thu Dec 12 17:18:31 UTC 2019


Issuing simple QMP commands is pain as they need to be wrapped by the
JSON wrapper:

 { "execute": "COMMAND" }

and optionally also:

 { "execute": "COMMAND", "arguments":...}

For simple commands without arguments we can add syntax sugar to virsh
which allows simple usage of QMP and additionally prepares also for
passing through of the 'arguments' section.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 docs/manpages/virsh.rst | 24 +++++++++++++++++-------
 tools/virsh-domain.c    | 32 +++++++++++++++++++++++++++++---
 2 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
index e9d6deaee1..697f83e5b2 100644
--- a/docs/manpages/virsh.rst
+++ b/docs/manpages/virsh.rst
@@ -7434,16 +7434,26 @@ qemu-monitor-command

 .. code-block:: shell

-   qemu-monitor-command domain { [--hmp] | [--pretty] } command...
+   qemu-monitor-command domain { [--hmp] | [--qmp] [--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
+qemu monitor.  The results of the command will be printed on stdout.
+
+*command* is directly passed to qemu. 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.
+
+If *--qmp* is passed the first argument passed as *command* is used as a QMP
+command name and appropriately wrapped into a JSON block. Additionally a second
+argument passed as *command* is appended as value of the 'arguments' parameter
+of the QMP command verbatim.
+
+If *--pretty* is given, and the monitor uses QMP, then the output will be
+pretty-printed.
+
+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.
+the result will also be converted back from QMP.


 qemu-agent-command
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 56137bdd74..9447fa2904 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -9522,6 +9522,10 @@ static const vshCmdOptDef opts_qemu_monitor_command[] = {
      .type = VSH_OT_BOOL,
      .help = N_("pretty-print any qemu monitor protocol output")
     },
+    {.name = "qmp",
+     .type = VSH_OT_BOOL,
+     .help = N_("wrap the 'cmd' argument in JSON wrapper for QMP")
+    },
     {.name = "cmd",
      .type = VSH_OT_ARGV,
      .flags = VSH_OFLAG_REQ,
@@ -9539,16 +9543,38 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd)
     unsigned int flags = 0;
     const vshCmdOpt *opt = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
+    bool qmp = vshCommandOptBool(cmd, "qmp");

     VSH_EXCLUSIVE_OPTIONS("hmp", "pretty");
+    VSH_EXCLUSIVE_OPTIONS("hmp", "qmp");

     if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
         return false;

-    while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
-        virBufferAsprintf(&buf, "%s ", opt->data);
+    if (qmp) {
+        const char *command = NULL;
+        const char *arguments = NULL;

-    virBufferTrim(&buf, " ", -1);
+        if ((opt = vshCommandOptArgv(ctl, cmd, opt)))
+            command = opt->data;
+        if ((opt = vshCommandOptArgv(ctl, cmd, opt)))
+            arguments = opt->data;
+
+        if (!command || (arguments && vshCommandOptArgv(ctl, cmd, opt))) {
+            vshError(ctl, "%s", _("-qmp option requires 1 or 2 arguments"));
+            return false;
+        }
+
+        virBufferAsprintf(&buf, "{\"execute\":\"%s\"", command);
+        if (arguments)
+            virBufferAsprintf(&buf, ", \"arguments\":%s", arguments);
+        virBufferAddLit(&buf, "}");
+    } else {
+        while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
+            virBufferAsprintf(&buf, "%s ", opt->data);
+
+        virBufferTrim(&buf, " ", -1);
+    }

     monitor_cmd = virBufferContentAndReset(&buf);

-- 
2.23.0




More information about the libvir-list mailing list