Unable to reclaim space in vbox disk
I am using virtual box in Ubuntu 21.04 to create VMs and test. I have one virtual machine which was using approx 70 GB data. this data was used from additional disk. as below:
puneet@puneet-PowerEdge-T30:/mnt/VMs/vms/mx19-102$ pwd
/mnt/VMs/vms/mx19-102
puneet@puneet-PowerEdge-T30:/mnt/VMs/vms/mx19-102$ du -sh *
772K Logs
69G mx19-102_1.vdi
8.0K mx19-102.vbox
8.0K mx19-102.vbox-prev
9.9G mx19-102.vdi
808K Snapshots
puneet@puneet-PowerEdge-T30:/mnt/VMs/vms/mx19-102$max space is taken by mx19-102_1.vdi in host system while in actual it is not in use.
puneet@mx:~
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 28G 0 part /
└─sda2 8:2 0 2G 0 part [SWAP]
sdb 8:16 0 168.8G 0 disk
└─sdb1 8:17 0 168.8G 0 part /media/data
sr0 11:0 1 1024M 0 rom
puneet@mx:~
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 964M 0 964M 0% /dev
tmpfs 200M 2.3M 198M 2% /run
/dev/sda1 28G 7.7G 19G 30% /
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 809M 76K 809M 1% /dev/shm
/dev/sdb1 169G 17M 167G 1% /media/data
cgroup 12K 0 12K 0% /sys/fs/cgroup
tmpfs 200M 4.0K 200M 1% /run/user/118
tmpfs 200M 0 200M 0% /run/user/1000
puneet@mx:~
$Disk Information:
Parent UUID: base
State: locked read
Type: normal (base)
Location: /mnt/VMs/vms/mx19-102/mx19-102_1.vdi
Storage format: VDI
Format variant: dynamic default
Capacity: 172785 MBytes
Size on disk: 70236 MBytesIs there any way by which I can reclaim space in host system.
31 Answer
Shrinking a .vdi is pretty straightforward, but will require that you intentionally “fill the device to capacity”. The basic process works like this:
- Uninstall any applications and delete any files you may not need anymore
- Create a giant file consisting of only zeroes on the storage device you want to shrink
- Let the file fill the entire device (this will not actually consume all the data on the host system)
- Delete the file
- Shut down the VM
- Shrink the
.vdifiles
This sounds like a lot of work but, after one or two run-throughs, you’ll see there’s a semi-easy way to automate this down to two steps if you’re so inclined.
So let’s get started doing it “the long way”:
- On the VM, open a Terminal (or SSH in)
- Assuming you’ve already removed everything you don’t need, create a big file of zeroes:
sudo dd if=/dev/zero of=/bigzero bs=4096k - Let the machine run out of space on that storage device, which will result in
ddreporting an error - Delete the file
sudo rm -f /bigzero - Shut down the VM
Now, on the host, do this:
- Open a Terminal (if one is not already open)
- Navigate to the directory with the
.vdi:
Note: You don’t need to be in the same directory, but it does make it easier to verify the shrunken file size after the upcoming operation.cd /mnt/VMs/vms/mx19-102 - Shrink the
.vdi:
You will see the progress as the tool outputs it’s completion status like this:VBoxManage modifymedium disk mx19-102_1.vdi --compact0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% - Verify the results:
du -sh * 772K Logs 30G mx19-102_1.vdi 8.0K mx19-102.vbox 8.0K mx19-102.vbox-prev 9.9G mx19-102.vdi 808K Snapshots
There are quite a few useful operations that modifymedium enables if you ever need to change an attribute of the virtual storage device.