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#
27# ident	"@(#)zpool_import_002_pos.ksh	1.3	07/10/09 SMI"
28#
29. $STF_SUITE/include/libtest.kshlib
30. $STF_SUITE/tests/cli_root/zfs_mount/zfs_mount.kshlib
31
32################################################################################
33#
34# __stc_assertion_start
35#
36# ID: zpool_import_002_pos
37#
38# DESCRIPTION:
39# Verify that an exported pool cannot be imported
40# more than once.
41#
42# STRATEGY:
43#	1. Populate the default test directory and unmount it.
44#	2. Export the default test pool.
45#	3. Import it using the various combinations.
46#		- Regular import
47#		- Alternate Root Specified
48#	4. Verify it shows up under 'zpool list'.
49#	5. Verify it contains a file.
50#	6. Attempt to import it for a second time. Verify this fails.
51#
52# TESTABILITY: explicit
53#
54# TEST_AUTOMATION_LEVEL: automated
55#
56# CODING_STATUS: COMPLETED (2005-07-04)
57#
58# __stc_assertion_end
59#
60################################################################################
61
62verify_runnable "global"
63
64set -A pools "$TESTPOOL" "$TESTPOOL1"
65set -A devs "" "-d $DEVICE_DIR"
66set -A options "" "-R $ALTER_ROOT"
67set -A mtpts "$TESTDIR" "$TESTDIR1"
68
69
70function cleanup
71{
72	typeset -i i=0
73	while (( i < ${#pools[*]} )); do
74		poolexists ${pools[i]} && \
75			log_must $ZPOOL export ${pools[i]}
76
77		datasetexists "${pools[i]}/$TESTFS" || \
78			log_must $ZPOOL import ${devs[i]} ${pools[i]}
79
80		ismounted "${pools[i]}/$TESTFS" || \
81			log_must $ZFS mount ${pools[i]}/$TESTFS
82
83		[[ -e ${mtpts[i]}/$TESTFILE0 ]] && \
84			log_must $RM -rf ${mtpts[i]}/$TESTFILE0
85
86		((i = i + 1))
87	done
88
89	cleanup_filesystem $TESTPOOL1 $TESTFS
90
91	destroy_pool $TESTPOOL1
92
93	[[ -d $ALTER_ROOT ]] && \
94		log_must $RM -rf $ALTER_ROOT
95}
96
97log_onexit cleanup
98
99log_assert "Verify that an exported pool cannot be imported more than once."
100
101setup_filesystem "$DEVICE_FILES" $TESTPOOL1 $TESTFS $TESTDIR1
102
103checksum1=$($SUM $MYTESTFILE | $AWK '{print $1}')
104
105typeset -i i=0
106typeset -i j=0
107typeset basedir
108
109function inner_test
110{
111	typeset pool=$1
112	typeset target=$2
113	typeset devs=$3
114	typeset opts=$4
115	typeset mtpt=$5
116
117	log_must $ZPOOL import ${devs} ${opts} $target
118	log_must poolexists $pool
119	log_must ismounted $pool/$TESTFS
120
121	basedir=$mtpt
122	[ -n "$opts" ] && basedir="$ALTER_ROOT/$mtpt"
123
124	[ ! -e "$basedir/$TESTFILE0" ] && \
125		log_fail "ERROR: $basedir/$TESTFILE0 missing after import."
126
127	checksum2=$($SUM $basedir/$TESTFILE0 | $AWK '{print $1}')
128	[[ "$checksum1" != "$checksum2" ]] && \
129		log_fail "ERROR: Checksums differ ($checksum1 != $checksum2)"
130
131	log_mustnot $ZPOOL import $devs $target
132}
133
134while (( i < ${#pools[*]} )); do
135	log_must $CP $MYTESTFILE ${mtpts[i]}/$TESTFILE0
136
137	log_must $ZFS umount ${mtpts[i]}
138
139	j=0
140	while (( j <  ${#options[*]} )); do
141		typeset pool=${pools[i]}
142		typeset vdevdir=""
143
144		log_must $ZPOOL export $pool
145
146		[ "$pool" = "$TESTPOOL1" ] && vdevdir="$DEVICE_DIR"
147		guid=$(get_config $pool pool_guid $vdevdir)
148		log_must test -n "$guid"
149		log_note "Importing '$pool' by guid '$guid'"
150		inner_test $pool $guid "${devs[i]}" "${options[j]}" ${mtpts[i]}
151
152		log_must $ZPOOL export $pool
153
154		log_note "Importing '$pool' by name."
155		inner_test $pool $pool "${devs[i]}" "${options[j]}" ${mtpts[i]}
156
157		((j = j + 1))
158	done
159
160	((i = i + 1))
161
162done
163
164log_pass "Able to import exported pools and import only once."
165