Skip to content

Instantly share code, notes, and snippets.

@h3po
Created May 2, 2017 04:31
Show Gist options
  • Save h3po/fe09a293afd33ff5d7228c450e7fa0ba to your computer and use it in GitHub Desktop.
Save h3po/fe09a293afd33ff5d7228c450e7fa0ba to your computer and use it in GitHub Desktop.
Quick wrapper script for lspci that lists devices by IOMMU group. Arguments (except -s and -d) are passed through to lspci.
#!/usr/bin/env python3
import os, subprocess, sys
from contextlib import suppress
if "-h" in sys.argv:
with suppress(subprocess.CalledProcessError):
print(subprocess.check_output(["lspci", "-h"], universal_newlines=True))
exit(0)
if "-s" in sys.argv or "-d" in sys.argv:
print("Do not use -s or -d selectors, the script does that for you.",
file=sys.stderr)
exit(1)
groups = sorted(os.scandir("/sys/kernel/iommu_groups"),
key=lambda de: int(de.name))
for i, group in enumerate(groups):
assert group.is_dir(follow_symlinks=False)
if i > 0:
print()
print(f"Group {int(group.name):2d}:")
devicespath = os.path.join(group, "devices")
devices = sorted(os.scandir(devicespath),
key=lambda de: int(de.name.translate(str.maketrans("", "", ":.")), 16))
for device in devices:
assert device.is_symlink()
assert device.is_dir(follow_symlinks=True)
lspci = subprocess.check_output(["lspci", "-s", device.name] + sys.argv[1:],
stderr=subprocess.STDOUT,
universal_newlines=True)
print(lspci.strip())
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment