Example #1
0
    def test_config_network_parse(self):
        xml = """<disk type="network" device="disk">
                   <driver name="qemu" type="qcow2"/>
                   <source name="foo.bar.com" protocol="iscsi"/>
                   <target bus="ide" dev="/dev/hda"/>
                 </disk>"""
        xmldoc = etree.fromstring(xml)

        obj = config.LibvirtConfigGuestDisk()
        obj.parse_dom(xmldoc)

        self.assertEqual(obj.source_type, 'network')
        self.assertEqual(obj.source_protocol, 'iscsi')
        self.assertEqual(obj.source_name, 'foo.bar.com')
        self.assertEqual(obj.driver_name, 'qemu')
        self.assertEqual(obj.driver_format, 'qcow2')
        self.assertEqual(obj.target_dev, '/dev/hda')
        self.assertEqual(obj.target_bus, 'ide')
 def _fake_libvirt_info(self,
                        mock_disk,
                        disk_info,
                        cache_mode,
                        extra_specs,
                        hypervisor_version,
                        disk_unit=None):
     # For tests in test_virt_drivers which expect libvirt_info to be
     # functional
     info = config.LibvirtConfigGuestDisk()
     info.source_type = 'file'
     info.source_device = disk_info['type']
     info.target_bus = disk_info['bus']
     info.target_dev = disk_info['dev']
     info.driver_cache = cache_mode
     info.driver_format = 'raw'
     info.source_path = mock_disk.path
     return info
    def test_config_network(self):
        obj = config.LibvirtConfigGuestDisk()
        obj.source_type = "network"
        obj.source_protocol = "iscsi"
        obj.source_host = "foo.bar.com"
        obj.driver_name = "qemu"
        obj.driver_format = "qcow2"
        obj.target_dev = "/dev/hda"
        obj.target_bus = "ide"

        xml = obj.to_xml()
        self.assertXmlEqual(
            xml, """
            <disk type="network" device="disk">
              <driver name="qemu" type="qcow2"/>
              <source name="foo.bar.com" protocol="iscsi"/>
              <target bus="ide" dev="/dev/hda"/>
            </disk>""")
Example #4
0
    def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode,
                     extra_specs, hypervisor_version, boot_order=None,
                     disk_unit=None):
        """Get `LibvirtConfigGuestDisk` filled for this image.

        :disk_dev: Disk bus device name
        :disk_bus: Disk bus type
        :device_type: Device type for this image.
        :cache_mode: Caching mode for this image
        :extra_specs: Instance type extra specs dict.
        :hypervisor_version: the hypervisor version
        :boot_order: Disk device boot order
        """
        info = vconfig.LibvirtConfigGuestDisk()

        hosts, ports = self.driver.get_mon_addrs()
        info.source_device = device_type
        info.driver_format = 'raw'
        info.driver_cache = cache_mode
        info.driver_discard = self.discard_mode
        info.target_bus = disk_bus
        info.target_dev = disk_dev
        info.source_type = 'network'
        info.source_protocol = 'rbd'
        info.source_name = '%s/%s' % (self.pool, self.rbd_name)
        info.source_hosts = hosts
        info.source_ports = ports
        info.boot_order = boot_order
        auth_enabled = (CONF.libvirt.rbd_user is not None)
        if CONF.libvirt.rbd_secret_uuid:
            info.auth_secret_uuid = CONF.libvirt.rbd_secret_uuid
            auth_enabled = True  # Force authentication locally
            if CONF.libvirt.rbd_user:
                info.auth_username = CONF.libvirt.rbd_user
        if auth_enabled:
            info.auth_secret_type = 'ceph'
            info.auth_secret_uuid = CONF.libvirt.rbd_secret_uuid

        if disk_bus == 'scsi':
            self.disk_scsi(info, disk_unit)

        self.disk_qos(info, extra_specs)

        return info
Example #5
0
    def test_config_xen_pv(self):
        obj = config.LibvirtConfigGuest()
        obj.virt_type = "xen"
        obj.memory = 1024 * 1024 * 100
        obj.vcpus = 2
        obj.cpuset = "0-3,^2,4-5"
        obj.name = "demo"
        obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
        obj.os_type = "linux"
        obj.os_kernel = "/tmp/vmlinuz"
        obj.os_initrd = "/tmp/ramdisk"
        obj.os_root = "root=xvda"
        obj.os_cmdline = "console=xvc0"

        disk = config.LibvirtConfigGuestDisk()
        disk.source_type = "file"
        disk.source_path = "/tmp/img"
        disk.target_dev = "/dev/xvda"
        disk.target_bus = "xen"

        obj.add_device(disk)

        xml = obj.to_xml()
        self.assertXmlEqual(
            xml, """
            <domain type="xen">
              <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
              <name>demo</name>
              <memory>104857600</memory>
              <vcpu cpuset="0-3,^2,4-5">2</vcpu>
              <os>
                <type>linux</type>
                <kernel>/tmp/vmlinuz</kernel>
                <initrd>/tmp/ramdisk</initrd>
                <cmdline>console=xvc0</cmdline>
                <root>root=xvda</root>
              </os>
              <devices>
                <disk type="file" device="disk">
                  <source file="/tmp/img"/>
                  <target bus="xen" dev="/dev/xvda"/>
                </disk>
              </devices>
            </domain>""")
