LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-08-2018, 11:52 AM   #1
sreyan32
Member
 
Registered: Jan 2015
Posts: 52

Rep: Reputation: Disabled
What is a bind mount and how are they different?


Confusing topic and no one has any clear answers.

What exactly is a bind mount in Linux? How are they different from normal/other types of mount?

This SE answer sheds some light but then raises more doubts like:
  • Are bind mounts a way of mounting a directory to another mount point? Isn't that what symlinks and hardlinks are for?
  • Are they relating to the live filesystem where if you make a change to the files they are not reflected on the disk?
  • Why are they useful?
 
Old 12-08-2018, 12:17 PM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Here: https://unix.stackexchange.com/quest...s-a-bind-mount
 
Old 12-08-2018, 10:42 PM   #3
sreyan32
Member
 
Registered: Jan 2015
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
I have already linked this exact same question in my post. I still had some doubts thats why I posted here.
 
Old 12-09-2018, 05:13 PM   #4
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
In the aforementioned article it says
Quote:
Unlike a hard link or symbolic link, a bind mount doesn't affect what is stored on the filesystem.
 
Old 12-10-2018, 05:05 AM   #5
sreyan32
Member
 
Registered: Jan 2015
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by JeremyBoden View Post
In the aforementioned article it says
Yes it also says that the changes reflect "magically" on both sides. If one side is a directory on the disk and the next side is the mount, and if both sides see the change magically then how can it be a property of the live system? Since even the source is getting change.

Do you see the confusion?
 
Old 12-10-2018, 06:08 AM   #6
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,830
Blog Entries: 17

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
this

Quote:
Originally Posted by mount --help
-B, --bind mount a subtree somewhere else (same as -o bind)

-R, --rbind mount a subtree and all submounts somewhere else
Quote:
Originally Posted by man mount
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 by using this fstab entry:

/olddir /newdir none bind

After this call the same contents are accessible in two places. One can also remount a single file (on a single file). It's also possible to use the bind
mount to create a mountpoint from a regular directory, for example:

mount --bind foo foo

The bind mount call attaches only (part of) a single filesystem, not possible submounts. The entire file hierarchy including submounts is attached a second
place by using:

mount --rbind olddir newdir

Note that the filesystem mount options will remain the same as those on the original mount point.

mount(8) since v2.27 allows to change the mount options by passing the relevant options along with --bind. For example:

mount --bind,ro foo foo

This feature is not supported by the Linux kernel; it is implemented in userspace by an additional mount(2) remounting syscall. This solution is not
atomic.

The alternative (classic) way to create a read-only bind mount is to use the remount operation, for example:

mount --bind olddir newdir
mount -o remount,ro,bind olddir newdir

Note that a read-only bind will create a read-only mountpoint (VFS entry), but the original filesystem superblock will still be writable, meaning that the
olddir will be writable, but the newdir will be read-only.

It's impossible to change mount options recursively (for example with -o rbind,ro).
What exactly is unclear?

Last edited by zeebra; 12-10-2018 at 06:10 AM.
 
Old 12-10-2018, 07:48 AM   #7
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Quote:
Unlike a hard link or symbolic link, a bind mount doesn't affect what is stored on the filesystem.
This is confusing. It just means that the mount itself doesn't write to the filesystem that got mounted over, while a link adds an entry in a directory. Once you unmount a bind mount, the underlying filesystem is unchanged. The mounted filesystem can be read and written and will reflect all of the changes that are made.
 
Old 12-10-2018, 08:42 AM   #8
sreyan32
Member
 
Registered: Jan 2015
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by zeebra View Post
this





What exactly is unclear?

So to clarify my understanding:

The bind mount is only useful when you want to mount part of the filesystem and the normal mounts are useful for mounting block devices. Am I correct?

When I change a file in the directory that is bind mounted does that change it in the filesystem also?

Why would you do a bind mount?
 
Old 12-10-2018, 09:40 AM   #9
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by sreyan32 View Post
When I change a file in the directory that is bind mounted does that change it in the filesystem also?

Why would you do a bind mount?
Yes, it is the same file, so it WILL be changed.

And why using a bind mount: often in preparation of a chroot (change root), when you want i.e. the /dev of the "original" fs visible into the chroot'ed tree, because, of course, after a chroot you won't be able to access any files/directories that were higher in the hierarchy from the new root dir.
 
1 members found this post helpful.
Old 12-11-2018, 05:31 AM   #10
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,830
Blog Entries: 17

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
Quote:
Originally Posted by sreyan32 View Post
So to clarify my understanding:

