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