Ejemplo n.º 1
0
    def test_block_device(self):
        swap = {"device_name": "/dev/sdb", "swap_size": 1}
        ephemerals = [{"num": 0, "virtual_name": "ephemeral0", "device_name": "/dev/sdc1", "size": 1}]
        block_device_mapping = [{"mount_device": "/dev/sde", "device_path": "fake_device"}]
        block_device_info = {
            "root_device_name": "/dev/sda",
            "swap": swap,
            "ephemerals": ephemerals,
            "block_device_mapping": block_device_mapping,
        }

        empty_block_device_info = {}

        self.assertEqual(driver.block_device_info_get_root(block_device_info), "/dev/sda")
        self.assertIsNone(driver.block_device_info_get_root(empty_block_device_info))
        self.assertIsNone(driver.block_device_info_get_root(None))

        self.assertEqual(driver.block_device_info_get_swap(block_device_info), swap)
        self.assertIsNone(driver.block_device_info_get_swap(empty_block_device_info)["device_name"])
        self.assertEqual(driver.block_device_info_get_swap(empty_block_device_info)["swap_size"], 0)
        self.assertIsNone(driver.block_device_info_get_swap({"swap": None})["device_name"])
        self.assertEqual(driver.block_device_info_get_swap({"swap": None})["swap_size"], 0)
        self.assertIsNone(driver.block_device_info_get_swap(None)["device_name"])
        self.assertEqual(driver.block_device_info_get_swap(None)["swap_size"], 0)

        self.assertEqual(driver.block_device_info_get_ephemerals(block_device_info), ephemerals)
        self.assertEqual(driver.block_device_info_get_ephemerals(empty_block_device_info), [])
        self.assertEqual(driver.block_device_info_get_ephemerals(None), [])
Ejemplo n.º 2
0
    def test_block_device(self):
        swap = {'device_name': '/dev/sdb', 'swap_size': 1}
        ephemerals = [{
            'num': 0,
            'virtual_name': 'ephemeral0',
            'device_name': '/dev/sdc1',
            'size': 1
        }]
        block_device_mapping = [{
            'mount_device': '/dev/sde',
            'device_path': 'fake_device'
        }]
        block_device_info = {
            'root_device_name': '/dev/sda',
            'swap': swap,
            'ephemerals': ephemerals,
            'block_device_mapping': block_device_mapping
        }

        empty_block_device_info = {}

        self.assertEqual(driver.block_device_info_get_root(block_device_info),
                         '/dev/sda')
        self.assertEqual(
            driver.block_device_info_get_root(empty_block_device_info), None)
        self.assertEqual(driver.block_device_info_get_root(None), None)

        self.assertEqual(driver.block_device_info_get_swap(block_device_info),
                         swap)
        self.assertEqual(
            driver.block_device_info_get_swap(empty_block_device_info)
            ['device_name'], None)
        self.assertEqual(
            driver.block_device_info_get_swap(empty_block_device_info)
            ['swap_size'], 0)
        self.assertEqual(
            driver.block_device_info_get_swap({'swap': None})['device_name'],
            None)
        self.assertEqual(
            driver.block_device_info_get_swap({'swap': None})['swap_size'], 0)
        self.assertEqual(
            driver.block_device_info_get_swap(None)['device_name'], None)
        self.assertEqual(
            driver.block_device_info_get_swap(None)['swap_size'], 0)

        self.assertEqual(
            driver.block_device_info_get_ephemerals(block_device_info),
            ephemerals)
        self.assertEqual(
            driver.block_device_info_get_ephemerals(empty_block_device_info),
            [])
        self.assertEqual(driver.block_device_info_get_ephemerals(None), [])
Ejemplo n.º 3
0
    def test_block_device(self):
        swap = {'device_name': '/dev/sdb',
                'swap_size': 1}
        ephemerals = [{'num': 0,
                       'virtual_name': 'ephemeral0',
                       'device_name': '/dev/sdc1',
                       'size': 1}]
        block_device_mapping = [{'mount_device': '/dev/sde',
                                 'device_path': 'fake_device'}]
        block_device_info = {
                'root_device_name': '/dev/sda',
                'swap': swap,
                'ephemerals': ephemerals,
                'block_device_mapping': block_device_mapping}

        empty_block_device_info = {}

        self.assertEqual(
            driver.block_device_info_get_root(block_device_info), '/dev/sda')
        self.assertEqual(
            driver.block_device_info_get_root(empty_block_device_info), None)
        self.assertEqual(
            driver.block_device_info_get_root(None), None)

        self.assertEqual(
            driver.block_device_info_get_swap(block_device_info), swap)
        self.assertEqual(driver.block_device_info_get_swap(
            empty_block_device_info)['device_name'], None)
        self.assertEqual(driver.block_device_info_get_swap(
            empty_block_device_info)['swap_size'], 0)
        self.assertEqual(
            driver.block_device_info_get_swap({'swap': None})['device_name'],
            None)
        self.assertEqual(
            driver.block_device_info_get_swap({'swap': None})['swap_size'],
            0)
        self.assertEqual(
            driver.block_device_info_get_swap(None)['device_name'], None)
        self.assertEqual(
            driver.block_device_info_get_swap(None)['swap_size'], 0)

        self.assertEqual(
            driver.block_device_info_get_ephemerals(block_device_info),
            ephemerals)
        self.assertEqual(
            driver.block_device_info_get_ephemerals(empty_block_device_info),
            [])
        self.assertEqual(
            driver.block_device_info_get_ephemerals(None),
            [])
Ejemplo n.º 4
0
def detach_ephemeral(client, block_device_info, lxd_config, instance):
    """Detach ephemeral device from the instance."""
    ephemeral_storage = driver.block_device_info_get_ephemerals(
        block_device_info)
    if ephemeral_storage:
        storage_driver = lxd_config['environment']['storage']

        for ephemeral in ephemeral_storage:
            if storage_driver == 'zfs':
                # NOTE(ajkavanagh) - BUG/1782329 - this is temporary until
                # storage pools is implemented.  LXD 3 removed the
                # storage.zfs_pool_name key from the config.  So, if it fails,
                # we need to grab the configured storage pool and use that as
                # the name instead.
                try:
                    zfs_pool = lxd_config['config']['storage.zfs_pool_name']
                except KeyError:
                    zfs_pool = CONF.lxd.pool

                utils.execute('zfs',
                              'destroy',
                              '%s/%s-ephemeral' % (zfs_pool, instance.name),
                              run_as_root=True)
            if storage_driver == 'lvm':
                lvm_pool = lxd_config['config']['storage.lvm_vg_name']

                lvm_path = '/dev/%s/%s-%s' % (lvm_pool, instance.name,
                                              ephemeral['virtual_name'])

                utils.execute('umount', lvm_path, run_as_root=True)
                utils.execute('lvremove', '-f', lvm_path, run_as_root=True)
