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
add comment

2 Answers

up vote 48 down vote accepted

You have to do the following steps:

  1. run defrag in the guest (Windows)
  2. nullify free space:

    With Linux guest run this:

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

    With Windows guest, download SysinternalsSuite and run this:

    sdelete –z
    
  3. shutdown the guest VM

  4. now run VBoxManage's compact command

    VBoxManage.exe modifyhd thedisk.vdi --compact
    

This reduces the 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
1  
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
    
@Dakatine I never got it working. Add a working command to the post. –  magicandre1981 Feb 20 at 19:35
1  
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
show 6 more comments

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
add comment

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.