作为个人学习笔记分享。有不论什么问题欢迎交流!

近期在Gerrit中看到一个change:https://review.openstack.org/#/c/94295/ , 它主要是对当前在Ceph中创建虚拟机的流程的改进。假设glance的backend是ceph, 则nova创建虚拟机到RBD的流程是这种:

通过glance从ceph中下载image --> 本地 --> 复制image到rbd

这个change的目的就是:不须要下载到本地。直接在rbd中复制image,以提高虚拟机创建的速度。

曾经仅仅知道nova从glance下载image,作为虚拟机的base,却没有细致的了解过这个过程,正好借这个机会。看一看nova创建虚拟机到rbd过程中关于image的部分。

1 nova创建VM时image的流程

经过nova-scheduler选择节点后,创建VM的请求到达了nova-compute。即nova/compute/manager.py:ComputeManager._run_instance():

def _run_instance(self, context, request_spec,
filter_properties, requested_networks, injected_files,
admin_password, is_first_time, node, instance,
legacy_bdm_in_spec):

关于镜像的元数据就保存在request_spec。

image_meta = request_spec['image']

取得元数据后就開始build_instance()了。可是以下的过程与image没太大关系,所以从简带过。

def _build_instance(self, context, request_spec, filter_properties,
requested_networks, injected_files, admin_password, is_first_time,
node, instance, image_meta, legacy_bdm_in_spec)
---->
def _spawn(self, context, instance, image_meta, network_info,
block_device_info, injected_files, admin_password,
set_access_ip=False)
----> self.driver.spawn(context, instance, image_meta,
injected_files, admin_password,
network_info,
block_device_info)

这里的driver就是你用的Hypervioser, 我用的是KVM,所以这个driver.spawn=nova/virt/libvirt/driver.py:LibvirtDriver.spawn():

def spawn(self, context, instance, image_meta, injected_files,
admin_password,network_info=None, block_device_info=None):
disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type,
instance,
block_device_info,
image_meta)
self._create_image(context, instance,
disk_info['mapping'],
network_info=network_info,
block_device_info=block_device_info,
files=injected_files,
admin_pass=admin_password)

那么这个disk_info['mapping']是什么呢?这里有一个方法,我们能够从test_libvirt_blockinfo.py里找到答案,所以结合測试用例来看代码真的非常实用。在Nova/tests/virt/libvirt/test_libvirt_blockinfo.py:

LibvirtBlockInfoTest.test_get_disk_mapping_simple_swap()里能够看到:

      expect = {
'disk': {'bus': 'virtio', 'dev': 'vda',
'type': 'disk', 'boot_index': '1'},
'disk.local': {'bus': 'virtio', 'dev': 'vdb', 'type': 'disk'},
'root': {'bus': 'virtio', 'dev': 'vda',
'type': 'disk', 'boot_index': '1'}
}

Expect就是期望disk_info['mapping']的样子。

以下就開始创建VM的image了:

    def _create_image(self, context, instance,
disk_mapping, suffix='',
disk_images=None, network_info=None,
block_device_info=None, files=None,
admin_pass=None, inject_files=True):
#以下这个函数就是返回VM image格式的相关类,位于 #libvirt/imagebackend.py中,这里image是rbd, 返回的就是Rbd #类。 def image(fname, image_type=CONF.libvirt.images_type):
return self.image_backend.image(instance,
fname + suffix, image_type)
...... if not booted_from_volume:
root_fname = imagecache.get_cache_fname(disk_images, 'image_id')#以image id作为文件名称
size = instance['root_gb'] * units.Gi if size == 0 or suffix == '.rescue':
size = None #这里有点复杂,用到了回调函数fetch_image,这里的cache是Rbd的父类#Image类的cache(),主要功能是从模板,也就是glance的image。 为VM创建一个image.
image('disk').cache(fetch_func=libvirt_utils.fetch_image,
context=context,
filename=root_fname,
size=size,
image_id=disk_images['image_id'],
user_id=instance['user_id'],
project_id=instance['project_id'])
#能够在cache()中看到:
if not self.check_image_exists() or not os.path.exists(base):
self.create_image(fetch_func_sync, base, size,
*args, **kwargs)

