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:t:")) != -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,
900 * Sanity check temporary pool name.
902 if (strchr(optarg, '/') != NULL) {
903 (void) fprintf(stderr, gettext("cannot create "
904 "'%s': invalid character '/' in temporary "
906 (void) fprintf(stderr, gettext("use 'zfs "
907 "create' to create a dataset\n"));
911 if (add_prop_list(zpool_prop_to_name(
912 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
914 if (add_prop_list_default(zpool_prop_to_name(
915 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
919 (void) fprintf(stderr, gettext("missing argument for "
920 "'%c' option\n"), optopt);
923 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
932 /* get pool name and check number of arguments */
934 (void) fprintf(stderr, gettext("missing pool name argument\n"));
938 (void) fprintf(stderr, gettext("missing vdev specification\n"));
945 * As a special case, check for use of '/' in the name, and direct the
946 * user to use 'zfs create' instead.
948 if (strchr(poolname, '/') != NULL) {
949 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
950 "character '/' in pool name\n"), poolname);
951 (void) fprintf(stderr, gettext("use 'zfs create' to "
952 "create a dataset\n"));
956 /* pass off to get_vdev_spec for bulk processing */
957 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
962 /* make_root_vdev() allows 0 toplevel children if there are spares */
963 if (!zfs_allocatable_devs(nvroot)) {
964 (void) fprintf(stderr, gettext("invalid vdev "
965 "specification: at least one toplevel vdev must be "
970 if (altroot != NULL && altroot[0] != '/') {
971 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
972 "must be an absolute path\n"), altroot);
977 * Check the validity of the mountpoint and direct the user to use the
978 * '-m' mountpoint option if it looks like its in use.
980 if (mountpoint == NULL ||
981 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
982 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
983 char buf[MAXPATHLEN];
986 if (mountpoint && mountpoint[0] != '/') {
987 (void) fprintf(stderr, gettext("invalid mountpoint "
988 "'%s': must be an absolute path, 'legacy', or "
989 "'none'\n"), mountpoint);
993 if (mountpoint == NULL) {
995 (void) snprintf(buf, sizeof (buf), "%s/%s",
998 (void) snprintf(buf, sizeof (buf), "/%s",
1001 if (altroot != NULL)
1002 (void) snprintf(buf, sizeof (buf), "%s%s",
1003 altroot, mountpoint);
1005 (void) snprintf(buf, sizeof (buf), "%s",
1009 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1010 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1011 "%s\n"), buf, strerror(errno));
1012 (void) fprintf(stderr, gettext("use '-m' "
1013 "option to provide a different default\n"));
1018 while (count < 3 && readdir(dirp) != NULL)
1020 (void) closedir(dirp);
1023 (void) fprintf(stderr, gettext("mountpoint "
1024 "'%s' exists and is not empty\n"), buf);
1025 (void) fprintf(stderr, gettext("use '-m' "
1026 "option to provide a "
1027 "different default\n"));
1034 * Now that the mountpoint's validity has been checked, ensure that
1035 * the property is set appropriately prior to creating the pool.
1037 if (mountpoint != NULL) {
1038 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1039 mountpoint, &fsprops, B_FALSE);
1047 * For a dry run invocation, print out a basic message and run
1048 * through all the vdevs in the list and print out in an
1049 * appropriate hierarchy.
1051 (void) printf(gettext("would create '%s' with the "
1052 "following layout:\n\n"), poolname);
1054 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1055 if (num_logs(nvroot) > 0)
1056 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1061 * Hand off to libzfs.
1063 if (enable_all_pool_feat) {
1065 for (i = 0; i < SPA_FEATURES; i++) {
1066 char propname[MAXPATHLEN];
1067 zfeature_info_t *feat = &spa_feature_table[i];
1069 (void) snprintf(propname, sizeof (propname),
1070 "feature@%s", feat->fi_uname);
1073 * Skip feature if user specified it manually
1074 * on the command line.
1076 if (nvlist_exists(props, propname))
1079 ret = add_prop_list(propname,
1080 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1087 if (zpool_create(g_zfs, poolname,
1088 nvroot, props, fsprops) == 0) {
1089 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1090 ZFS_TYPE_FILESYSTEM);
1092 if (zfs_mount(pool, NULL, 0) == 0)
1093 ret = zfs_shareall(pool);
1096 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1097 (void) fprintf(stderr, gettext("pool name may have "
1103 nvlist_free(nvroot);
1104 nvlist_free(fsprops);
1108 nvlist_free(fsprops);
1115 * zpool destroy <pool>
1117 * -f Forcefully unmount any datasets
1119 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1122 zpool_do_destroy(int argc, char **argv)
1124 boolean_t force = B_FALSE;
1127 zpool_handle_t *zhp;
1131 while ((c = getopt(argc, argv, "f")) != -1) {
1137 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1146 /* check arguments */
1148 (void) fprintf(stderr, gettext("missing pool argument\n"));
1152 (void) fprintf(stderr, gettext("too many arguments\n"));
1158 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1160 * As a special case, check for use of '/' in the name, and
1161 * direct the user to use 'zfs destroy' instead.
1163 if (strchr(pool, '/') != NULL)
1164 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1165 "destroy a dataset\n"));
1169 if (zpool_disable_datasets(zhp, force) != 0) {
1170 (void) fprintf(stderr, gettext("could not destroy '%s': "
1171 "could not unmount datasets\n"), zpool_get_name(zhp));
1175 /* The history must be logged as part of the export */
1176 log_history = B_FALSE;
1178 ret = (zpool_destroy(zhp, history_str) != 0);
1186 * zpool export [-f] <pool> ...
1188 * -f Forcefully unmount datasets
1190 * Export the given pools. By default, the command will attempt to cleanly
1191 * unmount any active datasets within the pool. If the '-f' flag is specified,
1192 * then the datasets will be forcefully unmounted.
1195 zpool_do_export(int argc, char **argv)
1197 boolean_t force = B_FALSE;
1198 boolean_t hardforce = B_FALSE;
1200 zpool_handle_t *zhp;
1205 while ((c = getopt(argc, argv, "fF")) != -1) {
1214 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1223 /* check arguments */
1225 (void) fprintf(stderr, gettext("missing pool argument\n"));
1230 for (i = 0; i < argc; i++) {
1231 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1236 if (zpool_disable_datasets(zhp, force) != 0) {
1242 /* The history must be logged as part of the export */
1243 log_history = B_FALSE;
1246 if (zpool_export_force(zhp, history_str) != 0)
1248 } else if (zpool_export(zhp, force, history_str) != 0) {
1259 * Given a vdev configuration, determine the maximum width needed for the device
1263 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1265 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1270 if (strlen(name) + depth > max)
1271 max = strlen(name) + depth;
1275 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1276 &child, &children) == 0) {
1277 for (c = 0; c < children; c++)
1278 if ((ret = max_width(zhp, child[c], depth + 2,
1283 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1284 &child, &children) == 0) {
1285 for (c = 0; c < children; c++)
1286 if ((ret = max_width(zhp, child[c], depth + 2,
1291 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1292 &child, &children) == 0) {
1293 for (c = 0; c < children; c++)
1294 if ((ret = max_width(zhp, child[c], depth + 2,
1303 typedef struct spare_cbdata {
1305 zpool_handle_t *cb_zhp;
1309 find_vdev(nvlist_t *nv, uint64_t search)
1315 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1319 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1320 &child, &children) == 0) {
1321 for (c = 0; c < children; c++)
1322 if (find_vdev(child[c], search))
1330 find_spare(zpool_handle_t *zhp, void *data)
1332 spare_cbdata_t *cbp = data;
1333 nvlist_t *config, *nvroot;
1335 config = zpool_get_config(zhp, NULL);
1336 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1339 if (find_vdev(nvroot, cbp->cb_guid)) {
1349 * Print out configuration state as requested by status_callback.
1352 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1353 int namewidth, int depth, boolean_t isspare)
1357 pool_scan_stat_t *ps = NULL;
1359 char rbuf[6], wbuf[6], cbuf[6];
1361 uint64_t notpresent;
1365 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1366 &child, &children) != 0)
1369 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1370 (uint64_t **)&vs, &c) == 0);
1372 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1375 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1378 if (vs->vs_aux == VDEV_AUX_SPARED)
1380 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1384 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1388 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1389 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1390 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1391 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1394 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1395 ¬present) == 0) {
1397 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1398 (void) printf(" was %s", path);
1399 } else if (vs->vs_aux != 0) {
1402 switch (vs->vs_aux) {
1403 case VDEV_AUX_OPEN_FAILED:
1404 (void) printf(gettext("cannot open"));
1407 case VDEV_AUX_BAD_GUID_SUM:
1408 (void) printf(gettext("missing device"));
1411 case VDEV_AUX_NO_REPLICAS:
1412 (void) printf(gettext("insufficient replicas"));
1415 case VDEV_AUX_VERSION_NEWER:
1416 (void) printf(gettext("newer version"));
1419 case VDEV_AUX_UNSUP_FEAT:
1420 (void) printf(gettext("unsupported feature(s)"));
1423 case VDEV_AUX_SPARED:
1424 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1426 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1427 if (strcmp(zpool_get_name(cb.cb_zhp),
1428 zpool_get_name(zhp)) == 0)
1429 (void) printf(gettext("currently in "
1432 (void) printf(gettext("in use by "
1434 zpool_get_name(cb.cb_zhp));
1435 zpool_close(cb.cb_zhp);
1437 (void) printf(gettext("currently in use"));
1441 case VDEV_AUX_ERR_EXCEEDED:
1442 (void) printf(gettext("too many errors"));
1445 case VDEV_AUX_IO_FAILURE:
1446 (void) printf(gettext("experienced I/O failures"));
1449 case VDEV_AUX_BAD_LOG:
1450 (void) printf(gettext("bad intent log"));
1453 case VDEV_AUX_EXTERNAL:
1454 (void) printf(gettext("external device fault"));
1457 case VDEV_AUX_SPLIT_POOL:
1458 (void) printf(gettext("split into new pool"));
1462 (void) printf(gettext("corrupted data"));
1467 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1468 (uint64_t **)&ps, &c);
1470 if (ps && ps->pss_state == DSS_SCANNING &&
1471 vs->vs_scan_processed != 0 && children == 0) {
1472 (void) printf(gettext(" (%s)"),
1473 (ps->pss_func == POOL_SCAN_RESILVER) ?
1474 "resilvering" : "repairing");
1477 (void) printf("\n");
1479 for (c = 0; c < children; c++) {
1480 uint64_t islog = B_FALSE, ishole = B_FALSE;
1482 /* Don't print logs or holes here */
1483 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1485 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1487 if (islog || ishole)
1489 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1490 print_status_config(zhp, vname, child[c],
1491 namewidth, depth + 2, isspare);
1498 * Print the configuration of an exported pool. Iterate over all vdevs in the
1499 * pool, printing out the name and status for each one.
1502 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1509 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1510 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1511 strcmp(type, VDEV_TYPE_HOLE) == 0)
1514 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1515 (uint64_t **)&vs, &c) == 0);
1517 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1518 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1520 if (vs->vs_aux != 0) {
1523 switch (vs->vs_aux) {
1524 case VDEV_AUX_OPEN_FAILED:
1525 (void) printf(gettext("cannot open"));
1528 case VDEV_AUX_BAD_GUID_SUM:
1529 (void) printf(gettext("missing device"));
1532 case VDEV_AUX_NO_REPLICAS:
1533 (void) printf(gettext("insufficient replicas"));
1536 case VDEV_AUX_VERSION_NEWER:
1537 (void) printf(gettext("newer version"));
1540 case VDEV_AUX_UNSUP_FEAT:
1541 (void) printf(gettext("unsupported feature(s)"));
1544 case VDEV_AUX_ERR_EXCEEDED:
1545 (void) printf(gettext("too many errors"));
1549 (void) printf(gettext("corrupted data"));
1553 (void) printf("\n");
1555 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1556 &child, &children) != 0)
1559 for (c = 0; c < children; c++) {
1560 uint64_t is_log = B_FALSE;
1562 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1567 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1568 print_import_config(vname, child[c], namewidth, depth + 2);
1572 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1573 &child, &children) == 0) {
1574 (void) printf(gettext("\tcache\n"));
1575 for (c = 0; c < children; c++) {
1576 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1577 (void) printf("\t %s\n", vname);
1582 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1583 &child, &children) == 0) {
1584 (void) printf(gettext("\tspares\n"));
1585 for (c = 0; c < children; c++) {
1586 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1587 (void) printf("\t %s\n", vname);
1595 * Logs are recorded as top level vdevs in the main pool child array
1596 * but with "is_log" set to 1. We use either print_status_config() or
1597 * print_import_config() to print the top level logs then any log
1598 * children (eg mirrored slogs) are printed recursively - which
1599 * works because only the top level vdev is marked "is_log"
1602 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1607 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1611 (void) printf(gettext("\tlogs\n"));
1613 for (c = 0; c < children; c++) {
1614 uint64_t is_log = B_FALSE;
1617 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1621 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1623 print_status_config(zhp, name, child[c], namewidth,
1626 print_import_config(name, child[c], namewidth, 2);
1632 * Display the status for the given pool.
1635 show_import(nvlist_t *config)
1637 uint64_t pool_state;
1643 zpool_status_t reason;
1644 zpool_errata_t errata;
1650 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1652 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1654 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1656 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1659 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1660 (uint64_t **)&vs, &vsc) == 0);
1661 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1663 reason = zpool_import_status(config, &msgid, &errata);
1665 (void) printf(gettext(" pool: %s\n"), name);
1666 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1667 (void) printf(gettext(" state: %s"), health);
1668 if (pool_state == POOL_STATE_DESTROYED)
1669 (void) printf(gettext(" (DESTROYED)"));
1670 (void) printf("\n");
1673 case ZPOOL_STATUS_MISSING_DEV_R:
1674 case ZPOOL_STATUS_MISSING_DEV_NR:
1675 case ZPOOL_STATUS_BAD_GUID_SUM:
1676 (void) printf(gettext(" status: One or more devices are "
1677 "missing from the system.\n"));
1680 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1681 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1682 (void) printf(gettext(" status: One or more devices contains "
1683 "corrupted data.\n"));
1686 case ZPOOL_STATUS_CORRUPT_DATA:
1688 gettext(" status: The pool data is corrupted.\n"));
1691 case ZPOOL_STATUS_OFFLINE_DEV:
1692 (void) printf(gettext(" status: One or more devices "
1693 "are offlined.\n"));
1696 case ZPOOL_STATUS_CORRUPT_POOL:
1697 (void) printf(gettext(" status: The pool metadata is "
1701 case ZPOOL_STATUS_VERSION_OLDER:
1702 (void) printf(gettext(" status: The pool is formatted using a "
1703 "legacy on-disk version.\n"));
1706 case ZPOOL_STATUS_VERSION_NEWER:
1707 (void) printf(gettext(" status: The pool is formatted using an "
1708 "incompatible version.\n"));
1711 case ZPOOL_STATUS_FEAT_DISABLED:
1712 (void) printf(gettext(" status: Some supported features are "
1713 "not enabled on the pool.\n"));
1716 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1717 (void) printf(gettext("status: The pool uses the following "
1718 "feature(s) not supported on this sytem:\n"));
1719 zpool_print_unsup_feat(config);
1722 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1723 (void) printf(gettext("status: The pool can only be accessed "
1724 "in read-only mode on this system. It\n\tcannot be "
1725 "accessed in read-write mode because it uses the "
1726 "following\n\tfeature(s) not supported on this system:\n"));
1727 zpool_print_unsup_feat(config);
1730 case ZPOOL_STATUS_HOSTID_MISMATCH:
1731 (void) printf(gettext(" status: The pool was last accessed by "
1732 "another system.\n"));
1735 case ZPOOL_STATUS_FAULTED_DEV_R:
1736 case ZPOOL_STATUS_FAULTED_DEV_NR:
1737 (void) printf(gettext(" status: One or more devices are "
1741 case ZPOOL_STATUS_BAD_LOG:
1742 (void) printf(gettext(" status: An intent log record cannot be "
1746 case ZPOOL_STATUS_RESILVERING:
1747 (void) printf(gettext(" status: One or more devices were being "
1751 case ZPOOL_STATUS_ERRATA:
1752 (void) printf(gettext(" status: Errata #%d detected.\n"),
1758 * No other status can be seen when importing pools.
1760 assert(reason == ZPOOL_STATUS_OK);
1764 * Print out an action according to the overall state of the pool.
1766 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1767 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1768 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1769 (void) printf(gettext(" action: The pool can be "
1770 "imported using its name or numeric identifier, "
1771 "though\n\tsome features will not be available "
1772 "without an explicit 'zpool upgrade'.\n"));
1773 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1774 (void) printf(gettext(" action: The pool can be "
1775 "imported using its name or numeric "
1776 "identifier and\n\tthe '-f' flag.\n"));
1777 } else if (reason == ZPOOL_STATUS_ERRATA) {
1779 case ZPOOL_ERRATA_NONE:
1782 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1783 (void) printf(gettext(" action: The pool can "
1784 "be imported using its name or numeric "
1785 "identifier,\n\thowever there is a compat"
1786 "ibility issue which should be corrected"
1787 "\n\tby running 'zpool scrub'\n"));
1790 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1791 (void) printf(gettext(" action: The pool can"
1792 "not be imported with this version of ZFS "
1793 "due to\n\tan active asynchronous destroy. "
1794 "Revert to an earlier version\n\tand "
1795 "allow the destroy to complete before "
1801 * All errata must contain an action message.
1806 (void) printf(gettext(" action: The pool can be "
1807 "imported using its name or numeric "
1810 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1811 (void) printf(gettext(" action: The pool can be imported "
1812 "despite missing or damaged devices. The\n\tfault "
1813 "tolerance of the pool may be compromised if imported.\n"));
1816 case ZPOOL_STATUS_VERSION_NEWER:
1817 (void) printf(gettext(" action: The pool cannot be "
1818 "imported. Access the pool on a system running "
1819 "newer\n\tsoftware, or recreate the pool from "
1822 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1823 (void) printf(gettext("action: The pool cannot be "
1824 "imported. Access the pool on a system that "
1825 "supports\n\tthe required feature(s), or recreate "
1826 "the pool from backup.\n"));
1828 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1829 (void) printf(gettext("action: The pool cannot be "
1830 "imported in read-write mode. Import the pool "
1832 "\t\"-o readonly=on\", access the pool on a system "
1833 "that supports the\n\trequired feature(s), or "
1834 "recreate the pool from backup.\n"));
1836 case ZPOOL_STATUS_MISSING_DEV_R:
1837 case ZPOOL_STATUS_MISSING_DEV_NR:
1838 case ZPOOL_STATUS_BAD_GUID_SUM:
1839 (void) printf(gettext(" action: The pool cannot be "
1840 "imported. Attach the missing\n\tdevices and try "
1844 (void) printf(gettext(" action: The pool cannot be "
1845 "imported due to damaged devices or data.\n"));
1849 /* Print the comment attached to the pool. */
1850 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1851 (void) printf(gettext("comment: %s\n"), comment);
1854 * If the state is "closed" or "can't open", and the aux state
1855 * is "corrupt data":
1857 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1858 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1859 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1860 if (pool_state == POOL_STATE_DESTROYED)
1861 (void) printf(gettext("\tThe pool was destroyed, "
1862 "but can be imported using the '-Df' flags.\n"));
1863 else if (pool_state != POOL_STATE_EXPORTED)
1864 (void) printf(gettext("\tThe pool may be active on "
1865 "another system, but can be imported using\n\t"
1866 "the '-f' flag.\n"));
1870 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1873 (void) printf(gettext(" config:\n\n"));
1875 namewidth = max_width(NULL, nvroot, 0, 0);
1879 print_import_config(name, nvroot, namewidth, 0);
1880 if (num_logs(nvroot) > 0)
1881 print_logs(NULL, nvroot, namewidth, B_FALSE);
1883 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1884 (void) printf(gettext("\n\tAdditional devices are known to "
1885 "be part of this pool, though their\n\texact "
1886 "configuration cannot be determined.\n"));
1891 * Perform the import for the given configuration. This passes the heavy
1892 * lifting off to zpool_import_props(), and then mounts the datasets contained
1896 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1897 nvlist_t *props, int flags)
1899 zpool_handle_t *zhp;
1904 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1907 verify(nvlist_lookup_uint64(config,
1908 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1909 verify(nvlist_lookup_uint64(config,
1910 ZPOOL_CONFIG_VERSION, &version) == 0);
1911 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1912 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1913 "is formatted using an unsupported ZFS version\n"), name);
1915 } else if (state != POOL_STATE_EXPORTED &&
1916 !(flags & ZFS_IMPORT_ANY_HOST)) {
1917 uint64_t hostid = 0;
1918 unsigned long system_hostid = gethostid() & 0xffffffff;
1920 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1923 if (hostid != 0 && (unsigned long)hostid != system_hostid) {
1928 verify(nvlist_lookup_string(config,
1929 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1930 verify(nvlist_lookup_uint64(config,
1931 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1933 (void) fprintf(stderr, gettext("cannot import "
1934 "'%s': pool may be in use from other "
1935 "system, it was last accessed by %s "
1936 "(hostid: 0x%lx) on %s"), name, hostname,
1937 (unsigned long)hostid,
1938 asctime(localtime(&t)));
1939 (void) fprintf(stderr, gettext("use '-f' to "
1940 "import anyway\n"));
1945 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1948 if (newname != NULL)
1949 name = (char *)newname;
1951 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1954 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1955 !(flags & ZFS_IMPORT_ONLY) &&
1956 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1966 * zpool import [-d dir] [-D]
1967 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1968 * [-d dir | -c cachefile] [-f] -a
1969 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1970 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1972 * -c Read pool information from a cachefile instead of searching
1975 * -d Scan in a specific directory, other than /dev/. More than
1976 * one directory can be specified using multiple '-d' options.
1978 * -D Scan for previously destroyed pools or import all or only
1979 * specified destroyed pools.
1981 * -R Temporarily import the pool, with all mountpoints relative to
1982 * the given root. The pool will remain exported when the machine
1985 * -V Import even in the presence of faulted vdevs. This is an
1986 * intentionally undocumented option for testing purposes, and
1987 * treats the pool configuration as complete, leaving any bad
1988 * vdevs in the FAULTED state. In other words, it does verbatim
1991 * -f Force import, even if it appears that the pool is active.
1993 * -F Attempt rewind if necessary.
1995 * -n See if rewind would work, but don't actually rewind.
1997 * -N Import the pool but don't mount datasets.
1999 * -T Specify a starting txg to use for import. This option is
2000 * intentionally undocumented option for testing purposes.
2002 * -a Import all pools found.
2004 * -o Set property=value and/or temporary mount options (without '=').
2006 * The import command scans for pools to import, and import pools based on pool
2007 * name and GUID. The pool can also be renamed as part of the import process.
2010 zpool_do_import(int argc, char **argv)
2012 char **searchdirs = NULL;
2013 char *env, *envdup = NULL;
2017 nvlist_t *pools = NULL;
2018 boolean_t do_all = B_FALSE;
2019 boolean_t do_destroyed = B_FALSE;
2020 char *mntopts = NULL;
2023 uint64_t searchguid = 0;
2024 char *searchname = NULL;
2026 nvlist_t *found_config;
2027 nvlist_t *policy = NULL;
2028 nvlist_t *props = NULL;
2030 int flags = ZFS_IMPORT_NORMAL;
2031 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2032 boolean_t dryrun = B_FALSE;
2033 boolean_t do_rewind = B_FALSE;
2034 boolean_t xtreme_rewind = B_FALSE;
2035 uint64_t pool_state, txg = -1ULL;
2036 char *cachefile = NULL;
2037 importargs_t idata = { 0 };
2041 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2050 if (searchdirs == NULL) {
2051 searchdirs = safe_malloc(sizeof (char *));
2053 char **tmp = safe_malloc((nsearch + 1) *
2055 bcopy(searchdirs, tmp, nsearch *
2060 searchdirs[nsearch++] = optarg;
2063 do_destroyed = B_TRUE;
2066 flags |= ZFS_IMPORT_ANY_HOST;
2072 flags |= ZFS_IMPORT_MISSING_LOG;
2078 flags |= ZFS_IMPORT_ONLY;
2081 if ((propval = strchr(optarg, '=')) != NULL) {
2084 if (add_prop_list(optarg, propval,
2092 if (add_prop_list(zpool_prop_to_name(
2093 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2095 if (add_prop_list_default(zpool_prop_to_name(
2096 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2100 flags |= ZFS_IMPORT_TEMP_NAME;
2101 if (add_prop_list_default(zpool_prop_to_name(
2102 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2108 txg = strtoull(optarg, &endptr, 0);
2109 if (errno != 0 || *endptr != '\0') {
2110 (void) fprintf(stderr,
2111 gettext("invalid txg value\n"));
2114 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2117 flags |= ZFS_IMPORT_VERBATIM;
2120 xtreme_rewind = B_TRUE;
2123 (void) fprintf(stderr, gettext("missing argument for "
2124 "'%c' option\n"), optopt);
2128 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2137 if (cachefile && nsearch != 0) {
2138 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2142 if ((dryrun || xtreme_rewind) && !do_rewind) {
2143 (void) fprintf(stderr,
2144 gettext("-n or -X only meaningful with -F\n"));
2148 rewind_policy = ZPOOL_TRY_REWIND;
2150 rewind_policy = ZPOOL_DO_REWIND;
2152 rewind_policy |= ZPOOL_EXTREME_REWIND;
2154 /* In the future, we can capture further policy and include it here */
2155 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2156 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2157 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2160 /* check argument count */
2163 (void) fprintf(stderr, gettext("too many arguments\n"));
2168 (void) fprintf(stderr, gettext("too many arguments\n"));
2173 * Check for the SYS_CONFIG privilege. We do this explicitly
2174 * here because otherwise any attempt to discover pools will
2177 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2178 (void) fprintf(stderr, gettext("cannot "
2179 "discover pools: permission denied\n"));
2180 if (searchdirs != NULL)
2183 nvlist_free(policy);
2189 * Depending on the arguments given, we do one of the following:
2191 * <none> Iterate through all pools and display information about
2194 * -a Iterate through all pools and try to import each one.
2196 * <id> Find the pool that corresponds to the given GUID/pool
2197 * name and import that one.
2199 * -D Above options applies only to destroyed pools.
2205 searchguid = strtoull(argv[0], &endptr, 10);
2206 if (errno != 0 || *endptr != '\0')
2207 searchname = argv[0];
2208 found_config = NULL;
2211 * User specified a name or guid. Ensure it's unique.
2213 idata.unique = B_TRUE;
2217 * Check the environment for the preferred search path.
2219 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2222 envdup = strdup(env);
2224 dir = strtok(envdup, ":");
2225 while (dir != NULL) {
2226 if (searchdirs == NULL) {
2227 searchdirs = safe_malloc(sizeof (char *));
2229 char **tmp = safe_malloc((nsearch + 1) *
2231 bcopy(searchdirs, tmp, nsearch *
2236 searchdirs[nsearch++] = dir;
2237 dir = strtok(NULL, ":");
2241 idata.path = searchdirs;
2242 idata.paths = nsearch;
2243 idata.poolname = searchname;
2244 idata.guid = searchguid;
2245 idata.cachefile = cachefile;
2247 pools = zpool_search_import(g_zfs, &idata);
2249 if (pools != NULL && idata.exists &&
2250 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2251 (void) fprintf(stderr, gettext("cannot import '%s': "
2252 "a pool with that name already exists\n"),
2254 (void) fprintf(stderr, gettext("use the form '%s "
2255 "<pool | id> <newpool>' to give it a new name\n"),
2258 } else if (pools == NULL && idata.exists) {
2259 (void) fprintf(stderr, gettext("cannot import '%s': "
2260 "a pool with that name is already created/imported,\n"),
2262 (void) fprintf(stderr, gettext("and no additional pools "
2263 "with that name were found\n"));
2265 } else if (pools == NULL) {
2267 (void) fprintf(stderr, gettext("cannot import '%s': "
2268 "no such pool available\n"), argv[0]);
2274 if (searchdirs != NULL)
2278 nvlist_free(policy);
2283 * At this point we have a list of import candidate configs. Even if
2284 * we were searching by pool name or guid, we still need to
2285 * post-process the list to deal with pool state and possible
2291 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2293 verify(nvpair_value_nvlist(elem, &config) == 0);
2295 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2297 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2299 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2302 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2309 (void) printf("\n");
2312 err |= do_import(config, NULL, mntopts,
2315 show_import(config);
2317 } else if (searchname != NULL) {
2321 * We are searching for a pool based on name.
2323 verify(nvlist_lookup_string(config,
2324 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2326 if (strcmp(name, searchname) == 0) {
2327 if (found_config != NULL) {
2328 (void) fprintf(stderr, gettext(
2329 "cannot import '%s': more than "
2330 "one matching pool\n"), searchname);
2331 (void) fprintf(stderr, gettext(
2332 "import by numeric ID instead\n"));
2335 found_config = config;
2341 * Search for a pool by guid.
2343 verify(nvlist_lookup_uint64(config,
2344 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2346 if (guid == searchguid)
2347 found_config = config;
2352 * If we were searching for a specific pool, verify that we found a
2353 * pool, and then do the import.
2355 if (argc != 0 && err == 0) {
2356 if (found_config == NULL) {
2357 (void) fprintf(stderr, gettext("cannot import '%s': "
2358 "no such pool available\n"), argv[0]);
2361 err |= do_import(found_config, argc == 1 ? NULL :
2362 argv[1], mntopts, props, flags);
2367 * If we were just looking for pools, report an error if none were
2370 if (argc == 0 && first)
2371 (void) fprintf(stderr,
2372 gettext("no pools available to import\n"));
2377 nvlist_free(policy);
2378 if (searchdirs != NULL)
2383 return (err ? 1 : 0);
2386 typedef struct iostat_cbdata {
2387 boolean_t cb_verbose;
2390 zpool_list_t *cb_list;
2394 print_iostat_separator(iostat_cbdata_t *cb)
2398 for (i = 0; i < cb->cb_namewidth; i++)
2400 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2404 print_iostat_header(iostat_cbdata_t *cb)
2406 (void) printf("%*s capacity operations bandwidth\n",
2407 cb->cb_namewidth, "");
2408 (void) printf("%-*s alloc free read write read write\n",
2409 cb->cb_namewidth, "pool");
2410 print_iostat_separator(cb);
2414 * Display a single statistic.
2417 print_one_stat(uint64_t value)
2421 zfs_nicenum(value, buf, sizeof (buf));
2422 (void) printf(" %5s", buf);
2426 * Print out all the statistics for the given vdev. This can either be the
2427 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2428 * is a verbose output, and we don't want to display the toplevel pool stats.
2431 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2432 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2434 nvlist_t **oldchild, **newchild;
2436 vdev_stat_t *oldvs, *newvs;
2437 vdev_stat_t zerovs = { 0 };
2442 if (oldnv != NULL) {
2443 verify(nvlist_lookup_uint64_array(oldnv,
2444 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2449 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2450 (uint64_t **)&newvs, &c) == 0);
2452 if (strlen(name) + depth > cb->cb_namewidth)
2453 (void) printf("%*s%s", depth, "", name);
2455 (void) printf("%*s%s%*s", depth, "", name,
2456 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2458 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2463 scale = (double)NANOSEC / tdelta;
2465 /* only toplevel vdevs have capacity stats */
2466 if (newvs->vs_space == 0) {
2467 (void) printf(" - -");
2469 print_one_stat(newvs->vs_alloc);
2470 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2473 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2474 oldvs->vs_ops[ZIO_TYPE_READ])));
2476 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2477 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2479 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2480 oldvs->vs_bytes[ZIO_TYPE_READ])));
2482 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2483 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2485 (void) printf("\n");
2487 if (!cb->cb_verbose)
2490 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2491 &newchild, &children) != 0)
2494 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2495 &oldchild, &c) != 0)
2498 for (c = 0; c < children; c++) {
2499 uint64_t ishole = B_FALSE, islog = B_FALSE;
2501 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2504 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2507 if (ishole || islog)
2510 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2511 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2512 newchild[c], cb, depth + 2);
2517 * Log device section
2520 if (num_logs(newnv) > 0) {
2521 (void) printf("%-*s - - - - - "
2522 "-\n", cb->cb_namewidth, "logs");
2524 for (c = 0; c < children; c++) {
2525 uint64_t islog = B_FALSE;
2526 (void) nvlist_lookup_uint64(newchild[c],
2527 ZPOOL_CONFIG_IS_LOG, &islog);
2530 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2532 print_vdev_stats(zhp, vname, oldnv ?
2533 oldchild[c] : NULL, newchild[c],
2542 * Include level 2 ARC devices in iostat output
2544 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2545 &newchild, &children) != 0)
2548 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2549 &oldchild, &c) != 0)
2553 (void) printf("%-*s - - - - - "
2554 "-\n", cb->cb_namewidth, "cache");
2555 for (c = 0; c < children; c++) {
2556 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2558 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2559 newchild[c], cb, depth + 2);
2566 refresh_iostat(zpool_handle_t *zhp, void *data)
2568 iostat_cbdata_t *cb = data;
2572 * If the pool has disappeared, remove it from the list and continue.
2574 if (zpool_refresh_stats(zhp, &missing) != 0)
2578 pool_list_remove(cb->cb_list, zhp);
2584 * Callback to print out the iostats for the given pool.
2587 print_iostat(zpool_handle_t *zhp, void *data)
2589 iostat_cbdata_t *cb = data;
2590 nvlist_t *oldconfig, *newconfig;
2591 nvlist_t *oldnvroot, *newnvroot;
2593 newconfig = zpool_get_config(zhp, &oldconfig);
2595 if (cb->cb_iteration == 1)
2598 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2601 if (oldconfig == NULL)
2604 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2608 * Print out the statistics for the pool.
2610 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2613 print_iostat_separator(cb);
2625 if (isatty(STDOUT_FILENO)) {
2626 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2628 columns = ws.ws_col;
2637 get_namewidth(zpool_handle_t *zhp, void *data)
2639 iostat_cbdata_t *cb = data;
2640 nvlist_t *config, *nvroot;
2643 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2644 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2646 if (!cb->cb_verbose)
2647 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2649 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2654 * The width must be at least 10, but may be as large as the
2655 * column width - 42 so that we can still fit in one line.
2657 columns = get_columns();
2659 if (cb->cb_namewidth < 10)
2660 cb->cb_namewidth = 10;
2661 if (cb->cb_namewidth > columns - 42)
2662 cb->cb_namewidth = columns - 42;
2668 * Parse the input string, get the 'interval' and 'count' value if there is one.
2671 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2674 unsigned long interval = 0, count = 0;
2678 * Determine if the last argument is an integer or a pool name
2680 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2684 interval = strtoul(argv[argc - 1], &end, 10);
2686 if (*end == '\0' && errno == 0) {
2687 if (interval == 0) {
2688 (void) fprintf(stderr, gettext("interval "
2689 "cannot be zero\n"));
2693 * Ignore the last parameter
2698 * If this is not a valid number, just plow on. The
2699 * user will get a more informative error message later
2707 * If the last argument is also an integer, then we have both a count
2710 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2715 interval = strtoul(argv[argc - 1], &end, 10);
2717 if (*end == '\0' && errno == 0) {
2718 if (interval == 0) {
2719 (void) fprintf(stderr, gettext("interval "
2720 "cannot be zero\n"));
2725 * Ignore the last parameter
2739 get_timestamp_arg(char c)
2742 timestamp_fmt = UDATE;
2744 timestamp_fmt = DDATE;
2750 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2752 * -v Display statistics for individual vdevs
2753 * -T Display a timestamp in date(1) or Unix format
2755 * This command can be tricky because we want to be able to deal with pool
2756 * creation/destruction as well as vdev configuration changes. The bulk of this
2757 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2758 * on pool_list_update() to detect the addition of new pools. Configuration
2759 * changes are all handled within libzfs.
2762 zpool_do_iostat(int argc, char **argv)
2767 unsigned long interval = 0, count = 0;
2769 boolean_t verbose = B_FALSE;
2773 while ((c = getopt(argc, argv, "T:v")) != -1) {
2776 get_timestamp_arg(*optarg);
2782 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2791 get_interval_count(&argc, argv, &interval, &count);
2794 * Construct the list of all interesting pools.
2797 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2800 if (pool_list_count(list) == 0 && argc != 0) {
2801 pool_list_free(list);
2805 if (pool_list_count(list) == 0 && interval == 0) {
2806 pool_list_free(list);
2807 (void) fprintf(stderr, gettext("no pools available\n"));
2812 * Enter the main iostat loop.
2815 cb.cb_verbose = verbose;
2816 cb.cb_iteration = 0;
2817 cb.cb_namewidth = 0;
2820 pool_list_update(list);
2822 if ((npools = pool_list_count(list)) == 0)
2823 (void) fprintf(stderr, gettext("no pools available\n"));
2826 * Refresh all statistics. This is done as an
2827 * explicit step before calculating the maximum name
2828 * width, so that any * configuration changes are
2829 * properly accounted for.
2831 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2835 * Iterate over all pools to determine the maximum width
2836 * for the pool / device name column across all pools.
2838 cb.cb_namewidth = 0;
2839 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2842 if (timestamp_fmt != NODATE)
2843 print_timestamp(timestamp_fmt);
2846 * If it's the first time, or verbose mode, print the
2849 if (++cb.cb_iteration == 1 || verbose)
2850 print_iostat_header(&cb);
2852 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2855 * If there's more than one pool, and we're not in
2856 * verbose mode (which prints a separator for us),
2857 * then print a separator.
2859 if (npools > 1 && !verbose)
2860 print_iostat_separator(&cb);
2863 (void) printf("\n");
2867 * Flush the output so that redirection to a file isn't buffered
2870 (void) fflush(stdout);
2875 if (count != 0 && --count == 0)
2878 (void) sleep(interval);
2881 pool_list_free(list);
2886 typedef struct list_cbdata {
2887 boolean_t cb_verbose;
2889 boolean_t cb_scripted;
2890 zprop_list_t *cb_proplist;
2894 * Given a list of columns to display, output appropriate headers for each one.
2897 print_header(list_cbdata_t *cb)
2899 zprop_list_t *pl = cb->cb_proplist;
2900 char headerbuf[ZPOOL_MAXPROPLEN];
2902 boolean_t first = B_TRUE;
2903 boolean_t right_justify;
2906 for (; pl != NULL; pl = pl->pl_next) {
2907 width = pl->pl_width;
2908 if (first && cb->cb_verbose) {
2910 * Reset the width to accommodate the verbose listing
2913 width = cb->cb_namewidth;
2921 right_justify = B_FALSE;
2922 if (pl->pl_prop != ZPROP_INVAL) {
2923 header = zpool_prop_column_name(pl->pl_prop);
2924 right_justify = zpool_prop_align_right(pl->pl_prop);
2928 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2929 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2930 headerbuf[i] = '\0';
2934 if (pl->pl_next == NULL && !right_justify)
2935 (void) printf("%s", header);
2936 else if (right_justify)
2937 (void) printf("%*s", (int)width, header);
2939 (void) printf("%-*s", (int)width, header);
2942 (void) printf("\n");
2946 * Given a pool and a list of properties, print out all the properties according
2947 * to the described layout.
2950 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2952 zprop_list_t *pl = cb->cb_proplist;
2953 boolean_t first = B_TRUE;
2954 char property[ZPOOL_MAXPROPLEN];
2956 boolean_t right_justify;
2959 for (; pl != NULL; pl = pl->pl_next) {
2961 width = pl->pl_width;
2962 if (first && cb->cb_verbose) {
2964 * Reset the width to accommodate the verbose listing
2967 width = cb->cb_namewidth;
2971 if (cb->cb_scripted)
2972 (void) printf("\t");
2979 right_justify = B_FALSE;
2980 if (pl->pl_prop != ZPROP_INVAL) {
2981 if (zpool_get_prop(zhp, pl->pl_prop, property,
2982 sizeof (property), NULL) != 0)
2987 right_justify = zpool_prop_align_right(pl->pl_prop);
2988 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2989 zpool_prop_unsupported(pl->pl_user_prop)) &&
2990 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2991 sizeof (property)) == 0) {
2999 * If this is being called in scripted mode, or if this is the
3000 * last column and it is left-justified, don't include a width
3003 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3004 (void) printf("%s", propstr);
3005 else if (right_justify)
3006 (void) printf("%*s", (int)width, propstr);
3008 (void) printf("%-*s", (int)width, propstr);
3011 (void) printf("\n");
3015 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3020 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3023 case ZPOOL_PROP_EXPANDSZ:
3025 (void) strlcpy(propval, "-", sizeof (propval));
3027 zfs_nicenum(value, propval, sizeof (propval));
3029 case ZPOOL_PROP_FRAGMENTATION:
3030 if (value == ZFS_FRAG_INVALID) {
3031 (void) strlcpy(propval, "-", sizeof (propval));
3033 (void) snprintf(propval, sizeof (propval), "%llu%%",
3034 (unsigned long long)value);
3037 case ZPOOL_PROP_CAPACITY:
3038 (void) snprintf(propval, sizeof (propval), "%llu%%",
3039 (unsigned long long)value);
3042 zfs_nicenum(value, propval, sizeof (propval));
3046 (void) strlcpy(propval, "-", sizeof (propval));
3049 (void) printf("\t%s", propval);
3051 (void) printf(" %*s", (int)width, propval);
3055 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3056 list_cbdata_t *cb, int depth)
3062 boolean_t scripted = cb->cb_scripted;
3064 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3065 (uint64_t **)&vs, &c) == 0);
3068 boolean_t toplevel = (vs->vs_space != 0);
3072 (void) printf("\t%s", name);
3073 else if (strlen(name) + depth > cb->cb_namewidth)
3074 (void) printf("%*s%s", depth, "", name);
3076 (void) printf("%*s%s%*s", depth, "", name,
3077 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3080 * Print the properties for the individual vdevs. Some
3081 * properties are only applicable to toplevel vdevs. The
3082 * 'toplevel' boolean value is passed to the print_one_column()
3083 * to indicate that the value is valid.
3085 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3087 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3089 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3090 scripted, toplevel);
3091 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3093 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3094 vs->vs_fragmentation, scripted,
3095 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3096 cap = (vs->vs_space == 0) ? 0 :
3097 (vs->vs_alloc * 100 / vs->vs_space);
3098 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3099 (void) printf("\n");
3102 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3103 &child, &children) != 0)
3106 for (c = 0; c < children; c++) {
3107 uint64_t ishole = B_FALSE;
3109 if (nvlist_lookup_uint64(child[c],
3110 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3113 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3114 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3119 * Include level 2 ARC devices in iostat output
3121 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3122 &child, &children) != 0)
3126 (void) printf("%-*s - - - - - "
3127 "-\n", cb->cb_namewidth, "cache");
3128 for (c = 0; c < children; c++) {
3129 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3131 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3139 * Generic callback function to list a pool.
3142 list_callback(zpool_handle_t *zhp, void *data)
3144 list_cbdata_t *cbp = data;
3148 config = zpool_get_config(zhp, NULL);
3150 print_pool(zhp, cbp);
3151 if (!cbp->cb_verbose)
3154 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3156 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3162 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3164 * -H Scripted mode. Don't display headers, and separate properties
3166 * -o List of properties to display. Defaults to
3167 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3168 * "dedupratio,health,altroot"
3169 * -T Display a timestamp in date(1) or Unix format
3171 * List all pools in the system, whether or not they're healthy. Output space
3172 * statistics for each one, as well as health status summary.
3175 zpool_do_list(int argc, char **argv)
3179 list_cbdata_t cb = { 0 };
3180 static char default_props[] =
3181 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3182 "dedupratio,health,altroot";
3183 char *props = default_props;
3184 unsigned long interval = 0, count = 0;
3186 boolean_t first = B_TRUE;
3189 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3192 cb.cb_scripted = B_TRUE;
3198 get_timestamp_arg(*optarg);
3201 cb.cb_verbose = B_TRUE;
3204 (void) fprintf(stderr, gettext("missing argument for "
3205 "'%c' option\n"), optopt);
3209 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3218 get_interval_count(&argc, argv, &interval, &count);
3220 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3223 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3226 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3227 (void) printf(gettext("no pools available\n"));
3228 zprop_free_list(cb.cb_proplist);
3233 pool_list_update(list);
3235 if (pool_list_count(list) == 0)
3238 if (timestamp_fmt != NODATE)
3239 print_timestamp(timestamp_fmt);
3241 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3245 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3250 if (count != 0 && --count == 0)
3253 (void) sleep(interval);
3256 zprop_free_list(cb.cb_proplist);
3261 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3263 boolean_t force = B_FALSE;
3266 char *poolname, *old_disk, *new_disk;
3267 zpool_handle_t *zhp;
3268 nvlist_t *props = NULL;
3273 while ((c = getopt(argc, argv, "fo:")) != -1) {
3279 if ((propval = strchr(optarg, '=')) == NULL) {
3280 (void) fprintf(stderr, gettext("missing "
3281 "'=' for -o option\n"));
3287 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3288 (add_prop_list(optarg, propval, &props, B_TRUE)))
3292 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3301 /* get pool name and check number of arguments */
3303 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3310 (void) fprintf(stderr,
3311 gettext("missing <device> specification\n"));
3319 (void) fprintf(stderr,
3320 gettext("missing <new_device> specification\n"));
3323 new_disk = old_disk;
3333 (void) fprintf(stderr, gettext("too many arguments\n"));
3337 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3340 if (zpool_get_config(zhp, NULL) == NULL) {
3341 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3347 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3349 if (nvroot == NULL) {
3354 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3356 nvlist_free(nvroot);
3363 * zpool replace [-f] <pool> <device> <new_device>
3365 * -f Force attach, even if <new_device> appears to be in use.
3367 * Replace <device> with <new_device>.
3371 zpool_do_replace(int argc, char **argv)
3373 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3377 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3379 * -f Force attach, even if <new_device> appears to be in use.
3380 * -o Set property=value.
3382 * Attach <new_device> to the mirror containing <device>. If <device> is not
3383 * part of a mirror, then <device> will be transformed into a mirror of
3384 * <device> and <new_device>. In either case, <new_device> will begin life
3385 * with a DTL of [0, now], and will immediately begin to resilver itself.
3388 zpool_do_attach(int argc, char **argv)
3390 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3394 * zpool detach [-f] <pool> <device>
3396 * -f Force detach of <device>, even if DTLs argue against it
3397 * (not supported yet)
3399 * Detach a device from a mirror. The operation will be refused if <device>
3400 * is the last device in the mirror, or if the DTLs indicate that this device
3401 * has the only valid copy of some data.
3405 zpool_do_detach(int argc, char **argv)
3408 char *poolname, *path;
3409 zpool_handle_t *zhp;
3413 while ((c = getopt(argc, argv, "f")) != -1) {
3417 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3426 /* get pool name and check number of arguments */
3428 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3433 (void) fprintf(stderr,
3434 gettext("missing <device> specification\n"));
3441 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3444 ret = zpool_vdev_detach(zhp, path);
3452 * zpool split [-n] [-o prop=val] ...
3454 * [-R altroot] <pool> <newpool> [<device> ...]
3456 * -n Do not split the pool, but display the resulting layout if
3457 * it were to be split.
3458 * -o Set property=value, or set mount options.
3459 * -R Mount the split-off pool under an alternate root.
3461 * Splits the named pool and gives it the new pool name. Devices to be split
3462 * off may be listed, provided that no more than one device is specified
3463 * per top-level vdev mirror. The newly split pool is left in an exported
3464 * state unless -R is specified.
3466 * Restrictions: the top-level of the pool pool must only be made up of
3467 * mirrors; all devices in the pool must be healthy; no device may be
3468 * undergoing a resilvering operation.
3471 zpool_do_split(int argc, char **argv)
3473 char *srcpool, *newpool, *propval;
3474 char *mntopts = NULL;
3477 zpool_handle_t *zhp;
3478 nvlist_t *config, *props = NULL;
3480 flags.dryrun = B_FALSE;
3481 flags.import = B_FALSE;
3484 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3487 flags.import = B_TRUE;
3489 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3490 &props, B_TRUE) != 0) {
3497 flags.dryrun = B_TRUE;
3500 if ((propval = strchr(optarg, '=')) != NULL) {
3503 if (add_prop_list(optarg, propval,
3504 &props, B_TRUE) != 0) {
3514 (void) fprintf(stderr, gettext("missing argument for "
3515 "'%c' option\n"), optopt);
3519 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3526 if (!flags.import && mntopts != NULL) {
3527 (void) fprintf(stderr, gettext("setting mntopts is only "
3528 "valid when importing the pool\n"));
3536 (void) fprintf(stderr, gettext("Missing pool name\n"));
3540 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3550 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3553 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3554 if (config == NULL) {
3558 (void) printf(gettext("would create '%s' with the "
3559 "following layout:\n\n"), newpool);
3560 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3562 nvlist_free(config);
3567 if (ret != 0 || flags.dryrun || !flags.import)
3571 * The split was successful. Now we need to open the new
3572 * pool and import it.
3574 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3576 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3577 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3579 (void) fprintf(stderr, gettext("Split was successful, but "
3580 "the datasets could not all be mounted\n"));
3581 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3582 "different altroot\n"), "zpool import");
3592 * zpool online <pool> <device> ...
3595 zpool_do_online(int argc, char **argv)
3599 zpool_handle_t *zhp;
3601 vdev_state_t newstate;
3605 while ((c = getopt(argc, argv, "et")) != -1) {
3608 flags |= ZFS_ONLINE_EXPAND;
3612 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3621 /* get pool name and check number of arguments */
3623 (void) fprintf(stderr, gettext("missing pool name\n"));
3627 (void) fprintf(stderr, gettext("missing device name\n"));
3633 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3636 for (i = 1; i < argc; i++) {
3637 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3638 if (newstate != VDEV_STATE_HEALTHY) {
3639 (void) printf(gettext("warning: device '%s' "
3640 "onlined, but remains in faulted state\n"),
3642 if (newstate == VDEV_STATE_FAULTED)
3643 (void) printf(gettext("use 'zpool "
3644 "clear' to restore a faulted "
3647 (void) printf(gettext("use 'zpool "
3648 "replace' to replace devices "
3649 "that are no longer present\n"));
3662 * zpool offline [-ft] <pool> <device> ...
3664 * -f Force the device into the offline state, even if doing
3665 * so would appear to compromise pool availability.
3666 * (not supported yet)
3668 * -t Only take the device off-line temporarily. The offline
3669 * state will not be persistent across reboots.
3673 zpool_do_offline(int argc, char **argv)
3677 zpool_handle_t *zhp;
3679 boolean_t istmp = B_FALSE;
3682 while ((c = getopt(argc, argv, "ft")) != -1) {
3689 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3698 /* get pool name and check number of arguments */
3700 (void) fprintf(stderr, gettext("missing pool name\n"));
3704 (void) fprintf(stderr, gettext("missing device name\n"));
3710 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3713 for (i = 1; i < argc; i++) {
3714 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3724 * zpool clear <pool> [device]
3726 * Clear all errors associated with a pool or a particular device.
3729 zpool_do_clear(int argc, char **argv)
3733 boolean_t dryrun = B_FALSE;
3734 boolean_t do_rewind = B_FALSE;
3735 boolean_t xtreme_rewind = B_FALSE;
3736 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3737 nvlist_t *policy = NULL;
3738 zpool_handle_t *zhp;
3739 char *pool, *device;
3742 while ((c = getopt(argc, argv, "FnX")) != -1) {
3751 xtreme_rewind = B_TRUE;
3754 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3764 (void) fprintf(stderr, gettext("missing pool name\n"));
3769 (void) fprintf(stderr, gettext("too many arguments\n"));
3773 if ((dryrun || xtreme_rewind) && !do_rewind) {
3774 (void) fprintf(stderr,
3775 gettext("-n or -X only meaningful with -F\n"));
3779 rewind_policy = ZPOOL_TRY_REWIND;
3781 rewind_policy = ZPOOL_DO_REWIND;
3783 rewind_policy |= ZPOOL_EXTREME_REWIND;
3785 /* In future, further rewind policy choices can be passed along here */
3786 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3787 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3791 device = argc == 2 ? argv[1] : NULL;
3793 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3794 nvlist_free(policy);
3798 if (zpool_clear(zhp, device, policy) != 0)
3803 nvlist_free(policy);
3809 * zpool reguid <pool>
3812 zpool_do_reguid(int argc, char **argv)
3816 zpool_handle_t *zhp;
3820 while ((c = getopt(argc, argv, "")) != -1) {
3823 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3832 /* get pool name and check number of arguments */
3834 (void) fprintf(stderr, gettext("missing pool name\n"));
3839 (void) fprintf(stderr, gettext("too many arguments\n"));
3844 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3847 ret = zpool_reguid(zhp);
3855 * zpool reopen <pool>
3857 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3860 zpool_do_reopen(int argc, char **argv)
3864 zpool_handle_t *zhp;
3868 while ((c = getopt(argc, argv, "")) != -1) {
3871 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3881 (void) fprintf(stderr, gettext("missing pool name\n"));
3886 (void) fprintf(stderr, gettext("too many arguments\n"));
3891 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3894 ret = zpool_reopen(zhp);
3899 typedef struct scrub_cbdata {
3906 scrub_callback(zpool_handle_t *zhp, void *data)
3908 scrub_cbdata_t *cb = data;
3912 * Ignore faulted pools.
3914 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3915 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3916 "currently unavailable\n"), zpool_get_name(zhp));
3920 err = zpool_scan(zhp, cb->cb_type);
3926 * zpool scrub [-s] <pool> ...
3928 * -s Stop. Stops any in-progress scrub.
3931 zpool_do_scrub(int argc, char **argv)
3936 cb.cb_type = POOL_SCAN_SCRUB;
3939 while ((c = getopt(argc, argv, "s")) != -1) {
3942 cb.cb_type = POOL_SCAN_NONE;
3945 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3957 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3961 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3964 typedef struct status_cbdata {
3966 boolean_t cb_allpools;
3967 boolean_t cb_verbose;
3968 boolean_t cb_explain;
3970 boolean_t cb_dedup_stats;
3974 * Print out detailed scrub status.
3977 print_scan_status(pool_scan_stat_t *ps)
3980 uint64_t elapsed, mins_left, hours_left;
3981 uint64_t pass_exam, examined, total;
3983 double fraction_done;
3984 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3986 (void) printf(gettext(" scan: "));
3988 /* If there's never been a scan, there's not much to say. */
3989 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3990 ps->pss_func >= POOL_SCAN_FUNCS) {
3991 (void) printf(gettext("none requested\n"));
3995 start = ps->pss_start_time;
3996 end = ps->pss_end_time;
3997 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3999 assert(ps->pss_func == POOL_SCAN_SCRUB ||
4000 ps->pss_func == POOL_SCAN_RESILVER);
4002 * Scan is finished or canceled.
4004 if (ps->pss_state == DSS_FINISHED) {
4005 uint64_t minutes_taken = (end - start) / 60;
4008 if (ps->pss_func == POOL_SCAN_SCRUB) {
4009 fmt = gettext("scrub repaired %s in %lluh%um with "
4010 "%llu errors on %s");
4011 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4012 fmt = gettext("resilvered %s in %lluh%um with "
4013 "%llu errors on %s");
4016 (void) printf(fmt, processed_buf,
4017 (u_longlong_t)(minutes_taken / 60),
4018 (uint_t)(minutes_taken % 60),
4019 (u_longlong_t)ps->pss_errors,
4020 ctime((time_t *)&end));
4022 } else if (ps->pss_state == DSS_CANCELED) {
4023 if (ps->pss_func == POOL_SCAN_SCRUB) {
4024 (void) printf(gettext("scrub canceled on %s"),
4026 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4027 (void) printf(gettext("resilver canceled on %s"),
4033 assert(ps->pss_state == DSS_SCANNING);
4036 * Scan is in progress.
4038 if (ps->pss_func == POOL_SCAN_SCRUB) {
4039 (void) printf(gettext("scrub in progress since %s"),
4041 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4042 (void) printf(gettext("resilver in progress since %s"),
4046 examined = ps->pss_examined ? ps->pss_examined : 1;
4047 total = ps->pss_to_examine;
4048 fraction_done = (double)examined / total;
4050 /* elapsed time for this pass */
4051 elapsed = time(NULL) - ps->pss_pass_start;
4052 elapsed = elapsed ? elapsed : 1;
4053 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4054 rate = pass_exam / elapsed;
4055 rate = rate ? rate : 1;
4056 mins_left = ((total - examined) / rate) / 60;
4057 hours_left = mins_left / 60;
4059 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4060 zfs_nicenum(total, total_buf, sizeof (total_buf));
4061 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4064 * do not print estimated time if hours_left is more than 30 days
4066 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4067 examined_buf, total_buf, rate_buf);
4068 if (hours_left < (30 * 24)) {
4069 (void) printf(gettext(", %lluh%um to go\n"),
4070 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4072 (void) printf(gettext(
4073 ", (scan is slow, no estimated time)\n"));
4076 if (ps->pss_func == POOL_SCAN_RESILVER) {
4077 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4078 processed_buf, 100 * fraction_done);
4079 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4080 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4081 processed_buf, 100 * fraction_done);
4086 print_error_log(zpool_handle_t *zhp)
4088 nvlist_t *nverrlist = NULL;
4091 size_t len = MAXPATHLEN * 2;
4093 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4094 (void) printf("errors: List of errors unavailable "
4095 "(insufficient privileges)\n");
4099 (void) printf("errors: Permanent errors have been "
4100 "detected in the following files:\n\n");
4102 pathname = safe_malloc(len);
4104 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4106 uint64_t dsobj, obj;
4108 verify(nvpair_value_nvlist(elem, &nv) == 0);
4109 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4111 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4113 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4114 (void) printf("%7s %s\n", "", pathname);
4117 nvlist_free(nverrlist);
4121 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4130 (void) printf(gettext("\tspares\n"));
4132 for (i = 0; i < nspares; i++) {
4133 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4134 print_status_config(zhp, name, spares[i],
4135 namewidth, 2, B_TRUE);
4141 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4150 (void) printf(gettext("\tcache\n"));
4152 for (i = 0; i < nl2cache; i++) {
4153 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4154 print_status_config(zhp, name, l2cache[i],
4155 namewidth, 2, B_FALSE);
4161 print_dedup_stats(nvlist_t *config)
4163 ddt_histogram_t *ddh;
4169 * If the pool was faulted then we may not have been able to
4170 * obtain the config. Otherwise, if we have anything in the dedup
4171 * table continue processing the stats.
4173 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4174 (uint64_t **)&ddo, &c) != 0)
4177 (void) printf("\n");
4178 (void) printf(gettext(" dedup: "));
4179 if (ddo->ddo_count == 0) {
4180 (void) printf(gettext("no DDT entries\n"));
4184 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4185 (u_longlong_t)ddo->ddo_count,
4186 (u_longlong_t)ddo->ddo_dspace,
4187 (u_longlong_t)ddo->ddo_mspace);
4189 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4190 (uint64_t **)&dds, &c) == 0);
4191 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4192 (uint64_t **)&ddh, &c) == 0);
4193 zpool_dump_ddt(dds, ddh);
4197 * Display a summary of pool status. Displays a summary such as:
4201 * reason: One or more devices ...
4202 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4208 * When given the '-v' option, we print out the complete config. If the '-e'
4209 * option is specified, then we print out error rate information as well.
4212 status_callback(zpool_handle_t *zhp, void *data)
4214 status_cbdata_t *cbp = data;
4215 nvlist_t *config, *nvroot;
4217 zpool_status_t reason;
4218 zpool_errata_t errata;
4223 config = zpool_get_config(zhp, NULL);
4224 reason = zpool_get_status(zhp, &msgid, &errata);
4229 * If we were given 'zpool status -x', only report those pools with
4232 if (cbp->cb_explain &&
4233 (reason == ZPOOL_STATUS_OK ||
4234 reason == ZPOOL_STATUS_VERSION_OLDER ||
4235 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4236 if (!cbp->cb_allpools) {
4237 (void) printf(gettext("pool '%s' is healthy\n"),
4238 zpool_get_name(zhp));
4240 cbp->cb_first = B_FALSE;
4246 cbp->cb_first = B_FALSE;
4248 (void) printf("\n");
4250 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4252 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4253 (uint64_t **)&vs, &c) == 0);
4254 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4256 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4257 (void) printf(gettext(" state: %s\n"), health);
4260 case ZPOOL_STATUS_MISSING_DEV_R:
4261 (void) printf(gettext("status: One or more devices could not "
4262 "be opened. Sufficient replicas exist for\n\tthe pool to "
4263 "continue functioning in a degraded state.\n"));
4264 (void) printf(gettext("action: Attach the missing device and "
4265 "online it using 'zpool online'.\n"));
4268 case ZPOOL_STATUS_MISSING_DEV_NR:
4269 (void) printf(gettext("status: One or more devices could not "
4270 "be opened. There are insufficient\n\treplicas for the "
4271 "pool to continue functioning.\n"));
4272 (void) printf(gettext("action: Attach the missing device and "
4273 "online it using 'zpool online'.\n"));
4276 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4277 (void) printf(gettext("status: One or more devices could not "
4278 "be used because the label is missing or\n\tinvalid. "
4279 "Sufficient replicas exist for the pool to continue\n\t"
4280 "functioning in a degraded state.\n"));
4281 (void) printf(gettext("action: Replace the device using "
4282 "'zpool replace'.\n"));
4285 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4286 (void) printf(gettext("status: One or more devices could not "
4287 "be used because the label is missing \n\tor invalid. "
4288 "There are insufficient replicas for the pool to "
4289 "continue\n\tfunctioning.\n"));
4290 zpool_explain_recover(zpool_get_handle(zhp),
4291 zpool_get_name(zhp), reason, config);
4294 case ZPOOL_STATUS_FAILING_DEV:
4295 (void) printf(gettext("status: One or more devices has "
4296 "experienced an unrecoverable error. An\n\tattempt was "
4297 "made to correct the error. Applications are "
4299 (void) printf(gettext("action: Determine if the device needs "
4300 "to be replaced, and clear the errors\n\tusing "
4301 "'zpool clear' or replace the device with 'zpool "
4305 case ZPOOL_STATUS_OFFLINE_DEV:
4306 (void) printf(gettext("status: One or more devices has "
4307 "been taken offline by the administrator.\n\tSufficient "
4308 "replicas exist for the pool to continue functioning in "
4309 "a\n\tdegraded state.\n"));
4310 (void) printf(gettext("action: Online the device using "
4311 "'zpool online' or replace the device with\n\t'zpool "
4315 case ZPOOL_STATUS_REMOVED_DEV:
4316 (void) printf(gettext("status: One or more devices has "
4317 "been removed by the administrator.\n\tSufficient "
4318 "replicas exist for the pool to continue functioning in "
4319 "a\n\tdegraded state.\n"));
4320 (void) printf(gettext("action: Online the device using "
4321 "'zpool online' or replace the device with\n\t'zpool "
4325 case ZPOOL_STATUS_RESILVERING:
4326 (void) printf(gettext("status: One or more devices is "
4327 "currently being resilvered. The pool will\n\tcontinue "
4328 "to function, possibly in a degraded state.\n"));
4329 (void) printf(gettext("action: Wait for the resilver to "
4333 case ZPOOL_STATUS_CORRUPT_DATA:
4334 (void) printf(gettext("status: One or more devices has "
4335 "experienced an error resulting in data\n\tcorruption. "
4336 "Applications may be affected.\n"));
4337 (void) printf(gettext("action: Restore the file in question "
4338 "if possible. Otherwise restore the\n\tentire pool from "
4342 case ZPOOL_STATUS_CORRUPT_POOL:
4343 (void) printf(gettext("status: The pool metadata is corrupted "
4344 "and the pool cannot be opened.\n"));
4345 zpool_explain_recover(zpool_get_handle(zhp),
4346 zpool_get_name(zhp), reason, config);
4349 case ZPOOL_STATUS_VERSION_OLDER:
4350 (void) printf(gettext("status: The pool is formatted using a "
4351 "legacy on-disk format. The pool can\n\tstill be used, "
4352 "but some features are unavailable.\n"));
4353 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4354 "upgrade'. Once this is done, the\n\tpool will no longer "
4355 "be accessible on software that does not support\n\t"
4356 "feature flags.\n"));
4359 case ZPOOL_STATUS_VERSION_NEWER:
4360 (void) printf(gettext("status: The pool has been upgraded to a "
4361 "newer, incompatible on-disk version.\n\tThe pool cannot "
4362 "be accessed on this system.\n"));
4363 (void) printf(gettext("action: Access the pool from a system "
4364 "running more recent software, or\n\trestore the pool from "
4368 case ZPOOL_STATUS_FEAT_DISABLED:
4369 (void) printf(gettext("status: Some supported features are not "
4370 "enabled on the pool. The pool can\n\tstill be used, but "
4371 "some features are unavailable.\n"));
4372 (void) printf(gettext("action: Enable all features using "
4373 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4374 "longer be accessible by software that does not support\n\t"
4375 "the features. See zpool-features(5) for details.\n"));
4378 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4379 (void) printf(gettext("status: The pool cannot be accessed on "
4380 "this system because it uses the\n\tfollowing feature(s) "
4381 "not supported on this system:\n"));
4382 zpool_print_unsup_feat(config);
4383 (void) printf("\n");
4384 (void) printf(gettext("action: Access the pool from a system "
4385 "that supports the required feature(s),\n\tor restore the "
4386 "pool from backup.\n"));
4389 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4390 (void) printf(gettext("status: The pool can only be accessed "
4391 "in read-only mode on this system. It\n\tcannot be "
4392 "accessed in read-write mode because it uses the "
4393 "following\n\tfeature(s) not supported on this system:\n"));
4394 zpool_print_unsup_feat(config);
4395 (void) printf("\n");
4396 (void) printf(gettext("action: The pool cannot be accessed in "
4397 "read-write mode. Import the pool with\n"
4398 "\t\"-o readonly=on\", access the pool from a system that "
4399 "supports the\n\trequired feature(s), or restore the "
4400 "pool from backup.\n"));
4403 case ZPOOL_STATUS_FAULTED_DEV_R:
4404 (void) printf(gettext("status: One or more devices are "
4405 "faulted in response to persistent errors.\n\tSufficient "
4406 "replicas exist for the pool to continue functioning "
4407 "in a\n\tdegraded state.\n"));
4408 (void) printf(gettext("action: Replace the faulted device, "
4409 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4412 case ZPOOL_STATUS_FAULTED_DEV_NR:
4413 (void) printf(gettext("status: One or more devices are "
4414 "faulted in response to persistent errors. There are "
4415 "insufficient replicas for the pool to\n\tcontinue "
4417 (void) printf(gettext("action: Destroy and re-create the pool "
4418 "from a backup source. Manually marking the device\n"
4419 "\trepaired using 'zpool clear' may allow some data "
4420 "to be recovered.\n"));
4423 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4424 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4425 (void) printf(gettext("status: One or more devices are "
4426 "faulted in response to IO failures.\n"));
4427 (void) printf(gettext("action: Make sure the affected devices "
4428 "are connected, then run 'zpool clear'.\n"));
4431 case ZPOOL_STATUS_BAD_LOG:
4432 (void) printf(gettext("status: An intent log record "
4433 "could not be read.\n"
4434 "\tWaiting for adminstrator intervention to fix the "
4435 "faulted pool.\n"));
4436 (void) printf(gettext("action: Either restore the affected "
4437 "device(s) and run 'zpool online',\n"
4438 "\tor ignore the intent log records by running "
4439 "'zpool clear'.\n"));
4442 case ZPOOL_STATUS_HOSTID_MISMATCH:
4443 (void) printf(gettext("status: Mismatch between pool hostid "
4444 "and system hostid on imported pool.\n\tThis pool was "
4445 "previously imported into a system with a different "
4446 "hostid,\n\tand then was verbatim imported into this "
4448 (void) printf(gettext("action: Export this pool on all systems "
4449 "on which it is imported.\n"
4450 "\tThen import it to correct the mismatch.\n"));
4453 case ZPOOL_STATUS_ERRATA:
4454 (void) printf(gettext("status: Errata #%d detected.\n"),
4458 case ZPOOL_ERRATA_NONE:
4461 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
4462 (void) printf(gettext("action: To correct the issue "
4463 "run 'zpool scrub'.\n"));
4468 * All errata which allow the pool to be imported
4469 * must contain an action message.
4477 * The remaining errors can't actually be generated, yet.
4479 assert(reason == ZPOOL_STATUS_OK);
4483 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4486 if (config != NULL) {
4489 nvlist_t **spares, **l2cache;
4490 uint_t nspares, nl2cache;
4491 pool_scan_stat_t *ps = NULL;
4493 (void) nvlist_lookup_uint64_array(nvroot,
4494 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4495 print_scan_status(ps);
4497 namewidth = max_width(zhp, nvroot, 0, 0);
4501 (void) printf(gettext("config:\n\n"));
4502 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4503 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4504 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4505 namewidth, 0, B_FALSE);
4507 if (num_logs(nvroot) > 0)
4508 print_logs(zhp, nvroot, namewidth, B_TRUE);
4509 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4510 &l2cache, &nl2cache) == 0)
4511 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4513 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4514 &spares, &nspares) == 0)
4515 print_spares(zhp, spares, nspares, namewidth);
4517 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4519 nvlist_t *nverrlist = NULL;
4522 * If the approximate error count is small, get a
4523 * precise count by fetching the entire log and
4524 * uniquifying the results.
4526 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4527 zpool_get_errlog(zhp, &nverrlist) == 0) {
4532 while ((elem = nvlist_next_nvpair(nverrlist,
4537 nvlist_free(nverrlist);
4539 (void) printf("\n");
4542 (void) printf(gettext("errors: No known data "
4544 else if (!cbp->cb_verbose)
4545 (void) printf(gettext("errors: %llu data "
4546 "errors, use '-v' for a list\n"),
4547 (u_longlong_t)nerr);
4549 print_error_log(zhp);
4552 if (cbp->cb_dedup_stats)
4553 print_dedup_stats(config);
4555 (void) printf(gettext("config: The configuration cannot be "
4563 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4565 * -v Display complete error logs
4566 * -x Display only pools with potential problems
4567 * -D Display dedup status (undocumented)
4568 * -T Display a timestamp in date(1) or Unix format
4570 * Describes the health status of all pools or some subset.
4573 zpool_do_status(int argc, char **argv)
4577 unsigned long interval = 0, count = 0;
4578 status_cbdata_t cb = { 0 };
4581 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4584 cb.cb_verbose = B_TRUE;
4587 cb.cb_explain = B_TRUE;
4590 cb.cb_dedup_stats = B_TRUE;
4593 get_timestamp_arg(*optarg);
4596 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4605 get_interval_count(&argc, argv, &interval, &count);
4608 cb.cb_allpools = B_TRUE;
4610 cb.cb_first = B_TRUE;
4613 if (timestamp_fmt != NODATE)
4614 print_timestamp(timestamp_fmt);
4616 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4617 status_callback, &cb);
4619 if (argc == 0 && cb.cb_count == 0)
4620 (void) fprintf(stderr, gettext("no pools available\n"));
4621 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4622 (void) printf(gettext("all pools are healthy\n"));
4630 if (count != 0 && --count == 0)
4633 (void) sleep(interval);
4639 typedef struct upgrade_cbdata {
4642 uint64_t cb_version;
4647 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
4649 int zfs_version = (int) zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
4650 int *count = (int *)unsupp_fs;
4652 if (zfs_version > ZPL_VERSION) {
4653 (void) printf(gettext("%s (v%d) is not supported by this "
4654 "implementation of ZFS.\n"),
4655 zfs_get_name(zhp), zfs_version);
4659 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
4667 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4671 uint64_t oldversion;
4674 config = zpool_get_config(zhp, NULL);
4675 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4678 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4679 assert(oldversion < version);
4681 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
4686 (void) printf(gettext("Upgrade not performed due to %d "
4687 "unsupported filesystems (max v%d).\n"),
4688 unsupp_fs, (int) ZPL_VERSION);
4692 ret = zpool_upgrade(zhp, version);
4696 if (version >= SPA_VERSION_FEATURES) {
4697 (void) printf(gettext("Successfully upgraded "
4698 "'%s' from version %llu to feature flags.\n"),
4699 zpool_get_name(zhp), (u_longlong_t) oldversion);
4701 (void) printf(gettext("Successfully upgraded "
4702 "'%s' from version %llu to version %llu.\n"),
4703 zpool_get_name(zhp), (u_longlong_t) oldversion,
4704 (u_longlong_t) version);
4711 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4714 boolean_t firstff = B_TRUE;
4715 nvlist_t *enabled = zpool_get_features(zhp);
4718 for (i = 0; i < SPA_FEATURES; i++) {
4719 const char *fname = spa_feature_table[i].fi_uname;
4720 const char *fguid = spa_feature_table[i].fi_guid;
4721 if (!nvlist_exists(enabled, fguid)) {
4723 verify(-1 != asprintf(&propname, "feature@%s", fname));
4724 ret = zpool_set_prop(zhp, propname,
4725 ZFS_FEATURE_ENABLED);
4733 (void) printf(gettext("Enabled the "
4734 "following features on '%s':\n"),
4735 zpool_get_name(zhp));
4738 (void) printf(gettext(" %s\n"), fname);
4749 upgrade_cb(zpool_handle_t *zhp, void *arg)
4751 upgrade_cbdata_t *cbp = arg;
4754 boolean_t printnl = B_FALSE;
4757 config = zpool_get_config(zhp, NULL);
4758 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4761 assert(SPA_VERSION_IS_SUPPORTED(version));
4763 if (version < cbp->cb_version) {
4764 cbp->cb_first = B_FALSE;
4765 ret = upgrade_version(zhp, cbp->cb_version);
4771 * If they did "zpool upgrade -a", then we could
4772 * be doing ioctls to different pools. We need
4773 * to log this history once to each pool, and bypass
4774 * the normal history logging that happens in main().
4776 (void) zpool_log_history(g_zfs, history_str);
4777 log_history = B_FALSE;
4780 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4782 ret = upgrade_enable_all(zhp, &count);
4787 cbp->cb_first = B_FALSE;
4793 (void) printf(gettext("\n"));
4800 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4802 upgrade_cbdata_t *cbp = arg;
4806 config = zpool_get_config(zhp, NULL);
4807 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4810 assert(SPA_VERSION_IS_SUPPORTED(version));
4812 if (version < SPA_VERSION_FEATURES) {
4813 if (cbp->cb_first) {
4814 (void) printf(gettext("The following pools are "
4815 "formatted with legacy version numbers and can\n"
4816 "be upgraded to use feature flags. After "
4817 "being upgraded, these pools\nwill no "
4818 "longer be accessible by software that does not "
4819 "support feature\nflags.\n\n"));
4820 (void) printf(gettext("VER POOL\n"));
4821 (void) printf(gettext("--- ------------\n"));
4822 cbp->cb_first = B_FALSE;
4825 (void) printf("%2llu %s\n", (u_longlong_t)version,
4826 zpool_get_name(zhp));
4833 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4835 upgrade_cbdata_t *cbp = arg;
4839 config = zpool_get_config(zhp, NULL);
4840 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4843 if (version >= SPA_VERSION_FEATURES) {
4845 boolean_t poolfirst = B_TRUE;
4846 nvlist_t *enabled = zpool_get_features(zhp);
4848 for (i = 0; i < SPA_FEATURES; i++) {
4849 const char *fguid = spa_feature_table[i].fi_guid;
4850 const char *fname = spa_feature_table[i].fi_uname;
4851 if (!nvlist_exists(enabled, fguid)) {
4852 if (cbp->cb_first) {
4853 (void) printf(gettext("\nSome "
4854 "supported features are not "
4855 "enabled on the following pools. "
4856 "Once a\nfeature is enabled the "
4857 "pool may become incompatible with "
4858 "software\nthat does not support "
4860 "zpool-features(5) for "
4862 (void) printf(gettext("POOL "
4864 (void) printf(gettext("------"
4866 cbp->cb_first = B_FALSE;
4870 (void) printf(gettext("%s\n"),
4871 zpool_get_name(zhp));
4872 poolfirst = B_FALSE;
4875 (void) printf(gettext(" %s\n"), fname);
4878 * If they did "zpool upgrade -a", then we could
4879 * be doing ioctls to different pools. We need
4880 * to log this history once to each pool, and bypass
4881 * the normal history logging that happens in main().
4883 (void) zpool_log_history(g_zfs, history_str);
4884 log_history = B_FALSE;
4893 upgrade_one(zpool_handle_t *zhp, void *data)
4895 boolean_t printnl = B_FALSE;
4896 upgrade_cbdata_t *cbp = data;
4897 uint64_t cur_version;
4900 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4901 (void) printf(gettext("'log' is now a reserved word\n"
4902 "Pool 'log' must be renamed using export and import"
4907 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4908 if (cur_version > cbp->cb_version) {
4909 (void) printf(gettext("Pool '%s' is already formatted "
4910 "using more current version '%llu'.\n\n"),
4911 zpool_get_name(zhp), (u_longlong_t) cur_version);
4915 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4916 (void) printf(gettext("Pool '%s' is already formatted "
4917 "using version %llu.\n\n"), zpool_get_name(zhp),
4918 (u_longlong_t) cbp->cb_version);
4922 if (cur_version != cbp->cb_version) {
4924 ret = upgrade_version(zhp, cbp->cb_version);
4929 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4931 ret = upgrade_enable_all(zhp, &count);
4937 } else if (cur_version == SPA_VERSION) {
4938 (void) printf(gettext("Pool '%s' already has all "
4939 "supported features enabled.\n"),
4940 zpool_get_name(zhp));
4945 (void) printf(gettext("\n"));
4954 * zpool upgrade [-V version] <-a | pool ...>
4956 * With no arguments, display downrev'd ZFS pool available for upgrade.
4957 * Individual pools can be upgraded by specifying the pool, and '-a' will
4958 * upgrade all pools.
4961 zpool_do_upgrade(int argc, char **argv)
4964 upgrade_cbdata_t cb = { 0 };
4966 boolean_t showversions = B_FALSE;
4967 boolean_t upgradeall = B_FALSE;
4972 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4975 upgradeall = B_TRUE;
4978 showversions = B_TRUE;
4981 cb.cb_version = strtoll(optarg, &end, 10);
4983 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4984 (void) fprintf(stderr,
4985 gettext("invalid version '%s'\n"), optarg);
4990 (void) fprintf(stderr, gettext("missing argument for "
4991 "'%c' option\n"), optopt);
4995 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5006 if (cb.cb_version == 0) {
5007 cb.cb_version = SPA_VERSION;
5008 } else if (!upgradeall && argc == 0) {
5009 (void) fprintf(stderr, gettext("-V option is "
5010 "incompatible with other arguments\n"));
5015 if (upgradeall || argc != 0) {
5016 (void) fprintf(stderr, gettext("-v option is "
5017 "incompatible with other arguments\n"));
5020 } else if (upgradeall) {
5022 (void) fprintf(stderr, gettext("-a option should not "
5023 "be used along with a pool name\n"));
5028 (void) printf(gettext("This system supports ZFS pool feature "
5033 (void) printf(gettext("The following features are "
5035 (void) printf(gettext("FEAT DESCRIPTION\n"));
5036 (void) printf("----------------------------------------------"
5037 "---------------\n");
5038 for (i = 0; i < SPA_FEATURES; i++) {
5039 zfeature_info_t *fi = &spa_feature_table[i];
5040 const char *ro = fi->fi_can_readonly ?
5041 " (read-only compatible)" : "";
5043 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5044 (void) printf(" %s\n", fi->fi_desc);
5046 (void) printf("\n");
5048 (void) printf(gettext("The following legacy versions are also "
5050 (void) printf(gettext("VER DESCRIPTION\n"));
5051 (void) printf("--- -----------------------------------------"
5052 "---------------\n");
5053 (void) printf(gettext(" 1 Initial ZFS version\n"));
5054 (void) printf(gettext(" 2 Ditto blocks "
5055 "(replicated metadata)\n"));
5056 (void) printf(gettext(" 3 Hot spares and double parity "
5058 (void) printf(gettext(" 4 zpool history\n"));
5059 (void) printf(gettext(" 5 Compression using the gzip "
5061 (void) printf(gettext(" 6 bootfs pool property\n"));
5062 (void) printf(gettext(" 7 Separate intent log devices\n"));
5063 (void) printf(gettext(" 8 Delegated administration\n"));
5064 (void) printf(gettext(" 9 refquota and refreservation "
5066 (void) printf(gettext(" 10 Cache devices\n"));
5067 (void) printf(gettext(" 11 Improved scrub performance\n"));
5068 (void) printf(gettext(" 12 Snapshot properties\n"));
5069 (void) printf(gettext(" 13 snapused property\n"));
5070 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5071 (void) printf(gettext(" 15 user/group space accounting\n"));
5072 (void) printf(gettext(" 16 stmf property support\n"));
5073 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5074 (void) printf(gettext(" 18 Snapshot user holds\n"));
5075 (void) printf(gettext(" 19 Log device removal\n"));
5076 (void) printf(gettext(" 20 Compression using zle "
5077 "(zero-length encoding)\n"));
5078 (void) printf(gettext(" 21 Deduplication\n"));
5079 (void) printf(gettext(" 22 Received properties\n"));
5080 (void) printf(gettext(" 23 Slim ZIL\n"));
5081 (void) printf(gettext(" 24 System attributes\n"));
5082 (void) printf(gettext(" 25 Improved scrub stats\n"));
5083 (void) printf(gettext(" 26 Improved snapshot deletion "
5085 (void) printf(gettext(" 27 Improved snapshot creation "
5087 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5088 (void) printf(gettext("\nFor more information on a particular "
5089 "version, including supported releases,\n"));
5090 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5091 } else if (argc == 0 && upgradeall) {
5092 cb.cb_first = B_TRUE;
5093 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5094 if (ret == 0 && cb.cb_first) {
5095 if (cb.cb_version == SPA_VERSION) {
5096 (void) printf(gettext("All pools are already "
5097 "formatted using feature flags.\n\n"));
5098 (void) printf(gettext("Every feature flags "
5099 "pool already has all supported features "
5102 (void) printf(gettext("All pools are already "
5103 "formatted with version %llu or higher.\n"),
5104 (u_longlong_t) cb.cb_version);
5107 } else if (argc == 0) {
5108 cb.cb_first = B_TRUE;
5109 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5113 (void) printf(gettext("All pools are formatted "
5114 "using feature flags.\n\n"));
5116 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5117 "for a list of available legacy versions.\n"));
5120 cb.cb_first = B_TRUE;
5121 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5125 (void) printf(gettext("Every feature flags pool has "
5126 "all supported features enabled.\n"));
5128 (void) printf(gettext("\n"));
5131 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5138 typedef struct hist_cbdata {
5145 * Print out the command history for a specific pool.
5148 get_history_one(zpool_handle_t *zhp, void *data)
5154 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5156 cb->first = B_FALSE;
5158 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5160 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5163 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5164 &records, &numrecords) == 0);
5165 for (i = 0; i < numrecords; i++) {
5166 nvlist_t *rec = records[i];
5169 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5173 tsec = fnvlist_lookup_uint64(records[i],
5175 (void) localtime_r(&tsec, &t);
5176 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5179 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5180 (void) printf("%s %s", tbuf,
5181 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5182 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5184 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5187 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5188 (void) printf("%s unrecognized record:\n",
5190 dump_nvlist(rec, 4);
5193 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5194 zfs_history_event_names[ievent],
5195 (longlong_t) fnvlist_lookup_uint64(
5196 rec, ZPOOL_HIST_TXG),
5197 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5198 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5201 (void) printf("%s [txg:%lld] %s", tbuf,
5202 (longlong_t) fnvlist_lookup_uint64(
5203 rec, ZPOOL_HIST_TXG),
5204 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5205 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5206 (void) printf(" %s (%llu)",
5207 fnvlist_lookup_string(rec,
5209 (u_longlong_t)fnvlist_lookup_uint64(rec,
5212 (void) printf(" %s", fnvlist_lookup_string(rec,
5213 ZPOOL_HIST_INT_STR));
5214 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5217 (void) printf("%s ioctl %s\n", tbuf,
5218 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5219 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5220 (void) printf(" input:\n");
5221 dump_nvlist(fnvlist_lookup_nvlist(rec,
5222 ZPOOL_HIST_INPUT_NVL), 8);
5224 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5225 (void) printf(" output:\n");
5226 dump_nvlist(fnvlist_lookup_nvlist(rec,
5227 ZPOOL_HIST_OUTPUT_NVL), 8);
5232 (void) printf("%s unrecognized record:\n", tbuf);
5233 dump_nvlist(rec, 4);
5237 (void) printf("\n");
5240 (void) printf(" [");
5241 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5242 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5243 struct passwd *pwd = getpwuid(who);
5244 (void) printf("user %d ", (int)who);
5246 (void) printf("(%s) ", pwd->pw_name);
5248 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5249 (void) printf("on %s",
5250 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5252 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5253 (void) printf(":%s",
5254 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5258 (void) printf("\n");
5260 (void) printf("\n");
5267 * zpool history <pool>
5269 * Displays the history of commands that modified pools.
5272 zpool_do_history(int argc, char **argv)
5274 hist_cbdata_t cbdata = { 0 };
5278 cbdata.first = B_TRUE;
5280 while ((c = getopt(argc, argv, "li")) != -1) {
5283 cbdata.longfmt = B_TRUE;
5286 cbdata.internal = B_TRUE;
5289 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5297 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5300 if (argc == 0 && cbdata.first == B_TRUE) {
5301 (void) fprintf(stderr, gettext("no pools available\n"));
5308 typedef struct ev_opts {
5316 zpool_do_events_short(nvlist_t *nvl)
5318 char ctime_str[26], str[32], *ptr;
5322 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5323 memset(str, ' ', 32);
5324 (void) ctime_r((const time_t *)&tv[0], ctime_str);
5325 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
5326 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
5327 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
5328 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
5329 (void) printf(gettext("%s "), str);
5331 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5332 (void) printf(gettext("%s\n"), ptr);
5336 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5340 for (nvp = nvlist_next_nvpair(nvl, NULL);
5341 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5343 data_type_t type = nvpair_type(nvp);
5344 const char *name = nvpair_name(nvp);
5354 printf(gettext("%*s%s = "), depth, "", name);
5357 case DATA_TYPE_BOOLEAN:
5358 printf(gettext("%s"), "1");
5361 case DATA_TYPE_BOOLEAN_VALUE:
5362 (void) nvpair_value_boolean_value(nvp, &b);
5363 printf(gettext("%s"), b ? "1" : "0");
5366 case DATA_TYPE_BYTE:
5367 (void) nvpair_value_byte(nvp, &i8);
5368 printf(gettext("0x%x"), i8);
5371 case DATA_TYPE_INT8:
5372 (void) nvpair_value_int8(nvp, (void *)&i8);
5373 printf(gettext("0x%x"), i8);
5376 case DATA_TYPE_UINT8:
5377 (void) nvpair_value_uint8(nvp, &i8);
5378 printf(gettext("0x%x"), i8);
5381 case DATA_TYPE_INT16:
5382 (void) nvpair_value_int16(nvp, (void *)&i16);
5383 printf(gettext("0x%x"), i16);
5386 case DATA_TYPE_UINT16:
5387 (void) nvpair_value_uint16(nvp, &i16);
5388 printf(gettext("0x%x"), i16);
5391 case DATA_TYPE_INT32:
5392 (void) nvpair_value_int32(nvp, (void *)&i32);
5393 printf(gettext("0x%x"), i32);
5396 case DATA_TYPE_UINT32:
5397 (void) nvpair_value_uint32(nvp, &i32);
5398 printf(gettext("0x%x"), i32);
5401 case DATA_TYPE_INT64:
5402 (void) nvpair_value_int64(nvp, (void *)&i64);
5403 printf(gettext("0x%llx"), (u_longlong_t)i64);
5406 case DATA_TYPE_UINT64:
5407 (void) nvpair_value_uint64(nvp, &i64);
5408 printf(gettext("0x%llx"), (u_longlong_t)i64);
5411 case DATA_TYPE_HRTIME:
5412 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5413 printf(gettext("0x%llx"), (u_longlong_t)i64);
5416 case DATA_TYPE_STRING:
5417 (void) nvpair_value_string(nvp, &str);
5418 printf(gettext("\"%s\""), str ? str : "<NULL>");
5421 case DATA_TYPE_NVLIST:
5422 printf(gettext("(embedded nvlist)\n"));
5423 (void) nvpair_value_nvlist(nvp, &cnv);
5424 zpool_do_events_nvprint(cnv, depth + 8);
5425 printf(gettext("%*s(end %s)"), depth, "", name);
5428 case DATA_TYPE_NVLIST_ARRAY: {
5432 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5433 printf(gettext("(%d embedded nvlists)\n"), nelem);
5434 for (i = 0; i < nelem; i++) {
5435 printf(gettext("%*s%s[%d] = %s\n"),
5436 depth, "", name, i, "(embedded nvlist)");
5437 zpool_do_events_nvprint(val[i], depth + 8);
5438 printf(gettext("%*s(end %s[%i])\n"),
5439 depth, "", name, i);
5441 printf(gettext("%*s(end %s)\n"), depth, "", name);
5445 case DATA_TYPE_INT8_ARRAY: {
5449 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5450 for (i = 0; i < nelem; i++)
5451 printf(gettext("0x%x "), val[i]);
5456 case DATA_TYPE_UINT8_ARRAY: {
5460 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5461 for (i = 0; i < nelem; i++)
5462 printf(gettext("0x%x "), val[i]);
5467 case DATA_TYPE_INT16_ARRAY: {
5471 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5472 for (i = 0; i < nelem; i++)
5473 printf(gettext("0x%x "), val[i]);
5478 case DATA_TYPE_UINT16_ARRAY: {
5482 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5483 for (i = 0; i < nelem; i++)
5484 printf(gettext("0x%x "), val[i]);
5489 case DATA_TYPE_INT32_ARRAY: {
5493 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5494 for (i = 0; i < nelem; i++)
5495 printf(gettext("0x%x "), val[i]);
5500 case DATA_TYPE_UINT32_ARRAY: {
5504 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5505 for (i = 0; i < nelem; i++)
5506 printf(gettext("0x%x "), val[i]);
5511 case DATA_TYPE_INT64_ARRAY: {
5515 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5516 for (i = 0; i < nelem; i++)
5517 printf(gettext("0x%llx "),
5518 (u_longlong_t)val[i]);
5523 case DATA_TYPE_UINT64_ARRAY: {
5527 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5528 for (i = 0; i < nelem; i++)
5529 printf(gettext("0x%llx "),
5530 (u_longlong_t)val[i]);
5535 case DATA_TYPE_STRING_ARRAY: {
5539 (void) nvpair_value_string_array(nvp, &str, &nelem);
5540 for (i = 0; i < nelem; i++)
5541 printf(gettext("\"%s\" "),
5542 str[i] ? str[i] : "<NULL>");
5547 case DATA_TYPE_BOOLEAN_ARRAY:
5548 case DATA_TYPE_BYTE_ARRAY:
5549 case DATA_TYPE_DOUBLE:
5550 case DATA_TYPE_UNKNOWN:
5551 printf(gettext("<unknown>"));
5555 printf(gettext("\n"));
5560 zpool_do_events_next(ev_opts_t *opts)
5563 int zevent_fd, ret, dropped;
5565 zevent_fd = open(ZFS_DEV, O_RDWR);
5566 VERIFY(zevent_fd >= 0);
5568 if (!opts->scripted)
5569 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5572 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5573 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
5574 if (ret || nvl == NULL)
5578 (void) printf(gettext("dropped %d events\n"), dropped);
5580 zpool_do_events_short(nvl);
5582 if (opts->verbose) {
5583 zpool_do_events_nvprint(nvl, 8);
5584 printf(gettext("\n"));
5586 (void) fflush(stdout);
5591 VERIFY(0 == close(zevent_fd));
5597 zpool_do_events_clear(ev_opts_t *opts)
5601 ret = zpool_events_clear(g_zfs, &count);
5603 (void) printf(gettext("cleared %d events\n"), count);
5609 * zpool events [-vfc]
5611 * Displays events logs by ZFS.
5614 zpool_do_events(int argc, char **argv)
5616 ev_opts_t opts = { 0 };
5621 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5636 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5645 ret = zpool_do_events_clear(&opts);
5647 ret = zpool_do_events_next(&opts);
5653 get_callback(zpool_handle_t *zhp, void *data)
5655 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5656 char value[MAXNAMELEN];
5657 zprop_source_t srctype;
5660 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5663 * Skip the special fake placeholder. This will also skip
5664 * over the name property when 'all' is specified.
5666 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5667 pl == cbp->cb_proplist)
5670 if (pl->pl_prop == ZPROP_INVAL &&
5671 (zpool_prop_feature(pl->pl_user_prop) ||
5672 zpool_prop_unsupported(pl->pl_user_prop))) {
5673 srctype = ZPROP_SRC_LOCAL;
5675 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5676 value, sizeof (value)) == 0) {
5677 zprop_print_one_property(zpool_get_name(zhp),
5678 cbp, pl->pl_user_prop, value, srctype,
5682 if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5683 sizeof (value), &srctype, cbp->cb_literal) != 0)
5686 zprop_print_one_property(zpool_get_name(zhp), cbp,
5687 zpool_prop_to_name(pl->pl_prop), value, srctype,
5695 zpool_do_get(int argc, char **argv)
5697 zprop_get_cbdata_t cb = { 0 };
5698 zprop_list_t fake_name = { 0 };
5702 while ((c = getopt(argc, argv, "pH")) != -1) {
5705 cb.cb_literal = B_TRUE;
5709 cb.cb_scripted = B_TRUE;
5713 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5723 (void) fprintf(stderr, gettext("missing property "
5728 cb.cb_first = B_TRUE;
5729 cb.cb_sources = ZPROP_SRC_ALL;
5730 cb.cb_columns[0] = GET_COL_NAME;
5731 cb.cb_columns[1] = GET_COL_PROPERTY;
5732 cb.cb_columns[2] = GET_COL_VALUE;
5733 cb.cb_columns[3] = GET_COL_SOURCE;
5734 cb.cb_type = ZFS_TYPE_POOL;
5736 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5742 if (cb.cb_proplist != NULL) {
5743 fake_name.pl_prop = ZPOOL_PROP_NAME;
5744 fake_name.pl_width = strlen(gettext("NAME"));
5745 fake_name.pl_next = cb.cb_proplist;
5746 cb.cb_proplist = &fake_name;
5749 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5752 if (cb.cb_proplist == &fake_name)
5753 zprop_free_list(fake_name.pl_next);
5755 zprop_free_list(cb.cb_proplist);
5760 typedef struct set_cbdata {
5763 boolean_t cb_any_successful;
5767 set_callback(zpool_handle_t *zhp, void *data)
5770 set_cbdata_t *cb = (set_cbdata_t *)data;
5772 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5775 cb->cb_any_successful = B_TRUE;
5781 zpool_do_set(int argc, char **argv)
5783 set_cbdata_t cb = { 0 };
5786 if (argc > 1 && argv[1][0] == '-') {
5787 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5793 (void) fprintf(stderr, gettext("missing property=value "
5799 (void) fprintf(stderr, gettext("missing pool name\n"));
5804 (void) fprintf(stderr, gettext("too many pool names\n"));
5808 cb.cb_propname = argv[1];
5809 cb.cb_value = strchr(cb.cb_propname, '=');
5810 if (cb.cb_value == NULL) {
5811 (void) fprintf(stderr, gettext("missing value in "
5812 "property=value argument\n"));
5816 *(cb.cb_value) = '\0';
5819 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5826 find_command_idx(char *command, int *idx)
5830 for (i = 0; i < NCOMMAND; i++) {
5831 if (command_table[i].name == NULL)
5834 if (strcmp(command, command_table[i].name) == 0) {
5843 main(int argc, char **argv)
5849 (void) setlocale(LC_ALL, "");
5850 (void) textdomain(TEXT_DOMAIN);
5855 * Make sure the user has specified some command.
5858 (void) fprintf(stderr, gettext("missing command\n"));
5867 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
5870 if ((g_zfs = libzfs_init()) == NULL)
5873 libzfs_print_on_error(g_zfs, B_TRUE);
5875 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5878 * Run the appropriate command.
5880 if (find_command_idx(cmdname, &i) == 0) {
5881 current_command = &command_table[i];
5882 ret = command_table[i].func(argc - 1, argv + 1);
5883 } else if (strchr(cmdname, '=')) {
5884 verify(find_command_idx("set", &i) == 0);
5885 current_command = &command_table[i];
5886 ret = command_table[i].func(argc, argv);
5887 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5889 * 'freeze' is a vile debugging abomination, so we treat
5893 int fd = open(ZFS_DEV, O_RDWR);
5894 (void) strcpy((void *)buf, argv[2]);
5895 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5897 (void) fprintf(stderr, gettext("unrecognized "
5898 "command '%s'\n"), cmdname);
5903 if (ret == 0 && log_history)
5904 (void) zpool_log_history(g_zfs, history_str);
5909 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5910 * for the purposes of running ::findleaks.
5912 if (getenv("ZFS_ABORT") != NULL) {
5913 (void) printf("dumping core by request\n");