Linux 文件系統擴展屬性

来源:https://www.cnblogs.com/xuyaowen/archive/2020/04/11/linux-xattrs.html
-Advertisement-
Play Games

最近需要基於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.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> // 使用此頭文件進行擴展屬性的操作;具體的函數細節可以使用時查看相關文檔
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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在owasp年度top 10 安全問題中,註入高居榜首。SQL註入攻擊指的是通過構建特殊的輸入作為參數傳入Web應用程式, 而這些輸入大都是SQL語法里的一些組合,通過執行SQL語句進而執行攻擊者所要的操作,其主要原因是程式沒有細緻地 過濾用戶輸入的數據,致使非法數據侵入系統。 1. 對於Web應用 ...
  • 1.XSS簡介 2.構造XSS腳本 常用HTML標簽 上面這些代碼下麵會有實驗來一一解釋,反射型xss和存儲型xss都能用。 3.反射型XSS 簡單來說反射型就是臨時的xss,只有別人把該鏈接發給你,你點開之後才會中圈套。但是你不點開,正常的去訪問該網站是沒什麼問題的。 這裡我們用kali當做被下套 ...
  • CentOS 7 LAMP搭建並且部署sqli labs 一、LAMP簡介(摘自百度百科) 1.1.簡介 LAMP是指一組通常一起使用來運行動態網站或者伺服器的自由軟體名稱首字母縮寫: L inux,操作系統 A pache,網頁伺服器 M ariaDB或MySQL,資料庫管理系統(或者資料庫伺服器 ...
  • df -Th 保持更新,更多內容請關註cnblogs.com/xuyaowen; http://man7.org/linux/man-pages/man1/df.1.html df - report file system disk space usage ...
  • 背景 By 魯迅 By 高爾基 說明: 1. Kernel版本:4.14 2. ARM64處理器,Contex A53,雙核 3. 使用工具:Source Insight 3.5, Visio 1. 概述 ,是Linux內核中的一種同步機制。 常被描述為讀寫鎖的替代品,它的特點是讀者並不需要直接與寫 ...
  • 引言: 在做練習項目的時候,需要使用Elasticsearch做搜索引擎。因為使用的是阿裡雲的伺服器,為了更好的管理所以使用了Docker來安裝Elasticsearch。但是本著安全性考慮,需要給Elasticsearch做安全驗證。這時後難題來了,Elasticsearch預設的是不開啟安全驗證 ...
  • 1. 不允許多個Host請求頭 2. 忽略額外的Transfer-Encoding請求頭 3.修複在HTTP/2時的socket泄露 4.修複使用OCSP時,工作進程中可能會發生分段錯誤 5.更改使用“ error_page”指令重定向了494時,把狀態碼400換成494 6.修複在njs模塊和使用 ...
  • 利用Docker搭建主從伺服器 首先拉取docker鏡像,我們這裡使用5.7版本的mysql: 然後使用此鏡像啟動容器,這裡需要分別啟動主從兩個容器 Master(主): Slave(從): Master對外映射的埠是3339,Slave對外映射的埠是3340。因為docker容器是相互獨立的, ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...