From 4f31bde5499d962332a31817a2e1fd31fd00fe5d Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 17 Apr 2024 10:49:52 +0200 Subject: 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. --- src/utils/disk.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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() -- cgit v1.2.3-18-g5258