Web Dev, Linux, WordPress & More

Compact a VirtualBox Windows Guest on Ubuntu Linux

by eric on November 11th, 2008 in: Linux

I use VirtualBox to run Windows XP as a virtual machine within Ubuntu Linux and periodically like to compact the virtual drive (VDI) file.

Being a bit boneheaded, every time I do this, I spend ten minutes searching for instructions, so I thought I’d post them here both for myself, and for anyone else it may help.

Steps to Compact a VirtualBox WinXP Guest in Linux

  1. While in your Windows XP virtual machine, download the sdelete program and save it to your c: drive in Windows.  This program allows you to ‘zero out’ the free space on the drive by writing zeros to the unused sectors.
  2. In WinXP go to Start/Run and type ‘cmd‘ to open a command prompt.
  3. Change directory (cd) to the location where you saved the sdelete.exe file.
  4. Enter ‘sdelete -c c:/’ to execute the program.
  5. Once sdelete is finished, shut down the Windows virtual machine.
  6. Open a terminal in Linux and cd to the directory that contains the VDI you’d like to compact.
  7. Type ‘VBoxManage modifyvdi xxxxx.vdi’ compact where xxxxx.vdi represents the filename of the virtual disk you’d like to compact.
  8. After the command is runs, your VDI will be reduced in size.

There’s a ton of information on Sun’s VirtualBox site and forum.  Here’s a forum thread that I found especially helpful.

8 Responses to ' Compact a VirtualBox Windows Guest on Ubuntu Linux '

Subscribe to comments with RSS or TrackBack to 'Compact a VirtualBox Windows Guest on Ubuntu Linux'.

  1. Sean B. said:

    November 11th, 2008 at 23:30

    Interesting–I haven’t thought of ever compacting my vdi files. I think I’ll give this a try now that I see my HD space ever dwindling (mostly due to VirtualBox in the first place!)

    Thanks for posting the instructions

  2. eric said:

    November 12th, 2008 at 09:00

    No problem. I ran it yesterday on my main WinXP virtual machine and the VDI went from 8.3 to 7GB. Let me know how it works for you.

  3. November 19th, 2008 at 02:57

    Ah storage space is like a cupboard, you can get in a lot there but never enough and the room is cluttered!

  4. Jeff McCune said:

    February 13th, 2009 at 13:10

    Do you recommend any tools for cleaning out things like the swap file before sealing a VM guest with sysprep?

  5. eric said:

    February 13th, 2009 at 13:47

    Hi Jeff,

    I’ve really not had any experience with that, but would be curious to know if you find anything.

  6. eric said:

    April 22nd, 2009 at 13:49

    I’ve just tried to run this procedure after upgrading VirtualBox to the latest version and it’s not working.

    Looks like Sun changed the command line ‘modifyvdi’ command to ‘modifyhd’ and has temporarily removed the ability to compact the disk.

  7. eric said:

    April 28th, 2009 at 12:51

    Okay, I’ve just upgraded to VirtualBox 2.2.2 and the modifyvdi command is deprecated and replaced by the modifyhd command..

    To run the VDI compact command in step g. above:

    Type ‘VBoxManage modifyhd xxxxx.vdi compact‘ where xxxxx.vdi represents the filename of the virtual disk you’d like to compact.

    If you get an error regarding the disk location, you may wish to use the full path & file in place of the xxxxx.vdi above. (ex. VBoxManage modifyhd /home/eric/.VirtualBox/VDI/WinXP_Pro.vdi compact)

  8. eric said:

    October 1st, 2009 at 06:36

    Hi MikeF,

    I’ve not had any trouble with sdelete, although I have only used it on virtualized WinXP systems. I don’t get the impression that it’s been updated in years, but I don’t think it really needs to be.

    This Microsoft Technet article seems to indicate that sdelete works with Windows 2000.

    I’ve also used Steve Gibson’s SpinRite, although it does a lot more than sdelete and is meant more for hard disk maintenance.

Leave a Reply

%d bloggers like this: