Created
July 5, 2017 11:19
-
-
Save Decstasy/744b3a0c2c344af0266ac4e6218aaadc to your computer and use it in GitHub Desktop.
Compare /etc/fstab with /proc/mounts and try remount if something is missing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Dennis Ullrich | |
# Version 0.1-0 (2017-07-02) | |
# [email protected] | |
if [ "$1" = "-v" ]; then | |
set -x | |
shift | |
fi | |
check_mounts(){ | |
# Ignore following fstab entry's for comparison: | |
# Commentary's ^# | |
# Empty lines ^$ | |
# ^[[:space:]]$ | |
# /sys ^/sys | |
# /proc ^/proc | |
# Lines with usbfs, debugfs, swap or tmpfs | |
mounts="$(awk '{print $2}' /proc/mounts)" | |
rc=0 | |
for fstab in $(grep -iv '^#\|^$\|^[[:space:]]$\|^/sys\|^/proc\|usbfs\|debugfs\|swap\|tmpfs' /etc/fstab | awk '{print $2}'); do | |
# Remove last / of path if character count is greater than 1 | |
[[ ${#fstab} -gt 1 ]] && fstab="${fstab%/}" | |
if ! grep -qs "^${fstab}$" <<<"$mounts"; then | |
>&2 echo "${fstab} is not mounted!" | |
rc=1 | |
fi | |
done | |
if [ $rc -ne 1 ]; then | |
echo "All filesystems are mounted." | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
try_remount(){ | |
# Save reference | |
mounts="$(awk '{print $2}' /proc/mounts)" | |
# Try mount -a | |
echo "Trying \"mount -a\"" | |
mount -a | |
case $? in | |
0) echo "Mount -a successful." | |
;; | |
1) >&2 echo "Incorrect invocation or permissions!" | |
return 1 | |
;; | |
2) >&2 echo "System error (out of memory, cannot fork, no more loop devices)!" | |
return 1 | |
;; | |
4) >&2 echo "Internal mount bug!" | |
return 1 | |
;; | |
8) >&2 echo "User interrupt!" | |
return 1 | |
;; | |
16) >&2 echo "Problems writing or locking /etc/mtab!" | |
return 1 | |
;; | |
32) >&2 echo "Mount failure!" | |
return 1 | |
;; | |
64) >&2 echo "Some mount(s) succeeded!" | |
;; | |
esac | |
# Return new filesystems and check again | |
newmounts="$(diff -aB --unchanged-line-format="" <(echo "$mounts") <(awk '{print $2}' /proc/mounts))" | |
if [ $? -eq 0 ]; then | |
>&2 echo "No new mounts detected." | |
# Return 1 as something is still wrong | |
return 1 | |
else | |
echo -e "New mounts detected:\n${newmounts}" | |
echo "Checking mounts again..." | |
# Return 1 if there is still a missing mount | |
check_mounts && return 0 || return 1 | |
fi | |
} | |
check_mounts || try_remount | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment