[SOLVED] VirtualBox – how to reduce virtual disk size

[SOLVED] VirtualBox – how to reduce virtual disk size

This guide shows you how to free up memory used by VirtualBox machines.

I. [Optional] Remove unnecessary data from the virtual machine

Run the virtual machine and remove unnecessary data. You can use tools like WinDirStat (Windows) or baobab (aka “Disk Usage Analyser” – for Linux).

#install baobab
sudo apt-get install baobab

#run Disk Usage Analyser as root
sudo baobab

 

II. [Optional] Fill unused disk space by zeros

Linux guest OS

There is a tool for Linux(called zerofree) that fills unused disk space by zeros which can be free up in the next step.

First you have to install zerofree  by:

sudo apt-get install zerofree

Then reboot the machine and run linux in safemode (press ESC  key on BIOS screen and choose Advanced -> Recovery Mode in Grub menu). Then run:

zerofree -v /dev/sda1

After all shutdown the machine:

shutdown now -hP

 

Windows Guest OS

Run virtual machine and then run Disk Defragmenter  for all disks.

 

III. Compact VirtualBox disk

Go to the VirtualBox installation directory. Default location is:

  • Windows: C:\Program Files\Oracle\VirtualBox;
  • Linux: /usr/share/virtualbox/ – /usr/bin/VBoxManage  is linked to /share/virtualbox/VBox.sh.

and run the following command to get the location of disk you are interested in:

#Windows
VBoxManage.exe list hdds

#Linux
VBoxManage list hdds

Here is my result:

UUID:           e94cfd7c-c792-4e47-9964-81479a1135f5
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       D:\VM\Better-Coding.vdi
Storage format: VDI
Capacity:       102400 MBytes
Encryption:     disabled

 

Compact chosen disk by the following command:

#Windows
VBoxManage.exe modifymedium disk <DISK_LOCATION> --compact

#Linux
VBoxManage modifymedium disk <DISK_LOCATION> --compact

 

 

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu