Wednesday, 29 September 2010

Shrinking vmware vmdk disk images

Firstly, you should delete all unnecessary files from within the guest. This includes stuff in /tmp and any cached files, including the apt-get cache. When you have cleared out everything

Deleting files is not enough however because when you a delete file it only deletes the directory entry for the file, all the "1's and 0's" stay as they were before and therefore cannot be efficiently shrunk.

Run this command to fill all empty space on each drive with zero's.

cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill

Note: if you have more than one virtual disk you will need to run this command from within any directory on the mounted disk.

Shut down the guest and log on to the host server.

Change directory (cd) to where the virtual machines vmdk files are located.

Use the vmware-vdiskmanager command to shrink the disk(s).

vmware-vdiskmanager -k sda.vmdk

When this completes, check the size of your vmdk files and they should now be considerably smaller!

No comments: