Virtualbox zero free unused HD Space

Source: Internet
Author: User
How to shrink a dynamically-expanding guest virtualbox Image

Http://dantwining.co.uk/2011/07/18/how-to-shrink-a-dynamically-expanding-guest-virtualbox-image/

 

Sometimes bigger isn' t always better. If your dynamically-expanding Virtual Machine images are growing out of control, then here's how to trim them back...

 

Background

I'm a big fan of virtualbox, and use separate virtual machines (VMS) for the various separate bits and pieces I 've got on the go (as I invariably end up messing something up, and can just trash the image and start again, without taking down whatever else it is I'm playing with at the time ).

All my VMS use a dynamically expanding image for their hard drive, where you set the maximum size of the disk, but the system will only grow to fill that space if required. by setting this nice and high, I can be sure that the hard drive space is there if I need it, without taking space away unnecessarily from the rest of the system.

Unfortunately, whilst virtualbox will dynamically expand the hard drive as it's required, it won't dynamically shrink it again if you free up space in the VM. this can be a problem, especially if, like me, the sum total of all those theoretical maximums exceeds the actual maximum capacity of the hard drive hosting all these VMS.

The good news is that you can shrink those images back down again. the bad news is that a lot of the guides on the Internet are out-of-date, and woefully misleading. here's what I did to get the job done...

1. free up space in the client machine

It's a bit of an obvious first step, but you can only shrink down the client VM by the size of the available free space therein, so delete the files and uninstall the programs that you no longer need but are hogging your resources.

2. Zero out the free space

Virtualbox only knows that the space is really free if it's been set to zero, and standard deletion won't do this.

If it's an Ubuntu VM

You'll want to use zerofree:

  • InstallSudo aptitude install zerofree
  • (If you don't have aptitude, you can either use apt-get to install zerofree (Sudo apt-Get install zerofree) Or use apt-get to install Aptitude (Sudo apt-Get install Aptitude). I 'd recommend getting hold of aptitude, as it does a great job of managing packages in Ubuntu)
  • Reboot the machine (Sudo shutdown-R now). During boot, hold down the left shift key. A menu will appear, you need to select "recovery mode"; this shoshould be the second item in the list.
  • You'll get another menu, towards the bottom there shoshould be the option to "drop to root shell prompt"
  • RunDFAnd look for the mount point that's that the biggest-this is where all your files are, and is the one we'll need to run zerofree against. for the rest of this Guide, we'll assume it's/dev/sda1
  • The following three commands (thanks, virtualbox Forum !) Stop background services that we can't have running:
    • Service rsyslog stop
    • Service Network-manager stop
    • Killall dhclient
  • Once they 've stopped, you can re-mount the partition as readonly (zerofree needs this)
    • Mount-n-o remount, ro-T ext3/dev/sda1/
  • You can now run zerofree
    • Zerofree-V/dev/sda1
  • Finally, shut down the VM
    • Shutdown-H now
If it's a Windows VM

You'll need to runSdelete; I 've never done this, but there are instructions on that here:

  • Http://maketecheasier.com/shrink-your-virtualbox-vm/2009/04/06
3. shrink the VM

Quite a lot of the online guides say that you'll have to clone the hard drive image to shrink it, as virtualbox 2.2 and above dropped support for compacting the image. this isn't true, certainly not for version 4.0.4, And you can shrink the image in-place with the following command:

  • Vboxmanage modifyhd my. VDI-compact
Where you replace my. VDI with the name of the VDI you 'd like to shrink (for more information on this command, see the virtualbox manual). That's it!

With any luck, you'll all now have plenty of disk space to fill will equally useless tat...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.