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) 2012 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.
50 #include <sys/fs/zfs.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 #include "statcommon.h"
61 static int zpool_do_create(int, char **);
62 static int zpool_do_destroy(int, char **);
64 static int zpool_do_add(int, char **);
65 static int zpool_do_remove(int, char **);
66 static int zpool_do_labelclear(int, char **);
68 static int zpool_do_list(int, char **);
69 static int zpool_do_iostat(int, char **);
70 static int zpool_do_status(int, char **);
72 static int zpool_do_online(int, char **);
73 static int zpool_do_offline(int, char **);
74 static int zpool_do_clear(int, char **);
75 static int zpool_do_reopen(int, char **);
77 static int zpool_do_reguid(int, char **);
79 static int zpool_do_attach(int, char **);
80 static int zpool_do_detach(int, char **);
81 static int zpool_do_replace(int, char **);
82 static int zpool_do_split(int, char **);
84 static int zpool_do_scrub(int, char **);
86 static int zpool_do_import(int, char **);
87 static int zpool_do_export(int, char **);
89 static int zpool_do_upgrade(int, char **);
91 static int zpool_do_history(int, char **);
93 static int zpool_do_get(int, char **);
94 static int zpool_do_set(int, char **);
97 * These libumem hooks provide a reasonable set of defaults for the allocator's
98 * debugging facilities.
103 _umem_debug_init(void)
105 return ("default,verbose"); /* $UMEM_DEBUG setting */
109 _umem_logging_init(void)
111 return ("fail,contents"); /* $UMEM_LOGGING setting */
143 typedef struct zpool_command {
145 int (*func)(int, char **);
150 * Master command table. Each ZFS command has a name, associated function, and
151 * usage message. The usage messages need to be internationalized, so we have
152 * to have a function to return the usage message based on a command index.
154 * These commands are organized according to how they are displayed in the usage
155 * message. An empty command (one with a NULL name) indicates an empty line in
156 * the generic usage message.
158 static zpool_command_t command_table[] = {
159 { "create", zpool_do_create, HELP_CREATE },
160 { "destroy", zpool_do_destroy, HELP_DESTROY },
162 { "add", zpool_do_add, HELP_ADD },
163 { "remove", zpool_do_remove, HELP_REMOVE },
165 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
167 { "list", zpool_do_list, HELP_LIST },
168 { "iostat", zpool_do_iostat, HELP_IOSTAT },
169 { "status", zpool_do_status, HELP_STATUS },
171 { "online", zpool_do_online, HELP_ONLINE },
172 { "offline", zpool_do_offline, HELP_OFFLINE },
173 { "clear", zpool_do_clear, HELP_CLEAR },
174 { "reopen", zpool_do_reopen, HELP_REOPEN },
176 { "attach", zpool_do_attach, HELP_ATTACH },
177 { "detach", zpool_do_detach, HELP_DETACH },
178 { "replace", zpool_do_replace, HELP_REPLACE },
179 { "split", zpool_do_split, HELP_SPLIT },
181 { "scrub", zpool_do_scrub, HELP_SCRUB },
183 { "import", zpool_do_import, HELP_IMPORT },
184 { "export", zpool_do_export, HELP_EXPORT },
185 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
186 { "reguid", zpool_do_reguid, HELP_REGUID },
188 { "history", zpool_do_history, HELP_HISTORY },
189 { "get", zpool_do_get, HELP_GET },
190 { "set", zpool_do_set, HELP_SET },
193 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
195 static zpool_command_t *current_command;
196 static char history_str[HIS_MAX_RECORD_LEN];
197 static boolean_t log_history = B_TRUE;
198 static uint_t timestamp_fmt = NODATE;
201 get_usage(zpool_help_t idx) {
204 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
206 return (gettext("\tattach [-f] <pool> <device> "
209 return (gettext("\tclear [-nF] <pool> [device]\n"));
211 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
212 "\t [-O file-system-property=value] ... \n"
213 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
215 return (gettext("\tdestroy [-f] <pool>\n"));
217 return (gettext("\tdetach <pool> <device>\n"));
219 return (gettext("\texport [-f] <pool> ...\n"));
221 return (gettext("\thistory [-il] [<pool>] ...\n"));
223 return (gettext("\timport [-d dir] [-D]\n"
224 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
225 "\timport [-o mntopts] [-o property=value] ... \n"
226 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
227 "[-R root] [-F [-n]] -a\n"
228 "\timport [-o mntopts] [-o property=value] ... \n"
229 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
230 "[-R root] [-F [-n]]\n"
231 "\t <pool | id> [newpool]\n"));
233 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
235 case HELP_LABELCLEAR:
236 return (gettext("\tlabelclear [-f] <vdev>\n"));
238 return (gettext("\tlist [-Hv] [-o property[,...]] "
239 "[-T d|u] [pool] ... [interval [count]]\n"));
241 return (gettext("\toffline [-t] <pool> <device> ...\n"));
243 return (gettext("\tonline [-e] <pool> <device> ...\n"));
245 return (gettext("\treplace [-f] <pool> <device> "
248 return (gettext("\tremove <pool> <device> ...\n"));
250 return (""); /* Undocumented command */
252 return (gettext("\tscrub [-s] <pool> ...\n"));
254 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
257 return (gettext("\tupgrade [-v]\n"
258 "\tupgrade [-V version] <-a | pool ...>\n"));
260 return (gettext("\tget <\"all\" | property[,...]> "
263 return (gettext("\tset <property=value> <pool> \n"));
265 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
266 "\t [-o property=value] <pool> <newpool> "
267 "[<device> ...]\n"));
269 return (gettext("\treguid <pool>\n"));
278 * Callback routine that will print out a pool property value.
281 print_prop_cb(int prop, void *cb)
285 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
287 if (zpool_prop_readonly(prop))
288 (void) fprintf(fp, " NO ");
290 (void) fprintf(fp, " YES ");
292 if (zpool_prop_values(prop) == NULL)
293 (void) fprintf(fp, "-\n");
295 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
301 * Display usage message. If we're inside a command, display only the usage for
302 * that command. Otherwise, iterate over the entire command table and display
303 * a complete usage message.
306 usage(boolean_t requested)
308 FILE *fp = requested ? stdout : stderr;
310 if (current_command == NULL) {
313 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
315 gettext("where 'command' is one of the following:\n\n"));
317 for (i = 0; i < NCOMMAND; i++) {
318 if (command_table[i].name == NULL)
319 (void) fprintf(fp, "\n");
321 (void) fprintf(fp, "%s",
322 get_usage(command_table[i].usage));
325 (void) fprintf(fp, gettext("usage:\n"));
326 (void) fprintf(fp, "%s", get_usage(current_command->usage));
329 if (current_command != NULL &&
330 ((strcmp(current_command->name, "set") == 0) ||
331 (strcmp(current_command->name, "get") == 0) ||
332 (strcmp(current_command->name, "list") == 0))) {
335 gettext("\nthe following properties are supported:\n"));
337 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
338 "PROPERTY", "EDIT", "VALUES");
340 /* Iterate over all properties */
341 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
344 (void) fprintf(fp, "\t%-15s ", "feature@...");
345 (void) fprintf(fp, "YES disabled | enabled | active\n");
347 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
348 "appended with a feature name.\nSee zpool-features(7).\n"));
352 * See comments at end of main().
354 if (getenv("ZFS_ABORT") != NULL) {
355 (void) printf("dumping core by request\n");
359 exit(requested ? 0 : 2);
363 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
364 boolean_t print_logs)
371 (void) printf("\t%*s%s\n", indent, "", name);
373 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
374 &child, &children) != 0)
377 for (c = 0; c < children; c++) {
378 uint64_t is_log = B_FALSE;
380 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
382 if ((is_log && !print_logs) || (!is_log && print_logs))
385 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
386 print_vdev_tree(zhp, vname, child[c], indent + 2,
393 prop_list_contains_feature(nvlist_t *proplist)
396 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
397 nvp = nvlist_next_nvpair(proplist, nvp)) {
398 if (zpool_prop_feature(nvpair_name(nvp)))
405 * Add a property pair (name, string-value) into a property nvlist.
408 add_prop_list(const char *propname, char *propval, nvlist_t **props,
411 zpool_prop_t prop = ZPROP_INVAL;
417 if (*props == NULL &&
418 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
419 (void) fprintf(stderr,
420 gettext("internal error: out of memory\n"));
427 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
429 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
430 !zpool_prop_feature(propname)) {
431 (void) fprintf(stderr, gettext("property '%s' is "
432 "not a valid pool property\n"), propname);
437 * feature@ properties and version should not be specified
440 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
441 nvlist_exists(proplist, vname)) ||
442 (prop == ZPOOL_PROP_VERSION &&
443 prop_list_contains_feature(proplist))) {
444 (void) fprintf(stderr, gettext("'feature@' and "
445 "'version' properties cannot be specified "
451 if (zpool_prop_feature(propname))
454 normnm = zpool_prop_to_name(prop);
456 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
457 normnm = zfs_prop_to_name(fprop);
463 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
464 prop != ZPOOL_PROP_CACHEFILE) {
465 (void) fprintf(stderr, gettext("property '%s' "
466 "specified multiple times\n"), propname);
470 if (nvlist_add_string(proplist, normnm, propval) != 0) {
471 (void) fprintf(stderr, gettext("internal "
472 "error: out of memory\n"));
480 * zpool add [-fn] <pool> <vdev> ...
482 * -f Force addition of devices, even if they appear in use
483 * -n Do not add the devices, but display the resulting layout if
484 * they were to be added.
486 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
487 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
491 zpool_do_add(int argc, char **argv)
493 boolean_t force = B_FALSE;
494 boolean_t dryrun = B_FALSE;
503 while ((c = getopt(argc, argv, "fn")) != -1) {
512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
521 /* get pool name and check number of arguments */
523 (void) fprintf(stderr, gettext("missing pool name argument\n"));
527 (void) fprintf(stderr, gettext("missing vdev specification\n"));
536 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
539 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
540 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
546 /* pass off to get_vdev_spec for processing */
547 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
549 if (nvroot == NULL) {
555 nvlist_t *poolnvroot;
557 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
560 (void) printf(gettext("would update '%s' to the following "
561 "configuration:\n"), zpool_get_name(zhp));
563 /* print original main pool and new tree */
564 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
565 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
567 /* Do the same for the logs */
568 if (num_logs(poolnvroot) > 0) {
569 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
570 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
571 } else if (num_logs(nvroot) > 0) {
572 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
577 ret = (zpool_add(zhp, nvroot) != 0);
587 * zpool remove <pool> <vdev> ...
589 * Removes the given vdev from the pool. Currently, this supports removing
590 * spares, cache, and log devices from the pool.
593 zpool_do_remove(int argc, char **argv)
602 /* get pool name and check number of arguments */
604 (void) fprintf(stderr, gettext("missing pool name argument\n"));
608 (void) fprintf(stderr, gettext("missing device\n"));
614 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
617 for (i = 1; i < argc; i++) {
618 if (zpool_vdev_remove(zhp, argv[i]) != 0)
626 * zpool labelclear <vdev>
628 * Verifies that the vdev is not active and zeros out the label information
632 zpool_do_labelclear(int argc, char **argv)
635 int c, fd = -1, ret = 0;
637 boolean_t inuse = B_FALSE;
638 boolean_t force = B_FALSE;
641 while ((c = getopt(argc, argv, "f")) != -1) {
647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
658 (void) fprintf(stderr, gettext("missing vdev device name\n"));
663 if ((fd = open(vdev, O_RDWR)) < 0) {
664 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
669 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
673 (void) fprintf(stderr,
674 gettext("Unable to determine pool state for %s\n"
675 "Use -f to force the clearing any label data\n"), vdev);
683 case POOL_STATE_ACTIVE:
684 case POOL_STATE_SPARE:
685 case POOL_STATE_L2CACHE:
686 (void) fprintf(stderr,
687 gettext("labelclear operation failed.\n"
688 "\tVdev %s is a member (%s), of pool \"%s\".\n"
689 "\tTo remove label information from this device, export or destroy\n"
690 "\tthe pool, or remove %s from the configuration of this pool\n"
691 "\tand retry the labelclear operation\n"),
692 vdev, zpool_pool_state_to_name(state), name, vdev);
696 case POOL_STATE_EXPORTED:
700 (void) fprintf(stderr,
701 gettext("labelclear operation failed.\n"
702 "\tVdev %s is a member of the exported pool \"%s\".\n"
703 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
709 case POOL_STATE_POTENTIALLY_ACTIVE:
713 (void) fprintf(stderr,
714 gettext("labelclear operation failed.\n"
715 "\tVdev %s is a member of the pool \"%s\".\n"
716 "\tThis pool is unknown to this system, but may be active on\n"
717 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
718 "\tremoval of label information.\n"),
723 case POOL_STATE_DESTROYED:
724 /* inuse should never be set for a destoryed pool... */
730 if (zpool_clear_label(fd) != 0) {
731 (void) fprintf(stderr,
732 gettext("Label clear failed on vdev %s\n"), vdev);
745 * zpool create [-fnd] [-o property=value] ...
746 * [-O file-system-property=value] ...
747 * [-R root] [-m mountpoint] <pool> <dev> ...
749 * -f Force creation, even if devices appear in use
750 * -n Do not create the pool, but display the resulting layout if it
751 * were to be created.
752 * -R Create a pool under an alternate root
753 * -m Set default mountpoint for the root dataset. By default it's
755 * -o Set property=value.
756 * -d Don't automatically enable all supported pool features
757 * (individual features can be enabled with -o).
758 * -O Set fsproperty=value in the pool's root file system
760 * Creates the named pool according to the given vdev specification. The
761 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
762 * we get the nvlist back from get_vdev_spec(), we either print out the contents
763 * (if '-n' was specified), or pass it to libzfs to do the creation.
766 zpool_do_create(int argc, char **argv)
768 boolean_t force = B_FALSE;
769 boolean_t dryrun = B_FALSE;
770 boolean_t enable_all_pool_feat = B_TRUE;
772 nvlist_t *nvroot = NULL;
775 char *altroot = NULL;
776 char *mountpoint = NULL;
777 nvlist_t *fsprops = NULL;
778 nvlist_t *props = NULL;
782 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
791 enable_all_pool_feat = B_FALSE;
795 if (add_prop_list(zpool_prop_to_name(
796 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
798 if (nvlist_lookup_string(props,
799 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
802 if (add_prop_list(zpool_prop_to_name(
803 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
807 /* Equivalent to -O mountpoint=optarg */
811 if ((propval = strchr(optarg, '=')) == NULL) {
812 (void) fprintf(stderr, gettext("missing "
813 "'=' for -o option\n"));
819 if (add_prop_list(optarg, propval, &props, B_TRUE))
823 * If the user is creating a pool that doesn't support
824 * feature flags, don't enable any features.
826 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
830 ver = strtoull(propval, &end, 10);
832 ver < SPA_VERSION_FEATURES) {
833 enable_all_pool_feat = B_FALSE;
838 if ((propval = strchr(optarg, '=')) == NULL) {
839 (void) fprintf(stderr, gettext("missing "
840 "'=' for -O option\n"));
847 * Mountpoints are checked and then added later.
848 * Uniquely among properties, they can be specified
849 * more than once, to avoid conflict with -m.
851 if (0 == strcmp(optarg,
852 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
853 mountpoint = propval;
854 } else if (add_prop_list(optarg, propval, &fsprops,
860 (void) fprintf(stderr, gettext("missing argument for "
861 "'%c' option\n"), optopt);
864 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
873 /* get pool name and check number of arguments */
875 (void) fprintf(stderr, gettext("missing pool name argument\n"));
879 (void) fprintf(stderr, gettext("missing vdev specification\n"));
886 * As a special case, check for use of '/' in the name, and direct the
887 * user to use 'zfs create' instead.
889 if (strchr(poolname, '/') != NULL) {
890 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
891 "character '/' in pool name\n"), poolname);
892 (void) fprintf(stderr, gettext("use 'zfs create' to "
893 "create a dataset\n"));
897 /* pass off to get_vdev_spec for bulk processing */
898 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
903 /* make_root_vdev() allows 0 toplevel children if there are spares */
904 if (!zfs_allocatable_devs(nvroot)) {
905 (void) fprintf(stderr, gettext("invalid vdev "
906 "specification: at least one toplevel vdev must be "
911 if (altroot != NULL && altroot[0] != '/') {
912 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
913 "must be an absolute path\n"), altroot);
918 * Check the validity of the mountpoint and direct the user to use the
919 * '-m' mountpoint option if it looks like its in use.
921 if (mountpoint == NULL ||
922 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
923 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
924 char buf[MAXPATHLEN];
927 if (mountpoint && mountpoint[0] != '/') {
928 (void) fprintf(stderr, gettext("invalid mountpoint "
929 "'%s': must be an absolute path, 'legacy', or "
930 "'none'\n"), mountpoint);
934 if (mountpoint == NULL) {
936 (void) snprintf(buf, sizeof (buf), "%s/%s",
939 (void) snprintf(buf, sizeof (buf), "/%s",
943 (void) snprintf(buf, sizeof (buf), "%s%s",
944 altroot, mountpoint);
946 (void) snprintf(buf, sizeof (buf), "%s",
950 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
951 (void) fprintf(stderr, gettext("mountpoint '%s' : "
952 "%s\n"), buf, strerror(errno));
953 (void) fprintf(stderr, gettext("use '-m' "
954 "option to provide a different default\n"));
959 while (count < 3 && readdir(dirp) != NULL)
961 (void) closedir(dirp);
964 (void) fprintf(stderr, gettext("mountpoint "
965 "'%s' exists and is not empty\n"), buf);
966 (void) fprintf(stderr, gettext("use '-m' "
967 "option to provide a "
968 "different default\n"));
975 * Now that the mountpoint's validity has been checked, ensure that
976 * the property is set appropriately prior to creating the pool.
978 if (mountpoint != NULL) {
979 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
980 mountpoint, &fsprops, B_FALSE);
988 * For a dry run invocation, print out a basic message and run
989 * through all the vdevs in the list and print out in an
990 * appropriate hierarchy.
992 (void) printf(gettext("would create '%s' with the "
993 "following layout:\n\n"), poolname);
995 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
996 if (num_logs(nvroot) > 0)
997 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1002 * Hand off to libzfs.
1004 if (enable_all_pool_feat) {
1006 for (i = 0; i < SPA_FEATURES; i++) {
1007 char propname[MAXPATHLEN];
1008 zfeature_info_t *feat = &spa_feature_table[i];
1010 (void) snprintf(propname, sizeof (propname),
1011 "feature@%s", feat->fi_uname);
1014 * Skip feature if user specified it manually
1015 * on the command line.
1017 if (nvlist_exists(props, propname))
1020 ret = add_prop_list(propname,
1021 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1028 if (zpool_create(g_zfs, poolname,
1029 nvroot, props, fsprops) == 0) {
1030 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1031 ZFS_TYPE_FILESYSTEM);
1033 if (zfs_mount(pool, NULL, 0) == 0)
1034 ret = zfs_shareall(pool);
1037 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1038 (void) fprintf(stderr, gettext("pool name may have "
1044 nvlist_free(nvroot);
1045 nvlist_free(fsprops);
1049 nvlist_free(fsprops);
1056 * zpool destroy <pool>
1058 * -f Forcefully unmount any datasets
1060 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1063 zpool_do_destroy(int argc, char **argv)
1065 boolean_t force = B_FALSE;
1068 zpool_handle_t *zhp;
1072 while ((c = getopt(argc, argv, "f")) != -1) {
1078 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1087 /* check arguments */
1089 (void) fprintf(stderr, gettext("missing pool argument\n"));
1093 (void) fprintf(stderr, gettext("too many arguments\n"));
1099 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1101 * As a special case, check for use of '/' in the name, and
1102 * direct the user to use 'zfs destroy' instead.
1104 if (strchr(pool, '/') != NULL)
1105 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1106 "destroy a dataset\n"));
1110 if (zpool_disable_datasets(zhp, force) != 0) {
1111 (void) fprintf(stderr, gettext("could not destroy '%s': "
1112 "could not unmount datasets\n"), zpool_get_name(zhp));
1116 /* The history must be logged as part of the export */
1117 log_history = B_FALSE;
1119 ret = (zpool_destroy(zhp, history_str) != 0);
1127 * zpool export [-f] <pool> ...
1129 * -f Forcefully unmount datasets
1131 * Export the given pools. By default, the command will attempt to cleanly
1132 * unmount any active datasets within the pool. If the '-f' flag is specified,
1133 * then the datasets will be forcefully unmounted.
1136 zpool_do_export(int argc, char **argv)
1138 boolean_t force = B_FALSE;
1139 boolean_t hardforce = B_FALSE;
1141 zpool_handle_t *zhp;
1146 while ((c = getopt(argc, argv, "fF")) != -1) {
1155 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1164 /* check arguments */
1166 (void) fprintf(stderr, gettext("missing pool argument\n"));
1171 for (i = 0; i < argc; i++) {
1172 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1177 if (zpool_disable_datasets(zhp, force) != 0) {
1183 /* The history must be logged as part of the export */
1184 log_history = B_FALSE;
1187 if (zpool_export_force(zhp, history_str) != 0)
1189 } else if (zpool_export(zhp, force, history_str) != 0) {
1200 * Given a vdev configuration, determine the maximum width needed for the device
1204 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1206 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1211 if (strlen(name) + depth > max)
1212 max = strlen(name) + depth;
1216 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1217 &child, &children) == 0) {
1218 for (c = 0; c < children; c++)
1219 if ((ret = max_width(zhp, child[c], depth + 2,
1224 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1225 &child, &children) == 0) {
1226 for (c = 0; c < children; c++)
1227 if ((ret = max_width(zhp, child[c], depth + 2,
1232 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1233 &child, &children) == 0) {
1234 for (c = 0; c < children; c++)
1235 if ((ret = max_width(zhp, child[c], depth + 2,
1244 typedef struct spare_cbdata {
1246 zpool_handle_t *cb_zhp;
1250 find_vdev(nvlist_t *nv, uint64_t search)
1256 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1260 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1261 &child, &children) == 0) {
1262 for (c = 0; c < children; c++)
1263 if (find_vdev(child[c], search))
1271 find_spare(zpool_handle_t *zhp, void *data)
1273 spare_cbdata_t *cbp = data;
1274 nvlist_t *config, *nvroot;
1276 config = zpool_get_config(zhp, NULL);
1277 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1280 if (find_vdev(nvroot, cbp->cb_guid)) {
1290 * Print out configuration state as requested by status_callback.
1293 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1294 int namewidth, int depth, boolean_t isspare)
1298 pool_scan_stat_t *ps = NULL;
1300 char rbuf[6], wbuf[6], cbuf[6];
1302 uint64_t notpresent;
1306 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1307 &child, &children) != 0)
1310 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1311 (uint64_t **)&vs, &c) == 0);
1313 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1316 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1319 if (vs->vs_aux == VDEV_AUX_SPARED)
1321 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1325 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1329 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1330 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1331 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1332 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1335 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1336 ¬present) == 0 ||
1337 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1339 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1340 (void) printf(" was %s", path);
1341 } else if (vs->vs_aux != 0) {
1344 switch (vs->vs_aux) {
1345 case VDEV_AUX_OPEN_FAILED:
1346 (void) printf(gettext("cannot open"));
1349 case VDEV_AUX_BAD_GUID_SUM:
1350 (void) printf(gettext("missing device"));
1353 case VDEV_AUX_NO_REPLICAS:
1354 (void) printf(gettext("insufficient replicas"));
1357 case VDEV_AUX_VERSION_NEWER:
1358 (void) printf(gettext("newer version"));
1361 case VDEV_AUX_UNSUP_FEAT:
1362 (void) printf(gettext("unsupported feature(s)"));
1365 case VDEV_AUX_SPARED:
1366 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1368 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1369 if (strcmp(zpool_get_name(cb.cb_zhp),
1370 zpool_get_name(zhp)) == 0)
1371 (void) printf(gettext("currently in "
1374 (void) printf(gettext("in use by "
1376 zpool_get_name(cb.cb_zhp));
1377 zpool_close(cb.cb_zhp);
1379 (void) printf(gettext("currently in use"));
1383 case VDEV_AUX_ERR_EXCEEDED:
1384 (void) printf(gettext("too many errors"));
1387 case VDEV_AUX_IO_FAILURE:
1388 (void) printf(gettext("experienced I/O failures"));
1391 case VDEV_AUX_BAD_LOG:
1392 (void) printf(gettext("bad intent log"));
1395 case VDEV_AUX_EXTERNAL:
1396 (void) printf(gettext("external device fault"));
1399 case VDEV_AUX_SPLIT_POOL:
1400 (void) printf(gettext("split into new pool"));
1404 (void) printf(gettext("corrupted data"));
1409 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1410 (uint64_t **)&ps, &c);
1412 if (ps && ps->pss_state == DSS_SCANNING &&
1413 vs->vs_scan_processed != 0 && children == 0) {
1414 (void) printf(gettext(" (%s)"),
1415 (ps->pss_func == POOL_SCAN_RESILVER) ?
1416 "resilvering" : "repairing");
1419 (void) printf("\n");
1421 for (c = 0; c < children; c++) {
1422 uint64_t islog = B_FALSE, ishole = B_FALSE;
1424 /* Don't print logs or holes here */
1425 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1427 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1429 if (islog || ishole)
1431 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1432 print_status_config(zhp, vname, child[c],
1433 namewidth, depth + 2, isspare);
1440 * Print the configuration of an exported pool. Iterate over all vdevs in the
1441 * pool, printing out the name and status for each one.
1444 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1451 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1452 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1453 strcmp(type, VDEV_TYPE_HOLE) == 0)
1456 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1457 (uint64_t **)&vs, &c) == 0);
1459 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1460 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1462 if (vs->vs_aux != 0) {
1465 switch (vs->vs_aux) {
1466 case VDEV_AUX_OPEN_FAILED:
1467 (void) printf(gettext("cannot open"));
1470 case VDEV_AUX_BAD_GUID_SUM:
1471 (void) printf(gettext("missing device"));
1474 case VDEV_AUX_NO_REPLICAS:
1475 (void) printf(gettext("insufficient replicas"));
1478 case VDEV_AUX_VERSION_NEWER:
1479 (void) printf(gettext("newer version"));
1482 case VDEV_AUX_UNSUP_FEAT:
1483 (void) printf(gettext("unsupported feature(s)"));
1486 case VDEV_AUX_ERR_EXCEEDED:
1487 (void) printf(gettext("too many errors"));
1491 (void) printf(gettext("corrupted data"));
1495 (void) printf("\n");
1497 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1498 &child, &children) != 0)
1501 for (c = 0; c < children; c++) {
1502 uint64_t is_log = B_FALSE;
1504 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1509 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1510 print_import_config(vname, child[c], namewidth, depth + 2);
1514 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1515 &child, &children) == 0) {
1516 (void) printf(gettext("\tcache\n"));
1517 for (c = 0; c < children; c++) {
1518 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1519 (void) printf("\t %s\n", vname);
1524 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1525 &child, &children) == 0) {
1526 (void) printf(gettext("\tspares\n"));
1527 for (c = 0; c < children; c++) {
1528 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1529 (void) printf("\t %s\n", vname);
1537 * Logs are recorded as top level vdevs in the main pool child array
1538 * but with "is_log" set to 1. We use either print_status_config() or
1539 * print_import_config() to print the top level logs then any log
1540 * children (eg mirrored slogs) are printed recursively - which
1541 * works because only the top level vdev is marked "is_log"
1544 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1549 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1553 (void) printf(gettext("\tlogs\n"));
1555 for (c = 0; c < children; c++) {
1556 uint64_t is_log = B_FALSE;
1559 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1563 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1565 print_status_config(zhp, name, child[c], namewidth,
1568 print_import_config(name, child[c], namewidth, 2);
1574 * Display the status for the given pool.
1577 show_import(nvlist_t *config)
1579 uint64_t pool_state;
1591 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1593 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1595 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1597 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1600 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1601 (uint64_t **)&vs, &vsc) == 0);
1602 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1604 reason = zpool_import_status(config, &msgid);
1606 (void) printf(gettext(" pool: %s\n"), name);
1607 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1608 (void) printf(gettext(" state: %s"), health);
1609 if (pool_state == POOL_STATE_DESTROYED)
1610 (void) printf(gettext(" (DESTROYED)"));
1611 (void) printf("\n");
1614 case ZPOOL_STATUS_MISSING_DEV_R:
1615 case ZPOOL_STATUS_MISSING_DEV_NR:
1616 case ZPOOL_STATUS_BAD_GUID_SUM:
1617 (void) printf(gettext(" status: One or more devices are "
1618 "missing from the system.\n"));
1621 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1622 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1623 (void) printf(gettext(" status: One or more devices contains "
1624 "corrupted data.\n"));
1627 case ZPOOL_STATUS_CORRUPT_DATA:
1629 gettext(" status: The pool data is corrupted.\n"));
1632 case ZPOOL_STATUS_OFFLINE_DEV:
1633 (void) printf(gettext(" status: One or more devices "
1634 "are offlined.\n"));
1637 case ZPOOL_STATUS_CORRUPT_POOL:
1638 (void) printf(gettext(" status: The pool metadata is "
1642 case ZPOOL_STATUS_VERSION_OLDER:
1643 (void) printf(gettext(" status: The pool is formatted using a "
1644 "legacy on-disk version.\n"));
1647 case ZPOOL_STATUS_VERSION_NEWER:
1648 (void) printf(gettext(" status: The pool is formatted using an "
1649 "incompatible version.\n"));
1652 case ZPOOL_STATUS_FEAT_DISABLED:
1653 (void) printf(gettext(" status: Some supported features are "
1654 "not enabled on the pool.\n"));
1657 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1658 (void) printf(gettext("status: The pool uses the following "
1659 "feature(s) not supported on this sytem:\n"));
1660 zpool_print_unsup_feat(config);
1663 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1664 (void) printf(gettext("status: The pool can only be accessed "
1665 "in read-only mode on this system. It\n\tcannot be "
1666 "accessed in read-write mode because it uses the "
1667 "following\n\tfeature(s) not supported on this system:\n"));
1668 zpool_print_unsup_feat(config);
1671 case ZPOOL_STATUS_HOSTID_MISMATCH:
1672 (void) printf(gettext(" status: The pool was last accessed by "
1673 "another system.\n"));
1676 case ZPOOL_STATUS_FAULTED_DEV_R:
1677 case ZPOOL_STATUS_FAULTED_DEV_NR:
1678 (void) printf(gettext(" status: One or more devices are "
1682 case ZPOOL_STATUS_BAD_LOG:
1683 (void) printf(gettext(" status: An intent log record cannot be "
1687 case ZPOOL_STATUS_RESILVERING:
1688 (void) printf(gettext(" status: One or more devices were being "
1694 * No other status can be seen when importing pools.
1696 assert(reason == ZPOOL_STATUS_OK);
1700 * Print out an action according to the overall state of the pool.
1702 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1703 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1704 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1705 (void) printf(gettext(" action: The pool can be "
1706 "imported using its name or numeric identifier, "
1707 "though\n\tsome features will not be available "
1708 "without an explicit 'zpool upgrade'.\n"));
1709 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1710 (void) printf(gettext(" action: The pool can be "
1711 "imported using its name or numeric "
1712 "identifier and\n\tthe '-f' flag.\n"));
1714 (void) printf(gettext(" action: The pool can be "
1715 "imported using its name or numeric "
1718 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1719 (void) printf(gettext(" action: The pool can be imported "
1720 "despite missing or damaged devices. The\n\tfault "
1721 "tolerance of the pool may be compromised if imported.\n"));
1724 case ZPOOL_STATUS_VERSION_NEWER:
1725 (void) printf(gettext(" action: The pool cannot be "
1726 "imported. Access the pool on a system running "
1727 "newer\n\tsoftware, or recreate the pool from "
1730 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1731 (void) printf(gettext("action: The pool cannot be "
1732 "imported. Access the pool on a system that "
1733 "supports\n\tthe required feature(s), or recreate "
1734 "the pool from backup.\n"));
1736 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1737 (void) printf(gettext("action: The pool cannot be "
1738 "imported in read-write mode. Import the pool "
1740 "\t\"-o readonly=on\", access the pool on a system "
1741 "that supports the\n\trequired feature(s), or "
1742 "recreate the pool from backup.\n"));
1744 case ZPOOL_STATUS_MISSING_DEV_R:
1745 case ZPOOL_STATUS_MISSING_DEV_NR:
1746 case ZPOOL_STATUS_BAD_GUID_SUM:
1747 (void) printf(gettext(" action: The pool cannot be "
1748 "imported. Attach the missing\n\tdevices and try "
1752 (void) printf(gettext(" action: The pool cannot be "
1753 "imported due to damaged devices or data.\n"));
1757 /* Print the comment attached to the pool. */
1758 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1759 (void) printf(gettext("comment: %s\n"), comment);
1762 * If the state is "closed" or "can't open", and the aux state
1763 * is "corrupt data":
1765 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1766 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1767 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1768 if (pool_state == POOL_STATE_DESTROYED)
1769 (void) printf(gettext("\tThe pool was destroyed, "
1770 "but can be imported using the '-Df' flags.\n"));
1771 else if (pool_state != POOL_STATE_EXPORTED)
1772 (void) printf(gettext("\tThe pool may be active on "
1773 "another system, but can be imported using\n\t"
1774 "the '-f' flag.\n"));
1778 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1781 (void) printf(gettext(" config:\n\n"));
1783 namewidth = max_width(NULL, nvroot, 0, 0);
1787 print_import_config(name, nvroot, namewidth, 0);
1788 if (num_logs(nvroot) > 0)
1789 print_logs(NULL, nvroot, namewidth, B_FALSE);
1791 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1792 (void) printf(gettext("\n\tAdditional devices are known to "
1793 "be part of this pool, though their\n\texact "
1794 "configuration cannot be determined.\n"));
1799 * Perform the import for the given configuration. This passes the heavy
1800 * lifting off to zpool_import_props(), and then mounts the datasets contained
1804 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1805 nvlist_t *props, int flags)
1807 zpool_handle_t *zhp;
1812 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1815 verify(nvlist_lookup_uint64(config,
1816 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1817 verify(nvlist_lookup_uint64(config,
1818 ZPOOL_CONFIG_VERSION, &version) == 0);
1819 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1820 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1821 "is formatted using an unsupported ZFS version\n"), name);
1823 } else if (state != POOL_STATE_EXPORTED &&
1824 !(flags & ZFS_IMPORT_ANY_HOST)) {
1827 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1829 if ((unsigned long)hostid != gethostid()) {
1834 verify(nvlist_lookup_string(config,
1835 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1836 verify(nvlist_lookup_uint64(config,
1837 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1839 (void) fprintf(stderr, gettext("cannot import "
1840 "'%s': pool may be in use from other "
1841 "system, it was last accessed by %s "
1842 "(hostid: 0x%lx) on %s"), name, hostname,
1843 (unsigned long)hostid,
1844 asctime(localtime(&t)));
1845 (void) fprintf(stderr, gettext("use '-f' to "
1846 "import anyway\n"));
1850 (void) fprintf(stderr, gettext("cannot import '%s': "
1851 "pool may be in use from other system\n"), name);
1852 (void) fprintf(stderr, gettext("use '-f' to import "
1858 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1861 if (newname != NULL)
1862 name = (char *)newname;
1864 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1867 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1868 !(flags & ZFS_IMPORT_ONLY) &&
1869 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1879 * zpool import [-d dir] [-D]
1880 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1881 * [-d dir | -c cachefile] [-f] -a
1882 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1883 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1885 * -c Read pool information from a cachefile instead of searching
1888 * -d Scan in a specific directory, other than /dev/dsk. More than
1889 * one directory can be specified using multiple '-d' options.
1891 * -D Scan for previously destroyed pools or import all or only
1892 * specified destroyed pools.
1894 * -R Temporarily import the pool, with all mountpoints relative to
1895 * the given root. The pool will remain exported when the machine
1898 * -V Import even in the presence of faulted vdevs. This is an
1899 * intentionally undocumented option for testing purposes, and
1900 * treats the pool configuration as complete, leaving any bad
1901 * vdevs in the FAULTED state. In other words, it does verbatim
1904 * -f Force import, even if it appears that the pool is active.
1906 * -F Attempt rewind if necessary.
1908 * -n See if rewind would work, but don't actually rewind.
1910 * -N Import the pool but don't mount datasets.
1912 * -T Specify a starting txg to use for import. This option is
1913 * intentionally undocumented option for testing purposes.
1915 * -a Import all pools found.
1917 * -o Set property=value and/or temporary mount options (without '=').
1919 * The import command scans for pools to import, and import pools based on pool
1920 * name and GUID. The pool can also be renamed as part of the import process.
1923 zpool_do_import(int argc, char **argv)
1925 char **searchdirs = NULL;
1929 nvlist_t *pools = NULL;
1930 boolean_t do_all = B_FALSE;
1931 boolean_t do_destroyed = B_FALSE;
1932 char *mntopts = NULL;
1935 uint64_t searchguid = 0;
1936 char *searchname = NULL;
1938 nvlist_t *found_config;
1939 nvlist_t *policy = NULL;
1940 nvlist_t *props = NULL;
1942 int flags = ZFS_IMPORT_NORMAL;
1943 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1944 boolean_t dryrun = B_FALSE;
1945 boolean_t do_rewind = B_FALSE;
1946 boolean_t xtreme_rewind = B_FALSE;
1947 uint64_t pool_state, txg = -1ULL;
1948 char *cachefile = NULL;
1949 importargs_t idata = { 0 };
1953 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1962 if (searchdirs == NULL) {
1963 searchdirs = safe_malloc(sizeof (char *));
1965 char **tmp = safe_malloc((nsearch + 1) *
1967 bcopy(searchdirs, tmp, nsearch *
1972 searchdirs[nsearch++] = optarg;
1975 do_destroyed = B_TRUE;
1978 flags |= ZFS_IMPORT_ANY_HOST;
1984 flags |= ZFS_IMPORT_MISSING_LOG;
1990 flags |= ZFS_IMPORT_ONLY;
1993 if ((propval = strchr(optarg, '=')) != NULL) {
1996 if (add_prop_list(optarg, propval,
2004 if (add_prop_list(zpool_prop_to_name(
2005 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2007 if (nvlist_lookup_string(props,
2008 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2011 if (add_prop_list(zpool_prop_to_name(
2012 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2017 txg = strtoull(optarg, &endptr, 10);
2018 if (errno != 0 || *endptr != '\0') {
2019 (void) fprintf(stderr,
2020 gettext("invalid txg value\n"));
2023 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2026 flags |= ZFS_IMPORT_VERBATIM;
2029 xtreme_rewind = B_TRUE;
2032 (void) fprintf(stderr, gettext("missing argument for "
2033 "'%c' option\n"), optopt);
2037 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2046 if (cachefile && nsearch != 0) {
2047 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2051 if ((dryrun || xtreme_rewind) && !do_rewind) {
2052 (void) fprintf(stderr,
2053 gettext("-n or -X only meaningful with -F\n"));
2057 rewind_policy = ZPOOL_TRY_REWIND;
2059 rewind_policy = ZPOOL_DO_REWIND;
2061 rewind_policy |= ZPOOL_EXTREME_REWIND;
2063 /* In the future, we can capture further policy and include it here */
2064 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2065 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2066 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2069 if (searchdirs == NULL) {
2070 searchdirs = safe_malloc(sizeof (char *));
2071 searchdirs[0] = "/dev";
2075 /* check argument count */
2078 (void) fprintf(stderr, gettext("too many arguments\n"));
2083 (void) fprintf(stderr, gettext("too many arguments\n"));
2088 * Check for the SYS_CONFIG privilege. We do this explicitly
2089 * here because otherwise any attempt to discover pools will
2092 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2093 (void) fprintf(stderr, gettext("cannot "
2094 "discover pools: permission denied\n"));
2096 nvlist_free(policy);
2102 * Depending on the arguments given, we do one of the following:
2104 * <none> Iterate through all pools and display information about
2107 * -a Iterate through all pools and try to import each one.
2109 * <id> Find the pool that corresponds to the given GUID/pool
2110 * name and import that one.
2112 * -D Above options applies only to destroyed pools.
2118 searchguid = strtoull(argv[0], &endptr, 10);
2119 if (errno != 0 || *endptr != '\0')
2120 searchname = argv[0];
2121 found_config = NULL;
2124 * User specified a name or guid. Ensure it's unique.
2126 idata.unique = B_TRUE;
2130 idata.path = searchdirs;
2131 idata.paths = nsearch;
2132 idata.poolname = searchname;
2133 idata.guid = searchguid;
2134 idata.cachefile = cachefile;
2136 pools = zpool_search_import(g_zfs, &idata);
2138 if (pools != NULL && idata.exists &&
2139 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2140 (void) fprintf(stderr, gettext("cannot import '%s': "
2141 "a pool with that name already exists\n"),
2143 (void) fprintf(stderr, gettext("use the form '%s "
2144 "<pool | id> <newpool>' to give it a new name\n"),
2147 } else if (pools == NULL && idata.exists) {
2148 (void) fprintf(stderr, gettext("cannot import '%s': "
2149 "a pool with that name is already created/imported,\n"),
2151 (void) fprintf(stderr, gettext("and no additional pools "
2152 "with that name were found\n"));
2154 } else if (pools == NULL) {
2156 (void) fprintf(stderr, gettext("cannot import '%s': "
2157 "no such pool available\n"), argv[0]);
2164 nvlist_free(policy);
2169 * At this point we have a list of import candidate configs. Even if
2170 * we were searching by pool name or guid, we still need to
2171 * post-process the list to deal with pool state and possible
2177 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2179 verify(nvpair_value_nvlist(elem, &config) == 0);
2181 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2183 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2185 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2188 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2195 (void) printf("\n");
2198 err |= do_import(config, NULL, mntopts,
2201 show_import(config);
2203 } else if (searchname != NULL) {
2207 * We are searching for a pool based on name.
2209 verify(nvlist_lookup_string(config,
2210 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2212 if (strcmp(name, searchname) == 0) {
2213 if (found_config != NULL) {
2214 (void) fprintf(stderr, gettext(
2215 "cannot import '%s': more than "
2216 "one matching pool\n"), searchname);
2217 (void) fprintf(stderr, gettext(
2218 "import by numeric ID instead\n"));
2221 found_config = config;
2227 * Search for a pool by guid.
2229 verify(nvlist_lookup_uint64(config,
2230 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2232 if (guid == searchguid)
2233 found_config = config;
2238 * If we were searching for a specific pool, verify that we found a
2239 * pool, and then do the import.
2241 if (argc != 0 && err == 0) {
2242 if (found_config == NULL) {
2243 (void) fprintf(stderr, gettext("cannot import '%s': "
2244 "no such pool available\n"), argv[0]);
2247 err |= do_import(found_config, argc == 1 ? NULL :
2248 argv[1], mntopts, props, flags);
2253 * If we were just looking for pools, report an error if none were
2256 if (argc == 0 && first)
2257 (void) fprintf(stderr,
2258 gettext("no pools available to import\n"));
2263 nvlist_free(policy);
2266 return (err ? 1 : 0);
2269 typedef struct iostat_cbdata {
2270 boolean_t cb_verbose;
2273 zpool_list_t *cb_list;
2277 print_iostat_separator(iostat_cbdata_t *cb)
2281 for (i = 0; i < cb->cb_namewidth; i++)
2283 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2287 print_iostat_header(iostat_cbdata_t *cb)
2289 (void) printf("%*s capacity operations bandwidth\n",
2290 cb->cb_namewidth, "");
2291 (void) printf("%-*s alloc free read write read write\n",
2292 cb->cb_namewidth, "pool");
2293 print_iostat_separator(cb);
2297 * Display a single statistic.
2300 print_one_stat(uint64_t value)
2304 zfs_nicenum(value, buf, sizeof (buf));
2305 (void) printf(" %5s", buf);
2309 * Print out all the statistics for the given vdev. This can either be the
2310 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2311 * is a verbose output, and we don't want to display the toplevel pool stats.
2314 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2315 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2317 nvlist_t **oldchild, **newchild;
2319 vdev_stat_t *oldvs, *newvs;
2320 vdev_stat_t zerovs = { 0 };
2325 if (oldnv != NULL) {
2326 verify(nvlist_lookup_uint64_array(oldnv,
2327 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2332 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2333 (uint64_t **)&newvs, &c) == 0);
2335 if (strlen(name) + depth > cb->cb_namewidth)
2336 (void) printf("%*s%s", depth, "", name);
2338 (void) printf("%*s%s%*s", depth, "", name,
2339 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2341 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2346 scale = (double)NANOSEC / tdelta;
2348 /* only toplevel vdevs have capacity stats */
2349 if (newvs->vs_space == 0) {
2350 (void) printf(" - -");
2352 print_one_stat(newvs->vs_alloc);
2353 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2356 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2357 oldvs->vs_ops[ZIO_TYPE_READ])));
2359 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2360 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2362 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2363 oldvs->vs_bytes[ZIO_TYPE_READ])));
2365 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2366 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2368 (void) printf("\n");
2370 if (!cb->cb_verbose)
2373 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2374 &newchild, &children) != 0)
2377 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2378 &oldchild, &c) != 0)
2381 for (c = 0; c < children; c++) {
2382 uint64_t ishole = B_FALSE, islog = B_FALSE;
2384 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2387 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2390 if (ishole || islog)
2393 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2394 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2395 newchild[c], cb, depth + 2);
2400 * Log device section
2403 if (num_logs(newnv) > 0) {
2404 (void) printf("%-*s - - - - - "
2405 "-\n", cb->cb_namewidth, "logs");
2407 for (c = 0; c < children; c++) {
2408 uint64_t islog = B_FALSE;
2409 (void) nvlist_lookup_uint64(newchild[c],
2410 ZPOOL_CONFIG_IS_LOG, &islog);
2413 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2415 print_vdev_stats(zhp, vname, oldnv ?
2416 oldchild[c] : NULL, newchild[c],
2425 * Include level 2 ARC devices in iostat output
2427 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2428 &newchild, &children) != 0)
2431 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2432 &oldchild, &c) != 0)
2436 (void) printf("%-*s - - - - - "
2437 "-\n", cb->cb_namewidth, "cache");
2438 for (c = 0; c < children; c++) {
2439 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2441 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2442 newchild[c], cb, depth + 2);
2449 refresh_iostat(zpool_handle_t *zhp, void *data)
2451 iostat_cbdata_t *cb = data;
2455 * If the pool has disappeared, remove it from the list and continue.
2457 if (zpool_refresh_stats(zhp, &missing) != 0)
2461 pool_list_remove(cb->cb_list, zhp);
2467 * Callback to print out the iostats for the given pool.
2470 print_iostat(zpool_handle_t *zhp, void *data)
2472 iostat_cbdata_t *cb = data;
2473 nvlist_t *oldconfig, *newconfig;
2474 nvlist_t *oldnvroot, *newnvroot;
2476 newconfig = zpool_get_config(zhp, &oldconfig);
2478 if (cb->cb_iteration == 1)
2481 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2484 if (oldconfig == NULL)
2487 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2491 * Print out the statistics for the pool.
2493 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2496 print_iostat_separator(cb);
2502 get_namewidth(zpool_handle_t *zhp, void *data)
2504 iostat_cbdata_t *cb = data;
2505 nvlist_t *config, *nvroot;
2507 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2508 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2510 if (!cb->cb_verbose)
2511 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2513 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2518 * The width must fall into the range [10,38]. The upper limit is the
2519 * maximum we can have and still fit in 80 columns.
2521 if (cb->cb_namewidth < 10)
2522 cb->cb_namewidth = 10;
2523 if (cb->cb_namewidth > 38)
2524 cb->cb_namewidth = 38;
2530 * Parse the input string, get the 'interval' and 'count' value if there is one.
2533 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2536 unsigned long interval = 0, count = 0;
2537 int argc = *argcp, errno;
2540 * Determine if the last argument is an integer or a pool name
2542 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2546 interval = strtoul(argv[argc - 1], &end, 10);
2548 if (*end == '\0' && errno == 0) {
2549 if (interval == 0) {
2550 (void) fprintf(stderr, gettext("interval "
2551 "cannot be zero\n"));
2555 * Ignore the last parameter
2560 * If this is not a valid number, just plow on. The
2561 * user will get a more informative error message later
2569 * If the last argument is also an integer, then we have both a count
2572 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2577 interval = strtoul(argv[argc - 1], &end, 10);
2579 if (*end == '\0' && errno == 0) {
2580 if (interval == 0) {
2581 (void) fprintf(stderr, gettext("interval "
2582 "cannot be zero\n"));
2587 * Ignore the last parameter
2601 get_timestamp_arg(char c)
2604 timestamp_fmt = UDATE;
2606 timestamp_fmt = DDATE;
2612 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2614 * -v Display statistics for individual vdevs
2615 * -T Display a timestamp in date(1) or Unix format
2617 * This command can be tricky because we want to be able to deal with pool
2618 * creation/destruction as well as vdev configuration changes. The bulk of this
2619 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2620 * on pool_list_update() to detect the addition of new pools. Configuration
2621 * changes are all handled within libzfs.
2624 zpool_do_iostat(int argc, char **argv)
2629 unsigned long interval = 0, count = 0;
2631 boolean_t verbose = B_FALSE;
2635 while ((c = getopt(argc, argv, "T:v")) != -1) {
2638 get_timestamp_arg(*optarg);
2644 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2653 get_interval_count(&argc, argv, &interval, &count);
2656 * Construct the list of all interesting pools.
2659 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2662 if (pool_list_count(list) == 0 && argc != 0) {
2663 pool_list_free(list);
2667 if (pool_list_count(list) == 0 && interval == 0) {
2668 pool_list_free(list);
2669 (void) fprintf(stderr, gettext("no pools available\n"));
2674 * Enter the main iostat loop.
2677 cb.cb_verbose = verbose;
2678 cb.cb_iteration = 0;
2679 cb.cb_namewidth = 0;
2682 pool_list_update(list);
2684 if ((npools = pool_list_count(list)) == 0)
2688 * Refresh all statistics. This is done as an explicit step
2689 * before calculating the maximum name width, so that any
2690 * configuration changes are properly accounted for.
2692 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2695 * Iterate over all pools to determine the maximum width
2696 * for the pool / device name column across all pools.
2698 cb.cb_namewidth = 0;
2699 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2701 if (timestamp_fmt != NODATE)
2702 print_timestamp(timestamp_fmt);
2705 * If it's the first time, or verbose mode, print the header.
2707 if (++cb.cb_iteration == 1 || verbose)
2708 print_iostat_header(&cb);
2710 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2713 * If there's more than one pool, and we're not in verbose mode
2714 * (which prints a separator for us), then print a separator.
2716 if (npools > 1 && !verbose)
2717 print_iostat_separator(&cb);
2720 (void) printf("\n");
2723 * Flush the output so that redirection to a file isn't buffered
2726 (void) fflush(stdout);
2731 if (count != 0 && --count == 0)
2734 (void) sleep(interval);
2737 pool_list_free(list);
2742 typedef struct list_cbdata {
2743 boolean_t cb_verbose;
2745 boolean_t cb_scripted;
2746 zprop_list_t *cb_proplist;
2750 * Given a list of columns to display, output appropriate headers for each one.
2753 print_header(list_cbdata_t *cb)
2755 zprop_list_t *pl = cb->cb_proplist;
2756 char headerbuf[ZPOOL_MAXPROPLEN];
2758 boolean_t first = B_TRUE;
2759 boolean_t right_justify;
2762 for (; pl != NULL; pl = pl->pl_next) {
2763 width = pl->pl_width;
2764 if (first && cb->cb_verbose) {
2766 * Reset the width to accommodate the verbose listing
2769 width = cb->cb_namewidth;
2777 right_justify = B_FALSE;
2778 if (pl->pl_prop != ZPROP_INVAL) {
2779 header = zpool_prop_column_name(pl->pl_prop);
2780 right_justify = zpool_prop_align_right(pl->pl_prop);
2784 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2785 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2786 headerbuf[i] = '\0';
2790 if (pl->pl_next == NULL && !right_justify)
2791 (void) printf("%s", header);
2792 else if (right_justify)
2793 (void) printf("%*s", width, header);
2795 (void) printf("%-*s", width, header);
2799 (void) printf("\n");
2803 * Given a pool and a list of properties, print out all the properties according
2804 * to the described layout.
2807 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2809 zprop_list_t *pl = cb->cb_proplist;
2810 boolean_t first = B_TRUE;
2811 char property[ZPOOL_MAXPROPLEN];
2813 boolean_t right_justify;
2816 for (; pl != NULL; pl = pl->pl_next) {
2818 width = pl->pl_width;
2819 if (first && cb->cb_verbose) {
2821 * Reset the width to accommodate the verbose listing
2824 width = cb->cb_namewidth;
2828 if (cb->cb_scripted)
2829 (void) printf("\t");
2836 right_justify = B_FALSE;
2837 if (pl->pl_prop != ZPROP_INVAL) {
2838 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2839 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2841 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2842 sizeof (property), NULL) != 0)
2847 right_justify = zpool_prop_align_right(pl->pl_prop);
2848 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2849 zpool_prop_unsupported(pl->pl_user_prop)) &&
2850 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2851 sizeof (property)) == 0) {
2859 * If this is being called in scripted mode, or if this is the
2860 * last column and it is left-justified, don't include a width
2863 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2864 (void) printf("%s", propstr);
2865 else if (right_justify)
2866 (void) printf("%*s", width, propstr);
2868 (void) printf("%-*s", width, propstr);
2871 (void) printf("\n");
2875 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2879 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2881 zfs_nicenum(value, propval, sizeof (propval));
2883 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2884 (void) strlcpy(propval, "-", sizeof (propval));
2887 (void) printf("\t%s", propval);
2889 (void) printf(" %*s", width, propval);
2893 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2894 list_cbdata_t *cb, int depth)
2900 boolean_t scripted = cb->cb_scripted;
2902 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2903 (uint64_t **)&vs, &c) == 0);
2907 (void) printf("\t%s", name);
2908 else if (strlen(name) + depth > cb->cb_namewidth)
2909 (void) printf("%*s%s", depth, "", name);
2911 (void) printf("%*s%s%*s", depth, "", name,
2912 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2914 /* only toplevel vdevs have capacity stats */
2915 if (vs->vs_space == 0) {
2917 (void) printf("\t-\t-\t-");
2919 (void) printf(" - - -");
2921 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2923 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2925 print_one_column(ZPOOL_PROP_FREE,
2926 vs->vs_space - vs->vs_alloc, scripted);
2928 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2930 (void) printf("\n");
2933 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2934 &child, &children) != 0)
2937 for (c = 0; c < children; c++) {
2938 uint64_t ishole = B_FALSE;
2940 if (nvlist_lookup_uint64(child[c],
2941 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2944 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2945 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2950 * Include level 2 ARC devices in iostat output
2952 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2953 &child, &children) != 0)
2957 (void) printf("%-*s - - - - - "
2958 "-\n", cb->cb_namewidth, "cache");
2959 for (c = 0; c < children; c++) {
2960 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2962 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2970 * Generic callback function to list a pool.
2973 list_callback(zpool_handle_t *zhp, void *data)
2975 list_cbdata_t *cbp = data;
2979 config = zpool_get_config(zhp, NULL);
2981 print_pool(zhp, cbp);
2982 if (!cbp->cb_verbose)
2985 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2987 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2993 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2995 * -H Scripted mode. Don't display headers, and separate properties
2997 * -o List of properties to display. Defaults to
2998 * "name,size,allocated,free,capacity,health,altroot"
2999 * -T Display a timestamp in date(1) or Unix format
3001 * List all pools in the system, whether or not they're healthy. Output space
3002 * statistics for each one, as well as health status summary.
3005 zpool_do_list(int argc, char **argv)
3009 list_cbdata_t cb = { 0 };
3010 static char default_props[] =
3011 "name,size,allocated,free,capacity,dedupratio,"
3013 char *props = default_props;
3014 unsigned long interval = 0, count = 0;
3016 boolean_t first = B_TRUE;
3019 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3022 cb.cb_scripted = B_TRUE;
3028 get_timestamp_arg(*optarg);
3031 cb.cb_verbose = B_TRUE;
3034 (void) fprintf(stderr, gettext("missing argument for "
3035 "'%c' option\n"), optopt);
3039 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3048 get_interval_count(&argc, argv, &interval, &count);
3050 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3053 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3056 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3057 (void) printf(gettext("no pools available\n"));
3058 zprop_free_list(cb.cb_proplist);
3063 pool_list_update(list);
3065 if (pool_list_count(list) == 0)
3068 cb.cb_namewidth = 0;
3069 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3071 if (timestamp_fmt != NODATE)
3072 print_timestamp(timestamp_fmt);
3074 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3078 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3083 if (count != 0 && --count == 0)
3086 (void) sleep(interval);
3089 zprop_free_list(cb.cb_proplist);
3094 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3101 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3102 &child, &children) != 0) {
3103 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3104 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3105 name += sizeof(_PATH_DEV) - 1;
3106 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3107 path += sizeof(_PATH_DEV) - 1;
3108 if (strcmp(name, path) == 0)
3113 for (c = 0; c < children; c++)
3114 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3121 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3123 boolean_t force = B_FALSE;
3126 char *poolname, *old_disk, *new_disk;
3127 zpool_handle_t *zhp;
3131 while ((c = getopt(argc, argv, "f")) != -1) {
3137 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3146 /* get pool name and check number of arguments */
3148 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3155 (void) fprintf(stderr,
3156 gettext("missing <device> specification\n"));
3164 (void) fprintf(stderr,
3165 gettext("missing <new_device> specification\n"));
3168 new_disk = old_disk;
3178 (void) fprintf(stderr, gettext("too many arguments\n"));
3182 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3185 if (zpool_get_config(zhp, NULL) == NULL) {
3186 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3192 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3194 if (nvroot == NULL) {
3199 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3201 nvlist_free(nvroot);
3208 * zpool replace [-f] <pool> <device> <new_device>
3210 * -f Force attach, even if <new_device> appears to be in use.
3212 * Replace <device> with <new_device>.
3216 zpool_do_replace(int argc, char **argv)
3218 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3222 * zpool attach [-f] <pool> <device> <new_device>
3224 * -f Force attach, even if <new_device> appears to be in use.
3226 * Attach <new_device> to the mirror containing <device>. If <device> is not
3227 * part of a mirror, then <device> will be transformed into a mirror of
3228 * <device> and <new_device>. In either case, <new_device> will begin life
3229 * with a DTL of [0, now], and will immediately begin to resilver itself.
3232 zpool_do_attach(int argc, char **argv)
3234 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3238 * zpool detach [-f] <pool> <device>
3240 * -f Force detach of <device>, even if DTLs argue against it
3241 * (not supported yet)
3243 * Detach a device from a mirror. The operation will be refused if <device>
3244 * is the last device in the mirror, or if the DTLs indicate that this device
3245 * has the only valid copy of some data.
3249 zpool_do_detach(int argc, char **argv)
3252 char *poolname, *path;
3253 zpool_handle_t *zhp;
3257 while ((c = getopt(argc, argv, "f")) != -1) {
3261 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3270 /* get pool name and check number of arguments */
3272 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3277 (void) fprintf(stderr,
3278 gettext("missing <device> specification\n"));
3285 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3288 ret = zpool_vdev_detach(zhp, path);
3296 * zpool split [-n] [-o prop=val] ...
3298 * [-R altroot] <pool> <newpool> [<device> ...]
3300 * -n Do not split the pool, but display the resulting layout if
3301 * it were to be split.
3302 * -o Set property=value, or set mount options.
3303 * -R Mount the split-off pool under an alternate root.
3305 * Splits the named pool and gives it the new pool name. Devices to be split
3306 * off may be listed, provided that no more than one device is specified
3307 * per top-level vdev mirror. The newly split pool is left in an exported
3308 * state unless -R is specified.
3310 * Restrictions: the top-level of the pool pool must only be made up of
3311 * mirrors; all devices in the pool must be healthy; no device may be
3312 * undergoing a resilvering operation.
3315 zpool_do_split(int argc, char **argv)
3317 char *srcpool, *newpool, *propval;
3318 char *mntopts = NULL;
3321 zpool_handle_t *zhp;
3322 nvlist_t *config, *props = NULL;
3324 flags.dryrun = B_FALSE;
3325 flags.import = B_FALSE;
3328 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3331 flags.import = B_TRUE;
3333 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3334 &props, B_TRUE) != 0) {
3341 flags.dryrun = B_TRUE;
3344 if ((propval = strchr(optarg, '=')) != NULL) {
3347 if (add_prop_list(optarg, propval,
3348 &props, B_TRUE) != 0) {
3358 (void) fprintf(stderr, gettext("missing argument for "
3359 "'%c' option\n"), optopt);
3363 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3370 if (!flags.import && mntopts != NULL) {
3371 (void) fprintf(stderr, gettext("setting mntopts is only "
3372 "valid when importing the pool\n"));
3380 (void) fprintf(stderr, gettext("Missing pool name\n"));
3384 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3394 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3397 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3398 if (config == NULL) {
3402 (void) printf(gettext("would create '%s' with the "
3403 "following layout:\n\n"), newpool);
3404 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3406 nvlist_free(config);
3411 if (ret != 0 || flags.dryrun || !flags.import)
3415 * The split was successful. Now we need to open the new
3416 * pool and import it.
3418 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3420 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3421 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3423 (void) fprintf(stderr, gettext("Split was successful, but "
3424 "the datasets could not all be mounted\n"));
3425 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3426 "different altroot\n"), "zpool import");
3436 * zpool online <pool> <device> ...
3439 zpool_do_online(int argc, char **argv)
3443 zpool_handle_t *zhp;
3445 vdev_state_t newstate;
3449 while ((c = getopt(argc, argv, "et")) != -1) {
3452 flags |= ZFS_ONLINE_EXPAND;
3456 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3465 /* get pool name and check number of arguments */
3467 (void) fprintf(stderr, gettext("missing pool name\n"));
3471 (void) fprintf(stderr, gettext("missing device name\n"));
3477 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3480 for (i = 1; i < argc; i++) {
3481 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3482 if (newstate != VDEV_STATE_HEALTHY) {
3483 (void) printf(gettext("warning: device '%s' "
3484 "onlined, but remains in faulted state\n"),
3486 if (newstate == VDEV_STATE_FAULTED)
3487 (void) printf(gettext("use 'zpool "
3488 "clear' to restore a faulted "
3491 (void) printf(gettext("use 'zpool "
3492 "replace' to replace devices "
3493 "that are no longer present\n"));
3506 * zpool offline [-ft] <pool> <device> ...
3508 * -f Force the device into the offline state, even if doing
3509 * so would appear to compromise pool availability.
3510 * (not supported yet)
3512 * -t Only take the device off-line temporarily. The offline
3513 * state will not be persistent across reboots.
3517 zpool_do_offline(int argc, char **argv)
3521 zpool_handle_t *zhp;
3523 boolean_t istmp = B_FALSE;
3526 while ((c = getopt(argc, argv, "ft")) != -1) {
3533 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3542 /* get pool name and check number of arguments */
3544 (void) fprintf(stderr, gettext("missing pool name\n"));
3548 (void) fprintf(stderr, gettext("missing device name\n"));
3554 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3557 for (i = 1; i < argc; i++) {
3558 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3568 * zpool clear <pool> [device]
3570 * Clear all errors associated with a pool or a particular device.
3573 zpool_do_clear(int argc, char **argv)
3577 boolean_t dryrun = B_FALSE;
3578 boolean_t do_rewind = B_FALSE;
3579 boolean_t xtreme_rewind = B_FALSE;
3580 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3581 nvlist_t *policy = NULL;
3582 zpool_handle_t *zhp;
3583 char *pool, *device;
3586 while ((c = getopt(argc, argv, "FnX")) != -1) {
3595 xtreme_rewind = B_TRUE;
3598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3608 (void) fprintf(stderr, gettext("missing pool name\n"));
3613 (void) fprintf(stderr, gettext("too many arguments\n"));
3617 if ((dryrun || xtreme_rewind) && !do_rewind) {
3618 (void) fprintf(stderr,
3619 gettext("-n or -X only meaningful with -F\n"));
3623 rewind_policy = ZPOOL_TRY_REWIND;
3625 rewind_policy = ZPOOL_DO_REWIND;
3627 rewind_policy |= ZPOOL_EXTREME_REWIND;
3629 /* In future, further rewind policy choices can be passed along here */
3630 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3631 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3635 device = argc == 2 ? argv[1] : NULL;
3637 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3638 nvlist_free(policy);
3642 if (zpool_clear(zhp, device, policy) != 0)
3647 nvlist_free(policy);
3653 * zpool reguid <pool>
3656 zpool_do_reguid(int argc, char **argv)
3660 zpool_handle_t *zhp;
3664 while ((c = getopt(argc, argv, "")) != -1) {
3667 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3676 /* get pool name and check number of arguments */
3678 (void) fprintf(stderr, gettext("missing pool name\n"));
3683 (void) fprintf(stderr, gettext("too many arguments\n"));
3688 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3691 ret = zpool_reguid(zhp);
3699 * zpool reopen <pool>
3701 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3703 * NOTE: This command is currently undocumented. If the command is ever
3704 * exposed then the appropriate usage() messages will need to be made.
3707 zpool_do_reopen(int argc, char **argv)
3710 zpool_handle_t *zhp;
3720 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3723 ret = zpool_reopen(zhp);
3728 typedef struct scrub_cbdata {
3735 scrub_callback(zpool_handle_t *zhp, void *data)
3737 scrub_cbdata_t *cb = data;
3741 * Ignore faulted pools.
3743 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3744 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3745 "currently unavailable\n"), zpool_get_name(zhp));
3749 err = zpool_scan(zhp, cb->cb_type);
3755 * zpool scrub [-s] <pool> ...
3757 * -s Stop. Stops any in-progress scrub.
3760 zpool_do_scrub(int argc, char **argv)
3765 cb.cb_type = POOL_SCAN_SCRUB;
3768 while ((c = getopt(argc, argv, "s")) != -1) {
3771 cb.cb_type = POOL_SCAN_NONE;
3774 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3786 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3790 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3793 typedef struct status_cbdata {
3795 boolean_t cb_allpools;
3796 boolean_t cb_verbose;
3797 boolean_t cb_explain;
3799 boolean_t cb_dedup_stats;
3803 * Print out detailed scrub status.
3806 print_scan_status(pool_scan_stat_t *ps)
3809 uint64_t elapsed, mins_left, hours_left;
3810 uint64_t pass_exam, examined, total;
3812 double fraction_done;
3813 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3815 (void) printf(gettext(" scan: "));
3817 /* If there's never been a scan, there's not much to say. */
3818 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3819 ps->pss_func >= POOL_SCAN_FUNCS) {
3820 (void) printf(gettext("none requested\n"));
3824 start = ps->pss_start_time;
3825 end = ps->pss_end_time;
3826 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3828 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3829 ps->pss_func == POOL_SCAN_RESILVER);
3831 * Scan is finished or canceled.
3833 if (ps->pss_state == DSS_FINISHED) {
3834 uint64_t minutes_taken = (end - start) / 60;
3837 if (ps->pss_func == POOL_SCAN_SCRUB) {
3838 fmt = gettext("scrub repaired %s in %lluh%um with "
3839 "%llu errors on %s");
3840 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3841 fmt = gettext("resilvered %s in %lluh%um with "
3842 "%llu errors on %s");
3845 (void) printf(fmt, processed_buf,
3846 (u_longlong_t)(minutes_taken / 60),
3847 (uint_t)(minutes_taken % 60),
3848 (u_longlong_t)ps->pss_errors,
3849 ctime((time_t *)&end));
3851 } else if (ps->pss_state == DSS_CANCELED) {
3852 if (ps->pss_func == POOL_SCAN_SCRUB) {
3853 (void) printf(gettext("scrub canceled on %s"),
3855 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3856 (void) printf(gettext("resilver canceled on %s"),
3862 assert(ps->pss_state == DSS_SCANNING);
3865 * Scan is in progress.
3867 if (ps->pss_func == POOL_SCAN_SCRUB) {
3868 (void) printf(gettext("scrub in progress since %s"),
3870 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3871 (void) printf(gettext("resilver in progress since %s"),
3875 examined = ps->pss_examined ? ps->pss_examined : 1;
3876 total = ps->pss_to_examine;
3877 fraction_done = (double)examined / total;
3879 /* elapsed time for this pass */
3880 elapsed = time(NULL) - ps->pss_pass_start;
3881 elapsed = elapsed ? elapsed : 1;
3882 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3883 rate = pass_exam / elapsed;
3884 rate = rate ? rate : 1;
3885 mins_left = ((total - examined) / rate) / 60;
3886 hours_left = mins_left / 60;
3888 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3889 zfs_nicenum(total, total_buf, sizeof (total_buf));
3890 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3893 * do not print estimated time if hours_left is more than 30 days
3895 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3896 examined_buf, total_buf, rate_buf);
3897 if (hours_left < (30 * 24)) {
3898 (void) printf(gettext(", %lluh%um to go\n"),
3899 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3901 (void) printf(gettext(
3902 ", (scan is slow, no estimated time)\n"));
3905 if (ps->pss_func == POOL_SCAN_RESILVER) {
3906 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3907 processed_buf, 100 * fraction_done);
3908 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3909 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3910 processed_buf, 100 * fraction_done);
3915 print_error_log(zpool_handle_t *zhp)
3917 nvlist_t *nverrlist = NULL;
3920 size_t len = MAXPATHLEN * 2;
3922 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3923 (void) printf("errors: List of errors unavailable "
3924 "(insufficient privileges)\n");
3928 (void) printf("errors: Permanent errors have been "
3929 "detected in the following files:\n\n");
3931 pathname = safe_malloc(len);
3933 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3935 uint64_t dsobj, obj;
3937 verify(nvpair_value_nvlist(elem, &nv) == 0);
3938 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3940 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3942 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3943 (void) printf("%7s %s\n", "", pathname);
3946 nvlist_free(nverrlist);
3950 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3959 (void) printf(gettext("\tspares\n"));
3961 for (i = 0; i < nspares; i++) {
3962 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3963 print_status_config(zhp, name, spares[i],
3964 namewidth, 2, B_TRUE);
3970 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3979 (void) printf(gettext("\tcache\n"));
3981 for (i = 0; i < nl2cache; i++) {
3982 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3983 print_status_config(zhp, name, l2cache[i],
3984 namewidth, 2, B_FALSE);
3990 print_dedup_stats(nvlist_t *config)
3992 ddt_histogram_t *ddh;
3998 * If the pool was faulted then we may not have been able to
3999 * obtain the config. Otherwise, if have anything in the dedup
4000 * table continue processing the stats.
4002 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4003 (uint64_t **)&ddo, &c) != 0)
4006 (void) printf("\n");
4007 (void) printf(gettext(" dedup: "));
4008 if (ddo->ddo_count == 0) {
4009 (void) printf(gettext("no DDT entries\n"));
4013 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4014 (u_longlong_t)ddo->ddo_count,
4015 (u_longlong_t)ddo->ddo_dspace,
4016 (u_longlong_t)ddo->ddo_mspace);
4018 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4019 (uint64_t **)&dds, &c) == 0);
4020 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4021 (uint64_t **)&ddh, &c) == 0);
4022 zpool_dump_ddt(dds, ddh);
4026 * Display a summary of pool status. Displays a summary such as:
4030 * reason: One or more devices ...
4031 * see: http://illumos.org/msg/ZFS-xxxx-01
4037 * When given the '-v' option, we print out the complete config. If the '-e'
4038 * option is specified, then we print out error rate information as well.
4041 status_callback(zpool_handle_t *zhp, void *data)
4043 status_cbdata_t *cbp = data;
4044 nvlist_t *config, *nvroot;
4051 config = zpool_get_config(zhp, NULL);
4052 reason = zpool_get_status(zhp, &msgid);
4057 * If we were given 'zpool status -x', only report those pools with
4060 if (cbp->cb_explain &&
4061 (reason == ZPOOL_STATUS_OK ||
4062 reason == ZPOOL_STATUS_VERSION_OLDER ||
4063 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4064 if (!cbp->cb_allpools) {
4065 (void) printf(gettext("pool '%s' is healthy\n"),
4066 zpool_get_name(zhp));
4068 cbp->cb_first = B_FALSE;
4074 cbp->cb_first = B_FALSE;
4076 (void) printf("\n");
4078 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4080 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4081 (uint64_t **)&vs, &c) == 0);
4082 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4084 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4085 (void) printf(gettext(" state: %s\n"), health);
4088 case ZPOOL_STATUS_MISSING_DEV_R:
4089 (void) printf(gettext("status: One or more devices could not "
4090 "be opened. Sufficient replicas exist for\n\tthe pool to "
4091 "continue functioning in a degraded state.\n"));
4092 (void) printf(gettext("action: Attach the missing device and "
4093 "online it using 'zpool online'.\n"));
4096 case ZPOOL_STATUS_MISSING_DEV_NR:
4097 (void) printf(gettext("status: One or more devices could not "
4098 "be opened. There are insufficient\n\treplicas for the "
4099 "pool to continue functioning.\n"));
4100 (void) printf(gettext("action: Attach the missing device and "
4101 "online it using 'zpool online'.\n"));
4104 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4105 (void) printf(gettext("status: One or more devices could not "
4106 "be used because the label is missing or\n\tinvalid. "
4107 "Sufficient replicas exist for the pool to continue\n\t"
4108 "functioning in a degraded state.\n"));
4109 (void) printf(gettext("action: Replace the device using "
4110 "'zpool replace'.\n"));
4113 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4114 (void) printf(gettext("status: One or more devices could not "
4115 "be used because the label is missing \n\tor invalid. "
4116 "There are insufficient replicas for the pool to "
4117 "continue\n\tfunctioning.\n"));
4118 zpool_explain_recover(zpool_get_handle(zhp),
4119 zpool_get_name(zhp), reason, config);
4122 case ZPOOL_STATUS_FAILING_DEV:
4123 (void) printf(gettext("status: One or more devices has "
4124 "experienced an unrecoverable error. An\n\tattempt was "
4125 "made to correct the error. Applications are "
4127 (void) printf(gettext("action: Determine if the device needs "
4128 "to be replaced, and clear the errors\n\tusing "
4129 "'zpool clear' or replace the device with 'zpool "
4133 case ZPOOL_STATUS_OFFLINE_DEV:
4134 (void) printf(gettext("status: One or more devices has "
4135 "been taken offline by the administrator.\n\tSufficient "
4136 "replicas exist for the pool to continue functioning in "
4137 "a\n\tdegraded state.\n"));
4138 (void) printf(gettext("action: Online the device using "
4139 "'zpool online' or replace the device with\n\t'zpool "
4143 case ZPOOL_STATUS_REMOVED_DEV:
4144 (void) printf(gettext("status: One or more devices has "
4145 "been removed by the administrator.\n\tSufficient "
4146 "replicas exist for the pool to continue functioning in "
4147 "a\n\tdegraded state.\n"));
4148 (void) printf(gettext("action: Online the device using "
4149 "'zpool online' or replace the device with\n\t'zpool "
4153 case ZPOOL_STATUS_RESILVERING:
4154 (void) printf(gettext("status: One or more devices is "
4155 "currently being resilvered. The pool will\n\tcontinue "
4156 "to function, possibly in a degraded state.\n"));
4157 (void) printf(gettext("action: Wait for the resilver to "
4161 case ZPOOL_STATUS_CORRUPT_DATA:
4162 (void) printf(gettext("status: One or more devices has "
4163 "experienced an error resulting in data\n\tcorruption. "
4164 "Applications may be affected.\n"));
4165 (void) printf(gettext("action: Restore the file in question "
4166 "if possible. Otherwise restore the\n\tentire pool from "
4170 case ZPOOL_STATUS_CORRUPT_POOL:
4171 (void) printf(gettext("status: The pool metadata is corrupted "
4172 "and the pool cannot be opened.\n"));
4173 zpool_explain_recover(zpool_get_handle(zhp),
4174 zpool_get_name(zhp), reason, config);
4177 case ZPOOL_STATUS_VERSION_OLDER:
4178 (void) printf(gettext("status: The pool is formatted using a "
4179 "legacy on-disk format. The pool can\n\tstill be used, "
4180 "but some features are unavailable.\n"));
4181 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4182 "upgrade'. Once this is done, the\n\tpool will no longer "
4183 "be accessible on software that does not support feature\n"
4187 case ZPOOL_STATUS_VERSION_NEWER:
4188 (void) printf(gettext("status: The pool has been upgraded to a "
4189 "newer, incompatible on-disk version.\n\tThe pool cannot "
4190 "be accessed on this system.\n"));
4191 (void) printf(gettext("action: Access the pool from a system "
4192 "running more recent software, or\n\trestore the pool from "
4196 case ZPOOL_STATUS_FEAT_DISABLED:
4197 (void) printf(gettext("status: Some supported features are not "
4198 "enabled on the pool. The pool can\n\tstill be used, but "
4199 "some features are unavailable.\n"));
4200 (void) printf(gettext("action: Enable all features using "
4201 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4202 "longer be accessible by software that does not support\n\t"
4203 "the features. See zpool-features(7) for details.\n"));
4206 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4207 (void) printf(gettext("status: The pool cannot be accessed on "
4208 "this system because it uses the\n\tfollowing feature(s) "
4209 "not supported on this system:\n"));
4210 zpool_print_unsup_feat(config);
4211 (void) printf("\n");
4212 (void) printf(gettext("action: Access the pool from a system "
4213 "that supports the required feature(s),\n\tor restore the "
4214 "pool from backup.\n"));
4217 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4218 (void) printf(gettext("status: The pool can only be accessed "
4219 "in read-only mode on this system. It\n\tcannot be "
4220 "accessed in read-write mode because it uses the "
4221 "following\n\tfeature(s) not supported on this system:\n"));
4222 zpool_print_unsup_feat(config);
4223 (void) printf("\n");
4224 (void) printf(gettext("action: The pool cannot be accessed in "
4225 "read-write mode. Import the pool with\n"
4226 "\t\"-o readonly=on\", access the pool from a system that "
4227 "supports the\n\trequired feature(s), or restore the "
4228 "pool from backup.\n"));
4231 case ZPOOL_STATUS_FAULTED_DEV_R:
4232 (void) printf(gettext("status: One or more devices are "
4233 "faulted in response to persistent errors.\n\tSufficient "
4234 "replicas exist for the pool to continue functioning "
4235 "in a\n\tdegraded state.\n"));
4236 (void) printf(gettext("action: Replace the faulted device, "
4237 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4240 case ZPOOL_STATUS_FAULTED_DEV_NR:
4241 (void) printf(gettext("status: One or more devices are "
4242 "faulted in response to persistent errors. There are "
4243 "insufficient replicas for the pool to\n\tcontinue "
4245 (void) printf(gettext("action: Destroy and re-create the pool "
4246 "from a backup source. Manually marking the device\n"
4247 "\trepaired using 'zpool clear' may allow some data "
4248 "to be recovered.\n"));
4251 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4252 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4253 (void) printf(gettext("status: One or more devices are "
4254 "faulted in response to IO failures.\n"));
4255 (void) printf(gettext("action: Make sure the affected devices "
4256 "are connected, then run 'zpool clear'.\n"));
4259 case ZPOOL_STATUS_BAD_LOG:
4260 (void) printf(gettext("status: An intent log record "
4261 "could not be read.\n"
4262 "\tWaiting for adminstrator intervention to fix the "
4263 "faulted pool.\n"));
4264 (void) printf(gettext("action: Either restore the affected "
4265 "device(s) and run 'zpool online',\n"
4266 "\tor ignore the intent log records by running "
4267 "'zpool clear'.\n"));
4272 * The remaining errors can't actually be generated, yet.
4274 assert(reason == ZPOOL_STATUS_OK);
4278 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4281 if (config != NULL) {
4284 nvlist_t **spares, **l2cache;
4285 uint_t nspares, nl2cache;
4286 pool_scan_stat_t *ps = NULL;
4288 (void) nvlist_lookup_uint64_array(nvroot,
4289 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4290 print_scan_status(ps);
4292 namewidth = max_width(zhp, nvroot, 0, 0);
4296 (void) printf(gettext("config:\n\n"));
4297 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4298 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4299 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4300 namewidth, 0, B_FALSE);
4302 if (num_logs(nvroot) > 0)
4303 print_logs(zhp, nvroot, namewidth, B_TRUE);
4304 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4305 &l2cache, &nl2cache) == 0)
4306 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4308 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4309 &spares, &nspares) == 0)
4310 print_spares(zhp, spares, nspares, namewidth);
4312 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4314 nvlist_t *nverrlist = NULL;
4317 * If the approximate error count is small, get a
4318 * precise count by fetching the entire log and
4319 * uniquifying the results.
4321 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4322 zpool_get_errlog(zhp, &nverrlist) == 0) {
4327 while ((elem = nvlist_next_nvpair(nverrlist,
4332 nvlist_free(nverrlist);
4334 (void) printf("\n");
4337 (void) printf(gettext("errors: No known data "
4339 else if (!cbp->cb_verbose)
4340 (void) printf(gettext("errors: %llu data "
4341 "errors, use '-v' for a list\n"),
4342 (u_longlong_t)nerr);
4344 print_error_log(zhp);
4347 if (cbp->cb_dedup_stats)
4348 print_dedup_stats(config);
4350 (void) printf(gettext("config: The configuration cannot be "
4358 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4360 * -v Display complete error logs
4361 * -x Display only pools with potential problems
4362 * -D Display dedup status (undocumented)
4363 * -T Display a timestamp in date(1) or Unix format
4365 * Describes the health status of all pools or some subset.
4368 zpool_do_status(int argc, char **argv)
4372 unsigned long interval = 0, count = 0;
4373 status_cbdata_t cb = { 0 };
4376 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4379 cb.cb_verbose = B_TRUE;
4382 cb.cb_explain = B_TRUE;
4385 cb.cb_dedup_stats = B_TRUE;
4388 get_timestamp_arg(*optarg);
4391 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4400 get_interval_count(&argc, argv, &interval, &count);
4403 cb.cb_allpools = B_TRUE;
4405 cb.cb_first = B_TRUE;
4408 if (timestamp_fmt != NODATE)
4409 print_timestamp(timestamp_fmt);
4411 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4412 status_callback, &cb);
4414 if (argc == 0 && cb.cb_count == 0)
4415 (void) printf(gettext("no pools available\n"));
4416 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4417 (void) printf(gettext("all pools are healthy\n"));
4425 if (count != 0 && --count == 0)
4428 (void) sleep(interval);
4434 typedef struct upgrade_cbdata {
4436 char cb_poolname[ZPOOL_MAXNAMELEN];
4438 uint64_t cb_version;
4444 is_root_pool(zpool_handle_t *zhp)
4446 static struct statfs sfs;
4447 static char *poolname = NULL;
4448 static boolean_t stated = B_FALSE;
4453 if (statfs("/", &sfs) == -1) {
4454 (void) fprintf(stderr,
4455 "Unable to stat root file system: %s.\n",
4459 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4461 poolname = sfs.f_mntfromname;
4462 if ((slash = strchr(poolname, '/')) != NULL)
4465 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4469 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4471 if (poolname[0] == '\0' && is_root_pool(zhp))
4472 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4474 #endif /* FreeBSD */
4477 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4481 uint64_t oldversion;
4483 config = zpool_get_config(zhp, NULL);
4484 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4487 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4488 assert(oldversion < version);
4490 ret = zpool_upgrade(zhp, version);
4494 if (version >= SPA_VERSION_FEATURES) {
4495 (void) printf(gettext("Successfully upgraded "
4496 "'%s' from version %llu to feature flags.\n"),
4497 zpool_get_name(zhp), oldversion);
4499 (void) printf(gettext("Successfully upgraded "
4500 "'%s' from version %llu to version %llu.\n"),
4501 zpool_get_name(zhp), oldversion, version);
4508 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4511 boolean_t firstff = B_TRUE;
4512 nvlist_t *enabled = zpool_get_features(zhp);
4515 for (i = 0; i < SPA_FEATURES; i++) {
4516 const char *fname = spa_feature_table[i].fi_uname;
4517 const char *fguid = spa_feature_table[i].fi_guid;
4518 if (!nvlist_exists(enabled, fguid)) {
4520 verify(-1 != asprintf(&propname, "feature@%s", fname));
4521 ret = zpool_set_prop(zhp, propname,
4522 ZFS_FEATURE_ENABLED);
4530 (void) printf(gettext("Enabled the "
4531 "following features on '%s':\n"),
4532 zpool_get_name(zhp));
4535 (void) printf(gettext(" %s\n"), fname);
4546 upgrade_cb(zpool_handle_t *zhp, void *arg)
4548 upgrade_cbdata_t *cbp = arg;
4551 boolean_t printnl = B_FALSE;
4554 config = zpool_get_config(zhp, NULL);
4555 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4558 assert(SPA_VERSION_IS_SUPPORTED(version));
4560 if (version < cbp->cb_version) {
4561 cbp->cb_first = B_FALSE;
4562 ret = upgrade_version(zhp, cbp->cb_version);
4566 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4567 sizeof(cbp->cb_poolname));
4568 #endif /* ___FreeBSD__ */
4573 * If they did "zpool upgrade -a", then we could
4574 * be doing ioctls to different pools. We need
4575 * to log this history once to each pool, and bypass
4576 * the normal history logging that happens in main().
4578 (void) zpool_log_history(g_zfs, history_str);
4579 log_history = B_FALSE;
4583 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4585 ret = upgrade_enable_all(zhp, &count);
4590 cbp->cb_first = B_FALSE;
4593 * If they did "zpool upgrade -a", then we could
4594 * be doing ioctls to different pools. We need
4595 * to log this history once to each pool, and bypass
4596 * the normal history logging that happens in main().
4598 (void) zpool_log_history(g_zfs, history_str);
4599 log_history = B_FALSE;
4604 (void) printf(gettext("\n"));
4611 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4613 upgrade_cbdata_t *cbp = arg;
4617 config = zpool_get_config(zhp, NULL);
4618 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4621 assert(SPA_VERSION_IS_SUPPORTED(version));
4623 if (version < SPA_VERSION_FEATURES) {
4624 if (cbp->cb_first) {
4625 (void) printf(gettext("The following pools are "
4626 "formatted with legacy version numbers and can\n"
4627 "be upgraded to use feature flags. After "
4628 "being upgraded, these pools\nwill no "
4629 "longer be accessible by software that does not "
4630 "support feature\nflags.\n\n"));
4631 (void) printf(gettext("VER POOL\n"));
4632 (void) printf(gettext("--- ------------\n"));
4633 cbp->cb_first = B_FALSE;
4636 (void) printf("%2llu %s\n", (u_longlong_t)version,
4637 zpool_get_name(zhp));
4644 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4646 upgrade_cbdata_t *cbp = arg;
4650 config = zpool_get_config(zhp, NULL);
4651 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4654 if (version >= SPA_VERSION_FEATURES) {
4656 boolean_t poolfirst = B_TRUE;
4657 nvlist_t *enabled = zpool_get_features(zhp);
4659 for (i = 0; i < SPA_FEATURES; i++) {
4660 const char *fguid = spa_feature_table[i].fi_guid;
4661 const char *fname = spa_feature_table[i].fi_uname;
4662 if (!nvlist_exists(enabled, fguid)) {
4663 if (cbp->cb_first) {
4664 (void) printf(gettext("\nSome "
4665 "supported features are not "
4666 "enabled on the following pools. "
4667 "Once a\nfeature is enabled the "
4668 "pool may become incompatible with "
4669 "software\nthat does not support "
4671 "zpool-features(7) for "
4673 (void) printf(gettext("POOL "
4675 (void) printf(gettext("------"
4677 cbp->cb_first = B_FALSE;
4681 (void) printf(gettext("%s\n"),
4682 zpool_get_name(zhp));
4683 poolfirst = B_FALSE;
4686 (void) printf(gettext(" %s\n"), fname);
4696 upgrade_one(zpool_handle_t *zhp, void *data)
4698 boolean_t printnl = B_FALSE;
4699 upgrade_cbdata_t *cbp = data;
4700 uint64_t cur_version;
4703 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4704 (void) printf(gettext("'log' is now a reserved word\n"
4705 "Pool 'log' must be renamed using export and import"
4710 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4711 if (cur_version > cbp->cb_version) {
4712 (void) printf(gettext("Pool '%s' is already formatted "
4713 "using more current version '%llu'.\n\n"),
4714 zpool_get_name(zhp), cur_version);
4718 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4719 (void) printf(gettext("Pool '%s' is already formatted "
4720 "using version %llu.\n\n"), zpool_get_name(zhp),
4725 if (cur_version != cbp->cb_version) {
4727 ret = upgrade_version(zhp, cbp->cb_version);
4731 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4732 sizeof(cbp->cb_poolname));
4733 #endif /* ___FreeBSD__ */
4736 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4738 ret = upgrade_enable_all(zhp, &count);
4745 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4746 sizeof(cbp->cb_poolname));
4747 #endif /* __FreeBSD __*/
4748 } else if (cur_version == SPA_VERSION) {
4749 (void) printf(gettext("Pool '%s' already has all "
4750 "supported features enabled.\n"),
4751 zpool_get_name(zhp));
4756 (void) printf(gettext("\n"));
4765 * zpool upgrade [-V version] <-a | pool ...>
4767 * With no arguments, display downrev'd ZFS pool available for upgrade.
4768 * Individual pools can be upgraded by specifying the pool, and '-a' will
4769 * upgrade all pools.
4772 zpool_do_upgrade(int argc, char **argv)
4775 upgrade_cbdata_t cb = { 0 };
4777 boolean_t showversions = B_FALSE;
4778 boolean_t upgradeall = B_FALSE;
4783 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4786 upgradeall = B_TRUE;
4789 showversions = B_TRUE;
4792 cb.cb_version = strtoll(optarg, &end, 10);
4794 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4795 (void) fprintf(stderr,
4796 gettext("invalid version '%s'\n"), optarg);
4801 (void) fprintf(stderr, gettext("missing argument for "
4802 "'%c' option\n"), optopt);
4806 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4817 if (cb.cb_version == 0) {
4818 cb.cb_version = SPA_VERSION;
4819 } else if (!upgradeall && argc == 0) {
4820 (void) fprintf(stderr, gettext("-V option is "
4821 "incompatible with other arguments\n"));
4826 if (upgradeall || argc != 0) {
4827 (void) fprintf(stderr, gettext("-v option is "
4828 "incompatible with other arguments\n"));
4831 } else if (upgradeall) {
4833 (void) fprintf(stderr, gettext("-a option should not "
4834 "be used along with a pool name\n"));
4839 (void) printf(gettext("This system supports ZFS pool feature "
4844 (void) printf(gettext("The following features are "
4846 (void) printf(gettext("FEAT DESCRIPTION\n"));
4847 (void) printf("----------------------------------------------"
4848 "---------------\n");
4849 for (i = 0; i < SPA_FEATURES; i++) {
4850 zfeature_info_t *fi = &spa_feature_table[i];
4851 const char *ro = fi->fi_can_readonly ?
4852 " (read-only compatible)" : "";
4854 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4855 (void) printf(" %s\n", fi->fi_desc);
4857 (void) printf("\n");
4859 (void) printf(gettext("The following legacy versions are also "
4861 (void) printf(gettext("VER DESCRIPTION\n"));
4862 (void) printf("--- -----------------------------------------"
4863 "---------------\n");
4864 (void) printf(gettext(" 1 Initial ZFS version\n"));
4865 (void) printf(gettext(" 2 Ditto blocks "
4866 "(replicated metadata)\n"));
4867 (void) printf(gettext(" 3 Hot spares and double parity "
4869 (void) printf(gettext(" 4 zpool history\n"));
4870 (void) printf(gettext(" 5 Compression using the gzip "
4872 (void) printf(gettext(" 6 bootfs pool property\n"));
4873 (void) printf(gettext(" 7 Separate intent log devices\n"));
4874 (void) printf(gettext(" 8 Delegated administration\n"));
4875 (void) printf(gettext(" 9 refquota and refreservation "
4877 (void) printf(gettext(" 10 Cache devices\n"));
4878 (void) printf(gettext(" 11 Improved scrub performance\n"));
4879 (void) printf(gettext(" 12 Snapshot properties\n"));
4880 (void) printf(gettext(" 13 snapused property\n"));
4881 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4882 (void) printf(gettext(" 15 user/group space accounting\n"));
4883 (void) printf(gettext(" 16 stmf property support\n"));
4884 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4885 (void) printf(gettext(" 18 Snapshot user holds\n"));
4886 (void) printf(gettext(" 19 Log device removal\n"));
4887 (void) printf(gettext(" 20 Compression using zle "
4888 "(zero-length encoding)\n"));
4889 (void) printf(gettext(" 21 Deduplication\n"));
4890 (void) printf(gettext(" 22 Received properties\n"));
4891 (void) printf(gettext(" 23 Slim ZIL\n"));
4892 (void) printf(gettext(" 24 System attributes\n"));
4893 (void) printf(gettext(" 25 Improved scrub stats\n"));
4894 (void) printf(gettext(" 26 Improved snapshot deletion "
4896 (void) printf(gettext(" 27 Improved snapshot creation "
4898 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4899 (void) printf(gettext("\nFor more information on a particular "
4900 "version, including supported releases,\n"));
4901 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4902 } else if (argc == 0 && upgradeall) {
4903 cb.cb_first = B_TRUE;
4904 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4905 if (ret == 0 && cb.cb_first) {
4906 if (cb.cb_version == SPA_VERSION) {
4907 (void) printf(gettext("All pools are already "
4908 "formatted using feature flags.\n\n"));
4909 (void) printf(gettext("Every feature flags "
4910 "pool already has all supported features "
4913 (void) printf(gettext("All pools are already "
4914 "formatted with version %llu or higher.\n"),
4918 } else if (argc == 0) {
4919 cb.cb_first = B_TRUE;
4920 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4924 (void) printf(gettext("All pools are formatted "
4925 "using feature flags.\n\n"));
4927 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4928 "for a list of available legacy versions.\n"));
4931 cb.cb_first = B_TRUE;
4932 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4936 (void) printf(gettext("Every feature flags pool has "
4937 "all supported features enabled.\n"));
4939 (void) printf(gettext("\n"));
4942 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4946 if (cb.cb_poolname[0] != '\0') {
4948 "If you boot from pool '%s', don't forget to update boot code.\n"
4949 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4950 "the following command will do it:\n"
4952 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4959 typedef struct hist_cbdata {
4966 * Print out the command history for a specific pool.
4969 get_history_one(zpool_handle_t *zhp, void *data)
4975 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4977 cb->first = B_FALSE;
4979 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4981 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4984 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4985 &records, &numrecords) == 0);
4986 for (i = 0; i < numrecords; i++) {
4987 nvlist_t *rec = records[i];
4990 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
4994 tsec = fnvlist_lookup_uint64(records[i],
4996 (void) localtime_r(&tsec, &t);
4997 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5000 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5001 (void) printf("%s %s", tbuf,
5002 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5003 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5005 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5008 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5009 (void) printf("%s unrecognized record:\n",
5011 dump_nvlist(rec, 4);
5014 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5015 zfs_history_event_names[ievent],
5016 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5017 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5018 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5021 (void) printf("%s [txg:%lld] %s", tbuf,
5022 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5023 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5024 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5025 (void) printf(" %s (%llu)",
5026 fnvlist_lookup_string(rec,
5028 fnvlist_lookup_uint64(rec,
5031 (void) printf(" %s", fnvlist_lookup_string(rec,
5032 ZPOOL_HIST_INT_STR));
5033 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5036 (void) printf("%s ioctl %s\n", tbuf,
5037 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5038 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5039 (void) printf(" input:\n");
5040 dump_nvlist(fnvlist_lookup_nvlist(rec,
5041 ZPOOL_HIST_INPUT_NVL), 8);
5043 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5044 (void) printf(" output:\n");
5045 dump_nvlist(fnvlist_lookup_nvlist(rec,
5046 ZPOOL_HIST_OUTPUT_NVL), 8);
5051 (void) printf("%s unrecognized record:\n", tbuf);
5052 dump_nvlist(rec, 4);
5056 (void) printf("\n");
5059 (void) printf(" [");
5060 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5061 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5062 struct passwd *pwd = getpwuid(who);
5063 (void) printf("user %d ", (int)who);
5065 (void) printf("(%s) ", pwd->pw_name);
5067 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5068 (void) printf("on %s",
5069 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5071 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5072 (void) printf(":%s",
5073 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5076 (void) printf("\n");
5078 (void) printf("\n");
5085 * zpool history <pool>
5087 * Displays the history of commands that modified pools.
5090 zpool_do_history(int argc, char **argv)
5092 hist_cbdata_t cbdata = { 0 };
5096 cbdata.first = B_TRUE;
5098 while ((c = getopt(argc, argv, "li")) != -1) {
5101 cbdata.longfmt = B_TRUE;
5104 cbdata.internal = B_TRUE;
5107 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5115 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5118 if (argc == 0 && cbdata.first == B_TRUE) {
5119 (void) printf(gettext("no pools available\n"));
5127 get_callback(zpool_handle_t *zhp, void *data)
5129 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5130 char value[MAXNAMELEN];
5131 zprop_source_t srctype;
5134 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5137 * Skip the special fake placeholder. This will also skip
5138 * over the name property when 'all' is specified.
5140 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5141 pl == cbp->cb_proplist)
5144 if (pl->pl_prop == ZPROP_INVAL &&
5145 (zpool_prop_feature(pl->pl_user_prop) ||
5146 zpool_prop_unsupported(pl->pl_user_prop))) {
5147 srctype = ZPROP_SRC_LOCAL;
5149 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5150 value, sizeof (value)) == 0) {
5151 zprop_print_one_property(zpool_get_name(zhp),
5152 cbp, pl->pl_user_prop, value, srctype,
5156 if (zpool_get_prop(zhp, pl->pl_prop, value,
5157 sizeof (value), &srctype) != 0)
5160 zprop_print_one_property(zpool_get_name(zhp), cbp,
5161 zpool_prop_to_name(pl->pl_prop), value, srctype,
5169 zpool_do_get(int argc, char **argv)
5171 zprop_get_cbdata_t cb = { 0 };
5172 zprop_list_t fake_name = { 0 };
5176 (void) fprintf(stderr, gettext("missing property "
5181 cb.cb_first = B_TRUE;
5182 cb.cb_sources = ZPROP_SRC_ALL;
5183 cb.cb_columns[0] = GET_COL_NAME;
5184 cb.cb_columns[1] = GET_COL_PROPERTY;
5185 cb.cb_columns[2] = GET_COL_VALUE;
5186 cb.cb_columns[3] = GET_COL_SOURCE;
5187 cb.cb_type = ZFS_TYPE_POOL;
5189 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5190 ZFS_TYPE_POOL) != 0)
5193 if (cb.cb_proplist != NULL) {
5194 fake_name.pl_prop = ZPOOL_PROP_NAME;
5195 fake_name.pl_width = strlen(gettext("NAME"));
5196 fake_name.pl_next = cb.cb_proplist;
5197 cb.cb_proplist = &fake_name;
5200 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5203 if (cb.cb_proplist == &fake_name)
5204 zprop_free_list(fake_name.pl_next);
5206 zprop_free_list(cb.cb_proplist);
5211 typedef struct set_cbdata {
5214 boolean_t cb_any_successful;
5218 set_callback(zpool_handle_t *zhp, void *data)
5221 set_cbdata_t *cb = (set_cbdata_t *)data;
5223 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5226 cb->cb_any_successful = B_TRUE;
5232 zpool_do_set(int argc, char **argv)
5234 set_cbdata_t cb = { 0 };
5237 if (argc > 1 && argv[1][0] == '-') {
5238 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5244 (void) fprintf(stderr, gettext("missing property=value "
5250 (void) fprintf(stderr, gettext("missing pool name\n"));
5255 (void) fprintf(stderr, gettext("too many pool names\n"));
5259 cb.cb_propname = argv[1];
5260 cb.cb_value = strchr(cb.cb_propname, '=');
5261 if (cb.cb_value == NULL) {
5262 (void) fprintf(stderr, gettext("missing value in "
5263 "property=value argument\n"));
5267 *(cb.cb_value) = '\0';
5270 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5277 find_command_idx(char *command, int *idx)
5281 for (i = 0; i < NCOMMAND; i++) {
5282 if (command_table[i].name == NULL)
5285 if (strcmp(command, command_table[i].name) == 0) {
5294 main(int argc, char **argv)
5300 (void) setlocale(LC_ALL, "");
5301 (void) textdomain(TEXT_DOMAIN);
5303 if ((g_zfs = libzfs_init()) == NULL) {
5304 (void) fprintf(stderr, gettext("internal error: failed to "
5305 "initialize ZFS library\n"));
5309 libzfs_print_on_error(g_zfs, B_TRUE);
5314 * Make sure the user has specified some command.
5317 (void) fprintf(stderr, gettext("missing command\n"));
5326 if (strcmp(cmdname, "-?") == 0)
5329 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5332 * Run the appropriate command.
5334 if (find_command_idx(cmdname, &i) == 0) {
5335 current_command = &command_table[i];
5336 ret = command_table[i].func(argc - 1, argv + 1);
5337 } else if (strchr(cmdname, '=')) {
5338 verify(find_command_idx("set", &i) == 0);
5339 current_command = &command_table[i];
5340 ret = command_table[i].func(argc, argv);
5341 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5343 * 'freeze' is a vile debugging abomination, so we treat
5346 zfs_cmd_t zc = { 0 };
5347 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5348 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5350 (void) fprintf(stderr, gettext("unrecognized "
5351 "command '%s'\n"), cmdname);
5355 if (ret == 0 && log_history)
5356 (void) zpool_log_history(g_zfs, history_str);
5361 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5362 * for the purposes of running ::findleaks.
5364 if (getenv("ZFS_ABORT") != NULL) {
5365 (void) printf("dumping core by request\n");