[PATCH] qemu: include libvirt & QEMU versions in QEMU log files

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

 



It is often helpful to know which version of libvirt and QEMU
was present when a guest was first launched. Ensure this info
is written into the QEMU log file for each guest.
---
 src/qemu/qemu_capabilities.c | 22 +++++++++++++++++-
 src/qemu/qemu_capabilities.h |  1 +
 src/qemu/qemu_command.c      |  2 +-
 src/qemu/qemu_process.c      | 54 ++++++++++++++++++++++++++++++++------------
 src/util/virlog.c            | 17 ++------------
 src/util/virlog.h            | 13 +++++++++++
 6 files changed, 77 insertions(+), 32 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 13f3cd3..a04095e 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -299,6 +299,7 @@ struct _virQEMUCaps {
 
     unsigned int version;
     unsigned int kvmVersion;
+    char *package;
 
     virArch arch;
 
@@ -1941,6 +1942,10 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps)
     ret->usedQMP = qemuCaps->usedQMP;
     ret->version = qemuCaps->version;
     ret->kvmVersion = qemuCaps->kvmVersion;
+
+    if (VIR_STRDUP(ret->package, qemuCaps->package) < 0)
+        goto error;
+
     ret->arch = qemuCaps->arch;
 
     if (VIR_ALLOC_N(ret->cpuDefinitions, qemuCaps->ncpuDefinitions) < 0)
@@ -1992,6 +1997,7 @@ void virQEMUCapsDispose(void *obj)
 
     virBitmapFree(qemuCaps->flags);
 
+    VIR_FREE(qemuCaps->package);
     VIR_FREE(qemuCaps->binary);
 }
 
@@ -2116,6 +2122,12 @@ unsigned int virQEMUCapsGetKVMVersion(virQEMUCapsPtr qemuCaps)
 }
 
 
+const char *virQEMUCapsGetPackage(virQEMUCapsPtr qemuCaps)
+{
+    return qemuCaps->package;
+}
+
+
 int virQEMUCapsAddCPUDefinition(virQEMUCapsPtr qemuCaps,
                                 const char *name)
 {
@@ -2672,6 +2684,9 @@ virQEMUCapsLoadCache(virQEMUCapsPtr qemuCaps, const char *filename,
         goto cleanup;
     }
 
+    /* Don't check for NULL, since it is optional and thus may be missing */
+    qemuCaps->package = virXPathString("string(./package)", ctxt);
+
     if (!(str = virXPathString("string(./arch)", ctxt))) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("missing arch in QEMU capabilities cache"));
@@ -2783,6 +2798,10 @@ virQEMUCapsSaveCache(virQEMUCapsPtr qemuCaps, const char *filename)
     virBufferAsprintf(&buf, "<kvmVersion>%d</kvmVersion>\n",
                       qemuCaps->kvmVersion);
 
+    if (qemuCaps->package)
+        virBufferAsprintf(&buf, "<package>%s</package>\n",
+                          qemuCaps->package);
+
     virBufferAsprintf(&buf, "<arch>%s</arch>\n",
                       virArchToString(qemuCaps->arch));
 
@@ -2872,6 +2891,7 @@ virQEMUCapsReset(virQEMUCapsPtr qemuCaps)
 
     virBitmapClearAll(qemuCaps->flags);
     qemuCaps->version = qemuCaps->kvmVersion = 0;
+    VIR_FREE(qemuCaps->package);
     qemuCaps->arch = VIR_ARCH_NONE;
     qemuCaps->usedQMP = false;
 
@@ -3204,6 +3224,7 @@ virQEMUCapsInitQMPMonitor(virQEMUCapsPtr qemuCaps,
     }
 
     qemuCaps->version = major * 1000000 + minor * 1000 + micro;
+    qemuCaps->package = package;
     qemuCaps->usedQMP = true;
 
     virQEMUCapsInitQMPBasic(qemuCaps);
@@ -3249,7 +3270,6 @@ virQEMUCapsInitQMPMonitor(virQEMUCapsPtr qemuCaps,
 
     ret = 0;
  cleanup:
-    VIR_FREE(package);
     return ret;
 }
 
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 12e1688..1c1227a 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -266,6 +266,7 @@ char *virQEMUCapsFlagsString(virQEMUCapsPtr qemuCaps);
 const char *virQEMUCapsGetBinary(virQEMUCapsPtr qemuCaps);
 virArch virQEMUCapsGetArch(virQEMUCapsPtr qemuCaps);
 unsigned int virQEMUCapsGetVersion(virQEMUCapsPtr qemuCaps);
