From nobody Wed May 8 14:00:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1581066497691670.3896444657771; Fri, 7 Feb 2020 01:08:17 -0800 (PST) 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-66-NizpX8iPM3m3Ww17xjPPEw-1; Fri, 07 Feb 2020 04:08:13 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C7D09801A26; Fri, 7 Feb 2020 09:08:07 +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 825691001B09; Fri, 7 Feb 2020 09:08:07 +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 3621C18089CF; Fri, 7 Feb 2020 09:08:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01797iiv027148 for ; Fri, 7 Feb 2020 04:07:44 -0500 Received: by smtp.corp.redhat.com (Postfix) id C1D47261A7; Fri, 7 Feb 2020 09:07:44 +0000 (UTC) Received: from angien.redhat.com (unknown [10.43.2.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id 49E1D2619F for ; Fri, 7 Feb 2020 09:07:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581066496; 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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=I1EODKWKk0e4AKrcH3J3vXvWm8XTx/Fp47JHtSK3FFE=; b=QpAFm8NkJWz+ptQB4C9UOGejm8QJXqiuj2kFg/cTUJ14B9lhgyDcfAQNfEpjwgtIidCihz q1PQk9vEV/rE63b0z6KufWgccqogB5X7WQgRbInT8EXq7UJPwktm9fw1iVnw3PDg+UgXPR kvIg1Bro/erV9J7otmS1U57TMD3EkG4= From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 1/2] docs: virsh: Modernize docs for qemu-monitor-command Date: Fri, 7 Feb 2020 10:07:39 +0100 Message-Id: <1bf1f57ab8d118aac0982a8922dfb1506ebe5435.1581066292.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.84 on 10.5.11.22 X-MC-Unique: NizpX8iPM3m3Ww17xjPPEw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Mention that we actually by default use QMP and clarify the rest of the documentation. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- 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 comm= and +and libvirt will automatically convert it into QMP and convert the result = back. qemu-agent-command --=20 2.24.1 From nobody Wed May 8 14:00:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1581066478613443.64639329851843; Fri, 7 Feb 2020 01:07:58 -0800 (PST) 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-338-qDd6_PH-Pd2Jdt_rh0G9ng-1; Fri, 07 Feb 2020 04:07:54 -0500 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 BB13EDB62; Fri, 7 Feb 2020 09:07:49 +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 1F7D260BEC; Fri, 7 Feb 2020 09:07:49 +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 A59DD18089CD; Fri, 7 Feb 2020 09:07:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 01797jrw027156 for ; Fri, 7 Feb 2020 04:07:45 -0500 Received: by smtp.corp.redhat.com (Postfix) id 932A6261A7; Fri, 7 Feb 2020 09:07:45 +0000 (UTC) Received: from angien.redhat.com (unknown [10.43.2.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1BA4F2619F for ; Fri, 7 Feb 2020 09:07:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581066476; 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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=r3NrF2ixFi/3gaJ/oad4xCWtJfAS1z0Fnc261gGEY8k=; b=WKqxmVqhOyYKKvsujLd4KBIeX1T2Md4XRp22EQ+s/k2lkZNxRELwdQFrbkLRfbhlzhjh+e QpA2ImsxdCbeuBTY/ggRwgAmqymxMPqKFwrwD6/pK1riUxzZkpEbTYFbwHARvB2ZLK2E35 jXgiRkI8kICgWsk7ub9SGoVeU2cAknM= From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 2/2] virsh: Allow extracting 'return' section of QMP command in 'qemu-monitor-command' Date: Fri, 7 Feb 2020 10:07:40 +0100 Message-Id: <4add984acf29546bf7f085054b41123e18bc9dd8.1581066292.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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 X-MC-Unique: qDd6_PH-Pd2Jdt_rh0G9ng-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Eric Blake --- 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] } c= ommand... 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 comm= and 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[= ] =3D { .type =3D VSH_OT_BOOL, .help =3D N_("pretty-print any qemu monitor protocol output") }, + {.name =3D "return-value", + .type =3D VSH_OT_BOOL, + .help =3D N_("extract the value of the 'return' key from the returned= string") + }, {.name =3D "cmd", .type =3D VSH_OT_ARGV, .flags =3D VSH_OFLAG_REQ, @@ -9558,11 +9562,17 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd= *cmd) g_autoptr(virshDomain) dom =3D NULL; g_autofree char *monitor_cmd =3D NULL; g_autofree char *result =3D NULL; + g_autoptr(virJSONValue) resultjson =3D NULL; unsigned int flags =3D 0; const vshCmdOpt *opt =3D NULL; virBuffer buf =3D VIR_BUFFER_INITIALIZER; + bool pretty =3D vshCommandOptBool(cmd, "pretty"); + bool returnval =3D vshCommandOptBool(cmd, "return-value"); + virJSONValuePtr formatjson; + g_autofree char *jsonstr =3D NULL; VSH_EXCLUSIVE_OPTIONS("hmp", "pretty"); + VSH_EXCLUSIVE_OPTIONS("hmp", "return-value"); if (!(dom =3D 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 =3D virJSONStringReformat(result, true))) { - VIR_FREE(result); - result =3D tmp; - virTrimSpaces(result, NULL); - } else { - vshResetLibvirtError(); + if (returnval || pretty) { + resultjson =3D 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 =3D virJSONValueObjectGet(resultjson, "return")))= { + vshError(ctl, "'return' member missing"); + return false; + } + } else { + formatjson =3D resultjson; + } + + jsonstr =3D virJSONValueToString(formatjson, pretty); + virTrimSpaces(jsonstr, NULL); + vshPrint(ctl, "%s", jsonstr); return true; } --=20 2.24.1