]> TLD Linux GIT Repositories - packages/mdadm.git/blob - mdadm-checkarray
- updated to 4.3
[packages/mdadm.git] / mdadm-checkarray
1 #!/bin/sh
2 #
3 # checkarray -- initiates a check run of an MD array's redundancy information.
4 #
5 # Copyright © martin f. krafft <madduck@debian.org>
6 # distributed under the terms of the Artistic Licence 2.0
7 #
8 set -eu
9
10 PROGNAME=${0##*/}
11
12 about()
13 {
14   echo "$PROGNAME -- MD array (RAID) redundancy checker tool"
15   echo '$Id$'
16   echo "Copyright (C) martin f. krafft <madduck@debian.org>"
17   echo "Released under the terms of the Artistic Licence 2.0"
18 }
19
20 usage()
21 {
22   about
23   echo
24   echo "Usage: $PROGNAME [options] [arrays]"
25   echo
26   echo "Valid options are:"
27   cat <<-_eof | column -s\& -t
28         -a|--all & check all assembled arrays (check /proc/mdstat).
29         -c|--cron & honour AUTOCHECK setting in /etc/sysconfig/mdadm.
30         -s|--status & print redundancy check status of devices.
31         -x|--cancel & queue a request to cancel a running redundancy check.
32         -q|--quiet & suppress informational messages.
33         -Q|--real-quiet & suppress all output messages, including warnings and errors.
34         -h|--help & show this output.
35         -V|--version & show version information.
36         _eof
37   echo
38   echo "Examples:"
39   echo "  $PROGNAME --all"
40   echo "  $PROGNAME --quiet /dev/md[123]"
41   echo "  $PROGNAME -sa"
42   echo "  $PROGNAME -x --all"
43   echo
44   echo "Devices can be specified in almost any format. The following are"
45   echo "all equivalent:"
46   echo "  /dev/md0, md0, /dev/md/0, /sys/block/md0"
47   echo
48   echo "The --all option overrides all arrays passed to the script."
49   echo
50   echo "You can also control the status of a check with /proc/mdstat ."
51 }
52
53 SHORTOPTS=achVqQsx
54 LONGOPTS=all,cron,help,version,quiet,real-quiet,status,cancel
55
56 if [ $# -eq 0 ]; then
57         usage >&2
58         exit 0
59 fi
60
61 eval set -- $(getopt -o $SHORTOPTS -l $LONGOPTS -n $PROGNAME -- "$@")
62
63 arrays=''
64 cron=0
65 all=0
66 quiet=0
67 status=0
68 action=check
69
70 for opt in "$@"; do
71   case "$opt" in
72     -a|--all) all=1;;
73     -c|--cron) cron=1;;
74     -s|--status) action=status;;
75     -x|--cancel) action=idle;;
76     -h|--help) usage; exit 0;;
77     -q|--quiet) quiet=1;;
78     -Q|--real-quiet) quiet=2;;
79     -V|--version) about; exit 0;;
80     /dev/md/*|md/*) arrays="${arrays:+$arrays }md${opt#*md/}";;
81     /dev/md*|md*) arrays="${arrays:+$arrays }${opt#/dev/}";;
82     /sys/block/md*) arrays="${arrays:+$arrays }${opt#/sys/block/}";;
83     --) :;;
84     *) echo "$PROGNAME: E: invalid option: $opt" >&2; usage >&2; exit 0;;
85   esac
86 done
87
88 is_true()
89 {
90   case "${1:-}" in
91     [Yy]es|[Yy]|1|[Tt]rue|[Tt]) return 0;;
92     *) return 1;
93   esac
94 }
95
96 DEBIANCONFIG=/etc/sysconfig/mdadm
97 [ -r $DEBIANCONFIG ] && . $DEBIANCONFIG
98 if [ $cron = 1 ] && ! is_true ${AUTOCHECK:-false}; then
99   [ $quiet -lt 1 ] && echo "$PROGNAME: I: disabled in $DEBIANCONFIG ." >&2
100   exit 0
101 fi
102
103 if [ ! -f /proc/mdstat ]; then
104   [ $quiet -lt 2 ] && echo "$PROGNAME: E: MD subsystem not loaded, or /proc unavailable." >&2
105   exit 2
106 fi
107
108 if [ ! -d /sys/block ]; then
109   [ $quiet -lt 2 ] && echo "$PROGNAME: E: /sys filesystem not available." >&2
110   exit 7
111 fi
112
113 if [ -z "$(ls /sys/block/md* 2>/dev/null)" ]; then
114   if [ $quiet -lt 2 ] && [ $cron != 1 ]; then
115     echo "$PROGNAME: W: no active MD arrays found." >&2
116     echo "$PROGNAME: W: (maybe uninstall the mdadm package?)" >&2
117   fi
118   exit 5
119 fi
120
121 if [ -z "$(ls /sys/block/md*/md/level 2>/dev/null)" ]; then
122   [ $quiet -lt 2 ] && echo "$PROGNAME: E: kernel too old, no support for redundancy checks." >&2
123   exit 6
124 fi
125
126 if ! egrep -q '^raid([1456]|10)$' /sys/block/md*/md/level 2>/dev/null; then
127   [ $quiet -lt 1 ] && echo "$PROGNAME: I: no redundant arrays present; skipping checks..." >&2
128   exit 0
129 fi
130
131 if [ -z "$(ls /sys/block/md*/md/sync_action 2>/dev/null)" ]; then
132   [ $quiet -lt 2 ] && echo "$PROGNAME: E: no kernel support for redundancy checks." >&2
133   exit 3
134 fi
135
136 if [ $all = 1 ]; then
137         arrays="$(ls -d1 /sys/block/md* | cut -d/ -f4)"
138 elif [ -z "$arrays" ]; then
139         echo "$PROGNAME: E: specify array devices in command line." >&2
140         exit 0
141 fi
142
143 for array in $arrays; do
144   SYNC_ACTION_CTL=/sys/block/$array/md/sync_action
145
146   if [ ! -e $SYNC_ACTION_CTL ]; then
147     [ $quiet -lt 1 ] && echo "$PROGNAME: I: skipping non-redundant array $array." >&2
148     continue
149   fi
150
151   cur_status="$(cat $SYNC_ACTION_CTL)"
152
153   if [ $action = status ]; then
154     echo "$array: $cur_status"
155     continue
156   fi
157
158   if [ ! -w $SYNC_ACTION_CTL ]; then
159     [ $quiet -lt 2 ] && echo "$PROGNAME: E: $SYNC_ACTION_CTL not writeable." >&2
160     exit 4
161   fi
162
163   case "$action" in
164     idle)
165       echo $action > $SYNC_ACTION_CTL
166       [ $quiet -lt 1 ] && echo "$PROGNAME: I: cancel request queued for array $array." >&2
167       ;;
168
169     check)
170       if [ "$cur_status" != idle ]; then
171         [ $quiet -lt 2 ] && echo "$PROGNAME: W: array $array not idle, skipping..." >&2
172         continue
173       fi
174
175       # queue request for the array. The kernel will make sure that these requests
176       # are properly queued so as to not kill one of the array.
177       echo $action > $SYNC_ACTION_CTL
178       [ $quiet -lt 1 ] && echo "$PROGNAME: I: check queued for array $array." >&2
179       ;;
180   esac
181
182 done
183
184 exit 0