4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
48 #include <sys/fs/zfs.h>
50 #include <sys/fm/util.h>
51 #include <sys/fm/protocol.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 #include "statcommon.h"
61 static int zpool_do_create(int, char **);
62 static int zpool_do_destroy(int, char **);
64 static int zpool_do_add(int, char **);
65 static int zpool_do_remove(int, char **);
67 static int zpool_do_list(int, char **);
68 static int zpool_do_iostat(int, char **);
69 static int zpool_do_status(int, char **);
71 static int zpool_do_online(int, char **);
72 static int zpool_do_offline(int, char **);
73 static int zpool_do_clear(int, char **);
74 static int zpool_do_reopen(int, char **);
76 static int zpool_do_reguid(int, char **);
78 static int zpool_do_attach(int, char **);
79 static int zpool_do_detach(int, char **);
80 static int zpool_do_replace(int, char **);
81 static int zpool_do_split(int, char **);
83 static int zpool_do_scrub(int, char **);
85 static int zpool_do_import(int, char **);
86 static int zpool_do_export(int, char **);
88 static int zpool_do_upgrade(int, char **);
90 static int zpool_do_history(int, char **);
91 static int zpool_do_events(int, char **);
93 static int zpool_do_get(int, char **);
94 static int zpool_do_set(int, char **);
97 * These libumem hooks provide a reasonable set of defaults for the allocator's
98 * debugging facilities.
103 _umem_debug_init(void)
105 return ("default,verbose"); /* $UMEM_DEBUG setting */
109 _umem_logging_init(void)
111 return ("fail,contents"); /* $UMEM_LOGGING setting */
143 typedef struct zpool_command {
145 int (*func)(int, char **);
150 * Master command table. Each ZFS command has a name, associated function, and
151 * usage message. The usage messages need to be internationalized, so we have
152 * to have a function to return the usage message based on a command index.
154 * These commands are organized according to how they are displayed in the usage
155 * message. An empty command (one with a NULL name) indicates an empty line in
156 * the generic usage message.
158 static zpool_command_t command_table[] = {
159 { "create", zpool_do_create, HELP_CREATE },
160 { "destroy", zpool_do_destroy, HELP_DESTROY },
162 { "add", zpool_do_add, HELP_ADD },
163 { "remove", zpool_do_remove, HELP_REMOVE },
165 { "list", zpool_do_list, HELP_LIST },
166 { "iostat", zpool_do_iostat, HELP_IOSTAT },
167 { "status", zpool_do_status, HELP_STATUS },
169 { "online", zpool_do_online, HELP_ONLINE },
170 { "offline", zpool_do_offline, HELP_OFFLINE },
171 { "clear", zpool_do_clear, HELP_CLEAR },
172 { "reopen", zpool_do_reopen, HELP_REOPEN },
174 { "attach", zpool_do_attach, HELP_ATTACH },
175 { "detach", zpool_do_detach, HELP_DETACH },
176 { "replace", zpool_do_replace, HELP_REPLACE },
177 { "split", zpool_do_split, HELP_SPLIT },
179 { "scrub", zpool_do_scrub, HELP_SCRUB },
181 { "import", zpool_do_import, HELP_IMPORT },
182 { "export", zpool_do_export, HELP_EXPORT },
183 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
184 { "reguid", zpool_do_reguid, HELP_REGUID },
186 { "history", zpool_do_history, HELP_HISTORY },
187 { "events", zpool_do_events, HELP_EVENTS },
189 { "get", zpool_do_get, HELP_GET },
190 { "set", zpool_do_set, HELP_SET },
193 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
195 zpool_command_t *current_command;
196 static char history_str[HIS_MAX_RECORD_LEN];
198 static uint_t timestamp_fmt = NODATE;
201 get_usage(zpool_help_t idx) {
204 return (gettext("\tadd [-fn] [-o property=value] "
205 "<pool> <vdev> ...\n"));
207 return (gettext("\tattach [-f] [-o property=value] "
208 "<pool> <device> <new-device>\n"));
210 return (gettext("\tclear [-nF] <pool> [device]\n"));
212 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
213 "\t [-O file-system-property=value] ... \n"
214 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
216 return (gettext("\tdestroy [-f] <pool>\n"));
218 return (gettext("\tdetach <pool> <device>\n"));
220 return (gettext("\texport [-f] <pool> ...\n"));
222 return (gettext("\thistory [-il] [<pool>] ...\n"));
224 return (gettext("\timport [-d dir] [-D]\n"
225 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
226 "\timport [-o mntopts] [-o property=value] ... \n"
227 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
228 "[-R root] [-F [-n]] -a\n"
229 "\timport [-o mntopts] [-o property=value] ... \n"
230 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
231 "[-R root] [-F [-n]]\n"
232 "\t <pool | id> [newpool]\n"));
234 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
237 return (gettext("\tlist [-H] [-o property[,...]] "
238 "[-T d|u] [pool] ... [interval [count]]\n"));
240 return (gettext("\toffline [-t] <pool> <device> ...\n"));
242 return (gettext("\tonline <pool> <device> ...\n"));
244 return (gettext("\treplace [-f] <pool> <device> "
247 return (gettext("\tremove <pool> <device> ...\n"));
249 return (""); /* Undocumented command */
251 return (gettext("\tscrub [-s] <pool> ...\n"));
253 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
256 return (gettext("\tupgrade\n"
258 "\tupgrade [-V version] <-a | pool ...>\n"));
260 return (gettext("\tevents [-vHfc]\n"));
262 return (gettext("\tget <\"all\" | property[,...]> "
265 return (gettext("\tset <property=value> <pool> \n"));
267 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
268 "\t [-o property=value] <pool> <newpool> "
269 "[<device> ...]\n"));
271 return (gettext("\treguid <pool>\n"));
280 * Callback routine that will print out a pool property value.
283 print_prop_cb(int prop, void *cb)
287 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
289 if (zpool_prop_readonly(prop))
290 (void) fprintf(fp, " NO ");
292 (void) fprintf(fp, " YES ");
294 if (zpool_prop_values(prop) == NULL)
295 (void) fprintf(fp, "-\n");
297 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
303 * Display usage message. If we're inside a command, display only the usage for
304 * that command. Otherwise, iterate over the entire command table and display
305 * a complete usage message.
308 usage(boolean_t requested)
310 FILE *fp = requested ? stdout : stderr;
312 if (current_command == NULL) {
315 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
317 gettext("where 'command' is one of the following:\n\n"));
319 for (i = 0; i < NCOMMAND; i++) {
320 if (command_table[i].name == NULL)
321 (void) fprintf(fp, "\n");
323 (void) fprintf(fp, "%s",
324 get_usage(command_table[i].usage));
327 (void) fprintf(fp, gettext("usage:\n"));
328 (void) fprintf(fp, "%s", get_usage(current_command->usage));
331 if (current_command != NULL &&
332 ((strcmp(current_command->name, "set") == 0) ||
333 (strcmp(current_command->name, "get") == 0) ||
334 (strcmp(current_command->name, "list") == 0))) {
337 gettext("\nthe following properties are supported:\n"));
339 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
340 "PROPERTY", "EDIT", "VALUES");
342 /* Iterate over all properties */
343 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
346 (void) fprintf(fp, "\t%-15s ", "feature@...");
347 (void) fprintf(fp, "YES disabled | enabled | active\n");
349 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
350 "appended with a feature name.\nSee zpool-features(5).\n"));
354 * See comments at end of main().
356 if (getenv("ZFS_ABORT") != NULL) {
357 (void) printf("dumping core by request\n");
361 exit(requested ? 0 : 2);
365 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
366 boolean_t print_logs)
373 (void) printf("\t%*s%s\n", indent, "", name);
375 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
376 &child, &children) != 0)
379 for (c = 0; c < children; c++) {
380 uint64_t is_log = B_FALSE;
382 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
384 if ((is_log && !print_logs) || (!is_log && print_logs))
387 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
388 print_vdev_tree(zhp, vname, child[c], indent + 2,
395 * Add a property pair (name, string-value) into a property nvlist.
398 add_prop_list(const char *propname, char *propval, nvlist_t **props,
401 zpool_prop_t prop = ZPROP_INVAL;
407 if (*props == NULL &&
408 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
409 (void) fprintf(stderr,
410 gettext("internal error: out of memory\n"));
417 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
418 !zpool_prop_feature(propname)) {
419 (void) fprintf(stderr, gettext("property '%s' is "
420 "not a valid pool property\n"), propname);
423 if (zpool_prop_feature(propname))
426 normnm = zpool_prop_to_name(prop);
428 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
429 normnm = zfs_prop_to_name(fprop);
435 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
436 prop != ZPOOL_PROP_CACHEFILE) {
437 (void) fprintf(stderr, gettext("property '%s' "
438 "specified multiple times\n"), propname);
442 if (nvlist_add_string(proplist, normnm, propval) != 0) {
443 (void) fprintf(stderr, gettext("internal "
444 "error: out of memory\n"));
452 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
454 * -f Force addition of devices, even if they appear in use
455 * -n Do not add the devices, but display the resulting layout if
456 * they were to be added.
457 * -o Set property=value.
459 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
460 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
464 zpool_do_add(int argc, char **argv)
466 boolean_t force = B_FALSE;
467 boolean_t dryrun = B_FALSE;
474 nvlist_t *props = NULL;
478 while ((c = getopt(argc, argv, "fno:")) != -1) {
487 if ((propval = strchr(optarg, '=')) == NULL) {
488 (void) fprintf(stderr, gettext("missing "
489 "'=' for -o option\n"));
495 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
496 (add_prop_list(optarg, propval, &props, B_TRUE)))
500 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
509 /* get pool name and check number of arguments */
511 (void) fprintf(stderr, gettext("missing pool name argument\n"));
515 (void) fprintf(stderr, gettext("missing vdev specification\n"));
524 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
527 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
528 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
534 /* pass off to get_vdev_spec for processing */
535 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
537 if (nvroot == NULL) {
543 nvlist_t *poolnvroot;
545 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
548 (void) printf(gettext("would update '%s' to the following "
549 "configuration:\n"), zpool_get_name(zhp));
551 /* print original main pool and new tree */
552 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
553 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
555 /* Do the same for the logs */
556 if (num_logs(poolnvroot) > 0) {
557 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
558 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
559 } else if (num_logs(nvroot) > 0) {
560 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
565 ret = (zpool_add(zhp, nvroot) != 0);
576 * zpool remove <pool> <vdev> ...
578 * Removes the given vdev from the pool. Currently, this supports removing
579 * spares, cache, and log devices from the pool.
582 zpool_do_remove(int argc, char **argv)
591 /* get pool name and check number of arguments */
593 (void) fprintf(stderr, gettext("missing pool name argument\n"));
597 (void) fprintf(stderr, gettext("missing device\n"));
603 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
606 for (i = 1; i < argc; i++) {
607 if (zpool_vdev_remove(zhp, argv[i]) != 0)
615 * zpool create [-fnd] [-o property=value] ...
616 * [-O file-system-property=value] ...
617 * [-R root] [-m mountpoint] <pool> <dev> ...
619 * -f Force creation, even if devices appear in use
620 * -n Do not create the pool, but display the resulting layout if it
621 * were to be created.
622 * -R Create a pool under an alternate root
623 * -m Set default mountpoint for the root dataset. By default it's
625 * -o Set property=value.
626 * -d Don't automatically enable all supported pool features
627 * (individual features can be enabled with -o).
628 * -O Set fsproperty=value in the pool's root file system
630 * Creates the named pool according to the given vdev specification. The
631 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
632 * we get the nvlist back from get_vdev_spec(), we either print out the contents
633 * (if '-n' was specified), or pass it to libzfs to do the creation.
636 zpool_do_create(int argc, char **argv)
638 boolean_t force = B_FALSE;
639 boolean_t dryrun = B_FALSE;
640 boolean_t enable_all_pool_feat = B_TRUE;
642 nvlist_t *nvroot = NULL;
645 char *altroot = NULL;
646 char *mountpoint = NULL;
647 nvlist_t *fsprops = NULL;
648 nvlist_t *props = NULL;
652 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
661 enable_all_pool_feat = B_FALSE;
665 if (add_prop_list(zpool_prop_to_name(
666 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
668 if (nvlist_lookup_string(props,
669 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
672 if (add_prop_list(zpool_prop_to_name(
673 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
680 if ((propval = strchr(optarg, '=')) == NULL) {
681 (void) fprintf(stderr, gettext("missing "
682 "'=' for -o option\n"));
688 if (add_prop_list(optarg, propval, &props, B_TRUE))
692 * If the user is creating a pool that doesn't support
693 * feature flags, don't enable any features.
695 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
699 ver = strtoull(propval, &end, 10);
701 ver < SPA_VERSION_FEATURES) {
702 enable_all_pool_feat = B_FALSE;
707 if ((propval = strchr(optarg, '=')) == NULL) {
708 (void) fprintf(stderr, gettext("missing "
709 "'=' for -O option\n"));
715 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
719 (void) fprintf(stderr, gettext("missing argument for "
720 "'%c' option\n"), optopt);
723 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
732 /* get pool name and check number of arguments */
734 (void) fprintf(stderr, gettext("missing pool name argument\n"));
738 (void) fprintf(stderr, gettext("missing vdev specification\n"));
745 * As a special case, check for use of '/' in the name, and direct the
746 * user to use 'zfs create' instead.
748 if (strchr(poolname, '/') != NULL) {
749 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
750 "character '/' in pool name\n"), poolname);
751 (void) fprintf(stderr, gettext("use 'zfs create' to "
752 "create a dataset\n"));
756 /* pass off to get_vdev_spec for bulk processing */
757 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
762 /* make_root_vdev() allows 0 toplevel children if there are spares */
763 if (!zfs_allocatable_devs(nvroot)) {
764 (void) fprintf(stderr, gettext("invalid vdev "
765 "specification: at least one toplevel vdev must be "
770 if (altroot != NULL && altroot[0] != '/') {
771 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
772 "must be an absolute path\n"), altroot);
777 * Check the validity of the mountpoint and direct the user to use the
778 * '-m' mountpoint option if it looks like its in use.
780 if (mountpoint == NULL ||
781 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
782 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
783 char buf[MAXPATHLEN];
786 if (mountpoint && mountpoint[0] != '/') {
787 (void) fprintf(stderr, gettext("invalid mountpoint "
788 "'%s': must be an absolute path, 'legacy', or "
789 "'none'\n"), mountpoint);
793 if (mountpoint == NULL) {
795 (void) snprintf(buf, sizeof (buf), "%s/%s",
798 (void) snprintf(buf, sizeof (buf), "/%s",
802 (void) snprintf(buf, sizeof (buf), "%s%s",
803 altroot, mountpoint);
805 (void) snprintf(buf, sizeof (buf), "%s",
809 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
810 (void) fprintf(stderr, gettext("mountpoint '%s' : "
811 "%s\n"), buf, strerror(errno));
812 (void) fprintf(stderr, gettext("use '-m' "
813 "option to provide a different default\n"));
818 while (count < 3 && readdir(dirp) != NULL)
820 (void) closedir(dirp);
823 (void) fprintf(stderr, gettext("mountpoint "
824 "'%s' exists and is not empty\n"), buf);
825 (void) fprintf(stderr, gettext("use '-m' "
826 "option to provide a "
827 "different default\n"));
835 * For a dry run invocation, print out a basic message and run
836 * through all the vdevs in the list and print out in an
837 * appropriate hierarchy.
839 (void) printf(gettext("would create '%s' with the "
840 "following layout:\n\n"), poolname);
842 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
843 if (num_logs(nvroot) > 0)
844 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
849 * Hand off to libzfs.
851 if (enable_all_pool_feat) {
853 for (i = 0; i < SPA_FEATURES; i++) {
854 char propname[MAXPATHLEN];
855 zfeature_info_t *feat = &spa_feature_table[i];
857 (void) snprintf(propname, sizeof (propname),
858 "feature@%s", feat->fi_uname);
861 * Skip feature if user specified it manually
862 * on the command line.
864 if (nvlist_exists(props, propname))
867 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
868 &props, B_TRUE) != 0)
872 if (zpool_create(g_zfs, poolname,
873 nvroot, props, fsprops) == 0) {
874 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
875 ZFS_TYPE_FILESYSTEM);
877 if (mountpoint != NULL)
878 verify(zfs_prop_set(pool,
880 ZFS_PROP_MOUNTPOINT),
882 if (zfs_mount(pool, NULL, 0) == 0)
883 ret = zfs_shareall(pool);
886 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
887 (void) fprintf(stderr, gettext("pool name may have "
894 nvlist_free(fsprops);
898 nvlist_free(fsprops);
905 * zpool destroy <pool>
907 * -f Forcefully unmount any datasets
909 * Destroy the given pool. Automatically unmounts any datasets in the pool.
912 zpool_do_destroy(int argc, char **argv)
914 boolean_t force = B_FALSE;
921 while ((c = getopt(argc, argv, "f")) != -1) {
927 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
936 /* check arguments */
938 (void) fprintf(stderr, gettext("missing pool argument\n"));
942 (void) fprintf(stderr, gettext("too many arguments\n"));
948 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
950 * As a special case, check for use of '/' in the name, and
951 * direct the user to use 'zfs destroy' instead.
953 if (strchr(pool, '/') != NULL)
954 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
955 "destroy a dataset\n"));
959 if (zpool_disable_datasets(zhp, force) != 0) {
960 (void) fprintf(stderr, gettext("could not destroy '%s': "
961 "could not unmount datasets\n"), zpool_get_name(zhp));
965 ret = (zpool_destroy(zhp) != 0);
973 * zpool export [-f] <pool> ...
975 * -f Forcefully unmount datasets
977 * Export the given pools. By default, the command will attempt to cleanly
978 * unmount any active datasets within the pool. If the '-f' flag is specified,
979 * then the datasets will be forcefully unmounted.
982 zpool_do_export(int argc, char **argv)
984 boolean_t force = B_FALSE;
985 boolean_t hardforce = B_FALSE;
992 while ((c = getopt(argc, argv, "fF")) != -1) {
1001 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1010 /* check arguments */
1012 (void) fprintf(stderr, gettext("missing pool argument\n"));
1017 for (i = 0; i < argc; i++) {
1018 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1023 if (zpool_disable_datasets(zhp, force) != 0) {
1030 if (zpool_export_force(zhp) != 0)
1032 } else if (zpool_export(zhp, force) != 0) {
1043 * Given a vdev configuration, determine the maximum width needed for the device
1047 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1049 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1054 if (strlen(name) + depth > max)
1055 max = strlen(name) + depth;
1059 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1060 &child, &children) == 0) {
1061 for (c = 0; c < children; c++)
1062 if ((ret = max_width(zhp, child[c], depth + 2,
1067 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1068 &child, &children) == 0) {
1069 for (c = 0; c < children; c++)
1070 if ((ret = max_width(zhp, child[c], depth + 2,
1075 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1076 &child, &children) == 0) {
1077 for (c = 0; c < children; c++)
1078 if ((ret = max_width(zhp, child[c], depth + 2,
1087 typedef struct spare_cbdata {
1089 zpool_handle_t *cb_zhp;
1093 find_vdev(nvlist_t *nv, uint64_t search)
1099 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1103 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1104 &child, &children) == 0) {
1105 for (c = 0; c < children; c++)
1106 if (find_vdev(child[c], search))
1114 find_spare(zpool_handle_t *zhp, void *data)
1116 spare_cbdata_t *cbp = data;
1117 nvlist_t *config, *nvroot;
1119 config = zpool_get_config(zhp, NULL);
1120 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1123 if (find_vdev(nvroot, cbp->cb_guid)) {
1133 * Print out configuration state as requested by status_callback.
1136 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1137 int namewidth, int depth, boolean_t isspare)
1141 pool_scan_stat_t *ps = NULL;
1143 char rbuf[6], wbuf[6], cbuf[6];
1145 uint64_t notpresent;
1149 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1150 &child, &children) != 0)
1153 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1154 (uint64_t **)&vs, &c) == 0);
1156 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1159 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1162 if (vs->vs_aux == VDEV_AUX_SPARED)
1164 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1168 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1172 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1173 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1174 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1175 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1178 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1179 ¬present) == 0) {
1181 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1182 (void) printf(" was %s", path);
1183 } else if (vs->vs_aux != 0) {
1186 switch (vs->vs_aux) {
1187 case VDEV_AUX_OPEN_FAILED:
1188 (void) printf(gettext("cannot open"));
1191 case VDEV_AUX_BAD_GUID_SUM:
1192 (void) printf(gettext("missing device"));
1195 case VDEV_AUX_NO_REPLICAS:
1196 (void) printf(gettext("insufficient replicas"));
1199 case VDEV_AUX_VERSION_NEWER:
1200 (void) printf(gettext("newer version"));
1203 case VDEV_AUX_UNSUP_FEAT:
1204 (void) printf(gettext("unsupported feature(s)"));
1207 case VDEV_AUX_SPARED:
1208 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1210 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1211 if (strcmp(zpool_get_name(cb.cb_zhp),
1212 zpool_get_name(zhp)) == 0)
1213 (void) printf(gettext("currently in "
1216 (void) printf(gettext("in use by "
1218 zpool_get_name(cb.cb_zhp));
1219 zpool_close(cb.cb_zhp);
1221 (void) printf(gettext("currently in use"));
1225 case VDEV_AUX_ERR_EXCEEDED:
1226 (void) printf(gettext("too many errors"));
1229 case VDEV_AUX_IO_FAILURE:
1230 (void) printf(gettext("experienced I/O failures"));
1233 case VDEV_AUX_BAD_LOG:
1234 (void) printf(gettext("bad intent log"));
1237 case VDEV_AUX_EXTERNAL:
1238 (void) printf(gettext("external device fault"));
1241 case VDEV_AUX_SPLIT_POOL:
1242 (void) printf(gettext("split into new pool"));
1246 (void) printf(gettext("corrupted data"));
1251 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1252 (uint64_t **)&ps, &c);
1254 if (ps && ps->pss_state == DSS_SCANNING &&
1255 vs->vs_scan_processed != 0 && children == 0) {
1256 (void) printf(gettext(" (%s)"),
1257 (ps->pss_func == POOL_SCAN_RESILVER) ?
1258 "resilvering" : "repairing");
1261 (void) printf("\n");
1263 for (c = 0; c < children; c++) {
1264 uint64_t islog = B_FALSE, ishole = B_FALSE;
1266 /* Don't print logs or holes here */
1267 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1269 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1271 if (islog || ishole)
1273 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1274 print_status_config(zhp, vname, child[c],
1275 namewidth, depth + 2, isspare);
1282 * Print the configuration of an exported pool. Iterate over all vdevs in the
1283 * pool, printing out the name and status for each one.
1286 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1293 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1294 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1295 strcmp(type, VDEV_TYPE_HOLE) == 0)
1298 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1299 (uint64_t **)&vs, &c) == 0);
1301 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1302 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1304 if (vs->vs_aux != 0) {
1307 switch (vs->vs_aux) {
1308 case VDEV_AUX_OPEN_FAILED:
1309 (void) printf(gettext("cannot open"));
1312 case VDEV_AUX_BAD_GUID_SUM:
1313 (void) printf(gettext("missing device"));
1316 case VDEV_AUX_NO_REPLICAS:
1317 (void) printf(gettext("insufficient replicas"));
1320 case VDEV_AUX_VERSION_NEWER:
1321 (void) printf(gettext("newer version"));
1324 case VDEV_AUX_UNSUP_FEAT:
1325 (void) printf(gettext("unsupported feature(s)"));
1328 case VDEV_AUX_ERR_EXCEEDED:
1329 (void) printf(gettext("too many errors"));
1333 (void) printf(gettext("corrupted data"));
1337 (void) printf("\n");
1339 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1340 &child, &children) != 0)
1343 for (c = 0; c < children; c++) {
1344 uint64_t is_log = B_FALSE;
1346 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1351 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1352 print_import_config(vname, child[c], namewidth, depth + 2);
1356 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1357 &child, &children) == 0) {
1358 (void) printf(gettext("\tcache\n"));
1359 for (c = 0; c < children; c++) {
1360 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1361 (void) printf("\t %s\n", vname);
1366 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1367 &child, &children) == 0) {
1368 (void) printf(gettext("\tspares\n"));
1369 for (c = 0; c < children; c++) {
1370 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1371 (void) printf("\t %s\n", vname);
1379 * Logs are recorded as top level vdevs in the main pool child array
1380 * but with "is_log" set to 1. We use either print_status_config() or
1381 * print_import_config() to print the top level logs then any log
1382 * children (eg mirrored slogs) are printed recursively - which
1383 * works because only the top level vdev is marked "is_log"
1386 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1391 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1395 (void) printf(gettext("\tlogs\n"));
1397 for (c = 0; c < children; c++) {
1398 uint64_t is_log = B_FALSE;
1401 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1405 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1407 print_status_config(zhp, name, child[c], namewidth,
1410 print_import_config(name, child[c], namewidth, 2);
1416 * Display the status for the given pool.
1419 show_import(nvlist_t *config)
1421 uint64_t pool_state;
1433 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1435 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1437 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1439 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1442 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1443 (uint64_t **)&vs, &vsc) == 0);
1444 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1446 reason = zpool_import_status(config, &msgid);
1448 (void) printf(gettext(" pool: %s\n"), name);
1449 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1450 (void) printf(gettext(" state: %s"), health);
1451 if (pool_state == POOL_STATE_DESTROYED)
1452 (void) printf(gettext(" (DESTROYED)"));
1453 (void) printf("\n");
1456 case ZPOOL_STATUS_MISSING_DEV_R:
1457 case ZPOOL_STATUS_MISSING_DEV_NR:
1458 case ZPOOL_STATUS_BAD_GUID_SUM:
1459 (void) printf(gettext(" status: One or more devices are "
1460 "missing from the system.\n"));
1463 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1464 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1465 (void) printf(gettext(" status: One or more devices contains "
1466 "corrupted data.\n"));
1469 case ZPOOL_STATUS_CORRUPT_DATA:
1471 gettext(" status: The pool data is corrupted.\n"));
1474 case ZPOOL_STATUS_OFFLINE_DEV:
1475 (void) printf(gettext(" status: One or more devices "
1476 "are offlined.\n"));
1479 case ZPOOL_STATUS_CORRUPT_POOL:
1480 (void) printf(gettext(" status: The pool metadata is "
1484 case ZPOOL_STATUS_VERSION_OLDER:
1485 (void) printf(gettext(" status: The pool is formatted using an "
1486 "older on-disk version.\n"));
1489 case ZPOOL_STATUS_VERSION_NEWER:
1490 (void) printf(gettext(" status: The pool is formatted using an "
1491 "incompatible version.\n"));
1494 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1495 (void) printf(gettext("status: The pool uses the following "
1496 "feature(s) not supported on this sytem:\n"));
1497 zpool_print_unsup_feat(config);
1500 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1501 (void) printf(gettext("status: The pool can only be accessed "
1502 "in read-only mode on this system. It\n\tcannot be "
1503 "accessed in read-write mode because it uses the "
1504 "following\n\tfeature(s) not supported on this system:\n"));
1505 zpool_print_unsup_feat(config);
1508 case ZPOOL_STATUS_HOSTID_MISMATCH:
1509 (void) printf(gettext(" status: The pool was last accessed by "
1510 "another system.\n"));
1513 case ZPOOL_STATUS_FAULTED_DEV_R:
1514 case ZPOOL_STATUS_FAULTED_DEV_NR:
1515 (void) printf(gettext(" status: One or more devices are "
1519 case ZPOOL_STATUS_BAD_LOG:
1520 (void) printf(gettext(" status: An intent log record cannot be "
1524 case ZPOOL_STATUS_RESILVERING:
1525 (void) printf(gettext(" status: One or more devices were being "
1531 * No other status can be seen when importing pools.
1533 assert(reason == ZPOOL_STATUS_OK);
1537 * Print out an action according to the overall state of the pool.
1539 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1540 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1541 (void) printf(gettext(" action: The pool can be "
1542 "imported using its name or numeric identifier, "
1543 "though\n\tsome features will not be available "
1544 "without an explicit 'zpool upgrade'.\n"));
1545 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1546 (void) printf(gettext(" action: The pool can be "
1547 "imported using its name or numeric "
1548 "identifier and\n\tthe '-f' flag.\n"));
1550 (void) printf(gettext(" action: The pool can be "
1551 "imported using its name or numeric "
1553 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1554 (void) printf(gettext(" action: The pool can be imported "
1555 "despite missing or damaged devices. The\n\tfault "
1556 "tolerance of the pool may be compromised if imported.\n"));
1559 case ZPOOL_STATUS_VERSION_NEWER:
1560 (void) printf(gettext(" action: The pool cannot be "
1561 "imported. Access the pool on a system running "
1562 "newer\n\tsoftware, or recreate the pool from "
1565 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1566 (void) printf(gettext("action: The pool cannot be "
1567 "imported. Access the pool on a system that "
1568 "supports\n\tthe required feature(s), or recreate "
1569 "the pool from backup.\n"));
1571 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1572 (void) printf(gettext("action: The pool cannot be "
1573 "imported in read-write mode. Import the pool "
1575 "\t\"-o readonly=on\", access the pool on a system "
1576 "that supports the\n\trequired feature(s), or "
1577 "recreate the pool from backup.\n"));
1579 case ZPOOL_STATUS_MISSING_DEV_R:
1580 case ZPOOL_STATUS_MISSING_DEV_NR:
1581 case ZPOOL_STATUS_BAD_GUID_SUM:
1582 (void) printf(gettext(" action: The pool cannot be "
1583 "imported. Attach the missing\n\tdevices and try "
1587 (void) printf(gettext(" action: The pool cannot be "
1588 "imported due to damaged devices or data.\n"));
1592 /* Print the comment attached to the pool. */
1593 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1594 (void) printf(gettext("comment: %s\n"), comment);
1597 * If the state is "closed" or "can't open", and the aux state
1598 * is "corrupt data":
1600 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1601 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1602 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1603 if (pool_state == POOL_STATE_DESTROYED)
1604 (void) printf(gettext("\tThe pool was destroyed, "
1605 "but can be imported using the '-Df' flags.\n"));
1606 else if (pool_state != POOL_STATE_EXPORTED)
1607 (void) printf(gettext("\tThe pool may be active on "
1608 "another system, but can be imported using\n\t"
1609 "the '-f' flag.\n"));
1613 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1616 (void) printf(gettext(" config:\n\n"));
1618 namewidth = max_width(NULL, nvroot, 0, 0);
1622 print_import_config(name, nvroot, namewidth, 0);
1623 if (num_logs(nvroot) > 0)
1624 print_logs(NULL, nvroot, namewidth, B_FALSE);
1626 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1627 (void) printf(gettext("\n\tAdditional devices are known to "
1628 "be part of this pool, though their\n\texact "
1629 "configuration cannot be determined.\n"));
1634 * Perform the import for the given configuration. This passes the heavy
1635 * lifting off to zpool_import_props(), and then mounts the datasets contained
1639 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1640 nvlist_t *props, int flags)
1642 zpool_handle_t *zhp;
1647 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1650 verify(nvlist_lookup_uint64(config,
1651 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1652 verify(nvlist_lookup_uint64(config,
1653 ZPOOL_CONFIG_VERSION, &version) == 0);
1654 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1655 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1656 "is formatted using an unsupported ZFS version\n"), name);
1658 } else if (state != POOL_STATE_EXPORTED &&
1659 !(flags & ZFS_IMPORT_ANY_HOST)) {
1662 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1664 unsigned long system_hostid = gethostid() & 0xffffffff;
1666 if ((unsigned long)hostid != system_hostid) {
1671 verify(nvlist_lookup_string(config,
1672 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1673 verify(nvlist_lookup_uint64(config,
1674 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1676 (void) fprintf(stderr, gettext("cannot import "
1677 "'%s': pool may be in use from other "
1678 "system, it was last accessed by %s "
1679 "(hostid: 0x%lx) on %s"), name, hostname,
1680 (unsigned long)hostid,
1681 asctime(localtime(&t)));
1682 (void) fprintf(stderr, gettext("use '-f' to "
1683 "import anyway\n"));
1687 (void) fprintf(stderr, gettext("cannot import '%s': "
1688 "pool may be in use from other system\n"), name);
1689 (void) fprintf(stderr, gettext("use '-f' to import "
1695 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1698 if (newname != NULL)
1699 name = (char *)newname;
1701 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1704 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1705 !(flags & ZFS_IMPORT_ONLY) &&
1706 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1716 * zpool import [-d dir] [-D]
1717 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1718 * [-d dir | -c cachefile] [-f] -a
1719 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1720 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1722 * -c Read pool information from a cachefile instead of searching
1725 * -d Scan in a specific directory, other than /dev/. More than
1726 * one directory can be specified using multiple '-d' options.
1728 * -D Scan for previously destroyed pools or import all or only
1729 * specified destroyed pools.
1731 * -R Temporarily import the pool, with all mountpoints relative to
1732 * the given root. The pool will remain exported when the machine
1735 * -V Import even in the presence of faulted vdevs. This is an
1736 * intentionally undocumented option for testing purposes, and
1737 * treats the pool configuration as complete, leaving any bad
1738 * vdevs in the FAULTED state. In other words, it does verbatim
1741 * -f Force import, even if it appears that the pool is active.
1743 * -F Attempt rewind if necessary.
1745 * -n See if rewind would work, but don't actually rewind.
1747 * -N Import the pool but don't mount datasets.
1749 * -T Specify a starting txg to use for import. This option is
1750 * intentionally undocumented option for testing purposes.
1752 * -a Import all pools found.
1754 * -o Set property=value and/or temporary mount options (without '=').
1756 * The import command scans for pools to import, and import pools based on pool
1757 * name and GUID. The pool can also be renamed as part of the import process.
1760 zpool_do_import(int argc, char **argv)
1762 char **searchdirs = NULL;
1763 char *env, *envdup = NULL;
1767 nvlist_t *pools = NULL;
1768 boolean_t do_all = B_FALSE;
1769 boolean_t do_destroyed = B_FALSE;
1770 char *mntopts = NULL;
1773 uint64_t searchguid = 0;
1774 char *searchname = NULL;
1776 nvlist_t *found_config;
1777 nvlist_t *policy = NULL;
1778 nvlist_t *props = NULL;
1780 int flags = ZFS_IMPORT_NORMAL;
1781 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1782 boolean_t dryrun = B_FALSE;
1783 boolean_t do_rewind = B_FALSE;
1784 boolean_t xtreme_rewind = B_FALSE;
1785 uint64_t pool_state, txg = -1ULL;
1786 char *cachefile = NULL;
1787 importargs_t idata = { 0 };
1791 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1800 if (searchdirs == NULL) {
1801 searchdirs = safe_malloc(sizeof (char *));
1803 char **tmp = safe_malloc((nsearch + 1) *
1805 bcopy(searchdirs, tmp, nsearch *
1810 searchdirs[nsearch++] = optarg;
1813 do_destroyed = B_TRUE;
1816 flags |= ZFS_IMPORT_ANY_HOST;
1822 flags |= ZFS_IMPORT_MISSING_LOG;
1828 flags |= ZFS_IMPORT_ONLY;
1831 if ((propval = strchr(optarg, '=')) != NULL) {
1834 if (add_prop_list(optarg, propval,
1842 if (add_prop_list(zpool_prop_to_name(
1843 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1845 if (nvlist_lookup_string(props,
1846 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1849 if (add_prop_list(zpool_prop_to_name(
1850 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1855 txg = strtoull(optarg, &endptr, 10);
1856 if (errno != 0 || *endptr != '\0') {
1857 (void) fprintf(stderr,
1858 gettext("invalid txg value\n"));
1861 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1864 flags |= ZFS_IMPORT_VERBATIM;
1867 xtreme_rewind = B_TRUE;
1870 (void) fprintf(stderr, gettext("missing argument for "
1871 "'%c' option\n"), optopt);
1875 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1884 if (cachefile && nsearch != 0) {
1885 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1889 if ((dryrun || xtreme_rewind) && !do_rewind) {
1890 (void) fprintf(stderr,
1891 gettext("-n or -X only meaningful with -F\n"));
1895 rewind_policy = ZPOOL_TRY_REWIND;
1897 rewind_policy = ZPOOL_DO_REWIND;
1899 rewind_policy |= ZPOOL_EXTREME_REWIND;
1901 /* In the future, we can capture further policy and include it here */
1902 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1903 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1904 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1907 /* check argument count */
1910 (void) fprintf(stderr, gettext("too many arguments\n"));
1915 (void) fprintf(stderr, gettext("too many arguments\n"));
1920 * Check for the SYS_CONFIG privilege. We do this explicitly
1921 * here because otherwise any attempt to discover pools will
1924 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1925 (void) fprintf(stderr, gettext("cannot "
1926 "discover pools: permission denied\n"));
1927 if (searchdirs != NULL)
1930 nvlist_free(policy);
1936 * Depending on the arguments given, we do one of the following:
1938 * <none> Iterate through all pools and display information about
1941 * -a Iterate through all pools and try to import each one.
1943 * <id> Find the pool that corresponds to the given GUID/pool
1944 * name and import that one.
1946 * -D Above options applies only to destroyed pools.
1952 searchguid = strtoull(argv[0], &endptr, 10);
1953 if (errno != 0 || *endptr != '\0')
1954 searchname = argv[0];
1955 found_config = NULL;
1958 * User specified a name or guid. Ensure it's unique.
1960 idata.unique = B_TRUE;
1964 * Check the environment for the preferred search path.
1966 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
1969 envdup = strdup(env);
1971 dir = strtok(envdup, ":");
1972 while (dir != NULL) {
1973 if (searchdirs == NULL) {
1974 searchdirs = safe_malloc(sizeof (char *));
1976 char **tmp = safe_malloc((nsearch + 1) *
1978 bcopy(searchdirs, tmp, nsearch *
1983 searchdirs[nsearch++] = dir;
1984 dir = strtok(NULL, ":");
1988 idata.path = searchdirs;
1989 idata.paths = nsearch;
1990 idata.poolname = searchname;
1991 idata.guid = searchguid;
1992 idata.cachefile = cachefile;
1994 pools = zpool_search_import(g_zfs, &idata);
1996 if (pools != NULL && idata.exists &&
1997 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1998 (void) fprintf(stderr, gettext("cannot import '%s': "
1999 "a pool with that name already exists\n"),
2001 (void) fprintf(stderr, gettext("use the form '%s "
2002 "<pool | id> <newpool>' to give it a new name\n"),
2005 } else if (pools == NULL && idata.exists) {
2006 (void) fprintf(stderr, gettext("cannot import '%s': "
2007 "a pool with that name is already created/imported,\n"),
2009 (void) fprintf(stderr, gettext("and no additional pools "
2010 "with that name were found\n"));
2012 } else if (pools == NULL) {
2014 (void) fprintf(stderr, gettext("cannot import '%s': "
2015 "no such pool available\n"), argv[0]);
2021 if (searchdirs != NULL)
2025 nvlist_free(policy);
2030 * At this point we have a list of import candidate configs. Even if
2031 * we were searching by pool name or guid, we still need to
2032 * post-process the list to deal with pool state and possible
2038 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2040 verify(nvpair_value_nvlist(elem, &config) == 0);
2042 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2044 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2046 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2049 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2056 (void) printf("\n");
2059 err |= do_import(config, NULL, mntopts,
2062 show_import(config);
2064 } else if (searchname != NULL) {
2068 * We are searching for a pool based on name.
2070 verify(nvlist_lookup_string(config,
2071 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2073 if (strcmp(name, searchname) == 0) {
2074 if (found_config != NULL) {
2075 (void) fprintf(stderr, gettext(
2076 "cannot import '%s': more than "
2077 "one matching pool\n"), searchname);
2078 (void) fprintf(stderr, gettext(
2079 "import by numeric ID instead\n"));
2082 found_config = config;
2088 * Search for a pool by guid.
2090 verify(nvlist_lookup_uint64(config,
2091 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2093 if (guid == searchguid)
2094 found_config = config;
2099 * If we were searching for a specific pool, verify that we found a
2100 * pool, and then do the import.
2102 if (argc != 0 && err == 0) {
2103 if (found_config == NULL) {
2104 (void) fprintf(stderr, gettext("cannot import '%s': "
2105 "no such pool available\n"), argv[0]);
2108 err |= do_import(found_config, argc == 1 ? NULL :
2109 argv[1], mntopts, props, flags);
2114 * If we were just looking for pools, report an error if none were
2117 if (argc == 0 && first)
2118 (void) fprintf(stderr,
2119 gettext("no pools available to import\n"));
2124 nvlist_free(policy);
2125 if (searchdirs != NULL)
2130 return (err ? 1 : 0);
2133 typedef struct iostat_cbdata {
2134 boolean_t cb_verbose;
2137 zpool_list_t *cb_list;
2141 print_iostat_separator(iostat_cbdata_t *cb)
2145 for (i = 0; i < cb->cb_namewidth; i++)
2147 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2151 print_iostat_header(iostat_cbdata_t *cb)
2153 (void) printf("%*s capacity operations bandwidth\n",
2154 cb->cb_namewidth, "");
2155 (void) printf("%-*s alloc free read write read write\n",
2156 cb->cb_namewidth, "pool");
2157 print_iostat_separator(cb);
2161 * Display a single statistic.
2164 print_one_stat(uint64_t value)
2168 zfs_nicenum(value, buf, sizeof (buf));
2169 (void) printf(" %5s", buf);
2173 * Print out all the statistics for the given vdev. This can either be the
2174 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2175 * is a verbose output, and we don't want to display the toplevel pool stats.
2178 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2179 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2181 nvlist_t **oldchild, **newchild;
2183 vdev_stat_t *oldvs, *newvs;
2184 vdev_stat_t zerovs = { 0 };
2189 if (oldnv != NULL) {
2190 verify(nvlist_lookup_uint64_array(oldnv,
2191 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2196 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2197 (uint64_t **)&newvs, &c) == 0);
2199 if (strlen(name) + depth > cb->cb_namewidth)
2200 (void) printf("%*s%s", depth, "", name);
2202 (void) printf("%*s%s%*s", depth, "", name,
2203 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2205 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2210 scale = (double)NANOSEC / tdelta;
2212 /* only toplevel vdevs have capacity stats */
2213 if (newvs->vs_space == 0) {
2214 (void) printf(" - -");
2216 print_one_stat(newvs->vs_alloc);
2217 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2220 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2221 oldvs->vs_ops[ZIO_TYPE_READ])));
2223 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2224 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2226 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2227 oldvs->vs_bytes[ZIO_TYPE_READ])));
2229 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2230 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2232 (void) printf("\n");
2234 if (!cb->cb_verbose)
2237 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2238 &newchild, &children) != 0)
2241 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2242 &oldchild, &c) != 0)
2245 for (c = 0; c < children; c++) {
2246 uint64_t ishole = B_FALSE, islog = B_FALSE;
2248 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2251 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2254 if (ishole || islog)
2257 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2258 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2259 newchild[c], cb, depth + 2);
2264 * Log device section
2267 if (num_logs(newnv) > 0) {
2268 (void) printf("%-*s - - - - - "
2269 "-\n", cb->cb_namewidth, "logs");
2271 for (c = 0; c < children; c++) {
2272 uint64_t islog = B_FALSE;
2273 (void) nvlist_lookup_uint64(newchild[c],
2274 ZPOOL_CONFIG_IS_LOG, &islog);
2277 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2279 print_vdev_stats(zhp, vname, oldnv ?
2280 oldchild[c] : NULL, newchild[c],
2289 * Include level 2 ARC devices in iostat output
2291 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2292 &newchild, &children) != 0)
2295 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2296 &oldchild, &c) != 0)
2300 (void) printf("%-*s - - - - - "
2301 "-\n", cb->cb_namewidth, "cache");
2302 for (c = 0; c < children; c++) {
2303 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2305 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2306 newchild[c], cb, depth + 2);
2313 refresh_iostat(zpool_handle_t *zhp, void *data)
2315 iostat_cbdata_t *cb = data;
2319 * If the pool has disappeared, remove it from the list and continue.
2321 if (zpool_refresh_stats(zhp, &missing) != 0)
2325 pool_list_remove(cb->cb_list, zhp);
2331 * Callback to print out the iostats for the given pool.
2334 print_iostat(zpool_handle_t *zhp, void *data)
2336 iostat_cbdata_t *cb = data;
2337 nvlist_t *oldconfig, *newconfig;
2338 nvlist_t *oldnvroot, *newnvroot;
2340 newconfig = zpool_get_config(zhp, &oldconfig);
2342 if (cb->cb_iteration == 1)
2345 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2348 if (oldconfig == NULL)
2351 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2355 * Print out the statistics for the pool.
2357 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2360 print_iostat_separator(cb);
2372 if (isatty(STDOUT_FILENO)) {
2373 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2375 columns = ws.ws_col;
2384 get_namewidth(zpool_handle_t *zhp, void *data)
2386 iostat_cbdata_t *cb = data;
2387 nvlist_t *config, *nvroot;
2390 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2391 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2393 if (!cb->cb_verbose)
2394 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2396 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2401 * The width must be at least 10, but may be as large as the
2402 * column width - 42 so that we can still fit in one line.
2404 columns = get_columns();
2406 if (cb->cb_namewidth < 10)
2407 cb->cb_namewidth = 10;
2408 if (cb->cb_namewidth > columns - 42)
2409 cb->cb_namewidth = columns - 42;
2415 * Parse the input string, get the 'interval' and 'count' value if there is one.
2418 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2421 unsigned long interval = 0, count = 0;
2425 * Determine if the last argument is an integer or a pool name
2427 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2431 interval = strtoul(argv[argc - 1], &end, 10);
2433 if (*end == '\0' && errno == 0) {
2434 if (interval == 0) {
2435 (void) fprintf(stderr, gettext("interval "
2436 "cannot be zero\n"));
2440 * Ignore the last parameter
2445 * If this is not a valid number, just plow on. The
2446 * user will get a more informative error message later
2454 * If the last argument is also an integer, then we have both a count
2457 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2462 interval = strtoul(argv[argc - 1], &end, 10);
2464 if (*end == '\0' && errno == 0) {
2465 if (interval == 0) {
2466 (void) fprintf(stderr, gettext("interval "
2467 "cannot be zero\n"));
2472 * Ignore the last parameter
2486 get_timestamp_arg(char c)
2489 timestamp_fmt = UDATE;
2491 timestamp_fmt = DDATE;
2497 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2499 * -v Display statistics for individual vdevs
2500 * -T Display a timestamp in date(1) or Unix format
2502 * This command can be tricky because we want to be able to deal with pool
2503 * creation/destruction as well as vdev configuration changes. The bulk of this
2504 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2505 * on pool_list_update() to detect the addition of new pools. Configuration
2506 * changes are all handled within libzfs.
2509 zpool_do_iostat(int argc, char **argv)
2514 unsigned long interval = 0, count = 0;
2516 boolean_t verbose = B_FALSE;
2520 while ((c = getopt(argc, argv, "T:v")) != -1) {
2523 get_timestamp_arg(*optarg);
2529 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2538 get_interval_count(&argc, argv, &interval, &count);
2541 * Construct the list of all interesting pools.
2544 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2547 if (pool_list_count(list) == 0 && argc != 0) {
2548 pool_list_free(list);
2552 if (pool_list_count(list) == 0 && interval == 0) {
2553 pool_list_free(list);
2554 (void) fprintf(stderr, gettext("no pools available\n"));
2559 * Enter the main iostat loop.
2562 cb.cb_verbose = verbose;
2563 cb.cb_iteration = 0;
2564 cb.cb_namewidth = 0;
2567 pool_list_update(list);
2569 if ((npools = pool_list_count(list)) == 0)
2570 (void) fprintf(stderr, gettext("no pools available\n"));
2573 * Refresh all statistics. This is done as an
2574 * explicit step before calculating the maximum name
2575 * width, so that any * configuration changes are
2576 * properly accounted for.
2578 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2582 * Iterate over all pools to determine the maximum width
2583 * for the pool / device name column across all pools.
2585 cb.cb_namewidth = 0;
2586 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2589 if (timestamp_fmt != NODATE)
2590 print_timestamp(timestamp_fmt);
2593 * If it's the first time, or verbose mode, print the
2596 if (++cb.cb_iteration == 1 || verbose)
2597 print_iostat_header(&cb);
2599 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2602 * If there's more than one pool, and we're not in
2603 * verbose mode (which prints a separator for us),
2604 * then print a separator.
2606 if (npools > 1 && !verbose)
2607 print_iostat_separator(&cb);
2610 (void) printf("\n");
2614 * Flush the output so that redirection to a file isn't buffered
2617 (void) fflush(stdout);
2622 if (count != 0 && --count == 0)
2625 (void) sleep(interval);
2628 pool_list_free(list);
2633 typedef struct list_cbdata {
2634 boolean_t cb_verbose;
2636 boolean_t cb_scripted;
2637 zprop_list_t *cb_proplist;
2641 * Given a list of columns to display, output appropriate headers for each one.
2644 print_header(list_cbdata_t *cb)
2646 zprop_list_t *pl = cb->cb_proplist;
2647 char headerbuf[ZPOOL_MAXPROPLEN];
2649 boolean_t first = B_TRUE;
2650 boolean_t right_justify;
2653 for (; pl != NULL; pl = pl->pl_next) {
2654 width = pl->pl_width;
2655 if (first && cb->cb_verbose) {
2657 * Reset the width to accommodate the verbose listing
2660 width = cb->cb_namewidth;
2668 right_justify = B_FALSE;
2669 if (pl->pl_prop != ZPROP_INVAL) {
2670 header = zpool_prop_column_name(pl->pl_prop);
2671 right_justify = zpool_prop_align_right(pl->pl_prop);
2675 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2676 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2677 headerbuf[i] = '\0';
2681 if (pl->pl_next == NULL && !right_justify)
2682 (void) printf("%s", header);
2683 else if (right_justify)
2684 (void) printf("%*s", (int)width, header);
2686 (void) printf("%-*s", (int)width, header);
2689 (void) printf("\n");
2693 * Given a pool and a list of properties, print out all the properties according
2694 * to the described layout.
2697 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2699 zprop_list_t *pl = cb->cb_proplist;
2700 boolean_t first = B_TRUE;
2701 char property[ZPOOL_MAXPROPLEN];
2703 boolean_t right_justify;
2706 for (; pl != NULL; pl = pl->pl_next) {
2708 width = pl->pl_width;
2709 if (first && cb->cb_verbose) {
2711 * Reset the width to accommodate the verbose listing
2714 width = cb->cb_namewidth;
2718 if (cb->cb_scripted)
2719 (void) printf("\t");
2726 right_justify = B_FALSE;
2727 if (pl->pl_prop != ZPROP_INVAL) {
2728 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2729 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2731 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2732 sizeof (property), NULL) != 0)
2737 right_justify = zpool_prop_align_right(pl->pl_prop);
2738 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2739 zpool_prop_unsupported(pl->pl_user_prop)) &&
2740 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2741 sizeof (property)) == 0) {
2749 * If this is being called in scripted mode, or if this is the
2750 * last column and it is left-justified, don't include a width
2753 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2754 (void) printf("%s", propstr);
2755 else if (right_justify)
2756 (void) printf("%*s", (int)width, propstr);
2758 (void) printf("%-*s", (int)width, propstr);
2761 (void) printf("\n");
2765 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2769 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2771 zfs_nicenum(value, propval, sizeof (propval));
2773 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2774 (void) strlcpy(propval, "-", sizeof (propval));
2777 (void) printf("\t%s", propval);
2779 (void) printf(" %*s", (int)width, propval);
2783 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2784 list_cbdata_t *cb, int depth)
2790 boolean_t scripted = cb->cb_scripted;
2792 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2793 (uint64_t **)&vs, &c) == 0);
2797 (void) printf("\t%s", name);
2798 else if (strlen(name) + depth > cb->cb_namewidth)
2799 (void) printf("%*s%s", depth, "", name);
2801 (void) printf("%*s%s%*s", depth, "", name,
2802 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2804 /* only toplevel vdevs have capacity stats */
2805 if (vs->vs_space == 0) {
2807 (void) printf("\t-\t-\t-");
2809 (void) printf(" - - -");
2811 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2813 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2815 print_one_column(ZPOOL_PROP_FREE,
2816 vs->vs_space - vs->vs_alloc, scripted);
2818 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2820 (void) printf("\n");
2823 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2824 &child, &children) != 0)
2827 for (c = 0; c < children; c++) {
2828 uint64_t ishole = B_FALSE;
2830 if (nvlist_lookup_uint64(child[c],
2831 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2834 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2835 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2840 * Include level 2 ARC devices in iostat output
2842 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2843 &child, &children) != 0)
2847 (void) printf("%-*s - - - - - "
2848 "-\n", cb->cb_namewidth, "cache");
2849 for (c = 0; c < children; c++) {
2850 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2852 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2860 * Generic callback function to list a pool.
2863 list_callback(zpool_handle_t *zhp, void *data)
2865 list_cbdata_t *cbp = data;
2869 config = zpool_get_config(zhp, NULL);
2871 print_pool(zhp, cbp);
2872 if (!cbp->cb_verbose)
2875 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2877 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2883 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2885 * -H Scripted mode. Don't display headers, and separate properties
2887 * -o List of properties to display. Defaults to
2888 * "name,size,allocated,free,capacity,health,altroot"
2889 * -T Display a timestamp in date(1) or Unix format
2891 * List all pools in the system, whether or not they're healthy. Output space
2892 * statistics for each one, as well as health status summary.
2895 zpool_do_list(int argc, char **argv)
2899 list_cbdata_t cb = { 0 };
2900 static char default_props[] =
2901 "name,size,allocated,free,capacity,dedupratio,"
2903 char *props = default_props;
2904 unsigned long interval = 0, count = 0;
2906 boolean_t first = B_TRUE;
2909 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2912 cb.cb_scripted = B_TRUE;
2918 get_timestamp_arg(*optarg);
2921 cb.cb_verbose = B_TRUE;
2924 (void) fprintf(stderr, gettext("missing argument for "
2925 "'%c' option\n"), optopt);
2929 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2938 get_interval_count(&argc, argv, &interval, &count);
2940 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2943 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
2946 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2947 (void) printf(gettext("no pools available\n"));
2948 zprop_free_list(cb.cb_proplist);
2953 pool_list_update(list);
2955 if (pool_list_count(list) == 0)
2958 if (timestamp_fmt != NODATE)
2959 print_timestamp(timestamp_fmt);
2961 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
2965 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
2970 if (count != 0 && --count == 0)
2973 (void) sleep(interval);
2976 zprop_free_list(cb.cb_proplist);
2981 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2983 boolean_t force = B_FALSE;
2986 char *poolname, *old_disk, *new_disk;
2987 zpool_handle_t *zhp;
2988 nvlist_t *props = NULL;
2993 while ((c = getopt(argc, argv, "fo:")) != -1) {
2999 if ((propval = strchr(optarg, '=')) == NULL) {
3000 (void) fprintf(stderr, gettext("missing "
3001 "'=' for -o option\n"));
3007 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3008 (add_prop_list(optarg, propval, &props, B_TRUE)))
3012 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3021 /* get pool name and check number of arguments */
3023 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3030 (void) fprintf(stderr,
3031 gettext("missing <device> specification\n"));
3039 (void) fprintf(stderr,
3040 gettext("missing <new_device> specification\n"));
3043 new_disk = old_disk;
3053 (void) fprintf(stderr, gettext("too many arguments\n"));
3057 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3060 if (zpool_get_config(zhp, NULL) == NULL) {
3061 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3067 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3069 if (nvroot == NULL) {
3074 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3076 nvlist_free(nvroot);
3083 * zpool replace [-f] <pool> <device> <new_device>
3085 * -f Force attach, even if <new_device> appears to be in use.
3087 * Replace <device> with <new_device>.
3091 zpool_do_replace(int argc, char **argv)
3093 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3097 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3099 * -f Force attach, even if <new_device> appears to be in use.
3100 * -o Set property=value.
3102 * Attach <new_device> to the mirror containing <device>. If <device> is not
3103 * part of a mirror, then <device> will be transformed into a mirror of
3104 * <device> and <new_device>. In either case, <new_device> will begin life
3105 * with a DTL of [0, now], and will immediately begin to resilver itself.
3108 zpool_do_attach(int argc, char **argv)
3110 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3114 * zpool detach [-f] <pool> <device>
3116 * -f Force detach of <device>, even if DTLs argue against it
3117 * (not supported yet)
3119 * Detach a device from a mirror. The operation will be refused if <device>
3120 * is the last device in the mirror, or if the DTLs indicate that this device
3121 * has the only valid copy of some data.
3125 zpool_do_detach(int argc, char **argv)
3128 char *poolname, *path;
3129 zpool_handle_t *zhp;
3133 while ((c = getopt(argc, argv, "f")) != -1) {
3137 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3146 /* get pool name and check number of arguments */
3148 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3153 (void) fprintf(stderr,
3154 gettext("missing <device> specification\n"));
3161 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3164 ret = zpool_vdev_detach(zhp, path);
3172 * zpool split [-n] [-o prop=val] ...
3174 * [-R altroot] <pool> <newpool> [<device> ...]
3176 * -n Do not split the pool, but display the resulting layout if
3177 * it were to be split.
3178 * -o Set property=value, or set mount options.
3179 * -R Mount the split-off pool under an alternate root.
3181 * Splits the named pool and gives it the new pool name. Devices to be split
3182 * off may be listed, provided that no more than one device is specified
3183 * per top-level vdev mirror. The newly split pool is left in an exported
3184 * state unless -R is specified.
3186 * Restrictions: the top-level of the pool pool must only be made up of
3187 * mirrors; all devices in the pool must be healthy; no device may be
3188 * undergoing a resilvering operation.
3191 zpool_do_split(int argc, char **argv)
3193 char *srcpool, *newpool, *propval;
3194 char *mntopts = NULL;
3197 zpool_handle_t *zhp;
3198 nvlist_t *config, *props = NULL;
3200 flags.dryrun = B_FALSE;
3201 flags.import = B_FALSE;
3204 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3207 flags.import = B_TRUE;
3209 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3210 &props, B_TRUE) != 0) {
3217 flags.dryrun = B_TRUE;
3220 if ((propval = strchr(optarg, '=')) != NULL) {
3223 if (add_prop_list(optarg, propval,
3224 &props, B_TRUE) != 0) {
3234 (void) fprintf(stderr, gettext("missing argument for "
3235 "'%c' option\n"), optopt);
3239 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3246 if (!flags.import && mntopts != NULL) {
3247 (void) fprintf(stderr, gettext("setting mntopts is only "
3248 "valid when importing the pool\n"));
3256 (void) fprintf(stderr, gettext("Missing pool name\n"));
3260 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3270 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3273 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3274 if (config == NULL) {
3278 (void) printf(gettext("would create '%s' with the "
3279 "following layout:\n\n"), newpool);
3280 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3282 nvlist_free(config);
3287 if (ret != 0 || flags.dryrun || !flags.import)
3291 * The split was successful. Now we need to open the new
3292 * pool and import it.
3294 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3296 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3297 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3299 (void) fprintf(stderr, gettext("Split was successful, but "
3300 "the datasets could not all be mounted\n"));
3301 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3302 "different altroot\n"), "zpool import");
3312 * zpool online <pool> <device> ...
3315 zpool_do_online(int argc, char **argv)
3319 zpool_handle_t *zhp;
3321 vdev_state_t newstate;
3325 while ((c = getopt(argc, argv, "et")) != -1) {
3328 flags |= ZFS_ONLINE_EXPAND;
3332 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3341 /* get pool name and check number of arguments */
3343 (void) fprintf(stderr, gettext("missing pool name\n"));
3347 (void) fprintf(stderr, gettext("missing device name\n"));
3353 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3356 for (i = 1; i < argc; i++) {
3357 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3358 if (newstate != VDEV_STATE_HEALTHY) {
3359 (void) printf(gettext("warning: device '%s' "
3360 "onlined, but remains in faulted state\n"),
3362 if (newstate == VDEV_STATE_FAULTED)
3363 (void) printf(gettext("use 'zpool "
3364 "clear' to restore a faulted "
3367 (void) printf(gettext("use 'zpool "
3368 "replace' to replace devices "
3369 "that are no longer present\n"));
3382 * zpool offline [-ft] <pool> <device> ...
3384 * -f Force the device into the offline state, even if doing
3385 * so would appear to compromise pool availability.
3386 * (not supported yet)
3388 * -t Only take the device off-line temporarily. The offline
3389 * state will not be persistent across reboots.
3393 zpool_do_offline(int argc, char **argv)
3397 zpool_handle_t *zhp;
3399 boolean_t istmp = B_FALSE;
3402 while ((c = getopt(argc, argv, "ft")) != -1) {
3409 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3418 /* get pool name and check number of arguments */
3420 (void) fprintf(stderr, gettext("missing pool name\n"));
3424 (void) fprintf(stderr, gettext("missing device name\n"));
3430 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3433 for (i = 1; i < argc; i++) {
3434 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3444 * zpool clear <pool> [device]
3446 * Clear all errors associated with a pool or a particular device.
3449 zpool_do_clear(int argc, char **argv)
3453 boolean_t dryrun = B_FALSE;
3454 boolean_t do_rewind = B_FALSE;
3455 boolean_t xtreme_rewind = B_FALSE;
3456 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3457 nvlist_t *policy = NULL;
3458 zpool_handle_t *zhp;
3459 char *pool, *device;
3462 while ((c = getopt(argc, argv, "FnX")) != -1) {
3471 xtreme_rewind = B_TRUE;
3474 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3484 (void) fprintf(stderr, gettext("missing pool name\n"));
3489 (void) fprintf(stderr, gettext("too many arguments\n"));
3493 if ((dryrun || xtreme_rewind) && !do_rewind) {
3494 (void) fprintf(stderr,
3495 gettext("-n or -X only meaningful with -F\n"));
3499 rewind_policy = ZPOOL_TRY_REWIND;
3501 rewind_policy = ZPOOL_DO_REWIND;
3503 rewind_policy |= ZPOOL_EXTREME_REWIND;
3505 /* In future, further rewind policy choices can be passed along here */
3506 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3507 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3511 device = argc == 2 ? argv[1] : NULL;
3513 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3514 nvlist_free(policy);
3518 if (zpool_clear(zhp, device, policy) != 0)
3523 nvlist_free(policy);
3529 * zpool reguid <pool>
3532 zpool_do_reguid(int argc, char **argv)
3536 zpool_handle_t *zhp;
3540 while ((c = getopt(argc, argv, "")) != -1) {
3543 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3552 /* get pool name and check number of arguments */
3554 (void) fprintf(stderr, gettext("missing pool name\n"));
3559 (void) fprintf(stderr, gettext("too many arguments\n"));
3564 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3567 ret = zpool_reguid(zhp);
3575 * zpool reopen <pool>
3577 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3579 * NOTE: This command is currently undocumented. If the command is ever
3580 * exposed then the appropriate usage() messages will need to be made.
3583 zpool_do_reopen(int argc, char **argv)
3586 zpool_handle_t *zhp;
3596 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3599 ret = zpool_reopen(zhp);
3604 typedef struct scrub_cbdata {
3611 scrub_callback(zpool_handle_t *zhp, void *data)
3613 scrub_cbdata_t *cb = data;
3617 * Ignore faulted pools.
3619 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3620 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3621 "currently unavailable\n"), zpool_get_name(zhp));
3625 err = zpool_scan(zhp, cb->cb_type);
3631 * zpool scrub [-s] <pool> ...
3633 * -s Stop. Stops any in-progress scrub.
3636 zpool_do_scrub(int argc, char **argv)
3641 cb.cb_type = POOL_SCAN_SCRUB;
3644 while ((c = getopt(argc, argv, "s")) != -1) {
3647 cb.cb_type = POOL_SCAN_NONE;
3650 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3662 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3666 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3669 typedef struct status_cbdata {
3671 boolean_t cb_allpools;
3672 boolean_t cb_verbose;
3673 boolean_t cb_explain;
3675 boolean_t cb_dedup_stats;
3679 * Print out detailed scrub status.
3682 print_scan_status(pool_scan_stat_t *ps)
3685 uint64_t elapsed, mins_left, hours_left;
3686 uint64_t pass_exam, examined, total;
3688 double fraction_done;
3689 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3691 (void) printf(gettext(" scan: "));
3693 /* If there's never been a scan, there's not much to say. */
3694 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3695 ps->pss_func >= POOL_SCAN_FUNCS) {
3696 (void) printf(gettext("none requested\n"));
3700 start = ps->pss_start_time;
3701 end = ps->pss_end_time;
3702 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3704 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3705 ps->pss_func == POOL_SCAN_RESILVER);
3707 * Scan is finished or canceled.
3709 if (ps->pss_state == DSS_FINISHED) {
3710 uint64_t minutes_taken = (end - start) / 60;
3713 if (ps->pss_func == POOL_SCAN_SCRUB) {
3714 fmt = gettext("scrub repaired %s in %lluh%um with "
3715 "%llu errors on %s");
3716 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3717 fmt = gettext("resilvered %s in %lluh%um with "
3718 "%llu errors on %s");
3721 (void) printf(fmt, processed_buf,
3722 (u_longlong_t)(minutes_taken / 60),
3723 (uint_t)(minutes_taken % 60),
3724 (u_longlong_t)ps->pss_errors,
3725 ctime((time_t *)&end));
3727 } else if (ps->pss_state == DSS_CANCELED) {
3728 if (ps->pss_func == POOL_SCAN_SCRUB) {
3729 (void) printf(gettext("scrub canceled on %s"),
3731 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3732 (void) printf(gettext("resilver canceled on %s"),
3738 assert(ps->pss_state == DSS_SCANNING);
3741 * Scan is in progress.
3743 if (ps->pss_func == POOL_SCAN_SCRUB) {
3744 (void) printf(gettext("scrub in progress since %s"),
3746 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3747 (void) printf(gettext("resilver in progress since %s"),
3751 examined = ps->pss_examined ? ps->pss_examined : 1;
3752 total = ps->pss_to_examine;
3753 fraction_done = (double)examined / total;
3755 /* elapsed time for this pass */
3756 elapsed = time(NULL) - ps->pss_pass_start;
3757 elapsed = elapsed ? elapsed : 1;
3758 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3759 rate = pass_exam / elapsed;
3760 rate = rate ? rate : 1;
3761 mins_left = ((total - examined) / rate) / 60;
3762 hours_left = mins_left / 60;
3764 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3765 zfs_nicenum(total, total_buf, sizeof (total_buf));
3766 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3769 * do not print estimated time if hours_left is more than 30 days
3771 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3772 examined_buf, total_buf, rate_buf);
3773 if (hours_left < (30 * 24)) {
3774 (void) printf(gettext(", %lluh%um to go\n"),
3775 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3777 (void) printf(gettext(
3778 ", (scan is slow, no estimated time)\n"));
3781 if (ps->pss_func == POOL_SCAN_RESILVER) {
3782 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3783 processed_buf, 100 * fraction_done);
3784 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3785 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3786 processed_buf, 100 * fraction_done);
3791 print_error_log(zpool_handle_t *zhp)
3793 nvlist_t *nverrlist = NULL;
3796 size_t len = MAXPATHLEN * 2;
3798 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3799 (void) printf("errors: List of errors unavailable "
3800 "(insufficient privileges)\n");
3804 (void) printf("errors: Permanent errors have been "
3805 "detected in the following files:\n\n");
3807 pathname = safe_malloc(len);
3809 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3811 uint64_t dsobj, obj;
3813 verify(nvpair_value_nvlist(elem, &nv) == 0);
3814 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3816 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3818 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3819 (void) printf("%7s %s\n", "", pathname);
3822 nvlist_free(nverrlist);
3826 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3835 (void) printf(gettext("\tspares\n"));
3837 for (i = 0; i < nspares; i++) {
3838 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3839 print_status_config(zhp, name, spares[i],
3840 namewidth, 2, B_TRUE);
3846 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3855 (void) printf(gettext("\tcache\n"));
3857 for (i = 0; i < nl2cache; i++) {
3858 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3859 print_status_config(zhp, name, l2cache[i],
3860 namewidth, 2, B_FALSE);
3866 print_dedup_stats(nvlist_t *config)
3868 ddt_histogram_t *ddh;
3874 * If the pool was faulted then we may not have been able to
3875 * obtain the config. Otherwise, if we have anything in the dedup
3876 * table continue processing the stats.
3878 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3879 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3882 (void) printf("\n");
3883 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3884 (u_longlong_t)ddo->ddo_count,
3885 (u_longlong_t)ddo->ddo_dspace,
3886 (u_longlong_t)ddo->ddo_mspace);
3888 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3889 (uint64_t **)&dds, &c) == 0);
3890 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3891 (uint64_t **)&ddh, &c) == 0);
3892 zpool_dump_ddt(dds, ddh);
3896 * Display a summary of pool status. Displays a summary such as:
3900 * reason: One or more devices ...
3901 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
3907 * When given the '-v' option, we print out the complete config. If the '-e'
3908 * option is specified, then we print out error rate information as well.
3911 status_callback(zpool_handle_t *zhp, void *data)
3913 status_cbdata_t *cbp = data;
3914 nvlist_t *config, *nvroot;
3921 config = zpool_get_config(zhp, NULL);
3922 reason = zpool_get_status(zhp, &msgid);
3927 * If we were given 'zpool status -x', only report those pools with
3930 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3931 if (!cbp->cb_allpools) {
3932 (void) printf(gettext("pool '%s' is healthy\n"),
3933 zpool_get_name(zhp));
3935 cbp->cb_first = B_FALSE;
3941 cbp->cb_first = B_FALSE;
3943 (void) printf("\n");
3945 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3947 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3948 (uint64_t **)&vs, &c) == 0);
3949 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3951 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3952 (void) printf(gettext(" state: %s\n"), health);
3955 case ZPOOL_STATUS_MISSING_DEV_R:
3956 (void) printf(gettext("status: One or more devices could not "
3957 "be opened. Sufficient replicas exist for\n\tthe pool to "
3958 "continue functioning in a degraded state.\n"));
3959 (void) printf(gettext("action: Attach the missing device and "
3960 "online it using 'zpool online'.\n"));
3963 case ZPOOL_STATUS_MISSING_DEV_NR:
3964 (void) printf(gettext("status: One or more devices could not "
3965 "be opened. There are insufficient\n\treplicas for the "
3966 "pool to continue functioning.\n"));
3967 (void) printf(gettext("action: Attach the missing device and "
3968 "online it using 'zpool online'.\n"));
3971 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3972 (void) printf(gettext("status: One or more devices could not "
3973 "be used because the label is missing or\n\tinvalid. "
3974 "Sufficient replicas exist for the pool to continue\n\t"
3975 "functioning in a degraded state.\n"));
3976 (void) printf(gettext("action: Replace the device using "
3977 "'zpool replace'.\n"));
3980 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3981 (void) printf(gettext("status: One or more devices could not "
3982 "be used because the label is missing \n\tor invalid. "
3983 "There are insufficient replicas for the pool to "
3984 "continue\n\tfunctioning.\n"));
3985 zpool_explain_recover(zpool_get_handle(zhp),
3986 zpool_get_name(zhp), reason, config);
3989 case ZPOOL_STATUS_FAILING_DEV:
3990 (void) printf(gettext("status: One or more devices has "
3991 "experienced an unrecoverable error. An\n\tattempt was "
3992 "made to correct the error. Applications are "
3994 (void) printf(gettext("action: Determine if the device needs "
3995 "to be replaced, and clear the errors\n\tusing "
3996 "'zpool clear' or replace the device with 'zpool "
4000 case ZPOOL_STATUS_OFFLINE_DEV:
4001 (void) printf(gettext("status: One or more devices has "
4002 "been taken offline by the administrator.\n\tSufficient "
4003 "replicas exist for the pool to continue functioning in "
4004 "a\n\tdegraded state.\n"));
4005 (void) printf(gettext("action: Online the device using "
4006 "'zpool online' or replace the device with\n\t'zpool "
4010 case ZPOOL_STATUS_REMOVED_DEV:
4011 (void) printf(gettext("status: One or more devices has "
4012 "been removed by the administrator.\n\tSufficient "
4013 "replicas exist for the pool to continue functioning in "
4014 "a\n\tdegraded state.\n"));
4015 (void) printf(gettext("action: Online the device using "
4016 "'zpool online' or replace the device with\n\t'zpool "
4020 case ZPOOL_STATUS_RESILVERING:
4021 (void) printf(gettext("status: One or more devices is "
4022 "currently being resilvered. The pool will\n\tcontinue "
4023 "to function, possibly in a degraded state.\n"));
4024 (void) printf(gettext("action: Wait for the resilver to "
4028 case ZPOOL_STATUS_CORRUPT_DATA:
4029 (void) printf(gettext("status: One or more devices has "
4030 "experienced an error resulting in data\n\tcorruption. "
4031 "Applications may be affected.\n"));
4032 (void) printf(gettext("action: Restore the file in question "
4033 "if possible. Otherwise restore the\n\tentire pool from "
4037 case ZPOOL_STATUS_CORRUPT_POOL:
4038 (void) printf(gettext("status: The pool metadata is corrupted "
4039 "and the pool cannot be opened.\n"));
4040 zpool_explain_recover(zpool_get_handle(zhp),
4041 zpool_get_name(zhp), reason, config);
4044 case ZPOOL_STATUS_VERSION_OLDER:
4045 (void) printf(gettext("status: The pool is formatted using an "
4046 "older on-disk format. The pool can\n\tstill be used, but "
4047 "some features are unavailable.\n"));
4048 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4049 "upgrade'. Once this is done, the\n\tpool will no longer "
4050 "be accessible on older software versions.\n"));
4053 case ZPOOL_STATUS_VERSION_NEWER:
4054 (void) printf(gettext("status: The pool has been upgraded to a "
4055 "newer, incompatible on-disk version.\n\tThe pool cannot "
4056 "be accessed on this system.\n"));
4057 (void) printf(gettext("action: Access the pool from a system "
4058 "running more recent software, or\n\trestore the pool from "
4062 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4063 (void) printf(gettext("status: The pool cannot be accessed on "
4064 "this system because it uses the\n\tfollowing feature(s) "
4065 "not supported on this system:\n"));
4066 zpool_print_unsup_feat(config);
4067 (void) printf("\n");
4068 (void) printf(gettext("action: Access the pool from a system "
4069 "that supports the required feature(s),\n\tor restore the "
4070 "pool from backup.\n"));
4073 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4074 (void) printf(gettext("status: The pool can only be accessed "
4075 "in read-only mode on this system. It\n\tcannot be "
4076 "accessed in read-write mode because it uses the "
4077 "following\n\tfeature(s) not supported on this system:\n"));
4078 zpool_print_unsup_feat(config);
4079 (void) printf("\n");
4080 (void) printf(gettext("action: The pool cannot be accessed in "
4081 "read-write mode. Import the pool with\n"
4082 "\t\"-o readonly=on\", access the pool from a system that "
4083 "supports the\n\trequired feature(s), or restore the "
4084 "pool from backup.\n"));
4087 case ZPOOL_STATUS_FAULTED_DEV_R:
4088 (void) printf(gettext("status: One or more devices are "
4089 "faulted in response to persistent errors.\n\tSufficient "
4090 "replicas exist for the pool to continue functioning "
4091 "in a\n\tdegraded state.\n"));
4092 (void) printf(gettext("action: Replace the faulted device, "
4093 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4096 case ZPOOL_STATUS_FAULTED_DEV_NR:
4097 (void) printf(gettext("status: One or more devices are "
4098 "faulted in response to persistent errors. There are "
4099 "insufficient replicas for the pool to\n\tcontinue "
4101 (void) printf(gettext("action: Destroy and re-create the pool "
4102 "from a backup source. Manually marking the device\n"
4103 "\trepaired using 'zpool clear' may allow some data "
4104 "to be recovered.\n"));
4107 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4108 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4109 (void) printf(gettext("status: One or more devices are "
4110 "faulted in response to IO failures.\n"));
4111 (void) printf(gettext("action: Make sure the affected devices "
4112 "are connected, then run 'zpool clear'.\n"));
4115 case ZPOOL_STATUS_BAD_LOG:
4116 (void) printf(gettext("status: An intent log record "
4117 "could not be read.\n"
4118 "\tWaiting for adminstrator intervention to fix the "
4119 "faulted pool.\n"));
4120 (void) printf(gettext("action: Either restore the affected "
4121 "device(s) and run 'zpool online',\n"
4122 "\tor ignore the intent log records by running "
4123 "'zpool clear'.\n"));
4128 * The remaining errors can't actually be generated, yet.
4130 assert(reason == ZPOOL_STATUS_OK);
4134 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4137 if (config != NULL) {
4140 nvlist_t **spares, **l2cache;
4141 uint_t nspares, nl2cache;
4142 pool_scan_stat_t *ps = NULL;
4144 (void) nvlist_lookup_uint64_array(nvroot,
4145 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4146 print_scan_status(ps);
4148 namewidth = max_width(zhp, nvroot, 0, 0);
4152 (void) printf(gettext("config:\n\n"));
4153 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4154 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4155 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4156 namewidth, 0, B_FALSE);
4158 if (num_logs(nvroot) > 0)
4159 print_logs(zhp, nvroot, namewidth, B_TRUE);
4160 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4161 &l2cache, &nl2cache) == 0)
4162 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4164 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4165 &spares, &nspares) == 0)
4166 print_spares(zhp, spares, nspares, namewidth);
4168 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4170 nvlist_t *nverrlist = NULL;
4173 * If the approximate error count is small, get a
4174 * precise count by fetching the entire log and
4175 * uniquifying the results.
4177 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4178 zpool_get_errlog(zhp, &nverrlist) == 0) {
4183 while ((elem = nvlist_next_nvpair(nverrlist,
4188 nvlist_free(nverrlist);
4190 (void) printf("\n");
4193 (void) printf(gettext("errors: No known data "
4195 else if (!cbp->cb_verbose)
4196 (void) printf(gettext("errors: %llu data "
4197 "errors, use '-v' for a list\n"),
4198 (u_longlong_t)nerr);
4200 print_error_log(zhp);
4203 if (cbp->cb_dedup_stats)
4204 print_dedup_stats(config);
4206 (void) printf(gettext("config: The configuration cannot be "
4214 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4216 * -v Display complete error logs
4217 * -x Display only pools with potential problems
4218 * -D Display dedup status (undocumented)
4219 * -T Display a timestamp in date(1) or Unix format
4221 * Describes the health status of all pools or some subset.
4224 zpool_do_status(int argc, char **argv)
4228 unsigned long interval = 0, count = 0;
4229 status_cbdata_t cb = { 0 };
4232 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4235 cb.cb_verbose = B_TRUE;
4238 cb.cb_explain = B_TRUE;
4241 cb.cb_dedup_stats = B_TRUE;
4244 get_timestamp_arg(*optarg);
4247 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4256 get_interval_count(&argc, argv, &interval, &count);
4259 cb.cb_allpools = B_TRUE;
4261 cb.cb_first = B_TRUE;
4264 if (timestamp_fmt != NODATE)
4265 print_timestamp(timestamp_fmt);
4267 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4268 status_callback, &cb);
4270 if (argc == 0 && cb.cb_count == 0)
4271 (void) fprintf(stderr, gettext("no pools available\n"));
4272 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4273 (void) printf(gettext("all pools are healthy\n"));
4281 if (count != 0 && --count == 0)
4284 (void) sleep(interval);
4290 typedef struct upgrade_cbdata {
4295 uint64_t cb_version;
4300 upgrade_cb(zpool_handle_t *zhp, void *arg)
4302 upgrade_cbdata_t *cbp = arg;
4307 config = zpool_get_config(zhp, NULL);
4308 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4311 if (!cbp->cb_newer && SPA_VERSION_IS_SUPPORTED(version) &&
4312 version != SPA_VERSION) {
4314 if (cbp->cb_first) {
4315 (void) printf(gettext("The following pools are "
4316 "out of date, and can be upgraded. After "
4317 "being\nupgraded, these pools will no "
4318 "longer be accessible by older software "
4320 (void) printf(gettext("VER POOL\n"));
4321 (void) printf(gettext("--- ------------\n"));
4322 cbp->cb_first = B_FALSE;
4325 (void) printf("%2llu %s\n", (u_longlong_t)version,
4326 zpool_get_name(zhp));
4328 cbp->cb_first = B_FALSE;
4329 ret = zpool_upgrade(zhp, cbp->cb_version);
4331 (void) printf(gettext("Successfully upgraded "
4332 "'%s'\n\n"), zpool_get_name(zhp));
4335 } else if (cbp->cb_newer && !SPA_VERSION_IS_SUPPORTED(version)) {
4336 assert(!cbp->cb_all);
4338 if (cbp->cb_first) {
4339 (void) printf(gettext("The following pools are "
4340 "formatted using an unsupported software version "
4341 "and\ncannot be accessed on the current "
4343 (void) printf(gettext("VER POOL\n"));
4344 (void) printf(gettext("--- ------------\n"));
4345 cbp->cb_first = B_FALSE;
4348 (void) printf("%2llu %s\n", (u_longlong_t)version,
4349 zpool_get_name(zhp));
4358 upgrade_one(zpool_handle_t *zhp, void *data)
4360 upgrade_cbdata_t *cbp = data;
4361 uint64_t cur_version;
4364 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4365 (void) printf(gettext("'log' is now a reserved word\n"
4366 "Pool 'log' must be renamed using export and import"
4371 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4372 if (cur_version > cbp->cb_version) {
4373 (void) printf(gettext("Pool '%s' is already formatted "
4374 "using more current version '%llu'.\n"),
4375 zpool_get_name(zhp), (u_longlong_t) cur_version);
4378 if (cur_version == cbp->cb_version) {
4379 (void) printf(gettext("Pool '%s' is already formatted "
4380 "using the current version.\n"), zpool_get_name(zhp));
4384 ret = zpool_upgrade(zhp, cbp->cb_version);
4387 (void) printf(gettext("Successfully upgraded '%s' "
4388 "from version %llu to version %llu\n\n"),
4389 zpool_get_name(zhp), (u_longlong_t)cur_version,
4390 (u_longlong_t)cbp->cb_version);
4399 * zpool upgrade [-V version] <-a | pool ...>
4401 * With no arguments, display downrev'd ZFS pool available for upgrade.
4402 * Individual pools can be upgraded by specifying the pool, and '-a' will
4403 * upgrade all pools.
4406 zpool_do_upgrade(int argc, char **argv)
4409 upgrade_cbdata_t cb = { 0 };
4411 boolean_t showversions = B_FALSE;
4416 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4422 showversions = B_TRUE;
4425 cb.cb_version = strtoll(optarg, &end, 10);
4427 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4428 (void) fprintf(stderr,
4429 gettext("invalid version '%s'\n"), optarg);
4434 (void) fprintf(stderr, gettext("missing argument for "
4435 "'%c' option\n"), optopt);
4439 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4450 if (cb.cb_version == 0) {
4451 cb.cb_version = SPA_VERSION;
4452 } else if (!cb.cb_all && argc == 0) {
4453 (void) fprintf(stderr, gettext("-V option is "
4454 "incompatible with other arguments\n"));
4459 if (cb.cb_all || argc != 0) {
4460 (void) fprintf(stderr, gettext("-v option is "
4461 "incompatible with other arguments\n"));
4464 } else if (cb.cb_all) {
4466 (void) fprintf(stderr, gettext("-a option should not "
4467 "be used along with a pool name\n"));
4472 (void) printf(gettext("This system supports ZFS pool feature "
4474 cb.cb_first = B_TRUE;
4476 (void) printf(gettext("The following versions are "
4478 (void) printf(gettext("VER DESCRIPTION\n"));
4479 (void) printf("--- -----------------------------------------"
4480 "---------------\n");
4481 (void) printf(gettext(" 1 Initial ZFS version\n"));
4482 (void) printf(gettext(" 2 Ditto blocks "
4483 "(replicated metadata)\n"));
4484 (void) printf(gettext(" 3 Hot spares and double parity "
4486 (void) printf(gettext(" 4 zpool history\n"));
4487 (void) printf(gettext(" 5 Compression using the gzip "
4489 (void) printf(gettext(" 6 bootfs pool property\n"));
4490 (void) printf(gettext(" 7 Separate intent log devices\n"));
4491 (void) printf(gettext(" 8 Delegated administration\n"));
4492 (void) printf(gettext(" 9 refquota and refreservation "
4494 (void) printf(gettext(" 10 Cache devices\n"));
4495 (void) printf(gettext(" 11 Improved scrub performance\n"));
4496 (void) printf(gettext(" 12 Snapshot properties\n"));
4497 (void) printf(gettext(" 13 snapused property\n"));
4498 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4499 (void) printf(gettext(" 15 user/group space accounting\n"));
4500 (void) printf(gettext(" 16 stmf property support\n"));
4501 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4502 (void) printf(gettext(" 18 Snapshot user holds\n"));
4503 (void) printf(gettext(" 19 Log device removal\n"));
4504 (void) printf(gettext(" 20 Compression using zle "
4505 "(zero-length encoding)\n"));
4506 (void) printf(gettext(" 21 Deduplication\n"));
4507 (void) printf(gettext(" 22 Received properties\n"));
4508 (void) printf(gettext(" 23 Slim ZIL\n"));
4509 (void) printf(gettext(" 24 System attributes\n"));
4510 (void) printf(gettext(" 25 Improved scrub stats\n"));
4511 (void) printf(gettext(" 26 Improved snapshot deletion "
4513 (void) printf(gettext(" 27 Improved snapshot creation "
4515 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4516 (void) printf(gettext("\nFor more information on a particular "
4517 "version, including supported releases,\n"));
4518 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4519 } else if (argc == 0) {
4522 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4523 notfound = cb.cb_first;
4525 if (!cb.cb_all && ret == 0) {
4527 (void) printf("\n");
4528 cb.cb_first = B_TRUE;
4529 cb.cb_newer = B_TRUE;
4530 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4533 (void) printf("\n");
4539 (void) printf(gettext("All pools are formatted "
4540 "using this version.\n"));
4541 else if (!cb.cb_all)
4542 (void) printf(gettext("Use 'zpool upgrade -v' "
4543 "for a list of available versions and "
4544 "their associated\nfeatures.\n"));
4547 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4554 typedef struct hist_cbdata {
4561 * Print out the command history for a specific pool.
4564 get_history_one(zpool_handle_t *zhp, void *data)
4580 char internalstr[MAXPATHLEN];
4581 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4585 cb->first = B_FALSE;
4587 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4589 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4592 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4593 &records, &numrecords) == 0);
4594 for (i = 0; i < numrecords; i++) {
4595 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4599 /* is it an internal event or a standard event? */
4600 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4602 if (cb->internal == 0)
4605 if (nvlist_lookup_uint64(records[i],
4606 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4608 verify(nvlist_lookup_uint64(records[i],
4609 ZPOOL_HIST_TXG, &txg) == 0);
4610 verify(nvlist_lookup_string(records[i],
4611 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4612 if (ievent >= LOG_END)
4614 (void) snprintf(internalstr,
4615 sizeof (internalstr),
4616 "[internal %s txg:%llu] %s",
4617 zfs_history_event_names[ievent], (u_longlong_t)txg,
4619 cmdstr = internalstr;
4622 (void) localtime_r(&tsec, &t);
4623 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4624 (void) printf("%s %s", tbuf, cmdstr);
4627 (void) printf("\n");
4630 (void) printf(" [");
4631 if (nvlist_lookup_uint64(records[i],
4632 ZPOOL_HIST_WHO, &who) == 0) {
4633 pwd = getpwuid((uid_t)who);
4635 (void) printf("user %s on",
4638 (void) printf("user %d on",
4641 (void) printf(gettext("no info]\n"));
4644 if (nvlist_lookup_string(records[i],
4645 ZPOOL_HIST_HOST, &hostname) == 0) {
4646 (void) printf(" %s", hostname);
4648 if (nvlist_lookup_string(records[i],
4649 ZPOOL_HIST_ZONE, &zonename) == 0) {
4650 (void) printf(":%s", zonename);
4654 (void) printf("\n");
4656 (void) printf("\n");
4663 * zpool history <pool>
4665 * Displays the history of commands that modified pools.
4670 zpool_do_history(int argc, char **argv)
4672 hist_cbdata_t cbdata = { 0 };
4676 cbdata.first = B_TRUE;
4678 while ((c = getopt(argc, argv, "li")) != -1) {
4684 cbdata.internal = 1;
4687 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4695 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4698 if (argc == 0 && cbdata.first == B_TRUE) {
4699 (void) fprintf(stderr, gettext("no pools available\n"));
4706 typedef struct ev_opts {
4714 zpool_do_events_short(nvlist_t *nvl)
4716 char ctime_str[26], str[32], *ptr;
4720 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
4721 memset(str, ' ', 32);
4722 (void) ctime_r((const time_t *)&tv[0], ctime_str);
4723 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
4724 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
4725 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
4726 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
4727 (void) printf(gettext("%s "), str);
4729 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
4730 (void) printf(gettext("%s\n"), ptr);
4734 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
4738 for (nvp = nvlist_next_nvpair(nvl, NULL);
4739 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
4741 data_type_t type = nvpair_type(nvp);
4742 const char *name = nvpair_name(nvp);
4752 printf(gettext("%*s%s = "), depth, "", name);
4755 case DATA_TYPE_BOOLEAN:
4756 printf(gettext("%s"), "1");
4759 case DATA_TYPE_BOOLEAN_VALUE:
4760 (void) nvpair_value_boolean_value(nvp, &b);
4761 printf(gettext("%s"), b ? "1" : "0");
4764 case DATA_TYPE_BYTE:
4765 (void) nvpair_value_byte(nvp, &i8);
4766 printf(gettext("0x%x"), i8);
4769 case DATA_TYPE_INT8:
4770 (void) nvpair_value_int8(nvp, (void *)&i8);
4771 printf(gettext("0x%x"), i8);
4774 case DATA_TYPE_UINT8:
4775 (void) nvpair_value_uint8(nvp, &i8);
4776 printf(gettext("0x%x"), i8);
4779 case DATA_TYPE_INT16:
4780 (void) nvpair_value_int16(nvp, (void *)&i16);
4781 printf(gettext("0x%x"), i16);
4784 case DATA_TYPE_UINT16:
4785 (void) nvpair_value_uint16(nvp, &i16);
4786 printf(gettext("0x%x"), i16);
4789 case DATA_TYPE_INT32:
4790 (void) nvpair_value_int32(nvp, (void *)&i32);
4791 printf(gettext("0x%x"), i32);
4794 case DATA_TYPE_UINT32:
4795 (void) nvpair_value_uint32(nvp, &i32);
4796 printf(gettext("0x%x"), i32);
4799 case DATA_TYPE_INT64:
4800 (void) nvpair_value_int64(nvp, (void *)&i64);
4801 printf(gettext("0x%llx"), (u_longlong_t)i64);
4804 case DATA_TYPE_UINT64:
4805 (void) nvpair_value_uint64(nvp, &i64);
4806 printf(gettext("0x%llx"), (u_longlong_t)i64);
4809 case DATA_TYPE_HRTIME:
4810 (void) nvpair_value_hrtime(nvp, (void *)&i64);
4811 printf(gettext("0x%llx"), (u_longlong_t)i64);
4814 case DATA_TYPE_STRING:
4815 (void) nvpair_value_string(nvp, &str);
4816 printf(gettext("\"%s\""), str ? str : "<NULL>");
4819 case DATA_TYPE_NVLIST:
4820 printf(gettext("(embedded nvlist)\n"));
4821 (void) nvpair_value_nvlist(nvp, &cnv);
4822 zpool_do_events_nvprint(cnv, depth + 8);
4823 printf(gettext("%*s(end %s)"), depth, "", name);
4826 case DATA_TYPE_NVLIST_ARRAY: {
4830 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
4831 printf(gettext("(%d embedded nvlists)\n"), nelem);
4832 for (i = 0; i < nelem; i++) {
4833 printf(gettext("%*s%s[%d] = %s\n"),
4834 depth, "", name, i, "(embedded nvlist)");
4835 zpool_do_events_nvprint(val[i], depth + 8);
4836 printf(gettext("%*s(end %s[%i])\n"),
4837 depth, "", name, i);
4839 printf(gettext("%*s(end %s)\n"), depth, "", name);
4843 case DATA_TYPE_INT8_ARRAY: {
4847 (void) nvpair_value_int8_array(nvp, &val, &nelem);
4848 for (i = 0; i < nelem; i++)
4849 printf(gettext("0x%x "), val[i]);
4854 case DATA_TYPE_UINT8_ARRAY: {
4858 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
4859 for (i = 0; i < nelem; i++)
4860 printf(gettext("0x%x "), val[i]);
4865 case DATA_TYPE_INT16_ARRAY: {
4869 (void) nvpair_value_int16_array(nvp, &val, &nelem);
4870 for (i = 0; i < nelem; i++)
4871 printf(gettext("0x%x "), val[i]);
4876 case DATA_TYPE_UINT16_ARRAY: {
4880 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
4881 for (i = 0; i < nelem; i++)
4882 printf(gettext("0x%x "), val[i]);
4887 case DATA_TYPE_INT32_ARRAY: {
4891 (void) nvpair_value_int32_array(nvp, &val, &nelem);
4892 for (i = 0; i < nelem; i++)
4893 printf(gettext("0x%x "), val[i]);
4898 case DATA_TYPE_UINT32_ARRAY: {
4902 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
4903 for (i = 0; i < nelem; i++)
4904 printf(gettext("0x%x "), val[i]);
4909 case DATA_TYPE_INT64_ARRAY: {
4913 (void) nvpair_value_int64_array(nvp, &val, &nelem);
4914 for (i = 0; i < nelem; i++)
4915 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4920 case DATA_TYPE_UINT64_ARRAY: {
4924 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
4925 for (i = 0; i < nelem; i++)
4926 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4931 case DATA_TYPE_STRING_ARRAY:
4932 case DATA_TYPE_BOOLEAN_ARRAY:
4933 case DATA_TYPE_BYTE_ARRAY:
4934 case DATA_TYPE_DOUBLE:
4935 case DATA_TYPE_UNKNOWN:
4936 printf(gettext("<unknown>"));
4940 printf(gettext("\n"));
4945 zpool_do_events_next(ev_opts_t *opts)
4948 int cleanup_fd, ret, dropped;
4950 cleanup_fd = open(ZFS_DEV, O_RDWR);
4951 VERIFY(cleanup_fd >= 0);
4953 if (!opts->scripted)
4954 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
4957 ret = zpool_events_next(g_zfs, &nvl, &dropped,
4958 !!opts->follow, cleanup_fd);
4959 if (ret || nvl == NULL)
4963 (void) printf(gettext("dropped %d events\n"), dropped);
4965 zpool_do_events_short(nvl);
4967 if (opts->verbose) {
4968 zpool_do_events_nvprint(nvl, 8);
4969 printf(gettext("\n"));
4975 VERIFY(0 == close(cleanup_fd));
4981 zpool_do_events_clear(ev_opts_t *opts)
4985 ret = zpool_events_clear(g_zfs, &count);
4987 (void) printf(gettext("cleared %d events\n"), count);
4993 * zpool events [-vfc]
4995 * Displays events logs by ZFS.
4998 zpool_do_events(int argc, char **argv)
5000 ev_opts_t opts = { 0 };
5005 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5020 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5029 ret = zpool_do_events_clear(&opts);
5031 ret = zpool_do_events_next(&opts);
5037 get_callback(zpool_handle_t *zhp, void *data)
5039 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5040 char value[MAXNAMELEN];
5041 zprop_source_t srctype;
5044 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5047 * Skip the special fake placeholder. This will also skip
5048 * over the name property when 'all' is specified.
5050 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5051 pl == cbp->cb_proplist)
5054 if (pl->pl_prop == ZPROP_INVAL &&
5055 (zpool_prop_feature(pl->pl_user_prop) ||
5056 zpool_prop_unsupported(pl->pl_user_prop))) {
5057 srctype = ZPROP_SRC_LOCAL;
5059 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5060 value, sizeof (value)) == 0) {
5061 zprop_print_one_property(zpool_get_name(zhp),
5062 cbp, pl->pl_user_prop, value, srctype,
5066 if (zpool_get_prop(zhp, pl->pl_prop, value,
5067 sizeof (value), &srctype) != 0)
5070 zprop_print_one_property(zpool_get_name(zhp), cbp,
5071 zpool_prop_to_name(pl->pl_prop), value, srctype,
5079 zpool_do_get(int argc, char **argv)
5081 zprop_get_cbdata_t cb = { 0 };
5082 zprop_list_t fake_name = { 0 };
5086 (void) fprintf(stderr, gettext("missing property "
5091 cb.cb_first = B_TRUE;
5092 cb.cb_sources = ZPROP_SRC_ALL;
5093 cb.cb_columns[0] = GET_COL_NAME;
5094 cb.cb_columns[1] = GET_COL_PROPERTY;
5095 cb.cb_columns[2] = GET_COL_VALUE;
5096 cb.cb_columns[3] = GET_COL_SOURCE;
5097 cb.cb_type = ZFS_TYPE_POOL;
5099 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5100 ZFS_TYPE_POOL) != 0)
5103 if (cb.cb_proplist != NULL) {
5104 fake_name.pl_prop = ZPOOL_PROP_NAME;
5105 fake_name.pl_width = strlen(gettext("NAME"));
5106 fake_name.pl_next = cb.cb_proplist;
5107 cb.cb_proplist = &fake_name;
5110 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5113 if (cb.cb_proplist == &fake_name)
5114 zprop_free_list(fake_name.pl_next);
5116 zprop_free_list(cb.cb_proplist);
5121 typedef struct set_cbdata {
5124 boolean_t cb_any_successful;
5128 set_callback(zpool_handle_t *zhp, void *data)
5131 set_cbdata_t *cb = (set_cbdata_t *)data;
5133 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5136 cb->cb_any_successful = B_TRUE;
5142 zpool_do_set(int argc, char **argv)
5144 set_cbdata_t cb = { 0 };
5147 if (argc > 1 && argv[1][0] == '-') {
5148 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5154 (void) fprintf(stderr, gettext("missing property=value "
5160 (void) fprintf(stderr, gettext("missing pool name\n"));
5165 (void) fprintf(stderr, gettext("too many pool names\n"));
5169 cb.cb_propname = argv[1];
5170 cb.cb_value = strchr(cb.cb_propname, '=');
5171 if (cb.cb_value == NULL) {
5172 (void) fprintf(stderr, gettext("missing value in "
5173 "property=value argument\n"));
5177 *(cb.cb_value) = '\0';
5180 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5187 find_command_idx(char *command, int *idx)
5191 for (i = 0; i < NCOMMAND; i++) {
5192 if (command_table[i].name == NULL)
5195 if (strcmp(command, command_table[i].name) == 0) {
5204 main(int argc, char **argv)
5210 (void) setlocale(LC_ALL, "");
5211 (void) textdomain(TEXT_DOMAIN);
5216 * Make sure the user has specified some command.
5219 (void) fprintf(stderr, gettext("missing command\n"));
5228 if ((strcmp(cmdname, "-?") == 0) ||
5229 strcmp(cmdname, "--help") == 0)
5232 if ((g_zfs = libzfs_init()) == NULL)
5235 libzfs_print_on_error(g_zfs, B_TRUE);
5237 zpool_set_history_str("zpool", argc, argv, history_str);
5238 verify(zpool_stage_history(g_zfs, history_str) == 0);
5241 * Run the appropriate command.
5243 if (find_command_idx(cmdname, &i) == 0) {
5244 current_command = &command_table[i];
5245 ret = command_table[i].func(argc - 1, argv + 1);
5246 } else if (strchr(cmdname, '=')) {
5247 verify(find_command_idx("set", &i) == 0);
5248 current_command = &command_table[i];
5249 ret = command_table[i].func(argc, argv);
5250 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5252 * 'freeze' is a vile debugging abomination, so we treat
5256 int fd = open(ZFS_DEV, O_RDWR);
5257 (void) strcpy((void *)buf, argv[2]);
5258 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5260 (void) fprintf(stderr, gettext("unrecognized "
5261 "command '%s'\n"), cmdname);
5269 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5270 * for the purposes of running ::findleaks.
5272 if (getenv("ZFS_ABORT") != NULL) {
5273 (void) printf("dumping core by request\n");