1# vim: filetype=sh
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 2007 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)zpool_add.kshlib	1.3	07/03/14 SMI"
30#
31
32. $STF_SUITE/include/libtest.kshlib
33
34#
35# check if the <pool> contains <vdev> ...
36#
37# $1 pool
38# $2..n <vdev> ...
39#
40# Return 0 if <vdev> are contained in the <pool>; 1 if not used; 2 if pool
41# name is missing
42#
43function iscontained
44{
45	typeset pool=$1
46	typeset vdev
47
48        if [[ -z $pool ]]; then
49                log_note "Missing pool name."
50                return 2
51        fi
52
53	shift
54
55	for vdev in $@; do
56
57# remove /dev/ in vdev if there is
58		vdev=${vdev#/dev/}
59
60		$ZPOOL status "$pool" | $AWK '$1 == vdevname {exit 1}' \
61			vdevname=$vdev >/dev/null 2>&1
62		(( $? != 1 )) && \
63			return 1
64	done
65
66	return 0;
67
68}
69
70#
71# Find the storage device in /etc/fstab
72#
73function find_vfstab_dev
74{
75	typeset vfstab="/etc/fstab"
76	typeset tmpfile="$TMPDIR/fstab.tmp"
77	typeset vfstabdev
78	typeset vfstabdevs=""
79	typeset line
80
81	$CAT $vfstab | $GREP "^/dev/" >$tmpfile
82	while read -r line
83	do
84		vfstabdev=`$ECHO "$line" | $AWK '{print $1}'`
85		vfstabdev=${vfstabdev%%:}
86		vfstabdevs="$vfstabdev $vfstabdevs"
87	done <$tmpfile
88
89	$RM -f $tmpfile
90	$ECHO $vfstabdevs
91}
92
93#
94# Find the storage device in /etc/mnttab
95#
96function find_mnttab_dev
97{
98	typeset mnttab="/etc/mnttab"
99	typeset tmpfile="$TMPDIR/mnttab.tmp"
100	typeset mnttabdev
101	typeset mnttabdevs=""
102	typeset line
103
104	$MOUNT | $GREP "^/dev/" >$tmpfile
105	while read -r line
106	do
107		mnttabdev=`$ECHO "$line" | $AWK '{print $1}'`
108		mnttabdev=${mnttabdev%%:}
109		mnttabdevs="$mnttabdev $mnttabdevs"
110	done <$tmpfile
111
112	$RM -f $tmpfile
113	$ECHO $mnttabdevs
114}
115
116#
117# Save the systme current dump device configuration
118#
119function save_dump_dev
120{
121
122	typeset dumpdev
123	typeset swapdev
124	typeset swapdevs=""
125	typeset tmpfile="$TMPDIR/swapinfo.tmp"
126
127	dumpdev=`readlink /dev/dumpdev`
128	swapinfo | $GREP "^/dev/" >$tmpfile
129	while read -r line
130	do
131		swapdev=`$ECHO "$line" | $AWK '{print $1}'`
132		swapdev=${swapdev%%:}
133		swapdevs="$swapdev $swapdevs"
134	done <$tmpfile
135	$ECHO "$dumpdev $swapdevs"
136}
137
138#
139# Common cleanup routine for partitions used in testing
140#
141function partition_cleanup
142{
143	log_note "Cleaning up partitions..."
144	if [[ -n $DISK ]]; then
145		partition_disk $SIZE $DISK 7
146	else
147		typeset disk=""
148		for disk in $DISK0 $DISK1; do
149			partition_disk $SIZE $disk 7
150		done
151	fi
152}
153