最近需要基於linux文件系統的擴展屬性,做一些自定義的操作;在這裡對調研過程進行簡要記錄;我們常見的很多服務如glusterfs 等,都是使用文件擴展屬性做一些定製化的操作; 擴展屬性(xattrs)提供了一種機制,用來將鍵值對永久得關聯到文件;讓現有的文件系統得以支持在原始設計中未提供的功能。擴 ...
最近需要基於linux文件系統的擴展屬性,做一些自定義的操作;在這裡對調研過程進行簡要記錄;我們常見的很多服務如glusterfs 等,都是使用文件擴展屬性做一些定製化的操作;
擴展屬性(xattrs)提供了一種機制,用來將鍵值對永久得關聯到文件;讓現有的文件系統得以支持在原始設計中未提供的功能。擴展屬性是目前流行的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.gzattr包內容
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> // 使用此頭文件進行擴展屬性的操作;具體的函數細節可以使用時查看相關文檔;

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.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