[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:

VBoxManage.exe list hdds

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:

VBoxManage.exe modifymedium disk <DISK_LOCATION> --compact

VBoxManage modifymedium disk <DISK_LOCATION> --compact

0 0 vote
Article Rating
Notify of
newest most voted
Inline Feedbacks
View all comments
1 year ago

Have you a prroblem with the compact process ?, my process, consumming all the ram, forcing other process to liberate ram too.

Then the proccess turning very slow, all the machine really.
some times if i try to run a program, the ram, go down to the normal ram used by this process liberating more than the 95%, but i cant see all the aspect for repeating the flush.

its safe to stop the process ?

1 year ago

Ubuntu 18.04’s rescue shell has the rootfs mounted writable. Zerofree refuses to run in this case and ‘mount -o remount,ro /’ just returns ‘/dev/sda1 is busy’. I used guestfish to run zerofree outside of virtualbox, but booting from an ISO image would also work