A mount point /mnt/sub is shadowed by another mount point /mnt. Is it always possible to access the mounted filesystem?

Root access is a given. The system is a reasonably recent Linux.

Example scenario: accessing the branches of an overlay root

The basic sequence of operations is:

mount device1 /mnt/sub
mount device2 /mnt

After this /mnt/sub is a file on device2 (if it exists). The question is how to access files on device1.

Some devices can be mounted twice, so mount device1 /elsewhere would work. But this doesn't work for all devices, in particular not for FUSE filesystems.

This differs from the case where a subdirectory is shadowed by a mount point, but the mount point of the subdirectory is itself visible, and a bind mount can create an unobscured view. In the example above, mount --bind / /elsewhere lets us see the /mnt/sub directory from the root filesystem on /elsewhere/mnt/sub, but this question is about accessing the filesystem on device1.

  • A quick test shows the shadowed directory is accessible using /proc/PID/cwd for a process that was running in it before the shadowing mount. That won't do, I guess? – muru May 11 '16 at 1:06
  • @muru That only works if the directory was the current directory of some process. It doesn't work in general for the scenario that inspired this question, which was accessing a branch of an overlay filesystem that was mounted on /. – Gilles 'SO- stop being evil' May 11 '16 at 7:27
  • Depending on how early it happened, wouldn't init's CWD still be in the old / in that scenario? It wouldn't work in general since there's no way to make sure a process is started in the old mount point, but with /, init is a good candidate, hopefully? – muru May 11 '16 at 7:41
  • @muru Not if the branch is a separate filesystem rather than a part of the root filesystem. – Gilles 'SO- stop being evil' May 11 '16 at 7:44
# unshare --mount  # this opens a sub-shell
# cd /
# umount /mnt

do what thou wilt

# exit  # close the sub-shell

Looks like device1 is completely shadowed, so it makes no difference* whether it is mounted or not. Unmount, then mount it elsewhere to have access to files.

*Well, I guess it makes no difference, unless there are files from device1 still in use since the time it wasn't shadowed yet. Is that the case?

  • device1 can still be in use even though it's shadowed. The most obvious case is indeed that files are still open on it (or even new files get opened because a process has its current directory on it). Another case is the one in the scenario I link to, where device1 is one of the branches in a union mount. – Gilles 'SO- stop being evil' Apr 20 '16 at 18:06

The files living on device1 under /mnt/sub are not accessible through that path at all. Remount the device elsewhere, or make sure that the two devices are mounted in the opposite order.

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.