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 -A testpools
52typeset -i number=0
53typeset -i i=0
54typeset checksum1
55typeset poolname
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	log_must cp $MYTESTFILE $mtpt/$TESTFILE0
66	log_must zpool export $pool
67
68	[[ -d $mtpt ]] && \
69		rm -rf $mtpt
70}
71
72function cleanup_all
73{
74	typeset -i id=1
75
76	#
77	# Try import individually if 'import -a' failed.
78	#
79	for pool in `zpool import | grep "pool:" | awk '{print $2}'`; do
80		zpool import -f $pool
81	done
82
83	for pool in `zpool import -d $DEVICE_DIR | grep "pool:" | awk '{print $2}'`; do
84		log_must zpool import -d $DEVICE_DIR -f $pool
85	done
86
87	while (( id < number )); do
88		if poolexists ${TESTPOOL}-$id ; then
89			cleanup_filesystem "${TESTPOOL}-$id" $TESTFS
90			destroy_pool ${TESTPOOL}-$id
91		fi
92		(( id = id + 1 ))
93        done
94
95	[[ -d $ALTER_ROOT ]] && \
96		rm -rf $ALTER_ROOT
97}
98
99function checksum_all #alter_root
100{
101	typeset alter_root=$1
102	typeset -i id=1
103	typeset file
104	typeset checksum2
105
106	while (( id < number )); do
107		file=${alter_root}/$TESTDIR.$id/$TESTFILE0
108		[[ ! -e $file ]] && \
109			log_fail "$file missing after import."
110
111		checksum2=$(sum $file | awk '{print $1}')
112		[[ "$checksum1" != "$checksum2" ]] && \
113			log_fail "Checksums differ ($checksum1 != $checksum2)"
114
115		(( id = id + 1 ))
116	done
117
118	return 0
119}
120
121
122log_assert "Verify that 'zpool import -a' succeeds as root."
123log_onexit cleanup_all
124
125checksum1=$(sum $MYTESTFILE | awk '{print $1}')
126number=1
127
128#
129# setup exported pools on raw files
130#
131for disk in $DEVICE_FILES
132do
133	poolname="${TESTPOOL}-$number"
134	setup_single_disk "$disk" \
135		"$poolname" \
136		"$TESTFS" \
137		"$TESTDIR.$number"
138	testpools[$poolname]=$poolname
139	(( number = number + 1 ))
140done
141
142while (( i < ${#options[*]} )); do
143
144	log_must zpool import -d $DEVICE_DIR ${options[i]} -a -f
145
146	# export unintentionally imported pools
147	for poolname in $(get_all_pools); do
148		if [[ -z ${testpools[$poolname]} ]]; then
149			log_must_busy zpool export $poolname
150		fi
151	done
152
153	if [[ -n ${options[i]} ]]; then
154		checksum_all $ALTER_ROOT
155	else
156		checksum_all
157	fi
158
159	for poolname in ${testpools[@]}; do
160		if poolexists $poolname ; then
161			log_must_busy zpool export $poolname
162		fi
163	done
164
165	(( i = i + 1 ))
166done
167
168log_pass "'zpool import -a' succeeds as root."
169