From 55540b11f8e0017d3d23f7cf02202877a6d38ffe Mon Sep 17 00:00:00 2001 From: Vojtech Trefny Date: Mon, 26 Aug 2024 10:12:46 +0200 Subject: [PATCH] Add partition type human-readable string to PartitionDevice Unfortunately parted doesn't have the human readable strings so if we don't want to simply copy the table from libfdisk, we need to start using libblockdev part plugin to get it. Fixes: #1258 --- blivet/__init__.py | 4 +-- blivet/devices/partition.py | 26 ++++++++++++++++++++ python-blivet.spec | 1 + tests/storage_tests/devices_test/lvm_test.py | 8 ++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/blivet/__init__.py b/blivet/__init__.py index b729df25d..572910f22 100644 --- a/blivet/__init__.py +++ b/blivet/__init__.py @@ -63,9 +63,9 @@ def log_bd_message(level, msg): from gi.repository import GLib from gi.repository import BlockDev as blockdev if arch.is_s390(): - _REQUESTED_PLUGIN_NAMES = set(("lvm", "btrfs", "swap", "crypto", "loop", "mdraid", "mpath", "dm", "s390", "nvme", "fs")) + _REQUESTED_PLUGIN_NAMES = set(("lvm", "btrfs", "swap", "crypto", "loop", "mdraid", "mpath", "dm", "s390", "nvme", "fs", "part")) else: - _REQUESTED_PLUGIN_NAMES = set(("lvm", "btrfs", "swap", "crypto", "loop", "mdraid", "mpath", "dm", "nvme", "fs")) + _REQUESTED_PLUGIN_NAMES = set(("lvm", "btrfs", "swap", "crypto", "loop", "mdraid", "mpath", "dm", "nvme", "fs", "part")) blockdev.utils_set_log_level(syslog.LOG_INFO) diff --git a/blivet/devices/partition.py b/blivet/devices/partition.py index fbed6ec81..eb97f3e79 100644 --- a/blivet/devices/partition.py +++ b/blivet/devices/partition.py @@ -38,6 +38,7 @@ from ..formats import DeviceFormat, get_format from ..devicelibs.gpt import gpt_part_uuid_for_mountpoint from ..size import Size, MiB, ROUND_DOWN +from .. import avail_plugs import logging log = logging.getLogger("blivet") @@ -159,6 +160,7 @@ def __init__(self, name, fmt=None, uuid=None, self._parted_partition = None self._orig_path = None self._part_type_uuid = None + self._part_type_name = None self._mountpoint = mountpoint if not exists and size is None: @@ -384,6 +386,30 @@ def part_type_uuid(self): pass return self._part_type_uuid + @property + def part_type_name(self): + """ Get the partition's type as a human-readable string. """ + if not self.exists: + return None + else: + if self._part_type_name: + return self._part_type_name + else: + if "part" not in avail_plugs: + log.info("Libblockdev part plugin not available, cannot get partition type") + return None + else: + try: + spec = blockdev.part.get_part_spec(self.disk.path, self.path) + except (blockdev.PartError, blockdev.BlockDevNotImplementedError) as e: + log.error("Failed to get partition spec for %s: %s", self.path, str(e)) + else: + if not spec or not hasattr(spec, "type_name"): + log.error("Failed to get partition spec for %s", self.path) + else: + self._part_type_name = spec.type_name + return self._part_type_name + @property def is_extended(self): return (self.part_type is not None and diff --git a/python-blivet.spec b/python-blivet.spec index 191d39425..d90a7944b 100644 --- a/python-blivet.spec +++ b/python-blivet.spec @@ -66,6 +66,7 @@ Recommends: libblockdev-lvm >= %{libblockdevver} Recommends: libblockdev-mdraid >= %{libblockdevver} Recommends: libblockdev-mpath >= %{libblockdevver} Recommends: libblockdev-nvme >= %{libblockdevver} +Recommends: libblockdev-part >= %{libblockdevver} Recommends: libblockdev-swap >= %{libblockdevver} %ifarch s390 s390x diff --git a/tests/storage_tests/devices_test/lvm_test.py b/tests/storage_tests/devices_test/lvm_test.py index eb9496b06..0ad1427e9 100644 --- a/tests/storage_tests/devices_test/lvm_test.py +++ b/tests/storage_tests/devices_test/lvm_test.py @@ -1,6 +1,9 @@ import os import shutil import subprocess +from uuid import UUID + +import parted from ..storagetestcase import StorageTestCase @@ -101,6 +104,11 @@ def test_lvm_basic(self): self.assertIsNone(pv.format.vg_name) self.assertIsNone(pv.format.vg_uuid) + # not really related to LVM, but we want to test the partition types somewhere + if hasattr(parted.Partition, "type_uuid"): + self.assertEqual(pv.part_type_uuid, UUID('e6d6d379-f507-44c2-a23c-238f2a3df928')) + self.assertEqual(pv.part_type_name, "Linux LVM") + def test_lvm_thin(self): disk = self.storage.devicetree.get_device_by_path(self.vdevs[0]) self.assertIsNotNone(disk)