How to reduce your virtualbox linux virtual machine size

After using a linux virtual machine that has been configured with dynamically expanding drive, the size gets larger even if you remove files from it.

Here is the steps to shrink your linux virtual machine size :

1- Remove any unnecessary files you don’t need from the virtual machine.

2- Fill the unused spaces on it with zeros.

  • Open the virtual machine.
  • Install zero free
sudo apt-get install zerofree
  • Reboot the machine in repair mode, and login to root shell
  • Kill any processes using the hard disk.
service rsyslog stop
service network-manager stop
killall dhclient
  • list your virtual machine harddisks
df
  • Unmount and Fill all the unused spaces for each physical drive on the machine with zeros.
mount -n -o remount,ro -t ext3 /dev/sda1 /
zerofree /dev/sda1
  • Shutdown the machine.

3- Clone the old HD file into a new shrinked one

VBoxManage clonehd oldHd.vdi newHd.vdi

4- Now add the new cloned HD file as the new HD for the machine and test it.

5- Remove your old HD file.

 

Leave a Reply

avatar
  Subscribe  
Notify of