utils: add disk index checks in get_partition_device

Control the possibility of a bad disk index. Handle the checks in
a similar fashion to the get_efi_partition function.
master
Alejandro Sirgo Rica 2024-03-11 09:44:38 +01:00 committed by lupoDharkael
parent a3ffdf2370
commit ddf08779ae
1 changed files with 5 additions and 1 deletions

View File

@ -27,7 +27,11 @@ def get_partition_device(disknum, partnum):
"""
Returns the device path, given a disk and partition number
"""
disk = get_disks()[disknum-1]
disk_index = disknum - 1
if disk_index < 0 or disk_index >= len(get_disks()):
raise ValueError(f'Invalid disk number {disknum}, {len(get_disks())} disks available.')
disk = get_disks()[disk_index]
cxt = fdisk.Context(f'/dev/{disk}')
for pa in cxt.partitions: