5a885efe463884cf773c8c51e6d22fbb11272dcc
[xfstests-dev.git] / common.dump
1 #/bin/sh
2
3 #
4 # Functions useful for xfsdump/xfsrestore tests
5 #
6 # Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
7
8 # This program is free software; you can redistribute it and/or modify it
9 # under the terms of version 2 of the GNU General Public License as
10 # published by the Free Software Foundation.
11
12 # This program is distributed in the hope that it would be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
16 # Further, this software is distributed without any warranty that it is
17 # free of the rightful claim of any third person regarding infringement
18 # or the like.  Any license provided herein, whether implied or
19 # otherwise, applies only to this software file.  Patent licenses, if
20 # any, provided herein do not apply to combinations of this program with
21 # other software, or any other product whatsoever.
22
23 # You should have received a copy of the GNU General Public License along
24 # with this program; if not, write the Free Software Foundation, Inc., 59
25 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
26
27 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
28 # Mountain View, CA  94043, or:
29
30 # http://www.sgi.com 
31
32 # For further information regarding this notice, see: 
33
34 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #
36
37 # --- initializations ---
38 rm -f $here/$seq.full
39
40 if [ -n "$DEBUGDUMP" ]; then
41   _dump_debug=-v5
42   _restore_debug=-v5
43   _invutil_debug=-d
44 fi
45  
46 # Use dump/restore in qa directory for debugging
47 PATH="$here:$PATH"
48 export PATH
49 #which xfsdump
50 #which xfsrestore
51 #which xfsinvutil
52
53 # status returned for not run tests
54 NOTRUNSTS=2
55
56 # name those directories
57 dump_file=$tmp.dumpfile
58 # dump_file=$here/dumpfile #TEMP OVERRIDE DUMP FILE
59 dump_sdir=dumpdir
60 dump_dir=$SCRATCH_MNT/$dump_sdir
61 restore_sdir=restoredir
62 restore_dir=$SCRATCH_MNT/$restore_sdir
63 multi=3
64 dumptape=$TAPE_DEV
65 media_label="stress_tape_media"
66 session_label="stress_$seq"
67
68 nobody=4 # define this uid/gid as a number
69 do_quota_check=true # do quota check if quotas enabled
70
71 _need_to_be_root
72
73 # install our cleaner
74 trap "_cleanup; exit \$status" 0 1 2 3 15
75
76 # start inventory from a known base - move it aside for test
77 for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
78     if [ -d $dir ]; then
79         [ -d $dir.$seq ] && rm -rf $dir.$seq
80         mv $dir $dir.$seq
81     fi
82 done
83
84 have_mtvariable=false
85 [ `uname` = "Linux" ] && have_mtvariable=true
86
87 #
88 # do a remote/local mt
89 #
90 _mt()
91 {
92     op=$1
93     if _isrmt; then  
94         # REMOTE
95         _rmtdev=`echo $dumptape | $AWK_PROG -F: '{print $2}'`
96
97         if echo $dumptape | grep '@' >/dev/null; then
98             _spec=`echo $dumptape | $AWK_PROG -F: '{print $1}'`
99             _rmtuser=`echo $_spec | $AWK_PROG -F@ '{print $1}'`
100             _rmthost=`echo $_spec | $AWK_PROG -F@ '{print $2}'`
101             rsh -n -l $_rmtuser $_rmthost "mt -t $_rmtdev $op"
102         else
103             _rmthost=`echo $dumptape | $AWK_PROG -F: '{print $1}'`
104             rsh -n $_rmthost "mt -t $_rmtdev $op"
105         fi
106     else
107         #LOCAL
108         mt -t $dumptape $op
109     fi
110 }
111
112 _check_onl()
113 {
114     _limit=10
115     i=0
116     while [ $i -lt $_limit ]; do  
117         echo "Checking online..." >>$here/$seq.full
118         if _mt status >$tmp.status 2>&1; then
119             break; 
120         else
121             sleep 2 
122         fi
123         i=`expr $i + 1`
124     done
125
126
127     if [ $i -eq $_limit ]; then
128         echo "ERROR: mt -f $dumptape failed"
129         cat $tmp.status
130
131         echo "mt -f $dumptape failed" >$seq.notrun 
132         status=$NOTRUNSTS
133         exit
134     fi 
135
136
137     if egrep -i 'onl|ready' $tmp.status | grep -iv 'not ready' >/dev/null; then
138         :
139     else
140         echo "ERROR: $dumptape is not online"
141         cat $tmp.status
142
143         echo "dumptape, $dumptape, is not online" >$seq.notrun 
144         status=$NOTRUNSTS
145         exit
146     fi
147 }
148
149 _wait_tape()
150 {
151     echo "Wait for tape, $dumptape, ..." >>$here/$seq.full
152
153     i=0
154     while [ $i -lt 20 ]; do  
155         echo "Checking status..." >>$here/$seq.full
156         if _mt status 2>&1 | tee -a $here/$seq.full | egrep -i "onl|ready" >/dev/null; then
157             break; 
158         else
159             sleep 2 
160         fi
161         i=`expr $i + 1`
162     done
163 }
164
165 #
166 # Keep trying so we know we really have rewound
167 #
168 _rewind()
169 {
170     echo "Initiate rewind..." >>$here/$seq.full
171     _wait_tape
172     _mt rewind >/dev/null
173     _wait_tape
174 }
175
176 #
177 # Do a custom erase because: 
178 # (i) some machines don't support it
179 # (ii) some machines take forever to do it
180 #
181 _erase_soft()
182 {
183     echo "Erasing tape" | tee -a $here/$seq.full
184     _rewind
185     _mt weof 3
186     _rewind
187 }
188
189 _erase_hard()
190 {
191     echo "Erasing tape" | tee -a $here/$seq.full
192     _mt erase
193 }
194
195 _isrmt()
196 {
197     echo $dumptape | grep ':' >/dev/null
198 }
199
200 #
201 # Get tape ready
202 #
203 _set_variable()
204 {
205     $have_mtvariable || return
206
207     if _isrmt; then
208         :
209     else
210         # LOCAL
211         echo "Put scsi tape driver into variable block size mode"
212         mt -f $dumptape setblk 0
213     fi  
214 }
215
216 _require_tape()
217 {
218     dumptape=$1
219
220     if [ -z "$dumptape" ]; then
221         echo "This test requires a dump tape - none was specified"
222         echo "No dump tape specified" >$seq.notrun 
223         status=$NOTRUNSTS
224         exit
225     fi
226
227     _check_onl
228     _set_variable
229 }
230
231 _wipe_fs()
232 {
233     _require_scratch
234
235     _scratch_mkfs_xfs >>$here/$seq.full || _fail "mkfs failed"
236     _scratch_mount >>$here/$seq.full || _fail "mount failed"
237 }
238
239
240 # Cleanup created dirs and files
241 # Called by trap
242 #
243 _cleanup()
244 {
245     cd $here
246     rm -f $tmp.*
247
248     if [ -n "$DEBUGDUMP" ]; then
249         # save it for inspection
250         for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
251             [ -d $dir ] || continue
252             tar -cvf $seq.inventory.tar $dir
253             ls -lR $dir >$seq.inventory.ls
254         done
255     fi
256
257     # put inventory dir back
258     for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
259         [ -d $dir.$seq ] || continue
260         rm -rf $dir             # get rid of new one
261         mv $dir.$seq $dir
262     done
263
264     if [ $status -ne $NOTRUNSTS ]; then
265         # Sleep added to stop _check_scratch_fs from complaining that the
266         # scratch_dev is still busy
267         sleep 10
268
269         _check_scratch_fs
270     fi
271 }
272
273 #
274 # ensure that bulkstat data will
275 # match with incore data
276 # by forcing disk data to be written out
277 #
278 _stable_fs()
279 {
280     _saveddir=`pwd`; cd /
281     umount $SCRATCH_MNT >>$here/$seq.full || _fail "unmount failed"
282     _scratch_mount >>$here/$seq.full || _fail "mount failed"
283     cd $_saveddir
284 }
285
286 #
287 # Run fsstress to create a mixture of 
288 # files,dirs,links,symlinks
289 #
290 # Pinched from test 013.
291 #
292 _create_dumpdir_stress()
293 {
294     echo "Creating directory system to dump using fsstress."
295
296     _wipe_fs
297
298     _param="-f link=10 -f creat=10 -f mkdir=10 -f truncate=5 -f symlink=10"
299     _count=200
300     rm -rf $dump_dir
301     if ! mkdir $dump_dir; then
302         echo "    failed to mkdir $dump_dir"
303         status=1
304         exit
305     fi
306     echo ""
307     echo "-----------------------------------------------"
308     echo "fsstress : $_param"
309     echo "-----------------------------------------------"
310     if ! $here/ltp/fsstress $_param -s 1 $FSSTRESS_AVOID -n $_count -d $dump_dir >$tmp.out 2>&1
311     then
312         echo "    fsstress (count=$_count) returned $? - see $here/$seq.full"
313         
314         echo "--------------------------------------"       >>$here/$here/$seq.full
315         echo "output from fsstress:"                        >>$here/$here/$seq.full
316         echo "--------------------------------------"       >>$here/$here/$seq.full
317         cat $tmp.out                                        >>$here/$here/$seq.full
318         status=1
319     fi
320
321     _stable_fs
322 }
323
324 _mk_fillconfig1()
325 {
326     cat <<End-of-File >$tmp.config
327 # pathname      size in bytes   owner   group
328 #
329 small           10      $nobody $nobody
330 big             102400  daemon  sys
331 sub/small       10      bin     bin
332 sub/big         102400  $nobody sys
333 #
334 sub/a           1       $nobody $nobody
335 sub/b           2       $nobody $nobody
336 sub/c           4       $nobody $nobody
337 sub/d           8       $nobody $nobody
338 sub/e           16      $nobody $nobody
339 sub/f           32      $nobody $nobody
340 sub/g           64      $nobody $nobody
341 sub/h           128     $nobody $nobody
342 sub/i           256     $nobody $nobody
343 sub/j           512     $nobody $nobody
344 sub/k           1024    $nobody $nobody
345 sub/l           2048    $nobody $nobody
346 sub/m           4096    $nobody $nobody
347 sub/n           8192    $nobody $nobody
348 #
349 sub/a00         100     $nobody $nobody
350 sub/b00         200     $nobody $nobody
351 sub/c00         400     $nobody $nobody
352 sub/d00         800     $nobody $nobody
353 sub/e00         1600    $nobody $nobody
354 sub/f00         3200    $nobody $nobody
355 sub/g00         6400    $nobody $nobody
356 sub/h00         12800   $nobody $nobody
357 sub/i00         25600   $nobody $nobody
358 sub/j00         51200   $nobody $nobody
359 sub/k00         102400  $nobody $nobody
360 sub/l00         204800  $nobody $nobody
361 sub/m00         409600  $nobody $nobody
362 sub/n00         819200  $nobody $nobody
363 #
364 sub/a000        1000    $nobody $nobody
365 sub/e000        16000   $nobody $nobody
366 sub/h000        128000  $nobody $nobody
367 sub/k000        1024000 $nobody $nobody
368 End-of-File
369 }
370
371 _mk_fillconfig2()
372 {
373     cat <<End-of-File >$tmp.config
374 # pathname      size in bytes
375 #
376 smalll          10      $nobody $nobody
377 biggg           102400  $nobody $nobody
378 sub/smalll      10      $nobody $nobody
379 sub/biggg       102400  $nobody $nobody
380 End-of-File
381 }
382
383 _mk_fillconfig_perm()
384 {
385     # dir_guid: ugo=rwx,g+s on dir is for IRIX chmod(1)
386
387     cat <<End-of-File >$tmp.config
388 # pathname      size/dir  user group mode
389 #
390 file_suid       10      $nobody $nobody 04777
391 file_guid       10      $nobody $nobody 02777
392 file_sticky     10      $nobody $nobody 01777
393 file_mix1       10      $nobody $nobody 761
394 file_mix2       10      $nobody $nobody 642
395 dir_suid        d       $nobody $nobody 04777
396 dir_guid        d       $nobody $nobody ugo=rwx,g+s
397 dir_sticky      d       $nobody $nobody 01777
398 dir_mix1        d       $nobody $nobody 761
399 dir_mix2        d       $nobody $nobody 642
400 End-of-File
401 }
402
403 _mk_fillconfig_ea()
404 {
405     cat <<End-of-File >$tmp.config
406 # pathname      size    user    group    perm   name value namespace
407 #
408 smalll          10      $nobody $nobody  777    attr1 some_text   user 
409 biggg           102400  $nobody $nobody  777    attr2 some_text2  root
410 sub/smalll      10      $nobody $nobody  777    attr3 some_text3  user
411 sub/biggg       102400  $nobody $nobody  777    attr4 some_text4  root
412 dir             d       $nobody $nobody  777    attr5 dir_text    user
413 #
414 # Add more files so that there are more than the number
415 # of streams.
416 # There are bugs in dump/restore for # non-dir files < # streams
417 # It can be tested in another configuration.
418 # It is a pathalogical case.
419 #
420 sub/a           1       $nobody $nobody
421 sub/b           2       $nobody $nobody
422 sub/c           4       $nobody $nobody
423 sub/d           8       $nobody $nobody
424 sub/e           16      $nobody $nobody
425 sub/f           32      $nobody $nobody
426 sub/g           64      $nobody $nobody
427 sub/h           128     $nobody $nobody
428 sub/i           256     $nobody $nobody
429 sub/j           512     $nobody $nobody
430 sub/k           1024    $nobody $nobody
431 sub/l           2048    $nobody $nobody
432 sub/m           4096    $nobody $nobody
433 sub/n           8192    $nobody $nobody
434 End-of-File
435 }
436
437 #
438 # extended file attribute flags
439 #
440 _mk_fillconfig_xattr()
441 {
442     cat <<End-of-File >$tmp.config
443 # pathname      size    user    group    perm   name 
444 #
445 xflag_realtime  10      $nobody $nobody  777    XFS_XFLAG_REALTIME
446 xflag_prealloc  10      $nobody $nobody  777    XFS_XFLAG_PREALLOC
447 xflag_immutable 10      $nobody $nobody  777    XFS_XFLAG_IMMUTABLE
448 xflag_append    10      $nobody $nobody  777    XFS_XFLAG_APPEND
449 xflag_sync      10      $nobody $nobody  777    XFS_XFLAG_SYNC
450 xflag_noatime   10      $nobody $nobody  777    XFS_XFLAG_NOATIME
451 xflag_nodump    10      $nobody $nobody  777    XFS_XFLAG_NODUMP
452 xflag_hasattr   10      $nobody $nobody  777    XFS_XFLAG_HASATTR
453 End-of-File
454 }
455
456 #
457 # Create a bunch of directories/files of different sizes
458 # filled with data.
459 #
460 # Pinched from test 001.
461 #
462 _do_create_dumpdir_fill()
463 {
464     echo "Creating directory system to dump using src/fill."
465
466     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
467     cd $dump_dir
468
469     $verbose && echo -n "Setup "
470     sed -e '/^#/d' $tmp.config \
471     | while read file nbytes owner group perms ea_name ea_value namespace
472     do
473         if [ $nbytes = "d" ]; then
474             # create a directory
475             dir=$file   
476             if [ ! -d $dir ]
477             then
478                 if mkdir $dir
479                 then
480                     :
481                 else
482                     $verbose && echo
483                     echo "Error: cannot mkdir \"$dir\""
484                     exit 1
485                 fi
486             fi
487         else
488             # create a directory/file
489             dir=`dirname $file`
490             if [ "$dir" != "." ]
491             then
492                 if [ ! -d $dir ]
493                 then
494                     if mkdir $dir
495                     then
496                         :
497                     else
498                         $verbose && echo
499                         echo "Error: cannot mkdir \"$dir\""
500                         exit 1
501                     fi
502                 fi
503             fi
504             rm -f $file
505             if $here/src/fill $file $file $nbytes
506             then
507                 :
508             else
509                 $verbose && echo
510                 echo "Error: cannot create \"$file\""
511                 exit 1
512             fi
513         fi
514         if [ -n "$owner" -a -n "$group" ]; then
515             chown $owner.$group $file
516         fi
517         if [ -n "$perms" ]; then
518             chmod $perms $file
519         fi
520         # extended attributes (EA)
521         if [ -n "$ea_name" -a -n "$ea_value" ]; then
522             if [ "X$namespace" = "Xroot" ]; then
523                 attr -R -s $ea_name -V $ea_value $file
524             else
525                 attr -s $ea_name -V $ea_value $file
526             fi
527         # extended file attribute flags - no value - NOT EAs
528         elif [ -n "$ea_name" -a -z "$ea_value" ]; then
529             # set the flag
530             # TODO XXX
531             # use xfs_io to send the ioctl
532             :
533         fi
534         $verbose && echo -n "."
535     done
536     $verbose && echo
537
538     cd $here
539 }
540
541 _create_dumpdir_largefile()
542 {
543     _wipe_fs
544     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
545     _largesize=4294967297
546     _largefile=$dump_dir/largefile
547     echo "dd a largefile at offset $_largesize"
548     POSIXLY_CORRECT=yes \
549     dd if=/dev/zero of=$_largefile bs=1 seek=$_largesize count=10 2>&1
550     _stable_fs
551 }       
552
553 _create_dumpdir_fill()
554 {
555     _wipe_fs
556     _mk_fillconfig1
557     _do_create_dumpdir_fill
558     _stable_fs
559 }       
560
561 _create_dumpdir_fill2()
562 {
563     _wipe_fs
564     _mk_fillconfig2
565     _do_create_dumpdir_fill
566     _stable_fs
567 }       
568
569 _create_dumpdir_fill_perm()
570 {
571     _wipe_fs
572     _mk_fillconfig_perm
573     _do_create_dumpdir_fill
574     _stable_fs
575 }       
576
577 _create_dumpdir_fill_ea()
578 {
579     _wipe_fs
580     _mk_fillconfig_ea
581     _do_create_dumpdir_fill
582     _stable_fs
583 }       
584
585
586 #
587 # Append a subset of the fill'ed files
588 # So we can see if just these get dumped on an incremental
589 #
590 _append_dumpdir_fill()
591 {
592     cd $dump_dir
593     cat <<End-of-File >$tmp.config
594 # pathname
595 #
596 small   
597 sub/big 
598 #
599 sub/a
600 sub/c
601 sub/e
602 End-of-File
603     sed -e '/^#/d' $tmp.config \
604     | while read file
605     do
606         echo 'Extra text' >>$file
607     done
608
609     cd $here
610     _stable_fs
611 }
612
613 _do_create_dump_symlinks()
614 {
615     echo "Creating directory system of symlinks to dump."
616
617     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
618     cd $dump_dir
619
620     $verbose && echo -n "Setup "
621     sed -e '/^#/d' $tmp.config \
622     | while read file nbytes owner group owner2 group2 perms perms2
623     do
624         dir=`dirname $file`
625         if [ "$dir" != "." ]
626         then
627             if [ ! -d $dir ]
628             then
629                 if mkdir $dir
630                 then
631                     :
632                 else
633                     $verbose && echo
634                     echo "Error: cannot mkdir \"$dir\""
635                     exit 1
636                 fi
637             fi
638         fi
639         rm -f $file
640         touch $file
641
642         # Do chmod on symlink using umask.
643         # This won't do the right thing as it subtracts permissions.
644         # However, I don't care, as long as I get some different perms
645         # for testing.
646         if [ -n "$perms2" ]; then
647             omask=`umask`
648             umask $perms2
649         fi
650         ln -s $file $file-link
651         if [ -n "$perms2" ]; then
652             umask $omask        
653         fi
654
655         if [ -n "$owner" -a -n "$group" ]; then
656             chown $owner.$group $file
657         fi
658         if [ -n "$owner" -a -n "$group" ]; then
659             chown -h $owner.$group $file-link
660         fi
661         if [ -n "$perms" ]; then
662             chmod $perms $file
663         fi
664         $verbose && echo -n "."
665     done
666     $verbose && echo
667
668     cd $here
669 }
670
671 _mk_symlink_config()
672 {
673     cat <<End-of-File >$tmp.config
674 # path  size    owner1  group1  owner2  group2  perm1   perm2 
675 #
676 a       0       $nobody $nobody daemon  sys     124     421
677 b       0       daemon  sys     bin     bin     347     743
678 sub/a   0       bin     bin     $nobody sys     777     777
679 sub/b   0       $nobody sys     $nobody $nobody 367     763
680 End-of-File
681 }
682
683 _create_dumpdir_symlinks()
684 {
685     _wipe_fs
686     _mk_symlink_config
687     _do_create_dump_symlinks
688     _stable_fs
689 }       
690
691 #
692 # create hardlinks of form $_fname, $_fname_h1 $_fname_h2 ...
693 #
694 _create_hardlinks()
695 {
696     _fname=$1   
697     _numlinks=$2
698
699     touch $_fname
700     _j=1
701     while [ $_j -le $_numlinks ]; do
702         _suffix=_h$_j
703         _hardlink=$_fname$_suffix
704         echo "creating hardlink $_hardlink to $_fname"
705         ln $_fname $_hardlink
706         _j=`expr $_j + 1`
707     done
708 }
709
710 #
711 # create a set of hardlinks
712 # create hardlinks of form file1, file1_h1 file1_h2 ...
713 # create hardlinks of form file2, file2_h1 file2_h2 ...
714 # create hardlinks of form file3, file3_h1 file3_h2 ...
715 #
716 _create_hardset()
717 {
718     _numsets=$1
719     _i=1
720     while [ $_i -le $_numsets ]; do
721         _create_hardlinks file$_i 5
722         _i=`expr $_i + 1`
723     done
724 }
725
726
727 _modify_level()
728 {
729     _level=$1
730     echo "mod level $_level" >$dump_dir/file$_level
731 }
732
733 _create_dumpdir_hardlinks()
734 {
735     _numsets=$1
736     _wipe_fs
737     echo "Creating directory system of hardlinks to incrementally dump."
738
739     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
740     cd $dump_dir
741
742     _create_hardset $_numsets
743
744     cd $here
745     _stable_fs
746 }
747
748 #
749 # Filter for ls
750 # Filter out dates on symlinks and char devices
751 # Filter out size on directories because this can differ
752 # when transitioning to long inode numbers (ie. 64 bits).
753 #
754 _ls_filter()
755 {
756   $AWK_PROG '
757         /^l/ { date = $8; sub(date,"DATE"); print}
758         /^c/ { date = $9; sub(date,"DATE"); print}
759         /^d/ { size = $5; sub(size,"SIZE"); print}
760         {print}' \
761   | sed -e 's/total [0-9][0-9]*/total TOTAL/'
762 }
763
764
765
766 # Filter out the non-deterministic dump msgs from
767 # xfsdump and xfsrestore
768 #
769 _dump_filter_main()
770 {
771   sed \
772       -e "s/`hostname`/HOSTNAME/"   \
773       -e "s#$SCRATCH_DEV#SCRATCH_DEV#"    \
774       -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
775       -e "s#$dumptape#TAPE_DEV#"    \
776       -e "s#$SCRATCH_MNT#SCRATCH_MNT#"    \
777       -e "s#$dump_file#DUMP_FILE#"  \
778       -e 's#/var/lib/xfsdump#/var/xfsdump#' \
779       -e 's/id:[        ]*[0-9a-f-]*/id: ID/'  \
780       -e 's/time:[      ].*/time: TIME/'       \
781       -e 's/date:[      ].*/date: DATE/'       \
782       -e 's/dump begun .*/dump begun DATE/'    \
783       -e 's/[0-9][0-9]* seconds/SECS seconds/' \
784       -e 's/restore.[0-9][0-9]*/restore.PID/' \
785       -e 's/ino [0-9][0-9]*/ino INO/' \
786       -e '/: dump size/s/[0-9][0-9]*/NUM/'     \
787       -e '/dump size:/s/[0-9][0-9]*/NUM/'      \
788       -e '/dump size per stream:/s/[0-9][0-9]*/NUM/' \
789       -e 's/\(media file size[   ]*\)[0-9][0-9]*/\1NUM/' \
790       -e 's/\(mfile size:[       ]*\)[0-9][0-9]*/\1NUM/' \
791       -e '/drive[        ]*[0-9][0-9]*:/d' \
792       -e '/\/dev\/tty/d' \
793       -e '/inventory session uuid/d' \
794       -e '/ - Running single-threaded/d' \
795       -e '/^.*I\/O metrics: .*$/d' \
796       -e 's/1048576/BLOCKSZ/' \
797       -e 's/2097152/BLOCKSZ/' \
798       -e 's/(pid[        ]*[1-9][0-9]*)/\(pid PID\)/' \
799   | perl -ne '
800       if ($_ =~ /(?:Dump|Restore) Summary/) {
801         $skip = 1;
802       } elsif ($_ =~ /(?:Dump|Restore) Status/) {
803         $skip = 0;
804       }
805       print if (! $skip);'
806 }
807
808 _dump_filter()
809 {
810    if $do_quota_check
811    then
812        _dump_filter_main | _check_quota_dumprestore | _check_quota_entries
813    else
814        _dump_filter_main
815    fi
816 }
817
818 _invutil_filter()
819 {
820   _dump_filter_main \
821   | sed \
822         -e 's/UUID[     ]*:[    ][0-9a-f-]*/UUID                :       ID/' \
823         -e 's/TIME OF DUMP[     ]*:.*/TIME OF DUMP      :       TIME/' \
824         -e 's/HOSTNAME:SCRATCH_MNT.*/HOSTNAME:SCRATCH_MNT/' \
825         -e 's#inventory/[0-9a-f-]*#inventory/UUID#' \
826
827 }
828
829
830 _dir_filter()
831 {
832   sed \
833     -e "s#$dump_file#DUMP_FILE#"      \
834     -e "s#$SCRATCH_DEV#SCRATCH_DEV#"        \
835     -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
836     -e "s#$dumptape#TAPE_DEV#"         \
837     -e "s#$dump_dir#DUMP_DIR#g"       \
838     -e "s#$restore_dir#RESTORE_DIR#g" \
839     -e "s#$SCRATCH_MNT#SCRATCH_MNT#g"       \
840     -e "s#$dump_sdir#DUMP_SUBDIR#g"   \
841     -e "s#$restore_sdir#RESTORE_SUBDIR#g" \
842     -e "s#$$#PID#g" \
843
844 }
845
846 #
847 # Note: requires a space between option letter and argument 
848 #
849 _parse_args()
850 {
851     OPTIND=0
852     dump_args=""
853     while [ $# -gt 0 ]
854     do
855         case $1
856         in
857         -f)
858             [ -z "$2" ] && _fail "missing argument for -f"
859             dumptape=$2 
860             shift
861             ;;
862         -L)
863             [ -z "$2" ] && _fail "missing argument for -L"
864             session_label=$2
865             shift
866             ;;
867         -o)
868             dump_args="$dump_args -o"
869             ;;
870         -F)
871             dump_args="$dump_args -F"
872             ;;
873         --multi)
874             multi=$2
875             shift
876             ;;
877         -q)
878             do_quota_check=true
879             ;;
880         -Q)
881             do_quota_check=false
882             ;;
883         -l)
884             [ -z "$2" ] && _fail "missing argument for -l"
885             dump_args="$dump_args -l$2"
886             shift
887             ;;
888         *)
889             _fail "invalid argument to common.dump function: $1"
890             ;;
891         esac
892         shift
893     done
894 }
895
896
897 #
898 # Dump a subdir
899 #
900 _do_dump_sub()
901 {
902     _parse_args $*
903
904     echo "Dumping to tape..."
905     opts="$_dump_debug$dump_args -s $dump_sdir -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
906     echo "xfsdump $opts" | _dir_filter  
907     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
908 }
909
910 #
911 # Do dump to tape
912 #
913 _do_dump()
914 {
915     _parse_args $*
916
917     echo "Dumping to tape..."
918     opts="$_dump_debug$dump_args -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
919     echo "xfsdump $opts" | _dir_filter  
920     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
921 }
922
923
924 #
925 # Do full dump with -m
926 #
927 _do_dump_min()
928 {
929     _parse_args $*
930
931     echo "Dumping to tape..."
932     onemeg=1048576
933     opts="$_dump_debug$dump_args -m -b $onemeg -l0 -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
934     echo "xfsdump $opts" | _dir_filter  
935     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
936 }
937
938
939 #
940 # Do full dump to file
941 #
942 _do_dump_file()
943 {
944     _parse_args $*
945
946     echo "Dumping to file..."
947     opts="$_dump_debug$dump_args -f $dump_file -M $media_label -L $session_label $SCRATCH_MNT"
948     echo "xfsdump $opts" | _dir_filter  
949     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
950 }
951
952 #
953 # Do full dump to multiple files
954 #
955 _do_dump_multi_file()
956 {
957     _parse_args "$@"
958
959     multi_args=""
960
961     i=0
962     while [ $i -lt $multi ]
963     do
964         multi_args="$multi_args -f $dump_file.$i -M $media_label.$i"
965         i=`expr $i + 1`
966     done
967
968     echo "Dumping to files..."
969     opts="$_dump_debug$dump_args $multi_args -L $session_label $SCRATCH_MNT"
970     echo "xfsdump $opts" | _dir_filter  
971     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
972 }
973
974
975 _prepare_restore_dir()
976 {
977     rm -rf $restore_dir
978     mkdir $restore_dir || _fail "failed to mkdir $restore_dir"
979 }
980
981
982 #
983 # Get tape ready and restore dir
984 #
985 _prepare_restore()
986 {
987     _prepare_restore_dir
988
989     echo "Rewinding tape"
990     _rewind
991 }
992
993 #
994 # Restore the tape into $restore_dir
995 #
996 _do_restore()
997 {
998     _parse_args $*
999     _prepare_restore
1000
1001
1002     echo "Restoring from tape..."
1003     opts="$_restore_debug -f $dumptape  -L $session_label $restore_dir"
1004     echo "xfsrestore $opts" | _dir_filter  
1005     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1006 }
1007
1008 #
1009 # Restore the tape into $restore_dir using -m
1010 #
1011 _do_restore_min()
1012 {
1013     _parse_args $*
1014     _prepare_restore
1015
1016     echo "Restoring from tape..."
1017     onemeg=1048576
1018     opts="$_restore_debug -m -b $onemeg -f $dumptape  -L $session_label $restore_dir"
1019     echo "xfsrestore $opts" | _dir_filter  
1020     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1021 }
1022
1023 #
1024 # Restore the tape from a dump file
1025 #
1026 _do_restore_file()
1027 {
1028     _parse_args $*
1029     _prepare_restore_dir
1030
1031     echo "Restoring from file..."
1032     opts="$_restore_debug -f $dump_file  -L $session_label $restore_dir"
1033     echo "xfsrestore $opts" | _dir_filter  
1034     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1035 }
1036
1037 #
1038 # Cumulative restore from a file
1039 # Need to specify the dump level e.g. "-l 0"
1040 #
1041 _do_restore_file_cum()
1042 {
1043     _parse_args $*
1044     if echo $dump_args | grep '\-l0' >/dev/null; then
1045         _prepare_restore_dir
1046     fi
1047
1048     echo "Restoring cumumlative from file..."
1049     opts="$_restore_debug -f $dump_file -r $restore_dir"
1050     echo "xfsrestore $opts" | _dir_filter  
1051     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1052 }
1053
1054 _do_restore_toc()
1055 {
1056     echo "Contents of dump ..."
1057     opts="$_restore_debug -f $dump_file -t"
1058     echo "xfsrestore $opts" | _dir_filter
1059     cd $SCRATCH_MNT # for IRIX which needs xfs cwd
1060     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter_main |\
1061     _check_quota_file |\
1062     _check_quota_entries |\
1063     $AWK_PROG 'NF != 1 { print; next }
1064                {files = sprintf("%s\n%s", files, $1)}
1065                 END { print files | "sort" } '
1066     # the above awk code is to alpha sort only the output
1067     # of files (and not the verbose restore msgs)
1068     cd $here # put back
1069 }
1070
1071 #
1072 # Restore the tape from multiple dump files
1073 #
1074 _do_restore_multi_file()
1075 {
1076     _parse_args "$@"
1077     _prepare_restore_dir
1078
1079     multi_args=""
1080
1081     i=0
1082     while [ $i -lt $multi ]
1083     do
1084         multi_args="$multi_args -f $dump_file.$i"
1085         i=`expr $i + 1`
1086     done
1087
1088     echo "Restoring from file..."
1089     opts="$_restore_debug $multi_args -L $session_label $restore_dir"
1090     echo "xfsrestore $opts" | _dir_filter  
1091     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1092 }
1093
1094 #
1095 # Do xfsdump piped into xfsrestore - xfsdump | xfsrestore
1096 #
1097 # Use -s as we want to dump and restore to the same xfs partition
1098 #
1099 _do_dump_restore()
1100 {
1101     _parse_args $*
1102     _prepare_restore_dir
1103     echo "xfsdump|xfsrestore ..."
1104     restore_opts="$_restore_debug - $restore_dir"
1105     dump_opts="$_dump_debug$dump_args -s $dump_sdir - $SCRATCH_MNT"
1106     echo "xfsdump $dump_opts | xfsrestore $restore_opts" | _dir_filter  
1107     xfsdump $dump_opts 2>$tmp.dump.mlog | xfsrestore $restore_opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1108     _dump_filter <$tmp.dump.mlog
1109 }
1110
1111 #
1112 # Compare dumped subdirectory with restored dir
1113 # using ls -lR.
1114 # Thus no contents are compared but permissions, sizes,
1115 # owners, etc... are.
1116 #
1117 _ls_compare_sub()
1118 {
1119     #
1120     # verify we got back what we dumped
1121     #
1122     echo "Comparing listing of dump directory with restore directory"
1123     ls -lR $dump_dir | tee -a $here/$seq.full | _ls_filter >$tmp.dump_dir
1124     ls -lR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1125     | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1126
1127     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1128 }
1129
1130 #
1131 # filter out the date fields
1132 #
1133 _ls_nodate_filter()
1134 {
1135     $AWK_PROG 'NF == 9 { print $1, $2, $3, $4, $9 }'
1136 }
1137
1138 #
1139 # _ls_compare_sub but don't compare dates
1140 _ls_nodate_compare_sub()
1141 {
1142     #
1143     # verify we got back what we dumped
1144     #
1145     echo "Comparing listing of dump directory with restore directory"
1146     ls -lR $dump_dir | tee -a $here/$seq.full | _ls_filter | _ls_nodate_filter >$tmp.dump_dir
1147     ls -lR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1148     | _ls_nodate_filter | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1149
1150     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1151 }
1152
1153 #
1154 # Compare using recursive diff the files of the dumped
1155 # subdirectory.
1156 # This one will compare the contents.
1157 #
1158 _diff_compare_sub()
1159 {
1160     echo "Comparing dump directory with restore directory"
1161     diff -rs $dump_dir $restore_dir/$dump_sdir | _dir_filter
1162 }
1163
1164 _get_eas_on_path()
1165 {
1166     _path=$1
1167
1168 # Tim - this is the IRIX way...
1169     # find $_path -exec attr -l {} \; |\
1170     # awk '{print $9, $2}' |\
1171     # sed 's/["]//g' |\
1172     # sort |\
1173 # and this is now the Linux way...
1174     echo "User names"
1175     getfattr --absolute-names -Rh $_path |\
1176     perl -wn -e '
1177         if (m/^# file: (\S+)/) { $file = $1 }
1178         elsif (m/^user\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1179     sort |\
1180     while read file ea_name; do
1181         attr -g $ea_name $file
1182     done
1183
1184     echo "Root names"
1185     getfattr --absolute-names -Rh -m trusted $_path |\
1186     perl -wn -e '
1187         if (m/^# file: (\S+)/) { $file = $1 }
1188         elsif (m/^trusted\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1189     sort |\
1190     while read file ea_name; do
1191         attr -R -g $ea_name $file
1192     done
1193 }
1194
1195 #
1196 # Compare the extended attributes of the files/dirs
1197 # b/w the dumped and restore dirs.
1198 #
1199 #
1200 # Attribute "attr5" had a 8 byte value for /spare1/dump.5460/dir:
1201 # Attribute "attr5" had a 8 byte value for /spare1/restore.5460/dump.5460/dir:
1202 #
1203 _diff_compare_eas()
1204 {
1205     echo "Comparing dump directory with restore directory"
1206     echo "Looking at the extended attributes (EAs)"
1207     echo "EAs on dump"
1208     _get_eas_on_path $dump_dir | tee $seq.ea1 | _dir_filter
1209     echo "EAs on restore"
1210     _get_eas_on_path $restore_dir/$dump_sdir \
1211     | sed -e "s#$restore_sdir\/##" \
1212     | tee $seq.ea2 \
1213     | _dir_filter
1214     diff -s $seq.ea1 $seq.ea2
1215 }
1216
1217
1218 #
1219 # Compare using recursive diff the files of the dumped
1220 # filesystem
1221 #
1222 _diff_compare()
1223 {
1224     echo "Comparing dump directory with restore directory"
1225     diff -rs $SCRATCH_MNT $restore_dir | _dir_filter | _check_quota_diff
1226 }
1227
1228 #
1229 # Check out the dump inventory
1230 #
1231 _dump_inventory()
1232 {
1233     xfsdump $_dump_debug -I | tee -a $here/$seq.full | _dump_filter_main
1234 }
1235
1236 #
1237 # Do the xfsinvutil cmd with debug and filters
1238 # Need to set variable: "$middate" to the invutil date 
1239 #
1240 _do_invutil()
1241 {
1242     host=`hostname`
1243     echo "xfsinvutil $_invutil_debug -M $host:$SCRATCH_MNT \"$middate\" $*" >$here/$seq.full
1244     xfsinvutil $_invutil_debug $* -M $host:$SCRATCH_MNT "$middate" \
1245     | tee -a $here/$seq.full | _invutil_filter
1246 }
1247
1248 #
1249 # ensure we can find the user quota msg if user quotas are on
1250 # ensure we can find the group quota msg if group quotas are on
1251 #
1252 _check_quota()
1253 {
1254     usermsg=$1 
1255     groupmsg=$2 
1256     uquota=0
1257     gquota=0 
1258     $here/src/feature -U $SCRATCH_DEV && uquota=1
1259     $here/src/feature -G $SCRATCH_DEV && gquota=1
1260
1261     $AWK_PROG -v uquota=$uquota -v gquota=$gquota -v full=$here/$seq.full \
1262               -v usermsg="$usermsg" -v groupmsg="$groupmsg" '
1263         $0 ~ groupmsg {
1264                         print "Found group quota:", $0 >>full
1265                         found_gquota = 1
1266                         if (!gquota) {
1267                             print "Found extra:", $0
1268                         }
1269                         next
1270         }
1271         $0 ~ usermsg {
1272                         print "Found user quota:", $0 >>full
1273                         found_uquota = 1
1274                         if (!uquota) {
1275                             print "Found extra:", $0
1276                         }
1277                         next
1278         }
1279                         { print }
1280         END {
1281                 if (uquota && !found_uquota) {
1282                     print "Missing: ", usermsg
1283                 }
1284                 if (gquota && !found_gquota) {
1285                     print "Missing: ", groupmsg
1286                 }
1287         }
1288     '
1289 }
1290
1291 #
1292 # xfsrestore: 3 directories and 40 entries processed 
1293 #   $5 = 40 
1294 #   num entries needs to be reduced by num quota file(s) 
1295 #
1296 _check_quota_entries()
1297 {
1298     uquota=0
1299     gquota=0 
1300     $here/src/feature -U $SCRATCH_DEV && uquota=1
1301     $here/src/feature -G $SCRATCH_DEV && gquota=1
1302     $AWK_PROG -v uquota=$uquota -v gquota=$gquota '
1303         /entries processed/ { 
1304                 if (uquota) $5--
1305                 if (gquota) $5--
1306         }
1307         {print}'
1308 }
1309
1310 #
1311 # Look for:
1312 # xfsdump: saving user quota information for: SCRATCH_MNT
1313 # xfsdump: saving group quota information for: SCRATCH_MNT
1314 # xfsrestore: user quota information written to ...'
1315 # xfsrestore: group quota information written to ...'
1316 #
1317 _check_quota_dumprestore()
1318 {
1319    _check_quota 'user quota information' \
1320                 'group quota information'
1321 }
1322
1323 #
1324 # Look for:
1325 # Only in RESTORE_DIR: xfsdump_quotas
1326 # Only in RESTORE_DIR: xfsdump_quotas_group
1327 #
1328 _check_quota_diff()
1329 {
1330    _check_quota 'Only in RESTORE_DIR: xfsdump_quotas' \
1331        'Only in RESTORE_DIR: xfsdump_quotas_group' 
1332 }
1333
1334 #
1335 # Look for the quota file in the output
1336 # Ensure that it is there if it should be
1337 # Filter it out so that the output is always the same
1338 # even with no quotas
1339 #
1340 _check_quota_file()
1341 {
1342    _check_quota 'xfsdump_quotas' 'xfsdump_quotas_group'
1343 }
1344
1345 # make sure this script returns success
1346 /bin/true