wiki:Linux/MountQemuQcowImages

Mount KVM/QEMU QCOW images

Since around kvm-77 the qemu-nbd program (known as kvm-nbd on Debian/Ubuntu?) has been available. It is a Network Block Device server that can mount a QCOW/QCOW2 disk image (created by qemu-img) on a host.

This is really handy for preparing the contents of a disk image without starting a virtual machine guest, or to copy files to/from the disk image without starting the virtual machine guest and having to use some form of network protocol to do it.

First, ensure the nbd kernel module is loaded:

sudo modprobe nbd

Now create a local block device (the process is put into the background):

sudo kvm-nbd -c /dev/nbd0 ubuntu-jaunty-desktop.qcow2 &
NBD device /dev/nbd0 is now connected to file ubuntu-jaunty-desktop.qcow2}}}
The block device is now found at `/dev/nbd0` so it can be treated like a physical disk. For example:
{{{
sudo cfdisk -P s /dev/nbd0
Partition Table for /dev/nbd0

               First       Last
 # Type       Sector      Sector   Offset    Length   Filesystem Type (ID) Flag
-- ------- ----------- ----------- ------ ----------- -------------------- ----
 1 Primary           0      208844     63      208845 Unknown (27)         None
 2 Primary      208845      803249      0      594405 HPFS/NTFS (07)       None
 3 Primary      803250     1799279      0      996030 Linux swap / So (82) None
 4 Primary     1799280    10474379      0     8675100 Extended (05)        None
 5 Logical     1799280     2104514     63      305235 Linux (83)           None
 6 Logical     2104515    10474379     63     8369865 Linux (83)           None
}}}
To disconnect the block device use `nbd-client`:
You might need to install the package:
{{{
sudo apt-get install nbd-client
}}}
To disconnect:
{{{
sudo nbd-client -d /dev/nbd0
}}}