tests out getfattr/setfattr with XFS - different namespaces, different
[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
423 # setup the .out file link, depending on which form of quota is
424 # enabled as this often influences how the test output appears.
425 # [NB: SCRATCH_DEV must be mounted for this to work]
426
427 _setup_seq_out()
428 {
429     # this lets us phase use of this into the dump/restore tests easier...
430     [ -f $seq.ugquota -a -f $seq.noquota -a $seq.usrquota -a $seq.grpquota ] \
431         || return
432
433     rm -f $seq.out
434     if src/feature -U $SCRATCH_DEV
435     then
436         if src/feature -G $SCRATCH_DEV
437         then
438             ln $seq.ugquota $seq.out
439         else
440             ln $seq.usrquota $seq.out
441         fi
442     elif src/feature -G $SCRATCH_DEV
443     then
444         ln $seq.grpquota $seq.out
445     else
446         ln $seq.noquota $seq.out
447     fi
448 }
449
450
451 # remount a FS to a new mode (ro or rw)
452 #
453 _remount()
454 {
455     if [ $# -ne 2 ]
456     then
457         echo "Usage: _remount device ro/rw" 1>&2
458         exit 1
459     fi
460     device=$1
461     mode=$2
462     
463     if ! mount -o remount,$mode $device
464     then
465         echo "_remount: failed to remount filesystem on $device as $mode"
466         exit 1
467     fi
468     
469     # we might like to check here later
470     #options=`_fs_options $device`
471
472 }
473
474 # run xfs_check on a FS. 
475 #
476 # if the filesystem is mounted, it's either remounted ro before being
477 # checked or it's unmounted and then remounted
478 #
479
480 USE_REMOUNT=0
481
482 _check_fs()
483 {
484     if [ $# -ne 1 ]
485     then
486         echo "Usage: _check_fs device" 1>&2
487         exit 1
488     fi
489     
490     device=$1
491     type=`_fs_type $device`
492     ok=1
493     
494     if [ "$type" = "xfs" ]
495     then
496         # mounted... 
497         
498         if [ $USE_REMOUNT -eq 0 ]
499         then
500             mountpoint=`_xfs_mounted $device`
501             umount $device
502         else
503             _remount $device ro
504         fi
505     fi
506
507     xfs_logprint -t $device 2>&1 | 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     
521     xfs_check $device 2>&1 | _fix_malloc >$tmp.fs_check 
522     if [ -s $tmp.fs_check ]
523     then
524         echo "_check_fs: filesystem on $device is inconsistent (c) (see $seq.full)"
525         
526         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
527         echo "*** xfs_check output ***"                         >>$seq.full
528         cat $tmp.fs_check                                       >>$seq.full
529         echo "*** end xfs_check output"                         >>$seq.full
530         
531         ok=0
532     fi
533     
534     if ! xfs_repair -n $device >$tmp.fs_check 2>&1
535     then
536         echo "_check_fs: filesystem on $device is inconsistent (r) (see $seq.full)"
537         
538         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
539         echo "*** xfs_repair -n output ***"                     >>$seq.full
540         cat $tmp.fs_check | _fix_malloc                         >>$seq.full
541         echo "*** end xfs_repair output"                        >>$seq.full
542         
543         ok=0
544     fi
545     rm -f $tmp.fs_check
546     
547     if [ $ok -eq 0 ]
548     then
549         echo "*** mount output ***"                             >>$seq.full
550         mount                                                   >>$seq.full
551         echo "*** end mount output"                             >>$seq.full
552     fi
553     
554     if [ "$type" = "xfs" ]
555     then
556         # mounted... 
557         if [ $USE_REMOUNT -eq 0 ]
558         then
559             if ! mount -t xfs $device $mountpoint
560             then
561                 echo "!!! failed to remount $device on $mountpoint"
562                 ok=0
563             fi
564         else
565             _remount $device rw
566         fi
567     fi
568     
569     [ $ok -eq 0 ] && exit 1
570     return 0    
571 }
572
573 ################################################################################
574
575 [ -d /usr/bsd ] && PATH=$PATH:/usr/bsd
576 [ -d /usr/freeware/bin ] && PATH=$PATH:/usr/freeware/bin
577
578 if [ "$iam" != new ]
579 then
580
581     # make some further configuration checks here
582     
583     if [ "$TEST_DEV" = ""  ]
584     then
585         echo "common.rc: Error: \$TEST_DEV is not set"
586         exit 1
587     fi
588     
589     # if $TEST_DEV is not mounted, mount it now as XFS
590     if [ -z "`_fs_type $TEST_DEV`" ]
591     then
592         # $TEST_DEV is not mounted
593         if ! mount -t xfs $TEST_DEV $TEST_DIR
594         then
595             echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
596             exit 1
597         fi
598     fi
599     
600     if [ "`_fs_type $TEST_DEV`" != "xfs" ]
601     then
602         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED XFS filesystem"
603         df -T $TEST_DEV
604         exit 1
605     fi
606
607 fi
608
609 # check for some required biunaries on our $PATH
610 #
611 for exec in mkfs.xfs xfs_logprint xfs_check xfs_repair xfs_db
612 do
613     if which $exec >/dev/null 2>&1 
614     then
615         :
616     else
617         echo "common.rc: cannot find $exec on \$PATH=$PATH"
618         exit 1
619     fi
620 done
621
622 # make sure this script returns success
623 /bin/true