November Echo Romeo Delta
a blog by Will Kemp

  • Recent Posts
    • Web Directions South 2009
    • Fedora 11 on a Samsung N140
    • WordPress Plugin Dependency Checking
    • Nokia 5800 Touch Screen Smartphone
    • Moving From Drupal Back To WordPress
    • Making A WordPress Theme
    • Object Oriented PHP With CodeIgniter
    • Optimising Ajax Search Suggestions
    • Passing Variables From PHP To Javascript
    • Ajax Search Suggestions Dropdown List
    • Subversion Hell
    • Converting From WordPress To Drupal
    • Moving A Wordress Site To A New Server
    • Shrinking VirtualBox VDI Files
    • Dynamic XML Sitemap Using PHP
    • Older Posts
  • Other
    • About Me
    • Contact
  • Misc
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

Shrinking VirtualBox VDI Files

December 20, 2008 – 2:24 pm

If you’re like me and have several VirtualBox Windows virtual machines, you’ll probably find they gradually take up more and more space as time goes on. This is at least partly because the filesystem gets fragmented and old deleted files still take up space in the VirtualBox VDI file. If you’ve created the VDI as a dynamically expanding image, it will only take up as much space in your filesystem as the files inside it take up – but when those files are deleted it won’t shrink again.

A VirtualBox VDI can be made to shrink, however, but first you need to do something about what’s inside it. A useful application called ‘VBoxManage’ can shrink those virtual disk images – but only if the empty space is zeroed first. I run VirtualBox on a Linux system and i use the method i’ve described here – but i can’t guarantee that all of what i’ve written applies to other operating systems.

The first step in reducing the amount of hard disk space a VDI takes up is to delete as much junk from inside it as you can. You can start by deleting the hidden folder of backup files which is created when you install a service pack. This is called \Windows\$NTServicePackUninstall. If you do this, you won’t be able to roll back that service pack upgrade. Personally, i’m not bothered by this because i keep a backup copy of the VDI which i can use to roll back and start again, in the unlikely event that i need to.

Depending on how the installation was done, there may also be a folder C:\Windows\SoftwareDistribution\Download. You can delete that too.

The service pack installation creates a temporary folder as well. This doesn’t always get properly removed. It will be on the drive you downloaded the setup files to, which is probably C:, and it’ll have a name made up of random characters – e.g., C:\be35a76f21ed. If it’s still there, you can delete it.

Don’t delete C:\Windows\ServicePackFiles though. This will be used in future. However, if you want, you can compress it and save a fair bit of space. Right click on it, select “Properties”, click on the “Advanced” button, and select “Compress”.

The next step is to defragment the Windows file system. I always do this two or three times in succession, as it seems to keep improving the way the files are scattered around a bit. That probably doesn’t matter much (or at all), but it seems tidier to me!

After that, you need to zero all the empty space in the filesystem. Download ‘sdelete’ from http://www.microsoft.com/technet/sysinternals/Security/SDelete.mspx to do that job. You should probably download sdelete before you defrag, as when you unzip it a small amount of extra fragmentation is likely to occur.

Open up a DOS window and run sdelete with

sdelete -c c:

(assuming C: is your filesystem). That will zero all the free space.

Then shut down Windows and you’re ready to shrink the VDI file by running the following command on it:

VBoxManage modifyvdi WinXP.vdi compact

(replace “WinXP.vdi” with the name of your vdi file).

One of my VDI files had grown to 5.2GB recently – from a couple of months of Windows updates, etc. Using the method above, i managed to shrink it back to 2.7GB.

20,536 views

