1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
19#
20
21. $STF_SUITE/include/libtest.shlib
22. $STF_SUITE/include/math.shlib
23. $STF_SUITE/tests/functional/fault/fault.cfg
24
25#
26# DESCRIPTION:
27# Spare devices (both files and disks) can be shared among different ZFS pools.
28#
29# STRATEGY:
30# 1. Create two pools
31# 2. Add the same spare device to different pools
32# 3. Inject IO errors with a zinject error handler
33# 4. Start a scrub
34# 5. Verify the ZED kicks in a hot spare and check pool/device status
35# 6. Clear the fault
36# 7. Verify the hot spare is available and check pool/device status
37#
38
39verify_runnable "both"
40
41if is_linux; then
42	# Add one 512b spare device (4Kn would generate IO errors on replace)
43	# NOTE: must be larger than other "file" vdevs and minimum SPA devsize:
44	# add 32m of fudge
45	load_scsi_debug $(($MINVDEVSIZE/1024/1024+32)) 1 1 1 '512b'
46else
47	log_unsupported "scsi debug module unsupported"
48fi
49
50function cleanup
51{
52	log_must zinject -c all
53	destroy_pool $TESTPOOL
54	destroy_pool $TESTPOOL1
55	unload_scsi_debug
56	rm -f $SAFE_FILEDEVPOOL1 $SAFE_FILEDEVPOOL2 $FAIL_FILEDEVPOOL1 \
57	    $FAIL_FILEDEVPOOL2 $SPARE_FILEDEV
58}
59
60log_assert "Spare devices can be shared among different ZFS pools"
61log_onexit cleanup
62
63# Clear events from previous runs
64zed_events_drain
65
66SAFE_FILEDEVPOOL1="$TEST_BASE_DIR/file-safe-dev1"
67FAIL_FILEDEVPOOL1="$TEST_BASE_DIR/file-fail-dev1"
68SAFE_FILEDEVPOOL2="$TEST_BASE_DIR/file-safe-dev2"
69FAIL_FILEDEVPOOL2="$TEST_BASE_DIR/file-fail-dev2"
70SPARE_FILEDEV="$TEST_BASE_DIR/file-spare-dev"
71SPARE_DISKDEV="$(get_debug_device)"
72
73log_must truncate -s $MINVDEVSIZE $SAFE_FILEDEVPOOL1 $SAFE_FILEDEVPOOL2 $FAIL_FILEDEVPOOL1 $FAIL_FILEDEVPOOL2 $SPARE_FILEDEV
74
75for spare in $SPARE_FILEDEV $SPARE_DISKDEV; do
76	# 1. Create two pools
77	log_must zpool create -f $TESTPOOL mirror $SAFE_FILEDEVPOOL1 $FAIL_FILEDEVPOOL1
78	log_must zpool create -f $TESTPOOL1 mirror $SAFE_FILEDEVPOOL2 $FAIL_FILEDEVPOOL2
79
80	# 2. Add the same spare device to different pools
81	log_must_busy zpool add $TESTPOOL spare $spare
82	log_must_busy zpool add $TESTPOOL1 spare $spare
83	log_must wait_hotspare_state $TESTPOOL $spare "AVAIL"
84	log_must wait_hotspare_state $TESTPOOL1 $spare "AVAIL"
85
86	# 3. Inject IO errors with a zinject error handler
87	log_must zinject -d $FAIL_FILEDEVPOOL1 -e io -T all -f 100 $TESTPOOL
88	log_must zinject -d $FAIL_FILEDEVPOOL2 -e io -T all -f 100 $TESTPOOL1
89
90	# 4. Start a scrub
91	log_must zpool scrub $TESTPOOL
92	log_must zpool scrub $TESTPOOL1
93
94	# 5. Verify the ZED kicks in a hot spare and check pool/device status
95	log_note "Wait for ZED to auto-spare"
96	log_must wait_vdev_state $TESTPOOL $FAIL_FILEDEVPOOL1 "FAULTED" 60
97	log_must wait_vdev_state $TESTPOOL $spare "ONLINE" 60
98	log_must wait_hotspare_state $TESTPOOL $spare "INUSE"
99	log_must check_state $TESTPOOL "" "DEGRADED"
100
101	# 6. Clear the fault
102	log_must zinject -c all
103	log_must zpool clear $TESTPOOL $FAIL_FILEDEVPOOL1
104
105	# 7. Verify the hot spare is available and check pool/device status
106	log_must wait_vdev_state $TESTPOOL $FAIL_FILEDEVPOOL1 "ONLINE" 60
107	log_must wait_hotspare_state $TESTPOOL $spare "AVAIL"
108	log_must is_pool_resilvered $TESTPOOL
109	log_must check_state $TESTPOOL "" "ONLINE"
110
111	# Cleanup
112	destroy_pool $TESTPOOL
113	destroy_pool $TESTPOOL1
114done
115
116log_pass "Spare devices can be shared among different ZFS pools"
117