1#!/usr/bin/env 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 27status=1 # failure is the default! 28 29_cleanup() 30{ 31 _cleanup_qemu 32 _rm_test_img "$TEST_IMG.src" 33 _cleanup_test_img 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. ./common.qemu 41 42_supported_fmt raw 43_supported_proto file 44_supported_os Linux 45_require_drivers qcow qcow2 qed vdi vmdk vpc 46 47qemu_comm_method=qmp 48 49run_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},aio=${AIOMODE},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 if test "$qmp_event" = BLOCK_JOB_ERROR; then 67 _send_qemu_cmd $QEMU_HANDLE '' '"status": "null"' 68 fi 69 _send_qemu_cmd $QEMU_HANDLE '{"execute":"query-block-jobs"}' "return" 70 _send_qemu_cmd $QEMU_HANDLE '{"execute":"quit"}' "return" 71 wait=1 _cleanup_qemu 72} 73 74for fmt in qcow qcow2 qed vdi vmdk vpc; do 75 76 echo 77 echo "=== Writing a $fmt header into raw ===" 78 echo 79 80 TEST_IMG="$TEST_IMG.src" IMGFMT=$fmt _make_test_img 64M 81 _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size 82 83 # This first test should fail: The image format was probed, we may not 84 # write an image header at the start of the image 85 run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" | 86 _filter_block_job_len 87 $QEMU_IO -c 'read -P 0 0 512' "$TEST_IMG" | _filter_qemu_io 88 89 90 # When raw was explicitly specified, the same must succeed 91 run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 92 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 93 94done 95 96 97for sample_img in empty.bochs iotest-dirtylog-10G-4M.vhdx parallels-v1 \ 98 simple-pattern.cloop; do 99 100 echo 101 echo "=== Copying sample image $sample_img into raw ===" 102 echo 103 104 # Can't use _use_sample_img because that isn't designed to be used multiple 105 # times and it overwrites $TEST_IMG (both breaks cleanup) 106 bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 107 _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size 108 109 run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" | 110 _filter_block_job_offset | _filter_block_job_len 111 $QEMU_IO -c 'read -P 0 0 512' "$TEST_IMG" | _filter_qemu_io 112 113 run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 114 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 115done 116 117echo 118echo "=== Write legitimate MBR into raw ===" 119echo 120 121for sample_img in grub_mbr.raw; do 122 bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 123 _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size 124 125 run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_READY" 126 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 127 128 run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 129 $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 130done 131 132 133# success, all done 134echo '*** done' 135rm -f $seq.full 136status=0 137