From nobody Wed Apr 24 09:18:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1631632596; cv=none; d=zohomail.com; s=zohoarc; b=XooYsqPRakDbJYVn+mYrZ/0p7bfAtmitTcyin4/+hoHKGzHhRMW6MmNiyuB7Wu+qAvp8NTOgB34iyNF7ZkEIwSpd7DL0ksH/q12MvQT/6lniGgMXLbpemcZLzKbC9lak0yC5wNd+vAWBIuf0lH2U5G6YNyjeYYxl1QtpOWz9XQs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1631632596; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=PQ6JoioTytfKLsj8qf/atdLZW2E8MpLTMh8Nqry0Y/Y=; b=gAmfK2BP7i/sGtCAorHaypvwQERD7LLrmq/Q+E+Xd2WeY4EgMDJ44EojOY0d06PqMuwTHK8HJyby03Ah100TIDN3CQCa31OqcP5UvWDjInJ/6J1XDT2iwSKns/9tu6HSWxTGF2qV0BANvq59Q5+xH5ByTJZl41SQN1fIVKii0Ok= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1631632596582719.6471199577019; Tue, 14 Sep 2021 08:16:36 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-589-9Wo1se5qPvSJu-miZyRrig-1; Tue, 14 Sep 2021 11:16:34 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 75BDE1054F91; Tue, 14 Sep 2021 15:16:28 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0BB5360C82; Tue, 14 Sep 2021 15:16:27 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5AFFE1809C84; Tue, 14 Sep 2021 15:16:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 18EFGNik024936 for ; Tue, 14 Sep 2021 11:16:23 -0400 Received: by smtp.corp.redhat.com (Postfix) id AF7ED5D9DC; Tue, 14 Sep 2021 15:16:23 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id 092D15D9CA for ; Tue, 14 Sep 2021 15:16:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631632595; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=PQ6JoioTytfKLsj8qf/atdLZW2E8MpLTMh8Nqry0Y/Y=; b=SoqEjAE1juxO9xDlmUak8gtCPE5N1DWReNp+RbkAqCBEKWZeTLBZzwZv3KOkJGWor/5uLn PPFdF5HsopdZAUtb/TkKZHR8s08a0D57JFooHuIICclqmTX6eddqRmuSVcDqDVOI/MnrU2 MbPDz+tqUQmbiuDtTjixDpDMtiBqvsU= X-MC-Unique: 9Wo1se5qPvSJu-miZyRrig-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH] virsh: Add QMP command wrapping for 'qemu-monitor-command' Date: Tue, 14 Sep 2021 17:16:11 +0200 Message-Id: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1631632598479100001 Content-Type: text/plain; charset="utf-8" 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: virsh qemu-monitor-command --qmpwrap $VM query-status is equivalent to virsh qemu-monitor-command $VM '{"execute":"query-status"}' and virsh qemu-monitor-command --qmpwrap $VM query-named-block-nodes '{"flat":= true}' is equivalent to virsh qemu-monitor-command $VM '{"execute":"query-named-block-nodes", "arg= uments":{"flat":true}}' Signed-off-by: Peter Krempa --- I originally called the flag '--qmp' but that could be confusing given that we have '--hmp'. On the other hand it's shorter so I wouldn't mind turning it back to '--qmp'. docs/manpages/virsh.rst | 9 ++++++++- tools/virsh-domain.c | 31 ++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 9561b3f59d..b8cb3cfbc2 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -7694,7 +7694,8 @@ qemu-monitor-command :: - qemu-monitor-command domain { [--hmp] | [--pretty] [--return-value] } c= ommand... + qemu-monitor-command domain { [--hmp] | [--qmpwrap] [--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. @@ -7705,6 +7706,12 @@ 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 *--qmpwrap* is passed the first argument passed as *command* is used as= a QMP +command name and appropriately wrapped into a JSON block. Additionally a s= econd +argument passed as *command* is appended as value of the 'arguments' param= eter +of the QMP command verbatim. *Note* that in the future automatic formatting +*--qmpwrap* of the arguments might be added, so scripts should format thei= r own JSON. + If *--pretty* is given the QMP reply is pretty-printed. If *--return-value* is given the 'return' key of the QMP response object is diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index e5bd1fdd75..ac57196b20 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -9380,6 +9380,10 @@ static const vshCmdOptDef opts_qemu_monitor_command[= ] =3D { .type =3D VSH_OT_BOOL, .help =3D N_("extract the value of the 'return' key from the returned= string") }, + {.name =3D "qmpwrap", + .type =3D VSH_OT_BOOL, + .help =3D N_("wrap the 'cmd' argument in JSON wrapper for QMP") + }, {.name =3D "cmd", .type =3D VSH_OT_ARGV, .flags =3D VSH_OFLAG_REQ, @@ -9405,14 +9409,35 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd= *cmd) VSH_EXCLUSIVE_OPTIONS("hmp", "pretty"); VSH_EXCLUSIVE_OPTIONS("hmp", "return-value"); + VSH_EXCLUSIVE_OPTIONS("hmp", "qmpwrap"); if (!(dom =3D virshCommandOptDomain(ctl, cmd, NULL))) return false; - while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) - virBufferAsprintf(&buf, "%s ", opt->data); + if (vshCommandOptBool(cmd, "qmpwrap")) { + const char *command =3D NULL; + const char *arguments =3D NULL; - virBufferTrim(&buf, " "); + if ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) + command =3D opt->data; + if ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) + arguments =3D opt->data; + + if (!command || (arguments && vshCommandOptArgv(ctl, cmd, opt))) { + vshError(ctl, "%s", _("--qmpwrap option requires 1 or 2 argume= nts")); + return false; + } + + virBufferAsprintf(&buf, "{\"execute\":\"%s\"", command); + if (arguments) + virBufferAsprintf(&buf, ", \"arguments\":%s", arguments); + virBufferAddLit(&buf, "}"); + } else { + while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) + virBufferAsprintf(&buf, "%s ", opt->data); + + virBufferTrim(&buf, " "); + } monitor_cmd =3D virBufferContentAndReset(&buf); --=20 2.31.1