7837cc373a8f18a4336d4d33b6c0ca2bb9539b18
[xfstests-dev.git] / tests / btrfs / 265
1 #! /bin/bash
2 # FS QA Test No. 265
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 seqres=$RESULT_DIR/$seq
27 seqres=$RESULT_DIR/$seq
28 seqres=$RESULT_DIR/$seq
29 echo "QA output created by $seq"
30
31 here=`pwd`
32 tmp=/tmp/$$
33 status=1        # failure is the default!
34 dev_removed=0
35 trap "_cleanup; exit \$status" 0 1 2 3 15
36
37 _cleanup()
38 {
39     cd /
40     rm -f $tmp.*
41     if [ $dev_removed == 1 ]; then
42         umount $SCRATCH_MNT
43         _devmgt_add "${DEVHTL}"
44     fi
45 }
46
47 # get standard environment, filters and checks
48 . ./common/rc
49 . ./common/filter
50
51 _need_to_be_root
52 _supported_fs btrfs
53 _supported_os Linux
54 _require_scratch
55 _require_scratch_dev_pool
56 _require_deletable_scratch_dev_pool
57
58 # Test cases related to raid in btrfs
59 _test_raid0()
60 {
61         export MKFS_OPTIONS="-m raid0 -d raid0"
62         _scratch_mkfs $SCRATCH_DEV_POOL > /dev/null 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_mkfs $SCRATCH_DEV_POOL > /dev/null 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_mkfs $SCRATCH_DEV_POOL > /dev/null 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_mkfs $SCRATCH_DEV_POOL > /dev/null 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 > /dev/null 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 1 $n`; do
113                 $BTRFS_UTIL_PROG device add ${devs[$i]} $SCRATCH_MNT > /dev/null 2>&1 || _fail "device add failed"
114         done
115         $BTRFS_UTIL_PROG filesystem balance $SCRATCH_MNT || _fail "balance failed"
116         umount $SCRATCH_MNT
117 }
118
119 _test_replace()
120 {
121         local i
122         local devs=( $SCRATCH_DEV_POOL )
123         local n=${#devs[@]}
124         local ds
125         local d
126         local DEVHTL=""
127
128         # exclude the last disk in the disk pool
129         n=$(($n-1))
130         ds=${devs[@]:0:$n}
131
132         export MKFS_OPTIONS="-m raid1 -d raid1"
133         _scratch_mkfs "$ds" > /dev/null 2>&1 || _fail "tr: mkfs failed"
134         _scratch_mount
135         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
136         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
137
138         #pick the 2nd last disk 
139         ds=${devs[@]:$(($n-1)):1}
140
141         # retrive the HTL for this scsi disk
142         d=`echo $ds|cut -d"/" -f3`
143         DEVHTL=`ls -l /sys/class/block/${d} | rev | cut -d "/" -f 3 | rev`
144
145         #fail disk
146         _devmgt_remove ${DEVHTL}
147         dev_removed=1
148
149         $BTRFS_UTIL_PROG fi show $SCRATCH_DEV | grep "Some devices missing" > /dev/null || _fail \
150                                                         "btrfs did not report device missing"
151
152         # add a new disk to btrfs
153         ds=${devs[@]:$(($n)):1}
154         $BTRFS_UTIL_PROG device add ${ds} $SCRATCH_MNT > /dev/null 2>&1 || _fail "dev add failed"
155         # in some system balance fails if there is no delay (a bug)
156         # putting sleep 10 to work around as of now
157         # sleep 10
158         $BTRFS_UTIL_PROG filesystem balance $SCRATCH_MNT || _fail "dev balance failed"
159
160         # cleaup. add the removed disk
161         umount $SCRATCH_MNT
162         _devmgt_add "${DEVHTL}"
163         dev_removed=0
164 }
165
166 _test_remove()
167 {
168         _scratch_mkfs "$SCRATCH_DEV_POOL" > /dev/null 2>&1 || _fail "mkfs failed"
169         _scratch_mount
170         dirp=`mktemp -duq $SCRATCH_MNT/dir.XXXXXX`
171         _populate_fs -n 1 -f 20 -d 10 -r $dirp -s 10
172
173         # pick last dev in the list
174         dev_del=`echo ${SCRATCH_DEV_POOL} | awk '{print $NF}'`
175         $BTRFS_UTIL_PROG device delete $dev_del $SCRATCH_MNT || _fail "btrfs device delete failed"
176         $BTRFS_UTIL_PROG filesystem show $SCRATCH_DEV 2>&1 | grep $dev_del > /dev/null && _fail "btrfs still shows the deleted dev"
177         umount $SCRATCH_MNT
178 }
179
180 _test_raid0
181 _test_raid1
182 _test_raid10
183 _test_single
184 _test_add
185 _test_replace
186 _test_remove
187
188 echo "Silence is golden"
189 status=0; exit