Compacting a VMDK file (.vmdk) is a good idea if you want to share or reuse it, such as with Vagrant. If you’ve tinkered with your VM a bit, deleting cached files isn’t enough. The space in the filesystem is marked as free, but isn’t erased. It still contains the data, so it doesn’t compress as well. Here, I’ll describe how to zero that free space and shrink that image.
How much it helps depends on your disk content, but for what it’s worth, my VMDK with an Ubuntu Maverick 64 install shrank from 2.0GB to 1.6GB, and gzipped they are now 679MB and 425MB, respectively. That’s a nice savings when you’re talking about shipping the image around between computers and using it as a Vagrant base box multiple times.
Zero Free Space
If you zero the free space, it can be efficiently compressed.
Note: If you can, defrag the volume. You can’t really defrag ext4 (you can, but it’s sketchy), so I’m not going into that.
You want to use the VirtualBox GUI to change the boot media for your VM to a lightweight live Linux like Finnix and boot from it.
Then install zerofree
Then zero the free space on your partition. Mine’s sda1, so it looks like
Then shut down the VM
Clone the VMDK
VirtualBox only lets you compact VDIs, but if we clone a VMDK we get a compressed copy. So find your disk image
If you don’t have any snapshots, just
Otherwise, find the snapshot you want. I just want the newest one, so I
And I get something like this at the top (newest file is on top):
11f18f3d... is the UUID. So I want to use that to clone the disk.
Attach the newly shrunk disk to the VM
Just use the VirtualBox GUI to edit the VM and attach your newly cloned disk instead of the original one. You’re done!