[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 7/8] Add monitor API for checking whether KVM is enabled



When attaching to an external QEMU process, it is neccessary
to check if the process is using KVM or not. This can be done
using a monitor command

* src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h,
  src/qemu/qemu_monitor_json.c, src/qemu/qemu_monitor_json.h,
  src/qemu/qemu_monitor_text.c, src/qemu/qemu_monitor_text.h: Add
  API for checking if KVM is enabled
---
 src/qemu/qemu_monitor.c      |   21 +++++++++++++++++++
 src/qemu/qemu_monitor.h      |    2 +
 src/qemu/qemu_monitor_json.c |   46 ++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_json.h |    2 +
 src/qemu/qemu_monitor_text.c |   22 ++++++++++++++++++++
 src/qemu/qemu_monitor_text.h |    2 +
 6 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 1428921..9bf141a 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -1115,6 +1115,27 @@ int qemuMonitorGetCPUInfo(qemuMonitorPtr mon,
     return ret;
 }
 
+
+int qemuMonitorGetVirtType(qemuMonitorPtr mon,
+                           int *virtType)
+{
+    int ret;
+    VIR_DEBUG("mon=%p", mon);
+
+    if (!mon) {
+        qemuReportError(VIR_ERR_INVALID_ARG, "%s",
+                        _("monitor must not be NULL"));
+        return -1;
+    }
+
+    if (mon->json)
+        ret = qemuMonitorJSONGetVirtType(mon, virtType);
+    else
+        ret = qemuMonitorTextGetVirtType(mon, virtType);
+    return ret;
+}
+
+
 int qemuMonitorGetBalloonInfo(qemuMonitorPtr mon,
                               unsigned long *currmem)
 {
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 3bb0269..33bace7 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -198,6 +198,8 @@ int qemuMonitorSystemPowerdown(qemuMonitorPtr mon);
 
 int qemuMonitorGetCPUInfo(qemuMonitorPtr mon,
                           int **pids);
+int qemuMonitorGetVirtType(qemuMonitorPtr mon,
+                           int *virtType);
 int qemuMonitorGetBalloonInfo(qemuMonitorPtr mon,
                               unsigned long *currmem);
 int qemuMonitorGetMemoryStats(qemuMonitorPtr mon,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 56ec65b..b8ec903 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -1054,6 +1054,52 @@ int qemuMonitorJSONGetCPUInfo(qemuMonitorPtr mon,
 }
 
 
+int qemuMonitorJSONGetVirtType(qemuMonitorPtr mon,
+                               int *virtType)
+{
+    int ret;
+    virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("query-kvm",
+                                                     NULL);
+    virJSONValuePtr reply = NULL;
+
+    *virtType = VIR_DOMAIN_VIRT_QEMU;
+
+    if (!cmd)
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
+
+    if (ret == 0)
+        ret = qemuMonitorJSONCheckError(cmd, reply);
+
+    if (ret == 0) {
+        virJSONValuePtr data;
+        bool val = false;
+
+        if (!(data = virJSONValueObjectGet(reply, "return"))) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                            _("info kvm reply was missing return data"));
+            ret = -1;
+            goto cleanup;
+        }
+
+        if (virJSONValueObjectGetBoolean(data, "enabled", &val) < 0) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                            _("info kvm reply missing 'running' field"));
+            ret = -1;
+            goto cleanup;
+        }
+        if (val)
+            *virtType = VIR_DOMAIN_VIRT_KVM;
+    }
+
+cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
+
+
 /*
  * Returns: 0 if balloon not supported, +1 if balloon query worked
  * or -1 on failure
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index 393d8fc..1ba7395 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -52,6 +52,8 @@ int qemuMonitorJSONSystemPowerdown(qemuMonitorPtr mon);
 
 int qemuMonitorJSONGetCPUInfo(qemuMonitorPtr mon,
                               int **pids);
+int qemuMonitorJSONGetVirtType(qemuMonitorPtr mon,
+                               int *virtType);
 int qemuMonitorJSONGetBalloonInfo(qemuMonitorPtr mon,
                                   unsigned long *currmem);
 int qemuMonitorJSONGetMemoryStats(qemuMonitorPtr mon,
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index a16ea91..961efd4 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -499,6 +499,28 @@ error:
     return 0;
 }
 
+
+int qemuMonitorTextGetVirtType(qemuMonitorPtr mon,
+                               int *virtType)
+{
+    char *reply = NULL;
+
+    *virtType = VIR_DOMAIN_VIRT_QEMU;
+
+    if (qemuMonitorHMPCommand(mon, "info kvm", &reply) < 0) {
+        qemuReportError(VIR_ERR_OPERATION_FAILED,
+                        "%s", _("could not query kvm status"));
+        return -1;
+    }
+
+    if (strstr(reply, "enabled"))
+        *virtType = VIR_DOMAIN_VIRT_KVM;
+
+    VIR_FREE(reply);
+    return 0;
+}
+
+
 static int parseMemoryStat(char **text, unsigned int tag,
                            const char *search, virDomainMemoryStatPtr stat)
 {
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index 4fa5064..5e137a8 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -49,6 +49,8 @@ int qemuMonitorTextSystemPowerdown(qemuMonitorPtr mon);
 
 int qemuMonitorTextGetCPUInfo(qemuMonitorPtr mon,
                               int **pids);
+int qemuMonitorTextGetVirtType(qemuMonitorPtr mon,
+                               int *virtType);
 int qemuMonitorTextGetBalloonInfo(qemuMonitorPtr mon,
                                   unsigned long *currmem);
 int qemuMonitorTextGetMemoryStats(qemuMonitorPtr mon,
-- 
1.7.4.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]