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_mount/zfs_mount.kshlib
34. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.cfg
35
36#
37# DESCRIPTION:
38# An exported pool can be imported under a different name. Hence
39# we test that a previously exported pool can be renamed.
40#
41# STRATEGY:
42#	1. Copy a file into the default test directory.
43#	2. Umount the default directory.
44#	3. Export the pool.
45#	4. Import the pool using the name ${TESTPOOL}-new,
46#	   and using the various combinations.
47#               - Regular import
48#               - Alternate Root Specified
49#	5. Verify it exists in the 'zpool list' output.
50#	6. Verify the default file system is mounted and that the file
51#	   from step (1) is present.
52#
53
54verify_runnable "global"
55
56set -A pools "$TESTPOOL" "$TESTPOOL1"
57set -A devs "" "-d $DEVICE_DIR"
58set -A options "" "-R $ALTER_ROOT"
59set -A mtpts "$TESTDIR" "$TESTDIR1"
60
61
62function cleanup
63{
64	typeset -i i=0
65	while (( i < ${#pools[*]} )); do
66		if poolexists "${pools[i]}-new" ; then
67			log_must zpool export "${pools[i]}-new"
68
69			[[ -d /${pools[i]}-new ]] && \
70				log_must rm -rf /${pools[i]}-new
71
72			log_must zpool import ${devs[i]} \
73				"${pools[i]}-new" ${pools[i]}
74		fi
75
76		datasetexists "${pools[i]}" || \
77			log_must zpool import ${devs[i]} ${pools[i]}
78
79		ismounted "${pools[i]}/$TESTFS" || \
80			log_must zfs mount ${pools[i]}/$TESTFS
81
82		[[ -e ${mtpts[i]}/$TESTFILE0 ]] && \
83			log_must rm -rf ${mtpts[i]}/$TESTFILE0
84
85		((i = i + 1))
86
87	done
88
89	cleanup_filesystem $TESTPOOL1 $TESTFS $TESTDIR1
90
91	destroy_pool $TESTPOOL1
92
93	[[ -d $ALTER_ROOT ]] && \
94		log_must rm -rf $ALTER_ROOT
95	[[ -e $VDEV_FILE ]] && \
96		log_must rm $VDEV_FILE
97}
98
99log_onexit cleanup
100
101log_assert "Verify that an imported pool can be renamed."
102
103setup_filesystem "$DEVICE_FILES" $TESTPOOL1 $TESTFS $TESTDIR1
104checksum1=$(sum $MYTESTFILE | awk '{print $1}')
105
106typeset -i i=0
107typeset -i j=0
108typeset basedir
109
110while (( i < ${#pools[*]} )); do
111	guid=$(get_config ${pools[i]} pool_guid)
112	log_must cp $MYTESTFILE ${mtpts[i]}/$TESTFILE0
113
114	log_must zfs umount ${mtpts[i]}
115
116	j=0
117	while (( j <  ${#options[*]} )); do
118		log_must zpool export ${pools[i]}
119
120		[[ -d /${pools[i]} ]] && \
121			log_must rm -rf /${pools[i]}
122
123		typeset target=${pools[i]}
124		if (( RANDOM % 2 == 0 )) ; then
125			target=$guid
126			log_note "Import by guid."
127		fi
128
129		log_must zpool import ${devs[i]} ${options[j]} \
130			$target ${pools[i]}-new
131
132		log_must poolexists "${pools[i]}-new"
133
134		log_must ismounted ${pools[i]}-new/$TESTFS
135
136		basedir=${mtpts[i]}
137		[[ -n ${options[j]} ]] && \
138			basedir=$ALTER_ROOT/${mtpts[i]}
139
140		[[ ! -e $basedir/$TESTFILE0 ]] && \
141			log_fail "$basedir/$TESTFILE0 missing after import."
142
143		checksum2=$(sum $basedir/$TESTFILE0 | awk '{print $1}')
144		[[ "$checksum1" != "$checksum2" ]] && \
145			log_fail "Checksums differ ($checksum1 != $checksum2)"
146
147		log_must zpool export "${pools[i]}-new"
148
149		[[ -d /${pools[i]}-new ]] && \
150			log_must rm -rf /${pools[i]}-new
151
152		target=${pools[i]}-new
153		if (( RANDOM % 2 == 0 )) ; then
154			target=$guid
155		fi
156		log_must zpool import ${devs[i]} $target ${pools[i]}
157
158		((j = j + 1))
159	done
160
161	((i = i + 1))
162done
163
164VDEV_FILE=$(mktemp /tmp/tmp.XXXXXX)
165
166log_must mkfile -n 128M $VDEV_FILE
167log_must zpool create overflow $VDEV_FILE
168log_must zfs create overflow/testfs
169ID=$(zpool get -Ho value guid overflow)
170log_must zpool export overflow
171log_mustnot zpool import -d /tmp $(echo id) \
172    $(printf "%*s\n" 250 "" | tr ' ' 'c')
173
174log_pass "Successfully imported and renamed a ZPOOL"
175