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)
62VDEV1=$mntpnt/vdev1; VDEV2=$mntpnt/vdev2;
63VDEV3=$mntpnt/vdev3; VDEV4=$mntpnt/vdev4;
64
65log_must mkfile $MINVDEVSIZE $VDEV1 $VDEV2 $VDEV3
66log_must mkfile $(($MINVDEVSIZE * 2)) $VDEV4
67
68run_and_verify -p "$MPOOL" "zpool create $MPOOL mirror $VDEV1 $VDEV2"
69run_and_verify -p "$MPOOL" "zpool add -f $MPOOL spare $VDEV3"
70run_and_verify -p "$MPOOL" "zpool remove $MPOOL $VDEV3"
71run_and_verify -p "$MPOOL" "zpool offline $MPOOL $VDEV1"
72run_and_verify -p "$MPOOL" "zpool online $MPOOL $VDEV1"
73run_and_verify -p "$MPOOL" "zpool attach $MPOOL $VDEV1 $VDEV4"
74run_and_verify -p "$MPOOL" "zpool detach $MPOOL $VDEV4"
75run_and_verify -p "$MPOOL" "zpool replace -f $MPOOL $VDEV1 $VDEV4"
76run_and_verify -p "$MPOOL" "zpool scrub $MPOOL"
77run_and_verify -p "$MPOOL" "zpool clear $MPOOL"
78
79# For export and destroy, mimic the behavior of run_and_verify using two
80# commands since the history will be unavailable until the pool is imported
81# again.
82commands=("zpool export $MPOOL" "zpool import -d $mntpnt $MPOOL"
83    "zpool destroy $MPOOL" "zpool import -D -f -d $mntpnt $MPOOL")
84for i in 0 2; do
85	cmd1="${commands[$i]}"
86	cmd2="${commands[(($i + 1 ))]}"
87
88	zpool history $MPOOL > $OLD_HISTORY 2>/dev/null
89	log_must $cmd1
90	log_must $cmd2
91	zpool history $MPOOL > $TMP_HISTORY 2>/dev/null
92	diff $OLD_HISTORY $TMP_HISTORY | grep "^> " | sed 's/^> //g' > \
93	    $NEW_HISTORY
94        if is_linux; then
95		grep "$(echo "$cmd1" | sed 's/^.*\/\(zpool .*\).*$/\1/')" \
96		    $NEW_HISTORY >/dev/null 2>&1 || \
97		    log_fail "Didn't find \"$cmd1\" in pool history"
98		grep "$(echo "$cmd2" | sed 's/^.*\/\(zpool .*\).*$/\1/')" \
99		    $NEW_HISTORY >/dev/null 2>&1 || \
100		    log_fail "Didn't find \"$cmd2\" in pool history"
101        else
102		grep "$(echo "$cmd1" | sed 's/\/usr\/sbin\///g')" \
103		    $NEW_HISTORY >/dev/null 2>&1 || \
104		    log_fail "Didn't find \"$cmd1\" in pool history"
105		grep "$(echo "$cmd2" | sed 's/\/usr\/sbin\///g')" \
106		    $NEW_HISTORY >/dev/null 2>&1 || \
107		    log_fail "Didn't find \"$cmd2\" in pool history"
108        fi
109done
110
111run_and_verify -p "$MPOOL" "zpool split $MPOOL ${MPOOL}_split"
112
113import_dir=$TEST_BASE_DIR/import_dir.$$
114log_must mkdir $import_dir
115log_must cp $STF_SUITE/tests/functional/history/zfs-pool-v4.dat.Z $import_dir
116log_must uncompress $import_dir/zfs-pool-v4.dat.Z
117upgrade_pool=$(zpool import -d $import_dir | awk '/pool:/ { print $2 }')
118log_must zpool import -d $import_dir $upgrade_pool
119run_and_verify -p "$upgrade_pool" "zpool upgrade $upgrade_pool"
120
121log_pass "zpool sub-commands which modify state are logged passed. "
122