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.
27 #pragma ident "@(#)zpool_main.c 1.54 08/02/13 SMI"
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.
90 _umem_debug_init(void)
92 return ("default,verbose"); /* $UMEM_DEBUG setting */
96 _umem_logging_init(void)
98 return ("fail,contents"); /* $UMEM_LOGGING setting */
125 typedef struct zpool_command {
127 int (*func)(int, char **);
132 * Master command table. Each ZFS command has a name, associated function, and
133 * usage message. The usage messages need to be internationalized, so we have
134 * to have a function to return the usage message based on a command index.
136 * These commands are organized according to how they are displayed in the usage
137 * message. An empty command (one with a NULL name) indicates an empty line in
138 * the generic usage message.
140 static zpool_command_t command_table[] = {
141 { "create", zpool_do_create, HELP_CREATE },
142 { "destroy", zpool_do_destroy, HELP_DESTROY },
144 { "add", zpool_do_add, HELP_ADD },
145 { "remove", zpool_do_remove, HELP_REMOVE },
147 { "list", zpool_do_list, HELP_LIST },
148 { "iostat", zpool_do_iostat, HELP_IOSTAT },
149 { "status", zpool_do_status, HELP_STATUS },
151 { "online", zpool_do_online, HELP_ONLINE },
152 { "offline", zpool_do_offline, HELP_OFFLINE },
153 { "clear", zpool_do_clear, HELP_CLEAR },
155 { "attach", zpool_do_attach, HELP_ATTACH },
156 { "detach", zpool_do_detach, HELP_DETACH },
157 { "replace", zpool_do_replace, HELP_REPLACE },
159 { "scrub", zpool_do_scrub, HELP_SCRUB },
161 { "import", zpool_do_import, HELP_IMPORT },
162 { "export", zpool_do_export, HELP_EXPORT },
163 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
165 { "history", zpool_do_history, HELP_HISTORY },
166 { "get", zpool_do_get, HELP_GET },
167 { "set", zpool_do_set, HELP_SET },
170 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
172 zpool_command_t *current_command;
173 static char history_str[HIS_MAX_RECORD_LEN];
176 get_usage(zpool_help_t idx) {
179 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
181 return (gettext("\tattach [-f] <pool> <device> "
184 return (gettext("\tclear <pool> [device]\n"));
186 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
187 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
189 return (gettext("\tdestroy [-f] <pool>\n"));
191 return (gettext("\tdetach <pool> <device>\n"));
193 return (gettext("\texport [-f] <pool> ...\n"));
195 return (gettext("\thistory [-il] [<pool>] ...\n"));
197 return (gettext("\timport [-d dir] [-D]\n"
198 "\timport [-o mntopts] [-o property=value] ... \n"
199 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] -a\n"
200 "\timport [-o mntopts] [-o property=value] ... \n"
201 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] "
202 "<pool | id> [newpool]\n"));
204 return (gettext("\tiostat [-v] [pool] ... [interval "
207 return (gettext("\tlist [-H] [-o property[,...]] "
210 return (gettext("\toffline [-t] <pool> <device> ...\n"));
212 return (gettext("\tonline <pool> <device> ...\n"));
214 return (gettext("\treplace [-f] <pool> <device> "
217 return (gettext("\tremove <pool> <device> ...\n"));
219 return (gettext("\tscrub [-s] <pool> ...\n"));
221 return (gettext("\tstatus [-vx] [pool] ...\n"));
223 return (gettext("\tupgrade\n"
225 "\tupgrade [-V version] <-a | pool ...>\n"));
227 return (gettext("\tget <\"all\" | property[,...]> "
230 return (gettext("\tset <property=value> <pool> \n"));
239 * Callback routine that will print out a pool property value.
242 print_prop_cb(int prop, void *cb)
246 (void) fprintf(fp, "\t%-13s ", zpool_prop_to_name(prop));
248 if (zpool_prop_readonly(prop))
249 (void) fprintf(fp, " NO ");
251 (void) fprintf(fp, " YES ");
253 if (zpool_prop_values(prop) == NULL)
254 (void) fprintf(fp, "-\n");
256 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
262 * Display usage message. If we're inside a command, display only the usage for
263 * that command. Otherwise, iterate over the entire command table and display
264 * a complete usage message.
267 usage(boolean_t requested)
269 FILE *fp = requested ? stdout : stderr;
271 if (current_command == NULL) {
274 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
276 gettext("where 'command' is one of the following:\n\n"));
278 for (i = 0; i < NCOMMAND; i++) {
279 if (command_table[i].name == NULL)
280 (void) fprintf(fp, "\n");
282 (void) fprintf(fp, "%s",
283 get_usage(command_table[i].usage));
286 (void) fprintf(fp, gettext("usage:\n"));
287 (void) fprintf(fp, "%s", get_usage(current_command->usage));
290 if (current_command != NULL &&
291 ((strcmp(current_command->name, "set") == 0) ||
292 (strcmp(current_command->name, "get") == 0) ||
293 (strcmp(current_command->name, "list") == 0))) {
296 gettext("\nthe following properties are supported:\n"));
298 (void) fprintf(fp, "\n\t%-13s %s %s\n\n",
299 "PROPERTY", "EDIT", "VALUES");
301 /* Iterate over all properties */
302 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
307 * See comments at end of main().
309 if (getenv("ZFS_ABORT") != NULL) {
310 (void) printf("dumping core by request\n");
314 exit(requested ? 0 : 2);
318 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
319 boolean_t print_logs)
326 (void) printf("\t%*s%s\n", indent, "", name);
328 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
329 &child, &children) != 0)
332 for (c = 0; c < children; c++) {
333 uint64_t is_log = B_FALSE;
335 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
337 if ((is_log && !print_logs) || (!is_log && print_logs))
340 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
341 print_vdev_tree(zhp, vname, child[c], indent + 2,
348 * Add a property pair (name, string-value) into a property nvlist.
351 add_prop_list(const char *propname, char *propval, nvlist_t **props)
357 if (*props == NULL &&
358 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
359 (void) fprintf(stderr,
360 gettext("internal error: out of memory\n"));
366 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
367 (void) fprintf(stderr, gettext("property '%s' is "
368 "not a valid pool property\n"), propname);
372 /* Use normalized property name for nvlist operations */
373 if (nvlist_lookup_string(proplist, zpool_prop_to_name(prop),
374 &strval) == 0 && prop != ZPOOL_PROP_CACHEFILE) {
375 (void) fprintf(stderr, gettext("property '%s' "
376 "specified multiple times\n"), propname);
380 if (nvlist_add_string(proplist, zpool_prop_to_name(prop),
382 (void) fprintf(stderr, gettext("internal "
383 "error: out of memory\n"));
391 * zpool add [-fn] <pool> <vdev> ...
393 * -f Force addition of devices, even if they appear in use
394 * -n Do not add the devices, but display the resulting layout if
395 * they were to be added.
397 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
398 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
402 zpool_do_add(int argc, char **argv)
404 boolean_t force = B_FALSE;
405 boolean_t dryrun = B_FALSE;
414 while ((c = getopt(argc, argv, "fn")) != -1) {
423 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
432 /* get pool name and check number of arguments */
434 (void) fprintf(stderr, gettext("missing pool name argument\n"));
438 (void) fprintf(stderr, gettext("missing vdev specification\n"));
447 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
450 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
451 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
457 /* pass off to get_vdev_spec for processing */
458 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, argc, argv);
459 if (nvroot == NULL) {
465 nvlist_t *poolnvroot;
467 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
470 (void) printf(gettext("would update '%s' to the following "
471 "configuration:\n"), zpool_get_name(zhp));
473 /* print original main pool and new tree */
474 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
475 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
477 /* Do the same for the logs */
478 if (num_logs(poolnvroot) > 0) {
479 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
480 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
481 } else if (num_logs(nvroot) > 0) {
482 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
487 ret = (zpool_add(zhp, nvroot) != 0);
497 * zpool remove <pool> <vdev> ...
499 * Removes the given vdev from the pool. Currently, this only supports removing
500 * spares and cache devices from the pool. Eventually, we'll want to support
501 * removing leaf vdevs (as an alias for 'detach') as well as toplevel vdevs.
504 zpool_do_remove(int argc, char **argv)
513 /* get pool name and check number of arguments */
515 (void) fprintf(stderr, gettext("missing pool name argument\n"));
519 (void) fprintf(stderr, gettext("missing device\n"));
525 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
528 for (i = 1; i < argc; i++) {
529 if (zpool_vdev_remove(zhp, argv[i]) != 0)
537 * zpool create [-fn] [-o property=value] ... [-R root] [-m mountpoint]
540 * -f Force creation, even if devices appear in use
541 * -n Do not create the pool, but display the resulting layout if it
542 * were to be created.
543 * -R Create a pool under an alternate root
544 * -m Set default mountpoint for the root dataset. By default it's
546 * -o Set property=value.
548 * Creates the named pool according to the given vdev specification. The
549 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
550 * we get the nvlist back from get_vdev_spec(), we either print out the contents
551 * (if '-n' was specified), or pass it to libzfs to do the creation.
554 zpool_do_create(int argc, char **argv)
556 boolean_t force = B_FALSE;
557 boolean_t dryrun = B_FALSE;
559 nvlist_t *nvroot = NULL;
562 char *altroot = NULL;
563 char *mountpoint = NULL;
564 nvlist_t *props = NULL;
568 while ((c = getopt(argc, argv, ":fnR:m:o:")) != -1) {
578 if (add_prop_list(zpool_prop_to_name(
579 ZPOOL_PROP_ALTROOT), optarg, &props))
581 if (nvlist_lookup_string(props,
582 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
585 if (add_prop_list(zpool_prop_to_name(
586 ZPOOL_PROP_CACHEFILE), "none", &props))
593 if ((propval = strchr(optarg, '=')) == NULL) {
594 (void) fprintf(stderr, gettext("missing "
595 "'=' for -o option\n"));
601 if (add_prop_list(optarg, propval, &props))
605 (void) fprintf(stderr, gettext("missing argument for "
606 "'%c' option\n"), optopt);
609 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
618 /* get pool name and check number of arguments */
620 (void) fprintf(stderr, gettext("missing pool name argument\n"));
624 (void) fprintf(stderr, gettext("missing vdev specification\n"));
631 * As a special case, check for use of '/' in the name, and direct the
632 * user to use 'zfs create' instead.
634 if (strchr(poolname, '/') != NULL) {
635 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
636 "character '/' in pool name\n"), poolname);
637 (void) fprintf(stderr, gettext("use 'zfs create' to "
638 "create a dataset\n"));
642 /* pass off to get_vdev_spec for bulk processing */
643 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, argc - 1,
648 /* make_root_vdev() allows 0 toplevel children if there are spares */
649 if (!zfs_allocatable_devs(nvroot)) {
650 (void) fprintf(stderr, gettext("invalid vdev "
651 "specification: at least one toplevel vdev must be "
657 if (altroot != NULL && altroot[0] != '/') {
658 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
659 "must be an absolute path\n"), altroot);
664 * Check the validity of the mountpoint and direct the user to use the
665 * '-m' mountpoint option if it looks like its in use.
667 if (mountpoint == NULL ||
668 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
669 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
670 char buf[MAXPATHLEN];
673 if (mountpoint && mountpoint[0] != '/') {
674 (void) fprintf(stderr, gettext("invalid mountpoint "
675 "'%s': must be an absolute path, 'legacy', or "
676 "'none'\n"), mountpoint);
680 if (mountpoint == NULL) {
682 (void) snprintf(buf, sizeof (buf), "%s/%s",
685 (void) snprintf(buf, sizeof (buf), "/%s",
689 (void) snprintf(buf, sizeof (buf), "%s%s",
690 altroot, mountpoint);
692 (void) snprintf(buf, sizeof (buf), "%s",
696 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
697 (void) fprintf(stderr, gettext("mountpoint '%s' : "
698 "%s\n"), buf, strerror(errno));
699 (void) fprintf(stderr, gettext("use '-m' "
700 "option to provide a different default\n"));
705 while (count < 3 && readdir(dirp) != NULL)
707 (void) closedir(dirp);
710 (void) fprintf(stderr, gettext("mountpoint "
711 "'%s' exists and is not empty\n"), buf);
712 (void) fprintf(stderr, gettext("use '-m' "
713 "option to provide a "
714 "different default\n"));
722 * For a dry run invocation, print out a basic message and run
723 * through all the vdevs in the list and print out in an
724 * appropriate hierarchy.
726 (void) printf(gettext("would create '%s' with the "
727 "following layout:\n\n"), poolname);
729 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
730 if (num_logs(nvroot) > 0)
731 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
736 * Hand off to libzfs.
738 if (zpool_create(g_zfs, poolname, nvroot, props) == 0) {
739 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
740 ZFS_TYPE_FILESYSTEM);
742 if (mountpoint != NULL)
743 verify(zfs_prop_set(pool,
745 ZFS_PROP_MOUNTPOINT),
747 if (zfs_mount(pool, NULL, 0) == 0)
748 ret = zfs_shareall(pool);
751 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
752 (void) fprintf(stderr, gettext("pool name may have "
768 * zpool destroy <pool>
770 * -f Forcefully unmount any datasets
772 * Destroy the given pool. Automatically unmounts any datasets in the pool.
775 zpool_do_destroy(int argc, char **argv)
777 boolean_t force = B_FALSE;
784 while ((c = getopt(argc, argv, "f")) != -1) {
790 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
799 /* check arguments */
801 (void) fprintf(stderr, gettext("missing pool argument\n"));
805 (void) fprintf(stderr, gettext("too many arguments\n"));
811 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
813 * As a special case, check for use of '/' in the name, and
814 * direct the user to use 'zfs destroy' instead.
816 if (strchr(pool, '/') != NULL)
817 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
818 "destroy a dataset\n"));
822 if (zpool_disable_datasets(zhp, force) != 0) {
823 (void) fprintf(stderr, gettext("could not destroy '%s': "
824 "could not unmount datasets\n"), zpool_get_name(zhp));
828 ret = (zpool_destroy(zhp) != 0);
836 * zpool export [-f] <pool> ...
838 * -f Forcefully unmount datasets
840 * Export the given pools. By default, the command will attempt to cleanly
841 * unmount any active datasets within the pool. If the '-f' flag is specified,
842 * then the datasets will be forcefully unmounted.
845 zpool_do_export(int argc, char **argv)
847 boolean_t force = B_FALSE;
854 while ((c = getopt(argc, argv, "f")) != -1) {
860 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
869 /* check arguments */
871 (void) fprintf(stderr, gettext("missing pool argument\n"));
876 for (i = 0; i < argc; i++) {
877 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
882 if (zpool_disable_datasets(zhp, force) != 0) {
888 if (zpool_export(zhp) != 0)
898 * Given a vdev configuration, determine the maximum width needed for the device
902 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
904 char *name = zpool_vdev_name(g_zfs, zhp, nv);
909 if (strlen(name) + depth > max)
910 max = strlen(name) + depth;
914 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
915 &child, &children) == 0) {
916 for (c = 0; c < children; c++)
917 if ((ret = max_width(zhp, child[c], depth + 2,
922 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
923 &child, &children) == 0) {
924 for (c = 0; c < children; c++)
925 if ((ret = max_width(zhp, child[c], depth + 2,
930 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
931 &child, &children) == 0) {
932 for (c = 0; c < children; c++)
933 if ((ret = max_width(zhp, child[c], depth + 2,
944 * Print the configuration of an exported pool. Iterate over all vdevs in the
945 * pool, printing out the name and status for each one.
948 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth,
949 boolean_t print_logs)
956 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
957 if (strcmp(type, VDEV_TYPE_MISSING) == 0)
960 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
961 (uint64_t **)&vs, &c) == 0);
963 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
964 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
966 if (vs->vs_aux != 0) {
969 switch (vs->vs_aux) {
970 case VDEV_AUX_OPEN_FAILED:
971 (void) printf(gettext("cannot open"));
974 case VDEV_AUX_BAD_GUID_SUM:
975 (void) printf(gettext("missing device"));
978 case VDEV_AUX_NO_REPLICAS:
979 (void) printf(gettext("insufficient replicas"));
982 case VDEV_AUX_VERSION_NEWER:
983 (void) printf(gettext("newer version"));
986 case VDEV_AUX_ERR_EXCEEDED:
987 (void) printf(gettext("too many errors"));
991 (void) printf(gettext("corrupted data"));
997 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
998 &child, &children) != 0)
1001 for (c = 0; c < children; c++) {
1002 uint64_t is_log = B_FALSE;
1004 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1006 if ((is_log && !print_logs) || (!is_log && print_logs))
1009 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1010 print_import_config(vname, child[c],
1011 namewidth, depth + 2, B_FALSE);
1015 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1016 &child, &children) == 0) {
1017 (void) printf(gettext("\tcache\n"));
1018 for (c = 0; c < children; c++) {
1019 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1020 (void) printf("\t %s\n", vname);
1025 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1026 &child, &children) == 0) {
1027 (void) printf(gettext("\tspares\n"));
1028 for (c = 0; c < children; c++) {
1029 vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1030 (void) printf("\t %s\n", vname);
1037 * Display the status for the given pool.
1040 show_import(nvlist_t *config)
1042 uint64_t pool_state;
1053 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1055 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1057 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1059 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1062 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
1063 (uint64_t **)&vs, &vsc) == 0);
1064 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1066 reason = zpool_import_status(config, &msgid);
1068 (void) printf(gettext(" pool: %s\n"), name);
1069 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1070 (void) printf(gettext(" state: %s"), health);
1071 if (pool_state == POOL_STATE_DESTROYED)
1072 (void) printf(gettext(" (DESTROYED)"));
1073 (void) printf("\n");
1076 case ZPOOL_STATUS_MISSING_DEV_R:
1077 case ZPOOL_STATUS_MISSING_DEV_NR:
1078 case ZPOOL_STATUS_BAD_GUID_SUM:
1079 (void) printf(gettext("status: One or more devices are missing "
1080 "from the system.\n"));
1083 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1084 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1085 (void) printf(gettext("status: One or more devices contains "
1086 "corrupted data.\n"));
1089 case ZPOOL_STATUS_CORRUPT_DATA:
1090 (void) printf(gettext("status: The pool data is corrupted.\n"));
1093 case ZPOOL_STATUS_OFFLINE_DEV:
1094 (void) printf(gettext("status: One or more devices "
1095 "are offlined.\n"));
1098 case ZPOOL_STATUS_CORRUPT_POOL:
1099 (void) printf(gettext("status: The pool metadata is "
1103 case ZPOOL_STATUS_VERSION_OLDER:
1104 (void) printf(gettext("status: The pool is formatted using an "
1105 "older on-disk version.\n"));
1108 case ZPOOL_STATUS_VERSION_NEWER:
1109 (void) printf(gettext("status: The pool is formatted using an "
1110 "incompatible version.\n"));
1112 case ZPOOL_STATUS_HOSTID_MISMATCH:
1113 (void) printf(gettext("status: The pool was last accessed by "
1114 "another system.\n"));
1116 case ZPOOL_STATUS_FAULTED_DEV_R:
1117 case ZPOOL_STATUS_FAULTED_DEV_NR:
1118 (void) printf(gettext("status: One or more devices are "
1124 * No other status can be seen when importing pools.
1126 assert(reason == ZPOOL_STATUS_OK);
1130 * Print out an action according to the overall state of the pool.
1132 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1133 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1134 (void) printf(gettext("action: The pool can be "
1135 "imported using its name or numeric identifier, "
1136 "though\n\tsome features will not be available "
1137 "without an explicit 'zpool upgrade'.\n"));
1138 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1139 (void) printf(gettext("action: The pool can be "
1140 "imported using its name or numeric "
1141 "identifier and\n\tthe '-f' flag.\n"));
1143 (void) printf(gettext("action: The pool can be "
1144 "imported using its name or numeric "
1146 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1147 (void) printf(gettext("action: The pool can be imported "
1148 "despite missing or damaged devices. The\n\tfault "
1149 "tolerance of the pool may be compromised if imported.\n"));
1152 case ZPOOL_STATUS_VERSION_NEWER:
1153 (void) printf(gettext("action: The pool cannot be "
1154 "imported. Access the pool on a system running "
1155 "newer\n\tsoftware, or recreate the pool from "
1158 case ZPOOL_STATUS_MISSING_DEV_R:
1159 case ZPOOL_STATUS_MISSING_DEV_NR:
1160 case ZPOOL_STATUS_BAD_GUID_SUM:
1161 (void) printf(gettext("action: The pool cannot be "
1162 "imported. Attach the missing\n\tdevices and try "
1166 (void) printf(gettext("action: The pool cannot be "
1167 "imported due to damaged devices or data.\n"));
1172 * If the state is "closed" or "can't open", and the aux state
1173 * is "corrupt data":
1175 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1176 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1177 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1178 if (pool_state == POOL_STATE_DESTROYED)
1179 (void) printf(gettext("\tThe pool was destroyed, "
1180 "but can be imported using the '-Df' flags.\n"));
1181 else if (pool_state != POOL_STATE_EXPORTED)
1182 (void) printf(gettext("\tThe pool may be active on "
1183 "another system, but can be imported using\n\t"
1184 "the '-f' flag.\n"));
1188 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1191 (void) printf(gettext("config:\n\n"));
1193 namewidth = max_width(NULL, nvroot, 0, 0);
1197 print_import_config(name, nvroot, namewidth, 0, B_FALSE);
1198 if (num_logs(nvroot) > 0) {
1199 (void) printf(gettext("\tlogs\n"));
1200 print_import_config(name, nvroot, namewidth, 0, B_TRUE);
1203 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1204 (void) printf(gettext("\n\tAdditional devices are known to "
1205 "be part of this pool, though their\n\texact "
1206 "configuration cannot be determined.\n"));
1211 * Perform the import for the given configuration. This passes the heavy
1212 * lifting off to zpool_import_props(), and then mounts the datasets contained
1216 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1217 int force, nvlist_t *props)
1219 zpool_handle_t *zhp;
1225 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1228 verify(nvlist_lookup_uint64(config,
1229 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1230 verify(nvlist_lookup_uint64(config,
1231 ZPOOL_CONFIG_VERSION, &version) == 0);
1232 if (version > SPA_VERSION) {
1233 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1234 "is formatted using a newer ZFS version\n"), name);
1236 } else if (state != POOL_STATE_EXPORTED && !force) {
1239 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1241 if ((unsigned long)hostid != gethostid()) {
1246 verify(nvlist_lookup_string(config,
1247 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1248 verify(nvlist_lookup_uint64(config,
1249 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1251 (void) fprintf(stderr, gettext("cannot import "
1252 "'%s': pool may be in use from other "
1253 "system, it was last accessed by %s "
1254 "(hostid: 0x%lx) on %s"), name, hostname,
1255 (unsigned long)hostid,
1256 asctime(localtime(&t)));
1257 (void) fprintf(stderr, gettext("use '-f' to "
1258 "import anyway\n"));
1262 (void) fprintf(stderr, gettext("cannot import '%s': "
1263 "pool may be in use from other system\n"), name);
1264 (void) fprintf(stderr, gettext("use '-f' to import "
1270 if (zpool_import_props(g_zfs, config, newname, props) != 0)
1273 if (newname != NULL)
1274 name = (char *)newname;
1276 verify((zhp = zpool_open(g_zfs, name)) != NULL);
1278 if (zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1288 * zpool import [-d dir] [-D]
1289 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1290 * [-d dir | -c cachefile] [-f] -a
1291 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1292 * [-d dir | -c cachefile] [-f] <pool | id> [newpool]
1294 * -c Read pool information from a cachefile instead of searching
1297 * -d Scan in a specific directory, other than /dev/dsk. More than
1298 * one directory can be specified using multiple '-d' options.
1300 * -D Scan for previously destroyed pools or import all or only
1301 * specified destroyed pools.
1303 * -R Temporarily import the pool, with all mountpoints relative to
1304 * the given root. The pool will remain exported when the machine
1307 * -f Force import, even if it appears that the pool is active.
1309 * -a Import all pools found.
1311 * -o Set property=value and/or temporary mount options (without '=').
1313 * The import command scans for pools to import, and import pools based on pool
1314 * name and GUID. The pool can also be renamed as part of the import process.
1317 zpool_do_import(int argc, char **argv)
1319 char **searchdirs = NULL;
1323 nvlist_t *pools = NULL;
1324 boolean_t do_all = B_FALSE;
1325 boolean_t do_destroyed = B_FALSE;
1326 char *mntopts = NULL;
1327 boolean_t do_force = B_FALSE;
1330 uint64_t searchguid;
1333 nvlist_t *found_config;
1334 nvlist_t *props = NULL;
1336 uint64_t pool_state;
1337 char *cachefile = NULL;
1340 while ((c = getopt(argc, argv, ":afc:d:Do:p:R:")) != -1) {
1349 if (searchdirs == NULL) {
1350 searchdirs = safe_malloc(sizeof (char *));
1352 char **tmp = safe_malloc((nsearch + 1) *
1354 bcopy(searchdirs, tmp, nsearch *
1359 searchdirs[nsearch++] = optarg;
1362 do_destroyed = B_TRUE;
1368 if ((propval = strchr(optarg, '=')) != NULL) {
1371 if (add_prop_list(optarg, propval, &props))
1378 if (add_prop_list(zpool_prop_to_name(
1379 ZPOOL_PROP_ALTROOT), optarg, &props))
1381 if (nvlist_lookup_string(props,
1382 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1385 if (add_prop_list(zpool_prop_to_name(
1386 ZPOOL_PROP_CACHEFILE), "none", &props))
1390 (void) fprintf(stderr, gettext("missing argument for "
1391 "'%c' option\n"), optopt);
1395 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1404 if (cachefile && nsearch != 0) {
1405 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1409 if (searchdirs == NULL) {
1410 searchdirs = safe_malloc(sizeof (char *));
1411 searchdirs[0] = "/dev/dsk";
1415 /* check argument count */
1418 (void) fprintf(stderr, gettext("too many arguments\n"));
1423 (void) fprintf(stderr, gettext("too many arguments\n"));
1428 * Check for the SYS_CONFIG privilege. We do this explicitly
1429 * here because otherwise any attempt to discover pools will
1432 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1433 (void) fprintf(stderr, gettext("cannot "
1434 "discover pools: permission denied\n"));
1441 pools = zpool_find_import_cached(g_zfs, cachefile, B_FALSE);
1443 pools = zpool_find_import(g_zfs, nsearch, searchdirs, B_FALSE);
1445 if (pools == NULL) {
1451 * We now have a list of all available pools in the given directories.
1452 * Depending on the arguments given, we do one of the following:
1454 * <none> Iterate through all pools and display information about
1457 * -a Iterate through all pools and try to import each one.
1459 * <id> Find the pool that corresponds to the given GUID/pool
1460 * name and import that one.
1462 * -D Above options applies only to destroyed pools.
1468 searchguid = strtoull(argv[0], &endptr, 10);
1469 if (errno != 0 || *endptr != '\0')
1470 searchname = argv[0];
1473 found_config = NULL;
1479 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1481 verify(nvpair_value_nvlist(elem, &config) == 0);
1483 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1485 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1487 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1494 (void) printf("\n");
1497 err |= do_import(config, NULL, mntopts,
1500 show_import(config);
1501 } else if (searchname != NULL) {
1505 * We are searching for a pool based on name.
1507 verify(nvlist_lookup_string(config,
1508 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1510 if (strcmp(name, searchname) == 0) {
1511 if (found_config != NULL) {
1512 (void) fprintf(stderr, gettext(
1513 "cannot import '%s': more than "
1514 "one matching pool\n"), searchname);
1515 (void) fprintf(stderr, gettext(
1516 "import by numeric ID instead\n"));
1519 found_config = config;
1525 * Search for a pool by guid.
1527 verify(nvlist_lookup_uint64(config,
1528 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1530 if (guid == searchguid)
1531 found_config = config;
1536 * If we were searching for a specific pool, verify that we found a
1537 * pool, and then do the import.
1539 if (argc != 0 && err == 0) {
1540 if (found_config == NULL) {
1541 (void) fprintf(stderr, gettext("cannot import '%s': "
1542 "no such pool available\n"), argv[0]);
1545 err |= do_import(found_config, argc == 1 ? NULL :
1546 argv[1], mntopts, do_force, props);
1551 * If we were just looking for pools, report an error if none were
1554 if (argc == 0 && first)
1555 (void) fprintf(stderr,
1556 gettext("no pools available to import\n"));
1563 return (err ? 1 : 0);
1566 typedef struct iostat_cbdata {
1567 zpool_list_t *cb_list;
1574 print_iostat_separator(iostat_cbdata_t *cb)
1578 for (i = 0; i < cb->cb_namewidth; i++)
1580 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1584 print_iostat_header(iostat_cbdata_t *cb)
1586 (void) printf("%*s capacity operations bandwidth\n",
1587 cb->cb_namewidth, "");
1588 (void) printf("%-*s used avail read write read write\n",
1589 cb->cb_namewidth, "pool");
1590 print_iostat_separator(cb);
1594 * Display a single statistic.
1597 print_one_stat(uint64_t value)
1601 zfs_nicenum(value, buf, sizeof (buf));
1602 (void) printf(" %5s", buf);
1606 * Print out all the statistics for the given vdev. This can either be the
1607 * toplevel configuration, or called recursively. If 'name' is NULL, then this
1608 * is a verbose output, and we don't want to display the toplevel pool stats.
1611 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1612 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1614 nvlist_t **oldchild, **newchild;
1616 vdev_stat_t *oldvs, *newvs;
1617 vdev_stat_t zerovs = { 0 };
1622 if (oldnv != NULL) {
1623 verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
1624 (uint64_t **)&oldvs, &c) == 0);
1629 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
1630 (uint64_t **)&newvs, &c) == 0);
1632 if (strlen(name) + depth > cb->cb_namewidth)
1633 (void) printf("%*s%s", depth, "", name);
1635 (void) printf("%*s%s%*s", depth, "", name,
1636 (int)(cb->cb_namewidth - strlen(name) - depth), "");
1638 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
1643 scale = (double)NANOSEC / tdelta;
1645 /* only toplevel vdevs have capacity stats */
1646 if (newvs->vs_space == 0) {
1647 (void) printf(" - -");
1649 print_one_stat(newvs->vs_alloc);
1650 print_one_stat(newvs->vs_space - newvs->vs_alloc);
1653 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
1654 oldvs->vs_ops[ZIO_TYPE_READ])));
1656 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
1657 oldvs->vs_ops[ZIO_TYPE_WRITE])));
1659 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
1660 oldvs->vs_bytes[ZIO_TYPE_READ])));
1662 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
1663 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
1665 (void) printf("\n");
1667 if (!cb->cb_verbose)
1670 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
1671 &newchild, &children) != 0)
1674 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
1675 &oldchild, &c) != 0)
1678 for (c = 0; c < children; c++) {
1679 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1680 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1681 newchild[c], cb, depth + 2);
1686 * Include level 2 ARC devices in iostat output
1688 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
1689 &newchild, &children) != 0)
1692 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
1693 &oldchild, &c) != 0)
1697 (void) printf("%-*s - - - - - "
1698 "-\n", cb->cb_namewidth, "cache");
1699 for (c = 0; c < children; c++) {
1700 vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1701 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1702 newchild[c], cb, depth + 2);
1709 refresh_iostat(zpool_handle_t *zhp, void *data)
1711 iostat_cbdata_t *cb = data;
1715 * If the pool has disappeared, remove it from the list and continue.
1717 if (zpool_refresh_stats(zhp, &missing) != 0)
1721 pool_list_remove(cb->cb_list, zhp);
1727 * Callback to print out the iostats for the given pool.
1730 print_iostat(zpool_handle_t *zhp, void *data)
1732 iostat_cbdata_t *cb = data;
1733 nvlist_t *oldconfig, *newconfig;
1734 nvlist_t *oldnvroot, *newnvroot;
1736 newconfig = zpool_get_config(zhp, &oldconfig);
1738 if (cb->cb_iteration == 1)
1741 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
1744 if (oldconfig == NULL)
1747 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
1751 * Print out the statistics for the pool.
1753 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
1756 print_iostat_separator(cb);
1762 get_namewidth(zpool_handle_t *zhp, void *data)
1764 iostat_cbdata_t *cb = data;
1765 nvlist_t *config, *nvroot;
1767 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
1768 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1770 if (!cb->cb_verbose)
1771 cb->cb_namewidth = strlen(zpool_get_name(zhp));
1773 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
1777 * The width must fall into the range [10,38]. The upper limit is the
1778 * maximum we can have and still fit in 80 columns.
1780 if (cb->cb_namewidth < 10)
1781 cb->cb_namewidth = 10;
1782 if (cb->cb_namewidth > 38)
1783 cb->cb_namewidth = 38;
1789 * zpool iostat [-v] [pool] ... [interval [count]]
1791 * -v Display statistics for individual vdevs
1793 * This command can be tricky because we want to be able to deal with pool
1794 * creation/destruction as well as vdev configuration changes. The bulk of this
1795 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
1796 * on pool_list_update() to detect the addition of new pools. Configuration
1797 * changes are all handled within libzfs.
1800 zpool_do_iostat(int argc, char **argv)
1805 unsigned long interval = 0, count = 0;
1807 boolean_t verbose = B_FALSE;
1811 while ((c = getopt(argc, argv, "v")) != -1) {
1817 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1827 * Determine if the last argument is an integer or a pool name
1829 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1833 interval = strtoul(argv[argc - 1], &end, 10);
1835 if (*end == '\0' && errno == 0) {
1836 if (interval == 0) {
1837 (void) fprintf(stderr, gettext("interval "
1838 "cannot be zero\n"));
1843 * Ignore the last parameter
1848 * If this is not a valid number, just plow on. The
1849 * user will get a more informative error message later
1857 * If the last argument is also an integer, then we have both a count
1860 if (argc > 0 && isdigit(argv[argc - 1][0])) {
1865 interval = strtoul(argv[argc - 1], &end, 10);
1867 if (*end == '\0' && errno == 0) {
1868 if (interval == 0) {
1869 (void) fprintf(stderr, gettext("interval "
1870 "cannot be zero\n"));
1875 * Ignore the last parameter
1884 * Construct the list of all interesting pools.
1887 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
1890 if (pool_list_count(list) == 0 && argc != 0) {
1891 pool_list_free(list);
1895 if (pool_list_count(list) == 0 && interval == 0) {
1896 pool_list_free(list);
1897 (void) fprintf(stderr, gettext("no pools available\n"));
1902 * Enter the main iostat loop.
1905 cb.cb_verbose = verbose;
1906 cb.cb_iteration = 0;
1907 cb.cb_namewidth = 0;
1910 pool_list_update(list);
1912 if ((npools = pool_list_count(list)) == 0)
1916 * Refresh all statistics. This is done as an explicit step
1917 * before calculating the maximum name width, so that any
1918 * configuration changes are properly accounted for.
1920 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
1923 * Iterate over all pools to determine the maximum width
1924 * for the pool / device name column across all pools.
1926 cb.cb_namewidth = 0;
1927 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
1930 * If it's the first time, or verbose mode, print the header.
1932 if (++cb.cb_iteration == 1 || verbose)
1933 print_iostat_header(&cb);
1935 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
1938 * If there's more than one pool, and we're not in verbose mode
1939 * (which prints a separator for us), then print a separator.
1941 if (npools > 1 && !verbose)
1942 print_iostat_separator(&cb);
1945 (void) printf("\n");
1948 * Flush the output so that redirection to a file isn't buffered
1951 (void) fflush(stdout);
1956 if (count != 0 && --count == 0)
1959 (void) sleep(interval);
1962 pool_list_free(list);
1967 typedef struct list_cbdata {
1968 boolean_t cb_scripted;
1970 zprop_list_t *cb_proplist;
1974 * Given a list of columns to display, output appropriate headers for each one.
1977 print_header(zprop_list_t *pl)
1980 boolean_t first = B_TRUE;
1981 boolean_t right_justify;
1983 for (; pl != NULL; pl = pl->pl_next) {
1984 if (pl->pl_prop == ZPROP_INVAL)
1992 header = zpool_prop_column_name(pl->pl_prop);
1993 right_justify = zpool_prop_align_right(pl->pl_prop);
1995 if (pl->pl_next == NULL && !right_justify)
1996 (void) printf("%s", header);
1997 else if (right_justify)
1998 (void) printf("%*s", pl->pl_width, header);
2000 (void) printf("%-*s", pl->pl_width, header);
2003 (void) printf("\n");
2007 * Given a pool and a list of properties, print out all the properties according
2008 * to the described layout.
2011 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2013 boolean_t first = B_TRUE;
2014 char property[ZPOOL_MAXPROPLEN];
2016 boolean_t right_justify;
2019 for (; pl != NULL; pl = pl->pl_next) {
2022 (void) printf("\t");
2029 right_justify = B_FALSE;
2030 if (pl->pl_prop != ZPROP_INVAL) {
2031 if (zpool_get_prop(zhp, pl->pl_prop, property,
2032 sizeof (property), NULL) != 0)
2037 right_justify = zpool_prop_align_right(pl->pl_prop);
2042 width = pl->pl_width;
2045 * If this is being called in scripted mode, or if this is the
2046 * last column and it is left-justified, don't include a width
2049 if (scripted || (pl->pl_next == NULL && !right_justify))
2050 (void) printf("%s", propstr);
2051 else if (right_justify)
2052 (void) printf("%*s", width, propstr);
2054 (void) printf("%-*s", width, propstr);
2057 (void) printf("\n");
2061 * Generic callback function to list a pool.
2064 list_callback(zpool_handle_t *zhp, void *data)
2066 list_cbdata_t *cbp = data;
2068 if (cbp->cb_first) {
2069 if (!cbp->cb_scripted)
2070 print_header(cbp->cb_proplist);
2071 cbp->cb_first = B_FALSE;
2074 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2080 * zpool list [-H] [-o prop[,prop]*] [pool] ...
2082 * -H Scripted mode. Don't display headers, and separate properties
2084 * -o List of properties to display. Defaults to
2085 * "name,size,used,available,capacity,health,altroot"
2087 * List all pools in the system, whether or not they're healthy. Output space
2088 * statistics for each one, as well as health status summary.
2091 zpool_do_list(int argc, char **argv)
2095 list_cbdata_t cb = { 0 };
2096 static char default_props[] =
2097 "name,size,used,available,capacity,health,altroot";
2098 char *props = default_props;
2101 while ((c = getopt(argc, argv, ":Ho:")) != -1) {
2104 cb.cb_scripted = B_TRUE;
2110 (void) fprintf(stderr, gettext("missing argument for "
2111 "'%c' option\n"), optopt);
2115 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2124 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2127 cb.cb_first = B_TRUE;
2129 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2130 list_callback, &cb);
2132 zprop_free_list(cb.cb_proplist);
2134 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2135 (void) printf(gettext("no pools available\n"));
2143 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2150 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2151 &child, &children) != 0) {
2152 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2153 if (strncmp(name, "/dev/dsk/", 9) == 0)
2155 if (strncmp(path, "/dev/dsk/", 9) == 0)
2157 if (strcmp(name, path) == 0)
2162 for (c = 0; c < children; c++)
2163 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2170 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2172 boolean_t force = B_FALSE;
2175 char *poolname, *old_disk, *new_disk;
2176 zpool_handle_t *zhp;
2180 while ((c = getopt(argc, argv, "f")) != -1) {
2186 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2195 /* get pool name and check number of arguments */
2197 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2204 (void) fprintf(stderr,
2205 gettext("missing <device> specification\n"));
2213 (void) fprintf(stderr,
2214 gettext("missing <new_device> specification\n"));
2217 new_disk = old_disk;
2227 (void) fprintf(stderr, gettext("too many arguments\n"));
2231 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2234 if (zpool_get_config(zhp, NULL) == NULL) {
2235 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2241 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, argc, argv);
2242 if (nvroot == NULL) {
2247 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2249 nvlist_free(nvroot);
2256 * zpool replace [-f] <pool> <device> <new_device>
2258 * -f Force attach, even if <new_device> appears to be in use.
2260 * Replace <device> with <new_device>.
2264 zpool_do_replace(int argc, char **argv)
2266 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2270 * zpool attach [-f] <pool> <device> <new_device>
2272 * -f Force attach, even if <new_device> appears to be in use.
2274 * Attach <new_device> to the mirror containing <device>. If <device> is not
2275 * part of a mirror, then <device> will be transformed into a mirror of
2276 * <device> and <new_device>. In either case, <new_device> will begin life
2277 * with a DTL of [0, now], and will immediately begin to resilver itself.
2280 zpool_do_attach(int argc, char **argv)
2282 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2286 * zpool detach [-f] <pool> <device>
2288 * -f Force detach of <device>, even if DTLs argue against it
2289 * (not supported yet)
2291 * Detach a device from a mirror. The operation will be refused if <device>
2292 * is the last device in the mirror, or if the DTLs indicate that this device
2293 * has the only valid copy of some data.
2297 zpool_do_detach(int argc, char **argv)
2300 char *poolname, *path;
2301 zpool_handle_t *zhp;
2305 while ((c = getopt(argc, argv, "f")) != -1) {
2309 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2318 /* get pool name and check number of arguments */
2320 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2325 (void) fprintf(stderr,
2326 gettext("missing <device> specification\n"));
2333 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2336 ret = zpool_vdev_detach(zhp, path);
2344 * zpool online <pool> <device> ...
2347 zpool_do_online(int argc, char **argv)
2351 zpool_handle_t *zhp;
2353 vdev_state_t newstate;
2356 while ((c = getopt(argc, argv, "t")) != -1) {
2360 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2369 /* get pool name and check number of arguments */
2371 (void) fprintf(stderr, gettext("missing pool name\n"));
2375 (void) fprintf(stderr, gettext("missing device name\n"));
2381 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2384 for (i = 1; i < argc; i++) {
2385 if (zpool_vdev_online(zhp, argv[i], 0, &newstate) == 0) {
2386 if (newstate != VDEV_STATE_HEALTHY) {
2387 (void) printf(gettext("warning: device '%s' "
2388 "onlined, but remains in faulted state\n"),
2390 if (newstate == VDEV_STATE_FAULTED)
2391 (void) printf(gettext("use 'zpool "
2392 "clear' to restore a faulted "
2395 (void) printf(gettext("use 'zpool "
2396 "replace' to replace devices "
2397 "that are no longer present\n"));
2410 * zpool offline [-ft] <pool> <device> ...
2412 * -f Force the device into the offline state, even if doing
2413 * so would appear to compromise pool availability.
2414 * (not supported yet)
2416 * -t Only take the device off-line temporarily. The offline
2417 * state will not be persistent across reboots.
2421 zpool_do_offline(int argc, char **argv)
2425 zpool_handle_t *zhp;
2427 boolean_t istmp = B_FALSE;
2430 while ((c = getopt(argc, argv, "ft")) != -1) {
2437 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2446 /* get pool name and check number of arguments */
2448 (void) fprintf(stderr, gettext("missing pool name\n"));
2452 (void) fprintf(stderr, gettext("missing device name\n"));
2458 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2461 for (i = 1; i < argc; i++) {
2462 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
2472 * zpool clear <pool> [device]
2474 * Clear all errors associated with a pool or a particular device.
2477 zpool_do_clear(int argc, char **argv)
2480 zpool_handle_t *zhp;
2481 char *pool, *device;
2484 (void) fprintf(stderr, gettext("missing pool name\n"));
2489 (void) fprintf(stderr, gettext("too many arguments\n"));
2494 device = argc == 3 ? argv[2] : NULL;
2496 if ((zhp = zpool_open(g_zfs, pool)) == NULL)
2499 if (zpool_clear(zhp, device) != 0)
2507 typedef struct scrub_cbdata {
2514 scrub_callback(zpool_handle_t *zhp, void *data)
2516 scrub_cbdata_t *cb = data;
2520 * Ignore faulted pools.
2522 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
2523 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
2524 "currently unavailable\n"), zpool_get_name(zhp));
2528 err = zpool_scrub(zhp, cb->cb_type);
2534 * zpool scrub [-s] <pool> ...
2536 * -s Stop. Stops any in-progress scrub.
2539 zpool_do_scrub(int argc, char **argv)
2544 cb.cb_type = POOL_SCRUB_EVERYTHING;
2547 while ((c = getopt(argc, argv, "s")) != -1) {
2550 cb.cb_type = POOL_SCRUB_NONE;
2553 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2565 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2569 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
2572 typedef struct status_cbdata {
2574 boolean_t cb_allpools;
2575 boolean_t cb_verbose;
2576 boolean_t cb_explain;
2581 * Print out detailed scrub status.
2584 print_scrub_status(nvlist_t *nvroot)
2588 time_t start, end, now;
2589 double fraction_done;
2590 uint64_t examined, total, minutes_left, minutes_taken;
2593 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
2594 (uint64_t **)&vs, &vsc) == 0);
2597 * If there's never been a scrub, there's not much to say.
2599 if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
2600 (void) printf(gettext("none requested\n"));
2604 scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2605 "resilver" : "scrub";
2607 start = vs->vs_scrub_start;
2608 end = vs->vs_scrub_end;
2610 examined = vs->vs_scrub_examined;
2611 total = vs->vs_alloc;
2614 minutes_taken = (uint64_t)((end - start) / 60);
2616 (void) printf(gettext("%s %s after %lluh%um with %llu errors "
2618 scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
2619 (u_longlong_t)(minutes_taken / 60),
2620 (uint_t)(minutes_taken % 60),
2621 (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
2627 if (examined > total)
2630 fraction_done = (double)examined / total;
2631 minutes_left = (uint64_t)((now - start) *
2632 (1 - fraction_done) / fraction_done / 60);
2633 minutes_taken = (uint64_t)((now - start) / 60);
2635 (void) printf(gettext("%s in progress for %lluh%um, %.2f%% done, "
2636 "%lluh%um to go\n"),
2637 scrub_type, (u_longlong_t)(minutes_taken / 60),
2638 (uint_t)(minutes_taken % 60), 100 * fraction_done,
2639 (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
2642 typedef struct spare_cbdata {
2644 zpool_handle_t *cb_zhp;
2648 find_vdev(nvlist_t *nv, uint64_t search)
2654 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2658 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2659 &child, &children) == 0) {
2660 for (c = 0; c < children; c++)
2661 if (find_vdev(child[c], search))
2669 find_spare(zpool_handle_t *zhp, void *data)
2671 spare_cbdata_t *cbp = data;
2672 nvlist_t *config, *nvroot;
2674 config = zpool_get_config(zhp, NULL);
2675 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2678 if (find_vdev(nvroot, cbp->cb_guid)) {
2688 * Print out configuration state as requested by status_callback.
2691 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2692 int namewidth, int depth, boolean_t isspare, boolean_t print_logs)
2697 char rbuf[6], wbuf[6], cbuf[6], repaired[7];
2699 uint64_t notpresent;
2703 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2704 (uint64_t **)&vs, &c) == 0);
2706 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2707 &child, &children) != 0)
2710 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2713 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2716 if (vs->vs_aux == VDEV_AUX_SPARED)
2718 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2722 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
2726 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2727 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2728 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
2729 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
2732 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2733 ¬present) == 0) {
2735 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2736 (void) printf(" was %s", path);
2737 } else if (vs->vs_aux != 0) {
2740 switch (vs->vs_aux) {
2741 case VDEV_AUX_OPEN_FAILED:
2742 (void) printf(gettext("cannot open"));
2745 case VDEV_AUX_BAD_GUID_SUM:
2746 (void) printf(gettext("missing device"));
2749 case VDEV_AUX_NO_REPLICAS:
2750 (void) printf(gettext("insufficient replicas"));
2753 case VDEV_AUX_VERSION_NEWER:
2754 (void) printf(gettext("newer version"));
2757 case VDEV_AUX_SPARED:
2758 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2760 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
2761 if (strcmp(zpool_get_name(cb.cb_zhp),
2762 zpool_get_name(zhp)) == 0)
2763 (void) printf(gettext("currently in "
2766 (void) printf(gettext("in use by "
2768 zpool_get_name(cb.cb_zhp));
2769 zpool_close(cb.cb_zhp);
2771 (void) printf(gettext("currently in use"));
2775 case VDEV_AUX_ERR_EXCEEDED:
2776 (void) printf(gettext("too many errors"));
2780 (void) printf(gettext("corrupted data"));
2783 } else if (vs->vs_scrub_repaired != 0 && children == 0) {
2785 * Report bytes resilvered/repaired on leaf devices.
2787 zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
2788 (void) printf(gettext(" %s %s"), repaired,
2789 (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
2790 "resilvered" : "repaired");
2793 (void) printf("\n");
2795 for (c = 0; c < children; c++) {
2796 uint64_t is_log = B_FALSE;
2798 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2800 if ((is_log && !print_logs) || (!is_log && print_logs))
2802 vname = zpool_vdev_name(g_zfs, zhp, child[c]);
2803 print_status_config(zhp, vname, child[c],
2804 namewidth, depth + 2, isspare, B_FALSE);
2810 print_error_log(zpool_handle_t *zhp)
2812 nvlist_t *nverrlist = NULL;
2815 size_t len = MAXPATHLEN * 2;
2817 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
2818 (void) printf("errors: List of errors unavailable "
2819 "(insufficient privileges)\n");
2823 (void) printf("errors: Permanent errors have been "
2824 "detected in the following files:\n\n");
2826 pathname = safe_malloc(len);
2828 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
2830 uint64_t dsobj, obj;
2832 verify(nvpair_value_nvlist(elem, &nv) == 0);
2833 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
2835 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
2837 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
2838 (void) printf("%7s %s\n", "", pathname);
2841 nvlist_free(nverrlist);
2845 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
2854 (void) printf(gettext("\tspares\n"));
2856 for (i = 0; i < nspares; i++) {
2857 name = zpool_vdev_name(g_zfs, zhp, spares[i]);
2858 print_status_config(zhp, name, spares[i],
2859 namewidth, 2, B_TRUE, B_FALSE);
2865 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
2874 (void) printf(gettext("\tcache\n"));
2876 for (i = 0; i < nl2cache; i++) {
2877 name = zpool_vdev_name(g_zfs, zhp, l2cache[i]);
2878 print_status_config(zhp, name, l2cache[i],
2879 namewidth, 2, B_FALSE, B_FALSE);
2885 * Display a summary of pool status. Displays a summary such as:
2889 * reason: One or more devices ...
2890 * see: http://www.sun.com/msg/ZFS-xxxx-01
2896 * When given the '-v' option, we print out the complete config. If the '-e'
2897 * option is specified, then we print out error rate information as well.
2900 status_callback(zpool_handle_t *zhp, void *data)
2902 status_cbdata_t *cbp = data;
2903 nvlist_t *config, *nvroot;
2910 config = zpool_get_config(zhp, NULL);
2911 reason = zpool_get_status(zhp, &msgid);
2916 * If we were given 'zpool status -x', only report those pools with
2919 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
2920 if (!cbp->cb_allpools) {
2921 (void) printf(gettext("pool '%s' is healthy\n"),
2922 zpool_get_name(zhp));
2924 cbp->cb_first = B_FALSE;
2930 cbp->cb_first = B_FALSE;
2932 (void) printf("\n");
2934 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2936 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
2937 (uint64_t **)&vs, &c) == 0);
2938 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2940 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
2941 (void) printf(gettext(" state: %s\n"), health);
2944 case ZPOOL_STATUS_MISSING_DEV_R:
2945 (void) printf(gettext("status: One or more devices could not "
2946 "be opened. Sufficient replicas exist for\n\tthe pool to "
2947 "continue functioning in a degraded state.\n"));
2948 (void) printf(gettext("action: Attach the missing device and "
2949 "online it using 'zpool online'.\n"));
2952 case ZPOOL_STATUS_MISSING_DEV_NR:
2953 (void) printf(gettext("status: One or more devices could not "
2954 "be opened. There are insufficient\n\treplicas for the "
2955 "pool to continue functioning.\n"));
2956 (void) printf(gettext("action: Attach the missing device and "
2957 "online it using 'zpool online'.\n"));
2960 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2961 (void) printf(gettext("status: One or more devices could not "
2962 "be used because the label is missing or\n\tinvalid. "
2963 "Sufficient replicas exist for the pool to continue\n\t"
2964 "functioning in a degraded state.\n"));
2965 (void) printf(gettext("action: Replace the device using "
2966 "'zpool replace'.\n"));
2969 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2970 (void) printf(gettext("status: One or more devices could not "
2971 "be used because the label is missing \n\tor invalid. "
2972 "There are insufficient replicas for the pool to "
2973 "continue\n\tfunctioning.\n"));
2974 (void) printf(gettext("action: Destroy and re-create the pool "
2975 "from a backup source.\n"));
2978 case ZPOOL_STATUS_FAILING_DEV:
2979 (void) printf(gettext("status: One or more devices has "
2980 "experienced an unrecoverable error. An\n\tattempt was "
2981 "made to correct the error. Applications are "
2983 (void) printf(gettext("action: Determine if the device needs "
2984 "to be replaced, and clear the errors\n\tusing "
2985 "'zpool clear' or replace the device with 'zpool "
2989 case ZPOOL_STATUS_OFFLINE_DEV:
2990 (void) printf(gettext("status: One or more devices has "
2991 "been taken offline by the administrator.\n\tSufficient "
2992 "replicas exist for the pool to continue functioning in "
2993 "a\n\tdegraded state.\n"));
2994 (void) printf(gettext("action: Online the device using "
2995 "'zpool online' or replace the device with\n\t'zpool "
2999 case ZPOOL_STATUS_RESILVERING:
3000 (void) printf(gettext("status: One or more devices is "
3001 "currently being resilvered. The pool will\n\tcontinue "
3002 "to function, possibly in a degraded state.\n"));
3003 (void) printf(gettext("action: Wait for the resilver to "
3007 case ZPOOL_STATUS_CORRUPT_DATA:
3008 (void) printf(gettext("status: One or more devices has "
3009 "experienced an error resulting in data\n\tcorruption. "
3010 "Applications may be affected.\n"));
3011 (void) printf(gettext("action: Restore the file in question "
3012 "if possible. Otherwise restore the\n\tentire pool from "
3016 case ZPOOL_STATUS_CORRUPT_POOL:
3017 (void) printf(gettext("status: The pool metadata is corrupted "
3018 "and the pool cannot be opened.\n"));
3019 (void) printf(gettext("action: Destroy and re-create the pool "
3020 "from a backup source.\n"));
3023 case ZPOOL_STATUS_VERSION_OLDER:
3024 (void) printf(gettext("status: The pool is formatted using an "
3025 "older on-disk format. The pool can\n\tstill be used, but "
3026 "some features are unavailable.\n"));
3027 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3028 "upgrade'. Once this is done, the\n\tpool will no longer "
3029 "be accessible on older software versions.\n"));
3032 case ZPOOL_STATUS_VERSION_NEWER:
3033 (void) printf(gettext("status: The pool has been upgraded to a "
3034 "newer, incompatible on-disk version.\n\tThe pool cannot "
3035 "be accessed on this system.\n"));
3036 (void) printf(gettext("action: Access the pool from a system "
3037 "running more recent software, or\n\trestore the pool from "
3041 case ZPOOL_STATUS_FAULTED_DEV_R:
3042 (void) printf(gettext("status: One or more devices are "
3043 "faulted in response to persistent errors.\n\tSufficient "
3044 "replicas exist for the pool to continue functioning "
3045 "in a\n\tdegraded state.\n"));
3046 (void) printf(gettext("action: Replace the faulted device, "
3047 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3050 case ZPOOL_STATUS_FAULTED_DEV_NR:
3051 (void) printf(gettext("status: One or more devices are "
3052 "faulted in response to persistent errors. There are "
3053 "insufficient replicas for the pool to\n\tcontinue "
3055 (void) printf(gettext("action: Destroy and re-create the pool "
3056 "from a backup source. Manually marking the device\n"
3057 "\trepaired using 'zpool clear' may allow some data "
3058 "to be recovered.\n"));
3063 * The remaining errors can't actually be generated, yet.
3065 assert(reason == ZPOOL_STATUS_OK);
3069 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3072 if (config != NULL) {
3075 nvlist_t **spares, **l2cache;
3076 uint_t nspares, nl2cache;
3079 (void) printf(gettext(" scrub: "));
3080 print_scrub_status(nvroot);
3082 namewidth = max_width(zhp, nvroot, 0, 0);
3086 (void) printf(gettext("config:\n\n"));
3087 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3088 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3089 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3090 namewidth, 0, B_FALSE, B_FALSE);
3091 if (num_logs(nvroot) > 0)
3092 print_status_config(zhp, "logs", nvroot, namewidth, 0,
3095 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3096 &l2cache, &nl2cache) == 0)
3097 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3099 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3100 &spares, &nspares) == 0)
3101 print_spares(zhp, spares, nspares, namewidth);
3103 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3105 nvlist_t *nverrlist = NULL;
3108 * If the approximate error count is small, get a
3109 * precise count by fetching the entire log and
3110 * uniquifying the results.
3112 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3113 zpool_get_errlog(zhp, &nverrlist) == 0) {
3118 while ((elem = nvlist_next_nvpair(nverrlist,
3123 nvlist_free(nverrlist);
3125 (void) printf("\n");
3128 (void) printf(gettext("errors: No known data "
3130 else if (!cbp->cb_verbose)
3131 (void) printf(gettext("errors: %llu data "
3132 "errors, use '-v' for a list\n"),
3133 (u_longlong_t)nerr);
3135 print_error_log(zhp);
3138 (void) printf(gettext("config: The configuration cannot be "
3146 * zpool status [-vx] [pool] ...
3148 * -v Display complete error logs
3149 * -x Display only pools with potential problems
3151 * Describes the health status of all pools or some subset.
3154 zpool_do_status(int argc, char **argv)
3158 status_cbdata_t cb = { 0 };
3161 while ((c = getopt(argc, argv, "vx")) != -1) {
3164 cb.cb_verbose = B_TRUE;
3167 cb.cb_explain = B_TRUE;
3170 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3179 cb.cb_first = B_TRUE;
3182 cb.cb_allpools = B_TRUE;
3184 ret = for_each_pool(argc, argv, B_TRUE, NULL, status_callback, &cb);
3186 if (argc == 0 && cb.cb_count == 0)
3187 (void) printf(gettext("no pools available\n"));
3188 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3189 (void) printf(gettext("all pools are healthy\n"));
3194 typedef struct upgrade_cbdata {
3199 uint64_t cb_version;
3204 upgrade_cb(zpool_handle_t *zhp, void *arg)
3206 upgrade_cbdata_t *cbp = arg;
3211 config = zpool_get_config(zhp, NULL);
3212 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3215 if (!cbp->cb_newer && version < SPA_VERSION) {
3217 if (cbp->cb_first) {
3218 (void) printf(gettext("The following pools are "
3219 "out of date, and can be upgraded. After "
3220 "being\nupgraded, these pools will no "
3221 "longer be accessible by older software "
3223 (void) printf(gettext("VER POOL\n"));
3224 (void) printf(gettext("--- ------------\n"));
3225 cbp->cb_first = B_FALSE;
3228 (void) printf("%2llu %s\n", (u_longlong_t)version,
3229 zpool_get_name(zhp));
3231 cbp->cb_first = B_FALSE;
3232 ret = zpool_upgrade(zhp, cbp->cb_version);
3234 (void) printf(gettext("Successfully upgraded "
3235 "'%s'\n\n"), zpool_get_name(zhp));
3238 } else if (cbp->cb_newer && version > SPA_VERSION) {
3239 assert(!cbp->cb_all);
3241 if (cbp->cb_first) {
3242 (void) printf(gettext("The following pools are "
3243 "formatted using a newer software version and\n"
3244 "cannot be accessed on the current system.\n\n"));
3245 (void) printf(gettext("VER POOL\n"));
3246 (void) printf(gettext("--- ------------\n"));
3247 cbp->cb_first = B_FALSE;
3250 (void) printf("%2llu %s\n", (u_longlong_t)version,
3251 zpool_get_name(zhp));
3260 upgrade_one(zpool_handle_t *zhp, void *data)
3262 upgrade_cbdata_t *cbp = data;
3263 uint64_t cur_version;
3266 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3267 (void) printf(gettext("'log' is now a reserved word\n"
3268 "Pool 'log' must be renamed using export and import"
3273 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3274 if (cur_version > cbp->cb_version) {
3275 (void) printf(gettext("Pool '%s' is already formatted "
3276 "using more current version '%llu'.\n"),
3277 zpool_get_name(zhp), cur_version);
3280 if (cur_version == cbp->cb_version) {
3281 (void) printf(gettext("Pool '%s' is already formatted "
3282 "using the current version.\n"), zpool_get_name(zhp));
3286 ret = zpool_upgrade(zhp, cbp->cb_version);
3289 (void) printf(gettext("Successfully upgraded '%s' "
3290 "from version %llu to version %llu\n\n"),
3291 zpool_get_name(zhp), (u_longlong_t)cur_version,
3292 (u_longlong_t)cbp->cb_version);
3301 * zpool upgrade [-V version] <-a | pool ...>
3303 * With no arguments, display downrev'd ZFS pool available for upgrade.
3304 * Individual pools can be upgraded by specifying the pool, and '-a' will
3305 * upgrade all pools.
3308 zpool_do_upgrade(int argc, char **argv)
3311 upgrade_cbdata_t cb = { 0 };
3313 boolean_t showversions = B_FALSE;
3318 while ((c = getopt(argc, argv, "avV:")) != -1) {
3324 showversions = B_TRUE;
3327 cb.cb_version = strtoll(optarg, &end, 10);
3328 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3329 cb.cb_version < SPA_VERSION_1) {
3330 (void) fprintf(stderr,
3331 gettext("invalid version '%s'\n"), optarg);
3336 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3347 if (cb.cb_version == 0) {
3348 cb.cb_version = SPA_VERSION;
3349 } else if (!cb.cb_all && argc == 0) {
3350 (void) fprintf(stderr, gettext("-V option is "
3351 "incompatible with other arguments\n"));
3356 if (cb.cb_all || argc != 0) {
3357 (void) fprintf(stderr, gettext("-v option is "
3358 "incompatible with other arguments\n"));
3361 } else if (cb.cb_all) {
3363 (void) fprintf(stderr, gettext("-a option should not "
3364 "be used along with a pool name\n"));
3369 (void) printf(gettext("This system is currently running "
3370 "ZFS pool version %llu.\n\n"), SPA_VERSION);
3371 cb.cb_first = B_TRUE;
3373 (void) printf(gettext("The following versions are "
3375 (void) printf(gettext("VER DESCRIPTION\n"));
3376 (void) printf("--- -----------------------------------------"
3377 "---------------\n");
3378 (void) printf(gettext(" 1 Initial ZFS version\n"));
3379 (void) printf(gettext(" 2 Ditto blocks "
3380 "(replicated metadata)\n"));
3381 (void) printf(gettext(" 3 Hot spares and double parity "
3383 (void) printf(gettext(" 4 zpool history\n"));
3384 (void) printf(gettext(" 5 Compression using the gzip "
3386 (void) printf(gettext(" 6 bootfs pool property\n"));
3387 (void) printf(gettext(" 7 Separate intent log devices\n"));
3388 (void) printf(gettext(" 8 Delegated administration\n"));
3389 (void) printf(gettext(" 9 refquota and refreservation "
3391 (void) printf(gettext(" 10 Cache devices\n"));
3392 (void) printf(gettext("For more information on a particular "
3393 "version, including supported releases, see:\n\n"));
3394 (void) printf("http://www.opensolaris.org/os/community/zfs/"
3396 (void) printf(gettext("Where 'N' is the version number.\n"));
3397 } else if (argc == 0) {
3400 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3401 notfound = cb.cb_first;
3403 if (!cb.cb_all && ret == 0) {
3405 (void) printf("\n");
3406 cb.cb_first = B_TRUE;
3407 cb.cb_newer = B_TRUE;
3408 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
3411 (void) printf("\n");
3417 (void) printf(gettext("All pools are formatted "
3418 "using this version.\n"));
3419 else if (!cb.cb_all)
3420 (void) printf(gettext("Use 'zpool upgrade -v' "
3421 "for a list of available versions and "
3422 "their associated\nfeatures.\n"));
3425 ret = for_each_pool(argc, argv, B_FALSE, NULL,
3432 typedef struct hist_cbdata {
3438 char *hist_event_table[LOG_END] = {
3454 "pool property set",
3458 "destroy_begin_sync",
3462 "permission update",
3463 "permission remove",
3464 "permission who remove",
3473 "filesystem version upgrade",
3475 "refreservation set",
3479 * Print out the command history for a specific pool.
3482 get_history_one(zpool_handle_t *zhp, void *data)
3498 char internalstr[MAXPATHLEN];
3499 hist_cbdata_t *cb = (hist_cbdata_t *)data;
3503 cb->first = B_FALSE;
3505 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
3507 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
3510 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
3511 &records, &numrecords) == 0);
3512 for (i = 0; i < numrecords; i++) {
3513 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
3517 /* is it an internal event or a standard event? */
3518 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
3520 if (cb->internal == 0)
3523 if (nvlist_lookup_uint64(records[i],
3524 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
3526 verify(nvlist_lookup_uint64(records[i],
3527 ZPOOL_HIST_TXG, &txg) == 0);
3528 verify(nvlist_lookup_string(records[i],
3529 ZPOOL_HIST_INT_STR, &pathstr) == 0);
3530 if (ievent > LOG_END)
3532 (void) snprintf(internalstr,
3533 sizeof (internalstr),
3534 "[internal %s txg:%lld] %s",
3535 hist_event_table[ievent], txg,
3537 cmdstr = internalstr;
3540 (void) localtime_r(&tsec, &t);
3541 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
3542 (void) printf("%s %s", tbuf, cmdstr);
3545 (void) printf("\n");
3548 (void) printf(" [");
3549 if (nvlist_lookup_uint64(records[i],
3550 ZPOOL_HIST_WHO, &who) == 0) {
3551 pwd = getpwuid((uid_t)who);
3553 (void) printf("user %s on",
3556 (void) printf("user %d on",
3559 (void) printf(gettext("no info]\n"));
3562 if (nvlist_lookup_string(records[i],
3563 ZPOOL_HIST_HOST, &hostname) == 0) {
3564 (void) printf(" %s", hostname);
3566 if (nvlist_lookup_string(records[i],
3567 ZPOOL_HIST_ZONE, &zonename) == 0) {
3568 (void) printf(":%s", zonename);
3572 (void) printf("\n");
3574 (void) printf("\n");
3581 * zpool history <pool>
3583 * Displays the history of commands that modified pools.
3588 zpool_do_history(int argc, char **argv)
3590 hist_cbdata_t cbdata = { 0 };
3594 cbdata.first = B_TRUE;
3596 while ((c = getopt(argc, argv, "li")) != -1) {
3602 cbdata.internal = 1;
3605 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3613 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
3616 if (argc == 0 && cbdata.first == B_TRUE) {
3617 (void) printf(gettext("no pools available\n"));
3625 get_callback(zpool_handle_t *zhp, void *data)
3627 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
3628 char value[MAXNAMELEN];
3629 zprop_source_t srctype;
3632 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
3635 * Skip the special fake placeholder. This will also skip
3636 * over the name property when 'all' is specified.
3638 if (pl->pl_prop == ZPOOL_PROP_NAME &&
3639 pl == cbp->cb_proplist)
3642 if (zpool_get_prop(zhp, pl->pl_prop,
3643 value, sizeof (value), &srctype) != 0)
3646 zprop_print_one_property(zpool_get_name(zhp), cbp,
3647 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL);
3653 zpool_do_get(int argc, char **argv)
3655 zprop_get_cbdata_t cb = { 0 };
3656 zprop_list_t fake_name = { 0 };
3662 cb.cb_first = B_TRUE;
3663 cb.cb_sources = ZPROP_SRC_ALL;
3664 cb.cb_columns[0] = GET_COL_NAME;
3665 cb.cb_columns[1] = GET_COL_PROPERTY;
3666 cb.cb_columns[2] = GET_COL_VALUE;
3667 cb.cb_columns[3] = GET_COL_SOURCE;
3668 cb.cb_type = ZFS_TYPE_POOL;
3670 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
3671 ZFS_TYPE_POOL) != 0)
3674 if (cb.cb_proplist != NULL) {
3675 fake_name.pl_prop = ZPOOL_PROP_NAME;
3676 fake_name.pl_width = strlen(gettext("NAME"));
3677 fake_name.pl_next = cb.cb_proplist;
3678 cb.cb_proplist = &fake_name;
3681 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
3684 if (cb.cb_proplist == &fake_name)
3685 zprop_free_list(fake_name.pl_next);
3687 zprop_free_list(cb.cb_proplist);
3692 typedef struct set_cbdata {
3695 boolean_t cb_any_successful;
3699 set_callback(zpool_handle_t *zhp, void *data)
3702 set_cbdata_t *cb = (set_cbdata_t *)data;
3704 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
3707 cb->cb_any_successful = B_TRUE;
3713 zpool_do_set(int argc, char **argv)
3715 set_cbdata_t cb = { 0 };
3718 if (argc > 1 && argv[1][0] == '-') {
3719 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3725 (void) fprintf(stderr, gettext("missing property=value "
3731 (void) fprintf(stderr, gettext("missing pool name\n"));
3736 (void) fprintf(stderr, gettext("too many pool names\n"));
3740 cb.cb_propname = argv[1];
3741 cb.cb_value = strchr(cb.cb_propname, '=');
3742 if (cb.cb_value == NULL) {
3743 (void) fprintf(stderr, gettext("missing value in "
3744 "property=value argument\n"));
3748 *(cb.cb_value) = '\0';
3751 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
3758 find_command_idx(char *command, int *idx)
3762 for (i = 0; i < NCOMMAND; i++) {
3763 if (command_table[i].name == NULL)
3766 if (strcmp(command, command_table[i].name) == 0) {
3775 main(int argc, char **argv)
3781 (void) setlocale(LC_ALL, "");
3782 (void) textdomain(TEXT_DOMAIN);
3784 if ((g_zfs = libzfs_init()) == NULL) {
3785 (void) fprintf(stderr, gettext("internal error: failed to "
3786 "initialize ZFS library\n"));
3790 libzfs_print_on_error(g_zfs, B_TRUE);
3795 * Make sure the user has specified some command.
3798 (void) fprintf(stderr, gettext("missing command\n"));
3807 if (strcmp(cmdname, "-?") == 0)
3810 zpool_set_history_str("zpool", argc, argv, history_str);
3811 verify(zpool_stage_history(g_zfs, history_str) == 0);
3814 * Run the appropriate command.
3816 if (find_command_idx(cmdname, &i) == 0) {
3817 current_command = &command_table[i];
3818 ret = command_table[i].func(argc - 1, argv + 1);
3819 } else if (strchr(cmdname, '=')) {
3820 verify(find_command_idx("set", &i) == 0);
3821 current_command = &command_table[i];
3822 ret = command_table[i].func(argc, argv);
3823 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
3825 * 'freeze' is a vile debugging abomination, so we treat
3829 int fd = open(ZFS_DEV, O_RDWR);
3830 (void) strcpy((void *)buf, argv[2]);
3831 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
3833 (void) fprintf(stderr, gettext("unrecognized "
3834 "command '%s'\n"), cmdname);
3841 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
3842 * for the purposes of running ::findleaks.
3844 if (getenv("ZFS_ABORT") != NULL) {
3845 (void) printf("dumping core by request\n");