Page 1 of 2

Seriously, how to compact a vdi?

Posted: 29. Oct 2007, 14:27
by nvivo
I have been delaying this question for some time thinking I was doing something wrong... I have tried many scenarios and I can't compact a VDI.

I use Ubuntu Gutsy as host, VirtualBox 1.5.2. I have XP/NTFS as guest, and the machine/disk never had a snapshot.

From windows explorer, the disk has 4.6Gb occupied. I run defrag, then JkDefrag to compact everything on the beginning of the disk. The file had 7Gb in size then.

Then I run SDELETE -Z, and the file went to ~15Gb.

The drive map from windows defrag and from jkdefrag shows a huge empty area on the end of the disk.

Tried to run VBoxManage modifyvdi <file> compact and there was absolutely no change on the file size. Not even one byte.

I have been seeing many posts and bug tickets open about this, but never saw an answer.

Is this really a bug? Am I doing something wrong?

Thanks in advance.

Posted: 29. Oct 2007, 16:28
by Ingo
You are doing something wrong.
From the User Manual:
8.13 VBoxManage modifyvdi
The modifyvdi command can be used to compact disk images, i.e. remove blocks
that only contains zeroes. For this operation to be effective, it is required to zero out
free space in the guest system using a suitable software tool.
I use this nice tiny program for "nullifying" my paritions within the virtual machine.
http://www.feyrer.de/g4u/nullfile-1.02.pas
http://www.feyrer.de/g4u/nullfile-1.02.exe

Re: Seriously, how to compact a vdi?

Posted: 29. Oct 2007, 16:40
by Ingo
Oh sorry, I've just seen:
nvivo wrote:Then I run SDELETE -Z, and the file went to ~15Gb.
Does it zero out with null or with random values?

Re: Seriously, how to compact a vdi?

Posted: 30. Oct 2007, 03:36
by nvivo
Ingo wrote:Oh sorry, I've just seen:
nvivo wrote:Then I run SDELETE -Z, and the file went to ~15Gb.
Does it zero out with null or with random values?
From what I have read, it writes zero, and it is the recomendation from the people here.

And it seems I'm not the only one... There are open tickets with no response about if this is going to be fixed or not.

http://www.virtualbox.org/ticket/28
http://www.virtualbox.org/ticket/709
http://forums.virtualbox.org/viewtopic.php?t=2218
http://forums.virtualbox.org/viewtopic.php?t=2246

I have read the manual and I know what it says.

But have anyone actually used it in real life?

Posted: 30. Oct 2007, 09:13
by Ingo
I've just tested the nice tiny program nullfile-1.02.exe and my VDI was shrunken from 3.2GB to 1.6GB.
It works. Give it a try.

Posted: 31. Oct 2007, 14:48
by nvivo
Ingo wrote:I've just tested the nice tiny program nullfile-1.02.exe and my VDI was shrunken from 3.2GB to 1.6GB.
It works. Give it a try.
Well, that finally works! Thanks.
Just recovered 35Gb of space =)

Have anyone tried to compile this on linux guests? From this ticket ( http://www.virtualbox.org/ticket/28 ), dd doesn't seem to work...

Also, this info should be on the manual.

Or better, there should be a tool included in the guest additions that does this. I believe that VMWare "Shrink" option that can shrink the disk while running the VM does something similiar, right?

Posted: 5. Dec 2007, 06:48
by Hancoque
You have to use "sdelete -c" and not "sdelete -z" to prepare a disk for compaction.

Posted: 10. Dec 2007, 22:52
by FrankVdb
Have you tried to restore the VDI afterwards?

Posted: 16. Dec 2007, 19:37
by the_camel
nvivo wrote: Have anyone tried to compile this on linux guests? From this ticket ( http://www.virtualbox.org/ticket/28 ), dd doesn't seem to work...
Try zerofree. See also here: Tip: (Better) Shrinking of Ext(2/3) partitions

Re: Seriously, how to compact a vdi?

Posted: 21. Jun 2010, 19:31
by birdie
dd works both on linux and Windows hosts.

Code: Select all

dd if=/dev/zero of=EMPTY bs=1M
rm/del EMPTY
and after you've blanked your virtual HDD, stop VirtualBox and run this command to compact VDI storage file:

Code: Select all

VBoxManage modifyhd Disk.vdi --compact
dd for windows can be downloaded here: http://www.chrysocome.net/dd

Re: Seriously, how to compact a vdi?

Posted: 21. Jun 2010, 20:22
by stefan.becker
CloneVDI works faster as every other solution.

Re: Seriously, how to compact a vdi?

Posted: 21. Jun 2010, 20:27
by Perryg
You can run CloneVDI in Linux but it requires that you install Wine.

Re: Seriously, how to compact a vdi?

Posted: 23. Jan 2011, 21:31
by kernelOfTruth
so to wrap it up

inside of your virtual machine

1) pick the defragment tool of your choice and configure it to pack data as close together as possible - leaving large areas of free space

(I used http://www.oo-software.com/free/en O&O Defrag Free Edition - a very potent free defrag utility 8) )

2) zero the free space with http://technet.microsoft.com/en-us/sysi ... s/bb897443 sdelete

sdelete -c C:\ (on C:\)

or whatever other drive



outside of the virtual machine - on your host

3) VBoxManage modifyhd Disk.vdi --compact


disk worked like a charm and gained me 9 GiB worth of additional harddrive space on my /home partition - and that was only one of those harddisks :D

Thanks ! :)

Re: Seriously, how to compact a vdi?

Posted: 25. Jan 2011, 10:46
by perkas
Thank you for your help!! :wink:

Re: Seriously, how to compact a vdi?

Posted: 14. Feb 2011, 12:48
by gammatwo
I would suggest to you to not use sdelete, and to use instead a NullFile .NET program that I've writed, based on pascal NullFile version of Matthias Jordan.
It's really faster and contain full source code.
Here http://www.gammatwo.com/2011/01/16/null ... oing-disk/ you can find all.

Bye

P.S.: For defragging I use UltraDefrag , it's open source.