Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to mount a qcow2 disk image

How to mount a qcow2 disk image

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

modprobe nbd max_part=8

Step 2 - Connect the QCOW2 as network block device

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

Step 3 - Find The Virtual Machine Partitions

fdisk /dev/nbd0 -l

Step 4 - Mount the partition from the VM

mount /dev/nbd0p1 /mnt/somepoint/

Step 5 - After you done, unmount and disconnect

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

This comment has been minimized.

Copy link

@poma poma commented Jan 27, 2018

In my case partition didn't receive its own file

root@master# fdisk /dev/nbd0 -l
Disk /dev/nbd0: 501 GiB, 537944653824 bytes, 1050673152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000198dd

Device      Boot Start        End    Sectors  Size Id Type
/dev/nbd0p1         63 1050673151 1050673089  501G 83 Linux

root@master# mount /dev/nbd0p1 /mnt/web/
mount: special device /dev/nbd0p1 does not exist

Fixed by running partx -a /dev/nbd0

@joshenders

This comment has been minimized.

Copy link

@joshenders joshenders commented Feb 5, 2018

Thanks @poma. I had to run partx -a /dev/nbd0 to create the /dev/nbd0p* device nodes before they could be mounted as well on Ubuntu 14.04

@GraysonPeddie

This comment has been minimized.

Copy link

@GraysonPeddie GraysonPeddie commented Dec 31, 2018

I found the step-by-step instructions in Google search engine and yours is very useful. Thanks.

I have shared your step-by-step instructions in Twitter.
https://twitter.com/GraysonPeddie/status/1079875947111821313

@ykfq

This comment has been minimized.

Copy link

@ykfq ykfq commented Jan 11, 2019

Error:

modprobe: FATAL: Module nbd not found.

# modprobe nbd max_part=8
modprobe: FATAL: Module nbd not found.

# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

# uname -a
Linux controller50 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
...
gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)

@SwetaleenaDash

This comment has been minimized.

Copy link

@SwetaleenaDash SwetaleenaDash commented Jan 17, 2019

command:
root@GTAPC:~# qemu-nbd --connect /dev/nbd1 /root/Automation/Gold/Working/QCOW/QCOW_VM1.qcow
Error:
Failed to open /dev/nbd1: No such file or directory
Disconnect client, due to: End of file

what doe this error mean?
(/root/Automation/Gold/Working/QCOW/QCOW_VM1.qcow is path of QCOW file)

@mluppov

This comment has been minimized.

Copy link

@mluppov mluppov commented Jan 19, 2019

modprobe nbd max_part=8
modprobe: FATAL: Module nbd not found.

Since modprobe is a tool for loading kernel modules, the support for network block device aka nbd in your kernel was not compiled as a module. Try to just skip this part and go to step 2. In case it will fail too, you don't have nbd support at all. In this case you need to include it.

@mluppov

This comment has been minimized.

Copy link

@mluppov mluppov commented Jan 19, 2019

Error:
Failed to open /dev/nbd1: No such file or directory

Most likely you either didn't load kernel module first or it failed to load and you did not pay attention to the error message. It may also be you don't have nbd support in your kernel at all.

@SwetaleenaDash

This comment has been minimized.

Copy link

@SwetaleenaDash SwetaleenaDash commented Feb 28, 2019

while doing step 4, i am getting the issue "mount: special device /dev/nbd1p2 does not exist". why am i getting this error?
after running "partx -a /dev/nbd0" also i am getting the error "partx: /dev/nbd1: failed to read partition table"

@MuralidharB

This comment has been minimized.

Copy link

@MuralidharB MuralidharB commented Oct 14, 2019

nbd module is not shipped with Fedora based distributions including centos/fedora/rhel. RedHat decided against shipping nbd kernel module due to security reasons. nbd module is only available on debian distributions.

Otherwise qemu-nbd is a nice tool to mount qcow2 images. The only other option is to use guestfish to mount and access individual files from qcow2 images.

@llegolas

This comment has been minimized.

Copy link

@llegolas llegolas commented Jan 7, 2020

nbd module is not shipped with Fedora based distributions including centos/fedora/rhel. RedHat decided against shipping nbd kernel module due to security reasons. nbd module is only available on debian distributions.

Otherwise qemu-nbd is a nice tool to mount qcow2 images. The only other option is to use guestfish to mount and access individual files from qcow2 images.

Not entirely correct.

$ lsb_release -a
LSB Version:	:core-4.1-amd64:core-4.1-noarch
Distributor ID:	Fedora
Description:	Fedora release 31 (Thirty One)
Release:	31
Codename:	ThirtyOne
$ rpm -qf /usr/lib/modules/$(uname -r)/kernel/drivers/block/nbd.ko.xz
kernel-core-5.3.16-300.fc31.x86_64
$ sudo modprobe nbd max_part=8
$ lsmod | grep nbd
nbd                    49152  0
@MuralidharB

This comment has been minimized.

Copy link

@MuralidharB MuralidharB commented Jan 7, 2020

I stand corrected then. I only tested with CentOS and Red Hat and I assumed Fedora falls into the same bucket. If nbd driver is available, then it is a better option than guest fish based mount.

@N0NB

This comment has been minimized.

Copy link

@N0NB N0NB commented Feb 26, 2020

Works perfectly on Debian 10.

@faywong

This comment has been minimized.

Copy link

@faywong faywong commented Mar 10, 2020

It doesn't work in my situation:

NTFS signature is missing.
Failed to mount '/dev/nbd0p2': invalid arguments
The device '/dev/nbd0p2' doesn't seem to have a valid NTFS.
Maybe the wrong device is used? Or the whole disk instead of a
partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?
@faywong

This comment has been minimized.

Copy link

@faywong faywong commented Mar 10, 2020

and filesystem type:

sudo file -s /dev/nbd0p2
/dev/nbd0p2: data
@faywong

This comment has been minimized.

Copy link

@faywong faywong commented Mar 10, 2020

lsmod |grep -i nbd
nbd                    45056  2
@fluttr

This comment has been minimized.

Copy link

@fluttr fluttr commented Jul 20, 2020

Thanks for the tip! There is another simple way to do this though:
guestmount -a path_to_image.qcow2 -i --ro /mount_point # safe, read only
guestmount -a path_to_image.qcow2 -i /mount_point # use only on not running vm image
guestmount utility can be found in libguestfs-tools package (on Debian and RHEL).

@willzhang

This comment has been minimized.

Copy link

@willzhang willzhang commented Jul 31, 2020

kernel must be 4.10.xx?

[root@rave-pony-1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

[root@rave-pony-1 ~]# uname -sr
Linux 4.10.4-1.el7.elrepo.x86_64

[root@rave-pony-1 ~]# modprobe nbd max_part=8
[root@rave-pony-1 ~]# 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

3.10.x kernel

[root@virt-tool ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

[root@virt-tool ~]# uname -sr
Linux 3.10.0-1127.el7.x86_64

[root@virt-tool ~]#  modprobe nbd max_part=16
modprobe: FATAL: Module nbd not found.
[root@virt-tool ~]#  ls /dev/nbd*
ls: cannot access /dev/nbd*: No such file or directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.