You are here

Reducing VirtualBox Dynamic VDI Size

Detailed instructions for reducing a dynamic VDI size can be found at http://forums.virtualbox.org/viewtopic.php?p=29272 (FAQ entry "How can I reduce the size of a dynamic VDI on disk?").

Reducing the size of a dynamic VDI is mainly a two stage process:

  1. Fill the filesystem free space with zeroes
    The mentioned way in the FAQ is to start the system in single-user mode, mount the root filesystem as read-only and run "zerofree" command. But trying to remount the root filesystem as read-only (from single user mode) in Debian was failing with a message "mount:/ is busy". An easy way to workaround this is to first boot using a LiveCD such as SystemRescueCD, which comes bundled with zerofree. If you boot using Ubuntu LiveCD, then you might have to install the zerofree package after booting the LiveCD. Next, you will just have to run the zerofree command for each of VDI's Linux (ext2/ext3/ext4) partitions.

    # zerofree -v /dev/sda1
    # zerofree -v /dev/sda2
    # zerofree -v /dev/sda3
    ...
    ...
    

    You will have to fill any non-Linux partitions, such as NTFS, with zeros as well (refer to the FAQ entry mentioned above).

  2. Compacting the VDI
    The VDI compact command should be run from the host system.

    # VBoxManage modifyhd /full/path/to/xxxx.vdi --compact
    

For your convenience, I have written a script which takes the absolute path to the VDI as an argument, mounts any Linux (ext2/ext3/ext4) partition it finds in the VDI, fills it with zeroes, and then finally compresses the VDI. I hope you find the script useful. Download the script, extract it (gunzip compact-vdi.sh_.gz && mv compact-vdi.sh_ compact-vdi.sh), and execute it (./compact-vdi.sh vdi-absolute-path).

AttachmentSize
Binary Data compact-vdi.sh_.gz652 bytes
Category: 

Add new comment