Skip to content

Commit

Permalink
Merge pull request #4970 from vojtechtrefny/master_btrfs-volume-reformat
Browse files Browse the repository at this point in the history
Allow reformatting of 'plain' btrfs volumes
  • Loading branch information
vojtechtrefny committed Jul 31, 2023
2 parents 346b932 + 7919bee commit be24ef2
Showing 1 changed file with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,17 +105,40 @@ 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.
:param storage: an instance of the Blivet's storage object
: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

0 comments on commit be24ef2

Please sign in to comment.