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 https://opensource.org/licenses/CDDL-1.0.
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
28#
29# Copyright (c) 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33
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
47verify_runnable "global"
48
49function cleanup
50{
51	typeset -i i=0
52	while (( i < ${#mntp_fs[*]} )); do
53		log_must zfs set sharenfs=off ${mntp_fs[((i+1))]}
54
55		((i = i + 2))
56	done
57
58	if mounted $TESTPOOL/$TESTCLONE; then
59		log_must zfs unmount $TESTDIR2
60	fi
61
62	[[ -d $TESTDIR2 ]] && \
63		log_must rm -rf $TESTDIR2
64
65	datasetexists "$TESTPOOL/$TESTCLONE" && \
66		destroy_dataset $TESTPOOL/$TESTCLONE -f
67
68	snapexists "$TESTPOOL/$TESTFS2@snapshot" && \
69		destroy_dataset $TESTPOOL/$TESTFS2@snapshot -f
70
71	datasetexists "$TESTPOOL/$TESTFS2" && \
72		destroy_dataset $TESTPOOL/$TESTFS2 -f
73}
74
75#
76# Main test routine.
77#
78# Given a mountpoint and file system this routine will attempt
79# unshare the filesystem via <filesystem|mountpoint> argument
80# and then verify it has been unshared.
81#
82function test_unshare # <mntp> <filesystem>
83{
84        typeset mntp=$1
85        typeset filesystem=$2
86	typeset prop_value
87
88	prop_value=$(get_prop "sharenfs" $filesystem)
89
90	if [[ $prop_value == "off" ]]; then
91		not_shared $mntp ||
92			log_must eval "unshare_nfs $mntp"
93		log_must zfs set sharenfs=on $filesystem
94		is_shared $mntp || \
95			log_fail "'zfs set sharenfs=on' fails to make" \
96				"file system $filesystem shared."
97	fi
98
99	is_shared $mntp || log_must zfs share $filesystem
100
101        #
102	# Verify 'zfs unshare <filesystem>' works as well.
103	#
104	log_must zfs unshare $filesystem
105	not_shared $mntp || log_fail "'zfs unshare <filesystem>' fails"
106
107	log_must zfs share $filesystem
108
109	log_must zfs unshare $mntp
110	not_shared $mntp || log_fail "'zfs unshare <mountpoint>' fails"
111
112        log_note "Unsharing an unshared file system fails."
113        log_mustnot zfs unshare $filesystem
114	log_mustnot zfs unshare $mntp
115}
116
117set -A mntp_fs \
118    "$TESTDIR" "$TESTPOOL/$TESTFS" \
119    "$TESTDIR1" "$TESTPOOL/$TESTCTR/$TESTFS1" \
120    "$TESTDIR2" "$TESTPOOL/$TESTCLONE"
121
122log_assert "Verify that 'zfs unshare [-a] <filesystem|mountpoint>' succeeds as root."
123log_onexit cleanup
124
125log_must zfs create $TESTPOOL/$TESTFS2
126log_must zfs snapshot $TESTPOOL/$TESTFS2@snapshot
127log_must zfs clone $TESTPOOL/$TESTFS2@snapshot $TESTPOOL/$TESTCLONE
128log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL/$TESTCLONE
129
130#
131# Invoke 'test_unshare' routine to test 'zfs unshare <filesystem|mountpoint>'.
132#
133typeset -i i=0
134while (( i < ${#mntp_fs[*]} )); do
135	test_unshare ${mntp_fs[i]} ${mntp_fs[((i + 1 ))]}
136
137	((i = i + 2))
138done
139
140log_note "Verify 'zfs unshare -a' succeeds as root."
141
142i=0
143typeset sharenfs_val
144while (( i < ${#mntp_fs[*]} )); do
145	sharenfs_val=$(get_prop "sharenfs" ${mntp_fs[((i+1))]})
146	if [[ $sharenfs_val == "on" ]]; then
147		not_shared ${mntp_fs[i]} && \
148			log_must zfs share ${mntp_fs[((i+1))]}
149	else
150		log_must zfs set sharenfs=on ${mntp_fs[((i+1))]}
151		is_shared ${mntp_fs[i]} || \
152			log_fail "'zfs set sharenfs=on' fails to share filesystem: ${mntp_fs[i]} not shared."
153	fi
154
155        ((i = i + 2))
156done
157
158#
159# test 'zfs unshare -a '
160#
161log_must zfs unshare -a
162
163#
164# verify all shared filesystems become unshared
165#
166i=0
167while (( i < ${#mntp_fs[*]} )); do
168        not_shared ${mntp_fs[i]} || \
169                log_fail "'zfs unshare -a' fails to unshare all shared zfs filesystems: ${mntp_fs[i]} still shared."
170
171        ((i = i + 2))
172done
173
174log_pass "'zfs unshare [-a] <filesystem|mountpoint>' succeeds as root."
175