How to shrink 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?

dwhite@squirt:/vm/srvoel099$ ls -lh
total 24G
drwx------ 2 dwhite dwhite 4,0K nov.  25 10:06 Logs
-rw------- 1 dwhite dwhite 9,7K nov.  20 21:43 srvoel099.vbox
-rw------- 1 dwhite dwhite 9,9K nov.  20 21:43 srvoel099.vbox-prev
-rw------- 1 dwhite dwhite  24G nov.  25 10:16 srvoel099.vdi


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:

VBoxManage modifyvdi srvoel099.vdi --compact

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

[root@srvoel099 ~]# rpm -ivh /media/sf_orasource/zerofree-1.0.1-5.el5.x86_64.rpm
attention: /media/sf_orasource/zerofree-1.0.1-5.el5.x86_64.rpm: Entête V3 DSA/SHA1 Signature, key ID 217521f6: NOKEY
Préparation... ########################################### [100%]
 1:zerofree ########################################### [100%]
[root@srvoel099 ~]# cat /etc/oracle-release
Oracle Linux Server release 6.5

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…
[root@srvoel001 ~]# mount -r -o remount /u01
[root@srvoel099 ~]# mount
/dev/mapper/vg_srvoel099-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg_srvoel099-lv_home on /home type ext4 (rw)
/dev/mapper/vg_srvoel099-lv_ora on /u01 type ext4 (ro)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
oracleasmfs on /dev/oracleasm type oracleasmfs (rw)
orasource on /media/sf_orasource type vboxsf (gid=492,rw)

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

[root@srvoel099 ~]# zerofree -v /dev/mapper/vg_srvoel099-lv_ora
 6.2%

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):

dwhite@squirt:/vm/srvoel001$ VBoxManage modifyvdi srvoel099.vdi --compact
0%...10%...20%...30%...40%...

Now look at the size of the file:

dwhite@squirt:/vm/srvoel099$ ls -lh
total 14G
drwx------ 2 dwhite dwhite 4,0K nov.  25 10:06 Logs
-rw------- 1 dwhite dwhite 9,7K nov.  25 10:21 srvoel099.vbox
-rw------- 1 dwhite dwhite 9,7K nov.  25 10:21 srvoel099.vbox-prev
-rw------- 1 dwhite dwhite  14G nov.  25 10:31 srvoel099.vdi

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:

VBoxManage.exe modifyvdi --compact "D:\vm\srvoel099\srvoel099.vdi"

1 réflexion sur “How to shrink your Oracle Virtualbox virtual machines”

Laisser un commentaire

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