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 from a cachefile
24# without force if 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. Backup the cachefile.
30#	4. Simulate the pool being torn down without export:
31#	4.1. Copy the underlying device state.
32#	4.2. Export the pool.
33#	4.3. Restore the device state from the copy.
34#	5. Change the hostid.
35#	6. Verify that importing the pool from the cachefile fails.
36#	7. Verify that importing the pool from the cachefile with force
37#	   succeeds.
38#
39
40verify_runnable "global"
41
42function custom_cleanup
43{
44	rm -f $HOSTID_FILE $CPATH $CPATHBKP $VDEV0.bak
45	cleanup
46}
47
48log_onexit custom_cleanup
49
50# 1. Set a hostid.
51log_must zgenhostid -f $HOSTID1
52
53# 2. Create a pool.
54log_must zpool create -o cachefile=$CPATH $TESTPOOL1 $VDEV0
55
56# 3. Backup the cachfile.
57log_must cp $CPATH $CPATHBKP
58
59# 4. Simulate the pool being torn down without export.
60log_must cp $VDEV0 $VDEV0.bak
61log_must zpool export $TESTPOOL1
62log_must cp -f $VDEV0.bak $VDEV0
63log_must rm -f $VDEV0.bak
64
65# 5. Change the hostid.
66log_must zgenhostid -f $HOSTID2
67
68# 6. Verify that importing the pool from the cachefile fails.
69log_mustnot zpool import -c $CPATHBKP $TESTPOOL1
70
71# 7. Verify that importing the pool from the cachefile with force succeeds.
72log_must zpool import -f -c $CPATHBKP $TESTPOOL1
73
74log_pass "zpool import from cachefile requires force if not cleanly " \
75    "exported and hostid changes."
76