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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2016, 2020 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33
34#
35# DESCRIPTION:
36# Verify that 'zfs set sharenfs' and 'zfs share' shares a given dataset.
37#
38# STRATEGY:
39# 1. Invoke 'zfs set sharenfs'.
40# 2. Verify that the file system is shared.
41# 3. Invoke 'zfs share'.
42# 4. Verify that the file system is shared.
43# 5. Verify that a shared filesystem cannot be shared again.
44# 6. Verify that share -a succeeds.
45#
46
47verify_runnable "global"
48
49set -A fs \
50    "$TESTDIR1" "$TESTPOOL/$TESTCTR/$TESTFS1" \
51    "$TESTDIR2" "$TESTPOOL/$TESTFS-clone" \
52    "$TESTDIR" "$TESTPOOL/$TESTFS"
53
54function cleanup
55{
56	typeset -i i=0
57	while (( i < ${#fs[*]} )); do
58		log_must zfs set sharenfs=off ${fs[((i+1))]}
59		unshare_fs ${fs[i]}
60
61		((i = i + 2))
62	done
63
64	if mounted $TESTPOOL/$TESTFS-clone; then
65		log_must zfs unmount $TESTDIR2
66	fi
67
68	datasetexists $TESTPOOL/$TESTFS-clone && \
69		log_must zfs destroy -f $TESTPOOL/$TESTFS-clone
70
71	if snapexists "$TESTPOOL/$TESTFS@snapshot"; then
72		log_must zfs destroy -f $TESTPOOL/$TESTFS@snapshot
73	fi
74
75	log_must zfs share -a
76}
77
78
79#
80# Main test routine.
81#
82# Given a mountpoint and file system this routine will attempt
83# share the mountpoint and then verify it has been shared.
84#
85function test_share # mntp filesystem
86{
87	typeset mntp=$1
88	typeset filesystem=$2
89
90	not_shared $mntp || \
91	    log_fail "File system $filesystem is already shared."
92
93	log_must zfs set sharenfs=on $filesystem
94	is_shared $mntp || \
95	    log_fail "File system $filesystem is not shared (set sharenfs)."
96
97	#
98	# Verify 'zfs share' works as well.
99	#
100	log_must zfs unshare $filesystem
101	is_shared $mntp && \
102	    log_fail "File system $filesystem is still shared."
103
104	log_must zfs share $filesystem
105	is_shared $mntp || \
106	    log_fail "file system $filesystem is not shared (zfs share)."
107
108	log_note "Sharing a shared file system fails."
109	log_mustnot zfs share $filesystem
110}
111
112log_assert "Verify that 'zfs share' succeeds as root."
113log_onexit cleanup
114
115log_must zfs snapshot $TESTPOOL/$TESTFS@snapshot
116log_must zfs clone $TESTPOOL/$TESTFS@snapshot $TESTPOOL/$TESTFS-clone
117log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL/$TESTFS-clone
118
119typeset -i i=0
120while (( i < ${#fs[*]} )); do
121	test_share ${fs[i]} ${fs[((i + 1))]}
122
123	((i = i + 2))
124done
125
126log_note "Verify 'zfs share -a' succeeds."
127
128#
129# Unshare each of the file systems.
130#
131i=0
132while (( i < ${#fs[*]} )); do
133	unshare_fs ${fs[i]}
134
135	((i = i + 2))
136done
137
138#
139# Try a zfs share -a and verify all file systems are shared.
140#
141log_must zfs share -a
142
143#
144# We need to unset __ZFS_POOL_EXCLUDE so that we include all file systems
145# in the os-specific zfs exports file. This will be reset by the next test.
146#
147unset __ZFS_POOL_EXCLUDE
148
149i=0
150while (( i < ${#fs[*]} )); do
151	is_shared ${fs[i]} || \
152	    log_fail "File system ${fs[i]} is not shared (share -a)"
153
154	is_exported ${fs[i]} || \
155	    log_fail "File system ${fs[i]} is not exported (share -a)"
156
157	((i = i + 2))
158done
159
160log_pass "'zfs share [ -a ] <filesystem>' succeeds as root."
161