Skip to content

Instantly share code, notes, and snippets.

@anishcorratech
Created May 12, 2021 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anishcorratech/868a8aca44fd5fb83cbee53099d796c6 to your computer and use it in GitHub Desktop.
Save anishcorratech/868a8aca44fd5fb83cbee53099d796c6 to your computer and use it in GitHub Desktop.
How to mount a QCOW2 disk image in Linux

How to mount a QCOW2 disk image in Linux

QCOW2_Disk_Image

In Linux KVM we use sparse file format called qcow and qcow2 for disk images. Sometimes we need to mount a qcow2 disk images of a guest devices on to your host server. We need to do this to salvage a file, reset password of root user, or troubleshoot a faulty disk image.

We can use this dcumentation to do mount a Linux KVM guest qcow2 disk.

1. Dynamically load the NDB driver to Linux kernel

modprobe nbd max_part=8

2. Attach the QCOW2 file as network block device

qemu-nbd --connect=/dev/nbd0 /var/lib/libvirt/images/cda_vm01/cda_vm01_deb11_amd64_hd0.qcow2

3. Probing the partitions inside the newly added Block device using fdisk

fdisk /dev/nbd0 -l

4. Create a empty directory for mounting the required volumes from new Block device

mkdir -vp /mnt/virtual/cda_vm01/

5. Mounting the partition from the newly added Block device

mount /dev/nbd0p1 /mnt/virtual/cda_vm01/

6. Unmounting partition and disconnecting the block device

umount /mnt/virtual/cda_vm01/
qemu-nbd --disconnect /dev/nbd0
rmmod nbd

Done!!!

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