1#!/usr/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 2009 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# Copyright 2019 Joyent, Inc.
31#
32
33. $STF_SUITE/include/libtest.shlib
34. $STF_SUITE/tests/functional/cli_root/zpool_create/zpool_create.cfg
35
36#
37# DESCRIPTION:
38#
39# zpool create -R works as expected
40#
41# STRATEGY:
42# 1. Create a -R altroot pool
43# 2. Verify the pool is mounted at the correct location
44# 3. Verify that cachefile=none for the pool
45# 4. Verify that root=<mountpoint> for the pool
46# 5. Verify that no reference to the pool is found in /etc/zfs/zpool.cache
47
48function cleanup
49{
50	if poolexists $TESTPOOL ; then
51                destroy_pool $TESTPOOL
52        fi
53	if [ -d $TESTPOOL_DIR ]
54	then
55		log_must rmdir $TESTPOOL_DIR
56	fi
57}
58
59TESTPOOL_DIR=/${TESTPOOL}.root
60
61log_onexit cleanup
62
63log_assert "zpool create -R works as expected"
64
65if [[ -n $DISK ]]; then
66	disk=$DISK
67else
68	disk=$DISK0
69fi
70
71log_must mkdir $TESTPOOL_DIR
72log_must zpool create -R $TESTPOOL_DIR $TESTPOOL $disk
73if [ ! -d $TESTPOOL_DIR ]
74then
75	log_fail "Mountpoint was not created when using zpool with -R flag!"
76fi
77
78FS=$(zfs list $TESTPOOL)
79if [ -z "$FS" ]
80then
81	log_fail "Mounted filesystem at $TESTPOOL_DIR isn't ZFS!"
82fi
83
84log_must zpool get all $TESTPOOL
85zpool get all $TESTPOOL > /tmp/values.$$
86
87# check for the cachefile property, verifying that it's set to 'none'
88grep "$TESTPOOL[ ]*cachefile[ ]*none" /tmp/values.$$ > /dev/null 2>&1
89if [ $? -ne 0 ]
90then
91	log_fail "zpool property \'cachefile\' was not set to \'none\'."
92fi
93
94# check that the root = /mountpoint property is set correctly
95grep "$TESTPOOL[ ]*altroot[ ]*$TESTPOOL_DIR" /tmp/values.$$ > /dev/null 2>&1
96if [ $? -ne 0 ]
97then
98	log_fail "zpool property root was not found in pool output."
99fi
100
101rm /tmp/values.$$
102
103# finally, check that the pool has no reference in /etc/zfs/zpool.cache
104if [[ -f /etc/zfs/zpool.cache ]] ; then
105	REF=$(strings /etc/zfs/zpool.cache | grep ${TESTPOOL})
106	if [ ! -z "$REF" ]
107	then
108		strings /etc/zfs/zpool.cache
109		log_fail "/etc/zfs/zpool.cache appears to have a reference to $TESTPOOL"
110	fi
111fi
112
113
114log_pass "zpool create -R works as expected"
115