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) 2011, 2014 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>
52 #include <sys/zfs_ioctl.h>
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 #include "statcommon.h"
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
69 static int zpool_do_list(int, char **);
70 static int zpool_do_iostat(int, char **);
71 static int zpool_do_status(int, char **);
73 static int zpool_do_online(int, char **);
74 static int zpool_do_offline(int, char **);
75 static int zpool_do_clear(int, char **);
76 static int zpool_do_reopen(int, char **);
78 static int zpool_do_reguid(int, char **);
80 static int zpool_do_attach(int, char **);
81 static int zpool_do_detach(int, char **);
82 static int zpool_do_replace(int, char **);
83 static int zpool_do_split(int, char **);
85 static int zpool_do_scrub(int, char **);
87 static int zpool_do_import(int, char **);
88 static int zpool_do_export(int, char **);
90 static int zpool_do_upgrade(int, char **);
92 static int zpool_do_history(int, char **);
93 static int zpool_do_events(int, char **);
95 static int zpool_do_get(int, char **);
96 static int zpool_do_set(int, char **);
99 * These libumem hooks provide a reasonable set of defaults for the allocator's
100 * debugging facilities.
105 _umem_debug_init(void)
107 return ("default,verbose"); /* $UMEM_DEBUG setting */
111 _umem_logging_init(void)
113 return ("fail,contents"); /* $UMEM_LOGGING setting */
146 typedef struct zpool_command {
148 int (*func)(int, char **);
153 * Master command table. Each ZFS command has a name, associated function, and
154 * usage message. The usage messages need to be internationalized, so we have
155 * to have a function to return the usage message based on a command index.
157 * These commands are organized according to how they are displayed in the usage
158 * message. An empty command (one with a NULL name) indicates an empty line in
159 * the generic usage message.
161 static zpool_command_t command_table[] = {
162 { "create", zpool_do_create, HELP_CREATE },
163 { "destroy", zpool_do_destroy, HELP_DESTROY },
165 { "add", zpool_do_add, HELP_ADD },
166 { "remove", zpool_do_remove, HELP_REMOVE },
168 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
170 { "list", zpool_do_list, HELP_LIST },
171 { "iostat", zpool_do_iostat, HELP_IOSTAT },
172 { "status", zpool_do_status, HELP_STATUS },
174 { "online", zpool_do_online, HELP_ONLINE },
175 { "offline", zpool_do_offline, HELP_OFFLINE },
176 { "clear", zpool_do_clear, HELP_CLEAR },
177 { "reopen", zpool_do_reopen, HELP_REOPEN },
179 { "attach", zpool_do_attach, HELP_ATTACH },
180 { "detach", zpool_do_detach, HELP_DETACH },
181 { "replace", zpool_do_replace, HELP_REPLACE },
182 { "split", zpool_do_split, HELP_SPLIT },
184 { "scrub", zpool_do_scrub, HELP_SCRUB },
186 { "import", zpool_do_import, HELP_IMPORT },
187 { "export", zpool_do_export, HELP_EXPORT },
188 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
189 { "reguid", zpool_do_reguid, HELP_REGUID },
191 { "history", zpool_do_history, HELP_HISTORY },
192 { "events", zpool_do_events, HELP_EVENTS },
194 { "get", zpool_do_get, HELP_GET },
195 { "set", zpool_do_set, HELP_SET },
198 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
200 static zpool_command_t *current_command;
201 static char history_str[HIS_MAX_RECORD_LEN];
202 static boolean_t log_history = B_TRUE;
203 static uint_t timestamp_fmt = NODATE;
206 get_usage(zpool_help_t idx) {
209 return (gettext("\tadd [-fn] [-o property=value] "
210 "<pool> <vdev> ...\n"));
212 return (gettext("\tattach [-f] [-o property=value] "
213 "<pool> <device> <new-device>\n"));
215 return (gettext("\tclear [-nF] <pool> [device]\n"));
217 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
218 "\t [-O file-system-property=value] ... \n"
219 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
221 return (gettext("\tdestroy [-f] <pool>\n"));
223 return (gettext("\tdetach <pool> <device>\n"));
225 return (gettext("\texport [-f] <pool> ...\n"));
227 return (gettext("\thistory [-il] [<pool>] ...\n"));
229 return (gettext("\timport [-d dir] [-D]\n"
230 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
231 "\timport [-o mntopts] [-o property=value] ... \n"
232 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
233 "[-R root] [-F [-n]] -a\n"
234 "\timport [-o mntopts] [-o property=value] ... \n"
235 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
236 "[-R root] [-F [-n]]\n"
237 "\t <pool | id> [newpool]\n"));
239 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
241 case HELP_LABELCLEAR:
242 return (gettext("\tlabelclear [-f] <vdev>\n"));
244 return (gettext("\tlist [-Hv] [-o property[,...]] "
245 "[-T d|u] [pool] ... [interval [count]]\n"));
247 return (gettext("\toffline [-t] <pool> <device> ...\n"));
249 return (gettext("\tonline <pool> <device> ...\n"));
251 return (gettext("\treplace [-f] [-o property=value] "
252 "<pool> <device> [new-device]\n"));
254 return (gettext("\tremove <pool> <device> ...\n"));
256 return (gettext("\treopen <pool>\n"));
258 return (gettext("\tscrub [-s] <pool> ...\n"));
260 return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
263 return (gettext("\tupgrade\n"
265 "\tupgrade [-V version] <-a | pool ...>\n"));
267 return (gettext("\tevents [-vHfc]\n"));
269 return (gettext("\tget [-pH] <\"all\" | property[,...]> "
272 return (gettext("\tset <property=value> <pool> \n"));
274 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
275 "\t [-o property=value] <pool> <newpool> "
276 "[<device> ...]\n"));
278 return (gettext("\treguid <pool>\n"));
287 * Callback routine that will print out a pool property value.
290 print_prop_cb(int prop, void *cb)
294 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
296 if (zpool_prop_readonly(prop))
297 (void) fprintf(fp, " NO ");
299 (void) fprintf(fp, " YES ");
301 if (zpool_prop_values(prop) == NULL)
302 (void) fprintf(fp, "-\n");
304 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
310 * Display usage message. If we're inside a command, display only the usage for
311 * that command. Otherwise, iterate over the entire command table and display
312 * a complete usage message.
315 usage(boolean_t requested)
317 FILE *fp = requested ? stdout : stderr;
319 if (current_command == NULL) {
322 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
324 gettext("where 'command' is one of the following:\n\n"));
326 for (i = 0; i < NCOMMAND; i++) {
327 if (command_table[i].name == NULL)
328 (void) fprintf(fp, "\n");
330 (void) fprintf(fp, "%s",
331 get_usage(command_table[i].usage));
334 (void) fprintf(fp, gettext("usage:\n"));
335 (void) fprintf(fp, "%s", get_usage(current_command->usage));
338 if (current_command != NULL &&
339 ((strcmp(current_command->name, "set") == 0) ||
340 (strcmp(current_command->name, "get") == 0) ||
341 (strcmp(current_command->name, "list") == 0))) {
344 gettext("\nthe following properties are supported:\n"));
346 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
347 "PROPERTY", "EDIT", "VALUES");
349 /* Iterate over all properties */
350 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
353 (void) fprintf(fp, "\t%-15s ", "feature@...");
354 (void) fprintf(fp, "YES disabled | enabled | active\n");
356 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
357 "appended with a feature name.\nSee zpool-features(5).\n"));
361 * See comments at end of main().
363 if (getenv("ZFS_ABORT") != NULL) {
364 (void) printf("dumping core by request\n");
368 exit(requested ? 0 : 2);
372 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
373 boolean_t print_logs)
380 (void) printf("\t%*s%s\n", indent, "", name);
382 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
383 &child, &children) != 0)
386 for (c = 0; c < children; c++) {
387 uint64_t is_log = B_FALSE;
389 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
391 if ((is_log && !print_logs) || (!is_log && print_logs))
394 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
395 print_vdev_tree(zhp, vname, child[c], indent + 2,
402 prop_list_contains_feature(nvlist_t *proplist)
405 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
406 nvp = nvlist_next_nvpair(proplist, nvp)) {
407 if (zpool_prop_feature(nvpair_name(nvp)))
414 * Add a property pair (name, string-value) into a property nvlist.
417 add_prop_list(const char *propname, char *propval, nvlist_t **props,
420 zpool_prop_t prop = ZPROP_INVAL;
426 if (*props == NULL &&
427 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
428 (void) fprintf(stderr,
429 gettext("internal error: out of memory\n"));
436 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
438 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
439 !zpool_prop_feature(propname)) {
440 (void) fprintf(stderr, gettext("property '%s' is "
441 "not a valid pool property\n"), propname);
446 * feature@ properties and version should not be specified
449 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
450 nvlist_exists(proplist, vname)) ||
451 (prop == ZPOOL_PROP_VERSION &&
452 prop_list_contains_feature(proplist))) {
453 (void) fprintf(stderr, gettext("'feature@' and "
454 "'version' properties cannot be specified "
460 if (zpool_prop_feature(propname))
463 normnm = zpool_prop_to_name(prop);
465 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
466 normnm = zfs_prop_to_name(fprop);
472 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
473 prop != ZPOOL_PROP_CACHEFILE) {
474 (void) fprintf(stderr, gettext("property '%s' "
475 "specified multiple times\n"), propname);
479 if (nvlist_add_string(proplist, normnm, propval) != 0) {
480 (void) fprintf(stderr, gettext("internal "
481 "error: out of memory\n"));
489 * Set a default property pair (name, string-value) in a property nvlist
492 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
497 if (nvlist_lookup_string(*props, propname, &pval) == 0)
500 return (add_prop_list(propname, propval, props, B_TRUE));
504 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
506 * -f Force addition of devices, even if they appear in use
507 * -n Do not add the devices, but display the resulting layout if
508 * they were to be added.
509 * -o Set property=value.
511 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
512 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
516 zpool_do_add(int argc, char **argv)
518 boolean_t force = B_FALSE;
519 boolean_t dryrun = B_FALSE;
526 nvlist_t *props = NULL;
530 while ((c = getopt(argc, argv, "fno:")) != -1) {
539 if ((propval = strchr(optarg, '=')) == NULL) {
540 (void) fprintf(stderr, gettext("missing "
541 "'=' for -o option\n"));
547 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
548 (add_prop_list(optarg, propval, &props, B_TRUE)))
552 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
561 /* get pool name and check number of arguments */
563 (void) fprintf(stderr, gettext("missing pool name argument\n"));
567 (void) fprintf(stderr, gettext("missing vdev specification\n"));
576 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
579 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
580 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
586 /* pass off to get_vdev_spec for processing */
587 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
589 if (nvroot == NULL) {
595 nvlist_t *poolnvroot;
597 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
600 (void) printf(gettext("would update '%s' to the following "
601 "configuration:\n"), zpool_get_name(zhp));
603 /* print original main pool and new tree */
604 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
605 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
607 /* Do the same for the logs */
608 if (num_logs(poolnvroot) > 0) {
609 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
610 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
611 } else if (num_logs(nvroot) > 0) {
612 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
617 ret = (zpool_add(zhp, nvroot) != 0);
628 * zpool remove <pool> <vdev> ...
630 * Removes the given vdev from the pool. Currently, this supports removing
631 * spares, cache, and log devices from the pool.
634 zpool_do_remove(int argc, char **argv)
643 /* get pool name and check number of arguments */
645 (void) fprintf(stderr, gettext("missing pool name argument\n"));
649 (void) fprintf(stderr, gettext("missing device\n"));
655 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
658 for (i = 1; i < argc; i++) {
659 if (zpool_vdev_remove(zhp, argv[i]) != 0)
667 * zpool labelclear <vdev>
669 * Verifies that the vdev is not active and zeros out the label information
673 zpool_do_labelclear(int argc, char **argv)
676 int c, fd = -1, ret = 0;
678 boolean_t inuse = B_FALSE;
679 boolean_t force = B_FALSE;
682 while ((c = getopt(argc, argv, "f")) != -1) {
688 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
699 (void) fprintf(stderr, gettext("missing vdev device name\n"));
704 if ((fd = open(vdev, O_RDWR)) < 0) {
705 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
710 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
714 (void) fprintf(stderr,
715 gettext("Unable to determine pool state for %s\n"
716 "Use -f to force the clearing any label data\n"), vdev);
724 case POOL_STATE_ACTIVE:
725 case POOL_STATE_SPARE:
726 case POOL_STATE_L2CACHE:
727 (void) fprintf(stderr,
728 gettext("labelclear operation failed.\n"
729 "\tVdev %s is a member (%s), of pool \"%s\".\n"
730 "\tTo remove label information from this device, "
731 "export or destroy\n\tthe pool, or remove %s from "
732 "the configuration of this pool\n\tand retry the "
733 "labelclear operation.\n"),
734 vdev, zpool_pool_state_to_name(state), name, vdev);
738 case POOL_STATE_EXPORTED:
742 (void) fprintf(stderr,
743 gettext("labelclear operation failed.\n\tVdev "
744 "%s is a member of the exported pool \"%s\".\n"
745 "\tUse \"zpool labelclear -f %s\" to force the "
746 "removal of label\n\tinformation.\n"),
751 case POOL_STATE_POTENTIALLY_ACTIVE:
755 (void) fprintf(stderr,
756 gettext("labelclear operation failed.\n"
757 "\tVdev %s is a member of the pool \"%s\".\n"
758 "\tThis pool is unknown to this system, but may "
759 "be active on\n\tanother system. Use "
760 "\'zpool labelclear -f %s\' to force the\n"
761 "\tremoval of label information.\n"),
766 case POOL_STATE_DESTROYED:
767 /* inuse should never be set for a destroyed pool... */
773 if (zpool_clear_label(fd) != 0) {
774 (void) fprintf(stderr,
775 gettext("Label clear failed on vdev %s\n"), vdev);
788 * zpool create [-fnd] [-o property=value] ...
789 * [-O file-system-property=value] ...
790 * [-R root] [-m mountpoint] <pool> <dev> ...
792 * -f Force creation, even if devices appear in use
793 * -n Do not create the pool, but display the resulting layout if it
794 * were to be created.
795 * -R Create a pool under an alternate root
796 * -m Set default mountpoint for the root dataset. By default it's
798 * -o Set property=value.
799 * -d Don't automatically enable all supported pool features
800 * (individual features can be enabled with -o).
801 * -O Set fsproperty=value in the pool's root file system
803 * Creates the named pool according to the given vdev specification. The
804 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
805 * we get the nvlist back from get_vdev_spec(), we either print out the contents
806 * (if '-n' was specified), or pass it to libzfs to do the creation.
809 zpool_do_create(int argc, char **argv)
811 boolean_t force = B_FALSE;
812 boolean_t dryrun = B_FALSE;
813 boolean_t enable_all_pool_feat = B_TRUE;
815 nvlist_t *nvroot = NULL;
819 char *altroot = NULL;
820 char *mountpoint = NULL;
821 nvlist_t *fsprops = NULL;
822 nvlist_t *props = NULL;
826 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
835 enable_all_pool_feat = B_FALSE;
839 if (add_prop_list(zpool_prop_to_name(
840 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
842 if (add_prop_list_default(zpool_prop_to_name(
843 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
847 /* Equivalent to -O mountpoint=optarg */
851 if ((propval = strchr(optarg, '=')) == NULL) {
852 (void) fprintf(stderr, gettext("missing "
853 "'=' for -o option\n"));
859 if (add_prop_list(optarg, propval, &props, B_TRUE))
863 * If the user is creating a pool that doesn't support
864 * feature flags, don't enable any features.
866 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
870 ver = strtoull(propval, &end, 10);
872 ver < SPA_VERSION_FEATURES) {
873 enable_all_pool_feat = B_FALSE;
878 if ((propval = strchr(optarg, '=')) == NULL) {
879 (void) fprintf(stderr, gettext("missing "
880 "'=' for -O option\n"));
887 * Mountpoints are checked and then added later.
888 * Uniquely among properties, they can be specified
889 * more than once, to avoid conflict with -m.
891 if (0 == strcmp(optarg,
892 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
893 mountpoint = propval;
894 } else if (add_prop_list(optarg, propval, &fsprops,
901 * Sanity check temporary pool name.
903 if (strchr(optarg, '/') != NULL) {
904 (void) fprintf(stderr, gettext("cannot create "
905 "'%s': invalid character '/' in temporary "
907 (void) fprintf(stderr, gettext("use 'zfs "
908 "create' to create a dataset\n"));
912 if (add_prop_list(zpool_prop_to_name(
913 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
915 if (add_prop_list_default(zpool_prop_to_name(
916 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
921 (void) fprintf(stderr, gettext("missing argument for "
922 "'%c' option\n"), optopt);
925 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
934 /* get pool name and check number of arguments */
936 (void) fprintf(stderr, gettext("missing pool name argument\n"));
940 (void) fprintf(stderr, gettext("missing vdev specification\n"));
947 * As a special case, check for use of '/' in the name, and direct the
948 * user to use 'zfs create' instead.
950 if (strchr(poolname, '/') != NULL) {
951 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
952 "character '/' in pool name\n"), poolname);
953 (void) fprintf(stderr, gettext("use 'zfs create' to "
954 "create a dataset\n"));
958 /* pass off to get_vdev_spec for bulk processing */
959 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
964 /* make_root_vdev() allows 0 toplevel children if there are spares */
965 if (!zfs_allocatable_devs(nvroot)) {
966 (void) fprintf(stderr, gettext("invalid vdev "
967 "specification: at least one toplevel vdev must be "
972 if (altroot != NULL && altroot[0] != '/') {
973 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
974 "must be an absolute path\n"), altroot);
979 * Check the validity of the mountpoint and direct the user to use the
980 * '-m' mountpoint option if it looks like its in use.
982 if (mountpoint == NULL ||
983 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
984 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
985 char buf[MAXPATHLEN];
988 if (mountpoint && mountpoint[0] != '/') {
989 (void) fprintf(stderr, gettext("invalid mountpoint "
990 "'%s': must be an absolute path, 'legacy', or "
991 "'none'\n"), mountpoint);
995 if (mountpoint == NULL) {
997 (void) snprintf(buf, sizeof (buf), "%s/%s",
1000 (void) snprintf(buf, sizeof (buf), "/%s",
1003 if (altroot != NULL)
1004 (void) snprintf(buf, sizeof (buf), "%s%s",
1005 altroot, mountpoint);
1007 (void) snprintf(buf, sizeof (buf), "%s",
1011 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1012 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1013 "%s\n"), buf, strerror(errno));
1014 (void) fprintf(stderr, gettext("use '-m' "
1015 "option to provide a different default\n"));
1020 while (count < 3 && readdir(dirp) != NULL)
1022 (void) closedir(dirp);
1025 (void) fprintf(stderr, gettext("mountpoint "
1026 "'%s' exists and is not empty\n"), buf);
1027 (void) fprintf(stderr, gettext("use '-m' "
1028 "option to provide a "
1029 "different default\n"));
1036 * Now that the mountpoint's validity has been checked, ensure that
1037 * the property is set appropriately prior to creating the pool.
1039 if (mountpoint != NULL) {
1040 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1041 mountpoint, &fsprops, B_FALSE);
1049 * For a dry run invocation, print out a basic message and run
1050 * through all the vdevs in the list and print out in an
1051 * appropriate hierarchy.
1053 (void) printf(gettext("would create '%s' with the "
1054 "following layout:\n\n"), poolname);
1056 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1057 if (num_logs(nvroot) > 0)
1058 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1063 * Hand off to libzfs.
1065 if (enable_all_pool_feat) {
1067 for (i = 0; i < SPA_FEATURES; i++) {
1068 char propname[MAXPATHLEN];
1069 zfeature_info_t *feat = &spa_feature_table[i];
1071 (void) snprintf(propname, sizeof (propname),
1072 "feature@%s", feat->fi_uname);
1075 * Skip feature if user specified it manually
1076 * on the command line.
1078 if (nvlist_exists(props, propname))
1081 ret = add_prop_list(propname,
1082 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1089 if (zpool_create(g_zfs, poolname,
1090 nvroot, props, fsprops) == 0) {
1091 zfs_handle_t *pool = zfs_open(g_zfs,
1092 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1094 if (zfs_mount(pool, NULL, 0) == 0)
1095 ret = zfs_shareall(pool);
1098 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1099 (void) fprintf(stderr, gettext("pool name may have "
1105 nvlist_free(nvroot);
1106 nvlist_free(fsprops);
1110 nvlist_free(fsprops);
1117 * zpool destroy <pool>
1119 * -f Forcefully unmount any datasets
1121 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1124 zpool_do_destroy(int argc, char **argv)
1126 boolean_t force = B_FALSE;
1129 zpool_handle_t *zhp;
1133 while ((c = getopt(argc, argv, "f")) != -1) {
1139 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1148 /* check arguments */
1150 (void) fprintf(stderr, gettext("missing pool argument\n"));
1154 (void) fprintf(stderr, gettext("too many arguments\n"));
1160 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1162 * As a special case, check for use of '/' in the name, and
1163 * direct the user to use 'zfs destroy' instead.
1165 if (strchr(pool, '/') != NULL)
1166 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1167 "destroy a dataset\n"));
1171 if (zpool_disable_datasets(zhp, force) != 0) {
1172 (void) fprintf(stderr, gettext("could not destroy '%s': "
1173 "could not unmount datasets\n"), zpool_get_name(zhp));
1177 /* The history must be logged as part of the export */
1178 log_history = B_FALSE;
1180 ret = (zpool_destroy(zhp, history_str) != 0);
1188 * zpool export [-f] <pool> ...
1190 * -f Forcefully unmount datasets
1192 * Export the given pools. By default, the command will attempt to cleanly
1193 * unmount any active datasets within the pool. If the '-f' flag is specified,
1194 * then the datasets will be forcefully unmounted.
1197 zpool_do_export(int argc, char **argv)
1199 boolean_t force = B_FALSE;
1200 boolean_t hardforce = B_FALSE;
1202 zpool_handle_t *zhp;
1207 while ((c = getopt(argc, argv, "fF")) != -1) {
1216 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1225 /* check arguments */
1227 (void) fprintf(stderr, gettext("missing pool argument\n"));
1232 for (i = 0; i < argc; i++) {
1233 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1238 if (zpool_disable_datasets(zhp, force) != 0) {
1244 /* The history must be logged as part of the export */
1245 log_history = B_FALSE;
1248 if (zpool_export_force(zhp, history_str) != 0)
1250 } else if (zpool_export(zhp, force, history_str) != 0) {
1261 * Given a vdev configuration, determine the maximum width needed for the device
1265 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1267 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1272 if (strlen(name) + depth > max)
1273 max = strlen(name) + depth;
1277 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1278 &child, &children) == 0) {
1279 for (c = 0; c < children; c++)
1280 if ((ret = max_width(zhp, child[c], depth + 2,
1285 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1286 &child, &children) == 0) {
1287 for (c = 0; c < children; c++)
1288 if ((ret = max_width(zhp, child[c], depth + 2,
1293 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1294 &child, &children) == 0) {
1295 for (c = 0; c < children; c++)
1296 if ((ret = max_width(zhp, child[c], depth + 2,
1305 typedef struct spare_cbdata {
1307 zpool_handle_t *cb_zhp;
1311 find_vdev(nvlist_t *nv, uint64_t search)
1317 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1321 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1322 &child, &children) == 0) {
1323 for (c = 0; c < children; c++)
1324 if (find_vdev(child[c], search))
1332 find_spare(zpool_handle_t *zhp, void *data)
1334 spare_cbdata_t *cbp = data;
1335 nvlist_t *config, *nvroot;
1337 config = zpool_get_config(zhp, NULL);
1338 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1341 if (find_vdev(nvroot, cbp->cb_guid)) {
1351 * Print out configuration state as requested by status_callback.
1354 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1355 int namewidth, int depth, boolean_t isspare)
1359 pool_scan_stat_t *ps = NULL;
1361 char rbuf[6], wbuf[6], cbuf[6];
1363 uint64_t notpresent;
1367 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1368 &child, &children) != 0)
1371 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1372 (uint64_t **)&vs, &c) == 0);
1374 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1377 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1380 if (vs->vs_aux == VDEV_AUX_SPARED)
1382 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1386 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1390 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1391 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1392 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1393 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1396 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1397 ¬present) == 0) {
1399 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1400 (void) printf(" was %s", path);
1401 } else if (vs->vs_aux != 0) {
1404 switch (vs->vs_aux) {
1405 case VDEV_AUX_OPEN_FAILED:
1406 (void) printf(gettext("cannot open"));
1409 case VDEV_AUX_BAD_GUID_SUM:
1410 (void) printf(gettext("missing device"));
1413 case VDEV_AUX_NO_REPLICAS:
1414 (void) printf(gettext("insufficient replicas"));
1417 case VDEV_AUX_VERSION_NEWER:
1418 (void) printf(gettext("newer version"));
1421 case VDEV_AUX_UNSUP_FEAT:
1422 (void) printf(gettext("unsupported feature(s)"));
1425 case VDEV_AUX_SPARED:
1426 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1428 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1429 if (strcmp(zpool_get_name(cb.cb_zhp),
1430 zpool_get_name(zhp)) == 0)
1431 (void) printf(gettext("currently in "
1434 (void) printf(gettext("in use by "
1436 zpool_get_name(cb.cb_zhp));
1437 zpool_close(cb.cb_zhp);
1439 (void) printf(gettext("currently in use"));
1443 case VDEV_AUX_ERR_EXCEEDED:
1444 (void) printf(gettext("too many errors"));
1447 case VDEV_AUX_IO_FAILURE:
1448 (void) printf(gettext("experienced I/O failures"));
1451 case VDEV_AUX_BAD_LOG:
1452 (void) printf(gettext("bad intent log"));
1455 case VDEV_AUX_EXTERNAL:
1456 (void) printf(gettext("external device fault"));
1459 case VDEV_AUX_SPLIT_POOL:
1460 (void) printf(gettext("split into new pool"));
1464 (void) printf(gettext("corrupted data"));
1469 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1470 (uint64_t **)&ps, &c);
1472 if (ps && ps->pss_state == DSS_SCANNING &&
1473 vs->vs_scan_processed != 0 && children == 0) {
1474 (void) printf(gettext(" (%s)"),
1475 (ps->pss_func == POOL_SCAN_RESILVER) ?
1476 "resilvering" : "repairing");
1479 (void) printf("\n");
1481 for (c = 0; c < children; c++) {
1482 uint64_t islog = B_FALSE, ishole = B_FALSE;
1484 /* Don't print logs or holes here */
1485 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1487 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1489 if (islog || ishole)
1491 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1492 print_status_config(zhp, vname, child[c],
1493 namewidth, depth + 2, isspare);
1500 * Print the configuration of an exported pool. Iterate over all vdevs in the
1501 * pool, printing out the name and status for each one.
1504 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1511 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1512 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1513 strcmp(type, VDEV_TYPE_HOLE) == 0)
1516 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1517 (uint64_t **)&vs, &c) == 0);
1519 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1520 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1522 if (vs->vs_aux != 0) {
1525 switch (vs->vs_aux) {
1526 case VDEV_AUX_OPEN_FAILED:
1527 (void) printf(gettext("cannot open"));
1530 case VDEV_AUX_BAD_GUID_SUM:
1531 (void) printf(gettext("missing device"));
1534 case VDEV_AUX_NO_REPLICAS:
1535 (void) printf(gettext("insufficient replicas"));
1538 case VDEV_AUX_VERSION_NEWER:
1539 (void) printf(gettext("newer version"));
1542 case VDEV_AUX_UNSUP_FEAT:
1543 (void) printf(gettext("unsupported feature(s)"));
1546 case VDEV_AUX_ERR_EXCEEDED:
1547 (void) printf(gettext("too many errors"));
1551 (void) printf(gettext("corrupted data"));
1555 (void) printf("\n");
1557 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1558 &child, &children) != 0)
1561 for (c = 0; c < children; c++) {
1562 uint64_t is_log = B_FALSE;
1564 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1569 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1570 print_import_config(vname, child[c], namewidth, depth + 2);
1574 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1575 &child, &children) == 0) {
1576 (void) printf(gettext("\tcache\n"));
1577 for (c = 0; c < children; c++) {
1578 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1579 (void) printf("\t %s\n", vname);
1584 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1585 &child, &children) == 0) {
1586 (void) printf(gettext("\tspares\n"));
1587 for (c = 0; c < children; c++) {
1588 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1589 (void) printf("\t %s\n", vname);
1597 * Logs are recorded as top level vdevs in the main pool child array
1598 * but with "is_log" set to 1. We use either print_status_config() or
1599 * print_import_config() to print the top level logs then any log
1600 * children (eg mirrored slogs) are printed recursively - which
1601 * works because only the top level vdev is marked "is_log"
1604 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1609 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1613 (void) printf(gettext("\tlogs\n"));
1615 for (c = 0; c < children; c++) {
1616 uint64_t is_log = B_FALSE;
1619 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1623 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1625 print_status_config(zhp, name, child[c], namewidth,
1628 print_import_config(name, child[c], namewidth, 2);
1634 * Display the status for the given pool.
1637 show_import(nvlist_t *config)
1639 uint64_t pool_state;
1645 zpool_status_t reason;
1646 zpool_errata_t errata;
1652 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1654 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1656 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1658 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1661 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1662 (uint64_t **)&vs, &vsc) == 0);
1663 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1665 reason = zpool_import_status(config, &msgid, &errata);
1667 (void) printf(gettext(" pool: %s\n"), name);
1668 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1669 (void) printf(gettext(" state: %s"), health);
1670 if (pool_state == POOL_STATE_DESTROYED)
1671 (void) printf(gettext(" (DESTROYED)"));
1672 (void) printf("\n");
1675 case ZPOOL_STATUS_MISSING_DEV_R:
1676 case ZPOOL_STATUS_MISSING_DEV_NR:
1677 case ZPOOL_STATUS_BAD_GUID_SUM:
1678 (void) printf(gettext(" status: One or more devices are "
1679 "missing from the system.\n"));
1682 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1683 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1684 (void) printf(gettext(" status: One or more devices contains "
1685 "corrupted data.\n"));
1688 case ZPOOL_STATUS_CORRUPT_DATA:
1690 gettext(" status: The pool data is corrupted.\n"));
1693 case ZPOOL_STATUS_OFFLINE_DEV:
1694 (void) printf(gettext(" status: One or more devices "
1695 "are offlined.\n"));
1698 case ZPOOL_STATUS_CORRUPT_POOL:
1699 (void) printf(gettext(" status: The pool metadata is "
1703 case ZPOOL_STATUS_VERSION_OLDER:
1704 (void) printf(gettext(" status: The pool is formatted using a "
1705 "legacy on-disk version.\n"));
1708 case ZPOOL_STATUS_VERSION_NEWER:
1709 (void) printf(gettext(" status: The pool is formatted using an "
1710 "incompatible version.\n"));
1713 case ZPOOL_STATUS_FEAT_DISABLED:
1714 (void) printf(gettext(" status: Some supported features are "
1715 "not enabled on the pool.\n"));
1718 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1719 (void) printf(gettext("status: The pool uses the following "
1720 "feature(s) not supported on this sytem:\n"));
1721 zpool_print_unsup_feat(config);
1724 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1725 (void) printf(gettext("status: The pool can only be accessed "
1726 "in read-only mode on this system. It\n\tcannot be "
1727 "accessed in read-write mode because it uses the "
1728 "following\n\tfeature(s) not supported on this system:\n"));
1729 zpool_print_unsup_feat(config);
1732 case ZPOOL_STATUS_HOSTID_MISMATCH:
1733 (void) printf(gettext(" status: The pool was last accessed by "
1734 "another system.\n"));
1737 case ZPOOL_STATUS_FAULTED_DEV_R:
1738 case ZPOOL_STATUS_FAULTED_DEV_NR:
1739 (void) printf(gettext(" status: One or more devices are "
1743 case ZPOOL_STATUS_BAD_LOG:
1744 (void) printf(gettext(" status: An intent log record cannot be "
1748 case ZPOOL_STATUS_RESILVERING:
1749 (void) printf(gettext(" status: One or more devices were being "
1753 case ZPOOL_STATUS_ERRATA:
1754 (void) printf(gettext(" status: Errata #%d detected.\n"),
1760 * No other status can be seen when importing pools.
1762 assert(reason == ZPOOL_STATUS_OK);
1766 * Print out an action according to the overall state of the pool.
1768 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1769 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1770 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1771 (void) printf(gettext(" action: The pool can be "
1772 "imported using its name or numeric identifier, "
1773 "though\n\tsome features will not be available "
1774 "without an explicit 'zpool upgrade'.\n"));
1775 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1776 (void) printf(gettext(" action: The pool can be "
1777 "imported using its name or numeric "
1778 "identifier and\n\tthe '-f' flag.\n"));
1779 } else if (reason == ZPOOL_STATUS_ERRATA) {
1781 case ZPOOL_ERRATA_NONE:
1784 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1785 (void) printf(gettext(" action: The pool can "
1786 "be imported using its name or numeric "
1787 "identifier,\n\thowever there is a compat"
1788 "ibility issue which should be corrected"
1789 "\n\tby running 'zpool scrub'\n"));
1792 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1793 (void) printf(gettext(" action: The pool can"
1794 "not be imported with this version of ZFS "
1795 "due to\n\tan active asynchronous destroy. "
1796 "Revert to an earlier version\n\tand "
1797 "allow the destroy to complete before "
1803 * All errata must contain an action message.
1808 (void) printf(gettext(" action: The pool can be "
1809 "imported using its name or numeric "
1812 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1813 (void) printf(gettext(" action: The pool can be imported "
1814 "despite missing or damaged devices. The\n\tfault "
1815 "tolerance of the pool may be compromised if imported.\n"));
1818 case ZPOOL_STATUS_VERSION_NEWER:
1819 (void) printf(gettext(" action: The pool cannot be "
1820 "imported. Access the pool on a system running "
1821 "newer\n\tsoftware, or recreate the pool from "
1824 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1825 (void) printf(gettext("action: The pool cannot be "
1826 "imported. Access the pool on a system that "
1827 "supports\n\tthe required feature(s), or recreate "
1828 "the pool from backup.\n"));
1830 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1831 (void) printf(gettext("action: The pool cannot be "
1832 "imported in read-write mode. Import the pool "
1834 "\t\"-o readonly=on\", access the pool on a system "
1835 "that supports the\n\trequired feature(s), or "
1836 "recreate the pool from backup.\n"));
1838 case ZPOOL_STATUS_MISSING_DEV_R:
1839 case ZPOOL_STATUS_MISSING_DEV_NR:
1840 case ZPOOL_STATUS_BAD_GUID_SUM:
1841 (void) printf(gettext(" action: The pool cannot be "
1842 "imported. Attach the missing\n\tdevices and try "
1846 (void) printf(gettext(" action: The pool cannot be "
1847 "imported due to damaged devices or data.\n"));
1851 /* Print the comment attached to the pool. */
1852 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1853 (void) printf(gettext("comment: %s\n"), comment);
1856 * If the state is "closed" or "can't open", and the aux state
1857 * is "corrupt data":
1859 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1860 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1861 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1862 if (pool_state == POOL_STATE_DESTROYED)
1863 (void) printf(gettext("\tThe pool was destroyed, "
1864 "but can be imported using the '-Df' flags.\n"));
1865 else if (pool_state != POOL_STATE_EXPORTED)
1866 (void) printf(gettext("\tThe pool may be active on "
1867 "another system, but can be imported using\n\t"
1868 "the '-f' flag.\n"));
1872 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1875 (void) printf(gettext(" config:\n\n"));
1877 namewidth = max_width(NULL, nvroot, 0, 0);
1881 print_import_config(name, nvroot, namewidth, 0);
1882 if (num_logs(nvroot) > 0)
1883 print_logs(NULL, nvroot, namewidth, B_FALSE);
1885 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1886 (void) printf(gettext("\n\tAdditional devices are known to "
1887 "be part of this pool, though their\n\texact "
1888 "configuration cannot be determined.\n"));
1893 * Perform the import for the given configuration. This passes the heavy
1894 * lifting off to zpool_import_props(), and then mounts the datasets contained
1898 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1899 nvlist_t *props, int flags)
1901 zpool_handle_t *zhp;
1906 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1909 verify(nvlist_lookup_uint64(config,
1910 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1911 verify(nvlist_lookup_uint64(config,
1912 ZPOOL_CONFIG_VERSION, &version) == 0);
1913 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1914 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1915 "is formatted using an unsupported ZFS version\n"), name);
1917 } else if (state != POOL_STATE_EXPORTED &&
1918 !(flags & ZFS_IMPORT_ANY_HOST)) {
1919 uint64_t hostid = 0;
1920 unsigned long system_hostid = gethostid() & 0xffffffff;
1922 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1925 if (hostid != 0 && (unsigned long)hostid != system_hostid) {
1930 verify(nvlist_lookup_string(config,
1931 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1932 verify(nvlist_lookup_uint64(config,
1933 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1935 (void) fprintf(stderr, gettext("cannot import "
1936 "'%s': pool may be in use from other "
1937 "system, it was last accessed by %s "
1938 "(hostid: 0x%lx) on %s"), name, hostname,
1939 (unsigned long)hostid,
1940 asctime(localtime(&t)));
1941 (void) fprintf(stderr, gettext("use '-f' to "
1942 "import anyway\n"));
1947 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1950 if (newname != NULL)
1951 name = (char *)newname;
1953 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1956 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1957 !(flags & ZFS_IMPORT_ONLY) &&
1958 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1968 * zpool import [-d dir] [-D]
1969 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1970 * [-d dir | -c cachefile] [-f] -a
1971 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1972 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1974 * -c Read pool information from a cachefile instead of searching
1977 * -d Scan in a specific directory, other than /dev/. More than
1978 * one directory can be specified using multiple '-d' options.
1980 * -D Scan for previously destroyed pools or import all or only
1981 * specified destroyed pools.
1983 * -R Temporarily import the pool, with all mountpoints relative to
1984 * the given root. The pool will remain exported when the machine
1987 * -V Import even in the presence of faulted vdevs. This is an
1988 * intentionally undocumented option for testing purposes, and
1989 * treats the pool configuration as complete, leaving any bad
1990 * vdevs in the FAULTED state. In other words, it does verbatim
1993 * -f Force import, even if it appears that the pool is active.
1995 * -F Attempt rewind if necessary.
1997 * -n See if rewind would work, but don't actually rewind.
1999 * -N Import the pool but don't mount datasets.
2001 * -T Specify a starting txg to use for import. This option is
2002 * intentionally undocumented option for testing purposes.
2004 * -a Import all pools found.
2006 * -o Set property=value and/or temporary mount options (without '=').
2008 * The import command scans for pools to import, and import pools based on pool
2009 * name and GUID. The pool can also be renamed as part of the import process.
2012 zpool_do_import(int argc, char **argv)
2014 char **searchdirs = NULL;
2015 char *env, *envdup = NULL;
2019 nvlist_t *pools = NULL;
2020 boolean_t do_all = B_FALSE;
2021 boolean_t do_destroyed = B_FALSE;
2022 char *mntopts = NULL;
2025 uint64_t searchguid = 0;
2026 char *searchname = NULL;
2028 nvlist_t *found_config;
2029 nvlist_t *policy = NULL;
2030 nvlist_t *props = NULL;
2032 int flags = ZFS_IMPORT_NORMAL;
2033 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2034 boolean_t dryrun = B_FALSE;
2035 boolean_t do_rewind = B_FALSE;
2036 boolean_t xtreme_rewind = B_FALSE;
2037 uint64_t pool_state, txg = -1ULL;
2038 char *cachefile = NULL;
2039 importargs_t idata = { 0 };
2043 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2052 if (searchdirs == NULL) {
2053 searchdirs = safe_malloc(sizeof (char *));
2055 char **tmp = safe_malloc((nsearch + 1) *
2057 bcopy(searchdirs, tmp, nsearch *
2062 searchdirs[nsearch++] = optarg;
2065 do_destroyed = B_TRUE;
2068 flags |= ZFS_IMPORT_ANY_HOST;
2074 flags |= ZFS_IMPORT_MISSING_LOG;
2080 flags |= ZFS_IMPORT_ONLY;
2083 if ((propval = strchr(optarg, '=')) != NULL) {
2086 if (add_prop_list(optarg, propval,
2094 if (add_prop_list(zpool_prop_to_name(
2095 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2097 if (add_prop_list_default(zpool_prop_to_name(
2098 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2102 flags |= ZFS_IMPORT_TEMP_NAME;
2103 if (add_prop_list_default(zpool_prop_to_name(
2104 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2110 txg = strtoull(optarg, &endptr, 0);
2111 if (errno != 0 || *endptr != '\0') {
2112 (void) fprintf(stderr,
2113 gettext("invalid txg value\n"));
2116 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2119 flags |= ZFS_IMPORT_VERBATIM;
2122 xtreme_rewind = B_TRUE;
2125 (void) fprintf(stderr, gettext("missing argument for "
2126 "'%c' option\n"), optopt);
2130 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2139 if (cachefile && nsearch != 0) {
2140 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2144 if ((dryrun || xtreme_rewind) && !do_rewind) {
2145 (void) fprintf(stderr,
2146 gettext("-n or -X only meaningful with -F\n"));
2150 rewind_policy = ZPOOL_TRY_REWIND;
2152 rewind_policy = ZPOOL_DO_REWIND;
2154 rewind_policy |= ZPOOL_EXTREME_REWIND;
2156 /* In the future, we can capture further policy and include it here */
2157 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2158 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2159 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2162 /* check argument count */
2165 (void) fprintf(stderr, gettext("too many arguments\n"));
2170 (void) fprintf(stderr, gettext("too many arguments\n"));
2175 * Check for the SYS_CONFIG privilege. We do this explicitly
2176 * here because otherwise any attempt to discover pools will
2179 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2180 (void) fprintf(stderr, gettext("cannot "
2181 "discover pools: permission denied\n"));
2182 if (searchdirs != NULL)
2185 nvlist_free(policy);
2191 * Depending on the arguments given, we do one of the following:
2193 * <none> Iterate through all pools and display information about
2196 * -a Iterate through all pools and try to import each one.
2198 * <id> Find the pool that corresponds to the given GUID/pool
2199 * name and import that one.
2201 * -D Above options applies only to destroyed pools.
2207 searchguid = strtoull(argv[0], &endptr, 10);
2208 if (errno != 0 || *endptr != '\0')
2209 searchname = argv[0];
2210 found_config = NULL;
2213 * User specified a name or guid. Ensure it's unique.
2215 idata.unique = B_TRUE;
2219 * Check the environment for the preferred search path.
2221 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2224 envdup = strdup(env);
2226 dir = strtok(envdup, ":");
2227 while (dir != NULL) {
2228 if (searchdirs == NULL) {
2229 searchdirs = safe_malloc(sizeof (char *));
2231 char **tmp = safe_malloc((nsearch + 1) *
2233 bcopy(searchdirs, tmp, nsearch *
2238 searchdirs[nsearch++] = dir;
2239 dir = strtok(NULL, ":");
2243 idata.path = searchdirs;
2244 idata.paths = nsearch;
2245 idata.poolname = searchname;
2246 idata.guid = searchguid;
2247 idata.cachefile = cachefile;
2249 pools = zpool_search_import(g_zfs, &idata);
2251 if (pools != NULL && idata.exists &&
2252 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2253 (void) fprintf(stderr, gettext("cannot import '%s': "
2254 "a pool with that name already exists\n"),
2256 (void) fprintf(stderr, gettext("use the form '%s "
2257 "<pool | id> <newpool>' to give it a new name\n"),
2260 } else if (pools == NULL && idata.exists) {
2261 (void) fprintf(stderr, gettext("cannot import '%s': "
2262 "a pool with that name is already created/imported,\n"),
2264 (void) fprintf(stderr, gettext("and no additional pools "
2265 "with that name were found\n"));
2267 } else if (pools == NULL) {
2269 (void) fprintf(stderr, gettext("cannot import '%s': "
2270 "no such pool available\n"), argv[0]);
2276 if (searchdirs != NULL)
2280 nvlist_free(policy);
2285 * At this point we have a list of import candidate configs. Even if
2286 * we were searching by pool name or guid, we still need to
2287 * post-process the list to deal with pool state and possible
2293 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2295 verify(nvpair_value_nvlist(elem, &config) == 0);
2297 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2299 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2301 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2304 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2311 (void) printf("\n");
2314 err |= do_import(config, NULL, mntopts,
2317 show_import(config);
2319 } else if (searchname != NULL) {
2323 * We are searching for a pool based on name.
2325 verify(nvlist_lookup_string(config,
2326 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2328 if (strcmp(name, searchname) == 0) {
2329 if (found_config != NULL) {
2330 (void) fprintf(stderr, gettext(
2331 "cannot import '%s': more than "
2332 "one matching pool\n"), searchname);
2333 (void) fprintf(stderr, gettext(
2334 "import by numeric ID instead\n"));
2337 found_config = config;
2343 * Search for a pool by guid.
2345 verify(nvlist_lookup_uint64(config,
2346 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2348 if (guid == searchguid)
2349 found_config = config;
2354 * If we were searching for a specific pool, verify that we found a
2355 * pool, and then do the import.
2357 if (argc != 0 && err == 0) {
2358 if (found_config == NULL) {
2359 (void) fprintf(stderr, gettext("cannot import '%s': "
2360 "no such pool available\n"), argv[0]);
2363 err |= do_import(found_config, argc == 1 ? NULL :
2364 argv[1], mntopts, props, flags);
2369 * If we were just looking for pools, report an error if none were
2372 if (argc == 0 && first)
2373 (void) fprintf(stderr,
2374 gettext("no pools available to import\n"));
2379 nvlist_free(policy);
2380 if (searchdirs != NULL)
2385 return (err ? 1 : 0);
2388 typedef struct iostat_cbdata {
2389 boolean_t cb_verbose;
2392 zpool_list_t *cb_list;
2396 print_iostat_separator(iostat_cbdata_t *cb)
2400 for (i = 0; i < cb->cb_namewidth; i++)
2402 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2406 print_iostat_header(iostat_cbdata_t *cb)
2408 (void) printf("%*s capacity operations bandwidth\n",
2409 cb->cb_namewidth, "");
2410 (void) printf("%-*s alloc free read write read write\n",
2411 cb->cb_namewidth, "pool");
2412 print_iostat_separator(cb);
2416 * Display a single statistic.
2419 print_one_stat(uint64_t value)
2423 zfs_nicenum(value, buf, sizeof (buf));
2424 (void) printf(" %5s", buf);
2428 * Print out all the statistics for the given vdev. This can either be the
2429 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2430 * is a verbose output, and we don't want to display the toplevel pool stats.
2433 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2434 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2436 nvlist_t **oldchild, **newchild;
2438 vdev_stat_t *oldvs, *newvs;
2439 vdev_stat_t zerovs = { 0 };
2444 if (oldnv != NULL) {
2445 verify(nvlist_lookup_uint64_array(oldnv,
2446 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2451 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2452 (uint64_t **)&newvs, &c) == 0);
2454 if (strlen(name) + depth > cb->cb_namewidth)
2455 (void) printf("%*s%s", depth, "", name);
2457 (void) printf("%*s%s%*s", depth, "", name,
2458 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2460 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2465 scale = (double)NANOSEC / tdelta;
2467 /* only toplevel vdevs have capacity stats */
2468 if (newvs->vs_space == 0) {
2469 (void) printf(" - -");
2471 print_one_stat(newvs->vs_alloc);
2472 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2475 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2476 oldvs->vs_ops[ZIO_TYPE_READ])));
2478 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2479 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2481 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2482 oldvs->vs_bytes[ZIO_TYPE_READ])));
2484 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2485 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2487 (void) printf("\n");
2489 if (!cb->cb_verbose)
2492 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2493 &newchild, &children) != 0)
2496 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2497 &oldchild, &c) != 0)
2500 for (c = 0; c < children; c++) {
2501 uint64_t ishole = B_FALSE, islog = B_FALSE;
2503 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2506 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2509 if (ishole || islog)
2512 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2513 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2514 newchild[c], cb, depth + 2);
2519 * Log device section
2522 if (num_logs(newnv) > 0) {
2523 (void) printf("%-*s - - - - - "
2524 "-\n", cb->cb_namewidth, "logs");
2526 for (c = 0; c < children; c++) {
2527 uint64_t islog = B_FALSE;
2528 (void) nvlist_lookup_uint64(newchild[c],
2529 ZPOOL_CONFIG_IS_LOG, &islog);
2532 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2534 print_vdev_stats(zhp, vname, oldnv ?
2535 oldchild[c] : NULL, newchild[c],
2544 * Include level 2 ARC devices in iostat output
2546 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2547 &newchild, &children) != 0)
2550 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2551 &oldchild, &c) != 0)
2555 (void) printf("%-*s - - - - - "
2556 "-\n", cb->cb_namewidth, "cache");
2557 for (c = 0; c < children; c++) {
2558 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2560 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2561 newchild[c], cb, depth + 2);
2568 refresh_iostat(zpool_handle_t *zhp, void *data)
2570 iostat_cbdata_t *cb = data;
2574 * If the pool has disappeared, remove it from the list and continue.
2576 if (zpool_refresh_stats(zhp, &missing) != 0)
2580 pool_list_remove(cb->cb_list, zhp);
2586 * Callback to print out the iostats for the given pool.
2589 print_iostat(zpool_handle_t *zhp, void *data)
2591 iostat_cbdata_t *cb = data;
2592 nvlist_t *oldconfig, *newconfig;
2593 nvlist_t *oldnvroot, *newnvroot;
2595 newconfig = zpool_get_config(zhp, &oldconfig);
2597 if (cb->cb_iteration == 1)
2600 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2603 if (oldconfig == NULL)
2606 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2610 * Print out the statistics for the pool.
2612 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2615 print_iostat_separator(cb);
2627 if (isatty(STDOUT_FILENO)) {
2628 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2630 columns = ws.ws_col;
2639 get_namewidth(zpool_handle_t *zhp, void *data)
2641 iostat_cbdata_t *cb = data;
2642 nvlist_t *config, *nvroot;
2645 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2646 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2648 if (!cb->cb_verbose)
2649 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2651 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2656 * The width must be at least 10, but may be as large as the
2657 * column width - 42 so that we can still fit in one line.
2659 columns = get_columns();
2661 if (cb->cb_namewidth < 10)
2662 cb->cb_namewidth = 10;
2663 if (cb->cb_namewidth > columns - 42)
2664 cb->cb_namewidth = columns - 42;
2670 * Parse the input string, get the 'interval' and 'count' value if there is one.
2673 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2676 unsigned long interval = 0, count = 0;
2680 * Determine if the last argument is an integer or a pool name
2682 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2686 interval = strtoul(argv[argc - 1], &end, 10);
2688 if (*end == '\0' && errno == 0) {
2689 if (interval == 0) {
2690 (void) fprintf(stderr, gettext("interval "
2691 "cannot be zero\n"));
2695 * Ignore the last parameter
2700 * If this is not a valid number, just plow on. The
2701 * user will get a more informative error message later
2709 * If the last argument is also an integer, then we have both a count
2712 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2717 interval = strtoul(argv[argc - 1], &end, 10);
2719 if (*end == '\0' && errno == 0) {
2720 if (interval == 0) {
2721 (void) fprintf(stderr, gettext("interval "
2722 "cannot be zero\n"));
2727 * Ignore the last parameter
2741 get_timestamp_arg(char c)
2744 timestamp_fmt = UDATE;
2746 timestamp_fmt = DDATE;
2752 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2754 * -v Display statistics for individual vdevs
2755 * -T Display a timestamp in date(1) or Unix format
2757 * This command can be tricky because we want to be able to deal with pool
2758 * creation/destruction as well as vdev configuration changes. The bulk of this
2759 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2760 * on pool_list_update() to detect the addition of new pools. Configuration
2761 * changes are all handled within libzfs.
2764 zpool_do_iostat(int argc, char **argv)
2769 unsigned long interval = 0, count = 0;
2771 boolean_t verbose = B_FALSE;
2775 while ((c = getopt(argc, argv, "T:v")) != -1) {
2778 get_timestamp_arg(*optarg);
2784 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2793 get_interval_count(&argc, argv, &interval, &count);
2796 * Construct the list of all interesting pools.
2799 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2802 if (pool_list_count(list) == 0 && argc != 0) {
2803 pool_list_free(list);
2807 if (pool_list_count(list) == 0 && interval == 0) {
2808 pool_list_free(list);
2809 (void) fprintf(stderr, gettext("no pools available\n"));
2814 * Enter the main iostat loop.
2817 cb.cb_verbose = verbose;
2818 cb.cb_iteration = 0;
2819 cb.cb_namewidth = 0;
2822 pool_list_update(list);
2824 if ((npools = pool_list_count(list)) == 0)
2825 (void) fprintf(stderr, gettext("no pools available\n"));
2828 * Refresh all statistics. This is done as an
2829 * explicit step before calculating the maximum name
2830 * width, so that any * configuration changes are
2831 * properly accounted for.
2833 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2837 * Iterate over all pools to determine the maximum width
2838 * for the pool / device name column across all pools.
2840 cb.cb_namewidth = 0;
2841 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2844 if (timestamp_fmt != NODATE)
2845 print_timestamp(timestamp_fmt);
2848 * If it's the first time, or verbose mode, print the
2851 if (++cb.cb_iteration == 1 || verbose)
2852 print_iostat_header(&cb);
2854 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2857 * If there's more than one pool, and we're not in
2858 * verbose mode (which prints a separator for us),
2859 * then print a separator.
2861 if (npools > 1 && !verbose)
2862 print_iostat_separator(&cb);
2865 (void) printf("\n");
2869 * Flush the output so that redirection to a file isn't buffered
2872 (void) fflush(stdout);
2877 if (count != 0 && --count == 0)
2880 (void) sleep(interval);
2883 pool_list_free(list);
2888 typedef struct list_cbdata {
2889 boolean_t cb_verbose;
2891 boolean_t cb_scripted;
2892 zprop_list_t *cb_proplist;
2896 * Given a list of columns to display, output appropriate headers for each one.
2899 print_header(list_cbdata_t *cb)
2901 zprop_list_t *pl = cb->cb_proplist;
2902 char headerbuf[ZPOOL_MAXPROPLEN];
2904 boolean_t first = B_TRUE;
2905 boolean_t right_justify;
2908 for (; pl != NULL; pl = pl->pl_next) {
2909 width = pl->pl_width;
2910 if (first && cb->cb_verbose) {
2912 * Reset the width to accommodate the verbose listing
2915 width = cb->cb_namewidth;
2923 right_justify = B_FALSE;
2924 if (pl->pl_prop != ZPROP_INVAL) {
2925 header = zpool_prop_column_name(pl->pl_prop);
2926 right_justify = zpool_prop_align_right(pl->pl_prop);
2930 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2931 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2932 headerbuf[i] = '\0';
2936 if (pl->pl_next == NULL && !right_justify)
2937 (void) printf("%s", header);
2938 else if (right_justify)
2939 (void) printf("%*s", (int)width, header);
2941 (void) printf("%-*s", (int)width, header);
2944 (void) printf("\n");
2948 * Given a pool and a list of properties, print out all the properties according
2949 * to the described layout.
2952 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2954 zprop_list_t *pl = cb->cb_proplist;
2955 boolean_t first = B_TRUE;
2956 char property[ZPOOL_MAXPROPLEN];
2958 boolean_t right_justify;
2961 for (; pl != NULL; pl = pl->pl_next) {
2963 width = pl->pl_width;
2964 if (first && cb->cb_verbose) {
2966 * Reset the width to accommodate the verbose listing
2969 width = cb->cb_namewidth;
2973 if (cb->cb_scripted)
2974 (void) printf("\t");
2981 right_justify = B_FALSE;
2982 if (pl->pl_prop != ZPROP_INVAL) {
2983 if (zpool_get_prop(zhp, pl->pl_prop, property,
2984 sizeof (property), NULL) != 0)
2989 right_justify = zpool_prop_align_right(pl->pl_prop);
2990 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2991 zpool_prop_unsupported(pl->pl_user_prop)) &&
2992 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2993 sizeof (property)) == 0) {
3001 * If this is being called in scripted mode, or if this is the
3002 * last column and it is left-justified, don't include a width
3005 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3006 (void) printf("%s", propstr);
3007 else if (right_justify)
3008 (void) printf("%*s", (int)width, propstr);
3010 (void) printf("%-*s", (int)width, propstr);
3013 (void) printf("\n");
3017 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3022 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3025 case ZPOOL_PROP_EXPANDSZ:
3027 (void) strlcpy(propval, "-", sizeof (propval));
3029 zfs_nicenum(value, propval, sizeof (propval));
3031 case ZPOOL_PROP_FRAGMENTATION:
3032 if (value == ZFS_FRAG_INVALID) {
3033 (void) strlcpy(propval, "-", sizeof (propval));
3035 (void) snprintf(propval, sizeof (propval), "%llu%%",
3036 (unsigned long long)value);
3039 case ZPOOL_PROP_CAPACITY:
3040 (void) snprintf(propval, sizeof (propval), "%llu%%",
3041 (unsigned long long)value);
3044 zfs_nicenum(value, propval, sizeof (propval));
3048 (void) strlcpy(propval, "-", sizeof (propval));
3051 (void) printf("\t%s", propval);
3053 (void) printf(" %*s", (int)width, propval);
3057 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3058 list_cbdata_t *cb, int depth)
3064 boolean_t scripted = cb->cb_scripted;
3066 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3067 (uint64_t **)&vs, &c) == 0);
3070 boolean_t toplevel = (vs->vs_space != 0);
3074 (void) printf("\t%s", name);
3075 else if (strlen(name) + depth > cb->cb_namewidth)
3076 (void) printf("%*s%s", depth, "", name);
3078 (void) printf("%*s%s%*s", depth, "", name,
3079 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3082 * Print the properties for the individual vdevs. Some
3083 * properties are only applicable to toplevel vdevs. The
3084 * 'toplevel' boolean value is passed to the print_one_column()
3085 * to indicate that the value is valid.
3087 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3089 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3091 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3092 scripted, toplevel);
3093 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3095 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3096 vs->vs_fragmentation, scripted,
3097 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3098 cap = (vs->vs_space == 0) ? 0 :
3099 (vs->vs_alloc * 100 / vs->vs_space);
3100 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3101 (void) printf("\n");
3104 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3105 &child, &children) != 0)
3108 for (c = 0; c < children; c++) {
3109 uint64_t ishole = B_FALSE;
3111 if (nvlist_lookup_uint64(child[c],
3112 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3115 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3116 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3121 * Include level 2 ARC devices in iostat output
3123 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3124 &child, &children) != 0)
3128 (void) printf("%-*s - - - - - "
3129 "-\n", cb->cb_namewidth, "cache");
3130 for (c = 0; c < children; c++) {
3131 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3133 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3141 * Generic callback function to list a pool.
3144 list_callback(zpool_handle_t *zhp, void *data)
3146 list_cbdata_t *cbp = data;
3150 config = zpool_get_config(zhp, NULL);
3152 print_pool(zhp, cbp);
3153 if (!cbp->cb_verbose)
3156 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3158 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3164 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3166 * -H Scripted mode. Don't display headers, and separate properties
3168 * -o List of properties to display. Defaults to
3169 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3170 * "dedupratio,health,altroot"
3171 * -T Display a timestamp in date(1) or Unix format
3173 * List all pools in the system, whether or not they're healthy. Output space
3174 * statistics for each one, as well as health status summary.
3177 zpool_do_list(int argc, char **argv)
3181 list_cbdata_t cb = { 0 };
3182 static char default_props[] =
3183 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3184 "dedupratio,health,altroot";
3185 char *props = default_props;
3186 unsigned long interval = 0, count = 0;
3188 boolean_t first = B_TRUE;
3191 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3194 cb.cb_scripted = B_TRUE;
3200 get_timestamp_arg(*optarg);
3203 cb.cb_verbose = B_TRUE;
3206 (void) fprintf(stderr, gettext("missing argument for "
3207 "'%c' option\n"), optopt);
3211 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3220 get_interval_count(&argc, argv, &interval, &count);
3222 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3225 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3228 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3229 (void) printf(gettext("no pools available\n"));
3230 zprop_free_list(cb.cb_proplist);
3235 pool_list_update(list);
3237 if (pool_list_count(list) == 0)
3240 if (timestamp_fmt != NODATE)
3241 print_timestamp(timestamp_fmt);
3243 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3247 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3252 if (count != 0 && --count == 0)
3255 (void) sleep(interval);
3258 zprop_free_list(cb.cb_proplist);
3263 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3265 boolean_t force = B_FALSE;
3268 char *poolname, *old_disk, *new_disk;
3269 zpool_handle_t *zhp;
3270 nvlist_t *props = NULL;
3275 while ((c = getopt(argc, argv, "fo:")) != -1) {
3281 if ((propval = strchr(optarg, '=')) == NULL) {
3282 (void) fprintf(stderr, gettext("missing "
3283 "'=' for -o option\n"));
3289 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3290 (add_prop_list(optarg, propval, &props, B_TRUE)))
3294 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3303 /* get pool name and check number of arguments */
3305 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3312 (void) fprintf(stderr,
3313 gettext("missing <device> specification\n"));
3321 (void) fprintf(stderr,
3322 gettext("missing <new_device> specification\n"));
3325 new_disk = old_disk;
3335 (void) fprintf(stderr, gettext("too many arguments\n"));
3339 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3342 if (zpool_get_config(zhp, NULL) == NULL) {
3343 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3349 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3351 if (nvroot == NULL) {
3356 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3358 nvlist_free(nvroot);
3365 * zpool replace [-f] <pool> <device> <new_device>
3367 * -f Force attach, even if <new_device> appears to be in use.
3369 * Replace <device> with <new_device>.
3373 zpool_do_replace(int argc, char **argv)
3375 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3379 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3381 * -f Force attach, even if <new_device> appears to be in use.
3382 * -o Set property=value.
3384 * Attach <new_device> to the mirror containing <device>. If <device> is not
3385 * part of a mirror, then <device> will be transformed into a mirror of
3386 * <device> and <new_device>. In either case, <new_device> will begin life
3387 * with a DTL of [0, now], and will immediately begin to resilver itself.
3390 zpool_do_attach(int argc, char **argv)
3392 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3396 * zpool detach [-f] <pool> <device>
3398 * -f Force detach of <device>, even if DTLs argue against it
3399 * (not supported yet)
3401 * Detach a device from a mirror. The operation will be refused if <device>
3402 * is the last device in the mirror, or if the DTLs indicate that this device
3403 * has the only valid copy of some data.
3407 zpool_do_detach(int argc, char **argv)
3410 char *poolname, *path;
3411 zpool_handle_t *zhp;
3415 while ((c = getopt(argc, argv, "f")) != -1) {
3419 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3428 /* get pool name and check number of arguments */
3430 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3435 (void) fprintf(stderr,
3436 gettext("missing <device> specification\n"));
3443 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3446 ret = zpool_vdev_detach(zhp, path);
3454 * zpool split [-n] [-o prop=val] ...
3456 * [-R altroot] <pool> <newpool> [<device> ...]
3458 * -n Do not split the pool, but display the resulting layout if
3459 * it were to be split.
3460 * -o Set property=value, or set mount options.
3461 * -R Mount the split-off pool under an alternate root.
3463 * Splits the named pool and gives it the new pool name. Devices to be split
3464 * off may be listed, provided that no more than one device is specified
3465 * per top-level vdev mirror. The newly split pool is left in an exported
3466 * state unless -R is specified.
3468 * Restrictions: the top-level of the pool pool must only be made up of
3469 * mirrors; all devices in the pool must be healthy; no device may be
3470 * undergoing a resilvering operation.
3473 zpool_do_split(int argc, char **argv)
3475 char *srcpool, *newpool, *propval;
3476 char *mntopts = NULL;
3479 zpool_handle_t *zhp;
3480 nvlist_t *config, *props = NULL;
3482 flags.dryrun = B_FALSE;
3483 flags.import = B_FALSE;
3486 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3489 flags.import = B_TRUE;
3491 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3492 &props, B_TRUE) != 0) {
3499 flags.dryrun = B_TRUE;
3502 if ((propval = strchr(optarg, '=')) != NULL) {
3505 if (add_prop_list(optarg, propval,
3506 &props, B_TRUE) != 0) {
3516 (void) fprintf(stderr, gettext("missing argument for "
3517 "'%c' option\n"), optopt);
3521 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3528 if (!flags.import && mntopts != NULL) {
3529 (void) fprintf(stderr, gettext("setting mntopts is only "
3530 "valid when importing the pool\n"));
3538 (void) fprintf(stderr, gettext("Missing pool name\n"));
3542 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3552 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3555 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3556 if (config == NULL) {
3560 (void) printf(gettext("would create '%s' with the "
3561 "following layout:\n\n"), newpool);
3562 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3564 nvlist_free(config);
3569 if (ret != 0 || flags.dryrun || !flags.import)
3573 * The split was successful. Now we need to open the new
3574 * pool and import it.
3576 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3578 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3579 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3581 (void) fprintf(stderr, gettext("Split was successful, but "
3582 "the datasets could not all be mounted\n"));
3583 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3584 "different altroot\n"), "zpool import");
3594 * zpool online <pool> <device> ...
3597 zpool_do_online(int argc, char **argv)
3601 zpool_handle_t *zhp;
3603 vdev_state_t newstate;
3607 while ((c = getopt(argc, argv, "et")) != -1) {
3610 flags |= ZFS_ONLINE_EXPAND;
3614 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3623 /* get pool name and check number of arguments */
3625 (void) fprintf(stderr, gettext("missing pool name\n"));
3629 (void) fprintf(stderr, gettext("missing device name\n"));
3635 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3638 for (i = 1; i < argc; i++) {
3639 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3640 if (newstate != VDEV_STATE_HEALTHY) {
3641 (void) printf(gettext("warning: device '%s' "
3642 "onlined, but remains in faulted state\n"),
3644 if (newstate == VDEV_STATE_FAULTED)
3645 (void) printf(gettext("use 'zpool "
3646 "clear' to restore a faulted "
3649 (void) printf(gettext("use 'zpool "
3650 "replace' to replace devices "
3651 "that are no longer present\n"));
3664 * zpool offline [-ft] <pool> <device> ...
3666 * -f Force the device into the offline state, even if doing
3667 * so would appear to compromise pool availability.
3668 * (not supported yet)
3670 * -t Only take the device off-line temporarily. The offline
3671 * state will not be persistent across reboots.
3675 zpool_do_offline(int argc, char **argv)
3679 zpool_handle_t *zhp;
3681 boolean_t istmp = B_FALSE;
3684 while ((c = getopt(argc, argv, "ft")) != -1) {
3691 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3700 /* get pool name and check number of arguments */
3702 (void) fprintf(stderr, gettext("missing pool name\n"));
3706 (void) fprintf(stderr, gettext("missing device name\n"));
3712 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3715 for (i = 1; i < argc; i++) {
3716 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3726 * zpool clear <pool> [device]
3728 * Clear all errors associated with a pool or a particular device.
3731 zpool_do_clear(int argc, char **argv)
3735 boolean_t dryrun = B_FALSE;
3736 boolean_t do_rewind = B_FALSE;
3737 boolean_t xtreme_rewind = B_FALSE;
3738 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3739 nvlist_t *policy = NULL;
3740 zpool_handle_t *zhp;
3741 char *pool, *device;
3744 while ((c = getopt(argc, argv, "FnX")) != -1) {
3753 xtreme_rewind = B_TRUE;
3756 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3766 (void) fprintf(stderr, gettext("missing pool name\n"));
3771 (void) fprintf(stderr, gettext("too many arguments\n"));
3775 if ((dryrun || xtreme_rewind) && !do_rewind) {
3776 (void) fprintf(stderr,
3777 gettext("-n or -X only meaningful with -F\n"));
3781 rewind_policy = ZPOOL_TRY_REWIND;
3783 rewind_policy = ZPOOL_DO_REWIND;
3785 rewind_policy |= ZPOOL_EXTREME_REWIND;
3787 /* In future, further rewind policy choices can be passed along here */
3788 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3789 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3793 device = argc == 2 ? argv[1] : NULL;
3795 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3796 nvlist_free(policy);
3800 if (zpool_clear(zhp, device, policy) != 0)
3805 nvlist_free(policy);
3811 * zpool reguid <pool>
3814 zpool_do_reguid(int argc, char **argv)
3818 zpool_handle_t *zhp;
3822 while ((c = getopt(argc, argv, "")) != -1) {
3825 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3834 /* get pool name and check number of arguments */
3836 (void) fprintf(stderr, gettext("missing pool name\n"));
3841 (void) fprintf(stderr, gettext("too many arguments\n"));
3846 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3849 ret = zpool_reguid(zhp);
3857 * zpool reopen <pool>
3859 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3862 zpool_do_reopen(int argc, char **argv)
3866 zpool_handle_t *zhp;
3870 while ((c = getopt(argc, argv, "")) != -1) {
3873 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3883 (void) fprintf(stderr, gettext("missing pool name\n"));
3888 (void) fprintf(stderr, gettext("too many arguments\n"));
3893 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3896 ret = zpool_reopen(zhp);
3901 typedef struct scrub_cbdata {
3908 scrub_callback(zpool_handle_t *zhp, void *data)
3910 scrub_cbdata_t *cb = data;
3914 * Ignore faulted pools.
3916 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3917 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3918 "currently unavailable\n"), zpool_get_name(zhp));
3922 err = zpool_scan(zhp, cb->cb_type);
3928 * zpool scrub [-s] <pool> ...
3930 * -s Stop. Stops any in-progress scrub.
3933 zpool_do_scrub(int argc, char **argv)
3938 cb.cb_type = POOL_SCAN_SCRUB;
3941 while ((c = getopt(argc, argv, "s")) != -1) {
3944 cb.cb_type = POOL_SCAN_NONE;
3947 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3959 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3963 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3966 typedef struct status_cbdata {
3968 boolean_t cb_allpools;
3969 boolean_t cb_verbose;
3970 boolean_t cb_explain;
3972 boolean_t cb_dedup_stats;
3976 * Print out detailed scrub status.
3979 print_scan_status(pool_scan_stat_t *ps)
3982 uint64_t elapsed, mins_left, hours_left;
3983 uint64_t pass_exam, examined, total;
3985 double fraction_done;
3986 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3988 (void) printf(gettext(" scan: "));
3990 /* If there's never been a scan, there's not much to say. */
3991 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3992 ps->pss_func >= POOL_SCAN_FUNCS) {
3993 (void) printf(gettext("none requested\n"));
3997 start = ps->pss_start_time;
3998 end = ps->pss_end_time;
3999 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4001 assert(ps->pss_func == POOL_SCAN_SCRUB ||
4002 ps->pss_func == POOL_SCAN_RESILVER);
4004 * Scan is finished or canceled.
4006 if (ps->pss_state == DSS_FINISHED) {
4007 uint64_t minutes_taken = (end - start) / 60;
4010 if (ps->pss_func == POOL_SCAN_SCRUB) {
4011 fmt = gettext("scrub repaired %s in %lluh%um with "
4012 "%llu errors on %s");
4013 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4014 fmt = gettext("resilvered %s in %lluh%um with "
4015 "%llu errors on %s");
4018 (void) printf(fmt, processed_buf,
4019 (u_longlong_t)(minutes_taken / 60),
4020 (uint_t)(minutes_taken % 60),
4021 (u_longlong_t)ps->pss_errors,
4022 ctime((time_t *)&end));
4024 } else if (ps->pss_state == DSS_CANCELED) {
4025 if (ps->pss_func == POOL_SCAN_SCRUB) {
4026 (void) printf(gettext("scrub canceled on %s"),
4028 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4029 (void) printf(gettext("resilver canceled on %s"),
4035 assert(ps->pss_state == DSS_SCANNING);
4038 * Scan is in progress.
4040 if (ps->pss_func == POOL_SCAN_SCRUB) {
4041 (void) printf(gettext("scrub in progress since %s"),
4043 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4044 (void) printf(gettext("resilver in progress since %s"),
4048 examined = ps->pss_examined ? ps->pss_examined : 1;
4049 total = ps->pss_to_examine;
4050 fraction_done = (double)examined / total;
4052 /* elapsed time for this pass */
4053 elapsed = time(NULL) - ps->pss_pass_start;
4054 elapsed = elapsed ? elapsed : 1;
4055 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4056 rate = pass_exam / elapsed;
4057 rate = rate ? rate : 1;
4058 mins_left = ((total - examined) / rate) / 60;
4059 hours_left = mins_left / 60;
4061 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4062 zfs_nicenum(total, total_buf, sizeof (total_buf));
4063 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4066 * do not print estimated time if hours_left is more than 30 days
4068 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4069 examined_buf, total_buf, rate_buf);
4070 if (hours_left < (30 * 24)) {
4071 (void) printf(gettext(", %lluh%um to go\n"),
4072 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4074 (void) printf(gettext(
4075 ", (scan is slow, no estimated time)\n"));
4078 if (ps->pss_func == POOL_SCAN_RESILVER) {
4079 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4080 processed_buf, 100 * fraction_done);
4081 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4082 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4083 processed_buf, 100 * fraction_done);
4088 print_error_log(zpool_handle_t *zhp)
4090 nvlist_t *nverrlist = NULL;
4093 size_t len = MAXPATHLEN * 2;
4095 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4096 (void) printf("errors: List of errors unavailable "
4097 "(insufficient privileges)\n");
4101 (void) printf("errors: Permanent errors have been "
4102 "detected in the following files:\n\n");
4104 pathname = safe_malloc(len);
4106 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4108 uint64_t dsobj, obj;
4110 verify(nvpair_value_nvlist(elem, &nv) == 0);
4111 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4113 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4115 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4116 (void) printf("%7s %s\n", "", pathname);
4119 nvlist_free(nverrlist);
4123 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4132 (void) printf(gettext("\tspares\n"));
4134 for (i = 0; i < nspares; i++) {
4135 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4136 print_status_config(zhp, name, spares[i],
4137 namewidth, 2, B_TRUE);
4143 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4152 (void) printf(gettext("\tcache\n"));
4154 for (i = 0; i < nl2cache; i++) {
4155 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4156 print_status_config(zhp, name, l2cache[i],
4157 namewidth, 2, B_FALSE);
4163 print_dedup_stats(nvlist_t *config)
4165 ddt_histogram_t *ddh;
4171 * If the pool was faulted then we may not have been able to
4172 * obtain the config. Otherwise, if we have anything in the dedup
4173 * table continue processing the stats.
4175 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4176 (uint64_t **)&ddo, &c) != 0)
4179 (void) printf("\n");
4180 (void) printf(gettext(" dedup: "));
4181 if (ddo->ddo_count == 0) {
4182 (void) printf(gettext("no DDT entries\n"));
4186 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4187 (u_longlong_t)ddo->ddo_count,
4188 (u_longlong_t)ddo->ddo_dspace,
4189 (u_longlong_t)ddo->ddo_mspace);
4191 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4192 (uint64_t **)&dds, &c) == 0);
4193 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4194 (uint64_t **)&ddh, &c) == 0);
4195 zpool_dump_ddt(dds, ddh);
4199 * Display a summary of pool status. Displays a summary such as:
4203 * reason: One or more devices ...
4204 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4210 * When given the '-v' option, we print out the complete config. If the '-e'
4211 * option is specified, then we print out error rate information as well.
4214 status_callback(zpool_handle_t *zhp, void *data)
4216 status_cbdata_t *cbp = data;
4217 nvlist_t *config, *nvroot;
4219 zpool_status_t reason;
4220 zpool_errata_t errata;
4225 config = zpool_get_config(zhp, NULL);
4226 reason = zpool_get_status(zhp, &msgid, &errata);
4231 * If we were given 'zpool status -x', only report those pools with
4234 if (cbp->cb_explain &&
4235 (reason == ZPOOL_STATUS_OK ||
4236 reason == ZPOOL_STATUS_VERSION_OLDER ||
4237 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4238 if (!cbp->cb_allpools) {
4239 (void) printf(gettext("pool '%s' is healthy\n"),
4240 zpool_get_name(zhp));
4242 cbp->cb_first = B_FALSE;
4248 cbp->cb_first = B_FALSE;
4250 (void) printf("\n");
4252 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4254 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4255 (uint64_t **)&vs, &c) == 0);
4256 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4258 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4259 (void) printf(gettext(" state: %s\n"), health);
4262 case ZPOOL_STATUS_MISSING_DEV_R:
4263 (void) printf(gettext("status: One or more devices could not "
4264 "be opened. Sufficient replicas exist for\n\tthe pool to "
4265 "continue functioning in a degraded state.\n"));
4266 (void) printf(gettext("action: Attach the missing device and "
4267 "online it using 'zpool online'.\n"));
4270 case ZPOOL_STATUS_MISSING_DEV_NR:
4271 (void) printf(gettext("status: One or more devices could not "
4272 "be opened. There are insufficient\n\treplicas for the "
4273 "pool to continue functioning.\n"));
4274 (void) printf(gettext("action: Attach the missing device and "
4275 "online it using 'zpool online'.\n"));
4278 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4279 (void) printf(gettext("status: One or more devices could not "
4280 "be used because the label is missing or\n\tinvalid. "
4281 "Sufficient replicas exist for the pool to continue\n\t"
4282 "functioning in a degraded state.\n"));
4283 (void) printf(gettext("action: Replace the device using "
4284 "'zpool replace'.\n"));
4287 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4288 (void) printf(gettext("status: One or more devices could not "
4289 "be used because the label is missing \n\tor invalid. "
4290 "There are insufficient replicas for the pool to "
4291 "continue\n\tfunctioning.\n"));
4292 zpool_explain_recover(zpool_get_handle(zhp),
4293 zpool_get_name(zhp), reason, config);
4296 case ZPOOL_STATUS_FAILING_DEV:
4297 (void) printf(gettext("status: One or more devices has "
4298 "experienced an unrecoverable error. An\n\tattempt was "
4299 "made to correct the error. Applications are "
4301 (void) printf(gettext("action: Determine if the device needs "
4302 "to be replaced, and clear the errors\n\tusing "
4303 "'zpool clear' or replace the device with 'zpool "
4307 case ZPOOL_STATUS_OFFLINE_DEV:
4308 (void) printf(gettext("status: One or more devices has "
4309 "been taken offline by the administrator.\n\tSufficient "
4310 "replicas exist for the pool to continue functioning in "
4311 "a\n\tdegraded state.\n"));
4312 (void) printf(gettext("action: Online the device using "
4313 "'zpool online' or replace the device with\n\t'zpool "
4317 case ZPOOL_STATUS_REMOVED_DEV:
4318 (void) printf(gettext("status: One or more devices has "
4319 "been removed by the administrator.\n\tSufficient "
4320 "replicas exist for the pool to continue functioning in "
4321 "a\n\tdegraded state.\n"));
4322 (void) printf(gettext("action: Online the device using "
4323 "'zpool online' or replace the device with\n\t'zpool "
4327 case ZPOOL_STATUS_RESILVERING:
4328 (void) printf(gettext("status: One or more devices is "
4329 "currently being resilvered. The pool will\n\tcontinue "
4330 "to function, possibly in a degraded state.\n"));
4331 (void) printf(gettext("action: Wait for the resilver to "
4335 case ZPOOL_STATUS_CORRUPT_DATA:
4336 (void) printf(gettext("status: One or more devices has "
4337 "experienced an error resulting in data\n\tcorruption. "
4338 "Applications may be affected.\n"));
4339 (void) printf(gettext("action: Restore the file in question "
4340 "if possible. Otherwise restore the\n\tentire pool from "
4344 case ZPOOL_STATUS_CORRUPT_POOL:
4345 (void) printf(gettext("status: The pool metadata is corrupted "
4346 "and the pool cannot be opened.\n"));
4347 zpool_explain_recover(zpool_get_handle(zhp),
4348 zpool_get_name(zhp), reason, config);
4351 case ZPOOL_STATUS_VERSION_OLDER:
4352 (void) printf(gettext("status: The pool is formatted using a "
4353 "legacy on-disk format. The pool can\n\tstill be used, "
4354 "but some features are unavailable.\n"));
4355 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4356 "upgrade'. Once this is done, the\n\tpool will no longer "
4357 "be accessible on software that does not support\n\t"
4358 "feature flags.\n"));
4361 case ZPOOL_STATUS_VERSION_NEWER:
4362 (void) printf(gettext("status: The pool has been upgraded to a "
4363 "newer, incompatible on-disk version.\n\tThe pool cannot "
4364 "be accessed on this system.\n"));
4365 (void) printf(gettext("action: Access the pool from a system "
4366 "running more recent software, or\n\trestore the pool from "
4370 case ZPOOL_STATUS_FEAT_DISABLED:
4371 (void) printf(gettext("status: Some supported features are not "
4372 "enabled on the pool. The pool can\n\tstill be used, but "
4373 "some features are unavailable.\n"));
4374 (void) printf(gettext("action: Enable all features using "
4375 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4376 "longer be accessible by software that does not support\n\t"
4377 "the features. See zpool-features(5) for details.\n"));
4380 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4381 (void) printf(gettext("status: The pool cannot be accessed on "
4382 "this system because it uses the\n\tfollowing feature(s) "
4383 "not supported on this system:\n"));
4384 zpool_print_unsup_feat(config);
4385 (void) printf("\n");
4386 (void) printf(gettext("action: Access the pool from a system "
4387 "that supports the required feature(s),\n\tor restore the "
4388 "pool from backup.\n"));
4391 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4392 (void) printf(gettext("status: The pool can only be accessed "
4393 "in read-only mode on this system. It\n\tcannot be "
4394 "accessed in read-write mode because it uses the "
4395 "following\n\tfeature(s) not supported on this system:\n"));
4396 zpool_print_unsup_feat(config);
4397 (void) printf("\n");
4398 (void) printf(gettext("action: The pool cannot be accessed in "
4399 "read-write mode. Import the pool with\n"
4400 "\t\"-o readonly=on\", access the pool from a system that "
4401 "supports the\n\trequired feature(s), or restore the "
4402 "pool from backup.\n"));
4405 case ZPOOL_STATUS_FAULTED_DEV_R:
4406 (void) printf(gettext("status: One or more devices are "
4407 "faulted in response to persistent errors.\n\tSufficient "
4408 "replicas exist for the pool to continue functioning "
4409 "in a\n\tdegraded state.\n"));
4410 (void) printf(gettext("action: Replace the faulted device, "
4411 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4414 case ZPOOL_STATUS_FAULTED_DEV_NR:
4415 (void) printf(gettext("status: One or more devices are "
4416 "faulted in response to persistent errors. There are "
4417 "insufficient replicas for the pool to\n\tcontinue "
4419 (void) printf(gettext("action: Destroy and re-create the pool "
4420 "from a backup source. Manually marking the device\n"
4421 "\trepaired using 'zpool clear' may allow some data "
4422 "to be recovered.\n"));
4425 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4426 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4427 (void) printf(gettext("status: One or more devices are "
4428 "faulted in response to IO failures.\n"));
4429 (void) printf(gettext("action: Make sure the affected devices "
4430 "are connected, then run 'zpool clear'.\n"));
4433 case ZPOOL_STATUS_BAD_LOG:
4434 (void) printf(gettext("status: An intent log record "
4435 "could not be read.\n"
4436 "\tWaiting for adminstrator intervention to fix the "
4437 "faulted pool.\n"));
4438 (void) printf(gettext("action: Either restore the affected "
4439 "device(s) and run 'zpool online',\n"
4440 "\tor ignore the intent log records by running "
4441 "'zpool clear'.\n"));
4444 case ZPOOL_STATUS_HOSTID_MISMATCH:
4445 (void) printf(gettext("status: Mismatch between pool hostid "
4446 "and system hostid on imported pool.\n\tThis pool was "
4447 "previously imported into a system with a different "
4448 "hostid,\n\tand then was verbatim imported into this "
4450 (void) printf(gettext("action: Export this pool on all systems "
4451 "on which it is imported.\n"
4452 "\tThen import it to correct the mismatch.\n"));
4455 case ZPOOL_STATUS_ERRATA:
4456 (void) printf(gettext("status: Errata #%d detected.\n"),
4460 case ZPOOL_ERRATA_NONE:
4463 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
4464 (void) printf(gettext("action: To correct the issue "
4465 "run 'zpool scrub'.\n"));
4470 * All errata which allow the pool to be imported
4471 * must contain an action message.
4479 * The remaining errors can't actually be generated, yet.
4481 assert(reason == ZPOOL_STATUS_OK);
4485 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4488 if (config != NULL) {
4491 nvlist_t **spares, **l2cache;
4492 uint_t nspares, nl2cache;
4493 pool_scan_stat_t *ps = NULL;
4495 (void) nvlist_lookup_uint64_array(nvroot,
4496 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4497 print_scan_status(ps);
4499 namewidth = max_width(zhp, nvroot, 0, 0);
4503 (void) printf(gettext("config:\n\n"));
4504 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4505 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4506 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4507 namewidth, 0, B_FALSE);
4509 if (num_logs(nvroot) > 0)
4510 print_logs(zhp, nvroot, namewidth, B_TRUE);
4511 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4512 &l2cache, &nl2cache) == 0)
4513 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4515 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4516 &spares, &nspares) == 0)
4517 print_spares(zhp, spares, nspares, namewidth);
4519 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4521 nvlist_t *nverrlist = NULL;
4524 * If the approximate error count is small, get a
4525 * precise count by fetching the entire log and
4526 * uniquifying the results.
4528 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4529 zpool_get_errlog(zhp, &nverrlist) == 0) {
4534 while ((elem = nvlist_next_nvpair(nverrlist,
4539 nvlist_free(nverrlist);
4541 (void) printf("\n");
4544 (void) printf(gettext("errors: No known data "
4546 else if (!cbp->cb_verbose)
4547 (void) printf(gettext("errors: %llu data "
4548 "errors, use '-v' for a list\n"),
4549 (u_longlong_t)nerr);
4551 print_error_log(zhp);
4554 if (cbp->cb_dedup_stats)
4555 print_dedup_stats(config);
4557 (void) printf(gettext("config: The configuration cannot be "
4565 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4567 * -v Display complete error logs
4568 * -x Display only pools with potential problems
4569 * -D Display dedup status (undocumented)
4570 * -T Display a timestamp in date(1) or Unix format
4572 * Describes the health status of all pools or some subset.
4575 zpool_do_status(int argc, char **argv)
4579 unsigned long interval = 0, count = 0;
4580 status_cbdata_t cb = { 0 };
4583 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4586 cb.cb_verbose = B_TRUE;
4589 cb.cb_explain = B_TRUE;
4592 cb.cb_dedup_stats = B_TRUE;
4595 get_timestamp_arg(*optarg);
4598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4607 get_interval_count(&argc, argv, &interval, &count);
4610 cb.cb_allpools = B_TRUE;
4612 cb.cb_first = B_TRUE;
4615 if (timestamp_fmt != NODATE)
4616 print_timestamp(timestamp_fmt);
4618 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4619 status_callback, &cb);
4621 if (argc == 0 && cb.cb_count == 0)
4622 (void) fprintf(stderr, gettext("no pools available\n"));
4623 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4624 (void) printf(gettext("all pools are healthy\n"));
4632 if (count != 0 && --count == 0)
4635 (void) sleep(interval);
4641 typedef struct upgrade_cbdata {
4644 uint64_t cb_version;
4649 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
4651 int zfs_version = (int) zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
4652 int *count = (int *)unsupp_fs;
4654 if (zfs_version > ZPL_VERSION) {
4655 (void) printf(gettext("%s (v%d) is not supported by this "
4656 "implementation of ZFS.\n"),
4657 zfs_get_name(zhp), zfs_version);
4661 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
4669 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4673 uint64_t oldversion;
4676 config = zpool_get_config(zhp, NULL);
4677 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4680 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4681 assert(oldversion < version);
4683 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
4688 (void) fprintf(stderr, gettext("Upgrade not performed due "
4689 "to %d unsupported filesystems (max v%d).\n"),
4690 unsupp_fs, (int) ZPL_VERSION);
4694 ret = zpool_upgrade(zhp, version);
4698 if (version >= SPA_VERSION_FEATURES) {
4699 (void) printf(gettext("Successfully upgraded "
4700 "'%s' from version %llu to feature flags.\n"),
4701 zpool_get_name(zhp), (u_longlong_t) oldversion);
4703 (void) printf(gettext("Successfully upgraded "
4704 "'%s' from version %llu to version %llu.\n"),
4705 zpool_get_name(zhp), (u_longlong_t) oldversion,
4706 (u_longlong_t) version);
4713 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4716 boolean_t firstff = B_TRUE;
4717 nvlist_t *enabled = zpool_get_features(zhp);
4720 for (i = 0; i < SPA_FEATURES; i++) {
4721 const char *fname = spa_feature_table[i].fi_uname;
4722 const char *fguid = spa_feature_table[i].fi_guid;
4723 if (!nvlist_exists(enabled, fguid)) {
4725 verify(-1 != asprintf(&propname, "feature@%s", fname));
4726 ret = zpool_set_prop(zhp, propname,
4727 ZFS_FEATURE_ENABLED);
4735 (void) printf(gettext("Enabled the "
4736 "following features on '%s':\n"),
4737 zpool_get_name(zhp));
4740 (void) printf(gettext(" %s\n"), fname);
4751 upgrade_cb(zpool_handle_t *zhp, void *arg)
4753 upgrade_cbdata_t *cbp = arg;
4756 boolean_t printnl = B_FALSE;
4759 config = zpool_get_config(zhp, NULL);
4760 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4763 assert(SPA_VERSION_IS_SUPPORTED(version));
4765 if (version < cbp->cb_version) {
4766 cbp->cb_first = B_FALSE;
4767 ret = upgrade_version(zhp, cbp->cb_version);
4773 * If they did "zpool upgrade -a", then we could
4774 * be doing ioctls to different pools. We need
4775 * to log this history once to each pool, and bypass
4776 * the normal history logging that happens in main().
4778 (void) zpool_log_history(g_zfs, history_str);
4779 log_history = B_FALSE;
4782 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4784 ret = upgrade_enable_all(zhp, &count);
4789 cbp->cb_first = B_FALSE;
4795 (void) printf(gettext("\n"));
4802 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4804 upgrade_cbdata_t *cbp = arg;
4808 config = zpool_get_config(zhp, NULL);
4809 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4812 assert(SPA_VERSION_IS_SUPPORTED(version));
4814 if (version < SPA_VERSION_FEATURES) {
4815 if (cbp->cb_first) {
4816 (void) printf(gettext("The following pools are "
4817 "formatted with legacy version numbers and can\n"
4818 "be upgraded to use feature flags. After "
4819 "being upgraded, these pools\nwill no "
4820 "longer be accessible by software that does not "
4821 "support feature\nflags.\n\n"));
4822 (void) printf(gettext("VER POOL\n"));
4823 (void) printf(gettext("--- ------------\n"));
4824 cbp->cb_first = B_FALSE;
4827 (void) printf("%2llu %s\n", (u_longlong_t)version,
4828 zpool_get_name(zhp));
4835 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4837 upgrade_cbdata_t *cbp = arg;
4841 config = zpool_get_config(zhp, NULL);
4842 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4845 if (version >= SPA_VERSION_FEATURES) {
4847 boolean_t poolfirst = B_TRUE;
4848 nvlist_t *enabled = zpool_get_features(zhp);
4850 for (i = 0; i < SPA_FEATURES; i++) {
4851 const char *fguid = spa_feature_table[i].fi_guid;
4852 const char *fname = spa_feature_table[i].fi_uname;
4853 if (!nvlist_exists(enabled, fguid)) {
4854 if (cbp->cb_first) {
4855 (void) printf(gettext("\nSome "
4856 "supported features are not "
4857 "enabled on the following pools. "
4858 "Once a\nfeature is enabled the "
4859 "pool may become incompatible with "
4860 "software\nthat does not support "
4862 "zpool-features(5) for "
4864 (void) printf(gettext("POOL "
4866 (void) printf(gettext("------"
4868 cbp->cb_first = B_FALSE;
4872 (void) printf(gettext("%s\n"),
4873 zpool_get_name(zhp));
4874 poolfirst = B_FALSE;
4877 (void) printf(gettext(" %s\n"), fname);
4880 * If they did "zpool upgrade -a", then we could
4881 * be doing ioctls to different pools. We need
4882 * to log this history once to each pool, and bypass
4883 * the normal history logging that happens in main().
4885 (void) zpool_log_history(g_zfs, history_str);
4886 log_history = B_FALSE;
4895 upgrade_one(zpool_handle_t *zhp, void *data)
4897 boolean_t printnl = B_FALSE;
4898 upgrade_cbdata_t *cbp = data;
4899 uint64_t cur_version;
4902 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4903 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
4904 "Pool 'log' must be renamed using export and import"
4909 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4910 if (cur_version > cbp->cb_version) {
4911 (void) printf(gettext("Pool '%s' is already formatted "
4912 "using more current version '%llu'.\n\n"),
4913 zpool_get_name(zhp), (u_longlong_t) cur_version);
4917 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4918 (void) printf(gettext("Pool '%s' is already formatted "
4919 "using version %llu.\n\n"), zpool_get_name(zhp),
4920 (u_longlong_t) cbp->cb_version);
4924 if (cur_version != cbp->cb_version) {
4926 ret = upgrade_version(zhp, cbp->cb_version);
4931 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4933 ret = upgrade_enable_all(zhp, &count);
4939 } else if (cur_version == SPA_VERSION) {
4940 (void) printf(gettext("Pool '%s' already has all "
4941 "supported features enabled.\n"),
4942 zpool_get_name(zhp));
4947 (void) printf(gettext("\n"));
4956 * zpool upgrade [-V version] <-a | pool ...>
4958 * With no arguments, display downrev'd ZFS pool available for upgrade.
4959 * Individual pools can be upgraded by specifying the pool, and '-a' will
4960 * upgrade all pools.
4963 zpool_do_upgrade(int argc, char **argv)
4966 upgrade_cbdata_t cb = { 0 };
4968 boolean_t showversions = B_FALSE;
4969 boolean_t upgradeall = B_FALSE;
4974 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4977 upgradeall = B_TRUE;
4980 showversions = B_TRUE;
4983 cb.cb_version = strtoll(optarg, &end, 10);
4985 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4986 (void) fprintf(stderr,
4987 gettext("invalid version '%s'\n"), optarg);
4992 (void) fprintf(stderr, gettext("missing argument for "
4993 "'%c' option\n"), optopt);
4997 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5008 if (cb.cb_version == 0) {
5009 cb.cb_version = SPA_VERSION;
5010 } else if (!upgradeall && argc == 0) {
5011 (void) fprintf(stderr, gettext("-V option is "
5012 "incompatible with other arguments\n"));
5017 if (upgradeall || argc != 0) {
5018 (void) fprintf(stderr, gettext("-v option is "
5019 "incompatible with other arguments\n"));
5022 } else if (upgradeall) {
5024 (void) fprintf(stderr, gettext("-a option should not "
5025 "be used along with a pool name\n"));
5030 (void) printf(gettext("This system supports ZFS pool feature "
5035 (void) printf(gettext("The following features are "
5037 (void) printf(gettext("FEAT DESCRIPTION\n"));
5038 (void) printf("----------------------------------------------"
5039 "---------------\n");
5040 for (i = 0; i < SPA_FEATURES; i++) {
5041 zfeature_info_t *fi = &spa_feature_table[i];
5042 const char *ro = fi->fi_can_readonly ?
5043 " (read-only compatible)" : "";
5045 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5046 (void) printf(" %s\n", fi->fi_desc);
5048 (void) printf("\n");
5050 (void) printf(gettext("The following legacy versions are also "
5052 (void) printf(gettext("VER DESCRIPTION\n"));
5053 (void) printf("--- -----------------------------------------"
5054 "---------------\n");
5055 (void) printf(gettext(" 1 Initial ZFS version\n"));
5056 (void) printf(gettext(" 2 Ditto blocks "
5057 "(replicated metadata)\n"));
5058 (void) printf(gettext(" 3 Hot spares and double parity "
5060 (void) printf(gettext(" 4 zpool history\n"));
5061 (void) printf(gettext(" 5 Compression using the gzip "
5063 (void) printf(gettext(" 6 bootfs pool property\n"));
5064 (void) printf(gettext(" 7 Separate intent log devices\n"));
5065 (void) printf(gettext(" 8 Delegated administration\n"));
5066 (void) printf(gettext(" 9 refquota and refreservation "
5068 (void) printf(gettext(" 10 Cache devices\n"));
5069 (void) printf(gettext(" 11 Improved scrub performance\n"));
5070 (void) printf(gettext(" 12 Snapshot properties\n"));
5071 (void) printf(gettext(" 13 snapused property\n"));
5072 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5073 (void) printf(gettext(" 15 user/group space accounting\n"));
5074 (void) printf(gettext(" 16 stmf property support\n"));
5075 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5076 (void) printf(gettext(" 18 Snapshot user holds\n"));
5077 (void) printf(gettext(" 19 Log device removal\n"));
5078 (void) printf(gettext(" 20 Compression using zle "
5079 "(zero-length encoding)\n"));
5080 (void) printf(gettext(" 21 Deduplication\n"));
5081 (void) printf(gettext(" 22 Received properties\n"));
5082 (void) printf(gettext(" 23 Slim ZIL\n"));
5083 (void) printf(gettext(" 24 System attributes\n"));
5084 (void) printf(gettext(" 25 Improved scrub stats\n"));
5085 (void) printf(gettext(" 26 Improved snapshot deletion "
5087 (void) printf(gettext(" 27 Improved snapshot creation "
5089 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5090 (void) printf(gettext("\nFor more information on a particular "
5091 "version, including supported releases,\n"));
5092 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5093 } else if (argc == 0 && upgradeall) {
5094 cb.cb_first = B_TRUE;
5095 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5096 if (ret == 0 && cb.cb_first) {
5097 if (cb.cb_version == SPA_VERSION) {
5098 (void) printf(gettext("All pools are already "
5099 "formatted using feature flags.\n\n"));
5100 (void) printf(gettext("Every feature flags "
5101 "pool already has all supported features "
5104 (void) printf(gettext("All pools are already "
5105 "formatted with version %llu or higher.\n"),
5106 (u_longlong_t) cb.cb_version);
5109 } else if (argc == 0) {
5110 cb.cb_first = B_TRUE;
5111 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5115 (void) printf(gettext("All pools are formatted "
5116 "using feature flags.\n\n"));
5118 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5119 "for a list of available legacy versions.\n"));
5122 cb.cb_first = B_TRUE;
5123 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5127 (void) printf(gettext("Every feature flags pool has "
5128 "all supported features enabled.\n"));
5130 (void) printf(gettext("\n"));
5133 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5140 typedef struct hist_cbdata {
5147 * Print out the command history for a specific pool.
5150 get_history_one(zpool_handle_t *zhp, void *data)
5156 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5158 cb->first = B_FALSE;
5160 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5162 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5165 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5166 &records, &numrecords) == 0);
5167 for (i = 0; i < numrecords; i++) {
5168 nvlist_t *rec = records[i];
5171 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5175 tsec = fnvlist_lookup_uint64(records[i],
5177 (void) localtime_r(&tsec, &t);
5178 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5181 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5182 (void) printf("%s %s", tbuf,
5183 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5184 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5186 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5189 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5190 (void) printf("%s unrecognized record:\n",
5192 dump_nvlist(rec, 4);
5195 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5196 zfs_history_event_names[ievent],
5197 (longlong_t) fnvlist_lookup_uint64(
5198 rec, ZPOOL_HIST_TXG),
5199 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5200 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5203 (void) printf("%s [txg:%lld] %s", tbuf,
5204 (longlong_t) fnvlist_lookup_uint64(
5205 rec, ZPOOL_HIST_TXG),
5206 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5207 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5208 (void) printf(" %s (%llu)",
5209 fnvlist_lookup_string(rec,
5211 (u_longlong_t)fnvlist_lookup_uint64(rec,
5214 (void) printf(" %s", fnvlist_lookup_string(rec,
5215 ZPOOL_HIST_INT_STR));
5216 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5219 (void) printf("%s ioctl %s\n", tbuf,
5220 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5221 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5222 (void) printf(" input:\n");
5223 dump_nvlist(fnvlist_lookup_nvlist(rec,
5224 ZPOOL_HIST_INPUT_NVL), 8);
5226 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5227 (void) printf(" output:\n");
5228 dump_nvlist(fnvlist_lookup_nvlist(rec,
5229 ZPOOL_HIST_OUTPUT_NVL), 8);
5234 (void) printf("%s unrecognized record:\n", tbuf);
5235 dump_nvlist(rec, 4);
5239 (void) printf("\n");
5242 (void) printf(" [");
5243 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5244 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5245 struct passwd *pwd = getpwuid(who);
5246 (void) printf("user %d ", (int)who);
5248 (void) printf("(%s) ", pwd->pw_name);
5250 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5251 (void) printf("on %s",
5252 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5254 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5255 (void) printf(":%s",
5256 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5260 (void) printf("\n");
5262 (void) printf("\n");
5269 * zpool history <pool>
5271 * Displays the history of commands that modified pools.
5274 zpool_do_history(int argc, char **argv)
5276 hist_cbdata_t cbdata = { 0 };
5280 cbdata.first = B_TRUE;
5282 while ((c = getopt(argc, argv, "li")) != -1) {
5285 cbdata.longfmt = B_TRUE;
5288 cbdata.internal = B_TRUE;
5291 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5299 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5302 if (argc == 0 && cbdata.first == B_TRUE) {
5303 (void) fprintf(stderr, gettext("no pools available\n"));
5310 typedef struct ev_opts {
5318 zpool_do_events_short(nvlist_t *nvl)
5320 char ctime_str[26], str[32], *ptr;
5324 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5325 memset(str, ' ', 32);
5326 (void) ctime_r((const time_t *)&tv[0], ctime_str);
5327 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
5328 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
5329 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
5330 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
5331 (void) printf(gettext("%s "), str);
5333 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5334 (void) printf(gettext("%s\n"), ptr);
5338 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5342 for (nvp = nvlist_next_nvpair(nvl, NULL);
5343 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5345 data_type_t type = nvpair_type(nvp);
5346 const char *name = nvpair_name(nvp);
5356 printf(gettext("%*s%s = "), depth, "", name);
5359 case DATA_TYPE_BOOLEAN:
5360 printf(gettext("%s"), "1");
5363 case DATA_TYPE_BOOLEAN_VALUE:
5364 (void) nvpair_value_boolean_value(nvp, &b);
5365 printf(gettext("%s"), b ? "1" : "0");
5368 case DATA_TYPE_BYTE:
5369 (void) nvpair_value_byte(nvp, &i8);
5370 printf(gettext("0x%x"), i8);
5373 case DATA_TYPE_INT8:
5374 (void) nvpair_value_int8(nvp, (void *)&i8);
5375 printf(gettext("0x%x"), i8);
5378 case DATA_TYPE_UINT8:
5379 (void) nvpair_value_uint8(nvp, &i8);
5380 printf(gettext("0x%x"), i8);
5383 case DATA_TYPE_INT16:
5384 (void) nvpair_value_int16(nvp, (void *)&i16);
5385 printf(gettext("0x%x"), i16);
5388 case DATA_TYPE_UINT16:
5389 (void) nvpair_value_uint16(nvp, &i16);
5390 printf(gettext("0x%x"), i16);
5393 case DATA_TYPE_INT32:
5394 (void) nvpair_value_int32(nvp, (void *)&i32);
5395 printf(gettext("0x%x"), i32);
5398 case DATA_TYPE_UINT32:
5399 (void) nvpair_value_uint32(nvp, &i32);
5400 printf(gettext("0x%x"), i32);
5403 case DATA_TYPE_INT64:
5404 (void) nvpair_value_int64(nvp, (void *)&i64);
5405 printf(gettext("0x%llx"), (u_longlong_t)i64);
5408 case DATA_TYPE_UINT64:
5409 (void) nvpair_value_uint64(nvp, &i64);
5410 printf(gettext("0x%llx"), (u_longlong_t)i64);
5413 case DATA_TYPE_HRTIME:
5414 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5415 printf(gettext("0x%llx"), (u_longlong_t)i64);
5418 case DATA_TYPE_STRING:
5419 (void) nvpair_value_string(nvp, &str);
5420 printf(gettext("\"%s\""), str ? str : "<NULL>");
5423 case DATA_TYPE_NVLIST:
5424 printf(gettext("(embedded nvlist)\n"));
5425 (void) nvpair_value_nvlist(nvp, &cnv);
5426 zpool_do_events_nvprint(cnv, depth + 8);
5427 printf(gettext("%*s(end %s)"), depth, "", name);
5430 case DATA_TYPE_NVLIST_ARRAY: {
5434 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5435 printf(gettext("(%d embedded nvlists)\n"), nelem);
5436 for (i = 0; i < nelem; i++) {
5437 printf(gettext("%*s%s[%d] = %s\n"),
5438 depth, "", name, i, "(embedded nvlist)");
5439 zpool_do_events_nvprint(val[i], depth + 8);
5440 printf(gettext("%*s(end %s[%i])\n"),
5441 depth, "", name, i);
5443 printf(gettext("%*s(end %s)\n"), depth, "", name);
5447 case DATA_TYPE_INT8_ARRAY: {
5451 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5452 for (i = 0; i < nelem; i++)
5453 printf(gettext("0x%x "), val[i]);
5458 case DATA_TYPE_UINT8_ARRAY: {
5462 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5463 for (i = 0; i < nelem; i++)
5464 printf(gettext("0x%x "), val[i]);
5469 case DATA_TYPE_INT16_ARRAY: {
5473 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5474 for (i = 0; i < nelem; i++)
5475 printf(gettext("0x%x "), val[i]);
5480 case DATA_TYPE_UINT16_ARRAY: {
5484 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5485 for (i = 0; i < nelem; i++)
5486 printf(gettext("0x%x "), val[i]);
5491 case DATA_TYPE_INT32_ARRAY: {
5495 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5496 for (i = 0; i < nelem; i++)
5497 printf(gettext("0x%x "), val[i]);
5502 case DATA_TYPE_UINT32_ARRAY: {
5506 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5507 for (i = 0; i < nelem; i++)
5508 printf(gettext("0x%x "), val[i]);
5513 case DATA_TYPE_INT64_ARRAY: {
5517 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5518 for (i = 0; i < nelem; i++)
5519 printf(gettext("0x%llx "),
5520 (u_longlong_t)val[i]);
5525 case DATA_TYPE_UINT64_ARRAY: {
5529 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5530 for (i = 0; i < nelem; i++)
5531 printf(gettext("0x%llx "),
5532 (u_longlong_t)val[i]);
5537 case DATA_TYPE_STRING_ARRAY: {
5541 (void) nvpair_value_string_array(nvp, &str, &nelem);
5542 for (i = 0; i < nelem; i++)
5543 printf(gettext("\"%s\" "),
5544 str[i] ? str[i] : "<NULL>");
5549 case DATA_TYPE_BOOLEAN_ARRAY:
5550 case DATA_TYPE_BYTE_ARRAY:
5551 case DATA_TYPE_DOUBLE:
5552 case DATA_TYPE_UNKNOWN:
5553 printf(gettext("<unknown>"));
5557 printf(gettext("\n"));
5562 zpool_do_events_next(ev_opts_t *opts)
5565 int zevent_fd, ret, dropped;
5567 zevent_fd = open(ZFS_DEV, O_RDWR);
5568 VERIFY(zevent_fd >= 0);
5570 if (!opts->scripted)
5571 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5574 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5575 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
5576 if (ret || nvl == NULL)
5580 (void) printf(gettext("dropped %d events\n"), dropped);
5582 zpool_do_events_short(nvl);
5584 if (opts->verbose) {
5585 zpool_do_events_nvprint(nvl, 8);
5586 printf(gettext("\n"));
5588 (void) fflush(stdout);
5593 VERIFY(0 == close(zevent_fd));
5599 zpool_do_events_clear(ev_opts_t *opts)
5603 ret = zpool_events_clear(g_zfs, &count);
5605 (void) printf(gettext("cleared %d events\n"), count);
5611 * zpool events [-vfc]
5613 * Displays events logs by ZFS.
5616 zpool_do_events(int argc, char **argv)
5618 ev_opts_t opts = { 0 };
5623 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5638 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5647 ret = zpool_do_events_clear(&opts);
5649 ret = zpool_do_events_next(&opts);
5655 get_callback(zpool_handle_t *zhp, void *data)
5657 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5658 char value[MAXNAMELEN];
5659 zprop_source_t srctype;
5662 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5665 * Skip the special fake placeholder. This will also skip
5666 * over the name property when 'all' is specified.
5668 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5669 pl == cbp->cb_proplist)
5672 if (pl->pl_prop == ZPROP_INVAL &&
5673 (zpool_prop_feature(pl->pl_user_prop) ||
5674 zpool_prop_unsupported(pl->pl_user_prop))) {
5675 srctype = ZPROP_SRC_LOCAL;
5677 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5678 value, sizeof (value)) == 0) {
5679 zprop_print_one_property(zpool_get_name(zhp),
5680 cbp, pl->pl_user_prop, value, srctype,
5684 if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5685 sizeof (value), &srctype, cbp->cb_literal) != 0)
5688 zprop_print_one_property(zpool_get_name(zhp), cbp,
5689 zpool_prop_to_name(pl->pl_prop), value, srctype,
5697 zpool_do_get(int argc, char **argv)
5699 zprop_get_cbdata_t cb = { 0 };
5700 zprop_list_t fake_name = { 0 };
5704 while ((c = getopt(argc, argv, "pH")) != -1) {
5707 cb.cb_literal = B_TRUE;
5711 cb.cb_scripted = B_TRUE;
5715 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5725 (void) fprintf(stderr, gettext("missing property "
5730 cb.cb_first = B_TRUE;
5731 cb.cb_sources = ZPROP_SRC_ALL;
5732 cb.cb_columns[0] = GET_COL_NAME;
5733 cb.cb_columns[1] = GET_COL_PROPERTY;
5734 cb.cb_columns[2] = GET_COL_VALUE;
5735 cb.cb_columns[3] = GET_COL_SOURCE;
5736 cb.cb_type = ZFS_TYPE_POOL;
5738 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5744 if (cb.cb_proplist != NULL) {
5745 fake_name.pl_prop = ZPOOL_PROP_NAME;
5746 fake_name.pl_width = strlen(gettext("NAME"));
5747 fake_name.pl_next = cb.cb_proplist;
5748 cb.cb_proplist = &fake_name;
5751 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5754 if (cb.cb_proplist == &fake_name)
5755 zprop_free_list(fake_name.pl_next);
5757 zprop_free_list(cb.cb_proplist);
5762 typedef struct set_cbdata {
5765 boolean_t cb_any_successful;
5769 set_callback(zpool_handle_t *zhp, void *data)
5772 set_cbdata_t *cb = (set_cbdata_t *)data;
5774 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5777 cb->cb_any_successful = B_TRUE;
5783 zpool_do_set(int argc, char **argv)
5785 set_cbdata_t cb = { 0 };
5788 if (argc > 1 && argv[1][0] == '-') {
5789 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5795 (void) fprintf(stderr, gettext("missing property=value "
5801 (void) fprintf(stderr, gettext("missing pool name\n"));
5806 (void) fprintf(stderr, gettext("too many pool names\n"));
5810 cb.cb_propname = argv[1];
5811 cb.cb_value = strchr(cb.cb_propname, '=');
5812 if (cb.cb_value == NULL) {
5813 (void) fprintf(stderr, gettext("missing value in "
5814 "property=value argument\n"));
5818 *(cb.cb_value) = '\0';
5821 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5828 find_command_idx(char *command, int *idx)
5832 for (i = 0; i < NCOMMAND; i++) {
5833 if (command_table[i].name == NULL)
5836 if (strcmp(command, command_table[i].name) == 0) {
5845 main(int argc, char **argv)
5851 (void) setlocale(LC_ALL, "");
5852 (void) textdomain(TEXT_DOMAIN);
5857 * Make sure the user has specified some command.
5860 (void) fprintf(stderr, gettext("missing command\n"));
5869 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
5872 if ((g_zfs = libzfs_init()) == NULL)
5875 libzfs_print_on_error(g_zfs, B_TRUE);
5877 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5880 * Run the appropriate command.
5882 if (find_command_idx(cmdname, &i) == 0) {
5883 current_command = &command_table[i];
5884 ret = command_table[i].func(argc - 1, argv + 1);
5885 } else if (strchr(cmdname, '=')) {
5886 verify(find_command_idx("set", &i) == 0);
5887 current_command = &command_table[i];
5888 ret = command_table[i].func(argc, argv);
5889 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5891 * 'freeze' is a vile debugging abomination, so we treat
5895 int fd = open(ZFS_DEV, O_RDWR);
5896 (void) strcpy((void *)buf, argv[2]);
5897 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5899 (void) fprintf(stderr, gettext("unrecognized "
5900 "command '%s'\n"), cmdname);
5905 if (ret == 0 && log_history)
5906 (void) zpool_log_history(g_zfs, history_str);
5911 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5912 * for the purposes of running ::findleaks.
5914 if (getenv("ZFS_ABORT") != NULL) {
5915 (void) printf("dumping core by request\n");