Linux : script de vérification des périphériques RAID pour Gentoo

Boris HUISGEN
Ce script shell permet de lancer une vérification des périphériques RAID (mdadm).
Il est largement inspiré du script checkarray écrit par Martin F. Krafft (intégré à la distribution Debian).
#!/bin/sh
#
# mdcheck.sh
#
# Boris HUISGEN <bhuisgen@hbis.fr>
#
set -eu
PROGNAME=${0##*/}
VERSION="0.1-20110328"
usage()
{
    echo "
Usage: $PROGNAME [OPTIONS] [ARRAY ...]
Options:
-a|--all       check all assembled arrays.
-s|--status    show status of arrays.
-x|--cancel    cancel a running redundancy check.
-h|--help      show this output.
-v|--version   show version information."
}
version()
{
    echo "$PROGNAME version $VERSION"
}
SHORTOPTS=asxhv
LONGOPTS=all,status,cancel,help,version
eval set -- $(getopt -o $SHORTOPTS -l $LONGOPTS -n $PROGNAME -- "$@")
arrays=''
all=0
status=0
action=check
for opt in $@; do
    case "$opt" in
    -a|--all) all=1;;
    -s|--status) action=status;;
    -x|--cancel) action=idle;;
    -h|--help) usage; exit 0;;
    -v|--version) version; exit 0;;
    /dev/md/*|md/*) arrays="${arrays:+$arrays }md${opt#*md/}";;
    /dev/md*|md*) arrays="${arrays:+$arrays }${opt#/dev/}";;
    /sys/block/md*) arrays="${arrays:+$arrays }${opt#/sys/block/}";;
    --) :;;
    *) echo "$PROGNAME: invalid option: $opt" >&2; usage; exit 0;;
    esac
done
if [ ! -f /proc/mdstat ]; then
    echo "$PROGNAME: MD subsystem not loaded, or /proc unavailable." >&2
    exit 1
fi
if [ ! -d /sys/block ]; then
    echo "$PROGNAME: /sys filesystem not available." >&2
    exit 2
fi
if [ -z "$(ls /sys/block/md* 2>/dev/null)" ]; then
    echo "$PROGNAME: no active MD arrays found." >&2
    exit 3
fi
if [ -z "$(ls /sys/block/md*/md/level 2>/dev/null)" ]; then
    echo "$PROGNAME: kernel too old, no support for redundancy checks." >&2
    exit 4
fi
if ! egrep -q '^raid([1456]|10)$' /sys/block/md*/md/level 2>/dev/null; then
    echo "$PROGNAME: no redundant arrays present." >&2
    exit 5
fi
if [ -z "$(ls /sys/block/md*/md/sync_action 2>/dev/null)" ]; then
    echo "$PROGNAME: no kernel support for redundancy checks." >&2
    exit 6
fi
[ $all = 1 ] && arrays="$(ls -d1 /sys/block/md* | cut -d/ -f4)"
    for array in $arrays; do
        SYNC_ACTION_CTL=/sys/block/$array/md/sync_action
        if [ ! -e $SYNC_ACTION_CTL ]; then
            echo "$PROGNAME: skipping non-redundant array $array." >&2
            continue
        fi
        cur_status="$(cat $SYNC_ACTION_CTL)"
        if [ $action = status ]; then
            echo "$array: $cur_status"
            continue
        fi
        if [ ! -w $SYNC_ACTION_CTL ]; then
            echo "$PROGNAME: $SYNC_ACTION_CTL not writeable." >&2
            exit 7
        fi
        if [ "$(cat /sys/block/$array/md/array_state)" = 'read-auto' ]; then
            echo "$PROGNAME: array $array in auto-read-only state, skipping." >&2
            continue
        fi
        case "$action" in
        check)
            if [ "$cur_status" != idle ]; then
                echo "$PROGNAME: array $array not idle, skipping." >&2
                continue
            fi
            echo $action > $SYNC_ACTION_CTL
            echo "$PROGNAME: check queued for array $array."
            ;;
        idle)
            echo $action > $SYNC_ACTION_CTL
            echo "$PROGNAME: cancel request queued for array $array."
            ;;
        esac
    done
exit 0