-
Notifications
You must be signed in to change notification settings - Fork 4
/
hotplugger.py
executable file
·173 lines (141 loc) · 5.28 KB
/
hotplugger.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python3
import re
import os
import ast
import sys
import yaml
import json
import time
import pprint
from pathlib import Path
from qemu import *
configFilename = Path(__file__).parent / "config.yaml"
tmpFolderPath = Path(__file__).parent / "tmp"
def printp(dict):
pprint.pprint(dict, width=1)
def sanitizeDevpath(devpath):
return devpath.replace('/', '_').replace(':', '_')
def sanitize(str):
return "".join(re.findall("[a-zA-Z0-9]+", str))
def loadConfig():
with open(configFilename) as file:
return yaml.load(file, Loader=yaml.FullLoader)
def savePortDeviceMetadata(metadata, devpath):
if not os.path.exists(tmpFolderPath):
os.makedirs(tmpFolderPath)
usbdefPath = f"{tmpFolderPath}/{sanitizeDevpath(devpath)}"
print(f"Saving port metadata to {usbdefPath} ...")
f = open(usbdefPath, "w")
f.write(json.dumps(metadata))
f.close()
def loadPortDeviceMetadata(config, devpath):
for rootKey, rootValue in config.items():
for k, v in rootValue.items():
for port in v['ports']:
if devpath.find(port) >= 0:
print(f"Found {devpath} in port {port}")
if not os.path.exists(tmpFolderPath):
os.makedirs(tmpFolderPath)
metadataFiles = [f for f in os.listdir(tmpFolderPath) if os.path.isfile(os.path.join(tmpFolderPath, f))]
metadataFiles.sort(key=len, reverse=True)
print(f"Metadata files:")
printp(metadataFiles)
usbDefPathFile = sanitizeDevpath(devpath)
for f in metadataFiles:
metadataFilename = os.path.join(tmpFolderPath, f)
if usbDefPathFile.find(f) >= 0:
print(f"Found {devpath} in {metadataFilename}")
with open(metadataFilename) as metadataFile:
rv = json.loads(metadataFile.read())
rv["SOCKET"] = rootValue[k]['socket']
rv["FILENAME"] = metadataFilename
rv["HUBS"] = rootValue[k]['hubs']
rv["DELAY"] = rootValue[k]['delay']
return rv
def plug():
print('==================================================================')
print('PLUG')
print('==================================================================')
printp(dict(os.environ))
print('==================================================================')
config = loadConfig()
devpath = os.environ['DEVPATH']
is_usb_port = (os.getenv('DEVNUM') or '') != '' and (os.getenv('BUSNUM') or '') != ''
print(f"Is USB Port? {is_usb_port}")
if is_usb_port == True:
savePortDeviceMetadata(json.loads(json.dumps(dict(os.environ))), devpath)
else:
metadata = loadPortDeviceMetadata(config, devpath)
if not metadata:
print(f"Metadata file for {devpath} not found")
else:
print(metadata)
print(f"Connecting to QEMU at {metadata['SOCKET']}...")
with QEMU(metadata["SOCKET"]) as qemu:
usbhost = qemu.hmp("info usbhost")
print(usbhost)
hostport = 0
hostaddr = metadata['DEVNUM'].lstrip('0')
hostbus = metadata['BUSNUM'].lstrip('0')
print(f"Looking for USB Bus: {hostbus}, Addr {hostaddr} ...")
for line in usbhost.splitlines():
if line.find(f"Bus {hostbus}") >= 0:
if line.find(f"Addr {hostaddr}") >= 0:
print('FOUND IN', line)
hostport_search = re.search(".*Port.*?([\d\.]*),", line, re.IGNORECASE)
hostport = hostport_search.group(1)
break
print(f"Found USB Bus: {hostbus}, Addr {hostaddr}, Port {hostport}")
if hostport != 0:
print(f"Plugging USB device in port {hostport}...")
with QEMU(metadata["SOCKET"]) as qemu:
device_id = sanitize(metadata['DEVNAME'])
print(f"Device ID = {device_id}")
for guesthub in metadata["HUBS"]:
time.sleep(int(metadata["DELAY"]))
result = qemu.hmp(f"device_add driver=usb-host,bus={guesthub},hostbus={hostbus},hostport={hostport},id={device_id}")
if result.find("speed mismatch trying to attach usb device") >= 0:
qemu.hmp(f"device_del {device_id}")
else:
print(f"Device plugged in on hub {guesthub}. Current USB devices on guest:")
print(qemu.hmp("info usb"))
break
if Path(metadata["FILENAME"]).exists():
os.remove(metadata["FILENAME"])
def unplug():
print('==================================================================')
print('UNPLUG')
print('==================================================================')
printp(dict(os.environ))
print('==================================================================')
config = loadConfig()
devpath = os.environ['DEVPATH']
if (os.getenv('DEVNAME') or '') != '':
for rootKey, rootValue in config.items():
for k, v in rootValue.items():
socket = rootValue[k]['socket']
socketFile = Path(socket)
if socketFile.exists():
print(f"Connecting to QEMU at {socket}...")
with QEMU(socket) as qemu:
usbhost = qemu.hmp("info usbhost")
print(usbhost)
with QEMU(socket) as qemu:
device_id = sanitize(os.environ["DEVNAME"])
qemu.hmp(f"device_del {device_id}")
print(f"Device unplugged from {k}")
print(qemu.hmp("info usb"))
usbDefPathFile = os.path.join(tmpFolderPath, sanitizeDevpath(devpath))
if Path(usbDefPathFile).exists():
os.remove(usbDefPathFile)
action = os.environ['ACTION']
if action == 'add':
plug()
elif action == 'remove':
unplug()
else:
print("")
print("Device plug/unplug helper script")
print("")
print("This should be run by an udev rules file you create that will trigger on every")
print("USB command. For more info have a look at the README file.")