예제 #1
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)])
예제 #2
0
파일: volumeutils.py 프로젝트: hadib/nova
    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
예제 #3
0
파일: blockinfo.py 프로젝트: AsherBond/nova
def get_root_info(virt_type, image_meta, root_bdm, disk_bus, cdrom_bus,
                  root_device_name=None):

    # NOTE (ndipanov): This is a hack to avoid considering an image
    #                  BDM with local target, as we don't support them
    #                  yet. Only applies when passed non-driver format
    no_root_bdm = (not root_bdm or (
        root_bdm.get('source_type') == 'image' and
        root_bdm.get('destination_type') == 'local'))
    if no_root_bdm:
        if (image_meta and image_meta.get('disk_format') == 'iso'):
            root_device_bus = cdrom_bus
            root_device_type = 'cdrom'
        else:
            root_device_bus = disk_bus
            root_device_type = 'disk'
        if root_device_name:
            root_device_bus = get_disk_bus_for_disk_dev(virt_type,
                                                        root_device_name)
        else:
            root_device_name = find_disk_dev_for_disk_bus({}, root_device_bus)

        return {'bus': root_device_bus,
                'type': root_device_type,
                'dev': block_device.strip_dev(root_device_name),
                'boot_index': '1'}
    else:
        if not get_device_name(root_bdm) and root_device_name:
            root_bdm = root_bdm.copy()
            root_bdm['device_name'] = root_device_name
        return get_info_from_bdm(virt_type, root_bdm, {}, disk_bus)
예제 #4
0
def get_root_info(virt_type, image_meta, root_bdm, disk_bus, cdrom_bus, root_device_name=None):

    # NOTE (ndipanov): This is a hack to avoid considering an image
    #                  BDM with local target, as we don't support them
    #                  yet. Only applies when passed non-driver format
    no_root_bdm = not root_bdm or (
        root_bdm.get("source_type") == "image" and root_bdm.get("destination_type") == "local"
    )
    if no_root_bdm:
        if image_meta and image_meta.get("disk_format") == "iso":
            root_device_bus = cdrom_bus
            root_device_type = "cdrom"
        else:
            root_device_bus = disk_bus
            root_device_type = "disk"
        if root_device_name:
            root_device_bus = get_disk_bus_for_disk_dev(virt_type, root_device_name)
        else:
            root_device_name = find_disk_dev_for_disk_bus({}, root_device_bus)

        return {
            "bus": root_device_bus,
            "type": root_device_type,
            "dev": block_device.strip_dev(root_device_name),
            "boot_index": "1",
        }
    else:
        if not get_device_name(root_bdm) and root_device_name:
            root_bdm = root_bdm.copy()
            root_bdm["device_name"] = root_device_name
        return get_info_from_bdm(virt_type, root_bdm, {}, disk_bus)
예제 #5
0
def get_info_from_bdm(instance,
                      virt_type,
                      image_meta,
                      bdm,
                      mapping=None,
                      disk_bus=None,
                      dev_type=None,
                      allowed_types=None,
                      assigned_devices=None):
    mapping = mapping or {}
    allowed_types = allowed_types or SUPPORTED_DEVICE_TYPES
    device_name = block_device.strip_dev(get_device_name(bdm))

    bdm_type = bdm.get('device_type') or dev_type
    if bdm_type not in allowed_types:
        bdm_type = 'disk'

    bdm_bus = bdm.get('disk_bus') or disk_bus
    if not is_disk_bus_valid_for_virt(virt_type, bdm_bus):
        if device_name:
            bdm_bus = get_disk_bus_for_disk_dev(virt_type, device_name)
        else:
            bdm_bus = get_disk_bus_for_device_type(instance, virt_type,
                                                   image_meta, bdm_type)

    if not device_name:
        if assigned_devices:
            padded_mapping = {dev: {'dev': dev} for dev in assigned_devices}
            padded_mapping.update(mapping)
        else:
            padded_mapping = mapping

        device_name = find_disk_dev_for_disk_bus(padded_mapping, bdm_bus)

    bdm_info = {'bus': bdm_bus, 'dev': device_name, 'type': bdm_type}

    bdm_format = bdm.get('guest_format')
    if bdm_format:
        bdm_info.update({'format': bdm_format})

    boot_index = bdm.get('boot_index')
    if boot_index is not None and boot_index >= 0:
        # NOTE(ndipanov): libvirt starts ordering from 1, not 0
        bdm_info['boot_index'] = str(boot_index + 1)

    return bdm_info
