How To Reduce vdi Size in VirtualBox and Free Disk Unused Space
VirtualBox is an oracle hypervisor that we install in our base machine or laptop to use another guest OS in this. Virtual box and we always select dynamically allocated disk which will increase as we add data in guest OS. But it does not automatically reduce even we delete data in guest OS. We have to reduce the size by using some commands with a process. This process I will show you here.
Related Article: How To Change Owner Of Home Directory In Linux With Extra Tip
Make sure you are shrinking the disk if you seen the difference between guest OS disk size is lower than vdi disk size.
Step 1 – Check you are using dynamically allocated disk
Step 2 – Need to Tell OS unallocated space has nothing(zero) or defrag to consolidate space.
Step 3 – Run Virtual Box Command to Shrink Disk
Step 1 – Check you are using dynamically allocated disk
Check the virtual box that it has dynamically allocated disk if it is fixed then you have to change it.
Power off the virtual machine and change it to dynamically allocated to shrink disk.
Step 2 – Need to Tell OS unallocated space has nothing(zero) or defrag to consolidate space
For windows, you need to defrag disk but here we will show you the Linux procedure.
1. Install zerofree
Go to guest OS or VM and install zerofree
# apt-get install zerofree
2.Reboot Server
Reboot server because this command will not work properly on normal mode so we have to boot in recovery mode.
3. Go to Advanced Option
Press the escape button to stop normal boot and the select Advance option for ubuntu.
4. Go to Recovery Mode
Select recovery mode
5.Drop to root shell Prompt
Select Drop to root shell Prompt
6. Check Root Partition
check partition name with df command
# df
7.Run zerofree command
Now run the command on the disk you want to reduce. In our case, it is the root.
# zerofree -v /dev/dm-0
After successfully completing this command run below command
# halt
8- Power off VM
Step 3 – Run Virtual Box Command to Shrink Disk
Now open the terminal in base OS and go to the location where vdi file is located. Otherwise, you have to mention the full path of vdi file.
Run below command :
VBoxManage modifyhd name.vdi –compact
#VBoxManage modifyhd webserver.vdi --compact
Now at this point, we have successfully decreased the size of vdi file.
This is very useful in the case of the Database server. Where you rotate data from one server to another.