[libvirt] [PATCH 3/5] qemu: domain: Implement helper for one-shot log entries to the VM log file

Peter Krempa pkrempa at redhat.com
Tue Jun 7 15:04:28 UTC 2016


Along with the virtlogd addition of the log file appending API implement
a helper for logging one-shot entries to the log file including the
fallback approach of using direct file access.

This will be used for noting the shutdown of the qemu proces and
possibly other actions such as VM migration and other critical VM
lifecycle events.
---
 src/qemu/qemu_domain.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_domain.h |  5 ++++
 2 files changed, 67 insertions(+)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 7e64545..b14afe0 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -3506,6 +3506,68 @@ ssize_t qemuDomainLogContextRead(qemuDomainLogContextPtr ctxt,
 }


+/**
+ * qemuDomainLogAppendMessage:
+ *
+ * This is a best-effort attempt to add a log message to the qemu log file
+ * either by using virtlogd or the legacy approach */
+int
+qemuDomainLogAppendMessage(virQEMUDriverPtr driver,
+                           virDomainObjPtr vm,
+                           const char *fmt,
+                           ...)
+{
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+    virLogManagerPtr manager = NULL;
+    va_list ap;
+    char *path = NULL;
+    int writefd = -1;
+    char *message = NULL;
+    int ret = -1;
+
+    va_start(ap, fmt);
+
+    if (virVasprintf(&message, fmt, ap) < 0)
+        goto cleanup;
+
+    VIR_DEBUG("Append log message (vm='%s' message='%s) stdioLogD=%d",
+              vm->def->name, message, cfg->stdioLogD);
+
+    if (virAsprintf(&path, "%s/%s.log", cfg->logDir, vm->def->name) < 0)
+        goto cleanup;
+
+    if (cfg->stdioLogD) {
+        if (!(manager = virLogManagerNew(virQEMUDriverIsPrivileged(driver))))
+            goto cleanup;
+
+        if (virLogManagerDomainAppendMessage(manager, "qemu", vm->def->uuid,
+                                             vm->def->name, path, message, 0) < 0)
+            goto cleanup;
+    } else {
+        if ((writefd = open(path, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)) < 0) {
+            virReportSystemError(errno, _("failed to create logfile %s"),
+                                 path);
+            goto cleanup;
+        }
+
+        if (safewrite(writefd, message, strlen(message)) < 0)
+            goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    va_end(ap);
+    VIR_FREE(message);
+    VIR_FORCE_CLOSE(writefd);
+    virLogManagerFree(manager);
+    virObjectUnref(cfg);
+    VIR_FREE(path);
+
+    return ret;
+}
+
+
 int qemuDomainLogContextGetWriteFD(qemuDomainLogContextPtr ctxt)
 {
     return ctxt->writefd;
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index a9a7295..25381b1 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -480,6 +480,11 @@ void qemuDomainLogContextFree(qemuDomainLogContextPtr ctxt);

 virLogManagerPtr qemuDomainLogContextGetManager(qemuDomainLogContextPtr ctxt);

+int qemuDomainLogAppendMessage(virQEMUDriverPtr driver,
+                               virDomainObjPtr vm,
+                               const char *fmt,
+                               ...) ATTRIBUTE_FMT_PRINTF(3, 4);
+
 const char *qemuFindQemuImgBinary(virQEMUDriverPtr driver);

 int qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
-- 
2.8.3




More information about the libvir-list mailing list