How To Shrink Your Virtualbox VM And Free Up Space For Your Hard Disk

shrink-virtualboxWhile using Virtualbox, one thing that I have found is that the size of the VM (a file with .VDI extension) expands over time. If you have selected the “Dynamically Expanding Storage” option when first creating the VM, you will find that the file size expands whenever you install new applications in the VM. That is perfectly normal. However, when you uninstall or delete applications in the VM, the file size does not shrink or return to its previous size. That is to say, if you are constantly installing and uninstalling applications in your VM, the VDI file could be taking up more space than it is actually using.

If you have a great deal of hard disk space to spare, you can simply ignore this. However, if you find yourself always running short of hard disk space, you might want to consider shrinking the VM and free up unused disk space.

This tutorial will go through the steps to shrink your Virtualbox VM.

It doesn’t matter if you are using Windows/Linux host. The instructions for a Windows and Linux guest are slightly different though. (I have not tried this on a Mac host yet. Theoretically it should work, but do it at your own risk.)

For Windows guest

If you are using Windows guest, do the following:

  • Uninstall all unnecessary applications
  • Perform a hard disk defragmentation. If you are using WinXP, you You can access the inbuilt disk fragmenter from Start -> All Programs -> Accessories -> System Tools -> Disk Defragmenter.

Download sdelete and extract it to your desktop.

Open a command prompt and cd to the sdelete directory. Type in the following command:

sdelete -c c:/

This will add zeroes to all the free space in your C: drive.

Once it is done, shutdown the VM.

For Linux guest

If you are using Linux guest (this tutorial assumes you are using Ubuntu and is running on ext2/ext3 filesytem. It does not work on ext4), here is what you need to do:

Install zerofree

sudo apt-get zerofree

Restart and boot your Linux VM into Recovery mode. (This is generally the second option in the Grub boot up screen.)

In the root shell prompt, type

df

This will display all the available filesystem in your OS. Record down the filesystem where the main directory lies. (it should be in /dev/sdaX or /dev/hdaX format)

vdi-filesystem

Mount the filesystem in read-only mode with the following command:

mount -n -o remount,ro -t ext3 /dev/sda1 /

replacing the /dev/sda1 with the filesytem info that you have recorded just now.

Next, type the following command to zero out all the free space.

zerofree /dev/sda1

Once again, replace the /dev/sda1 with your own filesystem info.

Once you are done, shutdown the Linux VM:

shutdown -h now

Shrinking the VM

Now that we are done with clearing up the VM and zero out all the free space, it’s time to shrink the file size using the VboxManage command.

If you are still using the older Virtualbox 2.0.x version, you can easily shrink the hard disk with the following command:

VboxManage modifyvdi /path/to/your/VM.vdi compact

If you have upgraded your Virtualbox to the 2.1.x version, you will find that the above command will no longer work. There is a bug in the software that disable the use of the modifyhd command. An alternative solution is to use the clonehd command to creat a smaller clone copy of the original VM.

In your terminal (or command prompt), type the following:

VboxManage clonehd name-of-original-vm.vdi name-of-clone-vm.vdi

This will create a clone of the original VM with the name that you have specified in the command above.

Result

After shrinking my VM, I manage to shave 45% off the size of the oirginal VM and free up a whopping 4.5GB of hard disk space to store my other stuff.

The original VDI: 10GB

The cloned VDI: 5.5GB

Importing the clone VM into Virtualbox

The whole process is not finished yet. You still need to import the clone VM into Virtualbox and remove/delete the original VM from your system.

In your Virtualbox console, click on the New button.

Follow the instructions to create a new VM. When it reaches the screen to ask you to choose the hard disk, select Existing.

vdi-new-vm

In the Virtual media manager window, click Add to add the newly cloned VM.

Highlight the newly added VM and click Select.

Back to the Hard disk selection window, you should see your new VM in the dropdown selection. Click on Next to finish up the import.

Once everything is in place, run the newly added VM to make sure that it is working fine.

On the main Virtualbox screen, highlight  the original VM and go to Machine -> Delete. This will remove the entry from the console.

