How to shrink your Oracle Virtualbox virtual machines

hrink your Oracle Virtualbox virtual machines

After playing a few time with a virtual machine, installing and un-installing Oracle software on Oracle VirtualBox, I realised that the vdi disk files of some VM where not shrinking despite deleting all files on the virtual machine itself, and I needed the disk space to build other VMs, so what can I do?

Oracle Virtualbox provides a command line utility called VBoxManage which I already mentioned in another post about mass creation of virtual disks. This time it allows you to compact your dynamically allocated disks:

but that won’t do much because you big, fat files may have been deleted from your VM, they have only been deleted from your volume’s partition tables, the actual data is still there in all the blocks. For the compact option to work, you have to reset all blocks that don’t match a file listed in the partition table to zero ‘0’. This low level reset can be done with a tiny piece of software called zerofree written by Ron Yorston. While you can get the source code (made up of 2 files..) which you have to compile, I used an rpm for Enterprise Linux 5 even though I was on an Oracle Enterprise Linux 6.3. So got the package from rpmbone and installed in on my guest system

zerofree must be run on a read only partition. While this is more complicated if you want to reset empty blocks on your / partition, I had everything on /u01, /u02 and /u03, so it is really easy to remount those file systems read only:

  • Stop anything running on those partitions: databases, listeners, weblogic domains, grid infrastructure, you name it!
  • Remount the partition in read-only mode, and check status with mount command, see /u01 now with the ‘ro’ flag:
  • If it complains about disk busy, it means your still have files opened for writing, or programs being run, I was on RAC node here and found some java program which I killed off…

Run zerofree against the device name, not the mount point, and watch it run:

This process may take a few minutes if there are many blocks to reset, once done, shutdown the virtual machine.
Back on the host system, run VBoxManage with the compact option against the disks (of the halted VM):

Now look at the size of the file:

Down from 24 to 14Gb, waow, wish I could do the same with my waistline!!

Now all this was done on an Ubuntu host with an Oracle Enterprise Linux VM, the VBoxManage also exists if you run VirtualBox under Windows, the command will look something like:

David White

About David White

David White has written 18 post in this blog.

Oracle Technologies Consultant at Easyteam

One thought on “How to shrink your Oracle Virtualbox virtual machines

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *