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 https://opensource.org/licenses/CDDL-1.0.
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 | awk '/pool:/ {print $2}'); do
80		zpool import -f $pool
81	done
82
83	for pool in $(zpool import -d $DEVICE_DIR | awk '/pool:/ {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		read -r checksum2 _ < <(cksum $file)
112		log_must [ "$checksum1" = "$checksum2" ]
113
114		(( id = id + 1 ))
115	done
116
117	return 0
118}
119
120
121log_assert "Verify that 'zpool import -a' succeeds as root."
122log_onexit cleanup_all
123
124read -r checksum1 _ < <(cksum $MYTESTFILE)
125number=1
126
127#
128# setup exported pools on raw files
129#
130for disk in $DEVICE_FILES
131do
132	poolname="${TESTPOOL}-$number"
133	setup_single_disk "$disk" \
134		"$poolname" \
135		"$TESTFS" \
136		"$TESTDIR.$number"
137	testpools[$poolname]=$poolname
138	(( number = number + 1 ))
139done
140
141while (( i < ${#options[*]} )); do
142
143	log_must zpool import -d $DEVICE_DIR ${options[i]} -a -f
144
145	# export unintentionally imported pools
146	for poolname in $(get_all_pools); do
147		if [[ -z ${testpools[$poolname]} ]]; then
148			log_must_busy zpool export $poolname
149		fi
150	done
151
152	if [[ -n ${options[i]} ]]; then
153		checksum_all $ALTER_ROOT
154	else
155		checksum_all
156	fi
157
158	for poolname in ${testpools[@]}; do
159		if poolexists $poolname ; then
160			log_must_busy zpool export $poolname
161		fi
162	done
163
164	(( i = i + 1 ))
165done
166
167log_pass "'zpool import -a' succeeds as root."
168