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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...