1#!/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) 2021 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 from a cachefile even if device paths
23#	have changed.
24#
25# STRATEGY:
26#	1. Create a pool using a cachefile
27#	2. Backup cachefile
28#	3. Export the pool.
29#	4. Change the paths of some of the devices.
30#	5. Verify that we can import the pool using the cachefile.
31#
32
33verify_runnable "global"
34
35log_onexit cleanup
36
37function test_new_paths
38{
39	typeset poolcreate="$1"
40	typeset pathstochange="$2"
41
42	log_note "$0: pool '$poolcreate', changing paths of $pathstochange."
43
44	log_must zpool create -o cachefile=$CPATH $TESTPOOL1 $poolcreate
45
46	log_must cp $CPATH $CPATHBKP
47
48	log_must zpool export $TESTPOOL1
49
50	for dev in $pathstochange; do
51		log_must mv $dev "${dev}_new"
52	done
53
54	log_must zpool import -c $CPATHBKP $TESTPOOL1
55	log_must check_pool_healthy $TESTPOOL1
56
57	# Cleanup
58	log_must zpool destroy $TESTPOOL1
59	log_must rm -f $CPATH $CPATHBKP
60	for dev in $pathstochange; do
61		log_must mv "${dev}_new" $dev
62	done
63
64	log_note ""
65}
66
67function test_duplicate_pools
68{
69	typeset poolcreate="$1"
70	typeset pathstocopy="$2"
71
72	log_note "$0: pool '$poolcreate', creating duplicate pool using $pathstocopy."
73
74	log_must zpool create -o cachefile=$CPATH $TESTPOOL1 $poolcreate
75	log_must zpool export $TESTPOOL1
76
77	for dev in $pathstocopy; do
78		log_must cp $dev "${dev}_orig"
79
80	done
81
82	log_must zpool create -f -o cachefile=$CPATH $TESTPOOL1 $poolcreate
83	log_must cp $CPATH $CPATHBKP
84	log_must zpool export $TESTPOOL1
85
86	for dev in $pathstocopy; do
87		log_must mv $dev "${dev}_new"
88	done
89
90	log_must zpool import -c $CPATHBKP
91	log_must zpool import -c $CPATHBKP $TESTPOOL1
92	log_must check_pool_healthy $TESTPOOL1
93
94	# Cleanup
95	log_must zpool destroy $TESTPOOL1
96	log_must rm -f $CPATH $CPATHBKP
97	for dev in $pathstocopy; do
98		log_must rm "${dev}_orig"
99		log_must mv "${dev}_new" $dev
100	done
101
102	log_note ""
103}
104
105test_new_paths "$VDEV0 $VDEV1" "$VDEV0 $VDEV1"
106test_new_paths "mirror $VDEV0 $VDEV1" "$VDEV0 $VDEV1"
107test_new_paths "$VDEV0 log $VDEV1" "$VDEV0 $VDEV1"
108test_new_paths "raidz $VDEV0 $VDEV1 $VDEV2" "$VDEV0 $VDEV1 $VDEV2"
109test_new_paths "draid $VDEV0 $VDEV1 $VDEV2" "$VDEV0 $VDEV1 $VDEV2"
110
111test_duplicate_pools "$VDEV0 $VDEV1" "$VDEV0 $VDEV1"
112test_duplicate_pools "mirror $VDEV0 $VDEV1" "$VDEV0 $VDEV1"
113test_duplicate_pools "$VDEV0 log $VDEV1" "$VDEV0 $VDEV1"
114test_duplicate_pools "raidz $VDEV0 $VDEV1 $VDEV2" "$VDEV0 $VDEV1 $VDEV2"
115test_duplicate_pools "draid $VDEV0 $VDEV1 $VDEV2" "$VDEV0 $VDEV1 $VDEV2"
116
117log_pass "zpool import with cachefile succeeded after changing device paths."
118