1#!/usr/local/bin/ksh93 -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# ident	"@(#)zpool_import_missing_001_pos.ksh	1.4	07/10/09 SMI"
28#
29. $STF_SUITE/include/libtest.kshlib
30. $STF_SUITE/tests/cli_root/zfs_mount/zfs_mount.kshlib
31. $STF_SUITE/tests/cli_root/zpool_import/zpool_import.kshlib
32
33################################################################################
34#
35# __stc_assertion_start
36#
37# ID: zpool_import_missing_001_pos
38#
39# DESCRIPTION:
40# 	Once a pool has been exported, and one or more devices are
41#	damaged or missing (d/m), import should handle this kind of situation
42#	as described:
43#		- Regular, report error while any number of devices failing.
44#		- Mirror could withstand (N-1) devices failing
45#		  before data integrity is compromised
46#		- Raidz could withstand one devices failing
47#		  before data integrity is compromised
48# 	Verify those are true.
49#
50# STRATEGY:
51#	1. Create test pool upon device files using the various combinations.
52#		- Regular pool
53#		- Mirror
54#		- Raidz
55#	2. Create necessary filesystem and test files.
56#	3. Export the test pool.
57#	4. Remove one or more devices
58#	5. Verify 'zpool import' will handle d/m device successfully.
59#	   Using the various combinations.
60#		- Regular import
61#		- Alternate Root Specified
62#	   It should be succeed with single d/m device upon 'raidz' & 'mirror',
63#	   but failed against 'regular' or more d/m devices.
64#	6. If import succeed, verify following is true:
65#		- The pool shows up under 'zpool list'.
66#		- The pool's health should be DEGRADED.
67#		- It contains the correct test file
68#
69# TESTABILITY: explicit
70#
71# TEST_AUTOMATION_LEVEL: automated
72#
73# CODING_STATUS: COMPLETED (2005-08-01)
74#
75# __stc_assertion_end
76#
77################################################################################
78
79verify_runnable "global"
80
81set -A vdevs "" "mirror" "raidz"
82set -A options "" "-R $ALTER_ROOT"
83
84function perform_inner_test
85{
86	typeset action=$1
87	typeset import_opts=$2
88	typeset target=$3
89	typeset basedir
90
91	$action $ZPOOL import -d $DEVICE_DIR ${import_opts} $target
92	[[ $action == "log_mustnot" ]] && return
93
94	log_must poolexists $TESTPOOL1
95
96	health=$($ZPOOL list -H -o health $TESTPOOL1)
97	[[ "$health" == "DEGRADED" ]] || \
98		log_fail "ERROR: $TESTPOOL1: Incorrect health '$health'"
99	log_must ismounted $TESTPOOL1/$TESTFS
100
101	basedir=$TESTDIR1
102	[[ -n "${import_opts}" ]] && basedir=$ALTER_ROOT/$TESTDIR1
103	[[ ! -e "$basedir/$TESTFILE0" ]] && \
104		log_fail "ERROR: $basedir/$TESTFILE0 missing after import."
105
106	checksum2=$($SUM $basedir/$TESTFILE0 | $AWK '{print $1}')
107	[[ "$checksum1" != "$checksum2" ]] && \
108		log_fail "ERROR: Checksums differ ($checksum1 != $checksum2)"
109
110	log_must $ZPOOL export $TESTPOOL1
111}
112
113log_onexit cleanup_missing
114
115log_assert "Verify that import could handle damaged or missing device."
116
117CWD=$PWD
118cd $DEVICE_DIR || log_fail "ERROR: Unable change directory to $DEVICE_DIR"
119
120checksum1=$($SUM $MYTESTFILE | $AWK '{print $1}')
121
122typeset -i i=0
123while :; do
124	typeset vdtype="${vdevs[i]}"
125
126	typeset -i j=0
127	while (( j < ${#options[*]} )); do
128		typeset opts="${options[j]}"
129		[ -n "$vdtype" ] && typestr="$vdtype" || typestr="stripe"
130
131		# Prepare the pool.
132		setup_missing_test_pool $vdtype
133		guid=$(get_config $TESTPOOL1 pool_guid $DEVICE_DIR)
134		log_note "*** Testing $typestr tvd guid $guid opts '${opts}'"
135
136		typeset -i count=0
137		for device in $DEVICE_FILES ; do
138			log_mustnot poolexists $TESTPOOL1
139			log_must $RM -f $device
140
141			(( count = count + 1 ))
142
143			action=log_must
144			case "$vdtype" in
145				'mirror') (( count == $GROUP_NUM )) && \
146						action=log_mustnot
147					;;
148				'raidz')  (( count > 1 )) && \
149						action=log_mustnot
150					;;
151				'')  action=log_mustnot
152					;;
153			esac
154
155			log_note "Testing import by name; ${count} removed."
156			perform_inner_test $action "${opts}" $TESTPOOL1
157
158			log_note "Testing import by GUID; ${count} removed."
159			perform_inner_test $action "${opts}" $guid
160		done
161
162		recreate_missing_files
163		(( j = j + 1 ))
164	done
165	(( i = i + 1 ))
166	(( i == ${#vdevs[*]} )) && break
167done
168
169log_pass "Import could handle damaged or missing device."
170