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.
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.
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]
23 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
28 # Copyright (c) 2012 by Delphix. All rights reserved.
31 . $STF_SUITE/include/libtest.shlib
32 . $STF_SUITE/tests/functional/cli_root/zfs_rollback/zfs_rollback.cfg
37 function getsum #fname
40 log_fail "Need give file name."
41 return $($SUM $1 | $AWK '{print $1}')
44 # Define global variable checksum, get the original file sum.
46 origsum=$(getsum /etc/passwd)
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.
52 # $1 number of snapshot. Note: Currently only support three snapshots.
53 # $2 indicate if it is necessary to create clone
55 function setup_snap_env
57 typeset -i cnt=${1:-3}
58 typeset createclone=${2:-"false"}
60 if datasetnonexists $FS; then
61 log_must $ZFS create $FS
62 log_must $ZFS set mountpoint=$TESTDIR $FS
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
70 # Make sure $VOL is volume
71 typeset type=$(get_prop type $VOL)
72 if datasetexists $VOL && \
73 [[ $type == 'volume' ]]; then
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
79 log_must eval "$ECHO "y" | \
80 $NEWFS -v $ZVOL_DEVDIR/$VOL > /dev/null 2>&1"
82 [[ ! -d $TESTDIR1 ]] && log_must $MKDIR $TESTDIR1
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
88 $ZVOL_DEVDIR/$TESTPOOL/$TESTVOL $TESTDIR1
92 # Separately Create three snapshots for file system & volume
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
104 while (( ind < cnt )); do
107 eval typeset snap=\$FSSNAP$ind
108 eval typeset clone=\$FSCLONE$ind
109 eval typeset fname=\$TESTDIR/\$TESTFILE$ind
112 eval typeset snap=\$VOLSNAP$ind
113 eval typeset clone=\$VOLCLONE$ind
114 eval typeset fname=\$TESTDIR1/\$TESTFILE$ind
118 if datasetnonexists $snap; then
119 log_must $CP /etc/passwd $fname
124 # using 'lockfs -f' to flush the writes
125 # to disk before taking a snapshot.
127 if [[ $dtst == $VOL ]]; then
128 log_must $LOCKFS -f $TESTDIR1
131 log_must $ZFS snapshot $snap
133 if [[ $createclone == "true" ]]; then
134 if datasetnonexists $clone; then
135 log_must $ZFS clone $snap $clone
143 function setup_clone_env
145 setup_snap_env $1 "true"
149 # Clean up the test environmnet
151 # $1 number of snapshot Note: Currently only support three snapshots.
155 typeset -i cnt=${1:-3}
162 if ismounted $TESTDIR1 $NEWFS_DEFAULT_FS; then
163 log_must $UMOUNT -f $TESTDIR1
166 [[ -d $TESTDIR ]] && log_must $RM -rf $TESTDIR/*
167 [[ -d $TESTDIR1 ]] && log_must $RM -rf $TESTDIR1/*
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
177 # Restore original test environment
178 if datasetnonexists $FS ; then
179 log_must $ZFS create $FS
181 if datasetnonexists $VOL ; then
182 if is_global_zone ; then
183 log_must $ZFS create -V $VOLSIZE $VOL
185 log_must $ZFS create $VOL
191 # check if the specified files have specified status.
194 # $2-n full file name
195 # If it is true return 0, else return 1
200 log_fail "The file name is not defined."
205 nonexist) opt="! -e" ;;
206 *) log_fail "Unsupported file status." ;;
210 while (( $# > 0 )); do
211 eval [[ $opt $1 ]] || return 1
220 file_status "exist" $@
223 function files_nonexist
225 file_status "nonexist" $@
229 # According to snapshot check if the file system was recovered to the right
232 # $1 snapshot. fs@snap or vol@snap
238 if [[ $(get_prop type $dtst) != snapshot ]]; then
239 log_fail "Parameter must be a snapshot."
242 typeset fsvol=${dtst%%@*}
243 typeset snap=${dtst##*@}
244 if [[ $(get_prop type $fsvol) == "filesystem" ]]; then
250 eval typeset file0=\$TESTDIR$ind/\$TESTFILE0
251 eval typeset file1=\$TESTDIR$ind/\$TESTFILE1
252 eval typeset file2=\$TESTDIR$ind/\$TESTFILE2
256 log_must files_exist $file0 $file1 $file2
258 typeset sum0=$(getsum $file0)
259 typeset sum1=$(getsum $file1)
260 typeset sum2=$(getsum $file2)
261 if [[ $sum0 != $origsum || \
262 $sum1 != $origsum || sum2 != $origsum ]]
264 log_fail "After rollback, file sum is changed."
268 log_must files_exist $file0 $file1
269 log_must files_nonexist $file2
271 typeset sum0=$(getsum $file0)
272 typeset sum1=$(getsum $file1)
273 if [[ $sum0 != $origsum || $sum1 != $origsum ]]
275 log_fail "After rollback, file sum is changed."
279 log_must files_exist $file0
280 log_must files_nonexist $file1 $file2
282 typeset sum0=$(getsum $file0)
283 if [[ $sum0 != $origsum ]]; then
284 log_fail "After rollback, file sum is changed."
290 # According to dataset type, write file to different directories.
294 function write_mountpoint_dir
299 if [[ $dtst == $FS ]]; then
301 log_must ismounted $dir
304 log_must ismounted $dir $NEWFS_DEFAULT_FS
306 $DD if=/dev/urandom of=$dir/$TESTFILE1 &