As per this article: https://www.linux-kvm.org/page/USB I can create a disk image and attach it as a USB drive to my running VM.
I tried this, via qemu-monitor-command
option of virsh
, but somehow wherever
I place the img
file, its unable to access!
$ sudo virsh qemu-monitor-command --hmp MyVM1 'drive_add 0 id=my_usb_disk,if=none,file=/tmp/usb.img'
Could not open '/tmp/usb.img': Permission denied
$ ls -l /tmp/usb.img
-rwxrwxrwx 1 libvirt-qemu kvm 8388608000 Apr 16 19:02 /tmp/usb.img
Unless I run this, I dont think I can do device_add
?!
This is my qemu version (on Bionic - 18.04):
$ qemu-system-x86_64 --version
QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.4)
Is there any other way (say, libvirt
XML) where I can attach a raw image as a USB drive?
(I searched in libvirt docs, I could not find any example for this)
What am I missing here ?
-- Thanks
You didn't outline the reasoning for this and before helping I wanted to let you know that "attaching as USB" in most cases is a bad decision, most other ways (virtio disk, scsi disk, passthrough, ...) are much better to pass a block device (if backed by a file or real) to the guest. Passing USB to guests is usually only used to pass real devices through to the guest.
But you might have a specific test in mind so for now let me assume there is a reason for using USB here :-) If you need an image as USB disk there are two things to do.
First if you use libvirt you can as well use it here for more than just passing your commands. It does most complex work for you (that is what it is for):
Of course you can convert this to an XML (or directly use an XML) for a more programmatic approach, but the above works fine.
And seconds you need to be aware of common and uncommon paths. If you just happen to attach a file to qemu it will be blocked by apparmor isolation - see the section in the server guide. You need to tell libvirt what path you use to make it allow that path. In my above example that all is done for you and just works. There is a set of paths defined for where all this can happen automatically and e.g. /var/lib/libvirt/images + telling libvirt what you do with attach-disk is one of them.
If need to have it run on other paths you need extra rules for virt-aa-helper in
/etc/apparmor.d/local/usr.lib.libvirt.virt-aa-helper
.And if further you "insist" to have to do it manually instead through libvirt you'd need to add a rule that allows the guest a given path/file to /etc/apparmor.d/libvirt/libvirt- and then reload that profile (or restart the guest). Then that path would be allowed for qemu to access.