Linux 文件系统扩展属性
最近需要基于linux文件系统的扩展属性,做一些自定义的操作;在这里对调研过程进行简要记录;我们常见的很多服务如glusterfs 等,都是使用文件扩展属性做一些定制化的操作;
提供了一种机制,用来将键值对永久得关联到文件;让现有的文件系统得以支持在原始设计中未提供的功能。扩展属性是目前流行的posix 文件系统具有的一项特殊的功能,可以给文件,文件夹添加额外的key-value的键值对,键和值都是字符串并且有一定长度的限制。扩展属性需要底层文件系统的支持,在使用扩展属性的时候,需要查看文件系统说明文章,看此文件系统是否支持扩展属性,以及对扩展属性命名空间等相关的支持。包括btrfs、ext2、ext3、ext4、jfs、reiserfs,lustrefs以及xfs等文件系统都支持ea。而各类文件系统对于扩展属性的支持都是可选项。
常用的命令:setfattr, getfattr, attr; 关于命令的详细使用可以参考man-pages进行发现;
apt list attr
/. /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
setfattr -n user.foo -v bar test #设置 getfattr -n user.foo test # 读取 setfattr -x user.foo test # 删除 attr -lq test # 列举属性,不包含命名空间 getfattr -d -m ".*" test # 列举所有属性,包含命名空间
这里的文件系统扩展属性,不是我们常说的中文件操作的属性。这里的扩展属性是完全自定义的。
扩展属性的支持,不同文件系统对其支持程度不同。某些系统对于文件关联的扩展属性的数量和大小还有更为严格的限制。
在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/
#include<sys/xattr.h> // 使用此头文件进行扩展属性的操作;具体的函数细节可以使用时查看相关文档;
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.
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).
原文地址:https://www.cnblogs.com/xuyaowen/p/linux-xattrs.html
参考链接:
https://en.wikipedia.org/wiki/extended_file_attributes
https://en.wikipedia.org/wiki/lustre_(file_system)
https://cyw3.github.io/yalesonchan/2016/ea.html
上一篇: jqgrid three 树形结构
下一篇: Goland开发工具安装教程