Ubuntu – Is it possible to run qemu-system-x86_64 virtual machine from raw disk image located on external flash drive

kvmpermissionsqemuvirt-managervirtualization

For some project I need to have QEMU/KVM virtual machine with raw disk image.
So I have created this raw disk image as simple as

dd if=/dev/zero of=~/disk.img bs=1M count=3092

and got 3 Gb of space on it.

Then I have set-up new virtual machine from Virtual Machine Manager (virt-manager) on Ubuntu 16.04 LTS using my normal account. So I have installed the Ubuntu from mini.iso to inside the ~/disk.img. My user is a member of libvirtd group. I can boot the VM when virtual disk image is inside my home folder.

Then I moved this file to the external FAT32 (vfat) medium which is mounted read-and-write. So it is located inside /media/username/FLASH/disk.img. I specified this path in the Virtual Disk setting section of the virtual machine.

But when I try to start this machine I get the following error message:

Error starting domain: internal error: process exited while connecting to monitor: 2020-01-22T21:45:20.221697Z qemu-system-x86_64: -drive file=/media/username/FLASH/disk.img,format=raw,if=none,id=drive-ide0-0-1: Could not open '/media/username/FLASH/disk.img': Permission denied

And this message persists even if I run the Virtual Machine Manager as root using sudo virt-manager.

The permissions of this file are the following:

$ ls -al /media/username/FLASH/disk.img 
-rw-r--r-- 1 username username 3221225472 Jan 23 00:18 /media/username/FLASH/disk.img

Also I tried to create a symbolic link using ln -s /media/username/FLASH/disk.img ~/disk.img and set machine to use the latter disk path. But it does not work too.

How can I boot QEMU/KVM virtual machine from the disk image located on external storage?

Best Answer

I had this permissions issue when running VM's from a thumb drive.

The problem I was having was that my system (Fedora 27) automatically mounts new drives (external HDDs, USB, SD) to

/run/media/(username)/(device name) (on your system it's /media/username/....)

For whatever reason this causes the permissions issue. By default it was mounted with fmask=0022 and dmask=0022 (use man mount for details). To fix the issue we need to unmount the drive from that location as root, and then mount it to /mnt. In the example below the thumb drive is /dev/sdc1. We need to remount it with fmask=0011 to allow write access for all users and groups.

# su  -     (then enter root password)
# umount  /run/media/yourusername/devicename
# mount  /dev/sdc1 /mnt -o fmask=0011        (mount usb to /mnt)

After that I just made sure to have symbolic links from the original location of the qcow2 drives to the new location (still as root)

# cd  /var/lib/libvirt/images
# ln  -s  (new target)  (link name)   

so if your VM is named MyVM

# ln  -s  /mnt/Myvm.qcow2  MyVm.qcow2

Once I saw that my VMs were now working, I edited /etc/fstab to automatically mount the drive at /mnt. The folders and qcow2 drives were owned by my standard user account and group. I am sure there are better ways to do it, but this is what worked for me.

Related Question