Accessing contents on the underlying mount point path

If I have partition /dev/sda1 which is mounted on root /, and I have partition /dev/sdb1 which is mounted on /var, is there a way I can access original contents of /var on sda1 without first unmounting sdb1?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In Linux, you can use a bind mount to remount part of the file hierarchy
somewhere else. So, for example, you could do:

# mkdir /mnt/bindroot
# mount --bind / /mnt/bindroot

At that point, /mnt/bindroot contain the contents of the root filesystem,
but without other filesystems mounted on the various directories.

# ls /home
user1 lost+found

# ls /mnt/bindroot/home
<whatever was in /home before a filesystem was mounted over it>

For FreeBSD, you can do something similar with nullfs mounts — see mount_nullfs.

Method 2

On Linux, you can do that without a bind-mount and root privileges, by just having a process which either chdir’s to or opens the underlying directory –before being hidden by the mount– and then just lingers on; other processes could then access the directory via /proc/<pid>/cwd or /proc/<pid>/fd/<dir_fd>.

Example with chdir:

# mkdir dir; touch dir/file                   # create a sample dir and file
# (cd dir; while sleep 3600; do :; done) &    # start a bg process with its pwd being dir
[1] 3734
# mount -t tmpfs tmpfs dir                    # mount a tmpfs over dir which will hide its previous content
# ls dir
# ls /proc/3734/cwd                           # you can still access the old dir via /proc/<pid>/cwd 
file

Example with opening the directory:

# mkdir -p dir1; touch dir1/file
# exec 9<dir1
# mount -t tmpfs tmpfs dir1
# ls dir1
# ls /proc/self/fd/9
file

If you want to do that after the fact, with a directory which was already mounted over, you can create a private namespace and unmount that directory inside the namespace.

In this case, you can also access it from outside via /proc/<pid>/root/<path_to_dir>, without having to open it or chdir to it:

mkdir -p dir; touch dir/file
mount -t tmpfs tmpfs dir

unshare -m sh -c 'umount dir; while sleep 1000; do :; done' &
sleep .1
ls "/proc/$!/root/$PWD/dir"  # will show 'file'

You can read more about the extra magic of /proc/<pid>/root in the proc(5) manpage. Notice that you can keep the namespace of a process alive even after the process terminates by bind-mounting its /proc/<pid>/ns/mnt somewhere else; you can then re-enter it with nsenter(1).

Method 3

Note: for those who may miss a comment.

sudo mount --bind original_location backup_link works, locations under oldlocation can be accessed after something is mounted over them and shadows.

But

  1. sudo mount --bind as I’ve found out need to be run after mount something original_location, not beforehand.
  2. As it is after shadowing mount, directory higher than one shadowed need to be mounted: sudo mount --bind parent_of_original_location backup_link


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x