QA test updates.
[xfstests-dev.git] / common.rc
1 ##/bin/sh
2
3 #
4 # Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of version 2 of the GNU General Public License as
8 # published by the Free Software Foundation.
9
10 # This program is distributed in the hope that it would be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 # Further, this software is distributed without any warranty that it is
15 # free of the rightful claim of any third person regarding infringement
16 # or the like.  Any license provided herein, whether implied or
17 # otherwise, applies only to this software file.  Patent licenses, if
18 # any, provided herein do not apply to combinations of this program with
19 # other software, or any other product whatsoever.
20
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write the Free Software Foundation, Inc., 59
23 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
24
25 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
26 # Mountain View, CA  94043, or:
27
28 # http://www.sgi.com 
29
30 # For further information regarding this notice, see: 
31
32 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #
34
35 # we need common.config
36 if ! . ./common.config
37 then
38     echo "$iam: failed to source common.config"
39     exit 1
40 fi
41
42 # make sure we have a standard umask
43 umask 022
44
45 # awk
46 AWK_PROG=awk
47
48 # extra parameters for fsstress
49 FSSTRESS_AVOID="-f resvsp=0 -f unresvsp=0"
50
51 export AWK_PROG FSSTRESS_AVOID
52
53 # we override mount and mkfs.xfs so we can specify extra options
54
55 mount()
56 {
57     case "$*"
58     in
59         *remount*)
60             /bin/mount $*
61             ;;
62         *ext2*)
63             /bin/mount $*
64             ;;
65         *xfs*)
66             /bin/mount $* $MOUNT_OPTIONS
67             ;;
68         *)
69             /bin/mount $*
70             ;;
71     esac
72 }
73
74 _test_mount()
75 {
76     TEST_OPTIONS=""
77     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
78         TEST_OPTIONS="$TEST_OPTIONS -ortdev=$TEST_RTDEV"
79     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
80         TEST_OPTIONS="$TEST_OPTIONS -ologdev=$TEST_LOGDEV"
81     mount -t xfs $TEST_OPTIONS $* $TEST_DEV $TEST_DIR
82 }
83
84 _scratch_mount_options()
85 {
86     SCRATCH_OPTIONS=""
87     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
88         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ortdev=$SCRATCH_RTDEV"
89     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
90         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ologdev=$SCRATCH_LOGDEV"
91     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
92 }
93
94 _scratch_mount()
95 {
96     SCRATCH_OPTIONS=""
97     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
98         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ortdev=$SCRATCH_RTDEV"
99     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
100         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ologdev=$SCRATCH_LOGDEV"
101     mount -t xfs $SCRATCH_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
102 }
103
104 _scratch_mkfs_options()
105 {
106     SCRATCH_OPTIONS=""
107     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
108         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -rrtdev=$SCRATCH_RTDEV"
109     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
110         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -llogdev=$SCRATCH_LOGDEV"
111     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
112 }
113
114 _scratch_mkfs_xfs()
115 {
116     SCRATCH_OPTIONS=""
117     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
118         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -rrtdev=$SCRATCH_RTDEV"
119     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
120         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -llogdev=$SCRATCH_LOGDEV"
121     /sbin/mkfs.xfs -f $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
122 }
123
124 _scratch_xfs_db_options()
125 {
126     SCRATCH_OPTIONS=""
127     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
128         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
129     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
130 }
131
132 _scratch_xfs_logprint()
133 {
134     SCRATCH_OPTIONS=""
135     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
136         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
137     /usr/sbin/xfs_logprint $SCRATCH_OPTIONS $* $SCRATCH_DEV
138 }
139
140 _scratch_xfs_repair()
141 {
142     SCRATCH_OPTIONS=""
143     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
144         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
145     /sbin/xfs_repair $SCRATCH_OPTIONS $* $SCRATCH_DEV
146 }
147
148 _get_pids_by_name()
149 {
150     if [ $# -ne 1 ]
151     then
152         echo "Usage: _get_pids_by_name process-name" 1>&2
153         exit 1
154     fi
155
156     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
157     # HH:MM:SS before the psargs field, use this as the search anchor.
158     #
159     # Matches with $1 (process-name) occur if the first psarg is $1
160     # or ends in /$1 ... the matching uses sed's regular expressions,
161     # so passing a regex into $1 will work.
162
163     ps $PS_ALL_FLAGS \
164     | sed -n \
165         -e 's/$/ /' \
166         -e 's/[         ][      ]*/ /g' \
167         -e 's/^ //' \
168         -e 's/^[^ ]* //' \
169         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
170         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
171 }
172
173 # fqdn for localhost
174 #
175 _get_fqdn()
176 {
177     host=`hostname`
178     nslookup $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
179 }
180
181 # fix malloc libs output
182 #
183 _fix_malloc()
184 {
185     # filter out the Electric Fence notice
186     perl -e '
187         while (<>) {
188             if (defined $o && /^\s+Electric Fence/) {
189                 chomp($o);
190                 print "$o";
191                 undef $o;
192                 next;
193             }
194             print $o if (defined $o);
195
196             $o=$_;
197         }
198         print $o if (defined $o);
199     '
200 }
201
202 # check if run as root
203 #
204 _need_to_be_root()
205 {
206     id=`id | sed -e 's/(.*//' -e 's/.*=//'`
207     if [ "$id" -ne 0 ]
208     then
209         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
210         exit 1
211     fi
212 }
213
214
215 #
216 # _df_device : get an IRIX style df line for a given device 
217 #
218 #       - returns "" if not mounted
219 #       - returns fs type in field two (ala IRIX)
220 #       - joins line together if split by fancy df formatting
221 #       - strips header etc
222 #
223
224 _df_device()
225 {
226     if [ $# -ne 1 ]
227     then
228         echo "Usage: _df_device device" 1>&2
229         exit 1
230     fi
231     
232     df -T 2>/dev/null | $AWK_PROG -v what=$1 '
233         match($1,what) && NF==1 { 
234             v=$1
235             getline
236             print v, $0
237             exit
238         }
239         match($1,what) {
240             print
241             exit
242         }
243     '
244 }
245
246 #
247 # _df_dir : get an IRIX style df line for device where a directory resides
248 #
249 #       - returns fs type in field two (ala IRIX)
250 #       - joins line together if split by fancy df formatting
251 #       - strips header etc
252 #
253
254 _df_dir()
255 {
256     if [ $# -ne 1 ]
257     then
258         echo "Usage: _df_dir device" 1>&2
259         exit 1
260     fi
261     
262     df -T $1 2>/dev/null | $AWK_PROG -v what=$1 '
263         NR == 2 && NF==1 { 
264             v=$1
265             getline 
266             print v, $0;
267             exit 0
268         }
269         NR == 2 {
270             print;
271             exit 0
272         }
273         {}
274     '
275     # otherwise, nada
276 }
277
278 # return percentage used disk space for mounted device
279
280 _used()
281 {
282     if [ $# -ne 1 ]
283     then
284         echo "Usage: _used device" 1>&2
285         exit 1
286     fi
287     
288     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
289 }
290
291 # return the FS type of a mounted device
292 #
293 _fs_type()
294 {
295     if [ $# -ne 1 ]
296     then
297         echo "Usage: _fs_type device" 1>&2
298         exit 1
299     fi
300     
301     _df_device $1 | $AWK_PROG '{ print $2 }'
302 }
303
304 # return the FS mount options of a mounted device
305 #
306 _fs_options()
307 {
308     if [ $# -ne 1 ]
309     then
310         echo "Usage: _fs_options device" 1>&2
311         exit 1
312     fi
313     
314     $AWK_PROG -v dev=$1 '
315         match($1,dev) { print $4 }
316     ' </proc/mounts
317     
318 }
319
320 # returns device number if a file is a block device
321 #
322 _is_block_dev()
323 {
324     if [ $# -ne 1 ]
325     then
326         echo "Usage: _is_block_dev dev" 1>&2
327         exit 1
328     fi
329     
330     [ -b $1 ] && src/lstat64 $1 | $AWK_PROG '/Device type:/ { print $9 }'
331 }
332
333 # Do a command, log it to $seq.full, optionally test return status
334 # and die if command fails. If called with one argument _do executes the
335 # command, logs it, and returns its exit status. With two arguments _do
336 # first prints the message passed in the first argument, and then "done"
337 # or "fail" depending on the return status of the command passed in the
338 # second argument. If the command fails and the variable _do_die_on_error
339 # is set to "always" or the two argument form is used and _do_die_on_error
340 # is set to "message_only" _do will print an error message to
341 # $seq.out and exit.
342
343 _do()
344 {
345   if [ $# -eq 1 ]; then
346     _cmd=$1
347   elif [ $# -eq 2 ]; then
348     _note=$1
349     _cmd=$2
350     echo -n "$_note... "
351   else
352     echo "Usage: _do [note] cmd" 1>&2
353     status=1; exit
354   fi
355
356   (eval "echo '---' \"$_cmd\"") >>$seq.full
357   (eval "$_cmd") >$tmp._out 2>&1; ret=$?
358   cat $tmp._out | _fix_malloc >>$seq.full
359   if [ $# -eq 2 ]; then
360     if [ $ret -eq 0 ]; then
361       echo "done"
362     else
363       echo "fail"
364     fi
365   fi
366   if [ $ret -ne 0  ] \
367      && [ "$_do_die_on_error" = "always" \
368          -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
369   then
370       [ $# -ne 2 ] && echo
371       eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
372       status=1; exit
373   fi
374
375   return $ret
376 }
377
378 # bail out, setting up .notrun file
379
380 _notrun()
381 {
382     echo "$*" >$seq.notrun
383     echo "$seq not run: $*"
384     status=0
385     exit
386 }
387
388 # just plain bail out
389
390 _fail()
391 {
392     echo "$*" | tee -a $seq.full
393     echo "(see $seq.full for details)"
394     status=1
395     exit 1
396 }
397
398 # this test needs a scratch partition - check we're ok & unmount it
399
400 _require_scratch()
401 {
402     if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
403     then
404         _notrun "this test requires a valid \$SCRATCH_DEV"
405     fi
406     
407     if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
408     then
409         _notrun "this test requires a valid \$SCRATCH_DEV"
410     fi
411     
412     # mounted?
413     if mount | grep -q $SCRATCH_DEV
414     then 
415         # if it's mounted, make sure its on $SCRATCH_MNT
416         if ! mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
417         then 
418             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
419             exit 1
420         fi
421         # and then unmount it
422         if ! umount $SCRATCH_DEV
423         then
424             echo "failed to unmount $SCRATCH_DEV"
425             exit 1
426         fi
427     fi
428 }
429
430 # this test needs a logdev 
431
432 _require_logdev()
433 {
434     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
435         _notrun "This test requires a valid \$SCRATCH_LOGDEV" 
436     [ "$USE_EXTERNAL" != yes ] && \
437         _notrun "This test requires USE_EXTERNAL to be enabled"
438
439     # ensure its not mounted
440     umount $SCRATCH_LOGDEV 2>/dev/null
441 }
442
443 # this test requires loopback device support
444
445 _require_loop()
446 {
447     modprobe loop >/dev/null 2>&1
448     if grep loop /proc/devices >/dev/null 2>&1
449     then
450         :
451     else
452         _notrun "This test requires loopback device support"
453     fi
454 }
455
456 # check that a FS is mounted as XFS. if so, return mount point
457 #
458 _xfs_mounted()
459 {
460     if [ $# -ne 1 ]
461     then
462         echo "Usage: _xfs_mounted device" 1>&2
463         exit 1
464     fi
465     
466     device=$1
467     
468     if mount | grep $device | $AWK_PROG '
469         /type xfs/  { print $3 ; exit 0 }
470         END         { exit 1 }
471     '
472     then
473         echo "_xfs_mounted: $device is not a mounted XFS FS"
474         exit 1
475     fi
476 }
477
478
479 # setup the .out file link, depending on which form of quota is
480 # enabled as this often influences how the test output appears.
481 # [NB: SCRATCH_DEV must be mounted for this to work]
482
483 _setup_seq_out()
484 {
485     # this lets us phase use of this into the dump/restore tests easier...
486     [ -f $seq.ugquota -a -f $seq.noquota -a $seq.usrquota -a $seq.grpquota ] \
487         || return
488
489     rm -f $seq.out
490     if src/feature -U $SCRATCH_DEV
491     then
492         if src/feature -G $SCRATCH_DEV
493         then
494             ln $seq.ugquota $seq.out
495         else
496             ln $seq.usrquota $seq.out
497         fi
498     elif src/feature -G $SCRATCH_DEV
499     then
500         ln $seq.grpquota $seq.out
501     else
502         ln $seq.noquota $seq.out
503     fi
504 }
505
506
507 # remount a FS to a new mode (ro or rw)
508 #
509 _remount()
510 {
511     if [ $# -ne 2 ]
512     then
513         echo "Usage: _remount device ro/rw" 1>&2
514         exit 1
515     fi
516     device=$1
517     mode=$2
518     
519     if ! mount -o remount,$mode $device
520     then
521         echo "_remount: failed to remount filesystem on $device as $mode"
522         exit 1
523     fi
524     
525     # we might like to check here later
526     #options=`_fs_options $device`
527
528 }
529
530 # run xfs_check and friends on a FS. 
531 #
532 # if the filesystem is mounted, it's either remounted ro before being
533 # checked or it's unmounted and then remounted
534 #
535
536 USE_REMOUNT=0
537
538 _check_filesystem()
539 {
540     if [ $# -ne 1 -a $# -ne 2 ]
541     then
542         echo "Usage: _check_fs device [logdevice]" 1>&2
543         exit 1
544     fi
545     
546     device=$1
547     if [ $# -eq 2 -a "$2" != "" ]; then
548         extra_log_options="-l$2"
549         extra_mount_options="-ologdev=$2"
550     fi
551     type=`_fs_type $device`
552     ok=1
553     
554     if [ "$type" = "xfs" ]
555     then
556         # mounted... 
557         
558         if [ $USE_REMOUNT -eq 0 ]
559         then
560             mountpoint=`_xfs_mounted $device`
561             umount $device
562         else
563             _remount $device ro
564         fi
565     fi
566
567     /usr/sbin/xfs_logprint -t $device $extra_log_options 2>&1 \
568                 | tee $tmp.fs_check | grep -q "<CLEAN>"
569     if [ $? -ne 0 ]
570     then
571         echo "_check_fs: filesystem on $device has dirty log (see $seq.full)"
572         
573         echo "_check_fs: filesystem on $device has dirty log"   >>$seq.full
574         echo "*** xfs_logprint -t output ***"                   >>$seq.full
575         cat $tmp.fs_check                                       >>$seq.full
576         echo "*** end xfs_logprint output"                      >>$seq.full
577         
578         ok=0
579     fi
580
581     /usr/sbin/xfs_check $device 2>&1 | _fix_malloc >$tmp.fs_check 
582     if [ -s $tmp.fs_check ]
583     then
584         echo "_check_fs: filesystem on $device is inconsistent (c) (see $seq.full)"
585         
586         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
587         echo "*** xfs_check output ***"                         >>$seq.full
588         cat $tmp.fs_check                                       >>$seq.full
589         echo "*** end xfs_check output"                         >>$seq.full
590         
591         ok=0
592     fi
593     
594     if ! /sbin/xfs_repair -n $device $extra_log_options >$tmp.fs_check 2>&1
595     then
596         echo "_check_fs: filesystem on $device is inconsistent (r) (see $seq.full)"
597         
598         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
599         echo "*** xfs_repair -n output ***"                     >>$seq.full
600         cat $tmp.fs_check | _fix_malloc                         >>$seq.full
601         echo "*** end xfs_repair output"                        >>$seq.full
602         
603         ok=0
604     fi
605     rm -f $tmp.fs_check
606     
607     if [ $ok -eq 0 ]
608     then
609         echo "*** mount output ***"                             >>$seq.full
610         mount                                                   >>$seq.full
611         echo "*** end mount output"                             >>$seq.full
612     elif [ "$type" = "xfs" ]
613     then
614         # mounted... 
615         if [ $USE_REMOUNT -eq 0 ]
616         then
617             if ! mount -t xfs $extra_mount_options $device $mountpoint
618             then
619                 echo "!!! failed to remount $device on $mountpoint"
620                 ok=0
621             fi
622         else
623             _remount $device rw
624         fi
625     fi
626     
627     [ $ok -eq 0 ] && exit 1
628     return 0    
629 }
630
631 _check_test_fs()
632 {
633     TEST_LOG=""
634     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
635         TEST_LOG="$TEST_LOGDEV"
636     _check_filesystem $TEST_DEV $TEST_LOG
637 }
638
639 _check_scratch_fs()
640 {
641     SCRATCH_LOG=""
642     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
643         SCRATCH_LOG="$SCRATCH_LOGDEV"
644     _check_filesystem $SCRATCH_DEV $SCRATCH_LOG
645 }
646
647 ################################################################################
648
649 [ -d /usr/bsd ] && PATH=$PATH:/usr/bsd
650 [ -d /usr/freeware/bin ] && PATH=$PATH:/usr/freeware/bin
651
652 if [ "$iam" != new ]
653 then
654
655     # make some further configuration checks here
656     
657     if [ "$TEST_DEV" = ""  ]
658     then
659         echo "common.rc: Error: \$TEST_DEV is not set"
660         exit 1
661     fi
662     
663     # if $TEST_DEV is not mounted, mount it now as XFS
664     if [ -z "`_fs_type $TEST_DEV`" ]
665     then
666         # $TEST_DEV is not mounted
667         if ! _test_mount
668         then
669             echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
670             exit 1
671         fi
672     fi
673     
674     if [ "`_fs_type $TEST_DEV`" != "xfs" ]
675     then
676         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED XFS filesystem"
677         df -T $TEST_DEV
678         exit 1
679     fi
680
681 fi
682
683 # check for some required binaries on our $PATH
684 #
685 for exec in mkfs.xfs xfs_logprint xfs_check xfs_repair xfs_db
686 do
687     if which $exec >/dev/null 2>&1 
688     then
689         :
690     else
691         echo "common.rc: cannot find $exec on \$PATH=$PATH"
692         exit 1
693     fi
694 done
695
696 # make sure this script returns success
697 /bin/true