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. $STF_SUITE/include/libtest.kshlib
27
28################################################################################
29#
30# __stc_assertion_start
31#
32# ID: clone_001_pos
33#
34# DESCRIPTION:
35#	Create a snapshot from regular filesystem, volume,
36#	or filesystem upon volume, Build a clone file system
37#	from the snapshot and verify new files can be written.
38#
39# STRATEGY:
40# 	1. Create snapshot use 3 combination:
41#		- Regular filesystem
42#		- Regular volume
43#		- Filesystem upon volume
44# 	2. Clone a new file system from the snapshot
45# 	3. Verify the cloned file system is writable
46#
47# TESTABILITY: explicit
48#
49# TEST_AUTOMATION_LEVEL: automated
50#
51# CODING_STATUS: COMPLETED (2005-08-25)
52#
53# __stc_assertion_end
54#
55################################################################################
56
57verify_runnable "both"
58
59# Setup array, 4 elements as a group, refer to:
60# i+0: name of a snapshot
61# i+1: mountpoint of the snapshot
62# i+2: clone created from the snapshot
63# i+3: mountpoint of the clone
64
65set -A args "$SNAPFS" "$SNAPDIR" "$TESTPOOL/$TESTCLONE" "$TESTDIR.0" \
66	"$SNAPFS1" "$SNAPDIR3" "$TESTPOOL/$TESTCLONE1" "" \
67	"$SNAPFS2" "$SNAPDIR2" "$TESTPOOL1/$TESTCLONE2" "$TESTDIR.2"
68
69function setup_all
70{
71	create_pool $TESTPOOL1 /dev/zvol/$TESTPOOL/$TESTVOL
72	log_must $ZFS create $TESTPOOL1/$TESTFS
73	log_must $ZFS set mountpoint=$TESTDIR2 $TESTPOOL1/$TESTFS
74
75	return 0
76}
77
78function cleanup_all
79{
80	destroy_pool $TESTPOOL1
81
82	[[ -d $TESTDIR2 ]] && \
83		log_must $RM -rf $TESTDIR2
84
85	return 0
86}
87
88log_assert "Verify a cloned file system is writable."
89
90log_onexit cleanup_all
91
92setup_all
93
94[[ -n $TESTDIR ]] && \
95    log_must $RM -rf $TESTDIR/* > /dev/null 2>&1
96
97typeset -i COUNT=10
98
99for mtpt in $TESTDIR $TESTDIR2 ; do
100	log_note "Populate the $mtpt directory (prior to snapshot)"
101	populate_dir $mtpt/before_file $COUNT $NUM_WRITES $BLOCKSZ ITER
102done
103
104typeset -i i=0
105while (( i < ${#args[*]} )); do
106	#
107	# Take a snapshot of the test file system.
108	#
109	log_must $ZFS snapshot ${args[i]}
110
111	#
112	# Clone a new file system from the snapshot
113	#
114	log_must $ZFS clone ${args[i]} ${args[i+2]}
115	if [[ -n ${args[i+3]} ]] ; then
116		log_must $ZFS set mountpoint=${args[i+3]} ${args[i+2]}
117
118		FILE_COUNT=`$LS -Al ${args[i+3]} | $GREP -v "total" |  wc -l`
119		if [[ $FILE_COUNT -ne $COUNT ]]; then
120			$LS -Al ${args[i+3]}
121			log_fail "AFTER: ${args[i+3]} contains $FILE_COUNT files(s)."
122		fi
123
124		log_note "Verify the ${args[i+3]} directory is writable"
125		populate_dir ${args[i+3]}/after_file $COUNT $NUM_WRITES \
126			$BLOCKSZ ITER
127
128		FILE_COUNT=`$LS -Al ${args[i+3]}/after* | $GREP -v "total" | wc -l`
129		if [[ $FILE_COUNT -ne $COUNT ]]; then
130			$LS -Al ${args[i+3]}
131			log_fail "${args[i+3]} contains $FILE_COUNT after* files(s)."
132		fi
133	fi
134
135	(( i = i + 4 ))
136done
137
138log_pass "The clone file system is writable."
139