Master code detach volume from VM

来源:互联网 发布:电力牵引预算编制软件 编辑:程序博客网 时间:2024/06/08 18:28

卷卸载 业务流程

  1. 访问 keystone 组件做用户认证
  2. 认证通过,携带 token 请求 novaclient
  3. novaclient 接受请求,进行卷卸载
    • 卸载请求路由到 VolumeAttachmentController,进入 nova-api
    • nova-api 调用 cinderclient 获取 volume 信息,检证,并设定状态 detaching
    • nova-api RPC CAST 到 nova-compute 进行卷卸载
    • 根据 nova.conf 的配置项 compute_driver 获取 LibvirtDriver
    • 获取 BDM,根据 BDM source_type 获取驱动 DriverVolumeBlockDevice
    • 调用 DriverVolumeBlockDevice 进行卸载
      • 调用 os_brick 获取 connector 信息
      • 调用 LibvirtDriver 在主机端卸载
        • VM xml 删除挂载卷信息
        • 根据 BDM 中 connection_info 的 driver_volume_type 获取 LibvirtISCSIVolumeDriver,调用 LibvirtISCSIVolumeDriver 进行主机卸载
        • 调用 cinderclient 进行存储端卸载
        • 调用 cinderclient 更新 cinder DB
    • 更新 BDM 数据库数据

nova detach volume 源码分析(基于 master 源码)

客户端接受请求并路由到 VolumeAttachmentController

nova client    ↓DELETE DELETE /v2.1/servers/c2486c45-5a8f-4028-8cd1-51c0425f677a/os-volume_attachments/30d72547-9316-4c2a-84c9-6ebc30912e0a    ↓nova.api.openstack.compute.volumes.VolumeAttachmentController.delete                    ↓        调用 cinderclient 获取 volumevolume = self.volume_api.get(context, volume_id)                    ↓获取 block_device_mapping 中该 instance 挂载的所有卷(用法应该可以优化)bdms = objects.BlockDeviceMappingList.get_by_instance_uuid                    ↓            核心的取消挂载过程self.compute_api.detach_volume(context, instance, volume)    self.compute_api 可用的有两个:        1. nova.compute.cells_api.ComputeCellsAPI        2. nova.compute.api.API    根据 nova.conf 配置项决定使用哪一个,默认情况下是 nova.compute.api.API

nova-api 处理过程

nova.compute.api.API.detach_volume    self._detach_volume        ↓nova.compute.api.API._detach_volume    step1: self._check_and_begin_detach(context, volume, instance)            ↓    step2: self.compute_rpcapi.detach_volume(context, instance=instance,            volume_id=volume['id'], attachment_id=attachment_id)

step1: 取消挂载前准备工作

nova.compute.rpcapi.ComputeAPI.detach_volume    1. self.volume_api.check_detach(context, volume, instance=instance)                        ↓        nova.volume.cinder.API.check_detach(和 cinder 组件无交互)        直接判断 volume 里面相关数据(available,detached,attachments)    2. self.volume_api.begin_detaching(context, volume['id'])                        ↓        nova.volume.cinder.API.begin_detaching        cinderclient begin_detaching(os-begin_detaching)        更新 cinder DB 数据:'status': 'detaching'

step2: 卸载卷

RPC CAST 到 nova-compute
self.compute_rpcapi.detach_volume            ↓nova.compute.rpcapi.detach_volume PRC CAST            ↓nova.compute.manager.ComputeManager.detach_volume
核心卸载卷流程(ComputeManager.detach_volume)
nova.compute.manager.ComputeManager.detach_volume            ↓nova.compute.manager.ComputeManager._detach_volume                ↓         获取 BlockDeviceMapping 对象bdm = objects.BlockDeviceMapping.get_by_volume_and_instance                ↓         获取 BDM driverdriver_bdm = driver_block_device.convert_volume(bdm)driver_bdm: nova.virt.block_device.DriverVolumeBlockDevice                ↓         实际上调用 BDM driver 执行取消挂载driver_bdm.detach(context, instance, self.volume_api, self.driver,                  attachment_id=attachment_id, destroy_bdm=destroy_bdm)self.volume_api = nova.volume.cinder.APIself.driver = nova.virt.libvirt.driver.LibvirtDriver                ↓         数据库删除(设定 deleted flag 位)        bdm.destroy()
BDM driver detach
      nova.virt.block_device.DriverVolumeBlockDevice.detach                             ↓         获取 connector,为 terminate_connection 提供入参+-STEP 1---------------------|-----------------------------------+| connector = virt_driver.get_volume_connector(instance)         ||                            ↓                                   || nova.virt.libvirt.driver.LibvirtDriver.get_volume_connector    ||                            ↓                                   || os_brick.initiator.connector.get_connector_properties          |+----------------------------|-----------------------------------+                             ↓                      主机端取消挂载卷+-STEP 2---------------------|-----------------------------------+| self.driver_detach(context, instance, volume_api, virt_driver) ||                            ↓                                   | |                virt_driver.detach_volume                       ||    nova.virt.libvirt.driver.LibvirtDriver.detach_volume        ||                            ↓                                   ||                    VM xml 删除挂载卷信息                       ||                guest.detach_device_with_retry                  ||                            ↓                                   ||                     主机端取消挂载                             ||        self._disconnect_volume(connection_info, disk_dev)      ||                            ↓                                   ||     根据 driver_volume_type 获取 LibvirtISCSIVolumeDriver      ||    vol_driver = self._get_volume_driver(connection_info)       ||    vol_driver.disconnect_volume(connection_info, disk_dev)     ||                            ↓                                   ||   nova.virt.libvirt.volume.iscsi.LibvirtISCSIVolumeDriver.     ||                    disconnect_volume                           |+----------------------------|-----------------------------------+                             ↓                     cinder 端取消后端存储挂载关系+-STEP 3---------------------|-----------------------------------+| volume_api.terminate_connection(context, volume_id, connector) ||                            ↓                                   | |       nova.volume.cinder.API.terminate_connection              ||   cinderclient terminate_connection(os-terminate_connection)   |+----------------------------|-----------------------------------+                             ↓                     更新 cinder DB 数据+-STEP 4---------------------|-----------------------------------+|                       volume_api.detach                        ||                            ↓                                   | |                nova.volume.cinder.API.detach                   ||                cinderclient detach(os-detach)                  |+----------------------------------------------------------------+

  • 卷卸载 业务流程
  • nova detach volume 源码分析基于 master 源码
      • 客户端接受请求并路由到 VolumeAttachmentController
      • nova-api 处理过程
        • step1 取消挂载前准备工作
        • step2 卸载卷
          • RPC CAST 到 nova-compute
          • 核心卸载卷流程ComputeManagerdetach_volume
            • BDM driver detach

0 0
原创粉丝点击