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>
45 #include <sys/fm/util.h>
46 #include <sys/fm/protocol.h>
50 #include "zpool_util.h"
51 #include "zfs_comutil.h"
53 #include "statcommon.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 **);
72 static int zpool_do_split(int, char **);
74 static int zpool_do_scrub(int, char **);
76 static int zpool_do_import(int, char **);
77 static int zpool_do_export(int, char **);
79 static int zpool_do_upgrade(int, char **);
81 static int zpool_do_history(int, char **);
82 static int zpool_do_events(int, char **);
84 static int zpool_do_get(int, char **);
85 static int zpool_do_set(int, char **);
88 * These libumem hooks provide a reasonable set of defaults for the allocator's
89 * debugging facilities.
94 _umem_debug_init(void)
96 return ("default,verbose"); /* $UMEM_DEBUG setting */
100 _umem_logging_init(void)
102 return ("fail,contents"); /* $UMEM_LOGGING setting */
132 typedef struct zpool_command {
134 int (*func)(int, char **);
139 * Master command table. Each ZFS command has a name, associated function, and
140 * usage message. The usage messages need to be internationalized, so we have
141 * to have a function to return the usage message based on a command index.
143 * These commands are organized according to how they are displayed in the usage
144 * message. An empty command (one with a NULL name) indicates an empty line in
145 * the generic usage message.
147 static zpool_command_t command_table[] = {
148 { "create", zpool_do_create, HELP_CREATE },
149 { "destroy", zpool_do_destroy, HELP_DESTROY },
151 { "add", zpool_do_add, HELP_ADD },
152 { "remove", zpool_do_remove, HELP_REMOVE },
154 { "list", zpool_do_list, HELP_LIST },
155 { "iostat", zpool_do_iostat, HELP_IOSTAT },
156 { "status", zpool_do_status, HELP_STATUS },
158 { "online", zpool_do_online, HELP_ONLINE },
159 { "offline", zpool_do_offline, HELP_OFFLINE },
160 { "clear", zpool_do_clear, HELP_CLEAR },
162 { "attach", zpool_do_attach, HELP_ATTACH },
163 { "detach", zpool_do_detach, HELP_DETACH },
164 { "replace", zpool_do_replace, HELP_REPLACE },
165 { "split", zpool_do_split, HELP_SPLIT },
167 { "scrub", zpool_do_scrub, HELP_SCRUB },
169 { "import", zpool_do_import, HELP_IMPORT },
170 { "export", zpool_do_export, HELP_EXPORT },
171 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
173 { "history", zpool_do_history, HELP_HISTORY },
174 { "events", zpool_do_events, HELP_EVENTS },
176 { "get", zpool_do_get, HELP_GET },
177 { "set", zpool_do_set, HELP_SET },
180 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
182 zpool_command_t *current_command;
183 static char history_str[HIS_MAX_RECORD_LEN];
185 static uint_t timestamp_fmt = NODATE;
188 get_usage(zpool_help_t idx) {
191 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
193 return (gettext("\tattach [-f] <pool> <device> "
196 return (gettext("\tclear [-nF] <pool> [device]\n"));
198 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
199 "\t [-O file-system-property=value] ... \n"
200 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
202 return (gettext("\tdestroy [-f] <pool>\n"));
204 return (gettext("\tdetach <pool> <device>\n"));
206 return (gettext("\texport [-f] <pool> ...\n"));
208 return (gettext("\thistory [-il] [<pool>] ...\n"));
210 return (gettext("\timport [-d dir] [-D]\n"
211 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
212 "\timport [-o mntopts] [-o property=value] ... \n"
213 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
214 "[-R root] [-F [-n]] -a\n"
215 "\timport [-o mntopts] [-o property=value] ... \n"
216 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
217 "[-R root] [-F [-n]]\n"
218 "\t <pool | id> [newpool]\n"));
220 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
223 return (gettext("\tlist [-H] [-o property[,...]] "
224 "[-T d|u] [pool] ... [interval [count]]\n"));
226 return (gettext("\toffline [-t] <pool> <device> ...\n"));
228 return (gettext("\tonline <pool> <device> ...\n"));
230 return (gettext("\treplace [-f] <pool> <device> "
233 return (gettext("\tremove <pool> <device> ...\n"));
235 return (gettext("\tscrub [-s] <pool> ...\n"));
237 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
240 return (gettext("\tupgrade\n"
242 "\tupgrade [-V version] <-a | pool ...>\n"));
244 return (gettext("\tevents [-vHfc]\n"));
246 return (gettext("\tget <\"all\" | property[,...]> "
249 return (gettext("\tset <property=value> <pool> \n"));
251 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
252 "\t [-o property=value] <pool> <newpool> "
253 "[<device> ...]\n"));
262 * Callback routine that will print out a pool property value.
265 print_prop_cb(int prop, void *cb)
269 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
271 if (zpool_prop_readonly(prop))
272 (void) fprintf(fp, " NO ");
274 (void) fprintf(fp, " YES ");
276 if (zpool_prop_values(prop) == NULL)
277 (void) fprintf(fp, "-\n");
279 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
285 * Display usage message. If we're inside a command, display only the usage for
286 * that command. Otherwise, iterate over the entire command table and display
287 * a complete usage message.
290 usage(boolean_t requested)
292 FILE *fp = requested ? stdout : stderr;
294 if (current_command == NULL) {
297 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
299 gettext("where 'command' is one of the following:\n\n"));
301 for (i = 0; i < NCOMMAND; i++) {
302 if (command_table[i].name == NULL)
303 (void) fprintf(fp, "\n");
305 (void) fprintf(fp, "%s",
306 get_usage(command_table[i].usage));
309 (void) fprintf(fp, gettext("usage:\n"));
310 (void) fprintf(fp, "%s", get_usage(current_command->usage));
313 if (current_command != NULL &&
314 ((strcmp(current_command->name, "set") == 0) ||
315 (strcmp(current_command->name, "get") == 0) ||
316 (strcmp(current_command->name, "list") == 0))) {
319 gettext("\nthe following properties are supported:\n"));
321 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
322 "PROPERTY", "EDIT", "VALUES");
324 /* Iterate over all properties */
325 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
330 * See comments at end of main().
332 if (getenv("ZFS_ABORT") != NULL) {
333 (void) printf("dumping core by request\n");
337 exit(requested ? 0 : 2);
341 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
342 boolean_t print_logs)
349 (void) printf("\t%*s%s\n", indent, "", name);
351 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
352 &child, &children) != 0)
355 for (c = 0; c < children; c++) {
356 uint64_t is_log = B_FALSE;
358 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
360 if ((is_log && !print_logs) || (!is_log && print_logs))
363 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
364 print_vdev_tree(zhp, vname, child[c], indent + 2,
371 * Add a property pair (name, string-value) into a property nvlist.
374 add_prop_list(const char *propname, char *propval, nvlist_t **props,
377 zpool_prop_t prop = ZPROP_INVAL;
383 if (*props == NULL &&
384 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
385 (void) fprintf(stderr,
386 gettext("internal error: out of memory\n"));
393 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
394 (void) fprintf(stderr, gettext("property '%s' is "
395 "not a valid pool property\n"), propname);
398 normnm = zpool_prop_to_name(prop);
400 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
401 normnm = zfs_prop_to_name(fprop);
407 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
408 prop != ZPOOL_PROP_CACHEFILE) {
409 (void) fprintf(stderr, gettext("property '%s' "
410 "specified multiple times\n"), propname);
414 if (nvlist_add_string(proplist, normnm, propval) != 0) {
415 (void) fprintf(stderr, gettext("internal "
416 "error: out of memory\n"));
424 * zpool add [-fn] <pool> <vdev> ...
426 * -f Force addition of devices, even if they appear in use
427 * -n Do not add the devices, but display the resulting layout if
428 * they were to be added.
430 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
431 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
435 zpool_do_add(int argc, char **argv)
437 boolean_t force = B_FALSE;
438 boolean_t dryrun = B_FALSE;
447 while ((c = getopt(argc, argv, "fn")) != -1) {
456 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
465 /* get pool name and check number of arguments */
467 (void) fprintf(stderr, gettext("missing pool name argument\n"));
471 (void) fprintf(stderr, gettext("missing vdev specification\n"));
480 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
483 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
484 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
490 /* pass off to get_vdev_spec for processing */
491 nvroot = make_root_vdev(zhp, NULL, force, !force, B_FALSE, dryrun,
493 if (nvroot == NULL) {
499 nvlist_t *poolnvroot;
501 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
504 (void) printf(gettext("would update '%s' to the following "
505 "configuration:\n"), zpool_get_name(zhp));
507 /* print original main pool and new tree */
508 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
509 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
511 /* Do the same for the logs */
512 if (num_logs(poolnvroot) > 0) {
513 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
514 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
515 } else if (num_logs(nvroot) > 0) {
516 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
521 ret = (zpool_add(zhp, nvroot) != 0);
531 * zpool remove <pool> <vdev> ...
533 * Removes the given vdev from the pool. Currently, this supports removing
534 * spares, cache, and log devices from the pool.
537 zpool_do_remove(int argc, char **argv)
546 /* get pool name and check number of arguments */
548 (void) fprintf(stderr, gettext("missing pool name argument\n"));
552 (void) fprintf(stderr, gettext("missing device\n"));
558 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
561 for (i = 1; i < argc; i++) {
562 if (zpool_vdev_remove(zhp, argv[i]) != 0)
570 * zpool create [-fn] [-o property=value] ...
571 * [-O file-system-property=value] ...
572 * [-R root] [-m mountpoint] <pool> <dev> ...
574 * -f Force creation, even if devices appear in use
575 * -n Do not create the pool, but display the resulting layout if it
576 * were to be created.
577 * -R Create a pool under an alternate root
578 * -m Set default mountpoint for the root dataset. By default it's
580 * -o Set property=value.
581 * -O Set fsproperty=value in the pool's root file system
583 * Creates the named pool according to the given vdev specification. The
584 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
585 * we get the nvlist back from get_vdev_spec(), we either print out the contents
586 * (if '-n' was specified), or pass it to libzfs to do the creation.
589 zpool_do_create(int argc, char **argv)
591 boolean_t force = B_FALSE;
592 boolean_t dryrun = B_FALSE;
594 nvlist_t *nvroot = NULL;
597 char *altroot = NULL;
598 char *mountpoint = NULL;
599 nvlist_t *fsprops = NULL;
600 nvlist_t *props = NULL;
604 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
614 if (add_prop_list(zpool_prop_to_name(
615 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
617 if (nvlist_lookup_string(props,
618 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
621 if (add_prop_list(zpool_prop_to_name(
622 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
629 if ((propval = strchr(optarg, '=')) == NULL) {
630 (void) fprintf(stderr, gettext("missing "
631 "'=' for -o option\n"));
637 if (add_prop_list(optarg, propval, &props, B_TRUE))
641 if ((propval = strchr(optarg, '=')) == NULL) {
642 (void) fprintf(stderr, gettext("missing "
643 "'=' for -O option\n"));
649 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
653 (void) fprintf(stderr, gettext("missing argument for "
654 "'%c' option\n"), optopt);
657 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
666 /* get pool name and check number of arguments */
668 (void) fprintf(stderr, gettext("missing pool name argument\n"));
672 (void) fprintf(stderr, gettext("missing vdev specification\n"));
679 * As a special case, check for use of '/' in the name, and direct the
680 * user to use 'zfs create' instead.
682 if (strchr(poolname, '/') != NULL) {
683 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
684 "character '/' in pool name\n"), poolname);
685 (void) fprintf(stderr, gettext("use 'zfs create' to "
686 "create a dataset\n"));
690 /* pass off to get_vdev_spec for bulk processing */
691 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
696 /* make_root_vdev() allows 0 toplevel children if there are spares */
697 if (!zfs_allocatable_devs(nvroot)) {
698 (void) fprintf(stderr, gettext("invalid vdev "
699 "specification: at least one toplevel vdev must be "
705 if (altroot != NULL && altroot[0] != '/') {
706 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
707 "must be an absolute path\n"), altroot);
712 * Check the validity of the mountpoint and direct the user to use the
713 * '-m' mountpoint option if it looks like its in use.
715 if (mountpoint == NULL ||
716 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
717 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
718 char buf[MAXPATHLEN];
721 if (mountpoint && mountpoint[0] != '/') {
722 (void) fprintf(stderr, gettext("invalid mountpoint "
723 "'%s': must be an absolute path, 'legacy', or "
724 "'none'\n"), mountpoint);
728 if (mountpoint == NULL) {
730 (void) snprintf(buf, sizeof (buf), "%s/%s",
733 (void) snprintf(buf, sizeof (buf), "/%s",
737 (void) snprintf(buf, sizeof (buf), "%s%s",
738 altroot, mountpoint);
740 (void) snprintf(buf, sizeof (buf), "%s",
744 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
745 (void) fprintf(stderr, gettext("mountpoint '%s' : "
746 "%s\n"), buf, strerror(errno));
747 (void) fprintf(stderr, gettext("use '-m' "
748 "option to provide a different default\n"));
753 while (count < 3 && readdir(dirp) != NULL)
755 (void) closedir(dirp);
758 (void) fprintf(stderr, gettext("mountpoint "
759 "'%s' exists and is not empty\n"), buf);
760 (void) fprintf(stderr, gettext("use '-m' "
761 "option to provide a "
762 "different default\n"));
770 * For a dry run invocation, print out a basic message and run
771 * through all the vdevs in the list and print out in an
772 * appropriate hierarchy.
774 (void) printf(gettext("would create '%s' with the "
775 "following layout:\n\n"), poolname);
777 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
778 if (num_logs(nvroot) > 0)
779 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
784 * Hand off to libzfs.
786 if (zpool_create(g_zfs, poolname,
787 nvroot, props, fsprops) == 0) {
788 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
789 ZFS_TYPE_FILESYSTEM);
791 if (mountpoint != NULL)
792 verify(zfs_prop_set(pool,
794 ZFS_PROP_MOUNTPOINT),
796 if (zfs_mount(pool, NULL, 0) == 0)
797 ret = zfs_shareall(pool);
800 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
801 (void) fprintf(stderr, gettext("pool name may have "
808 nvlist_free(fsprops);
812 nvlist_free(fsprops);
819 * zpool destroy <pool>
821 * -f Forcefully unmount any datasets
823 * Destroy the given pool. Automatically unmounts any datasets in the pool.
826 zpool_do_destroy(int argc, char **argv)
828 boolean_t force = B_FALSE;
835 while ((c = getopt(argc, argv, "f")) != -1) {
841 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
850 /* check arguments */
852 (void) fprintf(stderr, gettext("missing pool argument\n"));
856 (void) fprintf(stderr, gettext("too many arguments\n"));
862 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
864 * As a special case, check for use of '/' in the name, and
865 * direct the user to use 'zfs destroy' instead.
867 if (strchr(pool, '/') != NULL)
868 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
869 "destroy a dataset\n"));
873 if (zpool_disable_datasets(zhp, force) != 0) {
874 (void) fprintf(stderr, gettext("could not destroy '%s': "
875 "could not unmount datasets\n"), zpool_get_name(zhp));
879 ret = (zpool_destroy(zhp) != 0);
887 * zpool export [-f] <pool> ...
889 * -f Forcefully unmount datasets
891 * Export the given pools. By default, the command will attempt to cleanly
892 * unmount any active datasets within the pool. If the '-f' flag is specified,
893 * then the datasets will be forcefully unmounted.
896 zpool_do_export(int argc, char **argv)
898 boolean_t force = B_FALSE;
899 boolean_t hardforce = B_FALSE;
906 while ((c = getopt(argc, argv, "fF")) != -1) {
915 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
924 /* check arguments */
926 (void) fprintf(stderr, gettext("missing pool argument\n"));
931 for (i = 0; i < argc; i++) {
932 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
937 if (zpool_disable_datasets(zhp, force) != 0) {
944 if (zpool_export_force(zhp) != 0)
946 } else if (zpool_export(zhp, force) != 0) {
957 * Given a vdev configuration, determine the maximum width needed for the device
961 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
963 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
968 if (strlen(name) + depth > max)
969 max = strlen(name) + depth;
973 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
974 &child, &children) == 0) {
975 for (c = 0; c < children; c++)
976 if ((ret = max_width(zhp, child[c], depth + 2,
981 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
982 &child, &children) == 0) {
983 for (c = 0; c < children; c++)
984 if ((ret = max_width(zhp, child[c], depth + 2,
989 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
990 &child, &children) == 0) {
991 for (c = 0; c < children; c++)
992 if ((ret = max_width(zhp, child[c], depth + 2,
1001 typedef struct spare_cbdata {
1003 zpool_handle_t *cb_zhp;
1007 find_vdev(nvlist_t *nv, uint64_t search)
1013 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1017 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1018 &child, &children) == 0) {
1019 for (c = 0; c < children; c++)
1020 if (find_vdev(child[c], search))
1028 find_spare(zpool_handle_t *zhp, void *data)
1030 spare_cbdata_t *cbp = data;
1031 nvlist_t *config, *nvroot;
1033 config = zpool_get_config(zhp, NULL);
1034 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1037 if (find_vdev(nvroot, cbp->cb_guid)) {
1047 * Print out configuration state as requested by status_callback.
1050 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1051 int namewidth, int depth, boolean_t isspare)
1055 pool_scan_stat_t *ps = NULL;
1057 char rbuf[6], wbuf[6], cbuf[6];
1059 uint64_t notpresent;
1063 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1064 &child, &children) != 0)
1067 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1068 (uint64_t **)&vs, &c) == 0);
1070 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1073 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1076 if (vs->vs_aux == VDEV_AUX_SPARED)
1078 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1082 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1086 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1087 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1088 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1089 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1092 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1093 ¬present) == 0) {
1095 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1096 (void) printf(" was %s", path);
1097 } else if (vs->vs_aux != 0) {
1100 switch (vs->vs_aux) {
1101 case VDEV_AUX_OPEN_FAILED:
1102 (void) printf(gettext("cannot open"));
1105 case VDEV_AUX_BAD_GUID_SUM:
1106 (void) printf(gettext("missing device"));
1109 case VDEV_AUX_NO_REPLICAS:
1110 (void) printf(gettext("insufficient replicas"));
1113 case VDEV_AUX_VERSION_NEWER:
1114 (void) printf(gettext("newer version"));
1117 case VDEV_AUX_SPARED:
1118 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1120 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1121 if (strcmp(zpool_get_name(cb.cb_zhp),
1122 zpool_get_name(zhp)) == 0)
1123 (void) printf(gettext("currently in "
1126 (void) printf(gettext("in use by "
1128 zpool_get_name(cb.cb_zhp));
1129 zpool_close(cb.cb_zhp);
1131 (void) printf(gettext("currently in use"));
1135 case VDEV_AUX_ERR_EXCEEDED:
1136 (void) printf(gettext("too many errors"));
1139 case VDEV_AUX_IO_FAILURE:
1140 (void) printf(gettext("experienced I/O failures"));
1143 case VDEV_AUX_BAD_LOG:
1144 (void) printf(gettext("bad intent log"));
1147 case VDEV_AUX_EXTERNAL:
1148 (void) printf(gettext("external device fault"));
1151 case VDEV_AUX_SPLIT_POOL:
1152 (void) printf(gettext("split into new pool"));
1156 (void) printf(gettext("corrupted data"));
1161 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1162 (uint64_t **)&ps, &c);
1164 if (ps && ps->pss_state == DSS_SCANNING &&
1165 vs->vs_scan_processed != 0 && children == 0) {
1166 (void) printf(gettext(" (%s)"),
1167 (ps->pss_func == POOL_SCAN_RESILVER) ?
1168 "resilvering" : "repairing");
1171 (void) printf("\n");
1173 for (c = 0; c < children; c++) {
1174 uint64_t islog = B_FALSE, ishole = B_FALSE;
1176 /* Don't print logs or holes here */
1177 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1179 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1181 if (islog || ishole)
1183 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1184 print_status_config(zhp, vname, child[c],
1185 namewidth, depth + 2, isspare);
1192 * Print the configuration of an exported pool. Iterate over all vdevs in the
1193 * pool, printing out the name and status for each one.
1196 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1203 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1204 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1205 strcmp(type, VDEV_TYPE_HOLE) == 0)
1208 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1209 (uint64_t **)&vs, &c) == 0);
1211 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1212 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1214 if (vs->vs_aux != 0) {
1217 switch (vs->vs_aux) {
1218 case VDEV_AUX_OPEN_FAILED:
1219 (void) printf(gettext("cannot open"));
1222 case VDEV_AUX_BAD_GUID_SUM:
1223 (void) printf(gettext("missing device"));
1226 case VDEV_AUX_NO_REPLICAS:
1227 (void) printf(gettext("insufficient replicas"));
1230 case VDEV_AUX_VERSION_NEWER:
1231 (void) printf(gettext("newer version"));
1234 case VDEV_AUX_ERR_EXCEEDED:
1235 (void) printf(gettext("too many errors"));
1239 (void) printf(gettext("corrupted data"));
1243 (void) printf("\n");
1245 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1246 &child, &children) != 0)
1249 for (c = 0; c < children; c++) {
1250 uint64_t is_log = B_FALSE;
1252 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1257 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1258 print_import_config(vname, child[c], namewidth, depth + 2);
1262 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1263 &child, &children) == 0) {
1264 (void) printf(gettext("\tcache\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);
1272 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1273 &child, &children) == 0) {
1274 (void) printf(gettext("\tspares\n"));
1275 for (c = 0; c < children; c++) {
1276 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1277 (void) printf("\t %s\n", vname);
1285 * Logs are recorded as top level vdevs in the main pool child array
1286 * but with "is_log" set to 1. We use either print_status_config() or
1287 * print_import_config() to print the top level logs then any log
1288 * children (eg mirrored slogs) are printed recursively - which
1289 * works because only the top level vdev is marked "is_log"
1292 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1297 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1301 (void) printf(gettext("\tlogs\n"));
1303 for (c = 0; c < children; c++) {
1304 uint64_t is_log = B_FALSE;
1307 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1311 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1313 print_status_config(zhp, name, child[c], namewidth,
1316 print_import_config(name, child[c], namewidth, 2);
1322 * Display the status for the given pool.
1325 show_import(nvlist_t *config)
1327 uint64_t pool_state;
1338 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1340 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1342 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1344 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1347 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1348 (uint64_t **)&vs, &vsc) == 0);
1349 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1351 reason = zpool_import_status(config, &msgid);
1353 (void) printf(gettext(" pool: %s\n"), name);
1354 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1355 (void) printf(gettext(" state: %s"), health);
1356 if (pool_state == POOL_STATE_DESTROYED)
1357 (void) printf(gettext(" (DESTROYED)"));
1358 (void) printf("\n");
1361 case ZPOOL_STATUS_MISSING_DEV_R:
1362 case ZPOOL_STATUS_MISSING_DEV_NR:
1363 case ZPOOL_STATUS_BAD_GUID_SUM:
1364 (void) printf(gettext("status: One or more devices are missing "
1365 "from the system.\n"));
1368 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1369 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1370 (void) printf(gettext("status: One or more devices contains "
1371 "corrupted data.\n"));
1374 case ZPOOL_STATUS_CORRUPT_DATA:
1375 (void) printf(gettext("status: The pool data is corrupted.\n"));
1378 case ZPOOL_STATUS_OFFLINE_DEV:
1379 (void) printf(gettext("status: One or more devices "
1380 "are offlined.\n"));
1383 case ZPOOL_STATUS_CORRUPT_POOL:
1384 (void) printf(gettext("status: The pool metadata is "
1388 case ZPOOL_STATUS_VERSION_OLDER:
1389 (void) printf(gettext("status: The pool is formatted using an "
1390 "older on-disk version.\n"));
1393 case ZPOOL_STATUS_VERSION_NEWER:
1394 (void) printf(gettext("status: The pool is formatted using an "
1395 "incompatible version.\n"));
1398 case ZPOOL_STATUS_HOSTID_MISMATCH:
1399 (void) printf(gettext("status: The pool was last accessed by "
1400 "another system.\n"));
1403 case ZPOOL_STATUS_FAULTED_DEV_R:
1404 case ZPOOL_STATUS_FAULTED_DEV_NR:
1405 (void) printf(gettext("status: One or more devices are "
1409 case ZPOOL_STATUS_BAD_LOG:
1410 (void) printf(gettext("status: An intent log record cannot be "
1414 case ZPOOL_STATUS_RESILVERING:
1415 (void) printf(gettext("status: One or more devices were being "
1421 * No other status can be seen when importing pools.
1423 assert(reason == ZPOOL_STATUS_OK);
1427 * Print out an action according to the overall state of the pool.
1429 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1430 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1431 (void) printf(gettext("action: The pool can be "
1432 "imported using its name or numeric identifier, "
1433 "though\n\tsome features will not be available "
1434 "without an explicit 'zpool upgrade'.\n"));
1435 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1436 (void) printf(gettext("action: The pool can be "
1437 "imported using its name or numeric "
1438 "identifier and\n\tthe '-f' flag.\n"));
1440 (void) printf(gettext("action: The pool can be "
1441 "imported using its name or numeric "
1443 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1444 (void) printf(gettext("action: The pool can be imported "
1445 "despite missing or damaged devices. The\n\tfault "
1446 "tolerance of the pool may be compromised if imported.\n"));
1449 case ZPOOL_STATUS_VERSION_NEWER:
1450 (void) printf(gettext("action: The pool cannot be "
1451 "imported. Access the pool on a system running "
1452 "newer\n\tsoftware, or recreate the pool from "
1455 case ZPOOL_STATUS_MISSING_DEV_R:
1456 case ZPOOL_STATUS_MISSING_DEV_NR:
1457 case ZPOOL_STATUS_BAD_GUID_SUM:
1458 (void) printf(gettext("action: The pool cannot be "
1459 "imported. Attach the missing\n\tdevices and try "
1463 (void) printf(gettext("action: The pool cannot be "
1464 "imported due to damaged devices or data.\n"));
1469 * If the state is "closed" or "can't open", and the aux state
1470 * is "corrupt data":
1472 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1473 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1474 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1475 if (pool_state == POOL_STATE_DESTROYED)
1476 (void) printf(gettext("\tThe pool was destroyed, "
1477 "but can be imported using the '-Df' flags.\n"));
1478 else if (pool_state != POOL_STATE_EXPORTED)
1479 (void) printf(gettext("\tThe pool may be active on "
1480 "another system, but can be imported using\n\t"
1481 "the '-f' flag.\n"));
1485 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1488 (void) printf(gettext("config:\n\n"));
1490 namewidth = max_width(NULL, nvroot, 0, 0);
1494 print_import_config(name, nvroot, namewidth, 0);
1495 if (num_logs(nvroot) > 0)
1496 print_logs(NULL, nvroot, namewidth, B_FALSE);
1498 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1499 (void) printf(gettext("\n\tAdditional devices are known to "
1500 "be part of this pool, though their\n\texact "
1501 "configuration cannot be determined.\n"));
1506 * Perform the import for the given configuration. This passes the heavy
1507 * lifting off to zpool_import_props(), and then mounts the datasets contained
1511 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1512 nvlist_t *props, int flags)
1514 zpool_handle_t *zhp;
1519 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1522 verify(nvlist_lookup_uint64(config,
1523 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1524 verify(nvlist_lookup_uint64(config,
1525 ZPOOL_CONFIG_VERSION, &version) == 0);
1526 if (version > SPA_VERSION) {
1527 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1528 "is formatted using a newer ZFS version\n"), name);
1530 } else if (state != POOL_STATE_EXPORTED &&
1531 !(flags & ZFS_IMPORT_ANY_HOST)) {
1534 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1536 unsigned long system_hostid = gethostid() & 0xffffffff;
1538 if ((unsigned long)hostid != system_hostid) {
1543 verify(nvlist_lookup_string(config,
1544 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1545 verify(nvlist_lookup_uint64(config,
1546 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1548 (void) fprintf(stderr, gettext("cannot import "
1549 "'%s': pool may be in use from other "
1550 "system, it was last accessed by %s "
1551 "(hostid: 0x%lx) on %s"), name, hostname,
1552 (unsigned long)hostid,
1553 asctime(localtime(&t)));
1554 (void) fprintf(stderr, gettext("use '-f' to "
1555 "import anyway\n"));
1559 (void) fprintf(stderr, gettext("cannot import '%s': "
1560 "pool may be in use from other system\n"), name);
1561 (void) fprintf(stderr, gettext("use '-f' to import "
1567 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1570 if (newname != NULL)
1571 name = (char *)newname;
1573 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1576 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1577 !(flags & ZFS_IMPORT_ONLY) &&
1578 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1588 * zpool import [-d dir] [-D]
1589 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1590 * [-d dir | -c cachefile] [-f] -a
1591 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1592 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1594 * -c Read pool information from a cachefile instead of searching
1597 * -d Scan in a specific directory, other than /dev/. More than
1598 * one directory can be specified using multiple '-d' options.
1600 * -D Scan for previously destroyed pools or import all or only
1601 * specified destroyed pools.
1603 * -R Temporarily import the pool, with all mountpoints relative to
1604 * the given root. The pool will remain exported when the machine
1607 * -V Import even in the presence of faulted vdevs. This is an
1608 * intentionally undocumented option for testing purposes, and
1609 * treats the pool configuration as complete, leaving any bad
1610 * vdevs in the FAULTED state. In other words, it does verbatim
1613 * -f Force import, even if it appears that the pool is active.
1615 * -F Attempt rewind if necessary.
1617 * -n See if rewind would work, but don't actually rewind.
1619 * -N Import the pool but don't mount datasets.
1621 * -T Specify a starting txg to use for import. This option is
1622 * intentionally undocumented option for testing purposes.
1624 * -a Import all pools found.
1626 * -o Set property=value and/or temporary mount options (without '=').
1628 * The import command scans for pools to import, and import pools based on pool
1629 * name and GUID. The pool can also be renamed as part of the import process.
1632 zpool_do_import(int argc, char **argv)
1634 char **searchdirs = NULL;
1638 nvlist_t *pools = NULL;
1639 boolean_t do_all = B_FALSE;
1640 boolean_t do_destroyed = B_FALSE;
1641 char *mntopts = NULL;
1644 uint64_t searchguid = 0;
1645 char *searchname = NULL;
1647 nvlist_t *found_config;
1648 nvlist_t *policy = NULL;
1649 nvlist_t *props = NULL;
1651 int flags = ZFS_IMPORT_NORMAL;
1652 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1653 boolean_t dryrun = B_FALSE;
1654 boolean_t do_rewind = B_FALSE;
1655 boolean_t xtreme_rewind = B_FALSE;
1656 uint64_t pool_state, txg = -1ULL;
1657 char *cachefile = NULL;
1658 importargs_t idata = { 0 };
1662 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1671 if (searchdirs == NULL) {
1672 searchdirs = safe_malloc(sizeof (char *));
1674 char **tmp = safe_malloc((nsearch + 1) *
1676 bcopy(searchdirs, tmp, nsearch *
1681 searchdirs[nsearch++] = optarg;
1684 do_destroyed = B_TRUE;
1687 flags |= ZFS_IMPORT_ANY_HOST;
1693 flags |= ZFS_IMPORT_MISSING_LOG;
1699 flags |= ZFS_IMPORT_ONLY;
1702 if ((propval = strchr(optarg, '=')) != NULL) {
1705 if (add_prop_list(optarg, propval,
1713 if (add_prop_list(zpool_prop_to_name(
1714 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1716 if (nvlist_lookup_string(props,
1717 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1720 if (add_prop_list(zpool_prop_to_name(
1721 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1726 txg = strtoull(optarg, &endptr, 10);
1727 if (errno != 0 || *endptr != '\0') {
1728 (void) fprintf(stderr,
1729 gettext("invalid txg value\n"));
1732 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1735 flags |= ZFS_IMPORT_VERBATIM;
1738 xtreme_rewind = B_TRUE;
1741 (void) fprintf(stderr, gettext("missing argument for "
1742 "'%c' option\n"), optopt);
1746 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1755 if (cachefile && nsearch != 0) {
1756 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1760 if ((dryrun || xtreme_rewind) && !do_rewind) {
1761 (void) fprintf(stderr,
1762 gettext("-n or -X only meaningful with -F\n"));
1766 rewind_policy = ZPOOL_TRY_REWIND;
1768 rewind_policy = ZPOOL_DO_REWIND;
1770 rewind_policy |= ZPOOL_EXTREME_REWIND;
1772 /* In the future, we can capture further policy and include it here */
1773 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1774 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1775 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1778 /* check argument count */
1781 (void) fprintf(stderr, gettext("too many arguments\n"));
1786 (void) fprintf(stderr, gettext("too many arguments\n"));
1791 * Check for the SYS_CONFIG privilege. We do this explicitly
1792 * here because otherwise any attempt to discover pools will
1795 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1796 (void) fprintf(stderr, gettext("cannot "
1797 "discover pools: permission denied\n"));
1798 if (searchdirs != NULL)
1801 nvlist_free(policy);
1807 * Depending on the arguments given, we do one of the following:
1809 * <none> Iterate through all pools and display information about
1812 * -a Iterate through all pools and try to import each one.
1814 * <id> Find the pool that corresponds to the given GUID/pool
1815 * name and import that one.
1817 * -D Above options applies only to destroyed pools.
1823 searchguid = strtoull(argv[0], &endptr, 10);
1824 if (errno != 0 || *endptr != '\0')
1825 searchname = argv[0];
1826 found_config = NULL;
1829 * User specified a name or guid. Ensure it's unique.
1831 idata.unique = B_TRUE;
1835 idata.path = searchdirs;
1836 idata.paths = nsearch;
1837 idata.poolname = searchname;
1838 idata.guid = searchguid;
1839 idata.cachefile = cachefile;
1841 pools = zpool_search_import(g_zfs, &idata);
1843 if (pools != NULL && idata.exists &&
1844 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1845 (void) fprintf(stderr, gettext("cannot import '%s': "
1846 "a pool with that name already exists\n"),
1848 (void) fprintf(stderr, gettext("use the form '%s "
1849 "<pool | id> <newpool>' to give it a new name\n"),
1852 } else if (pools == NULL && idata.exists) {
1853 (void) fprintf(stderr, gettext("cannot import '%s': "
1854 "a pool with that name is already created/imported,\n"),
1856 (void) fprintf(stderr, gettext("and no additional pools "
1857 "with that name were found\n"));
1859 } else if (pools == NULL) {
1861 (void) fprintf(stderr, gettext("cannot import '%s': "
1862 "no such pool available\n"), argv[0]);
1868 if (searchdirs != NULL)
1870 nvlist_free(policy);
1875 * At this point we have a list of import candidate configs. Even if
1876 * we were searching by pool name or guid, we still need to
1877 * post-process the list to deal with pool state and possible
1883 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1885 verify(nvpair_value_nvlist(elem, &config) == 0);
1887 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1889 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1891 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1894 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1901 (void) printf("\n");
1904 err |= do_import(config, NULL, mntopts,
1907 show_import(config);
1909 } else if (searchname != NULL) {
1913 * We are searching for a pool based on name.
1915 verify(nvlist_lookup_string(config,
1916 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1918 if (strcmp(name, searchname) == 0) {
1919 if (found_config != NULL) {
1920 (void) fprintf(stderr, gettext(
1921 "cannot import '%s': more than "
1922 "one matching pool\n"), searchname);
1923 (void) fprintf(stderr, gettext(
1924 "import by numeric ID instead\n"));
1927 found_config = config;
1933 * Search for a pool by guid.
1935 verify(nvlist_lookup_uint64(config,
1936 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1938 if (guid == searchguid)
1939 found_config = config;
1944 * If we were searching for a specific pool, verify that we found a
1945 * pool, and then do the import.
1947 if (argc != 0 && err == 0) {
1948 if (found_config == NULL) {
1949 (void) fprintf(stderr, gettext("cannot import '%s': "
1950 "no such pool available\n"), argv[0]);
1953 err |= do_import(found_config, argc == 1 ? NULL :
1954 argv[1], mntopts, props, flags);
1959 * If we were just looking for pools, report an error if none were
1962 if (argc == 0 && first)
1963 (void) fprintf(stderr,
1964 gettext("no pools available to import\n"));
1969 nvlist_free(policy);
1970 if (searchdirs != NULL)
1973 return (err ? 1 : 0);
1976 typedef struct iostat_cbdata {
1977 zpool_list_t *cb_list;
1984 print_iostat_separator(iostat_cbdata_t *cb)
1988 for (i = 0; i < cb->cb_namewidth; i++)
1990 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1994 print_iostat_header(iostat_cbdata_t *cb)
1996 (void) printf("%*s capacity operations bandwidth\n",
1997 cb->cb_namewidth, "");
1998 (void) printf("%-*s alloc free read write read write\n",
1999 cb->cb_namewidth, "pool");
2000 print_iostat_separator(cb);
2004 * Display a single statistic.
2007 print_one_stat(uint64_t value)
2011 zfs_nicenum(value, buf, sizeof (buf));
2012 (void) printf(" %5s", buf);
2016 * Print out all the statistics for the given vdev. This can either be the
2017 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2018 * is a verbose output, and we don't want to display the toplevel pool stats.
2021 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2022 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2024 nvlist_t **oldchild, **newchild;
2026 vdev_stat_t *oldvs, *newvs;
2027 vdev_stat_t zerovs = { 0 };
2032 if (oldnv != NULL) {
2033 verify(nvlist_lookup_uint64_array(oldnv,
2034 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2039 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2040 (uint64_t **)&newvs, &c) == 0);
2042 if (strlen(name) + depth > cb->cb_namewidth)
2043 (void) printf("%*s%s", depth, "", name);
2045 (void) printf("%*s%s%*s", depth, "", name,
2046 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2048 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2053 scale = (double)NANOSEC / tdelta;
2055 /* only toplevel vdevs have capacity stats */
2056 if (newvs->vs_space == 0) {
2057 (void) printf(" - -");
2059 print_one_stat(newvs->vs_alloc);
2060 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2063 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2064 oldvs->vs_ops[ZIO_TYPE_READ])));
2066 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2067 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2069 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2070 oldvs->vs_bytes[ZIO_TYPE_READ])));
2072 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2073 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2075 (void) printf("\n");
2077 if (!cb->cb_verbose)
2080 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2081 &newchild, &children) != 0)
2084 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2085 &oldchild, &c) != 0)
2088 for (c = 0; c < children; c++) {
2089 uint64_t ishole = B_FALSE;
2091 if (nvlist_lookup_uint64(newchild[c],
2092 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2095 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2096 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2097 newchild[c], cb, depth + 2);
2102 * Include level 2 ARC devices in iostat output
2104 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2105 &newchild, &children) != 0)
2108 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2109 &oldchild, &c) != 0)
2113 (void) printf("%-*s - - - - - "
2114 "-\n", cb->cb_namewidth, "cache");
2115 for (c = 0; c < children; c++) {
2116 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2118 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2119 newchild[c], cb, depth + 2);
2126 refresh_iostat(zpool_handle_t *zhp, void *data)
2128 iostat_cbdata_t *cb = data;
2132 * If the pool has disappeared, remove it from the list and continue.
2134 if (zpool_refresh_stats(zhp, &missing) != 0)
2138 pool_list_remove(cb->cb_list, zhp);
2144 * Callback to print out the iostats for the given pool.
2147 print_iostat(zpool_handle_t *zhp, void *data)
2149 iostat_cbdata_t *cb = data;
2150 nvlist_t *oldconfig, *newconfig;
2151 nvlist_t *oldnvroot, *newnvroot;
2153 newconfig = zpool_get_config(zhp, &oldconfig);
2155 if (cb->cb_iteration == 1)
2158 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2161 if (oldconfig == NULL)
2164 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2168 * Print out the statistics for the pool.
2170 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2173 print_iostat_separator(cb);
2185 if (isatty(STDOUT_FILENO)) {
2186 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2188 columns = ws.ws_col;
2197 get_namewidth(zpool_handle_t *zhp, void *data)
2199 iostat_cbdata_t *cb = data;
2200 nvlist_t *config, *nvroot;
2203 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2204 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2206 if (!cb->cb_verbose)
2207 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2209 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2213 * The width must be at least 10, but may be as large as the
2214 * column width - 42 so that we can still fit in one line.
2216 columns = get_columns();
2218 if (cb->cb_namewidth < 10)
2219 cb->cb_namewidth = 10;
2220 if (cb->cb_namewidth > columns - 42)
2221 cb->cb_namewidth = columns - 42;
2227 * Parse the input string, get the 'interval' and 'count' value if there is one.
2230 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2233 unsigned long interval = 0, count = 0;
2237 * Determine if the last argument is an integer or a pool name
2239 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2243 interval = strtoul(argv[argc - 1], &end, 10);
2245 if (*end == '\0' && errno == 0) {
2246 if (interval == 0) {
2247 (void) fprintf(stderr, gettext("interval "
2248 "cannot be zero\n"));
2252 * Ignore the last parameter
2257 * If this is not a valid number, just plow on. The
2258 * user will get a more informative error message later
2266 * If the last argument is also an integer, then we have both a count
2269 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2274 interval = strtoul(argv[argc - 1], &end, 10);
2276 if (*end == '\0' && errno == 0) {
2277 if (interval == 0) {
2278 (void) fprintf(stderr, gettext("interval "
2279 "cannot be zero\n"));
2284 * Ignore the last parameter
2298 get_timestamp_arg(char c)
2301 timestamp_fmt = UDATE;
2303 timestamp_fmt = DDATE;
2309 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2311 * -v Display statistics for individual vdevs
2312 * -T Display a timestamp in date(1) or Unix format
2314 * This command can be tricky because we want to be able to deal with pool
2315 * creation/destruction as well as vdev configuration changes. The bulk of this
2316 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2317 * on pool_list_update() to detect the addition of new pools. Configuration
2318 * changes are all handled within libzfs.
2321 zpool_do_iostat(int argc, char **argv)
2326 unsigned long interval = 0, count = 0;
2328 boolean_t verbose = B_FALSE;
2332 while ((c = getopt(argc, argv, "T:v")) != -1) {
2335 get_timestamp_arg(*optarg);
2341 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2350 get_interval_count(&argc, argv, &interval, &count);
2353 * Construct the list of all interesting pools.
2356 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2359 if (pool_list_count(list) == 0 && argc != 0) {
2360 pool_list_free(list);
2364 if (pool_list_count(list) == 0 && interval == 0) {
2365 pool_list_free(list);
2366 (void) fprintf(stderr, gettext("no pools available\n"));
2371 * Enter the main iostat loop.
2374 cb.cb_verbose = verbose;
2375 cb.cb_iteration = 0;
2376 cb.cb_namewidth = 0;
2379 pool_list_update(list);
2381 if ((npools = pool_list_count(list)) == 0)
2382 (void) fprintf(stderr, gettext("no pools available\n"));
2385 * Refresh all statistics. This is done as an
2386 * explicit step before calculating the maximum name
2387 * width, so that any * configuration changes are
2388 * properly accounted for.
2390 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2394 * Iterate over all pools to determine the maximum width
2395 * for the pool / device name column across all pools.
2397 cb.cb_namewidth = 0;
2398 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2401 if (timestamp_fmt != NODATE)
2402 print_timestamp(timestamp_fmt);
2405 * If it's the first time, or verbose mode, print the
2408 if (++cb.cb_iteration == 1 || verbose)
2409 print_iostat_header(&cb);
2411 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2414 * If there's more than one pool, and we're not in
2415 * verbose mode (which prints a separator for us),
2416 * then print a separator.
2418 if (npools > 1 && !verbose)
2419 print_iostat_separator(&cb);
2422 (void) printf("\n");
2426 * Flush the output so that redirection to a file isn't buffered
2429 (void) fflush(stdout);
2434 if (count != 0 && --count == 0)
2437 (void) sleep(interval);
2440 pool_list_free(list);
2445 typedef struct list_cbdata {
2446 boolean_t cb_scripted;
2448 zprop_list_t *cb_proplist;
2452 * Given a list of columns to display, output appropriate headers for each one.
2455 print_header(zprop_list_t *pl)
2458 boolean_t first = B_TRUE;
2459 boolean_t right_justify;
2461 for (; pl != NULL; pl = pl->pl_next) {
2462 if (pl->pl_prop == ZPROP_INVAL)
2470 header = zpool_prop_column_name(pl->pl_prop);
2471 right_justify = zpool_prop_align_right(pl->pl_prop);
2473 if (pl->pl_next == NULL && !right_justify)
2474 (void) printf("%s", header);
2475 else if (right_justify)
2476 (void) printf("%*s", (int)pl->pl_width, header);
2478 (void) printf("%-*s", (int)pl->pl_width, header);
2481 (void) printf("\n");
2485 * Given a pool and a list of properties, print out all the properties according
2486 * to the described layout.
2489 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2491 boolean_t first = B_TRUE;
2492 char property[ZPOOL_MAXPROPLEN];
2494 boolean_t right_justify;
2497 for (; pl != NULL; pl = pl->pl_next) {
2500 (void) printf("\t");
2507 right_justify = B_FALSE;
2508 if (pl->pl_prop != ZPROP_INVAL) {
2509 if (zpool_get_prop(zhp, pl->pl_prop, property,
2510 sizeof (property), NULL) != 0)
2515 right_justify = zpool_prop_align_right(pl->pl_prop);
2520 width = pl->pl_width;
2523 * If this is being called in scripted mode, or if this is the
2524 * last column and it is left-justified, don't include a width
2527 if (scripted || (pl->pl_next == NULL && !right_justify))
2528 (void) printf("%s", propstr);
2529 else if (right_justify)
2530 (void) printf("%*s", width, propstr);
2532 (void) printf("%-*s", width, propstr);
2535 (void) printf("\n");
2539 * Generic callback function to list a pool.
2542 list_callback(zpool_handle_t *zhp, void *data)
2544 list_cbdata_t *cbp = data;
2546 if (cbp->cb_first) {
2547 if (!cbp->cb_scripted)
2548 print_header(cbp->cb_proplist);
2549 cbp->cb_first = B_FALSE;
2552 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2558 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2560 * -H Scripted mode. Don't display headers, and separate properties
2562 * -o List of properties to display. Defaults to
2563 * "name,size,allocated,free,capacity,health,altroot"
2564 * -T Display a timestamp in date(1) or Unix format
2566 * List all pools in the system, whether or not they're healthy. Output space
2567 * statistics for each one, as well as health status summary.
2570 zpool_do_list(int argc, char **argv)
2574 list_cbdata_t cb = { 0 };
2575 static char default_props[] =
2576 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2577 char *props = default_props;
2578 unsigned long interval = 0, count = 0;
2581 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2584 cb.cb_scripted = B_TRUE;
2590 get_timestamp_arg(*optarg);
2593 (void) fprintf(stderr, gettext("missing argument for "
2594 "'%c' option\n"), optopt);
2598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2607 get_interval_count(&argc, argv, &interval, &count);
2609 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2612 cb.cb_first = B_TRUE;
2616 if (timestamp_fmt != NODATE)
2617 print_timestamp(timestamp_fmt);
2619 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2620 list_callback, &cb);
2622 if (argc == 0 && cb.cb_first)
2623 (void) fprintf(stderr, gettext("no pools available\n"));
2624 else if (argc && cb.cb_first) {
2625 /* cannot open the given pool */
2626 zprop_free_list(cb.cb_proplist);
2633 if (count != 0 && --count == 0)
2636 (void) sleep(interval);
2639 zprop_free_list(cb.cb_proplist);
2644 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2646 boolean_t force = B_FALSE;
2649 char *poolname, *old_disk, *new_disk;
2650 zpool_handle_t *zhp;
2654 while ((c = getopt(argc, argv, "f")) != -1) {
2660 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2669 /* get pool name and check number of arguments */
2671 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2678 (void) fprintf(stderr,
2679 gettext("missing <device> specification\n"));
2687 (void) fprintf(stderr,
2688 gettext("missing <new_device> specification\n"));
2691 new_disk = old_disk;
2701 (void) fprintf(stderr, gettext("too many arguments\n"));
2705 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2708 if (zpool_get_config(zhp, NULL) == NULL) {
2709 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2715 nvroot = make_root_vdev(zhp, NULL, force, B_FALSE, replacing, B_FALSE,
2717 if (nvroot == NULL) {
2722 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2724 nvlist_free(nvroot);
2731 * zpool replace [-f] <pool> <device> <new_device>
2733 * -f Force attach, even if <new_device> appears to be in use.
2735 * Replace <device> with <new_device>.
2739 zpool_do_replace(int argc, char **argv)
2741 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2745 * zpool attach [-f] <pool> <device> <new_device>
2747 * -f Force attach, even if <new_device> appears to be in use.
2749 * Attach <new_device> to the mirror containing <device>. If <device> is not
2750 * part of a mirror, then <device> will be transformed into a mirror of
2751 * <device> and <new_device>. In either case, <new_device> will begin life
2752 * with a DTL of [0, now], and will immediately begin to resilver itself.
2755 zpool_do_attach(int argc, char **argv)
2757 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2761 * zpool detach [-f] <pool> <device>
2763 * -f Force detach of <device>, even if DTLs argue against it
2764 * (not supported yet)
2766 * Detach a device from a mirror. The operation will be refused if <device>
2767 * is the last device in the mirror, or if the DTLs indicate that this device
2768 * has the only valid copy of some data.
2772 zpool_do_detach(int argc, char **argv)
2775 char *poolname, *path;
2776 zpool_handle_t *zhp;
2780 while ((c = getopt(argc, argv, "f")) != -1) {
2784 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2793 /* get pool name and check number of arguments */
2795 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2800 (void) fprintf(stderr,
2801 gettext("missing <device> specification\n"));
2808 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2811 ret = zpool_vdev_detach(zhp, path);
2819 * zpool split [-n] [-o prop=val] ...
2821 * [-R altroot] <pool> <newpool> [<device> ...]
2823 * -n Do not split the pool, but display the resulting layout if
2824 * it were to be split.
2825 * -o Set property=value, or set mount options.
2826 * -R Mount the split-off pool under an alternate root.
2828 * Splits the named pool and gives it the new pool name. Devices to be split
2829 * off may be listed, provided that no more than one device is specified
2830 * per top-level vdev mirror. The newly split pool is left in an exported
2831 * state unless -R is specified.
2833 * Restrictions: the top-level of the pool pool must only be made up of
2834 * mirrors; all devices in the pool must be healthy; no device may be
2835 * undergoing a resilvering operation.
2838 zpool_do_split(int argc, char **argv)
2840 char *srcpool, *newpool, *propval;
2841 char *mntopts = NULL;
2844 zpool_handle_t *zhp;
2845 nvlist_t *config, *props = NULL;
2847 flags.dryrun = B_FALSE;
2848 flags.import = B_FALSE;
2851 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
2854 flags.import = B_TRUE;
2856 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
2857 &props, B_TRUE) != 0) {
2864 flags.dryrun = B_TRUE;
2867 if ((propval = strchr(optarg, '=')) != NULL) {
2870 if (add_prop_list(optarg, propval,
2871 &props, B_TRUE) != 0) {
2881 (void) fprintf(stderr, gettext("missing argument for "
2882 "'%c' option\n"), optopt);
2886 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2893 if (!flags.import && mntopts != NULL) {
2894 (void) fprintf(stderr, gettext("setting mntopts is only "
2895 "valid when importing the pool\n"));
2903 (void) fprintf(stderr, gettext("Missing pool name\n"));
2907 (void) fprintf(stderr, gettext("Missing new pool name\n"));
2917 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
2920 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
2921 if (config == NULL) {
2925 (void) printf(gettext("would create '%s' with the "
2926 "following layout:\n\n"), newpool);
2927 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
2929 nvlist_free(config);
2934 if (ret != 0 || flags.dryrun || !flags.import)
2938 * The split was successful. Now we need to open the new
2939 * pool and import it.
2941 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
2943 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2944 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2946 (void) fprintf(stderr, gettext("Split was succssful, but "
2947 "the datasets could not all be mounted\n"));
2948 (void) fprintf(stderr, gettext("Try doing '%s' with a "
2949 "different altroot\n"), "zpool import");
2959 * zpool online <pool> <device> ...
2962 zpool_do_online(int argc, char **argv)
2966 zpool_handle_t *zhp;
2968 vdev_state_t newstate;
2972 while ((c = getopt(argc, argv, "et")) != -1) {
2975 flags |= ZFS_ONLINE_EXPAND;
2979 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2988 /* get pool name and check number of arguments */
2990 (void) fprintf(stderr, gettext("missing pool name\n"));
2994 (void) fprintf(stderr, gettext("missing device name\n"));
3000 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3003 for (i = 1; i < argc; i++) {
3004 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3005 if (newstate != VDEV_STATE_HEALTHY) {
3006 (void) printf(gettext("warning: device '%s' "
3007 "onlined, but remains in faulted state\n"),
3009 if (newstate == VDEV_STATE_FAULTED)
3010 (void) printf(gettext("use 'zpool "
3011 "clear' to restore a faulted "
3014 (void) printf(gettext("use 'zpool "
3015 "replace' to replace devices "
3016 "that are no longer present\n"));
3029 * zpool offline [-ft] <pool> <device> ...
3031 * -f Force the device into the offline state, even if doing
3032 * so would appear to compromise pool availability.
3033 * (not supported yet)
3035 * -t Only take the device off-line temporarily. The offline
3036 * state will not be persistent across reboots.
3040 zpool_do_offline(int argc, char **argv)
3044 zpool_handle_t *zhp;
3046 boolean_t istmp = B_FALSE;
3049 while ((c = getopt(argc, argv, "ft")) != -1) {
3056 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3065 /* get pool name and check number of arguments */
3067 (void) fprintf(stderr, gettext("missing pool name\n"));
3071 (void) fprintf(stderr, gettext("missing device name\n"));
3077 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3080 for (i = 1; i < argc; i++) {
3081 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3091 * zpool clear <pool> [device]
3093 * Clear all errors associated with a pool or a particular device.
3096 zpool_do_clear(int argc, char **argv)
3100 boolean_t dryrun = B_FALSE;
3101 boolean_t do_rewind = B_FALSE;
3102 boolean_t xtreme_rewind = B_FALSE;
3103 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3104 nvlist_t *policy = NULL;
3105 zpool_handle_t *zhp;
3106 char *pool, *device;
3109 while ((c = getopt(argc, argv, "FnX")) != -1) {
3118 xtreme_rewind = B_TRUE;
3121 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3131 (void) fprintf(stderr, gettext("missing pool name\n"));
3136 (void) fprintf(stderr, gettext("too many arguments\n"));
3140 if ((dryrun || xtreme_rewind) && !do_rewind) {
3141 (void) fprintf(stderr,
3142 gettext("-n or -X only meaningful with -F\n"));
3146 rewind_policy = ZPOOL_TRY_REWIND;
3148 rewind_policy = ZPOOL_DO_REWIND;
3150 rewind_policy |= ZPOOL_EXTREME_REWIND;
3152 /* In future, further rewind policy choices can be passed along here */
3153 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3154 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3158 device = argc == 2 ? argv[1] : NULL;
3160 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3161 nvlist_free(policy);
3165 if (zpool_clear(zhp, device, policy) != 0)
3170 nvlist_free(policy);
3175 typedef struct scrub_cbdata {
3182 scrub_callback(zpool_handle_t *zhp, void *data)
3184 scrub_cbdata_t *cb = data;
3188 * Ignore faulted pools.
3190 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3191 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3192 "currently unavailable\n"), zpool_get_name(zhp));
3196 err = zpool_scan(zhp, cb->cb_type);
3202 * zpool scrub [-s] <pool> ...
3204 * -s Stop. Stops any in-progress scrub.
3207 zpool_do_scrub(int argc, char **argv)
3212 cb.cb_type = POOL_SCAN_SCRUB;
3215 while ((c = getopt(argc, argv, "s")) != -1) {
3218 cb.cb_type = POOL_SCAN_NONE;
3221 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3233 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3237 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3240 typedef struct status_cbdata {
3242 boolean_t cb_allpools;
3243 boolean_t cb_verbose;
3244 boolean_t cb_explain;
3246 boolean_t cb_dedup_stats;
3250 * Print out detailed scrub status.
3253 print_scan_status(pool_scan_stat_t *ps)
3256 uint64_t elapsed, mins_left, hours_left;
3257 uint64_t pass_exam, examined, total;
3259 double fraction_done;
3260 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3262 (void) printf(gettext(" scan: "));
3264 /* If there's never been a scan, there's not much to say. */
3265 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3266 ps->pss_func >= POOL_SCAN_FUNCS) {
3267 (void) printf(gettext("none requested\n"));
3271 start = ps->pss_start_time;
3272 end = ps->pss_end_time;
3273 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3275 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3276 ps->pss_func == POOL_SCAN_RESILVER);
3278 * Scan is finished or canceled.
3280 if (ps->pss_state == DSS_FINISHED) {
3281 uint64_t minutes_taken = (end - start) / 60;
3284 if (ps->pss_func == POOL_SCAN_SCRUB) {
3285 fmt = gettext("scrub repaired %s in %lluh%um with "
3286 "%llu errors on %s");
3287 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3288 fmt = gettext("resilvered %s in %lluh%um with "
3289 "%llu errors on %s");
3292 (void) printf(fmt, processed_buf,
3293 (u_longlong_t)(minutes_taken / 60),
3294 (uint_t)(minutes_taken % 60),
3295 (u_longlong_t)ps->pss_errors,
3296 ctime((time_t *)&end));
3298 } else if (ps->pss_state == DSS_CANCELED) {
3299 if (ps->pss_func == POOL_SCAN_SCRUB) {
3300 (void) printf(gettext("scrub canceled on %s"),
3302 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3303 (void) printf(gettext("resilver canceled on %s"),
3309 assert(ps->pss_state == DSS_SCANNING);
3312 * Scan is in progress.
3314 if (ps->pss_func == POOL_SCAN_SCRUB) {
3315 (void) printf(gettext("scrub in progress since %s"),
3317 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3318 (void) printf(gettext("resilver in progress since %s"),
3322 examined = ps->pss_examined ? ps->pss_examined : 1;
3323 total = ps->pss_to_examine;
3324 fraction_done = (double)examined / total;
3326 /* elapsed time for this pass */
3327 elapsed = time(NULL) - ps->pss_pass_start;
3328 elapsed = elapsed ? elapsed : 1;
3329 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3330 rate = pass_exam / elapsed;
3331 rate = rate ? rate : 1;
3332 mins_left = ((total - examined) / rate) / 60;
3333 hours_left = mins_left / 60;
3335 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3336 zfs_nicenum(total, total_buf, sizeof (total_buf));
3337 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3340 * do not print estimated time if hours_left is more than 30 days
3342 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3343 examined_buf, total_buf, rate_buf);
3344 if (hours_left < (30 * 24)) {
3345 (void) printf(gettext(", %lluh%um to go\n"),
3346 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3348 (void) printf(gettext(
3349 ", (scan is slow, no estimated time)\n"));
3352 if (ps->pss_func == POOL_SCAN_RESILVER) {
3353 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3354 processed_buf, 100 * fraction_done);
3355 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3356 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3357 processed_buf, 100 * fraction_done);
3362 print_error_log(zpool_handle_t *zhp)
3364 nvlist_t *nverrlist = NULL;
3367 size_t len = MAXPATHLEN * 2;
3369 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3370 (void) printf("errors: List of errors unavailable "
3371 "(insufficient privileges)\n");
3375 (void) printf("errors: Permanent errors have been "
3376 "detected in the following files:\n\n");
3378 pathname = safe_malloc(len);
3380 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3382 uint64_t dsobj, obj;
3384 verify(nvpair_value_nvlist(elem, &nv) == 0);
3385 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3387 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3389 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3390 (void) printf("%7s %s\n", "", pathname);
3393 nvlist_free(nverrlist);
3397 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3406 (void) printf(gettext("\tspares\n"));
3408 for (i = 0; i < nspares; i++) {
3409 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3410 print_status_config(zhp, name, spares[i],
3411 namewidth, 2, B_TRUE);
3417 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3426 (void) printf(gettext("\tcache\n"));
3428 for (i = 0; i < nl2cache; i++) {
3429 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3430 print_status_config(zhp, name, l2cache[i],
3431 namewidth, 2, B_FALSE);
3437 print_dedup_stats(nvlist_t *config)
3439 ddt_histogram_t *ddh;
3445 * If the pool was faulted then we may not have been able to
3446 * obtain the config. Otherwise, if have anything in the dedup
3447 * table continue processing the stats.
3449 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3450 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3453 (void) printf("\n");
3454 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3455 (u_longlong_t)ddo->ddo_count,
3456 (u_longlong_t)ddo->ddo_dspace,
3457 (u_longlong_t)ddo->ddo_mspace);
3459 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3460 (uint64_t **)&dds, &c) == 0);
3461 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3462 (uint64_t **)&ddh, &c) == 0);
3463 zpool_dump_ddt(dds, ddh);
3467 * Display a summary of pool status. Displays a summary such as:
3471 * reason: One or more devices ...
3472 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
3478 * When given the '-v' option, we print out the complete config. If the '-e'
3479 * option is specified, then we print out error rate information as well.
3482 status_callback(zpool_handle_t *zhp, void *data)
3484 status_cbdata_t *cbp = data;
3485 nvlist_t *config, *nvroot;
3492 config = zpool_get_config(zhp, NULL);
3493 reason = zpool_get_status(zhp, &msgid);
3498 * If we were given 'zpool status -x', only report those pools with
3501 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3502 if (!cbp->cb_allpools) {
3503 (void) printf(gettext("pool '%s' is healthy\n"),
3504 zpool_get_name(zhp));
3506 cbp->cb_first = B_FALSE;
3512 cbp->cb_first = B_FALSE;
3514 (void) printf("\n");
3516 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3518 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3519 (uint64_t **)&vs, &c) == 0);
3520 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3522 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3523 (void) printf(gettext(" state: %s\n"), health);
3526 case ZPOOL_STATUS_MISSING_DEV_R:
3527 (void) printf(gettext("status: One or more devices could not "
3528 "be opened. Sufficient replicas exist for\n\tthe pool to "
3529 "continue functioning in a degraded state.\n"));
3530 (void) printf(gettext("action: Attach the missing device and "
3531 "online it using 'zpool online'.\n"));
3534 case ZPOOL_STATUS_MISSING_DEV_NR:
3535 (void) printf(gettext("status: One or more devices could not "
3536 "be opened. There are insufficient\n\treplicas for the "
3537 "pool to continue functioning.\n"));
3538 (void) printf(gettext("action: Attach the missing device and "
3539 "online it using 'zpool online'.\n"));
3542 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3543 (void) printf(gettext("status: One or more devices could not "
3544 "be used because the label is missing or\n\tinvalid. "
3545 "Sufficient replicas exist for the pool to continue\n\t"
3546 "functioning in a degraded state.\n"));
3547 (void) printf(gettext("action: Replace the device using "
3548 "'zpool replace'.\n"));
3551 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3552 (void) printf(gettext("status: One or more devices could not "
3553 "be used because the label is missing \n\tor invalid. "
3554 "There are insufficient replicas for the pool to "
3555 "continue\n\tfunctioning.\n"));
3556 zpool_explain_recover(zpool_get_handle(zhp),
3557 zpool_get_name(zhp), reason, config);
3560 case ZPOOL_STATUS_FAILING_DEV:
3561 (void) printf(gettext("status: One or more devices has "
3562 "experienced an unrecoverable error. An\n\tattempt was "
3563 "made to correct the error. Applications are "
3565 (void) printf(gettext("action: Determine if the device needs "
3566 "to be replaced, and clear the errors\n\tusing "
3567 "'zpool clear' or replace the device with 'zpool "
3571 case ZPOOL_STATUS_OFFLINE_DEV:
3572 (void) printf(gettext("status: One or more devices has "
3573 "been taken offline by the administrator.\n\tSufficient "
3574 "replicas exist for the pool to continue functioning in "
3575 "a\n\tdegraded state.\n"));
3576 (void) printf(gettext("action: Online the device using "
3577 "'zpool online' or replace the device with\n\t'zpool "
3581 case ZPOOL_STATUS_REMOVED_DEV:
3582 (void) printf(gettext("status: One or more devices has "
3583 "been removed by the administrator.\n\tSufficient "
3584 "replicas exist for the pool to continue functioning in "
3585 "a\n\tdegraded state.\n"));
3586 (void) printf(gettext("action: Online the device using "
3587 "'zpool online' or replace the device with\n\t'zpool "
3591 case ZPOOL_STATUS_RESILVERING:
3592 (void) printf(gettext("status: One or more devices is "
3593 "currently being resilvered. The pool will\n\tcontinue "
3594 "to function, possibly in a degraded state.\n"));
3595 (void) printf(gettext("action: Wait for the resilver to "
3599 case ZPOOL_STATUS_CORRUPT_DATA:
3600 (void) printf(gettext("status: One or more devices has "
3601 "experienced an error resulting in data\n\tcorruption. "
3602 "Applications may be affected.\n"));
3603 (void) printf(gettext("action: Restore the file in question "
3604 "if possible. Otherwise restore the\n\tentire pool from "
3608 case ZPOOL_STATUS_CORRUPT_POOL:
3609 (void) printf(gettext("status: The pool metadata is corrupted "
3610 "and the pool cannot be opened.\n"));
3611 zpool_explain_recover(zpool_get_handle(zhp),
3612 zpool_get_name(zhp), reason, config);
3615 case ZPOOL_STATUS_VERSION_OLDER:
3616 (void) printf(gettext("status: The pool is formatted using an "
3617 "older on-disk format. The pool can\n\tstill be used, but "
3618 "some features are unavailable.\n"));
3619 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3620 "upgrade'. Once this is done, the\n\tpool will no longer "
3621 "be accessible on older software versions.\n"));
3624 case ZPOOL_STATUS_VERSION_NEWER:
3625 (void) printf(gettext("status: The pool has been upgraded to a "
3626 "newer, incompatible on-disk version.\n\tThe pool cannot "
3627 "be accessed on this system.\n"));
3628 (void) printf(gettext("action: Access the pool from a system "
3629 "running more recent software, or\n\trestore the pool from "
3633 case ZPOOL_STATUS_FAULTED_DEV_R:
3634 (void) printf(gettext("status: One or more devices are "
3635 "faulted in response to persistent errors.\n\tSufficient "
3636 "replicas exist for the pool to continue functioning "
3637 "in a\n\tdegraded state.\n"));
3638 (void) printf(gettext("action: Replace the faulted device, "
3639 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3642 case ZPOOL_STATUS_FAULTED_DEV_NR:
3643 (void) printf(gettext("status: One or more devices are "
3644 "faulted in response to persistent errors. There are "
3645 "insufficient replicas for the pool to\n\tcontinue "
3647 (void) printf(gettext("action: Destroy and re-create the pool "
3648 "from a backup source. Manually marking the device\n"
3649 "\trepaired using 'zpool clear' may allow some data "
3650 "to be recovered.\n"));
3653 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3654 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3655 (void) printf(gettext("status: One or more devices are "
3656 "faulted in response to IO failures.\n"));
3657 (void) printf(gettext("action: Make sure the affected devices "
3658 "are connected, then run 'zpool clear'.\n"));
3661 case ZPOOL_STATUS_BAD_LOG:
3662 (void) printf(gettext("status: An intent log record "
3663 "could not be read.\n"
3664 "\tWaiting for adminstrator intervention to fix the "
3665 "faulted pool.\n"));
3666 (void) printf(gettext("action: Either restore the affected "
3667 "device(s) and run 'zpool online',\n"
3668 "\tor ignore the intent log records by running "
3669 "'zpool clear'.\n"));
3674 * The remaining errors can't actually be generated, yet.
3676 assert(reason == ZPOOL_STATUS_OK);
3680 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
3683 if (config != NULL) {
3686 nvlist_t **spares, **l2cache;
3687 uint_t nspares, nl2cache;
3688 pool_scan_stat_t *ps = NULL;
3690 (void) nvlist_lookup_uint64_array(nvroot,
3691 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3692 print_scan_status(ps);
3694 namewidth = max_width(zhp, nvroot, 0, 0);
3698 (void) printf(gettext("config:\n\n"));
3699 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3700 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3701 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3702 namewidth, 0, B_FALSE);
3704 if (num_logs(nvroot) > 0)
3705 print_logs(zhp, nvroot, namewidth, B_TRUE);
3706 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3707 &l2cache, &nl2cache) == 0)
3708 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3710 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3711 &spares, &nspares) == 0)
3712 print_spares(zhp, spares, nspares, namewidth);
3714 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3716 nvlist_t *nverrlist = NULL;
3719 * If the approximate error count is small, get a
3720 * precise count by fetching the entire log and
3721 * uniquifying the results.
3723 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3724 zpool_get_errlog(zhp, &nverrlist) == 0) {
3729 while ((elem = nvlist_next_nvpair(nverrlist,
3734 nvlist_free(nverrlist);
3736 (void) printf("\n");
3739 (void) printf(gettext("errors: No known data "
3741 else if (!cbp->cb_verbose)
3742 (void) printf(gettext("errors: %llu data "
3743 "errors, use '-v' for a list\n"),
3744 (u_longlong_t)nerr);
3746 print_error_log(zhp);
3749 if (cbp->cb_dedup_stats)
3750 print_dedup_stats(config);
3752 (void) printf(gettext("config: The configuration cannot be "
3760 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3762 * -v Display complete error logs
3763 * -x Display only pools with potential problems
3764 * -D Display dedup status (undocumented)
3765 * -T Display a timestamp in date(1) or Unix format
3767 * Describes the health status of all pools or some subset.
3770 zpool_do_status(int argc, char **argv)
3774 unsigned long interval = 0, count = 0;
3775 status_cbdata_t cb = { 0 };
3778 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3781 cb.cb_verbose = B_TRUE;
3784 cb.cb_explain = B_TRUE;
3787 cb.cb_dedup_stats = B_TRUE;
3790 get_timestamp_arg(*optarg);
3793 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3802 get_interval_count(&argc, argv, &interval, &count);
3805 cb.cb_allpools = B_TRUE;
3807 cb.cb_first = B_TRUE;
3810 if (timestamp_fmt != NODATE)
3811 print_timestamp(timestamp_fmt);
3813 ret = for_each_pool(argc, argv, B_TRUE, NULL,
3814 status_callback, &cb);
3816 if (argc == 0 && cb.cb_count == 0)
3817 (void) fprintf(stderr, gettext("no pools available\n"));
3818 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3819 (void) printf(gettext("all pools are healthy\n"));
3827 if (count != 0 && --count == 0)
3830 (void) sleep(interval);
3836 typedef struct upgrade_cbdata {
3841 uint64_t cb_version;
3846 upgrade_cb(zpool_handle_t *zhp, void *arg)
3848 upgrade_cbdata_t *cbp = arg;
3853 config = zpool_get_config(zhp, NULL);
3854 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3857 if (!cbp->cb_newer && version < SPA_VERSION) {
3859 if (cbp->cb_first) {
3860 (void) printf(gettext("The following pools are "
3861 "out of date, and can be upgraded. After "
3862 "being\nupgraded, these pools will no "
3863 "longer be accessible by older software "
3865 (void) printf(gettext("VER POOL\n"));
3866 (void) printf(gettext("--- ------------\n"));
3867 cbp->cb_first = B_FALSE;
3870 (void) printf("%2llu %s\n", (u_longlong_t)version,
3871 zpool_get_name(zhp));
3873 cbp->cb_first = B_FALSE;
3874 ret = zpool_upgrade(zhp, cbp->cb_version);
3876 (void) printf(gettext("Successfully upgraded "
3877 "'%s'\n\n"), zpool_get_name(zhp));
3880 } else if (cbp->cb_newer && version > SPA_VERSION) {
3881 assert(!cbp->cb_all);
3883 if (cbp->cb_first) {
3884 (void) printf(gettext("The following pools are "
3885 "formatted using a newer software version and\n"
3886 "cannot be accessed on the current system.\n\n"));
3887 (void) printf(gettext("VER POOL\n"));
3888 (void) printf(gettext("--- ------------\n"));
3889 cbp->cb_first = B_FALSE;
3892 (void) printf("%2llu %s\n", (u_longlong_t)version,
3893 zpool_get_name(zhp));
3902 upgrade_one(zpool_handle_t *zhp, void *data)
3904 upgrade_cbdata_t *cbp = data;
3905 uint64_t cur_version;
3908 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3909 (void) printf(gettext("'log' is now a reserved word\n"
3910 "Pool 'log' must be renamed using export and import"
3915 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3916 if (cur_version > cbp->cb_version) {
3917 (void) printf(gettext("Pool '%s' is already formatted "
3918 "using more current version '%llu'.\n"),
3919 zpool_get_name(zhp), (u_longlong_t) cur_version);
3922 if (cur_version == cbp->cb_version) {
3923 (void) printf(gettext("Pool '%s' is already formatted "
3924 "using the current version.\n"), zpool_get_name(zhp));
3928 ret = zpool_upgrade(zhp, cbp->cb_version);
3931 (void) printf(gettext("Successfully upgraded '%s' "
3932 "from version %llu to version %llu\n\n"),
3933 zpool_get_name(zhp), (u_longlong_t)cur_version,
3934 (u_longlong_t)cbp->cb_version);
3943 * zpool upgrade [-V version] <-a | pool ...>
3945 * With no arguments, display downrev'd ZFS pool available for upgrade.
3946 * Individual pools can be upgraded by specifying the pool, and '-a' will
3947 * upgrade all pools.
3950 zpool_do_upgrade(int argc, char **argv)
3953 upgrade_cbdata_t cb = { 0 };
3955 boolean_t showversions = B_FALSE;
3960 while ((c = getopt(argc, argv, ":avV:")) != -1) {
3966 showversions = B_TRUE;
3969 cb.cb_version = strtoll(optarg, &end, 10);
3970 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3971 cb.cb_version < SPA_VERSION_1) {
3972 (void) fprintf(stderr,
3973 gettext("invalid version '%s'\n"), optarg);
3978 (void) fprintf(stderr, gettext("missing argument for "
3979 "'%c' option\n"), optopt);
3983 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3994 if (cb.cb_version == 0) {
3995 cb.cb_version = SPA_VERSION;
3996 } else if (!cb.cb_all && argc == 0) {
3997 (void) fprintf(stderr, gettext("-V option is "
3998 "incompatible with other arguments\n"));
4003 if (cb.cb_all || argc != 0) {
4004 (void) fprintf(stderr, gettext("-v option is "
4005 "incompatible with other arguments\n"));
4008 } else if (cb.cb_all) {
4010 (void) fprintf(stderr, gettext("-a option should not "
4011 "be used along with a pool name\n"));
4016 (void) printf(gettext("This system is currently running "
4017 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4018 cb.cb_first = B_TRUE;
4020 (void) printf(gettext("The following versions are "
4022 (void) printf(gettext("VER DESCRIPTION\n"));
4023 (void) printf("--- -----------------------------------------"
4024 "---------------\n");
4025 (void) printf(gettext(" 1 Initial ZFS version\n"));
4026 (void) printf(gettext(" 2 Ditto blocks "
4027 "(replicated metadata)\n"));
4028 (void) printf(gettext(" 3 Hot spares and double parity "
4030 (void) printf(gettext(" 4 zpool history\n"));
4031 (void) printf(gettext(" 5 Compression using the gzip "
4033 (void) printf(gettext(" 6 bootfs pool property\n"));
4034 (void) printf(gettext(" 7 Separate intent log devices\n"));
4035 (void) printf(gettext(" 8 Delegated administration\n"));
4036 (void) printf(gettext(" 9 refquota and refreservation "
4038 (void) printf(gettext(" 10 Cache devices\n"));
4039 (void) printf(gettext(" 11 Improved scrub performance\n"));
4040 (void) printf(gettext(" 12 Snapshot properties\n"));
4041 (void) printf(gettext(" 13 snapused property\n"));
4042 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4043 (void) printf(gettext(" 15 user/group space accounting\n"));
4044 (void) printf(gettext(" 16 stmf property support\n"));
4045 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4046 (void) printf(gettext(" 18 Snapshot user holds\n"));
4047 (void) printf(gettext(" 19 Log device removal\n"));
4048 (void) printf(gettext(" 20 Compression using zle "
4049 "(zero-length encoding)\n"));
4050 (void) printf(gettext(" 21 Deduplication\n"));
4051 (void) printf(gettext(" 22 Received properties\n"));
4052 (void) printf(gettext(" 23 Slim ZIL\n"));
4053 (void) printf(gettext(" 24 System attributes\n"));
4054 (void) printf(gettext(" 25 Improved scrub stats\n"));
4055 (void) printf(gettext(" 26 Improved snapshot deletion "
4057 (void) printf(gettext(" 27 Improved snapshot creation "
4059 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4060 (void) printf(gettext("\nFor more information on a particular "
4061 "version, including supported releases,\n"));
4062 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4063 } else if (argc == 0) {
4066 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4067 notfound = cb.cb_first;
4069 if (!cb.cb_all && ret == 0) {
4071 (void) printf("\n");
4072 cb.cb_first = B_TRUE;
4073 cb.cb_newer = B_TRUE;
4074 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4077 (void) printf("\n");
4083 (void) printf(gettext("All pools are formatted "
4084 "using this version.\n"));
4085 else if (!cb.cb_all)
4086 (void) printf(gettext("Use 'zpool upgrade -v' "
4087 "for a list of available versions and "
4088 "their associated\nfeatures.\n"));
4091 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4098 typedef struct hist_cbdata {
4105 * Print out the command history for a specific pool.
4108 get_history_one(zpool_handle_t *zhp, void *data)
4124 char internalstr[MAXPATHLEN];
4125 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4129 cb->first = B_FALSE;
4131 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4133 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4136 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4137 &records, &numrecords) == 0);
4138 for (i = 0; i < numrecords; i++) {
4139 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4143 /* is it an internal event or a standard event? */
4144 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4146 if (cb->internal == 0)
4149 if (nvlist_lookup_uint64(records[i],
4150 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4152 verify(nvlist_lookup_uint64(records[i],
4153 ZPOOL_HIST_TXG, &txg) == 0);
4154 verify(nvlist_lookup_string(records[i],
4155 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4156 if (ievent >= LOG_END)
4158 (void) snprintf(internalstr,
4159 sizeof (internalstr),
4160 "[internal %s txg:%llu] %s",
4161 zfs_history_event_names[ievent], (u_longlong_t)txg,
4163 cmdstr = internalstr;
4166 (void) localtime_r(&tsec, &t);
4167 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4168 (void) printf("%s %s", tbuf, cmdstr);
4171 (void) printf("\n");
4174 (void) printf(" [");
4175 if (nvlist_lookup_uint64(records[i],
4176 ZPOOL_HIST_WHO, &who) == 0) {
4177 pwd = getpwuid((uid_t)who);
4179 (void) printf("user %s on",
4182 (void) printf("user %d on",
4185 (void) printf(gettext("no info]\n"));
4188 if (nvlist_lookup_string(records[i],
4189 ZPOOL_HIST_HOST, &hostname) == 0) {
4190 (void) printf(" %s", hostname);
4192 if (nvlist_lookup_string(records[i],
4193 ZPOOL_HIST_ZONE, &zonename) == 0) {
4194 (void) printf(":%s", zonename);
4198 (void) printf("\n");
4200 (void) printf("\n");
4207 * zpool history <pool>
4209 * Displays the history of commands that modified pools.
4214 zpool_do_history(int argc, char **argv)
4216 hist_cbdata_t cbdata = { 0 };
4220 cbdata.first = B_TRUE;
4222 while ((c = getopt(argc, argv, "li")) != -1) {
4228 cbdata.internal = 1;
4231 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4239 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4242 if (argc == 0 && cbdata.first == B_TRUE) {
4243 (void) fprintf(stderr, gettext("no pools available\n"));
4250 typedef struct ev_opts {
4258 zpool_do_events_short(nvlist_t *nvl)
4260 char ctime_str[26], str[32], *ptr;
4264 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
4265 memset(str, ' ', 32);
4266 (void) ctime_r((const time_t *)&tv[0], ctime_str);
4267 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
4268 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
4269 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
4270 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
4271 (void) printf(gettext("%s "), str);
4273 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
4274 (void) printf(gettext("%s\n"), ptr);
4278 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
4282 for (nvp = nvlist_next_nvpair(nvl, NULL);
4283 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
4285 data_type_t type = nvpair_type(nvp);
4286 const char *name = nvpair_name(nvp);
4296 printf(gettext("%*s%s = "), depth, "", name);
4299 case DATA_TYPE_BOOLEAN:
4300 printf(gettext("%s"), "1");
4303 case DATA_TYPE_BOOLEAN_VALUE:
4304 (void) nvpair_value_boolean_value(nvp, &b);
4305 printf(gettext("%s"), b ? "1" : "0");
4308 case DATA_TYPE_BYTE:
4309 (void) nvpair_value_byte(nvp, &i8);
4310 printf(gettext("0x%x"), i8);
4313 case DATA_TYPE_INT8:
4314 (void) nvpair_value_int8(nvp, (void *)&i8);
4315 printf(gettext("0x%x"), i8);
4318 case DATA_TYPE_UINT8:
4319 (void) nvpair_value_uint8(nvp, &i8);
4320 printf(gettext("0x%x"), i8);
4323 case DATA_TYPE_INT16:
4324 (void) nvpair_value_int16(nvp, (void *)&i16);
4325 printf(gettext("0x%x"), i16);
4328 case DATA_TYPE_UINT16:
4329 (void) nvpair_value_uint16(nvp, &i16);
4330 printf(gettext("0x%x"), i16);
4333 case DATA_TYPE_INT32:
4334 (void) nvpair_value_int32(nvp, (void *)&i32);
4335 printf(gettext("0x%x"), i32);
4338 case DATA_TYPE_UINT32:
4339 (void) nvpair_value_uint32(nvp, &i32);
4340 printf(gettext("0x%x"), i32);
4343 case DATA_TYPE_INT64:
4344 (void) nvpair_value_int64(nvp, (void *)&i64);
4345 printf(gettext("0x%llx"), (u_longlong_t)i64);
4348 case DATA_TYPE_UINT64:
4349 (void) nvpair_value_uint64(nvp, &i64);
4350 printf(gettext("0x%llx"), (u_longlong_t)i64);
4353 case DATA_TYPE_HRTIME:
4354 (void) nvpair_value_hrtime(nvp, (void *)&i64);
4355 printf(gettext("0x%llx"), (u_longlong_t)i64);
4358 case DATA_TYPE_STRING:
4359 (void) nvpair_value_string(nvp, &str);
4360 printf(gettext("\"%s\""), str ? str : "<NULL>");
4363 case DATA_TYPE_NVLIST:
4364 printf(gettext("(embedded nvlist)\n"));
4365 (void) nvpair_value_nvlist(nvp, &cnv);
4366 zpool_do_events_nvprint(cnv, depth + 8);
4367 printf(gettext("%*s(end %s)"), depth, "", name);
4370 case DATA_TYPE_NVLIST_ARRAY: {
4374 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
4375 printf(gettext("(%d embedded nvlists)\n"), nelem);
4376 for (i = 0; i < nelem; i++) {
4377 printf(gettext("%*s%s[%d] = %s\n"),
4378 depth, "", name, i, "(embedded nvlist)");
4379 zpool_do_events_nvprint(val[i], depth + 8);
4380 printf(gettext("%*s(end %s[%i])\n"),
4381 depth, "", name, i);
4383 printf(gettext("%*s(end %s)\n"), depth, "", name);
4387 case DATA_TYPE_INT8_ARRAY: {
4391 (void) nvpair_value_int8_array(nvp, &val, &nelem);
4392 for (i = 0; i < nelem; i++)
4393 printf(gettext("0x%x "), val[i]);
4398 case DATA_TYPE_UINT8_ARRAY: {
4402 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
4403 for (i = 0; i < nelem; i++)
4404 printf(gettext("0x%x "), val[i]);
4409 case DATA_TYPE_INT16_ARRAY: {
4413 (void) nvpair_value_int16_array(nvp, &val, &nelem);
4414 for (i = 0; i < nelem; i++)
4415 printf(gettext("0x%x "), val[i]);
4420 case DATA_TYPE_UINT16_ARRAY: {
4424 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
4425 for (i = 0; i < nelem; i++)
4426 printf(gettext("0x%x "), val[i]);
4431 case DATA_TYPE_INT32_ARRAY: {
4435 (void) nvpair_value_int32_array(nvp, &val, &nelem);
4436 for (i = 0; i < nelem; i++)
4437 printf(gettext("0x%x "), val[i]);
4442 case DATA_TYPE_UINT32_ARRAY: {
4446 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
4447 for (i = 0; i < nelem; i++)
4448 printf(gettext("0x%x "), val[i]);
4453 case DATA_TYPE_INT64_ARRAY: {
4457 (void) nvpair_value_int64_array(nvp, &val, &nelem);
4458 for (i = 0; i < nelem; i++)
4459 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4464 case DATA_TYPE_UINT64_ARRAY: {
4468 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
4469 for (i = 0; i < nelem; i++)
4470 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4475 case DATA_TYPE_STRING_ARRAY:
4476 case DATA_TYPE_BOOLEAN_ARRAY:
4477 case DATA_TYPE_BYTE_ARRAY:
4478 case DATA_TYPE_DOUBLE:
4479 case DATA_TYPE_UNKNOWN:
4480 printf(gettext("<unknown>"));
4484 printf(gettext("\n"));
4489 zpool_do_events_next(ev_opts_t *opts)
4492 int cleanup_fd, ret, dropped;
4494 cleanup_fd = open(ZFS_DEV, O_RDWR);
4495 VERIFY(cleanup_fd >= 0);
4497 if (!opts->scripted)
4498 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
4501 ret = zpool_events_next(g_zfs, &nvl, &dropped,
4502 !!opts->follow, cleanup_fd);
4503 if (ret || nvl == NULL)
4507 (void) printf(gettext("dropped %d events\n"), dropped);
4509 zpool_do_events_short(nvl);
4511 if (opts->verbose) {
4512 zpool_do_events_nvprint(nvl, 8);
4513 printf(gettext("\n"));
4519 VERIFY(0 == close(cleanup_fd));
4525 zpool_do_events_clear(ev_opts_t *opts)
4529 ret = zpool_events_clear(g_zfs, &count);
4531 (void) printf(gettext("cleared %d events\n"), count);
4537 * zpool events [-vfc]
4539 * Displays events logs by ZFS.
4542 zpool_do_events(int argc, char **argv)
4544 ev_opts_t opts = { 0 };
4549 while ((c = getopt(argc, argv, "vHfc")) != -1) {
4564 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4573 ret = zpool_do_events_clear(&opts);
4575 ret = zpool_do_events_next(&opts);
4581 get_callback(zpool_handle_t *zhp, void *data)
4583 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4584 char value[MAXNAMELEN];
4585 zprop_source_t srctype;
4588 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4591 * Skip the special fake placeholder. This will also skip
4592 * over the name property when 'all' is specified.
4594 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4595 pl == cbp->cb_proplist)
4598 if (zpool_get_prop(zhp, pl->pl_prop,
4599 value, sizeof (value), &srctype) != 0)
4602 zprop_print_one_property(zpool_get_name(zhp), cbp,
4603 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4610 zpool_do_get(int argc, char **argv)
4612 zprop_get_cbdata_t cb = { 0 };
4613 zprop_list_t fake_name = { 0 };
4619 cb.cb_first = B_TRUE;
4620 cb.cb_sources = ZPROP_SRC_ALL;
4621 cb.cb_columns[0] = GET_COL_NAME;
4622 cb.cb_columns[1] = GET_COL_PROPERTY;
4623 cb.cb_columns[2] = GET_COL_VALUE;
4624 cb.cb_columns[3] = GET_COL_SOURCE;
4625 cb.cb_type = ZFS_TYPE_POOL;
4627 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4628 ZFS_TYPE_POOL) != 0)
4631 if (cb.cb_proplist != NULL) {
4632 fake_name.pl_prop = ZPOOL_PROP_NAME;
4633 fake_name.pl_width = strlen(gettext("NAME"));
4634 fake_name.pl_next = cb.cb_proplist;
4635 cb.cb_proplist = &fake_name;
4638 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4641 if (cb.cb_proplist == &fake_name)
4642 zprop_free_list(fake_name.pl_next);
4644 zprop_free_list(cb.cb_proplist);
4649 typedef struct set_cbdata {
4652 boolean_t cb_any_successful;
4656 set_callback(zpool_handle_t *zhp, void *data)
4659 set_cbdata_t *cb = (set_cbdata_t *)data;
4661 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4664 cb->cb_any_successful = B_TRUE;
4670 zpool_do_set(int argc, char **argv)
4672 set_cbdata_t cb = { 0 };
4675 if (argc > 1 && argv[1][0] == '-') {
4676 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4682 (void) fprintf(stderr, gettext("missing property=value "
4688 (void) fprintf(stderr, gettext("missing pool name\n"));
4693 (void) fprintf(stderr, gettext("too many pool names\n"));
4697 cb.cb_propname = argv[1];
4698 cb.cb_value = strchr(cb.cb_propname, '=');
4699 if (cb.cb_value == NULL) {
4700 (void) fprintf(stderr, gettext("missing value in "
4701 "property=value argument\n"));
4705 *(cb.cb_value) = '\0';
4708 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4715 find_command_idx(char *command, int *idx)
4719 for (i = 0; i < NCOMMAND; i++) {
4720 if (command_table[i].name == NULL)
4723 if (strcmp(command, command_table[i].name) == 0) {
4732 main(int argc, char **argv)
4738 (void) setlocale(LC_ALL, "");
4739 (void) textdomain(TEXT_DOMAIN);
4744 * Make sure the user has specified some command.
4747 (void) fprintf(stderr, gettext("missing command\n"));
4756 if ((strcmp(cmdname, "-?") == 0) ||
4757 strcmp(cmdname, "--help") == 0)
4760 if ((g_zfs = libzfs_init()) == NULL)
4763 libzfs_print_on_error(g_zfs, B_TRUE);
4765 zpool_set_history_str("zpool", argc, argv, history_str);
4766 verify(zpool_stage_history(g_zfs, history_str) == 0);
4769 * Run the appropriate command.
4771 if (find_command_idx(cmdname, &i) == 0) {
4772 current_command = &command_table[i];
4773 ret = command_table[i].func(argc - 1, argv + 1);
4774 } else if (strchr(cmdname, '=')) {
4775 verify(find_command_idx("set", &i) == 0);
4776 current_command = &command_table[i];
4777 ret = command_table[i].func(argc, argv);
4778 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4780 * 'freeze' is a vile debugging abomination, so we treat
4784 int fd = open(ZFS_DEV, O_RDWR);
4785 (void) strcpy((void *)buf, argv[2]);
4786 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4788 (void) fprintf(stderr, gettext("unrecognized "
4789 "command '%s'\n"), cmdname);
4797 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4798 * for the purposes of running ::findleaks.
4800 if (getenv("ZFS_ABORT") != NULL) {
4801 (void) printf("dumping core by request\n");