Example #6
0
    def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode):
        """Get `LibvirtConfigGuestDisk` filled for this image.

        :disk_dev: Disk bus device name
        :disk_bus: Disk bus type
        :device_type: Device type for this image.
        :cache_mode: Caching mode for this image
        """
        info = vconfig.LibvirtConfigGuestDisk()
        info.source_type = self.source_type
        info.source_device = device_type
        info.target_bus = disk_bus
        info.target_dev = disk_dev
        info.driver_cache = cache_mode
        info.driver_format = self.driver_format
        driver_name = libvirt_utils.pick_disk_driver_name(self.is_block_dev)
        info.driver_name = driver_name
        info.source_path = self.path
        return info
Example #7
0
    def test_config_kvm(self):
        obj = config.LibvirtConfigGuest()
        obj.virt_type = "kvm"
        obj.memory = 1024 * 1024 * 100
        obj.vcpus = 2
        obj.name = "demo"
        obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
        obj.os_type = "linux"
        obj.os_boot_dev = "hd"
        obj.acpi = True
        obj.apic = True

        disk = config.LibvirtConfigGuestDisk()
        disk.source_type = "file"
        disk.source_path = "/tmp/img"
        disk.target_dev = "/dev/vda"
        disk.target_bus = "virtio"

        obj.add_device(disk)

        xml = obj.to_xml()
        self.assertXmlEqual(
            xml, """
            <domain type="kvm">
              <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
              <name>demo</name>
              <memory>104857600</memory>
              <vcpu>2</vcpu>
              <os>
                <type>linux</type>
                <boot dev="hd"/>
              </os>
              <features>
                <acpi/>
                <apic/>
              </features>
              <devices>
                <disk type="file" device="disk">
                  <source file="/tmp/img"/>
                  <target bus="virtio" dev="/dev/vda"/>
                </disk>
              </devices>
            </domain>""")
Example #8
0
    def test_config_xen_hvm(self):
        obj = config.LibvirtConfigGuest()
        obj.virt_type = "xen"
        obj.memory = 1024 * 1024 * 100
        obj.vcpus = 2
        obj.name = "demo"
        obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
        obj.os_type = "hvm"
        obj.os_loader = '/usr/lib/xen/boot/hvmloader'
        obj.os_root = "root=xvda"
        obj.os_cmdline = "console=xvc0"

        disk = config.LibvirtConfigGuestDisk()
        disk.source_type = "file"
        disk.source_path = "/tmp/img"
        disk.target_dev = "/dev/xvda"
        disk.target_bus = "xen"

        obj.add_device(disk)

        xml = obj.to_xml()
        self.assertXmlEqual(
            xml, """
            <domain type="xen">
              <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
              <name>demo</name>
              <memory>104857600</memory>
              <vcpu>2</vcpu>
              <os>
                <type>hvm</type>
                <loader>/usr/lib/xen/boot/hvmloader</loader>
                <cmdline>console=xvc0</cmdline>
                <root>root=xvda</root>
              </os>
              <devices>
                <disk type="file" device="disk">
                  <source file="/tmp/img"/>
                  <target bus="xen" dev="/dev/xvda"/>
                </disk>
              </devices>
            </domain>""")
Example #9
0
 def _generate_cdrom_xml(instance):
     meta = instance.get("metadata", {})
     set_source = False
     if meta.get("__loading_update_driver_image", "") == "enable":
         set_source = True
     source = None
     if CONF.guest_os_driver_path and \
             os.path.exists(CONF.guest_os_driver_path) and set_source:
         source = CONF.guest_os_driver_path
     cdrom = config_original.LibvirtConfigGuestDisk()
     # source_path null
     cdrom.source_device = "cdrom"
     cdrom.driver_name = "qemu"
     cdrom.driver_format = "raw"
     cdrom.driver_cache = "none"
     cdrom.target_dev = "hdb"
     cdrom.target_bus = "ide"
     cdrom.source_path = source or ""
     # io use threads default
     cdrom.readonly = True
     return cdrom.format_dom()
Example #10
0
    def test_config_network_no_name(self):
        obj = config.LibvirtConfigGuestDisk()
        obj.source_type = 'network'
        obj.source_protocol = 'nbd'
        obj.source_hosts = ['foo.bar.com']
        obj.source_ports = [None]
        obj.driver_name = 'qemu'
        obj.driver_format = 'raw'
        obj.target_dev = '/dev/vda'
        obj.target_bus = 'virtio'

        xml = obj.to_xml()
        self.assertXmlEqual(
            xml, """
            <disk type="network" device="disk">
              <driver name="qemu" type="raw"/>
              <source protocol="nbd">
                <host name="foo.bar.com"/>
              </source>
              <target bus="virtio" dev="/dev/vda"/>
            </disk>""")
Example #11
0
    def libvirt_info(self,
                     disk_bus,
                     disk_dev,
                     device_type,
                     cache_mode,
                     extra_specs,
                     hypervisor_version,
                     boot_order=None,
                     disk_unit=None):
        """Get `LibvirtConfigGuestDisk` filled for this image.

        :disk_dev: Disk bus device name
        :disk_bus: Disk bus type
        :device_type: Device type for this image.
        :cache_mode: Caching mode for this image
        :extra_specs: Instance type extra specs dict.
        :hypervisor_version: the hypervisor version
        :boot_order: Disk device boot order
        """
        info = vconfig.LibvirtConfigGuestDisk()
        info.source_type = self.source_type
        info.source_device = device_type
        info.target_bus = disk_bus
        info.target_dev = disk_dev
        info.driver_cache = cache_mode
        info.driver_discard = self.discard_mode
        info.driver_io = self.driver_io
        info.driver_format = self.driver_format
        driver_name = libvirt_utils.pick_disk_driver_name(
            hypervisor_version, self.is_block_dev)
        info.driver_name = driver_name
        info.source_path = self.path
        info.boot_order = boot_order

        if disk_bus == 'scsi':
            self.disk_scsi(info, disk_unit)

        self.disk_qos(info, extra_specs)

        return info
