xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/replacement/detach.ksh (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3#
4# CDDL HEADER START
5#
6# The contents of this file are subject to the terms of the
7# Common Development and Distribution License (the "License").
8# You may not use this file except in compliance with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or https://opensource.org/licenses/CDDL-1.0.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23
24#
25# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27#
28
29#
30# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31#
32
33. $STF_SUITE/include/libtest.shlib
34. $STF_SUITE/tests/functional/replacement/replacement.cfg
35
36#
37# DESCRIPTION:
38# 	Detaching disks during I/O should pass for supported pools.
39#
40# STRATEGY:
41#	1. Create multidisk pools (stripe/mirror/raidz/draid) and
42#	   start some random I/O
43#	2. Detach a disk from the pool.
44#	3. Verify the integrity of the file system and the resilvering.
45#
46
47verify_runnable "global"
48
49function cleanup
50{
51	if [[ -n "$child_pids" ]]; then
52		for wait_pid in $child_pids
53		do
54		        kill $wait_pid
55		done
56	fi
57
58	if poolexists $TESTPOOL1; then
59		destroy_pool $TESTPOOL1
60	fi
61
62	[[ -e $TESTDIR ]] && log_must rm -rf $TESTDIR/*
63}
64
65log_assert "Replacing a disk during I/O completes."
66
67options=""
68options_display="default options"
69
70log_onexit cleanup
71
72[[ -n "$HOLES_FILESIZE" ]] && options=" $options -f $HOLES_FILESIZE "
73
74[[ -n "$HOLES_BLKSIZE" ]] && options="$options -b $HOLES_BLKSIZE "
75
76[[ -n "$HOLES_COUNT" ]] && options="$options -c $HOLES_COUNT "
77
78[[ -n "$HOLES_SEED" ]] && options="$options -s $HOLES_SEED "
79
80[[ -n "$HOLES_FILEOFFSET" ]] && options="$options -o $HOLES_FILEOFFSET "
81
82ptions="$options -r "
83
84[[ -n "$options" ]] && options_display=$options
85
86child_pids=""
87
88function detach_test
89{
90	typeset -i iters=2
91	typeset -i index=0
92	typeset disk1=$1
93
94	typeset i=0
95	while [[ $i -lt $iters ]]; do
96		log_note "Invoking file_trunc with: $options_display"
97		file_trunc $options $TESTDIR/$TESTFILE.$i &
98		typeset pid=$!
99
100		sleep 1
101
102		child_pids="$child_pids $pid"
103		((i = i + 1))
104	done
105
106	log_must zpool detach $TESTPOOL1 $disk1
107
108	sleep 10
109
110	for wait_pid in $child_pids
111	do
112		kill $wait_pid
113	done
114	child_pids=""
115
116        log_must zpool export $TESTPOOL1
117        log_must zpool import -d $TESTDIR $TESTPOOL1
118        log_must zfs umount $TESTPOOL1/$TESTFS1
119        log_must zdb -cdui $TESTPOOL1/$TESTFS1
120        log_must zfs mount $TESTPOOL1/$TESTFS1
121}
122
123specials_list=""
124i=0
125while [[ $i != 3 ]]; do
126	truncate -s $MINVDEVSIZE $TESTDIR/$TESTFILE1.$i
127	specials_list="$specials_list $TESTDIR/$TESTFILE1.$i"
128
129	((i = i + 1))
130done
131
132create_pool $TESTPOOL1 mirror $specials_list
133log_must zfs create $TESTPOOL1/$TESTFS1
134log_must zfs set mountpoint=$TESTDIR1 $TESTPOOL1/$TESTFS1
135
136detach_test $TESTDIR/$TESTFILE1.1
137
138log_mustnot eval "zpool iostat -v $TESTPOOL1 | grep \"$TESTFILE1.1\""
139
140destroy_pool $TESTPOOL1
141
142log_note "Verify 'zpool detach' fails with non-mirrors."
143
144for type in "" "raidz" "raidz1" "draid"; do
145	create_pool $TESTPOOL1 $type $specials_list
146	log_must zfs create $TESTPOOL1/$TESTFS1
147	log_must zfs set mountpoint=$TESTDIR1 $TESTPOOL1/$TESTFS1
148
149	log_mustnot zpool detach $TESTDIR/$TESTFILE1.1
150
151	log_must eval "zpool iostat -v $TESTPOOL1 | grep \"$TESTFILE1.1\""
152
153	destroy_pool $TESTPOOL1
154done
155
156log_pass
157