KVM - The Linux Kernel-Based Virtual Machine
News, Blogs and Resources on the Linux (KVM) Kernel-Based Virtual Machine

USB redirection over the network

USB Redirection allows you to make usb devices attached to your client machine accessible to your remote KVM virtual machine via the network. This qemu/kvm feature has been in development for some time with a first working version available around middle of last year, 2011.  

There are two modes of usb redirection in development; one mode which uses the spice protocol and a second which uses a tcp server.  The tcp server has no dependence on spice protocol whatsoever and so allow extensions to be added to tcp remoting protocols like vnc, for example, to take advantage of this feature. 

For using usb redirection over the spice protocol there's a GUI tool called spicy and for using the tcp server there are currently features added to virt-manager but I won't be looking at that in this post. For now I'll be taking a look at the procedure for usb redirection using the spice protocol.

USB redirection with the spice protocol requires two basic steps.

  • start your guest virtual machine with the required amount of usb spice channels
  • attach usb devices to your remote guest virtual machines

Below outlines these two steps



Start your virtual machine with SPICE usb channels

The amount of usb devices you can attach over the network to a single virtual machine is dependent on how many spice usb channels your guest was started with, the default being none.  If you would like to add spice usb channels then the simplest way is to start your guest virtual machine with virt-manager and add "usb redirection" virtual hardware as shown below.  


usb redirection


Add a separate device for each usb channel you require if you need to access to more than one usb devices simultaneously.  For example if you wanted the ability to add two usb devices simultaneously you would add two "usb redirection" to your virtual machine.  Your virt-manager guest details page would then look like the following snapshot.


usb channels


Be aware though that changing the spice usb channels in virt-manager will require a reboot of your guest virtual machine to take effect so think about this to minimize downtime due to reboots. 



Attach usb devices to your remote virtual machine

There's a GUI tool called spicy which , as the name suggests, is a spice client for remoting to your virtual machine.  On fedora, installing this client requires the spice-gtk-tools package which you can easily install using the following yum command

yum install spice-gtk-tools

Once installed you can run the following command to connect to your KVM virtual machine where is the ip address of your kvm linux virtual server and 5901 is the spice server port for your guest.

spicy -h -p 5901 -w password

You also could just run the command spicy with no arguments which will give you a GUI where you can then enter host, port and password for connecting to your spice enabled KVM guest.  Personally I just specify everything on the command line as this saves me time. 

spicy dialog

Once connected to your guest virtual machine, select the following menu on the spicy gui client. Input -> Select USB Devices for redirection.  


spice usb redirection


This will bring up the following dialog showing usb devices available for tunneling to your guest virtual machine.  

usb devices for redirection

Once you've selected your usb device for redirection the dialog will tell you if you have any more usb channels available. 

usb channels available

Your guest virtual machine will now show the usb device attached.  If you're doing this across a WAN, say from home to your office, then be patient as it can take a while before all the data on your usb device is available to your guest virtual machine.  It all depends on available network bandwidth between your client machine and your guest virtual machine. 

To disconnect your usb device in your guest, disconnect as you normally would and use the same dialog in spicy under Input -> Select USB Devices for redirection to uncheck your usb devices from redirection.



Final thoughts

USB redirection is a nice feature of KVM with many possible use cases.  Two that immediately come to mind include backing data from your virtual machine locally or attaching usb devices that provide additional functionality to your guest virtual machine.  All this without having physical access to your KVM virtual server.

The big downside to the method explained in this post is having to use multiple management tools to use this feature.  In this case it required using virt-manager to configure and start/stop your virtual machine and a separate tool called spicy to perform the usb redirection to your guest.  

If you're aware of other tools that provide this functionality or more interesting use cases I'm interested in hearing so please post a comment. 

See Also



You will obtain better devices support by switching the controller to USB2. You can do so from the virt-manager USB Controller details page (that video shows how to switch http://elmarco.fedorapeople.org/videos/virt-manager-usbredir.webm)

spicy is a testing tool, not really meant for end users. virt-viewer is better supported. It shouldn't be difficult to add the USB redirection dialog in virt-manager, there is room contributors, as often!

Another downside of current solution is that you can't migrate or suspend to disk a VM with USB redirection. For this reason, Boxes doesn't yet support USB redirection (although it can with pre-configured VM, although only in plugging auto-redirect mode atm)

Re: my2cents

The spicy tool is in fact a testing tool, you're right. I chose that over virt-viewer just because it's simpler to get going with it.  I haven't played with virt-viewer much just because I didn't take time to figure out the command line options.

Snapshots fail

No snapshots possible after adding usb redirect. "savevm" fails because of "Unmigratable device"

USB 2.0 speed

Hi everyone,

After a bit of struggle, i managed to build my own qemu binaries (1.1.1) to test that nice usb redirection on a centos box. It's working fine, except for the speed of data transfer. i'm getting a maximum of 5MB/s for USB 2.0 hdd (supposed to work around 30 MB/s). Can you share the speed you are getting while transfering data from usb hdd ?
Thank you

Re: usb 2.0 speed

I would expect a significant enough performance hit compared to directly attached usb.  Not only due to network redirection but within qemu itself.  I have not done any testing yet but will try to do some testing this week and provide some feedback.

Great, i'm looking forward to

Great, i'm looking forward to read your results

Wow!! What a great writing,

Wow!! What a great writing, really I appreciate such kind of topics. It will be very helpful for us. Waiting for more articles, blogs like this. I’m going bookmark your blog for future reference. Thanks a lot for sharing

Useless Function, Its similar

Useless Function,

Its similar to other Desktop Virtualisation Products but KVM should Focus for Serverside applications first bevore making such steps.

Like Working USB Redirection (with choseable USB LEvel like USB1, USB2, USB3) and working PCI Passtrough, or at least better I/O, Live snapshots, or at Least better Diskformats

Serisously, i hate vmware but the I/O on KVM is a real Dealbreaker

Post new comment

The content of this field is kept private and will not be shown publicly.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.