summaryrefslogtreecommitdiff
path: root/debian/checkarray
blob: 2043d8cd5601617fff78aefd4dbc3f2768a9fc5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/sh
#
# checkarray -- initiates a check run of an MD array's redundancy information.
#
# Copyright © martin f. krafft <madduck@debian.org>
# distributed under the terms of the Artistic Licence 2.0
#
set -eu

PROGNAME=${0##*/}

about()
{
  echo "$PROGNAME -- MD array (RAID) redundancy checker tool"
  echo "Copyright © martin f. krafft <madduck@debian.org>"
  echo "Released under the terms of the Artistic Licence 2.0"
}

usage()
{
  about
  echo
  echo "Usage: $PROGNAME [options] [arrays]"
  echo
  echo "Valid options are:"
  cat <<-_eof | column -s\& -t
	-a|--all & check all assembled arrays (check /proc/mdstat).
	-s|--status & print redundancy check status of devices.
	-x|--cancel & queue a request to cancel a running redundancy check.
	-i|--idle & perform check in a lowest scheduling class (idle)
	-l|--slow & perform check in a lower-than-standard scheduling class
	-f|--fast & perform check in higher-than-standard scheduling class
	--realtime & perform check in real-time scheduling class (DANGEROUS!)
	-c|--cron & honour AUTOCHECK setting in /etc/default/mdadm.
	-q|--quiet & suppress informational messages.
	-Q|--real-quiet & suppress all output messages, including warnings and errors.
	-h|--help & show this output.
	-V|--version & show version information.
	_eof
  echo
  echo "Examples:"
  echo "  $PROGNAME --all --idle"
  echo "  $PROGNAME --quiet /dev/md[123]"
  echo "  $PROGNAME -sa"
  echo "  $PROGNAME -x --all"
  echo
  echo "Devices can be specified in almost any format. The following are"
  echo "all equivalent:"
  echo "  /dev/md0, md0, /dev/md/0, /sys/block/md0"
  echo
  echo "The --all option overrides all arrays passed to the script."
  echo
  echo "You can also control the status of a check with /proc/mdstat ."
}

SHORTOPTS=achVqQsxilf
LONGOPTS=all,cron,help,version,quiet,real-quiet,status,cancel,idle,slow,fast,realtime

eval set -- $(getopt -o $SHORTOPTS -l $LONGOPTS -n $PROGNAME -- "$@")

arrays=''
cron=0
all=0
quiet=0
status=0
action=check
ionice=

for opt in $@; do
  case "$opt" in
    -a|--all) all=1;;
    -s|--status) action=status;;
    -x|--cancel) action=idle;;
    -i|--idle) ionice=idle;;
    -l|--slow) ionice=low;;
    -f|--fast) ionice=high;;
    --realtime) ionice=realtime;;
    -c|--cron) cron=1;;
    -q|--quiet) quiet=1;;
    -Q|--real-quiet) quiet=2;;
    -h|--help) usage; exit 0;;
    -V|--version) about; 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: E: invalid option: $opt" >&2; usage >&2; exit 0;;
  esac
done

is_true()
{
  case "${1:-}" in
    [Yy]es|[Yy]|1|[Tt]rue|[Tt]) return 0;;
    *) return 1;
  esac
}

DEBIANCONFIG=/etc/default/mdadm
[ -r $DEBIANCONFIG ] && . $DEBIANCONFIG
if [ $cron = 1 ] && ! is_true ${AUTOCHECK:-false}; then
  [ $quiet -lt 1 ] && echo "$PROGNAME: I: disabled in $DEBIANCONFIG ." >&2
  exit 0
fi

if [ ! -f /proc/mdstat ]; then
  [ $quiet -lt 2 ] && echo "$PROGNAME: E: MD subsystem not loaded, or /proc unavailable." >&2
  exit 2
fi

if [ ! -d /sys/block ]; then
  [ $quiet -lt 2 ] && echo "$PROGNAME: E: /sys filesystem not available." >&2
  exit 7
fi

if [ -z "$(ls /sys/block/md* 2>/dev/null)" ]; then
  if [ $quiet -lt 2 ] && [ $cron != 1 ]; then
    echo "$PROGNAME: W: no active MD arrays found." >&2
    echo "$PROGNAME: W: (maybe uninstall the mdadm package?)" >&2
  fi
  exit 0
fi

if [ -z "$(ls /sys/block/md*/md/level 2>/dev/null)" ]; then
  [ $quiet -lt 2 ] && echo "$PROGNAME: E: kernel too old, no support for redundancy checks." >&2
  exit 6
fi

if ! egrep -q '^raid([1456]|10)$' /sys/block/md*/md/level 2>/dev/null; then
  [ $quiet -lt 1 ] && echo "$PROGNAME: I: no redundant arrays present; skipping checks..." >&2
  exit 0
fi

if [ -z "$(ls /sys/block/md*/md/sync_action 2>/dev/null)" ]; then
  [ $quiet -lt 2 ] && echo "$PROGNAME: E: no kernel support for redundancy checks." >&2
  exit 3
fi

[ $all = 1 ] && arrays="$(ls -d1 /sys/block/md* | cut -d/ -f4)"

for array in $arrays; do
  MDBASE=/sys/block/$array/md

  if [ ! -e $MDBASE/sync_action ]; then
    [ $quiet -lt 1 ] && echo "$PROGNAME: I: skipping non-redundant array $array." >&2
    continue
  fi

  read cur_status < $MDBASE/sync_action

  if [ $action = status ]; then
    echo "$array: $cur_status"
    continue
  fi

  if [ ! -w $MDBASE/sync_action ]; then
    [ $quiet -lt 2 ] && echo "$PROGNAME: E: $MDBASE/sync_action not writeable." >&2
    exit 4
  fi

  if [ "$(cat $MDBASE/array_state)" = 'read-auto' ]; then
    [ $quiet -lt 1 ] && echo "$PROGNAME: W: array $array in auto-read-only state, skipping..." >&2
    continue
  fi

  case "$action" in
    idle)
      echo $action > $MDBASE/sync_action
      [ $quiet -lt 1 ] && echo "$PROGNAME: I: cancel request queued for array $array." >&2
      ;;

    check)
      if [ "$cur_status" != idle ]; then
        [ $quiet -lt 2 ] && echo "$PROGNAME: W: array $array not idle, skipping..." >&2
        continue
      fi

      # queue request for the array. The kernel will make sure that these requests
      # are properly queued so as to not kill one of the array.
      echo $action > $MDBASE/sync_action
      [ $quiet -lt 1 ] && echo "$PROGNAME: I: check queued for array $array." >&2

      case "$ionice" in
        idle) ioarg='-c3'; renice=15;;
        low) ioarg='-c2 -n7'; renice=5;;
        high) ioarg='-c2 -n0'; renice=0;;
        realtime) ioarg='-c1 -n4'; renice=-5;;
        *) break;;
      esac

      resync_pid= wait=5
      while [ $wait -gt 0 ]; do
        wait=$((wait - 1))
        resync_pid=$(ps -ef | awk -v dev=$array 'BEGIN { pattern = "^\\[" dev "_resync]$" } $8 ~ pattern { print $2 }')
        if [ -n "$resync_pid" ]; then
          [ $quiet -lt 1 ] && echo "$PROGNAME: I: selecting $ionice I/O scheduling class and $renice niceness for resync of $array." >&2
          ionice -p "$resync_pid" $ioarg || :
          renice -n $renice -p "$resync_pid" 1>/dev/null || :
          break
        fi
        sleep 1
      done
      ;;
  esac

done

exit 0