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