Ejemplo n.º 5
0
def detach_ephemeral(client, block_device_info, lxd_config, instance):
    """Detach ephemeral device from the instance."""
    ephemeral_storage = driver.block_device_info_get_ephemerals(
        block_device_info)
    if ephemeral_storage:
        storage_driver = lxd_config['environment']['storage']

        for ephemeral in ephemeral_storage:
            if storage_driver == 'zfs':
                # NOTE(ajkavanagh) - BUG/1782329 - this is temporary until
                # storage pools is implemented.  LXD 3 removed the
                # storage.zfs_pool_name key from the config.  So, if it fails,
                # we need to grab the configured storage pool and use that as
                # the name instead.
                try:
                    zfs_pool = lxd_config['config']['storage.zfs_pool_name']
                except KeyError:
                    zfs_pool = CONF.lxd.pool

                utils.execute(
                    'zfs', 'destroy',
                    '%s/%s-ephemeral' % (zfs_pool, instance.name),
                    run_as_root=True)
            if storage_driver == 'lvm':
                lvm_pool = lxd_config['config']['storage.lvm_vg_name']

                lvm_path = '/dev/%s/%s-%s' % (
                    lvm_pool, instance.name, ephemeral['virtual_name'])

                utils.execute('umount', lvm_path, run_as_root=True)
                utils.execute('lvremove', '-f', lvm_path, run_as_root=True)
Ejemplo n.º 6
0
Archivo: driver.py Proyecto: mahak/nova
    def _set_disk_list(self, instance, image_name, block_device_info):
        if instance.root_gb == 0:
            root_disk_size = self._hypervisor.image_get_root_disk_size(
                image_name)
        else:
            root_disk_size = '%ig' % instance.root_gb

        disk_list = []
        root_disk = {'size': root_disk_size,
                     'is_boot_disk': True
                    }
        disk_list.append(root_disk)
        ephemeral_disks_info = driver.block_device_info_get_ephemerals(
            block_device_info)

        eph_list = []
        for eph in ephemeral_disks_info:
            eph_dict = {'size': '%ig' % eph['size'],
                        'format': (CONF.default_ephemeral_format or
                                   DEFAULT_EPH_DISK_FMT)}
            eph_list.append(eph_dict)

        if eph_list:
            disk_list.extend(eph_list)
        return disk_list, eph_list
Ejemplo n.º 7
0
    def _check_target_flavor(self, instance, flavor, block_device_info):
        ephemerals = driver.block_device_info_get_ephemerals(block_device_info)
        eph_size = (block_device.get_bdm_ephemeral_disk_size(ephemerals) or
                    instance.flavor.ephemeral_gb)

        new_root_gb = flavor.root_gb
        curr_root_gb = instance.flavor.root_gb
        new_eph_size = flavor.ephemeral_gb

        root_down = new_root_gb < curr_root_gb
        ephemeral_down = new_eph_size < eph_size
        booted_from_volume = self._block_dev_man.is_boot_from_volume(
            block_device_info)

        if root_down and not booted_from_volume:
            raise exception.InstanceFaultRollback(
                exception.CannotResizeDisk(
                    reason=_("Cannot resize the root disk to a smaller size. "
                             "Current size: %(curr_root_gb)s GB. Requested "
                             "size: %(new_root_gb)s GB.") % {
                                 'curr_root_gb': curr_root_gb,
                                 'new_root_gb': new_root_gb}))
        # We allow having a new flavor with no ephemeral storage, in which
        # case we'll just remove all the ephemeral disks.
        elif ephemeral_down and new_eph_size:
            reason = (_("The new flavor ephemeral size (%(flavor_eph)s) is "
                       "smaller than the current total ephemeral disk size: "
                       "%(current_eph)s.") %
                      dict(flavor_eph=flavor.ephemeral_gb,
                           current_eph=eph_size))
            raise exception.InstanceFaultRollback(
                exception.CannotResizeDisk(reason=reason))
Ejemplo n.º 8
0
    def _check_target_flavor(self, instance, flavor, block_device_info):
        new_root_gb = flavor.root_gb
        curr_root_gb = instance.root_gb

        ephemerals = driver.block_device_info_get_ephemerals(block_device_info)
        curr_eph_gb = sum(eph.get('size', 0) for eph in ephemerals)
        new_eph_gb = instance.ephemeral_gb

        if new_root_gb < curr_root_gb:
            raise exception.InstanceFaultRollback(
                exception.CannotResizeDisk(
                    reason=_("Cannot resize the root disk to a smaller size. "
                             "Current size: %(curr_root_gb)s GB. Requested "
                             "size: %(new_root_gb)s GB.") % {
                                 'curr_root_gb': curr_root_gb,
                                 'new_root_gb': new_root_gb}))

        if new_eph_gb < curr_eph_gb:
            raise exception.InstanceFaultRollback(
                exception.CannotResizeDisk(
                    reason=_("Cannot resize the ephemeral disk(s) to a smaller"
                             " size. Current total ephemeral size: "
                             "%(curr_eph_gb)s GB. Requested total size: "
                             "%(new_eph_gb)s GB") % {
                                 'curr_eph_gb': curr_eph_gb,
                                 'new_eph_gb': new_eph_gb}))
Ejemplo n.º 9
0
    def _set_disk_list(self, instance, image_name, block_device_info):
        if instance.root_gb == 0:
            root_disk_size = self._hypervisor.image_get_root_disk_size(
                image_name)
        else:
            root_disk_size = '%ig' % instance.root_gb

        disk_list = []
        root_disk = {'size': root_disk_size, 'is_boot_disk': True}
        disk_list.append(root_disk)
        ephemeral_disks_info = driver.block_device_info_get_ephemerals(
            block_device_info)

        eph_list = []
        for eph in ephemeral_disks_info:
            eph_dict = {
                'size': '%ig' % eph['size'],
                'format': (CONF.default_ephemeral_format
                           or DEFAULT_EPH_DISK_FMT)
            }
            eph_list.append(eph_dict)

        if eph_list:
            disk_list.extend(eph_list)
        return disk_list, eph_list
Ejemplo n.º 10
0
    def _check_target_flavor(self, instance, flavor, block_device_info):
        new_root_gb = flavor.root_gb
        curr_root_gb = instance.root_gb

        ephemerals = driver.block_device_info_get_ephemerals(block_device_info)
        curr_eph_gb = sum(eph.get('size', 0) for eph in ephemerals)
        new_eph_gb = instance.ephemeral_gb

        if new_root_gb < curr_root_gb:
            raise exception.InstanceFaultRollback(
                exception.CannotResizeDisk(
                    reason=_("Cannot resize the root disk to a smaller size. "
                             "Current size: %(curr_root_gb)s GB. Requested "
                             "size: %(new_root_gb)s GB.") % {
                                 'curr_root_gb': curr_root_gb,
                                 'new_root_gb': new_root_gb
                             }))

        if new_eph_gb < curr_eph_gb:
            raise exception.InstanceFaultRollback(
                exception.CannotResizeDisk(
                    reason=_("Cannot resize the ephemeral disk(s) to a smaller"
                             " size. Current total ephemeral size: "
                             "%(curr_eph_gb)s GB. Requested total size: "
                             "%(new_eph_gb)s GB") % {
                                 'curr_eph_gb': curr_eph_gb,
                                 'new_eph_gb': new_eph_gb
                             }))
def has_default_ephemeral(instance, disk_bus, block_device_info, mapping):
    ephemerals = driver.block_device_info_get_ephemerals(block_device_info)
    if instance.ephemeral_gb <= 0 or ephemerals:
        return None
    else:
        info = get_next_disk_info(mapping, disk_bus)
        if block_device.volume_in_mapping(info['dev'], block_device_info):
            return None
        return info
Ejemplo n.º 12
0
def has_default_ephemeral(instance, disk_bus, block_device_info, mapping):
    ephemerals = driver.block_device_info_get_ephemerals(block_device_info)
    if instance['ephemeral_gb'] <= 0 or ephemerals:
        return None
    else:
        info = get_next_disk_info(mapping, disk_bus)
        if block_device.volume_in_mapping(info['dev'], block_device_info):
            return None
        return info
    def _get_boot_order_gen2(self, block_device_info):
        boot_order = [block_device_info['root_disk']]
        boot_order += driver.block_device_info_get_ephemerals(
            block_device_info)
        boot_order += driver.block_device_info_get_mapping(block_device_info)

        self._sort_by_boot_order(boot_order)

        return [self._volops.get_disk_resource_path(
                boot_dev['connection_info']) if boot_dev.get('connection_info')
                else boot_dev['path'] for boot_dev in boot_order]
Ejemplo n.º 14
0
def volume_in_mapping(mount_device, block_device_info):
    block_device_list = [
        block_device.strip_dev(volume['mount_device'])
        for volume in driver.block_device_info_get_mapping(block_device_info)
    ]

    swap = driver.block_device_info_get_swap(block_device_info)
    if driver.swap_is_usable(swap):
        block_device_list.append(block_device.strip_dev(swap['device_name']))
    block_device_list.extend([
        block_device.strip_dev(ephemeral['device_name']) for ephemeral in
        driver.block_device_info_get_ephemerals(block_device_info)
    ])
Ejemplo n.º 15
0
def volume_in_mapping(mount_device, block_device_info):
    block_device_list = [
        block_device.strip_dev(volume['mount_device']) for volume in
        driver.block_device_info_get_mapping(block_device_info)
    ]

    swap = driver.block_device_info_get_swap(block_device_info)
    if driver.swap_is_usable(swap):
        block_device_list.append(block_device.strip_dev(
            swap['device_name']))
    block_device_list.extend([
        block_device.strip_dev(ephemeral['device_name']) for ephemeral in
        driver.block_device_info_get_ephemerals(block_device_info)])
Ejemplo n.º 16
0
    def _get_boot_order_gen2(self, block_device_info):
        boot_order = [block_device_info['root_disk']]
        boot_order += driver.block_device_info_get_ephemerals(
            block_device_info)
        boot_order += driver.block_device_info_get_mapping(block_device_info)

        self._sort_by_boot_order(boot_order)

        return [
            self._volops.get_disk_resource_path(boot_dev['connection_info'])
            if boot_dev.get('connection_info') else boot_dev['path']
            for boot_dev in boot_order
        ]
Ejemplo n.º 17
0
def volume_in_mapping(mount_device, block_device_info):
    block_device_list = [
        strip_dev(vol["mount_device"]) for vol in driver.block_device_info_get_mapping(block_device_info)
    ]

    swap = driver.block_device_info_get_swap(block_device_info)
    if driver.swap_is_usable(swap):
        block_device_list.append(strip_dev(swap["device_name"]))

    block_device_list += [
        strip_dev(ephemeral["device_name"]) for ephemeral in driver.block_device_info_get_ephemerals(block_device_info)
    ]

    LOG.debug(_("block_device_list %s"), block_device_list)
    return strip_dev(mount_device) in block_device_list
Ejemplo n.º 18
0
def _ephemeral_storage(instance, _, __, block_info):
    instance_attributes = common.InstanceAttributes(instance)
    ephemeral_storage = driver.block_device_info_get_ephemerals(block_info)
    if ephemeral_storage:
        devices = {}
        for ephemeral in ephemeral_storage:
            ephemeral_src = os.path.join(
                instance_attributes.storage_path,
                ephemeral['virtual_name'])
            devices[ephemeral['virtual_name']] = {
                'path': '/mnt',
                'source': ephemeral_src,
                'type': 'disk',
            }
        return devices
Ejemplo n.º 19
0
    def volume_in_mapping(self, mount_device, block_device_info):
        block_device_list = [block_device.strip_dev(vol['mount_device'])
                             for vol in
                             driver.block_device_info_get_mapping(
                                 block_device_info)]
        swap = driver.block_device_info_get_swap(block_device_info)
        if driver.swap_is_usable(swap):
            block_device_list.append(
                block_device.strip_dev(swap['device_name']))
        block_device_list += [block_device.strip_dev(
            ephemeral['device_name'])
            for ephemeral in
            driver.block_device_info_get_ephemerals(block_device_info)]

        LOG.debug(_("block_device_list %s"), block_device_list)
        return block_device.strip_dev(mount_device) in block_device_list
Ejemplo n.º 20
0
    def volume_in_mapping(self, mount_device, block_device_info):
        block_device_list = [block_device.strip_dev(vol['mount_device'])
                             for vol in
                             driver.block_device_info_get_mapping(
                                 block_device_info)]
        swap = driver.block_device_info_get_swap(block_device_info)
        if driver.swap_is_usable(swap):
            block_device_list.append(
                block_device.strip_dev(swap['device_name']))
        block_device_list += [block_device.strip_dev(
            ephemeral['device_name'])
            for ephemeral in
            driver.block_device_info_get_ephemerals(block_device_info)]

        LOG.debug("block_device_list %s", block_device_list)
        return block_device.strip_dev(mount_device) in block_device_list
Ejemplo n.º 21
0
    def _get_boot_order_gen2(self, block_device_info):
        devices = [block_device_info['root_disk']]
        devices += driver.block_device_info_get_ephemerals(block_device_info)
        devices += driver.block_device_info_get_mapping(block_device_info)

        self._sort_by_boot_order(devices)

        boot_order = []
        for dev in devices:
            if dev.get('connection_info'):
                dev_path = self._volops.get_disk_resource_path(
                    dev['connection_info'])
                boot_order.append(dev_path)
            else:
                boot_order.append(dev['path'])

        return boot_order
