1#!/usr/bin/env bash
2# group: rw auto quick
3#
4# Check that errors while closing the image, in particular writing back dirty
5# bitmaps, is correctly reported with a failing qemu-img exit code.
6#
7# Copyright (C) 2023 Red Hat, Inc.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23# creator
24owner=kwolf@redhat.com
25
26seq="$(basename $0)"
27echo "QA output created by $seq"
28
29status=1	# failure is the default!
30
31_cleanup()
32{
33    _cleanup_test_img
34}
35trap "_cleanup; exit \$status" 0 1 2 3 15
36
37# get standard environment, filters and checks
38cd ..
39. ./common.rc
40. ./common.filter
41
42_supported_fmt qcow2
43_supported_proto file
44_supported_os Linux
45
46size=1G
47
48# The error we are going to use is ENOSPC. Depending on how many bitmaps we
49# create in the backing file (and therefore increase the used up space), we get
50# failures in different places. With a low number, only merging the bitmap
51# fails, whereas with a higher number, already 'qemu-img commit' fails.
52for max_bitmap in 6 7; do
53    echo
54    echo "=== Test with $max_bitmap bitmaps ==="
55
56    TEST_IMG="$TEST_IMG.base" _make_test_img -q $size
57    for i in $(seq 1 $max_bitmap); do
58        $QEMU_IMG bitmap --add "$TEST_IMG.base" "stale-bitmap-$i"
59    done
60
61    # Simulate a block device of 128 MB by resizing the image file accordingly
62    # and then enforcing the size with the raw driver
63    $QEMU_IO -f raw -c "truncate 128M" "$TEST_IMG.base"
64    BASE_JSON='json:{
65        "driver": "qcow2",
66        "file": {
67            "driver": "raw",
68            "size": 134217728,
69            "file": {
70                "driver": "file",
71                "filename":"'"$TEST_IMG.base"'"
72            }
73        }
74    }'
75
76    _make_test_img -q -b "$BASE_JSON" -F $IMGFMT
77    $QEMU_IMG bitmap --add "$TEST_IMG" "good-bitmap"
78
79    $QEMU_IO -c 'write 0 126m' "$TEST_IMG" | _filter_qemu_io
80
81    $QEMU_IMG commit -d "$TEST_IMG" 2>&1 | _filter_generated_node_ids
82    echo "qemu-img commit exit code: ${PIPESTATUS[0]}"
83
84    $QEMU_IMG bitmap --add "$BASE_JSON" "good-bitmap"
85    echo "qemu-img bitmap --add exit code: $?"
86
87    $QEMU_IMG bitmap --merge "good-bitmap" -b "$TEST_IMG" "$BASE_JSON" \
88        "good-bitmap" 2>&1 | _filter_generated_node_ids
89    echo "qemu-img bitmap --merge exit code:  ${PIPESTATUS[0]}"
90done
91
92# success, all done
93echo "*** done"
94rm -f $seq.full
95status=0
96
97