Added BlockDevice.largest_free_space and BlockDevice.free_space (iterator). Also added additional supported filesystems to parted. Apparently the online manpages doesn't agree with the local manpages, my previous statement that these gets ignored is false so added those in and removed some that isn't supported by my local manpages, 'ufs' for instance.

This commit is contained in:
Anton Hvornum 2021-06-14 18:53:34 +02:00
parent eb4c134c80
commit 2ac1f453cf
2 changed files with 52 additions and 10 deletions

View File

@ -15,16 +15,27 @@ MBR = 0b00000010
def valid_fs_type(fstype :str) -> bool:
# https://www.gnu.org/software/parted/manual/html_node/mkpart.html
# Above link doesn't agree with `man parted` /mkpart documentation:
"""
fs-type can
be one of "btrfs", "ext2",
"ext3", "ext4", "fat16",
"fat32", "hfs", "hfs+",
"linux-swap", "ntfs", "reis
erfs", "udf", or "xfs".
"""
return fstype in [
"ext2",
"fat16", "fat32",
"hfs", "hfs+", "hfsx",
"linux-swap",
"NTFS",
"reiserfs",
"ufs",
"btrfs",
"ext2",
"ext3", "ext4", # `man parted` allows these
"fat16", "fat32",
"hfs", "hfs+", # "hfsx", not included in `man parted`
"linux-swap",
"ntfs",
"reiserfs",
"udf", # "ufs", not included in `man parted`
"xfs", # `man parted` allows this
]
@ -335,6 +346,25 @@ class BlockDevice:
for device in output['blockdevices']:
return device['rota'] is True
@property
def free_space(self):
for line in SysCommand(f"parted --machine {self.path} print free"):
if 'free' in (free_space := line.decode('UTF-8')):
_, start, end, size, *_ = free_space.strip('\r\n;').split(':')
yield (start, end, size)
@property
def largest_free_space(self):
info = None
for space_info in self.free_space:
if not info:
info = space_info
else:
# [-1] = size
if space_info[-1] > info[-1]:
info = space_info
return info
def has_partitions(self):
return len(self.partitions)

View File

@ -567,6 +567,9 @@ def valid_parted_position(pos :str):
if pos[-3:].lower() in ['mib', 'kib', 'b', 'tib'] and pos[:-3].isdigit():
return True
if pos[-2:].lower() in ['kb', 'mb', 'gb', 'tb'] and pos[:-2].isdigit():
return True
return False
def partition_overlap(partitions :list, start :str, end :str) -> bool:
@ -617,7 +620,7 @@ def manage_new_and_existing_partitions(block_device :BlockDevice) -> dict:
mountpoints = {}
block_device_struct = {
"partitions" : []
"partitions" : [partition.__dump__() for partition in block_device.partitions.values()]
}
# Test code: [part.__dump__() for part in block_device.partitions.values()]
# TODO: Squeeze in BTRFS subvolumes here
@ -648,9 +651,18 @@ def manage_new_and_existing_partitions(block_device :BlockDevice) -> dict:
# https://www.gnu.org/software/parted/manual/html_node/mkpart.html
# https://www.gnu.org/software/parted/manual/html_node/mklabel.html
name = input("Enter a desired name for the partition: ").strip()
fstype = input("Enter a desired filesystem type for the partition: ").strip()
start = input("Enter the start sector of the partition (percentage or block number, ex: 0%): ").strip()
end = input("Enter the end sector of the partition (percentage or block number, ex: 100%): ").strip()
start = input(f"Enter the start sector (percentage or block number, default: {block_device.largest_free_space[0]}): ").strip()
if not start.strip():
start = block_device.largest_free_space[0]
end_suggested = block_device.largest_free_space[1]
else:
end_suggested = '100%'
end = input(f"Enter the end sector of the partition (percentage or block number, ex: {end_suggested}): ").strip()
if not end.strip():
end = end_suggested
if valid_parted_position(start) and valid_parted_position(end) and valid_fs_type(fstype):
if partition_overlap(block_device_struct, start, end):