1

Some time ago I wanted to be able to access files in a directory which was overshadowed by later mount point. I recall finding several QAs on SE giving answers, but I recall none I've tried worked. Maybe they worked on earlier Linux kernel, I don't know. I now try to use distros with one of latest kernels (5.x now).

If indeed there is a way using "standard" tools to access part of directory tree overshadowed by mount, please write as an answer.

I have some workarounds in mind.

  1. Maybe there is a tool to make softlink to hardlink (inode) of directory? AFAIK standard ln cannot do that, ln -s (AFAIK and by try-and-error) creates a link to a place in file tree, not inode. If where that place points to changed (via mount on top), that symlink would lead to a new place, not previous one.

Added in response to comments by @terdon:
From a user perspective (at least mine who reads man ln etc), symlink is a type of link that operating system (including system utilities) differentiates from inodes (hardlinks). It is a link that find would not traverse in loops.

  1. Semi-manually read physical inode data via dd (before mount) and then apply it to another later (basically hard-linking). How to do that?

  2. Enable hard linking to folders in the kernel (I plan to use it rarely for special tasks, I'm aware of loop reasoning against it, but many Unixes had it AFAIK and functioned). How to do that?

P.S. might be better to split the question into several...

Added 2, ln -s test:

$ echo 123 > 1/1/te.xt
$ ln -s 1/1 2
$ ls 2
te.xt
$ sudo mount -o loop /path/ubuntu-20.04.3-desktop-amd64.iso 1
$ ls 2 # 2 is displayed as broken link in GUI
2

Added 3, trying mount --bind from Accessing contents on the underlying mount point path - failed

$ mount --bind 1 2
$ ls 2
1
$ sudo mount -o loop /path/ubuntu-20.04.3-desktop-amd64.iso 1
$ ls 2
boot  casper  dists  EFI  install  isolinux  md5sum.txt  pool  preseed  ubuntu

Added 4: bind mount to one level up -failed same way (btw it is tmpfs system, maybe it works on ext4? have not found explanation on what you advice to do reading about --bind in man mount).

Added 5:
found out mount --bind need to be executed after overshadowing mount command.

1

1 Answer 1

3

You can't make hard links to directories, since that can lead to loops in the directory tree, and anyway, the .. entry in the directories wouldn't be unique. Since you can't make a hard link to directory, you also can't make a soft link to a hard link to a directory.

Unless you binary patch the filesystem itself, in which case you can break it like that, or in some other way. At least fsck will croak at you, and there's no telling if the filesystem driver will also barf.

If you have some data in /foo, and you need to keep that data available, while mounting something else on /foo, the easiest way to do that is to rename /foo to /bar, create a new empty directory /foo, and mount the other fs on that new /foo.

On Linux, you can do some things with bind mounts, e.g. after mounting something on /foo, you can bind mount the whole original / to /bar, so that the original /foo shows up in /bar/foo. But the rest of / also will be visible there, so you'll have /bar/bin, /bar/etc, and so on.

# mkdir /foo /bar
# touch /foo/file-in-root-fs.txt
# mount -t tmpfs tmpfs /foo
# mount --bind / /bar
# ls -l /bar/foo
total 0
-rw-r--r-- 1 root root 0 Dec  1 14:36 file-in-root-fs.txt

I tried to see if it's possible to just have the one directory bind mounted away from under the new mount, but didn't find a way to do that.

The straightforward attempt of bind mounting /foo to /bar first, and then mounting over /foo seemed to copy the new mount to both places too. (Though that's on an old kernel.)

# mount --bind /foo /bar
# mount -t tmpfs tmpfs /foo
# ls -ldi /foo /bar
46378188 drwxrwxrwt 2 root root 40 Dec  1 14:37 /bar/
46378188 drwxrwxrwt 2 root root 40 Dec  1 14:37 /foo/
6
  • just found out: sequence is important, bind after does the trick (your first example), not second (bind beforehand gets "cancelled"). Is it documented somewhere? (could not see such detail in man mount page) Commented Dec 1, 2021 at 13:21
  • and (surely) if needed location is already shadowed then --bind should aim at least one level higher in directory structure. Commented Dec 1, 2021 at 13:55
  • @Martian2020 If you need emergency access to a file or dir which was shadowed by a mount, you can create a private mount space, and unmount the shadowing filesystem inside it. You could also access the private ns from outside via /proc/PID/root. Example: mkdir -p a b; touch a/file; mount -t tmpfs t a; unshare -m bash -c 'umount a; sleep 1000' & sleep .1; ls /proc/$!/root/$PWD/a => file.
    – user313992
    Commented Dec 2, 2021 at 2:44
  • @Martian2020 If instead you want to explore the potential of complexity and confusion that using layered mounts has, there's really no point in it, as all that shit has been already discussed to death and is not very interesting.
    – user313992
    Commented Dec 2, 2021 at 2:47
  • 1
    See also: unix.stackexchange.com/a/388906/325065. And BTW, there's no such thing as "soft links". Symbolic links are not softer than hard links, as any symbolic link is itself a hard link to a special inode.
    – user313992
    Commented Dec 2, 2021 at 3:00

You must log in to answer this question.

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