1#!/bin/ksh -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
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_mount/zfs_mount.kshlib
34. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.cfg
35
36#
37# DESCRIPTION:
38# Once a pool has been exported, it should be recreated after a
39# successful import, all the sub-filesystems within it should all be restored,
40# include mount & share status. Verify that is true.
41#
42# STRATEGY:
43#	1. Create the test pool and hierarchical filesystems.
44#	2. Export the test pool, or destroy the test pool,
45#		depend on testing import [-Df].
46#	3. Import it using the various combinations.
47#		- Regular import
48#		- Alternate Root Specified
49#	4. Verify the mount & share status is restored.
50#
51
52verify_runnable "global"
53
54set -A pools "$TESTPOOL" "$TESTPOOL1"
55set -A devs "" "-d $DEVICE_DIR"
56set -A options "" "-R $ALTER_ROOT"
57set -A mtpts "$TESTDIR" "$TESTDIR1"
58
59
60function cleanup
61{
62	typeset -i i=0
63
64	while ((i < ${#pools[*]})); do
65		if poolexists ${pools[i]}; then
66			log_must zpool export ${pools[i]}
67			log_note "Try to import ${devs[i]} ${pools[i]}"
68			zpool import ${devs[i]} ${pools[i]}
69		else
70			log_note "Try to import $option ${devs[i]} ${pools[i]}"
71			zpool import $option ${devs[i]} ${pools[i]}
72		fi
73
74		if poolexists ${pools[i]}; then
75			is_shared ${pools[i]} && \
76			    log_must zfs set sharenfs=off ${pools[i]}
77
78			ismounted "${pools[i]}/$TESTFS" || \
79			    log_must zfs mount ${pools[i]}/$TESTFS
80		fi
81
82		((i = i + 1))
83	done
84
85	destroy_pool $TESTPOOL1
86
87	if datasetexists $TESTPOOL/$TESTFS; then
88		log_must zfs destroy -Rf $TESTPOOL/$TESTFS
89	fi
90	log_must zfs create $TESTPOOL/$TESTFS
91	log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
92
93	[[ -d $ALTER_ROOT ]] && \
94		log_must rm -rf $ALTER_ROOT
95}
96
97log_onexit cleanup
98
99log_assert "Verify all mount & share status of sub-filesystems within a pool \
100	can be restored after import [-Df]."
101
102setup_filesystem "$DEVICE_FILES" $TESTPOOL1 $TESTFS $TESTDIR1
103# create a heirarchy of filesystem
104for pool in ${pools[@]} ; do
105	log_must zfs create $pool/$TESTFS/$TESTCTR
106	log_must zfs create $pool/$TESTFS/$TESTCTR/$TESTCTR1
107	log_must zfs set canmount=off $pool/$TESTFS/$TESTCTR
108	log_must zfs set canmount=off $pool/$TESTFS/$TESTCTR/$TESTCTR1
109	log_must zfs create $pool/$TESTFS/$TESTCTR/$TESTFS1
110	log_must zfs create $pool/$TESTFS/$TESTCTR/$TESTCTR1/$TESTFS1
111	log_must zfs create $pool/$TESTFS/$TESTFS1
112	log_must zfs snapshot $pool/$TESTFS/$TESTFS1@snap
113	log_must zfs clone $pool/$TESTFS/$TESTFS1@snap $pool/$TESTCLONE1
114done
115
116typeset mount_fs="$TESTFS $TESTFS/$TESTFS1 $TESTCLONE1 \
117		$TESTFS/$TESTCTR/$TESTFS1 $TESTFS/$TESTCTR/$TESTCTR1/$TESTFS1"
118typeset nomount_fs="$TESTFS/$TESTCTR $TESTFS/$TESTCTR/$TESTCTR1"
119
120typeset -i i=0
121typeset -i j=0
122typeset -i nfs_share_bit=0
123typeset -i guid_bit=0
124typeset basedir
125
126for option in "" "-Df"; do
127	i=0
128	while ((i < ${#pools[*]})); do
129		pool=${pools[i]}
130		guid=$(get_pool_prop guid $pool)
131		j=0
132		while ((j < ${#options[*]})); do
133			# set sharenfs property off/on
134			nfs_share_bit=0
135			while ((nfs_share_bit <= 1)); do
136				typeset f_share=""
137				typeset nfs_flag="sharenfs=off"
138				if ((nfs_share_bit == 1)); then
139					log_note "Set sharenfs=on $pool"
140					log_must zfs set sharenfs=on $pool
141					log_must is_shared $pool
142					f_share="true"
143					nfs_flag="sharenfs=on"
144				fi
145				# for every off/on nfs bit import guid/pool_name
146				guid_bit=0
147				while ((guid_bit <= 1)); do
148					typeset guid_flag="pool name"
149					if [[ -z $option ]]; then
150						log_must zpool export $pool
151					else
152						log_must zpool destroy $pool
153					fi
154
155					typeset target=$pool
156					if ((guid_bit == 1)); then
157						log_note "Import by guid."
158						if [[ -z $guid ]]; then
159							log_fail "guid should "\
160							    "not be empty!"
161						else
162							target=$guid
163							guid_flag="$guid"
164						fi
165					fi
166					log_note "Import with $nfs_flag and " \
167					    "$guid_flag"
168					zpool import $option ${devs[i]} \
169					    ${options[j]} $target
170					#import by GUID if import by pool name fails
171					if [[ $? != 0 ]]; then
172						log_note "Possible pool name" \
173						    "duplicates. Try GUID import"
174						target=$guid
175						log_must zpool import $option \
176						    ${devs[i]} ${options[j]} \
177						    $target
178					fi
179					log_must poolexists $pool
180
181					for fs in $mount_fs; do
182						log_must ismounted $pool/$fs
183						[[ -n $f_share ]] && \
184						    log_must is_shared $pool/$fs
185					done
186
187					for fs in $nomount_fs; do
188						log_mustnot ismounted $pool/$fs
189						log_mustnot is_shared $pool/$fs
190					done
191					((guid_bit = guid_bit + 1))
192				done
193				# reset nfsshare=off
194				if [[ -n $f_share ]]; then
195					log_must zfs set sharenfs=off $pool
196					log_mustnot is_shared $pool
197				fi
198				((nfs_share_bit = nfs_share_bit + 1))
199			done
200
201			((j = j + 1))
202		done
203
204		((i = i + 1))
205	done
206done
207
208log_pass "All mount & share status of sub-filesystems within a pool \
209	can be restored after import [-Df]."
210