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

[libvirt] [PATCH 07/10] Qemu arbitrary monitor commands.



Implement the qemu driver's virDomainQemuMonitorCommand
and hook it into the API entry point.

Signed-off-by: Chris Lalancette <clalance redhat com>
---
 src/libvirt-qemu.c           |   12 +++++++++++-
 src/qemu/qemu_driver.c       |   38 ++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_driver.h       |    3 +++
 src/qemu/qemu_monitor.c      |   13 +++++++++++++
 src/qemu/qemu_monitor.h      |    2 ++
 src/qemu/qemu_monitor_json.c |   28 ++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_json.h |    4 ++++
 src/qemu/qemu_monitor_text.c |   21 +++++++++++++++++++++
 src/qemu/qemu_monitor_text.h |    3 +++
 9 files changed, 123 insertions(+), 1 deletions(-)

diff --git a/src/libvirt-qemu.c b/src/libvirt-qemu.c
index e6a4cc4..a8aeebc 100644
--- a/src/libvirt-qemu.c
+++ b/src/libvirt-qemu.c
@@ -4,6 +4,7 @@
 #include "logging.h"
 #include "datatypes.h"
 #include "libvirt/libvirt-qemu.h"
+#include "qemu/qemu_driver.h"
 
 /**
  * virLibConnError:
@@ -57,6 +58,7 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
                             char **result, unsigned int flags)
 {
     virConnectPtr conn;
+    int ret;
 
     DEBUG("domain=%p, cmd=%s, result=%p, flags=%u", domain, cmd, result, flags);
 
@@ -80,7 +82,15 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
         goto error;
     }
 
-    virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
+    if (STREQ(conn->driver->name, "QEMU"))
+        ret = qemuMonitorCommand(domain, cmd, result, flags);
+    else {
+        virLibConnError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
+        goto error;
+    }
+    if (ret < 0)
+        goto error;
+    return ret;
 
 error:
     virDispatchError(conn);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 0b297a7..973a752 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11418,6 +11418,44 @@ cleanup:
     return ret;
 }
 
+int qemuMonitorCommand(virDomainPtr domain, const char *cmd,
+                       char **result, unsigned int flags ATTRIBUTE_UNUSED)
+{
+    struct qemud_driver *driver = domain->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    int ret = -1;
+    qemuDomainObjPrivatePtr priv;
+
+    VIR_WARN(_("Qemu monitor command '%s' executed; libvirt results may be unpredictable!"), cmd);
+
+    qemuDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(domain->uuid, uuidstr);
+        qemuReportError(VIR_ERR_NO_DOMAIN,
+                        _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if (!virDomainObjIsActive(vm)) {
+        qemuReportError(VIR_ERR_OPERATION_INVALID,
+                        "%s", _("domain is not running"));
+        goto cleanup;
+   }
+
+    priv = vm->privateData;
+    qemuDomainObjEnterMonitorWithDriver(driver, vm);
+    ret = qemuMonitorArbitraryCommand(priv->mon, cmd, result);
+    qemuDomainObjExitMonitorWithDriver(driver, vm);
+
+cleanup:
+    if (vm)
+        virDomainObjUnlock(vm);
+    qemuDriverUnlock(driver);
+    return ret;
+}
+
 static virDriver qemuDriver = {
     VIR_DRV_QEMU,
     "QEMU",
diff --git a/src/qemu/qemu_driver.h b/src/qemu/qemu_driver.h
index 95b8bff..f061cba 100644
--- a/src/qemu/qemu_driver.h
+++ b/src/qemu/qemu_driver.h
@@ -49,4 +49,7 @@
 
 int qemuRegister(void);
 
+int qemuMonitorCommand(virDomainPtr domain, const char *cmd,
+                       char **result, unsigned int flags);
+
 #endif /* QEMUD_DRIVER_H */
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index c4f2725..da10d40 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -1545,3 +1545,16 @@ int qemuMonitorDeleteSnapshot(qemuMonitorPtr mon, const char *name)
         ret = qemuMonitorTextDeleteSnapshot(mon, name);
     return ret;
 }
+
+int qemuMonitorArbitraryCommand(qemuMonitorPtr mon, const char *cmd, char **reply)
+{
+    int ret;
+
+    DEBUG("mon=%p, cmd=%s, reply=%p", mon, cmd, reply);
+
+    if (mon->json)
+        ret = qemuMonitorJSONArbitraryCommand(mon, cmd, reply);
+    else
+        ret = qemuMonitorTextArbitraryCommand(mon, cmd, reply);
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 251233a..5ccbf12 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -351,4 +351,6 @@ int qemuMonitorCreateSnapshot(qemuMonitorPtr mon, const char *name);
 int qemuMonitorLoadSnapshot(qemuMonitorPtr mon, const char *name);
 int qemuMonitorDeleteSnapshot(qemuMonitorPtr mon, const char *name);
 
+int qemuMonitorArbitraryCommand(qemuMonitorPtr mon, const char *cmd, char **reply);
+
 #endif /* QEMU_MONITOR_H */
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index cde9899..d80cb95 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2177,3 +2177,31 @@ int qemuMonitorJSONDeleteSnapshot(qemuMonitorPtr mon, const char *name)
     virJSONValueFree(reply);
     return ret;
 }
+
+int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon,
+                                    const char *cmd_str,
+                                    char **reply_str)
+{
+    virJSONValuePtr cmd = NULL;
+    virJSONValuePtr reply = NULL;
+    int ret = -1;
+
+    cmd = virJSONValueFromString(cmd_str);
+    if (!cmd)
+        return -1;
+
+    if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
+        goto cleanup;
+
+    *reply_str = virJSONValueToString(reply);
+    if (!(*reply_str))
+        goto cleanup;
+
+    ret = 0;
+
+cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index f404c56..8de956d 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -182,4 +182,8 @@ int qemuMonitorJSONCreateSnapshot(qemuMonitorPtr mon, const char *name);
 int qemuMonitorJSONLoadSnapshot(qemuMonitorPtr mon, const char *name);
 int qemuMonitorJSONDeleteSnapshot(qemuMonitorPtr mon, const char *name);
 
+int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon,
+                                    const char *cmd_str,
+                                    char **reply_str);
+
 #endif /* QEMU_MONITOR_JSON_H */
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index 6ad07b1..f4b289d 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -2450,3 +2450,24 @@ cleanup:
     VIR_FREE(reply);
     return ret;
 }
+
+int qemuMonitorTextArbitraryCommand(qemuMonitorPtr mon, const char *cmd,
+                                    char **reply)
+{
+    char *safecmd = NULL;
+    int ret;
+
+    if (!(safecmd = qemuMonitorEscapeArg(cmd))) {
+        virReportOOMError();
+        return -1;
+    }
+
+    ret = qemuMonitorCommand(mon, safecmd, reply);
+    if (ret != 0)
+        qemuReportError(VIR_ERR_OPERATION_FAILED,
+                        _("failed to run cmd '%s'"), safecmd);
+
+    VIR_FREE(safecmd);
+
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index 3200660..4ee78ab 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -181,4 +181,7 @@ int qemuMonitorTextCreateSnapshot(qemuMonitorPtr mon, const char *name);
 int qemuMonitorTextLoadSnapshot(qemuMonitorPtr mon, const char *name);
 int qemuMonitorTextDeleteSnapshot(qemuMonitorPtr mon, const char *name);
 
+int qemuMonitorTextArbitraryCommand(qemuMonitorPtr mon, const char *cmd,
+                                    char **reply);
+
 #endif /* QEMU_MONITOR_TEXT_H */
-- 
1.6.6.1


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