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# Copyright (c) 2017 by Intel Corporation. All rights reserved.
24#
25
26. $STF_SUITE/include/libtest.shlib
27. $STF_SUITE/tests/functional/fault/fault.cfg
28
29#
30# DESCRIPTION:
31# Testing Fault Management Agent ZED Logic - Automated Auto-Replace Test.
32#
33# STRATEGY:
34# 1. Update /etc/zfs/vdev_id.conf with scsidebug alias for a persistent path.
35#    This creates keys ID_VDEV and ID_VDEV_PATH and set phys_path="scsidebug".
36# 2. Create a pool and set autoreplace=on (auto-replace is opt-in)
37# 3. Export a pool
38# 4. Wipe and offline the scsi_debug disk
39# 5. Import pool with missing disk
40# 6. Re-online the wiped scsi_debug disk
41# 7. Verify the ZED detects the new unused disk and adds it back to the pool
42#
43# Creates a raidz1 zpool using persistent disk path names
44# (ie not /dev/sdc)
45#
46# Auto-replace is opt in, and matches by phys_path.
47#
48
49verify_runnable "both"
50
51if ! is_physical_device $DISKS; then
52	log_unsupported "Unsupported disks for this test."
53fi
54
55function cleanup
56{
57	destroy_pool $TESTPOOL
58	sed -i '/alias scsidebug/d' $VDEVID_CONF
59	unload_scsi_debug
60}
61
62log_assert "Testing automated auto-replace FMA test"
63log_onexit cleanup
64
65load_scsi_debug $SDSIZE $SDHOSTS $SDTGTS $SDLUNS '512b'
66SD=$(get_debug_device)
67SD_DEVICE_ID=$(get_persistent_disk_name $SD)
68SD_HOST=$(get_scsi_host $SD)
69
70# Register vdev_id alias for scsi_debug device to create a persistent path
71echo "alias scsidebug /dev/disk/by-id/$SD_DEVICE_ID" >>$VDEVID_CONF
72block_device_wait
73
74SD_DEVICE=$(udevadm info -q all -n $DEV_DSKDIR/$SD | \
75    awk -F'=' '/ID_VDEV=/ {print $2; exit}')
76[ -z $SD_DEVICE ] && log_fail "vdev rule was not registered properly"
77
78log_must zpool events -c
79log_must zpool create -f $TESTPOOL raidz1 $SD_DEVICE $DISK1 $DISK2 $DISK3
80
81# Auto-replace is opt-in so need to set property
82log_must zpool set autoreplace=on $TESTPOOL
83
84# Add some data to the pool
85log_must mkfile $FSIZE /$TESTPOOL/data
86log_must zpool export $TESTPOOL
87
88# Wipe and offline the disk
89log_must dd if=/dev/zero of=/dev/disk/by-id/$SD_DEVICE_ID bs=1M count=$SDSIZE
90remove_disk $SD
91block_device_wait
92
93# Re-import pool with drive missing
94log_must zpool import $TESTPOOL
95log_must check_state $TESTPOOL "" "DEGRADED"
96block_device_wait
97
98# Online an empty disk in the same physical location
99insert_disk $SD $SD_HOST
100
101# Wait for the new disk to be online and replaced
102log_must wait_vdev_state $TESTPOOL "scsidebug" "ONLINE" $MAXTIMEOUT
103log_must wait_replacing $TESTPOOL
104
105# Validate auto-replace was successful
106log_must check_state $TESTPOOL "" "ONLINE"
107
108log_pass "Auto-replace test successful"
109