1#!/usr/local/bin/ksh93 -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# ident	"@(#)readonly_001_pos.ksh	1.1	09/05/19 SMI"
28#
29
30. $STF_SUITE/tests/cli_root/zfs_set/zfs_set_common.kshlib
31
32###############################################################################
33#
34# __stc_assertion_start
35#
36# ID: readonly_001_pos
37#
38# DESCRIPTION:
39# Setting readonly on a dataset, it should keep the dataset as readonly.
40#
41# STRATEGY:
42# 1. Create pool, then create filesystem and volume within it.
43# 2. Setting readonly to each dataset.
44# 3. Check the return value and make sure it is 0.
45# 4. Verify the stuff under mountpoint is readonly.
46#
47# TESTABILITY: explicit
48#
49# TEST_AUTOMATION_LEVEL: automated
50#
51# CODING_STATUS: COMPLETED (2009-04-21)
52#
53# __stc_assertion_end
54#
55################################################################################
56
57verify_runnable "both"
58
59function cleanup
60{
61	for dataset in $TESTPOOL/$TESTFS $TESTPOOL/$TESTVOL ; do
62		snapexists ${dataset}@$TESTSNAP && \
63			log_must $ZFS destroy -R ${dataset}@$TESTSNAP
64	done
65}
66
67function initial_dataset # $1 dataset
68{
69	typeset dataset=$1
70
71	typeset fstype=$(get_prop type $dataset)
72
73	if [[ $fstype == "filesystem" ]] ; then
74		typeset mtpt=$(get_prop mountpoint $dataset)
75		log_must $TOUCH $mtpt/$TESTFILE0
76		log_must $MKDIR -p $mtpt/$TESTDIR0
77	fi
78}
79
80
81function cleanup_dataset # $1 dataset
82{
83	typeset dataset=$1
84
85	typeset fstype=$(get_prop type $dataset)
86
87	if [[ $fstype == "filesystem" ]] ; then
88		typeset mtpt=$(get_prop mountpoint $dataset)
89		log_must $RM -f $mtpt/$TESTFILE0
90		log_must $RM -rf $mtpt/$TESTDIR0
91	fi
92}
93
94function verify_readonly # $1 dataset, $2 on|off
95{
96	typeset dataset=$1
97	typeset value=$2
98
99	if datasetnonexists $dataset ; then
100		log_note "$dataset not exist!"
101		return 1
102	fi
103
104	typeset fstype=$(get_prop type $dataset)
105
106	expect="log_must"
107
108	if [[ $2 == "on" ]] ; then
109		expect="log_mustnot"
110	fi
111
112	case $fstype in
113		filesystem)
114			typeset mtpt=$(get_prop mountpoint $dataset)
115			$expect $TOUCH $mtpt/$TESTFILE1
116			$expect $MKDIR -p $mtpt/$TESTDIR1
117			$expect $ECHO 'y' | $RM $mtpt/$TESTFILE0
118			$expect $RMDIR $mtpt/$TESTDIR0
119
120			if [[ $expect == "log_must" ]] ; then
121				log_must $ECHO 'y' | $RM $mtpt/$TESTFILE1
122				log_must $RMDIR $mtpt/$TESTDIR1
123				log_must $TOUCH $mtpt/$TESTFILE0
124				log_must $MKDIR -p $mtpt/$TESTDIR0
125			fi
126			;;
127		volume)
128			$expect eval "$ECHO 'y' | $NEWFS /dev/zvol/$dataset > /dev/null 2>&1"
129			;;
130		*)
131			;;
132	esac
133
134	return 0
135}
136
137log_onexit cleanup
138
139log_assert "Setting a valid readonly property on a dataset succeeds."
140
141typeset all_datasets
142
143log_must $ZFS mount -a
144
145log_must $ZFS snapshot $TESTPOOL/$TESTFS@$TESTSNAP
146log_must $ZFS clone $TESTPOOL/$TESTFS@$TESTSNAP $TESTPOOL/$TESTCLONE
147
148if is_global_zone ; then
149	log_must $ZFS snapshot $TESTPOOL/$TESTVOL@$TESTSNAP
150	log_must $ZFS clone $TESTPOOL/$TESTVOL@$TESTSNAP $TESTPOOL/$TESTCLONE1
151	all_datasets="$TESTPOOL $TESTPOOL/$TESTFS $TESTPOOL/$TESTVOL $TESTPOOL/$TESTCLONE $TESTPOOL/$TESTCLONE1"
152else
153	all_datasets="$TESTPOOL $TESTPOOL/$TESTFS $TESTPOOL/$TESTCLONE"
154fi
155
156
157for dataset in $all_datasets; do
158	for value in on off; do
159		set_n_check_prop "off" "readonly" "$dataset"
160		initial_dataset $dataset
161
162		set_n_check_prop "$value" "readonly" "$dataset"
163		verify_readonly $dataset $value
164
165		set_n_check_prop "off" "readonly" "$dataset"
166		cleanup_dataset $dataset
167	done
168done
169
170log_pass "Setting a valid readonly property on a dataset succeeds."
171