Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Disk image file on host computer doesn't decrease when I delete huge files in virtual machine? [closed]

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?

like image 453
vectorijk Avatar asked Feb 17 '13 13:02

vectorijk


2 Answers

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.

like image 146
Terry Wang Avatar answered Nov 14 '22 23:11

Terry Wang


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.

like image 39
SJuan76 Avatar answered Nov 14 '22 23:11

SJuan76