xref: /freebsd/tests/sys/cddl/zfs/tests/zvol/zvol_common.kshlib (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
12fae26bdSAlan Somers# vim: filetype=sh
22fae26bdSAlan Somers#
32fae26bdSAlan Somers# CDDL HEADER START
42fae26bdSAlan Somers#
52fae26bdSAlan Somers# The contents of this file are subject to the terms of the
62fae26bdSAlan Somers# Common Development and Distribution License (the "License").
72fae26bdSAlan Somers# You may not use this file except in compliance with the License.
82fae26bdSAlan Somers#
92fae26bdSAlan Somers# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
102fae26bdSAlan Somers# or http://www.opensolaris.org/os/licensing.
112fae26bdSAlan Somers# See the License for the specific language governing permissions
122fae26bdSAlan Somers# and limitations under the License.
132fae26bdSAlan Somers#
142fae26bdSAlan Somers# When distributing Covered Code, include this CDDL HEADER in each
152fae26bdSAlan Somers# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
162fae26bdSAlan Somers# If applicable, add the following below this CDDL HEADER, with the
172fae26bdSAlan Somers# fields enclosed by brackets "[]" replaced with your own identifying
182fae26bdSAlan Somers# information: Portions Copyright [yyyy] [name of copyright owner]
192fae26bdSAlan Somers#
202fae26bdSAlan Somers# CDDL HEADER END
212fae26bdSAlan Somers#
222fae26bdSAlan Somers
232fae26bdSAlan Somers#
242fae26bdSAlan Somers# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
252fae26bdSAlan Somers# Use is subject to license terms.
262fae26bdSAlan Somers
272fae26bdSAlan Somers. $STF_SUITE/include/libtest.kshlib
282fae26bdSAlan Somers
292fae26bdSAlan Somers#
302fae26bdSAlan Somers# Create a simple zvol volume
312fae26bdSAlan Somers#
322fae26bdSAlan Somers# Where disk_device: is the name of the disk to be used
332fae26bdSAlan Somers#       volume_size: is the size of the volume, e.g. 2G
342fae26bdSAlan Somers#
352fae26bdSAlan Somersfunction default_zvol_setup # disk_device volume_size
362fae26bdSAlan Somers{
372fae26bdSAlan Somers        typeset disk=$1
382fae26bdSAlan Somers        typeset size=$2
392fae26bdSAlan Somers	typeset savedumpdev
402fae26bdSAlan Somers	typeset -i output
412fae26bdSAlan Somers
422fae26bdSAlan Somers        create_pool $TESTPOOL "$disk"
432fae26bdSAlan Somers
442fae26bdSAlan Somers        log_must $ZFS create -V $size $TESTPOOL/$TESTVOL
452fae26bdSAlan Somers
462fae26bdSAlan Somers	if [[ -n $DUMPADM ]]; then
472fae26bdSAlan Somers		if is_dumpswap_supported $TESTPOOL ; then
482fae26bdSAlan Somers			set_dumpsize $TESTPOOL/$TESTVOL
492fae26bdSAlan Somers		fi
502fae26bdSAlan Somers	fi
512fae26bdSAlan Somers}
522fae26bdSAlan Somers
532fae26bdSAlan Somers#
542fae26bdSAlan Somers# Destroy the default zvol which was setup using
552fae26bdSAlan Somers# default_zvol_setup().
562fae26bdSAlan Somers#
572fae26bdSAlan Somersfunction default_zvol_cleanup
582fae26bdSAlan Somers{
592fae26bdSAlan Somers        if datasetexists $TESTPOOL/$TESTVOL ; then
602fae26bdSAlan Somers		log_must $ZFS destroy $TESTPOOL/$TESTVOL
612fae26bdSAlan Somers	fi
622fae26bdSAlan Somers
632fae26bdSAlan Somers        destroy_pool $TESTPOOL
642fae26bdSAlan Somers}
652fae26bdSAlan Somers
662fae26bdSAlan Somers#
672fae26bdSAlan Somers# Check if the given pool support "Swap and crash dumps"
682fae26bdSAlan Somers#
692fae26bdSAlan Somersfunction is_dumpswap_supported #pool
702fae26bdSAlan Somers{
712fae26bdSAlan Somers	typeset pool=$1
722fae26bdSAlan Somers
732fae26bdSAlan Somers	if [[ -z $pool ]] ; then
742fae26bdSAlan Somers		log_fail "No pool given."
752fae26bdSAlan Somers	fi
762fae26bdSAlan Somers
772fae26bdSAlan Somers	typeset -i SPA_VER_DUMPSWAP=10
782fae26bdSAlan Somers        typeset -i vp=$(get_pool_prop version $pool)
792fae26bdSAlan Somers
802fae26bdSAlan Somers	if (( vp >= SPA_VER_DUMPSWAP )) ; then
812fae26bdSAlan Somers		return 0
822fae26bdSAlan Somers	fi
832fae26bdSAlan Somers
842fae26bdSAlan Somers	return 1
852fae26bdSAlan Somers}
862fae26bdSAlan Somers
872fae26bdSAlan Somersfunction get_dumpdevice
882fae26bdSAlan Somers{
892fae26bdSAlan Somers	typeset ret=$($DUMPADM | $GREP "Dump device:" | $AWK '{print $3}')
902fae26bdSAlan Somers	print $ret
912fae26bdSAlan Somers}
922fae26bdSAlan Somers
932fae26bdSAlan Somersfunction set_dumpsize
942fae26bdSAlan Somers{
952fae26bdSAlan Somers	typeset volume=$1
962fae26bdSAlan Somers
972fae26bdSAlan Somers	if [[ -z $volume ]] ; then
982fae26bdSAlan Somers		log_note "No volume specified."
992fae26bdSAlan Somers		return 1
1002fae26bdSAlan Somers	fi
1012fae26bdSAlan Somers
1022fae26bdSAlan Somers	log_must $ZFS set volsize=64m $volume
1032fae26bdSAlan Somers
104*5a2fc464SAndriy Gapon	output=$($DUMPADM -d /dev/zvol/$volume 2>&1 | \
1052fae26bdSAlan Somers			$TAIL -1 | $AWK '{print $3}')
1062fae26bdSAlan Somers
1072fae26bdSAlan Somers	if [[ -n $output ]]; then
1082fae26bdSAlan Somers		(( output = output / 1024 / 1024 ))
1092fae26bdSAlan Somers		(( output = output + output / 5 ))
1102fae26bdSAlan Somers		log_must $ZFS set volsize=${output}m $volume
1112fae26bdSAlan Somers	fi
1122fae26bdSAlan Somers	return 0
1132fae26bdSAlan Somers}
1142fae26bdSAlan Somers
1152fae26bdSAlan Somersfunction safe_dumpadm
1162fae26bdSAlan Somers{
1172fae26bdSAlan Somers	typeset device=$1
1182fae26bdSAlan Somers
1192fae26bdSAlan Somers	if [[ -z $device || $device == "none" ]] ; then
1202fae26bdSAlan Somers		log_note "No dump device volume specified."
1212fae26bdSAlan Somers		return 1
1222fae26bdSAlan Somers	fi
123*5a2fc464SAndriy Gapon	if [[ $device == "/dev/zvol/"* ]] ; then
124*5a2fc464SAndriy Gapon		typeset volume=${device#/dev/zvol/}
1252fae26bdSAlan Somers		set_dumpsize $volume
1262fae26bdSAlan Somers		log_must $DUMPADM -d $device
1272fae26bdSAlan Somers	else
1282fae26bdSAlan Somers		log_must $SWAPADD
1292fae26bdSAlan Somers		if ! is_swap_inuse $device ; then
1302fae26bdSAlan Somers			log_must $SWAP -a $device
1312fae26bdSAlan Somers		fi
1322fae26bdSAlan Somers		log_must $DUMPADM -d swap
1332fae26bdSAlan Somers	fi
1342fae26bdSAlan Somers}
1352fae26bdSAlan Somers
1362fae26bdSAlan Somersfunction is_zvol_dumpified
1372fae26bdSAlan Somers{
1382fae26bdSAlan Somers	typeset volume=$1
1392fae26bdSAlan Somers
1402fae26bdSAlan Somers	if [[ -z $volume ]] ; then
1412fae26bdSAlan Somers		log_note "No volume specified."
1422fae26bdSAlan Somers		return 1
1432fae26bdSAlan Somers	fi
1442fae26bdSAlan Somers
1452fae26bdSAlan Somers	$ZDB -dddd $volume 2 | $GREP "dumpsize" > /dev/null 2>&1
1462fae26bdSAlan Somers	return $?
1472fae26bdSAlan Somers}
1482fae26bdSAlan Somers
1492fae26bdSAlan Somersfunction is_swap_inuse
1502fae26bdSAlan Somers{
1512fae26bdSAlan Somers	typeset device=$1
1522fae26bdSAlan Somers
1532fae26bdSAlan Somers	if [[ -z $device ]] ; then
1542fae26bdSAlan Somers		log_note "No device specified."
1552fae26bdSAlan Somers		return 1
1562fae26bdSAlan Somers	fi
1572fae26bdSAlan Somers
1582fae26bdSAlan Somers	$SWAP -l | $GREP -w $device > /dev/null 2>&1
1592fae26bdSAlan Somers	return $?
1602fae26bdSAlan Somers}
161