Esempio n. 1
0
    def shrink_device(self, device_name, size):
        """Shrink the size of the device.

        :param device_name: a name of the device
        :param size: a new size in bytes
        """
        size = Size(size)
        device = self.storage.devicetree.get_device_by_name(device_name)

        if not device:
            raise UnknownDeviceError(device_name)

        if device.protected:
            raise ProtectedDeviceError(device_name)

        # The device size is small enough.
        if device.size <= size:
            log.debug("The size of %s is already %s.", device_name,
                      device.size)
            return

        # Resize the device.
        log.debug("Shrinking a size of %s to %s.", device_name, size)
        aligned_size = device.align_target_size(size)
        self.storage.resize_device(device, aligned_size)
Esempio n. 2
0
    def remove_device(self, device_name):
        """Remove a device after removing its dependent devices.

        If the device is protected, do nothing. If the device has
        protected children, just remove the unprotected ones.

        :param device_name: a name of the device
        """
        device = self.storage.devicetree.get_device_by_name(device_name)

        if not device:
            raise UnknownDeviceError(device_name)

        if device.protected:
            raise ProtectedDeviceError(device_name)

        # Only remove unprotected children if any protected.
        if any(d.protected for d in device.children):
            log.debug("Removing unprotected children of %s.", device_name)

            for child in (d for d in device.children if not d.protected):
                self.storage.recursive_remove(child)

            return

        # No protected children, remove the device
        log.debug("Removing device %s.", device_name)
        self.storage.recursive_remove(device)
Esempio n. 3
0
    def get_device_data(self, name):
        """Get the device data.

        :param name: a device name
        :return: an instance of DeviceData
        :raise: UnknownDeviceError if the device is not found
        """
        # Find the device.
        device = self._get_device(name)

        if not device:
            raise UnknownDeviceError(name)

        # Collect the device data.
        data = DeviceData()
        data.type = device.type
        data.name = device.name
        data.path = device.path
        data.size = device.size.get_bytes()
        data.is_disk = device.is_disk

        # Collect the additional attributes.
        attrs = self._get_device_attrs(device)
        data.attrs = attrs

        return data
Esempio n. 4
0
    def get_device_data(self, name):
        """Get the device data.

        :param name: a device name
        :return: an instance of DeviceData
        :raise: UnknownDeviceError if the device is not found
        """
        # Find the device.
        device = self._get_device(name)

        if not device:
            raise UnknownDeviceError(name)

        # Collect the device data.
        data = DeviceData()
        data.type = device.type
        data.name = device.name
        data.path = device.path
        data.size = device.size.get_bytes()
        data.parents = [d.name for d in device.parents]
        data.is_disk = device.is_disk

        # Get the device description.
        # FIXME: We should generate the description from the device data.
        data.description = getattr(device, "description", "")

        # Collect the additional attributes.
        attrs = self._get_device_attrs(device)
        data.attrs = attrs

        return data
Esempio n. 5
0
    def _get_device(self, name):
        """Find a device by its name.

        :param name: a name of the device
        :return: an instance of the Blivet's device
        :raise: UnknownDeviceError if no device is found
        """
        raise UnknownDeviceError(name)
Esempio n. 6
0
def generate_device_factory_permissions(storage,
                                        request: DeviceFactoryRequest):
    """Generate permissions for the requested device.

    :param storage: an instance of Blivet
    :param request: a device factory request
    :return: device factory permissions
    """
    permissions = DeviceFactoryPermissions()
    device_name = request.device_spec
    device = storage.devicetree.resolve_device(device_name)

    if not device:
        raise UnknownDeviceError(device_name)

    permissions.device_type = not device.raw_device.exists
    permissions.device_raid_level = not device.raw_device.exists
    permissions.mount_point = device.format.mountable
    permissions.label = True

    permissions.reformat = \
        device.raw_device.exists \
        and not device.raw_device.format_immutable

    permissions.device_size = \
        device.resizable or (
                not device.exists
                and request.device_type not in {
                    devicefactory.DEVICE_TYPE_BTRFS
                }
        )

    permissions.device_name = \
        not device.raw_device.exists \
        and device.raw_device.type != "btrfs volume" \
        and request.device_type in NAMED_DEVICE_TYPES

    permissions.format_type = \
        request.reformat \
        and request.device_type not in {
            devicefactory.DEVICE_TYPE_BTRFS
        }

    permissions.device_encrypted = \
        request.reformat \
        and not request.container_encrypted \
        and request.device_type not in {
            devicefactory.DEVICE_TYPE_BTRFS,
            devicefactory.DEVICE_TYPE_LVM_THINP
        }

    return permissions
Esempio n. 7
0
    def _get_device(self, name):
        """Find a device by its name.

        :param name: a name of the device
        :return: an instance of the Blivet's device
        :raise: UnknownDeviceError if no device is found
        """
        device = self.storage.devicetree.get_device_by_name(name, hidden=True)

        if not device:
            raise UnknownDeviceError(name)

        return device
Esempio n. 8
0
def generate_device_factory_permissions(storage,
                                        request: DeviceFactoryRequest):
    """Generate permissions for the requested device.

    :param storage: an instance of Blivet
    :param request: a device factory request
    :return: device factory permissions
    """
    permissions = DeviceFactoryPermissions()
    device = storage.devicetree.resolve_device(request.device_spec)
    container = storage.devicetree.resolve_device(request.container_name)

    if not device:
        raise UnknownDeviceError(request.device_spec)

    if device.protected:
        return permissions

    permissions.device_type = not device.raw_device.exists
    permissions.device_raid_level = not device.raw_device.exists
    permissions.mount_point = get_format(request.format_type).mountable
    permissions.label = True

    permissions.reformat = \
        device.raw_device.exists \
        and not device.raw_device.format_immutable

    permissions.device_size = \
        device.resizable or (
                not device.exists
                and request.device_type not in {
                    devicefactory.DEVICE_TYPE_BTRFS
                }
        )

    permissions.device_name = \
        not device.raw_device.exists \
        and device.raw_device.type != "btrfs volume" \
        and request.device_type in NAMED_DEVICE_TYPES

    permissions.format_type = \
        request.reformat \
        and request.device_type not in {
            devicefactory.DEVICE_TYPE_BTRFS
        }

    permissions.device_encrypted = \
        request.reformat \
        and not request.container_encrypted \
        and request.device_type not in {
            devicefactory.DEVICE_TYPE_BTRFS,
            devicefactory.DEVICE_TYPE_LVM_THINP
        } \
        and not any(
            a.format.type == "luks" and a.format.exists
            for a in device.raw_device.ancestors if a != device
        )

    permissions.disks = \
        not device.exists \
        and request.device_type not in CONTAINER_DEVICE_TYPES

    can_change_container = \
        request.device_type in CONTAINER_DEVICE_TYPES \
        and not getattr(container, "exists", False)

    can_replace_container = \
        request.device_type in CONTAINER_DEVICE_TYPES \
        and not device.raw_device.exists \
        and device.raw_device != container

    permissions.container_spec = can_replace_container
    permissions.container_name = can_change_container
    permissions.container_encrypted = can_change_container
    permissions.container_raid_level = can_change_container
    permissions.container_size_policy = can_change_container

    return permissions