Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions virttest/utils_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import logging
import platform
import re

from avocado.utils import process
Expand All @@ -14,6 +15,8 @@

LOG = logging.getLogger("avocado." + __name__)

ARCH = platform.machine()


# TODO: check function in avocado.utils after the next LTS
def check_dmesg_output(pattern, expect=True, session=None):
Expand Down Expand Up @@ -133,3 +136,30 @@ def get_pids_for(process_names, sort_pids=True, session=None):
relevant_pids.sort()

return relevant_pids


def get_location_code(pci_id):
"""
Retrieves the location code for a PCI device using the `cat` command.


:param pci_id : The PCI device address in the format 'xxxx:xx:xx.x'.
:return: location code if available, or an error message.
Example location code = 'U898B.NT0.WDS0DT1-P0-C4-T0'
"""
try:
if ARCH == "ppc64le":
loc_code_path = "/sys/bus/pci/devices/%s/of_node/ibm,loc-code" % pci_id
else:
raise FileNotFoundError(
"Valid location code path is not defined for architecture: %s" % ARCH
)
with open(loc_code_path, "r") as f:
loc_code = f.read().strip()
loc_code = "".join(c for c in loc_code if 0x20 <= ord(c) <= 0x7E)
logging.debug("The location code of the pci device is %s", loc_code)
return loc_code
except FileNotFoundError:
return "Location code file not found for device %s.", pci_id
except Exception as e:
return "An error occurred: %s", e