예제 #6
0
파일: handler.py 프로젝트: KarimAllah/nova
    def _format_instance_mapping(self, ctxt, instance_ref):
        root_device_name = instance_ref['root_device_name']
        if root_device_name is None:
            return _DEFAULT_MAPPINGS

        mappings = {}
        mappings['ami'] = block_device.strip_dev(root_device_name)
        mappings['root'] = root_device_name
        default_ephemeral_device = \
                instance_ref.get('default_ephemeral_device')
        if default_ephemeral_device:
            mappings['ephemeral0'] = default_ephemeral_device
        default_swap_device = instance_ref.get('default_swap_device')
        if default_swap_device:
            mappings['swap'] = default_swap_device
        ebs_devices = []

        # 'ephemeralN', 'swap' and ebs
        for bdm in db.block_device_mapping_get_all_by_instance(
            ctxt, instance_ref['id']):
            if bdm['no_device']:
                continue

            # ebs volume case
            if (bdm['volume_id'] or bdm['snapshot_id']):
                ebs_devices.append(bdm['device_name'])
                continue

            virtual_name = bdm['virtual_name']
            if not virtual_name:
                continue

            if block_device.is_swap_or_ephemeral(virtual_name):
                mappings[virtual_name] = bdm['device_name']

        # NOTE(yamahata): I'm not sure how ebs device should be numbered.
        #                 Right now sort by device name for deterministic
        #                 result.
        if ebs_devices:
            nebs = 0
            ebs_devices.sort()
            for ebs in ebs_devices:
                mappings['ebs%d' % nebs] = ebs
                nebs += 1

        return mappings
예제 #7
0
파일: base.py 프로젝트: nati/nova
def _format_instance_mapping(ctxt, instance):
    root_device_name = instance["root_device_name"]
    if root_device_name is None:
        return _DEFAULT_MAPPINGS

    mappings = {}
    mappings["ami"] = block_device.strip_dev(root_device_name)
    mappings["root"] = root_device_name
    default_ephemeral_device = instance.get("default_ephemeral_device")
    if default_ephemeral_device:
        mappings["ephemeral0"] = default_ephemeral_device
    default_swap_device = instance.get("default_swap_device")
    if default_swap_device:
        mappings["swap"] = default_swap_device
    ebs_devices = []

    # 'ephemeralN', 'swap' and ebs
    for bdm in db.block_device_mapping_get_all_by_instance(ctxt, instance["uuid"]):
        if bdm["no_device"]:
            continue

        # ebs volume case
        if bdm["volume_id"] or bdm["snapshot_id"]:
            ebs_devices.append(bdm["device_name"])
            continue

        virtual_name = bdm["virtual_name"]
        if not virtual_name:
            continue

        if block_device.is_swap_or_ephemeral(virtual_name):
            mappings[virtual_name] = bdm["device_name"]

    # NOTE(yamahata): I'm not sure how ebs device should be numbered.
    #                 Right now sort by device name for deterministic
    #                 result.
    if ebs_devices:
        nebs = 0
        ebs_devices.sort()
        for ebs in ebs_devices:
            mappings["ebs%d" % nebs] = ebs
            nebs += 1

    return mappings
