6

We run thin-provisioned Ubuntu VMs which do a lot of file writes and deletes. Whilst the disk use on the guests increases slowly (net writes - deletes) the size of the virtual disk on the hosts grows much more quickly because the hosts have no way of knowing that many of the blocks are freed-up space.

On a regular basis we take each VM offline and write zeros to the free blocks in the guest filesystem. This allows us to then to shrink the virtual disk on the host.

I was wondering if anybody knows of a utility we can install on the guests which would automatically write zeros every time a file is deleted. If such a utility existed we could then automate the shrinking of the virtual disks on the host without manually writing zeros over deleted files in the guest.

I hope the question is clear and thanks in advance for any help

David Semeria
  • 219
  • 2
  • 8

2 Answers2

4

Use the fstrim command.

I do this for VMs and other thin-provisioned filesystems, ZFS zvols and SSD-based storage with an easy cron script:

#!/bin/bash

for fs in $(lsblk -o MOUNTPOINT,DISC-MAX,FSTYPE | grep -E '^/.* [1-9]+.* ' | awk '{print $1}')
  do fstrim $fs > /dev/null 2>&1
done
ewwhite
  • 194,921
  • 91
  • 434
  • 799
  • Thanks. Didn't know about fstrim, we use zerofree. Can fstrim work on mounted file systems? Zerofree requires they be unmounted, which greatly complicates automation via scripting. – David Semeria Jan 16 '15 at 21:23
  • Sorry @ewwhite, I asked my question before clicking on the fstrim link. It says right at the top that the filesystem can be mounted! – David Semeria Jan 16 '15 at 21:33
  • I too didn't know about this, but watch out for the warning on the man page-- lots of writes will decrease an SSD's lifespan. – Chris Mendez Jan 16 '15 at 21:52
  • I did a bit more research. Am I right in thinking that by punching zeros I can shrink a TP logical disk down to the actual space used on the guest, whereas with TRIM the logical disk will only grow when there are no free blocks available (because with TRIM the logical disk knows which blocks are free on the guest) ? – David Semeria Jan 17 '15 at 17:04
1

I don't know of any program, utility, or service that would automatically zero out deleted files. It would need to hook into the system calls or run within the kernel to achieve the automaticness that you're looking for.

There are utilities that delete files by zeroing them out first. You didn't say what program is making and deleting files, but maybe if it's a script or something you could hook into these utilities?

Otherwise, no, not an easy and automatic way of doing this. That's the downside of thin-provisioning.

Chris Mendez
  • 166
  • 6
  • Yup, I imagine such a utility would have to hook very deep into the OS, if not the kernel itself. We use zerofree to punch the zeros, but the disk has to be unmounted... – David Semeria Jan 16 '15 at 21:25