summaryrefslogtreecommitdiff
path: root/usr/src/test/zfs-tests/tests/functional/zvol/zvol_common.shlib
blob: c8571847513021e014e3108d1ee507682423d91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
# Copyright 2020 Joyent, Inc.
#

. $STF_SUITE/include/libtest.shlib
. $STF_SUITE/tests/functional/zvol/zvol.cfg

#
# Create a simple zvol volume
#
# Where disk_device: is the name of the disk to be used
#       volume_size: is the size of the volume, e.g. 2G
#
function default_zvol_setup # disk_device volume_size
{
        typeset disk=$1
        typeset size=$2
	typeset savedumpdev
	typeset -i output

        create_pool $TESTPOOL "$disk"

        log_must zfs create -V $size $TESTPOOL/$TESTVOL
}

#
# Destroy the default zvol which was setup using
# default_zvol_setup().
#
function default_zvol_cleanup
{
        if datasetexists $TESTPOOL/$TESTVOL ; then
		log_must zfs destroy $TESTPOOL/$TESTVOL
	fi

        destroy_pool $TESTPOOL
}

function get_dumpdevice
{
	typeset ret=$(dumpadm | grep "Dump device:" | awk '{print $3}')
	echo $ret
}

function set_dumpsize
{
	typeset volume=$1

	if [[ -z $volume ]] ; then
		log_note "No volume specified."
		return 1
	fi

	log_must zfs set volsize=64m $volume

	output=$(dumpadm -d /dev/zvol/dsk/$volume 2>&1 | \
			tail -1 | awk '{print $3}')

	if [[ -n $output ]]; then
		(( output = output / 1024 / 1024 ))
		(( output = output + output / 5 ))
		log_must zfs set volsize=${output}m $volume
	fi
	return 0
}

function safe_dumpadm
{
	typeset device=$1

	if [[ -z $device || $device == "none" ]] ; then
		log_note "No dump device volume specified."
		return 1
	fi
	if [[ $device == "/dev/zvol/dsk/"* ]] ; then
		typeset volume=${device#/dev/zvol/dsk/}
		set_dumpsize $volume
		log_must dumpadm -d $device
	else
		log_must swapadd
		if ! is_swap_inuse $device ; then
			log_must swap -a $device
		fi
		log_must dumpadm -d swap
	fi
}

function is_zvol_dumpified
{
	typeset volume=$1

	if [[ -z $volume ]] ; then
		log_note "No volume specified."
		return 1
	fi

	zdb -dddd $volume 2 | grep "dumpsize" > /dev/null 2>&1
	return $?
}

function is_swap_inuse
{
	typeset device=$1

	if [[ -z $device ]] ; then
		log_note "No device specified."
		return 1
	fi

	swap -l | awk 'NR > 1 { print $1 }' | \
	    grep "^$device\$" > /dev/null 2>&1
	return $?
}