bashfilesystemsembedded-linuxmountchown

How to use 'chown' on a mounted file system


I have a root filesystem, RFS1, that has its own groups and users and is currently being used by the kernel.

I mount another root filesystem, RFS2, which has its own groups and users. How can I run chown on a file/directory under RFS2 to create an owner from within the groups listed under /etc/groups of RFS2?

I am executing the chown while the OS (Linux) is using RFS1.


Solution

  • Since you want to chown RFS2 files to an RFS2 user, you don't need RFS1 for the chown, so the most straightforward way is chroot(1).

    You mount RFS2 e.g. on /mnt/rfs2. Then you chroot into the target /mnt/rfs2 as instructed here.

    chroot creates a new process that is jailed within /mnt/rfs2. When that process refers to /, the OS will translate it as /mnt/rfs2. If the process refers to /hello/world, the OS will tranlsate it as /mnt/rfs2/hello/world.

    If RFS2 has its own users, then it has its own /etc/passwd, and it's fair to assume it has it's own /bin/, /usr/lib/ etc.

    Once chrooted, make sure that your PATH matches where the binaries actually are.

    Within the chroot jail, every command you execute, such as chown, will be searched in the PATH. It will probably be found in /bin/chown, but now that's RFS2's /bin/chown (namely /mnt/rfs2/bin/chown), not RFS1's. Every dynamic library that chown may be using will also come from the RFS2 filesystem.

    When you chown user:group, chown will search the user and group in /etc/passwd and /etc/group, but those are RFS2's passwd and group file.

    Once you're done, you run exit and the jailed shell process that chroot created for you will exit. While all this was happening, the rest of the processes on the machine were still using RFS1.