Originally published August 20, 2016 @ 10:11 am

Sometimes you just need to do a quick sanity check on the filesystem mount status to make sure everything in fstab is mounted and everything’s that mounted is in the fstab. If, like myself, you’re a fan of NFS and bind mounts, getting a clear picture can get a bit hairy. So here’s an easy command making use of comm and the excellent findmnt utility to show any filesystems that should be mounted according to fstab but for some reason are not mounted:

o="lvnu -t $(awk '{print $NF}' /proc/filesystems | sort -u | xargs | sed -e 's/ /,/g'),bind -o TARGET"
comm -13 <(findmnt -k${o} | sort -u) <(findmnt -s${o} | sort -u)

To see any active mounts that are not in fstab you can use the following command:

o="lvnu -t $(awk '{print $NF}' /proc/filesystems | sort -u | xargs | sed -e 's/ /,/g'),bind -o TARGET"
comm -23 <(findmnt -k${o} | sort -u) <(findmnt -s${o} | sort -u)

The first findmnt gives you a list of what’s mounted according to the kernel. And the second part gives you a list of what should be mounted according to the fstab. Pretty simple. However, findmnt may not be available on older Linux versions. A viable alternative is the monutpoint command. Here’s a quick script that will list any filesystems from the fstab that are not mounted:

#!/bin/bash
egrep '^(/|[[:alnum:]\.-]*:/)' /etc/fstab | egrep -v "\Wswap\W" | awk '{print $2}' | sort -u | while read m
do
	if [ $(mountpoint "${m}" 2>/dev/null 1>&2; echo $?) -ne 0 ]
	then
		echo "${m}"
	fi
done

And here’s something extra: it may also be useful to make sure that whatever is listed in fstab and mtab as read-write actually is:

#!/bin/bash
random=$(echo "`expr ${RANDOM}${RANDOM} % 100000`+1"|bc -l)
o="lvnu -t ext2,ext3,ext4,reiserfs,btrfs,xfs,jfs,zfs,efs,nfs,bind"
for fs in $(sort -u <(findmnt -m${o} -o TARGET,OPTIONS | egrep "\srw" | awk '{print $1}') \
<(findmnt -s${o} -o TARGET,OPTIONS | egrep "\srw" | awk '{print $1}'))
do
	echo "Checking ${fs}"
	s="$(touch "${fs}/${random}" 2>/dev/null; echo $?)"
	if [ ${s} -ne 0 ]
	then
		echo "$(findmnt -k${o} -o TARGET,SOURCE | grep -m1 "^${fs}") appears to be read-only"
	else
		if [ ! -z "${random}" ]
		then
		/bin/rm -f "${fs}/${random}"
		fi
	fi
done