Compare commits

...

5 commits

2 changed files with 18 additions and 14 deletions

View file

@ -1,5 +1,4 @@
from os import listdir
from os.path import realpath, basename
def all_pci_device_ids() -> list[str]:
@ -8,9 +7,3 @@ def all_pci_device_ids() -> list[str]:
def all_pci_driver_ids() -> list[str]:
return listdir("/sys/bus/pci/drivers")
def driver_of_pci_device(pci_device_id: str) -> str:
driver_directory: str = realpath(
f"/sys/bus/pci/devices/{pci_device_id}/driver")
return basename(driver_directory)

View file

@ -1,15 +1,19 @@
from pci_passthrough_assist.pci import driver_of_pci_device
from os.path import exists
from os.path import exists, realpath, basename
from os import listdir
class PciDevice:
def __init__(self, device_id: str):
assert exists(f"/sys/bus/pci/devices/{device_id}")
self.device_id = device_id
def driver_name(self) -> str:
return driver_of_pci_device(self.device_id)
pci_driver_path = f"/sys/bus/pci/devices/{self.device_id}/driver"
if not exists(pci_driver_path):
return "NO-DRIVER-BOUND"
driver_directory: str = realpath(pci_driver_path)
return basename(driver_directory)
def is_vga(self) -> bool:
return exists(f"/sys/bus/pci/devices/{self.device_id}/boot_vga")
@ -42,13 +46,20 @@ class PciDevice:
if unbind_first:
self.unbind_driver()
with open(f"/sys/bus/pci/drivers/{driver_to_bind}/bind",
"w") as driver:
driver_bind_path = f"/sys/bus/pci/drivers/{driver_to_bind}/bind"
if not exists(driver_bind_path):
print(f"Can't bind to driver: {driver_bind_path}.")
return
with open(driver_bind_path, "w") as driver:
driver.write(self.device_id)
def devices_in_iommu_group(self) -> list['PciDevice']:
device_ids: list[str] = listdir(
f"/sys/bus/pci/devices/{self.device_id}/iommu_group/devices")
iommu_group_device_path = f"/sys/bus/pci/devices/{self.device_id}/iommu_group/devices"
if not exists(iommu_group_device_path):
print("Device does not have iommu_group devices.")
return []
device_ids: list[str] = listdir(iommu_group_device_path)
return [PciDevice(device_id) for device_id in device_ids]
def __str__(self) -> str: