Skip to content

Commit d552f44

Browse files
committed
Calculate UUID grain for Xen PV guests
1 parent 713a923 commit d552f44

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

changelog/99999.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added UUID detection for Xen paravirtualized guests from `/sys/hypervisor/uuid`

salt/grains/core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,6 +3258,23 @@ def _hw_data(osdata):
32583258
return {}
32593259

32603260
grains = {}
3261+
3262+
# For Xen para-virtualized guests read UUID from /sys/hypervisor/uuid
3263+
if osdata["kernel"] == "Linux" and os.path.exists("/sys/hypervisor/uuid"):
3264+
try:
3265+
with salt.utils.files.fopen("/sys/hypervisor/uuid", "r") as ifile:
3266+
hypervisor_uuid = salt.utils.stringutils.to_unicode(
3267+
ifile.read().strip(), errors="replace"
3268+
)
3269+
# Normalize to lowercase to match DMI format
3270+
grains["uuid"] = hypervisor_uuid.lower()
3271+
log.debug(
3272+
"Read UUID from /sys/hypervisor/uuid for para-virtualized guest: %s",
3273+
grains["uuid"],
3274+
)
3275+
except OSError as err:
3276+
log.debug("Unable to read /sys/hypervisor/uuid: %s", err)
3277+
32613278
if osdata["kernel"] == "Linux" and os.path.exists("/sys/class/dmi/id"):
32623279
# On many Linux distributions basic firmware information is available via sysfs
32633280
# requires CONFIG_DMIID to be enabled in the Linux kernel configuration
@@ -3272,6 +3289,9 @@ def _hw_data(osdata):
32723289
"serialnumber": "product_serial",
32733290
}
32743291
for key, fw_file in sysfs_firmware_info.items():
3292+
# Skip UUID if already read from /sys/hypervisor/uuid (Xen PV guests)
3293+
if key == "uuid" and "uuid" in grains:
3294+
continue
32753295
contents_file = os.path.join("/sys/class/dmi/id", fw_file)
32763296
if os.path.exists(contents_file):
32773297
try:

tests/pytests/unit/grains/test_core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,26 @@ def read(self):
35603560
assert core._hw_data({"kernel": "Linux"}) == {}
35613561

35623562

3563+
@pytest.mark.skip_unless_on_linux
3564+
def test__hw_data_xen_pv_uuid():
3565+
hypervisor_uuid = "12345678-1234-1234-1234-123456789ABC"
3566+
expected_uuid = "12345678-1234-1234-1234-123456789abc"
3567+
3568+
def _exists_side_effect(path):
3569+
if path == "/sys/hypervisor/uuid":
3570+
return True
3571+
return False
3572+
3573+
with patch("os.path.exists", side_effect=_exists_side_effect), patch(
3574+
"salt.utils.platform.is_proxy", return_value=False
3575+
), patch("salt.utils.path.which_bin", return_value=None), patch(
3576+
"salt.utils.files.fopen",
3577+
mock_open(read_data=hypervisor_uuid),
3578+
):
3579+
result = core._hw_data({"kernel": "Linux"})
3580+
assert result.get("uuid") == expected_uuid
3581+
3582+
35633583
@pytest.mark.skip_unless_on_windows
35643584
def test_kernelparams_return_windows():
35653585
"""

0 commit comments

Comments
 (0)