I installed CentOS on the virtualbox(Host OS is Windows 7). I created the disk image file that is VDI files(dynamically allocated image). Recently,when I delete huge files in CentOS,the VDI file on my disk doesn't decrease. Why? How to tackle this problem?
VirtualBox does not automatically reclaim disk space (on host) when you delete files within the VM. To shrink the disk image, you need to
First, zero out all free spaces (for all partitions and Logical Volumes on the HDD) and then turn the VM off.
cat /dev/zero > z;sync;sleep 3;sync;rm -f z
Then, use the following command to compact the VDI.
VBoxManage modifyhd /path/to/image.vdi --compact
VBox Doc
With the --compact option, can be used to compact disk images, i.e. remove blocks that only contains zeroes. This will shrink a dynamically allocated image again; it will reduce the physical size of the image without affecting the logical size of the virtual disk. Compaction works both for base images and for diff images created as part of a snapshot.
BTW: For VMWare (.vmdk), you need to use vmware-vdiskmanager -k xxx.vmdk
to do the same.
How can the host know which sectors offered to the VM are being used by the VM and which are free?
The only thing it knows is when the VM requests a sector that still was not assigned in the disk file (when it increases it).
Maybe "defragmenting" or a similar procedure could reorganize disk usage well enough so that you could manually tell the host to reduce the space. So far, I do not know of any VM host that offers that feature, though (and using it would always be very risky).
The simplest alternative would be cloning your VM to a new one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With