1#!/bin/ksh -p
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2021 by Delphix. All rights reserved.
16# Copyright (c) 2023 by Klara, Inc.
17#
18
19. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.kshlib
20
21#
22# DESCRIPTION:
23# A pool that wasn't cleanly exported should not be importable without force if
24# the local hostid doesn't match the on-disk hostid.
25#
26# STRATEGY:
27#	1. Set a hostid.
28#	2. Create a pool.
29#	3. Simulate the pool being torn down without export:
30#	3.1. Copy the underlying device state.
31#	3.2. Export the pool.
32#	3.3. Restore the device state from the copy.
33#	4. Change the hostid.
34#	5. Verify that importing the pool fails.
35#	6. Verify that importing the pool with force succeeds.
36#
37
38verify_runnable "global"
39
40function custom_cleanup
41{
42	rm -f $HOSTID_FILE $VDEV0.bak
43	cleanup
44}
45
46log_onexit custom_cleanup
47
48# 1. Set a hostid.
49log_must zgenhostid -f $HOSTID1
50
51# 2. Create a pool.
52log_must zpool create $TESTPOOL1 $VDEV0
53
54# 3. Simulate the pool being torn down without export.
55log_must cp $VDEV0 $VDEV0.bak
56log_must zpool export $TESTPOOL1
57log_must cp -f $VDEV0.bak $VDEV0
58log_must rm -f $VDEV0.bak
59
60# 4. Change the hostid.
61log_must zgenhostid -f $HOSTID2
62
63# 5. Verify that importing the pool fails.
64log_mustnot zpool import -d $DEVICE_DIR $TESTPOOL1
65
66# 6. Verify that importing the pool with force succeeds.
67log_must zpool import -d $DEVICE_DIR -f $TESTPOOL1
68
69log_pass "zpool import requires force if not cleanly exported " \
70    "and hostid changed."
71