From: Daniel P. Berrange Date: Fri, 17 Jun 2011 14:31:45 +0000 (+0100) Subject: Add monitor API for checking whether KVM is enabled X-Git-Tag: v0.9.4-rc1~235 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7760eaa05063aef5f564c34f5e572a6f86621ea2;p=thirdparty%2Flibvirt.git 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 --- diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 85732621a5..e593642f50 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 cb8f799e07..893f3e9c15 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 81b7f8c8c5..4db2b78ebc 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 c571adb8d3..380e26afa0 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 aa5d1c6eb8..0965a0886a 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 2d9288f50c..e53f69352b 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,