那么如今到class Rbd下能够找到create_image():

 def create_image(self, prepare_template, base, size, *args, **kwargs):
if self.rbd is None:
raise RuntimeError(_('rbd python libraries not found')) if not os.path.exists(base):
prepare_template(target=base, max_size=size, *args, **kwargs)##这里的prepare_temple()就是 libvirt_utils.fetch_image啦。

libvirt_utils.fetch_image=nova/virt/libvirt/utils.fetch_image():

def fetch_image(context, target, image_id, user_id, project_id, max_size=0):
"""Grab image."""
images.fetch_to_raw(context, image_id, target, user_id, project_id,
max_size=max_size)
---> def fetch_to_raw(context, image_href, path, user_id, project_id, max_size=0):
path_tmp = "%s.part" % path
fetch(context, image_href, path_tmp, user_id, project_id,
max_size=max_size) ---->
def fetch(context, image_href, path, _user_id, _project_id, max_size=0):
(image_service, image_id) = glance.get_remote_image_service(
context, image_href)#从glance获取image
with fileutils.remove_path_on_error(path):
#这里就是把image_id的数据download到path了。 Download()位于 #nova/image/glance.py。 image_service.download(context, image_id, dst_path=path)

回到class Rbd的create_image()中。

libvirt_utils.import_rbd_image(*args)把path的image数据写入rbd,至此,整个流程就到这里结束了。

2 Change中的改进

如今回到文章開始中提到的那个change, 看看它是怎么实现的。

首先它在fetch_to_raw中增加了一个推断。

def fetch_to_raw(context, image_href, path, user_id, project_id,  max_size=0):
#推断backend是否具有‘direct_fetch’的属性,假设有,则直接返回#direct_fetch()
if backend and hasattr(backend, 'direct_fetch'):
try:
return backend.direct_fetch(context, image_href)
except exception.ImageUnacceptable:
LOG.debug(_('could not fetch directly, falling back to download'))

给Rbd类加入了一个属性:

    def direct_fetch(self, context, image_href):
#推断driver是否支持layering(分层
#http://ceph.com/docs/firefly/dev/rbd-layering/ ,指的是块设备
#的cow克隆。支持高速创建image)
if not self.driver.supports_layering():
reason = _('installed version of librbd does not support cloning')
raise exception.ImageUnacceptable(image_id=image_href,
reason=reason) image_meta, locations = images.get_meta(context, image_href)
LOG.debug(_('Image locations are: %(locs)s') % {'locs': locations}) if image_meta.get('disk_format') not in ['raw', 'iso']:
reason = _('Image is not raw format')
raise exception.ImageUnacceptable(image_id=image_href,
reason=reason)
#克隆镜像(http://ceph.com/docs/master/rbd/librbdpy/)
for location in locations:
if self.driver.is_cloneable(location, image_meta):
return self.driver.clone(location, self.rbd_name) reason = _('No image locations are accessible')
raise exception.ImageUnacceptable(image_id=image_href, reason=reason)

这样就不须要想1中的那样先把image下载到local, 在写到rbd中。直接在rbd中克隆,从而提高了虚拟机的创建速度。

3总结

借这个机会既熟悉了创建VM时的image流程。又熟悉了ceph的使用方法国,同时学习大师们是如何实现的功能,看起来review是啊大大受益。:)

版权声明:本文博客原创文章,博客,未经同意,不得转载。

