Access a qcow2 Virtual Disk Image From The Host
Category : How-to
A disk image, such as the popular qcow2 disk image can be read and used as a file system without having to attach it to a running VM. That can be handy when you’ve got information on a backed up virtual image and don’t want to turn on a whole VM in order to access some data held on it.
If you’re using a host such as Proxmox then you’ll already have everything installed, but if you’re on some other Debian based system then you’ll need to install the required package:
apt-get install qemu-utils
What we’re trying to achieve is a standard mount point on the host that we can access like we would any other mounted block device. As you can imagine, it’s a little more tricky than just using a mount command along with a file name, but not by much.
Make sure that you have the required kernel module, nbd, loaded:
modprobe nbd
You should then find that you have plenty of object in /dev starting with nbd:
ls /dev/nbd* /dev/nbd0 /dev/nbd10 /dev/nbd12 /dev/nbd14 /dev/nbd2 /dev/nbd4 /dev/nbd6 /dev/nbd8 /dev/nbd1 /dev/nbd11 /dev/nbd13 /dev/nbd15 /dev/nbd3 /dev/nbd5 /dev/nbd7 /dev/nbd9
Each one of these devices is something you can use to attach a virtual image to, however you can only attach one image per device giving you a total of 16 images you can use at any one time.
Attach a qcow2 Virtual Image File
To attach an image file to one of these devices run the below command, substituting the nbd0 device and /var/lib/vz/images/107/vm-107-disk-1.qcow2 with your own values.
qemu-nbd -c /dev/nbd0 /var/lib/vz/images/107/vm-107-disk-1.qcow2
The device /dev/nbd0 will now contain the virtual image file as a block device and any partitions or volumes on the virtual image will be available for mounting.
You can check the partitions available on the virtual disk using your favorite partitioning tool, gparted, fdisk, etc:
partx -l /dev/nbd0
Partitions are named slightly differently to what you may be used to. With a normal partitioned disk (with no LVM) you’d reference the first partition with /dev/nbd0p1. For example, using a mount command you might use the below:
mount /dev/nbd0p1 /mnt/mntpoint
If you use LVM on the virtual disk image then you won’t be able to mount the partition directly – you’ll need to use the vg suite of tools to detect the logical volume. Run the two below commands vgscan and vgchange as below to detect the logical volumes.
vgscan Reading all physical volumes. This may take a while... Found volume group "pve" using metadata type lvm2 vgchange -ay 3 logical volume(s) in volume group "pve" now active
You can then use pvdisplay to find your volume name and mount it.
lvdisplay --- Logical volume --- LV Path /dev/pve/myvolume LV Name myvolume VG Name pve LV UUID jgok7M-c9x1-dTdt-PXXh-8NXf-BzgG-aRsaY7 LV Write Access read/write LV Creation host, time proxmox, 2015-04-06 20:28:28 +0100 LV Status available # open 1 LV Size 20.00 GiB Current LE 5120 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
mount /dev/pve/myvolume /mnt/mntpoint
Detach a qcow2 Image Virtual Image File
Once you have finished with the virtual image file, you’ll want to detach it and release the nbd process used for IO operations for that image. Assuming that any mounts based on the image have been umount‘d use the qemu-nbd command with the -D switch:
qemu-nbd -d /dev/nbd0
You can also remove the kernel module if you’ve detached all of your virtual images from their /dev/nbdX device:
rmmod nbd