LINUXMAKER, OpenSource, Tutorials

KVM - mount qcow2 image using qemu and recover data

With the KVM virtualization technology, there are different disk formats to choose from when creating them – one of these formats is qcow2. After deleting a KVM machine or if it can no longer be started, the data on the virtual hard disk is required again. If you haven't erased the disc or have a backup of this image, then using the KVM tool qemu-nbd is very easy to get the files back and save them for a new installation.

The image is mounted as follows:

modprobe nbd max_part=8
qemu-nbd --connect=/dev/nbd0 /var/lib/libvirt/images/OpenLDAP.qcow2
mount /dev/nbd0p1 /mnt/

From now on you can easily access the data under /mnt/.

The following commands are required to separate the image:

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

In this way, defective VMs, for example, can be restored very quickly with the previous config files and production files. And you lose less time with a complete reconfiguration of all services.