]> git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/tests/functional/cli_root/zfs_rollback/zfs_rollback_common.kshlib
Add the ZFS Test Suite
[mirror_zfs.git] / tests / zfs-tests / tests / functional / cli_root / zfs_rollback / zfs_rollback_common.kshlib
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 #
28 # Copyright (c) 2012 by Delphix. All rights reserved.
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
37 function getsum #fname
38 {
39 (( ${#1} == 0 )) && \
40 log_fail "Need give file name."
41 return $($SUM $1 | $AWK '{print $1}')
42 }
43
44 # Define global variable checksum, get the original file sum.
45 #
46 origsum=$(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 #
55 function setup_snap_env
56 {
57 typeset -i cnt=${1:-3}
58 typeset createclone=${2:-"false"}
59
60 if datasetnonexists $FS; then
61 log_must $ZFS create $FS
62 log_must $ZFS set mountpoint=$TESTDIR $FS
63 fi
64 # Volume can't be created in Local Zone.
65 if datasetnonexists $VOL && is_global_zone; then
66 log_must $ZFS create -V $VOLSIZE $VOL
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
76 # mount it. Otherwise, only check if this ufs|ext2 file system
77 # was mounted.
78 #
79 log_must eval "$ECHO "y" | \
80 $NEWFS -v $ZVOL_DEVDIR/$VOL > /dev/null 2>&1"
81
82 [[ ! -d $TESTDIR1 ]] && log_must $MKDIR $TESTDIR1
83
84 # Make sure the ufs|ext2 filesystem hasn't been mounted,
85 # then mount the new ufs|ext2 filesystem.
86 if ! ismounted "$ZVOL_DEVDIR/$VOL" $NEWFS_DEFAULT_FS; then
87 log_must $MOUNT \
88 $ZVOL_DEVDIR/$TESTPOOL/$TESTVOL $TESTDIR1
89 fi
90 fi
91
92 # Separately Create three snapshots for file system & volume
93 typeset -i ind=0
94 typeset dtst
95 for dtst in $FS $VOL; do
96 # Volume can be created in Local Zone.
97 if [[ $dtst == $VOL ]]; then
98 if ! is_global_zone; then
99 break
100 fi
101 fi
102
103 ind=0
104 while (( ind < cnt )); do
105 case $dtst in
106 $FS)
107 eval typeset snap=\$FSSNAP$ind
108 eval typeset clone=\$FSCLONE$ind
109 eval typeset fname=\$TESTDIR/\$TESTFILE$ind
110 ;;
111 $VOL)
112 eval typeset snap=\$VOLSNAP$ind
113 eval typeset clone=\$VOLCLONE$ind
114 eval typeset fname=\$TESTDIR1/\$TESTFILE$ind
115 ;;
116 esac
117
118 if datasetnonexists $snap; then
119 log_must $CP /etc/passwd $fname
120 if is_linux; then
121 log_must $SYNC
122 else
123 #
124 # using 'lockfs -f' to flush the writes
125 # to disk before taking a snapshot.
126 #
127 if [[ $dtst == $VOL ]]; then
128 log_must $LOCKFS -f $TESTDIR1
129 fi
130 fi
131 log_must $ZFS snapshot $snap
132 fi
133 if [[ $createclone == "true" ]]; then
134 if datasetnonexists $clone; then
135 log_must $ZFS clone $snap $clone
136 fi
137 fi
138 (( ind += 1 ))
139 done
140 done
141 }
142
143 function setup_clone_env
144 {
145 setup_snap_env $1 "true"
146 }
147
148 #
149 # Clean up the test environmnet
150 #
151 # $1 number of snapshot Note: Currently only support three snapshots.
152 #
153 function cleanup_env
154 {
155 typeset -i cnt=${1:-3}
156 typeset -i ind=0
157 typeset dtst
158 typeset snap
159
160 $PKILL ${DD##*/}
161
162 if ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
163 log_must $UMOUNT -f $TESTDIR1
164 fi
165
166 [[ -d $TESTDIR ]] && log_must $RM -rf $TESTDIR/*
167 [[ -d $TESTDIR1 ]] && log_must $RM -rf $TESTDIR1/*
168
169 for dtst in $FS $VOL; do
170 for snap in $TESTSNAP $TESTSNAP1 $TESTSNAP2; do
171 if snapexists $dtst@$snap; then
172 log_must $ZFS destroy -Rf $dtst@$snap
173 fi
174 done
175 done
176
177 # Restore original test environment
178 if datasetnonexists $FS ; then
179 log_must $ZFS create $FS
180 fi
181 if datasetnonexists $VOL ; then
182 if is_global_zone ; then
183 log_must $ZFS create -V $VOLSIZE $VOL
184 else
185 log_must $ZFS create $VOL
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 #
197 function 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
218 function files_exist
219 {
220 file_status "exist" $@
221 }
222
223 function 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 #
234 function 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 #
294 function 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
306 $DD if=/dev/urandom of=$dir/$TESTFILE1 &
307 log_must $SLEEP 3
308 }