Fix zpios-sanity.sh return code
[zfs.git] / scripts / zpios-sanity.sh
1 #!/bin/bash
2 #
3 # ZFS/ZPOOL configuration test script.
4
5 basedir="$(dirname $0)"
6
7 SCRIPT_COMMON=common.sh
8 if [ -f "${basedir}/${SCRIPT_COMMON}" ]; then
9 . "${basedir}/${SCRIPT_COMMON}"
10 else
11 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
12 fi
13
14 PROG=zpios-sanity.sh
15 HEADER=
16 FAILS=0
17
18 usage() {
19 cat << EOF
20 USAGE:
21 $0 [hvxfc]
22
23 DESCRIPTION:
24         ZPIOS sanity tests
25
26 OPTIONS:
27         -h      Show this message
28         -v      Verbose
29         -x      Destructive hd/sd/md/dm/ram tests
30         -f      Don't prompt due to -x
31         -c      Cleanup lo+file devices at start
32
33 EOF
34 }
35
36 while getopts 'hvxfc?' OPTION; do
37         case $OPTION in
38         h)
39                 usage
40                 exit 1
41                 ;;
42         v)
43                 VERBOSE=1
44                 ;;
45         x)
46                 DANGEROUS=1
47                 ;;
48         f)
49                 FORCE=1
50                 ;;
51         c)
52                 CLEANUP=1
53                 ;;
54         ?)
55                 usage
56                 exit
57                 ;;
58         esac
59 done
60
61 if [ $(id -u) != 0 ]; then
62         die "Must run as root"
63 fi
64
65 # Perform pre-cleanup is requested
66 if [ ${CLEANUP} ]; then
67         cleanup_loop_devices
68         rm -f /tmp/zpool.cache.*
69 fi
70
71 zpios_test() {
72         CONFIG=$1
73         TEST=$2
74         LOG=`mktemp`
75
76         ${ZPIOS_SH} -f -c ${CONFIG} -t ${TEST} &>${LOG}
77         if [ $? -ne 0 ]; then
78                 FAILS=1
79
80                 if [ ${VERBOSE} ]; then
81                         printf "FAIL:     %-13s\n" ${CONFIG}
82                         cat ${LOG}
83                 else
84                         if [ ! ${HEADER} ]; then
85                                 head -2 ${LOG}
86                                 HEADER=1
87                         fi
88
89                         printf "FAIL:     %-13s" ${CONFIG}
90                         tail -1 ${LOG}
91                 fi
92         else
93                 if [ ${VERBOSE} ]; then
94                         cat ${LOG}
95                 else
96                         if [ ! ${HEADER} ]; then
97                                 head -2 ${LOG}
98                                 HEADER=1
99                         fi
100
101                         tail -1 ${LOG}
102                 fi
103         fi
104
105         rm -f ${LOG}
106 }
107
108 if [ ${DANGEROUS} ] && [ ! ${FORCE} ]; then
109         cat << EOF
110 The -x option was passed which will result in UNRECOVERABLE DATA LOSS
111 on on the following block devices:
112
113   /dev/sd[abcd]
114   /dev/hda
115   /dev/ram0
116   /dev/md0
117   /dev/dm-0
118
119 To continue please confirm by entering YES:
120 EOF
121         read CONFIRM
122         if [ ${CONFIRM} != "YES" ] && [ ${CONFIRM} != "yes" ]; then
123                 exit 0;
124         fi
125 fi
126
127 #
128 # These configurations are all safe and pose no risk to any data on
129 # the system which runs them.  They will confine all their IO to a
130 # file in /tmp or a loopback device configured to use a file in /tmp.
131 #
132 SAFE_CONFIGS=(                                          \
133         file-raid0 file-raid10 file-raidz file-raidz2   \
134         lo-raid0 lo-raid10 lo-raidz lo-raidz2           \
135 )
136
137 #
138 # These configurations are down right dangerous.  They will attempt
139 # to use various real block devices on your system which may contain
140 # data you car about.  You are STRONGLY advised not to run this unless
141 # you are certain there is no data on the system you care about.
142 #
143 DANGEROUS_CONFIGS=(                                     \
144         hda-raid0                                       \
145         sda-raid0                                       \
146         ram0-raid0                                      \
147         md0-raid10 md0-raid5                            \
148         dm0-raid0                                       \
149 )
150
151 for CONFIG in ${SAFE_CONFIGS[*]}; do
152         zpios_test $CONFIG tiny
153 done
154
155 if [ ${DANGEROUS} ]; then
156         for CONFIG in ${DANGEROUS_CONFIGS[*]}; do
157                 zpios_test $CONFIG tiny
158         done
159 fi
160
161 exit $FAILS