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:

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

 


7 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.

Brian Candler

17-Oct-2019 at 9:18 am

> 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?

You definitely need to stop it: two machines accessing the same block filesystem will result in corruption, and there may be “dirty blocks” in the VM which haven’t been written out yet.

Note that there are dangers in mounting unknown/untrusted filesystems directly on your host. A safer way is to use guestfish (libguestfs-tools), which runs a short-lived VM for accessing the filesystem:

$ virt-list-filesystems -a hda_disk.qcow2 -l
/dev/sda1 vfat

$ virt-ls -a hda_disk.qcow2 -m /dev/sda1:/ -l /
… shows directory contents

$ virt-cat -a hda_disk.qcow2 -m /dev/sda1:/ /etc/passwd
… reads the file

$ guestfish -a hda_disk.qcow2 -m /dev/sda1:/ — upload passwd.new /etc/passwd
… writes into the VM image

If guestfish gives a “supermin” permissions error, `sudo chmod +r /boot/vmlinuz-*` is normally the solution.

    James Coyle

    24-Oct-2019 at 3:54 pm

    Thanks Brian.

Bruce N

13-Jul-2020 at 1:16 pm

Hi James,

I have a Windows server VM with qcow disk. If I mount it as you mentioned should I be able to read the Windows folder structure and all files? Or are your instruction for a Linux system qcow disk only.

Thanks,

Joe Mahmuh

7-Nov-2021 at 7:56 pm

Hey James,

This helped me a lot. However, I need to know how to be able to access my linux qcow disk through SSH. If you could help me out, I would be grateful. Thanks

Leave a Reply

Visit our advertisers

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers