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 http://www.opensolaris.org/os/licensing.
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#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/cli_root/zfs_mount/zfs_mount.kshlib
34. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.cfg
35
36#
37# DESCRIPTION:
38# Verify that 'zpool import -a' succeeds as root.
39#
40# STRATEGY:
41# 1. Create a group of pools with specified vdev.
42# 2. Create zfs filesystems within the given pools.
43# 3. Export the pools.
44# 4. Verify that import command succeed.
45#
46
47verify_runnable "global"
48
49set -A options "" "-R $ALTER_ROOT"
50
51typeset -i number=0
52typeset -i id=0
53typeset -i i=0
54typeset checksum1
55typeset unwantedpool
56
57function setup_single_disk #disk #pool #fs #mtpt
58{
59	typeset disk=$1
60	typeset pool=$2
61	typeset fs=${3##/}
62	typeset mtpt=$4
63
64	setup_filesystem "$disk" "$pool" "$fs" "$mtpt"
65
66	log_must cp $MYTESTFILE $mtpt/$TESTFILE0
67
68	log_must zpool export $pool
69
70	[[ -d $mtpt ]] && \
71		rm -rf $mtpt
72}
73
74function cleanup_all
75{
76	typeset -i id=0
77
78	#
79	# Try import individually if 'import -a' failed.
80	#
81	for pool in `zpool import | grep "pool:" | awk '{print $2}'`; do
82		zpool import -f $pool
83	done
84
85	for pool in `zpool import -d $DEVICE_DIR | grep "pool:" | awk '{print $2}'`; do
86		log_must zpool import -d $DEVICE_DIR -f $pool
87	done
88
89	while (( id < number )); do
90		if ! poolexists ${TESTPOOL}-$id ; then
91			(( id = id + 1 ))
92			continue
93		fi
94
95		if (( id == 0 )); then
96			log_must zpool export ${TESTPOOL}-$id
97
98			[[ -d /${TESTPOOL}-$id ]] && \
99				log_must rm -rf /${TESTPOOL}-$id
100
101			log_must zpool import -f ${TESTPOOL}-$id $TESTPOOL
102
103			[[ -e $TESTDIR/$TESTFILE0 ]] && \
104				log_must rm -rf $TESTDIR/$TESTFILE0
105		else
106			cleanup_filesystem "${TESTPOOL}-$id" $TESTFS
107
108			destroy_pool ${TESTPOOL}-$id
109		fi
110
111		(( id = id + 1 ))
112        done
113
114	[[ -d $ALTER_ROOT ]] && \
115		rm -rf $ALTER_ROOT
116}
117
118function checksum_all #alter_root
119{
120	typeset alter_root=$1
121	typeset -i id=0
122	typeset file
123	typeset checksum2
124
125	while (( id < number )); do
126		if (( id == 2 )); then
127			(( id = id + 1 ))
128			continue
129		fi
130
131		if (( id == 0 )); then
132			file=${alter_root}/$TESTDIR/$TESTFILE0
133		else
134			file=${alter_root}/$TESTDIR.$id/$TESTFILE0
135		fi
136		[[ ! -e $file ]] && \
137			log_fail "$file missing after import."
138
139		checksum2=$(sum $file | awk '{print $1}')
140		[[ "$checksum1" != "$checksum2" ]] && \
141			log_fail "Checksums differ ($checksum1 != $checksum2)"
142
143		(( id = id + 1 ))
144	done
145
146	return 0
147}
148
149
150log_assert "Verify that 'zpool import -a' succeeds as root."
151
152log_onexit cleanup_all
153
154checksum1=$(sum $MYTESTFILE | awk '{print $1}')
155
156log_must zpool export $TESTPOOL
157log_must zpool import $TESTPOOL ${TESTPOOL}-0
158log_must cp $MYTESTFILE $TESTDIR/$TESTFILE0
159log_must zpool export ${TESTPOOL}-0
160[[ -d /${TESTPOOL}-0 ]] && \
161	log_must rm -rf /${TESTPOOL}-0
162
163#
164# setup exported pools on normal devices
165#
166number=1
167while (( number <= $GROUP_NUM )); do
168	if (( number == 2)); then
169		(( number = number + 1 ))
170		continue
171	fi
172
173	setup_single_disk "${ZFS_DISK1}s${number}" \
174		"${TESTPOOL}-$number" \
175		"$TESTFS" \
176		"$TESTDIR.$number"
177
178	(( number = number + 1 ))
179done
180
181#
182# setup exported pools on raw files
183#
184for disk in $DEVICE_FILES
185do
186
187	setup_single_disk "$disk" \
188		"${TESTPOOL}-$number" \
189		"$TESTFS" \
190		"$TESTDIR.$number"
191
192	(( number = number + 1 ))
193done
194
195while (( i < ${#options[*]} )); do
196
197	log_must zpool import -d /dev/dsk -d $DEVICE_DIR ${options[i]} -a -f
198
199	# destroy unintentional imported pools
200	typeset exclude=`eval echo \"'(${KEEP})'\"`
201	for unwantedpool in $(zpool list -H -o name \
202	     | egrep -v "$exclude" | grep -v $TESTPOOL); do
203		log_must zpool export $unwantedpool
204	done
205
206	if [[ -n ${options[i]} ]]; then
207		checksum_all $ALTER_ROOT
208	else
209		checksum_all
210	fi
211
212	id=0
213	while (( id < number )); do
214		if poolexists ${TESTPOOL}-$id ; then
215			log_must zpool export ${TESTPOOL}-$id
216		fi
217		(( id = id + 1 ))
218	done
219
220	(( i = i + 1 ))
221done
222
223log_pass "'zpool import -a' succeeds as root."
224