Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Symlinks have limitations in how functions like ls, mv, and cp can operate on them because unlike shell initiated commands like cd, these functions do not have information about how the user accessed the directory with respect to the logical path (see related post). It seems like using the mount --bind option instead can get an around this, offering increased functionality and compatibility with samba and other file servers because the mounted directory will then have two independent physical paths, instead of a link.

I would like to replace all of my symbolic links with references using the mount --bind option but this would mean mounting over 150 points in fstab. Are there any performance issues that could potentially arise from this or any other drawbacks that I should consider?

Thanks for any advice.

share|improve this question
    
Have you considered using hard links? –  ire_and_curses Oct 1 '12 at 22:31
    
@ire_and_curses Most Unix-like systems prohibit hard links, for good reason (and for the same reasons, you should virtually never use them even on systems where you can). –  Eliah Kagan Oct 2 '12 at 7:06
2  
@ire_and_curses: To clarify Eliah's statement, you can't create a hard link to a directory (although HFS+ does support it, in a way). And creating a recursive tree of hard links doesn't keep both directory paths in sync. –  bahamat Oct 2 '12 at 16:25
    
@bahamat, Eliah Kagan - Thanks, this is helpful to know. –  ire_and_curses Oct 2 '12 at 18:11

3 Answers 3

up vote 21 down vote accepted

With mount --bind, a directory tree exists in two (or more) places in the directory hierarchy. This can cause a number of problems. Backups and other file copies will pick all copies. It becomes difficult to specify that you want to copy a filesystem: you'll end up copying the bind-mounted files twice. Searches with find, grep -r, locate, etc., will traverse all the copies, and so on.

You will not gain any “increased functionality and compatibility” with bind mounts. They look like any other directory, which most of the time is not desirable behavior. For example, Samba exposes symbolic links as directories by default; there is nothing to gain with using a bind mount. On the other hand, bind mounts can be useful to expose directory hierarchies over NFS.

You won't have any performance issues with bind mounts. What you'll have is administration headaches. Bind mounts have their uses, such as making a directory tree accessible from a chroot, or exposing a directory hidden by a mount point (this is usually a transient use while a directory structure is being remodeled). Don't use them if you don't have a need.

Only root can manipulate bind mounts. They can't be moved by ordinary means; they lock their location and the ancestor directories.

Generally speaking, if you pass a symbolic link to a command, the command acts on the link itself if it operates on files, and on the target of the link if it operates on file contents. This goes for directories too. This is usually the right thing. Some commands have options to treat symbolic links differently, for example ls -L, cp -d, rsync -l. Whatever you're trying to do, it's far more likely that symlinks are the right tool, than bind mounts being the right tool.

share|improve this answer
    
Thanks. I guess I wasn't considering the impact on backups, copies and file searches. I was able to get Samba to follow the symlinks by adding 'follow symlinks = yes' in smb.conf but this does compromise the security in that any samba user can execute say, 'ln -s /etc' in a writeable folder and gain access to system files. I'm trying to find a way around this. Please let me know if you know of one. –  mrtrujiyo Oct 2 '12 at 21:24
    
@mrtrujiyo For that requirement, I think it would make sense to run the samba server in a chroot, and bind-mount the directories you want to export inside that chroot. Make sure to exclude the root of the chroot from backups and so on (with this organization, you only need to exclude the one toplevel directory, so it isn't much of a maintenance headache). –  Gilles Oct 2 '12 at 21:31

In addition to what @Gilles wrote earlier, it's worth noting that some utilities might consider a bind-mounted directory to be a separate file system. This may have performance or functionality implications if the program can no longer assume that the same inode number refers to the same file (which it doesn't, if they are on different file systems), a move cannot be optimized as link-at-target-then-unlink-source, etc.

share|improve this answer
    
Thanks. I was wondering how simple utilities like df and du would handle directory size calculations on filesystems with bind mounts. –  mrtrujiyo Oct 2 '12 at 21:33
    
At least GNU df on my system doesn't even consider bind-mounted directories by default, but if asked specifically, it's treated as another mount of the same file system. (Which, if you ask me, is expected behavior for a tool with df's purpose.) –  Michael Kjörling Oct 3 '12 at 9:11

You should also want to use bind mounts instead of symlinks when you are relying on a support that might not always be in place (for example an external disk) and you want to be sure that the original directory structure is in place even if the support fails or is removed.

For example if I want to keep /var/tmp in an sd card I will use the bind mount, since some programs will expect /var/tmp to be a valid directory even if the card is removed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.