I've got an Ubuntu 64bit VM that I derived from a ~1GB ova file, I used it under VMware vSphere to install some more software on it and use the system against a few datasets that took some 2-3GB of space, but now that I deleted these big files, I was expecting that the newly generated ova file from it would also take about ~1.1GB of space, but instead is taking about 3GB. If I look at the details the .vmdk file in virtualbox, I see the following:
Hart Disk: SATA Port 0 Type (Format): Normal (VMDK) Virtual Size: 8.00 GB Actual Size: 7.90 GB Details: Dynamically allocated storage Location: /somewhere/myVM-disk1.vmdk Attached To: myVM.virtualbox
The size of the ova when I export this machine is ~3GB.
Any ideas how I can shrink it?
A space in .vmdk is allocated when OS makes a first write into particular sector. Once written, it's allocated forever.
So, you need to add second dynamically-sized virtual HDD to your virtual machine, then boot VM from LiveCD, create partitions with fdisk and copy all of the files (cp -R). Then shutdown VM, swap virtual HDDs, boot again and verify everything works, then detach original virtual HDD from VM and delete it as a file on disk of the host system.
Direct copying with dd in VM won't work, because it will write each sector into target .vmdk file, so it will expand to its full size instead of shrinking.
See the article How to shrink a dynamically-expanding guest virtualbox image.
It describes how to shrink the disk via :
- Zero out the free space, using zerofree for Ubuntu
- Shrink the VM using
VBoxManage modifyhd my.vdi –compact