Skip to content

Commit 10e2510

Browse files
committed
Add option to initiate migration from destination host
Fix ZSTAC-9603
1 parent 5210ef2 commit 10e2510

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

apibinding/apibinding/inventory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8383,6 +8383,7 @@ def __init__(self):
83838383
#mandatory field
83848384
self.vmInstanceUuid = NotNoneField()
83858385
self.hostUuid = None
8386+
self.migrateFromDestination = None
83868387
self.session = None
83878388
self.timeout = None
83888389
self.systemTags = OptionalList()

kvmagent/kvmagent/plugins/vm_plugin.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ def call_libvirt(conn):
928928
return secret.UUIDString()
929929

930930

931-
def get_vm_by_uuid(uuid, exception_if_not_existing=True):
931+
def get_vm_by_uuid(uuid, exception_if_not_existing=True, conn=None):
932932
try:
933933
# libvirt may not be able to find a VM when under a heavy workload, we re-try here
934934
@LibvirtAutoReconnect
@@ -937,7 +937,10 @@ def call_libvirt(conn):
937937

938938
@linux.retry(times=3, sleep_time=1)
939939
def retry_call_libvirt():
940-
return call_libvirt()
940+
if conn is None:
941+
return call_libvirt()
942+
else:
943+
return conn.lookupByName(uuid)
941944

942945
vm = Vm.from_virt_domain(retry_call_libvirt())
943946
return vm
@@ -1864,9 +1867,14 @@ def wait_job(_):
18641867
def migrate(self, cmd):
18651868
current_hostname = shell.call('hostname')
18661869
current_hostname = current_hostname.strip(' \t\n\r')
1870+
if cmd.migrateFromDestination:
1871+
hostname = cmd.destHostIp.replace('.', '-')
1872+
else:
1873+
hostname = cmd.srcHostIp.replace('.', '-')
1874+
18671875
if current_hostname == 'localhost.localdomain' or current_hostname == 'localhost':
18681876
# set the hostname, otherwise the migration will fail
1869-
shell.call('hostname %s.zstack.org' % cmd.srcHostIp.replace('.', '-'))
1877+
shell.call('hostname %s.zstack.org' % hostname)
18701878

18711879
destHostIp = cmd.destHostIp
18721880
destUrl = "qemu+tcp://{0}/system".format(destHostIp)
@@ -3360,13 +3368,33 @@ def detach_data_volume(self, req):
33603368

33613369
@kvmagent.replyerror
33623370
def migrate_vm(self, req):
3371+
@linux.retry(times=3, sleep_time=1)
3372+
def get_connect(srcHostIP):
3373+
return libvirt.open('qemu+tcp://{0}/system'.format(srcHostIP))
3374+
33633375
cmd = jsonobject.loads(req[http.REQUEST_BODY])
33643376
rsp = MigrateVmResponse()
33653377
try:
33663378
self._record_operation(cmd.vmUuid, self.VM_OP_MIGRATE)
33673379

3368-
vm = get_vm_by_uuid(cmd.vmUuid)
3369-
vm.migrate(cmd)
3380+
if cmd.migrateFromDestination:
3381+
conn = get_connect(cmd.srcHostIp)
3382+
if conn is None:
3383+
logger.warn('unable to connect qemu on host {0}'.format(cmd.srcHostIp))
3384+
raise kvmagent.KvmError('unable to connect qemu on host %s' % (cmd.srcHostIp))
3385+
3386+
vm = get_vm_by_uuid(cmd.vmUuid, False, conn)
3387+
if vm is None:
3388+
conn.close()
3389+
logger.warn('unable to find vm {0} on host {1}'.format(cmd.vmUuid, cmd.srcHostIp))
3390+
raise kvmagent.KvmError('unable to find vm %s on host %s' % (cmd.vmUuid, cmd.srcHostIp))
3391+
3392+
vm.migrate(cmd)
3393+
conn.close()
3394+
else:
3395+
vm = get_vm_by_uuid(cmd.vmUuid)
3396+
vm.migrate(cmd)
3397+
33703398
except kvmagent.KvmError as e:
33713399
logger.warn(linux.get_exception_stacktrace())
33723400
rsp.error = str(e)

0 commit comments

Comments
 (0)