1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
3#
4# CDDL HEADER START
5#
6# This file and its contents are supplied under the terms of the
7# Common Development and Distribution License ("CDDL"), version 1.0.
8# You may only use this file in accordance with the terms of version
9# 1.0 of the CDDL.
10#
11# A full copy of the text of the CDDL should have accompanied this
12# source.  A copy of the CDDL is also available via the Internet at
13# http://www.illumos.org/license/CDDL.
14#
15# CDDL HEADER END
16#
17# Copyright 2023 Mateusz Piotrowski
18#
19
20. $STF_SUITE/include/libtest.shlib
21
22#
23# DESCRIPTION:
24# Verify 'zpool reguid' can change pool's GUID.
25#
26# STRATEGY:
27# 1. Use zpool get to obtain the initial GUID of a pool.
28# 2. Change pool's GUID with zpool reguid.
29# 3. Verify the GUID has changed to a random GUID.
30#
31# 4. Change pool's GUID with zpool reguid -g.
32# 5. Verify the GUID has changed to the specified GUID.
33#
34
35# set_guid guid [expected_guid]
36set_guid() {
37	gflag_guid="$1"
38	expected_guid="${2:-"$gflag_guid"}"
39
40	initial_guid="$(zpool get -H -o value guid "$TESTPOOL")"
41	log_assert "Verify 'zpool reguid -g \"$gflag_guid\"' sets GUID as expected."
42	log_must zpool reguid -g "$gflag_guid" "$TESTPOOL"
43	retrieved_guid="$(zpool get -H -o value guid "$TESTPOOL")"
44	if [[ "$retrieved_guid" == "" ]]; then
45		log_fail "Unable to obtain the new GUID of pool $TESTPOOL"
46	fi
47	if [[ "$expected_guid" != "$retrieved_guid" ]]; then
48		log_fail "GUID set to '$retrieved_guid' instead of '$expected_guid'"
49	fi
50}
51
52log_assert "Verify 'zpool reguid' picks a new random GUID for the pool."
53initial_guid="$(zpool get -H -o value guid "$TESTPOOL")"
54if [[ $initial_guid == "" ]]; then
55	log_fail "Unable to obtain the initial GUID of pool $TESTPOOL"
56fi
57log_must zpool reguid "$TESTPOOL"
58new_guid="$(zpool get -H -o value guid "$TESTPOOL")"
59if [[ "$new_guid" == "" ]]; then
60	log_fail "Unable to obtain the new GUID of pool $TESTPOOL"
61fi
62if [[ "$initial_guid" == "$new_guid" ]]; then
63	log_fail "GUID change failed; GUID has not changed: $initial_guid"
64fi
65
66for g in "$(echo '2^64 - 1' | bc)" "314"; do
67	set_guid "$g"
68done
69# zpool-reguid(8) will strip the leading 0.
70set_guid 0123 "123"
71# GUID "-1" is effectively 2^64 - 1 in value.
72set_guid -1 "$(echo '2^64 - 1' | bc)"
73
74log_pass "'zpool reguid' changes GUID as expected."
75