Example #12
0
    def connect_volume(self, connection_info, disk_info):
        """Connect the volume. Returns xml for libvirt."""

        conf = vconfig.LibvirtConfigGuestDisk()
        conf.driver_name = virtutils.pick_disk_driver_name(
            self.connection.get_hypervisor_version(), self.is_block_dev)
        conf.source_device = disk_info['type']
        conf.driver_format = "raw"
        conf.driver_cache = "none"
        conf.target_dev = disk_info['dev']
        conf.target_bus = disk_info['bus']
        conf.serial = connection_info.get('serial')

        # Support for block size tuning
        data = {}
        if 'data' in connection_info:
            data = connection_info['data']
        if 'logical_block_size' in data:
            conf.logical_block_size = data['logical_block_size']
        if 'physical_block_size' in data:
            conf.physical_block_size = data['physical_block_size']

        # Extract rate_limit control parameters
        if 'qos_specs' in data and data['qos_specs']:
            tune_opts = [
                'total_bytes_sec', 'read_bytes_sec', 'write_bytes_sec',
                'total_iops_sec', 'read_iops_sec', 'write_iops_sec'
            ]
            specs = data['qos_specs']
            if isinstance(specs, dict):
                for k, v in specs.iteritems():
                    if k in tune_opts:
                        new_key = 'disk_' + k
                        setattr(conf, new_key, v)
            else:
                LOG.warn(
                    _('Unknown content in connection_info/'
                      'qos_specs: %s') % specs)

        return conf
Example #13
0
    def connect_volume(self, connection_info, disk_info):
        """Connect the volume. Returns xml for libvirt."""

        conf = vconfig.LibvirtConfigGuestDisk()
        conf.driver_name = virtutils.pick_disk_driver_name(
            self.connection.get_hypervisor_version(), self.is_block_dev)
        conf.device_type = disk_info['type']
        conf.driver_format = "raw"
        conf.driver_cache = "none"
        conf.target_dev = disk_info['dev']
        conf.target_bus = disk_info['bus']
        conf.serial = connection_info.get('serial')

        # Support for block size tuning
        data = {}
        if 'data' in connection_info:
            data = connection_info['data']
        if 'logical_block_size' in data:
            conf.logical_block_size = data['logical_block_size']
        if 'physical_block_size' in data:
            conf.physical_block_size = data['physical_block_size']
        return conf
Example #14
0
    def get_disk(self, device):
        """Returns the disk mounted at device

        :returns LivirtConfigGuestDisk: mounted at device or None
        """
        try:
            doc = etree.fromstring(self._domain.XMLDesc(0))
        except Exception:
            return None

        # FIXME(lyarwood): Workaround for the device being either a target dev
        # when called via swap_volume or source file when called via
        # live_snapshot. This should be removed once both are refactored to use
        # only the target dev of the device.
        node = doc.find("./devices/disk/target[@dev='%s'].." % device)
        if node is None:
            node = doc.find("./devices/disk/source[@file='%s'].." % device)

        if node is not None:
            conf = vconfig.LibvirtConfigGuestDisk()
            conf.parse_dom(node)
            return conf
Example #15
0
    def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode,
                     extra_specs, hypervisor_version):
        """Get `LibvirtConfigGuestDisk` filled for this image.

        :disk_dev: Disk bus device name
        :disk_bus: Disk bus type
        :device_type: Device type for this image.
        :cache_mode: Caching mode for this image
        :extra_specs: Instance type extra specs dict.
        """
        info = vconfig.LibvirtConfigGuestDisk()
        info.source_type = self.source_type
        info.source_device = device_type
        info.target_bus = disk_bus
        info.target_dev = disk_dev
        info.driver_cache = cache_mode
        info.driver_format = self.driver_format
        driver_name = libvirt_utils.pick_disk_driver_name(
            hypervisor_version, self.is_block_dev)
        info.driver_name = driver_name
        info.source_path = self.path

        tune_items = [
            'disk_read_bytes_sec', 'disk_read_iops_sec',
            'disk_write_bytes_sec', 'disk_write_iops_sec',
            'disk_total_bytes_sec', 'disk_total_iops_sec'
        ]
        # Note(yaguang): Currently, the only tuning available is Block I/O
        # throttling for qemu.
        if self.source_type in ['file', 'block']:
            for key, value in extra_specs.iteritems():
                scope = key.split(':')
                if len(scope) > 1 and scope[0] == 'quota':
                    if scope[1] in tune_items:
                        setattr(info, scope[1], value)
        return info
Example #16
0
    def test_config_network_multihost(self):
        obj = config.LibvirtConfigGuestDisk()
        obj.source_type = 'network'
        obj.source_protocol = 'rbd'
        obj.source_name = 'pool/image'
        obj.source_hosts = ['foo.bar.com', '::1', '1.2.3.4']
        obj.source_ports = [None, '123', '456']
        obj.driver_name = 'qemu'
        obj.driver_format = 'raw'
        obj.target_dev = '/dev/vda'
        obj.target_bus = 'virtio'

        xml = obj.to_xml()
        self.assertXmlEqual(
            xml, """
            <disk type="network" device="disk">
              <driver name="qemu" type="raw"/>
              <source name="pool/image" protocol="rbd">
                <host name="foo.bar.com"/>
                <host name="::1" port="123"/>
                <host name="1.2.3.4" port="456"/>
              </source>
              <target bus="virtio" dev="/dev/vda"/>
            </disk>""")
