Ubuntu – VirtualBox VDI file size keeps growing with Ubuntu 16.04

16.04disk-usagevirtualbox

My VDI file keeps growing and I don't understand why. VirtualBox support have confirmed this is an Ubuntu Guest OS issue, not a host VB one. (BTW, is this question best answered here, or should I be asking at https://superuser.com ?)

I am running latest VirtualBox (5.1.18) under Windows 7 (Home Premium) 64-bit as Host. Ubuntu 16.04 (latest patches) 64-bit as Guest. VDI file is using dynamically allocate storage up to 100G.

When I used to run Ubuntu 14.04 all seemed reasonable. Recently I have been finding the VDI image keeps growing in size. It has grown from 7G to 14G. (This may not be much to some of you, but with an 8G memory stick for backup it's a problem for me!) I can boot Ubuntu to Unity desktop and then immediately exit, and the image can grow by 0.5G.

In Ubuntu the output from df -h shows:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        97G  7.7G   84G   9% /

So that is saying there is an extra 6G beyond Used which has been "lost" compared to Avail. du -h -x -s / confirms 7.6G used.

  1. What & where is the extra missing storage?
  2. Are there known problems here between VirtualBox and Ubuntu 16.04 (14.04 did not seem to have this problem)? I am seeing some posts on the net stating that 16.04 is using excessive desktop memory, though nothing amounting to 6G….

Best Answer

In my knowledge, when you or any program writes something to the disk, after removing that file only the related inode to that file will be removed and data (zero and ones) remains on the disk, so while you are using dynamic allocated storage, the VirtualBox has no idea what files are living in your OS, it's just know for example 10G of data has been written to your disk, so it growth your hard disk size to manage that 10G of data.

What you can do?

  1. zero out the free space of hard disk in guest, in Linux distributions you can use dd utility to do that:

      sudo dd if=/dev/zero of=/home/zero bs=1M
    
  2. then use VBoxManage command with --compact switch to make your VDI files compact.

    VBoxManage modifyhd '/home/ravexina/VirtualBox VMs/Ubuntu/ubuntu.vdi'
    

More infromation about compacting: here

Reserved blocks

And about the size you mentioned, I guess that would be the reserved blocks for ext4 file system. for my home partition which is sda2.

user@machine:~$ sudo tune2fs -l /dev/sda2 | grep -ie 'reserved block count'

Reserved block count: 1408704 

And because my block size is 4096 byte, so 1408704x4096 makes 5770051584 bye which equals to 5.77 GB reserved space on my home patition.

Related Question