Crmgo Blog

KVM, USB Device Passthrough

KVM, USB Device Passthrough

Find out which bus the USB device is on: # lsusb Bus 007 Device 002: ID 058f:6387 Alcor Micro Corp. Transcend JetFlash Flash Drive Bus 007 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux…

KVM, USB Device Passthrough

 

Find out which bus the USB device is on:

# lsusb Bus 007 Device 002: ID 058f:6387 Alcor Micro Corp. Transcend JetFlash Flash Drive Bus 007 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

Create a configuration file for the device, following this format (using the data obtained earlier):

# cat usb.device.xml <hostdev mode='subsystem' type='usb' managed='yes'> <source> <address bus='007' device='002'/> </source> </hostdev>

Attach the device to the VPS:

# virsh attach-device <domain> <file>

In our case, it looks something like this:

# virsh attach-device vm300 usb.device.xml

Where vm300 is the name of the VM (VPS) or domain, as it is commonly referred to. And usb.device.xml is the XML configuration file containing the USB device data.

Thus, the device should immediately become available inside the guest OS. To make these changes persistent, i.e., to update the XML configuration of the virtual machine itself, run:

# virsh dumpxml <domain> > /full/path/to/<domain>.xml # virsh define <file>

For our example, this looks like:

# virsh dumpxml vm300 > vm300.xml # virsh define vm300.xml

The full configuration might look like this:

<domain type='kvm' id='23'> <name>vm300</name> <uuid>11b6aa11-e215-e4f0-20a3-c76a0c0958b7</uuid> <memory>524288</memory> <currentMemory>524288</currentMemory> <vcpu>1</vcpu> <os> <type arch='i386' machine='pc'>hvm</type> <boot dev='hd'/> </os> <features> <apic/> <pae/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/bin/kvm-freebsd</emulator> <disk type='block' device='disk'> <source dev='/dev/pool1/vm300'/> <target dev='hda' bus='ide'/> </disk> <disk type='file' device='cdrom'> <source file='/home/isos/freebsd/7.4/FreeBSD-7.4-RELEASE-i386-disc1.iso'/> <target dev='hdc' bus='ide'/> <readonly/> </disk> <interface type='bridge'> <mac address='54:52:00:79:b5:eb'/> <source bridge='kvmbr0'/> <target dev='vnet2'/> </interface> <interface type='bridge'> <mac address='54:52:00:47:5c:72'/> <source bridge='kvmbr1'/> <target dev='vnet3'/> </interface> <serial type='pty'> <source path='/dev/pts/6'/> <target port='0'/> </serial> <console type='pty' tty='/dev/pts/6'> <source path='/dev/pts/6'/> <target port='0'/> </console> <input type='mouse' bus='ps2'/> <hostdev mode='subsystem' type='usb'> <source> <address bus='7' device='2'/> </source> </hostdev> </devices> </domain>

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.

Categories: KVM