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#	Once a pool has been exported, and one or more devices are
39#	move to other place, import should handle this kind of situation
40#	as described:
41#		- Regular, report error while any number of devices failing.
42#		- Mirror could withstand (N-1) devices failing
43#		  before data integrity is compromised
44#		- Raidz could withstand one devices failing
45#		  before data integrity is compromised
46#	Verify that is true.
47#
48# STRATEGY:
49#	1. Create test pool upon device files using the various combinations.
50#		- Regular pool
51#		- Mirror
52#		- Raidz
53#	2. Create necessary filesystem and test files.
54#	3. Export the test pool.
55#	4. Move one or more device files to other directory
56#	5. Verify 'zpool import -d' with the new directory
57#	   will handle moved files successfullly.
58#	   Using the various combinations.
59#		- Regular import
60#		- Alternate Root Specified
61#
62
63verify_runnable "global"
64
65set -A vdevs "" "mirror" "raidz"
66set -A options "" "-R $ALTER_ROOT"
67
68function cleanup
69{
70	cd $DEVICE_DIR || log_fail "Unable change directory to $DEVICE_DIR"
71	[[ -e $DEVICE_DIR/$DEVICE_ARCHIVE ]] && \
72		log_must tar xf $DEVICE_DIR/$DEVICE_ARCHIVE
73
74	poolexists $TESTPOOL1 || \
75		log_must zpool import -d $DEVICE_DIR $TESTPOOL1
76
77	cleanup_filesystem $TESTPOOL1 $TESTFS
78
79	destroy_pool $TESTPOOL1
80}
81
82function cleanup_all
83{
84	cleanup
85
86	# recover dev files
87	typeset i=0
88	while (( i < $MAX_NUM )); do
89		typeset dev_file=${DEVICE_DIR}/${DEVICE_FILE}$i
90		if [[ ! -e ${dev_file} ]]; then
91			log_must mkfile $FILE_SIZE ${dev_file}
92		fi
93		((i += 1))
94	done
95
96	log_must rm -f $DEVICE_DIR/$DEVICE_ARCHIVE
97	cd $CWD || log_fail "Unable change directory to $CWD"
98
99	[[ -d $ALTER_ROOT ]] && \
100		log_must rm -rf $ALTER_ROOT
101
102	[[ -d $BACKUP_DEVICE_DIR ]] && \
103		log_must rm -rf $BACKUP_DEVICE_DIR
104}
105
106log_onexit cleanup_all
107
108log_assert "Verify that import could handle moving device."
109
110CWD=$PWD
111
112[[ ! -d $BACKUP_DEVICE_DIR ]] &&
113	log_must mkdir -p $BACKUP_DEVICE_DIR
114
115cd $DEVICE_DIR || log_fail "Unable change directory to $DEVICE_DIR"
116
117typeset -i i=0
118typeset -i j=0
119typeset -i count=0
120typeset basedir backup
121typeset action
122
123while (( i < ${#vdevs[*]} )); do
124
125	(( i != 0 )) && \
126		log_must tar xf $DEVICE_DIR/$DEVICE_ARCHIVE
127
128	setup_filesystem "$DEVICE_FILES" \
129		$TESTPOOL1 $TESTFS $TESTDIR1 \
130		"" ${vdevs[i]}
131
132	guid=$(get_config $TESTPOOL1 pool_guid)
133	backup=""
134
135	log_must cp $MYTESTFILE $TESTDIR1/$TESTFILE0
136
137	log_must zfs umount $TESTDIR1
138
139	j=0
140	while (( j <  ${#options[*]} )); do
141
142		count=0
143
144		#
145		# Restore all device files.
146		#
147		[[ -n $backup ]] && \
148			log_must tar xf $DEVICE_DIR/$DEVICE_ARCHIVE
149
150		log_must rm -f $BACKUP_DEVICE_DIR/*
151
152		for device in $DEVICE_FILES ; do
153
154			poolexists $TESTPOOL1 && \
155				log_must zpool export $TESTPOOL1
156
157			#
158			# Backup all device files while filesystem prepared.
159			#
160			if [[ -z $backup ]] ; then
161				log_must tar cf $DEVICE_DIR/$DEVICE_ARCHIVE ${DEVICE_FILE}*
162				backup="true"
163			fi
164
165			log_must mv $device $BACKUP_DEVICE_DIR
166
167			(( count = count + 1 ))
168
169			action=log_mustnot
170			case "${vdevs[i]}" in
171				'mirror') (( count < $GROUP_NUM )) && \
172					action=log_must
173					;;
174				'raidz')  (( count == 1 )) && \
175					action=log_must
176					;;
177			esac
178
179			typeset target=$TESTPOOL1
180			if (( RANDOM % 2 == 0 )) ; then
181				target=$guid
182				log_note "Import by guid."
183			fi
184			$action zpool import \
185				-d $DEVICE_DIR ${options[j]} $target
186
187		done
188
189		((j = j + 1))
190	done
191
192	cleanup
193
194	((i = i + 1))
195done
196
197log_pass "Import could handle moving device."
198