def change_device_test(self):
        """Test ChangeDevice."""
        dev1 = DiskDevice("dev1")
        dev2 = PartitionDevice("dev2",
                               size=Size("5 GiB"),
                               parents=[dev1],
                               fmt=get_format("ext4",
                                              mountpoint="/",
                                              label="root"))

        self._add_device(dev1)
        self._add_device(dev2)

        original_request = self.module.generate_device_factory_request("dev2")
        request = copy.deepcopy(original_request)

        request.device_type = DEVICE_TYPE_LVM
        request.mount_point = "/home"
        request.size = Size("4 GiB")
        request.label = "home"

        self.storage.factory_device = Mock()
        self.interface.ChangeDevice(
            DeviceFactoryRequest.to_structure(request),
            DeviceFactoryRequest.to_structure(original_request))
        self.storage.factory_device.assert_called_once()
    def validate_device_factory_request_test(self):
        """Test ValidateDeviceFactoryRequest."""
        dev1 = DiskDevice("dev1")
        dev2 = DiskDevice("dev2")
        dev3 = PartitionDevice("dev3", size=Size("10 GiB"), parents=[dev1])

        self._add_device(dev1)
        self._add_device(dev2)
        self._add_device(dev3)

        request = self.module.generate_device_factory_request("dev3")
        request.device_type = DEVICE_TYPE_LVM
        request.disks = ["dev1", "dev2"]
        request.format_type = "ext4"
        request.mount_point = "/boot"
        request.label = "root"
        request.reformat = True
        request.luks_version = "luks1"
        request.device_size = Size("5 GiB").get_bytes()
        request.device_encrypted = True
        request.device_raid_level = "raid1"

        result = self.interface.ValidateDeviceFactoryRequest(
            DeviceFactoryRequest.to_structure(request))
        self._check_report(result, "/boot cannot be encrypted")

        request.mount_point = "/"
        result = self.interface.ValidateDeviceFactoryRequest(
            DeviceFactoryRequest.to_structure(request))
        self._check_report(result, None)
Esempio n. 3
0
def update_container_data(storage, request: DeviceFactoryRequest,
                          container_name):
    """Update the container data in the device factory request.

    :param storage: an instance of Blivet
    :param request: a device factory request
    :param container_name: a container name to apply
    """
    # Reset all container data.
    request.reset_container_data()

    # Check the device type.
    if request.device_type not in CONTAINER_DEVICE_TYPES:
        raise StorageError("Invalid device type.")

    # Find the container in the device tree if any.
    container = storage.devicetree.get_device_by_name(container_name)

    if container:
        # Set the request from the found container.
        request.container_spec = container.name
        request.container_name = container.name
        request.container_encrypted = container.encrypted
        request.container_raid_level = get_device_raid_level_name(container)
        request.container_size_policy = get_container_size_policy(container)

        # Use the container's disks.
        request.disks = [d.name for d in container.disks]
    else:
        # Set the request from the new container.
        request.container_name = container_name
        request.container_raid_level = get_default_container_raid_level_name(
            request.device_type)
Esempio n. 4
0
def generate_container_data(storage, request: DeviceFactoryRequest):
    """Generate the container data for the device factory request.

    :param storage: an instance of Blivet
    :param request: a device factory request
    """
    # Reset all container data.
    request.reset_container_data()

    # Check the device type.
    if request.device_type not in CONTAINER_DEVICE_TYPES:
        return

    # Find a container of the requested type.
    device = storage.devicetree.resolve_device(request.device_spec)
    container = get_container(storage, request.device_type, device.raw_device)

    if container:
        # Set the request from the found container.
        set_container_data(request, container)
    else:
        # Set the request from a new container.
        request.container_name = storage.suggest_container_name()
        request.container_raid_level = get_default_container_raid_level_name(
            request.device_type)
