1*87fe52ceSAndrey Drobyshev#!/usr/bin/env bash 2*87fe52ceSAndrey Drobyshev# group: rw backing auto quick 3*87fe52ceSAndrey Drobyshev# 4*87fe52ceSAndrey Drobyshev# Test qemu-img rebase with compression 5*87fe52ceSAndrey Drobyshev# 6*87fe52ceSAndrey Drobyshev# Copyright (c) 2023 Virtuozzo International GmbH. 7*87fe52ceSAndrey Drobyshev# 8*87fe52ceSAndrey Drobyshev# This program is free software; you can redistribute it and/or modify 9*87fe52ceSAndrey Drobyshev# it under the terms of the GNU General Public License as published by 10*87fe52ceSAndrey Drobyshev# the Free Software Foundation; either version 2 of the License, or 11*87fe52ceSAndrey Drobyshev# (at your option) any later version. 12*87fe52ceSAndrey Drobyshev# 13*87fe52ceSAndrey Drobyshev# This program is distributed in the hope that it will be useful, 14*87fe52ceSAndrey Drobyshev# but WITHOUT ANY WARRANTY; without even the implied warranty of 15*87fe52ceSAndrey Drobyshev# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*87fe52ceSAndrey Drobyshev# GNU General Public License for more details. 17*87fe52ceSAndrey Drobyshev# 18*87fe52ceSAndrey Drobyshev# You should have received a copy of the GNU General Public License 19*87fe52ceSAndrey Drobyshev# along with this program. If not, see <http://www.gnu.org/licenses/>. 20*87fe52ceSAndrey Drobyshev# 21*87fe52ceSAndrey Drobyshev 22*87fe52ceSAndrey Drobyshev# creator 23*87fe52ceSAndrey Drobyshevowner=andrey.drobyshev@virtuozzo.com 24*87fe52ceSAndrey Drobyshev 25*87fe52ceSAndrey Drobyshevseq=`basename $0` 26*87fe52ceSAndrey Drobyshevecho "QA output created by $seq" 27*87fe52ceSAndrey Drobyshev 28*87fe52ceSAndrey Drobyshevstatus=1 # failure is the default! 29*87fe52ceSAndrey Drobyshev 30*87fe52ceSAndrey Drobyshev_cleanup() 31*87fe52ceSAndrey Drobyshev{ 32*87fe52ceSAndrey Drobyshev _cleanup_test_img 33*87fe52ceSAndrey Drobyshev _rm_test_img "$TEST_IMG.base" 34*87fe52ceSAndrey Drobyshev _rm_test_img "$TEST_IMG.itmd" 35*87fe52ceSAndrey Drobyshev} 36*87fe52ceSAndrey Drobyshevtrap "_cleanup; exit \$status" 0 1 2 3 15 37*87fe52ceSAndrey Drobyshev 38*87fe52ceSAndrey Drobyshev# get standard environment, filters and checks 39*87fe52ceSAndrey Drobyshev. ./common.rc 40*87fe52ceSAndrey Drobyshev. ./common.filter 41*87fe52ceSAndrey Drobyshev 42*87fe52ceSAndrey Drobyshev_supported_fmt qcow2 43*87fe52ceSAndrey Drobyshev_supported_proto file 44*87fe52ceSAndrey Drobyshev_supported_os Linux 45*87fe52ceSAndrey Drobyshev 46*87fe52ceSAndrey Drobyshev# Want the size divisible by 2 and 3 47*87fe52ceSAndrey Drobyshevsize=$(( 48 * 1024 * 1024 )) 48*87fe52ceSAndrey Drobyshevhalf_size=$(( size / 2 )) 49*87fe52ceSAndrey Drobyshevthird_size=$(( size / 3 )) 50*87fe52ceSAndrey Drobyshev 51*87fe52ceSAndrey Drobyshev# 1. "qemu-img rebase -c" should refuse working with any format which doesn't 52*87fe52ceSAndrey Drobyshev# support compression. We only check "-f raw" here. 53*87fe52ceSAndrey Drobyshevecho 54*87fe52ceSAndrey Drobyshevecho "=== Testing compressed rebase format compatibility ===" 55*87fe52ceSAndrey Drobyshevecho 56*87fe52ceSAndrey Drobyshev 57*87fe52ceSAndrey Drobyshev$QEMU_IMG create -f raw "$TEST_IMG" "$size" | _filter_img_create 58*87fe52ceSAndrey Drobyshev$QEMU_IMG rebase -c -f raw -b "" "$TEST_IMG" 59*87fe52ceSAndrey Drobyshev 60*87fe52ceSAndrey Drobyshev# 2. Write the 1st half of $size to backing file (compressed), 2nd half -- to 61*87fe52ceSAndrey Drobyshev# the top image (also compressed). Rebase the top image onto no backing file, 62*87fe52ceSAndrey Drobyshev# with compression (i.e. "qemu-img -c -b ''"). Check that the resulting image 63*87fe52ceSAndrey Drobyshev# has the written data preserved, and "qemu-img check" reports 100% clusters 64*87fe52ceSAndrey Drobyshev# as compressed. 65*87fe52ceSAndrey Drobyshevecho 66*87fe52ceSAndrey Drobyshevecho "=== Testing rebase with compression onto no backing file ===" 67*87fe52ceSAndrey Drobyshevecho 68*87fe52ceSAndrey Drobyshev 69*87fe52ceSAndrey DrobyshevTEST_IMG="$TEST_IMG.base" _make_test_img $size 70*87fe52ceSAndrey Drobyshev_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $size 71*87fe52ceSAndrey Drobyshev 72*87fe52ceSAndrey Drobyshev$QEMU_IO -c "write -c -P 0xaa 0 $half_size" "$TEST_IMG.base" | _filter_qemu_io 73*87fe52ceSAndrey Drobyshev$QEMU_IO -c "write -c -P 0xbb $half_size $half_size" "$TEST_IMG" \ 74*87fe52ceSAndrey Drobyshev | _filter_qemu_io 75*87fe52ceSAndrey Drobyshev 76*87fe52ceSAndrey Drobyshev$QEMU_IMG rebase -c -f $IMGFMT -b "" "$TEST_IMG" 77*87fe52ceSAndrey Drobyshev 78*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xaa 0 $half_size" "$TEST_IMG" | _filter_qemu_io 79*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xbb $half_size $half_size" "$TEST_IMG" | _filter_qemu_io 80*87fe52ceSAndrey Drobyshev 81*87fe52ceSAndrey Drobyshev$QEMU_IMG check "$TEST_IMG" | _filter_testdir 82*87fe52ceSAndrey Drobyshev 83*87fe52ceSAndrey Drobyshev# 3. Same as the previous one, but with raw backing file (hence write to 84*87fe52ceSAndrey Drobyshev# the backing is uncompressed). 85*87fe52ceSAndrey Drobyshevecho 86*87fe52ceSAndrey Drobyshevecho "=== Testing rebase with compression with raw backing file ===" 87*87fe52ceSAndrey Drobyshevecho 88*87fe52ceSAndrey Drobyshev 89*87fe52ceSAndrey Drobyshev$QEMU_IMG create -f raw "$TEST_IMG.base" "$half_size" | _filter_img_create 90*87fe52ceSAndrey Drobyshev_make_test_img -b "$TEST_IMG.base" -F raw $size 91*87fe52ceSAndrey Drobyshev 92*87fe52ceSAndrey Drobyshev$QEMU_IO -f raw -c "write -P 0xaa 0 $half_size" "$TEST_IMG.base" \ 93*87fe52ceSAndrey Drobyshev | _filter_qemu_io 94*87fe52ceSAndrey Drobyshev$QEMU_IO -c "write -c -P 0xbb $half_size $half_size" \ 95*87fe52ceSAndrey Drobyshev "$TEST_IMG" | _filter_qemu_io 96*87fe52ceSAndrey Drobyshev 97*87fe52ceSAndrey Drobyshev$QEMU_IMG rebase -c -f $IMGFMT -b "" "$TEST_IMG" 98*87fe52ceSAndrey Drobyshev 99*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xaa 0 $half_size" "$TEST_IMG" | _filter_qemu_io 100*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xbb $half_size $half_size" "$TEST_IMG" | _filter_qemu_io 101*87fe52ceSAndrey Drobyshev 102*87fe52ceSAndrey Drobyshev$QEMU_IMG check "$TEST_IMG" | _filter_testdir 103*87fe52ceSAndrey Drobyshev 104*87fe52ceSAndrey Drobyshev# 4. Create a backing chain base<--itmd<--img, filling 1st, 2nd and 3rd 105*87fe52ceSAndrey Drobyshev# thirds of them, respectively (with compression). Rebase img onto base, 106*87fe52ceSAndrey Drobyshev# effectively deleting itmd from the chain, and check that written data is 107*87fe52ceSAndrey Drobyshev# preserved in the resulting image. Also check that "qemu-img check" reports 108*87fe52ceSAndrey Drobyshev# 100% clusters as compressed. 109*87fe52ceSAndrey Drobyshevecho 110*87fe52ceSAndrey Drobyshevecho "=== Testing compressed rebase removing single delta from the chain ===" 111*87fe52ceSAndrey Drobyshevecho 112*87fe52ceSAndrey Drobyshev 113*87fe52ceSAndrey DrobyshevTEST_IMG="$TEST_IMG.base" _make_test_img $size 114*87fe52ceSAndrey DrobyshevTEST_IMG="$TEST_IMG.itmd" _make_test_img -b "$TEST_IMG.base" -F $IMGFMT $size 115*87fe52ceSAndrey Drobyshev_make_test_img -b "$TEST_IMG.itmd" -F $IMGFMT $size 116*87fe52ceSAndrey Drobyshev 117*87fe52ceSAndrey Drobyshev$QEMU_IO -c "write -c -P 0xaa 0 $third_size" \ 118*87fe52ceSAndrey Drobyshev "$TEST_IMG.base" | _filter_qemu_io 119*87fe52ceSAndrey Drobyshev$QEMU_IO -c "write -c -P 0xbb $third_size $third_size" \ 120*87fe52ceSAndrey Drobyshev "$TEST_IMG.itmd" | _filter_qemu_io 121*87fe52ceSAndrey Drobyshev$QEMU_IO -c "write -c -P 0xcc $((third_size * 2 )) $third_size" \ 122*87fe52ceSAndrey Drobyshev "$TEST_IMG" | _filter_qemu_io 123*87fe52ceSAndrey Drobyshev 124*87fe52ceSAndrey Drobyshev$QEMU_IMG rebase -c -f $IMGFMT -b "$TEST_IMG.base" -F $IMGFMT "$TEST_IMG" 125*87fe52ceSAndrey Drobyshev 126*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xaa 0 $third_size" "$TEST_IMG" | _filter_qemu_io 127*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xbb $third_size $third_size" \ 128*87fe52ceSAndrey Drobyshev "$TEST_IMG" | _filter_qemu_io 129*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xcc $(( third_size * 2 )) $third_size" \ 130*87fe52ceSAndrey Drobyshev "$TEST_IMG" | _filter_qemu_io 131*87fe52ceSAndrey Drobyshev 132*87fe52ceSAndrey Drobyshev$QEMU_IMG check "$TEST_IMG" | _filter_testdir 133*87fe52ceSAndrey Drobyshev 134*87fe52ceSAndrey Drobyshev# 5. Create one-cluster backing and overlay images, and fill only the first 135*87fe52ceSAndrey Drobyshev# (half - 1) bytes of the backing with data (uncompressed). Rebase the 136*87fe52ceSAndrey Drobyshev# overlay onto no backing file with compression. Check that data is still 137*87fe52ceSAndrey Drobyshev# read correctly, and that cluster is now really compressed ("qemu-img check" 138*87fe52ceSAndrey Drobyshev# reports 100% clusters as compressed. 139*87fe52ceSAndrey Drobyshevecho 140*87fe52ceSAndrey Drobyshevecho "=== Testing compressed rebase with unaligned unmerged data ===" 141*87fe52ceSAndrey Drobyshevecho 142*87fe52ceSAndrey Drobyshev 143*87fe52ceSAndrey DrobyshevCLUSTER_SIZE=65536 144*87fe52ceSAndrey Drobyshev 145*87fe52ceSAndrey DrobyshevTEST_IMG="$TEST_IMG.base" _make_test_img $CLUSTER_SIZE 146*87fe52ceSAndrey Drobyshev_make_test_img -b "$TEST_IMG.base" -F $IMGFMT $CLUSTER_SIZE 147*87fe52ceSAndrey Drobyshev 148*87fe52ceSAndrey Drobyshev$QEMU_IO -c "write -P 0xaa 0 $(( CLUSTER_SIZE / 2 - 1 ))" $TEST_IMG.base \ 149*87fe52ceSAndrey Drobyshev | _filter_qemu_io 150*87fe52ceSAndrey Drobyshev 151*87fe52ceSAndrey Drobyshev$QEMU_IMG rebase -c -f $IMGFMT -b "" "$TEST_IMG" 152*87fe52ceSAndrey Drobyshev 153*87fe52ceSAndrey Drobyshev$QEMU_IO -c "read -P 0xaa 0 $(( CLUSTER_SIZE / 2 - 1 ))" "$TEST_IMG" \ 154*87fe52ceSAndrey Drobyshev | _filter_qemu_io 155*87fe52ceSAndrey Drobyshev$QEMU_IO -c \ 156*87fe52ceSAndrey Drobyshev "read -P 0x00 $(( CLUSTER_SIZE / 2 - 1 )) $(( CLUSTER_SIZE / 2 + 1 ))" \ 157*87fe52ceSAndrey Drobyshev "$TEST_IMG" | _filter_qemu_io 158*87fe52ceSAndrey Drobyshev 159*87fe52ceSAndrey Drobyshev$QEMU_IMG check "$TEST_IMG" | _filter_testdir 160*87fe52ceSAndrey Drobyshev 161*87fe52ceSAndrey Drobyshev# success, all done 162*87fe52ceSAndrey Drobyshevecho 163*87fe52ceSAndrey Drobyshevecho '*** done' 164*87fe52ceSAndrey Drobyshevrm -f $seq.full 165*87fe52ceSAndrey Drobyshevstatus=0 166