原文网址:http://blog.csdn.net/xiangpingli/article/details/47912777
nova还有一种启动方式:“从镜像启动(创建一个新卷)”
这个流程中,nova会在_prep_block_device中的attach_block_device去调用cinder的create创建一个卷
然后会在_prep_block_device中的attach_block_device去调用cinder的attach挂载这个卷
然后才去spawn,孵化虚拟机,在spawn中会调用_create_image来创建镜像,这里创建镜像不是创建卷,卷是在_prep_block_device创建好的,除非是“从镜像启动”才会在
_create_image创建卷。
下面看下nova中“从镜像启动(创建一个新卷)”创建虚拟机的流程:
- nova.api.openstack.compute.servers.Controller.create
- ->nova.compute.api.API.create
- ->nova.compute.api.API._create_instance
- ->nova.comductor.ComputeTaskAPI.build_instances
- ->nova.conductor. rpcapi.ComputeTaskAPI.build_instances
- ->nova.conductor. manager.ComputeTaskAPI.build_instances
- ->nova.compute.rpcapi.ComputeManager.build_and_run_instance
- ->nova.compute.manager.ComputeManager._do_build_and_run_instance
- ->nova.compute.manager.ComputeManager._build_and_run_instance //直到这里,流程仍与前两种创建虚拟机的情况相同,看看最终是哪里有差别
- ->nova.compute.manager.ComputeManager._build_resources
- ->nova.compute.manager.ComputeManager._build_networks_for_instance //准备网络资源
- ->nova.compute.manager.ComputeManager._prep_block_device //准备块设备
- ->nova.compute.manager.ComputeManager.attach_block_devices
- ->nova.virt.block_device.attach_block_devices
- ->nova.virt.block_device.DriverImageBlockDevice.attach
- ->nova.virt.driver.create
- ->nova.virt.libvrit.driver.create 创建一个新卷
- ->nova.virt.block_device.DriverVolumeBlockDevice.attach (DriverVolumeBlockDevice是DriverImageBlockDevice的父类)
- ->nova.virt.driver.attach
- ->nova.virt.libvirt.driver.attach 挂载这个卷
- ->nova.virt.libvirt.driver.LibvirtDriver.spawn
- //再回过头来梳理下spawn,是不是这时候块设备都有了呢?之前的流程是不是有点问题?
- //是不是块设备在_prep_block_device都建好了,后面只是向里面填不同的内容?
- //那么spawn中的_create_image 又做了什么呢?上面的_prep_block_device和spawn中的_create_image分别做什么?看起来都像建卷?
- //事实上,是一个建卷(_prep_block_device只是块设备,里面没有东西)而一个是在卷上组织镜像(_create_image之前卷已经存在,这里只是组织镜像)
- //另外考虑分支情况:(1)如果卷存在_prep_block_device流程怎么走?(2)如果从云硬盘启动_create_image流程怎么走
- //也就是说把分支情况也要考虑好
- 接上面:
- ->nova.virt.libvirt.driver.LibvirtDriver.spawn
- ->Nova.virt.libvirt.driver._create_image //组织镜像
- 这里会检查是从云硬盘启动,还是不从云硬盘启动:
- booted_from_volume = self._is_booted_from_volume(
- instance, disk_mapping)
- ...
- if not booted_from_volume:
- ->Nova.virt.libvirt.driver._try_fetch_image_cache
- ->Nova.virt.libvirt.imagebackend.Image.cache
- ->Nova.virt.libvirt.imagebackend.Qcow2.create_image
- ->Nova.virt.libvirt.imagebackend.Qcow2.create_image:prepare_template即fetch_image
- ->Nova.virt.libvirt.utils.fetch_image
- ->Nova.virt.images.fetch_to_raw
- ->Nova.virt.images.fetch #下载系统镜像到/var/lib/nova/instances/_base/目录
- ->nova.image.glance.GlanceImageService.download
- ->nova.image.glance.GlanceClientWrapper.call
- ->Nova.virt.images.convert_image #若镜像不是raw格式,且nova.conf中force_raw_images=True,则将backing file强制转换为raw格式
- ->Nova.virt.libvirt.imagebackend.Qcow2.create_image:copy_qcow2_image
- ->nova.virt.libvirt.utils.create_cow_image