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) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
47 #include <sys/fs/zfs.h>
49 #include <sys/fm/util.h>
50 #include <sys/fm/protocol.h>
54 #include "zpool_util.h"
55 #include "zfs_comutil.h"
57 #include "statcommon.h"
59 static int zpool_do_create(int, char **);
60 static int zpool_do_destroy(int, char **);
62 static int zpool_do_add(int, char **);
63 static int zpool_do_remove(int, char **);
65 static int zpool_do_list(int, char **);
66 static int zpool_do_iostat(int, char **);
67 static int zpool_do_status(int, char **);
69 static int zpool_do_online(int, char **);
70 static int zpool_do_offline(int, char **);
71 static int zpool_do_clear(int, char **);
72 static int zpool_do_reopen(int, char **);
74 static int zpool_do_reguid(int, char **);
76 static int zpool_do_attach(int, char **);
77 static int zpool_do_detach(int, char **);
78 static int zpool_do_replace(int, char **);
79 static int zpool_do_split(int, char **);
81 static int zpool_do_scrub(int, char **);
83 static int zpool_do_import(int, char **);
84 static int zpool_do_export(int, char **);
86 static int zpool_do_upgrade(int, char **);
88 static int zpool_do_history(int, char **);
89 static int zpool_do_events(int, char **);
91 static int zpool_do_get(int, char **);
92 static int zpool_do_set(int, char **);
95 * These libumem hooks provide a reasonable set of defaults for the allocator's
96 * debugging facilities.
101 _umem_debug_init(void)
103 return ("default,verbose"); /* $UMEM_DEBUG setting */
107 _umem_logging_init(void)
109 return ("fail,contents"); /* $UMEM_LOGGING setting */
141 typedef struct zpool_command {
143 int (*func)(int, char **);
148 * Master command table. Each ZFS command has a name, associated function, and
149 * usage message. The usage messages need to be internationalized, so we have
150 * to have a function to return the usage message based on a command index.
152 * These commands are organized according to how they are displayed in the usage
153 * message. An empty command (one with a NULL name) indicates an empty line in
154 * the generic usage message.
156 static zpool_command_t command_table[] = {
157 { "create", zpool_do_create, HELP_CREATE },
158 { "destroy", zpool_do_destroy, HELP_DESTROY },
160 { "add", zpool_do_add, HELP_ADD },
161 { "remove", zpool_do_remove, HELP_REMOVE },
163 { "list", zpool_do_list, HELP_LIST },
164 { "iostat", zpool_do_iostat, HELP_IOSTAT },
165 { "status", zpool_do_status, HELP_STATUS },
167 { "online", zpool_do_online, HELP_ONLINE },
168 { "offline", zpool_do_offline, HELP_OFFLINE },
169 { "clear", zpool_do_clear, HELP_CLEAR },
170 { "reopen", zpool_do_reopen, HELP_REOPEN },
172 { "attach", zpool_do_attach, HELP_ATTACH },
173 { "detach", zpool_do_detach, HELP_DETACH },
174 { "replace", zpool_do_replace, HELP_REPLACE },
175 { "split", zpool_do_split, HELP_SPLIT },
177 { "scrub", zpool_do_scrub, HELP_SCRUB },
179 { "import", zpool_do_import, HELP_IMPORT },
180 { "export", zpool_do_export, HELP_EXPORT },
181 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
182 { "reguid", zpool_do_reguid, HELP_REGUID },
184 { "history", zpool_do_history, HELP_HISTORY },
185 { "events", zpool_do_events, HELP_EVENTS },
187 { "get", zpool_do_get, HELP_GET },
188 { "set", zpool_do_set, HELP_SET },
191 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
193 zpool_command_t *current_command;
194 static char history_str[HIS_MAX_RECORD_LEN];
196 static uint_t timestamp_fmt = NODATE;
199 get_usage(zpool_help_t idx) {
202 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
204 return (gettext("\tattach [-f] <pool> <device> "
207 return (gettext("\tclear [-nF] <pool> [device]\n"));
209 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
210 "\t [-O file-system-property=value] ... \n"
211 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
213 return (gettext("\tdestroy [-f] <pool>\n"));
215 return (gettext("\tdetach <pool> <device>\n"));
217 return (gettext("\texport [-f] <pool> ...\n"));
219 return (gettext("\thistory [-il] [<pool>] ...\n"));
221 return (gettext("\timport [-d dir] [-D]\n"
222 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
223 "\timport [-o mntopts] [-o property=value] ... \n"
224 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
225 "[-R root] [-F [-n]] -a\n"
226 "\timport [-o mntopts] [-o property=value] ... \n"
227 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
228 "[-R root] [-F [-n]]\n"
229 "\t <pool | id> [newpool]\n"));
231 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
234 return (gettext("\tlist [-H] [-o property[,...]] "
235 "[-T d|u] [pool] ... [interval [count]]\n"));
237 return (gettext("\toffline [-t] <pool> <device> ...\n"));
239 return (gettext("\tonline <pool> <device> ...\n"));
241 return (gettext("\treplace [-f] <pool> <device> "
244 return (gettext("\tremove <pool> <device> ...\n"));
246 return (""); /* Undocumented command */
248 return (gettext("\tscrub [-s] <pool> ...\n"));
250 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
253 return (gettext("\tupgrade\n"
255 "\tupgrade [-V version] <-a | pool ...>\n"));
257 return (gettext("\tevents [-vHfc]\n"));
259 return (gettext("\tget <\"all\" | property[,...]> "
262 return (gettext("\tset <property=value> <pool> \n"));
264 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
265 "\t [-o property=value] <pool> <newpool> "
266 "[<device> ...]\n"));
268 return (gettext("\treguid <pool>\n"));
277 * Callback routine that will print out a pool property value.
280 print_prop_cb(int prop, void *cb)
284 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
286 if (zpool_prop_readonly(prop))
287 (void) fprintf(fp, " NO ");
289 (void) fprintf(fp, " YES ");
291 if (zpool_prop_values(prop) == NULL)
292 (void) fprintf(fp, "-\n");
294 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
300 * Display usage message. If we're inside a command, display only the usage for
301 * that command. Otherwise, iterate over the entire command table and display
302 * a complete usage message.
305 usage(boolean_t requested)
307 FILE *fp = requested ? stdout : stderr;
309 if (current_command == NULL) {
312 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
314 gettext("where 'command' is one of the following:\n\n"));
316 for (i = 0; i < NCOMMAND; i++) {
317 if (command_table[i].name == NULL)
318 (void) fprintf(fp, "\n");
320 (void) fprintf(fp, "%s",
321 get_usage(command_table[i].usage));
324 (void) fprintf(fp, gettext("usage:\n"));
325 (void) fprintf(fp, "%s", get_usage(current_command->usage));
328 if (current_command != NULL &&
329 ((strcmp(current_command->name, "set") == 0) ||
330 (strcmp(current_command->name, "get") == 0) ||
331 (strcmp(current_command->name, "list") == 0))) {
334 gettext("\nthe following properties are supported:\n"));
336 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
337 "PROPERTY", "EDIT", "VALUES");
339 /* Iterate over all properties */
340 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
345 * See comments at end of main().
347 if (getenv("ZFS_ABORT") != NULL) {
348 (void) printf("dumping core by request\n");
352 exit(requested ? 0 : 2);
356 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
357 boolean_t print_logs)
364 (void) printf("\t%*s%s\n", indent, "", name);
366 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
367 &child, &children) != 0)
370 for (c = 0; c < children; c++) {
371 uint64_t is_log = B_FALSE;
373 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
375 if ((is_log && !print_logs) || (!is_log && print_logs))
378 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
379 print_vdev_tree(zhp, vname, child[c], indent + 2,
386 * Add a property pair (name, string-value) into a property nvlist.
389 add_prop_list(const char *propname, char *propval, nvlist_t **props,
392 zpool_prop_t prop = ZPROP_INVAL;
398 if (*props == NULL &&
399 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
400 (void) fprintf(stderr,
401 gettext("internal error: out of memory\n"));
408 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
409 (void) fprintf(stderr, gettext("property '%s' is "
410 "not a valid pool property\n"), propname);
413 normnm = zpool_prop_to_name(prop);
415 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
416 normnm = zfs_prop_to_name(fprop);
422 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
423 prop != ZPOOL_PROP_CACHEFILE) {
424 (void) fprintf(stderr, gettext("property '%s' "
425 "specified multiple times\n"), propname);
429 if (nvlist_add_string(proplist, normnm, propval) != 0) {
430 (void) fprintf(stderr, gettext("internal "
431 "error: out of memory\n"));
439 * zpool add [-fn] <pool> <vdev> ...
441 * -f Force addition of devices, even if they appear in use
442 * -n Do not add the devices, but display the resulting layout if
443 * they were to be added.
445 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
446 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
450 zpool_do_add(int argc, char **argv)
452 boolean_t force = B_FALSE;
453 boolean_t dryrun = B_FALSE;
462 while ((c = getopt(argc, argv, "fn")) != -1) {
471 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
480 /* get pool name and check number of arguments */
482 (void) fprintf(stderr, gettext("missing pool name argument\n"));
486 (void) fprintf(stderr, gettext("missing vdev specification\n"));
495 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
498 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
499 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
505 /* pass off to get_vdev_spec for processing */
506 nvroot = make_root_vdev(zhp, NULL, force, !force, B_FALSE, dryrun,
508 if (nvroot == NULL) {
514 nvlist_t *poolnvroot;
516 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
519 (void) printf(gettext("would update '%s' to the following "
520 "configuration:\n"), zpool_get_name(zhp));
522 /* print original main pool and new tree */
523 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
524 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
526 /* Do the same for the logs */
527 if (num_logs(poolnvroot) > 0) {
528 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
529 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
530 } else if (num_logs(nvroot) > 0) {
531 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
536 ret = (zpool_add(zhp, nvroot) != 0);
546 * zpool remove <pool> <vdev> ...
548 * Removes the given vdev from the pool. Currently, this supports removing
549 * spares, cache, and log devices from the pool.
552 zpool_do_remove(int argc, char **argv)
561 /* get pool name and check number of arguments */
563 (void) fprintf(stderr, gettext("missing pool name argument\n"));
567 (void) fprintf(stderr, gettext("missing device\n"));
573 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
576 for (i = 1; i < argc; i++) {
577 if (zpool_vdev_remove(zhp, argv[i]) != 0)
585 * zpool create [-fn] [-o property=value] ...
586 * [-O file-system-property=value] ...
587 * [-R root] [-m mountpoint] <pool> <dev> ...
589 * -f Force creation, even if devices appear in use
590 * -n Do not create the pool, but display the resulting layout if it
591 * were to be created.
592 * -R Create a pool under an alternate root
593 * -m Set default mountpoint for the root dataset. By default it's
595 * -o Set property=value.
596 * -O Set fsproperty=value in the pool's root file system
598 * Creates the named pool according to the given vdev specification. The
599 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
600 * we get the nvlist back from get_vdev_spec(), we either print out the contents
601 * (if '-n' was specified), or pass it to libzfs to do the creation.
604 zpool_do_create(int argc, char **argv)
606 boolean_t force = B_FALSE;
607 boolean_t dryrun = B_FALSE;
609 nvlist_t *nvroot = NULL;
612 char *altroot = NULL;
613 char *mountpoint = NULL;
614 nvlist_t *fsprops = NULL;
615 nvlist_t *props = NULL;
619 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
629 if (add_prop_list(zpool_prop_to_name(
630 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
632 if (nvlist_lookup_string(props,
633 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
636 if (add_prop_list(zpool_prop_to_name(
637 ZPOOL_PROP_CACHEFILE), "none", &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, &props, B_TRUE))
656 if ((propval = strchr(optarg, '=')) == NULL) {
657 (void) fprintf(stderr, gettext("missing "
658 "'=' for -O option\n"));
664 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
668 (void) fprintf(stderr, gettext("missing argument for "
669 "'%c' option\n"), optopt);
672 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
681 /* get pool name and check number of arguments */
683 (void) fprintf(stderr, gettext("missing pool name argument\n"));
687 (void) fprintf(stderr, gettext("missing vdev specification\n"));
694 * As a special case, check for use of '/' in the name, and direct the
695 * user to use 'zfs create' instead.
697 if (strchr(poolname, '/') != NULL) {
698 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
699 "character '/' in pool name\n"), poolname);
700 (void) fprintf(stderr, gettext("use 'zfs create' to "
701 "create a dataset\n"));
705 /* pass off to get_vdev_spec for bulk processing */
706 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
711 /* make_root_vdev() allows 0 toplevel children if there are spares */
712 if (!zfs_allocatable_devs(nvroot)) {
713 (void) fprintf(stderr, gettext("invalid vdev "
714 "specification: at least one toplevel vdev must be "
720 if (altroot != NULL && altroot[0] != '/') {
721 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
722 "must be an absolute path\n"), altroot);
727 * Check the validity of the mountpoint and direct the user to use the
728 * '-m' mountpoint option if it looks like its in use.
730 if (mountpoint == NULL ||
731 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
732 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
733 char buf[MAXPATHLEN];
736 if (mountpoint && mountpoint[0] != '/') {
737 (void) fprintf(stderr, gettext("invalid mountpoint "
738 "'%s': must be an absolute path, 'legacy', or "
739 "'none'\n"), mountpoint);
743 if (mountpoint == NULL) {
745 (void) snprintf(buf, sizeof (buf), "%s/%s",
748 (void) snprintf(buf, sizeof (buf), "/%s",
752 (void) snprintf(buf, sizeof (buf), "%s%s",
753 altroot, mountpoint);
755 (void) snprintf(buf, sizeof (buf), "%s",
759 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
760 (void) fprintf(stderr, gettext("mountpoint '%s' : "
761 "%s\n"), buf, strerror(errno));
762 (void) fprintf(stderr, gettext("use '-m' "
763 "option to provide a different default\n"));
768 while (count < 3 && readdir(dirp) != NULL)
770 (void) closedir(dirp);
773 (void) fprintf(stderr, gettext("mountpoint "
774 "'%s' exists and is not empty\n"), buf);
775 (void) fprintf(stderr, gettext("use '-m' "
776 "option to provide a "
777 "different default\n"));
785 * For a dry run invocation, print out a basic message and run
786 * through all the vdevs in the list and print out in an
787 * appropriate hierarchy.
789 (void) printf(gettext("would create '%s' with the "
790 "following layout:\n\n"), poolname);
792 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
793 if (num_logs(nvroot) > 0)
794 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
799 * Hand off to libzfs.
801 if (zpool_create(g_zfs, poolname,
802 nvroot, props, fsprops) == 0) {
803 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
804 ZFS_TYPE_FILESYSTEM);
806 if (mountpoint != NULL)
807 verify(zfs_prop_set(pool,
809 ZFS_PROP_MOUNTPOINT),
811 if (zfs_mount(pool, NULL, 0) == 0)
812 ret = zfs_shareall(pool);
815 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
816 (void) fprintf(stderr, gettext("pool name may have "
823 nvlist_free(fsprops);
827 nvlist_free(fsprops);
834 * zpool destroy <pool>
836 * -f Forcefully unmount any datasets
838 * Destroy the given pool. Automatically unmounts any datasets in the pool.
841 zpool_do_destroy(int argc, char **argv)
843 boolean_t force = B_FALSE;
850 while ((c = getopt(argc, argv, "f")) != -1) {
856 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
865 /* check arguments */
867 (void) fprintf(stderr, gettext("missing pool argument\n"));
871 (void) fprintf(stderr, gettext("too many arguments\n"));
877 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
879 * As a special case, check for use of '/' in the name, and
880 * direct the user to use 'zfs destroy' instead.
882 if (strchr(pool, '/') != NULL)
883 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
884 "destroy a dataset\n"));
888 if (zpool_disable_datasets(zhp, force) != 0) {
889 (void) fprintf(stderr, gettext("could not destroy '%s': "
890 "could not unmount datasets\n"), zpool_get_name(zhp));
894 ret = (zpool_destroy(zhp) != 0);
902 * zpool export [-f] <pool> ...
904 * -f Forcefully unmount datasets
906 * Export the given pools. By default, the command will attempt to cleanly
907 * unmount any active datasets within the pool. If the '-f' flag is specified,
908 * then the datasets will be forcefully unmounted.
911 zpool_do_export(int argc, char **argv)
913 boolean_t force = B_FALSE;
914 boolean_t hardforce = B_FALSE;
921 while ((c = getopt(argc, argv, "fF")) != -1) {
930 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
939 /* check arguments */
941 (void) fprintf(stderr, gettext("missing pool argument\n"));
946 for (i = 0; i < argc; i++) {
947 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
952 if (zpool_disable_datasets(zhp, force) != 0) {
959 if (zpool_export_force(zhp) != 0)
961 } else if (zpool_export(zhp, force) != 0) {
972 * Given a vdev configuration, determine the maximum width needed for the device
976 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
978 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
983 if (strlen(name) + depth > max)
984 max = strlen(name) + depth;
988 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
989 &child, &children) == 0) {
990 for (c = 0; c < children; c++)
991 if ((ret = max_width(zhp, child[c], depth + 2,
996 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
997 &child, &children) == 0) {
998 for (c = 0; c < children; c++)
999 if ((ret = max_width(zhp, child[c], depth + 2,
1004 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1005 &child, &children) == 0) {
1006 for (c = 0; c < children; c++)
1007 if ((ret = max_width(zhp, child[c], depth + 2,
1016 typedef struct spare_cbdata {
1018 zpool_handle_t *cb_zhp;
1022 find_vdev(nvlist_t *nv, uint64_t search)
1028 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1032 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1033 &child, &children) == 0) {
1034 for (c = 0; c < children; c++)
1035 if (find_vdev(child[c], search))
1043 find_spare(zpool_handle_t *zhp, void *data)
1045 spare_cbdata_t *cbp = data;
1046 nvlist_t *config, *nvroot;
1048 config = zpool_get_config(zhp, NULL);
1049 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1052 if (find_vdev(nvroot, cbp->cb_guid)) {
1062 * Print out configuration state as requested by status_callback.
1065 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1066 int namewidth, int depth, boolean_t isspare)
1070 pool_scan_stat_t *ps = NULL;
1072 char rbuf[6], wbuf[6], cbuf[6];
1074 uint64_t notpresent;
1078 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1079 &child, &children) != 0)
1082 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1083 (uint64_t **)&vs, &c) == 0);
1085 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1088 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1091 if (vs->vs_aux == VDEV_AUX_SPARED)
1093 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1097 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1101 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1102 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1103 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1104 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1107 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1108 ¬present) == 0) {
1110 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1111 (void) printf(" was %s", path);
1112 } else if (vs->vs_aux != 0) {
1115 switch (vs->vs_aux) {
1116 case VDEV_AUX_OPEN_FAILED:
1117 (void) printf(gettext("cannot open"));
1120 case VDEV_AUX_BAD_GUID_SUM:
1121 (void) printf(gettext("missing device"));
1124 case VDEV_AUX_NO_REPLICAS:
1125 (void) printf(gettext("insufficient replicas"));
1128 case VDEV_AUX_VERSION_NEWER:
1129 (void) printf(gettext("newer version"));
1132 case VDEV_AUX_SPARED:
1133 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1135 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1136 if (strcmp(zpool_get_name(cb.cb_zhp),
1137 zpool_get_name(zhp)) == 0)
1138 (void) printf(gettext("currently in "
1141 (void) printf(gettext("in use by "
1143 zpool_get_name(cb.cb_zhp));
1144 zpool_close(cb.cb_zhp);
1146 (void) printf(gettext("currently in use"));
1150 case VDEV_AUX_ERR_EXCEEDED:
1151 (void) printf(gettext("too many errors"));
1154 case VDEV_AUX_IO_FAILURE:
1155 (void) printf(gettext("experienced I/O failures"));
1158 case VDEV_AUX_BAD_LOG:
1159 (void) printf(gettext("bad intent log"));
1162 case VDEV_AUX_EXTERNAL:
1163 (void) printf(gettext("external device fault"));
1166 case VDEV_AUX_SPLIT_POOL:
1167 (void) printf(gettext("split into new pool"));
1171 (void) printf(gettext("corrupted data"));
1176 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1177 (uint64_t **)&ps, &c);
1179 if (ps && ps->pss_state == DSS_SCANNING &&
1180 vs->vs_scan_processed != 0 && children == 0) {
1181 (void) printf(gettext(" (%s)"),
1182 (ps->pss_func == POOL_SCAN_RESILVER) ?
1183 "resilvering" : "repairing");
1186 (void) printf("\n");
1188 for (c = 0; c < children; c++) {
1189 uint64_t islog = B_FALSE, ishole = B_FALSE;
1191 /* Don't print logs or holes here */
1192 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1194 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1196 if (islog || ishole)
1198 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1199 print_status_config(zhp, vname, child[c],
1200 namewidth, depth + 2, isspare);
1207 * Print the configuration of an exported pool. Iterate over all vdevs in the
1208 * pool, printing out the name and status for each one.
1211 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1218 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1219 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1220 strcmp(type, VDEV_TYPE_HOLE) == 0)
1223 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1224 (uint64_t **)&vs, &c) == 0);
1226 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1227 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1229 if (vs->vs_aux != 0) {
1232 switch (vs->vs_aux) {
1233 case VDEV_AUX_OPEN_FAILED:
1234 (void) printf(gettext("cannot open"));
1237 case VDEV_AUX_BAD_GUID_SUM:
1238 (void) printf(gettext("missing device"));
1241 case VDEV_AUX_NO_REPLICAS:
1242 (void) printf(gettext("insufficient replicas"));
1245 case VDEV_AUX_VERSION_NEWER:
1246 (void) printf(gettext("newer version"));
1249 case VDEV_AUX_ERR_EXCEEDED:
1250 (void) printf(gettext("too many errors"));
1254 (void) printf(gettext("corrupted data"));
1258 (void) printf("\n");
1260 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1261 &child, &children) != 0)
1264 for (c = 0; c < children; c++) {
1265 uint64_t is_log = B_FALSE;
1267 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1272 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1273 print_import_config(vname, child[c], namewidth, depth + 2);
1277 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1278 &child, &children) == 0) {
1279 (void) printf(gettext("\tcache\n"));
1280 for (c = 0; c < children; c++) {
1281 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1282 (void) printf("\t %s\n", vname);
1287 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1288 &child, &children) == 0) {
1289 (void) printf(gettext("\tspares\n"));
1290 for (c = 0; c < children; c++) {
1291 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1292 (void) printf("\t %s\n", vname);
1300 * Logs are recorded as top level vdevs in the main pool child array
1301 * but with "is_log" set to 1. We use either print_status_config() or
1302 * print_import_config() to print the top level logs then any log
1303 * children (eg mirrored slogs) are printed recursively - which
1304 * works because only the top level vdev is marked "is_log"
1307 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1312 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1316 (void) printf(gettext("\tlogs\n"));
1318 for (c = 0; c < children; c++) {
1319 uint64_t is_log = B_FALSE;
1322 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1326 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1328 print_status_config(zhp, name, child[c], namewidth,
1331 print_import_config(name, child[c], namewidth, 2);
1337 * Display the status for the given pool.
1340 show_import(nvlist_t *config)
1342 uint64_t pool_state;
1354 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1356 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1358 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1360 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1363 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1364 (uint64_t **)&vs, &vsc) == 0);
1365 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1367 reason = zpool_import_status(config, &msgid);
1369 (void) printf(gettext(" pool: %s\n"), name);
1370 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1371 (void) printf(gettext(" state: %s"), health);
1372 if (pool_state == POOL_STATE_DESTROYED)
1373 (void) printf(gettext(" (DESTROYED)"));
1374 (void) printf("\n");
1377 case ZPOOL_STATUS_MISSING_DEV_R:
1378 case ZPOOL_STATUS_MISSING_DEV_NR:
1379 case ZPOOL_STATUS_BAD_GUID_SUM:
1380 (void) printf(gettext(" status: One or more devices are "
1381 "missing from the system.\n"));
1384 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1385 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1386 (void) printf(gettext(" status: One or more devices contains "
1387 "corrupted data.\n"));
1390 case ZPOOL_STATUS_CORRUPT_DATA:
1392 gettext(" status: The pool data is corrupted.\n"));
1395 case ZPOOL_STATUS_OFFLINE_DEV:
1396 (void) printf(gettext(" status: One or more devices "
1397 "are offlined.\n"));
1400 case ZPOOL_STATUS_CORRUPT_POOL:
1401 (void) printf(gettext(" status: The pool metadata is "
1405 case ZPOOL_STATUS_VERSION_OLDER:
1406 (void) printf(gettext(" status: The pool is formatted using an "
1407 "older on-disk version.\n"));
1410 case ZPOOL_STATUS_VERSION_NEWER:
1411 (void) printf(gettext(" status: The pool is formatted using an "
1412 "incompatible version.\n"));
1415 case ZPOOL_STATUS_HOSTID_MISMATCH:
1416 (void) printf(gettext(" status: The pool was last accessed by "
1417 "another system.\n"));
1420 case ZPOOL_STATUS_FAULTED_DEV_R:
1421 case ZPOOL_STATUS_FAULTED_DEV_NR:
1422 (void) printf(gettext(" status: One or more devices are "
1426 case ZPOOL_STATUS_BAD_LOG:
1427 (void) printf(gettext(" status: An intent log record cannot be "
1431 case ZPOOL_STATUS_RESILVERING:
1432 (void) printf(gettext(" status: One or more devices were being "
1438 * No other status can be seen when importing pools.
1440 assert(reason == ZPOOL_STATUS_OK);
1444 * Print out an action according to the overall state of the pool.
1446 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1447 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1448 (void) printf(gettext(" action: The pool can be "
1449 "imported using its name or numeric identifier, "
1450 "though\n\tsome features will not be available "
1451 "without an explicit 'zpool upgrade'.\n"));
1452 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1453 (void) printf(gettext(" action: The pool can be "
1454 "imported using its name or numeric "
1455 "identifier and\n\tthe '-f' flag.\n"));
1457 (void) printf(gettext(" action: The pool can be "
1458 "imported using its name or numeric "
1460 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1461 (void) printf(gettext(" action: The pool can be imported "
1462 "despite missing or damaged devices. The\n\tfault "
1463 "tolerance of the pool may be compromised if imported.\n"));
1466 case ZPOOL_STATUS_VERSION_NEWER:
1467 (void) printf(gettext(" action: The pool cannot be "
1468 "imported. Access the pool on a system running "
1469 "newer\n\tsoftware, or recreate the pool from "
1472 case ZPOOL_STATUS_MISSING_DEV_R:
1473 case ZPOOL_STATUS_MISSING_DEV_NR:
1474 case ZPOOL_STATUS_BAD_GUID_SUM:
1475 (void) printf(gettext(" action: The pool cannot be "
1476 "imported. Attach the missing\n\tdevices and try "
1480 (void) printf(gettext(" action: The pool cannot be "
1481 "imported due to damaged devices or data.\n"));
1485 /* Print the comment attached to the pool. */
1486 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1487 (void) printf(gettext("comment: %s\n"), comment);
1490 * If the state is "closed" or "can't open", and the aux state
1491 * is "corrupt data":
1493 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1494 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1495 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1496 if (pool_state == POOL_STATE_DESTROYED)
1497 (void) printf(gettext("\tThe pool was destroyed, "
1498 "but can be imported using the '-Df' flags.\n"));
1499 else if (pool_state != POOL_STATE_EXPORTED)
1500 (void) printf(gettext("\tThe pool may be active on "
1501 "another system, but can be imported using\n\t"
1502 "the '-f' flag.\n"));
1506 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1509 (void) printf(gettext(" config:\n\n"));
1511 namewidth = max_width(NULL, nvroot, 0, 0);
1515 print_import_config(name, nvroot, namewidth, 0);
1516 if (num_logs(nvroot) > 0)
1517 print_logs(NULL, nvroot, namewidth, B_FALSE);
1519 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1520 (void) printf(gettext("\n\tAdditional devices are known to "
1521 "be part of this pool, though their\n\texact "
1522 "configuration cannot be determined.\n"));
1527 * Perform the import for the given configuration. This passes the heavy
1528 * lifting off to zpool_import_props(), and then mounts the datasets contained
1532 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1533 nvlist_t *props, int flags)
1535 zpool_handle_t *zhp;
1540 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1543 verify(nvlist_lookup_uint64(config,
1544 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1545 verify(nvlist_lookup_uint64(config,
1546 ZPOOL_CONFIG_VERSION, &version) == 0);
1547 if (version > SPA_VERSION) {
1548 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1549 "is formatted using a newer ZFS version\n"), name);
1551 } else if (state != POOL_STATE_EXPORTED &&
1552 !(flags & ZFS_IMPORT_ANY_HOST)) {
1555 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1557 unsigned long system_hostid = gethostid() & 0xffffffff;
1559 if ((unsigned long)hostid != system_hostid) {
1564 verify(nvlist_lookup_string(config,
1565 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1566 verify(nvlist_lookup_uint64(config,
1567 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1569 (void) fprintf(stderr, gettext("cannot import "
1570 "'%s': pool may be in use from other "
1571 "system, it was last accessed by %s "
1572 "(hostid: 0x%lx) on %s"), name, hostname,
1573 (unsigned long)hostid,
1574 asctime(localtime(&t)));
1575 (void) fprintf(stderr, gettext("use '-f' to "
1576 "import anyway\n"));
1580 (void) fprintf(stderr, gettext("cannot import '%s': "
1581 "pool may be in use from other system\n"), name);
1582 (void) fprintf(stderr, gettext("use '-f' to import "
1588 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1591 if (newname != NULL)
1592 name = (char *)newname;
1594 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1597 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1598 !(flags & ZFS_IMPORT_ONLY) &&
1599 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1609 * zpool import [-d dir] [-D]
1610 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1611 * [-d dir | -c cachefile] [-f] -a
1612 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1613 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1615 * -c Read pool information from a cachefile instead of searching
1618 * -d Scan in a specific directory, other than /dev/. More than
1619 * one directory can be specified using multiple '-d' options.
1621 * -D Scan for previously destroyed pools or import all or only
1622 * specified destroyed pools.
1624 * -R Temporarily import the pool, with all mountpoints relative to
1625 * the given root. The pool will remain exported when the machine
1628 * -V Import even in the presence of faulted vdevs. This is an
1629 * intentionally undocumented option for testing purposes, and
1630 * treats the pool configuration as complete, leaving any bad
1631 * vdevs in the FAULTED state. In other words, it does verbatim
1634 * -f Force import, even if it appears that the pool is active.
1636 * -F Attempt rewind if necessary.
1638 * -n See if rewind would work, but don't actually rewind.
1640 * -N Import the pool but don't mount datasets.
1642 * -T Specify a starting txg to use for import. This option is
1643 * intentionally undocumented option for testing purposes.
1645 * -a Import all pools found.
1647 * -o Set property=value and/or temporary mount options (without '=').
1649 * The import command scans for pools to import, and import pools based on pool
1650 * name and GUID. The pool can also be renamed as part of the import process.
1653 zpool_do_import(int argc, char **argv)
1655 char **searchdirs = NULL;
1656 char *env, *envdup = NULL;
1660 nvlist_t *pools = NULL;
1661 boolean_t do_all = B_FALSE;
1662 boolean_t do_destroyed = B_FALSE;
1663 char *mntopts = NULL;
1666 uint64_t searchguid = 0;
1667 char *searchname = NULL;
1669 nvlist_t *found_config;
1670 nvlist_t *policy = NULL;
1671 nvlist_t *props = NULL;
1673 int flags = ZFS_IMPORT_NORMAL;
1674 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1675 boolean_t dryrun = B_FALSE;
1676 boolean_t do_rewind = B_FALSE;
1677 boolean_t xtreme_rewind = B_FALSE;
1678 uint64_t pool_state, txg = -1ULL;
1679 char *cachefile = NULL;
1680 importargs_t idata = { 0 };
1684 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1693 if (searchdirs == NULL) {
1694 searchdirs = safe_malloc(sizeof (char *));
1696 char **tmp = safe_malloc((nsearch + 1) *
1698 bcopy(searchdirs, tmp, nsearch *
1703 searchdirs[nsearch++] = optarg;
1706 do_destroyed = B_TRUE;
1709 flags |= ZFS_IMPORT_ANY_HOST;
1715 flags |= ZFS_IMPORT_MISSING_LOG;
1721 flags |= ZFS_IMPORT_ONLY;
1724 if ((propval = strchr(optarg, '=')) != NULL) {
1727 if (add_prop_list(optarg, propval,
1735 if (add_prop_list(zpool_prop_to_name(
1736 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1738 if (nvlist_lookup_string(props,
1739 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1742 if (add_prop_list(zpool_prop_to_name(
1743 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1748 txg = strtoull(optarg, &endptr, 10);
1749 if (errno != 0 || *endptr != '\0') {
1750 (void) fprintf(stderr,
1751 gettext("invalid txg value\n"));
1754 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1757 flags |= ZFS_IMPORT_VERBATIM;
1760 xtreme_rewind = B_TRUE;
1763 (void) fprintf(stderr, gettext("missing argument for "
1764 "'%c' option\n"), optopt);
1768 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1777 if (cachefile && nsearch != 0) {
1778 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1782 if ((dryrun || xtreme_rewind) && !do_rewind) {
1783 (void) fprintf(stderr,
1784 gettext("-n or -X only meaningful with -F\n"));
1788 rewind_policy = ZPOOL_TRY_REWIND;
1790 rewind_policy = ZPOOL_DO_REWIND;
1792 rewind_policy |= ZPOOL_EXTREME_REWIND;
1794 /* In the future, we can capture further policy and include it here */
1795 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1796 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1797 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1800 /* check argument count */
1803 (void) fprintf(stderr, gettext("too many arguments\n"));
1808 (void) fprintf(stderr, gettext("too many arguments\n"));
1813 * Check for the SYS_CONFIG privilege. We do this explicitly
1814 * here because otherwise any attempt to discover pools will
1817 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1818 (void) fprintf(stderr, gettext("cannot "
1819 "discover pools: permission denied\n"));
1820 if (searchdirs != NULL)
1823 nvlist_free(policy);
1829 * Depending on the arguments given, we do one of the following:
1831 * <none> Iterate through all pools and display information about
1834 * -a Iterate through all pools and try to import each one.
1836 * <id> Find the pool that corresponds to the given GUID/pool
1837 * name and import that one.
1839 * -D Above options applies only to destroyed pools.
1845 searchguid = strtoull(argv[0], &endptr, 10);
1846 if (errno != 0 || *endptr != '\0')
1847 searchname = argv[0];
1848 found_config = NULL;
1851 * User specified a name or guid. Ensure it's unique.
1853 idata.unique = B_TRUE;
1857 * Check the environment for the preferred search path.
1859 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
1862 envdup = strdup(env);
1864 dir = strtok(envdup, ":");
1865 while (dir != NULL) {
1866 if (searchdirs == NULL) {
1867 searchdirs = safe_malloc(sizeof (char *));
1869 char **tmp = safe_malloc((nsearch + 1) *
1871 bcopy(searchdirs, tmp, nsearch *
1876 searchdirs[nsearch++] = dir;
1877 dir = strtok(NULL, ":");
1881 idata.path = searchdirs;
1882 idata.paths = nsearch;
1883 idata.poolname = searchname;
1884 idata.guid = searchguid;
1885 idata.cachefile = cachefile;
1887 pools = zpool_search_import(g_zfs, &idata);
1889 if (pools != NULL && idata.exists &&
1890 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1891 (void) fprintf(stderr, gettext("cannot import '%s': "
1892 "a pool with that name already exists\n"),
1894 (void) fprintf(stderr, gettext("use the form '%s "
1895 "<pool | id> <newpool>' to give it a new name\n"),
1898 } else if (pools == NULL && idata.exists) {
1899 (void) fprintf(stderr, gettext("cannot import '%s': "
1900 "a pool with that name is already created/imported,\n"),
1902 (void) fprintf(stderr, gettext("and no additional pools "
1903 "with that name were found\n"));
1905 } else if (pools == NULL) {
1907 (void) fprintf(stderr, gettext("cannot import '%s': "
1908 "no such pool available\n"), argv[0]);
1914 if (searchdirs != NULL)
1918 nvlist_free(policy);
1923 * At this point we have a list of import candidate configs. Even if
1924 * we were searching by pool name or guid, we still need to
1925 * post-process the list to deal with pool state and possible
1931 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1933 verify(nvpair_value_nvlist(elem, &config) == 0);
1935 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1937 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1939 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1942 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1949 (void) printf("\n");
1952 err |= do_import(config, NULL, mntopts,
1955 show_import(config);
1957 } else if (searchname != NULL) {
1961 * We are searching for a pool based on name.
1963 verify(nvlist_lookup_string(config,
1964 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1966 if (strcmp(name, searchname) == 0) {
1967 if (found_config != NULL) {
1968 (void) fprintf(stderr, gettext(
1969 "cannot import '%s': more than "
1970 "one matching pool\n"), searchname);
1971 (void) fprintf(stderr, gettext(
1972 "import by numeric ID instead\n"));
1975 found_config = config;
1981 * Search for a pool by guid.
1983 verify(nvlist_lookup_uint64(config,
1984 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1986 if (guid == searchguid)
1987 found_config = config;
1992 * If we were searching for a specific pool, verify that we found a
1993 * pool, and then do the import.
1995 if (argc != 0 && err == 0) {
1996 if (found_config == NULL) {
1997 (void) fprintf(stderr, gettext("cannot import '%s': "
1998 "no such pool available\n"), argv[0]);
2001 err |= do_import(found_config, argc == 1 ? NULL :
2002 argv[1], mntopts, props, flags);
2007 * If we were just looking for pools, report an error if none were
2010 if (argc == 0 && first)
2011 (void) fprintf(stderr,
2012 gettext("no pools available to import\n"));
2017 nvlist_free(policy);
2018 if (searchdirs != NULL)
2023 return (err ? 1 : 0);
2026 typedef struct iostat_cbdata {
2027 boolean_t cb_verbose;
2030 zpool_list_t *cb_list;
2034 print_iostat_separator(iostat_cbdata_t *cb)
2038 for (i = 0; i < cb->cb_namewidth; i++)
2040 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2044 print_iostat_header(iostat_cbdata_t *cb)
2046 (void) printf("%*s capacity operations bandwidth\n",
2047 cb->cb_namewidth, "");
2048 (void) printf("%-*s alloc free read write read write\n",
2049 cb->cb_namewidth, "pool");
2050 print_iostat_separator(cb);
2054 * Display a single statistic.
2057 print_one_stat(uint64_t value)
2061 zfs_nicenum(value, buf, sizeof (buf));
2062 (void) printf(" %5s", buf);
2066 * Print out all the statistics for the given vdev. This can either be the
2067 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2068 * is a verbose output, and we don't want to display the toplevel pool stats.
2071 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2072 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2074 nvlist_t **oldchild, **newchild;
2076 vdev_stat_t *oldvs, *newvs;
2077 vdev_stat_t zerovs = { 0 };
2082 if (oldnv != NULL) {
2083 verify(nvlist_lookup_uint64_array(oldnv,
2084 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2089 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2090 (uint64_t **)&newvs, &c) == 0);
2092 if (strlen(name) + depth > cb->cb_namewidth)
2093 (void) printf("%*s%s", depth, "", name);
2095 (void) printf("%*s%s%*s", depth, "", name,
2096 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2098 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2103 scale = (double)NANOSEC / tdelta;
2105 /* only toplevel vdevs have capacity stats */
2106 if (newvs->vs_space == 0) {
2107 (void) printf(" - -");
2109 print_one_stat(newvs->vs_alloc);
2110 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2113 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2114 oldvs->vs_ops[ZIO_TYPE_READ])));
2116 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2117 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2119 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2120 oldvs->vs_bytes[ZIO_TYPE_READ])));
2122 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2123 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2125 (void) printf("\n");
2127 if (!cb->cb_verbose)
2130 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2131 &newchild, &children) != 0)
2134 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2135 &oldchild, &c) != 0)
2138 for (c = 0; c < children; c++) {
2139 uint64_t ishole = B_FALSE, islog = B_FALSE;
2141 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2144 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2147 if (ishole || islog)
2150 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2151 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2152 newchild[c], cb, depth + 2);
2157 * Log device section
2160 if (num_logs(newnv) > 0) {
2161 (void) printf("%-*s - - - - - "
2162 "-\n", cb->cb_namewidth, "logs");
2164 for (c = 0; c < children; c++) {
2165 uint64_t islog = B_FALSE;
2166 (void) nvlist_lookup_uint64(newchild[c],
2167 ZPOOL_CONFIG_IS_LOG, &islog);
2170 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2172 print_vdev_stats(zhp, vname, oldnv ?
2173 oldchild[c] : NULL, newchild[c],
2182 * Include level 2 ARC devices in iostat output
2184 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2185 &newchild, &children) != 0)
2188 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2189 &oldchild, &c) != 0)
2193 (void) printf("%-*s - - - - - "
2194 "-\n", cb->cb_namewidth, "cache");
2195 for (c = 0; c < children; c++) {
2196 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2198 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2199 newchild[c], cb, depth + 2);
2206 refresh_iostat(zpool_handle_t *zhp, void *data)
2208 iostat_cbdata_t *cb = data;
2212 * If the pool has disappeared, remove it from the list and continue.
2214 if (zpool_refresh_stats(zhp, &missing) != 0)
2218 pool_list_remove(cb->cb_list, zhp);
2224 * Callback to print out the iostats for the given pool.
2227 print_iostat(zpool_handle_t *zhp, void *data)
2229 iostat_cbdata_t *cb = data;
2230 nvlist_t *oldconfig, *newconfig;
2231 nvlist_t *oldnvroot, *newnvroot;
2233 newconfig = zpool_get_config(zhp, &oldconfig);
2235 if (cb->cb_iteration == 1)
2238 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2241 if (oldconfig == NULL)
2244 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2248 * Print out the statistics for the pool.
2250 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2253 print_iostat_separator(cb);
2265 if (isatty(STDOUT_FILENO)) {
2266 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2268 columns = ws.ws_col;
2277 get_namewidth(zpool_handle_t *zhp, void *data)
2279 iostat_cbdata_t *cb = data;
2280 nvlist_t *config, *nvroot;
2283 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2284 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2286 if (!cb->cb_verbose)
2287 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2289 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2294 * The width must be at least 10, but may be as large as the
2295 * column width - 42 so that we can still fit in one line.
2297 columns = get_columns();
2299 if (cb->cb_namewidth < 10)
2300 cb->cb_namewidth = 10;
2301 if (cb->cb_namewidth > columns - 42)
2302 cb->cb_namewidth = columns - 42;
2308 * Parse the input string, get the 'interval' and 'count' value if there is one.
2311 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2314 unsigned long interval = 0, count = 0;
2318 * Determine if the last argument is an integer or a pool name
2320 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2324 interval = strtoul(argv[argc - 1], &end, 10);
2326 if (*end == '\0' && errno == 0) {
2327 if (interval == 0) {
2328 (void) fprintf(stderr, gettext("interval "
2329 "cannot be zero\n"));
2333 * Ignore the last parameter
2338 * If this is not a valid number, just plow on. The
2339 * user will get a more informative error message later
2347 * If the last argument is also an integer, then we have both a count
2350 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2355 interval = strtoul(argv[argc - 1], &end, 10);
2357 if (*end == '\0' && errno == 0) {
2358 if (interval == 0) {
2359 (void) fprintf(stderr, gettext("interval "
2360 "cannot be zero\n"));
2365 * Ignore the last parameter
2379 get_timestamp_arg(char c)
2382 timestamp_fmt = UDATE;
2384 timestamp_fmt = DDATE;
2390 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2392 * -v Display statistics for individual vdevs
2393 * -T Display a timestamp in date(1) or Unix format
2395 * This command can be tricky because we want to be able to deal with pool
2396 * creation/destruction as well as vdev configuration changes. The bulk of this
2397 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2398 * on pool_list_update() to detect the addition of new pools. Configuration
2399 * changes are all handled within libzfs.
2402 zpool_do_iostat(int argc, char **argv)
2407 unsigned long interval = 0, count = 0;
2409 boolean_t verbose = B_FALSE;
2413 while ((c = getopt(argc, argv, "T:v")) != -1) {
2416 get_timestamp_arg(*optarg);
2422 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2431 get_interval_count(&argc, argv, &interval, &count);
2434 * Construct the list of all interesting pools.
2437 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2440 if (pool_list_count(list) == 0 && argc != 0) {
2441 pool_list_free(list);
2445 if (pool_list_count(list) == 0 && interval == 0) {
2446 pool_list_free(list);
2447 (void) fprintf(stderr, gettext("no pools available\n"));
2452 * Enter the main iostat loop.
2455 cb.cb_verbose = verbose;
2456 cb.cb_iteration = 0;
2457 cb.cb_namewidth = 0;
2460 pool_list_update(list);
2462 if ((npools = pool_list_count(list)) == 0)
2463 (void) fprintf(stderr, gettext("no pools available\n"));
2466 * Refresh all statistics. This is done as an
2467 * explicit step before calculating the maximum name
2468 * width, so that any * configuration changes are
2469 * properly accounted for.
2471 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2475 * Iterate over all pools to determine the maximum width
2476 * for the pool / device name column across all pools.
2478 cb.cb_namewidth = 0;
2479 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2482 if (timestamp_fmt != NODATE)
2483 print_timestamp(timestamp_fmt);
2486 * If it's the first time, or verbose mode, print the
2489 if (++cb.cb_iteration == 1 || verbose)
2490 print_iostat_header(&cb);
2492 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2495 * If there's more than one pool, and we're not in
2496 * verbose mode (which prints a separator for us),
2497 * then print a separator.
2499 if (npools > 1 && !verbose)
2500 print_iostat_separator(&cb);
2503 (void) printf("\n");
2507 * Flush the output so that redirection to a file isn't buffered
2510 (void) fflush(stdout);
2515 if (count != 0 && --count == 0)
2518 (void) sleep(interval);
2521 pool_list_free(list);
2526 typedef struct list_cbdata {
2527 boolean_t cb_verbose;
2529 boolean_t cb_scripted;
2530 zprop_list_t *cb_proplist;
2534 * Given a list of columns to display, output appropriate headers for each one.
2537 print_header(list_cbdata_t *cb)
2539 zprop_list_t *pl = cb->cb_proplist;
2541 boolean_t first = B_TRUE;
2542 boolean_t right_justify;
2545 for (; pl != NULL; pl = pl->pl_next) {
2546 if (pl->pl_prop == ZPROP_INVAL)
2549 width = pl->pl_width;
2550 if (first && cb->cb_verbose) {
2552 * Reset the width to accommodate the verbose listing
2555 width = cb->cb_namewidth;
2563 header = zpool_prop_column_name(pl->pl_prop);
2564 right_justify = zpool_prop_align_right(pl->pl_prop);
2566 if (pl->pl_next == NULL && !right_justify)
2567 (void) printf("%s", header);
2568 else if (right_justify)
2569 (void) printf("%*s", (int)width, header);
2571 (void) printf("%-*s", (int)width, header);
2574 (void) printf("\n");
2578 * Given a pool and a list of properties, print out all the properties according
2579 * to the described layout.
2582 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2584 zprop_list_t *pl = cb->cb_proplist;
2585 boolean_t first = B_TRUE;
2586 char property[ZPOOL_MAXPROPLEN];
2588 boolean_t right_justify;
2591 for (; pl != NULL; pl = pl->pl_next) {
2593 width = pl->pl_width;
2594 if (first && cb->cb_verbose) {
2596 * Reset the width to accommodate the verbose listing
2599 width = cb->cb_namewidth;
2603 if (cb->cb_scripted)
2604 (void) printf("\t");
2611 right_justify = B_FALSE;
2612 if (pl->pl_prop != ZPROP_INVAL) {
2613 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2614 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2616 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2617 sizeof (property), NULL) != 0)
2622 right_justify = zpool_prop_align_right(pl->pl_prop);
2629 * If this is being called in scripted mode, or if this is the
2630 * last column and it is left-justified, don't include a width
2633 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2634 (void) printf("%s", propstr);
2635 else if (right_justify)
2636 (void) printf("%*s", (int)width, propstr);
2638 (void) printf("%-*s", (int)width, propstr);
2641 (void) printf("\n");
2645 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2649 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2651 zfs_nicenum(value, propval, sizeof (propval));
2653 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2654 (void) strlcpy(propval, "-", sizeof (propval));
2657 (void) printf("\t%s", propval);
2659 (void) printf(" %*s", (int)width, propval);
2663 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2664 list_cbdata_t *cb, int depth)
2670 boolean_t scripted = cb->cb_scripted;
2672 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2673 (uint64_t **)&vs, &c) == 0);
2677 (void) printf("\t%s", name);
2678 else if (strlen(name) + depth > cb->cb_namewidth)
2679 (void) printf("%*s%s", depth, "", name);
2681 (void) printf("%*s%s%*s", depth, "", name,
2682 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2684 /* only toplevel vdevs have capacity stats */
2685 if (vs->vs_space == 0) {
2687 (void) printf("\t-\t-\t-");
2689 (void) printf(" - - -");
2691 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2693 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2695 print_one_column(ZPOOL_PROP_FREE,
2696 vs->vs_space - vs->vs_alloc, scripted);
2698 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2700 (void) printf("\n");
2703 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2704 &child, &children) != 0)
2707 for (c = 0; c < children; c++) {
2708 uint64_t ishole = B_FALSE;
2710 if (nvlist_lookup_uint64(child[c],
2711 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2714 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2715 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2720 * Include level 2 ARC devices in iostat output
2722 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2723 &child, &children) != 0)
2727 (void) printf("%-*s - - - - - "
2728 "-\n", cb->cb_namewidth, "cache");
2729 for (c = 0; c < children; c++) {
2730 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2732 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2740 * Generic callback function to list a pool.
2743 list_callback(zpool_handle_t *zhp, void *data)
2745 list_cbdata_t *cbp = data;
2749 config = zpool_get_config(zhp, NULL);
2751 print_pool(zhp, cbp);
2752 if (!cbp->cb_verbose)
2755 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2757 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2763 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2765 * -H Scripted mode. Don't display headers, and separate properties
2767 * -o List of properties to display. Defaults to
2768 * "name,size,allocated,free,capacity,health,altroot"
2769 * -T Display a timestamp in date(1) or Unix format
2771 * List all pools in the system, whether or not they're healthy. Output space
2772 * statistics for each one, as well as health status summary.
2775 zpool_do_list(int argc, char **argv)
2779 list_cbdata_t cb = { 0 };
2780 static char default_props[] =
2781 "name,size,allocated,free,capacity,dedupratio,"
2783 char *props = default_props;
2784 unsigned long interval = 0, count = 0;
2786 boolean_t first = B_TRUE;
2789 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2792 cb.cb_scripted = B_TRUE;
2798 get_timestamp_arg(*optarg);
2801 cb.cb_verbose = B_TRUE;
2804 (void) fprintf(stderr, gettext("missing argument for "
2805 "'%c' option\n"), optopt);
2809 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2818 get_interval_count(&argc, argv, &interval, &count);
2820 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2823 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
2826 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2827 (void) printf(gettext("no pools available\n"));
2828 zprop_free_list(cb.cb_proplist);
2833 pool_list_update(list);
2835 if (pool_list_count(list) == 0)
2838 if (timestamp_fmt != NODATE)
2839 print_timestamp(timestamp_fmt);
2841 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
2845 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
2850 if (count != 0 && --count == 0)
2853 (void) sleep(interval);
2856 zprop_free_list(cb.cb_proplist);
2861 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2863 boolean_t force = B_FALSE;
2866 char *poolname, *old_disk, *new_disk;
2867 zpool_handle_t *zhp;
2871 while ((c = getopt(argc, argv, "f")) != -1) {
2877 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2886 /* get pool name and check number of arguments */
2888 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2895 (void) fprintf(stderr,
2896 gettext("missing <device> specification\n"));
2904 (void) fprintf(stderr,
2905 gettext("missing <new_device> specification\n"));
2908 new_disk = old_disk;
2918 (void) fprintf(stderr, gettext("too many arguments\n"));
2922 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2925 if (zpool_get_config(zhp, NULL) == NULL) {
2926 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2932 nvroot = make_root_vdev(zhp, NULL, force, B_FALSE, replacing, B_FALSE,
2934 if (nvroot == NULL) {
2939 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2941 nvlist_free(nvroot);
2948 * zpool replace [-f] <pool> <device> <new_device>
2950 * -f Force attach, even if <new_device> appears to be in use.
2952 * Replace <device> with <new_device>.
2956 zpool_do_replace(int argc, char **argv)
2958 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2962 * zpool attach [-f] <pool> <device> <new_device>
2964 * -f Force attach, even if <new_device> appears to be in use.
2966 * Attach <new_device> to the mirror containing <device>. If <device> is not
2967 * part of a mirror, then <device> will be transformed into a mirror of
2968 * <device> and <new_device>. In either case, <new_device> will begin life
2969 * with a DTL of [0, now], and will immediately begin to resilver itself.
2972 zpool_do_attach(int argc, char **argv)
2974 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2978 * zpool detach [-f] <pool> <device>
2980 * -f Force detach of <device>, even if DTLs argue against it
2981 * (not supported yet)
2983 * Detach a device from a mirror. The operation will be refused if <device>
2984 * is the last device in the mirror, or if the DTLs indicate that this device
2985 * has the only valid copy of some data.
2989 zpool_do_detach(int argc, char **argv)
2992 char *poolname, *path;
2993 zpool_handle_t *zhp;
2997 while ((c = getopt(argc, argv, "f")) != -1) {
3001 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3010 /* get pool name and check number of arguments */
3012 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3017 (void) fprintf(stderr,
3018 gettext("missing <device> specification\n"));
3025 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3028 ret = zpool_vdev_detach(zhp, path);
3036 * zpool split [-n] [-o prop=val] ...
3038 * [-R altroot] <pool> <newpool> [<device> ...]
3040 * -n Do not split the pool, but display the resulting layout if
3041 * it were to be split.
3042 * -o Set property=value, or set mount options.
3043 * -R Mount the split-off pool under an alternate root.
3045 * Splits the named pool and gives it the new pool name. Devices to be split
3046 * off may be listed, provided that no more than one device is specified
3047 * per top-level vdev mirror. The newly split pool is left in an exported
3048 * state unless -R is specified.
3050 * Restrictions: the top-level of the pool pool must only be made up of
3051 * mirrors; all devices in the pool must be healthy; no device may be
3052 * undergoing a resilvering operation.
3055 zpool_do_split(int argc, char **argv)
3057 char *srcpool, *newpool, *propval;
3058 char *mntopts = NULL;
3061 zpool_handle_t *zhp;
3062 nvlist_t *config, *props = NULL;
3064 flags.dryrun = B_FALSE;
3065 flags.import = B_FALSE;
3068 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3071 flags.import = B_TRUE;
3073 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3074 &props, B_TRUE) != 0) {
3081 flags.dryrun = B_TRUE;
3084 if ((propval = strchr(optarg, '=')) != NULL) {
3087 if (add_prop_list(optarg, propval,
3088 &props, B_TRUE) != 0) {
3098 (void) fprintf(stderr, gettext("missing argument for "
3099 "'%c' option\n"), optopt);
3103 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3110 if (!flags.import && mntopts != NULL) {
3111 (void) fprintf(stderr, gettext("setting mntopts is only "
3112 "valid when importing the pool\n"));
3120 (void) fprintf(stderr, gettext("Missing pool name\n"));
3124 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3134 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3137 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3138 if (config == NULL) {
3142 (void) printf(gettext("would create '%s' with the "
3143 "following layout:\n\n"), newpool);
3144 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3146 nvlist_free(config);
3151 if (ret != 0 || flags.dryrun || !flags.import)
3155 * The split was successful. Now we need to open the new
3156 * pool and import it.
3158 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3160 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3161 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3163 (void) fprintf(stderr, gettext("Split was successful, but "
3164 "the datasets could not all be mounted\n"));
3165 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3166 "different altroot\n"), "zpool import");
3176 * zpool online <pool> <device> ...
3179 zpool_do_online(int argc, char **argv)
3183 zpool_handle_t *zhp;
3185 vdev_state_t newstate;
3189 while ((c = getopt(argc, argv, "et")) != -1) {
3192 flags |= ZFS_ONLINE_EXPAND;
3196 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3205 /* get pool name and check number of arguments */
3207 (void) fprintf(stderr, gettext("missing pool name\n"));
3211 (void) fprintf(stderr, gettext("missing device name\n"));
3217 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3220 for (i = 1; i < argc; i++) {
3221 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3222 if (newstate != VDEV_STATE_HEALTHY) {
3223 (void) printf(gettext("warning: device '%s' "
3224 "onlined, but remains in faulted state\n"),
3226 if (newstate == VDEV_STATE_FAULTED)
3227 (void) printf(gettext("use 'zpool "
3228 "clear' to restore a faulted "
3231 (void) printf(gettext("use 'zpool "
3232 "replace' to replace devices "
3233 "that are no longer present\n"));
3246 * zpool offline [-ft] <pool> <device> ...
3248 * -f Force the device into the offline state, even if doing
3249 * so would appear to compromise pool availability.
3250 * (not supported yet)
3252 * -t Only take the device off-line temporarily. The offline
3253 * state will not be persistent across reboots.
3257 zpool_do_offline(int argc, char **argv)
3261 zpool_handle_t *zhp;
3263 boolean_t istmp = B_FALSE;
3266 while ((c = getopt(argc, argv, "ft")) != -1) {
3273 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3282 /* get pool name and check number of arguments */
3284 (void) fprintf(stderr, gettext("missing pool name\n"));
3288 (void) fprintf(stderr, gettext("missing device name\n"));
3294 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3297 for (i = 1; i < argc; i++) {
3298 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3308 * zpool clear <pool> [device]
3310 * Clear all errors associated with a pool or a particular device.
3313 zpool_do_clear(int argc, char **argv)
3317 boolean_t dryrun = B_FALSE;
3318 boolean_t do_rewind = B_FALSE;
3319 boolean_t xtreme_rewind = B_FALSE;
3320 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3321 nvlist_t *policy = NULL;
3322 zpool_handle_t *zhp;
3323 char *pool, *device;
3326 while ((c = getopt(argc, argv, "FnX")) != -1) {
3335 xtreme_rewind = B_TRUE;
3338 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3348 (void) fprintf(stderr, gettext("missing pool name\n"));
3353 (void) fprintf(stderr, gettext("too many arguments\n"));
3357 if ((dryrun || xtreme_rewind) && !do_rewind) {
3358 (void) fprintf(stderr,
3359 gettext("-n or -X only meaningful with -F\n"));
3363 rewind_policy = ZPOOL_TRY_REWIND;
3365 rewind_policy = ZPOOL_DO_REWIND;
3367 rewind_policy |= ZPOOL_EXTREME_REWIND;
3369 /* In future, further rewind policy choices can be passed along here */
3370 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3371 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3375 device = argc == 2 ? argv[1] : NULL;
3377 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3378 nvlist_free(policy);
3382 if (zpool_clear(zhp, device, policy) != 0)
3387 nvlist_free(policy);
3393 * zpool reguid <pool>
3396 zpool_do_reguid(int argc, char **argv)
3400 zpool_handle_t *zhp;
3404 while ((c = getopt(argc, argv, "")) != -1) {
3407 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3416 /* get pool name and check number of arguments */
3418 (void) fprintf(stderr, gettext("missing pool name\n"));
3423 (void) fprintf(stderr, gettext("too many arguments\n"));
3428 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3431 ret = zpool_reguid(zhp);
3439 * zpool reopen <pool>
3441 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3443 * NOTE: This command is currently undocumented. If the command is ever
3444 * exposed then the appropriate usage() messages will need to be made.
3447 zpool_do_reopen(int argc, char **argv)
3450 zpool_handle_t *zhp;
3460 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3463 ret = zpool_reopen(zhp);
3468 typedef struct scrub_cbdata {
3475 scrub_callback(zpool_handle_t *zhp, void *data)
3477 scrub_cbdata_t *cb = data;
3481 * Ignore faulted pools.
3483 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3484 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3485 "currently unavailable\n"), zpool_get_name(zhp));
3489 err = zpool_scan(zhp, cb->cb_type);
3495 * zpool scrub [-s] <pool> ...
3497 * -s Stop. Stops any in-progress scrub.
3500 zpool_do_scrub(int argc, char **argv)
3505 cb.cb_type = POOL_SCAN_SCRUB;
3508 while ((c = getopt(argc, argv, "s")) != -1) {
3511 cb.cb_type = POOL_SCAN_NONE;
3514 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3526 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3530 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3533 typedef struct status_cbdata {
3535 boolean_t cb_allpools;
3536 boolean_t cb_verbose;
3537 boolean_t cb_explain;
3539 boolean_t cb_dedup_stats;
3543 * Print out detailed scrub status.
3546 print_scan_status(pool_scan_stat_t *ps)
3549 uint64_t elapsed, mins_left, hours_left;
3550 uint64_t pass_exam, examined, total;
3552 double fraction_done;
3553 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3555 (void) printf(gettext(" scan: "));
3557 /* If there's never been a scan, there's not much to say. */
3558 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3559 ps->pss_func >= POOL_SCAN_FUNCS) {
3560 (void) printf(gettext("none requested\n"));
3564 start = ps->pss_start_time;
3565 end = ps->pss_end_time;
3566 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3568 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3569 ps->pss_func == POOL_SCAN_RESILVER);
3571 * Scan is finished or canceled.
3573 if (ps->pss_state == DSS_FINISHED) {
3574 uint64_t minutes_taken = (end - start) / 60;
3577 if (ps->pss_func == POOL_SCAN_SCRUB) {
3578 fmt = gettext("scrub repaired %s in %lluh%um with "
3579 "%llu errors on %s");
3580 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3581 fmt = gettext("resilvered %s in %lluh%um with "
3582 "%llu errors on %s");
3585 (void) printf(fmt, processed_buf,
3586 (u_longlong_t)(minutes_taken / 60),
3587 (uint_t)(minutes_taken % 60),
3588 (u_longlong_t)ps->pss_errors,
3589 ctime((time_t *)&end));
3591 } else if (ps->pss_state == DSS_CANCELED) {
3592 if (ps->pss_func == POOL_SCAN_SCRUB) {
3593 (void) printf(gettext("scrub canceled on %s"),
3595 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3596 (void) printf(gettext("resilver canceled on %s"),
3602 assert(ps->pss_state == DSS_SCANNING);
3605 * Scan is in progress.
3607 if (ps->pss_func == POOL_SCAN_SCRUB) {
3608 (void) printf(gettext("scrub in progress since %s"),
3610 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3611 (void) printf(gettext("resilver in progress since %s"),
3615 examined = ps->pss_examined ? ps->pss_examined : 1;
3616 total = ps->pss_to_examine;
3617 fraction_done = (double)examined / total;
3619 /* elapsed time for this pass */
3620 elapsed = time(NULL) - ps->pss_pass_start;
3621 elapsed = elapsed ? elapsed : 1;
3622 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3623 rate = pass_exam / elapsed;
3624 rate = rate ? rate : 1;
3625 mins_left = ((total - examined) / rate) / 60;
3626 hours_left = mins_left / 60;
3628 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3629 zfs_nicenum(total, total_buf, sizeof (total_buf));
3630 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3633 * do not print estimated time if hours_left is more than 30 days
3635 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3636 examined_buf, total_buf, rate_buf);
3637 if (hours_left < (30 * 24)) {
3638 (void) printf(gettext(", %lluh%um to go\n"),
3639 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3641 (void) printf(gettext(
3642 ", (scan is slow, no estimated time)\n"));
3645 if (ps->pss_func == POOL_SCAN_RESILVER) {
3646 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3647 processed_buf, 100 * fraction_done);
3648 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3649 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3650 processed_buf, 100 * fraction_done);
3655 print_error_log(zpool_handle_t *zhp)
3657 nvlist_t *nverrlist = NULL;
3660 size_t len = MAXPATHLEN * 2;
3662 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3663 (void) printf("errors: List of errors unavailable "
3664 "(insufficient privileges)\n");
3668 (void) printf("errors: Permanent errors have been "
3669 "detected in the following files:\n\n");
3671 pathname = safe_malloc(len);
3673 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3675 uint64_t dsobj, obj;
3677 verify(nvpair_value_nvlist(elem, &nv) == 0);
3678 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3680 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3682 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3683 (void) printf("%7s %s\n", "", pathname);
3686 nvlist_free(nverrlist);
3690 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3699 (void) printf(gettext("\tspares\n"));
3701 for (i = 0; i < nspares; i++) {
3702 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3703 print_status_config(zhp, name, spares[i],
3704 namewidth, 2, B_TRUE);
3710 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3719 (void) printf(gettext("\tcache\n"));
3721 for (i = 0; i < nl2cache; i++) {
3722 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3723 print_status_config(zhp, name, l2cache[i],
3724 namewidth, 2, B_FALSE);
3730 print_dedup_stats(nvlist_t *config)
3732 ddt_histogram_t *ddh;
3738 * If the pool was faulted then we may not have been able to
3739 * obtain the config. Otherwise, if have anything in the dedup
3740 * table continue processing the stats.
3742 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3743 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3746 (void) printf("\n");
3747 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3748 (u_longlong_t)ddo->ddo_count,
3749 (u_longlong_t)ddo->ddo_dspace,
3750 (u_longlong_t)ddo->ddo_mspace);
3752 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3753 (uint64_t **)&dds, &c) == 0);
3754 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3755 (uint64_t **)&ddh, &c) == 0);
3756 zpool_dump_ddt(dds, ddh);
3760 * Display a summary of pool status. Displays a summary such as:
3764 * reason: One or more devices ...
3765 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
3771 * When given the '-v' option, we print out the complete config. If the '-e'
3772 * option is specified, then we print out error rate information as well.
3775 status_callback(zpool_handle_t *zhp, void *data)
3777 status_cbdata_t *cbp = data;
3778 nvlist_t *config, *nvroot;
3785 config = zpool_get_config(zhp, NULL);
3786 reason = zpool_get_status(zhp, &msgid);
3791 * If we were given 'zpool status -x', only report those pools with
3794 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3795 if (!cbp->cb_allpools) {
3796 (void) printf(gettext("pool '%s' is healthy\n"),
3797 zpool_get_name(zhp));
3799 cbp->cb_first = B_FALSE;
3805 cbp->cb_first = B_FALSE;
3807 (void) printf("\n");
3809 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3811 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3812 (uint64_t **)&vs, &c) == 0);
3813 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3815 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3816 (void) printf(gettext(" state: %s\n"), health);
3819 case ZPOOL_STATUS_MISSING_DEV_R:
3820 (void) printf(gettext("status: One or more devices could not "
3821 "be opened. Sufficient replicas exist for\n\tthe pool to "
3822 "continue functioning in a degraded state.\n"));
3823 (void) printf(gettext("action: Attach the missing device and "
3824 "online it using 'zpool online'.\n"));
3827 case ZPOOL_STATUS_MISSING_DEV_NR:
3828 (void) printf(gettext("status: One or more devices could not "
3829 "be opened. There are insufficient\n\treplicas for the "
3830 "pool to continue functioning.\n"));
3831 (void) printf(gettext("action: Attach the missing device and "
3832 "online it using 'zpool online'.\n"));
3835 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3836 (void) printf(gettext("status: One or more devices could not "
3837 "be used because the label is missing or\n\tinvalid. "
3838 "Sufficient replicas exist for the pool to continue\n\t"
3839 "functioning in a degraded state.\n"));
3840 (void) printf(gettext("action: Replace the device using "
3841 "'zpool replace'.\n"));
3844 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3845 (void) printf(gettext("status: One or more devices could not "
3846 "be used because the label is missing \n\tor invalid. "
3847 "There are insufficient replicas for the pool to "
3848 "continue\n\tfunctioning.\n"));
3849 zpool_explain_recover(zpool_get_handle(zhp),
3850 zpool_get_name(zhp), reason, config);
3853 case ZPOOL_STATUS_FAILING_DEV:
3854 (void) printf(gettext("status: One or more devices has "
3855 "experienced an unrecoverable error. An\n\tattempt was "
3856 "made to correct the error. Applications are "
3858 (void) printf(gettext("action: Determine if the device needs "
3859 "to be replaced, and clear the errors\n\tusing "
3860 "'zpool clear' or replace the device with 'zpool "
3864 case ZPOOL_STATUS_OFFLINE_DEV:
3865 (void) printf(gettext("status: One or more devices has "
3866 "been taken offline by the administrator.\n\tSufficient "
3867 "replicas exist for the pool to continue functioning in "
3868 "a\n\tdegraded state.\n"));
3869 (void) printf(gettext("action: Online the device using "
3870 "'zpool online' or replace the device with\n\t'zpool "
3874 case ZPOOL_STATUS_REMOVED_DEV:
3875 (void) printf(gettext("status: One or more devices has "
3876 "been removed by the administrator.\n\tSufficient "
3877 "replicas exist for the pool to continue functioning in "
3878 "a\n\tdegraded state.\n"));
3879 (void) printf(gettext("action: Online the device using "
3880 "'zpool online' or replace the device with\n\t'zpool "
3884 case ZPOOL_STATUS_RESILVERING:
3885 (void) printf(gettext("status: One or more devices is "
3886 "currently being resilvered. The pool will\n\tcontinue "
3887 "to function, possibly in a degraded state.\n"));
3888 (void) printf(gettext("action: Wait for the resilver to "
3892 case ZPOOL_STATUS_CORRUPT_DATA:
3893 (void) printf(gettext("status: One or more devices has "
3894 "experienced an error resulting in data\n\tcorruption. "
3895 "Applications may be affected.\n"));
3896 (void) printf(gettext("action: Restore the file in question "
3897 "if possible. Otherwise restore the\n\tentire pool from "
3901 case ZPOOL_STATUS_CORRUPT_POOL:
3902 (void) printf(gettext("status: The pool metadata is corrupted "
3903 "and the pool cannot be opened.\n"));
3904 zpool_explain_recover(zpool_get_handle(zhp),
3905 zpool_get_name(zhp), reason, config);
3908 case ZPOOL_STATUS_VERSION_OLDER:
3909 (void) printf(gettext("status: The pool is formatted using an "
3910 "older on-disk format. The pool can\n\tstill be used, but "
3911 "some features are unavailable.\n"));
3912 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3913 "upgrade'. Once this is done, the\n\tpool will no longer "
3914 "be accessible on older software versions.\n"));
3917 case ZPOOL_STATUS_VERSION_NEWER:
3918 (void) printf(gettext("status: The pool has been upgraded to a "
3919 "newer, incompatible on-disk version.\n\tThe pool cannot "
3920 "be accessed on this system.\n"));
3921 (void) printf(gettext("action: Access the pool from a system "
3922 "running more recent software, or\n\trestore the pool from "
3926 case ZPOOL_STATUS_FAULTED_DEV_R:
3927 (void) printf(gettext("status: One or more devices are "
3928 "faulted in response to persistent errors.\n\tSufficient "
3929 "replicas exist for the pool to continue functioning "
3930 "in a\n\tdegraded state.\n"));
3931 (void) printf(gettext("action: Replace the faulted device, "
3932 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3935 case ZPOOL_STATUS_FAULTED_DEV_NR:
3936 (void) printf(gettext("status: One or more devices are "
3937 "faulted in response to persistent errors. There are "
3938 "insufficient replicas for the pool to\n\tcontinue "
3940 (void) printf(gettext("action: Destroy and re-create the pool "
3941 "from a backup source. Manually marking the device\n"
3942 "\trepaired using 'zpool clear' may allow some data "
3943 "to be recovered.\n"));
3946 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3947 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3948 (void) printf(gettext("status: One or more devices are "
3949 "faulted in response to IO failures.\n"));
3950 (void) printf(gettext("action: Make sure the affected devices "
3951 "are connected, then run 'zpool clear'.\n"));
3954 case ZPOOL_STATUS_BAD_LOG:
3955 (void) printf(gettext("status: An intent log record "
3956 "could not be read.\n"
3957 "\tWaiting for adminstrator intervention to fix the "
3958 "faulted pool.\n"));
3959 (void) printf(gettext("action: Either restore the affected "
3960 "device(s) and run 'zpool online',\n"
3961 "\tor ignore the intent log records by running "
3962 "'zpool clear'.\n"));
3967 * The remaining errors can't actually be generated, yet.
3969 assert(reason == ZPOOL_STATUS_OK);
3973 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
3976 if (config != NULL) {
3979 nvlist_t **spares, **l2cache;
3980 uint_t nspares, nl2cache;
3981 pool_scan_stat_t *ps = NULL;
3983 (void) nvlist_lookup_uint64_array(nvroot,
3984 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3985 print_scan_status(ps);
3987 namewidth = max_width(zhp, nvroot, 0, 0);
3991 (void) printf(gettext("config:\n\n"));
3992 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3993 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3994 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3995 namewidth, 0, B_FALSE);
3997 if (num_logs(nvroot) > 0)
3998 print_logs(zhp, nvroot, namewidth, B_TRUE);
3999 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4000 &l2cache, &nl2cache) == 0)
4001 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4003 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4004 &spares, &nspares) == 0)
4005 print_spares(zhp, spares, nspares, namewidth);
4007 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4009 nvlist_t *nverrlist = NULL;
4012 * If the approximate error count is small, get a
4013 * precise count by fetching the entire log and
4014 * uniquifying the results.
4016 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4017 zpool_get_errlog(zhp, &nverrlist) == 0) {
4022 while ((elem = nvlist_next_nvpair(nverrlist,
4027 nvlist_free(nverrlist);
4029 (void) printf("\n");
4032 (void) printf(gettext("errors: No known data "
4034 else if (!cbp->cb_verbose)
4035 (void) printf(gettext("errors: %llu data "
4036 "errors, use '-v' for a list\n"),
4037 (u_longlong_t)nerr);
4039 print_error_log(zhp);
4042 if (cbp->cb_dedup_stats)
4043 print_dedup_stats(config);
4045 (void) printf(gettext("config: The configuration cannot be "
4053 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4055 * -v Display complete error logs
4056 * -x Display only pools with potential problems
4057 * -D Display dedup status (undocumented)
4058 * -T Display a timestamp in date(1) or Unix format
4060 * Describes the health status of all pools or some subset.
4063 zpool_do_status(int argc, char **argv)
4067 unsigned long interval = 0, count = 0;
4068 status_cbdata_t cb = { 0 };
4071 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4074 cb.cb_verbose = B_TRUE;
4077 cb.cb_explain = B_TRUE;
4080 cb.cb_dedup_stats = B_TRUE;
4083 get_timestamp_arg(*optarg);
4086 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4095 get_interval_count(&argc, argv, &interval, &count);
4098 cb.cb_allpools = B_TRUE;
4100 cb.cb_first = B_TRUE;
4103 if (timestamp_fmt != NODATE)
4104 print_timestamp(timestamp_fmt);
4106 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4107 status_callback, &cb);
4109 if (argc == 0 && cb.cb_count == 0)
4110 (void) fprintf(stderr, gettext("no pools available\n"));
4111 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4112 (void) printf(gettext("all pools are healthy\n"));
4120 if (count != 0 && --count == 0)
4123 (void) sleep(interval);
4129 typedef struct upgrade_cbdata {
4134 uint64_t cb_version;
4139 upgrade_cb(zpool_handle_t *zhp, void *arg)
4141 upgrade_cbdata_t *cbp = arg;
4146 config = zpool_get_config(zhp, NULL);
4147 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4150 if (!cbp->cb_newer && version < SPA_VERSION) {
4152 if (cbp->cb_first) {
4153 (void) printf(gettext("The following pools are "
4154 "out of date, and can be upgraded. After "
4155 "being\nupgraded, these pools will no "
4156 "longer be accessible by older software "
4158 (void) printf(gettext("VER POOL\n"));
4159 (void) printf(gettext("--- ------------\n"));
4160 cbp->cb_first = B_FALSE;
4163 (void) printf("%2llu %s\n", (u_longlong_t)version,
4164 zpool_get_name(zhp));
4166 cbp->cb_first = B_FALSE;
4167 ret = zpool_upgrade(zhp, cbp->cb_version);
4169 (void) printf(gettext("Successfully upgraded "
4170 "'%s'\n\n"), zpool_get_name(zhp));
4173 } else if (cbp->cb_newer && version > SPA_VERSION) {
4174 assert(!cbp->cb_all);
4176 if (cbp->cb_first) {
4177 (void) printf(gettext("The following pools are "
4178 "formatted using a newer software version and\n"
4179 "cannot be accessed on the current system.\n\n"));
4180 (void) printf(gettext("VER POOL\n"));
4181 (void) printf(gettext("--- ------------\n"));
4182 cbp->cb_first = B_FALSE;
4185 (void) printf("%2llu %s\n", (u_longlong_t)version,
4186 zpool_get_name(zhp));
4195 upgrade_one(zpool_handle_t *zhp, void *data)
4197 upgrade_cbdata_t *cbp = data;
4198 uint64_t cur_version;
4201 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4202 (void) printf(gettext("'log' is now a reserved word\n"
4203 "Pool 'log' must be renamed using export and import"
4208 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4209 if (cur_version > cbp->cb_version) {
4210 (void) printf(gettext("Pool '%s' is already formatted "
4211 "using more current version '%llu'.\n"),
4212 zpool_get_name(zhp), (u_longlong_t) cur_version);
4215 if (cur_version == cbp->cb_version) {
4216 (void) printf(gettext("Pool '%s' is already formatted "
4217 "using the current version.\n"), zpool_get_name(zhp));
4221 ret = zpool_upgrade(zhp, cbp->cb_version);
4224 (void) printf(gettext("Successfully upgraded '%s' "
4225 "from version %llu to version %llu\n\n"),
4226 zpool_get_name(zhp), (u_longlong_t)cur_version,
4227 (u_longlong_t)cbp->cb_version);
4236 * zpool upgrade [-V version] <-a | pool ...>
4238 * With no arguments, display downrev'd ZFS pool available for upgrade.
4239 * Individual pools can be upgraded by specifying the pool, and '-a' will
4240 * upgrade all pools.
4243 zpool_do_upgrade(int argc, char **argv)
4246 upgrade_cbdata_t cb = { 0 };
4248 boolean_t showversions = B_FALSE;
4253 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4259 showversions = B_TRUE;
4262 cb.cb_version = strtoll(optarg, &end, 10);
4263 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4264 cb.cb_version < SPA_VERSION_1) {
4265 (void) fprintf(stderr,
4266 gettext("invalid version '%s'\n"), optarg);
4271 (void) fprintf(stderr, gettext("missing argument for "
4272 "'%c' option\n"), optopt);
4276 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4287 if (cb.cb_version == 0) {
4288 cb.cb_version = SPA_VERSION;
4289 } else if (!cb.cb_all && argc == 0) {
4290 (void) fprintf(stderr, gettext("-V option is "
4291 "incompatible with other arguments\n"));
4296 if (cb.cb_all || argc != 0) {
4297 (void) fprintf(stderr, gettext("-v option is "
4298 "incompatible with other arguments\n"));
4301 } else if (cb.cb_all) {
4303 (void) fprintf(stderr, gettext("-a option should not "
4304 "be used along with a pool name\n"));
4309 (void) printf(gettext("This system is currently running "
4310 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4311 cb.cb_first = B_TRUE;
4313 (void) printf(gettext("The following versions are "
4315 (void) printf(gettext("VER DESCRIPTION\n"));
4316 (void) printf("--- -----------------------------------------"
4317 "---------------\n");
4318 (void) printf(gettext(" 1 Initial ZFS version\n"));
4319 (void) printf(gettext(" 2 Ditto blocks "
4320 "(replicated metadata)\n"));
4321 (void) printf(gettext(" 3 Hot spares and double parity "
4323 (void) printf(gettext(" 4 zpool history\n"));
4324 (void) printf(gettext(" 5 Compression using the gzip "
4326 (void) printf(gettext(" 6 bootfs pool property\n"));
4327 (void) printf(gettext(" 7 Separate intent log devices\n"));
4328 (void) printf(gettext(" 8 Delegated administration\n"));
4329 (void) printf(gettext(" 9 refquota and refreservation "
4331 (void) printf(gettext(" 10 Cache devices\n"));
4332 (void) printf(gettext(" 11 Improved scrub performance\n"));
4333 (void) printf(gettext(" 12 Snapshot properties\n"));
4334 (void) printf(gettext(" 13 snapused property\n"));
4335 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4336 (void) printf(gettext(" 15 user/group space accounting\n"));
4337 (void) printf(gettext(" 16 stmf property support\n"));
4338 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4339 (void) printf(gettext(" 18 Snapshot user holds\n"));
4340 (void) printf(gettext(" 19 Log device removal\n"));
4341 (void) printf(gettext(" 20 Compression using zle "
4342 "(zero-length encoding)\n"));
4343 (void) printf(gettext(" 21 Deduplication\n"));
4344 (void) printf(gettext(" 22 Received properties\n"));
4345 (void) printf(gettext(" 23 Slim ZIL\n"));
4346 (void) printf(gettext(" 24 System attributes\n"));
4347 (void) printf(gettext(" 25 Improved scrub stats\n"));
4348 (void) printf(gettext(" 26 Improved snapshot deletion "
4350 (void) printf(gettext(" 27 Improved snapshot creation "
4352 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4353 (void) printf(gettext("\nFor more information on a particular "
4354 "version, including supported releases,\n"));
4355 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4356 } else if (argc == 0) {
4359 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4360 notfound = cb.cb_first;
4362 if (!cb.cb_all && ret == 0) {
4364 (void) printf("\n");
4365 cb.cb_first = B_TRUE;
4366 cb.cb_newer = B_TRUE;
4367 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4370 (void) printf("\n");
4376 (void) printf(gettext("All pools are formatted "
4377 "using this version.\n"));
4378 else if (!cb.cb_all)
4379 (void) printf(gettext("Use 'zpool upgrade -v' "
4380 "for a list of available versions and "
4381 "their associated\nfeatures.\n"));
4384 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4391 typedef struct hist_cbdata {
4398 * Print out the command history for a specific pool.
4401 get_history_one(zpool_handle_t *zhp, void *data)
4417 char internalstr[MAXPATHLEN];
4418 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4422 cb->first = B_FALSE;
4424 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4426 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4429 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4430 &records, &numrecords) == 0);
4431 for (i = 0; i < numrecords; i++) {
4432 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4436 /* is it an internal event or a standard event? */
4437 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4439 if (cb->internal == 0)
4442 if (nvlist_lookup_uint64(records[i],
4443 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4445 verify(nvlist_lookup_uint64(records[i],
4446 ZPOOL_HIST_TXG, &txg) == 0);
4447 verify(nvlist_lookup_string(records[i],
4448 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4449 if (ievent >= LOG_END)
4451 (void) snprintf(internalstr,
4452 sizeof (internalstr),
4453 "[internal %s txg:%llu] %s",
4454 zfs_history_event_names[ievent], (u_longlong_t)txg,
4456 cmdstr = internalstr;
4459 (void) localtime_r(&tsec, &t);
4460 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4461 (void) printf("%s %s", tbuf, cmdstr);
4464 (void) printf("\n");
4467 (void) printf(" [");
4468 if (nvlist_lookup_uint64(records[i],
4469 ZPOOL_HIST_WHO, &who) == 0) {
4470 pwd = getpwuid((uid_t)who);
4472 (void) printf("user %s on",
4475 (void) printf("user %d on",
4478 (void) printf(gettext("no info]\n"));
4481 if (nvlist_lookup_string(records[i],
4482 ZPOOL_HIST_HOST, &hostname) == 0) {
4483 (void) printf(" %s", hostname);
4485 if (nvlist_lookup_string(records[i],
4486 ZPOOL_HIST_ZONE, &zonename) == 0) {
4487 (void) printf(":%s", zonename);
4491 (void) printf("\n");
4493 (void) printf("\n");
4500 * zpool history <pool>
4502 * Displays the history of commands that modified pools.
4507 zpool_do_history(int argc, char **argv)
4509 hist_cbdata_t cbdata = { 0 };
4513 cbdata.first = B_TRUE;
4515 while ((c = getopt(argc, argv, "li")) != -1) {
4521 cbdata.internal = 1;
4524 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4532 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4535 if (argc == 0 && cbdata.first == B_TRUE) {
4536 (void) fprintf(stderr, gettext("no pools available\n"));
4543 typedef struct ev_opts {
4551 zpool_do_events_short(nvlist_t *nvl)
4553 char ctime_str[26], str[32], *ptr;
4557 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
4558 memset(str, ' ', 32);
4559 (void) ctime_r((const time_t *)&tv[0], ctime_str);
4560 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
4561 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
4562 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
4563 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
4564 (void) printf(gettext("%s "), str);
4566 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
4567 (void) printf(gettext("%s\n"), ptr);
4571 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
4575 for (nvp = nvlist_next_nvpair(nvl, NULL);
4576 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
4578 data_type_t type = nvpair_type(nvp);
4579 const char *name = nvpair_name(nvp);
4589 printf(gettext("%*s%s = "), depth, "", name);
4592 case DATA_TYPE_BOOLEAN:
4593 printf(gettext("%s"), "1");
4596 case DATA_TYPE_BOOLEAN_VALUE:
4597 (void) nvpair_value_boolean_value(nvp, &b);
4598 printf(gettext("%s"), b ? "1" : "0");
4601 case DATA_TYPE_BYTE:
4602 (void) nvpair_value_byte(nvp, &i8);
4603 printf(gettext("0x%x"), i8);
4606 case DATA_TYPE_INT8:
4607 (void) nvpair_value_int8(nvp, (void *)&i8);
4608 printf(gettext("0x%x"), i8);
4611 case DATA_TYPE_UINT8:
4612 (void) nvpair_value_uint8(nvp, &i8);
4613 printf(gettext("0x%x"), i8);
4616 case DATA_TYPE_INT16:
4617 (void) nvpair_value_int16(nvp, (void *)&i16);
4618 printf(gettext("0x%x"), i16);
4621 case DATA_TYPE_UINT16:
4622 (void) nvpair_value_uint16(nvp, &i16);
4623 printf(gettext("0x%x"), i16);
4626 case DATA_TYPE_INT32:
4627 (void) nvpair_value_int32(nvp, (void *)&i32);
4628 printf(gettext("0x%x"), i32);
4631 case DATA_TYPE_UINT32:
4632 (void) nvpair_value_uint32(nvp, &i32);
4633 printf(gettext("0x%x"), i32);
4636 case DATA_TYPE_INT64:
4637 (void) nvpair_value_int64(nvp, (void *)&i64);
4638 printf(gettext("0x%llx"), (u_longlong_t)i64);
4641 case DATA_TYPE_UINT64:
4642 (void) nvpair_value_uint64(nvp, &i64);
4643 printf(gettext("0x%llx"), (u_longlong_t)i64);
4646 case DATA_TYPE_HRTIME:
4647 (void) nvpair_value_hrtime(nvp, (void *)&i64);
4648 printf(gettext("0x%llx"), (u_longlong_t)i64);
4651 case DATA_TYPE_STRING:
4652 (void) nvpair_value_string(nvp, &str);
4653 printf(gettext("\"%s\""), str ? str : "<NULL>");
4656 case DATA_TYPE_NVLIST:
4657 printf(gettext("(embedded nvlist)\n"));
4658 (void) nvpair_value_nvlist(nvp, &cnv);
4659 zpool_do_events_nvprint(cnv, depth + 8);
4660 printf(gettext("%*s(end %s)"), depth, "", name);
4663 case DATA_TYPE_NVLIST_ARRAY: {
4667 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
4668 printf(gettext("(%d embedded nvlists)\n"), nelem);
4669 for (i = 0; i < nelem; i++) {
4670 printf(gettext("%*s%s[%d] = %s\n"),
4671 depth, "", name, i, "(embedded nvlist)");
4672 zpool_do_events_nvprint(val[i], depth + 8);
4673 printf(gettext("%*s(end %s[%i])\n"),
4674 depth, "", name, i);
4676 printf(gettext("%*s(end %s)\n"), depth, "", name);
4680 case DATA_TYPE_INT8_ARRAY: {
4684 (void) nvpair_value_int8_array(nvp, &val, &nelem);
4685 for (i = 0; i < nelem; i++)
4686 printf(gettext("0x%x "), val[i]);
4691 case DATA_TYPE_UINT8_ARRAY: {
4695 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
4696 for (i = 0; i < nelem; i++)
4697 printf(gettext("0x%x "), val[i]);
4702 case DATA_TYPE_INT16_ARRAY: {
4706 (void) nvpair_value_int16_array(nvp, &val, &nelem);
4707 for (i = 0; i < nelem; i++)
4708 printf(gettext("0x%x "), val[i]);
4713 case DATA_TYPE_UINT16_ARRAY: {
4717 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
4718 for (i = 0; i < nelem; i++)
4719 printf(gettext("0x%x "), val[i]);
4724 case DATA_TYPE_INT32_ARRAY: {
4728 (void) nvpair_value_int32_array(nvp, &val, &nelem);
4729 for (i = 0; i < nelem; i++)
4730 printf(gettext("0x%x "), val[i]);
4735 case DATA_TYPE_UINT32_ARRAY: {
4739 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
4740 for (i = 0; i < nelem; i++)
4741 printf(gettext("0x%x "), val[i]);
4746 case DATA_TYPE_INT64_ARRAY: {
4750 (void) nvpair_value_int64_array(nvp, &val, &nelem);
4751 for (i = 0; i < nelem; i++)
4752 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4757 case DATA_TYPE_UINT64_ARRAY: {
4761 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
4762 for (i = 0; i < nelem; i++)
4763 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4768 case DATA_TYPE_STRING_ARRAY:
4769 case DATA_TYPE_BOOLEAN_ARRAY:
4770 case DATA_TYPE_BYTE_ARRAY:
4771 case DATA_TYPE_DOUBLE:
4772 case DATA_TYPE_UNKNOWN:
4773 printf(gettext("<unknown>"));
4777 printf(gettext("\n"));
4782 zpool_do_events_next(ev_opts_t *opts)
4785 int cleanup_fd, ret, dropped;
4787 cleanup_fd = open(ZFS_DEV, O_RDWR);
4788 VERIFY(cleanup_fd >= 0);
4790 if (!opts->scripted)
4791 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
4794 ret = zpool_events_next(g_zfs, &nvl, &dropped,
4795 !!opts->follow, cleanup_fd);
4796 if (ret || nvl == NULL)
4800 (void) printf(gettext("dropped %d events\n"), dropped);
4802 zpool_do_events_short(nvl);
4804 if (opts->verbose) {
4805 zpool_do_events_nvprint(nvl, 8);
4806 printf(gettext("\n"));
4812 VERIFY(0 == close(cleanup_fd));
4818 zpool_do_events_clear(ev_opts_t *opts)
4822 ret = zpool_events_clear(g_zfs, &count);
4824 (void) printf(gettext("cleared %d events\n"), count);
4830 * zpool events [-vfc]
4832 * Displays events logs by ZFS.
4835 zpool_do_events(int argc, char **argv)
4837 ev_opts_t opts = { 0 };
4842 while ((c = getopt(argc, argv, "vHfc")) != -1) {
4857 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4866 ret = zpool_do_events_clear(&opts);
4868 ret = zpool_do_events_next(&opts);
4874 get_callback(zpool_handle_t *zhp, void *data)
4876 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4877 char value[MAXNAMELEN];
4878 zprop_source_t srctype;
4881 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4884 * Skip the special fake placeholder. This will also skip
4885 * over the name property when 'all' is specified.
4887 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4888 pl == cbp->cb_proplist)
4891 if (zpool_get_prop(zhp, pl->pl_prop,
4892 value, sizeof (value), &srctype) != 0)
4895 zprop_print_one_property(zpool_get_name(zhp), cbp,
4896 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4903 zpool_do_get(int argc, char **argv)
4905 zprop_get_cbdata_t cb = { 0 };
4906 zprop_list_t fake_name = { 0 };
4912 cb.cb_first = B_TRUE;
4913 cb.cb_sources = ZPROP_SRC_ALL;
4914 cb.cb_columns[0] = GET_COL_NAME;
4915 cb.cb_columns[1] = GET_COL_PROPERTY;
4916 cb.cb_columns[2] = GET_COL_VALUE;
4917 cb.cb_columns[3] = GET_COL_SOURCE;
4918 cb.cb_type = ZFS_TYPE_POOL;
4920 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4921 ZFS_TYPE_POOL) != 0)
4924 if (cb.cb_proplist != NULL) {
4925 fake_name.pl_prop = ZPOOL_PROP_NAME;
4926 fake_name.pl_width = strlen(gettext("NAME"));
4927 fake_name.pl_next = cb.cb_proplist;
4928 cb.cb_proplist = &fake_name;
4931 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4934 if (cb.cb_proplist == &fake_name)
4935 zprop_free_list(fake_name.pl_next);
4937 zprop_free_list(cb.cb_proplist);
4942 typedef struct set_cbdata {
4945 boolean_t cb_any_successful;
4949 set_callback(zpool_handle_t *zhp, void *data)
4952 set_cbdata_t *cb = (set_cbdata_t *)data;
4954 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4957 cb->cb_any_successful = B_TRUE;
4963 zpool_do_set(int argc, char **argv)
4965 set_cbdata_t cb = { 0 };
4968 if (argc > 1 && argv[1][0] == '-') {
4969 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4975 (void) fprintf(stderr, gettext("missing property=value "
4981 (void) fprintf(stderr, gettext("missing pool name\n"));
4986 (void) fprintf(stderr, gettext("too many pool names\n"));
4990 cb.cb_propname = argv[1];
4991 cb.cb_value = strchr(cb.cb_propname, '=');
4992 if (cb.cb_value == NULL) {
4993 (void) fprintf(stderr, gettext("missing value in "
4994 "property=value argument\n"));
4998 *(cb.cb_value) = '\0';
5001 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5008 find_command_idx(char *command, int *idx)
5012 for (i = 0; i < NCOMMAND; i++) {
5013 if (command_table[i].name == NULL)
5016 if (strcmp(command, command_table[i].name) == 0) {
5025 main(int argc, char **argv)
5031 (void) setlocale(LC_ALL, "");
5032 (void) textdomain(TEXT_DOMAIN);
5037 * Make sure the user has specified some command.
5040 (void) fprintf(stderr, gettext("missing command\n"));
5049 if ((strcmp(cmdname, "-?") == 0) ||
5050 strcmp(cmdname, "--help") == 0)
5053 if ((g_zfs = libzfs_init()) == NULL)
5056 libzfs_print_on_error(g_zfs, B_TRUE);
5058 zpool_set_history_str("zpool", argc, argv, history_str);
5059 verify(zpool_stage_history(g_zfs, history_str) == 0);
5062 * Run the appropriate command.
5064 if (find_command_idx(cmdname, &i) == 0) {
5065 current_command = &command_table[i];
5066 ret = command_table[i].func(argc - 1, argv + 1);
5067 } else if (strchr(cmdname, '=')) {
5068 verify(find_command_idx("set", &i) == 0);
5069 current_command = &command_table[i];
5070 ret = command_table[i].func(argc, argv);
5071 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5073 * 'freeze' is a vile debugging abomination, so we treat
5077 int fd = open(ZFS_DEV, O_RDWR);
5078 (void) strcpy((void *)buf, argv[2]);
5079 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5081 (void) fprintf(stderr, gettext("unrecognized "
5082 "command '%s'\n"), cmdname);
5090 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5091 * for the purposes of running ::findleaks.
5093 if (getenv("ZFS_ABORT") != NULL) {
5094 (void) printf("dumping core by request\n");