Shrink a dynamically growing disk from VirtualBox

I’m preparing Ubuntu image for the course. After installation of required software and datasets, the system image grew a lot. I have decided to shrink its size by disabling swap and removing unused data. But the system image didn’t shrink automatically after removal of the files. You need to do it manually:

  1. Run your system from LiveCD
  2. Install zerofree and free unused space
  3. sudo apt-get install zerofree
    # 10min
    sudo zerofree -v /dev/sda1
  4. In the host system, release and remove the image from VirtualBox (but keep it in the filesystem!)
  5. shrink1


  6. And compact image size
  7. VBoxManage modifyhd Ubuntu1404/Ubuntu1404.vdi --compact</code>
  8. Finally, you will have to add .vdi image back to VirtualBox machine.
  9. shrink3

In my case, the .vdi image shrunk from 7.2G to 6.2G (exactly the released size under Ubuntu), so I think it’s worth the effort.

Inspired by AskUbuntu.