在Ceph创建虚拟机的过程改进分析的更多相关文章

  1. nova创建虚拟机源码系列分析之二 wsgi模型

    openstack nova启动时首先通过命令行或者dashborad填写创建信息,然后通过restful api的方式调用openstack服务去创建虚拟机.数据信息从客户端到达openstack服 ...

  2. Nova创建虚拟机的底层代码分析

    作为个人学习笔记分享.有不论什么问题欢迎交流! 在openstack中创建虚拟机的底层实现是nova使用了libvirt,代码在nova/virt/libvirt/driver.py. #image_ ...

  3. nova创建虚拟机的详细过程

    Nova 创建虚拟机详细过程    

  4. VMware workstation批量创建虚拟机和自动化安装操作系统(二)

    一. 简述 在上一篇<VMware workstation批量创建虚拟机和自动化安装操作系统(一)>中,主要介绍了VMware workstation自定义创建虚拟机的过程,和一些其他的有 ...

  5. 创建虚拟机流程nova

    这篇博文借鉴于http://www.cnblogs.com/yjbjingcha/p/6977741.html,感谢博友提供. 本文试图具体地描写叙述openstack创建虚拟机的完整过程.从用户发起 ...

  6. QEMU-KVM自己主动创建虚拟机,以指定IP构造

    正在使用qemu不能指定创建虚拟机的过程IP住址,然而,在实际应用中,我们需要有一台虚拟机IP住址,不是人为的虚拟机操作系统配置. 于qemu虚拟机技术文档(http://qemu.weilnetz. ...

  7. VMware workstation创建虚拟机console

    1. 使用VMware workstation创建虚拟机硬件2. 安装操作系统3. 操作系统安装过程 1. 使用VMware workstation创建虚拟机硬件 使用VMware workstati ...

  8. VMware 虚拟化技术 创建虚拟机

    原文地址:https://www.linuxidc.com/Linux/2017-03/141972.htm 云最成熟的架构是IaaS(Infrastructure as a Service),其中用 ...

  9. nova创建虚拟机源码分析系列之六 api入口create方法

    openstack 版本:Newton 注:博文图片采用了很多大牛博客图片,仅作为总结学习,非商用.该图全面的说明了nova创建虚机的过程,从逻辑的角度清晰的描述了前端请求创建虚拟机之后发生的一系列反 ...

随机推荐

  1. gem5 设定checkpiont以及从checkpoint开始运行

    同spec2006中间bzip2一个例子,如何设置checkpoint .以及从checkpoint继续以启动运行.这样做的目的是为了,采纳automic运行N指令,然后detailed运行M指令. ...

  2. 从头来之【图解针对虚拟机iOS开发环境搭建】 (转)

    1.下载Mac OSX10.9. 点击下载 2.下载VMware Workstation 10,点击下载,网页中包含序列号.安装VM. 3.VM10-MacOS补丁.用于创建苹果虚拟机. 安装VM就不 ...

  3. php中页面跳转部分方法论述

    假设当前页面:http://localhost:80/index.php 1.include echo “include t1.php”; 跳转后,url地址栏http://localhost:80/ ...

  4. 数字证书及CA的扫盲介绍(转)

    ★ 先说一个通俗的例子 考虑到证书体系的相关知识比较枯燥.晦涩.俺先拿一个通俗的例子来说事儿. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 ...

  5. 用NMAKE创建VS2012 C++工程 HelloWorld

    由于需要精通GDAL的源代码,所以还是有必要精通NMAKE,先来尝试创建一个NMAKE工程. 之前一篇文章Windows7中Emacs 24 shell使用Gitbash已经介绍了如何在Emacs的s ...

  6. HDU 4643 GSM 暑期多校联合训练第五场 1001

    点击打开链接 我就不说官方题解有多坑了 V图那么高端的玩意儿 被精度坑粗翔了 AC前 AC后 简直不敢相信 只能怪自己没注意题目For the distance d1 and d2, if fabs( ...

  7. 在Button上、下、左、右位置加入图片和文字

    转载请注明出处:http://blog.csdn.net/droyon/article/details/37564419 非常多人有如标题所述的需求,并且大多数人採用了自己定义组件攻克了需求,事实上还 ...

  8. 【教你zencart仿站 文章1至6教训 高清1280x900视频下载】[支持手机端]

    [教你zencart仿站 第1至6课 高清晰1280x900视频下载][支持移动端] 经过筹备, 我们的课件最终出来了- 我们 zencart联盟合伙人 项目推出的 在线yy同步演示zencart仿站 ...

  9. Linux内核——定时器和时间管理

    定时器和时间管理 系统定时器是一种可编程硬件芯片.它能以固定频率产生中断.该中断就是所谓的定时器中断.它所相应的中断处理程序负责更新系统时间,还负责执行须要周期性执行的任务. 系统定时器和时钟中断处理 ...

  10. 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...