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