1# vim: filetype=sh
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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/include/libtest.kshlib
28. $STF_SUITE/tests/cli_root/zpool_add/zpool_add.kshlib
29
30
31function cleanup_devices_all
32{
33	$RM -f ${devarray[*]}
34	[ -d "$HOTSPARE_TMPDIR" ] && rmdir $HOTSPARE_TMPDIR
35
36	return 0
37}
38
39typeset -a pooldevs
40typeset -a sparedevs
41typeset -a logdevs
42typeset -a keywords=("" "mirror" "raidz" "raidz2")
43typeset -a devarray
44
45function set_devs
46{
47	mkdir $HOTSPARE_TMPDIR
48	typeset -i i=0
49	while (( i < $N_DEVARRAY_FILES )) ; do
50		eval devarray[$i]=$HOTSPARE_TMPDIR/file.$i
51		log_must create_vdevs ${devarray[$i]}
52		(( i = i + 1 ))
53	done
54
55	sparedevs=("${devarray[0]}" "${devarray[1]}")
56
57	pooldevs=("${devarray[3]}" "${devarray[4]}" "${devarray[5]}")
58
59	logdevs="${devarray[7]}"
60
61}
62
63function partition_cleanup
64{
65	cleanup_devices_all
66	return 0
67}
68
69#
70# $1: keyword, should be "" "mirror" "raidz" "raidz2"
71# $2: hotspare list, default as $sparedevs
72#
73function setup_hotspares # keyword, spares
74{
75	typeset keyword=$1
76	shift
77	typeset spares=${@:-${sparedevs[@]}}
78
79	create_pool "$TESTPOOL" "$keyword" \
80		${pooldevs[@]}
81	log_must poolexists "$TESTPOOL"
82	log_must $ZPOOL set autoreplace=on "$TESTPOOL"
83	log_must $ZPOOL add -f "$TESTPOOL" spare $spares
84	log_must iscontained "$TESTPOOL" "$spares"
85
86	if [[ -n ${logdevs[@]} ]] ; then
87		log_must $ZPOOL add -f "$TESTPOOL" log ${logdevs[@]}
88		log_must iscontained "$TESTPOOL" "${logdevs[@]}"
89	fi
90}
91
92#
93# $1: the function name that run for all hotspares
94# $2: hotspare list, default as $sparedevs
95#
96function iterate_over_hotspares # function, spares
97{
98	typeset function=$1
99	typeset spares=${2:-${sparedevs[@]}}
100
101	for spare in $spares
102	do
103		$function $spare
104	done
105}
106
107wait_until_resilvered() {
108typeset -i i=0
109typeset -i timeout=60
110while [[ $i -lt $timeout ]]; do
111	if is_pool_resilvered $TESTPOOL; then
112		break
113	fi
114	(( i += 1 ))
115	if [[ $i == $timeout ]]; then
116		$ZPOOL status $TESTPOOL
117		log_fail "Pool didn't resilver in ${timeout} seconds"
118	fi
119	$SLEEP 1
120done
121}
122