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	"@(#)zpool_add_006_pos.ksh	1.5	09/06/22 SMI"
30#
31. $STF_SUITE/include/libtest.kshlib
32. $STF_SUITE/tests/cli_root/zpool_add/zpool_add.kshlib
33
34################################################################################
35#
36# __stc_assertion_start
37#
38# ID: zpool_add_006_pos
39#
40# DESCRIPTION:
41# 'zpool add [-f]' can add large numbers of file-in-zfs-filesystem-based vdevs
42# to the specified pool without any errors.
43#
44# STRATEGY:
45# 1. Create assigned number of files in ZFS filesystem as vdevs and use the first
46# file to create a pool
47# 2. Add other vdevs to the pool should get success
48# 3  Fill in the filesystem and create a partially written file
49# as vdev
50# 4. Add the new file into the pool should be failed.
51#
52# TESTABILITY: explicit
53#
54# TEST_AUTOMATION_LEVEL: automated
55#
56# CODING_STATUS: COMPLETED (2005-10-09)
57#
58# __stc_assertion_end
59#
60################################################################################
61
62verify_runnable "global"
63
64function cleanup
65{
66	poolexists $TESTPOOL1 && \
67		destroy_pool $TESTPOOL1
68
69	datasetexists $TESTPOOL/$TESTFS && \
70		log_must $ZFS destroy -f $TESTPOOL/$TESTFS
71	poolexists $TESTPOOL && \
72		destroy_pool $TESTPOOL
73
74	if [[ -d $TESTDIR ]]; then
75		log_must $RM -rf $TESTDIR
76	fi
77
78	partition_cleanup
79}
80
81
82#
83# Create a pool and fs on the assigned disk, and dynamically create large
84# numbers of files as vdevs.(the default value is <VDEVS_NUM>)
85# the first file will be used to create a pool for other vdevs to be added into
86#
87
88function setup_vdevs #<disk>
89{
90	typeset disk=$1
91	typeset -i count=0
92	typeset -i largest_num=0
93	typeset -i slicesize=0
94	typeset vdev=""
95
96	fs_size=$(get_available_disk_size $disk)
97
98	# 64M is the minimum size for the pool
99	(( largest_num = fs_size / (1024 * 1024 * 64) ))
100	if (( largest_num < $VDEVS_NUM )); then
101		# Minus $largest_num/20 to leave 5% space for metadata.
102		(( vdevs_num=largest_num - largest_num/20 ))
103		file_size=64
104		vdev=$disk
105	else
106		vdevs_num=$VDEVS_NUM
107		(( file_size = fs_size / (1024 * 1024 * (vdevs_num + vdevs_num/20)) ))
108		if (( file_size > FILE_SIZE )); then
109			file_size=$FILE_SIZE
110		fi
111		# Plus $vdevs_num/20 to provide enough space for metadata.
112		(( slice_size = file_size * (vdevs_num + vdevs_num/20) ))
113		wipe_partition_table $disk
114		set_partition 0 "" ${slice_size}m $disk
115		vdev=${disk}p1
116        fi
117
118	create_pool $TESTPOOL $vdev
119	[[ -d $TESTDIR ]] && \
120		log_must $RM -rf $TESTDIR
121        log_must $MKDIR -p $TESTDIR
122        log_must $ZFS create $TESTPOOL/$TESTFS
123        log_must $ZFS set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
124
125# Create a pool first using the first file, and make subsequent files ready
126# as vdevs to add to the pool
127
128	vdev=${TESTDIR}/file.$count
129	VDEV_SIZE=${file_size}m
130	log_must create_vdevs ${TESTDIR}/file.$count
131	create_pool "$TESTPOOL1" "${TESTDIR}/file.$count"
132	log_must poolexists "$TESTPOOL1"
133
134	while (( count < vdevs_num )); do # minus 1 to avoid space non-enough
135		(( count = count + 1 ))
136		log_must create_vdevs ${TESTDIR}/file.$count
137		vdevs_list="$vdevs_list ${TESTDIR}/file.$count"
138	done
139	unset VDEV_SIZE
140}
141
142log_assert " 'zpool add [-f]' can add large numbers of vdevs to the specified" \
143	   " pool without any errors."
144log_onexit cleanup
145
146if [[ $DISK_ARRAY_NUM == 0 ]]; then
147        disk=$DISK
148else
149        disk=$DISK0
150fi
151
152vdevs_list=""
153vdevs_num=$VDEVS_NUM
154file_size=$FILE_SIZE
155
156setup_vdevs $disk
157log_must $ZPOOL add -f "$TESTPOOL1" $vdevs_list
158log_must iscontained "$TESTPOOL1" "$vdevs_list"
159
160log_pass "'zpool successfully add [-f]' can add large numbers of vdevs to the" \
161	 "specified pool without any errors."
162