After a virtual machine is used for a long time, it will find that the virtual hard disk is getting bigger and bigger. However, after reading the system commands in the virtual machine, the occupied space is far from the size of the virtual hard disk, this is unpleasant and inconvenient for sharing Virtual Machine images. Virtualbox does not seem to provide a graphical interface for us to compress the virtual hard disk size, but it can be achieved through the command line. According to actual tests, a 30 GB virtual hard disk can be compressed to 13 Gb, the space that can be compressed depends on the actual space occupied by your virtual machine.
Virtualbox virtual hard disk
Directory [-]
- 1. Fragment
- 2. compress the disk
Virtualbox supports both its own virtual hard disk format VDI and VMWare's vmdk format. The compression of the two formats is slightly different.
1. Fragment
The first step is to fragment, open the virtual machine, and execute the following command:
Linux:
12 |
sudo dd if = /dev/zero of= /EMPTY bs=1M sudo rm -f /EMPTY |
In Windows, you need to download sysinternals suite and execute:
2. compress the disk
Shut down the Virtual Machine. Now you can compress the virtual hard disk.
If your virtual hard disk is in the VDI format of virtualbox, find your virtual hard disk file and run the following command:
1 |
VBoxManage modifyhd mydisk.vdi --compact |
If your virtual hard disk is in the vmdk format of VMware, It is troublesome, because virtualbox does not support direct compression of the vmdk format, but it can be changed to VDI and compressed first, switch back to vmdk. Run the following command:
123 |
VBoxManage clonehd "source.vmdk" "cloned.vdi" -- format vdi VBoxManage modifyhd cloned.vdi --compact VBoxManage clonehd "cloned.vdi" "compressed.vmdk" -- format vmdk |
In fact, when executing the command, we can find that the conversion from vmdk to VDI seems to have been compressed, and the file size has been greatly reduced, the second command did not see any changes in the file size, so the second command should be omitted here.
Reference: http://my.oschina.net/tsl0922/blog/188276