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

原创
2017/02/06 10:45
阅读数 2.1K

#  一、冷迁移和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。 后面将继续介绍两者底层相同的调用逻辑。

展开阅读全文
打赏
0
2 收藏
分享
加载中
更多评论
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部