Source: http://blog.sina.com.cn/s/blog_591a2c940100anms.html#cmt_856531
Because I often need to test some software, I installed a set of windows in the virtualbox virtual machine. Anyone who has used virtual machines knows that dynamic expansion images are generally used to save hard disk space. It does not occupy the specified size of hard disk space at the beginning, but dynamically scales as needed. However, if files are deleted from the virtual system, the used space is not automatically recovered. After the virtual system has been used for a long time, the disk image file will become larger and larger.
I have used VMware before. There is a convenient shrink feature in VMware Tools to reclaim unused hard disk space, but this option is not available in virtualbox. After searching for information on the Internet, I finally found the shrink method in virtualbox (I used version 2.0.0). It only takes three steps:
Step 1: Clear disk fragments in guest OS. Defraggler is recommended. This is a free disk fragment tool that is easy to use and can be quickly organized.
Defraggler: http://www.filehippo.com/download_defraggler/
Step 2: Clears the remaining space of the guest OS disk, that is, writes the value 0 to every byte of the space not used by the disk. This step is critical, because in the next step, all disk space containing only 0 values will be withdrawn. You can use sdelete, a command line tool produced by sysinternals. Run the following command in the guest OS command line:
Sdelete-c :\
C: \ is the disk partition to be cleared. Perform this step on all virtual disk partitions in sequence, such as D: \ and E.
Sdelete: http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx
Step 3: Use the vboxmanage tool provided by virtualbox to shrink the disk image file. Enter the virtualbox installation directory (default: C: \ Program Files \ sun \ xvm virtualbox) in the command line of the Host OS, and execute:
Vboxmanage modifyvdi"Windows XP. VDI"Compact
Here, Windows XP. VDI is the name of the disk image to be shrunk. You do not need to specify a path to change it based on the actual name.
After these three steps, my VDI file has been reduced from 1.2 GB to GB, and the effect is very obvious. The tools used in the first two steps described above are for Windows. If Guest OS is Linux or other systems, you can find tool software with the same functions in the same principle.