Exemple #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)
Exemple #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)
Exemple #3
0
def shrink_device(storage, device, size):
    """Shrink the size of the device.

    :param storage: a storage model
    :param device: a device to shrink
    :param size: a new size of the device
    """
    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)
    storage.resize_device(device, aligned_size)
Exemple #4
0
def remove_device(storage, device):
    """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 storage: a storage model
    :param device: a device to remove
    """
    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):
            storage.recursive_remove(child)

        return

    # No protected children, remove the device
    log.debug("Removing device %s.", device.name)
    storage.recursive_remove(device)