1#!/usr/local/bin/ksh93 -p
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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26. $STF_SUITE/include/libtest.kshlib
27. $STF_SUITE/tests/cli_root/zfs_mount/zfs_mount.kshlib
28
29################################################################################
30#
31# __stc_assertion_start
32#
33# ID: zpool_import_rename_001_pos
34#
35# DESCRIPTION:
36# An exported pool can be imported under a different name. Hence
37# we test that a previously exported pool can be renamed.
38#
39# STRATEGY:
40#	1. Copy a file into the default test directory.
41#	2. Umount the default directory.
42#	3. Export the pool.
43#	4. Import the pool using the name ${TESTPOOL}-new,
44#	   and using the various combinations.
45#               - Regular import
46#               - Alternate Root Specified
47#	5. Verify it exists in the 'zpool list' output.
48#	6. Verify the default file system is mounted and that the file
49#	   from step (1) is present.
50#
51# TESTABILITY: explicit
52#
53# TEST_AUTOMATION_LEVEL: automated
54#
55# CODING_STATUS: COMPLETED (2005-07-04)
56#
57# __stc_assertion_end
58#
59################################################################################
60
61verify_runnable "global"
62
63set -A pools "$TESTPOOL" "$TESTPOOL1"
64set -A devs "" "-d $DEVICE_DIR"
65set -A options "" "-R $ALTER_ROOT"
66set -A mtpts "$TESTDIR" "$TESTDIR1"
67
68
69function cleanup
70{
71	typeset -i i=0
72	while (( i < ${#pools[*]} )); do
73		if poolexists "${pools[i]}-new" ; then
74			log_must $ZPOOL export "${pools[i]}-new"
75
76			[[ -d /${pools[i]}-new ]] && \
77				log_must $RM -rf /${pools[i]}-new
78
79			log_must $ZPOOL import ${devs[i]} \
80				"${pools[i]}-new" ${pools[i]}
81		fi
82
83		datasetexists "${pools[i]}" || \
84			log_must $ZPOOL import ${devs[i]} ${pools[i]}
85
86		ismounted "${pools[i]}/$TESTFS" || \
87			log_must $ZFS mount ${pools[i]}/$TESTFS
88
89		[[ -e ${mtpts[i]}/$TESTFILE0 ]] && \
90			log_must $RM -rf ${mtpts[i]}/$TESTFILE0
91
92		((i = i + 1))
93
94	done
95
96	cleanup_filesystem $TESTPOOL1 $TESTFS $TESTDIR1
97
98	destroy_pool $TESTPOOL1
99
100	[[ -d $ALTER_ROOT ]] && \
101		log_must $RM -rf $ALTER_ROOT
102}
103
104function perform_inner_test
105{
106	target=$1
107
108	log_must $ZPOOL import ${devs[i]} ${options[j]} \
109		$target ${pools[i]}-new
110
111	log_must poolexists "${pools[i]}-new"
112
113	log_must ismounted ${pools[i]}-new/$TESTFS
114
115	basedir=${mtpts[i]}
116	[[ -n ${options[j]} ]] && \
117		basedir=$ALTER_ROOT/${mtpts[i]}
118
119	[[ ! -e $basedir/$TESTFILE0 ]] && \
120		log_fail "$basedir/$TESTFILE0 missing after import."
121
122	checksum2=$($SUM $basedir/$TESTFILE0 | $AWK '{print $1}')
123	[[ "$checksum1" != "$checksum2" ]] && \
124		log_fail "Checksums differ ($checksum1 != $checksum2)"
125
126	log_must $ZPOOL export "${pools[i]}-new"
127
128	[[ -d /${pools[i]}-new ]] && \
129		log_must $RM -rf /${pools[i]}-new
130
131	target=${pools[i]}-new
132	if (( RANDOM % 2 == 0 )) ; then
133		target=$guid
134	fi
135	log_must $ZPOOL import ${devs[i]} $target ${pools[i]}
136}
137
138log_onexit cleanup
139
140log_assert "Verify that an imported pool can be renamed."
141
142setup_filesystem "$DEVICE_FILES" $TESTPOOL1 $TESTFS $TESTDIR1
143checksum1=$($SUM $MYTESTFILE | $AWK '{print $1}')
144
145typeset -i i=0
146typeset -i j=0
147typeset basedir
148
149while (( i < ${#pools[*]} )); do
150	guid=$(get_config ${pools[i]} pool_guid)
151	log_must $CP $MYTESTFILE ${mtpts[i]}/$TESTFILE0
152
153	log_must $ZFS umount ${mtpts[i]}
154
155	j=0
156	while (( j <  ${#options[*]} )); do
157		log_must $ZPOOL export ${pools[i]}
158
159		[[ -d /${pools[i]} ]] && \
160			log_must $RM -rf /${pools[i]}
161
162		log_note "Testing import by name."
163		perform_inner_test ${pools[i]}
164
165		log_must $ZPOOL export ${pools[i]}
166
167		log_note "Testing import by GUID."
168		perform_inner_test $guid
169
170		((j = j + 1))
171	done
172
173	((i = i + 1))
174done
175
176log_pass "Successfully imported and renamed a ZPOOL"
177