1*e4c795beSTom Caputi#
2*e4c795beSTom Caputi# This file and its contents are supplied under the terms of the
3*e4c795beSTom Caputi# Common Development and Distribution License ("CDDL"), version 1.0.
4*e4c795beSTom Caputi# You may only use this file in accordance with the terms of version
5*e4c795beSTom Caputi# 1.0 of the CDDL.
6*e4c795beSTom Caputi#
7*e4c795beSTom Caputi# A full copy of the text of the CDDL should have accompanied this
8*e4c795beSTom Caputi# source.  A copy of the CDDL is also available via the Internet at
9*e4c795beSTom Caputi# http://www.illumos.org/license/CDDL.
10*e4c795beSTom Caputi#
11*e4c795beSTom Caputi#
12*e4c795beSTom Caputi# Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
13*e4c795beSTom Caputi#
14*e4c795beSTom Caputi
15*e4c795beSTom Caputi. $STF_SUITE/tests/functional/cli_root/zpool_reopen/zpool_reopen.cfg
16*e4c795beSTom Caputi
17*e4c795beSTom Caputi#
18*e4c795beSTom Caputi# Clear labels on the given disks
19*e4c795beSTom Caputi#
20*e4c795beSTom Caputifunction clear_labels #disks
21*e4c795beSTom Caputi{
22*e4c795beSTom Caputi	for disk in $@; do
23*e4c795beSTom Caputi		if ( is_loop_device $disk ) || ( is_mpath_device $disk ); then
24*e4c795beSTom Caputi			zpool labelclear -f /dev/$disk
25*e4c795beSTom Caputi		else
26*e4c795beSTom Caputi			zpool labelclear -f /dev/${disk}1
27*e4c795beSTom Caputi		fi
28*e4c795beSTom Caputi	done
29*e4c795beSTom Caputi}
30*e4c795beSTom Caputi
31*e4c795beSTom Caputi#
32*e4c795beSTom Caputi# Set the REMOVED_DISK and REMOVED_DISK_ID constants for device
33*e4c795beSTom Caputi# used for re-plugging. When the disk is loop device use the
34*e4c795beSTom Caputi# scsi_debug emulated drive. Otherwise use the real drive.
35*e4c795beSTom Caputi#
36*e4c795beSTom Caputifunction set_removed_disk
37*e4c795beSTom Caputi{
38*e4c795beSTom Caputi	if is_loop_device $DISK1; then
39*e4c795beSTom Caputi		export REMOVED_DISK=$(get_debug_device)
40*e4c795beSTom Caputi		export REMOVED_DISK_ID=$(get_persistent_disk_name $REMOVED_DISK)
41*e4c795beSTom Caputi	elif ( is_real_device $DISK1 ) || ( is_mpath_device $DISK1 ); then
42*e4c795beSTom Caputi		export REMOVED_DISK="$DISK1"
43*e4c795beSTom Caputi		export REMOVED_DISK_ID=${devs_id[0]}
44*e4c795beSTom Caputi	else
45*e4c795beSTom Caputi		log_fail "No drives that supports removal"
46*e4c795beSTom Caputi	fi
47*e4c795beSTom Caputi}
48*e4c795beSTom Caputi
49*e4c795beSTom Caputi#
50*e4c795beSTom Caputi# Generate random file of the given size in MiB
51*e4c795beSTom Caputi#
52*e4c795beSTom Caputifunction generate_random_file #path size_mb
53*e4c795beSTom Caputi{
54*e4c795beSTom Caputi	typeset path=$1
55*e4c795beSTom Caputi	typeset -i size_mb=$2
56*e4c795beSTom Caputi	file_write -o create -f $path -b 1048576 -s0 -c $size_mb -d R
57*e4c795beSTom Caputi}
58*e4c795beSTom Caputi
59*e4c795beSTom Caputi#
60*e4c795beSTom Caputi# Wait until specific event or timeout occur.
61*e4c795beSTom Caputi#
62*e4c795beSTom Caputi# The passed function is executed with pool name as argument
63*e4c795beSTom Caputi# with an interval of 1 second until it succeeds or until the
64*e4c795beSTom Caputi# timeout occurs.
65*e4c795beSTom Caputi# It returns 1 on timeout or 0 otherwise.
66*e4c795beSTom Caputi#
67*e4c795beSTom Caputifunction wait_for_action #pool timeout function
68*e4c795beSTom Caputi{
69*e4c795beSTom Caputi	typeset pool=$1
70*e4c795beSTom Caputi	typeset -i timeout=$2
71*e4c795beSTom Caputi	typeset func=$3
72*e4c795beSTom Caputi
73*e4c795beSTom Caputi	while [ $timeout -gt 0 ]; do
74*e4c795beSTom Caputi		(( --timeout ))
75*e4c795beSTom Caputi		if ( $func $pool ); then
76*e4c795beSTom Caputi			return 0
77*e4c795beSTom Caputi		fi
78*e4c795beSTom Caputi		sleep 1
79*e4c795beSTom Caputi	done
80*e4c795beSTom Caputi
81*e4c795beSTom Caputi	return 1
82*e4c795beSTom Caputi}
83*e4c795beSTom Caputi
84*e4c795beSTom Caputi#
85*e4c795beSTom Caputi# Helpers for wait_for_action function:
86*e4c795beSTom Caputi# wait_for_resilver_start - wait until resilver is started
87*e4c795beSTom Caputi# wait_for_resilver_end - wait until resilver is finished
88*e4c795beSTom Caputi# wait_for_scrub_end - wait until scrub is finished
89*e4c795beSTom Caputi#
90*e4c795beSTom Caputifunction wait_for_resilver_start #pool timeout
91*e4c795beSTom Caputi{
92*e4c795beSTom Caputi	wait_for_action $1 $2 is_pool_resilvering
93*e4c795beSTom Caputi	return $?
94*e4c795beSTom Caputi}
95*e4c795beSTom Caputi
96*e4c795beSTom Caputifunction wait_for_resilver_end #pool timeout
97*e4c795beSTom Caputi{
98*e4c795beSTom Caputi	wait_for_action $1 $2 is_pool_resilvered
99*e4c795beSTom Caputi	return $?
100*e4c795beSTom Caputi}
101*e4c795beSTom Caputi
102*e4c795beSTom Caputifunction wait_for_scrub_end #pool timeout
103*e4c795beSTom Caputi{
104*e4c795beSTom Caputi	wait_for_action $1 $2 is_pool_scrubbed
105*e4c795beSTom Caputi	return $?
106*e4c795beSTom Caputi}
107*e4c795beSTom Caputi
108*e4c795beSTom Caputi#
109*e4c795beSTom Caputi# Check if scan action has been restarted on the given pool
110*e4c795beSTom Caputi#
111*e4c795beSTom Caputi
112*e4c795beSTom Caputifunction is_scan_restarted #pool
113*e4c795beSTom Caputi{
114*e4c795beSTom Caputi	typeset pool=$1
115*e4c795beSTom Caputi	zpool history -i $pool | grep -q "scan aborted, restarting"
116*e4c795beSTom Caputi	return $?
117*e4c795beSTom Caputi}
118*e4c795beSTom Caputi
119*e4c795beSTom Caputifunction is_deferred_scan_started #pool
120*e4c795beSTom Caputi{
121*e4c795beSTom Caputi	typeset pool=$1
122*e4c795beSTom Caputi	zpool history -i $pool | grep -q "starting deferred resilver"
123*e4c795beSTom Caputi	return $?
124*e4c795beSTom Caputi}
125