1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3
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
15#
16# Copyright (c) 2016 by Delphix. All rights reserved.
17#
18
19. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.kshlib
20
21#
22# DESCRIPTION:
23#	A pool should be importable when up to 2 top-level devices are missing.
24#
25# STRATEGY:
26#	1. Create a pool.
27#	2. Write some data to the pool and checksum it.
28#	3. Add one or more devices.
29#	4. Write more data to the pool and checksum it.
30#	5. Export the pool.
31#	6. Move added devices out of the devices directory.
32#	7. Import the pool with missing devices.
33#	8. Verify that the first batch of data is intact.
34#	9. Verify that accessing the second batch of data doesn't suspend pool.
35#	10. Export the pool, move back missing devices, Re-import the pool.
36#	11. Verify that all the data is intact.
37#
38
39verify_runnable "global"
40
41function custom_cleanup
42{
43	log_must set_spa_load_verify_metadata 1
44	log_must set_spa_load_verify_data 1
45	log_must set_zfs_max_missing_tvds 0
46	log_must rm -rf $BACKUP_DEVICE_DIR
47	# Highly damaged pools may fail to be destroyed, so we export them.
48	poolexists $TESTPOOL1 && log_must zpool export $TESTPOOL1
49	cleanup
50}
51
52log_onexit custom_cleanup
53
54function test_devices_missing
55{
56	typeset poolcreate="$1"
57	typeset addvdevs="$2"
58	typeset missingvdevs="$3"
59	typeset -i missingtvds="$4"
60
61	log_note "$0: pool '$poolcreate', adding $addvdevs, then" \
62	    "moving away $missingvdevs."
63
64	log_must zpool create $TESTPOOL1 $poolcreate
65
66	log_must generate_data $TESTPOOL1 $MD5FILE "first"
67
68	log_must zpool add $TESTPOOL1 $addvdevs
69
70	log_must generate_data $TESTPOOL1 $MD5FILE2 "second"
71
72	log_must_busy zpool export $TESTPOOL1
73
74	log_must mv $missingvdevs $BACKUP_DEVICE_DIR
75
76	# Tell zfs that it is ok to import a pool with missing top-level vdevs
77	log_must set_zfs_max_missing_tvds $missingtvds
78	# Missing devices means that data or metadata may be corrupted.
79	(( missingtvds > 1 )) && log_must set_spa_load_verify_metadata 0
80	log_must set_spa_load_verify_data 0
81	log_must zpool import -o readonly=on -d $DEVICE_DIR $TESTPOOL1
82
83	log_must verify_data_hashsums $MD5FILE
84
85	log_note "Try reading second batch of data, make sure pool doesn't" \
86	    "get suspended."
87	verify_data_hashsums $MD5FILE >/dev/null 2>&1
88
89	log_must_busy zpool export $TESTPOOL1
90
91	typeset newpaths=$(echo "$missingvdevs" | \
92		sed "s:$DEVICE_DIR:$BACKUP_DEVICE_DIR:g")
93	log_must mv $newpaths $DEVICE_DIR
94	log_must set_spa_load_verify_metadata 1
95	log_must set_spa_load_verify_data 1
96	log_must set_zfs_max_missing_tvds 0
97	log_must zpool import -d $DEVICE_DIR $TESTPOOL1
98
99	log_must verify_data_hashsums $MD5FILE
100	log_must verify_data_hashsums $MD5FILE2
101
102	# Cleanup
103	log_must zpool destroy $TESTPOOL1
104
105	log_note ""
106}
107
108log_must mkdir -p $BACKUP_DEVICE_DIR
109
110test_devices_missing "$VDEV0" "$VDEV1" "$VDEV1" 1
111test_devices_missing "$VDEV0" "$VDEV1 $VDEV2" "$VDEV1" 1
112test_devices_missing "mirror $VDEV0 $VDEV1" "mirror $VDEV2 $VDEV3" \
113    "$VDEV2 $VDEV3" 1
114test_devices_missing "$VDEV0 log $VDEV1" "$VDEV2" "$VDEV2" 1
115
116#
117# Note that we are testing for 2 non-consecutive missing devices.
118# Missing consecutive devices results in missing metadata. Because of
119# Missing metadata can cause the root dataset to fail to mount.
120#
121test_devices_missing "$VDEV0" "$VDEV1 $VDEV2 $VDEV3" "$VDEV1 $VDEV3" 2
122
123log_pass "zpool import succeeded with missing devices."
124