KVM是通过qemu来支持USB设备的,可以在启动的时候就指定需要连接的USB设备,也可以系统启动后动态的添加删除.通过qemu的help可知,使用qemu的usb_add host:xxx:xxx来添加usb设备,那么如何知道对应的host:xxx:xxx呢,有2种方法:
1)在host主机ubuntu中执行lsusb得到这些信息 比如在我机器上usb的信息如下:1 Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub2 Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub3 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub4 Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub5 Bus 001 Device 003: ID 0461:4d80 Primax Electronics, Ltd 6 Bus 001 Device 006: ID 0951:1665 Kingston Technology 7 Bus 001 Device 004: ID 058f:b002 Alcor Micro Corp. 8 Bus 002 Device 003: ID 0489:e010 Foxconn / Hon Hai
0951:1665就是我们需要的了。
2)进入服务端虚拟机后,通过ctrl+alt+2调出qemu控制台,输入info usb来列出相关已经加载的USB信息。如果显示:
(qemu): usb support not enabled
If you see 'USB support not enabled', confirm to use option -usb at startup.
-usbThis option adds pseudo USB mouse device to a guest OS.-usbdevice tabletWhen a guest OS has a driver for USB tablet, a mouse doesn't need to click in the guest OS windows to capture.-usbdevice host:xxxx:yyyyA USB device xxxx:yyyy on a host OS can be used from a guest OS.
3.)Press Ctrl-Alt-2 and check USB device ID of host OS in QEMU monotor.
Then connect USB device (0951:1665) to the guest OS. 0951 is a vender ID and 1665 is a product ID.
(qemu) usb_add host:0951:1665
Finally, PnP manager of WindowsXP guest recognizes USB mouse and a device driver is installed.
4.)When you want to remove the USB device, remove USB device in guest OS. For example, right click on task bar and select remove the device. The mouse doesn't need to do this. Then check a number of USB bus and address in QEMU monitor.
Use usb_del in QEMU monitor. 0.3 means that the bus number is 0 and the address of USB device is 3.
(qemu) usb_del 0.3
Then remove USB device in host OS. For example, right click on the task bar and remove the USB device as in guest OS. The mouse doesn't need to do this.
After that, unplug the USB device on your machine.