From 50716be0ed8f1336e7db57f9c91fe60c29bb7af1 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 4 Nov 2009 14:21:55 +0100
Subject: [PATCH] Migration: add check for listening target
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch adds a check for listening on the remote port in Xen and KVM
migrations. This will be generating a single β€œload of migration failed”
message for KVM, but otherwise not prevent the migration. For Xen (which
has a dedicated, always listening daemon) this should not create any
problems.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/hypervisor/hv_kvm.py | 6 +++++-
 lib/hypervisor/hv_xen.py | 6 ++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index eee6fe10c..11d25bd48 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -624,14 +624,18 @@ class KVMHypervisor(hv_base.BaseHypervisor):
 
     """
     instance_name = instance.name
+    port = instance.hvparams[constants.HV_MIGRATION_PORT]
     pidfile, pid, alive = self._InstancePidAlive(instance_name)
     if not alive:
       raise errors.HypervisorError("Instance not running, cannot migrate")
 
+    if not utils.TcpPing(target, port, live_port_needed=True):
+      raise errors.HypervisorError("Remote host %s not listening on port"
+                                   " %s, cannot migrate" % (target, port))
+
     if not live:
       self._CallMonitorCommand(instance_name, 'stop')
 
-    port = instance.hvparams[constants.HV_MIGRATION_PORT]
     migrate_command = 'migrate -d tcp:%s:%s' % (target, port)
     self._CallMonitorCommand(instance_name, migrate_command)
 
diff --git a/lib/hypervisor/hv_xen.py b/lib/hypervisor/hv_xen.py
index e29d4cab9..1addef7a3 100644
--- a/lib/hypervisor/hv_xen.py
+++ b/lib/hypervisor/hv_xen.py
@@ -402,7 +402,13 @@ class XenHypervisor(hv_base.BaseHypervisor):
     """
     if self.GetInstanceInfo(instance.name) is None:
       raise errors.HypervisorError("Instance not running, cannot migrate")
+
     port = instance.hvparams[constants.HV_MIGRATION_PORT]
+
+    if not utils.TcpPing(target, port, live_port_needed=True):
+      raise errors.HypervisorError("Remote host %s not listening on port"
+                                   " %s, cannot migrate" % (target, port))
+
     args = ["xm", "migrate", "-p", "%d" % port]
     if live:
       args.append("-l")
-- 
GitLab