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 2008 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 (void) fprintf(stderr, gettext("property '%s' is "
383 "not a valid file system property\n"), propname);
386 normnm = zfs_prop_to_name(fprop);
389 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
390 prop != ZPOOL_PROP_CACHEFILE) {
391 (void) fprintf(stderr, gettext("property '%s' "
392 "specified multiple times\n"), propname);
396 if (nvlist_add_string(proplist, normnm, propval) != 0) {
397 (void) fprintf(stderr, gettext("internal "
398 "error: out of memory\n"));
406 * zpool add [-fn] <pool> <vdev> ...
408 * -f Force addition of devices, even if they appear in use
409 * -n Do not add the devices, but display the resulting layout if
410 * they were to be added.
412 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
413 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
417 zpool_do_add(int argc, char **argv)
419 boolean_t force = B_FALSE;
420 boolean_t dryrun = B_FALSE;
429 while ((c = getopt(argc, argv, "fn")) != -1) {
438 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
447 /* get pool name and check number of arguments */
449 (void) fprintf(stderr, gettext("missing pool name argument\n"));
453 (void) fprintf(stderr, gettext("missing vdev specification\n"));
462 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
465 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
466 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
472 /* pass off to get_vdev_spec for processing */
473 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
475 if (nvroot == NULL) {
481 nvlist_t *poolnvroot;
483 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
486 (void) printf(gettext("would update '%s' to the following "
487 "configuration:\n"), zpool_get_name(zhp));
489 /* print original main pool and new tree */
490 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
491 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
493 /* Do the same for the logs */
494 if (num_logs(poolnvroot) > 0) {
495 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
496 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
497 } else if (num_logs(nvroot) > 0) {
498 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
503 ret = (zpool_add(zhp, nvroot) != 0);
513 * zpool remove <pool> <vdev> ...
515 * Removes the given vdev from the pool. Currently, this only supports removing
516 * spares and cache devices from the pool. Eventually, we'll want to support
517 * removing leaf vdevs (as an alias for 'detach') as well as toplevel vdevs.
520 zpool_do_remove(int argc, char **argv)
529 /* get pool name and check number of arguments */
531 (void) fprintf(stderr, gettext("missing pool name argument\n"));
535 (void) fprintf(stderr, gettext("missing device\n"));
541 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
544 for (i = 1; i < argc; i++) {
545 if (zpool_vdev_remove(zhp, argv[i]) != 0)
553 * zpool create [-fn] [-o property=value] ...
554 * [-O file-system-property=value] ...
555 * [-R root] [-m mountpoint] <pool> <dev> ...
557 * -f Force creation, even if devices appear in use
558 * -n Do not create the pool, but display the resulting layout if it
559 * were to be created.
560 * -R Create a pool under an alternate root
561 * -m Set default mountpoint for the root dataset. By default it's
563 * -o Set property=value.
564 * -O Set fsproperty=value in the pool's root file system
566 * Creates the named pool according to the given vdev specification. The
567 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
568 * we get the nvlist back from get_vdev_spec(), we either print out the contents
569 * (if '-n' was specified), or pass it to libzfs to do the creation.
572 zpool_do_create(int argc, char **argv)
574 boolean_t force = B_FALSE;
575 boolean_t dryrun = B_FALSE;
577 nvlist_t *nvroot = NULL;
580 char *altroot = NULL;
581 char *mountpoint = NULL;
582 nvlist_t *fsprops = NULL;
583 nvlist_t *props = NULL;
587 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
597 if (add_prop_list(zpool_prop_to_name(
598 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
600 if (nvlist_lookup_string(props,
601 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
604 if (add_prop_list(zpool_prop_to_name(
605 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
612 if ((propval = strchr(optarg, '=')) == NULL) {
613 (void) fprintf(stderr, gettext("missing "
614 "'=' for -o option\n"));
620 if (add_prop_list(optarg, propval, &props, B_TRUE))
624 if ((propval = strchr(optarg, '=')) == NULL) {
625 (void) fprintf(stderr, gettext("missing "
626 "'=' for -O option\n"));
632 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
636 (void) fprintf(stderr, gettext("missing argument for "
637 "'%c' option\n"), optopt);
640 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
649 /* get pool name and check number of arguments */
651 (void) fprintf(stderr, gettext("missing pool name argument\n"));
655 (void) fprintf(stderr, gettext("missing vdev specification\n"));
662 * As a special case, check for use of '/' in the name, and direct the
663 * user to use 'zfs create' instead.
665 if (strchr(poolname, '/') != NULL) {
666 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
667 "character '/' in pool name\n"), poolname);
668 (void) fprintf(stderr, gettext("use 'zfs create' to "
669 "create a dataset\n"));
673 /* pass off to get_vdev_spec for bulk processing */
674 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
679 /* make_root_vdev() allows 0 toplevel children if there are spares */
680 if (!zfs_allocatable_devs(nvroot)) {
681 (void) fprintf(stderr, gettext("invalid vdev "
682 "specification: at least one toplevel vdev must be "
688 if (altroot != NULL && altroot[0] != '/') {
689 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
690 "must be an absolute path\n"), altroot);
695 * Check the validity of the mountpoint and direct the user to use the
696 * '-m' mountpoint option if it looks like its in use.
698 if (mountpoint == NULL ||
699 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
700 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
701 char buf[MAXPATHLEN];
704 if (mountpoint && mountpoint[0] != '/') {
705 (void) fprintf(stderr, gettext("invalid mountpoint "
706 "'%s': must be an absolute path, 'legacy', or "
707 "'none'\n"), mountpoint);
711 if (mountpoint == NULL) {
713 (void) snprintf(buf, sizeof (buf), "%s/%s",
716 (void) snprintf(buf, sizeof (buf), "/%s",
720 (void) snprintf(buf, sizeof (buf), "%s%s",
721 altroot, mountpoint);
723 (void) snprintf(buf, sizeof (buf), "%s",
727 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
728 (void) fprintf(stderr, gettext("mountpoint '%s' : "
729 "%s\n"), buf, strerror(errno));
730 (void) fprintf(stderr, gettext("use '-m' "
731 "option to provide a different default\n"));
736 while (count < 3 && readdir(dirp) != NULL)
738 (void) closedir(dirp);
741 (void) fprintf(stderr, gettext("mountpoint "
742 "'%s' exists and is not empty\n"), buf);
743 (void) fprintf(stderr, gettext("use '-m' "
744 "option to provide a "
745 "different default\n"));
753 * For a dry run invocation, print out a basic message and run
754 * through all the vdevs in the list and print out in an
755 * appropriate hierarchy.
757 (void) printf(gettext("would create '%s' with the "
758 "following layout:\n\n"), poolname);
760 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
761 if (num_logs(nvroot) > 0)
762 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
767 * Hand off to libzfs.
769 if (zpool_create(g_zfs, poolname,
770 nvroot, props, fsprops) == 0) {
771 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
772 ZFS_TYPE_FILESYSTEM);
774 if (mountpoint != NULL)
775 verify(zfs_prop_set(pool,
777 ZFS_PROP_MOUNTPOINT),
779 if (zfs_mount(pool, NULL, 0) == 0)
780 ret = zfs_shareall(pool);
783 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
784 (void) fprintf(stderr, gettext("pool name may have "
791 nvlist_free(fsprops);
795 nvlist_free(fsprops);
802 * zpool destroy <pool>
804 * -f Forcefully unmount any datasets
806 * Destroy the given pool. Automatically unmounts any datasets in the pool.
809 zpool_do_destroy(int argc, char **argv)
811 boolean_t force = B_FALSE;
818 while ((c = getopt(argc, argv, "f")) != -1) {
824 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
833 /* check arguments */
835 (void) fprintf(stderr, gettext("missing pool argument\n"));
839 (void) fprintf(stderr, gettext("too many arguments\n"));
845 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
847 * As a special case, check for use of '/' in the name, and
848 * direct the user to use 'zfs destroy' instead.
850 if (strchr(pool, '/') != NULL)
851 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
852 "destroy a dataset\n"));
856 if (zpool_disable_datasets(zhp, force) != 0) {
857 (void) fprintf(stderr, gettext("could not destroy '%s': "
858 "could not unmount datasets\n"), zpool_get_name(zhp));
862 ret = (zpool_destroy(zhp) != 0);
870 * zpool export [-f] <pool> ...
872 * -f Forcefully unmount datasets
874 * Export the given pools. By default, the command will attempt to cleanly
875 * unmount any active datasets within the pool. If the '-f' flag is specified,
876 * then the datasets will be forcefully unmounted.
879 zpool_do_export(int argc, char **argv)
881 boolean_t force = B_FALSE;
882 boolean_t hardforce = B_FALSE;
889 while ((c = getopt(argc, argv, "fF")) != -1) {
898 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
907 /* check arguments */
909 (void) fprintf(stderr, gettext("missing pool argument\n"));
914 for (i = 0; i < argc; i++) {
915 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
920 if (zpool_disable_datasets(zhp, force) != 0) {
927 if (zpool_export_force(zhp) != 0)
929 } else if (zpool_export(zhp, force) != 0) {
940 * Given a vdev configuration, determine the maximum width needed for the device
944 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
946 char *name = zpool_vdev_name(g_zfs, zhp, nv);
951 if (strlen(name) + depth > max)
952 max = strlen(name) + depth;
956 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
957 &child, &children) == 0) {
958 for (c = 0; c < children; c++)
959 if ((ret = max_width(zhp, child[c], depth + 2,
964 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
965 &child, &children) == 0) {
966 for (c = 0; c < children; c++)
967 if ((ret = max_width(zhp, child[c], depth + 2,
972 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
973 &child, &children) == 0) {
974 for (c = 0; c < children; c++)
975 if ((ret = max_width(zhp, child[c], depth + 2,
986 * Print the configuration of an exported pool. Iterate over all vdevs in the
987 * pool, printing out the name and status for each one.
990 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth,
991 boolean_t print_logs)
998 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
999 if (strcmp(type, VDEV_TYPE_MISSING) == 0)
1002 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
1003 (uint64_t **)&vs, &c) == 0);
1005 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1006 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1008 if (vs->vs_aux != 0) {
1011 switch (vs->vs_aux) {
1012 case VDEV_AUX_OPEN_FAILED:
1013 (void) printf(gettext("cannot open"));
1016 case VDEV_AUX_BAD_GUID_SUM:
1017 (void) printf(gettext("missing device"));
1020 case VDEV_AUX_NO_REPLICAS:
1021 (void) printf(gettext("insufficient replicas"));
1024 case VDEV_AUX_VERSION_NEWER:
1025 (void) printf(gettext("newer version"));
1028 case VDEV_AUX_ERR_EXCEEDED:
1029 (void) printf(gettext("too many errors"));
1033 (void) printf(gettext("corrupted data"));
1037 (void) printf("\n");
1039 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1040 &child, &children) != 0)
1043 for (c = 0; c < children; c++) {
1044 uint64_t is_log = B_FALSE;
1046 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1048 if ((is_log && !print_logs) || (!is_log && print_logs))
1051 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1052 print_import_config(vname, child[c],
1053 namewidth, depth + 2, B_FALSE);
1057 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1058 &child, &children) == 0) {
1059 (void) printf(gettext("\tcache\n"));
1060 for (c = 0; c < children; c++) {
1061 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1062 (void) printf("\t %s\n", vname);
1067 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1068 &child, &children) == 0) {
1069 (void) printf(gettext("\tspares\n"));
1070 for (c = 0; c < children; c++) {
1071 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1072 (void) printf("\t %s\n", vname);
1079 * Display the status for the given pool.
1082 show_import(nvlist_t *config)
1084 uint64_t pool_state;
1095 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1097 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1099 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1101 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1104 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
1105 (uint64_t **)&vs, &vsc) == 0);
1106 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1108 reason = zpool_import_status(config, &msgid);
1110 (void) printf(gettext(" pool: %s\n"), name);
1111 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1112 (void) printf(gettext(" state: %s"), health);
1113 if (pool_state == POOL_STATE_DESTROYED)
1114 (void) printf(gettext(" (DESTROYED)"));
1115 (void) printf("\n");
1118 case ZPOOL_STATUS_MISSING_DEV_R:
1119 case ZPOOL_STATUS_MISSING_DEV_NR:
1120 case ZPOOL_STATUS_BAD_GUID_SUM:
1121 (void) printf(gettext("status: One or more devices are missing "
1122 "from the system.\n"));
1125 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1126 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1127 (void) printf(gettext("status: One or more devices contains "
1128 "corrupted data.\n"));
1131 case ZPOOL_STATUS_CORRUPT_DATA:
1132 (void) printf(gettext("status: The pool data is corrupted.\n"));
1135 case ZPOOL_STATUS_OFFLINE_DEV:
1136 (void) printf(gettext("status: One or more devices "
1137 "are offlined.\n"));
1140 case ZPOOL_STATUS_CORRUPT_POOL:
1141 (void) printf(gettext("status: The pool metadata is "
1145 case ZPOOL_STATUS_VERSION_OLDER:
1146 (void) printf(gettext("status: The pool is formatted using an "
1147 "older on-disk version.\n"));
1150 case ZPOOL_STATUS_VERSION_NEWER:
1151 (void) printf(gettext("status: The pool is formatted using an "
1152 "incompatible version.\n"));
1155 case ZPOOL_STATUS_HOSTID_MISMATCH:
1156 (void) printf(gettext("status: The pool was last accessed by "
1157 "another system.\n"));
1160 case ZPOOL_STATUS_FAULTED_DEV_R:
1161 case ZPOOL_STATUS_FAULTED_DEV_NR:
1162 (void) printf(gettext("status: One or more devices are "
1166 case ZPOOL_STATUS_BAD_LOG:
1167 (void) printf(gettext("status: An intent log record cannot be "
1173 * No other status can be seen when importing pools.
1175 assert(reason == ZPOOL_STATUS_OK);
1179 * Print out an action according to the overall state of the pool.
1181 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1182 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1183 (void) printf(gettext("action: The pool can be "
1184 "imported using its name or numeric identifier, "
1185 "though\n\tsome features will not be available "
1186 "without an explicit 'zpool upgrade'.\n"));
1187 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1188 (void) printf(gettext("action: The pool can be "
1189 "imported using its name or numeric "
1190 "identifier and\n\tthe '-f' flag.\n"));
1192 (void) printf(gettext("action: The pool can be "
1193 "imported using its name or numeric "
1195 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1196 (void) printf(gettext("action: The pool can be imported "
1197 "despite missing or damaged devices. The\n\tfault "
1198 "tolerance of the pool may be compromised if imported.\n"));
1201 case ZPOOL_STATUS_VERSION_NEWER:
1202 (void) printf(gettext("action: The pool cannot be "
1203 "imported. Access the pool on a system running "
1204 "newer\n\tsoftware, or recreate the pool from "
1207 case ZPOOL_STATUS_MISSING_DEV_R:
1208 case ZPOOL_STATUS_MISSING_DEV_NR:
1209 case ZPOOL_STATUS_BAD_GUID_SUM:
1210 (void) printf(gettext("action: The pool cannot be "
1211 "imported. Attach the missing\n\tdevices and try "
1215 (void) printf(gettext("action: The pool cannot be "
1216 "imported due to damaged devices or data.\n"));
1221 * If the state is "closed" or "can't open", and the aux state
1222 * is "corrupt data":
1224 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1225 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1226 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1227 if (pool_state == POOL_STATE_DESTROYED)
1228 (void) printf(gettext("\tThe pool was destroyed, "
1229 "but can be imported using the '-Df' flags.\n"));
1230 else if (pool_state != POOL_STATE_EXPORTED)
1231 (void) printf(gettext("\tThe pool may be active on "
1232 "another system, but can be imported using\n\t"
1233 "the '-f' flag.\n"));
1237 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1240 (void) printf(gettext("config:\n\n"));
1242 namewidth = max_width(NULL, nvroot, 0, 0);
1246 print_import_config(name, nvroot, namewidth, 0, B_FALSE);
1247 if (num_logs(nvroot) > 0) {
1248 (void) printf(gettext("\tlogs\n"));
1249 print_import_config(name, nvroot, namewidth, 0, B_TRUE);
1252 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1253 (void) printf(gettext("\n\tAdditional devices are known to "
1254 "be part of this pool, though their\n\texact "
1255 "configuration cannot be determined.\n"));
1260 * Perform the import for the given configuration. This passes the heavy
1261 * lifting off to zpool_import_props(), and then mounts the datasets contained
1265 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1266 int force, nvlist_t *props, boolean_t allowfaulted)
1268 zpool_handle_t *zhp;
1274 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1277 verify(nvlist_lookup_uint64(config,
1278 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1279 verify(nvlist_lookup_uint64(config,
1280 ZPOOL_CONFIG_VERSION, &version) == 0);
1281 if (version > SPA_VERSION) {
1282 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1283 "is formatted using a newer ZFS version\n"), name);
1285 } else if (state != POOL_STATE_EXPORTED && !force) {
1288 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1290 if ((unsigned long)hostid != gethostid()) {
1295 verify(nvlist_lookup_string(config,
1296 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1297 verify(nvlist_lookup_uint64(config,
1298 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1300 (void) fprintf(stderr, gettext("cannot import "
1301 "'%s': pool may be in use from other "
1302 "system, it was last accessed by %s "
1303 "(hostid: 0x%lx) on %s"), name, hostname,
1304 (unsigned long)hostid,
1305 asctime(localtime(&t)));
1306 (void) fprintf(stderr, gettext("use '-f' to "
1307 "import anyway\n"));
1311 (void) fprintf(stderr, gettext("cannot import '%s': "
1312 "pool may be in use from other system\n"), name);
1313 (void) fprintf(stderr, gettext("use '-f' to import "
1319 if (zpool_import_props(g_zfs, config, newname, props,
1323 if (newname != NULL)
1324 name = (char *)newname;
1326 verify((zhp = zpool_open_canfail(g_zfs, name)) != NULL);
1328 if (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.
1364 * -a Import all pools found.
1366 * -o Set property=value and/or temporary mount options (without '=').
1368 * The import command scans for pools to import, and import pools based on pool
1369 * name and GUID. The pool can also be renamed as part of the import process.
1372 zpool_do_import(int argc, char **argv)
1374 char **searchdirs = NULL;
1378 nvlist_t *pools = NULL;
1379 boolean_t do_all = B_FALSE;
1380 boolean_t do_destroyed = B_FALSE;
1381 char *mntopts = NULL;
1382 boolean_t do_force = B_FALSE;
1385 uint64_t searchguid = 0;
1386 char *searchname = NULL;
1388 nvlist_t *found_config;
1389 nvlist_t *props = NULL;
1391 boolean_t allow_faulted = B_FALSE;
1392 uint64_t pool_state;
1393 char *cachefile = NULL;
1396 while ((c = getopt(argc, argv, ":ac:d:DfFo:p:R:")) != -1) {
1405 if (searchdirs == NULL) {
1406 searchdirs = safe_malloc(sizeof (char *));
1408 char **tmp = safe_malloc((nsearch + 1) *
1410 bcopy(searchdirs, tmp, nsearch *
1415 searchdirs[nsearch++] = optarg;
1418 do_destroyed = B_TRUE;
1424 allow_faulted = B_TRUE;
1427 if ((propval = strchr(optarg, '=')) != NULL) {
1430 if (add_prop_list(optarg, propval,
1438 if (add_prop_list(zpool_prop_to_name(
1439 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1441 if (nvlist_lookup_string(props,
1442 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1445 if (add_prop_list(zpool_prop_to_name(
1446 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1450 (void) fprintf(stderr, gettext("missing argument for "
1451 "'%c' option\n"), optopt);
1455 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1464 if (cachefile && nsearch != 0) {
1465 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1469 if (searchdirs == NULL) {
1470 searchdirs = safe_malloc(sizeof (char *));
1471 searchdirs[0] = "/dev/dsk";
1475 /* check argument count */
1478 (void) fprintf(stderr, gettext("too many arguments\n"));
1483 (void) fprintf(stderr, gettext("too many arguments\n"));
1488 * Check for the SYS_CONFIG privilege. We do this explicitly
1489 * here because otherwise any attempt to discover pools will
1492 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1493 (void) fprintf(stderr, gettext("cannot "
1494 "discover pools: permission denied\n"));
1501 * Depending on the arguments given, we do one of the following:
1503 * <none> Iterate through all pools and display information about
1506 * -a Iterate through all pools and try to import each one.
1508 * <id> Find the pool that corresponds to the given GUID/pool
1509 * name and import that one.
1511 * -D Above options applies only to destroyed pools.
1517 searchguid = strtoull(argv[0], &endptr, 10);
1518 if (errno != 0 || *endptr != '\0')
1519 searchname = argv[0];
1520 found_config = NULL;
1524 pools = zpool_find_import_cached(g_zfs, cachefile, searchname,
1526 } else if (searchname != NULL) {
1527 pools = zpool_find_import_byname(g_zfs, nsearch, searchdirs,
1531 * It's OK to search by guid even if searchguid is 0.
1533 pools = zpool_find_import_byguid(g_zfs, nsearch, searchdirs,
1537 if (pools == NULL) {
1539 (void) fprintf(stderr, gettext("cannot import '%s': "
1540 "no such pool available\n"), argv[0]);
1547 * At this point we have a list of import candidate configs. Even if
1548 * we were searching by pool name or guid, we still need to
1549 * post-process the list to deal with pool state and possible
1555 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1557 verify(nvpair_value_nvlist(elem, &config) == 0);
1559 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1561 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1563 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1570 (void) printf("\n");
1573 err |= do_import(config, NULL, mntopts,
1574 do_force, props, allow_faulted);
1576 show_import(config);
1577 } else if (searchname != NULL) {
1581 * We are searching for a pool based on name.
1583 verify(nvlist_lookup_string(config,
1584 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1586 if (strcmp(name, searchname) == 0) {
1587 if (found_config != NULL) {
1588 (void) fprintf(stderr, gettext(
1589 "cannot import '%s': more than "
1590 "one matching pool\n"), searchname);
1591 (void) fprintf(stderr, gettext(
1592 "import by numeric ID instead\n"));
1595 found_config = config;
1601 * Search for a pool by guid.
1603 verify(nvlist_lookup_uint64(config,
1604 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1606 if (guid == searchguid)
1607 found_config = config;
1612 * If we were searching for a specific pool, verify that we found a
1613 * pool, and then do the import.
1615 if (argc != 0 && err == 0) {
1616 if (found_config == NULL) {
1617 (void) fprintf(stderr, gettext("cannot import '%s': "
1618 "no such pool available\n"), argv[0]);
1621 err |= do_import(found_config, argc == 1 ? NULL :
1622 argv[1], mntopts, do_force, props, allow_faulted);
1627 * If we were just looking for pools, report an error if none were
1630 if (argc == 0 && first)
1631 (void) fprintf(stderr,
1632 gettext("no pools available to import\n"));
1639 return (err ? 1 : 0);
1642 typedef struct iostat_cbdata {
1643 zpool_list_t *cb_list;
1650 print_iostat_separator(iostat_cbdata_t *cb)
1654 for (i = 0; i < cb->cb_namewidth; i++)
1656 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1660 print_iostat_header(iostat_cbdata_t *cb)
1662 (void) printf("%*s capacity operations bandwidth\n",
1663 cb->cb_namewidth, "");
1664 (void) printf("%-*s used avail read write read write\n",
1665 cb->cb_namewidth, "pool");
1666 print_iostat_separator(cb);
1670 * Display a single statistic.
1673 print_one_stat(uint64_t value)
1677 zfs_nicenum(value, buf, sizeof (buf));
1678 (void) printf(" %5s", buf);
1682 * Print out all the statistics for the given vdev. This can either be the
1683 * toplevel configuration, or called recursively. If 'name' is NULL, then this
1684 * is a verbose output, and we don't want to display the toplevel pool stats.
1687 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1688 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1690 nvlist_t **oldchild, **newchild;
1692 vdev_stat_t *oldvs, *newvs;
1693 vdev_stat_t zerovs = { 0 };
1698 if (oldnv != NULL) {
1699 verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
1700 (uint64_t **)&oldvs, &c) == 0);
1705 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
1706 (uint64_t **)&newvs, &c) == 0);
1708 if (strlen(name) + depth > cb->cb_namewidth)
1709 (void) printf("%*s%s", depth, "", name);
1711 (void) printf("%*s%s%*s", depth, "", name,
1712 (int)(cb->cb_namewidth - strlen(name) - depth), "");
1714 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
1719 scale = (double)NANOSEC / tdelta;
1721 /* only toplevel vdevs have capacity stats */
1722 if (newvs->vs_space == 0) {
1723 (void) printf(" - -");
1725 print_one_stat(newvs->vs_alloc);
1726 print_one_stat(newvs->vs_space - newvs->vs_alloc);
1729 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
1730 oldvs->vs_ops[ZIO_TYPE_READ])));
1732 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
1733 oldvs->vs_ops[ZIO_TYPE_WRITE])));
1735 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
1736 oldvs->vs_bytes[ZIO_TYPE_READ])));
1738 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
1739 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
1741 (void) printf("\n");
1743 if (!cb->cb_verbose)
1746 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
1747 &newchild, &children) != 0)
1750 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
1751 &oldchild, &c) != 0)
1754 for (c = 0; c < children; c++) {
1755 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1756 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1757 newchild[c], cb, depth + 2);
1762 * Include level 2 ARC devices in iostat output
1764 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
1765 &newchild, &children) != 0)
1768 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
1769 &oldchild, &c) != 0)
1773 (void) printf("%-*s - - - - - "
1774 "-\n", cb->cb_namewidth, "cache");
1775 for (c = 0; c < children; c++) {
1776 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1777 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1778 newchild[c], cb, depth + 2);
1785 refresh_iostat(zpool_handle_t *zhp, void *data)
1787 iostat_cbdata_t *cb = data;
1791 * If the pool has disappeared, remove it from the list and continue.
1793 if (zpool_refresh_stats(zhp, &missing) != 0)
1797 pool_list_remove(cb->cb_list, zhp);
1803 * Callback to print out the iostats for the given pool.
1806 print_iostat(zpool_handle_t *zhp, void *data)
1808 iostat_cbdata_t *cb = data;
1809 nvlist_t *oldconfig, *newconfig;
1810 nvlist_t *oldnvroot, *newnvroot;
1812 newconfig = zpool_get_config(zhp, &oldconfig);
1814 if (cb->cb_iteration == 1)
1817 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
1820 if (oldconfig == NULL)
1823 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
1827 * Print out the statistics for the pool.
1829 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
1832 print_iostat_separator(cb);
1838 get_namewidth(zpool_handle_t *zhp, void *data)
1840 iostat_cbdata_t *cb = data;
1841 nvlist_t *config, *nvroot;
1843 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
1844 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1846 if (!cb->cb_verbose)
1847 cb->cb_namewidth = strlen(zpool_get_name(zhp));
1849 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
1853 * The width must fall into the range [10,38]. The upper limit is the
1854 * maximum we can have and still fit in 80 columns.
1856 if (cb->cb_namewidth < 10)
1857 cb->cb_namewidth = 10;
1858 if (cb->cb_namewidth > 38)
1859 cb->cb_namewidth = 38;
1865 * zpool iostat [-v] [pool] ... [interval [count]]
1867 * -v Display statistics for individual vdevs
1869 * This command can be tricky because we want to be able to deal with pool
1870 * creation/destruction as well as vdev configuration changes. The bulk of this
1871 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
1872 * on pool_list_update() to detect the addition of new pools. Configuration
1873 * changes are all handled within libzfs.
1876 zpool_do_iostat(int argc, char **argv)
1881 unsigned long interval = 0, count = 0;
1883 boolean_t verbose = B_FALSE;
1887 while ((c = getopt(argc, argv, "v")) != -1) {
1893 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1903 * Determine if the last argument is an integer or a pool name
1905 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1909 interval = strtoul(argv[argc - 1], &end, 10);
1911 if (*end == '\0' && errno == 0) {
1912 if (interval == 0) {
1913 (void) fprintf(stderr, gettext("interval "
1914 "cannot be zero\n"));
1919 * Ignore the last parameter
1924 * If this is not a valid number, just plow on. The
1925 * user will get a more informative error message later
1933 * If the last argument is also an integer, then we have both a count
1936 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1941 interval = strtoul(argv[argc - 1], &end, 10);
1943 if (*end == '\0' && errno == 0) {
1944 if (interval == 0) {
1945 (void) fprintf(stderr, gettext("interval "
1946 "cannot be zero\n"));
1951 * Ignore the last parameter
1960 * Construct the list of all interesting pools.
1963 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
1966 if (pool_list_count(list) == 0 && argc != 0) {
1967 pool_list_free(list);
1971 if (pool_list_count(list) == 0 && interval == 0) {
1972 pool_list_free(list);
1973 (void) fprintf(stderr, gettext("no pools available\n"));
1978 * Enter the main iostat loop.
1981 cb.cb_verbose = verbose;
1982 cb.cb_iteration = 0;
1983 cb.cb_namewidth = 0;
1986 pool_list_update(list);
1988 if ((npools = pool_list_count(list)) == 0)
1992 * Refresh all statistics. This is done as an explicit step
1993 * before calculating the maximum name width, so that any
1994 * configuration changes are properly accounted for.
1996 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
1999 * Iterate over all pools to determine the maximum width
2000 * for the pool / device name column across all pools.
2002 cb.cb_namewidth = 0;
2003 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2006 * If it's the first time, or verbose mode, print the header.
2008 if (++cb.cb_iteration == 1 || verbose)
2009 print_iostat_header(&cb);
2011 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2014 * If there's more than one pool, and we're not in verbose mode
2015 * (which prints a separator for us), then print a separator.
2017 if (npools > 1 && !verbose)
2018 print_iostat_separator(&cb);
2021 (void) printf("\n");
2024 * Flush the output so that redirection to a file isn't buffered
2027 (void) fflush(stdout);
2032 if (count != 0 && --count == 0)
2035 (void) sleep(interval);
2038 pool_list_free(list);
2043 typedef struct list_cbdata {
2044 boolean_t cb_scripted;
2046 zprop_list_t *cb_proplist;
2050 * Given a list of columns to display, output appropriate headers for each one.
2053 print_header(zprop_list_t *pl)
2056 boolean_t first = B_TRUE;
2057 boolean_t right_justify;
2059 for (; pl != NULL; pl = pl->pl_next) {
2060 if (pl->pl_prop == ZPROP_INVAL)
2068 header = zpool_prop_column_name(pl->pl_prop);
2069 right_justify = zpool_prop_align_right(pl->pl_prop);
2071 if (pl->pl_next == NULL && !right_justify)
2072 (void) printf("%s", header);
2073 else if (right_justify)
2074 (void) printf("%*s", pl->pl_width, header);
2076 (void) printf("%-*s", pl->pl_width, header);
2079 (void) printf("\n");
2083 * Given a pool and a list of properties, print out all the properties according
2084 * to the described layout.
2087 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2089 boolean_t first = B_TRUE;
2090 char property[ZPOOL_MAXPROPLEN];
2092 boolean_t right_justify;
2095 for (; pl != NULL; pl = pl->pl_next) {
2098 (void) printf("\t");
2105 right_justify = B_FALSE;
2106 if (pl->pl_prop != ZPROP_INVAL) {
2107 if (zpool_get_prop(zhp, pl->pl_prop, property,
2108 sizeof (property), NULL) != 0)
2113 right_justify = zpool_prop_align_right(pl->pl_prop);
2118 width = pl->pl_width;
2121 * If this is being called in scripted mode, or if this is the
2122 * last column and it is left-justified, don't include a width
2125 if (scripted || (pl->pl_next == NULL && !right_justify))
2126 (void) printf("%s", propstr);
2127 else if (right_justify)
2128 (void) printf("%*s", width, propstr);
2130 (void) printf("%-*s", width, propstr);
2133 (void) printf("\n");
2137 * Generic callback function to list a pool.
2140 list_callback(zpool_handle_t *zhp, void *data)
2142 list_cbdata_t *cbp = data;
2144 if (cbp->cb_first) {
2145 if (!cbp->cb_scripted)
2146 print_header(cbp->cb_proplist);
2147 cbp->cb_first = B_FALSE;
2150 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2156 * zpool list [-H] [-o prop[,prop]*] [pool] ...
2158 * -H Scripted mode. Don't display headers, and separate properties
2160 * -o List of properties to display. Defaults to
2161 * "name,size,used,available,capacity,health,altroot"
2163 * List all pools in the system, whether or not they're healthy. Output space
2164 * statistics for each one, as well as health status summary.
2167 zpool_do_list(int argc, char **argv)
2171 list_cbdata_t cb = { 0 };
2172 static char default_props[] =
2173 "name,size,used,available,capacity,health,altroot";
2174 char *props = default_props;
2177 while ((c = getopt(argc, argv, ":Ho:")) != -1) {
2180 cb.cb_scripted = B_TRUE;
2186 (void) fprintf(stderr, gettext("missing argument for "
2187 "'%c' option\n"), optopt);
2191 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2200 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2203 cb.cb_first = B_TRUE;
2205 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2206 list_callback, &cb);
2208 zprop_free_list(cb.cb_proplist);
2210 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2211 (void) printf(gettext("no pools available\n"));
2219 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2226 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2227 &child, &children) != 0) {
2228 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2229 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV)-1) == 0)
2230 name += sizeof(_PATH_DEV)-1;
2231 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV)-1) == 0)
2232 path += sizeof(_PATH_DEV)-1;
2233 if (strcmp(name, path) == 0)
2238 for (c = 0; c < children; c++)
2239 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2246 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2248 boolean_t force = B_FALSE;
2251 char *poolname, *old_disk, *new_disk;
2252 zpool_handle_t *zhp;
2256 while ((c = getopt(argc, argv, "f")) != -1) {
2262 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2271 /* get pool name and check number of arguments */
2273 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2280 (void) fprintf(stderr,
2281 gettext("missing <device> specification\n"));
2289 (void) fprintf(stderr,
2290 gettext("missing <new_device> specification\n"));
2293 new_disk = old_disk;
2303 (void) fprintf(stderr, gettext("too many arguments\n"));
2307 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2310 if (zpool_get_config(zhp, NULL) == NULL) {
2311 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2317 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2319 if (nvroot == NULL) {
2324 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2326 nvlist_free(nvroot);
2333 * zpool replace [-f] <pool> <device> <new_device>
2335 * -f Force attach, even if <new_device> appears to be in use.
2337 * Replace <device> with <new_device>.
2341 zpool_do_replace(int argc, char **argv)
2343 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2347 * zpool attach [-f] <pool> <device> <new_device>
2349 * -f Force attach, even if <new_device> appears to be in use.
2351 * Attach <new_device> to the mirror containing <device>. If <device> is not
2352 * part of a mirror, then <device> will be transformed into a mirror of
2353 * <device> and <new_device>. In either case, <new_device> will begin life
2354 * with a DTL of [0, now], and will immediately begin to resilver itself.
2357 zpool_do_attach(int argc, char **argv)
2359 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2363 * zpool detach [-f] <pool> <device>
2365 * -f Force detach of <device>, even if DTLs argue against it
2366 * (not supported yet)
2368 * Detach a device from a mirror. The operation will be refused if <device>
2369 * is the last device in the mirror, or if the DTLs indicate that this device
2370 * has the only valid copy of some data.
2374 zpool_do_detach(int argc, char **argv)
2377 char *poolname, *path;
2378 zpool_handle_t *zhp;
2382 while ((c = getopt(argc, argv, "f")) != -1) {
2386 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2395 /* get pool name and check number of arguments */
2397 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2402 (void) fprintf(stderr,
2403 gettext("missing <device> specification\n"));
2410 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2413 ret = zpool_vdev_detach(zhp, path);
2421 * zpool online <pool> <device> ...
2424 zpool_do_online(int argc, char **argv)
2428 zpool_handle_t *zhp;
2430 vdev_state_t newstate;
2433 while ((c = getopt(argc, argv, "t")) != -1) {
2437 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2446 /* get pool name and check number of arguments */
2448 (void) fprintf(stderr, gettext("missing pool name\n"));
2452 (void) fprintf(stderr, gettext("missing device name\n"));
2458 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2461 for (i = 1; i < argc; i++) {
2462 if (zpool_vdev_online(zhp, argv[i], 0, &newstate) == 0) {
2463 if (newstate != VDEV_STATE_HEALTHY) {
2464 (void) printf(gettext("warning: device '%s' "
2465 "onlined, but remains in faulted state\n"),
2467 if (newstate == VDEV_STATE_FAULTED)
2468 (void) printf(gettext("use 'zpool "
2469 "clear' to restore a faulted "
2472 (void) printf(gettext("use 'zpool "
2473 "replace' to replace devices "
2474 "that are no longer present\n"));
2487 * zpool offline [-ft] <pool> <device> ...
2489 * -f Force the device into the offline state, even if doing
2490 * so would appear to compromise pool availability.
2491 * (not supported yet)
2493 * -t Only take the device off-line temporarily. The offline
2494 * state will not be persistent across reboots.
2498 zpool_do_offline(int argc, char **argv)
2502 zpool_handle_t *zhp;
2504 boolean_t istmp = B_FALSE;
2507 while ((c = getopt(argc, argv, "ft")) != -1) {
2514 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2523 /* get pool name and check number of arguments */
2525 (void) fprintf(stderr, gettext("missing pool name\n"));
2529 (void) fprintf(stderr, gettext("missing device name\n"));
2535 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2538 for (i = 1; i < argc; i++) {
2539 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
2549 * zpool clear <pool> [device]
2551 * Clear all errors associated with a pool or a particular device.
2554 zpool_do_clear(int argc, char **argv)
2557 zpool_handle_t *zhp;
2558 char *pool, *device;
2561 (void) fprintf(stderr, gettext("missing pool name\n"));
2566 (void) fprintf(stderr, gettext("too many arguments\n"));
2571 device = argc == 3 ? argv[2] : NULL;
2573 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
2576 if (zpool_clear(zhp, device) != 0)
2584 typedef struct scrub_cbdata {
2591 scrub_callback(zpool_handle_t *zhp, void *data)
2593 scrub_cbdata_t *cb = data;
2597 * Ignore faulted pools.
2599 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2600 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
2601 "currently unavailable\n"), zpool_get_name(zhp));
2605 err = zpool_scrub(zhp, cb->cb_type);
2611 * zpool scrub [-s] <pool> ...
2613 * -s Stop. Stops any in-progress scrub.
2616 zpool_do_scrub(int argc, char **argv)
2621 cb.cb_type = POOL_SCRUB_EVERYTHING;
2624 while ((c = getopt(argc, argv, "s")) != -1) {
2627 cb.cb_type = POOL_SCRUB_NONE;
2630 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2642 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2646 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
2649 typedef struct status_cbdata {
2651 boolean_t cb_allpools;
2652 boolean_t cb_verbose;
2653 boolean_t cb_explain;
2658 * Print out detailed scrub status.
2661 print_scrub_status(nvlist_t *nvroot)
2665 time_t start, end, now;
2666 double fraction_done;
2667 uint64_t examined, total, minutes_left, minutes_taken;
2670 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
2671 (uint64_t **)&vs, &vsc) == 0);
2674 * If there's never been a scrub, there's not much to say.
2676 if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
2677 (void) printf(gettext("none requested\n"));
2681 scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2682 "resilver" : "scrub";
2684 start = vs->vs_scrub_start;
2685 end = vs->vs_scrub_end;
2687 examined = vs->vs_scrub_examined;
2688 total = vs->vs_alloc;
2691 minutes_taken = (uint64_t)((end - start) / 60);
2693 (void) printf(gettext("%s %s after %lluh%um with %llu errors "
2695 scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
2696 (u_longlong_t)(minutes_taken / 60),
2697 (uint_t)(minutes_taken % 60),
2698 (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
2704 if (examined > total)
2707 fraction_done = (double)examined / total;
2708 minutes_left = (uint64_t)((now - start) *
2709 (1 - fraction_done) / fraction_done / 60);
2710 minutes_taken = (uint64_t)((now - start) / 60);
2712 (void) printf(gettext("%s in progress for %lluh%um, %.2f%% done, "
2713 "%lluh%um to go\n"),
2714 scrub_type, (u_longlong_t)(minutes_taken / 60),
2715 (uint_t)(minutes_taken % 60), 100 * fraction_done,
2716 (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
2719 typedef struct spare_cbdata {
2721 zpool_handle_t *cb_zhp;
2725 find_vdev(nvlist_t *nv, uint64_t search)
2731 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2735 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2736 &child, &children) == 0) {
2737 for (c = 0; c < children; c++)
2738 if (find_vdev(child[c], search))
2746 find_spare(zpool_handle_t *zhp, void *data)
2748 spare_cbdata_t *cbp = data;
2749 nvlist_t *config, *nvroot;
2751 config = zpool_get_config(zhp, NULL);
2752 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2755 if (find_vdev(nvroot, cbp->cb_guid)) {
2765 * Print out configuration state as requested by status_callback.
2768 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2769 int namewidth, int depth, boolean_t isspare, boolean_t print_logs)
2774 char rbuf[6], wbuf[6], cbuf[6], repaired[7];
2776 uint64_t notpresent;
2780 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2781 (uint64_t **)&vs, &c) == 0);
2783 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2784 &child, &children) != 0)
2787 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2790 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2793 if (vs->vs_aux == VDEV_AUX_SPARED)
2795 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2799 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
2803 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2804 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2805 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
2806 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
2809 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2810 ¬present) == 0) {
2812 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2813 (void) printf(" was %s", path);
2814 } else if (vs->vs_aux != 0) {
2817 switch (vs->vs_aux) {
2818 case VDEV_AUX_OPEN_FAILED:
2819 (void) printf(gettext("cannot open"));
2822 case VDEV_AUX_BAD_GUID_SUM:
2823 (void) printf(gettext("missing device"));
2826 case VDEV_AUX_NO_REPLICAS:
2827 (void) printf(gettext("insufficient replicas"));
2830 case VDEV_AUX_VERSION_NEWER:
2831 (void) printf(gettext("newer version"));
2834 case VDEV_AUX_SPARED:
2835 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2837 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
2838 if (strcmp(zpool_get_name(cb.cb_zhp),
2839 zpool_get_name(zhp)) == 0)
2840 (void) printf(gettext("currently in "
2843 (void) printf(gettext("in use by "
2845 zpool_get_name(cb.cb_zhp));
2846 zpool_close(cb.cb_zhp);
2848 (void) printf(gettext("currently in use"));
2852 case VDEV_AUX_ERR_EXCEEDED:
2853 (void) printf(gettext("too many errors"));
2856 case VDEV_AUX_IO_FAILURE:
2857 (void) printf(gettext("experienced I/O failures"));
2860 case VDEV_AUX_BAD_LOG:
2861 (void) printf(gettext("bad intent log"));
2865 (void) printf(gettext("corrupted data"));
2868 } else if (vs->vs_scrub_repaired != 0 && children == 0) {
2870 * Report bytes resilvered/repaired on leaf devices.
2872 zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
2873 (void) printf(gettext(" %s %s"), repaired,
2874 (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2875 "resilvered" : "repaired");
2878 (void) printf("\n");
2880 for (c = 0; c < children; c++) {
2881 uint64_t is_log = B_FALSE;
2883 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2885 if ((is_log && !print_logs) || (!is_log && print_logs))
2887 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
2888 print_status_config(zhp, vname, child[c],
2889 namewidth, depth + 2, isspare, B_FALSE);
2895 print_error_log(zpool_handle_t *zhp)
2897 nvlist_t *nverrlist = NULL;
2900 size_t len = MAXPATHLEN * 2;
2902 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
2903 (void) printf("errors: List of errors unavailable "
2904 "(insufficient privileges)\n");
2908 (void) printf("errors: Permanent errors have been "
2909 "detected in the following files:\n\n");
2911 pathname = safe_malloc(len);
2913 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
2915 uint64_t dsobj, obj;
2917 verify(nvpair_value_nvlist(elem, &nv) == 0);
2918 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
2920 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
2922 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
2923 (void) printf("%7s %s\n", "", pathname);
2926 nvlist_free(nverrlist);
2930 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
2939 (void) printf(gettext("\tspares\n"));
2941 for (i = 0; i < nspares; i++) {
2942 name = zpool_vdev_name(g_zfs, zhp, spares[i]);
2943 print_status_config(zhp, name, spares[i],
2944 namewidth, 2, B_TRUE, B_FALSE);
2950 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
2959 (void) printf(gettext("\tcache\n"));
2961 for (i = 0; i < nl2cache; i++) {
2962 name = zpool_vdev_name(g_zfs, zhp, l2cache[i]);
2963 print_status_config(zhp, name, l2cache[i],
2964 namewidth, 2, B_FALSE, B_FALSE);
2970 * Display a summary of pool status. Displays a summary such as:
2974 * reason: One or more devices ...
2975 * see: http://www.sun.com/msg/ZFS-xxxx-01
2981 * When given the '-v' option, we print out the complete config. If the '-e'
2982 * option is specified, then we print out error rate information as well.
2985 status_callback(zpool_handle_t *zhp, void *data)
2987 status_cbdata_t *cbp = data;
2988 nvlist_t *config, *nvroot;
2995 config = zpool_get_config(zhp, NULL);
2996 reason = zpool_get_status(zhp, &msgid);
3001 * If we were given 'zpool status -x', only report those pools with
3004 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3005 if (!cbp->cb_allpools) {
3006 (void) printf(gettext("pool '%s' is healthy\n"),
3007 zpool_get_name(zhp));
3009 cbp->cb_first = B_FALSE;
3015 cbp->cb_first = B_FALSE;
3017 (void) printf("\n");
3019 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3021 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
3022 (uint64_t **)&vs, &c) == 0);
3023 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3025 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3026 (void) printf(gettext(" state: %s\n"), health);
3029 case ZPOOL_STATUS_MISSING_DEV_R:
3030 (void) printf(gettext("status: One or more devices could not "
3031 "be opened. Sufficient replicas exist for\n\tthe pool to "
3032 "continue functioning in a degraded state.\n"));
3033 (void) printf(gettext("action: Attach the missing device and "
3034 "online it using 'zpool online'.\n"));
3037 case ZPOOL_STATUS_MISSING_DEV_NR:
3038 (void) printf(gettext("status: One or more devices could not "
3039 "be opened. There are insufficient\n\treplicas for the "
3040 "pool to continue functioning.\n"));
3041 (void) printf(gettext("action: Attach the missing device and "
3042 "online it using 'zpool online'.\n"));
3045 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3046 (void) printf(gettext("status: One or more devices could not "
3047 "be used because the label is missing or\n\tinvalid. "
3048 "Sufficient replicas exist for the pool to continue\n\t"
3049 "functioning in a degraded state.\n"));
3050 (void) printf(gettext("action: Replace the device using "
3051 "'zpool replace'.\n"));
3054 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3055 (void) printf(gettext("status: One or more devices could not "
3056 "be used because the label is missing \n\tor invalid. "
3057 "There are insufficient replicas for the pool to "
3058 "continue\n\tfunctioning.\n"));
3059 (void) printf(gettext("action: Destroy and re-create the pool "
3060 "from a backup source.\n"));
3063 case ZPOOL_STATUS_FAILING_DEV:
3064 (void) printf(gettext("status: One or more devices has "
3065 "experienced an unrecoverable error. An\n\tattempt was "
3066 "made to correct the error. Applications are "
3068 (void) printf(gettext("action: Determine if the device needs "
3069 "to be replaced, and clear the errors\n\tusing "
3070 "'zpool clear' or replace the device with 'zpool "
3074 case ZPOOL_STATUS_OFFLINE_DEV:
3075 (void) printf(gettext("status: One or more devices has "
3076 "been taken offline by the administrator.\n\tSufficient "
3077 "replicas exist for the pool to continue functioning in "
3078 "a\n\tdegraded state.\n"));
3079 (void) printf(gettext("action: Online the device using "
3080 "'zpool online' or replace the device with\n\t'zpool "
3084 case ZPOOL_STATUS_RESILVERING:
3085 (void) printf(gettext("status: One or more devices is "
3086 "currently being resilvered. The pool will\n\tcontinue "
3087 "to function, possibly in a degraded state.\n"));
3088 (void) printf(gettext("action: Wait for the resilver to "
3092 case ZPOOL_STATUS_CORRUPT_DATA:
3093 (void) printf(gettext("status: One or more devices has "
3094 "experienced an error resulting in data\n\tcorruption. "
3095 "Applications may be affected.\n"));
3096 (void) printf(gettext("action: Restore the file in question "
3097 "if possible. Otherwise restore the\n\tentire pool from "
3101 case ZPOOL_STATUS_CORRUPT_POOL:
3102 (void) printf(gettext("status: The pool metadata is corrupted "
3103 "and the pool cannot be opened.\n"));
3104 (void) printf(gettext("action: Destroy and re-create the pool "
3105 "from a backup source.\n"));
3108 case ZPOOL_STATUS_VERSION_OLDER:
3109 (void) printf(gettext("status: The pool is formatted using an "
3110 "older on-disk format. The pool can\n\tstill be used, but "
3111 "some features are unavailable.\n"));
3112 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3113 "upgrade'. Once this is done, the\n\tpool will no longer "
3114 "be accessible on older software versions.\n"));
3117 case ZPOOL_STATUS_VERSION_NEWER:
3118 (void) printf(gettext("status: The pool has been upgraded to a "
3119 "newer, incompatible on-disk version.\n\tThe pool cannot "
3120 "be accessed on this system.\n"));
3121 (void) printf(gettext("action: Access the pool from a system "
3122 "running more recent software, or\n\trestore the pool from "
3126 case ZPOOL_STATUS_FAULTED_DEV_R:
3127 (void) printf(gettext("status: One or more devices are "
3128 "faulted in response to persistent errors.\n\tSufficient "
3129 "replicas exist for the pool to continue functioning "
3130 "in a\n\tdegraded state.\n"));
3131 (void) printf(gettext("action: Replace the faulted device, "
3132 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3135 case ZPOOL_STATUS_FAULTED_DEV_NR:
3136 (void) printf(gettext("status: One or more devices are "
3137 "faulted in response to persistent errors. There are "
3138 "insufficient replicas for the pool to\n\tcontinue "
3140 (void) printf(gettext("action: Destroy and re-create the pool "
3141 "from a backup source. Manually marking the device\n"
3142 "\trepaired using 'zpool clear' may allow some data "
3143 "to be recovered.\n"));
3146 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3147 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3148 (void) printf(gettext("status: One or more devices are "
3149 "faulted in response to IO failures.\n"));
3150 (void) printf(gettext("action: Make sure the affected devices "
3151 "are connected, then run 'zpool clear'.\n"));
3154 case ZPOOL_STATUS_BAD_LOG:
3155 (void) printf(gettext("status: An intent log record "
3156 "could not be read.\n"
3157 "\tWaiting for adminstrator intervention to fix the "
3158 "faulted pool.\n"));
3159 (void) printf(gettext("action: Either restore the affected "
3160 "device(s) and run 'zpool online',\n"
3161 "\tor ignore the intent log records by running "
3162 "'zpool clear'.\n"));
3167 * The remaining errors can't actually be generated, yet.
3169 assert(reason == ZPOOL_STATUS_OK);
3173 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3176 if (config != NULL) {
3179 nvlist_t **spares, **l2cache;
3180 uint_t nspares, nl2cache;
3183 (void) printf(gettext(" scrub: "));
3184 print_scrub_status(nvroot);
3186 namewidth = max_width(zhp, nvroot, 0, 0);
3190 (void) printf(gettext("config:\n\n"));
3191 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3192 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3193 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3194 namewidth, 0, B_FALSE, B_FALSE);
3195 if (num_logs(nvroot) > 0)
3196 print_status_config(zhp, "logs", nvroot, namewidth, 0,
3199 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3200 &l2cache, &nl2cache) == 0)
3201 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3203 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3204 &spares, &nspares) == 0)
3205 print_spares(zhp, spares, nspares, namewidth);
3207 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3209 nvlist_t *nverrlist = NULL;
3212 * If the approximate error count is small, get a
3213 * precise count by fetching the entire log and
3214 * uniquifying the results.
3216 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3217 zpool_get_errlog(zhp, &nverrlist) == 0) {
3222 while ((elem = nvlist_next_nvpair(nverrlist,
3227 nvlist_free(nverrlist);
3229 (void) printf("\n");
3232 (void) printf(gettext("errors: No known data "
3234 else if (!cbp->cb_verbose)
3235 (void) printf(gettext("errors: %llu data "
3236 "errors, use '-v' for a list\n"),
3237 (u_longlong_t)nerr);
3239 print_error_log(zhp);
3242 (void) printf(gettext("config: The configuration cannot be "
3250 * zpool status [-vx] [pool] ...
3252 * -v Display complete error logs
3253 * -x Display only pools with potential problems
3255 * Describes the health status of all pools or some subset.
3258 zpool_do_status(int argc, char **argv)
3262 status_cbdata_t cb = { 0 };
3265 while ((c = getopt(argc, argv, "vx")) != -1) {
3268 cb.cb_verbose = B_TRUE;
3271 cb.cb_explain = B_TRUE;
3274 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3283 cb.cb_first = B_TRUE;
3286 cb.cb_allpools = B_TRUE;
3288 ret = for_each_pool(argc, argv, B_TRUE, NULL, status_callback, &cb);
3290 if (argc == 0 && cb.cb_count == 0)
3291 (void) printf(gettext("no pools available\n"));
3292 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3293 (void) printf(gettext("all pools are healthy\n"));
3298 typedef struct upgrade_cbdata {
3303 uint64_t cb_version;
3308 upgrade_cb(zpool_handle_t *zhp, void *arg)
3310 upgrade_cbdata_t *cbp = arg;
3315 config = zpool_get_config(zhp, NULL);
3316 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3319 if (!cbp->cb_newer && version < SPA_VERSION) {
3321 if (cbp->cb_first) {
3322 (void) printf(gettext("The following pools are "
3323 "out of date, and can be upgraded. After "
3324 "being\nupgraded, these pools will no "
3325 "longer be accessible by older software "
3327 (void) printf(gettext("VER POOL\n"));
3328 (void) printf(gettext("--- ------------\n"));
3329 cbp->cb_first = B_FALSE;
3332 (void) printf("%2llu %s\n", (u_longlong_t)version,
3333 zpool_get_name(zhp));
3335 cbp->cb_first = B_FALSE;
3336 ret = zpool_upgrade(zhp, cbp->cb_version);
3338 (void) printf(gettext("Successfully upgraded "
3339 "'%s'\n\n"), zpool_get_name(zhp));
3342 } else if (cbp->cb_newer && version > SPA_VERSION) {
3343 assert(!cbp->cb_all);
3345 if (cbp->cb_first) {
3346 (void) printf(gettext("The following pools are "
3347 "formatted using a newer software version and\n"
3348 "cannot be accessed on the current system.\n\n"));
3349 (void) printf(gettext("VER POOL\n"));
3350 (void) printf(gettext("--- ------------\n"));
3351 cbp->cb_first = B_FALSE;
3354 (void) printf("%2llu %s\n", (u_longlong_t)version,
3355 zpool_get_name(zhp));
3364 upgrade_one(zpool_handle_t *zhp, void *data)
3366 upgrade_cbdata_t *cbp = data;
3367 uint64_t cur_version;
3370 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3371 (void) printf(gettext("'log' is now a reserved word\n"
3372 "Pool 'log' must be renamed using export and import"
3377 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3378 if (cur_version > cbp->cb_version) {
3379 (void) printf(gettext("Pool '%s' is already formatted "
3380 "using more current version '%llu'.\n"),
3381 zpool_get_name(zhp), cur_version);
3384 if (cur_version == cbp->cb_version) {
3385 (void) printf(gettext("Pool '%s' is already formatted "
3386 "using the current version.\n"), zpool_get_name(zhp));
3390 ret = zpool_upgrade(zhp, cbp->cb_version);
3393 (void) printf(gettext("Successfully upgraded '%s' "
3394 "from version %llu to version %llu\n\n"),
3395 zpool_get_name(zhp), (u_longlong_t)cur_version,
3396 (u_longlong_t)cbp->cb_version);
3405 * zpool upgrade [-V version] <-a | pool ...>
3407 * With no arguments, display downrev'd ZFS pool available for upgrade.
3408 * Individual pools can be upgraded by specifying the pool, and '-a' will
3409 * upgrade all pools.
3412 zpool_do_upgrade(int argc, char **argv)
3415 upgrade_cbdata_t cb = { 0 };
3417 boolean_t showversions = B_FALSE;
3422 while ((c = getopt(argc, argv, "avV:")) != -1) {
3428 showversions = B_TRUE;
3431 cb.cb_version = strtoll(optarg, &end, 10);
3432 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3433 cb.cb_version < SPA_VERSION_1) {
3434 (void) fprintf(stderr,
3435 gettext("invalid version '%s'\n"), optarg);
3440 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3451 if (cb.cb_version == 0) {
3452 cb.cb_version = SPA_VERSION;
3453 } else if (!cb.cb_all && argc == 0) {
3454 (void) fprintf(stderr, gettext("-V option is "
3455 "incompatible with other arguments\n"));
3460 if (cb.cb_all || argc != 0) {
3461 (void) fprintf(stderr, gettext("-v option is "
3462 "incompatible with other arguments\n"));
3465 } else if (cb.cb_all) {
3467 (void) fprintf(stderr, gettext("-a option should not "
3468 "be used along with a pool name\n"));
3473 (void) printf(gettext("This system is currently running "
3474 "ZFS pool version %llu.\n\n"), SPA_VERSION);
3475 cb.cb_first = B_TRUE;
3477 (void) printf(gettext("The following versions are "
3479 (void) printf(gettext("VER DESCRIPTION\n"));
3480 (void) printf("--- -----------------------------------------"
3481 "---------------\n");
3482 (void) printf(gettext(" 1 Initial ZFS version\n"));
3483 (void) printf(gettext(" 2 Ditto blocks "
3484 "(replicated metadata)\n"));
3485 (void) printf(gettext(" 3 Hot spares and double parity "
3487 (void) printf(gettext(" 4 zpool history\n"));
3488 (void) printf(gettext(" 5 Compression using the gzip "
3490 (void) printf(gettext(" 6 bootfs pool property\n"));
3491 (void) printf(gettext(" 7 Separate intent log devices\n"));
3492 (void) printf(gettext(" 8 Delegated administration\n"));
3493 (void) printf(gettext(" 9 refquota and refreservation "
3495 (void) printf(gettext(" 10 Cache devices\n"));
3496 (void) printf(gettext(" 11 Improved scrub performance\n"));
3497 (void) printf(gettext(" 12 Snapshot properties\n"));
3498 (void) printf(gettext(" 13 snapused property\n"));
3499 (void) printf(gettext(" 14 passthrough-x aclinherit "
3501 (void) printf(gettext("For more information on a particular "
3502 "version, including supported releases, see:\n\n"));
3503 (void) printf("http://www.opensolaris.org/os/community/zfs/"
3505 (void) printf(gettext("Where 'N' is the version number.\n"));
3506 } else if (argc == 0) {
3509 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3510 notfound = cb.cb_first;
3512 if (!cb.cb_all && ret == 0) {
3514 (void) printf("\n");
3515 cb.cb_first = B_TRUE;
3516 cb.cb_newer = B_TRUE;
3517 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3520 (void) printf("\n");
3526 (void) printf(gettext("All pools are formatted "
3527 "using this version.\n"));
3528 else if (!cb.cb_all)
3529 (void) printf(gettext("Use 'zpool upgrade -v' "
3530 "for a list of available versions and "
3531 "their associated\nfeatures.\n"));
3534 ret = for_each_pool(argc, argv, B_FALSE, NULL,
3541 typedef struct hist_cbdata {
3547 char *hist_event_table[LOG_END] = {
3563 "pool property set",
3567 "destroy_begin_sync",
3571 "permission update",
3572 "permission remove",
3573 "permission who remove",
3582 "filesystem version upgrade",
3584 "refreservation set",
3589 * Print out the command history for a specific pool.
3592 get_history_one(zpool_handle_t *zhp, void *data)
3608 char internalstr[MAXPATHLEN];
3609 hist_cbdata_t *cb = (hist_cbdata_t *)data;
3613 cb->first = B_FALSE;
3615 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
3617 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
3620 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
3621 &records, &numrecords) == 0);
3622 for (i = 0; i < numrecords; i++) {
3623 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
3627 /* is it an internal event or a standard event? */
3628 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
3630 if (cb->internal == 0)
3633 if (nvlist_lookup_uint64(records[i],
3634 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
3636 verify(nvlist_lookup_uint64(records[i],
3637 ZPOOL_HIST_TXG, &txg) == 0);
3638 verify(nvlist_lookup_string(records[i],
3639 ZPOOL_HIST_INT_STR, &pathstr) == 0);
3640 if (ievent >= LOG_END)
3642 (void) snprintf(internalstr,
3643 sizeof (internalstr),
3644 "[internal %s txg:%lld] %s",
3645 hist_event_table[ievent], txg,
3647 cmdstr = internalstr;
3650 (void) localtime_r(&tsec, &t);
3651 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
3652 (void) printf("%s %s", tbuf, cmdstr);
3655 (void) printf("\n");
3658 (void) printf(" [");
3659 if (nvlist_lookup_uint64(records[i],
3660 ZPOOL_HIST_WHO, &who) == 0) {
3661 pwd = getpwuid((uid_t)who);
3663 (void) printf("user %s on",
3666 (void) printf("user %d on",
3669 (void) printf(gettext("no info]\n"));
3672 if (nvlist_lookup_string(records[i],
3673 ZPOOL_HIST_HOST, &hostname) == 0) {
3674 (void) printf(" %s", hostname);
3676 if (nvlist_lookup_string(records[i],
3677 ZPOOL_HIST_ZONE, &zonename) == 0) {
3678 (void) printf(":%s", zonename);
3682 (void) printf("\n");
3684 (void) printf("\n");
3691 * zpool history <pool>
3693 * Displays the history of commands that modified pools.
3698 zpool_do_history(int argc, char **argv)
3700 hist_cbdata_t cbdata = { 0 };
3704 cbdata.first = B_TRUE;
3706 while ((c = getopt(argc, argv, "li")) != -1) {
3712 cbdata.internal = 1;
3715 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3723 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
3726 if (argc == 0 && cbdata.first == B_TRUE) {
3727 (void) printf(gettext("no pools available\n"));
3735 get_callback(zpool_handle_t *zhp, void *data)
3737 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
3738 char value[MAXNAMELEN];
3739 zprop_source_t srctype;
3742 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
3745 * Skip the special fake placeholder. This will also skip
3746 * over the name property when 'all' is specified.
3748 if (pl->pl_prop == ZPOOL_PROP_NAME &&
3749 pl == cbp->cb_proplist)
3752 if (zpool_get_prop(zhp, pl->pl_prop,
3753 value, sizeof (value), &srctype) != 0)
3756 zprop_print_one_property(zpool_get_name(zhp), cbp,
3757 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL);
3763 zpool_do_get(int argc, char **argv)
3765 zprop_get_cbdata_t cb = { 0 };
3766 zprop_list_t fake_name = { 0 };
3772 cb.cb_first = B_TRUE;
3773 cb.cb_sources = ZPROP_SRC_ALL;
3774 cb.cb_columns[0] = GET_COL_NAME;
3775 cb.cb_columns[1] = GET_COL_PROPERTY;
3776 cb.cb_columns[2] = GET_COL_VALUE;
3777 cb.cb_columns[3] = GET_COL_SOURCE;
3778 cb.cb_type = ZFS_TYPE_POOL;
3780 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
3781 ZFS_TYPE_POOL) != 0)
3784 if (cb.cb_proplist != NULL) {
3785 fake_name.pl_prop = ZPOOL_PROP_NAME;
3786 fake_name.pl_width = strlen(gettext("NAME"));
3787 fake_name.pl_next = cb.cb_proplist;
3788 cb.cb_proplist = &fake_name;
3791 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
3794 if (cb.cb_proplist == &fake_name)
3795 zprop_free_list(fake_name.pl_next);
3797 zprop_free_list(cb.cb_proplist);
3802 typedef struct set_cbdata {
3805 boolean_t cb_any_successful;
3809 set_callback(zpool_handle_t *zhp, void *data)
3812 set_cbdata_t *cb = (set_cbdata_t *)data;
3814 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
3817 cb->cb_any_successful = B_TRUE;
3823 zpool_do_set(int argc, char **argv)
3825 set_cbdata_t cb = { 0 };
3828 if (argc > 1 && argv[1][0] == '-') {
3829 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3835 (void) fprintf(stderr, gettext("missing property=value "
3841 (void) fprintf(stderr, gettext("missing pool name\n"));
3846 (void) fprintf(stderr, gettext("too many pool names\n"));
3850 cb.cb_propname = argv[1];
3851 cb.cb_value = strchr(cb.cb_propname, '=');
3852 if (cb.cb_value == NULL) {
3853 (void) fprintf(stderr, gettext("missing value in "
3854 "property=value argument\n"));
3858 *(cb.cb_value) = '\0';
3861 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
3868 find_command_idx(char *command, int *idx)
3872 for (i = 0; i < NCOMMAND; i++) {
3873 if (command_table[i].name == NULL)
3876 if (strcmp(command, command_table[i].name) == 0) {
3885 main(int argc, char **argv)
3891 (void) setlocale(LC_ALL, "");
3892 (void) textdomain(TEXT_DOMAIN);
3894 if ((g_zfs = libzfs_init()) == NULL) {
3895 (void) fprintf(stderr, gettext("internal error: failed to "
3896 "initialize ZFS library\n"));
3900 libzfs_print_on_error(g_zfs, B_TRUE);
3905 * Make sure the user has specified some command.
3908 (void) fprintf(stderr, gettext("missing command\n"));
3917 if (strcmp(cmdname, "-?") == 0)
3920 zpool_set_history_str("zpool", argc, argv, history_str);
3921 verify(zpool_stage_history(g_zfs, history_str) == 0);
3924 * Run the appropriate command.
3926 if (find_command_idx(cmdname, &i) == 0) {
3927 current_command = &command_table[i];
3928 ret = command_table[i].func(argc - 1, argv + 1);
3929 } else if (strchr(cmdname, '=')) {
3930 verify(find_command_idx("set", &i) == 0);
3931 current_command = &command_table[i];
3932 ret = command_table[i].func(argc, argv);
3933 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
3935 * 'freeze' is a vile debugging abomination, so we treat
3939 int fd = open(ZFS_DEV, O_RDWR);
3940 (void) strcpy((void *)buf, argv[2]);
3941 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
3943 (void) fprintf(stderr, gettext("unrecognized "
3944 "command '%s'\n"), cmdname);
3951 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3952 * for the purposes of running ::findleaks.
3954 if (getenv("ZFS_ABORT") != NULL) {
3955 (void) printf("dumping core by request\n");