1#!/bin/ksh -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 https://opensource.org/licenses/CDDL-1.0.
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
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zpool_create/zpool_create.shlib
34
35#
36# DESCRIPTION:
37# 'zpool create -f <pool> <vspec> ...' can successfully create a
38# new pool in some cases.
39#
40# STRATEGY:
41# 1. Prepare the scenarios for '-f' option
42# 2. Use -f to override the devices to create new pools
43# 3. Verify the pool created successfully
44#
45
46verify_runnable "global"
47
48function cleanup
49{
50	for pool in $TESTPOOL $TESTPOOL1; do
51		poolexists $pool && destroy_pool $pool
52	done
53
54	rm -f $disk1 $disk2
55	if is_freebsd; then
56		umount -f $TESTDIR
57		rm -rf $TESTDIR
58	fi
59}
60
61log_onexit cleanup
62
63log_assert "'zpool create -f <pool> <vspec> ...' can successfully create" \
64	"a new pool in some cases."
65
66create_pool $TESTPOOL $DISK0
67log_must eval "new_fs ${DEV_RDSKDIR}/${DISK1} >/dev/null 2>&1"
68typeset disk1=$(create_blockfile $FILESIZE)
69typeset disk2=$(create_blockfile $FILESIZE1)
70
71unset NOINUSE_CHECK
72log_must zpool export $TESTPOOL
73log_note "'zpool create' without '-f' will fail " \
74	"while device belongs to an exported pool."
75log_mustnot zpool create $TESTPOOL1 $DISK0
76create_pool $TESTPOOL1 $DISK0
77log_must poolexists $TESTPOOL1
78
79log_must destroy_pool $TESTPOOL1
80
81log_note "'zpool create' without '-f' will fail " \
82	"while device is in use by a ufs filesystem."
83if is_freebsd; then
84	# fs must be mounted for create to fail on FreeBSD
85	log_must mkdir -p $TESTDIR
86	log_must mount ${DEV_DSKDIR}/${DISK1} $TESTDIR
87fi
88log_mustnot zpool create $TESTPOOL $DISK1
89if is_freebsd; then
90	# fs must not be mounted to create pool even with -f
91	log_must umount -f $TESTDIR
92	log_must rm -rf $TESTDIR
93fi
94create_pool $TESTPOOL $DISK1
95log_must poolexists $TESTPOOL
96
97log_must destroy_pool $TESTPOOL
98
99log_note "'zpool create' mirror without '-f' will fail " \
100	"while devices have different size."
101log_mustnot zpool create $TESTPOOL mirror $disk1 $disk2
102create_pool $TESTPOOL mirror $disk1 $disk2
103log_must poolexists $TESTPOOL
104
105log_must destroy_pool $TESTPOOL
106
107if ! is_freebsd; then
108	log_note "'zpool create' mirror without '-f' will fail " \
109		"while devices are of different types."
110	log_mustnot zpool create $TESTPOOL mirror $disk1 $DISK0
111	create_pool $TESTPOOL mirror $disk1 $DISK0
112	log_must poolexists $TESTPOOL
113
114	log_must destroy_pool $TESTPOOL
115fi
116
117log_note "'zpool create' without '-f' will fail " \
118	"while a device is part of a potentially active pool."
119create_pool $TESTPOOL mirror $DISK0 $DISK1
120log_must zpool offline $TESTPOOL $DISK0
121log_must zpool export $TESTPOOL
122log_mustnot zpool create $TESTPOOL1 $DISK0
123create_pool $TESTPOOL1 $DISK0
124log_must poolexists $TESTPOOL1
125
126log_must destroy_pool $TESTPOOL1
127
128log_pass "'zpool create -f <pool> <vspec> ...' success."
129