xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/slog/slog.kshlib (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
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 2008 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#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/slog/slog.cfg
33
34function setup
35{
36	log_must rm -rf $VDIR $VDIR2
37	log_must mkdir -p $VDIR $VDIR2
38	log_must truncate -s $MINVDEVSIZE $VDEV $SDEV $LDEV $VDEV2 $SDEV2 $LDEV2
39
40	return 0
41}
42
43function cleanup
44{
45	poolexists $TESTPOOL && destroy_pool $TESTPOOL
46	poolexists $TESTPOOL2 && destroy_pool $TESTPOOL2
47	rm -rf $TESTDIR $VDIR $VDIR2
48}
49
50#
51# Try zpool status/iostat for given pool
52#
53# $1 pool
54#
55function display_status
56{
57	typeset pool=$1
58
59	typeset -i ret=0
60	zpool status -xv $pool > /dev/null 2>&1
61	ret=$?
62
63	zpool iostat > /dev/null 2>&1
64	((ret |= $?))
65
66	typeset mntpnt=$(get_prop mountpoint $pool)
67	dd if=/dev/random of=$mntpnt/testfile.$$ &
68	typeset pid=$!
69
70	zpool iostat -v 1 3 > /dev/null
71	((ret |= $?))
72
73	kill -9 $pid
74
75	return $ret
76}
77
78#
79# Verify the give slog device have correct type and status
80#
81# $1 pool name
82# $2 device name
83# $3 device status
84# $4 device type
85#
86function verify_slog_device
87{
88	typeset pool=$1
89	typeset device=$2
90	typeset status=$3
91	typeset type=$4
92
93	if [[ -z $pool || -z $device || -z $status ]]; then
94		log_fail "Usage: verify_slog_device <pool> <device> " \
95			"<status> [type]"
96	fi
97
98	#
99	# Get all the slog devices and status table like below
100	#
101	# mirror:/disks/d ONLINE mirror:/disks/e ONLINE stripe:/disks/f ONLINE
102	#
103	set -A dev_stat_tab $(zpool status -v $pool | nawk 'BEGIN {start=0} \
104				/\tlogs/ {start=1}
105				/\tmirror/ || /\tspares/ || /^$/ {start=0}
106				(start==1) && /\t  (\/|[a-zA-Z])/ \
107					{print "stripe:" $1 " " $2}
108				(start==1) && /\t    (\/|[a-zA-Z])/ \
109					{print "mirror:" $1 " " $2}
110				# When hotspare is replacing
111				(start==1) && /\t      (\/|[a-zA-Z])/ \
112					{print "mirror:" $1 " " $2}'
113			     )
114
115	typeset -i i=0
116	typeset find=0
117	while (( i < ${#dev_stat_tab[@]} )); do
118		typeset dev=${dev_stat_tab[$i]}
119		typeset stat=${dev_stat_tab[((i+1))]}
120
121		case $dev in
122			stripe:$device)
123				if [[ "$type" == 'mirror' ]]; then
124					log_note "Unexpected type: mirror"
125					return 1
126				else
127					if [[ $stat != $status ]]; then
128						log_note "Status($stat) " \
129							"!= Expected stat($status)"
130						return 1
131					fi
132					return 0
133				fi
134				;;
135			mirror:$device)
136				if [[ -z "$type" || $type == 'stripe' ]]; then
137					log_note "Unexpected type: stripe"
138					return 1
139				else
140					if [[ $stat != $status ]]; then
141						log_note "Status($stat) " \
142							"!= Expected stat($status)"
143						return 1
144					fi
145					return 0
146				fi
147				;;
148		esac
149
150		((i += 2))
151	done
152
153	log_note "Can not find device: $device"
154
155	return 1
156}
157