4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
50 #include <sys/fs/zfs.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 #include "statcommon.h"
61 static int zpool_do_create(int, char **);
62 static int zpool_do_destroy(int, char **);
64 static int zpool_do_add(int, char **);
65 static int zpool_do_remove(int, char **);
66 static int zpool_do_labelclear(int, char **);
68 static int zpool_do_list(int, char **);
69 static int zpool_do_iostat(int, char **);
70 static int zpool_do_status(int, char **);
72 static int zpool_do_online(int, char **);
73 static int zpool_do_offline(int, char **);
74 static int zpool_do_clear(int, char **);
75 static int zpool_do_reopen(int, char **);
77 static int zpool_do_reguid(int, char **);
79 static int zpool_do_attach(int, char **);
80 static int zpool_do_detach(int, char **);
81 static int zpool_do_replace(int, char **);
82 static int zpool_do_split(int, char **);
84 static int zpool_do_scrub(int, char **);
86 static int zpool_do_import(int, char **);
87 static int zpool_do_export(int, char **);
89 static int zpool_do_upgrade(int, char **);
91 static int zpool_do_history(int, char **);
93 static int zpool_do_get(int, char **);
94 static int zpool_do_set(int, char **);
97 * These libumem hooks provide a reasonable set of defaults for the allocator's
98 * debugging facilities.
103 _umem_debug_init(void)
105 return ("default,verbose"); /* $UMEM_DEBUG setting */
109 _umem_logging_init(void)
111 return ("fail,contents"); /* $UMEM_LOGGING setting */
143 typedef struct zpool_command {
145 int (*func)(int, char **);
150 * Master command table. Each ZFS command has a name, associated function, and
151 * usage message. The usage messages need to be internationalized, so we have
152 * to have a function to return the usage message based on a command index.
154 * These commands are organized according to how they are displayed in the usage
155 * message. An empty command (one with a NULL name) indicates an empty line in
156 * the generic usage message.
158 static zpool_command_t command_table[] = {
159 { "create", zpool_do_create, HELP_CREATE },
160 { "destroy", zpool_do_destroy, HELP_DESTROY },
162 { "add", zpool_do_add, HELP_ADD },
163 { "remove", zpool_do_remove, HELP_REMOVE },
165 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
167 { "list", zpool_do_list, HELP_LIST },
168 { "iostat", zpool_do_iostat, HELP_IOSTAT },
169 { "status", zpool_do_status, HELP_STATUS },
171 { "online", zpool_do_online, HELP_ONLINE },
172 { "offline", zpool_do_offline, HELP_OFFLINE },
173 { "clear", zpool_do_clear, HELP_CLEAR },
174 { "reopen", zpool_do_reopen, HELP_REOPEN },
176 { "attach", zpool_do_attach, HELP_ATTACH },
177 { "detach", zpool_do_detach, HELP_DETACH },
178 { "replace", zpool_do_replace, HELP_REPLACE },
179 { "split", zpool_do_split, HELP_SPLIT },
181 { "scrub", zpool_do_scrub, HELP_SCRUB },
183 { "import", zpool_do_import, HELP_IMPORT },
184 { "export", zpool_do_export, HELP_EXPORT },
185 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
186 { "reguid", zpool_do_reguid, HELP_REGUID },
188 { "history", zpool_do_history, HELP_HISTORY },
189 { "get", zpool_do_get, HELP_GET },
190 { "set", zpool_do_set, HELP_SET },
193 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
195 static zpool_command_t *current_command;
196 static char history_str[HIS_MAX_RECORD_LEN];
197 static boolean_t log_history = B_TRUE;
198 static uint_t timestamp_fmt = NODATE;
201 get_usage(zpool_help_t idx) {
204 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
206 return (gettext("\tattach [-f] <pool> <device> "
209 return (gettext("\tclear [-nF] <pool> [device]\n"));
211 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
212 "\t [-O file-system-property=value] ... \n"
213 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
215 return (gettext("\tdestroy [-f] <pool>\n"));
217 return (gettext("\tdetach <pool> <device>\n"));
219 return (gettext("\texport [-f] <pool> ...\n"));
221 return (gettext("\thistory [-il] [<pool>] ...\n"));
223 return (gettext("\timport [-d dir] [-D]\n"
224 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
225 "\timport [-o mntopts] [-o property=value] ... \n"
226 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
227 "[-R root] [-F [-n]] -a\n"
228 "\timport [-o mntopts] [-o property=value] ... \n"
229 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
230 "[-R root] [-F [-n]]\n"
231 "\t <pool | id> [newpool]\n"));
233 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
235 case HELP_LABELCLEAR:
236 return (gettext("\tlabelclear [-f] <vdev>\n"));
238 return (gettext("\tlist [-Hv] [-o property[,...]] "
239 "[-T d|u] [pool] ... [interval [count]]\n"));
241 return (gettext("\toffline [-t] <pool> <device> ...\n"));
243 return (gettext("\tonline [-e] <pool> <device> ...\n"));
245 return (gettext("\treplace [-f] <pool> <device> "
248 return (gettext("\tremove <pool> <device> ...\n"));
250 return (""); /* Undocumented command */
252 return (gettext("\tscrub [-s] <pool> ...\n"));
254 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
257 return (gettext("\tupgrade [-v]\n"
258 "\tupgrade [-V version] <-a | pool ...>\n"));
260 return (gettext("\tget <\"all\" | property[,...]> "
263 return (gettext("\tset <property=value> <pool> \n"));
265 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
266 "\t [-o property=value] <pool> <newpool> "
267 "[<device> ...]\n"));
269 return (gettext("\treguid <pool>\n"));
278 * Callback routine that will print out a pool property value.
281 print_prop_cb(int prop, void *cb)
285 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
287 if (zpool_prop_readonly(prop))
288 (void) fprintf(fp, " NO ");
290 (void) fprintf(fp, " YES ");
292 if (zpool_prop_values(prop) == NULL)
293 (void) fprintf(fp, "-\n");
295 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
301 * Display usage message. If we're inside a command, display only the usage for
302 * that command. Otherwise, iterate over the entire command table and display
303 * a complete usage message.
306 usage(boolean_t requested)
308 FILE *fp = requested ? stdout : stderr;
310 if (current_command == NULL) {
313 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
315 gettext("where 'command' is one of the following:\n\n"));
317 for (i = 0; i < NCOMMAND; i++) {
318 if (command_table[i].name == NULL)
319 (void) fprintf(fp, "\n");
321 (void) fprintf(fp, "%s",
322 get_usage(command_table[i].usage));
325 (void) fprintf(fp, gettext("usage:\n"));
326 (void) fprintf(fp, "%s", get_usage(current_command->usage));
329 if (current_command != NULL &&
330 ((strcmp(current_command->name, "set") == 0) ||
331 (strcmp(current_command->name, "get") == 0) ||
332 (strcmp(current_command->name, "list") == 0))) {
335 gettext("\nthe following properties are supported:\n"));
337 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
338 "PROPERTY", "EDIT", "VALUES");
340 /* Iterate over all properties */
341 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
344 (void) fprintf(fp, "\t%-15s ", "feature@...");
345 (void) fprintf(fp, "YES disabled | enabled | active\n");
347 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
348 "appended with a feature name.\nSee zpool-features(7).\n"));
352 * See comments at end of main().
354 if (getenv("ZFS_ABORT") != NULL) {
355 (void) printf("dumping core by request\n");
359 exit(requested ? 0 : 2);
363 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
364 boolean_t print_logs)
371 (void) printf("\t%*s%s\n", indent, "", name);
373 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
374 &child, &children) != 0)
377 for (c = 0; c < children; c++) {
378 uint64_t is_log = B_FALSE;
380 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
382 if ((is_log && !print_logs) || (!is_log && print_logs))
385 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
386 print_vdev_tree(zhp, vname, child[c], indent + 2,
393 prop_list_contains_feature(nvlist_t *proplist)
396 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
397 nvp = nvlist_next_nvpair(proplist, nvp)) {
398 if (zpool_prop_feature(nvpair_name(nvp)))
405 * Add a property pair (name, string-value) into a property nvlist.
408 add_prop_list(const char *propname, char *propval, nvlist_t **props,
411 zpool_prop_t prop = ZPROP_INVAL;
417 if (*props == NULL &&
418 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
419 (void) fprintf(stderr,
420 gettext("internal error: out of memory\n"));
427 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
429 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
430 !zpool_prop_feature(propname)) {
431 (void) fprintf(stderr, gettext("property '%s' is "
432 "not a valid pool property\n"), propname);
437 * feature@ properties and version should not be specified
440 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
441 nvlist_exists(proplist, vname)) ||
442 (prop == ZPOOL_PROP_VERSION &&
443 prop_list_contains_feature(proplist))) {
444 (void) fprintf(stderr, gettext("'feature@' and "
445 "'version' properties cannot be specified "
451 if (zpool_prop_feature(propname))
454 normnm = zpool_prop_to_name(prop);
456 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
457 normnm = zfs_prop_to_name(fprop);
463 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
464 prop != ZPOOL_PROP_CACHEFILE) {
465 (void) fprintf(stderr, gettext("property '%s' "
466 "specified multiple times\n"), propname);
470 if (nvlist_add_string(proplist, normnm, propval) != 0) {
471 (void) fprintf(stderr, gettext("internal "
472 "error: out of memory\n"));
480 * zpool add [-fn] <pool> <vdev> ...
482 * -f Force addition of devices, even if they appear in use
483 * -n Do not add the devices, but display the resulting layout if
484 * they were to be added.
486 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
487 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
491 zpool_do_add(int argc, char **argv)
493 boolean_t force = B_FALSE;
494 boolean_t dryrun = B_FALSE;
503 while ((c = getopt(argc, argv, "fn")) != -1) {
512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
521 /* get pool name and check number of arguments */
523 (void) fprintf(stderr, gettext("missing pool name argument\n"));
527 (void) fprintf(stderr, gettext("missing vdev specification\n"));
536 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
539 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
540 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
546 /* pass off to get_vdev_spec for processing */
547 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
549 if (nvroot == NULL) {
555 nvlist_t *poolnvroot;
557 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
560 (void) printf(gettext("would update '%s' to the following "
561 "configuration:\n"), zpool_get_name(zhp));
563 /* print original main pool and new tree */
564 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
565 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
567 /* Do the same for the logs */
568 if (num_logs(poolnvroot) > 0) {
569 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
570 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
571 } else if (num_logs(nvroot) > 0) {
572 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
577 ret = (zpool_add(zhp, nvroot) != 0);
587 * zpool remove <pool> <vdev> ...
589 * Removes the given vdev from the pool. Currently, this supports removing
590 * spares, cache, and log devices from the pool.
593 zpool_do_remove(int argc, char **argv)
602 /* get pool name and check number of arguments */
604 (void) fprintf(stderr, gettext("missing pool name argument\n"));
608 (void) fprintf(stderr, gettext("missing device\n"));
614 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
617 for (i = 1; i < argc; i++) {
618 if (zpool_vdev_remove(zhp, argv[i]) != 0)
626 * zpool labelclear <vdev>
628 * Verifies that the vdev is not active and zeros out the label information
632 zpool_do_labelclear(int argc, char **argv)
635 int c, fd = -1, ret = 0;
637 boolean_t inuse = B_FALSE;
638 boolean_t force = B_FALSE;
641 while ((c = getopt(argc, argv, "f")) != -1) {
647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
658 (void) fprintf(stderr, gettext("missing vdev device name\n"));
663 if ((fd = open(vdev, O_RDWR)) < 0) {
664 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
669 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
673 (void) fprintf(stderr,
674 gettext("Unable to determine pool state for %s\n"
675 "Use -f to force the clearing any label data\n"), vdev);
683 case POOL_STATE_ACTIVE:
684 case POOL_STATE_SPARE:
685 case POOL_STATE_L2CACHE:
686 (void) fprintf(stderr,
687 gettext("labelclear operation failed.\n"
688 "\tVdev %s is a member (%s), of pool \"%s\".\n"
689 "\tTo remove label information from this device, export or destroy\n"
690 "\tthe pool, or remove %s from the configuration of this pool\n"
691 "\tand retry the labelclear operation\n"),
692 vdev, zpool_pool_state_to_name(state), name, vdev);
696 case POOL_STATE_EXPORTED:
700 (void) fprintf(stderr,
701 gettext("labelclear operation failed.\n"
702 "\tVdev %s is a member of the exported pool \"%s\".\n"
703 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
709 case POOL_STATE_POTENTIALLY_ACTIVE:
713 (void) fprintf(stderr,
714 gettext("labelclear operation failed.\n"
715 "\tVdev %s is a member of the pool \"%s\".\n"
716 "\tThis pool is unknown to this system, but may be active on\n"
717 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
718 "\tremoval of label information.\n"),
723 case POOL_STATE_DESTROYED:
724 /* inuse should never be set for a destoryed pool... */
730 if (zpool_clear_label(fd) != 0) {
731 (void) fprintf(stderr,
732 gettext("Label clear failed on vdev %s\n"), vdev);
745 * zpool create [-fnd] [-o property=value] ...
746 * [-O file-system-property=value] ...
747 * [-R root] [-m mountpoint] <pool> <dev> ...
749 * -f Force creation, even if devices appear in use
750 * -n Do not create the pool, but display the resulting layout if it
751 * were to be created.
752 * -R Create a pool under an alternate root
753 * -m Set default mountpoint for the root dataset. By default it's
755 * -o Set property=value.
756 * -d Don't automatically enable all supported pool features
757 * (individual features can be enabled with -o).
758 * -O Set fsproperty=value in the pool's root file system
760 * Creates the named pool according to the given vdev specification. The
761 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
762 * we get the nvlist back from get_vdev_spec(), we either print out the contents
763 * (if '-n' was specified), or pass it to libzfs to do the creation.
766 zpool_do_create(int argc, char **argv)
768 boolean_t force = B_FALSE;
769 boolean_t dryrun = B_FALSE;
770 boolean_t enable_all_pool_feat = B_TRUE;
772 nvlist_t *nvroot = NULL;
775 char *altroot = NULL;
776 char *mountpoint = NULL;
777 nvlist_t *fsprops = NULL;
778 nvlist_t *props = NULL;
782 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
791 enable_all_pool_feat = B_FALSE;
795 if (add_prop_list(zpool_prop_to_name(
796 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
798 if (nvlist_lookup_string(props,
799 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
802 if (add_prop_list(zpool_prop_to_name(
803 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
807 /* Equivalent to -O mountpoint=optarg */
811 if ((propval = strchr(optarg, '=')) == NULL) {
812 (void) fprintf(stderr, gettext("missing "
813 "'=' for -o option\n"));
819 if (add_prop_list(optarg, propval, &props, B_TRUE))
823 * If the user is creating a pool that doesn't support
824 * feature flags, don't enable any features.
826 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
830 ver = strtoull(propval, &end, 10);
832 ver < SPA_VERSION_FEATURES) {
833 enable_all_pool_feat = B_FALSE;
838 if ((propval = strchr(optarg, '=')) == NULL) {
839 (void) fprintf(stderr, gettext("missing "
840 "'=' for -O option\n"));
847 * Mountpoints are checked and then added later.
848 * Uniquely among properties, they can be specified
849 * more than once, to avoid conflict with -m.
851 if (0 == strcmp(optarg,
852 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
853 mountpoint = propval;
854 } else if (add_prop_list(optarg, propval, &fsprops,
860 (void) fprintf(stderr, gettext("missing argument for "
861 "'%c' option\n"), optopt);
864 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
873 /* get pool name and check number of arguments */
875 (void) fprintf(stderr, gettext("missing pool name argument\n"));
879 (void) fprintf(stderr, gettext("missing vdev specification\n"));
886 * As a special case, check for use of '/' in the name, and direct the
887 * user to use 'zfs create' instead.
889 if (strchr(poolname, '/') != NULL) {
890 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
891 "character '/' in pool name\n"), poolname);
892 (void) fprintf(stderr, gettext("use 'zfs create' to "
893 "create a dataset\n"));
897 /* pass off to get_vdev_spec for bulk processing */
898 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
903 /* make_root_vdev() allows 0 toplevel children if there are spares */
904 if (!zfs_allocatable_devs(nvroot)) {
905 (void) fprintf(stderr, gettext("invalid vdev "
906 "specification: at least one toplevel vdev must be "
911 if (altroot != NULL && altroot[0] != '/') {
912 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
913 "must be an absolute path\n"), altroot);
918 * Check the validity of the mountpoint and direct the user to use the
919 * '-m' mountpoint option if it looks like its in use.
920 * Ignore the checks if the '-f' option is given.
922 if (!force && (mountpoint == NULL ||
923 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
924 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
925 char buf[MAXPATHLEN];
928 if (mountpoint && mountpoint[0] != '/') {
929 (void) fprintf(stderr, gettext("invalid mountpoint "
930 "'%s': must be an absolute path, 'legacy', or "
931 "'none'\n"), mountpoint);
935 if (mountpoint == NULL) {
937 (void) snprintf(buf, sizeof (buf), "%s/%s",
940 (void) snprintf(buf, sizeof (buf), "/%s",
944 (void) snprintf(buf, sizeof (buf), "%s%s",
945 altroot, mountpoint);
947 (void) snprintf(buf, sizeof (buf), "%s",
951 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
952 (void) fprintf(stderr, gettext("mountpoint '%s' : "
953 "%s\n"), buf, strerror(errno));
954 (void) fprintf(stderr, gettext("use '-m' "
955 "option to provide a different default\n"));
960 while (count < 3 && readdir(dirp) != NULL)
962 (void) closedir(dirp);
965 (void) fprintf(stderr, gettext("mountpoint "
966 "'%s' exists and is not empty\n"), buf);
967 (void) fprintf(stderr, gettext("use '-m' "
968 "option to provide a "
969 "different default\n"));
976 * Now that the mountpoint's validity has been checked, ensure that
977 * the property is set appropriately prior to creating the pool.
979 if (mountpoint != NULL) {
980 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
981 mountpoint, &fsprops, B_FALSE);
989 * For a dry run invocation, print out a basic message and run
990 * through all the vdevs in the list and print out in an
991 * appropriate hierarchy.
993 (void) printf(gettext("would create '%s' with the "
994 "following layout:\n\n"), poolname);
996 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
997 if (num_logs(nvroot) > 0)
998 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1003 * Hand off to libzfs.
1005 if (enable_all_pool_feat) {
1007 for (i = 0; i < SPA_FEATURES; i++) {
1008 char propname[MAXPATHLEN];
1009 zfeature_info_t *feat = &spa_feature_table[i];
1011 (void) snprintf(propname, sizeof (propname),
1012 "feature@%s", feat->fi_uname);
1015 * Skip feature if user specified it manually
1016 * on the command line.
1018 if (nvlist_exists(props, propname))
1021 ret = add_prop_list(propname,
1022 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1029 if (zpool_create(g_zfs, poolname,
1030 nvroot, props, fsprops) == 0) {
1031 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1032 ZFS_TYPE_FILESYSTEM);
1034 if (zfs_mount(pool, NULL, 0) == 0)
1035 ret = zfs_shareall(pool);
1038 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1039 (void) fprintf(stderr, gettext("pool name may have "
1045 nvlist_free(nvroot);
1046 nvlist_free(fsprops);
1050 nvlist_free(fsprops);
1057 * zpool destroy <pool>
1059 * -f Forcefully unmount any datasets
1061 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1064 zpool_do_destroy(int argc, char **argv)
1066 boolean_t force = B_FALSE;
1069 zpool_handle_t *zhp;
1073 while ((c = getopt(argc, argv, "f")) != -1) {
1079 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1088 /* check arguments */
1090 (void) fprintf(stderr, gettext("missing pool argument\n"));
1094 (void) fprintf(stderr, gettext("too many arguments\n"));
1100 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1102 * As a special case, check for use of '/' in the name, and
1103 * direct the user to use 'zfs destroy' instead.
1105 if (strchr(pool, '/') != NULL)
1106 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1107 "destroy a dataset\n"));
1111 if (zpool_disable_datasets(zhp, force) != 0) {
1112 (void) fprintf(stderr, gettext("could not destroy '%s': "
1113 "could not unmount datasets\n"), zpool_get_name(zhp));
1117 /* The history must be logged as part of the export */
1118 log_history = B_FALSE;
1120 ret = (zpool_destroy(zhp, history_str) != 0);
1128 * zpool export [-f] <pool> ...
1130 * -f Forcefully unmount datasets
1132 * Export the given pools. By default, the command will attempt to cleanly
1133 * unmount any active datasets within the pool. If the '-f' flag is specified,
1134 * then the datasets will be forcefully unmounted.
1137 zpool_do_export(int argc, char **argv)
1139 boolean_t force = B_FALSE;
1140 boolean_t hardforce = B_FALSE;
1142 zpool_handle_t *zhp;
1147 while ((c = getopt(argc, argv, "fF")) != -1) {
1156 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1165 /* check arguments */
1167 (void) fprintf(stderr, gettext("missing pool argument\n"));
1172 for (i = 0; i < argc; i++) {
1173 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1178 if (zpool_disable_datasets(zhp, force) != 0) {
1184 /* The history must be logged as part of the export */
1185 log_history = B_FALSE;
1188 if (zpool_export_force(zhp, history_str) != 0)
1190 } else if (zpool_export(zhp, force, history_str) != 0) {
1201 * Given a vdev configuration, determine the maximum width needed for the device
1205 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1207 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1212 if (strlen(name) + depth > max)
1213 max = strlen(name) + depth;
1217 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1218 &child, &children) == 0) {
1219 for (c = 0; c < children; c++)
1220 if ((ret = max_width(zhp, child[c], depth + 2,
1225 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1226 &child, &children) == 0) {
1227 for (c = 0; c < children; c++)
1228 if ((ret = max_width(zhp, child[c], depth + 2,
1233 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1234 &child, &children) == 0) {
1235 for (c = 0; c < children; c++)
1236 if ((ret = max_width(zhp, child[c], depth + 2,
1245 typedef struct spare_cbdata {
1247 zpool_handle_t *cb_zhp;
1251 find_vdev(nvlist_t *nv, uint64_t search)
1257 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1261 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1262 &child, &children) == 0) {
1263 for (c = 0; c < children; c++)
1264 if (find_vdev(child[c], search))
1272 find_spare(zpool_handle_t *zhp, void *data)
1274 spare_cbdata_t *cbp = data;
1275 nvlist_t *config, *nvroot;
1277 config = zpool_get_config(zhp, NULL);
1278 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1281 if (find_vdev(nvroot, cbp->cb_guid)) {
1291 * Print out configuration state as requested by status_callback.
1294 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1295 int namewidth, int depth, boolean_t isspare)
1298 uint_t c, vsc, children;
1299 pool_scan_stat_t *ps = NULL;
1301 char rbuf[6], wbuf[6], cbuf[6];
1303 uint64_t notpresent;
1308 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1309 &child, &children) != 0)
1312 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1313 (uint64_t **)&vs, &vsc) == 0);
1315 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1318 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1321 if (vs->vs_aux == VDEV_AUX_SPARED)
1323 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1327 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1331 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1332 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1333 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1334 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1337 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1338 ¬present) == 0 ||
1339 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1341 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1342 (void) printf(" was %s", path);
1343 } else if (vs->vs_aux != 0) {
1346 switch (vs->vs_aux) {
1347 case VDEV_AUX_OPEN_FAILED:
1348 (void) printf(gettext("cannot open"));
1351 case VDEV_AUX_BAD_GUID_SUM:
1352 (void) printf(gettext("missing device"));
1355 case VDEV_AUX_NO_REPLICAS:
1356 (void) printf(gettext("insufficient replicas"));
1359 case VDEV_AUX_VERSION_NEWER:
1360 (void) printf(gettext("newer version"));
1363 case VDEV_AUX_UNSUP_FEAT:
1364 (void) printf(gettext("unsupported feature(s)"));
1367 case VDEV_AUX_ASHIFT_TOO_BIG:
1368 (void) printf(gettext("unsupported minimum blocksize"));
1371 case VDEV_AUX_SPARED:
1372 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1374 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1375 if (strcmp(zpool_get_name(cb.cb_zhp),
1376 zpool_get_name(zhp)) == 0)
1377 (void) printf(gettext("currently in "
1380 (void) printf(gettext("in use by "
1382 zpool_get_name(cb.cb_zhp));
1383 zpool_close(cb.cb_zhp);
1385 (void) printf(gettext("currently in use"));
1389 case VDEV_AUX_ERR_EXCEEDED:
1390 (void) printf(gettext("too many errors"));
1393 case VDEV_AUX_IO_FAILURE:
1394 (void) printf(gettext("experienced I/O failures"));
1397 case VDEV_AUX_BAD_LOG:
1398 (void) printf(gettext("bad intent log"));
1401 case VDEV_AUX_EXTERNAL:
1402 (void) printf(gettext("external device fault"));
1405 case VDEV_AUX_SPLIT_POOL:
1406 (void) printf(gettext("split into new pool"));
1410 (void) printf(gettext("corrupted data"));
1413 } else if (children == 0 && !isspare &&
1414 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1415 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1417 gettext(" block size: %dB configured, %dB native"),
1418 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1421 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1422 (uint64_t **)&ps, &c);
1424 if (ps && ps->pss_state == DSS_SCANNING &&
1425 vs->vs_scan_processed != 0 && children == 0) {
1426 (void) printf(gettext(" (%s)"),
1427 (ps->pss_func == POOL_SCAN_RESILVER) ?
1428 "resilvering" : "repairing");
1431 (void) printf("\n");
1433 for (c = 0; c < children; c++) {
1434 uint64_t islog = B_FALSE, ishole = B_FALSE;
1436 /* Don't print logs or holes here */
1437 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1439 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1441 if (islog || ishole)
1443 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1444 print_status_config(zhp, vname, child[c],
1445 namewidth, depth + 2, isspare);
1452 * Print the configuration of an exported pool. Iterate over all vdevs in the
1453 * pool, printing out the name and status for each one.
1456 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1463 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1464 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1465 strcmp(type, VDEV_TYPE_HOLE) == 0)
1468 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1469 (uint64_t **)&vs, &c) == 0);
1471 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1472 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1474 if (vs->vs_aux != 0) {
1477 switch (vs->vs_aux) {
1478 case VDEV_AUX_OPEN_FAILED:
1479 (void) printf(gettext("cannot open"));
1482 case VDEV_AUX_BAD_GUID_SUM:
1483 (void) printf(gettext("missing device"));
1486 case VDEV_AUX_NO_REPLICAS:
1487 (void) printf(gettext("insufficient replicas"));
1490 case VDEV_AUX_VERSION_NEWER:
1491 (void) printf(gettext("newer version"));
1494 case VDEV_AUX_UNSUP_FEAT:
1495 (void) printf(gettext("unsupported feature(s)"));
1498 case VDEV_AUX_ERR_EXCEEDED:
1499 (void) printf(gettext("too many errors"));
1503 (void) printf(gettext("corrupted data"));
1507 (void) printf("\n");
1509 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1510 &child, &children) != 0)
1513 for (c = 0; c < children; c++) {
1514 uint64_t is_log = B_FALSE;
1516 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1521 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1522 print_import_config(vname, child[c], namewidth, depth + 2);
1526 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1527 &child, &children) == 0) {
1528 (void) printf(gettext("\tcache\n"));
1529 for (c = 0; c < children; c++) {
1530 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1531 (void) printf("\t %s\n", vname);
1536 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1537 &child, &children) == 0) {
1538 (void) printf(gettext("\tspares\n"));
1539 for (c = 0; c < children; c++) {
1540 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1541 (void) printf("\t %s\n", vname);
1549 * Logs are recorded as top level vdevs in the main pool child array
1550 * but with "is_log" set to 1. We use either print_status_config() or
1551 * print_import_config() to print the top level logs then any log
1552 * children (eg mirrored slogs) are printed recursively - which
1553 * works because only the top level vdev is marked "is_log"
1556 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1561 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1565 (void) printf(gettext("\tlogs\n"));
1567 for (c = 0; c < children; c++) {
1568 uint64_t is_log = B_FALSE;
1571 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1575 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1577 print_status_config(zhp, name, child[c], namewidth,
1580 print_import_config(name, child[c], namewidth, 2);
1586 * Display the status for the given pool.
1589 show_import(nvlist_t *config)
1591 uint64_t pool_state;
1603 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1605 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1607 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1609 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1612 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1613 (uint64_t **)&vs, &vsc) == 0);
1614 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1616 reason = zpool_import_status(config, &msgid);
1618 (void) printf(gettext(" pool: %s\n"), name);
1619 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1620 (void) printf(gettext(" state: %s"), health);
1621 if (pool_state == POOL_STATE_DESTROYED)
1622 (void) printf(gettext(" (DESTROYED)"));
1623 (void) printf("\n");
1626 case ZPOOL_STATUS_MISSING_DEV_R:
1627 case ZPOOL_STATUS_MISSING_DEV_NR:
1628 case ZPOOL_STATUS_BAD_GUID_SUM:
1629 (void) printf(gettext(" status: One or more devices are "
1630 "missing from the system.\n"));
1633 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1634 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1635 (void) printf(gettext(" status: One or more devices contains "
1636 "corrupted data.\n"));
1639 case ZPOOL_STATUS_CORRUPT_DATA:
1641 gettext(" status: The pool data is corrupted.\n"));
1644 case ZPOOL_STATUS_OFFLINE_DEV:
1645 (void) printf(gettext(" status: One or more devices "
1646 "are offlined.\n"));
1649 case ZPOOL_STATUS_CORRUPT_POOL:
1650 (void) printf(gettext(" status: The pool metadata is "
1654 case ZPOOL_STATUS_VERSION_OLDER:
1655 (void) printf(gettext(" status: The pool is formatted using a "
1656 "legacy on-disk version.\n"));
1659 case ZPOOL_STATUS_VERSION_NEWER:
1660 (void) printf(gettext(" status: The pool is formatted using an "
1661 "incompatible version.\n"));
1664 case ZPOOL_STATUS_FEAT_DISABLED:
1665 (void) printf(gettext(" status: Some supported features are "
1666 "not enabled on the pool.\n"));
1669 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1670 (void) printf(gettext("status: The pool uses the following "
1671 "feature(s) not supported on this sytem:\n"));
1672 zpool_print_unsup_feat(config);
1675 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1676 (void) printf(gettext("status: The pool can only be accessed "
1677 "in read-only mode on this system. It\n\tcannot be "
1678 "accessed in read-write mode because it uses the "
1679 "following\n\tfeature(s) not supported on this system:\n"));
1680 zpool_print_unsup_feat(config);
1683 case ZPOOL_STATUS_HOSTID_MISMATCH:
1684 (void) printf(gettext(" status: The pool was last accessed by "
1685 "another system.\n"));
1688 case ZPOOL_STATUS_FAULTED_DEV_R:
1689 case ZPOOL_STATUS_FAULTED_DEV_NR:
1690 (void) printf(gettext(" status: One or more devices are "
1694 case ZPOOL_STATUS_BAD_LOG:
1695 (void) printf(gettext(" status: An intent log record cannot be "
1699 case ZPOOL_STATUS_RESILVERING:
1700 (void) printf(gettext(" status: One or more devices were being "
1706 * No other status can be seen when importing pools.
1708 assert(reason == ZPOOL_STATUS_OK);
1712 * Print out an action according to the overall state of the pool.
1714 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1715 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1716 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1717 (void) printf(gettext(" action: The pool can be "
1718 "imported using its name or numeric identifier, "
1719 "though\n\tsome features will not be available "
1720 "without an explicit 'zpool upgrade'.\n"));
1721 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1722 (void) printf(gettext(" action: The pool can be "
1723 "imported using its name or numeric "
1724 "identifier and\n\tthe '-f' flag.\n"));
1726 (void) printf(gettext(" action: The pool can be "
1727 "imported using its name or numeric "
1730 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1731 (void) printf(gettext(" action: The pool can be imported "
1732 "despite missing or damaged devices. The\n\tfault "
1733 "tolerance of the pool may be compromised if imported.\n"));
1736 case ZPOOL_STATUS_VERSION_NEWER:
1737 (void) printf(gettext(" action: The pool cannot be "
1738 "imported. Access the pool on a system running "
1739 "newer\n\tsoftware, or recreate the pool from "
1742 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1743 (void) printf(gettext("action: The pool cannot be "
1744 "imported. Access the pool on a system that "
1745 "supports\n\tthe required feature(s), or recreate "
1746 "the pool from backup.\n"));
1748 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1749 (void) printf(gettext("action: The pool cannot be "
1750 "imported in read-write mode. Import the pool "
1752 "\t\"-o readonly=on\", access the pool on a system "
1753 "that supports the\n\trequired feature(s), or "
1754 "recreate the pool from backup.\n"));
1756 case ZPOOL_STATUS_MISSING_DEV_R:
1757 case ZPOOL_STATUS_MISSING_DEV_NR:
1758 case ZPOOL_STATUS_BAD_GUID_SUM:
1759 (void) printf(gettext(" action: The pool cannot be "
1760 "imported. Attach the missing\n\tdevices and try "
1764 (void) printf(gettext(" action: The pool cannot be "
1765 "imported due to damaged devices or data.\n"));
1769 /* Print the comment attached to the pool. */
1770 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1771 (void) printf(gettext("comment: %s\n"), comment);
1774 * If the state is "closed" or "can't open", and the aux state
1775 * is "corrupt data":
1777 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1778 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1779 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1780 if (pool_state == POOL_STATE_DESTROYED)
1781 (void) printf(gettext("\tThe pool was destroyed, "
1782 "but can be imported using the '-Df' flags.\n"));
1783 else if (pool_state != POOL_STATE_EXPORTED)
1784 (void) printf(gettext("\tThe pool may be active on "
1785 "another system, but can be imported using\n\t"
1786 "the '-f' flag.\n"));
1790 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1793 (void) printf(gettext(" config:\n\n"));
1795 namewidth = max_width(NULL, nvroot, 0, 0);
1799 print_import_config(name, nvroot, namewidth, 0);
1800 if (num_logs(nvroot) > 0)
1801 print_logs(NULL, nvroot, namewidth, B_FALSE);
1803 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1804 (void) printf(gettext("\n\tAdditional devices are known to "
1805 "be part of this pool, though their\n\texact "
1806 "configuration cannot be determined.\n"));
1811 * Perform the import for the given configuration. This passes the heavy
1812 * lifting off to zpool_import_props(), and then mounts the datasets contained
1816 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1817 nvlist_t *props, int flags)
1819 zpool_handle_t *zhp;
1824 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1827 verify(nvlist_lookup_uint64(config,
1828 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1829 verify(nvlist_lookup_uint64(config,
1830 ZPOOL_CONFIG_VERSION, &version) == 0);
1831 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1832 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1833 "is formatted using an unsupported ZFS version\n"), name);
1835 } else if (state != POOL_STATE_EXPORTED &&
1836 !(flags & ZFS_IMPORT_ANY_HOST)) {
1839 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1841 if ((unsigned long)hostid != gethostid()) {
1846 verify(nvlist_lookup_string(config,
1847 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1848 verify(nvlist_lookup_uint64(config,
1849 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1851 (void) fprintf(stderr, gettext("cannot import "
1852 "'%s': pool may be in use from other "
1853 "system, it was last accessed by %s "
1854 "(hostid: 0x%lx) on %s"), name, hostname,
1855 (unsigned long)hostid,
1856 asctime(localtime(&t)));
1857 (void) fprintf(stderr, gettext("use '-f' to "
1858 "import anyway\n"));
1862 (void) fprintf(stderr, gettext("cannot import '%s': "
1863 "pool may be in use from other system\n"), name);
1864 (void) fprintf(stderr, gettext("use '-f' to import "
1870 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1873 if (newname != NULL)
1874 name = (char *)newname;
1876 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1879 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1880 !(flags & ZFS_IMPORT_ONLY) &&
1881 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1891 * zpool import [-d dir] [-D]
1892 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1893 * [-d dir | -c cachefile] [-f] -a
1894 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1895 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1897 * -c Read pool information from a cachefile instead of searching
1900 * -d Scan in a specific directory, other than /dev/dsk. More than
1901 * one directory can be specified using multiple '-d' options.
1903 * -D Scan for previously destroyed pools or import all or only
1904 * specified destroyed pools.
1906 * -R Temporarily import the pool, with all mountpoints relative to
1907 * the given root. The pool will remain exported when the machine
1910 * -V Import even in the presence of faulted vdevs. This is an
1911 * intentionally undocumented option for testing purposes, and
1912 * treats the pool configuration as complete, leaving any bad
1913 * vdevs in the FAULTED state. In other words, it does verbatim
1916 * -f Force import, even if it appears that the pool is active.
1918 * -F Attempt rewind if necessary.
1920 * -n See if rewind would work, but don't actually rewind.
1922 * -N Import the pool but don't mount datasets.
1924 * -T Specify a starting txg to use for import. This option is
1925 * intentionally undocumented option for testing purposes.
1927 * -a Import all pools found.
1929 * -o Set property=value and/or temporary mount options (without '=').
1931 * The import command scans for pools to import, and import pools based on pool
1932 * name and GUID. The pool can also be renamed as part of the import process.
1935 zpool_do_import(int argc, char **argv)
1937 char **searchdirs = NULL;
1941 nvlist_t *pools = NULL;
1942 boolean_t do_all = B_FALSE;
1943 boolean_t do_destroyed = B_FALSE;
1944 char *mntopts = NULL;
1947 uint64_t searchguid = 0;
1948 char *searchname = NULL;
1950 nvlist_t *found_config;
1951 nvlist_t *policy = NULL;
1952 nvlist_t *props = NULL;
1954 int flags = ZFS_IMPORT_NORMAL;
1955 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1956 boolean_t dryrun = B_FALSE;
1957 boolean_t do_rewind = B_FALSE;
1958 boolean_t xtreme_rewind = B_FALSE;
1959 uint64_t pool_state, txg = -1ULL;
1960 char *cachefile = NULL;
1961 importargs_t idata = { 0 };
1965 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1974 if (searchdirs == NULL) {
1975 searchdirs = safe_malloc(sizeof (char *));
1977 char **tmp = safe_malloc((nsearch + 1) *
1979 bcopy(searchdirs, tmp, nsearch *
1984 searchdirs[nsearch++] = optarg;
1987 do_destroyed = B_TRUE;
1990 flags |= ZFS_IMPORT_ANY_HOST;
1996 flags |= ZFS_IMPORT_MISSING_LOG;
2002 flags |= ZFS_IMPORT_ONLY;
2005 if ((propval = strchr(optarg, '=')) != NULL) {
2008 if (add_prop_list(optarg, propval,
2016 if (add_prop_list(zpool_prop_to_name(
2017 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2019 if (nvlist_lookup_string(props,
2020 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2023 if (add_prop_list(zpool_prop_to_name(
2024 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2029 txg = strtoull(optarg, &endptr, 10);
2030 if (errno != 0 || *endptr != '\0') {
2031 (void) fprintf(stderr,
2032 gettext("invalid txg value\n"));
2035 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2038 flags |= ZFS_IMPORT_VERBATIM;
2041 xtreme_rewind = B_TRUE;
2044 (void) fprintf(stderr, gettext("missing argument for "
2045 "'%c' option\n"), optopt);
2049 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2058 if (cachefile && nsearch != 0) {
2059 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2063 if ((dryrun || xtreme_rewind) && !do_rewind) {
2064 (void) fprintf(stderr,
2065 gettext("-n or -X only meaningful with -F\n"));
2069 rewind_policy = ZPOOL_TRY_REWIND;
2071 rewind_policy = ZPOOL_DO_REWIND;
2073 rewind_policy |= ZPOOL_EXTREME_REWIND;
2075 /* In the future, we can capture further policy and include it here */
2076 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2077 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2078 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2081 if (searchdirs == NULL) {
2082 searchdirs = safe_malloc(sizeof (char *));
2083 searchdirs[0] = "/dev";
2087 /* check argument count */
2090 (void) fprintf(stderr, gettext("too many arguments\n"));
2095 (void) fprintf(stderr, gettext("too many arguments\n"));
2100 * Check for the SYS_CONFIG privilege. We do this explicitly
2101 * here because otherwise any attempt to discover pools will
2104 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2105 (void) fprintf(stderr, gettext("cannot "
2106 "discover pools: permission denied\n"));
2108 nvlist_free(policy);
2114 * Depending on the arguments given, we do one of the following:
2116 * <none> Iterate through all pools and display information about
2119 * -a Iterate through all pools and try to import each one.
2121 * <id> Find the pool that corresponds to the given GUID/pool
2122 * name and import that one.
2124 * -D Above options applies only to destroyed pools.
2130 searchguid = strtoull(argv[0], &endptr, 10);
2131 if (errno != 0 || *endptr != '\0')
2132 searchname = argv[0];
2133 found_config = NULL;
2136 * User specified a name or guid. Ensure it's unique.
2138 idata.unique = B_TRUE;
2142 idata.path = searchdirs;
2143 idata.paths = nsearch;
2144 idata.poolname = searchname;
2145 idata.guid = searchguid;
2146 idata.cachefile = cachefile;
2148 pools = zpool_search_import(g_zfs, &idata);
2150 if (pools != NULL && idata.exists &&
2151 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2152 (void) fprintf(stderr, gettext("cannot import '%s': "
2153 "a pool with that name already exists\n"),
2155 (void) fprintf(stderr, gettext("use the form '%s "
2156 "<pool | id> <newpool>' to give it a new name\n"),
2159 } else if (pools == NULL && idata.exists) {
2160 (void) fprintf(stderr, gettext("cannot import '%s': "
2161 "a pool with that name is already created/imported,\n"),
2163 (void) fprintf(stderr, gettext("and no additional pools "
2164 "with that name were found\n"));
2166 } else if (pools == NULL) {
2168 (void) fprintf(stderr, gettext("cannot import '%s': "
2169 "no such pool available\n"), argv[0]);
2176 nvlist_free(policy);
2181 * At this point we have a list of import candidate configs. Even if
2182 * we were searching by pool name or guid, we still need to
2183 * post-process the list to deal with pool state and possible
2189 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2191 verify(nvpair_value_nvlist(elem, &config) == 0);
2193 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2195 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2197 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2200 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2207 (void) printf("\n");
2210 err |= do_import(config, NULL, mntopts,
2213 show_import(config);
2215 } else if (searchname != NULL) {
2219 * We are searching for a pool based on name.
2221 verify(nvlist_lookup_string(config,
2222 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2224 if (strcmp(name, searchname) == 0) {
2225 if (found_config != NULL) {
2226 (void) fprintf(stderr, gettext(
2227 "cannot import '%s': more than "
2228 "one matching pool\n"), searchname);
2229 (void) fprintf(stderr, gettext(
2230 "import by numeric ID instead\n"));
2233 found_config = config;
2239 * Search for a pool by guid.
2241 verify(nvlist_lookup_uint64(config,
2242 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2244 if (guid == searchguid)
2245 found_config = config;
2250 * If we were searching for a specific pool, verify that we found a
2251 * pool, and then do the import.
2253 if (argc != 0 && err == 0) {
2254 if (found_config == NULL) {
2255 (void) fprintf(stderr, gettext("cannot import '%s': "
2256 "no such pool available\n"), argv[0]);
2259 err |= do_import(found_config, argc == 1 ? NULL :
2260 argv[1], mntopts, props, flags);
2265 * If we were just looking for pools, report an error if none were
2268 if (argc == 0 && first)
2269 (void) fprintf(stderr,
2270 gettext("no pools available to import\n"));
2275 nvlist_free(policy);
2278 return (err ? 1 : 0);
2281 typedef struct iostat_cbdata {
2282 boolean_t cb_verbose;
2285 zpool_list_t *cb_list;
2289 print_iostat_separator(iostat_cbdata_t *cb)
2293 for (i = 0; i < cb->cb_namewidth; i++)
2295 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2299 print_iostat_header(iostat_cbdata_t *cb)
2301 (void) printf("%*s capacity operations bandwidth\n",
2302 cb->cb_namewidth, "");
2303 (void) printf("%-*s alloc free read write read write\n",
2304 cb->cb_namewidth, "pool");
2305 print_iostat_separator(cb);
2309 * Display a single statistic.
2312 print_one_stat(uint64_t value)
2316 zfs_nicenum(value, buf, sizeof (buf));
2317 (void) printf(" %5s", buf);
2321 * Print out all the statistics for the given vdev. This can either be the
2322 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2323 * is a verbose output, and we don't want to display the toplevel pool stats.
2326 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2327 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2329 nvlist_t **oldchild, **newchild;
2331 vdev_stat_t *oldvs, *newvs;
2332 vdev_stat_t zerovs = { 0 };
2337 if (oldnv != NULL) {
2338 verify(nvlist_lookup_uint64_array(oldnv,
2339 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2344 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2345 (uint64_t **)&newvs, &c) == 0);
2347 if (strlen(name) + depth > cb->cb_namewidth)
2348 (void) printf("%*s%s", depth, "", name);
2350 (void) printf("%*s%s%*s", depth, "", name,
2351 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2353 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2358 scale = (double)NANOSEC / tdelta;
2360 /* only toplevel vdevs have capacity stats */
2361 if (newvs->vs_space == 0) {
2362 (void) printf(" - -");
2364 print_one_stat(newvs->vs_alloc);
2365 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2368 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2369 oldvs->vs_ops[ZIO_TYPE_READ])));
2371 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2372 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2374 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2375 oldvs->vs_bytes[ZIO_TYPE_READ])));
2377 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2378 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2380 (void) printf("\n");
2382 if (!cb->cb_verbose)
2385 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2386 &newchild, &children) != 0)
2389 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2390 &oldchild, &c) != 0)
2393 for (c = 0; c < children; c++) {
2394 uint64_t ishole = B_FALSE, islog = B_FALSE;
2396 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2399 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2402 if (ishole || islog)
2405 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2406 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2407 newchild[c], cb, depth + 2);
2412 * Log device section
2415 if (num_logs(newnv) > 0) {
2416 (void) printf("%-*s - - - - - "
2417 "-\n", cb->cb_namewidth, "logs");
2419 for (c = 0; c < children; c++) {
2420 uint64_t islog = B_FALSE;
2421 (void) nvlist_lookup_uint64(newchild[c],
2422 ZPOOL_CONFIG_IS_LOG, &islog);
2425 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2427 print_vdev_stats(zhp, vname, oldnv ?
2428 oldchild[c] : NULL, newchild[c],
2437 * Include level 2 ARC devices in iostat output
2439 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2440 &newchild, &children) != 0)
2443 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2444 &oldchild, &c) != 0)
2448 (void) printf("%-*s - - - - - "
2449 "-\n", cb->cb_namewidth, "cache");
2450 for (c = 0; c < children; c++) {
2451 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2453 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2454 newchild[c], cb, depth + 2);
2461 refresh_iostat(zpool_handle_t *zhp, void *data)
2463 iostat_cbdata_t *cb = data;
2467 * If the pool has disappeared, remove it from the list and continue.
2469 if (zpool_refresh_stats(zhp, &missing) != 0)
2473 pool_list_remove(cb->cb_list, zhp);
2479 * Callback to print out the iostats for the given pool.
2482 print_iostat(zpool_handle_t *zhp, void *data)
2484 iostat_cbdata_t *cb = data;
2485 nvlist_t *oldconfig, *newconfig;
2486 nvlist_t *oldnvroot, *newnvroot;
2488 newconfig = zpool_get_config(zhp, &oldconfig);
2490 if (cb->cb_iteration == 1)
2493 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2496 if (oldconfig == NULL)
2499 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2503 * Print out the statistics for the pool.
2505 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2508 print_iostat_separator(cb);
2514 get_namewidth(zpool_handle_t *zhp, void *data)
2516 iostat_cbdata_t *cb = data;
2517 nvlist_t *config, *nvroot;
2519 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2520 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2522 if (!cb->cb_verbose)
2523 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2525 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2530 * The width must fall into the range [10,38]. The upper limit is the
2531 * maximum we can have and still fit in 80 columns.
2533 if (cb->cb_namewidth < 10)
2534 cb->cb_namewidth = 10;
2535 if (cb->cb_namewidth > 38)
2536 cb->cb_namewidth = 38;
2542 * Parse the input string, get the 'interval' and 'count' value if there is one.
2545 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2548 unsigned long interval = 0, count = 0;
2549 int argc = *argcp, errno;
2552 * Determine if the last argument is an integer or a pool name
2554 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2558 interval = strtoul(argv[argc - 1], &end, 10);
2560 if (*end == '\0' && errno == 0) {
2561 if (interval == 0) {
2562 (void) fprintf(stderr, gettext("interval "
2563 "cannot be zero\n"));
2567 * Ignore the last parameter
2572 * If this is not a valid number, just plow on. The
2573 * user will get a more informative error message later
2581 * If the last argument is also an integer, then we have both a count
2584 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2589 interval = strtoul(argv[argc - 1], &end, 10);
2591 if (*end == '\0' && errno == 0) {
2592 if (interval == 0) {
2593 (void) fprintf(stderr, gettext("interval "
2594 "cannot be zero\n"));
2599 * Ignore the last parameter
2613 get_timestamp_arg(char c)
2616 timestamp_fmt = UDATE;
2618 timestamp_fmt = DDATE;
2624 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2626 * -v Display statistics for individual vdevs
2627 * -T Display a timestamp in date(1) or Unix format
2629 * This command can be tricky because we want to be able to deal with pool
2630 * creation/destruction as well as vdev configuration changes. The bulk of this
2631 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2632 * on pool_list_update() to detect the addition of new pools. Configuration
2633 * changes are all handled within libzfs.
2636 zpool_do_iostat(int argc, char **argv)
2641 unsigned long interval = 0, count = 0;
2643 boolean_t verbose = B_FALSE;
2647 while ((c = getopt(argc, argv, "T:v")) != -1) {
2650 get_timestamp_arg(*optarg);
2656 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2665 get_interval_count(&argc, argv, &interval, &count);
2668 * Construct the list of all interesting pools.
2671 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2674 if (pool_list_count(list) == 0 && argc != 0) {
2675 pool_list_free(list);
2679 if (pool_list_count(list) == 0 && interval == 0) {
2680 pool_list_free(list);
2681 (void) fprintf(stderr, gettext("no pools available\n"));
2686 * Enter the main iostat loop.
2689 cb.cb_verbose = verbose;
2690 cb.cb_iteration = 0;
2691 cb.cb_namewidth = 0;
2694 pool_list_update(list);
2696 if ((npools = pool_list_count(list)) == 0)
2700 * Refresh all statistics. This is done as an explicit step
2701 * before calculating the maximum name width, so that any
2702 * configuration changes are properly accounted for.
2704 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2707 * Iterate over all pools to determine the maximum width
2708 * for the pool / device name column across all pools.
2710 cb.cb_namewidth = 0;
2711 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2713 if (timestamp_fmt != NODATE)
2714 print_timestamp(timestamp_fmt);
2717 * If it's the first time, or verbose mode, print the header.
2719 if (++cb.cb_iteration == 1 || verbose)
2720 print_iostat_header(&cb);
2722 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2725 * If there's more than one pool, and we're not in verbose mode
2726 * (which prints a separator for us), then print a separator.
2728 if (npools > 1 && !verbose)
2729 print_iostat_separator(&cb);
2732 (void) printf("\n");
2735 * Flush the output so that redirection to a file isn't buffered
2738 (void) fflush(stdout);
2743 if (count != 0 && --count == 0)
2746 (void) sleep(interval);
2749 pool_list_free(list);
2754 typedef struct list_cbdata {
2755 boolean_t cb_verbose;
2757 boolean_t cb_scripted;
2758 zprop_list_t *cb_proplist;
2762 * Given a list of columns to display, output appropriate headers for each one.
2765 print_header(list_cbdata_t *cb)
2767 zprop_list_t *pl = cb->cb_proplist;
2768 char headerbuf[ZPOOL_MAXPROPLEN];
2770 boolean_t first = B_TRUE;
2771 boolean_t right_justify;
2774 for (; pl != NULL; pl = pl->pl_next) {
2775 width = pl->pl_width;
2776 if (first && cb->cb_verbose) {
2778 * Reset the width to accommodate the verbose listing
2781 width = cb->cb_namewidth;
2789 right_justify = B_FALSE;
2790 if (pl->pl_prop != ZPROP_INVAL) {
2791 header = zpool_prop_column_name(pl->pl_prop);
2792 right_justify = zpool_prop_align_right(pl->pl_prop);
2796 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2797 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2798 headerbuf[i] = '\0';
2802 if (pl->pl_next == NULL && !right_justify)
2803 (void) printf("%s", header);
2804 else if (right_justify)
2805 (void) printf("%*s", width, header);
2807 (void) printf("%-*s", width, header);
2811 (void) printf("\n");
2815 * Given a pool and a list of properties, print out all the properties according
2816 * to the described layout.
2819 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2821 zprop_list_t *pl = cb->cb_proplist;
2822 boolean_t first = B_TRUE;
2823 char property[ZPOOL_MAXPROPLEN];
2825 boolean_t right_justify;
2828 for (; pl != NULL; pl = pl->pl_next) {
2830 width = pl->pl_width;
2831 if (first && cb->cb_verbose) {
2833 * Reset the width to accommodate the verbose listing
2836 width = cb->cb_namewidth;
2840 if (cb->cb_scripted)
2841 (void) printf("\t");
2848 right_justify = B_FALSE;
2849 if (pl->pl_prop != ZPROP_INVAL) {
2850 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2851 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2853 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2854 sizeof (property), NULL) != 0)
2859 right_justify = zpool_prop_align_right(pl->pl_prop);
2860 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2861 zpool_prop_unsupported(pl->pl_user_prop)) &&
2862 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2863 sizeof (property)) == 0) {
2871 * If this is being called in scripted mode, or if this is the
2872 * last column and it is left-justified, don't include a width
2875 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2876 (void) printf("%s", propstr);
2877 else if (right_justify)
2878 (void) printf("%*s", width, propstr);
2880 (void) printf("%-*s", width, propstr);
2883 (void) printf("\n");
2887 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2891 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2893 zfs_nicenum(value, propval, sizeof (propval));
2895 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2896 (void) strlcpy(propval, "-", sizeof (propval));
2899 (void) printf("\t%s", propval);
2901 (void) printf(" %*s", width, propval);
2905 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2906 list_cbdata_t *cb, int depth)
2912 boolean_t scripted = cb->cb_scripted;
2914 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2915 (uint64_t **)&vs, &c) == 0);
2919 (void) printf("\t%s", name);
2920 else if (strlen(name) + depth > cb->cb_namewidth)
2921 (void) printf("%*s%s", depth, "", name);
2923 (void) printf("%*s%s%*s", depth, "", name,
2924 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2926 /* only toplevel vdevs have capacity stats */
2927 if (vs->vs_space == 0) {
2929 (void) printf("\t-\t-\t-");
2931 (void) printf(" - - -");
2933 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2935 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2937 print_one_column(ZPOOL_PROP_FREE,
2938 vs->vs_space - vs->vs_alloc, scripted);
2940 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2942 (void) printf("\n");
2945 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2946 &child, &children) != 0)
2949 for (c = 0; c < children; c++) {
2950 uint64_t ishole = B_FALSE;
2952 if (nvlist_lookup_uint64(child[c],
2953 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2956 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2957 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2962 * Include level 2 ARC devices in iostat output
2964 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2965 &child, &children) != 0)
2969 (void) printf("%-*s - - - - - "
2970 "-\n", cb->cb_namewidth, "cache");
2971 for (c = 0; c < children; c++) {
2972 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2974 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2982 * Generic callback function to list a pool.
2985 list_callback(zpool_handle_t *zhp, void *data)
2987 list_cbdata_t *cbp = data;
2991 config = zpool_get_config(zhp, NULL);
2993 print_pool(zhp, cbp);
2994 if (!cbp->cb_verbose)
2997 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2999 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3005 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3007 * -H Scripted mode. Don't display headers, and separate properties
3009 * -o List of properties to display. Defaults to
3010 * "name,size,allocated,free,capacity,health,altroot"
3011 * -T Display a timestamp in date(1) or Unix format
3013 * List all pools in the system, whether or not they're healthy. Output space
3014 * statistics for each one, as well as health status summary.
3017 zpool_do_list(int argc, char **argv)
3021 list_cbdata_t cb = { 0 };
3022 static char default_props[] =
3023 "name,size,allocated,free,capacity,dedupratio,"
3025 char *props = default_props;
3026 unsigned long interval = 0, count = 0;
3028 boolean_t first = B_TRUE;
3031 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3034 cb.cb_scripted = B_TRUE;
3040 get_timestamp_arg(*optarg);
3043 cb.cb_verbose = B_TRUE;
3046 (void) fprintf(stderr, gettext("missing argument for "
3047 "'%c' option\n"), optopt);
3051 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3060 get_interval_count(&argc, argv, &interval, &count);
3062 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3065 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3068 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3069 (void) printf(gettext("no pools available\n"));
3070 zprop_free_list(cb.cb_proplist);
3075 pool_list_update(list);
3077 if (pool_list_count(list) == 0)
3080 cb.cb_namewidth = 0;
3081 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3083 if (timestamp_fmt != NODATE)
3084 print_timestamp(timestamp_fmt);
3086 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3090 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3095 if (count != 0 && --count == 0)
3098 (void) sleep(interval);
3101 zprop_free_list(cb.cb_proplist);
3106 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3113 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3114 &child, &children) != 0) {
3115 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3116 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3117 name += sizeof(_PATH_DEV) - 1;
3118 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3119 path += sizeof(_PATH_DEV) - 1;
3120 if (strcmp(name, path) == 0)
3125 for (c = 0; c < children; c++)
3126 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3133 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3135 boolean_t force = B_FALSE;
3138 char *poolname, *old_disk, *new_disk;
3139 zpool_handle_t *zhp;
3143 while ((c = getopt(argc, argv, "f")) != -1) {
3149 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3158 /* get pool name and check number of arguments */
3160 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3167 (void) fprintf(stderr,
3168 gettext("missing <device> specification\n"));
3176 (void) fprintf(stderr,
3177 gettext("missing <new_device> specification\n"));
3180 new_disk = old_disk;
3190 (void) fprintf(stderr, gettext("too many arguments\n"));
3194 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3197 if (zpool_get_config(zhp, NULL) == NULL) {
3198 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3204 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3206 if (nvroot == NULL) {
3211 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3213 nvlist_free(nvroot);
3220 * zpool replace [-f] <pool> <device> <new_device>
3222 * -f Force attach, even if <new_device> appears to be in use.
3224 * Replace <device> with <new_device>.
3228 zpool_do_replace(int argc, char **argv)
3230 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3234 * zpool attach [-f] <pool> <device> <new_device>
3236 * -f Force attach, even if <new_device> appears to be in use.
3238 * Attach <new_device> to the mirror containing <device>. If <device> is not
3239 * part of a mirror, then <device> will be transformed into a mirror of
3240 * <device> and <new_device>. In either case, <new_device> will begin life
3241 * with a DTL of [0, now], and will immediately begin to resilver itself.
3244 zpool_do_attach(int argc, char **argv)
3246 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3250 * zpool detach [-f] <pool> <device>
3252 * -f Force detach of <device>, even if DTLs argue against it
3253 * (not supported yet)
3255 * Detach a device from a mirror. The operation will be refused if <device>
3256 * is the last device in the mirror, or if the DTLs indicate that this device
3257 * has the only valid copy of some data.
3261 zpool_do_detach(int argc, char **argv)
3264 char *poolname, *path;
3265 zpool_handle_t *zhp;
3269 while ((c = getopt(argc, argv, "f")) != -1) {
3273 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3282 /* get pool name and check number of arguments */
3284 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3289 (void) fprintf(stderr,
3290 gettext("missing <device> specification\n"));
3297 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3300 ret = zpool_vdev_detach(zhp, path);
3308 * zpool split [-n] [-o prop=val] ...
3310 * [-R altroot] <pool> <newpool> [<device> ...]
3312 * -n Do not split the pool, but display the resulting layout if
3313 * it were to be split.
3314 * -o Set property=value, or set mount options.
3315 * -R Mount the split-off pool under an alternate root.
3317 * Splits the named pool and gives it the new pool name. Devices to be split
3318 * off may be listed, provided that no more than one device is specified
3319 * per top-level vdev mirror. The newly split pool is left in an exported
3320 * state unless -R is specified.
3322 * Restrictions: the top-level of the pool pool must only be made up of
3323 * mirrors; all devices in the pool must be healthy; no device may be
3324 * undergoing a resilvering operation.
3327 zpool_do_split(int argc, char **argv)
3329 char *srcpool, *newpool, *propval;
3330 char *mntopts = NULL;
3333 zpool_handle_t *zhp;
3334 nvlist_t *config, *props = NULL;
3336 flags.dryrun = B_FALSE;
3337 flags.import = B_FALSE;
3340 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3343 flags.import = B_TRUE;
3345 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3346 &props, B_TRUE) != 0) {
3353 flags.dryrun = B_TRUE;
3356 if ((propval = strchr(optarg, '=')) != NULL) {
3359 if (add_prop_list(optarg, propval,
3360 &props, B_TRUE) != 0) {
3370 (void) fprintf(stderr, gettext("missing argument for "
3371 "'%c' option\n"), optopt);
3375 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3382 if (!flags.import && mntopts != NULL) {
3383 (void) fprintf(stderr, gettext("setting mntopts is only "
3384 "valid when importing the pool\n"));
3392 (void) fprintf(stderr, gettext("Missing pool name\n"));
3396 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3406 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3409 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3410 if (config == NULL) {
3414 (void) printf(gettext("would create '%s' with the "
3415 "following layout:\n\n"), newpool);
3416 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3418 nvlist_free(config);
3423 if (ret != 0 || flags.dryrun || !flags.import)
3427 * The split was successful. Now we need to open the new
3428 * pool and import it.
3430 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3432 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3433 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3435 (void) fprintf(stderr, gettext("Split was successful, but "
3436 "the datasets could not all be mounted\n"));
3437 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3438 "different altroot\n"), "zpool import");
3448 * zpool online <pool> <device> ...
3451 zpool_do_online(int argc, char **argv)
3455 zpool_handle_t *zhp;
3457 vdev_state_t newstate;
3461 while ((c = getopt(argc, argv, "et")) != -1) {
3464 flags |= ZFS_ONLINE_EXPAND;
3468 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3477 /* get pool name and check number of arguments */
3479 (void) fprintf(stderr, gettext("missing pool name\n"));
3483 (void) fprintf(stderr, gettext("missing device name\n"));
3489 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3492 for (i = 1; i < argc; i++) {
3493 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3494 if (newstate != VDEV_STATE_HEALTHY) {
3495 (void) printf(gettext("warning: device '%s' "
3496 "onlined, but remains in faulted state\n"),
3498 if (newstate == VDEV_STATE_FAULTED)
3499 (void) printf(gettext("use 'zpool "
3500 "clear' to restore a faulted "
3503 (void) printf(gettext("use 'zpool "
3504 "replace' to replace devices "
3505 "that are no longer present\n"));
3518 * zpool offline [-ft] <pool> <device> ...
3520 * -f Force the device into the offline state, even if doing
3521 * so would appear to compromise pool availability.
3522 * (not supported yet)
3524 * -t Only take the device off-line temporarily. The offline
3525 * state will not be persistent across reboots.
3529 zpool_do_offline(int argc, char **argv)
3533 zpool_handle_t *zhp;
3535 boolean_t istmp = B_FALSE;
3538 while ((c = getopt(argc, argv, "ft")) != -1) {
3545 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3554 /* get pool name and check number of arguments */
3556 (void) fprintf(stderr, gettext("missing pool name\n"));
3560 (void) fprintf(stderr, gettext("missing device name\n"));
3566 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3569 for (i = 1; i < argc; i++) {
3570 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3580 * zpool clear <pool> [device]
3582 * Clear all errors associated with a pool or a particular device.
3585 zpool_do_clear(int argc, char **argv)
3589 boolean_t dryrun = B_FALSE;
3590 boolean_t do_rewind = B_FALSE;
3591 boolean_t xtreme_rewind = B_FALSE;
3592 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3593 nvlist_t *policy = NULL;
3594 zpool_handle_t *zhp;
3595 char *pool, *device;
3598 while ((c = getopt(argc, argv, "FnX")) != -1) {
3607 xtreme_rewind = B_TRUE;
3610 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3620 (void) fprintf(stderr, gettext("missing pool name\n"));
3625 (void) fprintf(stderr, gettext("too many arguments\n"));
3629 if ((dryrun || xtreme_rewind) && !do_rewind) {
3630 (void) fprintf(stderr,
3631 gettext("-n or -X only meaningful with -F\n"));
3635 rewind_policy = ZPOOL_TRY_REWIND;
3637 rewind_policy = ZPOOL_DO_REWIND;
3639 rewind_policy |= ZPOOL_EXTREME_REWIND;
3641 /* In future, further rewind policy choices can be passed along here */
3642 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3643 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3647 device = argc == 2 ? argv[1] : NULL;
3649 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3650 nvlist_free(policy);
3654 if (zpool_clear(zhp, device, policy) != 0)
3659 nvlist_free(policy);
3665 * zpool reguid <pool>
3668 zpool_do_reguid(int argc, char **argv)
3672 zpool_handle_t *zhp;
3676 while ((c = getopt(argc, argv, "")) != -1) {
3679 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3688 /* get pool name and check number of arguments */
3690 (void) fprintf(stderr, gettext("missing pool name\n"));
3695 (void) fprintf(stderr, gettext("too many arguments\n"));
3700 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3703 ret = zpool_reguid(zhp);
3711 * zpool reopen <pool>
3713 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3715 * NOTE: This command is currently undocumented. If the command is ever
3716 * exposed then the appropriate usage() messages will need to be made.
3719 zpool_do_reopen(int argc, char **argv)
3722 zpool_handle_t *zhp;
3732 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3735 ret = zpool_reopen(zhp);
3740 typedef struct scrub_cbdata {
3747 scrub_callback(zpool_handle_t *zhp, void *data)
3749 scrub_cbdata_t *cb = data;
3753 * Ignore faulted pools.
3755 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3756 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3757 "currently unavailable\n"), zpool_get_name(zhp));
3761 err = zpool_scan(zhp, cb->cb_type);
3767 * zpool scrub [-s] <pool> ...
3769 * -s Stop. Stops any in-progress scrub.
3772 zpool_do_scrub(int argc, char **argv)
3777 cb.cb_type = POOL_SCAN_SCRUB;
3780 while ((c = getopt(argc, argv, "s")) != -1) {
3783 cb.cb_type = POOL_SCAN_NONE;
3786 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3798 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3802 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3805 typedef struct status_cbdata {
3807 boolean_t cb_allpools;
3808 boolean_t cb_verbose;
3809 boolean_t cb_explain;
3811 boolean_t cb_dedup_stats;
3815 * Print out detailed scrub status.
3818 print_scan_status(pool_scan_stat_t *ps)
3821 uint64_t elapsed, mins_left, hours_left;
3822 uint64_t pass_exam, examined, total;
3824 double fraction_done;
3825 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3827 (void) printf(gettext(" scan: "));
3829 /* If there's never been a scan, there's not much to say. */
3830 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3831 ps->pss_func >= POOL_SCAN_FUNCS) {
3832 (void) printf(gettext("none requested\n"));
3836 start = ps->pss_start_time;
3837 end = ps->pss_end_time;
3838 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3840 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3841 ps->pss_func == POOL_SCAN_RESILVER);
3843 * Scan is finished or canceled.
3845 if (ps->pss_state == DSS_FINISHED) {
3846 uint64_t minutes_taken = (end - start) / 60;
3849 if (ps->pss_func == POOL_SCAN_SCRUB) {
3850 fmt = gettext("scrub repaired %s in %lluh%um with "
3851 "%llu errors on %s");
3852 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3853 fmt = gettext("resilvered %s in %lluh%um with "
3854 "%llu errors on %s");
3857 (void) printf(fmt, processed_buf,
3858 (u_longlong_t)(minutes_taken / 60),
3859 (uint_t)(minutes_taken % 60),
3860 (u_longlong_t)ps->pss_errors,
3861 ctime((time_t *)&end));
3863 } else if (ps->pss_state == DSS_CANCELED) {
3864 if (ps->pss_func == POOL_SCAN_SCRUB) {
3865 (void) printf(gettext("scrub canceled on %s"),
3867 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3868 (void) printf(gettext("resilver canceled on %s"),
3874 assert(ps->pss_state == DSS_SCANNING);
3877 * Scan is in progress.
3879 if (ps->pss_func == POOL_SCAN_SCRUB) {
3880 (void) printf(gettext("scrub in progress since %s"),
3882 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3883 (void) printf(gettext("resilver in progress since %s"),
3887 examined = ps->pss_examined ? ps->pss_examined : 1;
3888 total = ps->pss_to_examine;
3889 fraction_done = (double)examined / total;
3891 /* elapsed time for this pass */
3892 elapsed = time(NULL) - ps->pss_pass_start;
3893 elapsed = elapsed ? elapsed : 1;
3894 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3895 rate = pass_exam / elapsed;
3896 rate = rate ? rate : 1;
3897 mins_left = ((total - examined) / rate) / 60;
3898 hours_left = mins_left / 60;
3900 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3901 zfs_nicenum(total, total_buf, sizeof (total_buf));
3902 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3905 * do not print estimated time if hours_left is more than 30 days
3907 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3908 examined_buf, total_buf, rate_buf);
3909 if (hours_left < (30 * 24)) {
3910 (void) printf(gettext(", %lluh%um to go\n"),
3911 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3913 (void) printf(gettext(
3914 ", (scan is slow, no estimated time)\n"));
3917 if (ps->pss_func == POOL_SCAN_RESILVER) {
3918 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3919 processed_buf, 100 * fraction_done);
3920 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3921 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3922 processed_buf, 100 * fraction_done);
3927 print_error_log(zpool_handle_t *zhp)
3929 nvlist_t *nverrlist = NULL;
3932 size_t len = MAXPATHLEN * 2;
3934 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3935 (void) printf("errors: List of errors unavailable "
3936 "(insufficient privileges)\n");
3940 (void) printf("errors: Permanent errors have been "
3941 "detected in the following files:\n\n");
3943 pathname = safe_malloc(len);
3945 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3947 uint64_t dsobj, obj;
3949 verify(nvpair_value_nvlist(elem, &nv) == 0);
3950 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3952 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3954 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3955 (void) printf("%7s %s\n", "", pathname);
3958 nvlist_free(nverrlist);
3962 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3971 (void) printf(gettext("\tspares\n"));
3973 for (i = 0; i < nspares; i++) {
3974 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3975 print_status_config(zhp, name, spares[i],
3976 namewidth, 2, B_TRUE);
3982 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3991 (void) printf(gettext("\tcache\n"));
3993 for (i = 0; i < nl2cache; i++) {
3994 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3995 print_status_config(zhp, name, l2cache[i],
3996 namewidth, 2, B_FALSE);
4002 print_dedup_stats(nvlist_t *config)
4004 ddt_histogram_t *ddh;
4010 * If the pool was faulted then we may not have been able to
4011 * obtain the config. Otherwise, if we have anything in the dedup
4012 * table continue processing the stats.
4014 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4015 (uint64_t **)&ddo, &c) != 0)
4018 (void) printf("\n");
4019 (void) printf(gettext(" dedup: "));
4020 if (ddo->ddo_count == 0) {
4021 (void) printf(gettext("no DDT entries\n"));
4025 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4026 (u_longlong_t)ddo->ddo_count,
4027 (u_longlong_t)ddo->ddo_dspace,
4028 (u_longlong_t)ddo->ddo_mspace);
4030 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4031 (uint64_t **)&dds, &c) == 0);
4032 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4033 (uint64_t **)&ddh, &c) == 0);
4034 zpool_dump_ddt(dds, ddh);
4038 * Display a summary of pool status. Displays a summary such as:
4042 * reason: One or more devices ...
4043 * see: http://illumos.org/msg/ZFS-xxxx-01
4049 * When given the '-v' option, we print out the complete config. If the '-e'
4050 * option is specified, then we print out error rate information as well.
4053 status_callback(zpool_handle_t *zhp, void *data)
4055 status_cbdata_t *cbp = data;
4056 nvlist_t *config, *nvroot;
4063 config = zpool_get_config(zhp, NULL);
4064 reason = zpool_get_status(zhp, &msgid);
4069 * If we were given 'zpool status -x', only report those pools with
4072 if (cbp->cb_explain &&
4073 (reason == ZPOOL_STATUS_OK ||
4074 reason == ZPOOL_STATUS_VERSION_OLDER ||
4075 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4076 if (!cbp->cb_allpools) {
4077 (void) printf(gettext("pool '%s' is healthy\n"),
4078 zpool_get_name(zhp));
4080 cbp->cb_first = B_FALSE;
4086 cbp->cb_first = B_FALSE;
4088 (void) printf("\n");
4090 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4092 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4093 (uint64_t **)&vs, &c) == 0);
4094 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4096 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4097 (void) printf(gettext(" state: %s\n"), health);
4100 case ZPOOL_STATUS_MISSING_DEV_R:
4101 (void) printf(gettext("status: One or more devices could not "
4102 "be opened. Sufficient replicas exist for\n\tthe pool to "
4103 "continue functioning in a degraded state.\n"));
4104 (void) printf(gettext("action: Attach the missing device and "
4105 "online it using 'zpool online'.\n"));
4108 case ZPOOL_STATUS_MISSING_DEV_NR:
4109 (void) printf(gettext("status: One or more devices could not "
4110 "be opened. There are insufficient\n\treplicas for the "
4111 "pool to continue functioning.\n"));
4112 (void) printf(gettext("action: Attach the missing device and "
4113 "online it using 'zpool online'.\n"));
4116 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4117 (void) printf(gettext("status: One or more devices could not "
4118 "be used because the label is missing or\n\tinvalid. "
4119 "Sufficient replicas exist for the pool to continue\n\t"
4120 "functioning in a degraded state.\n"));
4121 (void) printf(gettext("action: Replace the device using "
4122 "'zpool replace'.\n"));
4125 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4126 (void) printf(gettext("status: One or more devices could not "
4127 "be used because the label is missing \n\tor invalid. "
4128 "There are insufficient replicas for the pool to "
4129 "continue\n\tfunctioning.\n"));
4130 zpool_explain_recover(zpool_get_handle(zhp),
4131 zpool_get_name(zhp), reason, config);
4134 case ZPOOL_STATUS_FAILING_DEV:
4135 (void) printf(gettext("status: One or more devices has "
4136 "experienced an unrecoverable error. An\n\tattempt was "
4137 "made to correct the error. Applications are "
4139 (void) printf(gettext("action: Determine if the device needs "
4140 "to be replaced, and clear the errors\n\tusing "
4141 "'zpool clear' or replace the device with 'zpool "
4145 case ZPOOL_STATUS_OFFLINE_DEV:
4146 (void) printf(gettext("status: One or more devices has "
4147 "been taken offline by the administrator.\n\tSufficient "
4148 "replicas exist for the pool to continue functioning in "
4149 "a\n\tdegraded state.\n"));
4150 (void) printf(gettext("action: Online the device using "
4151 "'zpool online' or replace the device with\n\t'zpool "
4155 case ZPOOL_STATUS_REMOVED_DEV:
4156 (void) printf(gettext("status: One or more devices has "
4157 "been removed by the administrator.\n\tSufficient "
4158 "replicas exist for the pool to continue functioning in "
4159 "a\n\tdegraded state.\n"));
4160 (void) printf(gettext("action: Online the device using "
4161 "'zpool online' or replace the device with\n\t'zpool "
4165 case ZPOOL_STATUS_RESILVERING:
4166 (void) printf(gettext("status: One or more devices is "
4167 "currently being resilvered. The pool will\n\tcontinue "
4168 "to function, possibly in a degraded state.\n"));
4169 (void) printf(gettext("action: Wait for the resilver to "
4173 case ZPOOL_STATUS_CORRUPT_DATA:
4174 (void) printf(gettext("status: One or more devices has "
4175 "experienced an error resulting in data\n\tcorruption. "
4176 "Applications may be affected.\n"));
4177 (void) printf(gettext("action: Restore the file in question "
4178 "if possible. Otherwise restore the\n\tentire pool from "
4182 case ZPOOL_STATUS_CORRUPT_POOL:
4183 (void) printf(gettext("status: The pool metadata is corrupted "
4184 "and the pool cannot be opened.\n"));
4185 zpool_explain_recover(zpool_get_handle(zhp),
4186 zpool_get_name(zhp), reason, config);
4189 case ZPOOL_STATUS_VERSION_OLDER:
4190 (void) printf(gettext("status: The pool is formatted using a "
4191 "legacy on-disk format. The pool can\n\tstill be used, "
4192 "but some features are unavailable.\n"));
4193 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4194 "upgrade'. Once this is done, the\n\tpool will no longer "
4195 "be accessible on software that does not support feature\n"
4199 case ZPOOL_STATUS_VERSION_NEWER:
4200 (void) printf(gettext("status: The pool has been upgraded to a "
4201 "newer, incompatible on-disk version.\n\tThe pool cannot "
4202 "be accessed on this system.\n"));
4203 (void) printf(gettext("action: Access the pool from a system "
4204 "running more recent software, or\n\trestore the pool from "
4208 case ZPOOL_STATUS_FEAT_DISABLED:
4209 (void) printf(gettext("status: Some supported features are not "
4210 "enabled on the pool. The pool can\n\tstill be used, but "
4211 "some features are unavailable.\n"));
4212 (void) printf(gettext("action: Enable all features using "
4213 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4214 "longer be accessible by software that does not support\n\t"
4215 "the features. See zpool-features(7) for details.\n"));
4218 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4219 (void) printf(gettext("status: The pool cannot be accessed on "
4220 "this system because it uses the\n\tfollowing feature(s) "
4221 "not supported on this system:\n"));
4222 zpool_print_unsup_feat(config);
4223 (void) printf("\n");
4224 (void) printf(gettext("action: Access the pool from a system "
4225 "that supports the required feature(s),\n\tor restore the "
4226 "pool from backup.\n"));
4229 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4230 (void) printf(gettext("status: The pool can only be accessed "
4231 "in read-only mode on this system. It\n\tcannot be "
4232 "accessed in read-write mode because it uses the "
4233 "following\n\tfeature(s) not supported on this system:\n"));
4234 zpool_print_unsup_feat(config);
4235 (void) printf("\n");
4236 (void) printf(gettext("action: The pool cannot be accessed in "
4237 "read-write mode. Import the pool with\n"
4238 "\t\"-o readonly=on\", access the pool from a system that "
4239 "supports the\n\trequired feature(s), or restore the "
4240 "pool from backup.\n"));
4243 case ZPOOL_STATUS_FAULTED_DEV_R:
4244 (void) printf(gettext("status: One or more devices are "
4245 "faulted in response to persistent errors.\n\tSufficient "
4246 "replicas exist for the pool to continue functioning "
4247 "in a\n\tdegraded state.\n"));
4248 (void) printf(gettext("action: Replace the faulted device, "
4249 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4252 case ZPOOL_STATUS_FAULTED_DEV_NR:
4253 (void) printf(gettext("status: One or more devices are "
4254 "faulted in response to persistent errors. There are "
4255 "insufficient replicas for the pool to\n\tcontinue "
4257 (void) printf(gettext("action: Destroy and re-create the pool "
4258 "from a backup source. Manually marking the device\n"
4259 "\trepaired using 'zpool clear' may allow some data "
4260 "to be recovered.\n"));
4263 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4264 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4265 (void) printf(gettext("status: One or more devices are "
4266 "faulted in response to IO failures.\n"));
4267 (void) printf(gettext("action: Make sure the affected devices "
4268 "are connected, then run 'zpool clear'.\n"));
4271 case ZPOOL_STATUS_BAD_LOG:
4272 (void) printf(gettext("status: An intent log record "
4273 "could not be read.\n"
4274 "\tWaiting for adminstrator intervention to fix the "
4275 "faulted pool.\n"));
4276 (void) printf(gettext("action: Either restore the affected "
4277 "device(s) and run 'zpool online',\n"
4278 "\tor ignore the intent log records by running "
4279 "'zpool clear'.\n"));
4282 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4283 (void) printf(gettext("status: One or more devices are "
4284 "configured to use a non-native block size.\n"
4285 "\tExpect reduced performance.\n"));
4286 (void) printf(gettext("action: Replace affected devices with "
4287 "devices that support the\n\tconfigured block size, or "
4288 "migrate data to a properly configured\n\tpool.\n"));
4293 * The remaining errors can't actually be generated, yet.
4295 assert(reason == ZPOOL_STATUS_OK);
4299 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4302 if (config != NULL) {
4305 nvlist_t **spares, **l2cache;
4306 uint_t nspares, nl2cache;
4307 pool_scan_stat_t *ps = NULL;
4309 (void) nvlist_lookup_uint64_array(nvroot,
4310 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4311 print_scan_status(ps);
4313 namewidth = max_width(zhp, nvroot, 0, 0);
4317 (void) printf(gettext("config:\n\n"));
4318 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4319 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4320 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4321 namewidth, 0, B_FALSE);
4323 if (num_logs(nvroot) > 0)
4324 print_logs(zhp, nvroot, namewidth, B_TRUE);
4325 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4326 &l2cache, &nl2cache) == 0)
4327 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4329 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4330 &spares, &nspares) == 0)
4331 print_spares(zhp, spares, nspares, namewidth);
4333 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4335 nvlist_t *nverrlist = NULL;
4338 * If the approximate error count is small, get a
4339 * precise count by fetching the entire log and
4340 * uniquifying the results.
4342 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4343 zpool_get_errlog(zhp, &nverrlist) == 0) {
4348 while ((elem = nvlist_next_nvpair(nverrlist,
4353 nvlist_free(nverrlist);
4355 (void) printf("\n");
4358 (void) printf(gettext("errors: No known data "
4360 else if (!cbp->cb_verbose)
4361 (void) printf(gettext("errors: %llu data "
4362 "errors, use '-v' for a list\n"),
4363 (u_longlong_t)nerr);
4365 print_error_log(zhp);
4368 if (cbp->cb_dedup_stats)
4369 print_dedup_stats(config);
4371 (void) printf(gettext("config: The configuration cannot be "
4379 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4381 * -v Display complete error logs
4382 * -x Display only pools with potential problems
4383 * -D Display dedup status (undocumented)
4384 * -T Display a timestamp in date(1) or Unix format
4386 * Describes the health status of all pools or some subset.
4389 zpool_do_status(int argc, char **argv)
4393 unsigned long interval = 0, count = 0;
4394 status_cbdata_t cb = { 0 };
4397 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4400 cb.cb_verbose = B_TRUE;
4403 cb.cb_explain = B_TRUE;
4406 cb.cb_dedup_stats = B_TRUE;
4409 get_timestamp_arg(*optarg);
4412 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4421 get_interval_count(&argc, argv, &interval, &count);
4424 cb.cb_allpools = B_TRUE;
4426 cb.cb_first = B_TRUE;
4429 if (timestamp_fmt != NODATE)
4430 print_timestamp(timestamp_fmt);
4432 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4433 status_callback, &cb);
4435 if (argc == 0 && cb.cb_count == 0)
4436 (void) printf(gettext("no pools available\n"));
4437 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4438 (void) printf(gettext("all pools are healthy\n"));
4446 if (count != 0 && --count == 0)
4449 (void) sleep(interval);
4455 typedef struct upgrade_cbdata {
4457 char cb_poolname[ZPOOL_MAXNAMELEN];
4459 uint64_t cb_version;
4465 is_root_pool(zpool_handle_t *zhp)
4467 static struct statfs sfs;
4468 static char *poolname = NULL;
4469 static boolean_t stated = B_FALSE;
4474 if (statfs("/", &sfs) == -1) {
4475 (void) fprintf(stderr,
4476 "Unable to stat root file system: %s.\n",
4480 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4482 poolname = sfs.f_mntfromname;
4483 if ((slash = strchr(poolname, '/')) != NULL)
4486 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4490 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4492 if (poolname[0] == '\0' && is_root_pool(zhp))
4493 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4495 #endif /* FreeBSD */
4498 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4502 uint64_t oldversion;
4504 config = zpool_get_config(zhp, NULL);
4505 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4508 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4509 assert(oldversion < version);
4511 ret = zpool_upgrade(zhp, version);
4515 if (version >= SPA_VERSION_FEATURES) {
4516 (void) printf(gettext("Successfully upgraded "
4517 "'%s' from version %llu to feature flags.\n"),
4518 zpool_get_name(zhp), oldversion);
4520 (void) printf(gettext("Successfully upgraded "
4521 "'%s' from version %llu to version %llu.\n"),
4522 zpool_get_name(zhp), oldversion, version);
4529 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4532 boolean_t firstff = B_TRUE;
4533 nvlist_t *enabled = zpool_get_features(zhp);
4536 for (i = 0; i < SPA_FEATURES; i++) {
4537 const char *fname = spa_feature_table[i].fi_uname;
4538 const char *fguid = spa_feature_table[i].fi_guid;
4539 if (!nvlist_exists(enabled, fguid)) {
4541 verify(-1 != asprintf(&propname, "feature@%s", fname));
4542 ret = zpool_set_prop(zhp, propname,
4543 ZFS_FEATURE_ENABLED);
4551 (void) printf(gettext("Enabled the "
4552 "following features on '%s':\n"),
4553 zpool_get_name(zhp));
4556 (void) printf(gettext(" %s\n"), fname);
4567 upgrade_cb(zpool_handle_t *zhp, void *arg)
4569 upgrade_cbdata_t *cbp = arg;
4572 boolean_t printnl = B_FALSE;
4575 config = zpool_get_config(zhp, NULL);
4576 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4579 assert(SPA_VERSION_IS_SUPPORTED(version));
4581 if (version < cbp->cb_version) {
4582 cbp->cb_first = B_FALSE;
4583 ret = upgrade_version(zhp, cbp->cb_version);
4587 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4588 sizeof(cbp->cb_poolname));
4589 #endif /* ___FreeBSD__ */
4594 * If they did "zpool upgrade -a", then we could
4595 * be doing ioctls to different pools. We need
4596 * to log this history once to each pool, and bypass
4597 * the normal history logging that happens in main().
4599 (void) zpool_log_history(g_zfs, history_str);
4600 log_history = B_FALSE;
4604 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4606 ret = upgrade_enable_all(zhp, &count);
4611 cbp->cb_first = B_FALSE;
4614 * If they did "zpool upgrade -a", then we could
4615 * be doing ioctls to different pools. We need
4616 * to log this history once to each pool, and bypass
4617 * the normal history logging that happens in main().
4619 (void) zpool_log_history(g_zfs, history_str);
4620 log_history = B_FALSE;
4625 (void) printf(gettext("\n"));
4632 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4634 upgrade_cbdata_t *cbp = arg;
4638 config = zpool_get_config(zhp, NULL);
4639 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4642 assert(SPA_VERSION_IS_SUPPORTED(version));
4644 if (version < SPA_VERSION_FEATURES) {
4645 if (cbp->cb_first) {
4646 (void) printf(gettext("The following pools are "
4647 "formatted with legacy version numbers and can\n"
4648 "be upgraded to use feature flags. After "
4649 "being upgraded, these pools\nwill no "
4650 "longer be accessible by software that does not "
4651 "support feature\nflags.\n\n"));
4652 (void) printf(gettext("VER POOL\n"));
4653 (void) printf(gettext("--- ------------\n"));
4654 cbp->cb_first = B_FALSE;
4657 (void) printf("%2llu %s\n", (u_longlong_t)version,
4658 zpool_get_name(zhp));
4665 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4667 upgrade_cbdata_t *cbp = arg;
4671 config = zpool_get_config(zhp, NULL);
4672 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4675 if (version >= SPA_VERSION_FEATURES) {
4677 boolean_t poolfirst = B_TRUE;
4678 nvlist_t *enabled = zpool_get_features(zhp);
4680 for (i = 0; i < SPA_FEATURES; i++) {
4681 const char *fguid = spa_feature_table[i].fi_guid;
4682 const char *fname = spa_feature_table[i].fi_uname;
4683 if (!nvlist_exists(enabled, fguid)) {
4684 if (cbp->cb_first) {
4685 (void) printf(gettext("\nSome "
4686 "supported features are not "
4687 "enabled on the following pools. "
4688 "Once a\nfeature is enabled the "
4689 "pool may become incompatible with "
4690 "software\nthat does not support "
4692 "zpool-features(7) for "
4694 (void) printf(gettext("POOL "
4696 (void) printf(gettext("------"
4698 cbp->cb_first = B_FALSE;
4702 (void) printf(gettext("%s\n"),
4703 zpool_get_name(zhp));
4704 poolfirst = B_FALSE;
4707 (void) printf(gettext(" %s\n"), fname);
4717 upgrade_one(zpool_handle_t *zhp, void *data)
4719 boolean_t printnl = B_FALSE;
4720 upgrade_cbdata_t *cbp = data;
4721 uint64_t cur_version;
4724 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4725 (void) printf(gettext("'log' is now a reserved word\n"
4726 "Pool 'log' must be renamed using export and import"
4731 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4732 if (cur_version > cbp->cb_version) {
4733 (void) printf(gettext("Pool '%s' is already formatted "
4734 "using more current version '%llu'.\n\n"),
4735 zpool_get_name(zhp), cur_version);
4739 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4740 (void) printf(gettext("Pool '%s' is already formatted "
4741 "using version %llu.\n\n"), zpool_get_name(zhp),
4746 if (cur_version != cbp->cb_version) {
4748 ret = upgrade_version(zhp, cbp->cb_version);
4752 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4753 sizeof(cbp->cb_poolname));
4754 #endif /* ___FreeBSD__ */
4757 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4759 ret = upgrade_enable_all(zhp, &count);
4766 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4767 sizeof(cbp->cb_poolname));
4768 #endif /* __FreeBSD __*/
4769 } else if (cur_version == SPA_VERSION) {
4770 (void) printf(gettext("Pool '%s' already has all "
4771 "supported features enabled.\n"),
4772 zpool_get_name(zhp));
4777 (void) printf(gettext("\n"));
4786 * zpool upgrade [-V version] <-a | pool ...>
4788 * With no arguments, display downrev'd ZFS pool available for upgrade.
4789 * Individual pools can be upgraded by specifying the pool, and '-a' will
4790 * upgrade all pools.
4793 zpool_do_upgrade(int argc, char **argv)
4796 upgrade_cbdata_t cb = { 0 };
4798 boolean_t showversions = B_FALSE;
4799 boolean_t upgradeall = B_FALSE;
4804 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4807 upgradeall = B_TRUE;
4810 showversions = B_TRUE;
4813 cb.cb_version = strtoll(optarg, &end, 10);
4815 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4816 (void) fprintf(stderr,
4817 gettext("invalid version '%s'\n"), optarg);
4822 (void) fprintf(stderr, gettext("missing argument for "
4823 "'%c' option\n"), optopt);
4827 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4838 if (cb.cb_version == 0) {
4839 cb.cb_version = SPA_VERSION;
4840 } else if (!upgradeall && argc == 0) {
4841 (void) fprintf(stderr, gettext("-V option is "
4842 "incompatible with other arguments\n"));
4847 if (upgradeall || argc != 0) {
4848 (void) fprintf(stderr, gettext("-v option is "
4849 "incompatible with other arguments\n"));
4852 } else if (upgradeall) {
4854 (void) fprintf(stderr, gettext("-a option should not "
4855 "be used along with a pool name\n"));
4860 (void) printf(gettext("This system supports ZFS pool feature "
4865 (void) printf(gettext("The following features are "
4867 (void) printf(gettext("FEAT DESCRIPTION\n"));
4868 (void) printf("----------------------------------------------"
4869 "---------------\n");
4870 for (i = 0; i < SPA_FEATURES; i++) {
4871 zfeature_info_t *fi = &spa_feature_table[i];
4872 const char *ro = fi->fi_can_readonly ?
4873 " (read-only compatible)" : "";
4875 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4876 (void) printf(" %s\n", fi->fi_desc);
4878 (void) printf("\n");
4880 (void) printf(gettext("The following legacy versions are also "
4882 (void) printf(gettext("VER DESCRIPTION\n"));
4883 (void) printf("--- -----------------------------------------"
4884 "---------------\n");
4885 (void) printf(gettext(" 1 Initial ZFS version\n"));
4886 (void) printf(gettext(" 2 Ditto blocks "
4887 "(replicated metadata)\n"));
4888 (void) printf(gettext(" 3 Hot spares and double parity "
4890 (void) printf(gettext(" 4 zpool history\n"));
4891 (void) printf(gettext(" 5 Compression using the gzip "
4893 (void) printf(gettext(" 6 bootfs pool property\n"));
4894 (void) printf(gettext(" 7 Separate intent log devices\n"));
4895 (void) printf(gettext(" 8 Delegated administration\n"));
4896 (void) printf(gettext(" 9 refquota and refreservation "
4898 (void) printf(gettext(" 10 Cache devices\n"));
4899 (void) printf(gettext(" 11 Improved scrub performance\n"));
4900 (void) printf(gettext(" 12 Snapshot properties\n"));
4901 (void) printf(gettext(" 13 snapused property\n"));
4902 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4903 (void) printf(gettext(" 15 user/group space accounting\n"));
4904 (void) printf(gettext(" 16 stmf property support\n"));
4905 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4906 (void) printf(gettext(" 18 Snapshot user holds\n"));
4907 (void) printf(gettext(" 19 Log device removal\n"));
4908 (void) printf(gettext(" 20 Compression using zle "
4909 "(zero-length encoding)\n"));
4910 (void) printf(gettext(" 21 Deduplication\n"));
4911 (void) printf(gettext(" 22 Received properties\n"));
4912 (void) printf(gettext(" 23 Slim ZIL\n"));
4913 (void) printf(gettext(" 24 System attributes\n"));
4914 (void) printf(gettext(" 25 Improved scrub stats\n"));
4915 (void) printf(gettext(" 26 Improved snapshot deletion "
4917 (void) printf(gettext(" 27 Improved snapshot creation "
4919 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4920 (void) printf(gettext("\nFor more information on a particular "
4921 "version, including supported releases,\n"));
4922 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4923 } else if (argc == 0 && upgradeall) {
4924 cb.cb_first = B_TRUE;
4925 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4926 if (ret == 0 && cb.cb_first) {
4927 if (cb.cb_version == SPA_VERSION) {
4928 (void) printf(gettext("All pools are already "
4929 "formatted using feature flags.\n\n"));
4930 (void) printf(gettext("Every feature flags "
4931 "pool already has all supported features "
4934 (void) printf(gettext("All pools are already "
4935 "formatted with version %llu or higher.\n"),
4939 } else if (argc == 0) {
4940 cb.cb_first = B_TRUE;
4941 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4945 (void) printf(gettext("All pools are formatted "
4946 "using feature flags.\n\n"));
4948 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4949 "for a list of available legacy versions.\n"));
4952 cb.cb_first = B_TRUE;
4953 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4957 (void) printf(gettext("Every feature flags pool has "
4958 "all supported features enabled.\n"));
4960 (void) printf(gettext("\n"));
4963 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4967 if (cb.cb_poolname[0] != '\0') {
4969 "If you boot from pool '%s', don't forget to update boot code.\n"
4970 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4971 "the following command will do it:\n"
4973 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4980 typedef struct hist_cbdata {
4987 * Print out the command history for a specific pool.
4990 get_history_one(zpool_handle_t *zhp, void *data)
4996 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4998 cb->first = B_FALSE;
5000 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5002 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5005 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5006 &records, &numrecords) == 0);
5007 for (i = 0; i < numrecords; i++) {
5008 nvlist_t *rec = records[i];
5011 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5015 tsec = fnvlist_lookup_uint64(records[i],
5017 (void) localtime_r(&tsec, &t);
5018 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5021 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5022 (void) printf("%s %s", tbuf,
5023 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5024 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5026 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5029 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5030 (void) printf("%s unrecognized record:\n",
5032 dump_nvlist(rec, 4);
5035 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5036 zfs_history_event_names[ievent],
5037 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5038 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5039 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5042 (void) printf("%s [txg:%lld] %s", tbuf,
5043 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5044 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5045 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5046 (void) printf(" %s (%llu)",
5047 fnvlist_lookup_string(rec,
5049 fnvlist_lookup_uint64(rec,
5052 (void) printf(" %s", fnvlist_lookup_string(rec,
5053 ZPOOL_HIST_INT_STR));
5054 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5057 (void) printf("%s ioctl %s\n", tbuf,
5058 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5059 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5060 (void) printf(" input:\n");
5061 dump_nvlist(fnvlist_lookup_nvlist(rec,
5062 ZPOOL_HIST_INPUT_NVL), 8);
5064 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5065 (void) printf(" output:\n");
5066 dump_nvlist(fnvlist_lookup_nvlist(rec,
5067 ZPOOL_HIST_OUTPUT_NVL), 8);
5072 (void) printf("%s unrecognized record:\n", tbuf);
5073 dump_nvlist(rec, 4);
5077 (void) printf("\n");
5080 (void) printf(" [");
5081 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5082 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5083 struct passwd *pwd = getpwuid(who);
5084 (void) printf("user %d ", (int)who);
5086 (void) printf("(%s) ", pwd->pw_name);
5088 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5089 (void) printf("on %s",
5090 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5092 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5093 (void) printf(":%s",
5094 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5097 (void) printf("\n");
5099 (void) printf("\n");
5106 * zpool history <pool>
5108 * Displays the history of commands that modified pools.
5111 zpool_do_history(int argc, char **argv)
5113 hist_cbdata_t cbdata = { 0 };
5117 cbdata.first = B_TRUE;
5119 while ((c = getopt(argc, argv, "li")) != -1) {
5122 cbdata.longfmt = B_TRUE;
5125 cbdata.internal = B_TRUE;
5128 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5136 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5139 if (argc == 0 && cbdata.first == B_TRUE) {
5140 (void) printf(gettext("no pools available\n"));
5148 get_callback(zpool_handle_t *zhp, void *data)
5150 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5151 char value[MAXNAMELEN];
5152 zprop_source_t srctype;
5155 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5158 * Skip the special fake placeholder. This will also skip
5159 * over the name property when 'all' is specified.
5161 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5162 pl == cbp->cb_proplist)
5165 if (pl->pl_prop == ZPROP_INVAL &&
5166 (zpool_prop_feature(pl->pl_user_prop) ||
5167 zpool_prop_unsupported(pl->pl_user_prop))) {
5168 srctype = ZPROP_SRC_LOCAL;
5170 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5171 value, sizeof (value)) == 0) {
5172 zprop_print_one_property(zpool_get_name(zhp),
5173 cbp, pl->pl_user_prop, value, srctype,
5177 if (zpool_get_prop(zhp, pl->pl_prop, value,
5178 sizeof (value), &srctype) != 0)
5181 zprop_print_one_property(zpool_get_name(zhp), cbp,
5182 zpool_prop_to_name(pl->pl_prop), value, srctype,
5190 zpool_do_get(int argc, char **argv)
5192 zprop_get_cbdata_t cb = { 0 };
5193 zprop_list_t fake_name = { 0 };
5197 (void) fprintf(stderr, gettext("missing property "
5202 cb.cb_first = B_TRUE;
5203 cb.cb_sources = ZPROP_SRC_ALL;
5204 cb.cb_columns[0] = GET_COL_NAME;
5205 cb.cb_columns[1] = GET_COL_PROPERTY;
5206 cb.cb_columns[2] = GET_COL_VALUE;
5207 cb.cb_columns[3] = GET_COL_SOURCE;
5208 cb.cb_type = ZFS_TYPE_POOL;
5210 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5211 ZFS_TYPE_POOL) != 0)
5214 if (cb.cb_proplist != NULL) {
5215 fake_name.pl_prop = ZPOOL_PROP_NAME;
5216 fake_name.pl_width = strlen(gettext("NAME"));
5217 fake_name.pl_next = cb.cb_proplist;
5218 cb.cb_proplist = &fake_name;
5221 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5224 if (cb.cb_proplist == &fake_name)
5225 zprop_free_list(fake_name.pl_next);
5227 zprop_free_list(cb.cb_proplist);
5232 typedef struct set_cbdata {
5235 boolean_t cb_any_successful;
5239 set_callback(zpool_handle_t *zhp, void *data)
5242 set_cbdata_t *cb = (set_cbdata_t *)data;
5244 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5247 cb->cb_any_successful = B_TRUE;
5253 zpool_do_set(int argc, char **argv)
5255 set_cbdata_t cb = { 0 };
5258 if (argc > 1 && argv[1][0] == '-') {
5259 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5265 (void) fprintf(stderr, gettext("missing property=value "
5271 (void) fprintf(stderr, gettext("missing pool name\n"));
5276 (void) fprintf(stderr, gettext("too many pool names\n"));
5280 cb.cb_propname = argv[1];
5281 cb.cb_value = strchr(cb.cb_propname, '=');
5282 if (cb.cb_value == NULL) {
5283 (void) fprintf(stderr, gettext("missing value in "
5284 "property=value argument\n"));
5288 *(cb.cb_value) = '\0';
5291 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5298 find_command_idx(char *command, int *idx)
5302 for (i = 0; i < NCOMMAND; i++) {
5303 if (command_table[i].name == NULL)
5306 if (strcmp(command, command_table[i].name) == 0) {
5315 main(int argc, char **argv)
5321 (void) setlocale(LC_ALL, "");
5322 (void) textdomain(TEXT_DOMAIN);
5324 if ((g_zfs = libzfs_init()) == NULL) {
5325 (void) fprintf(stderr, gettext("internal error: failed to "
5326 "initialize ZFS library\n"));
5330 libzfs_print_on_error(g_zfs, B_TRUE);
5335 * Make sure the user has specified some command.
5338 (void) fprintf(stderr, gettext("missing command\n"));
5347 if (strcmp(cmdname, "-?") == 0)
5350 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5353 * Run the appropriate command.
5355 if (find_command_idx(cmdname, &i) == 0) {
5356 current_command = &command_table[i];
5357 ret = command_table[i].func(argc - 1, argv + 1);
5358 } else if (strchr(cmdname, '=')) {
5359 verify(find_command_idx("set", &i) == 0);
5360 current_command = &command_table[i];
5361 ret = command_table[i].func(argc, argv);
5362 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5364 * 'freeze' is a vile debugging abomination, so we treat
5367 zfs_cmd_t zc = { 0 };
5368 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5369 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5371 (void) fprintf(stderr, gettext("unrecognized "
5372 "command '%s'\n"), cmdname);
5376 if (ret == 0 && log_history)
5377 (void) zpool_log_history(g_zfs, history_str);
5382 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5383 * for the purposes of running ::findleaks.
5385 if (getenv("ZFS_ABORT") != NULL) {
5386 (void) printf("dumping core by request\n");