Shrink qcow2 image

pcglue

Active Member
Mar 2, 2016
5
0
41
50
I wanted to increase a VM's hard drive size from 4G to 8G. I mistakenly added 4000G, thinking it was 4000M. So the VM thinks there is a 4TB drive now. I couldn't see a way to shrink the size in Proxmox GUI. I read that qemu-img from CLI can do it, but only on raw images. I have qcow2 image. I only have 500GB hard drive, so if I convert qcow2 to raw format in order to shrink it, will it try to create a 4TB raw image (for which I have no space for)? Is there another way to shrink the qcow2 size? Since qcow2 is sparse, I am OK as long as I keep the partition in my VM at the desired 8G, but still would like to correct this. Thanks for your help.
 
Thanks, what I ended up doing was creating a second 8GB hard disk, re-create the exact partition table, clone the partition from the first disk with dd and reinstalling grub onto the new disk. Then I tested I could boot from it and got rid of the first disk.
 
Could you elaborate on how you did that clone operation with dd? I have been trying to accomplish this exact feat, but through a NetCat pipe. When you ran "dd" did you set 'of=' to a real block device or a virtual one?
 
It was a real block device (real from the VM's perspective anyway).
Making sure the partitions were of equal size, I ran:
dd if=/dev/sda1 of=/dev/sdb1

/dev/sda was the original 4GB disk that I mistakenly increased to 4TB in size. /dev/sdb is the new 8GB disk.
 
Thanks, what I ended up doing was creating a second 8GB hard disk, re-create the exact partition table, clone the partition from the first disk with dd and reinstalling grub onto the new disk. Then I tested I could boot from it and got rid of the first disk.

That's good. There seems to be also a different way of doing from jamescoyle.net:

http://www.jamescoyle.net/how-to/323-reclaim-disk-space-from-a-sparse-image-file-qcow2-vmdk

His idea is similar to yours somewhat, instead he used qemu img convert to resize the disk.
 
It was a real block device (real from the VM's perspective anyway).
Making sure the partitions were of equal size, I ran:
dd if=/dev/sda1 of=/dev/sdb1

/dev/sda was the original 4GB disk that I mistakenly increased to 4TB in size. /dev/sdb is the new 8GB disk.
I used in the same situation livecd (gparted) and
dd if=/dev/vda of=/dev/vdb bs=1M conv=sync
and it cloned everything perfectrly clean, even boot records.