How to display bind mounts

The simplest possible solution to display bind mounts.

The information required to distinguish bind mounts from regular ones is missing from the standard /etc/mtab and /proc/mounts file due to nonextensibility or other potential issues.

The solution is to parse process mount namespace to inspect root of the mount and pathname of the mount point to locate bind mounts.

Create bind mounts that will serve as an example.

$ mkdir /home/milosz/mnt
$ mkdir /home/milosz/media
$ sudo mount --bind /mnt /home/milosz/mnt
$ sudo mount --bind /media /home/milosz/media

Inspect mount information - it doesn't contain required information.

$ cat /proc/mounts 
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,nosuid,noexec,relatime,size=8106216k,nr_inodes=2026554,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,nodev,noexec,relatime,size=1630204k,mode=755 0 0
/dev/mapper/vgubuntu-root / ext4 rw,relatime,errors=remount-ro 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0
cgroup2 /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,name=systemd 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
none /sys/fs/bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 0 0
cgroup /sys/fs/cgroup/rdma cgroup rw,nosuid,nodev,noexec,relatime,rdma 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=27,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=624 0 0
mqueue /dev/mqueue mqueue rw,nosuid,nodev,noexec,relatime 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,relatime,pagesize=2M 0 0
debugfs /sys/kernel/debug debugfs rw,nosuid,nodev,noexec,relatime 0 0
fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0
configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0
/dev/loop1 /snap/core18/1650 squashfs ro,nodev,relatime 0 0
/dev/loop2 /snap/gnome-3-28-1804/110 squashfs ro,nodev,relatime 0 0
/dev/loop4 /snap/core/8689 squashfs ro,nodev,relatime 0 0
/dev/loop3 /snap/gnome-3-28-1804/116 squashfs ro,nodev,relatime 0 0
/dev/loop5 /snap/gitkraken/151 squashfs ro,nodev,relatime 0 0
/dev/loop6 /snap/gitkraken/152 squashfs ro,nodev,relatime 0 0
/dev/loop7 /snap/gnome-characters/375 squashfs ro,nodev,relatime 0 0
/dev/loop8 /snap/gnome-characters/399 squashfs ro,nodev,relatime 0 0
/dev/loop0 /snap/core/8592 squashfs ro,nodev,relatime 0 0
/dev/loop9 /snap/pycharm-professional/183 squashfs ro,nodev,relatime 0 0
/dev/loop10 /snap/core18/1668 squashfs ro,nodev,relatime 0 0
/dev/loop11 /snap/gnome-calculator/544 squashfs ro,nodev,relatime 0 0
/dev/loop13 /snap/gnome-logs/81 squashfs ro,nodev,relatime 0 0
/dev/loop12 /snap/spotify/41 squashfs ro,nodev,relatime 0 0
/dev/loop14 /snap/gtk-common-themes/1440 squashfs ro,nodev,relatime 0 0
/dev/sda1 /boot ext4 rw,relatime 0 0
tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=1630200k,mode=700,uid=1000,gid=1000 0 0
gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0
/dev/fuse /run/user/1000/doc fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0
tracefs /sys/kernel/debug/tracing tracefs rw,nosuid,nodev,noexec,relatime 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
/dev/mapper/vgubuntu-root /home/milosz/mnt ext4 rw,relatime,errors=remount-ro 0 0
/dev/mapper/vgubuntu-root /home/milosz/media ext4 rw,relatime,errors=remount-ro 0 0

Inspect more detailed mount information - it does contain required information.

