xref: /openbmc/qemu/tests/qemu-iotests/109 (revision f389309d2937e66960dd371014a1971678fb4ce7)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw
300e04792SKevin Wolf#
400e04792SKevin Wolf# Test writing image headers of other formats into raw images
500e04792SKevin Wolf#
600e04792SKevin Wolf# Copyright (C) 2014 Red Hat, Inc.
700e04792SKevin Wolf#
800e04792SKevin Wolf# This program is free software; you can redistribute it and/or modify
900e04792SKevin Wolf# it under the terms of the GNU General Public License as published by
1000e04792SKevin Wolf# the Free Software Foundation; either version 2 of the License, or
1100e04792SKevin Wolf# (at your option) any later version.
1200e04792SKevin Wolf#
1300e04792SKevin Wolf# This program is distributed in the hope that it will be useful,
1400e04792SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
1500e04792SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1600e04792SKevin Wolf# GNU General Public License for more details.
1700e04792SKevin Wolf#
1800e04792SKevin Wolf# You should have received a copy of the GNU General Public License
1900e04792SKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2000e04792SKevin Wolf#
2100e04792SKevin Wolf
2200e04792SKevin Wolf# creator
2300e04792SKevin Wolfowner=kwolf@redhat.com
2400e04792SKevin Wolf
2500e04792SKevin Wolfseq="$(basename $0)"
2600e04792SKevin Wolfecho "QA output created by $seq"
2700e04792SKevin Wolf
2800e04792SKevin Wolfstatus=1	# failure is the default!
2900e04792SKevin Wolf
3000e04792SKevin Wolf_cleanup()
3100e04792SKevin Wolf{
32ecfa1854SJeff Cody    _cleanup_qemu
33f91ecbd7SMax Reitz    _rm_test_img "$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
46503034efSVladimir Sementsov-Ogievskiy_require_drivers qcow qcow2 qed vdi vmdk vpc
4700e04792SKevin Wolf
4800e04792SKevin Wolfqemu_comm_method=qmp
4900e04792SKevin Wolf
508cedcffdSEric Blakerun_qemu()
5100e04792SKevin Wolf{
5200e04792SKevin Wolf    local raw_img="$1"
5300e04792SKevin Wolf    local source_img="$2"
5400e04792SKevin Wolf    local qmp_format="$3"
5500e04792SKevin Wolf    local qmp_event="$4"
5600e04792SKevin Wolf
578dff69b9SAarushi Mehta    _launch_qemu -drive file="${source_img}",format=raw,cache=${CACHEMODE},aio=${AIOMODE},id=src
5800e04792SKevin Wolf    _send_qemu_cmd $QEMU_HANDLE "{ 'execute': 'qmp_capabilities' }" "return"
5900e04792SKevin Wolf
60*f389309dSStefan Hajnoczi    capture_events="$qmp_event" _send_qemu_cmd $QEMU_HANDLE \
6100e04792SKevin Wolf        "{'execute':'drive-mirror', 'arguments':{
6200e04792SKevin Wolf            'device': 'src', 'target': '$raw_img', $qmp_format
6300e04792SKevin Wolf            'mode': 'existing', 'sync': 'full'}}" \
6400e04792SKevin Wolf        "return"
6500e04792SKevin Wolf
66*f389309dSStefan Hajnoczi    capture_events="$qmp_event JOB_STATUS_CHANGE" _wait_event $QEMU_HANDLE "$qmp_event"
670a4c0c3fSPaolo Bonzini    if test "$qmp_event" = BLOCK_JOB_ERROR; then
681dac83f1SKevin Wolf        _send_qemu_cmd $QEMU_HANDLE '' '"status": "null"'
690a4c0c3fSPaolo Bonzini    fi
7000e04792SKevin Wolf    _send_qemu_cmd $QEMU_HANDLE '{"execute":"query-block-jobs"}' "return"
7153dd4015SCleber Rosa    _send_qemu_cmd $QEMU_HANDLE '{"execute":"quit"}' "return"
7253dd4015SCleber Rosa    wait=1 _cleanup_qemu
7300e04792SKevin Wolf}
7400e04792SKevin Wolf
7500e04792SKevin Wolffor fmt in qcow qcow2 qed vdi vmdk vpc; do
7600e04792SKevin Wolf
7700e04792SKevin Wolf    echo
7800e04792SKevin Wolf    echo "=== Writing a $fmt header into raw ==="
7900e04792SKevin Wolf    echo
8000e04792SKevin Wolf
8100e04792SKevin Wolf    TEST_IMG="$TEST_IMG.src" IMGFMT=$fmt _make_test_img 64M
82ffa41a62SKevin Wolf    _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size
8300e04792SKevin Wolf
8400e04792SKevin Wolf    # This first test should fail: The image format was probed, we may not
8500e04792SKevin Wolf    # write an image header at the start of the image
8624dfdfd0SFam Zheng    run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" |
8724dfdfd0SFam Zheng        _filter_block_job_len
88ffa41a62SKevin Wolf    $QEMU_IO -c 'read -P 0 0 512' "$TEST_IMG" | _filter_qemu_io
8900e04792SKevin Wolf
9000e04792SKevin Wolf
9100e04792SKevin Wolf    # When raw was explicitly specified, the same must succeed
9200e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY"
9300e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
9400e04792SKevin Wolf
9500e04792SKevin Wolfdone
9600e04792SKevin Wolf
9700e04792SKevin Wolf
9800e04792SKevin Wolffor sample_img in empty.bochs iotest-dirtylog-10G-4M.vhdx parallels-v1 \
9900e04792SKevin Wolf                  simple-pattern.cloop; do
10000e04792SKevin Wolf
10100e04792SKevin Wolf    echo
10200e04792SKevin Wolf    echo "=== Copying sample image $sample_img into raw ==="
10300e04792SKevin Wolf    echo
10400e04792SKevin Wolf
10500e04792SKevin Wolf    # Can't use _use_sample_img because that isn't designed to be used multiple
10600e04792SKevin Wolf    # times and it overwrites $TEST_IMG (both breaks cleanup)
10700e04792SKevin Wolf    bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src"
108ffa41a62SKevin Wolf    _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size
10900e04792SKevin Wolf
11024dfdfd0SFam Zheng    run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_ERROR" |
11124dfdfd0SFam Zheng        _filter_block_job_offset | _filter_block_job_len
112ffa41a62SKevin Wolf    $QEMU_IO -c 'read -P 0 0 512' "$TEST_IMG" | _filter_qemu_io
11300e04792SKevin Wolf
11400e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY"
11500e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
11600e04792SKevin Wolfdone
11700e04792SKevin Wolf
11800e04792SKevin Wolfecho
11900e04792SKevin Wolfecho "=== Write legitimate MBR into raw ==="
12000e04792SKevin Wolfecho
12100e04792SKevin Wolf
12200e04792SKevin Wolffor sample_img in grub_mbr.raw; do
12300e04792SKevin Wolf    bzcat "$SAMPLE_IMG_DIR/$sample_img.bz2" > "$TEST_IMG.src"
124ffa41a62SKevin Wolf    _make_test_img $(du -b "$TEST_IMG.src" | cut -f1) | _filter_img_create_size
12500e04792SKevin Wolf
12600e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "" "BLOCK_JOB_READY"
12700e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
12800e04792SKevin Wolf
12900e04792SKevin Wolf    run_qemu "$TEST_IMG" "$TEST_IMG.src" "'format': 'raw'," "BLOCK_JOB_READY"
13000e04792SKevin Wolf    $QEMU_IMG compare -f raw -F raw "$TEST_IMG" "$TEST_IMG.src"
13100e04792SKevin Wolfdone
13200e04792SKevin Wolf
13300e04792SKevin Wolf
13400e04792SKevin Wolf# success, all done
13500e04792SKevin Wolfecho '*** done'
13600e04792SKevin Wolfrm -f $seq.full
13700e04792SKevin Wolfstatus=0
138