Linux 文件系统扩展属性 xattr

摘要:
最近需要基于linux文件系统的扩展属性,做一些自定义的操作;在这里对调研过程进行简要记录;我们常见的很多服务如glusterfs等,都是使用文件扩展属性做一些定制化的操作;扩展属性提供了一种机制,用来将键值对永久得关联到文件;让现有的文件系统得以支持在原始设计中未提供的功能。而各类文件系统对于扩展属性的支持都是可选项。

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

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

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

apt list attr 
Linux 文件系统扩展属性 xattr第1张Linux 文件系统扩展属性 xattr第2张
/.
/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
attr包内容
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/

#include<sys/xattr.h> // 使用此头文件进行扩展属性的操作;具体的函数细节可以使用时查看相关文档
Linux 文件系统扩展属性 xattr第1张Linux 文件系统扩展属性 xattr第2张
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.
关于扩展属性的定义
Linux 文件系统扩展属性 xattr第1张Linux 文件系统扩展属性 xattr第2张
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, forexample.

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 4KiB).

Extended attributes can be accessed and modified using the attr, getfattr and setfattr commands from the attr package on most distributions.
Linux中对扩展属性的支持

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)

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

免责声明:文章转载自《Linux 文件系统扩展属性 xattr》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ios开发设置不同字体keepalived 配置文件参数详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

linux总结

用户登录 root用户 是一个特殊的管理账号,也可以成为超级管理员 root用户对系统有完全控制的权限 对系统的损害会无限大 在工作中,如果没有特殊的必要,尽量不要使用root 普通用户 权限有限 对系统的损害会小 终端 分类 设备终端 物理终端 虚拟终端 ctrl+alt+f[1-6] /dev/tty# 图形终端 /dev/tty7...

linux文件属性软硬链接知识

链接的概念 在linux系统中,链接可分为两种:一种为硬链接,另一种为软链接或符号链接。在默认不带参数的情况下,执行ln命令创建的链接是硬链接。 如果使用ln  -s创建链接则为软链接,前面文件类型为l(字母L)的是软链接。 硬链接:ln  源文件  目标文件 软链接:ln  -s  源文件  目标文件(目标文件不能事先存在) 1.硬链接 硬链接是指通过索...

CentOS 7 利用qemu模拟ARM vexpress A9开发板

听说qemu用于仿真arm很不错,今日就来试了一把。由于刚刚开始,了解的并不多。本文仅仅记录Qemu装载Linux kernel和busybox根文件系统的过程。后续将会深入了解仿真的其他内容。 先上成果图: 1. 安装QEMU QEMU是一套由Fabrice Bellard所编写的以GPL许可证分发源码的模拟处理器,在GNU/Linux平台上使用广泛。...

Linux SO_KEEPALIVE属性,心跳

对于面向连接的TCP socket,在实际应用中通常都要检測对端是否处于连接中,连接port分两种情况: 1、连接正常关闭,调用close() shutdown()连接优雅关闭,send与recv立刻返回错误,select返回SOCK_ERR; 2、连接的对端异常关闭,比方网络断掉,突然断电. 对于另外一种情况,推断连接是否断开的方法有一下几种: 1、自...

查看linux硬盘使用率

df -h df命令是Linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息,命令格式: df -hl 显示格式为:  文件系统 容量 已用 可用 已用% 挂载点  filesystem Size Used Avail Use% Mounted on /dev/hda2 45G 19G 24G 44% / /dev/hda1 4...

linux初级学习笔记七:linux用户管理,密码和组命令详解!(视频序号:04_1)

本节学习的命令:         用户管理命令:useradd,userdel,id,finger,usermod,chsh,chfn,passwd,pwck,         组管理命令:groupadd,groupmod,groupdel,gpasswd,newgrp,chage 本节学习的技能:         用户管理命令         密码管理...