xref: /qemu/tests/qemu-iotests/109 (revision 0a4c0c3f90c3fb0239cb17d63a68c14742965a1c)
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{
3200e04792SKevin Wolf    rm -f $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
4500e04792SKevin Wolf
4600e04792SKevin Wolfqemu_comm_method=qmp
4700e04792SKevin Wolf
4800e04792SKevin Wolffunction run_qemu()
4900e04792SKevin Wolf{
5000e04792SKevin Wolf    local raw_img="$1"
5100e04792SKevin Wolf    local source_img="$2"
5200e04792SKevin Wolf    local qmp_format="$3"
5300e04792SKevin Wolf    local qmp_event="$4"
5400e04792SKevin Wolf
5500e04792SKevin Wolf    _launch_qemu -drive file="${source_img}",format=raw,cache=${CACHEMODE},id=src
5600e04792SKevin Wolf    _send_qemu_cmd $QEMU_HANDLE "{ 'execute': 'qmp_capabilities' }" "return"
5700e04792SKevin Wolf
5800e04792SKevin Wolf    _send_qemu_cmd $QEMU_HANDLE \
5900e04792SKevin Wolf        "{'execute':'drive-mirror', 'arguments':{
6000e04792SKevin Wolf            'device': 'src', 'target': '$raw_img', $qmp_format
6100e04792SKevin Wolf            'mode': 'existing', 'sync': 'full'}}" \
6200e04792SKevin Wolf        "return"
6300e04792SKevin Wolf
6400e04792SKevin Wolf    _send_qemu_cmd $QEMU_HANDLE '' "$qmp_event"
65*0a4c0c3fSPaolo Bonzini    if test "$qmp_event" = BLOCK_JOB_ERROR; then
66*0a4c0c3fSPaolo Bonzini        _send_qemu_cmd $QEMU_HANDLE '' "BLOCK_JOB_COMPLETED"
67*0a4c0c3fSPaolo Bonzini    fi
6800e04792SKevin Wolf    _send_qemu_cmd $QEMU_HANDLE '{"execute":"query-block-jobs"}' "return"
6900e04792SKevin Wolf    _cleanup_qemu
7000e04792SKevin Wolf}
7100e04792SKevin Wolf
7200e04792SKevin Wolffor fmt in qcow qcow2 qed vdi vmdk vpc; do
7300e04792SKevin Wolf
7400e04792SKevin Wolf    echo
7500e04792SKevin Wolf    echo "=== Writing a $fmt header into raw ==="
7600e04792SKevin Wolf    echo
7700e04792SKevin Wolf
7800e04792SKevin Wolf    _make_test_img 64M
7900e04792SKevin Wolf    TEST_IMG="$TEST_IMG.src" IMGFMT=$fmt _make_test_img 64M
8000e04792SKevin Wolf
8100e04792SKevin Wolf    # This first test should fail: The image format was probed, we may not
8200e04792SKevin Wolf    # write an image header at the start of the image
8300e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR"
8400e04792SKevin Wolf    $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io
8500e04792SKevin Wolf
8600e04792SKevin Wolf
8700e04792SKevin Wolf    # When raw was explicitly specified, the same must succeed
8800e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY"
8900e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
9000e04792SKevin Wolf
9100e04792SKevin Wolfdone
9200e04792SKevin Wolf
9300e04792SKevin Wolf
9400e04792SKevin Wolffor sample_img in empty.bochs iotest-dirtylog-10G-4M.vhdx parallels-v1 \
9500e04792SKevin Wolf                  simple-pattern.cloop; do
9600e04792SKevin Wolf
9700e04792SKevin Wolf    echo
9800e04792SKevin Wolf    echo "=== Copying sample image $sample_img into raw ==="
9900e04792SKevin Wolf    echo
10000e04792SKevin Wolf
10100e04792SKevin Wolf    # Can't use _use_sample_img because that isn't designed to be used multiple
10200e04792SKevin Wolf    # times and it overwrites $TEST_IMG (both breaks cleanup)
10300e04792SKevin Wolf    _make_test_img 64M
10400e04792SKevin Wolf    bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src"
10500e04792SKevin Wolf
106a752e478SVladimir Sementsov-Ogievskiy    run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" | _filter_block_job_offset
10700e04792SKevin Wolf    $QEMU_IO -c 'read -P 0 0 64k' "$TEST_IMG" | _filter_qemu_io
10800e04792SKevin Wolf
10900e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY"
11000e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
11100e04792SKevin Wolfdone
11200e04792SKevin Wolf
11300e04792SKevin Wolfecho
11400e04792SKevin Wolfecho "=== Write legitimate MBR into raw ==="
11500e04792SKevin Wolfecho
11600e04792SKevin Wolf
11700e04792SKevin Wolffor sample_img in grub_mbr.raw; do
11800e04792SKevin Wolf    _make_test_img 64M
11900e04792SKevin Wolf    bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src"
12000e04792SKevin Wolf
12100e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_READY"
12200e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
12300e04792SKevin Wolf
12400e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY"
12500e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
12600e04792SKevin Wolfdone
12700e04792SKevin Wolf
12800e04792SKevin Wolf
12900e04792SKevin Wolf# success, all done
13000e04792SKevin Wolfecho '*** done'
13100e04792SKevin Wolfrm -f $seq.full
13200e04792SKevin Wolfstatus=0
133