How to use bind mounts in linux

Tags: , , , , , , , , ,

Have you ever dealt with a system that wasn’t partitioned properly when it was built and now it has gone into production? You’ll probably be hard pressed to find the time and patience to rebuild the system any time soon. Luckily there is a way to step around many of the limitations of a poorly partitioned system using bind mounts.

Bind mounts are quite simple. Instead of mounting a device (with a file system) on a particular path you are mounting one path into another path.

For example: Let’s say you have a small /var but a very large /opt partition and you need additional space for your growing log files.

First, shut down the services writing to log files, then…

mv /var/log /opt/var_log
mkdir /var/log
mount -o bind /opt/var_log /var/log

You will now see this reflected when running the mount command:

# mount | grep var
/opt/var_log on /var/log type none (rw,bind)

At this point you are ready to restart the previously stopped services.

If you want this to persist across reboots, you’ll just need to update your /etc/fstab with the bind mount as well.

# /etc/fstab
/opt/var_log              /var/log                 none    bind    0 0

And there you have it! Its not beautiful, but it will help you keep the lights on until you can get a long-term fix in place.

More details about bind mounts

From the man page on ‘mount’.

       The bind mounts.
              Since Linux 2.4.0 it is possible to remount part of the file hierarchy somewhere else. The call is
                     mount --bind olddir newdir
              or shortoption
                     mount -B olddir newdir
              or fstab entry is:
                     /olddir /newdir none bind
 
              After this call the same contents is accessible in two places.  One can also remount a single file (on a single file).
 
              This call attaches only (part of) a single filesystem, not possible submounts. The entire file hierarchy including submounts is attached a  sec-
              ond place using
                     mount --rbind olddir newdir
              or shortoption
                     mount -R olddir newdir
 
              Note that the filesystem mount options will remain the same as those on the original mount point, and cannot be changed by passing the -o option
              along with --bind/--rbind. The mount options can be changed by a separate remount command, for example:
 
                     mount --bind olddir newdir
                     mount -o remount,ro newdir

14 Responses to “How to use bind mounts in linux”

  1. Setup VSFTPD with custom multiple directories and (virtual) users accounts on Ubuntu (no database required) Says:

    [...] http://backdrift.org/how-to-use-bind-mounts-in-linux  [...]

  2. GNU/Linux bind mount « Giovanni’s Blog Says:

    [...] http://backdrift.org/how-to-use-bind-mounts-in-linux Like this:LikeBe the first to like this.   [...]

  3. Jeff Williams Says:

    This would be better accomplished with symbolic links, i.e.:

    mv /var/log /opt/var_log
    ln -s /opt/var_log /var/log

    Bind mounts are more useful for when you can’t use symlinks, i.e. linking a directory inside a chroot or VM.

    Jeff

    [Reply]

    Keith Reply:

    That’s another approach, but it has it’s own set of problems as well. Keep in mind that a symlink is just a file. In my case I want /var/log to function exactly as it would if there was a filesystem mounted there.

    Take backups as one example. It’s a common use case to explicitly define the directories that will be included in backups. So, if your backup system is expecting /var/log to be a directory containing logs, and it’s changed to a symlink, you will have effectively disabled backups of your logs. Instead, you’ll have only a backup of the symlink file itself.

    What I’m trying to say is that if the problem would be solved by repartitioning or growing a filesystem, but you can’t afford the immediate downtime to reconfigure the system, bind mounts are a useful stopgap solution.

    [Reply]

    jj Reply:

    My backups usually do NOT cross mount points, so I can backup root without everything else. If “find” treats it as a mount point, it won’t be crossed by most backup programs or scripts. But I see your point if the mount-directory itself is specified to a backup program that can’t be told to follow symbolic links.

    [Reply]

  4. NIK Says:

    Yes!that was exactly that i was searching for. Thanks

    [Reply]

  5. swa Says:

    If i want to mount across the server. can someone suggest me the command.

    [Reply]

  6. Juan Rial Says:

    Bind mounts are useful as more than a stopgap measure. An example would be under Android, where this is used by the S2E application to extend older phones’ limited storage (think HTC desire with only 148MB available for apps/data/cache) by creating a second partition on the SD card and bind-mounting folders in there onto the corresponding folders of the internal memory.

    Well, technically, it’s still a stopgap measure: it helps you along until you can justify plonking down another couple of hundred dollars on a new phone. ;)

    [Reply]

  7. Peter Woo Says:

    I like this bind mount but why is it described as a stop-gap measure? It appears to be elegant. Suppose I have disk full on my disk1, and trying to relocate some directories to disk2. By using bind mount, I don’t need to worry about partition sizing but just with one disk2 partition housing the several directories from disk1, and bind mount to disk2’s directories accordingly. Did I miss something more crucial?

    [Reply]

  8. Mike Says:

    I had a production server hitting a NAS storage through a NFS mount. In order to reproduce the behavior in QA without buying a NAS, we just faked it with a bind mount. Test program doesn’t know the difference.

    [Reply]

  9. z666z666z Says:

    Maybe this is stupid question… but maybe bind mount can solve it.

    I have:
    -Linux Ubuntu Server running over Android (as a process)
    -With RDP Android Application i can access Linux X desktop
    -Both Android and Linux share the / (if i access Read&Write from Android or from Linux to / i go to same place, very bad security in case of damage)
    -I can access as normal user and/or root to both Android and Linux

    I want:
    -Protect Android /, so Linux / is not Android / (chroot may help)
    -Access Android / on Read&Write from insed cush chroot Linux

    What steps must i follow?

    Notes:
    -Android is very poor, i can not run many Linux commands on it (they not exists)
    -Linux is loop mounted by an Android executable (i tried to do it with terminal commands and i can not, Android mount command does not understand loop)
    -Linux runs as an Android process
    -I can run such Linux as an RDP server or as a full command line terminal, so i can run normal Linux commands inside it as user and when needed as root (full mount command, etc)
    -From Linux command prompt i can see Android / as / (shared between Android and Linux, no chroot done)… i want to avoid this
    -I want not to loose access to Android / from inside Linux, but not as Linux /, instead as /mnt/AndroidRoot/

    Any help is wellcome

    [Reply]

  10. Home dir showing (some) contents of two drives. Says:

    […] As you can see from the last keyword on that line, its a 'bind' mount… try this http://backdrift.org/how-to-use-bind-mounts-in-linux […]

  11. Setup VSFTPD with custom multiple directories and (virtual) users accounts on Ubuntu (no database required) | SEÇTİKLERİM Says:

    […] If you want to create some sort of symlink to let your user access somewhere outside their chroot jail use mount –bind http://backdrift.org/how-to-use-bind-mounts-in-linux […]

  12. Owncloud and an external hard drive | Man and Keyboard Says:

    […] http://backdrift.org/how-to-use-bind-mounts-in-linux […]

Join the Conversation