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