Minor QA updates and a script for dumping local QA config information.
[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 _error()
232 {
233     echo "Error: $*" | tee -a $here/$seq.full
234     echo "(see $here/$seq.full for details)"
235     status=1
236     exit
237 }
238
239 _wipe_fs()
240 {
241     _require_scratch
242
243     _scratch_mkfs_xfs >>$here/$seq.full || _error "mkfs failed"
244     _scratch_mount >>$here/$seq.full || _error "mount failed"
245 }
246
247
248 # Cleanup created dirs and files
249 # Called by trap
250 #
251 _cleanup()
252 {
253     cd $here
254     rm -f $tmp.*
255
256     if [ -n "$DEBUGDUMP" ]; then
257         # save it for inspection
258         for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
259             [ -d $dir ] || continue
260             tar -cvf $seq.inventory.tar $dir
261             ls -lR $dir >$seq.inventory.ls
262         done
263     fi
264
265     # put inventory dir back
266     for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
267         [ -d $dir.$seq ] || continue
268         rm -rf $dir             # get rid of new one
269         mv $dir.$seq $dir
270     done
271
272     if [ $status -ne $NOTRUNSTS ]; then
273         # Sleep added to stop _check_scratch_fs from complaining that the
274         # scratch_dev is still busy
275         sleep 10
276
277         _check_scratch_fs
278     fi
279 }
280
281 #
282 # ensure that bulkstat data will
283 # match with incore data
284 # by forcing disk data to be written out
285 #
286 _stable_fs()
287 {
288     _saveddir=`pwd`; cd /
289     umount $SCRATCH_MNT >>$here/$seq.full || _error "unmount failed"
290     _scratch_mount >>$here/$seq.full || _error "mount failed"
291     cd $_saveddir
292 }
293
294 #
295 # Run src/fsstress to create a mixture of 
296 # files,dirs,links,symlinks
297 #
298 # Pinched from test 013.
299 #
300 _create_dumpdir_stress()
301 {
302     echo "Creating directory system to dump using src/fsstress."
303
304     _wipe_fs
305
306     _param="-f link=10 -f creat=10 -f mkdir=10 -f truncate=5 -f symlink=10"
307     _count=200
308     rm -rf $dump_dir
309     if ! mkdir $dump_dir; then
310         echo "    failed to mkdir $dump_dir"
311         status=1
312         exit
313     fi
314     echo ""
315     echo "-----------------------------------------------"
316     echo "fsstress : $_param"
317     echo "-----------------------------------------------"
318     if ! $here/src/fsstress $_param -s 1 $FSSTRESS_AVOID -n $_count -d $dump_dir >$tmp.out 2>&1
319     then
320         echo "    fsstress (count=$_count) returned $? - see $here/$seq.full"
321         
322         echo "--------------------------------------"       >>$here/$here/$seq.full
323         echo "output from fsstress:"                        >>$here/$here/$seq.full
324         echo "--------------------------------------"       >>$here/$here/$seq.full
325         cat $tmp.out                                        >>$here/$here/$seq.full
326         status=1
327     fi
328
329     _stable_fs
330 }
331
332 _mk_fillconfig1()
333 {
334     cat <<End-of-File >$tmp.config
335 # pathname      size in bytes   owner   group
336 #
337 small           10      $nobody $nobody
338 big             102400  daemon  sys
339 sub/small       10      bin     bin
340 sub/big         102400  $nobody sys
341 #
342 sub/a           1       $nobody $nobody
343 sub/b           2       $nobody $nobody
344 sub/c           4       $nobody $nobody
345 sub/d           8       $nobody $nobody
346 sub/e           16      $nobody $nobody
347 sub/f           32      $nobody $nobody
348 sub/g           64      $nobody $nobody
349 sub/h           128     $nobody $nobody
350 sub/i           256     $nobody $nobody
351 sub/j           512     $nobody $nobody
352 sub/k           1024    $nobody $nobody
353 sub/l           2048    $nobody $nobody
354 sub/m           4096    $nobody $nobody
355 sub/n           8192    $nobody $nobody
356 #
357 sub/a00         100     $nobody $nobody
358 sub/b00         200     $nobody $nobody
359 sub/c00         400     $nobody $nobody
360 sub/d00         800     $nobody $nobody
361 sub/e00         1600    $nobody $nobody
362 sub/f00         3200    $nobody $nobody
363 sub/g00         6400    $nobody $nobody
364 sub/h00         12800   $nobody $nobody
365 sub/i00         25600   $nobody $nobody
366 sub/j00         51200   $nobody $nobody
367 sub/k00         102400  $nobody $nobody
368 sub/l00         204800  $nobody $nobody
369 sub/m00         409600  $nobody $nobody
370 sub/n00         819200  $nobody $nobody
371 #
372 sub/a000        1000    $nobody $nobody
373 sub/e000        16000   $nobody $nobody
374 sub/h000        128000  $nobody $nobody
375 sub/k000        1024000 $nobody $nobody
376 End-of-File
377 }
378
379 _mk_fillconfig2()
380 {
381     cat <<End-of-File >$tmp.config
382 # pathname      size in bytes
383 #
384 smalll          10      $nobody $nobody
385 biggg           102400  $nobody $nobody
386 sub/smalll      10      $nobody $nobody
387 sub/biggg       102400  $nobody $nobody
388 End-of-File
389 }
390
391 _mk_fillconfig_perm()
392 {
393     # dir_guid: ugo=rwx,g+s on dir is for IRIX chmod(1)
394
395     cat <<End-of-File >$tmp.config
396 # pathname      size/dir  user group mode
397 #
398 file_suid       10      $nobody $nobody 04777
399 file_guid       10      $nobody $nobody 02777
400 file_sticky     10      $nobody $nobody 01777
401 file_mix1       10      $nobody $nobody 761
402 file_mix2       10      $nobody $nobody 642
403 dir_suid        d       $nobody $nobody 04777
404 dir_guid        d       $nobody $nobody ugo=rwx,g+s
405 dir_sticky      d       $nobody $nobody 01777
406 dir_mix1        d       $nobody $nobody 761
407 dir_mix2        d       $nobody $nobody 642
408 End-of-File
409 }
410
411 _mk_fillconfig_ea()
412 {
413     cat <<End-of-File >$tmp.config
414 # pathname      size    user    group    perm   name value namespace
415 #
416 smalll          10      $nobody $nobody  777    attr1 some_text   user 
417 biggg           102400  $nobody $nobody  777    attr2 some_text2  root
418 sub/smalll      10      $nobody $nobody  777    attr3 some_text3  user
419 sub/biggg       102400  $nobody $nobody  777    attr4 some_text4  root
420 dir             d       $nobody $nobody  777    attr5 dir_text    user
421 #
422 # Add more files so that there are more than the number
423 # of streams.
424 # There are bugs in dump/restore for # non-dir files < # streams
425 # It can be tested in another configuration.
426 # It is a pathalogical case.
427 #
428 sub/a           1       $nobody $nobody
429 sub/b           2       $nobody $nobody
430 sub/c           4       $nobody $nobody
431 sub/d           8       $nobody $nobody
432 sub/e           16      $nobody $nobody
433 sub/f           32      $nobody $nobody
434 sub/g           64      $nobody $nobody
435 sub/h           128     $nobody $nobody
436 sub/i           256     $nobody $nobody
437 sub/j           512     $nobody $nobody
438 sub/k           1024    $nobody $nobody
439 sub/l           2048    $nobody $nobody
440 sub/m           4096    $nobody $nobody
441 sub/n           8192    $nobody $nobody
442 End-of-File
443 }
444
445 #
446 # Create a bunch of directories/files of different sizes
447 # filled with data.
448 #
449 # Pinched from test 001.
450 #
451 _do_create_dumpdir_fill()
452 {
453     echo "Creating directory system to dump using src/fill."
454
455     mkdir -p $dump_dir ||\
456         _error "cannot mkdir \"$dump_dir\""
457     cd $dump_dir
458
459     $verbose && echo -n "Setup "
460     sed -e '/^#/d' $tmp.config \
461     | while read file nbytes owner group perms ea_name ea_value namespace
462     do
463         if [ $nbytes = "d" ]; then
464             # create a directory
465             dir=$file   
466             if [ ! -d $dir ]
467             then
468                 if mkdir $dir
469                 then
470                     :
471                 else
472                     $verbose && echo
473                     echo "Error: cannot mkdir \"$dir\""
474                     exit 1
475                 fi
476             fi
477         else
478             # create a directory/file
479             dir=`dirname $file`
480             if [ "$dir" != "." ]
481             then
482                 if [ ! -d $dir ]
483                 then
484                     if mkdir $dir
485                     then
486                         :
487                     else
488                         $verbose && echo
489                         echo "Error: cannot mkdir \"$dir\""
490                         exit 1
491                     fi
492                 fi
493             fi
494             rm -f $file
495             if $here/src/fill $file $file $nbytes
496             then
497                 :
498             else
499                 $verbose && echo
500                 echo "Error: cannot create \"$file\""
501                 exit 1
502             fi
503         fi
504         if [ -n "$owner" -a -n "$group" ]; then
505             chown $owner.$group $file
506         fi
507         if [ -n "$perms" ]; then
508             chmod $perms $file
509         fi
510         if [ -n "$ea_name" -a -n "$ea_value" ]; then
511             if [ "X$namespace" = "Xroot" ]; then
512                 attr -R -s $ea_name -V $ea_value $file
513             else
514                 attr -s $ea_name -V $ea_value $file
515             fi
516         fi
517         $verbose && echo -n "."
518     done
519     $verbose && echo
520
521     cd $here
522 }
523
524 _create_dumpdir_largefile()
525 {
526     _wipe_fs
527     mkdir -p $dump_dir ||\
528         _error "cannot mkdir \"$dump_dir\""
529     _largesize=4294967297
530     _largefile=$dump_dir/largefile
531     echo "dd a largefile at offset $_largesize"
532     dd if=/dev/zero of=$_largefile bs=1 seek=$_largesize count=10 2>&1
533     _stable_fs
534 }       
535
536 _create_dumpdir_fill()
537 {
538     _wipe_fs
539     _mk_fillconfig1
540     _do_create_dumpdir_fill
541     _stable_fs
542 }       
543
544 _create_dumpdir_fill2()
545 {
546     _wipe_fs
547     _mk_fillconfig2
548     _do_create_dumpdir_fill
549     _stable_fs
550 }       
551
552 _create_dumpdir_fill_perm()
553 {
554     _wipe_fs
555     _mk_fillconfig_perm
556     _do_create_dumpdir_fill
557     _stable_fs
558 }       
559
560 _create_dumpdir_fill_ea()
561 {
562     _wipe_fs
563     _mk_fillconfig_ea
564     _do_create_dumpdir_fill
565     _stable_fs
566 }       
567
568
569 #
570 # Append a subset of the fill'ed files
571 # So we can see if just these get dumped on an incremental
572 #
573 _append_dumpdir_fill()
574 {
575     cd $dump_dir
576     cat <<End-of-File >$tmp.config
577 # pathname
578 #
579 small   
580 sub/big 
581 #
582 sub/a
583 sub/c
584 sub/e
585 End-of-File
586     sed -e '/^#/d' $tmp.config \
587     | while read file
588     do
589         echo 'Extra text' >>$file
590     done
591
592     cd $here
593     _stable_fs
594 }
595
596 _do_create_dump_symlinks()
597 {
598     echo "Creating directory system of symlinks to dump."
599
600     mkdir -p $dump_dir ||\
601         _error "cannot mkdir \"$dump_dir\""
602     cd $dump_dir
603
604     $verbose && echo -n "Setup "
605     sed -e '/^#/d' $tmp.config \
606     | while read file nbytes owner group owner2 group2 perms perms2
607     do
608         dir=`dirname $file`
609         if [ "$dir" != "." ]
610         then
611             if [ ! -d $dir ]
612             then
613                 if mkdir $dir
614                 then
615                     :
616                 else
617                     $verbose && echo
618                     echo "Error: cannot mkdir \"$dir\""
619                     exit 1
620                 fi
621             fi
622         fi
623         rm -f $file
624         touch $file
625
626         # Do chmod on symlink using umask.
627         # This won't do the right thing as it subtracts permissions.
628         # However, I don't care, as long as I get some different perms
629         # for testing.
630         if [ -n "$perms2" ]; then
631             omask=`umask`
632             umask $perms2
633         fi
634         ln -s $file $file-link
635         if [ -n "$perms2" ]; then
636             umask $omask        
637         fi
638
639         if [ -n "$owner" -a -n "$group" ]; then
640             chown $owner.$group $file
641         fi
642         if [ -n "$owner" -a -n "$group" ]; then
643             chown -h $owner.$group $file-link
644         fi
645         if [ -n "$perms" ]; then
646             chmod $perms $file
647         fi
648         $verbose && echo -n "."
649     done
650     $verbose && echo
651
652     cd $here
653 }
654
655 _mk_symlink_config()
656 {
657     cat <<End-of-File >$tmp.config
658 # path  size    owner1  group1  owner2  group2  perm1   perm2 
659 #
660 a       0       $nobody $nobody daemon  sys     124     421
661 b       0       daemon  sys     bin     bin     347     743
662 sub/a   0       bin     bin     $nobody sys     777     777
663 sub/b   0       $nobody sys     $nobody $nobody 367     763
664 End-of-File
665 }
666
667 _create_dumpdir_symlinks()
668 {
669     _wipe_fs
670     _mk_symlink_config
671     _do_create_dump_symlinks
672     _stable_fs
673 }       
674
675 #
676 # create hardlinks of form $_fname, $_fname_h1 $_fname_h2 ...
677 #
678 _create_hardlinks()
679 {
680     _fname=$1   
681     _numlinks=$2
682
683     touch $_fname
684     _j=1
685     while [ $_j -le $_numlinks ]; do
686         _suffix=_h$_j
687         _hardlink=$_fname$_suffix
688         echo "creating hardlink $_hardlink to $_fname"
689         ln $_fname $_hardlink
690         _j=`expr $_j + 1`
691     done
692 }
693
694 #
695 # create a set of hardlinks
696 # create hardlinks of form file1, file1_h1 file1_h2 ...
697 # create hardlinks of form file2, file2_h1 file2_h2 ...
698 # create hardlinks of form file3, file3_h1 file3_h2 ...
699 #
700 _create_hardset()
701 {
702     _numsets=$1
703     _i=1
704     while [ $_i -le $_numsets ]; do
705         _create_hardlinks file$_i 5
706         _i=`expr $_i + 1`
707     done
708 }
709
710
711 _modify_level()
712 {
713     _level=$1
714     echo "mod level $_level" >$dump_dir/file$_level
715 }
716
717 _create_dumpdir_hardlinks()
718 {
719     _numsets=$1
720     _wipe_fs
721     echo "Creating directory system of hardlinks to incrementally dump."
722
723     mkdir -p $dump_dir ||\
724         _error "cannot mkdir \"$dump_dir\""
725     cd $dump_dir
726
727     _create_hardset $_numsets
728
729     cd $here
730     _stable_fs
731 }
732
733 #
734 # Filter for ls
735 # Filter out dates on symlinks and char devices
736 #
737 _ls_filter()
738 {
739   $AWK_PROG '
740         /^l/ { date = $8; sub(date,"DATE"); print}
741         /^c/ { date = $9; sub(date,"DATE"); print}
742         {print}' \
743   | sed -e 's/total [0-9][0-9]*/total TOTAL/'
744 }
745
746
747
748 # Filter out the non-deterministic dump msgs from
749 # xfsdump and xfsrestore
750 #
751 _dump_filter_main()
752 {
753   sed \
754       -e "s/`hostname`/HOSTNAME/"   \
755       -e "s#$SCRATCH_DEV#SCRATCH_DEV#"    \
756       -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
757       -e "s#$dumptape#TAPE_DEV#"    \
758       -e "s#$SCRATCH_MNT#SCRATCH_MNT#"    \
759       -e "s#$dump_file#DUMP_FILE#"  \
760       -e 's#/var/lib/xfsdump#/var/xfsdump#' \
761       -e 's/id:[        ]*[0-9a-f-]*/id: ID/'  \
762       -e 's/time:[      ].*/time: TIME/'       \
763       -e 's/date:[      ].*/date: DATE/'       \
764       -e 's/dump begun .*/dump begun DATE/'    \
765       -e 's/[0-9][0-9]* seconds/SECS seconds/' \
766       -e 's/restore.[0-9][0-9]*/restore.PID/' \
767       -e 's/ino [0-9][0-9]*/ino INO/' \
768       -e '/: dump size/s/[0-9][0-9]*/NUM/'     \
769       -e '/dump size:/s/[0-9][0-9]*/NUM/'      \
770       -e '/dump size per stream:/s/[0-9][0-9]*/NUM/' \
771       -e 's/\(media file size[   ]*\)[0-9][0-9]*/\1NUM/' \
772       -e 's/\(mfile size:[       ]*\)[0-9][0-9]*/\1NUM/' \
773       -e '/drive[        ]*[0-9][0-9]*:/d' \
774       -e '/\/dev\/tty/d' \
775       -e '/inventory session uuid/d' \
776       -e '/ - Running single-threaded/d' \
777       -e '/^.*I\/O metrics: .*$/d' \
778       -e 's/1048576/BLOCKSZ/' \
779       -e 's/2097152/BLOCKSZ/' \
780       -e 's/(pid[        ]*[1-9][0-9]*)/\(pid PID\)/' \
781   | perl -ne '
782       if ($_ =~ /(?:Dump|Restore) Summary/) {
783         $skip = 1;
784       } elsif ($_ =~ /(?:Dump|Restore) Status/) {
785         $skip = 0;
786       }
787       print if (! $skip);'
788 }
789
790 _dump_filter()
791 {
792    if $do_quota_check
793    then
794        _dump_filter_main | _check_quota_dumprestore | _check_quota_entries
795    else
796        _dump_filter_main
797    fi
798 }
799
800 _invutil_filter()
801 {
802   _dump_filter_main \
803   | sed \
804         -e 's/UUID[     ]*:[    ][0-9a-f-]*/UUID                :       ID/' \
805         -e 's/TIME OF DUMP[     ]*:.*/TIME OF DUMP      :       TIME/' \
806         -e 's/HOSTNAME:SCRATCH_MNT.*/HOSTNAME:SCRATCH_MNT/' \
807         -e 's#inventory/[0-9a-f-]*#inventory/UUID#' \
808
809 }
810
811
812 _dir_filter()
813 {
814   sed \
815     -e "s#$dump_file#DUMP_FILE#"      \
816     -e "s#$SCRATCH_DEV#SCRATCH_DEV#"        \
817     -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
818     -e "s#$dumptape#TAPE_DEV#"         \
819     -e "s#$dump_dir#DUMP_DIR#g"       \
820     -e "s#$restore_dir#RESTORE_DIR#g" \
821     -e "s#$SCRATCH_MNT#SCRATCH_MNT#g"       \
822     -e "s#$dump_sdir#DUMP_SUBDIR#g"   \
823     -e "s#$restore_sdir#RESTORE_SUBDIR#g" \
824     -e "s#$$#PID#g" \
825
826 }
827
828 #
829 # Note: requires a space between option letter and argument 
830 #
831 _parse_args()
832 {
833     OPTIND=0
834     dump_args=""
835     while [ $# -gt 0 ]
836     do
837         case $1
838         in
839         -f)
840             [ -z "$2" ] && _error "missing argument for -f"
841             dumptape=$2 
842             shift
843             ;;
844         -L)
845             [ -z "$2" ] && _error "missing argument for -L"
846             session_label=$2
847             shift
848             ;;
849         -o)
850             dump_args="$dump_args -o"
851             ;;
852         -F)
853             dump_args="$dump_args -F"
854             ;;
855         --multi)
856             multi=$2
857             shift
858             ;;
859         -q)
860             do_quota_check=true
861             ;;
862         -Q)
863             do_quota_check=false
864             ;;
865         -l)
866             [ -z "$2" ] && _error "missing argument for -l"
867             dump_args="$dump_args -l$2"
868             shift
869             ;;
870         *)
871             _error "invalid argument to common.dump function: $1"
872             ;;
873         esac
874         shift
875     done
876 }
877
878
879 #
880 # Dump a subdir
881 #
882 _do_dump_sub()
883 {
884     _parse_args $*
885
886     echo "Dumping to tape..."
887     opts="$_dump_debug$dump_args -s $dump_sdir -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
888     echo "xfsdump $opts" | _dir_filter  
889     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
890 }
891
892 #
893 # Do dump to tape
894 #
895 _do_dump()
896 {
897     _parse_args $*
898
899     echo "Dumping to tape..."
900     opts="$_dump_debug$dump_args -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
901     echo "xfsdump $opts" | _dir_filter  
902     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
903 }
904
905
906 #
907 # Do full dump with -m
908 #
909 _do_dump_min()
910 {
911     _parse_args $*
912
913     echo "Dumping to tape..."
914     onemeg=1048576
915     opts="$_dump_debug$dump_args -m -b $onemeg -l0 -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
916     echo "xfsdump $opts" | _dir_filter  
917     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
918 }
919
920
921 #
922 # Do full dump to file
923 #
924 _do_dump_file()
925 {
926     _parse_args $*
927
928     echo "Dumping to file..."
929     opts="$_dump_debug$dump_args -f $dump_file -M $media_label -L $session_label $SCRATCH_MNT"
930     echo "xfsdump $opts" | _dir_filter  
931     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
932 }
933
934 #
935 # Do full dump to multiple files
936 #
937 _do_dump_multi_file()
938 {
939     _parse_args "$@"
940
941     multi_args=""
942
943     i=0
944     while [ $i -lt $multi ]
945     do
946         multi_args="$multi_args -f $dump_file.$i -M $media_label.$i"
947         i=`expr $i + 1`
948     done
949
950     echo "Dumping to files..."
951     opts="$_dump_debug$dump_args $multi_args -L $session_label $SCRATCH_MNT"
952     echo "xfsdump $opts" | _dir_filter  
953     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
954 }
955
956
957 _prepare_restore_dir()
958 {
959     rm -rf $restore_dir
960     mkdir $restore_dir ||\
961         _error "failed to mkdir $restore_dir"
962 }
963
964
965 #
966 # Get tape ready and restore dir
967 #
968 _prepare_restore()
969 {
970     _prepare_restore_dir
971
972     echo "Rewinding tape"
973     _rewind
974 }
975
976 #
977 # Restore the tape into $restore_dir
978 #
979 _do_restore()
980 {
981     _parse_args $*
982     _prepare_restore
983
984
985     echo "Restoring from tape..."
986     opts="$_restore_debug -f $dumptape  -L $session_label $restore_dir"
987     echo "xfsrestore $opts" | _dir_filter  
988     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
989 }
990
991 #
992 # Restore the tape into $restore_dir using -m
993 #
994 _do_restore_min()
995 {
996     _parse_args $*
997     _prepare_restore
998
999     echo "Restoring from tape..."
1000     onemeg=1048576
1001     opts="$_restore_debug -m -b $onemeg -f $dumptape  -L $session_label $restore_dir"
1002     echo "xfsrestore $opts" | _dir_filter  
1003     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1004 }
1005
1006 #
1007 # Restore the tape from a dump file
1008 #
1009 _do_restore_file()
1010 {
1011     _parse_args $*
1012     _prepare_restore_dir
1013
1014     echo "Restoring from file..."
1015     opts="$_restore_debug -f $dump_file  -L $session_label $restore_dir"
1016     echo "xfsrestore $opts" | _dir_filter  
1017     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1018 }
1019
1020 #
1021 # Cumulative restore from a file
1022 # Need to specify the dump level e.g. "-l 0"
1023 #
1024 _do_restore_file_cum()
1025 {
1026     _parse_args $*
1027     if echo $dump_args | grep '\-l0' >/dev/null; then
1028         _prepare_restore_dir
1029     fi
1030
1031     echo "Restoring cumumlative from file..."
1032     opts="$_restore_debug -f $dump_file -r $restore_dir"
1033     echo "xfsrestore $opts" | _dir_filter  
1034     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1035 }
1036
1037 _do_restore_toc()
1038 {
1039     echo "Contents of dump ..."
1040     opts="$_restore_debug -f $dump_file -t"
1041     echo "xfsrestore $opts" | _dir_filter
1042     cd $SCRATCH_MNT # for IRIX which needs xfs cwd
1043     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter_main |\
1044     _check_quota_file |\
1045     _check_quota_entries |\
1046     $AWK_PROG 'NF != 1 { print; next }
1047                {files = sprintf("%s\n%s", files, $1)}
1048                 END { print files | "sort" } '
1049     # the above awk code is to alpha sort only the output
1050     # of files (and not the verbose restore msgs)
1051     cd $here # put back
1052 }
1053
1054 #
1055 # Restore the tape from multiple dump files
1056 #
1057 _do_restore_multi_file()
1058 {
1059     _parse_args "$@"
1060     _prepare_restore_dir
1061
1062     multi_args=""
1063
1064     i=0
1065     while [ $i -lt $multi ]
1066     do
1067         multi_args="$multi_args -f $dump_file.$i"
1068         i=`expr $i + 1`
1069     done
1070
1071     echo "Restoring from file..."
1072     opts="$_restore_debug $multi_args -L $session_label $restore_dir"
1073     echo "xfsrestore $opts" | _dir_filter  
1074     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1075 }
1076
1077 #
1078 # Do xfsdump piped into xfsrestore - xfsdump | xfsrestore
1079 #
1080 # Use -s as we want to dump and restore to the same xfs partition
1081 #
1082 _do_dump_restore()
1083 {
1084     _parse_args $*
1085     _prepare_restore_dir
1086     echo "xfsdump|xfsrestore ..."
1087     restore_opts="$_restore_debug - $restore_dir"
1088     dump_opts="$_dump_debug$dump_args -s $dump_sdir - $SCRATCH_MNT"
1089     echo "xfsdump $dump_opts | xfsrestore $restore_opts" | _dir_filter  
1090     xfsdump $dump_opts 2>$tmp.dump.mlog | xfsrestore $restore_opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1091     _dump_filter <$tmp.dump.mlog
1092 }
1093
1094 #
1095 # Compare dumped subdirectory with restored dir
1096 # using ls -lR.
1097 # Thus no contents are compared but permissions, sizes,
1098 # owners, etc... are.
1099 #
1100 _ls_compare_sub()
1101 {
1102     #
1103     # verify we got back what we dumped
1104     #
1105     echo "Comparing listing of dump directory with restore directory"
1106     ls -lR $dump_dir | tee -a $here/$seq.full | _ls_filter >$tmp.dump_dir
1107     ls -lR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1108     | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1109
1110     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1111 }
1112
1113 #
1114 # filter out the date fields
1115 #
1116 _ls_nodate_filter()
1117 {
1118     $AWK_PROG 'NF == 9 { print $1, $2, $3, $4, $9 }'
1119 }
1120
1121 #
1122 # _ls_compare_sub but don't compare dates
1123 _ls_nodate_compare_sub()
1124 {
1125     #
1126     # verify we got back what we dumped
1127     #
1128     echo "Comparing listing of dump directory with restore directory"
1129     ls -lR $dump_dir | tee -a $here/$seq.full | _ls_filter | _ls_nodate_filter >$tmp.dump_dir
1130     ls -lR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1131     | _ls_nodate_filter | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1132
1133     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1134 }
1135
1136 #
1137 # Compare using recursive diff the files of the dumped
1138 # subdirectory.
1139 # This one will compare the contents.
1140 #
1141 _diff_compare_sub()
1142 {
1143     echo "Comparing dump directory with restore directory"
1144     diff -rs $dump_dir $restore_dir/$dump_sdir | _dir_filter
1145 }
1146
1147 _get_eas_on_path()
1148 {
1149     _path=$1
1150
1151 # Tim - this is the IRIX way...
1152     # find $_path -exec attr -l {} \; |\
1153     # awk '{print $9, $2}' |\
1154     # sed 's/["]//g' |\
1155     # sort |\
1156 # and this is now the Linux way...
1157     echo "User names"
1158     getfattr --absolute-names -Rh $_path |\
1159     perl -wn -e '
1160         if (m/^# file: (\S+)/) { $file = $1 }
1161         elsif (m/^user\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1162     sort |\
1163     while read file ea_name; do
1164         attr -g $ea_name $file
1165     done
1166
1167     echo "Root names"
1168     getfattr --absolute-names -Rh -m trusted $_path |\
1169     perl -wn -e '
1170         if (m/^# file: (\S+)/) { $file = $1 }
1171         elsif (m/^trusted\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1172     sort |\
1173     while read file ea_name; do
1174         attr -R -g $ea_name $file
1175     done
1176 }
1177
1178 #
1179 # Compare the extended attributes of the files/dirs
1180 # b/w the dumped and restore dirs.
1181 #
1182 #
1183 # Attribute "attr5" had a 8 byte value for /spare1/dump.5460/dir:
1184 # Attribute "attr5" had a 8 byte value for /spare1/restore.5460/dump.5460/dir:
1185 #
1186 _diff_compare_eas()
1187 {
1188     echo "Comparing dump directory with restore directory"
1189     echo "Looking at the extended attributes (EAs)"
1190     echo "EAs on dump"
1191     _get_eas_on_path $dump_dir | tee $seq.ea1 | _dir_filter
1192     echo "EAs on restore"
1193     _get_eas_on_path $restore_dir/$dump_sdir \
1194     | sed -e "s#$restore_sdir\/##" \
1195     | tee $seq.ea2 \
1196     | _dir_filter
1197     diff -s $seq.ea1 $seq.ea2
1198 }
1199
1200
1201 #
1202 # Compare using recursive diff the files of the dumped
1203 # filesystem
1204 #
1205 _diff_compare()
1206 {
1207     echo "Comparing dump directory with restore directory"
1208     diff -rs $SCRATCH_MNT $restore_dir | _dir_filter | _check_quota_diff
1209 }
1210
1211 #
1212 # Check out the dump inventory
1213 #
1214 _dump_inventory()
1215 {
1216     xfsdump $_dump_debug -I | tee -a $here/$seq.full | _dump_filter_main
1217 }
1218
1219 #
1220 # Do the xfsinvutil cmd with debug and filters
1221 # Need to set variable: "$middate" to the invutil date 
1222 #
1223 _do_invutil()
1224 {
1225     host=`hostname`
1226     echo "xfsinvutil $_invutil_debug -M $host:$SCRATCH_MNT \"$middate\" $*" >$here/$seq.full
1227     xfsinvutil $_invutil_debug $* -M $host:$SCRATCH_MNT "$middate" \
1228     | tee -a $here/$seq.full | _invutil_filter
1229 }
1230
1231 #
1232 # ensure we can find the user quota msg if user quotas are on
1233 # ensure we can find the group quota msg if group quotas are on
1234 #
1235 _check_quota()
1236 {
1237     usermsg=$1 
1238     groupmsg=$2 
1239     uquota=0
1240     gquota=0 
1241     $here/src/feature -U $SCRATCH_DEV && uquota=1
1242     $here/src/feature -G $SCRATCH_DEV && gquota=1
1243
1244     $AWK_PROG -v uquota=$uquota -v gquota=$gquota -v full=$here/$seq.full \
1245               -v usermsg="$usermsg" -v groupmsg="$groupmsg" '
1246         $0 ~ groupmsg {
1247                         print "Found group quota:", $0 >>full
1248                         found_gquota = 1
1249                         if (!gquota) {
1250                             print "Found extra:", $0
1251                         }
1252                         next
1253         }
1254         $0 ~ usermsg {
1255                         print "Found user quota:", $0 >>full
1256                         found_uquota = 1
1257                         if (!uquota) {
1258                             print "Found extra:", $0
1259                         }
1260                         next
1261         }
1262                         { print }
1263         END {
1264                 if (uquota && !found_uquota) {
1265                     print "Missing: ", usermsg
1266                 }
1267                 if (gquota && !found_gquota) {
1268                     print "Missing: ", groupmsg
1269                 }
1270         }
1271     '
1272 }
1273
1274 #
1275 # xfsrestore: 3 directories and 40 entries processed 
1276 #   $5 = 40 
1277 #   num entries needs to be reduced by num quota file(s) 
1278 #
1279 _check_quota_entries()
1280 {
1281     uquota=0
1282     gquota=0 
1283     $here/src/feature -U $SCRATCH_DEV && uquota=1
1284     $here/src/feature -G $SCRATCH_DEV && gquota=1
1285     $AWK_PROG -v uquota=$uquota -v gquota=$gquota '
1286         /entries processed/ { 
1287                 if (uquota) $5--
1288                 if (gquota) $5--
1289         }
1290         {print}'
1291 }
1292
1293 #
1294 # Look for:
1295 # xfsdump: saving user quota information for: SCRATCH_MNT
1296 # xfsdump: saving group quota information for: SCRATCH_MNT
1297 # xfsrestore: user quota information written to ...'
1298 # xfsrestore: group quota information written to ...'
1299 #
1300 _check_quota_dumprestore()
1301 {
1302    _check_quota 'user quota information' \
1303                 'group quota information'
1304 }
1305
1306 #
1307 # Look for:
1308 # Only in RESTORE_DIR: xfsdump_quotas
1309 # Only in RESTORE_DIR: xfsdump_quotas_group
1310 #
1311 _check_quota_diff()
1312 {
1313    _check_quota 'Only in RESTORE_DIR: xfsdump_quotas' \
1314        'Only in RESTORE_DIR: xfsdump_quotas_group' 
1315 }
1316
1317 #
1318 # Look for the quota file in the output
1319 # Ensure that it is there if it should be
1320 # Filter it out so that the output is always the same
1321 # even with no quotas
1322 #
1323 _check_quota_file()
1324 {
1325    _check_quota 'xfsdump_quotas' 'xfsdump_quotas_group'
1326 }
1327
1328 # make sure this script returns success
1329 /bin/true