How to compact VirtualBox VDI (Virtual Disk Image) File.

Posted: August 5, 2011 in Software

 

As you may aware, VirtualBox has 2 types of VDI file; static and dynamic. While static VDI is good to minimize potential VDI file fragmentation, it requires a lot of unnecessary disk space. Assuming you allocate 55GB of disk space for VDI (logical hard disk space in VirtualBox machine), static type will need the whole 55 GB for the VDI although your host OS just consume 4 GB (typical Windows XP installation). Thus, using Dynamic type is more practical and sensible (allocate as much as your guest OS uses, generally).

However, dynamic VDI is not 100% efficient. Due to various factors such as empty space, overhead and block reading method, VDI files seem to use significantly more space. For example, if your guest OS consume 20 GB, there is big chance that your VDI file requires up to 25 GB of actual hard disk space.

To solve this problem, we basically need to make the VDI file more compact and efficient thru

  1. Defragmentation (Consolidate free space) @ guest OS
  2. Nullify free space (convert free space to become null value) @ guest OS
  3. Ask VirtualBox to perform compact command @ host OS.

 

Let get started.

This is my WinXP SP3 + Visual Studio SP1 + SQL Server 2008 R2 virtual machine. The size of VDI files (main + 1 snapshot) is 43.8 GB. Let see how much I could save by compacting this VDI file.

 

Step 1:

In guest OS (Windows XP), defrag it thoroughly. I use MyDefrag (was known as JKDefrag previously). The idea is to have the biggest possible free space area. Choose consolidate free space option. Note: you can use built in windows defragmenter as well.

 

Step 2:

Nullify (Zero) the free space using sdelete utility.

Download sdelete at :  http://technet.microsoft.com/en-us/sysinternals/bb897443

Copy sdelete.exe and paste it at your C drive.

At guest OS (Windows XP), open command prompt (Run -> Cmd).

Type sdelete –c C:\

Click Agree button.

Nullifying is in progress…

Once completed, shut down Guest OS (WinXP).

 

Step 3:

At host OS (Windows 7), type

VBoxManage modifyhd YourDisk.vdi –compact

Errr, clueless? Here is my example…

  • My VirtualBox was installed at -> C:\Program Files\Oracles\VirtualBox
  • My VDI image’s name: XP .NET.vdi.vdi (yeah, I accidently put .vdi as part of its name, resulting double .vdi there)
  • and it is located at G:\VirtualBox HDD\XP (.NET)

Therefore, the correct syntax for my case is:

C:\Program Files\Oracle\VirtualBox\vboxmanage modifyhd “G:\VirtualBox HDD\XP (.NET)\XP .NET.vdi.vdi” – compact

Result:

From 43.8 GB become 21.1 GB. Wow. That is huge saving don’t you think.

————————————————–

Alternatively, you may use this handy tool (CloneVDI) to achieve same result as above method + you can clone your VDI as well.

Comments
  1. Frank Mayer says:

    Compact and easy to digest! I read the online manual on virtualbox first, then stumbled upon your post. Nice

  2. Mike says:

    I believe your sdelete parameter is incorrect. You said to use “sdelete -c”, which cleans free space to securely delete files. This means that the free space will be overwritten, but not necessarily zeroed. You should be using “sdelete -z” in this case.

    • expertester says:

      I stand corrected, Thanks Mike.

      • Tim says:

        If you stand corrected, why don’t you correct the document?

      • grrrr says:

        THEN WHY IS IT STILL TELLING PEOPLE TO DO THE WRONG THING?? -c increase the space taken up by the drive, not decreases

      • Aaron says:

        Please correct the blog entry to reference sdelete -z. sdelete -c will fill up a dynamic disk to maximum capacity and can cause hardship. This blog is ranked high on google search so chances are many people will be negatively impacted if it is not corrected.

      • Aaron says:

        Check the help of the sdelete command, some old versions use -c to zero out the disk while newer versions use -z

  3. Luke Stanley says:

    Nice guide!

    One tweak though, the correct syntax for step 3 would probably be:

    cd “C:\Program Files\Oracle\VirtualBox\”

    vboxmanage modifyhd “G:\VirtualBox HDD\XP (.NET)\XP .NET.vdi.vdi” – compact

    (Putting the directory in the same line may confuse some.)

  4. Luke Stanley says:

    Another thing: WordPress can add those terrible fancy quote marks that can mess up pasting commands! 🙂

  5. Stef says:

    You should write every tutorial on the internet! 🙂

  6. Alex says:

    seriously, fix the fucking “-c” comment, you self-proclaimed “expert”.

  7. dsaf says:

    The steps are
    1) perform following in guest OS
    sdelete -z -c
    2) perform following in host machine
    vboxmanage modifyhd “C:\VMs\Mydisk.vdi” –compact

    This worked for me 🙂

  8. Hi, does the compact work when I have a 320 GB image (created from a physical HDD) but in this image I have 220 GB unallocated space?

  9. If the guest OS is Windows, you could also run Disk Cleanup, CCleaner or another file cleaning utility before the defrag to free up even more space.

  10. Jan says:

    Very helpful and to the point info. Took me ten minutes to get nullifying going in my virtualization with this howto. Thanks!

  11. Rastakrakken says:

    As the saying goes, “if it ain’t broken, don’t try to fix it” ! I tried this method on a dynamic, 1.7gb xp sp3 volume, and ended up with à 7.1gb volume !!! Duh…

  12. Angel says:

    This tutorial is very good and well intended, but you MUST fix the parameter of the sdelete command line (or at least mention that you should check the parameter depending on the version of sdelete that you are using). Otherwise, instead of decreasing the image size, you end up bloating it.

    This has been mentioned by other posters, there’s no reason for this not to be fixed.

  13. in linux the command to compact is:
    VBoxManage modifyhd –compact yourdisc.vdi
    (first of all, move to the dir of your .vdi an open a terminal)

  14. Never says:

    Great Link… Spyware, Malware, Crap, Maybe Viruses, who knows…
    And the best is the commandline in task manager while setting up that allmyshitapp…

    ~Temp\Setup.exe /S /token:whatever (ama:install:I’mOmnipotentI’mOmnipresentI’mRoot)

    ^^ In brackets unchanged.
    I don’t get it why you people fool so many in postig those links and ruin their computers. Thats something i would NEVER install on a true machine.

    PS: LINK without shitfuck: https://forums.virtualbox.org/viewtopic.php?f=6&t=22422#p98235

  15. Thanks for the note .Very well written and very help full..I would suggest you to have to look here. Here some good techniques are mentioned to recover data from corrupt files.These mentioned techniques are free and you should try it for once..

Leave a comment