7

I have a symlink:

/test

Which is pointing at a directory:

/source

I want to do a bind mount to mount /new over /test

mount -o bind /new /test

However this binds /new over /source (with /test pointing at /source - showing the files from /new, but not what I want). How do I stop mount following the symlink and get it to mount at /test.

2 Answers 2

1

Since /test is not a real directory, this won't work.
Unlink it, create it as a real directory, then do the bind mount.

1
  • 1
    You can use bind mounts to replace one file with another, but it appears that when the target is a symlink, the kernel expands it before doing the bind mount :(
    – Cheetah
    Sep 24, 2014 at 23:27
1

It is possible to create bindmount over symlinks with new Linux kernel mount API. Here is an example:

unshare -m
mount -t tmpfs tmpfs /tmp
cd /tmp
echo TEST1 > test1
echo TEST2 > test2
ln -s test1 symlink1
ln -s 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, source https://github.com/Snorch/linux-helpers/blob/master/bindmount-v2.c

I'm not aware about any attempt to add this to mount utility.

Only supported on v5.12+ kernels. AT_SYMLINK_NOFOLLOW is a key part to create bindmounts on top of symlinks. If you drop mount_setattr related hunks, which is probably not important for your case you would be able to do the same on v5.2+ kernels.

note: Symlink can not be bindmounted to or from directory.

You must log in to answer this question.

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