Example #17
0
    def test_config_network_auth(self):
        obj = config.LibvirtConfigGuestDisk()
        obj.source_type = "network"
        obj.source_protocol = "rbd"
        obj.source_host = "pool/image"
        obj.driver_name = "qemu"
        obj.driver_format = "raw"
        obj.target_dev = "/dev/vda"
        obj.target_bus = "virtio"
        obj.auth_username = "******"
        obj.auth_secret_type = "ceph"
        obj.auth_secret_uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"

        xml = obj.to_xml()
        self.assertXmlEqual(xml, """
            <disk type="network" device="disk">
              <driver name="qemu" type="raw"/>
              <source protocol="rbd" name="pool/image"/>
              <auth username="******">
                <secret type="ceph"
                uuid="b38a3f43-4be2-4046-897f-b67c2f5e0147"/>
              </auth>
              <target bus="virtio" dev="/dev/vda"/>
            </disk>""")
    def test_config_kvm(self):
        obj = config.LibvirtConfigGuest()
        obj.virt_type = "kvm"
        obj.memory = 1024 * 1024 * 100
        obj.vcpus = 2
        obj.cpu_shares = 100
        obj.cpu_quota = 50000
        obj.cpu_period = 25000
        obj.name = "demo"
        obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
        obj.os_type = "linux"
        obj.os_boot_dev = "hd"
        obj.os_smbios = config.LibvirtConfigGuestSMBIOS()
        obj.acpi = True
        obj.apic = True

        obj.sysinfo = config.LibvirtConfigGuestSysinfo()
        obj.sysinfo.bios_vendor = "Acme"
        obj.sysinfo.system_version = "1.0.0"

        disk = config.LibvirtConfigGuestDisk()
        disk.source_type = "file"
        disk.source_path = "/tmp/img"
        disk.target_dev = "/dev/vda"
        disk.target_bus = "virtio"

        obj.add_device(disk)

        xml = obj.to_xml()
        self.assertXmlEqual(
            xml, """
            <domain type="kvm">
              <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
              <name>demo</name>
              <memory>104857600</memory>
              <vcpu>2</vcpu>
              <sysinfo type='smbios'>
                 <bios>
                   <entry name="vendor">Acme</entry>
                 </bios>
                 <system>
                   <entry name="version">1.0.0</entry>
                 </system>
              </sysinfo>
              <os>
                <type>linux</type>
                <boot dev="hd"/>
                <smbios mode="sysinfo"/>
              </os>
              <features>
                <acpi/>
                <apic/>
              </features>
              <cputune>
                <shares>100</shares>
                <quota>50000</quota>
                <period>25000</period>
              </cputune>
              <devices>
                <disk type="file" device="disk">
                  <source file="/tmp/img"/>
                  <target bus="virtio" dev="/dev/vda"/>
                </disk>
              </devices>
            </domain>""")
Example #19
0
def fake_kvm_guest():
    obj = config.LibvirtConfigGuest()
    obj.virt_type = "kvm"
    obj.memory = 100 * units.Mi
    obj.vcpus = 2
    obj.cpuset = set([0, 1, 3, 4, 5])

    obj.cputune = config.LibvirtConfigGuestCPUTune()
    obj.cputune.shares = 100
    obj.cputune.quota = 50000
    obj.cputune.period = 25000

    obj.membacking = config.LibvirtConfigGuestMemoryBacking()
    page1 = config.LibvirtConfigGuestMemoryBackingPage()
    page1.size_kb = 2048
    page1.nodeset = [0, 1, 2, 3, 5]
    page2 = config.LibvirtConfigGuestMemoryBackingPage()
    page2.size_kb = 1048576
    page2.nodeset = [4]
    obj.membacking.hugepages.append(page1)
    obj.membacking.hugepages.append(page2)

    obj.memtune = config.LibvirtConfigGuestMemoryTune()
    obj.memtune.hard_limit = 496
    obj.memtune.soft_limit = 672
    obj.memtune.swap_hard_limit = 1638
    obj.memtune.min_guarantee = 2970

    obj.numatune = config.LibvirtConfigGuestNUMATune()

    numamemory = config.LibvirtConfigGuestNUMATuneMemory()
    numamemory.mode = "preferred"
    numamemory.nodeset = [0, 1, 2, 3, 8]

    obj.numatune.memory = numamemory

    numamemnode0 = config.LibvirtConfigGuestNUMATuneMemNode()
    numamemnode0.cellid = 0
    numamemnode0.mode = "preferred"
    numamemnode0.nodeset = [0, 1]

    numamemnode1 = config.LibvirtConfigGuestNUMATuneMemNode()
    numamemnode1.cellid = 1
    numamemnode1.mode = "preferred"
    numamemnode1.nodeset = [2, 3]

    numamemnode2 = config.LibvirtConfigGuestNUMATuneMemNode()
    numamemnode2.cellid = 2
    numamemnode2.mode = "preferred"
    numamemnode2.nodeset = [8]

    obj.numatune.memnodes.extend([numamemnode0, numamemnode1, numamemnode2])

    obj.name = "demo"
    obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
    obj.os_type = "linux"
    obj.os_boot_dev = ["hd", "cdrom", "fd"]
    obj.os_smbios = config.LibvirtConfigGuestSMBIOS()
    obj.features = [
        config.LibvirtConfigGuestFeatureACPI(),
        config.LibvirtConfigGuestFeatureAPIC(),
        config.LibvirtConfigGuestFeaturePAE(),
        config.LibvirtConfigGuestFeatureKvmHidden()
    ]

    obj.sysinfo = config.LibvirtConfigGuestSysinfo()
    obj.sysinfo.bios_vendor = "Acme"
    obj.sysinfo.system_version = "1.0.0"

    disk = config.LibvirtConfigGuestDisk()
    disk.source_type = "file"
    disk.source_path = "/tmp/img"
    disk.target_dev = "/dev/vda"
    disk.target_bus = "virtio"
    obj.add_device(disk)

    return obj
