Access a qcow2 Virtual Disk Image From The Host

Access a qcow2 Virtual Disk Image From The Host

Get Social!

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:

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:

You should then find that you have plenty of object in /dev starting with nbd:

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.

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:

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:

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.

You can then use pvdisplay to find your volume name and mount it.

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:

You can also remove the kernel module if you’ve detached all of your virtual images from their /dev/nbdX device:

 


Related posts:


3 Comments

Andrew

3-Jun-2017 at 12:24 pm

Very interesting and useful information.
I would like to know if before attaching qcow2 Virtual Image File i need to stop the virtual machine which uses the qcow2 image or can it work in parallel?

    Andrew

    3-Jun-2017 at 12:46 pm

    Can i to copy files from the mounted image to other partitions of my server?
    I want to recover some files from the qcow2 to my computer. This is possible?

    Thank you

      James Coyle

      5-Jun-2017 at 8:46 am

      Yes, sure. Just mount the image and use it as a local file system.

Leave a Reply

Visit our advertisers

Search

Quick Poll

How many Proxmox servers do you work with?

Visit our advertisers