1

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 MBytes

Is there any way by which I can reclaim space in host system.

3
  • There is no Ubuntu 21 so what are you using?
    – David
    Sep 4, 2021 at 13:42
  • It's Ubuntu 21.04. Updated the same in my query. Sep 4, 2021 at 17:12
  • VDI images can only grow. To "shrink" it you have to copy every thing to a new file. The complete process is described e.g. here superuser.com/questions/529149/…
    – Robert
    Sep 4, 2021 at 22:22

1 Answer 1

1

Shrinking a .vdi is pretty straightforward, but will require that you intentionally “fill the device to capacity”. The basic process works like this:

  1. Uninstall any applications and delete any files you may not need anymore
  2. Create a giant file consisting of only zeroes on the storage device you want to shrink
  3. Let the file fill the entire device (this will not actually consume all the data on the host system)
  4. Delete the file
  5. Shut down the VM
  6. Shrink the .vdi files

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

  1. On the VM, open a Terminal (or SSH in)
  2. 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
    
  3. Let the machine run out of space on that storage device, which will result in dd reporting an error
  4. Delete the file
    sudo rm -f /bigzero
    
  5. Shut down the VM

Now, on the host, do this:

  1. Open a Terminal (if one is not already open)
  2. Navigate to the directory with the .vdi:
    cd /mnt/VMs/vms/mx19-102
    
    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.
  3. Shrink the .vdi:
    VBoxManage modifymedium disk mx19-102_1.vdi --compact 
    
    You will see the progress as the tool outputs it’s completion status like this:
    0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
    
  4. 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .