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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_set/zfs_set_common.kshlib
34
35#
36# DESCRIPTION:
37# Setting valid canmount to filesystem, it is successful.
38# Whatever is set to volume or snapshot, it is failed.
39# 'zfs set canmount=noauto <fs>'
40#
41# STRATEGY:
42# 1. Setup a pool and create fs, volume, snapshot clone within it.
43# 2. Set canmount=noauto for each dataset and check the return value
44#    and check if it still can be mounted by mount -a or shared by
45#    share -a
46# 3. mount each dataset(except volume) to see if it can be mounted.
47# 4. verify that a mounted dataset can be shared by share -a.
48#
49
50verify_runnable "both"
51
52set -A dataset_pos \
53	"$TESTPOOL/$TESTFS" "$TESTPOOL/$TESTCTR" "$TESTPOOL/$TESTCLONE"
54
55if is_global_zone ; then
56	set -A dataset_neg \
57		"$TESTPOOL/$TESTVOL" "$TESTPOOL/$TESTFS@$TESTSNAP" \
58		"$TESTPOOL/$TESTVOL@$TESTSNAP"  "$TESTPOOL/$TESTCLONE1"
59else
60	set -A dataset_neg \
61		"$TESTPOOL/$TESTFS@$TESTSNAP" "$TESTPOOL/$TESTVOL@$TESTSNAP"
62fi
63
64function cleanup
65{
66	i=0
67	while (( i < ${#dataset_pos[*]} )); do
68		ds=${dataset_pos[i]}
69		if datasetexists $ds; then
70			log_must zfs set mountpoint=${old_mnt[i]} $ds
71			log_must zfs set canmount=${old_canmount[i]} $ds
72		fi
73		(( i = i + 1 ))
74	done
75
76	ds=$TESTPOOL/$TESTCLONE
77	if datasetexists $ds; then
78		mntp=$(get_prop mountpoint $ds)
79		destroy_dataset $ds
80		[ -d $mntp ] && rm -fr $mntp
81	fi
82
83	snapexists $TESTPOOL/$TESTFS@$TESTSNAP && \
84		destroy_dataset $TESTPOOL/$TESTFS@$TESTSNAP -R
85
86	snapexists $TESTPOOL/$TESTVOL@$TESTSNAP && \
87		destroy_dataset $TESTPOOL/$TESTVOL@$TESTSNAP -R
88
89	zfs unmount -a > /dev/null 2>&1
90	log_must zfs mount -a
91
92	[ -d $tmpmnt ] && rm -fr $tmpmnt
93}
94
95log_assert "Setting canmount=noauto to file system, it must be successful."
96log_onexit cleanup
97
98set -A old_mnt
99set -A old_canmount
100set -A old_sharenfs
101typeset tmpmnt=/tmpmount$$
102typeset ds
103
104log_must zfs snapshot $TESTPOOL/$TESTFS@$TESTSNAP
105log_must zfs snapshot $TESTPOOL/$TESTVOL@$TESTSNAP
106log_must zfs clone $TESTPOOL/$TESTFS@$TESTSNAP $TESTPOOL/$TESTCLONE
107log_must zfs clone $TESTPOOL/$TESTVOL@$TESTSNAP $TESTPOOL/$TESTCLONE1
108
109typeset -i i=0
110while (( i < ${#dataset_pos[*]} )); do
111	ds=${dataset_pos[i]}
112	old_mnt[i]=$(get_prop mountpoint $ds)
113	old_canmount[i]=$(get_prop canmount $ds)
114	old_sharenfs[i]=$(get_prop sharenfs $ds)
115	(( i = i + 1 ))
116done
117
118i=0
119while (( i < ${#dataset_pos[*]} )) ; do
120	dataset=${dataset_pos[i]}
121	set_n_check_prop "noauto" "canmount" "$dataset"
122	log_must zfs set mountpoint=$tmpmnt $dataset
123	log_must zfs set sharenfs=on $dataset
124	if ismounted $dataset; then
125		zfs unmount -a > /dev/null 2>&1
126		log_must mounted $dataset
127		log_must zfs unmount $dataset
128		log_must unmounted $dataset
129		log_must zfs mount -a
130		log_must unmounted $dataset
131		log_must zfs share -a
132		log_mustnot is_exported $tmpmnt
133	else
134		log_must zfs mount -a
135		log_must unmounted $dataset
136		zfs unmount -a > /dev/null 2>&1
137		log_must unmounted $dataset
138	fi
139
140	log_must zfs mount $dataset
141	log_must mounted $dataset
142	log_must zfs share -a
143	log_must is_exported $tmpmnt
144
145	log_must zfs set sharenfs="${old_sharenfs[i]}" $dataset
146	log_must zfs set canmount="${old_canmount[i]}" $dataset
147	log_must zfs set mountpoint="${old_mnt[i]}" $dataset
148	(( i = i + 1 ))
149done
150
151for dataset in "${dataset_neg[@]}" ; do
152	set_n_check_prop "noauto" "canmount" "$dataset" "false"
153	log_mustnot ismounted $dataset
154done
155
156log_pass "Setting canmount=noauto to filesystem pass."
157