[libvirt] [PATCH 6/7] qemu: Implement the driver methods

Michal Privoznik mprivozn at redhat.com
Fri Apr 1 15:29:20 UTC 2011


---
 src/qemu/qemu_driver.c       |  107 +++++++++++++++++++++++++++++++++++++++++-
 src/qemu/qemu_monitor.c      |   20 ++++++++
 src/qemu/qemu_monitor.h      |    3 +
 src/qemu/qemu_monitor_json.c |   23 +++++++++
 src/qemu/qemu_monitor_json.h |    4 ++
 src/qemu/qemu_monitor_text.c |   31 ++++++++++++
 src/qemu/qemu_monitor_text.h |    2 +
 7 files changed, 189 insertions(+), 1 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index c9cc74a..e25c7a6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -2383,6 +2383,111 @@ cleanup:
     return ret;
 }
 
+static int
+qemuDomainScreenshot(virDomainPtr dom, const char *path,
+                     int flags ATTRIBUTE_UNUSED)
+{
+    struct qemud_driver *driver = dom->conn->privateData;
+    virDomainObjPtr vm;
+    qemuDomainObjPrivatePtr priv;
+    char *tmp = NULL;
+    int path_fd = -1, tmp_fd = -1, ret = -1;
+    char buf[4096];
+    ssize_t nread, nwrite;
+
+    qemuDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+    qemuDriverUnlock(driver);
+
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        qemuReportError(VIR_ERR_NO_DOMAIN,
+                        _("no domain matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    priv = vm->privateData;
+
+    if (qemuDomainObjBeginJob(vm) < 0)
+        goto cleanup;
+
+    if (!virDomainObjIsActive(vm)) {
+        qemuReportError(VIR_ERR_OPERATION_INVALID,
+                        "%s", _("domain is not running"));
+        goto endjob;
+    }
+
+    if ((path_fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
+        qemuReportError(VIR_ERR_OPERATION_FAILED,
+                        _("failed to create '%s'"), path);
+        goto endjob;
+    }
+
+    if (virAsprintf(&tmp, "%s/qemu.screendump.XXXXXX", driver->cacheDir) < 0) {
+        virReportOOMError();
+        goto endjob;
+    }
+
+    /* Create a temporary filename */
+    if ((tmp_fd = mkstemp(tmp)) == -1) {
+        virReportSystemError(errno, _("mkstemp(\"%s\") failed"), tmp);
+        goto endjob;
+    }
+
+    qemuDomainObjEnterMonitor(vm);
+    if (qemuMonitorScreendump(priv->mon, tmp) < 0) {
+        qemuDomainObjExitMonitor(vm);
+        goto endjob;
+    }
+    qemuDomainObjExitMonitor(vm);
+
+    /* Copy to destination */
+    while ((nread=saferead(tmp_fd, buf, sizeof(buf))) > 0) {
+        nwrite = safewrite(path_fd, buf, nread);
+        if (nread != nwrite) {
+            virReportSystemError(errno, _("failed to write data to '%s'"),
+                                 path);
+            goto endjob;
+        }
+    }
+    if (nread < 0) {
+        virReportSystemError(errno, _("failed to read data from '%s'"),
+                             tmp);
+    }
+
+    if (VIR_CLOSE(tmp_fd) < 0) {
+        virReportSystemError(errno, _("error closing '%s'"), tmp);
+        goto endjob;
+    }
+    tmp_fd = -1;
+
+    if (VIR_CLOSE(path_fd) < 0) {
+        virReportSystemError(errno, _("error closing '%s'"), path);
+        goto endjob;
+    }
+    path_fd = -1;
+
+    ret = 0;
+
+endjob:
+    VIR_FORCE_CLOSE(tmp_fd);
+    if (tmp != NULL) {
+        unlink(tmp);
+        VIR_FREE(tmp);
+    }
+
+    VIR_FORCE_CLOSE(path_fd);
+
+    if (qemuDomainObjEndJob(vm) == 0)
+        vm = NULL;
+
+cleanup:
+    if (vm)
+        virDomainObjUnlock(vm);
+    return ret;
+}
+
 static void processWatchdogEvent(void *data, void *opaque)
 {
     int ret;
@@ -6850,7 +6955,7 @@ static virDriver qemuDriver = {
     qemudDomainSave, /* domainSave */
     qemuDomainRestore, /* domainRestore */
     qemudDomainCoreDump, /* domainCoreDump */
-    NULL, /* domainScreenshot */
+    qemuDomainScreenshot, /* domainScreenshot */
     qemudDomainSetVcpus, /* domainSetVcpus */
     qemudDomainSetVcpusFlags, /* domainSetVcpusFlags */
     qemudDomainGetVcpusFlags, /* domainGetVcpusFlags */
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 2d28f8d..6834d29 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -2228,3 +2228,23 @@ int qemuMonitorArbitraryCommand(qemuMonitorPtr mon,
         ret = qemuMonitorTextArbitraryCommand(mon, cmd, reply);
     return ret;
 }
+
+int qemuMonitorScreendump(qemuMonitorPtr mon,
+                          const char *file)
+{
+    int ret;
+
+    VIR_DEBUG("mon=%p, file=%s", mon, file);
+
+    if (!mon) {
+        qemuReportError(VIR_ERR_INVALID_ARG,"%s",
+                        _("monitor must not be NULL"));
+        return -1;
+    }
+
+    if (mon->json)
+        ret = qemuMonitorJSONScreendump(mon, file);
+    else
+        ret = qemuMonitorTextScreendump(mon, file);
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index c90219b..5c96f12 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -423,6 +423,9 @@ int qemuMonitorArbitraryCommand(qemuMonitorPtr mon,
                                 char **reply,
                                 bool hmp);
 
+int qemuMonitorScreendump(qemuMonitorPtr mon,
+                          const char *file);
+
 /**
  * When running two dd process and using <> redirection, we need a
  * shell that will not truncate files.  These two strings serve that
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 20a78e1..df45e4b 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2513,3 +2513,26 @@ cleanup:
 
     return ret;
 }
+
+int qemuMonitorJSONScreendump(qemuMonitorPtr mon,
+                              const char *file)
+{
+    int ret;
+    virJSONValuePtr cmd, reply = NULL;
+
+    cmd = qemuMonitorJSONMakeCommand("screendump",
+                                     "s:filename", file,
+                                     NULL);
+
+    if (!cmd)
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
+
+    if (ret == 0)
+        ret = qemuMonitorJSONCheckError(cmd, reply);
+
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index 086f0e1..bcfdd24 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -204,4 +204,8 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon,
                                     char **reply_str,
                                     bool hmp);
 
+int qemuMonitorJSONScreendump(qemuMonitorPtr mon,
+                              const char *file);
+
+
 #endif /* QEMU_MONITOR_JSON_H */
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index 168c60f..52c82d0 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -2628,3 +2628,34 @@ int qemuMonitorTextArbitraryCommand(qemuMonitorPtr mon, const char *cmd,
 
     return ret;
 }
+
+/* Returns -1 on error, -2 if not supported */
+int qemuMonitorTextScreendump(qemuMonitorPtr mon, const char *file)
+{
+    char *cmd = NULL;
+    char *reply = NULL;
+    int ret = -1;
+
+    if (virAsprintf(&cmd, "screendump %s", file) < 0){
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (qemuMonitorHMPCommand(mon, cmd, &reply) < 0) {
+        qemuReportError(VIR_ERR_OPERATION_FAILED,
+                        "%s", _("taking screenshot failed"));
+        goto cleanup;
+    }
+
+    if (strstr(reply, "unknown command:")) {
+        ret = -2;
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(reply);
+    VIR_FREE(cmd);
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index 0838a2b..773d33c 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -198,4 +198,6 @@ int qemuMonitorTextDeleteSnapshot(qemuMonitorPtr mon, const char *name);
 int qemuMonitorTextArbitraryCommand(qemuMonitorPtr mon, const char *cmd,
                                     char **reply);
 
+int qemuMonitorTextScreendump(qemuMonitorPtr mon, const char *file);
+
 #endif /* QEMU_MONITOR_TEXT_H */
-- 
1.7.4




More information about the libvir-list mailing list