qemu-img
QEMU disk image operations. Health check, refcount repair, format conversion, and the NFS-to-local recovery pattern.
Disk Health Check
Check qcow2 image for errors — non-destructive read-only scan
sudo qemu-img check /var/lib/libvirt/images/9800-WLC-02.qcow2
Expected healthy output
No errors were found on the image.
76849/262144 = 29.32% allocated, 2.88% fragmented, 0.00% compressed clusters
Image end offset: 17180000256
Error output — refcount corruption (from NFS instability)
ERROR cluster 66221 refcount=0 reference=1
...
66 errors were found on the image.
Data may be corrupted, or further writes to the image may corrupt it.
If the VM is running, qemu-img check fails with Failed to get shared "write" lock. Shut down the VM first: sudo virsh destroy <vm-name>
|
Repair
Repair all refcount errors — fixes leaked and corrupted clusters
sudo qemu-img check -r all /var/lib/libvirt/images/9800-WLC-02.qcow2
Successful repair output
Repairing cluster 7970 refcount=1 reference=0
...
The following inconsistencies were found and repaired:
10 leaked clusters
0 corruptions
Double checking the fixed image now...
No errors were found on the image.
| Never repair images on NFS — I/O errors during repair make it worse. Copy to local storage first, then repair. |
Recovery pattern — copy from NFS, repair locally, update VM config
# 1. Stop the VM
sudo virsh destroy 9800-WLC-02
# 2. Copy from NFS to local storage
sudo cp /mnt/nas/vms/9800-WLC-02.qcow2 /var/lib/libvirt/images/9800-WLC-02.qcow2
# 3. Repair on local disk
sudo qemu-img check -r all /var/lib/libvirt/images/9800-WLC-02.qcow2
# 4. Update VM to use local copy
sudo virt-xml 9800-WLC-02 --edit --disk path=/var/lib/libvirt/images/9800-WLC-02.qcow2
# 5. Start VM
sudo virsh start 9800-WLC-02
Image Information
Show image format, size, allocation
qemu-img info /var/lib/libvirt/images/9800-WLC-02.qcow2
Image Creation
Create a new qcow2 image — thin provisioned (grows on demand)
qemu-img create -f qcow2 /var/lib/libvirt/images/newvm.qcow2 50G
Create a backing file image — linked clone for fast lab VMs
qemu-img create -f qcow2 -b /var/lib/libvirt/images/base.qcow2 -F qcow2 /var/lib/libvirt/images/linked-clone.qcow2
Image Conversion
Convert raw to qcow2
qemu-img convert -f raw -O qcow2 disk.raw disk.qcow2
Convert vmdk (VMware) to qcow2
qemu-img convert -f vmdk -O qcow2 disk.vmdk disk.qcow2