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