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 the pool
38# 4. Wipe and offline the scsi_debug disk
39# 5. Import the pool with missing disk
40# 6. Re-online the wiped scsi_debug disk
41# 7. Verify ZED detects the new blank disk and replaces the missing vdev
42# 8. Verify that the scsi_debug disk was re-partitioned
43#
44# Creates a raidz1 zpool using persistent /dev/disk/by-vdev path names
45# (ie not /dev/sdc)
46#
47# Auto-replace is opt in, and matches by phys_path.
48#
49
50verify_runnable "both"
51
52if ! is_physical_device $DISKS; then
53	log_unsupported "Unsupported disks for this test."
54fi
55
56function cleanup
57{
58	zpool status $TESTPOOL
59	destroy_pool $TESTPOOL
60	sed -i '/alias scsidebug/d' $VDEVID_CONF
61	unload_scsi_debug
62}
63
64log_assert "Testing automated auto-replace FMA test"
65log_onexit cleanup
66
67load_scsi_debug $SDSIZE $SDHOSTS $SDTGTS $SDLUNS '512b'
68SD=$(get_debug_device)
69SD_DEVICE_ID=$(get_persistent_disk_name $SD)
70SD_HOST=$(get_scsi_host $SD)
71
72# Register vdev_id alias for scsi_debug device to create a persistent path
73echo "alias scsidebug /dev/disk/by-id/$SD_DEVICE_ID" >>$VDEVID_CONF
74block_device_wait
75
76SD_DEVICE=$(udevadm info -q all -n $DEV_DSKDIR/$SD | \
77    awk -F'=' '/ID_VDEV=/ {print $2; exit}')
78[ -z $SD_DEVICE ] && log_fail "vdev rule was not registered properly"
79
80log_must zpool events -c
81log_must zpool create -f $TESTPOOL raidz1 $SD_DEVICE $DISK1 $DISK2 $DISK3
82
83# Auto-replace is opt-in so need to set property
84log_must zpool set autoreplace=on $TESTPOOL
85
86# Add some data to the pool
87log_must zfs create $TESTPOOL/fs
88log_must fill_fs /$TESTPOOL/fs 4 100 4096 512 Z
89log_must zpool export $TESTPOOL
90
91# Record the partition UUID for later comparison
92part_uuid=$(udevadm info --query=property --property=ID_PART_TABLE_UUID \
93    --value /dev/disk/by-id/$SD_DEVICE_ID)
94[[ -z "$part_uuid" ]] || log_note original disk GPT uuid ${part_uuid}
95
96#
97# Wipe and offline the disk
98#
99# Note that it is not enough to zero the disk to expunge the partitions.
100# You also need to inform the kernel (e.g., 'hdparm -z' or 'partprobe').
101#
102# Using partprobe is overkill and hdparm is not as common as wipefs. So
103# we use wipefs which lets the kernel know the partition was removed
104# from the device (i.e., calls BLKRRPART ioctl).
105#
106log_must dd if=/dev/zero of=/dev/disk/by-id/$SD_DEVICE_ID bs=1M count=$SDSIZE
107log_must /usr/sbin/wipefs -a /dev/disk/by-id/$SD_DEVICE_ID
108remove_disk $SD
109block_device_wait
110
111# Re-import pool with drive missing
112log_must zpool import $TESTPOOL
113log_must check_state $TESTPOOL "" "DEGRADED"
114block_device_wait
115
116# Online an empty disk in the same physical location
117insert_disk $SD $SD_HOST
118
119# Wait for the new disk to be online and replaced
120log_must wait_vdev_state $TESTPOOL "scsidebug" "ONLINE" 60
121log_must wait_replacing $TESTPOOL 60
122
123# Validate auto-replace was successful
124log_must check_state $TESTPOOL "" "ONLINE"
125
126#
127# Confirm the partition UUID changed so we know the new disk was relabeled
128#
129# Note: some older versions of udevadm don't support "--property" option so
130# we'll # skip this test when it is not supported
131#
132if [ ! -z "$part_uuid" ]; then
133	new_uuid=$(udevadm info --query=property --property=ID_PART_TABLE_UUID \
134	    --value /dev/disk/by-id/$SD_DEVICE_ID)
135	log_note new disk GPT uuid ${new_uuid}
136	[[ "$part_uuid" = "$new_uuid" ]] && \
137	    log_fail "The new disk was not relabeled as expected"
138fi
139
140log_pass "Auto-replace test successful"
141