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