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