Make out of space test more general so I can try different scenarios more easily...
[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 # Create a bunch of directories/files of different sizes
439 # filled with data.
440 #
441 # Pinched from test 001.
442 #
443 _do_create_dumpdir_fill()
444 {
445     echo "Creating directory system to dump using src/fill."
446
447     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
448     cd $dump_dir
449
450     $verbose && echo -n "Setup "
451     sed -e '/^#/d' $tmp.config \
452     | while read file nbytes owner group perms ea_name ea_value namespace
453     do
454         if [ $nbytes = "d" ]; then
455             # create a directory
456             dir=$file   
457             if [ ! -d $dir ]
458             then
459                 if mkdir $dir
460                 then
461                     :
462                 else
463                     $verbose && echo
464                     echo "Error: cannot mkdir \"$dir\""
465                     exit 1
466                 fi
467             fi
468         else
469             # create a directory/file
470             dir=`dirname $file`
471             if [ "$dir" != "." ]
472             then
473                 if [ ! -d $dir ]
474                 then
475                     if mkdir $dir
476                     then
477                         :
478                     else
479                         $verbose && echo
480                         echo "Error: cannot mkdir \"$dir\""
481                         exit 1
482                     fi
483                 fi
484             fi
485             rm -f $file
486             if $here/src/fill $file $file $nbytes
487             then
488                 :
489             else
490                 $verbose && echo
491                 echo "Error: cannot create \"$file\""
492                 exit 1
493             fi
494         fi
495         if [ -n "$owner" -a -n "$group" ]; then
496             chown $owner.$group $file
497         fi
498         if [ -n "$perms" ]; then
499             chmod $perms $file
500         fi
501         if [ -n "$ea_name" -a -n "$ea_value" ]; then
502             if [ "X$namespace" = "Xroot" ]; then
503                 attr -R -s $ea_name -V $ea_value $file
504             else
505                 attr -s $ea_name -V $ea_value $file
506             fi
507         fi
508         $verbose && echo -n "."
509     done
510     $verbose && echo
511
512     cd $here
513 }
514
515 _create_dumpdir_largefile()
516 {
517     _wipe_fs
518     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
519     _largesize=4294967297
520     _largefile=$dump_dir/largefile
521     echo "dd a largefile at offset $_largesize"
522     POSIXLY_CORRECT=yes \
523     dd if=/dev/zero of=$_largefile bs=1 seek=$_largesize count=10 2>&1
524     _stable_fs
525 }       
526
527 _create_dumpdir_fill()
528 {
529     _wipe_fs
530     _mk_fillconfig1
531     _do_create_dumpdir_fill
532     _stable_fs
533 }       
534
535 _create_dumpdir_fill2()
536 {
537     _wipe_fs
538     _mk_fillconfig2
539     _do_create_dumpdir_fill
540     _stable_fs
541 }       
542
543 _create_dumpdir_fill_perm()
544 {
545     _wipe_fs
546     _mk_fillconfig_perm
547     _do_create_dumpdir_fill
548     _stable_fs
549 }       
550
551 _create_dumpdir_fill_ea()
552 {
553     _wipe_fs
554     _mk_fillconfig_ea
555     _do_create_dumpdir_fill
556     _stable_fs
557 }       
558
559
560 #
561 # Append a subset of the fill'ed files
562 # So we can see if just these get dumped on an incremental
563 #
564 _append_dumpdir_fill()
565 {
566     cd $dump_dir
567     cat <<End-of-File >$tmp.config
568 # pathname
569 #
570 small   
571 sub/big 
572 #
573 sub/a
574 sub/c
575 sub/e
576 End-of-File
577     sed -e '/^#/d' $tmp.config \
578     | while read file
579     do
580         echo 'Extra text' >>$file
581     done
582
583     cd $here
584     _stable_fs
585 }
586
587 _do_create_dump_symlinks()
588 {
589     echo "Creating directory system of symlinks to dump."
590
591     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
592     cd $dump_dir
593
594     $verbose && echo -n "Setup "
595     sed -e '/^#/d' $tmp.config \
596     | while read file nbytes owner group owner2 group2 perms perms2
597     do
598         dir=`dirname $file`
599         if [ "$dir" != "." ]
600         then
601             if [ ! -d $dir ]
602             then
603                 if mkdir $dir
604                 then
605                     :
606                 else
607                     $verbose && echo
608                     echo "Error: cannot mkdir \"$dir\""
609                     exit 1
610                 fi
611             fi
612         fi
613         rm -f $file
614         touch $file
615
616         # Do chmod on symlink using umask.
617         # This won't do the right thing as it subtracts permissions.
618         # However, I don't care, as long as I get some different perms
619         # for testing.
620         if [ -n "$perms2" ]; then
621             omask=`umask`
622             umask $perms2
623         fi
624         ln -s $file $file-link
625         if [ -n "$perms2" ]; then
626             umask $omask        
627         fi
628
629         if [ -n "$owner" -a -n "$group" ]; then
630             chown $owner.$group $file
631         fi
632         if [ -n "$owner" -a -n "$group" ]; then
633             chown -h $owner.$group $file-link
634         fi
635         if [ -n "$perms" ]; then
636             chmod $perms $file
637         fi
638         $verbose && echo -n "."
639     done
640     $verbose && echo
641
642     cd $here
643 }
644
645 _mk_symlink_config()
646 {
647     cat <<End-of-File >$tmp.config
648 # path  size    owner1  group1  owner2  group2  perm1   perm2 
649 #
650 a       0       $nobody $nobody daemon  sys     124     421
651 b       0       daemon  sys     bin     bin     347     743
652 sub/a   0       bin     bin     $nobody sys     777     777
653 sub/b   0       $nobody sys     $nobody $nobody 367     763
654 End-of-File
655 }
656
657 _create_dumpdir_symlinks()
658 {
659     _wipe_fs
660     _mk_symlink_config
661     _do_create_dump_symlinks
662     _stable_fs
663 }       
664
665 #
666 # create hardlinks of form $_fname, $_fname_h1 $_fname_h2 ...
667 #
668 _create_hardlinks()
669 {
670     _fname=$1   
671     _numlinks=$2
672
673     touch $_fname
674     _j=1
675     while [ $_j -le $_numlinks ]; do
676         _suffix=_h$_j
677         _hardlink=$_fname$_suffix
678         echo "creating hardlink $_hardlink to $_fname"
679         ln $_fname $_hardlink
680         _j=`expr $_j + 1`
681     done
682 }
683
684 #
685 # create a set of hardlinks
686 # create hardlinks of form file1, file1_h1 file1_h2 ...
687 # create hardlinks of form file2, file2_h1 file2_h2 ...
688 # create hardlinks of form file3, file3_h1 file3_h2 ...
689 #
690 _create_hardset()
691 {
692     _numsets=$1
693     _i=1
694     while [ $_i -le $_numsets ]; do
695         _create_hardlinks file$_i 5
696         _i=`expr $_i + 1`
697     done
698 }
699
700
701 _modify_level()
702 {
703     _level=$1
704     echo "mod level $_level" >$dump_dir/file$_level
705 }
706
707 _create_dumpdir_hardlinks()
708 {
709     _numsets=$1
710     _wipe_fs
711     echo "Creating directory system of hardlinks to incrementally dump."
712
713     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
714     cd $dump_dir
715
716     _create_hardset $_numsets
717
718     cd $here
719     _stable_fs
720 }
721
722 #
723 # Filter for ls
724 # Filter out dates on symlinks and char devices
725 # Filter out size on directories because this can differ
726 # when transitioning to long inode numbers (ie. 64 bits).
727 #
728 _ls_filter()
729 {
730   $AWK_PROG '
731         /^l/ { date = $8; sub(date,"DATE"); print}
732         /^c/ { date = $9; sub(date,"DATE"); print}
733         /^d/ { size = $5; sub(size,"SIZE"); print}
734         {print}' \
735   | sed -e 's/total [0-9][0-9]*/total TOTAL/'
736 }
737
738
739
740 # Filter out the non-deterministic dump msgs from
741 # xfsdump and xfsrestore
742 #
743 _dump_filter_main()
744 {
745   sed \
746       -e "s/`hostname`/HOSTNAME/"   \
747       -e "s#$SCRATCH_DEV#SCRATCH_DEV#"    \
748       -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
749       -e "s#$dumptape#TAPE_DEV#"    \
750       -e "s#$SCRATCH_MNT#SCRATCH_MNT#"    \
751       -e "s#$dump_file#DUMP_FILE#"  \
752       -e 's#/var/lib/xfsdump#/var/xfsdump#' \
753       -e 's/id:[        ]*[0-9a-f-]*/id: ID/'  \
754       -e 's/time:[      ].*/time: TIME/'       \
755       -e 's/date:[      ].*/date: DATE/'       \
756       -e 's/dump begun .*/dump begun DATE/'    \
757       -e 's/[0-9][0-9]* seconds/SECS seconds/' \
758       -e 's/restore.[0-9][0-9]*/restore.PID/' \
759       -e 's/ino [0-9][0-9]*/ino INO/' \
760       -e '/: dump size/s/[0-9][0-9]*/NUM/'     \
761       -e '/dump size:/s/[0-9][0-9]*/NUM/'      \
762       -e '/dump size per stream:/s/[0-9][0-9]*/NUM/' \
763       -e 's/\(media file size[   ]*\)[0-9][0-9]*/\1NUM/' \
764       -e 's/\(mfile size:[       ]*\)[0-9][0-9]*/\1NUM/' \
765       -e '/drive[        ]*[0-9][0-9]*:/d' \
766       -e '/\/dev\/tty/d' \
767       -e '/inventory session uuid/d' \
768       -e '/ - Running single-threaded/d' \
769       -e '/^.*I\/O metrics: .*$/d' \
770       -e 's/1048576/BLOCKSZ/' \
771       -e 's/2097152/BLOCKSZ/' \
772       -e 's/(pid[        ]*[1-9][0-9]*)/\(pid PID\)/' \
773   | perl -ne '
774       if ($_ =~ /(?:Dump|Restore) Summary/) {
775         $skip = 1;
776       } elsif ($_ =~ /(?:Dump|Restore) Status/) {
777         $skip = 0;
778       }
779       print if (! $skip);'
780 }
781
782 _dump_filter()
783 {
784    if $do_quota_check
785    then
786        _dump_filter_main | _check_quota_dumprestore | _check_quota_entries
787    else
788        _dump_filter_main
789    fi
790 }
791
792 _invutil_filter()
793 {
794   _dump_filter_main \
795   | sed \
796         -e 's/UUID[     ]*:[    ][0-9a-f-]*/UUID                :       ID/' \
797         -e 's/TIME OF DUMP[     ]*:.*/TIME OF DUMP      :       TIME/' \
798         -e 's/HOSTNAME:SCRATCH_MNT.*/HOSTNAME:SCRATCH_MNT/' \
799         -e 's#inventory/[0-9a-f-]*#inventory/UUID#' \
800
801 }
802
803
804 _dir_filter()
805 {
806   sed \
807     -e "s#$dump_file#DUMP_FILE#"      \
808     -e "s#$SCRATCH_DEV#SCRATCH_DEV#"        \
809     -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
810     -e "s#$dumptape#TAPE_DEV#"         \
811     -e "s#$dump_dir#DUMP_DIR#g"       \
812     -e "s#$restore_dir#RESTORE_DIR#g" \
813     -e "s#$SCRATCH_MNT#SCRATCH_MNT#g"       \
814     -e "s#$dump_sdir#DUMP_SUBDIR#g"   \
815     -e "s#$restore_sdir#RESTORE_SUBDIR#g" \
816     -e "s#$$#PID#g" \
817
818 }
819
820 #
821 # Note: requires a space between option letter and argument 
822 #
823 _parse_args()
824 {
825     OPTIND=0
826     dump_args=""
827     while [ $# -gt 0 ]
828     do
829         case $1
830         in
831         -f)
832             [ -z "$2" ] && _fail "missing argument for -f"
833             dumptape=$2 
834             shift
835             ;;
836         -L)
837             [ -z "$2" ] && _fail "missing argument for -L"
838             session_label=$2
839             shift
840             ;;
841         -o)
842             dump_args="$dump_args -o"
843             ;;
844         -F)
845             dump_args="$dump_args -F"
846             ;;
847         --multi)
848             multi=$2
849             shift
850             ;;
851         -q)
852             do_quota_check=true
853             ;;
854         -Q)
855             do_quota_check=false
856             ;;
857         -l)
858             [ -z "$2" ] && _fail "missing argument for -l"
859             dump_args="$dump_args -l$2"
860             shift
861             ;;
862         *)
863             _fail "invalid argument to common.dump function: $1"
864             ;;
865         esac
866         shift
867     done
868 }
869
870
871 #
872 # Dump a subdir
873 #
874 _do_dump_sub()
875 {
876     _parse_args $*
877
878     echo "Dumping to tape..."
879     opts="$_dump_debug$dump_args -s $dump_sdir -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
880     echo "xfsdump $opts" | _dir_filter  
881     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
882 }
883
884 #
885 # Do dump to tape
886 #
887 _do_dump()
888 {
889     _parse_args $*
890
891     echo "Dumping to tape..."
892     opts="$_dump_debug$dump_args -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
893     echo "xfsdump $opts" | _dir_filter  
894     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
895 }
896
897
898 #
899 # Do full dump with -m
900 #
901 _do_dump_min()
902 {
903     _parse_args $*
904
905     echo "Dumping to tape..."
906     onemeg=1048576
907     opts="$_dump_debug$dump_args -m -b $onemeg -l0 -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
908     echo "xfsdump $opts" | _dir_filter  
909     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
910 }
911
912
913 #
914 # Do full dump to file
915 #
916 _do_dump_file()
917 {
918     _parse_args $*
919
920     echo "Dumping to file..."
921     opts="$_dump_debug$dump_args -f $dump_file -M $media_label -L $session_label $SCRATCH_MNT"
922     echo "xfsdump $opts" | _dir_filter  
923     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
924 }
925
926 #
927 # Do full dump to multiple files
928 #
929 _do_dump_multi_file()
930 {
931     _parse_args "$@"
932
933     multi_args=""
934
935     i=0
936     while [ $i -lt $multi ]
937     do
938         multi_args="$multi_args -f $dump_file.$i -M $media_label.$i"
939         i=`expr $i + 1`
940     done
941
942     echo "Dumping to files..."
943     opts="$_dump_debug$dump_args $multi_args -L $session_label $SCRATCH_MNT"
944     echo "xfsdump $opts" | _dir_filter  
945     xfsdump $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
946 }
947
948
949 _prepare_restore_dir()
950 {
951     rm -rf $restore_dir
952     mkdir $restore_dir || _fail "failed to mkdir $restore_dir"
953 }
954
955
956 #
957 # Get tape ready and restore dir
958 #
959 _prepare_restore()
960 {
961     _prepare_restore_dir
962
963     echo "Rewinding tape"
964     _rewind
965 }
966
967 #
968 # Restore the tape into $restore_dir
969 #
970 _do_restore()
971 {
972     _parse_args $*
973     _prepare_restore
974
975
976     echo "Restoring from tape..."
977     opts="$_restore_debug -f $dumptape  -L $session_label $restore_dir"
978     echo "xfsrestore $opts" | _dir_filter  
979     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
980 }
981
982 #
983 # Restore the tape into $restore_dir using -m
984 #
985 _do_restore_min()
986 {
987     _parse_args $*
988     _prepare_restore
989
990     echo "Restoring from tape..."
991     onemeg=1048576
992     opts="$_restore_debug -m -b $onemeg -f $dumptape  -L $session_label $restore_dir"
993     echo "xfsrestore $opts" | _dir_filter  
994     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
995 }
996
997 #
998 # Restore the tape from a dump file
999 #
1000 _do_restore_file()
1001 {
1002     _parse_args $*
1003     _prepare_restore_dir
1004
1005     echo "Restoring from file..."
1006     opts="$_restore_debug -f $dump_file  -L $session_label $restore_dir"
1007     echo "xfsrestore $opts" | _dir_filter  
1008     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1009 }
1010
1011 #
1012 # Cumulative restore from a file
1013 # Need to specify the dump level e.g. "-l 0"
1014 #
1015 _do_restore_file_cum()
1016 {
1017     _parse_args $*
1018     if echo $dump_args | grep '\-l0' >/dev/null; then
1019         _prepare_restore_dir
1020     fi
1021
1022     echo "Restoring cumumlative from file..."
1023     opts="$_restore_debug -f $dump_file -r $restore_dir"
1024     echo "xfsrestore $opts" | _dir_filter  
1025     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1026 }
1027
1028 _do_restore_toc()
1029 {
1030     echo "Contents of dump ..."
1031     opts="$_restore_debug -f $dump_file -t"
1032     echo "xfsrestore $opts" | _dir_filter
1033     cd $SCRATCH_MNT # for IRIX which needs xfs cwd
1034     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter_main |\
1035     _check_quota_file |\
1036     _check_quota_entries |\
1037     $AWK_PROG 'NF != 1 { print; next }
1038                {files = sprintf("%s\n%s", files, $1)}
1039                 END { print files | "sort" } '
1040     # the above awk code is to alpha sort only the output
1041     # of files (and not the verbose restore msgs)
1042     cd $here # put back
1043 }
1044
1045 #
1046 # Restore the tape from multiple dump files
1047 #
1048 _do_restore_multi_file()
1049 {
1050     _parse_args "$@"
1051     _prepare_restore_dir
1052
1053     multi_args=""
1054
1055     i=0
1056     while [ $i -lt $multi ]
1057     do
1058         multi_args="$multi_args -f $dump_file.$i"
1059         i=`expr $i + 1`
1060     done
1061
1062     echo "Restoring from file..."
1063     opts="$_restore_debug $multi_args -L $session_label $restore_dir"
1064     echo "xfsrestore $opts" | _dir_filter  
1065     xfsrestore $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1066 }
1067
1068 #
1069 # Do xfsdump piped into xfsrestore - xfsdump | xfsrestore
1070 #
1071 # Use -s as we want to dump and restore to the same xfs partition
1072 #
1073 _do_dump_restore()
1074 {
1075     _parse_args $*
1076     _prepare_restore_dir
1077     echo "xfsdump|xfsrestore ..."
1078     restore_opts="$_restore_debug - $restore_dir"
1079     dump_opts="$_dump_debug$dump_args -s $dump_sdir - $SCRATCH_MNT"
1080     echo "xfsdump $dump_opts | xfsrestore $restore_opts" | _dir_filter  
1081     xfsdump $dump_opts 2>$tmp.dump.mlog | xfsrestore $restore_opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1082     _dump_filter <$tmp.dump.mlog
1083 }
1084
1085 #
1086 # Compare dumped subdirectory with restored dir
1087 # using ls -lR.
1088 # Thus no contents are compared but permissions, sizes,
1089 # owners, etc... are.
1090 #
1091 _ls_compare_sub()
1092 {
1093     #
1094     # verify we got back what we dumped
1095     #
1096     echo "Comparing listing of dump directory with restore directory"
1097     ls -lR $dump_dir | tee -a $here/$seq.full | _ls_filter >$tmp.dump_dir
1098     ls -lR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1099     | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1100
1101     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1102 }
1103
1104 #
1105 # filter out the date fields
1106 #
1107 _ls_nodate_filter()
1108 {
1109     $AWK_PROG 'NF == 9 { print $1, $2, $3, $4, $9 }'
1110 }
1111
1112 #
1113 # _ls_compare_sub but don't compare dates
1114 _ls_nodate_compare_sub()
1115 {
1116     #
1117     # verify we got back what we dumped
1118     #
1119     echo "Comparing listing of dump directory with restore directory"
1120     ls -lR $dump_dir | tee -a $here/$seq.full | _ls_filter | _ls_nodate_filter >$tmp.dump_dir
1121     ls -lR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1122     | _ls_nodate_filter | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1123
1124     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1125 }
1126
1127 #
1128 # Compare using recursive diff the files of the dumped
1129 # subdirectory.
1130 # This one will compare the contents.
1131 #
1132 _diff_compare_sub()
1133 {
1134     echo "Comparing dump directory with restore directory"
1135     diff -rs $dump_dir $restore_dir/$dump_sdir | _dir_filter
1136 }
1137
1138 _get_eas_on_path()
1139 {
1140     _path=$1
1141
1142 # Tim - this is the IRIX way...
1143     # find $_path -exec attr -l {} \; |\
1144     # awk '{print $9, $2}' |\
1145     # sed 's/["]//g' |\
1146     # sort |\
1147 # and this is now the Linux way...
1148     echo "User names"
1149     getfattr --absolute-names -Rh $_path |\
1150     perl -wn -e '
1151         if (m/^# file: (\S+)/) { $file = $1 }
1152         elsif (m/^user\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1153     sort |\
1154     while read file ea_name; do
1155         attr -g $ea_name $file
1156     done
1157
1158     echo "Root names"
1159     getfattr --absolute-names -Rh -m trusted $_path |\
1160     perl -wn -e '
1161         if (m/^# file: (\S+)/) { $file = $1 }
1162         elsif (m/^trusted\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1163     sort |\
1164     while read file ea_name; do
1165         attr -R -g $ea_name $file
1166     done
1167 }
1168
1169 #
1170 # Compare the extended attributes of the files/dirs
1171 # b/w the dumped and restore dirs.
1172 #
1173 #
1174 # Attribute "attr5" had a 8 byte value for /spare1/dump.5460/dir:
1175 # Attribute "attr5" had a 8 byte value for /spare1/restore.5460/dump.5460/dir:
1176 #
1177 _diff_compare_eas()
1178 {
1179     echo "Comparing dump directory with restore directory"
1180     echo "Looking at the extended attributes (EAs)"
1181     echo "EAs on dump"
1182     _get_eas_on_path $dump_dir | tee $seq.ea1 | _dir_filter
1183     echo "EAs on restore"
1184     _get_eas_on_path $restore_dir/$dump_sdir \
1185     | sed -e "s#$restore_sdir\/##" \
1186     | tee $seq.ea2 \
1187     | _dir_filter
1188     diff -s $seq.ea1 $seq.ea2
1189 }
1190
1191
1192 #
1193 # Compare using recursive diff the files of the dumped
1194 # filesystem
1195 #
1196 _diff_compare()
1197 {
1198     echo "Comparing dump directory with restore directory"
1199     diff -rs $SCRATCH_MNT $restore_dir | _dir_filter | _check_quota_diff
1200 }
1201
1202 #
1203 # Check out the dump inventory
1204 #
1205 _dump_inventory()
1206 {
1207     xfsdump $_dump_debug -I | tee -a $here/$seq.full | _dump_filter_main
1208 }
1209
1210 #
1211 # Do the xfsinvutil cmd with debug and filters
1212 # Need to set variable: "$middate" to the invutil date 
1213 #
1214 _do_invutil()
1215 {
1216     host=`hostname`
1217     echo "xfsinvutil $_invutil_debug -M $host:$SCRATCH_MNT \"$middate\" $*" >$here/$seq.full
1218     xfsinvutil $_invutil_debug $* -M $host:$SCRATCH_MNT "$middate" \
1219     | tee -a $here/$seq.full | _invutil_filter
1220 }
1221
1222 #
1223 # ensure we can find the user quota msg if user quotas are on
1224 # ensure we can find the group quota msg if group quotas are on
1225 #
1226 _check_quota()
1227 {
1228     usermsg=$1 
1229     groupmsg=$2 
1230     uquota=0
1231     gquota=0 
1232     $here/src/feature -U $SCRATCH_DEV && uquota=1
1233     $here/src/feature -G $SCRATCH_DEV && gquota=1
1234
1235     $AWK_PROG -v uquota=$uquota -v gquota=$gquota -v full=$here/$seq.full \
1236               -v usermsg="$usermsg" -v groupmsg="$groupmsg" '
1237         $0 ~ groupmsg {
1238                         print "Found group quota:", $0 >>full
1239                         found_gquota = 1
1240                         if (!gquota) {
1241                             print "Found extra:", $0
1242                         }
1243                         next
1244         }
1245         $0 ~ usermsg {
1246                         print "Found user quota:", $0 >>full
1247                         found_uquota = 1
1248                         if (!uquota) {
1249                             print "Found extra:", $0
1250                         }
1251                         next
1252         }
1253                         { print }
1254         END {
1255                 if (uquota && !found_uquota) {
1256                     print "Missing: ", usermsg
1257                 }
1258                 if (gquota && !found_gquota) {
1259                     print "Missing: ", groupmsg
1260                 }
1261         }
1262     '
1263 }
1264
1265 #
1266 # xfsrestore: 3 directories and 40 entries processed 
1267 #   $5 = 40 
1268 #   num entries needs to be reduced by num quota file(s) 
1269 #
1270 _check_quota_entries()
1271 {
1272     uquota=0
1273     gquota=0 
1274     $here/src/feature -U $SCRATCH_DEV && uquota=1
1275     $here/src/feature -G $SCRATCH_DEV && gquota=1
1276     $AWK_PROG -v uquota=$uquota -v gquota=$gquota '
1277         /entries processed/ { 
1278                 if (uquota) $5--
1279                 if (gquota) $5--
1280         }
1281         {print}'
1282 }
1283
1284 #
1285 # Look for:
1286 # xfsdump: saving user quota information for: SCRATCH_MNT
1287 # xfsdump: saving group quota information for: SCRATCH_MNT
1288 # xfsrestore: user quota information written to ...'
1289 # xfsrestore: group quota information written to ...'
1290 #
1291 _check_quota_dumprestore()
1292 {
1293    _check_quota 'user quota information' \
1294                 'group quota information'
1295 }
1296
1297 #
1298 # Look for:
1299 # Only in RESTORE_DIR: xfsdump_quotas
1300 # Only in RESTORE_DIR: xfsdump_quotas_group
1301 #
1302 _check_quota_diff()
1303 {
1304    _check_quota 'Only in RESTORE_DIR: xfsdump_quotas' \
1305        'Only in RESTORE_DIR: xfsdump_quotas_group' 
1306 }
1307
1308 #
1309 # Look for the quota file in the output
1310 # Ensure that it is there if it should be
1311 # Filter it out so that the output is always the same
1312 # even with no quotas
1313 #
1314 _check_quota_file()
1315 {
1316    _check_quota 'xfsdump_quotas' 'xfsdump_quotas_group'
1317 }
1318
1319 # make sure this script returns success
1320 /bin/true