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 2012 Spectra Logic.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"@(#)hotspare_replace_006_pos.ksh	1.0	12/08/10 SL"
28#
29. $STF_SUITE/tests/hotspare/hotspare.kshlib
30. $STF_SUITE/tests/zfsd/zfsd.kshlib
31. $STF_SUITE/include/libgnop.kshlib
32
33################################################################################
34#
35# __stc_assertion_start
36#
37# ID: zfs_hotspare_007_pos
38#
39# DESCRIPTION:
40#	If a vdev gets removed from a pool with a spare while zfsd is shut
41#	down, then the spare will be activated when zfsd restarts
42#
43#
44# STRATEGY:
45#	1. Create 1 storage pools with hot spares.
46#	2. Turn off zfsd
47#	3. Remove one vdev
48#	4. Restart zfsd
49#	5. Verify that the spare is in use.
50#
51# TESTABILITY: explicit
52#
53# TEST_AUTOMATION_LEVEL: automated
54#
55# CODING STATUS: COMPLETED (2014-09-17)
56#
57# __stc_assertion_end
58#
59###############################################################################
60
61verify_runnable "global"
62
63log_assert "zfsd will spare missing drives on startup"
64
65
66function verify_assertion # spare_dev
67{
68	typeset spare_dev=$1
69	stop_zfsd
70
71	log_must destroy_gnop $REMOVAL_DISK
72
73	# Check to make sure ZFS sees the disk as removed
74	wait_for_pool_removal 20
75
76	restart_zfsd
77
78	# Check that the spare was activated
79	wait_for_pool_dev_state_change 20 $spare_dev INUSE
80
81	# Re-enable the  missing disk
82	log_must create_gnop $REMOVAL_DISK $PHYSPATH
83
84	# And now the spare should be released
85	wait_for_pool_dev_state_change 20 $spare_dev AVAIL
86}
87
88typeset PHYSPATH="some_physical_path"
89typeset REMOVAL_DISK=$DISK0
90typeset REMOVAL_NOP=${DISK0}.nop
91typeset SPARE_DISK=$DISK4
92typeset SPARE_NOP=${DISK4}.nop
93typeset OTHER_DISKS="${DISK1} ${DISK2} ${DISK3}"
94typeset OTHER_NOPS=${OTHER_DISKS//~(E)([[:space:]]+|$)/.nop\1}
95set -A MY_KEYWORDS "mirror" "raidz1" "raidz2"
96ensure_zfsd_running
97log_must create_gnops $OTHER_DISKS $SPARE_DISK
98log_must create_gnop $REMOVAL_DISK $PHYSPATH
99for keyword in "${MY_KEYWORDS[@]}" ; do
100	log_must create_pool $TESTPOOL $keyword $REMOVAL_NOP $OTHER_NOPS spare $SPARE_NOP
101	log_must $ZPOOL set autoreplace=on $TESTPOOL
102	iterate_over_hotspares verify_assertion $SPARE_NOP
103
104	destroy_pool "$TESTPOOL"
105done
106
107log_pass
108