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