]> git.proxmox.com Git - mirror_zfs.git/blame - tests/zfs-tests/tests/functional/cli_root/zfs_rollback/zfs_rollback_common.kshlib
Update ZTS to work on FreeBSD
[mirror_zfs.git] / tests / zfs-tests / tests / functional / cli_root / zfs_rollback / zfs_rollback_common.kshlib
CommitLineData
6bb24f4d
BB
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24# Use is subject to license terms.
25#
26
27#
c1d9abf9 28# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
6bb24f4d
BB
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/cli_root/zfs_rollback/zfs_rollback.cfg
33
34# Get file sum
35#
36# $1 full file name
37function getsum #fname
38{
39 (( ${#1} == 0 )) && \
40 log_fail "Need give file name."
c1d9abf9 41 return $(sum $1 | awk '{print $1}')
6bb24f4d
BB
42}
43
44# Define global variable checksum, get the original file sum.
45#
46origsum=$(getsum /etc/passwd)
47
48#
49# Setup or recover the test environment. Firstly, copy /etc/passwd to ZFS file
50# system or volume, then make a snapshot or clone. Repeat up to three times.
51#
52# $1 number of snapshot. Note: Currently only support three snapshots.
53# $2 indicate if it is necessary to create clone
54#
55function setup_snap_env
56{
57 typeset -i cnt=${1:-3}
58 typeset createclone=${2:-"false"}
59
60 if datasetnonexists $FS; then
c1d9abf9
JWK
61 log_must zfs create $FS
62 log_must zfs set mountpoint=$TESTDIR $FS
6bb24f4d
BB
63 fi
64 # Volume can't be created in Local Zone.
65 if datasetnonexists $VOL && is_global_zone; then
c1d9abf9 66 log_must zfs create -V $VOLSIZE $VOL
6bb24f4d
BB
67 block_device_wait
68 fi
69
70 # Make sure $VOL is volume
71 typeset type=$(get_prop type $VOL)
72 if datasetexists $VOL && \
73 [[ $type == 'volume' ]]; then
74 #
75 # At the first time, Make a UFS file system in volume and
5c214ae3 76 # mount it. Otherwise, only check if this ufs|ext file system
6bb24f4d
BB
77 # was mounted.
78 #
7839c4b5 79 log_must new_fs $ZVOL_DEVDIR/$VOL
6bb24f4d 80
c1d9abf9 81 [[ ! -d $TESTDIR1 ]] && log_must mkdir $TESTDIR1
6bb24f4d 82
5c214ae3
BB
83 # Make sure the ufs|ext filesystem hasn't been mounted,
84 # then mount the new ufs|ext filesystem.
95401cb6 85 if ! ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
c1d9abf9 86 log_must mount \
6bb24f4d
BB
87 $ZVOL_DEVDIR/$TESTPOOL/$TESTVOL $TESTDIR1
88 fi
89 fi
90
91 # Separately Create three snapshots for file system & volume
92 typeset -i ind=0
93 typeset dtst
94 for dtst in $FS $VOL; do
95 # Volume can be created in Local Zone.
96 if [[ $dtst == $VOL ]]; then
97 if ! is_global_zone; then
98 break
99 fi
100 fi
101
102 ind=0
103 while (( ind < cnt )); do
104 case $dtst in
105 $FS)
106 eval typeset snap=\$FSSNAP$ind
107 eval typeset clone=\$FSCLONE$ind
108 eval typeset fname=\$TESTDIR/\$TESTFILE$ind
109 ;;
110 $VOL)
111 eval typeset snap=\$VOLSNAP$ind
112 eval typeset clone=\$VOLCLONE$ind
113 eval typeset fname=\$TESTDIR1/\$TESTFILE$ind
114 ;;
115 esac
116
117 if datasetnonexists $snap; then
c1d9abf9 118 log_must cp /etc/passwd $fname
6bb24f4d 119 if is_linux; then
c1d9abf9 120 log_must sync
6bb24f4d
BB
121 else
122 #
123 # using 'lockfs -f' to flush the writes
124 # to disk before taking a snapshot.
125 #
126 if [[ $dtst == $VOL ]]; then
10f25119 127 log_must lockfs -f $TESTDIR1
6bb24f4d
BB
128 fi
129 fi
c1d9abf9 130 log_must zfs snapshot $snap
6bb24f4d
BB
131 fi
132 if [[ $createclone == "true" ]]; then
133 if datasetnonexists $clone; then
c1d9abf9 134 log_must zfs clone $snap $clone
95401cb6 135 block_device_wait
6bb24f4d
BB
136 fi
137 fi
138 (( ind += 1 ))
139 done
140 done
141}
142
143function setup_clone_env
144{
145 setup_snap_env $1 "true"
146}
147
148#
c9539600 149# Clean up the test environment
6bb24f4d
BB
150#
151# $1 number of snapshot Note: Currently only support three snapshots.
152#
153function cleanup_env
154{
155 typeset -i cnt=${1:-3}
156 typeset -i ind=0
157 typeset dtst
158 typeset snap
159
95401cb6 160 pkill -x dd
6bb24f4d
BB
161
162 if ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
c1d9abf9 163 log_must umount -f $TESTDIR1
6bb24f4d
BB
164 fi
165
c1d9abf9
JWK
166 [[ -d $TESTDIR ]] && log_must rm -rf $TESTDIR/*
167 [[ -d $TESTDIR1 ]] && log_must rm -rf $TESTDIR1/*
6bb24f4d
BB
168
169 for dtst in $FS $VOL; do
170 for snap in $TESTSNAP $TESTSNAP1 $TESTSNAP2; do
171 if snapexists $dtst@$snap; then
c1d9abf9 172 log_must zfs destroy -Rf $dtst@$snap
6bb24f4d
BB
173 fi
174 done
175 done
176
177 # Restore original test environment
178 if datasetnonexists $FS ; then
c1d9abf9 179 log_must zfs create $FS
6bb24f4d
BB
180 fi
181 if datasetnonexists $VOL ; then
182 if is_global_zone ; then
c1d9abf9 183 log_must zfs create -V $VOLSIZE $VOL
6bb24f4d 184 else
c1d9abf9 185 log_must zfs create $VOL
6bb24f4d
BB
186 fi
187 fi
188}
189
190#
191# check if the specified files have specified status.
192#
193# $1 expected status
194# $2-n full file name
195# If it is true return 0, else return 1
196#
197function file_status
198{
199 (( $# == 0 )) && \
200 log_fail "The file name is not defined."
201
202 typeset opt
203 case $1 in
204 exist) opt="-e" ;;
205 nonexist) opt="! -e" ;;
206 *) log_fail "Unsupported file status." ;;
207 esac
208
209 shift
210 while (( $# > 0 )); do
211 eval [[ $opt $1 ]] || return 1
212 shift
213 done
214
215 return 0
216}
217
218function files_exist
219{
220 file_status "exist" $@
221}
222
223function files_nonexist
224{
225 file_status "nonexist" $@
226}
227
228#
229# According to snapshot check if the file system was recovered to the right
230# point.
231#
232# $1 snapshot. fs@snap or vol@snap
233#
234function check_files
235{
236 typeset dtst=$1
237
238 if [[ $(get_prop type $dtst) != snapshot ]]; then
239 log_fail "Parameter must be a snapshot."
240 fi
241
242 typeset fsvol=${dtst%%@*}
243 typeset snap=${dtst##*@}
244 if [[ $(get_prop type $fsvol) == "filesystem" ]]; then
245 ind=""
246 else
247 ind="1"
248 fi
249
250 eval typeset file0=\$TESTDIR$ind/\$TESTFILE0
251 eval typeset file1=\$TESTDIR$ind/\$TESTFILE1
252 eval typeset file2=\$TESTDIR$ind/\$TESTFILE2
253
254 case $snap in
255 $TESTSNAP2)
256 log_must files_exist $file0 $file1 $file2
257
258 typeset sum0=$(getsum $file0)
259 typeset sum1=$(getsum $file1)
260 typeset sum2=$(getsum $file2)
261 if [[ $sum0 != $origsum || \
262 $sum1 != $origsum || sum2 != $origsum ]]
263 then
264 log_fail "After rollback, file sum is changed."
265 fi
266 ;;
267 $TESTSNAP1)
268 log_must files_exist $file0 $file1
269 log_must files_nonexist $file2
270
271 typeset sum0=$(getsum $file0)
272 typeset sum1=$(getsum $file1)
273 if [[ $sum0 != $origsum || $sum1 != $origsum ]]
274 then
275 log_fail "After rollback, file sum is changed."
276 fi
277 ;;
278 $TESTSNAP)
279 log_must files_exist $file0
280 log_must files_nonexist $file1 $file2
281
282 typeset sum0=$(getsum $file0)
283 if [[ $sum0 != $origsum ]]; then
284 log_fail "After rollback, file sum is changed."
285 fi
286 ;;
287 esac
288}
289
290# According to dataset type, write file to different directories.
291#
292# $1 dataset
293#
294function write_mountpoint_dir
295{
296 typeset dtst=$1
297 typeset dir
298
299 if [[ $dtst == $FS ]]; then
300 dir=$TESTDIR
301 log_must ismounted $dir
302 else
303 dir=$TESTDIR1
304 log_must ismounted $dir $NEWFS_DEFAULT_FS
305 fi
c1d9abf9
JWK
306 dd if=/dev/urandom of=$dir/$TESTFILE1 &
307 log_must sleep 3
6bb24f4d 308}