Ejemplo n.º 22
0
def volume_in_mapping(mount_device, block_device_info):
    block_device_list = [strip_dev(vol['mount_device'])
                         for vol in
                         driver.block_device_info_get_mapping(
                         block_device_info)]

    swap = driver.block_device_info_get_swap(block_device_info)
    if driver.swap_is_usable(swap):
        block_device_list.append(strip_dev(swap['device_name']))

    block_device_list += [strip_dev(ephemeral['device_name'])
                          for ephemeral in
                          driver.block_device_info_get_ephemerals(
                          block_device_info)]

    LOG.debug("block_device_list %s", sorted(filter(None, block_device_list)))
    return strip_dev(mount_device) in block_device_list
    def _get_boot_order_gen2(self, block_device_info):
        devices = [block_device_info['root_disk']]
        devices += driver.block_device_info_get_ephemerals(
            block_device_info)
        devices += driver.block_device_info_get_mapping(block_device_info)

        self._sort_by_boot_order(devices)

        boot_order = []
        for dev in devices:
            if dev.get('connection_info'):
                dev_path = self._volops.get_disk_resource_path(
                    dev['connection_info'])
                boot_order.append(dev_path)
            else:
                boot_order.append(dev['path'])

        return boot_order
Ejemplo n.º 24
0
def _ephemeral_storage(instance, client, __, block_info):
    instance_attributes = common.InstanceAttributes(instance)
    ephemeral_storage = driver.block_device_info_get_ephemerals(block_info)
    if ephemeral_storage:
        devices = {}
        for ephemeral in ephemeral_storage:
            ephemeral_src = os.path.join(instance_attributes.storage_path,
                                         ephemeral['virtual_name'])
            device = {
                'path': '/mnt',
                'source': ephemeral_src,
                'type': 'disk',
            }
            if CONF.lxd.pool:
                extensions = client.host_info.get('api_extensions', [])
                if 'storage' in extensions:
                    device['pool'] = CONF.lxd.pool
                else:
                    msg = _("Host does not have storage pool support")
                    raise exception.NovaException(msg)
            devices[ephemeral['virtual_name']] = device
        return devices
Ejemplo n.º 25
0
def _ephemeral_storage(instance, client, __, block_info):
    instance_attributes = common.InstanceAttributes(instance)
    ephemeral_storage = driver.block_device_info_get_ephemerals(block_info)
    if ephemeral_storage:
        devices = {}
        for ephemeral in ephemeral_storage:
            ephemeral_src = os.path.join(
                instance_attributes.storage_path,
                ephemeral['virtual_name'])
            device = {
                'path': '/mnt',
                'source': ephemeral_src,
                'type': 'disk',
            }
            if CONF.lxd.pool:
                extensions = client.host_info.get('api_extensions', [])
                if 'storage' in extensions:
                    device['pool'] = CONF.lxd.pool
                else:
                    msg = _("Host does not have storage pool support")
                    raise exception.NovaException(msg)
            devices[ephemeral['virtual_name']] = device
        return devices
Ejemplo n.º 26
0
def detach_ephemeral(block_device_info, lxd_config, instance):
    """Detach ephemeral device from the instance."""
    ephemeral_storage = driver.block_device_info_get_ephemerals(
        block_device_info)
    if ephemeral_storage:
        storage_driver = lxd_config['environment']['storage']

        for ephemeral in ephemeral_storage:
            if storage_driver == 'zfs':
                zfs_pool = lxd_config['config']['storage.zfs_pool_name']

                utils.execute(
                    'zfs', 'destroy',
                    '%s/%s-ephemeral' % (zfs_pool, instance.name),
                    run_as_root=True)
            if storage_driver == 'lvm':
                lvm_pool = lxd_config['config']['storage.lvm_vg_name']

                lvm_path = '/dev/%s/%s-%s' % (
                    lvm_pool, instance.name, ephemeral['virtual_name'])

                utils.execute('umount', lvm_path, run_as_root=True)
                utils.execute('lvremove', '-f', lvm_path, run_as_root=True)
Ejemplo n.º 27
0
def _get_disk_mapping(virt_type, instance, disk_bus, cdrom_bus, image_meta,
                      block_device_info):
    """Build disk mapping for a given instance

    :param virt_type: Virt type used by libvirt.
    :param instance: nova.objects.instance.Instance object
    :param disk_bus: Disk bus to use within the mapping
    :param cdrom_bus: CD-ROM bus to use within the mapping
    :param image_meta: objects.image_meta.ImageMeta for the instance
    :param block_device_info: dict detailing disks and volumes attached

    :returns: Disk mapping for the given instance.
    """
    mapping = {}
    pre_assigned_device_names = \
    [block_device.strip_dev(get_device_name(bdm)) for bdm in itertools.chain(
        driver.block_device_info_get_ephemerals(block_device_info),
        [driver.block_device_info_get_swap(block_device_info)],
        driver.block_device_info_get_mapping(block_device_info))
     if get_device_name(bdm)]

    # NOTE (ndipanov): root_bdm can be None when we boot from image
    # as there is no driver representation of local targeted images
    # and they will not be in block_device_info list.
    root_bdm = block_device.get_root_bdm(
        driver.block_device_info_get_mapping(block_device_info))

    root_device_name = block_device.strip_dev(
        driver.block_device_info_get_root_device(block_device_info))
    root_info = get_root_info(
        instance, virt_type, image_meta, root_bdm,
        disk_bus, cdrom_bus, root_device_name)

    mapping['root'] = root_info
    # NOTE (ndipanov): This implicitly relies on image->local BDMs not
    #                  being considered in the driver layer - so missing
    #                  bdm with boot_index 0 means - use image, unless it was
    #                  overridden. This can happen when using legacy syntax and
    #                  no root_device_name is set on the instance.
    if not root_bdm and not block_device.volume_in_mapping(root_info['dev'],
                                                           block_device_info):
        mapping['disk'] = root_info
    elif root_bdm:
        # NOTE (ft): If device name is not set in root bdm, root_info has a
        # generated one. We have to copy device name to root bdm to prevent its
        # second generation in loop through bdms. If device name is already
        # set, nothing is changed.
        update_bdm(root_bdm, root_info)

    default_eph = get_default_ephemeral_info(instance, disk_bus,
                                             block_device_info, mapping)
    if default_eph:
        mapping['disk.local'] = default_eph

    for idx, eph in enumerate(driver.block_device_info_get_ephemerals(
            block_device_info)):
        eph_info = get_info_from_bdm(
            instance, virt_type, image_meta, eph, mapping, disk_bus,
            assigned_devices=pre_assigned_device_names)
        mapping[get_eph_disk(idx)] = eph_info
        update_bdm(eph, eph_info)

    swap = driver.block_device_info_get_swap(block_device_info)
    if swap and swap.get('swap_size', 0) > 0:
        swap_info = get_info_from_bdm(
            instance, virt_type, image_meta,
            swap, mapping, disk_bus)
        mapping['disk.swap'] = swap_info
        update_bdm(swap, swap_info)
    elif instance.get_flavor()['swap'] > 0:
        swap_info = get_next_disk_info(mapping, disk_bus,
            assigned_devices=pre_assigned_device_names)
        if not block_device.volume_in_mapping(swap_info['dev'],
                                              block_device_info):
            mapping['disk.swap'] = swap_info

    block_device_mapping = driver.block_device_info_get_mapping(
        block_device_info)

    for bdm in block_device_mapping:
        vol_info = get_info_from_bdm(
            instance, virt_type, image_meta, bdm, mapping,
            assigned_devices=pre_assigned_device_names)
        mapping[block_device.prepend_dev(vol_info['dev'])] = vol_info
        update_bdm(bdm, vol_info)

    if configdrive.required_by(instance):
        device_type = get_config_drive_type()
        disk_bus = get_disk_bus_for_device_type(instance,
                                                virt_type,
                                                image_meta,
                                                device_type)
        config_info = get_next_disk_info(mapping,
                                         disk_bus,
                                         device_type)
        mapping['disk.config'] = config_info
    return mapping
