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 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    "$TESTDIR" "$TESTPOOL/$TESTFS" \
51    "$TESTDIR1" "$TESTPOOL/$TESTCTR/$TESTFS1" \
52    "$TESTDIR2" "$TESTPOOL/$TESTFS-clone"
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
76
77#
78# Main test routine.
79#
80# Given a mountpoint and file system this routine will attempt
81# share the mountpoint and then verify it has been shared.
82#
83function test_share # mntp filesystem
84{
85	typeset mntp=$1
86	typeset filesystem=$2
87
88	not_shared $mntp || \
89	    log_fail "File system $filesystem is already shared."
90
91	log_must zfs set sharenfs=on $filesystem
92	is_shared $mntp || \
93	    log_fail "File system $filesystem is not shared (set sharenfs)."
94
95	#
96	# Verify 'zfs share' works as well.
97	#
98	log_must zfs unshare $filesystem
99	is_shared $mntp && \
100	    log_fail "File system $filesystem is still shared."
101
102	log_must zfs share $filesystem
103	is_shared $mntp || \
104	    log_fail "file system $filesystem is not shared (zfs share)."
105
106	log_note "Sharing a shared file system fails."
107	log_mustnot zfs share $filesystem
108}
109
110log_assert "Verify that 'zfs share' succeeds as root."
111log_onexit cleanup
112
113log_must zfs snapshot $TESTPOOL/$TESTFS@snapshot
114log_must zfs clone $TESTPOOL/$TESTFS@snapshot $TESTPOOL/$TESTFS-clone
115log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL/$TESTFS-clone
116
117typeset -i i=0
118while (( i < ${#fs[*]} )); do
119	test_share ${fs[i]} ${fs[((i + 1))]}
120
121	((i = i + 2))
122done
123
124log_note "Verify 'zfs share -a' succeeds."
125
126#
127# Unshare each of the file systems.
128#
129i=0
130while (( i < ${#fs[*]} )); do
131	unshare_fs ${fs[i]}
132
133	((i = i + 2))
134done
135
136#
137# Try a zfs share -a and verify all file systems are shared.
138#
139log_must zfs share -a
140
141i=0
142while (( i < ${#fs[*]} )); do
143	is_shared ${fs[i]} || \
144	    log_fail "File system ${fs[i]} is not shared (share -a)"
145
146	((i = i + 2))
147done
148
149log_pass "'zfs share [ -a ] <filesystem>' succeeds as root."
150