-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_vfio.py
executable file
·64 lines (51 loc) · 2.07 KB
/
bind_vfio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
from subprocess import check_output, check_call
from pathlib import Path
from time import sleep
from argparse import ArgumentParser
GROUPS = Path("/sys/kernel/iommu_groups")
# Show all groups and devices
def list_groups():
for group in sorted(GROUPS.iterdir(), key=lambda p: int(p.name)):
print(group.name)
for device in sorted((group / "devices").iterdir()):
print(" ", check_output(["lspci", "-nns", device.name], text=True).strip())
# Pass all devices from the group to VFIO
def pass_to_vfio(group: Path):
devices = list(sorted((group / "devices").iterdir()))
if len(devices) > 1:
print("Group has more than one device. Select one to pass through or 'all'")
for i, device in enumerate(devices):
print(f" {i}: {check_output(['lspci', '-nns', device.name], text=True).strip()}")
response = input("Device: ").strip()
if response != "all":
selected = int(response)
devices = [devices[selected]]
for device in devices:
(device / "driver/unbind").write_text(device.name)
check_call(["modprobe", "vfio-pci"])
(device / "driver_override").write_text("vfio-pci")
Path("/sys/bus/pci/drivers_probe").write_text(device.name)
sleep(1)
# Give us access
check_call(["chmod", "-R", "go+rw", "/dev/vfio"])
if __name__ == "__main__":
parser = ArgumentParser(description="Rebind an IOMMU group to VFIO")
parser.add_argument("-g", "--group", type=int,
help="ID of the IOMMU group to rebind")
parser.add_argument("-l", "--list", action="store_true",
help="Just list the IOMMU groups")
args = parser.parse_args()
group = None
if args.group is None:
list_groups()
if args.list:
exit(0)
selected = int(input("Group id: ").strip())
group = GROUPS / str(selected)
else:
group = GROUPS / str(args.group)
if group is None or not group.exists():
print("Invalid group id")
exit(1)
pass_to_vfio(group)