Ejemplo n.º 28
0
def attach_ephemeral(client, block_device_info, lxd_config, instance):
    """Attach ephemeral storage to an instance."""
    ephemeral_storage = driver.block_device_info_get_ephemerals(
        block_device_info)
    if ephemeral_storage:
        storage_driver = lxd_config['environment']['storage']

        container = client.containers.get(instance.name)
        container_id_map = container.config[
            'volatile.last_state.idmap'].split(',')
        storage_id = container_id_map[2].split(':')[1]

        instance_attrs = common.InstanceAttributes(instance)
        for ephemeral in ephemeral_storage:
            storage_dir = os.path.join(
                instance_attrs.storage_path, ephemeral['virtual_name'])
            if storage_driver == 'zfs':
                # NOTE(ajkavanagh) - BUG/1782329 - this is temporary until
                # storage pools is implemented.  LXD 3 removed the
                # storage.zfs_pool_name key from the config.  So, if it fails,
                # we need to grab the configured storage pool and use that as
                # the name instead.
                try:
                    zfs_pool = lxd_config['config']['storage.zfs_pool_name']
                except KeyError:
                    zfs_pool = CONF.lxd.pool

                utils.execute(
                    'zfs', 'create',
                    '-o', 'mountpoint=%s' % storage_dir,
                    '-o', 'quota=%sG' % instance.ephemeral_gb,
                          '%s/%s-ephemeral' % (zfs_pool, instance.name),
                    run_as_root=True)
            elif storage_driver == 'btrfs':
                # We re-use the same btrfs subvolumes that LXD uses,
                # so the ephemeral storage path is updated in the profile
                # before the container starts.
                storage_dir = os.path.join(
                    instance_attrs.container_path, ephemeral['virtual_name'])
                profile = client.profiles.get(instance.name)
                storage_name = ephemeral['virtual_name']
                profile.devices[storage_name]['source'] = storage_dir
                profile.save()

                utils.execute(
                    'btrfs', 'subvolume', 'create', storage_dir,
                    run_as_root=True)
                utils.execute(
                    'btrfs', 'qgroup', 'limit',
                    '%sg' % instance.ephemeral_gb, storage_dir,
                    run_as_root=True)
            elif storage_driver == 'lvm':
                fileutils.ensure_tree(storage_dir)

                lvm_pool = lxd_config['config']['storage.lvm_vg_name']
                lvm_volume = '%s-%s' % (instance.name,
                                        ephemeral['virtual_name'])
                lvm_path = '/dev/%s/%s' % (lvm_pool, lvm_volume)

                cmd = (
                    'lvcreate', '-L', '%sG' % instance.ephemeral_gb,
                    '-n', lvm_volume, lvm_pool)
                utils.execute(*cmd, run_as_root=True, attempts=3)

                utils.execute('mkfs', '-t', 'ext4',
                              lvm_path, run_as_root=True)
                cmd = ('mount', '-t', 'ext4', lvm_path, storage_dir)
                utils.execute(*cmd, run_as_root=True)
            else:
                reason = _("Unsupport LXD storage detected. Supported"
                           " storage drivers are zfs and btrfs.")
                raise exception.NovaException(reason)

            utils.execute(
                'chown', storage_id,
                storage_dir, run_as_root=True)
 def _check_and_update_ephemerals(self, vm_gen, block_device_info,
                                  slot_map):
     ephemerals = driver.block_device_info_get_ephemerals(block_device_info)
     for eph in ephemerals:
         self._check_and_update_bdm(slot_map, vm_gen, eph)
def get_disk_mapping(virt_type, instance,
                     disk_bus, cdrom_bus,
                     image_meta,
                     block_device_info=None,
                     rescue=False):
    """Determine how to map default disks to the virtual machine.

       This is about figuring out whether the default 'disk',
       'disk.local', 'disk.swap' and 'disk.config' images have
       been overridden by the block device mapping.

       Returns the guest disk mapping for the devices.
    """

    mapping = {}

    if rescue:
        rescue_info = get_next_disk_info(mapping,
                                         disk_bus, boot_index=1)
        mapping['disk.rescue'] = rescue_info
        mapping['root'] = rescue_info

        os_info = get_next_disk_info(mapping,
                                     disk_bus)
        mapping['disk'] = os_info

        return mapping

    inst_type = instance.get_flavor()

    pre_assigned_device_names = \
    [block_device.strip_dev(get_device_name(bdm)) for bdm in itertools.chain(
        driver.block_device_info_get_ephemerals(block_device_info),
        [driver.block_device_info_get_swap(block_device_info)],
        driver.block_device_info_get_mapping(block_device_info))
     if get_device_name(bdm)]

    # NOTE (ndipanov): root_bdm can be None when we boot from image
    # as there is no driver represenation of local targeted images
    # and they will not be in block_device_info list.
    root_bdm = block_device.get_root_bdm(
        driver.block_device_info_get_mapping(block_device_info))

    root_device_name = block_device.strip_dev(
        driver.block_device_info_get_root(block_device_info))
    root_info = get_root_info(
        instance, virt_type, image_meta, root_bdm,
        disk_bus, cdrom_bus, root_device_name)

    mapping['root'] = root_info
    # NOTE (ndipanov): This implicitly relies on image->local BDMs not
    #                  being considered in the driver layer - so missing
    #                  bdm with boot_index 0 means - use image, unless it was
    #                  overridden. This can happen when using legacy syntax and
    #                  no root_device_name is set on the instance.
    if not root_bdm and not block_device.volume_in_mapping(root_info['dev'],
                                                           block_device_info):
        mapping['disk'] = root_info
    elif root_bdm:
        # NOTE (ft): If device name is not set in root bdm, root_info has a
        # generated one. We have to copy device name to root bdm to prevent its
        # second generation in loop through bdms. If device name is already
        # set, nothing is changed.
        update_bdm(root_bdm, root_info)

    default_eph = has_default_ephemeral(instance, disk_bus, block_device_info,
                                        mapping)
    if default_eph:
        mapping['disk.local'] = default_eph

    for idx, eph in enumerate(driver.block_device_info_get_ephemerals(
            block_device_info)):
        eph_info = get_info_from_bdm(
            instance, virt_type, image_meta, eph, mapping, disk_bus,
            assigned_devices=pre_assigned_device_names)
        mapping[get_eph_disk(idx)] = eph_info
        update_bdm(eph, eph_info)

    swap = driver.block_device_info_get_swap(block_device_info)
    if swap and swap.get('swap_size', 0) > 0:
        swap_info = get_info_from_bdm(
            instance, virt_type, image_meta,
            swap, mapping, disk_bus)
        mapping['disk.swap'] = swap_info
        update_bdm(swap, swap_info)
    elif inst_type['swap'] > 0:
        swap_info = get_next_disk_info(mapping, disk_bus,
            assigned_devices=pre_assigned_device_names)
        if not block_device.volume_in_mapping(swap_info['dev'],
                                              block_device_info):
            mapping['disk.swap'] = swap_info

    block_device_mapping = driver.block_device_info_get_mapping(
        block_device_info)

    for vol in block_device_mapping:
        vol_info = get_info_from_bdm(
            instance, virt_type, image_meta, vol, mapping,
            assigned_devices=pre_assigned_device_names)
        mapping[block_device.prepend_dev(vol_info['dev'])] = vol_info
        update_bdm(vol, vol_info)

    if configdrive.required_by(instance):
        device_type = get_config_drive_type()
        disk_bus = get_disk_bus_for_device_type(instance,
                                                virt_type,
                                                image_meta,
                                                device_type)
        config_info = get_next_disk_info(mapping,
                                         disk_bus,
                                         device_type,
                                         last_device=True)
        mapping['disk.config'] = config_info

    return mapping
Ejemplo n.º 31
0
def attach_ephemeral(client, block_device_info, lxd_config, instance):
    """Attach ephemeral storage to an instance."""
    ephemeral_storage = driver.block_device_info_get_ephemerals(
        block_device_info)
    if ephemeral_storage:
        storage_driver = lxd_config['environment']['storage']

        container = client.containers.get(instance.name)
        container_id_map = container.config['volatile.last_state.idmap'].split(
            ',')
        storage_id = container_id_map[2].split(':')[1]

        instance_attrs = common.InstanceAttributes(instance)
        for ephemeral in ephemeral_storage:
            storage_dir = os.path.join(instance_attrs.storage_path,
                                       ephemeral['virtual_name'])
            if storage_driver == 'zfs':
                # NOTE(ajkavanagh) - BUG/1782329 - this is temporary until
                # storage pools is implemented.  LXD 3 removed the
                # storage.zfs_pool_name key from the config.  So, if it fails,
                # we need to grab the configured storage pool and use that as
                # the name instead.
                try:
                    zfs_pool = lxd_config['config']['storage.zfs_pool_name']
                except KeyError:
                    zfs_pool = CONF.lxd.pool

                utils.execute('zfs',
                              'create',
                              '-o',
                              'mountpoint=%s' % storage_dir,
                              '-o',
                              'quota=%sG' % instance.ephemeral_gb,
                              '%s/%s-ephemeral' % (zfs_pool, instance.name),
                              run_as_root=True)
            elif storage_driver == 'btrfs':
                # We re-use the same btrfs subvolumes that LXD uses,
                # so the ephemeral storage path is updated in the profile
                # before the container starts.
                storage_dir = os.path.join(instance_attrs.container_path,
                                           ephemeral['virtual_name'])
                profile = client.profiles.get(instance.name)
                storage_name = ephemeral['virtual_name']
                profile.devices[storage_name]['source'] = storage_dir
                profile.save()

                utils.execute('btrfs',
                              'subvolume',
                              'create',
                              storage_dir,
                              run_as_root=True)
                utils.execute('btrfs',
                              'qgroup',
                              'limit',
                              '%sg' % instance.ephemeral_gb,
                              storage_dir,
                              run_as_root=True)
            elif storage_driver == 'lvm':
                fileutils.ensure_tree(storage_dir)

                lvm_pool = lxd_config['config']['storage.lvm_vg_name']
                lvm_volume = '%s-%s' % (instance.name,
                                        ephemeral['virtual_name'])
                lvm_path = '/dev/%s/%s' % (lvm_pool, lvm_volume)

                cmd = ('lvcreate', '-L', '%sG' % instance.ephemeral_gb, '-n',
                       lvm_volume, lvm_pool)
                utils.execute(*cmd, run_as_root=True, attempts=3)

                utils.execute('mkfs', '-t', 'ext4', lvm_path, run_as_root=True)
                cmd = ('mount', '-t', 'ext4', lvm_path, storage_dir)
                utils.execute(*cmd, run_as_root=True)
            else:
                reason = _("Unsupport LXD storage detected. Supported"
                           " storage drivers are zfs and btrfs.")
                raise exception.NovaException(reason)

            utils.execute('chown', storage_id, storage_dir, run_as_root=True)
Ejemplo n.º 32
0
def get_disk_mapping(virt_type, instance,
                     disk_bus, cdrom_bus,
                     block_device_info=None,
                     image_meta=None, rescue=False):
    """Determine how to map default disks to the virtual machine.

       This is about figuring out whether the default 'disk',
       'disk.local', 'disk.swap' and 'disk.config' images have
       been overridden by the block device mapping.

       Returns the guest disk mapping for the devices.
    """

    inst_type = flavors.extract_flavor(instance)

    mapping = {}

    pre_assigned_device_names = \
    [block_device.strip_dev(get_device_name(bdm)) for bdm in itertools.chain(
        driver.block_device_info_get_ephemerals(block_device_info),
        [driver.block_device_info_get_swap(block_device_info)],
        driver.block_device_info_get_mapping(block_device_info))
     if get_device_name(bdm)]

    if virt_type == "lxc":
        # NOTE(zul): This information is not used by the libvirt driver
        # however we need to populate mapping so the image can be
        # created when the instance is started. This can
        # be removed when we convert LXC to use block devices.
        root_disk_bus = disk_bus
        root_device_type = 'disk'

        root_info = get_next_disk_info(mapping,
                                       root_disk_bus,
                                       root_device_type,
                                       boot_index=1)
        mapping['root'] = root_info
        mapping['disk'] = root_info

        return mapping

    if rescue:
        rescue_info = get_next_disk_info(mapping,
                                         disk_bus, boot_index=1)
        mapping['disk.rescue'] = rescue_info
        mapping['root'] = rescue_info

        os_info = get_next_disk_info(mapping,
                                     disk_bus)
        mapping['disk'] = os_info

        return mapping

    # NOTE (ndipanov): root_bdm can be None when we boot from image
    # as there is no driver represenation of local targeted images
    # and they will not be in block_device_info list.
    root_bdm = block_device.get_root_bdm(
        driver.block_device_info_get_mapping(block_device_info))

    root_device_name = block_device.strip_dev(
        driver.block_device_info_get_root(block_device_info))
    root_info = get_root_info(virt_type, image_meta, root_bdm,
                              disk_bus, cdrom_bus, root_device_name)

    mapping['root'] = root_info
    # NOTE (ndipanov): This implicitly relies on image->local BDMs not
    #                  being considered in the driver layer - so missing
    #                  bdm with boot_index 0 means - use image, unless it was
    #                  overridden. This can happen when using legacy syntax and
    #                  no root_device_name is set on the instance.
    if not root_bdm and not block_device.volume_in_mapping(root_info['dev'],
                                                           block_device_info):
        mapping['disk'] = root_info

    default_eph = has_default_ephemeral(instance, disk_bus, block_device_info,
                                        mapping)
    if default_eph:
        mapping['disk.local'] = default_eph

    for idx, eph in enumerate(driver.block_device_info_get_ephemerals(
            block_device_info)):
        eph_info = get_info_from_bdm(
            virt_type, eph, mapping, disk_bus,
            assigned_devices=pre_assigned_device_names)
        mapping[get_eph_disk(idx)] = eph_info
        update_bdm(eph, eph_info)

    swap = driver.block_device_info_get_swap(block_device_info)
    if swap and swap.get('swap_size', 0) > 0:
        swap_info = get_info_from_bdm(virt_type, swap, mapping, disk_bus)
        mapping['disk.swap'] = swap_info
        update_bdm(swap, swap_info)
    elif inst_type['swap'] > 0:
        swap_info = get_next_disk_info(mapping,
                                       disk_bus)
        if not block_device.volume_in_mapping(swap_info['dev'],
                                              block_device_info):
            mapping['disk.swap'] = swap_info

    block_device_mapping = driver.block_device_info_get_mapping(
        block_device_info)

    for vol in block_device_mapping:
        vol_info = get_info_from_bdm(
            virt_type, vol, mapping,
            assigned_devices=pre_assigned_device_names)
        mapping[block_device.prepend_dev(vol_info['dev'])] = vol_info
        update_bdm(vol, vol_info)

    if configdrive.required_by(instance):
        device_type = get_config_drive_type()
        disk_bus = get_disk_bus_for_device_type(virt_type,
                                                image_meta,
                                                device_type)
        config_info = get_next_disk_info(mapping,
                                         disk_bus,
                                         device_type,
                                         last_device=True)
        mapping['disk.config'] = config_info

    return mapping
Ejemplo n.º 33
0
def get_disk_mapping(virt_type, instance,
                     disk_bus, cdrom_bus,
                     block_device_info=None,
                     image_meta=None, rescue=False):
    """Determine how to map default disks to the virtual machine.

       This is about figuring out whether the default 'disk',
       'disk.local', 'disk.swap' and 'disk.config' images have
       been overridden by the block device mapping.

       Returns the guest disk mapping for the devices.
    """

    inst_type = flavors.extract_flavor(instance)

    mapping = {}

    pre_assigned_device_names = \
    [block_device.strip_dev(get_device_name(bdm)) for bdm in itertools.chain(
        driver.block_device_info_get_ephemerals(block_device_info),
        [driver.block_device_info_get_swap(block_device_info)],
        driver.block_device_info_get_mapping(block_device_info))
     if get_device_name(bdm)]

    if rescue:
        rescue_info = get_next_disk_info(mapping,
                                         disk_bus, boot_index=1)
        mapping['disk.rescue'] = rescue_info
        mapping['root'] = rescue_info

        os_info = get_next_disk_info(mapping,
                                     disk_bus)
        mapping['disk'] = os_info

        return mapping

    # NOTE (ndipanov): root_bdm can be None when we boot from image
    # as there is no driver represenation of local targeted images
    # and they will not be in block_device_info list.
    root_bdm = block_device.get_root_bdm(
        driver.block_device_info_get_mapping(block_device_info))

    root_device_name = block_device.strip_dev(
        driver.block_device_info_get_root(block_device_info))
    root_info = get_root_info(virt_type, image_meta, root_bdm,
                              disk_bus, cdrom_bus, root_device_name)

    mapping['root'] = root_info
    # NOTE (ndipanov): This implicitly relies on image->local BDMs not
    #                  being considered in the driver layer - so missing
    #                  bdm with boot_index 0 means - use image, unless it was
    #                  overridden. This can happen when using legacy syntax and
    #                  no root_device_name is set on the instance.
    if not root_bdm and not block_device.volume_in_mapping(root_info['dev'],
                                                           block_device_info):
        mapping['disk'] = root_info

    default_eph = has_default_ephemeral(instance, disk_bus, block_device_info,
                                        mapping)
    if default_eph:
        mapping['disk.local'] = default_eph

    for idx, eph in enumerate(driver.block_device_info_get_ephemerals(
            block_device_info)):
        eph_info = get_info_from_bdm(
            virt_type, eph, mapping, disk_bus,
            assigned_devices=pre_assigned_device_names)
        mapping[get_eph_disk(idx)] = eph_info
        update_bdm(eph, eph_info)

    swap = driver.block_device_info_get_swap(block_device_info)
    if swap and swap.get('swap_size', 0) > 0:
        swap_info = get_info_from_bdm(virt_type, swap, mapping, disk_bus)
        mapping['disk.swap'] = swap_info
        update_bdm(swap, swap_info)
    elif inst_type['swap'] > 0:
        swap_info = get_next_disk_info(mapping,
                                       disk_bus)
        if not block_device.volume_in_mapping(swap_info['dev'],
                                              block_device_info):
            mapping['disk.swap'] = swap_info

    block_device_mapping = driver.block_device_info_get_mapping(
        block_device_info)

    for vol in block_device_mapping:
        vol_info = get_info_from_bdm(
            virt_type, vol, mapping,
            assigned_devices=pre_assigned_device_names)
        mapping[block_device.prepend_dev(vol_info['dev'])] = vol_info
        update_bdm(vol, vol_info)

    if configdrive.required_by(instance):
        device_type = get_config_drive_type()
        disk_bus = get_disk_bus_for_device_type(virt_type,
                                                image_meta,
                                                device_type)
        config_info = get_next_disk_info(mapping,
                                         disk_bus,
                                         device_type,
                                         last_device=True)
        mapping['disk.config'] = config_info

    return mapping
Ejemplo n.º 34
0
 def _check_and_update_ephemerals(self, vm_gen, block_device_info,
                                  slot_map):
     ephemerals = driver.block_device_info_get_ephemerals(block_device_info)
     for eph in ephemerals:
         self._check_and_update_bdm(slot_map, vm_gen, eph)
Ejemplo n.º 35
0
def get_disk_mapping(virt_type,
                     instance,
                     disk_bus,
                     cdrom_bus,
                     block_device_info=None,
                     image_meta=None,
                     rescue=False):
    """Determine how to map default disks to the virtual machine.

       This is about figuring out whether the default 'disk',
       'disk.local', 'disk.swap' and 'disk.config' images have
       been overriden by the block device mapping.

       Returns the guest disk mapping for the devices."""

    inst_type = flavors.extract_instance_type(instance)

    mapping = {}

    if virt_type == "lxc":
        # NOTE(zul): This information is not used by the libvirt driver
        # however we need to populate mapping so the image can be
        # created when the instance is started. This can
        # be removed when we convert LXC to use block devices.
        root_disk_bus = disk_bus
        root_device_type = 'disk'

        root_info = get_next_disk_info(mapping, root_disk_bus,
                                       root_device_type)
        mapping['root'] = root_info
        mapping['disk'] = root_info

        return mapping

    if rescue:
        rescue_info = get_next_disk_info(mapping, disk_bus)
        mapping['disk.rescue'] = rescue_info
        mapping['root'] = rescue_info

        os_info = get_next_disk_info(mapping, disk_bus)
        mapping['disk'] = os_info

        return mapping

    if image_meta and image_meta.get('disk_format') == 'iso':
        root_disk_bus = cdrom_bus
        root_device_type = 'cdrom'
    else:
        root_disk_bus = disk_bus
        root_device_type = 'disk'

    root_device_name = driver.block_device_info_get_root(block_device_info)
    if root_device_name is not None:
        root_device = block_device.strip_dev(root_device_name)
        root_info = {
            'bus': get_disk_bus_for_disk_dev(virt_type, root_device),
            'dev': root_device,
            'type': root_device_type
        }
    else:
        root_info = get_next_disk_info(mapping, root_disk_bus,
                                       root_device_type)
    mapping['root'] = root_info
    if not block_device.volume_in_mapping(root_info['dev'], block_device_info):
        mapping['disk'] = root_info

    eph_info = get_next_disk_info(mapping, disk_bus)
    ephemeral_device = False
    if not (block_device.volume_in_mapping(eph_info['dev'], block_device_info)
            or 0 in [
                eph['num'] for eph in driver.block_device_info_get_ephemerals(
                    block_device_info)
            ]):
        if instance['ephemeral_gb'] > 0:
            ephemeral_device = True

    if ephemeral_device:
        mapping['disk.local'] = eph_info

    for eph in driver.block_device_info_get_ephemerals(block_device_info):
        disk_dev = block_device.strip_dev(eph['device_name'])
        disk_bus = get_disk_bus_for_disk_dev(virt_type, disk_dev)

        mapping[get_eph_disk(eph)] = {
            'bus': disk_bus,
            'dev': disk_dev,
            'type': 'disk'
        }

    swap = driver.block_device_info_get_swap(block_device_info)
    if driver.swap_is_usable(swap):
        disk_dev = block_device.strip_dev(swap['device_name'])
        disk_bus = get_disk_bus_for_disk_dev(virt_type, disk_dev)

        mapping['disk.swap'] = {
            'bus': disk_bus,
            'dev': disk_dev,
            'type': 'disk'
        }
    elif inst_type['swap'] > 0:
        swap_info = get_next_disk_info(mapping, disk_bus)
        if not block_device.volume_in_mapping(swap_info['dev'],
                                              block_device_info):
            mapping['disk.swap'] = swap_info

    block_device_mapping = driver.block_device_info_get_mapping(
        block_device_info)

    for vol in block_device_mapping:
        disk_dev = vol['mount_device'].rpartition("/")[2]
        disk_bus = get_disk_bus_for_disk_dev(virt_type, disk_dev)

        mapping[vol['mount_device']] = {
            'bus': disk_bus,
            'dev': disk_dev,
            'type': 'disk'
        }

    if configdrive.enabled_for(instance):
        config_info = get_next_disk_info(mapping, disk_bus, last_device=True)
        mapping['disk.config'] = config_info

    return mapping
Ejemplo n.º 36
0
def get_disk_mapping(virt_type, instance,
                     disk_bus, cdrom_bus,
                     block_device_info=None,
                     image_meta=None, rescue=False):
    """Determine how to map default disks to the virtual machine.

       This is about figuring out whether the default 'disk',
       'disk.local', 'disk.swap' and 'disk.config' images have
       been overriden by the block device mapping.

       Returns the guest disk mapping for the devices.
    """

    inst_type = flavors.extract_flavor(instance)

    mapping = {}

    if virt_type == "lxc":
        # NOTE(zul): This information is not used by the libvirt driver
        # however we need to populate mapping so the image can be
        # created when the instance is started. This can
        # be removed when we convert LXC to use block devices.
        root_disk_bus = disk_bus
        root_device_type = 'disk'

        root_info = get_next_disk_info(mapping,
                                       root_disk_bus,
                                       root_device_type)
        mapping['root'] = root_info
        mapping['disk'] = root_info

        return mapping

    if rescue:
        rescue_info = get_next_disk_info(mapping,
                                         disk_bus)
        mapping['disk.rescue'] = rescue_info
        mapping['root'] = rescue_info

        os_info = get_next_disk_info(mapping,
                                     disk_bus)
        mapping['disk'] = os_info

        return mapping

    if image_meta and image_meta.get('disk_format') == 'iso':
        root_disk_bus = cdrom_bus
        root_device_type = 'cdrom'
    else:
        root_disk_bus = disk_bus
        root_device_type = 'disk'

    root_device_name = driver.block_device_info_get_root(block_device_info)
    if root_device_name is not None:
        root_device = block_device.strip_dev(root_device_name)
        root_info = {'bus': get_disk_bus_for_disk_dev(virt_type,
                                                      root_device),
                     'dev': root_device,
                     'type': root_device_type}
    else:
        root_info = get_next_disk_info(mapping,
                                       root_disk_bus,
                                       root_device_type)
    mapping['root'] = root_info
    if not block_device.volume_in_mapping(root_info['dev'],
                                          block_device_info):
        mapping['disk'] = root_info

    eph_info = get_next_disk_info(mapping,
                                  disk_bus)
    ephemeral_device = False
    if not (block_device.volume_in_mapping(eph_info['dev'],
                                           block_device_info) or
            0 in [eph['num'] for eph in
                  driver.block_device_info_get_ephemerals(
                block_device_info)]):
        if instance['ephemeral_gb'] > 0:
            ephemeral_device = True

    if ephemeral_device:
        mapping['disk.local'] = eph_info

    for eph in driver.block_device_info_get_ephemerals(
            block_device_info):
        disk_dev = block_device.strip_dev(eph['device_name'])
        disk_bus = get_disk_bus_for_disk_dev(virt_type, disk_dev)

        mapping[get_eph_disk(eph)] = {'bus': disk_bus,
                                      'dev': disk_dev,
                                      'type': 'disk'}

    swap = driver.block_device_info_get_swap(block_device_info)
    if driver.swap_is_usable(swap):
        disk_dev = block_device.strip_dev(swap['device_name'])
        disk_bus = get_disk_bus_for_disk_dev(virt_type, disk_dev)

        mapping['disk.swap'] = {'bus': disk_bus,
                                'dev': disk_dev,
                                'type': 'disk'}
    elif inst_type['swap'] > 0:
        swap_info = get_next_disk_info(mapping,
                                       disk_bus)
        if not block_device.volume_in_mapping(swap_info['dev'],
                                              block_device_info):
            mapping['disk.swap'] = swap_info

    block_device_mapping = driver.block_device_info_get_mapping(
        block_device_info)

    for vol in block_device_mapping:
        disk_dev = vol['mount_device'].rpartition("/")[2]
        disk_bus = get_disk_bus_for_disk_dev(virt_type, disk_dev)

        mapping[vol['mount_device']] = {'bus': disk_bus,
                                        'dev': disk_dev,
                                        'type': 'disk'}

    if configdrive.required_by(instance):
        config_info = get_next_disk_info(mapping,
                                         disk_bus,
                                         last_device=True)
        mapping['disk.config'] = config_info

    return mapping