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 prop_list_contains_feature(nvlist_t *proplist)
398 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
399 nvp = nvlist_next_nvpair(proplist, nvp)) {
400 if (zpool_prop_feature(nvpair_name(nvp)))
407 * Add a property pair (name, string-value) into a property nvlist.
410 add_prop_list(const char *propname, char *propval, nvlist_t **props,
413 zpool_prop_t prop = ZPROP_INVAL;
419 if (*props == NULL &&
420 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
421 (void) fprintf(stderr,
422 gettext("internal error: out of memory\n"));
429 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
431 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
432 !zpool_prop_feature(propname)) {
433 (void) fprintf(stderr, gettext("property '%s' is "
434 "not a valid pool property\n"), propname);
439 * feature@ properties and version should not be specified
442 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
443 nvlist_exists(proplist, vname)) ||
444 (prop == ZPOOL_PROP_VERSION &&
445 prop_list_contains_feature(proplist))) {
446 (void) fprintf(stderr, gettext("'feature@' and "
447 "'version' properties cannot be specified "
453 if (zpool_prop_feature(propname))
456 normnm = zpool_prop_to_name(prop);
458 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
459 normnm = zfs_prop_to_name(fprop);
465 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
466 prop != ZPOOL_PROP_CACHEFILE) {
467 (void) fprintf(stderr, gettext("property '%s' "
468 "specified multiple times\n"), propname);
472 if (nvlist_add_string(proplist, normnm, propval) != 0) {
473 (void) fprintf(stderr, gettext("internal "
474 "error: out of memory\n"));
482 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
484 * -f Force addition of devices, even if they appear in use
485 * -n Do not add the devices, but display the resulting layout if
486 * they were to be added.
487 * -o Set property=value.
489 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
490 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
494 zpool_do_add(int argc, char **argv)
496 boolean_t force = B_FALSE;
497 boolean_t dryrun = B_FALSE;
504 nvlist_t *props = NULL;
508 while ((c = getopt(argc, argv, "fno:")) != -1) {
517 if ((propval = strchr(optarg, '=')) == NULL) {
518 (void) fprintf(stderr, gettext("missing "
519 "'=' for -o option\n"));
525 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
526 (add_prop_list(optarg, propval, &props, B_TRUE)))
530 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
539 /* get pool name and check number of arguments */
541 (void) fprintf(stderr, gettext("missing pool name argument\n"));
545 (void) fprintf(stderr, gettext("missing vdev specification\n"));
554 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
557 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
558 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
564 /* pass off to get_vdev_spec for processing */
565 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
567 if (nvroot == NULL) {
573 nvlist_t *poolnvroot;
575 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
578 (void) printf(gettext("would update '%s' to the following "
579 "configuration:\n"), zpool_get_name(zhp));
581 /* print original main pool and new tree */
582 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
583 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
585 /* Do the same for the logs */
586 if (num_logs(poolnvroot) > 0) {
587 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
588 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
589 } else if (num_logs(nvroot) > 0) {
590 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
595 ret = (zpool_add(zhp, nvroot) != 0);
606 * zpool remove <pool> <vdev> ...
608 * Removes the given vdev from the pool. Currently, this supports removing
609 * spares, cache, and log devices from the pool.
612 zpool_do_remove(int argc, char **argv)
621 /* get pool name and check number of arguments */
623 (void) fprintf(stderr, gettext("missing pool name argument\n"));
627 (void) fprintf(stderr, gettext("missing device\n"));
633 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
636 for (i = 1; i < argc; i++) {
637 if (zpool_vdev_remove(zhp, argv[i]) != 0)
645 * zpool create [-fnd] [-o property=value] ...
646 * [-O file-system-property=value] ...
647 * [-R root] [-m mountpoint] <pool> <dev> ...
649 * -f Force creation, even if devices appear in use
650 * -n Do not create the pool, but display the resulting layout if it
651 * were to be created.
652 * -R Create a pool under an alternate root
653 * -m Set default mountpoint for the root dataset. By default it's
655 * -o Set property=value.
656 * -d Don't automatically enable all supported pool features
657 * (individual features can be enabled with -o).
658 * -O Set fsproperty=value in the pool's root file system
660 * Creates the named pool according to the given vdev specification. The
661 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
662 * we get the nvlist back from get_vdev_spec(), we either print out the contents
663 * (if '-n' was specified), or pass it to libzfs to do the creation.
666 zpool_do_create(int argc, char **argv)
668 boolean_t force = B_FALSE;
669 boolean_t dryrun = B_FALSE;
670 boolean_t enable_all_pool_feat = B_TRUE;
672 nvlist_t *nvroot = NULL;
675 char *altroot = NULL;
676 char *mountpoint = NULL;
677 nvlist_t *fsprops = NULL;
678 nvlist_t *props = NULL;
682 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
691 enable_all_pool_feat = B_FALSE;
695 if (add_prop_list(zpool_prop_to_name(
696 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
698 if (nvlist_lookup_string(props,
699 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
702 if (add_prop_list(zpool_prop_to_name(
703 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
710 if ((propval = strchr(optarg, '=')) == NULL) {
711 (void) fprintf(stderr, gettext("missing "
712 "'=' for -o option\n"));
718 if (add_prop_list(optarg, propval, &props, B_TRUE))
722 * If the user is creating a pool that doesn't support
723 * feature flags, don't enable any features.
725 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
729 ver = strtoull(propval, &end, 10);
731 ver < SPA_VERSION_FEATURES) {
732 enable_all_pool_feat = B_FALSE;
737 if ((propval = strchr(optarg, '=')) == NULL) {
738 (void) fprintf(stderr, gettext("missing "
739 "'=' for -O option\n"));
745 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
749 (void) fprintf(stderr, gettext("missing argument for "
750 "'%c' option\n"), optopt);
753 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
762 /* get pool name and check number of arguments */
764 (void) fprintf(stderr, gettext("missing pool name argument\n"));
768 (void) fprintf(stderr, gettext("missing vdev specification\n"));
775 * As a special case, check for use of '/' in the name, and direct the
776 * user to use 'zfs create' instead.
778 if (strchr(poolname, '/') != NULL) {
779 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
780 "character '/' in pool name\n"), poolname);
781 (void) fprintf(stderr, gettext("use 'zfs create' to "
782 "create a dataset\n"));
786 /* pass off to get_vdev_spec for bulk processing */
787 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
792 /* make_root_vdev() allows 0 toplevel children if there are spares */
793 if (!zfs_allocatable_devs(nvroot)) {
794 (void) fprintf(stderr, gettext("invalid vdev "
795 "specification: at least one toplevel vdev must be "
800 if (altroot != NULL && altroot[0] != '/') {
801 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
802 "must be an absolute path\n"), altroot);
807 * Check the validity of the mountpoint and direct the user to use the
808 * '-m' mountpoint option if it looks like its in use.
810 if (mountpoint == NULL ||
811 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
812 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
813 char buf[MAXPATHLEN];
816 if (mountpoint && mountpoint[0] != '/') {
817 (void) fprintf(stderr, gettext("invalid mountpoint "
818 "'%s': must be an absolute path, 'legacy', or "
819 "'none'\n"), mountpoint);
823 if (mountpoint == NULL) {
825 (void) snprintf(buf, sizeof (buf), "%s/%s",
828 (void) snprintf(buf, sizeof (buf), "/%s",
832 (void) snprintf(buf, sizeof (buf), "%s%s",
833 altroot, mountpoint);
835 (void) snprintf(buf, sizeof (buf), "%s",
839 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
840 (void) fprintf(stderr, gettext("mountpoint '%s' : "
841 "%s\n"), buf, strerror(errno));
842 (void) fprintf(stderr, gettext("use '-m' "
843 "option to provide a different default\n"));
848 while (count < 3 && readdir(dirp) != NULL)
850 (void) closedir(dirp);
853 (void) fprintf(stderr, gettext("mountpoint "
854 "'%s' exists and is not empty\n"), buf);
855 (void) fprintf(stderr, gettext("use '-m' "
856 "option to provide a "
857 "different default\n"));
865 * For a dry run invocation, print out a basic message and run
866 * through all the vdevs in the list and print out in an
867 * appropriate hierarchy.
869 (void) printf(gettext("would create '%s' with the "
870 "following layout:\n\n"), poolname);
872 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
873 if (num_logs(nvroot) > 0)
874 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
879 * Hand off to libzfs.
881 if (enable_all_pool_feat) {
883 for (i = 0; i < SPA_FEATURES; i++) {
884 char propname[MAXPATHLEN];
885 zfeature_info_t *feat = &spa_feature_table[i];
887 (void) snprintf(propname, sizeof (propname),
888 "feature@%s", feat->fi_uname);
891 * Skip feature if user specified it manually
892 * on the command line.
894 if (nvlist_exists(props, propname))
897 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
898 &props, B_TRUE) != 0)
902 if (zpool_create(g_zfs, poolname,
903 nvroot, props, fsprops) == 0) {
904 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
905 ZFS_TYPE_FILESYSTEM);
907 if (mountpoint != NULL)
908 verify(zfs_prop_set(pool,
910 ZFS_PROP_MOUNTPOINT),
912 if (zfs_mount(pool, NULL, 0) == 0)
913 ret = zfs_shareall(pool);
916 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
917 (void) fprintf(stderr, gettext("pool name may have "
924 nvlist_free(fsprops);
928 nvlist_free(fsprops);
935 * zpool destroy <pool>
937 * -f Forcefully unmount any datasets
939 * Destroy the given pool. Automatically unmounts any datasets in the pool.
942 zpool_do_destroy(int argc, char **argv)
944 boolean_t force = B_FALSE;
951 while ((c = getopt(argc, argv, "f")) != -1) {
957 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
966 /* check arguments */
968 (void) fprintf(stderr, gettext("missing pool argument\n"));
972 (void) fprintf(stderr, gettext("too many arguments\n"));
978 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
980 * As a special case, check for use of '/' in the name, and
981 * direct the user to use 'zfs destroy' instead.
983 if (strchr(pool, '/') != NULL)
984 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
985 "destroy a dataset\n"));
989 if (zpool_disable_datasets(zhp, force) != 0) {
990 (void) fprintf(stderr, gettext("could not destroy '%s': "
991 "could not unmount datasets\n"), zpool_get_name(zhp));
995 ret = (zpool_destroy(zhp) != 0);
1003 * zpool export [-f] <pool> ...
1005 * -f Forcefully unmount datasets
1007 * Export the given pools. By default, the command will attempt to cleanly
1008 * unmount any active datasets within the pool. If the '-f' flag is specified,
1009 * then the datasets will be forcefully unmounted.
1012 zpool_do_export(int argc, char **argv)
1014 boolean_t force = B_FALSE;
1015 boolean_t hardforce = B_FALSE;
1017 zpool_handle_t *zhp;
1022 while ((c = getopt(argc, argv, "fF")) != -1) {
1031 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1040 /* check arguments */
1042 (void) fprintf(stderr, gettext("missing pool argument\n"));
1047 for (i = 0; i < argc; i++) {
1048 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1053 if (zpool_disable_datasets(zhp, force) != 0) {
1060 if (zpool_export_force(zhp) != 0)
1062 } else if (zpool_export(zhp, force) != 0) {
1073 * Given a vdev configuration, determine the maximum width needed for the device
1077 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1079 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1084 if (strlen(name) + depth > max)
1085 max = strlen(name) + depth;
1089 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1090 &child, &children) == 0) {
1091 for (c = 0; c < children; c++)
1092 if ((ret = max_width(zhp, child[c], depth + 2,
1097 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1098 &child, &children) == 0) {
1099 for (c = 0; c < children; c++)
1100 if ((ret = max_width(zhp, child[c], depth + 2,
1105 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1106 &child, &children) == 0) {
1107 for (c = 0; c < children; c++)
1108 if ((ret = max_width(zhp, child[c], depth + 2,
1117 typedef struct spare_cbdata {
1119 zpool_handle_t *cb_zhp;
1123 find_vdev(nvlist_t *nv, uint64_t search)
1129 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1133 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1134 &child, &children) == 0) {
1135 for (c = 0; c < children; c++)
1136 if (find_vdev(child[c], search))
1144 find_spare(zpool_handle_t *zhp, void *data)
1146 spare_cbdata_t *cbp = data;
1147 nvlist_t *config, *nvroot;
1149 config = zpool_get_config(zhp, NULL);
1150 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1153 if (find_vdev(nvroot, cbp->cb_guid)) {
1163 * Print out configuration state as requested by status_callback.
1166 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1167 int namewidth, int depth, boolean_t isspare)
1171 pool_scan_stat_t *ps = NULL;
1173 char rbuf[6], wbuf[6], cbuf[6];
1175 uint64_t notpresent;
1179 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1180 &child, &children) != 0)
1183 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1184 (uint64_t **)&vs, &c) == 0);
1186 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1189 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1192 if (vs->vs_aux == VDEV_AUX_SPARED)
1194 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1198 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1202 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1203 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1204 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1205 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1208 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1209 ¬present) == 0) {
1211 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1212 (void) printf(" was %s", path);
1213 } else if (vs->vs_aux != 0) {
1216 switch (vs->vs_aux) {
1217 case VDEV_AUX_OPEN_FAILED:
1218 (void) printf(gettext("cannot open"));
1221 case VDEV_AUX_BAD_GUID_SUM:
1222 (void) printf(gettext("missing device"));
1225 case VDEV_AUX_NO_REPLICAS:
1226 (void) printf(gettext("insufficient replicas"));
1229 case VDEV_AUX_VERSION_NEWER:
1230 (void) printf(gettext("newer version"));
1233 case VDEV_AUX_UNSUP_FEAT:
1234 (void) printf(gettext("unsupported feature(s)"));
1237 case VDEV_AUX_SPARED:
1238 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1240 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1241 if (strcmp(zpool_get_name(cb.cb_zhp),
1242 zpool_get_name(zhp)) == 0)
1243 (void) printf(gettext("currently in "
1246 (void) printf(gettext("in use by "
1248 zpool_get_name(cb.cb_zhp));
1249 zpool_close(cb.cb_zhp);
1251 (void) printf(gettext("currently in use"));
1255 case VDEV_AUX_ERR_EXCEEDED:
1256 (void) printf(gettext("too many errors"));
1259 case VDEV_AUX_IO_FAILURE:
1260 (void) printf(gettext("experienced I/O failures"));
1263 case VDEV_AUX_BAD_LOG:
1264 (void) printf(gettext("bad intent log"));
1267 case VDEV_AUX_EXTERNAL:
1268 (void) printf(gettext("external device fault"));
1271 case VDEV_AUX_SPLIT_POOL:
1272 (void) printf(gettext("split into new pool"));
1276 (void) printf(gettext("corrupted data"));
1281 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1282 (uint64_t **)&ps, &c);
1284 if (ps && ps->pss_state == DSS_SCANNING &&
1285 vs->vs_scan_processed != 0 && children == 0) {
1286 (void) printf(gettext(" (%s)"),
1287 (ps->pss_func == POOL_SCAN_RESILVER) ?
1288 "resilvering" : "repairing");
1291 (void) printf("\n");
1293 for (c = 0; c < children; c++) {
1294 uint64_t islog = B_FALSE, ishole = B_FALSE;
1296 /* Don't print logs or holes here */
1297 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1299 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1301 if (islog || ishole)
1303 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1304 print_status_config(zhp, vname, child[c],
1305 namewidth, depth + 2, isspare);
1312 * Print the configuration of an exported pool. Iterate over all vdevs in the
1313 * pool, printing out the name and status for each one.
1316 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1323 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1324 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1325 strcmp(type, VDEV_TYPE_HOLE) == 0)
1328 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1329 (uint64_t **)&vs, &c) == 0);
1331 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1332 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1334 if (vs->vs_aux != 0) {
1337 switch (vs->vs_aux) {
1338 case VDEV_AUX_OPEN_FAILED:
1339 (void) printf(gettext("cannot open"));
1342 case VDEV_AUX_BAD_GUID_SUM:
1343 (void) printf(gettext("missing device"));
1346 case VDEV_AUX_NO_REPLICAS:
1347 (void) printf(gettext("insufficient replicas"));
1350 case VDEV_AUX_VERSION_NEWER:
1351 (void) printf(gettext("newer version"));
1354 case VDEV_AUX_UNSUP_FEAT:
1355 (void) printf(gettext("unsupported feature(s)"));
1358 case VDEV_AUX_ERR_EXCEEDED:
1359 (void) printf(gettext("too many errors"));
1363 (void) printf(gettext("corrupted data"));
1367 (void) printf("\n");
1369 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1370 &child, &children) != 0)
1373 for (c = 0; c < children; c++) {
1374 uint64_t is_log = B_FALSE;
1376 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1381 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1382 print_import_config(vname, child[c], namewidth, depth + 2);
1386 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1387 &child, &children) == 0) {
1388 (void) printf(gettext("\tcache\n"));
1389 for (c = 0; c < children; c++) {
1390 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1391 (void) printf("\t %s\n", vname);
1396 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1397 &child, &children) == 0) {
1398 (void) printf(gettext("\tspares\n"));
1399 for (c = 0; c < children; c++) {
1400 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1401 (void) printf("\t %s\n", vname);
1409 * Logs are recorded as top level vdevs in the main pool child array
1410 * but with "is_log" set to 1. We use either print_status_config() or
1411 * print_import_config() to print the top level logs then any log
1412 * children (eg mirrored slogs) are printed recursively - which
1413 * works because only the top level vdev is marked "is_log"
1416 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1421 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1425 (void) printf(gettext("\tlogs\n"));
1427 for (c = 0; c < children; c++) {
1428 uint64_t is_log = B_FALSE;
1431 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1435 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1437 print_status_config(zhp, name, child[c], namewidth,
1440 print_import_config(name, child[c], namewidth, 2);
1446 * Display the status for the given pool.
1449 show_import(nvlist_t *config)
1451 uint64_t pool_state;
1463 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1465 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1467 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1469 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1472 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1473 (uint64_t **)&vs, &vsc) == 0);
1474 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1476 reason = zpool_import_status(config, &msgid);
1478 (void) printf(gettext(" pool: %s\n"), name);
1479 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1480 (void) printf(gettext(" state: %s"), health);
1481 if (pool_state == POOL_STATE_DESTROYED)
1482 (void) printf(gettext(" (DESTROYED)"));
1483 (void) printf("\n");
1486 case ZPOOL_STATUS_MISSING_DEV_R:
1487 case ZPOOL_STATUS_MISSING_DEV_NR:
1488 case ZPOOL_STATUS_BAD_GUID_SUM:
1489 (void) printf(gettext(" status: One or more devices are "
1490 "missing from the system.\n"));
1493 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1494 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1495 (void) printf(gettext(" status: One or more devices contains "
1496 "corrupted data.\n"));
1499 case ZPOOL_STATUS_CORRUPT_DATA:
1501 gettext(" status: The pool data is corrupted.\n"));
1504 case ZPOOL_STATUS_OFFLINE_DEV:
1505 (void) printf(gettext(" status: One or more devices "
1506 "are offlined.\n"));
1509 case ZPOOL_STATUS_CORRUPT_POOL:
1510 (void) printf(gettext(" status: The pool metadata is "
1514 case ZPOOL_STATUS_VERSION_OLDER:
1515 (void) printf(gettext(" status: The pool is formatted using a "
1516 "legacy on-disk version.\n"));
1519 case ZPOOL_STATUS_VERSION_NEWER:
1520 (void) printf(gettext(" status: The pool is formatted using an "
1521 "incompatible version.\n"));
1524 case ZPOOL_STATUS_FEAT_DISABLED:
1525 (void) printf(gettext(" status: Some supported features are "
1526 "not enabled on the pool.\n"));
1529 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1530 (void) printf(gettext("status: The pool uses the following "
1531 "feature(s) not supported on this sytem:\n"));
1532 zpool_print_unsup_feat(config);
1535 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1536 (void) printf(gettext("status: The pool can only be accessed "
1537 "in read-only mode on this system. It\n\tcannot be "
1538 "accessed in read-write mode because it uses the "
1539 "following\n\tfeature(s) not supported on this system:\n"));
1540 zpool_print_unsup_feat(config);
1543 case ZPOOL_STATUS_HOSTID_MISMATCH:
1544 (void) printf(gettext(" status: The pool was last accessed by "
1545 "another system.\n"));
1548 case ZPOOL_STATUS_FAULTED_DEV_R:
1549 case ZPOOL_STATUS_FAULTED_DEV_NR:
1550 (void) printf(gettext(" status: One or more devices are "
1554 case ZPOOL_STATUS_BAD_LOG:
1555 (void) printf(gettext(" status: An intent log record cannot be "
1559 case ZPOOL_STATUS_RESILVERING:
1560 (void) printf(gettext(" status: One or more devices were being "
1566 * No other status can be seen when importing pools.
1568 assert(reason == ZPOOL_STATUS_OK);
1572 * Print out an action according to the overall state of the pool.
1574 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1575 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1576 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1577 (void) printf(gettext(" action: The pool can be "
1578 "imported using its name or numeric identifier, "
1579 "though\n\tsome features will not be available "
1580 "without an explicit 'zpool upgrade'.\n"));
1581 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1582 (void) printf(gettext(" action: The pool can be "
1583 "imported using its name or numeric "
1584 "identifier and\n\tthe '-f' flag.\n"));
1586 (void) printf(gettext(" action: The pool can be "
1587 "imported using its name or numeric "
1590 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1591 (void) printf(gettext(" action: The pool can be imported "
1592 "despite missing or damaged devices. The\n\tfault "
1593 "tolerance of the pool may be compromised if imported.\n"));
1596 case ZPOOL_STATUS_VERSION_NEWER:
1597 (void) printf(gettext(" action: The pool cannot be "
1598 "imported. Access the pool on a system running "
1599 "newer\n\tsoftware, or recreate the pool from "
1602 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1603 (void) printf(gettext("action: The pool cannot be "
1604 "imported. Access the pool on a system that "
1605 "supports\n\tthe required feature(s), or recreate "
1606 "the pool from backup.\n"));
1608 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1609 (void) printf(gettext("action: The pool cannot be "
1610 "imported in read-write mode. Import the pool "
1612 "\t\"-o readonly=on\", access the pool on a system "
1613 "that supports the\n\trequired feature(s), or "
1614 "recreate the pool from backup.\n"));
1616 case ZPOOL_STATUS_MISSING_DEV_R:
1617 case ZPOOL_STATUS_MISSING_DEV_NR:
1618 case ZPOOL_STATUS_BAD_GUID_SUM:
1619 (void) printf(gettext(" action: The pool cannot be "
1620 "imported. Attach the missing\n\tdevices and try "
1624 (void) printf(gettext(" action: The pool cannot be "
1625 "imported due to damaged devices or data.\n"));
1629 /* Print the comment attached to the pool. */
1630 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1631 (void) printf(gettext("comment: %s\n"), comment);
1634 * If the state is "closed" or "can't open", and the aux state
1635 * is "corrupt data":
1637 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1638 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1639 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1640 if (pool_state == POOL_STATE_DESTROYED)
1641 (void) printf(gettext("\tThe pool was destroyed, "
1642 "but can be imported using the '-Df' flags.\n"));
1643 else if (pool_state != POOL_STATE_EXPORTED)
1644 (void) printf(gettext("\tThe pool may be active on "
1645 "another system, but can be imported using\n\t"
1646 "the '-f' flag.\n"));
1650 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1653 (void) printf(gettext(" config:\n\n"));
1655 namewidth = max_width(NULL, nvroot, 0, 0);
1659 print_import_config(name, nvroot, namewidth, 0);
1660 if (num_logs(nvroot) > 0)
1661 print_logs(NULL, nvroot, namewidth, B_FALSE);
1663 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1664 (void) printf(gettext("\n\tAdditional devices are known to "
1665 "be part of this pool, though their\n\texact "
1666 "configuration cannot be determined.\n"));
1671 * Perform the import for the given configuration. This passes the heavy
1672 * lifting off to zpool_import_props(), and then mounts the datasets contained
1676 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1677 nvlist_t *props, int flags)
1679 zpool_handle_t *zhp;
1684 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1687 verify(nvlist_lookup_uint64(config,
1688 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1689 verify(nvlist_lookup_uint64(config,
1690 ZPOOL_CONFIG_VERSION, &version) == 0);
1691 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1692 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1693 "is formatted using an unsupported ZFS version\n"), name);
1695 } else if (state != POOL_STATE_EXPORTED &&
1696 !(flags & ZFS_IMPORT_ANY_HOST)) {
1699 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1701 unsigned long system_hostid = gethostid() & 0xffffffff;
1703 if ((unsigned long)hostid != system_hostid) {
1708 verify(nvlist_lookup_string(config,
1709 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1710 verify(nvlist_lookup_uint64(config,
1711 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1713 (void) fprintf(stderr, gettext("cannot import "
1714 "'%s': pool may be in use from other "
1715 "system, it was last accessed by %s "
1716 "(hostid: 0x%lx) on %s"), name, hostname,
1717 (unsigned long)hostid,
1718 asctime(localtime(&t)));
1719 (void) fprintf(stderr, gettext("use '-f' to "
1720 "import anyway\n"));
1724 (void) fprintf(stderr, gettext("cannot import '%s': "
1725 "pool may be in use from other system\n"), name);
1726 (void) fprintf(stderr, gettext("use '-f' to import "
1732 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1735 if (newname != NULL)
1736 name = (char *)newname;
1738 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1741 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1742 !(flags & ZFS_IMPORT_ONLY) &&
1743 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1753 * zpool import [-d dir] [-D]
1754 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1755 * [-d dir | -c cachefile] [-f] -a
1756 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1757 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1759 * -c Read pool information from a cachefile instead of searching
1762 * -d Scan in a specific directory, other than /dev/. More than
1763 * one directory can be specified using multiple '-d' options.
1765 * -D Scan for previously destroyed pools or import all or only
1766 * specified destroyed pools.
1768 * -R Temporarily import the pool, with all mountpoints relative to
1769 * the given root. The pool will remain exported when the machine
1772 * -V Import even in the presence of faulted vdevs. This is an
1773 * intentionally undocumented option for testing purposes, and
1774 * treats the pool configuration as complete, leaving any bad
1775 * vdevs in the FAULTED state. In other words, it does verbatim
1778 * -f Force import, even if it appears that the pool is active.
1780 * -F Attempt rewind if necessary.
1782 * -n See if rewind would work, but don't actually rewind.
1784 * -N Import the pool but don't mount datasets.
1786 * -T Specify a starting txg to use for import. This option is
1787 * intentionally undocumented option for testing purposes.
1789 * -a Import all pools found.
1791 * -o Set property=value and/or temporary mount options (without '=').
1793 * The import command scans for pools to import, and import pools based on pool
1794 * name and GUID. The pool can also be renamed as part of the import process.
1797 zpool_do_import(int argc, char **argv)
1799 char **searchdirs = NULL;
1800 char *env, *envdup = NULL;
1804 nvlist_t *pools = NULL;
1805 boolean_t do_all = B_FALSE;
1806 boolean_t do_destroyed = B_FALSE;
1807 char *mntopts = NULL;
1810 uint64_t searchguid = 0;
1811 char *searchname = NULL;
1813 nvlist_t *found_config;
1814 nvlist_t *policy = NULL;
1815 nvlist_t *props = NULL;
1817 int flags = ZFS_IMPORT_NORMAL;
1818 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1819 boolean_t dryrun = B_FALSE;
1820 boolean_t do_rewind = B_FALSE;
1821 boolean_t xtreme_rewind = B_FALSE;
1822 uint64_t pool_state, txg = -1ULL;
1823 char *cachefile = NULL;
1824 importargs_t idata = { 0 };
1828 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1837 if (searchdirs == NULL) {
1838 searchdirs = safe_malloc(sizeof (char *));
1840 char **tmp = safe_malloc((nsearch + 1) *
1842 bcopy(searchdirs, tmp, nsearch *
1847 searchdirs[nsearch++] = optarg;
1850 do_destroyed = B_TRUE;
1853 flags |= ZFS_IMPORT_ANY_HOST;
1859 flags |= ZFS_IMPORT_MISSING_LOG;
1865 flags |= ZFS_IMPORT_ONLY;
1868 if ((propval = strchr(optarg, '=')) != NULL) {
1871 if (add_prop_list(optarg, propval,
1879 if (add_prop_list(zpool_prop_to_name(
1880 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1882 if (nvlist_lookup_string(props,
1883 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1886 if (add_prop_list(zpool_prop_to_name(
1887 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1892 txg = strtoull(optarg, &endptr, 10);
1893 if (errno != 0 || *endptr != '\0') {
1894 (void) fprintf(stderr,
1895 gettext("invalid txg value\n"));
1898 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1901 flags |= ZFS_IMPORT_VERBATIM;
1904 xtreme_rewind = B_TRUE;
1907 (void) fprintf(stderr, gettext("missing argument for "
1908 "'%c' option\n"), optopt);
1912 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1921 if (cachefile && nsearch != 0) {
1922 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1926 if ((dryrun || xtreme_rewind) && !do_rewind) {
1927 (void) fprintf(stderr,
1928 gettext("-n or -X only meaningful with -F\n"));
1932 rewind_policy = ZPOOL_TRY_REWIND;
1934 rewind_policy = ZPOOL_DO_REWIND;
1936 rewind_policy |= ZPOOL_EXTREME_REWIND;
1938 /* In the future, we can capture further policy and include it here */
1939 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1940 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1941 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1944 /* check argument count */
1947 (void) fprintf(stderr, gettext("too many arguments\n"));
1952 (void) fprintf(stderr, gettext("too many arguments\n"));
1957 * Check for the SYS_CONFIG privilege. We do this explicitly
1958 * here because otherwise any attempt to discover pools will
1961 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1962 (void) fprintf(stderr, gettext("cannot "
1963 "discover pools: permission denied\n"));
1964 if (searchdirs != NULL)
1967 nvlist_free(policy);
1973 * Depending on the arguments given, we do one of the following:
1975 * <none> Iterate through all pools and display information about
1978 * -a Iterate through all pools and try to import each one.
1980 * <id> Find the pool that corresponds to the given GUID/pool
1981 * name and import that one.
1983 * -D Above options applies only to destroyed pools.
1989 searchguid = strtoull(argv[0], &endptr, 10);
1990 if (errno != 0 || *endptr != '\0')
1991 searchname = argv[0];
1992 found_config = NULL;
1995 * User specified a name or guid. Ensure it's unique.
1997 idata.unique = B_TRUE;
2001 * Check the environment for the preferred search path.
2003 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2006 envdup = strdup(env);
2008 dir = strtok(envdup, ":");
2009 while (dir != NULL) {
2010 if (searchdirs == NULL) {
2011 searchdirs = safe_malloc(sizeof (char *));
2013 char **tmp = safe_malloc((nsearch + 1) *
2015 bcopy(searchdirs, tmp, nsearch *
2020 searchdirs[nsearch++] = dir;
2021 dir = strtok(NULL, ":");
2025 idata.path = searchdirs;
2026 idata.paths = nsearch;
2027 idata.poolname = searchname;
2028 idata.guid = searchguid;
2029 idata.cachefile = cachefile;
2031 pools = zpool_search_import(g_zfs, &idata);
2033 if (pools != NULL && idata.exists &&
2034 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2035 (void) fprintf(stderr, gettext("cannot import '%s': "
2036 "a pool with that name already exists\n"),
2038 (void) fprintf(stderr, gettext("use the form '%s "
2039 "<pool | id> <newpool>' to give it a new name\n"),
2042 } else if (pools == NULL && idata.exists) {
2043 (void) fprintf(stderr, gettext("cannot import '%s': "
2044 "a pool with that name is already created/imported,\n"),
2046 (void) fprintf(stderr, gettext("and no additional pools "
2047 "with that name were found\n"));
2049 } else if (pools == NULL) {
2051 (void) fprintf(stderr, gettext("cannot import '%s': "
2052 "no such pool available\n"), argv[0]);
2058 if (searchdirs != NULL)
2062 nvlist_free(policy);
2067 * At this point we have a list of import candidate configs. Even if
2068 * we were searching by pool name or guid, we still need to
2069 * post-process the list to deal with pool state and possible
2075 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2077 verify(nvpair_value_nvlist(elem, &config) == 0);
2079 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2081 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2083 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2086 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2093 (void) printf("\n");
2096 err |= do_import(config, NULL, mntopts,
2099 show_import(config);
2101 } else if (searchname != NULL) {
2105 * We are searching for a pool based on name.
2107 verify(nvlist_lookup_string(config,
2108 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2110 if (strcmp(name, searchname) == 0) {
2111 if (found_config != NULL) {
2112 (void) fprintf(stderr, gettext(
2113 "cannot import '%s': more than "
2114 "one matching pool\n"), searchname);
2115 (void) fprintf(stderr, gettext(
2116 "import by numeric ID instead\n"));
2119 found_config = config;
2125 * Search for a pool by guid.
2127 verify(nvlist_lookup_uint64(config,
2128 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2130 if (guid == searchguid)
2131 found_config = config;
2136 * If we were searching for a specific pool, verify that we found a
2137 * pool, and then do the import.
2139 if (argc != 0 && err == 0) {
2140 if (found_config == NULL) {
2141 (void) fprintf(stderr, gettext("cannot import '%s': "
2142 "no such pool available\n"), argv[0]);
2145 err |= do_import(found_config, argc == 1 ? NULL :
2146 argv[1], mntopts, props, flags);
2151 * If we were just looking for pools, report an error if none were
2154 if (argc == 0 && first)
2155 (void) fprintf(stderr,
2156 gettext("no pools available to import\n"));
2161 nvlist_free(policy);
2162 if (searchdirs != NULL)
2167 return (err ? 1 : 0);
2170 typedef struct iostat_cbdata {
2171 boolean_t cb_verbose;
2174 zpool_list_t *cb_list;
2178 print_iostat_separator(iostat_cbdata_t *cb)
2182 for (i = 0; i < cb->cb_namewidth; i++)
2184 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2188 print_iostat_header(iostat_cbdata_t *cb)
2190 (void) printf("%*s capacity operations bandwidth\n",
2191 cb->cb_namewidth, "");
2192 (void) printf("%-*s alloc free read write read write\n",
2193 cb->cb_namewidth, "pool");
2194 print_iostat_separator(cb);
2198 * Display a single statistic.
2201 print_one_stat(uint64_t value)
2205 zfs_nicenum(value, buf, sizeof (buf));
2206 (void) printf(" %5s", buf);
2210 * Print out all the statistics for the given vdev. This can either be the
2211 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2212 * is a verbose output, and we don't want to display the toplevel pool stats.
2215 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2216 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2218 nvlist_t **oldchild, **newchild;
2220 vdev_stat_t *oldvs, *newvs;
2221 vdev_stat_t zerovs = { 0 };
2226 if (oldnv != NULL) {
2227 verify(nvlist_lookup_uint64_array(oldnv,
2228 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2233 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2234 (uint64_t **)&newvs, &c) == 0);
2236 if (strlen(name) + depth > cb->cb_namewidth)
2237 (void) printf("%*s%s", depth, "", name);
2239 (void) printf("%*s%s%*s", depth, "", name,
2240 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2242 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2247 scale = (double)NANOSEC / tdelta;
2249 /* only toplevel vdevs have capacity stats */
2250 if (newvs->vs_space == 0) {
2251 (void) printf(" - -");
2253 print_one_stat(newvs->vs_alloc);
2254 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2257 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2258 oldvs->vs_ops[ZIO_TYPE_READ])));
2260 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2261 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2263 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2264 oldvs->vs_bytes[ZIO_TYPE_READ])));
2266 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2267 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2269 (void) printf("\n");
2271 if (!cb->cb_verbose)
2274 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2275 &newchild, &children) != 0)
2278 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2279 &oldchild, &c) != 0)
2282 for (c = 0; c < children; c++) {
2283 uint64_t ishole = B_FALSE, islog = B_FALSE;
2285 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2288 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2291 if (ishole || islog)
2294 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2295 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2296 newchild[c], cb, depth + 2);
2301 * Log device section
2304 if (num_logs(newnv) > 0) {
2305 (void) printf("%-*s - - - - - "
2306 "-\n", cb->cb_namewidth, "logs");
2308 for (c = 0; c < children; c++) {
2309 uint64_t islog = B_FALSE;
2310 (void) nvlist_lookup_uint64(newchild[c],
2311 ZPOOL_CONFIG_IS_LOG, &islog);
2314 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2316 print_vdev_stats(zhp, vname, oldnv ?
2317 oldchild[c] : NULL, newchild[c],
2326 * Include level 2 ARC devices in iostat output
2328 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2329 &newchild, &children) != 0)
2332 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2333 &oldchild, &c) != 0)
2337 (void) printf("%-*s - - - - - "
2338 "-\n", cb->cb_namewidth, "cache");
2339 for (c = 0; c < children; c++) {
2340 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2342 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2343 newchild[c], cb, depth + 2);
2350 refresh_iostat(zpool_handle_t *zhp, void *data)
2352 iostat_cbdata_t *cb = data;
2356 * If the pool has disappeared, remove it from the list and continue.
2358 if (zpool_refresh_stats(zhp, &missing) != 0)
2362 pool_list_remove(cb->cb_list, zhp);
2368 * Callback to print out the iostats for the given pool.
2371 print_iostat(zpool_handle_t *zhp, void *data)
2373 iostat_cbdata_t *cb = data;
2374 nvlist_t *oldconfig, *newconfig;
2375 nvlist_t *oldnvroot, *newnvroot;
2377 newconfig = zpool_get_config(zhp, &oldconfig);
2379 if (cb->cb_iteration == 1)
2382 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2385 if (oldconfig == NULL)
2388 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2392 * Print out the statistics for the pool.
2394 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2397 print_iostat_separator(cb);
2409 if (isatty(STDOUT_FILENO)) {
2410 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2412 columns = ws.ws_col;
2421 get_namewidth(zpool_handle_t *zhp, void *data)
2423 iostat_cbdata_t *cb = data;
2424 nvlist_t *config, *nvroot;
2427 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2428 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2430 if (!cb->cb_verbose)
2431 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2433 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2438 * The width must be at least 10, but may be as large as the
2439 * column width - 42 so that we can still fit in one line.
2441 columns = get_columns();
2443 if (cb->cb_namewidth < 10)
2444 cb->cb_namewidth = 10;
2445 if (cb->cb_namewidth > columns - 42)
2446 cb->cb_namewidth = columns - 42;
2452 * Parse the input string, get the 'interval' and 'count' value if there is one.
2455 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2458 unsigned long interval = 0, count = 0;
2462 * Determine if the last argument is an integer or a pool name
2464 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2468 interval = strtoul(argv[argc - 1], &end, 10);
2470 if (*end == '\0' && errno == 0) {
2471 if (interval == 0) {
2472 (void) fprintf(stderr, gettext("interval "
2473 "cannot be zero\n"));
2477 * Ignore the last parameter
2482 * If this is not a valid number, just plow on. The
2483 * user will get a more informative error message later
2491 * If the last argument is also an integer, then we have both a count
2494 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2499 interval = strtoul(argv[argc - 1], &end, 10);
2501 if (*end == '\0' && errno == 0) {
2502 if (interval == 0) {
2503 (void) fprintf(stderr, gettext("interval "
2504 "cannot be zero\n"));
2509 * Ignore the last parameter
2523 get_timestamp_arg(char c)
2526 timestamp_fmt = UDATE;
2528 timestamp_fmt = DDATE;
2534 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2536 * -v Display statistics for individual vdevs
2537 * -T Display a timestamp in date(1) or Unix format
2539 * This command can be tricky because we want to be able to deal with pool
2540 * creation/destruction as well as vdev configuration changes. The bulk of this
2541 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2542 * on pool_list_update() to detect the addition of new pools. Configuration
2543 * changes are all handled within libzfs.
2546 zpool_do_iostat(int argc, char **argv)
2551 unsigned long interval = 0, count = 0;
2553 boolean_t verbose = B_FALSE;
2557 while ((c = getopt(argc, argv, "T:v")) != -1) {
2560 get_timestamp_arg(*optarg);
2566 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2575 get_interval_count(&argc, argv, &interval, &count);
2578 * Construct the list of all interesting pools.
2581 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2584 if (pool_list_count(list) == 0 && argc != 0) {
2585 pool_list_free(list);
2589 if (pool_list_count(list) == 0 && interval == 0) {
2590 pool_list_free(list);
2591 (void) fprintf(stderr, gettext("no pools available\n"));
2596 * Enter the main iostat loop.
2599 cb.cb_verbose = verbose;
2600 cb.cb_iteration = 0;
2601 cb.cb_namewidth = 0;
2604 pool_list_update(list);
2606 if ((npools = pool_list_count(list)) == 0)
2607 (void) fprintf(stderr, gettext("no pools available\n"));
2610 * Refresh all statistics. This is done as an
2611 * explicit step before calculating the maximum name
2612 * width, so that any * configuration changes are
2613 * properly accounted for.
2615 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2619 * Iterate over all pools to determine the maximum width
2620 * for the pool / device name column across all pools.
2622 cb.cb_namewidth = 0;
2623 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2626 if (timestamp_fmt != NODATE)
2627 print_timestamp(timestamp_fmt);
2630 * If it's the first time, or verbose mode, print the
2633 if (++cb.cb_iteration == 1 || verbose)
2634 print_iostat_header(&cb);
2636 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2639 * If there's more than one pool, and we're not in
2640 * verbose mode (which prints a separator for us),
2641 * then print a separator.
2643 if (npools > 1 && !verbose)
2644 print_iostat_separator(&cb);
2647 (void) printf("\n");
2651 * Flush the output so that redirection to a file isn't buffered
2654 (void) fflush(stdout);
2659 if (count != 0 && --count == 0)
2662 (void) sleep(interval);
2665 pool_list_free(list);
2670 typedef struct list_cbdata {
2671 boolean_t cb_verbose;
2673 boolean_t cb_scripted;
2674 zprop_list_t *cb_proplist;
2678 * Given a list of columns to display, output appropriate headers for each one.
2681 print_header(list_cbdata_t *cb)
2683 zprop_list_t *pl = cb->cb_proplist;
2684 char headerbuf[ZPOOL_MAXPROPLEN];
2686 boolean_t first = B_TRUE;
2687 boolean_t right_justify;
2690 for (; pl != NULL; pl = pl->pl_next) {
2691 width = pl->pl_width;
2692 if (first && cb->cb_verbose) {
2694 * Reset the width to accommodate the verbose listing
2697 width = cb->cb_namewidth;
2705 right_justify = B_FALSE;
2706 if (pl->pl_prop != ZPROP_INVAL) {
2707 header = zpool_prop_column_name(pl->pl_prop);
2708 right_justify = zpool_prop_align_right(pl->pl_prop);
2712 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2713 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2714 headerbuf[i] = '\0';
2718 if (pl->pl_next == NULL && !right_justify)
2719 (void) printf("%s", header);
2720 else if (right_justify)
2721 (void) printf("%*s", (int)width, header);
2723 (void) printf("%-*s", (int)width, header);
2726 (void) printf("\n");
2730 * Given a pool and a list of properties, print out all the properties according
2731 * to the described layout.
2734 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2736 zprop_list_t *pl = cb->cb_proplist;
2737 boolean_t first = B_TRUE;
2738 char property[ZPOOL_MAXPROPLEN];
2740 boolean_t right_justify;
2743 for (; pl != NULL; pl = pl->pl_next) {
2745 width = pl->pl_width;
2746 if (first && cb->cb_verbose) {
2748 * Reset the width to accommodate the verbose listing
2751 width = cb->cb_namewidth;
2755 if (cb->cb_scripted)
2756 (void) printf("\t");
2763 right_justify = B_FALSE;
2764 if (pl->pl_prop != ZPROP_INVAL) {
2765 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2766 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2768 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2769 sizeof (property), NULL) != 0)
2774 right_justify = zpool_prop_align_right(pl->pl_prop);
2775 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2776 zpool_prop_unsupported(pl->pl_user_prop)) &&
2777 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2778 sizeof (property)) == 0) {
2786 * If this is being called in scripted mode, or if this is the
2787 * last column and it is left-justified, don't include a width
2790 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2791 (void) printf("%s", propstr);
2792 else if (right_justify)
2793 (void) printf("%*s", (int)width, propstr);
2795 (void) printf("%-*s", (int)width, propstr);
2798 (void) printf("\n");
2802 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2806 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2808 zfs_nicenum(value, propval, sizeof (propval));
2810 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2811 (void) strlcpy(propval, "-", sizeof (propval));
2814 (void) printf("\t%s", propval);
2816 (void) printf(" %*s", (int)width, propval);
2820 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2821 list_cbdata_t *cb, int depth)
2827 boolean_t scripted = cb->cb_scripted;
2829 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2830 (uint64_t **)&vs, &c) == 0);
2834 (void) printf("\t%s", name);
2835 else if (strlen(name) + depth > cb->cb_namewidth)
2836 (void) printf("%*s%s", depth, "", name);
2838 (void) printf("%*s%s%*s", depth, "", name,
2839 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2841 /* only toplevel vdevs have capacity stats */
2842 if (vs->vs_space == 0) {
2844 (void) printf("\t-\t-\t-");
2846 (void) printf(" - - -");
2848 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2850 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2852 print_one_column(ZPOOL_PROP_FREE,
2853 vs->vs_space - vs->vs_alloc, scripted);
2855 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2857 (void) printf("\n");
2860 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2861 &child, &children) != 0)
2864 for (c = 0; c < children; c++) {
2865 uint64_t ishole = B_FALSE;
2867 if (nvlist_lookup_uint64(child[c],
2868 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2871 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2872 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2877 * Include level 2 ARC devices in iostat output
2879 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2880 &child, &children) != 0)
2884 (void) printf("%-*s - - - - - "
2885 "-\n", cb->cb_namewidth, "cache");
2886 for (c = 0; c < children; c++) {
2887 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2889 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2897 * Generic callback function to list a pool.
2900 list_callback(zpool_handle_t *zhp, void *data)
2902 list_cbdata_t *cbp = data;
2906 config = zpool_get_config(zhp, NULL);
2908 print_pool(zhp, cbp);
2909 if (!cbp->cb_verbose)
2912 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2914 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2920 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2922 * -H Scripted mode. Don't display headers, and separate properties
2924 * -o List of properties to display. Defaults to
2925 * "name,size,allocated,free,capacity,health,altroot"
2926 * -T Display a timestamp in date(1) or Unix format
2928 * List all pools in the system, whether or not they're healthy. Output space
2929 * statistics for each one, as well as health status summary.
2932 zpool_do_list(int argc, char **argv)
2936 list_cbdata_t cb = { 0 };
2937 static char default_props[] =
2938 "name,size,allocated,free,capacity,dedupratio,"
2940 char *props = default_props;
2941 unsigned long interval = 0, count = 0;
2943 boolean_t first = B_TRUE;
2946 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2949 cb.cb_scripted = B_TRUE;
2955 get_timestamp_arg(*optarg);
2958 cb.cb_verbose = B_TRUE;
2961 (void) fprintf(stderr, gettext("missing argument for "
2962 "'%c' option\n"), optopt);
2966 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2975 get_interval_count(&argc, argv, &interval, &count);
2977 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2980 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
2983 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2984 (void) printf(gettext("no pools available\n"));
2985 zprop_free_list(cb.cb_proplist);
2990 pool_list_update(list);
2992 if (pool_list_count(list) == 0)
2995 if (timestamp_fmt != NODATE)
2996 print_timestamp(timestamp_fmt);
2998 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3002 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3007 if (count != 0 && --count == 0)
3010 (void) sleep(interval);
3013 zprop_free_list(cb.cb_proplist);
3018 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3020 boolean_t force = B_FALSE;
3023 char *poolname, *old_disk, *new_disk;
3024 zpool_handle_t *zhp;
3025 nvlist_t *props = NULL;
3030 while ((c = getopt(argc, argv, "fo:")) != -1) {
3036 if ((propval = strchr(optarg, '=')) == NULL) {
3037 (void) fprintf(stderr, gettext("missing "
3038 "'=' for -o option\n"));
3044 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3045 (add_prop_list(optarg, propval, &props, B_TRUE)))
3049 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3058 /* get pool name and check number of arguments */
3060 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3067 (void) fprintf(stderr,
3068 gettext("missing <device> specification\n"));
3076 (void) fprintf(stderr,
3077 gettext("missing <new_device> specification\n"));
3080 new_disk = old_disk;
3090 (void) fprintf(stderr, gettext("too many arguments\n"));
3094 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3097 if (zpool_get_config(zhp, NULL) == NULL) {
3098 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3104 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3106 if (nvroot == NULL) {
3111 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3113 nvlist_free(nvroot);
3120 * zpool replace [-f] <pool> <device> <new_device>
3122 * -f Force attach, even if <new_device> appears to be in use.
3124 * Replace <device> with <new_device>.
3128 zpool_do_replace(int argc, char **argv)
3130 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3134 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3136 * -f Force attach, even if <new_device> appears to be in use.
3137 * -o Set property=value.
3139 * Attach <new_device> to the mirror containing <device>. If <device> is not
3140 * part of a mirror, then <device> will be transformed into a mirror of
3141 * <device> and <new_device>. In either case, <new_device> will begin life
3142 * with a DTL of [0, now], and will immediately begin to resilver itself.
3145 zpool_do_attach(int argc, char **argv)
3147 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3151 * zpool detach [-f] <pool> <device>
3153 * -f Force detach of <device>, even if DTLs argue against it
3154 * (not supported yet)
3156 * Detach a device from a mirror. The operation will be refused if <device>
3157 * is the last device in the mirror, or if the DTLs indicate that this device
3158 * has the only valid copy of some data.
3162 zpool_do_detach(int argc, char **argv)
3165 char *poolname, *path;
3166 zpool_handle_t *zhp;
3170 while ((c = getopt(argc, argv, "f")) != -1) {
3174 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3183 /* get pool name and check number of arguments */
3185 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3190 (void) fprintf(stderr,
3191 gettext("missing <device> specification\n"));
3198 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3201 ret = zpool_vdev_detach(zhp, path);
3209 * zpool split [-n] [-o prop=val] ...
3211 * [-R altroot] <pool> <newpool> [<device> ...]
3213 * -n Do not split the pool, but display the resulting layout if
3214 * it were to be split.
3215 * -o Set property=value, or set mount options.
3216 * -R Mount the split-off pool under an alternate root.
3218 * Splits the named pool and gives it the new pool name. Devices to be split
3219 * off may be listed, provided that no more than one device is specified
3220 * per top-level vdev mirror. The newly split pool is left in an exported
3221 * state unless -R is specified.
3223 * Restrictions: the top-level of the pool pool must only be made up of
3224 * mirrors; all devices in the pool must be healthy; no device may be
3225 * undergoing a resilvering operation.
3228 zpool_do_split(int argc, char **argv)
3230 char *srcpool, *newpool, *propval;
3231 char *mntopts = NULL;
3234 zpool_handle_t *zhp;
3235 nvlist_t *config, *props = NULL;
3237 flags.dryrun = B_FALSE;
3238 flags.import = B_FALSE;
3241 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3244 flags.import = B_TRUE;
3246 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3247 &props, B_TRUE) != 0) {
3254 flags.dryrun = B_TRUE;
3257 if ((propval = strchr(optarg, '=')) != NULL) {
3260 if (add_prop_list(optarg, propval,
3261 &props, B_TRUE) != 0) {
3271 (void) fprintf(stderr, gettext("missing argument for "
3272 "'%c' option\n"), optopt);
3276 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3283 if (!flags.import && mntopts != NULL) {
3284 (void) fprintf(stderr, gettext("setting mntopts is only "
3285 "valid when importing the pool\n"));
3293 (void) fprintf(stderr, gettext("Missing pool name\n"));
3297 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3307 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3310 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3311 if (config == NULL) {
3315 (void) printf(gettext("would create '%s' with the "
3316 "following layout:\n\n"), newpool);
3317 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3319 nvlist_free(config);
3324 if (ret != 0 || flags.dryrun || !flags.import)
3328 * The split was successful. Now we need to open the new
3329 * pool and import it.
3331 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3333 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3334 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3336 (void) fprintf(stderr, gettext("Split was successful, but "
3337 "the datasets could not all be mounted\n"));
3338 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3339 "different altroot\n"), "zpool import");
3349 * zpool online <pool> <device> ...
3352 zpool_do_online(int argc, char **argv)
3356 zpool_handle_t *zhp;
3358 vdev_state_t newstate;
3362 while ((c = getopt(argc, argv, "et")) != -1) {
3365 flags |= ZFS_ONLINE_EXPAND;
3369 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3378 /* get pool name and check number of arguments */
3380 (void) fprintf(stderr, gettext("missing pool name\n"));
3384 (void) fprintf(stderr, gettext("missing device name\n"));
3390 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3393 for (i = 1; i < argc; i++) {
3394 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3395 if (newstate != VDEV_STATE_HEALTHY) {
3396 (void) printf(gettext("warning: device '%s' "
3397 "onlined, but remains in faulted state\n"),
3399 if (newstate == VDEV_STATE_FAULTED)
3400 (void) printf(gettext("use 'zpool "
3401 "clear' to restore a faulted "
3404 (void) printf(gettext("use 'zpool "
3405 "replace' to replace devices "
3406 "that are no longer present\n"));
3419 * zpool offline [-ft] <pool> <device> ...
3421 * -f Force the device into the offline state, even if doing
3422 * so would appear to compromise pool availability.
3423 * (not supported yet)
3425 * -t Only take the device off-line temporarily. The offline
3426 * state will not be persistent across reboots.
3430 zpool_do_offline(int argc, char **argv)
3434 zpool_handle_t *zhp;
3436 boolean_t istmp = B_FALSE;
3439 while ((c = getopt(argc, argv, "ft")) != -1) {
3446 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3455 /* get pool name and check number of arguments */
3457 (void) fprintf(stderr, gettext("missing pool name\n"));
3461 (void) fprintf(stderr, gettext("missing device name\n"));
3467 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3470 for (i = 1; i < argc; i++) {
3471 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3481 * zpool clear <pool> [device]
3483 * Clear all errors associated with a pool or a particular device.
3486 zpool_do_clear(int argc, char **argv)
3490 boolean_t dryrun = B_FALSE;
3491 boolean_t do_rewind = B_FALSE;
3492 boolean_t xtreme_rewind = B_FALSE;
3493 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3494 nvlist_t *policy = NULL;
3495 zpool_handle_t *zhp;
3496 char *pool, *device;
3499 while ((c = getopt(argc, argv, "FnX")) != -1) {
3508 xtreme_rewind = B_TRUE;
3511 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3521 (void) fprintf(stderr, gettext("missing pool name\n"));
3526 (void) fprintf(stderr, gettext("too many arguments\n"));
3530 if ((dryrun || xtreme_rewind) && !do_rewind) {
3531 (void) fprintf(stderr,
3532 gettext("-n or -X only meaningful with -F\n"));
3536 rewind_policy = ZPOOL_TRY_REWIND;
3538 rewind_policy = ZPOOL_DO_REWIND;
3540 rewind_policy |= ZPOOL_EXTREME_REWIND;
3542 /* In future, further rewind policy choices can be passed along here */
3543 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3544 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3548 device = argc == 2 ? argv[1] : NULL;
3550 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3551 nvlist_free(policy);
3555 if (zpool_clear(zhp, device, policy) != 0)
3560 nvlist_free(policy);
3566 * zpool reguid <pool>
3569 zpool_do_reguid(int argc, char **argv)
3573 zpool_handle_t *zhp;
3577 while ((c = getopt(argc, argv, "")) != -1) {
3580 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3589 /* get pool name and check number of arguments */
3591 (void) fprintf(stderr, gettext("missing pool name\n"));
3596 (void) fprintf(stderr, gettext("too many arguments\n"));
3601 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3604 ret = zpool_reguid(zhp);
3612 * zpool reopen <pool>
3614 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3616 * NOTE: This command is currently undocumented. If the command is ever
3617 * exposed then the appropriate usage() messages will need to be made.
3620 zpool_do_reopen(int argc, char **argv)
3623 zpool_handle_t *zhp;
3633 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3636 ret = zpool_reopen(zhp);
3641 typedef struct scrub_cbdata {
3648 scrub_callback(zpool_handle_t *zhp, void *data)
3650 scrub_cbdata_t *cb = data;
3654 * Ignore faulted pools.
3656 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3657 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3658 "currently unavailable\n"), zpool_get_name(zhp));
3662 err = zpool_scan(zhp, cb->cb_type);
3668 * zpool scrub [-s] <pool> ...
3670 * -s Stop. Stops any in-progress scrub.
3673 zpool_do_scrub(int argc, char **argv)
3678 cb.cb_type = POOL_SCAN_SCRUB;
3681 while ((c = getopt(argc, argv, "s")) != -1) {
3684 cb.cb_type = POOL_SCAN_NONE;
3687 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3699 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3703 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3706 typedef struct status_cbdata {
3708 boolean_t cb_allpools;
3709 boolean_t cb_verbose;
3710 boolean_t cb_explain;
3712 boolean_t cb_dedup_stats;
3716 * Print out detailed scrub status.
3719 print_scan_status(pool_scan_stat_t *ps)
3722 uint64_t elapsed, mins_left, hours_left;
3723 uint64_t pass_exam, examined, total;
3725 double fraction_done;
3726 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3728 (void) printf(gettext(" scan: "));
3730 /* If there's never been a scan, there's not much to say. */
3731 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3732 ps->pss_func >= POOL_SCAN_FUNCS) {
3733 (void) printf(gettext("none requested\n"));
3737 start = ps->pss_start_time;
3738 end = ps->pss_end_time;
3739 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3741 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3742 ps->pss_func == POOL_SCAN_RESILVER);
3744 * Scan is finished or canceled.
3746 if (ps->pss_state == DSS_FINISHED) {
3747 uint64_t minutes_taken = (end - start) / 60;
3750 if (ps->pss_func == POOL_SCAN_SCRUB) {
3751 fmt = gettext("scrub repaired %s in %lluh%um with "
3752 "%llu errors on %s");
3753 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3754 fmt = gettext("resilvered %s in %lluh%um with "
3755 "%llu errors on %s");
3758 (void) printf(fmt, processed_buf,
3759 (u_longlong_t)(minutes_taken / 60),
3760 (uint_t)(minutes_taken % 60),
3761 (u_longlong_t)ps->pss_errors,
3762 ctime((time_t *)&end));
3764 } else if (ps->pss_state == DSS_CANCELED) {
3765 if (ps->pss_func == POOL_SCAN_SCRUB) {
3766 (void) printf(gettext("scrub canceled on %s"),
3768 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3769 (void) printf(gettext("resilver canceled on %s"),
3775 assert(ps->pss_state == DSS_SCANNING);
3778 * Scan is in progress.
3780 if (ps->pss_func == POOL_SCAN_SCRUB) {
3781 (void) printf(gettext("scrub in progress since %s"),
3783 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3784 (void) printf(gettext("resilver in progress since %s"),
3788 examined = ps->pss_examined ? ps->pss_examined : 1;
3789 total = ps->pss_to_examine;
3790 fraction_done = (double)examined / total;
3792 /* elapsed time for this pass */
3793 elapsed = time(NULL) - ps->pss_pass_start;
3794 elapsed = elapsed ? elapsed : 1;
3795 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3796 rate = pass_exam / elapsed;
3797 rate = rate ? rate : 1;
3798 mins_left = ((total - examined) / rate) / 60;
3799 hours_left = mins_left / 60;
3801 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3802 zfs_nicenum(total, total_buf, sizeof (total_buf));
3803 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3806 * do not print estimated time if hours_left is more than 30 days
3808 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3809 examined_buf, total_buf, rate_buf);
3810 if (hours_left < (30 * 24)) {
3811 (void) printf(gettext(", %lluh%um to go\n"),
3812 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3814 (void) printf(gettext(
3815 ", (scan is slow, no estimated time)\n"));
3818 if (ps->pss_func == POOL_SCAN_RESILVER) {
3819 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3820 processed_buf, 100 * fraction_done);
3821 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3822 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3823 processed_buf, 100 * fraction_done);
3828 print_error_log(zpool_handle_t *zhp)
3830 nvlist_t *nverrlist = NULL;
3833 size_t len = MAXPATHLEN * 2;
3835 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3836 (void) printf("errors: List of errors unavailable "
3837 "(insufficient privileges)\n");
3841 (void) printf("errors: Permanent errors have been "
3842 "detected in the following files:\n\n");
3844 pathname = safe_malloc(len);
3846 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3848 uint64_t dsobj, obj;
3850 verify(nvpair_value_nvlist(elem, &nv) == 0);
3851 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3853 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3855 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3856 (void) printf("%7s %s\n", "", pathname);
3859 nvlist_free(nverrlist);
3863 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3872 (void) printf(gettext("\tspares\n"));
3874 for (i = 0; i < nspares; i++) {
3875 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3876 print_status_config(zhp, name, spares[i],
3877 namewidth, 2, B_TRUE);
3883 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3892 (void) printf(gettext("\tcache\n"));
3894 for (i = 0; i < nl2cache; i++) {
3895 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3896 print_status_config(zhp, name, l2cache[i],
3897 namewidth, 2, B_FALSE);
3903 print_dedup_stats(nvlist_t *config)
3905 ddt_histogram_t *ddh;
3911 * If the pool was faulted then we may not have been able to
3912 * obtain the config. Otherwise, if we have anything in the dedup
3913 * table continue processing the stats.
3915 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3916 (uint64_t **)&ddo, &c) != 0)
3919 (void) printf("\n");
3920 (void) printf(gettext(" dedup: "));
3921 if (ddo->ddo_count == 0) {
3922 (void) printf(gettext("no DDT entries\n"));
3926 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3927 (u_longlong_t)ddo->ddo_count,
3928 (u_longlong_t)ddo->ddo_dspace,
3929 (u_longlong_t)ddo->ddo_mspace);
3931 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3932 (uint64_t **)&dds, &c) == 0);
3933 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3934 (uint64_t **)&ddh, &c) == 0);
3935 zpool_dump_ddt(dds, ddh);
3939 * Display a summary of pool status. Displays a summary such as:
3943 * reason: One or more devices ...
3944 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
3950 * When given the '-v' option, we print out the complete config. If the '-e'
3951 * option is specified, then we print out error rate information as well.
3954 status_callback(zpool_handle_t *zhp, void *data)
3956 status_cbdata_t *cbp = data;
3957 nvlist_t *config, *nvroot;
3964 config = zpool_get_config(zhp, NULL);
3965 reason = zpool_get_status(zhp, &msgid);
3970 * If we were given 'zpool status -x', only report those pools with
3973 if (cbp->cb_explain &&
3974 (reason == ZPOOL_STATUS_OK ||
3975 reason == ZPOOL_STATUS_VERSION_OLDER ||
3976 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
3977 if (!cbp->cb_allpools) {
3978 (void) printf(gettext("pool '%s' is healthy\n"),
3979 zpool_get_name(zhp));
3981 cbp->cb_first = B_FALSE;
3987 cbp->cb_first = B_FALSE;
3989 (void) printf("\n");
3991 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3993 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3994 (uint64_t **)&vs, &c) == 0);
3995 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3997 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3998 (void) printf(gettext(" state: %s\n"), health);
4001 case ZPOOL_STATUS_MISSING_DEV_R:
4002 (void) printf(gettext("status: One or more devices could not "
4003 "be opened. Sufficient replicas exist for\n\tthe pool to "
4004 "continue functioning in a degraded state.\n"));
4005 (void) printf(gettext("action: Attach the missing device and "
4006 "online it using 'zpool online'.\n"));
4009 case ZPOOL_STATUS_MISSING_DEV_NR:
4010 (void) printf(gettext("status: One or more devices could not "
4011 "be opened. There are insufficient\n\treplicas for the "
4012 "pool to continue functioning.\n"));
4013 (void) printf(gettext("action: Attach the missing device and "
4014 "online it using 'zpool online'.\n"));
4017 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4018 (void) printf(gettext("status: One or more devices could not "
4019 "be used because the label is missing or\n\tinvalid. "
4020 "Sufficient replicas exist for the pool to continue\n\t"
4021 "functioning in a degraded state.\n"));
4022 (void) printf(gettext("action: Replace the device using "
4023 "'zpool replace'.\n"));
4026 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4027 (void) printf(gettext("status: One or more devices could not "
4028 "be used because the label is missing \n\tor invalid. "
4029 "There are insufficient replicas for the pool to "
4030 "continue\n\tfunctioning.\n"));
4031 zpool_explain_recover(zpool_get_handle(zhp),
4032 zpool_get_name(zhp), reason, config);
4035 case ZPOOL_STATUS_FAILING_DEV:
4036 (void) printf(gettext("status: One or more devices has "
4037 "experienced an unrecoverable error. An\n\tattempt was "
4038 "made to correct the error. Applications are "
4040 (void) printf(gettext("action: Determine if the device needs "
4041 "to be replaced, and clear the errors\n\tusing "
4042 "'zpool clear' or replace the device with 'zpool "
4046 case ZPOOL_STATUS_OFFLINE_DEV:
4047 (void) printf(gettext("status: One or more devices has "
4048 "been taken offline by the administrator.\n\tSufficient "
4049 "replicas exist for the pool to continue functioning in "
4050 "a\n\tdegraded state.\n"));
4051 (void) printf(gettext("action: Online the device using "
4052 "'zpool online' or replace the device with\n\t'zpool "
4056 case ZPOOL_STATUS_REMOVED_DEV:
4057 (void) printf(gettext("status: One or more devices has "
4058 "been removed by the administrator.\n\tSufficient "
4059 "replicas exist for the pool to continue functioning in "
4060 "a\n\tdegraded state.\n"));
4061 (void) printf(gettext("action: Online the device using "
4062 "'zpool online' or replace the device with\n\t'zpool "
4066 case ZPOOL_STATUS_RESILVERING:
4067 (void) printf(gettext("status: One or more devices is "
4068 "currently being resilvered. The pool will\n\tcontinue "
4069 "to function, possibly in a degraded state.\n"));
4070 (void) printf(gettext("action: Wait for the resilver to "
4074 case ZPOOL_STATUS_CORRUPT_DATA:
4075 (void) printf(gettext("status: One or more devices has "
4076 "experienced an error resulting in data\n\tcorruption. "
4077 "Applications may be affected.\n"));
4078 (void) printf(gettext("action: Restore the file in question "
4079 "if possible. Otherwise restore the\n\tentire pool from "
4083 case ZPOOL_STATUS_CORRUPT_POOL:
4084 (void) printf(gettext("status: The pool metadata is corrupted "
4085 "and the pool cannot be opened.\n"));
4086 zpool_explain_recover(zpool_get_handle(zhp),
4087 zpool_get_name(zhp), reason, config);
4090 case ZPOOL_STATUS_VERSION_OLDER:
4091 (void) printf(gettext("status: The pool is formatted using a "
4092 "legacy on-disk format. The pool can\n\tstill be used, "
4093 "but some features are unavailable.\n"));
4094 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4095 "upgrade'. Once this is done, the\n\tpool will no longer "
4096 "be accessible on software that does not support\n\t"
4097 "feature flags.\n"));
4100 case ZPOOL_STATUS_VERSION_NEWER:
4101 (void) printf(gettext("status: The pool has been upgraded to a "
4102 "newer, incompatible on-disk version.\n\tThe pool cannot "
4103 "be accessed on this system.\n"));
4104 (void) printf(gettext("action: Access the pool from a system "
4105 "running more recent software, or\n\trestore the pool from "
4109 case ZPOOL_STATUS_FEAT_DISABLED:
4110 (void) printf(gettext("status: Some supported features are not "
4111 "enabled on the pool. The pool can\n\tstill be used, but "
4112 "some features are unavailable.\n"));
4113 (void) printf(gettext("action: Enable all features using "
4114 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4115 "longer be accessible by software that does not support\n\t"
4116 "the features. See zpool-features(5) for details.\n"));
4119 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4120 (void) printf(gettext("status: The pool cannot be accessed on "
4121 "this system because it uses the\n\tfollowing feature(s) "
4122 "not supported on this system:\n"));
4123 zpool_print_unsup_feat(config);
4124 (void) printf("\n");
4125 (void) printf(gettext("action: Access the pool from a system "
4126 "that supports the required feature(s),\n\tor restore the "
4127 "pool from backup.\n"));
4130 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4131 (void) printf(gettext("status: The pool can only be accessed "
4132 "in read-only mode on this system. It\n\tcannot be "
4133 "accessed in read-write mode because it uses the "
4134 "following\n\tfeature(s) not supported on this system:\n"));
4135 zpool_print_unsup_feat(config);
4136 (void) printf("\n");
4137 (void) printf(gettext("action: The pool cannot be accessed in "
4138 "read-write mode. Import the pool with\n"
4139 "\t\"-o readonly=on\", access the pool from a system that "
4140 "supports the\n\trequired feature(s), or restore the "
4141 "pool from backup.\n"));
4144 case ZPOOL_STATUS_FAULTED_DEV_R:
4145 (void) printf(gettext("status: One or more devices are "
4146 "faulted in response to persistent errors.\n\tSufficient "
4147 "replicas exist for the pool to continue functioning "
4148 "in a\n\tdegraded state.\n"));
4149 (void) printf(gettext("action: Replace the faulted device, "
4150 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4153 case ZPOOL_STATUS_FAULTED_DEV_NR:
4154 (void) printf(gettext("status: One or more devices are "
4155 "faulted in response to persistent errors. There are "
4156 "insufficient replicas for the pool to\n\tcontinue "
4158 (void) printf(gettext("action: Destroy and re-create the pool "
4159 "from a backup source. Manually marking the device\n"
4160 "\trepaired using 'zpool clear' may allow some data "
4161 "to be recovered.\n"));
4164 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4165 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4166 (void) printf(gettext("status: One or more devices are "
4167 "faulted in response to IO failures.\n"));
4168 (void) printf(gettext("action: Make sure the affected devices "
4169 "are connected, then run 'zpool clear'.\n"));
4172 case ZPOOL_STATUS_BAD_LOG:
4173 (void) printf(gettext("status: An intent log record "
4174 "could not be read.\n"
4175 "\tWaiting for adminstrator intervention to fix the "
4176 "faulted pool.\n"));
4177 (void) printf(gettext("action: Either restore the affected "
4178 "device(s) and run 'zpool online',\n"
4179 "\tor ignore the intent log records by running "
4180 "'zpool clear'.\n"));
4185 * The remaining errors can't actually be generated, yet.
4187 assert(reason == ZPOOL_STATUS_OK);
4191 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4194 if (config != NULL) {
4197 nvlist_t **spares, **l2cache;
4198 uint_t nspares, nl2cache;
4199 pool_scan_stat_t *ps = NULL;
4201 (void) nvlist_lookup_uint64_array(nvroot,
4202 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4203 print_scan_status(ps);
4205 namewidth = max_width(zhp, nvroot, 0, 0);
4209 (void) printf(gettext("config:\n\n"));
4210 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4211 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4212 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4213 namewidth, 0, B_FALSE);
4215 if (num_logs(nvroot) > 0)
4216 print_logs(zhp, nvroot, namewidth, B_TRUE);
4217 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4218 &l2cache, &nl2cache) == 0)
4219 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4221 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4222 &spares, &nspares) == 0)
4223 print_spares(zhp, spares, nspares, namewidth);
4225 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4227 nvlist_t *nverrlist = NULL;
4230 * If the approximate error count is small, get a
4231 * precise count by fetching the entire log and
4232 * uniquifying the results.
4234 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4235 zpool_get_errlog(zhp, &nverrlist) == 0) {
4240 while ((elem = nvlist_next_nvpair(nverrlist,
4245 nvlist_free(nverrlist);
4247 (void) printf("\n");
4250 (void) printf(gettext("errors: No known data "
4252 else if (!cbp->cb_verbose)
4253 (void) printf(gettext("errors: %llu data "
4254 "errors, use '-v' for a list\n"),
4255 (u_longlong_t)nerr);
4257 print_error_log(zhp);
4260 if (cbp->cb_dedup_stats)
4261 print_dedup_stats(config);
4263 (void) printf(gettext("config: The configuration cannot be "
4271 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4273 * -v Display complete error logs
4274 * -x Display only pools with potential problems
4275 * -D Display dedup status (undocumented)
4276 * -T Display a timestamp in date(1) or Unix format
4278 * Describes the health status of all pools or some subset.
4281 zpool_do_status(int argc, char **argv)
4285 unsigned long interval = 0, count = 0;
4286 status_cbdata_t cb = { 0 };
4289 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4292 cb.cb_verbose = B_TRUE;
4295 cb.cb_explain = B_TRUE;
4298 cb.cb_dedup_stats = B_TRUE;
4301 get_timestamp_arg(*optarg);
4304 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4313 get_interval_count(&argc, argv, &interval, &count);
4316 cb.cb_allpools = B_TRUE;
4318 cb.cb_first = B_TRUE;
4321 if (timestamp_fmt != NODATE)
4322 print_timestamp(timestamp_fmt);
4324 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4325 status_callback, &cb);
4327 if (argc == 0 && cb.cb_count == 0)
4328 (void) fprintf(stderr, gettext("no pools available\n"));
4329 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4330 (void) printf(gettext("all pools are healthy\n"));
4338 if (count != 0 && --count == 0)
4341 (void) sleep(interval);
4347 typedef struct upgrade_cbdata {
4350 uint64_t cb_version;
4355 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4359 uint64_t oldversion;
4361 config = zpool_get_config(zhp, NULL);
4362 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4365 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4366 assert(oldversion < version);
4368 ret = zpool_upgrade(zhp, version);
4372 if (version >= SPA_VERSION_FEATURES) {
4373 (void) printf(gettext("Successfully upgraded "
4374 "'%s' from version %llu to feature flags.\n"),
4375 zpool_get_name(zhp), (u_longlong_t) oldversion);
4377 (void) printf(gettext("Successfully upgraded "
4378 "'%s' from version %llu to version %llu.\n"),
4379 zpool_get_name(zhp), (u_longlong_t) oldversion,
4380 (u_longlong_t) version);
4387 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4390 boolean_t firstff = B_TRUE;
4391 nvlist_t *enabled = zpool_get_features(zhp);
4394 for (i = 0; i < SPA_FEATURES; i++) {
4395 const char *fname = spa_feature_table[i].fi_uname;
4396 const char *fguid = spa_feature_table[i].fi_guid;
4397 if (!nvlist_exists(enabled, fguid)) {
4399 verify(-1 != asprintf(&propname, "feature@%s", fname));
4400 ret = zpool_set_prop(zhp, propname,
4401 ZFS_FEATURE_ENABLED);
4409 (void) printf(gettext("Enabled the "
4410 "following features on '%s':\n"),
4411 zpool_get_name(zhp));
4414 (void) printf(gettext(" %s\n"), fname);
4425 upgrade_cb(zpool_handle_t *zhp, void *arg)
4427 upgrade_cbdata_t *cbp = arg;
4430 boolean_t printnl = B_FALSE;
4433 config = zpool_get_config(zhp, NULL);
4434 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4437 assert(SPA_VERSION_IS_SUPPORTED(version));
4439 if (version < cbp->cb_version) {
4440 cbp->cb_first = B_FALSE;
4441 ret = upgrade_version(zhp, cbp->cb_version);
4448 * XXX: This code can be enabled when Illumos commit
4449 * 4445fffbbb1ea25fd0e9ea68b9380dd7a6709025 is merged.
4450 * It reworks the history logging among other things.
4454 * If they did "zpool upgrade -a", then we could
4455 * be doing ioctls to different pools. We need
4456 * to log this history once to each pool, and bypass
4457 * the normal history logging that happens in main().
4459 (void) zpool_log_history(g_zfs, history_str);
4460 log_history = B_FALSE;
4464 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4466 ret = upgrade_enable_all(zhp, &count);
4471 cbp->cb_first = B_FALSE;
4477 (void) printf(gettext("\n"));
4484 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4486 upgrade_cbdata_t *cbp = arg;
4490 config = zpool_get_config(zhp, NULL);
4491 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4494 assert(SPA_VERSION_IS_SUPPORTED(version));
4496 if (version < SPA_VERSION_FEATURES) {
4497 if (cbp->cb_first) {
4498 (void) printf(gettext("The following pools are "
4499 "formatted with legacy version numbers and can\n"
4500 "be upgraded to use feature flags. After "
4501 "being upgraded, these pools\nwill no "
4502 "longer be accessible by software that does not "
4503 "support feature\nflags.\n\n"));
4504 (void) printf(gettext("VER POOL\n"));
4505 (void) printf(gettext("--- ------------\n"));
4506 cbp->cb_first = B_FALSE;
4509 (void) printf("%2llu %s\n", (u_longlong_t)version,
4510 zpool_get_name(zhp));
4517 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4519 upgrade_cbdata_t *cbp = arg;
4523 config = zpool_get_config(zhp, NULL);
4524 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4527 if (version >= SPA_VERSION_FEATURES) {
4529 boolean_t poolfirst = B_TRUE;
4530 nvlist_t *enabled = zpool_get_features(zhp);
4532 for (i = 0; i < SPA_FEATURES; i++) {
4533 const char *fguid = spa_feature_table[i].fi_guid;
4534 const char *fname = spa_feature_table[i].fi_uname;
4535 if (!nvlist_exists(enabled, fguid)) {
4536 if (cbp->cb_first) {
4537 (void) printf(gettext("\nSome "
4538 "supported features are not "
4539 "enabled on the following pools. "
4540 "Once a\nfeature is enabled the "
4541 "pool may become incompatible with "
4542 "software\nthat does not support "
4544 "zpool-features(5) for "
4546 (void) printf(gettext("POOL "
4548 (void) printf(gettext("------"
4550 cbp->cb_first = B_FALSE;
4554 (void) printf(gettext("%s\n"),
4555 zpool_get_name(zhp));
4556 poolfirst = B_FALSE;
4559 (void) printf(gettext(" %s\n"), fname);
4569 upgrade_one(zpool_handle_t *zhp, void *data)
4571 boolean_t printnl = B_FALSE;
4572 upgrade_cbdata_t *cbp = data;
4573 uint64_t cur_version;
4576 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4577 (void) printf(gettext("'log' is now a reserved word\n"
4578 "Pool 'log' must be renamed using export and import"
4583 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4584 if (cur_version > cbp->cb_version) {
4585 (void) printf(gettext("Pool '%s' is already formatted "
4586 "using more current version '%llu'.\n\n"),
4587 zpool_get_name(zhp), (u_longlong_t) cur_version);
4591 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4592 (void) printf(gettext("Pool '%s' is already formatted "
4593 "using version %llu.\n\n"), zpool_get_name(zhp),
4594 (u_longlong_t) cbp->cb_version);
4598 if (cur_version != cbp->cb_version) {
4600 ret = upgrade_version(zhp, cbp->cb_version);
4605 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4607 ret = upgrade_enable_all(zhp, &count);
4613 } else if (cur_version == SPA_VERSION) {
4614 (void) printf(gettext("Pool '%s' already has all "
4615 "supported features enabled.\n"),
4616 zpool_get_name(zhp));
4621 (void) printf(gettext("\n"));
4630 * zpool upgrade [-V version] <-a | pool ...>
4632 * With no arguments, display downrev'd ZFS pool available for upgrade.
4633 * Individual pools can be upgraded by specifying the pool, and '-a' will
4634 * upgrade all pools.
4637 zpool_do_upgrade(int argc, char **argv)
4640 upgrade_cbdata_t cb = { 0 };
4642 boolean_t showversions = B_FALSE;
4643 boolean_t upgradeall = B_FALSE;
4648 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4651 upgradeall = B_TRUE;
4654 showversions = B_TRUE;
4657 cb.cb_version = strtoll(optarg, &end, 10);
4659 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4660 (void) fprintf(stderr,
4661 gettext("invalid version '%s'\n"), optarg);
4666 (void) fprintf(stderr, gettext("missing argument for "
4667 "'%c' option\n"), optopt);
4671 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4682 if (cb.cb_version == 0) {
4683 cb.cb_version = SPA_VERSION;
4684 } else if (!upgradeall && argc == 0) {
4685 (void) fprintf(stderr, gettext("-V option is "
4686 "incompatible with other arguments\n"));
4691 if (upgradeall || argc != 0) {
4692 (void) fprintf(stderr, gettext("-v option is "
4693 "incompatible with other arguments\n"));
4696 } else if (upgradeall) {
4698 (void) fprintf(stderr, gettext("-a option should not "
4699 "be used along with a pool name\n"));
4704 (void) printf(gettext("This system supports ZFS pool feature "
4709 (void) printf(gettext("The following features are "
4711 (void) printf(gettext("FEAT DESCRIPTION\n"));
4712 (void) printf("----------------------------------------------"
4713 "---------------\n");
4714 for (i = 0; i < SPA_FEATURES; i++) {
4715 zfeature_info_t *fi = &spa_feature_table[i];
4716 const char *ro = fi->fi_can_readonly ?
4717 " (read-only compatible)" : "";
4719 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4720 (void) printf(" %s\n", fi->fi_desc);
4722 (void) printf("\n");
4724 (void) printf(gettext("The following legacy versions are also "
4726 (void) printf(gettext("VER DESCRIPTION\n"));
4727 (void) printf("--- -----------------------------------------"
4728 "---------------\n");
4729 (void) printf(gettext(" 1 Initial ZFS version\n"));
4730 (void) printf(gettext(" 2 Ditto blocks "
4731 "(replicated metadata)\n"));
4732 (void) printf(gettext(" 3 Hot spares and double parity "
4734 (void) printf(gettext(" 4 zpool history\n"));
4735 (void) printf(gettext(" 5 Compression using the gzip "
4737 (void) printf(gettext(" 6 bootfs pool property\n"));
4738 (void) printf(gettext(" 7 Separate intent log devices\n"));
4739 (void) printf(gettext(" 8 Delegated administration\n"));
4740 (void) printf(gettext(" 9 refquota and refreservation "
4742 (void) printf(gettext(" 10 Cache devices\n"));
4743 (void) printf(gettext(" 11 Improved scrub performance\n"));
4744 (void) printf(gettext(" 12 Snapshot properties\n"));
4745 (void) printf(gettext(" 13 snapused property\n"));
4746 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4747 (void) printf(gettext(" 15 user/group space accounting\n"));
4748 (void) printf(gettext(" 16 stmf property support\n"));
4749 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4750 (void) printf(gettext(" 18 Snapshot user holds\n"));
4751 (void) printf(gettext(" 19 Log device removal\n"));
4752 (void) printf(gettext(" 20 Compression using zle "
4753 "(zero-length encoding)\n"));
4754 (void) printf(gettext(" 21 Deduplication\n"));
4755 (void) printf(gettext(" 22 Received properties\n"));
4756 (void) printf(gettext(" 23 Slim ZIL\n"));
4757 (void) printf(gettext(" 24 System attributes\n"));
4758 (void) printf(gettext(" 25 Improved scrub stats\n"));
4759 (void) printf(gettext(" 26 Improved snapshot deletion "
4761 (void) printf(gettext(" 27 Improved snapshot creation "
4763 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4764 (void) printf(gettext("\nFor more information on a particular "
4765 "version, including supported releases,\n"));
4766 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4767 } else if (argc == 0 && upgradeall) {
4768 cb.cb_first = B_TRUE;
4769 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4770 if (ret == 0 && cb.cb_first) {
4771 if (cb.cb_version == SPA_VERSION) {
4772 (void) printf(gettext("All pools are already "
4773 "formatted using feature flags.\n\n"));
4774 (void) printf(gettext("Every feature flags "
4775 "pool already has all supported features "
4778 (void) printf(gettext("All pools are already "
4779 "formatted with version %llu or higher.\n"),
4780 (u_longlong_t) cb.cb_version);
4783 } else if (argc == 0) {
4784 cb.cb_first = B_TRUE;
4785 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4789 (void) printf(gettext("All pools are formatted "
4790 "using feature flags.\n\n"));
4792 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4793 "for a list of available legacy versions.\n"));
4796 cb.cb_first = B_TRUE;
4797 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4801 (void) printf(gettext("Every feature flags pool has "
4802 "all supported features enabled.\n"));
4804 (void) printf(gettext("\n"));
4807 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4814 typedef struct hist_cbdata {
4821 * Print out the command history for a specific pool.
4824 get_history_one(zpool_handle_t *zhp, void *data)
4840 char internalstr[MAXPATHLEN];
4841 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4845 cb->first = B_FALSE;
4847 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4849 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4852 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4853 &records, &numrecords) == 0);
4854 for (i = 0; i < numrecords; i++) {
4855 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4859 /* is it an internal event or a standard event? */
4860 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4862 if (cb->internal == 0)
4865 if (nvlist_lookup_uint64(records[i],
4866 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4868 verify(nvlist_lookup_uint64(records[i],
4869 ZPOOL_HIST_TXG, &txg) == 0);
4870 verify(nvlist_lookup_string(records[i],
4871 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4872 if (ievent >= LOG_END)
4874 (void) snprintf(internalstr,
4875 sizeof (internalstr),
4876 "[internal %s txg:%llu] %s",
4877 zfs_history_event_names[ievent], (u_longlong_t)txg,
4879 cmdstr = internalstr;
4882 (void) localtime_r(&tsec, &t);
4883 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4884 (void) printf("%s %s", tbuf, cmdstr);
4887 (void) printf("\n");
4890 (void) printf(" [");
4891 if (nvlist_lookup_uint64(records[i],
4892 ZPOOL_HIST_WHO, &who) == 0) {
4893 pwd = getpwuid((uid_t)who);
4895 (void) printf("user %s on",
4898 (void) printf("user %d on",
4901 (void) printf(gettext("no info]\n"));
4904 if (nvlist_lookup_string(records[i],
4905 ZPOOL_HIST_HOST, &hostname) == 0) {
4906 (void) printf(" %s", hostname);
4908 if (nvlist_lookup_string(records[i],
4909 ZPOOL_HIST_ZONE, &zonename) == 0) {
4910 (void) printf(":%s", zonename);
4914 (void) printf("\n");
4916 (void) printf("\n");
4923 * zpool history <pool>
4925 * Displays the history of commands that modified pools.
4930 zpool_do_history(int argc, char **argv)
4932 hist_cbdata_t cbdata = { 0 };
4936 cbdata.first = B_TRUE;
4938 while ((c = getopt(argc, argv, "li")) != -1) {
4944 cbdata.internal = 1;
4947 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4955 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4958 if (argc == 0 && cbdata.first == B_TRUE) {
4959 (void) fprintf(stderr, gettext("no pools available\n"));
4966 typedef struct ev_opts {
4974 zpool_do_events_short(nvlist_t *nvl)
4976 char ctime_str[26], str[32], *ptr;
4980 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
4981 memset(str, ' ', 32);
4982 (void) ctime_r((const time_t *)&tv[0], ctime_str);
4983 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
4984 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
4985 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
4986 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
4987 (void) printf(gettext("%s "), str);
4989 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
4990 (void) printf(gettext("%s\n"), ptr);
4994 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
4998 for (nvp = nvlist_next_nvpair(nvl, NULL);
4999 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5001 data_type_t type = nvpair_type(nvp);
5002 const char *name = nvpair_name(nvp);
5012 printf(gettext("%*s%s = "), depth, "", name);
5015 case DATA_TYPE_BOOLEAN:
5016 printf(gettext("%s"), "1");
5019 case DATA_TYPE_BOOLEAN_VALUE:
5020 (void) nvpair_value_boolean_value(nvp, &b);
5021 printf(gettext("%s"), b ? "1" : "0");
5024 case DATA_TYPE_BYTE:
5025 (void) nvpair_value_byte(nvp, &i8);
5026 printf(gettext("0x%x"), i8);
5029 case DATA_TYPE_INT8:
5030 (void) nvpair_value_int8(nvp, (void *)&i8);
5031 printf(gettext("0x%x"), i8);
5034 case DATA_TYPE_UINT8:
5035 (void) nvpair_value_uint8(nvp, &i8);
5036 printf(gettext("0x%x"), i8);
5039 case DATA_TYPE_INT16:
5040 (void) nvpair_value_int16(nvp, (void *)&i16);
5041 printf(gettext("0x%x"), i16);
5044 case DATA_TYPE_UINT16:
5045 (void) nvpair_value_uint16(nvp, &i16);
5046 printf(gettext("0x%x"), i16);
5049 case DATA_TYPE_INT32:
5050 (void) nvpair_value_int32(nvp, (void *)&i32);
5051 printf(gettext("0x%x"), i32);
5054 case DATA_TYPE_UINT32:
5055 (void) nvpair_value_uint32(nvp, &i32);
5056 printf(gettext("0x%x"), i32);
5059 case DATA_TYPE_INT64:
5060 (void) nvpair_value_int64(nvp, (void *)&i64);
5061 printf(gettext("0x%llx"), (u_longlong_t)i64);
5064 case DATA_TYPE_UINT64:
5065 (void) nvpair_value_uint64(nvp, &i64);
5066 printf(gettext("0x%llx"), (u_longlong_t)i64);
5069 case DATA_TYPE_HRTIME:
5070 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5071 printf(gettext("0x%llx"), (u_longlong_t)i64);
5074 case DATA_TYPE_STRING:
5075 (void) nvpair_value_string(nvp, &str);
5076 printf(gettext("\"%s\""), str ? str : "<NULL>");
5079 case DATA_TYPE_NVLIST:
5080 printf(gettext("(embedded nvlist)\n"));
5081 (void) nvpair_value_nvlist(nvp, &cnv);
5082 zpool_do_events_nvprint(cnv, depth + 8);
5083 printf(gettext("%*s(end %s)"), depth, "", name);
5086 case DATA_TYPE_NVLIST_ARRAY: {
5090 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5091 printf(gettext("(%d embedded nvlists)\n"), nelem);
5092 for (i = 0; i < nelem; i++) {
5093 printf(gettext("%*s%s[%d] = %s\n"),
5094 depth, "", name, i, "(embedded nvlist)");
5095 zpool_do_events_nvprint(val[i], depth + 8);
5096 printf(gettext("%*s(end %s[%i])\n"),
5097 depth, "", name, i);
5099 printf(gettext("%*s(end %s)\n"), depth, "", name);
5103 case DATA_TYPE_INT8_ARRAY: {
5107 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5108 for (i = 0; i < nelem; i++)
5109 printf(gettext("0x%x "), val[i]);
5114 case DATA_TYPE_UINT8_ARRAY: {
5118 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5119 for (i = 0; i < nelem; i++)
5120 printf(gettext("0x%x "), val[i]);
5125 case DATA_TYPE_INT16_ARRAY: {
5129 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5130 for (i = 0; i < nelem; i++)
5131 printf(gettext("0x%x "), val[i]);
5136 case DATA_TYPE_UINT16_ARRAY: {
5140 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5141 for (i = 0; i < nelem; i++)
5142 printf(gettext("0x%x "), val[i]);
5147 case DATA_TYPE_INT32_ARRAY: {
5151 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5152 for (i = 0; i < nelem; i++)
5153 printf(gettext("0x%x "), val[i]);
5158 case DATA_TYPE_UINT32_ARRAY: {
5162 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5163 for (i = 0; i < nelem; i++)
5164 printf(gettext("0x%x "), val[i]);
5169 case DATA_TYPE_INT64_ARRAY: {
5173 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5174 for (i = 0; i < nelem; i++)
5175 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
5180 case DATA_TYPE_UINT64_ARRAY: {
5184 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5185 for (i = 0; i < nelem; i++)
5186 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
5191 case DATA_TYPE_STRING_ARRAY:
5192 case DATA_TYPE_BOOLEAN_ARRAY:
5193 case DATA_TYPE_BYTE_ARRAY:
5194 case DATA_TYPE_DOUBLE:
5195 case DATA_TYPE_UNKNOWN:
5196 printf(gettext("<unknown>"));
5200 printf(gettext("\n"));
5205 zpool_do_events_next(ev_opts_t *opts)
5208 int cleanup_fd, ret, dropped;
5210 cleanup_fd = open(ZFS_DEV, O_RDWR);
5211 VERIFY(cleanup_fd >= 0);
5213 if (!opts->scripted)
5214 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5217 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5218 !!opts->follow, cleanup_fd);
5219 if (ret || nvl == NULL)
5223 (void) printf(gettext("dropped %d events\n"), dropped);
5225 zpool_do_events_short(nvl);
5227 if (opts->verbose) {
5228 zpool_do_events_nvprint(nvl, 8);
5229 printf(gettext("\n"));
5235 VERIFY(0 == close(cleanup_fd));
5241 zpool_do_events_clear(ev_opts_t *opts)
5245 ret = zpool_events_clear(g_zfs, &count);
5247 (void) printf(gettext("cleared %d events\n"), count);
5253 * zpool events [-vfc]
5255 * Displays events logs by ZFS.
5258 zpool_do_events(int argc, char **argv)
5260 ev_opts_t opts = { 0 };
5265 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5280 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5289 ret = zpool_do_events_clear(&opts);
5291 ret = zpool_do_events_next(&opts);
5297 get_callback(zpool_handle_t *zhp, void *data)
5299 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5300 char value[MAXNAMELEN];
5301 zprop_source_t srctype;
5304 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5307 * Skip the special fake placeholder. This will also skip
5308 * over the name property when 'all' is specified.
5310 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5311 pl == cbp->cb_proplist)
5314 if (pl->pl_prop == ZPROP_INVAL &&
5315 (zpool_prop_feature(pl->pl_user_prop) ||
5316 zpool_prop_unsupported(pl->pl_user_prop))) {
5317 srctype = ZPROP_SRC_LOCAL;
5319 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5320 value, sizeof (value)) == 0) {
5321 zprop_print_one_property(zpool_get_name(zhp),
5322 cbp, pl->pl_user_prop, value, srctype,
5326 if (zpool_get_prop(zhp, pl->pl_prop, value,
5327 sizeof (value), &srctype) != 0)
5330 zprop_print_one_property(zpool_get_name(zhp), cbp,
5331 zpool_prop_to_name(pl->pl_prop), value, srctype,
5339 zpool_do_get(int argc, char **argv)
5341 zprop_get_cbdata_t cb = { 0 };
5342 zprop_list_t fake_name = { 0 };
5346 (void) fprintf(stderr, gettext("missing property "
5351 cb.cb_first = B_TRUE;
5352 cb.cb_sources = ZPROP_SRC_ALL;
5353 cb.cb_columns[0] = GET_COL_NAME;
5354 cb.cb_columns[1] = GET_COL_PROPERTY;
5355 cb.cb_columns[2] = GET_COL_VALUE;
5356 cb.cb_columns[3] = GET_COL_SOURCE;
5357 cb.cb_type = ZFS_TYPE_POOL;
5359 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5360 ZFS_TYPE_POOL) != 0)
5363 if (cb.cb_proplist != NULL) {
5364 fake_name.pl_prop = ZPOOL_PROP_NAME;
5365 fake_name.pl_width = strlen(gettext("NAME"));
5366 fake_name.pl_next = cb.cb_proplist;
5367 cb.cb_proplist = &fake_name;
5370 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5373 if (cb.cb_proplist == &fake_name)
5374 zprop_free_list(fake_name.pl_next);
5376 zprop_free_list(cb.cb_proplist);
5381 typedef struct set_cbdata {
5384 boolean_t cb_any_successful;
5388 set_callback(zpool_handle_t *zhp, void *data)
5391 set_cbdata_t *cb = (set_cbdata_t *)data;
5393 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5396 cb->cb_any_successful = B_TRUE;
5402 zpool_do_set(int argc, char **argv)
5404 set_cbdata_t cb = { 0 };
5407 if (argc > 1 && argv[1][0] == '-') {
5408 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5414 (void) fprintf(stderr, gettext("missing property=value "
5420 (void) fprintf(stderr, gettext("missing pool name\n"));
5425 (void) fprintf(stderr, gettext("too many pool names\n"));
5429 cb.cb_propname = argv[1];
5430 cb.cb_value = strchr(cb.cb_propname, '=');
5431 if (cb.cb_value == NULL) {
5432 (void) fprintf(stderr, gettext("missing value in "
5433 "property=value argument\n"));
5437 *(cb.cb_value) = '\0';
5440 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5447 find_command_idx(char *command, int *idx)
5451 for (i = 0; i < NCOMMAND; i++) {
5452 if (command_table[i].name == NULL)
5455 if (strcmp(command, command_table[i].name) == 0) {
5464 main(int argc, char **argv)
5470 (void) setlocale(LC_ALL, "");
5471 (void) textdomain(TEXT_DOMAIN);
5476 * Make sure the user has specified some command.
5479 (void) fprintf(stderr, gettext("missing command\n"));
5488 if ((strcmp(cmdname, "-?") == 0) ||
5489 strcmp(cmdname, "--help") == 0)
5492 if ((g_zfs = libzfs_init()) == NULL)
5495 libzfs_print_on_error(g_zfs, B_TRUE);
5497 zpool_set_history_str("zpool", argc, argv, history_str);
5498 verify(zpool_stage_history(g_zfs, history_str) == 0);
5501 * Run the appropriate command.
5503 if (find_command_idx(cmdname, &i) == 0) {
5504 current_command = &command_table[i];
5505 ret = command_table[i].func(argc - 1, argv + 1);
5506 } else if (strchr(cmdname, '=')) {
5507 verify(find_command_idx("set", &i) == 0);
5508 current_command = &command_table[i];
5509 ret = command_table[i].func(argc, argv);
5510 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5512 * 'freeze' is a vile debugging abomination, so we treat
5516 int fd = open(ZFS_DEV, O_RDWR);
5517 (void) strcpy((void *)buf, argv[2]);
5518 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5520 (void) fprintf(stderr, gettext("unrecognized "
5521 "command '%s'\n"), cmdname);
5529 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5530 * for the purposes of running ::findleaks.
5532 if (getenv("ZFS_ABORT") != NULL) {
5533 (void) printf("dumping core by request\n");