4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
51 #include <sys/fs/zfs.h>
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 #include "statcommon.h"
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
69 static int zpool_do_list(int, char **);
70 static int zpool_do_iostat(int, char **);
71 static int zpool_do_status(int, char **);
73 static int zpool_do_online(int, char **);
74 static int zpool_do_offline(int, char **);
75 static int zpool_do_clear(int, char **);
76 static int zpool_do_reopen(int, char **);
78 static int zpool_do_reguid(int, char **);
80 static int zpool_do_attach(int, char **);
81 static int zpool_do_detach(int, char **);
82 static int zpool_do_replace(int, char **);
83 static int zpool_do_split(int, char **);
85 static int zpool_do_scrub(int, char **);
87 static int zpool_do_import(int, char **);
88 static int zpool_do_export(int, char **);
90 static int zpool_do_upgrade(int, char **);
92 static int zpool_do_history(int, char **);
94 static int zpool_do_get(int, char **);
95 static int zpool_do_set(int, char **);
98 * These libumem hooks provide a reasonable set of defaults for the allocator's
99 * debugging facilities.
104 _umem_debug_init(void)
106 return ("default,verbose"); /* $UMEM_DEBUG setting */
110 _umem_logging_init(void)
112 return ("fail,contents"); /* $UMEM_LOGGING setting */
144 typedef struct zpool_command {
146 int (*func)(int, char **);
151 * Master command table. Each ZFS command has a name, associated function, and
152 * usage message. The usage messages need to be internationalized, so we have
153 * to have a function to return the usage message based on a command index.
155 * These commands are organized according to how they are displayed in the usage
156 * message. An empty command (one with a NULL name) indicates an empty line in
157 * the generic usage message.
159 static zpool_command_t command_table[] = {
160 { "create", zpool_do_create, HELP_CREATE },
161 { "destroy", zpool_do_destroy, HELP_DESTROY },
163 { "add", zpool_do_add, HELP_ADD },
164 { "remove", zpool_do_remove, HELP_REMOVE },
166 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
168 { "list", zpool_do_list, HELP_LIST },
169 { "iostat", zpool_do_iostat, HELP_IOSTAT },
170 { "status", zpool_do_status, HELP_STATUS },
172 { "online", zpool_do_online, HELP_ONLINE },
173 { "offline", zpool_do_offline, HELP_OFFLINE },
174 { "clear", zpool_do_clear, HELP_CLEAR },
175 { "reopen", zpool_do_reopen, HELP_REOPEN },
177 { "attach", zpool_do_attach, HELP_ATTACH },
178 { "detach", zpool_do_detach, HELP_DETACH },
179 { "replace", zpool_do_replace, HELP_REPLACE },
180 { "split", zpool_do_split, HELP_SPLIT },
182 { "scrub", zpool_do_scrub, HELP_SCRUB },
184 { "import", zpool_do_import, HELP_IMPORT },
185 { "export", zpool_do_export, HELP_EXPORT },
186 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
187 { "reguid", zpool_do_reguid, HELP_REGUID },
189 { "history", zpool_do_history, HELP_HISTORY },
190 { "get", zpool_do_get, HELP_GET },
191 { "set", zpool_do_set, HELP_SET },
194 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
196 static zpool_command_t *current_command;
197 static char history_str[HIS_MAX_RECORD_LEN];
198 static boolean_t log_history = B_TRUE;
199 static uint_t timestamp_fmt = NODATE;
202 get_usage(zpool_help_t idx) {
205 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
207 return (gettext("\tattach [-f] <pool> <device> "
210 return (gettext("\tclear [-nF] <pool> [device]\n"));
212 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
213 "\t [-O file-system-property=value] ... \n"
214 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
216 return (gettext("\tdestroy [-f] <pool>\n"));
218 return (gettext("\tdetach <pool> <device>\n"));
220 return (gettext("\texport [-f] <pool> ...\n"));
222 return (gettext("\thistory [-il] [<pool>] ...\n"));
224 return (gettext("\timport [-d dir] [-D]\n"
225 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
226 "\timport [-o mntopts] [-o property=value] ... \n"
227 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
228 "[-R root] [-F [-n]] -a\n"
229 "\timport [-o mntopts] [-o property=value] ... \n"
230 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
231 "[-R root] [-F [-n]]\n"
232 "\t <pool | id> [newpool]\n"));
234 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
236 case HELP_LABELCLEAR:
237 return (gettext("\tlabelclear [-f] <vdev>\n"));
239 return (gettext("\tlist [-Hpv] [-o property[,...]] "
240 "[-T d|u] [pool] ... [interval [count]]\n"));
242 return (gettext("\toffline [-t] <pool> <device> ...\n"));
244 return (gettext("\tonline [-e] <pool> <device> ...\n"));
246 return (gettext("\treplace [-f] <pool> <device> "
249 return (gettext("\tremove <pool> <device> ...\n"));
251 return (gettext("\treopen <pool>\n"));
253 return (gettext("\tscrub [-s] <pool> ...\n"));
255 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
258 return (gettext("\tupgrade [-v]\n"
259 "\tupgrade [-V version] <-a | pool ...>\n"));
261 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
262 "<\"all\" | property[,...]> <pool> ...\n"));
264 return (gettext("\tset <property=value> <pool> \n"));
266 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
267 "\t [-o property=value] <pool> <newpool> "
268 "[<device> ...]\n"));
270 return (gettext("\treguid <pool>\n"));
279 * Callback routine that will print out a pool property value.
282 print_prop_cb(int prop, void *cb)
286 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
288 if (zpool_prop_readonly(prop))
289 (void) fprintf(fp, " NO ");
291 (void) fprintf(fp, " YES ");
293 if (zpool_prop_values(prop) == NULL)
294 (void) fprintf(fp, "-\n");
296 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
302 * Display usage message. If we're inside a command, display only the usage for
303 * that command. Otherwise, iterate over the entire command table and display
304 * a complete usage message.
307 usage(boolean_t requested)
309 FILE *fp = requested ? stdout : stderr;
311 if (current_command == NULL) {
314 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
316 gettext("where 'command' is one of the following:\n\n"));
318 for (i = 0; i < NCOMMAND; i++) {
319 if (command_table[i].name == NULL)
320 (void) fprintf(fp, "\n");
322 (void) fprintf(fp, "%s",
323 get_usage(command_table[i].usage));
326 (void) fprintf(fp, gettext("usage:\n"));
327 (void) fprintf(fp, "%s", get_usage(current_command->usage));
330 if (current_command != NULL &&
331 ((strcmp(current_command->name, "set") == 0) ||
332 (strcmp(current_command->name, "get") == 0) ||
333 (strcmp(current_command->name, "list") == 0))) {
336 gettext("\nthe following properties are supported:\n"));
338 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
339 "PROPERTY", "EDIT", "VALUES");
341 /* Iterate over all properties */
342 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
345 (void) fprintf(fp, "\t%-15s ", "feature@...");
346 (void) fprintf(fp, "YES disabled | enabled | active\n");
348 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
349 "appended with a feature name.\nSee zpool-features(7).\n"));
353 * See comments at end of main().
355 if (getenv("ZFS_ABORT") != NULL) {
356 (void) printf("dumping core by request\n");
360 exit(requested ? 0 : 2);
364 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
365 boolean_t print_logs)
372 (void) printf("\t%*s%s\n", indent, "", name);
374 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
375 &child, &children) != 0)
378 for (c = 0; c < children; c++) {
379 uint64_t is_log = B_FALSE;
381 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
383 if ((is_log && !print_logs) || (!is_log && print_logs))
386 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
387 print_vdev_tree(zhp, vname, child[c], indent + 2,
394 prop_list_contains_feature(nvlist_t *proplist)
397 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
398 nvp = nvlist_next_nvpair(proplist, nvp)) {
399 if (zpool_prop_feature(nvpair_name(nvp)))
406 * Add a property pair (name, string-value) into a property nvlist.
409 add_prop_list(const char *propname, char *propval, nvlist_t **props,
412 zpool_prop_t prop = ZPROP_INVAL;
418 if (*props == NULL &&
419 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
420 (void) fprintf(stderr,
421 gettext("internal error: out of memory\n"));
428 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
430 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
431 !zpool_prop_feature(propname)) {
432 (void) fprintf(stderr, gettext("property '%s' is "
433 "not a valid pool property\n"), propname);
438 * feature@ properties and version should not be specified
441 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
442 nvlist_exists(proplist, vname)) ||
443 (prop == ZPOOL_PROP_VERSION &&
444 prop_list_contains_feature(proplist))) {
445 (void) fprintf(stderr, gettext("'feature@' and "
446 "'version' properties cannot be specified "
452 if (zpool_prop_feature(propname))
455 normnm = zpool_prop_to_name(prop);
457 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
458 normnm = zfs_prop_to_name(fprop);
464 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
465 prop != ZPOOL_PROP_CACHEFILE) {
466 (void) fprintf(stderr, gettext("property '%s' "
467 "specified multiple times\n"), propname);
471 if (nvlist_add_string(proplist, normnm, propval) != 0) {
472 (void) fprintf(stderr, gettext("internal "
473 "error: out of memory\n"));
481 * zpool add [-fn] <pool> <vdev> ...
483 * -f Force addition of devices, even if they appear in use
484 * -n Do not add the devices, but display the resulting layout if
485 * they were to be added.
487 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
488 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
492 zpool_do_add(int argc, char **argv)
494 boolean_t force = B_FALSE;
495 boolean_t dryrun = B_FALSE;
504 while ((c = getopt(argc, argv, "fn")) != -1) {
513 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
522 /* get pool name and check number of arguments */
524 (void) fprintf(stderr, gettext("missing pool name argument\n"));
528 (void) fprintf(stderr, gettext("missing vdev specification\n"));
537 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
540 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
541 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
547 /* pass off to get_vdev_spec for processing */
548 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
550 if (nvroot == NULL) {
556 nvlist_t *poolnvroot;
558 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
561 (void) printf(gettext("would update '%s' to the following "
562 "configuration:\n"), zpool_get_name(zhp));
564 /* print original main pool and new tree */
565 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
566 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
568 /* Do the same for the logs */
569 if (num_logs(poolnvroot) > 0) {
570 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
571 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
572 } else if (num_logs(nvroot) > 0) {
573 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
578 ret = (zpool_add(zhp, nvroot) != 0);
588 * zpool remove <pool> <vdev> ...
590 * Removes the given vdev from the pool. Currently, this supports removing
591 * spares, cache, and log devices from the pool.
594 zpool_do_remove(int argc, char **argv)
603 /* get pool name and check number of arguments */
605 (void) fprintf(stderr, gettext("missing pool name argument\n"));
609 (void) fprintf(stderr, gettext("missing device\n"));
615 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
618 for (i = 1; i < argc; i++) {
619 if (zpool_vdev_remove(zhp, argv[i]) != 0)
627 * zpool labelclear <vdev>
629 * Verifies that the vdev is not active and zeros out the label information
633 zpool_do_labelclear(int argc, char **argv)
636 int c, fd = -1, ret = 0;
638 boolean_t inuse = B_FALSE;
639 boolean_t force = B_FALSE;
642 while ((c = getopt(argc, argv, "f")) != -1) {
648 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
659 (void) fprintf(stderr, gettext("missing vdev device name\n"));
664 if ((fd = open(vdev, O_RDWR)) < 0) {
665 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
670 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
674 (void) fprintf(stderr,
675 gettext("Unable to determine pool state for %s\n"
676 "Use -f to force the clearing any label data\n"), vdev);
684 case POOL_STATE_ACTIVE:
685 case POOL_STATE_SPARE:
686 case POOL_STATE_L2CACHE:
687 (void) fprintf(stderr,
688 gettext("labelclear operation failed.\n"
689 "\tVdev %s is a member (%s), of pool \"%s\".\n"
690 "\tTo remove label information from this device, export or destroy\n"
691 "\tthe pool, or remove %s from the configuration of this pool\n"
692 "\tand retry the labelclear operation\n"),
693 vdev, zpool_pool_state_to_name(state), name, vdev);
697 case POOL_STATE_EXPORTED:
701 (void) fprintf(stderr,
702 gettext("labelclear operation failed.\n"
703 "\tVdev %s is a member of the exported pool \"%s\".\n"
704 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
710 case POOL_STATE_POTENTIALLY_ACTIVE:
714 (void) fprintf(stderr,
715 gettext("labelclear operation failed.\n"
716 "\tVdev %s is a member of the pool \"%s\".\n"
717 "\tThis pool is unknown to this system, but may be active on\n"
718 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
719 "\tremoval of label information.\n"),
724 case POOL_STATE_DESTROYED:
725 /* inuse should never be set for a destoryed pool... */
731 if (zpool_clear_label(fd) != 0) {
732 (void) fprintf(stderr,
733 gettext("Label clear failed on vdev %s\n"), vdev);
746 * zpool create [-fnd] [-o property=value] ...
747 * [-O file-system-property=value] ...
748 * [-R root] [-m mountpoint] <pool> <dev> ...
750 * -f Force creation, even if devices appear in use
751 * -n Do not create the pool, but display the resulting layout if it
752 * were to be created.
753 * -R Create a pool under an alternate root
754 * -m Set default mountpoint for the root dataset. By default it's
756 * -o Set property=value.
757 * -d Don't automatically enable all supported pool features
758 * (individual features can be enabled with -o).
759 * -O Set fsproperty=value in the pool's root file system
761 * Creates the named pool according to the given vdev specification. The
762 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
763 * we get the nvlist back from get_vdev_spec(), we either print out the contents
764 * (if '-n' was specified), or pass it to libzfs to do the creation.
767 zpool_do_create(int argc, char **argv)
769 boolean_t force = B_FALSE;
770 boolean_t dryrun = B_FALSE;
771 boolean_t enable_all_pool_feat = B_TRUE;
773 nvlist_t *nvroot = NULL;
776 char *altroot = NULL;
777 char *mountpoint = NULL;
778 nvlist_t *fsprops = NULL;
779 nvlist_t *props = NULL;
783 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
792 enable_all_pool_feat = B_FALSE;
796 if (add_prop_list(zpool_prop_to_name(
797 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
799 if (nvlist_lookup_string(props,
800 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
803 if (add_prop_list(zpool_prop_to_name(
804 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
808 /* Equivalent to -O mountpoint=optarg */
812 if ((propval = strchr(optarg, '=')) == NULL) {
813 (void) fprintf(stderr, gettext("missing "
814 "'=' for -o option\n"));
820 if (add_prop_list(optarg, propval, &props, B_TRUE))
824 * If the user is creating a pool that doesn't support
825 * feature flags, don't enable any features.
827 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
831 ver = strtoull(propval, &end, 10);
833 ver < SPA_VERSION_FEATURES) {
834 enable_all_pool_feat = B_FALSE;
839 if ((propval = strchr(optarg, '=')) == NULL) {
840 (void) fprintf(stderr, gettext("missing "
841 "'=' for -O option\n"));
848 * Mountpoints are checked and then added later.
849 * Uniquely among properties, they can be specified
850 * more than once, to avoid conflict with -m.
852 if (0 == strcmp(optarg,
853 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
854 mountpoint = propval;
855 } else if (add_prop_list(optarg, propval, &fsprops,
861 (void) fprintf(stderr, gettext("missing argument for "
862 "'%c' option\n"), optopt);
865 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
874 /* get pool name and check number of arguments */
876 (void) fprintf(stderr, gettext("missing pool name argument\n"));
880 (void) fprintf(stderr, gettext("missing vdev specification\n"));
887 * As a special case, check for use of '/' in the name, and direct the
888 * user to use 'zfs create' instead.
890 if (strchr(poolname, '/') != NULL) {
891 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
892 "character '/' in pool name\n"), poolname);
893 (void) fprintf(stderr, gettext("use 'zfs create' to "
894 "create a dataset\n"));
898 /* pass off to get_vdev_spec for bulk processing */
899 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
904 /* make_root_vdev() allows 0 toplevel children if there are spares */
905 if (!zfs_allocatable_devs(nvroot)) {
906 (void) fprintf(stderr, gettext("invalid vdev "
907 "specification: at least one toplevel vdev must be "
912 if (altroot != NULL && altroot[0] != '/') {
913 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
914 "must be an absolute path\n"), altroot);
919 * Check the validity of the mountpoint and direct the user to use the
920 * '-m' mountpoint option if it looks like its in use.
921 * Ignore the checks if the '-f' option is given.
923 if (!force && (mountpoint == NULL ||
924 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
925 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
926 char buf[MAXPATHLEN];
929 if (mountpoint && mountpoint[0] != '/') {
930 (void) fprintf(stderr, gettext("invalid mountpoint "
931 "'%s': must be an absolute path, 'legacy', or "
932 "'none'\n"), mountpoint);
936 if (mountpoint == NULL) {
938 (void) snprintf(buf, sizeof (buf), "%s/%s",
941 (void) snprintf(buf, sizeof (buf), "/%s",
945 (void) snprintf(buf, sizeof (buf), "%s%s",
946 altroot, mountpoint);
948 (void) snprintf(buf, sizeof (buf), "%s",
952 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
953 (void) fprintf(stderr, gettext("mountpoint '%s' : "
954 "%s\n"), buf, strerror(errno));
955 (void) fprintf(stderr, gettext("use '-m' "
956 "option to provide a different default\n"));
961 while (count < 3 && readdir(dirp) != NULL)
963 (void) closedir(dirp);
966 (void) fprintf(stderr, gettext("mountpoint "
967 "'%s' exists and is not empty\n"), buf);
968 (void) fprintf(stderr, gettext("use '-m' "
969 "option to provide a "
970 "different default\n"));
977 * Now that the mountpoint's validity has been checked, ensure that
978 * the property is set appropriately prior to creating the pool.
980 if (mountpoint != NULL) {
981 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
982 mountpoint, &fsprops, B_FALSE);
990 * For a dry run invocation, print out a basic message and run
991 * through all the vdevs in the list and print out in an
992 * appropriate hierarchy.
994 (void) printf(gettext("would create '%s' with the "
995 "following layout:\n\n"), poolname);
997 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
998 if (num_logs(nvroot) > 0)
999 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1004 * Hand off to libzfs.
1006 if (enable_all_pool_feat) {
1008 for (i = 0; i < SPA_FEATURES; i++) {
1009 char propname[MAXPATHLEN];
1010 zfeature_info_t *feat = &spa_feature_table[i];
1012 (void) snprintf(propname, sizeof (propname),
1013 "feature@%s", feat->fi_uname);
1016 * Skip feature if user specified it manually
1017 * on the command line.
1019 if (nvlist_exists(props, propname))
1022 ret = add_prop_list(propname,
1023 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1030 if (zpool_create(g_zfs, poolname,
1031 nvroot, props, fsprops) == 0) {
1032 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1033 ZFS_TYPE_FILESYSTEM);
1035 if (zfs_mount(pool, NULL, 0) == 0)
1036 ret = zfs_shareall(pool);
1039 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1040 (void) fprintf(stderr, gettext("pool name may have "
1046 nvlist_free(nvroot);
1047 nvlist_free(fsprops);
1051 nvlist_free(fsprops);
1058 * zpool destroy <pool>
1060 * -f Forcefully unmount any datasets
1062 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1065 zpool_do_destroy(int argc, char **argv)
1067 boolean_t force = B_FALSE;
1070 zpool_handle_t *zhp;
1074 while ((c = getopt(argc, argv, "f")) != -1) {
1080 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1089 /* check arguments */
1091 (void) fprintf(stderr, gettext("missing pool argument\n"));
1095 (void) fprintf(stderr, gettext("too many arguments\n"));
1101 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1103 * As a special case, check for use of '/' in the name, and
1104 * direct the user to use 'zfs destroy' instead.
1106 if (strchr(pool, '/') != NULL)
1107 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1108 "destroy a dataset\n"));
1112 if (zpool_disable_datasets(zhp, force) != 0) {
1113 (void) fprintf(stderr, gettext("could not destroy '%s': "
1114 "could not unmount datasets\n"), zpool_get_name(zhp));
1118 /* The history must be logged as part of the export */
1119 log_history = B_FALSE;
1121 ret = (zpool_destroy(zhp, history_str) != 0);
1129 * zpool export [-f] <pool> ...
1131 * -f Forcefully unmount datasets
1133 * Export the given pools. By default, the command will attempt to cleanly
1134 * unmount any active datasets within the pool. If the '-f' flag is specified,
1135 * then the datasets will be forcefully unmounted.
1138 zpool_do_export(int argc, char **argv)
1140 boolean_t force = B_FALSE;
1141 boolean_t hardforce = B_FALSE;
1143 zpool_handle_t *zhp;
1148 while ((c = getopt(argc, argv, "fF")) != -1) {
1157 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1166 /* check arguments */
1168 (void) fprintf(stderr, gettext("missing pool argument\n"));
1173 for (i = 0; i < argc; i++) {
1174 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1179 if (zpool_disable_datasets(zhp, force) != 0) {
1185 /* The history must be logged as part of the export */
1186 log_history = B_FALSE;
1189 if (zpool_export_force(zhp, history_str) != 0)
1191 } else if (zpool_export(zhp, force, history_str) != 0) {
1202 * Given a vdev configuration, determine the maximum width needed for the device
1206 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1208 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1213 if (strlen(name) + depth > max)
1214 max = strlen(name) + depth;
1218 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1219 &child, &children) == 0) {
1220 for (c = 0; c < children; c++)
1221 if ((ret = max_width(zhp, child[c], depth + 2,
1226 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1227 &child, &children) == 0) {
1228 for (c = 0; c < children; c++)
1229 if ((ret = max_width(zhp, child[c], depth + 2,
1234 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1235 &child, &children) == 0) {
1236 for (c = 0; c < children; c++)
1237 if ((ret = max_width(zhp, child[c], depth + 2,
1246 typedef struct spare_cbdata {
1248 zpool_handle_t *cb_zhp;
1252 find_vdev(nvlist_t *nv, uint64_t search)
1258 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1262 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1263 &child, &children) == 0) {
1264 for (c = 0; c < children; c++)
1265 if (find_vdev(child[c], search))
1273 find_spare(zpool_handle_t *zhp, void *data)
1275 spare_cbdata_t *cbp = data;
1276 nvlist_t *config, *nvroot;
1278 config = zpool_get_config(zhp, NULL);
1279 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1282 if (find_vdev(nvroot, cbp->cb_guid)) {
1292 * Print out configuration state as requested by status_callback.
1295 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1296 int namewidth, int depth, boolean_t isspare)
1299 uint_t c, vsc, children;
1300 pool_scan_stat_t *ps = NULL;
1302 char rbuf[6], wbuf[6], cbuf[6];
1304 uint64_t notpresent;
1309 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1310 &child, &children) != 0)
1313 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1314 (uint64_t **)&vs, &vsc) == 0);
1316 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1319 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1322 if (vs->vs_aux == VDEV_AUX_SPARED)
1324 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1328 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1332 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1333 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1334 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1335 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1338 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1339 ¬present) == 0 ||
1340 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1342 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1343 (void) printf(" was %s", path);
1344 } else if (vs->vs_aux != 0) {
1347 switch (vs->vs_aux) {
1348 case VDEV_AUX_OPEN_FAILED:
1349 (void) printf(gettext("cannot open"));
1352 case VDEV_AUX_BAD_GUID_SUM:
1353 (void) printf(gettext("missing device"));
1356 case VDEV_AUX_NO_REPLICAS:
1357 (void) printf(gettext("insufficient replicas"));
1360 case VDEV_AUX_VERSION_NEWER:
1361 (void) printf(gettext("newer version"));
1364 case VDEV_AUX_UNSUP_FEAT:
1365 (void) printf(gettext("unsupported feature(s)"));
1368 case VDEV_AUX_ASHIFT_TOO_BIG:
1369 (void) printf(gettext("unsupported minimum blocksize"));
1372 case VDEV_AUX_SPARED:
1373 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1375 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1376 if (strcmp(zpool_get_name(cb.cb_zhp),
1377 zpool_get_name(zhp)) == 0)
1378 (void) printf(gettext("currently in "
1381 (void) printf(gettext("in use by "
1383 zpool_get_name(cb.cb_zhp));
1384 zpool_close(cb.cb_zhp);
1386 (void) printf(gettext("currently in use"));
1390 case VDEV_AUX_ERR_EXCEEDED:
1391 (void) printf(gettext("too many errors"));
1394 case VDEV_AUX_IO_FAILURE:
1395 (void) printf(gettext("experienced I/O failures"));
1398 case VDEV_AUX_BAD_LOG:
1399 (void) printf(gettext("bad intent log"));
1402 case VDEV_AUX_EXTERNAL:
1403 (void) printf(gettext("external device fault"));
1406 case VDEV_AUX_SPLIT_POOL:
1407 (void) printf(gettext("split into new pool"));
1411 (void) printf(gettext("corrupted data"));
1414 } else if (children == 0 && !isspare &&
1415 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1416 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1418 gettext(" block size: %dB configured, %dB native"),
1419 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1422 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1423 (uint64_t **)&ps, &c);
1425 if (ps && ps->pss_state == DSS_SCANNING &&
1426 vs->vs_scan_processed != 0 && children == 0) {
1427 (void) printf(gettext(" (%s)"),
1428 (ps->pss_func == POOL_SCAN_RESILVER) ?
1429 "resilvering" : "repairing");
1432 (void) printf("\n");
1434 for (c = 0; c < children; c++) {
1435 uint64_t islog = B_FALSE, ishole = B_FALSE;
1437 /* Don't print logs or holes here */
1438 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1440 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1442 if (islog || ishole)
1444 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1445 print_status_config(zhp, vname, child[c],
1446 namewidth, depth + 2, isspare);
1453 * Print the configuration of an exported pool. Iterate over all vdevs in the
1454 * pool, printing out the name and status for each one.
1457 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1464 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1465 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1466 strcmp(type, VDEV_TYPE_HOLE) == 0)
1469 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1470 (uint64_t **)&vs, &c) == 0);
1472 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1473 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1475 if (vs->vs_aux != 0) {
1478 switch (vs->vs_aux) {
1479 case VDEV_AUX_OPEN_FAILED:
1480 (void) printf(gettext("cannot open"));
1483 case VDEV_AUX_BAD_GUID_SUM:
1484 (void) printf(gettext("missing device"));
1487 case VDEV_AUX_NO_REPLICAS:
1488 (void) printf(gettext("insufficient replicas"));
1491 case VDEV_AUX_VERSION_NEWER:
1492 (void) printf(gettext("newer version"));
1495 case VDEV_AUX_UNSUP_FEAT:
1496 (void) printf(gettext("unsupported feature(s)"));
1499 case VDEV_AUX_ERR_EXCEEDED:
1500 (void) printf(gettext("too many errors"));
1504 (void) printf(gettext("corrupted data"));
1508 (void) printf("\n");
1510 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1511 &child, &children) != 0)
1514 for (c = 0; c < children; c++) {
1515 uint64_t is_log = B_FALSE;
1517 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1522 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1523 print_import_config(vname, child[c], namewidth, depth + 2);
1527 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1528 &child, &children) == 0) {
1529 (void) printf(gettext("\tcache\n"));
1530 for (c = 0; c < children; c++) {
1531 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1532 (void) printf("\t %s\n", vname);
1537 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1538 &child, &children) == 0) {
1539 (void) printf(gettext("\tspares\n"));
1540 for (c = 0; c < children; c++) {
1541 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1542 (void) printf("\t %s\n", vname);
1550 * Logs are recorded as top level vdevs in the main pool child array
1551 * but with "is_log" set to 1. We use either print_status_config() or
1552 * print_import_config() to print the top level logs then any log
1553 * children (eg mirrored slogs) are printed recursively - which
1554 * works because only the top level vdev is marked "is_log"
1557 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1562 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1566 (void) printf(gettext("\tlogs\n"));
1568 for (c = 0; c < children; c++) {
1569 uint64_t is_log = B_FALSE;
1572 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1576 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1578 print_status_config(zhp, name, child[c], namewidth,
1581 print_import_config(name, child[c], namewidth, 2);
1587 * Display the status for the given pool.
1590 show_import(nvlist_t *config)
1592 uint64_t pool_state;
1604 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1606 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1608 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1610 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1613 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1614 (uint64_t **)&vs, &vsc) == 0);
1615 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1617 reason = zpool_import_status(config, &msgid);
1619 (void) printf(gettext(" pool: %s\n"), name);
1620 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1621 (void) printf(gettext(" state: %s"), health);
1622 if (pool_state == POOL_STATE_DESTROYED)
1623 (void) printf(gettext(" (DESTROYED)"));
1624 (void) printf("\n");
1627 case ZPOOL_STATUS_MISSING_DEV_R:
1628 case ZPOOL_STATUS_MISSING_DEV_NR:
1629 case ZPOOL_STATUS_BAD_GUID_SUM:
1630 (void) printf(gettext(" status: One or more devices are "
1631 "missing from the system.\n"));
1634 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1635 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1636 (void) printf(gettext(" status: One or more devices contains "
1637 "corrupted data.\n"));
1640 case ZPOOL_STATUS_CORRUPT_DATA:
1642 gettext(" status: The pool data is corrupted.\n"));
1645 case ZPOOL_STATUS_OFFLINE_DEV:
1646 (void) printf(gettext(" status: One or more devices "
1647 "are offlined.\n"));
1650 case ZPOOL_STATUS_CORRUPT_POOL:
1651 (void) printf(gettext(" status: The pool metadata is "
1655 case ZPOOL_STATUS_VERSION_OLDER:
1656 (void) printf(gettext(" status: The pool is formatted using a "
1657 "legacy on-disk version.\n"));
1660 case ZPOOL_STATUS_VERSION_NEWER:
1661 (void) printf(gettext(" status: The pool is formatted using an "
1662 "incompatible version.\n"));
1665 case ZPOOL_STATUS_FEAT_DISABLED:
1666 (void) printf(gettext(" status: Some supported features are "
1667 "not enabled on the pool.\n"));
1670 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1671 (void) printf(gettext("status: The pool uses the following "
1672 "feature(s) not supported on this sytem:\n"));
1673 zpool_print_unsup_feat(config);
1676 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1677 (void) printf(gettext("status: The pool can only be accessed "
1678 "in read-only mode on this system. It\n\tcannot be "
1679 "accessed in read-write mode because it uses the "
1680 "following\n\tfeature(s) not supported on this system:\n"));
1681 zpool_print_unsup_feat(config);
1684 case ZPOOL_STATUS_HOSTID_MISMATCH:
1685 (void) printf(gettext(" status: The pool was last accessed by "
1686 "another system.\n"));
1689 case ZPOOL_STATUS_FAULTED_DEV_R:
1690 case ZPOOL_STATUS_FAULTED_DEV_NR:
1691 (void) printf(gettext(" status: One or more devices are "
1695 case ZPOOL_STATUS_BAD_LOG:
1696 (void) printf(gettext(" status: An intent log record cannot be "
1700 case ZPOOL_STATUS_RESILVERING:
1701 (void) printf(gettext(" status: One or more devices were being "
1705 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
1706 (void) printf(gettext("status: One or more devices were "
1707 "configured to use a non-native block size.\n"
1708 "\tExpect reduced performance.\n"));
1713 * No other status can be seen when importing pools.
1715 assert(reason == ZPOOL_STATUS_OK);
1719 * Print out an action according to the overall state of the pool.
1721 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1722 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1723 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1724 (void) printf(gettext(" action: The pool can be "
1725 "imported using its name or numeric identifier, "
1726 "though\n\tsome features will not be available "
1727 "without an explicit 'zpool upgrade'.\n"));
1728 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1729 (void) printf(gettext(" action: The pool can be "
1730 "imported using its name or numeric "
1731 "identifier and\n\tthe '-f' flag.\n"));
1733 (void) printf(gettext(" action: The pool can be "
1734 "imported using its name or numeric "
1737 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1738 (void) printf(gettext(" action: The pool can be imported "
1739 "despite missing or damaged devices. The\n\tfault "
1740 "tolerance of the pool may be compromised if imported.\n"));
1743 case ZPOOL_STATUS_VERSION_NEWER:
1744 (void) printf(gettext(" action: The pool cannot be "
1745 "imported. Access the pool on a system running "
1746 "newer\n\tsoftware, or recreate the pool from "
1749 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1750 (void) printf(gettext("action: The pool cannot be "
1751 "imported. Access the pool on a system that "
1752 "supports\n\tthe required feature(s), or recreate "
1753 "the pool from backup.\n"));
1755 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1756 (void) printf(gettext("action: The pool cannot be "
1757 "imported in read-write mode. Import the pool "
1759 "\t\"-o readonly=on\", access the pool on a system "
1760 "that supports the\n\trequired feature(s), or "
1761 "recreate the pool from backup.\n"));
1763 case ZPOOL_STATUS_MISSING_DEV_R:
1764 case ZPOOL_STATUS_MISSING_DEV_NR:
1765 case ZPOOL_STATUS_BAD_GUID_SUM:
1766 (void) printf(gettext(" action: The pool cannot be "
1767 "imported. Attach the missing\n\tdevices and try "
1771 (void) printf(gettext(" action: The pool cannot be "
1772 "imported due to damaged devices or data.\n"));
1776 /* Print the comment attached to the pool. */
1777 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1778 (void) printf(gettext("comment: %s\n"), comment);
1781 * If the state is "closed" or "can't open", and the aux state
1782 * is "corrupt data":
1784 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1785 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1786 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1787 if (pool_state == POOL_STATE_DESTROYED)
1788 (void) printf(gettext("\tThe pool was destroyed, "
1789 "but can be imported using the '-Df' flags.\n"));
1790 else if (pool_state != POOL_STATE_EXPORTED)
1791 (void) printf(gettext("\tThe pool may be active on "
1792 "another system, but can be imported using\n\t"
1793 "the '-f' flag.\n"));
1797 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1800 (void) printf(gettext(" config:\n\n"));
1802 namewidth = max_width(NULL, nvroot, 0, 0);
1806 print_import_config(name, nvroot, namewidth, 0);
1807 if (num_logs(nvroot) > 0)
1808 print_logs(NULL, nvroot, namewidth, B_FALSE);
1810 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1811 (void) printf(gettext("\n\tAdditional devices are known to "
1812 "be part of this pool, though their\n\texact "
1813 "configuration cannot be determined.\n"));
1818 * Perform the import for the given configuration. This passes the heavy
1819 * lifting off to zpool_import_props(), and then mounts the datasets contained
1823 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1824 nvlist_t *props, int flags)
1826 zpool_handle_t *zhp;
1831 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1834 verify(nvlist_lookup_uint64(config,
1835 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1836 verify(nvlist_lookup_uint64(config,
1837 ZPOOL_CONFIG_VERSION, &version) == 0);
1838 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1839 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1840 "is formatted using an unsupported ZFS version\n"), name);
1842 } else if (state != POOL_STATE_EXPORTED &&
1843 !(flags & ZFS_IMPORT_ANY_HOST)) {
1846 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1848 if ((unsigned long)hostid != gethostid()) {
1853 verify(nvlist_lookup_string(config,
1854 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1855 verify(nvlist_lookup_uint64(config,
1856 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1858 (void) fprintf(stderr, gettext("cannot import "
1859 "'%s': pool may be in use from other "
1860 "system, it was last accessed by %s "
1861 "(hostid: 0x%lx) on %s"), name, hostname,
1862 (unsigned long)hostid,
1863 asctime(localtime(&t)));
1864 (void) fprintf(stderr, gettext("use '-f' to "
1865 "import anyway\n"));
1869 (void) fprintf(stderr, gettext("cannot import '%s': "
1870 "pool may be in use from other system\n"), name);
1871 (void) fprintf(stderr, gettext("use '-f' to import "
1877 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1880 if (newname != NULL)
1881 name = (char *)newname;
1883 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1886 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1887 !(flags & ZFS_IMPORT_ONLY) &&
1888 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1898 * zpool import [-d dir] [-D]
1899 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1900 * [-d dir | -c cachefile] [-f] -a
1901 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1902 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1904 * -c Read pool information from a cachefile instead of searching
1907 * -d Scan in a specific directory, other than /dev/dsk. More than
1908 * one directory can be specified using multiple '-d' options.
1910 * -D Scan for previously destroyed pools or import all or only
1911 * specified destroyed pools.
1913 * -R Temporarily import the pool, with all mountpoints relative to
1914 * the given root. The pool will remain exported when the machine
1917 * -V Import even in the presence of faulted vdevs. This is an
1918 * intentionally undocumented option for testing purposes, and
1919 * treats the pool configuration as complete, leaving any bad
1920 * vdevs in the FAULTED state. In other words, it does verbatim
1923 * -f Force import, even if it appears that the pool is active.
1925 * -F Attempt rewind if necessary.
1927 * -n See if rewind would work, but don't actually rewind.
1929 * -N Import the pool but don't mount datasets.
1931 * -T Specify a starting txg to use for import. This option is
1932 * intentionally undocumented option for testing purposes.
1934 * -a Import all pools found.
1936 * -o Set property=value and/or temporary mount options (without '=').
1938 * The import command scans for pools to import, and import pools based on pool
1939 * name and GUID. The pool can also be renamed as part of the import process.
1942 zpool_do_import(int argc, char **argv)
1944 char **searchdirs = NULL;
1948 nvlist_t *pools = NULL;
1949 boolean_t do_all = B_FALSE;
1950 boolean_t do_destroyed = B_FALSE;
1951 char *mntopts = NULL;
1954 uint64_t searchguid = 0;
1955 char *searchname = NULL;
1957 nvlist_t *found_config;
1958 nvlist_t *policy = NULL;
1959 nvlist_t *props = NULL;
1961 int flags = ZFS_IMPORT_NORMAL;
1962 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1963 boolean_t dryrun = B_FALSE;
1964 boolean_t do_rewind = B_FALSE;
1965 boolean_t xtreme_rewind = B_FALSE;
1966 uint64_t pool_state, txg = -1ULL;
1967 char *cachefile = NULL;
1968 importargs_t idata = { 0 };
1972 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:T:VX")) != -1) {
1981 if (searchdirs == NULL) {
1982 searchdirs = safe_malloc(sizeof (char *));
1984 char **tmp = safe_malloc((nsearch + 1) *
1986 bcopy(searchdirs, tmp, nsearch *
1991 searchdirs[nsearch++] = optarg;
1994 do_destroyed = B_TRUE;
1997 flags |= ZFS_IMPORT_ANY_HOST;
2003 flags |= ZFS_IMPORT_MISSING_LOG;
2009 flags |= ZFS_IMPORT_ONLY;
2012 if ((propval = strchr(optarg, '=')) != NULL) {
2015 if (add_prop_list(optarg, propval,
2023 if (add_prop_list(zpool_prop_to_name(
2024 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2026 if (nvlist_lookup_string(props,
2027 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2030 if (add_prop_list(zpool_prop_to_name(
2031 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2036 txg = strtoull(optarg, &endptr, 0);
2037 if (errno != 0 || *endptr != '\0') {
2038 (void) fprintf(stderr,
2039 gettext("invalid txg value\n"));
2042 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2045 flags |= ZFS_IMPORT_VERBATIM;
2048 xtreme_rewind = B_TRUE;
2051 (void) fprintf(stderr, gettext("missing argument for "
2052 "'%c' option\n"), optopt);
2056 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2065 if (cachefile && nsearch != 0) {
2066 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2070 if ((dryrun || xtreme_rewind) && !do_rewind) {
2071 (void) fprintf(stderr,
2072 gettext("-n or -X only meaningful with -F\n"));
2076 rewind_policy = ZPOOL_TRY_REWIND;
2078 rewind_policy = ZPOOL_DO_REWIND;
2080 rewind_policy |= ZPOOL_EXTREME_REWIND;
2082 /* In the future, we can capture further policy and include it here */
2083 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2084 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2085 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2088 if (searchdirs == NULL) {
2089 searchdirs = safe_malloc(sizeof (char *));
2090 searchdirs[0] = "/dev";
2094 /* check argument count */
2097 (void) fprintf(stderr, gettext("too many arguments\n"));
2102 (void) fprintf(stderr, gettext("too many arguments\n"));
2107 * Check for the SYS_CONFIG privilege. We do this explicitly
2108 * here because otherwise any attempt to discover pools will
2111 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2112 (void) fprintf(stderr, gettext("cannot "
2113 "discover pools: permission denied\n"));
2115 nvlist_free(policy);
2121 * Depending on the arguments given, we do one of the following:
2123 * <none> Iterate through all pools and display information about
2126 * -a Iterate through all pools and try to import each one.
2128 * <id> Find the pool that corresponds to the given GUID/pool
2129 * name and import that one.
2131 * -D Above options applies only to destroyed pools.
2137 searchguid = strtoull(argv[0], &endptr, 10);
2138 if (errno != 0 || *endptr != '\0') {
2139 searchname = argv[0];
2142 found_config = NULL;
2145 * User specified a name or guid. Ensure it's unique.
2147 idata.unique = B_TRUE;
2151 idata.path = searchdirs;
2152 idata.paths = nsearch;
2153 idata.poolname = searchname;
2154 idata.guid = searchguid;
2155 idata.cachefile = cachefile;
2157 pools = zpool_search_import(g_zfs, &idata);
2159 if (pools != NULL && idata.exists &&
2160 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2161 (void) fprintf(stderr, gettext("cannot import '%s': "
2162 "a pool with that name already exists\n"),
2164 (void) fprintf(stderr, gettext("use the form '%s "
2165 "<pool | id> <newpool>' to give it a new name\n"),
2168 } else if (pools == NULL && idata.exists) {
2169 (void) fprintf(stderr, gettext("cannot import '%s': "
2170 "a pool with that name is already created/imported,\n"),
2172 (void) fprintf(stderr, gettext("and no additional pools "
2173 "with that name were found\n"));
2175 } else if (pools == NULL) {
2177 (void) fprintf(stderr, gettext("cannot import '%s': "
2178 "no such pool available\n"), argv[0]);
2185 nvlist_free(policy);
2190 * At this point we have a list of import candidate configs. Even if
2191 * we were searching by pool name or guid, we still need to
2192 * post-process the list to deal with pool state and possible
2198 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2200 verify(nvpair_value_nvlist(elem, &config) == 0);
2202 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2204 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2206 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2209 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2216 (void) printf("\n");
2219 err |= do_import(config, NULL, mntopts,
2222 show_import(config);
2224 } else if (searchname != NULL) {
2228 * We are searching for a pool based on name.
2230 verify(nvlist_lookup_string(config,
2231 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2233 if (strcmp(name, searchname) == 0) {
2234 if (found_config != NULL) {
2235 (void) fprintf(stderr, gettext(
2236 "cannot import '%s': more than "
2237 "one matching pool\n"), searchname);
2238 (void) fprintf(stderr, gettext(
2239 "import by numeric ID instead\n"));
2242 found_config = config;
2248 * Search for a pool by guid.
2250 verify(nvlist_lookup_uint64(config,
2251 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2253 if (guid == searchguid)
2254 found_config = config;
2259 * If we were searching for a specific pool, verify that we found a
2260 * pool, and then do the import.
2262 if (argc != 0 && err == 0) {
2263 if (found_config == NULL) {
2264 (void) fprintf(stderr, gettext("cannot import '%s': "
2265 "no such pool available\n"), argv[0]);
2268 err |= do_import(found_config, argc == 1 ? NULL :
2269 argv[1], mntopts, props, flags);
2274 * If we were just looking for pools, report an error if none were
2277 if (argc == 0 && first)
2278 (void) fprintf(stderr,
2279 gettext("no pools available to import\n"));
2284 nvlist_free(policy);
2287 return (err ? 1 : 0);
2290 typedef struct iostat_cbdata {
2291 boolean_t cb_verbose;
2294 zpool_list_t *cb_list;
2298 print_iostat_separator(iostat_cbdata_t *cb)
2302 for (i = 0; i < cb->cb_namewidth; i++)
2304 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2308 print_iostat_header(iostat_cbdata_t *cb)
2310 (void) printf("%*s capacity operations bandwidth\n",
2311 cb->cb_namewidth, "");
2312 (void) printf("%-*s alloc free read write read write\n",
2313 cb->cb_namewidth, "pool");
2314 print_iostat_separator(cb);
2318 * Display a single statistic.
2321 print_one_stat(uint64_t value)
2325 zfs_nicenum(value, buf, sizeof (buf));
2326 (void) printf(" %5s", buf);
2330 * Print out all the statistics for the given vdev. This can either be the
2331 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2332 * is a verbose output, and we don't want to display the toplevel pool stats.
2335 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2336 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2338 nvlist_t **oldchild, **newchild;
2340 vdev_stat_t *oldvs, *newvs;
2341 vdev_stat_t zerovs = { 0 };
2346 if (oldnv != NULL) {
2347 verify(nvlist_lookup_uint64_array(oldnv,
2348 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2353 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2354 (uint64_t **)&newvs, &c) == 0);
2356 if (strlen(name) + depth > cb->cb_namewidth)
2357 (void) printf("%*s%s", depth, "", name);
2359 (void) printf("%*s%s%*s", depth, "", name,
2360 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2362 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2367 scale = (double)NANOSEC / tdelta;
2369 /* only toplevel vdevs have capacity stats */
2370 if (newvs->vs_space == 0) {
2371 (void) printf(" - -");
2373 print_one_stat(newvs->vs_alloc);
2374 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2377 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2378 oldvs->vs_ops[ZIO_TYPE_READ])));
2380 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2381 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2383 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2384 oldvs->vs_bytes[ZIO_TYPE_READ])));
2386 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2387 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2389 (void) printf("\n");
2391 if (!cb->cb_verbose)
2394 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2395 &newchild, &children) != 0)
2398 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2399 &oldchild, &c) != 0)
2402 for (c = 0; c < children; c++) {
2403 uint64_t ishole = B_FALSE, islog = B_FALSE;
2405 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2408 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2411 if (ishole || islog)
2414 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2415 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2416 newchild[c], cb, depth + 2);
2421 * Log device section
2424 if (num_logs(newnv) > 0) {
2425 (void) printf("%-*s - - - - - "
2426 "-\n", cb->cb_namewidth, "logs");
2428 for (c = 0; c < children; c++) {
2429 uint64_t islog = B_FALSE;
2430 (void) nvlist_lookup_uint64(newchild[c],
2431 ZPOOL_CONFIG_IS_LOG, &islog);
2434 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2436 print_vdev_stats(zhp, vname, oldnv ?
2437 oldchild[c] : NULL, newchild[c],
2446 * Include level 2 ARC devices in iostat output
2448 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2449 &newchild, &children) != 0)
2452 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2453 &oldchild, &c) != 0)
2457 (void) printf("%-*s - - - - - "
2458 "-\n", cb->cb_namewidth, "cache");
2459 for (c = 0; c < children; c++) {
2460 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2462 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2463 newchild[c], cb, depth + 2);
2470 refresh_iostat(zpool_handle_t *zhp, void *data)
2472 iostat_cbdata_t *cb = data;
2476 * If the pool has disappeared, remove it from the list and continue.
2478 if (zpool_refresh_stats(zhp, &missing) != 0)
2482 pool_list_remove(cb->cb_list, zhp);
2488 * Callback to print out the iostats for the given pool.
2491 print_iostat(zpool_handle_t *zhp, void *data)
2493 iostat_cbdata_t *cb = data;
2494 nvlist_t *oldconfig, *newconfig;
2495 nvlist_t *oldnvroot, *newnvroot;
2497 newconfig = zpool_get_config(zhp, &oldconfig);
2499 if (cb->cb_iteration == 1)
2502 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2505 if (oldconfig == NULL)
2508 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2512 * Print out the statistics for the pool.
2514 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2517 print_iostat_separator(cb);
2523 get_namewidth(zpool_handle_t *zhp, void *data)
2525 iostat_cbdata_t *cb = data;
2526 nvlist_t *config, *nvroot;
2528 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2529 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2531 if (!cb->cb_verbose)
2532 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2534 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2539 * The width must fall into the range [10,38]. The upper limit is the
2540 * maximum we can have and still fit in 80 columns.
2542 if (cb->cb_namewidth < 10)
2543 cb->cb_namewidth = 10;
2544 if (cb->cb_namewidth > 38)
2545 cb->cb_namewidth = 38;
2551 * Parse the input string, get the 'interval' and 'count' value if there is one.
2554 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2557 unsigned long interval = 0, count = 0;
2558 int argc = *argcp, errno;
2561 * Determine if the last argument is an integer or a pool name
2563 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2567 interval = strtoul(argv[argc - 1], &end, 10);
2569 if (*end == '\0' && errno == 0) {
2570 if (interval == 0) {
2571 (void) fprintf(stderr, gettext("interval "
2572 "cannot be zero\n"));
2576 * Ignore the last parameter
2581 * If this is not a valid number, just plow on. The
2582 * user will get a more informative error message later
2590 * If the last argument is also an integer, then we have both a count
2593 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2598 interval = strtoul(argv[argc - 1], &end, 10);
2600 if (*end == '\0' && errno == 0) {
2601 if (interval == 0) {
2602 (void) fprintf(stderr, gettext("interval "
2603 "cannot be zero\n"));
2608 * Ignore the last parameter
2622 get_timestamp_arg(char c)
2625 timestamp_fmt = UDATE;
2627 timestamp_fmt = DDATE;
2633 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2635 * -v Display statistics for individual vdevs
2636 * -T Display a timestamp in date(1) or Unix format
2638 * This command can be tricky because we want to be able to deal with pool
2639 * creation/destruction as well as vdev configuration changes. The bulk of this
2640 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2641 * on pool_list_update() to detect the addition of new pools. Configuration
2642 * changes are all handled within libzfs.
2645 zpool_do_iostat(int argc, char **argv)
2650 unsigned long interval = 0, count = 0;
2652 boolean_t verbose = B_FALSE;
2656 while ((c = getopt(argc, argv, "T:v")) != -1) {
2659 get_timestamp_arg(*optarg);
2665 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2674 get_interval_count(&argc, argv, &interval, &count);
2677 * Construct the list of all interesting pools.
2680 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2683 if (pool_list_count(list) == 0 && argc != 0) {
2684 pool_list_free(list);
2688 if (pool_list_count(list) == 0 && interval == 0) {
2689 pool_list_free(list);
2690 (void) fprintf(stderr, gettext("no pools available\n"));
2695 * Enter the main iostat loop.
2698 cb.cb_verbose = verbose;
2699 cb.cb_iteration = 0;
2700 cb.cb_namewidth = 0;
2703 pool_list_update(list);
2705 if ((npools = pool_list_count(list)) == 0)
2709 * Refresh all statistics. This is done as an explicit step
2710 * before calculating the maximum name width, so that any
2711 * configuration changes are properly accounted for.
2713 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2716 * Iterate over all pools to determine the maximum width
2717 * for the pool / device name column across all pools.
2719 cb.cb_namewidth = 0;
2720 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2722 if (timestamp_fmt != NODATE)
2723 print_timestamp(timestamp_fmt);
2726 * If it's the first time, or verbose mode, print the header.
2728 if (++cb.cb_iteration == 1 || verbose)
2729 print_iostat_header(&cb);
2731 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2734 * If there's more than one pool, and we're not in verbose mode
2735 * (which prints a separator for us), then print a separator.
2737 if (npools > 1 && !verbose)
2738 print_iostat_separator(&cb);
2741 (void) printf("\n");
2744 * Flush the output so that redirection to a file isn't buffered
2747 (void) fflush(stdout);
2752 if (count != 0 && --count == 0)
2755 (void) sleep(interval);
2758 pool_list_free(list);
2763 typedef struct list_cbdata {
2764 boolean_t cb_verbose;
2766 boolean_t cb_scripted;
2767 zprop_list_t *cb_proplist;
2768 boolean_t cb_literal;
2772 * Given a list of columns to display, output appropriate headers for each one.
2775 print_header(list_cbdata_t *cb)
2777 zprop_list_t *pl = cb->cb_proplist;
2778 char headerbuf[ZPOOL_MAXPROPLEN];
2780 boolean_t first = B_TRUE;
2781 boolean_t right_justify;
2784 for (; pl != NULL; pl = pl->pl_next) {
2785 width = pl->pl_width;
2786 if (first && cb->cb_verbose) {
2788 * Reset the width to accommodate the verbose listing
2791 width = cb->cb_namewidth;
2799 right_justify = B_FALSE;
2800 if (pl->pl_prop != ZPROP_INVAL) {
2801 header = zpool_prop_column_name(pl->pl_prop);
2802 right_justify = zpool_prop_align_right(pl->pl_prop);
2806 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2807 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2808 headerbuf[i] = '\0';
2812 if (pl->pl_next == NULL && !right_justify)
2813 (void) printf("%s", header);
2814 else if (right_justify)
2815 (void) printf("%*s", width, header);
2817 (void) printf("%-*s", width, header);
2821 (void) printf("\n");
2825 * Given a pool and a list of properties, print out all the properties according
2826 * to the described layout.
2829 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2831 zprop_list_t *pl = cb->cb_proplist;
2832 boolean_t first = B_TRUE;
2833 char property[ZPOOL_MAXPROPLEN];
2835 boolean_t right_justify;
2838 for (; pl != NULL; pl = pl->pl_next) {
2840 width = pl->pl_width;
2841 if (first && cb->cb_verbose) {
2843 * Reset the width to accommodate the verbose listing
2846 width = cb->cb_namewidth;
2850 if (cb->cb_scripted)
2851 (void) printf("\t");
2858 right_justify = B_FALSE;
2859 if (pl->pl_prop != ZPROP_INVAL) {
2860 if (zpool_get_prop(zhp, pl->pl_prop, property,
2861 sizeof (property), NULL, cb->cb_literal) != 0)
2866 right_justify = zpool_prop_align_right(pl->pl_prop);
2867 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2868 zpool_prop_unsupported(pl->pl_user_prop)) &&
2869 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2870 sizeof (property)) == 0) {
2878 * If this is being called in scripted mode, or if this is the
2879 * last column and it is left-justified, don't include a width
2882 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2883 (void) printf("%s", propstr);
2884 else if (right_justify)
2885 (void) printf("%*s", width, propstr);
2887 (void) printf("%-*s", width, propstr);
2890 (void) printf("\n");
2894 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
2899 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2902 case ZPOOL_PROP_EXPANDSZ:
2904 (void) strlcpy(propval, "-", sizeof (propval));
2906 zfs_nicenum(value, propval, sizeof (propval));
2908 case ZPOOL_PROP_FRAGMENTATION:
2909 if (value == ZFS_FRAG_INVALID) {
2910 (void) strlcpy(propval, "-", sizeof (propval));
2912 (void) snprintf(propval, sizeof (propval), "%llu%%",
2916 case ZPOOL_PROP_CAPACITY:
2917 (void) snprintf(propval, sizeof (propval), "%llu%%", value);
2920 zfs_nicenum(value, propval, sizeof (propval));
2924 (void) strlcpy(propval, "-", sizeof (propval));
2927 (void) printf("\t%s", propval);
2929 (void) printf(" %*s", width, propval);
2933 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2934 list_cbdata_t *cb, int depth)
2940 boolean_t scripted = cb->cb_scripted;
2942 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2943 (uint64_t **)&vs, &c) == 0);
2946 boolean_t toplevel = (vs->vs_space != 0);
2950 (void) printf("\t%s", name);
2951 else if (strlen(name) + depth > cb->cb_namewidth)
2952 (void) printf("%*s%s", depth, "", name);
2954 (void) printf("%*s%s%*s", depth, "", name,
2955 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2958 * Print the properties for the individual vdevs. Some
2959 * properties are only applicable to toplevel vdevs. The
2960 * 'toplevel' boolean value is passed to the print_one_column()
2961 * to indicate that the value is valid.
2963 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
2965 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
2967 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
2968 scripted, toplevel);
2969 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
2971 print_one_column(ZPOOL_PROP_FRAGMENTATION,
2972 vs->vs_fragmentation, scripted,
2973 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
2974 cap = (vs->vs_space == 0) ? 0 :
2975 (vs->vs_alloc * 100 / vs->vs_space);
2976 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
2977 (void) printf("\n");
2980 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2981 &child, &children) != 0)
2984 for (c = 0; c < children; c++) {
2985 uint64_t ishole = B_FALSE;
2987 if (nvlist_lookup_uint64(child[c],
2988 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2991 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2992 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2997 * Include level 2 ARC devices in iostat output
2999 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3000 &child, &children) != 0)
3004 (void) printf("%-*s - - - - - "
3005 "-\n", cb->cb_namewidth, "cache");
3006 for (c = 0; c < children; c++) {
3007 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3009 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3017 * Generic callback function to list a pool.
3020 list_callback(zpool_handle_t *zhp, void *data)
3022 list_cbdata_t *cbp = data;
3026 config = zpool_get_config(zhp, NULL);
3028 print_pool(zhp, cbp);
3029 if (!cbp->cb_verbose)
3032 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3034 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3040 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3042 * -H Scripted mode. Don't display headers, and separate properties
3044 * -o List of properties to display. Defaults to
3045 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3046 * "dedupratio,health,altroot"
3047 * -p Diplay values in parsable (exact) format.
3048 * -T Display a timestamp in date(1) or Unix format
3050 * List all pools in the system, whether or not they're healthy. Output space
3051 * statistics for each one, as well as health status summary.
3054 zpool_do_list(int argc, char **argv)
3058 list_cbdata_t cb = { 0 };
3059 static char default_props[] =
3060 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3061 "dedupratio,health,altroot";
3062 char *props = default_props;
3063 unsigned long interval = 0, count = 0;
3065 boolean_t first = B_TRUE;
3068 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3071 cb.cb_scripted = B_TRUE;
3077 cb.cb_literal = B_TRUE;
3080 get_timestamp_arg(*optarg);
3083 cb.cb_verbose = B_TRUE;
3086 (void) fprintf(stderr, gettext("missing argument for "
3087 "'%c' option\n"), optopt);
3091 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3100 get_interval_count(&argc, argv, &interval, &count);
3102 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3106 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3110 if (pool_list_count(list) == 0)
3113 cb.cb_namewidth = 0;
3114 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3116 if (timestamp_fmt != NODATE)
3117 print_timestamp(timestamp_fmt);
3119 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3123 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3128 if (count != 0 && --count == 0)
3131 pool_list_free(list);
3132 (void) sleep(interval);
3135 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3136 (void) printf(gettext("no pools available\n"));
3140 pool_list_free(list);
3141 zprop_free_list(cb.cb_proplist);
3146 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3153 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3154 &child, &children) != 0) {
3155 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3156 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3157 name += sizeof(_PATH_DEV) - 1;
3158 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3159 path += sizeof(_PATH_DEV) - 1;
3160 if (strcmp(name, path) == 0)
3165 for (c = 0; c < children; c++)
3166 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3173 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3175 boolean_t force = B_FALSE;
3178 char *poolname, *old_disk, *new_disk;
3179 zpool_handle_t *zhp;
3183 while ((c = getopt(argc, argv, "f")) != -1) {
3189 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3198 /* get pool name and check number of arguments */
3200 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3207 (void) fprintf(stderr,
3208 gettext("missing <device> specification\n"));
3216 (void) fprintf(stderr,
3217 gettext("missing <new_device> specification\n"));
3220 new_disk = old_disk;
3230 (void) fprintf(stderr, gettext("too many arguments\n"));
3234 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3237 if (zpool_get_config(zhp, NULL) == NULL) {
3238 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3244 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3246 if (nvroot == NULL) {
3251 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3253 nvlist_free(nvroot);
3260 * zpool replace [-f] <pool> <device> <new_device>
3262 * -f Force attach, even if <new_device> appears to be in use.
3264 * Replace <device> with <new_device>.
3268 zpool_do_replace(int argc, char **argv)
3270 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3274 * zpool attach [-f] <pool> <device> <new_device>
3276 * -f Force attach, even if <new_device> appears to be in use.
3278 * Attach <new_device> to the mirror containing <device>. If <device> is not
3279 * part of a mirror, then <device> will be transformed into a mirror of
3280 * <device> and <new_device>. In either case, <new_device> will begin life
3281 * with a DTL of [0, now], and will immediately begin to resilver itself.
3284 zpool_do_attach(int argc, char **argv)
3286 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3290 * zpool detach [-f] <pool> <device>
3292 * -f Force detach of <device>, even if DTLs argue against it
3293 * (not supported yet)
3295 * Detach a device from a mirror. The operation will be refused if <device>
3296 * is the last device in the mirror, or if the DTLs indicate that this device
3297 * has the only valid copy of some data.
3301 zpool_do_detach(int argc, char **argv)
3304 char *poolname, *path;
3305 zpool_handle_t *zhp;
3309 while ((c = getopt(argc, argv, "f")) != -1) {
3313 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3322 /* get pool name and check number of arguments */
3324 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3329 (void) fprintf(stderr,
3330 gettext("missing <device> specification\n"));
3337 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3340 ret = zpool_vdev_detach(zhp, path);
3348 * zpool split [-n] [-o prop=val] ...
3350 * [-R altroot] <pool> <newpool> [<device> ...]
3352 * -n Do not split the pool, but display the resulting layout if
3353 * it were to be split.
3354 * -o Set property=value, or set mount options.
3355 * -R Mount the split-off pool under an alternate root.
3357 * Splits the named pool and gives it the new pool name. Devices to be split
3358 * off may be listed, provided that no more than one device is specified
3359 * per top-level vdev mirror. The newly split pool is left in an exported
3360 * state unless -R is specified.
3362 * Restrictions: the top-level of the pool pool must only be made up of
3363 * mirrors; all devices in the pool must be healthy; no device may be
3364 * undergoing a resilvering operation.
3367 zpool_do_split(int argc, char **argv)
3369 char *srcpool, *newpool, *propval;
3370 char *mntopts = NULL;
3373 zpool_handle_t *zhp;
3374 nvlist_t *config, *props = NULL;
3376 flags.dryrun = B_FALSE;
3377 flags.import = B_FALSE;
3380 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3383 flags.import = B_TRUE;
3385 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3386 &props, B_TRUE) != 0) {
3393 flags.dryrun = B_TRUE;
3396 if ((propval = strchr(optarg, '=')) != NULL) {
3399 if (add_prop_list(optarg, propval,
3400 &props, B_TRUE) != 0) {
3410 (void) fprintf(stderr, gettext("missing argument for "
3411 "'%c' option\n"), optopt);
3415 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3422 if (!flags.import && mntopts != NULL) {
3423 (void) fprintf(stderr, gettext("setting mntopts is only "
3424 "valid when importing the pool\n"));
3432 (void) fprintf(stderr, gettext("Missing pool name\n"));
3436 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3446 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3449 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3450 if (config == NULL) {
3454 (void) printf(gettext("would create '%s' with the "
3455 "following layout:\n\n"), newpool);
3456 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3458 nvlist_free(config);
3463 if (ret != 0 || flags.dryrun || !flags.import)
3467 * The split was successful. Now we need to open the new
3468 * pool and import it.
3470 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3472 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3473 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3475 (void) fprintf(stderr, gettext("Split was successful, but "
3476 "the datasets could not all be mounted\n"));
3477 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3478 "different altroot\n"), "zpool import");
3488 * zpool online <pool> <device> ...
3491 zpool_do_online(int argc, char **argv)
3495 zpool_handle_t *zhp;
3497 vdev_state_t newstate;
3501 while ((c = getopt(argc, argv, "et")) != -1) {
3504 flags |= ZFS_ONLINE_EXPAND;
3508 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3517 /* get pool name and check number of arguments */
3519 (void) fprintf(stderr, gettext("missing pool name\n"));
3523 (void) fprintf(stderr, gettext("missing device name\n"));
3529 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3532 for (i = 1; i < argc; i++) {
3533 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3534 if (newstate != VDEV_STATE_HEALTHY) {
3535 (void) printf(gettext("warning: device '%s' "
3536 "onlined, but remains in faulted state\n"),
3538 if (newstate == VDEV_STATE_FAULTED)
3539 (void) printf(gettext("use 'zpool "
3540 "clear' to restore a faulted "
3543 (void) printf(gettext("use 'zpool "
3544 "replace' to replace devices "
3545 "that are no longer present\n"));
3558 * zpool offline [-ft] <pool> <device> ...
3560 * -f Force the device into the offline state, even if doing
3561 * so would appear to compromise pool availability.
3562 * (not supported yet)
3564 * -t Only take the device off-line temporarily. The offline
3565 * state will not be persistent across reboots.
3569 zpool_do_offline(int argc, char **argv)
3573 zpool_handle_t *zhp;
3575 boolean_t istmp = B_FALSE;
3578 while ((c = getopt(argc, argv, "ft")) != -1) {
3585 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3594 /* get pool name and check number of arguments */
3596 (void) fprintf(stderr, gettext("missing pool name\n"));
3600 (void) fprintf(stderr, gettext("missing device name\n"));
3606 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3609 for (i = 1; i < argc; i++) {
3610 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3620 * zpool clear <pool> [device]
3622 * Clear all errors associated with a pool or a particular device.
3625 zpool_do_clear(int argc, char **argv)
3629 boolean_t dryrun = B_FALSE;
3630 boolean_t do_rewind = B_FALSE;
3631 boolean_t xtreme_rewind = B_FALSE;
3632 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3633 nvlist_t *policy = NULL;
3634 zpool_handle_t *zhp;
3635 char *pool, *device;
3638 while ((c = getopt(argc, argv, "FnX")) != -1) {
3647 xtreme_rewind = B_TRUE;
3650 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3660 (void) fprintf(stderr, gettext("missing pool name\n"));
3665 (void) fprintf(stderr, gettext("too many arguments\n"));
3669 if ((dryrun || xtreme_rewind) && !do_rewind) {
3670 (void) fprintf(stderr,
3671 gettext("-n or -X only meaningful with -F\n"));
3675 rewind_policy = ZPOOL_TRY_REWIND;
3677 rewind_policy = ZPOOL_DO_REWIND;
3679 rewind_policy |= ZPOOL_EXTREME_REWIND;
3681 /* In future, further rewind policy choices can be passed along here */
3682 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3683 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3687 device = argc == 2 ? argv[1] : NULL;
3689 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3690 nvlist_free(policy);
3694 if (zpool_clear(zhp, device, policy) != 0)
3699 nvlist_free(policy);
3705 * zpool reguid <pool>
3708 zpool_do_reguid(int argc, char **argv)
3712 zpool_handle_t *zhp;
3716 while ((c = getopt(argc, argv, "")) != -1) {
3719 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3728 /* get pool name and check number of arguments */
3730 (void) fprintf(stderr, gettext("missing pool name\n"));
3735 (void) fprintf(stderr, gettext("too many arguments\n"));
3740 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3743 ret = zpool_reguid(zhp);
3751 * zpool reopen <pool>
3753 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3756 zpool_do_reopen(int argc, char **argv)
3760 zpool_handle_t *zhp;
3764 while ((c = getopt(argc, argv, "")) != -1) {
3767 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3777 (void) fprintf(stderr, gettext("missing pool name\n"));
3782 (void) fprintf(stderr, gettext("too many arguments\n"));
3787 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3790 ret = zpool_reopen(zhp);
3795 typedef struct scrub_cbdata {
3802 scrub_callback(zpool_handle_t *zhp, void *data)
3804 scrub_cbdata_t *cb = data;
3808 * Ignore faulted pools.
3810 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3811 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3812 "currently unavailable\n"), zpool_get_name(zhp));
3816 err = zpool_scan(zhp, cb->cb_type);
3822 * zpool scrub [-s] <pool> ...
3824 * -s Stop. Stops any in-progress scrub.
3827 zpool_do_scrub(int argc, char **argv)
3832 cb.cb_type = POOL_SCAN_SCRUB;
3835 while ((c = getopt(argc, argv, "s")) != -1) {
3838 cb.cb_type = POOL_SCAN_NONE;
3841 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3853 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3857 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3860 typedef struct status_cbdata {
3862 boolean_t cb_allpools;
3863 boolean_t cb_verbose;
3864 boolean_t cb_explain;
3866 boolean_t cb_dedup_stats;
3870 * Print out detailed scrub status.
3873 print_scan_status(pool_scan_stat_t *ps)
3876 uint64_t elapsed, mins_left, hours_left;
3877 uint64_t pass_exam, examined, total;
3879 double fraction_done;
3880 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3882 (void) printf(gettext(" scan: "));
3884 /* If there's never been a scan, there's not much to say. */
3885 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3886 ps->pss_func >= POOL_SCAN_FUNCS) {
3887 (void) printf(gettext("none requested\n"));
3891 start = ps->pss_start_time;
3892 end = ps->pss_end_time;
3893 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3895 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3896 ps->pss_func == POOL_SCAN_RESILVER);
3898 * Scan is finished or canceled.
3900 if (ps->pss_state == DSS_FINISHED) {
3901 uint64_t minutes_taken = (end - start) / 60;
3904 if (ps->pss_func == POOL_SCAN_SCRUB) {
3905 fmt = gettext("scrub repaired %s in %lluh%um with "
3906 "%llu errors on %s");
3907 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3908 fmt = gettext("resilvered %s in %lluh%um with "
3909 "%llu errors on %s");
3912 (void) printf(fmt, processed_buf,
3913 (u_longlong_t)(minutes_taken / 60),
3914 (uint_t)(minutes_taken % 60),
3915 (u_longlong_t)ps->pss_errors,
3916 ctime((time_t *)&end));
3918 } else if (ps->pss_state == DSS_CANCELED) {
3919 if (ps->pss_func == POOL_SCAN_SCRUB) {
3920 (void) printf(gettext("scrub canceled on %s"),
3922 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3923 (void) printf(gettext("resilver canceled on %s"),
3929 assert(ps->pss_state == DSS_SCANNING);
3932 * Scan is in progress.
3934 if (ps->pss_func == POOL_SCAN_SCRUB) {
3935 (void) printf(gettext("scrub in progress since %s"),
3937 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3938 (void) printf(gettext("resilver in progress since %s"),
3942 examined = ps->pss_examined ? ps->pss_examined : 1;
3943 total = ps->pss_to_examine;
3944 fraction_done = (double)examined / total;
3946 /* elapsed time for this pass */
3947 elapsed = time(NULL) - ps->pss_pass_start;
3948 elapsed = elapsed ? elapsed : 1;
3949 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3950 rate = pass_exam / elapsed;
3951 rate = rate ? rate : 1;
3952 mins_left = ((total - examined) / rate) / 60;
3953 hours_left = mins_left / 60;
3955 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3956 zfs_nicenum(total, total_buf, sizeof (total_buf));
3957 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3960 * do not print estimated time if hours_left is more than 30 days
3962 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3963 examined_buf, total_buf, rate_buf);
3964 if (hours_left < (30 * 24)) {
3965 (void) printf(gettext(", %lluh%um to go\n"),
3966 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3968 (void) printf(gettext(
3969 ", (scan is slow, no estimated time)\n"));
3972 if (ps->pss_func == POOL_SCAN_RESILVER) {
3973 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3974 processed_buf, 100 * fraction_done);
3975 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3976 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3977 processed_buf, 100 * fraction_done);
3982 print_error_log(zpool_handle_t *zhp)
3984 nvlist_t *nverrlist = NULL;
3987 size_t len = MAXPATHLEN * 2;
3989 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3990 (void) printf("errors: List of errors unavailable "
3991 "(insufficient privileges)\n");
3995 (void) printf("errors: Permanent errors have been "
3996 "detected in the following files:\n\n");
3998 pathname = safe_malloc(len);
4000 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4002 uint64_t dsobj, obj;
4004 verify(nvpair_value_nvlist(elem, &nv) == 0);
4005 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4007 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4009 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4010 (void) printf("%7s %s\n", "", pathname);
4013 nvlist_free(nverrlist);
4017 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4026 (void) printf(gettext("\tspares\n"));
4028 for (i = 0; i < nspares; i++) {
4029 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4030 print_status_config(zhp, name, spares[i],
4031 namewidth, 2, B_TRUE);
4037 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4046 (void) printf(gettext("\tcache\n"));
4048 for (i = 0; i < nl2cache; i++) {
4049 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4050 print_status_config(zhp, name, l2cache[i],
4051 namewidth, 2, B_FALSE);
4057 print_dedup_stats(nvlist_t *config)
4059 ddt_histogram_t *ddh;
4065 * If the pool was faulted then we may not have been able to
4066 * obtain the config. Otherwise, if we have anything in the dedup
4067 * table continue processing the stats.
4069 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4070 (uint64_t **)&ddo, &c) != 0)
4073 (void) printf("\n");
4074 (void) printf(gettext(" dedup: "));
4075 if (ddo->ddo_count == 0) {
4076 (void) printf(gettext("no DDT entries\n"));
4080 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4081 (u_longlong_t)ddo->ddo_count,
4082 (u_longlong_t)ddo->ddo_dspace,
4083 (u_longlong_t)ddo->ddo_mspace);
4085 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4086 (uint64_t **)&dds, &c) == 0);
4087 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4088 (uint64_t **)&ddh, &c) == 0);
4089 zpool_dump_ddt(dds, ddh);
4093 * Display a summary of pool status. Displays a summary such as:
4097 * reason: One or more devices ...
4098 * see: http://illumos.org/msg/ZFS-xxxx-01
4104 * When given the '-v' option, we print out the complete config. If the '-e'
4105 * option is specified, then we print out error rate information as well.
4108 status_callback(zpool_handle_t *zhp, void *data)
4110 status_cbdata_t *cbp = data;
4111 nvlist_t *config, *nvroot;
4118 config = zpool_get_config(zhp, NULL);
4119 reason = zpool_get_status(zhp, &msgid);
4124 * If we were given 'zpool status -x', only report those pools with
4127 if (cbp->cb_explain &&
4128 (reason == ZPOOL_STATUS_OK ||
4129 reason == ZPOOL_STATUS_VERSION_OLDER ||
4130 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4131 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4132 if (!cbp->cb_allpools) {
4133 (void) printf(gettext("pool '%s' is healthy\n"),
4134 zpool_get_name(zhp));
4136 cbp->cb_first = B_FALSE;
4142 cbp->cb_first = B_FALSE;
4144 (void) printf("\n");
4146 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4148 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4149 (uint64_t **)&vs, &c) == 0);
4150 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4152 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4153 (void) printf(gettext(" state: %s\n"), health);
4156 case ZPOOL_STATUS_MISSING_DEV_R:
4157 (void) printf(gettext("status: One or more devices could not "
4158 "be opened. Sufficient replicas exist for\n\tthe pool to "
4159 "continue functioning in a degraded state.\n"));
4160 (void) printf(gettext("action: Attach the missing device and "
4161 "online it using 'zpool online'.\n"));
4164 case ZPOOL_STATUS_MISSING_DEV_NR:
4165 (void) printf(gettext("status: One or more devices could not "
4166 "be opened. There are insufficient\n\treplicas for the "
4167 "pool to continue functioning.\n"));
4168 (void) printf(gettext("action: Attach the missing device and "
4169 "online it using 'zpool online'.\n"));
4172 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4173 (void) printf(gettext("status: One or more devices could not "
4174 "be used because the label is missing or\n\tinvalid. "
4175 "Sufficient replicas exist for the pool to continue\n\t"
4176 "functioning in a degraded state.\n"));
4177 (void) printf(gettext("action: Replace the device using "
4178 "'zpool replace'.\n"));
4181 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4182 (void) printf(gettext("status: One or more devices could not "
4183 "be used because the label is missing \n\tor invalid. "
4184 "There are insufficient replicas for the pool to "
4185 "continue\n\tfunctioning.\n"));
4186 zpool_explain_recover(zpool_get_handle(zhp),
4187 zpool_get_name(zhp), reason, config);
4190 case ZPOOL_STATUS_FAILING_DEV:
4191 (void) printf(gettext("status: One or more devices has "
4192 "experienced an unrecoverable error. An\n\tattempt was "
4193 "made to correct the error. Applications are "
4195 (void) printf(gettext("action: Determine if the device needs "
4196 "to be replaced, and clear the errors\n\tusing "
4197 "'zpool clear' or replace the device with 'zpool "
4201 case ZPOOL_STATUS_OFFLINE_DEV:
4202 (void) printf(gettext("status: One or more devices has "
4203 "been taken offline by the administrator.\n\tSufficient "
4204 "replicas exist for the pool to continue functioning in "
4205 "a\n\tdegraded state.\n"));
4206 (void) printf(gettext("action: Online the device using "
4207 "'zpool online' or replace the device with\n\t'zpool "
4211 case ZPOOL_STATUS_REMOVED_DEV:
4212 (void) printf(gettext("status: One or more devices has "
4213 "been removed by the administrator.\n\tSufficient "
4214 "replicas exist for the pool to continue functioning in "
4215 "a\n\tdegraded state.\n"));
4216 (void) printf(gettext("action: Online the device using "
4217 "'zpool online' or replace the device with\n\t'zpool "
4221 case ZPOOL_STATUS_RESILVERING:
4222 (void) printf(gettext("status: One or more devices is "
4223 "currently being resilvered. The pool will\n\tcontinue "
4224 "to function, possibly in a degraded state.\n"));
4225 (void) printf(gettext("action: Wait for the resilver to "
4229 case ZPOOL_STATUS_CORRUPT_DATA:
4230 (void) printf(gettext("status: One or more devices has "
4231 "experienced an error resulting in data\n\tcorruption. "
4232 "Applications may be affected.\n"));
4233 (void) printf(gettext("action: Restore the file in question "
4234 "if possible. Otherwise restore the\n\tentire pool from "
4238 case ZPOOL_STATUS_CORRUPT_POOL:
4239 (void) printf(gettext("status: The pool metadata is corrupted "
4240 "and the pool cannot be opened.\n"));
4241 zpool_explain_recover(zpool_get_handle(zhp),
4242 zpool_get_name(zhp), reason, config);
4245 case ZPOOL_STATUS_VERSION_OLDER:
4246 (void) printf(gettext("status: The pool is formatted using a "
4247 "legacy on-disk format. The pool can\n\tstill be used, "
4248 "but some features are unavailable.\n"));
4249 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4250 "upgrade'. Once this is done, the\n\tpool will no longer "
4251 "be accessible on software that does not support feature\n"
4255 case ZPOOL_STATUS_VERSION_NEWER:
4256 (void) printf(gettext("status: The pool has been upgraded to a "
4257 "newer, incompatible on-disk version.\n\tThe pool cannot "
4258 "be accessed on this system.\n"));
4259 (void) printf(gettext("action: Access the pool from a system "
4260 "running more recent software, or\n\trestore the pool from "
4264 case ZPOOL_STATUS_FEAT_DISABLED:
4265 (void) printf(gettext("status: Some supported features are not "
4266 "enabled on the pool. The pool can\n\tstill be used, but "
4267 "some features are unavailable.\n"));
4268 (void) printf(gettext("action: Enable all features using "
4269 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4270 "longer be accessible by software that does not support\n\t"
4271 "the features. See zpool-features(7) for details.\n"));
4274 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4275 (void) printf(gettext("status: The pool cannot be accessed on "
4276 "this system because it uses the\n\tfollowing feature(s) "
4277 "not supported on this system:\n"));
4278 zpool_print_unsup_feat(config);
4279 (void) printf("\n");
4280 (void) printf(gettext("action: Access the pool from a system "
4281 "that supports the required feature(s),\n\tor restore the "
4282 "pool from backup.\n"));
4285 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4286 (void) printf(gettext("status: The pool can only be accessed "
4287 "in read-only mode on this system. It\n\tcannot be "
4288 "accessed in read-write mode because it uses the "
4289 "following\n\tfeature(s) not supported on this system:\n"));
4290 zpool_print_unsup_feat(config);
4291 (void) printf("\n");
4292 (void) printf(gettext("action: The pool cannot be accessed in "
4293 "read-write mode. Import the pool with\n"
4294 "\t\"-o readonly=on\", access the pool from a system that "
4295 "supports the\n\trequired feature(s), or restore the "
4296 "pool from backup.\n"));
4299 case ZPOOL_STATUS_FAULTED_DEV_R:
4300 (void) printf(gettext("status: One or more devices are "
4301 "faulted in response to persistent errors.\n\tSufficient "
4302 "replicas exist for the pool to continue functioning "
4303 "in a\n\tdegraded state.\n"));
4304 (void) printf(gettext("action: Replace the faulted device, "
4305 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4308 case ZPOOL_STATUS_FAULTED_DEV_NR:
4309 (void) printf(gettext("status: One or more devices are "
4310 "faulted in response to persistent errors. There are "
4311 "insufficient replicas for the pool to\n\tcontinue "
4313 (void) printf(gettext("action: Destroy and re-create the pool "
4314 "from a backup source. Manually marking the device\n"
4315 "\trepaired using 'zpool clear' may allow some data "
4316 "to be recovered.\n"));
4319 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4320 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4321 (void) printf(gettext("status: One or more devices are "
4322 "faulted in response to IO failures.\n"));
4323 (void) printf(gettext("action: Make sure the affected devices "
4324 "are connected, then run 'zpool clear'.\n"));
4327 case ZPOOL_STATUS_BAD_LOG:
4328 (void) printf(gettext("status: An intent log record "
4329 "could not be read.\n"
4330 "\tWaiting for adminstrator intervention to fix the "
4331 "faulted pool.\n"));
4332 (void) printf(gettext("action: Either restore the affected "
4333 "device(s) and run 'zpool online',\n"
4334 "\tor ignore the intent log records by running "
4335 "'zpool clear'.\n"));
4338 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4339 (void) printf(gettext("status: One or more devices are "
4340 "configured to use a non-native block size.\n"
4341 "\tExpect reduced performance.\n"));
4342 (void) printf(gettext("action: Replace affected devices with "
4343 "devices that support the\n\tconfigured block size, or "
4344 "migrate data to a properly configured\n\tpool.\n"));
4349 * The remaining errors can't actually be generated, yet.
4351 assert(reason == ZPOOL_STATUS_OK);
4355 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4358 if (config != NULL) {
4361 nvlist_t **spares, **l2cache;
4362 uint_t nspares, nl2cache;
4363 pool_scan_stat_t *ps = NULL;
4365 (void) nvlist_lookup_uint64_array(nvroot,
4366 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4367 print_scan_status(ps);
4369 namewidth = max_width(zhp, nvroot, 0, 0);
4373 (void) printf(gettext("config:\n\n"));
4374 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4375 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4376 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4377 namewidth, 0, B_FALSE);
4379 if (num_logs(nvroot) > 0)
4380 print_logs(zhp, nvroot, namewidth, B_TRUE);
4381 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4382 &l2cache, &nl2cache) == 0)
4383 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4385 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4386 &spares, &nspares) == 0)
4387 print_spares(zhp, spares, nspares, namewidth);
4389 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4391 nvlist_t *nverrlist = NULL;
4394 * If the approximate error count is small, get a
4395 * precise count by fetching the entire log and
4396 * uniquifying the results.
4398 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4399 zpool_get_errlog(zhp, &nverrlist) == 0) {
4404 while ((elem = nvlist_next_nvpair(nverrlist,
4409 nvlist_free(nverrlist);
4411 (void) printf("\n");
4414 (void) printf(gettext("errors: No known data "
4416 else if (!cbp->cb_verbose)
4417 (void) printf(gettext("errors: %llu data "
4418 "errors, use '-v' for a list\n"),
4419 (u_longlong_t)nerr);
4421 print_error_log(zhp);
4424 if (cbp->cb_dedup_stats)
4425 print_dedup_stats(config);
4427 (void) printf(gettext("config: The configuration cannot be "
4435 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4437 * -v Display complete error logs
4438 * -x Display only pools with potential problems
4439 * -D Display dedup status (undocumented)
4440 * -T Display a timestamp in date(1) or Unix format
4442 * Describes the health status of all pools or some subset.
4445 zpool_do_status(int argc, char **argv)
4449 unsigned long interval = 0, count = 0;
4450 status_cbdata_t cb = { 0 };
4453 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4456 cb.cb_verbose = B_TRUE;
4459 cb.cb_explain = B_TRUE;
4462 cb.cb_dedup_stats = B_TRUE;
4465 get_timestamp_arg(*optarg);
4468 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4477 get_interval_count(&argc, argv, &interval, &count);
4480 cb.cb_allpools = B_TRUE;
4482 cb.cb_first = B_TRUE;
4485 if (timestamp_fmt != NODATE)
4486 print_timestamp(timestamp_fmt);
4488 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4489 status_callback, &cb);
4491 if (argc == 0 && cb.cb_count == 0)
4492 (void) printf(gettext("no pools available\n"));
4493 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4494 (void) printf(gettext("all pools are healthy\n"));
4502 if (count != 0 && --count == 0)
4505 (void) sleep(interval);
4511 typedef struct upgrade_cbdata {
4513 char cb_poolname[ZPOOL_MAXNAMELEN];
4515 uint64_t cb_version;
4521 is_root_pool(zpool_handle_t *zhp)
4523 static struct statfs sfs;
4524 static char *poolname = NULL;
4525 static boolean_t stated = B_FALSE;
4530 if (statfs("/", &sfs) == -1) {
4531 (void) fprintf(stderr,
4532 "Unable to stat root file system: %s.\n",
4536 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4538 poolname = sfs.f_mntfromname;
4539 if ((slash = strchr(poolname, '/')) != NULL)
4542 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4546 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
4549 if (poolname[0] == '\0' && is_root_pool(zhp))
4550 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4552 #endif /* FreeBSD */
4555 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4559 uint64_t oldversion;
4561 config = zpool_get_config(zhp, NULL);
4562 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4565 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4566 assert(oldversion < version);
4568 ret = zpool_upgrade(zhp, version);
4572 if (version >= SPA_VERSION_FEATURES) {
4573 (void) printf(gettext("Successfully upgraded "
4574 "'%s' from version %llu to feature flags.\n"),
4575 zpool_get_name(zhp), oldversion);
4577 (void) printf(gettext("Successfully upgraded "
4578 "'%s' from version %llu to version %llu.\n"),
4579 zpool_get_name(zhp), oldversion, version);
4586 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4589 boolean_t firstff = B_TRUE;
4590 nvlist_t *enabled = zpool_get_features(zhp);
4593 for (i = 0; i < SPA_FEATURES; i++) {
4594 const char *fname = spa_feature_table[i].fi_uname;
4595 const char *fguid = spa_feature_table[i].fi_guid;
4596 if (!nvlist_exists(enabled, fguid)) {
4598 verify(-1 != asprintf(&propname, "feature@%s", fname));
4599 ret = zpool_set_prop(zhp, propname,
4600 ZFS_FEATURE_ENABLED);
4608 (void) printf(gettext("Enabled the "
4609 "following features on '%s':\n"),
4610 zpool_get_name(zhp));
4613 (void) printf(gettext(" %s\n"), fname);
4624 upgrade_cb(zpool_handle_t *zhp, void *arg)
4626 upgrade_cbdata_t *cbp = arg;
4629 boolean_t printnl = B_FALSE;
4632 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4633 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
4634 "currently unavailable\n\n"), zpool_get_name(zhp));
4635 /* Allow iteration to continue. */
4639 config = zpool_get_config(zhp, NULL);
4640 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4643 assert(SPA_VERSION_IS_SUPPORTED(version));
4645 if (version < cbp->cb_version) {
4646 cbp->cb_first = B_FALSE;
4647 ret = upgrade_version(zhp, cbp->cb_version);
4651 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4652 sizeof(cbp->cb_poolname));
4653 #endif /* __FreeBSD__ */
4658 * If they did "zpool upgrade -a", then we could
4659 * be doing ioctls to different pools. We need
4660 * to log this history once to each pool, and bypass
4661 * the normal history logging that happens in main().
4663 (void) zpool_log_history(g_zfs, history_str);
4664 log_history = B_FALSE;
4668 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4670 ret = upgrade_enable_all(zhp, &count);
4675 cbp->cb_first = B_FALSE;
4678 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4679 sizeof(cbp->cb_poolname));
4680 #endif /* __FreeBSD__ */
4682 * If they did "zpool upgrade -a", then we could
4683 * be doing ioctls to different pools. We need
4684 * to log this history once to each pool, and bypass
4685 * the normal history logging that happens in main().
4687 (void) zpool_log_history(g_zfs, history_str);
4688 log_history = B_FALSE;
4693 (void) printf(gettext("\n"));
4700 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4702 upgrade_cbdata_t *cbp = arg;
4706 config = zpool_get_config(zhp, NULL);
4707 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4710 assert(SPA_VERSION_IS_SUPPORTED(version));
4712 if (version < SPA_VERSION_FEATURES) {
4713 if (cbp->cb_first) {
4714 (void) printf(gettext("The following pools are "
4715 "formatted with legacy version numbers and can\n"
4716 "be upgraded to use feature flags. After "
4717 "being upgraded, these pools\nwill no "
4718 "longer be accessible by software that does not "
4719 "support feature\nflags.\n\n"));
4720 (void) printf(gettext("VER POOL\n"));
4721 (void) printf(gettext("--- ------------\n"));
4722 cbp->cb_first = B_FALSE;
4725 (void) printf("%2llu %s\n", (u_longlong_t)version,
4726 zpool_get_name(zhp));
4733 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4735 upgrade_cbdata_t *cbp = arg;
4739 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4740 (void) fprintf(stderr, gettext("cannot check supported "
4741 "features on '%s': pool is currently unavailable\n\n"),
4742 zpool_get_name(zhp));
4743 /* Allow iteration to continue. */
4747 config = zpool_get_config(zhp, NULL);
4748 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4751 if (version >= SPA_VERSION_FEATURES) {
4753 boolean_t poolfirst = B_TRUE;
4754 nvlist_t *enabled = zpool_get_features(zhp);
4756 for (i = 0; i < SPA_FEATURES; i++) {
4757 const char *fguid = spa_feature_table[i].fi_guid;
4758 const char *fname = spa_feature_table[i].fi_uname;
4759 if (!nvlist_exists(enabled, fguid)) {
4760 if (cbp->cb_first) {
4761 (void) printf(gettext("\nSome "
4762 "supported features are not "
4763 "enabled on the following pools. "
4764 "Once a\nfeature is enabled the "
4765 "pool may become incompatible with "
4766 "software\nthat does not support "
4768 "zpool-features(7) for "
4770 (void) printf(gettext("POOL "
4772 (void) printf(gettext("------"
4774 cbp->cb_first = B_FALSE;
4778 (void) printf(gettext("%s\n"),
4779 zpool_get_name(zhp));
4780 poolfirst = B_FALSE;
4783 (void) printf(gettext(" %s\n"), fname);
4793 upgrade_one(zpool_handle_t *zhp, void *data)
4795 boolean_t printnl = B_FALSE;
4796 upgrade_cbdata_t *cbp = data;
4797 uint64_t cur_version;
4800 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4801 (void) printf(gettext("'log' is now a reserved word\n"
4802 "Pool 'log' must be renamed using export and import"
4807 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4808 if (cur_version > cbp->cb_version) {
4809 (void) printf(gettext("Pool '%s' is already formatted "
4810 "using more current version '%llu'.\n\n"),
4811 zpool_get_name(zhp), cur_version);
4815 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4816 (void) printf(gettext("Pool '%s' is already formatted "
4817 "using version %llu.\n\n"), zpool_get_name(zhp),
4822 if (cur_version != cbp->cb_version) {
4824 ret = upgrade_version(zhp, cbp->cb_version);
4828 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4829 sizeof(cbp->cb_poolname));
4830 #endif /* __FreeBSD__ */
4833 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4835 ret = upgrade_enable_all(zhp, &count);
4842 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4843 sizeof(cbp->cb_poolname));
4844 #endif /* __FreeBSD __*/
4845 } else if (cur_version == SPA_VERSION) {
4846 (void) printf(gettext("Pool '%s' already has all "
4847 "supported features enabled.\n"),
4848 zpool_get_name(zhp));
4853 (void) printf(gettext("\n"));
4862 * zpool upgrade [-V version] <-a | pool ...>
4864 * With no arguments, display downrev'd ZFS pool available for upgrade.
4865 * Individual pools can be upgraded by specifying the pool, and '-a' will
4866 * upgrade all pools.
4869 zpool_do_upgrade(int argc, char **argv)
4872 upgrade_cbdata_t cb = { 0 };
4874 boolean_t showversions = B_FALSE;
4875 boolean_t upgradeall = B_FALSE;
4880 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4883 upgradeall = B_TRUE;
4886 showversions = B_TRUE;
4889 cb.cb_version = strtoll(optarg, &end, 10);
4891 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4892 (void) fprintf(stderr,
4893 gettext("invalid version '%s'\n"), optarg);
4898 (void) fprintf(stderr, gettext("missing argument for "
4899 "'%c' option\n"), optopt);
4903 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4914 if (cb.cb_version == 0) {
4915 cb.cb_version = SPA_VERSION;
4916 } else if (!upgradeall && argc == 0) {
4917 (void) fprintf(stderr, gettext("-V option is "
4918 "incompatible with other arguments\n"));
4923 if (upgradeall || argc != 0) {
4924 (void) fprintf(stderr, gettext("-v option is "
4925 "incompatible with other arguments\n"));
4928 } else if (upgradeall) {
4930 (void) fprintf(stderr, gettext("-a option should not "
4931 "be used along with a pool name\n"));
4936 (void) printf(gettext("This system supports ZFS pool feature "
4941 (void) printf(gettext("The following features are "
4943 (void) printf(gettext("FEAT DESCRIPTION\n"));
4944 (void) printf("----------------------------------------------"
4945 "---------------\n");
4946 for (i = 0; i < SPA_FEATURES; i++) {
4947 zfeature_info_t *fi = &spa_feature_table[i];
4948 const char *ro = fi->fi_can_readonly ?
4949 " (read-only compatible)" : "";
4951 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4952 (void) printf(" %s\n", fi->fi_desc);
4954 (void) printf("\n");
4956 (void) printf(gettext("The following legacy versions are also "
4958 (void) printf(gettext("VER DESCRIPTION\n"));
4959 (void) printf("--- -----------------------------------------"
4960 "---------------\n");
4961 (void) printf(gettext(" 1 Initial ZFS version\n"));
4962 (void) printf(gettext(" 2 Ditto blocks "
4963 "(replicated metadata)\n"));
4964 (void) printf(gettext(" 3 Hot spares and double parity "
4966 (void) printf(gettext(" 4 zpool history\n"));
4967 (void) printf(gettext(" 5 Compression using the gzip "
4969 (void) printf(gettext(" 6 bootfs pool property\n"));
4970 (void) printf(gettext(" 7 Separate intent log devices\n"));
4971 (void) printf(gettext(" 8 Delegated administration\n"));
4972 (void) printf(gettext(" 9 refquota and refreservation "
4974 (void) printf(gettext(" 10 Cache devices\n"));
4975 (void) printf(gettext(" 11 Improved scrub performance\n"));
4976 (void) printf(gettext(" 12 Snapshot properties\n"));
4977 (void) printf(gettext(" 13 snapused property\n"));
4978 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4979 (void) printf(gettext(" 15 user/group space accounting\n"));
4980 (void) printf(gettext(" 16 stmf property support\n"));
4981 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4982 (void) printf(gettext(" 18 Snapshot user holds\n"));
4983 (void) printf(gettext(" 19 Log device removal\n"));
4984 (void) printf(gettext(" 20 Compression using zle "
4985 "(zero-length encoding)\n"));
4986 (void) printf(gettext(" 21 Deduplication\n"));
4987 (void) printf(gettext(" 22 Received properties\n"));
4988 (void) printf(gettext(" 23 Slim ZIL\n"));
4989 (void) printf(gettext(" 24 System attributes\n"));
4990 (void) printf(gettext(" 25 Improved scrub stats\n"));
4991 (void) printf(gettext(" 26 Improved snapshot deletion "
4993 (void) printf(gettext(" 27 Improved snapshot creation "
4995 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4996 (void) printf(gettext("\nFor more information on a particular "
4997 "version, including supported releases,\n"));
4998 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4999 } else if (argc == 0 && upgradeall) {
5000 cb.cb_first = B_TRUE;
5001 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5002 if (ret == 0 && cb.cb_first) {
5003 if (cb.cb_version == SPA_VERSION) {
5004 (void) printf(gettext("All pools are already "
5005 "formatted using feature flags.\n\n"));
5006 (void) printf(gettext("Every feature flags "
5007 "pool already has all supported features "
5010 (void) printf(gettext("All pools are already "
5011 "formatted with version %llu or higher.\n"),
5015 } else if (argc == 0) {
5016 cb.cb_first = B_TRUE;
5017 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5021 (void) printf(gettext("All pools are formatted "
5022 "using feature flags.\n\n"));
5024 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5025 "for a list of available legacy versions.\n"));
5028 cb.cb_first = B_TRUE;
5029 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5033 (void) printf(gettext("Every feature flags pool has "
5034 "all supported features enabled.\n"));
5036 (void) printf(gettext("\n"));
5039 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5043 if (cb.cb_poolname[0] != '\0') {
5045 "If you boot from pool '%s', don't forget to update boot code.\n"
5046 "Assuming you use GPT partitioning and da0 is your boot disk\n"
5047 "the following command will do it:\n"
5049 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5056 typedef struct hist_cbdata {
5063 * Print out the command history for a specific pool.
5066 get_history_one(zpool_handle_t *zhp, void *data)
5072 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5074 cb->first = B_FALSE;
5076 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5078 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5081 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5082 &records, &numrecords) == 0);
5083 for (i = 0; i < numrecords; i++) {
5084 nvlist_t *rec = records[i];
5087 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5091 tsec = fnvlist_lookup_uint64(records[i],
5093 (void) localtime_r(&tsec, &t);
5094 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5097 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5098 (void) printf("%s %s", tbuf,
5099 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5100 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5102 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5105 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5106 (void) printf("%s unrecognized record:\n",
5108 dump_nvlist(rec, 4);
5111 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5112 zfs_history_event_names[ievent],
5113 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5114 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5115 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5118 (void) printf("%s [txg:%lld] %s", tbuf,
5119 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5120 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5121 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5122 (void) printf(" %s (%llu)",
5123 fnvlist_lookup_string(rec,
5125 fnvlist_lookup_uint64(rec,
5128 (void) printf(" %s", fnvlist_lookup_string(rec,
5129 ZPOOL_HIST_INT_STR));
5130 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5133 (void) printf("%s ioctl %s\n", tbuf,
5134 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5135 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5136 (void) printf(" input:\n");
5137 dump_nvlist(fnvlist_lookup_nvlist(rec,
5138 ZPOOL_HIST_INPUT_NVL), 8);
5140 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5141 (void) printf(" output:\n");
5142 dump_nvlist(fnvlist_lookup_nvlist(rec,
5143 ZPOOL_HIST_OUTPUT_NVL), 8);
5148 (void) printf("%s unrecognized record:\n", tbuf);
5149 dump_nvlist(rec, 4);
5153 (void) printf("\n");
5156 (void) printf(" [");
5157 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5158 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5159 struct passwd *pwd = getpwuid(who);
5160 (void) printf("user %d ", (int)who);
5162 (void) printf("(%s) ", pwd->pw_name);
5164 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5165 (void) printf("on %s",
5166 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5168 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5169 (void) printf(":%s",
5170 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5173 (void) printf("\n");
5175 (void) printf("\n");
5182 * zpool history <pool>
5184 * Displays the history of commands that modified pools.
5187 zpool_do_history(int argc, char **argv)
5189 hist_cbdata_t cbdata = { 0 };
5193 cbdata.first = B_TRUE;
5195 while ((c = getopt(argc, argv, "li")) != -1) {
5198 cbdata.longfmt = B_TRUE;
5201 cbdata.internal = B_TRUE;
5204 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5212 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5215 if (argc == 0 && cbdata.first == B_TRUE) {
5216 (void) printf(gettext("no pools available\n"));
5224 get_callback(zpool_handle_t *zhp, void *data)
5226 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5227 char value[MAXNAMELEN];
5228 zprop_source_t srctype;
5231 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5234 * Skip the special fake placeholder. This will also skip
5235 * over the name property when 'all' is specified.
5237 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5238 pl == cbp->cb_proplist)
5241 if (pl->pl_prop == ZPROP_INVAL &&
5242 (zpool_prop_feature(pl->pl_user_prop) ||
5243 zpool_prop_unsupported(pl->pl_user_prop))) {
5244 srctype = ZPROP_SRC_LOCAL;
5246 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5247 value, sizeof (value)) == 0) {
5248 zprop_print_one_property(zpool_get_name(zhp),
5249 cbp, pl->pl_user_prop, value, srctype,
5253 if (zpool_get_prop(zhp, pl->pl_prop, value,
5254 sizeof (value), &srctype, cbp->cb_literal) != 0)
5257 zprop_print_one_property(zpool_get_name(zhp), cbp,
5258 zpool_prop_to_name(pl->pl_prop), value, srctype,
5266 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5268 * -H Scripted mode. Don't display headers, and separate properties
5270 * -o List of columns to display. Defaults to
5271 * "name,property,value,source".
5272 * -p Diplay values in parsable (exact) format.
5274 * Get properties of pools in the system. Output space statistics
5275 * for each one as well as other attributes.
5278 zpool_do_get(int argc, char **argv)
5280 zprop_get_cbdata_t cb = { 0 };
5281 zprop_list_t fake_name = { 0 };
5286 cb.cb_first = B_TRUE;
5289 * Set up default columns and sources.
5291 cb.cb_sources = ZPROP_SRC_ALL;
5292 cb.cb_columns[0] = GET_COL_NAME;
5293 cb.cb_columns[1] = GET_COL_PROPERTY;
5294 cb.cb_columns[2] = GET_COL_VALUE;
5295 cb.cb_columns[3] = GET_COL_SOURCE;
5296 cb.cb_type = ZFS_TYPE_POOL;
5299 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5302 cb.cb_literal = B_TRUE;
5305 cb.cb_scripted = B_TRUE;
5308 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5310 while (*optarg != '\0') {
5311 static char *col_subopts[] =
5312 { "name", "property", "value", "source",
5315 if (i == ZFS_GET_NCOLS) {
5316 (void) fprintf(stderr, gettext("too "
5317 "many fields given to -o "
5322 switch (getsubopt(&optarg, col_subopts,
5325 cb.cb_columns[i++] = GET_COL_NAME;
5328 cb.cb_columns[i++] = GET_COL_PROPERTY;
5331 cb.cb_columns[i++] = GET_COL_VALUE;
5334 cb.cb_columns[i++] = GET_COL_SOURCE;
5338 (void) fprintf(stderr,
5339 gettext("\"all\" conflicts "
5340 "with specific fields "
5341 "given to -o option\n"));
5344 cb.cb_columns[0] = GET_COL_NAME;
5345 cb.cb_columns[1] = GET_COL_PROPERTY;
5346 cb.cb_columns[2] = GET_COL_VALUE;
5347 cb.cb_columns[3] = GET_COL_SOURCE;
5351 (void) fprintf(stderr,
5352 gettext("invalid column name "
5359 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5369 (void) fprintf(stderr, gettext("missing property "
5374 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5375 ZFS_TYPE_POOL) != 0)
5381 if (cb.cb_proplist != NULL) {
5382 fake_name.pl_prop = ZPOOL_PROP_NAME;
5383 fake_name.pl_width = strlen(gettext("NAME"));
5384 fake_name.pl_next = cb.cb_proplist;
5385 cb.cb_proplist = &fake_name;
5388 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5391 if (cb.cb_proplist == &fake_name)
5392 zprop_free_list(fake_name.pl_next);
5394 zprop_free_list(cb.cb_proplist);
5399 typedef struct set_cbdata {
5402 boolean_t cb_any_successful;
5406 set_callback(zpool_handle_t *zhp, void *data)
5409 set_cbdata_t *cb = (set_cbdata_t *)data;
5411 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5414 cb->cb_any_successful = B_TRUE;
5420 zpool_do_set(int argc, char **argv)
5422 set_cbdata_t cb = { 0 };
5425 if (argc > 1 && argv[1][0] == '-') {
5426 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5432 (void) fprintf(stderr, gettext("missing property=value "
5438 (void) fprintf(stderr, gettext("missing pool name\n"));
5443 (void) fprintf(stderr, gettext("too many pool names\n"));
5447 cb.cb_propname = argv[1];
5448 cb.cb_value = strchr(cb.cb_propname, '=');
5449 if (cb.cb_value == NULL) {
5450 (void) fprintf(stderr, gettext("missing value in "
5451 "property=value argument\n"));
5455 *(cb.cb_value) = '\0';
5458 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5465 find_command_idx(char *command, int *idx)
5469 for (i = 0; i < NCOMMAND; i++) {
5470 if (command_table[i].name == NULL)
5473 if (strcmp(command, command_table[i].name) == 0) {
5482 main(int argc, char **argv)
5488 (void) setlocale(LC_ALL, "");
5489 (void) textdomain(TEXT_DOMAIN);
5491 if ((g_zfs = libzfs_init()) == NULL) {
5492 (void) fprintf(stderr, gettext("internal error: failed to "
5493 "initialize ZFS library\n"));
5497 libzfs_print_on_error(g_zfs, B_TRUE);
5502 * Make sure the user has specified some command.
5505 (void) fprintf(stderr, gettext("missing command\n"));
5514 if (strcmp(cmdname, "-?") == 0)
5517 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5520 * Run the appropriate command.
5522 if (find_command_idx(cmdname, &i) == 0) {
5523 current_command = &command_table[i];
5524 ret = command_table[i].func(argc - 1, argv + 1);
5525 } else if (strchr(cmdname, '=')) {
5526 verify(find_command_idx("set", &i) == 0);
5527 current_command = &command_table[i];
5528 ret = command_table[i].func(argc, argv);
5529 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5531 * 'freeze' is a vile debugging abomination, so we treat
5534 zfs_cmd_t zc = { 0 };
5535 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5536 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5538 (void) fprintf(stderr, gettext("unrecognized "
5539 "command '%s'\n"), cmdname);
5543 if (ret == 0 && log_history)
5544 (void) zpool_log_history(g_zfs, history_str);
5549 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5550 * for the purposes of running ::findleaks.
5552 if (getenv("ZFS_ABORT") != NULL) {
5553 (void) printf("dumping core by request\n");