这是在线迁移 源码分析的第三篇,Openstack liberty 云主机迁移源码分析之在线迁移2中分析了prepare
准备阶段nova-compute
的处理过程,本文将会分析execute
执行阶段的处理过程,下面一起来看具体内容:
execute
执行阶段
#`nova/compute/manager.py/ComputeManager._do_live_migration`
def _do_live_migration(self, context, dest, instance,
block_migration,
migration, migrate_data):
#省略prepare准备阶段的代码部分,具体分析请查阅前一篇博文
"""`prepare`准备阶段完成后,返回如下的字典pre_migration_data :
{'graphics_listen_addrs': {}, 'volume': {},
'serial_listen_addr': {}}
"""
migrate_data['pre_live_migration_result'] =
pre_migration_data
#更新`nova.instance_migrations`数据库,状态改为:running
if migration:
migration.status = 'running'
migration.save()
migrate_data['migration'] = migration
try:
"""调用虚拟化驱动(LibvirtDriver)执行迁移
请看下文的具体分析
"""
self.driver.live_migration(context, instance, dest,
self._post_live_migration,
self._rollback_live_migration,
block_migration, migrate_data)
except Exception:
# Executing live migration
# live_migration might raises exceptions, but
# nothing must be recovered in this version.
LOG.exception(_LE('Live migration failed.'),
instance=instance)
#迁移失败,更新`nova.instance_migrations`,状态改为:
#failed,并上抛异常
with excutils.save_and_reraise_exception():
if migration:
migration.status = 'failed'
migration.save()
---------------------------------------------------------------
#接上文:`nova/virt/libvirt/driver.py/LibvirtDriver.live_migration`
def live_migration(self, context, instance, dest,
post_method, recover_method,
block_migration=False,
migrate_data=None):
"""Spawning live_migration operation for distributing
high-load.
"""
# 'dest' will be substituted into 'migration_uri' so ensure
# it does't contain any characters that could be used to
# exploit the URI accepted by libivrt
#校验目的主机名是否合法,只能:单词字符、_、-、.、:
if not libvirt_utils.is_valid_hostname(dest):
raise exception.InvalidHostname(hostname=dest)
#下文分析
self._live_migration(context, instance, dest,
post_method, recover_method,
block_migration,
migrate_data)
---------------------------------------------------------------
#接上文:
def _live_migration(self, context, instance, dest, post_method,
recover_method, block_migration,
migrate_data):
"""Do live migration.
This fires off a new thread to run the blocking migration
operation, and then this thread monitors the progress of
migration and controls its operation
"""
#通过libvirt获取实例的virDomain对象,然后返回对应的Guest对象
guest = self._host.get_guest(instance)
# TODO(sahid): We are converting all calls from a
# virDomain object to use nova.virt.libvirt.Guest.
# We should be able to remove dom at the end.
dom = guest._domain
#启动新线程执行块迁移,下文具体分析
opthread = utils.spawn(self._live_migration_operation,
context, instance