Fix up 064 for quota checks.
[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/id:[        ]*[0-9a-f-]*/id: ID/'  \
757       -e 's/time:[      ].*/time: TIME/'       \
758       -e 's/date:[      ].*/date: DATE/'       \
759       -e 's/dump begun .*/dump begun DATE/'    \
760       -e 's/[0-9][0-9]* seconds/SECS seconds/' \
761       -e 's/restore.[0-9][0-9]*/restore.PID/' \
762       -e 's/ino [0-9][0-9]*/ino INO/' \
763       -e '/: dump size/s/[0-9][0-9]*/NUM/'     \
764       -e '/dump size:/s/[0-9][0-9]*/NUM/'      \
765       -e '/dump size per stream:/s/[0-9][0-9]*/NUM/' \
766       -e 's/\(media file size[   ]*\)[0-9][0-9]*/\1NUM/' \
767       -e 's/\(mfile size:[       ]*\)[0-9][0-9]*/\1NUM/' \
768       -e '/drive[        ]*[0-9][0-9]*:/d' \
769       -e '/\/dev\/tty/d' \
770       -e '/inventory session uuid/d' \
771       -e '/ - Running single-threaded/d' \
772       -e '/^.*I\/O metrics: .*$/d' \
773       -e 's/1048576/BLOCKSZ/' \
774       -e 's/2097152/BLOCKSZ/' \
775       -e 's/(pid[        ]*[1-9][0-9]*)/\(pid PID\)/' \
776   | perl -ne '
777       if ($_ =~ /(?:Dump|Restore) Summary/) {
778         $skip = 1;
779       } elsif ($_ =~ /(?:Dump|Restore) Status/) {
780         $skip = 0;
781       }
782       print if (! $skip);'
783 }
784
785 _dump_filter()
786 {
787    if $do_quota_check
788    then
789        _dump_filter_main | _check_quota_dumprestore
790    else
791        _dump_filter_main
792    fi
793 }
794
795 _invutil_filter()
796 {
797   _dump_filter_main \
798   | sed \
799         -e 's/UUID[     ]*:[    ][0-9a-f-]*/UUID                :       ID/' \
800         -e 's/TIME OF DUMP[     ]*:.*/TIME OF DUMP      :       TIME/' \
801         -e 's/HOSTNAME:SCRATCH_MNT.*/HOSTNAME:SCRATCH_MNT/' \
802         -e 's#inventory/[0-9a-f-]*#inventory/UUID#' \
803
804 }
805
806
807 _dir_filter()
808 {
809   sed \
810     -e "s#$dump_file#DUMP_FILE#"      \
811     -e "s#$SCRATCH_DEV#SCRATCH_DEV#"        \
812     -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
813     -e "s#$dumptape#TAPE_DEV#"         \
814     -e "s#$dump_dir#DUMP_DIR#g"       \
815     -e "s#$restore_dir#RESTORE_DIR#g" \
816     -e "s#$SCRATCH_MNT#SCRATCH_MNT#g"       \
817     -e "s#$dump_sdir#DUMP_SUBDIR#g"   \
818     -e "s#$restore_sdir#RESTORE_SUBDIR#g" \
819     -e "s#$$#PID#g" \
820
821 }
822
823 #
824 # Note: requires a space between option letter and argument 
825 #
826 _parse_args()
827 {
828     OPTIND=0
829     dump_args=""
830     while [ $# -gt 0 ]
831     do
832         case $1
833         in
834         -f)
835             [ -z "$2" ] && _error "missing argument for -f"
836             dumptape=$2 
837             shift
838             ;;
839         -L)
840             [ -z "$2" ] && _error "missing argument for -L"
841             session_label=$2
842             shift
843             ;;
844         -o)
845             dump_args="$dump_args -o"
846             ;;
847         -F)
848             dump_args="$dump_args -F"
849             ;;
850         --multi)
851             multi=$2
852             shift
853             ;;
854         -q)
855             do_quota_check=true
856             ;;
857         -Q)
858             do_quota_check=false
859             ;;
860         -l)
861             [ -z "$2" ] && _error "missing argument for -l"
862             dump_args="$dump_args -l$2"
863             shift
864             ;;
865         *)
866             _error "invalid argument to common.dump function: $1"
867             ;;
868         esac
869         shift
870     done
871 }
872
873
874 #
875 # Dump a subdir
876 #
877 _do_dump_sub()
878 {
879     _parse_args $*
880
881     echo "Dumping to tape..."
882     opts="$_dump_debug$dump_args -s $dump_sdir -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
883     echo "xfsdump $opts" | _dir_filter  
884     xfsdump $opts 2>&1 | tee -a $seq.full | _dump_filter
885 }
886
887 #
888 # Do dump to tape
889 #
890 _do_dump()
891 {
892     _parse_args $*
893
894     echo "Dumping to tape..."
895     opts="$_dump_debug$dump_args -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
896     echo "xfsdump $opts" | _dir_filter  
897     xfsdump $opts 2>&1 | tee -a $seq.full | _dump_filter
898 }
899
900
901 #
902 # Do full dump with -m
903 #
904 _do_dump_min()
905 {
906     _parse_args $*
907
908     echo "Dumping to tape..."
909     onemeg=1048576
910     opts="$_dump_debug$dump_args -m -b $onemeg -l0 -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
911     echo "xfsdump $opts" | _dir_filter  
912     xfsdump $opts 2>&1 | tee -a $seq.full | _dump_filter
913 }
914
915
916 #
917 # Do full dump to file
918 #
919 _do_dump_file()
920 {
921     _parse_args $*
922
923     echo "Dumping to file..."
924     opts="$_dump_debug$dump_args -f $dump_file -M $media_label -L $session_label $SCRATCH_MNT"
925     echo "xfsdump $opts" | _dir_filter  
926     xfsdump $opts 2>&1 | tee -a $seq.full | _dump_filter
927 }
928
929 #
930 # Do full dump to multiple files
931 #
932 _do_dump_multi_file()
933 {
934     _parse_args "$@"
935
936     multi_args=""
937
938     i=0
939     while [ $i -lt $multi ]
940     do
941         multi_args="$multi_args -f $dump_file.$i -M $media_label.$i"
942         i=`expr $i + 1`
943     done
944
945     echo "Dumping to files..."
946     opts="$_dump_debug$dump_args $multi_args -L $session_label $SCRATCH_MNT"
947     echo "xfsdump $opts" | _dir_filter  
948     xfsdump $opts 2>&1 | tee -a $seq.full | _dump_filter
949 }
950
951
952 _prepare_restore_dir()
953 {
954     rm -rf $restore_dir
955     mkdir $restore_dir ||\
956         _error "failed to mkdir $restore_dir"
957 }
958
959
960 #
961 # Get tape ready and restore dir
962 #
963 _prepare_restore()
964 {
965     _prepare_restore_dir
966
967     echo "Rewinding tape"
968     _rewind
969 }
970
971 #
972 # Restore the tape into $restore_dir
973 #
974 _do_restore()
975 {
976     _parse_args $*
977     _prepare_restore
978
979
980     echo "Restoring from tape..."
981     opts="$_restore_debug -f $dumptape  -L $session_label $restore_dir"
982     echo "xfsrestore $opts" | _dir_filter  
983     xfsrestore $opts 2>&1 | tee -a $seq.full | _dump_filter
984 }
985
986 #
987 # Restore the tape into $restore_dir using -m
988 #
989 _do_restore_min()
990 {
991     _parse_args $*
992     _prepare_restore
993
994     echo "Restoring from tape..."
995     onemeg=1048576
996     opts="$_restore_debug -m -b $onemeg -f $dumptape  -L $session_label $restore_dir"
997     echo "xfsrestore $opts" | _dir_filter  
998     xfsrestore $opts 2>&1 | tee -a $seq.full | _dump_filter
999 }
1000
1001 #
1002 # Restore the tape from a dump file
1003 #
1004 _do_restore_file()
1005 {
1006     _parse_args $*
1007     _prepare_restore_dir
1008
1009     echo "Restoring from file..."
1010     opts="$_restore_debug -f $dump_file  -L $session_label $restore_dir"
1011     echo "xfsrestore $opts" | _dir_filter  
1012     xfsrestore $opts 2>&1 | tee -a $seq.full | _dump_filter
1013 }
1014
1015 #
1016 # Cumulative restore from a file
1017 # Need to specify the dump level e.g. "-l 0"
1018 #
1019 _do_restore_file_cum()
1020 {
1021     _parse_args $*
1022     if echo $dump_args | grep '\-l0' >/dev/null; then
1023         _prepare_restore_dir
1024     fi
1025
1026     echo "Restoring cumumlative from file..."
1027     opts="$_restore_debug -f $dump_file -r $restore_dir"
1028     echo "xfsrestore $opts" | _dir_filter  
1029     xfsrestore $opts 2>&1 | tee -a $seq.full | _dump_filter
1030 }
1031
1032 _do_restore_toc()
1033 {
1034     echo "Contents of dump ..."
1035     opts="$_restore_debug -f $dump_file -t"
1036     echo "xfsrestore $opts" | _dir_filter
1037     cd $SCRATCH_MNT # for IRIX which needs xfs cwd
1038     xfsrestore $opts 2>&1 | tee -a $seq.full | _dump_filter_main |\
1039     _check_quota_file |\
1040     $AWK_PROG 'NF != 1 { print; next }
1041                {files = sprintf("%s\n%s", files, $1)}
1042                 END { print files | "sort" } '
1043     # the above awk code is to alpha sort only the output
1044     # of files (and not the verbose restore msgs)
1045     cd $here # put back
1046 }
1047
1048 #
1049 # Restore the tape from multiple dump files
1050 #
1051 _do_restore_multi_file()
1052 {
1053     _parse_args "$@"
1054     _prepare_restore_dir
1055
1056     multi_args=""
1057
1058     i=0
1059     while [ $i -lt $multi ]
1060     do
1061         multi_args="$multi_args -f $dump_file.$i"
1062         i=`expr $i + 1`
1063     done
1064
1065     echo "Restoring from file..."
1066     opts="$_restore_debug $multi_args -L $session_label $restore_dir"
1067     echo "xfsrestore $opts" | _dir_filter  
1068     xfsrestore $opts 2>&1 | tee -a $seq.full | _dump_filter
1069 }
1070
1071 #
1072 # Do xfsdump piped into xfsrestore - xfsdump | xfsrestore
1073 #
1074 # Use -s as we want to dump and restore to the same xfs partition
1075 #
1076 _do_dump_restore()
1077 {
1078     _parse_args $*
1079     _prepare_restore_dir
1080     echo "xfsdump|xfsrestore ..."
1081     restore_opts="$_restore_debug - $restore_dir"
1082     dump_opts="$_dump_debug$dump_args -s $dump_sdir - $SCRATCH_MNT"
1083     echo "xfsdump $dump_opts | xfsrestore $restore_opts" | _dir_filter  
1084     xfsdump $dump_opts 2>$tmp.dump.mlog | xfsrestore $restore_opts 2>&1 | tee -a $seq.full | _dump_filter
1085     _dump_filter <$tmp.dump.mlog
1086 }
1087
1088 #
1089 # Compare dumped subdirectory with restored dir
1090 # using ls -lR.
1091 # Thus no contents are compared but permissions, sizes,
1092 # owners, etc... are.
1093 #
1094 _ls_compare_sub()
1095 {
1096     #
1097     # verify we got back what we dumped
1098     #
1099     echo "Comparing listing of dump directory with restore directory"
1100     ls -lR $dump_dir | tee -a $seq.full | _ls_filter >$tmp.dump_dir
1101     ls -lR $restore_dir/$dump_sdir | tee -a $seq.full | _ls_filter \
1102     | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1103
1104     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1105 }
1106
1107 #
1108 # filter out the date fields
1109 #
1110 _ls_nodate_filter()
1111 {
1112     $AWK_PROG 'NF == 9 { print $1, $2, $3, $4, $9 }'
1113 }
1114
1115 #
1116 # _ls_compare_sub but don't compare dates
1117 _ls_nodate_compare_sub()
1118 {
1119     #
1120     # verify we got back what we dumped
1121     #
1122     echo "Comparing listing of dump directory with restore directory"
1123     ls -lR $dump_dir | tee -a $seq.full | _ls_filter | _ls_nodate_filter >$tmp.dump_dir
1124     ls -lR $restore_dir/$dump_sdir | tee -a $seq.full | _ls_filter \
1125     | _ls_nodate_filter | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1126
1127     diff -cs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1128 }
1129
1130 #
1131 # Compare using recursive diff the files of the dumped
1132 # subdirectory.
1133 # This one will compare the contents.
1134 #
1135 _diff_compare_sub()
1136 {
1137     echo "Comparing dump directory with restore directory"
1138     diff -rs $dump_dir $restore_dir/$dump_sdir | _dir_filter
1139 }
1140
1141 _get_eas_on_path()
1142 {
1143     _path=$1
1144
1145     find $_path -exec attr -l {} \; |\
1146     awk '{print $9, $2}' |\
1147     sed 's/["]//g' |\
1148     sort |\
1149     while read file ea_name; do
1150         attr -g $ea_name $file
1151     done
1152 }
1153
1154 #
1155 # Compare the extended attributes of the files/dirs
1156 # b/w the dumped and restore dirs.
1157 #
1158 #
1159 # Attribute "attr5" had a 8 byte value for /spare1/dump.5460/dir:
1160 # Attribute "attr5" had a 8 byte value for /spare1/restore.5460/dump.5460/dir:
1161 #
1162 _diff_compare_eas()
1163 {
1164     echo "Comparing dump directory with restore directory"
1165     echo "Looking at the extended attributes (EAs)"
1166     echo "EAs on dump"
1167     _get_eas_on_path $dump_dir | tee $seq.ea1 | _dir_filter
1168     echo "EAs on restore"
1169     _get_eas_on_path $restore_dir/$dump_sdir \
1170     | sed -e "s#$restore_sdir\/##" \
1171     | tee $seq.ea2 \
1172     | _dir_filter
1173     diff -s $seq.ea1 $seq.ea2
1174 }
1175
1176
1177 #
1178 # Compare using recursive diff the files of the dumped
1179 # filesystem
1180 #
1181 _diff_compare()
1182 {
1183     echo "Comparing dump directory with restore directory"
1184     diff -rs $SCRATCH_MNT $restore_dir | _dir_filter | _check_quota_diff
1185 }
1186
1187 #
1188 # Check out the dump inventory
1189 #
1190 _dump_inventory()
1191 {
1192     xfsdump $_dump_debug -I | tee -a $seq.full | _dump_filter_main
1193 }
1194
1195 #
1196 # Do the xfsinvutil cmd with debug and filters
1197 # Need to set variable: "$middate" to the invutil date 
1198 #
1199 _do_invutil()
1200 {
1201     host=`hostname`
1202     echo "xfsinvutil $_invutil_debug -M $host:$SCRATCH_MNT \"$middate\" $*" >$seq.full
1203     xfsinvutil $_invutil_debug $* -M $host:$SCRATCH_MNT "$middate" \
1204     | tee -a $seq.full | _invutil_filter
1205 }
1206
1207 #
1208 # ensure we can find the user quota msg if user quotas are on
1209 # ensure we can find the group quota msg if group quotas are on
1210 #
1211 _check_quota()
1212 {
1213     usermsg=$1 
1214     groupmsg=$2 
1215     uquota=0
1216     gquota=0 
1217     $here/src/feature -U $SCRATCH_DEV && uquota=1
1218     $here/src/feature -G $SCRATCH_DEV && gquota=1
1219
1220     $AWK_PROG -v uquota=$uquota -v gquota=$gquota -v full=$seq.full \
1221               -v usermsg=$usermsg -v groupmsg=$groupmsg '
1222         $0 ~ groupmsg {
1223                         print "Found group quota:", $0 >>full
1224                         found_gquota = 1
1225                         if (!gquota) {
1226                             print "Found extra:", $0
1227                         }
1228                         next
1229         }
1230         $0 ~ usermsg {
1231                         print "Found user quota:", $0 >>full
1232                         found_uquota = 1
1233                         if (!uquota) {
1234                             print "Found extra:", $0
1235                         }
1236                         next
1237         }
1238                         { print }
1239         END {
1240                 if (uquota && !found_uquota) {
1241                     print "Missing: ", usermsg
1242                 }
1243                 if (gquota && !found_gquota) {
1244                     print "Missing: ", groupmsg
1245                 }
1246         }
1247     '
1248 }
1249
1250 #
1251 # Look for:
1252 # xfsdump: saving user quota information for: SCRATCH_MNT
1253 # xfsdump: saving group quota information for: SCRATCH_MNT
1254 # xfsrestore: user quota information written to ...'
1255 # xfsrestore: group quota information written to ...'
1256 #
1257 _check_quota_dumprestore()
1258 {
1259    _check_quota 'user quota information' \
1260                 'group quota information'
1261 }
1262
1263 #
1264 # Look for:
1265 # Only in RESTORE_DIR: xfsdump_quotas
1266 # Only in RESTORE_DIR: xfsdump_quotas_group
1267 #
1268 _check_quota_diff()
1269 {
1270    _check_quota 'Only in RESTORE_DIR: xfsdump_quotas' \
1271        'Only in RESTORE_DIR: xfsdump_quotas_group' 
1272 }
1273
1274 #
1275 # Look for the quota file in the output
1276 # Ensure that it is there if it should be
1277 # Filter it out so that the output is always the same
1278 # even with no quotas
1279 #
1280 _check_quota_file()
1281 {
1282    _check_quota 'xfsdump_quotas' 'xfsdump_quotas_group'
1283 }
1284
1285 # make sure this script returns success
1286 /bin/true