1

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?

6
  • Submit a bug report - looking at the man-page it should work, it obviously doesn't for you (what filesystem are you using, btw?) and I just tried on ext4 w/ no luck. Commented Mar 2, 2024 at 3:50
  • You can't unless you wanna write a a script for that and store this info separately, e.g. in a text file. A Linux only, an ext4 only flag with no kernel syscall? Nah, I don't think tar will consider adding support for it. Commented Mar 2, 2024 at 5:36
  • @ArtemS.Tashkinov these kind of attributes are just called "flags" on bsd / Darwin. And them being ext only is a bit of a 1990s problem; by now, most Linux-supported "fully fledged" file systems from xfs (the oldest) to btrfs support things like the immutable flag/attr Commented Mar 2, 2024 at 6:44
  • @ArtemS.Tashkinov and to be quite honest, GNU core utilities support such a wealth of obscure features, I don't think tar supporting important file properties is a "won't even consider" thing. Commented Mar 2, 2024 at 6:45
  • @tink kernel 5.15.*, tested both ext4 and btrfs, same result. Commented Mar 2, 2024 at 7:36

1 Answer 1

1

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!

8
  • 1
    Sadly this is an extended well thought out comment but not an answer. Commented Mar 2, 2024 at 8:05
  • @sina bala Yes. It should be a shortcoming. I post a bug onto gnu-tar: savannah.gnu.org/bugs/index.php?65397 Commented Mar 2, 2024 at 8:15
  • 1
    @ArtemS.Tashkinov Q is: "how do I make GNU tar do what I need?" My answer: "Get it fixed upstream" Commented Mar 2, 2024 at 9:46
  • 1
    Which is already stated in my comment above ;) Commented Mar 2, 2024 at 23:03
  • 1
    I posted this question on bug-tar mailing list, Paul Eggert, one of maintainer of tar project, answered: The attributes we're talking about are inode flags (see "man ioctl_iflags"). There is no tar command line option to preserve attributes set by chattr and no current plan to implement this though it'd be nice to have such a feature. Commented Mar 5, 2024 at 13:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.