1# vim: filetype=sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/include/libtest.kshlib
28
29# This part of the test suite relies on variables being setup in the
30# zpool_upgrade.cfg script. Those variables give us details about which
31# files make up the pool, and what the pool name is.
32
33
34# A function to import a pool from files we have stored in the test suite
35# We import the pool, and create some random data in the pool.
36# $1 a version number we can use to get information about the pool
37function create_old_pool
38{
39	VERSION=$1
40	POOL_FILES=$($ENV | grep "ZPOOL_VERSION_${VERSION}_FILES"\
41		| $AWK -F= '{print $2}')
42	POOL_NAME=$($ENV|grep "ZPOOL_VERSION_${VERSION}_NAME"\
43		| $AWK -F= '{print $2}')
44
45	log_note "Creating $POOL_NAME from $POOL_FILES"
46	for pool_file in $POOL_FILES; do
47		$CP -f $STF_SUITE/tests/cli_root/zpool_upgrade/blockfiles/$pool_file.Z \
48		$TMPDIR
49		$UNCOMPRESS $TMPDIR/$pool_file.Z
50	done
51	log_must $ZPOOL import -d $TMPDIR $POOL_NAME
52
53	# Now put some random contents into the pool.
54	COUNT=0
55	while [ "$COUNT" -lt 1024 ]; do
56		$DD if=/dev/urandom of=/$POOL_NAME/random.$COUNT \
57			count=1 bs=1024 > /dev/null 2>&1
58		COUNT=$(( $COUNT + 1 ))
59	done
60}
61
62
63# A function to check the contents of a pool, upgrade it to the current version
64# and then verify that the data is consistent after upgrading. Note that we're
65# not using "zpool status -x" to see if the pool is healthy, as it's possible
66# to also upgrade faulted, or degraded pools.
67# $1 a version number we can use to get information about the pool
68function check_upgrade {
69	VERSION=$1
70	POOL_NAME=$($ENV| $GREP "ZPOOL_VERSION_${VERSION}_NAME"\
71		| $AWK -F= '{print $2}')
72	POOL_FILES=$($ENV | $GREP "ZPOOL_VERSION_${VERSION}_FILES"\
73		| $AWK -F= '{print $2}')
74
75	log_note "Checking if we can upgrade from ZFS version ${VERSION}."
76	PRE_UPGRADE_CHECKSUM=$(check_pool $POOL_NAME pre )
77	log_must $ZPOOL upgrade $POOL_NAME > /dev/null
78	POST_UPGRADE_CHECKSUM=$(check_pool $POOL_NAME post )
79
80	log_note "Checking that there are no differences between checksum output"
81	log_must $DIFF $PRE_UPGRADE_CHECKSUM $POST_UPGRADE_CHECKSUM
82	$RM $PRE_UPGRADE_CHECKSUM $POST_UPGRADE_CHECKSUM
83}
84
85# A function to destroy an upgraded pool, plus the files it was based on.
86# $1 a version number we can use to get information about the pool
87function destroy_upgraded_pool {
88	VERSION=$1
89	POOL_NAME=$($ENV|grep "ZPOOL_VERSION_${VERSION}_NAME"\
90		| $AWK -F= '{print $2}')
91	POOL_FILES=$($ENV | grep "ZPOOL_VERSION_${VERSION}_FILES"\
92		| $AWK -F= '{print $2}')
93	if poolexists "$POOL_NAME"; then
94		log_must $ZPOOL destroy $POOL_NAME
95	fi
96	for file in $POOL_FILES; do
97		if [ -e "$TMPDIR/$file" ]; then
98			$RM $TMPDIR/$file
99		fi
100	done
101}
102
103# This function does a basic sanity check on the pool by computing the
104# checksums of all files in the pool, printing the name of the file containing
105# the checksum results.
106# $1 the name of the pool
107# $2 a flag we can use to determine when this check is being performed
108#    (ie. pre or post pool-upgrade)
109function check_pool { # pool state
110	POOL=$1
111	STATE=$2
112	$FIND /$POOL -type f -exec $CKSUM {} + > \
113		$TMPDIR/pool-checksums.$POOL.$STATE
114	print $TMPDIR/pool-checksums.$POOL.$STATE
115}
116
117# This function simply checks that a pool has a particular version number
118# as reported by zdb and zpool upgrade -v
119# $1 the name of the pool
120# $2 the version of the pool we expect to see
121function check_poolversion { # pool version
122
123	POOL=$1
124	VERSION=$2
125
126	# check version using zdb
127	ACTUAL=$(get_config $POOL version)
128	[ "$ACTUAL" != "$VERSION" ] && log_fail \
129		"ERROR: $POOL not upgraded: wanted '$VERSION', got '$ACTUAL'"
130
131	# check version using zpool upgrade
132	ACTUAL=$($ZPOOL upgrade | $GREP $POOL$ | \
133	 $AWK '{print $1}' | $SED -e 's/ //g')
134	[ "$ACTUAL" != "$VERSION" ] &&
135		log_fail "$POOL reported version '$ACTUAL', expected '$VERSION'"
136}
137
138# A simple function to get a random number between two bounds
139# probably not the most efficient for large ranges, but it's okay.
140# Note since we're using $RANDOM, 32767 is the largest number we
141# can accept as the upper bound.
142# $1 lower bound
143# $2 upper bound
144function random { # min max
145
146	typeset MIN=$1
147	typeset MAX=$2
148	typeset RAND=0
149
150	while [ "$RAND" -lt "$MIN" ]
151	do
152		RAND=$(( $RANDOM % $MAX + 1))
153	done
154
155	print $RAND
156}
157
158