Go to File -> Virtual Media Manager. Highlight the original VM and click the Remove button. When prompt if you want to keep the file, select Delete. This will delete the vdi file from your system.

That’s it.

Slow computer? Try Auslogics BoostSpeed!

Damien Oh is the owner and chief editor of Make Tech Easier

Tags: , ,

 

Enjoyed the article? Subscribe to Make Tech Easier today to get your daily updates of technology tutorials, tips and tricks.

Enter your email:

4 pingbacks/trackbacks (Click to open)

14 Responses

  • VBoxUser says:

    Thank you for your writeup. I found your work-around for the missing compact feature very helpful. One thing to note is that using clonehd as a workaround changes the UUID. I have not found a command to manually revert the UUID for the purposes of making it a drop in replacement for the pre-shrunk VDI. Have you encountered a solution?

    Reply

  • Eagleman says:

    Great, but is does not work.

    The clonehd command creates a new vdi which is exactly as large as the original vdi, 15.3 Giga Bytes.

    However, windows (the vdi is a virtual XP system) reports that it
    only uses 8.9 GiB.

    Your thoughts please.

    Eagleman

    Reply

  • VBoxUser says:

    @Eagleman
    clonehd did indeed shrink my image. I would recommend following all of the steps including defragging and zeroing out the empty space to ensure a successful shrink. The one drawback with this method is UUID change.

    Reply

  • Eagleman says:

    IT WORKS!

    What I did not do (read the f** manual!) is running SDELETE in my XP guest OS.

    CLONEHD actually shrunk my vdi from 15.3 to 9.3 GiB.

    Thanks everybody!

    Eagleman

    Reply

  • svetlyachok says:

    Thanks Damien. I’m surprised how do you write a bunch of useful articles while on other blogs i read 1 needed article, then go away.

    Regarding this article. Glad that command “VboxManage modifyvdi |—| compact” worked on my Intrepid with VirtualBox 2.2.2. However i faced one difficulty with it – “bash: VboxManage: command not found”.
    The workaround was to change “VboxManage” to ‘/usr/lib/virtualbox/VBoxManage’ (path to executable with quotes), followed by arguments mentioned in article.

    Reply

    Damien Reply:

    Thanks for your kind words. I guess that’s because I live and breathe technology everyday

    Reply

  • shadowatthesun says:

    You are the best man…
    I only need these command (virtualbox 2.2.4):
    1. sdelete -c c:/
    in windows
    2. VboxManage modifyvdi HardDisks/Win.vdi compact
    in my host..
    cool..

    Reply

  • VBoxUser says:

    Has anyone found a way to change the UUID yet? This being to fix the outcoming image from clone performed while the modifyvdi was disabled in the 2.x versions before current.

    Reply

  • Rcamp says:

    I don’t suppose there is a method for Mac OSX users is there…?

    Reply

  • Damien says:

    Theoretically, the method is the same for Mac OSX users, but I haven’t give it a test yet, so I can’t confirm whether the above method works in OSX or not.

    I will update again as soon as I have tested it out. :)

    Reply

  • john says:

    The command “VBoxManage modifyvdi /path/to/your/VM.vdi compact” works with version 2.2.4. Just make sure you type VBoxManage instead of VboxManage.

    Reply

  • Conor says:

    the smae command should work on a Mac host. except has slightly renamed switches. just open up a terminal windows and run the command.
    VboxManage modifyhd /path/to/your/VM.vdi –compact

    Reply

    Cliff Reply:

    similar command works with Ubuntu 9.04 host using VBox 3.0.4.

    VBoxManage modifyhd –compact /path/to/your/VM.vdi

    Reply

  • Lance Wig says:

    Great and useful tutorial. Thank You. I was able to get the VboxManage modifyvdi command (with lowercase b) to work fine on Mac OS X Leopard with VB 2.2.4 http://bit.ly/yHk1t

    Reply

Leave a Comment




 

Sign in with Twitter

Comment

Please Note: Comments maybe under moderation after you submit your comments so there is no need to resubmit your comment again