The bind mount is only useful when you want to mount part of the filesystem and the normal mounts are useful for mounting block devices. Am I correct?

When I change a file in the directory that is bind mounted does that change it in the filesystem also?

Why would you do a bind mount?
Yes and Yes.

Let's say you want to mount already mounted /usr/share under /mnt/tmp, how would you go about that? This is different than mounting a filesystem block device, say /dev/sda7 under /mnt/tmp.

The folder you want to mount is under an already mounted block device and already mounted. If you try mount /usr/share /mnt/tmp you get an error. If you use mount --bind /usr/share /mnt/tmp you do not.

After you use mount --bind /usr/share /mnt/tmp, whatever is under /usr/share is ALSO visible under /mnt/tmp. This means you basically mount a non-blockdevice under another mountpoint, "duplication", which is otherwise not possible. So the same place is mounted two different places.

Why to use this? I can't answer that.. It can be useful in many cases I would guess. I know one case that I have done myself, during installation of Gentoo where you create a virtual system and set this up using "chroot". One of the steps you do when you chroot into Gentoo before the system is ready for use is the "mount --rbind" both /proc and /sys from your running distro into the chroot Gentoo environment. Why exactly this is necessary I do not know, you'd have to ask Gentoo, but my guess is that you trick Gentoo into thinking it is an actual running environment instead of a chroot, and thus can set the system up as if it was a running system and not a chroot environment.
 
1 members found this post helpful.
Old 12-11-2018, 11:03 AM   #11
sreyan32
Member
 
Registered: Jan 2015
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by zeebra View Post
Yes and Yes.

Let's say you want to mount already mounted /usr/share under /mnt/tmp, how would you go about that? This is different than mounting a filesystem block device, say /dev/sda7 under /mnt/tmp.

The folder you want to mount is under an already mounted block device and already mounted. If you try mount /usr/share /mnt/tmp you get an error. If you use mount --bind /usr/share /mnt/tmp you do not.

After you use mount --bind /usr/share /mnt/tmp, whatever is under /usr/share is ALSO visible under /mnt/tmp. This means you basically mount a non-blockdevice under another mountpoint, "duplication", which is otherwise not possible. So the same place is mounted two different places.

Why to use this? I can't answer that.. It can be useful in many cases I would guess. I know one case that I have done myself, during installation of Gentoo where you create a virtual system and set this up using "chroot". One of the steps you do when you chroot into Gentoo before the system is ready for use is the "mount --rbind" both /proc and /sys from your running distro into the chroot Gentoo environment. Why exactly this is necessary I do not know, you'd have to ask Gentoo, but my guess is that you trick Gentoo into thinking it is an actual running environment instead of a chroot, and thus can set the system up as if it was a running system and not a chroot environment.
Wow. Thanks so much, your explanation really helped. Especially for the mount point duplication part. Let me see if I have any doubts and I will get back to you if necessary.
 
Old 12-15-2018, 09:58 AM   #12
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Why exactly this is necessary I do not know, you'd have to ask Gentoo, but my guess is that you trick Gentoo into thinking it is an actual running environment instead of a chroot, and thus can set the system up as if it was a running system and not a chroot environment.
Pretty much the same as in an installation of Arch, you need the nodes accessible to the environment running inside the chroot. These are special files, and in some cases links don't work the same way.

There are also concerns about levels of links - you can only have so many, and many device nodes are already links to others. For example, on my box,
Code:
/dev/stdin -> /proc/self/fd/0
/proc/self -> /proc/2965
so you see we're already two levels deep. Bind mounting dev to the new environment's filesystem avoids this.

I've had instances in the past where I needed files in a specific hierarchy, and a link (sym or hard) was unusable - for a variety or reasons - a bind mount was the only way to avoid this.

Last edited by goumba; 12-15-2018 at 10:01 AM.
 
Old 12-15-2018, 07:09 PM   #13
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,830
Blog Entries: 17

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
Anyways, links are not nearly as convenient.. Imagine having to set up a bunch of links.. Blah.

I can imagine also on servers of some sorts that bind mounting some directories elsewhere in the tree could be useful in some ways.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
System Log Files - What information do they contain and how useful they can be? Hi_This_is_Dev Linux - Server 6 09-21-2010 11:34 AM
Software Repositories; What they are, and why they are the best Bluestreak Linux - Software 2 03-12-2009 08:56 PM
How much do they cost and what do they offer for their price? Nad0xFF Linux - Enterprise 11 04-20-2005 05:14 AM
Nvidia drivers, where do they install and what do they modify? jimdaworm Slackware 3 02-12-2005 11:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration