xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/zvol/zvol_common.shlib (revision 08855964b9970604433f7b19dcd71cf5af5e5f14)
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# Copyright 2024 MNX Cloud, Inc.
31#
32
33. $STF_SUITE/include/libtest.shlib
34. $STF_SUITE/tests/functional/zvol/zvol.cfg
35
36#
37# Create a simple zvol volume
38#
39# Where disk_device: is the name of the disk to be used
40#       volume_size: is the size of the volume, e.g. 2G
41#
42function default_zvol_setup # disk_device volume_size
43{
44        typeset disk=$1
45        typeset size=$2
46	typeset savedumpdev
47	typeset -i output
48
49        create_pool $TESTPOOL "$disk"
50
51        log_must zfs create -V $size $TESTPOOL/$TESTVOL
52}
53
54#
55# Destroy the default zvol which was setup using
56# default_zvol_setup().
57#
58function default_zvol_cleanup
59{
60        if datasetexists $TESTPOOL/$TESTVOL ; then
61		log_must zfs destroy $TESTPOOL/$TESTVOL
62	fi
63
64        destroy_pool $TESTPOOL
65}
66
67function get_dumpdevice
68{
69	typeset ret=$(dumpadm | grep "Dump device:" | awk '{print $3}')
70	echo $ret
71}
72
73function set_dumpsize
74{
75	typeset volume=$1
76
77	if [[ -z $volume ]] ; then
78		log_note "No volume specified."
79		return 1
80	fi
81
82	output=$(dumpadm -epH)
83
84	(( output = ceil(output / 1024.0 / 1024.0) ))
85	(( output = output + output / 5 ))
86	log_must zfs set volsize=${output}m $volume
87
88	return 0
89}
90
91function safe_dumpadm
92{
93	typeset device=$1
94
95	if [[ -z $device || $device == "none" ]] ; then
96		log_note "No dump device volume specified."
97		return 1
98	fi
99	if [[ $device == "/dev/zvol/dsk/"* ]] ; then
100		typeset volume=${device#/dev/zvol/dsk/}
101		set_dumpsize $volume
102		log_must dumpadm -d $device
103	else
104		log_must swapadd
105		if ! is_swap_inuse $device ; then
106			log_must swap -a $device
107		fi
108		log_must dumpadm -d swap
109	fi
110}
111
112function is_zvol_dumpified
113{
114	typeset volume=$1
115
116	if [[ -z $volume ]] ; then
117		log_note "No volume specified."
118		return 1
119	fi
120
121	zdb -dddd $volume 2 | grep "dumpsize" > /dev/null 2>&1
122	return $?
123}
124
125function is_swap_inuse
126{
127	typeset device=$1
128
129	if [[ -z $device ]] ; then
130		log_note "No device specified."
131		return 1
132	fi
133
134	swap -l | awk 'NR > 1 { print $1 }' | \
135	    grep "^$device\$" > /dev/null 2>&1
136	return $?
137}
138