1f38cb554SJohn Wren Kennedy#! /bin/ksh -p
2f38cb554SJohn Wren Kennedy#
3f38cb554SJohn Wren Kennedy# CDDL HEADER START
4f38cb554SJohn Wren Kennedy#
5f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
6f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
7f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
8f38cb554SJohn Wren Kennedy#
9f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
11f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
12f38cb554SJohn Wren Kennedy# and limitations under the License.
13f38cb554SJohn Wren Kennedy#
14f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
15f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
17f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
18f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
19f38cb554SJohn Wren Kennedy#
20f38cb554SJohn Wren Kennedy# CDDL HEADER END
21f38cb554SJohn Wren Kennedy#
22f38cb554SJohn Wren Kennedy
23f38cb554SJohn Wren Kennedy#
24f38cb554SJohn Wren Kennedy# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25f38cb554SJohn Wren Kennedy# Use is subject to license terms.
26f38cb554SJohn Wren Kennedy#
27f38cb554SJohn Wren Kennedy
28f38cb554SJohn Wren Kennedy#
29*1d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30f38cb554SJohn Wren Kennedy#
31f38cb554SJohn Wren Kennedy
32f38cb554SJohn Wren Kennedy. $STF_SUITE/include/libtest.shlib
33f38cb554SJohn Wren Kennedy. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
34f38cb554SJohn Wren Kennedy
35f38cb554SJohn Wren Kennedy#
36f38cb554SJohn Wren Kennedy# DESCRIPTION:
37f38cb554SJohn Wren Kennedy#	Create a snapshot from regular filesystem, volume,
38f38cb554SJohn Wren Kennedy#	or filesystem upon volume, Build a clone file system
39f38cb554SJohn Wren Kennedy#	from the snapshot and verify new files can be written.
40f38cb554SJohn Wren Kennedy#
41f38cb554SJohn Wren Kennedy# STRATEGY:
42f38cb554SJohn Wren Kennedy# 	1. Create snapshot use 3 combination:
43f38cb554SJohn Wren Kennedy#		- Regular filesystem
44f38cb554SJohn Wren Kennedy#		- Regular volume
45f38cb554SJohn Wren Kennedy#		- Filesystem upon volume
46f38cb554SJohn Wren Kennedy# 	2. Clone a new file system from the snapshot
47f38cb554SJohn Wren Kennedy# 	3. Verify the cloned file system is writable
48f38cb554SJohn Wren Kennedy#
49f38cb554SJohn Wren Kennedy
50f38cb554SJohn Wren Kennedyverify_runnable "both"
51f38cb554SJohn Wren Kennedy
52f38cb554SJohn Wren Kennedy# Setup array, 4 elements as a group, refer to:
53f38cb554SJohn Wren Kennedy# i+0: name of a snapshot
54f38cb554SJohn Wren Kennedy# i+1: mountpoint of the snapshot
55f38cb554SJohn Wren Kennedy# i+2: clone created from the snapshot
56f38cb554SJohn Wren Kennedy# i+3: mountpoint of the clone
57f38cb554SJohn Wren Kennedy
58f38cb554SJohn Wren Kennedyset -A args "$SNAPFS" "$SNAPDIR" "$TESTPOOL/$TESTCLONE" "$TESTDIR.0" \
59f38cb554SJohn Wren Kennedy	"$SNAPFS1" "$SNAPDIR3" "$TESTPOOL/$TESTCLONE1" "" \
60f38cb554SJohn Wren Kennedy	"$SNAPFS2" "$SNAPDIR2" "$TESTPOOL1/$TESTCLONE2" "$TESTDIR.2"
61f38cb554SJohn Wren Kennedy
62f38cb554SJohn Wren Kennedyfunction setup_all
63f38cb554SJohn Wren Kennedy{
64f38cb554SJohn Wren Kennedy	create_pool $TESTPOOL1 /dev/zvol/dsk/$TESTPOOL/$TESTVOL
65*1d32ba66SJohn Wren Kennedy	log_must zfs create $TESTPOOL1/$TESTFS
66*1d32ba66SJohn Wren Kennedy	log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL1/$TESTFS
67f38cb554SJohn Wren Kennedy
68f38cb554SJohn Wren Kennedy	return 0
69f38cb554SJohn Wren Kennedy}
70f38cb554SJohn Wren Kennedy
71f38cb554SJohn Wren Kennedyfunction cleanup_all
72f38cb554SJohn Wren Kennedy{
73f38cb554SJohn Wren Kennedy	typeset -i i=0
74f38cb554SJohn Wren Kennedy
75f38cb554SJohn Wren Kennedy	i=0
76f38cb554SJohn Wren Kennedy	while (( i < ${#args[*]} )); do
77f38cb554SJohn Wren Kennedy		snapexists ${args[i]} && \
78*1d32ba66SJohn Wren Kennedy			log_must zfs destroy -Rf ${args[i]}
79f38cb554SJohn Wren Kennedy
80f38cb554SJohn Wren Kennedy		[[ -d ${args[i+3]} ]] && \
81*1d32ba66SJohn Wren Kennedy			log_must rm -rf ${args[i+3]}
82f38cb554SJohn Wren Kennedy
83f38cb554SJohn Wren Kennedy		[[ -d ${args[i+1]} ]] && \
84*1d32ba66SJohn Wren Kennedy			log_must rm -rf ${args[i+1]}
85f38cb554SJohn Wren Kennedy
86f38cb554SJohn Wren Kennedy		(( i = i + 4 ))
87f38cb554SJohn Wren Kennedy	done
88f38cb554SJohn Wren Kennedy
89f38cb554SJohn Wren Kennedy	datasetexists $TESTPOOL1/$TESTFS  && \
90*1d32ba66SJohn Wren Kennedy		log_must zfs destroy -f $TESTPOOL1/$TESTFS
91f38cb554SJohn Wren Kennedy
92f38cb554SJohn Wren Kennedy	destroy_pool $TESTPOOL1
93f38cb554SJohn Wren Kennedy
94f38cb554SJohn Wren Kennedy	[[ -d $TESTDIR2 ]] && \
95*1d32ba66SJohn Wren Kennedy		log_must rm -rf $TESTDIR2
96f38cb554SJohn Wren Kennedy
97f38cb554SJohn Wren Kennedy	return 0
98f38cb554SJohn Wren Kennedy}
99f38cb554SJohn Wren Kennedy
100f38cb554SJohn Wren Kennedylog_assert "Verify a cloned file system is writable."
101f38cb554SJohn Wren Kennedy
102f38cb554SJohn Wren Kennedylog_onexit cleanup_all
103f38cb554SJohn Wren Kennedy
104f38cb554SJohn Wren Kennedysetup_all
105f38cb554SJohn Wren Kennedy
106f38cb554SJohn Wren Kennedy[[ -n $TESTDIR ]] && \
107*1d32ba66SJohn Wren Kennedy    log_must rm -rf $TESTDIR/* > /dev/null 2>&1
108f38cb554SJohn Wren Kennedy
109f38cb554SJohn Wren Kennedytypeset -i COUNT=10
110f38cb554SJohn Wren Kennedytypeset -i i=0
111f38cb554SJohn Wren Kennedy
112f38cb554SJohn Wren Kennedyfor mtpt in $TESTDIR $TESTDIR2 ; do
113f38cb554SJohn Wren Kennedy	log_note "Populate the $mtpt directory (prior to snapshot)"
114f38cb554SJohn Wren Kennedy	typeset -i j=1
115f38cb554SJohn Wren Kennedy	while [[ $j -le $COUNT ]]; do
116*1d32ba66SJohn Wren Kennedy		log_must file_write -o create -f $mtpt/before_file$j \
117f38cb554SJohn Wren Kennedy			-b $BLOCKSZ -c $NUM_WRITES -d $j
118f38cb554SJohn Wren Kennedy
119f38cb554SJohn Wren Kennedy		(( j = j + 1 ))
120f38cb554SJohn Wren Kennedy	done
121f38cb554SJohn Wren Kennedydone
122f38cb554SJohn Wren Kennedy
123f38cb554SJohn Wren Kennedywhile (( i < ${#args[*]} )); do
124f38cb554SJohn Wren Kennedy	#
125f38cb554SJohn Wren Kennedy	# Take a snapshot of the test file system.
126f38cb554SJohn Wren Kennedy	#
127*1d32ba66SJohn Wren Kennedy	log_must zfs snapshot ${args[i]}
128f38cb554SJohn Wren Kennedy
129f38cb554SJohn Wren Kennedy	#
130f38cb554SJohn Wren Kennedy	# Clone a new file system from the snapshot
131f38cb554SJohn Wren Kennedy	#
132*1d32ba66SJohn Wren Kennedy	log_must zfs clone ${args[i]} ${args[i+2]}
133f38cb554SJohn Wren Kennedy	if [[ -n ${args[i+3]} ]] ; then
134*1d32ba66SJohn Wren Kennedy		log_must zfs set mountpoint=${args[i+3]} ${args[i+2]}
135f38cb554SJohn Wren Kennedy
136*1d32ba66SJohn Wren Kennedy		FILE_COUNT=`ls -Al ${args[i+3]} | grep -v "total" \
137*1d32ba66SJohn Wren Kennedy		    | grep -v "\.zfs" | wc -l`
138f38cb554SJohn Wren Kennedy		if [[ $FILE_COUNT -ne $COUNT ]]; then
139*1d32ba66SJohn Wren Kennedy			ls -Al ${args[i+3]}
140f38cb554SJohn Wren Kennedy			log_fail "AFTER: ${args[i+3]} contains $FILE_COUNT files(s)."
141f38cb554SJohn Wren Kennedy		fi
142f38cb554SJohn Wren Kennedy
143f38cb554SJohn Wren Kennedy		log_note "Verify the ${args[i+3]} directory is writable"
144f38cb554SJohn Wren Kennedy		j=1
145f38cb554SJohn Wren Kennedy		while [[ $j -le $COUNT ]]; do
146*1d32ba66SJohn Wren Kennedy			log_must file_write -o create -f ${args[i+3]}/after_file$j \
147f38cb554SJohn Wren Kennedy			-b $BLOCKSZ -c $NUM_WRITES -d $j
148f38cb554SJohn Wren Kennedy			(( j = j + 1 ))
149f38cb554SJohn Wren Kennedy		done
150f38cb554SJohn Wren Kennedy
151*1d32ba66SJohn Wren Kennedy		FILE_COUNT=`ls -Al ${args[i+3]}/after* | grep -v "total" | wc -l`
152f38cb554SJohn Wren Kennedy		if [[ $FILE_COUNT -ne $COUNT ]]; then
153*1d32ba66SJohn Wren Kennedy			ls -Al ${args[i+3]}
154f38cb554SJohn Wren Kennedy			log_fail "${args[i+3]} contains $FILE_COUNT after* files(s)."
155f38cb554SJohn Wren Kennedy		fi
156f38cb554SJohn Wren Kennedy	fi
157f38cb554SJohn Wren Kennedy
158f38cb554SJohn Wren Kennedy	(( i = i + 4 ))
159f38cb554SJohn Wren Kennedydone
160f38cb554SJohn Wren Kennedy
161f38cb554SJohn Wren Kennedylog_pass "The clone file system is writable."
162