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.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
46 #include <sys/fs/zfs.h>
48 #include <sys/fm/util.h>
49 #include <sys/fm/protocol.h>
53 #include "zpool_util.h"
54 #include "zfs_comutil.h"
56 #include "statcommon.h"
58 static int zpool_do_create(int, char **);
59 static int zpool_do_destroy(int, char **);
61 static int zpool_do_add(int, char **);
62 static int zpool_do_remove(int, char **);
64 static int zpool_do_list(int, char **);
65 static int zpool_do_iostat(int, char **);
66 static int zpool_do_status(int, char **);
68 static int zpool_do_online(int, char **);
69 static int zpool_do_offline(int, char **);
70 static int zpool_do_clear(int, char **);
72 static int zpool_do_attach(int, char **);
73 static int zpool_do_detach(int, char **);
74 static int zpool_do_replace(int, char **);
75 static int zpool_do_split(int, char **);
77 static int zpool_do_scrub(int, char **);
79 static int zpool_do_import(int, char **);
80 static int zpool_do_export(int, char **);
82 static int zpool_do_upgrade(int, char **);
84 static int zpool_do_history(int, char **);
85 static int zpool_do_events(int, char **);
87 static int zpool_do_get(int, char **);
88 static int zpool_do_set(int, char **);
91 * These libumem hooks provide a reasonable set of defaults for the allocator's
92 * debugging facilities.
97 _umem_debug_init(void)
99 return ("default,verbose"); /* $UMEM_DEBUG setting */
103 _umem_logging_init(void)
105 return ("fail,contents"); /* $UMEM_LOGGING setting */
135 typedef struct zpool_command {
137 int (*func)(int, char **);
142 * Master command table. Each ZFS command has a name, associated function, and
143 * usage message. The usage messages need to be internationalized, so we have
144 * to have a function to return the usage message based on a command index.
146 * These commands are organized according to how they are displayed in the usage
147 * message. An empty command (one with a NULL name) indicates an empty line in
148 * the generic usage message.
150 static zpool_command_t command_table[] = {
151 { "create", zpool_do_create, HELP_CREATE },
152 { "destroy", zpool_do_destroy, HELP_DESTROY },
154 { "add", zpool_do_add, HELP_ADD },
155 { "remove", zpool_do_remove, HELP_REMOVE },
157 { "list", zpool_do_list, HELP_LIST },
158 { "iostat", zpool_do_iostat, HELP_IOSTAT },
159 { "status", zpool_do_status, HELP_STATUS },
161 { "online", zpool_do_online, HELP_ONLINE },
162 { "offline", zpool_do_offline, HELP_OFFLINE },
163 { "clear", zpool_do_clear, HELP_CLEAR },
165 { "attach", zpool_do_attach, HELP_ATTACH },
166 { "detach", zpool_do_detach, HELP_DETACH },
167 { "replace", zpool_do_replace, HELP_REPLACE },
168 { "split", zpool_do_split, HELP_SPLIT },
170 { "scrub", zpool_do_scrub, HELP_SCRUB },
172 { "import", zpool_do_import, HELP_IMPORT },
173 { "export", zpool_do_export, HELP_EXPORT },
174 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
176 { "history", zpool_do_history, HELP_HISTORY },
177 { "events", zpool_do_events, HELP_EVENTS },
179 { "get", zpool_do_get, HELP_GET },
180 { "set", zpool_do_set, HELP_SET },
183 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
185 zpool_command_t *current_command;
186 static char history_str[HIS_MAX_RECORD_LEN];
188 static uint_t timestamp_fmt = NODATE;
191 get_usage(zpool_help_t idx) {
194 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
196 return (gettext("\tattach [-f] <pool> <device> "
199 return (gettext("\tclear [-nF] <pool> [device]\n"));
201 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
202 "\t [-O file-system-property=value] ... \n"
203 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
205 return (gettext("\tdestroy [-f] <pool>\n"));
207 return (gettext("\tdetach <pool> <device>\n"));
209 return (gettext("\texport [-f] <pool> ...\n"));
211 return (gettext("\thistory [-il] [<pool>] ...\n"));
213 return (gettext("\timport [-d dir] [-D]\n"
214 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
215 "\timport [-o mntopts] [-o property=value] ... \n"
216 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
217 "[-R root] [-F [-n]] -a\n"
218 "\timport [-o mntopts] [-o property=value] ... \n"
219 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
220 "[-R root] [-F [-n]]\n"
221 "\t <pool | id> [newpool]\n"));
223 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
226 return (gettext("\tlist [-H] [-o property[,...]] "
227 "[-T d|u] [pool] ... [interval [count]]\n"));
229 return (gettext("\toffline [-t] <pool> <device> ...\n"));
231 return (gettext("\tonline <pool> <device> ...\n"));
233 return (gettext("\treplace [-f] <pool> <device> "
236 return (gettext("\tremove <pool> <device> ...\n"));
238 return (gettext("\tscrub [-s] <pool> ...\n"));
240 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
243 return (gettext("\tupgrade\n"
245 "\tupgrade [-V version] <-a | pool ...>\n"));
247 return (gettext("\tevents [-vHfc]\n"));
249 return (gettext("\tget <\"all\" | property[,...]> "
252 return (gettext("\tset <property=value> <pool> \n"));
254 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
255 "\t [-o property=value] <pool> <newpool> "
256 "[<device> ...]\n"));
265 * Callback routine that will print out a pool property value.
268 print_prop_cb(int prop, void *cb)
272 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
274 if (zpool_prop_readonly(prop))
275 (void) fprintf(fp, " NO ");
277 (void) fprintf(fp, " YES ");
279 if (zpool_prop_values(prop) == NULL)
280 (void) fprintf(fp, "-\n");
282 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
288 * Display usage message. If we're inside a command, display only the usage for
289 * that command. Otherwise, iterate over the entire command table and display
290 * a complete usage message.
293 usage(boolean_t requested)
295 FILE *fp = requested ? stdout : stderr;
297 if (current_command == NULL) {
300 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
302 gettext("where 'command' is one of the following:\n\n"));
304 for (i = 0; i < NCOMMAND; i++) {
305 if (command_table[i].name == NULL)
306 (void) fprintf(fp, "\n");
308 (void) fprintf(fp, "%s",
309 get_usage(command_table[i].usage));
312 (void) fprintf(fp, gettext("usage:\n"));
313 (void) fprintf(fp, "%s", get_usage(current_command->usage));
316 if (current_command != NULL &&
317 ((strcmp(current_command->name, "set") == 0) ||
318 (strcmp(current_command->name, "get") == 0) ||
319 (strcmp(current_command->name, "list") == 0))) {
322 gettext("\nthe following properties are supported:\n"));
324 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
325 "PROPERTY", "EDIT", "VALUES");
327 /* Iterate over all properties */
328 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
333 * See comments at end of main().
335 if (getenv("ZFS_ABORT") != NULL) {
336 (void) printf("dumping core by request\n");
340 exit(requested ? 0 : 2);
344 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
345 boolean_t print_logs)
352 (void) printf("\t%*s%s\n", indent, "", name);
354 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
355 &child, &children) != 0)
358 for (c = 0; c < children; c++) {
359 uint64_t is_log = B_FALSE;
361 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
363 if ((is_log && !print_logs) || (!is_log && print_logs))
366 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
367 print_vdev_tree(zhp, vname, child[c], indent + 2,
374 * Add a property pair (name, string-value) into a property nvlist.
377 add_prop_list(const char *propname, char *propval, nvlist_t **props,
380 zpool_prop_t prop = ZPROP_INVAL;
386 if (*props == NULL &&
387 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
388 (void) fprintf(stderr,
389 gettext("internal error: out of memory\n"));
396 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
397 (void) fprintf(stderr, gettext("property '%s' is "
398 "not a valid pool property\n"), propname);
401 normnm = zpool_prop_to_name(prop);
403 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
404 normnm = zfs_prop_to_name(fprop);
410 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
411 prop != ZPOOL_PROP_CACHEFILE) {
412 (void) fprintf(stderr, gettext("property '%s' "
413 "specified multiple times\n"), propname);
417 if (nvlist_add_string(proplist, normnm, propval) != 0) {
418 (void) fprintf(stderr, gettext("internal "
419 "error: out of memory\n"));
427 * zpool add [-fn] <pool> <vdev> ...
429 * -f Force addition of devices, even if they appear in use
430 * -n Do not add the devices, but display the resulting layout if
431 * they were to be added.
433 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
434 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
438 zpool_do_add(int argc, char **argv)
440 boolean_t force = B_FALSE;
441 boolean_t dryrun = B_FALSE;
450 while ((c = getopt(argc, argv, "fn")) != -1) {
459 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
468 /* get pool name and check number of arguments */
470 (void) fprintf(stderr, gettext("missing pool name argument\n"));
474 (void) fprintf(stderr, gettext("missing vdev specification\n"));
483 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
486 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
487 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
493 /* pass off to get_vdev_spec for processing */
494 nvroot = make_root_vdev(zhp, NULL, force, !force, B_FALSE, dryrun,
496 if (nvroot == NULL) {
502 nvlist_t *poolnvroot;
504 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
507 (void) printf(gettext("would update '%s' to the following "
508 "configuration:\n"), zpool_get_name(zhp));
510 /* print original main pool and new tree */
511 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
512 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
514 /* Do the same for the logs */
515 if (num_logs(poolnvroot) > 0) {
516 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
517 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
518 } else if (num_logs(nvroot) > 0) {
519 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
524 ret = (zpool_add(zhp, nvroot) != 0);
534 * zpool remove <pool> <vdev> ...
536 * Removes the given vdev from the pool. Currently, this supports removing
537 * spares, cache, and log devices from the pool.
540 zpool_do_remove(int argc, char **argv)
549 /* get pool name and check number of arguments */
551 (void) fprintf(stderr, gettext("missing pool name argument\n"));
555 (void) fprintf(stderr, gettext("missing device\n"));
561 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
564 for (i = 1; i < argc; i++) {
565 if (zpool_vdev_remove(zhp, argv[i]) != 0)
573 * zpool create [-fn] [-o property=value] ...
574 * [-O file-system-property=value] ...
575 * [-R root] [-m mountpoint] <pool> <dev> ...
577 * -f Force creation, even if devices appear in use
578 * -n Do not create the pool, but display the resulting layout if it
579 * were to be created.
580 * -R Create a pool under an alternate root
581 * -m Set default mountpoint for the root dataset. By default it's
583 * -o Set property=value.
584 * -O Set fsproperty=value in the pool's root file system
586 * Creates the named pool according to the given vdev specification. The
587 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
588 * we get the nvlist back from get_vdev_spec(), we either print out the contents
589 * (if '-n' was specified), or pass it to libzfs to do the creation.
592 zpool_do_create(int argc, char **argv)
594 boolean_t force = B_FALSE;
595 boolean_t dryrun = B_FALSE;
597 nvlist_t *nvroot = NULL;
600 char *altroot = NULL;
601 char *mountpoint = NULL;
602 nvlist_t *fsprops = NULL;
603 nvlist_t *props = NULL;
607 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
617 if (add_prop_list(zpool_prop_to_name(
618 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
620 if (nvlist_lookup_string(props,
621 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
624 if (add_prop_list(zpool_prop_to_name(
625 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
632 if ((propval = strchr(optarg, '=')) == NULL) {
633 (void) fprintf(stderr, gettext("missing "
634 "'=' for -o option\n"));
640 if (add_prop_list(optarg, propval, &props, B_TRUE))
644 if ((propval = strchr(optarg, '=')) == NULL) {
645 (void) fprintf(stderr, gettext("missing "
646 "'=' for -O option\n"));
652 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
656 (void) fprintf(stderr, gettext("missing argument for "
657 "'%c' option\n"), optopt);
660 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
669 /* get pool name and check number of arguments */
671 (void) fprintf(stderr, gettext("missing pool name argument\n"));
675 (void) fprintf(stderr, gettext("missing vdev specification\n"));
682 * As a special case, check for use of '/' in the name, and direct the
683 * user to use 'zfs create' instead.
685 if (strchr(poolname, '/') != NULL) {
686 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
687 "character '/' in pool name\n"), poolname);
688 (void) fprintf(stderr, gettext("use 'zfs create' to "
689 "create a dataset\n"));
693 /* pass off to get_vdev_spec for bulk processing */
694 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
699 /* make_root_vdev() allows 0 toplevel children if there are spares */
700 if (!zfs_allocatable_devs(nvroot)) {
701 (void) fprintf(stderr, gettext("invalid vdev "
702 "specification: at least one toplevel vdev must be "
708 if (altroot != NULL && altroot[0] != '/') {
709 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
710 "must be an absolute path\n"), altroot);
715 * Check the validity of the mountpoint and direct the user to use the
716 * '-m' mountpoint option if it looks like its in use.
718 if (mountpoint == NULL ||
719 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
720 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
721 char buf[MAXPATHLEN];
724 if (mountpoint && mountpoint[0] != '/') {
725 (void) fprintf(stderr, gettext("invalid mountpoint "
726 "'%s': must be an absolute path, 'legacy', or "
727 "'none'\n"), mountpoint);
731 if (mountpoint == NULL) {
733 (void) snprintf(buf, sizeof (buf), "%s/%s",
736 (void) snprintf(buf, sizeof (buf), "/%s",
740 (void) snprintf(buf, sizeof (buf), "%s%s",
741 altroot, mountpoint);
743 (void) snprintf(buf, sizeof (buf), "%s",
747 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
748 (void) fprintf(stderr, gettext("mountpoint '%s' : "
749 "%s\n"), buf, strerror(errno));
750 (void) fprintf(stderr, gettext("use '-m' "
751 "option to provide a different default\n"));
756 while (count < 3 && readdir(dirp) != NULL)
758 (void) closedir(dirp);
761 (void) fprintf(stderr, gettext("mountpoint "
762 "'%s' exists and is not empty\n"), buf);
763 (void) fprintf(stderr, gettext("use '-m' "
764 "option to provide a "
765 "different default\n"));
773 * For a dry run invocation, print out a basic message and run
774 * through all the vdevs in the list and print out in an
775 * appropriate hierarchy.
777 (void) printf(gettext("would create '%s' with the "
778 "following layout:\n\n"), poolname);
780 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
781 if (num_logs(nvroot) > 0)
782 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
787 * Hand off to libzfs.
789 if (zpool_create(g_zfs, poolname,
790 nvroot, props, fsprops) == 0) {
791 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
792 ZFS_TYPE_FILESYSTEM);
794 if (mountpoint != NULL)
795 verify(zfs_prop_set(pool,
797 ZFS_PROP_MOUNTPOINT),
799 if (zfs_mount(pool, NULL, 0) == 0)
800 ret = zfs_shareall(pool);
803 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
804 (void) fprintf(stderr, gettext("pool name may have "
811 nvlist_free(fsprops);
815 nvlist_free(fsprops);
822 * zpool destroy <pool>
824 * -f Forcefully unmount any datasets
826 * Destroy the given pool. Automatically unmounts any datasets in the pool.
829 zpool_do_destroy(int argc, char **argv)
831 boolean_t force = B_FALSE;
838 while ((c = getopt(argc, argv, "f")) != -1) {
844 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
853 /* check arguments */
855 (void) fprintf(stderr, gettext("missing pool argument\n"));
859 (void) fprintf(stderr, gettext("too many arguments\n"));
865 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
867 * As a special case, check for use of '/' in the name, and
868 * direct the user to use 'zfs destroy' instead.
870 if (strchr(pool, '/') != NULL)
871 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
872 "destroy a dataset\n"));
876 if (zpool_disable_datasets(zhp, force) != 0) {
877 (void) fprintf(stderr, gettext("could not destroy '%s': "
878 "could not unmount datasets\n"), zpool_get_name(zhp));
882 ret = (zpool_destroy(zhp) != 0);
890 * zpool export [-f] <pool> ...
892 * -f Forcefully unmount datasets
894 * Export the given pools. By default, the command will attempt to cleanly
895 * unmount any active datasets within the pool. If the '-f' flag is specified,
896 * then the datasets will be forcefully unmounted.
899 zpool_do_export(int argc, char **argv)
901 boolean_t force = B_FALSE;
902 boolean_t hardforce = B_FALSE;
909 while ((c = getopt(argc, argv, "fF")) != -1) {
918 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
927 /* check arguments */
929 (void) fprintf(stderr, gettext("missing pool argument\n"));
934 for (i = 0; i < argc; i++) {
935 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
940 if (zpool_disable_datasets(zhp, force) != 0) {
947 if (zpool_export_force(zhp) != 0)
949 } else if (zpool_export(zhp, force) != 0) {
960 * Given a vdev configuration, determine the maximum width needed for the device
964 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
966 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
971 if (strlen(name) + depth > max)
972 max = strlen(name) + depth;
976 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
977 &child, &children) == 0) {
978 for (c = 0; c < children; c++)
979 if ((ret = max_width(zhp, child[c], depth + 2,
984 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
985 &child, &children) == 0) {
986 for (c = 0; c < children; c++)
987 if ((ret = max_width(zhp, child[c], depth + 2,
992 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
993 &child, &children) == 0) {
994 for (c = 0; c < children; c++)
995 if ((ret = max_width(zhp, child[c], depth + 2,
1004 typedef struct spare_cbdata {
1006 zpool_handle_t *cb_zhp;
1010 find_vdev(nvlist_t *nv, uint64_t search)
1016 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1020 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1021 &child, &children) == 0) {
1022 for (c = 0; c < children; c++)
1023 if (find_vdev(child[c], search))
1031 find_spare(zpool_handle_t *zhp, void *data)
1033 spare_cbdata_t *cbp = data;
1034 nvlist_t *config, *nvroot;
1036 config = zpool_get_config(zhp, NULL);
1037 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1040 if (find_vdev(nvroot, cbp->cb_guid)) {
1050 * Print out configuration state as requested by status_callback.
1053 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1054 int namewidth, int depth, boolean_t isspare)
1058 pool_scan_stat_t *ps = NULL;
1060 char rbuf[6], wbuf[6], cbuf[6];
1062 uint64_t notpresent;
1066 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1067 &child, &children) != 0)
1070 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1071 (uint64_t **)&vs, &c) == 0);
1073 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1076 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1079 if (vs->vs_aux == VDEV_AUX_SPARED)
1081 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1085 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1089 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1090 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1091 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1092 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1095 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1096 ¬present) == 0) {
1098 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1099 (void) printf(" was %s", path);
1100 } else if (vs->vs_aux != 0) {
1103 switch (vs->vs_aux) {
1104 case VDEV_AUX_OPEN_FAILED:
1105 (void) printf(gettext("cannot open"));
1108 case VDEV_AUX_BAD_GUID_SUM:
1109 (void) printf(gettext("missing device"));
1112 case VDEV_AUX_NO_REPLICAS:
1113 (void) printf(gettext("insufficient replicas"));
1116 case VDEV_AUX_VERSION_NEWER:
1117 (void) printf(gettext("newer version"));
1120 case VDEV_AUX_SPARED:
1121 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1123 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1124 if (strcmp(zpool_get_name(cb.cb_zhp),
1125 zpool_get_name(zhp)) == 0)
1126 (void) printf(gettext("currently in "
1129 (void) printf(gettext("in use by "
1131 zpool_get_name(cb.cb_zhp));
1132 zpool_close(cb.cb_zhp);
1134 (void) printf(gettext("currently in use"));
1138 case VDEV_AUX_ERR_EXCEEDED:
1139 (void) printf(gettext("too many errors"));
1142 case VDEV_AUX_IO_FAILURE:
1143 (void) printf(gettext("experienced I/O failures"));
1146 case VDEV_AUX_BAD_LOG:
1147 (void) printf(gettext("bad intent log"));
1150 case VDEV_AUX_EXTERNAL:
1151 (void) printf(gettext("external device fault"));
1154 case VDEV_AUX_SPLIT_POOL:
1155 (void) printf(gettext("split into new pool"));
1159 (void) printf(gettext("corrupted data"));
1164 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1165 (uint64_t **)&ps, &c);
1167 if (ps && ps->pss_state == DSS_SCANNING &&
1168 vs->vs_scan_processed != 0 && children == 0) {
1169 (void) printf(gettext(" (%s)"),
1170 (ps->pss_func == POOL_SCAN_RESILVER) ?
1171 "resilvering" : "repairing");
1174 (void) printf("\n");
1176 for (c = 0; c < children; c++) {
1177 uint64_t islog = B_FALSE, ishole = B_FALSE;
1179 /* Don't print logs or holes here */
1180 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1182 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1184 if (islog || ishole)
1186 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1187 print_status_config(zhp, vname, child[c],
1188 namewidth, depth + 2, isspare);
1195 * Print the configuration of an exported pool. Iterate over all vdevs in the
1196 * pool, printing out the name and status for each one.
1199 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1206 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1207 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1208 strcmp(type, VDEV_TYPE_HOLE) == 0)
1211 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1212 (uint64_t **)&vs, &c) == 0);
1214 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1215 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1217 if (vs->vs_aux != 0) {
1220 switch (vs->vs_aux) {
1221 case VDEV_AUX_OPEN_FAILED:
1222 (void) printf(gettext("cannot open"));
1225 case VDEV_AUX_BAD_GUID_SUM:
1226 (void) printf(gettext("missing device"));
1229 case VDEV_AUX_NO_REPLICAS:
1230 (void) printf(gettext("insufficient replicas"));
1233 case VDEV_AUX_VERSION_NEWER:
1234 (void) printf(gettext("newer version"));
1237 case VDEV_AUX_ERR_EXCEEDED:
1238 (void) printf(gettext("too many errors"));
1242 (void) printf(gettext("corrupted data"));
1246 (void) printf("\n");
1248 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1249 &child, &children) != 0)
1252 for (c = 0; c < children; c++) {
1253 uint64_t is_log = B_FALSE;
1255 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1260 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1261 print_import_config(vname, child[c], namewidth, depth + 2);
1265 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1266 &child, &children) == 0) {
1267 (void) printf(gettext("\tcache\n"));
1268 for (c = 0; c < children; c++) {
1269 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1270 (void) printf("\t %s\n", vname);
1275 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1276 &child, &children) == 0) {
1277 (void) printf(gettext("\tspares\n"));
1278 for (c = 0; c < children; c++) {
1279 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1280 (void) printf("\t %s\n", vname);
1288 * Logs are recorded as top level vdevs in the main pool child array
1289 * but with "is_log" set to 1. We use either print_status_config() or
1290 * print_import_config() to print the top level logs then any log
1291 * children (eg mirrored slogs) are printed recursively - which
1292 * works because only the top level vdev is marked "is_log"
1295 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1300 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1304 (void) printf(gettext("\tlogs\n"));
1306 for (c = 0; c < children; c++) {
1307 uint64_t is_log = B_FALSE;
1310 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1314 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1316 print_status_config(zhp, name, child[c], namewidth,
1319 print_import_config(name, child[c], namewidth, 2);
1325 * Display the status for the given pool.
1328 show_import(nvlist_t *config)
1330 uint64_t pool_state;
1341 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1343 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1345 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1347 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1350 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1351 (uint64_t **)&vs, &vsc) == 0);
1352 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1354 reason = zpool_import_status(config, &msgid);
1356 (void) printf(gettext(" pool: %s\n"), name);
1357 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1358 (void) printf(gettext(" state: %s"), health);
1359 if (pool_state == POOL_STATE_DESTROYED)
1360 (void) printf(gettext(" (DESTROYED)"));
1361 (void) printf("\n");
1364 case ZPOOL_STATUS_MISSING_DEV_R:
1365 case ZPOOL_STATUS_MISSING_DEV_NR:
1366 case ZPOOL_STATUS_BAD_GUID_SUM:
1367 (void) printf(gettext("status: One or more devices are missing "
1368 "from the system.\n"));
1371 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1372 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1373 (void) printf(gettext("status: One or more devices contains "
1374 "corrupted data.\n"));
1377 case ZPOOL_STATUS_CORRUPT_DATA:
1378 (void) printf(gettext("status: The pool data is corrupted.\n"));
1381 case ZPOOL_STATUS_OFFLINE_DEV:
1382 (void) printf(gettext("status: One or more devices "
1383 "are offlined.\n"));
1386 case ZPOOL_STATUS_CORRUPT_POOL:
1387 (void) printf(gettext("status: The pool metadata is "
1391 case ZPOOL_STATUS_VERSION_OLDER:
1392 (void) printf(gettext("status: The pool is formatted using an "
1393 "older on-disk version.\n"));
1396 case ZPOOL_STATUS_VERSION_NEWER:
1397 (void) printf(gettext("status: The pool is formatted using an "
1398 "incompatible version.\n"));
1401 case ZPOOL_STATUS_HOSTID_MISMATCH:
1402 (void) printf(gettext("status: The pool was last accessed by "
1403 "another system.\n"));
1406 case ZPOOL_STATUS_FAULTED_DEV_R:
1407 case ZPOOL_STATUS_FAULTED_DEV_NR:
1408 (void) printf(gettext("status: One or more devices are "
1412 case ZPOOL_STATUS_BAD_LOG:
1413 (void) printf(gettext("status: An intent log record cannot be "
1417 case ZPOOL_STATUS_RESILVERING:
1418 (void) printf(gettext("status: One or more devices were being "
1424 * No other status can be seen when importing pools.
1426 assert(reason == ZPOOL_STATUS_OK);
1430 * Print out an action according to the overall state of the pool.
1432 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1433 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1434 (void) printf(gettext("action: The pool can be "
1435 "imported using its name or numeric identifier, "
1436 "though\n\tsome features will not be available "
1437 "without an explicit 'zpool upgrade'.\n"));
1438 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1439 (void) printf(gettext("action: The pool can be "
1440 "imported using its name or numeric "
1441 "identifier and\n\tthe '-f' flag.\n"));
1443 (void) printf(gettext("action: The pool can be "
1444 "imported using its name or numeric "
1446 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1447 (void) printf(gettext("action: The pool can be imported "
1448 "despite missing or damaged devices. The\n\tfault "
1449 "tolerance of the pool may be compromised if imported.\n"));
1452 case ZPOOL_STATUS_VERSION_NEWER:
1453 (void) printf(gettext("action: The pool cannot be "
1454 "imported. Access the pool on a system running "
1455 "newer\n\tsoftware, or recreate the pool from "
1458 case ZPOOL_STATUS_MISSING_DEV_R:
1459 case ZPOOL_STATUS_MISSING_DEV_NR:
1460 case ZPOOL_STATUS_BAD_GUID_SUM:
1461 (void) printf(gettext("action: The pool cannot be "
1462 "imported. Attach the missing\n\tdevices and try "
1466 (void) printf(gettext("action: The pool cannot be "
1467 "imported due to damaged devices or data.\n"));
1472 * If the state is "closed" or "can't open", and the aux state
1473 * is "corrupt data":
1475 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1476 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1477 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1478 if (pool_state == POOL_STATE_DESTROYED)
1479 (void) printf(gettext("\tThe pool was destroyed, "
1480 "but can be imported using the '-Df' flags.\n"));
1481 else if (pool_state != POOL_STATE_EXPORTED)
1482 (void) printf(gettext("\tThe pool may be active on "
1483 "another system, but can be imported using\n\t"
1484 "the '-f' flag.\n"));
1488 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1491 (void) printf(gettext("config:\n\n"));
1493 namewidth = max_width(NULL, nvroot, 0, 0);
1497 print_import_config(name, nvroot, namewidth, 0);
1498 if (num_logs(nvroot) > 0)
1499 print_logs(NULL, nvroot, namewidth, B_FALSE);
1501 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1502 (void) printf(gettext("\n\tAdditional devices are known to "
1503 "be part of this pool, though their\n\texact "
1504 "configuration cannot be determined.\n"));
1509 * Perform the import for the given configuration. This passes the heavy
1510 * lifting off to zpool_import_props(), and then mounts the datasets contained
1514 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1515 nvlist_t *props, int flags)
1517 zpool_handle_t *zhp;
1522 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1525 verify(nvlist_lookup_uint64(config,
1526 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1527 verify(nvlist_lookup_uint64(config,
1528 ZPOOL_CONFIG_VERSION, &version) == 0);
1529 if (version > SPA_VERSION) {
1530 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1531 "is formatted using a newer ZFS version\n"), name);
1533 } else if (state != POOL_STATE_EXPORTED &&
1534 !(flags & ZFS_IMPORT_ANY_HOST)) {
1537 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1539 unsigned long system_hostid = gethostid() & 0xffffffff;
1541 if ((unsigned long)hostid != system_hostid) {
1546 verify(nvlist_lookup_string(config,
1547 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1548 verify(nvlist_lookup_uint64(config,
1549 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1551 (void) fprintf(stderr, gettext("cannot import "
1552 "'%s': pool may be in use from other "
1553 "system, it was last accessed by %s "
1554 "(hostid: 0x%lx) on %s"), name, hostname,
1555 (unsigned long)hostid,
1556 asctime(localtime(&t)));
1557 (void) fprintf(stderr, gettext("use '-f' to "
1558 "import anyway\n"));
1562 (void) fprintf(stderr, gettext("cannot import '%s': "
1563 "pool may be in use from other system\n"), name);
1564 (void) fprintf(stderr, gettext("use '-f' to import "
1570 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1573 if (newname != NULL)
1574 name = (char *)newname;
1576 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1579 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1580 !(flags & ZFS_IMPORT_ONLY) &&
1581 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1591 * zpool import [-d dir] [-D]
1592 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1593 * [-d dir | -c cachefile] [-f] -a
1594 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1595 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1597 * -c Read pool information from a cachefile instead of searching
1600 * -d Scan in a specific directory, other than /dev/. More than
1601 * one directory can be specified using multiple '-d' options.
1603 * -D Scan for previously destroyed pools or import all or only
1604 * specified destroyed pools.
1606 * -R Temporarily import the pool, with all mountpoints relative to
1607 * the given root. The pool will remain exported when the machine
1610 * -V Import even in the presence of faulted vdevs. This is an
1611 * intentionally undocumented option for testing purposes, and
1612 * treats the pool configuration as complete, leaving any bad
1613 * vdevs in the FAULTED state. In other words, it does verbatim
1616 * -f Force import, even if it appears that the pool is active.
1618 * -F Attempt rewind if necessary.
1620 * -n See if rewind would work, but don't actually rewind.
1622 * -N Import the pool but don't mount datasets.
1624 * -T Specify a starting txg to use for import. This option is
1625 * intentionally undocumented option for testing purposes.
1627 * -a Import all pools found.
1629 * -o Set property=value and/or temporary mount options (without '=').
1631 * The import command scans for pools to import, and import pools based on pool
1632 * name and GUID. The pool can also be renamed as part of the import process.
1635 zpool_do_import(int argc, char **argv)
1637 char **searchdirs = NULL;
1641 nvlist_t *pools = NULL;
1642 boolean_t do_all = B_FALSE;
1643 boolean_t do_destroyed = B_FALSE;
1644 char *mntopts = NULL;
1647 uint64_t searchguid = 0;
1648 char *searchname = NULL;
1650 nvlist_t *found_config;
1651 nvlist_t *policy = NULL;
1652 nvlist_t *props = NULL;
1654 int flags = ZFS_IMPORT_NORMAL;
1655 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1656 boolean_t dryrun = B_FALSE;
1657 boolean_t do_rewind = B_FALSE;
1658 boolean_t xtreme_rewind = B_FALSE;
1659 uint64_t pool_state, txg = -1ULL;
1660 char *cachefile = NULL;
1661 importargs_t idata = { 0 };
1665 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1674 if (searchdirs == NULL) {
1675 searchdirs = safe_malloc(sizeof (char *));
1677 char **tmp = safe_malloc((nsearch + 1) *
1679 bcopy(searchdirs, tmp, nsearch *
1684 searchdirs[nsearch++] = optarg;
1687 do_destroyed = B_TRUE;
1690 flags |= ZFS_IMPORT_ANY_HOST;
1696 flags |= ZFS_IMPORT_MISSING_LOG;
1702 flags |= ZFS_IMPORT_ONLY;
1705 if ((propval = strchr(optarg, '=')) != NULL) {
1708 if (add_prop_list(optarg, propval,
1716 if (add_prop_list(zpool_prop_to_name(
1717 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1719 if (nvlist_lookup_string(props,
1720 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1723 if (add_prop_list(zpool_prop_to_name(
1724 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1729 txg = strtoull(optarg, &endptr, 10);
1730 if (errno != 0 || *endptr != '\0') {
1731 (void) fprintf(stderr,
1732 gettext("invalid txg value\n"));
1735 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1738 flags |= ZFS_IMPORT_VERBATIM;
1741 xtreme_rewind = B_TRUE;
1744 (void) fprintf(stderr, gettext("missing argument for "
1745 "'%c' option\n"), optopt);
1749 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1758 if (cachefile && nsearch != 0) {
1759 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1763 if ((dryrun || xtreme_rewind) && !do_rewind) {
1764 (void) fprintf(stderr,
1765 gettext("-n or -X only meaningful with -F\n"));
1769 rewind_policy = ZPOOL_TRY_REWIND;
1771 rewind_policy = ZPOOL_DO_REWIND;
1773 rewind_policy |= ZPOOL_EXTREME_REWIND;
1775 /* In the future, we can capture further policy and include it here */
1776 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1777 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1778 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1781 /* check argument count */
1784 (void) fprintf(stderr, gettext("too many arguments\n"));
1789 (void) fprintf(stderr, gettext("too many arguments\n"));
1794 * Check for the SYS_CONFIG privilege. We do this explicitly
1795 * here because otherwise any attempt to discover pools will
1798 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1799 (void) fprintf(stderr, gettext("cannot "
1800 "discover pools: permission denied\n"));
1801 if (searchdirs != NULL)
1804 nvlist_free(policy);
1810 * Depending on the arguments given, we do one of the following:
1812 * <none> Iterate through all pools and display information about
1815 * -a Iterate through all pools and try to import each one.
1817 * <id> Find the pool that corresponds to the given GUID/pool
1818 * name and import that one.
1820 * -D Above options applies only to destroyed pools.
1826 searchguid = strtoull(argv[0], &endptr, 10);
1827 if (errno != 0 || *endptr != '\0')
1828 searchname = argv[0];
1829 found_config = NULL;
1832 * User specified a name or guid. Ensure it's unique.
1834 idata.unique = B_TRUE;
1838 idata.path = searchdirs;
1839 idata.paths = nsearch;
1840 idata.poolname = searchname;
1841 idata.guid = searchguid;
1842 idata.cachefile = cachefile;
1844 pools = zpool_search_import(g_zfs, &idata);
1846 if (pools != NULL && idata.exists &&
1847 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1848 (void) fprintf(stderr, gettext("cannot import '%s': "
1849 "a pool with that name already exists\n"),
1851 (void) fprintf(stderr, gettext("use the form '%s "
1852 "<pool | id> <newpool>' to give it a new name\n"),
1855 } else if (pools == NULL && idata.exists) {
1856 (void) fprintf(stderr, gettext("cannot import '%s': "
1857 "a pool with that name is already created/imported,\n"),
1859 (void) fprintf(stderr, gettext("and no additional pools "
1860 "with that name were found\n"));
1862 } else if (pools == NULL) {
1864 (void) fprintf(stderr, gettext("cannot import '%s': "
1865 "no such pool available\n"), argv[0]);
1871 if (searchdirs != NULL)
1873 nvlist_free(policy);
1878 * At this point we have a list of import candidate configs. Even if
1879 * we were searching by pool name or guid, we still need to
1880 * post-process the list to deal with pool state and possible
1886 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1888 verify(nvpair_value_nvlist(elem, &config) == 0);
1890 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1892 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1894 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1897 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1904 (void) printf("\n");
1907 err |= do_import(config, NULL, mntopts,
1910 show_import(config);
1912 } else if (searchname != NULL) {
1916 * We are searching for a pool based on name.
1918 verify(nvlist_lookup_string(config,
1919 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1921 if (strcmp(name, searchname) == 0) {
1922 if (found_config != NULL) {
1923 (void) fprintf(stderr, gettext(
1924 "cannot import '%s': more than "
1925 "one matching pool\n"), searchname);
1926 (void) fprintf(stderr, gettext(
1927 "import by numeric ID instead\n"));
1930 found_config = config;
1936 * Search for a pool by guid.
1938 verify(nvlist_lookup_uint64(config,
1939 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1941 if (guid == searchguid)
1942 found_config = config;
1947 * If we were searching for a specific pool, verify that we found a
1948 * pool, and then do the import.
1950 if (argc != 0 && err == 0) {
1951 if (found_config == NULL) {
1952 (void) fprintf(stderr, gettext("cannot import '%s': "
1953 "no such pool available\n"), argv[0]);
1956 err |= do_import(found_config, argc == 1 ? NULL :
1957 argv[1], mntopts, props, flags);
1962 * If we were just looking for pools, report an error if none were
1965 if (argc == 0 && first)
1966 (void) fprintf(stderr,
1967 gettext("no pools available to import\n"));
1972 nvlist_free(policy);
1973 if (searchdirs != NULL)
1976 return (err ? 1 : 0);
1979 typedef struct iostat_cbdata {
1980 zpool_list_t *cb_list;
1987 print_iostat_separator(iostat_cbdata_t *cb)
1991 for (i = 0; i < cb->cb_namewidth; i++)
1993 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1997 print_iostat_header(iostat_cbdata_t *cb)
1999 (void) printf("%*s capacity operations bandwidth\n",
2000 cb->cb_namewidth, "");
2001 (void) printf("%-*s alloc free read write read write\n",
2002 cb->cb_namewidth, "pool");
2003 print_iostat_separator(cb);
2007 * Display a single statistic.
2010 print_one_stat(uint64_t value)
2014 zfs_nicenum(value, buf, sizeof (buf));
2015 (void) printf(" %5s", buf);
2019 * Print out all the statistics for the given vdev. This can either be the
2020 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2021 * is a verbose output, and we don't want to display the toplevel pool stats.
2024 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2025 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2027 nvlist_t **oldchild, **newchild;
2029 vdev_stat_t *oldvs, *newvs;
2030 vdev_stat_t zerovs = { 0 };
2035 if (oldnv != NULL) {
2036 verify(nvlist_lookup_uint64_array(oldnv,
2037 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2042 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2043 (uint64_t **)&newvs, &c) == 0);
2045 if (strlen(name) + depth > cb->cb_namewidth)
2046 (void) printf("%*s%s", depth, "", name);
2048 (void) printf("%*s%s%*s", depth, "", name,
2049 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2051 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2056 scale = (double)NANOSEC / tdelta;
2058 /* only toplevel vdevs have capacity stats */
2059 if (newvs->vs_space == 0) {
2060 (void) printf(" - -");
2062 print_one_stat(newvs->vs_alloc);
2063 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2066 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2067 oldvs->vs_ops[ZIO_TYPE_READ])));
2069 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2070 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2072 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2073 oldvs->vs_bytes[ZIO_TYPE_READ])));
2075 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2076 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2078 (void) printf("\n");
2080 if (!cb->cb_verbose)
2083 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2084 &newchild, &children) != 0)
2087 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2088 &oldchild, &c) != 0)
2091 for (c = 0; c < children; c++) {
2092 uint64_t ishole = B_FALSE, islog = B_FALSE;
2094 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2097 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2100 if (ishole || islog)
2103 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2104 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2105 newchild[c], cb, depth + 2);
2110 * Log device section
2113 if (num_logs(newnv) > 0) {
2114 (void) printf("%-*s - - - - - "
2115 "-\n", cb->cb_namewidth, "logs");
2117 for (c = 0; c < children; c++) {
2118 uint64_t islog = B_FALSE;
2119 (void) nvlist_lookup_uint64(newchild[c],
2120 ZPOOL_CONFIG_IS_LOG, &islog);
2123 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2125 print_vdev_stats(zhp, vname, oldnv ?
2126 oldchild[c] : NULL, newchild[c],
2135 * Include level 2 ARC devices in iostat output
2137 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2138 &newchild, &children) != 0)
2141 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2142 &oldchild, &c) != 0)
2146 (void) printf("%-*s - - - - - "
2147 "-\n", cb->cb_namewidth, "cache");
2148 for (c = 0; c < children; c++) {
2149 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2151 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2152 newchild[c], cb, depth + 2);
2159 refresh_iostat(zpool_handle_t *zhp, void *data)
2161 iostat_cbdata_t *cb = data;
2165 * If the pool has disappeared, remove it from the list and continue.
2167 if (zpool_refresh_stats(zhp, &missing) != 0)
2171 pool_list_remove(cb->cb_list, zhp);
2177 * Callback to print out the iostats for the given pool.
2180 print_iostat(zpool_handle_t *zhp, void *data)
2182 iostat_cbdata_t *cb = data;
2183 nvlist_t *oldconfig, *newconfig;
2184 nvlist_t *oldnvroot, *newnvroot;
2186 newconfig = zpool_get_config(zhp, &oldconfig);
2188 if (cb->cb_iteration == 1)
2191 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2194 if (oldconfig == NULL)
2197 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2201 * Print out the statistics for the pool.
2203 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2206 print_iostat_separator(cb);
2218 if (isatty(STDOUT_FILENO)) {
2219 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2221 columns = ws.ws_col;
2230 get_namewidth(zpool_handle_t *zhp, void *data)
2232 iostat_cbdata_t *cb = data;
2233 nvlist_t *config, *nvroot;
2236 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2237 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2239 if (!cb->cb_verbose)
2240 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2242 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2247 * The width must be at least 10, but may be as large as the
2248 * column width - 42 so that we can still fit in one line.
2250 columns = get_columns();
2252 if (cb->cb_namewidth < 10)
2253 cb->cb_namewidth = 10;
2254 if (cb->cb_namewidth > columns - 42)
2255 cb->cb_namewidth = columns - 42;
2261 * Parse the input string, get the 'interval' and 'count' value if there is one.
2264 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2267 unsigned long interval = 0, count = 0;
2271 * Determine if the last argument is an integer or a pool name
2273 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2277 interval = strtoul(argv[argc - 1], &end, 10);
2279 if (*end == '\0' && errno == 0) {
2280 if (interval == 0) {
2281 (void) fprintf(stderr, gettext("interval "
2282 "cannot be zero\n"));
2286 * Ignore the last parameter
2291 * If this is not a valid number, just plow on. The
2292 * user will get a more informative error message later
2300 * If the last argument is also an integer, then we have both a count
2303 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2308 interval = strtoul(argv[argc - 1], &end, 10);
2310 if (*end == '\0' && errno == 0) {
2311 if (interval == 0) {
2312 (void) fprintf(stderr, gettext("interval "
2313 "cannot be zero\n"));
2318 * Ignore the last parameter
2332 get_timestamp_arg(char c)
2335 timestamp_fmt = UDATE;
2337 timestamp_fmt = DDATE;
2343 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2345 * -v Display statistics for individual vdevs
2346 * -T Display a timestamp in date(1) or Unix format
2348 * This command can be tricky because we want to be able to deal with pool
2349 * creation/destruction as well as vdev configuration changes. The bulk of this
2350 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2351 * on pool_list_update() to detect the addition of new pools. Configuration
2352 * changes are all handled within libzfs.
2355 zpool_do_iostat(int argc, char **argv)
2360 unsigned long interval = 0, count = 0;
2362 boolean_t verbose = B_FALSE;
2366 while ((c = getopt(argc, argv, "T:v")) != -1) {
2369 get_timestamp_arg(*optarg);
2375 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2384 get_interval_count(&argc, argv, &interval, &count);
2387 * Construct the list of all interesting pools.
2390 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2393 if (pool_list_count(list) == 0 && argc != 0) {
2394 pool_list_free(list);
2398 if (pool_list_count(list) == 0 && interval == 0) {
2399 pool_list_free(list);
2400 (void) fprintf(stderr, gettext("no pools available\n"));
2405 * Enter the main iostat loop.
2408 cb.cb_verbose = verbose;
2409 cb.cb_iteration = 0;
2410 cb.cb_namewidth = 0;
2413 pool_list_update(list);
2415 if ((npools = pool_list_count(list)) == 0)
2416 (void) fprintf(stderr, gettext("no pools available\n"));
2419 * Refresh all statistics. This is done as an
2420 * explicit step before calculating the maximum name
2421 * width, so that any * configuration changes are
2422 * properly accounted for.
2424 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2428 * Iterate over all pools to determine the maximum width
2429 * for the pool / device name column across all pools.
2431 cb.cb_namewidth = 0;
2432 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2435 if (timestamp_fmt != NODATE)
2436 print_timestamp(timestamp_fmt);
2439 * If it's the first time, or verbose mode, print the
2442 if (++cb.cb_iteration == 1 || verbose)
2443 print_iostat_header(&cb);
2445 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2448 * If there's more than one pool, and we're not in
2449 * verbose mode (which prints a separator for us),
2450 * then print a separator.
2452 if (npools > 1 && !verbose)
2453 print_iostat_separator(&cb);
2456 (void) printf("\n");
2460 * Flush the output so that redirection to a file isn't buffered
2463 (void) fflush(stdout);
2468 if (count != 0 && --count == 0)
2471 (void) sleep(interval);
2474 pool_list_free(list);
2479 typedef struct list_cbdata {
2480 boolean_t cb_scripted;
2482 zprop_list_t *cb_proplist;
2486 * Given a list of columns to display, output appropriate headers for each one.
2489 print_header(zprop_list_t *pl)
2492 boolean_t first = B_TRUE;
2493 boolean_t right_justify;
2495 for (; pl != NULL; pl = pl->pl_next) {
2496 if (pl->pl_prop == ZPROP_INVAL)
2504 header = zpool_prop_column_name(pl->pl_prop);
2505 right_justify = zpool_prop_align_right(pl->pl_prop);
2507 if (pl->pl_next == NULL && !right_justify)
2508 (void) printf("%s", header);
2509 else if (right_justify)
2510 (void) printf("%*s", (int)pl->pl_width, header);
2512 (void) printf("%-*s", (int)pl->pl_width, header);
2515 (void) printf("\n");
2519 * Given a pool and a list of properties, print out all the properties according
2520 * to the described layout.
2523 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2525 boolean_t first = B_TRUE;
2526 char property[ZPOOL_MAXPROPLEN];
2528 boolean_t right_justify;
2531 for (; pl != NULL; pl = pl->pl_next) {
2534 (void) printf("\t");
2541 right_justify = B_FALSE;
2542 if (pl->pl_prop != ZPROP_INVAL) {
2543 if (zpool_get_prop(zhp, pl->pl_prop, property,
2544 sizeof (property), NULL) != 0)
2549 right_justify = zpool_prop_align_right(pl->pl_prop);
2554 width = pl->pl_width;
2557 * If this is being called in scripted mode, or if this is the
2558 * last column and it is left-justified, don't include a width
2561 if (scripted || (pl->pl_next == NULL && !right_justify))
2562 (void) printf("%s", propstr);
2563 else if (right_justify)
2564 (void) printf("%*s", width, propstr);
2566 (void) printf("%-*s", width, propstr);
2569 (void) printf("\n");
2573 * Generic callback function to list a pool.
2576 list_callback(zpool_handle_t *zhp, void *data)
2578 list_cbdata_t *cbp = data;
2580 if (cbp->cb_first) {
2581 if (!cbp->cb_scripted)
2582 print_header(cbp->cb_proplist);
2583 cbp->cb_first = B_FALSE;
2586 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2592 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2594 * -H Scripted mode. Don't display headers, and separate properties
2596 * -o List of properties to display. Defaults to
2597 * "name,size,allocated,free,capacity,health,altroot"
2598 * -T Display a timestamp in date(1) or Unix format
2600 * List all pools in the system, whether or not they're healthy. Output space
2601 * statistics for each one, as well as health status summary.
2604 zpool_do_list(int argc, char **argv)
2608 list_cbdata_t cb = { 0 };
2609 static char default_props[] =
2610 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2611 char *props = default_props;
2612 unsigned long interval = 0, count = 0;
2615 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2618 cb.cb_scripted = B_TRUE;
2624 get_timestamp_arg(*optarg);
2627 (void) fprintf(stderr, gettext("missing argument for "
2628 "'%c' option\n"), optopt);
2632 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2641 get_interval_count(&argc, argv, &interval, &count);
2643 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2646 cb.cb_first = B_TRUE;
2650 if (timestamp_fmt != NODATE)
2651 print_timestamp(timestamp_fmt);
2653 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2654 list_callback, &cb);
2656 if (argc == 0 && cb.cb_first)
2657 (void) fprintf(stderr, gettext("no pools available\n"));
2658 else if (argc && cb.cb_first) {
2659 /* cannot open the given pool */
2660 zprop_free_list(cb.cb_proplist);
2667 if (count != 0 && --count == 0)
2670 (void) sleep(interval);
2673 zprop_free_list(cb.cb_proplist);
2678 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2680 boolean_t force = B_FALSE;
2683 char *poolname, *old_disk, *new_disk;
2684 zpool_handle_t *zhp;
2688 while ((c = getopt(argc, argv, "f")) != -1) {
2694 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2703 /* get pool name and check number of arguments */
2705 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2712 (void) fprintf(stderr,
2713 gettext("missing <device> specification\n"));
2721 (void) fprintf(stderr,
2722 gettext("missing <new_device> specification\n"));
2725 new_disk = old_disk;
2735 (void) fprintf(stderr, gettext("too many arguments\n"));
2739 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2742 if (zpool_get_config(zhp, NULL) == NULL) {
2743 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2749 nvroot = make_root_vdev(zhp, NULL, force, B_FALSE, replacing, B_FALSE,
2751 if (nvroot == NULL) {
2756 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2758 nvlist_free(nvroot);
2765 * zpool replace [-f] <pool> <device> <new_device>
2767 * -f Force attach, even if <new_device> appears to be in use.
2769 * Replace <device> with <new_device>.
2773 zpool_do_replace(int argc, char **argv)
2775 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2779 * zpool attach [-f] <pool> <device> <new_device>
2781 * -f Force attach, even if <new_device> appears to be in use.
2783 * Attach <new_device> to the mirror containing <device>. If <device> is not
2784 * part of a mirror, then <device> will be transformed into a mirror of
2785 * <device> and <new_device>. In either case, <new_device> will begin life
2786 * with a DTL of [0, now], and will immediately begin to resilver itself.
2789 zpool_do_attach(int argc, char **argv)
2791 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2795 * zpool detach [-f] <pool> <device>
2797 * -f Force detach of <device>, even if DTLs argue against it
2798 * (not supported yet)
2800 * Detach a device from a mirror. The operation will be refused if <device>
2801 * is the last device in the mirror, or if the DTLs indicate that this device
2802 * has the only valid copy of some data.
2806 zpool_do_detach(int argc, char **argv)
2809 char *poolname, *path;
2810 zpool_handle_t *zhp;
2814 while ((c = getopt(argc, argv, "f")) != -1) {
2818 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2827 /* get pool name and check number of arguments */
2829 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2834 (void) fprintf(stderr,
2835 gettext("missing <device> specification\n"));
2842 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2845 ret = zpool_vdev_detach(zhp, path);
2853 * zpool split [-n] [-o prop=val] ...
2855 * [-R altroot] <pool> <newpool> [<device> ...]
2857 * -n Do not split the pool, but display the resulting layout if
2858 * it were to be split.
2859 * -o Set property=value, or set mount options.
2860 * -R Mount the split-off pool under an alternate root.
2862 * Splits the named pool and gives it the new pool name. Devices to be split
2863 * off may be listed, provided that no more than one device is specified
2864 * per top-level vdev mirror. The newly split pool is left in an exported
2865 * state unless -R is specified.
2867 * Restrictions: the top-level of the pool pool must only be made up of
2868 * mirrors; all devices in the pool must be healthy; no device may be
2869 * undergoing a resilvering operation.
2872 zpool_do_split(int argc, char **argv)
2874 char *srcpool, *newpool, *propval;
2875 char *mntopts = NULL;
2878 zpool_handle_t *zhp;
2879 nvlist_t *config, *props = NULL;
2881 flags.dryrun = B_FALSE;
2882 flags.import = B_FALSE;
2885 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
2888 flags.import = B_TRUE;
2890 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
2891 &props, B_TRUE) != 0) {
2898 flags.dryrun = B_TRUE;
2901 if ((propval = strchr(optarg, '=')) != NULL) {
2904 if (add_prop_list(optarg, propval,
2905 &props, B_TRUE) != 0) {
2915 (void) fprintf(stderr, gettext("missing argument for "
2916 "'%c' option\n"), optopt);
2920 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2927 if (!flags.import && mntopts != NULL) {
2928 (void) fprintf(stderr, gettext("setting mntopts is only "
2929 "valid when importing the pool\n"));
2937 (void) fprintf(stderr, gettext("Missing pool name\n"));
2941 (void) fprintf(stderr, gettext("Missing new pool name\n"));
2951 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
2954 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
2955 if (config == NULL) {
2959 (void) printf(gettext("would create '%s' with the "
2960 "following layout:\n\n"), newpool);
2961 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
2963 nvlist_free(config);
2968 if (ret != 0 || flags.dryrun || !flags.import)
2972 * The split was successful. Now we need to open the new
2973 * pool and import it.
2975 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
2977 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2978 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2980 (void) fprintf(stderr, gettext("Split was succssful, but "
2981 "the datasets could not all be mounted\n"));
2982 (void) fprintf(stderr, gettext("Try doing '%s' with a "
2983 "different altroot\n"), "zpool import");
2993 * zpool online <pool> <device> ...
2996 zpool_do_online(int argc, char **argv)
3000 zpool_handle_t *zhp;
3002 vdev_state_t newstate;
3006 while ((c = getopt(argc, argv, "et")) != -1) {
3009 flags |= ZFS_ONLINE_EXPAND;
3013 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3022 /* get pool name and check number of arguments */
3024 (void) fprintf(stderr, gettext("missing pool name\n"));
3028 (void) fprintf(stderr, gettext("missing device name\n"));
3034 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3037 for (i = 1; i < argc; i++) {
3038 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3039 if (newstate != VDEV_STATE_HEALTHY) {
3040 (void) printf(gettext("warning: device '%s' "
3041 "onlined, but remains in faulted state\n"),
3043 if (newstate == VDEV_STATE_FAULTED)
3044 (void) printf(gettext("use 'zpool "
3045 "clear' to restore a faulted "
3048 (void) printf(gettext("use 'zpool "
3049 "replace' to replace devices "
3050 "that are no longer present\n"));
3063 * zpool offline [-ft] <pool> <device> ...
3065 * -f Force the device into the offline state, even if doing
3066 * so would appear to compromise pool availability.
3067 * (not supported yet)
3069 * -t Only take the device off-line temporarily. The offline
3070 * state will not be persistent across reboots.
3074 zpool_do_offline(int argc, char **argv)
3078 zpool_handle_t *zhp;
3080 boolean_t istmp = B_FALSE;
3083 while ((c = getopt(argc, argv, "ft")) != -1) {
3090 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3099 /* get pool name and check number of arguments */
3101 (void) fprintf(stderr, gettext("missing pool name\n"));
3105 (void) fprintf(stderr, gettext("missing device name\n"));
3111 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3114 for (i = 1; i < argc; i++) {
3115 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3125 * zpool clear <pool> [device]
3127 * Clear all errors associated with a pool or a particular device.
3130 zpool_do_clear(int argc, char **argv)
3134 boolean_t dryrun = B_FALSE;
3135 boolean_t do_rewind = B_FALSE;
3136 boolean_t xtreme_rewind = B_FALSE;
3137 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3138 nvlist_t *policy = NULL;
3139 zpool_handle_t *zhp;
3140 char *pool, *device;
3143 while ((c = getopt(argc, argv, "FnX")) != -1) {
3152 xtreme_rewind = B_TRUE;
3155 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3165 (void) fprintf(stderr, gettext("missing pool name\n"));
3170 (void) fprintf(stderr, gettext("too many arguments\n"));
3174 if ((dryrun || xtreme_rewind) && !do_rewind) {
3175 (void) fprintf(stderr,
3176 gettext("-n or -X only meaningful with -F\n"));
3180 rewind_policy = ZPOOL_TRY_REWIND;
3182 rewind_policy = ZPOOL_DO_REWIND;
3184 rewind_policy |= ZPOOL_EXTREME_REWIND;
3186 /* In future, further rewind policy choices can be passed along here */
3187 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3188 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3192 device = argc == 2 ? argv[1] : NULL;
3194 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3195 nvlist_free(policy);
3199 if (zpool_clear(zhp, device, policy) != 0)
3204 nvlist_free(policy);
3209 typedef struct scrub_cbdata {
3216 scrub_callback(zpool_handle_t *zhp, void *data)
3218 scrub_cbdata_t *cb = data;
3222 * Ignore faulted pools.
3224 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3225 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3226 "currently unavailable\n"), zpool_get_name(zhp));
3230 err = zpool_scan(zhp, cb->cb_type);
3236 * zpool scrub [-s] <pool> ...
3238 * -s Stop. Stops any in-progress scrub.
3241 zpool_do_scrub(int argc, char **argv)
3246 cb.cb_type = POOL_SCAN_SCRUB;
3249 while ((c = getopt(argc, argv, "s")) != -1) {
3252 cb.cb_type = POOL_SCAN_NONE;
3255 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3267 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3271 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3274 typedef struct status_cbdata {
3276 boolean_t cb_allpools;
3277 boolean_t cb_verbose;
3278 boolean_t cb_explain;
3280 boolean_t cb_dedup_stats;
3284 * Print out detailed scrub status.
3287 print_scan_status(pool_scan_stat_t *ps)
3290 uint64_t elapsed, mins_left, hours_left;
3291 uint64_t pass_exam, examined, total;
3293 double fraction_done;
3294 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3296 (void) printf(gettext(" scan: "));
3298 /* If there's never been a scan, there's not much to say. */
3299 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3300 ps->pss_func >= POOL_SCAN_FUNCS) {
3301 (void) printf(gettext("none requested\n"));
3305 start = ps->pss_start_time;
3306 end = ps->pss_end_time;
3307 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3309 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3310 ps->pss_func == POOL_SCAN_RESILVER);
3312 * Scan is finished or canceled.
3314 if (ps->pss_state == DSS_FINISHED) {
3315 uint64_t minutes_taken = (end - start) / 60;
3318 if (ps->pss_func == POOL_SCAN_SCRUB) {
3319 fmt = gettext("scrub repaired %s in %lluh%um with "
3320 "%llu errors on %s");
3321 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3322 fmt = gettext("resilvered %s in %lluh%um with "
3323 "%llu errors on %s");
3326 (void) printf(fmt, processed_buf,
3327 (u_longlong_t)(minutes_taken / 60),
3328 (uint_t)(minutes_taken % 60),
3329 (u_longlong_t)ps->pss_errors,
3330 ctime((time_t *)&end));
3332 } else if (ps->pss_state == DSS_CANCELED) {
3333 if (ps->pss_func == POOL_SCAN_SCRUB) {
3334 (void) printf(gettext("scrub canceled on %s"),
3336 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3337 (void) printf(gettext("resilver canceled on %s"),
3343 assert(ps->pss_state == DSS_SCANNING);
3346 * Scan is in progress.
3348 if (ps->pss_func == POOL_SCAN_SCRUB) {
3349 (void) printf(gettext("scrub in progress since %s"),
3351 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3352 (void) printf(gettext("resilver in progress since %s"),
3356 examined = ps->pss_examined ? ps->pss_examined : 1;
3357 total = ps->pss_to_examine;
3358 fraction_done = (double)examined / total;
3360 /* elapsed time for this pass */
3361 elapsed = time(NULL) - ps->pss_pass_start;
3362 elapsed = elapsed ? elapsed : 1;
3363 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3364 rate = pass_exam / elapsed;
3365 rate = rate ? rate : 1;
3366 mins_left = ((total - examined) / rate) / 60;
3367 hours_left = mins_left / 60;
3369 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3370 zfs_nicenum(total, total_buf, sizeof (total_buf));
3371 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3374 * do not print estimated time if hours_left is more than 30 days
3376 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3377 examined_buf, total_buf, rate_buf);
3378 if (hours_left < (30 * 24)) {
3379 (void) printf(gettext(", %lluh%um to go\n"),
3380 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3382 (void) printf(gettext(
3383 ", (scan is slow, no estimated time)\n"));
3386 if (ps->pss_func == POOL_SCAN_RESILVER) {
3387 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3388 processed_buf, 100 * fraction_done);
3389 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3390 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3391 processed_buf, 100 * fraction_done);
3396 print_error_log(zpool_handle_t *zhp)
3398 nvlist_t *nverrlist = NULL;
3401 size_t len = MAXPATHLEN * 2;
3403 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3404 (void) printf("errors: List of errors unavailable "
3405 "(insufficient privileges)\n");
3409 (void) printf("errors: Permanent errors have been "
3410 "detected in the following files:\n\n");
3412 pathname = safe_malloc(len);
3414 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3416 uint64_t dsobj, obj;
3418 verify(nvpair_value_nvlist(elem, &nv) == 0);
3419 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3421 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3423 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3424 (void) printf("%7s %s\n", "", pathname);
3427 nvlist_free(nverrlist);
3431 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3440 (void) printf(gettext("\tspares\n"));
3442 for (i = 0; i < nspares; i++) {
3443 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3444 print_status_config(zhp, name, spares[i],
3445 namewidth, 2, B_TRUE);
3451 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3460 (void) printf(gettext("\tcache\n"));
3462 for (i = 0; i < nl2cache; i++) {
3463 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3464 print_status_config(zhp, name, l2cache[i],
3465 namewidth, 2, B_FALSE);
3471 print_dedup_stats(nvlist_t *config)
3473 ddt_histogram_t *ddh;
3479 * If the pool was faulted then we may not have been able to
3480 * obtain the config. Otherwise, if have anything in the dedup
3481 * table continue processing the stats.
3483 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3484 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3487 (void) printf("\n");
3488 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3489 (u_longlong_t)ddo->ddo_count,
3490 (u_longlong_t)ddo->ddo_dspace,
3491 (u_longlong_t)ddo->ddo_mspace);
3493 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3494 (uint64_t **)&dds, &c) == 0);
3495 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3496 (uint64_t **)&ddh, &c) == 0);
3497 zpool_dump_ddt(dds, ddh);
3501 * Display a summary of pool status. Displays a summary such as:
3505 * reason: One or more devices ...
3506 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
3512 * When given the '-v' option, we print out the complete config. If the '-e'
3513 * option is specified, then we print out error rate information as well.
3516 status_callback(zpool_handle_t *zhp, void *data)
3518 status_cbdata_t *cbp = data;
3519 nvlist_t *config, *nvroot;
3526 config = zpool_get_config(zhp, NULL);
3527 reason = zpool_get_status(zhp, &msgid);
3532 * If we were given 'zpool status -x', only report those pools with
3535 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3536 if (!cbp->cb_allpools) {
3537 (void) printf(gettext("pool '%s' is healthy\n"),
3538 zpool_get_name(zhp));
3540 cbp->cb_first = B_FALSE;
3546 cbp->cb_first = B_FALSE;
3548 (void) printf("\n");
3550 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3552 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3553 (uint64_t **)&vs, &c) == 0);
3554 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3556 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3557 (void) printf(gettext(" state: %s\n"), health);
3560 case ZPOOL_STATUS_MISSING_DEV_R:
3561 (void) printf(gettext("status: One or more devices could not "
3562 "be opened. Sufficient replicas exist for\n\tthe pool to "
3563 "continue functioning in a degraded state.\n"));
3564 (void) printf(gettext("action: Attach the missing device and "
3565 "online it using 'zpool online'.\n"));
3568 case ZPOOL_STATUS_MISSING_DEV_NR:
3569 (void) printf(gettext("status: One or more devices could not "
3570 "be opened. There are insufficient\n\treplicas for the "
3571 "pool to continue functioning.\n"));
3572 (void) printf(gettext("action: Attach the missing device and "
3573 "online it using 'zpool online'.\n"));
3576 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3577 (void) printf(gettext("status: One or more devices could not "
3578 "be used because the label is missing or\n\tinvalid. "
3579 "Sufficient replicas exist for the pool to continue\n\t"
3580 "functioning in a degraded state.\n"));
3581 (void) printf(gettext("action: Replace the device using "
3582 "'zpool replace'.\n"));
3585 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3586 (void) printf(gettext("status: One or more devices could not "
3587 "be used because the label is missing \n\tor invalid. "
3588 "There are insufficient replicas for the pool to "
3589 "continue\n\tfunctioning.\n"));
3590 zpool_explain_recover(zpool_get_handle(zhp),
3591 zpool_get_name(zhp), reason, config);
3594 case ZPOOL_STATUS_FAILING_DEV:
3595 (void) printf(gettext("status: One or more devices has "
3596 "experienced an unrecoverable error. An\n\tattempt was "
3597 "made to correct the error. Applications are "
3599 (void) printf(gettext("action: Determine if the device needs "
3600 "to be replaced, and clear the errors\n\tusing "
3601 "'zpool clear' or replace the device with 'zpool "
3605 case ZPOOL_STATUS_OFFLINE_DEV:
3606 (void) printf(gettext("status: One or more devices has "
3607 "been taken offline by the administrator.\n\tSufficient "
3608 "replicas exist for the pool to continue functioning in "
3609 "a\n\tdegraded state.\n"));
3610 (void) printf(gettext("action: Online the device using "
3611 "'zpool online' or replace the device with\n\t'zpool "
3615 case ZPOOL_STATUS_REMOVED_DEV:
3616 (void) printf(gettext("status: One or more devices has "
3617 "been removed by the administrator.\n\tSufficient "
3618 "replicas exist for the pool to continue functioning in "
3619 "a\n\tdegraded state.\n"));
3620 (void) printf(gettext("action: Online the device using "
3621 "'zpool online' or replace the device with\n\t'zpool "
3625 case ZPOOL_STATUS_RESILVERING:
3626 (void) printf(gettext("status: One or more devices is "
3627 "currently being resilvered. The pool will\n\tcontinue "
3628 "to function, possibly in a degraded state.\n"));
3629 (void) printf(gettext("action: Wait for the resilver to "
3633 case ZPOOL_STATUS_CORRUPT_DATA:
3634 (void) printf(gettext("status: One or more devices has "
3635 "experienced an error resulting in data\n\tcorruption. "
3636 "Applications may be affected.\n"));
3637 (void) printf(gettext("action: Restore the file in question "
3638 "if possible. Otherwise restore the\n\tentire pool from "
3642 case ZPOOL_STATUS_CORRUPT_POOL:
3643 (void) printf(gettext("status: The pool metadata is corrupted "
3644 "and the pool cannot be opened.\n"));
3645 zpool_explain_recover(zpool_get_handle(zhp),
3646 zpool_get_name(zhp), reason, config);
3649 case ZPOOL_STATUS_VERSION_OLDER:
3650 (void) printf(gettext("status: The pool is formatted using an "
3651 "older on-disk format. The pool can\n\tstill be used, but "
3652 "some features are unavailable.\n"));
3653 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3654 "upgrade'. Once this is done, the\n\tpool will no longer "
3655 "be accessible on older software versions.\n"));
3658 case ZPOOL_STATUS_VERSION_NEWER:
3659 (void) printf(gettext("status: The pool has been upgraded to a "
3660 "newer, incompatible on-disk version.\n\tThe pool cannot "
3661 "be accessed on this system.\n"));
3662 (void) printf(gettext("action: Access the pool from a system "
3663 "running more recent software, or\n\trestore the pool from "
3667 case ZPOOL_STATUS_FAULTED_DEV_R:
3668 (void) printf(gettext("status: One or more devices are "
3669 "faulted in response to persistent errors.\n\tSufficient "
3670 "replicas exist for the pool to continue functioning "
3671 "in a\n\tdegraded state.\n"));
3672 (void) printf(gettext("action: Replace the faulted device, "
3673 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3676 case ZPOOL_STATUS_FAULTED_DEV_NR:
3677 (void) printf(gettext("status: One or more devices are "
3678 "faulted in response to persistent errors. There are "
3679 "insufficient replicas for the pool to\n\tcontinue "
3681 (void) printf(gettext("action: Destroy and re-create the pool "
3682 "from a backup source. Manually marking the device\n"
3683 "\trepaired using 'zpool clear' may allow some data "
3684 "to be recovered.\n"));
3687 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3688 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3689 (void) printf(gettext("status: One or more devices are "
3690 "faulted in response to IO failures.\n"));
3691 (void) printf(gettext("action: Make sure the affected devices "
3692 "are connected, then run 'zpool clear'.\n"));
3695 case ZPOOL_STATUS_BAD_LOG:
3696 (void) printf(gettext("status: An intent log record "
3697 "could not be read.\n"
3698 "\tWaiting for adminstrator intervention to fix the "
3699 "faulted pool.\n"));
3700 (void) printf(gettext("action: Either restore the affected "
3701 "device(s) and run 'zpool online',\n"
3702 "\tor ignore the intent log records by running "
3703 "'zpool clear'.\n"));
3708 * The remaining errors can't actually be generated, yet.
3710 assert(reason == ZPOOL_STATUS_OK);
3714 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
3717 if (config != NULL) {
3720 nvlist_t **spares, **l2cache;
3721 uint_t nspares, nl2cache;
3722 pool_scan_stat_t *ps = NULL;
3724 (void) nvlist_lookup_uint64_array(nvroot,
3725 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3726 print_scan_status(ps);
3728 namewidth = max_width(zhp, nvroot, 0, 0);
3732 (void) printf(gettext("config:\n\n"));
3733 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3734 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3735 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3736 namewidth, 0, B_FALSE);
3738 if (num_logs(nvroot) > 0)
3739 print_logs(zhp, nvroot, namewidth, B_TRUE);
3740 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3741 &l2cache, &nl2cache) == 0)
3742 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3744 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3745 &spares, &nspares) == 0)
3746 print_spares(zhp, spares, nspares, namewidth);
3748 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3750 nvlist_t *nverrlist = NULL;
3753 * If the approximate error count is small, get a
3754 * precise count by fetching the entire log and
3755 * uniquifying the results.
3757 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3758 zpool_get_errlog(zhp, &nverrlist) == 0) {
3763 while ((elem = nvlist_next_nvpair(nverrlist,
3768 nvlist_free(nverrlist);
3770 (void) printf("\n");
3773 (void) printf(gettext("errors: No known data "
3775 else if (!cbp->cb_verbose)
3776 (void) printf(gettext("errors: %llu data "
3777 "errors, use '-v' for a list\n"),
3778 (u_longlong_t)nerr);
3780 print_error_log(zhp);
3783 if (cbp->cb_dedup_stats)
3784 print_dedup_stats(config);
3786 (void) printf(gettext("config: The configuration cannot be "
3794 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3796 * -v Display complete error logs
3797 * -x Display only pools with potential problems
3798 * -D Display dedup status (undocumented)
3799 * -T Display a timestamp in date(1) or Unix format
3801 * Describes the health status of all pools or some subset.
3804 zpool_do_status(int argc, char **argv)
3808 unsigned long interval = 0, count = 0;
3809 status_cbdata_t cb = { 0 };
3812 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3815 cb.cb_verbose = B_TRUE;
3818 cb.cb_explain = B_TRUE;
3821 cb.cb_dedup_stats = B_TRUE;
3824 get_timestamp_arg(*optarg);
3827 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3836 get_interval_count(&argc, argv, &interval, &count);
3839 cb.cb_allpools = B_TRUE;
3841 cb.cb_first = B_TRUE;
3844 if (timestamp_fmt != NODATE)
3845 print_timestamp(timestamp_fmt);
3847 ret = for_each_pool(argc, argv, B_TRUE, NULL,
3848 status_callback, &cb);
3850 if (argc == 0 && cb.cb_count == 0)
3851 (void) fprintf(stderr, gettext("no pools available\n"));
3852 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3853 (void) printf(gettext("all pools are healthy\n"));
3861 if (count != 0 && --count == 0)
3864 (void) sleep(interval);
3870 typedef struct upgrade_cbdata {
3875 uint64_t cb_version;
3880 upgrade_cb(zpool_handle_t *zhp, void *arg)
3882 upgrade_cbdata_t *cbp = arg;
3887 config = zpool_get_config(zhp, NULL);
3888 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3891 if (!cbp->cb_newer && version < SPA_VERSION) {
3893 if (cbp->cb_first) {
3894 (void) printf(gettext("The following pools are "
3895 "out of date, and can be upgraded. After "
3896 "being\nupgraded, these pools will no "
3897 "longer be accessible by older software "
3899 (void) printf(gettext("VER POOL\n"));
3900 (void) printf(gettext("--- ------------\n"));
3901 cbp->cb_first = B_FALSE;
3904 (void) printf("%2llu %s\n", (u_longlong_t)version,
3905 zpool_get_name(zhp));
3907 cbp->cb_first = B_FALSE;
3908 ret = zpool_upgrade(zhp, cbp->cb_version);
3910 (void) printf(gettext("Successfully upgraded "
3911 "'%s'\n\n"), zpool_get_name(zhp));
3914 } else if (cbp->cb_newer && version > SPA_VERSION) {
3915 assert(!cbp->cb_all);
3917 if (cbp->cb_first) {
3918 (void) printf(gettext("The following pools are "
3919 "formatted using a newer software version and\n"
3920 "cannot be accessed on the current system.\n\n"));
3921 (void) printf(gettext("VER POOL\n"));
3922 (void) printf(gettext("--- ------------\n"));
3923 cbp->cb_first = B_FALSE;
3926 (void) printf("%2llu %s\n", (u_longlong_t)version,
3927 zpool_get_name(zhp));
3936 upgrade_one(zpool_handle_t *zhp, void *data)
3938 upgrade_cbdata_t *cbp = data;
3939 uint64_t cur_version;
3942 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3943 (void) printf(gettext("'log' is now a reserved word\n"
3944 "Pool 'log' must be renamed using export and import"
3949 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3950 if (cur_version > cbp->cb_version) {
3951 (void) printf(gettext("Pool '%s' is already formatted "
3952 "using more current version '%llu'.\n"),
3953 zpool_get_name(zhp), (u_longlong_t) cur_version);
3956 if (cur_version == cbp->cb_version) {
3957 (void) printf(gettext("Pool '%s' is already formatted "
3958 "using the current version.\n"), zpool_get_name(zhp));
3962 ret = zpool_upgrade(zhp, cbp->cb_version);
3965 (void) printf(gettext("Successfully upgraded '%s' "
3966 "from version %llu to version %llu\n\n"),
3967 zpool_get_name(zhp), (u_longlong_t)cur_version,
3968 (u_longlong_t)cbp->cb_version);
3977 * zpool upgrade [-V version] <-a | pool ...>
3979 * With no arguments, display downrev'd ZFS pool available for upgrade.
3980 * Individual pools can be upgraded by specifying the pool, and '-a' will
3981 * upgrade all pools.
3984 zpool_do_upgrade(int argc, char **argv)
3987 upgrade_cbdata_t cb = { 0 };
3989 boolean_t showversions = B_FALSE;
3994 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4000 showversions = B_TRUE;
4003 cb.cb_version = strtoll(optarg, &end, 10);
4004 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4005 cb.cb_version < SPA_VERSION_1) {
4006 (void) fprintf(stderr,
4007 gettext("invalid version '%s'\n"), optarg);
4012 (void) fprintf(stderr, gettext("missing argument for "
4013 "'%c' option\n"), optopt);
4017 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4028 if (cb.cb_version == 0) {
4029 cb.cb_version = SPA_VERSION;
4030 } else if (!cb.cb_all && argc == 0) {
4031 (void) fprintf(stderr, gettext("-V option is "
4032 "incompatible with other arguments\n"));
4037 if (cb.cb_all || argc != 0) {
4038 (void) fprintf(stderr, gettext("-v option is "
4039 "incompatible with other arguments\n"));
4042 } else if (cb.cb_all) {
4044 (void) fprintf(stderr, gettext("-a option should not "
4045 "be used along with a pool name\n"));
4050 (void) printf(gettext("This system is currently running "
4051 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4052 cb.cb_first = B_TRUE;
4054 (void) printf(gettext("The following versions are "
4056 (void) printf(gettext("VER DESCRIPTION\n"));
4057 (void) printf("--- -----------------------------------------"
4058 "---------------\n");
4059 (void) printf(gettext(" 1 Initial ZFS version\n"));
4060 (void) printf(gettext(" 2 Ditto blocks "
4061 "(replicated metadata)\n"));
4062 (void) printf(gettext(" 3 Hot spares and double parity "
4064 (void) printf(gettext(" 4 zpool history\n"));
4065 (void) printf(gettext(" 5 Compression using the gzip "
4067 (void) printf(gettext(" 6 bootfs pool property\n"));
4068 (void) printf(gettext(" 7 Separate intent log devices\n"));
4069 (void) printf(gettext(" 8 Delegated administration\n"));
4070 (void) printf(gettext(" 9 refquota and refreservation "
4072 (void) printf(gettext(" 10 Cache devices\n"));
4073 (void) printf(gettext(" 11 Improved scrub performance\n"));
4074 (void) printf(gettext(" 12 Snapshot properties\n"));
4075 (void) printf(gettext(" 13 snapused property\n"));
4076 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4077 (void) printf(gettext(" 15 user/group space accounting\n"));
4078 (void) printf(gettext(" 16 stmf property support\n"));
4079 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4080 (void) printf(gettext(" 18 Snapshot user holds\n"));
4081 (void) printf(gettext(" 19 Log device removal\n"));
4082 (void) printf(gettext(" 20 Compression using zle "
4083 "(zero-length encoding)\n"));
4084 (void) printf(gettext(" 21 Deduplication\n"));
4085 (void) printf(gettext(" 22 Received properties\n"));
4086 (void) printf(gettext(" 23 Slim ZIL\n"));
4087 (void) printf(gettext(" 24 System attributes\n"));
4088 (void) printf(gettext(" 25 Improved scrub stats\n"));
4089 (void) printf(gettext(" 26 Improved snapshot deletion "
4091 (void) printf(gettext(" 27 Improved snapshot creation "
4093 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4094 (void) printf(gettext("\nFor more information on a particular "
4095 "version, including supported releases,\n"));
4096 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4097 } else if (argc == 0) {
4100 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4101 notfound = cb.cb_first;
4103 if (!cb.cb_all && ret == 0) {
4105 (void) printf("\n");
4106 cb.cb_first = B_TRUE;
4107 cb.cb_newer = B_TRUE;
4108 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4111 (void) printf("\n");
4117 (void) printf(gettext("All pools are formatted "
4118 "using this version.\n"));
4119 else if (!cb.cb_all)
4120 (void) printf(gettext("Use 'zpool upgrade -v' "
4121 "for a list of available versions and "
4122 "their associated\nfeatures.\n"));
4125 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4132 typedef struct hist_cbdata {
4139 * Print out the command history for a specific pool.
4142 get_history_one(zpool_handle_t *zhp, void *data)
4158 char internalstr[MAXPATHLEN];
4159 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4163 cb->first = B_FALSE;
4165 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4167 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4170 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4171 &records, &numrecords) == 0);
4172 for (i = 0; i < numrecords; i++) {
4173 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4177 /* is it an internal event or a standard event? */
4178 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4180 if (cb->internal == 0)
4183 if (nvlist_lookup_uint64(records[i],
4184 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4186 verify(nvlist_lookup_uint64(records[i],
4187 ZPOOL_HIST_TXG, &txg) == 0);
4188 verify(nvlist_lookup_string(records[i],
4189 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4190 if (ievent >= LOG_END)
4192 (void) snprintf(internalstr,
4193 sizeof (internalstr),
4194 "[internal %s txg:%llu] %s",
4195 zfs_history_event_names[ievent], (u_longlong_t)txg,
4197 cmdstr = internalstr;
4200 (void) localtime_r(&tsec, &t);
4201 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4202 (void) printf("%s %s", tbuf, cmdstr);
4205 (void) printf("\n");
4208 (void) printf(" [");
4209 if (nvlist_lookup_uint64(records[i],
4210 ZPOOL_HIST_WHO, &who) == 0) {
4211 pwd = getpwuid((uid_t)who);
4213 (void) printf("user %s on",
4216 (void) printf("user %d on",
4219 (void) printf(gettext("no info]\n"));
4222 if (nvlist_lookup_string(records[i],
4223 ZPOOL_HIST_HOST, &hostname) == 0) {
4224 (void) printf(" %s", hostname);
4226 if (nvlist_lookup_string(records[i],
4227 ZPOOL_HIST_ZONE, &zonename) == 0) {
4228 (void) printf(":%s", zonename);
4232 (void) printf("\n");
4234 (void) printf("\n");
4241 * zpool history <pool>
4243 * Displays the history of commands that modified pools.
4248 zpool_do_history(int argc, char **argv)
4250 hist_cbdata_t cbdata = { 0 };
4254 cbdata.first = B_TRUE;
4256 while ((c = getopt(argc, argv, "li")) != -1) {
4262 cbdata.internal = 1;
4265 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4273 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4276 if (argc == 0 && cbdata.first == B_TRUE) {
4277 (void) fprintf(stderr, gettext("no pools available\n"));
4284 typedef struct ev_opts {
4292 zpool_do_events_short(nvlist_t *nvl)
4294 char ctime_str[26], str[32], *ptr;
4298 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
4299 memset(str, ' ', 32);
4300 (void) ctime_r((const time_t *)&tv[0], ctime_str);
4301 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
4302 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
4303 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
4304 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
4305 (void) printf(gettext("%s "), str);
4307 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
4308 (void) printf(gettext("%s\n"), ptr);
4312 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
4316 for (nvp = nvlist_next_nvpair(nvl, NULL);
4317 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
4319 data_type_t type = nvpair_type(nvp);
4320 const char *name = nvpair_name(nvp);
4330 printf(gettext("%*s%s = "), depth, "", name);
4333 case DATA_TYPE_BOOLEAN:
4334 printf(gettext("%s"), "1");
4337 case DATA_TYPE_BOOLEAN_VALUE:
4338 (void) nvpair_value_boolean_value(nvp, &b);
4339 printf(gettext("%s"), b ? "1" : "0");
4342 case DATA_TYPE_BYTE:
4343 (void) nvpair_value_byte(nvp, &i8);
4344 printf(gettext("0x%x"), i8);
4347 case DATA_TYPE_INT8:
4348 (void) nvpair_value_int8(nvp, (void *)&i8);
4349 printf(gettext("0x%x"), i8);
4352 case DATA_TYPE_UINT8:
4353 (void) nvpair_value_uint8(nvp, &i8);
4354 printf(gettext("0x%x"), i8);
4357 case DATA_TYPE_INT16:
4358 (void) nvpair_value_int16(nvp, (void *)&i16);
4359 printf(gettext("0x%x"), i16);
4362 case DATA_TYPE_UINT16:
4363 (void) nvpair_value_uint16(nvp, &i16);
4364 printf(gettext("0x%x"), i16);
4367 case DATA_TYPE_INT32:
4368 (void) nvpair_value_int32(nvp, (void *)&i32);
4369 printf(gettext("0x%x"), i32);
4372 case DATA_TYPE_UINT32:
4373 (void) nvpair_value_uint32(nvp, &i32);
4374 printf(gettext("0x%x"), i32);
4377 case DATA_TYPE_INT64:
4378 (void) nvpair_value_int64(nvp, (void *)&i64);
4379 printf(gettext("0x%llx"), (u_longlong_t)i64);
4382 case DATA_TYPE_UINT64:
4383 (void) nvpair_value_uint64(nvp, &i64);
4384 printf(gettext("0x%llx"), (u_longlong_t)i64);
4387 case DATA_TYPE_HRTIME:
4388 (void) nvpair_value_hrtime(nvp, (void *)&i64);
4389 printf(gettext("0x%llx"), (u_longlong_t)i64);
4392 case DATA_TYPE_STRING:
4393 (void) nvpair_value_string(nvp, &str);
4394 printf(gettext("\"%s\""), str ? str : "<NULL>");
4397 case DATA_TYPE_NVLIST:
4398 printf(gettext("(embedded nvlist)\n"));
4399 (void) nvpair_value_nvlist(nvp, &cnv);
4400 zpool_do_events_nvprint(cnv, depth + 8);
4401 printf(gettext("%*s(end %s)"), depth, "", name);
4404 case DATA_TYPE_NVLIST_ARRAY: {
4408 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
4409 printf(gettext("(%d embedded nvlists)\n"), nelem);
4410 for (i = 0; i < nelem; i++) {
4411 printf(gettext("%*s%s[%d] = %s\n"),
4412 depth, "", name, i, "(embedded nvlist)");
4413 zpool_do_events_nvprint(val[i], depth + 8);
4414 printf(gettext("%*s(end %s[%i])\n"),
4415 depth, "", name, i);
4417 printf(gettext("%*s(end %s)\n"), depth, "", name);
4421 case DATA_TYPE_INT8_ARRAY: {
4425 (void) nvpair_value_int8_array(nvp, &val, &nelem);
4426 for (i = 0; i < nelem; i++)
4427 printf(gettext("0x%x "), val[i]);
4432 case DATA_TYPE_UINT8_ARRAY: {
4436 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
4437 for (i = 0; i < nelem; i++)
4438 printf(gettext("0x%x "), val[i]);
4443 case DATA_TYPE_INT16_ARRAY: {
4447 (void) nvpair_value_int16_array(nvp, &val, &nelem);
4448 for (i = 0; i < nelem; i++)
4449 printf(gettext("0x%x "), val[i]);
4454 case DATA_TYPE_UINT16_ARRAY: {
4458 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
4459 for (i = 0; i < nelem; i++)
4460 printf(gettext("0x%x "), val[i]);
4465 case DATA_TYPE_INT32_ARRAY: {
4469 (void) nvpair_value_int32_array(nvp, &val, &nelem);
4470 for (i = 0; i < nelem; i++)
4471 printf(gettext("0x%x "), val[i]);
4476 case DATA_TYPE_UINT32_ARRAY: {
4480 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
4481 for (i = 0; i < nelem; i++)
4482 printf(gettext("0x%x "), val[i]);
4487 case DATA_TYPE_INT64_ARRAY: {
4491 (void) nvpair_value_int64_array(nvp, &val, &nelem);
4492 for (i = 0; i < nelem; i++)
4493 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4498 case DATA_TYPE_UINT64_ARRAY: {
4502 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
4503 for (i = 0; i < nelem; i++)
4504 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4509 case DATA_TYPE_STRING_ARRAY:
4510 case DATA_TYPE_BOOLEAN_ARRAY:
4511 case DATA_TYPE_BYTE_ARRAY:
4512 case DATA_TYPE_DOUBLE:
4513 case DATA_TYPE_UNKNOWN:
4514 printf(gettext("<unknown>"));
4518 printf(gettext("\n"));
4523 zpool_do_events_next(ev_opts_t *opts)
4526 int cleanup_fd, ret, dropped;
4528 cleanup_fd = open(ZFS_DEV, O_RDWR);
4529 VERIFY(cleanup_fd >= 0);
4531 if (!opts->scripted)
4532 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
4535 ret = zpool_events_next(g_zfs, &nvl, &dropped,
4536 !!opts->follow, cleanup_fd);
4537 if (ret || nvl == NULL)
4541 (void) printf(gettext("dropped %d events\n"), dropped);
4543 zpool_do_events_short(nvl);
4545 if (opts->verbose) {
4546 zpool_do_events_nvprint(nvl, 8);
4547 printf(gettext("\n"));
4553 VERIFY(0 == close(cleanup_fd));
4559 zpool_do_events_clear(ev_opts_t *opts)
4563 ret = zpool_events_clear(g_zfs, &count);
4565 (void) printf(gettext("cleared %d events\n"), count);
4571 * zpool events [-vfc]
4573 * Displays events logs by ZFS.
4576 zpool_do_events(int argc, char **argv)
4578 ev_opts_t opts = { 0 };
4583 while ((c = getopt(argc, argv, "vHfc")) != -1) {
4598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4607 ret = zpool_do_events_clear(&opts);
4609 ret = zpool_do_events_next(&opts);
4615 get_callback(zpool_handle_t *zhp, void *data)
4617 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4618 char value[MAXNAMELEN];
4619 zprop_source_t srctype;
4622 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4625 * Skip the special fake placeholder. This will also skip
4626 * over the name property when 'all' is specified.
4628 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4629 pl == cbp->cb_proplist)
4632 if (zpool_get_prop(zhp, pl->pl_prop,
4633 value, sizeof (value), &srctype) != 0)
4636 zprop_print_one_property(zpool_get_name(zhp), cbp,
4637 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4644 zpool_do_get(int argc, char **argv)
4646 zprop_get_cbdata_t cb = { 0 };
4647 zprop_list_t fake_name = { 0 };
4653 cb.cb_first = B_TRUE;
4654 cb.cb_sources = ZPROP_SRC_ALL;
4655 cb.cb_columns[0] = GET_COL_NAME;
4656 cb.cb_columns[1] = GET_COL_PROPERTY;
4657 cb.cb_columns[2] = GET_COL_VALUE;
4658 cb.cb_columns[3] = GET_COL_SOURCE;
4659 cb.cb_type = ZFS_TYPE_POOL;
4661 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4662 ZFS_TYPE_POOL) != 0)
4665 if (cb.cb_proplist != NULL) {
4666 fake_name.pl_prop = ZPOOL_PROP_NAME;
4667 fake_name.pl_width = strlen(gettext("NAME"));
4668 fake_name.pl_next = cb.cb_proplist;
4669 cb.cb_proplist = &fake_name;
4672 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4675 if (cb.cb_proplist == &fake_name)
4676 zprop_free_list(fake_name.pl_next);
4678 zprop_free_list(cb.cb_proplist);
4683 typedef struct set_cbdata {
4686 boolean_t cb_any_successful;
4690 set_callback(zpool_handle_t *zhp, void *data)
4693 set_cbdata_t *cb = (set_cbdata_t *)data;
4695 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4698 cb->cb_any_successful = B_TRUE;
4704 zpool_do_set(int argc, char **argv)
4706 set_cbdata_t cb = { 0 };
4709 if (argc > 1 && argv[1][0] == '-') {
4710 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4716 (void) fprintf(stderr, gettext("missing property=value "
4722 (void) fprintf(stderr, gettext("missing pool name\n"));
4727 (void) fprintf(stderr, gettext("too many pool names\n"));
4731 cb.cb_propname = argv[1];
4732 cb.cb_value = strchr(cb.cb_propname, '=');
4733 if (cb.cb_value == NULL) {
4734 (void) fprintf(stderr, gettext("missing value in "
4735 "property=value argument\n"));
4739 *(cb.cb_value) = '\0';
4742 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4749 find_command_idx(char *command, int *idx)
4753 for (i = 0; i < NCOMMAND; i++) {
4754 if (command_table[i].name == NULL)
4757 if (strcmp(command, command_table[i].name) == 0) {
4766 main(int argc, char **argv)
4772 (void) setlocale(LC_ALL, "");
4773 (void) textdomain(TEXT_DOMAIN);
4778 * Make sure the user has specified some command.
4781 (void) fprintf(stderr, gettext("missing command\n"));
4790 if ((strcmp(cmdname, "-?") == 0) ||
4791 strcmp(cmdname, "--help") == 0)
4794 if ((g_zfs = libzfs_init()) == NULL)
4797 libzfs_print_on_error(g_zfs, B_TRUE);
4799 zpool_set_history_str("zpool", argc, argv, history_str);
4800 verify(zpool_stage_history(g_zfs, history_str) == 0);
4803 * Run the appropriate command.
4805 if (find_command_idx(cmdname, &i) == 0) {
4806 current_command = &command_table[i];
4807 ret = command_table[i].func(argc - 1, argv + 1);
4808 } else if (strchr(cmdname, '=')) {
4809 verify(find_command_idx("set", &i) == 0);
4810 current_command = &command_table[i];
4811 ret = command_table[i].func(argc, argv);
4812 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4814 * 'freeze' is a vile debugging abomination, so we treat
4818 int fd = open(ZFS_DEV, O_RDWR);
4819 (void) strcpy((void *)buf, argv[2]);
4820 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4822 (void) fprintf(stderr, gettext("unrecognized "
4823 "command '%s'\n"), cmdname);
4831 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4832 * for the purposes of running ::findleaks.
4834 if (getenv("ZFS_ABORT") != NULL) {
4835 (void) printf("dumping core by request\n");