예제 #8
0
파일: blockinfo.py 프로젝트: Juniper/nova
def get_root_info(instance, virt_type, image_meta, root_bdm,
                  disk_bus, cdrom_bus, root_device_name=None):

    # NOTE (ndipanov): This is a hack to avoid considering an image
    #                  BDM with local target, as we don't support them
    #                  yet. Only applies when passed non-driver format
    no_root_bdm = (not root_bdm or (
        root_bdm.get('source_type') == 'image' and
        root_bdm.get('destination_type') == 'local'))
    if no_root_bdm:
        # NOTE(mriedem): In case the image_meta object was constructed from
        # an empty dict, like in the case of evacuate, we have to first check
        # if disk_format is set on the ImageMeta object.
        if (image_meta.obj_attr_is_set('disk_format') and
                image_meta.disk_format == 'iso'):
            root_device_bus = cdrom_bus
            root_device_type = 'cdrom'
        else:
            root_device_bus = disk_bus
            root_device_type = 'disk'
        if root_device_name:
            root_device_bus = get_disk_bus_for_disk_dev(virt_type,
                                                        root_device_name)
        else:
            root_device_name = find_disk_dev_for_disk_bus({}, root_device_bus)

        return {'bus': root_device_bus,
                'type': root_device_type,
                'dev': block_device.strip_dev(root_device_name),
                'boot_index': '1'}

    if not get_device_name(root_bdm) and root_device_name:
        root_bdm = root_bdm.copy()
        # it can happen, eg for libvirt+Xen, that the root_device_name is
        # incompatible with the disk bus. In that case fix the root_device_name
        if virt_type == 'xen':
            dev_prefix = get_dev_prefix_for_disk_bus(disk_bus)
            if not root_device_name.startswith(dev_prefix):
                letter = block_device.get_device_letter(root_device_name)
                root_device_name = '%s%s' % (dev_prefix, letter)
        root_bdm['device_name'] = root_device_name
    return get_info_from_bdm(instance, virt_type, image_meta,
                             root_bdm, {}, disk_bus)
예제 #9
0
파일: blockinfo.py 프로젝트: dtroyer/nova
def get_info_from_bdm(
    virt_type, image_meta, bdm, mapping=None, disk_bus=None, dev_type=None, allowed_types=None, assigned_devices=None
):
    mapping = mapping or {}
    allowed_types = allowed_types or SUPPORTED_DEVICE_TYPES
    device_name = block_device.strip_dev(get_device_name(bdm))

    bdm_type = bdm.get("device_type") or dev_type
    if bdm_type not in allowed_types:
        bdm_type = "disk"

    bdm_bus = bdm.get("disk_bus") or disk_bus
    if not is_disk_bus_valid_for_virt(virt_type, bdm_bus):
        if device_name:
            bdm_bus = get_disk_bus_for_disk_dev(virt_type, device_name)
        else:
            bdm_bus = get_disk_bus_for_device_type(virt_type, image_meta, bdm_type)

    if not device_name:
        if assigned_devices:
            padded_mapping = {dev: {"dev": dev} for dev in assigned_devices}
            padded_mapping.update(mapping)
        else:
            padded_mapping = mapping

        device_name = find_disk_dev_for_disk_bus(padded_mapping, bdm_bus)

    bdm_info = {"bus": bdm_bus, "dev": device_name, "type": bdm_type}

    bdm_format = bdm.get("guest_format")
    if bdm_format:
        bdm_info.update({"format": bdm_format})

    boot_index = bdm.get("boot_index")
    if boot_index is not None and boot_index >= 0:
        # NOTE(ndipanov): libvirt starts ordering from 1, not 0
        bdm_info["boot_index"] = str(boot_index + 1)

    return bdm_info
