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# Copyright (c) 2023 by Klara, Inc. All rights reserved.
25#
26
27. $STF_SUITE/include/libtest.shlib
28. $STF_SUITE/tests/functional/fault/fault.cfg
29
30#
31# DESCRIPTION:
32# Testing Fault Management Agent ZED Logic - Automated Auto-Replace Test.
33# Verifys that auto-replace works with by-id paths.
34#
35# STRATEGY:
36# 1. Update /etc/zfs/vdev_id.conf with scsidebug alias for a persistent path.
37#    This creates keys ID_VDEV and ID_VDEV_PATH and set phys_path="scsidebug".
38# 2. Create a pool and set autoreplace=on (auto-replace is opt-in)
39# 3. Export the pool
40# 4. Wipe and offline the scsi_debug disk
41# 5. Import the pool with missing disk
42# 6. Re-online the wiped scsi_debug disk with a new serial number
43# 7. Verify ZED detects the new blank disk and replaces the missing vdev
44# 8. Verify that the scsi_debug disk was re-partitioned
45#
46# Creates a raidz1 zpool using persistent /dev/disk/by-id path names
47#
48# Auto-replace is opt in, and matches by phys_path.
49#
50
51verify_runnable "both"
52
53if ! is_physical_device $DISKS; then
54	log_unsupported "Unsupported disks for this test."
55fi
56
57function cleanup
58{
59	zpool status $TESTPOOL
60	destroy_pool $TESTPOOL
61	sed -i '/alias scsidebug/d' $VDEVID_CONF
62	unload_scsi_debug
63}
64
65#
66# Wait until a vdev transitions to its replacement vdev
67#
68# Return 0 when vdev reaches expected state, 1 on timeout.
69#
70# Note: index +2 is to skip over root and raidz-0 vdevs
71#
72function wait_vdev_online # pool index oldguid timeout
73{
74	typeset pool=$1
75	typeset -i index=$2+2
76	typeset guid=$3
77	typeset timeout=${4:-60}
78	typeset -i i=0
79
80	while [[ $i -lt $timeout ]]; do
81		vdev_guids=( $(zpool get -H -o value guid $pool all-vdevs) )
82
83		if [ "${vdev_guids[$index]}" != "${guid}" ]; then
84			log_note "new vdev[$((index-2))]: ${vdev_guids[$index]}, replacing ${guid}"
85			return 0
86		fi
87
88		i=$((i+1))
89		sleep 1
90	done
91
92	return 1
93}
94log_assert "automated auto-replace with by-id paths"
95log_onexit cleanup
96
97load_scsi_debug $SDSIZE $SDHOSTS $SDTGTS $SDLUNS '512b'
98SD=$(get_debug_device)
99SD_DEVICE_ID=$(get_persistent_disk_name $SD)
100SD_HOST=$(get_scsi_host $SD)
101
102# Register vdev_id alias for scsi_debug device to create a persistent path
103echo "alias scsidebug /dev/disk/by-id/$SD_DEVICE_ID" >>$VDEVID_CONF
104block_device_wait
105
106SD_DEVICE=$(udevadm info -q all -n $DEV_DSKDIR/$SD | \
107    awk -F'=' '/ID_VDEV=/ {print $2; exit}')
108[ -z $SD_DEVICE ] && log_fail "vdev rule was not registered properly"
109
110log_must zpool events -c
111log_must zpool create -f $TESTPOOL raidz1 $SD_DEVICE_ID $DISK1 $DISK2 $DISK3
112
113vdev_guid=$(zpool get guid -H -o value $TESTPOOL $SD_DEVICE_ID)
114log_note original vdev guid ${vdev_guid}
115
116# Auto-replace is opt-in so need to set property
117log_must zpool set autoreplace=on $TESTPOOL
118
119# Add some data to the pool
120log_must zfs create $TESTPOOL/fs
121log_must fill_fs /$TESTPOOL/fs 4 100 4096 512 Z
122log_must zpool export $TESTPOOL
123
124# Record the partition UUID for later comparison
125part_uuid=$(udevadm info --query=property --property=ID_PART_TABLE_UUID \
126    --value /dev/disk/by-id/$SD_DEVICE_ID)
127[[ -z "$part_uuid" ]] || log_note original disk GPT uuid ${part_uuid}
128
129#
130# Wipe and offline the disk
131#
132# Note that it is not enough to zero the disk to expunge the partitions.
133# You also need to inform the kernel (e.g., 'hdparm -z' or 'partprobe').
134#
135# Using partprobe is overkill and hdparm is not as common as wipefs. So
136# we use wipefs which lets the kernel know the partition was removed
137# from the device (i.e., calls BLKRRPART ioctl).
138#
139log_must dd if=/dev/zero of=/dev/disk/by-id/$SD_DEVICE_ID bs=1M count=$SDSIZE
140log_must /usr/sbin/wipefs -a /dev/disk/by-id/$SD_DEVICE_ID
141remove_disk $SD
142block_device_wait
143
144# Re-import pool with drive missing
145log_must zpool import $TESTPOOL
146log_must check_state $TESTPOOL "" "DEGRADED"
147block_device_wait
148
149#
150# Online an empty disk in the same physical location, with a different by-id
151# symlink. We use vpd_use_hostno to make sure the underlying serial number
152# changes for the new disk which in turn gives us a different by-id path.
153#
154# The original names were something like:
155# 	/dev/disk/by-id/scsi-SLinux_scsi_debug_16000-part1
156# 	/dev/disk/by-id/wwn-0x33333330000007d0-part1
157#
158# This new inserted disk, will have different links like:
159# 	/dev/disk/by-id/scsi-SLinux_scsi_debug_2000-part1
160# 	/dev/disk/by-id/wwn-0x0x3333333000003e80 -part1
161#
162echo '0' > /sys/bus/pseudo/drivers/scsi_debug/vpd_use_hostno
163
164insert_disk $SD $SD_HOST
165
166# make sure the physical path points to the same scsi-debug device
167SD_DEVICE_ID=$(get_persistent_disk_name $SD)
168echo "alias scsidebug /dev/disk/by-id/$SD_DEVICE_ID" >>$VDEVID_CONF
169block_device_wait
170
171# Wait for the new disk to be online and replaced
172log_must wait_vdev_online $TESTPOOL 0 $vdev_guid 45
173log_must wait_replacing $TESTPOOL 45
174
175# Validate auto-replace was successful
176log_must check_state $TESTPOOL "" "ONLINE"
177
178#
179# Confirm the partition UUID changed so we know the new disk was relabeled
180#
181# Note: some older versions of udevadm don't support "--property" option so
182# we'll # skip this test when it is not supported
183#
184if [ ! -z "$part_uuid" ]; then
185	new_uuid=$(udevadm info --query=property --property=ID_PART_TABLE_UUID \
186	    --value /dev/disk/by-id/$SD_DEVICE_ID)
187	log_note new disk GPT uuid ${new_uuid}
188	[[ "$part_uuid" = "$new_uuid" ]] && \
189	    log_fail "The new disk was not relabeled as expected"
190fi
191
192log_pass "automated auto-replace with by-id paths"
193