allow a couple more environment variables to be set before running QA.
[xfstests-dev.git] / tools / auto-qa
1 #!/bin/sh
2 #
3 # Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of version 2 of the GNU General Public License as
7 # published by the Free Software Foundation.
8
9 # This program is distributed in the hope that it would be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 # Further, this software is distributed without any warranty that it is
14 # free of the rightful claim of any third person regarding infringement
15 # or the like.  Any license provided herein, whether implied or
16 # otherwise, applies only to this software file.  Patent licenses, if
17 # any, provided herein do not apply to combinations of this program with
18 # other software, or any other product whatsoever.
19
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write the Free Software Foundation, Inc., 59
22 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
23
24 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 # Mountain View, CA  94043, or:
26
27 # http://www.sgi.com 
28
29 # For further information regarding this notice, see: 
30
31 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32 #
33
34 # automatic qa system. 31/08/00 dxm@sgi.com
35
36 # configuration (you could tune this)
37
38 BOOT="/boot"
39 SOAK_PASSES="-1"
40 SOAK_STRESS="10000"
41 SOAK_PROC="3"
42 export EXTRA=${EXTRA:=-xfs-qa}
43 export MKFS_OPTIONS=${MKFS_OPTIONS:=-bsize=4096}
44 export MOUNT_OPTIONS=${MOUNT_OPTIONS:=-ologbufs=2}
45 export MALLOCLIB=${MALLOCLIB:=/usr/lib/libefence.a}
46
47 _log()
48 {
49     echo "$*" >&2
50     echo "$*" >> $LOG
51 }
52
53 _fail()
54 {
55     if [ "$started" = "1" ] 
56     then
57         echo "auto-qa stopped" | wall
58         started=0
59     fi
60
61     _log "$*"
62     
63     # send special email if a cron'd qa run fails
64     case $state
65     in
66         cron*)
67             mail -s "XFS QA status report" $ADMINEMAIL \
68                 < $LOG 2>&1
69         ;;
70     esac
71
72     status=1
73     exit 1
74 }
75
76 _get_kernel_version()
77 {
78     [ -x "$WORKAREA" ] \
79         || _fail "can't access workarea $WORKAREA"
80     [ -r "$WORKAREA/linux/Makefile" ] \
81         || _fail "can't read makefile $WORKAREA/linux/Makefile"
82
83     eval `awk '
84         BEGIN { FS = "[ \t=]+" }
85         /^VERSION =/ { a=$2 }
86         /^PATCHLEVEL =/ { b=$2 }
87         /^SUBLEVEL =/ { c=$2 }
88         /^EXTRAVERSION =/ { d=$2 }
89         END { 
90             print "VERSION=" a "." b "." c d " ; SVERSION=" a "." b "." c
91         }
92    ' < $WORKAREA/linux/Makefile`
93 }
94
95 # this should be constant
96
97 ROOT="$HOME/qa"
98 HOST=`hostname -s`
99 if [ ! -z "$CVSROOT" ]; then
100     WORKAREA="$ROOT/linux-2.4-xfs"
101 else
102     [ -z "$WORKAREA" ] && WORKAREA="$ROOT/linux-xfs"
103 fi
104 export WORKAREA
105
106
107 export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/ptools:/usr/local/bin"
108 STATE=$ROOT/qa.state
109 QADIR="$WORKAREA/cmd/xfstests"
110 SUDO="su -c"
111 CONFIG="$ROOT/$HOST.config"
112 COMMON_CONFIG="$WORKAREA/cmd/xfstests/common.config"
113 SH="/bin/sh"
114 LOG="$ROOT/qa.log"
115
116 # need to add auto-qa hosts here
117
118 case $HOST
119 in
120     bruce)
121         EMAIL="nathans@larry"
122         ADMINEMAIL="nathans@larry"
123         MODULAR=0
124         ;;
125     sagan)
126         EMAIL="tes@larry"
127         ADMINEMAIL="tes@larry"
128         MODULAR=1
129         ;;
130     troppo)
131         EMAIL="kaos@larry"
132         ADMINEMAIL="kaos@larry"
133         MODULAR=0
134         ;;
135     frodo)
136         EMAIL="nathans@larry"
137         ADMINEMAIL="nathans@larry"
138         MODULAR=0
139         ;;
140     *)
141         _fail "auto-qa: no configuration information for host '$HOST'"
142         ;;
143 esac
144
145 # do some cleanup on exit
146
147 _cleanup()
148 {
149     umount $SCRATCH_DEV &> /dev/null
150     umount $TEST_DEV &> /dev/null
151     if [ "$started" = 1 ]
152     then
153         echo "auto-qa stopped" | wall
154         started=0
155     fi
156 }
157 status=1
158 trap "_cleanup; exit \$status" 0 1 2 3 15
159
160 # clean exit
161
162 _success()
163 {
164     status=0
165     exit 0
166 }
167
168 _get_state()
169 {
170     state=`cat $STATE`
171 }
172
173 _set_state()
174 {
175     echo $1 > $STATE
176     _get_state
177 }
178
179 _change_state()
180 {
181     new=$1
182     
183     case $state
184     in
185         *-*)
186             case $new
187             in
188                 *-*)
189                     _set_state $new
190                     ;;
191                 *)
192                     _set_state `echo $state | sed "s/-.*$/-$new/"`
193                     ;;
194             esac
195             ;;
196         *)
197             _set_state $new
198             ;;
199     esac
200 }
201
202 _sudo()
203 {
204     $ROOT/su -c "$*" < /dev/null ;# HACK - we need a hacked su at the mo
205 }
206
207 _restart()
208 {
209     exec $ROOT/su -c "(shutdown -r now \"auto-qa rebooting\" )&" < /dev/null
210 }
211
212 _update_autoqa_file()
213 {
214     SELF="$ROOT/auto-qa"
215     SELF_UPDATE="cmd/xfstests/tools/auto-qa"
216     if [ -z "$CVSROOT" ]; then
217             cmd="p_tupdate $SELF_UPDATE"
218     else
219             cmd="cvs -z3 update $SELF_UPDATE"
220     fi
221     exec $SH -c "cd $WORKAREA ; $cmd; chmod +x $SELF_UPDATE ; exec $SELF"
222 }
223
224 _update_workarea()
225 {
226     if [ -z "$CVSROOT" ]; then
227         _log "  *** p_tupdate"
228         cd $WORKAREA 
229         p_tupdate 2>&1 \
230                         || _fail "          !!! p_tupdate failed"
231
232         _log "  *** p_check/p_purge"
233         cd $WORKAREA 
234         p_check -s | p_purge -yiu 2>&1 \
235                         || _fail "          !!! p_check/p_purge failed"
236
237         _log "  *** non-trunk files"
238         cd $WORKAREA 
239         p_list -c 2>&1 \
240                         || _fail "          !!! p_list failed"
241     else
242         _log "  *** cvs update"
243         cd $WORKAREA
244         cvs -z3 update -d
245     fi
246 }
247             
248
249 _log "*** linux-xfs QA (`date`)"
250
251 _get_state
252
253 # check preconditions for starting state
254 case $1
255 in
256     cron-init)
257         case $state
258         in
259             *done)
260                 ;;
261             *)
262                 _fail "    !!! cron-init while not in \"*done\" state"
263                 ;;
264         esac
265         ;;
266     cron-restarted)
267         # we don't auto restart after reboot, but cron the restart
268         # to happen a bit later - it's much easier and safer that way
269         if [ "$state" != "cron-restarted" ]
270         then
271             _fail "    !!! cron-restarted while not in \"cron-restarted\" state"
272         fi
273         ;;
274 esac
275
276 if [ "$1" != "" ]
277 then
278     _set_state $1
279 fi
280
281 [ "$UID" -eq 0 ]        && _fail "    !!! QA most be run as a normal user"
282 [ -d "$ROOT" ]          || _fail "    !!! QA root \"$ROOT\" not found"
283 [ -d "$WORKAREA" ]      || _fail "    !!! QA workarea \"$WORKAREA\" not found"
284 [ -r "$CONFIG" ]        || _fail "    !!! Can't read config file $CONFIG"
285 . "$COMMON_CONFIG"      || _fail "    !!! Couldn't source $COMMON_CONFIG"
286
287 _get_kernel_version
288 IMAGE="$BOOT/vmlinuz$EXTRA"
289 SYSTEMMAP="$BOOT/System.map-$VERSION$EXTRA"
290 MODULES="/lib/modules/$SVERSION"
291
292 cd $ROOT
293
294 started=1
295 echo "auto-qa started" | wall
296
297 while true
298 do
299     _get_state
300
301     _log "    *** state $state start (`date`)"
302     _log "      (user=$USER, host=$HOST)"
303     new_state=""
304
305     case $state
306     in
307         *init)
308             echo "" > $ROOT/qa.log
309             echo "" > $ROOT/qa.full
310             _log "******************************************************"
311             _log "QA init $VERSION (`date`)"
312             _log "******************************************************"
313             _log "--- kernel ($IMAGE)"
314             _log "--- modules ($MODULES)"
315
316             _change_state "inited"
317             _update_autoqa_file
318             ;;
319             
320         *inited)
321             _log "      *** QA initialized"
322             new_state="update"
323             ;;
324         
325         *update)
326             _update_workarea
327             new_state="clean"
328             ;;
329
330         *clean)
331             # we need to configure or else we might fail to clean
332             for pkg in attr acl xfsprogs dmapi xfsdump xfstests
333             do
334                 cd $WORKAREA/cmd/$pkg
335                 _log "  *** clean $pkg tools"
336                 make realclean 2>&1 \
337                         || _fail "          !!! clean $pkg failed"
338             done
339
340             _log "      *** clean linux"
341             cd $WORKAREA/linux
342             make mrproper 2>&1 \
343                         || _fail "          !!! clean linux failed"
344
345             _log "      *** install configuration file"
346             cp -f $CONFIG $WORKAREA/linux/.config 2>&1 \
347                         || _fail "          !!! failed to install config"
348             
349             _log "      *** remove version file"
350             rm -f include/linux/version.h 2>&1 \
351                         || _fail "          !!! failed to clean version"
352
353             new_state="reconfig"
354             ;;
355             
356         *reconfig)
357         
358             _log "      *** reconfig kernel"
359             
360             _change_state "clean" ; # we better start from scratch if this fails
361             
362             cd $WORKAREA/linux
363             # we want to use default options for any new config options.
364             echo -e "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" | \
365                 make EXTRAVERSION=$EXTRA oldconfig 2>&1 \
366                         || _fail "          !!! reconfig oldconfig failed"
367             make EXTRAVERSION=$EXTRA dep 2>&1 \
368                         || _fail "          !!! reconfig dep failed"
369
370             new_state="buildtools"
371             ;;
372             
373         *buildtools)
374             _log "      *** build and install tools"
375             for pkg in attr acl xfsprogs dmapi xfsdump xfstests 
376             do
377                 cd $WORKAREA/cmd/$pkg
378
379                 # use e-fence - but this will only take effect on configure
380                 make configure 2>&1 \
381                         || _fail "          !!! configure $pkg failed"
382                 make default 2>&1 \
383                         || _fail "          !!! build $pkg failed"
384
385                 _sudo make install install-dev 2>&1 \
386                         || _fail "          !!! install $pkg failed"
387
388                 # attr and acl now have install-lib targets as well
389                 [ "$pkg" == "attr" -o "$pkg" == "acl" ] || continue
390                 _sudo make install-lib 2>&1 \
391                         || _fail "          !!! install $pkg failed"
392             done
393
394             new_state="buildkernel"
395             ;;
396
397         *buildkernel)
398             _log "      *** build kernel"
399             
400             _change_state "clean" ; # we better start from scratch if this fails
401             
402             cd $WORKAREA/linux
403             make -j4 EXTRAVERSION=$EXTRA bzImage modules 2>&1 \
404                         || _fail "          !!! build bzImage/modules failed"
405             new_state="install"
406             ;;
407
408         *install)
409             _log "      *** blat old modules"
410             
411             _sudo rm -rf $MODULES
412             
413             _log "      *** install kernel"
414             cd $WORKAREA/linux
415             _sudo cp -f $WORKAREA/linux/arch/i386/boot/bzImage $IMAGE 2>&1 \
416                         || _fail "          !!! install kernel failed"
417             _sudo cp -f $WORKAREA/linux/System.map $SYSTEMMAP 2>&1 \
418                         || _fail "          !!! install kernel failed"
419             _sudo make EXTRAVERSION=$EXTRA modules_install 2>&1 \
420                         || _fail "          !!! install modules failed"
421
422             _log "      *** reinit lilo"
423             _sudo /sbin/lilo 2>&1 \
424                         || _fail "          !!! reinit lilo failed"
425             new_state="restart"
426             ;;
427             
428         *restart)
429             _log "          *** select qa kernel"
430             _sudo /sbin/lilo -R linux-xfs-qa 2>&1 \
431                         || _fail "          !!! lilo failed"
432             
433             _log "          *** prepare to restart"
434             _change_state "restarted"
435             
436             _log "          *** restarting"
437
438             _restart # doesn't return
439             ;;
440             
441         *restarted)
442             _log "          *** QA reentered after restart"
443             
444             new_state="check"
445             ;;
446           
447         *check)
448             uname=`uname -ar`
449             _log "          *** uname $uname"
450             _log "          *** user tools"
451             ls -l /sbin/*xfs* /usr/sbin/*xfs* 2>&1
452             _log "          *** kernel"
453             ls -l /boot/*$EXTRA*  2>&1
454             _log "          *** kernel modules"
455             ls -l /lib/modules/$SVERSION$EXTRA/kernel/fs/xfs/*
456
457             if [ "$MODULAR" -eq 0 ]
458             then
459                 new_state="reset"
460             else
461                 new_state="probe"
462             fi
463             ;;
464             
465         *probe)
466             _log "          *** modules dependencies"
467             
468             _sudo depmod -a  2>&1 \
469                         || _fail "          !!! failed to depmod -a" 
470             
471             _log "          *** unmounting XFS mounts"
472             
473             _sudo umount -a -t xfs 2>&1
474             
475             _log "          *** removing modules"
476             
477             for m in xfsidbg xfs kdbm_pg kdbm_vm
478             do
479                 _sudo rmmod $m 2> /dev/null
480             done
481             
482             _log "          *** installing modules"
483
484             _sudo modprobe xfs 2>&1 \
485                         || _fail "          !!! failed to modprobe xfs"
486
487             new_state="reset"
488             ;;
489             
490         *reset)
491             
492             _log "          *** unmounting TEST_DEV"
493             
494             _sudo umount $TEST_DEV 2>&1
495             
496             _log "          *** unmounting SCRATCH_DEV"
497             
498             _sudo umount $SCRATCH_DEV 2>&1
499             
500             _log "          *** clean TEST_DEV"
501             
502             _sudo mkfs -t xfs -f $MKFS_OPTIONS $TEST_DEV 2>&1 \
503                         || _fail "          !!! failed to mkfs TEST_DEV"
504             
505             _log "          *** mounting TEST_DEV"
506             
507             _sudo mount -t xfs $TEST_DEV $TEST_DIR 2>&1 \
508                         || _fail "          !!! failed to mount"
509                                     
510             new_state="run"
511             ;;
512             
513             
514         soak-run)
515             cd $QADIR
516             
517             _log "          *** run soak test"
518             _sudo ./soak $SOAK_PASSES $SOAK_STRESS $SOAK_PROC\
519                         || _fail "          !!! failed to run soak test"
520
521             new_state="done"
522             ;;
523             
524         *run)
525             cd $QADIR
526             
527             _log "          *** run tests"
528             _sudo ./check -l -g auto 2>&1 | tee $ROOT/qa.out
529             
530             _log ""
531             _log "          *** send status mail"
532             mail -s "XFS QA status report" $EMAIL < $ROOT/qa.out 2>&1
533         
534             new_state="done"
535             ;;
536             
537         *done)
538             _log "*** QA run complete"
539
540             _success
541             ;;
542             
543         *nothing)
544             new_state="done"
545             _log "    *** do nothing"
546             ;;
547             
548         *)
549             _fail "        !!! unknown state $state"
550             ;;
551     esac
552
553     _log "    *** state $state done (`date`)"
554     [ "$new_state" = "" ] && _fail "    !!! no new state set"
555     _change_state $new_state
556     
557 done