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;
818 char *altroot = NULL;
819 char *mountpoint = NULL;
820 nvlist_t *fsprops = NULL;
821 nvlist_t *props = NULL;
825 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
834 enable_all_pool_feat = B_FALSE;
838 if (add_prop_list(zpool_prop_to_name(
839 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
841 if (add_prop_list_default(zpool_prop_to_name(
842 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
846 /* Equivalent to -O mountpoint=optarg */
850 if ((propval = strchr(optarg, '=')) == NULL) {
851 (void) fprintf(stderr, gettext("missing "
852 "'=' for -o option\n"));
858 if (add_prop_list(optarg, propval, &props, B_TRUE))
862 * If the user is creating a pool that doesn't support
863 * feature flags, don't enable any features.
865 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
869 ver = strtoull(propval, &end, 10);
871 ver < SPA_VERSION_FEATURES) {
872 enable_all_pool_feat = B_FALSE;
877 if ((propval = strchr(optarg, '=')) == NULL) {
878 (void) fprintf(stderr, gettext("missing "
879 "'=' for -O option\n"));
886 * Mountpoints are checked and then added later.
887 * Uniquely among properties, they can be specified
888 * more than once, to avoid conflict with -m.
890 if (0 == strcmp(optarg,
891 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
892 mountpoint = propval;
893 } else if (add_prop_list(optarg, propval, &fsprops,
899 (void) fprintf(stderr, gettext("missing argument for "
900 "'%c' option\n"), optopt);
903 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
912 /* get pool name and check number of arguments */
914 (void) fprintf(stderr, gettext("missing pool name argument\n"));
918 (void) fprintf(stderr, gettext("missing vdev specification\n"));
925 * As a special case, check for use of '/' in the name, and direct the
926 * user to use 'zfs create' instead.
928 if (strchr(poolname, '/') != NULL) {
929 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
930 "character '/' in pool name\n"), poolname);
931 (void) fprintf(stderr, gettext("use 'zfs create' to "
932 "create a dataset\n"));
936 /* pass off to get_vdev_spec for bulk processing */
937 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
942 /* make_root_vdev() allows 0 toplevel children if there are spares */
943 if (!zfs_allocatable_devs(nvroot)) {
944 (void) fprintf(stderr, gettext("invalid vdev "
945 "specification: at least one toplevel vdev must be "
950 if (altroot != NULL && altroot[0] != '/') {
951 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
952 "must be an absolute path\n"), altroot);
957 * Check the validity of the mountpoint and direct the user to use the
958 * '-m' mountpoint option if it looks like its in use.
960 if (mountpoint == NULL ||
961 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
962 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
963 char buf[MAXPATHLEN];
966 if (mountpoint && mountpoint[0] != '/') {
967 (void) fprintf(stderr, gettext("invalid mountpoint "
968 "'%s': must be an absolute path, 'legacy', or "
969 "'none'\n"), mountpoint);
973 if (mountpoint == NULL) {
975 (void) snprintf(buf, sizeof (buf), "%s/%s",
978 (void) snprintf(buf, sizeof (buf), "/%s",
982 (void) snprintf(buf, sizeof (buf), "%s%s",
983 altroot, mountpoint);
985 (void) snprintf(buf, sizeof (buf), "%s",
989 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
990 (void) fprintf(stderr, gettext("mountpoint '%s' : "
991 "%s\n"), buf, strerror(errno));
992 (void) fprintf(stderr, gettext("use '-m' "
993 "option to provide a different default\n"));
998 while (count < 3 && readdir(dirp) != NULL)
1000 (void) closedir(dirp);
1003 (void) fprintf(stderr, gettext("mountpoint "
1004 "'%s' exists and is not empty\n"), buf);
1005 (void) fprintf(stderr, gettext("use '-m' "
1006 "option to provide a "
1007 "different default\n"));
1014 * Now that the mountpoint's validity has been checked, ensure that
1015 * the property is set appropriately prior to creating the pool.
1017 if (mountpoint != NULL) {
1018 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1019 mountpoint, &fsprops, B_FALSE);
1027 * For a dry run invocation, print out a basic message and run
1028 * through all the vdevs in the list and print out in an
1029 * appropriate hierarchy.
1031 (void) printf(gettext("would create '%s' with the "
1032 "following layout:\n\n"), poolname);
1034 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1035 if (num_logs(nvroot) > 0)
1036 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1041 * Hand off to libzfs.
1043 if (enable_all_pool_feat) {
1045 for (i = 0; i < SPA_FEATURES; i++) {
1046 char propname[MAXPATHLEN];
1047 zfeature_info_t *feat = &spa_feature_table[i];
1049 (void) snprintf(propname, sizeof (propname),
1050 "feature@%s", feat->fi_uname);
1053 * Skip feature if user specified it manually
1054 * on the command line.
1056 if (nvlist_exists(props, propname))
1059 ret = add_prop_list(propname,
1060 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1067 if (zpool_create(g_zfs, poolname,
1068 nvroot, props, fsprops) == 0) {
1069 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1070 ZFS_TYPE_FILESYSTEM);
1072 if (zfs_mount(pool, NULL, 0) == 0)
1073 ret = zfs_shareall(pool);
1076 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1077 (void) fprintf(stderr, gettext("pool name may have "
1083 nvlist_free(nvroot);
1084 nvlist_free(fsprops);
1088 nvlist_free(fsprops);
1095 * zpool destroy <pool>
1097 * -f Forcefully unmount any datasets
1099 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1102 zpool_do_destroy(int argc, char **argv)
1104 boolean_t force = B_FALSE;
1107 zpool_handle_t *zhp;
1111 while ((c = getopt(argc, argv, "f")) != -1) {
1117 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1126 /* check arguments */
1128 (void) fprintf(stderr, gettext("missing pool argument\n"));
1132 (void) fprintf(stderr, gettext("too many arguments\n"));
1138 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1140 * As a special case, check for use of '/' in the name, and
1141 * direct the user to use 'zfs destroy' instead.
1143 if (strchr(pool, '/') != NULL)
1144 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1145 "destroy a dataset\n"));
1149 if (zpool_disable_datasets(zhp, force) != 0) {
1150 (void) fprintf(stderr, gettext("could not destroy '%s': "
1151 "could not unmount datasets\n"), zpool_get_name(zhp));
1155 /* The history must be logged as part of the export */
1156 log_history = B_FALSE;
1158 ret = (zpool_destroy(zhp, history_str) != 0);
1166 * zpool export [-f] <pool> ...
1168 * -f Forcefully unmount datasets
1170 * Export the given pools. By default, the command will attempt to cleanly
1171 * unmount any active datasets within the pool. If the '-f' flag is specified,
1172 * then the datasets will be forcefully unmounted.
1175 zpool_do_export(int argc, char **argv)
1177 boolean_t force = B_FALSE;
1178 boolean_t hardforce = B_FALSE;
1180 zpool_handle_t *zhp;
1185 while ((c = getopt(argc, argv, "fF")) != -1) {
1194 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1203 /* check arguments */
1205 (void) fprintf(stderr, gettext("missing pool argument\n"));
1210 for (i = 0; i < argc; i++) {
1211 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1216 if (zpool_disable_datasets(zhp, force) != 0) {
1222 /* The history must be logged as part of the export */
1223 log_history = B_FALSE;
1226 if (zpool_export_force(zhp, history_str) != 0)
1228 } else if (zpool_export(zhp, force, history_str) != 0) {
1239 * Given a vdev configuration, determine the maximum width needed for the device
1243 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1245 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1250 if (strlen(name) + depth > max)
1251 max = strlen(name) + depth;
1255 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1256 &child, &children) == 0) {
1257 for (c = 0; c < children; c++)
1258 if ((ret = max_width(zhp, child[c], depth + 2,
1263 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1264 &child, &children) == 0) {
1265 for (c = 0; c < children; c++)
1266 if ((ret = max_width(zhp, child[c], depth + 2,
1271 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1272 &child, &children) == 0) {
1273 for (c = 0; c < children; c++)
1274 if ((ret = max_width(zhp, child[c], depth + 2,
1283 typedef struct spare_cbdata {
1285 zpool_handle_t *cb_zhp;
1289 find_vdev(nvlist_t *nv, uint64_t search)
1295 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1299 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1300 &child, &children) == 0) {
1301 for (c = 0; c < children; c++)
1302 if (find_vdev(child[c], search))
1310 find_spare(zpool_handle_t *zhp, void *data)
1312 spare_cbdata_t *cbp = data;
1313 nvlist_t *config, *nvroot;
1315 config = zpool_get_config(zhp, NULL);
1316 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1319 if (find_vdev(nvroot, cbp->cb_guid)) {
1329 * Print out configuration state as requested by status_callback.
1332 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1333 int namewidth, int depth, boolean_t isspare)
1337 pool_scan_stat_t *ps = NULL;
1339 char rbuf[6], wbuf[6], cbuf[6];
1341 uint64_t notpresent;
1345 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1346 &child, &children) != 0)
1349 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1350 (uint64_t **)&vs, &c) == 0);
1352 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1355 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1358 if (vs->vs_aux == VDEV_AUX_SPARED)
1360 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1364 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1368 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1369 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1370 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1371 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1374 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1375 ¬present) == 0) {
1377 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1378 (void) printf(" was %s", path);
1379 } else if (vs->vs_aux != 0) {
1382 switch (vs->vs_aux) {
1383 case VDEV_AUX_OPEN_FAILED:
1384 (void) printf(gettext("cannot open"));
1387 case VDEV_AUX_BAD_GUID_SUM:
1388 (void) printf(gettext("missing device"));
1391 case VDEV_AUX_NO_REPLICAS:
1392 (void) printf(gettext("insufficient replicas"));
1395 case VDEV_AUX_VERSION_NEWER:
1396 (void) printf(gettext("newer version"));
1399 case VDEV_AUX_UNSUP_FEAT:
1400 (void) printf(gettext("unsupported feature(s)"));
1403 case VDEV_AUX_SPARED:
1404 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1406 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1407 if (strcmp(zpool_get_name(cb.cb_zhp),
1408 zpool_get_name(zhp)) == 0)
1409 (void) printf(gettext("currently in "
1412 (void) printf(gettext("in use by "
1414 zpool_get_name(cb.cb_zhp));
1415 zpool_close(cb.cb_zhp);
1417 (void) printf(gettext("currently in use"));
1421 case VDEV_AUX_ERR_EXCEEDED:
1422 (void) printf(gettext("too many errors"));
1425 case VDEV_AUX_IO_FAILURE:
1426 (void) printf(gettext("experienced I/O failures"));
1429 case VDEV_AUX_BAD_LOG:
1430 (void) printf(gettext("bad intent log"));
1433 case VDEV_AUX_EXTERNAL:
1434 (void) printf(gettext("external device fault"));
1437 case VDEV_AUX_SPLIT_POOL:
1438 (void) printf(gettext("split into new pool"));
1442 (void) printf(gettext("corrupted data"));
1447 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1448 (uint64_t **)&ps, &c);
1450 if (ps && ps->pss_state == DSS_SCANNING &&
1451 vs->vs_scan_processed != 0 && children == 0) {
1452 (void) printf(gettext(" (%s)"),
1453 (ps->pss_func == POOL_SCAN_RESILVER) ?
1454 "resilvering" : "repairing");
1457 (void) printf("\n");
1459 for (c = 0; c < children; c++) {
1460 uint64_t islog = B_FALSE, ishole = B_FALSE;
1462 /* Don't print logs or holes here */
1463 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1465 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1467 if (islog || ishole)
1469 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1470 print_status_config(zhp, vname, child[c],
1471 namewidth, depth + 2, isspare);
1478 * Print the configuration of an exported pool. Iterate over all vdevs in the
1479 * pool, printing out the name and status for each one.
1482 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1489 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1490 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1491 strcmp(type, VDEV_TYPE_HOLE) == 0)
1494 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1495 (uint64_t **)&vs, &c) == 0);
1497 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1498 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1500 if (vs->vs_aux != 0) {
1503 switch (vs->vs_aux) {
1504 case VDEV_AUX_OPEN_FAILED:
1505 (void) printf(gettext("cannot open"));
1508 case VDEV_AUX_BAD_GUID_SUM:
1509 (void) printf(gettext("missing device"));
1512 case VDEV_AUX_NO_REPLICAS:
1513 (void) printf(gettext("insufficient replicas"));
1516 case VDEV_AUX_VERSION_NEWER:
1517 (void) printf(gettext("newer version"));
1520 case VDEV_AUX_UNSUP_FEAT:
1521 (void) printf(gettext("unsupported feature(s)"));
1524 case VDEV_AUX_ERR_EXCEEDED:
1525 (void) printf(gettext("too many errors"));
1529 (void) printf(gettext("corrupted data"));
1533 (void) printf("\n");
1535 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1536 &child, &children) != 0)
1539 for (c = 0; c < children; c++) {
1540 uint64_t is_log = B_FALSE;
1542 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1547 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1548 print_import_config(vname, child[c], namewidth, depth + 2);
1552 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1553 &child, &children) == 0) {
1554 (void) printf(gettext("\tcache\n"));
1555 for (c = 0; c < children; c++) {
1556 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1557 (void) printf("\t %s\n", vname);
1562 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1563 &child, &children) == 0) {
1564 (void) printf(gettext("\tspares\n"));
1565 for (c = 0; c < children; c++) {
1566 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1567 (void) printf("\t %s\n", vname);
1575 * Logs are recorded as top level vdevs in the main pool child array
1576 * but with "is_log" set to 1. We use either print_status_config() or
1577 * print_import_config() to print the top level logs then any log
1578 * children (eg mirrored slogs) are printed recursively - which
1579 * works because only the top level vdev is marked "is_log"
1582 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1587 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1591 (void) printf(gettext("\tlogs\n"));
1593 for (c = 0; c < children; c++) {
1594 uint64_t is_log = B_FALSE;
1597 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1601 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1603 print_status_config(zhp, name, child[c], namewidth,
1606 print_import_config(name, child[c], namewidth, 2);
1612 * Display the status for the given pool.
1615 show_import(nvlist_t *config)
1617 uint64_t pool_state;
1623 zpool_status_t reason;
1624 zpool_errata_t errata;
1630 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1632 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1634 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1636 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1639 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1640 (uint64_t **)&vs, &vsc) == 0);
1641 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1643 reason = zpool_import_status(config, &msgid, &errata);
1645 (void) printf(gettext(" pool: %s\n"), name);
1646 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1647 (void) printf(gettext(" state: %s"), health);
1648 if (pool_state == POOL_STATE_DESTROYED)
1649 (void) printf(gettext(" (DESTROYED)"));
1650 (void) printf("\n");
1653 case ZPOOL_STATUS_MISSING_DEV_R:
1654 case ZPOOL_STATUS_MISSING_DEV_NR:
1655 case ZPOOL_STATUS_BAD_GUID_SUM:
1656 (void) printf(gettext(" status: One or more devices are "
1657 "missing from the system.\n"));
1660 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1661 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1662 (void) printf(gettext(" status: One or more devices contains "
1663 "corrupted data.\n"));
1666 case ZPOOL_STATUS_CORRUPT_DATA:
1668 gettext(" status: The pool data is corrupted.\n"));
1671 case ZPOOL_STATUS_OFFLINE_DEV:
1672 (void) printf(gettext(" status: One or more devices "
1673 "are offlined.\n"));
1676 case ZPOOL_STATUS_CORRUPT_POOL:
1677 (void) printf(gettext(" status: The pool metadata is "
1681 case ZPOOL_STATUS_VERSION_OLDER:
1682 (void) printf(gettext(" status: The pool is formatted using a "
1683 "legacy on-disk version.\n"));
1686 case ZPOOL_STATUS_VERSION_NEWER:
1687 (void) printf(gettext(" status: The pool is formatted using an "
1688 "incompatible version.\n"));
1691 case ZPOOL_STATUS_FEAT_DISABLED:
1692 (void) printf(gettext(" status: Some supported features are "
1693 "not enabled on the pool.\n"));
1696 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1697 (void) printf(gettext("status: The pool uses the following "
1698 "feature(s) not supported on this sytem:\n"));
1699 zpool_print_unsup_feat(config);
1702 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1703 (void) printf(gettext("status: The pool can only be accessed "
1704 "in read-only mode on this system. It\n\tcannot be "
1705 "accessed in read-write mode because it uses the "
1706 "following\n\tfeature(s) not supported on this system:\n"));
1707 zpool_print_unsup_feat(config);
1710 case ZPOOL_STATUS_HOSTID_MISMATCH:
1711 (void) printf(gettext(" status: The pool was last accessed by "
1712 "another system.\n"));
1715 case ZPOOL_STATUS_FAULTED_DEV_R:
1716 case ZPOOL_STATUS_FAULTED_DEV_NR:
1717 (void) printf(gettext(" status: One or more devices are "
1721 case ZPOOL_STATUS_BAD_LOG:
1722 (void) printf(gettext(" status: An intent log record cannot be "
1726 case ZPOOL_STATUS_RESILVERING:
1727 (void) printf(gettext(" status: One or more devices were being "
1731 case ZPOOL_STATUS_ERRATA:
1732 (void) printf(gettext(" status: Errata #%d detected.\n"),
1738 * No other status can be seen when importing pools.
1740 assert(reason == ZPOOL_STATUS_OK);
1744 * Print out an action according to the overall state of the pool.
1746 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1747 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1748 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1749 (void) printf(gettext(" action: The pool can be "
1750 "imported using its name or numeric identifier, "
1751 "though\n\tsome features will not be available "
1752 "without an explicit 'zpool upgrade'.\n"));
1753 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1754 (void) printf(gettext(" action: The pool can be "
1755 "imported using its name or numeric "
1756 "identifier and\n\tthe '-f' flag.\n"));
1757 } else if (reason == ZPOOL_STATUS_ERRATA) {
1759 case ZPOOL_ERRATA_NONE:
1762 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1763 (void) printf(gettext(" action: The pool can "
1764 "be imported using its name or numeric "
1765 "identifier,\n\thowever there is a compat"
1766 "ibility issue which should be corrected"
1767 "\n\tby running 'zpool scrub'\n"));
1770 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1771 (void) printf(gettext(" action: The pool can"
1772 "not be imported with this version of ZFS "
1773 "due to\n\tan active asynchronous destroy. "
1774 "Revert to an earlier version\n\tand "
1775 "allow the destroy to complete before "
1781 * All errata must contain an action message.
1786 (void) printf(gettext(" action: The pool can be "
1787 "imported using its name or numeric "
1790 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1791 (void) printf(gettext(" action: The pool can be imported "
1792 "despite missing or damaged devices. The\n\tfault "
1793 "tolerance of the pool may be compromised if imported.\n"));
1796 case ZPOOL_STATUS_VERSION_NEWER:
1797 (void) printf(gettext(" action: The pool cannot be "
1798 "imported. Access the pool on a system running "
1799 "newer\n\tsoftware, or recreate the pool from "
1802 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1803 (void) printf(gettext("action: The pool cannot be "
1804 "imported. Access the pool on a system that "
1805 "supports\n\tthe required feature(s), or recreate "
1806 "the pool from backup.\n"));
1808 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1809 (void) printf(gettext("action: The pool cannot be "
1810 "imported in read-write mode. Import the pool "
1812 "\t\"-o readonly=on\", access the pool on a system "
1813 "that supports the\n\trequired feature(s), or "
1814 "recreate the pool from backup.\n"));
1816 case ZPOOL_STATUS_MISSING_DEV_R:
1817 case ZPOOL_STATUS_MISSING_DEV_NR:
1818 case ZPOOL_STATUS_BAD_GUID_SUM:
1819 (void) printf(gettext(" action: The pool cannot be "
1820 "imported. Attach the missing\n\tdevices and try "
1824 (void) printf(gettext(" action: The pool cannot be "
1825 "imported due to damaged devices or data.\n"));
1829 /* Print the comment attached to the pool. */
1830 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1831 (void) printf(gettext("comment: %s\n"), comment);
1834 * If the state is "closed" or "can't open", and the aux state
1835 * is "corrupt data":
1837 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1838 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1839 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1840 if (pool_state == POOL_STATE_DESTROYED)
1841 (void) printf(gettext("\tThe pool was destroyed, "
1842 "but can be imported using the '-Df' flags.\n"));
1843 else if (pool_state != POOL_STATE_EXPORTED)
1844 (void) printf(gettext("\tThe pool may be active on "
1845 "another system, but can be imported using\n\t"
1846 "the '-f' flag.\n"));
1850 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1853 (void) printf(gettext(" config:\n\n"));
1855 namewidth = max_width(NULL, nvroot, 0, 0);
1859 print_import_config(name, nvroot, namewidth, 0);
1860 if (num_logs(nvroot) > 0)
1861 print_logs(NULL, nvroot, namewidth, B_FALSE);
1863 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1864 (void) printf(gettext("\n\tAdditional devices are known to "
1865 "be part of this pool, though their\n\texact "
1866 "configuration cannot be determined.\n"));
1871 * Perform the import for the given configuration. This passes the heavy
1872 * lifting off to zpool_import_props(), and then mounts the datasets contained
1876 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1877 nvlist_t *props, int flags)
1879 zpool_handle_t *zhp;
1884 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1887 verify(nvlist_lookup_uint64(config,
1888 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1889 verify(nvlist_lookup_uint64(config,
1890 ZPOOL_CONFIG_VERSION, &version) == 0);
1891 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1892 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1893 "is formatted using an unsupported ZFS version\n"), name);
1895 } else if (state != POOL_STATE_EXPORTED &&
1896 !(flags & ZFS_IMPORT_ANY_HOST)) {
1899 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1901 unsigned long system_hostid = gethostid() & 0xffffffff;
1903 if ((unsigned long)hostid != system_hostid) {
1908 verify(nvlist_lookup_string(config,
1909 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1910 verify(nvlist_lookup_uint64(config,
1911 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1913 (void) fprintf(stderr, gettext("cannot import "
1914 "'%s': pool may be in use from other "
1915 "system, it was last accessed by %s "
1916 "(hostid: 0x%lx) on %s"), name, hostname,
1917 (unsigned long)hostid,
1918 asctime(localtime(&t)));
1919 (void) fprintf(stderr, gettext("use '-f' to "
1920 "import anyway\n"));
1924 (void) fprintf(stderr, gettext("cannot import '%s': "
1925 "pool may be in use from other system\n"), name);
1926 (void) fprintf(stderr, gettext("use '-f' to import "
1932 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1935 if (newname != NULL)
1936 name = (char *)newname;
1938 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1941 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1942 !(flags & ZFS_IMPORT_ONLY) &&
1943 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1953 * zpool import [-d dir] [-D]
1954 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1955 * [-d dir | -c cachefile] [-f] -a
1956 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1957 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1959 * -c Read pool information from a cachefile instead of searching
1962 * -d Scan in a specific directory, other than /dev/. More than
1963 * one directory can be specified using multiple '-d' options.
1965 * -D Scan for previously destroyed pools or import all or only
1966 * specified destroyed pools.
1968 * -R Temporarily import the pool, with all mountpoints relative to
1969 * the given root. The pool will remain exported when the machine
1972 * -V Import even in the presence of faulted vdevs. This is an
1973 * intentionally undocumented option for testing purposes, and
1974 * treats the pool configuration as complete, leaving any bad
1975 * vdevs in the FAULTED state. In other words, it does verbatim
1978 * -f Force import, even if it appears that the pool is active.
1980 * -F Attempt rewind if necessary.
1982 * -n See if rewind would work, but don't actually rewind.
1984 * -N Import the pool but don't mount datasets.
1986 * -T Specify a starting txg to use for import. This option is
1987 * intentionally undocumented option for testing purposes.
1989 * -a Import all pools found.
1991 * -o Set property=value and/or temporary mount options (without '=').
1993 * The import command scans for pools to import, and import pools based on pool
1994 * name and GUID. The pool can also be renamed as part of the import process.
1997 zpool_do_import(int argc, char **argv)
1999 char **searchdirs = NULL;
2000 char *env, *envdup = NULL;
2004 nvlist_t *pools = NULL;
2005 boolean_t do_all = B_FALSE;
2006 boolean_t do_destroyed = B_FALSE;
2007 char *mntopts = NULL;
2010 uint64_t searchguid = 0;
2011 char *searchname = NULL;
2013 nvlist_t *found_config;
2014 nvlist_t *policy = NULL;
2015 nvlist_t *props = NULL;
2017 int flags = ZFS_IMPORT_NORMAL;
2018 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2019 boolean_t dryrun = B_FALSE;
2020 boolean_t do_rewind = B_FALSE;
2021 boolean_t xtreme_rewind = B_FALSE;
2022 uint64_t pool_state, txg = -1ULL;
2023 char *cachefile = NULL;
2024 importargs_t idata = { 0 };
2028 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2037 if (searchdirs == NULL) {
2038 searchdirs = safe_malloc(sizeof (char *));
2040 char **tmp = safe_malloc((nsearch + 1) *
2042 bcopy(searchdirs, tmp, nsearch *
2047 searchdirs[nsearch++] = optarg;
2050 do_destroyed = B_TRUE;
2053 flags |= ZFS_IMPORT_ANY_HOST;
2059 flags |= ZFS_IMPORT_MISSING_LOG;
2065 flags |= ZFS_IMPORT_ONLY;
2068 if ((propval = strchr(optarg, '=')) != NULL) {
2071 if (add_prop_list(optarg, propval,
2079 if (add_prop_list(zpool_prop_to_name(
2080 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2082 if (add_prop_list_default(zpool_prop_to_name(
2083 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2087 flags |= ZFS_IMPORT_TEMP_NAME;
2088 if (add_prop_list_default(zpool_prop_to_name(
2089 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2095 txg = strtoull(optarg, &endptr, 0);
2096 if (errno != 0 || *endptr != '\0') {
2097 (void) fprintf(stderr,
2098 gettext("invalid txg value\n"));
2101 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2104 flags |= ZFS_IMPORT_VERBATIM;
2107 xtreme_rewind = B_TRUE;
2110 (void) fprintf(stderr, gettext("missing argument for "
2111 "'%c' option\n"), optopt);
2115 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2124 if (cachefile && nsearch != 0) {
2125 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2129 if ((dryrun || xtreme_rewind) && !do_rewind) {
2130 (void) fprintf(stderr,
2131 gettext("-n or -X only meaningful with -F\n"));
2135 rewind_policy = ZPOOL_TRY_REWIND;
2137 rewind_policy = ZPOOL_DO_REWIND;
2139 rewind_policy |= ZPOOL_EXTREME_REWIND;
2141 /* In the future, we can capture further policy and include it here */
2142 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2143 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2144 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2147 /* check argument count */
2150 (void) fprintf(stderr, gettext("too many arguments\n"));
2155 (void) fprintf(stderr, gettext("too many arguments\n"));
2160 * Check for the SYS_CONFIG privilege. We do this explicitly
2161 * here because otherwise any attempt to discover pools will
2164 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2165 (void) fprintf(stderr, gettext("cannot "
2166 "discover pools: permission denied\n"));
2167 if (searchdirs != NULL)
2170 nvlist_free(policy);
2176 * Depending on the arguments given, we do one of the following:
2178 * <none> Iterate through all pools and display information about
2181 * -a Iterate through all pools and try to import each one.
2183 * <id> Find the pool that corresponds to the given GUID/pool
2184 * name and import that one.
2186 * -D Above options applies only to destroyed pools.
2192 searchguid = strtoull(argv[0], &endptr, 10);
2193 if (errno != 0 || *endptr != '\0')
2194 searchname = argv[0];
2195 found_config = NULL;
2198 * User specified a name or guid. Ensure it's unique.
2200 idata.unique = B_TRUE;
2204 * Check the environment for the preferred search path.
2206 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2209 envdup = strdup(env);
2211 dir = strtok(envdup, ":");
2212 while (dir != NULL) {
2213 if (searchdirs == NULL) {
2214 searchdirs = safe_malloc(sizeof (char *));
2216 char **tmp = safe_malloc((nsearch + 1) *
2218 bcopy(searchdirs, tmp, nsearch *
2223 searchdirs[nsearch++] = dir;
2224 dir = strtok(NULL, ":");
2228 idata.path = searchdirs;
2229 idata.paths = nsearch;
2230 idata.poolname = searchname;
2231 idata.guid = searchguid;
2232 idata.cachefile = cachefile;
2234 pools = zpool_search_import(g_zfs, &idata);
2236 if (pools != NULL && idata.exists &&
2237 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2238 (void) fprintf(stderr, gettext("cannot import '%s': "
2239 "a pool with that name already exists\n"),
2241 (void) fprintf(stderr, gettext("use the form '%s "
2242 "<pool | id> <newpool>' to give it a new name\n"),
2245 } else if (pools == NULL && idata.exists) {
2246 (void) fprintf(stderr, gettext("cannot import '%s': "
2247 "a pool with that name is already created/imported,\n"),
2249 (void) fprintf(stderr, gettext("and no additional pools "
2250 "with that name were found\n"));
2252 } else if (pools == NULL) {
2254 (void) fprintf(stderr, gettext("cannot import '%s': "
2255 "no such pool available\n"), argv[0]);
2261 if (searchdirs != NULL)
2265 nvlist_free(policy);
2270 * At this point we have a list of import candidate configs. Even if
2271 * we were searching by pool name or guid, we still need to
2272 * post-process the list to deal with pool state and possible
2278 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2280 verify(nvpair_value_nvlist(elem, &config) == 0);
2282 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2284 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2286 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2289 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2296 (void) printf("\n");
2299 err |= do_import(config, NULL, mntopts,
2302 show_import(config);
2304 } else if (searchname != NULL) {
2308 * We are searching for a pool based on name.
2310 verify(nvlist_lookup_string(config,
2311 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2313 if (strcmp(name, searchname) == 0) {
2314 if (found_config != NULL) {
2315 (void) fprintf(stderr, gettext(
2316 "cannot import '%s': more than "
2317 "one matching pool\n"), searchname);
2318 (void) fprintf(stderr, gettext(
2319 "import by numeric ID instead\n"));
2322 found_config = config;
2328 * Search for a pool by guid.
2330 verify(nvlist_lookup_uint64(config,
2331 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2333 if (guid == searchguid)
2334 found_config = config;
2339 * If we were searching for a specific pool, verify that we found a
2340 * pool, and then do the import.
2342 if (argc != 0 && err == 0) {
2343 if (found_config == NULL) {
2344 (void) fprintf(stderr, gettext("cannot import '%s': "
2345 "no such pool available\n"), argv[0]);
2348 err |= do_import(found_config, argc == 1 ? NULL :
2349 argv[1], mntopts, props, flags);
2354 * If we were just looking for pools, report an error if none were
2357 if (argc == 0 && first)
2358 (void) fprintf(stderr,
2359 gettext("no pools available to import\n"));
2364 nvlist_free(policy);
2365 if (searchdirs != NULL)
2370 return (err ? 1 : 0);
2373 typedef struct iostat_cbdata {
2374 boolean_t cb_verbose;
2377 zpool_list_t *cb_list;
2381 print_iostat_separator(iostat_cbdata_t *cb)
2385 for (i = 0; i < cb->cb_namewidth; i++)
2387 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2391 print_iostat_header(iostat_cbdata_t *cb)
2393 (void) printf("%*s capacity operations bandwidth\n",
2394 cb->cb_namewidth, "");
2395 (void) printf("%-*s alloc free read write read write\n",
2396 cb->cb_namewidth, "pool");
2397 print_iostat_separator(cb);
2401 * Display a single statistic.
2404 print_one_stat(uint64_t value)
2408 zfs_nicenum(value, buf, sizeof (buf));
2409 (void) printf(" %5s", buf);
2413 * Print out all the statistics for the given vdev. This can either be the
2414 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2415 * is a verbose output, and we don't want to display the toplevel pool stats.
2418 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2419 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2421 nvlist_t **oldchild, **newchild;
2423 vdev_stat_t *oldvs, *newvs;
2424 vdev_stat_t zerovs = { 0 };
2429 if (oldnv != NULL) {
2430 verify(nvlist_lookup_uint64_array(oldnv,
2431 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2436 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2437 (uint64_t **)&newvs, &c) == 0);
2439 if (strlen(name) + depth > cb->cb_namewidth)
2440 (void) printf("%*s%s", depth, "", name);
2442 (void) printf("%*s%s%*s", depth, "", name,
2443 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2445 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2450 scale = (double)NANOSEC / tdelta;
2452 /* only toplevel vdevs have capacity stats */
2453 if (newvs->vs_space == 0) {
2454 (void) printf(" - -");
2456 print_one_stat(newvs->vs_alloc);
2457 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2460 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2461 oldvs->vs_ops[ZIO_TYPE_READ])));
2463 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2464 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2466 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2467 oldvs->vs_bytes[ZIO_TYPE_READ])));
2469 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2470 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2472 (void) printf("\n");
2474 if (!cb->cb_verbose)
2477 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2478 &newchild, &children) != 0)
2481 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2482 &oldchild, &c) != 0)
2485 for (c = 0; c < children; c++) {
2486 uint64_t ishole = B_FALSE, islog = B_FALSE;
2488 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2491 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2494 if (ishole || islog)
2497 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2498 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2499 newchild[c], cb, depth + 2);
2504 * Log device section
2507 if (num_logs(newnv) > 0) {
2508 (void) printf("%-*s - - - - - "
2509 "-\n", cb->cb_namewidth, "logs");
2511 for (c = 0; c < children; c++) {
2512 uint64_t islog = B_FALSE;
2513 (void) nvlist_lookup_uint64(newchild[c],
2514 ZPOOL_CONFIG_IS_LOG, &islog);
2517 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2519 print_vdev_stats(zhp, vname, oldnv ?
2520 oldchild[c] : NULL, newchild[c],
2529 * Include level 2 ARC devices in iostat output
2531 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2532 &newchild, &children) != 0)
2535 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2536 &oldchild, &c) != 0)
2540 (void) printf("%-*s - - - - - "
2541 "-\n", cb->cb_namewidth, "cache");
2542 for (c = 0; c < children; c++) {
2543 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2545 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2546 newchild[c], cb, depth + 2);
2553 refresh_iostat(zpool_handle_t *zhp, void *data)
2555 iostat_cbdata_t *cb = data;
2559 * If the pool has disappeared, remove it from the list and continue.
2561 if (zpool_refresh_stats(zhp, &missing) != 0)
2565 pool_list_remove(cb->cb_list, zhp);
2571 * Callback to print out the iostats for the given pool.
2574 print_iostat(zpool_handle_t *zhp, void *data)
2576 iostat_cbdata_t *cb = data;
2577 nvlist_t *oldconfig, *newconfig;
2578 nvlist_t *oldnvroot, *newnvroot;
2580 newconfig = zpool_get_config(zhp, &oldconfig);
2582 if (cb->cb_iteration == 1)
2585 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2588 if (oldconfig == NULL)
2591 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2595 * Print out the statistics for the pool.
2597 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2600 print_iostat_separator(cb);
2612 if (isatty(STDOUT_FILENO)) {
2613 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2615 columns = ws.ws_col;
2624 get_namewidth(zpool_handle_t *zhp, void *data)
2626 iostat_cbdata_t *cb = data;
2627 nvlist_t *config, *nvroot;
2630 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2631 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2633 if (!cb->cb_verbose)
2634 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2636 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2641 * The width must be at least 10, but may be as large as the
2642 * column width - 42 so that we can still fit in one line.
2644 columns = get_columns();
2646 if (cb->cb_namewidth < 10)
2647 cb->cb_namewidth = 10;
2648 if (cb->cb_namewidth > columns - 42)
2649 cb->cb_namewidth = columns - 42;
2655 * Parse the input string, get the 'interval' and 'count' value if there is one.
2658 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2661 unsigned long interval = 0, count = 0;
2665 * Determine if the last argument is an integer or a pool name
2667 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2671 interval = strtoul(argv[argc - 1], &end, 10);
2673 if (*end == '\0' && errno == 0) {
2674 if (interval == 0) {
2675 (void) fprintf(stderr, gettext("interval "
2676 "cannot be zero\n"));
2680 * Ignore the last parameter
2685 * If this is not a valid number, just plow on. The
2686 * user will get a more informative error message later
2694 * If the last argument is also an integer, then we have both a count
2697 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2702 interval = strtoul(argv[argc - 1], &end, 10);
2704 if (*end == '\0' && errno == 0) {
2705 if (interval == 0) {
2706 (void) fprintf(stderr, gettext("interval "
2707 "cannot be zero\n"));
2712 * Ignore the last parameter
2726 get_timestamp_arg(char c)
2729 timestamp_fmt = UDATE;
2731 timestamp_fmt = DDATE;
2737 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2739 * -v Display statistics for individual vdevs
2740 * -T Display a timestamp in date(1) or Unix format
2742 * This command can be tricky because we want to be able to deal with pool
2743 * creation/destruction as well as vdev configuration changes. The bulk of this
2744 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2745 * on pool_list_update() to detect the addition of new pools. Configuration
2746 * changes are all handled within libzfs.
2749 zpool_do_iostat(int argc, char **argv)
2754 unsigned long interval = 0, count = 0;
2756 boolean_t verbose = B_FALSE;
2760 while ((c = getopt(argc, argv, "T:v")) != -1) {
2763 get_timestamp_arg(*optarg);
2769 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2778 get_interval_count(&argc, argv, &interval, &count);
2781 * Construct the list of all interesting pools.
2784 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2787 if (pool_list_count(list) == 0 && argc != 0) {
2788 pool_list_free(list);
2792 if (pool_list_count(list) == 0 && interval == 0) {
2793 pool_list_free(list);
2794 (void) fprintf(stderr, gettext("no pools available\n"));
2799 * Enter the main iostat loop.
2802 cb.cb_verbose = verbose;
2803 cb.cb_iteration = 0;
2804 cb.cb_namewidth = 0;
2807 pool_list_update(list);
2809 if ((npools = pool_list_count(list)) == 0)
2810 (void) fprintf(stderr, gettext("no pools available\n"));
2813 * Refresh all statistics. This is done as an
2814 * explicit step before calculating the maximum name
2815 * width, so that any * configuration changes are
2816 * properly accounted for.
2818 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2822 * Iterate over all pools to determine the maximum width
2823 * for the pool / device name column across all pools.
2825 cb.cb_namewidth = 0;
2826 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2829 if (timestamp_fmt != NODATE)
2830 print_timestamp(timestamp_fmt);
2833 * If it's the first time, or verbose mode, print the
2836 if (++cb.cb_iteration == 1 || verbose)
2837 print_iostat_header(&cb);
2839 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2842 * If there's more than one pool, and we're not in
2843 * verbose mode (which prints a separator for us),
2844 * then print a separator.
2846 if (npools > 1 && !verbose)
2847 print_iostat_separator(&cb);
2850 (void) printf("\n");
2854 * Flush the output so that redirection to a file isn't buffered
2857 (void) fflush(stdout);
2862 if (count != 0 && --count == 0)
2865 (void) sleep(interval);
2868 pool_list_free(list);
2873 typedef struct list_cbdata {
2874 boolean_t cb_verbose;
2876 boolean_t cb_scripted;
2877 zprop_list_t *cb_proplist;
2881 * Given a list of columns to display, output appropriate headers for each one.
2884 print_header(list_cbdata_t *cb)
2886 zprop_list_t *pl = cb->cb_proplist;
2887 char headerbuf[ZPOOL_MAXPROPLEN];
2889 boolean_t first = B_TRUE;
2890 boolean_t right_justify;
2893 for (; pl != NULL; pl = pl->pl_next) {
2894 width = pl->pl_width;
2895 if (first && cb->cb_verbose) {
2897 * Reset the width to accommodate the verbose listing
2900 width = cb->cb_namewidth;
2908 right_justify = B_FALSE;
2909 if (pl->pl_prop != ZPROP_INVAL) {
2910 header = zpool_prop_column_name(pl->pl_prop);
2911 right_justify = zpool_prop_align_right(pl->pl_prop);
2915 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2916 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2917 headerbuf[i] = '\0';
2921 if (pl->pl_next == NULL && !right_justify)
2922 (void) printf("%s", header);
2923 else if (right_justify)
2924 (void) printf("%*s", (int)width, header);
2926 (void) printf("%-*s", (int)width, header);
2929 (void) printf("\n");
2933 * Given a pool and a list of properties, print out all the properties according
2934 * to the described layout.
2937 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2939 zprop_list_t *pl = cb->cb_proplist;
2940 boolean_t first = B_TRUE;
2941 char property[ZPOOL_MAXPROPLEN];
2943 boolean_t right_justify;
2946 for (; pl != NULL; pl = pl->pl_next) {
2948 width = pl->pl_width;
2949 if (first && cb->cb_verbose) {
2951 * Reset the width to accommodate the verbose listing
2954 width = cb->cb_namewidth;
2958 if (cb->cb_scripted)
2959 (void) printf("\t");
2966 right_justify = B_FALSE;
2967 if (pl->pl_prop != ZPROP_INVAL) {
2968 if (zpool_get_prop(zhp, pl->pl_prop, property,
2969 sizeof (property), NULL) != 0)
2974 right_justify = zpool_prop_align_right(pl->pl_prop);
2975 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2976 zpool_prop_unsupported(pl->pl_user_prop)) &&
2977 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2978 sizeof (property)) == 0) {
2986 * If this is being called in scripted mode, or if this is the
2987 * last column and it is left-justified, don't include a width
2990 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2991 (void) printf("%s", propstr);
2992 else if (right_justify)
2993 (void) printf("%*s", (int)width, propstr);
2995 (void) printf("%-*s", (int)width, propstr);
2998 (void) printf("\n");
3002 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3007 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3010 case ZPOOL_PROP_EXPANDSZ:
3012 (void) strlcpy(propval, "-", sizeof (propval));
3014 zfs_nicenum(value, propval, sizeof (propval));
3016 case ZPOOL_PROP_FRAGMENTATION:
3017 if (value == ZFS_FRAG_INVALID) {
3018 (void) strlcpy(propval, "-", sizeof (propval));
3020 (void) snprintf(propval, sizeof (propval), "%llu%%",
3021 (unsigned long long)value);
3024 case ZPOOL_PROP_CAPACITY:
3025 (void) snprintf(propval, sizeof (propval), "%llu%%",
3026 (unsigned long long)value);
3029 zfs_nicenum(value, propval, sizeof (propval));
3033 (void) strlcpy(propval, "-", sizeof (propval));
3036 (void) printf("\t%s", propval);
3038 (void) printf(" %*s", (int)width, propval);
3042 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3043 list_cbdata_t *cb, int depth)
3049 boolean_t scripted = cb->cb_scripted;
3051 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3052 (uint64_t **)&vs, &c) == 0);
3055 boolean_t toplevel = (vs->vs_space != 0);
3059 (void) printf("\t%s", name);
3060 else if (strlen(name) + depth > cb->cb_namewidth)
3061 (void) printf("%*s%s", depth, "", name);
3063 (void) printf("%*s%s%*s", depth, "", name,
3064 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3067 * Print the properties for the individual vdevs. Some
3068 * properties are only applicable to toplevel vdevs. The
3069 * 'toplevel' boolean value is passed to the print_one_column()
3070 * to indicate that the value is valid.
3072 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3074 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3076 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3077 scripted, toplevel);
3078 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3080 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3081 vs->vs_fragmentation, scripted,
3082 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3083 cap = (vs->vs_space == 0) ? 0 :
3084 (vs->vs_alloc * 100 / vs->vs_space);
3085 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3086 (void) printf("\n");
3089 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3090 &child, &children) != 0)
3093 for (c = 0; c < children; c++) {
3094 uint64_t ishole = B_FALSE;
3096 if (nvlist_lookup_uint64(child[c],
3097 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3100 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3101 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3106 * Include level 2 ARC devices in iostat output
3108 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3109 &child, &children) != 0)
3113 (void) printf("%-*s - - - - - "
3114 "-\n", cb->cb_namewidth, "cache");
3115 for (c = 0; c < children; c++) {
3116 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3118 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3126 * Generic callback function to list a pool.
3129 list_callback(zpool_handle_t *zhp, void *data)
3131 list_cbdata_t *cbp = data;
3135 config = zpool_get_config(zhp, NULL);
3137 print_pool(zhp, cbp);
3138 if (!cbp->cb_verbose)
3141 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3143 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3149 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3151 * -H Scripted mode. Don't display headers, and separate properties
3153 * -o List of properties to display. Defaults to
3154 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3155 * "dedupratio,health,altroot"
3156 * -T Display a timestamp in date(1) or Unix format
3158 * List all pools in the system, whether or not they're healthy. Output space
3159 * statistics for each one, as well as health status summary.
3162 zpool_do_list(int argc, char **argv)
3166 list_cbdata_t cb = { 0 };
3167 static char default_props[] =
3168 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3169 "dedupratio,health,altroot";
3170 char *props = default_props;
3171 unsigned long interval = 0, count = 0;
3173 boolean_t first = B_TRUE;
3176 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3179 cb.cb_scripted = B_TRUE;
3185 get_timestamp_arg(*optarg);
3188 cb.cb_verbose = B_TRUE;
3191 (void) fprintf(stderr, gettext("missing argument for "
3192 "'%c' option\n"), optopt);
3196 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3205 get_interval_count(&argc, argv, &interval, &count);
3207 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3210 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3213 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3214 (void) printf(gettext("no pools available\n"));
3215 zprop_free_list(cb.cb_proplist);
3220 pool_list_update(list);
3222 if (pool_list_count(list) == 0)
3225 if (timestamp_fmt != NODATE)
3226 print_timestamp(timestamp_fmt);
3228 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3232 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3237 if (count != 0 && --count == 0)
3240 (void) sleep(interval);
3243 zprop_free_list(cb.cb_proplist);
3248 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3250 boolean_t force = B_FALSE;
3253 char *poolname, *old_disk, *new_disk;
3254 zpool_handle_t *zhp;
3255 nvlist_t *props = NULL;
3260 while ((c = getopt(argc, argv, "fo:")) != -1) {
3266 if ((propval = strchr(optarg, '=')) == NULL) {
3267 (void) fprintf(stderr, gettext("missing "
3268 "'=' for -o option\n"));
3274 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3275 (add_prop_list(optarg, propval, &props, B_TRUE)))
3279 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3288 /* get pool name and check number of arguments */
3290 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3297 (void) fprintf(stderr,
3298 gettext("missing <device> specification\n"));
3306 (void) fprintf(stderr,
3307 gettext("missing <new_device> specification\n"));
3310 new_disk = old_disk;
3320 (void) fprintf(stderr, gettext("too many arguments\n"));
3324 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3327 if (zpool_get_config(zhp, NULL) == NULL) {
3328 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3334 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3336 if (nvroot == NULL) {
3341 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3343 nvlist_free(nvroot);
3350 * zpool replace [-f] <pool> <device> <new_device>
3352 * -f Force attach, even if <new_device> appears to be in use.
3354 * Replace <device> with <new_device>.
3358 zpool_do_replace(int argc, char **argv)
3360 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3364 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3366 * -f Force attach, even if <new_device> appears to be in use.
3367 * -o Set property=value.
3369 * Attach <new_device> to the mirror containing <device>. If <device> is not
3370 * part of a mirror, then <device> will be transformed into a mirror of
3371 * <device> and <new_device>. In either case, <new_device> will begin life
3372 * with a DTL of [0, now], and will immediately begin to resilver itself.
3375 zpool_do_attach(int argc, char **argv)
3377 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3381 * zpool detach [-f] <pool> <device>
3383 * -f Force detach of <device>, even if DTLs argue against it
3384 * (not supported yet)
3386 * Detach a device from a mirror. The operation will be refused if <device>
3387 * is the last device in the mirror, or if the DTLs indicate that this device
3388 * has the only valid copy of some data.
3392 zpool_do_detach(int argc, char **argv)
3395 char *poolname, *path;
3396 zpool_handle_t *zhp;
3400 while ((c = getopt(argc, argv, "f")) != -1) {
3404 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3413 /* get pool name and check number of arguments */
3415 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3420 (void) fprintf(stderr,
3421 gettext("missing <device> specification\n"));
3428 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3431 ret = zpool_vdev_detach(zhp, path);
3439 * zpool split [-n] [-o prop=val] ...
3441 * [-R altroot] <pool> <newpool> [<device> ...]
3443 * -n Do not split the pool, but display the resulting layout if
3444 * it were to be split.
3445 * -o Set property=value, or set mount options.
3446 * -R Mount the split-off pool under an alternate root.
3448 * Splits the named pool and gives it the new pool name. Devices to be split
3449 * off may be listed, provided that no more than one device is specified
3450 * per top-level vdev mirror. The newly split pool is left in an exported
3451 * state unless -R is specified.
3453 * Restrictions: the top-level of the pool pool must only be made up of
3454 * mirrors; all devices in the pool must be healthy; no device may be
3455 * undergoing a resilvering operation.
3458 zpool_do_split(int argc, char **argv)
3460 char *srcpool, *newpool, *propval;
3461 char *mntopts = NULL;
3464 zpool_handle_t *zhp;
3465 nvlist_t *config, *props = NULL;
3467 flags.dryrun = B_FALSE;
3468 flags.import = B_FALSE;
3471 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3474 flags.import = B_TRUE;
3476 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3477 &props, B_TRUE) != 0) {
3484 flags.dryrun = B_TRUE;
3487 if ((propval = strchr(optarg, '=')) != NULL) {
3490 if (add_prop_list(optarg, propval,
3491 &props, B_TRUE) != 0) {
3501 (void) fprintf(stderr, gettext("missing argument for "
3502 "'%c' option\n"), optopt);
3506 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3513 if (!flags.import && mntopts != NULL) {
3514 (void) fprintf(stderr, gettext("setting mntopts is only "
3515 "valid when importing the pool\n"));
3523 (void) fprintf(stderr, gettext("Missing pool name\n"));
3527 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3537 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3540 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3541 if (config == NULL) {
3545 (void) printf(gettext("would create '%s' with the "
3546 "following layout:\n\n"), newpool);
3547 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3549 nvlist_free(config);
3554 if (ret != 0 || flags.dryrun || !flags.import)
3558 * The split was successful. Now we need to open the new
3559 * pool and import it.
3561 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3563 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3564 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3566 (void) fprintf(stderr, gettext("Split was successful, but "
3567 "the datasets could not all be mounted\n"));
3568 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3569 "different altroot\n"), "zpool import");
3579 * zpool online <pool> <device> ...
3582 zpool_do_online(int argc, char **argv)
3586 zpool_handle_t *zhp;
3588 vdev_state_t newstate;
3592 while ((c = getopt(argc, argv, "et")) != -1) {
3595 flags |= ZFS_ONLINE_EXPAND;
3599 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3608 /* get pool name and check number of arguments */
3610 (void) fprintf(stderr, gettext("missing pool name\n"));
3614 (void) fprintf(stderr, gettext("missing device name\n"));
3620 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3623 for (i = 1; i < argc; i++) {
3624 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3625 if (newstate != VDEV_STATE_HEALTHY) {
3626 (void) printf(gettext("warning: device '%s' "
3627 "onlined, but remains in faulted state\n"),
3629 if (newstate == VDEV_STATE_FAULTED)
3630 (void) printf(gettext("use 'zpool "
3631 "clear' to restore a faulted "
3634 (void) printf(gettext("use 'zpool "
3635 "replace' to replace devices "
3636 "that are no longer present\n"));
3649 * zpool offline [-ft] <pool> <device> ...
3651 * -f Force the device into the offline state, even if doing
3652 * so would appear to compromise pool availability.
3653 * (not supported yet)
3655 * -t Only take the device off-line temporarily. The offline
3656 * state will not be persistent across reboots.
3660 zpool_do_offline(int argc, char **argv)
3664 zpool_handle_t *zhp;
3666 boolean_t istmp = B_FALSE;
3669 while ((c = getopt(argc, argv, "ft")) != -1) {
3676 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3685 /* get pool name and check number of arguments */
3687 (void) fprintf(stderr, gettext("missing pool name\n"));
3691 (void) fprintf(stderr, gettext("missing device name\n"));
3697 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3700 for (i = 1; i < argc; i++) {
3701 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3711 * zpool clear <pool> [device]
3713 * Clear all errors associated with a pool or a particular device.
3716 zpool_do_clear(int argc, char **argv)
3720 boolean_t dryrun = B_FALSE;
3721 boolean_t do_rewind = B_FALSE;
3722 boolean_t xtreme_rewind = B_FALSE;
3723 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3724 nvlist_t *policy = NULL;
3725 zpool_handle_t *zhp;
3726 char *pool, *device;
3729 while ((c = getopt(argc, argv, "FnX")) != -1) {
3738 xtreme_rewind = B_TRUE;
3741 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3751 (void) fprintf(stderr, gettext("missing pool name\n"));
3756 (void) fprintf(stderr, gettext("too many arguments\n"));
3760 if ((dryrun || xtreme_rewind) && !do_rewind) {
3761 (void) fprintf(stderr,
3762 gettext("-n or -X only meaningful with -F\n"));
3766 rewind_policy = ZPOOL_TRY_REWIND;
3768 rewind_policy = ZPOOL_DO_REWIND;
3770 rewind_policy |= ZPOOL_EXTREME_REWIND;
3772 /* In future, further rewind policy choices can be passed along here */
3773 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3774 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3778 device = argc == 2 ? argv[1] : NULL;
3780 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3781 nvlist_free(policy);
3785 if (zpool_clear(zhp, device, policy) != 0)
3790 nvlist_free(policy);
3796 * zpool reguid <pool>
3799 zpool_do_reguid(int argc, char **argv)
3803 zpool_handle_t *zhp;
3807 while ((c = getopt(argc, argv, "")) != -1) {
3810 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3819 /* get pool name and check number of arguments */
3821 (void) fprintf(stderr, gettext("missing pool name\n"));
3826 (void) fprintf(stderr, gettext("too many arguments\n"));
3831 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3834 ret = zpool_reguid(zhp);
3842 * zpool reopen <pool>
3844 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3847 zpool_do_reopen(int argc, char **argv)
3851 zpool_handle_t *zhp;
3855 while ((c = getopt(argc, argv, "")) != -1) {
3858 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3868 (void) fprintf(stderr, gettext("missing pool name\n"));
3873 (void) fprintf(stderr, gettext("too many arguments\n"));
3878 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3881 ret = zpool_reopen(zhp);
3886 typedef struct scrub_cbdata {
3893 scrub_callback(zpool_handle_t *zhp, void *data)
3895 scrub_cbdata_t *cb = data;
3899 * Ignore faulted pools.
3901 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3902 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3903 "currently unavailable\n"), zpool_get_name(zhp));
3907 err = zpool_scan(zhp, cb->cb_type);
3913 * zpool scrub [-s] <pool> ...
3915 * -s Stop. Stops any in-progress scrub.
3918 zpool_do_scrub(int argc, char **argv)
3923 cb.cb_type = POOL_SCAN_SCRUB;
3926 while ((c = getopt(argc, argv, "s")) != -1) {
3929 cb.cb_type = POOL_SCAN_NONE;
3932 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3944 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3948 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3951 typedef struct status_cbdata {
3953 boolean_t cb_allpools;
3954 boolean_t cb_verbose;
3955 boolean_t cb_explain;
3957 boolean_t cb_dedup_stats;
3961 * Print out detailed scrub status.
3964 print_scan_status(pool_scan_stat_t *ps)
3967 uint64_t elapsed, mins_left, hours_left;
3968 uint64_t pass_exam, examined, total;
3970 double fraction_done;
3971 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3973 (void) printf(gettext(" scan: "));
3975 /* If there's never been a scan, there's not much to say. */
3976 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3977 ps->pss_func >= POOL_SCAN_FUNCS) {
3978 (void) printf(gettext("none requested\n"));
3982 start = ps->pss_start_time;
3983 end = ps->pss_end_time;
3984 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3986 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3987 ps->pss_func == POOL_SCAN_RESILVER);
3989 * Scan is finished or canceled.
3991 if (ps->pss_state == DSS_FINISHED) {
3992 uint64_t minutes_taken = (end - start) / 60;
3995 if (ps->pss_func == POOL_SCAN_SCRUB) {
3996 fmt = gettext("scrub repaired %s in %lluh%um with "
3997 "%llu errors on %s");
3998 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3999 fmt = gettext("resilvered %s in %lluh%um with "
4000 "%llu errors on %s");
4003 (void) printf(fmt, processed_buf,
4004 (u_longlong_t)(minutes_taken / 60),
4005 (uint_t)(minutes_taken % 60),
4006 (u_longlong_t)ps->pss_errors,
4007 ctime((time_t *)&end));
4009 } else if (ps->pss_state == DSS_CANCELED) {
4010 if (ps->pss_func == POOL_SCAN_SCRUB) {
4011 (void) printf(gettext("scrub canceled on %s"),
4013 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4014 (void) printf(gettext("resilver canceled on %s"),
4020 assert(ps->pss_state == DSS_SCANNING);
4023 * Scan is in progress.
4025 if (ps->pss_func == POOL_SCAN_SCRUB) {
4026 (void) printf(gettext("scrub in progress since %s"),
4028 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4029 (void) printf(gettext("resilver in progress since %s"),
4033 examined = ps->pss_examined ? ps->pss_examined : 1;
4034 total = ps->pss_to_examine;
4035 fraction_done = (double)examined / total;
4037 /* elapsed time for this pass */
4038 elapsed = time(NULL) - ps->pss_pass_start;
4039 elapsed = elapsed ? elapsed : 1;
4040 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4041 rate = pass_exam / elapsed;
4042 rate = rate ? rate : 1;
4043 mins_left = ((total - examined) / rate) / 60;
4044 hours_left = mins_left / 60;
4046 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4047 zfs_nicenum(total, total_buf, sizeof (total_buf));
4048 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4051 * do not print estimated time if hours_left is more than 30 days
4053 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4054 examined_buf, total_buf, rate_buf);
4055 if (hours_left < (30 * 24)) {
4056 (void) printf(gettext(", %lluh%um to go\n"),
4057 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4059 (void) printf(gettext(
4060 ", (scan is slow, no estimated time)\n"));
4063 if (ps->pss_func == POOL_SCAN_RESILVER) {
4064 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4065 processed_buf, 100 * fraction_done);
4066 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4067 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4068 processed_buf, 100 * fraction_done);
4073 print_error_log(zpool_handle_t *zhp)
4075 nvlist_t *nverrlist = NULL;
4078 size_t len = MAXPATHLEN * 2;
4080 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4081 (void) printf("errors: List of errors unavailable "
4082 "(insufficient privileges)\n");
4086 (void) printf("errors: Permanent errors have been "
4087 "detected in the following files:\n\n");
4089 pathname = safe_malloc(len);
4091 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4093 uint64_t dsobj, obj;
4095 verify(nvpair_value_nvlist(elem, &nv) == 0);
4096 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4098 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4100 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4101 (void) printf("%7s %s\n", "", pathname);
4104 nvlist_free(nverrlist);
4108 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4117 (void) printf(gettext("\tspares\n"));
4119 for (i = 0; i < nspares; i++) {
4120 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4121 print_status_config(zhp, name, spares[i],
4122 namewidth, 2, B_TRUE);
4128 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4137 (void) printf(gettext("\tcache\n"));
4139 for (i = 0; i < nl2cache; i++) {
4140 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4141 print_status_config(zhp, name, l2cache[i],
4142 namewidth, 2, B_FALSE);
4148 print_dedup_stats(nvlist_t *config)
4150 ddt_histogram_t *ddh;
4156 * If the pool was faulted then we may not have been able to
4157 * obtain the config. Otherwise, if we have anything in the dedup
4158 * table continue processing the stats.
4160 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4161 (uint64_t **)&ddo, &c) != 0)
4164 (void) printf("\n");
4165 (void) printf(gettext(" dedup: "));
4166 if (ddo->ddo_count == 0) {
4167 (void) printf(gettext("no DDT entries\n"));
4171 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4172 (u_longlong_t)ddo->ddo_count,
4173 (u_longlong_t)ddo->ddo_dspace,
4174 (u_longlong_t)ddo->ddo_mspace);
4176 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4177 (uint64_t **)&dds, &c) == 0);
4178 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4179 (uint64_t **)&ddh, &c) == 0);
4180 zpool_dump_ddt(dds, ddh);
4184 * Display a summary of pool status. Displays a summary such as:
4188 * reason: One or more devices ...
4189 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4195 * When given the '-v' option, we print out the complete config. If the '-e'
4196 * option is specified, then we print out error rate information as well.
4199 status_callback(zpool_handle_t *zhp, void *data)
4201 status_cbdata_t *cbp = data;
4202 nvlist_t *config, *nvroot;
4204 zpool_status_t reason;
4205 zpool_errata_t errata;
4210 config = zpool_get_config(zhp, NULL);
4211 reason = zpool_get_status(zhp, &msgid, &errata);
4216 * If we were given 'zpool status -x', only report those pools with
4219 if (cbp->cb_explain &&
4220 (reason == ZPOOL_STATUS_OK ||
4221 reason == ZPOOL_STATUS_VERSION_OLDER ||
4222 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4223 if (!cbp->cb_allpools) {
4224 (void) printf(gettext("pool '%s' is healthy\n"),
4225 zpool_get_name(zhp));
4227 cbp->cb_first = B_FALSE;
4233 cbp->cb_first = B_FALSE;
4235 (void) printf("\n");
4237 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4239 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4240 (uint64_t **)&vs, &c) == 0);
4241 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4243 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4244 (void) printf(gettext(" state: %s\n"), health);
4247 case ZPOOL_STATUS_MISSING_DEV_R:
4248 (void) printf(gettext("status: One or more devices could not "
4249 "be opened. Sufficient replicas exist for\n\tthe pool to "
4250 "continue functioning in a degraded state.\n"));
4251 (void) printf(gettext("action: Attach the missing device and "
4252 "online it using 'zpool online'.\n"));
4255 case ZPOOL_STATUS_MISSING_DEV_NR:
4256 (void) printf(gettext("status: One or more devices could not "
4257 "be opened. There are insufficient\n\treplicas for the "
4258 "pool to continue functioning.\n"));
4259 (void) printf(gettext("action: Attach the missing device and "
4260 "online it using 'zpool online'.\n"));
4263 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4264 (void) printf(gettext("status: One or more devices could not "
4265 "be used because the label is missing or\n\tinvalid. "
4266 "Sufficient replicas exist for the pool to continue\n\t"
4267 "functioning in a degraded state.\n"));
4268 (void) printf(gettext("action: Replace the device using "
4269 "'zpool replace'.\n"));
4272 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4273 (void) printf(gettext("status: One or more devices could not "
4274 "be used because the label is missing \n\tor invalid. "
4275 "There are insufficient replicas for the pool to "
4276 "continue\n\tfunctioning.\n"));
4277 zpool_explain_recover(zpool_get_handle(zhp),
4278 zpool_get_name(zhp), reason, config);
4281 case ZPOOL_STATUS_FAILING_DEV:
4282 (void) printf(gettext("status: One or more devices has "
4283 "experienced an unrecoverable error. An\n\tattempt was "
4284 "made to correct the error. Applications are "
4286 (void) printf(gettext("action: Determine if the device needs "
4287 "to be replaced, and clear the errors\n\tusing "
4288 "'zpool clear' or replace the device with 'zpool "
4292 case ZPOOL_STATUS_OFFLINE_DEV:
4293 (void) printf(gettext("status: One or more devices has "
4294 "been taken offline by the administrator.\n\tSufficient "
4295 "replicas exist for the pool to continue functioning in "
4296 "a\n\tdegraded state.\n"));
4297 (void) printf(gettext("action: Online the device using "
4298 "'zpool online' or replace the device with\n\t'zpool "
4302 case ZPOOL_STATUS_REMOVED_DEV:
4303 (void) printf(gettext("status: One or more devices has "
4304 "been removed by the administrator.\n\tSufficient "
4305 "replicas exist for the pool to continue functioning in "
4306 "a\n\tdegraded state.\n"));
4307 (void) printf(gettext("action: Online the device using "
4308 "'zpool online' or replace the device with\n\t'zpool "
4312 case ZPOOL_STATUS_RESILVERING:
4313 (void) printf(gettext("status: One or more devices is "
4314 "currently being resilvered. The pool will\n\tcontinue "
4315 "to function, possibly in a degraded state.\n"));
4316 (void) printf(gettext("action: Wait for the resilver to "
4320 case ZPOOL_STATUS_CORRUPT_DATA:
4321 (void) printf(gettext("status: One or more devices has "
4322 "experienced an error resulting in data\n\tcorruption. "
4323 "Applications may be affected.\n"));
4324 (void) printf(gettext("action: Restore the file in question "
4325 "if possible. Otherwise restore the\n\tentire pool from "
4329 case ZPOOL_STATUS_CORRUPT_POOL:
4330 (void) printf(gettext("status: The pool metadata is corrupted "
4331 "and the pool cannot be opened.\n"));
4332 zpool_explain_recover(zpool_get_handle(zhp),
4333 zpool_get_name(zhp), reason, config);
4336 case ZPOOL_STATUS_VERSION_OLDER:
4337 (void) printf(gettext("status: The pool is formatted using a "
4338 "legacy on-disk format. The pool can\n\tstill be used, "
4339 "but some features are unavailable.\n"));
4340 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4341 "upgrade'. Once this is done, the\n\tpool will no longer "
4342 "be accessible on software that does not support\n\t"
4343 "feature flags.\n"));
4346 case ZPOOL_STATUS_VERSION_NEWER:
4347 (void) printf(gettext("status: The pool has been upgraded to a "
4348 "newer, incompatible on-disk version.\n\tThe pool cannot "
4349 "be accessed on this system.\n"));
4350 (void) printf(gettext("action: Access the pool from a system "
4351 "running more recent software, or\n\trestore the pool from "
4355 case ZPOOL_STATUS_FEAT_DISABLED:
4356 (void) printf(gettext("status: Some supported features are not "
4357 "enabled on the pool. The pool can\n\tstill be used, but "
4358 "some features are unavailable.\n"));
4359 (void) printf(gettext("action: Enable all features using "
4360 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4361 "longer be accessible by software that does not support\n\t"
4362 "the features. See zpool-features(5) for details.\n"));
4365 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4366 (void) printf(gettext("status: The pool cannot be accessed on "
4367 "this system because it uses the\n\tfollowing feature(s) "
4368 "not supported on this system:\n"));
4369 zpool_print_unsup_feat(config);
4370 (void) printf("\n");
4371 (void) printf(gettext("action: Access the pool from a system "
4372 "that supports the required feature(s),\n\tor restore the "
4373 "pool from backup.\n"));
4376 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4377 (void) printf(gettext("status: The pool can only be accessed "
4378 "in read-only mode on this system. It\n\tcannot be "
4379 "accessed in read-write mode because it uses the "
4380 "following\n\tfeature(s) not supported on this system:\n"));
4381 zpool_print_unsup_feat(config);
4382 (void) printf("\n");
4383 (void) printf(gettext("action: The pool cannot be accessed in "
4384 "read-write mode. Import the pool with\n"
4385 "\t\"-o readonly=on\", access the pool from a system that "
4386 "supports the\n\trequired feature(s), or restore the "
4387 "pool from backup.\n"));
4390 case ZPOOL_STATUS_FAULTED_DEV_R:
4391 (void) printf(gettext("status: One or more devices are "
4392 "faulted in response to persistent errors.\n\tSufficient "
4393 "replicas exist for the pool to continue functioning "
4394 "in a\n\tdegraded state.\n"));
4395 (void) printf(gettext("action: Replace the faulted device, "
4396 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4399 case ZPOOL_STATUS_FAULTED_DEV_NR:
4400 (void) printf(gettext("status: One or more devices are "
4401 "faulted in response to persistent errors. There are "
4402 "insufficient replicas for the pool to\n\tcontinue "
4404 (void) printf(gettext("action: Destroy and re-create the pool "
4405 "from a backup source. Manually marking the device\n"
4406 "\trepaired using 'zpool clear' may allow some data "
4407 "to be recovered.\n"));
4410 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4411 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4412 (void) printf(gettext("status: One or more devices are "
4413 "faulted in response to IO failures.\n"));
4414 (void) printf(gettext("action: Make sure the affected devices "
4415 "are connected, then run 'zpool clear'.\n"));
4418 case ZPOOL_STATUS_BAD_LOG:
4419 (void) printf(gettext("status: An intent log record "
4420 "could not be read.\n"
4421 "\tWaiting for adminstrator intervention to fix the "
4422 "faulted pool.\n"));
4423 (void) printf(gettext("action: Either restore the affected "
4424 "device(s) and run 'zpool online',\n"
4425 "\tor ignore the intent log records by running "
4426 "'zpool clear'.\n"));
4429 case ZPOOL_STATUS_HOSTID_MISMATCH:
4430 (void) printf(gettext("status: Mismatch between pool hostid "
4431 "and system hostid on imported pool.\n\tThis pool was "
4432 "previously imported into a system with a different "
4433 "hostid,\n\tand then was verbatim imported into this "
4435 (void) printf(gettext("action: Export this pool on all systems "
4436 "on which it is imported.\n"
4437 "\tThen import it to correct the mismatch.\n"));
4440 case ZPOOL_STATUS_ERRATA:
4441 (void) printf(gettext("status: Errata #%d detected.\n"),
4445 case ZPOOL_ERRATA_NONE:
4448 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
4449 (void) printf(gettext("action: To correct the issue "
4450 "run 'zpool scrub'.\n"));
4455 * All errata which allow the pool to be imported
4456 * must contain an action message.
4464 * The remaining errors can't actually be generated, yet.
4466 assert(reason == ZPOOL_STATUS_OK);
4470 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4473 if (config != NULL) {
4476 nvlist_t **spares, **l2cache;
4477 uint_t nspares, nl2cache;
4478 pool_scan_stat_t *ps = NULL;
4480 (void) nvlist_lookup_uint64_array(nvroot,
4481 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4482 print_scan_status(ps);
4484 namewidth = max_width(zhp, nvroot, 0, 0);
4488 (void) printf(gettext("config:\n\n"));
4489 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4490 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4491 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4492 namewidth, 0, B_FALSE);
4494 if (num_logs(nvroot) > 0)
4495 print_logs(zhp, nvroot, namewidth, B_TRUE);
4496 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4497 &l2cache, &nl2cache) == 0)
4498 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4500 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4501 &spares, &nspares) == 0)
4502 print_spares(zhp, spares, nspares, namewidth);
4504 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4506 nvlist_t *nverrlist = NULL;
4509 * If the approximate error count is small, get a
4510 * precise count by fetching the entire log and
4511 * uniquifying the results.
4513 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4514 zpool_get_errlog(zhp, &nverrlist) == 0) {
4519 while ((elem = nvlist_next_nvpair(nverrlist,
4524 nvlist_free(nverrlist);
4526 (void) printf("\n");
4529 (void) printf(gettext("errors: No known data "
4531 else if (!cbp->cb_verbose)
4532 (void) printf(gettext("errors: %llu data "
4533 "errors, use '-v' for a list\n"),
4534 (u_longlong_t)nerr);
4536 print_error_log(zhp);
4539 if (cbp->cb_dedup_stats)
4540 print_dedup_stats(config);
4542 (void) printf(gettext("config: The configuration cannot be "
4550 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4552 * -v Display complete error logs
4553 * -x Display only pools with potential problems
4554 * -D Display dedup status (undocumented)
4555 * -T Display a timestamp in date(1) or Unix format
4557 * Describes the health status of all pools or some subset.
4560 zpool_do_status(int argc, char **argv)
4564 unsigned long interval = 0, count = 0;
4565 status_cbdata_t cb = { 0 };
4568 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4571 cb.cb_verbose = B_TRUE;
4574 cb.cb_explain = B_TRUE;
4577 cb.cb_dedup_stats = B_TRUE;
4580 get_timestamp_arg(*optarg);
4583 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4592 get_interval_count(&argc, argv, &interval, &count);
4595 cb.cb_allpools = B_TRUE;
4597 cb.cb_first = B_TRUE;
4600 if (timestamp_fmt != NODATE)
4601 print_timestamp(timestamp_fmt);
4603 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4604 status_callback, &cb);
4606 if (argc == 0 && cb.cb_count == 0)
4607 (void) fprintf(stderr, gettext("no pools available\n"));
4608 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4609 (void) printf(gettext("all pools are healthy\n"));
4617 if (count != 0 && --count == 0)
4620 (void) sleep(interval);
4626 typedef struct upgrade_cbdata {
4629 uint64_t cb_version;
4634 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
4636 int zfs_version = (int) zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
4637 int *count = (int *)unsupp_fs;
4639 if (zfs_version > ZPL_VERSION) {
4640 (void) printf(gettext("%s (v%d) is not supported by this "
4641 "implementation of ZFS.\n"),
4642 zfs_get_name(zhp), zfs_version);
4646 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
4654 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4658 uint64_t oldversion;
4661 config = zpool_get_config(zhp, NULL);
4662 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4665 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4666 assert(oldversion < version);
4668 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
4673 (void) printf(gettext("Upgrade not performed due to %d "
4674 "unsupported filesystems (max v%d).\n"),
4675 unsupp_fs, (int) ZPL_VERSION);
4679 ret = zpool_upgrade(zhp, version);
4683 if (version >= SPA_VERSION_FEATURES) {
4684 (void) printf(gettext("Successfully upgraded "
4685 "'%s' from version %llu to feature flags.\n"),
4686 zpool_get_name(zhp), (u_longlong_t) oldversion);
4688 (void) printf(gettext("Successfully upgraded "
4689 "'%s' from version %llu to version %llu.\n"),
4690 zpool_get_name(zhp), (u_longlong_t) oldversion,
4691 (u_longlong_t) version);
4698 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4701 boolean_t firstff = B_TRUE;
4702 nvlist_t *enabled = zpool_get_features(zhp);
4705 for (i = 0; i < SPA_FEATURES; i++) {
4706 const char *fname = spa_feature_table[i].fi_uname;
4707 const char *fguid = spa_feature_table[i].fi_guid;
4708 if (!nvlist_exists(enabled, fguid)) {
4710 verify(-1 != asprintf(&propname, "feature@%s", fname));
4711 ret = zpool_set_prop(zhp, propname,
4712 ZFS_FEATURE_ENABLED);
4720 (void) printf(gettext("Enabled the "
4721 "following features on '%s':\n"),
4722 zpool_get_name(zhp));
4725 (void) printf(gettext(" %s\n"), fname);
4736 upgrade_cb(zpool_handle_t *zhp, void *arg)
4738 upgrade_cbdata_t *cbp = arg;
4741 boolean_t printnl = B_FALSE;
4744 config = zpool_get_config(zhp, NULL);
4745 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4748 assert(SPA_VERSION_IS_SUPPORTED(version));
4750 if (version < cbp->cb_version) {
4751 cbp->cb_first = B_FALSE;
4752 ret = upgrade_version(zhp, cbp->cb_version);
4758 * If they did "zpool upgrade -a", then we could
4759 * be doing ioctls to different pools. We need
4760 * to log this history once to each pool, and bypass
4761 * the normal history logging that happens in main().
4763 (void) zpool_log_history(g_zfs, history_str);
4764 log_history = B_FALSE;
4767 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4769 ret = upgrade_enable_all(zhp, &count);
4774 cbp->cb_first = B_FALSE;
4780 (void) printf(gettext("\n"));
4787 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4789 upgrade_cbdata_t *cbp = arg;
4793 config = zpool_get_config(zhp, NULL);
4794 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4797 assert(SPA_VERSION_IS_SUPPORTED(version));
4799 if (version < SPA_VERSION_FEATURES) {
4800 if (cbp->cb_first) {
4801 (void) printf(gettext("The following pools are "
4802 "formatted with legacy version numbers and can\n"
4803 "be upgraded to use feature flags. After "
4804 "being upgraded, these pools\nwill no "
4805 "longer be accessible by software that does not "
4806 "support feature\nflags.\n\n"));
4807 (void) printf(gettext("VER POOL\n"));
4808 (void) printf(gettext("--- ------------\n"));
4809 cbp->cb_first = B_FALSE;
4812 (void) printf("%2llu %s\n", (u_longlong_t)version,
4813 zpool_get_name(zhp));
4820 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4822 upgrade_cbdata_t *cbp = arg;
4826 config = zpool_get_config(zhp, NULL);
4827 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4830 if (version >= SPA_VERSION_FEATURES) {
4832 boolean_t poolfirst = B_TRUE;
4833 nvlist_t *enabled = zpool_get_features(zhp);
4835 for (i = 0; i < SPA_FEATURES; i++) {
4836 const char *fguid = spa_feature_table[i].fi_guid;
4837 const char *fname = spa_feature_table[i].fi_uname;
4838 if (!nvlist_exists(enabled, fguid)) {
4839 if (cbp->cb_first) {
4840 (void) printf(gettext("\nSome "
4841 "supported features are not "
4842 "enabled on the following pools. "
4843 "Once a\nfeature is enabled the "
4844 "pool may become incompatible with "
4845 "software\nthat does not support "
4847 "zpool-features(5) for "
4849 (void) printf(gettext("POOL "
4851 (void) printf(gettext("------"
4853 cbp->cb_first = B_FALSE;
4857 (void) printf(gettext("%s\n"),
4858 zpool_get_name(zhp));
4859 poolfirst = B_FALSE;
4862 (void) printf(gettext(" %s\n"), fname);
4865 * If they did "zpool upgrade -a", then we could
4866 * be doing ioctls to different pools. We need
4867 * to log this history once to each pool, and bypass
4868 * the normal history logging that happens in main().
4870 (void) zpool_log_history(g_zfs, history_str);
4871 log_history = B_FALSE;
4880 upgrade_one(zpool_handle_t *zhp, void *data)
4882 boolean_t printnl = B_FALSE;
4883 upgrade_cbdata_t *cbp = data;
4884 uint64_t cur_version;
4887 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4888 (void) printf(gettext("'log' is now a reserved word\n"
4889 "Pool 'log' must be renamed using export and import"
4894 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4895 if (cur_version > cbp->cb_version) {
4896 (void) printf(gettext("Pool '%s' is already formatted "
4897 "using more current version '%llu'.\n\n"),
4898 zpool_get_name(zhp), (u_longlong_t) cur_version);
4902 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4903 (void) printf(gettext("Pool '%s' is already formatted "
4904 "using version %llu.\n\n"), zpool_get_name(zhp),
4905 (u_longlong_t) cbp->cb_version);
4909 if (cur_version != cbp->cb_version) {
4911 ret = upgrade_version(zhp, cbp->cb_version);
4916 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4918 ret = upgrade_enable_all(zhp, &count);
4924 } else if (cur_version == SPA_VERSION) {
4925 (void) printf(gettext("Pool '%s' already has all "
4926 "supported features enabled.\n"),
4927 zpool_get_name(zhp));
4932 (void) printf(gettext("\n"));
4941 * zpool upgrade [-V version] <-a | pool ...>
4943 * With no arguments, display downrev'd ZFS pool available for upgrade.
4944 * Individual pools can be upgraded by specifying the pool, and '-a' will
4945 * upgrade all pools.
4948 zpool_do_upgrade(int argc, char **argv)
4951 upgrade_cbdata_t cb = { 0 };
4953 boolean_t showversions = B_FALSE;
4954 boolean_t upgradeall = B_FALSE;
4959 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4962 upgradeall = B_TRUE;
4965 showversions = B_TRUE;
4968 cb.cb_version = strtoll(optarg, &end, 10);
4970 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4971 (void) fprintf(stderr,
4972 gettext("invalid version '%s'\n"), optarg);
4977 (void) fprintf(stderr, gettext("missing argument for "
4978 "'%c' option\n"), optopt);
4982 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4993 if (cb.cb_version == 0) {
4994 cb.cb_version = SPA_VERSION;
4995 } else if (!upgradeall && argc == 0) {
4996 (void) fprintf(stderr, gettext("-V option is "
4997 "incompatible with other arguments\n"));
5002 if (upgradeall || argc != 0) {
5003 (void) fprintf(stderr, gettext("-v option is "
5004 "incompatible with other arguments\n"));
5007 } else if (upgradeall) {
5009 (void) fprintf(stderr, gettext("-a option should not "
5010 "be used along with a pool name\n"));
5015 (void) printf(gettext("This system supports ZFS pool feature "
5020 (void) printf(gettext("The following features are "
5022 (void) printf(gettext("FEAT DESCRIPTION\n"));
5023 (void) printf("----------------------------------------------"
5024 "---------------\n");
5025 for (i = 0; i < SPA_FEATURES; i++) {
5026 zfeature_info_t *fi = &spa_feature_table[i];
5027 const char *ro = fi->fi_can_readonly ?
5028 " (read-only compatible)" : "";
5030 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5031 (void) printf(" %s\n", fi->fi_desc);
5033 (void) printf("\n");
5035 (void) printf(gettext("The following legacy versions are also "
5037 (void) printf(gettext("VER DESCRIPTION\n"));
5038 (void) printf("--- -----------------------------------------"
5039 "---------------\n");
5040 (void) printf(gettext(" 1 Initial ZFS version\n"));
5041 (void) printf(gettext(" 2 Ditto blocks "
5042 "(replicated metadata)\n"));
5043 (void) printf(gettext(" 3 Hot spares and double parity "
5045 (void) printf(gettext(" 4 zpool history\n"));
5046 (void) printf(gettext(" 5 Compression using the gzip "
5048 (void) printf(gettext(" 6 bootfs pool property\n"));
5049 (void) printf(gettext(" 7 Separate intent log devices\n"));
5050 (void) printf(gettext(" 8 Delegated administration\n"));
5051 (void) printf(gettext(" 9 refquota and refreservation "
5053 (void) printf(gettext(" 10 Cache devices\n"));
5054 (void) printf(gettext(" 11 Improved scrub performance\n"));
5055 (void) printf(gettext(" 12 Snapshot properties\n"));
5056 (void) printf(gettext(" 13 snapused property\n"));
5057 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5058 (void) printf(gettext(" 15 user/group space accounting\n"));
5059 (void) printf(gettext(" 16 stmf property support\n"));
5060 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5061 (void) printf(gettext(" 18 Snapshot user holds\n"));
5062 (void) printf(gettext(" 19 Log device removal\n"));
5063 (void) printf(gettext(" 20 Compression using zle "
5064 "(zero-length encoding)\n"));
5065 (void) printf(gettext(" 21 Deduplication\n"));
5066 (void) printf(gettext(" 22 Received properties\n"));
5067 (void) printf(gettext(" 23 Slim ZIL\n"));
5068 (void) printf(gettext(" 24 System attributes\n"));
5069 (void) printf(gettext(" 25 Improved scrub stats\n"));
5070 (void) printf(gettext(" 26 Improved snapshot deletion "
5072 (void) printf(gettext(" 27 Improved snapshot creation "
5074 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5075 (void) printf(gettext("\nFor more information on a particular "
5076 "version, including supported releases,\n"));
5077 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5078 } else if (argc == 0 && upgradeall) {
5079 cb.cb_first = B_TRUE;
5080 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5081 if (ret == 0 && cb.cb_first) {
5082 if (cb.cb_version == SPA_VERSION) {
5083 (void) printf(gettext("All pools are already "
5084 "formatted using feature flags.\n\n"));
5085 (void) printf(gettext("Every feature flags "
5086 "pool already has all supported features "
5089 (void) printf(gettext("All pools are already "
5090 "formatted with version %llu or higher.\n"),
5091 (u_longlong_t) cb.cb_version);
5094 } else if (argc == 0) {
5095 cb.cb_first = B_TRUE;
5096 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5100 (void) printf(gettext("All pools are formatted "
5101 "using feature flags.\n\n"));
5103 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5104 "for a list of available legacy versions.\n"));
5107 cb.cb_first = B_TRUE;
5108 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5112 (void) printf(gettext("Every feature flags pool has "
5113 "all supported features enabled.\n"));
5115 (void) printf(gettext("\n"));
5118 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5125 typedef struct hist_cbdata {
5132 * Print out the command history for a specific pool.
5135 get_history_one(zpool_handle_t *zhp, void *data)
5141 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5143 cb->first = B_FALSE;
5145 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5147 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5150 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5151 &records, &numrecords) == 0);
5152 for (i = 0; i < numrecords; i++) {
5153 nvlist_t *rec = records[i];
5156 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5160 tsec = fnvlist_lookup_uint64(records[i],
5162 (void) localtime_r(&tsec, &t);
5163 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5166 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5167 (void) printf("%s %s", tbuf,
5168 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5169 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5171 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5174 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5175 (void) printf("%s unrecognized record:\n",
5177 dump_nvlist(rec, 4);
5180 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5181 zfs_history_event_names[ievent],
5182 (longlong_t) fnvlist_lookup_uint64(
5183 rec, ZPOOL_HIST_TXG),
5184 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5185 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5188 (void) printf("%s [txg:%lld] %s", tbuf,
5189 (longlong_t) fnvlist_lookup_uint64(
5190 rec, ZPOOL_HIST_TXG),
5191 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5192 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5193 (void) printf(" %s (%llu)",
5194 fnvlist_lookup_string(rec,
5196 (u_longlong_t)fnvlist_lookup_uint64(rec,
5199 (void) printf(" %s", fnvlist_lookup_string(rec,
5200 ZPOOL_HIST_INT_STR));
5201 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5204 (void) printf("%s ioctl %s\n", tbuf,
5205 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5206 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5207 (void) printf(" input:\n");
5208 dump_nvlist(fnvlist_lookup_nvlist(rec,
5209 ZPOOL_HIST_INPUT_NVL), 8);
5211 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5212 (void) printf(" output:\n");
5213 dump_nvlist(fnvlist_lookup_nvlist(rec,
5214 ZPOOL_HIST_OUTPUT_NVL), 8);
5219 (void) printf("%s unrecognized record:\n", tbuf);
5220 dump_nvlist(rec, 4);
5224 (void) printf("\n");
5227 (void) printf(" [");
5228 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5229 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5230 struct passwd *pwd = getpwuid(who);
5231 (void) printf("user %d ", (int)who);
5233 (void) printf("(%s) ", pwd->pw_name);
5235 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5236 (void) printf("on %s",
5237 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5239 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5240 (void) printf(":%s",
5241 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5245 (void) printf("\n");
5247 (void) printf("\n");
5254 * zpool history <pool>
5256 * Displays the history of commands that modified pools.
5259 zpool_do_history(int argc, char **argv)
5261 hist_cbdata_t cbdata = { 0 };
5265 cbdata.first = B_TRUE;
5267 while ((c = getopt(argc, argv, "li")) != -1) {
5270 cbdata.longfmt = B_TRUE;
5273 cbdata.internal = B_TRUE;
5276 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5284 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5287 if (argc == 0 && cbdata.first == B_TRUE) {
5288 (void) fprintf(stderr, gettext("no pools available\n"));
5295 typedef struct ev_opts {
5303 zpool_do_events_short(nvlist_t *nvl)
5305 char ctime_str[26], str[32], *ptr;
5309 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5310 memset(str, ' ', 32);
5311 (void) ctime_r((const time_t *)&tv[0], ctime_str);
5312 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
5313 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
5314 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
5315 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
5316 (void) printf(gettext("%s "), str);
5318 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5319 (void) printf(gettext("%s\n"), ptr);
5323 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5327 for (nvp = nvlist_next_nvpair(nvl, NULL);
5328 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5330 data_type_t type = nvpair_type(nvp);
5331 const char *name = nvpair_name(nvp);
5341 printf(gettext("%*s%s = "), depth, "", name);
5344 case DATA_TYPE_BOOLEAN:
5345 printf(gettext("%s"), "1");
5348 case DATA_TYPE_BOOLEAN_VALUE:
5349 (void) nvpair_value_boolean_value(nvp, &b);
5350 printf(gettext("%s"), b ? "1" : "0");
5353 case DATA_TYPE_BYTE:
5354 (void) nvpair_value_byte(nvp, &i8);
5355 printf(gettext("0x%x"), i8);
5358 case DATA_TYPE_INT8:
5359 (void) nvpair_value_int8(nvp, (void *)&i8);
5360 printf(gettext("0x%x"), i8);
5363 case DATA_TYPE_UINT8:
5364 (void) nvpair_value_uint8(nvp, &i8);
5365 printf(gettext("0x%x"), i8);
5368 case DATA_TYPE_INT16:
5369 (void) nvpair_value_int16(nvp, (void *)&i16);
5370 printf(gettext("0x%x"), i16);
5373 case DATA_TYPE_UINT16:
5374 (void) nvpair_value_uint16(nvp, &i16);
5375 printf(gettext("0x%x"), i16);
5378 case DATA_TYPE_INT32:
5379 (void) nvpair_value_int32(nvp, (void *)&i32);
5380 printf(gettext("0x%x"), i32);
5383 case DATA_TYPE_UINT32:
5384 (void) nvpair_value_uint32(nvp, &i32);
5385 printf(gettext("0x%x"), i32);
5388 case DATA_TYPE_INT64:
5389 (void) nvpair_value_int64(nvp, (void *)&i64);
5390 printf(gettext("0x%llx"), (u_longlong_t)i64);
5393 case DATA_TYPE_UINT64:
5394 (void) nvpair_value_uint64(nvp, &i64);
5395 printf(gettext("0x%llx"), (u_longlong_t)i64);
5398 case DATA_TYPE_HRTIME:
5399 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5400 printf(gettext("0x%llx"), (u_longlong_t)i64);
5403 case DATA_TYPE_STRING:
5404 (void) nvpair_value_string(nvp, &str);
5405 printf(gettext("\"%s\""), str ? str : "<NULL>");
5408 case DATA_TYPE_NVLIST:
5409 printf(gettext("(embedded nvlist)\n"));
5410 (void) nvpair_value_nvlist(nvp, &cnv);
5411 zpool_do_events_nvprint(cnv, depth + 8);
5412 printf(gettext("%*s(end %s)"), depth, "", name);
5415 case DATA_TYPE_NVLIST_ARRAY: {
5419 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5420 printf(gettext("(%d embedded nvlists)\n"), nelem);
5421 for (i = 0; i < nelem; i++) {
5422 printf(gettext("%*s%s[%d] = %s\n"),
5423 depth, "", name, i, "(embedded nvlist)");
5424 zpool_do_events_nvprint(val[i], depth + 8);
5425 printf(gettext("%*s(end %s[%i])\n"),
5426 depth, "", name, i);
5428 printf(gettext("%*s(end %s)\n"), depth, "", name);
5432 case DATA_TYPE_INT8_ARRAY: {
5436 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5437 for (i = 0; i < nelem; i++)
5438 printf(gettext("0x%x "), val[i]);
5443 case DATA_TYPE_UINT8_ARRAY: {
5447 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5448 for (i = 0; i < nelem; i++)
5449 printf(gettext("0x%x "), val[i]);
5454 case DATA_TYPE_INT16_ARRAY: {
5458 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5459 for (i = 0; i < nelem; i++)
5460 printf(gettext("0x%x "), val[i]);
5465 case DATA_TYPE_UINT16_ARRAY: {
5469 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5470 for (i = 0; i < nelem; i++)
5471 printf(gettext("0x%x "), val[i]);
5476 case DATA_TYPE_INT32_ARRAY: {
5480 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5481 for (i = 0; i < nelem; i++)
5482 printf(gettext("0x%x "), val[i]);
5487 case DATA_TYPE_UINT32_ARRAY: {
5491 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5492 for (i = 0; i < nelem; i++)
5493 printf(gettext("0x%x "), val[i]);
5498 case DATA_TYPE_INT64_ARRAY: {
5502 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5503 for (i = 0; i < nelem; i++)
5504 printf(gettext("0x%llx "),
5505 (u_longlong_t)val[i]);
5510 case DATA_TYPE_UINT64_ARRAY: {
5514 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5515 for (i = 0; i < nelem; i++)
5516 printf(gettext("0x%llx "),
5517 (u_longlong_t)val[i]);
5522 case DATA_TYPE_STRING_ARRAY: {
5526 (void) nvpair_value_string_array(nvp, &str, &nelem);
5527 for (i = 0; i < nelem; i++)
5528 printf(gettext("\"%s\" "),
5529 str[i] ? str[i] : "<NULL>");
5534 case DATA_TYPE_BOOLEAN_ARRAY:
5535 case DATA_TYPE_BYTE_ARRAY:
5536 case DATA_TYPE_DOUBLE:
5537 case DATA_TYPE_UNKNOWN:
5538 printf(gettext("<unknown>"));
5542 printf(gettext("\n"));
5547 zpool_do_events_next(ev_opts_t *opts)
5550 int zevent_fd, ret, dropped;
5552 zevent_fd = open(ZFS_DEV, O_RDWR);
5553 VERIFY(zevent_fd >= 0);
5555 if (!opts->scripted)
5556 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5559 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5560 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
5561 if (ret || nvl == NULL)
5565 (void) printf(gettext("dropped %d events\n"), dropped);
5567 zpool_do_events_short(nvl);
5569 if (opts->verbose) {
5570 zpool_do_events_nvprint(nvl, 8);
5571 printf(gettext("\n"));
5573 (void) fflush(stdout);
5578 VERIFY(0 == close(zevent_fd));
5584 zpool_do_events_clear(ev_opts_t *opts)
5588 ret = zpool_events_clear(g_zfs, &count);
5590 (void) printf(gettext("cleared %d events\n"), count);
5596 * zpool events [-vfc]
5598 * Displays events logs by ZFS.
5601 zpool_do_events(int argc, char **argv)
5603 ev_opts_t opts = { 0 };
5608 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5623 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5632 ret = zpool_do_events_clear(&opts);
5634 ret = zpool_do_events_next(&opts);
5640 get_callback(zpool_handle_t *zhp, void *data)
5642 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5643 char value[MAXNAMELEN];
5644 zprop_source_t srctype;
5647 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5650 * Skip the special fake placeholder. This will also skip
5651 * over the name property when 'all' is specified.
5653 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5654 pl == cbp->cb_proplist)
5657 if (pl->pl_prop == ZPROP_INVAL &&
5658 (zpool_prop_feature(pl->pl_user_prop) ||
5659 zpool_prop_unsupported(pl->pl_user_prop))) {
5660 srctype = ZPROP_SRC_LOCAL;
5662 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5663 value, sizeof (value)) == 0) {
5664 zprop_print_one_property(zpool_get_name(zhp),
5665 cbp, pl->pl_user_prop, value, srctype,
5669 if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5670 sizeof (value), &srctype, cbp->cb_literal) != 0)
5673 zprop_print_one_property(zpool_get_name(zhp), cbp,
5674 zpool_prop_to_name(pl->pl_prop), value, srctype,
5682 zpool_do_get(int argc, char **argv)
5684 zprop_get_cbdata_t cb = { 0 };
5685 zprop_list_t fake_name = { 0 };
5689 while ((c = getopt(argc, argv, "pH")) != -1) {
5692 cb.cb_literal = B_TRUE;
5696 cb.cb_scripted = B_TRUE;
5700 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5710 (void) fprintf(stderr, gettext("missing property "
5715 cb.cb_first = B_TRUE;
5716 cb.cb_sources = ZPROP_SRC_ALL;
5717 cb.cb_columns[0] = GET_COL_NAME;
5718 cb.cb_columns[1] = GET_COL_PROPERTY;
5719 cb.cb_columns[2] = GET_COL_VALUE;
5720 cb.cb_columns[3] = GET_COL_SOURCE;
5721 cb.cb_type = ZFS_TYPE_POOL;
5723 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5729 if (cb.cb_proplist != NULL) {
5730 fake_name.pl_prop = ZPOOL_PROP_NAME;
5731 fake_name.pl_width = strlen(gettext("NAME"));
5732 fake_name.pl_next = cb.cb_proplist;
5733 cb.cb_proplist = &fake_name;
5736 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5739 if (cb.cb_proplist == &fake_name)
5740 zprop_free_list(fake_name.pl_next);
5742 zprop_free_list(cb.cb_proplist);
5747 typedef struct set_cbdata {
5750 boolean_t cb_any_successful;
5754 set_callback(zpool_handle_t *zhp, void *data)
5757 set_cbdata_t *cb = (set_cbdata_t *)data;
5759 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5762 cb->cb_any_successful = B_TRUE;
5768 zpool_do_set(int argc, char **argv)
5770 set_cbdata_t cb = { 0 };
5773 if (argc > 1 && argv[1][0] == '-') {
5774 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5780 (void) fprintf(stderr, gettext("missing property=value "
5786 (void) fprintf(stderr, gettext("missing pool name\n"));
5791 (void) fprintf(stderr, gettext("too many pool names\n"));
5795 cb.cb_propname = argv[1];
5796 cb.cb_value = strchr(cb.cb_propname, '=');
5797 if (cb.cb_value == NULL) {
5798 (void) fprintf(stderr, gettext("missing value in "
5799 "property=value argument\n"));
5803 *(cb.cb_value) = '\0';
5806 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5813 find_command_idx(char *command, int *idx)
5817 for (i = 0; i < NCOMMAND; i++) {
5818 if (command_table[i].name == NULL)
5821 if (strcmp(command, command_table[i].name) == 0) {
5830 main(int argc, char **argv)
5836 (void) setlocale(LC_ALL, "");
5837 (void) textdomain(TEXT_DOMAIN);
5842 * Make sure the user has specified some command.
5845 (void) fprintf(stderr, gettext("missing command\n"));
5854 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
5857 if ((g_zfs = libzfs_init()) == NULL)
5860 libzfs_print_on_error(g_zfs, B_TRUE);
5862 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5865 * Run the appropriate command.
5867 if (find_command_idx(cmdname, &i) == 0) {
5868 current_command = &command_table[i];
5869 ret = command_table[i].func(argc - 1, argv + 1);
5870 } else if (strchr(cmdname, '=')) {
5871 verify(find_command_idx("set", &i) == 0);
5872 current_command = &command_table[i];
5873 ret = command_table[i].func(argc, argv);
5874 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5876 * 'freeze' is a vile debugging abomination, so we treat
5880 int fd = open(ZFS_DEV, O_RDWR);
5881 (void) strcpy((void *)buf, argv[2]);
5882 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5884 (void) fprintf(stderr, gettext("unrecognized "
5885 "command '%s'\n"), cmdname);
5890 if (ret == 0 && log_history)
5891 (void) zpool_log_history(g_zfs, history_str);
5896 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5897 * for the purposes of running ::findleaks.
5899 if (getenv("ZFS_ABORT") != NULL) {
5900 (void) printf("dumping core by request\n");