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