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