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