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 (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
26 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
52 #include <sys/fs/zfs.h>
57 #include "zpool_util.h"
58 #include "zfs_comutil.h"
59 #include "zfeature_common.h"
61 #include "statcommon.h"
63 static int zpool_do_create(int, char **);
64 static int zpool_do_destroy(int, char **);
66 static int zpool_do_add(int, char **);
67 static int zpool_do_remove(int, char **);
68 static int zpool_do_labelclear(int, char **);
70 static int zpool_do_list(int, char **);
71 static int zpool_do_iostat(int, char **);
72 static int zpool_do_status(int, char **);
74 static int zpool_do_online(int, char **);
75 static int zpool_do_offline(int, char **);
76 static int zpool_do_clear(int, char **);
77 static int zpool_do_reopen(int, char **);
79 static int zpool_do_reguid(int, char **);
81 static int zpool_do_attach(int, char **);
82 static int zpool_do_detach(int, char **);
83 static int zpool_do_replace(int, char **);
84 static int zpool_do_split(int, char **);
86 static int zpool_do_scrub(int, char **);
88 static int zpool_do_import(int, char **);
89 static int zpool_do_export(int, char **);
91 static int zpool_do_upgrade(int, char **);
93 static int zpool_do_history(int, char **);
95 static int zpool_do_get(int, char **);
96 static int zpool_do_set(int, char **);
99 * These libumem hooks provide a reasonable set of defaults for the allocator's
100 * debugging facilities.
105 _umem_debug_init(void)
107 return ("default,verbose"); /* $UMEM_DEBUG setting */
111 _umem_logging_init(void)
113 return ("fail,contents"); /* $UMEM_LOGGING setting */
145 typedef struct zpool_command {
147 int (*func)(int, char **);
152 * Master command table. Each ZFS command has a name, associated function, and
153 * usage message. The usage messages need to be internationalized, so we have
154 * to have a function to return the usage message based on a command index.
156 * These commands are organized according to how they are displayed in the usage
157 * message. An empty command (one with a NULL name) indicates an empty line in
158 * the generic usage message.
160 static zpool_command_t command_table[] = {
161 { "create", zpool_do_create, HELP_CREATE },
162 { "destroy", zpool_do_destroy, HELP_DESTROY },
164 { "add", zpool_do_add, HELP_ADD },
165 { "remove", zpool_do_remove, HELP_REMOVE },
167 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
169 { "list", zpool_do_list, HELP_LIST },
170 { "iostat", zpool_do_iostat, HELP_IOSTAT },
171 { "status", zpool_do_status, HELP_STATUS },
173 { "online", zpool_do_online, HELP_ONLINE },
174 { "offline", zpool_do_offline, HELP_OFFLINE },
175 { "clear", zpool_do_clear, HELP_CLEAR },
176 { "reopen", zpool_do_reopen, HELP_REOPEN },
178 { "attach", zpool_do_attach, HELP_ATTACH },
179 { "detach", zpool_do_detach, HELP_DETACH },
180 { "replace", zpool_do_replace, HELP_REPLACE },
181 { "split", zpool_do_split, HELP_SPLIT },
183 { "scrub", zpool_do_scrub, HELP_SCRUB },
185 { "import", zpool_do_import, HELP_IMPORT },
186 { "export", zpool_do_export, HELP_EXPORT },
187 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
188 { "reguid", zpool_do_reguid, HELP_REGUID },
190 { "history", zpool_do_history, HELP_HISTORY },
191 { "get", zpool_do_get, HELP_GET },
192 { "set", zpool_do_set, HELP_SET },
195 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
197 static zpool_command_t *current_command;
198 static char history_str[HIS_MAX_RECORD_LEN];
199 static boolean_t log_history = B_TRUE;
200 static uint_t timestamp_fmt = NODATE;
203 get_usage(zpool_help_t idx)
207 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
209 return (gettext("\tattach [-f] <pool> <device> "
212 return (gettext("\tclear [-nF] <pool> [device]\n"));
214 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
215 "\t [-O file-system-property=value] ...\n"
216 "\t [-m mountpoint] [-R root] [-t tempname] "
217 "<pool> <vdev> ...\n"));
219 return (gettext("\tdestroy [-f] <pool>\n"));
221 return (gettext("\tdetach <pool> <device>\n"));
223 return (gettext("\texport [-f] <pool> ...\n"));
225 return (gettext("\thistory [-il] [<pool>] ...\n"));
227 return (gettext("\timport [-d dir] [-D]\n"
228 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
229 "\timport [-o mntopts] [-o property=value] ... \n"
230 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
231 "[-R root] [-F [-n]] -a\n"
232 "\timport [-o mntopts] [-o property=value] ... \n"
233 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
234 "[-R root] [-F [-n]] [-t]\n"
235 "\t <pool | id> [newpool]\n"));
237 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
239 case HELP_LABELCLEAR:
240 return (gettext("\tlabelclear [-f] <vdev>\n"));
242 return (gettext("\tlist [-Hpv] [-o property[,...]] "
243 "[-T d|u] [pool] ... [interval [count]]\n"));
245 return (gettext("\toffline [-t] <pool> <device> ...\n"));
247 return (gettext("\tonline [-e] <pool> <device> ...\n"));
249 return (gettext("\treplace [-f] <pool> <device> "
252 return (gettext("\tremove <pool> <device> ...\n"));
254 return (gettext("\treopen <pool>\n"));
256 return (gettext("\tscrub [-s] <pool> ...\n"));
258 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
261 return (gettext("\tupgrade [-v]\n"
262 "\tupgrade [-V version] <-a | pool ...>\n"));
264 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
265 "<\"all\" | property[,...]> <pool> ...\n"));
267 return (gettext("\tset <property=value> <pool> \n"));
269 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
270 "\t [-o property=value] <pool> <newpool> "
271 "[<device> ...]\n"));
273 return (gettext("\treguid <pool>\n"));
282 * Callback routine that will print out a pool property value.
285 print_prop_cb(int prop, void *cb)
289 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
291 if (zpool_prop_readonly(prop))
292 (void) fprintf(fp, " NO ");
294 (void) fprintf(fp, " YES ");
296 if (zpool_prop_values(prop) == NULL)
297 (void) fprintf(fp, "-\n");
299 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
305 * Display usage message. If we're inside a command, display only the usage for
306 * that command. Otherwise, iterate over the entire command table and display
307 * a complete usage message.
310 usage(boolean_t requested)
312 FILE *fp = requested ? stdout : stderr;
314 if (current_command == NULL) {
317 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
319 gettext("where 'command' is one of the following:\n\n"));
321 for (i = 0; i < NCOMMAND; i++) {
322 if (command_table[i].name == NULL)
323 (void) fprintf(fp, "\n");
325 (void) fprintf(fp, "%s",
326 get_usage(command_table[i].usage));
329 (void) fprintf(fp, gettext("usage:\n"));
330 (void) fprintf(fp, "%s", get_usage(current_command->usage));
333 if (current_command != NULL &&
334 ((strcmp(current_command->name, "set") == 0) ||
335 (strcmp(current_command->name, "get") == 0) ||
336 (strcmp(current_command->name, "list") == 0))) {
339 gettext("\nthe following properties are supported:\n"));
341 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
342 "PROPERTY", "EDIT", "VALUES");
344 /* Iterate over all properties */
345 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
348 (void) fprintf(fp, "\t%-15s ", "feature@...");
349 (void) fprintf(fp, "YES disabled | enabled | active\n");
351 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
352 "appended with a feature name.\nSee zpool-features(7).\n"));
356 * See comments at end of main().
358 if (getenv("ZFS_ABORT") != NULL) {
359 (void) printf("dumping core by request\n");
363 exit(requested ? 0 : 2);
367 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
368 boolean_t print_logs)
375 (void) printf("\t%*s%s\n", indent, "", name);
377 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
378 &child, &children) != 0)
381 for (c = 0; c < children; c++) {
382 uint64_t is_log = B_FALSE;
384 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
386 if ((is_log && !print_logs) || (!is_log && print_logs))
389 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
390 print_vdev_tree(zhp, vname, child[c], indent + 2,
397 prop_list_contains_feature(nvlist_t *proplist)
400 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
401 nvp = nvlist_next_nvpair(proplist, nvp)) {
402 if (zpool_prop_feature(nvpair_name(nvp)))
409 * Add a property pair (name, string-value) into a property nvlist.
412 add_prop_list(const char *propname, char *propval, nvlist_t **props,
415 zpool_prop_t prop = ZPROP_INVAL;
421 if (*props == NULL &&
422 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
423 (void) fprintf(stderr,
424 gettext("internal error: out of memory\n"));
431 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
433 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
434 !zpool_prop_feature(propname)) {
435 (void) fprintf(stderr, gettext("property '%s' is "
436 "not a valid pool property\n"), propname);
441 * feature@ properties and version should not be specified
444 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
445 nvlist_exists(proplist, vname)) ||
446 (prop == ZPOOL_PROP_VERSION &&
447 prop_list_contains_feature(proplist))) {
448 (void) fprintf(stderr, gettext("'feature@' and "
449 "'version' properties cannot be specified "
455 if (zpool_prop_feature(propname))
458 normnm = zpool_prop_to_name(prop);
460 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
461 normnm = zfs_prop_to_name(fprop);
467 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
468 prop != ZPOOL_PROP_CACHEFILE) {
469 (void) fprintf(stderr, gettext("property '%s' "
470 "specified multiple times\n"), propname);
474 if (nvlist_add_string(proplist, normnm, propval) != 0) {
475 (void) fprintf(stderr, gettext("internal "
476 "error: out of memory\n"));
484 * Set a default property pair (name, string-value) in a property nvlist
487 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
492 if (nvlist_lookup_string(*props, propname, &pval) == 0)
495 return (add_prop_list(propname, propval, props, poolprop));
499 * zpool add [-fn] <pool> <vdev> ...
501 * -f Force addition of devices, even if they appear in use
502 * -n Do not add the devices, but display the resulting layout if
503 * they were to be added.
505 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
506 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
510 zpool_do_add(int argc, char **argv)
512 boolean_t force = B_FALSE;
513 boolean_t dryrun = B_FALSE;
522 while ((c = getopt(argc, argv, "fn")) != -1) {
531 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
540 /* get pool name and check number of arguments */
542 (void) fprintf(stderr, gettext("missing pool name argument\n"));
546 (void) fprintf(stderr, gettext("missing vdev specification\n"));
555 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
558 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
559 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
565 /* pass off to get_vdev_spec for processing */
566 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
568 if (nvroot == NULL) {
574 nvlist_t *poolnvroot;
576 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
579 (void) printf(gettext("would update '%s' to the following "
580 "configuration:\n"), zpool_get_name(zhp));
582 /* print original main pool and new tree */
583 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
584 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
586 /* Do the same for the logs */
587 if (num_logs(poolnvroot) > 0) {
588 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
589 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
590 } else if (num_logs(nvroot) > 0) {
591 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
596 ret = (zpool_add(zhp, nvroot) != 0);
606 * zpool remove <pool> <vdev> ...
608 * Removes the given vdev from the pool. Currently, this supports removing
609 * spares, cache, and log devices from the pool.
612 zpool_do_remove(int argc, char **argv)
621 /* get pool name and check number of arguments */
623 (void) fprintf(stderr, gettext("missing pool name argument\n"));
627 (void) fprintf(stderr, gettext("missing device\n"));
633 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
636 for (i = 1; i < argc; i++) {
637 if (zpool_vdev_remove(zhp, argv[i]) != 0)
645 * zpool labelclear [-f] <vdev>
647 * -f Force clearing the label for the vdevs which are members of
648 * the exported or foreign pools.
650 * Verifies that the vdev is not active and zeros out the label information
654 zpool_do_labelclear(int argc, char **argv)
656 char vdev[MAXPATHLEN];
662 boolean_t inuse = B_FALSE;
663 boolean_t force = B_FALSE;
666 while ((c = getopt(argc, argv, "f")) != -1) {
672 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
683 (void) fprintf(stderr, gettext("missing vdev name\n"));
687 (void) fprintf(stderr, gettext("too many arguments\n"));
692 * Check if we were given absolute path and use it as is.
693 * Otherwise if the provided vdev name doesn't point to a file,
694 * try prepending dsk path and appending s0.
696 (void) strlcpy(vdev, argv[0], sizeof (vdev));
697 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
700 (void) snprintf(vdev, sizeof (vdev), "%s/%s",
702 ZFS_DISK_ROOT, argv[0]);
703 if ((s = strrchr(argv[0], 's')) == NULL ||
705 (void) strlcat(vdev, "s0", sizeof (vdev));
709 if (stat(vdev, &st) != 0) {
710 (void) fprintf(stderr, gettext(
711 "failed to find device %s, try specifying absolute "
712 "path instead\n"), argv[0]);
717 if ((fd = open(vdev, O_RDWR)) < 0) {
718 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
719 vdev, strerror(errno));
723 if (zpool_read_label(fd, &config) != 0) {
724 (void) fprintf(stderr,
725 gettext("failed to read label from %s\n"), vdev);
730 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
732 (void) fprintf(stderr,
733 gettext("failed to check state for %s\n"), vdev);
742 case POOL_STATE_ACTIVE:
743 case POOL_STATE_SPARE:
744 case POOL_STATE_L2CACHE:
745 (void) fprintf(stderr, gettext(
746 "%s is a member (%s) of pool \"%s\"\n"),
747 vdev, zpool_pool_state_to_name(state), name);
751 case POOL_STATE_EXPORTED:
754 (void) fprintf(stderr, gettext(
755 "use '-f' to override the following error:\n"
756 "%s is a member of exported pool \"%s\"\n"),
761 case POOL_STATE_POTENTIALLY_ACTIVE:
764 (void) fprintf(stderr, gettext(
765 "use '-f' to override the following error:\n"
766 "%s is a member of potentially active pool \"%s\"\n"),
771 case POOL_STATE_DESTROYED:
772 /* inuse should never be set for a destroyed pool */
778 ret = zpool_clear_label(fd);
780 (void) fprintf(stderr,
781 gettext("failed to clear label for %s\n"), vdev);
792 * zpool create [-fnd] [-o property=value] ...
793 * [-O file-system-property=value] ...
794 * [-R root] [-m mountpoint] [-t tempname] <pool> <dev> ...
796 * -f Force creation, even if devices appear in use
797 * -n Do not create the pool, but display the resulting layout if it
798 * were to be created.
799 * -R Create a pool under an alternate root
800 * -m Set default mountpoint for the root dataset. By default it's
802 * -t Use the temporary name until the pool is exported.
803 * -o Set property=value.
804 * -d Don't automatically enable all supported pool features
805 * (individual features can be enabled with -o).
806 * -O Set fsproperty=value in the pool's root file system
808 * Creates the named pool according to the given vdev specification. The
809 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
810 * we get the nvlist back from get_vdev_spec(), we either print out the contents
811 * (if '-n' was specified), or pass it to libzfs to do the creation.
814 zpool_do_create(int argc, char **argv)
816 boolean_t force = B_FALSE;
817 boolean_t dryrun = B_FALSE;
818 boolean_t enable_all_pool_feat = B_TRUE;
820 nvlist_t *nvroot = NULL;
824 char *altroot = NULL;
825 char *mountpoint = NULL;
826 nvlist_t *fsprops = NULL;
827 nvlist_t *props = NULL;
831 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
840 enable_all_pool_feat = B_FALSE;
844 if (add_prop_list(zpool_prop_to_name(
845 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
847 if (add_prop_list_default(zpool_prop_to_name(
848 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
852 /* Equivalent to -O mountpoint=optarg */
856 if ((propval = strchr(optarg, '=')) == NULL) {
857 (void) fprintf(stderr, gettext("missing "
858 "'=' for -o option\n"));
864 if (add_prop_list(optarg, propval, &props, B_TRUE))
868 * If the user is creating a pool that doesn't support
869 * feature flags, don't enable any features.
871 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
875 ver = strtoull(propval, &end, 10);
877 ver < SPA_VERSION_FEATURES) {
878 enable_all_pool_feat = B_FALSE;
881 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
885 if ((propval = strchr(optarg, '=')) == NULL) {
886 (void) fprintf(stderr, gettext("missing "
887 "'=' for -O option\n"));
894 * Mountpoints are checked and then added later.
895 * Uniquely among properties, they can be specified
896 * more than once, to avoid conflict with -m.
898 if (0 == strcmp(optarg,
899 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
900 mountpoint = propval;
901 } else if (add_prop_list(optarg, propval, &fsprops,
908 * Sanity check temporary pool name.
910 if (strchr(optarg, '/') != NULL) {
911 (void) fprintf(stderr, gettext("cannot create "
912 "'%s': invalid character '/' in temporary "
914 (void) fprintf(stderr, gettext("use 'zfs "
915 "create' to create a dataset\n"));
919 if (add_prop_list(zpool_prop_to_name(
920 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
922 if (add_prop_list_default(zpool_prop_to_name(
923 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
928 (void) fprintf(stderr, gettext("missing argument for "
929 "'%c' option\n"), optopt);
932 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
941 /* get pool name and check number of arguments */
943 (void) fprintf(stderr, gettext("missing pool name argument\n"));
947 (void) fprintf(stderr, gettext("missing vdev specification\n"));
954 * As a special case, check for use of '/' in the name, and direct the
955 * user to use 'zfs create' instead.
957 if (strchr(poolname, '/') != NULL) {
958 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
959 "character '/' in pool name\n"), poolname);
960 (void) fprintf(stderr, gettext("use 'zfs create' to "
961 "create a dataset\n"));
965 /* pass off to get_vdev_spec for bulk processing */
966 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
971 /* make_root_vdev() allows 0 toplevel children if there are spares */
972 if (!zfs_allocatable_devs(nvroot)) {
973 (void) fprintf(stderr, gettext("invalid vdev "
974 "specification: at least one toplevel vdev must be "
979 if (altroot != NULL && altroot[0] != '/') {
980 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
981 "must be an absolute path\n"), altroot);
986 * Check the validity of the mountpoint and direct the user to use the
987 * '-m' mountpoint option if it looks like its in use.
988 * Ignore the checks if the '-f' option is given.
990 if (!force && (mountpoint == NULL ||
991 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
992 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
993 char buf[MAXPATHLEN];
996 if (mountpoint && mountpoint[0] != '/') {
997 (void) fprintf(stderr, gettext("invalid mountpoint "
998 "'%s': must be an absolute path, 'legacy', or "
999 "'none'\n"), mountpoint);
1003 if (mountpoint == NULL) {
1004 if (altroot != NULL)
1005 (void) snprintf(buf, sizeof (buf), "%s/%s",
1008 (void) snprintf(buf, sizeof (buf), "/%s",
1011 if (altroot != NULL)
1012 (void) snprintf(buf, sizeof (buf), "%s%s",
1013 altroot, mountpoint);
1015 (void) snprintf(buf, sizeof (buf), "%s",
1019 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1020 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1021 "%s\n"), buf, strerror(errno));
1022 (void) fprintf(stderr, gettext("use '-m' "
1023 "option to provide a different default\n"));
1028 while (count < 3 && readdir(dirp) != NULL)
1030 (void) closedir(dirp);
1033 (void) fprintf(stderr, gettext("mountpoint "
1034 "'%s' exists and is not empty\n"), buf);
1035 (void) fprintf(stderr, gettext("use '-m' "
1036 "option to provide a "
1037 "different default\n"));
1044 * Now that the mountpoint's validity has been checked, ensure that
1045 * the property is set appropriately prior to creating the pool.
1047 if (mountpoint != NULL) {
1048 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1049 mountpoint, &fsprops, B_FALSE);
1057 * For a dry run invocation, print out a basic message and run
1058 * through all the vdevs in the list and print out in an
1059 * appropriate hierarchy.
1061 (void) printf(gettext("would create '%s' with the "
1062 "following layout:\n\n"), poolname);
1064 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1065 if (num_logs(nvroot) > 0)
1066 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1071 * Hand off to libzfs.
1073 if (enable_all_pool_feat) {
1075 for (i = 0; i < SPA_FEATURES; i++) {
1076 char propname[MAXPATHLEN];
1077 zfeature_info_t *feat = &spa_feature_table[i];
1079 (void) snprintf(propname, sizeof (propname),
1080 "feature@%s", feat->fi_uname);
1083 * Skip feature if user specified it manually
1084 * on the command line.
1086 if (nvlist_exists(props, propname))
1089 ret = add_prop_list(propname,
1090 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1097 if (zpool_create(g_zfs, poolname,
1098 nvroot, props, fsprops) == 0) {
1099 zfs_handle_t *pool = zfs_open(g_zfs,
1100 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1102 if (zfs_mount(pool, NULL, 0) == 0)
1103 ret = zfs_shareall(pool);
1106 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1107 (void) fprintf(stderr, gettext("pool name may have "
1113 nvlist_free(nvroot);
1114 nvlist_free(fsprops);
1118 nvlist_free(fsprops);
1125 * zpool destroy <pool>
1127 * -f Forcefully unmount any datasets
1129 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1132 zpool_do_destroy(int argc, char **argv)
1134 boolean_t force = B_FALSE;
1137 zpool_handle_t *zhp;
1141 while ((c = getopt(argc, argv, "f")) != -1) {
1147 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1156 /* check arguments */
1158 (void) fprintf(stderr, gettext("missing pool argument\n"));
1162 (void) fprintf(stderr, gettext("too many arguments\n"));
1168 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1170 * As a special case, check for use of '/' in the name, and
1171 * direct the user to use 'zfs destroy' instead.
1173 if (strchr(pool, '/') != NULL)
1174 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1175 "destroy a dataset\n"));
1179 if (zpool_disable_datasets(zhp, force) != 0) {
1180 (void) fprintf(stderr, gettext("could not destroy '%s': "
1181 "could not unmount datasets\n"), zpool_get_name(zhp));
1185 /* The history must be logged as part of the export */
1186 log_history = B_FALSE;
1188 ret = (zpool_destroy(zhp, history_str) != 0);
1196 * zpool export [-f] <pool> ...
1198 * -f Forcefully unmount datasets
1200 * Export the given pools. By default, the command will attempt to cleanly
1201 * unmount any active datasets within the pool. If the '-f' flag is specified,
1202 * then the datasets will be forcefully unmounted.
1205 zpool_do_export(int argc, char **argv)
1207 boolean_t force = B_FALSE;
1208 boolean_t hardforce = B_FALSE;
1210 zpool_handle_t *zhp;
1215 while ((c = getopt(argc, argv, "fF")) != -1) {
1224 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1233 /* check arguments */
1235 (void) fprintf(stderr, gettext("missing pool argument\n"));
1240 for (i = 0; i < argc; i++) {
1241 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1246 if (zpool_disable_datasets(zhp, force) != 0) {
1252 /* The history must be logged as part of the export */
1253 log_history = B_FALSE;
1256 if (zpool_export_force(zhp, history_str) != 0)
1258 } else if (zpool_export(zhp, force, history_str) != 0) {
1269 * Given a vdev configuration, determine the maximum width needed for the device
1273 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1275 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1280 if (strlen(name) + depth > max)
1281 max = strlen(name) + depth;
1285 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1286 &child, &children) == 0) {
1287 for (c = 0; c < children; c++)
1288 if ((ret = max_width(zhp, child[c], depth + 2,
1293 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1294 &child, &children) == 0) {
1295 for (c = 0; c < children; c++)
1296 if ((ret = max_width(zhp, child[c], depth + 2,
1301 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1302 &child, &children) == 0) {
1303 for (c = 0; c < children; c++)
1304 if ((ret = max_width(zhp, child[c], depth + 2,
1313 typedef struct spare_cbdata {
1315 zpool_handle_t *cb_zhp;
1319 find_vdev(nvlist_t *nv, uint64_t search)
1325 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1329 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1330 &child, &children) == 0) {
1331 for (c = 0; c < children; c++)
1332 if (find_vdev(child[c], search))
1340 find_spare(zpool_handle_t *zhp, void *data)
1342 spare_cbdata_t *cbp = data;
1343 nvlist_t *config, *nvroot;
1345 config = zpool_get_config(zhp, NULL);
1346 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1349 if (find_vdev(nvroot, cbp->cb_guid)) {
1359 * Print out configuration state as requested by status_callback.
1362 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1363 int namewidth, int depth, boolean_t isspare)
1366 uint_t c, vsc, children;
1367 pool_scan_stat_t *ps = NULL;
1369 char rbuf[6], wbuf[6], cbuf[6];
1371 uint64_t notpresent;
1376 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1377 &child, &children) != 0)
1380 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1381 (uint64_t **)&vs, &vsc) == 0);
1383 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1386 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1389 if (vs->vs_aux == VDEV_AUX_SPARED)
1391 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1395 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1399 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1400 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1401 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1402 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1405 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1406 ¬present) == 0 ||
1407 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1409 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1410 (void) printf(" was %s", path);
1411 } else if (vs->vs_aux != 0) {
1414 switch (vs->vs_aux) {
1415 case VDEV_AUX_OPEN_FAILED:
1416 (void) printf(gettext("cannot open"));
1419 case VDEV_AUX_BAD_GUID_SUM:
1420 (void) printf(gettext("missing device"));
1423 case VDEV_AUX_NO_REPLICAS:
1424 (void) printf(gettext("insufficient replicas"));
1427 case VDEV_AUX_VERSION_NEWER:
1428 (void) printf(gettext("newer version"));
1431 case VDEV_AUX_UNSUP_FEAT:
1432 (void) printf(gettext("unsupported feature(s)"));
1435 case VDEV_AUX_ASHIFT_TOO_BIG:
1436 (void) printf(gettext("unsupported minimum blocksize"));
1439 case VDEV_AUX_SPARED:
1440 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1442 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1443 if (strcmp(zpool_get_name(cb.cb_zhp),
1444 zpool_get_name(zhp)) == 0)
1445 (void) printf(gettext("currently in "
1448 (void) printf(gettext("in use by "
1450 zpool_get_name(cb.cb_zhp));
1451 zpool_close(cb.cb_zhp);
1453 (void) printf(gettext("currently in use"));
1457 case VDEV_AUX_ERR_EXCEEDED:
1458 (void) printf(gettext("too many errors"));
1461 case VDEV_AUX_IO_FAILURE:
1462 (void) printf(gettext("experienced I/O failures"));
1465 case VDEV_AUX_BAD_LOG:
1466 (void) printf(gettext("bad intent log"));
1469 case VDEV_AUX_EXTERNAL:
1470 (void) printf(gettext("external device fault"));
1473 case VDEV_AUX_SPLIT_POOL:
1474 (void) printf(gettext("split into new pool"));
1478 (void) printf(gettext("corrupted data"));
1481 } else if (children == 0 && !isspare &&
1482 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1483 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1485 gettext(" block size: %dB configured, %dB native"),
1486 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1489 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1490 (uint64_t **)&ps, &c);
1492 if (ps && ps->pss_state == DSS_SCANNING &&
1493 vs->vs_scan_processed != 0 && children == 0) {
1494 (void) printf(gettext(" (%s)"),
1495 (ps->pss_func == POOL_SCAN_RESILVER) ?
1496 "resilvering" : "repairing");
1499 (void) printf("\n");
1501 for (c = 0; c < children; c++) {
1502 uint64_t islog = B_FALSE, ishole = B_FALSE;
1504 /* Don't print logs or holes here */
1505 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1507 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1509 if (islog || ishole)
1511 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1512 print_status_config(zhp, vname, child[c],
1513 namewidth, depth + 2, isspare);
1520 * Print the configuration of an exported pool. Iterate over all vdevs in the
1521 * pool, printing out the name and status for each one.
1524 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1531 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1532 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1533 strcmp(type, VDEV_TYPE_HOLE) == 0)
1536 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1537 (uint64_t **)&vs, &c) == 0);
1539 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1540 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1542 if (vs->vs_aux != 0) {
1545 switch (vs->vs_aux) {
1546 case VDEV_AUX_OPEN_FAILED:
1547 (void) printf(gettext("cannot open"));
1550 case VDEV_AUX_BAD_GUID_SUM:
1551 (void) printf(gettext("missing device"));
1554 case VDEV_AUX_NO_REPLICAS:
1555 (void) printf(gettext("insufficient replicas"));
1558 case VDEV_AUX_VERSION_NEWER:
1559 (void) printf(gettext("newer version"));
1562 case VDEV_AUX_UNSUP_FEAT:
1563 (void) printf(gettext("unsupported feature(s)"));
1566 case VDEV_AUX_ERR_EXCEEDED:
1567 (void) printf(gettext("too many errors"));
1571 (void) printf(gettext("corrupted data"));
1575 (void) printf("\n");
1577 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1578 &child, &children) != 0)
1581 for (c = 0; c < children; c++) {
1582 uint64_t is_log = B_FALSE;
1584 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1589 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1590 print_import_config(vname, child[c], namewidth, depth + 2);
1594 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1595 &child, &children) == 0) {
1596 (void) printf(gettext("\tcache\n"));
1597 for (c = 0; c < children; c++) {
1598 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1599 (void) printf("\t %s\n", vname);
1604 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1605 &child, &children) == 0) {
1606 (void) printf(gettext("\tspares\n"));
1607 for (c = 0; c < children; c++) {
1608 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1609 (void) printf("\t %s\n", vname);
1617 * Logs are recorded as top level vdevs in the main pool child array
1618 * but with "is_log" set to 1. We use either print_status_config() or
1619 * print_import_config() to print the top level logs then any log
1620 * children (eg mirrored slogs) are printed recursively - which
1621 * works because only the top level vdev is marked "is_log"
1624 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1629 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1633 (void) printf(gettext("\tlogs\n"));
1635 for (c = 0; c < children; c++) {
1636 uint64_t is_log = B_FALSE;
1639 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1643 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1645 print_status_config(zhp, name, child[c], namewidth,
1648 print_import_config(name, child[c], namewidth, 2);
1654 * Display the status for the given pool.
1657 show_import(nvlist_t *config)
1659 uint64_t pool_state;
1671 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1673 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1675 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1677 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1680 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1681 (uint64_t **)&vs, &vsc) == 0);
1682 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1684 reason = zpool_import_status(config, &msgid);
1686 (void) printf(gettext(" pool: %s\n"), name);
1687 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1688 (void) printf(gettext(" state: %s"), health);
1689 if (pool_state == POOL_STATE_DESTROYED)
1690 (void) printf(gettext(" (DESTROYED)"));
1691 (void) printf("\n");
1694 case ZPOOL_STATUS_MISSING_DEV_R:
1695 case ZPOOL_STATUS_MISSING_DEV_NR:
1696 case ZPOOL_STATUS_BAD_GUID_SUM:
1697 (void) printf(gettext(" status: One or more devices are "
1698 "missing from the system.\n"));
1701 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1702 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1703 (void) printf(gettext(" status: One or more devices contains "
1704 "corrupted data.\n"));
1707 case ZPOOL_STATUS_CORRUPT_DATA:
1709 gettext(" status: The pool data is corrupted.\n"));
1712 case ZPOOL_STATUS_OFFLINE_DEV:
1713 (void) printf(gettext(" status: One or more devices "
1714 "are offlined.\n"));
1717 case ZPOOL_STATUS_CORRUPT_POOL:
1718 (void) printf(gettext(" status: The pool metadata is "
1722 case ZPOOL_STATUS_VERSION_OLDER:
1723 (void) printf(gettext(" status: The pool is formatted using a "
1724 "legacy on-disk version.\n"));
1727 case ZPOOL_STATUS_VERSION_NEWER:
1728 (void) printf(gettext(" status: The pool is formatted using an "
1729 "incompatible version.\n"));
1732 case ZPOOL_STATUS_FEAT_DISABLED:
1733 (void) printf(gettext(" status: Some supported features are "
1734 "not enabled on the pool.\n"));
1737 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1738 (void) printf(gettext("status: The pool uses the following "
1739 "feature(s) not supported on this sytem:\n"));
1740 zpool_print_unsup_feat(config);
1743 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1744 (void) printf(gettext("status: The pool can only be accessed "
1745 "in read-only mode on this system. It\n\tcannot be "
1746 "accessed in read-write mode because it uses the "
1747 "following\n\tfeature(s) not supported on this system:\n"));
1748 zpool_print_unsup_feat(config);
1751 case ZPOOL_STATUS_HOSTID_MISMATCH:
1752 (void) printf(gettext(" status: The pool was last accessed by "
1753 "another system.\n"));
1756 case ZPOOL_STATUS_FAULTED_DEV_R:
1757 case ZPOOL_STATUS_FAULTED_DEV_NR:
1758 (void) printf(gettext(" status: One or more devices are "
1762 case ZPOOL_STATUS_BAD_LOG:
1763 (void) printf(gettext(" status: An intent log record cannot be "
1767 case ZPOOL_STATUS_RESILVERING:
1768 (void) printf(gettext(" status: One or more devices were being "
1772 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
1773 (void) printf(gettext("status: One or more devices were "
1774 "configured to use a non-native block size.\n"
1775 "\tExpect reduced performance.\n"));
1780 * No other status can be seen when importing pools.
1782 assert(reason == ZPOOL_STATUS_OK);
1786 * Print out an action according to the overall state of the pool.
1788 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1789 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1790 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1791 (void) printf(gettext(" action: The pool can be "
1792 "imported using its name or numeric identifier, "
1793 "though\n\tsome features will not be available "
1794 "without an explicit 'zpool upgrade'.\n"));
1795 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1796 (void) printf(gettext(" action: The pool can be "
1797 "imported using its name or numeric "
1798 "identifier and\n\tthe '-f' flag.\n"));
1800 (void) printf(gettext(" action: The pool can be "
1801 "imported using its name or numeric "
1804 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1805 (void) printf(gettext(" action: The pool can be imported "
1806 "despite missing or damaged devices. The\n\tfault "
1807 "tolerance of the pool may be compromised if imported.\n"));
1810 case ZPOOL_STATUS_VERSION_NEWER:
1811 (void) printf(gettext(" action: The pool cannot be "
1812 "imported. Access the pool on a system running "
1813 "newer\n\tsoftware, or recreate the pool from "
1816 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1817 (void) printf(gettext("action: The pool cannot be "
1818 "imported. Access the pool on a system that "
1819 "supports\n\tthe required feature(s), or recreate "
1820 "the pool from backup.\n"));
1822 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1823 (void) printf(gettext("action: The pool cannot be "
1824 "imported in read-write mode. Import the pool "
1826 "\t\"-o readonly=on\", access the pool on a system "
1827 "that supports the\n\trequired feature(s), or "
1828 "recreate the pool from backup.\n"));
1830 case ZPOOL_STATUS_MISSING_DEV_R:
1831 case ZPOOL_STATUS_MISSING_DEV_NR:
1832 case ZPOOL_STATUS_BAD_GUID_SUM:
1833 (void) printf(gettext(" action: The pool cannot be "
1834 "imported. Attach the missing\n\tdevices and try "
1838 (void) printf(gettext(" action: The pool cannot be "
1839 "imported due to damaged devices or data.\n"));
1843 /* Print the comment attached to the pool. */
1844 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1845 (void) printf(gettext("comment: %s\n"), comment);
1848 * If the state is "closed" or "can't open", and the aux state
1849 * is "corrupt data":
1851 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1852 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1853 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1854 if (pool_state == POOL_STATE_DESTROYED)
1855 (void) printf(gettext("\tThe pool was destroyed, "
1856 "but can be imported using the '-Df' flags.\n"));
1857 else if (pool_state != POOL_STATE_EXPORTED)
1858 (void) printf(gettext("\tThe pool may be active on "
1859 "another system, but can be imported using\n\t"
1860 "the '-f' flag.\n"));
1864 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1867 (void) printf(gettext(" config:\n\n"));
1869 namewidth = max_width(NULL, nvroot, 0, 0);
1873 print_import_config(name, nvroot, namewidth, 0);
1874 if (num_logs(nvroot) > 0)
1875 print_logs(NULL, nvroot, namewidth, B_FALSE);
1877 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1878 (void) printf(gettext("\n\tAdditional devices are known to "
1879 "be part of this pool, though their\n\texact "
1880 "configuration cannot be determined.\n"));
1885 * Perform the import for the given configuration. This passes the heavy
1886 * lifting off to zpool_import_props(), and then mounts the datasets contained
1890 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1891 nvlist_t *props, int flags)
1893 zpool_handle_t *zhp;
1898 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1901 verify(nvlist_lookup_uint64(config,
1902 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1903 verify(nvlist_lookup_uint64(config,
1904 ZPOOL_CONFIG_VERSION, &version) == 0);
1905 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1906 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1907 "is formatted using an unsupported ZFS version\n"), name);
1909 } else if (state != POOL_STATE_EXPORTED &&
1910 !(flags & ZFS_IMPORT_ANY_HOST)) {
1913 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1915 if ((unsigned long)hostid != gethostid()) {
1920 verify(nvlist_lookup_string(config,
1921 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1922 verify(nvlist_lookup_uint64(config,
1923 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1925 (void) fprintf(stderr, gettext("cannot import "
1926 "'%s': pool may be in use from other "
1927 "system, it was last accessed by %s "
1928 "(hostid: 0x%lx) on %s"), name, hostname,
1929 (unsigned long)hostid,
1930 asctime(localtime(&t)));
1931 (void) fprintf(stderr, gettext("use '-f' to "
1932 "import anyway\n"));
1936 (void) fprintf(stderr, gettext("cannot import '%s': "
1937 "pool may be in use from other system\n"), name);
1938 (void) fprintf(stderr, gettext("use '-f' to import "
1944 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1947 if (newname != NULL)
1948 name = (char *)newname;
1950 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1953 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1954 !(flags & ZFS_IMPORT_ONLY) &&
1955 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1965 * zpool import [-d dir] [-D]
1966 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1967 * [-d dir | -c cachefile] [-f] -a
1968 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1969 * [-d dir | -c cachefile] [-f] [-n] [-F] [-t]
1970 * <pool | id> [newpool]
1972 * -c Read pool information from a cachefile instead of searching
1975 * -d Scan in a specific directory, other than /dev/dsk. More than
1976 * one directory can be specified using multiple '-d' options.
1978 * -D Scan for previously destroyed pools or import all or only
1979 * specified destroyed pools.
1981 * -R Temporarily import the pool, with all mountpoints relative to
1982 * the given root. The pool will remain exported when the machine
1985 * -V Import even in the presence of faulted vdevs. This is an
1986 * intentionally undocumented option for testing purposes, and
1987 * treats the pool configuration as complete, leaving any bad
1988 * vdevs in the FAULTED state. In other words, it does verbatim
1991 * -f Force import, even if it appears that the pool is active.
1993 * -F Attempt rewind if necessary.
1995 * -n See if rewind would work, but don't actually rewind.
1997 * -N Import the pool but don't mount datasets.
1999 * -t Use newpool as a temporary pool name instead of renaming
2002 * -T Specify a starting txg to use for import. This option is
2003 * intentionally undocumented option for testing purposes.
2005 * -a Import all pools found.
2007 * -o Set property=value and/or temporary mount options (without '=').
2009 * The import command scans for pools to import, and import pools based on pool
2010 * name and GUID. The pool can also be renamed as part of the import process.
2013 zpool_do_import(int argc, char **argv)
2015 char **searchdirs = NULL;
2019 nvlist_t *pools = NULL;
2020 boolean_t do_all = B_FALSE;
2021 boolean_t do_destroyed = B_FALSE;
2022 char *mntopts = NULL;
2025 uint64_t searchguid = 0;
2026 char *searchname = NULL;
2028 nvlist_t *found_config;
2029 nvlist_t *policy = NULL;
2030 nvlist_t *props = NULL;
2032 int flags = ZFS_IMPORT_NORMAL;
2033 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2034 boolean_t dryrun = B_FALSE;
2035 boolean_t do_rewind = B_FALSE;
2036 boolean_t xtreme_rewind = B_FALSE;
2037 uint64_t pool_state, txg = -1ULL;
2038 char *cachefile = NULL;
2039 importargs_t idata = { 0 };
2043 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2052 if (searchdirs == NULL) {
2053 searchdirs = safe_malloc(sizeof (char *));
2055 char **tmp = safe_malloc((nsearch + 1) *
2057 bcopy(searchdirs, tmp, nsearch *
2062 searchdirs[nsearch++] = optarg;
2065 do_destroyed = B_TRUE;
2068 flags |= ZFS_IMPORT_ANY_HOST;
2074 flags |= ZFS_IMPORT_MISSING_LOG;
2080 flags |= ZFS_IMPORT_ONLY;
2083 if ((propval = strchr(optarg, '=')) != NULL) {
2086 if (add_prop_list(optarg, propval,
2094 if (add_prop_list(zpool_prop_to_name(
2095 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2097 if (add_prop_list_default(zpool_prop_to_name(
2098 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2102 flags |= ZFS_IMPORT_TEMP_NAME;
2103 if (add_prop_list_default(zpool_prop_to_name(
2104 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2109 txg = strtoull(optarg, &endptr, 0);
2110 if (errno != 0 || *endptr != '\0') {
2111 (void) fprintf(stderr,
2112 gettext("invalid txg value\n"));
2115 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2118 flags |= ZFS_IMPORT_VERBATIM;
2121 xtreme_rewind = B_TRUE;
2124 (void) fprintf(stderr, gettext("missing argument for "
2125 "'%c' option\n"), optopt);
2129 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2138 if (cachefile && nsearch != 0) {
2139 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2143 if ((dryrun || xtreme_rewind) && !do_rewind) {
2144 (void) fprintf(stderr,
2145 gettext("-n or -X only meaningful with -F\n"));
2149 rewind_policy = ZPOOL_TRY_REWIND;
2151 rewind_policy = ZPOOL_DO_REWIND;
2153 rewind_policy |= ZPOOL_EXTREME_REWIND;
2155 /* In the future, we can capture further policy and include it here */
2156 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2157 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2158 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2161 if (searchdirs == NULL) {
2162 searchdirs = safe_malloc(sizeof (char *));
2163 searchdirs[0] = "/dev";
2167 /* check argument count */
2170 (void) fprintf(stderr, gettext("too many arguments\n"));
2175 (void) fprintf(stderr, gettext("too many arguments\n"));
2180 * Check for the SYS_CONFIG privilege. We do this explicitly
2181 * here because otherwise any attempt to discover pools will
2184 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2185 (void) fprintf(stderr, gettext("cannot "
2186 "discover pools: permission denied\n"));
2188 nvlist_free(policy);
2194 * Depending on the arguments given, we do one of the following:
2196 * <none> Iterate through all pools and display information about
2199 * -a Iterate through all pools and try to import each one.
2201 * <id> Find the pool that corresponds to the given GUID/pool
2202 * name and import that one.
2204 * -D Above options applies only to destroyed pools.
2210 searchguid = strtoull(argv[0], &endptr, 10);
2211 if (errno != 0 || *endptr != '\0') {
2212 searchname = argv[0];
2215 found_config = NULL;
2218 * User specified a name or guid. Ensure it's unique.
2220 idata.unique = B_TRUE;
2224 idata.path = searchdirs;
2225 idata.paths = nsearch;
2226 idata.poolname = searchname;
2227 idata.guid = searchguid;
2228 idata.cachefile = cachefile;
2230 pools = zpool_search_import(g_zfs, &idata);
2232 if (pools != NULL && idata.exists &&
2233 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2234 (void) fprintf(stderr, gettext("cannot import '%s': "
2235 "a pool with that name already exists\n"),
2237 (void) fprintf(stderr, gettext("use the form 'zpool import "
2238 "[-t] <pool | id> <newpool>' to give it a new temporary "
2239 "or permanent name\n"));
2241 } else if (pools == NULL && idata.exists) {
2242 (void) fprintf(stderr, gettext("cannot import '%s': "
2243 "a pool with that name is already created/imported,\n"),
2245 (void) fprintf(stderr, gettext("and no additional pools "
2246 "with that name were found\n"));
2248 } else if (pools == NULL) {
2250 (void) fprintf(stderr, gettext("cannot import '%s': "
2251 "no such pool available\n"), argv[0]);
2258 nvlist_free(policy);
2263 * At this point we have a list of import candidate configs. Even if
2264 * we were searching by pool name or guid, we still need to
2265 * post-process the list to deal with pool state and possible
2271 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2273 verify(nvpair_value_nvlist(elem, &config) == 0);
2275 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2277 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2279 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2282 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2289 (void) printf("\n");
2292 err |= do_import(config, NULL, mntopts,
2295 show_import(config);
2297 } else if (searchname != NULL) {
2301 * We are searching for a pool based on name.
2303 verify(nvlist_lookup_string(config,
2304 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2306 if (strcmp(name, searchname) == 0) {
2307 if (found_config != NULL) {
2308 (void) fprintf(stderr, gettext(
2309 "cannot import '%s': more than "
2310 "one matching pool\n"), searchname);
2311 (void) fprintf(stderr, gettext(
2312 "import by numeric ID instead\n"));
2315 found_config = config;
2321 * Search for a pool by guid.
2323 verify(nvlist_lookup_uint64(config,
2324 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2326 if (guid == searchguid)
2327 found_config = config;
2332 * If we were searching for a specific pool, verify that we found a
2333 * pool, and then do the import.
2335 if (argc != 0 && err == 0) {
2336 if (found_config == NULL) {
2337 (void) fprintf(stderr, gettext("cannot import '%s': "
2338 "no such pool available\n"), argv[0]);
2341 err |= do_import(found_config, argc == 1 ? NULL :
2342 argv[1], mntopts, props, flags);
2347 * If we were just looking for pools, report an error if none were
2350 if (argc == 0 && first)
2351 (void) fprintf(stderr,
2352 gettext("no pools available to import\n"));
2357 nvlist_free(policy);
2360 return (err ? 1 : 0);
2363 typedef struct iostat_cbdata {
2364 boolean_t cb_verbose;
2367 zpool_list_t *cb_list;
2371 print_iostat_separator(iostat_cbdata_t *cb)
2375 for (i = 0; i < cb->cb_namewidth; i++)
2377 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2381 print_iostat_header(iostat_cbdata_t *cb)
2383 (void) printf("%*s capacity operations bandwidth\n",
2384 cb->cb_namewidth, "");
2385 (void) printf("%-*s alloc free read write read write\n",
2386 cb->cb_namewidth, "pool");
2387 print_iostat_separator(cb);
2391 * Display a single statistic.
2394 print_one_stat(uint64_t value)
2398 zfs_nicenum(value, buf, sizeof (buf));
2399 (void) printf(" %5s", buf);
2403 * Print out all the statistics for the given vdev. This can either be the
2404 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2405 * is a verbose output, and we don't want to display the toplevel pool stats.
2408 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2409 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2411 nvlist_t **oldchild, **newchild;
2413 vdev_stat_t *oldvs, *newvs;
2414 vdev_stat_t zerovs = { 0 };
2419 if (oldnv != NULL) {
2420 verify(nvlist_lookup_uint64_array(oldnv,
2421 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2426 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2427 (uint64_t **)&newvs, &c) == 0);
2429 if (strlen(name) + depth > cb->cb_namewidth)
2430 (void) printf("%*s%s", depth, "", name);
2432 (void) printf("%*s%s%*s", depth, "", name,
2433 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2435 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2440 scale = (double)NANOSEC / tdelta;
2442 /* only toplevel vdevs have capacity stats */
2443 if (newvs->vs_space == 0) {
2444 (void) printf(" - -");
2446 print_one_stat(newvs->vs_alloc);
2447 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2450 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2451 oldvs->vs_ops[ZIO_TYPE_READ])));
2453 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2454 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2456 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2457 oldvs->vs_bytes[ZIO_TYPE_READ])));
2459 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2460 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2462 (void) printf("\n");
2464 if (!cb->cb_verbose)
2467 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2468 &newchild, &children) != 0)
2471 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2472 &oldchild, &c) != 0)
2475 for (c = 0; c < children; c++) {
2476 uint64_t ishole = B_FALSE, islog = B_FALSE;
2478 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2481 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2484 if (ishole || islog)
2487 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2488 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2489 newchild[c], cb, depth + 2);
2494 * Log device section
2497 if (num_logs(newnv) > 0) {
2498 (void) printf("%-*s - - - - - "
2499 "-\n", cb->cb_namewidth, "logs");
2501 for (c = 0; c < children; c++) {
2502 uint64_t islog = B_FALSE;
2503 (void) nvlist_lookup_uint64(newchild[c],
2504 ZPOOL_CONFIG_IS_LOG, &islog);
2507 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2509 print_vdev_stats(zhp, vname, oldnv ?
2510 oldchild[c] : NULL, newchild[c],
2519 * Include level 2 ARC devices in iostat output
2521 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2522 &newchild, &children) != 0)
2525 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2526 &oldchild, &c) != 0)
2530 (void) printf("%-*s - - - - - "
2531 "-\n", cb->cb_namewidth, "cache");
2532 for (c = 0; c < children; c++) {
2533 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2535 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2536 newchild[c], cb, depth + 2);
2543 refresh_iostat(zpool_handle_t *zhp, void *data)
2545 iostat_cbdata_t *cb = data;
2549 * If the pool has disappeared, remove it from the list and continue.
2551 if (zpool_refresh_stats(zhp, &missing) != 0)
2555 pool_list_remove(cb->cb_list, zhp);
2561 * Callback to print out the iostats for the given pool.
2564 print_iostat(zpool_handle_t *zhp, void *data)
2566 iostat_cbdata_t *cb = data;
2567 nvlist_t *oldconfig, *newconfig;
2568 nvlist_t *oldnvroot, *newnvroot;
2570 newconfig = zpool_get_config(zhp, &oldconfig);
2572 if (cb->cb_iteration == 1)
2575 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2578 if (oldconfig == NULL)
2581 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2585 * Print out the statistics for the pool.
2587 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2590 print_iostat_separator(cb);
2596 get_namewidth(zpool_handle_t *zhp, void *data)
2598 iostat_cbdata_t *cb = data;
2599 nvlist_t *config, *nvroot;
2601 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2602 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2604 if (!cb->cb_verbose)
2605 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2607 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2612 * The width must fall into the range [10,38]. The upper limit is the
2613 * maximum we can have and still fit in 80 columns.
2615 if (cb->cb_namewidth < 10)
2616 cb->cb_namewidth = 10;
2617 if (cb->cb_namewidth > 38)
2618 cb->cb_namewidth = 38;
2624 * Parse the input string, get the 'interval' and 'count' value if there is one.
2627 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2630 unsigned long interval = 0, count = 0;
2631 int argc = *argcp, errno;
2634 * Determine if the last argument is an integer or a pool name
2636 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2640 interval = strtoul(argv[argc - 1], &end, 10);
2642 if (*end == '\0' && errno == 0) {
2643 if (interval == 0) {
2644 (void) fprintf(stderr, gettext("interval "
2645 "cannot be zero\n"));
2649 * Ignore the last parameter
2654 * If this is not a valid number, just plow on. The
2655 * user will get a more informative error message later
2663 * If the last argument is also an integer, then we have both a count
2666 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2671 interval = strtoul(argv[argc - 1], &end, 10);
2673 if (*end == '\0' && errno == 0) {
2674 if (interval == 0) {
2675 (void) fprintf(stderr, gettext("interval "
2676 "cannot be zero\n"));
2681 * Ignore the last parameter
2695 get_timestamp_arg(char c)
2698 timestamp_fmt = UDATE;
2700 timestamp_fmt = DDATE;
2706 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2708 * -v Display statistics for individual vdevs
2709 * -T Display a timestamp in date(1) or Unix format
2711 * This command can be tricky because we want to be able to deal with pool
2712 * creation/destruction as well as vdev configuration changes. The bulk of this
2713 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2714 * on pool_list_update() to detect the addition of new pools. Configuration
2715 * changes are all handled within libzfs.
2718 zpool_do_iostat(int argc, char **argv)
2723 unsigned long interval = 0, count = 0;
2725 boolean_t verbose = B_FALSE;
2729 while ((c = getopt(argc, argv, "T:v")) != -1) {
2732 get_timestamp_arg(*optarg);
2738 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2747 get_interval_count(&argc, argv, &interval, &count);
2750 * Construct the list of all interesting pools.
2753 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2756 if (pool_list_count(list) == 0 && argc != 0) {
2757 pool_list_free(list);
2761 if (pool_list_count(list) == 0 && interval == 0) {
2762 pool_list_free(list);
2763 (void) fprintf(stderr, gettext("no pools available\n"));
2768 * Enter the main iostat loop.
2771 cb.cb_verbose = verbose;
2772 cb.cb_iteration = 0;
2773 cb.cb_namewidth = 0;
2776 pool_list_update(list);
2778 if ((npools = pool_list_count(list)) == 0)
2782 * Refresh all statistics. This is done as an explicit step
2783 * before calculating the maximum name width, so that any
2784 * configuration changes are properly accounted for.
2786 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2789 * Iterate over all pools to determine the maximum width
2790 * for the pool / device name column across all pools.
2792 cb.cb_namewidth = 0;
2793 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2795 if (timestamp_fmt != NODATE)
2796 print_timestamp(timestamp_fmt);
2799 * If it's the first time, or verbose mode, print the header.
2801 if (++cb.cb_iteration == 1 || verbose)
2802 print_iostat_header(&cb);
2804 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2807 * If there's more than one pool, and we're not in verbose mode
2808 * (which prints a separator for us), then print a separator.
2810 if (npools > 1 && !verbose)
2811 print_iostat_separator(&cb);
2814 (void) printf("\n");
2817 * Flush the output so that redirection to a file isn't buffered
2820 (void) fflush(stdout);
2825 if (count != 0 && --count == 0)
2828 (void) sleep(interval);
2831 pool_list_free(list);
2836 typedef struct list_cbdata {
2837 boolean_t cb_verbose;
2839 boolean_t cb_scripted;
2840 zprop_list_t *cb_proplist;
2841 boolean_t cb_literal;
2845 * Given a list of columns to display, output appropriate headers for each one.
2848 print_header(list_cbdata_t *cb)
2850 zprop_list_t *pl = cb->cb_proplist;
2851 char headerbuf[ZPOOL_MAXPROPLEN];
2853 boolean_t first = B_TRUE;
2854 boolean_t right_justify;
2857 for (; pl != NULL; pl = pl->pl_next) {
2858 width = pl->pl_width;
2859 if (first && cb->cb_verbose) {
2861 * Reset the width to accommodate the verbose listing
2864 width = cb->cb_namewidth;
2872 right_justify = B_FALSE;
2873 if (pl->pl_prop != ZPROP_INVAL) {
2874 header = zpool_prop_column_name(pl->pl_prop);
2875 right_justify = zpool_prop_align_right(pl->pl_prop);
2879 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2880 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2881 headerbuf[i] = '\0';
2885 if (pl->pl_next == NULL && !right_justify)
2886 (void) printf("%s", header);
2887 else if (right_justify)
2888 (void) printf("%*s", width, header);
2890 (void) printf("%-*s", width, header);
2894 (void) printf("\n");
2898 * Given a pool and a list of properties, print out all the properties according
2899 * to the described layout.
2902 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2904 zprop_list_t *pl = cb->cb_proplist;
2905 boolean_t first = B_TRUE;
2906 char property[ZPOOL_MAXPROPLEN];
2908 boolean_t right_justify;
2911 for (; pl != NULL; pl = pl->pl_next) {
2913 width = pl->pl_width;
2914 if (first && cb->cb_verbose) {
2916 * Reset the width to accommodate the verbose listing
2919 width = cb->cb_namewidth;
2923 if (cb->cb_scripted)
2924 (void) printf("\t");
2931 right_justify = B_FALSE;
2932 if (pl->pl_prop != ZPROP_INVAL) {
2933 if (zpool_get_prop(zhp, pl->pl_prop, property,
2934 sizeof (property), NULL, cb->cb_literal) != 0)
2939 right_justify = zpool_prop_align_right(pl->pl_prop);
2940 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2941 zpool_prop_unsupported(pl->pl_user_prop)) &&
2942 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2943 sizeof (property)) == 0) {
2951 * If this is being called in scripted mode, or if this is the
2952 * last column and it is left-justified, don't include a width
2955 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2956 (void) printf("%s", propstr);
2957 else if (right_justify)
2958 (void) printf("%*s", width, propstr);
2960 (void) printf("%-*s", width, propstr);
2963 (void) printf("\n");
2967 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
2972 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2975 case ZPOOL_PROP_EXPANDSZ:
2977 (void) strlcpy(propval, "-", sizeof (propval));
2979 zfs_nicenum(value, propval, sizeof (propval));
2981 case ZPOOL_PROP_FRAGMENTATION:
2982 if (value == ZFS_FRAG_INVALID) {
2983 (void) strlcpy(propval, "-", sizeof (propval));
2985 (void) snprintf(propval, sizeof (propval), "%llu%%",
2989 case ZPOOL_PROP_CAPACITY:
2990 (void) snprintf(propval, sizeof (propval), "%llu%%", value);
2993 zfs_nicenum(value, propval, sizeof (propval));
2997 (void) strlcpy(propval, "-", sizeof (propval));
3000 (void) printf("\t%s", propval);
3002 (void) printf(" %*s", width, propval);
3006 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3007 list_cbdata_t *cb, int depth)
3013 boolean_t scripted = cb->cb_scripted;
3014 uint64_t islog = B_FALSE;
3015 boolean_t haslog = B_FALSE;
3016 char *dashes = "%-*s - - - - - -\n";
3018 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3019 (uint64_t **)&vs, &c) == 0);
3022 boolean_t toplevel = (vs->vs_space != 0);
3026 (void) printf("\t%s", name);
3027 else if (strlen(name) + depth > cb->cb_namewidth)
3028 (void) printf("%*s%s", depth, "", name);
3030 (void) printf("%*s%s%*s", depth, "", name,
3031 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3034 * Print the properties for the individual vdevs. Some
3035 * properties are only applicable to toplevel vdevs. The
3036 * 'toplevel' boolean value is passed to the print_one_column()
3037 * to indicate that the value is valid.
3039 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3041 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3043 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3044 scripted, toplevel);
3045 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3047 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3048 vs->vs_fragmentation, scripted,
3049 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3050 cap = (vs->vs_space == 0) ? 0 :
3051 (vs->vs_alloc * 100 / vs->vs_space);
3052 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3053 (void) printf("\n");
3056 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3057 &child, &children) != 0)
3060 for (c = 0; c < children; c++) {
3061 uint64_t ishole = B_FALSE;
3063 if (nvlist_lookup_uint64(child[c],
3064 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3067 if (nvlist_lookup_uint64(child[c],
3068 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
3073 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3074 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3078 if (haslog == B_TRUE) {
3079 /* LINTED E_SEC_PRINTF_VAR_FMT */
3080 (void) printf(dashes, cb->cb_namewidth, "log");
3081 for (c = 0; c < children; c++) {
3082 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3083 &islog) != 0 || !islog)
3085 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3086 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3091 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3092 &child, &children) == 0 && children > 0) {
3093 /* LINTED E_SEC_PRINTF_VAR_FMT */
3094 (void) printf(dashes, cb->cb_namewidth, "cache");
3095 for (c = 0; c < children; c++) {
3096 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3097 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3102 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
3103 &children) == 0 && children > 0) {
3104 /* LINTED E_SEC_PRINTF_VAR_FMT */
3105 (void) printf(dashes, cb->cb_namewidth, "spare");
3106 for (c = 0; c < children; c++) {
3107 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3108 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3116 * Generic callback function to list a pool.
3119 list_callback(zpool_handle_t *zhp, void *data)
3121 list_cbdata_t *cbp = data;
3125 config = zpool_get_config(zhp, NULL);
3127 print_pool(zhp, cbp);
3128 if (!cbp->cb_verbose)
3131 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3133 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3139 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3141 * -H Scripted mode. Don't display headers, and separate properties
3143 * -o List of properties to display. Defaults to
3144 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3145 * "dedupratio,health,altroot"
3146 * -p Diplay values in parsable (exact) format.
3147 * -T Display a timestamp in date(1) or Unix format
3149 * List all pools in the system, whether or not they're healthy. Output space
3150 * statistics for each one, as well as health status summary.
3153 zpool_do_list(int argc, char **argv)
3157 list_cbdata_t cb = { 0 };
3158 static char default_props[] =
3159 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3160 "dedupratio,health,altroot";
3161 char *props = default_props;
3162 unsigned long interval = 0, count = 0;
3164 boolean_t first = B_TRUE;
3167 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3170 cb.cb_scripted = B_TRUE;
3176 cb.cb_literal = B_TRUE;
3179 get_timestamp_arg(*optarg);
3182 cb.cb_verbose = B_TRUE;
3185 (void) fprintf(stderr, gettext("missing argument for "
3186 "'%c' option\n"), optopt);
3190 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3199 get_interval_count(&argc, argv, &interval, &count);
3201 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3205 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3209 if (pool_list_count(list) == 0)
3212 cb.cb_namewidth = 0;
3213 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3215 if (timestamp_fmt != NODATE)
3216 print_timestamp(timestamp_fmt);
3218 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3222 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3227 if (count != 0 && --count == 0)
3230 pool_list_free(list);
3231 (void) sleep(interval);
3234 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3235 (void) printf(gettext("no pools available\n"));
3239 pool_list_free(list);
3240 zprop_free_list(cb.cb_proplist);
3245 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3247 boolean_t force = B_FALSE;
3250 char *poolname, *old_disk, *new_disk;
3251 zpool_handle_t *zhp;
3255 while ((c = getopt(argc, argv, "f")) != -1) {
3261 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3270 /* get pool name and check number of arguments */
3272 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3279 (void) fprintf(stderr,
3280 gettext("missing <device> specification\n"));
3288 (void) fprintf(stderr,
3289 gettext("missing <new_device> specification\n"));
3292 new_disk = old_disk;
3302 (void) fprintf(stderr, gettext("too many arguments\n"));
3306 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3309 if (zpool_get_config(zhp, NULL) == NULL) {
3310 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3316 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3318 if (nvroot == NULL) {
3323 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3325 nvlist_free(nvroot);
3332 * zpool replace [-f] <pool> <device> <new_device>
3334 * -f Force attach, even if <new_device> appears to be in use.
3336 * Replace <device> with <new_device>.
3340 zpool_do_replace(int argc, char **argv)
3342 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3346 * zpool attach [-f] <pool> <device> <new_device>
3348 * -f Force attach, even if <new_device> appears to be in use.
3350 * Attach <new_device> to the mirror containing <device>. If <device> is not
3351 * part of a mirror, then <device> will be transformed into a mirror of
3352 * <device> and <new_device>. In either case, <new_device> will begin life
3353 * with a DTL of [0, now], and will immediately begin to resilver itself.
3356 zpool_do_attach(int argc, char **argv)
3358 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3362 * zpool detach [-f] <pool> <device>
3364 * -f Force detach of <device>, even if DTLs argue against it
3365 * (not supported yet)
3367 * Detach a device from a mirror. The operation will be refused if <device>
3368 * is the last device in the mirror, or if the DTLs indicate that this device
3369 * has the only valid copy of some data.
3373 zpool_do_detach(int argc, char **argv)
3376 char *poolname, *path;
3377 zpool_handle_t *zhp;
3381 while ((c = getopt(argc, argv, "f")) != -1) {
3385 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3394 /* get pool name and check number of arguments */
3396 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3401 (void) fprintf(stderr,
3402 gettext("missing <device> specification\n"));
3409 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3412 ret = zpool_vdev_detach(zhp, path);
3420 * zpool split [-n] [-o prop=val] ...
3422 * [-R altroot] <pool> <newpool> [<device> ...]
3424 * -n Do not split the pool, but display the resulting layout if
3425 * it were to be split.
3426 * -o Set property=value, or set mount options.
3427 * -R Mount the split-off pool under an alternate root.
3429 * Splits the named pool and gives it the new pool name. Devices to be split
3430 * off may be listed, provided that no more than one device is specified
3431 * per top-level vdev mirror. The newly split pool is left in an exported
3432 * state unless -R is specified.
3434 * Restrictions: the top-level of the pool pool must only be made up of
3435 * mirrors; all devices in the pool must be healthy; no device may be
3436 * undergoing a resilvering operation.
3439 zpool_do_split(int argc, char **argv)
3441 char *srcpool, *newpool, *propval;
3442 char *mntopts = NULL;
3445 zpool_handle_t *zhp;
3446 nvlist_t *config, *props = NULL;
3448 flags.dryrun = B_FALSE;
3449 flags.import = B_FALSE;
3452 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3455 flags.import = B_TRUE;
3457 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3458 &props, B_TRUE) != 0) {
3464 flags.dryrun = B_TRUE;
3467 if ((propval = strchr(optarg, '=')) != NULL) {
3470 if (add_prop_list(optarg, propval,
3471 &props, B_TRUE) != 0) {
3480 (void) fprintf(stderr, gettext("missing argument for "
3481 "'%c' option\n"), optopt);
3485 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3492 if (!flags.import && mntopts != NULL) {
3493 (void) fprintf(stderr, gettext("setting mntopts is only "
3494 "valid when importing the pool\n"));
3502 (void) fprintf(stderr, gettext("Missing pool name\n"));
3506 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3516 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3519 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3520 if (config == NULL) {
3524 (void) printf(gettext("would create '%s' with the "
3525 "following layout:\n\n"), newpool);
3526 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3528 nvlist_free(config);
3533 if (ret != 0 || flags.dryrun || !flags.import)
3537 * The split was successful. Now we need to open the new
3538 * pool and import it.
3540 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3542 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3543 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3545 (void) fprintf(stderr, gettext("Split was successful, but "
3546 "the datasets could not all be mounted\n"));
3547 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3548 "different altroot\n"), "zpool import");
3558 * zpool online <pool> <device> ...
3561 zpool_do_online(int argc, char **argv)
3565 zpool_handle_t *zhp;
3567 vdev_state_t newstate;
3571 while ((c = getopt(argc, argv, "et")) != -1) {
3574 flags |= ZFS_ONLINE_EXPAND;
3578 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3587 /* get pool name and check number of arguments */
3589 (void) fprintf(stderr, gettext("missing pool name\n"));
3593 (void) fprintf(stderr, gettext("missing device name\n"));
3599 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3602 for (i = 1; i < argc; i++) {
3603 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3604 if (newstate != VDEV_STATE_HEALTHY) {
3605 (void) printf(gettext("warning: device '%s' "
3606 "onlined, but remains in faulted state\n"),
3608 if (newstate == VDEV_STATE_FAULTED)
3609 (void) printf(gettext("use 'zpool "
3610 "clear' to restore a faulted "
3613 (void) printf(gettext("use 'zpool "
3614 "replace' to replace devices "
3615 "that are no longer present\n"));
3628 * zpool offline [-ft] <pool> <device> ...
3630 * -f Force the device into the offline state, even if doing
3631 * so would appear to compromise pool availability.
3632 * (not supported yet)
3634 * -t Only take the device off-line temporarily. The offline
3635 * state will not be persistent across reboots.
3639 zpool_do_offline(int argc, char **argv)
3643 zpool_handle_t *zhp;
3645 boolean_t istmp = B_FALSE;
3648 while ((c = getopt(argc, argv, "ft")) != -1) {
3655 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3664 /* get pool name and check number of arguments */
3666 (void) fprintf(stderr, gettext("missing pool name\n"));
3670 (void) fprintf(stderr, gettext("missing device name\n"));
3676 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3679 for (i = 1; i < argc; i++) {
3680 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3690 * zpool clear <pool> [device]
3692 * Clear all errors associated with a pool or a particular device.
3695 zpool_do_clear(int argc, char **argv)
3699 boolean_t dryrun = B_FALSE;
3700 boolean_t do_rewind = B_FALSE;
3701 boolean_t xtreme_rewind = B_FALSE;
3702 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3703 nvlist_t *policy = NULL;
3704 zpool_handle_t *zhp;
3705 char *pool, *device;
3708 while ((c = getopt(argc, argv, "FnX")) != -1) {
3717 xtreme_rewind = B_TRUE;
3720 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3730 (void) fprintf(stderr, gettext("missing pool name\n"));
3735 (void) fprintf(stderr, gettext("too many arguments\n"));
3739 if ((dryrun || xtreme_rewind) && !do_rewind) {
3740 (void) fprintf(stderr,
3741 gettext("-n or -X only meaningful with -F\n"));
3745 rewind_policy = ZPOOL_TRY_REWIND;
3747 rewind_policy = ZPOOL_DO_REWIND;
3749 rewind_policy |= ZPOOL_EXTREME_REWIND;
3751 /* In future, further rewind policy choices can be passed along here */
3752 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3753 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3757 device = argc == 2 ? argv[1] : NULL;
3759 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3760 nvlist_free(policy);
3764 if (zpool_clear(zhp, device, policy) != 0)
3769 nvlist_free(policy);
3775 * zpool reguid <pool>
3778 zpool_do_reguid(int argc, char **argv)
3782 zpool_handle_t *zhp;
3786 while ((c = getopt(argc, argv, "")) != -1) {
3789 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3798 /* get pool name and check number of arguments */
3800 (void) fprintf(stderr, gettext("missing pool name\n"));
3805 (void) fprintf(stderr, gettext("too many arguments\n"));
3810 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3813 ret = zpool_reguid(zhp);
3821 * zpool reopen <pool>
3823 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3826 zpool_do_reopen(int argc, char **argv)
3830 zpool_handle_t *zhp;
3834 while ((c = getopt(argc, argv, "")) != -1) {
3837 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3847 (void) fprintf(stderr, gettext("missing pool name\n"));
3852 (void) fprintf(stderr, gettext("too many arguments\n"));
3857 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3860 ret = zpool_reopen(zhp);
3865 typedef struct scrub_cbdata {
3872 scrub_callback(zpool_handle_t *zhp, void *data)
3874 scrub_cbdata_t *cb = data;
3878 * Ignore faulted pools.
3880 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3881 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3882 "currently unavailable\n"), zpool_get_name(zhp));
3886 err = zpool_scan(zhp, cb->cb_type);
3892 * zpool scrub [-s] <pool> ...
3894 * -s Stop. Stops any in-progress scrub.
3897 zpool_do_scrub(int argc, char **argv)
3902 cb.cb_type = POOL_SCAN_SCRUB;
3905 while ((c = getopt(argc, argv, "s")) != -1) {
3908 cb.cb_type = POOL_SCAN_NONE;
3911 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3923 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3927 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3930 typedef struct status_cbdata {
3932 boolean_t cb_allpools;
3933 boolean_t cb_verbose;
3934 boolean_t cb_explain;
3936 boolean_t cb_dedup_stats;
3940 * Print out detailed scrub status.
3943 print_scan_status(pool_scan_stat_t *ps)
3946 uint64_t elapsed, mins_left, hours_left;
3947 uint64_t pass_exam, examined, total;
3949 double fraction_done;
3950 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3952 (void) printf(gettext(" scan: "));
3954 /* If there's never been a scan, there's not much to say. */
3955 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3956 ps->pss_func >= POOL_SCAN_FUNCS) {
3957 (void) printf(gettext("none requested\n"));
3961 start = ps->pss_start_time;
3962 end = ps->pss_end_time;
3963 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3965 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3966 ps->pss_func == POOL_SCAN_RESILVER);
3968 * Scan is finished or canceled.
3970 if (ps->pss_state == DSS_FINISHED) {
3971 uint64_t minutes_taken = (end - start) / 60;
3974 if (ps->pss_func == POOL_SCAN_SCRUB) {
3975 fmt = gettext("scrub repaired %s in %lluh%um with "
3976 "%llu errors on %s");
3977 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3978 fmt = gettext("resilvered %s in %lluh%um with "
3979 "%llu errors on %s");
3982 (void) printf(fmt, processed_buf,
3983 (u_longlong_t)(minutes_taken / 60),
3984 (uint_t)(minutes_taken % 60),
3985 (u_longlong_t)ps->pss_errors,
3986 ctime((time_t *)&end));
3988 } else if (ps->pss_state == DSS_CANCELED) {
3989 if (ps->pss_func == POOL_SCAN_SCRUB) {
3990 (void) printf(gettext("scrub canceled on %s"),
3992 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3993 (void) printf(gettext("resilver canceled on %s"),
3999 assert(ps->pss_state == DSS_SCANNING);
4002 * Scan is in progress.
4004 if (ps->pss_func == POOL_SCAN_SCRUB) {
4005 (void) printf(gettext("scrub in progress since %s"),
4007 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4008 (void) printf(gettext("resilver in progress since %s"),
4012 examined = ps->pss_examined ? ps->pss_examined : 1;
4013 total = ps->pss_to_examine;
4014 fraction_done = (double)examined / total;
4016 /* elapsed time for this pass */
4017 elapsed = time(NULL) - ps->pss_pass_start;
4018 elapsed = elapsed ? elapsed : 1;
4019 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4020 rate = pass_exam / elapsed;
4021 rate = rate ? rate : 1;
4022 mins_left = ((total - examined) / rate) / 60;
4023 hours_left = mins_left / 60;
4025 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4026 zfs_nicenum(total, total_buf, sizeof (total_buf));
4027 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4030 * do not print estimated time if hours_left is more than 30 days
4032 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4033 examined_buf, total_buf, rate_buf);
4034 if (hours_left < (30 * 24)) {
4035 (void) printf(gettext(", %lluh%um to go\n"),
4036 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4038 (void) printf(gettext(
4039 ", (scan is slow, no estimated time)\n"));
4042 if (ps->pss_func == POOL_SCAN_RESILVER) {
4043 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4044 processed_buf, 100 * fraction_done);
4045 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4046 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4047 processed_buf, 100 * fraction_done);
4052 print_error_log(zpool_handle_t *zhp)
4054 nvlist_t *nverrlist = NULL;
4057 size_t len = MAXPATHLEN * 2;
4059 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4060 (void) printf("errors: List of errors unavailable "
4061 "(insufficient privileges)\n");
4065 (void) printf("errors: Permanent errors have been "
4066 "detected in the following files:\n\n");
4068 pathname = safe_malloc(len);
4070 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4072 uint64_t dsobj, obj;
4074 verify(nvpair_value_nvlist(elem, &nv) == 0);
4075 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4077 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4079 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4080 (void) printf("%7s %s\n", "", pathname);
4083 nvlist_free(nverrlist);
4087 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4096 (void) printf(gettext("\tspares\n"));
4098 for (i = 0; i < nspares; i++) {
4099 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4100 print_status_config(zhp, name, spares[i],
4101 namewidth, 2, B_TRUE);
4107 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4116 (void) printf(gettext("\tcache\n"));
4118 for (i = 0; i < nl2cache; i++) {
4119 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4120 print_status_config(zhp, name, l2cache[i],
4121 namewidth, 2, B_FALSE);
4127 print_dedup_stats(nvlist_t *config)
4129 ddt_histogram_t *ddh;
4135 * If the pool was faulted then we may not have been able to
4136 * obtain the config. Otherwise, if we have anything in the dedup
4137 * table continue processing the stats.
4139 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4140 (uint64_t **)&ddo, &c) != 0)
4143 (void) printf("\n");
4144 (void) printf(gettext(" dedup: "));
4145 if (ddo->ddo_count == 0) {
4146 (void) printf(gettext("no DDT entries\n"));
4150 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4151 (u_longlong_t)ddo->ddo_count,
4152 (u_longlong_t)ddo->ddo_dspace,
4153 (u_longlong_t)ddo->ddo_mspace);
4155 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4156 (uint64_t **)&dds, &c) == 0);
4157 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4158 (uint64_t **)&ddh, &c) == 0);
4159 zpool_dump_ddt(dds, ddh);
4163 * Display a summary of pool status. Displays a summary such as:
4167 * reason: One or more devices ...
4168 * see: http://illumos.org/msg/ZFS-xxxx-01
4174 * When given the '-v' option, we print out the complete config. If the '-e'
4175 * option is specified, then we print out error rate information as well.
4178 status_callback(zpool_handle_t *zhp, void *data)
4180 status_cbdata_t *cbp = data;
4181 nvlist_t *config, *nvroot;
4188 config = zpool_get_config(zhp, NULL);
4189 reason = zpool_get_status(zhp, &msgid);
4194 * If we were given 'zpool status -x', only report those pools with
4197 if (cbp->cb_explain &&
4198 (reason == ZPOOL_STATUS_OK ||
4199 reason == ZPOOL_STATUS_VERSION_OLDER ||
4200 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4201 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4202 if (!cbp->cb_allpools) {
4203 (void) printf(gettext("pool '%s' is healthy\n"),
4204 zpool_get_name(zhp));
4206 cbp->cb_first = B_FALSE;
4212 cbp->cb_first = B_FALSE;
4214 (void) printf("\n");
4216 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4218 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4219 (uint64_t **)&vs, &c) == 0);
4220 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4222 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4223 (void) printf(gettext(" state: %s\n"), health);
4226 case ZPOOL_STATUS_MISSING_DEV_R:
4227 (void) printf(gettext("status: One or more devices could not "
4228 "be opened. Sufficient replicas exist for\n\tthe pool to "
4229 "continue functioning in a degraded state.\n"));
4230 (void) printf(gettext("action: Attach the missing device and "
4231 "online it using 'zpool online'.\n"));
4234 case ZPOOL_STATUS_MISSING_DEV_NR:
4235 (void) printf(gettext("status: One or more devices could not "
4236 "be opened. There are insufficient\n\treplicas for the "
4237 "pool to continue functioning.\n"));
4238 (void) printf(gettext("action: Attach the missing device and "
4239 "online it using 'zpool online'.\n"));
4242 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4243 (void) printf(gettext("status: One or more devices could not "
4244 "be used because the label is missing or\n\tinvalid. "
4245 "Sufficient replicas exist for the pool to continue\n\t"
4246 "functioning in a degraded state.\n"));
4247 (void) printf(gettext("action: Replace the device using "
4248 "'zpool replace'.\n"));
4251 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4252 (void) printf(gettext("status: One or more devices could not "
4253 "be used because the label is missing \n\tor invalid. "
4254 "There are insufficient replicas for the pool to "
4255 "continue\n\tfunctioning.\n"));
4256 zpool_explain_recover(zpool_get_handle(zhp),
4257 zpool_get_name(zhp), reason, config);
4260 case ZPOOL_STATUS_FAILING_DEV:
4261 (void) printf(gettext("status: One or more devices has "
4262 "experienced an unrecoverable error. An\n\tattempt was "
4263 "made to correct the error. Applications are "
4265 (void) printf(gettext("action: Determine if the device needs "
4266 "to be replaced, and clear the errors\n\tusing "
4267 "'zpool clear' or replace the device with 'zpool "
4271 case ZPOOL_STATUS_OFFLINE_DEV:
4272 (void) printf(gettext("status: One or more devices has "
4273 "been taken offline by the administrator.\n\tSufficient "
4274 "replicas exist for the pool to continue functioning in "
4275 "a\n\tdegraded state.\n"));
4276 (void) printf(gettext("action: Online the device using "
4277 "'zpool online' or replace the device with\n\t'zpool "
4281 case ZPOOL_STATUS_REMOVED_DEV:
4282 (void) printf(gettext("status: One or more devices has "
4283 "been removed by the administrator.\n\tSufficient "
4284 "replicas exist for the pool to continue functioning in "
4285 "a\n\tdegraded state.\n"));
4286 (void) printf(gettext("action: Online the device using "
4287 "'zpool online' or replace the device with\n\t'zpool "
4291 case ZPOOL_STATUS_RESILVERING:
4292 (void) printf(gettext("status: One or more devices is "
4293 "currently being resilvered. The pool will\n\tcontinue "
4294 "to function, possibly in a degraded state.\n"));
4295 (void) printf(gettext("action: Wait for the resilver to "
4299 case ZPOOL_STATUS_CORRUPT_DATA:
4300 (void) printf(gettext("status: One or more devices has "
4301 "experienced an error resulting in data\n\tcorruption. "
4302 "Applications may be affected.\n"));
4303 (void) printf(gettext("action: Restore the file in question "
4304 "if possible. Otherwise restore the\n\tentire pool from "
4308 case ZPOOL_STATUS_CORRUPT_POOL:
4309 (void) printf(gettext("status: The pool metadata is corrupted "
4310 "and the pool cannot be opened.\n"));
4311 zpool_explain_recover(zpool_get_handle(zhp),
4312 zpool_get_name(zhp), reason, config);
4315 case ZPOOL_STATUS_VERSION_OLDER:
4316 (void) printf(gettext("status: The pool is formatted using a "
4317 "legacy on-disk format. The pool can\n\tstill be used, "
4318 "but some features are unavailable.\n"));
4319 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4320 "upgrade'. Once this is done, the\n\tpool will no longer "
4321 "be accessible on software that does not support feature\n"
4325 case ZPOOL_STATUS_VERSION_NEWER:
4326 (void) printf(gettext("status: The pool has been upgraded to a "
4327 "newer, incompatible on-disk version.\n\tThe pool cannot "
4328 "be accessed on this system.\n"));
4329 (void) printf(gettext("action: Access the pool from a system "
4330 "running more recent software, or\n\trestore the pool from "
4334 case ZPOOL_STATUS_FEAT_DISABLED:
4335 (void) printf(gettext("status: Some supported features are not "
4336 "enabled on the pool. The pool can\n\tstill be used, but "
4337 "some features are unavailable.\n"));
4338 (void) printf(gettext("action: Enable all features using "
4339 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4340 "longer be accessible by software that does not support\n\t"
4341 "the features. See zpool-features(7) for details.\n"));
4344 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4345 (void) printf(gettext("status: The pool cannot be accessed on "
4346 "this system because it uses the\n\tfollowing feature(s) "
4347 "not supported on this system:\n"));
4348 zpool_print_unsup_feat(config);
4349 (void) printf("\n");
4350 (void) printf(gettext("action: Access the pool from a system "
4351 "that supports the required feature(s),\n\tor restore the "
4352 "pool from backup.\n"));
4355 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4356 (void) printf(gettext("status: The pool can only be accessed "
4357 "in read-only mode on this system. It\n\tcannot be "
4358 "accessed in read-write mode because it uses the "
4359 "following\n\tfeature(s) not supported on this system:\n"));
4360 zpool_print_unsup_feat(config);
4361 (void) printf("\n");
4362 (void) printf(gettext("action: The pool cannot be accessed in "
4363 "read-write mode. Import the pool with\n"
4364 "\t\"-o readonly=on\", access the pool from a system that "
4365 "supports the\n\trequired feature(s), or restore the "
4366 "pool from backup.\n"));
4369 case ZPOOL_STATUS_FAULTED_DEV_R:
4370 (void) printf(gettext("status: One or more devices are "
4371 "faulted in response to persistent errors.\n\tSufficient "
4372 "replicas exist for the pool to continue functioning "
4373 "in a\n\tdegraded state.\n"));
4374 (void) printf(gettext("action: Replace the faulted device, "
4375 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4378 case ZPOOL_STATUS_FAULTED_DEV_NR:
4379 (void) printf(gettext("status: One or more devices are "
4380 "faulted in response to persistent errors. There are "
4381 "insufficient replicas for the pool to\n\tcontinue "
4383 (void) printf(gettext("action: Destroy and re-create the pool "
4384 "from a backup source. Manually marking the device\n"
4385 "\trepaired using 'zpool clear' may allow some data "
4386 "to be recovered.\n"));
4389 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4390 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4391 (void) printf(gettext("status: One or more devices are "
4392 "faulted in response to IO failures.\n"));
4393 (void) printf(gettext("action: Make sure the affected devices "
4394 "are connected, then run 'zpool clear'.\n"));
4397 case ZPOOL_STATUS_BAD_LOG:
4398 (void) printf(gettext("status: An intent log record "
4399 "could not be read.\n"
4400 "\tWaiting for adminstrator intervention to fix the "
4401 "faulted pool.\n"));
4402 (void) printf(gettext("action: Either restore the affected "
4403 "device(s) and run 'zpool online',\n"
4404 "\tor ignore the intent log records by running "
4405 "'zpool clear'.\n"));
4408 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4409 (void) printf(gettext("status: One or more devices are "
4410 "configured to use a non-native block size.\n"
4411 "\tExpect reduced performance.\n"));
4412 (void) printf(gettext("action: Replace affected devices with "
4413 "devices that support the\n\tconfigured block size, or "
4414 "migrate data to a properly configured\n\tpool.\n"));
4419 * The remaining errors can't actually be generated, yet.
4421 assert(reason == ZPOOL_STATUS_OK);
4425 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4428 if (config != NULL) {
4431 nvlist_t **spares, **l2cache;
4432 uint_t nspares, nl2cache;
4433 pool_scan_stat_t *ps = NULL;
4435 (void) nvlist_lookup_uint64_array(nvroot,
4436 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4437 print_scan_status(ps);
4439 namewidth = max_width(zhp, nvroot, 0, 0);
4443 (void) printf(gettext("config:\n\n"));
4444 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4445 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4446 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4447 namewidth, 0, B_FALSE);
4449 if (num_logs(nvroot) > 0)
4450 print_logs(zhp, nvroot, namewidth, B_TRUE);
4451 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4452 &l2cache, &nl2cache) == 0)
4453 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4455 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4456 &spares, &nspares) == 0)
4457 print_spares(zhp, spares, nspares, namewidth);
4459 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4461 nvlist_t *nverrlist = NULL;
4464 * If the approximate error count is small, get a
4465 * precise count by fetching the entire log and
4466 * uniquifying the results.
4468 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4469 zpool_get_errlog(zhp, &nverrlist) == 0) {
4474 while ((elem = nvlist_next_nvpair(nverrlist,
4479 nvlist_free(nverrlist);
4481 (void) printf("\n");
4484 (void) printf(gettext("errors: No known data "
4486 else if (!cbp->cb_verbose)
4487 (void) printf(gettext("errors: %llu data "
4488 "errors, use '-v' for a list\n"),
4489 (u_longlong_t)nerr);
4491 print_error_log(zhp);
4494 if (cbp->cb_dedup_stats)
4495 print_dedup_stats(config);
4497 (void) printf(gettext("config: The configuration cannot be "
4505 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4507 * -v Display complete error logs
4508 * -x Display only pools with potential problems
4509 * -D Display dedup status (undocumented)
4510 * -T Display a timestamp in date(1) or Unix format
4512 * Describes the health status of all pools or some subset.
4515 zpool_do_status(int argc, char **argv)
4519 unsigned long interval = 0, count = 0;
4520 status_cbdata_t cb = { 0 };
4523 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4526 cb.cb_verbose = B_TRUE;
4529 cb.cb_explain = B_TRUE;
4532 cb.cb_dedup_stats = B_TRUE;
4535 get_timestamp_arg(*optarg);
4538 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4547 get_interval_count(&argc, argv, &interval, &count);
4550 cb.cb_allpools = B_TRUE;
4552 cb.cb_first = B_TRUE;
4555 if (timestamp_fmt != NODATE)
4556 print_timestamp(timestamp_fmt);
4558 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4559 status_callback, &cb);
4561 if (argc == 0 && cb.cb_count == 0)
4562 (void) printf(gettext("no pools available\n"));
4563 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4564 (void) printf(gettext("all pools are healthy\n"));
4572 if (count != 0 && --count == 0)
4575 (void) sleep(interval);
4581 typedef struct upgrade_cbdata {
4583 boolean_t cb_unavail;
4584 char cb_poolname[ZFS_MAX_DATASET_NAME_LEN];
4586 uint64_t cb_version;
4592 is_root_pool(zpool_handle_t *zhp)
4594 static struct statfs sfs;
4595 static char *poolname = NULL;
4596 static boolean_t stated = B_FALSE;
4601 if (statfs("/", &sfs) == -1) {
4602 (void) fprintf(stderr,
4603 "Unable to stat root file system: %s.\n",
4607 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4609 poolname = sfs.f_mntfromname;
4610 if ((slash = strchr(poolname, '/')) != NULL)
4613 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4617 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
4620 if (poolname[0] == '\0' && is_root_pool(zhp))
4621 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4623 #endif /* FreeBSD */
4626 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4630 uint64_t oldversion;
4632 config = zpool_get_config(zhp, NULL);
4633 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4636 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4637 assert(oldversion < version);
4639 ret = zpool_upgrade(zhp, version);
4643 if (version >= SPA_VERSION_FEATURES) {
4644 (void) printf(gettext("Successfully upgraded "
4645 "'%s' from version %llu to feature flags.\n"),
4646 zpool_get_name(zhp), oldversion);
4648 (void) printf(gettext("Successfully upgraded "
4649 "'%s' from version %llu to version %llu.\n"),
4650 zpool_get_name(zhp), oldversion, version);
4657 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4660 boolean_t firstff = B_TRUE;
4661 nvlist_t *enabled = zpool_get_features(zhp);
4664 for (i = 0; i < SPA_FEATURES; i++) {
4665 const char *fname = spa_feature_table[i].fi_uname;
4666 const char *fguid = spa_feature_table[i].fi_guid;
4667 if (!nvlist_exists(enabled, fguid)) {
4669 verify(-1 != asprintf(&propname, "feature@%s", fname));
4670 ret = zpool_set_prop(zhp, propname,
4671 ZFS_FEATURE_ENABLED);
4679 (void) printf(gettext("Enabled the "
4680 "following features on '%s':\n"),
4681 zpool_get_name(zhp));
4684 (void) printf(gettext(" %s\n"), fname);
4695 upgrade_cb(zpool_handle_t *zhp, void *arg)
4697 upgrade_cbdata_t *cbp = arg;
4700 boolean_t printnl = B_FALSE;
4703 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4704 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
4705 "currently unavailable.\n\n"), zpool_get_name(zhp));
4706 cbp->cb_unavail = B_TRUE;
4707 /* Allow iteration to continue. */
4711 config = zpool_get_config(zhp, NULL);
4712 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4715 assert(SPA_VERSION_IS_SUPPORTED(version));
4717 if (version < cbp->cb_version) {
4718 cbp->cb_first = B_FALSE;
4719 ret = upgrade_version(zhp, cbp->cb_version);
4723 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4724 sizeof(cbp->cb_poolname));
4725 #endif /* __FreeBSD__ */
4730 * If they did "zpool upgrade -a", then we could
4731 * be doing ioctls to different pools. We need
4732 * to log this history once to each pool, and bypass
4733 * the normal history logging that happens in main().
4735 (void) zpool_log_history(g_zfs, history_str);
4736 log_history = B_FALSE;
4740 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4742 ret = upgrade_enable_all(zhp, &count);
4747 cbp->cb_first = B_FALSE;
4750 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4751 sizeof(cbp->cb_poolname));
4752 #endif /* __FreeBSD__ */
4754 * If they did "zpool upgrade -a", then we could
4755 * be doing ioctls to different pools. We need
4756 * to log this history once to each pool, and bypass
4757 * the normal history logging that happens in main().
4759 (void) zpool_log_history(g_zfs, history_str);
4760 log_history = B_FALSE;
4765 (void) printf(gettext("\n"));
4772 upgrade_list_unavail(zpool_handle_t *zhp, void *arg)
4774 upgrade_cbdata_t *cbp = arg;
4776 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4777 if (cbp->cb_first) {
4778 (void) fprintf(stderr, gettext("The following pools "
4779 "are unavailable and cannot be upgraded as this "
4781 (void) fprintf(stderr, gettext("POOL\n"));
4782 (void) fprintf(stderr, gettext("------------\n"));
4783 cbp->cb_first = B_FALSE;
4785 (void) printf(gettext("%s\n"), zpool_get_name(zhp));
4786 cbp->cb_unavail = B_TRUE;
4792 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4794 upgrade_cbdata_t *cbp = arg;
4798 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4800 * This will have been reported by upgrade_list_unavail so
4801 * just allow iteration to continue.
4803 cbp->cb_unavail = B_TRUE;
4807 config = zpool_get_config(zhp, NULL);
4808 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4811 assert(SPA_VERSION_IS_SUPPORTED(version));
4813 if (version < SPA_VERSION_FEATURES) {
4814 if (cbp->cb_first) {
4815 (void) printf(gettext("The following pools are "
4816 "formatted with legacy version numbers and can\n"
4817 "be upgraded to use feature flags. After "
4818 "being upgraded, these pools\nwill no "
4819 "longer be accessible by software that does not "
4820 "support feature\nflags.\n\n"));
4821 (void) printf(gettext("VER POOL\n"));
4822 (void) printf(gettext("--- ------------\n"));
4823 cbp->cb_first = B_FALSE;
4826 (void) printf("%2llu %s\n", (u_longlong_t)version,
4827 zpool_get_name(zhp));
4834 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4836 upgrade_cbdata_t *cbp = arg;
4840 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4842 * This will have been reported by upgrade_list_unavail so
4843 * just allow iteration to continue.
4845 cbp->cb_unavail = B_TRUE;
4849 config = zpool_get_config(zhp, NULL);
4850 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4853 if (version >= SPA_VERSION_FEATURES) {
4855 boolean_t poolfirst = B_TRUE;
4856 nvlist_t *enabled = zpool_get_features(zhp);
4858 for (i = 0; i < SPA_FEATURES; i++) {
4859 const char *fguid = spa_feature_table[i].fi_guid;
4860 const char *fname = spa_feature_table[i].fi_uname;
4861 if (!nvlist_exists(enabled, fguid)) {
4862 if (cbp->cb_first) {
4863 (void) printf(gettext("\nSome "
4864 "supported features are not "
4865 "enabled on the following pools. "
4866 "Once a\nfeature is enabled the "
4867 "pool may become incompatible with "
4868 "software\nthat does not support "
4870 "zpool-features(7) for "
4872 (void) printf(gettext("POOL "
4874 (void) printf(gettext("------"
4876 cbp->cb_first = B_FALSE;
4880 (void) printf(gettext("%s\n"),
4881 zpool_get_name(zhp));
4882 poolfirst = B_FALSE;
4885 (void) printf(gettext(" %s\n"), fname);
4895 upgrade_one(zpool_handle_t *zhp, void *data)
4897 boolean_t printnl = B_FALSE;
4898 upgrade_cbdata_t *cbp = data;
4899 uint64_t cur_version;
4902 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4903 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
4904 "is currently unavailable.\n\n"), zpool_get_name(zhp));
4905 cbp->cb_unavail = B_TRUE;
4909 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4910 (void) printf(gettext("'log' is now a reserved word\n"
4911 "Pool 'log' must be renamed using export and import"
4912 " to upgrade.\n\n"));
4916 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4917 if (cur_version > cbp->cb_version) {
4918 (void) printf(gettext("Pool '%s' is already formatted "
4919 "using more current version '%llu'.\n\n"),
4920 zpool_get_name(zhp), cur_version);
4924 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4925 (void) printf(gettext("Pool '%s' is already formatted "
4926 "using version %llu.\n\n"), zpool_get_name(zhp),
4931 if (cur_version != cbp->cb_version) {
4933 ret = upgrade_version(zhp, cbp->cb_version);
4937 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4938 sizeof(cbp->cb_poolname));
4939 #endif /* __FreeBSD__ */
4942 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4944 ret = upgrade_enable_all(zhp, &count);
4951 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4952 sizeof(cbp->cb_poolname));
4953 #endif /* __FreeBSD __*/
4954 } else if (cur_version == SPA_VERSION) {
4955 (void) printf(gettext("Pool '%s' already has all "
4956 "supported features enabled.\n\n"),
4957 zpool_get_name(zhp));
4962 (void) printf(gettext("\n"));
4971 * zpool upgrade [-V version] <-a | pool ...>
4973 * With no arguments, display downrev'd ZFS pool available for upgrade.
4974 * Individual pools can be upgraded by specifying the pool, and '-a' will
4975 * upgrade all pools.
4978 zpool_do_upgrade(int argc, char **argv)
4981 upgrade_cbdata_t cb = { 0 };
4983 boolean_t showversions = B_FALSE;
4984 boolean_t upgradeall = B_FALSE;
4989 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4992 upgradeall = B_TRUE;
4995 showversions = B_TRUE;
4998 cb.cb_version = strtoll(optarg, &end, 10);
5000 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5001 (void) fprintf(stderr,
5002 gettext("invalid version '%s'\n"), optarg);
5007 (void) fprintf(stderr, gettext("missing argument for "
5008 "'%c' option\n"), optopt);
5012 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5023 if (cb.cb_version == 0) {
5024 cb.cb_version = SPA_VERSION;
5025 } else if (!upgradeall && argc == 0) {
5026 (void) fprintf(stderr, gettext("-V option is "
5027 "incompatible with other arguments\n"));
5032 if (upgradeall || argc != 0) {
5033 (void) fprintf(stderr, gettext("-v option is "
5034 "incompatible with other arguments\n"));
5037 } else if (upgradeall) {
5039 (void) fprintf(stderr, gettext("-a option should not "
5040 "be used along with a pool name\n"));
5045 (void) printf(gettext("This system supports ZFS pool feature "
5050 (void) printf(gettext("The following features are "
5052 (void) printf(gettext("FEAT DESCRIPTION\n"));
5053 (void) printf("----------------------------------------------"
5054 "---------------\n");
5055 for (i = 0; i < SPA_FEATURES; i++) {
5056 zfeature_info_t *fi = &spa_feature_table[i];
5058 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
5059 " (read-only compatible)" : "";
5061 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5062 (void) printf(" %s\n", fi->fi_desc);
5064 (void) printf("\n");
5066 (void) printf(gettext("The following legacy versions are also "
5068 (void) printf(gettext("VER DESCRIPTION\n"));
5069 (void) printf("--- -----------------------------------------"
5070 "---------------\n");
5071 (void) printf(gettext(" 1 Initial ZFS version\n"));
5072 (void) printf(gettext(" 2 Ditto blocks "
5073 "(replicated metadata)\n"));
5074 (void) printf(gettext(" 3 Hot spares and double parity "
5076 (void) printf(gettext(" 4 zpool history\n"));
5077 (void) printf(gettext(" 5 Compression using the gzip "
5079 (void) printf(gettext(" 6 bootfs pool property\n"));
5080 (void) printf(gettext(" 7 Separate intent log devices\n"));
5081 (void) printf(gettext(" 8 Delegated administration\n"));
5082 (void) printf(gettext(" 9 refquota and refreservation "
5084 (void) printf(gettext(" 10 Cache devices\n"));
5085 (void) printf(gettext(" 11 Improved scrub performance\n"));
5086 (void) printf(gettext(" 12 Snapshot properties\n"));
5087 (void) printf(gettext(" 13 snapused property\n"));
5088 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5089 (void) printf(gettext(" 15 user/group space accounting\n"));
5090 (void) printf(gettext(" 16 stmf property support\n"));
5091 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5092 (void) printf(gettext(" 18 Snapshot user holds\n"));
5093 (void) printf(gettext(" 19 Log device removal\n"));
5094 (void) printf(gettext(" 20 Compression using zle "
5095 "(zero-length encoding)\n"));
5096 (void) printf(gettext(" 21 Deduplication\n"));
5097 (void) printf(gettext(" 22 Received properties\n"));
5098 (void) printf(gettext(" 23 Slim ZIL\n"));
5099 (void) printf(gettext(" 24 System attributes\n"));
5100 (void) printf(gettext(" 25 Improved scrub stats\n"));
5101 (void) printf(gettext(" 26 Improved snapshot deletion "
5103 (void) printf(gettext(" 27 Improved snapshot creation "
5105 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5106 (void) printf(gettext("\nFor more information on a particular "
5107 "version, including supported releases,\n"));
5108 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5109 } else if (argc == 0 && upgradeall) {
5110 cb.cb_first = B_TRUE;
5111 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5112 if (ret == 0 && cb.cb_first) {
5113 if (cb.cb_version == SPA_VERSION) {
5114 (void) printf(gettext("All %spools are already "
5115 "formatted using feature flags.\n\n"),
5116 cb.cb_unavail ? gettext("available ") : "");
5117 (void) printf(gettext("Every %sfeature flags "
5118 "pool already has all supported features "
5120 cb.cb_unavail ? gettext("available ") : "");
5122 (void) printf(gettext("All pools are already "
5123 "formatted with version %llu or higher.\n"),
5127 } else if (argc == 0) {
5128 cb.cb_first = B_TRUE;
5129 ret = zpool_iter(g_zfs, upgrade_list_unavail, &cb);
5133 (void) fprintf(stderr, "\n");
5136 cb.cb_first = B_TRUE;
5137 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5141 (void) printf(gettext("All %spools are formatted using "
5142 "feature flags.\n\n"), cb.cb_unavail ?
5143 gettext("available ") : "");
5145 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5146 "for a list of available legacy versions.\n"));
5149 cb.cb_first = B_TRUE;
5150 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5154 (void) printf(gettext("Every %sfeature flags pool has "
5155 "all supported features enabled.\n"),
5156 cb.cb_unavail ? gettext("available ") : "");
5158 (void) printf(gettext("\n"));
5161 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5165 if (cb.cb_poolname[0] != '\0') {
5167 "If you boot from pool '%s', don't forget to update boot code.\n"
5168 "Assuming you use GPT partitioning and da0 is your boot disk\n"
5169 "the following command will do it:\n"
5171 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5178 typedef struct hist_cbdata {
5185 * Print out the command history for a specific pool.
5188 get_history_one(zpool_handle_t *zhp, void *data)
5194 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5196 cb->first = B_FALSE;
5198 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5200 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5203 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5204 &records, &numrecords) == 0);
5205 for (i = 0; i < numrecords; i++) {
5206 nvlist_t *rec = records[i];
5209 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5213 tsec = fnvlist_lookup_uint64(records[i],
5215 (void) localtime_r(&tsec, &t);
5216 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5219 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5220 (void) printf("%s %s", tbuf,
5221 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5222 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5224 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5227 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5228 (void) printf("%s unrecognized record:\n",
5230 dump_nvlist(rec, 4);
5233 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5234 zfs_history_event_names[ievent],
5235 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5236 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5237 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5240 (void) printf("%s [txg:%lld] %s", tbuf,
5241 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5242 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5243 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5244 (void) printf(" %s (%llu)",
5245 fnvlist_lookup_string(rec,
5247 fnvlist_lookup_uint64(rec,
5250 (void) printf(" %s", fnvlist_lookup_string(rec,
5251 ZPOOL_HIST_INT_STR));
5252 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5255 (void) printf("%s ioctl %s\n", tbuf,
5256 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5257 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5258 (void) printf(" input:\n");
5259 dump_nvlist(fnvlist_lookup_nvlist(rec,
5260 ZPOOL_HIST_INPUT_NVL), 8);
5262 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5263 (void) printf(" output:\n");
5264 dump_nvlist(fnvlist_lookup_nvlist(rec,
5265 ZPOOL_HIST_OUTPUT_NVL), 8);
5270 (void) printf("%s unrecognized record:\n", tbuf);
5271 dump_nvlist(rec, 4);
5275 (void) printf("\n");
5278 (void) printf(" [");
5279 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5280 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5281 struct passwd *pwd = getpwuid(who);
5282 (void) printf("user %d ", (int)who);
5284 (void) printf("(%s) ", pwd->pw_name);
5286 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5287 (void) printf("on %s",
5288 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5290 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5291 (void) printf(":%s",
5292 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5295 (void) printf("\n");
5297 (void) printf("\n");
5304 * zpool history <pool>
5306 * Displays the history of commands that modified pools.
5309 zpool_do_history(int argc, char **argv)
5311 hist_cbdata_t cbdata = { 0 };
5315 cbdata.first = B_TRUE;
5317 while ((c = getopt(argc, argv, "li")) != -1) {
5320 cbdata.longfmt = B_TRUE;
5323 cbdata.internal = B_TRUE;
5326 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5334 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5337 if (argc == 0 && cbdata.first == B_TRUE) {
5338 (void) printf(gettext("no pools available\n"));
5346 get_callback(zpool_handle_t *zhp, void *data)
5348 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5349 char value[MAXNAMELEN];
5350 zprop_source_t srctype;
5353 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5356 * Skip the special fake placeholder. This will also skip
5357 * over the name property when 'all' is specified.
5359 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5360 pl == cbp->cb_proplist)
5363 if (pl->pl_prop == ZPROP_INVAL &&
5364 (zpool_prop_feature(pl->pl_user_prop) ||
5365 zpool_prop_unsupported(pl->pl_user_prop))) {
5366 srctype = ZPROP_SRC_LOCAL;
5368 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5369 value, sizeof (value)) == 0) {
5370 zprop_print_one_property(zpool_get_name(zhp),
5371 cbp, pl->pl_user_prop, value, srctype,
5375 if (zpool_get_prop(zhp, pl->pl_prop, value,
5376 sizeof (value), &srctype, cbp->cb_literal) != 0)
5379 zprop_print_one_property(zpool_get_name(zhp), cbp,
5380 zpool_prop_to_name(pl->pl_prop), value, srctype,
5388 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5390 * -H Scripted mode. Don't display headers, and separate properties
5392 * -o List of columns to display. Defaults to
5393 * "name,property,value,source".
5394 * -p Diplay values in parsable (exact) format.
5396 * Get properties of pools in the system. Output space statistics
5397 * for each one as well as other attributes.
5400 zpool_do_get(int argc, char **argv)
5402 zprop_get_cbdata_t cb = { 0 };
5403 zprop_list_t fake_name = { 0 };
5408 cb.cb_first = B_TRUE;
5411 * Set up default columns and sources.
5413 cb.cb_sources = ZPROP_SRC_ALL;
5414 cb.cb_columns[0] = GET_COL_NAME;
5415 cb.cb_columns[1] = GET_COL_PROPERTY;
5416 cb.cb_columns[2] = GET_COL_VALUE;
5417 cb.cb_columns[3] = GET_COL_SOURCE;
5418 cb.cb_type = ZFS_TYPE_POOL;
5421 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5424 cb.cb_literal = B_TRUE;
5427 cb.cb_scripted = B_TRUE;
5430 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5432 while (*optarg != '\0') {
5433 static char *col_subopts[] =
5434 { "name", "property", "value", "source",
5437 if (i == ZFS_GET_NCOLS) {
5438 (void) fprintf(stderr, gettext("too "
5439 "many fields given to -o "
5444 switch (getsubopt(&optarg, col_subopts,
5447 cb.cb_columns[i++] = GET_COL_NAME;
5450 cb.cb_columns[i++] = GET_COL_PROPERTY;
5453 cb.cb_columns[i++] = GET_COL_VALUE;
5456 cb.cb_columns[i++] = GET_COL_SOURCE;
5460 (void) fprintf(stderr,
5461 gettext("\"all\" conflicts "
5462 "with specific fields "
5463 "given to -o option\n"));
5466 cb.cb_columns[0] = GET_COL_NAME;
5467 cb.cb_columns[1] = GET_COL_PROPERTY;
5468 cb.cb_columns[2] = GET_COL_VALUE;
5469 cb.cb_columns[3] = GET_COL_SOURCE;
5473 (void) fprintf(stderr,
5474 gettext("invalid column name "
5475 "'%s'\n"), suboptarg);
5481 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5491 (void) fprintf(stderr, gettext("missing property "
5496 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5497 ZFS_TYPE_POOL) != 0)
5503 if (cb.cb_proplist != NULL) {
5504 fake_name.pl_prop = ZPOOL_PROP_NAME;
5505 fake_name.pl_width = strlen(gettext("NAME"));
5506 fake_name.pl_next = cb.cb_proplist;
5507 cb.cb_proplist = &fake_name;
5510 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5513 if (cb.cb_proplist == &fake_name)
5514 zprop_free_list(fake_name.pl_next);
5516 zprop_free_list(cb.cb_proplist);
5521 typedef struct set_cbdata {
5524 boolean_t cb_any_successful;
5528 set_callback(zpool_handle_t *zhp, void *data)
5531 set_cbdata_t *cb = (set_cbdata_t *)data;
5533 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5536 cb->cb_any_successful = B_TRUE;
5542 zpool_do_set(int argc, char **argv)
5544 set_cbdata_t cb = { 0 };
5547 if (argc > 1 && argv[1][0] == '-') {
5548 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5554 (void) fprintf(stderr, gettext("missing property=value "
5560 (void) fprintf(stderr, gettext("missing pool name\n"));
5565 (void) fprintf(stderr, gettext("too many pool names\n"));
5569 cb.cb_propname = argv[1];
5570 cb.cb_value = strchr(cb.cb_propname, '=');
5571 if (cb.cb_value == NULL) {
5572 (void) fprintf(stderr, gettext("missing value in "
5573 "property=value argument\n"));
5577 *(cb.cb_value) = '\0';
5580 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5587 find_command_idx(char *command, int *idx)
5591 for (i = 0; i < NCOMMAND; i++) {
5592 if (command_table[i].name == NULL)
5595 if (strcmp(command, command_table[i].name) == 0) {
5604 main(int argc, char **argv)
5610 (void) setlocale(LC_ALL, "");
5611 (void) textdomain(TEXT_DOMAIN);
5613 if ((g_zfs = libzfs_init()) == NULL) {
5614 (void) fprintf(stderr, gettext("internal error: failed to "
5615 "initialize ZFS library\n"));
5619 libzfs_print_on_error(g_zfs, B_TRUE);
5624 * Make sure the user has specified some command.
5627 (void) fprintf(stderr, gettext("missing command\n"));
5636 if (strcmp(cmdname, "-?") == 0)
5639 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5642 * Run the appropriate command.
5644 if (find_command_idx(cmdname, &i) == 0) {
5645 current_command = &command_table[i];
5646 ret = command_table[i].func(argc - 1, argv + 1);
5647 } else if (strchr(cmdname, '=')) {
5648 verify(find_command_idx("set", &i) == 0);
5649 current_command = &command_table[i];
5650 ret = command_table[i].func(argc, argv);
5651 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5653 * 'freeze' is a vile debugging abomination, so we treat
5656 zfs_cmd_t zc = { 0 };
5657 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5658 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5660 (void) fprintf(stderr, gettext("unrecognized "
5661 "command '%s'\n"), cmdname);
5665 if (ret == 0 && log_history)
5666 (void) zpool_log_history(g_zfs, history_str);
5671 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5672 * for the purposes of running ::findleaks.
5674 if (getenv("ZFS_ABORT") != NULL) {
5675 (void) printf("dumping core by request\n");