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

Is it safe to use fallocate --dig-holes while being modified

· 52CE 我爱测

Is it safe to use fallocate --dig-holes on a file while it is being modified/written? For example on a QCOW2 image opened by a KVM guest?

问题原文

原帖链接 · Isabell Cowan · CC BY-SA 3.0

Is it safe to use fallocate --dig-holes on a file while it is being modified/written? For example on a QCOW2 image opened by a KVM guest?

已采纳解答

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

I'd generally say no. If the file is an image mounted to a VM, you can try TRIM (e.g., fstrim -a) instead. Strictly speaking, this is not an equivalent (TRIM can also free space of some deleted file that is not zeroed), but this might be what you want.

It might be safe in some specific cases, but I wouldn't rely on it. For example, when some data are sequentially streamed to a file without any preallocation, it sounds potentially safe. But since this is not implied by documentation and it would rely only on implementation, I strongly don't recommend it.

What can go wrong? Imagine the app/VM is going to overwrite some zeroed block and you concurrently run fallocate -d. The race can look like:

  1. Fallocate sees a block of zeroes, so it decides to dig a hole there.
  2. Another application/VM writes to the zeroed block some data.
  3. Fallocate digs a hole in the block.

It might sound like there is a small timeframe between 1 and 3. Maybe it is true, but fallocate might want to free multiple subsequent blocks at once. (Not sure, I haven't checked the implementation, but even if the opposite is true, you cannot rely on it in the future.) This can increase the delay between 1 and 3, making the race condition much more likely.

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

资料来源与适用范围

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

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

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