27 Responses to “Shrinking VirtualBox VDI Files”

  1. andré says:
    December 22, 2008 at 11:54 am

    I tried this tip but there was no change in the size of the .vdi, still 4.9GB. :(

  2. will says:
    December 22, 2008 at 12:07 pm

    Did you defragment the Windows file system and run ‘sdelete -c’ first?

    If so, how much space does Windows tell you is used in that filesystem?

  3. Hsiang says:
    January 19, 2009 at 2:39 pm

    I think one key point is that the clean up actions, especially the sdelete, need to be run in the guest OS in the virtual machine, not in the host.

  4. will says:
    January 22, 2009 at 2:47 pm

    Thanks Hsiang! I guess i didn’t make that clear in the article.

  5. Thomas says:
    January 27, 2009 at 10:45 pm

    Ooops :(

    D:\vBox>VBoxManage modifyvdi d:/vBox/Virtual_Machines/Win2K3srv.vdi compact
    VirtualBox Command Line Management Interface Version 2.1.2
    (C) 2005-2009 Sun Microsystems, Inc.
    All rights reserved.

    Error: Shrink hard disk operation is not implemented!

    D:\vBox>

    Have you any idea?

  6. will says:
    January 30, 2009 at 9:07 pm

    I suspect it may not work on the filesystem you’re running VirtualBox on. The filesystem that the VDI file resides on must support the method they use to make that file smaller.

    I’ve only used VirtualBox on Linux (ext3 filesystem) and i’ve got no idea how this stuff works with any of the Windows filesystems. I’d suggest posting your question in the VirtualBox Windows hosts forum.

  7. Dominik Enkelmann says:
    February 21, 2009 at 6:48 am

    It is as the message says, the ‘compact’ option to shrink disks is not available (in 2.1.2, 2.1.4), although it was in an earlier version.

    There is a simple workaround you can use to do the shrinking: clone the hard disk. The command would be something like:

    VBoxManage clonehd d:/vBox/Virtual_Machines/Win2K3srv.vdi d:/vBox/Virtual_Machines/Win2K3srvCLONE.vdi

    If you performed the steps described above (must run sdelete!), this will give you a shrinked new disk (with new UUID).

    Have fun!
    Dominik

  8. Jenn says:
    February 22, 2009 at 2:04 am

    I’m running Ubuntu 8.10 and I’m getting “Shrink hard disk operation is not implemented!” Is this something you can only do with a dynamic disk or is it supposed to work on a fixed disk as well? I made the mistake of making mine fixed and now I’m looking to shrink it.

  9. will says:
    February 22, 2009 at 11:57 am

    I’m sure you can’t shrink a fixed-size VDI – that wouldn’t make any sense at all.

    All i can suggest is to create a new blank dynamic VDI, install Linux in a virtual box virtual machine (probably with a “live” CD/DVD ISO as the boot disk), attach you current fixed VDI and the new dynamic VDI to that Linux VM, and copy one to the other, using ‘dd’.

    You’ll need to do it from the command line, and the command you’ll use will be something like:

    dd if=/dev/hda1 of=/dev/hdb1

    (assuming your existing Win VDI is /dev/hda and your new dynamic VDI is /dev/hdb).

    Before you do that, though, you’ll need to use ‘fdisk’ (in the running Linux virtual machine) to create a partition on the new variable disk.

    Then you should be able to remove the old fixed-size VDI from you Win VM and attach the new dynamic one – and, hopefully, if everything went right, it will just work.

  10. will says:
    March 30, 2009 at 7:31 pm

    After upgrading to VirtualBox 2.1, i got the “Shrink hard disk operation is not implemented!” problem too. Reading the comments in this bug report explained why.

    The solution is to uninstall VirtualBox and then install version 2.0.6 (which is the last build in the 2.0 series). You can download it from the old builds section of the VirtualBox download page. Shrinking a dynamic VDI with 2.0.6 works as outlined above.

    So long as there are no features of the 2.1 version that you need, 2.0.6 should solve your problem. However, as 2.0 can’t handle virtual machines created by 2.1, you’ll have to set up your virtual machines again – this doesn’t affect the VDIs, though, they work fine.

  11. Guti says:
    April 10, 2009 at 7:44 pm

    Thanks for the hint on clonehd for doing a vdi compact.

  12. Doug says:
    July 13, 2009 at 5:25 pm

    For those getting an error on ‘shrink not implemented’ .. you can also shrink by doing this:

    vboxmanage clonehd

    Essentially this will create a copy, but at the same time, shrink. Follow all the steps on defrag/sdelete above

  13. Doug says:
    July 13, 2009 at 5:26 pm

    missing params because of the angle brackets, should be:

    vboxmanage clonehd *sourcevdi* *destvdi*

  14. will says:
    July 13, 2009 at 8:45 pm

    Thanks Doug!

  15. Neo says:
    July 30, 2009 at 6:11 pm

    Will Kemp, thanks for this solution , its very good and helpful
    now the .vdi shrunk to its ~1.8 GB real size , difference about ~7 GB
    no need to clone or any other special commands

    am using VBox v 3.0.2 (XP host, XP guest), just followed the steps as it is
    1- Deleted unwanted files .
    2- used sdelete (on guest’s OS c: partition) to zero free space .
    3- modified the .vdi file from host command line >> VBoxManage modifyvdi WinXP.vdi compact .

    thanks again :)

  16. will says:
    July 30, 2009 at 6:30 pm

    Thanks for the feedback Neo. I’m glad it was helpful.

  17. Keith says:
    August 24, 2009 at 4:16 am

    Ok, I’m trying the steps above and no matter if i use the clonehd or compact… i get the error:

    ERROR: Could not find file for the hard disk ‘/home/Keith01/.VirtualBox/VDI/WINXP2.vdi’ (VERR_FILE_NOT_FOUND)
    Details: code VBOX_E_FILE_ERROR (0x80bb0004), component HardDisk, interface IHardDisk, callee nsISupports
    Context: “OpenHardDisk(Bstr(szFilenameAbs), AccessMode_ReadWrite, false, Bstr(“”), false, Bstr(“”), srcDisk.asOutParam())” at line 624 of file VBoxManageDisk.cpp

    I even went in the folder and still same thing…. i’m confused… any help would be appreciated..thanks

  18. Keith says:
    August 24, 2009 at 4:26 am

    Also, I’m running Ubuntu 9.04 x86 and VirtualBox is 3.04. The guest OS are WinXp…

  19. will says:
    August 24, 2009 at 6:58 am

    Does /home/Keith01/.VirtualBox/VDI/WINXP2.vdi exist? What happens if you do


    ls -l /home/Keith01/.VirtualBox/VDI/WINXP2.vdi

    ?

  20. Tony says:
    September 29, 2009 at 7:41 pm

    Thanks for the useful article. FYI I tried your steps without doing the defrag (VirtualBox 3.0.6) and it shrunk my VDI from 16GB to 8.5GB

  21. MikeF says:
    October 1, 2009 at 7:07 am

    Despite the cred of Sysinternals, sdelete appears to be badly broken. I’ve come across numerous postings about it hanging up before completion. Right now, it has hit a brick wall at 33% in my Win2K VM.

    Alternatives suggested like Eraser and BleachBit DO NOT set unused file system blocks to zeros – they offer alternative algorithms to overwrite previous data with random bits (as designed).

    Any alternatives to sdelete, anyone? Or is there a secret switch to make it complete?

  22. tripwire says:
    October 19, 2009 at 4:34 pm

    It might be interesting to disable service like sqlserver etc. before starting to defragment you’re hard disk.

  23. NotALinuxExpert says:
    November 28, 2009 at 1:49 pm

    Thanks a lot for this hint! Helped me a lot!

  24. Mihai says:
    January 10, 2010 at 10:52 pm

    Thanks!!! Worked for me on Linux Mint 8 (32 bit) reiserFS 3.6 and Windows XP on VirtualBox. From 7.5 GB to 4.6 GB.

  25. Jeff says:
    April 20, 2010 at 6:21 pm

    Thanks, I really appreciate your information and will be making your site one of my regular visits.

  26. Wolfram says:
    October 3, 2012 at 10:53 am

    It’s a combination of BleachBit (executed on the Linux guest) and a VM clone executed on the host, in case you are trying to compact a Linux VM running within VirtualBox. See also http://www.ontoport.de/?q=blog/wbartussek/how-compress-virtual-disk-linux-virtualbox.

  27. Koen says:
    February 24, 2013 at 6:35 pm

    Didn’t work for me with sdelete … but making the partition inside the VM smaller and doing a VDI compact after that worked.

Leave a Reply

Click here to cancel reply.

© 2008-2013 Will Kemp. All rights reserved.
Design: Wordpress Classic theme, modified by Will Kemp