xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/cli_root/zfs_unshare/zfs_unshare_001_pos.ksh (revision 915894ef19890baaed00080f85f6b69e225cda98)
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 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	if datasetexists "$TESTPOOL/$TESTCLONE"; then
66		log_must zfs destroy -f $TESTPOOL/$TESTCLONE
67	fi
68
69	if snapexists "$TESTPOOL/$TESTFS2@snapshot"; then
70		log_must zfs destroy -f $TESTPOOL/$TESTFS2@snapshot
71	fi
72
73	if datasetexists "$TESTPOOL/$TESTFS2"; then
74		log_must zfs destroy -f $TESTPOOL/$TESTFS2
75	fi
76}
77
78#
79# Main test routine.
80#
81# Given a mountpoint and file system this routine will attempt
82# unshare the filesystem via <filesystem|mountpoint> argument
83# and then verify it has been unshared.
84#
85function test_unshare # <mntp> <filesystem>
86{
87        typeset mntp=$1
88        typeset filesystem=$2
89	typeset prop_value
90
91	prop_value=$(get_prop "sharenfs" $filesystem)
92
93	if [[ $prop_value == "off" ]]; then
94		not_shared $mntp ||
95			log_must unshare -F nfs $mntp
96		log_must zfs set sharenfs=on $filesystem
97		is_shared $mntp || \
98			log_fail "'zfs set sharenfs=on' fails to make" \
99				"file system $filesystem shared."
100	fi
101
102	is_shared $mntp || log_must zfs share $filesystem
103
104        #
105	# Verify 'zfs unshare <filesystem>' works as well.
106	#
107	log_must zfs unshare $filesystem
108	not_shared $mntp || log_fail "'zfs unshare <filesystem>' fails"
109
110	log_must zfs share $filesystem
111
112	log_must zfs unshare $mntp
113	not_shared $mntp || log_fail "'zfs unshare <mountpoint>' fails"
114
115        log_note "Unsharing an unshared file system fails."
116        log_mustnot zfs unshare $filesystem
117	log_mustnot zfs unshare $mntp
118}
119
120set -A mntp_fs \
121    "$TESTDIR" "$TESTPOOL/$TESTFS" \
122    "$TESTDIR1" "$TESTPOOL/$TESTCTR/$TESTFS1" \
123    "$TESTDIR2" "$TESTPOOL/$TESTCLONE"
124
125log_assert "Verify that 'zfs unshare [-a] <filesystem|mountpoint>' succeeds as root."
126log_onexit cleanup
127
128log_must zfs create $TESTPOOL/$TESTFS2
129log_must zfs snapshot $TESTPOOL/$TESTFS2@snapshot
130log_must zfs clone $TESTPOOL/$TESTFS2@snapshot $TESTPOOL/$TESTCLONE
131log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL/$TESTCLONE
132
133#
134# Invoke 'test_unshare' routine to test 'zfs unshare <filesystem|mountpoint>'.
135#
136typeset -i i=0
137while (( i < ${#mntp_fs[*]} )); do
138	test_unshare ${mntp_fs[i]} ${mntp_fs[((i + 1 ))]}
139
140	((i = i + 2))
141done
142
143log_note "Verify 'zfs unshare -a' succeds as root."
144
145i=0
146typeset sharenfs_val
147while (( i < ${#mntp_fs[*]} )); do
148	sharenfs_val=$(get_prop "sharenfs" ${mntp_fs[((i+1))]})
149	if [[ $sharenfs_val == "on" ]]; then
150		not_shared ${mntp_fs[i]} && \
151			log_must zfs share ${mntp_fs[((i+1))]}
152	else
153		log_must zfs set sharenfs=on ${mntp_fs[((i+1))]}
154		is_shared ${mntp_fs[i]} || \
155			log_fail "'zfs set sharenfs=on' fails to share filesystem."
156	fi
157
158        ((i = i + 2))
159done
160
161#
162# test 'zfs unshare -a '
163#
164log_must zfs unshare -a
165
166#
167# verify all shared filesystems become unshared
168#
169i=0
170while (( i < ${#mntp_fs[*]} )); do
171        not_shared ${mntp_fs[i]} || \
172                log_fail "'zfs unshare -a' fails to unshare all shared zfs filesystems."
173
174        ((i = i + 2))
175done
176
177log_pass "'zfs unshare [-a] <filesystem|mountpoint>' succeeds as root."
178