Mapping USB drive direct to libvirt KVM virtual machine?

I am searching for information on mapping an existing drive (USB drive) to a KVM virtual machine using libvirt. I have been going over http://libvirt.org/formatstorage.html#StoragePoolTarget. But I don't quite understand what options are needed to expose an existing device to the VM (or if it is even possible).

From what I am reading, the target element is expecting a path element to a folder on the machine to store a file based image. Can the path be a device such as /dev/sdc1? with a type of raw?


Solution 1:

Check out the 'USB passthrough | Using Libvirt' section of this page from the Edubuntu wiki. More info here and here.

If you don't ever plan to hot plug/unplug the usb drive while the guest is running you could also try adding a section like

<disk type='block' device='disk'>
  <driver name='qemu' type='raw'/>
  <source dev='/dev/sdb'/>
  <target dev='sdb' bus='scsi'/>
</disk>

to your VM's xml definition file. Just change the source dev to match what was assigned to your usb when it was plugged in. However, you can't guarantee the usb's dev assignment will survive reboot if other devices are added or removed, so I'd recommend the approach in the first link above instead.

In case the link disappears, here is the relevant USB passthrough section of the linked docs:

Using Libvirt

fired up a pre-existing vm

virsh start maverick2

plugged in a usb drive
found the usb address using lsusb, which gave me
{{ Bus 002 Device 006: ID 1058:1023 Western Digital Technologies, Inc. }}}

defined a xml file with the device info:

<hostdev mode='subsystem' type='usb'> <source> <vendor id='0x1058'/> <product id='0x1023'/> </source> </hostdev>
passed the usb drive to the vm

sudo virsh attach-device maverick2 /tmp/a.xml

HOWEVER this does not work with apparmor enabled. You must either disable apparmor, or add

/dev/bus/usb/*/[0-9]* rw,

to either /etc/apparmor.d/libvirt-qemu (which gives all guests full access to physical host devices) or to

/etc/apparmor.d/libvirt/libvirt-<uuid>

which will give only the one guest that access. (Thanks to jdstrand for help getting that straight.)