9ce9d9462ee7b7f3b5b35017da7260aa94221274
[xfstests-dev.git] / tests / btrfs / 003
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2011 Oracle.  All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/003
6 #
7 # btrfs vol tests
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 dev_removed=0
17 removed_dev_htl=""
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 # Check if all scratch dev pools are deletable
21 deletable_scratch_dev_pool()
22 {
23         local i
24         local x
25         for i in $SCRATCH_DEV_POOL; do
26                 x=`echo $i | cut -d"/" -f 3`
27                 if [ ! -f /sys/class/block/${x}/device/delete ]; then
28                         return 1
29                 fi
30         done
31
32         return 0
33 }
34
35 _cleanup()
36 {
37     cd /
38     rm -f $tmp.*
39     if [ $dev_removed == 1 ]; then
40         _scratch_unmount
41         _devmgt_add "${removed_dev_htl}"
42     fi
43 }
44
45 # get standard environment, filters and checks
46 . ./common/rc
47 . ./common/filter
48
49 _supported_fs btrfs
50 _supported_os Linux
51 _require_scratch
52 _require_scratch_dev_pool 4
53 _require_command "$WIPEFS_PROG" wipefs
54
55 rm -f $seqres.full
56
57 # Test cases related to raid in btrfs
58 _test_raid0()
59 {
60         export MKFS_OPTIONS="-m raid0 -d raid0"
61         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
62         _scratch_mount
63         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
64         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10 -c
65         _scratch_unmount
66 }
67
68 _test_raid1()
69 {
70         export MKFS_OPTIONS="-m raid1 -d raid1"
71         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
72         _scratch_mount
73         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
74         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10 -c
75         _scratch_unmount
76 }
77
78 _test_raid10()
79 {
80         export MKFS_OPTIONS="-m raid10 -d raid10"
81         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
82         _scratch_mount
83         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
84         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
85         _scratch_unmount
86 }
87
88 _test_single()
89 {
90         export MKFS_OPTIONS="-m single -d single"
91         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
92         _scratch_mount
93         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
94         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
95         _scratch_unmount
96 }
97
98 _test_add()
99 {
100         local i
101         local -a devs="( $SCRATCH_DEV_POOL )"
102         local n=${#devs[@]}
103
104         n=$(($n-1))
105
106         export MKFS_OPTIONS=""
107         _scratch_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
108         _scratch_mount
109         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
110         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
111         for i in `seq 2 $n`; do
112                 $WIPEFS_PROG -a ${devs[$i]} >> $seqres.full 2>&1 || \
113                         _fail "wipefs failed"
114                 $BTRFS_UTIL_PROG device add ${devs[$i]} $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "device add failed"
115         done
116         _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
117         _scratch_unmount
118 }
119
120 _test_replace()
121 {
122         local i
123         local devs=( $SCRATCH_DEV_POOL )
124         local n=${#devs[@]}
125         local ds
126         local d
127
128         # If scratch devs are not deletable skip this test
129         if ! deletable_scratch_dev_pool; then return 0; fi
130
131         # exclude the first and the last disk in the disk pool
132         n=$(($n-1))
133         ds=${devs[@]:1:$(($n-1))}
134
135         export MKFS_OPTIONS="-m raid1 -d raid1"
136         _scratch_mkfs "$ds" >> $seqres.full 2>&1 || _fail "tr: mkfs failed"
137         _scratch_mount
138         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
139         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
140
141         #pick the 2nd last disk 
142         ds=${devs[@]:$(($n-1)):1}
143
144         # retrive the HTL for this scsi disk
145         d=`echo $ds|cut -d"/" -f3`
146         removed_dev_htl=`ls -l /sys/class/block/${d} | rev | cut -d "/" -f 3 | rev`
147
148         #fail disk
149         _devmgt_remove ${removed_dev_htl} $ds
150         dev_removed=1
151
152         $BTRFS_UTIL_PROG filesystem show $SCRATCH_DEV | grep "Some devices missing" >> $seqres.full || _fail \
153                                                         "btrfs did not report device missing"
154
155         # add a new disk to btrfs
156         ds=${devs[@]:$(($n)):1}
157         $WIPEFS_PROG -a ${ds} >> $seqres.full 2>&1 || _fail "wipefs failed"
158         $BTRFS_UTIL_PROG device add ${ds} $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "dev add failed"
159         # in some system balance fails if there is no delay (a bug)
160         # putting sleep 10 to work around as of now
161         # sleep 10
162         _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
163
164         # cleaup. add the removed disk
165         _scratch_unmount
166         _devmgt_add "${removed_dev_htl}"
167         dev_removed=0
168 }
169
170 _test_remove()
171 {
172         _scratch_pool_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
173         _scratch_mount
174         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
175         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
176
177         # pick last dev in the list
178         dev_del=`echo ${SCRATCH_DEV_POOL} | awk '{print $NF}'`
179         $BTRFS_UTIL_PROG device delete $dev_del $SCRATCH_MNT || _fail "btrfs device delete failed"
180         $BTRFS_UTIL_PROG filesystem show $SCRATCH_DEV 2>&1 | grep $dev_del >> $seqres.full && _fail "btrfs still shows the deleted dev"
181         _scratch_unmount
182 }
183
184 _test_raid0
185 _test_raid1
186 _test_raid10
187 _test_single
188 _test_add
189 _test_replace
190 _test_remove
191
192 echo "Silence is golden"
193 status=0; exit