forked from pbelskiy/docker-usb-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-usb-sync.py
executable file
·45 lines (32 loc) · 1.13 KB
/
docker-usb-sync.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
#!/usr/bin/env python3
import syslog
import subprocess
import os
def log(message):
print(message)
syslog.syslog(message)
def exec_in_container(cmd, name="container_name"):
return subprocess.check_output(
"docker exec {} bash -c '{}'".format(name, cmd),
universal_newlines=True,
shell=True
)
root_dev = "/dev/bus/usb"
for bus_num in os.listdir(root_dev):
bus_path = os.path.join(root_dev, bus_num)
container_devices = exec_in_container("ls " + bus_path).split("\n")
host_devices = os.listdir(bus_path)
# remove dead links
for device in container_devices:
if device and device not in host_devices:
exec_in_container("rm " + os.path.join(bus_path, device))
# add new links
for device in host_devices:
if device not in container_devices:
stat = os.stat(os.path.join(bus_path, device))
cmd = "mknod {device} c {major} {minor}".format(
device=os.path.join(bus_path, device),
major=os.major(stat.st_rdev),
minor=os.minor(stat.st_rdev)
)
exec_in_container(cmd)