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#	damaged or missing (d/m), 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 those are 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. Remove one or more devices
56#	5. Verify 'zpool import' will handle d/m device successfully.
57#	   Using the various combinations.
58#		- Regular import
59#		- Alternate Root Specified
60#	   It should be succeed with single d/m device upon 'raidz' & 'mirror',
61#	   but failed against 'regular' or more d/m devices.
62#	6. If import succeed, verify following is true:
63#		- The pool shows up under 'zpool list'.
64#		- The pool's health should be DEGRADED.
65#		- It contains the correct test file
66#
67
68verify_runnable "global"
69
70set -A vdevs "" "mirror" "raidz"
71set -A options "" "-R $ALTER_ROOT"
72
73function cleanup
74{
75	# recover the vdevs
76	recreate_files
77
78	[[ -d $ALTER_ROOT ]] && \
79		log_must rm -rf $ALTER_ROOT
80}
81
82function recreate_files
83{
84	if poolexists "$TESTPOOL1" ; then
85		cleanup_filesystem $TESTPOOL1 $TESTFS
86		destroy_pool $TESTPOOL1
87	fi
88
89	log_must rm -rf $DEVICE_DIR/*
90	typeset i=0
91	while (( i < $MAX_NUM )); do
92		log_must mkfile $FILE_SIZE ${DEVICE_DIR}/${DEVICE_FILE}$i
93		((i += 1))
94	done
95}
96
97log_onexit cleanup
98
99log_assert "Verify that import could handle damaged or missing device."
100
101CWD=$PWD
102cd $DEVICE_DIR || log_fail "Unable change directory to $DEVICE_DIR"
103
104checksum1=$(sum $MYTESTFILE | awk '{print $1}')
105
106typeset -i i=0
107typeset -i j=0
108typeset -i count=0
109typeset basedir backup
110
111while (( i < ${#vdevs[*]} )); do
112
113	setup_filesystem "$DEVICE_FILES" \
114		$TESTPOOL1 $TESTFS $TESTDIR1 \
115		"" ${vdevs[i]}
116
117	backup=""
118
119	guid=$(get_config $TESTPOOL1 pool_guid)
120	log_must cp $MYTESTFILE $TESTDIR1/$TESTFILE0
121
122	log_must zfs umount $TESTDIR1
123
124	j=0
125	while (( j <  ${#options[*]} )); do
126
127		count=0
128		action=log_must
129
130		#
131		# Restore all device files.
132		#
133		[[ -n $backup ]] && \
134			log_must tar xf $DEVICE_DIR/$DEVICE_ARCHIVE
135
136		for device in $DEVICE_FILES ; do
137			log_must rm -f $device
138
139			poolexists $TESTPOOL1 && \
140				log_must zpool export $TESTPOOL1
141
142			#
143			# Backup all device files while filesystem prepared.
144			#
145			if [[ -z $backup ]]; then
146				log_must tar cf $DEVICE_DIR/$DEVICE_ARCHIVE \
147					${DEVICE_FILE}*
148				backup="true"
149			fi
150
151			(( count = count + 1 ))
152
153			case "${vdevs[i]}" in
154				'mirror') (( count == $GROUP_NUM )) && \
155						action=log_mustnot
156					;;
157				'raidz')  (( count > 1 )) && \
158						action=log_mustnot
159					;;
160				'')  action=log_mustnot
161					;;
162			esac
163
164			typeset target=$TESTPOOL1
165			if (( RANDOM % 2 == 0 )) ; then
166				target=$guid
167				log_note "Import by guid."
168			fi
169			$action zpool import \
170				-d $DEVICE_DIR ${options[j]} $target
171
172			[[ $action == "log_mustnot" ]] && continue
173
174			log_must poolexists $TESTPOOL1
175
176			health=$(zpool list -H -o health $TESTPOOL1)
177
178			[[ $health == "DEGRADED" ]] || \
179				log_fail "$TESTPOOL1: Incorrect health($health)"
180			log_must ismounted $TESTPOOL1/$TESTFS
181
182			basedir=$TESTDIR1
183			[[ -n ${options[j]} ]] && \
184				basedir=$ALTER_ROOT/$TESTDIR1
185
186			[[ ! -e $basedir/$TESTFILE0 ]] && \
187				log_fail "$basedir/$TESTFILE0 missing after import."
188
189			checksum2=$(sum $basedir/$TESTFILE0 | awk '{print $1}')
190			[[ "$checksum1" != "$checksum2" ]] && \
191				log_fail "Checksums differ ($checksum1 != $checksum2)"
192
193		done
194
195		((j = j + 1))
196	done
197
198	recreate_files
199
200	((i = i + 1))
201done
202
203log_pass "Import could handle damaged or missing device."
204