1#!/usr/bin/env bash
2# group: rw quick
3#
4# Test qemu-img bitmap handling
5#
6# Copyright (C) 2018-2021 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22seq="$(basename $0)"
23echo "QA output created by $seq"
24
25status=1 # failure is the default!
26
27_cleanup()
28{
29    _cleanup_test_img
30    _rm_test_img "$TEST_IMG.copy"
31    nbd_server_stop
32}
33trap "_cleanup; exit \$status" 0 1 2 3 15
34
35# get standard environment, filters and checks
36cd ..
37. ./common.rc
38. ./common.filter
39. ./common.nbd
40
41_supported_fmt qcow2
42_supported_proto file fuse
43_supported_os Linux
44_require_command QEMU_NBD
45# compat=0.10 does not support bitmaps
46_unsupported_imgopts 'compat=0.10'
47
48# Filter irrelevant format-specific information from the qemu-img info
49# output (we only want the bitmaps, basically)
50_filter_irrelevant_img_info()
51{
52    grep -v -e 'compat' -e 'compression type' -e 'data file' -e 'extended l2' \
53            -e 'lazy refcounts' -e 'refcount bits'
54}
55
56echo
57echo "=== Initial image setup ==="
58echo
59
60# Create backing image with one bitmap
61TEST_IMG="$TEST_IMG.base" _make_test_img 10M
62$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG.base" b0
63$QEMU_IO -c 'w 3M 1M' -f $IMGFMT "$TEST_IMG.base" | _filter_qemu_io
64
65# Create initial image and populate two bitmaps: one active, one inactive.
66ORIG_IMG=$TEST_IMG
67TEST_IMG=$TEST_IMG.orig
68_make_test_img -b "$ORIG_IMG.base" -F $IMGFMT 10M
69$QEMU_IO -c 'w 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
70$QEMU_IMG bitmap --add -g 512k -f $IMGFMT "$TEST_IMG" b1
71$QEMU_IMG bitmap --add --disable -f $IMGFMT "$TEST_IMG" b2
72$QEMU_IO -c 'w 3M 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
73$QEMU_IMG bitmap --clear -f $IMGFMT "$TEST_IMG" b1
74$QEMU_IO -c 'w 1M 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
75$QEMU_IMG bitmap --disable -f $IMGFMT "$TEST_IMG" b1
76$QEMU_IMG bitmap --enable -f $IMGFMT "$TEST_IMG" b2
77$QEMU_IO -c 'w 2M 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
78
79echo
80echo "=== Bitmap preservation not possible to non-qcow2 ==="
81echo
82
83TEST_IMG=$ORIG_IMG
84$QEMU_IMG convert --bitmaps -O raw "$TEST_IMG.orig" "$TEST_IMG" &&
85    echo "unexpected success"
86
87echo
88echo "=== Convert with bitmap preservation ==="
89echo
90
91# Only bitmaps from the active layer are copied
92$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG.orig" "$TEST_IMG"
93_img_info --format-specific | _filter_irrelevant_img_info
94# But we can also merge in bitmaps from other layers.  This test is a bit
95# contrived to cover more code paths, in reality, you could merge directly
96# into b0 without going through tmp
97$QEMU_IMG bitmap --add --disable -f $IMGFMT "$TEST_IMG" b0
98$QEMU_IMG bitmap --add --merge b0 -b "$TEST_IMG.base" -F $IMGFMT \
99     -f $IMGFMT "$TEST_IMG" tmp
100$QEMU_IMG bitmap --merge tmp -f $IMGFMT "$TEST_IMG" b0
101$QEMU_IMG bitmap --remove --image-opts \
102    driver=$IMGFMT,file.driver=file,file.filename="$TEST_IMG" tmp
103_img_info --format-specific | _filter_irrelevant_img_info
104
105echo
106echo "=== Merge from top layer into backing image ==="
107echo
108
109$QEMU_IMG rebase -u -F qcow2 -b "$TEST_IMG.base" "$TEST_IMG"
110$QEMU_IMG bitmap --add --merge b2 -b "$TEST_IMG" -F $IMGFMT \
111     -f $IMGFMT "$TEST_IMG.base" b3
112_img_info --format-specific --backing-chain | _filter_irrelevant_img_info
113
114echo
115echo "=== Check bitmap contents ==="
116echo
117
118# x-dirty-bitmap is a hack for reading bitmaps; it abuses block status to
119# report "data":false for portions of the bitmap which are set
120IMG="driver=nbd,server.type=unix,server.path=$nbd_unix_socket"
121nbd_server_start_unix_socket -r -f qcow2 \
122    -B b0 -B b1 -B b2 -B b3 "$TEST_IMG"
123$QEMU_IMG map --output=json --image-opts \
124    "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b0" | _filter_qemu_img_map
125$QEMU_IMG map --output=json --image-opts \
126    "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b1" | _filter_qemu_img_map
127$QEMU_IMG map --output=json --image-opts \
128    "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b2" | _filter_qemu_img_map
129$QEMU_IMG map --output=json --image-opts \
130    "$IMG,x-dirty-bitmap=qemu:dirty-bitmap:b3" | _filter_qemu_img_map
131
132nbd_server_stop
133
134echo
135echo "=== Check handling of inconsistent bitmap ==="
136echo
137
138# Prepare image with corrupted bitmap
139$QEMU_IO -c abort "$TEST_IMG" 2>/dev/null
140$QEMU_IMG bitmap --add "$TEST_IMG" b4
141$QEMU_IMG bitmap --remove "$TEST_IMG" b1
142_img_info --format-specific | _filter_irrelevant_img_info
143# Proof that we fail fast if bitmaps can't be copied
144echo
145$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy" &&
146    echo "unexpected success"
147TEST_IMG="$TEST_IMG.copy" _img_info --format-specific \
148    | _filter_irrelevant_img_info
149# Skipping the broken bitmaps works,...
150echo
151$QEMU_IMG convert --bitmaps --skip-broken-bitmaps \
152    -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"
153TEST_IMG="$TEST_IMG.copy" _img_info --format-specific \
154    | _filter_irrelevant_img_info
155# ...as does removing them
156echo
157_rm_test_img "$TEST_IMG.copy"
158$QEMU_IMG bitmap --remove "$TEST_IMG" b0
159$QEMU_IMG bitmap --remove --add "$TEST_IMG" b2
160$QEMU_IMG convert --bitmaps -O qcow2 "$TEST_IMG" "$TEST_IMG.copy"
161TEST_IMG="$TEST_IMG.copy" _img_info --format-specific \
162    | _filter_irrelevant_img_info
163
164# success, all done
165echo '*** done'
166rm -f $seq.full
167status=0
168