| 1 | #!/usr/bin/env bash |
| 2 | # group: rw quick |
| 3 | # |
| 4 | # Test case for copy-on-read into qcow2 |
| 5 | # |
| 6 | # Copyright (C) 2017 Red Hat, Inc. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>. |
| 20 | # |
| 21 | |
| 22 | # creator |
| 23 | owner=eblake@redhat.com |
| 24 | |
| 25 | seq="$(basename $0)" |
| 26 | echo "QA output created by $seq" |
| 27 | |
| 28 | status=1 # failure is the default! |
| 29 | |
| 30 | # get standard environment, filters and checks |
| 31 | . ./common.rc |
| 32 | . ./common.filter |
| 33 | |
| 34 | TEST_WRAP="$TEST_DIR/t.wrap.qcow2" |
| 35 | BLKDBG_CONF="$TEST_DIR/blkdebug.conf" |
| 36 | |
| 37 | # Sanity check: our use of blkdebug fails if $TEST_DIR contains spaces |
| 38 | # or other problems |
| 39 | case "$TEST_DIR" in |
| 40 | *[^-_a-zA-Z0-9/]*) |
| 41 | _notrun "Suspicious TEST_DIR='$TEST_DIR', cowardly refusing to run" ;; |
| 42 | esac |
| 43 | |
| 44 | _cleanup() |
| 45 | { |
| 46 | _cleanup_test_img |
| 47 | _rm_test_img "$TEST_WRAP" |
| 48 | rm -f "$BLKDBG_CONF" |
| 49 | } |
| 50 | trap "_cleanup; exit \$status" 0 1 2 3 15 |
| 51 | |
| 52 | # Test is supported for any backing file; but we force qcow2 for our wrapper. |
| 53 | _supported_fmt generic |
| 54 | _supported_proto generic |
| 55 | # LUKS support may be possible, but it complicates things. |
| 56 | _unsupported_fmt luks |
| 57 | _unsupported_imgopts "subformat=streamOptimized" |
| 58 | |
| 59 | echo |
| 60 | echo '=== Copy-on-read ===' |
| 61 | echo |
| 62 | |
| 63 | # Prep the images |
| 64 | # VPC rounds image sizes to a specific geometry, force a specific size. |
| 65 | if [ "$IMGFMT" = "vpc" ]; then |
| 66 | IMGOPTS=$(_optstr_add "$IMGOPTS" "force_size") |
| 67 | fi |
| 68 | _make_test_img 4G |
| 69 | $QEMU_IO -c "write -P 55 3G 1k" "$TEST_IMG" | _filter_qemu_io |
| 70 | IMGPROTO=file IMGFMT=qcow2 TEST_IMG_FILE="$TEST_WRAP" \ |
| 71 | _make_test_img --no-opts -F "$IMGFMT" -b "$TEST_IMG" | _filter_img_create |
| 72 | $QEMU_IO -f qcow2 -c "write -z -u 1M 64k" "$TEST_WRAP" | _filter_qemu_io |
| 73 | |
| 74 | # Ensure that a read of two clusters, but where one is already allocated, |
| 75 | # does not re-write the allocated cluster |
| 76 | cat > "$BLKDBG_CONF" <<EOF |
| 77 | [inject-error] |
| 78 | event = "cor_write" |
| 79 | sector = "2048" |
| 80 | EOF |
| 81 | $QEMU_IO -c "open -C \ |
| 82 | -o driver=blkdebug,config=$BLKDBG_CONF,image.driver=qcow2 $TEST_WRAP" \ |
| 83 | -c "read -P 0 1M 128k" | _filter_qemu_io |
| 84 | |
| 85 | # Read the areas we want copied. A zero-length read should still be a |
| 86 | # no-op. The next read is under 2G, but aligned so that rounding to |
| 87 | # clusters copies more than 2G of zeroes. The final read will pick up |
| 88 | # the non-zero data in the same cluster. Since a 2G read may exhaust |
| 89 | # memory on some machines (particularly 32-bit), we skip the test if |
| 90 | # that fails due to memory pressure. |
| 91 | $QEMU_IO -f qcow2 -C -c "read 0 0" "$TEST_WRAP" | _filter_qemu_io |
| 92 | output=$($QEMU_IO -f qcow2 -C -c "read -P 0 1k $((2*1024*1024*1024 - 512))" \ |
| 93 | "$TEST_WRAP" 2>&1 | _filter_qemu_io) |
| 94 | case $output in |
| 95 | *allocate*) |
| 96 | _notrun "Insufficient memory to run test" ;; |
| 97 | *) printf '%s\n' "$output" ;; |
| 98 | esac |
| 99 | $QEMU_IO -f qcow2 -C -c "read -P 0 $((3*1024*1024*1024 + 1024)) 1k" \ |
| 100 | "$TEST_WRAP" | _filter_qemu_io |
| 101 | |
| 102 | # Copy-on-read is incompatible with read-only |
| 103 | $QEMU_IO -f qcow2 -C -r "$TEST_WRAP" 2>&1 | _filter_testdir |
| 104 | |
| 105 | # Break the backing chain, and show that images are identical, and that |
| 106 | # we properly copied over explicit zeros. |
| 107 | $QEMU_IMG rebase -u -b "" -f qcow2 "$TEST_WRAP" |
| 108 | $QEMU_IO -f qcow2 -c map "$TEST_WRAP" |
| 109 | _check_test_img |
| 110 | $QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP" |
| 111 | |
| 112 | echo |
| 113 | echo '=== Partial final cluster ===' |
| 114 | echo |
| 115 | |
| 116 | # Force compat=1.1, because writing zeroes on a v2 image without a |
| 117 | # backing file would just result in an unallocated cluster |
| 118 | # (Also, note that this is really a pure qcow2 test.) |
| 119 | IMGPROTO=file IMGFMT=qcow2 TEST_IMG_FILE="$TEST_WRAP" \ |
| 120 | _make_test_img --no-opts -o compat=1.1 1024 |
| 121 | $QEMU_IO -f qcow2 -C -c 'read 0 1024' "$TEST_WRAP" | _filter_qemu_io |
| 122 | $QEMU_IO -f qcow2 -c map "$TEST_WRAP" |
| 123 | _check_test_img |
| 124 | |
| 125 | echo |
| 126 | echo '=== Copy-on-read with subclusters ===' |
| 127 | echo |
| 128 | |
| 129 | # Create base and top images 64K (1 cluster) each. Make subclusters enabled |
| 130 | # for the top image |
| 131 | _make_test_img 64K |
| 132 | IMGPROTO=file IMGFMT=qcow2 TEST_IMG_FILE="$TEST_WRAP" \ |
| 133 | _make_test_img --no-opts -o extended_l2=true -F "$IMGFMT" -b "$TEST_IMG" \ |
| 134 | 64K | _filter_img_create |
| 135 | |
| 136 | $QEMU_IO -c "write -P 0xaa 0 64k" "$TEST_IMG" | _filter_qemu_io |
| 137 | |
| 138 | # Allocate individual subclusters in the top image, and not the whole cluster |
| 139 | $QEMU_IO -f qcow2 -c "write -P 0xbb 28K 2K" -c "write -P 0xcc 34K 2K" "$TEST_WRAP" \ |
| 140 | | _filter_qemu_io |
| 141 | |
| 142 | # Only 2 subclusters should be allocated in the top image at this point |
| 143 | $QEMU_IO -f qcow2 -c map "$TEST_WRAP" |
| 144 | |
| 145 | # Actual copy-on-read operation |
| 146 | $QEMU_IO -f qcow2 -C -c "read -P 0xaa 30K 4K" "$TEST_WRAP" | _filter_qemu_io |
| 147 | |
| 148 | # And here we should have 4 subclusters allocated right in the middle of the |
| 149 | # top image. Make sure the whole cluster remains unallocated |
| 150 | $QEMU_IO -f qcow2 -c map "$TEST_WRAP" |
| 151 | |
| 152 | _check_test_img |
| 153 | |
| 154 | # success, all done |
| 155 | echo '*** done' |
| 156 | status=0 |