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. $STF_SUITE/include/libtest.kshlib
28
29#################################################################################
30#
31# __stc_assertion_start
32#
33# ID: zfs_unshare_001_pos
34#
35# DESCRIPTION:
36# Verify that 'zfs unshare <filesystem|mountpoint>' unshares a given shared
37# filesystem.
38#
39# STRATEGY:
40# 1. Share filesystems
41# 2. Invoke 'zfs unshare <filesystem|mountpoint>' to unshare zfs file system
42# 3. Verify that the file system is unshared
43# 4. Verify that unsharing an unshared file system fails
44# 5. Verify that "zfs unshare -a" succeeds to unshare all zfs file systems.
45#
46# TESTABILITY: explicit
47#
48# TEST_AUTOMATION_LEVEL: automated
49#
50# CODING_STATUS: COMPLETED (2005-07-18)
51#
52# __stc_assertion_end
53#
54################################################################################
55
56verify_runnable "global"
57
58function cleanup
59{
60	typeset -i i=0
61	while (( i < ${#mntp_fs[*]} )); do
62		log_must $ZFS set sharenfs=off ${mntp_fs[((i+1))]}
63
64		((i = i + 2))
65	done
66
67	if mounted $TESTPOOL/$TESTCLONE; then
68		log_must $ZFS unmount $TESTDIR2
69	fi
70
71	[[ -d $TESTDIR2 ]] && \
72		log_must $RM -rf $TESTDIR2
73
74	if datasetexists "$TESTPOOL/$TESTCLONE"; then
75		log_must $ZFS destroy -f $TESTPOOL/$TESTCLONE
76	fi
77
78	if snapexists "$TESTPOOL/$TESTFS2@snapshot"; then
79		log_must $ZFS destroy -f $TESTPOOL/$TESTFS2@snapshot
80	fi
81
82	if datasetexists "$TESTPOOL/$TESTFS2"; then
83		log_must $ZFS destroy -f $TESTPOOL/$TESTFS2
84	fi
85}
86
87#
88# Main test routine.
89#
90# Given a mountpoint and file system this routine will attempt
91# unshare the filesystem via <filesystem|mountpoint> argument
92# and then verify it has been unshared.
93#
94function test_unshare # <mntp> <filesystem>
95{
96        typeset mntp=$1
97        typeset filesystem=$2
98	typeset prop_value
99
100	prop_value=$(get_prop "sharenfs" $filesystem)
101
102	if [[ $prop_value == "off" ]]; then
103		not_shared $mntp ||
104			log_must $UNSHARE -F nfs $mntp
105		log_must $ZFS set sharenfs=on $filesystem
106		is_shared $mntp || \
107			log_fail "'$ZFS set sharenfs=on' fails to make" \
108				"file system $filesystem shared."
109	fi
110
111       	is_shared $mntp || \
112		log_must $ZFS share $filesystem
113
114        #
115       	# Verify 'zfs unshare <filesystem>' works as well.
116       	#
117       	log_must $ZFS unshare $filesystem
118	not_shared $mntp || \
119		log_fail "'zfs unshare <filesystem>' fails"
120
121       	log_must $ZFS share $filesystem
122
123       	log_must $ZFS unshare $mntp
124 	not_shared $mntp || \
125		log_fail "'zfs unshare <mountpoint>' fails"
126
127        log_note "Unsharing an unshared file system fails."
128        log_mustnot $ZFS unshare $filesystem
129	log_mustnot $ZFS unshare $mntp
130}
131
132
133set -A mntp_fs \
134    "$TESTDIR" 	"$TESTPOOL/$TESTFS" \
135    "$TESTDIR1" "$TESTPOOL/$TESTCTR/$TESTFS1" \
136    "$TESTDIR2"	"$TESTPOOL/$TESTCLONE"
137
138log_assert "Verify that 'zfs unshare [-a] <filesystem|mountpoint>' succeeds as root."
139log_onexit cleanup
140
141log_must $ZFS create $TESTPOOL/$TESTFS2
142log_must $ZFS snapshot $TESTPOOL/$TESTFS2@snapshot
143log_must $ZFS clone $TESTPOOL/$TESTFS2@snapshot $TESTPOOL/$TESTCLONE
144log_must $ZFS set mountpoint=$TESTDIR2 $TESTPOOL/$TESTCLONE
145
146#
147# Invoke 'test_unshare' routine to test 'zfs unshare <filesystem|mountpoint>'.
148#
149typeset -i i=0
150while (( i < ${#mntp_fs[*]} )); do
151	test_unshare ${mntp_fs[i]} ${mntp_fs[((i + 1 ))]}
152
153	((i = i + 2))
154done
155
156log_note "Verify '$ZFS unshare -a' succeds as root."
157
158i=0
159typeset sharenfs_val
160while (( i < ${#mntp_fs[*]} )); do
161	sharenfs_val=$(get_prop "sharenfs" ${mntp_fs[((i+1))]})
162	if [[ $sharenfs_val == "on" ]]; then
163        	not_shared ${mntp_fs[i]} && \
164			log_must $ZFS share ${mntp_fs[((i+1))]}
165	else
166		log_must $ZFS set sharenfs=on ${mntp_fs[((i+1))]}
167		is_shared ${mntp_fs[i]} || \
168			log_fail "'$ZFS set sharenfs=on' fails to share filesystem."
169	fi
170
171        ((i = i + 2))
172done
173
174#
175# test 'zfs unshare -a '
176#
177log_must $ZFS unshare -a
178
179#
180# verify all shared filesystems become unshared
181#
182i=0
183while (( i < ${#mntp_fs[*]} )); do
184        not_shared ${mntp_fs[i]} || \
185                log_fail "'$ZFS unshare -a' fails to unshare all shared zfs filesystems."
186
187        ((i = i + 2))
188done
189
190log_pass "'$ZFS unshare [-a] <filesystem|mountpoint>' succeeds as root."
191