Example #20
0
    def test_update_volume_xml_update_encryption(self):
        connection_info = {
            'driver_volume_type': 'rbd',
            'serial': 'd299a078-f0db-4993-bf03-f10fe44fd192',
            'data': {
                'access_mode': 'rw',
                'secret_type': 'ceph',
                'name': 'cinder-volumes/volume-d299a078',
                'encrypted': False,
                'discard': True,
                'cluster_name': 'ceph',
                'secret_uuid': '1a790a26-dd49-4825-8d16-3dd627cf05a9',
                'qos_specs': None,
                'auth_enabled': True,
                'volume_id': 'd299a078-f0db-4993-bf03-f10fe44fd192',
                'hosts': ['172.16.128.101', '172.16.128.121'],
                'auth_username': '******',
                'ports': ['6789', '6789', '6789']
            }
        }
        bdm = objects.LibvirtLiveMigrateBDMInfo(
            serial='d299a078-f0db-4993-bf03-f10fe44fd192',
            bus='scsi',
            type='disk',
            dev='sdb',
            connection_info=connection_info,
            encryption_secret_uuid=uuids.encryption_secret_uuid_new)
        data = objects.LibvirtLiveMigrateData(target_connect_addr=None,
                                              bdms=[bdm],
                                              block_migration=False)
        xml = """<domain>
 <devices>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='writeback' discard='unmap'/>
      <auth username='******'>
        <secret type='ceph' uuid='1a790a26-dd49-4825-8d16-3dd627cf05a9'/>
      </auth>
      <source protocol='rbd' name='cinder-volumes/volume-d299a078'>
        <host name='172.16.128.101' port='6789'/>
        <host name='172.16.128.121' port='6789'/>
      </source>
      <backingStore/>
      <target dev='sdb' bus='scsi'/>
      <serial>d299a078-f0db-4993-bf03-f10fe44fd192</serial>
      <alias name='scsi0-0-0-1'/>
      <encryption format='luks'>
        <secret type='passphrase' uuid='%(encryption_secret_uuid)s'/>
      </encryption>
      <address type='drive' controller='0' bus='0' target='0' unit='1'/>
    </disk>
 </devices>
</domain>""" % {
            'encryption_secret_uuid': uuids.encryption_secret_uuid_old
        }
        conf = vconfig.LibvirtConfigGuestDisk()
        conf.source_device = bdm.type
        conf.driver_name = "qemu"
        conf.driver_format = "raw"
        conf.driver_cache = "writeback"
        conf.target_dev = bdm.dev
        conf.target_bus = bdm.bus
        conf.serial = bdm.connection_info.get('serial')
        conf.source_type = "network"
        conf.driver_discard = 'unmap'
        conf.device_addr = vconfig.LibvirtConfigGuestDeviceAddressDrive()
        conf.device_addr.controller = 0

        get_volume_config = mock.MagicMock(return_value=conf)
        doc = etree.fromstring(xml)
        res = etree.tostring(migration._update_volume_xml(
            doc, data, get_volume_config),
                             encoding='unicode')
        new_xml = xml.replace(uuids.encryption_secret_uuid_old,
                              uuids.encryption_secret_uuid_new)
        self.assertThat(res, matchers.XMLMatches(new_xml))
