Shrinking VirtualBox Linux images with ZeroFree

VirtualBox sparse VDI images always take up as much space as they have maximally had utilized at some point in time. To fix this, we need to fill the empty space on the vdi with zeros and resize it:

Download the SystemRescueCd Live Image.

Mount it as a CD on your virtual machine and boot into the live distro.

Open up a Root Terminal.

Find all available partition and LVM (volume groups) with:

fdisk -l 

After we find our partition run zerofree to zero out the free space. In our example we will run it on the LVM root partition.

zerofree -v /dev/mapper/vg_rheldev-lv_root

If you want to try a dry run without modifying the hard-drive, add the -n flag.
For LVM, there might also be a _home partition that needs a zerofree run as well.

Finally, boot down your virtual machine, and run VBoxManage on the host to resize the .vdi file.

VBoxManage.exe modifyhd --compact "C:\Users\k\VMs\CentOS Dev.vdi"

(Example is on a Windows host. Linux command should look the same, just omit .exe and add in the correct path.)



11 thoughts on “Shrinking VirtualBox Linux images with ZeroFree

  1. David Roheim

    Maybe a better / easier way is to take the VM down to runlevel 1, remount the partitions as read-only and then run zerofree && halt. At least that is what I do. :)

    1. Stanislav Khromov Post author

      That does sound easier! Can you write down the commands required to perform this?

      1. David Roheim

        Sure. Quite easy and short. For me it is most of the time 6 lines or so.

        runlevel 1
        mount -n -o remount,ro -t ext4 /dev/mapper/lv_home
        mount -n -o remount,ro -t ext4 /dev/mapper/lv_root
        zerofree -v /dev/mapper/lv_home
        zerofree -v /dev/mapper/lv_root

        And then this:

        VBoxManage modifyhd my-large-file.vdi โ€“compact

        You can find zerofree for CentOS 6 here in my repo:

  2. tony

    you said:
    “Mount it as a CD on your virtual machine.
    Open up a Root Terminal in Partition Magic.”
    how do you get from mount cd to partition magic? I couldn’t see any folder called partition magic in the mounted cd.
    btw I’m running a centos6 server which is command line only….

    1. Stanislav Khromov Post author

      Sorry for the late reply Tony, when you reboot your machine, let live CD boot instead of your CentOS system, you’ll get a new shell inside the live CD where you can run the commands. There is also a way of doing it that does not need another live distro, check out David Roheims comment above.

    1. Stanislav Khromov Post author

      Thanks for the info Nick,

      Quite annoying. I was earlier recommending PartedMagic, that went on to be a paid live CD, now GParted no longer has zerofree, grr.

      I checked out SystemRescueCd real quick and it seems to have all the utilities, so I have changed the link in the post. Let me know if that works out for you!

      You can also try the way David Roheim suggested which does not include third party distros.

  3. Peter Ermke

    I found a much simpler solution:
    Use: dd if=/dev/zero of=nullfile
    rm nullfile
    Shutdown the VM
    VBoxManage modifyhd –compact “filename.vdi”

    1. Mark

      The ‘dd’ approach does work, but fully realizes the dynamic disk before compaction. Your physical drive may not have enough free space left for that. If the physical drive is an SSD this growth also adds unnecessary wear and tear. The appeal of ‘zerofree’ is that it does not grow your virtual disk, and operates only on blocks already used.

      1. Peter Ermke

        I just tried it again: No, the virtual disk does NOT grow with the ‘dd’ command.


Leave a Reply

Your email address will not be published. Required fields are marked *

Markdown is allowed in comments.