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 [-a] <filesystem|mountpoint>' is aware of legacy share.
37#
38# STRATEGY:
39# 1. Set 'zfs set sharenfs=off'
40# 2. Use 'share' to share given filesystem
41# 3. Verify that 'zfs unshare <filesystem|mountpoint>' is aware of legacy share
42# 4. Verify that 'zfs unshare -a' is aware of legacy share.
43#
44
45verify_runnable "global"
46
47if is_linux; then
48	log_unsupported "zfs set sharenfs=off won't unshare if already off"
49fi
50
51function cleanup
52{
53	typeset -i i=0
54	while (( i < ${#mntp_fs[*]} )); do
55		is_shared ${mntp_fs[i]} && \
56			log_must eval "unshare_nfs ${mntp_fs[i]}"
57
58		((i = i + 2))
59	done
60
61	if mounted $TESTPOOL/$TESTCLONE; then
62		log_must zfs unmount $TESTDIR2
63	fi
64
65	[[ -d $TESTDIR2 ]] && \
66		log_must rm -rf $TESTDIR2
67
68	datasetexists "$TESTPOOL/$TESTCLONE" && \
69		destroy_dataset $TESTPOOL/$TESTCLONE -f
70
71	snapexists "$TESTPOOL/$TESTFS2@snapshot" && \
72		destroy_dataset $TESTPOOL/$TESTFS2@snapshot -f
73
74	datasetexists "$TESTPOOL/$TESTFS2" && \
75		destroy_dataset $TESTPOOL/$TESTFS2 -f
76}
77
78#
79# Main test routine.
80#
81# Given a mountpoint and file system this routine will attempt
82# to verify 'zfs unshare' is aware of legacy share.
83#
84function test_legacy_unshare # <mntp> <filesystem>
85{
86        typeset mntp=$1
87        typeset filesystem=$2
88
89	log_must zfs set sharenfs=off $filesystem
90	not_shared $mntp || \
91	    log_fail "'zfs set sharenfs=off' fails to make ZFS " \
92	    "filesystem $filesystem unshared."
93
94	log_must share_nfs $mntp
95	is_shared $mntp || \
96	    log_fail "'share' command fails to share ZFS file system."
97	#
98	# Verify 'zfs unshare <filesystem>' is aware of legacy share.
99	#
100	log_mustnot zfs unshare $filesystem
101	is_shared $mntp || \
102	    log_fail "'zfs unshare <filesystem>' fails to be aware" \
103	    "of legacy share."
104
105	#
106	# Verify 'zfs unshare <filesystem>' is aware of legacy share.
107	#
108	log_mustnot zfs unshare $mntp
109	is_shared $mntp || \
110	    log_fail "'zfs unshare <mountpoint>' fails to be aware" \
111	    "of legacy share."
112}
113
114
115set -A mntp_fs \
116    "$TESTDIR" "$TESTPOOL/$TESTFS" \
117    "$TESTDIR1" "$TESTPOOL/$TESTCTR/$TESTFS1" \
118    "$TESTDIR2" "$TESTPOOL/$TESTCLONE"
119
120log_assert "Verify that 'zfs unshare [-a]' is aware of legacy share."
121log_onexit cleanup
122
123log_must zfs create $TESTPOOL/$TESTFS2
124log_must zfs snapshot $TESTPOOL/$TESTFS2@snapshot
125log_must zfs clone $TESTPOOL/$TESTFS2@snapshot $TESTPOOL/$TESTCLONE
126log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL/$TESTCLONE
127
128#
129# Invoke 'test_legacy_unshare' routine to verify.
130#
131typeset -i i=0
132while (( i < ${#mntp_fs[*]} )); do
133	test_legacy_unshare ${mntp_fs[i]} ${mntp_fs[((i + 1 ))]}
134
135	((i = i + 2))
136done
137
138
139log_note "Verify 'zfs unshare -a' is aware of legacy share."
140
141#
142# set the 'sharenfs' property to 'off' for each filesystem
143#
144i=0
145while (( i < ${#mntp_fs[*]} )); do
146        log_must zfs set sharenfs=off ${mntp_fs[((i + 1))]}
147        not_shared ${mntp_fs[i]} || \
148                log_fail "'zfs set sharenfs=off' unshares file system failed."
149
150        ((i = i + 2))
151done
152
153#
154# Share each of the file systems via legacy share.
155#
156i=0
157while (( i < ${#mntp_fs[*]} )); do
158        share_nfs ${mntp_fs[i]}
159        is_shared ${mntp_fs[i]} || \
160                log_fail "'share' shares ZFS filesystem failed."
161
162        ((i = i + 2))
163done
164
165#
166# Verify that 'zfs unshare -a' is aware of legacy share
167#
168log_must zfs unshare -a
169
170#
171# verify ZFS filesystems are still shared
172#
173i=0
174while (( i < ${#mntp_fs[*]} )); do
175        is_shared ${mntp_fs[i]} || \
176            log_fail "'zfs  unshare -a' fails to be aware of legacy share."
177
178        ((i = i + 2))
179done
180
181log_pass "'zfs unshare [-a]' succeeds to be aware of legacy share."
182