Esempio n. 5
0
    def update_container_data_test(self):
        """Test UpdateContainerData."""
        pv1 = StorageDevice(
            "pv1",
            size=Size("1025 MiB"),
            fmt=get_format("lvmpv")
        )
        pv2 = StorageDevice(
            "pv2",
            size=Size("513 MiB"),
            fmt=get_format("lvmpv")
        )
        vg = LVMVolumeGroupDevice(
            "testvg",
            parents=[pv1, pv2]
        )

        self._add_device(pv1)
        self._add_device(pv2)
        self._add_device(vg)

        request = DeviceFactoryRequest()
        request.device_type = DEVICE_TYPE_PARTITION

        with self.assertRaises(StorageError):
            self.interface.UpdateContainerData(
                    DeviceFactoryRequest.to_structure(request),
                    "anaconda"
            )

        request.device_type = DEVICE_TYPE_BTRFS
        request = DeviceFactoryRequest.from_structure(
            self.interface.UpdateContainerData(
                DeviceFactoryRequest.to_structure(request),
                "anaconda"
            )
        )

        self.assertEqual(request.container_spec, "")
        self.assertEqual(request.container_name, "anaconda")
        self.assertEqual(request.container_encrypted, False)
        self.assertEqual(request.container_raid_level, "single")
        self.assertEqual(request.container_size_policy, 0)
        self.assertEqual(request.disks, [])

        request.device_type = DEVICE_TYPE_LVM
        request = DeviceFactoryRequest.from_structure(
            self.interface.UpdateContainerData(
                DeviceFactoryRequest.to_structure(request),
                "testvg"
            )
        )

        self.assertEqual(request.container_spec, "testvg")
        self.assertEqual(request.container_name, "testvg")
        self.assertEqual(request.container_encrypted, False)
        self.assertEqual(request.container_raid_level, "")
        self.assertEqual(request.container_size_policy, Size("1.5 GiB").get_bytes())
        self.assertEqual(request.disks, [])
Esempio n. 6
0
    def GenerateContainerData(self, request: Structure) -> Structure:
        """Generate the container data for the device factory request.

        :param request: a device factory request
        :return: a device factory request
        """
        request = DeviceFactoryRequest.from_structure(request)
        self.implementation.generate_container_data(request)
        return DeviceFactoryRequest.to_structure(request)
Esempio n. 7
0
    def UpdateContainerData(self, request: Structure,
                            container_name: Str) -> Structure:
        """Update the container data in the device factory request.

        :param request: a device factory request
        :param container_name: a container name
        :return: a device factory request
        """
        request = DeviceFactoryRequest.from_structure(request)
        self.implementation.update_container_data(request, container_name)
        return DeviceFactoryRequest.to_structure(request)
Esempio n. 8
0
    def ChangeDevice(self, request: Structure, original_request: Structure):
        """Change a device in the storage model.

        FIXME: Remove the original request from the arguments.

        :param request: a device factory request
        :param original_request: an original device factory request
        :raise: StorageError if the device cannot be changed
        """
        self.implementation.change_device(
            DeviceFactoryRequest.from_structure(request),
            DeviceFactoryRequest.from_structure(original_request))
Esempio n. 9
0
    def reset_device_factory_request_test(self):
        """Test reset_container_data."""
        default = DeviceFactoryRequest()
        request = DeviceFactoryRequest()

        request.container_spec = "dev1"
        request.container_name = "dev1"
        request.container_size_policy = 123
        request.container_encrypted = True
        request.container_raid_level = "raid1"
        request.reset_container_data()

        self.assertEqual(compare_data(request, default), True)
    def generate_device_factory_request_raid_test(self, blockdev):
        disk1 = DiskDevice("dev1", fmt=get_format("mdmember"))
        disk2 = DiskDevice("dev2", fmt=get_format("mdmember"))
        device = MDRaidArrayDevice("dev3",
                                   level="raid1",
                                   parents=[disk1, disk2])

        request = utils.generate_device_factory_request(self.storage, device)
        self.assertEqual(
            DeviceFactoryRequest.to_structure(request), {
                "device-spec": get_variant(Str, "dev3"),
                "disks": get_variant(List[Str], ["dev1", "dev2"]),
                "mount-point": get_variant(Str, ""),
                "reformat": get_variant(Bool, True),
                "format-type": get_variant(Str, ""),
                "label": get_variant(Str, ""),
                "luks-version": get_variant(Str, ""),
                "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_MD),
                "device-name": get_variant(Str, "dev3"),
                "device-size": get_variant(UInt64, 0),
                "device-encrypted": get_variant(Bool, False),
                "device-raid-level": get_variant(Str, "raid1"),
                "container-spec": get_variant(Str, ""),
                "container-name": get_variant(Str, ""),
                "container-size-policy": get_variant(Int64, 0),
                "container-encrypted": get_variant(Bool, False),
                "container-raid-level": get_variant(Str, ""),
            })
