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)