1#!/bin/ksh -p
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#
14# Copyright (c) 2018 by Delphix. All rights reserved.
15#
16
17. $STF_SUITE/include/libtest.shlib
18. $STF_SUITE/tests/functional/cli_root/zpool_wait/zpool_wait.kshlib
19
20#
21# DESCRIPTION:
22# 'zpool wait' works when waiting for a device to be removed.
23#
24# STRATEGY:
25# 1. Create a pool with two disks and some data.
26# 2. Modify a tunable to make sure removal doesn't make any progress.
27# 3. Start removing one of the disks.
28# 4. Start 'zpool wait'.
29# 5. Sleep for a few seconds and check that the process is actually waiting.
30# 6. Modify tunable to allow removal to complete.
31# 7. Monitor the waiting process to make sure it returns neither too soon nor
32#    too late.
33# 8. Repeat 1-7, except using the '-w' flag for 'zpool remove' instead of using
34#    'zpool wait'.
35#
36
37function cleanup
38{
39	kill_if_running $pid
40	log_must set_tunable32 REMOVAL_SUSPEND_PROGRESS 0
41	poolexists $TESTPOOL && destroy_pool $TESTPOOL
42}
43
44function do_test
45{
46	typeset use_flag=$1
47
48	log_must zpool create -f $TESTPOOL $DISK1 $DISK2
49	log_must dd if=/dev/urandom of="/$TESTPOOL/testfile" bs=1k count=16k
50
51	# Start removal, but don't allow it to make any progress at first
52	log_must set_tunable32 REMOVAL_SUSPEND_PROGRESS 1
53
54	if $use_flag; then
55		log_bkgrnd zpool remove -w $TESTPOOL $DISK1
56		pid=$!
57
58		while ! is_pool_removing $TESTPOOL && proc_exists $pid; do
59			log_must sleep .5
60		done
61	else
62		log_must zpool remove $TESTPOOL $DISK1
63		log_bkgrnd zpool wait -t remove $TESTPOOL
64		pid=$!
65	fi
66
67	# Make sure the 'zpool wait' is actually waiting
68	log_must sleep 3
69	proc_must_exist $pid
70
71	# Unpause removal, and wait for it to finish
72	log_must set_tunable32 REMOVAL_SUSPEND_PROGRESS 0
73	check_while_waiting $pid "is_pool_removing $TESTPOOL"
74
75	log_must zpool destroy $TESTPOOL
76}
77
78log_onexit cleanup
79
80typeset pid
81
82do_test true
83do_test false
84
85log_pass "'zpool wait -t remove' and 'zpool remove -w' work."
86