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 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_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
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 })