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, 2017 by Delphix. All rights reserved.
29# Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.cfg
34
35# This part of the test suite relies on variables being setup in the
36# zpool_upgrade.cfg script. Those variables give us details about which
37# files make up the pool, and what the pool name is.
38
39
40# A function to import a pool from files we have stored in the test suite
41# We import the pool, and create some random data in the pool.
42# $1 a version number we can use to get information about the pool
43function create_old_pool
44{
45	typeset vers=$1
46	typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
47	typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME
48
49	log_note "Creating $pool_name from $pool_files"
50	for pool_file in $pool_files; do
51		log_must bzcat \
52		    $STF_SUITE/tests/functional/cli_root/zpool_upgrade/blockfiles/$pool_file.bz2 \
53		    >/$TESTPOOL/$pool_file
54	done
55	log_must zpool import -d /$TESTPOOL $pool_name
56
57	# Put some random contents into the pool
58	for i in {1..1024} ; do
59		dd if=/dev/urandom of=/$pool_name/random.$i \
60		    count=1 bs=1024 > /dev/null 2>&1
61	done
62}
63
64
65# A function to check the contents of a pool, upgrade it to the current version
66# and then verify that the data is consistent after upgrading. Note that we're
67# not using "zpool status -x" to see if the pool is healthy, as it's possible
68# to also upgrade faulted, or degraded pools.
69# $1 a version number we can use to get information about the pool
70function check_upgrade
71{
72	typeset vers=$1
73	typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
74	typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME
75	typeset pre_upgrade_checksum
76	typeset post_upgrade_checksum
77
78	log_note "Checking if we can upgrade from ZFS version $vers"
79	pre_upgrade_checksum=$(check_pool $pool_name pre)
80	log_must zpool upgrade $pool_name
81	post_upgrade_checksum=$(check_pool $pool_name post)
82
83	log_note "Checking that there are no differences between checksum output"
84	log_must diff $pre_upgrade_checksum $post_upgrade_checksum
85	rm $pre_upgrade_checksum $post_upgrade_checksum
86}
87
88# A function to destroy an upgraded pool, plus the files it was based on.
89# $1 a version number we can use to get information about the pool
90function destroy_upgraded_pool
91{
92	typeset vers=$1
93	typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
94	typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME
95
96	destroy_pool $pool_name
97	for file in $pool_files; do
98		rm -f /$TESTPOOL/$file
99	done
100}
101
102# This function does a basic sanity check on the pool by computing the
103# checksums of all files in the pool, echoing the name of the file containing
104# the checksum results.
105# $1 the name of the pool
106# $2 a flag we can use to determine when this check is being performed
107#    (ie. pre or post pool-upgrade)
108function check_pool
109{
110	typeset pool=$1
111	typeset flag=$2
112	find /$pool -type f -exec cksum {} + > \
113		/$TESTPOOL/pool-checksums.$pool.$flag
114	echo /$TESTPOOL/pool-checksums.$pool.$flag
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
122{
123	typeset pool=$1
124	typeset vers=$2
125	typeset actual
126
127	# check version using zdb
128	actual=$(zdb -C $pool | sed -n 's/^.*version: \(.*\)$/\1/p')
129	if [[ $actual != $vers ]] ; then
130		log_fail "$pool: zdb reported version $actual, expected $vers"
131	fi
132
133	# check version using zpool upgrade
134	actual=$(zpool upgrade | grep $pool$ | \
135	    awk '{print $1}' | sed -e 's/ //g')
136	if [[ $actual != $vers ]] ; then
137		log_fail "$pool: zpool reported version $actual, expected $vers"
138	fi
139}
140
141# A simple function to get a random number between two bounds
142# probably not the most efficient for large ranges, but it's okay.
143# Note since we're using $RANDOM, 32767 is the largest number we
144# can accept as the upper bound.
145# $1 lower bound
146# $2 upper bound
147function random
148{
149	typeset min=$1
150	typeset max=$2
151	typeset rand=0
152
153	while [[ $rand -lt $min ]] ; do
154		rand=$(( $RANDOM % $max + 1))
155	done
156
157	echo $rand
158}
159