xref: /titanic_50/usr/src/test/zfs-tests/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.kshlib (revision c2b09db8b5b01162dadf9205ddd83ccf4f7d5535)
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# 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	if poolexists $pool_name; then
97		log_must $ZPOOL destroy $pool_name
98	fi
99	for file in $pool_files; do
100		$RM -f /$TESTPOOL/$file
101	done
102}
103
104# This function does a basic sanity check on the pool by computing the
105# checksums of all files in the pool, echoing the name of the file containing
106# the checksum results.
107# $1 the name of the pool
108# $2 a flag we can use to determine when this check is being performed
109#    (ie. pre or post pool-upgrade)
110function check_pool
111{
112	typeset pool=$1
113	typeset flag=$2
114	$FIND /$pool -type f -exec $CKSUM {} + > \
115		/$TESTPOOL/pool-checksums.$pool.$flag
116	echo /$TESTPOOL/pool-checksums.$pool.$flag
117}
118
119# This function simply checks that a pool has a particular version number
120# as reported by zdb and zpool upgrade -v
121# $1 the name of the pool
122# $2 the version of the pool we expect to see
123function check_poolversion
124{
125	typeset pool=$1
126	typeset vers=$2
127	typeset actual
128
129	# check version using zdb
130	actual=$($ZDB -C $pool | $SED -n 's/^.*version: \(.*\)$/\1/p')
131	if [[ $actual != $vers ]] ; then
132		log_fail "$pool: zdb reported version $actual, expected $vers"
133	fi
134
135	# check version using zpool upgrade
136	actual=$($ZPOOL upgrade | $GREP $pool$ | \
137	    $AWK '{print $1}' | $SED -e 's/ //g')
138	if [[ $actual != $vers ]] ; then
139		log_fail "$pool: zpool reported version $actual, expected $vers"
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
150{
151	typeset min=$1
152	typeset max=$2
153	typeset rand=0
154
155	while [[ $rand -lt $min ]] ; do
156		rand=$(( $RANDOM % $max + 1))
157	done
158
159	echo $rand
160}
161