openstack-nova-虚机重启过程以及源码分析

基于openstack stein

api收到重启请求后, _action_reboot 调用 compute_api 进行reboot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
nova.api.openstack.compute.servers.ServersController
def _action_reboot(self, req, id, body):

reboot_type = body['reboot']['type'].upper()
context = req.environ['nova.context']
context.can(server_policies.SERVERS % 'reboot')
instance = self._get_server(context, req, id)

try:
self.compute_api.reboot(context, instance, reboot_type)
.................................................

nova.compute.api.API
@check_instance_lock
def reboot(self, context, instance, reboot_type):
"""Reboot the given instance."""
if reboot_type == 'SOFT':
self._soft_reboot(context, instance)
else:
self._hard_reboot(context, instance)

软重启和硬重启,将请求通过rpc 传给了nova compute

1
2
3
4
5
6
7
8
9
nova.compute.manager.ComputeManager
def reboot_instance(self, context, instance, block_device_info,
reboot_type):
@utils.synchronized(instance.uuid)
def do_reboot_instance(context, instance, block_device_info,
reboot_type):
self._reboot_instance(context, instance, block_device_info,
reboot_type)
do_reboot_instance(context, instance, block_device_info, reboot_type)

变更虚机状态=> 查询虚机block_device_map 信息=>查询虚机network info, 最终调用libvirt 重启虚机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def _reboot_instance(self, context, instance, block_device_info,
reboot_type):
"""Reboot an instance on this host."""
# acknowledge the request made it to the manager
if reboot_type == "SOFT":
instance.task_state = task_states.REBOOT_PENDING
expected_states = task_states.soft_reboot_states
else:
instance.task_state = task_states.REBOOT_PENDING_HARD
expected_states = task_states.hard_reboot_states

context = context.elevated()
LOG.info("Rebooting instance", instance=instance)

bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(
context, instance.uuid)
block_device_info = self._get_instance_block_device_info(
context, instance, bdms=bdms)

network_info = self.network_api.get_instance_nw_info(context, instance)
........................................................................
self.driver.reboot(context, instance,
network_info,
reboot_type,
block_device_info=block_device_info,
bad_volumes_callback=bad_volumes_callback)