This repository was archived by the owner on Feb 19, 2024. It is now read-only.
forked from ipatix/libvirt-usb-hotplug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibvirt-usb-hotplug.py
More file actions
executable file
·109 lines (83 loc) · 2.54 KB
/
libvirt-usb-hotplug.py
File metadata and controls
executable file
·109 lines (83 loc) · 2.54 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3
import os
import sys
import subprocess
##################### CONFIG #######################
config = [
(
"vm1", [
"/devices/pci0000:00/0000:00:14.0/usb3/3-11",
]
), (
"vm2", [
"/devices/pci0000:00/0000:00:14.0/usb3/3-12",
]
),
]
debug = True
###################### CODE ########################
def dbg(msg):
global debug
if debug:
print(msg, file=sys.stderr)
# identify action and actual device added
dbg("--- BEGIN ---")
action = os.getenv("ACTION") or ""
if action == "":
print("Unsupported ACTION: " + action)
sys.exit(2)
subsystem = os.getenv("SUBSYSTEM") or ""
if subsystem != "usb":
dbg("don't care about SUBSYSTEM: " + subsystem)
sys.exit(0)
busnum = os.getenv("BUSNUM") or ""
if busnum == "":
dbg("don't care about BUSNUM: " + busnum)
sys.exit(0)
busnum=int(busnum)
devnum = os.getenv("DEVNUM") or ""
if devnum == "":
dbg("don't care about DEVNUM: " + devnum)
sys.exit(0)
devnum=int(devnum)
devpath = os.getenv("DEVPATH") or ""
if devpath == "":
dbg("don't care about DEVPATH: " + devpath)
sys.exit(0)
devpath = os.path.realpath(devpath)
hub_search = [ "ID_MODEL", "ID_MODEL_FROM_DATABASE" ]
for s in hub_search:
if "hub" in (os.getenv(s) or "").lower():
dbg("don't care about USB hubs: " + devpath)
sys.exit(0)
# find domain for current device
found_domain = ""
for domain, ports in config:
for port in ports:
if port != devpath and (port + "/") not in devpath:
continue
# If device path does match, but there are sub devices available
# ignore this device (don't pass through a USB hub itself, causes problems)
found_domain = domain
break
if found_domain != "":
break
if found_domain == "":
dbg("udev event doesn't match any device in config")
sys.exit(0)
# tell libvirt to attach/detach device
if action == "add":
dbg("attaching device")
op = "attach-device"
elif action == "remove":
dbg("detaching device")
op = "detach-device"
else:
dbg("Unsupported ACTION: " + action)
dbg("busnum={} devnum={} devpath={}".format(busnum, devnum, devpath))
device_xml = '<hostdev mode="subsystem" type="usb"><source><address bus="{}" device="{}"/></source></hostdev>'
device_xml = device_xml.format(busnum, devnum)
virsh = subprocess.Popen(["virsh", op, found_domain, "/dev/stdin"], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL)
virsh.communicate(input=device_xml.encode("ascii"))
virsh.wait()
dbg("--- END ---")