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