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

Daniel P. Berrange berrange at redhat.com
Mon Jul 4 10:28:27 UTC 2011


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 8573262..e593642 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -1122,6 +1122,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 cb8f799..893f3e9 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -191,6 +191,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 81b7f8c..4db2b78 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -1042,6 +1042,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 c571adb..380e26a 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -53,6 +53,8 @@ int qemuMonitorJSONSystemReset(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 aa5d1c6..0965a08 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -512,6 +512,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 2d9288f..e53f693 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -50,6 +50,8 @@ int qemuMonitorTextSystemReset(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




More information about the libvir-list mailing list