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#
17
18. $STF_SUITE/tests/functional/cli_root/zpool_labelclear/labelclear.cfg
19
20# DESCRIPTION:
21#    Check that `zpool labelclear` only clears valid labels.  Expected
22#    label offsets which do not contain intact labels are left untouched.
23#
24# STRATEGY:
25# 1. Create a pool with primary, log, spare and cache devices.
26# 2. Export the pool.
27# 3. Write a known pattern over the first two device labels.
28# 4. Verify with zdb that only the last two device labels are intact.
29# 5. Verify the pool could be imported using those labels.
30# 6. Run `zpool labelclear` to destroy those last two labels.
31# 7. Verify the pool can no longer be found; let alone imported.
32# 8. Verify the pattern is intact to confirm `zpool labelclear` did
33#    not write to first two label offsets.
34# 9. Verify that no valid label remain.
35#
36
37verify_runnable "global"
38
39function cleanup
40{
41	poolexists $TESTPOOL && destroy_pool $TESTPOOL
42	rm -f $PATTERN_FILE $DISK_PATTERN_FILE \
43	    $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
50DISK_PATTERN_FILE=$TEST_BASE_DIR/disk-pattern
51
52DEVICE1="$TEST_BASE_DIR/device-1"
53DEVICE2="$TEST_BASE_DIR/device-2"
54DEVICE3="$TEST_BASE_DIR/device-3"
55DEVICE4="$TEST_BASE_DIR/device-4"
56
57log_must dd if=/dev/urandom of=$PATTERN_FILE bs=1048576 count=4
58
59log_must truncate -s $SPA_MINDEVSIZE $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4
60
61log_must zpool create -O mountpoint=none -f $TESTPOOL $DEVICE1 \
62     log $DEVICE2 cache $DEVICE3 spare $DEVICE4
63log_must zpool export $TESTPOOL
64
65# Overwrite the first 4M of each device and verify the expected labels.
66for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
67	dd if=$PATTERN_FILE of=$dev bs=1048576 conv=notrunc
68	log_must eval "zdb -l $dev | grep 'labels = 2 3'"
69done
70
71# Verify the pool could be imported using those labels.
72log_must eval "zpool import -d $TEST_BASE_DIR | grep $TESTPOOL"
73
74# Verify the last two labels on each vdev can be cleared.
75for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
76	log_must zpool labelclear -f $dev
77done
78
79# Verify there is no longer a pool which can be imported.
80log_mustnot eval "zpool import -d $TEST_BASE_DIR | grep $TESTPOOL"
81
82# Verify the original pattern over the first two labels is intact
83for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
84	log_must dd if=$dev of=$DISK_PATTERN_FILE bs=1048576 count=4
85	log_must cmp $DISK_PATTERN_FILE $PATTERN_FILE
86	log_mustnot zdb -lq $dev
87done
88
89# Verify an error is reported when there are no labels to clear.
90for dev in $DEVICE1 $DEVICE2 $DEVICE3 $DEVICE4; do
91	log_mustnot zpool labelclear -f $dev
92done
93
94log_pass "zpool labelclear will only clear valid labels"
95