Shrinking a Linux OEL 6 virtual Box image (vdi) hosted on Windows 7
By AndyBaker on Oct 18, 2013
After copying over all the binaries and performing all the installations the virtual box became in the region of 80Gb used size on the host operating system, however internally it only really needed around 20Gb. This meant 60Gb had been used when copying over all the binaries and although now free was not returned to the host operating system due to the growth of the virtual box storage '.vdi' file. Once the ‘vdi’ storage had grown it is not shrunk automatically afterwards.
Space is always tight on the laptop so it was desirable to shrink the virtual box back to a minimal size and here is the process that was followed.
Install 'zerofree' Linux
package into the OEL6 virtual box
The RPM was downloaded and installed from a site similar to below;
A simple internet search for ’zerofree Linux rpm’ was easy to perform and find the required rpm.
Execute 'zerofree' package on the desired Linux file system
To execute this package the desired file system needs to be mounted read only. The following steps outline this process.
As root: # umount /u01
As root:# mount –o ro –t ext4 /u01
NOTE: The –o is options and the –t is the file system type found in the /etc/fstab.
Next run zerofree against the required storage, this is located by a simple ‘df –h’ command to see the device associated with the mount.
As root:# zerofree –v /dev/sda11
NOTE: This takes a while to run but the ‘-v’ option gives feedback on the process.
What does Zerofree do?
Zerofree’s purpose is to go through the file system and zero out any unused sectors on the volume so that the later stages can shrink the virtual box storage obtaining the free space back.
When zerofree has completed the virtual box can be shutdown as the last stage is performed on the physical host where the virtual box vdi files are located.
Compact the virtual box ‘.vdi’ files
The final stage is to get virtual box to shrink back the storage that has been correctly flagged as free space after executing zerofree.
On the physical host in this case a windows 7 laptop a DOS window was opened.
At the prompt the first step is to put the virtual box binaries onto the PATH.
C:\ >echo %PATH%
The above shows the current value of the PATH environment variable.
C:\ >set PATH=%PATH%;c:\program files\Oracle\Virtual Box;
The above adds onto the existing path the virtual box binary location.
The above changes directory to where the VDI files are located for the required virtual box machine.
C:\Users\xxxxx\OEL6.1>VBoxManage.exe modifyhd zzzzzz.vdi compact
NOTE: The zzzzzz.vdi is the name of the required vdi file to shrink.
Finally the above command is executed to perform the compact operation on the ‘.vdi’ file(s). This also takes a long time to complete but shrinks the VDI file back to a minimum size. In the case of the demonstration virtual box OEM12c this reduced the virtual box to 20Gb from 80Gb which was a great outcome to achieve.