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, 2015 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;
837 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
841 if ((propval = strchr(optarg, '=')) == NULL) {
842 (void) fprintf(stderr, gettext("missing "
843 "'=' for -O option\n"));
850 * Mountpoints are checked and then added later.
851 * Uniquely among properties, they can be specified
852 * more than once, to avoid conflict with -m.
854 if (0 == strcmp(optarg,
855 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
856 mountpoint = propval;
857 } else if (add_prop_list(optarg, propval, &fsprops,
863 (void) fprintf(stderr, gettext("missing argument for "
864 "'%c' option\n"), optopt);
867 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
876 /* get pool name and check number of arguments */
878 (void) fprintf(stderr, gettext("missing pool name argument\n"));
882 (void) fprintf(stderr, gettext("missing vdev specification\n"));
889 * As a special case, check for use of '/' in the name, and direct the
890 * user to use 'zfs create' instead.
892 if (strchr(poolname, '/') != NULL) {
893 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
894 "character '/' in pool name\n"), poolname);
895 (void) fprintf(stderr, gettext("use 'zfs create' to "
896 "create a dataset\n"));
900 /* pass off to get_vdev_spec for bulk processing */
901 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
906 /* make_root_vdev() allows 0 toplevel children if there are spares */
907 if (!zfs_allocatable_devs(nvroot)) {
908 (void) fprintf(stderr, gettext("invalid vdev "
909 "specification: at least one toplevel vdev must be "
914 if (altroot != NULL && altroot[0] != '/') {
915 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
916 "must be an absolute path\n"), altroot);
921 * Check the validity of the mountpoint and direct the user to use the
922 * '-m' mountpoint option if it looks like its in use.
923 * Ignore the checks if the '-f' option is given.
925 if (!force && (mountpoint == NULL ||
926 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
927 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
928 char buf[MAXPATHLEN];
931 if (mountpoint && mountpoint[0] != '/') {
932 (void) fprintf(stderr, gettext("invalid mountpoint "
933 "'%s': must be an absolute path, 'legacy', or "
934 "'none'\n"), mountpoint);
938 if (mountpoint == NULL) {
940 (void) snprintf(buf, sizeof (buf), "%s/%s",
943 (void) snprintf(buf, sizeof (buf), "/%s",
947 (void) snprintf(buf, sizeof (buf), "%s%s",
948 altroot, mountpoint);
950 (void) snprintf(buf, sizeof (buf), "%s",
954 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
955 (void) fprintf(stderr, gettext("mountpoint '%s' : "
956 "%s\n"), buf, strerror(errno));
957 (void) fprintf(stderr, gettext("use '-m' "
958 "option to provide a different default\n"));
963 while (count < 3 && readdir(dirp) != NULL)
965 (void) closedir(dirp);
968 (void) fprintf(stderr, gettext("mountpoint "
969 "'%s' exists and is not empty\n"), buf);
970 (void) fprintf(stderr, gettext("use '-m' "
971 "option to provide a "
972 "different default\n"));
979 * Now that the mountpoint's validity has been checked, ensure that
980 * the property is set appropriately prior to creating the pool.
982 if (mountpoint != NULL) {
983 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
984 mountpoint, &fsprops, B_FALSE);
992 * For a dry run invocation, print out a basic message and run
993 * through all the vdevs in the list and print out in an
994 * appropriate hierarchy.
996 (void) printf(gettext("would create '%s' with the "
997 "following layout:\n\n"), poolname);
999 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1000 if (num_logs(nvroot) > 0)
1001 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1006 * Hand off to libzfs.
1008 if (enable_all_pool_feat) {
1010 for (i = 0; i < SPA_FEATURES; i++) {
1011 char propname[MAXPATHLEN];
1012 zfeature_info_t *feat = &spa_feature_table[i];
1014 (void) snprintf(propname, sizeof (propname),
1015 "feature@%s", feat->fi_uname);
1018 * Skip feature if user specified it manually
1019 * on the command line.
1021 if (nvlist_exists(props, propname))
1024 ret = add_prop_list(propname,
1025 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1032 if (zpool_create(g_zfs, poolname,
1033 nvroot, props, fsprops) == 0) {
1034 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1035 ZFS_TYPE_FILESYSTEM);
1037 if (zfs_mount(pool, NULL, 0) == 0)
1038 ret = zfs_shareall(pool);
1041 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1042 (void) fprintf(stderr, gettext("pool name may have "
1048 nvlist_free(nvroot);
1049 nvlist_free(fsprops);
1053 nvlist_free(fsprops);
1060 * zpool destroy <pool>
1062 * -f Forcefully unmount any datasets
1064 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1067 zpool_do_destroy(int argc, char **argv)
1069 boolean_t force = B_FALSE;
1072 zpool_handle_t *zhp;
1076 while ((c = getopt(argc, argv, "f")) != -1) {
1082 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1091 /* check arguments */
1093 (void) fprintf(stderr, gettext("missing pool argument\n"));
1097 (void) fprintf(stderr, gettext("too many arguments\n"));
1103 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1105 * As a special case, check for use of '/' in the name, and
1106 * direct the user to use 'zfs destroy' instead.
1108 if (strchr(pool, '/') != NULL)
1109 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1110 "destroy a dataset\n"));
1114 if (zpool_disable_datasets(zhp, force) != 0) {
1115 (void) fprintf(stderr, gettext("could not destroy '%s': "
1116 "could not unmount datasets\n"), zpool_get_name(zhp));
1120 /* The history must be logged as part of the export */
1121 log_history = B_FALSE;
1123 ret = (zpool_destroy(zhp, history_str) != 0);
1131 * zpool export [-f] <pool> ...
1133 * -f Forcefully unmount datasets
1135 * Export the given pools. By default, the command will attempt to cleanly
1136 * unmount any active datasets within the pool. If the '-f' flag is specified,
1137 * then the datasets will be forcefully unmounted.
1140 zpool_do_export(int argc, char **argv)
1142 boolean_t force = B_FALSE;
1143 boolean_t hardforce = B_FALSE;
1145 zpool_handle_t *zhp;
1150 while ((c = getopt(argc, argv, "fF")) != -1) {
1159 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1168 /* check arguments */
1170 (void) fprintf(stderr, gettext("missing pool argument\n"));
1175 for (i = 0; i < argc; i++) {
1176 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1181 if (zpool_disable_datasets(zhp, force) != 0) {
1187 /* The history must be logged as part of the export */
1188 log_history = B_FALSE;
1191 if (zpool_export_force(zhp, history_str) != 0)
1193 } else if (zpool_export(zhp, force, history_str) != 0) {
1204 * Given a vdev configuration, determine the maximum width needed for the device
1208 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1210 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1215 if (strlen(name) + depth > max)
1216 max = strlen(name) + depth;
1220 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1221 &child, &children) == 0) {
1222 for (c = 0; c < children; c++)
1223 if ((ret = max_width(zhp, child[c], depth + 2,
1228 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1229 &child, &children) == 0) {
1230 for (c = 0; c < children; c++)
1231 if ((ret = max_width(zhp, child[c], depth + 2,
1236 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1237 &child, &children) == 0) {
1238 for (c = 0; c < children; c++)
1239 if ((ret = max_width(zhp, child[c], depth + 2,
1248 typedef struct spare_cbdata {
1250 zpool_handle_t *cb_zhp;
1254 find_vdev(nvlist_t *nv, uint64_t search)
1260 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1264 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1265 &child, &children) == 0) {
1266 for (c = 0; c < children; c++)
1267 if (find_vdev(child[c], search))
1275 find_spare(zpool_handle_t *zhp, void *data)
1277 spare_cbdata_t *cbp = data;
1278 nvlist_t *config, *nvroot;
1280 config = zpool_get_config(zhp, NULL);
1281 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1284 if (find_vdev(nvroot, cbp->cb_guid)) {
1294 * Print out configuration state as requested by status_callback.
1297 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1298 int namewidth, int depth, boolean_t isspare)
1301 uint_t c, vsc, children;
1302 pool_scan_stat_t *ps = NULL;
1304 char rbuf[6], wbuf[6], cbuf[6];
1306 uint64_t notpresent;
1311 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1312 &child, &children) != 0)
1315 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1316 (uint64_t **)&vs, &vsc) == 0);
1318 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1321 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1324 if (vs->vs_aux == VDEV_AUX_SPARED)
1326 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1330 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1334 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1335 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1336 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1337 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1340 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1341 ¬present) == 0 ||
1342 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1344 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1345 (void) printf(" was %s", path);
1346 } else if (vs->vs_aux != 0) {
1349 switch (vs->vs_aux) {
1350 case VDEV_AUX_OPEN_FAILED:
1351 (void) printf(gettext("cannot open"));
1354 case VDEV_AUX_BAD_GUID_SUM:
1355 (void) printf(gettext("missing device"));
1358 case VDEV_AUX_NO_REPLICAS:
1359 (void) printf(gettext("insufficient replicas"));
1362 case VDEV_AUX_VERSION_NEWER:
1363 (void) printf(gettext("newer version"));
1366 case VDEV_AUX_UNSUP_FEAT:
1367 (void) printf(gettext("unsupported feature(s)"));
1370 case VDEV_AUX_ASHIFT_TOO_BIG:
1371 (void) printf(gettext("unsupported minimum blocksize"));
1374 case VDEV_AUX_SPARED:
1375 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1377 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1378 if (strcmp(zpool_get_name(cb.cb_zhp),
1379 zpool_get_name(zhp)) == 0)
1380 (void) printf(gettext("currently in "
1383 (void) printf(gettext("in use by "
1385 zpool_get_name(cb.cb_zhp));
1386 zpool_close(cb.cb_zhp);
1388 (void) printf(gettext("currently in use"));
1392 case VDEV_AUX_ERR_EXCEEDED:
1393 (void) printf(gettext("too many errors"));
1396 case VDEV_AUX_IO_FAILURE:
1397 (void) printf(gettext("experienced I/O failures"));
1400 case VDEV_AUX_BAD_LOG:
1401 (void) printf(gettext("bad intent log"));
1404 case VDEV_AUX_EXTERNAL:
1405 (void) printf(gettext("external device fault"));
1408 case VDEV_AUX_SPLIT_POOL:
1409 (void) printf(gettext("split into new pool"));
1413 (void) printf(gettext("corrupted data"));
1416 } else if (children == 0 && !isspare &&
1417 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1418 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1420 gettext(" block size: %dB configured, %dB native"),
1421 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1424 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1425 (uint64_t **)&ps, &c);
1427 if (ps && ps->pss_state == DSS_SCANNING &&
1428 vs->vs_scan_processed != 0 && children == 0) {
1429 (void) printf(gettext(" (%s)"),
1430 (ps->pss_func == POOL_SCAN_RESILVER) ?
1431 "resilvering" : "repairing");
1434 (void) printf("\n");
1436 for (c = 0; c < children; c++) {
1437 uint64_t islog = B_FALSE, ishole = B_FALSE;
1439 /* Don't print logs or holes here */
1440 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1442 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1444 if (islog || ishole)
1446 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1447 print_status_config(zhp, vname, child[c],
1448 namewidth, depth + 2, isspare);
1455 * Print the configuration of an exported pool. Iterate over all vdevs in the
1456 * pool, printing out the name and status for each one.
1459 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1466 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1467 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1468 strcmp(type, VDEV_TYPE_HOLE) == 0)
1471 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1472 (uint64_t **)&vs, &c) == 0);
1474 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1475 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1477 if (vs->vs_aux != 0) {
1480 switch (vs->vs_aux) {
1481 case VDEV_AUX_OPEN_FAILED:
1482 (void) printf(gettext("cannot open"));
1485 case VDEV_AUX_BAD_GUID_SUM:
1486 (void) printf(gettext("missing device"));
1489 case VDEV_AUX_NO_REPLICAS:
1490 (void) printf(gettext("insufficient replicas"));
1493 case VDEV_AUX_VERSION_NEWER:
1494 (void) printf(gettext("newer version"));
1497 case VDEV_AUX_UNSUP_FEAT:
1498 (void) printf(gettext("unsupported feature(s)"));
1501 case VDEV_AUX_ERR_EXCEEDED:
1502 (void) printf(gettext("too many errors"));
1506 (void) printf(gettext("corrupted data"));
1510 (void) printf("\n");
1512 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1513 &child, &children) != 0)
1516 for (c = 0; c < children; c++) {
1517 uint64_t is_log = B_FALSE;
1519 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1524 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1525 print_import_config(vname, child[c], namewidth, depth + 2);
1529 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1530 &child, &children) == 0) {
1531 (void) printf(gettext("\tcache\n"));
1532 for (c = 0; c < children; c++) {
1533 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1534 (void) printf("\t %s\n", vname);
1539 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1540 &child, &children) == 0) {
1541 (void) printf(gettext("\tspares\n"));
1542 for (c = 0; c < children; c++) {
1543 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1544 (void) printf("\t %s\n", vname);
1552 * Logs are recorded as top level vdevs in the main pool child array
1553 * but with "is_log" set to 1. We use either print_status_config() or
1554 * print_import_config() to print the top level logs then any log
1555 * children (eg mirrored slogs) are printed recursively - which
1556 * works because only the top level vdev is marked "is_log"
1559 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1564 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1568 (void) printf(gettext("\tlogs\n"));
1570 for (c = 0; c < children; c++) {
1571 uint64_t is_log = B_FALSE;
1574 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1578 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1580 print_status_config(zhp, name, child[c], namewidth,
1583 print_import_config(name, child[c], namewidth, 2);
1589 * Display the status for the given pool.
1592 show_import(nvlist_t *config)
1594 uint64_t pool_state;
1606 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1608 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1610 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1612 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1615 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1616 (uint64_t **)&vs, &vsc) == 0);
1617 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1619 reason = zpool_import_status(config, &msgid);
1621 (void) printf(gettext(" pool: %s\n"), name);
1622 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1623 (void) printf(gettext(" state: %s"), health);
1624 if (pool_state == POOL_STATE_DESTROYED)
1625 (void) printf(gettext(" (DESTROYED)"));
1626 (void) printf("\n");
1629 case ZPOOL_STATUS_MISSING_DEV_R:
1630 case ZPOOL_STATUS_MISSING_DEV_NR:
1631 case ZPOOL_STATUS_BAD_GUID_SUM:
1632 (void) printf(gettext(" status: One or more devices are "
1633 "missing from the system.\n"));
1636 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1637 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1638 (void) printf(gettext(" status: One or more devices contains "
1639 "corrupted data.\n"));
1642 case ZPOOL_STATUS_CORRUPT_DATA:
1644 gettext(" status: The pool data is corrupted.\n"));
1647 case ZPOOL_STATUS_OFFLINE_DEV:
1648 (void) printf(gettext(" status: One or more devices "
1649 "are offlined.\n"));
1652 case ZPOOL_STATUS_CORRUPT_POOL:
1653 (void) printf(gettext(" status: The pool metadata is "
1657 case ZPOOL_STATUS_VERSION_OLDER:
1658 (void) printf(gettext(" status: The pool is formatted using a "
1659 "legacy on-disk version.\n"));
1662 case ZPOOL_STATUS_VERSION_NEWER:
1663 (void) printf(gettext(" status: The pool is formatted using an "
1664 "incompatible version.\n"));
1667 case ZPOOL_STATUS_FEAT_DISABLED:
1668 (void) printf(gettext(" status: Some supported features are "
1669 "not enabled on the pool.\n"));
1672 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1673 (void) printf(gettext("status: The pool uses the following "
1674 "feature(s) not supported on this sytem:\n"));
1675 zpool_print_unsup_feat(config);
1678 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1679 (void) printf(gettext("status: The pool can only be accessed "
1680 "in read-only mode on this system. It\n\tcannot be "
1681 "accessed in read-write mode because it uses the "
1682 "following\n\tfeature(s) not supported on this system:\n"));
1683 zpool_print_unsup_feat(config);
1686 case ZPOOL_STATUS_HOSTID_MISMATCH:
1687 (void) printf(gettext(" status: The pool was last accessed by "
1688 "another system.\n"));
1691 case ZPOOL_STATUS_FAULTED_DEV_R:
1692 case ZPOOL_STATUS_FAULTED_DEV_NR:
1693 (void) printf(gettext(" status: One or more devices are "
1697 case ZPOOL_STATUS_BAD_LOG:
1698 (void) printf(gettext(" status: An intent log record cannot be "
1702 case ZPOOL_STATUS_RESILVERING:
1703 (void) printf(gettext(" status: One or more devices were being "
1707 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
1708 (void) printf(gettext("status: One or more devices were "
1709 "configured to use a non-native block size.\n"
1710 "\tExpect reduced performance.\n"));
1715 * No other status can be seen when importing pools.
1717 assert(reason == ZPOOL_STATUS_OK);
1721 * Print out an action according to the overall state of the pool.
1723 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1724 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1725 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1726 (void) printf(gettext(" action: The pool can be "
1727 "imported using its name or numeric identifier, "
1728 "though\n\tsome features will not be available "
1729 "without an explicit 'zpool upgrade'.\n"));
1730 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1731 (void) printf(gettext(" action: The pool can be "
1732 "imported using its name or numeric "
1733 "identifier and\n\tthe '-f' flag.\n"));
1735 (void) printf(gettext(" action: The pool can be "
1736 "imported using its name or numeric "
1739 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1740 (void) printf(gettext(" action: The pool can be imported "
1741 "despite missing or damaged devices. The\n\tfault "
1742 "tolerance of the pool may be compromised if imported.\n"));
1745 case ZPOOL_STATUS_VERSION_NEWER:
1746 (void) printf(gettext(" action: The pool cannot be "
1747 "imported. Access the pool on a system running "
1748 "newer\n\tsoftware, or recreate the pool from "
1751 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1752 (void) printf(gettext("action: The pool cannot be "
1753 "imported. Access the pool on a system that "
1754 "supports\n\tthe required feature(s), or recreate "
1755 "the pool from backup.\n"));
1757 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1758 (void) printf(gettext("action: The pool cannot be "
1759 "imported in read-write mode. Import the pool "
1761 "\t\"-o readonly=on\", access the pool on a system "
1762 "that supports the\n\trequired feature(s), or "
1763 "recreate the pool from backup.\n"));
1765 case ZPOOL_STATUS_MISSING_DEV_R:
1766 case ZPOOL_STATUS_MISSING_DEV_NR:
1767 case ZPOOL_STATUS_BAD_GUID_SUM:
1768 (void) printf(gettext(" action: The pool cannot be "
1769 "imported. Attach the missing\n\tdevices and try "
1773 (void) printf(gettext(" action: The pool cannot be "
1774 "imported due to damaged devices or data.\n"));
1778 /* Print the comment attached to the pool. */
1779 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1780 (void) printf(gettext("comment: %s\n"), comment);
1783 * If the state is "closed" or "can't open", and the aux state
1784 * is "corrupt data":
1786 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1787 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1788 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1789 if (pool_state == POOL_STATE_DESTROYED)
1790 (void) printf(gettext("\tThe pool was destroyed, "
1791 "but can be imported using the '-Df' flags.\n"));
1792 else if (pool_state != POOL_STATE_EXPORTED)
1793 (void) printf(gettext("\tThe pool may be active on "
1794 "another system, but can be imported using\n\t"
1795 "the '-f' flag.\n"));
1799 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1802 (void) printf(gettext(" config:\n\n"));
1804 namewidth = max_width(NULL, nvroot, 0, 0);
1808 print_import_config(name, nvroot, namewidth, 0);
1809 if (num_logs(nvroot) > 0)
1810 print_logs(NULL, nvroot, namewidth, B_FALSE);
1812 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1813 (void) printf(gettext("\n\tAdditional devices are known to "
1814 "be part of this pool, though their\n\texact "
1815 "configuration cannot be determined.\n"));
1820 * Perform the import for the given configuration. This passes the heavy
1821 * lifting off to zpool_import_props(), and then mounts the datasets contained
1825 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1826 nvlist_t *props, int flags)
1828 zpool_handle_t *zhp;
1833 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1836 verify(nvlist_lookup_uint64(config,
1837 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1838 verify(nvlist_lookup_uint64(config,
1839 ZPOOL_CONFIG_VERSION, &version) == 0);
1840 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1841 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1842 "is formatted using an unsupported ZFS version\n"), name);
1844 } else if (state != POOL_STATE_EXPORTED &&
1845 !(flags & ZFS_IMPORT_ANY_HOST)) {
1848 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1850 if ((unsigned long)hostid != gethostid()) {
1855 verify(nvlist_lookup_string(config,
1856 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1857 verify(nvlist_lookup_uint64(config,
1858 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1860 (void) fprintf(stderr, gettext("cannot import "
1861 "'%s': pool may be in use from other "
1862 "system, it was last accessed by %s "
1863 "(hostid: 0x%lx) on %s"), name, hostname,
1864 (unsigned long)hostid,
1865 asctime(localtime(&t)));
1866 (void) fprintf(stderr, gettext("use '-f' to "
1867 "import anyway\n"));
1871 (void) fprintf(stderr, gettext("cannot import '%s': "
1872 "pool may be in use from other system\n"), name);
1873 (void) fprintf(stderr, gettext("use '-f' to import "
1879 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1882 if (newname != NULL)
1883 name = (char *)newname;
1885 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1888 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1889 !(flags & ZFS_IMPORT_ONLY) &&
1890 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1900 * zpool import [-d dir] [-D]
1901 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1902 * [-d dir | -c cachefile] [-f] -a
1903 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1904 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1906 * -c Read pool information from a cachefile instead of searching
1909 * -d Scan in a specific directory, other than /dev/dsk. More than
1910 * one directory can be specified using multiple '-d' options.
1912 * -D Scan for previously destroyed pools or import all or only
1913 * specified destroyed pools.
1915 * -R Temporarily import the pool, with all mountpoints relative to
1916 * the given root. The pool will remain exported when the machine
1919 * -V Import even in the presence of faulted vdevs. This is an
1920 * intentionally undocumented option for testing purposes, and
1921 * treats the pool configuration as complete, leaving any bad
1922 * vdevs in the FAULTED state. In other words, it does verbatim
1925 * -f Force import, even if it appears that the pool is active.
1927 * -F Attempt rewind if necessary.
1929 * -n See if rewind would work, but don't actually rewind.
1931 * -N Import the pool but don't mount datasets.
1933 * -T Specify a starting txg to use for import. This option is
1934 * intentionally undocumented option for testing purposes.
1936 * -a Import all pools found.
1938 * -o Set property=value and/or temporary mount options (without '=').
1940 * The import command scans for pools to import, and import pools based on pool
1941 * name and GUID. The pool can also be renamed as part of the import process.
1944 zpool_do_import(int argc, char **argv)
1946 char **searchdirs = NULL;
1950 nvlist_t *pools = NULL;
1951 boolean_t do_all = B_FALSE;
1952 boolean_t do_destroyed = B_FALSE;
1953 char *mntopts = NULL;
1956 uint64_t searchguid = 0;
1957 char *searchname = NULL;
1959 nvlist_t *found_config;
1960 nvlist_t *policy = NULL;
1961 nvlist_t *props = NULL;
1963 int flags = ZFS_IMPORT_NORMAL;
1964 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1965 boolean_t dryrun = B_FALSE;
1966 boolean_t do_rewind = B_FALSE;
1967 boolean_t xtreme_rewind = B_FALSE;
1968 uint64_t pool_state, txg = -1ULL;
1969 char *cachefile = NULL;
1970 importargs_t idata = { 0 };
1974 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:T:VX")) != -1) {
1983 if (searchdirs == NULL) {
1984 searchdirs = safe_malloc(sizeof (char *));
1986 char **tmp = safe_malloc((nsearch + 1) *
1988 bcopy(searchdirs, tmp, nsearch *
1993 searchdirs[nsearch++] = optarg;
1996 do_destroyed = B_TRUE;
1999 flags |= ZFS_IMPORT_ANY_HOST;
2005 flags |= ZFS_IMPORT_MISSING_LOG;
2011 flags |= ZFS_IMPORT_ONLY;
2014 if ((propval = strchr(optarg, '=')) != NULL) {
2017 if (add_prop_list(optarg, propval,
2025 if (add_prop_list(zpool_prop_to_name(
2026 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2028 if (nvlist_lookup_string(props,
2029 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2032 if (add_prop_list(zpool_prop_to_name(
2033 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2038 txg = strtoull(optarg, &endptr, 0);
2039 if (errno != 0 || *endptr != '\0') {
2040 (void) fprintf(stderr,
2041 gettext("invalid txg value\n"));
2044 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2047 flags |= ZFS_IMPORT_VERBATIM;
2050 xtreme_rewind = B_TRUE;
2053 (void) fprintf(stderr, gettext("missing argument for "
2054 "'%c' option\n"), optopt);
2058 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2067 if (cachefile && nsearch != 0) {
2068 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2072 if ((dryrun || xtreme_rewind) && !do_rewind) {
2073 (void) fprintf(stderr,
2074 gettext("-n or -X only meaningful with -F\n"));
2078 rewind_policy = ZPOOL_TRY_REWIND;
2080 rewind_policy = ZPOOL_DO_REWIND;
2082 rewind_policy |= ZPOOL_EXTREME_REWIND;
2084 /* In the future, we can capture further policy and include it here */
2085 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2086 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2087 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2090 if (searchdirs == NULL) {
2091 searchdirs = safe_malloc(sizeof (char *));
2092 searchdirs[0] = "/dev";
2096 /* check argument count */
2099 (void) fprintf(stderr, gettext("too many arguments\n"));
2104 (void) fprintf(stderr, gettext("too many arguments\n"));
2109 * Check for the SYS_CONFIG privilege. We do this explicitly
2110 * here because otherwise any attempt to discover pools will
2113 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2114 (void) fprintf(stderr, gettext("cannot "
2115 "discover pools: permission denied\n"));
2117 nvlist_free(policy);
2123 * Depending on the arguments given, we do one of the following:
2125 * <none> Iterate through all pools and display information about
2128 * -a Iterate through all pools and try to import each one.
2130 * <id> Find the pool that corresponds to the given GUID/pool
2131 * name and import that one.
2133 * -D Above options applies only to destroyed pools.
2139 searchguid = strtoull(argv[0], &endptr, 10);
2140 if (errno != 0 || *endptr != '\0') {
2141 searchname = argv[0];
2144 found_config = NULL;
2147 * User specified a name or guid. Ensure it's unique.
2149 idata.unique = B_TRUE;
2153 idata.path = searchdirs;
2154 idata.paths = nsearch;
2155 idata.poolname = searchname;
2156 idata.guid = searchguid;
2157 idata.cachefile = cachefile;
2159 pools = zpool_search_import(g_zfs, &idata);
2161 if (pools != NULL && idata.exists &&
2162 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2163 (void) fprintf(stderr, gettext("cannot import '%s': "
2164 "a pool with that name already exists\n"),
2166 (void) fprintf(stderr, gettext("use the form '%s "
2167 "<pool | id> <newpool>' to give it a new name\n"),
2170 } else if (pools == NULL && idata.exists) {
2171 (void) fprintf(stderr, gettext("cannot import '%s': "
2172 "a pool with that name is already created/imported,\n"),
2174 (void) fprintf(stderr, gettext("and no additional pools "
2175 "with that name were found\n"));
2177 } else if (pools == NULL) {
2179 (void) fprintf(stderr, gettext("cannot import '%s': "
2180 "no such pool available\n"), argv[0]);
2187 nvlist_free(policy);
2192 * At this point we have a list of import candidate configs. Even if
2193 * we were searching by pool name or guid, we still need to
2194 * post-process the list to deal with pool state and possible
2200 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2202 verify(nvpair_value_nvlist(elem, &config) == 0);
2204 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2206 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2208 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2211 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2218 (void) printf("\n");
2221 err |= do_import(config, NULL, mntopts,
2224 show_import(config);
2226 } else if (searchname != NULL) {
2230 * We are searching for a pool based on name.
2232 verify(nvlist_lookup_string(config,
2233 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2235 if (strcmp(name, searchname) == 0) {
2236 if (found_config != NULL) {
2237 (void) fprintf(stderr, gettext(
2238 "cannot import '%s': more than "
2239 "one matching pool\n"), searchname);
2240 (void) fprintf(stderr, gettext(
2241 "import by numeric ID instead\n"));
2244 found_config = config;
2250 * Search for a pool by guid.
2252 verify(nvlist_lookup_uint64(config,
2253 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2255 if (guid == searchguid)
2256 found_config = config;
2261 * If we were searching for a specific pool, verify that we found a
2262 * pool, and then do the import.
2264 if (argc != 0 && err == 0) {
2265 if (found_config == NULL) {
2266 (void) fprintf(stderr, gettext("cannot import '%s': "
2267 "no such pool available\n"), argv[0]);
2270 err |= do_import(found_config, argc == 1 ? NULL :
2271 argv[1], mntopts, props, flags);
2276 * If we were just looking for pools, report an error if none were
2279 if (argc == 0 && first)
2280 (void) fprintf(stderr,
2281 gettext("no pools available to import\n"));
2286 nvlist_free(policy);
2289 return (err ? 1 : 0);
2292 typedef struct iostat_cbdata {
2293 boolean_t cb_verbose;
2296 zpool_list_t *cb_list;
2300 print_iostat_separator(iostat_cbdata_t *cb)
2304 for (i = 0; i < cb->cb_namewidth; i++)
2306 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2310 print_iostat_header(iostat_cbdata_t *cb)
2312 (void) printf("%*s capacity operations bandwidth\n",
2313 cb->cb_namewidth, "");
2314 (void) printf("%-*s alloc free read write read write\n",
2315 cb->cb_namewidth, "pool");
2316 print_iostat_separator(cb);
2320 * Display a single statistic.
2323 print_one_stat(uint64_t value)
2327 zfs_nicenum(value, buf, sizeof (buf));
2328 (void) printf(" %5s", buf);
2332 * Print out all the statistics for the given vdev. This can either be the
2333 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2334 * is a verbose output, and we don't want to display the toplevel pool stats.
2337 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2338 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2340 nvlist_t **oldchild, **newchild;
2342 vdev_stat_t *oldvs, *newvs;
2343 vdev_stat_t zerovs = { 0 };
2348 if (oldnv != NULL) {
2349 verify(nvlist_lookup_uint64_array(oldnv,
2350 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2355 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2356 (uint64_t **)&newvs, &c) == 0);
2358 if (strlen(name) + depth > cb->cb_namewidth)
2359 (void) printf("%*s%s", depth, "", name);
2361 (void) printf("%*s%s%*s", depth, "", name,
2362 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2364 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2369 scale = (double)NANOSEC / tdelta;
2371 /* only toplevel vdevs have capacity stats */
2372 if (newvs->vs_space == 0) {
2373 (void) printf(" - -");
2375 print_one_stat(newvs->vs_alloc);
2376 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2379 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2380 oldvs->vs_ops[ZIO_TYPE_READ])));
2382 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2383 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2385 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2386 oldvs->vs_bytes[ZIO_TYPE_READ])));
2388 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2389 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2391 (void) printf("\n");
2393 if (!cb->cb_verbose)
2396 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2397 &newchild, &children) != 0)
2400 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2401 &oldchild, &c) != 0)
2404 for (c = 0; c < children; c++) {
2405 uint64_t ishole = B_FALSE, islog = B_FALSE;
2407 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2410 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2413 if (ishole || islog)
2416 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2417 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2418 newchild[c], cb, depth + 2);
2423 * Log device section
2426 if (num_logs(newnv) > 0) {
2427 (void) printf("%-*s - - - - - "
2428 "-\n", cb->cb_namewidth, "logs");
2430 for (c = 0; c < children; c++) {
2431 uint64_t islog = B_FALSE;
2432 (void) nvlist_lookup_uint64(newchild[c],
2433 ZPOOL_CONFIG_IS_LOG, &islog);
2436 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2438 print_vdev_stats(zhp, vname, oldnv ?
2439 oldchild[c] : NULL, newchild[c],
2448 * Include level 2 ARC devices in iostat output
2450 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2451 &newchild, &children) != 0)
2454 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2455 &oldchild, &c) != 0)
2459 (void) printf("%-*s - - - - - "
2460 "-\n", cb->cb_namewidth, "cache");
2461 for (c = 0; c < children; c++) {
2462 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2464 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2465 newchild[c], cb, depth + 2);
2472 refresh_iostat(zpool_handle_t *zhp, void *data)
2474 iostat_cbdata_t *cb = data;
2478 * If the pool has disappeared, remove it from the list and continue.
2480 if (zpool_refresh_stats(zhp, &missing) != 0)
2484 pool_list_remove(cb->cb_list, zhp);
2490 * Callback to print out the iostats for the given pool.
2493 print_iostat(zpool_handle_t *zhp, void *data)
2495 iostat_cbdata_t *cb = data;
2496 nvlist_t *oldconfig, *newconfig;
2497 nvlist_t *oldnvroot, *newnvroot;
2499 newconfig = zpool_get_config(zhp, &oldconfig);
2501 if (cb->cb_iteration == 1)
2504 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2507 if (oldconfig == NULL)
2510 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2514 * Print out the statistics for the pool.
2516 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2519 print_iostat_separator(cb);
2525 get_namewidth(zpool_handle_t *zhp, void *data)
2527 iostat_cbdata_t *cb = data;
2528 nvlist_t *config, *nvroot;
2530 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2531 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2533 if (!cb->cb_verbose)
2534 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2536 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2541 * The width must fall into the range [10,38]. The upper limit is the
2542 * maximum we can have and still fit in 80 columns.
2544 if (cb->cb_namewidth < 10)
2545 cb->cb_namewidth = 10;
2546 if (cb->cb_namewidth > 38)
2547 cb->cb_namewidth = 38;
2553 * Parse the input string, get the 'interval' and 'count' value if there is one.
2556 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2559 unsigned long interval = 0, count = 0;
2560 int argc = *argcp, errno;
2563 * Determine if the last argument is an integer or a pool name
2565 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2569 interval = strtoul(argv[argc - 1], &end, 10);
2571 if (*end == '\0' && errno == 0) {
2572 if (interval == 0) {
2573 (void) fprintf(stderr, gettext("interval "
2574 "cannot be zero\n"));
2578 * Ignore the last parameter
2583 * If this is not a valid number, just plow on. The
2584 * user will get a more informative error message later
2592 * If the last argument is also an integer, then we have both a count
2595 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2600 interval = strtoul(argv[argc - 1], &end, 10);
2602 if (*end == '\0' && errno == 0) {
2603 if (interval == 0) {
2604 (void) fprintf(stderr, gettext("interval "
2605 "cannot be zero\n"));
2610 * Ignore the last parameter
2624 get_timestamp_arg(char c)
2627 timestamp_fmt = UDATE;
2629 timestamp_fmt = DDATE;
2635 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2637 * -v Display statistics for individual vdevs
2638 * -T Display a timestamp in date(1) or Unix format
2640 * This command can be tricky because we want to be able to deal with pool
2641 * creation/destruction as well as vdev configuration changes. The bulk of this
2642 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2643 * on pool_list_update() to detect the addition of new pools. Configuration
2644 * changes are all handled within libzfs.
2647 zpool_do_iostat(int argc, char **argv)
2652 unsigned long interval = 0, count = 0;
2654 boolean_t verbose = B_FALSE;
2658 while ((c = getopt(argc, argv, "T:v")) != -1) {
2661 get_timestamp_arg(*optarg);
2667 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2676 get_interval_count(&argc, argv, &interval, &count);
2679 * Construct the list of all interesting pools.
2682 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2685 if (pool_list_count(list) == 0 && argc != 0) {
2686 pool_list_free(list);
2690 if (pool_list_count(list) == 0 && interval == 0) {
2691 pool_list_free(list);
2692 (void) fprintf(stderr, gettext("no pools available\n"));
2697 * Enter the main iostat loop.
2700 cb.cb_verbose = verbose;
2701 cb.cb_iteration = 0;
2702 cb.cb_namewidth = 0;
2705 pool_list_update(list);
2707 if ((npools = pool_list_count(list)) == 0)
2711 * Refresh all statistics. This is done as an explicit step
2712 * before calculating the maximum name width, so that any
2713 * configuration changes are properly accounted for.
2715 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2718 * Iterate over all pools to determine the maximum width
2719 * for the pool / device name column across all pools.
2721 cb.cb_namewidth = 0;
2722 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2724 if (timestamp_fmt != NODATE)
2725 print_timestamp(timestamp_fmt);
2728 * If it's the first time, or verbose mode, print the header.
2730 if (++cb.cb_iteration == 1 || verbose)
2731 print_iostat_header(&cb);
2733 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2736 * If there's more than one pool, and we're not in verbose mode
2737 * (which prints a separator for us), then print a separator.
2739 if (npools > 1 && !verbose)
2740 print_iostat_separator(&cb);
2743 (void) printf("\n");
2746 * Flush the output so that redirection to a file isn't buffered
2749 (void) fflush(stdout);
2754 if (count != 0 && --count == 0)
2757 (void) sleep(interval);
2760 pool_list_free(list);
2765 typedef struct list_cbdata {
2766 boolean_t cb_verbose;
2768 boolean_t cb_scripted;
2769 zprop_list_t *cb_proplist;
2770 boolean_t cb_literal;
2774 * Given a list of columns to display, output appropriate headers for each one.
2777 print_header(list_cbdata_t *cb)
2779 zprop_list_t *pl = cb->cb_proplist;
2780 char headerbuf[ZPOOL_MAXPROPLEN];
2782 boolean_t first = B_TRUE;
2783 boolean_t right_justify;
2786 for (; pl != NULL; pl = pl->pl_next) {
2787 width = pl->pl_width;
2788 if (first && cb->cb_verbose) {
2790 * Reset the width to accommodate the verbose listing
2793 width = cb->cb_namewidth;
2801 right_justify = B_FALSE;
2802 if (pl->pl_prop != ZPROP_INVAL) {
2803 header = zpool_prop_column_name(pl->pl_prop);
2804 right_justify = zpool_prop_align_right(pl->pl_prop);
2808 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2809 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2810 headerbuf[i] = '\0';
2814 if (pl->pl_next == NULL && !right_justify)
2815 (void) printf("%s", header);
2816 else if (right_justify)
2817 (void) printf("%*s", width, header);
2819 (void) printf("%-*s", width, header);
2823 (void) printf("\n");
2827 * Given a pool and a list of properties, print out all the properties according
2828 * to the described layout.
2831 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2833 zprop_list_t *pl = cb->cb_proplist;
2834 boolean_t first = B_TRUE;
2835 char property[ZPOOL_MAXPROPLEN];
2837 boolean_t right_justify;
2840 for (; pl != NULL; pl = pl->pl_next) {
2842 width = pl->pl_width;
2843 if (first && cb->cb_verbose) {
2845 * Reset the width to accommodate the verbose listing
2848 width = cb->cb_namewidth;
2852 if (cb->cb_scripted)
2853 (void) printf("\t");
2860 right_justify = B_FALSE;
2861 if (pl->pl_prop != ZPROP_INVAL) {
2862 if (zpool_get_prop(zhp, pl->pl_prop, property,
2863 sizeof (property), NULL, cb->cb_literal) != 0)
2868 right_justify = zpool_prop_align_right(pl->pl_prop);
2869 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2870 zpool_prop_unsupported(pl->pl_user_prop)) &&
2871 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2872 sizeof (property)) == 0) {
2880 * If this is being called in scripted mode, or if this is the
2881 * last column and it is left-justified, don't include a width
2884 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2885 (void) printf("%s", propstr);
2886 else if (right_justify)
2887 (void) printf("%*s", width, propstr);
2889 (void) printf("%-*s", width, propstr);
2892 (void) printf("\n");
2896 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
2901 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2904 case ZPOOL_PROP_EXPANDSZ:
2906 (void) strlcpy(propval, "-", sizeof (propval));
2908 zfs_nicenum(value, propval, sizeof (propval));
2910 case ZPOOL_PROP_FRAGMENTATION:
2911 if (value == ZFS_FRAG_INVALID) {
2912 (void) strlcpy(propval, "-", sizeof (propval));
2914 (void) snprintf(propval, sizeof (propval), "%llu%%",
2918 case ZPOOL_PROP_CAPACITY:
2919 (void) snprintf(propval, sizeof (propval), "%llu%%", value);
2922 zfs_nicenum(value, propval, sizeof (propval));
2926 (void) strlcpy(propval, "-", sizeof (propval));
2929 (void) printf("\t%s", propval);
2931 (void) printf(" %*s", width, propval);
2935 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2936 list_cbdata_t *cb, int depth)
2942 boolean_t scripted = cb->cb_scripted;
2943 uint64_t islog = B_FALSE;
2944 boolean_t haslog = B_FALSE;
2945 char *dashes = "%-*s - - - - - -\n";
2947 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2948 (uint64_t **)&vs, &c) == 0);
2951 boolean_t toplevel = (vs->vs_space != 0);
2955 (void) printf("\t%s", name);
2956 else if (strlen(name) + depth > cb->cb_namewidth)
2957 (void) printf("%*s%s", depth, "", name);
2959 (void) printf("%*s%s%*s", depth, "", name,
2960 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2963 * Print the properties for the individual vdevs. Some
2964 * properties are only applicable to toplevel vdevs. The
2965 * 'toplevel' boolean value is passed to the print_one_column()
2966 * to indicate that the value is valid.
2968 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
2970 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
2972 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
2973 scripted, toplevel);
2974 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
2976 print_one_column(ZPOOL_PROP_FRAGMENTATION,
2977 vs->vs_fragmentation, scripted,
2978 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
2979 cap = (vs->vs_space == 0) ? 0 :
2980 (vs->vs_alloc * 100 / vs->vs_space);
2981 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
2982 (void) printf("\n");
2985 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2986 &child, &children) != 0)
2989 for (c = 0; c < children; c++) {
2990 uint64_t ishole = B_FALSE;
2992 if (nvlist_lookup_uint64(child[c],
2993 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2996 if (nvlist_lookup_uint64(child[c],
2997 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
3002 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3003 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3007 if (haslog == B_TRUE) {
3008 /* LINTED E_SEC_PRINTF_VAR_FMT */
3009 (void) printf(dashes, cb->cb_namewidth, "log");
3010 for (c = 0; c < children; c++) {
3011 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3012 &islog) != 0 || !islog)
3014 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3015 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3020 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3021 &child, &children) == 0 && children > 0) {
3022 /* LINTED E_SEC_PRINTF_VAR_FMT */
3023 (void) printf(dashes, cb->cb_namewidth, "cache");
3024 for (c = 0; c < children; c++) {
3025 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3026 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3031 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
3032 &children) == 0 && children > 0) {
3033 /* LINTED E_SEC_PRINTF_VAR_FMT */
3034 (void) printf(dashes, cb->cb_namewidth, "spare");
3035 for (c = 0; c < children; c++) {
3036 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3037 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3045 * Generic callback function to list a pool.
3048 list_callback(zpool_handle_t *zhp, void *data)
3050 list_cbdata_t *cbp = data;
3054 config = zpool_get_config(zhp, NULL);
3056 print_pool(zhp, cbp);
3057 if (!cbp->cb_verbose)
3060 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3062 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3068 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3070 * -H Scripted mode. Don't display headers, and separate properties
3072 * -o List of properties to display. Defaults to
3073 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3074 * "dedupratio,health,altroot"
3075 * -p Diplay values in parsable (exact) format.
3076 * -T Display a timestamp in date(1) or Unix format
3078 * List all pools in the system, whether or not they're healthy. Output space
3079 * statistics for each one, as well as health status summary.
3082 zpool_do_list(int argc, char **argv)
3086 list_cbdata_t cb = { 0 };
3087 static char default_props[] =
3088 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3089 "dedupratio,health,altroot";
3090 char *props = default_props;
3091 unsigned long interval = 0, count = 0;
3093 boolean_t first = B_TRUE;
3096 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3099 cb.cb_scripted = B_TRUE;
3105 cb.cb_literal = B_TRUE;
3108 get_timestamp_arg(*optarg);
3111 cb.cb_verbose = B_TRUE;
3114 (void) fprintf(stderr, gettext("missing argument for "
3115 "'%c' option\n"), optopt);
3119 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3128 get_interval_count(&argc, argv, &interval, &count);
3130 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3134 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3138 if (pool_list_count(list) == 0)
3141 cb.cb_namewidth = 0;
3142 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3144 if (timestamp_fmt != NODATE)
3145 print_timestamp(timestamp_fmt);
3147 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3151 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3156 if (count != 0 && --count == 0)
3159 pool_list_free(list);
3160 (void) sleep(interval);
3163 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3164 (void) printf(gettext("no pools available\n"));
3168 pool_list_free(list);
3169 zprop_free_list(cb.cb_proplist);
3174 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3181 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3182 &child, &children) != 0) {
3183 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3184 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3185 name += sizeof(_PATH_DEV) - 1;
3186 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3187 path += sizeof(_PATH_DEV) - 1;
3188 if (strcmp(name, path) == 0)
3193 for (c = 0; c < children; c++)
3194 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3201 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3203 boolean_t force = B_FALSE;
3206 char *poolname, *old_disk, *new_disk;
3207 zpool_handle_t *zhp;
3211 while ((c = getopt(argc, argv, "f")) != -1) {
3217 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3226 /* get pool name and check number of arguments */
3228 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3235 (void) fprintf(stderr,
3236 gettext("missing <device> specification\n"));
3244 (void) fprintf(stderr,
3245 gettext("missing <new_device> specification\n"));
3248 new_disk = old_disk;
3258 (void) fprintf(stderr, gettext("too many arguments\n"));
3262 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3265 if (zpool_get_config(zhp, NULL) == NULL) {
3266 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3272 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3274 if (nvroot == NULL) {
3279 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3281 nvlist_free(nvroot);
3288 * zpool replace [-f] <pool> <device> <new_device>
3290 * -f Force attach, even if <new_device> appears to be in use.
3292 * Replace <device> with <new_device>.
3296 zpool_do_replace(int argc, char **argv)
3298 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3302 * zpool attach [-f] <pool> <device> <new_device>
3304 * -f Force attach, even if <new_device> appears to be in use.
3306 * Attach <new_device> to the mirror containing <device>. If <device> is not
3307 * part of a mirror, then <device> will be transformed into a mirror of
3308 * <device> and <new_device>. In either case, <new_device> will begin life
3309 * with a DTL of [0, now], and will immediately begin to resilver itself.
3312 zpool_do_attach(int argc, char **argv)
3314 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3318 * zpool detach [-f] <pool> <device>
3320 * -f Force detach of <device>, even if DTLs argue against it
3321 * (not supported yet)
3323 * Detach a device from a mirror. The operation will be refused if <device>
3324 * is the last device in the mirror, or if the DTLs indicate that this device
3325 * has the only valid copy of some data.
3329 zpool_do_detach(int argc, char **argv)
3332 char *poolname, *path;
3333 zpool_handle_t *zhp;
3337 while ((c = getopt(argc, argv, "f")) != -1) {
3341 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3350 /* get pool name and check number of arguments */
3352 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3357 (void) fprintf(stderr,
3358 gettext("missing <device> specification\n"));
3365 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3368 ret = zpool_vdev_detach(zhp, path);
3376 * zpool split [-n] [-o prop=val] ...
3378 * [-R altroot] <pool> <newpool> [<device> ...]
3380 * -n Do not split the pool, but display the resulting layout if
3381 * it were to be split.
3382 * -o Set property=value, or set mount options.
3383 * -R Mount the split-off pool under an alternate root.
3385 * Splits the named pool and gives it the new pool name. Devices to be split
3386 * off may be listed, provided that no more than one device is specified
3387 * per top-level vdev mirror. The newly split pool is left in an exported
3388 * state unless -R is specified.
3390 * Restrictions: the top-level of the pool pool must only be made up of
3391 * mirrors; all devices in the pool must be healthy; no device may be
3392 * undergoing a resilvering operation.
3395 zpool_do_split(int argc, char **argv)
3397 char *srcpool, *newpool, *propval;
3398 char *mntopts = NULL;
3401 zpool_handle_t *zhp;
3402 nvlist_t *config, *props = NULL;
3404 flags.dryrun = B_FALSE;
3405 flags.import = B_FALSE;
3408 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3411 flags.import = B_TRUE;
3413 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3414 &props, B_TRUE) != 0) {
3421 flags.dryrun = B_TRUE;
3424 if ((propval = strchr(optarg, '=')) != NULL) {
3427 if (add_prop_list(optarg, propval,
3428 &props, B_TRUE) != 0) {
3438 (void) fprintf(stderr, gettext("missing argument for "
3439 "'%c' option\n"), optopt);
3443 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3450 if (!flags.import && mntopts != NULL) {
3451 (void) fprintf(stderr, gettext("setting mntopts is only "
3452 "valid when importing the pool\n"));
3460 (void) fprintf(stderr, gettext("Missing pool name\n"));
3464 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3474 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3477 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3478 if (config == NULL) {
3482 (void) printf(gettext("would create '%s' with the "
3483 "following layout:\n\n"), newpool);
3484 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3486 nvlist_free(config);
3491 if (ret != 0 || flags.dryrun || !flags.import)
3495 * The split was successful. Now we need to open the new
3496 * pool and import it.
3498 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3500 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3501 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3503 (void) fprintf(stderr, gettext("Split was successful, but "
3504 "the datasets could not all be mounted\n"));
3505 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3506 "different altroot\n"), "zpool import");
3516 * zpool online <pool> <device> ...
3519 zpool_do_online(int argc, char **argv)
3523 zpool_handle_t *zhp;
3525 vdev_state_t newstate;
3529 while ((c = getopt(argc, argv, "et")) != -1) {
3532 flags |= ZFS_ONLINE_EXPAND;
3536 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3545 /* get pool name and check number of arguments */
3547 (void) fprintf(stderr, gettext("missing pool name\n"));
3551 (void) fprintf(stderr, gettext("missing device name\n"));
3557 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3560 for (i = 1; i < argc; i++) {
3561 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3562 if (newstate != VDEV_STATE_HEALTHY) {
3563 (void) printf(gettext("warning: device '%s' "
3564 "onlined, but remains in faulted state\n"),
3566 if (newstate == VDEV_STATE_FAULTED)
3567 (void) printf(gettext("use 'zpool "
3568 "clear' to restore a faulted "
3571 (void) printf(gettext("use 'zpool "
3572 "replace' to replace devices "
3573 "that are no longer present\n"));
3586 * zpool offline [-ft] <pool> <device> ...
3588 * -f Force the device into the offline state, even if doing
3589 * so would appear to compromise pool availability.
3590 * (not supported yet)
3592 * -t Only take the device off-line temporarily. The offline
3593 * state will not be persistent across reboots.
3597 zpool_do_offline(int argc, char **argv)
3601 zpool_handle_t *zhp;
3603 boolean_t istmp = B_FALSE;
3606 while ((c = getopt(argc, argv, "ft")) != -1) {
3613 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3622 /* get pool name and check number of arguments */
3624 (void) fprintf(stderr, gettext("missing pool name\n"));
3628 (void) fprintf(stderr, gettext("missing device name\n"));
3634 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3637 for (i = 1; i < argc; i++) {
3638 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3648 * zpool clear <pool> [device]
3650 * Clear all errors associated with a pool or a particular device.
3653 zpool_do_clear(int argc, char **argv)
3657 boolean_t dryrun = B_FALSE;
3658 boolean_t do_rewind = B_FALSE;
3659 boolean_t xtreme_rewind = B_FALSE;
3660 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3661 nvlist_t *policy = NULL;
3662 zpool_handle_t *zhp;
3663 char *pool, *device;
3666 while ((c = getopt(argc, argv, "FnX")) != -1) {
3675 xtreme_rewind = B_TRUE;
3678 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3688 (void) fprintf(stderr, gettext("missing pool name\n"));
3693 (void) fprintf(stderr, gettext("too many arguments\n"));
3697 if ((dryrun || xtreme_rewind) && !do_rewind) {
3698 (void) fprintf(stderr,
3699 gettext("-n or -X only meaningful with -F\n"));
3703 rewind_policy = ZPOOL_TRY_REWIND;
3705 rewind_policy = ZPOOL_DO_REWIND;
3707 rewind_policy |= ZPOOL_EXTREME_REWIND;
3709 /* In future, further rewind policy choices can be passed along here */
3710 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3711 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3715 device = argc == 2 ? argv[1] : NULL;
3717 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3718 nvlist_free(policy);
3722 if (zpool_clear(zhp, device, policy) != 0)
3727 nvlist_free(policy);
3733 * zpool reguid <pool>
3736 zpool_do_reguid(int argc, char **argv)
3740 zpool_handle_t *zhp;
3744 while ((c = getopt(argc, argv, "")) != -1) {
3747 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3756 /* get pool name and check number of arguments */
3758 (void) fprintf(stderr, gettext("missing pool name\n"));
3763 (void) fprintf(stderr, gettext("too many arguments\n"));
3768 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3771 ret = zpool_reguid(zhp);
3779 * zpool reopen <pool>
3781 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3784 zpool_do_reopen(int argc, char **argv)
3788 zpool_handle_t *zhp;
3792 while ((c = getopt(argc, argv, "")) != -1) {
3795 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3805 (void) fprintf(stderr, gettext("missing pool name\n"));
3810 (void) fprintf(stderr, gettext("too many arguments\n"));
3815 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3818 ret = zpool_reopen(zhp);
3823 typedef struct scrub_cbdata {
3830 scrub_callback(zpool_handle_t *zhp, void *data)
3832 scrub_cbdata_t *cb = data;
3836 * Ignore faulted pools.
3838 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3839 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3840 "currently unavailable\n"), zpool_get_name(zhp));
3844 err = zpool_scan(zhp, cb->cb_type);
3850 * zpool scrub [-s] <pool> ...
3852 * -s Stop. Stops any in-progress scrub.
3855 zpool_do_scrub(int argc, char **argv)
3860 cb.cb_type = POOL_SCAN_SCRUB;
3863 while ((c = getopt(argc, argv, "s")) != -1) {
3866 cb.cb_type = POOL_SCAN_NONE;
3869 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3881 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3885 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3888 typedef struct status_cbdata {
3890 boolean_t cb_allpools;
3891 boolean_t cb_verbose;
3892 boolean_t cb_explain;
3894 boolean_t cb_dedup_stats;
3898 * Print out detailed scrub status.
3901 print_scan_status(pool_scan_stat_t *ps)
3904 uint64_t elapsed, mins_left, hours_left;
3905 uint64_t pass_exam, examined, total;
3907 double fraction_done;
3908 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3910 (void) printf(gettext(" scan: "));
3912 /* If there's never been a scan, there's not much to say. */
3913 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3914 ps->pss_func >= POOL_SCAN_FUNCS) {
3915 (void) printf(gettext("none requested\n"));
3919 start = ps->pss_start_time;
3920 end = ps->pss_end_time;
3921 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3923 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3924 ps->pss_func == POOL_SCAN_RESILVER);
3926 * Scan is finished or canceled.
3928 if (ps->pss_state == DSS_FINISHED) {
3929 uint64_t minutes_taken = (end - start) / 60;
3932 if (ps->pss_func == POOL_SCAN_SCRUB) {
3933 fmt = gettext("scrub repaired %s in %lluh%um with "
3934 "%llu errors on %s");
3935 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3936 fmt = gettext("resilvered %s in %lluh%um with "
3937 "%llu errors on %s");
3940 (void) printf(fmt, processed_buf,
3941 (u_longlong_t)(minutes_taken / 60),
3942 (uint_t)(minutes_taken % 60),
3943 (u_longlong_t)ps->pss_errors,
3944 ctime((time_t *)&end));
3946 } else if (ps->pss_state == DSS_CANCELED) {
3947 if (ps->pss_func == POOL_SCAN_SCRUB) {
3948 (void) printf(gettext("scrub canceled on %s"),
3950 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3951 (void) printf(gettext("resilver canceled on %s"),
3957 assert(ps->pss_state == DSS_SCANNING);
3960 * Scan is in progress.
3962 if (ps->pss_func == POOL_SCAN_SCRUB) {
3963 (void) printf(gettext("scrub in progress since %s"),
3965 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3966 (void) printf(gettext("resilver in progress since %s"),
3970 examined = ps->pss_examined ? ps->pss_examined : 1;
3971 total = ps->pss_to_examine;
3972 fraction_done = (double)examined / total;
3974 /* elapsed time for this pass */
3975 elapsed = time(NULL) - ps->pss_pass_start;
3976 elapsed = elapsed ? elapsed : 1;
3977 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3978 rate = pass_exam / elapsed;
3979 rate = rate ? rate : 1;
3980 mins_left = ((total - examined) / rate) / 60;
3981 hours_left = mins_left / 60;
3983 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3984 zfs_nicenum(total, total_buf, sizeof (total_buf));
3985 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3988 * do not print estimated time if hours_left is more than 30 days
3990 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3991 examined_buf, total_buf, rate_buf);
3992 if (hours_left < (30 * 24)) {
3993 (void) printf(gettext(", %lluh%um to go\n"),
3994 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3996 (void) printf(gettext(
3997 ", (scan is slow, no estimated time)\n"));
4000 if (ps->pss_func == POOL_SCAN_RESILVER) {
4001 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4002 processed_buf, 100 * fraction_done);
4003 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4004 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4005 processed_buf, 100 * fraction_done);
4010 print_error_log(zpool_handle_t *zhp)
4012 nvlist_t *nverrlist = NULL;
4015 size_t len = MAXPATHLEN * 2;
4017 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4018 (void) printf("errors: List of errors unavailable "
4019 "(insufficient privileges)\n");
4023 (void) printf("errors: Permanent errors have been "
4024 "detected in the following files:\n\n");
4026 pathname = safe_malloc(len);
4028 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4030 uint64_t dsobj, obj;
4032 verify(nvpair_value_nvlist(elem, &nv) == 0);
4033 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4035 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4037 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4038 (void) printf("%7s %s\n", "", pathname);
4041 nvlist_free(nverrlist);
4045 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4054 (void) printf(gettext("\tspares\n"));
4056 for (i = 0; i < nspares; i++) {
4057 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4058 print_status_config(zhp, name, spares[i],
4059 namewidth, 2, B_TRUE);
4065 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4074 (void) printf(gettext("\tcache\n"));
4076 for (i = 0; i < nl2cache; i++) {
4077 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4078 print_status_config(zhp, name, l2cache[i],
4079 namewidth, 2, B_FALSE);
4085 print_dedup_stats(nvlist_t *config)
4087 ddt_histogram_t *ddh;
4093 * If the pool was faulted then we may not have been able to
4094 * obtain the config. Otherwise, if we have anything in the dedup
4095 * table continue processing the stats.
4097 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4098 (uint64_t **)&ddo, &c) != 0)
4101 (void) printf("\n");
4102 (void) printf(gettext(" dedup: "));
4103 if (ddo->ddo_count == 0) {
4104 (void) printf(gettext("no DDT entries\n"));
4108 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4109 (u_longlong_t)ddo->ddo_count,
4110 (u_longlong_t)ddo->ddo_dspace,
4111 (u_longlong_t)ddo->ddo_mspace);
4113 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4114 (uint64_t **)&dds, &c) == 0);
4115 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4116 (uint64_t **)&ddh, &c) == 0);
4117 zpool_dump_ddt(dds, ddh);
4121 * Display a summary of pool status. Displays a summary such as:
4125 * reason: One or more devices ...
4126 * see: http://illumos.org/msg/ZFS-xxxx-01
4132 * When given the '-v' option, we print out the complete config. If the '-e'
4133 * option is specified, then we print out error rate information as well.
4136 status_callback(zpool_handle_t *zhp, void *data)
4138 status_cbdata_t *cbp = data;
4139 nvlist_t *config, *nvroot;
4146 config = zpool_get_config(zhp, NULL);
4147 reason = zpool_get_status(zhp, &msgid);
4152 * If we were given 'zpool status -x', only report those pools with
4155 if (cbp->cb_explain &&
4156 (reason == ZPOOL_STATUS_OK ||
4157 reason == ZPOOL_STATUS_VERSION_OLDER ||
4158 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4159 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4160 if (!cbp->cb_allpools) {
4161 (void) printf(gettext("pool '%s' is healthy\n"),
4162 zpool_get_name(zhp));
4164 cbp->cb_first = B_FALSE;
4170 cbp->cb_first = B_FALSE;
4172 (void) printf("\n");
4174 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4176 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4177 (uint64_t **)&vs, &c) == 0);
4178 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4180 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4181 (void) printf(gettext(" state: %s\n"), health);
4184 case ZPOOL_STATUS_MISSING_DEV_R:
4185 (void) printf(gettext("status: One or more devices could not "
4186 "be opened. Sufficient replicas exist for\n\tthe pool to "
4187 "continue functioning in a degraded state.\n"));
4188 (void) printf(gettext("action: Attach the missing device and "
4189 "online it using 'zpool online'.\n"));
4192 case ZPOOL_STATUS_MISSING_DEV_NR:
4193 (void) printf(gettext("status: One or more devices could not "
4194 "be opened. There are insufficient\n\treplicas for the "
4195 "pool to continue functioning.\n"));
4196 (void) printf(gettext("action: Attach the missing device and "
4197 "online it using 'zpool online'.\n"));
4200 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4201 (void) printf(gettext("status: One or more devices could not "
4202 "be used because the label is missing or\n\tinvalid. "
4203 "Sufficient replicas exist for the pool to continue\n\t"
4204 "functioning in a degraded state.\n"));
4205 (void) printf(gettext("action: Replace the device using "
4206 "'zpool replace'.\n"));
4209 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4210 (void) printf(gettext("status: One or more devices could not "
4211 "be used because the label is missing \n\tor invalid. "
4212 "There are insufficient replicas for the pool to "
4213 "continue\n\tfunctioning.\n"));
4214 zpool_explain_recover(zpool_get_handle(zhp),
4215 zpool_get_name(zhp), reason, config);
4218 case ZPOOL_STATUS_FAILING_DEV:
4219 (void) printf(gettext("status: One or more devices has "
4220 "experienced an unrecoverable error. An\n\tattempt was "
4221 "made to correct the error. Applications are "
4223 (void) printf(gettext("action: Determine if the device needs "
4224 "to be replaced, and clear the errors\n\tusing "
4225 "'zpool clear' or replace the device with 'zpool "
4229 case ZPOOL_STATUS_OFFLINE_DEV:
4230 (void) printf(gettext("status: One or more devices has "
4231 "been taken offline by the administrator.\n\tSufficient "
4232 "replicas exist for the pool to continue functioning in "
4233 "a\n\tdegraded state.\n"));
4234 (void) printf(gettext("action: Online the device using "
4235 "'zpool online' or replace the device with\n\t'zpool "
4239 case ZPOOL_STATUS_REMOVED_DEV:
4240 (void) printf(gettext("status: One or more devices has "
4241 "been removed by the administrator.\n\tSufficient "
4242 "replicas exist for the pool to continue functioning in "
4243 "a\n\tdegraded state.\n"));
4244 (void) printf(gettext("action: Online the device using "
4245 "'zpool online' or replace the device with\n\t'zpool "
4249 case ZPOOL_STATUS_RESILVERING:
4250 (void) printf(gettext("status: One or more devices is "
4251 "currently being resilvered. The pool will\n\tcontinue "
4252 "to function, possibly in a degraded state.\n"));
4253 (void) printf(gettext("action: Wait for the resilver to "
4257 case ZPOOL_STATUS_CORRUPT_DATA:
4258 (void) printf(gettext("status: One or more devices has "
4259 "experienced an error resulting in data\n\tcorruption. "
4260 "Applications may be affected.\n"));
4261 (void) printf(gettext("action: Restore the file in question "
4262 "if possible. Otherwise restore the\n\tentire pool from "
4266 case ZPOOL_STATUS_CORRUPT_POOL:
4267 (void) printf(gettext("status: The pool metadata is corrupted "
4268 "and the pool cannot be opened.\n"));
4269 zpool_explain_recover(zpool_get_handle(zhp),
4270 zpool_get_name(zhp), reason, config);
4273 case ZPOOL_STATUS_VERSION_OLDER:
4274 (void) printf(gettext("status: The pool is formatted using a "
4275 "legacy on-disk format. The pool can\n\tstill be used, "
4276 "but some features are unavailable.\n"));
4277 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4278 "upgrade'. Once this is done, the\n\tpool will no longer "
4279 "be accessible on software that does not support feature\n"
4283 case ZPOOL_STATUS_VERSION_NEWER:
4284 (void) printf(gettext("status: The pool has been upgraded to a "
4285 "newer, incompatible on-disk version.\n\tThe pool cannot "
4286 "be accessed on this system.\n"));
4287 (void) printf(gettext("action: Access the pool from a system "
4288 "running more recent software, or\n\trestore the pool from "
4292 case ZPOOL_STATUS_FEAT_DISABLED:
4293 (void) printf(gettext("status: Some supported features are not "
4294 "enabled on the pool. The pool can\n\tstill be used, but "
4295 "some features are unavailable.\n"));
4296 (void) printf(gettext("action: Enable all features using "
4297 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4298 "longer be accessible by software that does not support\n\t"
4299 "the features. See zpool-features(7) for details.\n"));
4302 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4303 (void) printf(gettext("status: The pool cannot be accessed on "
4304 "this system because it uses the\n\tfollowing feature(s) "
4305 "not supported on this system:\n"));
4306 zpool_print_unsup_feat(config);
4307 (void) printf("\n");
4308 (void) printf(gettext("action: Access the pool from a system "
4309 "that supports the required feature(s),\n\tor restore the "
4310 "pool from backup.\n"));
4313 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4314 (void) printf(gettext("status: The pool can only be accessed "
4315 "in read-only mode on this system. It\n\tcannot be "
4316 "accessed in read-write mode because it uses the "
4317 "following\n\tfeature(s) not supported on this system:\n"));
4318 zpool_print_unsup_feat(config);
4319 (void) printf("\n");
4320 (void) printf(gettext("action: The pool cannot be accessed in "
4321 "read-write mode. Import the pool with\n"
4322 "\t\"-o readonly=on\", access the pool from a system that "
4323 "supports the\n\trequired feature(s), or restore the "
4324 "pool from backup.\n"));
4327 case ZPOOL_STATUS_FAULTED_DEV_R:
4328 (void) printf(gettext("status: One or more devices are "
4329 "faulted in response to persistent errors.\n\tSufficient "
4330 "replicas exist for the pool to continue functioning "
4331 "in a\n\tdegraded state.\n"));
4332 (void) printf(gettext("action: Replace the faulted device, "
4333 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4336 case ZPOOL_STATUS_FAULTED_DEV_NR:
4337 (void) printf(gettext("status: One or more devices are "
4338 "faulted in response to persistent errors. There are "
4339 "insufficient replicas for the pool to\n\tcontinue "
4341 (void) printf(gettext("action: Destroy and re-create the pool "
4342 "from a backup source. Manually marking the device\n"
4343 "\trepaired using 'zpool clear' may allow some data "
4344 "to be recovered.\n"));
4347 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4348 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4349 (void) printf(gettext("status: One or more devices are "
4350 "faulted in response to IO failures.\n"));
4351 (void) printf(gettext("action: Make sure the affected devices "
4352 "are connected, then run 'zpool clear'.\n"));
4355 case ZPOOL_STATUS_BAD_LOG:
4356 (void) printf(gettext("status: An intent log record "
4357 "could not be read.\n"
4358 "\tWaiting for adminstrator intervention to fix the "
4359 "faulted pool.\n"));
4360 (void) printf(gettext("action: Either restore the affected "
4361 "device(s) and run 'zpool online',\n"
4362 "\tor ignore the intent log records by running "
4363 "'zpool clear'.\n"));
4366 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4367 (void) printf(gettext("status: One or more devices are "
4368 "configured to use a non-native block size.\n"
4369 "\tExpect reduced performance.\n"));
4370 (void) printf(gettext("action: Replace affected devices with "
4371 "devices that support the\n\tconfigured block size, or "
4372 "migrate data to a properly configured\n\tpool.\n"));
4377 * The remaining errors can't actually be generated, yet.
4379 assert(reason == ZPOOL_STATUS_OK);
4383 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4386 if (config != NULL) {
4389 nvlist_t **spares, **l2cache;
4390 uint_t nspares, nl2cache;
4391 pool_scan_stat_t *ps = NULL;
4393 (void) nvlist_lookup_uint64_array(nvroot,
4394 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4395 print_scan_status(ps);
4397 namewidth = max_width(zhp, nvroot, 0, 0);
4401 (void) printf(gettext("config:\n\n"));
4402 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4403 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4404 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4405 namewidth, 0, B_FALSE);
4407 if (num_logs(nvroot) > 0)
4408 print_logs(zhp, nvroot, namewidth, B_TRUE);
4409 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4410 &l2cache, &nl2cache) == 0)
4411 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4413 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4414 &spares, &nspares) == 0)
4415 print_spares(zhp, spares, nspares, namewidth);
4417 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4419 nvlist_t *nverrlist = NULL;
4422 * If the approximate error count is small, get a
4423 * precise count by fetching the entire log and
4424 * uniquifying the results.
4426 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4427 zpool_get_errlog(zhp, &nverrlist) == 0) {
4432 while ((elem = nvlist_next_nvpair(nverrlist,
4437 nvlist_free(nverrlist);
4439 (void) printf("\n");
4442 (void) printf(gettext("errors: No known data "
4444 else if (!cbp->cb_verbose)
4445 (void) printf(gettext("errors: %llu data "
4446 "errors, use '-v' for a list\n"),
4447 (u_longlong_t)nerr);
4449 print_error_log(zhp);
4452 if (cbp->cb_dedup_stats)
4453 print_dedup_stats(config);
4455 (void) printf(gettext("config: The configuration cannot be "
4463 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4465 * -v Display complete error logs
4466 * -x Display only pools with potential problems
4467 * -D Display dedup status (undocumented)
4468 * -T Display a timestamp in date(1) or Unix format
4470 * Describes the health status of all pools or some subset.
4473 zpool_do_status(int argc, char **argv)
4477 unsigned long interval = 0, count = 0;
4478 status_cbdata_t cb = { 0 };
4481 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4484 cb.cb_verbose = B_TRUE;
4487 cb.cb_explain = B_TRUE;
4490 cb.cb_dedup_stats = B_TRUE;
4493 get_timestamp_arg(*optarg);
4496 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4505 get_interval_count(&argc, argv, &interval, &count);
4508 cb.cb_allpools = B_TRUE;
4510 cb.cb_first = B_TRUE;
4513 if (timestamp_fmt != NODATE)
4514 print_timestamp(timestamp_fmt);
4516 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4517 status_callback, &cb);
4519 if (argc == 0 && cb.cb_count == 0)
4520 (void) printf(gettext("no pools available\n"));
4521 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4522 (void) printf(gettext("all pools are healthy\n"));
4530 if (count != 0 && --count == 0)
4533 (void) sleep(interval);
4539 typedef struct upgrade_cbdata {
4541 boolean_t cb_unavail;
4542 char cb_poolname[ZPOOL_MAXNAMELEN];
4544 uint64_t cb_version;
4550 is_root_pool(zpool_handle_t *zhp)
4552 static struct statfs sfs;
4553 static char *poolname = NULL;
4554 static boolean_t stated = B_FALSE;
4559 if (statfs("/", &sfs) == -1) {
4560 (void) fprintf(stderr,
4561 "Unable to stat root file system: %s.\n",
4565 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4567 poolname = sfs.f_mntfromname;
4568 if ((slash = strchr(poolname, '/')) != NULL)
4571 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4575 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
4578 if (poolname[0] == '\0' && is_root_pool(zhp))
4579 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4581 #endif /* FreeBSD */
4584 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4588 uint64_t oldversion;
4590 config = zpool_get_config(zhp, NULL);
4591 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4594 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4595 assert(oldversion < version);
4597 ret = zpool_upgrade(zhp, version);
4601 if (version >= SPA_VERSION_FEATURES) {
4602 (void) printf(gettext("Successfully upgraded "
4603 "'%s' from version %llu to feature flags.\n"),
4604 zpool_get_name(zhp), oldversion);
4606 (void) printf(gettext("Successfully upgraded "
4607 "'%s' from version %llu to version %llu.\n"),
4608 zpool_get_name(zhp), oldversion, version);
4615 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4618 boolean_t firstff = B_TRUE;
4619 nvlist_t *enabled = zpool_get_features(zhp);
4622 for (i = 0; i < SPA_FEATURES; i++) {
4623 const char *fname = spa_feature_table[i].fi_uname;
4624 const char *fguid = spa_feature_table[i].fi_guid;
4625 if (!nvlist_exists(enabled, fguid)) {
4627 verify(-1 != asprintf(&propname, "feature@%s", fname));
4628 ret = zpool_set_prop(zhp, propname,
4629 ZFS_FEATURE_ENABLED);
4637 (void) printf(gettext("Enabled the "
4638 "following features on '%s':\n"),
4639 zpool_get_name(zhp));
4642 (void) printf(gettext(" %s\n"), fname);
4653 upgrade_cb(zpool_handle_t *zhp, void *arg)
4655 upgrade_cbdata_t *cbp = arg;
4658 boolean_t printnl = B_FALSE;
4661 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4662 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
4663 "currently unavailable.\n\n"), zpool_get_name(zhp));
4664 cbp->cb_unavail = B_TRUE;
4665 /* Allow iteration to continue. */
4669 config = zpool_get_config(zhp, NULL);
4670 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4673 assert(SPA_VERSION_IS_SUPPORTED(version));
4675 if (version < cbp->cb_version) {
4676 cbp->cb_first = B_FALSE;
4677 ret = upgrade_version(zhp, cbp->cb_version);
4681 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4682 sizeof(cbp->cb_poolname));
4683 #endif /* __FreeBSD__ */
4688 * If they did "zpool upgrade -a", then we could
4689 * be doing ioctls to different pools. We need
4690 * to log this history once to each pool, and bypass
4691 * the normal history logging that happens in main().
4693 (void) zpool_log_history(g_zfs, history_str);
4694 log_history = B_FALSE;
4698 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4700 ret = upgrade_enable_all(zhp, &count);
4705 cbp->cb_first = B_FALSE;
4708 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4709 sizeof(cbp->cb_poolname));
4710 #endif /* __FreeBSD__ */
4712 * If they did "zpool upgrade -a", then we could
4713 * be doing ioctls to different pools. We need
4714 * to log this history once to each pool, and bypass
4715 * the normal history logging that happens in main().
4717 (void) zpool_log_history(g_zfs, history_str);
4718 log_history = B_FALSE;
4723 (void) printf(gettext("\n"));
4730 upgrade_list_unavail(zpool_handle_t *zhp, void *arg)
4732 upgrade_cbdata_t *cbp = arg;
4734 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4735 if (cbp->cb_first) {
4736 (void) fprintf(stderr, gettext("The following pools "
4737 "are unavailable and cannot be upgraded as this "
4739 (void) fprintf(stderr, gettext("POOL\n"));
4740 (void) fprintf(stderr, gettext("------------\n"));
4741 cbp->cb_first = B_FALSE;
4743 (void) printf(gettext("%s\n"), zpool_get_name(zhp));
4744 cbp->cb_unavail = B_TRUE;
4750 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4752 upgrade_cbdata_t *cbp = arg;
4756 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4758 * This will have been reported by upgrade_list_unavail so
4759 * just allow iteration to continue.
4761 cbp->cb_unavail = B_TRUE;
4765 config = zpool_get_config(zhp, NULL);
4766 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4769 assert(SPA_VERSION_IS_SUPPORTED(version));
4771 if (version < SPA_VERSION_FEATURES) {
4772 if (cbp->cb_first) {
4773 (void) printf(gettext("The following pools are "
4774 "formatted with legacy version numbers and can\n"
4775 "be upgraded to use feature flags. After "
4776 "being upgraded, these pools\nwill no "
4777 "longer be accessible by software that does not "
4778 "support feature\nflags.\n\n"));
4779 (void) printf(gettext("VER POOL\n"));
4780 (void) printf(gettext("--- ------------\n"));
4781 cbp->cb_first = B_FALSE;
4784 (void) printf("%2llu %s\n", (u_longlong_t)version,
4785 zpool_get_name(zhp));
4792 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4794 upgrade_cbdata_t *cbp = arg;
4798 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4800 * This will have been reported by upgrade_list_unavail so
4801 * just allow iteration to continue.
4803 cbp->cb_unavail = B_TRUE;
4807 config = zpool_get_config(zhp, NULL);
4808 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4811 if (version >= SPA_VERSION_FEATURES) {
4813 boolean_t poolfirst = B_TRUE;
4814 nvlist_t *enabled = zpool_get_features(zhp);
4816 for (i = 0; i < SPA_FEATURES; i++) {
4817 const char *fguid = spa_feature_table[i].fi_guid;
4818 const char *fname = spa_feature_table[i].fi_uname;
4819 if (!nvlist_exists(enabled, fguid)) {
4820 if (cbp->cb_first) {
4821 (void) printf(gettext("\nSome "
4822 "supported features are not "
4823 "enabled on the following pools. "
4824 "Once a\nfeature is enabled the "
4825 "pool may become incompatible with "
4826 "software\nthat does not support "
4828 "zpool-features(7) for "
4830 (void) printf(gettext("POOL "
4832 (void) printf(gettext("------"
4834 cbp->cb_first = B_FALSE;
4838 (void) printf(gettext("%s\n"),
4839 zpool_get_name(zhp));
4840 poolfirst = B_FALSE;
4843 (void) printf(gettext(" %s\n"), fname);
4853 upgrade_one(zpool_handle_t *zhp, void *data)
4855 boolean_t printnl = B_FALSE;
4856 upgrade_cbdata_t *cbp = data;
4857 uint64_t cur_version;
4860 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4861 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
4862 "is currently unavailable.\n\n"), zpool_get_name(zhp));
4863 cbp->cb_unavail = B_TRUE;
4867 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4868 (void) printf(gettext("'log' is now a reserved word\n"
4869 "Pool 'log' must be renamed using export and import"
4870 " to upgrade.\n\n"));
4874 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4875 if (cur_version > cbp->cb_version) {
4876 (void) printf(gettext("Pool '%s' is already formatted "
4877 "using more current version '%llu'.\n\n"),
4878 zpool_get_name(zhp), cur_version);
4882 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4883 (void) printf(gettext("Pool '%s' is already formatted "
4884 "using version %llu.\n\n"), zpool_get_name(zhp),
4889 if (cur_version != cbp->cb_version) {
4891 ret = upgrade_version(zhp, cbp->cb_version);
4895 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4896 sizeof(cbp->cb_poolname));
4897 #endif /* __FreeBSD__ */
4900 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4902 ret = upgrade_enable_all(zhp, &count);
4909 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4910 sizeof(cbp->cb_poolname));
4911 #endif /* __FreeBSD __*/
4912 } else if (cur_version == SPA_VERSION) {
4913 (void) printf(gettext("Pool '%s' already has all "
4914 "supported features enabled.\n\n"),
4915 zpool_get_name(zhp));
4920 (void) printf(gettext("\n"));
4929 * zpool upgrade [-V version] <-a | pool ...>
4931 * With no arguments, display downrev'd ZFS pool available for upgrade.
4932 * Individual pools can be upgraded by specifying the pool, and '-a' will
4933 * upgrade all pools.
4936 zpool_do_upgrade(int argc, char **argv)
4939 upgrade_cbdata_t cb = { 0 };
4941 boolean_t showversions = B_FALSE;
4942 boolean_t upgradeall = B_FALSE;
4947 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4950 upgradeall = B_TRUE;
4953 showversions = B_TRUE;
4956 cb.cb_version = strtoll(optarg, &end, 10);
4958 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4959 (void) fprintf(stderr,
4960 gettext("invalid version '%s'\n"), optarg);
4965 (void) fprintf(stderr, gettext("missing argument for "
4966 "'%c' option\n"), optopt);
4970 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4981 if (cb.cb_version == 0) {
4982 cb.cb_version = SPA_VERSION;
4983 } else if (!upgradeall && argc == 0) {
4984 (void) fprintf(stderr, gettext("-V option is "
4985 "incompatible with other arguments\n"));
4990 if (upgradeall || argc != 0) {
4991 (void) fprintf(stderr, gettext("-v option is "
4992 "incompatible with other arguments\n"));
4995 } else if (upgradeall) {
4997 (void) fprintf(stderr, gettext("-a option should not "
4998 "be used along with a pool name\n"));
5003 (void) printf(gettext("This system supports ZFS pool feature "
5008 (void) printf(gettext("The following features are "
5010 (void) printf(gettext("FEAT DESCRIPTION\n"));
5011 (void) printf("----------------------------------------------"
5012 "---------------\n");
5013 for (i = 0; i < SPA_FEATURES; i++) {
5014 zfeature_info_t *fi = &spa_feature_table[i];
5016 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
5017 " (read-only compatible)" : "";
5019 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5020 (void) printf(" %s\n", fi->fi_desc);
5022 (void) printf("\n");
5024 (void) printf(gettext("The following legacy versions are also "
5026 (void) printf(gettext("VER DESCRIPTION\n"));
5027 (void) printf("--- -----------------------------------------"
5028 "---------------\n");
5029 (void) printf(gettext(" 1 Initial ZFS version\n"));
5030 (void) printf(gettext(" 2 Ditto blocks "
5031 "(replicated metadata)\n"));
5032 (void) printf(gettext(" 3 Hot spares and double parity "
5034 (void) printf(gettext(" 4 zpool history\n"));
5035 (void) printf(gettext(" 5 Compression using the gzip "
5037 (void) printf(gettext(" 6 bootfs pool property\n"));
5038 (void) printf(gettext(" 7 Separate intent log devices\n"));
5039 (void) printf(gettext(" 8 Delegated administration\n"));
5040 (void) printf(gettext(" 9 refquota and refreservation "
5042 (void) printf(gettext(" 10 Cache devices\n"));
5043 (void) printf(gettext(" 11 Improved scrub performance\n"));
5044 (void) printf(gettext(" 12 Snapshot properties\n"));
5045 (void) printf(gettext(" 13 snapused property\n"));
5046 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5047 (void) printf(gettext(" 15 user/group space accounting\n"));
5048 (void) printf(gettext(" 16 stmf property support\n"));
5049 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5050 (void) printf(gettext(" 18 Snapshot user holds\n"));
5051 (void) printf(gettext(" 19 Log device removal\n"));
5052 (void) printf(gettext(" 20 Compression using zle "
5053 "(zero-length encoding)\n"));
5054 (void) printf(gettext(" 21 Deduplication\n"));
5055 (void) printf(gettext(" 22 Received properties\n"));
5056 (void) printf(gettext(" 23 Slim ZIL\n"));
5057 (void) printf(gettext(" 24 System attributes\n"));
5058 (void) printf(gettext(" 25 Improved scrub stats\n"));
5059 (void) printf(gettext(" 26 Improved snapshot deletion "
5061 (void) printf(gettext(" 27 Improved snapshot creation "
5063 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5064 (void) printf(gettext("\nFor more information on a particular "
5065 "version, including supported releases,\n"));
5066 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5067 } else if (argc == 0 && upgradeall) {
5068 cb.cb_first = B_TRUE;
5069 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5070 if (ret == 0 && cb.cb_first) {
5071 if (cb.cb_version == SPA_VERSION) {
5072 (void) printf(gettext("All %spools are already "
5073 "formatted using feature flags.\n\n"),
5074 cb.cb_unavail ? gettext("available ") : "");
5075 (void) printf(gettext("Every %sfeature flags "
5076 "pool already has all supported features "
5078 cb.cb_unavail ? gettext("available ") : "");
5080 (void) printf(gettext("All pools are already "
5081 "formatted with version %llu or higher.\n"),
5085 } else if (argc == 0) {
5086 cb.cb_first = B_TRUE;
5087 ret = zpool_iter(g_zfs, upgrade_list_unavail, &cb);
5091 (void) fprintf(stderr, "\n");
5094 cb.cb_first = B_TRUE;
5095 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5099 (void) printf(gettext("All %spools are formatted using "
5100 "feature flags.\n\n"), cb.cb_unavail ?
5101 gettext("available ") : "");
5103 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5104 "for a list of available legacy versions.\n"));
5107 cb.cb_first = B_TRUE;
5108 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5112 (void) printf(gettext("Every %sfeature flags pool has "
5113 "all supported features enabled.\n"),
5114 cb.cb_unavail ? gettext("available ") : "");
5116 (void) printf(gettext("\n"));
5119 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5123 if (cb.cb_poolname[0] != '\0') {
5125 "If you boot from pool '%s', don't forget to update boot code.\n"
5126 "Assuming you use GPT partitioning and da0 is your boot disk\n"
5127 "the following command will do it:\n"
5129 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5136 typedef struct hist_cbdata {
5143 * Print out the command history for a specific pool.
5146 get_history_one(zpool_handle_t *zhp, void *data)
5152 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5154 cb->first = B_FALSE;
5156 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5158 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5161 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5162 &records, &numrecords) == 0);
5163 for (i = 0; i < numrecords; i++) {
5164 nvlist_t *rec = records[i];
5167 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5171 tsec = fnvlist_lookup_uint64(records[i],
5173 (void) localtime_r(&tsec, &t);
5174 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5177 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5178 (void) printf("%s %s", tbuf,
5179 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5180 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5182 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5185 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5186 (void) printf("%s unrecognized record:\n",
5188 dump_nvlist(rec, 4);
5191 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5192 zfs_history_event_names[ievent],
5193 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5194 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5195 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5198 (void) printf("%s [txg:%lld] %s", tbuf,
5199 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5200 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5201 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5202 (void) printf(" %s (%llu)",
5203 fnvlist_lookup_string(rec,
5205 fnvlist_lookup_uint64(rec,
5208 (void) printf(" %s", fnvlist_lookup_string(rec,
5209 ZPOOL_HIST_INT_STR));
5210 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5213 (void) printf("%s ioctl %s\n", tbuf,
5214 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5215 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5216 (void) printf(" input:\n");
5217 dump_nvlist(fnvlist_lookup_nvlist(rec,
5218 ZPOOL_HIST_INPUT_NVL), 8);
5220 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5221 (void) printf(" output:\n");
5222 dump_nvlist(fnvlist_lookup_nvlist(rec,
5223 ZPOOL_HIST_OUTPUT_NVL), 8);
5228 (void) printf("%s unrecognized record:\n", tbuf);
5229 dump_nvlist(rec, 4);
5233 (void) printf("\n");
5236 (void) printf(" [");
5237 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5238 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5239 struct passwd *pwd = getpwuid(who);
5240 (void) printf("user %d ", (int)who);
5242 (void) printf("(%s) ", pwd->pw_name);
5244 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5245 (void) printf("on %s",
5246 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5248 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5249 (void) printf(":%s",
5250 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5253 (void) printf("\n");
5255 (void) printf("\n");
5262 * zpool history <pool>
5264 * Displays the history of commands that modified pools.
5267 zpool_do_history(int argc, char **argv)
5269 hist_cbdata_t cbdata = { 0 };
5273 cbdata.first = B_TRUE;
5275 while ((c = getopt(argc, argv, "li")) != -1) {
5278 cbdata.longfmt = B_TRUE;
5281 cbdata.internal = B_TRUE;
5284 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5292 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5295 if (argc == 0 && cbdata.first == B_TRUE) {
5296 (void) printf(gettext("no pools available\n"));
5304 get_callback(zpool_handle_t *zhp, void *data)
5306 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5307 char value[MAXNAMELEN];
5308 zprop_source_t srctype;
5311 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5314 * Skip the special fake placeholder. This will also skip
5315 * over the name property when 'all' is specified.
5317 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5318 pl == cbp->cb_proplist)
5321 if (pl->pl_prop == ZPROP_INVAL &&
5322 (zpool_prop_feature(pl->pl_user_prop) ||
5323 zpool_prop_unsupported(pl->pl_user_prop))) {
5324 srctype = ZPROP_SRC_LOCAL;
5326 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5327 value, sizeof (value)) == 0) {
5328 zprop_print_one_property(zpool_get_name(zhp),
5329 cbp, pl->pl_user_prop, value, srctype,
5333 if (zpool_get_prop(zhp, pl->pl_prop, value,
5334 sizeof (value), &srctype, cbp->cb_literal) != 0)
5337 zprop_print_one_property(zpool_get_name(zhp), cbp,
5338 zpool_prop_to_name(pl->pl_prop), value, srctype,
5346 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5348 * -H Scripted mode. Don't display headers, and separate properties
5350 * -o List of columns to display. Defaults to
5351 * "name,property,value,source".
5352 * -p Diplay values in parsable (exact) format.
5354 * Get properties of pools in the system. Output space statistics
5355 * for each one as well as other attributes.
5358 zpool_do_get(int argc, char **argv)
5360 zprop_get_cbdata_t cb = { 0 };
5361 zprop_list_t fake_name = { 0 };
5366 cb.cb_first = B_TRUE;
5369 * Set up default columns and sources.
5371 cb.cb_sources = ZPROP_SRC_ALL;
5372 cb.cb_columns[0] = GET_COL_NAME;
5373 cb.cb_columns[1] = GET_COL_PROPERTY;
5374 cb.cb_columns[2] = GET_COL_VALUE;
5375 cb.cb_columns[3] = GET_COL_SOURCE;
5376 cb.cb_type = ZFS_TYPE_POOL;
5379 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5382 cb.cb_literal = B_TRUE;
5385 cb.cb_scripted = B_TRUE;
5388 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5390 while (*optarg != '\0') {
5391 static char *col_subopts[] =
5392 { "name", "property", "value", "source",
5395 if (i == ZFS_GET_NCOLS) {
5396 (void) fprintf(stderr, gettext("too "
5397 "many fields given to -o "
5402 switch (getsubopt(&optarg, col_subopts,
5405 cb.cb_columns[i++] = GET_COL_NAME;
5408 cb.cb_columns[i++] = GET_COL_PROPERTY;
5411 cb.cb_columns[i++] = GET_COL_VALUE;
5414 cb.cb_columns[i++] = GET_COL_SOURCE;
5418 (void) fprintf(stderr,
5419 gettext("\"all\" conflicts "
5420 "with specific fields "
5421 "given to -o option\n"));
5424 cb.cb_columns[0] = GET_COL_NAME;
5425 cb.cb_columns[1] = GET_COL_PROPERTY;
5426 cb.cb_columns[2] = GET_COL_VALUE;
5427 cb.cb_columns[3] = GET_COL_SOURCE;
5431 (void) fprintf(stderr,
5432 gettext("invalid column name "
5439 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5449 (void) fprintf(stderr, gettext("missing property "
5454 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5455 ZFS_TYPE_POOL) != 0)
5461 if (cb.cb_proplist != NULL) {
5462 fake_name.pl_prop = ZPOOL_PROP_NAME;
5463 fake_name.pl_width = strlen(gettext("NAME"));
5464 fake_name.pl_next = cb.cb_proplist;
5465 cb.cb_proplist = &fake_name;
5468 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5471 if (cb.cb_proplist == &fake_name)
5472 zprop_free_list(fake_name.pl_next);
5474 zprop_free_list(cb.cb_proplist);
5479 typedef struct set_cbdata {
5482 boolean_t cb_any_successful;
5486 set_callback(zpool_handle_t *zhp, void *data)
5489 set_cbdata_t *cb = (set_cbdata_t *)data;
5491 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5494 cb->cb_any_successful = B_TRUE;
5500 zpool_do_set(int argc, char **argv)
5502 set_cbdata_t cb = { 0 };
5505 if (argc > 1 && argv[1][0] == '-') {
5506 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5512 (void) fprintf(stderr, gettext("missing property=value "
5518 (void) fprintf(stderr, gettext("missing pool name\n"));
5523 (void) fprintf(stderr, gettext("too many pool names\n"));
5527 cb.cb_propname = argv[1];
5528 cb.cb_value = strchr(cb.cb_propname, '=');
5529 if (cb.cb_value == NULL) {
5530 (void) fprintf(stderr, gettext("missing value in "
5531 "property=value argument\n"));
5535 *(cb.cb_value) = '\0';
5538 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5545 find_command_idx(char *command, int *idx)
5549 for (i = 0; i < NCOMMAND; i++) {
5550 if (command_table[i].name == NULL)
5553 if (strcmp(command, command_table[i].name) == 0) {
5562 main(int argc, char **argv)
5568 (void) setlocale(LC_ALL, "");
5569 (void) textdomain(TEXT_DOMAIN);
5571 if ((g_zfs = libzfs_init()) == NULL) {
5572 (void) fprintf(stderr, gettext("internal error: failed to "
5573 "initialize ZFS library\n"));
5577 libzfs_print_on_error(g_zfs, B_TRUE);
5582 * Make sure the user has specified some command.
5585 (void) fprintf(stderr, gettext("missing command\n"));
5594 if (strcmp(cmdname, "-?") == 0)
5597 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5600 * Run the appropriate command.
5602 if (find_command_idx(cmdname, &i) == 0) {
5603 current_command = &command_table[i];
5604 ret = command_table[i].func(argc - 1, argv + 1);
5605 } else if (strchr(cmdname, '=')) {
5606 verify(find_command_idx("set", &i) == 0);
5607 current_command = &command_table[i];
5608 ret = command_table[i].func(argc, argv);
5609 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5611 * 'freeze' is a vile debugging abomination, so we treat
5614 zfs_cmd_t zc = { 0 };
5615 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5616 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5618 (void) fprintf(stderr, gettext("unrecognized "
5619 "command '%s'\n"), cmdname);
5623 if (ret == 0 && log_history)
5624 (void) zpool_log_history(g_zfs, history_str);
5629 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5630 * for the purposes of running ::findleaks.
5632 if (getenv("ZFS_ABORT") != NULL) {
5633 (void) printf("dumping core by request\n");