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