100e04792SKevin Wolf#!/bin/bash 200e04792SKevin Wolf# 300e04792SKevin Wolf# Test writing image headers of other formats into raw images 400e04792SKevin Wolf# 500e04792SKevin Wolf# Copyright (C) 2014 Red Hat, Inc. 600e04792SKevin Wolf# 700e04792SKevin Wolf# This program is free software; you can redistribute it and/or modify 800e04792SKevin Wolf# it under the terms of the GNU General Public License as published by 900e04792SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 1000e04792SKevin Wolf# (at your option) any later version. 1100e04792SKevin Wolf# 1200e04792SKevin Wolf# This program is distributed in the hope that it will be useful, 1300e04792SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 1400e04792SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1500e04792SKevin Wolf# GNU General Public License for more details. 1600e04792SKevin Wolf# 1700e04792SKevin Wolf# You should have received a copy of the GNU General Public License 1800e04792SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 1900e04792SKevin Wolf# 2000e04792SKevin Wolf 2100e04792SKevin Wolf# creator 2200e04792SKevin Wolfowner=kwolf@redhat.com 2300e04792SKevin Wolf 2400e04792SKevin Wolfseq="$(basename $0)" 2500e04792SKevin Wolfecho "QA output created by $seq" 2600e04792SKevin Wolf 2700e04792SKevin Wolfhere="$PWD" 2800e04792SKevin Wolfstatus=1 # failure is the default! 2900e04792SKevin Wolf 3000e04792SKevin Wolf_cleanup() 3100e04792SKevin Wolf{ 32ecfa1854SJeff Cody _cleanup_qemu 3300e04792SKevin Wolf rm -f $TEST_IMG.src 3400e04792SKevin Wolf _cleanup_test_img 3500e04792SKevin Wolf} 3600e04792SKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 3700e04792SKevin Wolf 3800e04792SKevin Wolf# get standard environment, filters and checks 3900e04792SKevin Wolf. ./common.rc 4000e04792SKevin Wolf. ./common.filter 4100e04792SKevin Wolf. ./common.qemu 4200e04792SKevin Wolf 4300e04792SKevin Wolf_supported_fmt raw 4400e04792SKevin Wolf_supported_proto file 4500e04792SKevin Wolf_supported_os Linux 4600e04792SKevin Wolf 4700e04792SKevin Wolfqemu_comm_method=qmp 4800e04792SKevin Wolf 4900e04792SKevin Wolffunction run_qemu() 5000e04792SKevin Wolf{ 5100e04792SKevin Wolf local raw_img="$1" 5200e04792SKevin Wolf local source_img="$2" 5300e04792SKevin Wolf local qmp_format="$3" 5400e04792SKevin Wolf local qmp_event="$4" 5500e04792SKevin Wolf 5600e04792SKevin Wolf _launch_qemu -drive file="${source_img}",format=raw,cache=${CACHEMODE},id=src 5700e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE "{ 'execute': 'qmp_capabilities' }" "return" 5800e04792SKevin Wolf 5900e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE \ 6000e04792SKevin Wolf "{'execute':'drive-mirror', 'arguments':{ 6100e04792SKevin Wolf 'device': 'src', 'target': '$raw_img', $qmp_format 6200e04792SKevin Wolf 'mode': 'existing', 'sync': 'full'}}" \ 6300e04792SKevin Wolf "return" 6400e04792SKevin Wolf 6500e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE '' "$qmp_event" 660a4c0c3fSPaolo Bonzini if test "$qmp_event" = BLOCK_JOB_ERROR; then 670a4c0c3fSPaolo Bonzini _send_qemu_cmd $QEMU_HANDLE '' "BLOCK_JOB_COMPLETED" 680a4c0c3fSPaolo Bonzini fi 6900e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE '{"execute":"query-block-jobs"}' "return" 7000e04792SKevin Wolf _cleanup_qemu 7100e04792SKevin Wolf} 7200e04792SKevin Wolf 7300e04792SKevin Wolffor fmt in qcow qcow2 qed vdi vmdk vpc; do 7400e04792SKevin Wolf 7500e04792SKevin Wolf echo 7600e04792SKevin Wolf echo "=== Writing a $fmt header into raw ===" 7700e04792SKevin Wolf echo 7800e04792SKevin Wolf 7900e04792SKevin Wolf _make_test_img 64M 8000e04792SKevin Wolf TEST_IMG="$TEST_IMG.src" IMGFMT=$fmt _make_test_img 64M 8100e04792SKevin Wolf 8200e04792SKevin Wolf # This first test should fail: The image format was probed, we may not 8300e04792SKevin Wolf # write an image header at the start of the image 84*24dfdfd0SFam Zheng run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" | 85*24dfdfd0SFam Zheng _filter_block_job_len 8600e04792SKevin Wolf $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io 8700e04792SKevin Wolf 8800e04792SKevin Wolf 8900e04792SKevin Wolf # When raw was explicitly specified, the same must succeed 9000e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 9100e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 9200e04792SKevin Wolf 9300e04792SKevin Wolfdone 9400e04792SKevin Wolf 9500e04792SKevin Wolf 9600e04792SKevin Wolffor sample_img in empty.bochs iotest-dirtylog-10G-4M.vhdx parallels-v1 \ 9700e04792SKevin Wolf simple-pattern.cloop; do 9800e04792SKevin Wolf 9900e04792SKevin Wolf echo 10000e04792SKevin Wolf echo "=== Copying sample image $sample_img into raw ===" 10100e04792SKevin Wolf echo 10200e04792SKevin Wolf 10300e04792SKevin Wolf # Can't use _use_sample_img because that isn't designed to be used multiple 10400e04792SKevin Wolf # times and it overwrites $TEST_IMG (both breaks cleanup) 10500e04792SKevin Wolf _make_test_img 64M 10600e04792SKevin Wolf bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 10700e04792SKevin Wolf 108*24dfdfd0SFam Zheng run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" | 109*24dfdfd0SFam Zheng _filter_block_job_offset | _filter_block_job_len 11000e04792SKevin Wolf $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io 11100e04792SKevin Wolf 11200e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 11300e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 11400e04792SKevin Wolfdone 11500e04792SKevin Wolf 11600e04792SKevin Wolfecho 11700e04792SKevin Wolfecho "=== Write legitimate MBR into raw ===" 11800e04792SKevin Wolfecho 11900e04792SKevin Wolf 12000e04792SKevin Wolffor sample_img in grub_mbr.raw; do 12100e04792SKevin Wolf _make_test_img 64M 12200e04792SKevin Wolf bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 12300e04792SKevin Wolf 12400e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_READY" 12500e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 12600e04792SKevin Wolf 12700e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 12800e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 12900e04792SKevin Wolfdone 13000e04792SKevin Wolf 13100e04792SKevin Wolf 13200e04792SKevin Wolf# success, all done 13300e04792SKevin Wolfecho '*** done' 13400e04792SKevin Wolfrm -f $seq.full 13500e04792SKevin Wolfstatus=0 136