# 1、冷迁移和resize前端
迁移是指将虚拟机从一个计算节点迁移到另一个节点上。冷迁移是相对热迁移而言,区别在于冷迁移过程当中虚拟机时关机或者是处于不可用的状态,而热迁移则须要保证虚拟机时刻运行。 Resize则是根据需求从新调整虚拟机的计算能力和资源。Resize和冷迁移的工做流程相同,区别只是Resize时必须保持新的Flavor配置大于旧的配置,而冷迁移则要求二者相同。Resize的工做流程如图所示:api
nova-api在收到虚拟机resize的请求后,会去调用/nova/api/openstack/compute/servers.py中的_action_resize()方法,部分代码和注释以下: ui
@wsgi.response(202) @extensions.expected_errors((400, 401, 403, 404, 409)) @wsgi.action('resize') @validation.schema(schema_server_resize) def _action_resize(self, req, id, body): """Resizes a given instance to the flavor size requested.""" resize_dict = body['resize'] #获取升级flavor flavor_ref = str(resize_dict["flavorRef"])orm
resize_kwargs = {}server
if list(self.resize_extension_manager): self.resize_extension_manager.map(self._resize_extension_point, resize_dict, resize_kwargs)接口
self._resize(req, id, flavor_ref, **resize_kwargs)资源
接着调用_resize()方法,部分代码和注释以下:get
def _resize(self, req, instance_id, flavor_id, **kwargs): """Begin the resize process with given instance/flavor.""" context = req.environ["nova.context"] authorize(context, action='resize') #经过instance_id获取虚拟机实例的具体信息 instance = self._get_server(context, req, instance_id)虚拟机
try: #传递instance和flavor_id两个参数 self.compute_api.resize(context, instance, flavor_id, **kwargs) except exception.InstanceUnknownCell as e: raise exc.HTTPNotFound(explanation=e.format_message()) except exception.QuotaError as error: raise exc.HTTPForbidden( explanation=error.format_message()) except exception.FlavorNotFound: msg = _("Unable to locate requested flavor.") raise exc.HTTPBadRequest(explanation=msg) except exception.CannotResizeToSameFlavor: msg = _("Resize requires a flavor change.") raise exc.HTTPBadRequest(explanation=msg) except (exception.CannotResizeDisk, exception.AutoDiskConfigDisabledByImage) as e: raise exc.HTTPBadRequest(explanation=e.format_message()) except exception.InstanceIsLocked as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'resize', instance_id) except exception.ImageNotAuthorized: msg = _("You are not authorized to access the image " "the instance was started with.") raise exc.HTTPUnauthorized(explanation=msg) except exception.ImageNotFound: msg = _("Image that the instance was started " "with could not be found.") raise exc.HTTPBadRequest(explanation=msg) except (exception.NoValidHost, exception.AutoDiskConfigDisabledByImage) as e: raise exc.HTTPBadRequest(explanation=e.format_message()) except exception.Invalid: msg = _("Invalid instance image.") raise exc.HTTPBadRequest(explanation=msg)工作流
nova-api在收到虚拟机冷迁移的请求后,会去调用/nova/api/openstack/compute/migrate_server.py中的_migrate()方法,部分代码和注释以下:
@wsgi.response(202) @extensions.expected_errors((400, 403, 404, 409)) @wsgi.action('migrate') def _migrate(self, req, id, body): """Permit admins to migrate a server to a new host.""" context = req.environ['nova.context'] authorize(context, action='migrate') #得到虚拟机信息 instance = common.get_instance(self.compute_api, context, id) try: #仅传递instance一个参数 self.compute_api.resize(req.environ['nova.context'], instance) except (exception.TooManyInstances, exception.QuotaError) as e: raise exc.HTTPForbidden(explanation=e.format_message()) except exception.InstanceIsLocked as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'migrate', id) except exception.InstanceNotFound as e: raise exc.HTTPNotFound(explanation=e.format_message()) except exception.NoValidHost as e: raise exc.HTTPBadRequest(explanation=e.format_message())
从上面的分析能够看出,冷迁移和resize底层接口一致,假如前端传入了新的flavor,则是resize,新的flavor被传入底层。迁移时传入底层的flavor则为自身实例相同的flavor。底层根据传入的flavor参数执行相同的逻辑。resize和冷迁移的区别是在迁移的同时,是否改变虚拟机的flavor。 后面将继续介绍二者底层相同的调用逻辑。