云主机快照功能实现:
OpenStack虚拟机启动源支持镜像和卷,这两种方式创建出的虚拟机,快照过程
也不同。
1:由镜像启动的虚拟机做快照时,只对系统盘进行快照,不对数据盘进行快照,快照过程
中要挂起虚拟机,因此虚拟机中运行的业务可能会中断。快照完成后,将快照上传到
glance服务中,保存为snapshot类型的镜像,并恢复虚拟机状态。
2:由卷启动的虚拟机做快照时,同时对系统盘和数据盘进行快照,快照实际执行由cinder
存储服务完成。因此可以利用存储后端的快照功能。当cinder存储后端为ceph时,就可以
利用ceph的增量快照功能,降低快照时间,减少快照过程对于虚拟机中运行业务的影响。
具体流程如下:
boot from volume vs boot from image in nova snapshot
* boot from volume
* nova snapshot will create snapshot for all attached cinder volume
* boot from image
* nova snapshot will not create snapshot for any attached cinder volume
*
从 api 层,对于两种启动方式就做了不同的处理,具体看 [ def _action_create_image(self, req, id, body):](https://github.com/openstack/nova/blob/6c52612fb5d25b3e23a7b4d0b8c1f91a3d9b4b41/nova/api/openstack/compute/servers.py#L1231-L1246)
```python
try:
if compute_utils.is_volume_backed_instance(context, instance,
bdms):
context.can(server_policies.SERVERS %
'create_image:allow_volume_backed', target=target)
image = self.compute_api.snapshot_volume_backed(
context,
instance,
image_name,
extra_properties=
metadata)
else:
image = self.compute_api.snapshot(context,
instance,
image_name,
extra_properties=metadata)
对于 boot from volume 的虚拟机,也会生成一个 0 字节的 image snapshot, 他主要拿着volume 的相关信息,当恢复的时候,可以从这个 image snapshot 恢复
[
{
"guest_format": null,
"boot_index": 0,
"delete_on_termination": true,
"no_device": null,
"snapshot_id": "1e9b1bbc-13c8-46de-b692-0a5cb6485cd0",
"volume_type": null,
"device_name": "/dev/sda",
"disk_bus": "scsi",
"image_id": null,
"source_type": "snapshot",
"tag": null,
"device_type": "disk",
"volume_id": null,
"destination_type": "volume",
"volume_size": 40
},
{
"guest_format": null,
"boot_index": null,
"delete_on_termination": false,
"no_device": null,
"snapshot_id": "5a1548d7-f105-4fbd-afa4-f61a68cdef3b",
"volume_type": null,
"device_name": "/dev/sda",
"disk_bus": "scsi",
"image_id": null,
"source_type": "snapshot",
"tag": null,
"device_type": "disk",
"volume_id": null,
"destination_type": "volume",
"volume_size": 40
},