xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.kshlib (revision b6805bf78d2bbbeeaea8909a05623587b42d58b3)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2012 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.cfg
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=$(eval $ECHO \$ZPOOL_VERSION_${VERSION}_FILES)
46	POOL_NAME=$(eval $ECHO \$ZPOOL_VERSION_${VERSION}_NAME)
47
48	log_note "Creating $POOL_NAME from $POOL_FILES"
49	for pool_file in $POOL_FILES; do
50		log_must $BZCAT \
51		    $STF_SUITE/tests/functional/cli_root/zpool_upgrade/blockfiles/$pool_file.bz2 \
52		    >/$TESTPOOL/$pool_file
53	done
54	log_must $ZPOOL import -d /$TESTPOOL $POOL_NAME
55
56	# Now put some random contents into the pool.
57	COUNT=0
58	while [ $COUNT -lt 1024 ]; do
59		$DD if=/dev/urandom of=/$POOL_NAME/random.$COUNT \
60		count=1 bs=1024 > /dev/null 2>&1
61		COUNT=$(( $COUNT + 1 ))
62	done
63}
64
65
66# A function to check the contents of a pool, upgrade it to the current version
67# and then verify that the data is consistent after upgrading. Note that we're
68# not using "zpool status -x" to see if the pool is healthy, as it's possible
69# to also upgrade faulted, or degraded pools.
70# $1 a version number we can use to get information about the pool
71function check_upgrade {
72	VERSION=$1
73	POOL_FILES=$(eval $ECHO \$ZPOOL_VERSION_${VERSION}_FILES)
74	POOL_NAME=$(eval $ECHO \$ZPOOL_VERSION_${VERSION}_NAME)
75
76	log_note "Checking if we can upgrade from ZFS version ${VERSION}."
77	PRE_UPGRADE_CHECKSUM=$(check_pool $POOL_NAME pre )
78	log_must $ZPOOL upgrade $POOL_NAME > /dev/null
79	POST_UPGRADE_CHECKSUM=$(check_pool $POOL_NAME post )
80
81	log_note "Checking that there are no differences between checksum output"
82	log_must $DIFF $PRE_UPGRADE_CHECKSUM $POST_UPGRADE_CHECKSUM
83	$RM $PRE_UPGRADE_CHECKSUM $POST_UPGRADE_CHECKSUM
84}
85
86# A function to destroy an upgraded pool, plus the files it was based on.
87# $1 a version number we can use to get information about the pool
88function destroy_upgraded_pool {
89	VERSION=$1
90	POOL_FILES=$(eval $ECHO \$ZPOOL_VERSION_${VERSION}_FILES)
91	POOL_NAME=$(eval $ECHO \$ZPOOL_VERSION_${VERSION}_NAME)
92
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 /$TESTPOOL/$file ]; then
98			$RM /$TESTPOOL/$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, echoing 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		/$TESTPOOL/pool-checksums.$POOL.$STATE
114	echo /$TESTPOOL/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=$($ZDB -C $POOL | $SED -n 's/version: \(.*\)$/\1/p')
128
129	if [ $ACTUAL != $VERSION ]
130	then
131		log_fail "$POOL not upgraded, ver. $ACTUAL, expected $VERSION"
132	fi
133
134	# check version using zpool upgrade
135	ACTUAL=$($ZPOOL upgrade | $GREP $POOL$ | \
136	 $AWK '{print $1}' | $SED -e 's/ //g')
137	if [ $ACTUAL != $VERSION ]
138	then
139		log_fail "$POOL reported version $ACTUAL, expected $VERSION"
140	fi
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	echo $RAND
161}
162
163