1*00e04792SKevin Wolf#!/bin/bash 2*00e04792SKevin Wolf# 3*00e04792SKevin Wolf# Test writing image headers of other formats into raw images 4*00e04792SKevin Wolf# 5*00e04792SKevin Wolf# Copyright (C) 2014 Red Hat, Inc. 6*00e04792SKevin Wolf# 7*00e04792SKevin Wolf# This program is free software; you can redistribute it and/or modify 8*00e04792SKevin Wolf# it under the terms of the GNU General Public License as published by 9*00e04792SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 10*00e04792SKevin Wolf# (at your option) any later version. 11*00e04792SKevin Wolf# 12*00e04792SKevin Wolf# This program is distributed in the hope that it will be useful, 13*00e04792SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*00e04792SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*00e04792SKevin Wolf# GNU General Public License for more details. 16*00e04792SKevin Wolf# 17*00e04792SKevin Wolf# You should have received a copy of the GNU General Public License 18*00e04792SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 19*00e04792SKevin Wolf# 20*00e04792SKevin Wolf 21*00e04792SKevin Wolf# creator 22*00e04792SKevin Wolfowner=kwolf@redhat.com 23*00e04792SKevin Wolf 24*00e04792SKevin Wolfseq="$(basename $0)" 25*00e04792SKevin Wolfecho "QA output created by $seq" 26*00e04792SKevin Wolf 27*00e04792SKevin Wolfhere="$PWD" 28*00e04792SKevin Wolftmp=/tmp/$$ 29*00e04792SKevin Wolfstatus=1 # failure is the default! 30*00e04792SKevin Wolf 31*00e04792SKevin Wolf_cleanup() 32*00e04792SKevin Wolf{ 33*00e04792SKevin Wolf rm -f $TEST_IMG.src 34*00e04792SKevin Wolf _cleanup_test_img 35*00e04792SKevin Wolf} 36*00e04792SKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 37*00e04792SKevin Wolf 38*00e04792SKevin Wolf# get standard environment, filters and checks 39*00e04792SKevin Wolf. ./common.rc 40*00e04792SKevin Wolf. ./common.filter 41*00e04792SKevin Wolf. ./common.qemu 42*00e04792SKevin Wolf 43*00e04792SKevin Wolf_supported_fmt raw 44*00e04792SKevin Wolf_supported_proto file 45*00e04792SKevin Wolf_supported_os Linux 46*00e04792SKevin Wolf 47*00e04792SKevin Wolfqemu_comm_method=qmp 48*00e04792SKevin Wolf 49*00e04792SKevin Wolffunction run_qemu() 50*00e04792SKevin Wolf{ 51*00e04792SKevin Wolf local raw_img="$1" 52*00e04792SKevin Wolf local source_img="$2" 53*00e04792SKevin Wolf local qmp_format="$3" 54*00e04792SKevin Wolf local qmp_event="$4" 55*00e04792SKevin Wolf 56*00e04792SKevin Wolf _launch_qemu -drive file="${source_img}",format=raw,cache=${CACHEMODE},id=src 57*00e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE "{ 'execute': 'qmp_capabilities' }" "return" 58*00e04792SKevin Wolf 59*00e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE \ 60*00e04792SKevin Wolf "{'execute':'drive-mirror', 'arguments':{ 61*00e04792SKevin Wolf 'device': 'src', 'target': '$raw_img', $qmp_format 62*00e04792SKevin Wolf 'mode': 'existing', 'sync': 'full'}}" \ 63*00e04792SKevin Wolf "return" 64*00e04792SKevin Wolf 65*00e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE '' "$qmp_event" 66*00e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE '{"execute":"query-block-jobs"}' "return" 67*00e04792SKevin Wolf _cleanup_qemu 68*00e04792SKevin Wolf} 69*00e04792SKevin Wolf 70*00e04792SKevin Wolffor fmt in qcow qcow2 qed vdi vmdk vpc; do 71*00e04792SKevin Wolf 72*00e04792SKevin Wolf echo 73*00e04792SKevin Wolf echo "=== Writing a $fmt header into raw ===" 74*00e04792SKevin Wolf echo 75*00e04792SKevin Wolf 76*00e04792SKevin Wolf _make_test_img 64M 77*00e04792SKevin Wolf TEST_IMG="$TEST_IMG.src" IMGFMT=$fmt _make_test_img 64M 78*00e04792SKevin Wolf 79*00e04792SKevin Wolf # This first test should fail: The image format was probed, we may not 80*00e04792SKevin Wolf # write an image header at the start of the image 81*00e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" 82*00e04792SKevin Wolf $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io 83*00e04792SKevin Wolf 84*00e04792SKevin Wolf 85*00e04792SKevin Wolf # When raw was explicitly specified, the same must succeed 86*00e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 87*00e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 88*00e04792SKevin Wolf 89*00e04792SKevin Wolfdone 90*00e04792SKevin Wolf 91*00e04792SKevin Wolf 92*00e04792SKevin Wolffor sample_img in empty.bochs iotest-dirtylog-10G-4M.vhdx parallels-v1 \ 93*00e04792SKevin Wolf simple-pattern.cloop; do 94*00e04792SKevin Wolf 95*00e04792SKevin Wolf echo 96*00e04792SKevin Wolf echo "=== Copying sample image $sample_img into raw ===" 97*00e04792SKevin Wolf echo 98*00e04792SKevin Wolf 99*00e04792SKevin Wolf # Can't use _use_sample_img because that isn't designed to be used multiple 100*00e04792SKevin Wolf # times and it overwrites $TEST_IMG (both breaks cleanup) 101*00e04792SKevin Wolf _make_test_img 64M 102*00e04792SKevin Wolf bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 103*00e04792SKevin Wolf 104*00e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" 105*00e04792SKevin Wolf $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io 106*00e04792SKevin Wolf 107*00e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 108*00e04792SKevin Wolf # qemu-img compare can't handle unaligned file sizes 109*00e04792SKevin Wolf $QEMU_IMG resize -f raw "$TEST_IMG.src" +0 110*00e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 111*00e04792SKevin Wolfdone 112*00e04792SKevin Wolf 113*00e04792SKevin Wolfecho 114*00e04792SKevin Wolfecho "=== Write legitimate MBR into raw ===" 115*00e04792SKevin Wolfecho 116*00e04792SKevin Wolf 117*00e04792SKevin Wolffor sample_img in grub_mbr.raw; do 118*00e04792SKevin Wolf _make_test_img 64M 119*00e04792SKevin Wolf bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 120*00e04792SKevin Wolf 121*00e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_READY" 122*00e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 123*00e04792SKevin Wolf 124*00e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 125*00e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 126*00e04792SKevin Wolfdone 127*00e04792SKevin Wolf 128*00e04792SKevin Wolf 129*00e04792SKevin Wolf# success, all done 130*00e04792SKevin Wolfecho '*** done' 131*00e04792SKevin Wolfrm -f $seq.full 132*00e04792SKevin Wolfstatus=0 133