如果我正确阅读了 ext4 文档,从 Linux 3.8 开始,对于非常小的文件,应该可以将数据直接存储在 inode 中。
我期望这样一个文件的大小为 0 块,但事实并非如此。
# creating a small file
printf "abcde" > small_file
# checking size of file in bytes
stat --printf='%s\n' small_file
5
# number of 512-byte blocks used by file
stat --printf='%b\n' small_file
8
Run Code Online (Sandbox Code Playgroud)
我希望这里的最后一个数字是 0。我错过了什么吗?
Ste*_*itt 14
要在 ext4 中启用内联数据,您需要使用e2fsprogs
1.43 或更高版本。对内联数据的支持于 2014 年 3 月添加到Git 存储库中,但仅在 2016 年 5 月发布。
一旦有了它,您就可以mke2fs -O inline_data
在适当的设备上运行以创建具有内联数据支持的新文件系统;这将删除您的所有数据。显然还不可能在现有文件系统上激活内联数据(至少,tune2fs
不支持它)。
现在创建一个小文件,并debugfs
在文件系统上运行。cd
到适当的目录,然后运行stat smallfile
;你会得到类似的东西
Inode: 32770 Type: regular Mode: 0644 Flags: 0x10000000
Generation: 2302340561 Version: 0x00000000:00000001
User: 1000 Group: 1000 Size: 6
File ACL: 0 Directory ACL: 0
Links: 1 Blockcount: 0
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0x553731e9:330badf8 -- Wed Apr 22 07:30:17 2015
atime: 0x553731e9:330badf8 -- Wed Apr 22 07:30:17 2015
mtime: 0x553731e9:330badf8 -- Wed Apr 22 07:30:17 2015
crtime: 0x553731e9:330badf8 -- Wed Apr 22 07:30:17 2015
Size of extra inode fields: 28
Extended attributes:
system.data (0)
Size of inline data: 60
Run Code Online (Sandbox Code Playgroud)
如您所见,数据是内联存储的。这也可以使用df
; 在创建文件之前:
% df -i /mnt/new
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg--large--mirror-inline 65536 12 65524 1% /mnt/new
% df /mnt/new
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg--large--mirror-inline 1032088 1280 978380 1% /mnt/new
Run Code Online (Sandbox Code Playgroud)
创建文件后:
% echo Hello > smallfile
% ls -l
total 1
-rw-r--r-- 1 steve steve 6 Apr 22 07:35 smallfile
% df -i /mnt/new
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg--large--mirror-inline 65536 13 65523 1% /mnt/new
% df /mnt/new
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg--large--mirror-inline 1032088 1280 978380 1% /mnt/new
Run Code Online (Sandbox Code Playgroud)
该文件在那里,它使用一个 inode,但可用的存储空间没有改变。
小智 6
如果您的e2fsprogs
版本太旧,或者文件系统已经创建,您可以使用设置功能标志debugfs
(该标志自 2012 年起受支持,而mke2fs
其他工具在 2014+ 上增加了支持,许多发行版在 2016 年仍然没有发布它们,包括 Ubuntu Xenial)。
为此,请以读写模式打开分区:
debugfs -w /dev/sdxx
Run Code Online (Sandbox Code Playgroud)
然后添加标志:
feature inline_data
Run Code Online (Sandbox Code Playgroud)
(或者feature -inline_data
关闭它,但如果已经有内联文件,这可能是一个非常糟糕的主意!)
但是,请注意,如果您的系统e2fsprogs
很旧,那么您debugfs
将把自己逼入绝境,因为实用程序(包括它自己)在设置标志后将拒绝接触这样的文件系统。
另请注意,当前GRUB
(2.02) 不支持此功能,因此在启动分区上设置它会使系统无法启动。有一个未合并的补丁来添加支持。
在撰写本文时,最多inode_size-128
可以内联文件和目录,因此默认 256 字节 inode 为 128 字节。如果您想要更多内联,您可以使用更大的 inode。