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