回收VirtualBox虚拟机磁盘空间方法

转自: http://maketecheasier.com/shrink-your-virtualbox-vm/2009/04/06

发现最好启动到用户模式下操作,否则会提示分区正忙,无法重新挂载。
zerofree软件可以在rpmfind.net网站上下载

While 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.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26194851/viewspace-723559/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26194851/viewspace-723559/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值