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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"@(#)zpool_import_012_pos.ksh	1.4	09/05/19 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_012_pos
37#
38# DESCRIPTION:
39# Once a pool has been exported, it should be recreated after a
40# successful import, all the sub-filesystems within it should all be restored,
41# include mount & share status. Verify that is true.
42#
43# STRATEGY:
44#	1. Create the test pool and hierarchical filesystems.
45#	2. Export the test pool, or destroy the test pool,
46#		depend on testing import [-Df].
47#	3. Import it using the various combinations.
48#		- Regular import
49#		- Alternate Root Specified
50#	4. Verify the mount & share status is restored.
51#
52# TESTABILITY: explicit
53#
54# TEST_AUTOMATION_LEVEL: automated
55#
56# CODING_STATUS: COMPLETED (2006-11-01)
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
74	while (( i < ${#pools[*]} )); do
75		if poolexists ${pools[i]} ; then
76			log_must $ZPOOL export ${pools[i]}
77			log_note "Try to import ${devs[i]} ${pools[i]}"
78			$ZPOOL import ${devs[i]} ${pools[i]}
79		else
80			log_note "Try to import $option ${devs[i]} ${pools[i]}"
81			$ZPOOL import $option ${devs[i]} ${pools[i]}
82		fi
83
84		if poolexists ${pools[i]} ; then
85			is_shared ${pools[i]} && \
86				log_must $ZFS set sharenfs=off ${pools[i]}
87
88			ismounted "${pools[i]}/$TESTFS" || \
89				log_must $ZFS mount ${pools[i]}/$TESTFS
90		fi
91
92		((i = i + 1))
93	done
94
95	destroy_pool $TESTPOOL1
96
97	if datasetexists $TESTPOOL/$TESTFS ; then
98		log_must $ZFS destroy -Rf $TESTPOOL/$TESTFS
99	fi
100	log_must $ZFS create $TESTPOOL/$TESTFS
101	log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
102
103	[[ -d $ALTER_ROOT ]] && \
104		log_must $RM -rf $ALTER_ROOT
105}
106
107log_onexit cleanup
108
109log_assert "Verify all mount & share status of sub-filesystems within a pool \
110	can be restored after import [-Df]."
111
112setup_filesystem "$DEVICE_FILES" $TESTPOOL1 $TESTFS $TESTDIR1
113for pool in ${pools[@]} ; do
114	log_must $ZFS create $pool/$TESTFS/$TESTCTR
115	log_must $ZFS create $pool/$TESTFS/$TESTCTR/$TESTCTR1
116	log_must $ZFS set canmount=off $pool/$TESTFS/$TESTCTR
117	log_must $ZFS set canmount=off $pool/$TESTFS/$TESTCTR/$TESTCTR1
118	log_must $ZFS create $pool/$TESTFS/$TESTCTR/$TESTFS1
119	log_must $ZFS create $pool/$TESTFS/$TESTCTR/$TESTCTR1/$TESTFS1
120	log_must $ZFS create $pool/$TESTFS/$TESTFS1
121	log_must $ZFS snapshot $pool/$TESTFS/$TESTFS1@snap
122	log_must $ZFS clone $pool/$TESTFS/$TESTFS1@snap $pool/$TESTCLONE1
123done
124
125typeset mount_fs="$TESTFS $TESTFS/$TESTFS1 $TESTCLONE1 \
126		$TESTFS/$TESTCTR/$TESTFS1 $TESTFS/$TESTCTR/$TESTCTR1/$TESTFS1"
127typeset nomount_fs="$TESTFS/$TESTCTR $TESTFS/$TESTCTR/$TESTCTR1"
128
129typeset -i i=0
130typeset -i j=0
131typeset basedir
132
133for option in "" "-Df" ; do
134	i=0
135	while (( i < ${#pools[*]} )); do
136		pool=${pools[i]}
137		guid=$(get_config $pool pool_guid)
138		j=0
139		while (( j < ${#options[*]} )); do
140			typeset f_share=""
141			if ((RANDOM % 2 == 0)); then
142				log_note "Set sharenfs=on $pool"
143				log_must $ZFS set sharenfs=on $pool
144				log_must is_shared $pool
145				f_share="true"
146			fi
147
148			if [[ -z $option ]]; then
149				log_must $ZPOOL export $pool
150			else
151				log_must $ZPOOL destroy $pool
152			fi
153
154			typeset target=$pool
155			if (( RANDOM % 2 == 0 )) ; then
156				log_note "Import by guid."
157				if [[ -z $guid ]]; then
158					log_fail "guid should not be empty!"
159				else
160					target=$guid
161				fi
162			fi
163			log_must $ZPOOL import $option \
164				${devs[i]} ${options[j]} $target
165
166			log_must poolexists $pool
167
168			for fs in $mount_fs ; do
169				log_must ismounted $pool/$fs
170				[[ -n $f_share ]] && \
171					log_must is_shared $pool/$fs
172			done
173
174			for fs in $nomount_fs ; do
175				log_mustnot ismounted $pool/$fs
176				log_mustnot is_shared $pool/$fs
177			done
178
179			if [[ -n $f_share ]] ; then
180				log_must $ZFS set sharenfs=off $pool
181				log_mustnot is_shared $pool
182			fi
183
184			((j = j + 1))
185		done
186
187		((i = i + 1))
188	done
189done
190
191log_pass "All mount & share status of sub-filesystems within a pool \
192	can be restored after import [-Df]."
193