Difference between xattr and chattr
The attributes as handled by lsattr
/chattr
on Linux and some of which can be stored by quite a few file systems (ext2/3/4, reiserfs, JFS, OCFS2, btrfs, XFS, nilfs2, hfsplus...) and even queried over CIFS/SMB (when with POSIX extensions) are flags. Just bits than can be turned on or off to disable or enable an attribute (like immutable or archive...). How they are stored is file system specific, but generally as a 16/32/64 bit record in the inode.
The full list of flags is found on Linux native filesystems (ext2/3/4, btrfs...) though not all of the flags apply to all of FS, and for other non-native FS, Linux tries to map them to equivalent features in the corresponding file system. For instance the simmutable flag as stored by OSX on HFS+ file systems is mapped to the corresponding immutable flag in Linux chattr
. What flag is supported by what file system is hardly documented at all. Often, reading the kernel source code is the only option.
Extended attributes on the other hand, as set with setfattr
or attr
on Linux store more than flags. They are attached to a file as well, and are key/value pairs that can be (both key and value) arbitrary arrays of bytes (though with limitation of size on some file systems).
The key can be for instance: system.posix_acl_access
or user.rsync.%stat
. The system
namespace is reserved for the system (you wouldn't change the POSIX ACLs with setfattr
, but more with setfacl
, POSIX ACLs just happen to be stored as extended attributes at least on some file systems), while the user
namespace can be used by applications (here rsync
uses it for its --fake-super
option, to store information about ownership or permissions when you're not superuser).
Again, how they are stored is filesystem specific. See WikiPedia for more information.
Cerberuzs
Updated on September 18, 2022Comments
-
Cerberuzs almost 2 years
What is the relation and the difference between
xattr
andchattr
? I want to know when I set achattr
attribute in Linux what is happening inside the Linux kernel and inode metadata.-
Graeme over 10 yearsI wrote a long answer on a related question yesterday which might be helpful - unix.stackexchange.com/questions/118840/…. Note that the
attr
command is intended for XFS filesystems, the equivalent filesystem independent command issetfattr
-
-
Graeme over 10 yearsDo
lsattr
/chattr
work for nonext
filesystems? If not how would you change the flags on nonext
systems on Linux? Also theattr
man page says that it is written for XFS (even though it works for me onext4
). I actually spent a long time looking into this yesterday, good information is hard to come by. Please see - unix.stackexchange.com/questions/118840/… -
Stéphane Chazelas over 10 years@Graeme, answers to those questions are in my answer.
lsattr/chattr
are for file flags. Not all flags are supported by all filesystems, for instance, some of them like compression won't work on ext4 but will work on btrfs. -
Graeme over 10 yearsIf you are interested, I wrote a script to test these flags on a few filesystems and put the results on my answer linked above. I had no success setting/reading any of them on
reiserfs
.