OpenStack快照分析:(三)从磁盘启动云主机离线(在线)快照分析

磁盘启动云主机离线(在线)快照

1.1.   nova-api处理过程

磁盘启动的云主机在做离线快照时,还是首先是nova-api接收请求,函数入口和前述一样,还是 nova/api/openstack/compute/servers.py/ServersController._action_create_image下面一起来看看:

@wsgi.response(202)
@
extensions.expected_errors((400, 403, 404, 409))
# 定义关联的API接口
@
wsgi.action('createImage')
@
common.check_snapshots_enabled
@validation.schema(schema_servers.create_image, '2.0', '2.0')
@
validation.schema(schema_servers.create_image, '2.1')
def _action_create_image(self, req, id, body):
   
"""Snapshot a server instance."""
    # req中获取请求的上下文,并验证执行权限
   
context = req.environ['nova.context']
    context.can(server_policies.SERVERS %
'create_image')

   
# body中解析出传递的参数,快照名称及属性信息
    entity = body[
"createImage"]
    image_name = common.normalize_name(entity[
"name"])
    metadata = entity.get(
'metadata', {})
    snapshot_id = entity.get(
"snapshot_id", None)

   
# Starting from microversion 2.39 we don't check quotas on createImage
   
if api_version_request.is_supported(req, max_version=api_version_request.MAX_IMAGE_META_PROXY_API_VERSION):
      
 # 检查快照属性的相关配置信息
        common.check_img_metadata_properties_quota(context
, metadata)
   
    instance =
self._get_server(context, req, id)

    snapshot = snapshot_current(context
, instance, self.compute_rpcapi)
   
if snapshot:  # if there are snapshots, then create an image with snashots.
       
if not snapshot_id:
            snapshot_id = snapshot[
"id"]
        image = snapshot_create_image(context
, snapshot_id, instance, self.compute_rpcapi, entity)
   
else:
       
#从数据库中获取实例对象(InstanceV2)及块设备映射列表
        bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(context
, instance.uuid)

       
# 判断实例是镜像启动还是磁盘启动
       
if compute_utils.is_volume_backed_instance(context, instance, bdms):
           
# 校验执行权限
            context.can(server_policies.SERVERS % 
'create_image:allow_volume_backed')
    
       # 这里执行的是磁盘启动方式的快照,传递的参数包括:
            # 1
、关于权限的上下文context
            # 2
、虚拟机的实例对象instance
            # 3
、快照的名称image_name
            #
本章节讲的磁盘启动的云主机快照就是进入该分支进行操作。
            image =
self.compute_api.snapshot_volume_backed(contextinstanceimage_nameextra_properties=metadata)
       
else:
           
# 这里是镜像启动的云主机快照的入口,即上节内容
            image =
self.compute_api.snapshot(context, instance, image_name, extra_properties=metadata)

   
if api_version_request.is_supported(req, '2.45'):
       
return {'image_id': image['id']}

   
# build location of newly-created image entity
   
image_id = str(image['id'])
    image_ref = glance.generate_image_url(image_id)

    resp = webob.Response(
status_int=202)
    resp.headers[
'Location'] = image_ref
   
return resp

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxggp.html