Linux 文件系统扩展属性 xattr

2020-12-30 14:35:32 浏览数 (1)

最近需要基于linux文件系统的扩展属性,做一些自定义的操作;在这里对调研过程进行简要记录;我们常见的很多服务如glusterfs 等,都是使用文件扩展属性做一些定制化的操作;

扩展属性(xattrs)提供了一种机制,用来将键值对永久得关联到文件;让现有的文件系统得以支持在原始设计中未提供的功能。扩展属性是目前流行的POSIX 文件系统具有的一项特殊的功能,可以给文件,文件夹添加额外的Key-value的键值对,键和值都是字符串并且有一定长度的限制。扩展属性需要底层文件系统的支持,在使用扩展属性的时候,需要查看文件系统说明文章,看此文件系统是否支持扩展属性,以及对扩展属性命名空间等相关的支持。包括btrfs、ext2、ext3、ext4、JFS、Reiserfs,Lustrefs以及XFS等文件系统都支持EA。而各类文件系统对于扩展属性的支持都是可选项。

常用的命令:setfattr, getfattr, attr; 关于命令的详细使用可以参考man-pages进行发现;

代码语言:javascript复制
apt list attr 
代码语言:javascript复制
/.
/usr
/usr/bin
/usr/bin/attr
/usr/bin/getfattr
/usr/bin/setfattr
/usr/share
/usr/share/doc
/usr/share/doc/attr
/usr/share/doc/attr/PORTING
/usr/share/doc/attr/README
/usr/share/doc/attr/copyright
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/attr.1.gz
/usr/share/man/man1/getfattr.1.gz
/usr/share/man/man1/setfattr.1.gz
/usr/share/man/man5
/usr/share/man/man5/attr.5.gz
/usr/share/doc/attr/changelog.Debian.gz
/usr/share/doc/attr/changelog.gz
代码语言:javascript复制
setfattr -n user.foo -v bar test #设置 
getfattr -n user.foo test # 读取 
setfattr -x user.foo test  # 删除 
attr -lq test # 列举属性,不包含命名空间 
getfattr -d -m ".*"  test # 列举所有属性,包含命名空间 

这里的文件系统扩展属性,不是我们常说的lsattr和chattr中文件操作的属性。这里的扩展属性是完全自定义的。

扩展属性的支持,不同文件系统对其支持程度不同。某些系统对于文件关联的扩展属性的数量和大小还有更为严格的限制。

在xfs之中,扩展属性的names,最长为 256bytes, 由第一个0byte为止;names 可以为ascii 也可以是其他种类的字符集;values 可以为最长64kb任意的二进制数据,方便用户定制;扩展属性可以用来添加给所有种类的xfs inodes,包括:常规文件,目录,符号文件,设备文件等。

xfs文件系统中,存在两个不相交的命名空间,root和user; root命名空间中的属性可以由superuser 设置,对其他用户不可见;user命名空间中的属性,受linux权限机制保护,所以文件所有者可以决定其文件的扩展属性可以被谁看到和被修改;

在ext2,ext3,ext4文件系统上,如果想设置user 与 文件关联,需要在文件系统挂载的时候使用user_xattr 选项:mount –o user_xattr device directory;在ext2/3/4文件系统中,与一文件关联地所有EA命名和EA值地总字节数不会超过单个逻辑磁盘块的大小:1024字节、2048字节或4096字节;

关于扩展属性的操作函数位于:/usr/include/x86_64-linux-gnu/sys/ 

代码语言:javascript复制
#include<sys/xattr.h> // 使用此头文件进行扩展属性的操作;具体的函数细节可以使用时查看相关文档;
代码语言:javascript复制
Extended file attributes are file system features that enable users to associate computer files with metadata not interpreted by the filesystem, whereas regular attributes have a purpose strictly defined by the filesystem (such as permissions or records of creation and modification times). Unlike forks, which can usually be as large as the maximum file size, extended attributes are usually limited in size to a value significantly smaller than the maximum file size. Typical uses include storing the author of a document, the character encoding of a plain-text document, or a checksum, cryptographic hash or digital certificate, and discretionary access control information.
代码语言:javascript复制
In Linux, the ext2, ext3, ext4, JFS, Squashfs, Yaffs2, ReiserFS, Reiser4, XFS, Btrfs, OrangeFS, Lustre, OCFS2 1.6, ZFS, and F2FS filesystems support extended attributes (abbreviated xattr) when enabled in the kernel configuration. Any regular file or directory may have extended attributes consisting of a name and associated data. The name must be a null-terminated string prefixed by a namespace identifier and a dot character. Currently, four namespaces exist: user, trusted, security and system. The user namespace has no restrictions with regard to naming or contents. The system namespace is primarily used by the kernel for access control lists. The security namespace is used by SELinux, for example.

Support for the extended attribute concept from a POSIX.1e draft that had been withdrawn in 1997 was added to Linux around 2002. As of 2016, they are not yet in widespread use by user-space Linux programs, but are used by Beagle, OpenStack Swift, Dropbox, KDE's semantic metadata framework (Baloo), Chromium, Wget and cURL. A set of recommendations for using them is available at freedesktop.org.

The Linux kernel allows extended attribute to have names of up to 255 bytes and values of up to 64KiB, as do XFS and ReiserFS, but ext2/3/4 and btrfs impose much smaller limits, requiring all the attributes (names and values) of one file to fit in one "filesystem block" (usually 4 KiB).

Extended attributes can be accessed and modified using the attr, getfattr and setfattr commands from the attr package on most distributions.

The Linux kernel allows extended attribute to have names of up to 255 bytes and values of up to 64KiB, as do XFS and ReiserFS, but ext2/3/4 and btrfs impose much smaller limits, requiring all the attributes (names and values) of one file to fit in one "filesystem block" (usually 4 KiB).

保持更新,转载请注明出处,更多内容请关注cnblogs.com/xuyaowen; 

参考链接:

https://en.wikipedia.org/wiki/Extended_file_attributes

https://en.wikipedia.org/wiki/Lustre_(file_system)(https://en.wikipedia.org/wiki/Lustre_(file_system))

http://man7.org/linux/man-pages/man7/xattr.7.html

https://cyw3.github.io/YalesonChan/2016/EA.html

扩展文件属性

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/limits.h

0 人点赞