openstack.nova.manager创建虚拟机流程(run_instance)
上面的代码整理自 nova/compute/manager.py 代码的顺序有改动,只是为了方便查看,完整的代码可以参考:https://github.com/openstack/nova/blob /master/nova/compute/manager.py
虚拟机的创建流程为3步走:
- 向nova-network申请IP
- 向nova-volumn申请block_device
- 向driver申请创建虚拟机
同时伴随整个过程,虚拟机的vm_state始终是building,但是task_state将会变化三次:
- networking
- block_device_mapping
- spawing
最后,虚拟机的vm_state变成active.
def run_instance(self): building proccess: None + networking | network_api.allocate_for_instance (nova-network) + block_device_mapping | setup_block_device_mapping (nova-volumn) + spawing | driver.spawn (nova-compute.driver) + Active """ instance = self.db.instance_get_by_uuid(context, instance_uuid) self._check_instance_not_already_created(context, instance) image_meta = self._check_image_size(context, instance) # VM_state = building # task_state = None self._instance_update(context, instance['uuid'], host=self.host, launched_on=self.host, vm_state=vm_states.BUILDING, task_state=None) # VM_state = building # task_state = networking self._instance_update(context, instance['uuid'], vm_state=vm_states.BUILDING, task_state=task_states.NETWORKING) network_info = self.network_api.allocate_for_instance( context, instance, vpn=is_vpn, requested_networks=requested_networks) # VM_state = building # task_statue = block_device_mapping self._instance_update(context, instance['uuid'], vm_state=vm_states.BUILDING, task_state=task_states.BLOCK_DEVICE_MAPPING) self._setup_block_device_mapping(context, instance) # VM_state = building # task_statue = spawing self._instance_update(context, instance['uuid'], vm_state=vm_states.BUILDING, task_state=task_states.SPAWNING) instance['injected_files'] = injected_files instance['admin_pass'] = admin_pass self.driver.spawn(context, instance, image_meta, self._legacy_nw_info(network_info), block_device_info) # VM_state = active # task_statue = None self._instance_update(context, instance['uuid'], power_state=current_power_state, vm_state=vm_states.ACTIVE, task_state=None, launched_at=utils.utcnow())