implement auto cold migration fallback at timeout
Signed-off-by: Wen Congyang <wency(a)cn.fujitsu.com>
---
tools/virsh.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/tools/virsh.c b/tools/virsh.c
index 4e37f2d..794b97a 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -54,6 +54,7 @@
#include "files.h"
#include "../daemon/event.h"
#include "configmake.h"
+#include "timer.h"
static char *progname;
@@ -3378,9 +3379,32 @@ static const vshCmdOptDef opts_migrate[] = {
{"desturi", VSH_OT_DATA, VSH_OFLAG_REQ, N_("connection URI of the
destination host")},
{"migrateuri", VSH_OT_DATA, 0, N_("migration URI, usually can be
omitted")},
{"dname", VSH_OT_DATA, 0, N_("rename to new name during migration (if
supported)")},
+ {"timeout", VSH_OT_INT, 0, N_("auto cold migration fallback when live
migration timeouts(in seconds)")},
{NULL, 0, 0, NULL}
};
+static void migrateTimeoutHandler(void *data)
+{
+ virDomainPtr dom = (virDomainPtr)data;
+ virDomainInfo info;
+ unsigned int id;
+
+ id = virDomainGetID(dom);
+ if (id == ((unsigned int)-1))
+ return;
+
+ /* The error reason has been reported in virDomainGetInfo() and
+ * virDomainSuspend() when it fails. So we do not check the return value.
+ */
+ if (virDomainGetInfo(dom, &info) == 0) {
+ if (info.state == VIR_DOMAIN_SHUTOFF)
+ return;
+
+ /* suspend the domain when migration timeouts. */
+ virDomainSuspend(dom);
+ }
+}
+
static int
cmdMigrate (vshControl *ctl, const vshCmd *cmd)
{
@@ -3388,6 +3412,8 @@ cmdMigrate (vshControl *ctl, const vshCmd *cmd)
const char *desturi;
const char *migrateuri;
const char *dname;
+ long long timeout;
+ virTimerPtr migratetimer = NULL;
int flags = 0, found, ret = FALSE;
if (!vshConnectionUsability (ctl, ctl->conn))
@@ -3425,6 +3451,23 @@ cmdMigrate (vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptBool (cmd, "copy-storage-inc"))
flags |= VIR_MIGRATE_NON_SHARED_INC;
+ timeout = vshCommandOptLongLong(cmd, "timeout", &found);
+ if (found) {
+ if (! flags & VIR_MIGRATE_LIVE) {
+ vshError(ctl, "%s", _("migrate: Unexpected timeout for cold
migration"));
+ goto done;
+ }
+
+ if (timeout < 1) {
+ vshError(ctl, "%s", _("migrate: Invalid timeout"));
+ goto done;
+ }
+
+ migratetimer = virNewTimer(migrateTimeoutHandler, (void *)dom);
+ if (!migratetimer)
+ goto done;
+ }
+
if ((flags & VIR_MIGRATE_PEER2PEER) ||
vshCommandOptBool (cmd, "direct")) {
/* For peer2peer migration or direct migration we only expect one URI
@@ -3435,6 +3478,9 @@ cmdMigrate (vshControl *ctl, const vshCmd *cmd)
goto done;
}
+ if (migratetimer)
+ virModTimer(migratetimer, get_clock() + timeout * 1000000000ULL);
+
if (virDomainMigrateToURI (dom, desturi, flags, dname, 0) == 0)
ret = TRUE;
} else {
@@ -3445,6 +3491,9 @@ cmdMigrate (vshControl *ctl, const vshCmd *cmd)
dconn = virConnectOpenAuth (desturi, virConnectAuthPtrDefault, 0);
if (!dconn) goto done;
+ if (migratetimer)
+ virModTimer(migratetimer, get_clock() + timeout * 1000000000ULL);
+
ddom = virDomainMigrate (dom, dconn, flags, dname, migrateuri, 0);
if (ddom) {
virDomainFree(ddom);
@@ -3454,6 +3503,10 @@ cmdMigrate (vshControl *ctl, const vshCmd *cmd)
}
done:
+ if (migratetimer) {
+ virDelTimer(migratetimer);
+ virFreeTimer(migratetimer);
+ }
if (dom) virDomainFree (dom);
return ret;
}
--
1.7.1