Example #21
0
    def get_config(self, connection_info, disk_info):
        """Returns xml for libvirt."""
        conf = vconfig.LibvirtConfigGuestDisk()

        conf.source_device = disk_info['type']
        conf.driver_format = "raw"
        conf.driver_cache = "none"
        conf.target_dev = disk_info['dev']
        conf.target_bus = disk_info['bus']
        conf.serial = connection_info.get('serial')

        if CONF.libvirt.virt_type in ('qemu', 'kvm'):
            # the QEMU backend supports multiple backends, so tell libvirt
            # which one to use
            conf.driver_name = 'qemu'

        # Support for block size tuning
        data = {}
        if 'data' in connection_info:
            data = connection_info['data']
        if 'logical_block_size' in data:
            conf.logical_block_size = data['logical_block_size']
        if 'physical_block_size' in data:
            conf.physical_block_size = data['physical_block_size']

        # Extract rate_limit control parameters
        if 'qos_specs' in data and data['qos_specs']:
            tune_opts = [
                'total_bytes_sec', 'read_bytes_sec', 'write_bytes_sec',
                'total_iops_sec', 'read_iops_sec', 'write_iops_sec',
                'read_bytes_sec_max', 'read_iops_sec_max',
                'write_bytes_sec_max', 'write_iops_sec_max',
                'total_bytes_sec_max', 'total_iops_sec_max', 'size_iops_sec'
            ]
            specs = data['qos_specs']
            if isinstance(specs, dict):
                for k, v in specs.items():
                    if k in tune_opts:
                        new_key = 'disk_' + k
                        setattr(conf, new_key, v)
            else:
                LOG.warning(
                    'Unknown content in connection_info/'
                    'qos_specs: %s', specs)

        # Extract access_mode control parameters
        if 'access_mode' in data and data['access_mode']:
            access_mode = data['access_mode']
            if access_mode in ('ro', 'rw'):
                conf.readonly = access_mode == 'ro'
            else:
                LOG.error(
                    'Unknown content in '
                    'connection_info/access_mode: %s', access_mode)
                raise exception.InvalidVolumeAccessMode(
                    access_mode=access_mode)

        # Configure usage of discard
        if data.get('discard', False) is True:
            conf.driver_discard = 'unmap'

        # NOTE(melwitt): We set the device address unit number manually in the
        # case of the virtio-scsi controller, in order to allow attachment of
        # up to 256 devices. So, we should only be setting the address tag
        # if we intend to set the unit number. Otherwise, we will let libvirt
        # handle autogeneration of the address tag.
        # See https://bugs.launchpad.net/nova/+bug/1792077 for details.
        if disk_info['bus'] == 'scsi' and 'unit' in disk_info:
            # The driver is responsible to create the SCSI controller
            # at index 0.
            conf.device_addr = vconfig.LibvirtConfigGuestDeviceAddressDrive()
            conf.device_addr.controller = 0
            # In order to allow up to 256 disks handled by one
            # virtio-scsi controller, the device addr should be
            # specified.
            conf.device_addr.unit = disk_info['unit']

        if connection_info.get('multiattach', False):
            # Note that driver_cache should be disabled (none) when using
            # a shareable disk.
            conf.shareable = True

        volume_id = driver_block_device.get_volume_id(connection_info)
        volume_secret = None
        if volume_id:
            volume_secret = self.host.find_secret('volume', volume_id)
        if volume_secret:
            conf.encryption = vconfig.LibvirtConfigGuestDiskEncryption()
            secret = vconfig.LibvirtConfigGuestDiskEncryptionSecret()
            secret.type = 'passphrase'
            secret.uuid = volume_secret.UUIDString()
            conf.encryption.format = 'luks'
            conf.encryption.secret = secret

        return conf
Example #22
0
    def setUp(self):
        super(_LibvirtEvacuateTest, self).setUp()

        self.useFixture(nova_fixtures.CinderFixture(self))
        self.useFixture(nova_fixtures.NeutronFixture(self))
        self.useFixture(nova_fixtures.GlanceFixture(self))
        self.useFixture(func_fixtures.PlacementFixture())
        fake_network.set_stub_network_methods(self)

        api_fixture = self.useFixture(
            nova_fixtures.OSAPIFixture(api_version='v2.1'))

        self.api = api_fixture.admin_api
        # force_down and evacuate without onSharedStorage
        self.api.microversion = '2.14'

        fake_notifier.stub_notifier(self)
        self.addCleanup(fake_notifier.reset)

        self.useFixture(fakelibvirt.FakeLibvirtFixture())

        # Fake out all the details of volume connection
        self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.driver.LibvirtDriver.get_volume_connector'))
        self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.driver.LibvirtDriver._connect_volume'))
        # For cleanup
        self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.driver.LibvirtDriver._disconnect_volume'))

        volume_config = libvirt_config.LibvirtConfigGuestDisk()
        volume_config.driver_name = 'fake-volume-driver'
        volume_config.source_path = 'fake-source-path'
        volume_config.target_dev = 'fake-target-dev'
        volume_config.target_bus = 'fake-target-bus'
        get_volume_config = self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.driver.LibvirtDriver._get_volume_config')
        ).mock
        get_volume_config.return_value = volume_config

        # Ensure our computes report lots of available disk, vcpu, and ram
        lots = 10000000
        get_local_gb_info = self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.driver.LibvirtDriver._get_local_gb_info')
        ).mock
        get_local_gb_info.return_value = {
            'total': lots,
            'free': lots,
            'used': 1
        }
        get_vcpu_available = self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.driver.LibvirtDriver._get_vcpu_available')
        ).mock
        get_vcpu_available.return_value = set(cpu for cpu in range(24))
        get_memory_mb_total = self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.host.Host.get_memory_mb_total')).mock
        get_memory_mb_total.return_value = lots

        # Mock out adding rng devices
        self.useFixture(
            fixtures.MockPatch(
                'nova.virt.libvirt.driver.LibvirtDriver._add_rng_device')).mock

        self.start_service('conductor')
        self.start_service('scheduler')

        self.flags(compute_driver='libvirt.LibvirtDriver')

        ctxt = context.get_admin_context()
        for flavor in FLAVOR_FIXTURES:
            objects.Flavor(context=ctxt, **flavor).create()
    def get_config(self, connection_info, disk_info):
        """Returns xml for libvirt."""
        conf = vconfig.LibvirtConfigGuestDisk()
        conf.driver_name = libvirt_utils.pick_disk_driver_name(
            self.connection._host.get_version(), self.is_block_dev)

        conf.source_device = disk_info['type']
        conf.driver_format = "raw"
        conf.driver_cache = "none"
        conf.target_dev = disk_info['dev']
        conf.target_bus = disk_info['bus']
        conf.serial = connection_info.get('serial')

        # Support for block size tuning
        data = {}
        if 'data' in connection_info:
            data = connection_info['data']
        if 'logical_block_size' in data:
            conf.logical_block_size = data['logical_block_size']
        if 'physical_block_size' in data:
            conf.physical_block_size = data['physical_block_size']

        # Extract rate_limit control parameters
        if 'qos_specs' in data and data['qos_specs']:
            tune_opts = [
                'total_bytes_sec', 'read_bytes_sec', 'write_bytes_sec',
                'total_iops_sec', 'read_iops_sec', 'write_iops_sec'
            ]
            specs = data['qos_specs']
            if isinstance(specs, dict):
                for k, v in six.iteritems(specs):
                    if k in tune_opts:
                        new_key = 'disk_' + k
                        setattr(conf, new_key, v)
            else:
                LOG.warning(
                    _LW('Unknown content in connection_info/'
                        'qos_specs: %s'), specs)

        # Extract access_mode control parameters
        if 'access_mode' in data and data['access_mode']:
            access_mode = data['access_mode']
            if access_mode in ('ro', 'rw'):
                conf.readonly = access_mode == 'ro'
            else:
                LOG.error(
                    _LE('Unknown content in '
                        'connection_info/access_mode: %s'), access_mode)
                raise exception.InvalidVolumeAccessMode(
                    access_mode=access_mode)

        # Configure usage of discard
        if data.get('discard', False) is True:
            min_qemu = nova.virt.libvirt.driver.MIN_QEMU_DISCARD_VERSION
            min_libvirt = nova.virt.libvirt.driver.MIN_LIBVIRT_DISCARD_VERSION
            if self.connection._host.has_min_version(min_libvirt, min_qemu,
                                                     host.HV_DRIVER_QEMU):
                conf.driver_discard = 'unmap'
            else:
                global SHOULD_LOG_DISCARD_WARNING
                if SHOULD_LOG_DISCARD_WARNING:
                    SHOULD_LOG_DISCARD_WARNING = False
                    LOG.warning(
                        _LW('Unable to attach %(type)s volume '
                            '%(serial)s with discard enabled: qemu '
                            '%(qemu)s and libvirt %(libvirt)s or '
                            'later are required.'), {
                                'qemu': min_qemu,
                                'libvirt': min_libvirt,
                                'serial': conf.serial,
                                'type': connection_info['driver_volume_type']
                            })

        return conf
