summaryrefslogtreecommitdiffstats
path: root/src/utils/disk.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-03-06 13:36:09 +0100
committerlupoDharkael <izhe@hotmail.es>2024-03-08 12:43:01 +0100
commitaa34704b4d28225afb3e9a0d563826c7bb58a378 (patch)
treeb86578fd6807468c0a0c3d12cbbd6e9426e3c66a /src/utils/disk.py
parent673cada250e3612eaa8295c67560968445199220 (diff)
utils: improve logging in the get_efi_partition function
Log each partition that gets checked and make the exception messages more informative.
Diffstat (limited to 'src/utils/disk.py')
-rw-r--r--src/utils/disk.py29
1 files changed, 16 insertions, 13 deletions
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}')