[libvirt] [PATCH 7/9] virsh: domstate: report detailed state if available

Bjoern Walk bwalk at linux.ibm.com
Wed Jul 11 10:49:20 UTC 2018


Add a new parameter to virsh domstate, --info, to additionally report
additional information for the domain state:

    virsh # domstate --info guest-1
    crashed (panicked: disabled-wait core='1' psw-mask='0x000000000010f146' \
             psw-addr='0x0002000180000000')

When the new API virDomainGetStateParams is not available for the server
or not supported by the hypervisor driver, fall back to the old API
using virDomainGetState function.

The --info parameter implies the --reason parameter and if additional
information is not available, the output is the same.

Reviewed-by: Boris Fiuczynski <fiuczy at linux.ibm.com>
Reviewed-by: Stefan Zimmermann <stzi at linux.ibm.com>
Signed-off-by: Bjoern Walk <bwalk at linux.ibm.com>
---
 tools/virsh-domain-monitor.c | 31 ++++++++++++++++++++++++++-----
 tools/virsh.pod              |  5 +++--
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 87660ee6..ed732126 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1372,6 +1372,10 @@ static const vshCmdOptDef opts_domstate[] = {
      .type = VSH_OT_BOOL,
      .help = N_("also print reason for the state")
     },
+    {.name = "info",
+     .type = VSH_OT_BOOL,
+     .help = N_("also print reason and information for the state")
+    },
     {.name = NULL}
 };
 
@@ -1381,26 +1385,43 @@ cmdDomstate(vshControl *ctl, const vshCmd *cmd)
     virDomainPtr dom;
     bool ret = true;
     bool showReason = vshCommandOptBool(cmd, "reason");
+    bool showInfo = vshCommandOptBool(cmd, "info");
+    virTypedParameterPtr params = NULL;
+    int nparams = 0;
     int state, reason;
+    const char *info = NULL;
 
     if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
         return false;
 
-    if ((state = virshDomainState(ctl, dom, &reason)) < 0) {
-        ret = false;
-        goto cleanup;
+    if (virDomainGetStateParams(dom, &params, &nparams, 0) < 0 ||
+        virTypedParamsGetInt(params, nparams, VIR_DOMAIN_STATE_PARAMS_STATE,
+                             &state) < 0 ||
+        virTypedParamsGetInt(params, nparams, VIR_DOMAIN_STATE_PARAMS_REASON,
+                             &reason) < 0 ||
+        virTypedParamsGetString(params, nparams, VIR_DOMAIN_STATE_PARAMS_INFO,
+                                &info) < 0) {
+        if ((state = virshDomainState(ctl, dom, &reason)) < 0) {
+            ret = false;
+            goto cleanup;
+        }
     }
 
-    if (showReason) {
-        vshPrint(ctl, "%s (%s)\n",
+    if (showInfo || showReason) {
+        vshPrint(ctl, "%s (%s",
                  virshDomainStateToString(state),
                  virshDomainStateReasonToString(state, reason));
+        if (showInfo && info && strlen(info) > 0)
+            vshPrint(ctl, ": %s)\n", info);
+        else
+            vshPrint(ctl, ")\n");
     } else {
         vshPrint(ctl, "%s\n",
                  virshDomainStateToString(state));
     }
 
  cleanup:
+    virTypedParamsFree(params, nparams);
     virshDomainFree(dom);
     return ret;
 }
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 4c6e21fc..b30b0b79 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -1519,10 +1519,11 @@ specified in the second argument.
 
 B<Note>: Domain must be inactive and without snapshots.
 
-=item B<domstate> I<domain> [I<--reason>]
+=item B<domstate> I<domain> [I<--reason>] [I<--info>]
 
 Returns state about a domain.  I<--reason> tells virsh to also print
-reason for the state.
+reason for the state. I<--info> prints additional state information if
+available, I<--info> implies I<--reason>.
 
 =item B<domcontrol> I<domain>
 
-- 
2.17.0




More information about the libvir-list mailing list