From bb3264d8f4ec8e3dd2f5c3ab206393517a70cc19 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Wed, 16 Aug 2023 13:49:17 +0200 Subject: disk: add get_efi_partition function Add utility function inside disk.py to find, if any, the first ESP partition of a given disk. The disk is provided as an integer (starting at 1 following OpenGnsys scripts usual values), meaning the (n-1)th disk from the disk array returned from get_disks(). In the future a better mechanism should be put in place to fetch probed disks from a running client. This change is part of the upcoming drop of "IniciarSesion" script in favor of a Python native approach. Specifically regarding UEFI systems. --- src/utils/disk.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/utils/disk.py b/src/utils/disk.py index 1f1fdab..02c63fc 100644 --- a/src/utils/disk.py +++ b/src/utils/disk.py @@ -34,3 +34,27 @@ def get_partition_device(disknum, partnum): return cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE) raise ValueError(f'No such partition with disk index {disknum} and partition index {partnum}') + + +def get_efi_partition(disknum): + """ + Look for an EFI System Partition at the n-th disk. If disknum is invalid an exception is thrown + + Returns tuple with: + - Device name containing the ESP + - /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 -- cgit v1.2.3-18-g5258