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