DEV Community

ivnilv
ivnilv

Posted on

Reclaim free space from VirtualBox VM on Windows host

When you delete files in your virtualbox VM in order to free up space on the host filesystem, this space is not automatically reclaimed.
In order for the host system to see the changes you need to rewrite the free space with zeroes.
Follow the below steps to perform this operation:

  • Install zerofree package. It is needed to rewrite the free space with zeroes.
  • Mount the filesystem as "readonly". This is needed for the tool to be able to perform it's task. If you're working with the "/", easiest way to mount it as readonly is to edit the kernel parameters.
    • Edit /etc/default/grub.
    • Find the GRUB_CMDLINE_LINUX_DEFAULT line.
    • Add init=/bin/bash to it
    • reboot
  • Run zerofree -v /dev/sdX. This could run for some time, depending on the size of your disk.
  • After it's done, run exec init to finish booting up.
  • Shutdown the VM in order to be able to run the next command which requires a lock on the VDI volume.
  • On the Windows host run VBoxManage.exe modifymedium "path\to\disk.vdi" --compact

Top comments (0)