24fb1090587ce297738e23791b20f9f18253d43d
[platform/upstream/btrfs-progs.git] / tests / fsck-tests.sh
1 #!/bin/bash
2 #
3 # loop through all of our bad images and make sure fsck repairs them properly
4 #
5 # It's GPL, same as everything else in this tree.
6 #
7
8 here=`pwd`
9 TEST_DEV=
10 TEST_MNT=
11 RESULT="fsck-tests-results.txt"
12
13 _fail()
14 {
15         echo "$*" | tee -a $RESULT
16         exit 1
17 }
18
19 run_check()
20 {
21         echo "############### $@" >> $RESULT 2>&1
22         "$@" >> $RESULT 2>&1 || _fail "failed: $@"
23 }
24
25 check_prereq()
26 {
27         if ! [ -f $here/$1 ]; then
28                 _fail "Failed prerequisities: $1";
29         fi
30 }
31
32 rm -f $RESULT
33
34 # test rely on corrupting blocks tool
35 check_prereq btrfs-corrupt-block
36 check_prereq btrfs-image
37 check_prereq btrfs
38
39 # Some broken filesystem images are kept as .img files, created by the tool
40 # btrfs-image, and others are kept as .tar.xz files that contain raw filesystem
41 # image (the backing file of a loop device, as a sparse file). The reason for
42 # keeping some as tarballs of raw images is that for these cases btrfs-image
43 # isn't able to preserve all the (bad) filesystem structure for some reason.
44 for i in $(find $here/tests/fsck-tests -name '*.img' -o -name '*.tar.xz' | sort)
45 do
46         echo "     [TEST]    $(basename $i)"
47         echo "testing image $i" >> $RESULT
48
49         extension=${i#*.}
50
51         if [ $extension == "img" ]; then
52                 run_check $here/btrfs-image -r $i test.img
53         else
54                 run_check tar xJf $i
55         fi
56
57         $here/btrfs check test.img >> $RESULT 2>&1
58         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
59
60         run_check $here/btrfs check --repair test.img
61         run_check $here/btrfs check test.img
62 done
63
64 if [ -z $TEST_DEV ] || [ -z $TEST_MNT ];then
65         echo "     [NOTRUN] extent tree rebuild"
66         exit 0
67 fi
68
69 # test whether fsck can rebuild a corrupted extent tree
70 test_extent_tree_rebuild()
71 {
72         echo "     [TEST]    extent tree rebuild"
73         $here/mkfs.btrfs -f $TEST_DEV >> /dev/null 2>&1 || _fail "fail to mkfs"
74
75         run_check mount $TEST_DEV $TEST_MNT
76         cp -aR /lib/modules/`uname -r`/ $TEST_MNT 2>&1
77
78         for i in `seq 1 100`;do
79                 $here/btrfs sub snapshot $TEST_MNT \
80                         $TEST_MNT/snapaaaaaaa_$i >& /dev/null
81         done
82         run_check umount $TEST_DEV
83
84         # get extent root bytenr
85         extent_root_bytenr=`$here/btrfs-debug-tree -r $TEST_DEV | grep extent | awk '{print $7}'`
86         if [ -z $extent_root_bytenr ];then
87                 _fail "fail to get extent root bytenr"
88         fi
89
90         # corrupt extent root node block
91         run_check $here/btrfs-corrupt-block -l $extent_root_bytenr \
92                 -b 4096 $TEST_DEV
93
94         $here/btrfs check $TEST_DEV >& /dev/null && \
95                         _fail "btrfs check should detect failure"
96         run_check $here/btrfs check --init-extent-tree $TEST_DEV
97         run_check $here/btrfs check $TEST_DEV
98 }
99
100 test_extent_tree_rebuild