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.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
48 #include <sys/fs/zfs.h>
50 #include <sys/fm/util.h>
51 #include <sys/fm/protocol.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
58 #include "statcommon.h"
60 static int zpool_do_create(int, char **);
61 static int zpool_do_destroy(int, char **);
63 static int zpool_do_add(int, char **);
64 static int zpool_do_remove(int, char **);
66 static int zpool_do_list(int, char **);
67 static int zpool_do_iostat(int, char **);
68 static int zpool_do_status(int, char **);
70 static int zpool_do_online(int, char **);
71 static int zpool_do_offline(int, char **);
72 static int zpool_do_clear(int, char **);
73 static int zpool_do_reopen(int, char **);
75 static int zpool_do_reguid(int, char **);
77 static int zpool_do_attach(int, char **);
78 static int zpool_do_detach(int, char **);
79 static int zpool_do_replace(int, char **);
80 static int zpool_do_split(int, char **);
82 static int zpool_do_scrub(int, char **);
84 static int zpool_do_import(int, char **);
85 static int zpool_do_export(int, char **);
87 static int zpool_do_upgrade(int, char **);
89 static int zpool_do_history(int, char **);
90 static int zpool_do_events(int, char **);
92 static int zpool_do_get(int, char **);
93 static int zpool_do_set(int, char **);
96 * These libumem hooks provide a reasonable set of defaults for the allocator's
97 * debugging facilities.
102 _umem_debug_init(void)
104 return ("default,verbose"); /* $UMEM_DEBUG setting */
108 _umem_logging_init(void)
110 return ("fail,contents"); /* $UMEM_LOGGING setting */
142 typedef struct zpool_command {
144 int (*func)(int, char **);
149 * Master command table. Each ZFS command has a name, associated function, and
150 * usage message. The usage messages need to be internationalized, so we have
151 * to have a function to return the usage message based on a command index.
153 * These commands are organized according to how they are displayed in the usage
154 * message. An empty command (one with a NULL name) indicates an empty line in
155 * the generic usage message.
157 static zpool_command_t command_table[] = {
158 { "create", zpool_do_create, HELP_CREATE },
159 { "destroy", zpool_do_destroy, HELP_DESTROY },
161 { "add", zpool_do_add, HELP_ADD },
162 { "remove", zpool_do_remove, HELP_REMOVE },
164 { "list", zpool_do_list, HELP_LIST },
165 { "iostat", zpool_do_iostat, HELP_IOSTAT },
166 { "status", zpool_do_status, HELP_STATUS },
168 { "online", zpool_do_online, HELP_ONLINE },
169 { "offline", zpool_do_offline, HELP_OFFLINE },
170 { "clear", zpool_do_clear, HELP_CLEAR },
171 { "reopen", zpool_do_reopen, HELP_REOPEN },
173 { "attach", zpool_do_attach, HELP_ATTACH },
174 { "detach", zpool_do_detach, HELP_DETACH },
175 { "replace", zpool_do_replace, HELP_REPLACE },
176 { "split", zpool_do_split, HELP_SPLIT },
178 { "scrub", zpool_do_scrub, HELP_SCRUB },
180 { "import", zpool_do_import, HELP_IMPORT },
181 { "export", zpool_do_export, HELP_EXPORT },
182 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
183 { "reguid", zpool_do_reguid, HELP_REGUID },
185 { "history", zpool_do_history, HELP_HISTORY },
186 { "events", zpool_do_events, HELP_EVENTS },
188 { "get", zpool_do_get, HELP_GET },
189 { "set", zpool_do_set, HELP_SET },
192 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
194 zpool_command_t *current_command;
195 static char history_str[HIS_MAX_RECORD_LEN];
197 static uint_t timestamp_fmt = NODATE;
200 get_usage(zpool_help_t idx) {
203 return (gettext("\tadd [-fn] [-o property=value] "
204 "<pool> <vdev> ...\n"));
206 return (gettext("\tattach [-f] [-o property=value] "
207 "<pool> <device> <new-device>\n"));
209 return (gettext("\tclear [-nF] <pool> [device]\n"));
211 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
212 "\t [-O file-system-property=value] ... \n"
213 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
215 return (gettext("\tdestroy [-f] <pool>\n"));
217 return (gettext("\tdetach <pool> <device>\n"));
219 return (gettext("\texport [-f] <pool> ...\n"));
221 return (gettext("\thistory [-il] [<pool>] ...\n"));
223 return (gettext("\timport [-d dir] [-D]\n"
224 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
225 "\timport [-o mntopts] [-o property=value] ... \n"
226 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
227 "[-R root] [-F [-n]] -a\n"
228 "\timport [-o mntopts] [-o property=value] ... \n"
229 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
230 "[-R root] [-F [-n]]\n"
231 "\t <pool | id> [newpool]\n"));
233 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
236 return (gettext("\tlist [-H] [-o property[,...]] "
237 "[-T d|u] [pool] ... [interval [count]]\n"));
239 return (gettext("\toffline [-t] <pool> <device> ...\n"));
241 return (gettext("\tonline <pool> <device> ...\n"));
243 return (gettext("\treplace [-f] <pool> <device> "
246 return (gettext("\tremove <pool> <device> ...\n"));
248 return (""); /* Undocumented command */
250 return (gettext("\tscrub [-s] <pool> ...\n"));
252 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
255 return (gettext("\tupgrade\n"
257 "\tupgrade [-V version] <-a | pool ...>\n"));
259 return (gettext("\tevents [-vHfc]\n"));
261 return (gettext("\tget <\"all\" | property[,...]> "
264 return (gettext("\tset <property=value> <pool> \n"));
266 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
267 "\t [-o property=value] <pool> <newpool> "
268 "[<device> ...]\n"));
270 return (gettext("\treguid <pool>\n"));
279 * Callback routine that will print out a pool property value.
282 print_prop_cb(int prop, void *cb)
286 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
288 if (zpool_prop_readonly(prop))
289 (void) fprintf(fp, " NO ");
291 (void) fprintf(fp, " YES ");
293 if (zpool_prop_values(prop) == NULL)
294 (void) fprintf(fp, "-\n");
296 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
302 * Display usage message. If we're inside a command, display only the usage for
303 * that command. Otherwise, iterate over the entire command table and display
304 * a complete usage message.
307 usage(boolean_t requested)
309 FILE *fp = requested ? stdout : stderr;
311 if (current_command == NULL) {
314 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
316 gettext("where 'command' is one of the following:\n\n"));
318 for (i = 0; i < NCOMMAND; i++) {
319 if (command_table[i].name == NULL)
320 (void) fprintf(fp, "\n");
322 (void) fprintf(fp, "%s",
323 get_usage(command_table[i].usage));
326 (void) fprintf(fp, gettext("usage:\n"));
327 (void) fprintf(fp, "%s", get_usage(current_command->usage));
330 if (current_command != NULL &&
331 ((strcmp(current_command->name, "set") == 0) ||
332 (strcmp(current_command->name, "get") == 0) ||
333 (strcmp(current_command->name, "list") == 0))) {
336 gettext("\nthe following properties are supported:\n"));
338 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
339 "PROPERTY", "EDIT", "VALUES");
341 /* Iterate over all properties */
342 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
347 * See comments at end of main().
349 if (getenv("ZFS_ABORT") != NULL) {
350 (void) printf("dumping core by request\n");
354 exit(requested ? 0 : 2);
358 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
359 boolean_t print_logs)
366 (void) printf("\t%*s%s\n", indent, "", name);
368 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
369 &child, &children) != 0)
372 for (c = 0; c < children; c++) {
373 uint64_t is_log = B_FALSE;
375 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
377 if ((is_log && !print_logs) || (!is_log && print_logs))
380 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
381 print_vdev_tree(zhp, vname, child[c], indent + 2,
388 * Add a property pair (name, string-value) into a property nvlist.
391 add_prop_list(const char *propname, char *propval, nvlist_t **props,
394 zpool_prop_t prop = ZPROP_INVAL;
400 if (*props == NULL &&
401 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
402 (void) fprintf(stderr,
403 gettext("internal error: out of memory\n"));
410 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
411 (void) fprintf(stderr, gettext("property '%s' is "
412 "not a valid pool property\n"), propname);
415 normnm = zpool_prop_to_name(prop);
417 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
418 normnm = zfs_prop_to_name(fprop);
424 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
425 prop != ZPOOL_PROP_CACHEFILE) {
426 (void) fprintf(stderr, gettext("property '%s' "
427 "specified multiple times\n"), propname);
431 if (nvlist_add_string(proplist, normnm, propval) != 0) {
432 (void) fprintf(stderr, gettext("internal "
433 "error: out of memory\n"));
441 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
443 * -f Force addition of devices, even if they appear in use
444 * -n Do not add the devices, but display the resulting layout if
445 * they were to be added.
446 * -o Set property=value.
448 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
449 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
453 zpool_do_add(int argc, char **argv)
455 boolean_t force = B_FALSE;
456 boolean_t dryrun = B_FALSE;
463 nvlist_t *props = NULL;
467 while ((c = getopt(argc, argv, "fno:")) != -1) {
476 if ((propval = strchr(optarg, '=')) == NULL) {
477 (void) fprintf(stderr, gettext("missing "
478 "'=' for -o option\n"));
484 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
485 (add_prop_list(optarg, propval, &props, B_TRUE)))
489 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
498 /* get pool name and check number of arguments */
500 (void) fprintf(stderr, gettext("missing pool name argument\n"));
504 (void) fprintf(stderr, gettext("missing vdev specification\n"));
513 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
516 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
517 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
523 /* pass off to get_vdev_spec for processing */
524 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
526 if (nvroot == NULL) {
532 nvlist_t *poolnvroot;
534 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
537 (void) printf(gettext("would update '%s' to the following "
538 "configuration:\n"), zpool_get_name(zhp));
540 /* print original main pool and new tree */
541 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
542 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
544 /* Do the same for the logs */
545 if (num_logs(poolnvroot) > 0) {
546 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
547 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
548 } else if (num_logs(nvroot) > 0) {
549 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
554 ret = (zpool_add(zhp, nvroot) != 0);
565 * zpool remove <pool> <vdev> ...
567 * Removes the given vdev from the pool. Currently, this supports removing
568 * spares, cache, and log devices from the pool.
571 zpool_do_remove(int argc, char **argv)
580 /* get pool name and check number of arguments */
582 (void) fprintf(stderr, gettext("missing pool name argument\n"));
586 (void) fprintf(stderr, gettext("missing device\n"));
592 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
595 for (i = 1; i < argc; i++) {
596 if (zpool_vdev_remove(zhp, argv[i]) != 0)
604 * zpool create [-fn] [-o property=value] ...
605 * [-O file-system-property=value] ...
606 * [-R root] [-m mountpoint] <pool> <dev> ...
608 * -f Force creation, even if devices appear in use
609 * -n Do not create the pool, but display the resulting layout if it
610 * were to be created.
611 * -R Create a pool under an alternate root
612 * -m Set default mountpoint for the root dataset. By default it's
614 * -o Set property=value.
615 * -O Set fsproperty=value in the pool's root file system
617 * Creates the named pool according to the given vdev specification. The
618 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
619 * we get the nvlist back from get_vdev_spec(), we either print out the contents
620 * (if '-n' was specified), or pass it to libzfs to do the creation.
623 zpool_do_create(int argc, char **argv)
625 boolean_t force = B_FALSE;
626 boolean_t dryrun = B_FALSE;
628 nvlist_t *nvroot = NULL;
631 char *altroot = NULL;
632 char *mountpoint = NULL;
633 nvlist_t *fsprops = NULL;
634 nvlist_t *props = NULL;
638 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
648 if (add_prop_list(zpool_prop_to_name(
649 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
651 if (nvlist_lookup_string(props,
652 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
655 if (add_prop_list(zpool_prop_to_name(
656 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
663 if ((propval = strchr(optarg, '=')) == NULL) {
664 (void) fprintf(stderr, gettext("missing "
665 "'=' for -o option\n"));
671 if (add_prop_list(optarg, propval, &props, B_TRUE))
675 if ((propval = strchr(optarg, '=')) == NULL) {
676 (void) fprintf(stderr, gettext("missing "
677 "'=' for -O option\n"));
683 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
687 (void) fprintf(stderr, gettext("missing argument for "
688 "'%c' option\n"), optopt);
691 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
700 /* get pool name and check number of arguments */
702 (void) fprintf(stderr, gettext("missing pool name argument\n"));
706 (void) fprintf(stderr, gettext("missing vdev specification\n"));
713 * As a special case, check for use of '/' in the name, and direct the
714 * user to use 'zfs create' instead.
716 if (strchr(poolname, '/') != NULL) {
717 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
718 "character '/' in pool name\n"), poolname);
719 (void) fprintf(stderr, gettext("use 'zfs create' to "
720 "create a dataset\n"));
724 /* pass off to get_vdev_spec for bulk processing */
725 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
730 /* make_root_vdev() allows 0 toplevel children if there are spares */
731 if (!zfs_allocatable_devs(nvroot)) {
732 (void) fprintf(stderr, gettext("invalid vdev "
733 "specification: at least one toplevel vdev must be "
739 if (altroot != NULL && altroot[0] != '/') {
740 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
741 "must be an absolute path\n"), altroot);
746 * Check the validity of the mountpoint and direct the user to use the
747 * '-m' mountpoint option if it looks like its in use.
749 if (mountpoint == NULL ||
750 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
751 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
752 char buf[MAXPATHLEN];
755 if (mountpoint && mountpoint[0] != '/') {
756 (void) fprintf(stderr, gettext("invalid mountpoint "
757 "'%s': must be an absolute path, 'legacy', or "
758 "'none'\n"), mountpoint);
762 if (mountpoint == NULL) {
764 (void) snprintf(buf, sizeof (buf), "%s/%s",
767 (void) snprintf(buf, sizeof (buf), "/%s",
771 (void) snprintf(buf, sizeof (buf), "%s%s",
772 altroot, mountpoint);
774 (void) snprintf(buf, sizeof (buf), "%s",
778 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
779 (void) fprintf(stderr, gettext("mountpoint '%s' : "
780 "%s\n"), buf, strerror(errno));
781 (void) fprintf(stderr, gettext("use '-m' "
782 "option to provide a different default\n"));
787 while (count < 3 && readdir(dirp) != NULL)
789 (void) closedir(dirp);
792 (void) fprintf(stderr, gettext("mountpoint "
793 "'%s' exists and is not empty\n"), buf);
794 (void) fprintf(stderr, gettext("use '-m' "
795 "option to provide a "
796 "different default\n"));
804 * For a dry run invocation, print out a basic message and run
805 * through all the vdevs in the list and print out in an
806 * appropriate hierarchy.
808 (void) printf(gettext("would create '%s' with the "
809 "following layout:\n\n"), poolname);
811 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
812 if (num_logs(nvroot) > 0)
813 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
818 * Hand off to libzfs.
820 if (zpool_create(g_zfs, poolname,
821 nvroot, props, fsprops) == 0) {
822 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
823 ZFS_TYPE_FILESYSTEM);
825 if (mountpoint != NULL)
826 verify(zfs_prop_set(pool,
828 ZFS_PROP_MOUNTPOINT),
830 if (zfs_mount(pool, NULL, 0) == 0)
831 ret = zfs_shareall(pool);
834 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
835 (void) fprintf(stderr, gettext("pool name may have "
842 nvlist_free(fsprops);
846 nvlist_free(fsprops);
853 * zpool destroy <pool>
855 * -f Forcefully unmount any datasets
857 * Destroy the given pool. Automatically unmounts any datasets in the pool.
860 zpool_do_destroy(int argc, char **argv)
862 boolean_t force = B_FALSE;
869 while ((c = getopt(argc, argv, "f")) != -1) {
875 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
884 /* check arguments */
886 (void) fprintf(stderr, gettext("missing pool argument\n"));
890 (void) fprintf(stderr, gettext("too many arguments\n"));
896 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
898 * As a special case, check for use of '/' in the name, and
899 * direct the user to use 'zfs destroy' instead.
901 if (strchr(pool, '/') != NULL)
902 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
903 "destroy a dataset\n"));
907 if (zpool_disable_datasets(zhp, force) != 0) {
908 (void) fprintf(stderr, gettext("could not destroy '%s': "
909 "could not unmount datasets\n"), zpool_get_name(zhp));
913 ret = (zpool_destroy(zhp) != 0);
921 * zpool export [-f] <pool> ...
923 * -f Forcefully unmount datasets
925 * Export the given pools. By default, the command will attempt to cleanly
926 * unmount any active datasets within the pool. If the '-f' flag is specified,
927 * then the datasets will be forcefully unmounted.
930 zpool_do_export(int argc, char **argv)
932 boolean_t force = B_FALSE;
933 boolean_t hardforce = B_FALSE;
940 while ((c = getopt(argc, argv, "fF")) != -1) {
949 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
958 /* check arguments */
960 (void) fprintf(stderr, gettext("missing pool argument\n"));
965 for (i = 0; i < argc; i++) {
966 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
971 if (zpool_disable_datasets(zhp, force) != 0) {
978 if (zpool_export_force(zhp) != 0)
980 } else if (zpool_export(zhp, force) != 0) {
991 * Given a vdev configuration, determine the maximum width needed for the device
995 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
997 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1002 if (strlen(name) + depth > max)
1003 max = strlen(name) + depth;
1007 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1008 &child, &children) == 0) {
1009 for (c = 0; c < children; c++)
1010 if ((ret = max_width(zhp, child[c], depth + 2,
1015 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1016 &child, &children) == 0) {
1017 for (c = 0; c < children; c++)
1018 if ((ret = max_width(zhp, child[c], depth + 2,
1023 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1024 &child, &children) == 0) {
1025 for (c = 0; c < children; c++)
1026 if ((ret = max_width(zhp, child[c], depth + 2,
1035 typedef struct spare_cbdata {
1037 zpool_handle_t *cb_zhp;
1041 find_vdev(nvlist_t *nv, uint64_t search)
1047 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1051 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1052 &child, &children) == 0) {
1053 for (c = 0; c < children; c++)
1054 if (find_vdev(child[c], search))
1062 find_spare(zpool_handle_t *zhp, void *data)
1064 spare_cbdata_t *cbp = data;
1065 nvlist_t *config, *nvroot;
1067 config = zpool_get_config(zhp, NULL);
1068 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1071 if (find_vdev(nvroot, cbp->cb_guid)) {
1081 * Print out configuration state as requested by status_callback.
1084 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1085 int namewidth, int depth, boolean_t isspare)
1089 pool_scan_stat_t *ps = NULL;
1091 char rbuf[6], wbuf[6], cbuf[6];
1093 uint64_t notpresent;
1097 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1098 &child, &children) != 0)
1101 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1102 (uint64_t **)&vs, &c) == 0);
1104 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1107 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1110 if (vs->vs_aux == VDEV_AUX_SPARED)
1112 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1116 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1120 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1121 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1122 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1123 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1126 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1127 ¬present) == 0) {
1129 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1130 (void) printf(" was %s", path);
1131 } else if (vs->vs_aux != 0) {
1134 switch (vs->vs_aux) {
1135 case VDEV_AUX_OPEN_FAILED:
1136 (void) printf(gettext("cannot open"));
1139 case VDEV_AUX_BAD_GUID_SUM:
1140 (void) printf(gettext("missing device"));
1143 case VDEV_AUX_NO_REPLICAS:
1144 (void) printf(gettext("insufficient replicas"));
1147 case VDEV_AUX_VERSION_NEWER:
1148 (void) printf(gettext("newer version"));
1151 case VDEV_AUX_SPARED:
1152 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1154 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1155 if (strcmp(zpool_get_name(cb.cb_zhp),
1156 zpool_get_name(zhp)) == 0)
1157 (void) printf(gettext("currently in "
1160 (void) printf(gettext("in use by "
1162 zpool_get_name(cb.cb_zhp));
1163 zpool_close(cb.cb_zhp);
1165 (void) printf(gettext("currently in use"));
1169 case VDEV_AUX_ERR_EXCEEDED:
1170 (void) printf(gettext("too many errors"));
1173 case VDEV_AUX_IO_FAILURE:
1174 (void) printf(gettext("experienced I/O failures"));
1177 case VDEV_AUX_BAD_LOG:
1178 (void) printf(gettext("bad intent log"));
1181 case VDEV_AUX_EXTERNAL:
1182 (void) printf(gettext("external device fault"));
1185 case VDEV_AUX_SPLIT_POOL:
1186 (void) printf(gettext("split into new pool"));
1190 (void) printf(gettext("corrupted data"));
1195 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1196 (uint64_t **)&ps, &c);
1198 if (ps && ps->pss_state == DSS_SCANNING &&
1199 vs->vs_scan_processed != 0 && children == 0) {
1200 (void) printf(gettext(" (%s)"),
1201 (ps->pss_func == POOL_SCAN_RESILVER) ?
1202 "resilvering" : "repairing");
1205 (void) printf("\n");
1207 for (c = 0; c < children; c++) {
1208 uint64_t islog = B_FALSE, ishole = B_FALSE;
1210 /* Don't print logs or holes here */
1211 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1213 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1215 if (islog || ishole)
1217 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1218 print_status_config(zhp, vname, child[c],
1219 namewidth, depth + 2, isspare);
1226 * Print the configuration of an exported pool. Iterate over all vdevs in the
1227 * pool, printing out the name and status for each one.
1230 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1237 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1238 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1239 strcmp(type, VDEV_TYPE_HOLE) == 0)
1242 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1243 (uint64_t **)&vs, &c) == 0);
1245 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1246 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1248 if (vs->vs_aux != 0) {
1251 switch (vs->vs_aux) {
1252 case VDEV_AUX_OPEN_FAILED:
1253 (void) printf(gettext("cannot open"));
1256 case VDEV_AUX_BAD_GUID_SUM:
1257 (void) printf(gettext("missing device"));
1260 case VDEV_AUX_NO_REPLICAS:
1261 (void) printf(gettext("insufficient replicas"));
1264 case VDEV_AUX_VERSION_NEWER:
1265 (void) printf(gettext("newer version"));
1268 case VDEV_AUX_ERR_EXCEEDED:
1269 (void) printf(gettext("too many errors"));
1273 (void) printf(gettext("corrupted data"));
1277 (void) printf("\n");
1279 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1280 &child, &children) != 0)
1283 for (c = 0; c < children; c++) {
1284 uint64_t is_log = B_FALSE;
1286 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1291 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1292 print_import_config(vname, child[c], namewidth, depth + 2);
1296 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1297 &child, &children) == 0) {
1298 (void) printf(gettext("\tcache\n"));
1299 for (c = 0; c < children; c++) {
1300 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1301 (void) printf("\t %s\n", vname);
1306 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1307 &child, &children) == 0) {
1308 (void) printf(gettext("\tspares\n"));
1309 for (c = 0; c < children; c++) {
1310 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1311 (void) printf("\t %s\n", vname);
1319 * Logs are recorded as top level vdevs in the main pool child array
1320 * but with "is_log" set to 1. We use either print_status_config() or
1321 * print_import_config() to print the top level logs then any log
1322 * children (eg mirrored slogs) are printed recursively - which
1323 * works because only the top level vdev is marked "is_log"
1326 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1331 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1335 (void) printf(gettext("\tlogs\n"));
1337 for (c = 0; c < children; c++) {
1338 uint64_t is_log = B_FALSE;
1341 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1345 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1347 print_status_config(zhp, name, child[c], namewidth,
1350 print_import_config(name, child[c], namewidth, 2);
1356 * Display the status for the given pool.
1359 show_import(nvlist_t *config)
1361 uint64_t pool_state;
1373 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1375 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1377 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1379 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1382 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1383 (uint64_t **)&vs, &vsc) == 0);
1384 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1386 reason = zpool_import_status(config, &msgid);
1388 (void) printf(gettext(" pool: %s\n"), name);
1389 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1390 (void) printf(gettext(" state: %s"), health);
1391 if (pool_state == POOL_STATE_DESTROYED)
1392 (void) printf(gettext(" (DESTROYED)"));
1393 (void) printf("\n");
1396 case ZPOOL_STATUS_MISSING_DEV_R:
1397 case ZPOOL_STATUS_MISSING_DEV_NR:
1398 case ZPOOL_STATUS_BAD_GUID_SUM:
1399 (void) printf(gettext(" status: One or more devices are "
1400 "missing from the system.\n"));
1403 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1404 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1405 (void) printf(gettext(" status: One or more devices contains "
1406 "corrupted data.\n"));
1409 case ZPOOL_STATUS_CORRUPT_DATA:
1411 gettext(" status: The pool data is corrupted.\n"));
1414 case ZPOOL_STATUS_OFFLINE_DEV:
1415 (void) printf(gettext(" status: One or more devices "
1416 "are offlined.\n"));
1419 case ZPOOL_STATUS_CORRUPT_POOL:
1420 (void) printf(gettext(" status: The pool metadata is "
1424 case ZPOOL_STATUS_VERSION_OLDER:
1425 (void) printf(gettext(" status: The pool is formatted using an "
1426 "older on-disk version.\n"));
1429 case ZPOOL_STATUS_VERSION_NEWER:
1430 (void) printf(gettext(" status: The pool is formatted using an "
1431 "incompatible version.\n"));
1434 case ZPOOL_STATUS_HOSTID_MISMATCH:
1435 (void) printf(gettext(" status: The pool was last accessed by "
1436 "another system.\n"));
1439 case ZPOOL_STATUS_FAULTED_DEV_R:
1440 case ZPOOL_STATUS_FAULTED_DEV_NR:
1441 (void) printf(gettext(" status: One or more devices are "
1445 case ZPOOL_STATUS_BAD_LOG:
1446 (void) printf(gettext(" status: An intent log record cannot be "
1450 case ZPOOL_STATUS_RESILVERING:
1451 (void) printf(gettext(" status: One or more devices were being "
1457 * No other status can be seen when importing pools.
1459 assert(reason == ZPOOL_STATUS_OK);
1463 * Print out an action according to the overall state of the pool.
1465 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1466 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1467 (void) printf(gettext(" action: The pool can be "
1468 "imported using its name or numeric identifier, "
1469 "though\n\tsome features will not be available "
1470 "without an explicit 'zpool upgrade'.\n"));
1471 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1472 (void) printf(gettext(" action: The pool can be "
1473 "imported using its name or numeric "
1474 "identifier and\n\tthe '-f' flag.\n"));
1476 (void) printf(gettext(" action: The pool can be "
1477 "imported using its name or numeric "
1479 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1480 (void) printf(gettext(" action: The pool can be imported "
1481 "despite missing or damaged devices. The\n\tfault "
1482 "tolerance of the pool may be compromised if imported.\n"));
1485 case ZPOOL_STATUS_VERSION_NEWER:
1486 (void) printf(gettext(" action: The pool cannot be "
1487 "imported. Access the pool on a system running "
1488 "newer\n\tsoftware, or recreate the pool from "
1491 case ZPOOL_STATUS_MISSING_DEV_R:
1492 case ZPOOL_STATUS_MISSING_DEV_NR:
1493 case ZPOOL_STATUS_BAD_GUID_SUM:
1494 (void) printf(gettext(" action: The pool cannot be "
1495 "imported. Attach the missing\n\tdevices and try "
1499 (void) printf(gettext(" action: The pool cannot be "
1500 "imported due to damaged devices or data.\n"));
1504 /* Print the comment attached to the pool. */
1505 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1506 (void) printf(gettext("comment: %s\n"), comment);
1509 * If the state is "closed" or "can't open", and the aux state
1510 * is "corrupt data":
1512 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1513 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1514 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1515 if (pool_state == POOL_STATE_DESTROYED)
1516 (void) printf(gettext("\tThe pool was destroyed, "
1517 "but can be imported using the '-Df' flags.\n"));
1518 else if (pool_state != POOL_STATE_EXPORTED)
1519 (void) printf(gettext("\tThe pool may be active on "
1520 "another system, but can be imported using\n\t"
1521 "the '-f' flag.\n"));
1525 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1528 (void) printf(gettext(" config:\n\n"));
1530 namewidth = max_width(NULL, nvroot, 0, 0);
1534 print_import_config(name, nvroot, namewidth, 0);
1535 if (num_logs(nvroot) > 0)
1536 print_logs(NULL, nvroot, namewidth, B_FALSE);
1538 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1539 (void) printf(gettext("\n\tAdditional devices are known to "
1540 "be part of this pool, though their\n\texact "
1541 "configuration cannot be determined.\n"));
1546 * Perform the import for the given configuration. This passes the heavy
1547 * lifting off to zpool_import_props(), and then mounts the datasets contained
1551 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1552 nvlist_t *props, int flags)
1554 zpool_handle_t *zhp;
1559 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1562 verify(nvlist_lookup_uint64(config,
1563 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1564 verify(nvlist_lookup_uint64(config,
1565 ZPOOL_CONFIG_VERSION, &version) == 0);
1566 if (version > SPA_VERSION) {
1567 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1568 "is formatted using a newer ZFS version\n"), name);
1570 } else if (state != POOL_STATE_EXPORTED &&
1571 !(flags & ZFS_IMPORT_ANY_HOST)) {
1574 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1576 unsigned long system_hostid = gethostid() & 0xffffffff;
1578 if ((unsigned long)hostid != system_hostid) {
1583 verify(nvlist_lookup_string(config,
1584 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1585 verify(nvlist_lookup_uint64(config,
1586 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1588 (void) fprintf(stderr, gettext("cannot import "
1589 "'%s': pool may be in use from other "
1590 "system, it was last accessed by %s "
1591 "(hostid: 0x%lx) on %s"), name, hostname,
1592 (unsigned long)hostid,
1593 asctime(localtime(&t)));
1594 (void) fprintf(stderr, gettext("use '-f' to "
1595 "import anyway\n"));
1599 (void) fprintf(stderr, gettext("cannot import '%s': "
1600 "pool may be in use from other system\n"), name);
1601 (void) fprintf(stderr, gettext("use '-f' to import "
1607 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1610 if (newname != NULL)
1611 name = (char *)newname;
1613 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1616 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1617 !(flags & ZFS_IMPORT_ONLY) &&
1618 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1628 * zpool import [-d dir] [-D]
1629 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1630 * [-d dir | -c cachefile] [-f] -a
1631 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1632 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1634 * -c Read pool information from a cachefile instead of searching
1637 * -d Scan in a specific directory, other than /dev/. More than
1638 * one directory can be specified using multiple '-d' options.
1640 * -D Scan for previously destroyed pools or import all or only
1641 * specified destroyed pools.
1643 * -R Temporarily import the pool, with all mountpoints relative to
1644 * the given root. The pool will remain exported when the machine
1647 * -V Import even in the presence of faulted vdevs. This is an
1648 * intentionally undocumented option for testing purposes, and
1649 * treats the pool configuration as complete, leaving any bad
1650 * vdevs in the FAULTED state. In other words, it does verbatim
1653 * -f Force import, even if it appears that the pool is active.
1655 * -F Attempt rewind if necessary.
1657 * -n See if rewind would work, but don't actually rewind.
1659 * -N Import the pool but don't mount datasets.
1661 * -T Specify a starting txg to use for import. This option is
1662 * intentionally undocumented option for testing purposes.
1664 * -a Import all pools found.
1666 * -o Set property=value and/or temporary mount options (without '=').
1668 * The import command scans for pools to import, and import pools based on pool
1669 * name and GUID. The pool can also be renamed as part of the import process.
1672 zpool_do_import(int argc, char **argv)
1674 char **searchdirs = NULL;
1675 char *env, *envdup = NULL;
1679 nvlist_t *pools = NULL;
1680 boolean_t do_all = B_FALSE;
1681 boolean_t do_destroyed = B_FALSE;
1682 char *mntopts = NULL;
1685 uint64_t searchguid = 0;
1686 char *searchname = NULL;
1688 nvlist_t *found_config;
1689 nvlist_t *policy = NULL;
1690 nvlist_t *props = NULL;
1692 int flags = ZFS_IMPORT_NORMAL;
1693 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1694 boolean_t dryrun = B_FALSE;
1695 boolean_t do_rewind = B_FALSE;
1696 boolean_t xtreme_rewind = B_FALSE;
1697 uint64_t pool_state, txg = -1ULL;
1698 char *cachefile = NULL;
1699 importargs_t idata = { 0 };
1703 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1712 if (searchdirs == NULL) {
1713 searchdirs = safe_malloc(sizeof (char *));
1715 char **tmp = safe_malloc((nsearch + 1) *
1717 bcopy(searchdirs, tmp, nsearch *
1722 searchdirs[nsearch++] = optarg;
1725 do_destroyed = B_TRUE;
1728 flags |= ZFS_IMPORT_ANY_HOST;
1734 flags |= ZFS_IMPORT_MISSING_LOG;
1740 flags |= ZFS_IMPORT_ONLY;
1743 if ((propval = strchr(optarg, '=')) != NULL) {
1746 if (add_prop_list(optarg, propval,
1754 if (add_prop_list(zpool_prop_to_name(
1755 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1757 if (nvlist_lookup_string(props,
1758 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1761 if (add_prop_list(zpool_prop_to_name(
1762 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1767 txg = strtoull(optarg, &endptr, 10);
1768 if (errno != 0 || *endptr != '\0') {
1769 (void) fprintf(stderr,
1770 gettext("invalid txg value\n"));
1773 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1776 flags |= ZFS_IMPORT_VERBATIM;
1779 xtreme_rewind = B_TRUE;
1782 (void) fprintf(stderr, gettext("missing argument for "
1783 "'%c' option\n"), optopt);
1787 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1796 if (cachefile && nsearch != 0) {
1797 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1801 if ((dryrun || xtreme_rewind) && !do_rewind) {
1802 (void) fprintf(stderr,
1803 gettext("-n or -X only meaningful with -F\n"));
1807 rewind_policy = ZPOOL_TRY_REWIND;
1809 rewind_policy = ZPOOL_DO_REWIND;
1811 rewind_policy |= ZPOOL_EXTREME_REWIND;
1813 /* In the future, we can capture further policy and include it here */
1814 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1815 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1816 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1819 /* check argument count */
1822 (void) fprintf(stderr, gettext("too many arguments\n"));
1827 (void) fprintf(stderr, gettext("too many arguments\n"));
1832 * Check for the SYS_CONFIG privilege. We do this explicitly
1833 * here because otherwise any attempt to discover pools will
1836 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1837 (void) fprintf(stderr, gettext("cannot "
1838 "discover pools: permission denied\n"));
1839 if (searchdirs != NULL)
1842 nvlist_free(policy);
1848 * Depending on the arguments given, we do one of the following:
1850 * <none> Iterate through all pools and display information about
1853 * -a Iterate through all pools and try to import each one.
1855 * <id> Find the pool that corresponds to the given GUID/pool
1856 * name and import that one.
1858 * -D Above options applies only to destroyed pools.
1864 searchguid = strtoull(argv[0], &endptr, 10);
1865 if (errno != 0 || *endptr != '\0')
1866 searchname = argv[0];
1867 found_config = NULL;
1870 * User specified a name or guid. Ensure it's unique.
1872 idata.unique = B_TRUE;
1876 * Check the environment for the preferred search path.
1878 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
1881 envdup = strdup(env);
1883 dir = strtok(envdup, ":");
1884 while (dir != NULL) {
1885 if (searchdirs == NULL) {
1886 searchdirs = safe_malloc(sizeof (char *));
1888 char **tmp = safe_malloc((nsearch + 1) *
1890 bcopy(searchdirs, tmp, nsearch *
1895 searchdirs[nsearch++] = dir;
1896 dir = strtok(NULL, ":");
1900 idata.path = searchdirs;
1901 idata.paths = nsearch;
1902 idata.poolname = searchname;
1903 idata.guid = searchguid;
1904 idata.cachefile = cachefile;
1906 pools = zpool_search_import(g_zfs, &idata);
1908 if (pools != NULL && idata.exists &&
1909 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1910 (void) fprintf(stderr, gettext("cannot import '%s': "
1911 "a pool with that name already exists\n"),
1913 (void) fprintf(stderr, gettext("use the form '%s "
1914 "<pool | id> <newpool>' to give it a new name\n"),
1917 } else if (pools == NULL && idata.exists) {
1918 (void) fprintf(stderr, gettext("cannot import '%s': "
1919 "a pool with that name is already created/imported,\n"),
1921 (void) fprintf(stderr, gettext("and no additional pools "
1922 "with that name were found\n"));
1924 } else if (pools == NULL) {
1926 (void) fprintf(stderr, gettext("cannot import '%s': "
1927 "no such pool available\n"), argv[0]);
1933 if (searchdirs != NULL)
1937 nvlist_free(policy);
1942 * At this point we have a list of import candidate configs. Even if
1943 * we were searching by pool name or guid, we still need to
1944 * post-process the list to deal with pool state and possible
1950 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1952 verify(nvpair_value_nvlist(elem, &config) == 0);
1954 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1956 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1958 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1961 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1968 (void) printf("\n");
1971 err |= do_import(config, NULL, mntopts,
1974 show_import(config);
1976 } else if (searchname != NULL) {
1980 * We are searching for a pool based on name.
1982 verify(nvlist_lookup_string(config,
1983 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1985 if (strcmp(name, searchname) == 0) {
1986 if (found_config != NULL) {
1987 (void) fprintf(stderr, gettext(
1988 "cannot import '%s': more than "
1989 "one matching pool\n"), searchname);
1990 (void) fprintf(stderr, gettext(
1991 "import by numeric ID instead\n"));
1994 found_config = config;
2000 * Search for a pool by guid.
2002 verify(nvlist_lookup_uint64(config,
2003 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2005 if (guid == searchguid)
2006 found_config = config;
2011 * If we were searching for a specific pool, verify that we found a
2012 * pool, and then do the import.
2014 if (argc != 0 && err == 0) {
2015 if (found_config == NULL) {
2016 (void) fprintf(stderr, gettext("cannot import '%s': "
2017 "no such pool available\n"), argv[0]);
2020 err |= do_import(found_config, argc == 1 ? NULL :
2021 argv[1], mntopts, props, flags);
2026 * If we were just looking for pools, report an error if none were
2029 if (argc == 0 && first)
2030 (void) fprintf(stderr,
2031 gettext("no pools available to import\n"));
2036 nvlist_free(policy);
2037 if (searchdirs != NULL)
2042 return (err ? 1 : 0);
2045 typedef struct iostat_cbdata {
2046 boolean_t cb_verbose;
2049 zpool_list_t *cb_list;
2053 print_iostat_separator(iostat_cbdata_t *cb)
2057 for (i = 0; i < cb->cb_namewidth; i++)
2059 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2063 print_iostat_header(iostat_cbdata_t *cb)
2065 (void) printf("%*s capacity operations bandwidth\n",
2066 cb->cb_namewidth, "");
2067 (void) printf("%-*s alloc free read write read write\n",
2068 cb->cb_namewidth, "pool");
2069 print_iostat_separator(cb);
2073 * Display a single statistic.
2076 print_one_stat(uint64_t value)
2080 zfs_nicenum(value, buf, sizeof (buf));
2081 (void) printf(" %5s", buf);
2085 * Print out all the statistics for the given vdev. This can either be the
2086 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2087 * is a verbose output, and we don't want to display the toplevel pool stats.
2090 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2091 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2093 nvlist_t **oldchild, **newchild;
2095 vdev_stat_t *oldvs, *newvs;
2096 vdev_stat_t zerovs = { 0 };
2101 if (oldnv != NULL) {
2102 verify(nvlist_lookup_uint64_array(oldnv,
2103 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2108 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2109 (uint64_t **)&newvs, &c) == 0);
2111 if (strlen(name) + depth > cb->cb_namewidth)
2112 (void) printf("%*s%s", depth, "", name);
2114 (void) printf("%*s%s%*s", depth, "", name,
2115 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2117 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2122 scale = (double)NANOSEC / tdelta;
2124 /* only toplevel vdevs have capacity stats */
2125 if (newvs->vs_space == 0) {
2126 (void) printf(" - -");
2128 print_one_stat(newvs->vs_alloc);
2129 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2132 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2133 oldvs->vs_ops[ZIO_TYPE_READ])));
2135 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2136 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2138 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2139 oldvs->vs_bytes[ZIO_TYPE_READ])));
2141 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2142 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2144 (void) printf("\n");
2146 if (!cb->cb_verbose)
2149 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2150 &newchild, &children) != 0)
2153 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2154 &oldchild, &c) != 0)
2157 for (c = 0; c < children; c++) {
2158 uint64_t ishole = B_FALSE, islog = B_FALSE;
2160 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2163 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2166 if (ishole || islog)
2169 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2170 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2171 newchild[c], cb, depth + 2);
2176 * Log device section
2179 if (num_logs(newnv) > 0) {
2180 (void) printf("%-*s - - - - - "
2181 "-\n", cb->cb_namewidth, "logs");
2183 for (c = 0; c < children; c++) {
2184 uint64_t islog = B_FALSE;
2185 (void) nvlist_lookup_uint64(newchild[c],
2186 ZPOOL_CONFIG_IS_LOG, &islog);
2189 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2191 print_vdev_stats(zhp, vname, oldnv ?
2192 oldchild[c] : NULL, newchild[c],
2201 * Include level 2 ARC devices in iostat output
2203 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2204 &newchild, &children) != 0)
2207 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2208 &oldchild, &c) != 0)
2212 (void) printf("%-*s - - - - - "
2213 "-\n", cb->cb_namewidth, "cache");
2214 for (c = 0; c < children; c++) {
2215 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2217 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2218 newchild[c], cb, depth + 2);
2225 refresh_iostat(zpool_handle_t *zhp, void *data)
2227 iostat_cbdata_t *cb = data;
2231 * If the pool has disappeared, remove it from the list and continue.
2233 if (zpool_refresh_stats(zhp, &missing) != 0)
2237 pool_list_remove(cb->cb_list, zhp);
2243 * Callback to print out the iostats for the given pool.
2246 print_iostat(zpool_handle_t *zhp, void *data)
2248 iostat_cbdata_t *cb = data;
2249 nvlist_t *oldconfig, *newconfig;
2250 nvlist_t *oldnvroot, *newnvroot;
2252 newconfig = zpool_get_config(zhp, &oldconfig);
2254 if (cb->cb_iteration == 1)
2257 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2260 if (oldconfig == NULL)
2263 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2267 * Print out the statistics for the pool.
2269 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2272 print_iostat_separator(cb);
2284 if (isatty(STDOUT_FILENO)) {
2285 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2287 columns = ws.ws_col;
2296 get_namewidth(zpool_handle_t *zhp, void *data)
2298 iostat_cbdata_t *cb = data;
2299 nvlist_t *config, *nvroot;
2302 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2303 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2305 if (!cb->cb_verbose)
2306 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2308 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2313 * The width must be at least 10, but may be as large as the
2314 * column width - 42 so that we can still fit in one line.
2316 columns = get_columns();
2318 if (cb->cb_namewidth < 10)
2319 cb->cb_namewidth = 10;
2320 if (cb->cb_namewidth > columns - 42)
2321 cb->cb_namewidth = columns - 42;
2327 * Parse the input string, get the 'interval' and 'count' value if there is one.
2330 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2333 unsigned long interval = 0, count = 0;
2337 * Determine if the last argument is an integer or a pool name
2339 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2343 interval = strtoul(argv[argc - 1], &end, 10);
2345 if (*end == '\0' && errno == 0) {
2346 if (interval == 0) {
2347 (void) fprintf(stderr, gettext("interval "
2348 "cannot be zero\n"));
2352 * Ignore the last parameter
2357 * If this is not a valid number, just plow on. The
2358 * user will get a more informative error message later
2366 * If the last argument is also an integer, then we have both a count
2369 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2374 interval = strtoul(argv[argc - 1], &end, 10);
2376 if (*end == '\0' && errno == 0) {
2377 if (interval == 0) {
2378 (void) fprintf(stderr, gettext("interval "
2379 "cannot be zero\n"));
2384 * Ignore the last parameter
2398 get_timestamp_arg(char c)
2401 timestamp_fmt = UDATE;
2403 timestamp_fmt = DDATE;
2409 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2411 * -v Display statistics for individual vdevs
2412 * -T Display a timestamp in date(1) or Unix format
2414 * This command can be tricky because we want to be able to deal with pool
2415 * creation/destruction as well as vdev configuration changes. The bulk of this
2416 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2417 * on pool_list_update() to detect the addition of new pools. Configuration
2418 * changes are all handled within libzfs.
2421 zpool_do_iostat(int argc, char **argv)
2426 unsigned long interval = 0, count = 0;
2428 boolean_t verbose = B_FALSE;
2432 while ((c = getopt(argc, argv, "T:v")) != -1) {
2435 get_timestamp_arg(*optarg);
2441 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2450 get_interval_count(&argc, argv, &interval, &count);
2453 * Construct the list of all interesting pools.
2456 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2459 if (pool_list_count(list) == 0 && argc != 0) {
2460 pool_list_free(list);
2464 if (pool_list_count(list) == 0 && interval == 0) {
2465 pool_list_free(list);
2466 (void) fprintf(stderr, gettext("no pools available\n"));
2471 * Enter the main iostat loop.
2474 cb.cb_verbose = verbose;
2475 cb.cb_iteration = 0;
2476 cb.cb_namewidth = 0;
2479 pool_list_update(list);
2481 if ((npools = pool_list_count(list)) == 0)
2482 (void) fprintf(stderr, gettext("no pools available\n"));
2485 * Refresh all statistics. This is done as an
2486 * explicit step before calculating the maximum name
2487 * width, so that any * configuration changes are
2488 * properly accounted for.
2490 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2494 * Iterate over all pools to determine the maximum width
2495 * for the pool / device name column across all pools.
2497 cb.cb_namewidth = 0;
2498 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2501 if (timestamp_fmt != NODATE)
2502 print_timestamp(timestamp_fmt);
2505 * If it's the first time, or verbose mode, print the
2508 if (++cb.cb_iteration == 1 || verbose)
2509 print_iostat_header(&cb);
2511 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2514 * If there's more than one pool, and we're not in
2515 * verbose mode (which prints a separator for us),
2516 * then print a separator.
2518 if (npools > 1 && !verbose)
2519 print_iostat_separator(&cb);
2522 (void) printf("\n");
2526 * Flush the output so that redirection to a file isn't buffered
2529 (void) fflush(stdout);
2534 if (count != 0 && --count == 0)
2537 (void) sleep(interval);
2540 pool_list_free(list);
2545 typedef struct list_cbdata {
2546 boolean_t cb_verbose;
2548 boolean_t cb_scripted;
2549 zprop_list_t *cb_proplist;
2553 * Given a list of columns to display, output appropriate headers for each one.
2556 print_header(list_cbdata_t *cb)
2558 zprop_list_t *pl = cb->cb_proplist;
2560 boolean_t first = B_TRUE;
2561 boolean_t right_justify;
2564 for (; pl != NULL; pl = pl->pl_next) {
2565 if (pl->pl_prop == ZPROP_INVAL)
2568 width = pl->pl_width;
2569 if (first && cb->cb_verbose) {
2571 * Reset the width to accommodate the verbose listing
2574 width = cb->cb_namewidth;
2582 header = zpool_prop_column_name(pl->pl_prop);
2583 right_justify = zpool_prop_align_right(pl->pl_prop);
2585 if (pl->pl_next == NULL && !right_justify)
2586 (void) printf("%s", header);
2587 else if (right_justify)
2588 (void) printf("%*s", (int)width, header);
2590 (void) printf("%-*s", (int)width, header);
2593 (void) printf("\n");
2597 * Given a pool and a list of properties, print out all the properties according
2598 * to the described layout.
2601 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2603 zprop_list_t *pl = cb->cb_proplist;
2604 boolean_t first = B_TRUE;
2605 char property[ZPOOL_MAXPROPLEN];
2607 boolean_t right_justify;
2610 for (; pl != NULL; pl = pl->pl_next) {
2612 width = pl->pl_width;
2613 if (first && cb->cb_verbose) {
2615 * Reset the width to accommodate the verbose listing
2618 width = cb->cb_namewidth;
2622 if (cb->cb_scripted)
2623 (void) printf("\t");
2630 right_justify = B_FALSE;
2631 if (pl->pl_prop != ZPROP_INVAL) {
2632 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2633 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2635 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2636 sizeof (property), NULL) != 0)
2641 right_justify = zpool_prop_align_right(pl->pl_prop);
2648 * If this is being called in scripted mode, or if this is the
2649 * last column and it is left-justified, don't include a width
2652 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2653 (void) printf("%s", propstr);
2654 else if (right_justify)
2655 (void) printf("%*s", (int)width, propstr);
2657 (void) printf("%-*s", (int)width, propstr);
2660 (void) printf("\n");
2664 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2668 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2670 zfs_nicenum(value, propval, sizeof (propval));
2672 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2673 (void) strlcpy(propval, "-", sizeof (propval));
2676 (void) printf("\t%s", propval);
2678 (void) printf(" %*s", (int)width, propval);
2682 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2683 list_cbdata_t *cb, int depth)
2689 boolean_t scripted = cb->cb_scripted;
2691 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2692 (uint64_t **)&vs, &c) == 0);
2696 (void) printf("\t%s", name);
2697 else if (strlen(name) + depth > cb->cb_namewidth)
2698 (void) printf("%*s%s", depth, "", name);
2700 (void) printf("%*s%s%*s", depth, "", name,
2701 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2703 /* only toplevel vdevs have capacity stats */
2704 if (vs->vs_space == 0) {
2706 (void) printf("\t-\t-\t-");
2708 (void) printf(" - - -");
2710 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2712 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2714 print_one_column(ZPOOL_PROP_FREE,
2715 vs->vs_space - vs->vs_alloc, scripted);
2717 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2719 (void) printf("\n");
2722 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2723 &child, &children) != 0)
2726 for (c = 0; c < children; c++) {
2727 uint64_t ishole = B_FALSE;
2729 if (nvlist_lookup_uint64(child[c],
2730 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2733 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2734 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2739 * Include level 2 ARC devices in iostat output
2741 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2742 &child, &children) != 0)
2746 (void) printf("%-*s - - - - - "
2747 "-\n", cb->cb_namewidth, "cache");
2748 for (c = 0; c < children; c++) {
2749 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2751 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2759 * Generic callback function to list a pool.
2762 list_callback(zpool_handle_t *zhp, void *data)
2764 list_cbdata_t *cbp = data;
2768 config = zpool_get_config(zhp, NULL);
2770 print_pool(zhp, cbp);
2771 if (!cbp->cb_verbose)
2774 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2776 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2782 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2784 * -H Scripted mode. Don't display headers, and separate properties
2786 * -o List of properties to display. Defaults to
2787 * "name,size,allocated,free,capacity,health,altroot"
2788 * -T Display a timestamp in date(1) or Unix format
2790 * List all pools in the system, whether or not they're healthy. Output space
2791 * statistics for each one, as well as health status summary.
2794 zpool_do_list(int argc, char **argv)
2798 list_cbdata_t cb = { 0 };
2799 static char default_props[] =
2800 "name,size,allocated,free,capacity,dedupratio,"
2802 char *props = default_props;
2803 unsigned long interval = 0, count = 0;
2805 boolean_t first = B_TRUE;
2808 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2811 cb.cb_scripted = B_TRUE;
2817 get_timestamp_arg(*optarg);
2820 cb.cb_verbose = B_TRUE;
2823 (void) fprintf(stderr, gettext("missing argument for "
2824 "'%c' option\n"), optopt);
2828 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2837 get_interval_count(&argc, argv, &interval, &count);
2839 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2842 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
2845 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2846 (void) printf(gettext("no pools available\n"));
2847 zprop_free_list(cb.cb_proplist);
2852 pool_list_update(list);
2854 if (pool_list_count(list) == 0)
2857 if (timestamp_fmt != NODATE)
2858 print_timestamp(timestamp_fmt);
2860 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
2864 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
2869 if (count != 0 && --count == 0)
2872 (void) sleep(interval);
2875 zprop_free_list(cb.cb_proplist);
2880 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2882 boolean_t force = B_FALSE;
2885 char *poolname, *old_disk, *new_disk;
2886 zpool_handle_t *zhp;
2887 nvlist_t *props = NULL;
2892 while ((c = getopt(argc, argv, "f")) != -1) {
2898 if ((propval = strchr(optarg, '=')) == NULL) {
2899 (void) fprintf(stderr, gettext("missing "
2900 "'=' for -o option\n"));
2906 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
2907 (add_prop_list(optarg, propval, &props, B_TRUE)))
2911 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2920 /* get pool name and check number of arguments */
2922 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2929 (void) fprintf(stderr,
2930 gettext("missing <device> specification\n"));
2938 (void) fprintf(stderr,
2939 gettext("missing <new_device> specification\n"));
2942 new_disk = old_disk;
2952 (void) fprintf(stderr, gettext("too many arguments\n"));
2956 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2959 if (zpool_get_config(zhp, NULL) == NULL) {
2960 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2966 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
2968 if (nvroot == NULL) {
2973 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2975 nvlist_free(nvroot);
2982 * zpool replace [-f] <pool> <device> <new_device>
2984 * -f Force attach, even if <new_device> appears to be in use.
2986 * Replace <device> with <new_device>.
2990 zpool_do_replace(int argc, char **argv)
2992 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2996 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
2998 * -f Force attach, even if <new_device> appears to be in use.
2999 * -o Set property=value.
3001 * Attach <new_device> to the mirror containing <device>. If <device> is not
3002 * part of a mirror, then <device> will be transformed into a mirror of
3003 * <device> and <new_device>. In either case, <new_device> will begin life
3004 * with a DTL of [0, now], and will immediately begin to resilver itself.
3007 zpool_do_attach(int argc, char **argv)
3009 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3013 * zpool detach [-f] <pool> <device>
3015 * -f Force detach of <device>, even if DTLs argue against it
3016 * (not supported yet)
3018 * Detach a device from a mirror. The operation will be refused if <device>
3019 * is the last device in the mirror, or if the DTLs indicate that this device
3020 * has the only valid copy of some data.
3024 zpool_do_detach(int argc, char **argv)
3027 char *poolname, *path;
3028 zpool_handle_t *zhp;
3032 while ((c = getopt(argc, argv, "f")) != -1) {
3036 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3045 /* get pool name and check number of arguments */
3047 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3052 (void) fprintf(stderr,
3053 gettext("missing <device> specification\n"));
3060 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3063 ret = zpool_vdev_detach(zhp, path);
3071 * zpool split [-n] [-o prop=val] ...
3073 * [-R altroot] <pool> <newpool> [<device> ...]
3075 * -n Do not split the pool, but display the resulting layout if
3076 * it were to be split.
3077 * -o Set property=value, or set mount options.
3078 * -R Mount the split-off pool under an alternate root.
3080 * Splits the named pool and gives it the new pool name. Devices to be split
3081 * off may be listed, provided that no more than one device is specified
3082 * per top-level vdev mirror. The newly split pool is left in an exported
3083 * state unless -R is specified.
3085 * Restrictions: the top-level of the pool pool must only be made up of
3086 * mirrors; all devices in the pool must be healthy; no device may be
3087 * undergoing a resilvering operation.
3090 zpool_do_split(int argc, char **argv)
3092 char *srcpool, *newpool, *propval;
3093 char *mntopts = NULL;
3096 zpool_handle_t *zhp;
3097 nvlist_t *config, *props = NULL;
3099 flags.dryrun = B_FALSE;
3100 flags.import = B_FALSE;
3103 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3106 flags.import = B_TRUE;
3108 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3109 &props, B_TRUE) != 0) {
3116 flags.dryrun = B_TRUE;
3119 if ((propval = strchr(optarg, '=')) != NULL) {
3122 if (add_prop_list(optarg, propval,
3123 &props, B_TRUE) != 0) {
3133 (void) fprintf(stderr, gettext("missing argument for "
3134 "'%c' option\n"), optopt);
3138 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3145 if (!flags.import && mntopts != NULL) {
3146 (void) fprintf(stderr, gettext("setting mntopts is only "
3147 "valid when importing the pool\n"));
3155 (void) fprintf(stderr, gettext("Missing pool name\n"));
3159 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3169 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3172 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3173 if (config == NULL) {
3177 (void) printf(gettext("would create '%s' with the "
3178 "following layout:\n\n"), newpool);
3179 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3181 nvlist_free(config);
3186 if (ret != 0 || flags.dryrun || !flags.import)
3190 * The split was successful. Now we need to open the new
3191 * pool and import it.
3193 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3195 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3196 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3198 (void) fprintf(stderr, gettext("Split was successful, but "
3199 "the datasets could not all be mounted\n"));
3200 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3201 "different altroot\n"), "zpool import");
3211 * zpool online <pool> <device> ...
3214 zpool_do_online(int argc, char **argv)
3218 zpool_handle_t *zhp;
3220 vdev_state_t newstate;
3224 while ((c = getopt(argc, argv, "et")) != -1) {
3227 flags |= ZFS_ONLINE_EXPAND;
3231 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3240 /* get pool name and check number of arguments */
3242 (void) fprintf(stderr, gettext("missing pool name\n"));
3246 (void) fprintf(stderr, gettext("missing device name\n"));
3252 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3255 for (i = 1; i < argc; i++) {
3256 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3257 if (newstate != VDEV_STATE_HEALTHY) {
3258 (void) printf(gettext("warning: device '%s' "
3259 "onlined, but remains in faulted state\n"),
3261 if (newstate == VDEV_STATE_FAULTED)
3262 (void) printf(gettext("use 'zpool "
3263 "clear' to restore a faulted "
3266 (void) printf(gettext("use 'zpool "
3267 "replace' to replace devices "
3268 "that are no longer present\n"));
3281 * zpool offline [-ft] <pool> <device> ...
3283 * -f Force the device into the offline state, even if doing
3284 * so would appear to compromise pool availability.
3285 * (not supported yet)
3287 * -t Only take the device off-line temporarily. The offline
3288 * state will not be persistent across reboots.
3292 zpool_do_offline(int argc, char **argv)
3296 zpool_handle_t *zhp;
3298 boolean_t istmp = B_FALSE;
3301 while ((c = getopt(argc, argv, "ft")) != -1) {
3308 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3317 /* get pool name and check number of arguments */
3319 (void) fprintf(stderr, gettext("missing pool name\n"));
3323 (void) fprintf(stderr, gettext("missing device name\n"));
3329 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3332 for (i = 1; i < argc; i++) {
3333 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3343 * zpool clear <pool> [device]
3345 * Clear all errors associated with a pool or a particular device.
3348 zpool_do_clear(int argc, char **argv)
3352 boolean_t dryrun = B_FALSE;
3353 boolean_t do_rewind = B_FALSE;
3354 boolean_t xtreme_rewind = B_FALSE;
3355 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3356 nvlist_t *policy = NULL;
3357 zpool_handle_t *zhp;
3358 char *pool, *device;
3361 while ((c = getopt(argc, argv, "FnX")) != -1) {
3370 xtreme_rewind = B_TRUE;
3373 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3383 (void) fprintf(stderr, gettext("missing pool name\n"));
3388 (void) fprintf(stderr, gettext("too many arguments\n"));
3392 if ((dryrun || xtreme_rewind) && !do_rewind) {
3393 (void) fprintf(stderr,
3394 gettext("-n or -X only meaningful with -F\n"));
3398 rewind_policy = ZPOOL_TRY_REWIND;
3400 rewind_policy = ZPOOL_DO_REWIND;
3402 rewind_policy |= ZPOOL_EXTREME_REWIND;
3404 /* In future, further rewind policy choices can be passed along here */
3405 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3406 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3410 device = argc == 2 ? argv[1] : NULL;
3412 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3413 nvlist_free(policy);
3417 if (zpool_clear(zhp, device, policy) != 0)
3422 nvlist_free(policy);
3428 * zpool reguid <pool>
3431 zpool_do_reguid(int argc, char **argv)
3435 zpool_handle_t *zhp;
3439 while ((c = getopt(argc, argv, "")) != -1) {
3442 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3451 /* get pool name and check number of arguments */
3453 (void) fprintf(stderr, gettext("missing pool name\n"));
3458 (void) fprintf(stderr, gettext("too many arguments\n"));
3463 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3466 ret = zpool_reguid(zhp);
3474 * zpool reopen <pool>
3476 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3478 * NOTE: This command is currently undocumented. If the command is ever
3479 * exposed then the appropriate usage() messages will need to be made.
3482 zpool_do_reopen(int argc, char **argv)
3485 zpool_handle_t *zhp;
3495 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3498 ret = zpool_reopen(zhp);
3503 typedef struct scrub_cbdata {
3510 scrub_callback(zpool_handle_t *zhp, void *data)
3512 scrub_cbdata_t *cb = data;
3516 * Ignore faulted pools.
3518 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3519 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3520 "currently unavailable\n"), zpool_get_name(zhp));
3524 err = zpool_scan(zhp, cb->cb_type);
3530 * zpool scrub [-s] <pool> ...
3532 * -s Stop. Stops any in-progress scrub.
3535 zpool_do_scrub(int argc, char **argv)
3540 cb.cb_type = POOL_SCAN_SCRUB;
3543 while ((c = getopt(argc, argv, "s")) != -1) {
3546 cb.cb_type = POOL_SCAN_NONE;
3549 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3561 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3565 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3568 typedef struct status_cbdata {
3570 boolean_t cb_allpools;
3571 boolean_t cb_verbose;
3572 boolean_t cb_explain;
3574 boolean_t cb_dedup_stats;
3578 * Print out detailed scrub status.
3581 print_scan_status(pool_scan_stat_t *ps)
3584 uint64_t elapsed, mins_left, hours_left;
3585 uint64_t pass_exam, examined, total;
3587 double fraction_done;
3588 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3590 (void) printf(gettext(" scan: "));
3592 /* If there's never been a scan, there's not much to say. */
3593 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3594 ps->pss_func >= POOL_SCAN_FUNCS) {
3595 (void) printf(gettext("none requested\n"));
3599 start = ps->pss_start_time;
3600 end = ps->pss_end_time;
3601 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3603 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3604 ps->pss_func == POOL_SCAN_RESILVER);
3606 * Scan is finished or canceled.
3608 if (ps->pss_state == DSS_FINISHED) {
3609 uint64_t minutes_taken = (end - start) / 60;
3612 if (ps->pss_func == POOL_SCAN_SCRUB) {
3613 fmt = gettext("scrub repaired %s in %lluh%um with "
3614 "%llu errors on %s");
3615 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3616 fmt = gettext("resilvered %s in %lluh%um with "
3617 "%llu errors on %s");
3620 (void) printf(fmt, processed_buf,
3621 (u_longlong_t)(minutes_taken / 60),
3622 (uint_t)(minutes_taken % 60),
3623 (u_longlong_t)ps->pss_errors,
3624 ctime((time_t *)&end));
3626 } else if (ps->pss_state == DSS_CANCELED) {
3627 if (ps->pss_func == POOL_SCAN_SCRUB) {
3628 (void) printf(gettext("scrub canceled on %s"),
3630 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3631 (void) printf(gettext("resilver canceled on %s"),
3637 assert(ps->pss_state == DSS_SCANNING);
3640 * Scan is in progress.
3642 if (ps->pss_func == POOL_SCAN_SCRUB) {
3643 (void) printf(gettext("scrub in progress since %s"),
3645 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3646 (void) printf(gettext("resilver in progress since %s"),
3650 examined = ps->pss_examined ? ps->pss_examined : 1;
3651 total = ps->pss_to_examine;
3652 fraction_done = (double)examined / total;
3654 /* elapsed time for this pass */
3655 elapsed = time(NULL) - ps->pss_pass_start;
3656 elapsed = elapsed ? elapsed : 1;
3657 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3658 rate = pass_exam / elapsed;
3659 rate = rate ? rate : 1;
3660 mins_left = ((total - examined) / rate) / 60;
3661 hours_left = mins_left / 60;
3663 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3664 zfs_nicenum(total, total_buf, sizeof (total_buf));
3665 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3668 * do not print estimated time if hours_left is more than 30 days
3670 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3671 examined_buf, total_buf, rate_buf);
3672 if (hours_left < (30 * 24)) {
3673 (void) printf(gettext(", %lluh%um to go\n"),
3674 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3676 (void) printf(gettext(
3677 ", (scan is slow, no estimated time)\n"));
3680 if (ps->pss_func == POOL_SCAN_RESILVER) {
3681 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3682 processed_buf, 100 * fraction_done);
3683 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3684 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3685 processed_buf, 100 * fraction_done);
3690 print_error_log(zpool_handle_t *zhp)
3692 nvlist_t *nverrlist = NULL;
3695 size_t len = MAXPATHLEN * 2;
3697 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3698 (void) printf("errors: List of errors unavailable "
3699 "(insufficient privileges)\n");
3703 (void) printf("errors: Permanent errors have been "
3704 "detected in the following files:\n\n");
3706 pathname = safe_malloc(len);
3708 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3710 uint64_t dsobj, obj;
3712 verify(nvpair_value_nvlist(elem, &nv) == 0);
3713 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3715 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3717 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3718 (void) printf("%7s %s\n", "", pathname);
3721 nvlist_free(nverrlist);
3725 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3734 (void) printf(gettext("\tspares\n"));
3736 for (i = 0; i < nspares; i++) {
3737 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3738 print_status_config(zhp, name, spares[i],
3739 namewidth, 2, B_TRUE);
3745 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3754 (void) printf(gettext("\tcache\n"));
3756 for (i = 0; i < nl2cache; i++) {
3757 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3758 print_status_config(zhp, name, l2cache[i],
3759 namewidth, 2, B_FALSE);
3765 print_dedup_stats(nvlist_t *config)
3767 ddt_histogram_t *ddh;
3773 * If the pool was faulted then we may not have been able to
3774 * obtain the config. Otherwise, if we have anything in the dedup
3775 * table continue processing the stats.
3777 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3778 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3781 (void) printf("\n");
3782 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3783 (u_longlong_t)ddo->ddo_count,
3784 (u_longlong_t)ddo->ddo_dspace,
3785 (u_longlong_t)ddo->ddo_mspace);
3787 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3788 (uint64_t **)&dds, &c) == 0);
3789 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3790 (uint64_t **)&ddh, &c) == 0);
3791 zpool_dump_ddt(dds, ddh);
3795 * Display a summary of pool status. Displays a summary such as:
3799 * reason: One or more devices ...
3800 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
3806 * When given the '-v' option, we print out the complete config. If the '-e'
3807 * option is specified, then we print out error rate information as well.
3810 status_callback(zpool_handle_t *zhp, void *data)
3812 status_cbdata_t *cbp = data;
3813 nvlist_t *config, *nvroot;
3820 config = zpool_get_config(zhp, NULL);
3821 reason = zpool_get_status(zhp, &msgid);
3826 * If we were given 'zpool status -x', only report those pools with
3829 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3830 if (!cbp->cb_allpools) {
3831 (void) printf(gettext("pool '%s' is healthy\n"),
3832 zpool_get_name(zhp));
3834 cbp->cb_first = B_FALSE;
3840 cbp->cb_first = B_FALSE;
3842 (void) printf("\n");
3844 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3846 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3847 (uint64_t **)&vs, &c) == 0);
3848 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3850 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3851 (void) printf(gettext(" state: %s\n"), health);
3854 case ZPOOL_STATUS_MISSING_DEV_R:
3855 (void) printf(gettext("status: One or more devices could not "
3856 "be opened. Sufficient replicas exist for\n\tthe pool to "
3857 "continue functioning in a degraded state.\n"));
3858 (void) printf(gettext("action: Attach the missing device and "
3859 "online it using 'zpool online'.\n"));
3862 case ZPOOL_STATUS_MISSING_DEV_NR:
3863 (void) printf(gettext("status: One or more devices could not "
3864 "be opened. There are insufficient\n\treplicas for the "
3865 "pool to continue functioning.\n"));
3866 (void) printf(gettext("action: Attach the missing device and "
3867 "online it using 'zpool online'.\n"));
3870 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3871 (void) printf(gettext("status: One or more devices could not "
3872 "be used because the label is missing or\n\tinvalid. "
3873 "Sufficient replicas exist for the pool to continue\n\t"
3874 "functioning in a degraded state.\n"));
3875 (void) printf(gettext("action: Replace the device using "
3876 "'zpool replace'.\n"));
3879 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3880 (void) printf(gettext("status: One or more devices could not "
3881 "be used because the label is missing \n\tor invalid. "
3882 "There are insufficient replicas for the pool to "
3883 "continue\n\tfunctioning.\n"));
3884 zpool_explain_recover(zpool_get_handle(zhp),
3885 zpool_get_name(zhp), reason, config);
3888 case ZPOOL_STATUS_FAILING_DEV:
3889 (void) printf(gettext("status: One or more devices has "
3890 "experienced an unrecoverable error. An\n\tattempt was "
3891 "made to correct the error. Applications are "
3893 (void) printf(gettext("action: Determine if the device needs "
3894 "to be replaced, and clear the errors\n\tusing "
3895 "'zpool clear' or replace the device with 'zpool "
3899 case ZPOOL_STATUS_OFFLINE_DEV:
3900 (void) printf(gettext("status: One or more devices has "
3901 "been taken offline by the administrator.\n\tSufficient "
3902 "replicas exist for the pool to continue functioning in "
3903 "a\n\tdegraded state.\n"));
3904 (void) printf(gettext("action: Online the device using "
3905 "'zpool online' or replace the device with\n\t'zpool "
3909 case ZPOOL_STATUS_REMOVED_DEV:
3910 (void) printf(gettext("status: One or more devices has "
3911 "been removed by the administrator.\n\tSufficient "
3912 "replicas exist for the pool to continue functioning in "
3913 "a\n\tdegraded state.\n"));
3914 (void) printf(gettext("action: Online the device using "
3915 "'zpool online' or replace the device with\n\t'zpool "
3919 case ZPOOL_STATUS_RESILVERING:
3920 (void) printf(gettext("status: One or more devices is "
3921 "currently being resilvered. The pool will\n\tcontinue "
3922 "to function, possibly in a degraded state.\n"));
3923 (void) printf(gettext("action: Wait for the resilver to "
3927 case ZPOOL_STATUS_CORRUPT_DATA:
3928 (void) printf(gettext("status: One or more devices has "
3929 "experienced an error resulting in data\n\tcorruption. "
3930 "Applications may be affected.\n"));
3931 (void) printf(gettext("action: Restore the file in question "
3932 "if possible. Otherwise restore the\n\tentire pool from "
3936 case ZPOOL_STATUS_CORRUPT_POOL:
3937 (void) printf(gettext("status: The pool metadata is corrupted "
3938 "and the pool cannot be opened.\n"));
3939 zpool_explain_recover(zpool_get_handle(zhp),
3940 zpool_get_name(zhp), reason, config);
3943 case ZPOOL_STATUS_VERSION_OLDER:
3944 (void) printf(gettext("status: The pool is formatted using an "
3945 "older on-disk format. The pool can\n\tstill be used, but "
3946 "some features are unavailable.\n"));
3947 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3948 "upgrade'. Once this is done, the\n\tpool will no longer "
3949 "be accessible on older software versions.\n"));
3952 case ZPOOL_STATUS_VERSION_NEWER:
3953 (void) printf(gettext("status: The pool has been upgraded to a "
3954 "newer, incompatible on-disk version.\n\tThe pool cannot "
3955 "be accessed on this system.\n"));
3956 (void) printf(gettext("action: Access the pool from a system "
3957 "running more recent software, or\n\trestore the pool from "
3961 case ZPOOL_STATUS_FAULTED_DEV_R:
3962 (void) printf(gettext("status: One or more devices are "
3963 "faulted in response to persistent errors.\n\tSufficient "
3964 "replicas exist for the pool to continue functioning "
3965 "in a\n\tdegraded state.\n"));
3966 (void) printf(gettext("action: Replace the faulted device, "
3967 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3970 case ZPOOL_STATUS_FAULTED_DEV_NR:
3971 (void) printf(gettext("status: One or more devices are "
3972 "faulted in response to persistent errors. There are "
3973 "insufficient replicas for the pool to\n\tcontinue "
3975 (void) printf(gettext("action: Destroy and re-create the pool "
3976 "from a backup source. Manually marking the device\n"
3977 "\trepaired using 'zpool clear' may allow some data "
3978 "to be recovered.\n"));
3981 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3982 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3983 (void) printf(gettext("status: One or more devices are "
3984 "faulted in response to IO failures.\n"));
3985 (void) printf(gettext("action: Make sure the affected devices "
3986 "are connected, then run 'zpool clear'.\n"));
3989 case ZPOOL_STATUS_BAD_LOG:
3990 (void) printf(gettext("status: An intent log record "
3991 "could not be read.\n"
3992 "\tWaiting for adminstrator intervention to fix the "
3993 "faulted pool.\n"));
3994 (void) printf(gettext("action: Either restore the affected "
3995 "device(s) and run 'zpool online',\n"
3996 "\tor ignore the intent log records by running "
3997 "'zpool clear'.\n"));
4002 * The remaining errors can't actually be generated, yet.
4004 assert(reason == ZPOOL_STATUS_OK);
4008 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4011 if (config != NULL) {
4014 nvlist_t **spares, **l2cache;
4015 uint_t nspares, nl2cache;
4016 pool_scan_stat_t *ps = NULL;
4018 (void) nvlist_lookup_uint64_array(nvroot,
4019 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4020 print_scan_status(ps);
4022 namewidth = max_width(zhp, nvroot, 0, 0);
4026 (void) printf(gettext("config:\n\n"));
4027 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4028 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4029 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4030 namewidth, 0, B_FALSE);
4032 if (num_logs(nvroot) > 0)
4033 print_logs(zhp, nvroot, namewidth, B_TRUE);
4034 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4035 &l2cache, &nl2cache) == 0)
4036 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4038 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4039 &spares, &nspares) == 0)
4040 print_spares(zhp, spares, nspares, namewidth);
4042 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4044 nvlist_t *nverrlist = NULL;
4047 * If the approximate error count is small, get a
4048 * precise count by fetching the entire log and
4049 * uniquifying the results.
4051 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4052 zpool_get_errlog(zhp, &nverrlist) == 0) {
4057 while ((elem = nvlist_next_nvpair(nverrlist,
4062 nvlist_free(nverrlist);
4064 (void) printf("\n");
4067 (void) printf(gettext("errors: No known data "
4069 else if (!cbp->cb_verbose)
4070 (void) printf(gettext("errors: %llu data "
4071 "errors, use '-v' for a list\n"),
4072 (u_longlong_t)nerr);
4074 print_error_log(zhp);
4077 if (cbp->cb_dedup_stats)
4078 print_dedup_stats(config);
4080 (void) printf(gettext("config: The configuration cannot be "
4088 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4090 * -v Display complete error logs
4091 * -x Display only pools with potential problems
4092 * -D Display dedup status (undocumented)
4093 * -T Display a timestamp in date(1) or Unix format
4095 * Describes the health status of all pools or some subset.
4098 zpool_do_status(int argc, char **argv)
4102 unsigned long interval = 0, count = 0;
4103 status_cbdata_t cb = { 0 };
4106 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4109 cb.cb_verbose = B_TRUE;
4112 cb.cb_explain = B_TRUE;
4115 cb.cb_dedup_stats = B_TRUE;
4118 get_timestamp_arg(*optarg);
4121 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4130 get_interval_count(&argc, argv, &interval, &count);
4133 cb.cb_allpools = B_TRUE;
4135 cb.cb_first = B_TRUE;
4138 if (timestamp_fmt != NODATE)
4139 print_timestamp(timestamp_fmt);
4141 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4142 status_callback, &cb);
4144 if (argc == 0 && cb.cb_count == 0)
4145 (void) fprintf(stderr, gettext("no pools available\n"));
4146 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4147 (void) printf(gettext("all pools are healthy\n"));
4155 if (count != 0 && --count == 0)
4158 (void) sleep(interval);
4164 typedef struct upgrade_cbdata {
4169 uint64_t cb_version;
4174 upgrade_cb(zpool_handle_t *zhp, void *arg)
4176 upgrade_cbdata_t *cbp = arg;
4181 config = zpool_get_config(zhp, NULL);
4182 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4185 if (!cbp->cb_newer && version < SPA_VERSION) {
4187 if (cbp->cb_first) {
4188 (void) printf(gettext("The following pools are "
4189 "out of date, and can be upgraded. After "
4190 "being\nupgraded, these pools will no "
4191 "longer be accessible by older software "
4193 (void) printf(gettext("VER POOL\n"));
4194 (void) printf(gettext("--- ------------\n"));
4195 cbp->cb_first = B_FALSE;
4198 (void) printf("%2llu %s\n", (u_longlong_t)version,
4199 zpool_get_name(zhp));
4201 cbp->cb_first = B_FALSE;
4202 ret = zpool_upgrade(zhp, cbp->cb_version);
4204 (void) printf(gettext("Successfully upgraded "
4205 "'%s'\n\n"), zpool_get_name(zhp));
4208 } else if (cbp->cb_newer && version > SPA_VERSION) {
4209 assert(!cbp->cb_all);
4211 if (cbp->cb_first) {
4212 (void) printf(gettext("The following pools are "
4213 "formatted using a newer software version and\n"
4214 "cannot be accessed on the current system.\n\n"));
4215 (void) printf(gettext("VER POOL\n"));
4216 (void) printf(gettext("--- ------------\n"));
4217 cbp->cb_first = B_FALSE;
4220 (void) printf("%2llu %s\n", (u_longlong_t)version,
4221 zpool_get_name(zhp));
4230 upgrade_one(zpool_handle_t *zhp, void *data)
4232 upgrade_cbdata_t *cbp = data;
4233 uint64_t cur_version;
4236 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4237 (void) printf(gettext("'log' is now a reserved word\n"
4238 "Pool 'log' must be renamed using export and import"
4243 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4244 if (cur_version > cbp->cb_version) {
4245 (void) printf(gettext("Pool '%s' is already formatted "
4246 "using more current version '%llu'.\n"),
4247 zpool_get_name(zhp), (u_longlong_t) cur_version);
4250 if (cur_version == cbp->cb_version) {
4251 (void) printf(gettext("Pool '%s' is already formatted "
4252 "using the current version.\n"), zpool_get_name(zhp));
4256 ret = zpool_upgrade(zhp, cbp->cb_version);
4259 (void) printf(gettext("Successfully upgraded '%s' "
4260 "from version %llu to version %llu\n\n"),
4261 zpool_get_name(zhp), (u_longlong_t)cur_version,
4262 (u_longlong_t)cbp->cb_version);
4271 * zpool upgrade [-V version] <-a | pool ...>
4273 * With no arguments, display downrev'd ZFS pool available for upgrade.
4274 * Individual pools can be upgraded by specifying the pool, and '-a' will
4275 * upgrade all pools.
4278 zpool_do_upgrade(int argc, char **argv)
4281 upgrade_cbdata_t cb = { 0 };
4283 boolean_t showversions = B_FALSE;
4288 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4294 showversions = B_TRUE;
4297 cb.cb_version = strtoll(optarg, &end, 10);
4298 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4299 cb.cb_version < SPA_VERSION_1) {
4300 (void) fprintf(stderr,
4301 gettext("invalid version '%s'\n"), optarg);
4306 (void) fprintf(stderr, gettext("missing argument for "
4307 "'%c' option\n"), optopt);
4311 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4322 if (cb.cb_version == 0) {
4323 cb.cb_version = SPA_VERSION;
4324 } else if (!cb.cb_all && argc == 0) {
4325 (void) fprintf(stderr, gettext("-V option is "
4326 "incompatible with other arguments\n"));
4331 if (cb.cb_all || argc != 0) {
4332 (void) fprintf(stderr, gettext("-v option is "
4333 "incompatible with other arguments\n"));
4336 } else if (cb.cb_all) {
4338 (void) fprintf(stderr, gettext("-a option should not "
4339 "be used along with a pool name\n"));
4344 (void) printf(gettext("This system is currently running "
4345 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4346 cb.cb_first = B_TRUE;
4348 (void) printf(gettext("The following versions are "
4350 (void) printf(gettext("VER DESCRIPTION\n"));
4351 (void) printf("--- -----------------------------------------"
4352 "---------------\n");
4353 (void) printf(gettext(" 1 Initial ZFS version\n"));
4354 (void) printf(gettext(" 2 Ditto blocks "
4355 "(replicated metadata)\n"));
4356 (void) printf(gettext(" 3 Hot spares and double parity "
4358 (void) printf(gettext(" 4 zpool history\n"));
4359 (void) printf(gettext(" 5 Compression using the gzip "
4361 (void) printf(gettext(" 6 bootfs pool property\n"));
4362 (void) printf(gettext(" 7 Separate intent log devices\n"));
4363 (void) printf(gettext(" 8 Delegated administration\n"));
4364 (void) printf(gettext(" 9 refquota and refreservation "
4366 (void) printf(gettext(" 10 Cache devices\n"));
4367 (void) printf(gettext(" 11 Improved scrub performance\n"));
4368 (void) printf(gettext(" 12 Snapshot properties\n"));
4369 (void) printf(gettext(" 13 snapused property\n"));
4370 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4371 (void) printf(gettext(" 15 user/group space accounting\n"));
4372 (void) printf(gettext(" 16 stmf property support\n"));
4373 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4374 (void) printf(gettext(" 18 Snapshot user holds\n"));
4375 (void) printf(gettext(" 19 Log device removal\n"));
4376 (void) printf(gettext(" 20 Compression using zle "
4377 "(zero-length encoding)\n"));
4378 (void) printf(gettext(" 21 Deduplication\n"));
4379 (void) printf(gettext(" 22 Received properties\n"));
4380 (void) printf(gettext(" 23 Slim ZIL\n"));
4381 (void) printf(gettext(" 24 System attributes\n"));
4382 (void) printf(gettext(" 25 Improved scrub stats\n"));
4383 (void) printf(gettext(" 26 Improved snapshot deletion "
4385 (void) printf(gettext(" 27 Improved snapshot creation "
4387 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4388 (void) printf(gettext("\nFor more information on a particular "
4389 "version, including supported releases,\n"));
4390 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4391 } else if (argc == 0) {
4394 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4395 notfound = cb.cb_first;
4397 if (!cb.cb_all && ret == 0) {
4399 (void) printf("\n");
4400 cb.cb_first = B_TRUE;
4401 cb.cb_newer = B_TRUE;
4402 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4405 (void) printf("\n");
4411 (void) printf(gettext("All pools are formatted "
4412 "using this version.\n"));
4413 else if (!cb.cb_all)
4414 (void) printf(gettext("Use 'zpool upgrade -v' "
4415 "for a list of available versions and "
4416 "their associated\nfeatures.\n"));
4419 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4426 typedef struct hist_cbdata {
4433 * Print out the command history for a specific pool.
4436 get_history_one(zpool_handle_t *zhp, void *data)
4452 char internalstr[MAXPATHLEN];
4453 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4457 cb->first = B_FALSE;
4459 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4461 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4464 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4465 &records, &numrecords) == 0);
4466 for (i = 0; i < numrecords; i++) {
4467 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4471 /* is it an internal event or a standard event? */
4472 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4474 if (cb->internal == 0)
4477 if (nvlist_lookup_uint64(records[i],
4478 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4480 verify(nvlist_lookup_uint64(records[i],
4481 ZPOOL_HIST_TXG, &txg) == 0);
4482 verify(nvlist_lookup_string(records[i],
4483 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4484 if (ievent >= LOG_END)
4486 (void) snprintf(internalstr,
4487 sizeof (internalstr),
4488 "[internal %s txg:%llu] %s",
4489 zfs_history_event_names[ievent], (u_longlong_t)txg,
4491 cmdstr = internalstr;
4494 (void) localtime_r(&tsec, &t);
4495 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4496 (void) printf("%s %s", tbuf, cmdstr);
4499 (void) printf("\n");
4502 (void) printf(" [");
4503 if (nvlist_lookup_uint64(records[i],
4504 ZPOOL_HIST_WHO, &who) == 0) {
4505 pwd = getpwuid((uid_t)who);
4507 (void) printf("user %s on",
4510 (void) printf("user %d on",
4513 (void) printf(gettext("no info]\n"));
4516 if (nvlist_lookup_string(records[i],
4517 ZPOOL_HIST_HOST, &hostname) == 0) {
4518 (void) printf(" %s", hostname);
4520 if (nvlist_lookup_string(records[i],
4521 ZPOOL_HIST_ZONE, &zonename) == 0) {
4522 (void) printf(":%s", zonename);
4526 (void) printf("\n");
4528 (void) printf("\n");
4535 * zpool history <pool>
4537 * Displays the history of commands that modified pools.
4542 zpool_do_history(int argc, char **argv)
4544 hist_cbdata_t cbdata = { 0 };
4548 cbdata.first = B_TRUE;
4550 while ((c = getopt(argc, argv, "li")) != -1) {
4556 cbdata.internal = 1;
4559 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4567 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4570 if (argc == 0 && cbdata.first == B_TRUE) {
4571 (void) fprintf(stderr, gettext("no pools available\n"));
4578 typedef struct ev_opts {
4586 zpool_do_events_short(nvlist_t *nvl)
4588 char ctime_str[26], str[32], *ptr;
4592 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
4593 memset(str, ' ', 32);
4594 (void) ctime_r((const time_t *)&tv[0], ctime_str);
4595 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
4596 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
4597 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
4598 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
4599 (void) printf(gettext("%s "), str);
4601 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
4602 (void) printf(gettext("%s\n"), ptr);
4606 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
4610 for (nvp = nvlist_next_nvpair(nvl, NULL);
4611 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
4613 data_type_t type = nvpair_type(nvp);
4614 const char *name = nvpair_name(nvp);
4624 printf(gettext("%*s%s = "), depth, "", name);
4627 case DATA_TYPE_BOOLEAN:
4628 printf(gettext("%s"), "1");
4631 case DATA_TYPE_BOOLEAN_VALUE:
4632 (void) nvpair_value_boolean_value(nvp, &b);
4633 printf(gettext("%s"), b ? "1" : "0");
4636 case DATA_TYPE_BYTE:
4637 (void) nvpair_value_byte(nvp, &i8);
4638 printf(gettext("0x%x"), i8);
4641 case DATA_TYPE_INT8:
4642 (void) nvpair_value_int8(nvp, (void *)&i8);
4643 printf(gettext("0x%x"), i8);
4646 case DATA_TYPE_UINT8:
4647 (void) nvpair_value_uint8(nvp, &i8);
4648 printf(gettext("0x%x"), i8);
4651 case DATA_TYPE_INT16:
4652 (void) nvpair_value_int16(nvp, (void *)&i16);
4653 printf(gettext("0x%x"), i16);
4656 case DATA_TYPE_UINT16:
4657 (void) nvpair_value_uint16(nvp, &i16);
4658 printf(gettext("0x%x"), i16);
4661 case DATA_TYPE_INT32:
4662 (void) nvpair_value_int32(nvp, (void *)&i32);
4663 printf(gettext("0x%x"), i32);
4666 case DATA_TYPE_UINT32:
4667 (void) nvpair_value_uint32(nvp, &i32);
4668 printf(gettext("0x%x"), i32);
4671 case DATA_TYPE_INT64:
4672 (void) nvpair_value_int64(nvp, (void *)&i64);
4673 printf(gettext("0x%llx"), (u_longlong_t)i64);
4676 case DATA_TYPE_UINT64:
4677 (void) nvpair_value_uint64(nvp, &i64);
4678 printf(gettext("0x%llx"), (u_longlong_t)i64);
4681 case DATA_TYPE_HRTIME:
4682 (void) nvpair_value_hrtime(nvp, (void *)&i64);
4683 printf(gettext("0x%llx"), (u_longlong_t)i64);
4686 case DATA_TYPE_STRING:
4687 (void) nvpair_value_string(nvp, &str);
4688 printf(gettext("\"%s\""), str ? str : "<NULL>");
4691 case DATA_TYPE_NVLIST:
4692 printf(gettext("(embedded nvlist)\n"));
4693 (void) nvpair_value_nvlist(nvp, &cnv);
4694 zpool_do_events_nvprint(cnv, depth + 8);
4695 printf(gettext("%*s(end %s)"), depth, "", name);
4698 case DATA_TYPE_NVLIST_ARRAY: {
4702 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
4703 printf(gettext("(%d embedded nvlists)\n"), nelem);
4704 for (i = 0; i < nelem; i++) {
4705 printf(gettext("%*s%s[%d] = %s\n"),
4706 depth, "", name, i, "(embedded nvlist)");
4707 zpool_do_events_nvprint(val[i], depth + 8);
4708 printf(gettext("%*s(end %s[%i])\n"),
4709 depth, "", name, i);
4711 printf(gettext("%*s(end %s)\n"), depth, "", name);
4715 case DATA_TYPE_INT8_ARRAY: {
4719 (void) nvpair_value_int8_array(nvp, &val, &nelem);
4720 for (i = 0; i < nelem; i++)
4721 printf(gettext("0x%x "), val[i]);
4726 case DATA_TYPE_UINT8_ARRAY: {
4730 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
4731 for (i = 0; i < nelem; i++)
4732 printf(gettext("0x%x "), val[i]);
4737 case DATA_TYPE_INT16_ARRAY: {
4741 (void) nvpair_value_int16_array(nvp, &val, &nelem);
4742 for (i = 0; i < nelem; i++)
4743 printf(gettext("0x%x "), val[i]);
4748 case DATA_TYPE_UINT16_ARRAY: {
4752 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
4753 for (i = 0; i < nelem; i++)
4754 printf(gettext("0x%x "), val[i]);
4759 case DATA_TYPE_INT32_ARRAY: {
4763 (void) nvpair_value_int32_array(nvp, &val, &nelem);
4764 for (i = 0; i < nelem; i++)
4765 printf(gettext("0x%x "), val[i]);
4770 case DATA_TYPE_UINT32_ARRAY: {
4774 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
4775 for (i = 0; i < nelem; i++)
4776 printf(gettext("0x%x "), val[i]);
4781 case DATA_TYPE_INT64_ARRAY: {
4785 (void) nvpair_value_int64_array(nvp, &val, &nelem);
4786 for (i = 0; i < nelem; i++)
4787 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4792 case DATA_TYPE_UINT64_ARRAY: {
4796 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
4797 for (i = 0; i < nelem; i++)
4798 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
4803 case DATA_TYPE_STRING_ARRAY:
4804 case DATA_TYPE_BOOLEAN_ARRAY:
4805 case DATA_TYPE_BYTE_ARRAY:
4806 case DATA_TYPE_DOUBLE:
4807 case DATA_TYPE_UNKNOWN:
4808 printf(gettext("<unknown>"));
4812 printf(gettext("\n"));
4817 zpool_do_events_next(ev_opts_t *opts)
4820 int cleanup_fd, ret, dropped;
4822 cleanup_fd = open(ZFS_DEV, O_RDWR);
4823 VERIFY(cleanup_fd >= 0);
4825 if (!opts->scripted)
4826 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
4829 ret = zpool_events_next(g_zfs, &nvl, &dropped,
4830 !!opts->follow, cleanup_fd);
4831 if (ret || nvl == NULL)
4835 (void) printf(gettext("dropped %d events\n"), dropped);
4837 zpool_do_events_short(nvl);
4839 if (opts->verbose) {
4840 zpool_do_events_nvprint(nvl, 8);
4841 printf(gettext("\n"));
4847 VERIFY(0 == close(cleanup_fd));
4853 zpool_do_events_clear(ev_opts_t *opts)
4857 ret = zpool_events_clear(g_zfs, &count);
4859 (void) printf(gettext("cleared %d events\n"), count);
4865 * zpool events [-vfc]
4867 * Displays events logs by ZFS.
4870 zpool_do_events(int argc, char **argv)
4872 ev_opts_t opts = { 0 };
4877 while ((c = getopt(argc, argv, "vHfc")) != -1) {
4892 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4901 ret = zpool_do_events_clear(&opts);
4903 ret = zpool_do_events_next(&opts);
4909 get_callback(zpool_handle_t *zhp, void *data)
4911 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4912 char value[MAXNAMELEN];
4913 zprop_source_t srctype;
4916 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4919 * Skip the special fake placeholder. This will also skip
4920 * over the name property when 'all' is specified.
4922 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4923 pl == cbp->cb_proplist)
4926 if (zpool_get_prop(zhp, pl->pl_prop,
4927 value, sizeof (value), &srctype) != 0)
4930 zprop_print_one_property(zpool_get_name(zhp), cbp,
4931 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4938 zpool_do_get(int argc, char **argv)
4940 zprop_get_cbdata_t cb = { 0 };
4941 zprop_list_t fake_name = { 0 };
4947 cb.cb_first = B_TRUE;
4948 cb.cb_sources = ZPROP_SRC_ALL;
4949 cb.cb_columns[0] = GET_COL_NAME;
4950 cb.cb_columns[1] = GET_COL_PROPERTY;
4951 cb.cb_columns[2] = GET_COL_VALUE;
4952 cb.cb_columns[3] = GET_COL_SOURCE;
4953 cb.cb_type = ZFS_TYPE_POOL;
4955 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4956 ZFS_TYPE_POOL) != 0)
4959 if (cb.cb_proplist != NULL) {
4960 fake_name.pl_prop = ZPOOL_PROP_NAME;
4961 fake_name.pl_width = strlen(gettext("NAME"));
4962 fake_name.pl_next = cb.cb_proplist;
4963 cb.cb_proplist = &fake_name;
4966 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4969 if (cb.cb_proplist == &fake_name)
4970 zprop_free_list(fake_name.pl_next);
4972 zprop_free_list(cb.cb_proplist);
4977 typedef struct set_cbdata {
4980 boolean_t cb_any_successful;
4984 set_callback(zpool_handle_t *zhp, void *data)
4987 set_cbdata_t *cb = (set_cbdata_t *)data;
4989 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4992 cb->cb_any_successful = B_TRUE;
4998 zpool_do_set(int argc, char **argv)
5000 set_cbdata_t cb = { 0 };
5003 if (argc > 1 && argv[1][0] == '-') {
5004 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5010 (void) fprintf(stderr, gettext("missing property=value "
5016 (void) fprintf(stderr, gettext("missing pool name\n"));
5021 (void) fprintf(stderr, gettext("too many pool names\n"));
5025 cb.cb_propname = argv[1];
5026 cb.cb_value = strchr(cb.cb_propname, '=');
5027 if (cb.cb_value == NULL) {
5028 (void) fprintf(stderr, gettext("missing value in "
5029 "property=value argument\n"));
5033 *(cb.cb_value) = '\0';
5036 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5043 find_command_idx(char *command, int *idx)
5047 for (i = 0; i < NCOMMAND; i++) {
5048 if (command_table[i].name == NULL)
5051 if (strcmp(command, command_table[i].name) == 0) {
5060 main(int argc, char **argv)
5066 (void) setlocale(LC_ALL, "");
5067 (void) textdomain(TEXT_DOMAIN);
5072 * Make sure the user has specified some command.
5075 (void) fprintf(stderr, gettext("missing command\n"));
5084 if ((strcmp(cmdname, "-?") == 0) ||
5085 strcmp(cmdname, "--help") == 0)
5088 if ((g_zfs = libzfs_init()) == NULL)
5091 libzfs_print_on_error(g_zfs, B_TRUE);
5093 zpool_set_history_str("zpool", argc, argv, history_str);
5094 verify(zpool_stage_history(g_zfs, history_str) == 0);
5097 * Run the appropriate command.
5099 if (find_command_idx(cmdname, &i) == 0) {
5100 current_command = &command_table[i];
5101 ret = command_table[i].func(argc - 1, argv + 1);
5102 } else if (strchr(cmdname, '=')) {
5103 verify(find_command_idx("set", &i) == 0);
5104 current_command = &command_table[i];
5105 ret = command_table[i].func(argc, argv);
5106 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5108 * 'freeze' is a vile debugging abomination, so we treat
5112 int fd = open(ZFS_DEV, O_RDWR);
5113 (void) strcpy((void *)buf, argv[2]);
5114 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5116 (void) fprintf(stderr, gettext("unrecognized "
5117 "command '%s'\n"), cmdname);
5125 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5126 * for the purposes of running ::findleaks.
5128 if (getenv("ZFS_ABORT") != NULL) {
5129 (void) printf("dumping core by request\n");