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#
24# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/include/libtest.kshlib
28
29function force_unmount #dev
30{
31	typeset dev=$1
32
33	ismounted $dev
34	if (( $? == 0 )); then
35		log_must $ZFS $unmountforce $dev
36	fi
37	return 0
38}
39
40# Create pool and  ( fs | container | vol ) with the given parameters,
41# it'll destroy prior exist one that has the same name.
42
43function setup_filesystem #disklist #pool #fs #mntpoint #type #vdev
44{
45	typeset disklist=$1
46	typeset pool=$2
47	typeset fs=${3##/}
48	typeset mntpoint=$4
49	typeset type=$5
50	typeset vdev=$6
51
52	if [[ -z $pool || -z $fs || -z $mntpoint ]]; then
53		log_note "Missing parameter: (\"$pool\", \"$fs\", \"$mntpoint\")"
54		return 1
55	fi
56
57	if is_global_zone && [[ -z $disklist ]] ; then
58		log_note "Missing disklist."
59		return 1
60	fi
61
62	if [[ $vdev != "" && \
63		$vdev != "mirror" && \
64		$vdev != "raidz" ]] ; then
65
66		log_note "Wrong vdev: (\"$vdev\")"
67		return 1
68	fi
69
70	poolexists $pool || \
71		create_pool $pool $vdev $disklist
72
73	datasetexists $pool/$fs && \
74		log_must cleanup_filesystem $pool $fs
75
76	$RMDIR $mntpoint > /dev/null 2>&1
77	if [[ ! -d $mntpoint ]]; then
78		log_must $MKDIR -p $mntpoint
79	fi
80
81	case "$type" in
82		'ctr') 	log_must $ZFS create $pool/$fs
83			log_must $ZFS set mountpoint=$mntpoint $pool/$fs
84			;;
85		'vol') 	log_must $ZFS create -V $VOLSIZE $pool/$fs
86			;;
87		*) 	log_must $ZFS create $pool/$fs
88			log_must $ZFS set mountpoint=$mntpoint $pool/$fs
89			;;
90	esac
91
92	return 0
93}
94
95# Destroy ( fs | container | vol ) with the given parameters.
96function cleanup_filesystem #pool #fs
97{
98	typeset pool=$1
99	typeset fs=${2##/}
100	typeset mtpt=""
101
102	if [[ -z $pool || -z $fs ]]; then
103		log_note "Missing parameter: (\"$pool\", \"$fs\")"
104		return 1
105	fi
106
107	if datasetexists "$pool/$fs" ; then
108		mtpt=$(get_prop mountpoint "$pool/$fs")
109		log_must $ZFS destroy -r $pool/$fs
110
111		[[ -d $mtpt ]] && \
112			log_must $RM -rf $mtpt
113	else
114		return 1
115	fi
116
117	return 0
118}
119
120# Make sure 'zfs mount' should display all ZFS filesystems currently mounted.
121# The results of 'zfs mount' and 'df -F zfs' should be identical.
122function verify_mount_display
123{
124	typeset fs
125
126	for fs in $($ZFS $mountcmd | $AWK '{print $1}') ; do
127		log_must mounted $fs
128	done
129	return 0
130}
131