Resize qcow2 - Reduce size of the disk

starnetwork

Renowned Member
Dec 8, 2009
430
10
83
Hi,
it's possible to reduce the size of the KVM qcow2 virtio partition without windows clone backup and restore?
any other quickly solution?

Regards,
 
The problem I always seem to run into, is that the guest OS needs to be reduced in size first. You need the right utility to achieve this. Generally you can't just steal GBs from a guest OS without using a shrinking tool on it first like gpartd to get everything out of the reclaimed space, and have the right partition info for the smaller volume


If it were me I would convert the qcow2 to raw, shrink the guest OS, make another smaller raw drive to copy the shrunken guest OS to using dd .

There might be another easier way but I don't know of any.
 
Last edited:
Hi,
I did the resize with the gpartd, and now I have 100GB Disk and another 250GB that are unlocated, now what I can do so the qcow2 size will be only 100GB instead of 350?
 
Thanks, I already did
qemu-img convert -O qcow2 image.qcow2_backup image.qcow2
no changes for disk size, still 350G

 
Thanks, I already did
qemu-img convert -O qcow2 image.qcow2_backup image.qcow2
no changes for disk size, still 350G


Try qemu-img convert -O raw image.qcow2_backup image.raw and see what size it comes out as. Then convert it back to qcow2

Another trick is to zero out the free space in the guest OS by creating a big empty file.

eg. if it's a linux guest, and /home has 50GB free then do something like:

dd if=/dev/zero of=50GB bs=1G count=50

That will make a 50GB file full of zeros which you delete when it's finished and qcow2 should reclaim that 50GB when you convert from .raw to qcow2 again as per my example above.
 
So in order of events.

Use dd or a windows equiv in the guest OS to zero out the free space, and delete the big file it created as per my previous post.

Stop the VM and convert the disk image to .raw
convert the .raw back to .qcow2.


Make sure there are no snapshots before you do all that, they add to the .qcow2 file size.
 
Last edited:
Thanks, now the size is 84GB, but the Hard drive (virtio0) is still 350GB, who can I change it to 100GB?

Regards,
 
This parameter is only informative, you can safely ignore it ( or change it manually in /etc/pve/qemu-server/_your_vm_id.conf )
 
Hi,
I did and it's changed to 100GB
but the Storage -> Content -> Disk Image show still 350G
any reason why?

Regards,
 
Try to reboot the VM after editing this parameter, to trigger a reload of the configuration file in the GUI.
 
well,
am Uploading here 4 Screenshots:
Proxmox-Disk-1.pngProxmox-Disk-2.pngProxmox-Disk-3.pngProxmox-Disk-4.png
Proxmox 1 - from GUI -> VM HW Rseources View - 100GB
Proxmox 2 - from GUI -> Local storage -> Content View - 350GB
Proxmox 3 - from the VM, Win 2k8R2 -> Computer manager Local storage view - 100GB + Unlocated 250GB
Proxmox 4 - from node -> VM -> Qcow2 file size - 84GB

Regards,
 
Hi Starnet

I'd like to remember one thing: since qcow2 is a sparse disk format. event if the VM sees 350 GB, in the reality it is only using 84GB, so what you see is correct.

You can check the difference with:
# what the VM thinks it has
du -sh --apparent-size /var/lib/vz/images/101/vm-101-disk-2.qcow2
17G

# what is really using
du -sh /var/lib/vz/images/101/vm-101-disk-2.qcow2
1.8G

I guest in your case you will get 350GB, and 100GB.

If you absolutely want to reduce the apparent size, you have the force the VM to release the last blocks of the disk image, there is a detailled procedure here:
http://www.jamescoyle.net/how-to/323-reclaim-disk-space-from-a-sparse-image-file-qcow2-vmdk