111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2939e1640SKevin Wolf# 3939e1640SKevin Wolf# Rebasing COW images 4939e1640SKevin Wolf# 5939e1640SKevin Wolf# Copyright (C) 2009 Red Hat, Inc. 6939e1640SKevin Wolf# 7939e1640SKevin Wolf# This program is free software; you can redistribute it and/or modify 8939e1640SKevin Wolf# it under the terms of the GNU General Public License as published by 9939e1640SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 10939e1640SKevin Wolf# (at your option) any later version. 11939e1640SKevin Wolf# 12939e1640SKevin Wolf# This program is distributed in the hope that it will be useful, 13939e1640SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 14939e1640SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15939e1640SKevin Wolf# GNU General Public License for more details. 16939e1640SKevin Wolf# 17939e1640SKevin Wolf# You should have received a copy of the GNU General Public License 18939e1640SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 19939e1640SKevin Wolf# 20939e1640SKevin Wolf 21939e1640SKevin Wolf# creator 22939e1640SKevin Wolfowner=kwolf@redhat.com 23939e1640SKevin Wolf 24939e1640SKevin Wolfseq=`basename $0` 25939e1640SKevin Wolfecho "QA output created by $seq" 26939e1640SKevin Wolf 27939e1640SKevin Wolfstatus=1 # failure is the default! 28939e1640SKevin Wolf 29939e1640SKevin Wolf_cleanup() 30939e1640SKevin Wolf{ 31939e1640SKevin Wolf _cleanup_test_img 32f91ecbd7SMax Reitz _rm_test_img "$TEST_DIR/t.$IMGFMT.base_old" 33f91ecbd7SMax Reitz _rm_test_img "$TEST_DIR/t.$IMGFMT.base_new" 3428036a7fSMax Reitz 35f91ecbd7SMax Reitz _rm_test_img "$TEST_DIR/subdir/t.$IMGFMT" 36f91ecbd7SMax Reitz _rm_test_img "$TEST_DIR/subdir/t.$IMGFMT.base_old" 37f91ecbd7SMax Reitz _rm_test_img "$TEST_DIR/subdir/t.$IMGFMT.base_new" 3828036a7fSMax Reitz rmdir "$TEST_DIR/subdir" 2> /dev/null 39939e1640SKevin Wolf} 40939e1640SKevin Wolftrap "_cleanup; exit \$status" 0 1 2 3 15 41939e1640SKevin Wolf 42939e1640SKevin Wolf# get standard environment, filters and checks 43939e1640SKevin Wolf. ./common.rc 44939e1640SKevin Wolf. ./common.filter 45939e1640SKevin Wolf. ./common.pattern 46939e1640SKevin Wolf 47f5a4bbd9SStefan Hajnoczi# Currently only qcow2 and qed support rebasing 48f5a4bbd9SStefan Hajnoczi_supported_fmt qcow2 qed 491f7bf7d0SPeter Lieven_supported_proto file 50939e1640SKevin Wolf_supported_os Linux 51939e1640SKevin Wolf 52939e1640SKevin WolfCLUSTER_SIZE=65536 53939e1640SKevin Wolf 54939e1640SKevin Wolf# Cluster allocations to be tested: 55939e1640SKevin Wolf# 56939e1640SKevin Wolf# Backing (old) 11 -- 11 -- 11 -- 11 -- 57939e1640SKevin Wolf# Backing (new) 22 22 -- -- 22 22 -- -- 58939e1640SKevin Wolf# COW image 33 33 33 33 -- -- -- -- 59d586bc63SKevin Wolf# 60d586bc63SKevin Wolf# The pattern is written twice to have both an alloc -> non-alloc and a 61d586bc63SKevin Wolf# non-alloc -> alloc transition in the COW image. 62939e1640SKevin Wolf 63939e1640SKevin Wolfecho "Creating backing file" 64939e1640SKevin Wolfecho 65939e1640SKevin Wolf 66f19f1189SFam ZhengTEST_IMG_SAVE="$TEST_IMG" 67f19f1189SFam ZhengTEST_IMG="$TEST_IMG.base_old" 68f19f1189SFam Zheng 69939e1640SKevin Wolf_make_test_img 1G 70dd0c35d6SStefan Hajnocziio_pattern writev 0 $CLUSTER_SIZE $((2 * CLUSTER_SIZE)) 8 0x11 71f19f1189SFam Zheng 72f19f1189SFam ZhengTEST_IMG="$TEST_IMG_SAVE.base_new" 73939e1640SKevin Wolf 74939e1640SKevin Wolfecho "Creating new backing file" 75939e1640SKevin Wolfecho 76939e1640SKevin Wolf 77939e1640SKevin Wolf_make_test_img 1G 78dd0c35d6SStefan Hajnocziio_pattern writev 0 $((2 * CLUSTER_SIZE)) $((4 * CLUSTER_SIZE)) 4 0x22 79f19f1189SFam Zheng 80f19f1189SFam Zheng 81f19f1189SFam ZhengTEST_IMG="$TEST_IMG_SAVE" 82939e1640SKevin Wolf 83939e1640SKevin Wolfecho "Creating COW image" 84939e1640SKevin Wolfecho 85939e1640SKevin Wolf 86*b66ff2c2SEric Blake_make_test_img -b "$TEST_IMG.base_old" -F $IMGFMT 1G 87939e1640SKevin Wolfio_pattern writev 0 $((4 * CLUSTER_SIZE)) 0 1 0x33 88d586bc63SKevin Wolfio_pattern writev $((8 * CLUSTER_SIZE)) $((4 * CLUSTER_SIZE)) 0 1 0x33 89939e1640SKevin Wolf 90939e1640SKevin Wolfecho "Read before the rebase to make sure everything is set up correctly" 91939e1640SKevin Wolfecho 92939e1640SKevin Wolfio_pattern readv $((0 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 93939e1640SKevin Wolfio_pattern readv $((1 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 94939e1640SKevin Wolfio_pattern readv $((2 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 95939e1640SKevin Wolfio_pattern readv $((3 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 96939e1640SKevin Wolfio_pattern readv $((4 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 97939e1640SKevin Wolfio_pattern readv $((5 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 98939e1640SKevin Wolfio_pattern readv $((6 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 99939e1640SKevin Wolfio_pattern readv $((7 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 100d586bc63SKevin Wolfio_pattern readv $((8 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 101d586bc63SKevin Wolfio_pattern readv $((9 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 102d586bc63SKevin Wolfio_pattern readv $((10 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 103d586bc63SKevin Wolfio_pattern readv $((11 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 104d586bc63SKevin Wolfio_pattern readv $((12 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 105d586bc63SKevin Wolfio_pattern readv $((13 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 106d586bc63SKevin Wolfio_pattern readv $((14 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 107d586bc63SKevin Wolfio_pattern readv $((15 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 108939e1640SKevin Wolf 109939e1640SKevin Wolfecho 110939e1640SKevin Wolfecho Rebase and test again 111939e1640SKevin Wolfecho 112*b66ff2c2SEric Blake$QEMU_IMG rebase -b "$TEST_IMG.base_new" -F $IMGFMT "$TEST_IMG" 113939e1640SKevin Wolfio_pattern readv $((0 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 114939e1640SKevin Wolfio_pattern readv $((1 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 115939e1640SKevin Wolfio_pattern readv $((2 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 116939e1640SKevin Wolfio_pattern readv $((3 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 117939e1640SKevin Wolfio_pattern readv $((4 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 118939e1640SKevin Wolfio_pattern readv $((5 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 119939e1640SKevin Wolfio_pattern readv $((6 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 120939e1640SKevin Wolfio_pattern readv $((7 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 121d586bc63SKevin Wolfio_pattern readv $((8 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 122d586bc63SKevin Wolfio_pattern readv $((9 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 123d586bc63SKevin Wolfio_pattern readv $((10 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 124d586bc63SKevin Wolfio_pattern readv $((11 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x33 125d586bc63SKevin Wolfio_pattern readv $((12 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 126d586bc63SKevin Wolfio_pattern readv $((13 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 127d586bc63SKevin Wolfio_pattern readv $((14 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x11 128d586bc63SKevin Wolfio_pattern readv $((15 * CLUSTER_SIZE)) $CLUSTER_SIZE 0 1 0x00 129939e1640SKevin Wolf 13028036a7fSMax Reitzecho 13128036a7fSMax Reitzecho "=== Test rebase in a subdirectory of the working directory ===" 13228036a7fSMax Reitzecho 13328036a7fSMax Reitz 13428036a7fSMax Reitz# Clean up the old images beforehand so they do not interfere with 13528036a7fSMax Reitz# this test 13628036a7fSMax Reitz_cleanup 13728036a7fSMax Reitz 13828036a7fSMax Reitzmkdir "$TEST_DIR/subdir" 13928036a7fSMax Reitz 14028036a7fSMax Reitz# Relative to the overlay 14128036a7fSMax ReitzBASE_OLD_OREL="t.$IMGFMT.base_old" 14228036a7fSMax ReitzBASE_NEW_OREL="t.$IMGFMT.base_new" 14328036a7fSMax Reitz 14428036a7fSMax Reitz# Relative to $TEST_DIR (which is going to be our working directory) 14528036a7fSMax ReitzOVERLAY_WREL="subdir/t.$IMGFMT" 14628036a7fSMax Reitz 14728036a7fSMax ReitzBASE_OLD="$TEST_DIR/subdir/$BASE_OLD_OREL" 14828036a7fSMax ReitzBASE_NEW="$TEST_DIR/subdir/$BASE_NEW_OREL" 14928036a7fSMax ReitzOVERLAY="$TEST_DIR/$OVERLAY_WREL" 15028036a7fSMax Reitz 15128036a7fSMax Reitz# Test done here: 15228036a7fSMax Reitz# 15328036a7fSMax Reitz# Backing (old): 11 11 -- 11 15428036a7fSMax Reitz# Backing (new): -- 22 22 11 15528036a7fSMax Reitz# Overlay: -- -- -- -- 15628036a7fSMax Reitz# 15728036a7fSMax Reitz# Rebasing works, we have verified that above. Here, we just want to 15828036a7fSMax Reitz# see that rebasing is done for the correct target backing file. 15928036a7fSMax Reitz 16028036a7fSMax ReitzTEST_IMG=$BASE_OLD _make_test_img 1M 16128036a7fSMax ReitzTEST_IMG=$BASE_NEW _make_test_img 1M 162*b66ff2c2SEric BlakeTEST_IMG=$OVERLAY _make_test_img -b "$BASE_OLD_OREL" -F $IMGFMT 1M 16328036a7fSMax Reitz 16428036a7fSMax Reitzecho 16528036a7fSMax Reitz 16628036a7fSMax Reitz$QEMU_IO "$BASE_OLD" \ 16728036a7fSMax Reitz -c "write -P 0x11 $((0 * CLUSTER_SIZE)) $((2 * CLUSTER_SIZE))" \ 16828036a7fSMax Reitz -c "write -P 0x11 $((3 * CLUSTER_SIZE)) $((1 * CLUSTER_SIZE))" \ 16928036a7fSMax Reitz | _filter_qemu_io 17028036a7fSMax Reitz 17128036a7fSMax Reitz$QEMU_IO "$BASE_NEW" \ 17228036a7fSMax Reitz -c "write -P 0x22 $((1 * CLUSTER_SIZE)) $((2 * CLUSTER_SIZE))" \ 17328036a7fSMax Reitz -c "write -P 0x11 $((3 * CLUSTER_SIZE)) $((1 * CLUSTER_SIZE))" \ 17428036a7fSMax Reitz | _filter_qemu_io 17528036a7fSMax Reitz 17628036a7fSMax Reitzecho 17728036a7fSMax Reitz 17828036a7fSMax Reitzpushd "$TEST_DIR" >/dev/null 179*b66ff2c2SEric Blake$QEMU_IMG rebase -f "$IMGFMT" -b "$BASE_NEW_OREL" -F $IMGFMT "$OVERLAY_WREL" 18028036a7fSMax Reitzpopd >/dev/null 18128036a7fSMax Reitz 18228036a7fSMax Reitz# Verify the backing path is correct 183*b66ff2c2SEric BlakeTEST_IMG=$OVERLAY _img_info | grep '^backing file:' 18428036a7fSMax Reitz 18528036a7fSMax Reitzecho 18628036a7fSMax Reitz 18728036a7fSMax Reitz# Verify the data is correct 18828036a7fSMax Reitz$QEMU_IO "$OVERLAY" \ 18928036a7fSMax Reitz -c "read -P 0x11 $((0 * CLUSTER_SIZE)) $CLUSTER_SIZE" \ 19028036a7fSMax Reitz -c "read -P 0x11 $((1 * CLUSTER_SIZE)) $CLUSTER_SIZE" \ 19128036a7fSMax Reitz -c "read -P 0x00 $((2 * CLUSTER_SIZE)) $CLUSTER_SIZE" \ 19228036a7fSMax Reitz -c "read -P 0x11 $((3 * CLUSTER_SIZE)) $CLUSTER_SIZE" \ 19328036a7fSMax Reitz | _filter_qemu_io 19428036a7fSMax Reitz 19528036a7fSMax Reitzecho 19628036a7fSMax Reitz 19728036a7fSMax Reitz# Verify that cluster #3 is not allocated (because it is the same in 19828036a7fSMax Reitz# $BASE_OLD and $BASE_NEW) 19928036a7fSMax Reitz$QEMU_IMG map "$OVERLAY" | _filter_qemu_img_map 20028036a7fSMax Reitz 201939e1640SKevin Wolf 202939e1640SKevin Wolf# success, all done 203939e1640SKevin Wolfecho "*** done" 204939e1640SKevin Wolfrm -f $seq.full 205939e1640SKevin Wolfstatus=0 206