2

This is similar like How do I do a bind mount over a symlink?

But here the question is how to do a mount --bind from a symlink?

I have /linux-4.13.0 and a symlink linux pointing to that: /linux -> /linux-4.13.0

No I like to do a mount --bind /linux /current but that actually mounts the target /linux-4.13.0 and not the symlink. That means changing the symlink later doesn't have any effect on the mount anymore. Is there any way to change that?

2 Answers 2

4

Unfortunately, what you want can't be done due to the fundamental principles of mounts.

Calling "mount A B" takes the directory "A" and attaches it into the in-kernel directory hierarchy in place of "B". In the common case where "A" is a block device containing a filesystem, the root directory of this FS is attached at "B". Bind mounts just simply grab the "A" directory. (Move mounts are essentially the same as bind mounts, they just additionally detach "A" from its original place.)

So whatever paths you supply as "A" and "B" are first resolved by walking all symlinks and relative components to get the two actual directories. The kernel simply does not have the original paths anymore. (That's also why some systems use a file - /etc/mtab - to remember what exactly was the "mount" command called with.)

All of this also applies when mounting a non-directory (like a regular file) onto another non-directory. You just cannot mount a directory onto a non-directory or vice versa.

2
  • 1
    This answer is not entirely correct. You can bind mount regular files and device inodes as well. It does not have to be directories.
    – kasperd
    Commented Oct 1, 2017 at 13:08
  • Good catch. I was trying hard to avoid going into too much detail on kernel internals and I oversimplified things a bit.
    – TooTea
    Commented Mar 22, 2018 at 21:35
0

With Linux v5.12+ kernels it is possible to create bindmount from symlinks with new mount API. Here is an example:

$ unshare --mount
$ mount --types tmpfs tmpfs /tmp
$ cd /tmp
$ echo TEST1 > test1
$ echo TEST2 > test2
$ ln --symbolic test1 symlink1
$ ln --symbolic test2 symlink2
$ cat symlink1
TEST1
$ cat symlink2
TEST2
$ bindmount-v2 symlink1 symlink2
Mounting symlink1 into symlink2
$ cat symlink2
TEST1
$ cat /proc/self/mountinfo | grep symlink
2221 2323 0:120 /symlink1 /tmp/symlink2 rw,relatime - tmpfs tmpfs rw,inode64

This uses simple C program bindmount-v2. I'm not aware about any attempt to add this to the standard mount utility.

The addition of AT_SYMLINK_NOFOLLOW is a key part to create bindmounts from symlinks.

Notes:

  • This is almost the same answer I gave on ServerFault as it suits both "over symlink" and "from symlink" cases of bindmount.
  • Symlinks can not be bindmounted to or from directory.

You must log in to answer this question.