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 http://www.opensolaris.org/os/licensing.
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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_rename/zfs_rename.kshlib
34
35#
36# DESCRIPTION:
37#	Rename dataset, verify that the data haven't changed.
38#
39# STRATEGY:
40#	1. Create random data and copy to dataset.
41#	2. Perform renaming commands.
42#	3. Verify that the data haven't changed.
43#
44
45verify_runnable "both"
46
47function cleanup
48{
49	if datasetexists $TESTPOOL/$TESTFS ; then
50		log_must zfs destroy -Rf $TESTPOOL/$TESTFS
51	fi
52	log_must zfs create $TESTPOOL/$TESTFS
53	log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
54
55	rm -f $SRC_FILE $DST_FILE
56}
57
58function target_obj
59{
60	typeset dtst=$1
61
62	typeset obj
63	typeset type=$(get_prop type $dtst)
64	if [[ $type == "filesystem" ]]; then
65		obj=$(get_prop mountpoint $dtst)/${SRC_FILE##*/}
66	elif [[ $type == "volume" ]]; then
67		obj=$ZVOL_DEVDIR/$dtst
68	fi
69
70	echo $obj
71}
72
73log_assert "Rename dataset, verify that the data haven't changed."
74log_onexit cleanup
75
76# Generate random data
77#
78BS=512 ; CNT=2048
79SRC_FILE=$TESTDIR/srcfile.$$
80DST_FILE=$TESTDIR/dstfile.$$
81log_must dd if=/dev/urandom of=$SRC_FILE bs=$BS count=$CNT
82
83fs=$TESTPOOL/$TESTFS/fs.$$
84fsclone=$TESTPOOL/$TESTFS/fsclone.$$
85log_must zfs create $fs
86
87obj=$(target_obj $fs)
88log_must cp $SRC_FILE $obj
89
90snap=${fs}@snap.$$
91log_must zfs snapshot $snap
92log_must zfs clone $snap $fsclone
93
94# Rename dataset & clone
95#
96log_must zfs rename $fs ${fs}-new
97log_must zfs rename $fsclone ${fsclone}-new
98
99# Compare source file and target file
100#
101obj=$(target_obj ${fs}-new)
102log_must diff $SRC_FILE $obj
103obj=$(target_obj ${fsclone}-new)
104log_must diff $SRC_FILE $obj
105
106# Rename snapshot and re-clone dataset
107#
108log_must zfs rename ${fs}-new $fs
109log_must zfs rename $snap ${snap}-new
110log_must zfs clone ${snap}-new $fsclone
111
112# Compare source file and target file
113#
114obj=$(target_obj $fsclone)
115log_must diff $SRC_FILE $obj
116
117if is_global_zone; then
118	vol=$TESTPOOL/$TESTFS/vol.$$ ;	volclone=$TESTPOOL/$TESTFS/volclone.$$
119	log_must zfs create -V 100M $vol
120
121	obj=$(target_obj $vol)
122	block_device_wait $obj
123	log_must dd if=$SRC_FILE of=$obj bs=$BS count=$CNT
124
125	snap=${vol}@snap.$$
126	log_must zfs snapshot $snap
127	log_must zfs clone $snap $volclone
128
129	# Rename dataset & clone
130	log_must zfs rename $vol ${vol}-new
131	log_must zfs rename $volclone ${volclone}-new
132
133	# Compare source file and target file
134	obj=$(target_obj ${vol}-new)
135	block_device_wait $obj
136	log_must dd if=$obj of=$DST_FILE bs=$BS count=$CNT
137	log_must diff $SRC_FILE $DST_FILE
138	obj=$(target_obj ${volclone}-new)
139	block_device_wait $obj
140	log_must dd if=$obj of=$DST_FILE bs=$BS count=$CNT
141	log_must diff $SRC_FILE $DST_FILE
142
143	# Rename snapshot and re-clone dataset
144	log_must zfs rename ${vol}-new $vol
145	log_must zfs rename $snap ${snap}-new
146	log_must zfs clone ${snap}-new $volclone
147
148	# Compare source file and target file
149	obj=$(target_obj $volclone)
150	block_device_wait $obj
151	log_must dd if=$obj of=$DST_FILE bs=$BS count=$CNT
152	log_must diff $SRC_FILE $DST_FILE
153fi
154
155log_pass "Rename dataset, the data haven't changed passed."
156