| 
									
										
										
										
											2019-03-07 15:58:38 +01:00
										 |  |  | #!/usr/bin/env bash | 
					
						
							| 
									
										
										
										
											2021-01-16 16:44:19 +03:00
										 |  |  | # group: rw auto quick | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2017-05-04 01:11:20 +02:00
										 |  |  | # Test case for preallocated zero clusters in qcow2 | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2013 Red Hat, Inc. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  | # it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | # the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  | # (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # creator | 
					
						
							| 
									
										
										
										
											2022-03-22 13:42:12 -04:00
										 |  |  | owner=hreitz@redhat.com | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | seq="$(basename $0)" | 
					
						
							|  |  |  | echo "QA output created by $seq" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | status=1	# failure is the default! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _cleanup() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	_cleanup_test_img | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | trap "_cleanup; exit \$status" 0 1 2 3 15 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # get standard environment, filters and checks | 
					
						
							|  |  |  | . ./common.rc | 
					
						
							|  |  |  | . ./common.filter | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-07 17:36:47 +01:00
										 |  |  | # This tests qcow2-specific low-level functionality | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | _supported_fmt qcow2 | 
					
						
							| 
									
										
										
										
											2024-03-15 12:11:01 +01:00
										 |  |  | _supported_proto file | 
					
						
							| 
									
										
										
										
											2019-11-07 17:36:52 +01:00
										 |  |  | # We need zero clusters and snapshots | 
					
						
							| 
									
										
										
										
											2019-11-07 17:37:07 +01:00
										 |  |  | # (TODO: Consider splitting the snapshot part into a separate test | 
					
						
							|  |  |  | #        file, so this one runs with refcount_bits=1 and data_file) | 
					
						
							|  |  |  | _unsupported_imgopts 'compat=0.10' 'refcount_bits=1[^0-9]' data_file | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 20:37:09 -05:00
										 |  |  | # Intentionally create an unaligned image | 
					
						
							|  |  |  | IMG_SIZE=$((64 * 1024 * 1024 + 512)) | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | echo | 
					
						
							| 
									
										
										
										
											2017-04-06 20:37:09 -05:00
										 |  |  | echo "=== Testing cluster discards ===" | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | echo | 
					
						
							|  |  |  | _make_test_img $IMG_SIZE | 
					
						
							| 
									
										
										
										
											2017-04-06 20:37:09 -05:00
										 |  |  | # Write some normal clusters, zero some of them (creating preallocated | 
					
						
							|  |  |  | # zero clusters) and discard everything. Everything should now read as 0. | 
					
						
							|  |  |  | $QEMU_IO -c "write 0 256k" -c "write -z 0 256k" -c "write 64M 512" \ | 
					
						
							|  |  |  | 	 -c "discard 0 $IMG_SIZE" -c "read -P 0 0 $IMG_SIZE" "$TEST_IMG" \ | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  |          | _filter_qemu_io | 
					
						
							| 
									
										
										
										
											2017-05-04 01:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | # Check the image (there shouldn't be any leaks) | 
					
						
							|  |  |  | _check_test_img | 
					
						
							| 
									
										
										
										
											2017-05-04 01:11:20 +02:00
										 |  |  | # Map the image (we want all clusters to be gone) | 
					
						
							|  |  |  | $QEMU_IMG map "$TEST_IMG" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _cleanup_test_img | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo | 
					
						
							|  |  |  | echo '=== Writing to preallocated zero clusters ===' | 
					
						
							|  |  |  | echo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _make_test_img $IMG_SIZE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Create data clusters (not aligned to an L2 table) | 
					
						
							|  |  |  | $QEMU_IO -c 'write -P 42 1M 256k' "$TEST_IMG" | _filter_qemu_io | 
					
						
							|  |  |  | orig_map=$($QEMU_IMG map --output=json "$TEST_IMG") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Convert the data clusters to preallocated zero clusters | 
					
						
							|  |  |  | $QEMU_IO -c 'write -z 1M 256k' "$TEST_IMG" | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Now write to them (with a COW needed for the head and tail) | 
					
						
							|  |  |  | $QEMU_IO -c "write -P 23 $(((1024 + 32) * 1024)) 192k" "$TEST_IMG" \ | 
					
						
							|  |  |  |     | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check metadata correctness | 
					
						
							|  |  |  | _check_test_img | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check data correctness | 
					
						
							|  |  |  | $QEMU_IO -c "read -P  0 $(( 1024             * 1024)) 32k" \ | 
					
						
							|  |  |  |          -c "read -P 23 $(((1024 + 32)       * 1024)) 192k" \ | 
					
						
							|  |  |  |          -c "read -P  0 $(((1024 + 32 + 192) * 1024)) 32k" \ | 
					
						
							|  |  |  |          "$TEST_IMG" \ | 
					
						
							|  |  |  |          | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check that we have actually reused the original area | 
					
						
							|  |  |  | new_map=$($QEMU_IMG map --output=json "$TEST_IMG") | 
					
						
							|  |  |  | if [ "$new_map" = "$orig_map" ]; then | 
					
						
							|  |  |  |     echo 'Successfully reused original clusters.' | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |     echo 'Failed to reuse original clusters.' | 
					
						
							|  |  |  |     echo 'Original map:' | 
					
						
							|  |  |  |     echo "$orig_map" | 
					
						
							|  |  |  |     echo 'New map:' | 
					
						
							|  |  |  |     echo "$new_map" | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _cleanup_test_img | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo | 
					
						
							|  |  |  | echo '=== Writing to a snapshotted preallocated zero cluster ===' | 
					
						
							|  |  |  | echo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _make_test_img 64k | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Create a preallocated zero cluster | 
					
						
							|  |  |  | $QEMU_IO -c 'write -P 42 0 64k' -c 'write -z 0 64k' "$TEST_IMG" \ | 
					
						
							|  |  |  |     | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Snapshot it | 
					
						
							|  |  |  | $QEMU_IMG snapshot -c foo "$TEST_IMG" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Write to the cluster | 
					
						
							|  |  |  | $QEMU_IO -c 'write -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check metadata correctness | 
					
						
							|  |  |  | _check_test_img | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check data correctness | 
					
						
							|  |  |  | $QEMU_IO -c 'read -P 23 0 64k' "$TEST_IMG" | _filter_qemu_io | 
					
						
							|  |  |  | $QEMU_IMG snapshot -a foo "$TEST_IMG" | 
					
						
							|  |  |  | $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _cleanup_test_img | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo | 
					
						
							|  |  |  | echo '=== Consecutive write to a preallocated zero cluster ===' | 
					
						
							|  |  |  | echo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _make_test_img 192k | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Create three normal clusters | 
					
						
							|  |  |  | $QEMU_IO -c 'write -P 42 0 192k' "$TEST_IMG" | _filter_qemu_io | 
					
						
							|  |  |  | orig_map=$($QEMU_IMG map --output=json "$TEST_IMG") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Make the middle cluster a preallocated zero cluster | 
					
						
							|  |  |  | $QEMU_IO -c 'write -z 64k 64k' "$TEST_IMG" | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Try to overwrite everything: This should reuse the whole range. To test that | 
					
						
							|  |  |  | # this only issues a single continuous write request, use blkdebug. | 
					
						
							|  |  |  | $QEMU_IO -c 'write -P 42 0 192k' \ | 
					
						
							|  |  |  |     "json:{ | 
					
						
							|  |  |  |         'driver': '$IMGFMT', | 
					
						
							|  |  |  |         'file': { | 
					
						
							|  |  |  |             'driver': 'blkdebug', | 
					
						
							|  |  |  |             'image.filename': '$TEST_IMG', | 
					
						
							|  |  |  |             'set-state': [{ | 
					
						
							|  |  |  |                 'event': 'write_aio', | 
					
						
							|  |  |  |                 'new_state': 2 | 
					
						
							|  |  |  |             }], | 
					
						
							|  |  |  |             'inject-error': [{ | 
					
						
							|  |  |  |                 'event': 'write_aio', | 
					
						
							|  |  |  |                 'state': 2 | 
					
						
							|  |  |  |             }] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }" \ | 
					
						
							|  |  |  |     | _filter_qemu_io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check metadata correctness | 
					
						
							|  |  |  | _check_test_img | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Check that we have actually reused the original area | 
					
						
							|  |  |  | new_map=$($QEMU_IMG map --output=json "$TEST_IMG") | 
					
						
							|  |  |  | if [ "$new_map" = "$orig_map" ]; then | 
					
						
							|  |  |  |     echo 'Successfully reused original clusters.' | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |     echo 'Failed to reuse original clusters.' | 
					
						
							|  |  |  |     echo 'Original map:' | 
					
						
							|  |  |  |     echo "$orig_map" | 
					
						
							|  |  |  |     echo 'New map:' | 
					
						
							|  |  |  |     echo "$new_map" | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _cleanup_test_img | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-09 12:07:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # success, all done | 
					
						
							|  |  |  | echo "*** done" | 
					
						
							|  |  |  | rm -f $seq.full | 
					
						
							|  |  |  | status=0 |