1#!/usr/bin/ksh -p
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2016 by Delphix. All rights reserved.
16#
17
18. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.kshlib
19
20#
21# DESCRIPTION:
22#	A pool should be importable using an outdated cachefile that is unaware
23#	that one or more vdevs were removed.
24#
25# STRATEGY:
26#	1. Create a pool with some devices and an alternate cachefile.
27#	2. Backup the cachefile.
28#	3. Remove device(s) from the pool and remove them.
29#	4. (Optionally) Add device(s) to pool.
30#	5. Export the pool.
31#	6. Verify that we can import the pool using the backed-up cachefile.
32#
33
34verify_runnable "global"
35
36function custom_cleanup
37{
38	cleanup
39}
40
41log_onexit custom_cleanup
42
43function test_remove_vdev
44{
45	typeset poolcreate="$1"
46	typeset removevdev="$2"
47	typeset poolcheck="$3"
48
49	log_note "$0: pool '$poolcreate', remove $2."
50
51	log_must zpool create -o cachefile=$CPATH $TESTPOOL1 $poolcreate
52
53	log_must cp $CPATH $CPATHBKP
54
55	log_must zpool remove $TESTPOOL1 $removevdev
56	log_must wait_for_pool_config $TESTPOOL1 "$poolcheck"
57	log_must rm $removevdev
58
59	log_must zpool export $TESTPOOL1
60
61	log_must zpool import -c $CPATHBKP $TESTPOOL1
62	log_must check_pool_config $TESTPOOL1 "$poolcheck"
63
64	# Cleanup
65	log_must zpool destroy $TESTPOOL1
66	log_must rm -f $CPATH $CPATHBKP
67	log_must mkfile $FILE_SIZE $removevdev
68
69	log_note ""
70}
71
72#
73# We have to remove top-level non-log vdevs one by one, else there is a high
74# chance pool will report busy and command will fail for the second vdev.
75#
76function test_remove_two_vdevs
77{
78	log_note "$0."
79	log_must zpool create -o cachefile=$CPATH $TESTPOOL1 \
80	    $VDEV0 $VDEV1 $VDEV2 $VDEV3 $VDEV4
81
82	log_must cp $CPATH $CPATHBKP
83
84	log_must zpool remove $TESTPOOL1 $VDEV4
85	log_must wait_for_pool_config $TESTPOOL1 \
86	    "$VDEV0 $VDEV1 $VDEV2 $VDEV3"
87	log_must zpool remove $TESTPOOL1 $VDEV3
88	log_must wait_for_pool_config $TESTPOOL1 "$VDEV0 $VDEV1 $VDEV2"
89	log_must rm $VDEV3 $VDEV4
90
91	log_must zpool export $TESTPOOL1
92
93	log_must zpool import -c $CPATHBKP $TESTPOOL1
94	log_must check_pool_config $TESTPOOL1 "$VDEV0 $VDEV1 $VDEV2"
95
96	# Cleanup
97	log_must zpool destroy $TESTPOOL1
98	log_must rm -f $CPATH $CPATHBKP
99	log_must mkfile $FILE_SIZE $VDEV3 $VDEV4
100
101	log_note ""
102}
103
104#
105# We want to test the case where a whole created by a log device is filled
106# by a regular device
107#
108function test_remove_log_then_add_vdev
109{
110	log_note "$0."
111	log_must zpool create -o cachefile=$CPATH $TESTPOOL1 \
112	    $VDEV0 $VDEV1 $VDEV2 log $VDEV3
113
114	log_must cp $CPATH $CPATHBKP
115
116	log_must zpool remove $TESTPOOL1 $VDEV1
117	log_must wait_for_pool_config $TESTPOOL1 "$VDEV0 $VDEV2 log $VDEV3"
118	log_must zpool remove $TESTPOOL1 $VDEV3
119	log_must check_pool_config $TESTPOOL1 "$VDEV0 $VDEV2"
120	log_must rm $VDEV1 $VDEV3
121	log_must zpool add $TESTPOOL1 $VDEV4
122
123	log_must zpool export $TESTPOOL1
124
125	log_must zpool import -c $CPATHBKP $TESTPOOL1
126	log_must check_pool_config $TESTPOOL1 "$VDEV0 $VDEV2 $VDEV4"
127
128	# Cleanup
129	log_must zpool destroy $TESTPOOL1
130	log_must rm -f $CPATH $CPATHBKP
131	log_must mkfile $FILE_SIZE $VDEV1 $VDEV3
132
133	log_note ""
134}
135
136test_remove_vdev "$VDEV0 $VDEV1 $VDEV2" "$VDEV2" "$VDEV0 $VDEV1"
137test_remove_vdev "$VDEV0 $VDEV1 $VDEV2" "$VDEV1" "$VDEV0 $VDEV2"
138test_remove_vdev "$VDEV0 log $VDEV1" "$VDEV1" "$VDEV0"
139test_remove_vdev "$VDEV0 log $VDEV1 $VDEV2" "$VDEV1 $VDEV2" "$VDEV0"
140test_remove_vdev "$VDEV0 $VDEV1 $VDEV2 log $VDEV3" "$VDEV2" \
141    "$VDEV0 $VDEV1 log $VDEV3"
142test_remove_two_vdevs
143test_remove_log_then_add_vdev
144
145log_pass "zpool import -c cachefile_unaware_of_remove passed."
146