How to use bind mounts in linux
Tags: bind, fstab, howto, linux, mount, partition, storage, sysadmin, tips, unix
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 |
June 3rd, 2012 at 11:54 am
[…] http://backdrift.org/how-to-use-bind-mounts-in-linux […]
June 25th, 2012 at 7:02 pm
[…] http://backdrift.org/how-to-use-bind-mounts-in-linux Like this:LikeBe the first to like this. […]
July 18th, 2012 at 3:16 am
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:
July 18th, 2012 at 7:26 am
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:
March 22nd, 2014 at 3:50 pm
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]
November 20th, 2012 at 6:33 am
Yes!that was exactly that i was searching for. Thanks
[Reply]
February 5th, 2013 at 12:58 am
If i want to mount across the server. can someone suggest me the command.
[Reply]
March 1st, 2013 at 7:30 am
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]
April 11th, 2013 at 2:04 pm
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]
May 30th, 2013 at 5:57 pm
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]
November 18th, 2013 at 8:08 am
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]
December 20th, 2013 at 5:46 am
[…] 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 […]
August 26th, 2014 at 5:50 am
[…] 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 […]
September 15th, 2014 at 1:03 am
[…] http://backdrift.org/how-to-use-bind-mounts-in-linux […]
April 10th, 2015 at 9:04 am
The way I used the –bind – I have an ftp server with chrooted users. I wanted to allow 3 people to access the same files. The files are outside their jail – as mentioned above. What I did not want to do was just have all three people ftp in using the same login – so each user has their own login/password – but all have access to the same files (root owns the files, and all have the same group ownership, and these users are all in the same group – 774 permissions on the files). Now if down the road, I need to remove a user I can remove that user from the group, unmounts their binded directory – and viola – no more access to those files – and didn’t need to have the other users change their passwords.
[Reply]
April 10th, 2015 at 9:07 am
Does mount do an auto bind? Just curious – On Fedora 21 – I just did 3 mounts, mounting the same device to three different mount points. the system allowed it just fine. I was surprised I didn’t get a device busy message on the second mount attempt.
[Reply]
May 26th, 2015 at 5:33 am
[…] creditz: http://backdrift.org/how-to-use-bind-mounts-in-linux […]
November 26th, 2015 at 6:26 pm
And how does one do a rbind with rslave in the fstab?
[Reply]
January 9th, 2016 at 6:05 am
If you don’t have enough discipline to resist these temptations, then you will not be able
to reach your goal of saving money for financial emergencies or making
a major purchase in the future. If you feel you’re
a VPN provider fails in this, run very fast without looking back.
All transactions are handled by PSP ‘ one of the most reputable and popular platforms
handling payments with the online currency.
[Reply]