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;
888 while ((c = getopt(argc, argv, "f")) != -1) {
894 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
903 /* check arguments */
905 (void) fprintf(stderr, gettext("missing pool argument\n"));
910 for (i = 0; i < argc; i++) {
911 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
916 if (zpool_disable_datasets(zhp, force) != 0) {
922 if (zpool_export(zhp, force) != 0)
932 * Given a vdev configuration, determine the maximum width needed for the device
936 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
938 char *name = zpool_vdev_name(g_zfs, zhp, nv);
943 if (strlen(name) + depth > max)
944 max = strlen(name) + depth;
948 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
949 &child, &children) == 0) {
950 for (c = 0; c < children; c++)
951 if ((ret = max_width(zhp, child[c], depth + 2,
956 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
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_CHILDREN,
965 &child, &children) == 0) {
966 for (c = 0; c < children; c++)
967 if ((ret = max_width(zhp, child[c], depth + 2,
978 * Print the configuration of an exported pool. Iterate over all vdevs in the
979 * pool, printing out the name and status for each one.
982 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth,
983 boolean_t print_logs)
990 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
991 if (strcmp(type, VDEV_TYPE_MISSING) == 0)
994 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
995 (uint64_t **)&vs, &c) == 0);
997 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
998 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1000 if (vs->vs_aux != 0) {
1003 switch (vs->vs_aux) {
1004 case VDEV_AUX_OPEN_FAILED:
1005 (void) printf(gettext("cannot open"));
1008 case VDEV_AUX_BAD_GUID_SUM:
1009 (void) printf(gettext("missing device"));
1012 case VDEV_AUX_NO_REPLICAS:
1013 (void) printf(gettext("insufficient replicas"));
1016 case VDEV_AUX_VERSION_NEWER:
1017 (void) printf(gettext("newer version"));
1020 case VDEV_AUX_ERR_EXCEEDED:
1021 (void) printf(gettext("too many errors"));
1025 (void) printf(gettext("corrupted data"));
1029 (void) printf("\n");
1031 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1032 &child, &children) != 0)
1035 for (c = 0; c < children; c++) {
1036 uint64_t is_log = B_FALSE;
1038 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1040 if ((is_log && !print_logs) || (!is_log && print_logs))
1043 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1044 print_import_config(vname, child[c],
1045 namewidth, depth + 2, B_FALSE);
1049 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1050 &child, &children) == 0) {
1051 (void) printf(gettext("\tcache\n"));
1052 for (c = 0; c < children; c++) {
1053 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1054 (void) printf("\t %s\n", vname);
1059 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1060 &child, &children) == 0) {
1061 (void) printf(gettext("\tspares\n"));
1062 for (c = 0; c < children; c++) {
1063 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1064 (void) printf("\t %s\n", vname);
1071 * Display the status for the given pool.
1074 show_import(nvlist_t *config)
1076 uint64_t pool_state;
1087 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1089 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1091 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1093 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1096 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
1097 (uint64_t **)&vs, &vsc) == 0);
1098 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1100 reason = zpool_import_status(config, &msgid);
1102 (void) printf(gettext(" pool: %s\n"), name);
1103 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1104 (void) printf(gettext(" state: %s"), health);
1105 if (pool_state == POOL_STATE_DESTROYED)
1106 (void) printf(gettext(" (DESTROYED)"));
1107 (void) printf("\n");
1110 case ZPOOL_STATUS_MISSING_DEV_R:
1111 case ZPOOL_STATUS_MISSING_DEV_NR:
1112 case ZPOOL_STATUS_BAD_GUID_SUM:
1113 (void) printf(gettext("status: One or more devices are missing "
1114 "from the system.\n"));
1117 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1118 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1119 (void) printf(gettext("status: One or more devices contains "
1120 "corrupted data.\n"));
1123 case ZPOOL_STATUS_CORRUPT_DATA:
1124 (void) printf(gettext("status: The pool data is corrupted.\n"));
1127 case ZPOOL_STATUS_OFFLINE_DEV:
1128 (void) printf(gettext("status: One or more devices "
1129 "are offlined.\n"));
1132 case ZPOOL_STATUS_CORRUPT_POOL:
1133 (void) printf(gettext("status: The pool metadata is "
1137 case ZPOOL_STATUS_VERSION_OLDER:
1138 (void) printf(gettext("status: The pool is formatted using an "
1139 "older on-disk version.\n"));
1142 case ZPOOL_STATUS_VERSION_NEWER:
1143 (void) printf(gettext("status: The pool is formatted using an "
1144 "incompatible version.\n"));
1147 case ZPOOL_STATUS_HOSTID_MISMATCH:
1148 (void) printf(gettext("status: The pool was last accessed by "
1149 "another system.\n"));
1152 case ZPOOL_STATUS_FAULTED_DEV_R:
1153 case ZPOOL_STATUS_FAULTED_DEV_NR:
1154 (void) printf(gettext("status: One or more devices are "
1158 case ZPOOL_STATUS_BAD_LOG:
1159 (void) printf(gettext("status: An intent log record cannot be "
1165 * No other status can be seen when importing pools.
1167 assert(reason == ZPOOL_STATUS_OK);
1171 * Print out an action according to the overall state of the pool.
1173 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1174 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1175 (void) printf(gettext("action: The pool can be "
1176 "imported using its name or numeric identifier, "
1177 "though\n\tsome features will not be available "
1178 "without an explicit 'zpool upgrade'.\n"));
1179 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1180 (void) printf(gettext("action: The pool can be "
1181 "imported using its name or numeric "
1182 "identifier and\n\tthe '-f' flag.\n"));
1184 (void) printf(gettext("action: The pool can be "
1185 "imported using its name or numeric "
1187 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1188 (void) printf(gettext("action: The pool can be imported "
1189 "despite missing or damaged devices. The\n\tfault "
1190 "tolerance of the pool may be compromised if imported.\n"));
1193 case ZPOOL_STATUS_VERSION_NEWER:
1194 (void) printf(gettext("action: The pool cannot be "
1195 "imported. Access the pool on a system running "
1196 "newer\n\tsoftware, or recreate the pool from "
1199 case ZPOOL_STATUS_MISSING_DEV_R:
1200 case ZPOOL_STATUS_MISSING_DEV_NR:
1201 case ZPOOL_STATUS_BAD_GUID_SUM:
1202 (void) printf(gettext("action: The pool cannot be "
1203 "imported. Attach the missing\n\tdevices and try "
1207 (void) printf(gettext("action: The pool cannot be "
1208 "imported due to damaged devices or data.\n"));
1213 * If the state is "closed" or "can't open", and the aux state
1214 * is "corrupt data":
1216 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1217 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1218 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1219 if (pool_state == POOL_STATE_DESTROYED)
1220 (void) printf(gettext("\tThe pool was destroyed, "
1221 "but can be imported using the '-Df' flags.\n"));
1222 else if (pool_state != POOL_STATE_EXPORTED)
1223 (void) printf(gettext("\tThe pool may be active on "
1224 "another system, but can be imported using\n\t"
1225 "the '-f' flag.\n"));
1229 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1232 (void) printf(gettext("config:\n\n"));
1234 namewidth = max_width(NULL, nvroot, 0, 0);
1238 print_import_config(name, nvroot, namewidth, 0, B_FALSE);
1239 if (num_logs(nvroot) > 0) {
1240 (void) printf(gettext("\tlogs\n"));
1241 print_import_config(name, nvroot, namewidth, 0, B_TRUE);
1244 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1245 (void) printf(gettext("\n\tAdditional devices are known to "
1246 "be part of this pool, though their\n\texact "
1247 "configuration cannot be determined.\n"));
1252 * Perform the import for the given configuration. This passes the heavy
1253 * lifting off to zpool_import_props(), and then mounts the datasets contained
1257 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1258 int force, nvlist_t *props, boolean_t allowfaulted)
1260 zpool_handle_t *zhp;
1266 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1269 verify(nvlist_lookup_uint64(config,
1270 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1271 verify(nvlist_lookup_uint64(config,
1272 ZPOOL_CONFIG_VERSION, &version) == 0);
1273 if (version > SPA_VERSION) {
1274 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1275 "is formatted using a newer ZFS version\n"), name);
1277 } else if (state != POOL_STATE_EXPORTED && !force) {
1280 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1282 if ((unsigned long)hostid != gethostid()) {
1287 verify(nvlist_lookup_string(config,
1288 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1289 verify(nvlist_lookup_uint64(config,
1290 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1292 (void) fprintf(stderr, gettext("cannot import "
1293 "'%s': pool may be in use from other "
1294 "system, it was last accessed by %s "
1295 "(hostid: 0x%lx) on %s"), name, hostname,
1296 (unsigned long)hostid,
1297 asctime(localtime(&t)));
1298 (void) fprintf(stderr, gettext("use '-f' to "
1299 "import anyway\n"));
1303 (void) fprintf(stderr, gettext("cannot import '%s': "
1304 "pool may be in use from other system\n"), name);
1305 (void) fprintf(stderr, gettext("use '-f' to import "
1311 if (zpool_import_props(g_zfs, config, newname, props,
1315 if (newname != NULL)
1316 name = (char *)newname;
1318 verify((zhp = zpool_open_canfail(g_zfs, name)) != NULL);
1320 if (zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1330 * zpool import [-d dir] [-D]
1331 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1332 * [-d dir | -c cachefile] [-f] -a
1333 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1334 * [-d dir | -c cachefile] [-f] <pool | id> [newpool]
1336 * -c Read pool information from a cachefile instead of searching
1339 * -d Scan in a specific directory, other than /dev/dsk. More than
1340 * one directory can be specified using multiple '-d' options.
1342 * -D Scan for previously destroyed pools or import all or only
1343 * specified destroyed pools.
1345 * -R Temporarily import the pool, with all mountpoints relative to
1346 * the given root. The pool will remain exported when the machine
1349 * -f Force import, even if it appears that the pool is active.
1351 * -F Import even in the presence of faulted vdevs. This is an
1352 * intentionally undocumented option for testing purposes, and
1353 * treats the pool configuration as complete, leaving any bad
1354 * vdevs in the FAULTED state.
1356 * -a Import all pools found.
1358 * -o Set property=value and/or temporary mount options (without '=').
1360 * The import command scans for pools to import, and import pools based on pool
1361 * name and GUID. The pool can also be renamed as part of the import process.
1364 zpool_do_import(int argc, char **argv)
1366 char **searchdirs = NULL;
1370 nvlist_t *pools = NULL;
1371 boolean_t do_all = B_FALSE;
1372 boolean_t do_destroyed = B_FALSE;
1373 char *mntopts = NULL;
1374 boolean_t do_force = B_FALSE;
1377 uint64_t searchguid = 0;
1378 char *searchname = NULL;
1380 nvlist_t *found_config;
1381 nvlist_t *props = NULL;
1383 boolean_t allow_faulted = B_FALSE;
1384 uint64_t pool_state;
1385 char *cachefile = NULL;
1388 while ((c = getopt(argc, argv, ":ac:d:DfFo:p:R:")) != -1) {
1397 if (searchdirs == NULL) {
1398 searchdirs = safe_malloc(sizeof (char *));
1400 char **tmp = safe_malloc((nsearch + 1) *
1402 bcopy(searchdirs, tmp, nsearch *
1407 searchdirs[nsearch++] = optarg;
1410 do_destroyed = B_TRUE;
1416 allow_faulted = B_TRUE;
1419 if ((propval = strchr(optarg, '=')) != NULL) {
1422 if (add_prop_list(optarg, propval,
1430 if (add_prop_list(zpool_prop_to_name(
1431 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1433 if (nvlist_lookup_string(props,
1434 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1437 if (add_prop_list(zpool_prop_to_name(
1438 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1442 (void) fprintf(stderr, gettext("missing argument for "
1443 "'%c' option\n"), optopt);
1447 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1456 if (cachefile && nsearch != 0) {
1457 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1461 if (searchdirs == NULL) {
1462 searchdirs = safe_malloc(sizeof (char *));
1463 searchdirs[0] = "/dev/dsk";
1467 /* check argument count */
1470 (void) fprintf(stderr, gettext("too many arguments\n"));
1475 (void) fprintf(stderr, gettext("too many arguments\n"));
1480 * Check for the SYS_CONFIG privilege. We do this explicitly
1481 * here because otherwise any attempt to discover pools will
1484 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1485 (void) fprintf(stderr, gettext("cannot "
1486 "discover pools: permission denied\n"));
1493 * Depending on the arguments given, we do one of the following:
1495 * <none> Iterate through all pools and display information about
1498 * -a Iterate through all pools and try to import each one.
1500 * <id> Find the pool that corresponds to the given GUID/pool
1501 * name and import that one.
1503 * -D Above options applies only to destroyed pools.
1509 searchguid = strtoull(argv[0], &endptr, 10);
1510 if (errno != 0 || *endptr != '\0')
1511 searchname = argv[0];
1512 found_config = NULL;
1516 pools = zpool_find_import_cached(g_zfs, cachefile, searchname,
1518 } else if (searchname != NULL) {
1519 pools = zpool_find_import_byname(g_zfs, nsearch, searchdirs,
1523 * It's OK to search by guid even if searchguid is 0.
1525 pools = zpool_find_import_byguid(g_zfs, nsearch, searchdirs,
1529 if (pools == NULL) {
1531 (void) fprintf(stderr, gettext("cannot import '%s': "
1532 "no such pool available\n"), argv[0]);
1539 * At this point we have a list of import candidate configs. Even if
1540 * we were searching by pool name or guid, we still need to
1541 * post-process the list to deal with pool state and possible
1547 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1549 verify(nvpair_value_nvlist(elem, &config) == 0);
1551 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1553 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1555 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1562 (void) printf("\n");
1565 err |= do_import(config, NULL, mntopts,
1566 do_force, props, allow_faulted);
1568 show_import(config);
1569 } else if (searchname != NULL) {
1573 * We are searching for a pool based on name.
1575 verify(nvlist_lookup_string(config,
1576 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1578 if (strcmp(name, searchname) == 0) {
1579 if (found_config != NULL) {
1580 (void) fprintf(stderr, gettext(
1581 "cannot import '%s': more than "
1582 "one matching pool\n"), searchname);
1583 (void) fprintf(stderr, gettext(
1584 "import by numeric ID instead\n"));
1587 found_config = config;
1593 * Search for a pool by guid.
1595 verify(nvlist_lookup_uint64(config,
1596 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1598 if (guid == searchguid)
1599 found_config = config;
1604 * If we were searching for a specific pool, verify that we found a
1605 * pool, and then do the import.
1607 if (argc != 0 && err == 0) {
1608 if (found_config == NULL) {
1609 (void) fprintf(stderr, gettext("cannot import '%s': "
1610 "no such pool available\n"), argv[0]);
1613 err |= do_import(found_config, argc == 1 ? NULL :
1614 argv[1], mntopts, do_force, props, allow_faulted);
1619 * If we were just looking for pools, report an error if none were
1622 if (argc == 0 && first)
1623 (void) fprintf(stderr,
1624 gettext("no pools available to import\n"));
1631 return (err ? 1 : 0);
1634 typedef struct iostat_cbdata {
1635 zpool_list_t *cb_list;
1642 print_iostat_separator(iostat_cbdata_t *cb)
1646 for (i = 0; i < cb->cb_namewidth; i++)
1648 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1652 print_iostat_header(iostat_cbdata_t *cb)
1654 (void) printf("%*s capacity operations bandwidth\n",
1655 cb->cb_namewidth, "");
1656 (void) printf("%-*s used avail read write read write\n",
1657 cb->cb_namewidth, "pool");
1658 print_iostat_separator(cb);
1662 * Display a single statistic.
1665 print_one_stat(uint64_t value)
1669 zfs_nicenum(value, buf, sizeof (buf));
1670 (void) printf(" %5s", buf);
1674 * Print out all the statistics for the given vdev. This can either be the
1675 * toplevel configuration, or called recursively. If 'name' is NULL, then this
1676 * is a verbose output, and we don't want to display the toplevel pool stats.
1679 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1680 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1682 nvlist_t **oldchild, **newchild;
1684 vdev_stat_t *oldvs, *newvs;
1685 vdev_stat_t zerovs = { 0 };
1690 if (oldnv != NULL) {
1691 verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
1692 (uint64_t **)&oldvs, &c) == 0);
1697 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
1698 (uint64_t **)&newvs, &c) == 0);
1700 if (strlen(name) + depth > cb->cb_namewidth)
1701 (void) printf("%*s%s", depth, "", name);
1703 (void) printf("%*s%s%*s", depth, "", name,
1704 (int)(cb->cb_namewidth - strlen(name) - depth), "");
1706 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
1711 scale = (double)NANOSEC / tdelta;
1713 /* only toplevel vdevs have capacity stats */
1714 if (newvs->vs_space == 0) {
1715 (void) printf(" - -");
1717 print_one_stat(newvs->vs_alloc);
1718 print_one_stat(newvs->vs_space - newvs->vs_alloc);
1721 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
1722 oldvs->vs_ops[ZIO_TYPE_READ])));
1724 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
1725 oldvs->vs_ops[ZIO_TYPE_WRITE])));
1727 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
1728 oldvs->vs_bytes[ZIO_TYPE_READ])));
1730 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
1731 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
1733 (void) printf("\n");
1735 if (!cb->cb_verbose)
1738 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
1739 &newchild, &children) != 0)
1742 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
1743 &oldchild, &c) != 0)
1746 for (c = 0; c < children; c++) {
1747 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1748 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1749 newchild[c], cb, depth + 2);
1754 * Include level 2 ARC devices in iostat output
1756 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
1757 &newchild, &children) != 0)
1760 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
1761 &oldchild, &c) != 0)
1765 (void) printf("%-*s - - - - - "
1766 "-\n", cb->cb_namewidth, "cache");
1767 for (c = 0; c < children; c++) {
1768 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1769 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1770 newchild[c], cb, depth + 2);
1777 refresh_iostat(zpool_handle_t *zhp, void *data)
1779 iostat_cbdata_t *cb = data;
1783 * If the pool has disappeared, remove it from the list and continue.
1785 if (zpool_refresh_stats(zhp, &missing) != 0)
1789 pool_list_remove(cb->cb_list, zhp);
1795 * Callback to print out the iostats for the given pool.
1798 print_iostat(zpool_handle_t *zhp, void *data)
1800 iostat_cbdata_t *cb = data;
1801 nvlist_t *oldconfig, *newconfig;
1802 nvlist_t *oldnvroot, *newnvroot;
1804 newconfig = zpool_get_config(zhp, &oldconfig);
1806 if (cb->cb_iteration == 1)
1809 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
1812 if (oldconfig == NULL)
1815 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
1819 * Print out the statistics for the pool.
1821 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
1824 print_iostat_separator(cb);
1830 get_namewidth(zpool_handle_t *zhp, void *data)
1832 iostat_cbdata_t *cb = data;
1833 nvlist_t *config, *nvroot;
1835 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
1836 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1838 if (!cb->cb_verbose)
1839 cb->cb_namewidth = strlen(zpool_get_name(zhp));
1841 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
1845 * The width must fall into the range [10,38]. The upper limit is the
1846 * maximum we can have and still fit in 80 columns.
1848 if (cb->cb_namewidth < 10)
1849 cb->cb_namewidth = 10;
1850 if (cb->cb_namewidth > 38)
1851 cb->cb_namewidth = 38;
1857 * zpool iostat [-v] [pool] ... [interval [count]]
1859 * -v Display statistics for individual vdevs
1861 * This command can be tricky because we want to be able to deal with pool
1862 * creation/destruction as well as vdev configuration changes. The bulk of this
1863 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
1864 * on pool_list_update() to detect the addition of new pools. Configuration
1865 * changes are all handled within libzfs.
1868 zpool_do_iostat(int argc, char **argv)
1873 unsigned long interval = 0, count = 0;
1875 boolean_t verbose = B_FALSE;
1879 while ((c = getopt(argc, argv, "v")) != -1) {
1885 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1895 * Determine if the last argument is an integer or a pool name
1897 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1901 interval = strtoul(argv[argc - 1], &end, 10);
1903 if (*end == '\0' && errno == 0) {
1904 if (interval == 0) {
1905 (void) fprintf(stderr, gettext("interval "
1906 "cannot be zero\n"));
1911 * Ignore the last parameter
1916 * If this is not a valid number, just plow on. The
1917 * user will get a more informative error message later
1925 * If the last argument is also an integer, then we have both a count
1928 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1933 interval = strtoul(argv[argc - 1], &end, 10);
1935 if (*end == '\0' && errno == 0) {
1936 if (interval == 0) {
1937 (void) fprintf(stderr, gettext("interval "
1938 "cannot be zero\n"));
1943 * Ignore the last parameter
1952 * Construct the list of all interesting pools.
1955 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
1958 if (pool_list_count(list) == 0 && argc != 0) {
1959 pool_list_free(list);
1963 if (pool_list_count(list) == 0 && interval == 0) {
1964 pool_list_free(list);
1965 (void) fprintf(stderr, gettext("no pools available\n"));
1970 * Enter the main iostat loop.
1973 cb.cb_verbose = verbose;
1974 cb.cb_iteration = 0;
1975 cb.cb_namewidth = 0;
1978 pool_list_update(list);
1980 if ((npools = pool_list_count(list)) == 0)
1984 * Refresh all statistics. This is done as an explicit step
1985 * before calculating the maximum name width, so that any
1986 * configuration changes are properly accounted for.
1988 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
1991 * Iterate over all pools to determine the maximum width
1992 * for the pool / device name column across all pools.
1994 cb.cb_namewidth = 0;
1995 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
1998 * If it's the first time, or verbose mode, print the header.
2000 if (++cb.cb_iteration == 1 || verbose)
2001 print_iostat_header(&cb);
2003 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2006 * If there's more than one pool, and we're not in verbose mode
2007 * (which prints a separator for us), then print a separator.
2009 if (npools > 1 && !verbose)
2010 print_iostat_separator(&cb);
2013 (void) printf("\n");
2016 * Flush the output so that redirection to a file isn't buffered
2019 (void) fflush(stdout);
2024 if (count != 0 && --count == 0)
2027 (void) sleep(interval);
2030 pool_list_free(list);
2035 typedef struct list_cbdata {
2036 boolean_t cb_scripted;
2038 zprop_list_t *cb_proplist;
2042 * Given a list of columns to display, output appropriate headers for each one.
2045 print_header(zprop_list_t *pl)
2048 boolean_t first = B_TRUE;
2049 boolean_t right_justify;
2051 for (; pl != NULL; pl = pl->pl_next) {
2052 if (pl->pl_prop == ZPROP_INVAL)
2060 header = zpool_prop_column_name(pl->pl_prop);
2061 right_justify = zpool_prop_align_right(pl->pl_prop);
2063 if (pl->pl_next == NULL && !right_justify)
2064 (void) printf("%s", header);
2065 else if (right_justify)
2066 (void) printf("%*s", pl->pl_width, header);
2068 (void) printf("%-*s", pl->pl_width, header);
2071 (void) printf("\n");
2075 * Given a pool and a list of properties, print out all the properties according
2076 * to the described layout.
2079 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2081 boolean_t first = B_TRUE;
2082 char property[ZPOOL_MAXPROPLEN];
2084 boolean_t right_justify;
2087 for (; pl != NULL; pl = pl->pl_next) {
2090 (void) printf("\t");
2097 right_justify = B_FALSE;
2098 if (pl->pl_prop != ZPROP_INVAL) {
2099 if (zpool_get_prop(zhp, pl->pl_prop, property,
2100 sizeof (property), NULL) != 0)
2105 right_justify = zpool_prop_align_right(pl->pl_prop);
2110 width = pl->pl_width;
2113 * If this is being called in scripted mode, or if this is the
2114 * last column and it is left-justified, don't include a width
2117 if (scripted || (pl->pl_next == NULL && !right_justify))
2118 (void) printf("%s", propstr);
2119 else if (right_justify)
2120 (void) printf("%*s", width, propstr);
2122 (void) printf("%-*s", width, propstr);
2125 (void) printf("\n");
2129 * Generic callback function to list a pool.
2132 list_callback(zpool_handle_t *zhp, void *data)
2134 list_cbdata_t *cbp = data;
2136 if (cbp->cb_first) {
2137 if (!cbp->cb_scripted)
2138 print_header(cbp->cb_proplist);
2139 cbp->cb_first = B_FALSE;
2142 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2148 * zpool list [-H] [-o prop[,prop]*] [pool] ...
2150 * -H Scripted mode. Don't display headers, and separate properties
2152 * -o List of properties to display. Defaults to
2153 * "name,size,used,available,capacity,health,altroot"
2155 * List all pools in the system, whether or not they're healthy. Output space
2156 * statistics for each one, as well as health status summary.
2159 zpool_do_list(int argc, char **argv)
2163 list_cbdata_t cb = { 0 };
2164 static char default_props[] =
2165 "name,size,used,available,capacity,health,altroot";
2166 char *props = default_props;
2169 while ((c = getopt(argc, argv, ":Ho:")) != -1) {
2172 cb.cb_scripted = B_TRUE;
2178 (void) fprintf(stderr, gettext("missing argument for "
2179 "'%c' option\n"), optopt);
2183 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2192 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2195 cb.cb_first = B_TRUE;
2197 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2198 list_callback, &cb);
2200 zprop_free_list(cb.cb_proplist);
2202 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2203 (void) printf(gettext("no pools available\n"));
2211 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2218 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2219 &child, &children) != 0) {
2220 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2221 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV)-1) == 0)
2222 name += sizeof(_PATH_DEV)-1;
2223 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV)-1) == 0)
2224 path += sizeof(_PATH_DEV)-1;
2225 if (strcmp(name, path) == 0)
2230 for (c = 0; c < children; c++)
2231 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2238 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2240 boolean_t force = B_FALSE;
2243 char *poolname, *old_disk, *new_disk;
2244 zpool_handle_t *zhp;
2248 while ((c = getopt(argc, argv, "f")) != -1) {
2254 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2263 /* get pool name and check number of arguments */
2265 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2272 (void) fprintf(stderr,
2273 gettext("missing <device> specification\n"));
2281 (void) fprintf(stderr,
2282 gettext("missing <new_device> specification\n"));
2285 new_disk = old_disk;
2295 (void) fprintf(stderr, gettext("too many arguments\n"));
2299 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2302 if (zpool_get_config(zhp, NULL) == NULL) {
2303 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2309 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2311 if (nvroot == NULL) {
2316 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2318 nvlist_free(nvroot);
2325 * zpool replace [-f] <pool> <device> <new_device>
2327 * -f Force attach, even if <new_device> appears to be in use.
2329 * Replace <device> with <new_device>.
2333 zpool_do_replace(int argc, char **argv)
2335 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2339 * zpool attach [-f] <pool> <device> <new_device>
2341 * -f Force attach, even if <new_device> appears to be in use.
2343 * Attach <new_device> to the mirror containing <device>. If <device> is not
2344 * part of a mirror, then <device> will be transformed into a mirror of
2345 * <device> and <new_device>. In either case, <new_device> will begin life
2346 * with a DTL of [0, now], and will immediately begin to resilver itself.
2349 zpool_do_attach(int argc, char **argv)
2351 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2355 * zpool detach [-f] <pool> <device>
2357 * -f Force detach of <device>, even if DTLs argue against it
2358 * (not supported yet)
2360 * Detach a device from a mirror. The operation will be refused if <device>
2361 * is the last device in the mirror, or if the DTLs indicate that this device
2362 * has the only valid copy of some data.
2366 zpool_do_detach(int argc, char **argv)
2369 char *poolname, *path;
2370 zpool_handle_t *zhp;
2374 while ((c = getopt(argc, argv, "f")) != -1) {
2378 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2387 /* get pool name and check number of arguments */
2389 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2394 (void) fprintf(stderr,
2395 gettext("missing <device> specification\n"));
2402 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2405 ret = zpool_vdev_detach(zhp, path);
2413 * zpool online <pool> <device> ...
2416 zpool_do_online(int argc, char **argv)
2420 zpool_handle_t *zhp;
2422 vdev_state_t newstate;
2425 while ((c = getopt(argc, argv, "t")) != -1) {
2429 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2438 /* get pool name and check number of arguments */
2440 (void) fprintf(stderr, gettext("missing pool name\n"));
2444 (void) fprintf(stderr, gettext("missing device name\n"));
2450 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2453 for (i = 1; i < argc; i++) {
2454 if (zpool_vdev_online(zhp, argv[i], 0, &newstate) == 0) {
2455 if (newstate != VDEV_STATE_HEALTHY) {
2456 (void) printf(gettext("warning: device '%s' "
2457 "onlined, but remains in faulted state\n"),
2459 if (newstate == VDEV_STATE_FAULTED)
2460 (void) printf(gettext("use 'zpool "
2461 "clear' to restore a faulted "
2464 (void) printf(gettext("use 'zpool "
2465 "replace' to replace devices "
2466 "that are no longer present\n"));
2479 * zpool offline [-ft] <pool> <device> ...
2481 * -f Force the device into the offline state, even if doing
2482 * so would appear to compromise pool availability.
2483 * (not supported yet)
2485 * -t Only take the device off-line temporarily. The offline
2486 * state will not be persistent across reboots.
2490 zpool_do_offline(int argc, char **argv)
2494 zpool_handle_t *zhp;
2496 boolean_t istmp = B_FALSE;
2499 while ((c = getopt(argc, argv, "ft")) != -1) {
2506 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2515 /* get pool name and check number of arguments */
2517 (void) fprintf(stderr, gettext("missing pool name\n"));
2521 (void) fprintf(stderr, gettext("missing device name\n"));
2527 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2530 for (i = 1; i < argc; i++) {
2531 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
2541 * zpool clear <pool> [device]
2543 * Clear all errors associated with a pool or a particular device.
2546 zpool_do_clear(int argc, char **argv)
2549 zpool_handle_t *zhp;
2550 char *pool, *device;
2553 (void) fprintf(stderr, gettext("missing pool name\n"));
2558 (void) fprintf(stderr, gettext("too many arguments\n"));
2563 device = argc == 3 ? argv[2] : NULL;
2565 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
2568 if (zpool_clear(zhp, device) != 0)
2576 typedef struct scrub_cbdata {
2583 scrub_callback(zpool_handle_t *zhp, void *data)
2585 scrub_cbdata_t *cb = data;
2589 * Ignore faulted pools.
2591 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2592 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
2593 "currently unavailable\n"), zpool_get_name(zhp));
2597 err = zpool_scrub(zhp, cb->cb_type);
2603 * zpool scrub [-s] <pool> ...
2605 * -s Stop. Stops any in-progress scrub.
2608 zpool_do_scrub(int argc, char **argv)
2613 cb.cb_type = POOL_SCRUB_EVERYTHING;
2616 while ((c = getopt(argc, argv, "s")) != -1) {
2619 cb.cb_type = POOL_SCRUB_NONE;
2622 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2634 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2638 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
2641 typedef struct status_cbdata {
2643 boolean_t cb_allpools;
2644 boolean_t cb_verbose;
2645 boolean_t cb_explain;
2650 * Print out detailed scrub status.
2653 print_scrub_status(nvlist_t *nvroot)
2657 time_t start, end, now;
2658 double fraction_done;
2659 uint64_t examined, total, minutes_left, minutes_taken;
2662 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
2663 (uint64_t **)&vs, &vsc) == 0);
2666 * If there's never been a scrub, there's not much to say.
2668 if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
2669 (void) printf(gettext("none requested\n"));
2673 scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2674 "resilver" : "scrub";
2676 start = vs->vs_scrub_start;
2677 end = vs->vs_scrub_end;
2679 examined = vs->vs_scrub_examined;
2680 total = vs->vs_alloc;
2683 minutes_taken = (uint64_t)((end - start) / 60);
2685 (void) printf(gettext("%s %s after %lluh%um with %llu errors "
2687 scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
2688 (u_longlong_t)(minutes_taken / 60),
2689 (uint_t)(minutes_taken % 60),
2690 (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
2696 if (examined > total)
2699 fraction_done = (double)examined / total;
2700 minutes_left = (uint64_t)((now - start) *
2701 (1 - fraction_done) / fraction_done / 60);
2702 minutes_taken = (uint64_t)((now - start) / 60);
2704 (void) printf(gettext("%s in progress for %lluh%um, %.2f%% done, "
2705 "%lluh%um to go\n"),
2706 scrub_type, (u_longlong_t)(minutes_taken / 60),
2707 (uint_t)(minutes_taken % 60), 100 * fraction_done,
2708 (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
2711 typedef struct spare_cbdata {
2713 zpool_handle_t *cb_zhp;
2717 find_vdev(nvlist_t *nv, uint64_t search)
2723 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2727 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2728 &child, &children) == 0) {
2729 for (c = 0; c < children; c++)
2730 if (find_vdev(child[c], search))
2738 find_spare(zpool_handle_t *zhp, void *data)
2740 spare_cbdata_t *cbp = data;
2741 nvlist_t *config, *nvroot;
2743 config = zpool_get_config(zhp, NULL);
2744 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2747 if (find_vdev(nvroot, cbp->cb_guid)) {
2757 * Print out configuration state as requested by status_callback.
2760 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2761 int namewidth, int depth, boolean_t isspare, boolean_t print_logs)
2766 char rbuf[6], wbuf[6], cbuf[6], repaired[7];
2768 uint64_t notpresent;
2772 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2773 (uint64_t **)&vs, &c) == 0);
2775 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2776 &child, &children) != 0)
2779 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2782 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2785 if (vs->vs_aux == VDEV_AUX_SPARED)
2787 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2791 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
2795 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2796 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2797 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
2798 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
2801 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2802 ¬present) == 0) {
2804 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2805 (void) printf(" was %s", path);
2806 } else if (vs->vs_aux != 0) {
2809 switch (vs->vs_aux) {
2810 case VDEV_AUX_OPEN_FAILED:
2811 (void) printf(gettext("cannot open"));
2814 case VDEV_AUX_BAD_GUID_SUM:
2815 (void) printf(gettext("missing device"));
2818 case VDEV_AUX_NO_REPLICAS:
2819 (void) printf(gettext("insufficient replicas"));
2822 case VDEV_AUX_VERSION_NEWER:
2823 (void) printf(gettext("newer version"));
2826 case VDEV_AUX_SPARED:
2827 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2829 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
2830 if (strcmp(zpool_get_name(cb.cb_zhp),
2831 zpool_get_name(zhp)) == 0)
2832 (void) printf(gettext("currently in "
2835 (void) printf(gettext("in use by "
2837 zpool_get_name(cb.cb_zhp));
2838 zpool_close(cb.cb_zhp);
2840 (void) printf(gettext("currently in use"));
2844 case VDEV_AUX_ERR_EXCEEDED:
2845 (void) printf(gettext("too many errors"));
2848 case VDEV_AUX_IO_FAILURE:
2849 (void) printf(gettext("experienced I/O failures"));
2852 case VDEV_AUX_BAD_LOG:
2853 (void) printf(gettext("bad intent log"));
2857 (void) printf(gettext("corrupted data"));
2860 } else if (vs->vs_scrub_repaired != 0 && children == 0) {
2862 * Report bytes resilvered/repaired on leaf devices.
2864 zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
2865 (void) printf(gettext(" %s %s"), repaired,
2866 (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2867 "resilvered" : "repaired");
2870 (void) printf("\n");
2872 for (c = 0; c < children; c++) {
2873 uint64_t is_log = B_FALSE;
2875 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2877 if ((is_log && !print_logs) || (!is_log && print_logs))
2879 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
2880 print_status_config(zhp, vname, child[c],
2881 namewidth, depth + 2, isspare, B_FALSE);
2887 print_error_log(zpool_handle_t *zhp)
2889 nvlist_t *nverrlist = NULL;
2892 size_t len = MAXPATHLEN * 2;
2894 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
2895 (void) printf("errors: List of errors unavailable "
2896 "(insufficient privileges)\n");
2900 (void) printf("errors: Permanent errors have been "
2901 "detected in the following files:\n\n");
2903 pathname = safe_malloc(len);
2905 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
2907 uint64_t dsobj, obj;
2909 verify(nvpair_value_nvlist(elem, &nv) == 0);
2910 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
2912 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
2914 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
2915 (void) printf("%7s %s\n", "", pathname);
2918 nvlist_free(nverrlist);
2922 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
2931 (void) printf(gettext("\tspares\n"));
2933 for (i = 0; i < nspares; i++) {
2934 name = zpool_vdev_name(g_zfs, zhp, spares[i]);
2935 print_status_config(zhp, name, spares[i],
2936 namewidth, 2, B_TRUE, B_FALSE);
2942 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
2951 (void) printf(gettext("\tcache\n"));
2953 for (i = 0; i < nl2cache; i++) {
2954 name = zpool_vdev_name(g_zfs, zhp, l2cache[i]);
2955 print_status_config(zhp, name, l2cache[i],
2956 namewidth, 2, B_FALSE, B_FALSE);
2962 * Display a summary of pool status. Displays a summary such as:
2966 * reason: One or more devices ...
2967 * see: http://www.sun.com/msg/ZFS-xxxx-01
2973 * When given the '-v' option, we print out the complete config. If the '-e'
2974 * option is specified, then we print out error rate information as well.
2977 status_callback(zpool_handle_t *zhp, void *data)
2979 status_cbdata_t *cbp = data;
2980 nvlist_t *config, *nvroot;
2987 config = zpool_get_config(zhp, NULL);
2988 reason = zpool_get_status(zhp, &msgid);
2993 * If we were given 'zpool status -x', only report those pools with
2996 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
2997 if (!cbp->cb_allpools) {
2998 (void) printf(gettext("pool '%s' is healthy\n"),
2999 zpool_get_name(zhp));
3001 cbp->cb_first = B_FALSE;
3007 cbp->cb_first = B_FALSE;
3009 (void) printf("\n");
3011 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3013 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
3014 (uint64_t **)&vs, &c) == 0);
3015 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3017 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3018 (void) printf(gettext(" state: %s\n"), health);
3021 case ZPOOL_STATUS_MISSING_DEV_R:
3022 (void) printf(gettext("status: One or more devices could not "
3023 "be opened. Sufficient replicas exist for\n\tthe pool to "
3024 "continue functioning in a degraded state.\n"));
3025 (void) printf(gettext("action: Attach the missing device and "
3026 "online it using 'zpool online'.\n"));
3029 case ZPOOL_STATUS_MISSING_DEV_NR:
3030 (void) printf(gettext("status: One or more devices could not "
3031 "be opened. There are insufficient\n\treplicas for the "
3032 "pool to continue functioning.\n"));
3033 (void) printf(gettext("action: Attach the missing device and "
3034 "online it using 'zpool online'.\n"));
3037 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3038 (void) printf(gettext("status: One or more devices could not "
3039 "be used because the label is missing or\n\tinvalid. "
3040 "Sufficient replicas exist for the pool to continue\n\t"
3041 "functioning in a degraded state.\n"));
3042 (void) printf(gettext("action: Replace the device using "
3043 "'zpool replace'.\n"));
3046 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3047 (void) printf(gettext("status: One or more devices could not "
3048 "be used because the label is missing \n\tor invalid. "
3049 "There are insufficient replicas for the pool to "
3050 "continue\n\tfunctioning.\n"));
3051 (void) printf(gettext("action: Destroy and re-create the pool "
3052 "from a backup source.\n"));
3055 case ZPOOL_STATUS_FAILING_DEV:
3056 (void) printf(gettext("status: One or more devices has "
3057 "experienced an unrecoverable error. An\n\tattempt was "
3058 "made to correct the error. Applications are "
3060 (void) printf(gettext("action: Determine if the device needs "
3061 "to be replaced, and clear the errors\n\tusing "
3062 "'zpool clear' or replace the device with 'zpool "
3066 case ZPOOL_STATUS_OFFLINE_DEV:
3067 (void) printf(gettext("status: One or more devices has "
3068 "been taken offline by the administrator.\n\tSufficient "
3069 "replicas exist for the pool to continue functioning in "
3070 "a\n\tdegraded state.\n"));
3071 (void) printf(gettext("action: Online the device using "
3072 "'zpool online' or replace the device with\n\t'zpool "
3076 case ZPOOL_STATUS_RESILVERING:
3077 (void) printf(gettext("status: One or more devices is "
3078 "currently being resilvered. The pool will\n\tcontinue "
3079 "to function, possibly in a degraded state.\n"));
3080 (void) printf(gettext("action: Wait for the resilver to "
3084 case ZPOOL_STATUS_CORRUPT_DATA:
3085 (void) printf(gettext("status: One or more devices has "
3086 "experienced an error resulting in data\n\tcorruption. "
3087 "Applications may be affected.\n"));
3088 (void) printf(gettext("action: Restore the file in question "
3089 "if possible. Otherwise restore the\n\tentire pool from "
3093 case ZPOOL_STATUS_CORRUPT_POOL:
3094 (void) printf(gettext("status: The pool metadata is corrupted "
3095 "and the pool cannot be opened.\n"));
3096 (void) printf(gettext("action: Destroy and re-create the pool "
3097 "from a backup source.\n"));
3100 case ZPOOL_STATUS_VERSION_OLDER:
3101 (void) printf(gettext("status: The pool is formatted using an "
3102 "older on-disk format. The pool can\n\tstill be used, but "
3103 "some features are unavailable.\n"));
3104 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3105 "upgrade'. Once this is done, the\n\tpool will no longer "
3106 "be accessible on older software versions.\n"));
3109 case ZPOOL_STATUS_VERSION_NEWER:
3110 (void) printf(gettext("status: The pool has been upgraded to a "
3111 "newer, incompatible on-disk version.\n\tThe pool cannot "
3112 "be accessed on this system.\n"));
3113 (void) printf(gettext("action: Access the pool from a system "
3114 "running more recent software, or\n\trestore the pool from "
3118 case ZPOOL_STATUS_FAULTED_DEV_R:
3119 (void) printf(gettext("status: One or more devices are "
3120 "faulted in response to persistent errors.\n\tSufficient "
3121 "replicas exist for the pool to continue functioning "
3122 "in a\n\tdegraded state.\n"));
3123 (void) printf(gettext("action: Replace the faulted device, "
3124 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3127 case ZPOOL_STATUS_FAULTED_DEV_NR:
3128 (void) printf(gettext("status: One or more devices are "
3129 "faulted in response to persistent errors. There are "
3130 "insufficient replicas for the pool to\n\tcontinue "
3132 (void) printf(gettext("action: Destroy and re-create the pool "
3133 "from a backup source. Manually marking the device\n"
3134 "\trepaired using 'zpool clear' may allow some data "
3135 "to be recovered.\n"));
3138 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3139 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3140 (void) printf(gettext("status: One or more devices are "
3141 "faulted in response to IO failures.\n"));
3142 (void) printf(gettext("action: Make sure the affected devices "
3143 "are connected, then run 'zpool clear'.\n"));
3146 case ZPOOL_STATUS_BAD_LOG:
3147 (void) printf(gettext("status: An intent log record "
3148 "could not be read.\n"
3149 "\tWaiting for adminstrator intervention to fix the "
3150 "faulted pool.\n"));
3151 (void) printf(gettext("action: Either restore the affected "
3152 "device(s) and run 'zpool online',\n"
3153 "\tor ignore the intent log records by running "
3154 "'zpool clear'.\n"));
3159 * The remaining errors can't actually be generated, yet.
3161 assert(reason == ZPOOL_STATUS_OK);
3165 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3168 if (config != NULL) {
3171 nvlist_t **spares, **l2cache;
3172 uint_t nspares, nl2cache;
3175 (void) printf(gettext(" scrub: "));
3176 print_scrub_status(nvroot);
3178 namewidth = max_width(zhp, nvroot, 0, 0);
3182 (void) printf(gettext("config:\n\n"));
3183 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3184 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3185 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3186 namewidth, 0, B_FALSE, B_FALSE);
3187 if (num_logs(nvroot) > 0)
3188 print_status_config(zhp, "logs", nvroot, namewidth, 0,
3191 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3192 &l2cache, &nl2cache) == 0)
3193 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3195 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3196 &spares, &nspares) == 0)
3197 print_spares(zhp, spares, nspares, namewidth);
3199 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3201 nvlist_t *nverrlist = NULL;
3204 * If the approximate error count is small, get a
3205 * precise count by fetching the entire log and
3206 * uniquifying the results.
3208 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3209 zpool_get_errlog(zhp, &nverrlist) == 0) {
3214 while ((elem = nvlist_next_nvpair(nverrlist,
3219 nvlist_free(nverrlist);
3221 (void) printf("\n");
3224 (void) printf(gettext("errors: No known data "
3226 else if (!cbp->cb_verbose)
3227 (void) printf(gettext("errors: %llu data "
3228 "errors, use '-v' for a list\n"),
3229 (u_longlong_t)nerr);
3231 print_error_log(zhp);
3234 (void) printf(gettext("config: The configuration cannot be "
3242 * zpool status [-vx] [pool] ...
3244 * -v Display complete error logs
3245 * -x Display only pools with potential problems
3247 * Describes the health status of all pools or some subset.
3250 zpool_do_status(int argc, char **argv)
3254 status_cbdata_t cb = { 0 };
3257 while ((c = getopt(argc, argv, "vx")) != -1) {
3260 cb.cb_verbose = B_TRUE;
3263 cb.cb_explain = B_TRUE;
3266 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3275 cb.cb_first = B_TRUE;
3278 cb.cb_allpools = B_TRUE;
3280 ret = for_each_pool(argc, argv, B_TRUE, NULL, status_callback, &cb);
3282 if (argc == 0 && cb.cb_count == 0)
3283 (void) printf(gettext("no pools available\n"));
3284 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3285 (void) printf(gettext("all pools are healthy\n"));
3290 typedef struct upgrade_cbdata {
3295 uint64_t cb_version;
3300 upgrade_cb(zpool_handle_t *zhp, void *arg)
3302 upgrade_cbdata_t *cbp = arg;
3307 config = zpool_get_config(zhp, NULL);
3308 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3311 if (!cbp->cb_newer && version < SPA_VERSION) {
3313 if (cbp->cb_first) {
3314 (void) printf(gettext("The following pools are "
3315 "out of date, and can be upgraded. After "
3316 "being\nupgraded, these pools will no "
3317 "longer be accessible by older software "
3319 (void) printf(gettext("VER POOL\n"));
3320 (void) printf(gettext("--- ------------\n"));
3321 cbp->cb_first = B_FALSE;
3324 (void) printf("%2llu %s\n", (u_longlong_t)version,
3325 zpool_get_name(zhp));
3327 cbp->cb_first = B_FALSE;
3328 ret = zpool_upgrade(zhp, cbp->cb_version);
3330 (void) printf(gettext("Successfully upgraded "
3331 "'%s'\n\n"), zpool_get_name(zhp));
3334 } else if (cbp->cb_newer && version > SPA_VERSION) {
3335 assert(!cbp->cb_all);
3337 if (cbp->cb_first) {
3338 (void) printf(gettext("The following pools are "
3339 "formatted using a newer software version and\n"
3340 "cannot be accessed on the current system.\n\n"));
3341 (void) printf(gettext("VER POOL\n"));
3342 (void) printf(gettext("--- ------------\n"));
3343 cbp->cb_first = B_FALSE;
3346 (void) printf("%2llu %s\n", (u_longlong_t)version,
3347 zpool_get_name(zhp));
3356 upgrade_one(zpool_handle_t *zhp, void *data)
3358 upgrade_cbdata_t *cbp = data;
3359 uint64_t cur_version;
3362 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3363 (void) printf(gettext("'log' is now a reserved word\n"
3364 "Pool 'log' must be renamed using export and import"
3369 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3370 if (cur_version > cbp->cb_version) {
3371 (void) printf(gettext("Pool '%s' is already formatted "
3372 "using more current version '%llu'.\n"),
3373 zpool_get_name(zhp), cur_version);
3376 if (cur_version == cbp->cb_version) {
3377 (void) printf(gettext("Pool '%s' is already formatted "
3378 "using the current version.\n"), zpool_get_name(zhp));
3382 ret = zpool_upgrade(zhp, cbp->cb_version);
3385 (void) printf(gettext("Successfully upgraded '%s' "
3386 "from version %llu to version %llu\n\n"),
3387 zpool_get_name(zhp), (u_longlong_t)cur_version,
3388 (u_longlong_t)cbp->cb_version);
3397 * zpool upgrade [-V version] <-a | pool ...>
3399 * With no arguments, display downrev'd ZFS pool available for upgrade.
3400 * Individual pools can be upgraded by specifying the pool, and '-a' will
3401 * upgrade all pools.
3404 zpool_do_upgrade(int argc, char **argv)
3407 upgrade_cbdata_t cb = { 0 };
3409 boolean_t showversions = B_FALSE;
3414 while ((c = getopt(argc, argv, "avV:")) != -1) {
3420 showversions = B_TRUE;
3423 cb.cb_version = strtoll(optarg, &end, 10);
3424 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3425 cb.cb_version < SPA_VERSION_1) {
3426 (void) fprintf(stderr,
3427 gettext("invalid version '%s'\n"), optarg);
3432 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3443 if (cb.cb_version == 0) {
3444 cb.cb_version = SPA_VERSION;
3445 } else if (!cb.cb_all && argc == 0) {
3446 (void) fprintf(stderr, gettext("-V option is "
3447 "incompatible with other arguments\n"));
3452 if (cb.cb_all || argc != 0) {
3453 (void) fprintf(stderr, gettext("-v option is "
3454 "incompatible with other arguments\n"));
3457 } else if (cb.cb_all) {
3459 (void) fprintf(stderr, gettext("-a option should not "
3460 "be used along with a pool name\n"));
3465 (void) printf(gettext("This system is currently running "
3466 "ZFS pool version %llu.\n\n"), SPA_VERSION);
3467 cb.cb_first = B_TRUE;
3469 (void) printf(gettext("The following versions are "
3471 (void) printf(gettext("VER DESCRIPTION\n"));
3472 (void) printf("--- -----------------------------------------"
3473 "---------------\n");
3474 (void) printf(gettext(" 1 Initial ZFS version\n"));
3475 (void) printf(gettext(" 2 Ditto blocks "
3476 "(replicated metadata)\n"));
3477 (void) printf(gettext(" 3 Hot spares and double parity "
3479 (void) printf(gettext(" 4 zpool history\n"));
3480 (void) printf(gettext(" 5 Compression using the gzip "
3482 (void) printf(gettext(" 6 bootfs pool property\n"));
3483 (void) printf(gettext(" 7 Separate intent log devices\n"));
3484 (void) printf(gettext(" 8 Delegated administration\n"));
3485 (void) printf(gettext(" 9 refquota and refreservation "
3487 (void) printf(gettext(" 10 Cache devices\n"));
3488 (void) printf(gettext(" 11 Improved scrub performance\n"));
3489 (void) printf(gettext(" 12 Snapshot properties\n"));
3490 (void) printf(gettext(" 13 snapused property\n"));
3491 (void) printf(gettext("For more information on a particular "
3492 "version, including supported releases, see:\n\n"));
3493 (void) printf("http://www.opensolaris.org/os/community/zfs/"
3495 (void) printf(gettext("Where 'N' is the version number.\n"));
3496 } else if (argc == 0) {
3499 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3500 notfound = cb.cb_first;
3502 if (!cb.cb_all && ret == 0) {
3504 (void) printf("\n");
3505 cb.cb_first = B_TRUE;
3506 cb.cb_newer = B_TRUE;
3507 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3510 (void) printf("\n");
3516 (void) printf(gettext("All pools are formatted "
3517 "using this version.\n"));
3518 else if (!cb.cb_all)
3519 (void) printf(gettext("Use 'zpool upgrade -v' "
3520 "for a list of available versions and "
3521 "their associated\nfeatures.\n"));
3524 ret = for_each_pool(argc, argv, B_FALSE, NULL,
3531 typedef struct hist_cbdata {
3537 char *hist_event_table[LOG_END] = {
3553 "pool property set",
3557 "destroy_begin_sync",
3561 "permission update",
3562 "permission remove",
3563 "permission who remove",
3572 "filesystem version upgrade",
3574 "refreservation set",
3579 * Print out the command history for a specific pool.
3582 get_history_one(zpool_handle_t *zhp, void *data)
3598 char internalstr[MAXPATHLEN];
3599 hist_cbdata_t *cb = (hist_cbdata_t *)data;
3603 cb->first = B_FALSE;
3605 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
3607 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
3610 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
3611 &records, &numrecords) == 0);
3612 for (i = 0; i < numrecords; i++) {
3613 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
3617 /* is it an internal event or a standard event? */
3618 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
3620 if (cb->internal == 0)
3623 if (nvlist_lookup_uint64(records[i],
3624 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
3626 verify(nvlist_lookup_uint64(records[i],
3627 ZPOOL_HIST_TXG, &txg) == 0);
3628 verify(nvlist_lookup_string(records[i],
3629 ZPOOL_HIST_INT_STR, &pathstr) == 0);
3630 if (ievent >= LOG_END)
3632 (void) snprintf(internalstr,
3633 sizeof (internalstr),
3634 "[internal %s txg:%lld] %s",
3635 hist_event_table[ievent], txg,
3637 cmdstr = internalstr;
3640 (void) localtime_r(&tsec, &t);
3641 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
3642 (void) printf("%s %s", tbuf, cmdstr);
3645 (void) printf("\n");
3648 (void) printf(" [");
3649 if (nvlist_lookup_uint64(records[i],
3650 ZPOOL_HIST_WHO, &who) == 0) {
3651 pwd = getpwuid((uid_t)who);
3653 (void) printf("user %s on",
3656 (void) printf("user %d on",
3659 (void) printf(gettext("no info]\n"));
3662 if (nvlist_lookup_string(records[i],
3663 ZPOOL_HIST_HOST, &hostname) == 0) {
3664 (void) printf(" %s", hostname);
3666 if (nvlist_lookup_string(records[i],
3667 ZPOOL_HIST_ZONE, &zonename) == 0) {
3668 (void) printf(":%s", zonename);
3672 (void) printf("\n");
3674 (void) printf("\n");
3681 * zpool history <pool>
3683 * Displays the history of commands that modified pools.
3688 zpool_do_history(int argc, char **argv)
3690 hist_cbdata_t cbdata = { 0 };
3694 cbdata.first = B_TRUE;
3696 while ((c = getopt(argc, argv, "li")) != -1) {
3702 cbdata.internal = 1;
3705 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3713 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
3716 if (argc == 0 && cbdata.first == B_TRUE) {
3717 (void) printf(gettext("no pools available\n"));
3725 get_callback(zpool_handle_t *zhp, void *data)
3727 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
3728 char value[MAXNAMELEN];
3729 zprop_source_t srctype;
3732 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
3735 * Skip the special fake placeholder. This will also skip
3736 * over the name property when 'all' is specified.
3738 if (pl->pl_prop == ZPOOL_PROP_NAME &&
3739 pl == cbp->cb_proplist)
3742 if (zpool_get_prop(zhp, pl->pl_prop,
3743 value, sizeof (value), &srctype) != 0)
3746 zprop_print_one_property(zpool_get_name(zhp), cbp,
3747 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL);
3753 zpool_do_get(int argc, char **argv)
3755 zprop_get_cbdata_t cb = { 0 };
3756 zprop_list_t fake_name = { 0 };
3762 cb.cb_first = B_TRUE;
3763 cb.cb_sources = ZPROP_SRC_ALL;
3764 cb.cb_columns[0] = GET_COL_NAME;
3765 cb.cb_columns[1] = GET_COL_PROPERTY;
3766 cb.cb_columns[2] = GET_COL_VALUE;
3767 cb.cb_columns[3] = GET_COL_SOURCE;
3768 cb.cb_type = ZFS_TYPE_POOL;
3770 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
3771 ZFS_TYPE_POOL) != 0)
3774 if (cb.cb_proplist != NULL) {
3775 fake_name.pl_prop = ZPOOL_PROP_NAME;
3776 fake_name.pl_width = strlen(gettext("NAME"));
3777 fake_name.pl_next = cb.cb_proplist;
3778 cb.cb_proplist = &fake_name;
3781 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
3784 if (cb.cb_proplist == &fake_name)
3785 zprop_free_list(fake_name.pl_next);
3787 zprop_free_list(cb.cb_proplist);
3792 typedef struct set_cbdata {
3795 boolean_t cb_any_successful;
3799 set_callback(zpool_handle_t *zhp, void *data)
3802 set_cbdata_t *cb = (set_cbdata_t *)data;
3804 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
3807 cb->cb_any_successful = B_TRUE;
3813 zpool_do_set(int argc, char **argv)
3815 set_cbdata_t cb = { 0 };
3818 if (argc > 1 && argv[1][0] == '-') {
3819 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3825 (void) fprintf(stderr, gettext("missing property=value "
3831 (void) fprintf(stderr, gettext("missing pool name\n"));
3836 (void) fprintf(stderr, gettext("too many pool names\n"));
3840 cb.cb_propname = argv[1];
3841 cb.cb_value = strchr(cb.cb_propname, '=');
3842 if (cb.cb_value == NULL) {
3843 (void) fprintf(stderr, gettext("missing value in "
3844 "property=value argument\n"));
3848 *(cb.cb_value) = '\0';
3851 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
3858 find_command_idx(char *command, int *idx)
3862 for (i = 0; i < NCOMMAND; i++) {
3863 if (command_table[i].name == NULL)
3866 if (strcmp(command, command_table[i].name) == 0) {
3875 main(int argc, char **argv)
3881 (void) setlocale(LC_ALL, "");
3882 (void) textdomain(TEXT_DOMAIN);
3884 if ((g_zfs = libzfs_init()) == NULL) {
3885 (void) fprintf(stderr, gettext("internal error: failed to "
3886 "initialize ZFS library\n"));
3890 libzfs_print_on_error(g_zfs, B_TRUE);
3895 * Make sure the user has specified some command.
3898 (void) fprintf(stderr, gettext("missing command\n"));
3907 if (strcmp(cmdname, "-?") == 0)
3910 zpool_set_history_str("zpool", argc, argv, history_str);
3911 verify(zpool_stage_history(g_zfs, history_str) == 0);
3914 * Run the appropriate command.
3916 if (find_command_idx(cmdname, &i) == 0) {
3917 current_command = &command_table[i];
3918 ret = command_table[i].func(argc - 1, argv + 1);
3919 } else if (strchr(cmdname, '=')) {
3920 verify(find_command_idx("set", &i) == 0);
3921 current_command = &command_table[i];
3922 ret = command_table[i].func(argc, argv);
3923 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
3925 * 'freeze' is a vile debugging abomination, so we treat
3929 int fd = open(ZFS_DEV, O_RDWR);
3930 (void) strcpy((void *)buf, argv[2]);
3931 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
3933 (void) fprintf(stderr, gettext("unrecognized "
3934 "command '%s'\n"), cmdname);
3941 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3942 * for the purposes of running ::findleaks.
3944 if (getenv("ZFS_ABORT") != NULL) {
3945 (void) printf("dumping core by request\n");