Esempio n. 11
0
    def test_generate_device_factory_request_btrfs(self, blockdev):
        dev1 = StorageDevice("dev1",
                             fmt=get_format("btrfs"),
                             size=Size("10 GiB"))

        dev2 = BTRFSVolumeDevice("dev2", data_level="single", parents=[dev1])

        dev3 = BTRFSSubVolumeDevice(
            parents=[dev2],
            fmt=get_format("btrfs", mountpoint="/boot"),
        )

        request = utils.generate_device_factory_request(self.storage, dev3)
        assert DeviceFactoryRequest.to_structure(request) == {
            "device-spec": get_variant(Str, dev3.name),
            "disks": get_variant(List[Str], []),
            "mount-point": get_variant(Str, "/boot"),
            "reformat": get_variant(Bool, True),
            "format-type": get_variant(Str, "btrfs"),
            "label": get_variant(Str, ""),
            "luks-version": get_variant(Str, ""),
            "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_BTRFS),
            "device-name": get_variant(Str, dev3.name),
            "device-size": get_variant(UInt64,
                                       Size("10 GiB").get_bytes()),
            "device-encrypted": get_variant(Bool, False),
            "device-raid-level": get_variant(Str, ""),
            "container-spec": get_variant(Str, dev2.name),
            "container-name": get_variant(Str, dev2.name),
            "container-size-policy": get_variant(Int64,
                                                 Size("10 GiB").get_bytes()),
            "container-encrypted": get_variant(Bool, False),
            "container-raid-level": get_variant(Str, "single"),
        }
Esempio n. 12
0
    def AddDevice(self, request: Structure):
        """Add a new device to the storage model.

        :param request: a device factory request
        :raise: StorageError if the device cannot be created
        """
        self.implementation.add_device(
            DeviceFactoryRequest.from_structure(request))
Esempio n. 13
0
    def _complete_device_factory_request(self, storage,
                                         request: DeviceFactoryRequest):
        """Complete the device factory request.

        :param storage: an instance of Blivet
        :param request: a device factory request
        """
        # Set the defaults.
        if not request.luks_version:
            request.luks_version = storage.default_luks_version

        # Set the file system type for the given mount point.
        if not request.format_type:
            request.format_type = storage.get_fstype(request.mount_point)

        # Fix the mount point.
        if lowerASCII(request.mount_point) in ("swap", "biosboot", "prepboot"):
            request.mount_point = ""

        # We should create a partition in some cases.
        # These devices should never be encrypted.
        if (request.mount_point.startswith("/boot")
                or request.format_type in PARTITION_ONLY_FORMAT_TYPES):
            request.device_type = devicefactory.DEVICE_TYPE_PARTITION
            request.device_encrypted = False

        # We shouldn't create swap on a thinly provisioned volume.
        if (request.format_type == "swap" and request.device_type
                == devicefactory.DEVICE_TYPE_LVM_THINP):
            request.device_type = devicefactory.DEVICE_TYPE_LVM

        # Encryption of thinly provisioned volumes isn't supported.
        if request.device_type == devicefactory.DEVICE_TYPE_LVM_THINP:
            request.device_encrypted = False
Esempio n. 14
0
    def ValidateDeviceFactoryRequest(self, request: Structure) -> Structure:
        """Validate the given device factory request.

        :param request: a device factory request
        :return: a validation report
        """
        request = DeviceFactoryRequest.from_structure(request)
        report = self.implementation.validate_device_factory_request(request)
        return ValidationReport.to_structure(report)
    def test_update_container_data(self):
        """Test UpdateContainerData."""
        pv1 = StorageDevice("pv1",
                            size=Size("1025 MiB"),
                            fmt=get_format("lvmpv"))
        pv2 = StorageDevice("pv2",
                            size=Size("513 MiB"),
                            fmt=get_format("lvmpv"))
        vg = LVMVolumeGroupDevice("testvg", parents=[pv1, pv2])

        self._add_device(pv1)
        self._add_device(pv2)
        self._add_device(vg)

        request = DeviceFactoryRequest()
        request.device_type = DEVICE_TYPE_PARTITION

        with pytest.raises(StorageError):
            self.interface.UpdateContainerData(
                DeviceFactoryRequest.to_structure(request), "anaconda")

        request.device_type = DEVICE_TYPE_BTRFS
        request = DeviceFactoryRequest.from_structure(
            self.interface.UpdateContainerData(
                DeviceFactoryRequest.to_structure(request), "anaconda"))

        assert request.container_spec == ""
        assert request.container_name == "anaconda"
        assert request.container_encrypted is False
        assert request.container_raid_level == "single"
        assert request.container_size_policy == 0
        assert request.disks == []

        request.device_type = DEVICE_TYPE_LVM
        request = DeviceFactoryRequest.from_structure(
            self.interface.UpdateContainerData(
                DeviceFactoryRequest.to_structure(request), "testvg"))

        assert request.container_spec == "testvg"
        assert request.container_name == "testvg"
        assert request.container_encrypted is False
        assert request.container_raid_level == ""
        assert request.container_size_policy == Size("1.5 GiB").get_bytes()
        assert request.disks == []
Esempio n. 16
0
    def GenerateDeviceFactoryRequest(self, device_name: Str) -> Structure:
        """Generate a device factory request for the given device.

        The request will reflect the current state of the device.
        It can be modified and used to change the device.

        :param device_name: a device name
        :return: a device factory request
        """
        return DeviceFactoryRequest.to_structure(
            self.implementation.generate_device_factory_request(device_name))
Esempio n. 17
0
    def GenerateDeviceFactoryPermissions(self,
                                         request: Structure) -> Structure:
        """Generate device factory permissions for the given request.

        The permissions will reflect which device attributes we are allowed
        to change in the requested device.

        :param request: a device factory request
        :return: device factory permissions
        """
        request = DeviceFactoryRequest.from_structure(request)
        permissions = self.implementation.generate_device_factory_permissions(
            request)
        return DeviceFactoryPermissions.to_structure(permissions)
Esempio n. 18
0
    def _add_device(self,
                    storage,
                    request: DeviceFactoryRequest,
                    use_existing_container=False):
        """Add a device to the storage model.

        :param storage: an instance of Blivet
        :param request: a device factory request
        :param use_existing_container: should we use an existing container?
        :raise: StorageError if the device cannot be created
        """
        # Create the device factory.
        factory = devicefactory.get_device_factory(
            storage,
            device_type=request.device_type,
            size=Size(request.device_size) if request.device_size else None)

        # Find a container.
        container = factory.get_container(
            allow_existing=use_existing_container)

        if use_existing_container and not container:
            raise StorageError("No existing container found.")

        # Update the device info.
        if container:
            # Don't override user-initiated changes to a defined container.
            request.disks = [d.name for d in container.disks]
            request.container_encrypted = container.encrypted
            request.container_raid_level = get_device_raid_level_name(
                container)
            request.container_size_policy = get_container_size_policy(
                container)

            # The existing container has a name.
            if use_existing_container:
                request.container_name = container.name

            # The container is already encrypted
            if container.encrypted:
                request.device_encrypted = False

        # Create the device.
        dev_info = get_device_factory_arguments(storage, request)

        try:
            storage.factory_device(**dev_info)
        except StorageError as e:
            log.error("The device creation has failed: %s", e)
            raise
        except OverflowError as e:
            log.error("Invalid partition size set: %s", str(e))
            raise StorageError(
                "Invalid partition size set. Use a valid integer.") from None
    def add_device_test(self):
        """Test AddDevice."""
        self._add_device(
            DiskDevice("dev1",
                       exists=True,
                       size=Size("15 GiB"),
                       fmt=get_format("disklabel")))

        request = DeviceFactoryRequest()
        request.device_type = DEVICE_TYPE_LVM
        request.mount_point = "/home"
        request.size = Size("5 GiB")
        request.disks = ["dev1"]

        self.storage.factory_device = Mock()
        self.interface.AddDevice(DeviceFactoryRequest.to_structure(request))
        self.storage.factory_device.assert_called_once()
Esempio n. 20
0
def set_container_data(request: DeviceFactoryRequest, container):
    """Set the container data in the device factory request.

    :param request: a device factory request
    :param container: a container
    """
    request.container_spec = container.name
    request.container_name = container.name
    request.container_encrypted = container.encrypted
    request.container_raid_level = get_device_raid_level_name(container)
    request.container_size_policy = get_container_size_policy(container)

    if request.container_encrypted:
        request.luks_version = get_container_luks_version(container)
    def generate_device_factory_request_lvm_test(self, blockdev):
        pv1 = StorageDevice("pv1",
                            size=Size("1025 MiB"),
                            fmt=get_format("lvmpv"))
        pv2 = StorageDevice("pv2",
                            size=Size("513 MiB"),
                            fmt=get_format("lvmpv"))
        vg = LVMVolumeGroupDevice("testvg", parents=[pv1, pv2])
        lv = LVMLogicalVolumeDevice("testlv",
                                    size=Size("512 MiB"),
                                    parents=[vg],
                                    fmt=get_format("xfs"),
                                    exists=False,
                                    seg_type="raid1",
                                    pvs=[pv1, pv2])

        request = utils.generate_device_factory_request(self.storage, lv)
        self.assertEqual(
            DeviceFactoryRequest.to_structure(request), {
                "device-spec": get_variant(Str, "testvg-testlv"),
                "disks": get_variant(List[Str], []),
                "mount-point": get_variant(Str, ""),
                "reformat": get_variant(Bool, True),
                "format-type": get_variant(Str, "xfs"),
                "label": get_variant(Str, ""),
                "luks-version": get_variant(Str, ""),
                "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_LVM),
                "device-name": get_variant(Str, "testlv"),
                "device-size": get_variant(UInt64,
                                           Size("508 MiB").get_bytes()),
                "device-encrypted": get_variant(Bool, False),
                "device-raid-level": get_variant(Str, ""),
                "container-spec": get_variant(Str, "testvg"),
                "container-name": get_variant(Str, "testvg"),
                "container-size-policy": get_variant(Int64, Size("1.5 GiB")),
                "container-encrypted": get_variant(Bool, False),
                "container-raid-level": get_variant(Str, ""),
            })
Esempio n. 22
0
    def get_device_factory_arguments_test(self):
        """Test get_device_factory_arguments."""
        dev1 = StorageDevice("dev1")
        self._add_device(dev1)

        dev2 = StorageDevice("dev2")
        self._add_device(dev2)

        dev3 = StorageDevice("dev3")
        self._add_device(dev3)

        request = DeviceFactoryRequest()
        request.device_spec = "dev3"
        request.disks = ["dev1", "dev2"]
        request.device_name = "dev3"
        request.device_type = devicefactory.DEVICE_TYPE_LVM_THINP
        request.device_size = Size("10 GiB").get_bytes()
        request.mount_point = "/"
        request.format_type = "xfs"
        request.label = "root"
        request.device_encrypted = True
        request.luks_version = "luks1"
        request.device_raid_level = "raid1"

        self.assertEqual(utils.get_device_factory_arguments(self.storage, request), {
            "device": dev3,
            "disks": [dev1, dev2],
            "device_type": devicefactory.DEVICE_TYPE_LVM_THINP,
            "device_name": "dev3",
            "size": Size("10 GiB"),
            "mountpoint": "/",
            "fstype": "xfs",
            "label": "root",
            "encrypted": True,
            "luks_version": "luks1",
            "raid_level": raid.RAID1,
            "container_name": None,
            "container_size": devicefactory.SIZE_POLICY_AUTO,
            "container_raid_level": None,
            "container_encrypted": False
        })

        request = DeviceFactoryRequest()
        request.device_spec = "dev3"
        request.disks = ["dev1", "dev2"]
        request.device_name = "dev3"
        request.container_name = "container1"
        request.container_size_policy = Size("10 GiB").get_bytes()
        request.container_encrypted = True
        request.container_raid_level = "raid1"

        self.assertEqual(utils.get_device_factory_arguments(self.storage, request), {
            "device": dev3,
            "disks": [dev1, dev2],
            "device_type": devicefactory.DEVICE_TYPE_LVM,
            "device_name": "dev3",
            "size": None,
            "mountpoint": None,
            "fstype": None,
            "label": None,
            "encrypted": False,
            "luks_version": None,
            "raid_level": None,
            "container_name": "container1",
            "container_size": Size("10 GiB"),
            "container_raid_level": raid.RAID1,
            "container_encrypted": True
        })
