4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2011 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
49 #include <sys/fs/zfs.h>
54 #include "zpool_util.h"
55 #include "zfs_comutil.h"
57 #include "statcommon.h"
59 static int zpool_do_create(int, char **);
60 static int zpool_do_destroy(int, char **);
62 static int zpool_do_add(int, char **);
63 static int zpool_do_remove(int, char **);
64 static int zpool_do_labelclear(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 **);
74 static int zpool_do_reguid(int, char **);
76 static int zpool_do_attach(int, char **);
77 static int zpool_do_detach(int, char **);
78 static int zpool_do_replace(int, char **);
79 static int zpool_do_split(int, char **);
81 static int zpool_do_scrub(int, char **);
83 static int zpool_do_import(int, char **);
84 static int zpool_do_export(int, char **);
86 static int zpool_do_upgrade(int, char **);
88 static int zpool_do_history(int, char **);
90 static int zpool_do_get(int, char **);
91 static int zpool_do_set(int, char **);
94 * These libumem hooks provide a reasonable set of defaults for the allocator's
95 * debugging facilities.
100 _umem_debug_init(void)
102 return ("default,verbose"); /* $UMEM_DEBUG setting */
106 _umem_logging_init(void)
108 return ("fail,contents"); /* $UMEM_LOGGING setting */
139 typedef struct zpool_command {
141 int (*func)(int, char **);
146 * Master command table. Each ZFS command has a name, associated function, and
147 * usage message. The usage messages need to be internationalized, so we have
148 * to have a function to return the usage message based on a command index.
150 * These commands are organized according to how they are displayed in the usage
151 * message. An empty command (one with a NULL name) indicates an empty line in
152 * the generic usage message.
154 static zpool_command_t command_table[] = {
155 { "create", zpool_do_create, HELP_CREATE },
156 { "destroy", zpool_do_destroy, HELP_DESTROY },
158 { "add", zpool_do_add, HELP_ADD },
159 { "remove", zpool_do_remove, HELP_REMOVE },
161 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
163 { "list", zpool_do_list, HELP_LIST },
164 { "iostat", zpool_do_iostat, HELP_IOSTAT },
165 { "status", zpool_do_status, HELP_STATUS },
167 { "online", zpool_do_online, HELP_ONLINE },
168 { "offline", zpool_do_offline, HELP_OFFLINE },
169 { "clear", zpool_do_clear, HELP_CLEAR },
171 { "attach", zpool_do_attach, HELP_ATTACH },
172 { "detach", zpool_do_detach, HELP_DETACH },
173 { "replace", zpool_do_replace, HELP_REPLACE },
174 { "split", zpool_do_split, HELP_SPLIT },
176 { "scrub", zpool_do_scrub, HELP_SCRUB },
178 { "import", zpool_do_import, HELP_IMPORT },
179 { "export", zpool_do_export, HELP_EXPORT },
180 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
181 { "reguid", zpool_do_reguid, HELP_REGUID },
183 { "history", zpool_do_history, HELP_HISTORY },
184 { "get", zpool_do_get, HELP_GET },
185 { "set", zpool_do_set, HELP_SET },
188 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
190 zpool_command_t *current_command;
191 static char history_str[HIS_MAX_RECORD_LEN];
193 static uint_t timestamp_fmt = NODATE;
196 get_usage(zpool_help_t idx) {
199 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
201 return (gettext("\tattach [-f] <pool> <device> "
204 return (gettext("\tclear [-nF] <pool> [device]\n"));
206 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
207 "\t [-O file-system-property=value] ... \n"
208 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
210 return (gettext("\tdestroy [-f] <pool>\n"));
212 return (gettext("\tdetach <pool> <device>\n"));
214 return (gettext("\texport [-f] <pool> ...\n"));
216 return (gettext("\thistory [-il] [<pool>] ...\n"));
218 return (gettext("\timport [-d dir] [-D]\n"
219 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
220 "\timport [-o mntopts] [-o property=value] ... \n"
221 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
222 "[-R root] [-F [-n]] -a\n"
223 "\timport [-o mntopts] [-o property=value] ... \n"
224 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
225 "[-R root] [-F [-n]]\n"
226 "\t <pool | id> [newpool]\n"));
228 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
230 case HELP_LABELCLEAR:
231 return (gettext("\tlabelclear [-f] <vdev>\n"));
233 return (gettext("\tlist [-H] [-o property[,...]] "
234 "[-T d|u] [pool] ... [interval [count]]\n"));
236 return (gettext("\toffline [-t] <pool> <device> ...\n"));
238 return (gettext("\tonline [-e] <pool> <device> ...\n"));
240 return (gettext("\treplace [-f] <pool> <device> "
243 return (gettext("\tremove <pool> <device> ...\n"));
245 return (gettext("\tscrub [-s] <pool> ...\n"));
247 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
250 return (gettext("\tupgrade [-v]\n"
251 "\tupgrade [-V version] <-a | pool ...>\n"));
253 return (gettext("\tget <\"all\" | property[,...]> "
256 return (gettext("\tset <property=value> <pool> \n"));
258 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
259 "\t [-o property=value] <pool> <newpool> "
260 "[<device> ...]\n"));
262 return (gettext("\treguid <pool>\n"));
271 * Callback routine that will print out a pool property value.
274 print_prop_cb(int prop, void *cb)
278 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
280 if (zpool_prop_readonly(prop))
281 (void) fprintf(fp, " NO ");
283 (void) fprintf(fp, " YES ");
285 if (zpool_prop_values(prop) == NULL)
286 (void) fprintf(fp, "-\n");
288 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
294 * Display usage message. If we're inside a command, display only the usage for
295 * that command. Otherwise, iterate over the entire command table and display
296 * a complete usage message.
299 usage(boolean_t requested)
301 FILE *fp = requested ? stdout : stderr;
303 if (current_command == NULL) {
306 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
308 gettext("where 'command' is one of the following:\n\n"));
310 for (i = 0; i < NCOMMAND; i++) {
311 if (command_table[i].name == NULL)
312 (void) fprintf(fp, "\n");
314 (void) fprintf(fp, "%s",
315 get_usage(command_table[i].usage));
318 (void) fprintf(fp, gettext("usage:\n"));
319 (void) fprintf(fp, "%s", get_usage(current_command->usage));
322 if (current_command != NULL &&
323 ((strcmp(current_command->name, "set") == 0) ||
324 (strcmp(current_command->name, "get") == 0) ||
325 (strcmp(current_command->name, "list") == 0))) {
328 gettext("\nthe following properties are supported:\n"));
330 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
331 "PROPERTY", "EDIT", "VALUES");
333 /* Iterate over all properties */
334 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
339 * See comments at end of main().
341 if (getenv("ZFS_ABORT") != NULL) {
342 (void) printf("dumping core by request\n");
346 exit(requested ? 0 : 2);
350 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
351 boolean_t print_logs)
358 (void) printf("\t%*s%s\n", indent, "", name);
360 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
361 &child, &children) != 0)
364 for (c = 0; c < children; c++) {
365 uint64_t is_log = B_FALSE;
367 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
369 if ((is_log && !print_logs) || (!is_log && print_logs))
372 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
373 print_vdev_tree(zhp, vname, child[c], indent + 2,
380 * Add a property pair (name, string-value) into a property nvlist.
383 add_prop_list(const char *propname, char *propval, nvlist_t **props,
386 zpool_prop_t prop = ZPROP_INVAL;
392 if (*props == NULL &&
393 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
394 (void) fprintf(stderr,
395 gettext("internal error: out of memory\n"));
402 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
403 (void) fprintf(stderr, gettext("property '%s' is "
404 "not a valid pool property\n"), propname);
407 normnm = zpool_prop_to_name(prop);
409 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
410 normnm = zfs_prop_to_name(fprop);
416 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
417 prop != ZPOOL_PROP_CACHEFILE) {
418 (void) fprintf(stderr, gettext("property '%s' "
419 "specified multiple times\n"), propname);
423 if (nvlist_add_string(proplist, normnm, propval) != 0) {
424 (void) fprintf(stderr, gettext("internal "
425 "error: out of memory\n"));
433 * zpool add [-fn] <pool> <vdev> ...
435 * -f Force addition of devices, even if they appear in use
436 * -n Do not add the devices, but display the resulting layout if
437 * they were to be added.
439 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
440 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
444 zpool_do_add(int argc, char **argv)
446 boolean_t force = B_FALSE;
447 boolean_t dryrun = B_FALSE;
456 while ((c = getopt(argc, argv, "fn")) != -1) {
465 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
474 /* get pool name and check number of arguments */
476 (void) fprintf(stderr, gettext("missing pool name argument\n"));
480 (void) fprintf(stderr, gettext("missing vdev specification\n"));
489 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
492 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
493 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
499 /* pass off to get_vdev_spec for processing */
500 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
502 if (nvroot == NULL) {
508 nvlist_t *poolnvroot;
510 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
513 (void) printf(gettext("would update '%s' to the following "
514 "configuration:\n"), zpool_get_name(zhp));
516 /* print original main pool and new tree */
517 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
518 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
520 /* Do the same for the logs */
521 if (num_logs(poolnvroot) > 0) {
522 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
523 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
524 } else if (num_logs(nvroot) > 0) {
525 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
530 ret = (zpool_add(zhp, nvroot) != 0);
540 * zpool remove <pool> <vdev> ...
542 * Removes the given vdev from the pool. Currently, this supports removing
543 * spares, cache, and log devices from the pool.
546 zpool_do_remove(int argc, char **argv)
555 /* get pool name and check number of arguments */
557 (void) fprintf(stderr, gettext("missing pool name argument\n"));
561 (void) fprintf(stderr, gettext("missing device\n"));
567 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
570 for (i = 1; i < argc; i++) {
571 if (zpool_vdev_remove(zhp, argv[i]) != 0)
579 * zpool labelclear <vdev>
581 * Verifies that the vdev is not active and zeros out the label information
585 zpool_do_labelclear(int argc, char **argv)
588 int c, fd = -1, ret = 0;
590 boolean_t inuse = B_FALSE;
591 boolean_t force = B_FALSE;
594 while ((c = getopt(argc, argv, "f")) != -1) {
600 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
611 (void) fprintf(stderr, gettext("missing vdev device name\n"));
616 if ((fd = open(vdev, O_RDWR)) < 0) {
617 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
622 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
626 (void) fprintf(stderr,
627 gettext("Unable to determine pool state for %s\n"
628 "Use -f to force the clearing any label data\n"), vdev);
636 case POOL_STATE_ACTIVE:
637 case POOL_STATE_SPARE:
638 case POOL_STATE_L2CACHE:
639 (void) fprintf(stderr,
640 gettext("labelclear operation failed.\n"
641 "\tVdev %s is a member (%s), of pool \"%s\".\n"
642 "\tTo remove label information from this device, export or destroy\n"
643 "\tthe pool, or remove %s from the configuration of this pool\n"
644 "\tand retry the labelclear operation\n"),
645 vdev, zpool_pool_state_to_name(state), name, vdev);
649 case POOL_STATE_EXPORTED:
653 (void) fprintf(stderr,
654 gettext("labelclear operation failed.\n"
655 "\tVdev %s is a member of the exported pool \"%s\".\n"
656 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
662 case POOL_STATE_POTENTIALLY_ACTIVE:
666 (void) fprintf(stderr,
667 gettext("labelclear operation failed.\n"
668 "\tVdev %s is a member of the pool \"%s\".\n"
669 "\tThis pool is unknown to this system, but may be active on\n"
670 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
671 "\tremoval of label information.\n"),
676 case POOL_STATE_DESTROYED:
677 /* inuse should never be set for a destoryed pool... */
683 if (zpool_clear_label(fd) != 0) {
684 (void) fprintf(stderr,
685 gettext("Label clear failed on vdev %s\n"), vdev);
698 * zpool create [-fn] [-o property=value] ...
699 * [-O file-system-property=value] ...
700 * [-R root] [-m mountpoint] <pool> <dev> ...
702 * -f Force creation, even if devices appear in use
703 * -n Do not create the pool, but display the resulting layout if it
704 * were to be created.
705 * -R Create a pool under an alternate root
706 * -m Set default mountpoint for the root dataset. By default it's
708 * -o Set property=value.
709 * -O Set fsproperty=value in the pool's root file system
711 * Creates the named pool according to the given vdev specification. The
712 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
713 * we get the nvlist back from get_vdev_spec(), we either print out the contents
714 * (if '-n' was specified), or pass it to libzfs to do the creation.
717 zpool_do_create(int argc, char **argv)
719 boolean_t force = B_FALSE;
720 boolean_t dryrun = B_FALSE;
722 nvlist_t *nvroot = NULL;
725 char *altroot = NULL;
726 char *mountpoint = NULL;
727 nvlist_t *fsprops = NULL;
728 nvlist_t *props = NULL;
732 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
742 if (add_prop_list(zpool_prop_to_name(
743 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
745 if (nvlist_lookup_string(props,
746 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
749 if (add_prop_list(zpool_prop_to_name(
750 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
757 if ((propval = strchr(optarg, '=')) == NULL) {
758 (void) fprintf(stderr, gettext("missing "
759 "'=' for -o option\n"));
765 if (add_prop_list(optarg, propval, &props, B_TRUE))
769 if ((propval = strchr(optarg, '=')) == NULL) {
770 (void) fprintf(stderr, gettext("missing "
771 "'=' for -O option\n"));
777 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
781 (void) fprintf(stderr, gettext("missing argument for "
782 "'%c' option\n"), optopt);
785 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
794 /* get pool name and check number of arguments */
796 (void) fprintf(stderr, gettext("missing pool name argument\n"));
800 (void) fprintf(stderr, gettext("missing vdev specification\n"));
807 * As a special case, check for use of '/' in the name, and direct the
808 * user to use 'zfs create' instead.
810 if (strchr(poolname, '/') != NULL) {
811 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
812 "character '/' in pool name\n"), poolname);
813 (void) fprintf(stderr, gettext("use 'zfs create' to "
814 "create a dataset\n"));
818 /* pass off to get_vdev_spec for bulk processing */
819 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
824 /* make_root_vdev() allows 0 toplevel children if there are spares */
825 if (!zfs_allocatable_devs(nvroot)) {
826 (void) fprintf(stderr, gettext("invalid vdev "
827 "specification: at least one toplevel vdev must be "
833 if (altroot != NULL && altroot[0] != '/') {
834 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
835 "must be an absolute path\n"), altroot);
840 * Check the validity of the mountpoint and direct the user to use the
841 * '-m' mountpoint option if it looks like its in use.
843 if (mountpoint == NULL ||
844 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
845 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
846 char buf[MAXPATHLEN];
849 if (mountpoint && mountpoint[0] != '/') {
850 (void) fprintf(stderr, gettext("invalid mountpoint "
851 "'%s': must be an absolute path, 'legacy', or "
852 "'none'\n"), mountpoint);
856 if (mountpoint == NULL) {
858 (void) snprintf(buf, sizeof (buf), "%s/%s",
861 (void) snprintf(buf, sizeof (buf), "/%s",
865 (void) snprintf(buf, sizeof (buf), "%s%s",
866 altroot, mountpoint);
868 (void) snprintf(buf, sizeof (buf), "%s",
872 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
873 (void) fprintf(stderr, gettext("mountpoint '%s' : "
874 "%s\n"), buf, strerror(errno));
875 (void) fprintf(stderr, gettext("use '-m' "
876 "option to provide a different default\n"));
881 while (count < 3 && readdir(dirp) != NULL)
883 (void) closedir(dirp);
886 (void) fprintf(stderr, gettext("mountpoint "
887 "'%s' exists and is not empty\n"), buf);
888 (void) fprintf(stderr, gettext("use '-m' "
889 "option to provide a "
890 "different default\n"));
898 * For a dry run invocation, print out a basic message and run
899 * through all the vdevs in the list and print out in an
900 * appropriate hierarchy.
902 (void) printf(gettext("would create '%s' with the "
903 "following layout:\n\n"), poolname);
905 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
906 if (num_logs(nvroot) > 0)
907 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
912 * Hand off to libzfs.
914 if (zpool_create(g_zfs, poolname,
915 nvroot, props, fsprops) == 0) {
916 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
917 ZFS_TYPE_FILESYSTEM);
919 if (mountpoint != NULL)
920 verify(zfs_prop_set(pool,
922 ZFS_PROP_MOUNTPOINT),
924 if (zfs_mount(pool, NULL, 0) == 0)
925 ret = zfs_shareall(pool);
928 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
929 (void) fprintf(stderr, gettext("pool name may have "
936 nvlist_free(fsprops);
940 nvlist_free(fsprops);
947 * zpool destroy <pool>
949 * -f Forcefully unmount any datasets
951 * Destroy the given pool. Automatically unmounts any datasets in the pool.
954 zpool_do_destroy(int argc, char **argv)
956 boolean_t force = B_FALSE;
963 while ((c = getopt(argc, argv, "f")) != -1) {
969 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
978 /* check arguments */
980 (void) fprintf(stderr, gettext("missing pool argument\n"));
984 (void) fprintf(stderr, gettext("too many arguments\n"));
990 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
992 * As a special case, check for use of '/' in the name, and
993 * direct the user to use 'zfs destroy' instead.
995 if (strchr(pool, '/') != NULL)
996 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
997 "destroy a dataset\n"));
1001 if (zpool_disable_datasets(zhp, force) != 0) {
1002 (void) fprintf(stderr, gettext("could not destroy '%s': "
1003 "could not unmount datasets\n"), zpool_get_name(zhp));
1007 ret = (zpool_destroy(zhp) != 0);
1015 * zpool export [-f] <pool> ...
1017 * -f Forcefully unmount datasets
1019 * Export the given pools. By default, the command will attempt to cleanly
1020 * unmount any active datasets within the pool. If the '-f' flag is specified,
1021 * then the datasets will be forcefully unmounted.
1024 zpool_do_export(int argc, char **argv)
1026 boolean_t force = B_FALSE;
1027 boolean_t hardforce = B_FALSE;
1029 zpool_handle_t *zhp;
1034 while ((c = getopt(argc, argv, "fF")) != -1) {
1043 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1052 /* check arguments */
1054 (void) fprintf(stderr, gettext("missing pool argument\n"));
1059 for (i = 0; i < argc; i++) {
1060 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1065 if (zpool_disable_datasets(zhp, force) != 0) {
1072 if (zpool_export_force(zhp) != 0)
1074 } else if (zpool_export(zhp, force) != 0) {
1085 * Given a vdev configuration, determine the maximum width needed for the device
1089 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1091 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1096 if (strlen(name) + depth > max)
1097 max = strlen(name) + depth;
1101 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1102 &child, &children) == 0) {
1103 for (c = 0; c < children; c++)
1104 if ((ret = max_width(zhp, child[c], depth + 2,
1109 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1110 &child, &children) == 0) {
1111 for (c = 0; c < children; c++)
1112 if ((ret = max_width(zhp, child[c], depth + 2,
1117 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1118 &child, &children) == 0) {
1119 for (c = 0; c < children; c++)
1120 if ((ret = max_width(zhp, child[c], depth + 2,
1129 typedef struct spare_cbdata {
1131 zpool_handle_t *cb_zhp;
1135 find_vdev(nvlist_t *nv, uint64_t search)
1141 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1145 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1146 &child, &children) == 0) {
1147 for (c = 0; c < children; c++)
1148 if (find_vdev(child[c], search))
1156 find_spare(zpool_handle_t *zhp, void *data)
1158 spare_cbdata_t *cbp = data;
1159 nvlist_t *config, *nvroot;
1161 config = zpool_get_config(zhp, NULL);
1162 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1165 if (find_vdev(nvroot, cbp->cb_guid)) {
1175 * Print out configuration state as requested by status_callback.
1178 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1179 int namewidth, int depth, boolean_t isspare)
1183 pool_scan_stat_t *ps = NULL;
1185 char rbuf[6], wbuf[6], cbuf[6];
1187 uint64_t notpresent;
1191 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1192 &child, &children) != 0)
1195 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1196 (uint64_t **)&vs, &c) == 0);
1198 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1201 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1204 if (vs->vs_aux == VDEV_AUX_SPARED)
1206 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1210 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1214 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1215 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1216 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1217 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1220 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1221 ¬present) == 0 ||
1222 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1224 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1225 (void) printf(" was %s", path);
1226 } else if (vs->vs_aux != 0) {
1229 switch (vs->vs_aux) {
1230 case VDEV_AUX_OPEN_FAILED:
1231 (void) printf(gettext("cannot open"));
1234 case VDEV_AUX_BAD_GUID_SUM:
1235 (void) printf(gettext("missing device"));
1238 case VDEV_AUX_NO_REPLICAS:
1239 (void) printf(gettext("insufficient replicas"));
1242 case VDEV_AUX_VERSION_NEWER:
1243 (void) printf(gettext("newer version"));
1246 case VDEV_AUX_SPARED:
1247 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1249 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1250 if (strcmp(zpool_get_name(cb.cb_zhp),
1251 zpool_get_name(zhp)) == 0)
1252 (void) printf(gettext("currently in "
1255 (void) printf(gettext("in use by "
1257 zpool_get_name(cb.cb_zhp));
1258 zpool_close(cb.cb_zhp);
1260 (void) printf(gettext("currently in use"));
1264 case VDEV_AUX_ERR_EXCEEDED:
1265 (void) printf(gettext("too many errors"));
1268 case VDEV_AUX_IO_FAILURE:
1269 (void) printf(gettext("experienced I/O failures"));
1272 case VDEV_AUX_BAD_LOG:
1273 (void) printf(gettext("bad intent log"));
1276 case VDEV_AUX_EXTERNAL:
1277 (void) printf(gettext("external device fault"));
1280 case VDEV_AUX_SPLIT_POOL:
1281 (void) printf(gettext("split into new pool"));
1285 (void) printf(gettext("corrupted data"));
1290 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1291 (uint64_t **)&ps, &c);
1293 if (ps && ps->pss_state == DSS_SCANNING &&
1294 vs->vs_scan_processed != 0 && children == 0) {
1295 (void) printf(gettext(" (%s)"),
1296 (ps->pss_func == POOL_SCAN_RESILVER) ?
1297 "resilvering" : "repairing");
1300 (void) printf("\n");
1302 for (c = 0; c < children; c++) {
1303 uint64_t islog = B_FALSE, ishole = B_FALSE;
1305 /* Don't print logs or holes here */
1306 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1308 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1310 if (islog || ishole)
1312 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1313 print_status_config(zhp, vname, child[c],
1314 namewidth, depth + 2, isspare);
1321 * Print the configuration of an exported pool. Iterate over all vdevs in the
1322 * pool, printing out the name and status for each one.
1325 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1332 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1333 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1334 strcmp(type, VDEV_TYPE_HOLE) == 0)
1337 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1338 (uint64_t **)&vs, &c) == 0);
1340 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1341 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1343 if (vs->vs_aux != 0) {
1346 switch (vs->vs_aux) {
1347 case VDEV_AUX_OPEN_FAILED:
1348 (void) printf(gettext("cannot open"));
1351 case VDEV_AUX_BAD_GUID_SUM:
1352 (void) printf(gettext("missing device"));
1355 case VDEV_AUX_NO_REPLICAS:
1356 (void) printf(gettext("insufficient replicas"));
1359 case VDEV_AUX_VERSION_NEWER:
1360 (void) printf(gettext("newer version"));
1363 case VDEV_AUX_ERR_EXCEEDED:
1364 (void) printf(gettext("too many errors"));
1368 (void) printf(gettext("corrupted data"));
1372 (void) printf("\n");
1374 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1375 &child, &children) != 0)
1378 for (c = 0; c < children; c++) {
1379 uint64_t is_log = B_FALSE;
1381 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1386 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1387 print_import_config(vname, child[c], namewidth, depth + 2);
1391 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1392 &child, &children) == 0) {
1393 (void) printf(gettext("\tcache\n"));
1394 for (c = 0; c < children; c++) {
1395 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1396 (void) printf("\t %s\n", vname);
1401 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1402 &child, &children) == 0) {
1403 (void) printf(gettext("\tspares\n"));
1404 for (c = 0; c < children; c++) {
1405 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1406 (void) printf("\t %s\n", vname);
1414 * Logs are recorded as top level vdevs in the main pool child array
1415 * but with "is_log" set to 1. We use either print_status_config() or
1416 * print_import_config() to print the top level logs then any log
1417 * children (eg mirrored slogs) are printed recursively - which
1418 * works because only the top level vdev is marked "is_log"
1421 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1426 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1430 (void) printf(gettext("\tlogs\n"));
1432 for (c = 0; c < children; c++) {
1433 uint64_t is_log = B_FALSE;
1436 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1440 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1442 print_status_config(zhp, name, child[c], namewidth,
1445 print_import_config(name, child[c], namewidth, 2);
1451 * Display the status for the given pool.
1454 show_import(nvlist_t *config)
1456 uint64_t pool_state;
1468 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1470 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1472 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1474 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1477 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1478 (uint64_t **)&vs, &vsc) == 0);
1479 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1481 reason = zpool_import_status(config, &msgid);
1483 (void) printf(gettext(" pool: %s\n"), name);
1484 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1485 (void) printf(gettext(" state: %s"), health);
1486 if (pool_state == POOL_STATE_DESTROYED)
1487 (void) printf(gettext(" (DESTROYED)"));
1488 (void) printf("\n");
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(" status: One or more devices are "
1495 "missing from the system.\n"));
1498 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1499 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1500 (void) printf(gettext(" status: One or more devices contains "
1501 "corrupted data.\n"));
1504 case ZPOOL_STATUS_CORRUPT_DATA:
1506 gettext(" status: The pool data is corrupted.\n"));
1509 case ZPOOL_STATUS_OFFLINE_DEV:
1510 (void) printf(gettext(" status: One or more devices "
1511 "are offlined.\n"));
1514 case ZPOOL_STATUS_CORRUPT_POOL:
1515 (void) printf(gettext(" status: The pool metadata is "
1519 case ZPOOL_STATUS_VERSION_OLDER:
1520 (void) printf(gettext(" status: The pool is formatted using an "
1521 "older on-disk version.\n"));
1524 case ZPOOL_STATUS_VERSION_NEWER:
1525 (void) printf(gettext(" status: The pool is formatted using an "
1526 "incompatible version.\n"));
1529 case ZPOOL_STATUS_HOSTID_MISMATCH:
1530 (void) printf(gettext(" status: The pool was last accessed by "
1531 "another system.\n"));
1534 case ZPOOL_STATUS_FAULTED_DEV_R:
1535 case ZPOOL_STATUS_FAULTED_DEV_NR:
1536 (void) printf(gettext(" status: One or more devices are "
1540 case ZPOOL_STATUS_BAD_LOG:
1541 (void) printf(gettext(" status: An intent log record cannot be "
1545 case ZPOOL_STATUS_RESILVERING:
1546 (void) printf(gettext(" status: One or more devices were being "
1552 * No other status can be seen when importing pools.
1554 assert(reason == ZPOOL_STATUS_OK);
1558 * Print out an action according to the overall state of the pool.
1560 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1561 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1562 (void) printf(gettext(" action: The pool can be "
1563 "imported using its name or numeric identifier, "
1564 "though\n\tsome features will not be available "
1565 "without an explicit 'zpool upgrade'.\n"));
1566 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1567 (void) printf(gettext(" action: The pool can be "
1568 "imported using its name or numeric "
1569 "identifier and\n\tthe '-f' flag.\n"));
1571 (void) printf(gettext(" action: The pool can be "
1572 "imported using its name or numeric "
1574 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1575 (void) printf(gettext(" action: The pool can be imported "
1576 "despite missing or damaged devices. The\n\tfault "
1577 "tolerance of the pool may be compromised if imported.\n"));
1580 case ZPOOL_STATUS_VERSION_NEWER:
1581 (void) printf(gettext(" action: The pool cannot be "
1582 "imported. Access the pool on a system running "
1583 "newer\n\tsoftware, or recreate the pool from "
1586 case ZPOOL_STATUS_MISSING_DEV_R:
1587 case ZPOOL_STATUS_MISSING_DEV_NR:
1588 case ZPOOL_STATUS_BAD_GUID_SUM:
1589 (void) printf(gettext(" action: The pool cannot be "
1590 "imported. Attach the missing\n\tdevices and try "
1594 (void) printf(gettext(" action: The pool cannot be "
1595 "imported due to damaged devices or data.\n"));
1599 /* Print the comment attached to the pool. */
1600 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1601 (void) printf(gettext("comment: %s\n"), comment);
1604 * If the state is "closed" or "can't open", and the aux state
1605 * is "corrupt data":
1607 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1608 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1609 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1610 if (pool_state == POOL_STATE_DESTROYED)
1611 (void) printf(gettext("\tThe pool was destroyed, "
1612 "but can be imported using the '-Df' flags.\n"));
1613 else if (pool_state != POOL_STATE_EXPORTED)
1614 (void) printf(gettext("\tThe pool may be active on "
1615 "another system, but can be imported using\n\t"
1616 "the '-f' flag.\n"));
1620 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1623 (void) printf(gettext(" config:\n\n"));
1625 namewidth = max_width(NULL, nvroot, 0, 0);
1629 print_import_config(name, nvroot, namewidth, 0);
1630 if (num_logs(nvroot) > 0)
1631 print_logs(NULL, nvroot, namewidth, B_FALSE);
1633 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1634 (void) printf(gettext("\n\tAdditional devices are known to "
1635 "be part of this pool, though their\n\texact "
1636 "configuration cannot be determined.\n"));
1641 * Perform the import for the given configuration. This passes the heavy
1642 * lifting off to zpool_import_props(), and then mounts the datasets contained
1646 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1647 nvlist_t *props, int flags)
1649 zpool_handle_t *zhp;
1654 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1657 verify(nvlist_lookup_uint64(config,
1658 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1659 verify(nvlist_lookup_uint64(config,
1660 ZPOOL_CONFIG_VERSION, &version) == 0);
1661 if (version > SPA_VERSION) {
1662 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1663 "is formatted using a newer ZFS version\n"), name);
1665 } else if (state != POOL_STATE_EXPORTED &&
1666 !(flags & ZFS_IMPORT_ANY_HOST)) {
1669 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1671 if ((unsigned long)hostid != gethostid()) {
1676 verify(nvlist_lookup_string(config,
1677 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1678 verify(nvlist_lookup_uint64(config,
1679 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1681 (void) fprintf(stderr, gettext("cannot import "
1682 "'%s': pool may be in use from other "
1683 "system, it was last accessed by %s "
1684 "(hostid: 0x%lx) on %s"), name, hostname,
1685 (unsigned long)hostid,
1686 asctime(localtime(&t)));
1687 (void) fprintf(stderr, gettext("use '-f' to "
1688 "import anyway\n"));
1692 (void) fprintf(stderr, gettext("cannot import '%s': "
1693 "pool may be in use from other system\n"), name);
1694 (void) fprintf(stderr, gettext("use '-f' to import "
1700 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1703 if (newname != NULL)
1704 name = (char *)newname;
1706 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1709 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1710 !(flags & ZFS_IMPORT_ONLY) &&
1711 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1721 * zpool import [-d dir] [-D]
1722 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1723 * [-d dir | -c cachefile] [-f] -a
1724 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1725 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1727 * -c Read pool information from a cachefile instead of searching
1730 * -d Scan in a specific directory, other than /dev/dsk. More than
1731 * one directory can be specified using multiple '-d' options.
1733 * -D Scan for previously destroyed pools or import all or only
1734 * specified destroyed pools.
1736 * -R Temporarily import the pool, with all mountpoints relative to
1737 * the given root. The pool will remain exported when the machine
1740 * -V Import even in the presence of faulted vdevs. This is an
1741 * intentionally undocumented option for testing purposes, and
1742 * treats the pool configuration as complete, leaving any bad
1743 * vdevs in the FAULTED state. In other words, it does verbatim
1746 * -f Force import, even if it appears that the pool is active.
1748 * -F Attempt rewind if necessary.
1750 * -n See if rewind would work, but don't actually rewind.
1752 * -N Import the pool but don't mount datasets.
1754 * -T Specify a starting txg to use for import. This option is
1755 * intentionally undocumented option for testing purposes.
1757 * -a Import all pools found.
1759 * -o Set property=value and/or temporary mount options (without '=').
1761 * The import command scans for pools to import, and import pools based on pool
1762 * name and GUID. The pool can also be renamed as part of the import process.
1765 zpool_do_import(int argc, char **argv)
1767 char **searchdirs = NULL;
1771 nvlist_t *pools = NULL;
1772 boolean_t do_all = B_FALSE;
1773 boolean_t do_destroyed = B_FALSE;
1774 char *mntopts = NULL;
1777 uint64_t searchguid = 0;
1778 char *searchname = NULL;
1780 nvlist_t *found_config;
1781 nvlist_t *policy = NULL;
1782 nvlist_t *props = NULL;
1784 int flags = ZFS_IMPORT_NORMAL;
1785 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1786 boolean_t dryrun = B_FALSE;
1787 boolean_t do_rewind = B_FALSE;
1788 boolean_t xtreme_rewind = B_FALSE;
1789 uint64_t pool_state, txg = -1ULL;
1790 char *cachefile = NULL;
1791 importargs_t idata = { 0 };
1795 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1804 if (searchdirs == NULL) {
1805 searchdirs = safe_malloc(sizeof (char *));
1807 char **tmp = safe_malloc((nsearch + 1) *
1809 bcopy(searchdirs, tmp, nsearch *
1814 searchdirs[nsearch++] = optarg;
1817 do_destroyed = B_TRUE;
1820 flags |= ZFS_IMPORT_ANY_HOST;
1826 flags |= ZFS_IMPORT_MISSING_LOG;
1832 flags |= ZFS_IMPORT_ONLY;
1835 if ((propval = strchr(optarg, '=')) != NULL) {
1838 if (add_prop_list(optarg, propval,
1846 if (add_prop_list(zpool_prop_to_name(
1847 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1849 if (nvlist_lookup_string(props,
1850 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1853 if (add_prop_list(zpool_prop_to_name(
1854 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1859 txg = strtoull(optarg, &endptr, 10);
1860 if (errno != 0 || *endptr != '\0') {
1861 (void) fprintf(stderr,
1862 gettext("invalid txg value\n"));
1865 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1868 flags |= ZFS_IMPORT_VERBATIM;
1871 xtreme_rewind = B_TRUE;
1874 (void) fprintf(stderr, gettext("missing argument for "
1875 "'%c' option\n"), optopt);
1879 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1888 if (cachefile && nsearch != 0) {
1889 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1893 if ((dryrun || xtreme_rewind) && !do_rewind) {
1894 (void) fprintf(stderr,
1895 gettext("-n or -X only meaningful with -F\n"));
1899 rewind_policy = ZPOOL_TRY_REWIND;
1901 rewind_policy = ZPOOL_DO_REWIND;
1903 rewind_policy |= ZPOOL_EXTREME_REWIND;
1905 /* In the future, we can capture further policy and include it here */
1906 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1907 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1908 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1911 if (searchdirs == NULL) {
1912 searchdirs = safe_malloc(sizeof (char *));
1913 searchdirs[0] = "/dev";
1917 /* check argument count */
1920 (void) fprintf(stderr, gettext("too many arguments\n"));
1925 (void) fprintf(stderr, gettext("too many arguments\n"));
1930 * Check for the SYS_CONFIG privilege. We do this explicitly
1931 * here because otherwise any attempt to discover pools will
1934 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1935 (void) fprintf(stderr, gettext("cannot "
1936 "discover pools: permission denied\n"));
1938 nvlist_free(policy);
1944 * Depending on the arguments given, we do one of the following:
1946 * <none> Iterate through all pools and display information about
1949 * -a Iterate through all pools and try to import each one.
1951 * <id> Find the pool that corresponds to the given GUID/pool
1952 * name and import that one.
1954 * -D Above options applies only to destroyed pools.
1960 searchguid = strtoull(argv[0], &endptr, 10);
1961 if (errno != 0 || *endptr != '\0')
1962 searchname = argv[0];
1963 found_config = NULL;
1966 * User specified a name or guid. Ensure it's unique.
1968 idata.unique = B_TRUE;
1972 idata.path = searchdirs;
1973 idata.paths = nsearch;
1974 idata.poolname = searchname;
1975 idata.guid = searchguid;
1976 idata.cachefile = cachefile;
1978 pools = zpool_search_import(g_zfs, &idata);
1980 if (pools != NULL && idata.exists &&
1981 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1982 (void) fprintf(stderr, gettext("cannot import '%s': "
1983 "a pool with that name already exists\n"),
1985 (void) fprintf(stderr, gettext("use the form '%s "
1986 "<pool | id> <newpool>' to give it a new name\n"),
1989 } else if (pools == NULL && idata.exists) {
1990 (void) fprintf(stderr, gettext("cannot import '%s': "
1991 "a pool with that name is already created/imported,\n"),
1993 (void) fprintf(stderr, gettext("and no additional pools "
1994 "with that name were found\n"));
1996 } else if (pools == NULL) {
1998 (void) fprintf(stderr, gettext("cannot import '%s': "
1999 "no such pool available\n"), argv[0]);
2006 nvlist_free(policy);
2011 * At this point we have a list of import candidate configs. Even if
2012 * we were searching by pool name or guid, we still need to
2013 * post-process the list to deal with pool state and possible
2019 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2021 verify(nvpair_value_nvlist(elem, &config) == 0);
2023 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2025 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2027 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2030 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2037 (void) printf("\n");
2040 err |= do_import(config, NULL, mntopts,
2043 show_import(config);
2045 } else if (searchname != NULL) {
2049 * We are searching for a pool based on name.
2051 verify(nvlist_lookup_string(config,
2052 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2054 if (strcmp(name, searchname) == 0) {
2055 if (found_config != NULL) {
2056 (void) fprintf(stderr, gettext(
2057 "cannot import '%s': more than "
2058 "one matching pool\n"), searchname);
2059 (void) fprintf(stderr, gettext(
2060 "import by numeric ID instead\n"));
2063 found_config = config;
2069 * Search for a pool by guid.
2071 verify(nvlist_lookup_uint64(config,
2072 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2074 if (guid == searchguid)
2075 found_config = config;
2080 * If we were searching for a specific pool, verify that we found a
2081 * pool, and then do the import.
2083 if (argc != 0 && err == 0) {
2084 if (found_config == NULL) {
2085 (void) fprintf(stderr, gettext("cannot import '%s': "
2086 "no such pool available\n"), argv[0]);
2089 err |= do_import(found_config, argc == 1 ? NULL :
2090 argv[1], mntopts, props, flags);
2095 * If we were just looking for pools, report an error if none were
2098 if (argc == 0 && first)
2099 (void) fprintf(stderr,
2100 gettext("no pools available to import\n"));
2105 nvlist_free(policy);
2108 return (err ? 1 : 0);
2111 typedef struct iostat_cbdata {
2112 zpool_list_t *cb_list;
2119 print_iostat_separator(iostat_cbdata_t *cb)
2123 for (i = 0; i < cb->cb_namewidth; i++)
2125 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2129 print_iostat_header(iostat_cbdata_t *cb)
2131 (void) printf("%*s capacity operations bandwidth\n",
2132 cb->cb_namewidth, "");
2133 (void) printf("%-*s alloc free read write read write\n",
2134 cb->cb_namewidth, "pool");
2135 print_iostat_separator(cb);
2139 * Display a single statistic.
2142 print_one_stat(uint64_t value)
2146 zfs_nicenum(value, buf, sizeof (buf));
2147 (void) printf(" %5s", buf);
2151 * Print out all the statistics for the given vdev. This can either be the
2152 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2153 * is a verbose output, and we don't want to display the toplevel pool stats.
2156 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2157 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2159 nvlist_t **oldchild, **newchild;
2161 vdev_stat_t *oldvs, *newvs;
2162 vdev_stat_t zerovs = { 0 };
2167 if (oldnv != NULL) {
2168 verify(nvlist_lookup_uint64_array(oldnv,
2169 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2174 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2175 (uint64_t **)&newvs, &c) == 0);
2177 if (strlen(name) + depth > cb->cb_namewidth)
2178 (void) printf("%*s%s", depth, "", name);
2180 (void) printf("%*s%s%*s", depth, "", name,
2181 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2183 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2188 scale = (double)NANOSEC / tdelta;
2190 /* only toplevel vdevs have capacity stats */
2191 if (newvs->vs_space == 0) {
2192 (void) printf(" - -");
2194 print_one_stat(newvs->vs_alloc);
2195 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2198 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2199 oldvs->vs_ops[ZIO_TYPE_READ])));
2201 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2202 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2204 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2205 oldvs->vs_bytes[ZIO_TYPE_READ])));
2207 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2208 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2210 (void) printf("\n");
2212 if (!cb->cb_verbose)
2215 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2216 &newchild, &children) != 0)
2219 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2220 &oldchild, &c) != 0)
2223 for (c = 0; c < children; c++) {
2224 uint64_t ishole = B_FALSE, islog = B_FALSE;
2226 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2229 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2232 if (ishole || islog)
2235 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2236 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2237 newchild[c], cb, depth + 2);
2242 * Log device section
2245 if (num_logs(newnv) > 0) {
2246 (void) printf("%-*s - - - - - "
2247 "-\n", cb->cb_namewidth, "logs");
2249 for (c = 0; c < children; c++) {
2250 uint64_t islog = B_FALSE;
2251 (void) nvlist_lookup_uint64(newchild[c],
2252 ZPOOL_CONFIG_IS_LOG, &islog);
2255 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2257 print_vdev_stats(zhp, vname, oldnv ?
2258 oldchild[c] : NULL, newchild[c],
2267 * Include level 2 ARC devices in iostat output
2269 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2270 &newchild, &children) != 0)
2273 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2274 &oldchild, &c) != 0)
2278 (void) printf("%-*s - - - - - "
2279 "-\n", cb->cb_namewidth, "cache");
2280 for (c = 0; c < children; c++) {
2281 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2283 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2284 newchild[c], cb, depth + 2);
2291 refresh_iostat(zpool_handle_t *zhp, void *data)
2293 iostat_cbdata_t *cb = data;
2297 * If the pool has disappeared, remove it from the list and continue.
2299 if (zpool_refresh_stats(zhp, &missing) != 0)
2303 pool_list_remove(cb->cb_list, zhp);
2309 * Callback to print out the iostats for the given pool.
2312 print_iostat(zpool_handle_t *zhp, void *data)
2314 iostat_cbdata_t *cb = data;
2315 nvlist_t *oldconfig, *newconfig;
2316 nvlist_t *oldnvroot, *newnvroot;
2318 newconfig = zpool_get_config(zhp, &oldconfig);
2320 if (cb->cb_iteration == 1)
2323 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2326 if (oldconfig == NULL)
2329 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2333 * Print out the statistics for the pool.
2335 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2338 print_iostat_separator(cb);
2344 get_namewidth(zpool_handle_t *zhp, void *data)
2346 iostat_cbdata_t *cb = data;
2347 nvlist_t *config, *nvroot;
2349 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2350 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2352 if (!cb->cb_verbose)
2353 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2355 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2360 * The width must fall into the range [10,38]. The upper limit is the
2361 * maximum we can have and still fit in 80 columns.
2363 if (cb->cb_namewidth < 10)
2364 cb->cb_namewidth = 10;
2365 if (cb->cb_namewidth > 38)
2366 cb->cb_namewidth = 38;
2372 * Parse the input string, get the 'interval' and 'count' value if there is one.
2375 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2378 unsigned long interval = 0, count = 0;
2379 int argc = *argcp, errno;
2382 * Determine if the last argument is an integer or a pool name
2384 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2388 interval = strtoul(argv[argc - 1], &end, 10);
2390 if (*end == '\0' && errno == 0) {
2391 if (interval == 0) {
2392 (void) fprintf(stderr, gettext("interval "
2393 "cannot be zero\n"));
2397 * Ignore the last parameter
2402 * If this is not a valid number, just plow on. The
2403 * user will get a more informative error message later
2411 * If the last argument is also an integer, then we have both a count
2414 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2419 interval = strtoul(argv[argc - 1], &end, 10);
2421 if (*end == '\0' && errno == 0) {
2422 if (interval == 0) {
2423 (void) fprintf(stderr, gettext("interval "
2424 "cannot be zero\n"));
2429 * Ignore the last parameter
2443 get_timestamp_arg(char c)
2446 timestamp_fmt = UDATE;
2448 timestamp_fmt = DDATE;
2454 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2456 * -v Display statistics for individual vdevs
2457 * -T Display a timestamp in date(1) or Unix format
2459 * This command can be tricky because we want to be able to deal with pool
2460 * creation/destruction as well as vdev configuration changes. The bulk of this
2461 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2462 * on pool_list_update() to detect the addition of new pools. Configuration
2463 * changes are all handled within libzfs.
2466 zpool_do_iostat(int argc, char **argv)
2471 unsigned long interval = 0, count = 0;
2473 boolean_t verbose = B_FALSE;
2477 while ((c = getopt(argc, argv, "T:v")) != -1) {
2480 get_timestamp_arg(*optarg);
2486 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2495 get_interval_count(&argc, argv, &interval, &count);
2498 * Construct the list of all interesting pools.
2501 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2504 if (pool_list_count(list) == 0 && argc != 0) {
2505 pool_list_free(list);
2509 if (pool_list_count(list) == 0 && interval == 0) {
2510 pool_list_free(list);
2511 (void) fprintf(stderr, gettext("no pools available\n"));
2516 * Enter the main iostat loop.
2519 cb.cb_verbose = verbose;
2520 cb.cb_iteration = 0;
2521 cb.cb_namewidth = 0;
2524 pool_list_update(list);
2526 if ((npools = pool_list_count(list)) == 0)
2530 * Refresh all statistics. This is done as an explicit step
2531 * before calculating the maximum name width, so that any
2532 * configuration changes are properly accounted for.
2534 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2537 * Iterate over all pools to determine the maximum width
2538 * for the pool / device name column across all pools.
2540 cb.cb_namewidth = 0;
2541 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2543 if (timestamp_fmt != NODATE)
2544 print_timestamp(timestamp_fmt);
2547 * If it's the first time, or verbose mode, print the header.
2549 if (++cb.cb_iteration == 1 || verbose)
2550 print_iostat_header(&cb);
2552 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2555 * If there's more than one pool, and we're not in verbose mode
2556 * (which prints a separator for us), then print a separator.
2558 if (npools > 1 && !verbose)
2559 print_iostat_separator(&cb);
2562 (void) printf("\n");
2565 * Flush the output so that redirection to a file isn't buffered
2568 (void) fflush(stdout);
2573 if (count != 0 && --count == 0)
2576 (void) sleep(interval);
2579 pool_list_free(list);
2584 typedef struct list_cbdata {
2585 boolean_t cb_scripted;
2587 zprop_list_t *cb_proplist;
2591 * Given a list of columns to display, output appropriate headers for each one.
2594 print_header(zprop_list_t *pl)
2597 boolean_t first = B_TRUE;
2598 boolean_t right_justify;
2600 for (; pl != NULL; pl = pl->pl_next) {
2601 if (pl->pl_prop == ZPROP_INVAL)
2609 header = zpool_prop_column_name(pl->pl_prop);
2610 right_justify = zpool_prop_align_right(pl->pl_prop);
2612 if (pl->pl_next == NULL && !right_justify)
2613 (void) printf("%s", header);
2614 else if (right_justify)
2615 (void) printf("%*s", pl->pl_width, header);
2617 (void) printf("%-*s", pl->pl_width, header);
2620 (void) printf("\n");
2624 * Given a pool and a list of properties, print out all the properties according
2625 * to the described layout.
2628 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2630 boolean_t first = B_TRUE;
2631 char property[ZPOOL_MAXPROPLEN];
2633 boolean_t right_justify;
2636 for (; pl != NULL; pl = pl->pl_next) {
2639 (void) printf("\t");
2646 right_justify = B_FALSE;
2647 if (pl->pl_prop != ZPROP_INVAL) {
2648 if (zpool_get_prop(zhp, pl->pl_prop, property,
2649 sizeof (property), NULL) != 0)
2654 right_justify = zpool_prop_align_right(pl->pl_prop);
2659 width = pl->pl_width;
2662 * If this is being called in scripted mode, or if this is the
2663 * last column and it is left-justified, don't include a width
2666 if (scripted || (pl->pl_next == NULL && !right_justify))
2667 (void) printf("%s", propstr);
2668 else if (right_justify)
2669 (void) printf("%*s", width, propstr);
2671 (void) printf("%-*s", width, propstr);
2674 (void) printf("\n");
2678 * Generic callback function to list a pool.
2681 list_callback(zpool_handle_t *zhp, void *data)
2683 list_cbdata_t *cbp = data;
2685 if (cbp->cb_first) {
2686 if (!cbp->cb_scripted)
2687 print_header(cbp->cb_proplist);
2688 cbp->cb_first = B_FALSE;
2691 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2697 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2699 * -H Scripted mode. Don't display headers, and separate properties
2701 * -o List of properties to display. Defaults to
2702 * "name,size,allocated,free,capacity,health,altroot"
2703 * -T Display a timestamp in date(1) or Unix format
2705 * List all pools in the system, whether or not they're healthy. Output space
2706 * statistics for each one, as well as health status summary.
2709 zpool_do_list(int argc, char **argv)
2713 list_cbdata_t cb = { 0 };
2714 static char default_props[] =
2715 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2716 char *props = default_props;
2717 unsigned long interval = 0, count = 0;
2720 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2723 cb.cb_scripted = B_TRUE;
2729 get_timestamp_arg(*optarg);
2732 (void) fprintf(stderr, gettext("missing argument for "
2733 "'%c' option\n"), optopt);
2737 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2746 get_interval_count(&argc, argv, &interval, &count);
2748 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2751 cb.cb_first = B_TRUE;
2755 if (timestamp_fmt != NODATE)
2756 print_timestamp(timestamp_fmt);
2758 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2759 list_callback, &cb);
2761 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2762 (void) printf(gettext("no pools available\n"));
2763 zprop_free_list(cb.cb_proplist);
2770 if (count != 0 && --count == 0)
2773 (void) sleep(interval);
2776 zprop_free_list(cb.cb_proplist);
2781 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2788 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2789 &child, &children) != 0) {
2790 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2791 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2792 name += sizeof(_PATH_DEV) - 1;
2793 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2794 path += sizeof(_PATH_DEV) - 1;
2795 if (strcmp(name, path) == 0)
2800 for (c = 0; c < children; c++)
2801 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2808 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2810 boolean_t force = B_FALSE;
2813 char *poolname, *old_disk, *new_disk;
2814 zpool_handle_t *zhp;
2818 while ((c = getopt(argc, argv, "f")) != -1) {
2824 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2833 /* get pool name and check number of arguments */
2835 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2842 (void) fprintf(stderr,
2843 gettext("missing <device> specification\n"));
2851 (void) fprintf(stderr,
2852 gettext("missing <new_device> specification\n"));
2855 new_disk = old_disk;
2865 (void) fprintf(stderr, gettext("too many arguments\n"));
2869 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2872 if (zpool_get_config(zhp, NULL) == NULL) {
2873 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2879 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2881 if (nvroot == NULL) {
2886 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2888 nvlist_free(nvroot);
2895 * zpool replace [-f] <pool> <device> <new_device>
2897 * -f Force attach, even if <new_device> appears to be in use.
2899 * Replace <device> with <new_device>.
2903 zpool_do_replace(int argc, char **argv)
2905 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2909 * zpool attach [-f] <pool> <device> <new_device>
2911 * -f Force attach, even if <new_device> appears to be in use.
2913 * Attach <new_device> to the mirror containing <device>. If <device> is not
2914 * part of a mirror, then <device> will be transformed into a mirror of
2915 * <device> and <new_device>. In either case, <new_device> will begin life
2916 * with a DTL of [0, now], and will immediately begin to resilver itself.
2919 zpool_do_attach(int argc, char **argv)
2921 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2925 * zpool detach [-f] <pool> <device>
2927 * -f Force detach of <device>, even if DTLs argue against it
2928 * (not supported yet)
2930 * Detach a device from a mirror. The operation will be refused if <device>
2931 * is the last device in the mirror, or if the DTLs indicate that this device
2932 * has the only valid copy of some data.
2936 zpool_do_detach(int argc, char **argv)
2939 char *poolname, *path;
2940 zpool_handle_t *zhp;
2944 while ((c = getopt(argc, argv, "f")) != -1) {
2948 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2957 /* get pool name and check number of arguments */
2959 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2964 (void) fprintf(stderr,
2965 gettext("missing <device> specification\n"));
2972 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2975 ret = zpool_vdev_detach(zhp, path);
2983 * zpool split [-n] [-o prop=val] ...
2985 * [-R altroot] <pool> <newpool> [<device> ...]
2987 * -n Do not split the pool, but display the resulting layout if
2988 * it were to be split.
2989 * -o Set property=value, or set mount options.
2990 * -R Mount the split-off pool under an alternate root.
2992 * Splits the named pool and gives it the new pool name. Devices to be split
2993 * off may be listed, provided that no more than one device is specified
2994 * per top-level vdev mirror. The newly split pool is left in an exported
2995 * state unless -R is specified.
2997 * Restrictions: the top-level of the pool pool must only be made up of
2998 * mirrors; all devices in the pool must be healthy; no device may be
2999 * undergoing a resilvering operation.
3002 zpool_do_split(int argc, char **argv)
3004 char *srcpool, *newpool, *propval;
3005 char *mntopts = NULL;
3008 zpool_handle_t *zhp;
3009 nvlist_t *config, *props = NULL;
3011 flags.dryrun = B_FALSE;
3012 flags.import = B_FALSE;
3015 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3018 flags.import = B_TRUE;
3020 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3021 &props, B_TRUE) != 0) {
3028 flags.dryrun = B_TRUE;
3031 if ((propval = strchr(optarg, '=')) != NULL) {
3034 if (add_prop_list(optarg, propval,
3035 &props, B_TRUE) != 0) {
3045 (void) fprintf(stderr, gettext("missing argument for "
3046 "'%c' option\n"), optopt);
3050 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3057 if (!flags.import && mntopts != NULL) {
3058 (void) fprintf(stderr, gettext("setting mntopts is only "
3059 "valid when importing the pool\n"));
3067 (void) fprintf(stderr, gettext("Missing pool name\n"));
3071 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3081 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3084 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3085 if (config == NULL) {
3089 (void) printf(gettext("would create '%s' with the "
3090 "following layout:\n\n"), newpool);
3091 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3093 nvlist_free(config);
3098 if (ret != 0 || flags.dryrun || !flags.import)
3102 * The split was successful. Now we need to open the new
3103 * pool and import it.
3105 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3107 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3108 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3110 (void) fprintf(stderr, gettext("Split was succssful, but "
3111 "the datasets could not all be mounted\n"));
3112 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3113 "different altroot\n"), "zpool import");
3123 * zpool online <pool> <device> ...
3126 zpool_do_online(int argc, char **argv)
3130 zpool_handle_t *zhp;
3132 vdev_state_t newstate;
3136 while ((c = getopt(argc, argv, "et")) != -1) {
3139 flags |= ZFS_ONLINE_EXPAND;
3143 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3152 /* get pool name and check number of arguments */
3154 (void) fprintf(stderr, gettext("missing pool name\n"));
3158 (void) fprintf(stderr, gettext("missing device name\n"));
3164 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3167 for (i = 1; i < argc; i++) {
3168 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3169 if (newstate != VDEV_STATE_HEALTHY) {
3170 (void) printf(gettext("warning: device '%s' "
3171 "onlined, but remains in faulted state\n"),
3173 if (newstate == VDEV_STATE_FAULTED)
3174 (void) printf(gettext("use 'zpool "
3175 "clear' to restore a faulted "
3178 (void) printf(gettext("use 'zpool "
3179 "replace' to replace devices "
3180 "that are no longer present\n"));
3193 * zpool offline [-ft] <pool> <device> ...
3195 * -f Force the device into the offline state, even if doing
3196 * so would appear to compromise pool availability.
3197 * (not supported yet)
3199 * -t Only take the device off-line temporarily. The offline
3200 * state will not be persistent across reboots.
3204 zpool_do_offline(int argc, char **argv)
3208 zpool_handle_t *zhp;
3210 boolean_t istmp = B_FALSE;
3213 while ((c = getopt(argc, argv, "ft")) != -1) {
3220 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3229 /* get pool name and check number of arguments */
3231 (void) fprintf(stderr, gettext("missing pool name\n"));
3235 (void) fprintf(stderr, gettext("missing device name\n"));
3241 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3244 for (i = 1; i < argc; i++) {
3245 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3255 * zpool clear <pool> [device]
3257 * Clear all errors associated with a pool or a particular device.
3260 zpool_do_clear(int argc, char **argv)
3264 boolean_t dryrun = B_FALSE;
3265 boolean_t do_rewind = B_FALSE;
3266 boolean_t xtreme_rewind = B_FALSE;
3267 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3268 nvlist_t *policy = NULL;
3269 zpool_handle_t *zhp;
3270 char *pool, *device;
3273 while ((c = getopt(argc, argv, "FnX")) != -1) {
3282 xtreme_rewind = B_TRUE;
3285 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3295 (void) fprintf(stderr, gettext("missing pool name\n"));
3300 (void) fprintf(stderr, gettext("too many arguments\n"));
3304 if ((dryrun || xtreme_rewind) && !do_rewind) {
3305 (void) fprintf(stderr,
3306 gettext("-n or -X only meaningful with -F\n"));
3310 rewind_policy = ZPOOL_TRY_REWIND;
3312 rewind_policy = ZPOOL_DO_REWIND;
3314 rewind_policy |= ZPOOL_EXTREME_REWIND;
3316 /* In future, further rewind policy choices can be passed along here */
3317 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3318 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3322 device = argc == 2 ? argv[1] : NULL;
3324 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3325 nvlist_free(policy);
3329 if (zpool_clear(zhp, device, policy) != 0)
3334 nvlist_free(policy);
3340 * zpool reguid <pool>
3343 zpool_do_reguid(int argc, char **argv)
3347 zpool_handle_t *zhp;
3351 while ((c = getopt(argc, argv, "")) != -1) {
3354 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3363 /* get pool name and check number of arguments */
3365 (void) fprintf(stderr, gettext("missing pool name\n"));
3370 (void) fprintf(stderr, gettext("too many arguments\n"));
3375 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3378 ret = zpool_reguid(zhp);
3385 typedef struct scrub_cbdata {
3392 scrub_callback(zpool_handle_t *zhp, void *data)
3394 scrub_cbdata_t *cb = data;
3398 * Ignore faulted pools.
3400 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3401 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3402 "currently unavailable\n"), zpool_get_name(zhp));
3406 err = zpool_scan(zhp, cb->cb_type);
3412 * zpool scrub [-s] <pool> ...
3414 * -s Stop. Stops any in-progress scrub.
3417 zpool_do_scrub(int argc, char **argv)
3422 cb.cb_type = POOL_SCAN_SCRUB;
3425 while ((c = getopt(argc, argv, "s")) != -1) {
3428 cb.cb_type = POOL_SCAN_NONE;
3431 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3443 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3447 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3450 typedef struct status_cbdata {
3452 boolean_t cb_allpools;
3453 boolean_t cb_verbose;
3454 boolean_t cb_explain;
3456 boolean_t cb_dedup_stats;
3460 * Print out detailed scrub status.
3463 print_scan_status(pool_scan_stat_t *ps)
3466 uint64_t elapsed, mins_left, hours_left;
3467 uint64_t pass_exam, examined, total;
3469 double fraction_done;
3470 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3472 (void) printf(gettext(" scan: "));
3474 /* If there's never been a scan, there's not much to say. */
3475 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3476 ps->pss_func >= POOL_SCAN_FUNCS) {
3477 (void) printf(gettext("none requested\n"));
3481 start = ps->pss_start_time;
3482 end = ps->pss_end_time;
3483 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3485 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3486 ps->pss_func == POOL_SCAN_RESILVER);
3488 * Scan is finished or canceled.
3490 if (ps->pss_state == DSS_FINISHED) {
3491 uint64_t minutes_taken = (end - start) / 60;
3494 if (ps->pss_func == POOL_SCAN_SCRUB) {
3495 fmt = gettext("scrub repaired %s in %lluh%um with "
3496 "%llu errors on %s");
3497 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3498 fmt = gettext("resilvered %s in %lluh%um with "
3499 "%llu errors on %s");
3502 (void) printf(fmt, processed_buf,
3503 (u_longlong_t)(minutes_taken / 60),
3504 (uint_t)(minutes_taken % 60),
3505 (u_longlong_t)ps->pss_errors,
3506 ctime((time_t *)&end));
3508 } else if (ps->pss_state == DSS_CANCELED) {
3509 if (ps->pss_func == POOL_SCAN_SCRUB) {
3510 (void) printf(gettext("scrub canceled on %s"),
3512 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3513 (void) printf(gettext("resilver canceled on %s"),
3519 assert(ps->pss_state == DSS_SCANNING);
3522 * Scan is in progress.
3524 if (ps->pss_func == POOL_SCAN_SCRUB) {
3525 (void) printf(gettext("scrub in progress since %s"),
3527 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3528 (void) printf(gettext("resilver in progress since %s"),
3532 examined = ps->pss_examined ? ps->pss_examined : 1;
3533 total = ps->pss_to_examine;
3534 fraction_done = (double)examined / total;
3536 /* elapsed time for this pass */
3537 elapsed = time(NULL) - ps->pss_pass_start;
3538 elapsed = elapsed ? elapsed : 1;
3539 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3540 rate = pass_exam / elapsed;
3541 rate = rate ? rate : 1;
3542 mins_left = ((total - examined) / rate) / 60;
3543 hours_left = mins_left / 60;
3545 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3546 zfs_nicenum(total, total_buf, sizeof (total_buf));
3547 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3550 * do not print estimated time if hours_left is more than 30 days
3552 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3553 examined_buf, total_buf, rate_buf);
3554 if (hours_left < (30 * 24)) {
3555 (void) printf(gettext(", %lluh%um to go\n"),
3556 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3558 (void) printf(gettext(
3559 ", (scan is slow, no estimated time)\n"));
3562 if (ps->pss_func == POOL_SCAN_RESILVER) {
3563 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3564 processed_buf, 100 * fraction_done);
3565 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3566 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3567 processed_buf, 100 * fraction_done);
3572 print_error_log(zpool_handle_t *zhp)
3574 nvlist_t *nverrlist = NULL;
3577 size_t len = MAXPATHLEN * 2;
3579 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3580 (void) printf("errors: List of errors unavailable "
3581 "(insufficient privileges)\n");
3585 (void) printf("errors: Permanent errors have been "
3586 "detected in the following files:\n\n");
3588 pathname = safe_malloc(len);
3590 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3592 uint64_t dsobj, obj;
3594 verify(nvpair_value_nvlist(elem, &nv) == 0);
3595 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3597 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3599 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3600 (void) printf("%7s %s\n", "", pathname);
3603 nvlist_free(nverrlist);
3607 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3616 (void) printf(gettext("\tspares\n"));
3618 for (i = 0; i < nspares; i++) {
3619 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3620 print_status_config(zhp, name, spares[i],
3621 namewidth, 2, B_TRUE);
3627 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3636 (void) printf(gettext("\tcache\n"));
3638 for (i = 0; i < nl2cache; i++) {
3639 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3640 print_status_config(zhp, name, l2cache[i],
3641 namewidth, 2, B_FALSE);
3647 print_dedup_stats(nvlist_t *config)
3649 ddt_histogram_t *ddh;
3655 * If the pool was faulted then we may not have been able to
3656 * obtain the config. Otherwise, if have anything in the dedup
3657 * table continue processing the stats.
3659 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3660 (uint64_t **)&ddo, &c) != 0)
3663 (void) printf("\n");
3664 (void) printf(gettext(" dedup: "));
3665 if (ddo->ddo_count == 0) {
3666 (void) printf(gettext("no DDT entries\n"));
3670 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3671 (u_longlong_t)ddo->ddo_count,
3672 (u_longlong_t)ddo->ddo_dspace,
3673 (u_longlong_t)ddo->ddo_mspace);
3675 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3676 (uint64_t **)&dds, &c) == 0);
3677 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3678 (uint64_t **)&ddh, &c) == 0);
3679 zpool_dump_ddt(dds, ddh);
3683 * Display a summary of pool status. Displays a summary such as:
3687 * reason: One or more devices ...
3688 * see: http://illumos.org/msg/ZFS-xxxx-01
3694 * When given the '-v' option, we print out the complete config. If the '-e'
3695 * option is specified, then we print out error rate information as well.
3698 status_callback(zpool_handle_t *zhp, void *data)
3700 status_cbdata_t *cbp = data;
3701 nvlist_t *config, *nvroot;
3708 config = zpool_get_config(zhp, NULL);
3709 reason = zpool_get_status(zhp, &msgid);
3714 * If we were given 'zpool status -x', only report those pools with
3717 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3718 if (!cbp->cb_allpools) {
3719 (void) printf(gettext("pool '%s' is healthy\n"),
3720 zpool_get_name(zhp));
3722 cbp->cb_first = B_FALSE;
3728 cbp->cb_first = B_FALSE;
3730 (void) printf("\n");
3732 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3734 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3735 (uint64_t **)&vs, &c) == 0);
3736 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3738 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3739 (void) printf(gettext(" state: %s\n"), health);
3742 case ZPOOL_STATUS_MISSING_DEV_R:
3743 (void) printf(gettext("status: One or more devices could not "
3744 "be opened. Sufficient replicas exist for\n\tthe pool to "
3745 "continue functioning in a degraded state.\n"));
3746 (void) printf(gettext("action: Attach the missing device and "
3747 "online it using 'zpool online'.\n"));
3750 case ZPOOL_STATUS_MISSING_DEV_NR:
3751 (void) printf(gettext("status: One or more devices could not "
3752 "be opened. There are insufficient\n\treplicas for the "
3753 "pool to continue functioning.\n"));
3754 (void) printf(gettext("action: Attach the missing device and "
3755 "online it using 'zpool online'.\n"));
3758 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3759 (void) printf(gettext("status: One or more devices could not "
3760 "be used because the label is missing or\n\tinvalid. "
3761 "Sufficient replicas exist for the pool to continue\n\t"
3762 "functioning in a degraded state.\n"));
3763 (void) printf(gettext("action: Replace the device using "
3764 "'zpool replace'.\n"));
3767 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3768 (void) printf(gettext("status: One or more devices could not "
3769 "be used because the label is missing \n\tor invalid. "
3770 "There are insufficient replicas for the pool to "
3771 "continue\n\tfunctioning.\n"));
3772 zpool_explain_recover(zpool_get_handle(zhp),
3773 zpool_get_name(zhp), reason, config);
3776 case ZPOOL_STATUS_FAILING_DEV:
3777 (void) printf(gettext("status: One or more devices has "
3778 "experienced an unrecoverable error. An\n\tattempt was "
3779 "made to correct the error. Applications are "
3781 (void) printf(gettext("action: Determine if the device needs "
3782 "to be replaced, and clear the errors\n\tusing "
3783 "'zpool clear' or replace the device with 'zpool "
3787 case ZPOOL_STATUS_OFFLINE_DEV:
3788 (void) printf(gettext("status: One or more devices has "
3789 "been taken offline by the administrator.\n\tSufficient "
3790 "replicas exist for the pool to continue functioning in "
3791 "a\n\tdegraded state.\n"));
3792 (void) printf(gettext("action: Online the device using "
3793 "'zpool online' or replace the device with\n\t'zpool "
3797 case ZPOOL_STATUS_REMOVED_DEV:
3798 (void) printf(gettext("status: One or more devices has "
3799 "been removed by the administrator.\n\tSufficient "
3800 "replicas exist for the pool to continue functioning in "
3801 "a\n\tdegraded state.\n"));
3802 (void) printf(gettext("action: Online the device using "
3803 "'zpool online' or replace the device with\n\t'zpool "
3807 case ZPOOL_STATUS_RESILVERING:
3808 (void) printf(gettext("status: One or more devices is "
3809 "currently being resilvered. The pool will\n\tcontinue "
3810 "to function, possibly in a degraded state.\n"));
3811 (void) printf(gettext("action: Wait for the resilver to "
3815 case ZPOOL_STATUS_CORRUPT_DATA:
3816 (void) printf(gettext("status: One or more devices has "
3817 "experienced an error resulting in data\n\tcorruption. "
3818 "Applications may be affected.\n"));
3819 (void) printf(gettext("action: Restore the file in question "
3820 "if possible. Otherwise restore the\n\tentire pool from "
3824 case ZPOOL_STATUS_CORRUPT_POOL:
3825 (void) printf(gettext("status: The pool metadata is corrupted "
3826 "and the pool cannot be opened.\n"));
3827 zpool_explain_recover(zpool_get_handle(zhp),
3828 zpool_get_name(zhp), reason, config);
3831 case ZPOOL_STATUS_VERSION_OLDER:
3832 (void) printf(gettext("status: The pool is formatted using an "
3833 "older on-disk format. The pool can\n\tstill be used, but "
3834 "some features are unavailable.\n"));
3835 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3836 "upgrade'. Once this is done, the\n\tpool will no longer "
3837 "be accessible on older software versions.\n"));
3840 case ZPOOL_STATUS_VERSION_NEWER:
3841 (void) printf(gettext("status: The pool has been upgraded to a "
3842 "newer, incompatible on-disk version.\n\tThe pool cannot "
3843 "be accessed on this system.\n"));
3844 (void) printf(gettext("action: Access the pool from a system "
3845 "running more recent software, or\n\trestore the pool from "
3849 case ZPOOL_STATUS_FAULTED_DEV_R:
3850 (void) printf(gettext("status: One or more devices are "
3851 "faulted in response to persistent errors.\n\tSufficient "
3852 "replicas exist for the pool to continue functioning "
3853 "in a\n\tdegraded state.\n"));
3854 (void) printf(gettext("action: Replace the faulted device, "
3855 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3858 case ZPOOL_STATUS_FAULTED_DEV_NR:
3859 (void) printf(gettext("status: One or more devices are "
3860 "faulted in response to persistent errors. There are "
3861 "insufficient replicas for the pool to\n\tcontinue "
3863 (void) printf(gettext("action: Destroy and re-create the pool "
3864 "from a backup source. Manually marking the device\n"
3865 "\trepaired using 'zpool clear' may allow some data "
3866 "to be recovered.\n"));
3869 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3870 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3871 (void) printf(gettext("status: One or more devices are "
3872 "faulted in response to IO failures.\n"));
3873 (void) printf(gettext("action: Make sure the affected devices "
3874 "are connected, then run 'zpool clear'.\n"));
3877 case ZPOOL_STATUS_BAD_LOG:
3878 (void) printf(gettext("status: An intent log record "
3879 "could not be read.\n"
3880 "\tWaiting for adminstrator intervention to fix the "
3881 "faulted pool.\n"));
3882 (void) printf(gettext("action: Either restore the affected "
3883 "device(s) and run 'zpool online',\n"
3884 "\tor ignore the intent log records by running "
3885 "'zpool clear'.\n"));
3890 * The remaining errors can't actually be generated, yet.
3892 assert(reason == ZPOOL_STATUS_OK);
3896 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
3899 if (config != NULL) {
3902 nvlist_t **spares, **l2cache;
3903 uint_t nspares, nl2cache;
3904 pool_scan_stat_t *ps = NULL;
3906 (void) nvlist_lookup_uint64_array(nvroot,
3907 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3908 print_scan_status(ps);
3910 namewidth = max_width(zhp, nvroot, 0, 0);
3914 (void) printf(gettext("config:\n\n"));
3915 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3916 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3917 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3918 namewidth, 0, B_FALSE);
3920 if (num_logs(nvroot) > 0)
3921 print_logs(zhp, nvroot, namewidth, B_TRUE);
3922 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3923 &l2cache, &nl2cache) == 0)
3924 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3926 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3927 &spares, &nspares) == 0)
3928 print_spares(zhp, spares, nspares, namewidth);
3930 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3932 nvlist_t *nverrlist = NULL;
3935 * If the approximate error count is small, get a
3936 * precise count by fetching the entire log and
3937 * uniquifying the results.
3939 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3940 zpool_get_errlog(zhp, &nverrlist) == 0) {
3945 while ((elem = nvlist_next_nvpair(nverrlist,
3950 nvlist_free(nverrlist);
3952 (void) printf("\n");
3955 (void) printf(gettext("errors: No known data "
3957 else if (!cbp->cb_verbose)
3958 (void) printf(gettext("errors: %llu data "
3959 "errors, use '-v' for a list\n"),
3960 (u_longlong_t)nerr);
3962 print_error_log(zhp);
3965 if (cbp->cb_dedup_stats)
3966 print_dedup_stats(config);
3968 (void) printf(gettext("config: The configuration cannot be "
3976 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3978 * -v Display complete error logs
3979 * -x Display only pools with potential problems
3980 * -D Display dedup status (undocumented)
3981 * -T Display a timestamp in date(1) or Unix format
3983 * Describes the health status of all pools or some subset.
3986 zpool_do_status(int argc, char **argv)
3990 unsigned long interval = 0, count = 0;
3991 status_cbdata_t cb = { 0 };
3994 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3997 cb.cb_verbose = B_TRUE;
4000 cb.cb_explain = B_TRUE;
4003 cb.cb_dedup_stats = B_TRUE;
4006 get_timestamp_arg(*optarg);
4009 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4018 get_interval_count(&argc, argv, &interval, &count);
4021 cb.cb_allpools = B_TRUE;
4023 cb.cb_first = B_TRUE;
4026 if (timestamp_fmt != NODATE)
4027 print_timestamp(timestamp_fmt);
4029 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4030 status_callback, &cb);
4032 if (argc == 0 && cb.cb_count == 0)
4033 (void) printf(gettext("no pools available\n"));
4034 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4035 (void) printf(gettext("all pools are healthy\n"));
4043 if (count != 0 && --count == 0)
4046 (void) sleep(interval);
4052 typedef struct upgrade_cbdata {
4056 char cb_poolname[ZPOOL_MAXNAMELEN];
4058 uint64_t cb_version;
4063 is_root_pool(zpool_handle_t *zhp)
4065 static struct statfs sfs;
4066 static char *poolname = NULL;
4067 static boolean_t stated = B_FALSE;
4072 if (statfs("/", &sfs) == -1) {
4073 (void) fprintf(stderr,
4074 "Unable to stat root file system: %s.\n",
4078 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4080 poolname = sfs.f_mntfromname;
4081 if ((slash = strchr(poolname, '/')) != NULL)
4084 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4088 upgrade_cb(zpool_handle_t *zhp, void *arg)
4090 upgrade_cbdata_t *cbp = arg;
4095 config = zpool_get_config(zhp, NULL);
4096 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4099 if (!cbp->cb_newer && version < SPA_VERSION) {
4101 if (cbp->cb_first) {
4102 (void) printf(gettext("The following pools are "
4103 "out of date, and can be upgraded. After "
4104 "being\nupgraded, these pools will no "
4105 "longer be accessible by older software "
4107 (void) printf(gettext("VER POOL\n"));
4108 (void) printf(gettext("--- ------------\n"));
4109 cbp->cb_first = B_FALSE;
4112 (void) printf("%2llu %s\n", (u_longlong_t)version,
4113 zpool_get_name(zhp));
4115 cbp->cb_first = B_FALSE;
4116 ret = zpool_upgrade(zhp, cbp->cb_version);
4118 (void) printf(gettext("Successfully upgraded "
4119 "'%s'\n\n"), zpool_get_name(zhp));
4120 if (cbp->cb_poolname[0] == '\0' &&
4121 is_root_pool(zhp)) {
4122 (void) strlcpy(cbp->cb_poolname,
4123 zpool_get_name(zhp),
4124 sizeof(cbp->cb_poolname));
4128 } else if (cbp->cb_newer && version > SPA_VERSION) {
4129 assert(!cbp->cb_all);
4131 if (cbp->cb_first) {
4132 (void) printf(gettext("The following pools are "
4133 "formatted using a newer software version and\n"
4134 "cannot be accessed on the current system.\n\n"));
4135 (void) printf(gettext("VER POOL\n"));
4136 (void) printf(gettext("--- ------------\n"));
4137 cbp->cb_first = B_FALSE;
4140 (void) printf("%2llu %s\n", (u_longlong_t)version,
4141 zpool_get_name(zhp));
4150 upgrade_one(zpool_handle_t *zhp, void *data)
4152 upgrade_cbdata_t *cbp = data;
4153 uint64_t cur_version;
4156 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4157 (void) printf(gettext("'log' is now a reserved word\n"
4158 "Pool 'log' must be renamed using export and import"
4163 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4164 if (cur_version > cbp->cb_version) {
4165 (void) printf(gettext("Pool '%s' is already formatted "
4166 "using more current version '%llu'.\n"),
4167 zpool_get_name(zhp), cur_version);
4170 if (cur_version == cbp->cb_version) {
4171 (void) printf(gettext("Pool '%s' is already formatted "
4172 "using the current version.\n"), zpool_get_name(zhp));
4176 ret = zpool_upgrade(zhp, cbp->cb_version);
4179 (void) printf(gettext("Successfully upgraded '%s' "
4180 "from version %llu to version %llu\n\n"),
4181 zpool_get_name(zhp), (u_longlong_t)cur_version,
4182 (u_longlong_t)cbp->cb_version);
4183 if (cbp->cb_poolname[0] == '\0' && is_root_pool(zhp)) {
4184 (void) strlcpy(cbp->cb_poolname, zpool_get_name(zhp),
4185 sizeof(cbp->cb_poolname));
4195 * zpool upgrade [-V version] <-a | pool ...>
4197 * With no arguments, display downrev'd ZFS pool available for upgrade.
4198 * Individual pools can be upgraded by specifying the pool, and '-a' will
4199 * upgrade all pools.
4202 zpool_do_upgrade(int argc, char **argv)
4205 upgrade_cbdata_t cb = { 0 };
4207 boolean_t showversions = B_FALSE;
4212 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4218 showversions = B_TRUE;
4221 cb.cb_version = strtoll(optarg, &end, 10);
4222 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4223 cb.cb_version < SPA_VERSION_1) {
4224 (void) fprintf(stderr,
4225 gettext("invalid version '%s'\n"), optarg);
4230 (void) fprintf(stderr, gettext("missing argument for "
4231 "'%c' option\n"), optopt);
4235 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4246 if (cb.cb_version == 0) {
4247 cb.cb_version = SPA_VERSION;
4248 } else if (!cb.cb_all && argc == 0) {
4249 (void) fprintf(stderr, gettext("-V option is "
4250 "incompatible with other arguments\n"));
4255 if (cb.cb_all || argc != 0) {
4256 (void) fprintf(stderr, gettext("-v option is "
4257 "incompatible with other arguments\n"));
4260 } else if (cb.cb_all) {
4262 (void) fprintf(stderr, gettext("-a option should not "
4263 "be used along with a pool name\n"));
4268 (void) printf(gettext("This system is currently running "
4269 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4270 cb.cb_first = B_TRUE;
4272 (void) printf(gettext("The following versions are "
4274 (void) printf(gettext("VER DESCRIPTION\n"));
4275 (void) printf("--- -----------------------------------------"
4276 "---------------\n");
4277 (void) printf(gettext(" 1 Initial ZFS version\n"));
4278 (void) printf(gettext(" 2 Ditto blocks "
4279 "(replicated metadata)\n"));
4280 (void) printf(gettext(" 3 Hot spares and double parity "
4282 (void) printf(gettext(" 4 zpool history\n"));
4283 (void) printf(gettext(" 5 Compression using the gzip "
4285 (void) printf(gettext(" 6 bootfs pool property\n"));
4286 (void) printf(gettext(" 7 Separate intent log devices\n"));
4287 (void) printf(gettext(" 8 Delegated administration\n"));
4288 (void) printf(gettext(" 9 refquota and refreservation "
4290 (void) printf(gettext(" 10 Cache devices\n"));
4291 (void) printf(gettext(" 11 Improved scrub performance\n"));
4292 (void) printf(gettext(" 12 Snapshot properties\n"));
4293 (void) printf(gettext(" 13 snapused property\n"));
4294 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4295 (void) printf(gettext(" 15 user/group space accounting\n"));
4296 (void) printf(gettext(" 16 stmf property support\n"));
4297 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4298 (void) printf(gettext(" 18 Snapshot user holds\n"));
4299 (void) printf(gettext(" 19 Log device removal\n"));
4300 (void) printf(gettext(" 20 Compression using zle "
4301 "(zero-length encoding)\n"));
4302 (void) printf(gettext(" 21 Deduplication\n"));
4303 (void) printf(gettext(" 22 Received properties\n"));
4304 (void) printf(gettext(" 23 Slim ZIL\n"));
4305 (void) printf(gettext(" 24 System attributes\n"));
4306 (void) printf(gettext(" 25 Improved scrub stats\n"));
4307 (void) printf(gettext(" 26 Improved snapshot deletion "
4309 (void) printf(gettext(" 27 Improved snapshot creation "
4311 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4312 (void) printf(gettext("\nFor more information on a particular "
4313 "version, including supported releases,\n"));
4314 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4315 } else if (argc == 0) {
4318 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4319 notfound = cb.cb_first;
4321 if (!cb.cb_all && ret == 0) {
4323 (void) printf("\n");
4324 cb.cb_first = B_TRUE;
4325 cb.cb_newer = B_TRUE;
4326 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4329 (void) printf("\n");
4335 (void) printf(gettext("All pools are formatted "
4336 "using this version.\n"));
4337 else if (!cb.cb_all)
4338 (void) printf(gettext("Use 'zpool upgrade -v' "
4339 "for a list of available versions and "
4340 "their associated\nfeatures.\n"));
4343 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4347 if (cb.cb_poolname[0] != '\0') {
4349 "If you boot from pool '%s', don't forget to update boot code.\n"
4350 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4351 "the following command will do it:\n"
4353 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4360 typedef struct hist_cbdata {
4367 * Print out the command history for a specific pool.
4370 get_history_one(zpool_handle_t *zhp, void *data)
4386 char internalstr[MAXPATHLEN];
4387 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4391 cb->first = B_FALSE;
4393 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4395 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4398 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4399 &records, &numrecords) == 0);
4400 for (i = 0; i < numrecords; i++) {
4401 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4405 /* is it an internal event or a standard event? */
4406 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4408 if (cb->internal == 0)
4411 if (nvlist_lookup_uint64(records[i],
4412 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4414 verify(nvlist_lookup_uint64(records[i],
4415 ZPOOL_HIST_TXG, &txg) == 0);
4416 verify(nvlist_lookup_string(records[i],
4417 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4418 if (ievent >= LOG_END)
4420 (void) snprintf(internalstr,
4421 sizeof (internalstr),
4422 "[internal %s txg:%lld] %s",
4423 zfs_history_event_names[ievent], txg,
4425 cmdstr = internalstr;
4428 (void) localtime_r(&tsec, &t);
4429 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4430 (void) printf("%s %s", tbuf, cmdstr);
4433 (void) printf("\n");
4436 (void) printf(" [");
4437 if (nvlist_lookup_uint64(records[i],
4438 ZPOOL_HIST_WHO, &who) == 0) {
4439 pwd = getpwuid((uid_t)who);
4441 (void) printf("user %s on",
4444 (void) printf("user %d on",
4447 (void) printf(gettext("no info]\n"));
4450 if (nvlist_lookup_string(records[i],
4451 ZPOOL_HIST_HOST, &hostname) == 0) {
4452 (void) printf(" %s", hostname);
4454 if (nvlist_lookup_string(records[i],
4455 ZPOOL_HIST_ZONE, &zonename) == 0) {
4456 (void) printf(":%s", zonename);
4460 (void) printf("\n");
4462 (void) printf("\n");
4469 * zpool history <pool>
4471 * Displays the history of commands that modified pools.
4476 zpool_do_history(int argc, char **argv)
4478 hist_cbdata_t cbdata = { 0 };
4482 cbdata.first = B_TRUE;
4484 while ((c = getopt(argc, argv, "li")) != -1) {
4490 cbdata.internal = 1;
4493 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4501 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4504 if (argc == 0 && cbdata.first == B_TRUE) {
4505 (void) printf(gettext("no pools available\n"));
4513 get_callback(zpool_handle_t *zhp, void *data)
4515 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4516 char value[MAXNAMELEN];
4517 zprop_source_t srctype;
4520 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4523 * Skip the special fake placeholder. This will also skip
4524 * over the name property when 'all' is specified.
4526 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4527 pl == cbp->cb_proplist)
4530 if (zpool_get_prop(zhp, pl->pl_prop,
4531 value, sizeof (value), &srctype) != 0)
4534 zprop_print_one_property(zpool_get_name(zhp), cbp,
4535 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4542 zpool_do_get(int argc, char **argv)
4544 zprop_get_cbdata_t cb = { 0 };
4545 zprop_list_t fake_name = { 0 };
4551 cb.cb_first = B_TRUE;
4552 cb.cb_sources = ZPROP_SRC_ALL;
4553 cb.cb_columns[0] = GET_COL_NAME;
4554 cb.cb_columns[1] = GET_COL_PROPERTY;
4555 cb.cb_columns[2] = GET_COL_VALUE;
4556 cb.cb_columns[3] = GET_COL_SOURCE;
4557 cb.cb_type = ZFS_TYPE_POOL;
4559 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4560 ZFS_TYPE_POOL) != 0)
4563 if (cb.cb_proplist != NULL) {
4564 fake_name.pl_prop = ZPOOL_PROP_NAME;
4565 fake_name.pl_width = strlen(gettext("NAME"));
4566 fake_name.pl_next = cb.cb_proplist;
4567 cb.cb_proplist = &fake_name;
4570 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4573 if (cb.cb_proplist == &fake_name)
4574 zprop_free_list(fake_name.pl_next);
4576 zprop_free_list(cb.cb_proplist);
4581 typedef struct set_cbdata {
4584 boolean_t cb_any_successful;
4588 set_callback(zpool_handle_t *zhp, void *data)
4591 set_cbdata_t *cb = (set_cbdata_t *)data;
4593 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4596 cb->cb_any_successful = B_TRUE;
4602 zpool_do_set(int argc, char **argv)
4604 set_cbdata_t cb = { 0 };
4607 if (argc > 1 && argv[1][0] == '-') {
4608 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4614 (void) fprintf(stderr, gettext("missing property=value "
4620 (void) fprintf(stderr, gettext("missing pool name\n"));
4625 (void) fprintf(stderr, gettext("too many pool names\n"));
4629 cb.cb_propname = argv[1];
4630 cb.cb_value = strchr(cb.cb_propname, '=');
4631 if (cb.cb_value == NULL) {
4632 (void) fprintf(stderr, gettext("missing value in "
4633 "property=value argument\n"));
4637 *(cb.cb_value) = '\0';
4640 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4647 find_command_idx(char *command, int *idx)
4651 for (i = 0; i < NCOMMAND; i++) {
4652 if (command_table[i].name == NULL)
4655 if (strcmp(command, command_table[i].name) == 0) {
4664 main(int argc, char **argv)
4670 (void) setlocale(LC_ALL, "");
4671 (void) textdomain(TEXT_DOMAIN);
4673 if ((g_zfs = libzfs_init()) == NULL) {
4674 (void) fprintf(stderr, gettext("internal error: failed to "
4675 "initialize ZFS library\n"));
4679 libzfs_print_on_error(g_zfs, B_TRUE);
4684 * Make sure the user has specified some command.
4687 (void) fprintf(stderr, gettext("missing command\n"));
4696 if (strcmp(cmdname, "-?") == 0)
4699 zpool_set_history_str("zpool", argc, argv, history_str);
4700 verify(zpool_stage_history(g_zfs, history_str) == 0);
4703 * Run the appropriate command.
4705 if (find_command_idx(cmdname, &i) == 0) {
4706 current_command = &command_table[i];
4707 ret = command_table[i].func(argc - 1, argv + 1);
4708 } else if (strchr(cmdname, '=')) {
4709 verify(find_command_idx("set", &i) == 0);
4710 current_command = &command_table[i];
4711 ret = command_table[i].func(argc, argv);
4712 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4714 * 'freeze' is a vile debugging abomination, so we treat
4718 int fd = open(ZFS_DEV, O_RDWR);
4719 (void) strcpy((void *)buf, argv[2]);
4720 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4722 (void) fprintf(stderr, gettext("unrecognized "
4723 "command '%s'\n"), cmdname);
4730 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4731 * for the purposes of running ::findleaks.
4733 if (getenv("ZFS_ABORT") != NULL) {
4734 (void) printf("dumping core by request\n");