This is a Python script that automates the process of connecting a USB device to a KVM virtual machine. The USB device will be automatically attached to the guest when it is connected to the host and detached when it is disconnected.
Download:
wget https://github.com/dvaoru/kvm-autoconnect-usb/releases/latest/download/kvm-autoconnect-usb.py
Make executable:
sudo chmod +x kvm-autoconnect-usb.py
Run the script on the KVM host machine.
For starting monitoring a USB device and autoconnection to a virtual machine:
sudo python kvm-autoconnect-usb.py start <usb_vendor_id>:<usb_model_id> <vm_domain_name>
Example:
sudo python kvm-autoconnect-usb.py start 03f0:002a my_debian_vm
For stopping monitoring a USB device and autoconnection to a virtual machine:
sudo python kvm-autoconnect-usb.py stop <usb_vendor_id>:<usb_model_id> <vm_domain_name>
Example:
sudo python kvm-autoconnect-usb.py start 03f0:002a my_debian_vm
For getting a list of USB devices that are automatically connected to the guest:
sudo python kvm-autoconnect-usb.py list
For stopping autoconnecting all USB devices:
sudo python kvm-autoconnect-usb.py clear
The script creates a udev rules file /etc/udev/rules.d/80-kvm-autoconnect-usb.rules
for monitoring connecting and disconnecting USB devices,
and creates a bash file /usr/local/bin/kvm-autoconnect-script.sh
to attach and detach the devices to virtual machine by using virsh
.
If you have any question, email me at [email protected]