openstack关于libvirt相关代码的分析(一)

本文主要探讨OpenStack结合Xen环境下,通过分析 nova/virt/libvirt/connection.py 文件,理解Spawn instance过程,特别是如何创建libvirt XML来初始化实例,重点关注to_xml和get_guest_config函数的作用。

这段时间研究openstack+xen可惜资料太少,决定看一下源代码得了。

如果看过一篇关于启动Openstack中启动一个虚拟机的过程的文章的话,就一定知道有一个叫做Spawn instance的过程。

我使用的代码是nova-2012.2的版本。

下面我们看一下nova/virt/libvirt/connection.py文件。

"""
A connection to a hypervisor through libvirt.

Supports KVM, LXC, QEMU, UML, and XEN.

**Related Flags**

:libvirt_type:  Libvirt domain type.  Can be kvm, qemu, uml, xen
                (default: kvm).
:libvirt_uri:  Override for the default libvirt URI (depends on libvirt_type).
:libvirt_disk_prefix:  Override the default disk prefix for the devices
                       attached to a server.
:rescue_image_id:  Rescue ami image (None = original image).
:rescue_kernel_id:  Rescue aki image (None = original image).
:rescue_ramdisk_id:  Rescue ari image (None = original image).
:injected_network_template:  Template file for injected network
:allow_same_net_traffic:  Whether to allow in project network traffic

我们需要创建以个libvirt xml去新建一个实例。这将会涉及到其中的to_xml函数。

   def to_xml(self, instance, network_info, image_meta=None, rescue=False,
               block_device_info=None):
        LOG.debug(_('Starting toXML method'), instance=instance)
        conf = self.get_guest_config(instance, network_info, image_meta,
                                     rescue, block_device_info)
        xml = conf.to_xml()
        LOG.debug(_('Finished toXML method'), instance=instance)
        return xml

可以看到,它调用了一个叫get_guest_config的函数

    def get_guest_config(self, instance, network_info, image_meta, rescue,
                         block_device_info=None):
        block_device_mapping = driver.block_device_info_get_mapping(
            block_device_info)

        devs = []

        # FIXME(vish): stick this in db
        inst_type_id = instance['instance_type_id']
        inst_type = instance_types.get_instance_type(inst_type_id)

        guest = config.LibvirtConfigGuest()
        guest.virt_type = FLAGS.libvirt_type
        guest.name = instance['name']
        guest.uuid = instance['uuid']
        guest.memory = inst_type['memory_mb'] * 1024
        guest.vcpus = inst_type['vcpus']

        root_device_name = driver.block_device_info_get_root(block_device_info)
        if root_device_name:
            root_device = block_device.strip_dev(root_device_name)
        else:
            # NOTE(yamahata):
            # for nova.api.ec2.cloud.CloudController.get_metadata()
            root_device = self.default_root_device
            db.instance_update(
                nova_context.get_admin_context(), instance['id'],
                {'root_device_name': '/dev/' + self.default_root_device})

        if FLAGS.libvirt_type == "lxc":
            guest.os_type = "exe"
            guest.os_init_path = "/sbin/init"
            guest.os_cmdline = "c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值