Re: Can i shrink the image file size?

Alberto Treviño
2009-03-23T07:31:43Z

On Saturday 21 March 2009 10:45:58 am John Wong wrote:
> I create the 30G qcow2 image file, installed winxp, winxp show me the
> harddisk used 5G, but the image file size is 12G now.
> can i shrink the image file size? how?

I wrote a simple utility in .Net 2.0 that creates a file in Windows with all
0's until the drive is full, then deletes it. This allows qemu-img to
recreate the image and reclaim any sectors that are all 0's. This is what I
do:

1. Defragment the Windows drive(s). Run the defragmenter at least 3 times
to make sure you get good results.

2. Run my tool (http://mel.byu.edu/zerofill.exe). It will automatically
detect all drive letters, create a file filled with zero's and delete it whn
it's done. Make sure the VM is not under a lot of use or the continually
decreasing amount of disk space may interfere with whatever may be running.

3. Run qemu-img and recreate the virtual drive with the same format
parameters. For example, if your image is in qcow2 format you run:

qemu-img convert -O qcow2 win_xp.img win_xp.img.new

4. Rename win_xp.img to win_xp.img.old and win_xp.img.new to win_xp.img.
Run your VM and make sure everything works. If it does, remove
win_xp.img.old and go on with life.

I would recommend you back up your drive image before your start the first
couple of times, just to be sure. It's not fun when you screw up and can't
get back into your VM. I've done that a few times while developing this
technique. :-)