I Just encountered a Qcow2 Disk issue in one of my KVM Guest Machine. Its condition are worst and unresponsive. . So In this port we would see how to repair qcow2 disk images from corruptions

Now i have to repair my Qcow2 Disk, so that My Guest Machine would be back online with all those settings configured on same.

How to Mount Guest Qcow2 KVM Disk on Host Machine

Setup

I am using below Host machine for KVM.

root@jarvis (~)#lsb_release -d
Description:	Ubuntu 16.04.3 LTS

root@jarvis (~)#virsh version
Compiled against library: libvirt 1.3.1
Using library: libvirt 1.3.1
Using API: QEMU 1.3.1
Running hypervisor: QEMU 2.5.0

Guest machine is based on CentOS7, but right now not able to boot and login. I already had console which is unresponsive, so i destroyed guest with “virsh destroy domain” and later not able to start Guest again. Below status of same.

root@jarvis (~)# virsh console srv7
Connected to domain srv7
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-514.26.2.el7.x86_64 on an x86_64

srv7-master login: root
Password: 
Last login: Tue Aug 15 13:05:25 on ttyS0
[root@srv7-master ~]# ls
-bash: ls: command not found



root@jarvis (~)#virsh start srv7
error: Failed to start domain srv7
error: internal error: early end of file from monitor, possible problem: 2017-08-15T18:10:58.063913Z qemu-system-x86_64: -drive file=/data/vm_images/CentOS7/system.img,format=qcow2,if=none,id=drive-virtio-disk0: qcow2: Image is corrupt; cannot be opened read/write

root@jarvis (~)#qemu-img info /data/vm_images/CentOS7/system.img
image: /data/vm_images/CentOS7/system.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 6.8G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         srv7-before-MySQL-Posts   347M 2017-08-10 20:44:07   00:03:33.196
2         Before_MYSQL_upgrade   652M 2017-08-15 07:20:12   00:46:35.477
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: true
root@jarvis (~)#)`ls -l': ls -lh /data/vm_images/CentOS7/system.img
-rw------- 1 root root 6.8G Aug 15 23:35 /data/vm_images/CentOS7/system.img

So no i know that My Disk get corrupted, now my first step is to find level of corruption. For this i used below command, that display huge output and in last provide good description of corruption.

qemu-img check /data/vm_images/CentOS7/system.img

Now we should try to correct this corruption with below command, that provide enough output of work on corruption completed by command.

root@jarvis (~)#qemu-img check -r all /data/vm_images/CentOS7/system.img

     ......
     ......

Rebuilding refcount structure
Repairing cluster 1 refcount=1 reference=0
The following inconsistencies were found and repaired:

    0 leaked clusters
    110796 corruptions

Double checking the fixed image now...
No errors were found on the image.
75027/327680 = 22.90% allocated, 5.02% fragmented, 0.00% compressed clusters
Image end offset: 7261454336

Now check corruption again with above commands.

root@jarvis (~)#qemu-img check /data/vm_images/CentOS7/system.img
No errors were found on the image.
75027/327680 = 22.90% allocated, 5.02% fragmented, 0.00% compressed clusters
Image end offset: 7261454336

root@jarvis (~)#qemu-img info /data/vm_images/CentOS7/system.img
image: /data/vm_images/CentOS7/system.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 6.8G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         srv7-before-MySQL-Posts   347M 2017-08-10 20:44:07   00:03:33.196
2         Before_MYSQL_upgrade   652M 2017-08-15 07:20:12   00:46:35.477
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

Thanks God, Now as per “qemu-imh info” there is no corruption in image, let’s start Guest Machine once more.

root@jarvis (~)#virsh start srv7
Domain srv7 started

root@jarvis (~)#virsh console srv7
Connected to domain srv7
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-514.26.2.el7.x86_64 on an x86_64

srv7-master login: root
Password: 
Last login: Tue Aug 15 13:05:25 on ttyS0
[root@srv7-master ~]# ls
anaconda-ks.cfg