From 7919beeb2d6b57e8a2c7d36c47127544ccb94b9e Mon Sep 17 00:00:00 2001 From: Vojtech Trefny Date: Thu, 27 Jul 2023 16:19:23 +0200 Subject: [PATCH] Allow reformatting of 'plain' btrfs volumes Similar to previous support for btrfs subvolumes, this allows reusing the btrfs volume directly. Only btrfs volumes without subvolumes can be reused which allows using btrfs as a 'normal' single device filesystem instead of using subvolumes only. --- .../manual/manual_partitioning.py | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/pyanaconda/modules/storage/partitioning/manual/manual_partitioning.py b/pyanaconda/modules/storage/partitioning/manual/manual_partitioning.py index 359dc93a807..5ea4f2870b9 100644 --- a/pyanaconda/modules/storage/partitioning/manual/manual_partitioning.py +++ b/pyanaconda/modules/storage/partitioning/manual/manual_partitioning.py @@ -85,7 +85,7 @@ def _setup_mount_point(self, storage, mount_data): fmt = get_format(old_fmt.type) - if device.raw_device.type == "btrfs subvolume": + if device.raw_device.type in ("btrfs volume", "btrfs subvolume"): # 'Format', or rather clear the device by recreating it device = self._recreate_device(storage, device_spec) mount_data.mount_options = device.format.options @@ -105,6 +105,24 @@ def _setup_mount_point(self, storage, mount_data): device.format.create_options = mount_data.format_options device.format.options = mount_data.mount_options + def _recreate_btrfs_volume(self, storage, device): + """Recreate a btrfs volume device by destroying and adding it. + + :param storage: an instance of the Blivet's storage object + :param dev_spec: a BtrfsVolumeDevice to recreate + """ + if device.children: + raise StorageError( + _("Cannot reformat Btrfs volume '{}' with " + "existing subvolumes").format(device.name)) + storage.destroy_device(device) + for parent in device.parents: + storage.format_device(parent, get_format("btrfs")) + new_btrfs = storage.new_btrfs(parents=device.parents[:], + name=device.name) + storage.create_device(new_btrfs) + return new_btrfs + def _recreate_device(self, storage, dev_spec): """Recreate a device by destroying and adding it. @@ -112,10 +130,15 @@ def _recreate_device(self, storage, dev_spec): :param dev_spec: a string describing a block device to be recreated """ device = storage.devicetree.resolve_device(dev_spec) - request = generate_device_factory_request(storage, device) - destroy_device(storage, device) - task = AddDeviceTask(storage, request) - task.run() - device = storage.devicetree.resolve_device(dev_spec) - return device + if device.type == "btrfs volume": + # can't use device factory for just the volume + return self._recreate_btrfs_volume(storage, device) + else: + request = generate_device_factory_request(storage, device) + destroy_device(storage, device) + task = AddDeviceTask(storage, request) + task.run() + device = storage.devicetree.resolve_device(dev_spec) + + return device