1#!/usr/bin/env bash 2# 3# Test Quorum block driver 4# 5# Copyright (C) 2013 Nodalink, SARL. 6# 7# This program is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 2 of the License, or 10# (at your option) any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program. If not, see <http://www.gnu.org/licenses/>. 19# 20 21# creator 22owner=benoit@irqsave.net 23 24seq=`basename $0` 25echo "QA output created by $seq" 26 27status=1 # failure is the default! 28 29_cleanup() 30{ 31 _rm_test_img "$TEST_DIR/1.raw" 32 _rm_test_img "$TEST_DIR/2.raw" 33 _rm_test_img "$TEST_DIR/3.raw" 34} 35trap "_cleanup; exit \$status" 0 1 2 3 15 36 37# get standard environment, filters and checks 38. ./common.rc 39. ./common.filter 40 41_supported_fmt raw 42_supported_proto file 43_supported_os Linux 44_require_drivers quorum 45_require_devices virtio-scsi 46 47do_run_qemu() 48{ 49 echo Testing: "$@" 50 $QEMU -nographic -qmp stdio -serial none "$@" 51 echo 52} 53 54run_qemu() 55{ 56 do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_imgfmt | _filter_qemu \ 57 | _filter_qmp | _filter_qemu_io \ 58 | _filter_generated_node_ids 59} 60 61quorum="driver=raw,file.driver=quorum,file.vote-threshold=2" 62quorum="$quorum,file.children.0.file.filename=$TEST_DIR/1.raw" 63quorum="$quorum,file.children.1.file.filename=$TEST_DIR/2.raw" 64quorum="$quorum,file.children.2.file.filename=$TEST_DIR/3.raw" 65quorum="$quorum,file.children.0.driver=raw" 66quorum="$quorum,file.children.1.driver=raw" 67quorum="$quorum,file.children.2.driver=raw" 68 69echo 70echo "== creating quorum files ==" 71 72size=10M 73 74TEST_IMG="$TEST_DIR/1.raw" _make_test_img $size 75TEST_IMG="$TEST_DIR/2.raw" _make_test_img $size 76TEST_IMG="$TEST_DIR/3.raw" _make_test_img $size 77 78echo 79echo "== writing images ==" 80 81$QEMU_IO -c "open -o $quorum" -c "write -P 0x32 0 $size" | _filter_qemu_io 82 83echo 84echo "== checking quorum write ==" 85 86$QEMU_IO -c "read -P 0x32 0 $size" "$TEST_DIR/1.raw" | _filter_qemu_io 87$QEMU_IO -c "read -P 0x32 0 $size" "$TEST_DIR/2.raw" | _filter_qemu_io 88$QEMU_IO -c "read -P 0x32 0 $size" "$TEST_DIR/3.raw" | _filter_qemu_io 89 90echo 91echo "== corrupting image ==" 92 93$QEMU_IO -c "write -P 0x42 0 $size" "$TEST_DIR/2.raw" | _filter_qemu_io 94 95echo 96echo "== checking quorum correction ==" 97 98$QEMU_IO -c "open -o $quorum" -c "read -P 0x32 0 $size" | _filter_qemu_io 99 100echo 101echo "== checking mixed reference/option specification ==" 102 103run_qemu <<EOF 104{ "execute": "qmp_capabilities" } 105{ "execute": "blockdev-add", 106 "arguments": { 107 "node-name": "drive2", 108 "driver": "$IMGFMT", 109 "file": { 110 "driver": "file", 111 "filename": "$TEST_DIR/2.raw" 112 } 113 } 114} 115{ "execute": "blockdev-add", 116 "arguments": { 117 "driver": "quorum", 118 "node-name": "drive0-quorum", 119 "vote-threshold": 2, 120 "children": [ 121 { 122 "driver": "$IMGFMT", 123 "file": { 124 "driver": "file", 125 "filename": "$TEST_DIR/1.raw" 126 } 127 }, 128 "drive2", 129 { 130 "driver": "$IMGFMT", 131 "file": { 132 "driver": "file", 133 "filename": "$TEST_DIR/3.raw" 134 } 135 } 136 ] 137 } 138} 139{ "execute": "human-monitor-command", 140 "arguments": { 141 "command-line": 'qemu-io drive0-quorum "read -P 0x32 0 $size"' 142 } 143} 144{ "execute": "quit" } 145EOF 146 147echo 148echo "== using quorum rewrite corrupted mode ==" 149 150quorum="$quorum,file.rewrite-corrupted=on" 151 152$QEMU_IO -c "open -o $quorum" -c "read -P 0x32 0 $size" | _filter_qemu_io 153 154echo 155echo "== checking that quorum has corrected the corrupted file ==" 156 157$QEMU_IO -c "read -P 0x32 0 $size" "$TEST_DIR/2.raw" | _filter_qemu_io 158 159echo 160echo "== using quorum rewrite corrupted mode without WRITE permission ==" 161 162# The same as above, but this time, do it on a quorum node whose only 163# parent will not take the WRITE permission 164 165echo '-- corrupting --' 166# Only corrupt a portion: The guest device (scsi-hd on virtio-scsi) 167# will read some data (looking for a partition table to guess the 168# disk's geometry), which would trigger a quorum mismatch if the 169# beginning of the image was corrupted. The subsequent 170# QUORUM_REPORT_BAD event would be suppressed (because at that point, 171# there cannot have been a qmp_capabilities on the monitor). Because 172# that event is rate-limited, the next QUORUM_REPORT_BAD that happens 173# thanks to our qemu-io read (which should trigger a mismatch) would 174# then be delayed past the VM quit and not appear in the output. 175# So we keep the first 1M intact to see a QUORUM_REPORT_BAD resulting 176# from the qemu-io invocation. 177$QEMU_IO -c "write -P 0x42 1M 1M" "$TEST_DIR/2.raw" | _filter_qemu_io 178 179# Fix the corruption (on a read-only quorum node, i.e. without taking 180# the WRITE permission on it -- its child nodes need to be R/W OTOH, 181# so that rewrite-corrupted works) 182echo 183echo '-- running quorum --' 184run_qemu \ 185 -blockdev file,node-name=file1,filename="$TEST_DIR/1.raw" \ 186 -blockdev file,node-name=file2,filename="$TEST_DIR/2.raw" \ 187 -blockdev file,node-name=file3,filename="$TEST_DIR/3.raw" \ 188 -blockdev '{ 189 "driver": "quorum", 190 "node-name": "quorum", 191 "read-only": true, 192 "vote-threshold": 2, 193 "rewrite-corrupted": true, 194 "children": [ "file1", "file2", "file3" ] 195 }' \ 196 -device virtio-scsi,id=scsi \ 197 -device scsi-hd,id=quorum-drive,bus=scsi.0,drive=quorum \ 198 <<EOF 199{ "execute": "qmp_capabilities" } 200{ 201 "execute": "human-monitor-command", 202 "arguments": { 203 "command-line": 'qemu-io -d quorum-drive "read -P 0x32 0 $size"' 204 } 205} 206{ "execute": "quit" } 207EOF 208 209echo '-- checking that the image has been corrected --' 210$QEMU_IO -c "read -P 0x32 0 $size" "$TEST_DIR/2.raw" | _filter_qemu_io 211 212echo 213echo "== breaking quorum ==" 214 215$QEMU_IO -c "write -P 0x41 0 $size" "$TEST_DIR/1.raw" | _filter_qemu_io 216$QEMU_IO -c "write -P 0x42 0 $size" "$TEST_DIR/2.raw" | _filter_qemu_io 217 218echo 219echo "== checking that quorum is broken ==" 220 221$QEMU_IO -c "open -o $quorum" -c "read -P 0x32 0 $size" | _filter_qemu_io 222 223echo 224echo "== checking the blkverify mode with broken content ==" 225 226quorum="driver=raw,file.driver=quorum,file.vote-threshold=2,file.blkverify=on" 227quorum="$quorum,file.children.0.file.filename=$TEST_DIR/1.raw" 228quorum="$quorum,file.children.1.file.filename=$TEST_DIR/2.raw" 229quorum="$quorum,file.children.0.driver=raw" 230quorum="$quorum,file.children.1.driver=raw" 231 232$QEMU_IO -c "open -o $quorum" -c "read -P 0x32 0 $size" | _filter_qemu_io 233 234echo 235echo "== writing the same data to both files ==" 236 237$QEMU_IO -c "write -P 0x32 0 $size" "$TEST_DIR/1.raw" | _filter_qemu_io 238$QEMU_IO -c "write -P 0x32 0 $size" "$TEST_DIR/2.raw" | _filter_qemu_io 239 240echo 241echo "== checking the blkverify mode with valid content ==" 242 243$QEMU_IO -c "open -o $quorum" -c "read -P 0x32 0 $size" | _filter_qemu_io 244 245echo 246echo "== checking the blkverify mode with invalid settings ==" 247 248quorum="$quorum,file.children.2.file.filename=$TEST_DIR/3.raw" 249quorum="$quorum,file.children.2.driver=raw" 250 251$QEMU_IO -c "open -o $quorum" | _filter_qemu_io 252 253echo 254echo "== dynamically adding a child to a quorum ==" 255 256for verify in false true; do 257 run_qemu <<EOF 258 { "execute": "qmp_capabilities" } 259 { "execute": "blockdev-add", 260 "arguments": { 261 "driver": "quorum", 262 "node-name": "drive0-quorum", 263 "vote-threshold": 2, 264 "blkverify": ${verify}, 265 "children": [ 266 { 267 "driver": "$IMGFMT", 268 "file": { 269 "driver": "file", 270 "filename": "$TEST_DIR/1.raw" 271 } 272 }, 273 { 274 "driver": "$IMGFMT", 275 "file": { 276 "driver": "file", 277 "filename": "$TEST_DIR/2.raw" 278 } 279 } 280 ] 281 } 282 } 283 { "execute": "blockdev-add", 284 "arguments": { 285 "node-name": "drive3", 286 "driver": "$IMGFMT", 287 "file": { 288 "driver": "file", 289 "filename": "$TEST_DIR/2.raw" 290 } 291 } 292 } 293 { "execute": "x-blockdev-change", 294 "arguments": { "parent": "drive0-quorum", 295 "node": "drive3" } } 296 { "execute": "quit" } 297EOF 298done 299 300echo 301echo "== dynamically removing a child from a quorum ==" 302 303for verify in false true; do 304 for vote_threshold in 1 2; do 305 run_qemu <<EOF 306 { "execute": "qmp_capabilities" } 307 { "execute": "blockdev-add", 308 "arguments": { 309 "driver": "quorum", 310 "node-name": "drive0-quorum", 311 "vote-threshold": ${vote_threshold}, 312 "blkverify": ${verify}, 313 "children": [ 314 { 315 "driver": "$IMGFMT", 316 "file": { 317 "driver": "file", 318 "filename": "$TEST_DIR/1.raw" 319 } 320 }, 321 { 322 "driver": "$IMGFMT", 323 "file": { 324 "driver": "file", 325 "filename": "$TEST_DIR/2.raw" 326 } 327 } 328 ] 329 } 330 } 331 { "execute": "x-blockdev-change", 332 "arguments": { "parent": "drive0-quorum", 333 "child": "children.1" } } 334 { "execute": "quit" } 335EOF 336 done 337done 338 339# success, all done 340echo "*** done" 341rm -f $seq.full 342status=0 343