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))
810 if ((propval = strchr(optarg, '=')) == NULL) {
811 (void) fprintf(stderr, gettext("missing "
812 "'=' for -o option\n"));
818 if (add_prop_list(optarg, propval, &props, B_TRUE))
822 * If the user is creating a pool that doesn't support
823 * feature flags, don't enable any features.
825 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
829 ver = strtoull(propval, &end, 10);
831 ver < SPA_VERSION_FEATURES) {
832 enable_all_pool_feat = B_FALSE;
837 if ((propval = strchr(optarg, '=')) == NULL) {
838 (void) fprintf(stderr, gettext("missing "
839 "'=' for -O option\n"));
845 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
849 (void) fprintf(stderr, gettext("missing argument for "
850 "'%c' option\n"), optopt);
853 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
862 /* get pool name and check number of arguments */
864 (void) fprintf(stderr, gettext("missing pool name argument\n"));
868 (void) fprintf(stderr, gettext("missing vdev specification\n"));
875 * As a special case, check for use of '/' in the name, and direct the
876 * user to use 'zfs create' instead.
878 if (strchr(poolname, '/') != NULL) {
879 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
880 "character '/' in pool name\n"), poolname);
881 (void) fprintf(stderr, gettext("use 'zfs create' to "
882 "create a dataset\n"));
886 /* pass off to get_vdev_spec for bulk processing */
887 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
892 /* make_root_vdev() allows 0 toplevel children if there are spares */
893 if (!zfs_allocatable_devs(nvroot)) {
894 (void) fprintf(stderr, gettext("invalid vdev "
895 "specification: at least one toplevel vdev must be "
900 if (altroot != NULL && altroot[0] != '/') {
901 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
902 "must be an absolute path\n"), altroot);
907 * Check the validity of the mountpoint and direct the user to use the
908 * '-m' mountpoint option if it looks like its in use.
909 * Ignore the checks if the '-f' option is given.
911 if (!force && (mountpoint == NULL ||
912 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
913 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
914 char buf[MAXPATHLEN];
917 if (mountpoint && mountpoint[0] != '/') {
918 (void) fprintf(stderr, gettext("invalid mountpoint "
919 "'%s': must be an absolute path, 'legacy', or "
920 "'none'\n"), mountpoint);
924 if (mountpoint == NULL) {
926 (void) snprintf(buf, sizeof (buf), "%s/%s",
929 (void) snprintf(buf, sizeof (buf), "/%s",
933 (void) snprintf(buf, sizeof (buf), "%s%s",
934 altroot, mountpoint);
936 (void) snprintf(buf, sizeof (buf), "%s",
940 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
941 (void) fprintf(stderr, gettext("mountpoint '%s' : "
942 "%s\n"), buf, strerror(errno));
943 (void) fprintf(stderr, gettext("use '-m' "
944 "option to provide a different default\n"));
949 while (count < 3 && readdir(dirp) != NULL)
951 (void) closedir(dirp);
954 (void) fprintf(stderr, gettext("mountpoint "
955 "'%s' exists and is not empty\n"), buf);
956 (void) fprintf(stderr, gettext("use '-m' "
957 "option to provide a "
958 "different default\n"));
966 * For a dry run invocation, print out a basic message and run
967 * through all the vdevs in the list and print out in an
968 * appropriate hierarchy.
970 (void) printf(gettext("would create '%s' with the "
971 "following layout:\n\n"), poolname);
973 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
974 if (num_logs(nvroot) > 0)
975 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
980 * Hand off to libzfs.
982 if (enable_all_pool_feat) {
984 for (i = 0; i < SPA_FEATURES; i++) {
985 char propname[MAXPATHLEN];
986 zfeature_info_t *feat = &spa_feature_table[i];
988 (void) snprintf(propname, sizeof (propname),
989 "feature@%s", feat->fi_uname);
992 * Skip feature if user specified it manually
993 * on the command line.
995 if (nvlist_exists(props, propname))
998 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
999 &props, B_TRUE) != 0)
1003 if (zpool_create(g_zfs, poolname,
1004 nvroot, props, fsprops) == 0) {
1005 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1006 ZFS_TYPE_FILESYSTEM);
1008 if (mountpoint != NULL)
1009 verify(zfs_prop_set(pool,
1011 ZFS_PROP_MOUNTPOINT),
1013 if (zfs_mount(pool, NULL, 0) == 0)
1014 ret = zfs_shareall(pool);
1017 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1018 (void) fprintf(stderr, gettext("pool name may have "
1024 nvlist_free(nvroot);
1025 nvlist_free(fsprops);
1029 nvlist_free(fsprops);
1036 * zpool destroy <pool>
1038 * -f Forcefully unmount any datasets
1040 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1043 zpool_do_destroy(int argc, char **argv)
1045 boolean_t force = B_FALSE;
1048 zpool_handle_t *zhp;
1052 while ((c = getopt(argc, argv, "f")) != -1) {
1058 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1067 /* check arguments */
1069 (void) fprintf(stderr, gettext("missing pool argument\n"));
1073 (void) fprintf(stderr, gettext("too many arguments\n"));
1079 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1081 * As a special case, check for use of '/' in the name, and
1082 * direct the user to use 'zfs destroy' instead.
1084 if (strchr(pool, '/') != NULL)
1085 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1086 "destroy a dataset\n"));
1090 if (zpool_disable_datasets(zhp, force) != 0) {
1091 (void) fprintf(stderr, gettext("could not destroy '%s': "
1092 "could not unmount datasets\n"), zpool_get_name(zhp));
1096 /* The history must be logged as part of the export */
1097 log_history = B_FALSE;
1099 ret = (zpool_destroy(zhp, history_str) != 0);
1107 * zpool export [-f] <pool> ...
1109 * -f Forcefully unmount datasets
1111 * Export the given pools. By default, the command will attempt to cleanly
1112 * unmount any active datasets within the pool. If the '-f' flag is specified,
1113 * then the datasets will be forcefully unmounted.
1116 zpool_do_export(int argc, char **argv)
1118 boolean_t force = B_FALSE;
1119 boolean_t hardforce = B_FALSE;
1121 zpool_handle_t *zhp;
1126 while ((c = getopt(argc, argv, "fF")) != -1) {
1135 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1144 /* check arguments */
1146 (void) fprintf(stderr, gettext("missing pool argument\n"));
1151 for (i = 0; i < argc; i++) {
1152 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1157 if (zpool_disable_datasets(zhp, force) != 0) {
1163 /* The history must be logged as part of the export */
1164 log_history = B_FALSE;
1167 if (zpool_export_force(zhp, history_str) != 0)
1169 } else if (zpool_export(zhp, force, history_str) != 0) {
1180 * Given a vdev configuration, determine the maximum width needed for the device
1184 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1186 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1191 if (strlen(name) + depth > max)
1192 max = strlen(name) + depth;
1196 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1197 &child, &children) == 0) {
1198 for (c = 0; c < children; c++)
1199 if ((ret = max_width(zhp, child[c], depth + 2,
1204 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1205 &child, &children) == 0) {
1206 for (c = 0; c < children; c++)
1207 if ((ret = max_width(zhp, child[c], depth + 2,
1212 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1213 &child, &children) == 0) {
1214 for (c = 0; c < children; c++)
1215 if ((ret = max_width(zhp, child[c], depth + 2,
1224 typedef struct spare_cbdata {
1226 zpool_handle_t *cb_zhp;
1230 find_vdev(nvlist_t *nv, uint64_t search)
1236 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1240 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1241 &child, &children) == 0) {
1242 for (c = 0; c < children; c++)
1243 if (find_vdev(child[c], search))
1251 find_spare(zpool_handle_t *zhp, void *data)
1253 spare_cbdata_t *cbp = data;
1254 nvlist_t *config, *nvroot;
1256 config = zpool_get_config(zhp, NULL);
1257 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1260 if (find_vdev(nvroot, cbp->cb_guid)) {
1270 * Print out configuration state as requested by status_callback.
1273 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1274 int namewidth, int depth, boolean_t isspare)
1278 pool_scan_stat_t *ps = NULL;
1280 char rbuf[6], wbuf[6], cbuf[6];
1282 uint64_t notpresent;
1286 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1287 &child, &children) != 0)
1290 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1291 (uint64_t **)&vs, &c) == 0);
1293 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1296 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1299 if (vs->vs_aux == VDEV_AUX_SPARED)
1301 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1305 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1309 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1310 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1311 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1312 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1315 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1316 ¬present) == 0 ||
1317 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1319 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1320 (void) printf(" was %s", path);
1321 } else if (vs->vs_aux != 0) {
1324 switch (vs->vs_aux) {
1325 case VDEV_AUX_OPEN_FAILED:
1326 (void) printf(gettext("cannot open"));
1329 case VDEV_AUX_BAD_GUID_SUM:
1330 (void) printf(gettext("missing device"));
1333 case VDEV_AUX_NO_REPLICAS:
1334 (void) printf(gettext("insufficient replicas"));
1337 case VDEV_AUX_VERSION_NEWER:
1338 (void) printf(gettext("newer version"));
1341 case VDEV_AUX_UNSUP_FEAT:
1342 (void) printf(gettext("unsupported feature(s)"));
1345 case VDEV_AUX_SPARED:
1346 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1348 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1349 if (strcmp(zpool_get_name(cb.cb_zhp),
1350 zpool_get_name(zhp)) == 0)
1351 (void) printf(gettext("currently in "
1354 (void) printf(gettext("in use by "
1356 zpool_get_name(cb.cb_zhp));
1357 zpool_close(cb.cb_zhp);
1359 (void) printf(gettext("currently in use"));
1363 case VDEV_AUX_ERR_EXCEEDED:
1364 (void) printf(gettext("too many errors"));
1367 case VDEV_AUX_IO_FAILURE:
1368 (void) printf(gettext("experienced I/O failures"));
1371 case VDEV_AUX_BAD_LOG:
1372 (void) printf(gettext("bad intent log"));
1375 case VDEV_AUX_EXTERNAL:
1376 (void) printf(gettext("external device fault"));
1379 case VDEV_AUX_SPLIT_POOL:
1380 (void) printf(gettext("split into new pool"));
1384 (void) printf(gettext("corrupted data"));
1389 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1390 (uint64_t **)&ps, &c);
1392 if (ps && ps->pss_state == DSS_SCANNING &&
1393 vs->vs_scan_processed != 0 && children == 0) {
1394 (void) printf(gettext(" (%s)"),
1395 (ps->pss_func == POOL_SCAN_RESILVER) ?
1396 "resilvering" : "repairing");
1399 (void) printf("\n");
1401 for (c = 0; c < children; c++) {
1402 uint64_t islog = B_FALSE, ishole = B_FALSE;
1404 /* Don't print logs or holes here */
1405 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1407 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1409 if (islog || ishole)
1411 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1412 print_status_config(zhp, vname, child[c],
1413 namewidth, depth + 2, isspare);
1420 * Print the configuration of an exported pool. Iterate over all vdevs in the
1421 * pool, printing out the name and status for each one.
1424 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1431 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1432 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1433 strcmp(type, VDEV_TYPE_HOLE) == 0)
1436 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1437 (uint64_t **)&vs, &c) == 0);
1439 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1440 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1442 if (vs->vs_aux != 0) {
1445 switch (vs->vs_aux) {
1446 case VDEV_AUX_OPEN_FAILED:
1447 (void) printf(gettext("cannot open"));
1450 case VDEV_AUX_BAD_GUID_SUM:
1451 (void) printf(gettext("missing device"));
1454 case VDEV_AUX_NO_REPLICAS:
1455 (void) printf(gettext("insufficient replicas"));
1458 case VDEV_AUX_VERSION_NEWER:
1459 (void) printf(gettext("newer version"));
1462 case VDEV_AUX_UNSUP_FEAT:
1463 (void) printf(gettext("unsupported feature(s)"));
1466 case VDEV_AUX_ERR_EXCEEDED:
1467 (void) printf(gettext("too many errors"));
1471 (void) printf(gettext("corrupted data"));
1475 (void) printf("\n");
1477 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1478 &child, &children) != 0)
1481 for (c = 0; c < children; c++) {
1482 uint64_t is_log = B_FALSE;
1484 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1489 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1490 print_import_config(vname, child[c], namewidth, depth + 2);
1494 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1495 &child, &children) == 0) {
1496 (void) printf(gettext("\tcache\n"));
1497 for (c = 0; c < children; c++) {
1498 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1499 (void) printf("\t %s\n", vname);
1504 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1505 &child, &children) == 0) {
1506 (void) printf(gettext("\tspares\n"));
1507 for (c = 0; c < children; c++) {
1508 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1509 (void) printf("\t %s\n", vname);
1517 * Logs are recorded as top level vdevs in the main pool child array
1518 * but with "is_log" set to 1. We use either print_status_config() or
1519 * print_import_config() to print the top level logs then any log
1520 * children (eg mirrored slogs) are printed recursively - which
1521 * works because only the top level vdev is marked "is_log"
1524 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1529 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1533 (void) printf(gettext("\tlogs\n"));
1535 for (c = 0; c < children; c++) {
1536 uint64_t is_log = B_FALSE;
1539 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1543 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1545 print_status_config(zhp, name, child[c], namewidth,
1548 print_import_config(name, child[c], namewidth, 2);
1554 * Display the status for the given pool.
1557 show_import(nvlist_t *config)
1559 uint64_t pool_state;
1571 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1573 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1575 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1577 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1580 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1581 (uint64_t **)&vs, &vsc) == 0);
1582 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1584 reason = zpool_import_status(config, &msgid);
1586 (void) printf(gettext(" pool: %s\n"), name);
1587 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1588 (void) printf(gettext(" state: %s"), health);
1589 if (pool_state == POOL_STATE_DESTROYED)
1590 (void) printf(gettext(" (DESTROYED)"));
1591 (void) printf("\n");
1594 case ZPOOL_STATUS_MISSING_DEV_R:
1595 case ZPOOL_STATUS_MISSING_DEV_NR:
1596 case ZPOOL_STATUS_BAD_GUID_SUM:
1597 (void) printf(gettext(" status: One or more devices are "
1598 "missing from the system.\n"));
1601 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1602 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1603 (void) printf(gettext(" status: One or more devices contains "
1604 "corrupted data.\n"));
1607 case ZPOOL_STATUS_CORRUPT_DATA:
1609 gettext(" status: The pool data is corrupted.\n"));
1612 case ZPOOL_STATUS_OFFLINE_DEV:
1613 (void) printf(gettext(" status: One or more devices "
1614 "are offlined.\n"));
1617 case ZPOOL_STATUS_CORRUPT_POOL:
1618 (void) printf(gettext(" status: The pool metadata is "
1622 case ZPOOL_STATUS_VERSION_OLDER:
1623 (void) printf(gettext(" status: The pool is formatted using a "
1624 "legacy on-disk version.\n"));
1627 case ZPOOL_STATUS_VERSION_NEWER:
1628 (void) printf(gettext(" status: The pool is formatted using an "
1629 "incompatible version.\n"));
1632 case ZPOOL_STATUS_FEAT_DISABLED:
1633 (void) printf(gettext(" status: Some supported features are "
1634 "not enabled on the pool.\n"));
1637 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1638 (void) printf(gettext("status: The pool uses the following "
1639 "feature(s) not supported on this sytem:\n"));
1640 zpool_print_unsup_feat(config);
1643 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1644 (void) printf(gettext("status: The pool can only be accessed "
1645 "in read-only mode on this system. It\n\tcannot be "
1646 "accessed in read-write mode because it uses the "
1647 "following\n\tfeature(s) not supported on this system:\n"));
1648 zpool_print_unsup_feat(config);
1651 case ZPOOL_STATUS_HOSTID_MISMATCH:
1652 (void) printf(gettext(" status: The pool was last accessed by "
1653 "another system.\n"));
1656 case ZPOOL_STATUS_FAULTED_DEV_R:
1657 case ZPOOL_STATUS_FAULTED_DEV_NR:
1658 (void) printf(gettext(" status: One or more devices are "
1662 case ZPOOL_STATUS_BAD_LOG:
1663 (void) printf(gettext(" status: An intent log record cannot be "
1667 case ZPOOL_STATUS_RESILVERING:
1668 (void) printf(gettext(" status: One or more devices were being "
1674 * No other status can be seen when importing pools.
1676 assert(reason == ZPOOL_STATUS_OK);
1680 * Print out an action according to the overall state of the pool.
1682 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1683 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1684 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1685 (void) printf(gettext(" action: The pool can be "
1686 "imported using its name or numeric identifier, "
1687 "though\n\tsome features will not be available "
1688 "without an explicit 'zpool upgrade'.\n"));
1689 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1690 (void) printf(gettext(" action: The pool can be "
1691 "imported using its name or numeric "
1692 "identifier and\n\tthe '-f' flag.\n"));
1694 (void) printf(gettext(" action: The pool can be "
1695 "imported using its name or numeric "
1698 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1699 (void) printf(gettext(" action: The pool can be imported "
1700 "despite missing or damaged devices. The\n\tfault "
1701 "tolerance of the pool may be compromised if imported.\n"));
1704 case ZPOOL_STATUS_VERSION_NEWER:
1705 (void) printf(gettext(" action: The pool cannot be "
1706 "imported. Access the pool on a system running "
1707 "newer\n\tsoftware, or recreate the pool from "
1710 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1711 (void) printf(gettext("action: The pool cannot be "
1712 "imported. Access the pool on a system that "
1713 "supports\n\tthe required feature(s), or recreate "
1714 "the pool from backup.\n"));
1716 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1717 (void) printf(gettext("action: The pool cannot be "
1718 "imported in read-write mode. Import the pool "
1720 "\t\"-o readonly=on\", access the pool on a system "
1721 "that supports the\n\trequired feature(s), or "
1722 "recreate the pool from backup.\n"));
1724 case ZPOOL_STATUS_MISSING_DEV_R:
1725 case ZPOOL_STATUS_MISSING_DEV_NR:
1726 case ZPOOL_STATUS_BAD_GUID_SUM:
1727 (void) printf(gettext(" action: The pool cannot be "
1728 "imported. Attach the missing\n\tdevices and try "
1732 (void) printf(gettext(" action: The pool cannot be "
1733 "imported due to damaged devices or data.\n"));
1737 /* Print the comment attached to the pool. */
1738 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1739 (void) printf(gettext("comment: %s\n"), comment);
1742 * If the state is "closed" or "can't open", and the aux state
1743 * is "corrupt data":
1745 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1746 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1747 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1748 if (pool_state == POOL_STATE_DESTROYED)
1749 (void) printf(gettext("\tThe pool was destroyed, "
1750 "but can be imported using the '-Df' flags.\n"));
1751 else if (pool_state != POOL_STATE_EXPORTED)
1752 (void) printf(gettext("\tThe pool may be active on "
1753 "another system, but can be imported using\n\t"
1754 "the '-f' flag.\n"));
1758 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1761 (void) printf(gettext(" config:\n\n"));
1763 namewidth = max_width(NULL, nvroot, 0, 0);
1767 print_import_config(name, nvroot, namewidth, 0);
1768 if (num_logs(nvroot) > 0)
1769 print_logs(NULL, nvroot, namewidth, B_FALSE);
1771 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1772 (void) printf(gettext("\n\tAdditional devices are known to "
1773 "be part of this pool, though their\n\texact "
1774 "configuration cannot be determined.\n"));
1779 * Perform the import for the given configuration. This passes the heavy
1780 * lifting off to zpool_import_props(), and then mounts the datasets contained
1784 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1785 nvlist_t *props, int flags)
1787 zpool_handle_t *zhp;
1792 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1795 verify(nvlist_lookup_uint64(config,
1796 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1797 verify(nvlist_lookup_uint64(config,
1798 ZPOOL_CONFIG_VERSION, &version) == 0);
1799 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1800 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1801 "is formatted using an unsupported ZFS version\n"), name);
1803 } else if (state != POOL_STATE_EXPORTED &&
1804 !(flags & ZFS_IMPORT_ANY_HOST)) {
1807 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1809 if ((unsigned long)hostid != gethostid()) {
1814 verify(nvlist_lookup_string(config,
1815 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1816 verify(nvlist_lookup_uint64(config,
1817 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1819 (void) fprintf(stderr, gettext("cannot import "
1820 "'%s': pool may be in use from other "
1821 "system, it was last accessed by %s "
1822 "(hostid: 0x%lx) on %s"), name, hostname,
1823 (unsigned long)hostid,
1824 asctime(localtime(&t)));
1825 (void) fprintf(stderr, gettext("use '-f' to "
1826 "import anyway\n"));
1830 (void) fprintf(stderr, gettext("cannot import '%s': "
1831 "pool may be in use from other system\n"), name);
1832 (void) fprintf(stderr, gettext("use '-f' to import "
1838 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1841 if (newname != NULL)
1842 name = (char *)newname;
1844 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1847 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1848 !(flags & ZFS_IMPORT_ONLY) &&
1849 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1859 * zpool import [-d dir] [-D]
1860 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1861 * [-d dir | -c cachefile] [-f] -a
1862 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1863 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1865 * -c Read pool information from a cachefile instead of searching
1868 * -d Scan in a specific directory, other than /dev/dsk. More than
1869 * one directory can be specified using multiple '-d' options.
1871 * -D Scan for previously destroyed pools or import all or only
1872 * specified destroyed pools.
1874 * -R Temporarily import the pool, with all mountpoints relative to
1875 * the given root. The pool will remain exported when the machine
1878 * -V Import even in the presence of faulted vdevs. This is an
1879 * intentionally undocumented option for testing purposes, and
1880 * treats the pool configuration as complete, leaving any bad
1881 * vdevs in the FAULTED state. In other words, it does verbatim
1884 * -f Force import, even if it appears that the pool is active.
1886 * -F Attempt rewind if necessary.
1888 * -n See if rewind would work, but don't actually rewind.
1890 * -N Import the pool but don't mount datasets.
1892 * -T Specify a starting txg to use for import. This option is
1893 * intentionally undocumented option for testing purposes.
1895 * -a Import all pools found.
1897 * -o Set property=value and/or temporary mount options (without '=').
1899 * The import command scans for pools to import, and import pools based on pool
1900 * name and GUID. The pool can also be renamed as part of the import process.
1903 zpool_do_import(int argc, char **argv)
1905 char **searchdirs = NULL;
1909 nvlist_t *pools = NULL;
1910 boolean_t do_all = B_FALSE;
1911 boolean_t do_destroyed = B_FALSE;
1912 char *mntopts = NULL;
1915 uint64_t searchguid = 0;
1916 char *searchname = NULL;
1918 nvlist_t *found_config;
1919 nvlist_t *policy = NULL;
1920 nvlist_t *props = NULL;
1922 int flags = ZFS_IMPORT_NORMAL;
1923 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1924 boolean_t dryrun = B_FALSE;
1925 boolean_t do_rewind = B_FALSE;
1926 boolean_t xtreme_rewind = B_FALSE;
1927 uint64_t pool_state, txg = -1ULL;
1928 char *cachefile = NULL;
1929 importargs_t idata = { 0 };
1933 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1942 if (searchdirs == NULL) {
1943 searchdirs = safe_malloc(sizeof (char *));
1945 char **tmp = safe_malloc((nsearch + 1) *
1947 bcopy(searchdirs, tmp, nsearch *
1952 searchdirs[nsearch++] = optarg;
1955 do_destroyed = B_TRUE;
1958 flags |= ZFS_IMPORT_ANY_HOST;
1964 flags |= ZFS_IMPORT_MISSING_LOG;
1970 flags |= ZFS_IMPORT_ONLY;
1973 if ((propval = strchr(optarg, '=')) != NULL) {
1976 if (add_prop_list(optarg, propval,
1984 if (add_prop_list(zpool_prop_to_name(
1985 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1987 if (nvlist_lookup_string(props,
1988 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1991 if (add_prop_list(zpool_prop_to_name(
1992 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1997 txg = strtoull(optarg, &endptr, 10);
1998 if (errno != 0 || *endptr != '\0') {
1999 (void) fprintf(stderr,
2000 gettext("invalid txg value\n"));
2003 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2006 flags |= ZFS_IMPORT_VERBATIM;
2009 xtreme_rewind = B_TRUE;
2012 (void) fprintf(stderr, gettext("missing argument for "
2013 "'%c' option\n"), optopt);
2017 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2026 if (cachefile && nsearch != 0) {
2027 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2031 if ((dryrun || xtreme_rewind) && !do_rewind) {
2032 (void) fprintf(stderr,
2033 gettext("-n or -X only meaningful with -F\n"));
2037 rewind_policy = ZPOOL_TRY_REWIND;
2039 rewind_policy = ZPOOL_DO_REWIND;
2041 rewind_policy |= ZPOOL_EXTREME_REWIND;
2043 /* In the future, we can capture further policy and include it here */
2044 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2045 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2046 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2049 if (searchdirs == NULL) {
2050 searchdirs = safe_malloc(sizeof (char *));
2051 searchdirs[0] = "/dev";
2055 /* check argument count */
2058 (void) fprintf(stderr, gettext("too many arguments\n"));
2063 (void) fprintf(stderr, gettext("too many arguments\n"));
2068 * Check for the SYS_CONFIG privilege. We do this explicitly
2069 * here because otherwise any attempt to discover pools will
2072 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2073 (void) fprintf(stderr, gettext("cannot "
2074 "discover pools: permission denied\n"));
2076 nvlist_free(policy);
2082 * Depending on the arguments given, we do one of the following:
2084 * <none> Iterate through all pools and display information about
2087 * -a Iterate through all pools and try to import each one.
2089 * <id> Find the pool that corresponds to the given GUID/pool
2090 * name and import that one.
2092 * -D Above options applies only to destroyed pools.
2098 searchguid = strtoull(argv[0], &endptr, 10);
2099 if (errno != 0 || *endptr != '\0')
2100 searchname = argv[0];
2101 found_config = NULL;
2104 * User specified a name or guid. Ensure it's unique.
2106 idata.unique = B_TRUE;
2110 idata.path = searchdirs;
2111 idata.paths = nsearch;
2112 idata.poolname = searchname;
2113 idata.guid = searchguid;
2114 idata.cachefile = cachefile;
2116 pools = zpool_search_import(g_zfs, &idata);
2118 if (pools != NULL && idata.exists &&
2119 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2120 (void) fprintf(stderr, gettext("cannot import '%s': "
2121 "a pool with that name already exists\n"),
2123 (void) fprintf(stderr, gettext("use the form '%s "
2124 "<pool | id> <newpool>' to give it a new name\n"),
2127 } else if (pools == NULL && idata.exists) {
2128 (void) fprintf(stderr, gettext("cannot import '%s': "
2129 "a pool with that name is already created/imported,\n"),
2131 (void) fprintf(stderr, gettext("and no additional pools "
2132 "with that name were found\n"));
2134 } else if (pools == NULL) {
2136 (void) fprintf(stderr, gettext("cannot import '%s': "
2137 "no such pool available\n"), argv[0]);
2144 nvlist_free(policy);
2149 * At this point we have a list of import candidate configs. Even if
2150 * we were searching by pool name or guid, we still need to
2151 * post-process the list to deal with pool state and possible
2157 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2159 verify(nvpair_value_nvlist(elem, &config) == 0);
2161 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2163 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2165 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2168 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2175 (void) printf("\n");
2178 err |= do_import(config, NULL, mntopts,
2181 show_import(config);
2183 } else if (searchname != NULL) {
2187 * We are searching for a pool based on name.
2189 verify(nvlist_lookup_string(config,
2190 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2192 if (strcmp(name, searchname) == 0) {
2193 if (found_config != NULL) {
2194 (void) fprintf(stderr, gettext(
2195 "cannot import '%s': more than "
2196 "one matching pool\n"), searchname);
2197 (void) fprintf(stderr, gettext(
2198 "import by numeric ID instead\n"));
2201 found_config = config;
2207 * Search for a pool by guid.
2209 verify(nvlist_lookup_uint64(config,
2210 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2212 if (guid == searchguid)
2213 found_config = config;
2218 * If we were searching for a specific pool, verify that we found a
2219 * pool, and then do the import.
2221 if (argc != 0 && err == 0) {
2222 if (found_config == NULL) {
2223 (void) fprintf(stderr, gettext("cannot import '%s': "
2224 "no such pool available\n"), argv[0]);
2227 err |= do_import(found_config, argc == 1 ? NULL :
2228 argv[1], mntopts, props, flags);
2233 * If we were just looking for pools, report an error if none were
2236 if (argc == 0 && first)
2237 (void) fprintf(stderr,
2238 gettext("no pools available to import\n"));
2243 nvlist_free(policy);
2246 return (err ? 1 : 0);
2249 typedef struct iostat_cbdata {
2250 boolean_t cb_verbose;
2253 zpool_list_t *cb_list;
2257 print_iostat_separator(iostat_cbdata_t *cb)
2261 for (i = 0; i < cb->cb_namewidth; i++)
2263 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2267 print_iostat_header(iostat_cbdata_t *cb)
2269 (void) printf("%*s capacity operations bandwidth\n",
2270 cb->cb_namewidth, "");
2271 (void) printf("%-*s alloc free read write read write\n",
2272 cb->cb_namewidth, "pool");
2273 print_iostat_separator(cb);
2277 * Display a single statistic.
2280 print_one_stat(uint64_t value)
2284 zfs_nicenum(value, buf, sizeof (buf));
2285 (void) printf(" %5s", buf);
2289 * Print out all the statistics for the given vdev. This can either be the
2290 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2291 * is a verbose output, and we don't want to display the toplevel pool stats.
2294 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2295 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2297 nvlist_t **oldchild, **newchild;
2299 vdev_stat_t *oldvs, *newvs;
2300 vdev_stat_t zerovs = { 0 };
2305 if (oldnv != NULL) {
2306 verify(nvlist_lookup_uint64_array(oldnv,
2307 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2312 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2313 (uint64_t **)&newvs, &c) == 0);
2315 if (strlen(name) + depth > cb->cb_namewidth)
2316 (void) printf("%*s%s", depth, "", name);
2318 (void) printf("%*s%s%*s", depth, "", name,
2319 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2321 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2326 scale = (double)NANOSEC / tdelta;
2328 /* only toplevel vdevs have capacity stats */
2329 if (newvs->vs_space == 0) {
2330 (void) printf(" - -");
2332 print_one_stat(newvs->vs_alloc);
2333 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2336 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2337 oldvs->vs_ops[ZIO_TYPE_READ])));
2339 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2340 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2342 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2343 oldvs->vs_bytes[ZIO_TYPE_READ])));
2345 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2346 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2348 (void) printf("\n");
2350 if (!cb->cb_verbose)
2353 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2354 &newchild, &children) != 0)
2357 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2358 &oldchild, &c) != 0)
2361 for (c = 0; c < children; c++) {
2362 uint64_t ishole = B_FALSE, islog = B_FALSE;
2364 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2367 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2370 if (ishole || islog)
2373 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2374 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2375 newchild[c], cb, depth + 2);
2380 * Log device section
2383 if (num_logs(newnv) > 0) {
2384 (void) printf("%-*s - - - - - "
2385 "-\n", cb->cb_namewidth, "logs");
2387 for (c = 0; c < children; c++) {
2388 uint64_t islog = B_FALSE;
2389 (void) nvlist_lookup_uint64(newchild[c],
2390 ZPOOL_CONFIG_IS_LOG, &islog);
2393 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2395 print_vdev_stats(zhp, vname, oldnv ?
2396 oldchild[c] : NULL, newchild[c],
2405 * Include level 2 ARC devices in iostat output
2407 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2408 &newchild, &children) != 0)
2411 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2412 &oldchild, &c) != 0)
2416 (void) printf("%-*s - - - - - "
2417 "-\n", cb->cb_namewidth, "cache");
2418 for (c = 0; c < children; c++) {
2419 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2421 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2422 newchild[c], cb, depth + 2);
2429 refresh_iostat(zpool_handle_t *zhp, void *data)
2431 iostat_cbdata_t *cb = data;
2435 * If the pool has disappeared, remove it from the list and continue.
2437 if (zpool_refresh_stats(zhp, &missing) != 0)
2441 pool_list_remove(cb->cb_list, zhp);
2447 * Callback to print out the iostats for the given pool.
2450 print_iostat(zpool_handle_t *zhp, void *data)
2452 iostat_cbdata_t *cb = data;
2453 nvlist_t *oldconfig, *newconfig;
2454 nvlist_t *oldnvroot, *newnvroot;
2456 newconfig = zpool_get_config(zhp, &oldconfig);
2458 if (cb->cb_iteration == 1)
2461 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2464 if (oldconfig == NULL)
2467 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2471 * Print out the statistics for the pool.
2473 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2476 print_iostat_separator(cb);
2482 get_namewidth(zpool_handle_t *zhp, void *data)
2484 iostat_cbdata_t *cb = data;
2485 nvlist_t *config, *nvroot;
2487 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2488 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2490 if (!cb->cb_verbose)
2491 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2493 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2498 * The width must fall into the range [10,38]. The upper limit is the
2499 * maximum we can have and still fit in 80 columns.
2501 if (cb->cb_namewidth < 10)
2502 cb->cb_namewidth = 10;
2503 if (cb->cb_namewidth > 38)
2504 cb->cb_namewidth = 38;
2510 * Parse the input string, get the 'interval' and 'count' value if there is one.
2513 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2516 unsigned long interval = 0, count = 0;
2517 int argc = *argcp, errno;
2520 * Determine if the last argument is an integer or a pool name
2522 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2526 interval = strtoul(argv[argc - 1], &end, 10);
2528 if (*end == '\0' && errno == 0) {
2529 if (interval == 0) {
2530 (void) fprintf(stderr, gettext("interval "
2531 "cannot be zero\n"));
2535 * Ignore the last parameter
2540 * If this is not a valid number, just plow on. The
2541 * user will get a more informative error message later
2549 * If the last argument is also an integer, then we have both a count
2552 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2557 interval = strtoul(argv[argc - 1], &end, 10);
2559 if (*end == '\0' && errno == 0) {
2560 if (interval == 0) {
2561 (void) fprintf(stderr, gettext("interval "
2562 "cannot be zero\n"));
2567 * Ignore the last parameter
2581 get_timestamp_arg(char c)
2584 timestamp_fmt = UDATE;
2586 timestamp_fmt = DDATE;
2592 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2594 * -v Display statistics for individual vdevs
2595 * -T Display a timestamp in date(1) or Unix format
2597 * This command can be tricky because we want to be able to deal with pool
2598 * creation/destruction as well as vdev configuration changes. The bulk of this
2599 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2600 * on pool_list_update() to detect the addition of new pools. Configuration
2601 * changes are all handled within libzfs.
2604 zpool_do_iostat(int argc, char **argv)
2609 unsigned long interval = 0, count = 0;
2611 boolean_t verbose = B_FALSE;
2615 while ((c = getopt(argc, argv, "T:v")) != -1) {
2618 get_timestamp_arg(*optarg);
2624 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2633 get_interval_count(&argc, argv, &interval, &count);
2636 * Construct the list of all interesting pools.
2639 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2642 if (pool_list_count(list) == 0 && argc != 0) {
2643 pool_list_free(list);
2647 if (pool_list_count(list) == 0 && interval == 0) {
2648 pool_list_free(list);
2649 (void) fprintf(stderr, gettext("no pools available\n"));
2654 * Enter the main iostat loop.
2657 cb.cb_verbose = verbose;
2658 cb.cb_iteration = 0;
2659 cb.cb_namewidth = 0;
2662 pool_list_update(list);
2664 if ((npools = pool_list_count(list)) == 0)
2668 * Refresh all statistics. This is done as an explicit step
2669 * before calculating the maximum name width, so that any
2670 * configuration changes are properly accounted for.
2672 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2675 * Iterate over all pools to determine the maximum width
2676 * for the pool / device name column across all pools.
2678 cb.cb_namewidth = 0;
2679 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2681 if (timestamp_fmt != NODATE)
2682 print_timestamp(timestamp_fmt);
2685 * If it's the first time, or verbose mode, print the header.
2687 if (++cb.cb_iteration == 1 || verbose)
2688 print_iostat_header(&cb);
2690 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2693 * If there's more than one pool, and we're not in verbose mode
2694 * (which prints a separator for us), then print a separator.
2696 if (npools > 1 && !verbose)
2697 print_iostat_separator(&cb);
2700 (void) printf("\n");
2703 * Flush the output so that redirection to a file isn't buffered
2706 (void) fflush(stdout);
2711 if (count != 0 && --count == 0)
2714 (void) sleep(interval);
2717 pool_list_free(list);
2722 typedef struct list_cbdata {
2723 boolean_t cb_verbose;
2725 boolean_t cb_scripted;
2726 zprop_list_t *cb_proplist;
2730 * Given a list of columns to display, output appropriate headers for each one.
2733 print_header(list_cbdata_t *cb)
2735 zprop_list_t *pl = cb->cb_proplist;
2736 char headerbuf[ZPOOL_MAXPROPLEN];
2738 boolean_t first = B_TRUE;
2739 boolean_t right_justify;
2742 for (; pl != NULL; pl = pl->pl_next) {
2743 width = pl->pl_width;
2744 if (first && cb->cb_verbose) {
2746 * Reset the width to accommodate the verbose listing
2749 width = cb->cb_namewidth;
2757 right_justify = B_FALSE;
2758 if (pl->pl_prop != ZPROP_INVAL) {
2759 header = zpool_prop_column_name(pl->pl_prop);
2760 right_justify = zpool_prop_align_right(pl->pl_prop);
2764 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2765 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2766 headerbuf[i] = '\0';
2770 if (pl->pl_next == NULL && !right_justify)
2771 (void) printf("%s", header);
2772 else if (right_justify)
2773 (void) printf("%*s", width, header);
2775 (void) printf("%-*s", width, header);
2779 (void) printf("\n");
2783 * Given a pool and a list of properties, print out all the properties according
2784 * to the described layout.
2787 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2789 zprop_list_t *pl = cb->cb_proplist;
2790 boolean_t first = B_TRUE;
2791 char property[ZPOOL_MAXPROPLEN];
2793 boolean_t right_justify;
2796 for (; pl != NULL; pl = pl->pl_next) {
2798 width = pl->pl_width;
2799 if (first && cb->cb_verbose) {
2801 * Reset the width to accommodate the verbose listing
2804 width = cb->cb_namewidth;
2808 if (cb->cb_scripted)
2809 (void) printf("\t");
2816 right_justify = B_FALSE;
2817 if (pl->pl_prop != ZPROP_INVAL) {
2818 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2819 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2821 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2822 sizeof (property), NULL) != 0)
2827 right_justify = zpool_prop_align_right(pl->pl_prop);
2828 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2829 zpool_prop_unsupported(pl->pl_user_prop)) &&
2830 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2831 sizeof (property)) == 0) {
2839 * If this is being called in scripted mode, or if this is the
2840 * last column and it is left-justified, don't include a width
2843 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2844 (void) printf("%s", propstr);
2845 else if (right_justify)
2846 (void) printf("%*s", width, propstr);
2848 (void) printf("%-*s", width, propstr);
2851 (void) printf("\n");
2855 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2859 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2861 zfs_nicenum(value, propval, sizeof (propval));
2863 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2864 (void) strlcpy(propval, "-", sizeof (propval));
2867 (void) printf("\t%s", propval);
2869 (void) printf(" %*s", width, propval);
2873 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2874 list_cbdata_t *cb, int depth)
2880 boolean_t scripted = cb->cb_scripted;
2882 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2883 (uint64_t **)&vs, &c) == 0);
2887 (void) printf("\t%s", name);
2888 else if (strlen(name) + depth > cb->cb_namewidth)
2889 (void) printf("%*s%s", depth, "", name);
2891 (void) printf("%*s%s%*s", depth, "", name,
2892 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2894 /* only toplevel vdevs have capacity stats */
2895 if (vs->vs_space == 0) {
2897 (void) printf("\t-\t-\t-");
2899 (void) printf(" - - -");
2901 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2903 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2905 print_one_column(ZPOOL_PROP_FREE,
2906 vs->vs_space - vs->vs_alloc, scripted);
2908 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2910 (void) printf("\n");
2913 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2914 &child, &children) != 0)
2917 for (c = 0; c < children; c++) {
2918 uint64_t ishole = B_FALSE;
2920 if (nvlist_lookup_uint64(child[c],
2921 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2924 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2925 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2930 * Include level 2 ARC devices in iostat output
2932 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2933 &child, &children) != 0)
2937 (void) printf("%-*s - - - - - "
2938 "-\n", cb->cb_namewidth, "cache");
2939 for (c = 0; c < children; c++) {
2940 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2942 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2950 * Generic callback function to list a pool.
2953 list_callback(zpool_handle_t *zhp, void *data)
2955 list_cbdata_t *cbp = data;
2959 config = zpool_get_config(zhp, NULL);
2961 print_pool(zhp, cbp);
2962 if (!cbp->cb_verbose)
2965 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2967 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2973 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2975 * -H Scripted mode. Don't display headers, and separate properties
2977 * -o List of properties to display. Defaults to
2978 * "name,size,allocated,free,capacity,health,altroot"
2979 * -T Display a timestamp in date(1) or Unix format
2981 * List all pools in the system, whether or not they're healthy. Output space
2982 * statistics for each one, as well as health status summary.
2985 zpool_do_list(int argc, char **argv)
2989 list_cbdata_t cb = { 0 };
2990 static char default_props[] =
2991 "name,size,allocated,free,capacity,dedupratio,"
2993 char *props = default_props;
2994 unsigned long interval = 0, count = 0;
2996 boolean_t first = B_TRUE;
2999 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3002 cb.cb_scripted = B_TRUE;
3008 get_timestamp_arg(*optarg);
3011 cb.cb_verbose = B_TRUE;
3014 (void) fprintf(stderr, gettext("missing argument for "
3015 "'%c' option\n"), optopt);
3019 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3028 get_interval_count(&argc, argv, &interval, &count);
3030 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3033 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3036 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3037 (void) printf(gettext("no pools available\n"));
3038 zprop_free_list(cb.cb_proplist);
3043 pool_list_update(list);
3045 if (pool_list_count(list) == 0)
3048 cb.cb_namewidth = 0;
3049 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3051 if (timestamp_fmt != NODATE)
3052 print_timestamp(timestamp_fmt);
3054 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3058 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3063 if (count != 0 && --count == 0)
3066 (void) sleep(interval);
3069 zprop_free_list(cb.cb_proplist);
3074 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3081 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3082 &child, &children) != 0) {
3083 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3084 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3085 name += sizeof(_PATH_DEV) - 1;
3086 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3087 path += sizeof(_PATH_DEV) - 1;
3088 if (strcmp(name, path) == 0)
3093 for (c = 0; c < children; c++)
3094 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3101 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3103 boolean_t force = B_FALSE;
3106 char *poolname, *old_disk, *new_disk;
3107 zpool_handle_t *zhp;
3111 while ((c = getopt(argc, argv, "f")) != -1) {
3117 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3126 /* get pool name and check number of arguments */
3128 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3135 (void) fprintf(stderr,
3136 gettext("missing <device> specification\n"));
3144 (void) fprintf(stderr,
3145 gettext("missing <new_device> specification\n"));
3148 new_disk = old_disk;
3158 (void) fprintf(stderr, gettext("too many arguments\n"));
3162 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3165 if (zpool_get_config(zhp, NULL) == NULL) {
3166 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3172 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3174 if (nvroot == NULL) {
3179 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3181 nvlist_free(nvroot);
3188 * zpool replace [-f] <pool> <device> <new_device>
3190 * -f Force attach, even if <new_device> appears to be in use.
3192 * Replace <device> with <new_device>.
3196 zpool_do_replace(int argc, char **argv)
3198 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3202 * zpool attach [-f] <pool> <device> <new_device>
3204 * -f Force attach, even if <new_device> appears to be in use.
3206 * Attach <new_device> to the mirror containing <device>. If <device> is not
3207 * part of a mirror, then <device> will be transformed into a mirror of
3208 * <device> and <new_device>. In either case, <new_device> will begin life
3209 * with a DTL of [0, now], and will immediately begin to resilver itself.
3212 zpool_do_attach(int argc, char **argv)
3214 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3218 * zpool detach [-f] <pool> <device>
3220 * -f Force detach of <device>, even if DTLs argue against it
3221 * (not supported yet)
3223 * Detach a device from a mirror. The operation will be refused if <device>
3224 * is the last device in the mirror, or if the DTLs indicate that this device
3225 * has the only valid copy of some data.
3229 zpool_do_detach(int argc, char **argv)
3232 char *poolname, *path;
3233 zpool_handle_t *zhp;
3237 while ((c = getopt(argc, argv, "f")) != -1) {
3241 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3250 /* get pool name and check number of arguments */
3252 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3257 (void) fprintf(stderr,
3258 gettext("missing <device> specification\n"));
3265 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3268 ret = zpool_vdev_detach(zhp, path);
3276 * zpool split [-n] [-o prop=val] ...
3278 * [-R altroot] <pool> <newpool> [<device> ...]
3280 * -n Do not split the pool, but display the resulting layout if
3281 * it were to be split.
3282 * -o Set property=value, or set mount options.
3283 * -R Mount the split-off pool under an alternate root.
3285 * Splits the named pool and gives it the new pool name. Devices to be split
3286 * off may be listed, provided that no more than one device is specified
3287 * per top-level vdev mirror. The newly split pool is left in an exported
3288 * state unless -R is specified.
3290 * Restrictions: the top-level of the pool pool must only be made up of
3291 * mirrors; all devices in the pool must be healthy; no device may be
3292 * undergoing a resilvering operation.
3295 zpool_do_split(int argc, char **argv)
3297 char *srcpool, *newpool, *propval;
3298 char *mntopts = NULL;
3301 zpool_handle_t *zhp;
3302 nvlist_t *config, *props = NULL;
3304 flags.dryrun = B_FALSE;
3305 flags.import = B_FALSE;
3308 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3311 flags.import = B_TRUE;
3313 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3314 &props, B_TRUE) != 0) {
3321 flags.dryrun = B_TRUE;
3324 if ((propval = strchr(optarg, '=')) != NULL) {
3327 if (add_prop_list(optarg, propval,
3328 &props, B_TRUE) != 0) {
3338 (void) fprintf(stderr, gettext("missing argument for "
3339 "'%c' option\n"), optopt);
3343 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3350 if (!flags.import && mntopts != NULL) {
3351 (void) fprintf(stderr, gettext("setting mntopts is only "
3352 "valid when importing the pool\n"));
3360 (void) fprintf(stderr, gettext("Missing pool name\n"));
3364 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3374 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3377 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3378 if (config == NULL) {
3382 (void) printf(gettext("would create '%s' with the "
3383 "following layout:\n\n"), newpool);
3384 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3386 nvlist_free(config);
3391 if (ret != 0 || flags.dryrun || !flags.import)
3395 * The split was successful. Now we need to open the new
3396 * pool and import it.
3398 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3400 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3401 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3403 (void) fprintf(stderr, gettext("Split was successful, but "
3404 "the datasets could not all be mounted\n"));
3405 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3406 "different altroot\n"), "zpool import");
3416 * zpool online <pool> <device> ...
3419 zpool_do_online(int argc, char **argv)
3423 zpool_handle_t *zhp;
3425 vdev_state_t newstate;
3429 while ((c = getopt(argc, argv, "et")) != -1) {
3432 flags |= ZFS_ONLINE_EXPAND;
3436 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3445 /* get pool name and check number of arguments */
3447 (void) fprintf(stderr, gettext("missing pool name\n"));
3451 (void) fprintf(stderr, gettext("missing device name\n"));
3457 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3460 for (i = 1; i < argc; i++) {
3461 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3462 if (newstate != VDEV_STATE_HEALTHY) {
3463 (void) printf(gettext("warning: device '%s' "
3464 "onlined, but remains in faulted state\n"),
3466 if (newstate == VDEV_STATE_FAULTED)
3467 (void) printf(gettext("use 'zpool "
3468 "clear' to restore a faulted "
3471 (void) printf(gettext("use 'zpool "
3472 "replace' to replace devices "
3473 "that are no longer present\n"));
3486 * zpool offline [-ft] <pool> <device> ...
3488 * -f Force the device into the offline state, even if doing
3489 * so would appear to compromise pool availability.
3490 * (not supported yet)
3492 * -t Only take the device off-line temporarily. The offline
3493 * state will not be persistent across reboots.
3497 zpool_do_offline(int argc, char **argv)
3501 zpool_handle_t *zhp;
3503 boolean_t istmp = B_FALSE;
3506 while ((c = getopt(argc, argv, "ft")) != -1) {
3513 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3522 /* get pool name and check number of arguments */
3524 (void) fprintf(stderr, gettext("missing pool name\n"));
3528 (void) fprintf(stderr, gettext("missing device name\n"));
3534 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3537 for (i = 1; i < argc; i++) {
3538 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3548 * zpool clear <pool> [device]
3550 * Clear all errors associated with a pool or a particular device.
3553 zpool_do_clear(int argc, char **argv)
3557 boolean_t dryrun = B_FALSE;
3558 boolean_t do_rewind = B_FALSE;
3559 boolean_t xtreme_rewind = B_FALSE;
3560 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3561 nvlist_t *policy = NULL;
3562 zpool_handle_t *zhp;
3563 char *pool, *device;
3566 while ((c = getopt(argc, argv, "FnX")) != -1) {
3575 xtreme_rewind = B_TRUE;
3578 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3588 (void) fprintf(stderr, gettext("missing pool name\n"));
3593 (void) fprintf(stderr, gettext("too many arguments\n"));
3597 if ((dryrun || xtreme_rewind) && !do_rewind) {
3598 (void) fprintf(stderr,
3599 gettext("-n or -X only meaningful with -F\n"));
3603 rewind_policy = ZPOOL_TRY_REWIND;
3605 rewind_policy = ZPOOL_DO_REWIND;
3607 rewind_policy |= ZPOOL_EXTREME_REWIND;
3609 /* In future, further rewind policy choices can be passed along here */
3610 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3611 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3615 device = argc == 2 ? argv[1] : NULL;
3617 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3618 nvlist_free(policy);
3622 if (zpool_clear(zhp, device, policy) != 0)
3627 nvlist_free(policy);
3633 * zpool reguid <pool>
3636 zpool_do_reguid(int argc, char **argv)
3640 zpool_handle_t *zhp;
3644 while ((c = getopt(argc, argv, "")) != -1) {
3647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3656 /* get pool name and check number of arguments */
3658 (void) fprintf(stderr, gettext("missing pool name\n"));
3663 (void) fprintf(stderr, gettext("too many arguments\n"));
3668 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3671 ret = zpool_reguid(zhp);
3679 * zpool reopen <pool>
3681 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3683 * NOTE: This command is currently undocumented. If the command is ever
3684 * exposed then the appropriate usage() messages will need to be made.
3687 zpool_do_reopen(int argc, char **argv)
3690 zpool_handle_t *zhp;
3700 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3703 ret = zpool_reopen(zhp);
3708 typedef struct scrub_cbdata {
3715 scrub_callback(zpool_handle_t *zhp, void *data)
3717 scrub_cbdata_t *cb = data;
3721 * Ignore faulted pools.
3723 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3724 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3725 "currently unavailable\n"), zpool_get_name(zhp));
3729 err = zpool_scan(zhp, cb->cb_type);
3735 * zpool scrub [-s] <pool> ...
3737 * -s Stop. Stops any in-progress scrub.
3740 zpool_do_scrub(int argc, char **argv)
3745 cb.cb_type = POOL_SCAN_SCRUB;
3748 while ((c = getopt(argc, argv, "s")) != -1) {
3751 cb.cb_type = POOL_SCAN_NONE;
3754 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3766 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3770 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3773 typedef struct status_cbdata {
3775 boolean_t cb_allpools;
3776 boolean_t cb_verbose;
3777 boolean_t cb_explain;
3779 boolean_t cb_dedup_stats;
3783 * Print out detailed scrub status.
3786 print_scan_status(pool_scan_stat_t *ps)
3789 uint64_t elapsed, mins_left, hours_left;
3790 uint64_t pass_exam, examined, total;
3792 double fraction_done;
3793 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3795 (void) printf(gettext(" scan: "));
3797 /* If there's never been a scan, there's not much to say. */
3798 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3799 ps->pss_func >= POOL_SCAN_FUNCS) {
3800 (void) printf(gettext("none requested\n"));
3804 start = ps->pss_start_time;
3805 end = ps->pss_end_time;
3806 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3808 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3809 ps->pss_func == POOL_SCAN_RESILVER);
3811 * Scan is finished or canceled.
3813 if (ps->pss_state == DSS_FINISHED) {
3814 uint64_t minutes_taken = (end - start) / 60;
3817 if (ps->pss_func == POOL_SCAN_SCRUB) {
3818 fmt = gettext("scrub repaired %s in %lluh%um with "
3819 "%llu errors on %s");
3820 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3821 fmt = gettext("resilvered %s in %lluh%um with "
3822 "%llu errors on %s");
3825 (void) printf(fmt, processed_buf,
3826 (u_longlong_t)(minutes_taken / 60),
3827 (uint_t)(minutes_taken % 60),
3828 (u_longlong_t)ps->pss_errors,
3829 ctime((time_t *)&end));
3831 } else if (ps->pss_state == DSS_CANCELED) {
3832 if (ps->pss_func == POOL_SCAN_SCRUB) {
3833 (void) printf(gettext("scrub canceled on %s"),
3835 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3836 (void) printf(gettext("resilver canceled on %s"),
3842 assert(ps->pss_state == DSS_SCANNING);
3845 * Scan is in progress.
3847 if (ps->pss_func == POOL_SCAN_SCRUB) {
3848 (void) printf(gettext("scrub in progress since %s"),
3850 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3851 (void) printf(gettext("resilver in progress since %s"),
3855 examined = ps->pss_examined ? ps->pss_examined : 1;
3856 total = ps->pss_to_examine;
3857 fraction_done = (double)examined / total;
3859 /* elapsed time for this pass */
3860 elapsed = time(NULL) - ps->pss_pass_start;
3861 elapsed = elapsed ? elapsed : 1;
3862 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3863 rate = pass_exam / elapsed;
3864 rate = rate ? rate : 1;
3865 mins_left = ((total - examined) / rate) / 60;
3866 hours_left = mins_left / 60;
3868 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3869 zfs_nicenum(total, total_buf, sizeof (total_buf));
3870 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3873 * do not print estimated time if hours_left is more than 30 days
3875 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3876 examined_buf, total_buf, rate_buf);
3877 if (hours_left < (30 * 24)) {
3878 (void) printf(gettext(", %lluh%um to go\n"),
3879 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3881 (void) printf(gettext(
3882 ", (scan is slow, no estimated time)\n"));
3885 if (ps->pss_func == POOL_SCAN_RESILVER) {
3886 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3887 processed_buf, 100 * fraction_done);
3888 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3889 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3890 processed_buf, 100 * fraction_done);
3895 print_error_log(zpool_handle_t *zhp)
3897 nvlist_t *nverrlist = NULL;
3900 size_t len = MAXPATHLEN * 2;
3902 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3903 (void) printf("errors: List of errors unavailable "
3904 "(insufficient privileges)\n");
3908 (void) printf("errors: Permanent errors have been "
3909 "detected in the following files:\n\n");
3911 pathname = safe_malloc(len);
3913 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3915 uint64_t dsobj, obj;
3917 verify(nvpair_value_nvlist(elem, &nv) == 0);
3918 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3920 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3922 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3923 (void) printf("%7s %s\n", "", pathname);
3926 nvlist_free(nverrlist);
3930 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3939 (void) printf(gettext("\tspares\n"));
3941 for (i = 0; i < nspares; i++) {
3942 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3943 print_status_config(zhp, name, spares[i],
3944 namewidth, 2, B_TRUE);
3950 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3959 (void) printf(gettext("\tcache\n"));
3961 for (i = 0; i < nl2cache; i++) {
3962 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3963 print_status_config(zhp, name, l2cache[i],
3964 namewidth, 2, B_FALSE);
3970 print_dedup_stats(nvlist_t *config)
3972 ddt_histogram_t *ddh;
3978 * If the pool was faulted then we may not have been able to
3979 * obtain the config. Otherwise, if have anything in the dedup
3980 * table continue processing the stats.
3982 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3983 (uint64_t **)&ddo, &c) != 0)
3986 (void) printf("\n");
3987 (void) printf(gettext(" dedup: "));
3988 if (ddo->ddo_count == 0) {
3989 (void) printf(gettext("no DDT entries\n"));
3993 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3994 (u_longlong_t)ddo->ddo_count,
3995 (u_longlong_t)ddo->ddo_dspace,
3996 (u_longlong_t)ddo->ddo_mspace);
3998 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3999 (uint64_t **)&dds, &c) == 0);
4000 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4001 (uint64_t **)&ddh, &c) == 0);
4002 zpool_dump_ddt(dds, ddh);
4006 * Display a summary of pool status. Displays a summary such as:
4010 * reason: One or more devices ...
4011 * see: http://illumos.org/msg/ZFS-xxxx-01
4017 * When given the '-v' option, we print out the complete config. If the '-e'
4018 * option is specified, then we print out error rate information as well.
4021 status_callback(zpool_handle_t *zhp, void *data)
4023 status_cbdata_t *cbp = data;
4024 nvlist_t *config, *nvroot;
4031 config = zpool_get_config(zhp, NULL);
4032 reason = zpool_get_status(zhp, &msgid);
4037 * If we were given 'zpool status -x', only report those pools with
4040 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
4041 if (!cbp->cb_allpools) {
4042 (void) printf(gettext("pool '%s' is healthy\n"),
4043 zpool_get_name(zhp));
4045 cbp->cb_first = B_FALSE;
4051 cbp->cb_first = B_FALSE;
4053 (void) printf("\n");
4055 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4057 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4058 (uint64_t **)&vs, &c) == 0);
4059 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4061 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4062 (void) printf(gettext(" state: %s\n"), health);
4065 case ZPOOL_STATUS_MISSING_DEV_R:
4066 (void) printf(gettext("status: One or more devices could not "
4067 "be opened. Sufficient replicas exist for\n\tthe pool to "
4068 "continue functioning in a degraded state.\n"));
4069 (void) printf(gettext("action: Attach the missing device and "
4070 "online it using 'zpool online'.\n"));
4073 case ZPOOL_STATUS_MISSING_DEV_NR:
4074 (void) printf(gettext("status: One or more devices could not "
4075 "be opened. There are insufficient\n\treplicas for the "
4076 "pool to continue functioning.\n"));
4077 (void) printf(gettext("action: Attach the missing device and "
4078 "online it using 'zpool online'.\n"));
4081 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4082 (void) printf(gettext("status: One or more devices could not "
4083 "be used because the label is missing or\n\tinvalid. "
4084 "Sufficient replicas exist for the pool to continue\n\t"
4085 "functioning in a degraded state.\n"));
4086 (void) printf(gettext("action: Replace the device using "
4087 "'zpool replace'.\n"));
4090 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4091 (void) printf(gettext("status: One or more devices could not "
4092 "be used because the label is missing \n\tor invalid. "
4093 "There are insufficient replicas for the pool to "
4094 "continue\n\tfunctioning.\n"));
4095 zpool_explain_recover(zpool_get_handle(zhp),
4096 zpool_get_name(zhp), reason, config);
4099 case ZPOOL_STATUS_FAILING_DEV:
4100 (void) printf(gettext("status: One or more devices has "
4101 "experienced an unrecoverable error. An\n\tattempt was "
4102 "made to correct the error. Applications are "
4104 (void) printf(gettext("action: Determine if the device needs "
4105 "to be replaced, and clear the errors\n\tusing "
4106 "'zpool clear' or replace the device with 'zpool "
4110 case ZPOOL_STATUS_OFFLINE_DEV:
4111 (void) printf(gettext("status: One or more devices has "
4112 "been taken offline by the administrator.\n\tSufficient "
4113 "replicas exist for the pool to continue functioning in "
4114 "a\n\tdegraded state.\n"));
4115 (void) printf(gettext("action: Online the device using "
4116 "'zpool online' or replace the device with\n\t'zpool "
4120 case ZPOOL_STATUS_REMOVED_DEV:
4121 (void) printf(gettext("status: One or more devices has "
4122 "been removed by the administrator.\n\tSufficient "
4123 "replicas exist for the pool to continue functioning in "
4124 "a\n\tdegraded state.\n"));
4125 (void) printf(gettext("action: Online the device using "
4126 "'zpool online' or replace the device with\n\t'zpool "
4130 case ZPOOL_STATUS_RESILVERING:
4131 (void) printf(gettext("status: One or more devices is "
4132 "currently being resilvered. The pool will\n\tcontinue "
4133 "to function, possibly in a degraded state.\n"));
4134 (void) printf(gettext("action: Wait for the resilver to "
4138 case ZPOOL_STATUS_CORRUPT_DATA:
4139 (void) printf(gettext("status: One or more devices has "
4140 "experienced an error resulting in data\n\tcorruption. "
4141 "Applications may be affected.\n"));
4142 (void) printf(gettext("action: Restore the file in question "
4143 "if possible. Otherwise restore the\n\tentire pool from "
4147 case ZPOOL_STATUS_CORRUPT_POOL:
4148 (void) printf(gettext("status: The pool metadata is corrupted "
4149 "and the pool cannot be opened.\n"));
4150 zpool_explain_recover(zpool_get_handle(zhp),
4151 zpool_get_name(zhp), reason, config);
4154 case ZPOOL_STATUS_VERSION_OLDER:
4155 (void) printf(gettext("status: The pool is formatted using a "
4156 "legacy on-disk format. The pool can\n\tstill be used, "
4157 "but some features are unavailable.\n"));
4158 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4159 "upgrade'. Once this is done, the\n\tpool will no longer "
4160 "be accessible on software that does not support feature\n"
4164 case ZPOOL_STATUS_VERSION_NEWER:
4165 (void) printf(gettext("status: The pool has been upgraded to a "
4166 "newer, incompatible on-disk version.\n\tThe pool cannot "
4167 "be accessed on this system.\n"));
4168 (void) printf(gettext("action: Access the pool from a system "
4169 "running more recent software, or\n\trestore the pool from "
4173 case ZPOOL_STATUS_FEAT_DISABLED:
4174 (void) printf(gettext("status: Some supported features are not "
4175 "enabled on the pool. The pool can\n\tstill be used, but "
4176 "some features are unavailable.\n"));
4177 (void) printf(gettext("action: Enable all features using "
4178 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4179 "longer be accessible by software that does not support\n\t"
4180 "the features. See zpool-features(7) for details.\n"));
4183 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4184 (void) printf(gettext("status: The pool cannot be accessed on "
4185 "this system because it uses the\n\tfollowing feature(s) "
4186 "not supported on this system:\n"));
4187 zpool_print_unsup_feat(config);
4188 (void) printf("\n");
4189 (void) printf(gettext("action: Access the pool from a system "
4190 "that supports the required feature(s),\n\tor restore the "
4191 "pool from backup.\n"));
4194 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4195 (void) printf(gettext("status: The pool can only be accessed "
4196 "in read-only mode on this system. It\n\tcannot be "
4197 "accessed in read-write mode because it uses the "
4198 "following\n\tfeature(s) not supported on this system:\n"));
4199 zpool_print_unsup_feat(config);
4200 (void) printf("\n");
4201 (void) printf(gettext("action: The pool cannot be accessed in "
4202 "read-write mode. Import the pool with\n"
4203 "\t\"-o readonly=on\", access the pool from a system that "
4204 "supports the\n\trequired feature(s), or restore the "
4205 "pool from backup.\n"));
4208 case ZPOOL_STATUS_FAULTED_DEV_R:
4209 (void) printf(gettext("status: One or more devices are "
4210 "faulted in response to persistent errors.\n\tSufficient "
4211 "replicas exist for the pool to continue functioning "
4212 "in a\n\tdegraded state.\n"));
4213 (void) printf(gettext("action: Replace the faulted device, "
4214 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4217 case ZPOOL_STATUS_FAULTED_DEV_NR:
4218 (void) printf(gettext("status: One or more devices are "
4219 "faulted in response to persistent errors. There are "
4220 "insufficient replicas for the pool to\n\tcontinue "
4222 (void) printf(gettext("action: Destroy and re-create the pool "
4223 "from a backup source. Manually marking the device\n"
4224 "\trepaired using 'zpool clear' may allow some data "
4225 "to be recovered.\n"));
4228 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4229 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4230 (void) printf(gettext("status: One or more devices are "
4231 "faulted in response to IO failures.\n"));
4232 (void) printf(gettext("action: Make sure the affected devices "
4233 "are connected, then run 'zpool clear'.\n"));
4236 case ZPOOL_STATUS_BAD_LOG:
4237 (void) printf(gettext("status: An intent log record "
4238 "could not be read.\n"
4239 "\tWaiting for adminstrator intervention to fix the "
4240 "faulted pool.\n"));
4241 (void) printf(gettext("action: Either restore the affected "
4242 "device(s) and run 'zpool online',\n"
4243 "\tor ignore the intent log records by running "
4244 "'zpool clear'.\n"));
4249 * The remaining errors can't actually be generated, yet.
4251 assert(reason == ZPOOL_STATUS_OK);
4255 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4258 if (config != NULL) {
4261 nvlist_t **spares, **l2cache;
4262 uint_t nspares, nl2cache;
4263 pool_scan_stat_t *ps = NULL;
4265 (void) nvlist_lookup_uint64_array(nvroot,
4266 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4267 print_scan_status(ps);
4269 namewidth = max_width(zhp, nvroot, 0, 0);
4273 (void) printf(gettext("config:\n\n"));
4274 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4275 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4276 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4277 namewidth, 0, B_FALSE);
4279 if (num_logs(nvroot) > 0)
4280 print_logs(zhp, nvroot, namewidth, B_TRUE);
4281 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4282 &l2cache, &nl2cache) == 0)
4283 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4285 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4286 &spares, &nspares) == 0)
4287 print_spares(zhp, spares, nspares, namewidth);
4289 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4291 nvlist_t *nverrlist = NULL;
4294 * If the approximate error count is small, get a
4295 * precise count by fetching the entire log and
4296 * uniquifying the results.
4298 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4299 zpool_get_errlog(zhp, &nverrlist) == 0) {
4304 while ((elem = nvlist_next_nvpair(nverrlist,
4309 nvlist_free(nverrlist);
4311 (void) printf("\n");
4314 (void) printf(gettext("errors: No known data "
4316 else if (!cbp->cb_verbose)
4317 (void) printf(gettext("errors: %llu data "
4318 "errors, use '-v' for a list\n"),
4319 (u_longlong_t)nerr);
4321 print_error_log(zhp);
4324 if (cbp->cb_dedup_stats)
4325 print_dedup_stats(config);
4327 (void) printf(gettext("config: The configuration cannot be "
4335 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4337 * -v Display complete error logs
4338 * -x Display only pools with potential problems
4339 * -D Display dedup status (undocumented)
4340 * -T Display a timestamp in date(1) or Unix format
4342 * Describes the health status of all pools or some subset.
4345 zpool_do_status(int argc, char **argv)
4349 unsigned long interval = 0, count = 0;
4350 status_cbdata_t cb = { 0 };
4353 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4356 cb.cb_verbose = B_TRUE;
4359 cb.cb_explain = B_TRUE;
4362 cb.cb_dedup_stats = B_TRUE;
4365 get_timestamp_arg(*optarg);
4368 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4377 get_interval_count(&argc, argv, &interval, &count);
4380 cb.cb_allpools = B_TRUE;
4382 cb.cb_first = B_TRUE;
4385 if (timestamp_fmt != NODATE)
4386 print_timestamp(timestamp_fmt);
4388 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4389 status_callback, &cb);
4391 if (argc == 0 && cb.cb_count == 0)
4392 (void) printf(gettext("no pools available\n"));
4393 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4394 (void) printf(gettext("all pools are healthy\n"));
4402 if (count != 0 && --count == 0)
4405 (void) sleep(interval);
4411 typedef struct upgrade_cbdata {
4413 char cb_poolname[ZPOOL_MAXNAMELEN];
4415 uint64_t cb_version;
4421 is_root_pool(zpool_handle_t *zhp)
4423 static struct statfs sfs;
4424 static char *poolname = NULL;
4425 static boolean_t stated = B_FALSE;
4430 if (statfs("/", &sfs) == -1) {
4431 (void) fprintf(stderr,
4432 "Unable to stat root file system: %s.\n",
4436 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4438 poolname = sfs.f_mntfromname;
4439 if ((slash = strchr(poolname, '/')) != NULL)
4442 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4446 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4448 if (poolname[0] == '\0' && is_root_pool(zhp))
4449 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4451 #endif /* FreeBSD */
4454 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4458 uint64_t oldversion;
4460 config = zpool_get_config(zhp, NULL);
4461 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4464 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4465 assert(oldversion < version);
4467 ret = zpool_upgrade(zhp, version);
4471 if (version >= SPA_VERSION_FEATURES) {
4472 (void) printf(gettext("Successfully upgraded "
4473 "'%s' from version %llu to feature flags.\n"),
4474 zpool_get_name(zhp), oldversion);
4476 (void) printf(gettext("Successfully upgraded "
4477 "'%s' from version %llu to version %llu.\n"),
4478 zpool_get_name(zhp), oldversion, version);
4485 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4488 boolean_t firstff = B_TRUE;
4489 nvlist_t *enabled = zpool_get_features(zhp);
4492 for (i = 0; i < SPA_FEATURES; i++) {
4493 const char *fname = spa_feature_table[i].fi_uname;
4494 const char *fguid = spa_feature_table[i].fi_guid;
4495 if (!nvlist_exists(enabled, fguid)) {
4497 verify(-1 != asprintf(&propname, "feature@%s", fname));
4498 ret = zpool_set_prop(zhp, propname,
4499 ZFS_FEATURE_ENABLED);
4507 (void) printf(gettext("Enabled the "
4508 "following features on '%s':\n"),
4509 zpool_get_name(zhp));
4512 (void) printf(gettext(" %s\n"), fname);
4523 upgrade_cb(zpool_handle_t *zhp, void *arg)
4525 upgrade_cbdata_t *cbp = arg;
4528 boolean_t printnl = B_FALSE;
4531 config = zpool_get_config(zhp, NULL);
4532 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4535 assert(SPA_VERSION_IS_SUPPORTED(version));
4537 if (version < cbp->cb_version) {
4538 cbp->cb_first = B_FALSE;
4539 ret = upgrade_version(zhp, cbp->cb_version);
4543 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4544 sizeof(cbp->cb_poolname));
4545 #endif /* ___FreeBSD__ */
4550 * If they did "zpool upgrade -a", then we could
4551 * be doing ioctls to different pools. We need
4552 * to log this history once to each pool, and bypass
4553 * the normal history logging that happens in main().
4555 (void) zpool_log_history(g_zfs, history_str);
4556 log_history = B_FALSE;
4560 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4562 ret = upgrade_enable_all(zhp, &count);
4567 cbp->cb_first = B_FALSE;
4570 * If they did "zpool upgrade -a", then we could
4571 * be doing ioctls to different pools. We need
4572 * to log this history once to each pool, and bypass
4573 * the normal history logging that happens in main().
4575 (void) zpool_log_history(g_zfs, history_str);
4576 log_history = B_FALSE;
4581 (void) printf(gettext("\n"));
4588 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4590 upgrade_cbdata_t *cbp = arg;
4594 config = zpool_get_config(zhp, NULL);
4595 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4598 assert(SPA_VERSION_IS_SUPPORTED(version));
4600 if (version < SPA_VERSION_FEATURES) {
4601 if (cbp->cb_first) {
4602 (void) printf(gettext("The following pools are "
4603 "formatted with legacy version numbers and can\n"
4604 "be upgraded to use feature flags. After "
4605 "being upgraded, these pools\nwill no "
4606 "longer be accessible by software that does not "
4607 "support feature\nflags.\n\n"));
4608 (void) printf(gettext("VER POOL\n"));
4609 (void) printf(gettext("--- ------------\n"));
4610 cbp->cb_first = B_FALSE;
4613 (void) printf("%2llu %s\n", (u_longlong_t)version,
4614 zpool_get_name(zhp));
4621 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4623 upgrade_cbdata_t *cbp = arg;
4627 config = zpool_get_config(zhp, NULL);
4628 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4631 if (version >= SPA_VERSION_FEATURES) {
4633 boolean_t poolfirst = B_TRUE;
4634 nvlist_t *enabled = zpool_get_features(zhp);
4636 for (i = 0; i < SPA_FEATURES; i++) {
4637 const char *fguid = spa_feature_table[i].fi_guid;
4638 const char *fname = spa_feature_table[i].fi_uname;
4639 if (!nvlist_exists(enabled, fguid)) {
4640 if (cbp->cb_first) {
4641 (void) printf(gettext("\nSome "
4642 "supported features are not "
4643 "enabled on the following pools. "
4644 "Once a\nfeature is enabled the "
4645 "pool may become incompatible with "
4646 "software\nthat does not support "
4648 "zpool-features(7) for "
4650 (void) printf(gettext("POOL "
4652 (void) printf(gettext("------"
4654 cbp->cb_first = B_FALSE;
4658 (void) printf(gettext("%s\n"),
4659 zpool_get_name(zhp));
4660 poolfirst = B_FALSE;
4663 (void) printf(gettext(" %s\n"), fname);
4673 upgrade_one(zpool_handle_t *zhp, void *data)
4675 boolean_t printnl = B_FALSE;
4676 upgrade_cbdata_t *cbp = data;
4677 uint64_t cur_version;
4680 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4681 (void) printf(gettext("'log' is now a reserved word\n"
4682 "Pool 'log' must be renamed using export and import"
4687 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4688 if (cur_version > cbp->cb_version) {
4689 (void) printf(gettext("Pool '%s' is already formatted "
4690 "using more current version '%llu'.\n\n"),
4691 zpool_get_name(zhp), cur_version);
4695 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4696 (void) printf(gettext("Pool '%s' is already formatted "
4697 "using version %llu.\n\n"), zpool_get_name(zhp),
4702 if (cur_version != cbp->cb_version) {
4704 ret = upgrade_version(zhp, cbp->cb_version);
4708 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4709 sizeof(cbp->cb_poolname));
4710 #endif /* ___FreeBSD__ */
4713 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4715 ret = upgrade_enable_all(zhp, &count);
4722 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4723 sizeof(cbp->cb_poolname));
4724 #endif /* __FreeBSD __*/
4725 } else if (cur_version == SPA_VERSION) {
4726 (void) printf(gettext("Pool '%s' already has all "
4727 "supported features enabled.\n"),
4728 zpool_get_name(zhp));
4733 (void) printf(gettext("\n"));
4742 * zpool upgrade [-V version] <-a | pool ...>
4744 * With no arguments, display downrev'd ZFS pool available for upgrade.
4745 * Individual pools can be upgraded by specifying the pool, and '-a' will
4746 * upgrade all pools.
4749 zpool_do_upgrade(int argc, char **argv)
4752 upgrade_cbdata_t cb = { 0 };
4754 boolean_t showversions = B_FALSE;
4755 boolean_t upgradeall = B_FALSE;
4760 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4763 upgradeall = B_TRUE;
4766 showversions = B_TRUE;
4769 cb.cb_version = strtoll(optarg, &end, 10);
4771 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4772 (void) fprintf(stderr,
4773 gettext("invalid version '%s'\n"), optarg);
4778 (void) fprintf(stderr, gettext("missing argument for "
4779 "'%c' option\n"), optopt);
4783 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4794 if (cb.cb_version == 0) {
4795 cb.cb_version = SPA_VERSION;
4796 } else if (!upgradeall && argc == 0) {
4797 (void) fprintf(stderr, gettext("-V option is "
4798 "incompatible with other arguments\n"));
4803 if (upgradeall || argc != 0) {
4804 (void) fprintf(stderr, gettext("-v option is "
4805 "incompatible with other arguments\n"));
4808 } else if (upgradeall) {
4810 (void) fprintf(stderr, gettext("-a option should not "
4811 "be used along with a pool name\n"));
4816 (void) printf(gettext("This system supports ZFS pool feature "
4821 (void) printf(gettext("The following features are "
4823 (void) printf(gettext("FEAT DESCRIPTION\n"));
4824 (void) printf("----------------------------------------------"
4825 "---------------\n");
4826 for (i = 0; i < SPA_FEATURES; i++) {
4827 zfeature_info_t *fi = &spa_feature_table[i];
4828 const char *ro = fi->fi_can_readonly ?
4829 " (read-only compatible)" : "";
4831 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4832 (void) printf(" %s\n", fi->fi_desc);
4834 (void) printf("\n");
4836 (void) printf(gettext("The following legacy versions are also "
4838 (void) printf(gettext("VER DESCRIPTION\n"));
4839 (void) printf("--- -----------------------------------------"
4840 "---------------\n");
4841 (void) printf(gettext(" 1 Initial ZFS version\n"));
4842 (void) printf(gettext(" 2 Ditto blocks "
4843 "(replicated metadata)\n"));
4844 (void) printf(gettext(" 3 Hot spares and double parity "
4846 (void) printf(gettext(" 4 zpool history\n"));
4847 (void) printf(gettext(" 5 Compression using the gzip "
4849 (void) printf(gettext(" 6 bootfs pool property\n"));
4850 (void) printf(gettext(" 7 Separate intent log devices\n"));
4851 (void) printf(gettext(" 8 Delegated administration\n"));
4852 (void) printf(gettext(" 9 refquota and refreservation "
4854 (void) printf(gettext(" 10 Cache devices\n"));
4855 (void) printf(gettext(" 11 Improved scrub performance\n"));
4856 (void) printf(gettext(" 12 Snapshot properties\n"));
4857 (void) printf(gettext(" 13 snapused property\n"));
4858 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4859 (void) printf(gettext(" 15 user/group space accounting\n"));
4860 (void) printf(gettext(" 16 stmf property support\n"));
4861 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4862 (void) printf(gettext(" 18 Snapshot user holds\n"));
4863 (void) printf(gettext(" 19 Log device removal\n"));
4864 (void) printf(gettext(" 20 Compression using zle "
4865 "(zero-length encoding)\n"));
4866 (void) printf(gettext(" 21 Deduplication\n"));
4867 (void) printf(gettext(" 22 Received properties\n"));
4868 (void) printf(gettext(" 23 Slim ZIL\n"));
4869 (void) printf(gettext(" 24 System attributes\n"));
4870 (void) printf(gettext(" 25 Improved scrub stats\n"));
4871 (void) printf(gettext(" 26 Improved snapshot deletion "
4873 (void) printf(gettext(" 27 Improved snapshot creation "
4875 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4876 (void) printf(gettext("\nFor more information on a particular "
4877 "version, including supported releases,\n"));
4878 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4879 } else if (argc == 0 && upgradeall) {
4880 cb.cb_first = B_TRUE;
4881 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4882 if (ret == 0 && cb.cb_first) {
4883 if (cb.cb_version == SPA_VERSION) {
4884 (void) printf(gettext("All pools are already "
4885 "formatted using feature flags.\n\n"));
4886 (void) printf(gettext("Every feature flags "
4887 "pool already has all supported features "
4890 (void) printf(gettext("All pools are already "
4891 "formatted with version %llu or higher.\n"),
4895 } else if (argc == 0) {
4896 cb.cb_first = B_TRUE;
4897 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4901 (void) printf(gettext("All pools are formatted "
4902 "using feature flags.\n\n"));
4904 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4905 "for a list of available legacy versions.\n"));
4908 cb.cb_first = B_TRUE;
4909 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4913 (void) printf(gettext("Every feature flags pool has "
4914 "all supported features enabled.\n"));
4916 (void) printf(gettext("\n"));
4919 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4923 if (cb.cb_poolname[0] != '\0') {
4925 "If you boot from pool '%s', don't forget to update boot code.\n"
4926 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4927 "the following command will do it:\n"
4929 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4936 typedef struct hist_cbdata {
4943 * Print out the command history for a specific pool.
4946 get_history_one(zpool_handle_t *zhp, void *data)
4952 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4954 cb->first = B_FALSE;
4956 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4958 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4961 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4962 &records, &numrecords) == 0);
4963 for (i = 0; i < numrecords; i++) {
4964 nvlist_t *rec = records[i];
4967 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
4971 tsec = fnvlist_lookup_uint64(records[i],
4973 (void) localtime_r(&tsec, &t);
4974 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4977 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
4978 (void) printf("%s %s", tbuf,
4979 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
4980 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
4982 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
4985 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
4986 (void) printf("%s unrecognized record:\n",
4988 dump_nvlist(rec, 4);
4991 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
4992 zfs_history_event_names[ievent],
4993 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
4994 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
4995 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
4998 (void) printf("%s [txg:%lld] %s", tbuf,
4999 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5000 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5001 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5002 (void) printf(" %s (%llu)",
5003 fnvlist_lookup_string(rec,
5005 fnvlist_lookup_uint64(rec,
5008 (void) printf(" %s", fnvlist_lookup_string(rec,
5009 ZPOOL_HIST_INT_STR));
5010 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5013 (void) printf("%s ioctl %s\n", tbuf,
5014 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5015 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5016 (void) printf(" input:\n");
5017 dump_nvlist(fnvlist_lookup_nvlist(rec,
5018 ZPOOL_HIST_INPUT_NVL), 8);
5020 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5021 (void) printf(" output:\n");
5022 dump_nvlist(fnvlist_lookup_nvlist(rec,
5023 ZPOOL_HIST_OUTPUT_NVL), 8);
5028 (void) printf("%s unrecognized record:\n", tbuf);
5029 dump_nvlist(rec, 4);
5033 (void) printf("\n");
5036 (void) printf(" [");
5037 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5038 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5039 struct passwd *pwd = getpwuid(who);
5040 (void) printf("user %d ", (int)who);
5042 (void) printf("(%s) ", pwd->pw_name);
5044 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5045 (void) printf("on %s",
5046 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5048 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5049 (void) printf(":%s",
5050 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5053 (void) printf("\n");
5055 (void) printf("\n");
5062 * zpool history <pool>
5064 * Displays the history of commands that modified pools.
5067 zpool_do_history(int argc, char **argv)
5069 hist_cbdata_t cbdata = { 0 };
5073 cbdata.first = B_TRUE;
5075 while ((c = getopt(argc, argv, "li")) != -1) {
5078 cbdata.longfmt = B_TRUE;
5081 cbdata.internal = B_TRUE;
5084 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5092 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5095 if (argc == 0 && cbdata.first == B_TRUE) {
5096 (void) printf(gettext("no pools available\n"));
5104 get_callback(zpool_handle_t *zhp, void *data)
5106 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5107 char value[MAXNAMELEN];
5108 zprop_source_t srctype;
5111 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5114 * Skip the special fake placeholder. This will also skip
5115 * over the name property when 'all' is specified.
5117 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5118 pl == cbp->cb_proplist)
5121 if (pl->pl_prop == ZPROP_INVAL &&
5122 (zpool_prop_feature(pl->pl_user_prop) ||
5123 zpool_prop_unsupported(pl->pl_user_prop))) {
5124 srctype = ZPROP_SRC_LOCAL;
5126 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5127 value, sizeof (value)) == 0) {
5128 zprop_print_one_property(zpool_get_name(zhp),
5129 cbp, pl->pl_user_prop, value, srctype,
5133 if (zpool_get_prop(zhp, pl->pl_prop, value,
5134 sizeof (value), &srctype) != 0)
5137 zprop_print_one_property(zpool_get_name(zhp), cbp,
5138 zpool_prop_to_name(pl->pl_prop), value, srctype,
5146 zpool_do_get(int argc, char **argv)
5148 zprop_get_cbdata_t cb = { 0 };
5149 zprop_list_t fake_name = { 0 };
5153 (void) fprintf(stderr, gettext("missing property "
5158 cb.cb_first = B_TRUE;
5159 cb.cb_sources = ZPROP_SRC_ALL;
5160 cb.cb_columns[0] = GET_COL_NAME;
5161 cb.cb_columns[1] = GET_COL_PROPERTY;
5162 cb.cb_columns[2] = GET_COL_VALUE;
5163 cb.cb_columns[3] = GET_COL_SOURCE;
5164 cb.cb_type = ZFS_TYPE_POOL;
5166 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5167 ZFS_TYPE_POOL) != 0)
5170 if (cb.cb_proplist != NULL) {
5171 fake_name.pl_prop = ZPOOL_PROP_NAME;
5172 fake_name.pl_width = strlen(gettext("NAME"));
5173 fake_name.pl_next = cb.cb_proplist;
5174 cb.cb_proplist = &fake_name;
5177 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5180 if (cb.cb_proplist == &fake_name)
5181 zprop_free_list(fake_name.pl_next);
5183 zprop_free_list(cb.cb_proplist);
5188 typedef struct set_cbdata {
5191 boolean_t cb_any_successful;
5195 set_callback(zpool_handle_t *zhp, void *data)
5198 set_cbdata_t *cb = (set_cbdata_t *)data;
5200 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5203 cb->cb_any_successful = B_TRUE;
5209 zpool_do_set(int argc, char **argv)
5211 set_cbdata_t cb = { 0 };
5214 if (argc > 1 && argv[1][0] == '-') {
5215 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5221 (void) fprintf(stderr, gettext("missing property=value "
5227 (void) fprintf(stderr, gettext("missing pool name\n"));
5232 (void) fprintf(stderr, gettext("too many pool names\n"));
5236 cb.cb_propname = argv[1];
5237 cb.cb_value = strchr(cb.cb_propname, '=');
5238 if (cb.cb_value == NULL) {
5239 (void) fprintf(stderr, gettext("missing value in "
5240 "property=value argument\n"));
5244 *(cb.cb_value) = '\0';
5247 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5254 find_command_idx(char *command, int *idx)
5258 for (i = 0; i < NCOMMAND; i++) {
5259 if (command_table[i].name == NULL)
5262 if (strcmp(command, command_table[i].name) == 0) {
5271 main(int argc, char **argv)
5277 (void) setlocale(LC_ALL, "");
5278 (void) textdomain(TEXT_DOMAIN);
5280 if ((g_zfs = libzfs_init()) == NULL) {
5281 (void) fprintf(stderr, gettext("internal error: failed to "
5282 "initialize ZFS library\n"));
5286 libzfs_print_on_error(g_zfs, B_TRUE);
5291 * Make sure the user has specified some command.
5294 (void) fprintf(stderr, gettext("missing command\n"));
5303 if (strcmp(cmdname, "-?") == 0)
5306 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5309 * Run the appropriate command.
5311 if (find_command_idx(cmdname, &i) == 0) {
5312 current_command = &command_table[i];
5313 ret = command_table[i].func(argc - 1, argv + 1);
5314 } else if (strchr(cmdname, '=')) {
5315 verify(find_command_idx("set", &i) == 0);
5316 current_command = &command_table[i];
5317 ret = command_table[i].func(argc, argv);
5318 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5320 * 'freeze' is a vile debugging abomination, so we treat
5324 int fd = open(ZFS_DEV, O_RDWR);
5325 (void) strcpy((void *)buf, argv[2]);
5326 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5328 (void) fprintf(stderr, gettext("unrecognized "
5329 "command '%s'\n"), cmdname);
5333 if (ret == 0 && log_history)
5334 (void) zpool_log_history(g_zfs, history_str);
5339 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5340 * for the purposes of running ::findleaks.
5342 if (getenv("ZFS_ABORT") != NULL) {
5343 (void) printf("dumping core by request\n");