-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusb-libvirt-hotplug.sh
More file actions
106 lines (95 loc) · 3.39 KB
/
Copy pathusb-libvirt-hotplug.sh
File metadata and controls
106 lines (95 loc) · 3.39 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
#!/bin/bash
#
# usb-libvirt-hotplug.sh
#
# This can be used to attach devices when they are plugged into a
# specific port on the host machine.
#
# See: https://github.com/daybat/usb-libvirt-hotplug
#
source ./config.sh
#DOMAIN="win7-64-modules"
#USBALLOW="/etc/libvirt/${DOMAIN}.usb"
USBDEVICES=${USBALLOW}.attach
# Abort script execution on errors
set -e
PROG="$(basename "$0")"
if [ ! -t 1 ]; then
# stdout is not a tty. Send all output to syslog.
coproc logger --tag "${PROG}"
exec >&${COPROC[1]} 2>&1
fi
t=1
z="0"
x="0x"
tmpfile=${USBDEVICES}.1
while [ $t -gt 0 ]
do
ps=$(virsh dominfo ${DOMAIN})
if [[ $ps == *"running"* ]];
then
( [ -e "$USBDEVICES" ] || touch "$USBDEVICES" ) && [ ! -w "$USBDEVICES" ] && echo cannot write to $USBDEVICES && exit 1
( [ -e "$tmpfile" ] || touch "$tmpfile" ) && [ ! -w "$tmpfile" ] && echo cannot write to $tmpfile && exit 1
lsusb=$(lsusb)
while IFS= read -r line || [[ "$line" ]]; do
VendorID="$(echo ${line} | cut -d':' -f1)"
DeviceID="$(echo ${line} | cut -d':' -f2)"
busnum="$(echo ${line} | cut -d':' -f3)"
devnum="$(echo ${line} | cut -d':' -f4)"
[ ${#busnum} == 2 ] && busnum1="${z}${busnum}"
[ ${#busnum} == 1 ] && busnum1="${z}${z}${busnum}"
[ ${#devnum} == 2 ] && devnum1="${z}${devnum}"
[ ${#devnum} == 1 ] && devnum1="${z}${z}${devnum}"
if [[ $lsusb != *"Bus ${busnum1} Device ${devnum1}:"* ]];
then
# echo "detach: <hostdev mode='subsystem' type='usb' managed='yes'><source startupPolicy='optional'><vendor id='${VendorID}' /><product id='${DeviceID}' /><address type='usb' bus='${busnum}' device='${devnum}' /></source></hostdev>"
/usr/bin/virsh detach-device "${DOMAIN}" /dev/stdin<<END
<hostdev mode='subsystem' type='usb' managed='yes'>
<source startupPolicy='optional'>
<vendor id='${VendorID}' />
<product id='${DeviceID}' />
<address bus='${busnum}' device='${devnum}' />
</source>
</hostdev>
END
else
echo "${VendorID}":"${DeviceID}":"${busnum}":"${devnum}" >> "$tmpfile"
fi
done < $USBDEVICES
mv $tmpfile $USBDEVICES
while IFS= read -r line || [[ "$line" ]]; do
# echo "line: ${line}"
VendorID="$(echo ${line} | cut -d':' -f1)"
DeviceID="$(echo ${line} | cut -d':' -f2)"
VendorID="${x}${VendorID}"
DeviceID="${x}${DeviceID}"
while IFS= read -r usb || [[ "$usb" ]]; do
# echo "usb: ${usb}"
busnum="$(echo ${usb} | cut -d' ' -f2)"
devnum="$(echo ${usb} | cut -d' ' -f4)"
devnum="$(echo ${devnum} | cut -d':' -f1)"
busnum=$((10#$busnum))
devnum=$((10#$devnum))
if !(grep -Fxq "${VendorID}":"${DeviceID}":"${busnum}":"${devnum}" "$USBDEVICES");
then
if [ ${busnum} != 0 -a ${devnum} != 0 ];
then
# echo "attach: <hostdev mode='subsystem' type='usb' managed='yes'><source startupPolicy='optional'><vendor id='${VendorID}' /><product id='${DeviceID}' /><address type='usb' bus='${busnum}' device='${devnum}' /></source></hostdev>"
virsh attach-device "${DOMAIN}" /dev/stdin<<END
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='${VendorID}' />
<product id='${DeviceID}' />
<address bus='${busnum}' device='${devnum}' />
</source>
</hostdev>
END
grep -Fxq -- "${VendorID}":"${DeviceID}":"${busnum}":"${devnum}" "$USBDEVICES" || echo "${VendorID}":"${DeviceID}":"${busnum}":"${devnum}" >> "$USBDEVICES"
fi
fi
done <<<$(lsusb|grep $line)
done <$USBALLOW
fi
sleep 1
# x=$(( $x - 1 ))
done