How to mount a qcow2 disk image on Ubuntu

This is a quick guide to mounting a qcow2 disk images on your host server. This is useful to reset passwords, edit files, or recover something without the virtual machine running.

Step 1 – Enable NBD on the Host
You will need to enable the nbd (network block device) kernel module on the host. This should be available on all Ubuntu servers after Intrepid.

sudo modprobe nbd max_part=8

Step 2- Connect the QCOW2 as network block device
Use qemu-nbd to connect your qcow2 file as a network block device. Note: You will need to specify the full path of qcow2 file even if you are the directory of the qcow files.

sudo qemu-nbd --connect=/dev/nbd0 /mnt/kvm/wordpress-sites.qcow2

Step 3 – Find The Virtual Machine Partitions
You can find a list of the partitions on the vm using the following command

sudo fdisk /dev/nbd0 -l

Step 4 – Mount the partition from the virtual machine.
For example, I want to mount partition 1 and therefore run:

 sudo mount /dev/nbd0p1 /mnt/somepoint/

Step 5 – Browse Your VM
You can then just browse the files in mnt/somepoint/

Afterwards unmount:

 sudo umount /mnt/somepoint/

Then disconnect the disk:

sudo qemu-nbd --disconnect /dev/nbd0

7 thoughts to “How to mount a qcow2 disk image on Ubuntu”

  1. This didn’t work for me. The qemu-nbd command couldn’t find the qcow2 file, even though it was in the same directory as the command was issued from. This was on Ubuntu 12.04 LTS.

  2. For some reason you need specify the full path to your qcow file, even if you are running qemu-nbd from the same directory as the qcow file.

    $ sudo qemu-nbd -c /dev/nbd0 `pwd`/foo.qcow2

  3. This is very cool but I ran into problems because the disk image was an logical volume.
    It’s still nice to know about.

Leave a Reply