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
sda1 without first unmounting
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
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
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  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
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.
sudo mount --bindas I've found out need to be run after
mount something original_location, not beforehand.
- As it is after shadowing mount, directory higher than one shadowed need to be mounted:
sudo mount --bind parent_of_original_location backup_link