Skip to content

Instantly share code, notes, and snippets.

@shamil
Last active November 14, 2024 09:14
Show Gist options
  • Save shamil/62935d9b456a6f9877b5 to your computer and use it in GitHub Desktop.
Save shamil/62935d9b456a6f9877b5 to your computer and use it in GitHub Desktop.
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
@SwetaleenaDash
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

N0NB commented Feb 26, 2020

Works perfectly on Debian 10.

@faywong
Copy link

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
Copy link

faywong commented Mar 10, 2020

and filesystem type:

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

@faywong
Copy link

faywong commented Mar 10, 2020

lsmod |grep -i nbd
nbd                    45056  2

@fluttr
Copy link

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
Copy link

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

@MaxMatti
Copy link

you kill my arch Linux angryangryangryangryangryangryangryangryangryangryangryangryangryangryangry

If you don't know how to mount a filesystem then you shouldn't use archlinux. Have a look at the documentation for the mount command to learn how to fix your problem.

@0xcpu
Copy link

0xcpu commented Jan 26, 2021

I have some issues with step 4.
step 3 output:

Disk /dev/nbd0: 8 GiB, 8589934592 bytes, 16777216 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: gpt
Disk identifier: 0CBEE71E-00C3-470C-B2C4-74D20202393E

Device        Start      End  Sectors Size Type
/dev/nbd0p1    2048     4095     2048   1M BIOS boot
/dev/nbd0p2    4096  2101247  2097152   1G Linux filesystem
/dev/nbd0p3 2101248 16775167 14673920   7G Linux filesystem

Then mount -o ro /dev/nbd0p1 /mnt/fs_mnt/

wrong fs type, bad option, bad superblock on /dev/nbd0p1, missing codepage or helper program, or other error.

I guess, the issue is because it's a GPT disk.

Any idea how this can be solved? (Tried fixing the drive with fsck, but it didn't help.)

@MaxMatti
Copy link

MaxMatti commented Jan 27, 2021

@0xcpu:

See this link

As far as I know the "BIOS Boot Partition" does not contain any filesystem but only the Grub image. It is the space you need that would otherwise be put somewhere between the MBR and the first partition.

Seems like you wouldn't wan to "mount" that but rather just mount the other two partitions.

@SKGEzhil
Copy link

I successfully mounted but I can't able copy and paste files

@chrisgraf
Copy link

thanks! found that via google!

@jussihirvi
Copy link

I had the same problem as @poma. fdisk listed the partitions but there were no corresponding device files. partx did not help. Run as partx -v -a /dev/nbd0 it complained about "invalid argument" and did not create anything.

kpartx helped. First I checked with kpartx -l /dev/nbd0, then actually created the device files with kpartx -a /dev/nbd0. The device files appeared in /dev/mapper.

@AggamR
Copy link

AggamR commented Jun 30, 2021

thanks! very useful!

@cindrmon
Copy link

gives me this error, since i made my qcow2 an LVM

“mount: unknown filesystem type LVM2_member”

Is there any other way for me to mount this as an LVM?

@ololobster
Copy link

ololobster commented Dec 13, 2021

Is there any other way for me to mount this as an LVM?

Hello from December 2021 :)

  1. Run sudo pvs to get a volume group (column VG) for your device.
  2. Run sudo lvdisplay ⟨your VG⟩ to get a LV Path.
  3. Run sudo vgchange -a y to activate all groups.
  4. Now you can mount it: sudo mount ⟨your LV Path⟩ /mnt/somepoint

@bilalyaqoob
Copy link

How to change the "config.plist" file, I don't have permission to edit it.

@gabrielmoura
Copy link

Thanks

@ra7narajm
Copy link

Is there any way to see path of the qcow2 file connected to nbd device (after successful "qemu-nbd -c")?
for example, want to see file name being mounted on /dev/nbd0 from a monitoring program.

@AysadKozanoglu
Copy link

AysadKozanoglu commented Nov 28, 2023

works fine under OS

lsb_release -a
LSB Version:	core-11.1.0ubuntu4-noarch:printing-11.1.0ubuntu4-noarch:security-11.1.0ubuntu4-noarch
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy

i had .img file ending instead of .qcow2

after i checked with qemu-img info XXXX.img

i see that it is a qcow2 format

thanks a lot

@AysadKozanoglu
Copy link

AysadKozanoglu commented Nov 28, 2023

gives me this error, since i made my qcow2 an LVM

“mount: unknown filesystem type LVM2_member”

Is there any other way for me to mount this as an LVM?

you have to remove missing disk from LVM member group, you can read my howto to fix it here:
(http://aysad.cloudns.cc/blog/2023/11/28/lvm/raid1/disk/mount/recovery/howto/)

@marXtevens
Copy link

Thanks. Ckear, concise. Worked on Fedora Server 38.

@SzczurekYT
Copy link

Very useful, but I think it worth to add nbds_max=1 (or 2) to the modprobe, so it only creates one or two devices, instead of 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment