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