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
?
3 Answers
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
.
-
right, "At that point" but after mount somewhere over / --bind can get cancelled, then just run it again ;-) Dec 1, 2021 at 13:25
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)
.
-
1In the context of the question, this would involve time travel, wouldn’t it? Oct 6, 2021 at 21:37
-
If that means that my answer is worthless and I should remove it: no way. It may help people coming from searches, which may try that mount-bind trick to access a directory which was already a mount point before being mounted over. Being aware of other options may help.– user313992Oct 7, 2021 at 20:50
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
sudo mount --bind
as I've found out need to be run aftermount 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