导读:
该文件位于nova/virt/libvirt目录下的connection.py!我只是浅浅的分析了一下类中函数的方法 细节并没有多看,肯定有很多地方是错的 或者不好!希望大家能够帮忙指出错误!
接下来 看源代码如下:中文部分是我加的注释 !或许大家会问 为什么要看这个connection.py呢 因为我发现该文件外部virt目录下有个connection.py 其中引用了 这个文件 所以觉得这个应该很重要 而且发现 好多方法都是重写的底层的driver的方法
- import hashlib
- import functools
- import multiprocessing
- import netaddr
- import os
- import random
- import re
- import shutil
- import sys
- import tempfile
- import time
- import uuid
- from xml.dom import minidom
- from xml.etree import ElementTree
-
- from eventlet import greenthread
- from eventlet import tpool
-
- from nova import block_device
- from nova import context as nova_context
- from nova import db
- from nova import exception
- from nova import flags
- import nova.image
- from nova import log as logging
- from nova import utils
- from nova import vnc
- from nova.auth import manager
- from nova.compute import instance_types
- from nova.compute import power_state
- from nova.virt import disk
- from nova.virt import driver
- from nova.virt import images
- from nova.virt.libvirt import netutils
-
-
- libvirt = None
- libxml2 = None
- Template = None
-
-
- LOG = logging.getLogger('nova.virt.libvirt_conn')
-
-
- FLAGS = flags.FLAGS
- flags.DECLARE('live_migration_retry_count', 'nova.compute.manager')
- # TODO(vish): These flags should probably go into a shared location
- #这些标志应该进入共享位置
- flags.DEFINE_string('rescue_image_id', None, 'Rescue ami image')
- flags.DEFINE_string('rescue_kernel_id', None, 'Rescue aki image')
- flags.DEFINE_string('rescue_ramdisk_id', None, 'Rescue ari image')
- flags.DEFINE_string('libvirt_xml_template',
- utils.abspath('virt/libvirt.xml.template'),
- 'Libvirt XML Template')
- flags.DEFINE_string('libvirt_type',
- 'kvm',
- 'Libvirt domain type (valid options are: '
- 'kvm, lxc, qemu, uml, xen)')
- flags.DEFINE_string('libvirt_uri',
- '',
- 'Override the default libvirt URI (which is dependent'
- ' on libvirt_type)')
- flags.DEFINE_bool('allow_same_net_traffic',
- True,
- 'Whether to allow network traffic from same network')
- flags.DEFINE_bool('use_cow_images',
- True,
- 'Whether to use cow images')
- flags.DEFINE_string('ajaxterm_portrange',
- '10000-12000',
- 'Range of ports that ajaxterm should randomly try to bind')
- flags.DEFINE_string('firewall_driver',
- 'nova.virt.libvirt.firewall.IptablesFirewallDriver',
- 'Firewall driver (defaults to iptables)')
- flags.DEFINE_string('cpuinfo_xml_template',
- utils.abspath('virt/cpuinfo.xml.template'),
- 'CpuInfo XML Template (Used only live migration now)')
- flags.DEFINE_string('live_migration_uri',
- "qemu+tcp://%s/system",
- 'Define protocol used by live_migration feature')
- flags.DEFINE_string('live_migration_flag',
- "VIR_MIGRATE_UNDEFINE_SOURCE, VIR_MIGRATE_PEER2PEER",
- 'Define live migration behavior.')
- flags.DEFINE_string('block_migration_flag',
- "VIR_MIGRATE_UNDEFINE_SOURCE, VIR_MIGRATE_PEER2PEER, "
- "VIR_MIGRATE_NON_SHARED_INC",
- 'Define block migration behavior.')
- flags.DEFINE_integer('live_migration_bandwidth', 0,
- 'Define live migration behavior')
- flags.DEFINE_string('snapshot_image_format', None,
- 'Snapshot image format (valid options are : '
- 'raw, qcow2, vmdk, vdi).'
- 'Defaults to same as source image')
- flags.DEFINE_string('libvirt_vif_type', 'bridge',
- 'Type of VIF to create.')
- flags.DEFINE_string('libvirt_vif_driver',
- 'nova.virt.libvirt.vif.LibvirtBridgeDriver',
- 'The libvirt VIF driver to configure the VIFs.')
- flags.DEFINE_string('default_local_format',
- None,
- 'The default format a local_volume will be formatted with '
- 'on creation.')
- flags.DEFINE_bool('libvirt_use_virtio_for_bridges',
- False,
- 'Use virtio for bridge interfaces')
-
- #get_connection 获得与hypervisor(管理程序)的连接
- def get_connection(read_only):
- # These are loaded late so that there's no need to install these
- # libraries when not using libvirt.
- # Cheetah is separate because the unit tests want to load Cheetah,
- # but not libvirt.
- global libvirt
- global libxml2
- if libvirt is None:
- libvirt = __import__('libvirt')
- if libxml2 is None:
- libxml2 = __import__('libxml2')
- _late_load_cheetah() #实际上是延迟引入模板
- return LibvirtConnection(read_only)
-
-
- def _late_load_cheetah():
- global Template
- if Template is None:
- t = __import__('Cheetah.Template', globals(), locals(),
- ['Template'], -1)
- Template = t.Template
-
-
- def _get_eph_disk(ephemeral):
- return 'disk.eph' + str(ephemeral['num'])
-
-
- class LibvirtConnection(driver.ComputeDriver):
- #这个类 LibvirtConnection是继承的drive.ComputerDriver
-
-
- def __init__(self, read_only):
- super(LibvirtConnection, self).__init__() #父类的初始化
- self.libvirt_uri = self.get_uri()
- #2、获得链接
- """
- self.libvirt_uri = self.get_uri()
-
- get_uri()函数如下定义。
-
- def get_uri(self):
- if FLAGS.libvirt_type == 'uml':
- uri = FLAGS.libvirt_uri or 'uml:///system'
- elif FLAGS.libvirt_type == 'xen':
- uri = FLAGS.libvirt_uri or 'xen:///'
- elif FLAGS.libvirt_type == 'lxc':
- uri = FLAGS.libvirt_uri or 'lxc:///'
- else:
- uri = FLAGS.libvirt_uri or 'qemu:///system'
- return uri
- """
-
- #设置模板
- self.libvirt_xml = open(FLAGS.libvirt_xml_template).read()
- self.cpuinfo_xml = open(FLAGS.cpuinfo_xml_template).read()
- self._wrapped_conn = None
- self.read_only = read_only
- #设置firewall
- fw_class = utils.import_class(FLAGS.firewall_driver) #获得类名
- self.firewall_driver = fw_class(get_connection=self._get_connection)
- #获得链接调用—get_connection
- """函数如下
- def _get_connection(self):
- if not self._wrapped_conn or not self._test_connection():
- LOG.debug(_('Connecting to libvirt: %s'), self.libvirt_uri)
- self._wrapped_conn = self._connect(self.libvirt_uri,
- self.read_only)
- #此处是_connect函数 这个函数是从底层libvirt库中拿到链接
- #def _connect(self, uri, read_only): auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],'root',None]
-
- # if read_only:
- # return libvirt.openReadOnly(uri)
- # else:
- # return libvirt.openAuth(uri, auth, 0)
-
- return self._wrapped_conn
- """
- self.vif_driver = utils.import_object(FLAGS.libvirt_vif_driver)
-
- def init_host(self, host):
- # NOTE(nsokolov): moved instance restarting to ComputeManager
- pass
-
- def _get_connection(self):
- #获得链接
- if not self._wrapped_conn or not self._test_connection():
- LOG.debug(_('Connecting to libvirt: %s'), self.libvirt_uri)
- self._wrapped_conn = self._connect(self.libvirt_uri,
- self.read_only)
- return self._wrapped_conn
- _conn = property(_get_connection)
-
- def _test_connection(self):
- #测试链接
- try:
- self._wrapped_conn.getCapabilities()
- return True
- except libvirt.libvirtError as e:
- if e.get_error_code() == libvirt.VIR_ERR_SYSTEM_ERROR and \
- e.get_error_domain() == libvirt.VIR_FROM_REMOTE:
- LOG.debug(_('Connection to libvirt broke'))
- return False
- raise
-
- def get_uri(self):
- #获得url 提供被调用
- if FLAGS.libvirt_type == 'uml':
- uri = FLAGS.libvirt_uri or 'uml:///system'
- elif FLAGS.libvirt_type == 'xen':
- uri = FLAGS.libvirt_uri or 'xen:///'
- elif FLAGS.libvirt_type == 'lxc':
- uri = FLAGS.libvirt_uri or 'lxc:///'
- else:
- uri = FLAGS.libvirt_uri or 'qemu:///system'
- return uri
-
- def _connect(self, uri, read_only):
- #链接
- auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
- 'root',
- None]
-
- if read_only:
- return libvirt.openReadOnly(uri)
- else:
- return libvirt.openAuth(uri, auth, 0)
-
- def list_instances(self):
- return [self._conn.lookupByID(x).name()
- for x in self._conn.listDomainsID()]
-
- def _map_to_instance_info(self, domain):
- #从一个virsh(虚拟)域对象到一个 InstanceInfO得到信息
- """Gets info from a virsh domain object into an InstanceInfo"""
-
- # domain.info() returns a list of:
- # state: one of the state values (virDomainState)
- # maxMemory: the maximum memory used by the domain
- # memory: the current amount of memory used by the domain
- # nbVirtCPU: the number of virtual CPU
- # puTime: the time used by the domain in nanoseconds
-
- (state, _max_mem, _mem, _num_cpu, _cpu_time) = domain.info()
- name = domain.name()
-
- return driver.InstanceInfo(name, state)
-
- def list_instances_detail(self):
- #返回一个信息列表
- infos = []
- for domain_id in self._conn.listDomainsID():
- domain = self._conn.lookupByID(domain_id)
- info = self._map_to_instance_info(domain)
- infos.append(info)
- return infos
-
- def plug_vifs(self, instance, network_info):
- #接通VIFs(虚拟接口) 的网络
-
- """Plugin VIFs into networks."""
- for (network, mapping) in network_info:
- self.vif_driver.plug(instance, network, mapping)
-
- def destroy(self, instance, network_info, cleanup=True):
- #Destroy (shutdown and delete) the specified instance.
-
- instance_name = instance['name']
-
- try:
- virt_dom = self._lookup_by_name(instance_name)
- except exception.NotFound:
- virt_dom = None
-
- # If the instance is already terminated, we're still happy
- # Otherwise, destroy it
- if virt_dom is not None:
- try:
- virt_dom.destroy()
- except libvirt.libvirtError as e:
- is_okay = False
- errcode = e.get_error_code()
- if errcode == libvirt.VIR_ERR_OPERATION_INVALID:
- # If the instance if already shut off, we get this:
- # Code=55 Error=Requested operation is not valid:
- # domain is not running
- (state, _max_mem, _mem, _cpus, _t) = virt_dom.info()
- if state == power_state.SHUTOFF:
- is_okay = True
-
- if not is_okay:
- LOG.warning(_("Error from libvirt during destroy of "
- "%(instance_name)s. Code=%(errcode)s "
- "Error=%(e)s") %
- locals())
- raise
-
- try:
- # NOTE(justinsb): We remove the domain definition. We probably
- # would do better to keep it if cleanup=False (e.g. volumes?)
- # (e.g. #2 - not losing machines on failure)
- virt_dom.undefine()
- except libvirt.libvirtError as e:
- errcode = e.get_error_code()
- LOG.warning(_("Error from libvirt during undefine of "
- "%(instance_name)s. Code=%(errcode)s "
- "Error=%(e)s") %
- locals())
- raise
-
- for (network, mapping) in network_info:
- self.vif_driver.unplug(instance, network, mapping)
-
- def _wait_for_destroy():
- """Called at an interval until the VM is gone."""
- instance_name = instance['name']
-
- try:
- state = self.get_info(instance_name)['state']
- except exception.NotFound:
- msg = _("Instance %s destroyed successfully.") % instance_name
- LOG.info(msg)
- raise utils.LoopingCallDone
-
- timer = utils.LoopingCall(_wait_for_destroy)
- timer.start(interval=0.5, now=True)
-
- self.firewall_driver.unfilter_instance(instance,
- network_info=network_info)
-
- if cleanup:
- self._cleanup(instance)
-
- return True
-
- def _cleanup(self, instance):
- target = os.path.join(FLAGS.instances_path, instance['name'])
- instance_name = instance['name']
- LOG.info(_('instance %(instance_name)s: deleting instance files'
- ' %(target)s') % locals())
- if FLAGS.libvirt_type == 'lxc':
- disk.destroy_container(target, instance, nbd=FLAGS.use_cow_images)
- if os.path.exists(target):
- shutil.rmtree(target)
-
- @exception.wrap_exception()
- def attach_volume(self, instance_name, device_path, mountpoint):
- #在挂载点设置实例磁盘的路径
- virt_dom = self._lookup_by_name(instance_name)
- mount_device = mountpoint.rpartition("/")[2]
- (type, protocol, name) = \
- self._get_volume_device_info(device_path)
- if type == 'block':
- xml = """<disk type='block'>
- <driver name='qemu' type='raw'/>
- <source dev='%s'/>
- <target dev='%s' bus='virtio'/>
- </disk>""" % (device_path, mount_device)
- elif type == 'network':
- xml = """<disk type='network'>
- <driver name='qemu' type='raw'/>
- <source protocol='%s' name='%s'/>
- <target dev='%s' bus='virtio'/>
- </disk>""" % (protocol, name, mount_device)
- virt_dom.attachDevice(xml)
-
- def _get_disk_xml(self, xml, device):
- """Returns the xml for the disk mounted at device"""
- #返回磁盘在设备上磁盘安装的xml
- try:
- doc = libxml2.parseDoc(xml)
- except Exception:
- return None
- ctx = doc.xpathNewContext()
- try:
- ret = ctx.xpathEval('/domain/devices/disk')
- for node in ret:
- for child in node.children:
- if child.name == 'target':
- if child.prop('dev') == device:
- return str(node)
- finally:
- if ctx is not None:
- ctx.xpathFreeContext()
- if doc is not None:
- doc.freeDoc()
-
- @exception.wrap_exception()
- def detach_volume(self, instance_name, mountpoint):
- virt_dom = self._lookup_by_name(instance_name)
- mount_device = mountpoint.rpartition("/")[2]
- xml = self._get_disk_xml(virt_dom.XMLDesc(0), mount_device)
- if not xml:
- raise exception.DiskNotFound(location=mount_device)
- virt_dom.detachDevice(xml)
-
- @exception.wrap_exception()
- def snapshot(self, context, instance, image_href):
- """Create snapshot from a running VM instance.
-
- This command only works with qemu 0.14+
- """
- virt_dom = self._lookup_by_name(instance['name'])
-
- (image_service, image_id) = nova.image.get_image_service(
- context, instance['image_ref'])
- base = image_service.show(context, image_id)
- (snapshot_image_service, snapshot_image_id) = \
- nova.image.get_image_service(context, image_href)
- snapshot = snapshot_image_service.show(context, snapshot_image_id)
-
- metadata = {'is_public': False,
- 'status': 'active',
- 'name': snapshot['name'],
- 'properties': {
- 'kernel_id': instance['kernel_id'],
- 'image_location': 'snapshot',
- 'image_state': 'available',
- 'owner_id': instance['project_id'],
- 'ramdisk_id': instance['ramdisk_id'],
- }
- }
- if 'architecture' in base['properties']:
- arch = base['properties']['architecture']
- metadata['properties']['architecture'] = arch
-
- source_format = base.get('disk_format') or 'raw'
- image_format = FLAGS.snapshot_image_format or source_format
- if FLAGS.use_cow_images:
- source_format = 'qcow2'
- metadata['disk_format'] = image_format
-
- if 'container_format' in base:
- metadata['container_format'] = base['container_format']
-
- # Make the snapshot
- snapshot_name = uuid.uuid4().hex
- snapshot_xml = """
- <domainsnapshot>
- <name>%s</name>
- </domainsnapshot>
- """ % snapshot_name
- snapshot_ptr = virt_dom.snapshotCreateXML(snapshot_xml, 0)
-
- # Find the disk
- xml_desc = virt_dom.XMLDesc(0)
- domain = ElementTree.fromstring(xml_desc)
- source = domain.find('devices/disk/source')
- disk_path = source.get('file')
-
- # Export the snapshot to a raw image
- temp_dir = tempfile.mkdtemp()
- out_path = os.path.join(temp_dir, snapshot_name)
- qemu_img_cmd = ('qemu-img',
- 'convert',
- '-f',
- source_format,
- '-O',
- image_format,
- '-s',
- snapshot_name,
- disk_path,
- out_path)
- utils.execute(*qemu_img_cmd)
-
- # Upload that image to the image service
- with open(out_path) as image_file:
- image_service.update(context,
- image_href,
- metadata,
- image_file)
-
- # Clean up
- shutil.rmtree(temp_dir)
- snapshot_ptr.delete(0)
-
- @exception.wrap_exception()
- def reboot(self, instance, network_info, xml=None):
- """Reboot a virtual machine, given an instance reference.
-
- This method actually destroys and re-creates the domain to ensure the
- reboot happens, as the guest OS cannot ignore this action.
-
- """
- virt_dom = self._conn.lookupByName(instance['name'])
- # NOTE(itoumsn): Use XML delived from the running instance
- # instead of using to_xml(instance, network_info). This is almost
- # the ultimate stupid workaround.
- if not xml:
- xml = virt_dom.XMLDesc(0)
-
- # NOTE(itoumsn): self.shutdown() and wait instead of self.destroy() is
- # better because we cannot ensure flushing dirty buffers
- # in the guest OS. But, in case of KVM, shutdown() does not work...
- self.destroy(instance, network_info, cleanup=False)
- self.plug_vifs(instance, network_info)
- self.firewall_driver.setup_basic_filtering(instance, network_info)
- self.firewall_driver.prepare_instance_filter(instance, network_info)
- self._create_new_domain(xml)
- self.firewall_driver.apply_instance_filter(instance, network_info)
-
- def _wait_for_reboot():
- """Called at an interval until the VM is running again."""
- instance_name = instance['name']
-
- try:
- state = self.get_info(instance_name)['state']
- except exception.NotFound:
- msg = _("During reboot, %s disappeared.") % instance_name
- LOG.error(msg)
- raise utils.LoopingCallDone
-
- if state == power_state.RUNNING:
- msg = _("Instance %s rebooted successfully.") % instance_name
- LOG.info(msg)
- raise utils.LoopingCallDone
-
- timer = utils.LoopingCall(_wait_for_reboot)
- return timer.start(interval=0.5, now=True)
-
- @exception.wrap_exception()
- def pause(self, instance, callback):
- """Pause VM instance"""
- dom = self._lookup_by_name(instance.name)
- dom.suspend()
-
- @exception.wrap_exception()
- def unpause(self, instance, callback):
- """Unpause paused VM instance"""
- dom = self._lookup_by_name(instance.name)
- dom.resume()
-
- @exception.wrap_exception()
- def suspend(self, instance, callback):
- """Suspend the specified instance"""
- dom = self._lookup_by_name(instance.name)
- dom.managedSave(0)
-
- @exception.wrap_exception()
- def resume(self, instance, callback):
- """resume the specified instance"""
- dom = self._lookup_by_name(instance.name)
- dom.create()
-
- @exception.wrap_exception()
复制代码
该文件位于nova/virt/libvirt目录下的connection.py!我只是浅浅的分析了一下类中函数的方法 细节并没有多看,肯定有很多地方是错的 或者不好!希望大家能够帮忙指出错误!
接下来 看源代码如下:中文部分是我加的注释 !或许大家会问 为什么要看这个connection.py呢 因为我发现该文件外部virt目录下有个connection.py 其中引用了 这个文件 所以觉得这个应该很重要 而且发现 好多方法都是重写的底层的driver的方法
- def rescue(self, context, instance, callback, network_info):
- #使用恢复镜像 加载一个vM
- """Loads a VM using rescue images.
-
- A rescue is normally performed when something goes wrong with the
- primary images and data needs to be corrected/recovered. Rescuing
- should not edit or over-ride the original image, only allow for
- data recovery.
-
- """
-
- virt_dom = self._conn.lookupByName(instance['name'])
- unrescue_xml = virt_dom.XMLDesc(0)
- unrescue_xml_path = os.path.join(FLAGS.instances_path,
- instance['name'],
- 'unrescue.xml')
- f = open(unrescue_xml_path, 'w')
- f.write(unrescue_xml)
- f.close()
-
- xml = self.to_xml(instance, network_info, rescue=True)
- rescue_images = {
- 'image_id': FLAGS.rescue_image_id or instance['image_ref'],
- 'kernel_id': FLAGS.rescue_kernel_id or instance['kernel_id'],
- 'ramdisk_id': FLAGS.rescue_ramdisk_id or instance['ramdisk_id'],
- }
- self._create_image(context, instance, xml, '.rescue', rescue_images,
- network_info=network_info)
- self.reboot(instance, network_info, xml=xml)
-
- @exception.wrap_exception()
- def unrescue(self, instance, callback, network_info):
- """Reboot the VM which is being rescued back into primary images.
-
- Because reboot destroys and re-creates instances, unresue should
- simply call reboot.
-
- """
- unrescue_xml_path = os.path.join(FLAGS.instances_path,
- instance['name'],
- 'unrescue.xml')
- f = open(unrescue_xml_path)
- unrescue_xml = f.read()
- f.close()
- os.remove(unrescue_xml_path)
- self.reboot(instance, network_info, xml=unrescue_xml)
-
- @exception.wrap_exception()
- def poll_rescued_instances(self, timeout):
- #轮询已经恢复的实例
- pass
-
- # NOTE(ilyaalekseyev): Implementation like in multinics
- # for xenapi(tr3buchet)
- @exception.wrap_exception()
- def spawn(self, context, instance, network_info,
- block_device_info=None):
- #感觉像是防火墙的过滤
- xml = self.to_xml(instance, network_info, False,
- block_device_info=block_device_info)
- self.firewall_driver.setup_basic_filtering(instance, network_info)
- self.firewall_driver.prepare_instance_filter(instance, network_info)
- self._create_image(context, instance, xml, network_info=network_info,
- block_device_info=block_device_info)
-
- domain = self._create_new_domain(xml)
- LOG.debug(_("instance %s: is running"), instance['name'])
- self.firewall_driver.apply_instance_filter(instance, network_info)
-
- def _wait_for_boot():
- """Called at an interval until the VM is running."""
- instance_name = instance['name']
-
- try:
- state = self.get_info(instance_name)['state']
- except exception.NotFound:
- msg = _("During reboot, %s disappeared.") % instance_name
- LOG.error(msg)
- raise utils.LoopingCallDone
-
- if state == power_state.RUNNING:
- msg = _("Instance %s spawned successfully.") % instance_name
- LOG.info(msg)
- raise utils.LoopingCallDone
-
- timer = utils.LoopingCall(_wait_for_boot)
- return timer.start(interval=0.5, now=True)
-
- def _flush_xen_console(self, virsh_output):
- #清除??
- LOG.info(_('virsh said: %r'), virsh_output)
- virsh_output = virsh_output[0].strip()
-
- if virsh_output.startswith('/dev/'):
- LOG.info(_("cool, it's a device"))
- out, err = utils.execute('dd',
- "if=%s" % virsh_output,
- 'iflag=nonblock',
- run_as_root=True,
- check_exit_code=False)
- return out
- else:
- return ''
-
- def _append_to_file(self, data, fpath):
- #写入fp中 返回fpath
- LOG.info(_('data: %(data)r, fpath: %(fpath)r') % locals())
- fp = open(fpath, 'a+')
- fp.write(data)
- return fpath
-
- def _dump_file(self, fpath):
- #返回读出来的contents
- fp = open(fpath, 'r+')
- contents = fp.read()
- LOG.info(_('Contents of file %(fpath)s: %(contents)r') % locals())
- return contents
-
- @exception.wrap_exception()
- def get_console_output(self, instance):
- #得到控制台的outPut 最后返回的是fpath里面的contents
- console_log = os.path.join(FLAGS.instances_path, instance['name'],
- 'console.log')
-
- utils.execute('chown', os.getuid(), console_log, run_as_root=True)
-
- if FLAGS.libvirt_type == 'xen':
- # Xen is special
- virsh_output = utils.execute('virsh', 'ttyconsole',
- instance['name'])
- data = self._flush_xen_console(virsh_output)
- fpath = self._append_to_file(data, console_log)
- elif FLAGS.libvirt_type == 'lxc':
- # LXC is also special
- LOG.info(_("Unable to read LXC console"))
- else:
- fpath = console_log
-
- return self._dump_file(fpath)
-
- @exception.wrap_exception()
- def get_ajax_console(self, instance):
- def get_open_port():
- start_port, end_port = FLAGS.ajaxterm_portrange.split("-")
- for i in xrange(0, 100): # don't loop forever
- port = random.randint(int(start_port), int(end_port))
- # netcat will exit with 0 only if the port is in use,
- # so a nonzero return value implies it is unused
- cmd = 'netcat', '0.0.0.0', port, '-w', '1'
- try:
- stdout, stderr = utils.execute(*cmd, process_input='')
- except exception.ProcessExecutionError:
- return port
- raise Exception(_('Unable to find an open port'))
-
- def get_pty_for_instance(instance_name):
- virt_dom = self._lookup_by_name(instance_name)
- xml = virt_dom.XMLDesc(0)
- dom = minidom.parseString(xml)
-
- for serial in dom.getElementsByTagName('serial'):
- if serial.getAttribute('type') == 'pty':
- source = serial.getElementsByTagName('source')[0]
- return source.getAttribute('path')
-
- port = get_open_port()
- token = str(uuid.uuid4())
- host = instance['host']
-
- ajaxterm_cmd = 'sudo socat - %s' \
- % get_pty_for_instance(instance['name'])
-
- cmd = ['%s/tools/ajaxterm/ajaxterm.py' % utils.novadir(),
- '--command', ajaxterm_cmd, '-t', token, '-p', port]
-
- utils.execute(cmd)
- return {'token': token, 'host': host, 'port': port}
-
- def get_host_ip_addr(self):
- # Retrieves the IP address of the dom0得到主机ip地址
- return FLAGS.my_ip
-
- @exception.wrap_exception()
- def get_vnc_console(self, instance):
- #取得虚拟网络计算机的控制 最后返回 token,host,port
- def get_vnc_port_for_instance(instance_name):
- virt_dom = self._lookup_by_name(instance_name)
- xml = virt_dom.XMLDesc(0)
- # TODO: use etree instead of minidom
- dom = minidom.parseString(xml)
-
- for graphic in dom.getElementsByTagName('graphics'):
- if graphic.getAttribute('type') == 'vnc':
- return graphic.getAttribute('port')
-
- port = get_vnc_port_for_instance(instance['name'])
- token = str(uuid.uuid4())
- host = instance['host']
-
- return {'token': token, 'host': host, 'port': port}
-
- @staticmethod
- def _cache_image(fn, target, fname, cow=False, *args, **kwargs):
- #封装一个方法是他能够创建一个可以缓存镜像的镜像
- """Wrapper for a method that creates an image that caches the image.
-
- This wrapper will save the image into a common store and create a
- copy for use by the hypervisor.
-
- The underlying method should specify a kwarg of target representing
- where the image will be saved.
-
- fname is used as the filename of the base image. The filename needs
- to be unique to a given image.
-
- If cow is True, it will make a CoW image instead of a copy.
- """
-
- if not os.path.exists(target):
- base_dir = os.path.join(FLAGS.instances_path, '_base')
- if not os.path.exists(base_dir):
- os.mkdir(base_dir)
- base = os.path.join(base_dir, fname)
-
- @utils.synchronized(fname)
- def call_if_not_exists(base, fn, *args, **kwargs):
- if not os.path.exists(base):
- fn(target=base, *args, **kwargs)
-
- call_if_not_exists(base, fn, *args, **kwargs)
-
- if cow:
- utils.execute('qemu-img', 'create', '-f', 'qcow2', '-o',
- 'cluster_size=2M,backing_file=%s' % base,
- target)
- else:
- utils.execute('cp', base, target)
-
- def _fetch_image(self, context, target, image_id, user_id, project_id,
- size=None):
- """Grab image and optionally attempt to resize it"""
- #GRAB 镜像和可选地试图调整他
- images.fetch_to_raw(context, image_id, target, user_id, project_id)
- if size:
- disk.extend(target, size)
-
- def _create_local(self, target, local_size, unit='G', fs_format=None):
- """Create a blank image of specified size"""
- #创建一个空的指定size的镜像
-
- if not fs_format:
- fs_format = FLAGS.default_local_format
-
- utils.execute('truncate', target, '-s', "%d%c" % (local_size, unit))
- if fs_format:
- utils.execute('mkfs', '-t', fs_format, target)
-
- def _create_ephemeral(self, target, local_size, fs_label, os_type):
- self._create_local(target, local_size)
- disk.mkfs(os_type, fs_label, target)
-
- def _create_swap(self, target, swap_mb):
- """Create a swap file of specified size"""
- #创建一个指定size的swap file
- self._create_local(target, swap_mb, unit='M')
- utils.execute('mkswap', target)
-
- def _create_image(self, context, inst, libvirt_xml, suffix='',
- disk_images=None, network_info=None,
- block_device_info=None):
- if not suffix:
- suffix = ''
-
- # syntactic nicety
- def basepath(fname='', suffix=suffix):
- return os.path.join(FLAGS.instances_path,
- inst['name'],
- fname + suffix)
-
- # ensure directories exist and are writable
- utils.execute('mkdir', '-p', basepath(suffix=''))
-
- LOG.info(_('instance %s: Creating image'), inst['name'])
- f = open(basepath('libvirt.xml'), 'w')
- f.write(libvirt_xml)
- f.close()
-
- if FLAGS.libvirt_type == 'lxc':
- container_dir = '%s/rootfs' % basepath(suffix='')
- utils.execute('mkdir', '-p', container_dir)
-
- # NOTE(vish): No need add the suffix to console.log
- console_log = basepath('console.log', '')
- if os.path.exists(console_log):
- utils.execute('chown', os.getuid(), console_log, run_as_root=True)
- os.close(os.open(console_log, os.O_CREAT | os.O_WRONLY, 0660))
-
- if not disk_images:
- disk_images = {'image_id': inst['image_ref'],
- 'kernel_id': inst['kernel_id'],
- 'ramdisk_id': inst['ramdisk_id']}
-
- if disk_images['kernel_id']:
- fname = '%08x' % int(disk_images['kernel_id'])
- self._cache_image(fn=self._fetch_image,
- context=context,
- target=basepath('kernel'),
- fname=fname,
- image_id=disk_images['kernel_id'],
- user_id=inst['user_id'],
- project_id=inst['project_id'])
- if disk_images['ramdisk_id']:
- fname = '%08x' % int(disk_images['ramdisk_id'])
- self._cache_image(fn=self._fetch_image,
- context=context,
- target=basepath('ramdisk'),
- fname=fname,
- image_id=disk_images['ramdisk_id'],
- user_id=inst['user_id'],
- project_id=inst['project_id'])
-
- root_fname = hashlib.sha1(disk_images['image_id']).hexdigest()
- size = FLAGS.minimum_root_size
-
- inst_type_id = inst['instance_type_id']
- inst_type = instance_types.get_instance_type(inst_type_id)
- if inst_type['name'] == 'm1.tiny' or suffix == '.rescue':
- size = None
- root_fname += "_sm"
-
- if not self._volume_in_mapping(self.default_root_device,
- block_device_info):
- self._cache_image(fn=self._fetch_image,
- context=context,
- target=basepath('disk'),
- fname=root_fname,
- cow=FLAGS.use_cow_images,
- image_id=disk_images['image_id'],
- user_id=inst['user_id'],
- project_id=inst['project_id'],
- size=size)
-
- local_gb = inst['local_gb']
- if local_gb and not self._volume_in_mapping(
- self.default_local_device, block_device_info):
- fn = functools.partial(self._create_ephemeral,
- fs_label='ephemeral0',
- os_type=inst.os_type)
- self._cache_image(fn=fn,
- target=basepath('disk.local'),
- fname="ephemeral_%s_%s_%s" %
- ("0", local_gb, inst.os_type),
- cow=FLAGS.use_cow_images,
- local_size=local_gb)
-
- for eph in driver.block_device_info_get_ephemerals(block_device_info):
- fn = functools.partial(self._create_ephemeral,
- fs_label='ephemeral%d' % eph['num'],
- os_type=inst.os_type)
- self._cache_image(fn=fn,
- target=basepath(_get_eph_disk(eph)),
- fname="ephemeral_%s_%s_%s" %
- (eph['num'], eph['size'], inst.os_type),
- cow=FLAGS.use_cow_images,
- local_size=eph['size'])
-
- swap_mb = 0
-
- swap = driver.block_device_info_get_swap(block_device_info)
- if driver.swap_is_usable(swap):
- swap_mb = swap['swap_size']
- elif (inst_type['swap'] > 0 and
- not self._volume_in_mapping(self.default_swap_device,
- block_device_info)):
- swap_mb = inst_type['swap']
-
- if swap_mb > 0:
- self._cache_image(fn=self._create_swap,
- target=basepath('disk.swap'),
- fname="swap_%s" % swap_mb,
- cow=FLAGS.use_cow_images,
- swap_mb=swap_mb)
-
- # For now, we assume that if we're not using a kernel, we're using a
- # partitioned disk image where the target partition is the first
- # partition
- target_partition = None
- if not inst['kernel_id']:
- target_partition = "1"
-
- config_drive_id = inst.get('config_drive_id')
- config_drive = inst.get('config_drive')
-
- if any((FLAGS.libvirt_type == 'lxc', config_drive, config_drive_id)):
- target_partition = None
-
- if config_drive_id:
- fname = '%08x' % int(config_drive_id)
- self._cache_image(fn=self._fetch_image,
- target=basepath('disk.config'),
- fname=fname,
- image_id=config_drive_id,
- user_id=inst['user_id'],
- project_id=inst['project_id'],)
- elif config_drive:
- self._create_local(basepath('disk.config'), 64, unit='M',
- fs_format='msdos') # 64MB
-
- if inst['key_data']:
- key = str(inst['key_data'])
- else:
- key = None
- net = None
-
- nets = []
- ifc_template = open(FLAGS.injected_network_template).read()
- ifc_num = -1
- have_injected_networks = False
- admin_context = nova_context.get_admin_context()
- for (network_ref, mapping) in network_info:
- ifc_num += 1
-
- if not network_ref['injected']:
- continue
-
- have_injected_networks = True
- address = mapping['ips'][0]['ip']
- netmask = mapping['ips'][0]['netmask']
- address_v6 = None
- gateway_v6 = None
- netmask_v6 = None
- if FLAGS.use_ipv6:
- address_v6 = mapping['ip6s'][0]['ip']
- netmask_v6 = mapping['ip6s'][0]['netmask']
- gateway_v6 = mapping['gateway6']
- net_info = {'name': 'eth%d' % ifc_num,
- 'address': address,
- 'netmask': netmask,
- 'gateway': mapping['gateway'],
- 'broadcast': mapping['broadcast'],
- 'dns': ' '.join(mapping['dns']),
- 'address_v6': address_v6,
- 'gateway6': gateway_v6,
- 'netmask_v6': netmask_v6}
- nets.append(net_info)
-
- if have_injected_networks:
- net = str(Template(ifc_template,
- searchList=[{'interfaces': nets,
- 'use_ipv6': FLAGS.use_ipv6}]))
-
- metadata = inst.get('metadata')
- if any((key, net, metadata)):
- inst_name = inst['name']
-
- if config_drive: # Should be True or None by now.
- injection_path = basepath('disk.config')
- img_id = 'config-drive'
- tune2fs = False
- else:
- injection_path = basepath('disk')
- img_id = inst.image_ref
- tune2fs = True
-
- for injection in ('metadata', 'key', 'net'):
- if locals()[injection]:
- LOG.info(_('instance %(inst_name)s: injecting '
- '%(injection)s into image %(img_id)s'
- % locals()))
- try:
- disk.inject_data(injection_path, key, net, metadata,
- partition=target_partition,
- nbd=FLAGS.use_cow_images,
- tune2fs=tune2fs)
-
- except Exception as e:
- # This could be a windows image, or a vmdk format disk
- LOG.warn(_('instance %(inst_name)s: ignoring error injecting'
- ' data into image %(img_id)s (%(e)s)') % locals())
-
- if FLAGS.libvirt_type == 'lxc':
- disk.setup_container(basepath('disk'),
- container_dir=container_dir,
- nbd=FLAGS.use_cow_images)
-
- if FLAGS.libvirt_type == 'uml':
- utils.execute('chown', 'root', basepath('disk'), run_as_root=True)
复制代码
相关内容:
openstack nova 源码分析1-setup脚本
http://www.aboutyun.com/thread-10090-1-1.html
openstack nova 源码分析2之nova-api,nova-compute
http://www.aboutyun.com/thread-10091-1-1.html
openstack nova 源码分析3-nova目录下的service.py、driver.py
http://www.aboutyun.com/thread-10092-1-1.html
openstack nova 源码分析4-2 -nova/virt/libvirt目录下的connection.py
http://www.aboutyun.com/thread-10095-1-1.html
出处http://brucemars.blog.51cto.com/5288106/968433 |