Tuesday 27 June 2017

Script to check if all fstab entries are mounted

In this article I'd like to share a script which would compare file system entries in the /etc/fstab file with the file systems that are currently mounted and tell us if there is any file system which has an entry in the /etc/fstab file but is not mounted.

So, here is the script:

[root@still ~]# cat tab.bash
#!/bin/bash

FSTAB_ENTRIES=$(cat /etc/fstab | awk '$1 !~/#|^$|swap/ {print $2}')

for FS in ${FSTAB_ENTRIES}

do

df -hPT | grep -wq ${FS}

if [ $? -eq 0 ]
 then
  echo "The file system ${FS} has an entry in /etc/fstab file and is mounted"
 else
  echo "The file system ${FS} has an entry in /etc/fstab file but is not mounted"
fi

done
[root@still ~]#


Let's test it out.

This is my /etc/fstab file and the current df output.

[root@still ~]# cat /etc/fstab ; df -hTP

#
# /etc/fstab
# Created by anaconda on Sat Dec 24 12:03:40 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        1 1
UUID=93b957e6-f8f9-42f9-a7ad-9927f694f1ce /boot                   xfs     defaults        1 2
/dev/mapper/centos-swap swap                    swap    defaults        0 0

##testing##
/dev/vg1/lv01   /test_dir1                       ext4     defaults        1 1
/dev/vg1/lv02   /test_dir2                       ext4     defaults        1 1
/dev/vg1/lv03   /test_dir3                       ext4     defaults        1 1
/dev/vg1/lv04   /test_dir4                       ext4     defaults        1 1
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        18G  6.0G   12G  35% /
devtmpfs                devtmpfs  481M     0  481M   0% /dev
tmpfs                   tmpfs     490M   80K  490M   1% /dev/shm
tmpfs                   tmpfs     490M  7.1M  483M   2% /run
tmpfs                   tmpfs     490M     0  490M   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  134M  363M  27% /boot
/dev/mapper/vg1-lv01    ext4      283M  2.1M  262M   1% /test_dir1
/dev/mapper/vg1-lv02    ext4      283M  2.1M  262M   1% /test_dir2


You can notice that /test_dir3 and /test_dir4 file systems have entries in the /etc/fstab file but are not mounted at the moment.

Let's run the script to verify.

[root@still ~]# ./tab.bash
The file system / has an entry in /etc/fstab file and is mounted
The file system /boot has an entry in /etc/fstab file and is mounted
The file system /test_dir1 has an entry in /etc/fstab file and is mounted
The file system /test_dir2 has an entry in /etc/fstab file and is mounted
The file system /test_dir3 has an entry in /etc/fstab file but is not mounted
The file system /test_dir4 has an entry in /etc/fstab file but is not mounted


There you have it.

I hope this script helps you in being used as a pre-check or post-check during maintenance activities.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...