Shrinking Linux VMware images

I’ve been putting together a VMware image based on RedHat over the past couple of weeks and the last step was to try and reduce it in size from a massive 18Gb (28Gb if you included the snapshots). Given that 5Gb of this was free space, this should have been relatively easy to achieve. The original disk wasn’t created at a fixed size and, having removed any snapshots and set the disk to be independent-persistent the Shrink panel of VMware tools finally became active. Unfortunately, it only showed the boot partition.

The next step was to try and use vmware-vdiskmanager to shrink the image from the outside. The instructions suggest that you need to mount each volume individually first and prepare them before shrinking. Unfortunately the DiskMount utility only supports FAT and NTFS partitions. After a bit of searching around on the VMware forums I eventually found a thread with the solution. You need to zero out the unused disk space first before performing the shrink. They recommended the command dd if=/dev/zero of=/empty_file; rm empty_file although I never got as far as the remove because the disk was full! Still, it did the trick and the image is now down to 13Gb. It’s still going to take me 9 hours to upload the 8Gb zip to the US though!

5 Responses to “Shrinking Linux VMware images”

  1. David Currie says:

    Faster upload

    I eventually gave up on the 9 hour upload but, with a little help from Adrian, tracked the problem down to my use of sftp. Having switched to ftp the speed shot up five-hundred fold from 200KB/s to 10MB/s bringing the upload time down to just over 10 minutes…

  2. Ben says:

    Thanks so much for mentioning the empty_file trick. (The thread to which you referred is no longer valid.) Performing this on my FC8 VM — in conjunction with Shrink — has resulted in a smaller “clean” image than was otherwise being achieved. So far, you’re the only person I’ve found to make mention of zeroing-out unused data on Linux…

  3. Sachin says:

    The thread link is valid just remove the two extra ?? at the end.

  4. Dave says:

    Thanks Sachin. Not sure what happened there – I’ve corrected it now.

  5. nobody says:

    Thank you so much for this post! I searched for ages trying to find out why shrinking my VMWare image wasn’t making it any smaller, but zeroing out the empty space finally did the trick.

Leave a Reply