Esempio n. 23
0
    def generate_device_factory_request_test(self, blockdev):
        device = StorageDevice("dev1")
        with self.assertRaises(UnsupportedDeviceError):
            utils.generate_device_factory_request(self.storage, device)

        disk = DiskDevice("dev2")

        request = utils.generate_device_factory_request(self.storage, disk)
        self.assertEqual(DeviceFactoryRequest.to_structure(request), {
            "device-spec": get_variant(Str, "dev2"),
            "disks": get_variant(List[Str], ["dev2"]),
            "mount-point": get_variant(Str, ""),
            "reformat": get_variant(Bool, False),
            "format-type": get_variant(Str, ""),
            "label": get_variant(Str, ""),
            "luks-version": get_variant(Str, ""),
            "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_DISK),
            "device-name": get_variant(Str, "dev2"),
            "device-size": get_variant(UInt64, 0),
            "device-encrypted": get_variant(Bool, False),
            "device-raid-level": get_variant(Str, ""),
            "container-spec": get_variant(Str, ""),
            "container-name": get_variant(Str, ""),
            "container-size-policy": get_variant(Int64, devicefactory.SIZE_POLICY_AUTO),
            "container-encrypted": get_variant(Bool, False),
            "container-raid-level": get_variant(Str, ""),
        })

        partition = PartitionDevice(
            "dev3",
            size=Size("5 GiB"),
            parents=[disk],
            fmt=get_format("ext4", mountpoint="/", label="root")
        )

        request = utils.generate_device_factory_request(self.storage, partition)
        self.assertEqual(DeviceFactoryRequest.to_structure(request), {
            "device-spec": get_variant(Str, "dev3"),
            "disks": get_variant(List[Str], ["dev2"]),
            "mount-point": get_variant(Str, "/"),
            "reformat": get_variant(Bool, True),
            "format-type": get_variant(Str, "ext4"),
            "label": get_variant(Str, "root"),
            "luks-version": get_variant(Str, ""),
            "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_PARTITION),
            "device-name": get_variant(Str, "dev3"),
            "device-size": get_variant(UInt64, Size("5 GiB").get_bytes()),
            "device-encrypted": get_variant(Bool, False),
            "device-raid-level": get_variant(Str, ""),
            "container-spec": get_variant(Str, ""),
            "container-name": get_variant(Str, ""),
            "container-size-policy": get_variant(Int64, devicefactory.SIZE_POLICY_AUTO),
            "container-encrypted": get_variant(Bool, False),
            "container-raid-level": get_variant(Str, ""),
        })

        pv1 = StorageDevice(
            "pv1",
            size=Size("1025 MiB"),
            fmt=get_format("lvmpv")
        )
        pv2 = StorageDevice(
            "pv2",
            size=Size("513 MiB"),
            fmt=get_format("lvmpv")
        )
        vg = LVMVolumeGroupDevice(
            "testvg",
            parents=[pv1, pv2]
        )
        lv = LVMLogicalVolumeDevice(
            "testlv",
            size=Size("512 MiB"),
            parents=[vg],
            fmt=get_format("xfs"),
            exists=False,
            seg_type="raid1",
            pvs=[pv1, pv2]
        )

        request = utils.generate_device_factory_request(self.storage, lv)
        self.assertEqual(DeviceFactoryRequest.to_structure(request), {
            "device-spec": get_variant(Str, "testvg-testlv"),
            "disks": get_variant(List[Str], []),
            "mount-point": get_variant(Str, ""),
            "reformat": get_variant(Bool, True),
            "format-type": get_variant(Str, "xfs"),
            "label": get_variant(Str, ""),
            "luks-version": get_variant(Str, ""),
            "device-type": get_variant(Int, devicefactory.DEVICE_TYPE_LVM),
            "device-name": get_variant(Str, "testlv"),
            "device-size": get_variant(UInt64, Size("508 MiB").get_bytes()),
            "device-encrypted": get_variant(Bool, False),
            "device-raid-level": get_variant(Str, ""),
            "container-spec": get_variant(Str, "testvg"),
            "container-name": get_variant(Str, "testvg"),
            "container-size-policy": get_variant(Int64, Size("1.5 GiB")),
            "container-encrypted": get_variant(Bool, False),
            "container-raid-level": get_variant(Str, ""),
        })
Esempio n. 24
0
    def generate_container_data_test(self, proxy_getter):
        """Test GenerateContainerData."""
        network_proxy = Mock()
        network_proxy.Hostname = "localhost"
        network_proxy.GetCurrentHostname.return_value = "localhost"
        proxy_getter.return_value = network_proxy

        pv1 = StorageDevice("pv1",
                            size=Size("1025 MiB"),
                            fmt=get_format("lvmpv"))
        pv2 = StorageDevice("pv2",
                            size=Size("513 MiB"),
                            fmt=get_format("lvmpv"))
        vg = LVMVolumeGroupDevice("testvg", parents=[pv1, pv2])
        lv = LVMLogicalVolumeDevice("testlv",
                                    size=Size("512 MiB"),
                                    parents=[vg],
                                    fmt=get_format("xfs"),
                                    exists=False,
                                    seg_type="raid1",
                                    pvs=[pv1, pv2])

        self._add_device(pv1)
        self._add_device(pv2)
        self._add_device(vg)
        self._add_device(lv)

        request = DeviceFactoryRequest()
        request.device_spec = lv.name

        request.device_type = DEVICE_TYPE_LVM
        request = DeviceFactoryRequest.from_structure(
            self.interface.GenerateContainerData(
                DeviceFactoryRequest.to_structure(request)))

        self.assertEqual(request.container_spec, "testvg")
        self.assertEqual(request.container_name, "testvg")
        self.assertEqual(request.container_encrypted, False)
        self.assertEqual(request.container_raid_level, "")
        self.assertEqual(request.container_size_policy,
                         Size("1.5 GiB").get_bytes())

        request.device_type = DEVICE_TYPE_BTRFS
        request = DeviceFactoryRequest.from_structure(
            self.interface.GenerateContainerData(
                DeviceFactoryRequest.to_structure(request)))

        self.assertEqual(request.container_spec, "")
        self.assertEqual(request.container_name, "anaconda")
        self.assertEqual(request.container_encrypted, False)
        self.assertEqual(request.container_raid_level, "single")
        self.assertEqual(request.container_size_policy, 0)

        request.device_type = DEVICE_TYPE_PARTITION
        request = DeviceFactoryRequest.from_structure(
            self.interface.GenerateContainerData(
                DeviceFactoryRequest.to_structure(request)))

        self.assertEqual(request.container_spec, "")
        self.assertEqual(request.container_name, "")
        self.assertEqual(request.container_encrypted, False)
        self.assertEqual(request.container_raid_level, "")
        self.assertEqual(request.container_size_policy, 0)
Esempio n. 25
0
def generate_device_factory_request(storage, device) -> DeviceFactoryRequest:
    """Generate a device info for the given device.

    :param storage: an instance of Blivet
    :param device: a device
    :return: a device factory request
    """
    device_type = devicefactory.get_device_type(device)

    if device_type is None:
        raise UnsupportedDeviceError("Unsupported type of {}.".format(
            device.name))

    request = DeviceFactoryRequest()
    request.device_spec = device.name
    request.device_name = getattr(device.raw_device, "lvname",
                                  device.raw_device.name)
    request.device_size = device.size.get_bytes()
    request.device_type = device_type
    request.reformat = not device.format.exists
    request.format_type = device.format.type or ""
    request.device_encrypted = isinstance(device, LUKSDevice)
    request.luks_version = get_device_luks_version(device) or ""
    request.label = getattr(device.format, "label", "") or ""
    request.mount_point = getattr(device.format, "mountpoint", "") or ""
    request.device_raid_level = get_device_raid_level_name(device)

    if hasattr(device, "req_disks") and not device.exists:
        disks = device.req_disks
    else:
        disks = device.disks

    request.disks = [d.name for d in disks]

    factory = devicefactory.get_device_factory(storage,
                                               device_type=device_type,
                                               device=device.raw_device)
    container = factory.get_container()

    if container:
        request.container_spec = container.name
        request.container_name = container.name
        request.container_encrypted = container.encrypted
        request.container_raid_level = get_device_raid_level_name(container)
        request.container_size_policy = get_container_size_policy(container)

    return request
Esempio n. 26
0
    def generate_request_description_test(self):
        """Test generate_request_description."""
        request = DeviceFactoryRequest()
        request.device_spec = "dev3"
        request.disks = ["dev1", "dev2"]
        request.device_name = "dev3"
        request.device_type = devicefactory.DEVICE_TYPE_LVM_THINP
        request.device_size = Size("10 GiB").get_bytes()
        request.mount_point = "/"
        request.format_type = "xfs"
        request.label = "root"
        request.device_encrypted = True
        request.luks_version = "luks1"
        request.device_raid_level = "raid1"

        expected = dedent("""
        {
        container-encrypted = False
        container-name = ''
        container-raid-level = ''
        container-size-policy = 0
        container-spec = ''
        device-encrypted = True
        device-name = 'dev3'
        device-raid-level = 'raid1'
        device-size = 10737418240
        device-spec = 'dev3'
        device-type = 5
        disks = ['dev1', 'dev2']
        format-type = 'xfs'
        label = 'root'
        luks-version = 'luks1'
        mount-point = '/'
        reformat = False
        }
        """).strip()

        self.assertEqual(generate_request_description(request), expected)

        original = copy.deepcopy(request)
        self.assertEqual(generate_request_description(request, original),
                         expected)

        request.device_name = "dev4"
        request.disks = ["dev1"]
        request.device_encrypted = False

        expected = dedent("""
        {
        container-encrypted = False
        container-name = ''
        container-raid-level = ''
        container-size-policy = 0
        container-spec = ''
        device-encrypted = True -> False
        device-name = 'dev3' -> 'dev4'
        device-raid-level = 'raid1'
        device-size = 10737418240
        device-spec = 'dev3'
        device-type = 5
        disks = ['dev1', 'dev2'] -> ['dev1']
        format-type = 'xfs'
        label = 'root'
        luks-version = 'luks1'
        mount-point = '/'
        reformat = False
        }
        """).strip()

        self.assertEqual(generate_request_description(request, original),
                         expected)
    def generate_device_factory_request_partition_test(self, blockdev):
        disk = DiskDevice("dev2")

        request = utils.generate_device_factory_request(self.storage, disk)
        self.assertEqual(
            DeviceFactoryRequest.to_structure(request), {
                "device-spec":
                get_variant(Str, "dev2"),
                "disks":
                get_variant(List[Str], ["dev2"]),
                "mount-point":
                get_variant(Str, ""),
                "reformat":
                get_variant(Bool, False),
                "format-type":
                get_variant(Str, ""),
                "label":
                get_variant(Str, ""),
                "luks-version":
                get_variant(Str, ""),
                "device-type":
                get_variant(Int, devicefactory.DEVICE_TYPE_DISK),
                "device-name":
                get_variant(Str, "dev2"),
                "device-size":
                get_variant(UInt64, 0),
                "device-encrypted":
                get_variant(Bool, False),
                "device-raid-level":
                get_variant(Str, ""),
                "container-spec":
                get_variant(Str, ""),
                "container-name":
                get_variant(Str, ""),
                "container-size-policy":
                get_variant(Int64, devicefactory.SIZE_POLICY_AUTO),
                "container-encrypted":
                get_variant(Bool, False),
                "container-raid-level":
                get_variant(Str, ""),
            })

        partition = PartitionDevice("dev3",
                                    size=Size("5 GiB"),
                                    parents=[disk],
                                    fmt=get_format("ext4",
                                                   mountpoint="/",
                                                   label="root"))

        request = utils.generate_device_factory_request(
            self.storage, partition)
        self.assertEqual(
            DeviceFactoryRequest.to_structure(request), {
                "device-spec":
                get_variant(Str, "dev3"),
                "disks":
                get_variant(List[Str], ["dev2"]),
                "mount-point":
                get_variant(Str, "/"),
                "reformat":
                get_variant(Bool, True),
                "format-type":
                get_variant(Str, "ext4"),
                "label":
                get_variant(Str, "root"),
                "luks-version":
                get_variant(Str, ""),
                "device-type":
                get_variant(Int, devicefactory.DEVICE_TYPE_PARTITION),
                "device-name":
                get_variant(Str, "dev3"),
                "device-size":
                get_variant(UInt64,
                            Size("5 GiB").get_bytes()),
                "device-encrypted":
                get_variant(Bool, False),
                "device-raid-level":
                get_variant(Str, ""),
                "container-spec":
                get_variant(Str, ""),
                "container-name":
                get_variant(Str, ""),
                "container-size-policy":
                get_variant(Int64, devicefactory.SIZE_POLICY_AUTO),
                "container-encrypted":
                get_variant(Bool, False),
                "container-raid-level":
                get_variant(Str, ""),
            })