How To Reduce vdi Size in VirtualBox and Free Disk Unused Space | Linux Servers

How To Reduce vdi Size in Virtual Box and Free Disk Unused Space | Linux Servers

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.
Make sure you are shrinking the disk if you seen the difference between guest OS disk size is lower than vdi disk size.
1 size how to reduce vdi size in virtualbox
2 %2Bsize%2Bof%2Bvdi how to reduce vdi size in virtualbox

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.
3 size%2Bof%2B.vdi%2Bfile how to reduce vdi size in virtualbox

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
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.
reboot server

3. Go to Advanced Option

Press the escape button to stop normal boot and the select Advance option for ubuntu.
Advance option for Ubuntu

4. Go to Recovery Mode

Select recovery mode
recovery mode

5.Drop to root shell Prompt

Select Drop to root shell Prompt
Drop to root Shell prompt

6. Check Root Partition

check partition name with df command
# df 
9 %2Bcheck%2Broot%2Bpartion%2Bby%2Bdf how to reduce vdi size in virtualbox

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
zerofree
After successfully completing this command run below command
# halt
system halt

8- Power off VM

Power off Virtual Box

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
 
vboxmanage
 
Now at this point, we have successfully decreased the size of vdi file.
15 %2Bsize%2Breduced how to reduce vdi size in virtualbox
 
16 how to reduce vdi size in virtualbox
This is very useful in the case of the Database server. Where you rotate data from one server to another.

Leave a Comment

Your email address will not be published. Required fields are marked *