GNU tar how to preserve file immutable attribute set by "chattr +i" in Linux?

I want use tar(GNU tar 1.30) to backup files with immutable attribute set by "chattr +i" in Linux. Command lines below:

mdx@e5450:~$ touch test.txt
mdx@e5450:~$ sudo chattr +i test.txt
mdx@e5450:~$ lsattr test.txt
----i---------e---- test.txt
mdx@e5450:~$ sudo tar --acls --selinux --xattrs --xattrs-include='*' -cpvvzf test.tar.gz test.txt
-rw-r--r--  mdx/mdx           0 2024-03-01 16:30 test.txt
mdx@e5450:~$ mkdir test
mdx@e5450:~$ sudo tar --acls --selinux --xattrs --xattrs-include='*' -xpvvzf test.tar.gz -C test
-rw-r--r--  mdx/mdx           0 2024-03-01 16:30 test.txt
mdx@e5450:~$ lsattr test/test.txt
--------------e---- test/test.txt
mdx@e5450:~$

You see, the immutable attribute is gone after extracting.

I can use bsdtar 3.3.3 archive test.txt, the immutable attribute will be preserved. Detail below:

mdx@e5450:~$ sudo bsdtar -cvvzf test.btar.gz test.txt
a -rw-r--r--  1 mdx    mdx         0 Mar  1 16:30 test.txt
mdx@e5450:~$ mkdir btest
mdx@e5450:~$ sudo bsdtar -xvvzf test.btar.gz -C btest
x -rw-r--r--  0 mdx    mdx         0 Mar  1 16:30 test.txt
mdx@e5450:~$ lsattr btest/test.txt
----i---------e---- btest/test.txt

Is it possible preserve file immutable attribute set by "chattr +i" when using GNU tar in Linux?

Asked By: littlebat

||

I think you’ve found a shortcoming in GNU tar. But it’s not like you think!

The "i" is what is called a "attribute" in Linux file system lingo. Now, you’re correctly saving and restoring extended attributes, a much later addition, but not the old-school attributes.

Sadly, GNU tar has no command line option to enforce these, and doesn’t seem to consistently handle them. The fact that bsdtar indeed does correctly do that indicates there’s a bug or a feature shortcoming. My bet is that it’s simply not implemented, because I couldn’t quickly find an ioctl on FS_IMMUTABLE_FL in the GNU tar source code. Shouldn’t be too much of an addition – do send an email to their mailing list asking whether this is intended!

Answered By: sina bala
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.