summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-04-17 10:49:52 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-07-29 15:07:22 +0200
commit4f31bde5499d962332a31817a2e1fd31fd00fe5d (patch)
tree681e324d4bc1df23377cd87322783e8afe5ee111
parent41b5f830c6d74dca69cfea1c259f7e21d4ad9bb9 (diff)
utils: add functions to obtain disk and partition UUIDs
Add get_partition_id and get_disk_id to obtain the UUID of a disk or partition as a string. These values are obtained invoking the program blkid as a subprocess. This commit is preparatory work for the new native postinstall code.
-rw-r--r--src/utils/disk.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/utils/disk.py b/src/utils/disk.py
index a44153d..81faa07 100644
--- a/src/utils/disk.py
+++ b/src/utils/disk.py
@@ -8,6 +8,8 @@
import os
import logging
+import shlex
+import subprocess
from src.log import OgError
import fdisk
@@ -69,3 +71,26 @@ def get_efi_partition(disknum, enforce_gpt):
if pa.type.name == 'EFI System':
return cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE), f'/dev/{disk}', pa.partno + 1
raise OgError(f'Cannot find "EFI System" partition at /dev/{disk}')
+
+
+def get_partition_id(disk_index, part_index):
+ device = get_partition_device(disk_index, part_index)
+ cmd = f'blkid -s PARTUUID -o value {device}'
+ proc = subprocess.run(shlex.split(cmd),
+ stdout=subprocess.PIPE,
+ encoding='utf-8')
+ if proc.returncode != 0:
+ raise OgError(f'failed to query partition UUID for {device}')
+ return proc.stdout.strip()
+
+
+def get_disk_id(disk_index):
+ disk = get_disks()[disk_index - 1]
+ disk_path = f'/dev/{disk}'
+ cmd = f'blkid -s PTUUID -o value {disk_path}'
+ proc = subprocess.run(shlex.split(cmd),
+ stdout=subprocess.PIPE,
+ encoding='utf-8')
+ if proc.returncode != 0:
+ raise OgError(f'failed to query disk UUID for {disk_path}')
+ return proc.stdout.strip()