shrink size of ova file in virtualbox

See the article How to shrink a dynamically-expanding guest virtualbox image.

It describes how to shrink the disk via :

  1. Zero out the free space, using zerofree for Ubuntu
  2. Shrink the VM using
    VBoxManage modifyhd my.vdi –compact

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.