1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2017 by Lawrence Livermore National Security, LLC.
19# Copyright 2019 Joyent, Inc.
20#
21
22# DESCRIPTION:
23#	Under no circumstances when multihost is active, should an active pool
24#	with one hostid be importable by a host with a different hostid.
25#
26# STRATEGY:
27#	 1. Simulate an active pool on another host with ztest.
28#	 2. Verify 'zpool import' reports an active pool.
29#	 3. Verify 'zpool import [-f] $MMP_POOL' cannot import the pool.
30#	 4. Kill ztest to make pool eligible for import.
31#	 5. Verify 'zpool import' fails with the expected error message.
32#	 6. Verify 'zpool import $MMP_POOL' fails with the expected message.
33#	 7. Verify 'zpool import -f $MMP_POOL' can now import the pool.
34#	 8. Verify pool may be exported/imported without -f argument.
35#
36
37. $STF_SUITE/include/libtest.shlib
38. $STF_SUITE/tests/functional/mmp/mmp.cfg
39. $STF_SUITE/tests/functional/mmp/mmp.kshlib
40
41verify_runnable "both"
42
43function cleanup
44{
45	mmp_pool_destroy $MMP_POOL $MMP_DIR
46	log_must set_tunable64 zfs_multihost_interval $MMP_INTERVAL_DEFAULT
47	log_must mmp_clear_hostid
48}
49
50log_assert "multihost=on|off active pool activity checks"
51log_onexit cleanup
52
53# 1. Simulate an active pool on another host with ztest.
54mmp_pool_destroy $MMP_POOL $MMP_DIR
55mmp_pool_create $MMP_POOL $MMP_DIR
56
57# 2. Verify 'zpool import' reports an active pool.
58log_must mmp_set_hostid $HOSTID2
59log_must set_tunable64 zfs_multihost_interval $MMP_INTERVAL_MIN
60log_must is_pool_imported $MMP_POOL "-d $MMP_DIR"
61
62# 3. Verify 'zpool import [-f] $MMP_POOL' cannot import the pool.
63MMP_IMPORTED_MSG="Cannot import '$MMP_POOL': pool is imported"
64
65log_must try_pool_import $MMP_POOL "-d $MMP_DIR" "$MMP_IMPORTED_MSG"
66for i in {1..10}; do
67	log_must try_pool_import $MMP_POOL "-f -d $MMP_DIR" "$MMP_IMPORTED_MSG"
68done
69
70log_must try_pool_import $MMP_POOL "-c ${MMP_CACHE}.stale" "$MMP_IMPORTED_MSG"
71
72for i in {1..10}; do
73	log_must try_pool_import $MMP_POOL "-f -c ${MMP_CACHE}.stale" \
74	    "$MMP_IMPORTED_MSG"
75done
76
77# 4. Kill ztest to make pool eligible for import.  Poll with 'zpool status'.
78ZTESTPID=$(pgrep ztest)
79if [ -n "$ZTESTPID" ]; then
80	log_must kill -9 $ZTESTPID
81fi
82log_must wait_pool_imported $MMP_POOL "-d $MMP_DIR"
83
84# 5. Verify 'zpool import' fails with the expected error message, when
85#    - hostid=0:        - configuration error
86#    - hostid=matches   - safe to import the pool
87#    - hostid=different - previously imported on a different system
88#
89log_must mmp_clear_hostid
90case "$(uname)" in
91Linux)  MMP_IMPORTED_MSG="Set a unique system hostid";;
92SunOS)  MMP_IMPORTED_MSG="Check the SMF svc:/system/hostid service.";;
93esac
94log_must check_pool_import $MMP_POOL "-d $MMP_DIR" "action" "$MMP_IMPORTED_MSG"
95
96log_must mmp_set_hostid $HOSTID1
97MMP_IMPORTED_MSG="The pool can be imported"
98log_must check_pool_import $MMP_POOL "-d $MMP_DIR" "action" "$MMP_IMPORTED_MSG"
99
100log_must mmp_clear_hostid
101log_must mmp_set_hostid $HOSTID2
102MMP_IMPORTED_MSG="The pool was last accessed by another system."
103log_must check_pool_import $MMP_POOL "-d $MMP_DIR" "status" "$MMP_IMPORTED_MSG"
104
105# 6. Verify 'zpool import $MMP_POOL' fails with the expected message.
106MMP_IMPORTED_MSG="pool was previously in use from another system."
107log_must try_pool_import $MMP_POOL "-d $MMP_DIR" "$MMP_IMPORTED_MSG"
108
109# 7. Verify 'zpool import -f $MMP_POOL' can now import the pool.
110# Default interval results in minimum activity test 10s which
111# makes detection of the activity test reliable.
112log_must set_tunable64 zfs_multihost_interval $MMP_INTERVAL_DEFAULT
113log_must import_activity_check $MMP_POOL "-f -d $MMP_DIR"
114
115# 8 Verify pool may be exported/imported without -f argument.
116log_must zpool export $MMP_POOL
117log_must import_no_activity_check $MMP_POOL "-d $MMP_DIR"
118
119log_pass "multihost=on|off active pool activity checks passed"
120