Most virtualization products support thin provisioned virtual disks, that is vDisks that start small but grow as required by the guest OS.
Now these growable vdisks are great for saving space on the host and overcommitting host storage but over time the guest uses the space for various things and creates and deletes data but the vdisk on the host remains at the grown size, i.e. it doesn't shrink by itself.
VirtualBox and VMware products have commands to "compact" or "shrink" the vdisks.
For VirtualBox:
For VMware Server:
But here is the trick. Even if you run those commands against your vdisks you will likely NOT shrink the vdisk very much at all. That is because the way those utilities identify the "unused" space is by looking for blocks of zero data and when an OS deletes a file from its disk it only removes the entry in the file allocation table but the data actually remains on the disk until it is reused/overwritten and is therefor not "zero" and cannot be reclaimed by the vdisk utilities.
To overcome this I used to run the following script on the guest before running the vdisk utilities.
But the problem with that is that it does actually fill the disk and grows the vdisk to it maximum which means that you must have enough free space on the host to accommodate the full size vdisk before you run the vdisk utilities to compact/shrink it down.
So I recently found a new utility that actually zero fills only unused non-zero blocks. zerofree
zerofree supports ext2, ext3 and ext4 filesystems.
Thankfully this utility has been packaged up by the good people at EPEL for RHEL and CentOS, it is also available in the debian/ubuntu repos.
To install from EPEL
The only catch w/ zerofree is that it must be run against an unmounted or at least read-only volume/disk. For non-root disks this is not a real problem, you can just schedule maintenance, stop your applications/services that are using the volume and umount it before running zerofree against the device.
For example:
But what about root?
You can boot into single user mode or telinit 1 for the same but that will disable the network and your remote access. So I made a little patch that allows zerofree to be run on the root disk during boot.
This patch is against CentOS 5 /etc/rc.d/rc.sysinit in package initscripts-8.45.30-3.el5.centos.
Here is the patch
To apply
Once the patch is applied and zerofree installed you can enable zerofree at boot with
During the next boot zerofree will be run before root is remounted read-write.
Whichever method you choose to zero out unused space within your guest OS you can then reclaim the space on the host.
For example with VirtualBox
This method of zeroing unused space can also be used with SAN solutions that support thin provisioning.
I use DataCore SANmelody thin provisioning HA SAN's and use this method to reclaim SAU's!
Thats it.
Market Astrology
ReplyDelete