How to mount qcow2 disk image

img

Use nbd module and qemu-nbd.

sudo modprobe nbd max_part=8
sudo qemu-nbd --connect=/dev/nbd0 imagename.qcow
sudo partprobe
mkdir -p /tmp/vm-ro
sudo mount -o ro /dev/nbd0p2 /tmp/vm-ro

#when done
umount /tmp/vm-ro
qemu-nbd -d /dev/nbd0
rmmod nbd

You can safely mount RW if the image is not used by a VM, or VM is stopped. It can be a quick alternative to virt-customize if you need to make simple modifications on an image.

Kernel module ?

I just came across this paper. The guy created a kernel module, qloop to use a qcow2 image directly like a block device.

losetup /dev/qloop0 file.qcow2

That is all. Now, the device file /dev/qloop0 will behave like a block device containing all the data in the disk described in file.qcow2.

losetup -d /dev/qloop0 #when done

The paper also summaries all the current methods to mount qcow2 image and even if it's from 2010, it's still worth mentioning.

Links: