1#!/bin/ksh -p
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2016 Nexenta Systems, Inc.
15# Copyright (c) 2019 by Lawrence Livermore National Security, LLC.
16# Copyright 2020 Joyent, Inc.
17#
18
19. $STF_SUITE/tests/functional/cli_root/zpool_labelclear/labelclear.cfg
20
21# DESCRIPTION:
22#    Check that `zpool labelclear` only clears valid labels.  Expected
23#    label offsets which do not contain intact labels are left untouched.
24#
25# STRATEGY:
26# 1. Create a pool with primary, log, spare and cache devices.
27# 2. Export the pool.
28# 3. Write a known pattern over the first two device labels.
29# 4. Verify with zdb that only the last two device labels are intact.
30# 5. Verify the pool could be imported using those labels.
31# 6. Run `zpool labelclear` to destroy those last two labels.
32# 7. Verify the pool can no longer be found; let alone imported.
33# 8. Verify the pattern is intact to confirm `zpool labelclear` did
34#    not write to first two label offsets.
35# 9. Verify that no valid label remain.
36#
37
38verify_runnable "global"
39
40function cleanup
41{
42	poolexists $TESTPOOL && destroy_pool $TESTPOOL
43	rm -f $PATTERN_FILE $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4
44}
45
46log_onexit cleanup
47log_assert "zpool labelclear will only clear valid labels"
48
49PATTERN_FILE=$TEST_BASE_DIR/pattern
50
51DEVICE1="$TEST_BASE_DIR/device-1"
52DEVICE2="$TEST_BASE_DIR/device-2"
53DEVICE3="$TEST_BASE_DIR/device-3"
54DEVICE4="$TEST_BASE_DIR/device-4"
55
56log_must dd if=/dev/urandom of=$PATTERN_FILE bs=1048576 count=4
57
58log_must truncate -s $SPA_MINDEVSIZE $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4
59
60log_must zpool create -O mountpoint=none -f $TESTPOOL $DEVICE1 \
61     log $DEVICE2 cache $DEVICE3 spare $DEVICE4
62log_must zpool export $TESTPOOL
63
64# Overwrite the first 4M of each device and verify the expected labels.
65for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
66	dd if=$PATTERN_FILE of=$dev bs=1048576 conv=notrunc
67	log_must eval "zdb -l $dev | grep 'labels = 2 3'"
68done
69
70# Verify the pool could be imported using those labels.
71log_must eval "zpool import -d $TEST_BASE_DIR | grep $TESTPOOL"
72
73# Verify the last two labels on each vdev can be cleared.
74for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
75	log_must zpool labelclear -f $dev
76done
77
78# Verify there is no longer a pool which can be imported.
79log_mustnot eval "zpool import -d $TEST_BASE_DIR | grep $TESTPOOL"
80
81# Verify the original pattern over the first two labels is intact
82for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
83	log_must dd if=$dev bs=1048576 count=4 | cmp -- - $PATTERN_FILE
84	log_mustnot zdb -lq $dev
85done
86
87# Verify an error is reported when there are no labels to clear.
88for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
89	log_mustnot zpool labelclear -f $dev
90done
91
92log_pass "zpool labelclear will only clear valid labels"
93