1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27#
28# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/tests/functional/history/history_common.kshlib
32
33#
34# DESCRIPTION:
35#	Create a scenario to verify the following zpool subcommands are logged.
36#	create, destroy, add, remove, offline, online, attach, detach, replace,
37#	scrub, export, import, clear, upgrade.
38#
39# STRATEGY:
40#	1. Create three virtual disk files and create a mirror.
41#	2. Run and verify pool commands, with special casing for destroy/export.
42#	3. Import a pool and upgrade it, verifying 'upgrade' was logged.
43#
44
45verify_runnable "global"
46
47function cleanup
48{
49	destroy_pool $MPOOL
50	destroy_pool $upgrade_pool
51
52	[[ -d $import_dir ]] && rm -rf $import_dir
53	for file in $VDEV1 $VDEV2 $VDEV3 $VDEV4; do
54		[[ -f $file ]] && rm -f $file
55	done
56}
57
58log_assert "Verify zpool sub-commands which modify state are logged."
59log_onexit cleanup
60
61mntpnt=$(get_prop mountpoint $TESTPOOL)
62(( $? != 0)) && log_fail "get_prop($TESTPOOL mountpoint)"
63VDEV1=$mntpnt/vdev1; VDEV2=$mntpnt/vdev2;
64VDEV3=$mntpnt/vdev3; VDEV4=$mntpnt/vdev4;
65
66log_must mkfile $MINVDEVSIZE $VDEV1 $VDEV2 $VDEV3
67log_must mkfile $(($MINVDEVSIZE * 2)) $VDEV4
68
69run_and_verify -p "$MPOOL" "zpool create $MPOOL mirror $VDEV1 $VDEV2"
70run_and_verify -p "$MPOOL" "zpool add -f $MPOOL spare $VDEV3"
71run_and_verify -p "$MPOOL" "zpool remove $MPOOL $VDEV3"
72run_and_verify -p "$MPOOL" "zpool offline $MPOOL $VDEV1"
73run_and_verify -p "$MPOOL" "zpool online $MPOOL $VDEV1"
74run_and_verify -p "$MPOOL" "zpool attach $MPOOL $VDEV1 $VDEV4"
75run_and_verify -p "$MPOOL" "zpool detach $MPOOL $VDEV4"
76run_and_verify -p "$MPOOL" "zpool replace -f $MPOOL $VDEV1 $VDEV4"
77run_and_verify -p "$MPOOL" "zpool scrub $MPOOL"
78run_and_verify -p "$MPOOL" "zpool clear $MPOOL"
79
80# For export and destroy, mimic the behavior of run_and_verify using two
81# commands since the history will be unavailable until the pool is imported
82# again.
83commands=("zpool export $MPOOL" "zpool import -d $mntpnt $MPOOL"
84    "zpool destroy $MPOOL" "zpool import -D -f -d $mntpnt $MPOOL")
85for i in 0 2; do
86	cmd1="${commands[$i]}"
87	cmd2="${commands[(($i + 1 ))]}"
88
89	zpool history $MPOOL > $OLD_HISTORY 2>/dev/null
90	log_must $cmd1
91	log_must $cmd2
92	zpool history $MPOOL > $TMP_HISTORY 2>/dev/null
93	diff $OLD_HISTORY $TMP_HISTORY | grep "^> " | sed 's/^> //g' > \
94	    $NEW_HISTORY
95	grep "$(echo "$cmd1" | sed 's/\/usr\/sbin\///g')" $NEW_HISTORY \
96	    >/dev/null 2>&1 || log_fail "Didn't find \"$cmd1\" in pool history"
97	grep "$(echo "$cmd2" | sed 's/\/usr\/sbin\///g')" $NEW_HISTORY \
98	    >/dev/null 2>&1 || log_fail "Didn't find \"$cmd2\" in pool history"
99done
100
101run_and_verify -p "$MPOOL" "zpool split $MPOOL ${MPOOL}_split"
102
103import_dir=/var/tmp/import_dir.$$
104log_must mkdir $import_dir
105log_must cp $STF_SUITE/tests/functional/history/zfs-pool-v4.dat.Z $import_dir
106log_must uncompress $import_dir/zfs-pool-v4.dat.Z
107upgrade_pool=$(zpool import -d $import_dir | grep "pool:" | awk '{print $2}')
108log_must zpool import -d $import_dir $upgrade_pool
109run_and_verify -p "$upgrade_pool" "zpool upgrade $upgrade_pool"
110
111log_pass "zpool sub-commands which modify state are logged passed. "
112