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