Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've a VirtualBox VM which configured a very large hard disk size (bigger than host). By my mistake, a program on the VM generated lots of log files and the VDI file size keeps growing until there is no space on the host.

Now I've deleted the log files but the VDI file size are not getting smaller after using VBoxManage.exe modifyhd "C:\Virts\mybox-i386.vdi" compact

Is there a way to really compact the VDI file size? Thanks!

share|improve this question

2 Answers 2

up vote 86 down vote accepted

You have to do the following steps:

  1. Run defrag in the guest (Windows only)
  2. Nullify free space:

    With a Linux Guest run this:

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

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

    sdelete –z
    
  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
    

This reduces the vdi size.

share|improve this answer
    
@user248749 FYI, dd with no argument copies one block at a time, not one byte. Using a larger block size can speed things up, but the effect is pretty minor. –  Gilles Aug 27 '13 at 0:15
3  
For the next person, my command ended up looking like this: "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd "C:\Users\daniel\VirtualBox VMs\....\thedisk.vdi" --compact –  Daniel Dec 13 '13 at 17:57
4  
According to the zerofree Linux utility manpage (manpages.ubuntu.com/manpages/natty/man8/zerofree.8.html), zerofree should be better than dd for this job. dd would not been recommended because "it is slow", "it makes the disk image (temporarily) grow to its maximal extent", "it (temporarily) uses all free space on the disk, so other concurrent write actions may fail". Zerofree is available on Ubuntu Linux via apt, or you can compile it yourself. –  Dakatine Feb 20 at 13:16
3  
It's fun that the the manpage of zerofree states that with dd other concurrent writes will fail, but zerofree needs the filesystem to be mounted read-only! *duh* –  Madarco Feb 25 at 12:04
3  
@Dakatine Using VirtualBox 4.3.10, the disk image file did not grow to its maximal extent. VirtualBox is clever enough to not bother writing all zero blocks to the physical disk. –  jlh May 14 at 17:50

If running the command in the accepted answer produces an unhelpful error message like this

VBoxManage.exe: error: Cannot register the hard disk 'thedisk.vdi'
{aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeee} because a hard disk 'thedisk.vdi'
with UUID {aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeee} already exists

Simply run the command by the UUID instead of the filename:

VBoxManage.exe modifyhd {aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeee} --compact
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.