Skip to content

Instantly share code, notes, and snippets.

@kuznero
Last active July 25, 2024 06:26
Show Gist options
  • Save kuznero/576e848c39080745ac1915c6b3e4820b to your computer and use it in GitHub Desktop.
Save kuznero/576e848c39080745ac1915c6b3e4820b to your computer and use it in GitHub Desktop.
How to compact VirtualBox's VDI file size?

Source: StackOverflow

1. Run defrag in the guest (Windows only)

2. Nullify free space:

With a Linux Guest run this:

sudo dd if=/dev/zero | pv | sudo dd of=/bigemptyfile bs=4096k
sudo rm -rf /bigemptyfile

or

telinit 1
mount -o remount,ro /dev/sda1
zerofree -v /dev/sda1

With a Windows Guest, download SDelete from SysInternals and run this:

sdelete.exe c: -z

(replace C: with the drive letter of the VDI)

3. Shutdown the guest VM

4. Now run VBoxManage's modifyhd command with the --compact option:

With a Linux Host run this:

vboxmanage modifyhd /path/to/thedisk.vdi --compact

With a Windows Host run this:

VBoxManage.exe modifyhd c:\path\to\thedisk.vdi --compact

With a Mac Host run this:

VBoxManage modifyhd /path/to/thedisk.vdi --compact
@plauCkp3
Copy link

my sdelete need this command instead:
sdelete.exe c:\ -z

@spaceduck8
Copy link

sudo dd if=/dev/zero | pv | sudo dd of=/bigemptyfile bs=4096k
sudo rm -rf /bigemptyfile

Thanks, this was the only one that worked.

@hajosattila
Copy link

hajosattila commented May 10, 2023

Very useful. Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment