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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Rather than using mount | grep, I'd like to use mount -l -t bind, but that doesn't work, and -t none shows all mounts.

share|improve this question
up vote 11 down vote accepted

Bind mounts are not a filesystem type, nor a parameter of a mounted filesystem; they're parameters of a mount operation. As far as I know, the following sequences of commands lead to essentially identical system states as far as the kernel is concerned:

mount /dev/foo /mnt/one; mount --bind /mnt/one /mnt/two
mount /dev/foo /mnt/two; mount --bind /mnt/two /mnt/one

So the only way to remember what mounts were bind mounts is the log of mount commands left in /etc/mtab. A bind mount operation is indicated by the bind mount option (which causes the filesystem type to be ignored). But mount has no option to list only filesystems mounted with a particular set of sets of options. Therefore you need to do your own filtering.

mount | grep -E '[,(]bind[,)]'
</etc/mtab awk '$4 ~ /(^|,)bind(,|$)/'

Note that /etc/mtab is only useful here if it's a text file maintained by mount. Some distributions set up /etc/mtab as a symbolic link to /proc/mounts instead; /proc/mounts is mostly equivalent to /etc/mtab but does have a few differences, one of which is not tracking bind mounts.

One piece of information that is retained by the kernel, but not shown in /proc/mounts, is when a mount point only shows a part of the directory tree on the mounted filesystem. In practice this mostly happens with bind mounts:

mount --bind /mnt/one/sub /mnt/partial

In /proc/mounts, the entries for /mnt/one and /mnt/partial have the same device, the same filesystem type and the same options. The information that /mnt/partial only shows the part of the filesystem that's rooted at /sub is visible in the per-process mount point information in /proc/$pid/mountinfo (column 4). Entries there look like this:

12 34 56:78 / /mnt/one rw,relatime - ext3 /dev/foo rw,errors=remount-ro,data=ordered
12 34 56:78 /sub /mnt/partial rw,relatime - ext3 /dev/foo rw,errors=remount-ro,data=ordered
share|improve this answer
    
This (grepping) will not necessarily work. It doesn't for me. There is no word bind in neither /etc/mtab, /proc/mounts o the output of mount. The directories that have been bound are listed as if it were their root filesystems mounted at those places. – rozcietrzewiacz Aug 4 '11 at 18:07
1  
@rozcietrzewiacz As I explain in my first paragraph, bind will never appear in /proc/mounts, because the kernel doesn't distinguish the two mount locations. /etc/mtab is the only place where bind has a chance of being remembered; apart from that there is no notion of original and re-bound mount point. What do you have in /etc/mtab then? – Gilles Aug 4 '11 at 18:35
    
:) Well, it is a symlink to /proc/mounts! :D I think I might have done this myself following a tip somewhere... But I noticed the same thing on my computer at work, where I almost cetainly haven't created the link myself. – rozcietrzewiacz Aug 4 '11 at 20:07
1  
@Gilles Actually, you can do this simply using findmnt | fgrep [ as explained here. – aculich Mar 6 '12 at 16:39
1  
@aculich You should post findmnt as an answer. It only works if the target directory is not another mount point, by the way. Try for example sudo mount --bind / foo && findmnt | grep foo – l0b0 Nov 19 '13 at 22:56

Maybe this could do the trick:

findmnt | grep  "\["

Example:

$ mkdir /tmp/foo
$ sudo mount --bind /media/ /tmp/foo
$ findmnt | grep  "\["
│ └─/tmp/foo                     /dev/sda2[/media] ext4            rw,relatime,data=ordered
share|improve this answer
    
+1 That works, but the result is tricky to parse. – l0b0 Jun 17 at 18:18

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.