xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/zvol/zvol_common.shlib (revision 8459c777fc1aaabb2f7dad05de1313aa169417cd)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29# Copyright 2020 Joyent, Inc.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/zvol/zvol.cfg
34
35#
36# Create a simple zvol volume
37#
38# Where disk_device: is the name of the disk to be used
39#       volume_size: is the size of the volume, e.g. 2G
40#
41function default_zvol_setup # disk_device volume_size
42{
43        typeset disk=$1
44        typeset size=$2
45	typeset savedumpdev
46	typeset -i output
47
48        create_pool $TESTPOOL "$disk"
49
50        log_must zfs create -V $size $TESTPOOL/$TESTVOL
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
66function get_dumpdevice
67{
68	typeset ret=$(dumpadm | grep "Dump device:" | awk '{print $3}')
69	echo $ret
70}
71
72function set_dumpsize
73{
74	typeset volume=$1
75
76	if [[ -z $volume ]] ; then
77		log_note "No volume specified."
78		return 1
79	fi
80
81	log_must zfs set volsize=64m $volume
82
83	output=$(dumpadm -d /dev/zvol/dsk/$volume 2>&1 | \
84			tail -1 | awk '{print $3}')
85
86	if [[ -n $output ]]; then
87		(( output = output / 1024 / 1024 ))
88		(( output = output + output / 5 ))
89		log_must zfs set volsize=${output}m $volume
90	fi
91	return 0
92}
93
94function safe_dumpadm
95{
96	typeset device=$1
97
98	if [[ -z $device || $device == "none" ]] ; then
99		log_note "No dump device volume specified."
100		return 1
101	fi
102	if [[ $device == "/dev/zvol/dsk/"* ]] ; then
103		typeset volume=${device#/dev/zvol/dsk/}
104		set_dumpsize $volume
105		log_must dumpadm -d $device
106	else
107		log_must swapadd
108		if ! is_swap_inuse $device ; then
109			log_must swap -a $device
110		fi
111		log_must dumpadm -d swap
112	fi
113}
114
115function is_zvol_dumpified
116{
117	typeset volume=$1
118
119	if [[ -z $volume ]] ; then
120		log_note "No volume specified."
121		return 1
122	fi
123
124	zdb -dddd $volume 2 | grep "dumpsize" > /dev/null 2>&1
125	return $?
126}
127
128function is_swap_inuse
129{
130	typeset device=$1
131
132	if [[ -z $device ]] ; then
133		log_note "No device specified."
134		return 1
135	fi
136
137	swap -l | awk 'NR > 1 { print $1 }' | \
138	    grep "^$device\$" > /dev/null 2>&1
139	return $?
140}
141