[libvirt] [PATCH v4 5/7] Add a watchdog action `dump'

Hu Tao hutao at cn.fujitsu.com
Thu Dec 2 01:05:36 UTC 2010


`dump' watchdog action lets libvirtd to dump the guest when receives a
watchdog event (which probably means a guest crash)

Currently only qemu is supported.
---
 cfg.mk                 |    3 +-
 src/Makefile.am        |    2 +-
 src/conf/domain_conf.c |    1 +
 src/conf/domain_conf.h |    1 +
 src/qemu/qemu.conf     |    5 ++
 src/qemu/qemu_conf.c   |   16 +++++++-
 src/qemu/qemu_conf.h   |    5 ++
 src/qemu/qemu_driver.c |   99 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/threadpool.c  |    3 +
 9 files changed, 132 insertions(+), 3 deletions(-)

diff --git a/cfg.mk b/cfg.mk
index 5576ecb..2eb0f77 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -129,7 +129,8 @@ useless_free_options =				\
   --name=virStorageVolDefFree			\
   --name=xmlFree				\
   --name=xmlXPathFreeContext			\
-  --name=xmlXPathFreeObject
+  --name=xmlXPathFreeObject			\
+  --name=virWorkerPoolFree
 
 # The following template was generated by this command:
 # make ID && aid free|grep '^vi'|sed 's/ .*//;s/^/#   /'
diff --git a/src/Makefile.am b/src/Makefile.am
index 5febd76..83ccd7c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -646,7 +646,7 @@ endif
 libvirt_driver_qemu_la_CFLAGS = $(NUMACTL_CFLAGS) \
 		-I at top_srcdir@/src/conf $(AM_CFLAGS)
 libvirt_driver_qemu_la_LDFLAGS = $(AM_LDFLAGS)
