xref: /freebsd/tests/sys/cddl/zfs/tests/zvol/zvol_common.kshlib (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
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
29#
30# Create a simple zvol volume
31#
32# Where disk_device: is the name of the disk to be used
33#       volume_size: is the size of the volume, e.g. 2G
34#
35function default_zvol_setup # disk_device volume_size
36{
37        typeset disk=$1
38        typeset size=$2
39	typeset savedumpdev
40	typeset -i output
41
42        create_pool $TESTPOOL "$disk"
43
44        log_must $ZFS create -V $size $TESTPOOL/$TESTVOL
45
46	if [[ -n $DUMPADM ]]; then
47		if is_dumpswap_supported $TESTPOOL ; then
48			set_dumpsize $TESTPOOL/$TESTVOL
49		fi
50	fi
51}
52
53#
54# Destroy the default zvol which was setup using
55# default_zvol_setup().
56#
57function default_zvol_cleanup
58{
59        if datasetexists $TESTPOOL/$TESTVOL ; then
60		log_must $ZFS destroy $TESTPOOL/$TESTVOL
61	fi
62
63        destroy_pool $TESTPOOL
64}
65
66#
67# Check if the given pool support "Swap and crash dumps"
68#
69function is_dumpswap_supported #pool
70{
71	typeset pool=$1
72
73	if [[ -z $pool ]] ; then
74		log_fail "No pool given."
75	fi
76
77	typeset -i SPA_VER_DUMPSWAP=10
78        typeset -i vp=$(get_pool_prop version $pool)
79
80	if (( vp >= SPA_VER_DUMPSWAP )) ; then
81		return 0
82	fi
83
84	return 1
85}
86
87function get_dumpdevice
88{
89	typeset ret=$($DUMPADM | $GREP "Dump device:" | $AWK '{print $3}')
90	print $ret
91}
92
93function set_dumpsize
94{
95	typeset volume=$1
96
97	if [[ -z $volume ]] ; then
98		log_note "No volume specified."
99		return 1
100	fi
101
102	log_must $ZFS set volsize=64m $volume
103
104	output=$($DUMPADM -d /dev/zvol/$volume 2>&1 | \
105			$TAIL -1 | $AWK '{print $3}')
106
107	if [[ -n $output ]]; then
108		(( output = output / 1024 / 1024 ))
109		(( output = output + output / 5 ))
110		log_must $ZFS set volsize=${output}m $volume
111	fi
112	return 0
113}
114
115function safe_dumpadm
116{
117	typeset device=$1
118
119	if [[ -z $device || $device == "none" ]] ; then
120		log_note "No dump device volume specified."
121		return 1
122	fi
123	if [[ $device == "/dev/zvol/"* ]] ; then
124		typeset volume=${device#/dev/zvol/}
125		set_dumpsize $volume
126		log_must $DUMPADM -d $device
127	else
128		log_must $SWAPADD
129		if ! is_swap_inuse $device ; then
130			log_must $SWAP -a $device
131		fi
132		log_must $DUMPADM -d swap
133	fi
134}
135
136function is_zvol_dumpified
137{
138	typeset volume=$1
139
140	if [[ -z $volume ]] ; then
141		log_note "No volume specified."
142		return 1
143	fi
144
145	$ZDB -dddd $volume 2 | $GREP "dumpsize" > /dev/null 2>&1
146	return $?
147}
148
149function is_swap_inuse
150{
151	typeset device=$1
152
153	if [[ -z $device ]] ; then
154		log_note "No device specified."
155		return 1
156	fi
157
158	$SWAP -l | $GREP -w $device > /dev/null 2>&1
159	return $?
160}
161