Shrink VirtualBox image and exporting to OVA/OVF file format

Recently, I have done an OS security assignment on hardening a Linux desktop edition machine for hosting a web server. After completing this task, I thought to export this image in-order to use it in the future for any CTF’s.  When I just exported the image into a OVA its size came around 1.2 GB. Then I Googled and found an interesting command call dd (used to convert and copy files) which will zero out the unused space in the virtual machine. Later, after doing the below given steps, I was able to reduce the size of OVA file to 70% of its orginal. It became 830 MB in the end! Hence I thought to blog about it.

$ dd if=/dev/zero of=/bigemptyfile
$ sudo rm -rf /bigemptyfile

If the free space on the guest OS is not much left, you can use the following alternative:

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

Shutdown the Virtual machine. If you want to shrink the Virtual Disk Image (VDI), then you can run this command to reduce its size:

$ VBoxManage modifyhd --compact Ubuntu.vdi

You can also export the virtual machine into standard Open Virtualization Formats

  • If you choose an ova extension, then all the files will be combined into one single Open Virtualization Format Archive.
  • If you use an ovf extension, several files will be written separately. (I always prefer ova file)



One comment

  1. Thanks for the tip it worked like a charm. I just had to clone my vmdk file to vdi first in order to use the –compact option:
    VBoxManage clonehd –format vdi disk.vmdk disk.vdi
    (with the disk unattached from the VM)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s