$ cat /proc/self/mountinfo
24 31 0:22 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw
25 31 0:5 / /proc rw,nosuid,nodev,noexec,relatime shared:14 - proc proc rw
26 31 0:6 / /dev rw,nosuid,noexec,relatime shared:2 - devtmpfs udev rw,size=8106216k,nr_inodes=2026554,mode=755
27 26 0:23 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000
28 31 0:24 / /run rw,nosuid,nodev,noexec,relatime shared:5 - tmpfs tmpfs rw,size=1630204k,mode=755
31 1 253:1 / / rw,relatime shared:1 - ext4 /dev/mapper/vgubuntu-root rw,errors=remount-ro
32 24 0:7 / /sys/kernel/security rw,nosuid,nodev,noexec,relatime shared:8 - securityfs securityfs rw
33 26 0:27 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw
34 28 0:28 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k
35 24 0:29 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:9 - tmpfs tmpfs ro,mode=755
36 35 0:30 / /sys/fs/cgroup/unified rw,nosuid,nodev,noexec,relatime shared:10 - cgroup2 cgroup2 rw,nsdelegate
37 35 0:31 / /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime shared:11 - cgroup cgroup rw,xattr,name=systemd
38 24 0:32 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:12 - pstore pstore rw
39 24 0:33 / /sys/fs/bpf rw,nosuid,nodev,noexec,relatime shared:13 - bpf none rw,mode=700
40 35 0:34 / /sys/fs/cgroup/rdma rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,rdma
41 35 0:35 / /sys/fs/cgroup/net_cls,net_prio rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,net_cls,net_prio
42 35 0:36 / /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime shared:17 - cgroup cgroup rw,perf_event
43 35 0:37 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:18 - cgroup cgroup rw,cpu,cpuacct
44 35 0:38 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:19 - cgroup cgroup rw,freezer
45 35 0:39 / /sys/fs/cgroup/pids rw,nosuid,nodev,noexec,relatime shared:20 - cgroup cgroup rw,pids
46 35 0:40 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:21 - cgroup cgroup rw,memory
47 35 0:41 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:22 - cgroup cgroup rw,cpuset
48 35 0:42 / /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime shared:23 - cgroup cgroup rw,devices
49 35 0:43 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:24 - cgroup cgroup rw,blkio
50 35 0:44 / /sys/fs/cgroup/hugetlb rw,nosuid,nodev,noexec,relatime shared:25 - cgroup cgroup rw,hugetlb
51 25 0:45 / /proc/sys/fs/binfmt_misc rw,relatime shared:26 - autofs systemd-1 rw,fd=27,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=624
52 26 0:20 / /dev/mqueue rw,nosuid,nodev,noexec,relatime shared:27 - mqueue mqueue rw
53 26 0:46 / /dev/hugepages rw,relatime shared:28 - hugetlbfs hugetlbfs rw,pagesize=2M
54 24 0:8 / /sys/kernel/debug rw,nosuid,nodev,noexec,relatime shared:29 - debugfs debugfs rw
116 24 0:47 / /sys/fs/fuse/connections rw,nosuid,nodev,noexec,relatime shared:61 - fusectl fusectl rw
119 24 0:21 / /sys/kernel/config rw,nosuid,nodev,noexec,relatime shared:63 - configfs configfs rw
122 31 7:1 / /snap/core18/1650 ro,nodev,relatime shared:65 - squashfs /dev/loop1 ro
125 31 7:2 / /snap/gnome-3-28-1804/110 ro,nodev,relatime shared:67 - squashfs /dev/loop2 ro
128 31 7:4 / /snap/core/8689 ro,nodev,relatime shared:69 - squashfs /dev/loop4 ro
131 31 7:3 / /snap/gnome-3-28-1804/116 ro,nodev,relatime shared:71 - squashfs /dev/loop3 ro
134 31 7:5 / /snap/gitkraken/151 ro,nodev,relatime shared:73 - squashfs /dev/loop5 ro
137 31 7:6 / /snap/gitkraken/152 ro,nodev,relatime shared:75 - squashfs /dev/loop6 ro
140 31 7:7 / /snap/gnome-characters/375 ro,nodev,relatime shared:77 - squashfs /dev/loop7 ro
143 31 7:8 / /snap/gnome-characters/399 ro,nodev,relatime shared:79 - squashfs /dev/loop8 ro
146 31 7:0 / /snap/core/8592 ro,nodev,relatime shared:81 - squashfs /dev/loop0 ro
232 31 7:9 / /snap/pycharm-professional/183 ro,nodev,relatime shared:83 - squashfs /dev/loop9 ro
236 31 7:10 / /snap/core18/1668 ro,nodev,relatime shared:85 - squashfs /dev/loop10 ro
240 31 7:11 / /snap/gnome-calculator/544 ro,nodev,relatime shared:87 - squashfs /dev/loop11 ro
244 31 7:13 / /snap/gnome-logs/81 ro,nodev,relatime shared:89 - squashfs /dev/loop13 ro
248 31 7:12 / /snap/spotify/41 ro,nodev,relatime shared:91 - squashfs /dev/loop12 ro
531 31 7:14 / /snap/gtk-common-themes/1440 ro,nodev,relatime shared:93 - squashfs /dev/loop14 ro
149 31 8:1 / /boot rw,relatime shared:95 - ext4 /dev/sda1 rw
1599 28 0:53 / /run/user/1000 rw,nosuid,nodev,relatime shared:888 - tmpfs tmpfs rw,size=1630200k,mode=700,uid=1000,gid=1000
1629 1599 0:54 / /run/user/1000/gvfs rw,nosuid,nodev,relatime shared:904 - fuse.gvfsd-fuse gvfsd-fuse rw,user_id=1000,group_id=1000
1659 1599 0:55 / /run/user/1000/doc rw,nosuid,nodev,relatime shared:920 - fuse /dev/fuse rw,user_id=1000,group_id=1000
1029 54 0:12 / /sys/kernel/debug/tracing rw,nosuid,nodev,noexec,relatime shared:564 - tracefs tracefs rw
1407 51 0:65 / /proc/sys/fs/binfmt_misc rw,nosuid,nodev,noexec,relatime shared:760 - binfmt_misc binfmt_misc rw
1023 31 253:1 /mnt /home/milosz/mnt rw,relatime shared:1 - ext4 /dev/mapper/vgubuntu-root rw,errors=remount-ro
1438 31 253:1 /media /home/milosz/media rw,relatime shared:1 - ext4 /dev/mapper/vgubuntu-root rw,errors=remount-ro

Use awk to parse /proc/self/mountinfo file and display mount points for which root of the mount is not the / (root filesystem).

$ awk '$4 != "/" {printf("%-20s -> %-20s\n",$4,$5)}' /proc/self/mountinfo 
/mnt                 -> /home/milosz/mnt    
/media               -> /home/milosz/media  

That was simple.