How can I reduce the size of a Virtual Box VM?

I have an Ubuntu Server installed in Virtualbox. Its VDI file's size is 4.2G and it has a Dynamically allocated drive. When I export it, the file size of the OVA file is 1.4G. I want to reduce the size of the OVA file somehow. I uninstalled all the unwanted programs and deleted unwanted files and then compacted the disk already. I also tried to compress the OVA file with 7zip, but that only decreases the size by 100MB.

The reason for doing so is because I want to share the VM with a friend of mine. What I usually do is once I am done working with the VM, I export it (File->Export Appliance) and then send the exported OVA file. Since working with a big size is not that convenient, I want to make it as small as possible. My final goal is to make the OVA file smaller than 800MB after I export it.


I am unclear what you have done to compact the disk. Did you create a large file on the disk with 0's and then delete that? If not, doing that will help significantly.

It occurs to me that you likely also have swap. I expect that zeroing out the contents of the swap partition will make a substantial difference. I have not tested this, but I expect you can do this as follows.

As root:

  1. Identify your swap partition (usually this is listed in /etc/fstab).
  2. Unmount swap, with "swapoff -a"
  3. Write zero's to it - for example, if pv is installed "pv < /dev/zero > /dev/swap-drive or if you need to use dd "dd if=/dev/zero of=/dev/swap-drive bs=40960"
  4. Reformat swap drive "mkswap /dev/swap-drive"

If /boot is a separate partition you may need to "compact it" as well. Likewise if you are using LVM and have resized volumes in the past you may want to create a volume with all the free space, zero it out and then delete it.