49

I've read that with qemu-nbd and the network block device kernel module, I can mount a qcow2 image. I haven't seen any tutorials on mounting a qcow2 via a loop device. Is it possible? If not, why?

I don't really understand the difference between a qcow2 and an iso.

3

6 Answers 6

56

Step 1 - Enable NBD on the host

modprobe nbd max_part=8

Step 2 - Connect the QCOW2 as a network block device

qemu-nbd --connect=/dev/nbd0 /var/lib/vz/images/100/vm-100-disk-1.qcow2

Step 3 - List partitions inside the QCOW2

fdisk /dev/nbd0 -l

Step 4 - Mount the partition from the VM

mount /dev/nbd0p1 /mnt/somepoint/

You can also mount the filesystem with normal user permissions, ie. non-root:

mount /dev/nbd0p1 /mnt/somepoint -o uid=$UID,gid=$(id -g)

Step 5 - After you're done, unmount and disconnect

umount /mnt/somepoint/
qemu-nbd --disconnect /dev/nbd0
rmmod nbd

Shamefully stolen from: https://gist.github.com/shamil/62935d9b456a6f9877b5

1
  • 16
    :) I'm the author of this gist. Thanks for sharing... Jul 20, 2020 at 18:00
36

Thanks to Gilles for pointing out guestmount. Mounting a qcow2 image is very simple:

  1. First install guestmount (comes as part of libguestfs-tools or libguestfs in Fedora/Debian/Arch/etc)

    yum install libguestfs-tools
    
  2. Then you should be able to auto-magically mount your qcow2 image using the -i option

    guestmount -a path_to_image.qcow2 -i --ro /mount_point
    

You can manually specify mount points (within the image) using the -m option.
As always read the man page on guestmount for more details...

Note: This only addresses the question title. Please see Peter's answer for the differences between qcow2 and ISOs...

3
  • Since jammy Ubuntu also has a package guestmount, which seems to be the same thing. Dec 22, 2022 at 23:33
  • Ugh... Debian 11: apt-get install libguestfs-tools: 379 packages newly installed, 251 MB of archives, after unpacking 861 MB occupied. I'll try to avoid that.
    – frr
    Jan 23, 2023 at 15:02
  • on Arch the package is called libguestfs.
    – zs11
    Jul 4, 2023 at 15:29
17

A loop device just turns a file into a block device. If the file has some special internal mapping of its blocks, the loop device won't translate any of it. qcow2 is special... it has special mapping inside that handles different snapshots of the same blocks stored in different places. If you mount that as a loop device, you'll just get one big block device that doesn't represent the actual data in the image.

Another option is to convert to raw and mount as a loop device:

qemu-img convert -p -O raw oldfile.qcow2 newfile.raw

But then you have to convert it back to qcow2 to use it again as before.

I think using qemu-nbd is not the most efficient IO, but is easy. Mounting it in a VM, like one booted with a live usb, is easy too. Converting doesn't make much sense... it was just an example of how they're different.

3
  • 4
    guestmount is the easy way! It does the mounting-in-a-vm under the hood, but it's all wrapped up neatly. Mar 8, 2016 at 23:08
  • nice! I hadn't heard of that.
    – onlyanegg
    Mar 10, 2016 at 4:18
  • Thanks, it is the best&simplest solution! Jun 6, 2021 at 14:26
3

There's also a way to do this with FUSE with nbdfuse. It has the advantage that you do not need root access, but it won't be as efficient as the NBD module approach given in other answers:

$ touch /tmp/file.raw
$ nbdfuse /tmp/file.raw [ qemu-nbd -f qcow2 file.qcow2 ] &
$ ls -l /tmp/file.raw
 -rw-rw-rw-. 1 nbd nbd 1073741824 Jan  1 10:10 /tmp/file.raw
0

Debian 10 with QEMU inside VirtualBox. Need to copy some big files to a QEMU VM. Network speed was too slow.

  • Install guestmount from libguestfs-tools as suggested above.
  • Stop emulator to free up qcow2 file.
    sudo guestmount -a /path/to/some.qcow2 -i --rw /path/to/tempdir
    
    Then I've had read/write access to the filesystem. Copy speed more than 5 MB/s.
  • Unmount:
    guestunmount /path/to/tempdir
    
-3

I could not make guestmount work on my VirtualBox machine and qemu-img convert was not an option or maybe I just did not know the way to convert into a format where the disk was not of full size defined for Qcow2 - it wanted to create 40GB of VDI apparently although there was only few GB of disk used.

So I looked for something different and found qemu-nbd to be a great alternative especially if you have many partitions on the disk. It is a bit more commands but it is still faster than the path I would have to go to make guestmount to work on my VirtualBox VM.

The manual for qemu-nbd is realtievely simple but you can see here for further advice.

It worked like a charm. So in case you cannot get over the mount error 1, you can try this.

1
  • 2
    Give a command example, how to mount using qemu-nbd
    – Krackout
    Jul 3, 2020 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .