Preventing Specific USB Device (Vendor ID: Product ID) Driver Loading in Debian Linux KVM Virtual Machines
In this article, we will discuss how to prevent the loading of a specific USB device driver in Debian Linux KVM virtual machines. This is particularly useful when you have host USB devices attached and would like to pass-through one of them to a guest virtual machine.
Prerequisites
- Debian Linux host with KVM (libvirt) virtualization installed
- A USB device with a unique vendor ID and product ID that you want to pass-through to a guest virtual machine
Step 1: Identify the USB Device
First, you need to identify the USB device that you want to pass-through to the guest virtual machine. You can use the lsusb command to list all USB devices connected to the host.
# lsusb
The output will look something like this:
Bus 002 Device 002: ID 8087:8000 Intel Corp.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 003: ID 138a:0011 Generic USB Keyboard
Bus 001 Device 002: ID 046d:c077 Logitech, Inc. M90/M100 Optical Mouse
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
In this example, the USB keyboard has a vendor ID of 138a and a product ID of 0011.
Step 2: Create a udev Rule
Next, you need to create a udev rule to prevent the loading of the specific USB device driver in the guest virtual machine. To do this, create a new file in the /etc/udev/rules.d/ directory with a name ending in .rules (for example, 51-usb-blacklist.rules).
# sudo nano /etc/udev/rules.d/51-usb-blacklist.rules
Add the following line to the file, replacing VENDOR_ID and PRODUCT_ID with the vendor ID and product ID of the USB device you want to pass-through.
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="VENDOR\_ID", ATTR{idProduct}=="PRODUCT\_ID", RUN+="/usr/bin/udevinfo -q property -n %n | /usr/bin/grep -q 'DRIVER=' && /bin/echo '0' > /sys/%n/driver/loadFor the example USB keyboard, the line would look like this:
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="138a", ATTR{idProduct}=="0011", RUN+="/usr/bin/udevinfo -q property -n %n | /usr/bin/grep -q 'DRIVER=' && /bin/echo '0' > /sys/%n/driver/loadSave and close the file.
Step 3: Reload the udev Rules
After creating the udev rule, you need to reload the udev rules for the changes to take effect.
# sudo udevadm control --reload-rules
And restart the udev service.
# sudo service udev restart
Step 4: Pass-through the USB Device to the Guest Virtual Machine
Finally, you can pass-through the USB device to the guest virtual machine using the virsh command.
# virsh attach-device VM\_NAME /path/to/usb-device.xml
Where VM\_NAME is the name of the guest virtual machine and /path/to/usb-device.xml is the path to the XML file that describes the USB device.
In this article, we discussed how to prevent the loading of a specific USB device driver in Debian Linux KVM virtual machines. This is particularly useful when you have host USB devices attached and would like to pass-through one of them to a guest virtual machine. We covered the following key concepts:
- Identifying the USB device
- Creating a udev rule to prevent the loading of the USB device driver
- Reloading the udev rules
- Pass-through the USB device to the guest virtual machine