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