+const char *virQEMUCapsGetPackage(virQEMUCapsPtr qemuCaps);
 unsigned int virQEMUCapsGetKVMVersion(virQEMUCapsPtr qemuCaps);
 int virQEMUCapsAddCPUDefinition(virQEMUCapsPtr qemuCaps,
                                 const char *name);
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 100deed..6cc7a24 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -7988,7 +7988,7 @@ qemuBuildCommandLine(virConnectPtr conn,
     if (def->mem.nhugepages && (!def->cpu || !def->cpu->ncells)) {
         const long system_page_size = sysconf(_SC_PAGESIZE) / 1024;
         char *mem_path = NULL;
-
+        fprintf(stderr, "SYSCONF %ld\n", system_page_size);
         if (def->mem.hugepages[0].size == system_page_size) {
             /* There is one special case: if user specified "huge"
              * pages of regular system pages size. */
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index b0f7b1c..3d5d58e 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -76,7 +76,6 @@
 
 VIR_LOG_INIT("qemu.qemu_process");
 
-#define START_POSTFIX ": starting up\n"
 #define ATTACH_POSTFIX ": attaching\n"
 #define SHUTDOWN_POSTFIX ": shutting down\n"
 
@@ -4249,6 +4248,44 @@ qemuPrepareNVRAM(virQEMUDriverConfigPtr cfg,
 }
 
 
+static void qemuLogOperation(virDomainObjPtr vm,
+                             const char *msg,
+                             int logfd,
+                             virCommandPtr cmd)
+{
+    char *timestamp;
+    char *logline;
+    qemuDomainObjPrivatePtr priv = vm->privateData;
+    int qemuVersion = virQEMUCapsGetVersion(priv->qemuCaps);
+    const char *package = virQEMUCapsGetPackage(priv->qemuCaps);
+    char ebuf[1024];
+
+    if ((timestamp = virTimeStringNow()) == NULL)
+        goto error;
+
+    if (virAsprintf(&logline, "%s: %s %s, qemu version: %d.%d.%d%s\n",
+                    timestamp, msg, VIR_LOG_VERSION_STRING,
+                    (qemuVersion / 1000000) % 1000, (qemuVersion / 1000) % 1000, qemuVersion % 1000,
+                    package ? package : "") < 0)
+        goto error;
+
+    if (safewrite(logfd, logline, strlen(logline)) < 0)
+        goto error;
+
+    if (cmd)
+        virCommandWriteArgLog(cmd, logfd);
+
+ cleanup:
+    VIR_FREE(timestamp);
+    VIR_FREE(logline);
+    return;
+
+ error:
+    VIR_WARN("Unable to write banner to logfile: %s",
+             virStrerror(errno, ebuf, sizeof(ebuf)));
+    goto cleanup;
+}
+
 int qemuProcessStart(virConnectPtr conn,
                      virQEMUDriverPtr driver,
                      virDomainObjPtr vm,
@@ -4264,7 +4301,6 @@ int qemuProcessStart(virConnectPtr conn,
     off_t pos = -1;
     char ebuf[1024];
     int logfile = -1;
-    char *timestamp;
     qemuDomainObjPrivatePtr priv = vm->privateData;
     virCommandPtr cmd = NULL;
     struct qemuProcessHookData hookData;
@@ -4620,19 +4656,7 @@ int qemuProcessStart(virConnectPtr conn,
             goto cleanup;
     }
 
-    if ((timestamp = virTimeStringNow()) == NULL) {
-        goto cleanup;
-    } else {
-        if (safewrite(logfile, timestamp, strlen(timestamp)) < 0 ||
-            safewrite(logfile, START_POSTFIX, strlen(START_POSTFIX)) < 0) {
-            VIR_WARN("Unable to write timestamp to logfile: %s",
-                     virStrerror(errno, ebuf, sizeof(ebuf)));
-        }
-
-        VIR_FREE(timestamp);
-    }
-
-    virCommandWriteArgLog(cmd, logfile);
+    qemuLogOperation(vm, "starting up", logfile, cmd);
 
     qemuDomainObjCheckTaint(driver, vm, logfile);
 
diff --git a/src/util/virlog.c b/src/util/virlog.c
index 14eb129..b45ee91 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -448,21 +448,8 @@ static int
 virLogVersionString(const char **rawmsg,
                     char **msg)
 {
-#ifdef PACKAGER_VERSION
-# ifdef PACKAGER
-#  define LOG_VERSION_STRING \
-    "libvirt version: " VERSION ", package: " PACKAGER_VERSION " (" PACKAGER ")"
-# else
-#  define LOG_VERSION_STRING \
-    "libvirt version: " VERSION ", package: " PACKAGER_VERSION
-# endif
-#else
-# define LOG_VERSION_STRING  \
-    "libvirt version: " VERSION
-#endif
-
-    *rawmsg = LOG_VERSION_STRING;
-    return virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, LOG_VERSION_STRING);
+    *rawmsg = VIR_LOG_VERSION_STRING;
+    return virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, VIR_LOG_VERSION_STRING);
 }
 
 
diff --git a/src/util/virlog.h b/src/util/virlog.h
index 5b38891..443b3cd 100644
--- a/src/util/virlog.h
+++ b/src/util/virlog.h
@@ -25,6 +25,19 @@
 # include "internal.h"
 # include "virbuffer.h"
 
+# ifdef PACKAGER_VERSION
+#  ifdef PACKAGER
+#   define VIR_LOG_VERSION_STRING \
+     "libvirt version: " VERSION ", package: " PACKAGER_VERSION " (" PACKAGER ")"
+#  else
+#   define VIR_LOG_VERSION_STRING \
+     "libvirt version: " VERSION ", package: " PACKAGER_VERSION
+#  endif
+# else
+#  define VIR_LOG_VERSION_STRING  \
+    "libvirt version: " VERSION
+# endif
+
 /*
  * To be made public
  */
-- 
2.1.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]