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