Example #24
0
def fake_kvm_guest():
    obj = config.LibvirtConfigGuest()
    obj.virt_type = "kvm"
    obj.memory = 100 * units.Mi
    obj.vcpus = 2
    obj.cpuset = set([0, 1, 3, 4, 5])

    obj.cputune = config.LibvirtConfigGuestCPUTune()
    obj.cputune.shares = 100
    obj.cputune.quota = 50000
    obj.cputune.period = 25000

    obj.membacking = config.LibvirtConfigGuestMemoryBacking()
    page1 = config.LibvirtConfigGuestMemoryBackingPage()
    page1.size_kb = 2048
    page1.nodeset = [0, 1, 2, 3, 5]
    page2 = config.LibvirtConfigGuestMemoryBackingPage()
    page2.size_kb = 1048576
    page2.nodeset = [4]
    obj.membacking.hugepages.append(page1)
    obj.membacking.hugepages.append(page2)

    obj.memtune = config.LibvirtConfigGuestMemoryTune()
    obj.memtune.hard_limit = 496
    obj.memtune.soft_limit = 672
    obj.memtune.swap_hard_limit = 1638
    obj.memtune.min_guarantee = 2970

    obj.numatune = config.LibvirtConfigGuestNUMATune()

    numamemory = config.LibvirtConfigGuestNUMATuneMemory()
    numamemory.mode = "preferred"
    numamemory.nodeset = [0, 1, 2, 3, 8]

    obj.numatune.memory = numamemory

    numamemnode0 = config.LibvirtConfigGuestNUMATuneMemNode()
    numamemnode0.cellid = 0
    numamemnode0.mode = "preferred"
    numamemnode0.nodeset = [0, 1]

    numamemnode1 = config.LibvirtConfigGuestNUMATuneMemNode()
    numamemnode1.cellid = 1
    numamemnode1.mode = "preferred"
    numamemnode1.nodeset = [2, 3]

    numamemnode2 = config.LibvirtConfigGuestNUMATuneMemNode()
    numamemnode2.cellid = 2
    numamemnode2.mode = "preferred"
    numamemnode2.nodeset = [8]

    obj.numatune.memnodes.extend([numamemnode0, numamemnode1, numamemnode2])

    obj.name = "demo"
    obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
    obj.os_type = "linux"
    obj.os_boot_dev = ["hd", "cdrom", "fd"]
    obj.os_smbios = config.LibvirtConfigGuestSMBIOS()
    obj.features = [
        config.LibvirtConfigGuestFeatureACPI(),
        config.LibvirtConfigGuestFeatureAPIC(),
        config.LibvirtConfigGuestFeaturePAE(),
        config.LibvirtConfigGuestFeatureKvmHidden()
    ]

    obj.sysinfo = config.LibvirtConfigGuestSysinfo()
    obj.sysinfo.bios_vendor = "Acme"
    obj.sysinfo.system_version = "1.0.0"

    # obj.devices[0]
    disk = config.LibvirtConfigGuestDisk()
    disk.source_type = "file"
    disk.source_path = "/tmp/disk-img"
    disk.target_dev = "vda"
    disk.target_bus = "virtio"
    obj.add_device(disk)

    # obj.devices[1]
    disk = config.LibvirtConfigGuestDisk()
    disk.source_device = "cdrom"
    disk.source_type = "file"
    disk.source_path = "/tmp/cdrom-img"
    disk.target_dev = "sda"
    disk.target_bus = "sata"
    obj.add_device(disk)

    # obj.devices[2]
    intf = config.LibvirtConfigGuestInterface()
    intf.net_type = "network"
    intf.mac_addr = "52:54:00:f6:35:8f"
    intf.model = "virtio"
    intf.source_dev = "virbr0"
    obj.add_device(intf)

    # obj.devices[3]
    balloon = config.LibvirtConfigMemoryBalloon()
    balloon.model = 'virtio'
    balloon.period = 11
    obj.add_device(balloon)

    # obj.devices[4]
    mouse = config.LibvirtConfigGuestInput()
    mouse.type = "mouse"
    mouse.bus = "virtio"
    obj.add_device(mouse)

    # obj.devices[5]
    gfx = config.LibvirtConfigGuestGraphics()
    gfx.type = "vnc"
    gfx.autoport = True
    gfx.keymap = "en_US"
    gfx.listen = "127.0.0.1"
    obj.add_device(gfx)

    # obj.devices[6]
    video = config.LibvirtConfigGuestVideo()
    video.type = 'virtio'
    obj.add_device(video)

    # obj.devices[7]
    serial = config.LibvirtConfigGuestSerial()
    serial.type = "file"
    serial.source_path = "/tmp/vm.log"
    obj.add_device(serial)

    # obj.devices[8]
    rng = config.LibvirtConfigGuestRng()
    rng.backend = '/dev/urandom'
    rng.rate_period = '12'
    rng.rate_bytes = '34'
    obj.add_device(rng)

    # obj.devices[9]
    controller = config.LibvirtConfigGuestController()
    controller.type = 'scsi'
    controller.model = 'virtio-scsi'  # usually set from image meta
    controller.index = 0
    obj.add_device(controller)

    return obj
