大网数据广告活动
服务器运维问答

Automatically delete files by writing zeros on Linux

· 52CE 我爱测

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…

问题原文

原帖链接 · David Semeria · CC BY-SA 3.0

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

已采纳解答

原帖链接 · ewwhite · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-01-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。