1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or https://opensource.org/licenses/CDDL-1.0.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2016 by Delphix. All rights reserved.
25# Copyright (C) 2023 Lawrence Livermore National Security, LLC.
26#
27. $STF_SUITE/include/libtest.shlib
28. $STF_SUITE/tests/functional/cli_root/zpool_initialize/zpool_initialize.kshlib
29
30#
31# DESCRIPTION:
32# Starting, stopping, uninitializing, and restart an initialize works.
33#
34# STRATEGY:
35# 1. Create a one-disk pool.
36# 2. Verify uninitialize succeeds for uninitialized pool.
37# 3. Verify pool wide cancel|suspend + uninit
38#   a. Start initializing and verify that initializing is active.
39#   b. Verify uninitialize fails when actively initializing.
40#   c. Cancel or suspend initializing and verify that initializing is not active.
41#   d. Verify uninitialize succeeds after being cancelled.
42# 4. Verify per-disk cancel|suspend + uninit
43#
44
45DISK1="$(echo $DISKS | cut -d' ' -f1)"
46DISK2="$(echo $DISKS | cut -d' ' -f2)"
47DISK3="$(echo $DISKS | cut -d' ' -f3)"
48
49function status_check # pool disk1-state disk2-state disk3-state
50{
51        typeset pool="$1"
52        typeset disk1_state="$2"
53        typeset disk2_state="$3"
54        typeset disk3_state="$4"
55
56	state=$(zpool status -i "$pool" | grep "$DISK1" | grep "$disk1_state")
57        if [[ -z "$state" ]]; then
58		log_fail "DISK1 state; expected='$disk1_state' got '$state'"
59	fi
60
61	state=$(zpool status -i "$pool" | grep "$DISK2" | grep "$disk2_state")
62        if [[ -z "$state" ]]; then
63		log_fail "DISK2 state; expected='$disk2_state' got '$state'"
64	fi
65
66	state=$(zpool status -i "$pool" | grep "$DISK3" | grep "$disk3_state")
67        if [[ -z "$state" ]]; then
68		log_fail "DISK3 state; expected='$disk3_state' got '$state'"
69	fi
70}
71
72function status_check_all # pool disk-state
73{
74        typeset pool="$1"
75        typeset disk_state="$2"
76
77	status_check "$pool" "$disk_state" "$disk_state" "$disk_state"
78}
79
80# 1. Create a one-disk pool.
81log_must zpool create -f $TESTPOOL $DISK1 $DISK2 $DISK3
82status_check_all $TESTPOOL "uninitialized"
83
84# 2. Verify uninitialize succeeds for uninitialized pool.
85log_must zpool initialize -u $TESTPOOL
86status_check_all $TESTPOOL "uninitialized"
87
88# 3. Verify pool wide cancel + uninit
89log_must zpool initialize $TESTPOOL
90status_check_all $TESTPOOL "[[:digit:]]* initialized"
91
92log_mustnot zpool initialize -u $TESTPOOL
93status_check_all $TESTPOOL "[[:digit:]]* initialized"
94
95log_must zpool initialize -c $TESTPOOL
96status_check_all $TESTPOOL "uninitialized"
97
98log_must zpool initialize -u $TESTPOOL
99status_check_all $TESTPOOL "uninitialized"
100
101# 3. Verify pool wide suspend + uninit
102log_must zpool initialize $TESTPOOL
103status_check_all $TESTPOOL "[[:digit:]]* initialized"
104
105log_mustnot zpool initialize -u $TESTPOOL
106status_check_all $TESTPOOL "[[:digit:]]* initialized"
107
108log_must zpool initialize -s $TESTPOOL
109status_check_all $TESTPOOL "suspended"
110
111log_must zpool initialize -u $TESTPOOL
112status_check_all $TESTPOOL "uninitialized"
113
114# 4. Verify per-disk cancel|suspend + uninit
115log_must zpool initialize $TESTPOOL
116status_check_all $TESTPOOL "[[:digit:]]* initialized"
117
118log_must zpool initialize -c $TESTPOOL $DISK1
119log_must zpool initialize -s $TESTPOOL $DISK2
120log_mustnot zpool initialize -u $TESTPOOL $DISK3
121status_check $TESTPOOL "uninitialized" "suspended" "[[:digit:]]* initialized"
122
123log_must zpool initialize -u $TESTPOOL $DISK1
124status_check $TESTPOOL "uninitialized" "suspended" "[[:digit:]]* initialized"
125
126log_must zpool initialize -u $TESTPOOL $DISK2
127status_check $TESTPOOL "uninitialized" "uninitialized" "[[:digit:]]* initialized"
128
129log_must zpool initialize $TESTPOOL $DISK1
130status_check $TESTPOOL "[[:digit:]]* initialized" "uninitialized" "[[:digit:]]* initialized"
131
132log_must zpool initialize $TESTPOOL $DISK2
133status_check_all $TESTPOOL "[[:digit:]]* initialized"
134
135log_must zpool initialize -s $TESTPOOL
136status_check_all $TESTPOOL "suspended"
137
138log_must zpool initialize -u $TESTPOOL $DISK1 $DISK2 $DISK3
139status_check_all $TESTPOOL "uninitialized"
140
141log_pass "Initialize start + cancel/suspend + uninit + start works"
142