Reclaim Thin Provisioned Disk Space on Ubuntu Linux Virtual Machine

Reclaiming disk space in VMware can be quite helpful when you are oversubscribing lab data stores. I had a Cacti database that had grown quite large and after exporting the data, I wanted to reclaim the disk space.

This process for Linux will write zeros to a file until there is no more free space using the dd command. Be mindful if you are using this on a partition that may include active applications (i.e. logging or mySQL servers).

The command that is used in this example is:

sudo dd if=/dev/zero of=/var/lib/zerofill.tmp & rm /var/lib/zerofill.tmp

To break out the usage:

  • sudo: Enables root impersonation
  • dd: The appliction
  • if=/dev/zero: Input device for dd command
  • of=/var/lib/zerofill.tmp: Output device, partition, or in this case, a file on a specific mounted partition.
  • &: Command delimiter
  • rm /var/lib/zerofill.tmp: Remove file after dd completes

Below is the full output while reclaiming space on my Cacti installation.  This did take some time to write zeros to the file and it appears my sudo access timer expired.  I simply had to re-enter the last part of the command to remove the file.

Last but not least, migrate the VM to a different data store selecting “Thin Provision” for the disk format.

localadmin@Cacti:~$ 
localadmin@Cacti:~$ sudo dd if=/dev/zero of=/var/lib/zerofill.tmp & rm /var/lib/zerofill.tmp
[2] 14363
rm: cannot remove `/var/lib/zerofill.tmp': No such file or directory
localadmin@Cacti:~$ sudo dd if=/dev/zero of=/var/lib/zerofill.tmp ; sync ; sleep 1 ; sync ; rm /var/lib/zerofill.tmp
dd: writing to `/var/lib/zerofill.tmp': No space left on device
176048977+0 records in
176048976+0 records out
90137075712 bytes (90 GB) copied, 5608.94 s, 16.1 MB/s

^C111085185+0 records in
111085185+0 records out
56875614720 bytes (57 GB) copied, 8736.41 s, 6.5 MB/s
[2]-  Exit 1                  sudo dd if=/dev/zero of=/var/lib/zerofill.tmp
rm: remove write-protected regular file `/var/lib/zerofill.tmp'? 
localadmin@Cacti:~$ 
localadmin@Cacti:~$ sudo rm /var/lib/zerofill.tmp 
[sudo] password for localadmin: 
localadmin@Cacti:~$

Leave a Reply

Your email address will not be published. Required fields are marked *