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 zpool_command_t *current_command;
196 static char history_str[HIS_MAX_RECORD_LEN];
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 ret = (zpool_destroy(zhp) != 0);
1104 * zpool export [-f] <pool> ...
1106 * -f Forcefully unmount datasets
1108 * Export the given pools. By default, the command will attempt to cleanly
1109 * unmount any active datasets within the pool. If the '-f' flag is specified,
1110 * then the datasets will be forcefully unmounted.
1113 zpool_do_export(int argc, char **argv)
1115 boolean_t force = B_FALSE;
1116 boolean_t hardforce = B_FALSE;
1118 zpool_handle_t *zhp;
1123 while ((c = getopt(argc, argv, "fF")) != -1) {
1132 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1141 /* check arguments */
1143 (void) fprintf(stderr, gettext("missing pool argument\n"));
1148 for (i = 0; i < argc; i++) {
1149 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1154 if (zpool_disable_datasets(zhp, force) != 0) {
1161 if (zpool_export_force(zhp) != 0)
1163 } else if (zpool_export(zhp, force) != 0) {
1174 * Given a vdev configuration, determine the maximum width needed for the device
1178 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1180 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1185 if (strlen(name) + depth > max)
1186 max = strlen(name) + depth;
1190 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1191 &child, &children) == 0) {
1192 for (c = 0; c < children; c++)
1193 if ((ret = max_width(zhp, child[c], depth + 2,
1198 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1199 &child, &children) == 0) {
1200 for (c = 0; c < children; c++)
1201 if ((ret = max_width(zhp, child[c], depth + 2,
1206 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1207 &child, &children) == 0) {
1208 for (c = 0; c < children; c++)
1209 if ((ret = max_width(zhp, child[c], depth + 2,
1218 typedef struct spare_cbdata {
1220 zpool_handle_t *cb_zhp;
1224 find_vdev(nvlist_t *nv, uint64_t search)
1230 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1234 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1235 &child, &children) == 0) {
1236 for (c = 0; c < children; c++)
1237 if (find_vdev(child[c], search))
1245 find_spare(zpool_handle_t *zhp, void *data)
1247 spare_cbdata_t *cbp = data;
1248 nvlist_t *config, *nvroot;
1250 config = zpool_get_config(zhp, NULL);
1251 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1254 if (find_vdev(nvroot, cbp->cb_guid)) {
1264 * Print out configuration state as requested by status_callback.
1267 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1268 int namewidth, int depth, boolean_t isspare)
1272 pool_scan_stat_t *ps = NULL;
1274 char rbuf[6], wbuf[6], cbuf[6];
1276 uint64_t notpresent;
1280 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1281 &child, &children) != 0)
1284 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1285 (uint64_t **)&vs, &c) == 0);
1287 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1290 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1293 if (vs->vs_aux == VDEV_AUX_SPARED)
1295 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1299 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1303 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1304 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1305 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1306 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1309 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1310 ¬present) == 0 ||
1311 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1313 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1314 (void) printf(" was %s", path);
1315 } else if (vs->vs_aux != 0) {
1318 switch (vs->vs_aux) {
1319 case VDEV_AUX_OPEN_FAILED:
1320 (void) printf(gettext("cannot open"));
1323 case VDEV_AUX_BAD_GUID_SUM:
1324 (void) printf(gettext("missing device"));
1327 case VDEV_AUX_NO_REPLICAS:
1328 (void) printf(gettext("insufficient replicas"));
1331 case VDEV_AUX_VERSION_NEWER:
1332 (void) printf(gettext("newer version"));
1335 case VDEV_AUX_UNSUP_FEAT:
1336 (void) printf(gettext("unsupported feature(s)"));
1339 case VDEV_AUX_SPARED:
1340 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1342 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1343 if (strcmp(zpool_get_name(cb.cb_zhp),
1344 zpool_get_name(zhp)) == 0)
1345 (void) printf(gettext("currently in "
1348 (void) printf(gettext("in use by "
1350 zpool_get_name(cb.cb_zhp));
1351 zpool_close(cb.cb_zhp);
1353 (void) printf(gettext("currently in use"));
1357 case VDEV_AUX_ERR_EXCEEDED:
1358 (void) printf(gettext("too many errors"));
1361 case VDEV_AUX_IO_FAILURE:
1362 (void) printf(gettext("experienced I/O failures"));
1365 case VDEV_AUX_BAD_LOG:
1366 (void) printf(gettext("bad intent log"));
1369 case VDEV_AUX_EXTERNAL:
1370 (void) printf(gettext("external device fault"));
1373 case VDEV_AUX_SPLIT_POOL:
1374 (void) printf(gettext("split into new pool"));
1378 (void) printf(gettext("corrupted data"));
1383 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1384 (uint64_t **)&ps, &c);
1386 if (ps && ps->pss_state == DSS_SCANNING &&
1387 vs->vs_scan_processed != 0 && children == 0) {
1388 (void) printf(gettext(" (%s)"),
1389 (ps->pss_func == POOL_SCAN_RESILVER) ?
1390 "resilvering" : "repairing");
1393 (void) printf("\n");
1395 for (c = 0; c < children; c++) {
1396 uint64_t islog = B_FALSE, ishole = B_FALSE;
1398 /* Don't print logs or holes here */
1399 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1401 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1403 if (islog || ishole)
1405 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1406 print_status_config(zhp, vname, child[c],
1407 namewidth, depth + 2, isspare);
1414 * Print the configuration of an exported pool. Iterate over all vdevs in the
1415 * pool, printing out the name and status for each one.
1418 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1425 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1426 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1427 strcmp(type, VDEV_TYPE_HOLE) == 0)
1430 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1431 (uint64_t **)&vs, &c) == 0);
1433 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1434 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1436 if (vs->vs_aux != 0) {
1439 switch (vs->vs_aux) {
1440 case VDEV_AUX_OPEN_FAILED:
1441 (void) printf(gettext("cannot open"));
1444 case VDEV_AUX_BAD_GUID_SUM:
1445 (void) printf(gettext("missing device"));
1448 case VDEV_AUX_NO_REPLICAS:
1449 (void) printf(gettext("insufficient replicas"));
1452 case VDEV_AUX_VERSION_NEWER:
1453 (void) printf(gettext("newer version"));
1456 case VDEV_AUX_UNSUP_FEAT:
1457 (void) printf(gettext("unsupported feature(s)"));
1460 case VDEV_AUX_ERR_EXCEEDED:
1461 (void) printf(gettext("too many errors"));
1465 (void) printf(gettext("corrupted data"));
1469 (void) printf("\n");
1471 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1472 &child, &children) != 0)
1475 for (c = 0; c < children; c++) {
1476 uint64_t is_log = B_FALSE;
1478 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1483 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1484 print_import_config(vname, child[c], namewidth, depth + 2);
1488 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1489 &child, &children) == 0) {
1490 (void) printf(gettext("\tcache\n"));
1491 for (c = 0; c < children; c++) {
1492 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1493 (void) printf("\t %s\n", vname);
1498 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1499 &child, &children) == 0) {
1500 (void) printf(gettext("\tspares\n"));
1501 for (c = 0; c < children; c++) {
1502 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1503 (void) printf("\t %s\n", vname);
1511 * Logs are recorded as top level vdevs in the main pool child array
1512 * but with "is_log" set to 1. We use either print_status_config() or
1513 * print_import_config() to print the top level logs then any log
1514 * children (eg mirrored slogs) are printed recursively - which
1515 * works because only the top level vdev is marked "is_log"
1518 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1523 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1527 (void) printf(gettext("\tlogs\n"));
1529 for (c = 0; c < children; c++) {
1530 uint64_t is_log = B_FALSE;
1533 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1537 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1539 print_status_config(zhp, name, child[c], namewidth,
1542 print_import_config(name, child[c], namewidth, 2);
1548 * Display the status for the given pool.
1551 show_import(nvlist_t *config)
1553 uint64_t pool_state;
1565 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1567 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1569 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1571 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1574 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1575 (uint64_t **)&vs, &vsc) == 0);
1576 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1578 reason = zpool_import_status(config, &msgid);
1580 (void) printf(gettext(" pool: %s\n"), name);
1581 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1582 (void) printf(gettext(" state: %s"), health);
1583 if (pool_state == POOL_STATE_DESTROYED)
1584 (void) printf(gettext(" (DESTROYED)"));
1585 (void) printf("\n");
1588 case ZPOOL_STATUS_MISSING_DEV_R:
1589 case ZPOOL_STATUS_MISSING_DEV_NR:
1590 case ZPOOL_STATUS_BAD_GUID_SUM:
1591 (void) printf(gettext(" status: One or more devices are "
1592 "missing from the system.\n"));
1595 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1596 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1597 (void) printf(gettext(" status: One or more devices contains "
1598 "corrupted data.\n"));
1601 case ZPOOL_STATUS_CORRUPT_DATA:
1603 gettext(" status: The pool data is corrupted.\n"));
1606 case ZPOOL_STATUS_OFFLINE_DEV:
1607 (void) printf(gettext(" status: One or more devices "
1608 "are offlined.\n"));
1611 case ZPOOL_STATUS_CORRUPT_POOL:
1612 (void) printf(gettext(" status: The pool metadata is "
1616 case ZPOOL_STATUS_VERSION_OLDER:
1617 (void) printf(gettext(" status: The pool is formatted using a "
1618 "legacy on-disk version.\n"));
1621 case ZPOOL_STATUS_VERSION_NEWER:
1622 (void) printf(gettext(" status: The pool is formatted using an "
1623 "incompatible version.\n"));
1626 case ZPOOL_STATUS_FEAT_DISABLED:
1627 (void) printf(gettext(" status: Some supported features are "
1628 "not enabled on the pool.\n"));
1631 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1632 (void) printf(gettext("status: The pool uses the following "
1633 "feature(s) not supported on this sytem:\n"));
1634 zpool_print_unsup_feat(config);
1637 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1638 (void) printf(gettext("status: The pool can only be accessed "
1639 "in read-only mode on this system. It\n\tcannot be "
1640 "accessed in read-write mode because it uses the "
1641 "following\n\tfeature(s) not supported on this system:\n"));
1642 zpool_print_unsup_feat(config);
1645 case ZPOOL_STATUS_HOSTID_MISMATCH:
1646 (void) printf(gettext(" status: The pool was last accessed by "
1647 "another system.\n"));
1650 case ZPOOL_STATUS_FAULTED_DEV_R:
1651 case ZPOOL_STATUS_FAULTED_DEV_NR:
1652 (void) printf(gettext(" status: One or more devices are "
1656 case ZPOOL_STATUS_BAD_LOG:
1657 (void) printf(gettext(" status: An intent log record cannot be "
1661 case ZPOOL_STATUS_RESILVERING:
1662 (void) printf(gettext(" status: One or more devices were being "
1668 * No other status can be seen when importing pools.
1670 assert(reason == ZPOOL_STATUS_OK);
1674 * Print out an action according to the overall state of the pool.
1676 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1677 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1678 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1679 (void) printf(gettext(" action: The pool can be "
1680 "imported using its name or numeric identifier, "
1681 "though\n\tsome features will not be available "
1682 "without an explicit 'zpool upgrade'.\n"));
1683 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1684 (void) printf(gettext(" action: The pool can be "
1685 "imported using its name or numeric "
1686 "identifier and\n\tthe '-f' flag.\n"));
1688 (void) printf(gettext(" action: The pool can be "
1689 "imported using its name or numeric "
1692 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1693 (void) printf(gettext(" action: The pool can be imported "
1694 "despite missing or damaged devices. The\n\tfault "
1695 "tolerance of the pool may be compromised if imported.\n"));
1698 case ZPOOL_STATUS_VERSION_NEWER:
1699 (void) printf(gettext(" action: The pool cannot be "
1700 "imported. Access the pool on a system running "
1701 "newer\n\tsoftware, or recreate the pool from "
1704 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1705 (void) printf(gettext("action: The pool cannot be "
1706 "imported. Access the pool on a system that "
1707 "supports\n\tthe required feature(s), or recreate "
1708 "the pool from backup.\n"));
1710 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1711 (void) printf(gettext("action: The pool cannot be "
1712 "imported in read-write mode. Import the pool "
1714 "\t\"-o readonly=on\", access the pool on a system "
1715 "that supports the\n\trequired feature(s), or "
1716 "recreate the pool from backup.\n"));
1718 case ZPOOL_STATUS_MISSING_DEV_R:
1719 case ZPOOL_STATUS_MISSING_DEV_NR:
1720 case ZPOOL_STATUS_BAD_GUID_SUM:
1721 (void) printf(gettext(" action: The pool cannot be "
1722 "imported. Attach the missing\n\tdevices and try "
1726 (void) printf(gettext(" action: The pool cannot be "
1727 "imported due to damaged devices or data.\n"));
1731 /* Print the comment attached to the pool. */
1732 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1733 (void) printf(gettext("comment: %s\n"), comment);
1736 * If the state is "closed" or "can't open", and the aux state
1737 * is "corrupt data":
1739 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1740 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1741 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1742 if (pool_state == POOL_STATE_DESTROYED)
1743 (void) printf(gettext("\tThe pool was destroyed, "
1744 "but can be imported using the '-Df' flags.\n"));
1745 else if (pool_state != POOL_STATE_EXPORTED)
1746 (void) printf(gettext("\tThe pool may be active on "
1747 "another system, but can be imported using\n\t"
1748 "the '-f' flag.\n"));
1752 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1755 (void) printf(gettext(" config:\n\n"));
1757 namewidth = max_width(NULL, nvroot, 0, 0);
1761 print_import_config(name, nvroot, namewidth, 0);
1762 if (num_logs(nvroot) > 0)
1763 print_logs(NULL, nvroot, namewidth, B_FALSE);
1765 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1766 (void) printf(gettext("\n\tAdditional devices are known to "
1767 "be part of this pool, though their\n\texact "
1768 "configuration cannot be determined.\n"));
1773 * Perform the import for the given configuration. This passes the heavy
1774 * lifting off to zpool_import_props(), and then mounts the datasets contained
1778 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1779 nvlist_t *props, int flags)
1781 zpool_handle_t *zhp;
1786 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1789 verify(nvlist_lookup_uint64(config,
1790 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1791 verify(nvlist_lookup_uint64(config,
1792 ZPOOL_CONFIG_VERSION, &version) == 0);
1793 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1794 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1795 "is formatted using an unsupported ZFS version\n"), name);
1797 } else if (state != POOL_STATE_EXPORTED &&
1798 !(flags & ZFS_IMPORT_ANY_HOST)) {
1801 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1803 if ((unsigned long)hostid != gethostid()) {
1808 verify(nvlist_lookup_string(config,
1809 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1810 verify(nvlist_lookup_uint64(config,
1811 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1813 (void) fprintf(stderr, gettext("cannot import "
1814 "'%s': pool may be in use from other "
1815 "system, it was last accessed by %s "
1816 "(hostid: 0x%lx) on %s"), name, hostname,
1817 (unsigned long)hostid,
1818 asctime(localtime(&t)));
1819 (void) fprintf(stderr, gettext("use '-f' to "
1820 "import anyway\n"));
1824 (void) fprintf(stderr, gettext("cannot import '%s': "
1825 "pool may be in use from other system\n"), name);
1826 (void) fprintf(stderr, gettext("use '-f' to import "
1832 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1835 if (newname != NULL)
1836 name = (char *)newname;
1838 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1841 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1842 !(flags & ZFS_IMPORT_ONLY) &&
1843 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1853 * zpool import [-d dir] [-D]
1854 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1855 * [-d dir | -c cachefile] [-f] -a
1856 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1857 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1859 * -c Read pool information from a cachefile instead of searching
1862 * -d Scan in a specific directory, other than /dev/dsk. More than
1863 * one directory can be specified using multiple '-d' options.
1865 * -D Scan for previously destroyed pools or import all or only
1866 * specified destroyed pools.
1868 * -R Temporarily import the pool, with all mountpoints relative to
1869 * the given root. The pool will remain exported when the machine
1872 * -V Import even in the presence of faulted vdevs. This is an
1873 * intentionally undocumented option for testing purposes, and
1874 * treats the pool configuration as complete, leaving any bad
1875 * vdevs in the FAULTED state. In other words, it does verbatim
1878 * -f Force import, even if it appears that the pool is active.
1880 * -F Attempt rewind if necessary.
1882 * -n See if rewind would work, but don't actually rewind.
1884 * -N Import the pool but don't mount datasets.
1886 * -T Specify a starting txg to use for import. This option is
1887 * intentionally undocumented option for testing purposes.
1889 * -a Import all pools found.
1891 * -o Set property=value and/or temporary mount options (without '=').
1893 * The import command scans for pools to import, and import pools based on pool
1894 * name and GUID. The pool can also be renamed as part of the import process.
1897 zpool_do_import(int argc, char **argv)
1899 char **searchdirs = NULL;
1903 nvlist_t *pools = NULL;
1904 boolean_t do_all = B_FALSE;
1905 boolean_t do_destroyed = B_FALSE;
1906 char *mntopts = NULL;
1909 uint64_t searchguid = 0;
1910 char *searchname = NULL;
1912 nvlist_t *found_config;
1913 nvlist_t *policy = NULL;
1914 nvlist_t *props = NULL;
1916 int flags = ZFS_IMPORT_NORMAL;
1917 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1918 boolean_t dryrun = B_FALSE;
1919 boolean_t do_rewind = B_FALSE;
1920 boolean_t xtreme_rewind = B_FALSE;
1921 uint64_t pool_state, txg = -1ULL;
1922 char *cachefile = NULL;
1923 importargs_t idata = { 0 };
1927 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1936 if (searchdirs == NULL) {
1937 searchdirs = safe_malloc(sizeof (char *));
1939 char **tmp = safe_malloc((nsearch + 1) *
1941 bcopy(searchdirs, tmp, nsearch *
1946 searchdirs[nsearch++] = optarg;
1949 do_destroyed = B_TRUE;
1952 flags |= ZFS_IMPORT_ANY_HOST;
1958 flags |= ZFS_IMPORT_MISSING_LOG;
1964 flags |= ZFS_IMPORT_ONLY;
1967 if ((propval = strchr(optarg, '=')) != NULL) {
1970 if (add_prop_list(optarg, propval,
1978 if (add_prop_list(zpool_prop_to_name(
1979 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1981 if (nvlist_lookup_string(props,
1982 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1985 if (add_prop_list(zpool_prop_to_name(
1986 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1991 txg = strtoull(optarg, &endptr, 10);
1992 if (errno != 0 || *endptr != '\0') {
1993 (void) fprintf(stderr,
1994 gettext("invalid txg value\n"));
1997 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2000 flags |= ZFS_IMPORT_VERBATIM;
2003 xtreme_rewind = B_TRUE;
2006 (void) fprintf(stderr, gettext("missing argument for "
2007 "'%c' option\n"), optopt);
2011 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2020 if (cachefile && nsearch != 0) {
2021 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2025 if ((dryrun || xtreme_rewind) && !do_rewind) {
2026 (void) fprintf(stderr,
2027 gettext("-n or -X only meaningful with -F\n"));
2031 rewind_policy = ZPOOL_TRY_REWIND;
2033 rewind_policy = ZPOOL_DO_REWIND;
2035 rewind_policy |= ZPOOL_EXTREME_REWIND;
2037 /* In the future, we can capture further policy and include it here */
2038 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2039 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2040 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2043 if (searchdirs == NULL) {
2044 searchdirs = safe_malloc(sizeof (char *));
2045 searchdirs[0] = "/dev";
2049 /* check argument count */
2052 (void) fprintf(stderr, gettext("too many arguments\n"));
2057 (void) fprintf(stderr, gettext("too many arguments\n"));
2062 * Check for the SYS_CONFIG privilege. We do this explicitly
2063 * here because otherwise any attempt to discover pools will
2066 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2067 (void) fprintf(stderr, gettext("cannot "
2068 "discover pools: permission denied\n"));
2070 nvlist_free(policy);
2076 * Depending on the arguments given, we do one of the following:
2078 * <none> Iterate through all pools and display information about
2081 * -a Iterate through all pools and try to import each one.
2083 * <id> Find the pool that corresponds to the given GUID/pool
2084 * name and import that one.
2086 * -D Above options applies only to destroyed pools.
2092 searchguid = strtoull(argv[0], &endptr, 10);
2093 if (errno != 0 || *endptr != '\0')
2094 searchname = argv[0];
2095 found_config = NULL;
2098 * User specified a name or guid. Ensure it's unique.
2100 idata.unique = B_TRUE;
2104 idata.path = searchdirs;
2105 idata.paths = nsearch;
2106 idata.poolname = searchname;
2107 idata.guid = searchguid;
2108 idata.cachefile = cachefile;
2110 pools = zpool_search_import(g_zfs, &idata);
2112 if (pools != NULL && idata.exists &&
2113 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2114 (void) fprintf(stderr, gettext("cannot import '%s': "
2115 "a pool with that name already exists\n"),
2117 (void) fprintf(stderr, gettext("use the form '%s "
2118 "<pool | id> <newpool>' to give it a new name\n"),
2121 } else if (pools == NULL && idata.exists) {
2122 (void) fprintf(stderr, gettext("cannot import '%s': "
2123 "a pool with that name is already created/imported,\n"),
2125 (void) fprintf(stderr, gettext("and no additional pools "
2126 "with that name were found\n"));
2128 } else if (pools == NULL) {
2130 (void) fprintf(stderr, gettext("cannot import '%s': "
2131 "no such pool available\n"), argv[0]);
2138 nvlist_free(policy);
2143 * At this point we have a list of import candidate configs. Even if
2144 * we were searching by pool name or guid, we still need to
2145 * post-process the list to deal with pool state and possible
2151 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2153 verify(nvpair_value_nvlist(elem, &config) == 0);
2155 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2157 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2159 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2162 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2169 (void) printf("\n");
2172 err |= do_import(config, NULL, mntopts,
2175 show_import(config);
2177 } else if (searchname != NULL) {
2181 * We are searching for a pool based on name.
2183 verify(nvlist_lookup_string(config,
2184 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2186 if (strcmp(name, searchname) == 0) {
2187 if (found_config != NULL) {
2188 (void) fprintf(stderr, gettext(
2189 "cannot import '%s': more than "
2190 "one matching pool\n"), searchname);
2191 (void) fprintf(stderr, gettext(
2192 "import by numeric ID instead\n"));
2195 found_config = config;
2201 * Search for a pool by guid.
2203 verify(nvlist_lookup_uint64(config,
2204 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2206 if (guid == searchguid)
2207 found_config = config;
2212 * If we were searching for a specific pool, verify that we found a
2213 * pool, and then do the import.
2215 if (argc != 0 && err == 0) {
2216 if (found_config == NULL) {
2217 (void) fprintf(stderr, gettext("cannot import '%s': "
2218 "no such pool available\n"), argv[0]);
2221 err |= do_import(found_config, argc == 1 ? NULL :
2222 argv[1], mntopts, props, flags);
2227 * If we were just looking for pools, report an error if none were
2230 if (argc == 0 && first)
2231 (void) fprintf(stderr,
2232 gettext("no pools available to import\n"));
2237 nvlist_free(policy);
2240 return (err ? 1 : 0);
2243 typedef struct iostat_cbdata {
2244 boolean_t cb_verbose;
2247 zpool_list_t *cb_list;
2251 print_iostat_separator(iostat_cbdata_t *cb)
2255 for (i = 0; i < cb->cb_namewidth; i++)
2257 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2261 print_iostat_header(iostat_cbdata_t *cb)
2263 (void) printf("%*s capacity operations bandwidth\n",
2264 cb->cb_namewidth, "");
2265 (void) printf("%-*s alloc free read write read write\n",
2266 cb->cb_namewidth, "pool");
2267 print_iostat_separator(cb);
2271 * Display a single statistic.
2274 print_one_stat(uint64_t value)
2278 zfs_nicenum(value, buf, sizeof (buf));
2279 (void) printf(" %5s", buf);
2283 * Print out all the statistics for the given vdev. This can either be the
2284 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2285 * is a verbose output, and we don't want to display the toplevel pool stats.
2288 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2289 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2291 nvlist_t **oldchild, **newchild;
2293 vdev_stat_t *oldvs, *newvs;
2294 vdev_stat_t zerovs = { 0 };
2299 if (oldnv != NULL) {
2300 verify(nvlist_lookup_uint64_array(oldnv,
2301 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2306 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2307 (uint64_t **)&newvs, &c) == 0);
2309 if (strlen(name) + depth > cb->cb_namewidth)
2310 (void) printf("%*s%s", depth, "", name);
2312 (void) printf("%*s%s%*s", depth, "", name,
2313 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2315 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2320 scale = (double)NANOSEC / tdelta;
2322 /* only toplevel vdevs have capacity stats */
2323 if (newvs->vs_space == 0) {
2324 (void) printf(" - -");
2326 print_one_stat(newvs->vs_alloc);
2327 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2330 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2331 oldvs->vs_ops[ZIO_TYPE_READ])));
2333 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2334 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2336 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2337 oldvs->vs_bytes[ZIO_TYPE_READ])));
2339 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2340 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2342 (void) printf("\n");
2344 if (!cb->cb_verbose)
2347 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2348 &newchild, &children) != 0)
2351 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2352 &oldchild, &c) != 0)
2355 for (c = 0; c < children; c++) {
2356 uint64_t ishole = B_FALSE, islog = B_FALSE;
2358 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2361 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2364 if (ishole || islog)
2367 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2368 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2369 newchild[c], cb, depth + 2);
2374 * Log device section
2377 if (num_logs(newnv) > 0) {
2378 (void) printf("%-*s - - - - - "
2379 "-\n", cb->cb_namewidth, "logs");
2381 for (c = 0; c < children; c++) {
2382 uint64_t islog = B_FALSE;
2383 (void) nvlist_lookup_uint64(newchild[c],
2384 ZPOOL_CONFIG_IS_LOG, &islog);
2387 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2389 print_vdev_stats(zhp, vname, oldnv ?
2390 oldchild[c] : NULL, newchild[c],
2399 * Include level 2 ARC devices in iostat output
2401 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2402 &newchild, &children) != 0)
2405 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2406 &oldchild, &c) != 0)
2410 (void) printf("%-*s - - - - - "
2411 "-\n", cb->cb_namewidth, "cache");
2412 for (c = 0; c < children; c++) {
2413 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2415 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2416 newchild[c], cb, depth + 2);
2423 refresh_iostat(zpool_handle_t *zhp, void *data)
2425 iostat_cbdata_t *cb = data;
2429 * If the pool has disappeared, remove it from the list and continue.
2431 if (zpool_refresh_stats(zhp, &missing) != 0)
2435 pool_list_remove(cb->cb_list, zhp);
2441 * Callback to print out the iostats for the given pool.
2444 print_iostat(zpool_handle_t *zhp, void *data)
2446 iostat_cbdata_t *cb = data;
2447 nvlist_t *oldconfig, *newconfig;
2448 nvlist_t *oldnvroot, *newnvroot;
2450 newconfig = zpool_get_config(zhp, &oldconfig);
2452 if (cb->cb_iteration == 1)
2455 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2458 if (oldconfig == NULL)
2461 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2465 * Print out the statistics for the pool.
2467 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2470 print_iostat_separator(cb);
2476 get_namewidth(zpool_handle_t *zhp, void *data)
2478 iostat_cbdata_t *cb = data;
2479 nvlist_t *config, *nvroot;
2481 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2482 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2484 if (!cb->cb_verbose)
2485 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2487 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2492 * The width must fall into the range [10,38]. The upper limit is the
2493 * maximum we can have and still fit in 80 columns.
2495 if (cb->cb_namewidth < 10)
2496 cb->cb_namewidth = 10;
2497 if (cb->cb_namewidth > 38)
2498 cb->cb_namewidth = 38;
2504 * Parse the input string, get the 'interval' and 'count' value if there is one.
2507 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2510 unsigned long interval = 0, count = 0;
2511 int argc = *argcp, errno;
2514 * Determine if the last argument is an integer or a pool name
2516 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2520 interval = strtoul(argv[argc - 1], &end, 10);
2522 if (*end == '\0' && errno == 0) {
2523 if (interval == 0) {
2524 (void) fprintf(stderr, gettext("interval "
2525 "cannot be zero\n"));
2529 * Ignore the last parameter
2534 * If this is not a valid number, just plow on. The
2535 * user will get a more informative error message later
2543 * If the last argument is also an integer, then we have both a count
2546 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2551 interval = strtoul(argv[argc - 1], &end, 10);
2553 if (*end == '\0' && errno == 0) {
2554 if (interval == 0) {
2555 (void) fprintf(stderr, gettext("interval "
2556 "cannot be zero\n"));
2561 * Ignore the last parameter
2575 get_timestamp_arg(char c)
2578 timestamp_fmt = UDATE;
2580 timestamp_fmt = DDATE;
2586 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2588 * -v Display statistics for individual vdevs
2589 * -T Display a timestamp in date(1) or Unix format
2591 * This command can be tricky because we want to be able to deal with pool
2592 * creation/destruction as well as vdev configuration changes. The bulk of this
2593 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2594 * on pool_list_update() to detect the addition of new pools. Configuration
2595 * changes are all handled within libzfs.
2598 zpool_do_iostat(int argc, char **argv)
2603 unsigned long interval = 0, count = 0;
2605 boolean_t verbose = B_FALSE;
2609 while ((c = getopt(argc, argv, "T:v")) != -1) {
2612 get_timestamp_arg(*optarg);
2618 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2627 get_interval_count(&argc, argv, &interval, &count);
2630 * Construct the list of all interesting pools.
2633 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2636 if (pool_list_count(list) == 0 && argc != 0) {
2637 pool_list_free(list);
2641 if (pool_list_count(list) == 0 && interval == 0) {
2642 pool_list_free(list);
2643 (void) fprintf(stderr, gettext("no pools available\n"));
2648 * Enter the main iostat loop.
2651 cb.cb_verbose = verbose;
2652 cb.cb_iteration = 0;
2653 cb.cb_namewidth = 0;
2656 pool_list_update(list);
2658 if ((npools = pool_list_count(list)) == 0)
2662 * Refresh all statistics. This is done as an explicit step
2663 * before calculating the maximum name width, so that any
2664 * configuration changes are properly accounted for.
2666 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2669 * Iterate over all pools to determine the maximum width
2670 * for the pool / device name column across all pools.
2672 cb.cb_namewidth = 0;
2673 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2675 if (timestamp_fmt != NODATE)
2676 print_timestamp(timestamp_fmt);
2679 * If it's the first time, or verbose mode, print the header.
2681 if (++cb.cb_iteration == 1 || verbose)
2682 print_iostat_header(&cb);
2684 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2687 * If there's more than one pool, and we're not in verbose mode
2688 * (which prints a separator for us), then print a separator.
2690 if (npools > 1 && !verbose)
2691 print_iostat_separator(&cb);
2694 (void) printf("\n");
2697 * Flush the output so that redirection to a file isn't buffered
2700 (void) fflush(stdout);
2705 if (count != 0 && --count == 0)
2708 (void) sleep(interval);
2711 pool_list_free(list);
2716 typedef struct list_cbdata {
2717 boolean_t cb_verbose;
2719 boolean_t cb_scripted;
2720 zprop_list_t *cb_proplist;
2724 * Given a list of columns to display, output appropriate headers for each one.
2727 print_header(list_cbdata_t *cb)
2729 zprop_list_t *pl = cb->cb_proplist;
2730 char headerbuf[ZPOOL_MAXPROPLEN];
2732 boolean_t first = B_TRUE;
2733 boolean_t right_justify;
2736 for (; pl != NULL; pl = pl->pl_next) {
2737 width = pl->pl_width;
2738 if (first && cb->cb_verbose) {
2740 * Reset the width to accommodate the verbose listing
2743 width = cb->cb_namewidth;
2751 right_justify = B_FALSE;
2752 if (pl->pl_prop != ZPROP_INVAL) {
2753 header = zpool_prop_column_name(pl->pl_prop);
2754 right_justify = zpool_prop_align_right(pl->pl_prop);
2758 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2759 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2760 headerbuf[i] = '\0';
2764 if (pl->pl_next == NULL && !right_justify)
2765 (void) printf("%s", header);
2766 else if (right_justify)
2767 (void) printf("%*s", width, header);
2769 (void) printf("%-*s", width, header);
2773 (void) printf("\n");
2777 * Given a pool and a list of properties, print out all the properties according
2778 * to the described layout.
2781 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2783 zprop_list_t *pl = cb->cb_proplist;
2784 boolean_t first = B_TRUE;
2785 char property[ZPOOL_MAXPROPLEN];
2787 boolean_t right_justify;
2790 for (; pl != NULL; pl = pl->pl_next) {
2792 width = pl->pl_width;
2793 if (first && cb->cb_verbose) {
2795 * Reset the width to accommodate the verbose listing
2798 width = cb->cb_namewidth;
2802 if (cb->cb_scripted)
2803 (void) printf("\t");
2810 right_justify = B_FALSE;
2811 if (pl->pl_prop != ZPROP_INVAL) {
2812 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2813 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2815 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2816 sizeof (property), NULL) != 0)
2821 right_justify = zpool_prop_align_right(pl->pl_prop);
2822 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2823 zpool_prop_unsupported(pl->pl_user_prop)) &&
2824 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2825 sizeof (property)) == 0) {
2833 * If this is being called in scripted mode, or if this is the
2834 * last column and it is left-justified, don't include a width
2837 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2838 (void) printf("%s", propstr);
2839 else if (right_justify)
2840 (void) printf("%*s", width, propstr);
2842 (void) printf("%-*s", width, propstr);
2845 (void) printf("\n");
2849 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2853 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2855 zfs_nicenum(value, propval, sizeof (propval));
2857 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2858 (void) strlcpy(propval, "-", sizeof (propval));
2861 (void) printf("\t%s", propval);
2863 (void) printf(" %*s", width, propval);
2867 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2868 list_cbdata_t *cb, int depth)
2874 boolean_t scripted = cb->cb_scripted;
2876 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2877 (uint64_t **)&vs, &c) == 0);
2881 (void) printf("\t%s", name);
2882 else if (strlen(name) + depth > cb->cb_namewidth)
2883 (void) printf("%*s%s", depth, "", name);
2885 (void) printf("%*s%s%*s", depth, "", name,
2886 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2888 /* only toplevel vdevs have capacity stats */
2889 if (vs->vs_space == 0) {
2891 (void) printf("\t-\t-\t-");
2893 (void) printf(" - - -");
2895 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2897 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2899 print_one_column(ZPOOL_PROP_FREE,
2900 vs->vs_space - vs->vs_alloc, scripted);
2902 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2904 (void) printf("\n");
2907 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2908 &child, &children) != 0)
2911 for (c = 0; c < children; c++) {
2912 uint64_t ishole = B_FALSE;
2914 if (nvlist_lookup_uint64(child[c],
2915 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2918 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2919 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2924 * Include level 2 ARC devices in iostat output
2926 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2927 &child, &children) != 0)
2931 (void) printf("%-*s - - - - - "
2932 "-\n", cb->cb_namewidth, "cache");
2933 for (c = 0; c < children; c++) {
2934 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2936 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2944 * Generic callback function to list a pool.
2947 list_callback(zpool_handle_t *zhp, void *data)
2949 list_cbdata_t *cbp = data;
2953 config = zpool_get_config(zhp, NULL);
2955 print_pool(zhp, cbp);
2956 if (!cbp->cb_verbose)
2959 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2961 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2967 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2969 * -H Scripted mode. Don't display headers, and separate properties
2971 * -o List of properties to display. Defaults to
2972 * "name,size,allocated,free,capacity,health,altroot"
2973 * -T Display a timestamp in date(1) or Unix format
2975 * List all pools in the system, whether or not they're healthy. Output space
2976 * statistics for each one, as well as health status summary.
2979 zpool_do_list(int argc, char **argv)
2983 list_cbdata_t cb = { 0 };
2984 static char default_props[] =
2985 "name,size,allocated,free,capacity,dedupratio,"
2987 char *props = default_props;
2988 unsigned long interval = 0, count = 0;
2990 boolean_t first = B_TRUE;
2993 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2996 cb.cb_scripted = B_TRUE;
3002 get_timestamp_arg(*optarg);
3005 cb.cb_verbose = B_TRUE;
3008 (void) fprintf(stderr, gettext("missing argument for "
3009 "'%c' option\n"), optopt);
3013 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3022 get_interval_count(&argc, argv, &interval, &count);
3024 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3027 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3030 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3031 (void) printf(gettext("no pools available\n"));
3032 zprop_free_list(cb.cb_proplist);
3037 pool_list_update(list);
3039 if (pool_list_count(list) == 0)
3042 cb.cb_namewidth = 0;
3043 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3045 if (timestamp_fmt != NODATE)
3046 print_timestamp(timestamp_fmt);
3048 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3052 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3057 if (count != 0 && --count == 0)
3060 (void) sleep(interval);
3063 zprop_free_list(cb.cb_proplist);
3068 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3075 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3076 &child, &children) != 0) {
3077 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3078 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3079 name += sizeof(_PATH_DEV) - 1;
3080 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3081 path += sizeof(_PATH_DEV) - 1;
3082 if (strcmp(name, path) == 0)
3087 for (c = 0; c < children; c++)
3088 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3095 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3097 boolean_t force = B_FALSE;
3100 char *poolname, *old_disk, *new_disk;
3101 zpool_handle_t *zhp;
3105 while ((c = getopt(argc, argv, "f")) != -1) {
3111 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3120 /* get pool name and check number of arguments */
3122 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3129 (void) fprintf(stderr,
3130 gettext("missing <device> specification\n"));
3138 (void) fprintf(stderr,
3139 gettext("missing <new_device> specification\n"));
3142 new_disk = old_disk;
3152 (void) fprintf(stderr, gettext("too many arguments\n"));
3156 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3159 if (zpool_get_config(zhp, NULL) == NULL) {
3160 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3166 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3168 if (nvroot == NULL) {
3173 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3175 nvlist_free(nvroot);
3182 * zpool replace [-f] <pool> <device> <new_device>
3184 * -f Force attach, even if <new_device> appears to be in use.
3186 * Replace <device> with <new_device>.
3190 zpool_do_replace(int argc, char **argv)
3192 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3196 * zpool attach [-f] <pool> <device> <new_device>
3198 * -f Force attach, even if <new_device> appears to be in use.
3200 * Attach <new_device> to the mirror containing <device>. If <device> is not
3201 * part of a mirror, then <device> will be transformed into a mirror of
3202 * <device> and <new_device>. In either case, <new_device> will begin life
3203 * with a DTL of [0, now], and will immediately begin to resilver itself.
3206 zpool_do_attach(int argc, char **argv)
3208 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3212 * zpool detach [-f] <pool> <device>
3214 * -f Force detach of <device>, even if DTLs argue against it
3215 * (not supported yet)
3217 * Detach a device from a mirror. The operation will be refused if <device>
3218 * is the last device in the mirror, or if the DTLs indicate that this device
3219 * has the only valid copy of some data.
3223 zpool_do_detach(int argc, char **argv)
3226 char *poolname, *path;
3227 zpool_handle_t *zhp;
3231 while ((c = getopt(argc, argv, "f")) != -1) {
3235 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3244 /* get pool name and check number of arguments */
3246 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3251 (void) fprintf(stderr,
3252 gettext("missing <device> specification\n"));
3259 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3262 ret = zpool_vdev_detach(zhp, path);
3270 * zpool split [-n] [-o prop=val] ...
3272 * [-R altroot] <pool> <newpool> [<device> ...]
3274 * -n Do not split the pool, but display the resulting layout if
3275 * it were to be split.
3276 * -o Set property=value, or set mount options.
3277 * -R Mount the split-off pool under an alternate root.
3279 * Splits the named pool and gives it the new pool name. Devices to be split
3280 * off may be listed, provided that no more than one device is specified
3281 * per top-level vdev mirror. The newly split pool is left in an exported
3282 * state unless -R is specified.
3284 * Restrictions: the top-level of the pool pool must only be made up of
3285 * mirrors; all devices in the pool must be healthy; no device may be
3286 * undergoing a resilvering operation.
3289 zpool_do_split(int argc, char **argv)
3291 char *srcpool, *newpool, *propval;
3292 char *mntopts = NULL;
3295 zpool_handle_t *zhp;
3296 nvlist_t *config, *props = NULL;
3298 flags.dryrun = B_FALSE;
3299 flags.import = B_FALSE;
3302 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3305 flags.import = B_TRUE;
3307 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3308 &props, B_TRUE) != 0) {
3315 flags.dryrun = B_TRUE;
3318 if ((propval = strchr(optarg, '=')) != NULL) {
3321 if (add_prop_list(optarg, propval,
3322 &props, B_TRUE) != 0) {
3332 (void) fprintf(stderr, gettext("missing argument for "
3333 "'%c' option\n"), optopt);
3337 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3344 if (!flags.import && mntopts != NULL) {
3345 (void) fprintf(stderr, gettext("setting mntopts is only "
3346 "valid when importing the pool\n"));
3354 (void) fprintf(stderr, gettext("Missing pool name\n"));
3358 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3368 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3371 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3372 if (config == NULL) {
3376 (void) printf(gettext("would create '%s' with the "
3377 "following layout:\n\n"), newpool);
3378 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3380 nvlist_free(config);
3385 if (ret != 0 || flags.dryrun || !flags.import)
3389 * The split was successful. Now we need to open the new
3390 * pool and import it.
3392 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3394 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3395 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3397 (void) fprintf(stderr, gettext("Split was successful, but "
3398 "the datasets could not all be mounted\n"));
3399 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3400 "different altroot\n"), "zpool import");
3410 * zpool online <pool> <device> ...
3413 zpool_do_online(int argc, char **argv)
3417 zpool_handle_t *zhp;
3419 vdev_state_t newstate;
3423 while ((c = getopt(argc, argv, "et")) != -1) {
3426 flags |= ZFS_ONLINE_EXPAND;
3430 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3439 /* get pool name and check number of arguments */
3441 (void) fprintf(stderr, gettext("missing pool name\n"));
3445 (void) fprintf(stderr, gettext("missing device name\n"));
3451 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3454 for (i = 1; i < argc; i++) {
3455 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3456 if (newstate != VDEV_STATE_HEALTHY) {
3457 (void) printf(gettext("warning: device '%s' "
3458 "onlined, but remains in faulted state\n"),
3460 if (newstate == VDEV_STATE_FAULTED)
3461 (void) printf(gettext("use 'zpool "
3462 "clear' to restore a faulted "
3465 (void) printf(gettext("use 'zpool "
3466 "replace' to replace devices "
3467 "that are no longer present\n"));
3480 * zpool offline [-ft] <pool> <device> ...
3482 * -f Force the device into the offline state, even if doing
3483 * so would appear to compromise pool availability.
3484 * (not supported yet)
3486 * -t Only take the device off-line temporarily. The offline
3487 * state will not be persistent across reboots.
3491 zpool_do_offline(int argc, char **argv)
3495 zpool_handle_t *zhp;
3497 boolean_t istmp = B_FALSE;
3500 while ((c = getopt(argc, argv, "ft")) != -1) {
3507 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3516 /* get pool name and check number of arguments */
3518 (void) fprintf(stderr, gettext("missing pool name\n"));
3522 (void) fprintf(stderr, gettext("missing device name\n"));
3528 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3531 for (i = 1; i < argc; i++) {
3532 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3542 * zpool clear <pool> [device]
3544 * Clear all errors associated with a pool or a particular device.
3547 zpool_do_clear(int argc, char **argv)
3551 boolean_t dryrun = B_FALSE;
3552 boolean_t do_rewind = B_FALSE;
3553 boolean_t xtreme_rewind = B_FALSE;
3554 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3555 nvlist_t *policy = NULL;
3556 zpool_handle_t *zhp;
3557 char *pool, *device;
3560 while ((c = getopt(argc, argv, "FnX")) != -1) {
3569 xtreme_rewind = B_TRUE;
3572 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3582 (void) fprintf(stderr, gettext("missing pool name\n"));
3587 (void) fprintf(stderr, gettext("too many arguments\n"));
3591 if ((dryrun || xtreme_rewind) && !do_rewind) {
3592 (void) fprintf(stderr,
3593 gettext("-n or -X only meaningful with -F\n"));
3597 rewind_policy = ZPOOL_TRY_REWIND;
3599 rewind_policy = ZPOOL_DO_REWIND;
3601 rewind_policy |= ZPOOL_EXTREME_REWIND;
3603 /* In future, further rewind policy choices can be passed along here */
3604 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3605 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3609 device = argc == 2 ? argv[1] : NULL;
3611 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3612 nvlist_free(policy);
3616 if (zpool_clear(zhp, device, policy) != 0)
3621 nvlist_free(policy);
3627 * zpool reguid <pool>
3630 zpool_do_reguid(int argc, char **argv)
3634 zpool_handle_t *zhp;
3638 while ((c = getopt(argc, argv, "")) != -1) {
3641 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3650 /* get pool name and check number of arguments */
3652 (void) fprintf(stderr, gettext("missing pool name\n"));
3657 (void) fprintf(stderr, gettext("too many arguments\n"));
3662 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3665 ret = zpool_reguid(zhp);
3673 * zpool reopen <pool>
3675 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3677 * NOTE: This command is currently undocumented. If the command is ever
3678 * exposed then the appropriate usage() messages will need to be made.
3681 zpool_do_reopen(int argc, char **argv)
3684 zpool_handle_t *zhp;
3694 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3697 ret = zpool_reopen(zhp);
3702 typedef struct scrub_cbdata {
3709 scrub_callback(zpool_handle_t *zhp, void *data)
3711 scrub_cbdata_t *cb = data;
3715 * Ignore faulted pools.
3717 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3718 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3719 "currently unavailable\n"), zpool_get_name(zhp));
3723 err = zpool_scan(zhp, cb->cb_type);
3729 * zpool scrub [-s] <pool> ...
3731 * -s Stop. Stops any in-progress scrub.
3734 zpool_do_scrub(int argc, char **argv)
3739 cb.cb_type = POOL_SCAN_SCRUB;
3742 while ((c = getopt(argc, argv, "s")) != -1) {
3745 cb.cb_type = POOL_SCAN_NONE;
3748 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3760 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3764 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3767 typedef struct status_cbdata {
3769 boolean_t cb_allpools;
3770 boolean_t cb_verbose;
3771 boolean_t cb_explain;
3773 boolean_t cb_dedup_stats;
3777 * Print out detailed scrub status.
3780 print_scan_status(pool_scan_stat_t *ps)
3783 uint64_t elapsed, mins_left, hours_left;
3784 uint64_t pass_exam, examined, total;
3786 double fraction_done;
3787 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3789 (void) printf(gettext(" scan: "));
3791 /* If there's never been a scan, there's not much to say. */
3792 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3793 ps->pss_func >= POOL_SCAN_FUNCS) {
3794 (void) printf(gettext("none requested\n"));
3798 start = ps->pss_start_time;
3799 end = ps->pss_end_time;
3800 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3802 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3803 ps->pss_func == POOL_SCAN_RESILVER);
3805 * Scan is finished or canceled.
3807 if (ps->pss_state == DSS_FINISHED) {
3808 uint64_t minutes_taken = (end - start) / 60;
3811 if (ps->pss_func == POOL_SCAN_SCRUB) {
3812 fmt = gettext("scrub repaired %s in %lluh%um with "
3813 "%llu errors on %s");
3814 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3815 fmt = gettext("resilvered %s in %lluh%um with "
3816 "%llu errors on %s");
3819 (void) printf(fmt, processed_buf,
3820 (u_longlong_t)(minutes_taken / 60),
3821 (uint_t)(minutes_taken % 60),
3822 (u_longlong_t)ps->pss_errors,
3823 ctime((time_t *)&end));
3825 } else if (ps->pss_state == DSS_CANCELED) {
3826 if (ps->pss_func == POOL_SCAN_SCRUB) {
3827 (void) printf(gettext("scrub canceled on %s"),
3829 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3830 (void) printf(gettext("resilver canceled on %s"),
3836 assert(ps->pss_state == DSS_SCANNING);
3839 * Scan is in progress.
3841 if (ps->pss_func == POOL_SCAN_SCRUB) {
3842 (void) printf(gettext("scrub in progress since %s"),
3844 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3845 (void) printf(gettext("resilver in progress since %s"),
3849 examined = ps->pss_examined ? ps->pss_examined : 1;
3850 total = ps->pss_to_examine;
3851 fraction_done = (double)examined / total;
3853 /* elapsed time for this pass */
3854 elapsed = time(NULL) - ps->pss_pass_start;
3855 elapsed = elapsed ? elapsed : 1;
3856 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3857 rate = pass_exam / elapsed;
3858 rate = rate ? rate : 1;
3859 mins_left = ((total - examined) / rate) / 60;
3860 hours_left = mins_left / 60;
3862 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3863 zfs_nicenum(total, total_buf, sizeof (total_buf));
3864 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3867 * do not print estimated time if hours_left is more than 30 days
3869 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3870 examined_buf, total_buf, rate_buf);
3871 if (hours_left < (30 * 24)) {
3872 (void) printf(gettext(", %lluh%um to go\n"),
3873 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3875 (void) printf(gettext(
3876 ", (scan is slow, no estimated time)\n"));
3879 if (ps->pss_func == POOL_SCAN_RESILVER) {
3880 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3881 processed_buf, 100 * fraction_done);
3882 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3883 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3884 processed_buf, 100 * fraction_done);
3889 print_error_log(zpool_handle_t *zhp)
3891 nvlist_t *nverrlist = NULL;
3894 size_t len = MAXPATHLEN * 2;
3896 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3897 (void) printf("errors: List of errors unavailable "
3898 "(insufficient privileges)\n");
3902 (void) printf("errors: Permanent errors have been "
3903 "detected in the following files:\n\n");
3905 pathname = safe_malloc(len);
3907 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3909 uint64_t dsobj, obj;
3911 verify(nvpair_value_nvlist(elem, &nv) == 0);
3912 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3914 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3916 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3917 (void) printf("%7s %s\n", "", pathname);
3920 nvlist_free(nverrlist);
3924 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3933 (void) printf(gettext("\tspares\n"));
3935 for (i = 0; i < nspares; i++) {
3936 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3937 print_status_config(zhp, name, spares[i],
3938 namewidth, 2, B_TRUE);
3944 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3953 (void) printf(gettext("\tcache\n"));
3955 for (i = 0; i < nl2cache; i++) {
3956 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3957 print_status_config(zhp, name, l2cache[i],
3958 namewidth, 2, B_FALSE);
3964 print_dedup_stats(nvlist_t *config)
3966 ddt_histogram_t *ddh;
3972 * If the pool was faulted then we may not have been able to
3973 * obtain the config. Otherwise, if have anything in the dedup
3974 * table continue processing the stats.
3976 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3977 (uint64_t **)&ddo, &c) != 0)
3980 (void) printf("\n");
3981 (void) printf(gettext(" dedup: "));
3982 if (ddo->ddo_count == 0) {
3983 (void) printf(gettext("no DDT entries\n"));
3987 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3988 (u_longlong_t)ddo->ddo_count,
3989 (u_longlong_t)ddo->ddo_dspace,
3990 (u_longlong_t)ddo->ddo_mspace);
3992 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3993 (uint64_t **)&dds, &c) == 0);
3994 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3995 (uint64_t **)&ddh, &c) == 0);
3996 zpool_dump_ddt(dds, ddh);
4000 * Display a summary of pool status. Displays a summary such as:
4004 * reason: One or more devices ...
4005 * see: http://illumos.org/msg/ZFS-xxxx-01
4011 * When given the '-v' option, we print out the complete config. If the '-e'
4012 * option is specified, then we print out error rate information as well.
4015 status_callback(zpool_handle_t *zhp, void *data)
4017 status_cbdata_t *cbp = data;
4018 nvlist_t *config, *nvroot;
4025 config = zpool_get_config(zhp, NULL);
4026 reason = zpool_get_status(zhp, &msgid);
4031 * If we were given 'zpool status -x', only report those pools with
4034 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
4035 if (!cbp->cb_allpools) {
4036 (void) printf(gettext("pool '%s' is healthy\n"),
4037 zpool_get_name(zhp));
4039 cbp->cb_first = B_FALSE;
4045 cbp->cb_first = B_FALSE;
4047 (void) printf("\n");
4049 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4051 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4052 (uint64_t **)&vs, &c) == 0);
4053 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4055 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4056 (void) printf(gettext(" state: %s\n"), health);
4059 case ZPOOL_STATUS_MISSING_DEV_R:
4060 (void) printf(gettext("status: One or more devices could not "
4061 "be opened. Sufficient replicas exist for\n\tthe pool to "
4062 "continue functioning in a degraded state.\n"));
4063 (void) printf(gettext("action: Attach the missing device and "
4064 "online it using 'zpool online'.\n"));
4067 case ZPOOL_STATUS_MISSING_DEV_NR:
4068 (void) printf(gettext("status: One or more devices could not "
4069 "be opened. There are insufficient\n\treplicas for the "
4070 "pool to continue functioning.\n"));
4071 (void) printf(gettext("action: Attach the missing device and "
4072 "online it using 'zpool online'.\n"));
4075 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4076 (void) printf(gettext("status: One or more devices could not "
4077 "be used because the label is missing or\n\tinvalid. "
4078 "Sufficient replicas exist for the pool to continue\n\t"
4079 "functioning in a degraded state.\n"));
4080 (void) printf(gettext("action: Replace the device using "
4081 "'zpool replace'.\n"));
4084 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4085 (void) printf(gettext("status: One or more devices could not "
4086 "be used because the label is missing \n\tor invalid. "
4087 "There are insufficient replicas for the pool to "
4088 "continue\n\tfunctioning.\n"));
4089 zpool_explain_recover(zpool_get_handle(zhp),
4090 zpool_get_name(zhp), reason, config);
4093 case ZPOOL_STATUS_FAILING_DEV:
4094 (void) printf(gettext("status: One or more devices has "
4095 "experienced an unrecoverable error. An\n\tattempt was "
4096 "made to correct the error. Applications are "
4098 (void) printf(gettext("action: Determine if the device needs "
4099 "to be replaced, and clear the errors\n\tusing "
4100 "'zpool clear' or replace the device with 'zpool "
4104 case ZPOOL_STATUS_OFFLINE_DEV:
4105 (void) printf(gettext("status: One or more devices has "
4106 "been taken offline by the administrator.\n\tSufficient "
4107 "replicas exist for the pool to continue functioning in "
4108 "a\n\tdegraded state.\n"));
4109 (void) printf(gettext("action: Online the device using "
4110 "'zpool online' or replace the device with\n\t'zpool "
4114 case ZPOOL_STATUS_REMOVED_DEV:
4115 (void) printf(gettext("status: One or more devices has "
4116 "been removed by the administrator.\n\tSufficient "
4117 "replicas exist for the pool to continue functioning in "
4118 "a\n\tdegraded state.\n"));
4119 (void) printf(gettext("action: Online the device using "
4120 "'zpool online' or replace the device with\n\t'zpool "
4124 case ZPOOL_STATUS_RESILVERING:
4125 (void) printf(gettext("status: One or more devices is "
4126 "currently being resilvered. The pool will\n\tcontinue "
4127 "to function, possibly in a degraded state.\n"));
4128 (void) printf(gettext("action: Wait for the resilver to "
4132 case ZPOOL_STATUS_CORRUPT_DATA:
4133 (void) printf(gettext("status: One or more devices has "
4134 "experienced an error resulting in data\n\tcorruption. "
4135 "Applications may be affected.\n"));
4136 (void) printf(gettext("action: Restore the file in question "
4137 "if possible. Otherwise restore the\n\tentire pool from "
4141 case ZPOOL_STATUS_CORRUPT_POOL:
4142 (void) printf(gettext("status: The pool metadata is corrupted "
4143 "and the pool cannot be opened.\n"));
4144 zpool_explain_recover(zpool_get_handle(zhp),
4145 zpool_get_name(zhp), reason, config);
4148 case ZPOOL_STATUS_VERSION_OLDER:
4149 (void) printf(gettext("status: The pool is formatted using a "
4150 "legacy on-disk format. The pool can\n\tstill be used, "
4151 "but some features are unavailable.\n"));
4152 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4153 "upgrade'. Once this is done, the\n\tpool will no longer "
4154 "be accessible on software that does not support feature\n"
4158 case ZPOOL_STATUS_VERSION_NEWER:
4159 (void) printf(gettext("status: The pool has been upgraded to a "
4160 "newer, incompatible on-disk version.\n\tThe pool cannot "
4161 "be accessed on this system.\n"));
4162 (void) printf(gettext("action: Access the pool from a system "
4163 "running more recent software, or\n\trestore the pool from "
4167 case ZPOOL_STATUS_FEAT_DISABLED:
4168 (void) printf(gettext("status: Some supported features are not "
4169 "enabled on the pool. The pool can\n\tstill be used, but "
4170 "some features are unavailable.\n"));
4171 (void) printf(gettext("action: Enable all features using "
4172 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4173 "longer be accessible by software that does not support\n\t"
4174 "the features. See zpool-features(7) for details.\n"));
4177 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4178 (void) printf(gettext("status: The pool cannot be accessed on "
4179 "this system because it uses the\n\tfollowing feature(s) "
4180 "not supported on this system:\n"));
4181 zpool_print_unsup_feat(config);
4182 (void) printf("\n");
4183 (void) printf(gettext("action: Access the pool from a system "
4184 "that supports the required feature(s),\n\tor restore the "
4185 "pool from backup.\n"));
4188 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4189 (void) printf(gettext("status: The pool can only be accessed "
4190 "in read-only mode on this system. It\n\tcannot be "
4191 "accessed in read-write mode because it uses the "
4192 "following\n\tfeature(s) not supported on this system:\n"));
4193 zpool_print_unsup_feat(config);
4194 (void) printf("\n");
4195 (void) printf(gettext("action: The pool cannot be accessed in "
4196 "read-write mode. Import the pool with\n"
4197 "\t\"-o readonly=on\", access the pool from a system that "
4198 "supports the\n\trequired feature(s), or restore the "
4199 "pool from backup.\n"));
4202 case ZPOOL_STATUS_FAULTED_DEV_R:
4203 (void) printf(gettext("status: One or more devices are "
4204 "faulted in response to persistent errors.\n\tSufficient "
4205 "replicas exist for the pool to continue functioning "
4206 "in a\n\tdegraded state.\n"));
4207 (void) printf(gettext("action: Replace the faulted device, "
4208 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4211 case ZPOOL_STATUS_FAULTED_DEV_NR:
4212 (void) printf(gettext("status: One or more devices are "
4213 "faulted in response to persistent errors. There are "
4214 "insufficient replicas for the pool to\n\tcontinue "
4216 (void) printf(gettext("action: Destroy and re-create the pool "
4217 "from a backup source. Manually marking the device\n"
4218 "\trepaired using 'zpool clear' may allow some data "
4219 "to be recovered.\n"));
4222 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4223 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4224 (void) printf(gettext("status: One or more devices are "
4225 "faulted in response to IO failures.\n"));
4226 (void) printf(gettext("action: Make sure the affected devices "
4227 "are connected, then run 'zpool clear'.\n"));
4230 case ZPOOL_STATUS_BAD_LOG:
4231 (void) printf(gettext("status: An intent log record "
4232 "could not be read.\n"
4233 "\tWaiting for adminstrator intervention to fix the "
4234 "faulted pool.\n"));
4235 (void) printf(gettext("action: Either restore the affected "
4236 "device(s) and run 'zpool online',\n"
4237 "\tor ignore the intent log records by running "
4238 "'zpool clear'.\n"));
4243 * The remaining errors can't actually be generated, yet.
4245 assert(reason == ZPOOL_STATUS_OK);
4249 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4252 if (config != NULL) {
4255 nvlist_t **spares, **l2cache;
4256 uint_t nspares, nl2cache;
4257 pool_scan_stat_t *ps = NULL;
4259 (void) nvlist_lookup_uint64_array(nvroot,
4260 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4261 print_scan_status(ps);
4263 namewidth = max_width(zhp, nvroot, 0, 0);
4267 (void) printf(gettext("config:\n\n"));
4268 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4269 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4270 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4271 namewidth, 0, B_FALSE);
4273 if (num_logs(nvroot) > 0)
4274 print_logs(zhp, nvroot, namewidth, B_TRUE);
4275 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4276 &l2cache, &nl2cache) == 0)
4277 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4279 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4280 &spares, &nspares) == 0)
4281 print_spares(zhp, spares, nspares, namewidth);
4283 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4285 nvlist_t *nverrlist = NULL;
4288 * If the approximate error count is small, get a
4289 * precise count by fetching the entire log and
4290 * uniquifying the results.
4292 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4293 zpool_get_errlog(zhp, &nverrlist) == 0) {
4298 while ((elem = nvlist_next_nvpair(nverrlist,
4303 nvlist_free(nverrlist);
4305 (void) printf("\n");
4308 (void) printf(gettext("errors: No known data "
4310 else if (!cbp->cb_verbose)
4311 (void) printf(gettext("errors: %llu data "
4312 "errors, use '-v' for a list\n"),
4313 (u_longlong_t)nerr);
4315 print_error_log(zhp);
4318 if (cbp->cb_dedup_stats)
4319 print_dedup_stats(config);
4321 (void) printf(gettext("config: The configuration cannot be "
4329 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4331 * -v Display complete error logs
4332 * -x Display only pools with potential problems
4333 * -D Display dedup status (undocumented)
4334 * -T Display a timestamp in date(1) or Unix format
4336 * Describes the health status of all pools or some subset.
4339 zpool_do_status(int argc, char **argv)
4343 unsigned long interval = 0, count = 0;
4344 status_cbdata_t cb = { 0 };
4347 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4350 cb.cb_verbose = B_TRUE;
4353 cb.cb_explain = B_TRUE;
4356 cb.cb_dedup_stats = B_TRUE;
4359 get_timestamp_arg(*optarg);
4362 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4371 get_interval_count(&argc, argv, &interval, &count);
4374 cb.cb_allpools = B_TRUE;
4376 cb.cb_first = B_TRUE;
4379 if (timestamp_fmt != NODATE)
4380 print_timestamp(timestamp_fmt);
4382 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4383 status_callback, &cb);
4385 if (argc == 0 && cb.cb_count == 0)
4386 (void) printf(gettext("no pools available\n"));
4387 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4388 (void) printf(gettext("all pools are healthy\n"));
4396 if (count != 0 && --count == 0)
4399 (void) sleep(interval);
4405 typedef struct upgrade_cbdata {
4407 char cb_poolname[ZPOOL_MAXNAMELEN];
4409 uint64_t cb_version;
4415 is_root_pool(zpool_handle_t *zhp)
4417 static struct statfs sfs;
4418 static char *poolname = NULL;
4419 static boolean_t stated = B_FALSE;
4424 if (statfs("/", &sfs) == -1) {
4425 (void) fprintf(stderr,
4426 "Unable to stat root file system: %s.\n",
4430 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4432 poolname = sfs.f_mntfromname;
4433 if ((slash = strchr(poolname, '/')) != NULL)
4436 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4440 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4442 if (poolname[0] == '\0' && is_root_pool(zhp))
4443 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4445 #endif /* FreeBSD */
4448 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4452 uint64_t oldversion;
4454 config = zpool_get_config(zhp, NULL);
4455 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4458 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4459 assert(oldversion < version);
4461 ret = zpool_upgrade(zhp, version);
4465 if (version >= SPA_VERSION_FEATURES) {
4466 (void) printf(gettext("Successfully upgraded "
4467 "'%s' from version %llu to feature flags.\n"),
4468 zpool_get_name(zhp), oldversion);
4470 (void) printf(gettext("Successfully upgraded "
4471 "'%s' from version %llu to version %llu.\n"),
4472 zpool_get_name(zhp), oldversion, version);
4479 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4482 boolean_t firstff = B_TRUE;
4483 nvlist_t *enabled = zpool_get_features(zhp);
4486 for (i = 0; i < SPA_FEATURES; i++) {
4487 const char *fname = spa_feature_table[i].fi_uname;
4488 const char *fguid = spa_feature_table[i].fi_guid;
4489 if (!nvlist_exists(enabled, fguid)) {
4491 verify(-1 != asprintf(&propname, "feature@%s", fname));
4492 ret = zpool_set_prop(zhp, propname,
4493 ZFS_FEATURE_ENABLED);
4501 (void) printf(gettext("Enabled the "
4502 "following features on '%s':\n"),
4503 zpool_get_name(zhp));
4506 (void) printf(gettext(" %s\n"), fname);
4517 upgrade_cb(zpool_handle_t *zhp, void *arg)
4519 upgrade_cbdata_t *cbp = arg;
4522 boolean_t printnl = B_FALSE;
4525 config = zpool_get_config(zhp, NULL);
4526 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4529 assert(SPA_VERSION_IS_SUPPORTED(version));
4531 if (version < cbp->cb_version) {
4532 cbp->cb_first = B_FALSE;
4533 ret = upgrade_version(zhp, cbp->cb_version);
4537 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4538 sizeof(cbp->cb_poolname));
4539 #endif /* ___FreeBSD__ */
4544 * If they did "zpool upgrade -a", then we could
4545 * be doing ioctls to different pools. We need
4546 * to log this history once to each pool, and bypass
4547 * the normal history logging that happens in main().
4549 (void) zpool_log_history(g_zfs, history_str);
4550 log_history = B_FALSE;
4554 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4556 ret = upgrade_enable_all(zhp, &count);
4561 cbp->cb_first = B_FALSE;
4567 (void) printf(gettext("\n"));
4574 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4576 upgrade_cbdata_t *cbp = arg;
4580 config = zpool_get_config(zhp, NULL);
4581 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4584 assert(SPA_VERSION_IS_SUPPORTED(version));
4586 if (version < SPA_VERSION_FEATURES) {
4587 if (cbp->cb_first) {
4588 (void) printf(gettext("The following pools are "
4589 "formatted with legacy version numbers and can\n"
4590 "be upgraded to use feature flags. After "
4591 "being upgraded, these pools\nwill no "
4592 "longer be accessible by software that does not "
4593 "support feature\nflags.\n\n"));
4594 (void) printf(gettext("VER POOL\n"));
4595 (void) printf(gettext("--- ------------\n"));
4596 cbp->cb_first = B_FALSE;
4599 (void) printf("%2llu %s\n", (u_longlong_t)version,
4600 zpool_get_name(zhp));
4607 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4609 upgrade_cbdata_t *cbp = arg;
4613 config = zpool_get_config(zhp, NULL);
4614 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4617 if (version >= SPA_VERSION_FEATURES) {
4619 boolean_t poolfirst = B_TRUE;
4620 nvlist_t *enabled = zpool_get_features(zhp);
4622 for (i = 0; i < SPA_FEATURES; i++) {
4623 const char *fguid = spa_feature_table[i].fi_guid;
4624 const char *fname = spa_feature_table[i].fi_uname;
4625 if (!nvlist_exists(enabled, fguid)) {
4626 if (cbp->cb_first) {
4627 (void) printf(gettext("\nSome "
4628 "supported features are not "
4629 "enabled on the following pools. "
4630 "Once a\nfeature is enabled the "
4631 "pool may become incompatible with "
4632 "software\nthat does not support "
4634 "zpool-features(7) for "
4636 (void) printf(gettext("POOL "
4638 (void) printf(gettext("------"
4640 cbp->cb_first = B_FALSE;
4644 (void) printf(gettext("%s\n"),
4645 zpool_get_name(zhp));
4646 poolfirst = B_FALSE;
4649 (void) printf(gettext(" %s\n"), fname);
4659 upgrade_one(zpool_handle_t *zhp, void *data)
4661 boolean_t printnl = B_FALSE;
4662 upgrade_cbdata_t *cbp = data;
4663 uint64_t cur_version;
4666 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4667 (void) printf(gettext("'log' is now a reserved word\n"
4668 "Pool 'log' must be renamed using export and import"
4673 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4674 if (cur_version > cbp->cb_version) {
4675 (void) printf(gettext("Pool '%s' is already formatted "
4676 "using more current version '%llu'.\n\n"),
4677 zpool_get_name(zhp), cur_version);
4681 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4682 (void) printf(gettext("Pool '%s' is already formatted "
4683 "using version %llu.\n\n"), zpool_get_name(zhp),
4688 if (cur_version != cbp->cb_version) {
4690 ret = upgrade_version(zhp, cbp->cb_version);
4694 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4695 sizeof(cbp->cb_poolname));
4696 #endif /* ___FreeBSD__ */
4699 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4701 ret = upgrade_enable_all(zhp, &count);
4708 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4709 sizeof(cbp->cb_poolname));
4710 #endif /* __FreeBSD __*/
4711 } else if (cur_version == SPA_VERSION) {
4712 (void) printf(gettext("Pool '%s' already has all "
4713 "supported features enabled.\n"),
4714 zpool_get_name(zhp));
4719 (void) printf(gettext("\n"));
4728 * zpool upgrade [-V version] <-a | pool ...>
4730 * With no arguments, display downrev'd ZFS pool available for upgrade.
4731 * Individual pools can be upgraded by specifying the pool, and '-a' will
4732 * upgrade all pools.
4735 zpool_do_upgrade(int argc, char **argv)
4738 upgrade_cbdata_t cb = { 0 };
4740 boolean_t showversions = B_FALSE;
4741 boolean_t upgradeall = B_FALSE;
4746 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4749 upgradeall = B_TRUE;
4752 showversions = B_TRUE;
4755 cb.cb_version = strtoll(optarg, &end, 10);
4757 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4758 (void) fprintf(stderr,
4759 gettext("invalid version '%s'\n"), optarg);
4764 (void) fprintf(stderr, gettext("missing argument for "
4765 "'%c' option\n"), optopt);
4769 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4780 if (cb.cb_version == 0) {
4781 cb.cb_version = SPA_VERSION;
4782 } else if (!upgradeall && argc == 0) {
4783 (void) fprintf(stderr, gettext("-V option is "
4784 "incompatible with other arguments\n"));
4789 if (upgradeall || argc != 0) {
4790 (void) fprintf(stderr, gettext("-v option is "
4791 "incompatible with other arguments\n"));
4794 } else if (upgradeall) {
4796 (void) fprintf(stderr, gettext("-a option should not "
4797 "be used along with a pool name\n"));
4802 (void) printf(gettext("This system supports ZFS pool feature "
4807 (void) printf(gettext("The following features are "
4809 (void) printf(gettext("FEAT DESCRIPTION\n"));
4810 (void) printf("----------------------------------------------"
4811 "---------------\n");
4812 for (i = 0; i < SPA_FEATURES; i++) {
4813 zfeature_info_t *fi = &spa_feature_table[i];
4814 const char *ro = fi->fi_can_readonly ?
4815 " (read-only compatible)" : "";
4817 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4818 (void) printf(" %s\n", fi->fi_desc);
4820 (void) printf("\n");
4822 (void) printf(gettext("The following legacy versions are also "
4824 (void) printf(gettext("VER DESCRIPTION\n"));
4825 (void) printf("--- -----------------------------------------"
4826 "---------------\n");
4827 (void) printf(gettext(" 1 Initial ZFS version\n"));
4828 (void) printf(gettext(" 2 Ditto blocks "
4829 "(replicated metadata)\n"));
4830 (void) printf(gettext(" 3 Hot spares and double parity "
4832 (void) printf(gettext(" 4 zpool history\n"));
4833 (void) printf(gettext(" 5 Compression using the gzip "
4835 (void) printf(gettext(" 6 bootfs pool property\n"));
4836 (void) printf(gettext(" 7 Separate intent log devices\n"));
4837 (void) printf(gettext(" 8 Delegated administration\n"));
4838 (void) printf(gettext(" 9 refquota and refreservation "
4840 (void) printf(gettext(" 10 Cache devices\n"));
4841 (void) printf(gettext(" 11 Improved scrub performance\n"));
4842 (void) printf(gettext(" 12 Snapshot properties\n"));
4843 (void) printf(gettext(" 13 snapused property\n"));
4844 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4845 (void) printf(gettext(" 15 user/group space accounting\n"));
4846 (void) printf(gettext(" 16 stmf property support\n"));
4847 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4848 (void) printf(gettext(" 18 Snapshot user holds\n"));
4849 (void) printf(gettext(" 19 Log device removal\n"));
4850 (void) printf(gettext(" 20 Compression using zle "
4851 "(zero-length encoding)\n"));
4852 (void) printf(gettext(" 21 Deduplication\n"));
4853 (void) printf(gettext(" 22 Received properties\n"));
4854 (void) printf(gettext(" 23 Slim ZIL\n"));
4855 (void) printf(gettext(" 24 System attributes\n"));
4856 (void) printf(gettext(" 25 Improved scrub stats\n"));
4857 (void) printf(gettext(" 26 Improved snapshot deletion "
4859 (void) printf(gettext(" 27 Improved snapshot creation "
4861 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4862 (void) printf(gettext("\nFor more information on a particular "
4863 "version, including supported releases,\n"));
4864 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4865 } else if (argc == 0 && upgradeall) {
4866 cb.cb_first = B_TRUE;
4867 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4868 if (ret == 0 && cb.cb_first) {
4869 if (cb.cb_version == SPA_VERSION) {
4870 (void) printf(gettext("All pools are already "
4871 "formatted using feature flags.\n\n"));
4872 (void) printf(gettext("Every feature flags "
4873 "pool already has all supported features "
4876 (void) printf(gettext("All pools are already "
4877 "formatted with version %llu or higher.\n"),
4881 } else if (argc == 0) {
4882 cb.cb_first = B_TRUE;
4883 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4887 (void) printf(gettext("All pools are formatted "
4888 "using feature flags.\n\n"));
4890 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4891 "for a list of available legacy versions.\n"));
4894 cb.cb_first = B_TRUE;
4895 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4899 (void) printf(gettext("Every feature flags pool has "
4900 "all supported features enabled.\n"));
4902 (void) printf(gettext("\n"));
4905 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4909 if (cb.cb_poolname[0] != '\0') {
4911 "If you boot from pool '%s', don't forget to update boot code.\n"
4912 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4913 "the following command will do it:\n"
4915 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4922 typedef struct hist_cbdata {
4929 * Print out the command history for a specific pool.
4932 get_history_one(zpool_handle_t *zhp, void *data)
4948 char internalstr[MAXPATHLEN];
4949 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4953 cb->first = B_FALSE;
4955 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4957 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4960 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4961 &records, &numrecords) == 0);
4962 for (i = 0; i < numrecords; i++) {
4963 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4967 /* is it an internal event or a standard event? */
4968 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4970 if (cb->internal == 0)
4973 if (nvlist_lookup_uint64(records[i],
4974 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4976 verify(nvlist_lookup_uint64(records[i],
4977 ZPOOL_HIST_TXG, &txg) == 0);
4978 verify(nvlist_lookup_string(records[i],
4979 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4980 if (ievent >= LOG_END)
4982 (void) snprintf(internalstr,
4983 sizeof (internalstr),
4984 "[internal %s txg:%lld] %s",
4985 zfs_history_event_names[ievent], txg,
4987 cmdstr = internalstr;
4990 (void) localtime_r(&tsec, &t);
4991 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4992 (void) printf("%s %s", tbuf, cmdstr);
4995 (void) printf("\n");
4998 (void) printf(" [");
4999 if (nvlist_lookup_uint64(records[i],
5000 ZPOOL_HIST_WHO, &who) == 0) {
5001 pwd = getpwuid((uid_t)who);
5003 (void) printf("user %s on",
5006 (void) printf("user %d on",
5009 (void) printf(gettext("no info]\n"));
5012 if (nvlist_lookup_string(records[i],
5013 ZPOOL_HIST_HOST, &hostname) == 0) {
5014 (void) printf(" %s", hostname);
5016 if (nvlist_lookup_string(records[i],
5017 ZPOOL_HIST_ZONE, &zonename) == 0) {
5018 (void) printf(":%s", zonename);
5022 (void) printf("\n");
5024 (void) printf("\n");
5031 * zpool history <pool>
5033 * Displays the history of commands that modified pools.
5038 zpool_do_history(int argc, char **argv)
5040 hist_cbdata_t cbdata = { 0 };
5044 cbdata.first = B_TRUE;
5046 while ((c = getopt(argc, argv, "li")) != -1) {
5052 cbdata.internal = 1;
5055 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5063 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5066 if (argc == 0 && cbdata.first == B_TRUE) {
5067 (void) printf(gettext("no pools available\n"));
5075 get_callback(zpool_handle_t *zhp, void *data)
5077 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5078 char value[MAXNAMELEN];
5079 zprop_source_t srctype;
5082 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5085 * Skip the special fake placeholder. This will also skip
5086 * over the name property when 'all' is specified.
5088 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5089 pl == cbp->cb_proplist)
5092 if (pl->pl_prop == ZPROP_INVAL &&
5093 (zpool_prop_feature(pl->pl_user_prop) ||
5094 zpool_prop_unsupported(pl->pl_user_prop))) {
5095 srctype = ZPROP_SRC_LOCAL;
5097 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5098 value, sizeof (value)) == 0) {
5099 zprop_print_one_property(zpool_get_name(zhp),
5100 cbp, pl->pl_user_prop, value, srctype,
5104 if (zpool_get_prop(zhp, pl->pl_prop, value,
5105 sizeof (value), &srctype) != 0)
5108 zprop_print_one_property(zpool_get_name(zhp), cbp,
5109 zpool_prop_to_name(pl->pl_prop), value, srctype,
5117 zpool_do_get(int argc, char **argv)
5119 zprop_get_cbdata_t cb = { 0 };
5120 zprop_list_t fake_name = { 0 };
5124 (void) fprintf(stderr, gettext("missing property "
5129 cb.cb_first = B_TRUE;
5130 cb.cb_sources = ZPROP_SRC_ALL;
5131 cb.cb_columns[0] = GET_COL_NAME;
5132 cb.cb_columns[1] = GET_COL_PROPERTY;
5133 cb.cb_columns[2] = GET_COL_VALUE;
5134 cb.cb_columns[3] = GET_COL_SOURCE;
5135 cb.cb_type = ZFS_TYPE_POOL;
5137 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5138 ZFS_TYPE_POOL) != 0)
5141 if (cb.cb_proplist != NULL) {
5142 fake_name.pl_prop = ZPOOL_PROP_NAME;
5143 fake_name.pl_width = strlen(gettext("NAME"));
5144 fake_name.pl_next = cb.cb_proplist;
5145 cb.cb_proplist = &fake_name;
5148 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5151 if (cb.cb_proplist == &fake_name)
5152 zprop_free_list(fake_name.pl_next);
5154 zprop_free_list(cb.cb_proplist);
5159 typedef struct set_cbdata {
5162 boolean_t cb_any_successful;
5166 set_callback(zpool_handle_t *zhp, void *data)
5169 set_cbdata_t *cb = (set_cbdata_t *)data;
5171 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5174 cb->cb_any_successful = B_TRUE;
5180 zpool_do_set(int argc, char **argv)
5182 set_cbdata_t cb = { 0 };
5185 if (argc > 1 && argv[1][0] == '-') {
5186 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5192 (void) fprintf(stderr, gettext("missing property=value "
5198 (void) fprintf(stderr, gettext("missing pool name\n"));
5203 (void) fprintf(stderr, gettext("too many pool names\n"));
5207 cb.cb_propname = argv[1];
5208 cb.cb_value = strchr(cb.cb_propname, '=');
5209 if (cb.cb_value == NULL) {
5210 (void) fprintf(stderr, gettext("missing value in "
5211 "property=value argument\n"));
5215 *(cb.cb_value) = '\0';
5218 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5225 find_command_idx(char *command, int *idx)
5229 for (i = 0; i < NCOMMAND; i++) {
5230 if (command_table[i].name == NULL)
5233 if (strcmp(command, command_table[i].name) == 0) {
5242 main(int argc, char **argv)
5248 (void) setlocale(LC_ALL, "");
5249 (void) textdomain(TEXT_DOMAIN);
5251 if ((g_zfs = libzfs_init()) == NULL) {
5252 (void) fprintf(stderr, gettext("internal error: failed to "
5253 "initialize ZFS library\n"));
5257 libzfs_print_on_error(g_zfs, B_TRUE);
5262 * Make sure the user has specified some command.
5265 (void) fprintf(stderr, gettext("missing command\n"));
5274 if (strcmp(cmdname, "-?") == 0)
5277 zpool_set_history_str("zpool", argc, argv, history_str);
5278 verify(zpool_stage_history(g_zfs, history_str) == 0);
5281 * Run the appropriate command.
5283 if (find_command_idx(cmdname, &i) == 0) {
5284 current_command = &command_table[i];
5285 ret = command_table[i].func(argc - 1, argv + 1);
5286 } else if (strchr(cmdname, '=')) {
5287 verify(find_command_idx("set", &i) == 0);
5288 current_command = &command_table[i];
5289 ret = command_table[i].func(argc, argv);
5290 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5292 * 'freeze' is a vile debugging abomination, so we treat
5296 int fd = open(ZFS_DEV, O_RDWR);
5297 (void) strcpy((void *)buf, argv[2]);
5298 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5300 (void) fprintf(stderr, gettext("unrecognized "
5301 "command '%s'\n"), cmdname);
5308 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5309 * for the purposes of running ::findleaks.
5311 if (getenv("ZFS_ABORT") != NULL) {
5312 (void) printf("dumping core by request\n");