From aa34704b4d28225afb3e9a0d563826c7bb58a378 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 6 Mar 2024 13:36:09 +0100 Subject: utils: improve logging in the get_efi_partition function Log each partition that gets checked and make the exception messages more informative. --- src/utils/disk.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'src/utils') diff --git a/src/utils/disk.py b/src/utils/disk.py index 02c63fc..0848cff 100644 --- a/src/utils/disk.py +++ b/src/utils/disk.py @@ -7,6 +7,7 @@ # (at your option) any later version. import os +import logging import fdisk @@ -45,16 +46,18 @@ def get_efi_partition(disknum): - /dev/{device} string - Partition number (starting at 1) """ - try: - disk = get_disks()[disknum-1] - cxt = fdisk.Context(f'/dev/{disk}') - - if cxt.label == fdisk.FDISK_DISKLABEL_DOS: - raise RuntimeError('Disk has DOS partition scheme, cannot find ESP.') - - for pa in cxt.partitions: - if pa.type.name == 'EFI System': - return cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE), f'/dev/{disk}', pa.partno + 1 - except: - logging.error(f'Unable to find efi partition at disk number {disknum}') - raise + disk_index = disknum - 1 + if disk_index < 0 or disk_index >= len(get_disks()): + raise ValueError(f'Invalid disk number {disknum} when trying to find ESP, {len(get_disks())} disks available.') + + disk = get_disks()[disk_index] + cxt = fdisk.Context(f'/dev/{disk}') + + if cxt.label == fdisk.FDISK_DISKLABEL_DOS: + raise RuntimeError(f'Disk has DOS partition scheme, cannot find ESP at /dev/{disk}') + + for pa in cxt.partitions: + logging.info(f'Checking partition "{pa.type.name}"...') + if pa.type.name == 'EFI System': + return cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE), f'/dev/{disk}', pa.partno + 1 + raise RuntimeError(f'Cannot find "EFI System" partition at /dev/{disk}') -- cgit v1.2.3-18-g5258