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