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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
43 #include <sys/fs/zfs.h>
48 #include "zpool_util.h"
49 #include "zfs_comutil.h"
51 #include "statcommon.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 **);
70 static int zpool_do_split(int, char **);
72 static int zpool_do_scrub(int, char **);
74 static int zpool_do_import(int, char **);
75 static int zpool_do_export(int, char **);
77 static int zpool_do_upgrade(int, char **);
79 static int zpool_do_history(int, char **);
81 static int zpool_do_get(int, char **);
82 static int zpool_do_set(int, char **);
85 * These libumem hooks provide a reasonable set of defaults for the allocator's
86 * debugging facilities.
91 _umem_debug_init(void)
93 return ("default,verbose"); /* $UMEM_DEBUG setting */
97 _umem_logging_init(void)
99 return ("fail,contents"); /* $UMEM_LOGGING setting */
128 typedef struct zpool_command {
130 int (*func)(int, char **);
135 * Master command table. Each ZFS command has a name, associated function, and
136 * usage message. The usage messages need to be internationalized, so we have
137 * to have a function to return the usage message based on a command index.
139 * These commands are organized according to how they are displayed in the usage
140 * message. An empty command (one with a NULL name) indicates an empty line in
141 * the generic usage message.
143 static zpool_command_t command_table[] = {
144 { "create", zpool_do_create, HELP_CREATE },
145 { "destroy", zpool_do_destroy, HELP_DESTROY },
147 { "add", zpool_do_add, HELP_ADD },
148 { "remove", zpool_do_remove, HELP_REMOVE },
150 { "list", zpool_do_list, HELP_LIST },
151 { "iostat", zpool_do_iostat, HELP_IOSTAT },
152 { "status", zpool_do_status, HELP_STATUS },
154 { "online", zpool_do_online, HELP_ONLINE },
155 { "offline", zpool_do_offline, HELP_OFFLINE },
156 { "clear", zpool_do_clear, HELP_CLEAR },
158 { "attach", zpool_do_attach, HELP_ATTACH },
159 { "detach", zpool_do_detach, HELP_DETACH },
160 { "replace", zpool_do_replace, HELP_REPLACE },
161 { "split", zpool_do_split, HELP_SPLIT },
163 { "scrub", zpool_do_scrub, HELP_SCRUB },
165 { "import", zpool_do_import, HELP_IMPORT },
166 { "export", zpool_do_export, HELP_EXPORT },
167 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
169 { "history", zpool_do_history, HELP_HISTORY },
170 { "get", zpool_do_get, HELP_GET },
171 { "set", zpool_do_set, HELP_SET },
174 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
176 zpool_command_t *current_command;
177 static char history_str[HIS_MAX_RECORD_LEN];
179 static uint_t timestamp_fmt = NODATE;
182 get_usage(zpool_help_t idx) {
185 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
187 return (gettext("\tattach [-f] <pool> <device> "
190 return (gettext("\tclear [-nF] <pool> [device]\n"));
192 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
193 "\t [-O file-system-property=value] ... \n"
194 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
196 return (gettext("\tdestroy [-f] <pool>\n"));
198 return (gettext("\tdetach <pool> <device>\n"));
200 return (gettext("\texport [-f] <pool> ...\n"));
202 return (gettext("\thistory [-il] [<pool>] ...\n"));
204 return (gettext("\timport [-d dir] [-D]\n"
205 "\timport [-d dir | -c cachefile] [-n] -F <pool | id>\n"
206 "\timport [-o mntopts] [-o property=value] ... \n"
207 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] -a\n"
208 "\timport [-o mntopts] [-o property=value] ... \n"
209 "\t [-d dir | -c cachefile] [-D] [-f] [-R root] "
210 "<pool | id> [newpool]\n"));
212 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
215 return (gettext("\tlist [-H] [-o property[,...]] "
216 "[-T d|u] [pool] ... [interval [count]]\n"));
218 return (gettext("\toffline [-t] <pool> <device> ...\n"));
220 return (gettext("\tonline <pool> <device> ...\n"));
222 return (gettext("\treplace [-f] <pool> <device> "
225 return (gettext("\tremove <pool> <device> ...\n"));
227 return (gettext("\tscrub [-s] <pool> ...\n"));
229 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
232 return (gettext("\tupgrade\n"
234 "\tupgrade [-V version] <-a | pool ...>\n"));
236 return (gettext("\tget <\"all\" | property[,...]> "
239 return (gettext("\tset <property=value> <pool> \n"));
241 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
242 "\t [-o property=value] <pool> <newpool> "
243 "[<device> ...]\n"));
252 * Callback routine that will print out a pool property value.
255 print_prop_cb(int prop, void *cb)
259 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
261 if (zpool_prop_readonly(prop))
262 (void) fprintf(fp, " NO ");
264 (void) fprintf(fp, " YES ");
266 if (zpool_prop_values(prop) == NULL)
267 (void) fprintf(fp, "-\n");
269 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
275 * Display usage message. If we're inside a command, display only the usage for
276 * that command. Otherwise, iterate over the entire command table and display
277 * a complete usage message.
280 usage(boolean_t requested)
282 FILE *fp = requested ? stdout : stderr;
284 if (current_command == NULL) {
287 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
289 gettext("where 'command' is one of the following:\n\n"));
291 for (i = 0; i < NCOMMAND; i++) {
292 if (command_table[i].name == NULL)
293 (void) fprintf(fp, "\n");
295 (void) fprintf(fp, "%s",
296 get_usage(command_table[i].usage));
299 (void) fprintf(fp, gettext("usage:\n"));
300 (void) fprintf(fp, "%s", get_usage(current_command->usage));
303 if (current_command != NULL &&
304 ((strcmp(current_command->name, "set") == 0) ||
305 (strcmp(current_command->name, "get") == 0) ||
306 (strcmp(current_command->name, "list") == 0))) {
309 gettext("\nthe following properties are supported:\n"));
311 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
312 "PROPERTY", "EDIT", "VALUES");
314 /* Iterate over all properties */
315 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
320 * See comments at end of main().
322 if (getenv("ZFS_ABORT") != NULL) {
323 (void) printf("dumping core by request\n");
327 exit(requested ? 0 : 2);
331 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
332 boolean_t print_logs)
339 (void) printf("\t%*s%s\n", indent, "", name);
341 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
342 &child, &children) != 0)
345 for (c = 0; c < children; c++) {
346 uint64_t is_log = B_FALSE;
348 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
350 if ((is_log && !print_logs) || (!is_log && print_logs))
353 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
354 print_vdev_tree(zhp, vname, child[c], indent + 2,
361 * Add a property pair (name, string-value) into a property nvlist.
364 add_prop_list(const char *propname, char *propval, nvlist_t **props,
367 zpool_prop_t prop = ZPROP_INVAL;
373 if (*props == NULL &&
374 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
375 (void) fprintf(stderr,
376 gettext("internal error: out of memory\n"));
383 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
384 (void) fprintf(stderr, gettext("property '%s' is "
385 "not a valid pool property\n"), propname);
388 normnm = zpool_prop_to_name(prop);
390 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
391 normnm = zfs_prop_to_name(fprop);
397 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
398 prop != ZPOOL_PROP_CACHEFILE) {
399 (void) fprintf(stderr, gettext("property '%s' "
400 "specified multiple times\n"), propname);
404 if (nvlist_add_string(proplist, normnm, propval) != 0) {
405 (void) fprintf(stderr, gettext("internal "
406 "error: out of memory\n"));
414 * zpool add [-fn] <pool> <vdev> ...
416 * -f Force addition of devices, even if they appear in use
417 * -n Do not add the devices, but display the resulting layout if
418 * they were to be added.
420 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
421 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
425 zpool_do_add(int argc, char **argv)
427 boolean_t force = B_FALSE;
428 boolean_t dryrun = B_FALSE;
437 while ((c = getopt(argc, argv, "fn")) != -1) {
446 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
455 /* get pool name and check number of arguments */
457 (void) fprintf(stderr, gettext("missing pool name argument\n"));
461 (void) fprintf(stderr, gettext("missing vdev specification\n"));
470 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
473 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
474 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
480 /* pass off to get_vdev_spec for processing */
481 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
483 if (nvroot == NULL) {
489 nvlist_t *poolnvroot;
491 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
494 (void) printf(gettext("would update '%s' to the following "
495 "configuration:\n"), zpool_get_name(zhp));
497 /* print original main pool and new tree */
498 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
499 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
501 /* Do the same for the logs */
502 if (num_logs(poolnvroot) > 0) {
503 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
504 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
505 } else if (num_logs(nvroot) > 0) {
506 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
511 ret = (zpool_add(zhp, nvroot) != 0);
521 * zpool remove <pool> <vdev> ...
523 * Removes the given vdev from the pool. Currently, this supports removing
524 * spares, cache, and log devices from the pool.
527 zpool_do_remove(int argc, char **argv)
536 /* get pool name and check number of arguments */
538 (void) fprintf(stderr, gettext("missing pool name argument\n"));
542 (void) fprintf(stderr, gettext("missing device\n"));
548 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
551 for (i = 1; i < argc; i++) {
552 if (zpool_vdev_remove(zhp, argv[i]) != 0)
560 * zpool create [-fn] [-o property=value] ...
561 * [-O file-system-property=value] ...
562 * [-R root] [-m mountpoint] <pool> <dev> ...
564 * -f Force creation, even if devices appear in use
565 * -n Do not create the pool, but display the resulting layout if it
566 * were to be created.
567 * -R Create a pool under an alternate root
568 * -m Set default mountpoint for the root dataset. By default it's
570 * -o Set property=value.
571 * -O Set fsproperty=value in the pool's root file system
573 * Creates the named pool according to the given vdev specification. The
574 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
575 * we get the nvlist back from get_vdev_spec(), we either print out the contents
576 * (if '-n' was specified), or pass it to libzfs to do the creation.
579 zpool_do_create(int argc, char **argv)
581 boolean_t force = B_FALSE;
582 boolean_t dryrun = B_FALSE;
584 nvlist_t *nvroot = NULL;
587 char *altroot = NULL;
588 char *mountpoint = NULL;
589 nvlist_t *fsprops = NULL;
590 nvlist_t *props = NULL;
594 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
604 if (add_prop_list(zpool_prop_to_name(
605 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
607 if (nvlist_lookup_string(props,
608 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
611 if (add_prop_list(zpool_prop_to_name(
612 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
619 if ((propval = strchr(optarg, '=')) == NULL) {
620 (void) fprintf(stderr, gettext("missing "
621 "'=' for -o option\n"));
627 if (add_prop_list(optarg, propval, &props, B_TRUE))
631 if ((propval = strchr(optarg, '=')) == NULL) {
632 (void) fprintf(stderr, gettext("missing "
633 "'=' for -O option\n"));
639 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
643 (void) fprintf(stderr, gettext("missing argument for "
644 "'%c' option\n"), optopt);
647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
656 /* get pool name and check number of arguments */
658 (void) fprintf(stderr, gettext("missing pool name argument\n"));
662 (void) fprintf(stderr, gettext("missing vdev specification\n"));
669 * As a special case, check for use of '/' in the name, and direct the
670 * user to use 'zfs create' instead.
672 if (strchr(poolname, '/') != NULL) {
673 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
674 "character '/' in pool name\n"), poolname);
675 (void) fprintf(stderr, gettext("use 'zfs create' to "
676 "create a dataset\n"));
680 /* pass off to get_vdev_spec for bulk processing */
681 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
686 /* make_root_vdev() allows 0 toplevel children if there are spares */
687 if (!zfs_allocatable_devs(nvroot)) {
688 (void) fprintf(stderr, gettext("invalid vdev "
689 "specification: at least one toplevel vdev must be "
695 if (altroot != NULL && altroot[0] != '/') {
696 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
697 "must be an absolute path\n"), altroot);
702 * Check the validity of the mountpoint and direct the user to use the
703 * '-m' mountpoint option if it looks like its in use.
705 if (mountpoint == NULL ||
706 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
707 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
708 char buf[MAXPATHLEN];
711 if (mountpoint && mountpoint[0] != '/') {
712 (void) fprintf(stderr, gettext("invalid mountpoint "
713 "'%s': must be an absolute path, 'legacy', or "
714 "'none'\n"), mountpoint);
718 if (mountpoint == NULL) {
720 (void) snprintf(buf, sizeof (buf), "%s/%s",
723 (void) snprintf(buf, sizeof (buf), "/%s",
727 (void) snprintf(buf, sizeof (buf), "%s%s",
728 altroot, mountpoint);
730 (void) snprintf(buf, sizeof (buf), "%s",
734 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
735 (void) fprintf(stderr, gettext("mountpoint '%s' : "
736 "%s\n"), buf, strerror(errno));
737 (void) fprintf(stderr, gettext("use '-m' "
738 "option to provide a different default\n"));
743 while (count < 3 && readdir(dirp) != NULL)
745 (void) closedir(dirp);
748 (void) fprintf(stderr, gettext("mountpoint "
749 "'%s' exists and is not empty\n"), buf);
750 (void) fprintf(stderr, gettext("use '-m' "
751 "option to provide a "
752 "different default\n"));
760 * For a dry run invocation, print out a basic message and run
761 * through all the vdevs in the list and print out in an
762 * appropriate hierarchy.
764 (void) printf(gettext("would create '%s' with the "
765 "following layout:\n\n"), poolname);
767 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
768 if (num_logs(nvroot) > 0)
769 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
774 * Hand off to libzfs.
776 if (zpool_create(g_zfs, poolname,
777 nvroot, props, fsprops) == 0) {
778 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
779 ZFS_TYPE_FILESYSTEM);
781 if (mountpoint != NULL)
782 verify(zfs_prop_set(pool,
784 ZFS_PROP_MOUNTPOINT),
786 if (zfs_mount(pool, NULL, 0) == 0)
787 ret = zfs_shareall(pool);
790 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
791 (void) fprintf(stderr, gettext("pool name may have "
798 nvlist_free(fsprops);
802 nvlist_free(fsprops);
809 * zpool destroy <pool>
811 * -f Forcefully unmount any datasets
813 * Destroy the given pool. Automatically unmounts any datasets in the pool.
816 zpool_do_destroy(int argc, char **argv)
818 boolean_t force = B_FALSE;
825 while ((c = getopt(argc, argv, "f")) != -1) {
831 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
840 /* check arguments */
842 (void) fprintf(stderr, gettext("missing pool argument\n"));
846 (void) fprintf(stderr, gettext("too many arguments\n"));
852 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
854 * As a special case, check for use of '/' in the name, and
855 * direct the user to use 'zfs destroy' instead.
857 if (strchr(pool, '/') != NULL)
858 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
859 "destroy a dataset\n"));
863 if (zpool_disable_datasets(zhp, force) != 0) {
864 (void) fprintf(stderr, gettext("could not destroy '%s': "
865 "could not unmount datasets\n"), zpool_get_name(zhp));
869 ret = (zpool_destroy(zhp) != 0);
877 * zpool export [-f] <pool> ...
879 * -f Forcefully unmount datasets
881 * Export the given pools. By default, the command will attempt to cleanly
882 * unmount any active datasets within the pool. If the '-f' flag is specified,
883 * then the datasets will be forcefully unmounted.
886 zpool_do_export(int argc, char **argv)
888 boolean_t force = B_FALSE;
889 boolean_t hardforce = B_FALSE;
896 while ((c = getopt(argc, argv, "fF")) != -1) {
905 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
914 /* check arguments */
916 (void) fprintf(stderr, gettext("missing pool argument\n"));
921 for (i = 0; i < argc; i++) {
922 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
927 if (zpool_disable_datasets(zhp, force) != 0) {
934 if (zpool_export_force(zhp) != 0)
936 } else if (zpool_export(zhp, force) != 0) {
947 * Given a vdev configuration, determine the maximum width needed for the device
951 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
953 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
958 if (strlen(name) + depth > max)
959 max = strlen(name) + depth;
963 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
964 &child, &children) == 0) {
965 for (c = 0; c < children; c++)
966 if ((ret = max_width(zhp, child[c], depth + 2,
971 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
972 &child, &children) == 0) {
973 for (c = 0; c < children; c++)
974 if ((ret = max_width(zhp, child[c], depth + 2,
979 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
980 &child, &children) == 0) {
981 for (c = 0; c < children; c++)
982 if ((ret = max_width(zhp, child[c], depth + 2,
991 typedef struct spare_cbdata {
993 zpool_handle_t *cb_zhp;
997 find_vdev(nvlist_t *nv, uint64_t search)
1003 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1007 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1008 &child, &children) == 0) {
1009 for (c = 0; c < children; c++)
1010 if (find_vdev(child[c], search))
1018 find_spare(zpool_handle_t *zhp, void *data)
1020 spare_cbdata_t *cbp = data;
1021 nvlist_t *config, *nvroot;
1023 config = zpool_get_config(zhp, NULL);
1024 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1027 if (find_vdev(nvroot, cbp->cb_guid)) {
1037 * Print out configuration state as requested by status_callback.
1040 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1041 int namewidth, int depth, boolean_t isspare)
1045 pool_scan_stat_t *ps = NULL;
1047 char rbuf[6], wbuf[6], cbuf[6];
1049 uint64_t notpresent;
1053 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1054 &child, &children) != 0)
1057 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1058 (uint64_t **)&vs, &c) == 0);
1060 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1063 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1066 if (vs->vs_aux == VDEV_AUX_SPARED)
1068 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1072 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1076 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1077 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1078 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1079 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1082 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1083 ¬present) == 0) {
1085 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1086 (void) printf(" was %s", path);
1087 } else if (vs->vs_aux != 0) {
1090 switch (vs->vs_aux) {
1091 case VDEV_AUX_OPEN_FAILED:
1092 (void) printf(gettext("cannot open"));
1095 case VDEV_AUX_BAD_GUID_SUM:
1096 (void) printf(gettext("missing device"));
1099 case VDEV_AUX_NO_REPLICAS:
1100 (void) printf(gettext("insufficient replicas"));
1103 case VDEV_AUX_VERSION_NEWER:
1104 (void) printf(gettext("newer version"));
1107 case VDEV_AUX_SPARED:
1108 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1110 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1111 if (strcmp(zpool_get_name(cb.cb_zhp),
1112 zpool_get_name(zhp)) == 0)
1113 (void) printf(gettext("currently in "
1116 (void) printf(gettext("in use by "
1118 zpool_get_name(cb.cb_zhp));
1119 zpool_close(cb.cb_zhp);
1121 (void) printf(gettext("currently in use"));
1125 case VDEV_AUX_ERR_EXCEEDED:
1126 (void) printf(gettext("too many errors"));
1129 case VDEV_AUX_IO_FAILURE:
1130 (void) printf(gettext("experienced I/O failures"));
1133 case VDEV_AUX_BAD_LOG:
1134 (void) printf(gettext("bad intent log"));
1137 case VDEV_AUX_EXTERNAL:
1138 (void) printf(gettext("external device fault"));
1141 case VDEV_AUX_SPLIT_POOL:
1142 (void) printf(gettext("split into new pool"));
1146 (void) printf(gettext("corrupted data"));
1151 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1152 (uint64_t **)&ps, &c);
1154 if (ps && ps->pss_state == DSS_SCANNING &&
1155 vs->vs_scan_processed != 0 && children == 0) {
1156 (void) printf(gettext(" (%s)"),
1157 (ps->pss_func == POOL_SCAN_RESILVER) ?
1158 "resilvering" : "repairing");
1161 (void) printf("\n");
1163 for (c = 0; c < children; c++) {
1164 uint64_t islog = B_FALSE, ishole = B_FALSE;
1166 /* Don't print logs or holes here */
1167 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1169 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1171 if (islog || ishole)
1173 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1174 print_status_config(zhp, vname, child[c],
1175 namewidth, depth + 2, isspare);
1182 * Print the configuration of an exported pool. Iterate over all vdevs in the
1183 * pool, printing out the name and status for each one.
1186 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1193 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1194 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1195 strcmp(type, VDEV_TYPE_HOLE) == 0)
1198 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1199 (uint64_t **)&vs, &c) == 0);
1201 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1202 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1204 if (vs->vs_aux != 0) {
1207 switch (vs->vs_aux) {
1208 case VDEV_AUX_OPEN_FAILED:
1209 (void) printf(gettext("cannot open"));
1212 case VDEV_AUX_BAD_GUID_SUM:
1213 (void) printf(gettext("missing device"));
1216 case VDEV_AUX_NO_REPLICAS:
1217 (void) printf(gettext("insufficient replicas"));
1220 case VDEV_AUX_VERSION_NEWER:
1221 (void) printf(gettext("newer version"));
1224 case VDEV_AUX_ERR_EXCEEDED:
1225 (void) printf(gettext("too many errors"));
1229 (void) printf(gettext("corrupted data"));
1233 (void) printf("\n");
1235 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1236 &child, &children) != 0)
1239 for (c = 0; c < children; c++) {
1240 uint64_t is_log = B_FALSE;
1242 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1247 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1248 print_import_config(vname, child[c], namewidth, depth + 2);
1252 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1253 &child, &children) == 0) {
1254 (void) printf(gettext("\tcache\n"));
1255 for (c = 0; c < children; c++) {
1256 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1257 (void) printf("\t %s\n", vname);
1262 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1263 &child, &children) == 0) {
1264 (void) printf(gettext("\tspares\n"));
1265 for (c = 0; c < children; c++) {
1266 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1267 (void) printf("\t %s\n", vname);
1275 * Logs are recorded as top level vdevs in the main pool child array
1276 * but with "is_log" set to 1. We use either print_status_config() or
1277 * print_import_config() to print the top level logs then any log
1278 * children (eg mirrored slogs) are printed recursively - which
1279 * works because only the top level vdev is marked "is_log"
1282 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1287 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1291 (void) printf(gettext("\tlogs\n"));
1293 for (c = 0; c < children; c++) {
1294 uint64_t is_log = B_FALSE;
1297 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1301 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1303 print_status_config(zhp, name, child[c], namewidth,
1306 print_import_config(name, child[c], namewidth, 2);
1312 * Display the status for the given pool.
1315 show_import(nvlist_t *config)
1317 uint64_t pool_state;
1328 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1330 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1332 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1334 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1337 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1338 (uint64_t **)&vs, &vsc) == 0);
1339 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1341 reason = zpool_import_status(config, &msgid);
1343 (void) printf(gettext(" pool: %s\n"), name);
1344 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1345 (void) printf(gettext(" state: %s"), health);
1346 if (pool_state == POOL_STATE_DESTROYED)
1347 (void) printf(gettext(" (DESTROYED)"));
1348 (void) printf("\n");
1351 case ZPOOL_STATUS_MISSING_DEV_R:
1352 case ZPOOL_STATUS_MISSING_DEV_NR:
1353 case ZPOOL_STATUS_BAD_GUID_SUM:
1354 (void) printf(gettext("status: One or more devices are missing "
1355 "from the system.\n"));
1358 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1359 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1360 (void) printf(gettext("status: One or more devices contains "
1361 "corrupted data.\n"));
1364 case ZPOOL_STATUS_CORRUPT_DATA:
1365 (void) printf(gettext("status: The pool data is corrupted.\n"));
1368 case ZPOOL_STATUS_OFFLINE_DEV:
1369 (void) printf(gettext("status: One or more devices "
1370 "are offlined.\n"));
1373 case ZPOOL_STATUS_CORRUPT_POOL:
1374 (void) printf(gettext("status: The pool metadata is "
1378 case ZPOOL_STATUS_VERSION_OLDER:
1379 (void) printf(gettext("status: The pool is formatted using an "
1380 "older on-disk version.\n"));
1383 case ZPOOL_STATUS_VERSION_NEWER:
1384 (void) printf(gettext("status: The pool is formatted using an "
1385 "incompatible version.\n"));
1388 case ZPOOL_STATUS_HOSTID_MISMATCH:
1389 (void) printf(gettext("status: The pool was last accessed by "
1390 "another system.\n"));
1393 case ZPOOL_STATUS_FAULTED_DEV_R:
1394 case ZPOOL_STATUS_FAULTED_DEV_NR:
1395 (void) printf(gettext("status: One or more devices are "
1399 case ZPOOL_STATUS_BAD_LOG:
1400 (void) printf(gettext("status: An intent log record cannot be "
1404 case ZPOOL_STATUS_RESILVERING:
1405 (void) printf(gettext("status: One or more devices were being "
1411 * No other status can be seen when importing pools.
1413 assert(reason == ZPOOL_STATUS_OK);
1417 * Print out an action according to the overall state of the pool.
1419 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1420 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1421 (void) printf(gettext("action: The pool can be "
1422 "imported using its name or numeric identifier, "
1423 "though\n\tsome features will not be available "
1424 "without an explicit 'zpool upgrade'.\n"));
1425 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1426 (void) printf(gettext("action: The pool can be "
1427 "imported using its name or numeric "
1428 "identifier and\n\tthe '-f' flag.\n"));
1430 (void) printf(gettext("action: The pool can be "
1431 "imported using its name or numeric "
1433 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1434 (void) printf(gettext("action: The pool can be imported "
1435 "despite missing or damaged devices. The\n\tfault "
1436 "tolerance of the pool may be compromised if imported.\n"));
1439 case ZPOOL_STATUS_VERSION_NEWER:
1440 (void) printf(gettext("action: The pool cannot be "
1441 "imported. Access the pool on a system running "
1442 "newer\n\tsoftware, or recreate the pool from "
1445 case ZPOOL_STATUS_MISSING_DEV_R:
1446 case ZPOOL_STATUS_MISSING_DEV_NR:
1447 case ZPOOL_STATUS_BAD_GUID_SUM:
1448 (void) printf(gettext("action: The pool cannot be "
1449 "imported. Attach the missing\n\tdevices and try "
1453 (void) printf(gettext("action: The pool cannot be "
1454 "imported due to damaged devices or data.\n"));
1459 * If the state is "closed" or "can't open", and the aux state
1460 * is "corrupt data":
1462 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1463 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1464 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1465 if (pool_state == POOL_STATE_DESTROYED)
1466 (void) printf(gettext("\tThe pool was destroyed, "
1467 "but can be imported using the '-Df' flags.\n"));
1468 else if (pool_state != POOL_STATE_EXPORTED)
1469 (void) printf(gettext("\tThe pool may be active on "
1470 "another system, but can be imported using\n\t"
1471 "the '-f' flag.\n"));
1475 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1478 (void) printf(gettext("config:\n\n"));
1480 namewidth = max_width(NULL, nvroot, 0, 0);
1484 print_import_config(name, nvroot, namewidth, 0);
1485 if (num_logs(nvroot) > 0)
1486 print_logs(NULL, nvroot, namewidth, B_FALSE);
1488 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1489 (void) printf(gettext("\n\tAdditional devices are known to "
1490 "be part of this pool, though their\n\texact "
1491 "configuration cannot be determined.\n"));
1496 * Perform the import for the given configuration. This passes the heavy
1497 * lifting off to zpool_import_props(), and then mounts the datasets contained
1501 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1502 int force, nvlist_t *props, boolean_t do_verbatim)
1504 zpool_handle_t *zhp;
1509 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1512 verify(nvlist_lookup_uint64(config,
1513 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1514 verify(nvlist_lookup_uint64(config,
1515 ZPOOL_CONFIG_VERSION, &version) == 0);
1516 if (version > SPA_VERSION) {
1517 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1518 "is formatted using a newer ZFS version\n"), name);
1520 } else if (state != POOL_STATE_EXPORTED && !force) {
1523 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1525 if ((unsigned long)hostid != gethostid()) {
1530 verify(nvlist_lookup_string(config,
1531 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1532 verify(nvlist_lookup_uint64(config,
1533 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1535 (void) fprintf(stderr, gettext("cannot import "
1536 "'%s': pool may be in use from other "
1537 "system, it was last accessed by %s "
1538 "(hostid: 0x%lx) on %s"), name, hostname,
1539 (unsigned long)hostid,
1540 asctime(localtime(&t)));
1541 (void) fprintf(stderr, gettext("use '-f' to "
1542 "import anyway\n"));
1546 (void) fprintf(stderr, gettext("cannot import '%s': "
1547 "pool may be in use from other system\n"), name);
1548 (void) fprintf(stderr, gettext("use '-f' to import "
1554 if (zpool_import_props(g_zfs, config, newname, props, do_verbatim) != 0)
1557 if (newname != NULL)
1558 name = (char *)newname;
1560 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1563 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1564 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1574 * zpool import [-d dir] [-D]
1575 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1576 * [-d dir | -c cachefile] [-f] -a
1577 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1578 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1580 * -c Read pool information from a cachefile instead of searching
1583 * -d Scan in a specific directory, other than /dev/dsk. More than
1584 * one directory can be specified using multiple '-d' options.
1586 * -D Scan for previously destroyed pools or import all or only
1587 * specified destroyed pools.
1589 * -R Temporarily import the pool, with all mountpoints relative to
1590 * the given root. The pool will remain exported when the machine
1593 * -V Import even in the presence of faulted vdevs. This is an
1594 * intentionally undocumented option for testing purposes, and
1595 * treats the pool configuration as complete, leaving any bad
1596 * vdevs in the FAULTED state. In other words, it does verbatim
1599 * -f Force import, even if it appears that the pool is active.
1601 * -F Attempt rewind if necessary.
1603 * -n See if rewind would work, but don't actually rewind.
1605 * -a Import all pools found.
1607 * -o Set property=value and/or temporary mount options (without '=').
1609 * The import command scans for pools to import, and import pools based on pool
1610 * name and GUID. The pool can also be renamed as part of the import process.
1613 zpool_do_import(int argc, char **argv)
1615 char **searchdirs = NULL;
1619 nvlist_t *pools = NULL;
1620 boolean_t do_all = B_FALSE;
1621 boolean_t do_destroyed = B_FALSE;
1622 char *mntopts = NULL;
1623 boolean_t do_force = B_FALSE;
1626 uint64_t searchguid = 0;
1627 char *searchname = NULL;
1629 nvlist_t *found_config;
1630 nvlist_t *policy = NULL;
1631 nvlist_t *props = NULL;
1633 boolean_t do_verbatim = B_FALSE;
1634 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1635 boolean_t dryrun = B_FALSE;
1636 boolean_t do_rewind = B_FALSE;
1637 boolean_t xtreme_rewind = B_FALSE;
1638 uint64_t pool_state;
1639 char *cachefile = NULL;
1640 importargs_t idata = { 0 };
1643 while ((c = getopt(argc, argv, ":aCc:d:DEfFno:rR:VX")) != -1) {
1652 if (searchdirs == NULL) {
1653 searchdirs = safe_malloc(sizeof (char *));
1655 char **tmp = safe_malloc((nsearch + 1) *
1657 bcopy(searchdirs, tmp, nsearch *
1662 searchdirs[nsearch++] = optarg;
1665 do_destroyed = B_TRUE;
1677 if ((propval = strchr(optarg, '=')) != NULL) {
1680 if (add_prop_list(optarg, propval,
1688 if (add_prop_list(zpool_prop_to_name(
1689 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1691 if (nvlist_lookup_string(props,
1692 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1695 if (add_prop_list(zpool_prop_to_name(
1696 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1700 do_verbatim = B_TRUE;
1703 xtreme_rewind = B_TRUE;
1706 (void) fprintf(stderr, gettext("missing argument for "
1707 "'%c' option\n"), optopt);
1711 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1720 if (cachefile && nsearch != 0) {
1721 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1725 if ((dryrun || xtreme_rewind) && !do_rewind) {
1726 (void) fprintf(stderr,
1727 gettext("-n or -X only meaningful with -F\n"));
1731 rewind_policy = ZPOOL_TRY_REWIND;
1733 rewind_policy = ZPOOL_DO_REWIND;
1735 rewind_policy |= ZPOOL_EXTREME_REWIND;
1737 /* In the future, we can capture further policy and include it here */
1738 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1739 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1742 if (searchdirs == NULL) {
1743 searchdirs = safe_malloc(sizeof (char *));
1744 searchdirs[0] = "/dev/dsk";
1748 /* check argument count */
1751 (void) fprintf(stderr, gettext("too many arguments\n"));
1756 (void) fprintf(stderr, gettext("too many arguments\n"));
1761 * Check for the SYS_CONFIG privilege. We do this explicitly
1762 * here because otherwise any attempt to discover pools will
1765 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1766 (void) fprintf(stderr, gettext("cannot "
1767 "discover pools: permission denied\n"));
1769 nvlist_free(policy);
1775 * Depending on the arguments given, we do one of the following:
1777 * <none> Iterate through all pools and display information about
1780 * -a Iterate through all pools and try to import each one.
1782 * <id> Find the pool that corresponds to the given GUID/pool
1783 * name and import that one.
1785 * -D Above options applies only to destroyed pools.
1791 searchguid = strtoull(argv[0], &endptr, 10);
1792 if (errno != 0 || *endptr != '\0')
1793 searchname = argv[0];
1794 found_config = NULL;
1797 * User specified a name or guid. Ensure it's unique.
1799 idata.unique = B_TRUE;
1803 idata.path = searchdirs;
1804 idata.paths = nsearch;
1805 idata.poolname = searchname;
1806 idata.guid = searchguid;
1807 idata.cachefile = cachefile;
1809 pools = zpool_search_import(g_zfs, &idata);
1811 if (pools != NULL && idata.exists &&
1812 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1813 (void) fprintf(stderr, gettext("cannot import '%s': "
1814 "a pool with that name already exists\n"),
1816 (void) fprintf(stderr, gettext("use the form '%s "
1817 "<pool | id> <newpool>' to give it a new name\n"),
1820 } else if (pools == NULL && idata.exists) {
1821 (void) fprintf(stderr, gettext("cannot import '%s': "
1822 "a pool with that name is already created/imported,\n"),
1824 (void) fprintf(stderr, gettext("and no additional pools "
1825 "with that name were found\n"));
1827 } else if (pools == NULL) {
1829 (void) fprintf(stderr, gettext("cannot import '%s': "
1830 "no such pool available\n"), argv[0]);
1837 nvlist_free(policy);
1842 * At this point we have a list of import candidate configs. Even if
1843 * we were searching by pool name or guid, we still need to
1844 * post-process the list to deal with pool state and possible
1850 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1852 verify(nvpair_value_nvlist(elem, &config) == 0);
1854 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1856 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1858 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1861 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1868 (void) printf("\n");
1871 err |= do_import(config, NULL, mntopts,
1872 do_force, props, do_verbatim);
1874 show_import(config);
1876 } else if (searchname != NULL) {
1880 * We are searching for a pool based on name.
1882 verify(nvlist_lookup_string(config,
1883 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1885 if (strcmp(name, searchname) == 0) {
1886 if (found_config != NULL) {
1887 (void) fprintf(stderr, gettext(
1888 "cannot import '%s': more than "
1889 "one matching pool\n"), searchname);
1890 (void) fprintf(stderr, gettext(
1891 "import by numeric ID instead\n"));
1894 found_config = config;
1900 * Search for a pool by guid.
1902 verify(nvlist_lookup_uint64(config,
1903 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1905 if (guid == searchguid)
1906 found_config = config;
1911 * If we were searching for a specific pool, verify that we found a
1912 * pool, and then do the import.
1914 if (argc != 0 && err == 0) {
1915 if (found_config == NULL) {
1916 (void) fprintf(stderr, gettext("cannot import '%s': "
1917 "no such pool available\n"), argv[0]);
1920 err |= do_import(found_config, argc == 1 ? NULL :
1921 argv[1], mntopts, do_force, props, do_verbatim);
1926 * If we were just looking for pools, report an error if none were
1929 if (argc == 0 && first)
1930 (void) fprintf(stderr,
1931 gettext("no pools available to import\n"));
1936 nvlist_free(policy);
1939 return (err ? 1 : 0);
1942 typedef struct iostat_cbdata {
1943 zpool_list_t *cb_list;
1950 print_iostat_separator(iostat_cbdata_t *cb)
1954 for (i = 0; i < cb->cb_namewidth; i++)
1956 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1960 print_iostat_header(iostat_cbdata_t *cb)
1962 (void) printf("%*s capacity operations bandwidth\n",
1963 cb->cb_namewidth, "");
1964 (void) printf("%-*s alloc free read write read write\n",
1965 cb->cb_namewidth, "pool");
1966 print_iostat_separator(cb);
1970 * Display a single statistic.
1973 print_one_stat(uint64_t value)
1977 zfs_nicenum(value, buf, sizeof (buf));
1978 (void) printf(" %5s", buf);
1982 * Print out all the statistics for the given vdev. This can either be the
1983 * toplevel configuration, or called recursively. If 'name' is NULL, then this
1984 * is a verbose output, and we don't want to display the toplevel pool stats.
1987 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
1988 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
1990 nvlist_t **oldchild, **newchild;
1992 vdev_stat_t *oldvs, *newvs;
1993 vdev_stat_t zerovs = { 0 };
1998 if (oldnv != NULL) {
1999 verify(nvlist_lookup_uint64_array(oldnv,
2000 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2005 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2006 (uint64_t **)&newvs, &c) == 0);
2008 if (strlen(name) + depth > cb->cb_namewidth)
2009 (void) printf("%*s%s", depth, "", name);
2011 (void) printf("%*s%s%*s", depth, "", name,
2012 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2014 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2019 scale = (double)NANOSEC / tdelta;
2021 /* only toplevel vdevs have capacity stats */
2022 if (newvs->vs_space == 0) {
2023 (void) printf(" - -");
2025 print_one_stat(newvs->vs_alloc);
2026 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2029 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2030 oldvs->vs_ops[ZIO_TYPE_READ])));
2032 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2033 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2035 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2036 oldvs->vs_bytes[ZIO_TYPE_READ])));
2038 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2039 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2041 (void) printf("\n");
2043 if (!cb->cb_verbose)
2046 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2047 &newchild, &children) != 0)
2050 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2051 &oldchild, &c) != 0)
2054 for (c = 0; c < children; c++) {
2055 uint64_t ishole = B_FALSE;
2057 if (nvlist_lookup_uint64(newchild[c],
2058 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2061 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2062 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2063 newchild[c], cb, depth + 2);
2068 * Include level 2 ARC devices in iostat output
2070 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2071 &newchild, &children) != 0)
2074 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2075 &oldchild, &c) != 0)
2079 (void) printf("%-*s - - - - - "
2080 "-\n", cb->cb_namewidth, "cache");
2081 for (c = 0; c < children; c++) {
2082 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2084 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2085 newchild[c], cb, depth + 2);
2092 refresh_iostat(zpool_handle_t *zhp, void *data)
2094 iostat_cbdata_t *cb = data;
2098 * If the pool has disappeared, remove it from the list and continue.
2100 if (zpool_refresh_stats(zhp, &missing) != 0)
2104 pool_list_remove(cb->cb_list, zhp);
2110 * Callback to print out the iostats for the given pool.
2113 print_iostat(zpool_handle_t *zhp, void *data)
2115 iostat_cbdata_t *cb = data;
2116 nvlist_t *oldconfig, *newconfig;
2117 nvlist_t *oldnvroot, *newnvroot;
2119 newconfig = zpool_get_config(zhp, &oldconfig);
2121 if (cb->cb_iteration == 1)
2124 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2127 if (oldconfig == NULL)
2130 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2134 * Print out the statistics for the pool.
2136 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2139 print_iostat_separator(cb);
2145 get_namewidth(zpool_handle_t *zhp, void *data)
2147 iostat_cbdata_t *cb = data;
2148 nvlist_t *config, *nvroot;
2150 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2151 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2153 if (!cb->cb_verbose)
2154 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2156 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2160 * The width must fall into the range [10,38]. The upper limit is the
2161 * maximum we can have and still fit in 80 columns.
2163 if (cb->cb_namewidth < 10)
2164 cb->cb_namewidth = 10;
2165 if (cb->cb_namewidth > 38)
2166 cb->cb_namewidth = 38;
2172 * Parse the input string, get the 'interval' and 'count' value if there is one.
2175 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2178 unsigned long interval = 0, count = 0;
2179 int argc = *argcp, errno;
2182 * Determine if the last argument is an integer or a pool name
2184 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2188 interval = strtoul(argv[argc - 1], &end, 10);
2190 if (*end == '\0' && errno == 0) {
2191 if (interval == 0) {
2192 (void) fprintf(stderr, gettext("interval "
2193 "cannot be zero\n"));
2197 * Ignore the last parameter
2202 * If this is not a valid number, just plow on. The
2203 * user will get a more informative error message later
2211 * If the last argument is also an integer, then we have both a count
2214 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2219 interval = strtoul(argv[argc - 1], &end, 10);
2221 if (*end == '\0' && errno == 0) {
2222 if (interval == 0) {
2223 (void) fprintf(stderr, gettext("interval "
2224 "cannot be zero\n"));
2229 * Ignore the last parameter
2243 get_timestamp_arg(char c)
2246 timestamp_fmt = UDATE;
2248 timestamp_fmt = DDATE;
2254 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2256 * -v Display statistics for individual vdevs
2257 * -T Display a timestamp in date(1) or Unix format
2259 * This command can be tricky because we want to be able to deal with pool
2260 * creation/destruction as well as vdev configuration changes. The bulk of this
2261 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2262 * on pool_list_update() to detect the addition of new pools. Configuration
2263 * changes are all handled within libzfs.
2266 zpool_do_iostat(int argc, char **argv)
2271 unsigned long interval = 0, count = 0;
2273 boolean_t verbose = B_FALSE;
2277 while ((c = getopt(argc, argv, "T:v")) != -1) {
2280 get_timestamp_arg(*optarg);
2286 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2295 get_interval_count(&argc, argv, &interval, &count);
2298 * Construct the list of all interesting pools.
2301 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2304 if (pool_list_count(list) == 0 && argc != 0) {
2305 pool_list_free(list);
2309 if (pool_list_count(list) == 0 && interval == 0) {
2310 pool_list_free(list);
2311 (void) fprintf(stderr, gettext("no pools available\n"));
2316 * Enter the main iostat loop.
2319 cb.cb_verbose = verbose;
2320 cb.cb_iteration = 0;
2321 cb.cb_namewidth = 0;
2324 pool_list_update(list);
2326 if ((npools = pool_list_count(list)) == 0)
2330 * Refresh all statistics. This is done as an explicit step
2331 * before calculating the maximum name width, so that any
2332 * configuration changes are properly accounted for.
2334 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2337 * Iterate over all pools to determine the maximum width
2338 * for the pool / device name column across all pools.
2340 cb.cb_namewidth = 0;
2341 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2343 if (timestamp_fmt != NODATE)
2344 print_timestamp(timestamp_fmt);
2347 * If it's the first time, or verbose mode, print the header.
2349 if (++cb.cb_iteration == 1 || verbose)
2350 print_iostat_header(&cb);
2352 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2355 * If there's more than one pool, and we're not in verbose mode
2356 * (which prints a separator for us), then print a separator.
2358 if (npools > 1 && !verbose)
2359 print_iostat_separator(&cb);
2362 (void) printf("\n");
2365 * Flush the output so that redirection to a file isn't buffered
2368 (void) fflush(stdout);
2373 if (count != 0 && --count == 0)
2376 (void) sleep(interval);
2379 pool_list_free(list);
2384 typedef struct list_cbdata {
2385 boolean_t cb_scripted;
2387 zprop_list_t *cb_proplist;
2391 * Given a list of columns to display, output appropriate headers for each one.
2394 print_header(zprop_list_t *pl)
2397 boolean_t first = B_TRUE;
2398 boolean_t right_justify;
2400 for (; pl != NULL; pl = pl->pl_next) {
2401 if (pl->pl_prop == ZPROP_INVAL)
2409 header = zpool_prop_column_name(pl->pl_prop);
2410 right_justify = zpool_prop_align_right(pl->pl_prop);
2412 if (pl->pl_next == NULL && !right_justify)
2413 (void) printf("%s", header);
2414 else if (right_justify)
2415 (void) printf("%*s", pl->pl_width, header);
2417 (void) printf("%-*s", pl->pl_width, header);
2420 (void) printf("\n");
2424 * Given a pool and a list of properties, print out all the properties according
2425 * to the described layout.
2428 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2430 boolean_t first = B_TRUE;
2431 char property[ZPOOL_MAXPROPLEN];
2433 boolean_t right_justify;
2436 for (; pl != NULL; pl = pl->pl_next) {
2439 (void) printf("\t");
2446 right_justify = B_FALSE;
2447 if (pl->pl_prop != ZPROP_INVAL) {
2448 if (zpool_get_prop(zhp, pl->pl_prop, property,
2449 sizeof (property), NULL) != 0)
2454 right_justify = zpool_prop_align_right(pl->pl_prop);
2459 width = pl->pl_width;
2462 * If this is being called in scripted mode, or if this is the
2463 * last column and it is left-justified, don't include a width
2466 if (scripted || (pl->pl_next == NULL && !right_justify))
2467 (void) printf("%s", propstr);
2468 else if (right_justify)
2469 (void) printf("%*s", width, propstr);
2471 (void) printf("%-*s", width, propstr);
2474 (void) printf("\n");
2478 * Generic callback function to list a pool.
2481 list_callback(zpool_handle_t *zhp, void *data)
2483 list_cbdata_t *cbp = data;
2485 if (cbp->cb_first) {
2486 if (!cbp->cb_scripted)
2487 print_header(cbp->cb_proplist);
2488 cbp->cb_first = B_FALSE;
2491 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2497 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2499 * -H Scripted mode. Don't display headers, and separate properties
2501 * -o List of properties to display. Defaults to
2502 * "name,size,allocated,free,capacity,health,altroot"
2503 * -T Display a timestamp in date(1) or Unix format
2505 * List all pools in the system, whether or not they're healthy. Output space
2506 * statistics for each one, as well as health status summary.
2509 zpool_do_list(int argc, char **argv)
2513 list_cbdata_t cb = { 0 };
2514 static char default_props[] =
2515 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2516 char *props = default_props;
2517 unsigned long interval = 0, count = 0;
2520 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2523 cb.cb_scripted = B_TRUE;
2529 get_timestamp_arg(*optarg);
2532 (void) fprintf(stderr, gettext("missing argument for "
2533 "'%c' option\n"), optopt);
2537 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2546 get_interval_count(&argc, argv, &interval, &count);
2548 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2551 cb.cb_first = B_TRUE;
2555 if (timestamp_fmt != NODATE)
2556 print_timestamp(timestamp_fmt);
2558 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2559 list_callback, &cb);
2561 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2562 (void) printf(gettext("no pools available\n"));
2563 zprop_free_list(cb.cb_proplist);
2570 if (count != 0 && --count == 0)
2573 (void) sleep(interval);
2576 zprop_free_list(cb.cb_proplist);
2581 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2588 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2589 &child, &children) != 0) {
2590 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2591 if (strncmp(name, "/dev/dsk/", 9) == 0)
2593 if (strncmp(path, "/dev/dsk/", 9) == 0)
2595 if (strcmp(name, path) == 0)
2600 for (c = 0; c < children; c++)
2601 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2608 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2610 boolean_t force = B_FALSE;
2613 char *poolname, *old_disk, *new_disk;
2614 zpool_handle_t *zhp;
2618 while ((c = getopt(argc, argv, "f")) != -1) {
2624 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2633 /* get pool name and check number of arguments */
2635 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2642 (void) fprintf(stderr,
2643 gettext("missing <device> specification\n"));
2651 (void) fprintf(stderr,
2652 gettext("missing <new_device> specification\n"));
2655 new_disk = old_disk;
2665 (void) fprintf(stderr, gettext("too many arguments\n"));
2669 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2672 if (zpool_get_config(zhp, NULL) == NULL) {
2673 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2679 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2681 if (nvroot == NULL) {
2686 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2688 nvlist_free(nvroot);
2695 * zpool replace [-f] <pool> <device> <new_device>
2697 * -f Force attach, even if <new_device> appears to be in use.
2699 * Replace <device> with <new_device>.
2703 zpool_do_replace(int argc, char **argv)
2705 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2709 * zpool attach [-f] <pool> <device> <new_device>
2711 * -f Force attach, even if <new_device> appears to be in use.
2713 * Attach <new_device> to the mirror containing <device>. If <device> is not
2714 * part of a mirror, then <device> will be transformed into a mirror of
2715 * <device> and <new_device>. In either case, <new_device> will begin life
2716 * with a DTL of [0, now], and will immediately begin to resilver itself.
2719 zpool_do_attach(int argc, char **argv)
2721 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2725 * zpool detach [-f] <pool> <device>
2727 * -f Force detach of <device>, even if DTLs argue against it
2728 * (not supported yet)
2730 * Detach a device from a mirror. The operation will be refused if <device>
2731 * is the last device in the mirror, or if the DTLs indicate that this device
2732 * has the only valid copy of some data.
2736 zpool_do_detach(int argc, char **argv)
2739 char *poolname, *path;
2740 zpool_handle_t *zhp;
2744 while ((c = getopt(argc, argv, "f")) != -1) {
2748 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2757 /* get pool name and check number of arguments */
2759 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2764 (void) fprintf(stderr,
2765 gettext("missing <device> specification\n"));
2772 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2775 ret = zpool_vdev_detach(zhp, path);
2783 * zpool split [-n] [-o prop=val] ...
2785 * [-R altroot] <pool> <newpool> [<device> ...]
2787 * -n Do not split the pool, but display the resulting layout if
2788 * it were to be split.
2789 * -o Set property=value, or set mount options.
2790 * -R Mount the split-off pool under an alternate root.
2792 * Splits the named pool and gives it the new pool name. Devices to be split
2793 * off may be listed, provided that no more than one device is specified
2794 * per top-level vdev mirror. The newly split pool is left in an exported
2795 * state unless -R is specified.
2797 * Restrictions: the top-level of the pool pool must only be made up of
2798 * mirrors; all devices in the pool must be healthy; no device may be
2799 * undergoing a resilvering operation.
2802 zpool_do_split(int argc, char **argv)
2804 char *srcpool, *newpool, *propval;
2805 char *mntopts = NULL;
2808 zpool_handle_t *zhp;
2809 nvlist_t *config, *props = NULL;
2811 flags.dryrun = B_FALSE;
2812 flags.import = B_FALSE;
2815 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
2818 flags.import = B_TRUE;
2820 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
2821 &props, B_TRUE) != 0) {
2828 flags.dryrun = B_TRUE;
2831 if ((propval = strchr(optarg, '=')) != NULL) {
2834 if (add_prop_list(optarg, propval,
2835 &props, B_TRUE) != 0) {
2845 (void) fprintf(stderr, gettext("missing argument for "
2846 "'%c' option\n"), optopt);
2850 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2857 if (!flags.import && mntopts != NULL) {
2858 (void) fprintf(stderr, gettext("setting mntopts is only "
2859 "valid when importing the pool\n"));
2867 (void) fprintf(stderr, gettext("Missing pool name\n"));
2871 (void) fprintf(stderr, gettext("Missing new pool name\n"));
2881 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
2884 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
2885 if (config == NULL) {
2889 (void) printf(gettext("would create '%s' with the "
2890 "following layout:\n\n"), newpool);
2891 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
2893 nvlist_free(config);
2898 if (ret != 0 || flags.dryrun || !flags.import)
2902 * The split was successful. Now we need to open the new
2903 * pool and import it.
2905 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
2907 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2908 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2910 (void) fprintf(stderr, gettext("Split was succssful, but "
2911 "the datasets could not all be mounted\n"));
2912 (void) fprintf(stderr, gettext("Try doing '%s' with a "
2913 "different altroot\n"), "zpool import");
2923 * zpool online <pool> <device> ...
2926 zpool_do_online(int argc, char **argv)
2930 zpool_handle_t *zhp;
2932 vdev_state_t newstate;
2936 while ((c = getopt(argc, argv, "et")) != -1) {
2939 flags |= ZFS_ONLINE_EXPAND;
2943 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2952 /* get pool name and check number of arguments */
2954 (void) fprintf(stderr, gettext("missing pool name\n"));
2958 (void) fprintf(stderr, gettext("missing device name\n"));
2964 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2967 for (i = 1; i < argc; i++) {
2968 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
2969 if (newstate != VDEV_STATE_HEALTHY) {
2970 (void) printf(gettext("warning: device '%s' "
2971 "onlined, but remains in faulted state\n"),
2973 if (newstate == VDEV_STATE_FAULTED)
2974 (void) printf(gettext("use 'zpool "
2975 "clear' to restore a faulted "
2978 (void) printf(gettext("use 'zpool "
2979 "replace' to replace devices "
2980 "that are no longer present\n"));
2993 * zpool offline [-ft] <pool> <device> ...
2995 * -f Force the device into the offline state, even if doing
2996 * so would appear to compromise pool availability.
2997 * (not supported yet)
2999 * -t Only take the device off-line temporarily. The offline
3000 * state will not be persistent across reboots.
3004 zpool_do_offline(int argc, char **argv)
3008 zpool_handle_t *zhp;
3010 boolean_t istmp = B_FALSE;
3013 while ((c = getopt(argc, argv, "ft")) != -1) {
3020 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3029 /* get pool name and check number of arguments */
3031 (void) fprintf(stderr, gettext("missing pool name\n"));
3035 (void) fprintf(stderr, gettext("missing device name\n"));
3041 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3044 for (i = 1; i < argc; i++) {
3045 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3055 * zpool clear <pool> [device]
3057 * Clear all errors associated with a pool or a particular device.
3060 zpool_do_clear(int argc, char **argv)
3064 boolean_t dryrun = B_FALSE;
3065 boolean_t do_rewind = B_FALSE;
3066 boolean_t xtreme_rewind = B_FALSE;
3067 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3068 nvlist_t *policy = NULL;
3069 zpool_handle_t *zhp;
3070 char *pool, *device;
3073 while ((c = getopt(argc, argv, "FnX")) != -1) {
3082 xtreme_rewind = B_TRUE;
3085 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3095 (void) fprintf(stderr, gettext("missing pool name\n"));
3100 (void) fprintf(stderr, gettext("too many arguments\n"));
3104 if ((dryrun || xtreme_rewind) && !do_rewind) {
3105 (void) fprintf(stderr,
3106 gettext("-n or -X only meaningful with -F\n"));
3110 rewind_policy = ZPOOL_TRY_REWIND;
3112 rewind_policy = ZPOOL_DO_REWIND;
3114 rewind_policy |= ZPOOL_EXTREME_REWIND;
3116 /* In future, further rewind policy choices can be passed along here */
3117 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3118 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3122 device = argc == 2 ? argv[1] : NULL;
3124 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3125 nvlist_free(policy);
3129 if (zpool_clear(zhp, device, policy) != 0)
3134 nvlist_free(policy);
3139 typedef struct scrub_cbdata {
3146 scrub_callback(zpool_handle_t *zhp, void *data)
3148 scrub_cbdata_t *cb = data;
3152 * Ignore faulted pools.
3154 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3155 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3156 "currently unavailable\n"), zpool_get_name(zhp));
3160 err = zpool_scan(zhp, cb->cb_type);
3166 * zpool scrub [-s] <pool> ...
3168 * -s Stop. Stops any in-progress scrub.
3171 zpool_do_scrub(int argc, char **argv)
3176 cb.cb_type = POOL_SCAN_SCRUB;
3179 while ((c = getopt(argc, argv, "s")) != -1) {
3182 cb.cb_type = POOL_SCAN_NONE;
3185 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3197 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3201 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3204 typedef struct status_cbdata {
3206 boolean_t cb_allpools;
3207 boolean_t cb_verbose;
3208 boolean_t cb_explain;
3210 boolean_t cb_dedup_stats;
3214 * Print out detailed scrub status.
3217 print_scan_status(pool_scan_stat_t *ps)
3220 uint64_t elapsed, mins_left;
3221 uint64_t pass_exam, examined, total;
3223 double fraction_done;
3224 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3226 (void) printf(gettext(" scan: "));
3228 /* If there's never been a scan, there's not much to say. */
3229 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3230 ps->pss_func >= POOL_SCAN_FUNCS) {
3231 (void) printf(gettext("none requested\n"));
3235 start = ps->pss_start_time;
3236 end = ps->pss_end_time;
3237 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3239 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3240 ps->pss_func == POOL_SCAN_RESILVER);
3242 * Scan is finished or canceled.
3244 if (ps->pss_state == DSS_FINISHED) {
3245 uint64_t minutes_taken = (end - start) / 60;
3248 if (ps->pss_func == POOL_SCAN_SCRUB) {
3249 fmt = gettext("scrub repaired %s in %lluh%um with "
3250 "%llu errors on %s");
3251 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3252 fmt = gettext("resilvered %s in %lluh%um with "
3253 "%llu errors on %s");
3256 (void) printf(fmt, processed_buf,
3257 (u_longlong_t)(minutes_taken / 60),
3258 (uint_t)(minutes_taken % 60),
3259 (u_longlong_t)ps->pss_errors,
3260 ctime((time_t *)&end));
3262 } else if (ps->pss_state == DSS_CANCELED) {
3263 if (ps->pss_func == POOL_SCAN_SCRUB) {
3264 (void) printf(gettext("scrub canceled on %s"),
3266 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3267 (void) printf(gettext("resilver canceled on %s"),
3273 assert(ps->pss_state == DSS_SCANNING);
3276 * Scan is in progress.
3278 if (ps->pss_func == POOL_SCAN_SCRUB) {
3279 (void) printf(gettext("scrub in progress since %s"),
3281 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3282 (void) printf(gettext("resilver in progress since %s"),
3286 examined = ps->pss_examined ? ps->pss_examined : 1;
3287 total = ps->pss_to_examine;
3288 fraction_done = (double)examined / total;
3290 /* elapsed time for this pass */
3291 elapsed = time(NULL) - ps->pss_pass_start;
3292 elapsed = elapsed ? elapsed : 1;
3293 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3294 rate = pass_exam / elapsed;
3295 rate = rate ? rate : 1;
3296 mins_left = ((total - examined) / rate) / 60;
3298 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3299 zfs_nicenum(total, total_buf, sizeof (total_buf));
3300 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3302 (void) printf(gettext(" %s scanned out of %s at "
3303 "%s/s, %lluh%um to go\n"), examined_buf, total_buf, rate_buf,
3304 (u_longlong_t)(mins_left / 60),
3305 (uint_t)(mins_left % 60));
3307 if (ps->pss_func == POOL_SCAN_RESILVER) {
3308 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3309 processed_buf, 100 * fraction_done);
3310 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3311 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3312 processed_buf, 100 * fraction_done);
3317 print_error_log(zpool_handle_t *zhp)
3319 nvlist_t *nverrlist = NULL;
3322 size_t len = MAXPATHLEN * 2;
3324 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3325 (void) printf("errors: List of errors unavailable "
3326 "(insufficient privileges)\n");
3330 (void) printf("errors: Permanent errors have been "
3331 "detected in the following files:\n\n");
3333 pathname = safe_malloc(len);
3335 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3337 uint64_t dsobj, obj;
3339 verify(nvpair_value_nvlist(elem, &nv) == 0);
3340 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3342 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3344 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3345 (void) printf("%7s %s\n", "", pathname);
3348 nvlist_free(nverrlist);
3352 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3361 (void) printf(gettext("\tspares\n"));
3363 for (i = 0; i < nspares; i++) {
3364 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3365 print_status_config(zhp, name, spares[i],
3366 namewidth, 2, B_TRUE);
3372 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3381 (void) printf(gettext("\tcache\n"));
3383 for (i = 0; i < nl2cache; i++) {
3384 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3385 print_status_config(zhp, name, l2cache[i],
3386 namewidth, 2, B_FALSE);
3392 print_dedup_stats(nvlist_t *config)
3394 ddt_histogram_t *ddh;
3400 * If the pool was faulted then we may not have been able to
3401 * obtain the config. Otherwise, if have anything in the dedup
3402 * table continue processing the stats.
3404 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3405 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3408 (void) printf("\n");
3409 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3410 (u_longlong_t)ddo->ddo_count,
3411 (u_longlong_t)ddo->ddo_dspace,
3412 (u_longlong_t)ddo->ddo_mspace);
3414 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3415 (uint64_t **)&dds, &c) == 0);
3416 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3417 (uint64_t **)&ddh, &c) == 0);
3418 zpool_dump_ddt(dds, ddh);
3422 * Display a summary of pool status. Displays a summary such as:
3426 * reason: One or more devices ...
3427 * see: http://www.sun.com/msg/ZFS-xxxx-01
3433 * When given the '-v' option, we print out the complete config. If the '-e'
3434 * option is specified, then we print out error rate information as well.
3437 status_callback(zpool_handle_t *zhp, void *data)
3439 status_cbdata_t *cbp = data;
3440 nvlist_t *config, *nvroot;
3447 config = zpool_get_config(zhp, NULL);
3448 reason = zpool_get_status(zhp, &msgid);
3453 * If we were given 'zpool status -x', only report those pools with
3456 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3457 if (!cbp->cb_allpools) {
3458 (void) printf(gettext("pool '%s' is healthy\n"),
3459 zpool_get_name(zhp));
3461 cbp->cb_first = B_FALSE;
3467 cbp->cb_first = B_FALSE;
3469 (void) printf("\n");
3471 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3473 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3474 (uint64_t **)&vs, &c) == 0);
3475 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3477 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3478 (void) printf(gettext(" state: %s\n"), health);
3481 case ZPOOL_STATUS_MISSING_DEV_R:
3482 (void) printf(gettext("status: One or more devices could not "
3483 "be opened. Sufficient replicas exist for\n\tthe pool to "
3484 "continue functioning in a degraded state.\n"));
3485 (void) printf(gettext("action: Attach the missing device and "
3486 "online it using 'zpool online'.\n"));
3489 case ZPOOL_STATUS_MISSING_DEV_NR:
3490 (void) printf(gettext("status: One or more devices could not "
3491 "be opened. There are insufficient\n\treplicas for the "
3492 "pool to continue functioning.\n"));
3493 (void) printf(gettext("action: Attach the missing device and "
3494 "online it using 'zpool online'.\n"));
3497 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3498 (void) printf(gettext("status: One or more devices could not "
3499 "be used because the label is missing or\n\tinvalid. "
3500 "Sufficient replicas exist for the pool to continue\n\t"
3501 "functioning in a degraded state.\n"));
3502 (void) printf(gettext("action: Replace the device using "
3503 "'zpool replace'.\n"));
3506 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3507 (void) printf(gettext("status: One or more devices could not "
3508 "be used because the label is missing \n\tor invalid. "
3509 "There are insufficient replicas for the pool to "
3510 "continue\n\tfunctioning.\n"));
3511 zpool_explain_recover(zpool_get_handle(zhp),
3512 zpool_get_name(zhp), reason, config);
3515 case ZPOOL_STATUS_FAILING_DEV:
3516 (void) printf(gettext("status: One or more devices has "
3517 "experienced an unrecoverable error. An\n\tattempt was "
3518 "made to correct the error. Applications are "
3520 (void) printf(gettext("action: Determine if the device needs "
3521 "to be replaced, and clear the errors\n\tusing "
3522 "'zpool clear' or replace the device with 'zpool "
3526 case ZPOOL_STATUS_OFFLINE_DEV:
3527 (void) printf(gettext("status: One or more devices has "
3528 "been taken offline by the administrator.\n\tSufficient "
3529 "replicas exist for the pool to continue functioning in "
3530 "a\n\tdegraded state.\n"));
3531 (void) printf(gettext("action: Online the device using "
3532 "'zpool online' or replace the device with\n\t'zpool "
3536 case ZPOOL_STATUS_REMOVED_DEV:
3537 (void) printf(gettext("status: One or more devices has "
3538 "been removed by the administrator.\n\tSufficient "
3539 "replicas exist for the pool to continue functioning in "
3540 "a\n\tdegraded state.\n"));
3541 (void) printf(gettext("action: Online the device using "
3542 "'zpool online' or replace the device with\n\t'zpool "
3546 case ZPOOL_STATUS_RESILVERING:
3547 (void) printf(gettext("status: One or more devices is "
3548 "currently being resilvered. The pool will\n\tcontinue "
3549 "to function, possibly in a degraded state.\n"));
3550 (void) printf(gettext("action: Wait for the resilver to "
3554 case ZPOOL_STATUS_CORRUPT_DATA:
3555 (void) printf(gettext("status: One or more devices has "
3556 "experienced an error resulting in data\n\tcorruption. "
3557 "Applications may be affected.\n"));
3558 (void) printf(gettext("action: Restore the file in question "
3559 "if possible. Otherwise restore the\n\tentire pool from "
3563 case ZPOOL_STATUS_CORRUPT_POOL:
3564 (void) printf(gettext("status: The pool metadata is corrupted "
3565 "and the pool cannot be opened.\n"));
3566 zpool_explain_recover(zpool_get_handle(zhp),
3567 zpool_get_name(zhp), reason, config);
3570 case ZPOOL_STATUS_VERSION_OLDER:
3571 (void) printf(gettext("status: The pool is formatted using an "
3572 "older on-disk format. The pool can\n\tstill be used, but "
3573 "some features are unavailable.\n"));
3574 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3575 "upgrade'. Once this is done, the\n\tpool will no longer "
3576 "be accessible on older software versions.\n"));
3579 case ZPOOL_STATUS_VERSION_NEWER:
3580 (void) printf(gettext("status: The pool has been upgraded to a "
3581 "newer, incompatible on-disk version.\n\tThe pool cannot "
3582 "be accessed on this system.\n"));
3583 (void) printf(gettext("action: Access the pool from a system "
3584 "running more recent software, or\n\trestore the pool from "
3588 case ZPOOL_STATUS_FAULTED_DEV_R:
3589 (void) printf(gettext("status: One or more devices are "
3590 "faulted in response to persistent errors.\n\tSufficient "
3591 "replicas exist for the pool to continue functioning "
3592 "in a\n\tdegraded state.\n"));
3593 (void) printf(gettext("action: Replace the faulted device, "
3594 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3597 case ZPOOL_STATUS_FAULTED_DEV_NR:
3598 (void) printf(gettext("status: One or more devices are "
3599 "faulted in response to persistent errors. There are "
3600 "insufficient replicas for the pool to\n\tcontinue "
3602 (void) printf(gettext("action: Destroy and re-create the pool "
3603 "from a backup source. Manually marking the device\n"
3604 "\trepaired using 'zpool clear' may allow some data "
3605 "to be recovered.\n"));
3608 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3609 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3610 (void) printf(gettext("status: One or more devices are "
3611 "faulted in response to IO failures.\n"));
3612 (void) printf(gettext("action: Make sure the affected devices "
3613 "are connected, then run 'zpool clear'.\n"));
3616 case ZPOOL_STATUS_BAD_LOG:
3617 (void) printf(gettext("status: An intent log record "
3618 "could not be read.\n"
3619 "\tWaiting for adminstrator intervention to fix the "
3620 "faulted pool.\n"));
3621 (void) printf(gettext("action: Either restore the affected "
3622 "device(s) and run 'zpool online',\n"
3623 "\tor ignore the intent log records by running "
3624 "'zpool clear'.\n"));
3629 * The remaining errors can't actually be generated, yet.
3631 assert(reason == ZPOOL_STATUS_OK);
3635 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3638 if (config != NULL) {
3641 nvlist_t **spares, **l2cache;
3642 uint_t nspares, nl2cache;
3643 pool_scan_stat_t *ps = NULL;
3645 (void) nvlist_lookup_uint64_array(nvroot,
3646 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3647 print_scan_status(ps);
3649 namewidth = max_width(zhp, nvroot, 0, 0);
3653 (void) printf(gettext("config:\n\n"));
3654 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3655 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3656 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3657 namewidth, 0, B_FALSE);
3659 if (num_logs(nvroot) > 0)
3660 print_logs(zhp, nvroot, namewidth, B_TRUE);
3661 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3662 &l2cache, &nl2cache) == 0)
3663 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3665 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3666 &spares, &nspares) == 0)
3667 print_spares(zhp, spares, nspares, namewidth);
3669 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3671 nvlist_t *nverrlist = NULL;
3674 * If the approximate error count is small, get a
3675 * precise count by fetching the entire log and
3676 * uniquifying the results.
3678 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3679 zpool_get_errlog(zhp, &nverrlist) == 0) {
3684 while ((elem = nvlist_next_nvpair(nverrlist,
3689 nvlist_free(nverrlist);
3691 (void) printf("\n");
3694 (void) printf(gettext("errors: No known data "
3696 else if (!cbp->cb_verbose)
3697 (void) printf(gettext("errors: %llu data "
3698 "errors, use '-v' for a list\n"),
3699 (u_longlong_t)nerr);
3701 print_error_log(zhp);
3704 if (cbp->cb_dedup_stats)
3705 print_dedup_stats(config);
3707 (void) printf(gettext("config: The configuration cannot be "
3715 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3717 * -v Display complete error logs
3718 * -x Display only pools with potential problems
3719 * -D Display dedup status (undocumented)
3720 * -T Display a timestamp in date(1) or Unix format
3722 * Describes the health status of all pools or some subset.
3725 zpool_do_status(int argc, char **argv)
3729 unsigned long interval = 0, count = 0;
3730 status_cbdata_t cb = { 0 };
3733 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3736 cb.cb_verbose = B_TRUE;
3739 cb.cb_explain = B_TRUE;
3742 cb.cb_dedup_stats = B_TRUE;
3745 get_timestamp_arg(*optarg);
3748 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3757 get_interval_count(&argc, argv, &interval, &count);
3760 cb.cb_allpools = B_TRUE;
3762 cb.cb_first = B_TRUE;
3765 if (timestamp_fmt != NODATE)
3766 print_timestamp(timestamp_fmt);
3768 ret = for_each_pool(argc, argv, B_TRUE, NULL,
3769 status_callback, &cb);
3771 if (argc == 0 && cb.cb_count == 0)
3772 (void) printf(gettext("no pools available\n"));
3773 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3774 (void) printf(gettext("all pools are healthy\n"));
3782 if (count != 0 && --count == 0)
3785 (void) sleep(interval);
3791 typedef struct upgrade_cbdata {
3796 uint64_t cb_version;
3801 upgrade_cb(zpool_handle_t *zhp, void *arg)
3803 upgrade_cbdata_t *cbp = arg;
3808 config = zpool_get_config(zhp, NULL);
3809 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3812 if (!cbp->cb_newer && version < SPA_VERSION) {
3814 if (cbp->cb_first) {
3815 (void) printf(gettext("The following pools are "
3816 "out of date, and can be upgraded. After "
3817 "being\nupgraded, these pools will no "
3818 "longer be accessible by older software "
3820 (void) printf(gettext("VER POOL\n"));
3821 (void) printf(gettext("--- ------------\n"));
3822 cbp->cb_first = B_FALSE;
3825 (void) printf("%2llu %s\n", (u_longlong_t)version,
3826 zpool_get_name(zhp));
3828 cbp->cb_first = B_FALSE;
3829 ret = zpool_upgrade(zhp, cbp->cb_version);
3831 (void) printf(gettext("Successfully upgraded "
3832 "'%s'\n\n"), zpool_get_name(zhp));
3835 } else if (cbp->cb_newer && version > SPA_VERSION) {
3836 assert(!cbp->cb_all);
3838 if (cbp->cb_first) {
3839 (void) printf(gettext("The following pools are "
3840 "formatted using a newer software version and\n"
3841 "cannot be accessed on the current system.\n\n"));
3842 (void) printf(gettext("VER POOL\n"));
3843 (void) printf(gettext("--- ------------\n"));
3844 cbp->cb_first = B_FALSE;
3847 (void) printf("%2llu %s\n", (u_longlong_t)version,
3848 zpool_get_name(zhp));
3857 upgrade_one(zpool_handle_t *zhp, void *data)
3859 upgrade_cbdata_t *cbp = data;
3860 uint64_t cur_version;
3863 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3864 (void) printf(gettext("'log' is now a reserved word\n"
3865 "Pool 'log' must be renamed using export and import"
3870 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3871 if (cur_version > cbp->cb_version) {
3872 (void) printf(gettext("Pool '%s' is already formatted "
3873 "using more current version '%llu'.\n"),
3874 zpool_get_name(zhp), cur_version);
3877 if (cur_version == cbp->cb_version) {
3878 (void) printf(gettext("Pool '%s' is already formatted "
3879 "using the current version.\n"), zpool_get_name(zhp));
3883 ret = zpool_upgrade(zhp, cbp->cb_version);
3886 (void) printf(gettext("Successfully upgraded '%s' "
3887 "from version %llu to version %llu\n\n"),
3888 zpool_get_name(zhp), (u_longlong_t)cur_version,
3889 (u_longlong_t)cbp->cb_version);
3898 * zpool upgrade [-V version] <-a | pool ...>
3900 * With no arguments, display downrev'd ZFS pool available for upgrade.
3901 * Individual pools can be upgraded by specifying the pool, and '-a' will
3902 * upgrade all pools.
3905 zpool_do_upgrade(int argc, char **argv)
3908 upgrade_cbdata_t cb = { 0 };
3910 boolean_t showversions = B_FALSE;
3915 while ((c = getopt(argc, argv, ":avV:")) != -1) {
3921 showversions = B_TRUE;
3924 cb.cb_version = strtoll(optarg, &end, 10);
3925 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3926 cb.cb_version < SPA_VERSION_1) {
3927 (void) fprintf(stderr,
3928 gettext("invalid version '%s'\n"), optarg);
3933 (void) fprintf(stderr, gettext("missing argument for "
3934 "'%c' option\n"), optopt);
3938 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3949 if (cb.cb_version == 0) {
3950 cb.cb_version = SPA_VERSION;
3951 } else if (!cb.cb_all && argc == 0) {
3952 (void) fprintf(stderr, gettext("-V option is "
3953 "incompatible with other arguments\n"));
3958 if (cb.cb_all || argc != 0) {
3959 (void) fprintf(stderr, gettext("-v option is "
3960 "incompatible with other arguments\n"));
3963 } else if (cb.cb_all) {
3965 (void) fprintf(stderr, gettext("-a option should not "
3966 "be used along with a pool name\n"));
3971 (void) printf(gettext("This system is currently running "
3972 "ZFS pool version %llu.\n\n"), SPA_VERSION);
3973 cb.cb_first = B_TRUE;
3975 (void) printf(gettext("The following versions are "
3977 (void) printf(gettext("VER DESCRIPTION\n"));
3978 (void) printf("--- -----------------------------------------"
3979 "---------------\n");
3980 (void) printf(gettext(" 1 Initial ZFS version\n"));
3981 (void) printf(gettext(" 2 Ditto blocks "
3982 "(replicated metadata)\n"));
3983 (void) printf(gettext(" 3 Hot spares and double parity "
3985 (void) printf(gettext(" 4 zpool history\n"));
3986 (void) printf(gettext(" 5 Compression using the gzip "
3988 (void) printf(gettext(" 6 bootfs pool property\n"));
3989 (void) printf(gettext(" 7 Separate intent log devices\n"));
3990 (void) printf(gettext(" 8 Delegated administration\n"));
3991 (void) printf(gettext(" 9 refquota and refreservation "
3993 (void) printf(gettext(" 10 Cache devices\n"));
3994 (void) printf(gettext(" 11 Improved scrub performance\n"));
3995 (void) printf(gettext(" 12 Snapshot properties\n"));
3996 (void) printf(gettext(" 13 snapused property\n"));
3997 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
3998 (void) printf(gettext(" 15 user/group space accounting\n"));
3999 (void) printf(gettext(" 16 stmf property support\n"));
4000 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4001 (void) printf(gettext(" 18 Snapshot user holds\n"));
4002 (void) printf(gettext(" 19 Log device removal\n"));
4003 (void) printf(gettext(" 20 Compression using zle "
4004 "(zero-length encoding)\n"));
4005 (void) printf(gettext(" 21 Deduplication\n"));
4006 (void) printf(gettext(" 22 Received properties\n"));
4007 (void) printf(gettext(" 23 Slim ZIL\n"));
4008 (void) printf(gettext(" 24 System attributes\n"));
4009 (void) printf(gettext(" 25 Improved scrub stats\n"));
4010 (void) printf(gettext(" 26 Improved snapshot deletion "
4012 (void) printf(gettext("\nFor more information on a particular "
4013 "version, including supported releases,\n"));
4014 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4015 } else if (argc == 0) {
4018 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4019 notfound = cb.cb_first;
4021 if (!cb.cb_all && ret == 0) {
4023 (void) printf("\n");
4024 cb.cb_first = B_TRUE;
4025 cb.cb_newer = B_TRUE;
4026 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4029 (void) printf("\n");
4035 (void) printf(gettext("All pools are formatted "
4036 "using this version.\n"));
4037 else if (!cb.cb_all)
4038 (void) printf(gettext("Use 'zpool upgrade -v' "
4039 "for a list of available versions and "
4040 "their associated\nfeatures.\n"));
4043 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4050 typedef struct hist_cbdata {
4057 * Print out the command history for a specific pool.
4060 get_history_one(zpool_handle_t *zhp, void *data)
4076 char internalstr[MAXPATHLEN];
4077 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4081 cb->first = B_FALSE;
4083 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4085 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4088 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4089 &records, &numrecords) == 0);
4090 for (i = 0; i < numrecords; i++) {
4091 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4095 /* is it an internal event or a standard event? */
4096 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4098 if (cb->internal == 0)
4101 if (nvlist_lookup_uint64(records[i],
4102 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4104 verify(nvlist_lookup_uint64(records[i],
4105 ZPOOL_HIST_TXG, &txg) == 0);
4106 verify(nvlist_lookup_string(records[i],
4107 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4108 if (ievent >= LOG_END)
4110 (void) snprintf(internalstr,
4111 sizeof (internalstr),
4112 "[internal %s txg:%lld] %s",
4113 zfs_history_event_names[ievent], txg,
4115 cmdstr = internalstr;
4118 (void) localtime_r(&tsec, &t);
4119 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4120 (void) printf("%s %s", tbuf, cmdstr);
4123 (void) printf("\n");
4126 (void) printf(" [");
4127 if (nvlist_lookup_uint64(records[i],
4128 ZPOOL_HIST_WHO, &who) == 0) {
4129 pwd = getpwuid((uid_t)who);
4131 (void) printf("user %s on",
4134 (void) printf("user %d on",
4137 (void) printf(gettext("no info]\n"));
4140 if (nvlist_lookup_string(records[i],
4141 ZPOOL_HIST_HOST, &hostname) == 0) {
4142 (void) printf(" %s", hostname);
4144 if (nvlist_lookup_string(records[i],
4145 ZPOOL_HIST_ZONE, &zonename) == 0) {
4146 (void) printf(":%s", zonename);
4150 (void) printf("\n");
4152 (void) printf("\n");
4159 * zpool history <pool>
4161 * Displays the history of commands that modified pools.
4166 zpool_do_history(int argc, char **argv)
4168 hist_cbdata_t cbdata = { 0 };
4172 cbdata.first = B_TRUE;
4174 while ((c = getopt(argc, argv, "li")) != -1) {
4180 cbdata.internal = 1;
4183 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4191 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4194 if (argc == 0 && cbdata.first == B_TRUE) {
4195 (void) printf(gettext("no pools available\n"));
4203 get_callback(zpool_handle_t *zhp, void *data)
4205 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4206 char value[MAXNAMELEN];
4207 zprop_source_t srctype;
4210 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4213 * Skip the special fake placeholder. This will also skip
4214 * over the name property when 'all' is specified.
4216 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4217 pl == cbp->cb_proplist)
4220 if (zpool_get_prop(zhp, pl->pl_prop,
4221 value, sizeof (value), &srctype) != 0)
4224 zprop_print_one_property(zpool_get_name(zhp), cbp,
4225 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4232 zpool_do_get(int argc, char **argv)
4234 zprop_get_cbdata_t cb = { 0 };
4235 zprop_list_t fake_name = { 0 };
4241 cb.cb_first = B_TRUE;
4242 cb.cb_sources = ZPROP_SRC_ALL;
4243 cb.cb_columns[0] = GET_COL_NAME;
4244 cb.cb_columns[1] = GET_COL_PROPERTY;
4245 cb.cb_columns[2] = GET_COL_VALUE;
4246 cb.cb_columns[3] = GET_COL_SOURCE;
4247 cb.cb_type = ZFS_TYPE_POOL;
4249 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4250 ZFS_TYPE_POOL) != 0)
4253 if (cb.cb_proplist != NULL) {
4254 fake_name.pl_prop = ZPOOL_PROP_NAME;
4255 fake_name.pl_width = strlen(gettext("NAME"));
4256 fake_name.pl_next = cb.cb_proplist;
4257 cb.cb_proplist = &fake_name;
4260 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4263 if (cb.cb_proplist == &fake_name)
4264 zprop_free_list(fake_name.pl_next);
4266 zprop_free_list(cb.cb_proplist);
4271 typedef struct set_cbdata {
4274 boolean_t cb_any_successful;
4278 set_callback(zpool_handle_t *zhp, void *data)
4281 set_cbdata_t *cb = (set_cbdata_t *)data;
4283 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4286 cb->cb_any_successful = B_TRUE;
4292 zpool_do_set(int argc, char **argv)
4294 set_cbdata_t cb = { 0 };
4297 if (argc > 1 && argv[1][0] == '-') {
4298 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4304 (void) fprintf(stderr, gettext("missing property=value "
4310 (void) fprintf(stderr, gettext("missing pool name\n"));
4315 (void) fprintf(stderr, gettext("too many pool names\n"));
4319 cb.cb_propname = argv[1];
4320 cb.cb_value = strchr(cb.cb_propname, '=');
4321 if (cb.cb_value == NULL) {
4322 (void) fprintf(stderr, gettext("missing value in "
4323 "property=value argument\n"));
4327 *(cb.cb_value) = '\0';
4330 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4337 find_command_idx(char *command, int *idx)
4341 for (i = 0; i < NCOMMAND; i++) {
4342 if (command_table[i].name == NULL)
4345 if (strcmp(command, command_table[i].name) == 0) {
4354 main(int argc, char **argv)
4360 (void) setlocale(LC_ALL, "");
4361 (void) textdomain(TEXT_DOMAIN);
4363 if ((g_zfs = libzfs_init()) == NULL) {
4364 (void) fprintf(stderr, gettext("internal error: failed to "
4365 "initialize ZFS library\n"));
4369 libzfs_print_on_error(g_zfs, B_TRUE);
4374 * Make sure the user has specified some command.
4377 (void) fprintf(stderr, gettext("missing command\n"));
4386 if (strcmp(cmdname, "-?") == 0)
4389 zpool_set_history_str("zpool", argc, argv, history_str);
4390 verify(zpool_stage_history(g_zfs, history_str) == 0);
4393 * Run the appropriate command.
4395 if (find_command_idx(cmdname, &i) == 0) {
4396 current_command = &command_table[i];
4397 ret = command_table[i].func(argc - 1, argv + 1);
4398 } else if (strchr(cmdname, '=')) {
4399 verify(find_command_idx("set", &i) == 0);
4400 current_command = &command_table[i];
4401 ret = command_table[i].func(argc, argv);
4402 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4404 * 'freeze' is a vile debugging abomination, so we treat
4408 int fd = open(ZFS_DEV, O_RDWR);
4409 (void) strcpy((void *)buf, argv[2]);
4410 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4412 (void) fprintf(stderr, gettext("unrecognized "
4413 "command '%s'\n"), cmdname);
4420 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4421 * for the purposes of running ::findleaks.
4423 if (getenv("ZFS_ABORT") != NULL) {
4424 (void) printf("dumping core by request\n");