diff --git a/cmd/zpool/zpool_main.c b/cmd/zpool/zpool_main.c index d80cbc6c63..b6b6f0cf40 100644 --- a/cmd/zpool/zpool_main.c +++ b/cmd/zpool/zpool_main.c @@ -354,7 +354,7 @@ get_usage(zpool_help_t idx) "\tupgrade -v\n" "\tupgrade [-V version] <-a | pool ...>\n")); case HELP_EVENTS: - return (gettext("\tevents [-vHfc]\n")); + return (gettext("\tevents [-vHf [pool] | -c]\n")); case HELP_GET: return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] " "<\"all\" | property[,...]> ...\n")); @@ -7376,6 +7376,7 @@ typedef struct ev_opts { int scripted; int follow; int clear; + char poolname[ZFS_MAX_DATASET_NAME_LEN]; } ev_opts_t; static void @@ -7643,6 +7644,7 @@ zpool_do_events_next(ev_opts_t *opts) { nvlist_t *nvl; int zevent_fd, ret, dropped; + char *pool; zevent_fd = open(ZFS_DEV, O_RDWR); VERIFY(zevent_fd >= 0); @@ -7659,6 +7661,11 @@ zpool_do_events_next(ev_opts_t *opts) if (dropped > 0) (void) printf(gettext("dropped %d events\n"), dropped); + if (strlen(opts->poolname) > 0 && + nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 && + strcmp(opts->poolname, pool) != 0) + continue; + zpool_do_events_short(nvl, opts); if (opts->verbose) { @@ -7688,7 +7695,7 @@ zpool_do_events_clear(ev_opts_t *opts) } /* - * zpool events [-vfc] + * zpool events [-vHf [pool] | -c] * * Displays events logs by ZFS. */ @@ -7723,6 +7730,25 @@ zpool_do_events(int argc, char **argv) argc -= optind; argv += optind; + if (argc > 1) { + (void) fprintf(stderr, gettext("too many arguments\n")); + usage(B_FALSE); + } else if (argc == 1) { + (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname)); + if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) { + (void) fprintf(stderr, + gettext("invalid pool name '%s'\n"), opts.poolname); + usage(B_FALSE); + } + } + + if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) && + opts.clear) { + (void) fprintf(stderr, + gettext("invalid options combined with -c\n")); + usage(B_FALSE); + } + if (opts.clear) ret = zpool_do_events_clear(&opts); else diff --git a/configure.ac b/configure.ac index 655641afdd..6d1de13f94 100644 --- a/configure.ac +++ b/configure.ac @@ -214,6 +214,7 @@ AC_CONFIG_FILES([ tests/zfs-tests/tests/functional/cli_root/zpool_create/Makefile tests/zfs-tests/tests/functional/cli_root/zpool_destroy/Makefile tests/zfs-tests/tests/functional/cli_root/zpool_detach/Makefile + tests/zfs-tests/tests/functional/cli_root/zpool_events/Makefile tests/zfs-tests/tests/functional/cli_root/zpool_expand/Makefile tests/zfs-tests/tests/functional/cli_root/zpool_export/Makefile tests/zfs-tests/tests/functional/cli_root/zpool_get/Makefile diff --git a/man/man8/zpool.8 b/man/man8/zpool.8 index 5c45b049ba..4eea6689d8 100644 --- a/man/man8/zpool.8 +++ b/man/man8/zpool.8 @@ -68,8 +68,7 @@ .Ar pool device .Nm .Cm events -.Op Fl vHfc -.Op Ar pool +.Op Fl vHf Oo Ar pool Oc | Fl c .Nm .Cm export .Op Fl a @@ -1027,8 +1026,7 @@ command instead. .It Xo .Nm .Cm events -.Op Fl cfHv -.Op Ar pool Ns ... +.Op Fl vHf Oo Ar pool Oc | Fl c .Xc Lists all recent events generated by the ZFS kernel modules. These events are consumed by the diff --git a/tests/runfiles/linux.run b/tests/runfiles/linux.run index f0c785f590..6edb2e1a01 100644 --- a/tests/runfiles/linux.run +++ b/tests/runfiles/linux.run @@ -252,6 +252,10 @@ post = [tests/functional/cli_root/zpool_detach] tests = ['zpool_detach_001_neg'] +[tests/functional/cli_root/zpool_events] +tests = ['zpool_events_clear', 'zpool_events_cliargs', 'zpool_events_follow', + 'zpool_events_poolname'] + [tests/functional/cli_root/zpool_expand] tests = ['zpool_expand_001_pos', 'zpool_expand_002_pos', 'zpool_expand_003_neg', 'zpool_expand_004_pos'] diff --git a/tests/zfs-tests/tests/functional/cli_root/Makefile.am b/tests/zfs-tests/tests/functional/cli_root/Makefile.am index 6c70fa13ec..bc64e3bba7 100644 --- a/tests/zfs-tests/tests/functional/cli_root/Makefile.am +++ b/tests/zfs-tests/tests/functional/cli_root/Makefile.am @@ -37,6 +37,7 @@ SUBDIRS = \ zpool_create \ zpool_destroy \ zpool_detach \ + zpool_events \ zpool_expand \ zpool_export \ zpool_get \ diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/Makefile.am b/tests/zfs-tests/tests/functional/cli_root/zpool_events/Makefile.am new file mode 100644 index 0000000000..44a28e2895 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/Makefile.am @@ -0,0 +1,10 @@ +pkgdatadir = $(datadir)/@PACKAGE@/zfs-tests/tests/functional/cli_root/zpool_events +dist_pkgdata_SCRIPTS = \ + setup.ksh \ + cleanup.ksh \ + zpool_events.cfg \ + zpool_events.kshlib \ + zpool_events_clear.ksh \ + zpool_events_cliargs.ksh \ + zpool_events_follow.ksh \ + zpool_events_poolname.ksh diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/cleanup.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_events/cleanup.ksh new file mode 100755 index 0000000000..1adac153d7 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/cleanup.ksh @@ -0,0 +1,19 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib + +default_cleanup diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/setup.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_events/setup.ksh new file mode 100755 index 0000000000..e37841addf --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/setup.ksh @@ -0,0 +1,21 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib + +DISK=${DISKS%% *} + +default_volume_setup $DISK diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events.cfg b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events.cfg new file mode 100644 index 0000000000..6f5da0a76d --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events.cfg @@ -0,0 +1,17 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +export EVENTS_NUM=42 diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events.kshlib b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events.kshlib new file mode 100755 index 0000000000..a64f14a850 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events.kshlib @@ -0,0 +1,40 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib +. $STF_SUITE/tests/functional/cli_root/zpool_events/zpool_events.cfg + +# +# Wait to allow the kernel module to process ZFS events until we reach $eventnum +# or a timeout of $timeout seconds expire, whichever comes first +# +function zpool_events_settle # [timeout] +{ + typeset eventnum="${1:-$EVENTS_NUM}" + typeset timeout="${2:-3}" + typeset -i count + typeset -i i=0 + + while [[ $i -lt $timeout ]]; do + count=$(zpool events -H | wc -l) + if [[ $count -ge $eventnum ]]; then + break + fi + i=$((i+1)) + sleep 1 + done + log_note "waited $i seconds" +} diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_clear.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_clear.ksh new file mode 100755 index 0000000000..ab862354b8 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_clear.ksh @@ -0,0 +1,58 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib +. $STF_SUITE/tests/functional/cli_root/zpool_events/zpool_events.kshlib + +# +# DESCRIPTION: +# 'zpool events -c' should successfully clear events. +# +# STRATEGY: +# 1. Clear all ZFS events +# 2. Generate some new ZFS events +# 3. Verify 'zpool events -c' successfully clears new events +# + +verify_runnable "both" + +log_assert "'zpool events -c' should successfully clear events." + +# 1. Clear all ZFS events +# This is needed because we may already over the max number or events queued +# (zfs_zevent_len_max) generated by previous tests: generating $EVENTS_NUM new +# events and then counting them is racy and leads to failues, so start from 0. +log_must zpool events -c + +# 2. Generate some new ZFS events +for i in `seq 1 $EVENTS_NUM`; do + log_must zpool clear $TESTPOOL +done +# wait a bit to allow the kernel module to process new events +zpool_events_settle +EVENTS_NUM="$(zpool events -H | wc -l)" + +# 3. Verify 'zpool events -c' successfully clear new events +CLEAR_OUTPUT="$(zpool events -c)" +if [[ "$CLEAR_OUTPUT" != "cleared $EVENTS_NUM events" ]]; then + log_fail "Failed to clear $EVENTS_NUM events: $CLEAR_OUTPUT" +fi +EVENTS_NUM="$(zpool events -H | wc -l)" +if [[ $EVENTS_NUM -ne 0 ]]; then + log_fail "Unexpected events number: $EVENTS_NUM != 0" +fi + +log_pass "'zpool events -c' successfully clears events." diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_cliargs.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_cliargs.ksh new file mode 100755 index 0000000000..1623a18e47 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_cliargs.ksh @@ -0,0 +1,89 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib + +# +# DESCRIPTION: +# 'zpool events' should only work with supported options. +# +# STRATEGY: +# 1. Verify every supported option is accepted +# 2. Verify supported options raise an error with unsupported arguments +# 3. Verify other unsupported options raise an error +# + +verify_runnable "both" + +function log_must_follow # +{ + typeset command="$1" + + log_must eval "$command > /dev/null &" + pid=$! + sleep 3 + kill $pid + if [[ $? -ne 0 ]]; then + log_fail "'$command' does not work as expected." + else + log_note "'$command' works successfully." + fi +} + +log_assert "'zpool events' should only work with supported options." + +typeset goodopts=("" "-v" "-H" "-f" "-vH" "-vf" "-Hf" "-vHf") +typeset badopts=("-vV" "-Hh" "-fF" "-cC" "-x" "-o" "-") + +# 1. Verify every supported option is accepted +for opt in ${goodopts[@]} +do + # when in 'follow' mode we can't use log_must() + if [[ $opt =~ 'f' ]]; then + log_must_follow "zpool events $opt" + log_must_follow "zpool events $opt $TESTPOOL" + else + log_must eval "zpool events $opt > /dev/null" + log_must eval "zpool events $opt $TESTPOOL > /dev/null" + fi +done + +# 2.1 Verify supported options raise an error with unsupported arguments +for opt in ${goodopts[@]} +do + log_mustnot zpool events $opt "/tmp/" + log_mustnot zpool events $opt "$TESTPOOL/fs" + log_mustnot zpool events $opt "$TESTPOOL@snap" + log_mustnot zpool events $opt "$TESTPOOL#bm" + log_mustnot zpool events $opt "$TESTPOOL" "$TESTPOOL" +done + +# 2.2 Additionally, 'zpool events -c' does not support any other option|argument +log_must eval "zpool events -c > /dev/null" +log_mustnot zpool events -c "$TESTPOOL" +for opt in ${goodopts[@]} +do + log_mustnot zpool events -c $opt +done + +# 3. Verify other unsupported options raise an error +for opt in ${badopts[@]} +do + log_mustnot zpool events $opt + log_mustnot zpool events $opt "$TESTPOOL" +done + +log_pass "'zpool events' only works with supported options." diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_follow.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_follow.ksh new file mode 100755 index 0000000000..a996e57c14 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_follow.ksh @@ -0,0 +1,64 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib +. $STF_SUITE/tests/functional/cli_root/zpool_events/zpool_events.kshlib + +# +# DESCRIPTION: +# 'zpool events -f' should successfully follow new events. +# +# STRATEGY: +# 1. Clear all ZFS events +# 2. Run 'zpool events -f' in background, saving its output to a temporary file +# 3. Generate some ZFS events +# 4. Verify 'zpool events -f' successfully recorded these new events +# + +verify_runnable "both" + +function cleanup +{ + [[ -n $pid ]] && kill $pid 2>/dev/null + rm -f $EVENTS_FILE +} + +log_assert "'zpool events -f' should follow new events." +log_onexit cleanup + +EVENTS_FILE="$TESTDIR/zpool_events.$$" + +# 1. Clear all ZFS events +log_must zpool events -c + +# 2. Run 'zpool events -f' in background, saving its output to a temporary file +log_must eval "zpool events -H -f > $EVENTS_FILE &" +pid=$! + +# 3. Generate some ZFS events +for i in `seq 1 $EVENTS_NUM`; do + log_must zpool clear $TESTPOOL +done +# wait a bit to allow the kernel module to process new events +zpool_events_settle + +# 4. Verify 'zpool events -f' successfully recorded these new events +EVENTS_LOG="$(cat $EVENTS_FILE | wc -l)" +if [[ "$EVENTS_LOG" != "$EVENTS_NUM" ]]; then + log_fail "Unexpected number of events: $EVENTS_LOG != $EVENTS_NUM" +fi + +log_pass "'zpool events -f' successfully follows new events." diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_poolname.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_poolname.ksh new file mode 100755 index 0000000000..42c46712f3 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_events/zpool_events_poolname.ksh @@ -0,0 +1,74 @@ +#!/bin/ksh -p +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright 2017, loli10K . All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib +. $STF_SUITE/tests/functional/cli_root/zpool_events/zpool_events.kshlib + +# +# DESCRIPTION: +# 'zpool events poolname' should only display events from the chosen pool. +# +# STRATEGY: +# 1. Create an additional pool +# 2. Clear all ZFS events +# 3. Generate some ZFS events on both pools +# 4. Verify 'zpool events poolname' successfully display events +# + +verify_runnable "both" + +function cleanup +{ + destroy_pool $NEWPOOL + rm -f $DISK +} + +log_assert "'zpool events poolname' should only display events from poolname." +log_onexit cleanup + +NEWPOOL="newpool" +DISK="$TEST_BASE_DIR/$NEWPOOL.dat" + +# 1. Create an additional pool +log_must truncate -s $MINVDEVSIZE $DISK +log_must zpool create $NEWPOOL $DISK + +# 2. Clear all ZFS events +log_must zpool events -c + +# 3. Generate some ZFS events on both pools +for i in `seq 1 $EVENTS_NUM`; do + log_must zpool clear $TESTPOOL +done +for i in `seq 1 $EVENTS_NUM`; do + log_must zpool clear $NEWPOOL +done +# wait a bit to allow the kernel module to process new events +zpool_events_settle + +# 4. Verify 'zpool events poolname' successfully display events +zpool events -v $TESTPOOL | + awk -v POOL=$TESTPOOL '/pool = / {if ($3 != "\""POOL"\"") exit 1}' +if [[ $? -ne 0 ]]; then + log_fail "Unexpected events for pools other than $TESTPOOL" +fi +zpool events -v $NEWPOOL | + awk -v POOL=$NEWPOOL '/pool = / {if ($3 != "\""POOL"\"") exit 1}' +if [[ $? -ne 0 ]]; then + log_fail "Unexpected events for pools other than $NEWPOOL" +fi + +log_pass "'zpool events poolname' display events only from the chosen pool." diff --git a/tests/zfs-tests/tests/functional/fault/auto_online_001_pos.ksh b/tests/zfs-tests/tests/functional/fault/auto_online_001_pos.ksh index 50f721c27f..0f6e38ac22 100755 --- a/tests/zfs-tests/tests/functional/fault/auto_online_001_pos.ksh +++ b/tests/zfs-tests/tests/functional/fault/auto_online_001_pos.ksh @@ -111,7 +111,7 @@ do fi # Clear zpool events - zpool events -c $TESTPOOL + log_must zpool events -c # Online disk insert_disk $offline_disk $host diff --git a/tests/zfs-tests/tests/functional/fault/auto_replace_001_pos.ksh b/tests/zfs-tests/tests/functional/fault/auto_replace_001_pos.ksh index 0f71a575e1..40a680a9bb 100755 --- a/tests/zfs-tests/tests/functional/fault/auto_replace_001_pos.ksh +++ b/tests/zfs-tests/tests/functional/fault/auto_replace_001_pos.ksh @@ -122,7 +122,7 @@ if (($? != 0)); then fi # Clear zpool events -zpool events -c $TESTPOOL +log_must zpool events -c # Create another scsi_debug device setup