@@ -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 )
@@ -3367,13 +3375,33 @@ def detach_data_volume(self, req):
33673375
33683376 @kvmagent .replyerror
33693377 def migrate_vm (self , req ):
3378+ @linux .retry (times = 3 , sleep_time = 1 )
3379+ def get_connect (srcHostIP ):
3380+ return libvirt .open ('qemu+tcp://{0}/system' .format (srcHostIP ))
3381+
33703382 cmd = jsonobject .loads (req [http .REQUEST_BODY ])
33713383 rsp = MigrateVmResponse ()
33723384 try :
33733385 self ._record_operation (cmd .vmUuid , self .VM_OP_MIGRATE )
33743386
3375- vm = get_vm_by_uuid (cmd .vmUuid )
3376- vm .migrate (cmd )
3387+ if cmd .migrateFromDestination :
3388+ conn = get_connect (cmd .srcHostIp )
3389+ if conn is None :
3390+ logger .warn ('unable to connect qemu on host {0}' .format (cmd .srcHostIp ))
3391+ raise kvmagent .KvmError ('unable to connect qemu on host %s' % (cmd .srcHostIp ))
3392+
3393+ vm = get_vm_by_uuid (cmd .vmUuid , False , conn )
3394+ if vm is None :
3395+ conn .close ()
3396+ logger .warn ('unable to find vm {0} on host {1}' .format (cmd .vmUuid , cmd .srcHostIp ))
3397+ raise kvmagent .KvmError ('unable to find vm %s on host %s' % (cmd .vmUuid , cmd .srcHostIp ))
3398+
3399+ vm .migrate (cmd )
3400+ conn .close ()
3401+ else :
3402+ vm = get_vm_by_uuid (cmd .vmUuid )
3403+ vm .migrate (cmd )
3404+
33773405 except kvmagent .KvmError as e :
33783406 logger .warn (linux .get_exception_stacktrace ())
33793407 rsp .error = str (e )
0 commit comments