Example #25
0
    def get_config(self, connection_info, disk_info):
        """Returns xml for libvirt."""
        conf = vconfig.LibvirtConfigGuestDisk()
        conf.driver_name = libvirt_utils.pick_disk_driver_name(
            self.host.get_version(), self.is_block_dev)

        conf.source_device = disk_info['type']
        conf.driver_format = "raw"
        conf.driver_cache = "none"
        conf.target_dev = disk_info['dev']
        conf.target_bus = disk_info['bus']
        conf.serial = connection_info.get('serial')

        # Support for block size tuning
        data = {}
        if 'data' in connection_info:
            data = connection_info['data']
        if 'logical_block_size' in data:
            conf.logical_block_size = data['logical_block_size']
        if 'physical_block_size' in data:
            conf.physical_block_size = data['physical_block_size']

        # Extract rate_limit control parameters
        if 'qos_specs' in data and data['qos_specs']:
            tune_opts = [
                'total_bytes_sec', 'read_bytes_sec', 'write_bytes_sec',
                'total_iops_sec', 'read_iops_sec', 'write_iops_sec'
            ]
            specs = data['qos_specs']
            if isinstance(specs, dict):
                for k, v in specs.items():
                    if k in tune_opts:
                        new_key = 'disk_' + k
                        setattr(conf, new_key, v)
            else:
                LOG.warning(
                    'Unknown content in connection_info/'
                    'qos_specs: %s', specs)

        # Extract access_mode control parameters
        if 'access_mode' in data and data['access_mode']:
            access_mode = data['access_mode']
            if access_mode in ('ro', 'rw'):
                conf.readonly = access_mode == 'ro'
            else:
                LOG.error(
                    'Unknown content in '
                    'connection_info/access_mode: %s', access_mode)
                raise exception.InvalidVolumeAccessMode(
                    access_mode=access_mode)

        # Configure usage of discard
        if data.get('discard', False) is True:
            conf.driver_discard = 'unmap'

        if disk_info['bus'] == 'scsi':
            # The driver is responsible to create the SCSI controller
            # at index 0.
            conf.device_addr = vconfig.LibvirtConfigGuestDeviceAddressDrive()
            conf.device_addr.controller = 0
            if 'unit' in disk_info:
                # In order to allow up to 256 disks handled by one
                # virtio-scsi controller, the device addr should be
                # specified.
                conf.device_addr.unit = disk_info['unit']

        if connection_info.get('multiattach', False):
            # Note that driver_cache should be disabled (none) when using
            # a shareable disk.
            conf.shareable = True

        volume_id = connection_info.get('data', {}).get('volume_id')
        volume_secret = None
        if volume_id:
            volume_secret = self.host.find_secret('volume', volume_id)
        if volume_secret:
            conf.encryption = vconfig.LibvirtConfigGuestDiskEncryption()
            secret = vconfig.LibvirtConfigGuestDiskEncryptionSecret()
            secret.type = 'passphrase'
            secret.uuid = volume_secret.UUIDString()
            conf.encryption.format = 'luks'
            conf.encryption.secret = secret

        return conf
Example #26
0
 def fake_disk(disk):
     libvirt_disk = libvirt_config.LibvirtConfigGuestDisk()
     libvirt_disk.source_type = disk[0]
     libvirt_disk.source_path = os.path.join(*disk[1])
     return libvirt_disk