common: fix "utility required warning" with empty utility name
[xfstests-dev.git] / tests / btrfs / 011
1 #! /bin/bash
2 # FSQA Test No. btrfs/011
3 #
4 # Test of the btrfs replace operation.
5 #
6 # The amount of tests done depends on the number of devices in the
7 # SCRATCH_DEV_POOL. For full test coverage, at least 5 devices should
8 # be available (e.g. 5 partitions).
9 #
10 # The source and target devices for the replace operation are
11 # arbitrarily chosen out of SCRATCH_DEV_POOl. Since the target device
12 # mustn't be smaller than the source device, the requirement for this
13 # test is that all devices have _exactly_ the same size. If this is
14 # not the case, this test is not run.
15 #
16 # To check the filesystems after replacing a device, a scrub run is
17 # performed, a btrfsck run, and finally the filesystem is remounted.
18 #
19 #-----------------------------------------------------------------------
20 # Copyright (C) 2013 STRATO.  All rights reserved.
21 #
22 # This program is free software; you can redistribute it and/or
23 # modify it under the terms of the GNU General Public License as
24 # published by the Free Software Foundation.
25 #
26 # This program is distributed in the hope that it would be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 # GNU General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write the Free Software Foundation,
33 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
34 #
35 #-----------------------------------------------------------------------
36 #
37
38 seq=`basename $0`
39 seqres=$RESULT_DIR/$seq
40 echo "QA output created by $seq"
41
42 here=`pwd`
43 tmp=/tmp/$$
44 status=1
45 noise_pid=0
46
47 _cleanup()
48 {
49         if [ $noise_pid -ne 0 ] && ps -p $noise_pid | grep -q $noise_pid; then
50                 kill -TERM $noise_pid
51         fi
52         wait
53         rm -f $tmp.tmp
54 }
55 trap "_cleanup; exit \$status" 0 1 2 3 15
56
57 # get standard environment, filters and checks
58 . ./common/rc
59 . ./common/filter
60
61 # real QA test starts here
62 _need_to_be_root
63 _supported_fs btrfs
64 _require_scratch_nocheck
65 _require_scratch_dev_pool 4
66 _require_command "$BTRFS_SHOW_SUPER_PROG" btrfs-show-super
67
68 rm -f $seqres.full
69 rm -f $tmp.tmp
70
71 echo "*** test btrfs replace"
72
73 workout()
74 {
75         local mkfs_options="$1"
76         local num_devs4raid="$2"
77         local with_cancel="$3"
78         local quick="$4"
79         local source_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $1}'`"
80         local target_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $NF}'`"
81         local fssize
82
83         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt `expr $num_devs4raid + 1` ]; then
84                 echo "Skip workout $1 $2 $3 $4" >> $seqres.full
85                 echo "Too few devices in SCRATCH_DEV_POOL $SCRATCH_DEV_POOL, required: " `expr $num_devs4raid + 1` >> $seqres.full
86                 return 0
87         fi
88
89         # use min number of disks in order to fill up the disk to replace
90         # as much as possible
91         local used_devs_without_1st="`echo $SCRATCH_DEV_POOL | \
92                 awk '{ORS=\" \"; for (i = 2; i <= (NF - 1 < '$num_devs4raid' ? NF - 1 : '$num_devs4raid'); i++) print $i}'`"
93
94         # _scratch_mkfs adds the 1st device again (which is $SCRATCH_DEV)
95         _scratch_mkfs $mkfs_options $used_devs_without_1st >> $seqres.full 2>&1 || _fail "mkfs failed"
96
97         # create a filesystem on the target device just for the sake of
98         # being able to query its size with btrfs-show-super
99         $MKFS_BTRFS_PROG $MKFS_OPTIONS $target_dev >> $seqres.full 2>&1 || _fail "mkfs target_dev failed"
100
101         # The source and target devices for the replace operation are
102         # arbitrarily chosen out of the pool. Since the target device mustn't
103         # be smaller than the source device, the requirement for this test is
104         # that all devices have _exactly_ the same size. If this is not the
105         # case, this test is not run.
106         local num_lines=`$BTRFS_SHOW_SUPER_PROG $SCRATCH_DEV $used_devs_without_1st $target_dev | grep dev_item.total_bytes | uniq | wc -l`
107         if [ $num_lines -gt 1 ]; then
108                 _notrun "Different device sizes detected"
109         fi
110
111         if [ `$BTRFS_SHOW_SUPER_PROG $SCRATCH_DEV | grep dev_item.total_bytes | awk '{print $2}'` -lt 2500000000 ]; then
112                 _notrun "device size too small"
113         fi
114
115         _scratch_mount
116
117         # Generate metadata and some minimal user data, generate 500 times
118         # 20K extents in the data chunk and fill up metadata with inline
119         # extents.
120         for i in `seq 1 500`; do
121                 dd if=/dev/urandom of=$SCRATCH_MNT/l$i bs=16385 count=1
122                 dd if=/dev/urandom of=$SCRATCH_MNT/s$i bs=3800 count=1
123         done > /dev/null 2>&1
124
125         # /dev/urandom is slow but has the benefit that the generated
126         # contents does not shrink during compression.
127         # Generate a template once and quickly copy it multiple times.
128         # Obviously with online deduplication this will not work anymore.
129         dd if=/dev/urandom of=$SCRATCH_MNT/t0 bs=1M count=1 > /dev/null 2>&1
130
131         if [ "${quick}Q" = "thoroughQ" ]; then
132                 # The intention of this "thorough" test is to increase
133                 # the probability of random errors, in particular in
134                 # conjunction with the background noise generator and
135                 # a sync call while the replace operation is ongoing.
136                 fssize=2048
137         elif [ "${with_cancel}Q" = "cancelQ" ]; then
138                 # The goal is to produce enough data to prevent that the
139                 # replace operation finishes before the cancel request
140                 # is started.
141                 fssize=1024
142         else
143                 fssize=64
144         fi
145
146         # since the available size was tested before, do not tolerate
147         # any failures
148         for i in `seq $fssize`; do
149                 cp $SCRATCH_MNT/t0 $SCRATCH_MNT/t$i || _fail "cp failed"
150         done > /dev/null 2>> $seqres.full
151         sync; sync
152
153         btrfs_replace_test $source_dev $target_dev "" $with_cancel $quick
154         umount $SCRATCH_MNT > /dev/null 2>&1
155
156         if echo $mkfs_options | egrep -qv "raid1|raid5|raid6|raid10" || \
157            [ "${with_cancel}Q" = "cancelQ" ]; then
158                 # the -r option has no effect without mirrors, skip -r test
159                 # in this case, and if only the canceling should be tested
160                 # as well
161                 return 0
162         fi
163
164         # One more time with the '-r' option this time. Instead of wasting
165         # time to populate the filesystem with data again, use the
166         # existing filesystem in the state as it is after the previous
167         # replace operation.
168         # If possible, use a strategy to select the source and target
169         # device so that we really change bits on the target disk, see
170         # below.
171
172         # The default: For the 2nd run, the new target drive is the old
173         # source drive, and the new source drive is the old target drive.
174         # Since except for the noise data, the copied data is already on
175         # the new target disk (which is the old source disk), this is not
176         # optimal to check whether data is copied correctly.
177         local tmp_dev="$source_dev"
178         source_dev="$target_dev"
179         target_dev="$tmp_dev"
180
181         # If we have at least one more device in the SCRATCH_DEV_POOL than
182         # used so far, use one of those for the new target devive.
183         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -gt `expr $num_devs4raid + 1` ]; then
184                 target_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $(NF-1)}'`"
185         fi
186
187         # If the filesystem is built out of more than one devices, use a
188         # different source device for this round.
189         if [ $num_devs4raid -gt 1 ]; then
190                 source_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $2}'`"
191         fi
192
193         # Mount similar to _scratch_mount, but since the SCRATCH_DEV (the
194         # 1st device in SCRATCH_DEV_POOL) was replaced by the previous
195         # btrfs replace operation, substitute SCRATCH_DEV with a device
196         # that is known to be part of the SCRATCH_MNT filesystem.
197         _mount -t $FSTYP `_scratch_mount_options | sed "s&${SCRATCH_DEV}&${source_dev}&"`
198         if [ $? -ne 0 ]; then
199                 echo "mount failed"
200                 return 1
201         fi
202
203         btrfs_replace_test $source_dev $target_dev "-r" $with_cancel $quick
204         umount $SCRATCH_MNT > /dev/null 2>&1
205 }
206
207 btrfs_replace_test()
208 {
209         local source_dev="$1"
210         local target_dev="$2"
211         local replace_options="$3"
212         local with_cancel="$4"
213         local quick="$5"
214
215         # generate some (slow) background traffic in parallel to the
216         # replace operation. It is not a problem if cat fails early
217         # with ENOSPC.
218         cat /dev/urandom > $SCRATCH_MNT/noise 2>> $seqres.full &
219         noise_pid=$!
220
221         if [ "${with_cancel}Q" = "cancelQ" ]; then
222                 # background the replace operation (no '-B' option given)
223                 $BTRFS_UTIL_PROG replace start -f $source_dev $target_dev $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs replace start failed"
224                 sleep 1
225                 $BTRFS_UTIL_PROG replace cancel $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs replace cancel failed"
226
227                 # 'replace status' waits for the replace operation to finish
228                 # before the status is printed
229                 $BTRFS_UTIL_PROG replace status $SCRATCH_MNT > $tmp.tmp 2>&1
230                 cat $tmp.tmp >> $seqres.full
231                 grep -q canceled $tmp.tmp || _fail "btrfs replace status (canceled) failed"
232         else
233                 if [ "${quick}Q" = "thoroughQ" ]; then
234                         # On current hardware, the thorough test runs
235                         # more than a second. This is a chance to force
236                         # a sync in the middle of the replace operation.
237                         (sleep 1; sync) > /dev/null 2>&1 &
238                 fi
239                 $BTRFS_UTIL_PROG replace start -Bf $source_dev $target_dev $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs replace start failed"
240
241                 $BTRFS_UTIL_PROG replace status $SCRATCH_MNT > $tmp.tmp 2>&1
242                 cat $tmp.tmp >> $seqres.full
243                 grep -q finished $tmp.tmp || _fail "btrfs replace status (finished) failed"
244         fi
245
246         if ps -p $noise_pid | grep -q $noise_pid; then
247                 kill -TERM $noise_pid 2> /dev/null
248         fi
249         noise_pid=0
250         wait
251
252         # scrub tests on-disk data, that's the reason for the sync.
253         # With the '-B' option (don't background), any type of error causes
254         # exit values != 0, including detected correctable and uncorrectable
255         # errors on the device.
256         sync; sync
257         $BTRFS_UTIL_PROG scrub start -B $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs scrub failed"
258
259         # Two tests are performed, the 1st is to btrfsck the filesystem,
260         # and the 2nd test is to mount the filesystem.
261         # Usually _check_btrfs_filesystem would perform the mount test,
262         # but it gets confused by the mount output that shows SCRATCH_MNT
263         # mounted but not being mounted to SCRATCH_DEV. This happens
264         # because in /proc/mounts the 2nd device of the filesystem is
265         # shown after the replace operation. Let's just do the mount
266         # test manually after _check_btrfs_filesystem is finished.
267         umount $SCRATCH_MNT > /dev/null 2>&1
268         if [ "${with_cancel}Q" != "cancelQ" ]; then
269                 # after the replace operation, use the target_dev for everything
270                 _check_btrfs_filesystem $target_dev
271                 _mount -t $FSTYP `_scratch_mount_options | sed "s&${SCRATCH_DEV}&${target_dev}&"`
272         else
273                 _check_btrfs_filesystem $source_dev
274                 _scratch_mount
275         fi
276 }
277
278 workout "-m single -d single" 1 no quick
279 workout "-m single -d single -M" 1 no quick
280 workout "-m dup -d single" 1 no quick
281 workout "-m dup -d single" 1 cancel quick
282 workout "-m dup -d dup -M" 1 no quick
283 workout "-m raid0 -d raid0" 2 no quick
284 workout "-m raid1 -d raid1" 2 no thorough
285 #workout "-m raid5 -d raid5" 2 no quick # not yet supported for btrfs replace
286 #workout "-m raid6 -d raid6" 3 no quick # not yet supported for btrfs replace
287 workout "-m raid10 -d raid10" 4 no quick
288
289 echo "*** done"
290 status=0
291 exit