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.

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

1 Answer 1

up vote 5 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
    
@Gilles Actually, you can do this simply using findmnt | fgrep [ as explained here. –  aculich Mar 6 '12 at 16:39
    
@Gilles What mount --version are you using that records any bind information in /etc/mtab? I am using version 2.20.1 and I looked at the latest sources and in neither case do I see bind information recorded anywhere that would allow you to grep for bind. On the other hand, what I suggested in my answer does in fact list bind mounts created with --bind as well as using the bind option. –  aculich Mar 6 '12 at 18:16

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.