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.
44 #include <sys/fs/zfs.h>
50 #include "zpool_util.h"
51 #include "zfs_comutil.h"
53 static int zpool_do_create(int, char **);
54 static int zpool_do_destroy(int, char **);
56 static int zpool_do_add(int, char **);
57 static int zpool_do_remove(int, char **);
59 static int zpool_do_list(int, char **);
60 static int zpool_do_iostat(int, char **);
61 static int zpool_do_status(int, char **);
63 static int zpool_do_online(int, char **);
64 static int zpool_do_offline(int, char **);
65 static int zpool_do_clear(int, char **);
67 static int zpool_do_attach(int, char **);
68 static int zpool_do_detach(int, char **);
69 static int zpool_do_replace(int, char **);
71 static int zpool_do_scrub(int, char **);
73 static int zpool_do_import(int, char **);
74 static int zpool_do_export(int, char **);
76 static int zpool_do_upgrade(int, char **);
78 static int zpool_do_history(int, char **);
80 static int zpool_do_get(int, char **);
81 static int zpool_do_set(int, char **);
84 * These libumem hooks provide a reasonable set of defaults for the allocator's
85 * debugging facilities.
90 _umem_debug_init(void)
92 return ("default,verbose"); /* $UMEM_DEBUG setting */
96 _umem_logging_init(void)
98 return ("fail,contents"); /* $UMEM_LOGGING setting */
126 typedef struct zpool_command {
128 int (*func)(int, char **);
133 * Master command table. Each ZFS command has a name, associated function, and
134 * usage message. The usage messages need to be internationalized, so we have
135 * to have a function to return the usage message based on a command index.
137 * These commands are organized according to how they are displayed in the usage
138 * message. An empty command (one with a NULL name) indicates an empty line in
139 * the generic usage message.
141 static zpool_command_t command_table[] = {
142 { "create", zpool_do_create, HELP_CREATE },
143 { "destroy", zpool_do_destroy, HELP_DESTROY },
145 { "add", zpool_do_add, HELP_ADD },
146 { "remove", zpool_do_remove, HELP_REMOVE },
148 { "list", zpool_do_list, HELP_LIST },
149 { "iostat", zpool_do_iostat, HELP_IOSTAT },
150 { "status", zpool_do_status, HELP_STATUS },
152 { "online", zpool_do_online, HELP_ONLINE },
153 { "offline", zpool_do_offline, HELP_OFFLINE },
154 { "clear", zpool_do_clear, HELP_CLEAR },
156 { "attach", zpool_do_attach, HELP_ATTACH },
157 { "detach", zpool_do_detach, HELP_DETACH },
158 { "replace", zpool_do_replace, HELP_REPLACE },
160 { "scrub", zpool_do_scrub, HELP_SCRUB },
162 { "import", zpool_do_import, HELP_IMPORT },
163 { "export", zpool_do_export, HELP_EXPORT },
164 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
166 { "history", zpool_do_history, HELP_HISTORY },
167 { "get", zpool_do_get, HELP_GET },
168 { "set", zpool_do_set, HELP_SET },
171 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
173 zpool_command_t *current_command;
174 static char history_str[HIS_MAX_RECORD_LEN];
177 get_usage(zpool_help_t idx) {
180 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
182 return (gettext("\tattach [-f] <pool> <device> "
185 return (gettext("\tclear <pool> [device]\n"));
187 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
188 "\t [-O file-system-property=value] ... \n"
189 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
191 return (gettext("\tdestroy [-f] <pool>\n"));
193 return (gettext("\tdetach <pool> <device>\n"));
195 return (gettext("\texport [-f] <pool> ...\n"));
197 return (gettext("\thistory [-il] [<pool>] ...\n"));
199 return (gettext("\timport [-d dir] [-D]\n"
200 "\timport [-o mntopts] [-o property=value] ... \n"
201 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] -a\n"
202 "\timport [-o mntopts] [-o property=value] ... \n"
203 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] "
204 "<pool | id> [newpool]\n"));
206 return (gettext("\tiostat [-v] [pool] ... [interval "
209 return (gettext("\tlist [-H] [-o property[,...]] "
212 return (gettext("\toffline [-t] <pool> <device> ...\n"));
214 return (gettext("\tonline <pool> <device> ...\n"));
216 return (gettext("\treplace [-f] <pool> <device> "
219 return (gettext("\tremove <pool> <device> ...\n"));
221 return (gettext("\tscrub [-s] <pool> ...\n"));
223 return (gettext("\tstatus [-vx] [pool] ...\n"));
225 return (gettext("\tupgrade\n"
227 "\tupgrade [-V version] <-a | pool ...>\n"));
229 return (gettext("\tget <\"all\" | property[,...]> "
232 return (gettext("\tset <property=value> <pool> \n"));
241 * Callback routine that will print out a pool property value.
244 print_prop_cb(int prop, void *cb)
248 (void) fprintf(fp, "\t%-13s ", zpool_prop_to_name(prop));
250 if (zpool_prop_readonly(prop))
251 (void) fprintf(fp, " NO ");
253 (void) fprintf(fp, " YES ");
255 if (zpool_prop_values(prop) == NULL)
256 (void) fprintf(fp, "-\n");
258 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
264 * Display usage message. If we're inside a command, display only the usage for
265 * that command. Otherwise, iterate over the entire command table and display
266 * a complete usage message.
269 usage(boolean_t requested)
271 FILE *fp = requested ? stdout : stderr;
273 if (current_command == NULL) {
276 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
278 gettext("where 'command' is one of the following:\n\n"));
280 for (i = 0; i < NCOMMAND; i++) {
281 if (command_table[i].name == NULL)
282 (void) fprintf(fp, "\n");
284 (void) fprintf(fp, "%s",
285 get_usage(command_table[i].usage));
288 (void) fprintf(fp, gettext("usage:\n"));
289 (void) fprintf(fp, "%s", get_usage(current_command->usage));
292 if (current_command != NULL &&
293 ((strcmp(current_command->name, "set") == 0) ||
294 (strcmp(current_command->name, "get") == 0) ||
295 (strcmp(current_command->name, "list") == 0))) {
298 gettext("\nthe following properties are supported:\n"));
300 (void) fprintf(fp, "\n\t%-13s %s %s\n\n",
301 "PROPERTY", "EDIT", "VALUES");
303 /* Iterate over all properties */
304 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
309 * See comments at end of main().
311 if (getenv("ZFS_ABORT") != NULL) {
312 (void) printf("dumping core by request\n");
316 exit(requested ? 0 : 2);
320 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
321 boolean_t print_logs)
328 (void) printf("\t%*s%s\n", indent, "", name);
330 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
331 &child, &children) != 0)
334 for (c = 0; c < children; c++) {
335 uint64_t is_log = B_FALSE;
337 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
339 if ((is_log && !print_logs) || (!is_log && print_logs))
342 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
343 print_vdev_tree(zhp, vname, child[c], indent + 2,
350 * Add a property pair (name, string-value) into a property nvlist.
353 add_prop_list(const char *propname, char *propval, nvlist_t **props,
356 zpool_prop_t prop = ZPROP_INVAL;
362 if (*props == NULL &&
363 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
364 (void) fprintf(stderr,
365 gettext("internal error: out of memory\n"));
372 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
373 (void) fprintf(stderr, gettext("property '%s' is "
374 "not a valid pool property\n"), propname);
377 normnm = zpool_prop_to_name(prop);
379 if ((fprop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
380 (void) fprintf(stderr, gettext("property '%s' is "
381 "not a valid file system property\n"), propname);
384 normnm = zfs_prop_to_name(fprop);
387 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
388 prop != ZPOOL_PROP_CACHEFILE) {
389 (void) fprintf(stderr, gettext("property '%s' "
390 "specified multiple times\n"), propname);
394 if (nvlist_add_string(proplist, normnm, propval) != 0) {
395 (void) fprintf(stderr, gettext("internal "
396 "error: out of memory\n"));
404 * zpool add [-fn] <pool> <vdev> ...
406 * -f Force addition of devices, even if they appear in use
407 * -n Do not add the devices, but display the resulting layout if
408 * they were to be added.
410 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
411 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
415 zpool_do_add(int argc, char **argv)
417 boolean_t force = B_FALSE;
418 boolean_t dryrun = B_FALSE;
427 while ((c = getopt(argc, argv, "fn")) != -1) {
436 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
445 /* get pool name and check number of arguments */
447 (void) fprintf(stderr, gettext("missing pool name argument\n"));
451 (void) fprintf(stderr, gettext("missing vdev specification\n"));
460 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
463 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
464 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
470 /* pass off to get_vdev_spec for processing */
471 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
473 if (nvroot == NULL) {
479 nvlist_t *poolnvroot;
481 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
484 (void) printf(gettext("would update '%s' to the following "
485 "configuration:\n"), zpool_get_name(zhp));
487 /* print original main pool and new tree */
488 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
489 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
491 /* Do the same for the logs */
492 if (num_logs(poolnvroot) > 0) {
493 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
494 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
495 } else if (num_logs(nvroot) > 0) {
496 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
501 ret = (zpool_add(zhp, nvroot) != 0);
511 * zpool remove <pool> <vdev> ...
513 * Removes the given vdev from the pool. Currently, this only supports removing
514 * spares and cache devices from the pool. Eventually, we'll want to support
515 * removing leaf vdevs (as an alias for 'detach') as well as toplevel vdevs.
518 zpool_do_remove(int argc, char **argv)
527 /* get pool name and check number of arguments */
529 (void) fprintf(stderr, gettext("missing pool name argument\n"));
533 (void) fprintf(stderr, gettext("missing device\n"));
539 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
542 for (i = 1; i < argc; i++) {
543 if (zpool_vdev_remove(zhp, argv[i]) != 0)
551 * zpool create [-fn] [-o property=value] ...
552 * [-O file-system-property=value] ...
553 * [-R root] [-m mountpoint] <pool> <dev> ...
555 * -f Force creation, even if devices appear in use
556 * -n Do not create the pool, but display the resulting layout if it
557 * were to be created.
558 * -R Create a pool under an alternate root
559 * -m Set default mountpoint for the root dataset. By default it's
561 * -o Set property=value.
562 * -O Set fsproperty=value in the pool's root file system
564 * Creates the named pool according to the given vdev specification. The
565 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
566 * we get the nvlist back from get_vdev_spec(), we either print out the contents
567 * (if '-n' was specified), or pass it to libzfs to do the creation.
570 zpool_do_create(int argc, char **argv)
572 boolean_t force = B_FALSE;
573 boolean_t dryrun = B_FALSE;
575 nvlist_t *nvroot = NULL;
578 char *altroot = NULL;
579 char *mountpoint = NULL;
580 nvlist_t *fsprops = NULL;
581 nvlist_t *props = NULL;
585 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
595 if (add_prop_list(zpool_prop_to_name(
596 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
598 if (nvlist_lookup_string(props,
599 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
602 if (add_prop_list(zpool_prop_to_name(
603 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
610 if ((propval = strchr(optarg, '=')) == NULL) {
611 (void) fprintf(stderr, gettext("missing "
612 "'=' for -o option\n"));
618 if (add_prop_list(optarg, propval, &props, B_TRUE))
622 if ((propval = strchr(optarg, '=')) == NULL) {
623 (void) fprintf(stderr, gettext("missing "
624 "'=' for -O option\n"));
630 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
634 (void) fprintf(stderr, gettext("missing argument for "
635 "'%c' option\n"), optopt);
638 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
647 /* get pool name and check number of arguments */
649 (void) fprintf(stderr, gettext("missing pool name argument\n"));
653 (void) fprintf(stderr, gettext("missing vdev specification\n"));
660 * As a special case, check for use of '/' in the name, and direct the
661 * user to use 'zfs create' instead.
663 if (strchr(poolname, '/') != NULL) {
664 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
665 "character '/' in pool name\n"), poolname);
666 (void) fprintf(stderr, gettext("use 'zfs create' to "
667 "create a dataset\n"));
671 /* pass off to get_vdev_spec for bulk processing */
672 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
677 /* make_root_vdev() allows 0 toplevel children if there are spares */
678 if (!zfs_allocatable_devs(nvroot)) {
679 (void) fprintf(stderr, gettext("invalid vdev "
680 "specification: at least one toplevel vdev must be "
686 if (altroot != NULL && altroot[0] != '/') {
687 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
688 "must be an absolute path\n"), altroot);
693 * Check the validity of the mountpoint and direct the user to use the
694 * '-m' mountpoint option if it looks like its in use.
696 if (mountpoint == NULL ||
697 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
698 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
699 char buf[MAXPATHLEN];
702 if (mountpoint && mountpoint[0] != '/') {
703 (void) fprintf(stderr, gettext("invalid mountpoint "
704 "'%s': must be an absolute path, 'legacy', or "
705 "'none'\n"), mountpoint);
709 if (mountpoint == NULL) {
711 (void) snprintf(buf, sizeof (buf), "%s/%s",
714 (void) snprintf(buf, sizeof (buf), "/%s",
718 (void) snprintf(buf, sizeof (buf), "%s%s",
719 altroot, mountpoint);
721 (void) snprintf(buf, sizeof (buf), "%s",
725 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
726 (void) fprintf(stderr, gettext("mountpoint '%s' : "
727 "%s\n"), buf, strerror(errno));
728 (void) fprintf(stderr, gettext("use '-m' "
729 "option to provide a different default\n"));
734 while (count < 3 && readdir(dirp) != NULL)
736 (void) closedir(dirp);
739 (void) fprintf(stderr, gettext("mountpoint "
740 "'%s' exists and is not empty\n"), buf);
741 (void) fprintf(stderr, gettext("use '-m' "
742 "option to provide a "
743 "different default\n"));
751 * For a dry run invocation, print out a basic message and run
752 * through all the vdevs in the list and print out in an
753 * appropriate hierarchy.
755 (void) printf(gettext("would create '%s' with the "
756 "following layout:\n\n"), poolname);
758 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
759 if (num_logs(nvroot) > 0)
760 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
765 * Hand off to libzfs.
767 if (zpool_create(g_zfs, poolname,
768 nvroot, props, fsprops) == 0) {
769 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
770 ZFS_TYPE_FILESYSTEM);
772 if (mountpoint != NULL)
773 verify(zfs_prop_set(pool,
775 ZFS_PROP_MOUNTPOINT),
777 if (zfs_mount(pool, NULL, 0) == 0)
778 ret = zfs_shareall(pool);
781 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
782 (void) fprintf(stderr, gettext("pool name may have "
789 nvlist_free(fsprops);
793 nvlist_free(fsprops);
800 * zpool destroy <pool>
802 * -f Forcefully unmount any datasets
804 * Destroy the given pool. Automatically unmounts any datasets in the pool.
807 zpool_do_destroy(int argc, char **argv)
809 boolean_t force = B_FALSE;
816 while ((c = getopt(argc, argv, "f")) != -1) {
822 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
831 /* check arguments */
833 (void) fprintf(stderr, gettext("missing pool argument\n"));
837 (void) fprintf(stderr, gettext("too many arguments\n"));
843 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
845 * As a special case, check for use of '/' in the name, and
846 * direct the user to use 'zfs destroy' instead.
848 if (strchr(pool, '/') != NULL)
849 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
850 "destroy a dataset\n"));
854 if (zpool_disable_datasets(zhp, force) != 0) {
855 (void) fprintf(stderr, gettext("could not destroy '%s': "
856 "could not unmount datasets\n"), zpool_get_name(zhp));
860 ret = (zpool_destroy(zhp) != 0);
868 * zpool export [-f] <pool> ...
870 * -f Forcefully unmount datasets
872 * Export the given pools. By default, the command will attempt to cleanly
873 * unmount any active datasets within the pool. If the '-f' flag is specified,
874 * then the datasets will be forcefully unmounted.
877 zpool_do_export(int argc, char **argv)
879 boolean_t force = B_FALSE;
880 boolean_t hardforce = B_FALSE;
887 while ((c = getopt(argc, argv, "fF")) != -1) {
896 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
905 /* check arguments */
907 (void) fprintf(stderr, gettext("missing pool argument\n"));
912 for (i = 0; i < argc; i++) {
913 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
918 if (zpool_disable_datasets(zhp, force) != 0) {
925 if (zpool_export_force(zhp) != 0)
927 } else if (zpool_export(zhp, force) != 0) {
938 * Given a vdev configuration, determine the maximum width needed for the device
942 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
944 char *name = zpool_vdev_name(g_zfs, zhp, nv);
949 if (strlen(name) + depth > max)
950 max = strlen(name) + depth;
954 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
955 &child, &children) == 0) {
956 for (c = 0; c < children; c++)
957 if ((ret = max_width(zhp, child[c], depth + 2,
962 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
963 &child, &children) == 0) {
964 for (c = 0; c < children; c++)
965 if ((ret = max_width(zhp, child[c], depth + 2,
970 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
971 &child, &children) == 0) {
972 for (c = 0; c < children; c++)
973 if ((ret = max_width(zhp, child[c], depth + 2,
984 * Print the configuration of an exported pool. Iterate over all vdevs in the
985 * pool, printing out the name and status for each one.
988 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth,
989 boolean_t print_logs)
996 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
997 if (strcmp(type, VDEV_TYPE_MISSING) == 0)
1000 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
1001 (uint64_t **)&vs, &c) == 0);
1003 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1004 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1006 if (vs->vs_aux != 0) {
1009 switch (vs->vs_aux) {
1010 case VDEV_AUX_OPEN_FAILED:
1011 (void) printf(gettext("cannot open"));
1014 case VDEV_AUX_BAD_GUID_SUM:
1015 (void) printf(gettext("missing device"));
1018 case VDEV_AUX_NO_REPLICAS:
1019 (void) printf(gettext("insufficient replicas"));
1022 case VDEV_AUX_VERSION_NEWER:
1023 (void) printf(gettext("newer version"));
1026 case VDEV_AUX_ERR_EXCEEDED:
1027 (void) printf(gettext("too many errors"));
1031 (void) printf(gettext("corrupted data"));
1035 (void) printf("\n");
1037 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1038 &child, &children) != 0)
1041 for (c = 0; c < children; c++) {
1042 uint64_t is_log = B_FALSE;
1044 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1046 if ((is_log && !print_logs) || (!is_log && print_logs))
1049 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1050 print_import_config(vname, child[c],
1051 namewidth, depth + 2, B_FALSE);
1055 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1056 &child, &children) == 0) {
1057 (void) printf(gettext("\tcache\n"));
1058 for (c = 0; c < children; c++) {
1059 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1060 (void) printf("\t %s\n", vname);
1065 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1066 &child, &children) == 0) {
1067 (void) printf(gettext("\tspares\n"));
1068 for (c = 0; c < children; c++) {
1069 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1070 (void) printf("\t %s\n", vname);
1077 * Display the status for the given pool.
1080 show_import(nvlist_t *config)
1082 uint64_t pool_state;
1093 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1095 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1097 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1099 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1102 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
1103 (uint64_t **)&vs, &vsc) == 0);
1104 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1106 reason = zpool_import_status(config, &msgid);
1108 (void) printf(gettext(" pool: %s\n"), name);
1109 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1110 (void) printf(gettext(" state: %s"), health);
1111 if (pool_state == POOL_STATE_DESTROYED)
1112 (void) printf(gettext(" (DESTROYED)"));
1113 (void) printf("\n");
1116 case ZPOOL_STATUS_MISSING_DEV_R:
1117 case ZPOOL_STATUS_MISSING_DEV_NR:
1118 case ZPOOL_STATUS_BAD_GUID_SUM:
1119 (void) printf(gettext("status: One or more devices are missing "
1120 "from the system.\n"));
1123 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1124 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1125 (void) printf(gettext("status: One or more devices contains "
1126 "corrupted data.\n"));
1129 case ZPOOL_STATUS_CORRUPT_DATA:
1130 (void) printf(gettext("status: The pool data is corrupted.\n"));
1133 case ZPOOL_STATUS_OFFLINE_DEV:
1134 (void) printf(gettext("status: One or more devices "
1135 "are offlined.\n"));
1138 case ZPOOL_STATUS_CORRUPT_POOL:
1139 (void) printf(gettext("status: The pool metadata is "
1143 case ZPOOL_STATUS_VERSION_OLDER:
1144 (void) printf(gettext("status: The pool is formatted using an "
1145 "older on-disk version.\n"));
1148 case ZPOOL_STATUS_VERSION_NEWER:
1149 (void) printf(gettext("status: The pool is formatted using an "
1150 "incompatible version.\n"));
1153 case ZPOOL_STATUS_HOSTID_MISMATCH:
1154 (void) printf(gettext("status: The pool was last accessed by "
1155 "another system.\n"));
1158 case ZPOOL_STATUS_FAULTED_DEV_R:
1159 case ZPOOL_STATUS_FAULTED_DEV_NR:
1160 (void) printf(gettext("status: One or more devices are "
1164 case ZPOOL_STATUS_BAD_LOG:
1165 (void) printf(gettext("status: An intent log record cannot be "
1171 * No other status can be seen when importing pools.
1173 assert(reason == ZPOOL_STATUS_OK);
1177 * Print out an action according to the overall state of the pool.
1179 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1180 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1181 (void) printf(gettext("action: The pool can be "
1182 "imported using its name or numeric identifier, "
1183 "though\n\tsome features will not be available "
1184 "without an explicit 'zpool upgrade'.\n"));
1185 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1186 (void) printf(gettext("action: The pool can be "
1187 "imported using its name or numeric "
1188 "identifier and\n\tthe '-f' flag.\n"));
1190 (void) printf(gettext("action: The pool can be "
1191 "imported using its name or numeric "
1193 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1194 (void) printf(gettext("action: The pool can be imported "
1195 "despite missing or damaged devices. The\n\tfault "
1196 "tolerance of the pool may be compromised if imported.\n"));
1199 case ZPOOL_STATUS_VERSION_NEWER:
1200 (void) printf(gettext("action: The pool cannot be "
1201 "imported. Access the pool on a system running "
1202 "newer\n\tsoftware, or recreate the pool from "
1205 case ZPOOL_STATUS_MISSING_DEV_R:
1206 case ZPOOL_STATUS_MISSING_DEV_NR:
1207 case ZPOOL_STATUS_BAD_GUID_SUM:
1208 (void) printf(gettext("action: The pool cannot be "
1209 "imported. Attach the missing\n\tdevices and try "
1213 (void) printf(gettext("action: The pool cannot be "
1214 "imported due to damaged devices or data.\n"));
1219 * If the state is "closed" or "can't open", and the aux state
1220 * is "corrupt data":
1222 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1223 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1224 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1225 if (pool_state == POOL_STATE_DESTROYED)
1226 (void) printf(gettext("\tThe pool was destroyed, "
1227 "but can be imported using the '-Df' flags.\n"));
1228 else if (pool_state != POOL_STATE_EXPORTED)
1229 (void) printf(gettext("\tThe pool may be active on "
1230 "another system, but can be imported using\n\t"
1231 "the '-f' flag.\n"));
1235 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1238 (void) printf(gettext("config:\n\n"));
1240 namewidth = max_width(NULL, nvroot, 0, 0);
1244 print_import_config(name, nvroot, namewidth, 0, B_FALSE);
1245 if (num_logs(nvroot) > 0) {
1246 (void) printf(gettext("\tlogs\n"));
1247 print_import_config(name, nvroot, namewidth, 0, B_TRUE);
1250 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1251 (void) printf(gettext("\n\tAdditional devices are known to "
1252 "be part of this pool, though their\n\texact "
1253 "configuration cannot be determined.\n"));
1258 * Perform the import for the given configuration. This passes the heavy
1259 * lifting off to zpool_import_props(), and then mounts the datasets contained
1263 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1264 int force, nvlist_t *props, boolean_t allowfaulted)
1266 zpool_handle_t *zhp;
1272 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1275 verify(nvlist_lookup_uint64(config,
1276 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1277 verify(nvlist_lookup_uint64(config,
1278 ZPOOL_CONFIG_VERSION, &version) == 0);
1279 if (version > SPA_VERSION) {
1280 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1281 "is formatted using a newer ZFS version\n"), name);
1283 } else if (state != POOL_STATE_EXPORTED && !force) {
1286 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1288 if ((unsigned long)hostid != gethostid()) {
1293 verify(nvlist_lookup_string(config,
1294 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1295 verify(nvlist_lookup_uint64(config,
1296 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1298 (void) fprintf(stderr, gettext("cannot import "
1299 "'%s': pool may be in use from other "
1300 "system, it was last accessed by %s "
1301 "(hostid: 0x%lx) on %s"), name, hostname,
1302 (unsigned long)hostid,
1303 asctime(localtime(&t)));
1304 (void) fprintf(stderr, gettext("use '-f' to "
1305 "import anyway\n"));
1309 (void) fprintf(stderr, gettext("cannot import '%s': "
1310 "pool may be in use from other system\n"), name);
1311 (void) fprintf(stderr, gettext("use '-f' to import "
1317 if (zpool_import_props(g_zfs, config, newname, props,
1321 if (newname != NULL)
1322 name = (char *)newname;
1324 verify((zhp = zpool_open_canfail(g_zfs, name)) != NULL);
1326 if (zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1336 * zpool import [-d dir] [-D]
1337 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1338 * [-d dir | -c cachefile] [-f] -a
1339 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1340 * [-d dir | -c cachefile] [-f] <pool | id> [newpool]
1342 * -c Read pool information from a cachefile instead of searching
1345 * -d Scan in a specific directory, other than /dev/dsk. More than
1346 * one directory can be specified using multiple '-d' options.
1348 * -D Scan for previously destroyed pools or import all or only
1349 * specified destroyed pools.
1351 * -R Temporarily import the pool, with all mountpoints relative to
1352 * the given root. The pool will remain exported when the machine
1355 * -f Force import, even if it appears that the pool is active.
1357 * -F Import even in the presence of faulted vdevs. This is an
1358 * intentionally undocumented option for testing purposes, and
1359 * treats the pool configuration as complete, leaving any bad
1360 * vdevs in the FAULTED state.
1362 * -a Import all pools found.
1364 * -o Set property=value and/or temporary mount options (without '=').
1366 * The import command scans for pools to import, and import pools based on pool
1367 * name and GUID. The pool can also be renamed as part of the import process.
1370 zpool_do_import(int argc, char **argv)
1372 char **searchdirs = NULL;
1376 nvlist_t *pools = NULL;
1377 boolean_t do_all = B_FALSE;
1378 boolean_t do_destroyed = B_FALSE;
1379 char *mntopts = NULL;
1380 boolean_t do_force = B_FALSE;
1383 uint64_t searchguid = 0;
1384 char *searchname = NULL;
1386 nvlist_t *found_config;
1387 nvlist_t *props = NULL;
1389 boolean_t allow_faulted = B_FALSE;
1390 uint64_t pool_state;
1391 char *cachefile = NULL;
1394 while ((c = getopt(argc, argv, ":ac:d:DfFo:p:R:")) != -1) {
1403 if (searchdirs == NULL) {
1404 searchdirs = safe_malloc(sizeof (char *));
1406 char **tmp = safe_malloc((nsearch + 1) *
1408 bcopy(searchdirs, tmp, nsearch *
1413 searchdirs[nsearch++] = optarg;
1416 do_destroyed = B_TRUE;
1422 allow_faulted = B_TRUE;
1425 if ((propval = strchr(optarg, '=')) != NULL) {
1428 if (add_prop_list(optarg, propval,
1436 if (add_prop_list(zpool_prop_to_name(
1437 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1439 if (nvlist_lookup_string(props,
1440 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1443 if (add_prop_list(zpool_prop_to_name(
1444 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1448 (void) fprintf(stderr, gettext("missing argument for "
1449 "'%c' option\n"), optopt);
1453 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1462 if (cachefile && nsearch != 0) {
1463 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1467 if (searchdirs == NULL) {
1468 searchdirs = safe_malloc(sizeof (char *));
1469 searchdirs[0] = "/dev/dsk";
1473 /* check argument count */
1476 (void) fprintf(stderr, gettext("too many arguments\n"));
1481 (void) fprintf(stderr, gettext("too many arguments\n"));
1486 * Check for the SYS_CONFIG privilege. We do this explicitly
1487 * here because otherwise any attempt to discover pools will
1490 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1491 (void) fprintf(stderr, gettext("cannot "
1492 "discover pools: permission denied\n"));
1499 * Depending on the arguments given, we do one of the following:
1501 * <none> Iterate through all pools and display information about
1504 * -a Iterate through all pools and try to import each one.
1506 * <id> Find the pool that corresponds to the given GUID/pool
1507 * name and import that one.
1509 * -D Above options applies only to destroyed pools.
1515 searchguid = strtoull(argv[0], &endptr, 10);
1516 if (errno != 0 || *endptr != '\0')
1517 searchname = argv[0];
1518 found_config = NULL;
1522 pools = zpool_find_import_cached(g_zfs, cachefile, searchname,
1524 } else if (searchname != NULL) {
1525 pools = zpool_find_import_byname(g_zfs, nsearch, searchdirs,
1529 * It's OK to search by guid even if searchguid is 0.
1531 pools = zpool_find_import_byguid(g_zfs, nsearch, searchdirs,
1535 if (pools == NULL) {
1537 (void) fprintf(stderr, gettext("cannot import '%s': "
1538 "no such pool available\n"), argv[0]);
1545 * At this point we have a list of import candidate configs. Even if
1546 * we were searching by pool name or guid, we still need to
1547 * post-process the list to deal with pool state and possible
1553 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1555 verify(nvpair_value_nvlist(elem, &config) == 0);
1557 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1559 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1561 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1568 (void) printf("\n");
1571 err |= do_import(config, NULL, mntopts,
1572 do_force, props, allow_faulted);
1574 show_import(config);
1575 } else if (searchname != NULL) {
1579 * We are searching for a pool based on name.
1581 verify(nvlist_lookup_string(config,
1582 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1584 if (strcmp(name, searchname) == 0) {
1585 if (found_config != NULL) {
1586 (void) fprintf(stderr, gettext(
1587 "cannot import '%s': more than "
1588 "one matching pool\n"), searchname);
1589 (void) fprintf(stderr, gettext(
1590 "import by numeric ID instead\n"));
1593 found_config = config;
1599 * Search for a pool by guid.
1601 verify(nvlist_lookup_uint64(config,
1602 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1604 if (guid == searchguid)
1605 found_config = config;
1610 * If we were searching for a specific pool, verify that we found a
1611 * pool, and then do the import.
1613 if (argc != 0 && err == 0) {
1614 if (found_config == NULL) {
1615 (void) fprintf(stderr, gettext("cannot import '%s': "
1616 "no such pool available\n"), argv[0]);
1619 err |= do_import(found_config, argc == 1 ? NULL :
1620 argv[1], mntopts, do_force, props, allow_faulted);
1625 * If we were just looking for pools, report an error if none were
1628 if (argc == 0 && first)
1629 (void) fprintf(stderr,
1630 gettext("no pools available to import\n"));
1637 return (err ? 1 : 0);
1640 typedef struct iostat_cbdata {
1641 zpool_list_t *cb_list;
1648 print_iostat_separator(iostat_cbdata_t *cb)
1652 for (i = 0; i < cb->cb_namewidth; i++)
1654 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1658 print_iostat_header(iostat_cbdata_t *cb)
1660 (void) printf("%*s capacity operations bandwidth\n",
1661 cb->cb_namewidth, "");
1662 (void) printf("%-*s used avail read write read write\n",
1663 cb->cb_namewidth, "pool");
1664 print_iostat_separator(cb);
1668 * Display a single statistic.
1671 print_one_stat(uint64_t value)
1675 zfs_nicenum(value, buf, sizeof (buf));
1676 (void) printf(" %5s", buf);
1680 * Print out all the statistics for the given vdev. This can either be the
1681 * toplevel configuration, or called recursively. If 'name' is NULL, then this
1682 * is a verbose output, and we don't want to display the toplevel pool stats.
1685 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1686 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1688 nvlist_t **oldchild, **newchild;
1690 vdev_stat_t *oldvs, *newvs;
1691 vdev_stat_t zerovs = { 0 };
1696 if (oldnv != NULL) {
1697 verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
1698 (uint64_t **)&oldvs, &c) == 0);
1703 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
1704 (uint64_t **)&newvs, &c) == 0);
1706 if (strlen(name) + depth > cb->cb_namewidth)
1707 (void) printf("%*s%s", depth, "", name);
1709 (void) printf("%*s%s%*s", depth, "", name,
1710 (int)(cb->cb_namewidth - strlen(name) - depth), "");
1712 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
1717 scale = (double)NANOSEC / tdelta;
1719 /* only toplevel vdevs have capacity stats */
1720 if (newvs->vs_space == 0) {
1721 (void) printf(" - -");
1723 print_one_stat(newvs->vs_alloc);
1724 print_one_stat(newvs->vs_space - newvs->vs_alloc);
1727 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
1728 oldvs->vs_ops[ZIO_TYPE_READ])));
1730 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
1731 oldvs->vs_ops[ZIO_TYPE_WRITE])));
1733 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
1734 oldvs->vs_bytes[ZIO_TYPE_READ])));
1736 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
1737 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
1739 (void) printf("\n");
1741 if (!cb->cb_verbose)
1744 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
1745 &newchild, &children) != 0)
1748 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
1749 &oldchild, &c) != 0)
1752 for (c = 0; c < children; c++) {
1753 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1754 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1755 newchild[c], cb, depth + 2);
1760 * Include level 2 ARC devices in iostat output
1762 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
1763 &newchild, &children) != 0)
1766 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
1767 &oldchild, &c) != 0)
1771 (void) printf("%-*s - - - - - "
1772 "-\n", cb->cb_namewidth, "cache");
1773 for (c = 0; c < children; c++) {
1774 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1775 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1776 newchild[c], cb, depth + 2);
1783 refresh_iostat(zpool_handle_t *zhp, void *data)
1785 iostat_cbdata_t *cb = data;
1789 * If the pool has disappeared, remove it from the list and continue.
1791 if (zpool_refresh_stats(zhp, &missing) != 0)
1795 pool_list_remove(cb->cb_list, zhp);
1801 * Callback to print out the iostats for the given pool.
1804 print_iostat(zpool_handle_t *zhp, void *data)
1806 iostat_cbdata_t *cb = data;
1807 nvlist_t *oldconfig, *newconfig;
1808 nvlist_t *oldnvroot, *newnvroot;
1810 newconfig = zpool_get_config(zhp, &oldconfig);
1812 if (cb->cb_iteration == 1)
1815 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
1818 if (oldconfig == NULL)
1821 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
1825 * Print out the statistics for the pool.
1827 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
1830 print_iostat_separator(cb);
1836 get_namewidth(zpool_handle_t *zhp, void *data)
1838 iostat_cbdata_t *cb = data;
1839 nvlist_t *config, *nvroot;
1841 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
1842 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1844 if (!cb->cb_verbose)
1845 cb->cb_namewidth = strlen(zpool_get_name(zhp));
1847 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
1851 * The width must fall into the range [10,38]. The upper limit is the
1852 * maximum we can have and still fit in 80 columns.
1854 if (cb->cb_namewidth < 10)
1855 cb->cb_namewidth = 10;
1856 if (cb->cb_namewidth > 38)
1857 cb->cb_namewidth = 38;
1863 * zpool iostat [-v] [pool] ... [interval [count]]
1865 * -v Display statistics for individual vdevs
1867 * This command can be tricky because we want to be able to deal with pool
1868 * creation/destruction as well as vdev configuration changes. The bulk of this
1869 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
1870 * on pool_list_update() to detect the addition of new pools. Configuration
1871 * changes are all handled within libzfs.
1874 zpool_do_iostat(int argc, char **argv)
1879 unsigned long interval = 0, count = 0;
1881 boolean_t verbose = B_FALSE;
1885 while ((c = getopt(argc, argv, "v")) != -1) {
1891 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1901 * Determine if the last argument is an integer or a pool name
1903 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1907 interval = strtoul(argv[argc - 1], &end, 10);
1909 if (*end == '\0' && errno == 0) {
1910 if (interval == 0) {
1911 (void) fprintf(stderr, gettext("interval "
1912 "cannot be zero\n"));
1917 * Ignore the last parameter
1922 * If this is not a valid number, just plow on. The
1923 * user will get a more informative error message later
1931 * If the last argument is also an integer, then we have both a count
1934 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1939 interval = strtoul(argv[argc - 1], &end, 10);
1941 if (*end == '\0' && errno == 0) {
1942 if (interval == 0) {
1943 (void) fprintf(stderr, gettext("interval "
1944 "cannot be zero\n"));
1949 * Ignore the last parameter
1958 * Construct the list of all interesting pools.
1961 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
1964 if (pool_list_count(list) == 0 && argc != 0) {
1965 pool_list_free(list);
1969 if (pool_list_count(list) == 0 && interval == 0) {
1970 pool_list_free(list);
1971 (void) fprintf(stderr, gettext("no pools available\n"));
1976 * Enter the main iostat loop.
1979 cb.cb_verbose = verbose;
1980 cb.cb_iteration = 0;
1981 cb.cb_namewidth = 0;
1984 pool_list_update(list);
1986 if ((npools = pool_list_count(list)) == 0)
1990 * Refresh all statistics. This is done as an explicit step
1991 * before calculating the maximum name width, so that any
1992 * configuration changes are properly accounted for.
1994 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
1997 * Iterate over all pools to determine the maximum width
1998 * for the pool / device name column across all pools.
2000 cb.cb_namewidth = 0;
2001 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2004 * If it's the first time, or verbose mode, print the header.
2006 if (++cb.cb_iteration == 1 || verbose)
2007 print_iostat_header(&cb);
2009 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2012 * If there's more than one pool, and we're not in verbose mode
2013 * (which prints a separator for us), then print a separator.
2015 if (npools > 1 && !verbose)
2016 print_iostat_separator(&cb);
2019 (void) printf("\n");
2022 * Flush the output so that redirection to a file isn't buffered
2025 (void) fflush(stdout);
2030 if (count != 0 && --count == 0)
2033 (void) sleep(interval);
2036 pool_list_free(list);
2041 typedef struct list_cbdata {
2042 boolean_t cb_scripted;
2044 zprop_list_t *cb_proplist;
2048 * Given a list of columns to display, output appropriate headers for each one.
2051 print_header(zprop_list_t *pl)
2054 boolean_t first = B_TRUE;
2055 boolean_t right_justify;
2057 for (; pl != NULL; pl = pl->pl_next) {
2058 if (pl->pl_prop == ZPROP_INVAL)
2066 header = zpool_prop_column_name(pl->pl_prop);
2067 right_justify = zpool_prop_align_right(pl->pl_prop);
2069 if (pl->pl_next == NULL && !right_justify)
2070 (void) printf("%s", header);
2071 else if (right_justify)
2072 (void) printf("%*s", pl->pl_width, header);
2074 (void) printf("%-*s", pl->pl_width, header);
2077 (void) printf("\n");
2081 * Given a pool and a list of properties, print out all the properties according
2082 * to the described layout.
2085 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2087 boolean_t first = B_TRUE;
2088 char property[ZPOOL_MAXPROPLEN];
2090 boolean_t right_justify;
2093 for (; pl != NULL; pl = pl->pl_next) {
2096 (void) printf("\t");
2103 right_justify = B_FALSE;
2104 if (pl->pl_prop != ZPROP_INVAL) {
2105 if (zpool_get_prop(zhp, pl->pl_prop, property,
2106 sizeof (property), NULL) != 0)
2111 right_justify = zpool_prop_align_right(pl->pl_prop);
2116 width = pl->pl_width;
2119 * If this is being called in scripted mode, or if this is the
2120 * last column and it is left-justified, don't include a width
2123 if (scripted || (pl->pl_next == NULL && !right_justify))
2124 (void) printf("%s", propstr);
2125 else if (right_justify)
2126 (void) printf("%*s", width, propstr);
2128 (void) printf("%-*s", width, propstr);
2131 (void) printf("\n");
2135 * Generic callback function to list a pool.
2138 list_callback(zpool_handle_t *zhp, void *data)
2140 list_cbdata_t *cbp = data;
2142 if (cbp->cb_first) {
2143 if (!cbp->cb_scripted)
2144 print_header(cbp->cb_proplist);
2145 cbp->cb_first = B_FALSE;
2148 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2154 * zpool list [-H] [-o prop[,prop]*] [pool] ...
2156 * -H Scripted mode. Don't display headers, and separate properties
2158 * -o List of properties to display. Defaults to
2159 * "name,size,used,available,capacity,health,altroot"
2161 * List all pools in the system, whether or not they're healthy. Output space
2162 * statistics for each one, as well as health status summary.
2165 zpool_do_list(int argc, char **argv)
2169 list_cbdata_t cb = { 0 };
2170 static char default_props[] =
2171 "name,size,used,available,capacity,health,altroot";
2172 char *props = default_props;
2175 while ((c = getopt(argc, argv, ":Ho:")) != -1) {
2178 cb.cb_scripted = B_TRUE;
2184 (void) fprintf(stderr, gettext("missing argument for "
2185 "'%c' option\n"), optopt);
2189 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2198 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2201 cb.cb_first = B_TRUE;
2203 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2204 list_callback, &cb);
2206 zprop_free_list(cb.cb_proplist);
2208 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2209 (void) printf(gettext("no pools available\n"));
2217 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2224 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2225 &child, &children) != 0) {
2226 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2227 if (strncmp(name, "/dev/dsk/", 9) == 0)
2229 if (strncmp(path, "/dev/dsk/", 9) == 0)
2231 if (strcmp(name, path) == 0)
2236 for (c = 0; c < children; c++)
2237 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2244 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2246 boolean_t force = B_FALSE;
2249 char *poolname, *old_disk, *new_disk;
2250 zpool_handle_t *zhp;
2254 while ((c = getopt(argc, argv, "f")) != -1) {
2260 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2269 /* get pool name and check number of arguments */
2271 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2278 (void) fprintf(stderr,
2279 gettext("missing <device> specification\n"));
2287 (void) fprintf(stderr,
2288 gettext("missing <new_device> specification\n"));
2291 new_disk = old_disk;
2301 (void) fprintf(stderr, gettext("too many arguments\n"));
2305 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2308 if (zpool_get_config(zhp, NULL) == NULL) {
2309 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2315 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2317 if (nvroot == NULL) {
2322 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2324 nvlist_free(nvroot);
2331 * zpool replace [-f] <pool> <device> <new_device>
2333 * -f Force attach, even if <new_device> appears to be in use.
2335 * Replace <device> with <new_device>.
2339 zpool_do_replace(int argc, char **argv)
2341 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2345 * zpool attach [-f] <pool> <device> <new_device>
2347 * -f Force attach, even if <new_device> appears to be in use.
2349 * Attach <new_device> to the mirror containing <device>. If <device> is not
2350 * part of a mirror, then <device> will be transformed into a mirror of
2351 * <device> and <new_device>. In either case, <new_device> will begin life
2352 * with a DTL of [0, now], and will immediately begin to resilver itself.
2355 zpool_do_attach(int argc, char **argv)
2357 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2361 * zpool detach [-f] <pool> <device>
2363 * -f Force detach of <device>, even if DTLs argue against it
2364 * (not supported yet)
2366 * Detach a device from a mirror. The operation will be refused if <device>
2367 * is the last device in the mirror, or if the DTLs indicate that this device
2368 * has the only valid copy of some data.
2372 zpool_do_detach(int argc, char **argv)
2375 char *poolname, *path;
2376 zpool_handle_t *zhp;
2380 while ((c = getopt(argc, argv, "f")) != -1) {
2384 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2393 /* get pool name and check number of arguments */
2395 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2400 (void) fprintf(stderr,
2401 gettext("missing <device> specification\n"));
2408 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2411 ret = zpool_vdev_detach(zhp, path);
2419 * zpool online <pool> <device> ...
2422 zpool_do_online(int argc, char **argv)
2426 zpool_handle_t *zhp;
2428 vdev_state_t newstate;
2431 while ((c = getopt(argc, argv, "t")) != -1) {
2435 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2444 /* get pool name and check number of arguments */
2446 (void) fprintf(stderr, gettext("missing pool name\n"));
2450 (void) fprintf(stderr, gettext("missing device name\n"));
2456 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2459 for (i = 1; i < argc; i++) {
2460 if (zpool_vdev_online(zhp, argv[i], 0, &newstate) == 0) {
2461 if (newstate != VDEV_STATE_HEALTHY) {
2462 (void) printf(gettext("warning: device '%s' "
2463 "onlined, but remains in faulted state\n"),
2465 if (newstate == VDEV_STATE_FAULTED)
2466 (void) printf(gettext("use 'zpool "
2467 "clear' to restore a faulted "
2470 (void) printf(gettext("use 'zpool "
2471 "replace' to replace devices "
2472 "that are no longer present\n"));
2485 * zpool offline [-ft] <pool> <device> ...
2487 * -f Force the device into the offline state, even if doing
2488 * so would appear to compromise pool availability.
2489 * (not supported yet)
2491 * -t Only take the device off-line temporarily. The offline
2492 * state will not be persistent across reboots.
2496 zpool_do_offline(int argc, char **argv)
2500 zpool_handle_t *zhp;
2502 boolean_t istmp = B_FALSE;
2505 while ((c = getopt(argc, argv, "ft")) != -1) {
2512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2521 /* get pool name and check number of arguments */
2523 (void) fprintf(stderr, gettext("missing pool name\n"));
2527 (void) fprintf(stderr, gettext("missing device name\n"));
2533 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2536 for (i = 1; i < argc; i++) {
2537 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
2547 * zpool clear <pool> [device]
2549 * Clear all errors associated with a pool or a particular device.
2552 zpool_do_clear(int argc, char **argv)
2555 zpool_handle_t *zhp;
2556 char *pool, *device;
2559 (void) fprintf(stderr, gettext("missing pool name\n"));
2564 (void) fprintf(stderr, gettext("too many arguments\n"));
2569 device = argc == 3 ? argv[2] : NULL;
2571 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
2574 if (zpool_clear(zhp, device) != 0)
2582 typedef struct scrub_cbdata {
2589 scrub_callback(zpool_handle_t *zhp, void *data)
2591 scrub_cbdata_t *cb = data;
2595 * Ignore faulted pools.
2597 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2598 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
2599 "currently unavailable\n"), zpool_get_name(zhp));
2603 err = zpool_scrub(zhp, cb->cb_type);
2609 * zpool scrub [-s] <pool> ...
2611 * -s Stop. Stops any in-progress scrub.
2614 zpool_do_scrub(int argc, char **argv)
2619 cb.cb_type = POOL_SCRUB_EVERYTHING;
2622 while ((c = getopt(argc, argv, "s")) != -1) {
2625 cb.cb_type = POOL_SCRUB_NONE;
2628 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2640 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2644 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
2647 typedef struct status_cbdata {
2649 boolean_t cb_allpools;
2650 boolean_t cb_verbose;
2651 boolean_t cb_explain;
2656 * Print out detailed scrub status.
2659 print_scrub_status(nvlist_t *nvroot)
2663 time_t start, end, now;
2664 double fraction_done;
2665 uint64_t examined, total, minutes_left, minutes_taken;
2668 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
2669 (uint64_t **)&vs, &vsc) == 0);
2672 * If there's never been a scrub, there's not much to say.
2674 if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
2675 (void) printf(gettext("none requested\n"));
2679 scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2680 "resilver" : "scrub";
2682 start = vs->vs_scrub_start;
2683 end = vs->vs_scrub_end;
2685 examined = vs->vs_scrub_examined;
2686 total = vs->vs_alloc;
2689 minutes_taken = (uint64_t)((end - start) / 60);
2691 (void) printf(gettext("%s %s after %lluh%um with %llu errors "
2693 scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
2694 (u_longlong_t)(minutes_taken / 60),
2695 (uint_t)(minutes_taken % 60),
2696 (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
2702 if (examined > total)
2705 fraction_done = (double)examined / total;
2706 minutes_left = (uint64_t)((now - start) *
2707 (1 - fraction_done) / fraction_done / 60);
2708 minutes_taken = (uint64_t)((now - start) / 60);
2710 (void) printf(gettext("%s in progress for %lluh%um, %.2f%% done, "
2711 "%lluh%um to go\n"),
2712 scrub_type, (u_longlong_t)(minutes_taken / 60),
2713 (uint_t)(minutes_taken % 60), 100 * fraction_done,
2714 (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
2717 typedef struct spare_cbdata {
2719 zpool_handle_t *cb_zhp;
2723 find_vdev(nvlist_t *nv, uint64_t search)
2729 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2733 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2734 &child, &children) == 0) {
2735 for (c = 0; c < children; c++)
2736 if (find_vdev(child[c], search))
2744 find_spare(zpool_handle_t *zhp, void *data)
2746 spare_cbdata_t *cbp = data;
2747 nvlist_t *config, *nvroot;
2749 config = zpool_get_config(zhp, NULL);
2750 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2753 if (find_vdev(nvroot, cbp->cb_guid)) {
2763 * Print out configuration state as requested by status_callback.
2766 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2767 int namewidth, int depth, boolean_t isspare, boolean_t print_logs)
2772 char rbuf[6], wbuf[6], cbuf[6], repaired[7];
2774 uint64_t notpresent;
2778 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2779 (uint64_t **)&vs, &c) == 0);
2781 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2782 &child, &children) != 0)
2785 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2788 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2791 if (vs->vs_aux == VDEV_AUX_SPARED)
2793 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2797 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
2801 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2802 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2803 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
2804 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
2807 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2808 ¬present) == 0) {
2810 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2811 (void) printf(" was %s", path);
2812 } else if (vs->vs_aux != 0) {
2815 switch (vs->vs_aux) {
2816 case VDEV_AUX_OPEN_FAILED:
2817 (void) printf(gettext("cannot open"));
2820 case VDEV_AUX_BAD_GUID_SUM:
2821 (void) printf(gettext("missing device"));
2824 case VDEV_AUX_NO_REPLICAS:
2825 (void) printf(gettext("insufficient replicas"));
2828 case VDEV_AUX_VERSION_NEWER:
2829 (void) printf(gettext("newer version"));
2832 case VDEV_AUX_SPARED:
2833 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2835 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
2836 if (strcmp(zpool_get_name(cb.cb_zhp),
2837 zpool_get_name(zhp)) == 0)
2838 (void) printf(gettext("currently in "
2841 (void) printf(gettext("in use by "
2843 zpool_get_name(cb.cb_zhp));
2844 zpool_close(cb.cb_zhp);
2846 (void) printf(gettext("currently in use"));
2850 case VDEV_AUX_ERR_EXCEEDED:
2851 (void) printf(gettext("too many errors"));
2854 case VDEV_AUX_IO_FAILURE:
2855 (void) printf(gettext("experienced I/O failures"));
2858 case VDEV_AUX_BAD_LOG:
2859 (void) printf(gettext("bad intent log"));
2863 (void) printf(gettext("corrupted data"));
2866 } else if (vs->vs_scrub_repaired != 0 && children == 0) {
2868 * Report bytes resilvered/repaired on leaf devices.
2870 zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
2871 (void) printf(gettext(" %s %s"), repaired,
2872 (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2873 "resilvered" : "repaired");
2876 (void) printf("\n");
2878 for (c = 0; c < children; c++) {
2879 uint64_t is_log = B_FALSE;
2881 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2883 if ((is_log && !print_logs) || (!is_log && print_logs))
2885 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
2886 print_status_config(zhp, vname, child[c],
2887 namewidth, depth + 2, isspare, B_FALSE);
2893 print_error_log(zpool_handle_t *zhp)
2895 nvlist_t *nverrlist = NULL;
2898 size_t len = MAXPATHLEN * 2;
2900 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
2901 (void) printf("errors: List of errors unavailable "
2902 "(insufficient privileges)\n");
2906 (void) printf("errors: Permanent errors have been "
2907 "detected in the following files:\n\n");
2909 pathname = safe_malloc(len);
2911 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
2913 uint64_t dsobj, obj;
2915 verify(nvpair_value_nvlist(elem, &nv) == 0);
2916 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
2918 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
2920 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
2921 (void) printf("%7s %s\n", "", pathname);
2924 nvlist_free(nverrlist);
2928 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
2937 (void) printf(gettext("\tspares\n"));
2939 for (i = 0; i < nspares; i++) {
2940 name = zpool_vdev_name(g_zfs, zhp, spares[i]);
2941 print_status_config(zhp, name, spares[i],
2942 namewidth, 2, B_TRUE, B_FALSE);
2948 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
2957 (void) printf(gettext("\tcache\n"));
2959 for (i = 0; i < nl2cache; i++) {
2960 name = zpool_vdev_name(g_zfs, zhp, l2cache[i]);
2961 print_status_config(zhp, name, l2cache[i],
2962 namewidth, 2, B_FALSE, B_FALSE);
2968 * Display a summary of pool status. Displays a summary such as:
2972 * reason: One or more devices ...
2973 * see: http://www.sun.com/msg/ZFS-xxxx-01
2979 * When given the '-v' option, we print out the complete config. If the '-e'
2980 * option is specified, then we print out error rate information as well.
2983 status_callback(zpool_handle_t *zhp, void *data)
2985 status_cbdata_t *cbp = data;
2986 nvlist_t *config, *nvroot;
2993 config = zpool_get_config(zhp, NULL);
2994 reason = zpool_get_status(zhp, &msgid);
2999 * If we were given 'zpool status -x', only report those pools with
3002 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3003 if (!cbp->cb_allpools) {
3004 (void) printf(gettext("pool '%s' is healthy\n"),
3005 zpool_get_name(zhp));
3007 cbp->cb_first = B_FALSE;
3013 cbp->cb_first = B_FALSE;
3015 (void) printf("\n");
3017 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3019 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
3020 (uint64_t **)&vs, &c) == 0);
3021 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3023 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3024 (void) printf(gettext(" state: %s\n"), health);
3027 case ZPOOL_STATUS_MISSING_DEV_R:
3028 (void) printf(gettext("status: One or more devices could not "
3029 "be opened. Sufficient replicas exist for\n\tthe pool to "
3030 "continue functioning in a degraded state.\n"));
3031 (void) printf(gettext("action: Attach the missing device and "
3032 "online it using 'zpool online'.\n"));
3035 case ZPOOL_STATUS_MISSING_DEV_NR:
3036 (void) printf(gettext("status: One or more devices could not "
3037 "be opened. There are insufficient\n\treplicas for the "
3038 "pool to continue functioning.\n"));
3039 (void) printf(gettext("action: Attach the missing device and "
3040 "online it using 'zpool online'.\n"));
3043 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3044 (void) printf(gettext("status: One or more devices could not "
3045 "be used because the label is missing or\n\tinvalid. "
3046 "Sufficient replicas exist for the pool to continue\n\t"
3047 "functioning in a degraded state.\n"));
3048 (void) printf(gettext("action: Replace the device using "
3049 "'zpool replace'.\n"));
3052 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3053 (void) printf(gettext("status: One or more devices could not "
3054 "be used because the label is missing \n\tor invalid. "
3055 "There are insufficient replicas for the pool to "
3056 "continue\n\tfunctioning.\n"));
3057 (void) printf(gettext("action: Destroy and re-create the pool "
3058 "from a backup source.\n"));
3061 case ZPOOL_STATUS_FAILING_DEV:
3062 (void) printf(gettext("status: One or more devices has "
3063 "experienced an unrecoverable error. An\n\tattempt was "
3064 "made to correct the error. Applications are "
3066 (void) printf(gettext("action: Determine if the device needs "
3067 "to be replaced, and clear the errors\n\tusing "
3068 "'zpool clear' or replace the device with 'zpool "
3072 case ZPOOL_STATUS_OFFLINE_DEV:
3073 (void) printf(gettext("status: One or more devices has "
3074 "been taken offline by the administrator.\n\tSufficient "
3075 "replicas exist for the pool to continue functioning in "
3076 "a\n\tdegraded state.\n"));
3077 (void) printf(gettext("action: Online the device using "
3078 "'zpool online' or replace the device with\n\t'zpool "
3082 case ZPOOL_STATUS_RESILVERING:
3083 (void) printf(gettext("status: One or more devices is "
3084 "currently being resilvered. The pool will\n\tcontinue "
3085 "to function, possibly in a degraded state.\n"));
3086 (void) printf(gettext("action: Wait for the resilver to "
3090 case ZPOOL_STATUS_CORRUPT_DATA:
3091 (void) printf(gettext("status: One or more devices has "
3092 "experienced an error resulting in data\n\tcorruption. "
3093 "Applications may be affected.\n"));
3094 (void) printf(gettext("action: Restore the file in question "
3095 "if possible. Otherwise restore the\n\tentire pool from "
3099 case ZPOOL_STATUS_CORRUPT_POOL:
3100 (void) printf(gettext("status: The pool metadata is corrupted "
3101 "and the pool cannot be opened.\n"));
3102 (void) printf(gettext("action: Destroy and re-create the pool "
3103 "from a backup source.\n"));
3106 case ZPOOL_STATUS_VERSION_OLDER:
3107 (void) printf(gettext("status: The pool is formatted using an "
3108 "older on-disk format. The pool can\n\tstill be used, but "
3109 "some features are unavailable.\n"));
3110 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3111 "upgrade'. Once this is done, the\n\tpool will no longer "
3112 "be accessible on older software versions.\n"));
3115 case ZPOOL_STATUS_VERSION_NEWER:
3116 (void) printf(gettext("status: The pool has been upgraded to a "
3117 "newer, incompatible on-disk version.\n\tThe pool cannot "
3118 "be accessed on this system.\n"));
3119 (void) printf(gettext("action: Access the pool from a system "
3120 "running more recent software, or\n\trestore the pool from "
3124 case ZPOOL_STATUS_FAULTED_DEV_R:
3125 (void) printf(gettext("status: One or more devices are "
3126 "faulted in response to persistent errors.\n\tSufficient "
3127 "replicas exist for the pool to continue functioning "
3128 "in a\n\tdegraded state.\n"));
3129 (void) printf(gettext("action: Replace the faulted device, "
3130 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3133 case ZPOOL_STATUS_FAULTED_DEV_NR:
3134 (void) printf(gettext("status: One or more devices are "
3135 "faulted in response to persistent errors. There are "
3136 "insufficient replicas for the pool to\n\tcontinue "
3138 (void) printf(gettext("action: Destroy and re-create the pool "
3139 "from a backup source. Manually marking the device\n"
3140 "\trepaired using 'zpool clear' may allow some data "
3141 "to be recovered.\n"));
3144 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3145 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3146 (void) printf(gettext("status: One or more devices are "
3147 "faulted in response to IO failures.\n"));
3148 (void) printf(gettext("action: Make sure the affected devices "
3149 "are connected, then run 'zpool clear'.\n"));
3152 case ZPOOL_STATUS_BAD_LOG:
3153 (void) printf(gettext("status: An intent log record "
3154 "could not be read.\n"
3155 "\tWaiting for adminstrator intervention to fix the "
3156 "faulted pool.\n"));
3157 (void) printf(gettext("action: Either restore the affected "
3158 "device(s) and run 'zpool online',\n"
3159 "\tor ignore the intent log records by running "
3160 "'zpool clear'.\n"));
3165 * The remaining errors can't actually be generated, yet.
3167 assert(reason == ZPOOL_STATUS_OK);
3171 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3174 if (config != NULL) {
3177 nvlist_t **spares, **l2cache;
3178 uint_t nspares, nl2cache;
3181 (void) printf(gettext(" scrub: "));
3182 print_scrub_status(nvroot);
3184 namewidth = max_width(zhp, nvroot, 0, 0);
3188 (void) printf(gettext("config:\n\n"));
3189 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3190 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3191 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3192 namewidth, 0, B_FALSE, B_FALSE);
3193 if (num_logs(nvroot) > 0)
3194 print_status_config(zhp, "logs", nvroot, namewidth, 0,
3197 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3198 &l2cache, &nl2cache) == 0)
3199 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3201 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3202 &spares, &nspares) == 0)
3203 print_spares(zhp, spares, nspares, namewidth);
3205 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3207 nvlist_t *nverrlist = NULL;
3210 * If the approximate error count is small, get a
3211 * precise count by fetching the entire log and
3212 * uniquifying the results.
3214 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3215 zpool_get_errlog(zhp, &nverrlist) == 0) {
3220 while ((elem = nvlist_next_nvpair(nverrlist,
3225 nvlist_free(nverrlist);
3227 (void) printf("\n");
3230 (void) printf(gettext("errors: No known data "
3232 else if (!cbp->cb_verbose)
3233 (void) printf(gettext("errors: %llu data "
3234 "errors, use '-v' for a list\n"),
3235 (u_longlong_t)nerr);
3237 print_error_log(zhp);
3240 (void) printf(gettext("config: The configuration cannot be "
3248 * zpool status [-vx] [pool] ...
3250 * -v Display complete error logs
3251 * -x Display only pools with potential problems
3253 * Describes the health status of all pools or some subset.
3256 zpool_do_status(int argc, char **argv)
3260 status_cbdata_t cb = { 0 };
3263 while ((c = getopt(argc, argv, "vx")) != -1) {
3266 cb.cb_verbose = B_TRUE;
3269 cb.cb_explain = B_TRUE;
3272 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3281 cb.cb_first = B_TRUE;
3284 cb.cb_allpools = B_TRUE;
3286 ret = for_each_pool(argc, argv, B_TRUE, NULL, status_callback, &cb);
3288 if (argc == 0 && cb.cb_count == 0)
3289 (void) printf(gettext("no pools available\n"));
3290 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3291 (void) printf(gettext("all pools are healthy\n"));
3296 typedef struct upgrade_cbdata {
3301 uint64_t cb_version;
3306 upgrade_cb(zpool_handle_t *zhp, void *arg)
3308 upgrade_cbdata_t *cbp = arg;
3313 config = zpool_get_config(zhp, NULL);
3314 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3317 if (!cbp->cb_newer && version < SPA_VERSION) {
3319 if (cbp->cb_first) {
3320 (void) printf(gettext("The following pools are "
3321 "out of date, and can be upgraded. After "
3322 "being\nupgraded, these pools will no "
3323 "longer be accessible by older software "
3325 (void) printf(gettext("VER POOL\n"));
3326 (void) printf(gettext("--- ------------\n"));
3327 cbp->cb_first = B_FALSE;
3330 (void) printf("%2llu %s\n", (u_longlong_t)version,
3331 zpool_get_name(zhp));
3333 cbp->cb_first = B_FALSE;
3334 ret = zpool_upgrade(zhp, cbp->cb_version);
3336 (void) printf(gettext("Successfully upgraded "
3337 "'%s'\n\n"), zpool_get_name(zhp));
3340 } else if (cbp->cb_newer && version > SPA_VERSION) {
3341 assert(!cbp->cb_all);
3343 if (cbp->cb_first) {
3344 (void) printf(gettext("The following pools are "
3345 "formatted using a newer software version and\n"
3346 "cannot be accessed on the current system.\n\n"));
3347 (void) printf(gettext("VER POOL\n"));
3348 (void) printf(gettext("--- ------------\n"));
3349 cbp->cb_first = B_FALSE;
3352 (void) printf("%2llu %s\n", (u_longlong_t)version,
3353 zpool_get_name(zhp));
3362 upgrade_one(zpool_handle_t *zhp, void *data)
3364 upgrade_cbdata_t *cbp = data;
3365 uint64_t cur_version;
3368 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3369 (void) printf(gettext("'log' is now a reserved word\n"
3370 "Pool 'log' must be renamed using export and import"
3375 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3376 if (cur_version > cbp->cb_version) {
3377 (void) printf(gettext("Pool '%s' is already formatted "
3378 "using more current version '%llu'.\n"),
3379 zpool_get_name(zhp), cur_version);
3382 if (cur_version == cbp->cb_version) {
3383 (void) printf(gettext("Pool '%s' is already formatted "
3384 "using the current version.\n"), zpool_get_name(zhp));
3388 ret = zpool_upgrade(zhp, cbp->cb_version);
3391 (void) printf(gettext("Successfully upgraded '%s' "
3392 "from version %llu to version %llu\n\n"),
3393 zpool_get_name(zhp), (u_longlong_t)cur_version,
3394 (u_longlong_t)cbp->cb_version);
3403 * zpool upgrade [-V version] <-a | pool ...>
3405 * With no arguments, display downrev'd ZFS pool available for upgrade.
3406 * Individual pools can be upgraded by specifying the pool, and '-a' will
3407 * upgrade all pools.
3410 zpool_do_upgrade(int argc, char **argv)
3413 upgrade_cbdata_t cb = { 0 };
3415 boolean_t showversions = B_FALSE;
3420 while ((c = getopt(argc, argv, "avV:")) != -1) {
3426 showversions = B_TRUE;
3429 cb.cb_version = strtoll(optarg, &end, 10);
3430 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3431 cb.cb_version < SPA_VERSION_1) {
3432 (void) fprintf(stderr,
3433 gettext("invalid version '%s'\n"), optarg);
3438 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3449 if (cb.cb_version == 0) {
3450 cb.cb_version = SPA_VERSION;
3451 } else if (!cb.cb_all && argc == 0) {
3452 (void) fprintf(stderr, gettext("-V option is "
3453 "incompatible with other arguments\n"));
3458 if (cb.cb_all || argc != 0) {
3459 (void) fprintf(stderr, gettext("-v option is "
3460 "incompatible with other arguments\n"));
3463 } else if (cb.cb_all) {
3465 (void) fprintf(stderr, gettext("-a option should not "
3466 "be used along with a pool name\n"));
3471 (void) printf(gettext("This system is currently running "
3472 "ZFS pool version %llu.\n\n"), SPA_VERSION);
3473 cb.cb_first = B_TRUE;
3475 (void) printf(gettext("The following versions are "
3477 (void) printf(gettext("VER DESCRIPTION\n"));
3478 (void) printf("--- -----------------------------------------"
3479 "---------------\n");
3480 (void) printf(gettext(" 1 Initial ZFS version\n"));
3481 (void) printf(gettext(" 2 Ditto blocks "
3482 "(replicated metadata)\n"));
3483 (void) printf(gettext(" 3 Hot spares and double parity "
3485 (void) printf(gettext(" 4 zpool history\n"));
3486 (void) printf(gettext(" 5 Compression using the gzip "
3488 (void) printf(gettext(" 6 bootfs pool property\n"));
3489 (void) printf(gettext(" 7 Separate intent log devices\n"));
3490 (void) printf(gettext(" 8 Delegated administration\n"));
3491 (void) printf(gettext(" 9 refquota and refreservation "
3493 (void) printf(gettext(" 10 Cache devices\n"));
3494 (void) printf(gettext(" 11 Improved scrub performance\n"));
3495 (void) printf(gettext(" 12 Snapshot properties\n"));
3496 (void) printf(gettext(" 13 snapused property\n"));
3497 (void) printf(gettext(" 14 passthrough-x aclinherit "
3499 (void) printf(gettext("For more information on a particular "
3500 "version, including supported releases, see:\n\n"));
3501 (void) printf("http://www.opensolaris.org/os/community/zfs/"
3503 (void) printf(gettext("Where 'N' is the version number.\n"));
3504 } else if (argc == 0) {
3507 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3508 notfound = cb.cb_first;
3510 if (!cb.cb_all && ret == 0) {
3512 (void) printf("\n");
3513 cb.cb_first = B_TRUE;
3514 cb.cb_newer = B_TRUE;
3515 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3518 (void) printf("\n");
3524 (void) printf(gettext("All pools are formatted "
3525 "using this version.\n"));
3526 else if (!cb.cb_all)
3527 (void) printf(gettext("Use 'zpool upgrade -v' "
3528 "for a list of available versions and "
3529 "their associated\nfeatures.\n"));
3532 ret = for_each_pool(argc, argv, B_FALSE, NULL,
3539 typedef struct hist_cbdata {
3545 char *hist_event_table[LOG_END] = {
3561 "pool property set",
3565 "destroy_begin_sync",
3569 "permission update",
3570 "permission remove",
3571 "permission who remove",
3580 "filesystem version upgrade",
3582 "refreservation set",
3587 * Print out the command history for a specific pool.
3590 get_history_one(zpool_handle_t *zhp, void *data)
3606 char internalstr[MAXPATHLEN];
3607 hist_cbdata_t *cb = (hist_cbdata_t *)data;
3611 cb->first = B_FALSE;
3613 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
3615 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
3618 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
3619 &records, &numrecords) == 0);
3620 for (i = 0; i < numrecords; i++) {
3621 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
3625 /* is it an internal event or a standard event? */
3626 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
3628 if (cb->internal == 0)
3631 if (nvlist_lookup_uint64(records[i],
3632 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
3634 verify(nvlist_lookup_uint64(records[i],
3635 ZPOOL_HIST_TXG, &txg) == 0);
3636 verify(nvlist_lookup_string(records[i],
3637 ZPOOL_HIST_INT_STR, &pathstr) == 0);
3638 if (ievent >= LOG_END)
3640 (void) snprintf(internalstr,
3641 sizeof (internalstr),
3642 "[internal %s txg:%lld] %s",
3643 hist_event_table[ievent], txg,
3645 cmdstr = internalstr;
3648 (void) localtime_r(&tsec, &t);
3649 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
3650 (void) printf("%s %s", tbuf, cmdstr);
3653 (void) printf("\n");
3656 (void) printf(" [");
3657 if (nvlist_lookup_uint64(records[i],
3658 ZPOOL_HIST_WHO, &who) == 0) {
3659 pwd = getpwuid((uid_t)who);
3661 (void) printf("user %s on",
3664 (void) printf("user %d on",
3667 (void) printf(gettext("no info]\n"));
3670 if (nvlist_lookup_string(records[i],
3671 ZPOOL_HIST_HOST, &hostname) == 0) {
3672 (void) printf(" %s", hostname);
3674 if (nvlist_lookup_string(records[i],
3675 ZPOOL_HIST_ZONE, &zonename) == 0) {
3676 (void) printf(":%s", zonename);
3680 (void) printf("\n");
3682 (void) printf("\n");
3689 * zpool history <pool>
3691 * Displays the history of commands that modified pools.
3696 zpool_do_history(int argc, char **argv)
3698 hist_cbdata_t cbdata = { 0 };
3702 cbdata.first = B_TRUE;
3704 while ((c = getopt(argc, argv, "li")) != -1) {
3710 cbdata.internal = 1;
3713 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3721 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
3724 if (argc == 0 && cbdata.first == B_TRUE) {
3725 (void) printf(gettext("no pools available\n"));
3733 get_callback(zpool_handle_t *zhp, void *data)
3735 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
3736 char value[MAXNAMELEN];
3737 zprop_source_t srctype;
3740 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
3743 * Skip the special fake placeholder. This will also skip
3744 * over the name property when 'all' is specified.
3746 if (pl->pl_prop == ZPOOL_PROP_NAME &&
3747 pl == cbp->cb_proplist)
3750 if (zpool_get_prop(zhp, pl->pl_prop,
3751 value, sizeof (value), &srctype) != 0)
3754 zprop_print_one_property(zpool_get_name(zhp), cbp,
3755 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL);
3761 zpool_do_get(int argc, char **argv)
3763 zprop_get_cbdata_t cb = { 0 };
3764 zprop_list_t fake_name = { 0 };
3770 cb.cb_first = B_TRUE;
3771 cb.cb_sources = ZPROP_SRC_ALL;
3772 cb.cb_columns[0] = GET_COL_NAME;
3773 cb.cb_columns[1] = GET_COL_PROPERTY;
3774 cb.cb_columns[2] = GET_COL_VALUE;
3775 cb.cb_columns[3] = GET_COL_SOURCE;
3776 cb.cb_type = ZFS_TYPE_POOL;
3778 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
3779 ZFS_TYPE_POOL) != 0)
3782 if (cb.cb_proplist != NULL) {
3783 fake_name.pl_prop = ZPOOL_PROP_NAME;
3784 fake_name.pl_width = strlen(gettext("NAME"));
3785 fake_name.pl_next = cb.cb_proplist;
3786 cb.cb_proplist = &fake_name;
3789 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
3792 if (cb.cb_proplist == &fake_name)
3793 zprop_free_list(fake_name.pl_next);
3795 zprop_free_list(cb.cb_proplist);
3800 typedef struct set_cbdata {
3803 boolean_t cb_any_successful;
3807 set_callback(zpool_handle_t *zhp, void *data)
3810 set_cbdata_t *cb = (set_cbdata_t *)data;
3812 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
3815 cb->cb_any_successful = B_TRUE;
3821 zpool_do_set(int argc, char **argv)
3823 set_cbdata_t cb = { 0 };
3826 if (argc > 1 && argv[1][0] == '-') {
3827 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3833 (void) fprintf(stderr, gettext("missing property=value "
3839 (void) fprintf(stderr, gettext("missing pool name\n"));
3844 (void) fprintf(stderr, gettext("too many pool names\n"));
3848 cb.cb_propname = argv[1];
3849 cb.cb_value = strchr(cb.cb_propname, '=');
3850 if (cb.cb_value == NULL) {
3851 (void) fprintf(stderr, gettext("missing value in "
3852 "property=value argument\n"));
3856 *(cb.cb_value) = '\0';
3859 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
3866 find_command_idx(char *command, int *idx)
3870 for (i = 0; i < NCOMMAND; i++) {
3871 if (command_table[i].name == NULL)
3874 if (strcmp(command, command_table[i].name) == 0) {
3883 main(int argc, char **argv)
3889 (void) setlocale(LC_ALL, "");
3890 (void) textdomain(TEXT_DOMAIN);
3892 if ((g_zfs = libzfs_init()) == NULL) {
3893 (void) fprintf(stderr, gettext("internal error: failed to "
3894 "initialize ZFS library\n"));
3898 libzfs_print_on_error(g_zfs, B_TRUE);
3903 * Make sure the user has specified some command.
3906 (void) fprintf(stderr, gettext("missing command\n"));
3915 if (strcmp(cmdname, "-?") == 0)
3918 zpool_set_history_str("zpool", argc, argv, history_str);
3919 verify(zpool_stage_history(g_zfs, history_str) == 0);
3922 * Run the appropriate command.
3924 if (find_command_idx(cmdname, &i) == 0) {
3925 current_command = &command_table[i];
3926 ret = command_table[i].func(argc - 1, argv + 1);
3927 } else if (strchr(cmdname, '=')) {
3928 verify(find_command_idx("set", &i) == 0);
3929 current_command = &command_table[i];
3930 ret = command_table[i].func(argc, argv);
3931 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
3933 * 'freeze' is a vile debugging abomination, so we treat
3937 int fd = open(ZFS_DEV, O_RDWR);
3938 (void) strcpy((void *)buf, argv[2]);
3939 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
3941 (void) fprintf(stderr, gettext("unrecognized "
3942 "command '%s'\n"), cmdname);
3949 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3950 * for the purposes of running ::findleaks.
3952 if (getenv("ZFS_ABORT") != NULL) {
3953 (void) printf("dumping core by request\n");