朕喜技术(ZhenxiJS)
2026.08.25
1 分钟阅读

No space left on device 但磁盘有空间 —— inode 耗尽排查

关键词:Linux、磁盘满、No space left on device、inode、df -h、df -i、文件系统、运维排障、ext4

一、问题背景

写文件报 No space left on device,可 df -h 显示磁盘还有大量可用空间。 这是运维经典"灵异"现象,真凶通常是 inode 耗尽。

二、复现步骤

cd scenario/2026-08-25/
docker compose up -d --build
docker compose exec app bash
bash /workspace/scenario/2026-08-25/fill_inodes.sh

脚本用 mkfs.ext4 -N 500 格式化出仅 500 个 inode 的小文件系统,循环创建空文件耗尽 inode。

复现结果:

$ df -h /mnt/smallfs          # 还有空间
/dev/loop0  19M  13M  5.5M  71% /mnt/smallfs

$ df -i /mnt/smallfs          # inode 已 100%
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/loop0       500   500     0  100% /mnt/smallfs

$ touch /mnt/smallfs/new_file
touch: cannot touch '/mnt/smallfs/new_file': No space left on device

三、根本原因

文件系统分数据块和 inode 两部分。每个文件占 1 个 inode(存元数据), mkfs.ext4 -N 500 固定了 inode 上限约 500。inode 用尽后无法再建新文件, 即使磁盘块还有剩余。df -h 只看块,df -i 看 inode —— 两个维度独立。

四、解决办法

清理无用小文件释放 inode(治标):

find /mnt/smallfs -xdev -type f -delete
df -i /mnt/smallfs          # inode 使用率下降
touch /mnt/smallfs/new_file # 现在能写

规划更大 inode 数 / 高密度 inode(治本):

mkfs.ext4 -N 5000 /tmp/smallfs.img     # 明确指定 inode 数
mkfs.ext4 -T small /tmp/smallfs.img    # 小文件高密度 inode

五、验证

bash /workspace/scenario/2026-08-25/check.sh
# inode 使用率下降,写入成功

六、经验与避坑

  1. df -h 有空间 ≠ 能写文件,必须同时看 df -i。
  2. 0 字节文件也占 inode,大量小文件最易耗尽。
  3. 排障顺序:df -h → df -i → find ... | wc -l。
  4. 日志/临时文件堆积是 inode 耗尽高发场景,建议监控 df -i + 定时清理。
  5. inode 在格式化时决定,事后难调整,创建文件系统就要规划好。

版权声明

本文为博主原创文章,遵循 CC BY-NC-SA 4.0 许可协议。 转载请注明出处:http://localhost/blogs/2026-08-25-no-space-left-but-disk-not-full

分享这篇文章