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 already covered 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.

  • 1
    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
    Commented May 11, 2016 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 /. Commented May 11, 2016 at 7:27
  • 1
    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
    Commented May 11, 2016 at 7:41
  • @muru Not if the branch is a separate filesystem rather than a part of the root filesystem. Commented May 11, 2016 at 7:44

3 Answers 3

# unshare --mount  # this opens a sub-shell
# cd /
# umount /mnt

do what thou wilt

# exit  # close the sub-shell

This probably comes too late, but the way I normally do this is:

  • mount the original device again on a different directory
  • the new directory now only contains the original device's folders, and not any submounts.
  • this does not depend on mount order, kernel namespace support, etc!
  • does not require you to touch / move the current mount (which may be used by your services)

Here's how this would work for your root device, for example:

mount /dev/mapper/ubuntu--vg-ubuntu--lv /mnt
cd /mnt
# this folder only contains files from the root device

In your example:

# your actual (untouched) work dirs
mount device1 /mnt/sub
mount device2 /mnt

# access /mnt/sub contents in another dir by remounting it there
mount device1 /mnt/tmp
cd /mnt/tmp
# any operations here apply solely to device1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .