111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env 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 Wolfstatus=1 # failure is the default! 2800e04792SKevin Wolf 2900e04792SKevin Wolf_cleanup() 3000e04792SKevin Wolf{ 31ecfa1854SJeff Cody _cleanup_qemu 32f91ecbd7SMax Reitz _rm_test_img "$TEST_IMG.src" 3300e04792SKevin Wolf _cleanup_test_img 3400e04792SKevin Wolf} 3500e04792SKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 3600e04792SKevin Wolf 3700e04792SKevin Wolf# get standard environment, filters and checks 3800e04792SKevin Wolf. ./common.rc 3900e04792SKevin Wolf. ./common.filter 4000e04792SKevin Wolf. ./common.qemu 4100e04792SKevin Wolf 4200e04792SKevin Wolf_supported_fmt raw 4300e04792SKevin Wolf_supported_proto file 4400e04792SKevin Wolf_supported_os Linux 45503034efSVladimir Sementsov-Ogievskiy_require_drivers qcow qcow2 qed vdi vmdk vpc 4600e04792SKevin Wolf 4700e04792SKevin Wolfqemu_comm_method=qmp 4800e04792SKevin Wolf 498cedcffdSEric Blakerun_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 568dff69b9SAarushi Mehta _launch_qemu -drive file="${source_img}",format=raw,cache=${CACHEMODE},aio=${AIOMODE},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 671dac83f1SKevin Wolf _send_qemu_cmd $QEMU_HANDLE '' '"status": "null"' 680a4c0c3fSPaolo Bonzini fi 6900e04792SKevin Wolf _send_qemu_cmd $QEMU_HANDLE '{"execute":"query-block-jobs"}' "return" 7053dd4015SCleber Rosa _send_qemu_cmd $QEMU_HANDLE '{"execute":"quit"}' "return" 7153dd4015SCleber Rosa wait=1 _cleanup_qemu 7200e04792SKevin Wolf} 7300e04792SKevin Wolf 7400e04792SKevin Wolffor fmt in qcow qcow2 qed vdi vmdk vpc; do 7500e04792SKevin Wolf 7600e04792SKevin Wolf echo 7700e04792SKevin Wolf echo "=== Writing a $fmt header into raw ===" 7800e04792SKevin Wolf echo 7900e04792SKevin Wolf 8000e04792SKevin Wolf TEST_IMG="$TEST_IMG.src" IMGFMT=$fmt _make_test_img 64M 81*ffa41a62SKevin Wolf _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size 8200e04792SKevin Wolf 8300e04792SKevin Wolf # This first test should fail: The image format was probed, we may not 8400e04792SKevin Wolf # write an image header at the start of the image 8524dfdfd0SFam Zheng run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" | 8624dfdfd0SFam Zheng _filter_block_job_len 87*ffa41a62SKevin Wolf $QEMU_IO -c 'read -P 0 0 512' "$TEST_IMG" | _filter_qemu_io 8800e04792SKevin Wolf 8900e04792SKevin Wolf 9000e04792SKevin Wolf # When raw was explicitly specified, the same must succeed 9100e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 9200e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 9300e04792SKevin Wolf 9400e04792SKevin Wolfdone 9500e04792SKevin Wolf 9600e04792SKevin Wolf 9700e04792SKevin Wolffor sample_img in empty.bochs iotest-dirtylog-10G-4M.vhdx parallels-v1 \ 9800e04792SKevin Wolf simple-pattern.cloop; do 9900e04792SKevin Wolf 10000e04792SKevin Wolf echo 10100e04792SKevin Wolf echo "=== Copying sample image $sample_img into raw ===" 10200e04792SKevin Wolf echo 10300e04792SKevin Wolf 10400e04792SKevin Wolf # Can't use _use_sample_img because that isn't designed to be used multiple 10500e04792SKevin Wolf # times and it overwrites $TEST_IMG (both breaks cleanup) 10600e04792SKevin Wolf bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 107*ffa41a62SKevin Wolf _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size 10800e04792SKevin Wolf 10924dfdfd0SFam Zheng run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" | 11024dfdfd0SFam Zheng _filter_block_job_offset | _filter_block_job_len 111*ffa41a62SKevin Wolf $QEMU_IO -c 'read -P 0 0 512' "$TEST_IMG" | _filter_qemu_io 11200e04792SKevin Wolf 11300e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 11400e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 11500e04792SKevin Wolfdone 11600e04792SKevin Wolf 11700e04792SKevin Wolfecho 11800e04792SKevin Wolfecho "=== Write legitimate MBR into raw ===" 11900e04792SKevin Wolfecho 12000e04792SKevin Wolf 12100e04792SKevin Wolffor sample_img in grub_mbr.raw; do 12200e04792SKevin Wolf bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src" 123*ffa41a62SKevin Wolf _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size 12400e04792SKevin Wolf 12500e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_READY" 12600e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 12700e04792SKevin Wolf 12800e04792SKevin Wolf run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY" 12900e04792SKevin Wolf $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src" 13000e04792SKevin Wolfdone 13100e04792SKevin Wolf 13200e04792SKevin Wolf 13300e04792SKevin Wolf# success, all done 13400e04792SKevin Wolfecho '*** done' 13500e04792SKevin Wolfrm -f $seq.full 13600e04792SKevin Wolfstatus=0 137