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# $FreeBSD$
24
25#
26# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)hotspare_add_003_neg.ksh	1.7	09/06/22 SMI"
30#
31. $STF_SUITE/tests/hotspare/hotspare.kshlib
32
33################################################################################
34#
35# __stc_assertion_start
36#
37# ID: hotspare_add_003_neg
38#
39# DESCRIPTION:
40# 'zpool add' with hot spares will fail
41# while the hot spares belong to the following cases:
42#	- nonexist device,
43#	- part of an active pool,
44#	- currently mounted,
45#	- devices in /etc/vfstab,
46#	- specified as the dedicated dump device,
47#	- identical with the basic or spares vdev within the pool,
48#	- belong to a exported or potentially active ZFS pool,
49#	- a volume device that belong to the given pool,
50#
51# STRATEGY:
52#	1. Create case scenarios
53#	2. For each scenario, try to add [-f] the device to the pool
54#	3. Verify the add operation failes as expected.
55#
56# TESTABILITY: explicit
57#
58# TEST_AUTOMATION_LEVEL: automated
59#
60# CODING_STATUS: COMPLETED (2006-06-07)
61#
62# __stc_assertion_end
63#
64###############################################################################
65
66verify_runnable "global"
67
68function cleanup
69{
70	poolexists "$TESTPOOL" && \
71		destroy_pool "$TESTPOOL"
72	poolexists "$TESTPOOL1" && \
73		destroy_pool "$TESTPOOL1"
74
75	if [[ -n $saved_dump_dev ]]; then
76		if [[ -n $DUMPADM ]]; then
77			log_must $DUMPADM -u -d $saved_dump_dev
78		fi
79	fi
80
81	if [[ -n $DUMPADM ]]; then
82		cleanup_devices $dump_dev
83	fi
84
85	partition_cleanup
86}
87
88log_assert "'zpool add [-f]' with hot spares should fail with inapplicable scenarios."
89
90log_onexit cleanup
91
92set_devs
93
94mnttab_dev=$(find_mnttab_dev)
95vfstab_dev=$(find_vfstab_dev)
96saved_dump_dev=$(save_dump_dev)
97dump_dev=${disk}s0
98nonexist_dev=${disk}sbad_slice_num
99
100create_pool "$TESTPOOL" "${pooldevs[0]}"
101log_must poolexists "$TESTPOOL"
102
103create_pool "$TESTPOOL1" "${pooldevs[1]}"
104log_must poolexists "$TESTPOOL1"
105
106[[ -n $mnttab_dev ]] || log_note "No mnttab devices found"
107[[ -n $vfstab_dev ]] || log_note "No vfstab devices found"
108#	- nonexist device,
109#	- part of an active pool,
110#	- currently mounted,
111#	- devices in /etc/vfstab,
112#	- identical with the basic or spares vdev within the pool,
113
114set -A arg "$nonexist_dev" \
115	"${pooldevs[0]}" \
116	"${pooldevs[1]}" \
117	"$mnttab_dev" \
118	"$vfstab_dev"
119
120typeset -i i=0
121while (( i < ${#arg[*]} )); do
122	if [[ -n "${arg[i]}" ]]; then
123		log_mustnot $ZPOOL add $TESTPOOL spare ${arg[i]}
124		log_mustnot $ZPOOL add -f $TESTPOOL spare ${arg[i]}
125	fi
126	(( i = i + 1 ))
127done
128
129#	- specified as the dedicated dump device,
130# This part of the test can only be run on platforms for which DUMPADM is
131# defined; ie Solaris
132if [[ -n $DUMPADM ]]; then
133	log_must $DUMPADM -u -d /dev/$dump_dev
134	log_mustnot $ZPOOL add "$TESTPOOL" spare $dump_dev
135	log_mustnot $ZPOOL add -f "$TESTPOOL" spare $dump_dev
136fi
137
138#	- belong to a exported or potentially active ZFS pool,
139
140log_must $ZPOOL export $TESTPOOL1
141log_mustnot $ZPOOL add "$TESTPOOL" spare ${pooldevs[1]}
142log_must $ZPOOL import -d $HOTSPARE_TMPDIR $TESTPOOL1
143
144log_pass "'zpool add [-f]' with hot spares should fail with inapplicable scenarios."
145