#!/bin/sh
#
# Check that we are using ext3/4 filesystems with expected options

if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

LC_ALL=C
export LC_ALL

scriptname="$0"

while read -r line; do
    set -- $line
    case $3 in
        ext2)
            if [ $2 != '/boot' ]; then
                printf 'error: %s: Using ext2 on %s\n' "${scriptname}" "$1"
            fi
            ;;
        ext3|ext4)
            printf 'success: %s: Using ext3/4 on %s\n' "${scriptname}" "$1"

            # Check if the filesystems on the mountpoints support acls
            if chacl -l "$1" >/dev/null 2>&1; then
                printf "success: %s: %s supports acls\n" "${scriptname}" "$1"
            else
                printf "error: %s: %s doesn't support acls\n" "${scriptname}" "$1"
            fi

            # Make sure all ext3/ext4 mount points are online resizable
            if ! tune2fs -l "$1" | grep -q '^Filesystem features:.* resize_inode'; then
                printf 'error: %s: Missing resize_inode in ext3/ext4 fs %s\n' "${scriptname}" "$2"
            fi
            ;;
    esac
done </proc/mounts

case $PROFILE in
*Main-Server*)
    # Make sure autofs do not hide the real file systems
    if [ -d /skole/tjener/home0/lost+found ] ; then
        printf 'success: %s: Found lost+found in /skole/tjener/home0/\n' "${scriptname}"
    else
        printf 'error: %s: No lost+found in /skole/tjener/home0/.  Blocked by autofs?\n' "${scriptname}"
    fi

    # Make sure home0 and backup have acl and user_xattr enabled.  See
    # if bug #638822 is present or not.
    for dir in /skole/tjener/home0 /skole/backup; do
        dev="$(findmnt -T "${dir}" -n -o SOURCE)"
        for opt in acl user_xattr; do
            if tune2fs -l "${dev}" | grep -q "^Default mount options:.* ${opt}"; then
                printf "success: %s: Found option %s in %s.\n" "${scriptname}" "${opt}" "${dir}"
            else
                printf "error: %s: Did not find option %s in %s.\n" "${scriptname}" "${opt}" "${dir}"
            fi
        done
    done
    ;;
esac

# Report too full file systems.  Should have at least 20% free to
# avoid warning from Nagios, preferably between 20% and 25%.
df -m -P -x tmpfs |
  awk '/\/dev\// { if ($3/$2 >= 0.80) print "error: Too full file system", $1, "uses", $3, "of", $2, "MiB" }'
