Changed lsblk to blkid where possible (#802)

* Swapped lsblk for blkid
* Added a hefty sleep on partprobe()

And added a TODO for the future
This commit is contained in:
Anton Hvornum 2021-12-16 09:00:10 +00:00 committed by GitHub
parent 3c2e71b4bb
commit 7a01841586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 16 deletions

View File

@ -150,8 +150,7 @@ class BlockDevice:
This is more reliable than relying on /dev/disk/by-partuuid as This is more reliable than relying on /dev/disk/by-partuuid as
it doesn't seam to be able to detect md raid partitions. it doesn't seam to be able to detect md raid partitions.
""" """
for partition in json.loads(SysCommand(f'lsblk -J -o+UUID {self.path}').decode('UTF-8'))['blockdevices']: return SysCommand(f'blkid -s PTUUID -o value {self.path}').decode('UTF-8')
return partition.get('uuid', None)
@property @property
def size(self): def size(self):

View File

@ -36,6 +36,9 @@ class Filesystem:
def partuuid_to_index(self, uuid): def partuuid_to_index(self, uuid):
for i in range(storage['DISK_RETRY_ATTEMPTS']): for i in range(storage['DISK_RETRY_ATTEMPTS']):
self.partprobe() self.partprobe()
time.sleep(5)
# TODO: Convert to blkid (or something similar, but blkid doesn't support traversing to list sub-PARTUUIDs based on blockdevice path?)
output = json.loads(SysCommand(f"lsblk --json -o+PARTUUID {self.blockdevice.device}").decode('UTF-8')) output = json.loads(SysCommand(f"lsblk --json -o+PARTUUID {self.blockdevice.device}").decode('UTF-8'))
for device in output['blockdevices']: for device in output['blockdevices']:
@ -127,7 +130,6 @@ class Filesystem:
def partprobe(self): def partprobe(self):
SysCommand(f'bash -c "partprobe"') SysCommand(f'bash -c "partprobe"')
time.sleep(1)
def raw_parted(self, string: str): def raw_parted(self, string: str):
if (cmd_handle := SysCommand(f'/usr/bin/parted -s {string}')).exit_code != 0: if (cmd_handle := SysCommand(f'/usr/bin/parted -s {string}')).exit_code != 0:
@ -205,5 +207,9 @@ class Filesystem:
SysCommand(f'bash -c "umount {device}?"') SysCommand(f'bash -c "umount {device}?"')
except: except:
pass pass
self.partprobe() self.partprobe()
return self.raw_parted(f'{device} mklabel {disk_label}').exit_code == 0 worked = self.raw_parted(f'{device} mklabel {disk_label}').exit_code == 0
self.partprobe()
return worked

View File

@ -214,10 +214,14 @@ def find_partition_by_mountpoint(block_devices, relative_mountpoint :str):
def partprobe(): def partprobe():
SysCommand(f'bash -c "partprobe"') SysCommand(f'bash -c "partprobe"')
time.sleep(5)
def convert_device_to_uuid(path :str) -> str: def convert_device_to_uuid(path :str) -> str:
for i in range(storage['DISK_RETRY_ATTEMPTS']): for i in range(storage['DISK_RETRY_ATTEMPTS']):
partprobe() partprobe()
# TODO: Convert lsblk to blkid
# (lsblk supports BlockDev and Partition UUID grabbing, blkid requires you to pick PTUUID and PARTUUID)
output = json.loads(SysCommand(f"lsblk --json -o+UUID {path}").decode('UTF-8')) output = json.loads(SysCommand(f"lsblk --json -o+UUID {path}").decode('UTF-8'))
for device in output['blockdevices']: for device in output['blockdevices']:
@ -226,4 +230,4 @@ def convert_device_to_uuid(path :str) -> str:
time.sleep(storage['DISK_TIMEOUTS']) time.sleep(storage['DISK_TIMEOUTS'])
raise DiskError(f"Could not retrieve the UUID of {path} within a timely manner.") raise DiskError(f"Could not retrieve the UUID of {path} within a timely manner.")

View File

@ -159,15 +159,13 @@ class Partition:
for i in range(storage['DISK_RETRY_ATTEMPTS']): for i in range(storage['DISK_RETRY_ATTEMPTS']):
self.partprobe() self.partprobe()
partuuid_struct = SysCommand(f'lsblk -J -o+PARTUUID {self.path}') partuuid = self._safe_uuid
if partuuid_struct.exit_code == 0: if partuuid:
if partition_information := next(iter(json.loads(partuuid_struct.decode('UTF-8'))['blockdevices']), None): return partuuid
if partuuid := partition_information.get('partuuid', None):
return partuuid
time.sleep(storage['DISK_TIMEOUTS']) time.sleep(storage['DISK_TIMEOUTS'])
raise DiskError(f"Could not get PARTUUID for {self.path} using 'lsblk -J -o+PARTUUID {self.path}'") raise DiskError(f"Could not get PARTUUID for {self.path} using 'blkid -s PARTUUID -o value {self.path}'")
@property @property
def _safe_uuid(self) -> Optional[str]: def _safe_uuid(self) -> Optional[str]:
@ -178,11 +176,7 @@ class Partition:
""" """
self.partprobe() self.partprobe()
partuuid_struct = SysCommand(f'lsblk -J -o+PARTUUID {self.path}') return SysCommand(f'blkid -s PARTUUID -o value {self.path}').decode('UTF-8').strip()
if partuuid_struct.exit_code == 0:
if partition_information := next(iter(json.loads(partuuid_struct.decode('UTF-8'))['blockdevices']), None):
if partuuid := partition_information.get('partuuid', None):
return partuuid
@property @property
def encrypted(self): def encrypted(self):