Mapping USB drive direct to libvirt KVM virtual machine?
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 usinglsusb
, 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 vmsudo 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.)
Related videos on Youtube
Andy Shinn
Updated on September 18, 2022Comments
-
Andy Shinn almost 2 years
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 apath
element to a folder on the machine to store a file based image. Can thepath
be a device such as/dev/sdc1
? with atype
ofraw
? -
Andy Shinn over 12 yearsThanks, that page was very helpful. I did get it working. But instead of a vendor and product, I used the
address
element. When using vendor and product I couldn't mount 2 USB drives that were the same. It would only pass through the first one.