这段时间研究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