4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
51 #include <sys/fs/zfs.h>
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 #include "statcommon.h"
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
69 static int zpool_do_list(int, char **);
70 static int zpool_do_iostat(int, char **);
71 static int zpool_do_status(int, char **);
73 static int zpool_do_online(int, char **);
74 static int zpool_do_offline(int, char **);
75 static int zpool_do_clear(int, char **);
76 static int zpool_do_reopen(int, char **);
78 static int zpool_do_reguid(int, char **);
80 static int zpool_do_attach(int, char **);
81 static int zpool_do_detach(int, char **);
82 static int zpool_do_replace(int, char **);
83 static int zpool_do_split(int, char **);
85 static int zpool_do_scrub(int, char **);
87 static int zpool_do_import(int, char **);
88 static int zpool_do_export(int, char **);
90 static int zpool_do_upgrade(int, char **);
92 static int zpool_do_history(int, char **);
94 static int zpool_do_get(int, char **);
95 static int zpool_do_set(int, char **);
98 * These libumem hooks provide a reasonable set of defaults for the allocator's
99 * debugging facilities.
104 _umem_debug_init(void)
106 return ("default,verbose"); /* $UMEM_DEBUG setting */
110 _umem_logging_init(void)
112 return ("fail,contents"); /* $UMEM_LOGGING setting */
144 typedef struct zpool_command {
146 int (*func)(int, char **);
151 * Master command table. Each ZFS command has a name, associated function, and
152 * usage message. The usage messages need to be internationalized, so we have
153 * to have a function to return the usage message based on a command index.
155 * These commands are organized according to how they are displayed in the usage
156 * message. An empty command (one with a NULL name) indicates an empty line in
157 * the generic usage message.
159 static zpool_command_t command_table[] = {
160 { "create", zpool_do_create, HELP_CREATE },
161 { "destroy", zpool_do_destroy, HELP_DESTROY },
163 { "add", zpool_do_add, HELP_ADD },
164 { "remove", zpool_do_remove, HELP_REMOVE },
166 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
168 { "list", zpool_do_list, HELP_LIST },
169 { "iostat", zpool_do_iostat, HELP_IOSTAT },
170 { "status", zpool_do_status, HELP_STATUS },
172 { "online", zpool_do_online, HELP_ONLINE },
173 { "offline", zpool_do_offline, HELP_OFFLINE },
174 { "clear", zpool_do_clear, HELP_CLEAR },
175 { "reopen", zpool_do_reopen, HELP_REOPEN },
177 { "attach", zpool_do_attach, HELP_ATTACH },
178 { "detach", zpool_do_detach, HELP_DETACH },
179 { "replace", zpool_do_replace, HELP_REPLACE },
180 { "split", zpool_do_split, HELP_SPLIT },
182 { "scrub", zpool_do_scrub, HELP_SCRUB },
184 { "import", zpool_do_import, HELP_IMPORT },
185 { "export", zpool_do_export, HELP_EXPORT },
186 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
187 { "reguid", zpool_do_reguid, HELP_REGUID },
189 { "history", zpool_do_history, HELP_HISTORY },
190 { "get", zpool_do_get, HELP_GET },
191 { "set", zpool_do_set, HELP_SET },
194 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
196 static zpool_command_t *current_command;
197 static char history_str[HIS_MAX_RECORD_LEN];
198 static boolean_t log_history = B_TRUE;
199 static uint_t timestamp_fmt = NODATE;
202 get_usage(zpool_help_t idx) {
205 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
207 return (gettext("\tattach [-f] <pool> <device> "
210 return (gettext("\tclear [-nF] <pool> [device]\n"));
212 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
213 "\t [-O file-system-property=value] ... \n"
214 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
216 return (gettext("\tdestroy [-f] <pool>\n"));
218 return (gettext("\tdetach <pool> <device>\n"));
220 return (gettext("\texport [-f] <pool> ...\n"));
222 return (gettext("\thistory [-il] [<pool>] ...\n"));
224 return (gettext("\timport [-d dir] [-D]\n"
225 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
226 "\timport [-o mntopts] [-o property=value] ... \n"
227 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
228 "[-R root] [-F [-n]] -a\n"
229 "\timport [-o mntopts] [-o property=value] ... \n"
230 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
231 "[-R root] [-F [-n]]\n"
232 "\t <pool | id> [newpool]\n"));
234 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
236 case HELP_LABELCLEAR:
237 return (gettext("\tlabelclear [-f] <vdev>\n"));
239 return (gettext("\tlist [-Hpv] [-o property[,...]] "
240 "[-T d|u] [pool] ... [interval [count]]\n"));
242 return (gettext("\toffline [-t] <pool> <device> ...\n"));
244 return (gettext("\tonline [-e] <pool> <device> ...\n"));
246 return (gettext("\treplace [-f] <pool> <device> "
249 return (gettext("\tremove <pool> <device> ...\n"));
251 return (gettext("\treopen <pool>\n"));
253 return (gettext("\tscrub [-s] <pool> ...\n"));
255 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
258 return (gettext("\tupgrade [-v]\n"
259 "\tupgrade [-V version] <-a | pool ...>\n"));
261 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
262 "<\"all\" | property[,...]> <pool> ...\n"));
264 return (gettext("\tset <property=value> <pool> \n"));
266 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
267 "\t [-o property=value] <pool> <newpool> "
268 "[<device> ...]\n"));
270 return (gettext("\treguid <pool>\n"));
279 * Callback routine that will print out a pool property value.
282 print_prop_cb(int prop, void *cb)
286 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
288 if (zpool_prop_readonly(prop))
289 (void) fprintf(fp, " NO ");
291 (void) fprintf(fp, " YES ");
293 if (zpool_prop_values(prop) == NULL)
294 (void) fprintf(fp, "-\n");
296 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
302 * Display usage message. If we're inside a command, display only the usage for
303 * that command. Otherwise, iterate over the entire command table and display
304 * a complete usage message.
307 usage(boolean_t requested)
309 FILE *fp = requested ? stdout : stderr;
311 if (current_command == NULL) {
314 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
316 gettext("where 'command' is one of the following:\n\n"));
318 for (i = 0; i < NCOMMAND; i++) {
319 if (command_table[i].name == NULL)
320 (void) fprintf(fp, "\n");
322 (void) fprintf(fp, "%s",
323 get_usage(command_table[i].usage));
326 (void) fprintf(fp, gettext("usage:\n"));
327 (void) fprintf(fp, "%s", get_usage(current_command->usage));
330 if (current_command != NULL &&
331 ((strcmp(current_command->name, "set") == 0) ||
332 (strcmp(current_command->name, "get") == 0) ||
333 (strcmp(current_command->name, "list") == 0))) {
336 gettext("\nthe following properties are supported:\n"));
338 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
339 "PROPERTY", "EDIT", "VALUES");
341 /* Iterate over all properties */
342 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
345 (void) fprintf(fp, "\t%-15s ", "feature@...");
346 (void) fprintf(fp, "YES disabled | enabled | active\n");
348 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
349 "appended with a feature name.\nSee zpool-features(7).\n"));
353 * See comments at end of main().
355 if (getenv("ZFS_ABORT") != NULL) {
356 (void) printf("dumping core by request\n");
360 exit(requested ? 0 : 2);
364 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
365 boolean_t print_logs)
372 (void) printf("\t%*s%s\n", indent, "", name);
374 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
375 &child, &children) != 0)
378 for (c = 0; c < children; c++) {
379 uint64_t is_log = B_FALSE;
381 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
383 if ((is_log && !print_logs) || (!is_log && print_logs))
386 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
387 print_vdev_tree(zhp, vname, child[c], indent + 2,
394 prop_list_contains_feature(nvlist_t *proplist)
397 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
398 nvp = nvlist_next_nvpair(proplist, nvp)) {
399 if (zpool_prop_feature(nvpair_name(nvp)))
406 * Add a property pair (name, string-value) into a property nvlist.
409 add_prop_list(const char *propname, char *propval, nvlist_t **props,
412 zpool_prop_t prop = ZPROP_INVAL;
418 if (*props == NULL &&
419 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
420 (void) fprintf(stderr,
421 gettext("internal error: out of memory\n"));
428 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
430 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
431 !zpool_prop_feature(propname)) {
432 (void) fprintf(stderr, gettext("property '%s' is "
433 "not a valid pool property\n"), propname);
438 * feature@ properties and version should not be specified
441 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
442 nvlist_exists(proplist, vname)) ||
443 (prop == ZPOOL_PROP_VERSION &&
444 prop_list_contains_feature(proplist))) {
445 (void) fprintf(stderr, gettext("'feature@' and "
446 "'version' properties cannot be specified "
452 if (zpool_prop_feature(propname))
455 normnm = zpool_prop_to_name(prop);
457 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
458 normnm = zfs_prop_to_name(fprop);
464 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
465 prop != ZPOOL_PROP_CACHEFILE) {
466 (void) fprintf(stderr, gettext("property '%s' "
467 "specified multiple times\n"), propname);
471 if (nvlist_add_string(proplist, normnm, propval) != 0) {
472 (void) fprintf(stderr, gettext("internal "
473 "error: out of memory\n"));
481 * zpool add [-fn] <pool> <vdev> ...
483 * -f Force addition of devices, even if they appear in use
484 * -n Do not add the devices, but display the resulting layout if
485 * they were to be added.
487 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
488 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
492 zpool_do_add(int argc, char **argv)
494 boolean_t force = B_FALSE;
495 boolean_t dryrun = B_FALSE;
504 while ((c = getopt(argc, argv, "fn")) != -1) {
513 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
522 /* get pool name and check number of arguments */
524 (void) fprintf(stderr, gettext("missing pool name argument\n"));
528 (void) fprintf(stderr, gettext("missing vdev specification\n"));
537 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
540 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
541 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
547 /* pass off to get_vdev_spec for processing */
548 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
550 if (nvroot == NULL) {
556 nvlist_t *poolnvroot;
558 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
561 (void) printf(gettext("would update '%s' to the following "
562 "configuration:\n"), zpool_get_name(zhp));
564 /* print original main pool and new tree */
565 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
566 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
568 /* Do the same for the logs */
569 if (num_logs(poolnvroot) > 0) {
570 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
571 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
572 } else if (num_logs(nvroot) > 0) {
573 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
578 ret = (zpool_add(zhp, nvroot) != 0);
588 * zpool remove <pool> <vdev> ...
590 * Removes the given vdev from the pool. Currently, this supports removing
591 * spares, cache, and log devices from the pool.
594 zpool_do_remove(int argc, char **argv)
603 /* get pool name and check number of arguments */
605 (void) fprintf(stderr, gettext("missing pool name argument\n"));
609 (void) fprintf(stderr, gettext("missing device\n"));
615 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
618 for (i = 1; i < argc; i++) {
619 if (zpool_vdev_remove(zhp, argv[i]) != 0)
627 * zpool labelclear <vdev>
629 * Verifies that the vdev is not active and zeros out the label information
633 zpool_do_labelclear(int argc, char **argv)
636 int c, fd = -1, ret = 0;
638 boolean_t inuse = B_FALSE;
639 boolean_t force = B_FALSE;
642 while ((c = getopt(argc, argv, "f")) != -1) {
648 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
659 (void) fprintf(stderr, gettext("missing vdev device name\n"));
664 if ((fd = open(vdev, O_RDWR)) < 0) {
665 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
670 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
674 (void) fprintf(stderr,
675 gettext("Unable to determine pool state for %s\n"
676 "Use -f to force the clearing any label data\n"), vdev);
684 case POOL_STATE_ACTIVE:
685 case POOL_STATE_SPARE:
686 case POOL_STATE_L2CACHE:
687 (void) fprintf(stderr,
688 gettext("labelclear operation failed.\n"
689 "\tVdev %s is a member (%s), of pool \"%s\".\n"
690 "\tTo remove label information from this device, export or destroy\n"
691 "\tthe pool, or remove %s from the configuration of this pool\n"
692 "\tand retry the labelclear operation\n"),
693 vdev, zpool_pool_state_to_name(state), name, vdev);
697 case POOL_STATE_EXPORTED:
701 (void) fprintf(stderr,
702 gettext("labelclear operation failed.\n"
703 "\tVdev %s is a member of the exported pool \"%s\".\n"
704 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
710 case POOL_STATE_POTENTIALLY_ACTIVE:
714 (void) fprintf(stderr,
715 gettext("labelclear operation failed.\n"
716 "\tVdev %s is a member of the pool \"%s\".\n"
717 "\tThis pool is unknown to this system, but may be active on\n"
718 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
719 "\tremoval of label information.\n"),
724 case POOL_STATE_DESTROYED:
725 /* inuse should never be set for a destoryed pool... */
731 if (zpool_clear_label(fd) != 0) {
732 (void) fprintf(stderr,
733 gettext("Label clear failed on vdev %s\n"), vdev);
746 * zpool create [-fnd] [-o property=value] ...
747 * [-O file-system-property=value] ...
748 * [-R root] [-m mountpoint] <pool> <dev> ...
750 * -f Force creation, even if devices appear in use
751 * -n Do not create the pool, but display the resulting layout if it
752 * were to be created.
753 * -R Create a pool under an alternate root
754 * -m Set default mountpoint for the root dataset. By default it's
756 * -o Set property=value.
757 * -d Don't automatically enable all supported pool features
758 * (individual features can be enabled with -o).
759 * -O Set fsproperty=value in the pool's root file system
761 * Creates the named pool according to the given vdev specification. The
762 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
763 * we get the nvlist back from get_vdev_spec(), we either print out the contents
764 * (if '-n' was specified), or pass it to libzfs to do the creation.
767 zpool_do_create(int argc, char **argv)
769 boolean_t force = B_FALSE;
770 boolean_t dryrun = B_FALSE;
771 boolean_t enable_all_pool_feat = B_TRUE;
773 nvlist_t *nvroot = NULL;
776 char *altroot = NULL;
777 char *mountpoint = NULL;
778 nvlist_t *fsprops = NULL;
779 nvlist_t *props = NULL;
783 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
792 enable_all_pool_feat = B_FALSE;
796 if (add_prop_list(zpool_prop_to_name(
797 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
799 if (nvlist_lookup_string(props,
800 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
803 if (add_prop_list(zpool_prop_to_name(
804 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
808 /* Equivalent to -O mountpoint=optarg */
812 if ((propval = strchr(optarg, '=')) == NULL) {
813 (void) fprintf(stderr, gettext("missing "
814 "'=' for -o option\n"));
820 if (add_prop_list(optarg, propval, &props, B_TRUE))
824 * If the user is creating a pool that doesn't support
825 * feature flags, don't enable any features.
827 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
831 ver = strtoull(propval, &end, 10);
833 ver < SPA_VERSION_FEATURES) {
834 enable_all_pool_feat = B_FALSE;
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;
2944 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2945 (uint64_t **)&vs, &c) == 0);
2948 boolean_t toplevel = (vs->vs_space != 0);
2952 (void) printf("\t%s", name);
2953 else if (strlen(name) + depth > cb->cb_namewidth)
2954 (void) printf("%*s%s", depth, "", name);
2956 (void) printf("%*s%s%*s", depth, "", name,
2957 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2960 * Print the properties for the individual vdevs. Some
2961 * properties are only applicable to toplevel vdevs. The
2962 * 'toplevel' boolean value is passed to the print_one_column()
2963 * to indicate that the value is valid.
2965 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
2967 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
2969 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
2970 scripted, toplevel);
2971 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
2973 print_one_column(ZPOOL_PROP_FRAGMENTATION,
2974 vs->vs_fragmentation, scripted,
2975 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
2976 cap = (vs->vs_space == 0) ? 0 :
2977 (vs->vs_alloc * 100 / vs->vs_space);
2978 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
2979 (void) printf("\n");
2982 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2983 &child, &children) != 0)
2986 for (c = 0; c < children; c++) {
2987 uint64_t ishole = B_FALSE;
2989 if (nvlist_lookup_uint64(child[c],
2990 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2993 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2994 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2999 * Include level 2 ARC devices in iostat output
3001 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3002 &child, &children) != 0)
3006 (void) printf("%-*s - - - - - "
3007 "-\n", cb->cb_namewidth, "cache");
3008 for (c = 0; c < children; c++) {
3009 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3011 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3019 * Generic callback function to list a pool.
3022 list_callback(zpool_handle_t *zhp, void *data)
3024 list_cbdata_t *cbp = data;
3028 config = zpool_get_config(zhp, NULL);
3030 print_pool(zhp, cbp);
3031 if (!cbp->cb_verbose)
3034 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3036 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3042 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3044 * -H Scripted mode. Don't display headers, and separate properties
3046 * -o List of properties to display. Defaults to
3047 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3048 * "dedupratio,health,altroot"
3049 * -p Diplay values in parsable (exact) format.
3050 * -T Display a timestamp in date(1) or Unix format
3052 * List all pools in the system, whether or not they're healthy. Output space
3053 * statistics for each one, as well as health status summary.
3056 zpool_do_list(int argc, char **argv)
3060 list_cbdata_t cb = { 0 };
3061 static char default_props[] =
3062 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3063 "dedupratio,health,altroot";
3064 char *props = default_props;
3065 unsigned long interval = 0, count = 0;
3067 boolean_t first = B_TRUE;
3070 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3073 cb.cb_scripted = B_TRUE;
3079 cb.cb_literal = B_TRUE;
3082 get_timestamp_arg(*optarg);
3085 cb.cb_verbose = B_TRUE;
3088 (void) fprintf(stderr, gettext("missing argument for "
3089 "'%c' option\n"), optopt);
3093 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3102 get_interval_count(&argc, argv, &interval, &count);
3104 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3108 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3112 if (pool_list_count(list) == 0)
3115 cb.cb_namewidth = 0;
3116 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3118 if (timestamp_fmt != NODATE)
3119 print_timestamp(timestamp_fmt);
3121 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3125 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3130 if (count != 0 && --count == 0)
3133 pool_list_free(list);
3134 (void) sleep(interval);
3137 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3138 (void) printf(gettext("no pools available\n"));
3142 pool_list_free(list);
3143 zprop_free_list(cb.cb_proplist);
3148 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3155 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3156 &child, &children) != 0) {
3157 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3158 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3159 name += sizeof(_PATH_DEV) - 1;
3160 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3161 path += sizeof(_PATH_DEV) - 1;
3162 if (strcmp(name, path) == 0)
3167 for (c = 0; c < children; c++)
3168 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3175 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3177 boolean_t force = B_FALSE;
3180 char *poolname, *old_disk, *new_disk;
3181 zpool_handle_t *zhp;
3185 while ((c = getopt(argc, argv, "f")) != -1) {
3191 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3200 /* get pool name and check number of arguments */
3202 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3209 (void) fprintf(stderr,
3210 gettext("missing <device> specification\n"));
3218 (void) fprintf(stderr,
3219 gettext("missing <new_device> specification\n"));
3222 new_disk = old_disk;
3232 (void) fprintf(stderr, gettext("too many arguments\n"));
3236 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3239 if (zpool_get_config(zhp, NULL) == NULL) {
3240 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3246 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3248 if (nvroot == NULL) {
3253 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3255 nvlist_free(nvroot);
3262 * zpool replace [-f] <pool> <device> <new_device>
3264 * -f Force attach, even if <new_device> appears to be in use.
3266 * Replace <device> with <new_device>.
3270 zpool_do_replace(int argc, char **argv)
3272 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3276 * zpool attach [-f] <pool> <device> <new_device>
3278 * -f Force attach, even if <new_device> appears to be in use.
3280 * Attach <new_device> to the mirror containing <device>. If <device> is not
3281 * part of a mirror, then <device> will be transformed into a mirror of
3282 * <device> and <new_device>. In either case, <new_device> will begin life
3283 * with a DTL of [0, now], and will immediately begin to resilver itself.
3286 zpool_do_attach(int argc, char **argv)
3288 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3292 * zpool detach [-f] <pool> <device>
3294 * -f Force detach of <device>, even if DTLs argue against it
3295 * (not supported yet)
3297 * Detach a device from a mirror. The operation will be refused if <device>
3298 * is the last device in the mirror, or if the DTLs indicate that this device
3299 * has the only valid copy of some data.
3303 zpool_do_detach(int argc, char **argv)
3306 char *poolname, *path;
3307 zpool_handle_t *zhp;
3311 while ((c = getopt(argc, argv, "f")) != -1) {
3315 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3324 /* get pool name and check number of arguments */
3326 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3331 (void) fprintf(stderr,
3332 gettext("missing <device> specification\n"));
3339 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3342 ret = zpool_vdev_detach(zhp, path);
3350 * zpool split [-n] [-o prop=val] ...
3352 * [-R altroot] <pool> <newpool> [<device> ...]
3354 * -n Do not split the pool, but display the resulting layout if
3355 * it were to be split.
3356 * -o Set property=value, or set mount options.
3357 * -R Mount the split-off pool under an alternate root.
3359 * Splits the named pool and gives it the new pool name. Devices to be split
3360 * off may be listed, provided that no more than one device is specified
3361 * per top-level vdev mirror. The newly split pool is left in an exported
3362 * state unless -R is specified.
3364 * Restrictions: the top-level of the pool pool must only be made up of
3365 * mirrors; all devices in the pool must be healthy; no device may be
3366 * undergoing a resilvering operation.
3369 zpool_do_split(int argc, char **argv)
3371 char *srcpool, *newpool, *propval;
3372 char *mntopts = NULL;
3375 zpool_handle_t *zhp;
3376 nvlist_t *config, *props = NULL;
3378 flags.dryrun = B_FALSE;
3379 flags.import = B_FALSE;
3382 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3385 flags.import = B_TRUE;
3387 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3388 &props, B_TRUE) != 0) {
3395 flags.dryrun = B_TRUE;
3398 if ((propval = strchr(optarg, '=')) != NULL) {
3401 if (add_prop_list(optarg, propval,
3402 &props, B_TRUE) != 0) {
3412 (void) fprintf(stderr, gettext("missing argument for "
3413 "'%c' option\n"), optopt);
3417 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3424 if (!flags.import && mntopts != NULL) {
3425 (void) fprintf(stderr, gettext("setting mntopts is only "
3426 "valid when importing the pool\n"));
3434 (void) fprintf(stderr, gettext("Missing pool name\n"));
3438 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3448 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3451 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3452 if (config == NULL) {
3456 (void) printf(gettext("would create '%s' with the "
3457 "following layout:\n\n"), newpool);
3458 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3460 nvlist_free(config);
3465 if (ret != 0 || flags.dryrun || !flags.import)
3469 * The split was successful. Now we need to open the new
3470 * pool and import it.
3472 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3474 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3475 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3477 (void) fprintf(stderr, gettext("Split was successful, but "
3478 "the datasets could not all be mounted\n"));
3479 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3480 "different altroot\n"), "zpool import");
3490 * zpool online <pool> <device> ...
3493 zpool_do_online(int argc, char **argv)
3497 zpool_handle_t *zhp;
3499 vdev_state_t newstate;
3503 while ((c = getopt(argc, argv, "et")) != -1) {
3506 flags |= ZFS_ONLINE_EXPAND;
3510 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3519 /* get pool name and check number of arguments */
3521 (void) fprintf(stderr, gettext("missing pool name\n"));
3525 (void) fprintf(stderr, gettext("missing device name\n"));
3531 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3534 for (i = 1; i < argc; i++) {
3535 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3536 if (newstate != VDEV_STATE_HEALTHY) {
3537 (void) printf(gettext("warning: device '%s' "
3538 "onlined, but remains in faulted state\n"),
3540 if (newstate == VDEV_STATE_FAULTED)
3541 (void) printf(gettext("use 'zpool "
3542 "clear' to restore a faulted "
3545 (void) printf(gettext("use 'zpool "
3546 "replace' to replace devices "
3547 "that are no longer present\n"));
3560 * zpool offline [-ft] <pool> <device> ...
3562 * -f Force the device into the offline state, even if doing
3563 * so would appear to compromise pool availability.
3564 * (not supported yet)
3566 * -t Only take the device off-line temporarily. The offline
3567 * state will not be persistent across reboots.
3571 zpool_do_offline(int argc, char **argv)
3575 zpool_handle_t *zhp;
3577 boolean_t istmp = B_FALSE;
3580 while ((c = getopt(argc, argv, "ft")) != -1) {
3587 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3596 /* get pool name and check number of arguments */
3598 (void) fprintf(stderr, gettext("missing pool name\n"));
3602 (void) fprintf(stderr, gettext("missing device name\n"));
3608 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3611 for (i = 1; i < argc; i++) {
3612 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3622 * zpool clear <pool> [device]
3624 * Clear all errors associated with a pool or a particular device.
3627 zpool_do_clear(int argc, char **argv)
3631 boolean_t dryrun = B_FALSE;
3632 boolean_t do_rewind = B_FALSE;
3633 boolean_t xtreme_rewind = B_FALSE;
3634 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3635 nvlist_t *policy = NULL;
3636 zpool_handle_t *zhp;
3637 char *pool, *device;
3640 while ((c = getopt(argc, argv, "FnX")) != -1) {
3649 xtreme_rewind = B_TRUE;
3652 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3662 (void) fprintf(stderr, gettext("missing pool name\n"));
3667 (void) fprintf(stderr, gettext("too many arguments\n"));
3671 if ((dryrun || xtreme_rewind) && !do_rewind) {
3672 (void) fprintf(stderr,
3673 gettext("-n or -X only meaningful with -F\n"));
3677 rewind_policy = ZPOOL_TRY_REWIND;
3679 rewind_policy = ZPOOL_DO_REWIND;
3681 rewind_policy |= ZPOOL_EXTREME_REWIND;
3683 /* In future, further rewind policy choices can be passed along here */
3684 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3685 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3689 device = argc == 2 ? argv[1] : NULL;
3691 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3692 nvlist_free(policy);
3696 if (zpool_clear(zhp, device, policy) != 0)
3701 nvlist_free(policy);
3707 * zpool reguid <pool>
3710 zpool_do_reguid(int argc, char **argv)
3714 zpool_handle_t *zhp;
3718 while ((c = getopt(argc, argv, "")) != -1) {
3721 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3730 /* get pool name and check number of arguments */
3732 (void) fprintf(stderr, gettext("missing pool name\n"));
3737 (void) fprintf(stderr, gettext("too many arguments\n"));
3742 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3745 ret = zpool_reguid(zhp);
3753 * zpool reopen <pool>
3755 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3758 zpool_do_reopen(int argc, char **argv)
3762 zpool_handle_t *zhp;
3766 while ((c = getopt(argc, argv, "")) != -1) {
3769 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3779 (void) fprintf(stderr, gettext("missing pool name\n"));
3784 (void) fprintf(stderr, gettext("too many arguments\n"));
3789 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3792 ret = zpool_reopen(zhp);
3797 typedef struct scrub_cbdata {
3804 scrub_callback(zpool_handle_t *zhp, void *data)
3806 scrub_cbdata_t *cb = data;
3810 * Ignore faulted pools.
3812 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3813 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3814 "currently unavailable\n"), zpool_get_name(zhp));
3818 err = zpool_scan(zhp, cb->cb_type);
3824 * zpool scrub [-s] <pool> ...
3826 * -s Stop. Stops any in-progress scrub.
3829 zpool_do_scrub(int argc, char **argv)
3834 cb.cb_type = POOL_SCAN_SCRUB;
3837 while ((c = getopt(argc, argv, "s")) != -1) {
3840 cb.cb_type = POOL_SCAN_NONE;
3843 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3855 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3859 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3862 typedef struct status_cbdata {
3864 boolean_t cb_allpools;
3865 boolean_t cb_verbose;
3866 boolean_t cb_explain;
3868 boolean_t cb_dedup_stats;
3872 * Print out detailed scrub status.
3875 print_scan_status(pool_scan_stat_t *ps)
3878 uint64_t elapsed, mins_left, hours_left;
3879 uint64_t pass_exam, examined, total;
3881 double fraction_done;
3882 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3884 (void) printf(gettext(" scan: "));
3886 /* If there's never been a scan, there's not much to say. */
3887 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3888 ps->pss_func >= POOL_SCAN_FUNCS) {
3889 (void) printf(gettext("none requested\n"));
3893 start = ps->pss_start_time;
3894 end = ps->pss_end_time;
3895 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3897 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3898 ps->pss_func == POOL_SCAN_RESILVER);
3900 * Scan is finished or canceled.
3902 if (ps->pss_state == DSS_FINISHED) {
3903 uint64_t minutes_taken = (end - start) / 60;
3906 if (ps->pss_func == POOL_SCAN_SCRUB) {
3907 fmt = gettext("scrub repaired %s in %lluh%um with "
3908 "%llu errors on %s");
3909 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3910 fmt = gettext("resilvered %s in %lluh%um with "
3911 "%llu errors on %s");
3914 (void) printf(fmt, processed_buf,
3915 (u_longlong_t)(minutes_taken / 60),
3916 (uint_t)(minutes_taken % 60),
3917 (u_longlong_t)ps->pss_errors,
3918 ctime((time_t *)&end));
3920 } else if (ps->pss_state == DSS_CANCELED) {
3921 if (ps->pss_func == POOL_SCAN_SCRUB) {
3922 (void) printf(gettext("scrub canceled on %s"),
3924 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3925 (void) printf(gettext("resilver canceled on %s"),
3931 assert(ps->pss_state == DSS_SCANNING);
3934 * Scan is in progress.
3936 if (ps->pss_func == POOL_SCAN_SCRUB) {
3937 (void) printf(gettext("scrub in progress since %s"),
3939 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3940 (void) printf(gettext("resilver in progress since %s"),
3944 examined = ps->pss_examined ? ps->pss_examined : 1;
3945 total = ps->pss_to_examine;
3946 fraction_done = (double)examined / total;
3948 /* elapsed time for this pass */
3949 elapsed = time(NULL) - ps->pss_pass_start;
3950 elapsed = elapsed ? elapsed : 1;
3951 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3952 rate = pass_exam / elapsed;
3953 rate = rate ? rate : 1;
3954 mins_left = ((total - examined) / rate) / 60;
3955 hours_left = mins_left / 60;
3957 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3958 zfs_nicenum(total, total_buf, sizeof (total_buf));
3959 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3962 * do not print estimated time if hours_left is more than 30 days
3964 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3965 examined_buf, total_buf, rate_buf);
3966 if (hours_left < (30 * 24)) {
3967 (void) printf(gettext(", %lluh%um to go\n"),
3968 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3970 (void) printf(gettext(
3971 ", (scan is slow, no estimated time)\n"));
3974 if (ps->pss_func == POOL_SCAN_RESILVER) {
3975 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3976 processed_buf, 100 * fraction_done);
3977 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3978 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3979 processed_buf, 100 * fraction_done);
3984 print_error_log(zpool_handle_t *zhp)
3986 nvlist_t *nverrlist = NULL;
3989 size_t len = MAXPATHLEN * 2;
3991 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3992 (void) printf("errors: List of errors unavailable "
3993 "(insufficient privileges)\n");
3997 (void) printf("errors: Permanent errors have been "
3998 "detected in the following files:\n\n");
4000 pathname = safe_malloc(len);
4002 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4004 uint64_t dsobj, obj;
4006 verify(nvpair_value_nvlist(elem, &nv) == 0);
4007 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4009 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4011 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4012 (void) printf("%7s %s\n", "", pathname);
4015 nvlist_free(nverrlist);
4019 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4028 (void) printf(gettext("\tspares\n"));
4030 for (i = 0; i < nspares; i++) {
4031 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4032 print_status_config(zhp, name, spares[i],
4033 namewidth, 2, B_TRUE);
4039 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4048 (void) printf(gettext("\tcache\n"));
4050 for (i = 0; i < nl2cache; i++) {
4051 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4052 print_status_config(zhp, name, l2cache[i],
4053 namewidth, 2, B_FALSE);
4059 print_dedup_stats(nvlist_t *config)
4061 ddt_histogram_t *ddh;
4067 * If the pool was faulted then we may not have been able to
4068 * obtain the config. Otherwise, if we have anything in the dedup
4069 * table continue processing the stats.
4071 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4072 (uint64_t **)&ddo, &c) != 0)
4075 (void) printf("\n");
4076 (void) printf(gettext(" dedup: "));
4077 if (ddo->ddo_count == 0) {
4078 (void) printf(gettext("no DDT entries\n"));
4082 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4083 (u_longlong_t)ddo->ddo_count,
4084 (u_longlong_t)ddo->ddo_dspace,
4085 (u_longlong_t)ddo->ddo_mspace);
4087 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4088 (uint64_t **)&dds, &c) == 0);
4089 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4090 (uint64_t **)&ddh, &c) == 0);
4091 zpool_dump_ddt(dds, ddh);
4095 * Display a summary of pool status. Displays a summary such as:
4099 * reason: One or more devices ...
4100 * see: http://illumos.org/msg/ZFS-xxxx-01
4106 * When given the '-v' option, we print out the complete config. If the '-e'
4107 * option is specified, then we print out error rate information as well.
4110 status_callback(zpool_handle_t *zhp, void *data)
4112 status_cbdata_t *cbp = data;
4113 nvlist_t *config, *nvroot;
4120 config = zpool_get_config(zhp, NULL);
4121 reason = zpool_get_status(zhp, &msgid);
4126 * If we were given 'zpool status -x', only report those pools with
4129 if (cbp->cb_explain &&
4130 (reason == ZPOOL_STATUS_OK ||
4131 reason == ZPOOL_STATUS_VERSION_OLDER ||
4132 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4133 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4134 if (!cbp->cb_allpools) {
4135 (void) printf(gettext("pool '%s' is healthy\n"),
4136 zpool_get_name(zhp));
4138 cbp->cb_first = B_FALSE;
4144 cbp->cb_first = B_FALSE;
4146 (void) printf("\n");
4148 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4150 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4151 (uint64_t **)&vs, &c) == 0);
4152 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4154 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4155 (void) printf(gettext(" state: %s\n"), health);
4158 case ZPOOL_STATUS_MISSING_DEV_R:
4159 (void) printf(gettext("status: One or more devices could not "
4160 "be opened. Sufficient replicas exist for\n\tthe pool to "
4161 "continue functioning in a degraded state.\n"));
4162 (void) printf(gettext("action: Attach the missing device and "
4163 "online it using 'zpool online'.\n"));
4166 case ZPOOL_STATUS_MISSING_DEV_NR:
4167 (void) printf(gettext("status: One or more devices could not "
4168 "be opened. There are insufficient\n\treplicas for the "
4169 "pool to continue functioning.\n"));
4170 (void) printf(gettext("action: Attach the missing device and "
4171 "online it using 'zpool online'.\n"));
4174 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4175 (void) printf(gettext("status: One or more devices could not "
4176 "be used because the label is missing or\n\tinvalid. "
4177 "Sufficient replicas exist for the pool to continue\n\t"
4178 "functioning in a degraded state.\n"));
4179 (void) printf(gettext("action: Replace the device using "
4180 "'zpool replace'.\n"));
4183 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4184 (void) printf(gettext("status: One or more devices could not "
4185 "be used because the label is missing \n\tor invalid. "
4186 "There are insufficient replicas for the pool to "
4187 "continue\n\tfunctioning.\n"));
4188 zpool_explain_recover(zpool_get_handle(zhp),
4189 zpool_get_name(zhp), reason, config);
4192 case ZPOOL_STATUS_FAILING_DEV:
4193 (void) printf(gettext("status: One or more devices has "
4194 "experienced an unrecoverable error. An\n\tattempt was "
4195 "made to correct the error. Applications are "
4197 (void) printf(gettext("action: Determine if the device needs "
4198 "to be replaced, and clear the errors\n\tusing "
4199 "'zpool clear' or replace the device with 'zpool "
4203 case ZPOOL_STATUS_OFFLINE_DEV:
4204 (void) printf(gettext("status: One or more devices has "
4205 "been taken offline by the administrator.\n\tSufficient "
4206 "replicas exist for the pool to continue functioning in "
4207 "a\n\tdegraded state.\n"));
4208 (void) printf(gettext("action: Online the device using "
4209 "'zpool online' or replace the device with\n\t'zpool "
4213 case ZPOOL_STATUS_REMOVED_DEV:
4214 (void) printf(gettext("status: One or more devices has "
4215 "been removed by the administrator.\n\tSufficient "
4216 "replicas exist for the pool to continue functioning in "
4217 "a\n\tdegraded state.\n"));
4218 (void) printf(gettext("action: Online the device using "
4219 "'zpool online' or replace the device with\n\t'zpool "
4223 case ZPOOL_STATUS_RESILVERING:
4224 (void) printf(gettext("status: One or more devices is "
4225 "currently being resilvered. The pool will\n\tcontinue "
4226 "to function, possibly in a degraded state.\n"));
4227 (void) printf(gettext("action: Wait for the resilver to "
4231 case ZPOOL_STATUS_CORRUPT_DATA:
4232 (void) printf(gettext("status: One or more devices has "
4233 "experienced an error resulting in data\n\tcorruption. "
4234 "Applications may be affected.\n"));
4235 (void) printf(gettext("action: Restore the file in question "
4236 "if possible. Otherwise restore the\n\tentire pool from "
4240 case ZPOOL_STATUS_CORRUPT_POOL:
4241 (void) printf(gettext("status: The pool metadata is corrupted "
4242 "and the pool cannot be opened.\n"));
4243 zpool_explain_recover(zpool_get_handle(zhp),
4244 zpool_get_name(zhp), reason, config);
4247 case ZPOOL_STATUS_VERSION_OLDER:
4248 (void) printf(gettext("status: The pool is formatted using a "
4249 "legacy on-disk format. The pool can\n\tstill be used, "
4250 "but some features are unavailable.\n"));
4251 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4252 "upgrade'. Once this is done, the\n\tpool will no longer "
4253 "be accessible on software that does not support feature\n"
4257 case ZPOOL_STATUS_VERSION_NEWER:
4258 (void) printf(gettext("status: The pool has been upgraded to a "
4259 "newer, incompatible on-disk version.\n\tThe pool cannot "
4260 "be accessed on this system.\n"));
4261 (void) printf(gettext("action: Access the pool from a system "
4262 "running more recent software, or\n\trestore the pool from "
4266 case ZPOOL_STATUS_FEAT_DISABLED:
4267 (void) printf(gettext("status: Some supported features are not "
4268 "enabled on the pool. The pool can\n\tstill be used, but "
4269 "some features are unavailable.\n"));
4270 (void) printf(gettext("action: Enable all features using "
4271 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4272 "longer be accessible by software that does not support\n\t"
4273 "the features. See zpool-features(7) for details.\n"));
4276 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4277 (void) printf(gettext("status: The pool cannot be accessed on "
4278 "this system because it uses the\n\tfollowing feature(s) "
4279 "not supported on this system:\n"));
4280 zpool_print_unsup_feat(config);
4281 (void) printf("\n");
4282 (void) printf(gettext("action: Access the pool from a system "
4283 "that supports the required feature(s),\n\tor restore the "
4284 "pool from backup.\n"));
4287 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4288 (void) printf(gettext("status: The pool can only be accessed "
4289 "in read-only mode on this system. It\n\tcannot be "
4290 "accessed in read-write mode because it uses the "
4291 "following\n\tfeature(s) not supported on this system:\n"));
4292 zpool_print_unsup_feat(config);
4293 (void) printf("\n");
4294 (void) printf(gettext("action: The pool cannot be accessed in "
4295 "read-write mode. Import the pool with\n"
4296 "\t\"-o readonly=on\", access the pool from a system that "
4297 "supports the\n\trequired feature(s), or restore the "
4298 "pool from backup.\n"));
4301 case ZPOOL_STATUS_FAULTED_DEV_R:
4302 (void) printf(gettext("status: One or more devices are "
4303 "faulted in response to persistent errors.\n\tSufficient "
4304 "replicas exist for the pool to continue functioning "
4305 "in a\n\tdegraded state.\n"));
4306 (void) printf(gettext("action: Replace the faulted device, "
4307 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4310 case ZPOOL_STATUS_FAULTED_DEV_NR:
4311 (void) printf(gettext("status: One or more devices are "
4312 "faulted in response to persistent errors. There are "
4313 "insufficient replicas for the pool to\n\tcontinue "
4315 (void) printf(gettext("action: Destroy and re-create the pool "
4316 "from a backup source. Manually marking the device\n"
4317 "\trepaired using 'zpool clear' may allow some data "
4318 "to be recovered.\n"));
4321 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4322 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4323 (void) printf(gettext("status: One or more devices are "
4324 "faulted in response to IO failures.\n"));
4325 (void) printf(gettext("action: Make sure the affected devices "
4326 "are connected, then run 'zpool clear'.\n"));
4329 case ZPOOL_STATUS_BAD_LOG:
4330 (void) printf(gettext("status: An intent log record "
4331 "could not be read.\n"
4332 "\tWaiting for adminstrator intervention to fix the "
4333 "faulted pool.\n"));
4334 (void) printf(gettext("action: Either restore the affected "
4335 "device(s) and run 'zpool online',\n"
4336 "\tor ignore the intent log records by running "
4337 "'zpool clear'.\n"));
4340 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4341 (void) printf(gettext("status: One or more devices are "
4342 "configured to use a non-native block size.\n"
4343 "\tExpect reduced performance.\n"));
4344 (void) printf(gettext("action: Replace affected devices with "
4345 "devices that support the\n\tconfigured block size, or "
4346 "migrate data to a properly configured\n\tpool.\n"));
4351 * The remaining errors can't actually be generated, yet.
4353 assert(reason == ZPOOL_STATUS_OK);
4357 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4360 if (config != NULL) {
4363 nvlist_t **spares, **l2cache;
4364 uint_t nspares, nl2cache;
4365 pool_scan_stat_t *ps = NULL;
4367 (void) nvlist_lookup_uint64_array(nvroot,
4368 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4369 print_scan_status(ps);
4371 namewidth = max_width(zhp, nvroot, 0, 0);
4375 (void) printf(gettext("config:\n\n"));
4376 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4377 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4378 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4379 namewidth, 0, B_FALSE);
4381 if (num_logs(nvroot) > 0)
4382 print_logs(zhp, nvroot, namewidth, B_TRUE);
4383 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4384 &l2cache, &nl2cache) == 0)
4385 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4387 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4388 &spares, &nspares) == 0)
4389 print_spares(zhp, spares, nspares, namewidth);
4391 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4393 nvlist_t *nverrlist = NULL;
4396 * If the approximate error count is small, get a
4397 * precise count by fetching the entire log and
4398 * uniquifying the results.
4400 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4401 zpool_get_errlog(zhp, &nverrlist) == 0) {
4406 while ((elem = nvlist_next_nvpair(nverrlist,
4411 nvlist_free(nverrlist);
4413 (void) printf("\n");
4416 (void) printf(gettext("errors: No known data "
4418 else if (!cbp->cb_verbose)
4419 (void) printf(gettext("errors: %llu data "
4420 "errors, use '-v' for a list\n"),
4421 (u_longlong_t)nerr);
4423 print_error_log(zhp);
4426 if (cbp->cb_dedup_stats)
4427 print_dedup_stats(config);
4429 (void) printf(gettext("config: The configuration cannot be "
4437 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4439 * -v Display complete error logs
4440 * -x Display only pools with potential problems
4441 * -D Display dedup status (undocumented)
4442 * -T Display a timestamp in date(1) or Unix format
4444 * Describes the health status of all pools or some subset.
4447 zpool_do_status(int argc, char **argv)
4451 unsigned long interval = 0, count = 0;
4452 status_cbdata_t cb = { 0 };
4455 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4458 cb.cb_verbose = B_TRUE;
4461 cb.cb_explain = B_TRUE;
4464 cb.cb_dedup_stats = B_TRUE;
4467 get_timestamp_arg(*optarg);
4470 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4479 get_interval_count(&argc, argv, &interval, &count);
4482 cb.cb_allpools = B_TRUE;
4484 cb.cb_first = B_TRUE;
4487 if (timestamp_fmt != NODATE)
4488 print_timestamp(timestamp_fmt);
4490 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4491 status_callback, &cb);
4493 if (argc == 0 && cb.cb_count == 0)
4494 (void) printf(gettext("no pools available\n"));
4495 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4496 (void) printf(gettext("all pools are healthy\n"));
4504 if (count != 0 && --count == 0)
4507 (void) sleep(interval);
4513 typedef struct upgrade_cbdata {
4515 boolean_t cb_unavail;
4516 char cb_poolname[ZPOOL_MAXNAMELEN];
4518 uint64_t cb_version;
4524 is_root_pool(zpool_handle_t *zhp)
4526 static struct statfs sfs;
4527 static char *poolname = NULL;
4528 static boolean_t stated = B_FALSE;
4533 if (statfs("/", &sfs) == -1) {
4534 (void) fprintf(stderr,
4535 "Unable to stat root file system: %s.\n",
4539 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4541 poolname = sfs.f_mntfromname;
4542 if ((slash = strchr(poolname, '/')) != NULL)
4545 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4549 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
4552 if (poolname[0] == '\0' && is_root_pool(zhp))
4553 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4555 #endif /* FreeBSD */
4558 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4562 uint64_t oldversion;
4564 config = zpool_get_config(zhp, NULL);
4565 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4568 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4569 assert(oldversion < version);
4571 ret = zpool_upgrade(zhp, version);
4575 if (version >= SPA_VERSION_FEATURES) {
4576 (void) printf(gettext("Successfully upgraded "
4577 "'%s' from version %llu to feature flags.\n"),
4578 zpool_get_name(zhp), oldversion);
4580 (void) printf(gettext("Successfully upgraded "
4581 "'%s' from version %llu to version %llu.\n"),
4582 zpool_get_name(zhp), oldversion, version);
4589 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4592 boolean_t firstff = B_TRUE;
4593 nvlist_t *enabled = zpool_get_features(zhp);
4596 for (i = 0; i < SPA_FEATURES; i++) {
4597 const char *fname = spa_feature_table[i].fi_uname;
4598 const char *fguid = spa_feature_table[i].fi_guid;
4599 if (!nvlist_exists(enabled, fguid)) {
4601 verify(-1 != asprintf(&propname, "feature@%s", fname));
4602 ret = zpool_set_prop(zhp, propname,
4603 ZFS_FEATURE_ENABLED);
4611 (void) printf(gettext("Enabled the "
4612 "following features on '%s':\n"),
4613 zpool_get_name(zhp));
4616 (void) printf(gettext(" %s\n"), fname);
4627 upgrade_cb(zpool_handle_t *zhp, void *arg)
4629 upgrade_cbdata_t *cbp = arg;
4632 boolean_t printnl = B_FALSE;
4635 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4636 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
4637 "currently unavailable.\n\n"), zpool_get_name(zhp));
4638 cbp->cb_unavail = B_TRUE;
4639 /* Allow iteration to continue. */
4643 config = zpool_get_config(zhp, NULL);
4644 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4647 assert(SPA_VERSION_IS_SUPPORTED(version));
4649 if (version < cbp->cb_version) {
4650 cbp->cb_first = B_FALSE;
4651 ret = upgrade_version(zhp, cbp->cb_version);
4655 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4656 sizeof(cbp->cb_poolname));
4657 #endif /* __FreeBSD__ */
4662 * If they did "zpool upgrade -a", then we could
4663 * be doing ioctls to different pools. We need
4664 * to log this history once to each pool, and bypass
4665 * the normal history logging that happens in main().
4667 (void) zpool_log_history(g_zfs, history_str);
4668 log_history = B_FALSE;
4672 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4674 ret = upgrade_enable_all(zhp, &count);
4679 cbp->cb_first = B_FALSE;
4682 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4683 sizeof(cbp->cb_poolname));
4684 #endif /* __FreeBSD__ */
4686 * If they did "zpool upgrade -a", then we could
4687 * be doing ioctls to different pools. We need
4688 * to log this history once to each pool, and bypass
4689 * the normal history logging that happens in main().
4691 (void) zpool_log_history(g_zfs, history_str);
4692 log_history = B_FALSE;
4697 (void) printf(gettext("\n"));
4704 upgrade_list_unavail(zpool_handle_t *zhp, void *arg)
4706 upgrade_cbdata_t *cbp = arg;
4708 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4709 if (cbp->cb_first) {
4710 (void) fprintf(stderr, gettext("The following pools "
4711 "are unavailable and cannot be upgraded as this "
4713 (void) fprintf(stderr, gettext("POOL\n"));
4714 (void) fprintf(stderr, gettext("------------\n"));
4715 cbp->cb_first = B_FALSE;
4717 (void) printf(gettext("%s\n"), zpool_get_name(zhp));
4718 cbp->cb_unavail = B_TRUE;
4724 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4726 upgrade_cbdata_t *cbp = arg;
4730 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4732 * This will have been reported by upgrade_list_unavail so
4733 * just allow iteration to continue.
4735 cbp->cb_unavail = B_TRUE;
4739 config = zpool_get_config(zhp, NULL);
4740 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4743 assert(SPA_VERSION_IS_SUPPORTED(version));
4745 if (version < SPA_VERSION_FEATURES) {
4746 if (cbp->cb_first) {
4747 (void) printf(gettext("The following pools are "
4748 "formatted with legacy version numbers and can\n"
4749 "be upgraded to use feature flags. After "
4750 "being upgraded, these pools\nwill no "
4751 "longer be accessible by software that does not "
4752 "support feature\nflags.\n\n"));
4753 (void) printf(gettext("VER POOL\n"));
4754 (void) printf(gettext("--- ------------\n"));
4755 cbp->cb_first = B_FALSE;
4758 (void) printf("%2llu %s\n", (u_longlong_t)version,
4759 zpool_get_name(zhp));
4766 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4768 upgrade_cbdata_t *cbp = arg;
4772 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4774 * This will have been reported by upgrade_list_unavail so
4775 * just allow iteration to continue.
4777 cbp->cb_unavail = B_TRUE;
4781 config = zpool_get_config(zhp, NULL);
4782 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4785 if (version >= SPA_VERSION_FEATURES) {
4787 boolean_t poolfirst = B_TRUE;
4788 nvlist_t *enabled = zpool_get_features(zhp);
4790 for (i = 0; i < SPA_FEATURES; i++) {
4791 const char *fguid = spa_feature_table[i].fi_guid;
4792 const char *fname = spa_feature_table[i].fi_uname;
4793 if (!nvlist_exists(enabled, fguid)) {
4794 if (cbp->cb_first) {
4795 (void) printf(gettext("\nSome "
4796 "supported features are not "
4797 "enabled on the following pools. "
4798 "Once a\nfeature is enabled the "
4799 "pool may become incompatible with "
4800 "software\nthat does not support "
4802 "zpool-features(7) for "
4804 (void) printf(gettext("POOL "
4806 (void) printf(gettext("------"
4808 cbp->cb_first = B_FALSE;
4812 (void) printf(gettext("%s\n"),
4813 zpool_get_name(zhp));
4814 poolfirst = B_FALSE;
4817 (void) printf(gettext(" %s\n"), fname);
4827 upgrade_one(zpool_handle_t *zhp, void *data)
4829 boolean_t printnl = B_FALSE;
4830 upgrade_cbdata_t *cbp = data;
4831 uint64_t cur_version;
4834 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4835 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
4836 "is currently unavailable.\n\n"), zpool_get_name(zhp));
4837 cbp->cb_unavail = B_TRUE;
4841 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4842 (void) printf(gettext("'log' is now a reserved word\n"
4843 "Pool 'log' must be renamed using export and import"
4844 " to upgrade.\n\n"));
4848 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4849 if (cur_version > cbp->cb_version) {
4850 (void) printf(gettext("Pool '%s' is already formatted "
4851 "using more current version '%llu'.\n\n"),
4852 zpool_get_name(zhp), cur_version);
4856 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4857 (void) printf(gettext("Pool '%s' is already formatted "
4858 "using version %llu.\n\n"), zpool_get_name(zhp),
4863 if (cur_version != cbp->cb_version) {
4865 ret = upgrade_version(zhp, cbp->cb_version);
4869 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4870 sizeof(cbp->cb_poolname));
4871 #endif /* __FreeBSD__ */
4874 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4876 ret = upgrade_enable_all(zhp, &count);
4883 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4884 sizeof(cbp->cb_poolname));
4885 #endif /* __FreeBSD __*/
4886 } else if (cur_version == SPA_VERSION) {
4887 (void) printf(gettext("Pool '%s' already has all "
4888 "supported features enabled.\n\n"),
4889 zpool_get_name(zhp));
4894 (void) printf(gettext("\n"));
4903 * zpool upgrade [-V version] <-a | pool ...>
4905 * With no arguments, display downrev'd ZFS pool available for upgrade.
4906 * Individual pools can be upgraded by specifying the pool, and '-a' will
4907 * upgrade all pools.
4910 zpool_do_upgrade(int argc, char **argv)
4913 upgrade_cbdata_t cb = { 0 };
4915 boolean_t showversions = B_FALSE;
4916 boolean_t upgradeall = B_FALSE;
4921 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4924 upgradeall = B_TRUE;
4927 showversions = B_TRUE;
4930 cb.cb_version = strtoll(optarg, &end, 10);
4932 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4933 (void) fprintf(stderr,
4934 gettext("invalid version '%s'\n"), optarg);
4939 (void) fprintf(stderr, gettext("missing argument for "
4940 "'%c' option\n"), optopt);
4944 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4955 if (cb.cb_version == 0) {
4956 cb.cb_version = SPA_VERSION;
4957 } else if (!upgradeall && argc == 0) {
4958 (void) fprintf(stderr, gettext("-V option is "
4959 "incompatible with other arguments\n"));
4964 if (upgradeall || argc != 0) {
4965 (void) fprintf(stderr, gettext("-v option is "
4966 "incompatible with other arguments\n"));
4969 } else if (upgradeall) {
4971 (void) fprintf(stderr, gettext("-a option should not "
4972 "be used along with a pool name\n"));
4977 (void) printf(gettext("This system supports ZFS pool feature "
4982 (void) printf(gettext("The following features are "
4984 (void) printf(gettext("FEAT DESCRIPTION\n"));
4985 (void) printf("----------------------------------------------"
4986 "---------------\n");
4987 for (i = 0; i < SPA_FEATURES; i++) {
4988 zfeature_info_t *fi = &spa_feature_table[i];
4989 const char *ro = fi->fi_can_readonly ?
4990 " (read-only compatible)" : "";
4992 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4993 (void) printf(" %s\n", fi->fi_desc);
4995 (void) printf("\n");
4997 (void) printf(gettext("The following legacy versions are also "
4999 (void) printf(gettext("VER DESCRIPTION\n"));
5000 (void) printf("--- -----------------------------------------"
5001 "---------------\n");
5002 (void) printf(gettext(" 1 Initial ZFS version\n"));
5003 (void) printf(gettext(" 2 Ditto blocks "
5004 "(replicated metadata)\n"));
5005 (void) printf(gettext(" 3 Hot spares and double parity "
5007 (void) printf(gettext(" 4 zpool history\n"));
5008 (void) printf(gettext(" 5 Compression using the gzip "
5010 (void) printf(gettext(" 6 bootfs pool property\n"));
5011 (void) printf(gettext(" 7 Separate intent log devices\n"));
5012 (void) printf(gettext(" 8 Delegated administration\n"));
5013 (void) printf(gettext(" 9 refquota and refreservation "
5015 (void) printf(gettext(" 10 Cache devices\n"));
5016 (void) printf(gettext(" 11 Improved scrub performance\n"));
5017 (void) printf(gettext(" 12 Snapshot properties\n"));
5018 (void) printf(gettext(" 13 snapused property\n"));
5019 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5020 (void) printf(gettext(" 15 user/group space accounting\n"));
5021 (void) printf(gettext(" 16 stmf property support\n"));
5022 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5023 (void) printf(gettext(" 18 Snapshot user holds\n"));
5024 (void) printf(gettext(" 19 Log device removal\n"));
5025 (void) printf(gettext(" 20 Compression using zle "
5026 "(zero-length encoding)\n"));
5027 (void) printf(gettext(" 21 Deduplication\n"));
5028 (void) printf(gettext(" 22 Received properties\n"));
5029 (void) printf(gettext(" 23 Slim ZIL\n"));
5030 (void) printf(gettext(" 24 System attributes\n"));
5031 (void) printf(gettext(" 25 Improved scrub stats\n"));
5032 (void) printf(gettext(" 26 Improved snapshot deletion "
5034 (void) printf(gettext(" 27 Improved snapshot creation "
5036 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5037 (void) printf(gettext("\nFor more information on a particular "
5038 "version, including supported releases,\n"));
5039 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5040 } else if (argc == 0 && upgradeall) {
5041 cb.cb_first = B_TRUE;
5042 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5043 if (ret == 0 && cb.cb_first) {
5044 if (cb.cb_version == SPA_VERSION) {
5045 (void) printf(gettext("All %spools are already "
5046 "formatted using feature flags.\n\n"),
5047 cb.cb_unavail ? gettext("available ") : "");
5048 (void) printf(gettext("Every %sfeature flags "
5049 "pool already has all supported features "
5051 cb.cb_unavail ? gettext("available ") : "");
5053 (void) printf(gettext("All pools are already "
5054 "formatted with version %llu or higher.\n"),
5058 } else if (argc == 0) {
5059 cb.cb_first = B_TRUE;
5060 ret = zpool_iter(g_zfs, upgrade_list_unavail, &cb);
5064 (void) fprintf(stderr, "\n");
5067 cb.cb_first = B_TRUE;
5068 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5072 (void) printf(gettext("All %spools are formatted using "
5073 "feature flags.\n\n"), cb.cb_unavail ?
5074 gettext("available ") : "");
5076 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5077 "for a list of available legacy versions.\n"));
5080 cb.cb_first = B_TRUE;
5081 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5085 (void) printf(gettext("Every %sfeature flags pool has "
5086 "all supported features enabled.\n"),
5087 cb.cb_unavail ? gettext("available ") : "");
5089 (void) printf(gettext("\n"));
5092 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5096 if (cb.cb_poolname[0] != '\0') {
5098 "If you boot from pool '%s', don't forget to update boot code.\n"
5099 "Assuming you use GPT partitioning and da0 is your boot disk\n"
5100 "the following command will do it:\n"
5102 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5109 typedef struct hist_cbdata {
5116 * Print out the command history for a specific pool.
5119 get_history_one(zpool_handle_t *zhp, void *data)
5125 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5127 cb->first = B_FALSE;
5129 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5131 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5134 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5135 &records, &numrecords) == 0);
5136 for (i = 0; i < numrecords; i++) {
5137 nvlist_t *rec = records[i];
5140 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5144 tsec = fnvlist_lookup_uint64(records[i],
5146 (void) localtime_r(&tsec, &t);
5147 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5150 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5151 (void) printf("%s %s", tbuf,
5152 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5153 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5155 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5158 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5159 (void) printf("%s unrecognized record:\n",
5161 dump_nvlist(rec, 4);
5164 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5165 zfs_history_event_names[ievent],
5166 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5167 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5168 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5171 (void) printf("%s [txg:%lld] %s", tbuf,
5172 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5173 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5174 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5175 (void) printf(" %s (%llu)",
5176 fnvlist_lookup_string(rec,
5178 fnvlist_lookup_uint64(rec,
5181 (void) printf(" %s", fnvlist_lookup_string(rec,
5182 ZPOOL_HIST_INT_STR));
5183 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5186 (void) printf("%s ioctl %s\n", tbuf,
5187 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5188 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5189 (void) printf(" input:\n");
5190 dump_nvlist(fnvlist_lookup_nvlist(rec,
5191 ZPOOL_HIST_INPUT_NVL), 8);
5193 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5194 (void) printf(" output:\n");
5195 dump_nvlist(fnvlist_lookup_nvlist(rec,
5196 ZPOOL_HIST_OUTPUT_NVL), 8);
5201 (void) printf("%s unrecognized record:\n", tbuf);
5202 dump_nvlist(rec, 4);
5206 (void) printf("\n");
5209 (void) printf(" [");
5210 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5211 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5212 struct passwd *pwd = getpwuid(who);
5213 (void) printf("user %d ", (int)who);
5215 (void) printf("(%s) ", pwd->pw_name);
5217 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5218 (void) printf("on %s",
5219 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5221 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5222 (void) printf(":%s",
5223 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5226 (void) printf("\n");
5228 (void) printf("\n");
5235 * zpool history <pool>
5237 * Displays the history of commands that modified pools.
5240 zpool_do_history(int argc, char **argv)
5242 hist_cbdata_t cbdata = { 0 };
5246 cbdata.first = B_TRUE;
5248 while ((c = getopt(argc, argv, "li")) != -1) {
5251 cbdata.longfmt = B_TRUE;
5254 cbdata.internal = B_TRUE;
5257 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5265 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5268 if (argc == 0 && cbdata.first == B_TRUE) {
5269 (void) printf(gettext("no pools available\n"));
5277 get_callback(zpool_handle_t *zhp, void *data)
5279 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5280 char value[MAXNAMELEN];
5281 zprop_source_t srctype;
5284 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5287 * Skip the special fake placeholder. This will also skip
5288 * over the name property when 'all' is specified.
5290 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5291 pl == cbp->cb_proplist)
5294 if (pl->pl_prop == ZPROP_INVAL &&
5295 (zpool_prop_feature(pl->pl_user_prop) ||
5296 zpool_prop_unsupported(pl->pl_user_prop))) {
5297 srctype = ZPROP_SRC_LOCAL;
5299 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5300 value, sizeof (value)) == 0) {
5301 zprop_print_one_property(zpool_get_name(zhp),
5302 cbp, pl->pl_user_prop, value, srctype,
5306 if (zpool_get_prop(zhp, pl->pl_prop, value,
5307 sizeof (value), &srctype, cbp->cb_literal) != 0)
5310 zprop_print_one_property(zpool_get_name(zhp), cbp,
5311 zpool_prop_to_name(pl->pl_prop), value, srctype,
5319 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5321 * -H Scripted mode. Don't display headers, and separate properties
5323 * -o List of columns to display. Defaults to
5324 * "name,property,value,source".
5325 * -p Diplay values in parsable (exact) format.
5327 * Get properties of pools in the system. Output space statistics
5328 * for each one as well as other attributes.
5331 zpool_do_get(int argc, char **argv)
5333 zprop_get_cbdata_t cb = { 0 };
5334 zprop_list_t fake_name = { 0 };
5339 cb.cb_first = B_TRUE;
5342 * Set up default columns and sources.
5344 cb.cb_sources = ZPROP_SRC_ALL;
5345 cb.cb_columns[0] = GET_COL_NAME;
5346 cb.cb_columns[1] = GET_COL_PROPERTY;
5347 cb.cb_columns[2] = GET_COL_VALUE;
5348 cb.cb_columns[3] = GET_COL_SOURCE;
5349 cb.cb_type = ZFS_TYPE_POOL;
5352 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5355 cb.cb_literal = B_TRUE;
5358 cb.cb_scripted = B_TRUE;
5361 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5363 while (*optarg != '\0') {
5364 static char *col_subopts[] =
5365 { "name", "property", "value", "source",
5368 if (i == ZFS_GET_NCOLS) {
5369 (void) fprintf(stderr, gettext("too "
5370 "many fields given to -o "
5375 switch (getsubopt(&optarg, col_subopts,
5378 cb.cb_columns[i++] = GET_COL_NAME;
5381 cb.cb_columns[i++] = GET_COL_PROPERTY;
5384 cb.cb_columns[i++] = GET_COL_VALUE;
5387 cb.cb_columns[i++] = GET_COL_SOURCE;
5391 (void) fprintf(stderr,
5392 gettext("\"all\" conflicts "
5393 "with specific fields "
5394 "given to -o option\n"));
5397 cb.cb_columns[0] = GET_COL_NAME;
5398 cb.cb_columns[1] = GET_COL_PROPERTY;
5399 cb.cb_columns[2] = GET_COL_VALUE;
5400 cb.cb_columns[3] = GET_COL_SOURCE;
5404 (void) fprintf(stderr,
5405 gettext("invalid column name "
5412 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5422 (void) fprintf(stderr, gettext("missing property "
5427 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5428 ZFS_TYPE_POOL) != 0)
5434 if (cb.cb_proplist != NULL) {
5435 fake_name.pl_prop = ZPOOL_PROP_NAME;
5436 fake_name.pl_width = strlen(gettext("NAME"));
5437 fake_name.pl_next = cb.cb_proplist;
5438 cb.cb_proplist = &fake_name;
5441 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5444 if (cb.cb_proplist == &fake_name)
5445 zprop_free_list(fake_name.pl_next);
5447 zprop_free_list(cb.cb_proplist);
5452 typedef struct set_cbdata {
5455 boolean_t cb_any_successful;
5459 set_callback(zpool_handle_t *zhp, void *data)
5462 set_cbdata_t *cb = (set_cbdata_t *)data;
5464 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5467 cb->cb_any_successful = B_TRUE;
5473 zpool_do_set(int argc, char **argv)
5475 set_cbdata_t cb = { 0 };
5478 if (argc > 1 && argv[1][0] == '-') {
5479 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5485 (void) fprintf(stderr, gettext("missing property=value "
5491 (void) fprintf(stderr, gettext("missing pool name\n"));
5496 (void) fprintf(stderr, gettext("too many pool names\n"));
5500 cb.cb_propname = argv[1];
5501 cb.cb_value = strchr(cb.cb_propname, '=');
5502 if (cb.cb_value == NULL) {
5503 (void) fprintf(stderr, gettext("missing value in "
5504 "property=value argument\n"));
5508 *(cb.cb_value) = '\0';
5511 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5518 find_command_idx(char *command, int *idx)
5522 for (i = 0; i < NCOMMAND; i++) {
5523 if (command_table[i].name == NULL)
5526 if (strcmp(command, command_table[i].name) == 0) {
5535 main(int argc, char **argv)
5541 (void) setlocale(LC_ALL, "");
5542 (void) textdomain(TEXT_DOMAIN);
5544 if ((g_zfs = libzfs_init()) == NULL) {
5545 (void) fprintf(stderr, gettext("internal error: failed to "
5546 "initialize ZFS library\n"));
5550 libzfs_print_on_error(g_zfs, B_TRUE);
5555 * Make sure the user has specified some command.
5558 (void) fprintf(stderr, gettext("missing command\n"));
5567 if (strcmp(cmdname, "-?") == 0)
5570 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5573 * Run the appropriate command.
5575 if (find_command_idx(cmdname, &i) == 0) {
5576 current_command = &command_table[i];
5577 ret = command_table[i].func(argc - 1, argv + 1);
5578 } else if (strchr(cmdname, '=')) {
5579 verify(find_command_idx("set", &i) == 0);
5580 current_command = &command_table[i];
5581 ret = command_table[i].func(argc, argv);
5582 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5584 * 'freeze' is a vile debugging abomination, so we treat
5587 zfs_cmd_t zc = { 0 };
5588 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5589 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5591 (void) fprintf(stderr, gettext("unrecognized "
5592 "command '%s'\n"), cmdname);
5596 if (ret == 0 && log_history)
5597 (void) zpool_log_history(g_zfs, history_str);
5602 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5603 * for the purposes of running ::findleaks.
5605 if (getenv("ZFS_ABORT") != NULL) {
5606 (void) printf("dumping core by request\n");