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