-libvirt_driver_qemu_la_LIBADD = $(NUMACTL_LIBS)
+libvirt_driver_qemu_la_LIBADD = $(NUMACTL_LIBS) ../src/libvirt_util.la
 if WITH_DRIVER_MODULES
 libvirt_driver_qemu_la_LIBADD += ../gnulib/lib/libgnu.la
 libvirt_driver_qemu_la_LDFLAGS += -module -avoid-version
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 3f14cee..a6cb444 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -245,6 +245,7 @@ VIR_ENUM_IMPL(virDomainWatchdogAction, VIR_DOMAIN_WATCHDOG_ACTION_LAST,
               "shutdown",
               "poweroff",
               "pause",
+              "dump",
               "none")
 
 VIR_ENUM_IMPL(virDomainVideo, VIR_DOMAIN_VIDEO_TYPE_LAST,
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 899b19f..7f50b79 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -462,6 +462,7 @@ enum virDomainWatchdogAction {
     VIR_DOMAIN_WATCHDOG_ACTION_SHUTDOWN,
     VIR_DOMAIN_WATCHDOG_ACTION_POWEROFF,
     VIR_DOMAIN_WATCHDOG_ACTION_PAUSE,
+    VIR_DOMAIN_WATCHDOG_ACTION_DUMP,
     VIR_DOMAIN_WATCHDOG_ACTION_NONE,
 
     VIR_DOMAIN_WATCHDOG_ACTION_LAST
diff --git a/src/qemu/qemu.conf b/src/qemu/qemu.conf
index f4f965e..ba41f80 100644
--- a/src/qemu/qemu.conf
+++ b/src/qemu/qemu.conf
@@ -191,6 +191,11 @@
 # save_image_format = "raw"
 # dump_image_format = "raw"
 
+# When a domain is configured to be auto-dumped when libvirtd receives a
+# watchdog event from qemu guest, libvirtd will save dump files in directory
+# specified by auto_dump_path. Default value is /var/lib/libvirt/qemu/dump
+#
+# auto_dump_path = "/var/lib/libvirt/qemu/dump"
 
 # If provided by the host and a hugetlbfs mount point is configured,
 # a guest may request huge page backing.  When this mount point is
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index b0343c6..6296378 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -386,6 +386,17 @@ int qemudLoadDriverConfig(struct qemud_driver *driver,
         }
     }
 
+    p = virConfGetValue (conf, "auto_dump_path");
+    CHECK_TYPE ("auto_dump_path", VIR_CONF_STRING);
+    if (p && p->str) {
+        VIR_FREE(driver->autoDumpPath);
+        if (!(driver->autoDumpPath = strdup(p->str))) {
+            virReportOOMError();
+            virConfFree(conf);
+            return -1;
+        }
+    }
+
      p = virConfGetValue (conf, "hugetlbfs_mount");
      CHECK_TYPE ("hugetlbfs_mount", VIR_CONF_STRING);
      if (p && p->str) {
@@ -5373,7 +5384,10 @@ int qemudBuildCommandLine(virConnectPtr conn,
         }
         ADD_ARG(optstr);
 
-        const char *action = virDomainWatchdogActionTypeToString(watchdog->action);
+        int act = watchdog->action;
+        if (act == VIR_DOMAIN_WATCHDOG_ACTION_DUMP)
+            act = VIR_DOMAIN_WATCHDOG_ACTION_PAUSE;
+        const char *action = virDomainWatchdogActionTypeToString(act);
         if (!action) {
             qemuReportError(VIR_ERR_INTERNAL_ERROR,
                             "%s", _("invalid watchdog action"));
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index aba64d6..0956e7b 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -41,6 +41,7 @@
 # include "driver.h"
 # include "bitmap.h"
 # include "macvtap.h"
+# include "threadpool.h"
 
 # define qemudDebug(fmt, ...) do {} while(0)
 
@@ -107,6 +108,8 @@ enum qemud_cmd_flags {
 struct qemud_driver {
     virMutex lock;
 
+    virWorkerPoolPtr workerPool;
+
     int privileged;
 
     uid_t user;
@@ -174,6 +177,8 @@ struct qemud_driver {
     char *saveImageFormat;
     char *dumpImageFormat;
 
+    char *autoDumpPath;
+
     pciDeviceList *activePciHostdevs;
 
     virBitmapPtr reservedVNCPorts;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index cc6891e..d99733c 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -85,6 +85,7 @@
 #include "files.h"
 #include "fdstream.h"
 #include "configmake.h"
+#include "threadpool.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
@@ -137,6 +138,16 @@ struct _qemuDomainObjPrivate {
     int persistentAddrs;
 };
 
+struct watchdogEvent
+{
+    virDomainObjPtr vm;
+    int action;
+};
+
+static enum qemud_save_formats getCompressionType(struct qemud_driver *driver);
+static int doCoreDump(struct qemud_driver *driver, virDomainObjPtr vm, const char *path, enum qemud_save_formats compress);
+static void processWatchdogEvent(void *data);
+
 static int qemudShutdown(void);
 
 static void qemuDriverLock(struct qemud_driver *driver)
@@ -1204,6 +1215,17 @@ qemuHandleDomainWatchdog(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
         if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
             VIR_WARN("Unable to save status on vm %s after IO error", vm->def->name);
     }
+
+    if (vm->def->watchdog->action == VIR_DOMAIN_WATCHDOG_ACTION_DUMP) {
+        struct watchdogEvent *wdEvent;
+        if (VIR_ALLOC(wdEvent) == 0) {
+            wdEvent->action = VIR_DOMAIN_WATCHDOG_ACTION_DUMP;
+            wdEvent->vm = vm;
+            virWorkerPoolSendJob(driver->workerPool, wdEvent);
+        } else
+            virReportOOMError();
+    }
+
     virDomainObjUnlock(vm);
 
     if (watchdogEvent || lifecycleEvent) {
@@ -1786,6 +1808,9 @@ qemudStartup(int privileged) {
         if (virAsprintf(&qemu_driver->snapshotDir,
                         "%s/lib/libvirt/qemu/snapshot", LOCALSTATEDIR) == -1)
             goto out_of_memory;
+        if (virAsprintf(&qemu_driver->autoDumpPath,
+                        "%s/lib/libvirt/qemu/dump", LOCALSTATEDIR) == -1)
+            goto out_of_memory;
     } else {
         uid_t uid = geteuid();
         char *userdir = virGetUserDirectory(uid);
@@ -1814,6 +1839,8 @@ qemudStartup(int privileged) {
             goto out_of_memory;
         if (virAsprintf(&qemu_driver->snapshotDir, "%s/qemu/snapshot", base) == -1)
             goto out_of_memory;
+        if (virAsprintf(&qemu_driver->autoDumpPath, "%s/qemu/dump", base) == -1)
+            goto out_of_memory;
     }
 
     if (virFileMakePath(qemu_driver->stateDir) != 0) {
@@ -1846,6 +1873,12 @@ qemudStartup(int privileged) {
                   qemu_driver->snapshotDir, virStrerror(errno, ebuf, sizeof ebuf));
         goto error;
     }
+    if (virFileMakePath(qemu_driver->autoDumpPath) != 0) {
+        char ebuf[1024];
+        VIR_ERROR(_("Failed to create dump dir '%s': %s"),
+                  qemu_driver->autoDumpPath, virStrerror(errno, ebuf, sizeof ebuf));
+        goto error;
+    }
 
     /* Configuration paths are either ~/.libvirt/qemu/... (session) or
      * /etc/libvirt/qemu/... (system).
@@ -1971,6 +2004,10 @@ qemudStartup(int privileged) {
 
     qemudAutostartConfigs(qemu_driver);
 
+    qemu_driver->workerPool = virWorkerPoolNew(0, 1, processWatchdogEvent);
+    if (!qemu_driver->workerPool)
+        goto error;
+
     if (conn)
         virConnectClose(conn);
 
@@ -2078,6 +2115,7 @@ qemudShutdown(void) {
     VIR_FREE(qemu_driver->cacheDir);
     VIR_FREE(qemu_driver->saveDir);
     VIR_FREE(qemu_driver->snapshotDir);
+    VIR_FREE(qemu_driver->autoDumpPath);
     VIR_FREE(qemu_driver->vncTLSx509certdir);
     VIR_FREE(qemu_driver->vncListen);
     VIR_FREE(qemu_driver->vncPassword);
@@ -2106,6 +2144,7 @@ qemudShutdown(void) {
 
     qemuDriverUnlock(qemu_driver);
     virMutexDestroy(&qemu_driver->lock);
+    virWorkerPoolFree(qemu_driver->workerPool);
     VIR_FREE(qemu_driver);
 
     return 0;
@@ -4432,6 +4471,8 @@ retry:
     }
 }
 
+
+
 static virDrvOpenStatus qemudOpen(virConnectPtr conn,
                                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                   int flags ATTRIBUTE_UNUSED) {
@@ -6253,6 +6294,64 @@ cleanup:
     return ret;
 }
 
+static void processWatchdogEvent(void *data)
+{
+    int ret;
+    struct watchdogEvent *wdEvent = data;
+
+    switch (wdEvent->action) {
+    case VIR_DOMAIN_WATCHDOG_ACTION_DUMP:
+        {
+            char *dumpfile;
+            int i;
+
+            qemuDomainObjPrivatePtr priv = wdEvent->vm->privateData;
+
+            i = virAsprintf(&dumpfile, "%s/%s-%u",
+                            qemu_driver->autoDumpPath,
+                            wdEvent->vm->def->name,
+                            (unsigned int)time(NULL));
+
+            qemuDriverLock(qemu_driver);
+            virDomainObjLock(wdEvent->vm);
+
+            if (qemuDomainObjBeginJobWithDriver(qemu_driver, wdEvent->vm) < 0)
+                break;
+
+            if (!virDomainObjIsActive(wdEvent->vm)) {
+                qemuReportError(VIR_ERR_OPERATION_INVALID,
+                                "%s", _("domain is not running"));
+                break;
+            }
+
+            ret = doCoreDump(qemu_driver,
+                             wdEvent->vm,
+                             dumpfile,
+                             getCompressionType(qemu_driver));
+            if (ret < 0)
+                qemuReportError(VIR_ERR_OPERATION_FAILED,
+                                "%s", _("Dump failed"));
+
+            qemuDomainObjEnterMonitorWithDriver(qemu_driver, wdEvent->vm);
+            ret = qemuMonitorStartCPUs(priv->mon, NULL);
+            qemuDomainObjExitMonitorWithDriver(qemu_driver, wdEvent->vm);
+
+            if (ret < 0)
+                qemuReportError(VIR_ERR_OPERATION_FAILED,
+                                "%s", _("Resuming after dump failed"));
+
+            qemuDomainObjEndJob(wdEvent->vm);
+
+            virDomainObjUnlock(wdEvent->vm);
+            qemuDriverUnlock(qemu_driver);
+
+            VIR_FREE(dumpfile);
+        }
+        break;
+    }
+
+    VIR_FREE(wdEvent);
+}
 
 static int qemudDomainHotplugVcpus(virDomainObjPtr vm, unsigned int nvcpus)
 {
diff --git a/src/util/threadpool.c b/src/util/threadpool.c
index 79f9fbb..b5034b3 100644
--- a/src/util/threadpool.c
+++ b/src/util/threadpool.c
@@ -118,6 +118,9 @@ error:
 
 void virWorkerPoolFree(virWorkerPoolPtr pool)
 {
+    if (!pool)
+        return;
+
     virMutexLock(&pool->mutex);
     pool->quit = true;
     if (pool->nWorker > 0) {
-- 
1.7.3


-- 
Thanks,
Hu Tao




More information about the libvir-list mailing list