xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/snapshot/clone_001_pos.ksh (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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 https://opensource.org/licenses/CDDL-1.0.
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) 2013, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/snapshot/snapshot.cfg
34
35#
36# DESCRIPTION:
37#	Create a snapshot from regular filesystem, volume,
38#	or filesystem upon volume, Build a clone file system
39#	from the snapshot and verify new files can be written.
40#
41# STRATEGY:
42# 	1. Create snapshot use 3 combination:
43#		- Regular filesystem
44#		- Regular volume
45#		- Filesystem upon volume
46# 	2. Clone a new file system from the snapshot
47# 	3. Verify the cloned file system is writable
48#
49
50verify_runnable "both"
51
52# Setup array, 4 elements as a group, refer to:
53# i+0: name of a snapshot
54# i+1: mountpoint of the snapshot
55# i+2: clone created from the snapshot
56# i+3: mountpoint of the clone
57
58set -A args "$SNAPFS" "$SNAPDIR" "$TESTPOOL/$TESTCLONE" "$TESTDIR.0" \
59	"$SNAPFS1" "$SNAPDIR3" "$TESTPOOL/$TESTCLONE1" "" \
60	"$SNAPFS2" "$SNAPDIR2" "$TESTPOOL1/$TESTCLONE2" "$TESTDIR.2"
61
62function setup_all
63{
64	if is_freebsd; then
65		# Pool creation on zvols is forbidden by default.
66		# Save and the current setting.
67		typeset _saved=$(get_tunable VOL_RECURSIVE)
68		log_must set_tunable64 VOL_RECURSIVE 1
69	fi
70	create_pool $TESTPOOL1 ${ZVOL_DEVDIR}/$TESTPOOL/$TESTVOL
71	if is_freebsd; then
72		# Restore the previous setting.
73		log_must set_tunable64 VOL_RECURSIVE $_saved
74	fi
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	typeset -i i=0
84
85	i=0
86	while (( i < ${#args[*]} )); do
87		snapexists ${args[i]} && \
88			destroy_dataset "${args[i]}" "-Rf"
89
90		[[ -d ${args[i+3]} ]] && \
91			log_must rm -rf ${args[i+3]}
92
93		[[ -d ${args[i+1]} ]] && \
94			log_must rm -rf ${args[i+1]}
95
96		(( i = i + 4 ))
97	done
98
99	datasetexists $TESTPOOL1/$TESTFS && \
100		destroy_dataset $TESTPOOL1/$TESTFS -f
101
102	destroy_pool $TESTPOOL1
103
104	[[ -d $TESTDIR2 ]] && \
105		log_must rm -rf $TESTDIR2
106
107	return 0
108}
109
110log_assert "Verify a cloned file system is writable."
111
112log_onexit cleanup_all
113
114setup_all
115
116[ -n $TESTDIR ] && log_must rm -rf $TESTDIR/*
117
118typeset -i COUNT=10
119typeset -i i=0
120
121for mtpt in $TESTDIR $TESTDIR2 ; do
122	log_note "Populate the $mtpt directory (prior to snapshot)"
123	typeset -i j=1
124	while [[ $j -le $COUNT ]]; do
125		log_must file_write -o create -f $mtpt/before_file$j \
126			-b $BLOCKSZ -c $NUM_WRITES -d $j
127
128		(( j = j + 1 ))
129	done
130done
131
132while (( i < ${#args[*]} )); do
133	#
134	# Take a snapshot of the test file system.
135	#
136	log_must zfs snapshot ${args[i]}
137
138	#
139	# Clone a new file system from the snapshot
140	#
141	log_must zfs clone ${args[i]} ${args[i+2]}
142	if [[ -n ${args[i+3]} ]] ; then
143		log_must zfs set mountpoint=${args[i+3]} ${args[i+2]}
144
145		FILE_COUNT=$(ls -A ${args[i+3]} | grep -cvF ".zfs")
146		if [[ $FILE_COUNT -ne $COUNT ]]; then
147			ls -Al ${args[i+3]}
148			log_fail "AFTER: ${args[i+3]} contains $FILE_COUNT files(s)."
149		fi
150
151		log_note "Verify the ${args[i+3]} directory is writable"
152		j=1
153		while [[ $j -le $COUNT ]]; do
154			log_must file_write -o create -f ${args[i+3]}/after_file$j \
155			-b $BLOCKSZ -c $NUM_WRITES -d $j
156			(( j = j + 1 ))
157		done
158
159		FILE_COUNT=$(ls -A ${args[i+3]}/after* | wc -l)
160		if [[ $FILE_COUNT -ne $COUNT ]]; then
161			ls -Al ${args[i+3]}
162			log_fail "${args[i+3]} contains $FILE_COUNT after* files(s)."
163		fi
164	fi
165
166	(( i = i + 4 ))
167done
168
169log_pass "The clone file system is writable."
170