예제 #10
0
def get_info_from_bdm(virt_type, bdm, mapping={}, disk_bus=None,
                      dev_type=None, allowed_types=None,
                      assigned_devices=None):
    allowed_types = allowed_types or SUPPORTED_DEVICE_TYPES
    device_name = block_device.strip_dev(get_device_name(bdm))

    bdm_type = bdm.get('device_type') or dev_type
    if bdm_type not in allowed_types:
        bdm_type = 'disk'

    bdm_bus = bdm.get('disk_bus') or disk_bus
    if not is_disk_bus_valid_for_virt(virt_type, bdm_bus):
        if device_name:
            bdm_bus = get_disk_bus_for_disk_dev(virt_type, device_name)
        else:
            bdm_bus = get_disk_bus_for_device_type(virt_type, None, bdm_type)

    if not device_name:
        if assigned_devices:
            padded_mapping = dict((dev, {'dev': dev})
                for dev in assigned_devices)
            padded_mapping.update(mapping)
        else:
            padded_mapping = mapping

        device_name = find_disk_dev_for_disk_bus(padded_mapping, bdm_bus)

    bdm_info = {'bus': bdm_bus,
                'dev': device_name,
                'type': bdm_type}

    bdm_format = bdm.get('guest_format')
    if bdm_format:
        bdm_info.update({'format': bdm_format})

    return bdm_info
예제 #11
0
def get_root_info(virt_type,
                  image_meta,
                  root_bdm,
                  disk_bus,
                  cdrom_bus,
                  root_device_name=None):

    # NOTE (ndipanov): This is a hack to avoid considering an image
    #                  BDM with local target, as we don't support them
    #                  yet. Only aplies when passed non-driver format
    no_root_bdm = (not root_bdm
                   or (root_bdm.get('source_type') == 'image'
                       and root_bdm.get('destination_type') == 'local'))
    if no_root_bdm:
        if (image_meta and image_meta.get('disk_format') == 'iso'):
            root_device_bus = cdrom_bus
            root_device_type = 'cdrom'
        else:
            root_device_bus = disk_bus
            root_device_type = 'disk'
        if root_device_name:
            root_device_bus = get_disk_bus_for_disk_dev(
                virt_type, root_device_name)
        else:
            root_device_name = find_disk_dev_for_disk_bus({}, root_device_bus)

        return {
            'bus': root_device_bus,
            'type': root_device_type,
            'dev': block_device.strip_dev(root_device_name)
        }
    else:
        if not get_device_name(root_bdm) and root_device_name:
            root_bdm = root_bdm.copy()
            root_bdm['device_name'] = root_device_name
        return get_info_from_bdm(virt_type, root_bdm, {})
예제 #12
0
파일: blockinfo.py 프로젝트: AsherBond/nova
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
예제 #13
0
 def test_strip_dev(self):
     self.assertEqual(block_device.strip_dev('/dev/sda'), 'sda')
     self.assertEqual(block_device.strip_dev('sda'), 'sda')
예제 #14
0
 def test_strip_dev(self):
     self.assertEqual('sda', block_device.strip_dev('/dev/sda'))
     self.assertEqual('sda', block_device.strip_dev('sda'))
     self.assertIsNone(block_device.strip_dev(None))
예제 #15
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
예제 #16
0
파일: blockinfo.py 프로젝트: yuans/nova
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
예제 #17
0
 def test_strip_dev(self):
     self.assertEqual(block_device.strip_dev('/dev/sda'), 'sda')
     self.assertEqual(block_device.strip_dev('sda'), 'sda')
예제 #18
0
 def test_strip_dev(self):
     self.assertEqual('sda', block_device.strip_dev('/dev/sda'))
     self.assertEqual('sda', block_device.strip_dev('sda'))
     self.assertIsNone(block_device.strip_dev(None))
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
예제 #20
0
파일: blockinfo.py 프로젝트: iBeacons/nova
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
def is_volume_root(root_device, mountpoint):
    """This judges if the moutpoint equals the root_device."""
    return block_device.strip_dev(mountpoint) == block_device.strip_dev(
                                                                root_device)
예제 #22
0
 def test_strip_dev(self):
     self.assertEqual(block_device.strip_dev("/dev/sda"), "sda")
     self.assertEqual(block_device.strip_dev("sda"), "sda")
예제 #23
0
def is_volume_root(root_device, mountpoint):
    """This judges if the moutpoint equals the root_device."""
    return block_device.strip_dev(mountpoint) == block_device.strip_dev(
        root_device)
예제 #24
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