注:本文为 “ ext4 文件系统索引节点” 相关讨论合辑。
英文引文,机翻未校。
如有内容异常,请看原文。
How can I increase the number of inodes in an ext4 filesystem?
如何增加 ext4 文件系统的索引节点数量?
dfshowed 50% available space. After searching for about an hour I saw a forum post where the guy useddf -i. The option looks for inodes usage. The system was out of inodes, a simple problem that I didn’t realize. The partition had only 3.2M inodes.df显示空间剩余 50%。一小时后我在论坛帖子中看到df -i命令,该命令用于查看索引节点使用情况,最终确认是索引节点耗尽,该分区仅分配 320 万索引节点。
Now, my questions are: Can I make the system have more inodes? Should/can it be set when formatting the disk? With the 3.2M inodes, how many files could I have?
我的问题如下:能否为系统增加索引节点数量?是否可在磁盘格式化时设置该值?320 万索引节点最多可存储多少文件?
Every file or directory uses one inode. A hard link to a file does not create an inode.
每个文件或目录占用一个索引节点,文件的硬链接不创建新索引节点。
Answers
It seems that you have a lot more files than normal expectation.
你的文件数量明显超出常规预期。
I don’t know whether there is a solution to change the inode table size dynamically. I’m afraid that you need to back-up your data, and create new filesystem, and restore your data.
目前无法动态调整索引节点表大小,需备份数据,重新创建文件系统后恢复数据。
To create new filesystem with such a huge inode table, you need to use ‘-N’ option of mke2fs(8).
创建大容量索引节点表的文件系统,需使用mke2fs的-N参数。
I’d recommend to use ‘-n’ option first (which does not create the fs, but display the useful information) so that you could get the estimated number of inodes. Then if you need to, use ‘-N’ to create your filesystem with a specific inode numbers.
建议先使用-n参数预览配置,该参数不实际创建文件系统,仅显示预估索引节点数量;之后按需使用-N指定索引节点数量创建文件系统。
You can usemke2fs -ito specify the number of inodes. Its documentation indicates that “it is not possible to expand the number of inodes on a filesystem after it is created”.
可使用mke2fs -i设置索引节点相关参数,文档说明文件系统创建后无法扩展索引节点数量。
@Gilles The-ioption specifies the size of the inode, not how many there are. The-Noption sets the number of inodes.-i用于设置索引节点大小,-N用于设置索引节点数量。
With 3.2 million inodes, you can have 3.2 million files and directories, total (but multiple hardlinks to a file only use one inode).
320 万索引节点最多可存储 320 万个文件与目录,同一文件的多个硬链接共用一个索引节点。
Yes, it can be set when creating a filesystem on the partition. The options-T usage-type,-N number-of-inodes, or-i bytes-per-inodecan all set the number of inodes.
索引节点数量可在分区创建文件系统时设置,参数包括-T、-N与-i。
No, it can’t be changed in-place on an existing filesystem. However:
索引节点数量无法在现有文件系统上在线修改,但存在如下替代方案:
- If you’re running LVM or the filesystem is on a SAN’s LUN, or you have empty space on the disk after the partition, you can grow the partition and then use
resize2fsto expand the filesystem. This adds more inodes in proportion to the added space.
若使用 LVM、SAN 存储分区,或分区后存在剩余磁盘空间,可扩展分区并使用resize2fs扩容文件系统,索引节点会按空间比例增加。 - If you have enough space and can take the filesystem offline, then take it offline, create a new filesystem with more inodes, and copy all the files over.
若空间充足且可离线操作,卸载文件系统后重新创建并迁移数据。 - If just a subset of the files are using a lot of the inodes and you have enough free space, create a filesystem on a loop device backed by a file on the filesystem, create a filesystem with more inodes on it, and move the corresponding directories into it.
若仅部分文件占用大量索引节点且空间充足,可在循环设备上创建新文件系统并分配更多索引节点,迁移对应目录。 - And of course, if you can delete a lot of unneeded files, that should help too.
清理大量无用文件可直接释放索引节点。
As another workaround I could suggest considering packing huge collections of files into an uncompressedtararchive, and then usingarchivemountto mount it as a filesystem.
另一种方案是将大量文件打包为无压缩 tar 归档,通过archivemount挂载为文件系统。
If the collection is supposed to be read-only,squashfsmay be an option.
若为只读场景,可使用squashfs。
I have an alternate solution for this situation.
我有一个替代方案。
# Check inode usagedf-i# Create an empty image fileddif=/dev/zeroof=/data/new_data# Create ext4 filesystem on the imagemkfs.ext4 /data/new_data# Create mount pointmkdir/data1# Mount the new filesystemmount/data/new_data /data1For permanent mounting
永久挂载配置:
/data/new_data /data1 ext4 defaults 0 1Trydu -s --inodes * 2>/dev/null | sort -gthen cd into the last directory in output and repeat.
尝试执行du -s --inodes * 2>/dev/null | sort -g,进入输出末尾目录后重复该操作。
Full Disclosure: not all OS’s support--inodesflag for du command but many Linux OS’s do.
说明:并非所有系统的du都支持--inodes参数,但多数 Linux 发行版支持。
Recently ran into this issue when using apt or aptitude upgrade.
我在使用apt或aptitude升级时遇到该问题。
df-hdf-iIssued command:
执行命令:
du/|sort-k1-nRevealed most of files were in subfolders for several kernel versions within/usr/src/linux-headers.
发现大量文件位于/usr/src/linux-headers内核头文件目录。
Removed those subfolders and inode problem was fixed.
删除该目录后索引节点问题解决。
How to store one billion files on ext4?
如何在 ext4 文件系统上存储 10 亿个文件?
I only created about 8 million files, then there was no free inode in /dev/sdb1.
我仅创建了约 800 万个文件,/dev/sdb1分区的索引节点就已耗尽。
df -iSomeone says can specify the inode count when format the partition, e.g.mkfs.ext4 -N 1000000000.
有人说可以在分区格式化时通过mkfs.ext4 -N 1000000000指定索引节点数量。
I tried but got an error: “inode_size (256) * inodes_count (1000000000) too big…specify higher inode_ratio (-i) or lower inode count (-N).”
我尝试后出现错误:索引节点大小(256)× 索引节点数量(1000000000)超出限制,请设置更大的索引节点比率(-i)或更小的索引节点数量(-N)。
What’s the appropriate inode_ratio value? I heard the min inode_ratio value is 1024 for ext4.
合适的索引节点比率是多少?ext4 的最小索引节点比率为 1024。
Is it possible to store one billion files on a single partition? How to? And someone says it will be very slow.
在单个分区上存储 10 亿个文件是否可行?如何实现?有人说这样操作会非常慢。
The smallest file system block size allowed is usually 1024 bytes. The smallest bytes per inode you can specify is as small as the file system block size. With file system block size at 1k and bytes per inode at 1k, a 1 TB disk will almost have 1 billion inodes.
文件系统允许的最小块大小通常为 1024 字节。可设置的最小每索引节点字节数与文件系统块大小一致。块大小与每索引节点字节数均为 1 KB 时,1 TB 磁盘可分配近 10 亿个索引节点。
However, avoid having many million files in the same directory, use a hierarchical naming scheme.
避免在单个目录中存放数百万个文件,可使用分级目录命名方案。
Answer
When creating an ext4 file system, you can specify the usage type:
创建 ext4 文件系统时,可以指定使用类型:
mkfs.ext4-Tusage-type /dev/somethingThe available usage types are listed in /etc/mke2fs.conf. The main difference between usage types is the inode ratio. The lower the inode ratio, the more files can be created.
可用的使用类型在/etc/mke2fs.conf中定义,不同类型的区别为索引节点比率,该值越小可创建的文件越多。
The usage type which allocates the highest number of inodes is “news”. With this type on a 1 TB drive, ext4 creates 244 million inodes.
分配索引节点最多的类型为news,1 TB 硬盘可创建 2.44 亿个索引节点。
This means that it would require more than 4 TB to create an ext4 file system with-T newsthat can hold 1 billion inodes.
使用-T news创建支持 10 亿索引节点的 ext4 文件系统,分区容量需超过 4 TB。
Will it be slow if creating so many inodes?
创建如此多的索引节点会导致性能变慢吗?
Yes, it will take a very long time. Creating 1 billion files probably requires days or weeks.
会的,耗时极长,创建 10 亿个文件可能需要数天至数周。
ext4 文件系统索引节点(inode)
1 索引节点基本特性
索引节点是 ext4 文件系统中用于存储文件元数据的结构,每个文件或目录均占用一个索引节点;同一文件的多个硬链接共享同一个索引节点,不额外消耗索引节点资源。
索引节点占用量仅与文件/目录数量相关,与文件大小无关。3.2×10⁶ 个索引节点最多可对应存储 3.2×10⁶ 个文件与目录。
2 索引节点数量的设置规则
ext4 文件系统的索引节点总数在文件系统创建(格式化)阶段确定,创建完成后无法在线动态调整。若需修改索引节点数量,必须备份数据,重新创建文件系统并恢复数据。
2.1 格式化时配置索引节点的常用参数
使用mke2fs/mkfs.ext4格式化时,可通过以下参数控制索引节点数量:
-N 总数:直接指定索引节点总数,适用于需要精确控制的场景。-i 字节数:设定每多少字节分配一个索引节点,字节数越小,单位空间分配的索引节点越多。-T 类型:加载/etc/mke2fs.conf中的预设场景模板,不同模板对应不同索引节点分配比例。-n:仅预览配置信息,不实际创建文件系统,用于提前校验参数合理性。
示例命令:
mke2fs-n/dev/sdXX mkfs.ext4-N10000000/dev/sdXX2.2 索引节点分配与容量限制
ext4 允许的最小块大小为 1024 字节,可设置的最小每索引节点字节数与文件系统块大小一致,低于块大小会产生无法使用的冗余索引节点。
- 块大小 1KB 且每索引节点字节数 1KB 时,1TB 磁盘可分配接近 10 亿个索引节点。
- 使用
-T news模板时,1TB 磁盘可创建约 2.44×10⁸ 个索引节点。 - 要在 ext4 上实现 10 亿个索引节点,分区容量需要大于 4TB。
直接使用-N 1000000000常会出现容量超限报错,提示需要提高索引节点比率或降低节点数量。
3 inode 耗尽的典型现象与排查
在 Fedora 等 Linux 环境中,可能出现df显示磁盘空间剩余充足(如 50%),但应用异常终止、无法新建文件的情况。
典型报错:
bash: cannot create temp file for here-document: No space left on device排查命令:
df-idu-s--inodes*2>/dev/null|sort-g常见高占用目录:旧内核头文件目录/usr/src/linux-headers、系统日志、临时缓存目录、海量小文件业务目录。
4 不重新格式化的应急处理方案
文件系统扩容
分区基于 LVM 或后部存在未分配空间时,可扩展分区并使用resize2fs扩容,索引节点会随空间按比例增加。虚拟磁盘文件挂载
在现有分区创建镜像文件,格式化为 ext4 并挂载,获得独立索引节点资源。df-iddif=/dev/zeroof=/data/new_data mkfs.ext4 /data/new_datamkdir/data1mount/data/new_data /data1永久挂载写入
/etc/fstab:/data/new_data /data1 ext4 defaults 0 1文件归档挂载
大量小文件打包为无压缩 tar 并通过archivemount挂载;只读场景可使用squashfs,减少索引节点占用。冗余文件清理
定位并删除旧内核、过期日志、临时缓存等无用文件,直接释放索引节点。
5 海量文件存储的性能与规范
- ext4 对超大规模文件数量支持较弱,单目录创建 1×10⁴ 个文件约需 14 秒,1×10⁵ 个文件需超过 2 分钟,10 亿级文件创建耗时可达数天至数周。
- 避免在单个目录存放大量文件,宜采用多级目录结构(如
/data/012/345/file.txt),单目录文件数量建议控制在数千级别。 - 海量小文件场景优先选用 Btrfs、XFS 等无固定索引节点上限的文件系统。
reference
- files - How can I increase the number of inodes in an ext4 filesystem? - 2011
https://unix.stackexchange.com/questions/26598/how-can-i-increase-the-number-of-inodes-in-an-ext4-filesystem - linux - How to store one billion files on ext4? - 2014
https://stackoverflow.com/questions/21397110/how-to-store-one-billion-files-on-ext4