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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"@(#)hotspare_add_003_neg.ksh	1.7	09/06/22 SMI"
28#
29. $STF_SUITE/tests/hotspare/hotspare.kshlib
30
31################################################################################
32#
33# __stc_assertion_start
34#
35# ID: hotspare_add_003_neg
36#
37# DESCRIPTION:
38# 'zpool add' with hot spares will fail
39# while the hot spares belong to the following cases:
40#	- nonexistent device,
41#	- part of an active pool,
42#	- currently mounted,
43#	- a swap device,
44#	- a dump device,
45#	- identical with the basic or spares vdev within the pool,
46#	- belong to a exported or potentially active ZFS pool,
47#	- a volume device that belong to the given pool,
48#
49# STRATEGY:
50#	1. Create case scenarios
51#	2. For each scenario, try to add [-f] the device to the pool
52#	3. Verify the add operation failes as expected.
53#
54# TESTABILITY: explicit
55#
56# TEST_AUTOMATION_LEVEL: automated
57#
58# CODING_STATUS: COMPLETED (2006-06-07)
59#
60# __stc_assertion_end
61#
62###############################################################################
63
64verify_runnable "global"
65
66function cleanup
67{
68	poolexists "$TESTPOOL" && \
69		destroy_pool "$TESTPOOL"
70	poolexists "$TESTPOOL1" && \
71		destroy_pool "$TESTPOOL1"
72
73	log_onfail $UMOUNT $TMPDIR/mounted_dir
74	log_onfail $SWAPOFF $swap_dev
75	log_onfail $DUMPON -r $dump_dev
76
77	partition_cleanup
78}
79
80log_assert "'zpool add [-f]' with hot spares should fail with inapplicable scenarios."
81
82log_onexit cleanup
83
84set_devs
85
86mounted_dev=${DISK0}
87swap_dev=${DISK1}
88dump_dev=${DISK2}
89nonexist_dev=${DISK2}bad_slice_num
90
91create_pool "$TESTPOOL" "${pooldevs[0]}"
92log_must poolexists "$TESTPOOL"
93
94create_pool "$TESTPOOL1" "${pooldevs[1]}"
95log_must poolexists "$TESTPOOL1"
96
97log_must $MKDIR $TMPDIR/mounted_dir
98log_must $NEWFS $mounted_dev
99log_must $MOUNT $mounted_dev $TMPDIR/mounted_dir
100
101log_must $SWAPON $swap_dev
102
103log_must $DUMPON $dump_dev
104
105#	- nonexistent device,
106#	- part of an active pool,
107#	- currently mounted,
108#	- a swap device,
109#	- identical with the basic or spares vdev within the pool,
110
111set -A arg "$nonexist_dev" \
112	"${pooldevs[0]}" \
113	"${pooldevs[1]}" \
114	"$mounted_dev" \
115	"$swap_dev"
116
117typeset -i i=0
118while (( i < ${#arg[*]} )); do
119	if [[ -n "${arg[i]}" ]]; then
120		log_mustnot $ZPOOL add $TESTPOOL spare ${arg[i]}
121		log_mustnot $ZPOOL add -f $TESTPOOL spare ${arg[i]}
122	fi
123	(( i = i + 1 ))
124done
125
126#	- a dump device,
127# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=241070
128# When that bug is fixed, add $dump_dev to $arg and remove this block.
129log_must $ZPOOL add $TESTPOOL spare ${dump_dev}
130log_must $ZPOOL remove $TESTPOOL ${dump_dev}
131log_must $ZPOOL add -f $TESTPOOL spare ${dump_dev}
132log_must $ZPOOL remove $TESTPOOL ${dump_dev}
133
134#	- belong to a exported or potentially active ZFS pool,
135
136log_must $ZPOOL export $TESTPOOL1
137log_mustnot $ZPOOL add "$TESTPOOL" spare ${pooldevs[1]}
138log_must $ZPOOL import -d $HOTSPARE_TMPDIR $TESTPOOL1
139
140log_pass "'zpool add [-f]' with hot spares should fail with inapplicable scenarios."
141