0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

xfsでchattr

Last updated at Posted at 2019-02-20

ファイルシステムのファイル属性を変更するコマンドであるchattrをxfsで試してみる。

XFSとは

Linuxのファイルシステムの一つ。
redhat6/centos6ではext4が推奨ファイルシステムだったが
redhat7/centos7ではxfsが標準のファイルシステムとして採用されている。

参考
[Red Hat Enterprise Linux 7 インストールガイド]
(https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/installation_guide/sect-disk-partitioning-setup-x86)

XFS はスケーラビリティーに優れた高いパフォーマンス性を有するファイルシステムです。(中略)強く推奨されるファイルシステムであり、デフォルトではこのファイルシステムが選択されます。

chattrとは

Linuxファイルシステム(ext2/ext3/ext4)のファイル属性を変更するコマンド。
パーミッション設定とは別に、圧縮属性や変更不可(削除不可)の属性を付与できる。
マニュアルではext2についてだけ記載されているがext3やext4でも利用できるらしい。

[chattrマニュアル]
(https://linuxjm.osdn.jp/html/e2fsprogs/man1/chattr.1.html)

Linux 第 2 拡張ファイルシステム (ext2fs) 上にあるファイルの属性 (attribute) を変更する

環境

AWSのceontos7で確認

検証

ストレージデバイスの状態を確認

dfコマンドでファイルシステムのフォーマットを確認する。
ファイルシステムタイプがxfsになっているのがわかる。
ディスクデバイスは通常は/dev/sdから始まる名称のことが多いが
今回の環境(AWS)では/dev/xvから始まる名称であった。

$df -T
ファイルシス   タイプ   1K-ブロック    使用   使用可 使用% マウント位置
/dev/xvda1     xfs         41931756 1039576 40892180    3% /
devtmpfs       devtmpfs      484356       0   484356    0% /dev
tmpfs          tmpfs         506944       0   506944    0% /dev/shm
tmpfs          tmpfs         506944   25804   481140    6% /run
tmpfs          tmpfs         506944       0   506944    0% /sys/fs/cgroup
tmpfs          tmpfs         101392       0   101392    0% /run/user/1000

chattrの検証

ceontosユーザーで検証する。

$whoami
centos

ホームディレクリにディレクトリとファイルを作成する。

$mkdir -p ~/test/dir
$cd ~/test
$touch test-file
$ls -al
合計 0
drwxrwxr-x. 3 centos centos  34  2月 20 16:11 .
drwx------. 4 centos centos 123  2月 20 16:10 ..
drwxrwxr-x. 2 centos centos   6  2月 20 16:10 dir
-rw-rw-r--. 1 centos centos   0  2月 20 16:11 test-file

chattr属性で変更不可属性をディレクトリとファイルに付与してみる。
しかし自分がオーナーのディレクトリ・ファイルでも属性の変更ができずにエラーとなった。

$chattr +i dir
chattr: 許可されていない操作です while setting flags on dir
$chattr +i test-file
chattr: 許可されていない操作です while setting flags on test-file

マニュアルを確認すると下記のように記載されており、一般のユーザーでは
i属性を付与できないことが分かる。

i属性が設定されているファイルは、変更することができない。
すなわち、削除、名前の変更、このファイルを指すリンクの作成、このファイルに対するデータの書き込みが禁止される。
スーパーユーザーまたはCAP_LINUX_IMMUTABLE ケーパビリティを持つプロセスだけが、この属性を設定・解除できる。

root権限で変更不可属性を付与したところ、問題なく設定ができた。

$sudo chattr +i dir
$sudo chattr +i test-file 

lsattrコマンドでファイルの属性を確認できる。
対象のディレクトリとファイルに変更不可(i)属性が付与された。

$lsattr -a
---------------- ./.
---------------- ./..
----i----------- ./dir
----i----------- ./test-file

変更不可属性が付与されたファイルを削除してみる。
書き込み権限のパーミッションがあっても「許可されていない操作」と
表示されて削除できない。

$rm -rf dir
rm: `dir' を削除できません: 許可されていない操作です
$rm -f test-file 
rm: `test-file' を削除できません: 許可されていない操作です

root権限でも削除できない。

$sudo rm -rf dir
rm: `dir' を削除できません: 許可されていない操作です
$sudo rm -f test-file 
rm: `test-file' を削除できません: 許可されていない操作です

削除だけでなく移動もできない。

$mv dir dir2
mv: `dir' から `dir2' へ移動できません: 許可されていない操作です
$mv test-file test-file2
mv: `test-file' から `test-file2' へ移動できません: 許可されていない操作です

ファイルに対する書き込みもできない。

$echo hello >> test-file
-bash: test-file: 許可がありません

ファイルコピーは問題なく実施できる。コピー後のファイルには属性は引き継がれない。

$cp test-file new-file
$ls -al
合計 0
drwxrwxr-x. 3 centos centos  50  2月 20 16:37 .
drwx------. 4 centos centos 123  2月 20 16:10 ..
drwxrwxr-x. 2 centos centos   6  2月 20 16:10 dir
-rw-rw-r--. 1 centos centos   0  2月 20 16:37 new-file
-rw-rw-r--. 1 centos centos   0  2月 20 16:11 test-file
$lsattr
----i----------- ./dir
----i----------- ./test-file
---------------- ./new-file

変更不可属性のディレクトリの下にディレクトリもファイルも作成できない。

$touch dir/create-file
touch: `dir/create-file' に touch できません: 許可がありません
$mkdir dir/create-dir
mkdir: ディレクトリ `dir/create-dir' を作成できません: 許可がありません

変更不可属性を解除する。

$sudo chattr -i dir test-file
$lsattr -a
---------------- ./.
---------------- ./..
---------------- ./dir
---------------- ./test-file
---------------- ./new-file

変更不可属性が解除されたため、ファイル削除ができるようになった。

$rm -rf dir
$rm -f test-file
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?