Extended attributes

From LQWiki
Jump to navigation Jump to search

extended attributes are name-value pairs associated with a file or directory on a filesystem. They are sometimes also called EA or xattrs. These attributes are in addition to the common attributes (the permissions) each file/directory has in Linux (and other unixes]. With extended attributes you can add any name-value pair to a file or directory. The name and value can be any string you want (the maximum length of the name string, value contents and the maximum number of attributes on each file/directory is limited by the implementation of the filesystem). This kind of information added to a file/directory is usually called metadata.

An example usage of these attributes is the implementation of POSIX Access Control Lists (ACL). Also an application like Beagle uses extended attributes to specify if the file has already been indexed. It could also be used for things like storing the character encoding of a file and the song information of a music/mp3 file (could theoretically also contain a thumbnail for an image file, but the size of the value is usually too restricted, so this can't be used for what Mac OS X and Windows XP call multiple streams/forks of a file).

Currently, in kernel version 2.6, ext2, ext3, ReiserFS, JFS, XFS and NFS all support extended attributes. It does need the option enabled at compile time. For ext2/ext3 the mount option 'user_xattr' must be used.

Namespaces

The name of the attributes are usually on a period-separated format like 'user.foo', the first part designates a namespace. This is used to prevent clashes between attributes of the same name. The common namespaces that are defined are:

  • The 'user' namespace. This is protected by the normal unix permission settings on the file (so having write access on the file also allows the user to set an extended attribute) and is meant to be used by the user and any application that is run by the user.
  • The 'root' or 'system' namespace, which can only be set with root access. ACL uses this namespace, it stores its access control lists in attributes like 'system.posix_acl_access' and 'system.posix_acl_default'.
  • The 'security' namespace. For example, SELinux uses the 'security.selinux' attribute.

Usage

The command line tools to get and set these attributes are available in a package called 'attr' in most distributions. This package contains the command line tools 'getfattr' and 'setfattr' (and also a command called 'attr', which is similar to the command on IRIX and mainly aimed at people using XFS on IRIX and migrating to Linux).

Example of setting an extended attribute:

$ setfattr -n user.foo -v bar test-file

Example of getting an extended attribute:

$ getfattr -n user.foo test-file

Output will look like:

# file: test-file
user.foo="bar"

Example of removing an attribute:

$ setfattr -x user.foo test-file

External links