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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
46 #include <sys/fs/zfs.h>
52 #include "zpool_util.h"
53 #include "zfs_comutil.h"
55 static int zpool_do_create(int, char **);
56 static int zpool_do_destroy(int, char **);
58 static int zpool_do_add(int, char **);
59 static int zpool_do_remove(int, char **);
61 static int zpool_do_list(int, char **);
62 static int zpool_do_iostat(int, char **);
63 static int zpool_do_status(int, char **);
65 static int zpool_do_online(int, char **);
66 static int zpool_do_offline(int, char **);
67 static int zpool_do_clear(int, char **);
69 static int zpool_do_attach(int, char **);
70 static int zpool_do_detach(int, char **);
71 static int zpool_do_replace(int, char **);
73 static int zpool_do_scrub(int, char **);
75 static int zpool_do_import(int, char **);
76 static int zpool_do_export(int, char **);
78 static int zpool_do_upgrade(int, char **);
80 static int zpool_do_history(int, char **);
82 static int zpool_do_get(int, char **);
83 static int zpool_do_set(int, char **);
86 * These libumem hooks provide a reasonable set of defaults for the allocator's
87 * debugging facilities.
92 _umem_debug_init(void)
94 return ("default,verbose"); /* $UMEM_DEBUG setting */
98 _umem_logging_init(void)
100 return ("fail,contents"); /* $UMEM_LOGGING setting */
128 typedef struct zpool_command {
130 int (*func)(int, char **);
135 * Master command table. Each ZFS command has a name, associated function, and
136 * usage message. The usage messages need to be internationalized, so we have
137 * to have a function to return the usage message based on a command index.
139 * These commands are organized according to how they are displayed in the usage
140 * message. An empty command (one with a NULL name) indicates an empty line in
141 * the generic usage message.
143 static zpool_command_t command_table[] = {
144 { "create", zpool_do_create, HELP_CREATE },
145 { "destroy", zpool_do_destroy, HELP_DESTROY },
147 { "add", zpool_do_add, HELP_ADD },
148 { "remove", zpool_do_remove, HELP_REMOVE },
150 { "list", zpool_do_list, HELP_LIST },
151 { "iostat", zpool_do_iostat, HELP_IOSTAT },
152 { "status", zpool_do_status, HELP_STATUS },
154 { "online", zpool_do_online, HELP_ONLINE },
155 { "offline", zpool_do_offline, HELP_OFFLINE },
156 { "clear", zpool_do_clear, HELP_CLEAR },
158 { "attach", zpool_do_attach, HELP_ATTACH },
159 { "detach", zpool_do_detach, HELP_DETACH },
160 { "replace", zpool_do_replace, HELP_REPLACE },
162 { "scrub", zpool_do_scrub, HELP_SCRUB },
164 { "import", zpool_do_import, HELP_IMPORT },
165 { "export", zpool_do_export, HELP_EXPORT },
166 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
168 { "history", zpool_do_history, HELP_HISTORY },
169 { "get", zpool_do_get, HELP_GET },
170 { "set", zpool_do_set, HELP_SET },
173 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
175 zpool_command_t *current_command;
176 static char history_str[HIS_MAX_RECORD_LEN];
179 get_usage(zpool_help_t idx) {
182 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
184 return (gettext("\tattach [-f] <pool> <device> "
187 return (gettext("\tclear <pool> [device]\n"));
189 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
190 "\t [-O file-system-property=value] ... \n"
191 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
193 return (gettext("\tdestroy [-f] <pool>\n"));
195 return (gettext("\tdetach <pool> <device>\n"));
197 return (gettext("\texport [-f] <pool> ...\n"));
199 return (gettext("\thistory [-il] [<pool>] ...\n"));
201 return (gettext("\timport [-d dir] [-D]\n"
202 "\timport [-o mntopts] [-o property=value] ... \n"
203 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] -a\n"
204 "\timport [-o mntopts] [-o property=value] ... \n"
205 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] "
206 "<pool | id> [newpool]\n"));
208 return (gettext("\tiostat [-v] [pool] ... [interval "
211 return (gettext("\tlist [-H] [-o property[,...]] "
214 return (gettext("\toffline [-t] <pool> <device> ...\n"));
216 return (gettext("\tonline <pool> <device> ...\n"));
218 return (gettext("\treplace [-f] <pool> <device> "
221 return (gettext("\tremove <pool> <device> ...\n"));
223 return (gettext("\tscrub [-s] <pool> ...\n"));
225 return (gettext("\tstatus [-vx] [pool] ...\n"));
227 return (gettext("\tupgrade\n"
229 "\tupgrade [-V version] <-a | pool ...>\n"));
231 return (gettext("\tget <\"all\" | property[,...]> "
234 return (gettext("\tset <property=value> <pool> \n"));
243 * Callback routine that will print out a pool property value.
246 print_prop_cb(int prop, void *cb)
250 (void) fprintf(fp, "\t%-13s ", zpool_prop_to_name(prop));
252 if (zpool_prop_readonly(prop))
253 (void) fprintf(fp, " NO ");
255 (void) fprintf(fp, " YES ");
257 if (zpool_prop_values(prop) == NULL)
258 (void) fprintf(fp, "-\n");
260 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
266 * Display usage message. If we're inside a command, display only the usage for
267 * that command. Otherwise, iterate over the entire command table and display
268 * a complete usage message.
271 usage(boolean_t requested)
273 FILE *fp = requested ? stdout : stderr;
275 if (current_command == NULL) {
278 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
280 gettext("where 'command' is one of the following:\n\n"));
282 for (i = 0; i < NCOMMAND; i++) {
283 if (command_table[i].name == NULL)
284 (void) fprintf(fp, "\n");
286 (void) fprintf(fp, "%s",
287 get_usage(command_table[i].usage));
290 (void) fprintf(fp, gettext("usage:\n"));
291 (void) fprintf(fp, "%s", get_usage(current_command->usage));
294 if (current_command != NULL &&
295 ((strcmp(current_command->name, "set") == 0) ||
296 (strcmp(current_command->name, "get") == 0) ||
297 (strcmp(current_command->name, "list") == 0))) {
300 gettext("\nthe following properties are supported:\n"));
302 (void) fprintf(fp, "\n\t%-13s %s %s\n\n",
303 "PROPERTY", "EDIT", "VALUES");
305 /* Iterate over all properties */
306 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
311 * See comments at end of main().
313 if (getenv("ZFS_ABORT") != NULL) {
314 (void) printf("dumping core by request\n");
318 exit(requested ? 0 : 2);
322 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
323 boolean_t print_logs)
330 (void) printf("\t%*s%s\n", indent, "", name);
332 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
333 &child, &children) != 0)
336 for (c = 0; c < children; c++) {
337 uint64_t is_log = B_FALSE;
339 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
341 if ((is_log && !print_logs) || (!is_log && print_logs))
344 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
345 print_vdev_tree(zhp, vname, child[c], indent + 2,
352 * Add a property pair (name, string-value) into a property nvlist.
355 add_prop_list(const char *propname, char *propval, nvlist_t **props,
358 zpool_prop_t prop = ZPROP_INVAL;
364 if (*props == NULL &&
365 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
366 (void) fprintf(stderr,
367 gettext("internal error: out of memory\n"));
374 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
375 (void) fprintf(stderr, gettext("property '%s' is "
376 "not a valid pool property\n"), propname);
379 normnm = zpool_prop_to_name(prop);
381 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
382 normnm = zfs_prop_to_name(fprop);
388 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
389 prop != ZPOOL_PROP_CACHEFILE) {
390 (void) fprintf(stderr, gettext("property '%s' "
391 "specified multiple times\n"), propname);
395 if (nvlist_add_string(proplist, normnm, propval) != 0) {
396 (void) fprintf(stderr, gettext("internal "
397 "error: out of memory\n"));
405 * zpool add [-fn] <pool> <vdev> ...
407 * -f Force addition of devices, even if they appear in use
408 * -n Do not add the devices, but display the resulting layout if
409 * they were to be added.
411 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
412 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
416 zpool_do_add(int argc, char **argv)
418 boolean_t force = B_FALSE;
419 boolean_t dryrun = B_FALSE;
428 while ((c = getopt(argc, argv, "fn")) != -1) {
437 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
446 /* get pool name and check number of arguments */
448 (void) fprintf(stderr, gettext("missing pool name argument\n"));
452 (void) fprintf(stderr, gettext("missing vdev specification\n"));
461 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
464 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
465 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
471 /* pass off to get_vdev_spec for processing */
472 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
474 if (nvroot == NULL) {
480 nvlist_t *poolnvroot;
482 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
485 (void) printf(gettext("would update '%s' to the following "
486 "configuration:\n"), zpool_get_name(zhp));
488 /* print original main pool and new tree */
489 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
490 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
492 /* Do the same for the logs */
493 if (num_logs(poolnvroot) > 0) {
494 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
495 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
496 } else if (num_logs(nvroot) > 0) {
497 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
502 ret = (zpool_add(zhp, nvroot) != 0);
512 * zpool remove <pool> <vdev> ...
514 * Removes the given vdev from the pool. Currently, this only supports removing
515 * spares and cache devices from the pool. Eventually, we'll want to support
516 * removing leaf vdevs (as an alias for 'detach') as well as toplevel vdevs.
519 zpool_do_remove(int argc, char **argv)
528 /* get pool name and check number of arguments */
530 (void) fprintf(stderr, gettext("missing pool name argument\n"));
534 (void) fprintf(stderr, gettext("missing device\n"));
540 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
543 for (i = 1; i < argc; i++) {
544 if (zpool_vdev_remove(zhp, argv[i]) != 0)
552 * zpool create [-fn] [-o property=value] ...
553 * [-O file-system-property=value] ...
554 * [-R root] [-m mountpoint] <pool> <dev> ...
556 * -f Force creation, even if devices appear in use
557 * -n Do not create the pool, but display the resulting layout if it
558 * were to be created.
559 * -R Create a pool under an alternate root
560 * -m Set default mountpoint for the root dataset. By default it's
562 * -o Set property=value.
563 * -O Set fsproperty=value in the pool's root file system
565 * Creates the named pool according to the given vdev specification. The
566 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
567 * we get the nvlist back from get_vdev_spec(), we either print out the contents
568 * (if '-n' was specified), or pass it to libzfs to do the creation.
571 zpool_do_create(int argc, char **argv)
573 boolean_t force = B_FALSE;
574 boolean_t dryrun = B_FALSE;
576 nvlist_t *nvroot = NULL;
579 char *altroot = NULL;
580 char *mountpoint = NULL;
581 nvlist_t *fsprops = NULL;
582 nvlist_t *props = NULL;
586 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
596 if (add_prop_list(zpool_prop_to_name(
597 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
599 if (nvlist_lookup_string(props,
600 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
603 if (add_prop_list(zpool_prop_to_name(
604 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
611 if ((propval = strchr(optarg, '=')) == NULL) {
612 (void) fprintf(stderr, gettext("missing "
613 "'=' for -o option\n"));
619 if (add_prop_list(optarg, propval, &props, B_TRUE))
623 if ((propval = strchr(optarg, '=')) == NULL) {
624 (void) fprintf(stderr, gettext("missing "
625 "'=' for -O option\n"));
631 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
635 (void) fprintf(stderr, gettext("missing argument for "
636 "'%c' option\n"), optopt);
639 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
648 /* get pool name and check number of arguments */
650 (void) fprintf(stderr, gettext("missing pool name argument\n"));
654 (void) fprintf(stderr, gettext("missing vdev specification\n"));
661 * As a special case, check for use of '/' in the name, and direct the
662 * user to use 'zfs create' instead.
664 if (strchr(poolname, '/') != NULL) {
665 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
666 "character '/' in pool name\n"), poolname);
667 (void) fprintf(stderr, gettext("use 'zfs create' to "
668 "create a dataset\n"));
672 /* pass off to get_vdev_spec for bulk processing */
673 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
678 /* make_root_vdev() allows 0 toplevel children if there are spares */
679 if (!zfs_allocatable_devs(nvroot)) {
680 (void) fprintf(stderr, gettext("invalid vdev "
681 "specification: at least one toplevel vdev must be "
687 if (altroot != NULL && altroot[0] != '/') {
688 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
689 "must be an absolute path\n"), altroot);
694 * Check the validity of the mountpoint and direct the user to use the
695 * '-m' mountpoint option if it looks like its in use.
697 if (mountpoint == NULL ||
698 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
699 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
700 char buf[MAXPATHLEN];
703 if (mountpoint && mountpoint[0] != '/') {
704 (void) fprintf(stderr, gettext("invalid mountpoint "
705 "'%s': must be an absolute path, 'legacy', or "
706 "'none'\n"), mountpoint);
710 if (mountpoint == NULL) {
712 (void) snprintf(buf, sizeof (buf), "%s/%s",
715 (void) snprintf(buf, sizeof (buf), "/%s",
719 (void) snprintf(buf, sizeof (buf), "%s%s",
720 altroot, mountpoint);
722 (void) snprintf(buf, sizeof (buf), "%s",
726 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
727 (void) fprintf(stderr, gettext("mountpoint '%s' : "
728 "%s\n"), buf, strerror(errno));
729 (void) fprintf(stderr, gettext("use '-m' "
730 "option to provide a different default\n"));
735 while (count < 3 && readdir(dirp) != NULL)
737 (void) closedir(dirp);
740 (void) fprintf(stderr, gettext("mountpoint "
741 "'%s' exists and is not empty\n"), buf);
742 (void) fprintf(stderr, gettext("use '-m' "
743 "option to provide a "
744 "different default\n"));
752 * For a dry run invocation, print out a basic message and run
753 * through all the vdevs in the list and print out in an
754 * appropriate hierarchy.
756 (void) printf(gettext("would create '%s' with the "
757 "following layout:\n\n"), poolname);
759 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
760 if (num_logs(nvroot) > 0)
761 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
766 * Hand off to libzfs.
768 if (zpool_create(g_zfs, poolname,
769 nvroot, props, fsprops) == 0) {
770 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
771 ZFS_TYPE_FILESYSTEM);
773 if (mountpoint != NULL)
774 verify(zfs_prop_set(pool,
776 ZFS_PROP_MOUNTPOINT),
778 if (zfs_mount(pool, NULL, 0) == 0)
779 ret = zfs_shareall(pool);
782 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
783 (void) fprintf(stderr, gettext("pool name may have "
790 nvlist_free(fsprops);
794 nvlist_free(fsprops);
801 * zpool destroy <pool>
803 * -f Forcefully unmount any datasets
805 * Destroy the given pool. Automatically unmounts any datasets in the pool.
808 zpool_do_destroy(int argc, char **argv)
810 boolean_t force = B_FALSE;
817 while ((c = getopt(argc, argv, "f")) != -1) {
823 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
832 /* check arguments */
834 (void) fprintf(stderr, gettext("missing pool argument\n"));
838 (void) fprintf(stderr, gettext("too many arguments\n"));
844 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
846 * As a special case, check for use of '/' in the name, and
847 * direct the user to use 'zfs destroy' instead.
849 if (strchr(pool, '/') != NULL)
850 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
851 "destroy a dataset\n"));
855 if (zpool_disable_datasets(zhp, force) != 0) {
856 (void) fprintf(stderr, gettext("could not destroy '%s': "
857 "could not unmount datasets\n"), zpool_get_name(zhp));
861 ret = (zpool_destroy(zhp) != 0);
869 * zpool export [-f] <pool> ...
871 * -f Forcefully unmount datasets
873 * Export the given pools. By default, the command will attempt to cleanly
874 * unmount any active datasets within the pool. If the '-f' flag is specified,
875 * then the datasets will be forcefully unmounted.
878 zpool_do_export(int argc, char **argv)
880 boolean_t force = B_FALSE;
881 boolean_t hardforce = B_FALSE;
888 while ((c = getopt(argc, argv, "fF")) != -1) {
897 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
906 /* check arguments */
908 (void) fprintf(stderr, gettext("missing pool argument\n"));
913 for (i = 0; i < argc; i++) {
914 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
919 if (zpool_disable_datasets(zhp, force) != 0) {
926 if (zpool_export_force(zhp) != 0)
928 } else if (zpool_export(zhp, force) != 0) {
939 * Given a vdev configuration, determine the maximum width needed for the device
943 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
945 char *name = zpool_vdev_name(g_zfs, zhp, nv);
950 if (strlen(name) + depth > max)
951 max = strlen(name) + depth;
955 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
956 &child, &children) == 0) {
957 for (c = 0; c < children; c++)
958 if ((ret = max_width(zhp, child[c], depth + 2,
963 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
964 &child, &children) == 0) {
965 for (c = 0; c < children; c++)
966 if ((ret = max_width(zhp, child[c], depth + 2,
971 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
972 &child, &children) == 0) {
973 for (c = 0; c < children; c++)
974 if ((ret = max_width(zhp, child[c], depth + 2,
985 * Print the configuration of an exported pool. Iterate over all vdevs in the
986 * pool, printing out the name and status for each one.
989 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth,
990 boolean_t print_logs)
997 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
998 if (strcmp(type, VDEV_TYPE_MISSING) == 0)
1001 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
1002 (uint64_t **)&vs, &c) == 0);
1004 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1005 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1007 if (vs->vs_aux != 0) {
1010 switch (vs->vs_aux) {
1011 case VDEV_AUX_OPEN_FAILED:
1012 (void) printf(gettext("cannot open"));
1015 case VDEV_AUX_BAD_GUID_SUM:
1016 (void) printf(gettext("missing device"));
1019 case VDEV_AUX_NO_REPLICAS:
1020 (void) printf(gettext("insufficient replicas"));
1023 case VDEV_AUX_VERSION_NEWER:
1024 (void) printf(gettext("newer version"));
1027 case VDEV_AUX_ERR_EXCEEDED:
1028 (void) printf(gettext("too many errors"));
1032 (void) printf(gettext("corrupted data"));
1036 (void) printf("\n");
1038 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1039 &child, &children) != 0)
1042 for (c = 0; c < children; c++) {
1043 uint64_t is_log = B_FALSE;
1045 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1047 if ((is_log && !print_logs) || (!is_log && print_logs))
1050 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1051 print_import_config(vname, child[c],
1052 namewidth, depth + 2, B_FALSE);
1056 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1057 &child, &children) == 0) {
1058 (void) printf(gettext("\tcache\n"));
1059 for (c = 0; c < children; c++) {
1060 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1061 (void) printf("\t %s\n", vname);
1066 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1067 &child, &children) == 0) {
1068 (void) printf(gettext("\tspares\n"));
1069 for (c = 0; c < children; c++) {
1070 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1071 (void) printf("\t %s\n", vname);
1078 * Display the status for the given pool.
1081 show_import(nvlist_t *config)
1083 uint64_t pool_state;
1094 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1096 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1098 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1100 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1103 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
1104 (uint64_t **)&vs, &vsc) == 0);
1105 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1107 reason = zpool_import_status(config, &msgid);
1109 (void) printf(gettext(" pool: %s\n"), name);
1110 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1111 (void) printf(gettext(" state: %s"), health);
1112 if (pool_state == POOL_STATE_DESTROYED)
1113 (void) printf(gettext(" (DESTROYED)"));
1114 (void) printf("\n");
1117 case ZPOOL_STATUS_MISSING_DEV_R:
1118 case ZPOOL_STATUS_MISSING_DEV_NR:
1119 case ZPOOL_STATUS_BAD_GUID_SUM:
1120 (void) printf(gettext("status: One or more devices are missing "
1121 "from the system.\n"));
1124 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1125 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1126 (void) printf(gettext("status: One or more devices contains "
1127 "corrupted data.\n"));
1130 case ZPOOL_STATUS_CORRUPT_DATA:
1131 (void) printf(gettext("status: The pool data is corrupted.\n"));
1134 case ZPOOL_STATUS_OFFLINE_DEV:
1135 (void) printf(gettext("status: One or more devices "
1136 "are offlined.\n"));
1139 case ZPOOL_STATUS_CORRUPT_POOL:
1140 (void) printf(gettext("status: The pool metadata is "
1144 case ZPOOL_STATUS_VERSION_OLDER:
1145 (void) printf(gettext("status: The pool is formatted using an "
1146 "older on-disk version.\n"));
1149 case ZPOOL_STATUS_VERSION_NEWER:
1150 (void) printf(gettext("status: The pool is formatted using an "
1151 "incompatible version.\n"));
1154 case ZPOOL_STATUS_HOSTID_MISMATCH:
1155 (void) printf(gettext("status: The pool was last accessed by "
1156 "another system.\n"));
1159 case ZPOOL_STATUS_FAULTED_DEV_R:
1160 case ZPOOL_STATUS_FAULTED_DEV_NR:
1161 (void) printf(gettext("status: One or more devices are "
1165 case ZPOOL_STATUS_BAD_LOG:
1166 (void) printf(gettext("status: An intent log record cannot be "
1172 * No other status can be seen when importing pools.
1174 assert(reason == ZPOOL_STATUS_OK);
1178 * Print out an action according to the overall state of the pool.
1180 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1181 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1182 (void) printf(gettext("action: The pool can be "
1183 "imported using its name or numeric identifier, "
1184 "though\n\tsome features will not be available "
1185 "without an explicit 'zpool upgrade'.\n"));
1186 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1187 (void) printf(gettext("action: The pool can be "
1188 "imported using its name or numeric "
1189 "identifier and\n\tthe '-f' flag.\n"));
1191 (void) printf(gettext("action: The pool can be "
1192 "imported using its name or numeric "
1194 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1195 (void) printf(gettext("action: The pool can be imported "
1196 "despite missing or damaged devices. The\n\tfault "
1197 "tolerance of the pool may be compromised if imported.\n"));
1200 case ZPOOL_STATUS_VERSION_NEWER:
1201 (void) printf(gettext("action: The pool cannot be "
1202 "imported. Access the pool on a system running "
1203 "newer\n\tsoftware, or recreate the pool from "
1206 case ZPOOL_STATUS_MISSING_DEV_R:
1207 case ZPOOL_STATUS_MISSING_DEV_NR:
1208 case ZPOOL_STATUS_BAD_GUID_SUM:
1209 (void) printf(gettext("action: The pool cannot be "
1210 "imported. Attach the missing\n\tdevices and try "
1214 (void) printf(gettext("action: The pool cannot be "
1215 "imported due to damaged devices or data.\n"));
1220 * If the state is "closed" or "can't open", and the aux state
1221 * is "corrupt data":
1223 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1224 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1225 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1226 if (pool_state == POOL_STATE_DESTROYED)
1227 (void) printf(gettext("\tThe pool was destroyed, "
1228 "but can be imported using the '-Df' flags.\n"));
1229 else if (pool_state != POOL_STATE_EXPORTED)
1230 (void) printf(gettext("\tThe pool may be active on "
1231 "another system, but can be imported using\n\t"
1232 "the '-f' flag.\n"));
1236 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1239 (void) printf(gettext("config:\n\n"));
1241 namewidth = max_width(NULL, nvroot, 0, 0);
1245 print_import_config(name, nvroot, namewidth, 0, B_FALSE);
1246 if (num_logs(nvroot) > 0) {
1247 (void) printf(gettext("\tlogs\n"));
1248 print_import_config(name, nvroot, namewidth, 0, B_TRUE);
1251 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1252 (void) printf(gettext("\n\tAdditional devices are known to "
1253 "be part of this pool, though their\n\texact "
1254 "configuration cannot be determined.\n"));
1259 * Perform the import for the given configuration. This passes the heavy
1260 * lifting off to zpool_import_props(), and then mounts the datasets contained
1264 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1265 int force, nvlist_t *props, boolean_t do_verbatim)
1267 zpool_handle_t *zhp;
1273 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1276 verify(nvlist_lookup_uint64(config,
1277 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1278 verify(nvlist_lookup_uint64(config,
1279 ZPOOL_CONFIG_VERSION, &version) == 0);
1280 if (version > SPA_VERSION) {
1281 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1282 "is formatted using a newer ZFS version\n"), name);
1284 } else if (state != POOL_STATE_EXPORTED && !force) {
1287 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1289 if ((unsigned long)hostid != gethostid()) {
1294 verify(nvlist_lookup_string(config,
1295 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1296 verify(nvlist_lookup_uint64(config,
1297 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1299 (void) fprintf(stderr, gettext("cannot import "
1300 "'%s': pool may be in use from other "
1301 "system, it was last accessed by %s "
1302 "(hostid: 0x%lx) on %s"), name, hostname,
1303 (unsigned long)hostid,
1304 asctime(localtime(&t)));
1305 (void) fprintf(stderr, gettext("use '-f' to "
1306 "import anyway\n"));
1310 (void) fprintf(stderr, gettext("cannot import '%s': "
1311 "pool may be in use from other system\n"), name);
1312 (void) fprintf(stderr, gettext("use '-f' to import "
1318 if (zpool_import_props(g_zfs, config, newname, props, do_verbatim) != 0)
1321 if (newname != NULL)
1322 name = (char *)newname;
1324 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1327 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1328 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1338 * zpool import [-d dir] [-D]
1339 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1340 * [-d dir | -c cachefile] [-f] -a
1341 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1342 * [-d dir | -c cachefile] [-f] <pool | id> [newpool]
1344 * -c Read pool information from a cachefile instead of searching
1347 * -d Scan in a specific directory, other than /dev/dsk. More than
1348 * one directory can be specified using multiple '-d' options.
1350 * -D Scan for previously destroyed pools or import all or only
1351 * specified destroyed pools.
1353 * -R Temporarily import the pool, with all mountpoints relative to
1354 * the given root. The pool will remain exported when the machine
1357 * -f Force import, even if it appears that the pool is active.
1359 * -F Import even in the presence of faulted vdevs. This is an
1360 * intentionally undocumented option for testing purposes, and
1361 * treats the pool configuration as complete, leaving any bad
1362 * vdevs in the FAULTED state. In other words, it does verbatim
1365 * -a Import all pools found.
1367 * -o Set property=value and/or temporary mount options (without '=').
1369 * The import command scans for pools to import, and import pools based on pool
1370 * name and GUID. The pool can also be renamed as part of the import process.
1373 zpool_do_import(int argc, char **argv)
1375 char **searchdirs = NULL;
1379 nvlist_t *pools = NULL;
1380 boolean_t do_all = B_FALSE;
1381 boolean_t do_destroyed = B_FALSE;
1382 char *mntopts = NULL;
1383 boolean_t do_force = B_FALSE;
1386 uint64_t searchguid = 0;
1387 char *searchname = NULL;
1389 nvlist_t *found_config;
1390 nvlist_t *props = NULL;
1392 boolean_t do_verbatim = B_FALSE;
1393 uint64_t pool_state;
1394 char *cachefile = NULL;
1397 while ((c = getopt(argc, argv, ":ac:d:DfFo:p:R:")) != -1) {
1406 if (searchdirs == NULL) {
1407 searchdirs = safe_malloc(sizeof (char *));
1409 char **tmp = safe_malloc((nsearch + 1) *
1411 bcopy(searchdirs, tmp, nsearch *
1416 searchdirs[nsearch++] = optarg;
1419 do_destroyed = B_TRUE;
1425 do_verbatim = B_TRUE;
1428 if ((propval = strchr(optarg, '=')) != NULL) {
1431 if (add_prop_list(optarg, propval,
1439 if (add_prop_list(zpool_prop_to_name(
1440 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1442 if (nvlist_lookup_string(props,
1443 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1446 if (add_prop_list(zpool_prop_to_name(
1447 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1451 (void) fprintf(stderr, gettext("missing argument for "
1452 "'%c' option\n"), optopt);
1456 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1465 if (cachefile && nsearch != 0) {
1466 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1470 if (searchdirs == NULL) {
1471 searchdirs = safe_malloc(sizeof (char *));
1472 searchdirs[0] = "/dev/dsk";
1476 /* check argument count */
1479 (void) fprintf(stderr, gettext("too many arguments\n"));
1484 (void) fprintf(stderr, gettext("too many arguments\n"));
1489 * Check for the SYS_CONFIG privilege. We do this explicitly
1490 * here because otherwise any attempt to discover pools will
1493 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1494 (void) fprintf(stderr, gettext("cannot "
1495 "discover pools: permission denied\n"));
1502 * Depending on the arguments given, we do one of the following:
1504 * <none> Iterate through all pools and display information about
1507 * -a Iterate through all pools and try to import each one.
1509 * <id> Find the pool that corresponds to the given GUID/pool
1510 * name and import that one.
1512 * -D Above options applies only to destroyed pools.
1518 searchguid = strtoull(argv[0], &endptr, 10);
1519 if (errno != 0 || *endptr != '\0')
1520 searchname = argv[0];
1521 found_config = NULL;
1525 pools = zpool_find_import_cached(g_zfs, cachefile, searchname,
1527 } else if (searchname != NULL) {
1528 pools = zpool_find_import_byname(g_zfs, nsearch, searchdirs,
1532 * It's OK to search by guid even if searchguid is 0.
1534 pools = zpool_find_import_byguid(g_zfs, nsearch, searchdirs,
1538 if (pools == NULL) {
1540 (void) fprintf(stderr, gettext("cannot import '%s': "
1541 "no such pool available\n"), argv[0]);
1548 * At this point we have a list of import candidate configs. Even if
1549 * we were searching by pool name or guid, we still need to
1550 * post-process the list to deal with pool state and possible
1556 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1558 verify(nvpair_value_nvlist(elem, &config) == 0);
1560 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1562 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1564 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1571 (void) printf("\n");
1574 err |= do_import(config, NULL, mntopts,
1575 do_force, props, do_verbatim);
1577 show_import(config);
1578 } else if (searchname != NULL) {
1582 * We are searching for a pool based on name.
1584 verify(nvlist_lookup_string(config,
1585 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1587 if (strcmp(name, searchname) == 0) {
1588 if (found_config != NULL) {
1589 (void) fprintf(stderr, gettext(
1590 "cannot import '%s': more than "
1591 "one matching pool\n"), searchname);
1592 (void) fprintf(stderr, gettext(
1593 "import by numeric ID instead\n"));
1596 found_config = config;
1602 * Search for a pool by guid.
1604 verify(nvlist_lookup_uint64(config,
1605 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1607 if (guid == searchguid)
1608 found_config = config;
1613 * If we were searching for a specific pool, verify that we found a
1614 * pool, and then do the import.
1616 if (argc != 0 && err == 0) {
1617 if (found_config == NULL) {
1618 (void) fprintf(stderr, gettext("cannot import '%s': "
1619 "no such pool available\n"), argv[0]);
1622 err |= do_import(found_config, argc == 1 ? NULL :
1623 argv[1], mntopts, do_force, props, do_verbatim);
1628 * If we were just looking for pools, report an error if none were
1631 if (argc == 0 && first)
1632 (void) fprintf(stderr,
1633 gettext("no pools available to import\n"));
1640 return (err ? 1 : 0);
1643 typedef struct iostat_cbdata {
1644 zpool_list_t *cb_list;
1651 print_iostat_separator(iostat_cbdata_t *cb)
1655 for (i = 0; i < cb->cb_namewidth; i++)
1657 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1661 print_iostat_header(iostat_cbdata_t *cb)
1663 (void) printf("%*s capacity operations bandwidth\n",
1664 cb->cb_namewidth, "");
1665 (void) printf("%-*s used avail read write read write\n",
1666 cb->cb_namewidth, "pool");
1667 print_iostat_separator(cb);
1671 * Display a single statistic.
1674 print_one_stat(uint64_t value)
1678 zfs_nicenum(value, buf, sizeof (buf));
1679 (void) printf(" %5s", buf);
1683 * Print out all the statistics for the given vdev. This can either be the
1684 * toplevel configuration, or called recursively. If 'name' is NULL, then this
1685 * is a verbose output, and we don't want to display the toplevel pool stats.
1688 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1689 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1691 nvlist_t **oldchild, **newchild;
1693 vdev_stat_t *oldvs, *newvs;
1694 vdev_stat_t zerovs = { 0 };
1699 if (oldnv != NULL) {
1700 verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
1701 (uint64_t **)&oldvs, &c) == 0);
1706 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
1707 (uint64_t **)&newvs, &c) == 0);
1709 if (strlen(name) + depth > cb->cb_namewidth)
1710 (void) printf("%*s%s", depth, "", name);
1712 (void) printf("%*s%s%*s", depth, "", name,
1713 (int)(cb->cb_namewidth - strlen(name) - depth), "");
1715 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
1720 scale = (double)NANOSEC / tdelta;
1722 /* only toplevel vdevs have capacity stats */
1723 if (newvs->vs_space == 0) {
1724 (void) printf(" - -");
1726 print_one_stat(newvs->vs_alloc);
1727 print_one_stat(newvs->vs_space - newvs->vs_alloc);
1730 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
1731 oldvs->vs_ops[ZIO_TYPE_READ])));
1733 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
1734 oldvs->vs_ops[ZIO_TYPE_WRITE])));
1736 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
1737 oldvs->vs_bytes[ZIO_TYPE_READ])));
1739 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
1740 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
1742 (void) printf("\n");
1744 if (!cb->cb_verbose)
1747 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
1748 &newchild, &children) != 0)
1751 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
1752 &oldchild, &c) != 0)
1755 for (c = 0; c < children; c++) {
1756 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1757 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1758 newchild[c], cb, depth + 2);
1763 * Include level 2 ARC devices in iostat output
1765 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
1766 &newchild, &children) != 0)
1769 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
1770 &oldchild, &c) != 0)
1774 (void) printf("%-*s - - - - - "
1775 "-\n", cb->cb_namewidth, "cache");
1776 for (c = 0; c < children; c++) {
1777 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1778 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1779 newchild[c], cb, depth + 2);
1786 refresh_iostat(zpool_handle_t *zhp, void *data)
1788 iostat_cbdata_t *cb = data;
1792 * If the pool has disappeared, remove it from the list and continue.
1794 if (zpool_refresh_stats(zhp, &missing) != 0)
1798 pool_list_remove(cb->cb_list, zhp);
1804 * Callback to print out the iostats for the given pool.
1807 print_iostat(zpool_handle_t *zhp, void *data)
1809 iostat_cbdata_t *cb = data;
1810 nvlist_t *oldconfig, *newconfig;
1811 nvlist_t *oldnvroot, *newnvroot;
1813 newconfig = zpool_get_config(zhp, &oldconfig);
1815 if (cb->cb_iteration == 1)
1818 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
1821 if (oldconfig == NULL)
1824 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
1828 * Print out the statistics for the pool.
1830 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
1833 print_iostat_separator(cb);
1839 get_namewidth(zpool_handle_t *zhp, void *data)
1841 iostat_cbdata_t *cb = data;
1842 nvlist_t *config, *nvroot;
1844 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
1845 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1847 if (!cb->cb_verbose)
1848 cb->cb_namewidth = strlen(zpool_get_name(zhp));
1850 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
1854 * The width must fall into the range [10,38]. The upper limit is the
1855 * maximum we can have and still fit in 80 columns.
1857 if (cb->cb_namewidth < 10)
1858 cb->cb_namewidth = 10;
1859 if (cb->cb_namewidth > 38)
1860 cb->cb_namewidth = 38;
1866 * zpool iostat [-v] [pool] ... [interval [count]]
1868 * -v Display statistics for individual vdevs
1870 * This command can be tricky because we want to be able to deal with pool
1871 * creation/destruction as well as vdev configuration changes. The bulk of this
1872 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
1873 * on pool_list_update() to detect the addition of new pools. Configuration
1874 * changes are all handled within libzfs.
1877 zpool_do_iostat(int argc, char **argv)
1882 unsigned long interval = 0, count = 0;
1884 boolean_t verbose = B_FALSE;
1888 while ((c = getopt(argc, argv, "v")) != -1) {
1894 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1904 * Determine if the last argument is an integer or a pool name
1906 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1910 interval = strtoul(argv[argc - 1], &end, 10);
1912 if (*end == '\0' && errno == 0) {
1913 if (interval == 0) {
1914 (void) fprintf(stderr, gettext("interval "
1915 "cannot be zero\n"));
1920 * Ignore the last parameter
1925 * If this is not a valid number, just plow on. The
1926 * user will get a more informative error message later
1934 * If the last argument is also an integer, then we have both a count
1937 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1942 interval = strtoul(argv[argc - 1], &end, 10);
1944 if (*end == '\0' && errno == 0) {
1945 if (interval == 0) {
1946 (void) fprintf(stderr, gettext("interval "
1947 "cannot be zero\n"));
1952 * Ignore the last parameter
1961 * Construct the list of all interesting pools.
1964 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
1967 if (pool_list_count(list) == 0 && argc != 0) {
1968 pool_list_free(list);
1972 if (pool_list_count(list) == 0 && interval == 0) {
1973 pool_list_free(list);
1974 (void) fprintf(stderr, gettext("no pools available\n"));
1979 * Enter the main iostat loop.
1982 cb.cb_verbose = verbose;
1983 cb.cb_iteration = 0;
1984 cb.cb_namewidth = 0;
1987 pool_list_update(list);
1989 if ((npools = pool_list_count(list)) == 0)
1993 * Refresh all statistics. This is done as an explicit step
1994 * before calculating the maximum name width, so that any
1995 * configuration changes are properly accounted for.
1997 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2000 * Iterate over all pools to determine the maximum width
2001 * for the pool / device name column across all pools.
2003 cb.cb_namewidth = 0;
2004 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2007 * If it's the first time, or verbose mode, print the header.
2009 if (++cb.cb_iteration == 1 || verbose)
2010 print_iostat_header(&cb);
2012 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2015 * If there's more than one pool, and we're not in verbose mode
2016 * (which prints a separator for us), then print a separator.
2018 if (npools > 1 && !verbose)
2019 print_iostat_separator(&cb);
2022 (void) printf("\n");
2025 * Flush the output so that redirection to a file isn't buffered
2028 (void) fflush(stdout);
2033 if (count != 0 && --count == 0)
2036 (void) sleep(interval);
2039 pool_list_free(list);
2044 typedef struct list_cbdata {
2045 boolean_t cb_scripted;
2047 zprop_list_t *cb_proplist;
2051 * Given a list of columns to display, output appropriate headers for each one.
2054 print_header(zprop_list_t *pl)
2057 boolean_t first = B_TRUE;
2058 boolean_t right_justify;
2060 for (; pl != NULL; pl = pl->pl_next) {
2061 if (pl->pl_prop == ZPROP_INVAL)
2069 header = zpool_prop_column_name(pl->pl_prop);
2070 right_justify = zpool_prop_align_right(pl->pl_prop);
2072 if (pl->pl_next == NULL && !right_justify)
2073 (void) printf("%s", header);
2074 else if (right_justify)
2075 (void) printf("%*s", pl->pl_width, header);
2077 (void) printf("%-*s", pl->pl_width, header);
2080 (void) printf("\n");
2084 * Given a pool and a list of properties, print out all the properties according
2085 * to the described layout.
2088 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2090 boolean_t first = B_TRUE;
2091 char property[ZPOOL_MAXPROPLEN];
2093 boolean_t right_justify;
2096 for (; pl != NULL; pl = pl->pl_next) {
2099 (void) printf("\t");
2106 right_justify = B_FALSE;
2107 if (pl->pl_prop != ZPROP_INVAL) {
2108 if (zpool_get_prop(zhp, pl->pl_prop, property,
2109 sizeof (property), NULL) != 0)
2114 right_justify = zpool_prop_align_right(pl->pl_prop);
2119 width = pl->pl_width;
2122 * If this is being called in scripted mode, or if this is the
2123 * last column and it is left-justified, don't include a width
2126 if (scripted || (pl->pl_next == NULL && !right_justify))
2127 (void) printf("%s", propstr);
2128 else if (right_justify)
2129 (void) printf("%*s", width, propstr);
2131 (void) printf("%-*s", width, propstr);
2134 (void) printf("\n");
2138 * Generic callback function to list a pool.
2141 list_callback(zpool_handle_t *zhp, void *data)
2143 list_cbdata_t *cbp = data;
2145 if (cbp->cb_first) {
2146 if (!cbp->cb_scripted)
2147 print_header(cbp->cb_proplist);
2148 cbp->cb_first = B_FALSE;
2151 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2157 * zpool list [-H] [-o prop[,prop]*] [pool] ...
2159 * -H Scripted mode. Don't display headers, and separate properties
2161 * -o List of properties to display. Defaults to
2162 * "name,size,used,available,capacity,health,altroot"
2164 * List all pools in the system, whether or not they're healthy. Output space
2165 * statistics for each one, as well as health status summary.
2168 zpool_do_list(int argc, char **argv)
2172 list_cbdata_t cb = { 0 };
2173 static char default_props[] =
2174 "name,size,used,available,capacity,health,altroot";
2175 char *props = default_props;
2178 while ((c = getopt(argc, argv, ":Ho:")) != -1) {
2181 cb.cb_scripted = B_TRUE;
2187 (void) fprintf(stderr, gettext("missing argument for "
2188 "'%c' option\n"), optopt);
2192 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2201 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2204 cb.cb_first = B_TRUE;
2206 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2207 list_callback, &cb);
2209 zprop_free_list(cb.cb_proplist);
2211 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2212 (void) printf(gettext("no pools available\n"));
2220 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2227 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2228 &child, &children) != 0) {
2229 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2230 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV)-1) == 0)
2231 name += sizeof(_PATH_DEV)-1;
2232 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV)-1) == 0)
2233 path += sizeof(_PATH_DEV)-1;
2234 if (strcmp(name, path) == 0)
2239 for (c = 0; c < children; c++)
2240 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2247 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2249 boolean_t force = B_FALSE;
2252 char *poolname, *old_disk, *new_disk;
2253 zpool_handle_t *zhp;
2257 while ((c = getopt(argc, argv, "f")) != -1) {
2263 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2272 /* get pool name and check number of arguments */
2274 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2281 (void) fprintf(stderr,
2282 gettext("missing <device> specification\n"));
2290 (void) fprintf(stderr,
2291 gettext("missing <new_device> specification\n"));
2294 new_disk = old_disk;
2304 (void) fprintf(stderr, gettext("too many arguments\n"));
2308 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2311 if (zpool_get_config(zhp, NULL) == NULL) {
2312 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2318 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2320 if (nvroot == NULL) {
2325 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2327 nvlist_free(nvroot);
2334 * zpool replace [-f] <pool> <device> <new_device>
2336 * -f Force attach, even if <new_device> appears to be in use.
2338 * Replace <device> with <new_device>.
2342 zpool_do_replace(int argc, char **argv)
2344 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2348 * zpool attach [-f] <pool> <device> <new_device>
2350 * -f Force attach, even if <new_device> appears to be in use.
2352 * Attach <new_device> to the mirror containing <device>. If <device> is not
2353 * part of a mirror, then <device> will be transformed into a mirror of
2354 * <device> and <new_device>. In either case, <new_device> will begin life
2355 * with a DTL of [0, now], and will immediately begin to resilver itself.
2358 zpool_do_attach(int argc, char **argv)
2360 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2364 * zpool detach [-f] <pool> <device>
2366 * -f Force detach of <device>, even if DTLs argue against it
2367 * (not supported yet)
2369 * Detach a device from a mirror. The operation will be refused if <device>
2370 * is the last device in the mirror, or if the DTLs indicate that this device
2371 * has the only valid copy of some data.
2375 zpool_do_detach(int argc, char **argv)
2378 char *poolname, *path;
2379 zpool_handle_t *zhp;
2383 while ((c = getopt(argc, argv, "f")) != -1) {
2387 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2396 /* get pool name and check number of arguments */
2398 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2403 (void) fprintf(stderr,
2404 gettext("missing <device> specification\n"));
2411 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2414 ret = zpool_vdev_detach(zhp, path);
2422 * zpool online <pool> <device> ...
2425 zpool_do_online(int argc, char **argv)
2429 zpool_handle_t *zhp;
2431 vdev_state_t newstate;
2434 while ((c = getopt(argc, argv, "t")) != -1) {
2438 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2447 /* get pool name and check number of arguments */
2449 (void) fprintf(stderr, gettext("missing pool name\n"));
2453 (void) fprintf(stderr, gettext("missing device name\n"));
2459 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2462 for (i = 1; i < argc; i++) {
2463 if (zpool_vdev_online(zhp, argv[i], 0, &newstate) == 0) {
2464 if (newstate != VDEV_STATE_HEALTHY) {
2465 (void) printf(gettext("warning: device '%s' "
2466 "onlined, but remains in faulted state\n"),
2468 if (newstate == VDEV_STATE_FAULTED)
2469 (void) printf(gettext("use 'zpool "
2470 "clear' to restore a faulted "
2473 (void) printf(gettext("use 'zpool "
2474 "replace' to replace devices "
2475 "that are no longer present\n"));
2488 * zpool offline [-ft] <pool> <device> ...
2490 * -f Force the device into the offline state, even if doing
2491 * so would appear to compromise pool availability.
2492 * (not supported yet)
2494 * -t Only take the device off-line temporarily. The offline
2495 * state will not be persistent across reboots.
2499 zpool_do_offline(int argc, char **argv)
2503 zpool_handle_t *zhp;
2505 boolean_t istmp = B_FALSE;
2508 while ((c = getopt(argc, argv, "ft")) != -1) {
2515 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2524 /* get pool name and check number of arguments */
2526 (void) fprintf(stderr, gettext("missing pool name\n"));
2530 (void) fprintf(stderr, gettext("missing device name\n"));
2536 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2539 for (i = 1; i < argc; i++) {
2540 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
2550 * zpool clear <pool> [device]
2552 * Clear all errors associated with a pool or a particular device.
2555 zpool_do_clear(int argc, char **argv)
2558 zpool_handle_t *zhp;
2559 char *pool, *device;
2562 (void) fprintf(stderr, gettext("missing pool name\n"));
2567 (void) fprintf(stderr, gettext("too many arguments\n"));
2572 device = argc == 3 ? argv[2] : NULL;
2574 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
2577 if (zpool_clear(zhp, device) != 0)
2585 typedef struct scrub_cbdata {
2592 scrub_callback(zpool_handle_t *zhp, void *data)
2594 scrub_cbdata_t *cb = data;
2598 * Ignore faulted pools.
2600 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2601 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
2602 "currently unavailable\n"), zpool_get_name(zhp));
2606 err = zpool_scrub(zhp, cb->cb_type);
2612 * zpool scrub [-s] <pool> ...
2614 * -s Stop. Stops any in-progress scrub.
2617 zpool_do_scrub(int argc, char **argv)
2622 cb.cb_type = POOL_SCRUB_EVERYTHING;
2625 while ((c = getopt(argc, argv, "s")) != -1) {
2628 cb.cb_type = POOL_SCRUB_NONE;
2631 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2643 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2647 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
2650 typedef struct status_cbdata {
2652 boolean_t cb_allpools;
2653 boolean_t cb_verbose;
2654 boolean_t cb_explain;
2659 * Print out detailed scrub status.
2662 print_scrub_status(nvlist_t *nvroot)
2666 time_t start, end, now;
2667 double fraction_done;
2668 uint64_t examined, total, minutes_left, minutes_taken;
2671 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
2672 (uint64_t **)&vs, &vsc) == 0);
2675 * If there's never been a scrub, there's not much to say.
2677 if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
2678 (void) printf(gettext("none requested\n"));
2682 scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2683 "resilver" : "scrub";
2685 start = vs->vs_scrub_start;
2686 end = vs->vs_scrub_end;
2688 examined = vs->vs_scrub_examined;
2689 total = vs->vs_alloc;
2692 minutes_taken = (uint64_t)((end - start) / 60);
2694 (void) printf(gettext("%s %s after %lluh%um with %llu errors "
2696 scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
2697 (u_longlong_t)(minutes_taken / 60),
2698 (uint_t)(minutes_taken % 60),
2699 (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
2705 if (examined > total)
2708 fraction_done = (double)examined / total;
2709 minutes_left = (uint64_t)((now - start) *
2710 (1 - fraction_done) / fraction_done / 60);
2711 minutes_taken = (uint64_t)((now - start) / 60);
2713 (void) printf(gettext("%s in progress for %lluh%um, %.2f%% done, "
2714 "%lluh%um to go\n"),
2715 scrub_type, (u_longlong_t)(minutes_taken / 60),
2716 (uint_t)(minutes_taken % 60), 100 * fraction_done,
2717 (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
2720 typedef struct spare_cbdata {
2722 zpool_handle_t *cb_zhp;
2726 find_vdev(nvlist_t *nv, uint64_t search)
2732 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2736 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2737 &child, &children) == 0) {
2738 for (c = 0; c < children; c++)
2739 if (find_vdev(child[c], search))
2747 find_spare(zpool_handle_t *zhp, void *data)
2749 spare_cbdata_t *cbp = data;
2750 nvlist_t *config, *nvroot;
2752 config = zpool_get_config(zhp, NULL);
2753 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2756 if (find_vdev(nvroot, cbp->cb_guid)) {
2766 * Print out configuration state as requested by status_callback.
2769 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2770 int namewidth, int depth, boolean_t isspare)
2775 char rbuf[6], wbuf[6], cbuf[6], repaired[7];
2777 uint64_t notpresent;
2781 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2782 (uint64_t **)&vs, &c) == 0);
2784 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2785 &child, &children) != 0)
2788 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2791 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2794 if (vs->vs_aux == VDEV_AUX_SPARED)
2796 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2800 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
2804 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2805 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2806 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
2807 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
2810 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2811 ¬present) == 0) {
2813 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2814 (void) printf(" was %s", path);
2815 } else if (vs->vs_aux != 0) {
2818 switch (vs->vs_aux) {
2819 case VDEV_AUX_OPEN_FAILED:
2820 (void) printf(gettext("cannot open"));
2823 case VDEV_AUX_BAD_GUID_SUM:
2824 (void) printf(gettext("missing device"));
2827 case VDEV_AUX_NO_REPLICAS:
2828 (void) printf(gettext("insufficient replicas"));
2831 case VDEV_AUX_VERSION_NEWER:
2832 (void) printf(gettext("newer version"));
2835 case VDEV_AUX_SPARED:
2836 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2838 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
2839 if (strcmp(zpool_get_name(cb.cb_zhp),
2840 zpool_get_name(zhp)) == 0)
2841 (void) printf(gettext("currently in "
2844 (void) printf(gettext("in use by "
2846 zpool_get_name(cb.cb_zhp));
2847 zpool_close(cb.cb_zhp);
2849 (void) printf(gettext("currently in use"));
2853 case VDEV_AUX_ERR_EXCEEDED:
2854 (void) printf(gettext("too many errors"));
2857 case VDEV_AUX_IO_FAILURE:
2858 (void) printf(gettext("experienced I/O failures"));
2861 case VDEV_AUX_BAD_LOG:
2862 (void) printf(gettext("bad intent log"));
2866 (void) printf(gettext("corrupted data"));
2869 } else if (vs->vs_scrub_repaired != 0 && children == 0) {
2871 * Report bytes resilvered/repaired on leaf devices.
2873 zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
2874 (void) printf(gettext(" %s %s"), repaired,
2875 (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2876 "resilvered" : "repaired");
2879 (void) printf("\n");
2881 for (c = 0; c < children; c++) {
2882 uint64_t is_log = B_FALSE;
2884 /* Don't print logs here */
2885 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2889 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
2890 print_status_config(zhp, vname, child[c],
2891 namewidth, depth + 2, isspare);
2897 print_error_log(zpool_handle_t *zhp)
2899 nvlist_t *nverrlist = NULL;
2902 size_t len = MAXPATHLEN * 2;
2904 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
2905 (void) printf("errors: List of errors unavailable "
2906 "(insufficient privileges)\n");
2910 (void) printf("errors: Permanent errors have been "
2911 "detected in the following files:\n\n");
2913 pathname = safe_malloc(len);
2915 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
2917 uint64_t dsobj, obj;
2919 verify(nvpair_value_nvlist(elem, &nv) == 0);
2920 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
2922 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
2924 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
2925 (void) printf("%7s %s\n", "", pathname);
2928 nvlist_free(nverrlist);
2932 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
2941 (void) printf(gettext("\tspares\n"));
2943 for (i = 0; i < nspares; i++) {
2944 name = zpool_vdev_name(g_zfs, zhp, spares[i]);
2945 print_status_config(zhp, name, spares[i],
2946 namewidth, 2, B_TRUE);
2952 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
2961 (void) printf(gettext("\tcache\n"));
2963 for (i = 0; i < nl2cache; i++) {
2964 name = zpool_vdev_name(g_zfs, zhp, l2cache[i]);
2965 print_status_config(zhp, name, l2cache[i],
2966 namewidth, 2, B_FALSE);
2973 * Logs are recorded as top level vdevs in the main pool child array but with
2974 * "is_log" set to 1. We use print_status_config() to print the top level logs
2975 * then any log children (eg mirrored slogs) are printed recursively - which
2976 * works because only the top level vdev is marked "is_log"
2979 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth)
2984 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
2988 (void) printf(gettext("\tlogs\n"));
2990 for (c = 0; c < children; c++) {
2991 uint64_t is_log = B_FALSE;
2994 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2998 name = zpool_vdev_name(g_zfs, zhp, child[c]);
2999 print_status_config(zhp, name, child[c], namewidth, 2, B_FALSE);
3005 * Display a summary of pool status. Displays a summary such as:
3009 * reason: One or more devices ...
3010 * see: http://www.sun.com/msg/ZFS-xxxx-01
3016 * When given the '-v' option, we print out the complete config. If the '-e'
3017 * option is specified, then we print out error rate information as well.
3020 status_callback(zpool_handle_t *zhp, void *data)
3022 status_cbdata_t *cbp = data;
3023 nvlist_t *config, *nvroot;
3030 config = zpool_get_config(zhp, NULL);
3031 reason = zpool_get_status(zhp, &msgid);
3036 * If we were given 'zpool status -x', only report those pools with
3039 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3040 if (!cbp->cb_allpools) {
3041 (void) printf(gettext("pool '%s' is healthy\n"),
3042 zpool_get_name(zhp));
3044 cbp->cb_first = B_FALSE;
3050 cbp->cb_first = B_FALSE;
3052 (void) printf("\n");
3054 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3056 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
3057 (uint64_t **)&vs, &c) == 0);
3058 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3060 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3061 (void) printf(gettext(" state: %s\n"), health);
3064 case ZPOOL_STATUS_MISSING_DEV_R:
3065 (void) printf(gettext("status: One or more devices could not "
3066 "be opened. Sufficient replicas exist for\n\tthe pool to "
3067 "continue functioning in a degraded state.\n"));
3068 (void) printf(gettext("action: Attach the missing device and "
3069 "online it using 'zpool online'.\n"));
3072 case ZPOOL_STATUS_MISSING_DEV_NR:
3073 (void) printf(gettext("status: One or more devices could not "
3074 "be opened. There are insufficient\n\treplicas for the "
3075 "pool to continue functioning.\n"));
3076 (void) printf(gettext("action: Attach the missing device and "
3077 "online it using 'zpool online'.\n"));
3080 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3081 (void) printf(gettext("status: One or more devices could not "
3082 "be used because the label is missing or\n\tinvalid. "
3083 "Sufficient replicas exist for the pool to continue\n\t"
3084 "functioning in a degraded state.\n"));
3085 (void) printf(gettext("action: Replace the device using "
3086 "'zpool replace'.\n"));
3089 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3090 (void) printf(gettext("status: One or more devices could not "
3091 "be used because the label is missing \n\tor invalid. "
3092 "There are insufficient replicas for the pool to "
3093 "continue\n\tfunctioning.\n"));
3094 (void) printf(gettext("action: Destroy and re-create the pool "
3095 "from a backup source.\n"));
3098 case ZPOOL_STATUS_FAILING_DEV:
3099 (void) printf(gettext("status: One or more devices has "
3100 "experienced an unrecoverable error. An\n\tattempt was "
3101 "made to correct the error. Applications are "
3103 (void) printf(gettext("action: Determine if the device needs "
3104 "to be replaced, and clear the errors\n\tusing "
3105 "'zpool clear' or replace the device with 'zpool "
3109 case ZPOOL_STATUS_OFFLINE_DEV:
3110 (void) printf(gettext("status: One or more devices has "
3111 "been taken offline by the administrator.\n\tSufficient "
3112 "replicas exist for the pool to continue functioning in "
3113 "a\n\tdegraded state.\n"));
3114 (void) printf(gettext("action: Online the device using "
3115 "'zpool online' or replace the device with\n\t'zpool "
3119 case ZPOOL_STATUS_RESILVERING:
3120 (void) printf(gettext("status: One or more devices is "
3121 "currently being resilvered. The pool will\n\tcontinue "
3122 "to function, possibly in a degraded state.\n"));
3123 (void) printf(gettext("action: Wait for the resilver to "
3127 case ZPOOL_STATUS_CORRUPT_DATA:
3128 (void) printf(gettext("status: One or more devices has "
3129 "experienced an error resulting in data\n\tcorruption. "
3130 "Applications may be affected.\n"));
3131 (void) printf(gettext("action: Restore the file in question "
3132 "if possible. Otherwise restore the\n\tentire pool from "
3136 case ZPOOL_STATUS_CORRUPT_POOL:
3137 (void) printf(gettext("status: The pool metadata is corrupted "
3138 "and the pool cannot be opened.\n"));
3139 (void) printf(gettext("action: Destroy and re-create the pool "
3140 "from a backup source.\n"));
3143 case ZPOOL_STATUS_VERSION_OLDER:
3144 (void) printf(gettext("status: The pool is formatted using an "
3145 "older on-disk format. The pool can\n\tstill be used, but "
3146 "some features are unavailable.\n"));
3147 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3148 "upgrade'. Once this is done, the\n\tpool will no longer "
3149 "be accessible on older software versions.\n"));
3152 case ZPOOL_STATUS_VERSION_NEWER:
3153 (void) printf(gettext("status: The pool has been upgraded to a "
3154 "newer, incompatible on-disk version.\n\tThe pool cannot "
3155 "be accessed on this system.\n"));
3156 (void) printf(gettext("action: Access the pool from a system "
3157 "running more recent software, or\n\trestore the pool from "
3161 case ZPOOL_STATUS_FAULTED_DEV_R:
3162 (void) printf(gettext("status: One or more devices are "
3163 "faulted in response to persistent errors.\n\tSufficient "
3164 "replicas exist for the pool to continue functioning "
3165 "in a\n\tdegraded state.\n"));
3166 (void) printf(gettext("action: Replace the faulted device, "
3167 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3170 case ZPOOL_STATUS_FAULTED_DEV_NR:
3171 (void) printf(gettext("status: One or more devices are "
3172 "faulted in response to persistent errors. There are "
3173 "insufficient replicas for the pool to\n\tcontinue "
3175 (void) printf(gettext("action: Destroy and re-create the pool "
3176 "from a backup source. Manually marking the device\n"
3177 "\trepaired using 'zpool clear' may allow some data "
3178 "to be recovered.\n"));
3181 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3182 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3183 (void) printf(gettext("status: One or more devices are "
3184 "faulted in response to IO failures.\n"));
3185 (void) printf(gettext("action: Make sure the affected devices "
3186 "are connected, then run 'zpool clear'.\n"));
3189 case ZPOOL_STATUS_BAD_LOG:
3190 (void) printf(gettext("status: An intent log record "
3191 "could not be read.\n"
3192 "\tWaiting for adminstrator intervention to fix the "
3193 "faulted pool.\n"));
3194 (void) printf(gettext("action: Either restore the affected "
3195 "device(s) and run 'zpool online',\n"
3196 "\tor ignore the intent log records by running "
3197 "'zpool clear'.\n"));
3202 * The remaining errors can't actually be generated, yet.
3204 assert(reason == ZPOOL_STATUS_OK);
3208 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3211 if (config != NULL) {
3214 nvlist_t **spares, **l2cache;
3215 uint_t nspares, nl2cache;
3218 (void) printf(gettext(" scrub: "));
3219 print_scrub_status(nvroot);
3221 namewidth = max_width(zhp, nvroot, 0, 0);
3225 (void) printf(gettext("config:\n\n"));
3226 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3227 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3228 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3229 namewidth, 0, B_FALSE);
3231 if (num_logs(nvroot) > 0)
3232 print_logs(zhp, nvroot, namewidth);
3233 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3234 &l2cache, &nl2cache) == 0)
3235 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3237 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3238 &spares, &nspares) == 0)
3239 print_spares(zhp, spares, nspares, namewidth);
3241 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3243 nvlist_t *nverrlist = NULL;
3246 * If the approximate error count is small, get a
3247 * precise count by fetching the entire log and
3248 * uniquifying the results.
3250 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3251 zpool_get_errlog(zhp, &nverrlist) == 0) {
3256 while ((elem = nvlist_next_nvpair(nverrlist,
3261 nvlist_free(nverrlist);
3263 (void) printf("\n");
3266 (void) printf(gettext("errors: No known data "
3268 else if (!cbp->cb_verbose)
3269 (void) printf(gettext("errors: %llu data "
3270 "errors, use '-v' for a list\n"),
3271 (u_longlong_t)nerr);
3273 print_error_log(zhp);
3276 (void) printf(gettext("config: The configuration cannot be "
3284 * zpool status [-vx] [pool] ...
3286 * -v Display complete error logs
3287 * -x Display only pools with potential problems
3289 * Describes the health status of all pools or some subset.
3292 zpool_do_status(int argc, char **argv)
3296 status_cbdata_t cb = { 0 };
3299 while ((c = getopt(argc, argv, "vx")) != -1) {
3302 cb.cb_verbose = B_TRUE;
3305 cb.cb_explain = B_TRUE;
3308 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3317 cb.cb_first = B_TRUE;
3320 cb.cb_allpools = B_TRUE;
3322 ret = for_each_pool(argc, argv, B_TRUE, NULL, status_callback, &cb);
3324 if (argc == 0 && cb.cb_count == 0)
3325 (void) printf(gettext("no pools available\n"));
3326 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3327 (void) printf(gettext("all pools are healthy\n"));
3332 typedef struct upgrade_cbdata {
3336 char cb_poolname[ZPOOL_MAXNAMELEN];
3338 uint64_t cb_version;
3343 is_root_pool(zpool_handle_t *zhp)
3345 static struct statfs sfs;
3346 static char *poolname = NULL;
3347 static boolean_t stated = B_FALSE;
3352 if (statfs("/", &sfs) == -1) {
3353 (void) fprintf(stderr,
3354 "Unable to stat root file system: %s.\n",
3358 if (strcmp(sfs.f_fstypename, "zfs") != 0)
3360 poolname = sfs.f_mntfromname;
3361 if ((slash = strchr(poolname, '/')) != NULL)
3365 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
3369 upgrade_cb(zpool_handle_t *zhp, void *arg)
3371 upgrade_cbdata_t *cbp = arg;
3376 config = zpool_get_config(zhp, NULL);
3377 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3380 if (!cbp->cb_newer && version < SPA_VERSION) {
3382 if (cbp->cb_first) {
3383 (void) printf(gettext("The following pools are "
3384 "out of date, and can be upgraded. After "
3385 "being\nupgraded, these pools will no "
3386 "longer be accessible by older software "
3388 (void) printf(gettext("VER POOL\n"));
3389 (void) printf(gettext("--- ------------\n"));
3390 cbp->cb_first = B_FALSE;
3393 (void) printf("%2llu %s\n", (u_longlong_t)version,
3394 zpool_get_name(zhp));
3396 cbp->cb_first = B_FALSE;
3397 ret = zpool_upgrade(zhp, cbp->cb_version);
3399 (void) printf(gettext("Successfully upgraded "
3400 "'%s'\n\n"), zpool_get_name(zhp));
3401 if (cbp->cb_poolname[0] == '\0' &&
3402 is_root_pool(zhp)) {
3403 (void) strlcpy(cbp->cb_poolname,
3404 zpool_get_name(zhp),
3405 sizeof(cbp->cb_poolname));
3409 } else if (cbp->cb_newer && version > SPA_VERSION) {
3410 assert(!cbp->cb_all);
3412 if (cbp->cb_first) {
3413 (void) printf(gettext("The following pools are "
3414 "formatted using a newer software version and\n"
3415 "cannot be accessed on the current system.\n\n"));
3416 (void) printf(gettext("VER POOL\n"));
3417 (void) printf(gettext("--- ------------\n"));
3418 cbp->cb_first = B_FALSE;
3421 (void) printf("%2llu %s\n", (u_longlong_t)version,
3422 zpool_get_name(zhp));
3431 upgrade_one(zpool_handle_t *zhp, void *data)
3433 upgrade_cbdata_t *cbp = data;
3434 uint64_t cur_version;
3437 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3438 (void) printf(gettext("'log' is now a reserved word\n"
3439 "Pool 'log' must be renamed using export and import"
3444 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3445 if (cur_version > cbp->cb_version) {
3446 (void) printf(gettext("Pool '%s' is already formatted "
3447 "using more current version '%llu'.\n"),
3448 zpool_get_name(zhp), cur_version);
3451 if (cur_version == cbp->cb_version) {
3452 (void) printf(gettext("Pool '%s' is already formatted "
3453 "using the current version.\n"), zpool_get_name(zhp));
3457 ret = zpool_upgrade(zhp, cbp->cb_version);
3460 (void) printf(gettext("Successfully upgraded '%s' "
3461 "from version %llu to version %llu\n\n"),
3462 zpool_get_name(zhp), (u_longlong_t)cur_version,
3463 (u_longlong_t)cbp->cb_version);
3464 if (cbp->cb_poolname[0] == '\0' && is_root_pool(zhp)) {
3465 (void) strlcpy(cbp->cb_poolname, zpool_get_name(zhp),
3466 sizeof(cbp->cb_poolname));
3476 * zpool upgrade [-V version] <-a | pool ...>
3478 * With no arguments, display downrev'd ZFS pool available for upgrade.
3479 * Individual pools can be upgraded by specifying the pool, and '-a' will
3480 * upgrade all pools.
3483 zpool_do_upgrade(int argc, char **argv)
3486 upgrade_cbdata_t cb = { 0 };
3488 boolean_t showversions = B_FALSE;
3493 while ((c = getopt(argc, argv, "avV:")) != -1) {
3499 showversions = B_TRUE;
3502 cb.cb_version = strtoll(optarg, &end, 10);
3503 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3504 cb.cb_version < SPA_VERSION_1) {
3505 (void) fprintf(stderr,
3506 gettext("invalid version '%s'\n"), optarg);
3511 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3522 if (cb.cb_version == 0) {
3523 cb.cb_version = SPA_VERSION;
3524 } else if (!cb.cb_all && argc == 0) {
3525 (void) fprintf(stderr, gettext("-V option is "
3526 "incompatible with other arguments\n"));
3531 if (cb.cb_all || argc != 0) {
3532 (void) fprintf(stderr, gettext("-v option is "
3533 "incompatible with other arguments\n"));
3536 } else if (cb.cb_all) {
3538 (void) fprintf(stderr, gettext("-a option should not "
3539 "be used along with a pool name\n"));
3544 (void) printf(gettext("This system is currently running "
3545 "ZFS pool version %llu.\n\n"), SPA_VERSION);
3546 cb.cb_first = B_TRUE;
3548 (void) printf(gettext("The following versions are "
3550 (void) printf(gettext("VER DESCRIPTION\n"));
3551 (void) printf("--- -----------------------------------------"
3552 "---------------\n");
3553 (void) printf(gettext(" 1 Initial ZFS version\n"));
3554 (void) printf(gettext(" 2 Ditto blocks "
3555 "(replicated metadata)\n"));
3556 (void) printf(gettext(" 3 Hot spares and double parity "
3558 (void) printf(gettext(" 4 zpool history\n"));
3559 (void) printf(gettext(" 5 Compression using the gzip "
3561 (void) printf(gettext(" 6 bootfs pool property\n"));
3562 (void) printf(gettext(" 7 Separate intent log devices\n"));
3563 (void) printf(gettext(" 8 Delegated administration\n"));
3564 (void) printf(gettext(" 9 refquota and refreservation "
3566 (void) printf(gettext(" 10 Cache devices\n"));
3567 (void) printf(gettext(" 11 Improved scrub performance\n"));
3568 (void) printf(gettext(" 12 Snapshot properties\n"));
3569 (void) printf(gettext(" 13 snapused property\n"));
3570 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
3571 (void) printf(gettext(" 15 user/group space accounting\n"));
3572 (void) printf(gettext("For more information on a particular "
3573 "version, including supported releases, see:\n\n"));
3574 (void) printf("http://www.opensolaris.org/os/community/zfs/"
3576 (void) printf(gettext("Where 'N' is the version number.\n"));
3577 } else if (argc == 0) {
3580 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3581 notfound = cb.cb_first;
3583 if (!cb.cb_all && ret == 0) {
3585 (void) printf("\n");
3586 cb.cb_first = B_TRUE;
3587 cb.cb_newer = B_TRUE;
3588 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3591 (void) printf("\n");
3597 (void) printf(gettext("All pools are formatted "
3598 "using this version.\n"));
3599 else if (!cb.cb_all)
3600 (void) printf(gettext("Use 'zpool upgrade -v' "
3601 "for a list of available versions and "
3602 "their associated\nfeatures.\n"));
3605 ret = for_each_pool(argc, argv, B_FALSE, NULL,
3609 if (cb.cb_poolname[0] != '\0') {
3611 "If you boot from pool '%s', don't forget to update boot code.\n"
3612 "Assuming you use GPT partitioning and da0 is your boot disk\n"
3613 "the following command will do it:\n"
3615 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
3622 typedef struct hist_cbdata {
3628 char *hist_event_table[LOG_END] = {
3644 "pool property set",
3648 "destroy_begin_sync",
3652 "permission update",
3653 "permission remove",
3654 "permission who remove",
3663 "filesystem version upgrade",
3665 "refreservation set",
3670 * Print out the command history for a specific pool.
3673 get_history_one(zpool_handle_t *zhp, void *data)
3689 char internalstr[MAXPATHLEN];
3690 hist_cbdata_t *cb = (hist_cbdata_t *)data;
3694 cb->first = B_FALSE;
3696 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
3698 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
3701 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
3702 &records, &numrecords) == 0);
3703 for (i = 0; i < numrecords; i++) {
3704 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
3708 /* is it an internal event or a standard event? */
3709 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
3711 if (cb->internal == 0)
3714 if (nvlist_lookup_uint64(records[i],
3715 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
3717 verify(nvlist_lookup_uint64(records[i],
3718 ZPOOL_HIST_TXG, &txg) == 0);
3719 verify(nvlist_lookup_string(records[i],
3720 ZPOOL_HIST_INT_STR, &pathstr) == 0);
3721 if (ievent >= LOG_END)
3723 (void) snprintf(internalstr,
3724 sizeof (internalstr),
3725 "[internal %s txg:%lld] %s",
3726 hist_event_table[ievent], txg,
3728 cmdstr = internalstr;
3731 (void) localtime_r(&tsec, &t);
3732 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
3733 (void) printf("%s %s", tbuf, cmdstr);
3736 (void) printf("\n");
3739 (void) printf(" [");
3740 if (nvlist_lookup_uint64(records[i],
3741 ZPOOL_HIST_WHO, &who) == 0) {
3742 pwd = getpwuid((uid_t)who);
3744 (void) printf("user %s on",
3747 (void) printf("user %d on",
3750 (void) printf(gettext("no info]\n"));
3753 if (nvlist_lookup_string(records[i],
3754 ZPOOL_HIST_HOST, &hostname) == 0) {
3755 (void) printf(" %s", hostname);
3757 if (nvlist_lookup_string(records[i],
3758 ZPOOL_HIST_ZONE, &zonename) == 0) {
3759 (void) printf(":%s", zonename);
3763 (void) printf("\n");
3765 (void) printf("\n");
3772 * zpool history <pool>
3774 * Displays the history of commands that modified pools.
3779 zpool_do_history(int argc, char **argv)
3781 hist_cbdata_t cbdata = { 0 };
3785 cbdata.first = B_TRUE;
3787 while ((c = getopt(argc, argv, "li")) != -1) {
3793 cbdata.internal = 1;
3796 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3804 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
3807 if (argc == 0 && cbdata.first == B_TRUE) {
3808 (void) printf(gettext("no pools available\n"));
3816 get_callback(zpool_handle_t *zhp, void *data)
3818 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
3819 char value[MAXNAMELEN];
3820 zprop_source_t srctype;
3823 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
3826 * Skip the special fake placeholder. This will also skip
3827 * over the name property when 'all' is specified.
3829 if (pl->pl_prop == ZPOOL_PROP_NAME &&
3830 pl == cbp->cb_proplist)
3833 if (zpool_get_prop(zhp, pl->pl_prop,
3834 value, sizeof (value), &srctype) != 0)
3837 zprop_print_one_property(zpool_get_name(zhp), cbp,
3838 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL);
3844 zpool_do_get(int argc, char **argv)
3846 zprop_get_cbdata_t cb = { 0 };
3847 zprop_list_t fake_name = { 0 };
3853 cb.cb_first = B_TRUE;
3854 cb.cb_sources = ZPROP_SRC_ALL;
3855 cb.cb_columns[0] = GET_COL_NAME;
3856 cb.cb_columns[1] = GET_COL_PROPERTY;
3857 cb.cb_columns[2] = GET_COL_VALUE;
3858 cb.cb_columns[3] = GET_COL_SOURCE;
3859 cb.cb_type = ZFS_TYPE_POOL;
3861 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
3862 ZFS_TYPE_POOL) != 0)
3865 if (cb.cb_proplist != NULL) {
3866 fake_name.pl_prop = ZPOOL_PROP_NAME;
3867 fake_name.pl_width = strlen(gettext("NAME"));
3868 fake_name.pl_next = cb.cb_proplist;
3869 cb.cb_proplist = &fake_name;
3872 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
3875 if (cb.cb_proplist == &fake_name)
3876 zprop_free_list(fake_name.pl_next);
3878 zprop_free_list(cb.cb_proplist);
3883 typedef struct set_cbdata {
3886 boolean_t cb_any_successful;
3890 set_callback(zpool_handle_t *zhp, void *data)
3893 set_cbdata_t *cb = (set_cbdata_t *)data;
3895 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
3898 cb->cb_any_successful = B_TRUE;
3904 zpool_do_set(int argc, char **argv)
3906 set_cbdata_t cb = { 0 };
3909 if (argc > 1 && argv[1][0] == '-') {
3910 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3916 (void) fprintf(stderr, gettext("missing property=value "
3922 (void) fprintf(stderr, gettext("missing pool name\n"));
3927 (void) fprintf(stderr, gettext("too many pool names\n"));
3931 cb.cb_propname = argv[1];
3932 cb.cb_value = strchr(cb.cb_propname, '=');
3933 if (cb.cb_value == NULL) {
3934 (void) fprintf(stderr, gettext("missing value in "
3935 "property=value argument\n"));
3939 *(cb.cb_value) = '\0';
3942 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
3949 find_command_idx(char *command, int *idx)
3953 for (i = 0; i < NCOMMAND; i++) {
3954 if (command_table[i].name == NULL)
3957 if (strcmp(command, command_table[i].name) == 0) {
3966 main(int argc, char **argv)
3972 (void) setlocale(LC_ALL, "");
3973 (void) textdomain(TEXT_DOMAIN);
3975 if ((g_zfs = libzfs_init()) == NULL) {
3976 (void) fprintf(stderr, gettext("internal error: failed to "
3977 "initialize ZFS library\n"));
3981 libzfs_print_on_error(g_zfs, B_TRUE);
3986 * Make sure the user has specified some command.
3989 (void) fprintf(stderr, gettext("missing command\n"));
3998 if (strcmp(cmdname, "-?") == 0)
4001 zpool_set_history_str("zpool", argc, argv, history_str);
4002 verify(zpool_stage_history(g_zfs, history_str) == 0);
4005 * Run the appropriate command.
4007 if (find_command_idx(cmdname, &i) == 0) {
4008 current_command = &command_table[i];
4009 ret = command_table[i].func(argc - 1, argv + 1);
4010 } else if (strchr(cmdname, '=')) {
4011 verify(find_command_idx("set", &i) == 0);
4012 current_command = &command_table[i];
4013 ret = command_table[i].func(argc, argv);
4014 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4016 * 'freeze' is a vile debugging abomination, so we treat
4020 int fd = open(ZFS_DEV, O_RDWR);
4021 (void) strcpy((void *)buf, argv[2]);
4022 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4024 (void) fprintf(stderr, gettext("unrecognized "
4025 "command '%s'\n"), cmdname);
4032 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4033 * for the purposes of running ::findleaks.
4035 if (getenv("ZFS_ABORT") != NULL) {
4036 (void) printf("dumping core by request\n");