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) 2011 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
48 #include <sys/fs/zfs.h>
53 #include "zpool_util.h"
54 #include "zfs_comutil.h"
56 #include "statcommon.h"
58 static int zpool_do_create(int, char **);
59 static int zpool_do_destroy(int, char **);
61 static int zpool_do_add(int, char **);
62 static int zpool_do_remove(int, char **);
63 static int zpool_do_labelclear(int, char **);
65 static int zpool_do_list(int, char **);
66 static int zpool_do_iostat(int, char **);
67 static int zpool_do_status(int, char **);
69 static int zpool_do_online(int, char **);
70 static int zpool_do_offline(int, char **);
71 static int zpool_do_clear(int, char **);
73 static int zpool_do_reguid(int, char **);
75 static int zpool_do_attach(int, char **);
76 static int zpool_do_detach(int, char **);
77 static int zpool_do_replace(int, char **);
78 static int zpool_do_split(int, char **);
80 static int zpool_do_scrub(int, char **);
82 static int zpool_do_import(int, char **);
83 static int zpool_do_export(int, char **);
85 static int zpool_do_upgrade(int, char **);
87 static int zpool_do_history(int, char **);
89 static int zpool_do_get(int, char **);
90 static int zpool_do_set(int, char **);
93 * These libumem hooks provide a reasonable set of defaults for the allocator's
94 * debugging facilities.
99 _umem_debug_init(void)
101 return ("default,verbose"); /* $UMEM_DEBUG setting */
105 _umem_logging_init(void)
107 return ("fail,contents"); /* $UMEM_LOGGING setting */
138 typedef struct zpool_command {
140 int (*func)(int, char **);
145 * Master command table. Each ZFS command has a name, associated function, and
146 * usage message. The usage messages need to be internationalized, so we have
147 * to have a function to return the usage message based on a command index.
149 * These commands are organized according to how they are displayed in the usage
150 * message. An empty command (one with a NULL name) indicates an empty line in
151 * the generic usage message.
153 static zpool_command_t command_table[] = {
154 { "create", zpool_do_create, HELP_CREATE },
155 { "destroy", zpool_do_destroy, HELP_DESTROY },
157 { "add", zpool_do_add, HELP_ADD },
158 { "remove", zpool_do_remove, HELP_REMOVE },
160 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
162 { "list", zpool_do_list, HELP_LIST },
163 { "iostat", zpool_do_iostat, HELP_IOSTAT },
164 { "status", zpool_do_status, HELP_STATUS },
166 { "online", zpool_do_online, HELP_ONLINE },
167 { "offline", zpool_do_offline, HELP_OFFLINE },
168 { "clear", zpool_do_clear, HELP_CLEAR },
170 { "attach", zpool_do_attach, HELP_ATTACH },
171 { "detach", zpool_do_detach, HELP_DETACH },
172 { "replace", zpool_do_replace, HELP_REPLACE },
173 { "split", zpool_do_split, HELP_SPLIT },
175 { "scrub", zpool_do_scrub, HELP_SCRUB },
177 { "import", zpool_do_import, HELP_IMPORT },
178 { "export", zpool_do_export, HELP_EXPORT },
179 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
180 { "reguid", zpool_do_reguid, HELP_REGUID },
182 { "history", zpool_do_history, HELP_HISTORY },
183 { "get", zpool_do_get, HELP_GET },
184 { "set", zpool_do_set, HELP_SET },
187 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
189 zpool_command_t *current_command;
190 static char history_str[HIS_MAX_RECORD_LEN];
192 static uint_t timestamp_fmt = NODATE;
195 get_usage(zpool_help_t idx) {
198 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
200 return (gettext("\tattach [-f] <pool> <device> "
203 return (gettext("\tclear [-nF] <pool> [device]\n"));
205 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
206 "\t [-O file-system-property=value] ... \n"
207 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
209 return (gettext("\tdestroy [-f] <pool>\n"));
211 return (gettext("\tdetach <pool> <device>\n"));
213 return (gettext("\texport [-f] <pool> ...\n"));
215 return (gettext("\thistory [-il] [<pool>] ...\n"));
217 return (gettext("\timport [-d dir] [-D]\n"
218 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
219 "\timport [-o mntopts] [-o property=value] ... \n"
220 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
221 "[-R root] [-F [-n]] -a\n"
222 "\timport [-o mntopts] [-o property=value] ... \n"
223 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
224 "[-R root] [-F [-n]]\n"
225 "\t <pool | id> [newpool]\n"));
227 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
229 case HELP_LABELCLEAR:
230 return (gettext("\tlabelclear [-f] <vdev>\n"));
232 return (gettext("\tlist [-H] [-o property[,...]] "
233 "[-T d|u] [pool] ... [interval [count]]\n"));
235 return (gettext("\toffline [-t] <pool> <device> ...\n"));
237 return (gettext("\tonline [-e] <pool> <device> ...\n"));
239 return (gettext("\treplace [-f] <pool> <device> "
242 return (gettext("\tremove <pool> <device> ...\n"));
244 return (gettext("\tscrub [-s] <pool> ...\n"));
246 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
249 return (gettext("\tupgrade [-v]\n"
250 "\tupgrade [-V version] <-a | pool ...>\n"));
252 return (gettext("\tget <\"all\" | property[,...]> "
255 return (gettext("\tset <property=value> <pool> \n"));
257 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
258 "\t [-o property=value] <pool> <newpool> "
259 "[<device> ...]\n"));
261 return (gettext("\treguid <pool>\n"));
270 * Callback routine that will print out a pool property value.
273 print_prop_cb(int prop, void *cb)
277 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
279 if (zpool_prop_readonly(prop))
280 (void) fprintf(fp, " NO ");
282 (void) fprintf(fp, " YES ");
284 if (zpool_prop_values(prop) == NULL)
285 (void) fprintf(fp, "-\n");
287 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
293 * Display usage message. If we're inside a command, display only the usage for
294 * that command. Otherwise, iterate over the entire command table and display
295 * a complete usage message.
298 usage(boolean_t requested)
300 FILE *fp = requested ? stdout : stderr;
302 if (current_command == NULL) {
305 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
307 gettext("where 'command' is one of the following:\n\n"));
309 for (i = 0; i < NCOMMAND; i++) {
310 if (command_table[i].name == NULL)
311 (void) fprintf(fp, "\n");
313 (void) fprintf(fp, "%s",
314 get_usage(command_table[i].usage));
317 (void) fprintf(fp, gettext("usage:\n"));
318 (void) fprintf(fp, "%s", get_usage(current_command->usage));
321 if (current_command != NULL &&
322 ((strcmp(current_command->name, "set") == 0) ||
323 (strcmp(current_command->name, "get") == 0) ||
324 (strcmp(current_command->name, "list") == 0))) {
327 gettext("\nthe following properties are supported:\n"));
329 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
330 "PROPERTY", "EDIT", "VALUES");
332 /* Iterate over all properties */
333 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
338 * See comments at end of main().
340 if (getenv("ZFS_ABORT") != NULL) {
341 (void) printf("dumping core by request\n");
345 exit(requested ? 0 : 2);
349 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
350 boolean_t print_logs)
357 (void) printf("\t%*s%s\n", indent, "", name);
359 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
360 &child, &children) != 0)
363 for (c = 0; c < children; c++) {
364 uint64_t is_log = B_FALSE;
366 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
368 if ((is_log && !print_logs) || (!is_log && print_logs))
371 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
372 print_vdev_tree(zhp, vname, child[c], indent + 2,
379 * Add a property pair (name, string-value) into a property nvlist.
382 add_prop_list(const char *propname, char *propval, nvlist_t **props,
385 zpool_prop_t prop = ZPROP_INVAL;
391 if (*props == NULL &&
392 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
393 (void) fprintf(stderr,
394 gettext("internal error: out of memory\n"));
401 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
402 (void) fprintf(stderr, gettext("property '%s' is "
403 "not a valid pool property\n"), propname);
406 normnm = zpool_prop_to_name(prop);
408 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
409 normnm = zfs_prop_to_name(fprop);
415 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
416 prop != ZPOOL_PROP_CACHEFILE) {
417 (void) fprintf(stderr, gettext("property '%s' "
418 "specified multiple times\n"), propname);
422 if (nvlist_add_string(proplist, normnm, propval) != 0) {
423 (void) fprintf(stderr, gettext("internal "
424 "error: out of memory\n"));
432 * zpool add [-fn] <pool> <vdev> ...
434 * -f Force addition of devices, even if they appear in use
435 * -n Do not add the devices, but display the resulting layout if
436 * they were to be added.
438 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
439 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
443 zpool_do_add(int argc, char **argv)
445 boolean_t force = B_FALSE;
446 boolean_t dryrun = B_FALSE;
455 while ((c = getopt(argc, argv, "fn")) != -1) {
464 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
473 /* get pool name and check number of arguments */
475 (void) fprintf(stderr, gettext("missing pool name argument\n"));
479 (void) fprintf(stderr, gettext("missing vdev specification\n"));
488 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
491 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
492 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
498 /* pass off to get_vdev_spec for processing */
499 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
501 if (nvroot == NULL) {
507 nvlist_t *poolnvroot;
509 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
512 (void) printf(gettext("would update '%s' to the following "
513 "configuration:\n"), zpool_get_name(zhp));
515 /* print original main pool and new tree */
516 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
517 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
519 /* Do the same for the logs */
520 if (num_logs(poolnvroot) > 0) {
521 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
522 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
523 } else if (num_logs(nvroot) > 0) {
524 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
529 ret = (zpool_add(zhp, nvroot) != 0);
539 * zpool remove <pool> <vdev> ...
541 * Removes the given vdev from the pool. Currently, this supports removing
542 * spares, cache, and log devices from the pool.
545 zpool_do_remove(int argc, char **argv)
554 /* get pool name and check number of arguments */
556 (void) fprintf(stderr, gettext("missing pool name argument\n"));
560 (void) fprintf(stderr, gettext("missing device\n"));
566 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
569 for (i = 1; i < argc; i++) {
570 if (zpool_vdev_remove(zhp, argv[i]) != 0)
578 * zpool labelclear <vdev>
580 * Verifies that the vdev is not active and zeros out the label information
584 zpool_do_labelclear(int argc, char **argv)
587 int c, fd = -1, ret = 0;
589 boolean_t inuse = B_FALSE;
590 boolean_t force = B_FALSE;
593 while ((c = getopt(argc, argv, "f")) != -1) {
599 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
610 (void) fprintf(stderr, gettext("missing vdev device name\n"));
615 if ((fd = open(vdev, O_RDWR)) < 0) {
616 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
621 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
625 (void) fprintf(stderr,
626 gettext("Unable to determine pool state for %s\n"
627 "Use -f to force the clearing any label data\n"), vdev);
635 case POOL_STATE_ACTIVE:
636 case POOL_STATE_SPARE:
637 case POOL_STATE_L2CACHE:
638 (void) fprintf(stderr,
639 gettext("labelclear operation failed.\n"
640 "\tVdev %s is a member (%s), of pool \"%s\".\n"
641 "\tTo remove label information from this device, export or destroy\n"
642 "\tthe pool, or remove %s from the configuration of this pool\n"
643 "\tand retry the labelclear operation\n"),
644 vdev, zpool_pool_state_to_name(state), name, vdev);
648 case POOL_STATE_EXPORTED:
652 (void) fprintf(stderr,
653 gettext("labelclear operation failed.\n"
654 "\tVdev %s is a member of the exported pool \"%s\".\n"
655 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
661 case POOL_STATE_POTENTIALLY_ACTIVE:
665 (void) fprintf(stderr,
666 gettext("labelclear operation failed.\n"
667 "\tVdev %s is a member of the pool \"%s\".\n"
668 "\tThis pool is unknown to this system, but may be active on\n"
669 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
670 "\tremoval of label information.\n"),
675 case POOL_STATE_DESTROYED:
676 /* inuse should never be set for a destoryed pool... */
682 if (zpool_clear_label(fd) != 0) {
683 (void) fprintf(stderr,
684 gettext("Label clear failed on vdev %s\n"), vdev);
697 * zpool create [-fn] [-o property=value] ...
698 * [-O file-system-property=value] ...
699 * [-R root] [-m mountpoint] <pool> <dev> ...
701 * -f Force creation, even if devices appear in use
702 * -n Do not create the pool, but display the resulting layout if it
703 * were to be created.
704 * -R Create a pool under an alternate root
705 * -m Set default mountpoint for the root dataset. By default it's
707 * -o Set property=value.
708 * -O Set fsproperty=value in the pool's root file system
710 * Creates the named pool according to the given vdev specification. The
711 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
712 * we get the nvlist back from get_vdev_spec(), we either print out the contents
713 * (if '-n' was specified), or pass it to libzfs to do the creation.
716 zpool_do_create(int argc, char **argv)
718 boolean_t force = B_FALSE;
719 boolean_t dryrun = B_FALSE;
721 nvlist_t *nvroot = NULL;
724 char *altroot = NULL;
725 char *mountpoint = NULL;
726 nvlist_t *fsprops = NULL;
727 nvlist_t *props = NULL;
731 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
741 if (add_prop_list(zpool_prop_to_name(
742 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
744 if (nvlist_lookup_string(props,
745 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
748 if (add_prop_list(zpool_prop_to_name(
749 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
756 if ((propval = strchr(optarg, '=')) == NULL) {
757 (void) fprintf(stderr, gettext("missing "
758 "'=' for -o option\n"));
764 if (add_prop_list(optarg, propval, &props, B_TRUE))
768 if ((propval = strchr(optarg, '=')) == NULL) {
769 (void) fprintf(stderr, gettext("missing "
770 "'=' for -O option\n"));
776 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
780 (void) fprintf(stderr, gettext("missing argument for "
781 "'%c' option\n"), optopt);
784 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
793 /* get pool name and check number of arguments */
795 (void) fprintf(stderr, gettext("missing pool name argument\n"));
799 (void) fprintf(stderr, gettext("missing vdev specification\n"));
806 * As a special case, check for use of '/' in the name, and direct the
807 * user to use 'zfs create' instead.
809 if (strchr(poolname, '/') != NULL) {
810 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
811 "character '/' in pool name\n"), poolname);
812 (void) fprintf(stderr, gettext("use 'zfs create' to "
813 "create a dataset\n"));
817 /* pass off to get_vdev_spec for bulk processing */
818 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
823 /* make_root_vdev() allows 0 toplevel children if there are spares */
824 if (!zfs_allocatable_devs(nvroot)) {
825 (void) fprintf(stderr, gettext("invalid vdev "
826 "specification: at least one toplevel vdev must be "
832 if (altroot != NULL && altroot[0] != '/') {
833 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
834 "must be an absolute path\n"), altroot);
839 * Check the validity of the mountpoint and direct the user to use the
840 * '-m' mountpoint option if it looks like its in use.
842 if (mountpoint == NULL ||
843 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
844 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
845 char buf[MAXPATHLEN];
848 if (mountpoint && mountpoint[0] != '/') {
849 (void) fprintf(stderr, gettext("invalid mountpoint "
850 "'%s': must be an absolute path, 'legacy', or "
851 "'none'\n"), mountpoint);
855 if (mountpoint == NULL) {
857 (void) snprintf(buf, sizeof (buf), "%s/%s",
860 (void) snprintf(buf, sizeof (buf), "/%s",
864 (void) snprintf(buf, sizeof (buf), "%s%s",
865 altroot, mountpoint);
867 (void) snprintf(buf, sizeof (buf), "%s",
871 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
872 (void) fprintf(stderr, gettext("mountpoint '%s' : "
873 "%s\n"), buf, strerror(errno));
874 (void) fprintf(stderr, gettext("use '-m' "
875 "option to provide a different default\n"));
880 while (count < 3 && readdir(dirp) != NULL)
882 (void) closedir(dirp);
885 (void) fprintf(stderr, gettext("mountpoint "
886 "'%s' exists and is not empty\n"), buf);
887 (void) fprintf(stderr, gettext("use '-m' "
888 "option to provide a "
889 "different default\n"));
897 * For a dry run invocation, print out a basic message and run
898 * through all the vdevs in the list and print out in an
899 * appropriate hierarchy.
901 (void) printf(gettext("would create '%s' with the "
902 "following layout:\n\n"), poolname);
904 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
905 if (num_logs(nvroot) > 0)
906 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
911 * Hand off to libzfs.
913 if (zpool_create(g_zfs, poolname,
914 nvroot, props, fsprops) == 0) {
915 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
916 ZFS_TYPE_FILESYSTEM);
918 if (mountpoint != NULL)
919 verify(zfs_prop_set(pool,
921 ZFS_PROP_MOUNTPOINT),
923 if (zfs_mount(pool, NULL, 0) == 0)
924 ret = zfs_shareall(pool);
927 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
928 (void) fprintf(stderr, gettext("pool name may have "
935 nvlist_free(fsprops);
939 nvlist_free(fsprops);
946 * zpool destroy <pool>
948 * -f Forcefully unmount any datasets
950 * Destroy the given pool. Automatically unmounts any datasets in the pool.
953 zpool_do_destroy(int argc, char **argv)
955 boolean_t force = B_FALSE;
962 while ((c = getopt(argc, argv, "f")) != -1) {
968 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
977 /* check arguments */
979 (void) fprintf(stderr, gettext("missing pool argument\n"));
983 (void) fprintf(stderr, gettext("too many arguments\n"));
989 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
991 * As a special case, check for use of '/' in the name, and
992 * direct the user to use 'zfs destroy' instead.
994 if (strchr(pool, '/') != NULL)
995 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
996 "destroy a dataset\n"));
1000 if (zpool_disable_datasets(zhp, force) != 0) {
1001 (void) fprintf(stderr, gettext("could not destroy '%s': "
1002 "could not unmount datasets\n"), zpool_get_name(zhp));
1006 ret = (zpool_destroy(zhp) != 0);
1014 * zpool export [-f] <pool> ...
1016 * -f Forcefully unmount datasets
1018 * Export the given pools. By default, the command will attempt to cleanly
1019 * unmount any active datasets within the pool. If the '-f' flag is specified,
1020 * then the datasets will be forcefully unmounted.
1023 zpool_do_export(int argc, char **argv)
1025 boolean_t force = B_FALSE;
1026 boolean_t hardforce = B_FALSE;
1028 zpool_handle_t *zhp;
1033 while ((c = getopt(argc, argv, "fF")) != -1) {
1042 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1051 /* check arguments */
1053 (void) fprintf(stderr, gettext("missing pool argument\n"));
1058 for (i = 0; i < argc; i++) {
1059 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1064 if (zpool_disable_datasets(zhp, force) != 0) {
1071 if (zpool_export_force(zhp) != 0)
1073 } else if (zpool_export(zhp, force) != 0) {
1084 * Given a vdev configuration, determine the maximum width needed for the device
1088 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1090 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1095 if (strlen(name) + depth > max)
1096 max = strlen(name) + depth;
1100 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1101 &child, &children) == 0) {
1102 for (c = 0; c < children; c++)
1103 if ((ret = max_width(zhp, child[c], depth + 2,
1108 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1109 &child, &children) == 0) {
1110 for (c = 0; c < children; c++)
1111 if ((ret = max_width(zhp, child[c], depth + 2,
1116 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1117 &child, &children) == 0) {
1118 for (c = 0; c < children; c++)
1119 if ((ret = max_width(zhp, child[c], depth + 2,
1128 typedef struct spare_cbdata {
1130 zpool_handle_t *cb_zhp;
1134 find_vdev(nvlist_t *nv, uint64_t search)
1140 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1144 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1145 &child, &children) == 0) {
1146 for (c = 0; c < children; c++)
1147 if (find_vdev(child[c], search))
1155 find_spare(zpool_handle_t *zhp, void *data)
1157 spare_cbdata_t *cbp = data;
1158 nvlist_t *config, *nvroot;
1160 config = zpool_get_config(zhp, NULL);
1161 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1164 if (find_vdev(nvroot, cbp->cb_guid)) {
1174 * Print out configuration state as requested by status_callback.
1177 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1178 int namewidth, int depth, boolean_t isspare)
1182 pool_scan_stat_t *ps = NULL;
1184 char rbuf[6], wbuf[6], cbuf[6];
1186 uint64_t notpresent;
1190 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1191 &child, &children) != 0)
1194 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1195 (uint64_t **)&vs, &c) == 0);
1197 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1200 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1203 if (vs->vs_aux == VDEV_AUX_SPARED)
1205 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1209 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1213 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1214 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1215 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1216 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1219 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1220 ¬present) == 0 ||
1221 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1223 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1224 (void) printf(" was %s", path);
1225 } else if (vs->vs_aux != 0) {
1228 switch (vs->vs_aux) {
1229 case VDEV_AUX_OPEN_FAILED:
1230 (void) printf(gettext("cannot open"));
1233 case VDEV_AUX_BAD_GUID_SUM:
1234 (void) printf(gettext("missing device"));
1237 case VDEV_AUX_NO_REPLICAS:
1238 (void) printf(gettext("insufficient replicas"));
1241 case VDEV_AUX_VERSION_NEWER:
1242 (void) printf(gettext("newer version"));
1245 case VDEV_AUX_SPARED:
1246 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1248 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1249 if (strcmp(zpool_get_name(cb.cb_zhp),
1250 zpool_get_name(zhp)) == 0)
1251 (void) printf(gettext("currently in "
1254 (void) printf(gettext("in use by "
1256 zpool_get_name(cb.cb_zhp));
1257 zpool_close(cb.cb_zhp);
1259 (void) printf(gettext("currently in use"));
1263 case VDEV_AUX_ERR_EXCEEDED:
1264 (void) printf(gettext("too many errors"));
1267 case VDEV_AUX_IO_FAILURE:
1268 (void) printf(gettext("experienced I/O failures"));
1271 case VDEV_AUX_BAD_LOG:
1272 (void) printf(gettext("bad intent log"));
1275 case VDEV_AUX_EXTERNAL:
1276 (void) printf(gettext("external device fault"));
1279 case VDEV_AUX_SPLIT_POOL:
1280 (void) printf(gettext("split into new pool"));
1284 (void) printf(gettext("corrupted data"));
1289 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1290 (uint64_t **)&ps, &c);
1292 if (ps && ps->pss_state == DSS_SCANNING &&
1293 vs->vs_scan_processed != 0 && children == 0) {
1294 (void) printf(gettext(" (%s)"),
1295 (ps->pss_func == POOL_SCAN_RESILVER) ?
1296 "resilvering" : "repairing");
1299 (void) printf("\n");
1301 for (c = 0; c < children; c++) {
1302 uint64_t islog = B_FALSE, ishole = B_FALSE;
1304 /* Don't print logs or holes here */
1305 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1307 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1309 if (islog || ishole)
1311 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1312 print_status_config(zhp, vname, child[c],
1313 namewidth, depth + 2, isspare);
1320 * Print the configuration of an exported pool. Iterate over all vdevs in the
1321 * pool, printing out the name and status for each one.
1324 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1331 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1332 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1333 strcmp(type, VDEV_TYPE_HOLE) == 0)
1336 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1337 (uint64_t **)&vs, &c) == 0);
1339 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1340 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1342 if (vs->vs_aux != 0) {
1345 switch (vs->vs_aux) {
1346 case VDEV_AUX_OPEN_FAILED:
1347 (void) printf(gettext("cannot open"));
1350 case VDEV_AUX_BAD_GUID_SUM:
1351 (void) printf(gettext("missing device"));
1354 case VDEV_AUX_NO_REPLICAS:
1355 (void) printf(gettext("insufficient replicas"));
1358 case VDEV_AUX_VERSION_NEWER:
1359 (void) printf(gettext("newer version"));
1362 case VDEV_AUX_ERR_EXCEEDED:
1363 (void) printf(gettext("too many errors"));
1367 (void) printf(gettext("corrupted data"));
1371 (void) printf("\n");
1373 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1374 &child, &children) != 0)
1377 for (c = 0; c < children; c++) {
1378 uint64_t is_log = B_FALSE;
1380 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1385 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1386 print_import_config(vname, child[c], namewidth, depth + 2);
1390 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1391 &child, &children) == 0) {
1392 (void) printf(gettext("\tcache\n"));
1393 for (c = 0; c < children; c++) {
1394 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1395 (void) printf("\t %s\n", vname);
1400 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1401 &child, &children) == 0) {
1402 (void) printf(gettext("\tspares\n"));
1403 for (c = 0; c < children; c++) {
1404 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1405 (void) printf("\t %s\n", vname);
1413 * Logs are recorded as top level vdevs in the main pool child array
1414 * but with "is_log" set to 1. We use either print_status_config() or
1415 * print_import_config() to print the top level logs then any log
1416 * children (eg mirrored slogs) are printed recursively - which
1417 * works because only the top level vdev is marked "is_log"
1420 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1425 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1429 (void) printf(gettext("\tlogs\n"));
1431 for (c = 0; c < children; c++) {
1432 uint64_t is_log = B_FALSE;
1435 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1439 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1441 print_status_config(zhp, name, child[c], namewidth,
1444 print_import_config(name, child[c], namewidth, 2);
1450 * Display the status for the given pool.
1453 show_import(nvlist_t *config)
1455 uint64_t pool_state;
1467 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1469 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1471 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1473 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1476 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1477 (uint64_t **)&vs, &vsc) == 0);
1478 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1480 reason = zpool_import_status(config, &msgid);
1482 (void) printf(gettext(" pool: %s\n"), name);
1483 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1484 (void) printf(gettext(" state: %s"), health);
1485 if (pool_state == POOL_STATE_DESTROYED)
1486 (void) printf(gettext(" (DESTROYED)"));
1487 (void) printf("\n");
1490 case ZPOOL_STATUS_MISSING_DEV_R:
1491 case ZPOOL_STATUS_MISSING_DEV_NR:
1492 case ZPOOL_STATUS_BAD_GUID_SUM:
1493 (void) printf(gettext(" status: One or more devices are "
1494 "missing from the system.\n"));
1497 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1498 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1499 (void) printf(gettext(" status: One or more devices contains "
1500 "corrupted data.\n"));
1503 case ZPOOL_STATUS_CORRUPT_DATA:
1505 gettext(" status: The pool data is corrupted.\n"));
1508 case ZPOOL_STATUS_OFFLINE_DEV:
1509 (void) printf(gettext(" status: One or more devices "
1510 "are offlined.\n"));
1513 case ZPOOL_STATUS_CORRUPT_POOL:
1514 (void) printf(gettext(" status: The pool metadata is "
1518 case ZPOOL_STATUS_VERSION_OLDER:
1519 (void) printf(gettext(" status: The pool is formatted using an "
1520 "older on-disk version.\n"));
1523 case ZPOOL_STATUS_VERSION_NEWER:
1524 (void) printf(gettext(" status: The pool is formatted using an "
1525 "incompatible version.\n"));
1528 case ZPOOL_STATUS_HOSTID_MISMATCH:
1529 (void) printf(gettext(" status: The pool was last accessed by "
1530 "another system.\n"));
1533 case ZPOOL_STATUS_FAULTED_DEV_R:
1534 case ZPOOL_STATUS_FAULTED_DEV_NR:
1535 (void) printf(gettext(" status: One or more devices are "
1539 case ZPOOL_STATUS_BAD_LOG:
1540 (void) printf(gettext(" status: An intent log record cannot be "
1544 case ZPOOL_STATUS_RESILVERING:
1545 (void) printf(gettext(" status: One or more devices were being "
1551 * No other status can be seen when importing pools.
1553 assert(reason == ZPOOL_STATUS_OK);
1557 * Print out an action according to the overall state of the pool.
1559 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1560 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1561 (void) printf(gettext(" action: The pool can be "
1562 "imported using its name or numeric identifier, "
1563 "though\n\tsome features will not be available "
1564 "without an explicit 'zpool upgrade'.\n"));
1565 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1566 (void) printf(gettext(" action: The pool can be "
1567 "imported using its name or numeric "
1568 "identifier and\n\tthe '-f' flag.\n"));
1570 (void) printf(gettext(" action: The pool can be "
1571 "imported using its name or numeric "
1573 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1574 (void) printf(gettext(" action: The pool can be imported "
1575 "despite missing or damaged devices. The\n\tfault "
1576 "tolerance of the pool may be compromised if imported.\n"));
1579 case ZPOOL_STATUS_VERSION_NEWER:
1580 (void) printf(gettext(" action: The pool cannot be "
1581 "imported. Access the pool on a system running "
1582 "newer\n\tsoftware, or recreate the pool from "
1585 case ZPOOL_STATUS_MISSING_DEV_R:
1586 case ZPOOL_STATUS_MISSING_DEV_NR:
1587 case ZPOOL_STATUS_BAD_GUID_SUM:
1588 (void) printf(gettext(" action: The pool cannot be "
1589 "imported. Attach the missing\n\tdevices and try "
1593 (void) printf(gettext(" action: The pool cannot be "
1594 "imported due to damaged devices or data.\n"));
1598 /* Print the comment attached to the pool. */
1599 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1600 (void) printf(gettext("comment: %s\n"), comment);
1603 * If the state is "closed" or "can't open", and the aux state
1604 * is "corrupt data":
1606 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1607 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1608 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1609 if (pool_state == POOL_STATE_DESTROYED)
1610 (void) printf(gettext("\tThe pool was destroyed, "
1611 "but can be imported using the '-Df' flags.\n"));
1612 else if (pool_state != POOL_STATE_EXPORTED)
1613 (void) printf(gettext("\tThe pool may be active on "
1614 "another system, but can be imported using\n\t"
1615 "the '-f' flag.\n"));
1619 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1622 (void) printf(gettext(" config:\n\n"));
1624 namewidth = max_width(NULL, nvroot, 0, 0);
1628 print_import_config(name, nvroot, namewidth, 0);
1629 if (num_logs(nvroot) > 0)
1630 print_logs(NULL, nvroot, namewidth, B_FALSE);
1632 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1633 (void) printf(gettext("\n\tAdditional devices are known to "
1634 "be part of this pool, though their\n\texact "
1635 "configuration cannot be determined.\n"));
1640 * Perform the import for the given configuration. This passes the heavy
1641 * lifting off to zpool_import_props(), and then mounts the datasets contained
1645 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1646 nvlist_t *props, int flags)
1648 zpool_handle_t *zhp;
1653 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1656 verify(nvlist_lookup_uint64(config,
1657 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1658 verify(nvlist_lookup_uint64(config,
1659 ZPOOL_CONFIG_VERSION, &version) == 0);
1660 if (version > SPA_VERSION) {
1661 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1662 "is formatted using a newer ZFS version\n"), name);
1664 } else if (state != POOL_STATE_EXPORTED &&
1665 !(flags & ZFS_IMPORT_ANY_HOST)) {
1668 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1670 if ((unsigned long)hostid != gethostid()) {
1675 verify(nvlist_lookup_string(config,
1676 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1677 verify(nvlist_lookup_uint64(config,
1678 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1680 (void) fprintf(stderr, gettext("cannot import "
1681 "'%s': pool may be in use from other "
1682 "system, it was last accessed by %s "
1683 "(hostid: 0x%lx) on %s"), name, hostname,
1684 (unsigned long)hostid,
1685 asctime(localtime(&t)));
1686 (void) fprintf(stderr, gettext("use '-f' to "
1687 "import anyway\n"));
1691 (void) fprintf(stderr, gettext("cannot import '%s': "
1692 "pool may be in use from other system\n"), name);
1693 (void) fprintf(stderr, gettext("use '-f' to import "
1699 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1702 if (newname != NULL)
1703 name = (char *)newname;
1705 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1708 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1709 !(flags & ZFS_IMPORT_ONLY) &&
1710 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1720 * zpool import [-d dir] [-D]
1721 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1722 * [-d dir | -c cachefile] [-f] -a
1723 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1724 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1726 * -c Read pool information from a cachefile instead of searching
1729 * -d Scan in a specific directory, other than /dev/dsk. More than
1730 * one directory can be specified using multiple '-d' options.
1732 * -D Scan for previously destroyed pools or import all or only
1733 * specified destroyed pools.
1735 * -R Temporarily import the pool, with all mountpoints relative to
1736 * the given root. The pool will remain exported when the machine
1739 * -V Import even in the presence of faulted vdevs. This is an
1740 * intentionally undocumented option for testing purposes, and
1741 * treats the pool configuration as complete, leaving any bad
1742 * vdevs in the FAULTED state. In other words, it does verbatim
1745 * -f Force import, even if it appears that the pool is active.
1747 * -F Attempt rewind if necessary.
1749 * -n See if rewind would work, but don't actually rewind.
1751 * -N Import the pool but don't mount datasets.
1753 * -T Specify a starting txg to use for import. This option is
1754 * intentionally undocumented option for testing purposes.
1756 * -a Import all pools found.
1758 * -o Set property=value and/or temporary mount options (without '=').
1760 * The import command scans for pools to import, and import pools based on pool
1761 * name and GUID. The pool can also be renamed as part of the import process.
1764 zpool_do_import(int argc, char **argv)
1766 char **searchdirs = NULL;
1770 nvlist_t *pools = NULL;
1771 boolean_t do_all = B_FALSE;
1772 boolean_t do_destroyed = B_FALSE;
1773 char *mntopts = NULL;
1776 uint64_t searchguid = 0;
1777 char *searchname = NULL;
1779 nvlist_t *found_config;
1780 nvlist_t *policy = NULL;
1781 nvlist_t *props = NULL;
1783 int flags = ZFS_IMPORT_NORMAL;
1784 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1785 boolean_t dryrun = B_FALSE;
1786 boolean_t do_rewind = B_FALSE;
1787 boolean_t xtreme_rewind = B_FALSE;
1788 uint64_t pool_state, txg = -1ULL;
1789 char *cachefile = NULL;
1790 importargs_t idata = { 0 };
1794 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1803 if (searchdirs == NULL) {
1804 searchdirs = safe_malloc(sizeof (char *));
1806 char **tmp = safe_malloc((nsearch + 1) *
1808 bcopy(searchdirs, tmp, nsearch *
1813 searchdirs[nsearch++] = optarg;
1816 do_destroyed = B_TRUE;
1819 flags |= ZFS_IMPORT_ANY_HOST;
1825 flags |= ZFS_IMPORT_MISSING_LOG;
1831 flags |= ZFS_IMPORT_ONLY;
1834 if ((propval = strchr(optarg, '=')) != NULL) {
1837 if (add_prop_list(optarg, propval,
1845 if (add_prop_list(zpool_prop_to_name(
1846 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1848 if (nvlist_lookup_string(props,
1849 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1852 if (add_prop_list(zpool_prop_to_name(
1853 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1858 txg = strtoull(optarg, &endptr, 10);
1859 if (errno != 0 || *endptr != '\0') {
1860 (void) fprintf(stderr,
1861 gettext("invalid txg value\n"));
1864 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1867 flags |= ZFS_IMPORT_VERBATIM;
1870 xtreme_rewind = B_TRUE;
1873 (void) fprintf(stderr, gettext("missing argument for "
1874 "'%c' option\n"), optopt);
1878 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1887 if (cachefile && nsearch != 0) {
1888 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1892 if ((dryrun || xtreme_rewind) && !do_rewind) {
1893 (void) fprintf(stderr,
1894 gettext("-n or -X only meaningful with -F\n"));
1898 rewind_policy = ZPOOL_TRY_REWIND;
1900 rewind_policy = ZPOOL_DO_REWIND;
1902 rewind_policy |= ZPOOL_EXTREME_REWIND;
1904 /* In the future, we can capture further policy and include it here */
1905 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1906 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1907 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1910 if (searchdirs == NULL) {
1911 searchdirs = safe_malloc(sizeof (char *));
1912 searchdirs[0] = "/dev/dsk";
1916 /* check argument count */
1919 (void) fprintf(stderr, gettext("too many arguments\n"));
1924 (void) fprintf(stderr, gettext("too many arguments\n"));
1929 * Check for the SYS_CONFIG privilege. We do this explicitly
1930 * here because otherwise any attempt to discover pools will
1933 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1934 (void) fprintf(stderr, gettext("cannot "
1935 "discover pools: permission denied\n"));
1937 nvlist_free(policy);
1943 * Depending on the arguments given, we do one of the following:
1945 * <none> Iterate through all pools and display information about
1948 * -a Iterate through all pools and try to import each one.
1950 * <id> Find the pool that corresponds to the given GUID/pool
1951 * name and import that one.
1953 * -D Above options applies only to destroyed pools.
1959 searchguid = strtoull(argv[0], &endptr, 10);
1960 if (errno != 0 || *endptr != '\0')
1961 searchname = argv[0];
1962 found_config = NULL;
1965 * User specified a name or guid. Ensure it's unique.
1967 idata.unique = B_TRUE;
1971 idata.path = searchdirs;
1972 idata.paths = nsearch;
1973 idata.poolname = searchname;
1974 idata.guid = searchguid;
1975 idata.cachefile = cachefile;
1977 pools = zpool_search_import(g_zfs, &idata);
1979 if (pools != NULL && idata.exists &&
1980 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1981 (void) fprintf(stderr, gettext("cannot import '%s': "
1982 "a pool with that name already exists\n"),
1984 (void) fprintf(stderr, gettext("use the form '%s "
1985 "<pool | id> <newpool>' to give it a new name\n"),
1988 } else if (pools == NULL && idata.exists) {
1989 (void) fprintf(stderr, gettext("cannot import '%s': "
1990 "a pool with that name is already created/imported,\n"),
1992 (void) fprintf(stderr, gettext("and no additional pools "
1993 "with that name were found\n"));
1995 } else if (pools == NULL) {
1997 (void) fprintf(stderr, gettext("cannot import '%s': "
1998 "no such pool available\n"), argv[0]);
2005 nvlist_free(policy);
2010 * At this point we have a list of import candidate configs. Even if
2011 * we were searching by pool name or guid, we still need to
2012 * post-process the list to deal with pool state and possible
2018 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2020 verify(nvpair_value_nvlist(elem, &config) == 0);
2022 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2024 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2026 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2029 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2036 (void) printf("\n");
2039 err |= do_import(config, NULL, mntopts,
2042 show_import(config);
2044 } else if (searchname != NULL) {
2048 * We are searching for a pool based on name.
2050 verify(nvlist_lookup_string(config,
2051 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2053 if (strcmp(name, searchname) == 0) {
2054 if (found_config != NULL) {
2055 (void) fprintf(stderr, gettext(
2056 "cannot import '%s': more than "
2057 "one matching pool\n"), searchname);
2058 (void) fprintf(stderr, gettext(
2059 "import by numeric ID instead\n"));
2062 found_config = config;
2068 * Search for a pool by guid.
2070 verify(nvlist_lookup_uint64(config,
2071 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2073 if (guid == searchguid)
2074 found_config = config;
2079 * If we were searching for a specific pool, verify that we found a
2080 * pool, and then do the import.
2082 if (argc != 0 && err == 0) {
2083 if (found_config == NULL) {
2084 (void) fprintf(stderr, gettext("cannot import '%s': "
2085 "no such pool available\n"), argv[0]);
2088 err |= do_import(found_config, argc == 1 ? NULL :
2089 argv[1], mntopts, props, flags);
2094 * If we were just looking for pools, report an error if none were
2097 if (argc == 0 && first)
2098 (void) fprintf(stderr,
2099 gettext("no pools available to import\n"));
2104 nvlist_free(policy);
2107 return (err ? 1 : 0);
2110 typedef struct iostat_cbdata {
2111 zpool_list_t *cb_list;
2118 print_iostat_separator(iostat_cbdata_t *cb)
2122 for (i = 0; i < cb->cb_namewidth; i++)
2124 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2128 print_iostat_header(iostat_cbdata_t *cb)
2130 (void) printf("%*s capacity operations bandwidth\n",
2131 cb->cb_namewidth, "");
2132 (void) printf("%-*s alloc free read write read write\n",
2133 cb->cb_namewidth, "pool");
2134 print_iostat_separator(cb);
2138 * Display a single statistic.
2141 print_one_stat(uint64_t value)
2145 zfs_nicenum(value, buf, sizeof (buf));
2146 (void) printf(" %5s", buf);
2150 * Print out all the statistics for the given vdev. This can either be the
2151 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2152 * is a verbose output, and we don't want to display the toplevel pool stats.
2155 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2156 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2158 nvlist_t **oldchild, **newchild;
2160 vdev_stat_t *oldvs, *newvs;
2161 vdev_stat_t zerovs = { 0 };
2166 if (oldnv != NULL) {
2167 verify(nvlist_lookup_uint64_array(oldnv,
2168 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2173 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2174 (uint64_t **)&newvs, &c) == 0);
2176 if (strlen(name) + depth > cb->cb_namewidth)
2177 (void) printf("%*s%s", depth, "", name);
2179 (void) printf("%*s%s%*s", depth, "", name,
2180 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2182 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2187 scale = (double)NANOSEC / tdelta;
2189 /* only toplevel vdevs have capacity stats */
2190 if (newvs->vs_space == 0) {
2191 (void) printf(" - -");
2193 print_one_stat(newvs->vs_alloc);
2194 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2197 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2198 oldvs->vs_ops[ZIO_TYPE_READ])));
2200 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2201 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2203 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2204 oldvs->vs_bytes[ZIO_TYPE_READ])));
2206 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2207 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2209 (void) printf("\n");
2211 if (!cb->cb_verbose)
2214 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2215 &newchild, &children) != 0)
2218 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2219 &oldchild, &c) != 0)
2222 for (c = 0; c < children; c++) {
2223 uint64_t ishole = B_FALSE, islog = B_FALSE;
2225 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2228 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2231 if (ishole || islog)
2234 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2235 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2236 newchild[c], cb, depth + 2);
2241 * Log device section
2244 if (num_logs(newnv) > 0) {
2245 (void) printf("%-*s - - - - - "
2246 "-\n", cb->cb_namewidth, "logs");
2248 for (c = 0; c < children; c++) {
2249 uint64_t islog = B_FALSE;
2250 (void) nvlist_lookup_uint64(newchild[c],
2251 ZPOOL_CONFIG_IS_LOG, &islog);
2254 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2256 print_vdev_stats(zhp, vname, oldnv ?
2257 oldchild[c] : NULL, newchild[c],
2266 * Include level 2 ARC devices in iostat output
2268 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2269 &newchild, &children) != 0)
2272 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2273 &oldchild, &c) != 0)
2277 (void) printf("%-*s - - - - - "
2278 "-\n", cb->cb_namewidth, "cache");
2279 for (c = 0; c < children; c++) {
2280 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2282 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2283 newchild[c], cb, depth + 2);
2290 refresh_iostat(zpool_handle_t *zhp, void *data)
2292 iostat_cbdata_t *cb = data;
2296 * If the pool has disappeared, remove it from the list and continue.
2298 if (zpool_refresh_stats(zhp, &missing) != 0)
2302 pool_list_remove(cb->cb_list, zhp);
2308 * Callback to print out the iostats for the given pool.
2311 print_iostat(zpool_handle_t *zhp, void *data)
2313 iostat_cbdata_t *cb = data;
2314 nvlist_t *oldconfig, *newconfig;
2315 nvlist_t *oldnvroot, *newnvroot;
2317 newconfig = zpool_get_config(zhp, &oldconfig);
2319 if (cb->cb_iteration == 1)
2322 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2325 if (oldconfig == NULL)
2328 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2332 * Print out the statistics for the pool.
2334 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2337 print_iostat_separator(cb);
2343 get_namewidth(zpool_handle_t *zhp, void *data)
2345 iostat_cbdata_t *cb = data;
2346 nvlist_t *config, *nvroot;
2348 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2349 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2351 if (!cb->cb_verbose)
2352 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2354 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2358 * The width must fall into the range [10,38]. The upper limit is the
2359 * maximum we can have and still fit in 80 columns.
2361 if (cb->cb_namewidth < 10)
2362 cb->cb_namewidth = 10;
2363 if (cb->cb_namewidth > 38)
2364 cb->cb_namewidth = 38;
2370 * Parse the input string, get the 'interval' and 'count' value if there is one.
2373 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2376 unsigned long interval = 0, count = 0;
2377 int argc = *argcp, errno;
2380 * Determine if the last argument is an integer or a pool name
2382 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2386 interval = strtoul(argv[argc - 1], &end, 10);
2388 if (*end == '\0' && errno == 0) {
2389 if (interval == 0) {
2390 (void) fprintf(stderr, gettext("interval "
2391 "cannot be zero\n"));
2395 * Ignore the last parameter
2400 * If this is not a valid number, just plow on. The
2401 * user will get a more informative error message later
2409 * If the last argument is also an integer, then we have both a count
2412 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2417 interval = strtoul(argv[argc - 1], &end, 10);
2419 if (*end == '\0' && errno == 0) {
2420 if (interval == 0) {
2421 (void) fprintf(stderr, gettext("interval "
2422 "cannot be zero\n"));
2427 * Ignore the last parameter
2441 get_timestamp_arg(char c)
2444 timestamp_fmt = UDATE;
2446 timestamp_fmt = DDATE;
2452 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2454 * -v Display statistics for individual vdevs
2455 * -T Display a timestamp in date(1) or Unix format
2457 * This command can be tricky because we want to be able to deal with pool
2458 * creation/destruction as well as vdev configuration changes. The bulk of this
2459 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2460 * on pool_list_update() to detect the addition of new pools. Configuration
2461 * changes are all handled within libzfs.
2464 zpool_do_iostat(int argc, char **argv)
2469 unsigned long interval = 0, count = 0;
2471 boolean_t verbose = B_FALSE;
2475 while ((c = getopt(argc, argv, "T:v")) != -1) {
2478 get_timestamp_arg(*optarg);
2484 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2493 get_interval_count(&argc, argv, &interval, &count);
2496 * Construct the list of all interesting pools.
2499 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2502 if (pool_list_count(list) == 0 && argc != 0) {
2503 pool_list_free(list);
2507 if (pool_list_count(list) == 0 && interval == 0) {
2508 pool_list_free(list);
2509 (void) fprintf(stderr, gettext("no pools available\n"));
2514 * Enter the main iostat loop.
2517 cb.cb_verbose = verbose;
2518 cb.cb_iteration = 0;
2519 cb.cb_namewidth = 0;
2522 pool_list_update(list);
2524 if ((npools = pool_list_count(list)) == 0)
2528 * Refresh all statistics. This is done as an explicit step
2529 * before calculating the maximum name width, so that any
2530 * configuration changes are properly accounted for.
2532 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2535 * Iterate over all pools to determine the maximum width
2536 * for the pool / device name column across all pools.
2538 cb.cb_namewidth = 0;
2539 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2541 if (timestamp_fmt != NODATE)
2542 print_timestamp(timestamp_fmt);
2545 * If it's the first time, or verbose mode, print the header.
2547 if (++cb.cb_iteration == 1 || verbose)
2548 print_iostat_header(&cb);
2550 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2553 * If there's more than one pool, and we're not in verbose mode
2554 * (which prints a separator for us), then print a separator.
2556 if (npools > 1 && !verbose)
2557 print_iostat_separator(&cb);
2560 (void) printf("\n");
2563 * Flush the output so that redirection to a file isn't buffered
2566 (void) fflush(stdout);
2571 if (count != 0 && --count == 0)
2574 (void) sleep(interval);
2577 pool_list_free(list);
2582 typedef struct list_cbdata {
2583 boolean_t cb_scripted;
2585 zprop_list_t *cb_proplist;
2589 * Given a list of columns to display, output appropriate headers for each one.
2592 print_header(zprop_list_t *pl)
2595 boolean_t first = B_TRUE;
2596 boolean_t right_justify;
2598 for (; pl != NULL; pl = pl->pl_next) {
2599 if (pl->pl_prop == ZPROP_INVAL)
2607 header = zpool_prop_column_name(pl->pl_prop);
2608 right_justify = zpool_prop_align_right(pl->pl_prop);
2610 if (pl->pl_next == NULL && !right_justify)
2611 (void) printf("%s", header);
2612 else if (right_justify)
2613 (void) printf("%*s", pl->pl_width, header);
2615 (void) printf("%-*s", pl->pl_width, header);
2618 (void) printf("\n");
2622 * Given a pool and a list of properties, print out all the properties according
2623 * to the described layout.
2626 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2628 boolean_t first = B_TRUE;
2629 char property[ZPOOL_MAXPROPLEN];
2631 boolean_t right_justify;
2634 for (; pl != NULL; pl = pl->pl_next) {
2637 (void) printf("\t");
2644 right_justify = B_FALSE;
2645 if (pl->pl_prop != ZPROP_INVAL) {
2646 if (zpool_get_prop(zhp, pl->pl_prop, property,
2647 sizeof (property), NULL) != 0)
2652 right_justify = zpool_prop_align_right(pl->pl_prop);
2657 width = pl->pl_width;
2660 * If this is being called in scripted mode, or if this is the
2661 * last column and it is left-justified, don't include a width
2664 if (scripted || (pl->pl_next == NULL && !right_justify))
2665 (void) printf("%s", propstr);
2666 else if (right_justify)
2667 (void) printf("%*s", width, propstr);
2669 (void) printf("%-*s", width, propstr);
2672 (void) printf("\n");
2676 * Generic callback function to list a pool.
2679 list_callback(zpool_handle_t *zhp, void *data)
2681 list_cbdata_t *cbp = data;
2683 if (cbp->cb_first) {
2684 if (!cbp->cb_scripted)
2685 print_header(cbp->cb_proplist);
2686 cbp->cb_first = B_FALSE;
2689 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2695 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2697 * -H Scripted mode. Don't display headers, and separate properties
2699 * -o List of properties to display. Defaults to
2700 * "name,size,allocated,free,capacity,health,altroot"
2701 * -T Display a timestamp in date(1) or Unix format
2703 * List all pools in the system, whether or not they're healthy. Output space
2704 * statistics for each one, as well as health status summary.
2707 zpool_do_list(int argc, char **argv)
2711 list_cbdata_t cb = { 0 };
2712 static char default_props[] =
2713 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2714 char *props = default_props;
2715 unsigned long interval = 0, count = 0;
2718 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2721 cb.cb_scripted = B_TRUE;
2727 get_timestamp_arg(*optarg);
2730 (void) fprintf(stderr, gettext("missing argument for "
2731 "'%c' option\n"), optopt);
2735 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2744 get_interval_count(&argc, argv, &interval, &count);
2746 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2749 cb.cb_first = B_TRUE;
2753 if (timestamp_fmt != NODATE)
2754 print_timestamp(timestamp_fmt);
2756 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2757 list_callback, &cb);
2759 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2760 (void) printf(gettext("no pools available\n"));
2761 zprop_free_list(cb.cb_proplist);
2768 if (count != 0 && --count == 0)
2771 (void) sleep(interval);
2774 zprop_free_list(cb.cb_proplist);
2779 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2786 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2787 &child, &children) != 0) {
2788 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2789 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2790 name += sizeof(_PATH_DEV) - 1;
2791 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2792 path += sizeof(_PATH_DEV) - 1;
2793 if (strcmp(name, path) == 0)
2798 for (c = 0; c < children; c++)
2799 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2806 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2808 boolean_t force = B_FALSE;
2811 char *poolname, *old_disk, *new_disk;
2812 zpool_handle_t *zhp;
2816 while ((c = getopt(argc, argv, "f")) != -1) {
2822 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2831 /* get pool name and check number of arguments */
2833 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2840 (void) fprintf(stderr,
2841 gettext("missing <device> specification\n"));
2849 (void) fprintf(stderr,
2850 gettext("missing <new_device> specification\n"));
2853 new_disk = old_disk;
2863 (void) fprintf(stderr, gettext("too many arguments\n"));
2867 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2870 if (zpool_get_config(zhp, NULL) == NULL) {
2871 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2877 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2879 if (nvroot == NULL) {
2884 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2886 nvlist_free(nvroot);
2893 * zpool replace [-f] <pool> <device> <new_device>
2895 * -f Force attach, even if <new_device> appears to be in use.
2897 * Replace <device> with <new_device>.
2901 zpool_do_replace(int argc, char **argv)
2903 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2907 * zpool attach [-f] <pool> <device> <new_device>
2909 * -f Force attach, even if <new_device> appears to be in use.
2911 * Attach <new_device> to the mirror containing <device>. If <device> is not
2912 * part of a mirror, then <device> will be transformed into a mirror of
2913 * <device> and <new_device>. In either case, <new_device> will begin life
2914 * with a DTL of [0, now], and will immediately begin to resilver itself.
2917 zpool_do_attach(int argc, char **argv)
2919 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2923 * zpool detach [-f] <pool> <device>
2925 * -f Force detach of <device>, even if DTLs argue against it
2926 * (not supported yet)
2928 * Detach a device from a mirror. The operation will be refused if <device>
2929 * is the last device in the mirror, or if the DTLs indicate that this device
2930 * has the only valid copy of some data.
2934 zpool_do_detach(int argc, char **argv)
2937 char *poolname, *path;
2938 zpool_handle_t *zhp;
2942 while ((c = getopt(argc, argv, "f")) != -1) {
2946 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2955 /* get pool name and check number of arguments */
2957 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2962 (void) fprintf(stderr,
2963 gettext("missing <device> specification\n"));
2970 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2973 ret = zpool_vdev_detach(zhp, path);
2981 * zpool split [-n] [-o prop=val] ...
2983 * [-R altroot] <pool> <newpool> [<device> ...]
2985 * -n Do not split the pool, but display the resulting layout if
2986 * it were to be split.
2987 * -o Set property=value, or set mount options.
2988 * -R Mount the split-off pool under an alternate root.
2990 * Splits the named pool and gives it the new pool name. Devices to be split
2991 * off may be listed, provided that no more than one device is specified
2992 * per top-level vdev mirror. The newly split pool is left in an exported
2993 * state unless -R is specified.
2995 * Restrictions: the top-level of the pool pool must only be made up of
2996 * mirrors; all devices in the pool must be healthy; no device may be
2997 * undergoing a resilvering operation.
3000 zpool_do_split(int argc, char **argv)
3002 char *srcpool, *newpool, *propval;
3003 char *mntopts = NULL;
3006 zpool_handle_t *zhp;
3007 nvlist_t *config, *props = NULL;
3009 flags.dryrun = B_FALSE;
3010 flags.import = B_FALSE;
3013 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3016 flags.import = B_TRUE;
3018 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3019 &props, B_TRUE) != 0) {
3026 flags.dryrun = B_TRUE;
3029 if ((propval = strchr(optarg, '=')) != NULL) {
3032 if (add_prop_list(optarg, propval,
3033 &props, B_TRUE) != 0) {
3043 (void) fprintf(stderr, gettext("missing argument for "
3044 "'%c' option\n"), optopt);
3048 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3055 if (!flags.import && mntopts != NULL) {
3056 (void) fprintf(stderr, gettext("setting mntopts is only "
3057 "valid when importing the pool\n"));
3065 (void) fprintf(stderr, gettext("Missing pool name\n"));
3069 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3079 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3082 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3083 if (config == NULL) {
3087 (void) printf(gettext("would create '%s' with the "
3088 "following layout:\n\n"), newpool);
3089 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3091 nvlist_free(config);
3096 if (ret != 0 || flags.dryrun || !flags.import)
3100 * The split was successful. Now we need to open the new
3101 * pool and import it.
3103 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3105 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3106 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3108 (void) fprintf(stderr, gettext("Split was succssful, but "
3109 "the datasets could not all be mounted\n"));
3110 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3111 "different altroot\n"), "zpool import");
3121 * zpool online <pool> <device> ...
3124 zpool_do_online(int argc, char **argv)
3128 zpool_handle_t *zhp;
3130 vdev_state_t newstate;
3134 while ((c = getopt(argc, argv, "et")) != -1) {
3137 flags |= ZFS_ONLINE_EXPAND;
3141 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3150 /* get pool name and check number of arguments */
3152 (void) fprintf(stderr, gettext("missing pool name\n"));
3156 (void) fprintf(stderr, gettext("missing device name\n"));
3162 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3165 for (i = 1; i < argc; i++) {
3166 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3167 if (newstate != VDEV_STATE_HEALTHY) {
3168 (void) printf(gettext("warning: device '%s' "
3169 "onlined, but remains in faulted state\n"),
3171 if (newstate == VDEV_STATE_FAULTED)
3172 (void) printf(gettext("use 'zpool "
3173 "clear' to restore a faulted "
3176 (void) printf(gettext("use 'zpool "
3177 "replace' to replace devices "
3178 "that are no longer present\n"));
3191 * zpool offline [-ft] <pool> <device> ...
3193 * -f Force the device into the offline state, even if doing
3194 * so would appear to compromise pool availability.
3195 * (not supported yet)
3197 * -t Only take the device off-line temporarily. The offline
3198 * state will not be persistent across reboots.
3202 zpool_do_offline(int argc, char **argv)
3206 zpool_handle_t *zhp;
3208 boolean_t istmp = B_FALSE;
3211 while ((c = getopt(argc, argv, "ft")) != -1) {
3218 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3227 /* get pool name and check number of arguments */
3229 (void) fprintf(stderr, gettext("missing pool name\n"));
3233 (void) fprintf(stderr, gettext("missing device name\n"));
3239 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3242 for (i = 1; i < argc; i++) {
3243 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3253 * zpool clear <pool> [device]
3255 * Clear all errors associated with a pool or a particular device.
3258 zpool_do_clear(int argc, char **argv)
3262 boolean_t dryrun = B_FALSE;
3263 boolean_t do_rewind = B_FALSE;
3264 boolean_t xtreme_rewind = B_FALSE;
3265 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3266 nvlist_t *policy = NULL;
3267 zpool_handle_t *zhp;
3268 char *pool, *device;
3271 while ((c = getopt(argc, argv, "FnX")) != -1) {
3280 xtreme_rewind = B_TRUE;
3283 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3293 (void) fprintf(stderr, gettext("missing pool name\n"));
3298 (void) fprintf(stderr, gettext("too many arguments\n"));
3302 if ((dryrun || xtreme_rewind) && !do_rewind) {
3303 (void) fprintf(stderr,
3304 gettext("-n or -X only meaningful with -F\n"));
3308 rewind_policy = ZPOOL_TRY_REWIND;
3310 rewind_policy = ZPOOL_DO_REWIND;
3312 rewind_policy |= ZPOOL_EXTREME_REWIND;
3314 /* In future, further rewind policy choices can be passed along here */
3315 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3316 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3320 device = argc == 2 ? argv[1] : NULL;
3322 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3323 nvlist_free(policy);
3327 if (zpool_clear(zhp, device, policy) != 0)
3332 nvlist_free(policy);
3338 * zpool reguid <pool>
3341 zpool_do_reguid(int argc, char **argv)
3345 zpool_handle_t *zhp;
3349 while ((c = getopt(argc, argv, "")) != -1) {
3352 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3361 /* get pool name and check number of arguments */
3363 (void) fprintf(stderr, gettext("missing pool name\n"));
3368 (void) fprintf(stderr, gettext("too many arguments\n"));
3373 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3376 ret = zpool_reguid(zhp);
3383 typedef struct scrub_cbdata {
3390 scrub_callback(zpool_handle_t *zhp, void *data)
3392 scrub_cbdata_t *cb = data;
3396 * Ignore faulted pools.
3398 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3399 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3400 "currently unavailable\n"), zpool_get_name(zhp));
3404 err = zpool_scan(zhp, cb->cb_type);
3410 * zpool scrub [-s] <pool> ...
3412 * -s Stop. Stops any in-progress scrub.
3415 zpool_do_scrub(int argc, char **argv)
3420 cb.cb_type = POOL_SCAN_SCRUB;
3423 while ((c = getopt(argc, argv, "s")) != -1) {
3426 cb.cb_type = POOL_SCAN_NONE;
3429 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3441 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3445 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3448 typedef struct status_cbdata {
3450 boolean_t cb_allpools;
3451 boolean_t cb_verbose;
3452 boolean_t cb_explain;
3454 boolean_t cb_dedup_stats;
3458 * Print out detailed scrub status.
3461 print_scan_status(pool_scan_stat_t *ps)
3464 uint64_t elapsed, mins_left, hours_left;
3465 uint64_t pass_exam, examined, total;
3467 double fraction_done;
3468 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3470 (void) printf(gettext(" scan: "));
3472 /* If there's never been a scan, there's not much to say. */
3473 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3474 ps->pss_func >= POOL_SCAN_FUNCS) {
3475 (void) printf(gettext("none requested\n"));
3479 start = ps->pss_start_time;
3480 end = ps->pss_end_time;
3481 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3483 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3484 ps->pss_func == POOL_SCAN_RESILVER);
3486 * Scan is finished or canceled.
3488 if (ps->pss_state == DSS_FINISHED) {
3489 uint64_t minutes_taken = (end - start) / 60;
3492 if (ps->pss_func == POOL_SCAN_SCRUB) {
3493 fmt = gettext("scrub repaired %s in %lluh%um with "
3494 "%llu errors on %s");
3495 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3496 fmt = gettext("resilvered %s in %lluh%um with "
3497 "%llu errors on %s");
3500 (void) printf(fmt, processed_buf,
3501 (u_longlong_t)(minutes_taken / 60),
3502 (uint_t)(minutes_taken % 60),
3503 (u_longlong_t)ps->pss_errors,
3504 ctime((time_t *)&end));
3506 } else if (ps->pss_state == DSS_CANCELED) {
3507 if (ps->pss_func == POOL_SCAN_SCRUB) {
3508 (void) printf(gettext("scrub canceled on %s"),
3510 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3511 (void) printf(gettext("resilver canceled on %s"),
3517 assert(ps->pss_state == DSS_SCANNING);
3520 * Scan is in progress.
3522 if (ps->pss_func == POOL_SCAN_SCRUB) {
3523 (void) printf(gettext("scrub in progress since %s"),
3525 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3526 (void) printf(gettext("resilver in progress since %s"),
3530 examined = ps->pss_examined ? ps->pss_examined : 1;
3531 total = ps->pss_to_examine;
3532 fraction_done = (double)examined / total;
3534 /* elapsed time for this pass */
3535 elapsed = time(NULL) - ps->pss_pass_start;
3536 elapsed = elapsed ? elapsed : 1;
3537 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3538 rate = pass_exam / elapsed;
3539 rate = rate ? rate : 1;
3540 mins_left = ((total - examined) / rate) / 60;
3541 hours_left = mins_left / 60;
3543 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3544 zfs_nicenum(total, total_buf, sizeof (total_buf));
3545 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3548 * do not print estimated time if hours_left is more than 30 days
3550 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3551 examined_buf, total_buf, rate_buf);
3552 if (hours_left < (30 * 24)) {
3553 (void) printf(gettext(", %lluh%um to go\n"),
3554 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3556 (void) printf(gettext(
3557 ", (scan is slow, no estimated time)\n"));
3560 if (ps->pss_func == POOL_SCAN_RESILVER) {
3561 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3562 processed_buf, 100 * fraction_done);
3563 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3564 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3565 processed_buf, 100 * fraction_done);
3570 print_error_log(zpool_handle_t *zhp)
3572 nvlist_t *nverrlist = NULL;
3575 size_t len = MAXPATHLEN * 2;
3577 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3578 (void) printf("errors: List of errors unavailable "
3579 "(insufficient privileges)\n");
3583 (void) printf("errors: Permanent errors have been "
3584 "detected in the following files:\n\n");
3586 pathname = safe_malloc(len);
3588 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3590 uint64_t dsobj, obj;
3592 verify(nvpair_value_nvlist(elem, &nv) == 0);
3593 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3595 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3597 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3598 (void) printf("%7s %s\n", "", pathname);
3601 nvlist_free(nverrlist);
3605 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3614 (void) printf(gettext("\tspares\n"));
3616 for (i = 0; i < nspares; i++) {
3617 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3618 print_status_config(zhp, name, spares[i],
3619 namewidth, 2, B_TRUE);
3625 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3634 (void) printf(gettext("\tcache\n"));
3636 for (i = 0; i < nl2cache; i++) {
3637 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3638 print_status_config(zhp, name, l2cache[i],
3639 namewidth, 2, B_FALSE);
3645 print_dedup_stats(nvlist_t *config)
3647 ddt_histogram_t *ddh;
3653 * If the pool was faulted then we may not have been able to
3654 * obtain the config. Otherwise, if have anything in the dedup
3655 * table continue processing the stats.
3657 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3658 (uint64_t **)&ddo, &c) != 0)
3661 (void) printf("\n");
3662 (void) printf(gettext(" dedup: "));
3663 if (ddo->ddo_count == 0) {
3664 (void) printf(gettext("no DDT entries\n"));
3668 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3669 (u_longlong_t)ddo->ddo_count,
3670 (u_longlong_t)ddo->ddo_dspace,
3671 (u_longlong_t)ddo->ddo_mspace);
3673 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3674 (uint64_t **)&dds, &c) == 0);
3675 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3676 (uint64_t **)&ddh, &c) == 0);
3677 zpool_dump_ddt(dds, ddh);
3681 * Display a summary of pool status. Displays a summary such as:
3685 * reason: One or more devices ...
3686 * see: http://www.sun.com/msg/ZFS-xxxx-01
3692 * When given the '-v' option, we print out the complete config. If the '-e'
3693 * option is specified, then we print out error rate information as well.
3696 status_callback(zpool_handle_t *zhp, void *data)
3698 status_cbdata_t *cbp = data;
3699 nvlist_t *config, *nvroot;
3706 config = zpool_get_config(zhp, NULL);
3707 reason = zpool_get_status(zhp, &msgid);
3712 * If we were given 'zpool status -x', only report those pools with
3715 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3716 if (!cbp->cb_allpools) {
3717 (void) printf(gettext("pool '%s' is healthy\n"),
3718 zpool_get_name(zhp));
3720 cbp->cb_first = B_FALSE;
3726 cbp->cb_first = B_FALSE;
3728 (void) printf("\n");
3730 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3732 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3733 (uint64_t **)&vs, &c) == 0);
3734 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3736 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3737 (void) printf(gettext(" state: %s\n"), health);
3740 case ZPOOL_STATUS_MISSING_DEV_R:
3741 (void) printf(gettext("status: One or more devices could not "
3742 "be opened. Sufficient replicas exist for\n\tthe pool to "
3743 "continue functioning in a degraded state.\n"));
3744 (void) printf(gettext("action: Attach the missing device and "
3745 "online it using 'zpool online'.\n"));
3748 case ZPOOL_STATUS_MISSING_DEV_NR:
3749 (void) printf(gettext("status: One or more devices could not "
3750 "be opened. There are insufficient\n\treplicas for the "
3751 "pool to continue functioning.\n"));
3752 (void) printf(gettext("action: Attach the missing device and "
3753 "online it using 'zpool online'.\n"));
3756 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3757 (void) printf(gettext("status: One or more devices could not "
3758 "be used because the label is missing or\n\tinvalid. "
3759 "Sufficient replicas exist for the pool to continue\n\t"
3760 "functioning in a degraded state.\n"));
3761 (void) printf(gettext("action: Replace the device using "
3762 "'zpool replace'.\n"));
3765 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3766 (void) printf(gettext("status: One or more devices could not "
3767 "be used because the label is missing \n\tor invalid. "
3768 "There are insufficient replicas for the pool to "
3769 "continue\n\tfunctioning.\n"));
3770 zpool_explain_recover(zpool_get_handle(zhp),
3771 zpool_get_name(zhp), reason, config);
3774 case ZPOOL_STATUS_FAILING_DEV:
3775 (void) printf(gettext("status: One or more devices has "
3776 "experienced an unrecoverable error. An\n\tattempt was "
3777 "made to correct the error. Applications are "
3779 (void) printf(gettext("action: Determine if the device needs "
3780 "to be replaced, and clear the errors\n\tusing "
3781 "'zpool clear' or replace the device with 'zpool "
3785 case ZPOOL_STATUS_OFFLINE_DEV:
3786 (void) printf(gettext("status: One or more devices has "
3787 "been taken offline by the administrator.\n\tSufficient "
3788 "replicas exist for the pool to continue functioning in "
3789 "a\n\tdegraded state.\n"));
3790 (void) printf(gettext("action: Online the device using "
3791 "'zpool online' or replace the device with\n\t'zpool "
3795 case ZPOOL_STATUS_REMOVED_DEV:
3796 (void) printf(gettext("status: One or more devices has "
3797 "been removed by the administrator.\n\tSufficient "
3798 "replicas exist for the pool to continue functioning in "
3799 "a\n\tdegraded state.\n"));
3800 (void) printf(gettext("action: Online the device using "
3801 "'zpool online' or replace the device with\n\t'zpool "
3805 case ZPOOL_STATUS_RESILVERING:
3806 (void) printf(gettext("status: One or more devices is "
3807 "currently being resilvered. The pool will\n\tcontinue "
3808 "to function, possibly in a degraded state.\n"));
3809 (void) printf(gettext("action: Wait for the resilver to "
3813 case ZPOOL_STATUS_CORRUPT_DATA:
3814 (void) printf(gettext("status: One or more devices has "
3815 "experienced an error resulting in data\n\tcorruption. "
3816 "Applications may be affected.\n"));
3817 (void) printf(gettext("action: Restore the file in question "
3818 "if possible. Otherwise restore the\n\tentire pool from "
3822 case ZPOOL_STATUS_CORRUPT_POOL:
3823 (void) printf(gettext("status: The pool metadata is corrupted "
3824 "and the pool cannot be opened.\n"));
3825 zpool_explain_recover(zpool_get_handle(zhp),
3826 zpool_get_name(zhp), reason, config);
3829 case ZPOOL_STATUS_VERSION_OLDER:
3830 (void) printf(gettext("status: The pool is formatted using an "
3831 "older on-disk format. The pool can\n\tstill be used, but "
3832 "some features are unavailable.\n"));
3833 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3834 "upgrade'. Once this is done, the\n\tpool will no longer "
3835 "be accessible on older software versions.\n"));
3838 case ZPOOL_STATUS_VERSION_NEWER:
3839 (void) printf(gettext("status: The pool has been upgraded to a "
3840 "newer, incompatible on-disk version.\n\tThe pool cannot "
3841 "be accessed on this system.\n"));
3842 (void) printf(gettext("action: Access the pool from a system "
3843 "running more recent software, or\n\trestore the pool from "
3847 case ZPOOL_STATUS_FAULTED_DEV_R:
3848 (void) printf(gettext("status: One or more devices are "
3849 "faulted in response to persistent errors.\n\tSufficient "
3850 "replicas exist for the pool to continue functioning "
3851 "in a\n\tdegraded state.\n"));
3852 (void) printf(gettext("action: Replace the faulted device, "
3853 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3856 case ZPOOL_STATUS_FAULTED_DEV_NR:
3857 (void) printf(gettext("status: One or more devices are "
3858 "faulted in response to persistent errors. There are "
3859 "insufficient replicas for the pool to\n\tcontinue "
3861 (void) printf(gettext("action: Destroy and re-create the pool "
3862 "from a backup source. Manually marking the device\n"
3863 "\trepaired using 'zpool clear' may allow some data "
3864 "to be recovered.\n"));
3867 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3868 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3869 (void) printf(gettext("status: One or more devices are "
3870 "faulted in response to IO failures.\n"));
3871 (void) printf(gettext("action: Make sure the affected devices "
3872 "are connected, then run 'zpool clear'.\n"));
3875 case ZPOOL_STATUS_BAD_LOG:
3876 (void) printf(gettext("status: An intent log record "
3877 "could not be read.\n"
3878 "\tWaiting for adminstrator intervention to fix the "
3879 "faulted pool.\n"));
3880 (void) printf(gettext("action: Either restore the affected "
3881 "device(s) and run 'zpool online',\n"
3882 "\tor ignore the intent log records by running "
3883 "'zpool clear'.\n"));
3888 * The remaining errors can't actually be generated, yet.
3890 assert(reason == ZPOOL_STATUS_OK);
3894 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3897 if (config != NULL) {
3900 nvlist_t **spares, **l2cache;
3901 uint_t nspares, nl2cache;
3902 pool_scan_stat_t *ps = NULL;
3904 (void) nvlist_lookup_uint64_array(nvroot,
3905 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3906 print_scan_status(ps);
3908 namewidth = max_width(zhp, nvroot, 0, 0);
3912 (void) printf(gettext("config:\n\n"));
3913 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3914 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3915 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3916 namewidth, 0, B_FALSE);
3918 if (num_logs(nvroot) > 0)
3919 print_logs(zhp, nvroot, namewidth, B_TRUE);
3920 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3921 &l2cache, &nl2cache) == 0)
3922 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3924 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3925 &spares, &nspares) == 0)
3926 print_spares(zhp, spares, nspares, namewidth);
3928 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3930 nvlist_t *nverrlist = NULL;
3933 * If the approximate error count is small, get a
3934 * precise count by fetching the entire log and
3935 * uniquifying the results.
3937 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3938 zpool_get_errlog(zhp, &nverrlist) == 0) {
3943 while ((elem = nvlist_next_nvpair(nverrlist,
3948 nvlist_free(nverrlist);
3950 (void) printf("\n");
3953 (void) printf(gettext("errors: No known data "
3955 else if (!cbp->cb_verbose)
3956 (void) printf(gettext("errors: %llu data "
3957 "errors, use '-v' for a list\n"),
3958 (u_longlong_t)nerr);
3960 print_error_log(zhp);
3963 if (cbp->cb_dedup_stats)
3964 print_dedup_stats(config);
3966 (void) printf(gettext("config: The configuration cannot be "
3974 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3976 * -v Display complete error logs
3977 * -x Display only pools with potential problems
3978 * -D Display dedup status (undocumented)
3979 * -T Display a timestamp in date(1) or Unix format
3981 * Describes the health status of all pools or some subset.
3984 zpool_do_status(int argc, char **argv)
3988 unsigned long interval = 0, count = 0;
3989 status_cbdata_t cb = { 0 };
3992 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3995 cb.cb_verbose = B_TRUE;
3998 cb.cb_explain = B_TRUE;
4001 cb.cb_dedup_stats = B_TRUE;
4004 get_timestamp_arg(*optarg);
4007 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4016 get_interval_count(&argc, argv, &interval, &count);
4019 cb.cb_allpools = B_TRUE;
4021 cb.cb_first = B_TRUE;
4024 if (timestamp_fmt != NODATE)
4025 print_timestamp(timestamp_fmt);
4027 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4028 status_callback, &cb);
4030 if (argc == 0 && cb.cb_count == 0)
4031 (void) printf(gettext("no pools available\n"));
4032 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4033 (void) printf(gettext("all pools are healthy\n"));
4041 if (count != 0 && --count == 0)
4044 (void) sleep(interval);
4050 typedef struct upgrade_cbdata {
4054 char cb_poolname[ZPOOL_MAXNAMELEN];
4056 uint64_t cb_version;
4061 is_root_pool(zpool_handle_t *zhp)
4063 static struct statfs sfs;
4064 static char *poolname = NULL;
4065 static boolean_t stated = B_FALSE;
4070 if (statfs("/", &sfs) == -1) {
4071 (void) fprintf(stderr,
4072 "Unable to stat root file system: %s.\n",
4076 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4078 poolname = sfs.f_mntfromname;
4079 if ((slash = strchr(poolname, '/')) != NULL)
4082 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4086 upgrade_cb(zpool_handle_t *zhp, void *arg)
4088 upgrade_cbdata_t *cbp = arg;
4093 config = zpool_get_config(zhp, NULL);
4094 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4097 if (!cbp->cb_newer && version < SPA_VERSION) {
4099 if (cbp->cb_first) {
4100 (void) printf(gettext("The following pools are "
4101 "out of date, and can be upgraded. After "
4102 "being\nupgraded, these pools will no "
4103 "longer be accessible by older software "
4105 (void) printf(gettext("VER POOL\n"));
4106 (void) printf(gettext("--- ------------\n"));
4107 cbp->cb_first = B_FALSE;
4110 (void) printf("%2llu %s\n", (u_longlong_t)version,
4111 zpool_get_name(zhp));
4113 cbp->cb_first = B_FALSE;
4114 ret = zpool_upgrade(zhp, cbp->cb_version);
4116 (void) printf(gettext("Successfully upgraded "
4117 "'%s'\n\n"), zpool_get_name(zhp));
4118 if (cbp->cb_poolname[0] == '\0' &&
4119 is_root_pool(zhp)) {
4120 (void) strlcpy(cbp->cb_poolname,
4121 zpool_get_name(zhp),
4122 sizeof(cbp->cb_poolname));
4126 } else if (cbp->cb_newer && version > SPA_VERSION) {
4127 assert(!cbp->cb_all);
4129 if (cbp->cb_first) {
4130 (void) printf(gettext("The following pools are "
4131 "formatted using a newer software version and\n"
4132 "cannot be accessed on the current system.\n\n"));
4133 (void) printf(gettext("VER POOL\n"));
4134 (void) printf(gettext("--- ------------\n"));
4135 cbp->cb_first = B_FALSE;
4138 (void) printf("%2llu %s\n", (u_longlong_t)version,
4139 zpool_get_name(zhp));
4148 upgrade_one(zpool_handle_t *zhp, void *data)
4150 upgrade_cbdata_t *cbp = data;
4151 uint64_t cur_version;
4154 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4155 (void) printf(gettext("'log' is now a reserved word\n"
4156 "Pool 'log' must be renamed using export and import"
4161 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4162 if (cur_version > cbp->cb_version) {
4163 (void) printf(gettext("Pool '%s' is already formatted "
4164 "using more current version '%llu'.\n"),
4165 zpool_get_name(zhp), cur_version);
4168 if (cur_version == cbp->cb_version) {
4169 (void) printf(gettext("Pool '%s' is already formatted "
4170 "using the current version.\n"), zpool_get_name(zhp));
4174 ret = zpool_upgrade(zhp, cbp->cb_version);
4177 (void) printf(gettext("Successfully upgraded '%s' "
4178 "from version %llu to version %llu\n\n"),
4179 zpool_get_name(zhp), (u_longlong_t)cur_version,
4180 (u_longlong_t)cbp->cb_version);
4181 if (cbp->cb_poolname[0] == '\0' && is_root_pool(zhp)) {
4182 (void) strlcpy(cbp->cb_poolname, zpool_get_name(zhp),
4183 sizeof(cbp->cb_poolname));
4193 * zpool upgrade [-V version] <-a | pool ...>
4195 * With no arguments, display downrev'd ZFS pool available for upgrade.
4196 * Individual pools can be upgraded by specifying the pool, and '-a' will
4197 * upgrade all pools.
4200 zpool_do_upgrade(int argc, char **argv)
4203 upgrade_cbdata_t cb = { 0 };
4205 boolean_t showversions = B_FALSE;
4210 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4216 showversions = B_TRUE;
4219 cb.cb_version = strtoll(optarg, &end, 10);
4220 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4221 cb.cb_version < SPA_VERSION_1) {
4222 (void) fprintf(stderr,
4223 gettext("invalid version '%s'\n"), optarg);
4228 (void) fprintf(stderr, gettext("missing argument for "
4229 "'%c' option\n"), optopt);
4233 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4244 if (cb.cb_version == 0) {
4245 cb.cb_version = SPA_VERSION;
4246 } else if (!cb.cb_all && argc == 0) {
4247 (void) fprintf(stderr, gettext("-V option is "
4248 "incompatible with other arguments\n"));
4253 if (cb.cb_all || argc != 0) {
4254 (void) fprintf(stderr, gettext("-v option is "
4255 "incompatible with other arguments\n"));
4258 } else if (cb.cb_all) {
4260 (void) fprintf(stderr, gettext("-a option should not "
4261 "be used along with a pool name\n"));
4266 (void) printf(gettext("This system is currently running "
4267 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4268 cb.cb_first = B_TRUE;
4270 (void) printf(gettext("The following versions are "
4272 (void) printf(gettext("VER DESCRIPTION\n"));
4273 (void) printf("--- -----------------------------------------"
4274 "---------------\n");
4275 (void) printf(gettext(" 1 Initial ZFS version\n"));
4276 (void) printf(gettext(" 2 Ditto blocks "
4277 "(replicated metadata)\n"));
4278 (void) printf(gettext(" 3 Hot spares and double parity "
4280 (void) printf(gettext(" 4 zpool history\n"));
4281 (void) printf(gettext(" 5 Compression using the gzip "
4283 (void) printf(gettext(" 6 bootfs pool property\n"));
4284 (void) printf(gettext(" 7 Separate intent log devices\n"));
4285 (void) printf(gettext(" 8 Delegated administration\n"));
4286 (void) printf(gettext(" 9 refquota and refreservation "
4288 (void) printf(gettext(" 10 Cache devices\n"));
4289 (void) printf(gettext(" 11 Improved scrub performance\n"));
4290 (void) printf(gettext(" 12 Snapshot properties\n"));
4291 (void) printf(gettext(" 13 snapused property\n"));
4292 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4293 (void) printf(gettext(" 15 user/group space accounting\n"));
4294 (void) printf(gettext(" 16 stmf property support\n"));
4295 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4296 (void) printf(gettext(" 18 Snapshot user holds\n"));
4297 (void) printf(gettext(" 19 Log device removal\n"));
4298 (void) printf(gettext(" 20 Compression using zle "
4299 "(zero-length encoding)\n"));
4300 (void) printf(gettext(" 21 Deduplication\n"));
4301 (void) printf(gettext(" 22 Received properties\n"));
4302 (void) printf(gettext(" 23 Slim ZIL\n"));
4303 (void) printf(gettext(" 24 System attributes\n"));
4304 (void) printf(gettext(" 25 Improved scrub stats\n"));
4305 (void) printf(gettext(" 26 Improved snapshot deletion "
4307 (void) printf(gettext(" 27 Improved snapshot creation "
4309 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4310 (void) printf(gettext("\nFor more information on a particular "
4311 "version, including supported releases,\n"));
4312 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4313 } else if (argc == 0) {
4316 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4317 notfound = cb.cb_first;
4319 if (!cb.cb_all && ret == 0) {
4321 (void) printf("\n");
4322 cb.cb_first = B_TRUE;
4323 cb.cb_newer = B_TRUE;
4324 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4327 (void) printf("\n");
4333 (void) printf(gettext("All pools are formatted "
4334 "using this version.\n"));
4335 else if (!cb.cb_all)
4336 (void) printf(gettext("Use 'zpool upgrade -v' "
4337 "for a list of available versions and "
4338 "their associated\nfeatures.\n"));
4341 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4345 if (cb.cb_poolname[0] != '\0') {
4347 "If you boot from pool '%s', don't forget to update boot code.\n"
4348 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4349 "the following command will do it:\n"
4351 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4358 typedef struct hist_cbdata {
4365 * Print out the command history for a specific pool.
4368 get_history_one(zpool_handle_t *zhp, void *data)
4384 char internalstr[MAXPATHLEN];
4385 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4389 cb->first = B_FALSE;
4391 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4393 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4396 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4397 &records, &numrecords) == 0);
4398 for (i = 0; i < numrecords; i++) {
4399 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4403 /* is it an internal event or a standard event? */
4404 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4406 if (cb->internal == 0)
4409 if (nvlist_lookup_uint64(records[i],
4410 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4412 verify(nvlist_lookup_uint64(records[i],
4413 ZPOOL_HIST_TXG, &txg) == 0);
4414 verify(nvlist_lookup_string(records[i],
4415 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4416 if (ievent >= LOG_END)
4418 (void) snprintf(internalstr,
4419 sizeof (internalstr),
4420 "[internal %s txg:%lld] %s",
4421 zfs_history_event_names[ievent], txg,
4423 cmdstr = internalstr;
4426 (void) localtime_r(&tsec, &t);
4427 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4428 (void) printf("%s %s", tbuf, cmdstr);
4431 (void) printf("\n");
4434 (void) printf(" [");
4435 if (nvlist_lookup_uint64(records[i],
4436 ZPOOL_HIST_WHO, &who) == 0) {
4437 pwd = getpwuid((uid_t)who);
4439 (void) printf("user %s on",
4442 (void) printf("user %d on",
4445 (void) printf(gettext("no info]\n"));
4448 if (nvlist_lookup_string(records[i],
4449 ZPOOL_HIST_HOST, &hostname) == 0) {
4450 (void) printf(" %s", hostname);
4452 if (nvlist_lookup_string(records[i],
4453 ZPOOL_HIST_ZONE, &zonename) == 0) {
4454 (void) printf(":%s", zonename);
4458 (void) printf("\n");
4460 (void) printf("\n");
4467 * zpool history <pool>
4469 * Displays the history of commands that modified pools.
4474 zpool_do_history(int argc, char **argv)
4476 hist_cbdata_t cbdata = { 0 };
4480 cbdata.first = B_TRUE;
4482 while ((c = getopt(argc, argv, "li")) != -1) {
4488 cbdata.internal = 1;
4491 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4499 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4502 if (argc == 0 && cbdata.first == B_TRUE) {
4503 (void) printf(gettext("no pools available\n"));
4511 get_callback(zpool_handle_t *zhp, void *data)
4513 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4514 char value[MAXNAMELEN];
4515 zprop_source_t srctype;
4518 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4521 * Skip the special fake placeholder. This will also skip
4522 * over the name property when 'all' is specified.
4524 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4525 pl == cbp->cb_proplist)
4528 if (zpool_get_prop(zhp, pl->pl_prop,
4529 value, sizeof (value), &srctype) != 0)
4532 zprop_print_one_property(zpool_get_name(zhp), cbp,
4533 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4540 zpool_do_get(int argc, char **argv)
4542 zprop_get_cbdata_t cb = { 0 };
4543 zprop_list_t fake_name = { 0 };
4549 cb.cb_first = B_TRUE;
4550 cb.cb_sources = ZPROP_SRC_ALL;
4551 cb.cb_columns[0] = GET_COL_NAME;
4552 cb.cb_columns[1] = GET_COL_PROPERTY;
4553 cb.cb_columns[2] = GET_COL_VALUE;
4554 cb.cb_columns[3] = GET_COL_SOURCE;
4555 cb.cb_type = ZFS_TYPE_POOL;
4557 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4558 ZFS_TYPE_POOL) != 0)
4561 if (cb.cb_proplist != NULL) {
4562 fake_name.pl_prop = ZPOOL_PROP_NAME;
4563 fake_name.pl_width = strlen(gettext("NAME"));
4564 fake_name.pl_next = cb.cb_proplist;
4565 cb.cb_proplist = &fake_name;
4568 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4571 if (cb.cb_proplist == &fake_name)
4572 zprop_free_list(fake_name.pl_next);
4574 zprop_free_list(cb.cb_proplist);
4579 typedef struct set_cbdata {
4582 boolean_t cb_any_successful;
4586 set_callback(zpool_handle_t *zhp, void *data)
4589 set_cbdata_t *cb = (set_cbdata_t *)data;
4591 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4594 cb->cb_any_successful = B_TRUE;
4600 zpool_do_set(int argc, char **argv)
4602 set_cbdata_t cb = { 0 };
4605 if (argc > 1 && argv[1][0] == '-') {
4606 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4612 (void) fprintf(stderr, gettext("missing property=value "
4618 (void) fprintf(stderr, gettext("missing pool name\n"));
4623 (void) fprintf(stderr, gettext("too many pool names\n"));
4627 cb.cb_propname = argv[1];
4628 cb.cb_value = strchr(cb.cb_propname, '=');
4629 if (cb.cb_value == NULL) {
4630 (void) fprintf(stderr, gettext("missing value in "
4631 "property=value argument\n"));
4635 *(cb.cb_value) = '\0';
4638 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4645 find_command_idx(char *command, int *idx)
4649 for (i = 0; i < NCOMMAND; i++) {
4650 if (command_table[i].name == NULL)
4653 if (strcmp(command, command_table[i].name) == 0) {
4662 main(int argc, char **argv)
4668 (void) setlocale(LC_ALL, "");
4669 (void) textdomain(TEXT_DOMAIN);
4671 if ((g_zfs = libzfs_init()) == NULL) {
4672 (void) fprintf(stderr, gettext("internal error: failed to "
4673 "initialize ZFS library\n"));
4677 libzfs_print_on_error(g_zfs, B_TRUE);
4682 * Make sure the user has specified some command.
4685 (void) fprintf(stderr, gettext("missing command\n"));
4694 if (strcmp(cmdname, "-?") == 0)
4697 zpool_set_history_str("zpool", argc, argv, history_str);
4698 verify(zpool_stage_history(g_zfs, history_str) == 0);
4701 * Run the appropriate command.
4703 if (find_command_idx(cmdname, &i) == 0) {
4704 current_command = &command_table[i];
4705 ret = command_table[i].func(argc - 1, argv + 1);
4706 } else if (strchr(cmdname, '=')) {
4707 verify(find_command_idx("set", &i) == 0);
4708 current_command = &command_table[i];
4709 ret = command_table[i].func(argc, argv);
4710 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4712 * 'freeze' is a vile debugging abomination, so we treat
4716 int fd = open(ZFS_DEV, O_RDWR);
4717 (void) strcpy((void *)buf, argv[2]);
4718 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4720 (void) fprintf(stderr, gettext("unrecognized "
4721 "command '%s'\n"), cmdname);
4728 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4729 * for the purposes of running ::findleaks.
4731 if (getenv("ZFS_ABORT") != NULL) {
4732 (void) printf("dumping core by request\n");