MaiDeveloper

How to reclaim free space/shrink qcow2 file?

Qcow2 file grows overtime; even if we delete files and snapshots. It still does not reduce the file size. The only way to that is to fill the disk with zero and then convert the disk to qcow2.

Guest

Do a disk free command

df -h

centos_7_df-2

We need to fill root and home with zero.

dd if=/dev/zero of=/mytempfile bs=10M; rm -f /mytempfile
dd if=/dev/zero of=/home/mytempfile bs=10M; rm -f /home/mytempfile

Shutdown

shutdown -h now

Host

qemu-img convert -O qcow2 vm_learn.qcow2 vm_learn_new.qcow2

Edit guest and use the newly created qcow2 disk.

virsh edit vm_learn


Mike Mai
Mike Mai   Brooklyn, New York
I am full-stack web developer, passionate about building world class web applications. Knowledge in designing, coding, testing, and debugging. I love to solve problems.