1#!/bin/bash 2# 3# Test writing image headers of other formats into raw images 4# 5# Copyright (C) 2014 Red Hat, Inc. 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=kwolf@redhat.com 23 24seq="$(basename $0)" 25echo "QA output created by $seq" 26 27here="$PWD" 28tmp=/tmp/$$ 29status=1 # failure is the default! 30 31_cleanup() 32{ 33 rm -f $TEST_IMG.src 34 _cleanup_test_img 35} 36trap "_cleanup; exit \$status" 0 1 2 3 15 37 38# get standard environment, filters and checks 39. ./common.rc 40. ./common.filter 41. ./common.qemu 42 43_supported_fmt raw 44_supported_proto file 45_supported_os Linux 46 47qemu_comm_method=qmp 48 49function run_qemu() 50{ 51 local raw_img="$1" 52 local source_img="$2" 53 local qmp_format="$3" 54 local qmp_event="$4" 55 56 _launch_qemu -drive file="${source_img}",format=raw,cache=${CACHEMODE},id=src 57 _send_qemu_cmd $QEMU_HANDLE "{ 'execute': 'qmp_capabilities' }" "return" 58 59 _send_qemu_cmd $QEMU_HANDLE \ 60 "{'execute':'drive-mirror', 'arguments':{ 61 'device': 'src', 'target': '$raw_img', $qmp_format 62 'mode': 'existing', 'sync': 'full'}}" \ 63 "return" 64 65 _send_qemu_cmd $QEMU_HANDLE '' "$qmp_event" 66 _send_qemu_cmd $QEMU_HANDLE '{"execute":"query-block-jobs"}' "return" 67 _cleanup_qemu 68} 69 70for fmt in qcow qcow2 qed vdi vmdk vpc; do 71 72 echo 73 echo "=== Writing a $fmt header into raw ===" 74 echo 75 76 _make_test_img 64M 77 TEST_IMG="$TEST_IMG.src" IMGFMT=$fmt _make_test_img 64M 78 79 # This first test should fail: The image format was probed, we may not 80 # write an image header at the start of the image 81 run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" 82 $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io 83 84 85 # When raw was explicitly specified, the same must succeed 86 run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 87 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 88 89done 90 91 92for sample_img in empty.bochs iotest-dirtylog-10G-4M.vhdx parallels-v1 \ 93 simple-pattern.cloop; do 94 95 echo 96 echo "=== Copying sample image $sample_img into raw ===" 97 echo 98 99 # Can't use _use_sample_img because that isn't designed to be used multiple 100 # times and it overwrites $TEST_IMG (both breaks cleanup) 101 _make_test_img 64M 102 bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 103 104 run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" 105 $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io 106 107 run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 108 # qemu-img compare can't handle unaligned file sizes 109 $QEMU_IMG resize -f raw "$TEST_IMG.src" +0 110 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 111done 112 113echo 114echo "=== Write legitimate MBR into raw ===" 115echo 116 117for sample_img in grub_mbr.raw; do 118 _make_test_img 64M 119 bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 120 121 run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_READY" 122 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 123 124 run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 125 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 126done 127 128 129# success, all done 130echo '*** done' 131rm -f $seq.full 132status=0 133