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) 2013 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 (gettext("\treopen <pool>\n"));
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.
921 if (mountpoint == NULL ||
922 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
923 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
924 char buf[MAXPATHLEN];
927 if (mountpoint && mountpoint[0] != '/') {
928 (void) fprintf(stderr, gettext("invalid mountpoint "
929 "'%s': must be an absolute path, 'legacy', or "
930 "'none'\n"), mountpoint);
934 if (mountpoint == NULL) {
936 (void) snprintf(buf, sizeof (buf), "%s/%s",
939 (void) snprintf(buf, sizeof (buf), "/%s",
943 (void) snprintf(buf, sizeof (buf), "%s%s",
944 altroot, mountpoint);
946 (void) snprintf(buf, sizeof (buf), "%s",
950 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
951 (void) fprintf(stderr, gettext("mountpoint '%s' : "
952 "%s\n"), buf, strerror(errno));
953 (void) fprintf(stderr, gettext("use '-m' "
954 "option to provide a different default\n"));
959 while (count < 3 && readdir(dirp) != NULL)
961 (void) closedir(dirp);
964 (void) fprintf(stderr, gettext("mountpoint "
965 "'%s' exists and is not empty\n"), buf);
966 (void) fprintf(stderr, gettext("use '-m' "
967 "option to provide a "
968 "different default\n"));
975 * Now that the mountpoint's validity has been checked, ensure that
976 * the property is set appropriately prior to creating the pool.
978 if (mountpoint != NULL) {
979 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
980 mountpoint, &fsprops, B_FALSE);
988 * For a dry run invocation, print out a basic message and run
989 * through all the vdevs in the list and print out in an
990 * appropriate hierarchy.
992 (void) printf(gettext("would create '%s' with the "
993 "following layout:\n\n"), poolname);
995 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
996 if (num_logs(nvroot) > 0)
997 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1002 * Hand off to libzfs.
1004 if (enable_all_pool_feat) {
1006 for (i = 0; i < SPA_FEATURES; i++) {
1007 char propname[MAXPATHLEN];
1008 zfeature_info_t *feat = &spa_feature_table[i];
1010 (void) snprintf(propname, sizeof (propname),
1011 "feature@%s", feat->fi_uname);
1014 * Skip feature if user specified it manually
1015 * on the command line.
1017 if (nvlist_exists(props, propname))
1020 ret = add_prop_list(propname,
1021 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1028 if (zpool_create(g_zfs, poolname,
1029 nvroot, props, fsprops) == 0) {
1030 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1031 ZFS_TYPE_FILESYSTEM);
1033 if (zfs_mount(pool, NULL, 0) == 0)
1034 ret = zfs_shareall(pool);
1037 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1038 (void) fprintf(stderr, gettext("pool name may have "
1044 nvlist_free(nvroot);
1045 nvlist_free(fsprops);
1049 nvlist_free(fsprops);
1056 * zpool destroy <pool>
1058 * -f Forcefully unmount any datasets
1060 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1063 zpool_do_destroy(int argc, char **argv)
1065 boolean_t force = B_FALSE;
1068 zpool_handle_t *zhp;
1072 while ((c = getopt(argc, argv, "f")) != -1) {
1078 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1087 /* check arguments */
1089 (void) fprintf(stderr, gettext("missing pool argument\n"));
1093 (void) fprintf(stderr, gettext("too many arguments\n"));
1099 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1101 * As a special case, check for use of '/' in the name, and
1102 * direct the user to use 'zfs destroy' instead.
1104 if (strchr(pool, '/') != NULL)
1105 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1106 "destroy a dataset\n"));
1110 if (zpool_disable_datasets(zhp, force) != 0) {
1111 (void) fprintf(stderr, gettext("could not destroy '%s': "
1112 "could not unmount datasets\n"), zpool_get_name(zhp));
1116 /* The history must be logged as part of the export */
1117 log_history = B_FALSE;
1119 ret = (zpool_destroy(zhp, history_str) != 0);
1127 * zpool export [-f] <pool> ...
1129 * -f Forcefully unmount datasets
1131 * Export the given pools. By default, the command will attempt to cleanly
1132 * unmount any active datasets within the pool. If the '-f' flag is specified,
1133 * then the datasets will be forcefully unmounted.
1136 zpool_do_export(int argc, char **argv)
1138 boolean_t force = B_FALSE;
1139 boolean_t hardforce = B_FALSE;
1141 zpool_handle_t *zhp;
1146 while ((c = getopt(argc, argv, "fF")) != -1) {
1155 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1164 /* check arguments */
1166 (void) fprintf(stderr, gettext("missing pool argument\n"));
1171 for (i = 0; i < argc; i++) {
1172 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1177 if (zpool_disable_datasets(zhp, force) != 0) {
1183 /* The history must be logged as part of the export */
1184 log_history = B_FALSE;
1187 if (zpool_export_force(zhp, history_str) != 0)
1189 } else if (zpool_export(zhp, force, history_str) != 0) {
1200 * Given a vdev configuration, determine the maximum width needed for the device
1204 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1206 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1211 if (strlen(name) + depth > max)
1212 max = strlen(name) + depth;
1216 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1217 &child, &children) == 0) {
1218 for (c = 0; c < children; c++)
1219 if ((ret = max_width(zhp, child[c], depth + 2,
1224 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1225 &child, &children) == 0) {
1226 for (c = 0; c < children; c++)
1227 if ((ret = max_width(zhp, child[c], depth + 2,
1232 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1233 &child, &children) == 0) {
1234 for (c = 0; c < children; c++)
1235 if ((ret = max_width(zhp, child[c], depth + 2,
1244 typedef struct spare_cbdata {
1246 zpool_handle_t *cb_zhp;
1250 find_vdev(nvlist_t *nv, uint64_t search)
1256 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1260 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1261 &child, &children) == 0) {
1262 for (c = 0; c < children; c++)
1263 if (find_vdev(child[c], search))
1271 find_spare(zpool_handle_t *zhp, void *data)
1273 spare_cbdata_t *cbp = data;
1274 nvlist_t *config, *nvroot;
1276 config = zpool_get_config(zhp, NULL);
1277 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1280 if (find_vdev(nvroot, cbp->cb_guid)) {
1290 * Print out configuration state as requested by status_callback.
1293 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1294 int namewidth, int depth, boolean_t isspare)
1297 uint_t c, vsc, children;
1298 pool_scan_stat_t *ps = NULL;
1300 char rbuf[6], wbuf[6], cbuf[6];
1302 uint64_t notpresent;
1307 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1308 &child, &children) != 0)
1311 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1312 (uint64_t **)&vs, &vsc) == 0);
1314 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1317 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1320 if (vs->vs_aux == VDEV_AUX_SPARED)
1322 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1326 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1330 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1331 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1332 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1333 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1336 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1337 ¬present) == 0 ||
1338 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1340 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1341 (void) printf(" was %s", path);
1342 } else if (vs->vs_aux != 0) {
1345 switch (vs->vs_aux) {
1346 case VDEV_AUX_OPEN_FAILED:
1347 (void) printf(gettext("cannot open"));
1350 case VDEV_AUX_BAD_GUID_SUM:
1351 (void) printf(gettext("missing device"));
1354 case VDEV_AUX_NO_REPLICAS:
1355 (void) printf(gettext("insufficient replicas"));
1358 case VDEV_AUX_VERSION_NEWER:
1359 (void) printf(gettext("newer version"));
1362 case VDEV_AUX_UNSUP_FEAT:
1363 (void) printf(gettext("unsupported feature(s)"));
1366 case VDEV_AUX_ASHIFT_TOO_BIG:
1367 (void) printf(gettext("unsupported minimum blocksize"));
1370 case VDEV_AUX_SPARED:
1371 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1373 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1374 if (strcmp(zpool_get_name(cb.cb_zhp),
1375 zpool_get_name(zhp)) == 0)
1376 (void) printf(gettext("currently in "
1379 (void) printf(gettext("in use by "
1381 zpool_get_name(cb.cb_zhp));
1382 zpool_close(cb.cb_zhp);
1384 (void) printf(gettext("currently in use"));
1388 case VDEV_AUX_ERR_EXCEEDED:
1389 (void) printf(gettext("too many errors"));
1392 case VDEV_AUX_IO_FAILURE:
1393 (void) printf(gettext("experienced I/O failures"));
1396 case VDEV_AUX_BAD_LOG:
1397 (void) printf(gettext("bad intent log"));
1400 case VDEV_AUX_EXTERNAL:
1401 (void) printf(gettext("external device fault"));
1404 case VDEV_AUX_SPLIT_POOL:
1405 (void) printf(gettext("split into new pool"));
1409 (void) printf(gettext("corrupted data"));
1412 } else if (children == 0 && !isspare &&
1413 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1414 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1416 gettext(" block size: %dB configured, %dB native"),
1417 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1420 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1421 (uint64_t **)&ps, &c);
1423 if (ps && ps->pss_state == DSS_SCANNING &&
1424 vs->vs_scan_processed != 0 && children == 0) {
1425 (void) printf(gettext(" (%s)"),
1426 (ps->pss_func == POOL_SCAN_RESILVER) ?
1427 "resilvering" : "repairing");
1430 (void) printf("\n");
1432 for (c = 0; c < children; c++) {
1433 uint64_t islog = B_FALSE, ishole = B_FALSE;
1435 /* Don't print logs or holes here */
1436 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1438 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1440 if (islog || ishole)
1442 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1443 print_status_config(zhp, vname, child[c],
1444 namewidth, depth + 2, isspare);
1451 * Print the configuration of an exported pool. Iterate over all vdevs in the
1452 * pool, printing out the name and status for each one.
1455 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1462 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1463 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1464 strcmp(type, VDEV_TYPE_HOLE) == 0)
1467 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1468 (uint64_t **)&vs, &c) == 0);
1470 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1471 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1473 if (vs->vs_aux != 0) {
1476 switch (vs->vs_aux) {
1477 case VDEV_AUX_OPEN_FAILED:
1478 (void) printf(gettext("cannot open"));
1481 case VDEV_AUX_BAD_GUID_SUM:
1482 (void) printf(gettext("missing device"));
1485 case VDEV_AUX_NO_REPLICAS:
1486 (void) printf(gettext("insufficient replicas"));
1489 case VDEV_AUX_VERSION_NEWER:
1490 (void) printf(gettext("newer version"));
1493 case VDEV_AUX_UNSUP_FEAT:
1494 (void) printf(gettext("unsupported feature(s)"));
1497 case VDEV_AUX_ERR_EXCEEDED:
1498 (void) printf(gettext("too many errors"));
1502 (void) printf(gettext("corrupted data"));
1506 (void) printf("\n");
1508 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1509 &child, &children) != 0)
1512 for (c = 0; c < children; c++) {
1513 uint64_t is_log = B_FALSE;
1515 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1520 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1521 print_import_config(vname, child[c], namewidth, depth + 2);
1525 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1526 &child, &children) == 0) {
1527 (void) printf(gettext("\tcache\n"));
1528 for (c = 0; c < children; c++) {
1529 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1530 (void) printf("\t %s\n", vname);
1535 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1536 &child, &children) == 0) {
1537 (void) printf(gettext("\tspares\n"));
1538 for (c = 0; c < children; c++) {
1539 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1540 (void) printf("\t %s\n", vname);
1548 * Logs are recorded as top level vdevs in the main pool child array
1549 * but with "is_log" set to 1. We use either print_status_config() or
1550 * print_import_config() to print the top level logs then any log
1551 * children (eg mirrored slogs) are printed recursively - which
1552 * works because only the top level vdev is marked "is_log"
1555 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1560 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1564 (void) printf(gettext("\tlogs\n"));
1566 for (c = 0; c < children; c++) {
1567 uint64_t is_log = B_FALSE;
1570 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1574 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1576 print_status_config(zhp, name, child[c], namewidth,
1579 print_import_config(name, child[c], namewidth, 2);
1585 * Display the status for the given pool.
1588 show_import(nvlist_t *config)
1590 uint64_t pool_state;
1602 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1604 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1606 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1608 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1611 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1612 (uint64_t **)&vs, &vsc) == 0);
1613 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1615 reason = zpool_import_status(config, &msgid);
1617 (void) printf(gettext(" pool: %s\n"), name);
1618 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1619 (void) printf(gettext(" state: %s"), health);
1620 if (pool_state == POOL_STATE_DESTROYED)
1621 (void) printf(gettext(" (DESTROYED)"));
1622 (void) printf("\n");
1625 case ZPOOL_STATUS_MISSING_DEV_R:
1626 case ZPOOL_STATUS_MISSING_DEV_NR:
1627 case ZPOOL_STATUS_BAD_GUID_SUM:
1628 (void) printf(gettext(" status: One or more devices are "
1629 "missing from the system.\n"));
1632 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1633 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1634 (void) printf(gettext(" status: One or more devices contains "
1635 "corrupted data.\n"));
1638 case ZPOOL_STATUS_CORRUPT_DATA:
1640 gettext(" status: The pool data is corrupted.\n"));
1643 case ZPOOL_STATUS_OFFLINE_DEV:
1644 (void) printf(gettext(" status: One or more devices "
1645 "are offlined.\n"));
1648 case ZPOOL_STATUS_CORRUPT_POOL:
1649 (void) printf(gettext(" status: The pool metadata is "
1653 case ZPOOL_STATUS_VERSION_OLDER:
1654 (void) printf(gettext(" status: The pool is formatted using a "
1655 "legacy on-disk version.\n"));
1658 case ZPOOL_STATUS_VERSION_NEWER:
1659 (void) printf(gettext(" status: The pool is formatted using an "
1660 "incompatible version.\n"));
1663 case ZPOOL_STATUS_FEAT_DISABLED:
1664 (void) printf(gettext(" status: Some supported features are "
1665 "not enabled on the pool.\n"));
1668 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1669 (void) printf(gettext("status: The pool uses the following "
1670 "feature(s) not supported on this sytem:\n"));
1671 zpool_print_unsup_feat(config);
1674 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1675 (void) printf(gettext("status: The pool can only be accessed "
1676 "in read-only mode on this system. It\n\tcannot be "
1677 "accessed in read-write mode because it uses the "
1678 "following\n\tfeature(s) not supported on this system:\n"));
1679 zpool_print_unsup_feat(config);
1682 case ZPOOL_STATUS_HOSTID_MISMATCH:
1683 (void) printf(gettext(" status: The pool was last accessed by "
1684 "another system.\n"));
1687 case ZPOOL_STATUS_FAULTED_DEV_R:
1688 case ZPOOL_STATUS_FAULTED_DEV_NR:
1689 (void) printf(gettext(" status: One or more devices are "
1693 case ZPOOL_STATUS_BAD_LOG:
1694 (void) printf(gettext(" status: An intent log record cannot be "
1698 case ZPOOL_STATUS_RESILVERING:
1699 (void) printf(gettext(" status: One or more devices were being "
1705 * No other status can be seen when importing pools.
1707 assert(reason == ZPOOL_STATUS_OK);
1711 * Print out an action according to the overall state of the pool.
1713 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1714 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1715 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1716 (void) printf(gettext(" action: The pool can be "
1717 "imported using its name or numeric identifier, "
1718 "though\n\tsome features will not be available "
1719 "without an explicit 'zpool upgrade'.\n"));
1720 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1721 (void) printf(gettext(" action: The pool can be "
1722 "imported using its name or numeric "
1723 "identifier and\n\tthe '-f' flag.\n"));
1725 (void) printf(gettext(" action: The pool can be "
1726 "imported using its name or numeric "
1729 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1730 (void) printf(gettext(" action: The pool can be imported "
1731 "despite missing or damaged devices. The\n\tfault "
1732 "tolerance of the pool may be compromised if imported.\n"));
1735 case ZPOOL_STATUS_VERSION_NEWER:
1736 (void) printf(gettext(" action: The pool cannot be "
1737 "imported. Access the pool on a system running "
1738 "newer\n\tsoftware, or recreate the pool from "
1741 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1742 (void) printf(gettext("action: The pool cannot be "
1743 "imported. Access the pool on a system that "
1744 "supports\n\tthe required feature(s), or recreate "
1745 "the pool from backup.\n"));
1747 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1748 (void) printf(gettext("action: The pool cannot be "
1749 "imported in read-write mode. Import the pool "
1751 "\t\"-o readonly=on\", access the pool on a system "
1752 "that supports the\n\trequired feature(s), or "
1753 "recreate the pool from backup.\n"));
1755 case ZPOOL_STATUS_MISSING_DEV_R:
1756 case ZPOOL_STATUS_MISSING_DEV_NR:
1757 case ZPOOL_STATUS_BAD_GUID_SUM:
1758 (void) printf(gettext(" action: The pool cannot be "
1759 "imported. Attach the missing\n\tdevices and try "
1763 (void) printf(gettext(" action: The pool cannot be "
1764 "imported due to damaged devices or data.\n"));
1768 /* Print the comment attached to the pool. */
1769 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1770 (void) printf(gettext("comment: %s\n"), comment);
1773 * If the state is "closed" or "can't open", and the aux state
1774 * is "corrupt data":
1776 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1777 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1778 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1779 if (pool_state == POOL_STATE_DESTROYED)
1780 (void) printf(gettext("\tThe pool was destroyed, "
1781 "but can be imported using the '-Df' flags.\n"));
1782 else if (pool_state != POOL_STATE_EXPORTED)
1783 (void) printf(gettext("\tThe pool may be active on "
1784 "another system, but can be imported using\n\t"
1785 "the '-f' flag.\n"));
1789 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1792 (void) printf(gettext(" config:\n\n"));
1794 namewidth = max_width(NULL, nvroot, 0, 0);
1798 print_import_config(name, nvroot, namewidth, 0);
1799 if (num_logs(nvroot) > 0)
1800 print_logs(NULL, nvroot, namewidth, B_FALSE);
1802 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1803 (void) printf(gettext("\n\tAdditional devices are known to "
1804 "be part of this pool, though their\n\texact "
1805 "configuration cannot be determined.\n"));
1810 * Perform the import for the given configuration. This passes the heavy
1811 * lifting off to zpool_import_props(), and then mounts the datasets contained
1815 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1816 nvlist_t *props, int flags)
1818 zpool_handle_t *zhp;
1823 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1826 verify(nvlist_lookup_uint64(config,
1827 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1828 verify(nvlist_lookup_uint64(config,
1829 ZPOOL_CONFIG_VERSION, &version) == 0);
1830 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1831 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1832 "is formatted using an unsupported ZFS version\n"), name);
1834 } else if (state != POOL_STATE_EXPORTED &&
1835 !(flags & ZFS_IMPORT_ANY_HOST)) {
1838 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1840 if ((unsigned long)hostid != gethostid()) {
1845 verify(nvlist_lookup_string(config,
1846 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1847 verify(nvlist_lookup_uint64(config,
1848 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1850 (void) fprintf(stderr, gettext("cannot import "
1851 "'%s': pool may be in use from other "
1852 "system, it was last accessed by %s "
1853 "(hostid: 0x%lx) on %s"), name, hostname,
1854 (unsigned long)hostid,
1855 asctime(localtime(&t)));
1856 (void) fprintf(stderr, gettext("use '-f' to "
1857 "import anyway\n"));
1861 (void) fprintf(stderr, gettext("cannot import '%s': "
1862 "pool may be in use from other system\n"), name);
1863 (void) fprintf(stderr, gettext("use '-f' to import "
1869 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1872 if (newname != NULL)
1873 name = (char *)newname;
1875 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1878 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1879 !(flags & ZFS_IMPORT_ONLY) &&
1880 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1890 * zpool import [-d dir] [-D]
1891 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1892 * [-d dir | -c cachefile] [-f] -a
1893 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1894 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1896 * -c Read pool information from a cachefile instead of searching
1899 * -d Scan in a specific directory, other than /dev/dsk. More than
1900 * one directory can be specified using multiple '-d' options.
1902 * -D Scan for previously destroyed pools or import all or only
1903 * specified destroyed pools.
1905 * -R Temporarily import the pool, with all mountpoints relative to
1906 * the given root. The pool will remain exported when the machine
1909 * -V Import even in the presence of faulted vdevs. This is an
1910 * intentionally undocumented option for testing purposes, and
1911 * treats the pool configuration as complete, leaving any bad
1912 * vdevs in the FAULTED state. In other words, it does verbatim
1915 * -f Force import, even if it appears that the pool is active.
1917 * -F Attempt rewind if necessary.
1919 * -n See if rewind would work, but don't actually rewind.
1921 * -N Import the pool but don't mount datasets.
1923 * -T Specify a starting txg to use for import. This option is
1924 * intentionally undocumented option for testing purposes.
1926 * -a Import all pools found.
1928 * -o Set property=value and/or temporary mount options (without '=').
1930 * The import command scans for pools to import, and import pools based on pool
1931 * name and GUID. The pool can also be renamed as part of the import process.
1934 zpool_do_import(int argc, char **argv)
1936 char **searchdirs = NULL;
1940 nvlist_t *pools = NULL;
1941 boolean_t do_all = B_FALSE;
1942 boolean_t do_destroyed = B_FALSE;
1943 char *mntopts = NULL;
1946 uint64_t searchguid = 0;
1947 char *searchname = NULL;
1949 nvlist_t *found_config;
1950 nvlist_t *policy = NULL;
1951 nvlist_t *props = NULL;
1953 int flags = ZFS_IMPORT_NORMAL;
1954 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1955 boolean_t dryrun = B_FALSE;
1956 boolean_t do_rewind = B_FALSE;
1957 boolean_t xtreme_rewind = B_FALSE;
1958 uint64_t pool_state, txg = -1ULL;
1959 char *cachefile = NULL;
1960 importargs_t idata = { 0 };
1964 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1973 if (searchdirs == NULL) {
1974 searchdirs = safe_malloc(sizeof (char *));
1976 char **tmp = safe_malloc((nsearch + 1) *
1978 bcopy(searchdirs, tmp, nsearch *
1983 searchdirs[nsearch++] = optarg;
1986 do_destroyed = B_TRUE;
1989 flags |= ZFS_IMPORT_ANY_HOST;
1995 flags |= ZFS_IMPORT_MISSING_LOG;
2001 flags |= ZFS_IMPORT_ONLY;
2004 if ((propval = strchr(optarg, '=')) != NULL) {
2007 if (add_prop_list(optarg, propval,
2015 if (add_prop_list(zpool_prop_to_name(
2016 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2018 if (nvlist_lookup_string(props,
2019 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2022 if (add_prop_list(zpool_prop_to_name(
2023 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2028 txg = strtoull(optarg, &endptr, 10);
2029 if (errno != 0 || *endptr != '\0') {
2030 (void) fprintf(stderr,
2031 gettext("invalid txg value\n"));
2034 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2037 flags |= ZFS_IMPORT_VERBATIM;
2040 xtreme_rewind = B_TRUE;
2043 (void) fprintf(stderr, gettext("missing argument for "
2044 "'%c' option\n"), optopt);
2048 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2057 if (cachefile && nsearch != 0) {
2058 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2062 if ((dryrun || xtreme_rewind) && !do_rewind) {
2063 (void) fprintf(stderr,
2064 gettext("-n or -X only meaningful with -F\n"));
2068 rewind_policy = ZPOOL_TRY_REWIND;
2070 rewind_policy = ZPOOL_DO_REWIND;
2072 rewind_policy |= ZPOOL_EXTREME_REWIND;
2074 /* In the future, we can capture further policy and include it here */
2075 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2076 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2077 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2080 if (searchdirs == NULL) {
2081 searchdirs = safe_malloc(sizeof (char *));
2082 searchdirs[0] = "/dev";
2086 /* check argument count */
2089 (void) fprintf(stderr, gettext("too many arguments\n"));
2094 (void) fprintf(stderr, gettext("too many arguments\n"));
2099 * Check for the SYS_CONFIG privilege. We do this explicitly
2100 * here because otherwise any attempt to discover pools will
2103 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2104 (void) fprintf(stderr, gettext("cannot "
2105 "discover pools: permission denied\n"));
2107 nvlist_free(policy);
2113 * Depending on the arguments given, we do one of the following:
2115 * <none> Iterate through all pools and display information about
2118 * -a Iterate through all pools and try to import each one.
2120 * <id> Find the pool that corresponds to the given GUID/pool
2121 * name and import that one.
2123 * -D Above options applies only to destroyed pools.
2129 searchguid = strtoull(argv[0], &endptr, 10);
2130 if (errno != 0 || *endptr != '\0')
2131 searchname = argv[0];
2132 found_config = NULL;
2135 * User specified a name or guid. Ensure it's unique.
2137 idata.unique = B_TRUE;
2141 idata.path = searchdirs;
2142 idata.paths = nsearch;
2143 idata.poolname = searchname;
2144 idata.guid = searchguid;
2145 idata.cachefile = cachefile;
2147 pools = zpool_search_import(g_zfs, &idata);
2149 if (pools != NULL && idata.exists &&
2150 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2151 (void) fprintf(stderr, gettext("cannot import '%s': "
2152 "a pool with that name already exists\n"),
2154 (void) fprintf(stderr, gettext("use the form '%s "
2155 "<pool | id> <newpool>' to give it a new name\n"),
2158 } else if (pools == NULL && idata.exists) {
2159 (void) fprintf(stderr, gettext("cannot import '%s': "
2160 "a pool with that name is already created/imported,\n"),
2162 (void) fprintf(stderr, gettext("and no additional pools "
2163 "with that name were found\n"));
2165 } else if (pools == NULL) {
2167 (void) fprintf(stderr, gettext("cannot import '%s': "
2168 "no such pool available\n"), argv[0]);
2175 nvlist_free(policy);
2180 * At this point we have a list of import candidate configs. Even if
2181 * we were searching by pool name or guid, we still need to
2182 * post-process the list to deal with pool state and possible
2188 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2190 verify(nvpair_value_nvlist(elem, &config) == 0);
2192 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2194 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2196 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2199 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2206 (void) printf("\n");
2209 err |= do_import(config, NULL, mntopts,
2212 show_import(config);
2214 } else if (searchname != NULL) {
2218 * We are searching for a pool based on name.
2220 verify(nvlist_lookup_string(config,
2221 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2223 if (strcmp(name, searchname) == 0) {
2224 if (found_config != NULL) {
2225 (void) fprintf(stderr, gettext(
2226 "cannot import '%s': more than "
2227 "one matching pool\n"), searchname);
2228 (void) fprintf(stderr, gettext(
2229 "import by numeric ID instead\n"));
2232 found_config = config;
2238 * Search for a pool by guid.
2240 verify(nvlist_lookup_uint64(config,
2241 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2243 if (guid == searchguid)
2244 found_config = config;
2249 * If we were searching for a specific pool, verify that we found a
2250 * pool, and then do the import.
2252 if (argc != 0 && err == 0) {
2253 if (found_config == NULL) {
2254 (void) fprintf(stderr, gettext("cannot import '%s': "
2255 "no such pool available\n"), argv[0]);
2258 err |= do_import(found_config, argc == 1 ? NULL :
2259 argv[1], mntopts, props, flags);
2264 * If we were just looking for pools, report an error if none were
2267 if (argc == 0 && first)
2268 (void) fprintf(stderr,
2269 gettext("no pools available to import\n"));
2274 nvlist_free(policy);
2277 return (err ? 1 : 0);
2280 typedef struct iostat_cbdata {
2281 boolean_t cb_verbose;
2284 zpool_list_t *cb_list;
2288 print_iostat_separator(iostat_cbdata_t *cb)
2292 for (i = 0; i < cb->cb_namewidth; i++)
2294 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2298 print_iostat_header(iostat_cbdata_t *cb)
2300 (void) printf("%*s capacity operations bandwidth\n",
2301 cb->cb_namewidth, "");
2302 (void) printf("%-*s alloc free read write read write\n",
2303 cb->cb_namewidth, "pool");
2304 print_iostat_separator(cb);
2308 * Display a single statistic.
2311 print_one_stat(uint64_t value)
2315 zfs_nicenum(value, buf, sizeof (buf));
2316 (void) printf(" %5s", buf);
2320 * Print out all the statistics for the given vdev. This can either be the
2321 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2322 * is a verbose output, and we don't want to display the toplevel pool stats.
2325 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2326 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2328 nvlist_t **oldchild, **newchild;
2330 vdev_stat_t *oldvs, *newvs;
2331 vdev_stat_t zerovs = { 0 };
2336 if (oldnv != NULL) {
2337 verify(nvlist_lookup_uint64_array(oldnv,
2338 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2343 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2344 (uint64_t **)&newvs, &c) == 0);
2346 if (strlen(name) + depth > cb->cb_namewidth)
2347 (void) printf("%*s%s", depth, "", name);
2349 (void) printf("%*s%s%*s", depth, "", name,
2350 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2352 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2357 scale = (double)NANOSEC / tdelta;
2359 /* only toplevel vdevs have capacity stats */
2360 if (newvs->vs_space == 0) {
2361 (void) printf(" - -");
2363 print_one_stat(newvs->vs_alloc);
2364 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2367 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2368 oldvs->vs_ops[ZIO_TYPE_READ])));
2370 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2371 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2373 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2374 oldvs->vs_bytes[ZIO_TYPE_READ])));
2376 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2377 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2379 (void) printf("\n");
2381 if (!cb->cb_verbose)
2384 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2385 &newchild, &children) != 0)
2388 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2389 &oldchild, &c) != 0)
2392 for (c = 0; c < children; c++) {
2393 uint64_t ishole = B_FALSE, islog = B_FALSE;
2395 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2398 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2401 if (ishole || islog)
2404 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2405 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2406 newchild[c], cb, depth + 2);
2411 * Log device section
2414 if (num_logs(newnv) > 0) {
2415 (void) printf("%-*s - - - - - "
2416 "-\n", cb->cb_namewidth, "logs");
2418 for (c = 0; c < children; c++) {
2419 uint64_t islog = B_FALSE;
2420 (void) nvlist_lookup_uint64(newchild[c],
2421 ZPOOL_CONFIG_IS_LOG, &islog);
2424 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2426 print_vdev_stats(zhp, vname, oldnv ?
2427 oldchild[c] : NULL, newchild[c],
2436 * Include level 2 ARC devices in iostat output
2438 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2439 &newchild, &children) != 0)
2442 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2443 &oldchild, &c) != 0)
2447 (void) printf("%-*s - - - - - "
2448 "-\n", cb->cb_namewidth, "cache");
2449 for (c = 0; c < children; c++) {
2450 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2452 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2453 newchild[c], cb, depth + 2);
2460 refresh_iostat(zpool_handle_t *zhp, void *data)
2462 iostat_cbdata_t *cb = data;
2466 * If the pool has disappeared, remove it from the list and continue.
2468 if (zpool_refresh_stats(zhp, &missing) != 0)
2472 pool_list_remove(cb->cb_list, zhp);
2478 * Callback to print out the iostats for the given pool.
2481 print_iostat(zpool_handle_t *zhp, void *data)
2483 iostat_cbdata_t *cb = data;
2484 nvlist_t *oldconfig, *newconfig;
2485 nvlist_t *oldnvroot, *newnvroot;
2487 newconfig = zpool_get_config(zhp, &oldconfig);
2489 if (cb->cb_iteration == 1)
2492 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2495 if (oldconfig == NULL)
2498 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2502 * Print out the statistics for the pool.
2504 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2507 print_iostat_separator(cb);
2513 get_namewidth(zpool_handle_t *zhp, void *data)
2515 iostat_cbdata_t *cb = data;
2516 nvlist_t *config, *nvroot;
2518 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2519 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2521 if (!cb->cb_verbose)
2522 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2524 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2529 * The width must fall into the range [10,38]. The upper limit is the
2530 * maximum we can have and still fit in 80 columns.
2532 if (cb->cb_namewidth < 10)
2533 cb->cb_namewidth = 10;
2534 if (cb->cb_namewidth > 38)
2535 cb->cb_namewidth = 38;
2541 * Parse the input string, get the 'interval' and 'count' value if there is one.
2544 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2547 unsigned long interval = 0, count = 0;
2548 int argc = *argcp, errno;
2551 * Determine if the last argument is an integer or a pool name
2553 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2557 interval = strtoul(argv[argc - 1], &end, 10);
2559 if (*end == '\0' && errno == 0) {
2560 if (interval == 0) {
2561 (void) fprintf(stderr, gettext("interval "
2562 "cannot be zero\n"));
2566 * Ignore the last parameter
2571 * If this is not a valid number, just plow on. The
2572 * user will get a more informative error message later
2580 * If the last argument is also an integer, then we have both a count
2583 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2588 interval = strtoul(argv[argc - 1], &end, 10);
2590 if (*end == '\0' && errno == 0) {
2591 if (interval == 0) {
2592 (void) fprintf(stderr, gettext("interval "
2593 "cannot be zero\n"));
2598 * Ignore the last parameter
2612 get_timestamp_arg(char c)
2615 timestamp_fmt = UDATE;
2617 timestamp_fmt = DDATE;
2623 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2625 * -v Display statistics for individual vdevs
2626 * -T Display a timestamp in date(1) or Unix format
2628 * This command can be tricky because we want to be able to deal with pool
2629 * creation/destruction as well as vdev configuration changes. The bulk of this
2630 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2631 * on pool_list_update() to detect the addition of new pools. Configuration
2632 * changes are all handled within libzfs.
2635 zpool_do_iostat(int argc, char **argv)
2640 unsigned long interval = 0, count = 0;
2642 boolean_t verbose = B_FALSE;
2646 while ((c = getopt(argc, argv, "T:v")) != -1) {
2649 get_timestamp_arg(*optarg);
2655 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2664 get_interval_count(&argc, argv, &interval, &count);
2667 * Construct the list of all interesting pools.
2670 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2673 if (pool_list_count(list) == 0 && argc != 0) {
2674 pool_list_free(list);
2678 if (pool_list_count(list) == 0 && interval == 0) {
2679 pool_list_free(list);
2680 (void) fprintf(stderr, gettext("no pools available\n"));
2685 * Enter the main iostat loop.
2688 cb.cb_verbose = verbose;
2689 cb.cb_iteration = 0;
2690 cb.cb_namewidth = 0;
2693 pool_list_update(list);
2695 if ((npools = pool_list_count(list)) == 0)
2699 * Refresh all statistics. This is done as an explicit step
2700 * before calculating the maximum name width, so that any
2701 * configuration changes are properly accounted for.
2703 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2706 * Iterate over all pools to determine the maximum width
2707 * for the pool / device name column across all pools.
2709 cb.cb_namewidth = 0;
2710 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2712 if (timestamp_fmt != NODATE)
2713 print_timestamp(timestamp_fmt);
2716 * If it's the first time, or verbose mode, print the header.
2718 if (++cb.cb_iteration == 1 || verbose)
2719 print_iostat_header(&cb);
2721 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2724 * If there's more than one pool, and we're not in verbose mode
2725 * (which prints a separator for us), then print a separator.
2727 if (npools > 1 && !verbose)
2728 print_iostat_separator(&cb);
2731 (void) printf("\n");
2734 * Flush the output so that redirection to a file isn't buffered
2737 (void) fflush(stdout);
2742 if (count != 0 && --count == 0)
2745 (void) sleep(interval);
2748 pool_list_free(list);
2753 typedef struct list_cbdata {
2754 boolean_t cb_verbose;
2756 boolean_t cb_scripted;
2757 zprop_list_t *cb_proplist;
2761 * Given a list of columns to display, output appropriate headers for each one.
2764 print_header(list_cbdata_t *cb)
2766 zprop_list_t *pl = cb->cb_proplist;
2767 char headerbuf[ZPOOL_MAXPROPLEN];
2769 boolean_t first = B_TRUE;
2770 boolean_t right_justify;
2773 for (; pl != NULL; pl = pl->pl_next) {
2774 width = pl->pl_width;
2775 if (first && cb->cb_verbose) {
2777 * Reset the width to accommodate the verbose listing
2780 width = cb->cb_namewidth;
2788 right_justify = B_FALSE;
2789 if (pl->pl_prop != ZPROP_INVAL) {
2790 header = zpool_prop_column_name(pl->pl_prop);
2791 right_justify = zpool_prop_align_right(pl->pl_prop);
2795 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2796 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2797 headerbuf[i] = '\0';
2801 if (pl->pl_next == NULL && !right_justify)
2802 (void) printf("%s", header);
2803 else if (right_justify)
2804 (void) printf("%*s", width, header);
2806 (void) printf("%-*s", width, header);
2810 (void) printf("\n");
2814 * Given a pool and a list of properties, print out all the properties according
2815 * to the described layout.
2818 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2820 zprop_list_t *pl = cb->cb_proplist;
2821 boolean_t first = B_TRUE;
2822 char property[ZPOOL_MAXPROPLEN];
2824 boolean_t right_justify;
2827 for (; pl != NULL; pl = pl->pl_next) {
2829 width = pl->pl_width;
2830 if (first && cb->cb_verbose) {
2832 * Reset the width to accommodate the verbose listing
2835 width = cb->cb_namewidth;
2839 if (cb->cb_scripted)
2840 (void) printf("\t");
2847 right_justify = B_FALSE;
2848 if (pl->pl_prop != ZPROP_INVAL) {
2849 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2850 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2852 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2853 sizeof (property), NULL) != 0)
2858 right_justify = zpool_prop_align_right(pl->pl_prop);
2859 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2860 zpool_prop_unsupported(pl->pl_user_prop)) &&
2861 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2862 sizeof (property)) == 0) {
2870 * If this is being called in scripted mode, or if this is the
2871 * last column and it is left-justified, don't include a width
2874 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2875 (void) printf("%s", propstr);
2876 else if (right_justify)
2877 (void) printf("%*s", width, propstr);
2879 (void) printf("%-*s", width, propstr);
2882 (void) printf("\n");
2886 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2890 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2892 zfs_nicenum(value, propval, sizeof (propval));
2894 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2895 (void) strlcpy(propval, "-", sizeof (propval));
2898 (void) printf("\t%s", propval);
2900 (void) printf(" %*s", width, propval);
2904 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2905 list_cbdata_t *cb, int depth)
2911 boolean_t scripted = cb->cb_scripted;
2913 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2914 (uint64_t **)&vs, &c) == 0);
2918 (void) printf("\t%s", name);
2919 else if (strlen(name) + depth > cb->cb_namewidth)
2920 (void) printf("%*s%s", depth, "", name);
2922 (void) printf("%*s%s%*s", depth, "", name,
2923 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2925 /* only toplevel vdevs have capacity stats */
2926 if (vs->vs_space == 0) {
2928 (void) printf("\t-\t-\t-");
2930 (void) printf(" - - -");
2932 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2934 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2936 print_one_column(ZPOOL_PROP_FREE,
2937 vs->vs_space - vs->vs_alloc, scripted);
2939 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2941 (void) printf("\n");
2944 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2945 &child, &children) != 0)
2948 for (c = 0; c < children; c++) {
2949 uint64_t ishole = B_FALSE;
2951 if (nvlist_lookup_uint64(child[c],
2952 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2955 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2956 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2961 * Include level 2 ARC devices in iostat output
2963 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2964 &child, &children) != 0)
2968 (void) printf("%-*s - - - - - "
2969 "-\n", cb->cb_namewidth, "cache");
2970 for (c = 0; c < children; c++) {
2971 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2973 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2981 * Generic callback function to list a pool.
2984 list_callback(zpool_handle_t *zhp, void *data)
2986 list_cbdata_t *cbp = data;
2990 config = zpool_get_config(zhp, NULL);
2992 print_pool(zhp, cbp);
2993 if (!cbp->cb_verbose)
2996 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2998 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3004 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3006 * -H Scripted mode. Don't display headers, and separate properties
3008 * -o List of properties to display. Defaults to
3009 * "name,size,allocated,free,capacity,health,altroot"
3010 * -T Display a timestamp in date(1) or Unix format
3012 * List all pools in the system, whether or not they're healthy. Output space
3013 * statistics for each one, as well as health status summary.
3016 zpool_do_list(int argc, char **argv)
3020 list_cbdata_t cb = { 0 };
3021 static char default_props[] =
3022 "name,size,allocated,free,capacity,dedupratio,"
3024 char *props = default_props;
3025 unsigned long interval = 0, count = 0;
3027 boolean_t first = B_TRUE;
3030 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3033 cb.cb_scripted = B_TRUE;
3039 get_timestamp_arg(*optarg);
3042 cb.cb_verbose = B_TRUE;
3045 (void) fprintf(stderr, gettext("missing argument for "
3046 "'%c' option\n"), optopt);
3050 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3059 get_interval_count(&argc, argv, &interval, &count);
3061 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3064 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3067 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3068 (void) printf(gettext("no pools available\n"));
3069 zprop_free_list(cb.cb_proplist);
3074 pool_list_update(list);
3076 if (pool_list_count(list) == 0)
3079 cb.cb_namewidth = 0;
3080 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3082 if (timestamp_fmt != NODATE)
3083 print_timestamp(timestamp_fmt);
3085 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3089 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3094 if (count != 0 && --count == 0)
3097 (void) sleep(interval);
3100 zprop_free_list(cb.cb_proplist);
3105 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3112 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3113 &child, &children) != 0) {
3114 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3115 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3116 name += sizeof(_PATH_DEV) - 1;
3117 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3118 path += sizeof(_PATH_DEV) - 1;
3119 if (strcmp(name, path) == 0)
3124 for (c = 0; c < children; c++)
3125 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3132 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3134 boolean_t force = B_FALSE;
3137 char *poolname, *old_disk, *new_disk;
3138 zpool_handle_t *zhp;
3142 while ((c = getopt(argc, argv, "f")) != -1) {
3148 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3157 /* get pool name and check number of arguments */
3159 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3166 (void) fprintf(stderr,
3167 gettext("missing <device> specification\n"));
3175 (void) fprintf(stderr,
3176 gettext("missing <new_device> specification\n"));
3179 new_disk = old_disk;
3189 (void) fprintf(stderr, gettext("too many arguments\n"));
3193 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3196 if (zpool_get_config(zhp, NULL) == NULL) {
3197 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3203 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3205 if (nvroot == NULL) {
3210 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3212 nvlist_free(nvroot);
3219 * zpool replace [-f] <pool> <device> <new_device>
3221 * -f Force attach, even if <new_device> appears to be in use.
3223 * Replace <device> with <new_device>.
3227 zpool_do_replace(int argc, char **argv)
3229 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3233 * zpool attach [-f] <pool> <device> <new_device>
3235 * -f Force attach, even if <new_device> appears to be in use.
3237 * Attach <new_device> to the mirror containing <device>. If <device> is not
3238 * part of a mirror, then <device> will be transformed into a mirror of
3239 * <device> and <new_device>. In either case, <new_device> will begin life
3240 * with a DTL of [0, now], and will immediately begin to resilver itself.
3243 zpool_do_attach(int argc, char **argv)
3245 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3249 * zpool detach [-f] <pool> <device>
3251 * -f Force detach of <device>, even if DTLs argue against it
3252 * (not supported yet)
3254 * Detach a device from a mirror. The operation will be refused if <device>
3255 * is the last device in the mirror, or if the DTLs indicate that this device
3256 * has the only valid copy of some data.
3260 zpool_do_detach(int argc, char **argv)
3263 char *poolname, *path;
3264 zpool_handle_t *zhp;
3268 while ((c = getopt(argc, argv, "f")) != -1) {
3272 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3281 /* get pool name and check number of arguments */
3283 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3288 (void) fprintf(stderr,
3289 gettext("missing <device> specification\n"));
3296 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3299 ret = zpool_vdev_detach(zhp, path);
3307 * zpool split [-n] [-o prop=val] ...
3309 * [-R altroot] <pool> <newpool> [<device> ...]
3311 * -n Do not split the pool, but display the resulting layout if
3312 * it were to be split.
3313 * -o Set property=value, or set mount options.
3314 * -R Mount the split-off pool under an alternate root.
3316 * Splits the named pool and gives it the new pool name. Devices to be split
3317 * off may be listed, provided that no more than one device is specified
3318 * per top-level vdev mirror. The newly split pool is left in an exported
3319 * state unless -R is specified.
3321 * Restrictions: the top-level of the pool pool must only be made up of
3322 * mirrors; all devices in the pool must be healthy; no device may be
3323 * undergoing a resilvering operation.
3326 zpool_do_split(int argc, char **argv)
3328 char *srcpool, *newpool, *propval;
3329 char *mntopts = NULL;
3332 zpool_handle_t *zhp;
3333 nvlist_t *config, *props = NULL;
3335 flags.dryrun = B_FALSE;
3336 flags.import = B_FALSE;
3339 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3342 flags.import = B_TRUE;
3344 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3345 &props, B_TRUE) != 0) {
3352 flags.dryrun = B_TRUE;
3355 if ((propval = strchr(optarg, '=')) != NULL) {
3358 if (add_prop_list(optarg, propval,
3359 &props, B_TRUE) != 0) {
3369 (void) fprintf(stderr, gettext("missing argument for "
3370 "'%c' option\n"), optopt);
3374 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3381 if (!flags.import && mntopts != NULL) {
3382 (void) fprintf(stderr, gettext("setting mntopts is only "
3383 "valid when importing the pool\n"));
3391 (void) fprintf(stderr, gettext("Missing pool name\n"));
3395 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3405 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3408 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3409 if (config == NULL) {
3413 (void) printf(gettext("would create '%s' with the "
3414 "following layout:\n\n"), newpool);
3415 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3417 nvlist_free(config);
3422 if (ret != 0 || flags.dryrun || !flags.import)
3426 * The split was successful. Now we need to open the new
3427 * pool and import it.
3429 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3431 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3432 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3434 (void) fprintf(stderr, gettext("Split was successful, but "
3435 "the datasets could not all be mounted\n"));
3436 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3437 "different altroot\n"), "zpool import");
3447 * zpool online <pool> <device> ...
3450 zpool_do_online(int argc, char **argv)
3454 zpool_handle_t *zhp;
3456 vdev_state_t newstate;
3460 while ((c = getopt(argc, argv, "et")) != -1) {
3463 flags |= ZFS_ONLINE_EXPAND;
3467 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3476 /* get pool name and check number of arguments */
3478 (void) fprintf(stderr, gettext("missing pool name\n"));
3482 (void) fprintf(stderr, gettext("missing device name\n"));
3488 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3491 for (i = 1; i < argc; i++) {
3492 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3493 if (newstate != VDEV_STATE_HEALTHY) {
3494 (void) printf(gettext("warning: device '%s' "
3495 "onlined, but remains in faulted state\n"),
3497 if (newstate == VDEV_STATE_FAULTED)
3498 (void) printf(gettext("use 'zpool "
3499 "clear' to restore a faulted "
3502 (void) printf(gettext("use 'zpool "
3503 "replace' to replace devices "
3504 "that are no longer present\n"));
3517 * zpool offline [-ft] <pool> <device> ...
3519 * -f Force the device into the offline state, even if doing
3520 * so would appear to compromise pool availability.
3521 * (not supported yet)
3523 * -t Only take the device off-line temporarily. The offline
3524 * state will not be persistent across reboots.
3528 zpool_do_offline(int argc, char **argv)
3532 zpool_handle_t *zhp;
3534 boolean_t istmp = B_FALSE;
3537 while ((c = getopt(argc, argv, "ft")) != -1) {
3544 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3553 /* get pool name and check number of arguments */
3555 (void) fprintf(stderr, gettext("missing pool name\n"));
3559 (void) fprintf(stderr, gettext("missing device name\n"));
3565 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3568 for (i = 1; i < argc; i++) {
3569 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3579 * zpool clear <pool> [device]
3581 * Clear all errors associated with a pool or a particular device.
3584 zpool_do_clear(int argc, char **argv)
3588 boolean_t dryrun = B_FALSE;
3589 boolean_t do_rewind = B_FALSE;
3590 boolean_t xtreme_rewind = B_FALSE;
3591 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3592 nvlist_t *policy = NULL;
3593 zpool_handle_t *zhp;
3594 char *pool, *device;
3597 while ((c = getopt(argc, argv, "FnX")) != -1) {
3606 xtreme_rewind = B_TRUE;
3609 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3619 (void) fprintf(stderr, gettext("missing pool name\n"));
3624 (void) fprintf(stderr, gettext("too many arguments\n"));
3628 if ((dryrun || xtreme_rewind) && !do_rewind) {
3629 (void) fprintf(stderr,
3630 gettext("-n or -X only meaningful with -F\n"));
3634 rewind_policy = ZPOOL_TRY_REWIND;
3636 rewind_policy = ZPOOL_DO_REWIND;
3638 rewind_policy |= ZPOOL_EXTREME_REWIND;
3640 /* In future, further rewind policy choices can be passed along here */
3641 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3642 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3646 device = argc == 2 ? argv[1] : NULL;
3648 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3649 nvlist_free(policy);
3653 if (zpool_clear(zhp, device, policy) != 0)
3658 nvlist_free(policy);
3664 * zpool reguid <pool>
3667 zpool_do_reguid(int argc, char **argv)
3671 zpool_handle_t *zhp;
3675 while ((c = getopt(argc, argv, "")) != -1) {
3678 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3687 /* get pool name and check number of arguments */
3689 (void) fprintf(stderr, gettext("missing pool name\n"));
3694 (void) fprintf(stderr, gettext("too many arguments\n"));
3699 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3702 ret = zpool_reguid(zhp);
3710 * zpool reopen <pool>
3712 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3715 zpool_do_reopen(int argc, char **argv)
3719 zpool_handle_t *zhp;
3723 while ((c = getopt(argc, argv, "")) != -1) {
3726 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3736 (void) fprintf(stderr, gettext("missing pool name\n"));
3741 (void) fprintf(stderr, gettext("too many arguments\n"));
3746 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3749 ret = zpool_reopen(zhp);
3754 typedef struct scrub_cbdata {
3761 scrub_callback(zpool_handle_t *zhp, void *data)
3763 scrub_cbdata_t *cb = data;
3767 * Ignore faulted pools.
3769 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3770 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3771 "currently unavailable\n"), zpool_get_name(zhp));
3775 err = zpool_scan(zhp, cb->cb_type);
3781 * zpool scrub [-s] <pool> ...
3783 * -s Stop. Stops any in-progress scrub.
3786 zpool_do_scrub(int argc, char **argv)
3791 cb.cb_type = POOL_SCAN_SCRUB;
3794 while ((c = getopt(argc, argv, "s")) != -1) {
3797 cb.cb_type = POOL_SCAN_NONE;
3800 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3812 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3816 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3819 typedef struct status_cbdata {
3821 boolean_t cb_allpools;
3822 boolean_t cb_verbose;
3823 boolean_t cb_explain;
3825 boolean_t cb_dedup_stats;
3829 * Print out detailed scrub status.
3832 print_scan_status(pool_scan_stat_t *ps)
3835 uint64_t elapsed, mins_left, hours_left;
3836 uint64_t pass_exam, examined, total;
3838 double fraction_done;
3839 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3841 (void) printf(gettext(" scan: "));
3843 /* If there's never been a scan, there's not much to say. */
3844 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3845 ps->pss_func >= POOL_SCAN_FUNCS) {
3846 (void) printf(gettext("none requested\n"));
3850 start = ps->pss_start_time;
3851 end = ps->pss_end_time;
3852 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3854 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3855 ps->pss_func == POOL_SCAN_RESILVER);
3857 * Scan is finished or canceled.
3859 if (ps->pss_state == DSS_FINISHED) {
3860 uint64_t minutes_taken = (end - start) / 60;
3863 if (ps->pss_func == POOL_SCAN_SCRUB) {
3864 fmt = gettext("scrub repaired %s in %lluh%um with "
3865 "%llu errors on %s");
3866 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3867 fmt = gettext("resilvered %s in %lluh%um with "
3868 "%llu errors on %s");
3871 (void) printf(fmt, processed_buf,
3872 (u_longlong_t)(minutes_taken / 60),
3873 (uint_t)(minutes_taken % 60),
3874 (u_longlong_t)ps->pss_errors,
3875 ctime((time_t *)&end));
3877 } else if (ps->pss_state == DSS_CANCELED) {
3878 if (ps->pss_func == POOL_SCAN_SCRUB) {
3879 (void) printf(gettext("scrub canceled on %s"),
3881 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3882 (void) printf(gettext("resilver canceled on %s"),
3888 assert(ps->pss_state == DSS_SCANNING);
3891 * Scan is in progress.
3893 if (ps->pss_func == POOL_SCAN_SCRUB) {
3894 (void) printf(gettext("scrub in progress since %s"),
3896 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3897 (void) printf(gettext("resilver in progress since %s"),
3901 examined = ps->pss_examined ? ps->pss_examined : 1;
3902 total = ps->pss_to_examine;
3903 fraction_done = (double)examined / total;
3905 /* elapsed time for this pass */
3906 elapsed = time(NULL) - ps->pss_pass_start;
3907 elapsed = elapsed ? elapsed : 1;
3908 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3909 rate = pass_exam / elapsed;
3910 rate = rate ? rate : 1;
3911 mins_left = ((total - examined) / rate) / 60;
3912 hours_left = mins_left / 60;
3914 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3915 zfs_nicenum(total, total_buf, sizeof (total_buf));
3916 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3919 * do not print estimated time if hours_left is more than 30 days
3921 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3922 examined_buf, total_buf, rate_buf);
3923 if (hours_left < (30 * 24)) {
3924 (void) printf(gettext(", %lluh%um to go\n"),
3925 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3927 (void) printf(gettext(
3928 ", (scan is slow, no estimated time)\n"));
3931 if (ps->pss_func == POOL_SCAN_RESILVER) {
3932 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3933 processed_buf, 100 * fraction_done);
3934 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3935 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3936 processed_buf, 100 * fraction_done);
3941 print_error_log(zpool_handle_t *zhp)
3943 nvlist_t *nverrlist = NULL;
3946 size_t len = MAXPATHLEN * 2;
3948 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3949 (void) printf("errors: List of errors unavailable "
3950 "(insufficient privileges)\n");
3954 (void) printf("errors: Permanent errors have been "
3955 "detected in the following files:\n\n");
3957 pathname = safe_malloc(len);
3959 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3961 uint64_t dsobj, obj;
3963 verify(nvpair_value_nvlist(elem, &nv) == 0);
3964 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3966 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3968 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3969 (void) printf("%7s %s\n", "", pathname);
3972 nvlist_free(nverrlist);
3976 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3985 (void) printf(gettext("\tspares\n"));
3987 for (i = 0; i < nspares; i++) {
3988 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3989 print_status_config(zhp, name, spares[i],
3990 namewidth, 2, B_TRUE);
3996 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4005 (void) printf(gettext("\tcache\n"));
4007 for (i = 0; i < nl2cache; i++) {
4008 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4009 print_status_config(zhp, name, l2cache[i],
4010 namewidth, 2, B_FALSE);
4016 print_dedup_stats(nvlist_t *config)
4018 ddt_histogram_t *ddh;
4024 * If the pool was faulted then we may not have been able to
4025 * obtain the config. Otherwise, if have anything in the dedup
4026 * table continue processing the stats.
4028 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4029 (uint64_t **)&ddo, &c) != 0)
4032 (void) printf("\n");
4033 (void) printf(gettext(" dedup: "));
4034 if (ddo->ddo_count == 0) {
4035 (void) printf(gettext("no DDT entries\n"));
4039 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4040 (u_longlong_t)ddo->ddo_count,
4041 (u_longlong_t)ddo->ddo_dspace,
4042 (u_longlong_t)ddo->ddo_mspace);
4044 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4045 (uint64_t **)&dds, &c) == 0);
4046 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4047 (uint64_t **)&ddh, &c) == 0);
4048 zpool_dump_ddt(dds, ddh);
4052 * Display a summary of pool status. Displays a summary such as:
4056 * reason: One or more devices ...
4057 * see: http://illumos.org/msg/ZFS-xxxx-01
4063 * When given the '-v' option, we print out the complete config. If the '-e'
4064 * option is specified, then we print out error rate information as well.
4067 status_callback(zpool_handle_t *zhp, void *data)
4069 status_cbdata_t *cbp = data;
4070 nvlist_t *config, *nvroot;
4077 config = zpool_get_config(zhp, NULL);
4078 reason = zpool_get_status(zhp, &msgid);
4083 * If we were given 'zpool status -x', only report those pools with
4086 if (cbp->cb_explain &&
4087 (reason == ZPOOL_STATUS_OK ||
4088 reason == ZPOOL_STATUS_VERSION_OLDER ||
4089 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4090 if (!cbp->cb_allpools) {
4091 (void) printf(gettext("pool '%s' is healthy\n"),
4092 zpool_get_name(zhp));
4094 cbp->cb_first = B_FALSE;
4100 cbp->cb_first = B_FALSE;
4102 (void) printf("\n");
4104 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4106 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4107 (uint64_t **)&vs, &c) == 0);
4108 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4110 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4111 (void) printf(gettext(" state: %s\n"), health);
4114 case ZPOOL_STATUS_MISSING_DEV_R:
4115 (void) printf(gettext("status: One or more devices could not "
4116 "be opened. Sufficient replicas exist for\n\tthe pool to "
4117 "continue functioning in a degraded state.\n"));
4118 (void) printf(gettext("action: Attach the missing device and "
4119 "online it using 'zpool online'.\n"));
4122 case ZPOOL_STATUS_MISSING_DEV_NR:
4123 (void) printf(gettext("status: One or more devices could not "
4124 "be opened. There are insufficient\n\treplicas for the "
4125 "pool to continue functioning.\n"));
4126 (void) printf(gettext("action: Attach the missing device and "
4127 "online it using 'zpool online'.\n"));
4130 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4131 (void) printf(gettext("status: One or more devices could not "
4132 "be used because the label is missing or\n\tinvalid. "
4133 "Sufficient replicas exist for the pool to continue\n\t"
4134 "functioning in a degraded state.\n"));
4135 (void) printf(gettext("action: Replace the device using "
4136 "'zpool replace'.\n"));
4139 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4140 (void) printf(gettext("status: One or more devices could not "
4141 "be used because the label is missing \n\tor invalid. "
4142 "There are insufficient replicas for the pool to "
4143 "continue\n\tfunctioning.\n"));
4144 zpool_explain_recover(zpool_get_handle(zhp),
4145 zpool_get_name(zhp), reason, config);
4148 case ZPOOL_STATUS_FAILING_DEV:
4149 (void) printf(gettext("status: One or more devices has "
4150 "experienced an unrecoverable error. An\n\tattempt was "
4151 "made to correct the error. Applications are "
4153 (void) printf(gettext("action: Determine if the device needs "
4154 "to be replaced, and clear the errors\n\tusing "
4155 "'zpool clear' or replace the device with 'zpool "
4159 case ZPOOL_STATUS_OFFLINE_DEV:
4160 (void) printf(gettext("status: One or more devices has "
4161 "been taken offline by the administrator.\n\tSufficient "
4162 "replicas exist for the pool to continue functioning in "
4163 "a\n\tdegraded state.\n"));
4164 (void) printf(gettext("action: Online the device using "
4165 "'zpool online' or replace the device with\n\t'zpool "
4169 case ZPOOL_STATUS_REMOVED_DEV:
4170 (void) printf(gettext("status: One or more devices has "
4171 "been removed by the administrator.\n\tSufficient "
4172 "replicas exist for the pool to continue functioning in "
4173 "a\n\tdegraded state.\n"));
4174 (void) printf(gettext("action: Online the device using "
4175 "'zpool online' or replace the device with\n\t'zpool "
4179 case ZPOOL_STATUS_RESILVERING:
4180 (void) printf(gettext("status: One or more devices is "
4181 "currently being resilvered. The pool will\n\tcontinue "
4182 "to function, possibly in a degraded state.\n"));
4183 (void) printf(gettext("action: Wait for the resilver to "
4187 case ZPOOL_STATUS_CORRUPT_DATA:
4188 (void) printf(gettext("status: One or more devices has "
4189 "experienced an error resulting in data\n\tcorruption. "
4190 "Applications may be affected.\n"));
4191 (void) printf(gettext("action: Restore the file in question "
4192 "if possible. Otherwise restore the\n\tentire pool from "
4196 case ZPOOL_STATUS_CORRUPT_POOL:
4197 (void) printf(gettext("status: The pool metadata is corrupted "
4198 "and the pool cannot be opened.\n"));
4199 zpool_explain_recover(zpool_get_handle(zhp),
4200 zpool_get_name(zhp), reason, config);
4203 case ZPOOL_STATUS_VERSION_OLDER:
4204 (void) printf(gettext("status: The pool is formatted using a "
4205 "legacy on-disk format. The pool can\n\tstill be used, "
4206 "but some features are unavailable.\n"));
4207 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4208 "upgrade'. Once this is done, the\n\tpool will no longer "
4209 "be accessible on software that does not support feature\n"
4213 case ZPOOL_STATUS_VERSION_NEWER:
4214 (void) printf(gettext("status: The pool has been upgraded to a "
4215 "newer, incompatible on-disk version.\n\tThe pool cannot "
4216 "be accessed on this system.\n"));
4217 (void) printf(gettext("action: Access the pool from a system "
4218 "running more recent software, or\n\trestore the pool from "
4222 case ZPOOL_STATUS_FEAT_DISABLED:
4223 (void) printf(gettext("status: Some supported features are not "
4224 "enabled on the pool. The pool can\n\tstill be used, but "
4225 "some features are unavailable.\n"));
4226 (void) printf(gettext("action: Enable all features using "
4227 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4228 "longer be accessible by software that does not support\n\t"
4229 "the features. See zpool-features(7) for details.\n"));
4232 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4233 (void) printf(gettext("status: The pool cannot be accessed on "
4234 "this system because it uses the\n\tfollowing feature(s) "
4235 "not supported on this system:\n"));
4236 zpool_print_unsup_feat(config);
4237 (void) printf("\n");
4238 (void) printf(gettext("action: Access the pool from a system "
4239 "that supports the required feature(s),\n\tor restore the "
4240 "pool from backup.\n"));
4243 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4244 (void) printf(gettext("status: The pool can only be accessed "
4245 "in read-only mode on this system. It\n\tcannot be "
4246 "accessed in read-write mode because it uses the "
4247 "following\n\tfeature(s) not supported on this system:\n"));
4248 zpool_print_unsup_feat(config);
4249 (void) printf("\n");
4250 (void) printf(gettext("action: The pool cannot be accessed in "
4251 "read-write mode. Import the pool with\n"
4252 "\t\"-o readonly=on\", access the pool from a system that "
4253 "supports the\n\trequired feature(s), or restore the "
4254 "pool from backup.\n"));
4257 case ZPOOL_STATUS_FAULTED_DEV_R:
4258 (void) printf(gettext("status: One or more devices are "
4259 "faulted in response to persistent errors.\n\tSufficient "
4260 "replicas exist for the pool to continue functioning "
4261 "in a\n\tdegraded state.\n"));
4262 (void) printf(gettext("action: Replace the faulted device, "
4263 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4266 case ZPOOL_STATUS_FAULTED_DEV_NR:
4267 (void) printf(gettext("status: One or more devices are "
4268 "faulted in response to persistent errors. There are "
4269 "insufficient replicas for the pool to\n\tcontinue "
4271 (void) printf(gettext("action: Destroy and re-create the pool "
4272 "from a backup source. Manually marking the device\n"
4273 "\trepaired using 'zpool clear' may allow some data "
4274 "to be recovered.\n"));
4277 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4278 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4279 (void) printf(gettext("status: One or more devices are "
4280 "faulted in response to IO failures.\n"));
4281 (void) printf(gettext("action: Make sure the affected devices "
4282 "are connected, then run 'zpool clear'.\n"));
4285 case ZPOOL_STATUS_BAD_LOG:
4286 (void) printf(gettext("status: An intent log record "
4287 "could not be read.\n"
4288 "\tWaiting for adminstrator intervention to fix the "
4289 "faulted pool.\n"));
4290 (void) printf(gettext("action: Either restore the affected "
4291 "device(s) and run 'zpool online',\n"
4292 "\tor ignore the intent log records by running "
4293 "'zpool clear'.\n"));
4296 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4297 (void) printf(gettext("status: One or more devices are "
4298 "configured to use a non-native block size.\n"
4299 "\tExpect reduced performance.\n"));
4300 (void) printf(gettext("action: Replace affected devices with "
4301 "devices that support the\n\tconfigured block size, or "
4302 "migrate data to a properly configured\n\tpool.\n"));
4307 * The remaining errors can't actually be generated, yet.
4309 assert(reason == ZPOOL_STATUS_OK);
4313 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4316 if (config != NULL) {
4319 nvlist_t **spares, **l2cache;
4320 uint_t nspares, nl2cache;
4321 pool_scan_stat_t *ps = NULL;
4323 (void) nvlist_lookup_uint64_array(nvroot,
4324 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4325 print_scan_status(ps);
4327 namewidth = max_width(zhp, nvroot, 0, 0);
4331 (void) printf(gettext("config:\n\n"));
4332 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4333 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4334 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4335 namewidth, 0, B_FALSE);
4337 if (num_logs(nvroot) > 0)
4338 print_logs(zhp, nvroot, namewidth, B_TRUE);
4339 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4340 &l2cache, &nl2cache) == 0)
4341 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4343 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4344 &spares, &nspares) == 0)
4345 print_spares(zhp, spares, nspares, namewidth);
4347 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4349 nvlist_t *nverrlist = NULL;
4352 * If the approximate error count is small, get a
4353 * precise count by fetching the entire log and
4354 * uniquifying the results.
4356 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4357 zpool_get_errlog(zhp, &nverrlist) == 0) {
4362 while ((elem = nvlist_next_nvpair(nverrlist,
4367 nvlist_free(nverrlist);
4369 (void) printf("\n");
4372 (void) printf(gettext("errors: No known data "
4374 else if (!cbp->cb_verbose)
4375 (void) printf(gettext("errors: %llu data "
4376 "errors, use '-v' for a list\n"),
4377 (u_longlong_t)nerr);
4379 print_error_log(zhp);
4382 if (cbp->cb_dedup_stats)
4383 print_dedup_stats(config);
4385 (void) printf(gettext("config: The configuration cannot be "
4393 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4395 * -v Display complete error logs
4396 * -x Display only pools with potential problems
4397 * -D Display dedup status (undocumented)
4398 * -T Display a timestamp in date(1) or Unix format
4400 * Describes the health status of all pools or some subset.
4403 zpool_do_status(int argc, char **argv)
4407 unsigned long interval = 0, count = 0;
4408 status_cbdata_t cb = { 0 };
4411 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4414 cb.cb_verbose = B_TRUE;
4417 cb.cb_explain = B_TRUE;
4420 cb.cb_dedup_stats = B_TRUE;
4423 get_timestamp_arg(*optarg);
4426 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4435 get_interval_count(&argc, argv, &interval, &count);
4438 cb.cb_allpools = B_TRUE;
4440 cb.cb_first = B_TRUE;
4443 if (timestamp_fmt != NODATE)
4444 print_timestamp(timestamp_fmt);
4446 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4447 status_callback, &cb);
4449 if (argc == 0 && cb.cb_count == 0)
4450 (void) printf(gettext("no pools available\n"));
4451 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4452 (void) printf(gettext("all pools are healthy\n"));
4460 if (count != 0 && --count == 0)
4463 (void) sleep(interval);
4469 typedef struct upgrade_cbdata {
4471 char cb_poolname[ZPOOL_MAXNAMELEN];
4473 uint64_t cb_version;
4479 is_root_pool(zpool_handle_t *zhp)
4481 static struct statfs sfs;
4482 static char *poolname = NULL;
4483 static boolean_t stated = B_FALSE;
4488 if (statfs("/", &sfs) == -1) {
4489 (void) fprintf(stderr,
4490 "Unable to stat root file system: %s.\n",
4494 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4496 poolname = sfs.f_mntfromname;
4497 if ((slash = strchr(poolname, '/')) != NULL)
4500 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4504 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4506 if (poolname[0] == '\0' && is_root_pool(zhp))
4507 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4509 #endif /* FreeBSD */
4512 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4516 uint64_t oldversion;
4518 config = zpool_get_config(zhp, NULL);
4519 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4522 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4523 assert(oldversion < version);
4525 ret = zpool_upgrade(zhp, version);
4529 if (version >= SPA_VERSION_FEATURES) {
4530 (void) printf(gettext("Successfully upgraded "
4531 "'%s' from version %llu to feature flags.\n"),
4532 zpool_get_name(zhp), oldversion);
4534 (void) printf(gettext("Successfully upgraded "
4535 "'%s' from version %llu to version %llu.\n"),
4536 zpool_get_name(zhp), oldversion, version);
4543 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4546 boolean_t firstff = B_TRUE;
4547 nvlist_t *enabled = zpool_get_features(zhp);
4550 for (i = 0; i < SPA_FEATURES; i++) {
4551 const char *fname = spa_feature_table[i].fi_uname;
4552 const char *fguid = spa_feature_table[i].fi_guid;
4553 if (!nvlist_exists(enabled, fguid)) {
4555 verify(-1 != asprintf(&propname, "feature@%s", fname));
4556 ret = zpool_set_prop(zhp, propname,
4557 ZFS_FEATURE_ENABLED);
4565 (void) printf(gettext("Enabled the "
4566 "following features on '%s':\n"),
4567 zpool_get_name(zhp));
4570 (void) printf(gettext(" %s\n"), fname);
4581 upgrade_cb(zpool_handle_t *zhp, void *arg)
4583 upgrade_cbdata_t *cbp = arg;
4586 boolean_t printnl = B_FALSE;
4589 config = zpool_get_config(zhp, NULL);
4590 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4593 assert(SPA_VERSION_IS_SUPPORTED(version));
4595 if (version < cbp->cb_version) {
4596 cbp->cb_first = B_FALSE;
4597 ret = upgrade_version(zhp, cbp->cb_version);
4601 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4602 sizeof(cbp->cb_poolname));
4603 #endif /* ___FreeBSD__ */
4608 * If they did "zpool upgrade -a", then we could
4609 * be doing ioctls to different pools. We need
4610 * to log this history once to each pool, and bypass
4611 * the normal history logging that happens in main().
4613 (void) zpool_log_history(g_zfs, history_str);
4614 log_history = B_FALSE;
4618 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4620 ret = upgrade_enable_all(zhp, &count);
4625 cbp->cb_first = B_FALSE;
4628 * If they did "zpool upgrade -a", then we could
4629 * be doing ioctls to different pools. We need
4630 * to log this history once to each pool, and bypass
4631 * the normal history logging that happens in main().
4633 (void) zpool_log_history(g_zfs, history_str);
4634 log_history = B_FALSE;
4639 (void) printf(gettext("\n"));
4646 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4648 upgrade_cbdata_t *cbp = arg;
4652 config = zpool_get_config(zhp, NULL);
4653 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4656 assert(SPA_VERSION_IS_SUPPORTED(version));
4658 if (version < SPA_VERSION_FEATURES) {
4659 if (cbp->cb_first) {
4660 (void) printf(gettext("The following pools are "
4661 "formatted with legacy version numbers and can\n"
4662 "be upgraded to use feature flags. After "
4663 "being upgraded, these pools\nwill no "
4664 "longer be accessible by software that does not "
4665 "support feature\nflags.\n\n"));
4666 (void) printf(gettext("VER POOL\n"));
4667 (void) printf(gettext("--- ------------\n"));
4668 cbp->cb_first = B_FALSE;
4671 (void) printf("%2llu %s\n", (u_longlong_t)version,
4672 zpool_get_name(zhp));
4679 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4681 upgrade_cbdata_t *cbp = arg;
4685 config = zpool_get_config(zhp, NULL);
4686 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4689 if (version >= SPA_VERSION_FEATURES) {
4691 boolean_t poolfirst = B_TRUE;
4692 nvlist_t *enabled = zpool_get_features(zhp);
4694 for (i = 0; i < SPA_FEATURES; i++) {
4695 const char *fguid = spa_feature_table[i].fi_guid;
4696 const char *fname = spa_feature_table[i].fi_uname;
4697 if (!nvlist_exists(enabled, fguid)) {
4698 if (cbp->cb_first) {
4699 (void) printf(gettext("\nSome "
4700 "supported features are not "
4701 "enabled on the following pools. "
4702 "Once a\nfeature is enabled the "
4703 "pool may become incompatible with "
4704 "software\nthat does not support "
4706 "zpool-features(7) for "
4708 (void) printf(gettext("POOL "
4710 (void) printf(gettext("------"
4712 cbp->cb_first = B_FALSE;
4716 (void) printf(gettext("%s\n"),
4717 zpool_get_name(zhp));
4718 poolfirst = B_FALSE;
4721 (void) printf(gettext(" %s\n"), fname);
4731 upgrade_one(zpool_handle_t *zhp, void *data)
4733 boolean_t printnl = B_FALSE;
4734 upgrade_cbdata_t *cbp = data;
4735 uint64_t cur_version;
4738 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4739 (void) printf(gettext("'log' is now a reserved word\n"
4740 "Pool 'log' must be renamed using export and import"
4745 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4746 if (cur_version > cbp->cb_version) {
4747 (void) printf(gettext("Pool '%s' is already formatted "
4748 "using more current version '%llu'.\n\n"),
4749 zpool_get_name(zhp), cur_version);
4753 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4754 (void) printf(gettext("Pool '%s' is already formatted "
4755 "using version %llu.\n\n"), zpool_get_name(zhp),
4760 if (cur_version != cbp->cb_version) {
4762 ret = upgrade_version(zhp, cbp->cb_version);
4766 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4767 sizeof(cbp->cb_poolname));
4768 #endif /* ___FreeBSD__ */
4771 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4773 ret = upgrade_enable_all(zhp, &count);
4780 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4781 sizeof(cbp->cb_poolname));
4782 #endif /* __FreeBSD __*/
4783 } else if (cur_version == SPA_VERSION) {
4784 (void) printf(gettext("Pool '%s' already has all "
4785 "supported features enabled.\n"),
4786 zpool_get_name(zhp));
4791 (void) printf(gettext("\n"));
4800 * zpool upgrade [-V version] <-a | pool ...>
4802 * With no arguments, display downrev'd ZFS pool available for upgrade.
4803 * Individual pools can be upgraded by specifying the pool, and '-a' will
4804 * upgrade all pools.
4807 zpool_do_upgrade(int argc, char **argv)
4810 upgrade_cbdata_t cb = { 0 };
4812 boolean_t showversions = B_FALSE;
4813 boolean_t upgradeall = B_FALSE;
4818 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4821 upgradeall = B_TRUE;
4824 showversions = B_TRUE;
4827 cb.cb_version = strtoll(optarg, &end, 10);
4829 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4830 (void) fprintf(stderr,
4831 gettext("invalid version '%s'\n"), optarg);
4836 (void) fprintf(stderr, gettext("missing argument for "
4837 "'%c' option\n"), optopt);
4841 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4852 if (cb.cb_version == 0) {
4853 cb.cb_version = SPA_VERSION;
4854 } else if (!upgradeall && argc == 0) {
4855 (void) fprintf(stderr, gettext("-V option is "
4856 "incompatible with other arguments\n"));
4861 if (upgradeall || argc != 0) {
4862 (void) fprintf(stderr, gettext("-v option is "
4863 "incompatible with other arguments\n"));
4866 } else if (upgradeall) {
4868 (void) fprintf(stderr, gettext("-a option should not "
4869 "be used along with a pool name\n"));
4874 (void) printf(gettext("This system supports ZFS pool feature "
4879 (void) printf(gettext("The following features are "
4881 (void) printf(gettext("FEAT DESCRIPTION\n"));
4882 (void) printf("----------------------------------------------"
4883 "---------------\n");
4884 for (i = 0; i < SPA_FEATURES; i++) {
4885 zfeature_info_t *fi = &spa_feature_table[i];
4886 const char *ro = fi->fi_can_readonly ?
4887 " (read-only compatible)" : "";
4889 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4890 (void) printf(" %s\n", fi->fi_desc);
4892 (void) printf("\n");
4894 (void) printf(gettext("The following legacy versions are also "
4896 (void) printf(gettext("VER DESCRIPTION\n"));
4897 (void) printf("--- -----------------------------------------"
4898 "---------------\n");
4899 (void) printf(gettext(" 1 Initial ZFS version\n"));
4900 (void) printf(gettext(" 2 Ditto blocks "
4901 "(replicated metadata)\n"));
4902 (void) printf(gettext(" 3 Hot spares and double parity "
4904 (void) printf(gettext(" 4 zpool history\n"));
4905 (void) printf(gettext(" 5 Compression using the gzip "
4907 (void) printf(gettext(" 6 bootfs pool property\n"));
4908 (void) printf(gettext(" 7 Separate intent log devices\n"));
4909 (void) printf(gettext(" 8 Delegated administration\n"));
4910 (void) printf(gettext(" 9 refquota and refreservation "
4912 (void) printf(gettext(" 10 Cache devices\n"));
4913 (void) printf(gettext(" 11 Improved scrub performance\n"));
4914 (void) printf(gettext(" 12 Snapshot properties\n"));
4915 (void) printf(gettext(" 13 snapused property\n"));
4916 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4917 (void) printf(gettext(" 15 user/group space accounting\n"));
4918 (void) printf(gettext(" 16 stmf property support\n"));
4919 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4920 (void) printf(gettext(" 18 Snapshot user holds\n"));
4921 (void) printf(gettext(" 19 Log device removal\n"));
4922 (void) printf(gettext(" 20 Compression using zle "
4923 "(zero-length encoding)\n"));
4924 (void) printf(gettext(" 21 Deduplication\n"));
4925 (void) printf(gettext(" 22 Received properties\n"));
4926 (void) printf(gettext(" 23 Slim ZIL\n"));
4927 (void) printf(gettext(" 24 System attributes\n"));
4928 (void) printf(gettext(" 25 Improved scrub stats\n"));
4929 (void) printf(gettext(" 26 Improved snapshot deletion "
4931 (void) printf(gettext(" 27 Improved snapshot creation "
4933 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4934 (void) printf(gettext("\nFor more information on a particular "
4935 "version, including supported releases,\n"));
4936 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4937 } else if (argc == 0 && upgradeall) {
4938 cb.cb_first = B_TRUE;
4939 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4940 if (ret == 0 && cb.cb_first) {
4941 if (cb.cb_version == SPA_VERSION) {
4942 (void) printf(gettext("All pools are already "
4943 "formatted using feature flags.\n\n"));
4944 (void) printf(gettext("Every feature flags "
4945 "pool already has all supported features "
4948 (void) printf(gettext("All pools are already "
4949 "formatted with version %llu or higher.\n"),
4953 } else if (argc == 0) {
4954 cb.cb_first = B_TRUE;
4955 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4959 (void) printf(gettext("All pools are formatted "
4960 "using feature flags.\n\n"));
4962 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4963 "for a list of available legacy versions.\n"));
4966 cb.cb_first = B_TRUE;
4967 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4971 (void) printf(gettext("Every feature flags pool has "
4972 "all supported features enabled.\n"));
4974 (void) printf(gettext("\n"));
4977 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4981 if (cb.cb_poolname[0] != '\0') {
4983 "If you boot from pool '%s', don't forget to update boot code.\n"
4984 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4985 "the following command will do it:\n"
4987 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4994 typedef struct hist_cbdata {
5001 * Print out the command history for a specific pool.
5004 get_history_one(zpool_handle_t *zhp, void *data)
5010 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5012 cb->first = B_FALSE;
5014 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5016 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5019 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5020 &records, &numrecords) == 0);
5021 for (i = 0; i < numrecords; i++) {
5022 nvlist_t *rec = records[i];
5025 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5029 tsec = fnvlist_lookup_uint64(records[i],
5031 (void) localtime_r(&tsec, &t);
5032 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5035 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5036 (void) printf("%s %s", tbuf,
5037 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5038 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5040 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5043 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5044 (void) printf("%s unrecognized record:\n",
5046 dump_nvlist(rec, 4);
5049 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5050 zfs_history_event_names[ievent],
5051 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5052 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5053 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5056 (void) printf("%s [txg:%lld] %s", tbuf,
5057 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5058 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5059 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5060 (void) printf(" %s (%llu)",
5061 fnvlist_lookup_string(rec,
5063 fnvlist_lookup_uint64(rec,
5066 (void) printf(" %s", fnvlist_lookup_string(rec,
5067 ZPOOL_HIST_INT_STR));
5068 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5071 (void) printf("%s ioctl %s\n", tbuf,
5072 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5073 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5074 (void) printf(" input:\n");
5075 dump_nvlist(fnvlist_lookup_nvlist(rec,
5076 ZPOOL_HIST_INPUT_NVL), 8);
5078 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5079 (void) printf(" output:\n");
5080 dump_nvlist(fnvlist_lookup_nvlist(rec,
5081 ZPOOL_HIST_OUTPUT_NVL), 8);
5086 (void) printf("%s unrecognized record:\n", tbuf);
5087 dump_nvlist(rec, 4);
5091 (void) printf("\n");
5094 (void) printf(" [");
5095 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5096 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5097 struct passwd *pwd = getpwuid(who);
5098 (void) printf("user %d ", (int)who);
5100 (void) printf("(%s) ", pwd->pw_name);
5102 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5103 (void) printf("on %s",
5104 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5106 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5107 (void) printf(":%s",
5108 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5111 (void) printf("\n");
5113 (void) printf("\n");
5120 * zpool history <pool>
5122 * Displays the history of commands that modified pools.
5125 zpool_do_history(int argc, char **argv)
5127 hist_cbdata_t cbdata = { 0 };
5131 cbdata.first = B_TRUE;
5133 while ((c = getopt(argc, argv, "li")) != -1) {
5136 cbdata.longfmt = B_TRUE;
5139 cbdata.internal = B_TRUE;
5142 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5150 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5153 if (argc == 0 && cbdata.first == B_TRUE) {
5154 (void) printf(gettext("no pools available\n"));
5162 get_callback(zpool_handle_t *zhp, void *data)
5164 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5165 char value[MAXNAMELEN];
5166 zprop_source_t srctype;
5169 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5172 * Skip the special fake placeholder. This will also skip
5173 * over the name property when 'all' is specified.
5175 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5176 pl == cbp->cb_proplist)
5179 if (pl->pl_prop == ZPROP_INVAL &&
5180 (zpool_prop_feature(pl->pl_user_prop) ||
5181 zpool_prop_unsupported(pl->pl_user_prop))) {
5182 srctype = ZPROP_SRC_LOCAL;
5184 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5185 value, sizeof (value)) == 0) {
5186 zprop_print_one_property(zpool_get_name(zhp),
5187 cbp, pl->pl_user_prop, value, srctype,
5191 if (zpool_get_prop(zhp, pl->pl_prop, value,
5192 sizeof (value), &srctype) != 0)
5195 zprop_print_one_property(zpool_get_name(zhp), cbp,
5196 zpool_prop_to_name(pl->pl_prop), value, srctype,
5204 zpool_do_get(int argc, char **argv)
5206 zprop_get_cbdata_t cb = { 0 };
5207 zprop_list_t fake_name = { 0 };
5211 (void) fprintf(stderr, gettext("missing property "
5216 cb.cb_first = B_TRUE;
5217 cb.cb_sources = ZPROP_SRC_ALL;
5218 cb.cb_columns[0] = GET_COL_NAME;
5219 cb.cb_columns[1] = GET_COL_PROPERTY;
5220 cb.cb_columns[2] = GET_COL_VALUE;
5221 cb.cb_columns[3] = GET_COL_SOURCE;
5222 cb.cb_type = ZFS_TYPE_POOL;
5224 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5225 ZFS_TYPE_POOL) != 0)
5228 if (cb.cb_proplist != NULL) {
5229 fake_name.pl_prop = ZPOOL_PROP_NAME;
5230 fake_name.pl_width = strlen(gettext("NAME"));
5231 fake_name.pl_next = cb.cb_proplist;
5232 cb.cb_proplist = &fake_name;
5235 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5238 if (cb.cb_proplist == &fake_name)
5239 zprop_free_list(fake_name.pl_next);
5241 zprop_free_list(cb.cb_proplist);
5246 typedef struct set_cbdata {
5249 boolean_t cb_any_successful;
5253 set_callback(zpool_handle_t *zhp, void *data)
5256 set_cbdata_t *cb = (set_cbdata_t *)data;
5258 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5261 cb->cb_any_successful = B_TRUE;
5267 zpool_do_set(int argc, char **argv)
5269 set_cbdata_t cb = { 0 };
5272 if (argc > 1 && argv[1][0] == '-') {
5273 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5279 (void) fprintf(stderr, gettext("missing property=value "
5285 (void) fprintf(stderr, gettext("missing pool name\n"));
5290 (void) fprintf(stderr, gettext("too many pool names\n"));
5294 cb.cb_propname = argv[1];
5295 cb.cb_value = strchr(cb.cb_propname, '=');
5296 if (cb.cb_value == NULL) {
5297 (void) fprintf(stderr, gettext("missing value in "
5298 "property=value argument\n"));
5302 *(cb.cb_value) = '\0';
5305 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5312 find_command_idx(char *command, int *idx)
5316 for (i = 0; i < NCOMMAND; i++) {
5317 if (command_table[i].name == NULL)
5320 if (strcmp(command, command_table[i].name) == 0) {
5329 main(int argc, char **argv)
5335 (void) setlocale(LC_ALL, "");
5336 (void) textdomain(TEXT_DOMAIN);
5338 if ((g_zfs = libzfs_init()) == NULL) {
5339 (void) fprintf(stderr, gettext("internal error: failed to "
5340 "initialize ZFS library\n"));
5344 libzfs_print_on_error(g_zfs, B_TRUE);
5349 * Make sure the user has specified some command.
5352 (void) fprintf(stderr, gettext("missing command\n"));
5361 if (strcmp(cmdname, "-?") == 0)
5364 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5367 * Run the appropriate command.
5369 if (find_command_idx(cmdname, &i) == 0) {
5370 current_command = &command_table[i];
5371 ret = command_table[i].func(argc - 1, argv + 1);
5372 } else if (strchr(cmdname, '=')) {
5373 verify(find_command_idx("set", &i) == 0);
5374 current_command = &command_table[i];
5375 ret = command_table[i].func(argc, argv);
5376 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5378 * 'freeze' is a vile debugging abomination, so we treat
5381 zfs_cmd_t zc = { 0 };
5382 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5383 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5385 (void) fprintf(stderr, gettext("unrecognized "
5386 "command '%s'\n"), cmdname);
5390 if (ret == 0 && log_history)
5391 (void) zpool_log_history(g_zfs, history_str);
5396 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5397 * for the purposes of running ::findleaks.
5399 if (getenv("ZFS_ABORT") != NULL) {
5400 (void) printf("dumping core by request\n");