2

I want to capture everything in a chroot but am unsure whether I need to first chroot into it to see the full filesystem/preserver permissions etc. From outside the chroot, it seems like I would miss the root's home directory /root. What is the way to fully capture a chroot, much like you could fully capture a VM?:

cd ~/mychroot
# Un-mount any previously mounted filesystems first?
# umount /proc
tar -cpzf /tmp/mychroot.tar.gz --one-file-system --exclude=/tmp --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev /    cp ~/mychroot/mychoort.tar.gz /path/to/somewhere/safe

sudo chroot ~/mychroot
# Un-mount any previously mounted filesystems first?
tar -cpzf /tmp/mychroot.tar.gz --one-file-system --exclude=/tmp --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev /    # Exit the chroot
exit
cp ~/mychroot/mychroot.tar.gz /path/to/somewhere/safe
1
  • is the answer anything other than sudo tar -c mychroot -f mychroot.tar ? Aug 22 '18 at 19:49
2

You don't have to chroot to tar things. You can use the --numeric-owner option instead. Chroot is only necessary if you also want to record the (correct) owner/group names inside the tar archive, which is not strictly necessary in this case [when /etc/passwd etc. is part of the tar].

And without actually chrooting you also don't need to mount /dev, /proc, /sys etc. in the first place, so that's one headache removed.

The --one-file-system option is a false friend. It will simply give you an incomplete tar, since it skips any and all mountpoints. For example, some systems have static device nodes in /dev even before udev gets mounted there; they would be gone and as a result your system might not boot.

So don't ever use --one-file-system unless an incomplete result absolutely is what you want.

An alternative is bind mounting, for example for the root partition:

mkdir /mnt/root
mount --bind / /mnt/root

The bind mount does not carry submounts along with it so /mnt/root gives you the full picture of your root filesystem, including static nodes in /dev/ and other things, if present. You can tar it in its entirety, without all the hassle of --one-file-system and dozens of --exclude options.

12
  • Just a note: these options are not sopported by tar, they are gtar specific.
    – schily
    Aug 24 '15 at 15:51
  • @schily, they are supported by bsdtar as well. Actually, you'll probably want bsdtar anyway to be able to store extended attributes, sparseness (with -S). Note that --numeric-owner is more needed at extraction time. It's fine to omit it at creation time. Can star store extended attributes, ext2 file attributes, sparseness and things like that? Aug 24 '15 at 16:03
  • @schily, sorry -S is actually to make sparse files when extracting archive members that are not necessarily sparse. bsdtar does attempt to store and restore sparseness when possible by default. Aug 24 '15 at 16:11
  • @schily, looks like star would do as well here with -sparse -xattr -xfflags. Aug 24 '15 at 16:24
  • I always assume GNU/Linux, unless the question gives reason not to. Aug 24 '15 at 16:44
0

Check out what chroot(2) is all about (user command chroot(1) is just a thin wrapper around the system call): It changes where the affected process sees /, so it can't (easily) escape/access files outside the targeted directory.

The "chroot" directory is just a bog-standard directory (probably set up with special contents by your friendly system administrator), so yo can manipulate it just like anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.