nova start 虚机的代码流程分析
nova start 虚机的代码流程分析,以ocata版本为分析基础
1、nova api服务接受用户下发的 nova start启动虚机请求
其对应的http restfull api接口为post /servers/{server_id}/action
发送的action为os-start
nova/api/openstack/compute/servers.py
class ServersController(wsgi.Controller):
def _start_server(self, req, id, body):
"""Start an instance."""
context = req.environ['nova.context']
instance = self._get_instance(context, id)
context.can(server_policies.SERVERS % 'start', instance)
try:
self.compute_api.start(context, instance)--------compute服务的api模块接受该请求
except (exception.InstanceNotReady, exception.InstanceIsLocked) as e:
raise webob.exc.HTTPConflict(explanation=e.format_message())
except exception.InstanceUnknownCell as e:
raise exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,
'start', id)
2、nova compute模块的api处理该请求
nova/compute/api.py
class API(base.Base):
@check_instance_state(vm_state=[vm_states.STOPPED])
def start(self, context, instance):
"""Start an instance."""
LOG.debug("Going to try to start instance", instance=instance) instance.task_state = task_states.POWERING_ON
instance.save(expected_task_state=[None]) self._record_action_start(context, instance, instance_actions.START)-----记录对虚机的action操作
# TODO(yamahata): injected_files isn't supported right now.
# It is used only for osapi. not for ec2 api.
# availability_zone isn't used by run_instance.
self.compute_rpcapi.start_instance(context, instance)-----给nova compute服务发送rpc请求
3、nova-compute接受rpc请求,最终接受rpc请求的是,对应的manager.py文件中的对应方法
nova/compute/rpcapi.py
@profiler.trace_cls("rpc")
class ComputeAPI(object):
def start_instance(self, ctxt, instance):
version = '4.0'
cctxt = self.router.by_instance(ctxt, instance).prepare(
server=_compute_host(None, instance), version=version)
cctxt.cast(ctxt, 'start_instance', instance=instance) nova/compute/manager.py
def start_instance(self, context, instance):
"""Starting an instance on this host."""
self._notify_about_instance_usage(context, instance, "power_on.start")-----发送虚机上电开始的信息
compute_utils.notify_about_instance_action(context, instance,
self.host, action=fields.NotificationAction.POWER_ON,
phase=fields.NotificationPhase.START)
self._power_on(context, instance)-------核心,给虚机上电 3.1
instance.power_state = self._get_power_state(context, instance)
instance.vm_state = vm_states.ACTIVE
instance.task_state = None # Delete an image(VM snapshot) for a shelved instance
snapshot_id = instance.system_metadata.get('shelved_image_id')
if snapshot_id:
self._delete_snapshot_of_shelved_instance(context, instance,
snapshot_id) # Delete system_metadata for a shelved instance
compute_utils.remove_shelved_keys_from_system_metadata(instance) instance.save(expected_task_state=task_states.POWERING_ON)
self._notify_about_instance_usage(context, instance, "power_on.end")----发送虚机上电完成的信息
compute_utils.notify_about_instance_action(context, instance,
self.host, action=fields.NotificationAction.POWER_ON,
phase=fields.NotificationPhase.END)
3.1 对_power_on(context, instance)函数的详解
nova/compute/manager.py
def _power_on(self, context, instance):
network_info = self.network_api.get_instance_nw_info(context, instance)-----s1 获取虚拟机的网络信息
block_device_info = self._get_instance_block_device_info(context,instance)----s2 获取虚机挂在卷的信息
self.driver.power_on(context, instance,network_info,block_device_info)--- s3
s3 由于openstack默认使用libvirt,所以调用的libvirt的驱动,
为了确保在创建虚机的时候,镜像、网络、要挂在的块设备有效和正常建立,采用了硬重启
nova/virt/libvirt/driver.py
class LibvirtDriver(driver.ComputeDriver):
def power_on(self, context, instance, network_info,
block_device_info=None):
"""Power on the specified instance."""
self._hard_reboot(context, instance, network_info, block_device_info)
3.1.1 对_hard_reboot函数的详解
该函数主要做的功能是:
1)强制关闭虚拟机
2)删除虚机的xml文件
3)获取虚机镜像信息
4)生成xml文件
5)创建xml,启动虚机
nova/virt/libvirt/driver.py
class LibvirtDriver(driver.ComputeDriver): def _hard_reboot(self, context, instance, network_info,
block_device_info=None):
"""Reboot a virtual machine, given an instance reference.
Performs a Libvirt reset (if supported) on the domain.
If Libvirt reset is unavailable this method actually destroys and
re-creates the domain to ensure the reboot happens, as the guest
OS cannot ignore this action.
""" self._destroy(instance)-----s1
# Domain XML will be redefined so we can safely undefine it
# from libvirt. This ensure that such process as create serial
# console for guest will run smoothly.
self._undefine_domain(instance)--s2 # Convert the system metadata to image metadata
# NOTE(mdbooth): This is a workaround for stateless Nova compute
# https://bugs.launchpad.net/nova/+bug/1349978
instance_dir = libvirt_utils.get_instance_path(instance)----s3
fileutils.ensure_tree(instance_dir) disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type,instance, instance.image_meta,---s4
block_device_info)
# NOTE(vish): This could generate the wrong device_format if we are
# using the raw backend and the images don't exist yet.
# The create_images_and_backing below doesn't properly
# regenerate raw backend images, however, so when it
# does we need to (re)generate the xml after the images
# are in place.
xml = self._get_guest_xml(context, instance, network_info, disk_info,----s5
instance.image_meta,
block_device_info=block_device_info) # NOTE(mdbooth): context.auth_token will not be set when we call
# _hard_reboot from resume_state_on_host_boot()
if context.auth_token is not None:-----------------s6
# NOTE (rmk): Re-populate any missing backing files.
backing_disk_info = self._get_instance_disk_info(instance.name,
xml,
block_device_info)
self._create_images_and_backing(context, instance, instance_dir,
backing_disk_info) # Initialize all the necessary networking, block devices and
# start the instance.
self._create_domain_and_network(context, xml, instance, network_info,-------s7
disk_info,
block_device_info=block_device_info,
reboot=True,
vifs_already_plugged=True)
self._prepare_pci_devices_for_use(
pci_manager.get_instance_pci_devs(instance, 'all')) def _wait_for_reboot():
"""Called at an interval until the VM is running again."""
state = self.get_info(instance).state if state == power_state.RUNNING:
LOG.info(_LI("Instance rebooted successfully."),
instance=instance)
raise loopingcall.LoopingCallDone() timer = loopingcall.FixedIntervalLoopingCall(_wait_for_reboot)
timer.start(interval=0.5).wait()
在虚机启动的时候,涉及虚机相关信息变化的目录有三个
/var/lib/libvirt/qemu----存放虚机运行domain域的目录
/etc/libvirt/qemu---------存放虚机xml文件的目录
/os_instance/_base--------存储虚机镜像的目录
/os_instance/虚机uuid-----存放虚机disk磁盘信息的目录
s1 执行时,上面目录没有任何变化
s2 执行时,/etc/libvirt/qemu目录下,虚机对应的xml删除,其他目录变化
s3 执行时,获取虚机的目录/os_instance/03cb8a7c-786f-402a-b059-1f2d90e69bd4
s4 执行时,获取虚机disk信息
主要相关参数的值如下:
CONF.libvirt.virt_type='kvm'
block_device_info={'swap': None, 'root_device_name': u'/dev/vda', 'ephemerals': [], 'block_device_mapping': []}
disk_info={'disk_bus': 'virtio',
'cdrom_bus': 'ide',
'mapping': {'disk.config': {'bus': 'ide', 'type': 'cdrom', 'dev': 'hda'},
'disk': {'bus': 'virtio', 'boot_index': '1', 'type': 'disk', 'dev': u'vda'},
'root': {'bus': 'virtio', 'boot_index': '1', 'type': 'disk', 'dev': u'vda'}}
}
s5 执行时,生成xml文件信息,此时,只是在内存里面存放,还没有写到/etc/libvirt/qemu目录对应的xml文件里面
s6 context.auth_token 存放token信息
backing_disk_info=[
{'disk_size': 149159936,
'backing_file': '4c4935095cb43925d61d67395c452ea248e6b1c4',
'virt_disk_size': 85899345920,
'path': '/os_instance/03cb8a7c-786f-402a-b059-1f2d90e69bd4/disk',
'type': 'qcow2',
'over_committed_disk_size': 85750185984},
{'disk_size': 489472,
'backing_file': '',
'virt_disk_size': 489472,
'path': '/os_instance/03cb8a7c-786f-402a-b059-1f2d90e69bd4/disk.config',
'type': 'raw',
'over_committed_disk_size': 0}
]
s7 执行时,在/etc/libvirt/qemu目录下生成虚机对应的xml文件,其他目录无变化
nova stop 虚机的时候,这三个目录的变化情况
/var/lib/libvirt/qemu目录下domain-21-instance-xx,相关的目录会被删除,其他目录无变化
nova start 虚机的代码流程分析的更多相关文章
- Openstack之Nova创建虚机流程分析
前言 Openstack作为一个虚拟机管理平台,核心功能自然是虚拟机的生命周期的管理,而负责虚机管理的模块就是Nova. 本文就是openstack中Nova模块的分析,所以本文重点是以 ...
- u-boot移植(三)---修改前工作:代码流程分析2
一.vectors.S 1.1 代码地址 vectors.S (arch\arm\lib) 1.2 流程跳转 跳转符号 B 为 start.S 中的 reset 执行代码,暂且先不看,先看看 vect ...
- u-boot移植(四)---修改前工作:代码流程分析3---代码重定位
一.重定位 1.以前版本的重定位 2.新版本 我们的程序不只涉及一个变量和函数,我们若想访问程序里面的地址,则必须使用SDRAM处的新地址,即我们的程序里面的变量和函数必须修改地址.我们要修改地址,则 ...
- openstack nova 创建虚机流程
1文件 nova.api.openstack.coumpute.servers1函数 def create(self, req, body):1调用 (instances, resv_id) = se ...
- u-boot移植(二)---修改前工作:代码流程分析1
一.代码执行总体流程图 1.1 代码路径 U-boot.lds (arch\arm\cpu) vectors.S (arch\arm\lib) start.S (arch\arm\cpu\arm920 ...
- hyperledger fabric超级账本java sdk样例e2e代码流程分析
一 checkConfig Before 1.1 private static final TestConfig testConfig = TestConfig.getConfig() ...
- 十、uboot 代码流程分析---run_main_loop
调用 board_init_r,传入全局 GD 和 SDRAM 中的目的地址 gd->rellocaddr void board_init_r(gd_t *new_gd, ulong dest_ ...
- 九、uboot 代码流程分析---relloc_code
执行完 board_init_f 后,重新跳转回 _main 中执行. 9.1 relloc_code 前 9.1.1 gd 设置 在调用board_init_f()完成板卡与全局结构体变量 gd 的 ...
- 八、uboot 代码流程分析---board_init_f
接着上一节,板子开始做前期初始化工作. 8.1 board_init_f Board_f.c (common) /* 板子初次初始化.boot_flags = 0 */ void board_init ...
随机推荐
- hashlib加密算法
# import hashlib # mima = hashlib.md5()#创建hash对象,md5是信息摘要算法,生成128位密文 # print(mima) # # mima.update(' ...
- PHP octdec() 函数
实例 把八进制转换为十进制: <?php高佣联盟 www.cgewang.comecho octdec("36") . "<br>";echo ...
- luogu P5410 模板 扩展 KMP Z函数 模板
LINK:P5410 模板 扩展 KMP Z 函数 画了10min学习了一下. 不算很难 思想就是利用前面的最长匹配来更新后面的东西. 复杂度是线性的 如果不要求线性可能直接上SA更舒服一点? 不管了 ...
- 7.12 NOI模拟赛 积性函数求和 数论基础变换 莫比乌斯反演
神题! 一眼powerful number 复习了一下+推半天. 可以发现G函数只能为\(\sum_{d}[d|x]d\) 不断的推 可以发现最后需要求很多块G函数的前缀和 发现只有\(\sqrt(n ...
- 剑指 Offer 58 - I. 翻转单词顺序
本题 题目链接 题目描述 我的题解 方法一:库函数split() 要注意str.split()函数: 字符串str前有 n 个空格时,分割出来的字符串列表中会多出 n 个空字符串: 字符串str某两个 ...
- jetbrain的plugin repository地址
jetbrain的plugin repository地址:https://plugins.jetbrains.com/plugins/alpha/5047 有的时候 plugins内搜不到东西 把这个 ...
- 我靠!Semaphore里面居然有这么一个大坑!
这是why的第 59 篇原创文章 荒腔走板 大家好,我是why哥 ,欢迎来到我连续周更优质原创文章的第 59 篇. 上周写了一篇文章,一不小心戳到了大家的爽点,其中一个转载我文章的大号,阅读量居然突破 ...
- 双下划线开头的attr方法
# class Foo: # x=1 # def __init__(self,y): # self.y=y # # def __getattr__(self, item): # print('执行__ ...
- vue中一些常见的面试题
前言 一位正在学习前端的菜鸟,虽菜,但还未放弃. 内容 1,说一下vue中的指令 答: ①,v-html:主要用来渲染html节点,其作用与原生的innerHtml基本一致 ②,v-text:主要用来 ...
- Qt数据库 QSqlTableModel实例操作(转)
本文介绍的是Qt数据库 QSqlTableModel实例操作,详细操作请先来看内容.与上篇内容衔接着,不顾本文也有关于上篇内容的链接. Qt数据库 QSqlTableModel实例操作是本文所介绍的内 ...