虚拟机冷迁移和resize代码分析(一)

#  一、冷迁移和resize

  迁移是指将虚拟机从一个计算节点迁移到另外一个节点上。冷迁移是相对热迁移而言,区别在于冷迁移过程中虚拟机时关机或者是处于不可用的状态,而热迁移则需要保证虚拟机时刻运行。      Resize则是根据需求重新调整虚拟机的计算能力和资源。Resize和冷迁移的工作流程相同,区别只是Resize时必须保持新的Flavor配置大于旧的配置,而冷迁移则要求两者相同。Resize的工作流程如图所示:

1.resize过程

  nova-api在收到虚拟机resize的请求后,会去调用/nova/api/openstack/compute/servers.py中的_action_resize()方法,部分代码和注释如下: 

    @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"])

        resize_kwargs = {}

        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()方法,部分代码和注释如下:

    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)

2.冷迁移过程

  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。 后面将继续介绍两者底层相同的调用逻辑。

转载于:https://my.oschina.net/u/1179767/blog/832254

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值