How to Reclaim Space in VM ?

Many times, we come across situations where we need to reclaim some space from the VM for the host OS.

You can reclaim the space from your VM by using below steps.

1. Inside the running Linux VM, Check the available space. You can replace /u01 mount point according to your requirement. 

[root@test ~]# df -hT /u01
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sda5      xfs   345G  167G  179G  49% /u01

2. Create a temporary file that fills the unused space with zeros. Below command will make a file zero.fill of the size 100GB.

[root@test ~]# dd if=/dev/zero of=/u01/zero.fill bs=1M count=102400 status=progress

3. Remove the temporary file.

[root@test ~]# rm -f /u01/zero.fill
[root@test ~]# sync

4. Shutdown the VM.

5. On Windows, Open Command Prompt and run the following, replacing the example path with your actual VDI path:

C:\Users> "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifymedium disk "C:\TEST.vdi" --compact
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%...

Wait for compaction to finish, then check the actual size of TEST.vdi in Windows Explorer.

6. Start your VM and check the size of /u01 mount point.

[root@test ~]# df -hT /u01

Disclaimer: 

This is an independent personal blog and is not affiliated with, sponsored by, or endorsed by Oracle Corporation. The views, explanations, and experiences expressed here are the author’s own. Commands, scripts, and procedures may be based on Oracle’s official documentation and are provided for educational and reference purposes only. Always test and validate them before use in a Live/Production environment. Sample output shown in this article was generated during the author’s own testing and may vary depending on the environment.

Please read full Disclaimer.