Optimized a bunch of partprobe calls. (#1088)

* Optimized a bunch of partprobe calls. Namely fixed sleep calls, added optional path to the general archinstall.partprobe() call. And fixed some error handling in a few places which should tell us where #1083 might be going wrong.

* Fixed some flake8 complaints

* Fixed sleep having a min() of 0.1 or given value.

* Fixed sleep having a correct range variable.

* Fixed sleep logic to use max() instead of min() as it will never use the higer sleep values otheride
This commit is contained in:
Anton Hvornum 2022-04-26 17:13:47 +02:00 committed by GitHub
parent eafbf49cdc
commit 59c35df067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 22 deletions

View File

@ -11,7 +11,7 @@ if TYPE_CHECKING:
from .partition import Partition from .partition import Partition
from .validators import valid_fs_type from .validators import valid_fs_type
from ..exceptions import DiskError from ..exceptions import DiskError, SysCallError
from ..general import SysCommand from ..general import SysCommand
from ..output import log from ..output import log
from ..storage import storage from ..storage import storage
@ -49,7 +49,7 @@ class Filesystem:
def partuuid_to_index(self, uuid :str) -> Optional[int]: def partuuid_to_index(self, uuid :str) -> Optional[int]:
for i in range(storage['DISK_RETRY_ATTEMPTS']): for i in range(storage['DISK_RETRY_ATTEMPTS']):
self.partprobe() self.partprobe()
time.sleep(5) time.sleep(max(0.1, storage['DISK_TIMEOUTS'] * i))
# We'll use unreliable lbslk to grab children under the /dev/<device> # We'll use unreliable lbslk to grab children under the /dev/<device>
output = json.loads(SysCommand(f"lsblk --json {self.blockdevice.device}").decode('UTF-8')) output = json.loads(SysCommand(f"lsblk --json {self.blockdevice.device}").decode('UTF-8'))
@ -61,8 +61,6 @@ class Filesystem:
if partition_uuid.lower() == uuid.lower(): if partition_uuid.lower() == uuid.lower():
return index return index
time.sleep(storage['DISK_TIMEOUTS'])
raise DiskError(f"Failed to convert PARTUUID {uuid} to a partition index number on blockdevice {self.blockdevice.device}") raise DiskError(f"Failed to convert PARTUUID {uuid} to a partition index number on blockdevice {self.blockdevice.device}")
def load_layout(self, layout :Dict[str, Any]) -> None: def load_layout(self, layout :Dict[str, Any]) -> None:
@ -162,11 +160,11 @@ class Filesystem:
return partition return partition
def partprobe(self) -> bool: def partprobe(self) -> bool:
result = SysCommand(f'partprobe {self.blockdevice.device}') try:
SysCommand(f'partprobe {self.blockdevice.device}')
if result.exit_code != 0: except SysCallError as error:
log(f"Could not execute partprobe: {result!r}", level=logging.ERROR, fg="red") log(f"Could not execute partprobe: {error!r}", level=logging.ERROR, fg="red")
raise DiskError(f"Could not run partprobe: {result!r}") raise DiskError(f"Could not run partprobe on {self.blockdevice.device}: {error!r}")
return True return True

View File

@ -418,16 +418,20 @@ def find_partition_by_mountpoint(block_devices :List[BlockDevice], relative_moun
if partition.get('mountpoint', None) == relative_mountpoint: if partition.get('mountpoint', None) == relative_mountpoint:
return partition return partition
def partprobe() -> bool: def partprobe(path :str = '') -> bool:
if SysCommand(f'bash -c "partprobe"').exit_code == 0: try:
time.sleep(5) # TODO: Remove, we should be relying on blkid instead of lsblk if SysCommand(f'bash -c "partprobe {path}"').exit_code == 0:
return True return True
except SysCallError:
pass
return False return False
def convert_device_to_uuid(path :str) -> str: def convert_device_to_uuid(path :str) -> str:
device_name, bind_name = split_bind_name(path) device_name, bind_name = split_bind_name(path)
for i in range(storage['DISK_RETRY_ATTEMPTS']): for i in range(storage['DISK_RETRY_ATTEMPTS']):
partprobe() partprobe(device_name)
time.sleep(max(0.1, storage['DISK_TIMEOUTS'] * i)) # TODO: Remove, we should be relying on blkid instead of lsblk
# TODO: Convert lsblk to blkid # TODO: Convert lsblk to blkid
# (lsblk supports BlockDev and Partition UUID grabbing, blkid requires you to pick PTUUID and PARTUUID) # (lsblk supports BlockDev and Partition UUID grabbing, blkid requires you to pick PTUUID and PARTUUID)
@ -437,8 +441,6 @@ def convert_device_to_uuid(path :str) -> str:
if (dev_uuid := device.get('uuid', None)): if (dev_uuid := device.get('uuid', None)):
return dev_uuid return dev_uuid
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.")
def has_mountpoint(partition: Union[dict,Partition,MapperDev], target: str, strict: bool = True) -> bool: def has_mountpoint(partition: Union[dict,Partition,MapperDev], target: str, strict: bool = True) -> bool:

View File

@ -142,6 +142,7 @@ class Partition:
def size(self) -> Optional[float]: def size(self) -> Optional[float]:
for i in range(storage['DISK_RETRY_ATTEMPTS']): for i in range(storage['DISK_RETRY_ATTEMPTS']):
self.partprobe() self.partprobe()
time.sleep(max(0.1, storage['DISK_TIMEOUTS'] * i))
try: try:
lsblk = json.loads(SysCommand(f"lsblk --json -b -o+SIZE {self.device_path}").decode()) lsblk = json.loads(SysCommand(f"lsblk --json -b -o+SIZE {self.device_path}").decode())
@ -154,8 +155,6 @@ class Partition:
else: else:
raise error raise error
time.sleep(storage['DISK_TIMEOUTS'])
@property @property
def boot(self) -> bool: def boot(self) -> bool:
output = json.loads(SysCommand(f"sfdisk --json {self.block_device.path}").decode('UTF-8')) output = json.loads(SysCommand(f"sfdisk --json {self.block_device.path}").decode('UTF-8'))
@ -194,13 +193,12 @@ class Partition:
""" """
for i in range(storage['DISK_RETRY_ATTEMPTS']): for i in range(storage['DISK_RETRY_ATTEMPTS']):
self.partprobe() self.partprobe()
time.sleep(max(0.1, storage['DISK_TIMEOUTS'] * i))
partuuid = self._safe_uuid partuuid = self._safe_uuid
if partuuid: if partuuid:
return partuuid return partuuid
time.sleep(storage['DISK_TIMEOUTS'])
raise DiskError(f"Could not get PARTUUID for {self.path} using 'blkid -s PARTUUID -o value {self.path}'") raise DiskError(f"Could not get PARTUUID for {self.path} using 'blkid -s PARTUUID -o value {self.path}'")
@property @property
@ -210,7 +208,14 @@ class Partition:
This function should only be used where uuid is not crucial. This function should only be used where uuid is not crucial.
For instance when you want to get a __repr__ of the class. For instance when you want to get a __repr__ of the class.
""" """
self.partprobe() try:
self.partprobe()
except SysCallError as partprobe_error:
if self.block_device.info.get('TYPE') == 'iso9660':
return None
raise DiskError(f"Could not get PARTUUID of partition {self} due to partprobe error: {partprobe_error}")
try: try:
return SysCommand(f'blkid -s PARTUUID -o value {self.device_path}').decode('UTF-8').strip() return SysCommand(f'blkid -s PARTUUID -o value {self.device_path}').decode('UTF-8').strip()
except SysCallError as error: except SysCallError as error:
@ -263,7 +268,6 @@ class Partition:
def partprobe(self) -> bool: def partprobe(self) -> bool:
if self.block_device and SysCommand(f'partprobe {self.block_device.device}').exit_code == 0: if self.block_device and SysCommand(f'partprobe {self.block_device.device}').exit_code == 0:
time.sleep(1)
return True return True
return False return False