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 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 retuen value
44#    and check if it still can be mounted by mount -a.
45# 3. mount each dataset(except volume) to see if it can be mounted.
46#
47
48verify_runnable "both"
49
50set -A dataset_pos \
51	"$TESTPOOL/$TESTFS" "$TESTPOOL/$TESTCTR" "$TESTPOOL/$TESTCLONE"
52
53if is_global_zone ; then
54	set -A dataset_neg \
55		"$TESTPOOL/$TESTVOL" "$TESTPOOL/$TESTFS@$TESTSNAP" \
56		"$TESTPOOL/$TESTVOL@$TESTSNAP"  "$TESTPOOL/$TESTCLONE1"
57else
58	set -A dataset_neg \
59		"$TESTPOOL/$TESTFS@$TESTSNAP" "$TESTPOOL/$TESTVOL@$TESTSNAP"
60fi
61
62function cleanup
63{
64	i=0
65	while (( i < ${#dataset_pos[*]} )); do
66		ds=${dataset_pos[i]}
67		if datasetexists $ds; then
68			log_must zfs set mountpoint=${old_mnt[i]} $ds
69			log_must zfs set canmount=${old_canmount[i]} $ds
70		fi
71		(( i = i + 1 ))
72	done
73
74	ds=$TESTPOOL/$TESTCLONE
75	if datasetexists $ds; then
76		mntp=$(get_prop mountpoint $ds)
77		log_must zfs destroy $ds
78		if [[ -d $mntp ]]; then
79			rm -fr $mntp
80		fi
81	fi
82
83	if snapexists $TESTPOOL/$TESTFS@$TESTSNAP ; then
84		log_must zfs destroy -R $TESTPOOL/$TESTFS@$TESTSNAP
85	fi
86	if snapexists $TESTPOOL/$TESTVOL@$TESTSNAP ; then
87		log_must zfs destroy -R $TESTPOOL/$TESTVOL@$TESTSNAP
88	fi
89
90	zfs unmount -a > /dev/null 2>&1
91	log_must zfs mount -a
92
93	if [[ -d $tmpmnt ]]; then
94		rm -fr $tmpmnt
95	fi
96}
97
98log_assert "Setting canmount=noauto to file system, it must be successful."
99log_onexit cleanup
100
101set -A old_mnt
102set -A old_canmount
103typeset tmpmnt=/tmpmount$$
104typeset ds
105
106log_must zfs snapshot $TESTPOOL/$TESTFS@$TESTSNAP
107log_must zfs snapshot $TESTPOOL/$TESTVOL@$TESTSNAP
108log_must zfs clone $TESTPOOL/$TESTFS@$TESTSNAP $TESTPOOL/$TESTCLONE
109log_must zfs clone $TESTPOOL/$TESTVOL@$TESTSNAP $TESTPOOL/$TESTCLONE1
110
111typeset -i i=0
112while (( i < ${#dataset_pos[*]} )); do
113	ds=${dataset_pos[i]}
114	old_mnt[i]=$(get_prop mountpoint $ds)
115	old_canmount[i]=$(get_prop canmount $ds)
116	(( i = i + 1 ))
117done
118
119i=0
120while (( i < ${#dataset_pos[*]} )) ; do
121	dataset=${dataset_pos[i]}
122	set_n_check_prop "noauto" "canmount" "$dataset"
123	log_must zfs set mountpoint=$tmpmnt $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	else
132		log_must zfs mount -a
133		log_must unmounted $dataset
134		zfs unmount -a > /dev/null 2>&1
135		log_must unmounted $dataset
136	fi
137
138	log_must zfs mount $dataset
139	log_must mounted $dataset
140	log_must zfs set canmount="${old_canmount[i]}" $dataset
141	log_must zfs set mountpoint="${old_mnt[i]}" $dataset
142	(( i = i + 1 ))
143done
144
145for dataset in "${dataset_neg[@]}" ; do
146	set_n_check_prop "noauto" "canmount" "$dataset" "false"
147	log_mustnot ismounted $dataset
148done
149
150log_pass "Setting canmount=noauto to filesystem pass."
151