4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
50 #include <sys/fs/zfs.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 #include "statcommon.h"
61 static int zpool_do_create(int, char **);
62 static int zpool_do_destroy(int, char **);
64 static int zpool_do_add(int, char **);
65 static int zpool_do_remove(int, char **);
66 static int zpool_do_labelclear(int, char **);
68 static int zpool_do_list(int, char **);
69 static int zpool_do_iostat(int, char **);
70 static int zpool_do_status(int, char **);
72 static int zpool_do_online(int, char **);
73 static int zpool_do_offline(int, char **);
74 static int zpool_do_clear(int, char **);
75 static int zpool_do_reopen(int, char **);
77 static int zpool_do_reguid(int, char **);
79 static int zpool_do_attach(int, char **);
80 static int zpool_do_detach(int, char **);
81 static int zpool_do_replace(int, char **);
82 static int zpool_do_split(int, char **);
84 static int zpool_do_scrub(int, char **);
86 static int zpool_do_import(int, char **);
87 static int zpool_do_export(int, char **);
89 static int zpool_do_upgrade(int, char **);
91 static int zpool_do_history(int, char **);
93 static int zpool_do_get(int, char **);
94 static int zpool_do_set(int, char **);
97 * These libumem hooks provide a reasonable set of defaults for the allocator's
98 * debugging facilities.
103 _umem_debug_init(void)
105 return ("default,verbose"); /* $UMEM_DEBUG setting */
109 _umem_logging_init(void)
111 return ("fail,contents"); /* $UMEM_LOGGING setting */
143 typedef struct zpool_command {
145 int (*func)(int, char **);
150 * Master command table. Each ZFS command has a name, associated function, and
151 * usage message. The usage messages need to be internationalized, so we have
152 * to have a function to return the usage message based on a command index.
154 * These commands are organized according to how they are displayed in the usage
155 * message. An empty command (one with a NULL name) indicates an empty line in
156 * the generic usage message.
158 static zpool_command_t command_table[] = {
159 { "create", zpool_do_create, HELP_CREATE },
160 { "destroy", zpool_do_destroy, HELP_DESTROY },
162 { "add", zpool_do_add, HELP_ADD },
163 { "remove", zpool_do_remove, HELP_REMOVE },
165 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
167 { "list", zpool_do_list, HELP_LIST },
168 { "iostat", zpool_do_iostat, HELP_IOSTAT },
169 { "status", zpool_do_status, HELP_STATUS },
171 { "online", zpool_do_online, HELP_ONLINE },
172 { "offline", zpool_do_offline, HELP_OFFLINE },
173 { "clear", zpool_do_clear, HELP_CLEAR },
174 { "reopen", zpool_do_reopen, HELP_REOPEN },
176 { "attach", zpool_do_attach, HELP_ATTACH },
177 { "detach", zpool_do_detach, HELP_DETACH },
178 { "replace", zpool_do_replace, HELP_REPLACE },
179 { "split", zpool_do_split, HELP_SPLIT },
181 { "scrub", zpool_do_scrub, HELP_SCRUB },
183 { "import", zpool_do_import, HELP_IMPORT },
184 { "export", zpool_do_export, HELP_EXPORT },
185 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
186 { "reguid", zpool_do_reguid, HELP_REGUID },
188 { "history", zpool_do_history, HELP_HISTORY },
189 { "get", zpool_do_get, HELP_GET },
190 { "set", zpool_do_set, HELP_SET },
193 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
195 static zpool_command_t *current_command;
196 static char history_str[HIS_MAX_RECORD_LEN];
197 static boolean_t log_history = B_TRUE;
198 static uint_t timestamp_fmt = NODATE;
201 get_usage(zpool_help_t idx) {
204 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
206 return (gettext("\tattach [-f] <pool> <device> "
209 return (gettext("\tclear [-nF] <pool> [device]\n"));
211 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
212 "\t [-O file-system-property=value] ... \n"
213 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
215 return (gettext("\tdestroy [-f] <pool>\n"));
217 return (gettext("\tdetach <pool> <device>\n"));
219 return (gettext("\texport [-f] <pool> ...\n"));
221 return (gettext("\thistory [-il] [<pool>] ...\n"));
223 return (gettext("\timport [-d dir] [-D]\n"
224 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
225 "\timport [-o mntopts] [-o property=value] ... \n"
226 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
227 "[-R root] [-F [-n]] -a\n"
228 "\timport [-o mntopts] [-o property=value] ... \n"
229 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
230 "[-R root] [-F [-n]]\n"
231 "\t <pool | id> [newpool]\n"));
233 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
235 case HELP_LABELCLEAR:
236 return (gettext("\tlabelclear [-f] <vdev>\n"));
238 return (gettext("\tlist [-Hv] [-o property[,...]] "
239 "[-T d|u] [pool] ... [interval [count]]\n"));
241 return (gettext("\toffline [-t] <pool> <device> ...\n"));
243 return (gettext("\tonline [-e] <pool> <device> ...\n"));
245 return (gettext("\treplace [-f] <pool> <device> "
248 return (gettext("\tremove <pool> <device> ...\n"));
250 return (""); /* Undocumented command */
252 return (gettext("\tscrub [-s] <pool> ...\n"));
254 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
257 return (gettext("\tupgrade [-v]\n"
258 "\tupgrade [-V version] <-a | pool ...>\n"));
260 return (gettext("\tget <\"all\" | property[,...]> "
263 return (gettext("\tset <property=value> <pool> \n"));
265 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
266 "\t [-o property=value] <pool> <newpool> "
267 "[<device> ...]\n"));
269 return (gettext("\treguid <pool>\n"));
278 * Callback routine that will print out a pool property value.
281 print_prop_cb(int prop, void *cb)
285 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
287 if (zpool_prop_readonly(prop))
288 (void) fprintf(fp, " NO ");
290 (void) fprintf(fp, " YES ");
292 if (zpool_prop_values(prop) == NULL)
293 (void) fprintf(fp, "-\n");
295 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
301 * Display usage message. If we're inside a command, display only the usage for
302 * that command. Otherwise, iterate over the entire command table and display
303 * a complete usage message.
306 usage(boolean_t requested)
308 FILE *fp = requested ? stdout : stderr;
310 if (current_command == NULL) {
313 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
315 gettext("where 'command' is one of the following:\n\n"));
317 for (i = 0; i < NCOMMAND; i++) {
318 if (command_table[i].name == NULL)
319 (void) fprintf(fp, "\n");
321 (void) fprintf(fp, "%s",
322 get_usage(command_table[i].usage));
325 (void) fprintf(fp, gettext("usage:\n"));
326 (void) fprintf(fp, "%s", get_usage(current_command->usage));
329 if (current_command != NULL &&
330 ((strcmp(current_command->name, "set") == 0) ||
331 (strcmp(current_command->name, "get") == 0) ||
332 (strcmp(current_command->name, "list") == 0))) {
335 gettext("\nthe following properties are supported:\n"));
337 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
338 "PROPERTY", "EDIT", "VALUES");
340 /* Iterate over all properties */
341 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
344 (void) fprintf(fp, "\t%-15s ", "feature@...");
345 (void) fprintf(fp, "YES disabled | enabled | active\n");
347 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
348 "appended with a feature name.\nSee zpool-features(7).\n"));
352 * See comments at end of main().
354 if (getenv("ZFS_ABORT") != NULL) {
355 (void) printf("dumping core by request\n");
359 exit(requested ? 0 : 2);
363 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
364 boolean_t print_logs)
371 (void) printf("\t%*s%s\n", indent, "", name);
373 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
374 &child, &children) != 0)
377 for (c = 0; c < children; c++) {
378 uint64_t is_log = B_FALSE;
380 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
382 if ((is_log && !print_logs) || (!is_log && print_logs))
385 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
386 print_vdev_tree(zhp, vname, child[c], indent + 2,
393 prop_list_contains_feature(nvlist_t *proplist)
396 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
397 nvp = nvlist_next_nvpair(proplist, nvp)) {
398 if (zpool_prop_feature(nvpair_name(nvp)))
405 * Add a property pair (name, string-value) into a property nvlist.
408 add_prop_list(const char *propname, char *propval, nvlist_t **props,
411 zpool_prop_t prop = ZPROP_INVAL;
417 if (*props == NULL &&
418 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
419 (void) fprintf(stderr,
420 gettext("internal error: out of memory\n"));
427 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
429 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
430 !zpool_prop_feature(propname)) {
431 (void) fprintf(stderr, gettext("property '%s' is "
432 "not a valid pool property\n"), propname);
437 * feature@ properties and version should not be specified
440 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
441 nvlist_exists(proplist, vname)) ||
442 (prop == ZPOOL_PROP_VERSION &&
443 prop_list_contains_feature(proplist))) {
444 (void) fprintf(stderr, gettext("'feature@' and "
445 "'version' properties cannot be specified "
451 if (zpool_prop_feature(propname))
454 normnm = zpool_prop_to_name(prop);
456 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
457 normnm = zfs_prop_to_name(fprop);
463 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
464 prop != ZPOOL_PROP_CACHEFILE) {
465 (void) fprintf(stderr, gettext("property '%s' "
466 "specified multiple times\n"), propname);
470 if (nvlist_add_string(proplist, normnm, propval) != 0) {
471 (void) fprintf(stderr, gettext("internal "
472 "error: out of memory\n"));
480 * zpool add [-fn] <pool> <vdev> ...
482 * -f Force addition of devices, even if they appear in use
483 * -n Do not add the devices, but display the resulting layout if
484 * they were to be added.
486 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
487 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
491 zpool_do_add(int argc, char **argv)
493 boolean_t force = B_FALSE;
494 boolean_t dryrun = B_FALSE;
503 while ((c = getopt(argc, argv, "fn")) != -1) {
512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
521 /* get pool name and check number of arguments */
523 (void) fprintf(stderr, gettext("missing pool name argument\n"));
527 (void) fprintf(stderr, gettext("missing vdev specification\n"));
536 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
539 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
540 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
546 /* pass off to get_vdev_spec for processing */
547 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
549 if (nvroot == NULL) {
555 nvlist_t *poolnvroot;
557 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
560 (void) printf(gettext("would update '%s' to the following "
561 "configuration:\n"), zpool_get_name(zhp));
563 /* print original main pool and new tree */
564 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
565 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
567 /* Do the same for the logs */
568 if (num_logs(poolnvroot) > 0) {
569 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
570 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
571 } else if (num_logs(nvroot) > 0) {
572 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
577 ret = (zpool_add(zhp, nvroot) != 0);
587 * zpool remove <pool> <vdev> ...
589 * Removes the given vdev from the pool. Currently, this supports removing
590 * spares, cache, and log devices from the pool.
593 zpool_do_remove(int argc, char **argv)
602 /* get pool name and check number of arguments */
604 (void) fprintf(stderr, gettext("missing pool name argument\n"));
608 (void) fprintf(stderr, gettext("missing device\n"));
614 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
617 for (i = 1; i < argc; i++) {
618 if (zpool_vdev_remove(zhp, argv[i]) != 0)
626 * zpool labelclear <vdev>
628 * Verifies that the vdev is not active and zeros out the label information
632 zpool_do_labelclear(int argc, char **argv)
635 int c, fd = -1, ret = 0;
637 boolean_t inuse = B_FALSE;
638 boolean_t force = B_FALSE;
641 while ((c = getopt(argc, argv, "f")) != -1) {
647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
658 (void) fprintf(stderr, gettext("missing vdev device name\n"));
663 if ((fd = open(vdev, O_RDWR)) < 0) {
664 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
669 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
673 (void) fprintf(stderr,
674 gettext("Unable to determine pool state for %s\n"
675 "Use -f to force the clearing any label data\n"), vdev);
683 case POOL_STATE_ACTIVE:
684 case POOL_STATE_SPARE:
685 case POOL_STATE_L2CACHE:
686 (void) fprintf(stderr,
687 gettext("labelclear operation failed.\n"
688 "\tVdev %s is a member (%s), of pool \"%s\".\n"
689 "\tTo remove label information from this device, export or destroy\n"
690 "\tthe pool, or remove %s from the configuration of this pool\n"
691 "\tand retry the labelclear operation\n"),
692 vdev, zpool_pool_state_to_name(state), name, vdev);
696 case POOL_STATE_EXPORTED:
700 (void) fprintf(stderr,
701 gettext("labelclear operation failed.\n"
702 "\tVdev %s is a member of the exported pool \"%s\".\n"
703 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
709 case POOL_STATE_POTENTIALLY_ACTIVE:
713 (void) fprintf(stderr,
714 gettext("labelclear operation failed.\n"
715 "\tVdev %s is a member of the pool \"%s\".\n"
716 "\tThis pool is unknown to this system, but may be active on\n"
717 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
718 "\tremoval of label information.\n"),
723 case POOL_STATE_DESTROYED:
724 /* inuse should never be set for a destoryed pool... */
730 if (zpool_clear_label(fd) != 0) {
731 (void) fprintf(stderr,
732 gettext("Label clear failed on vdev %s\n"), vdev);
745 * zpool create [-fnd] [-o property=value] ...
746 * [-O file-system-property=value] ...
747 * [-R root] [-m mountpoint] <pool> <dev> ...
749 * -f Force creation, even if devices appear in use
750 * -n Do not create the pool, but display the resulting layout if it
751 * were to be created.
752 * -R Create a pool under an alternate root
753 * -m Set default mountpoint for the root dataset. By default it's
755 * -o Set property=value.
756 * -d Don't automatically enable all supported pool features
757 * (individual features can be enabled with -o).
758 * -O Set fsproperty=value in the pool's root file system
760 * Creates the named pool according to the given vdev specification. The
761 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
762 * we get the nvlist back from get_vdev_spec(), we either print out the contents
763 * (if '-n' was specified), or pass it to libzfs to do the creation.
766 zpool_do_create(int argc, char **argv)
768 boolean_t force = B_FALSE;
769 boolean_t dryrun = B_FALSE;
770 boolean_t enable_all_pool_feat = B_TRUE;
772 nvlist_t *nvroot = NULL;
775 char *altroot = NULL;
776 char *mountpoint = NULL;
777 nvlist_t *fsprops = NULL;
778 nvlist_t *props = NULL;
782 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
791 enable_all_pool_feat = B_FALSE;
795 if (add_prop_list(zpool_prop_to_name(
796 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
798 if (nvlist_lookup_string(props,
799 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
802 if (add_prop_list(zpool_prop_to_name(
803 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
810 if ((propval = strchr(optarg, '=')) == NULL) {
811 (void) fprintf(stderr, gettext("missing "
812 "'=' for -o option\n"));
818 if (add_prop_list(optarg, propval, &props, B_TRUE))
822 * If the user is creating a pool that doesn't support
823 * feature flags, don't enable any features.
825 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
829 ver = strtoull(propval, &end, 10);
831 ver < SPA_VERSION_FEATURES) {
832 enable_all_pool_feat = B_FALSE;
837 if ((propval = strchr(optarg, '=')) == NULL) {
838 (void) fprintf(stderr, gettext("missing "
839 "'=' for -O option\n"));
845 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
849 (void) fprintf(stderr, gettext("missing argument for "
850 "'%c' option\n"), optopt);
853 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
862 /* get pool name and check number of arguments */
864 (void) fprintf(stderr, gettext("missing pool name argument\n"));
868 (void) fprintf(stderr, gettext("missing vdev specification\n"));
875 * As a special case, check for use of '/' in the name, and direct the
876 * user to use 'zfs create' instead.
878 if (strchr(poolname, '/') != NULL) {
879 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
880 "character '/' in pool name\n"), poolname);
881 (void) fprintf(stderr, gettext("use 'zfs create' to "
882 "create a dataset\n"));
886 /* pass off to get_vdev_spec for bulk processing */
887 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
892 /* make_root_vdev() allows 0 toplevel children if there are spares */
893 if (!zfs_allocatable_devs(nvroot)) {
894 (void) fprintf(stderr, gettext("invalid vdev "
895 "specification: at least one toplevel vdev must be "
900 if (altroot != NULL && altroot[0] != '/') {
901 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
902 "must be an absolute path\n"), altroot);
907 * Check the validity of the mountpoint and direct the user to use the
908 * '-m' mountpoint option if it looks like its in use.
910 if (mountpoint == NULL ||
911 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
912 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
913 char buf[MAXPATHLEN];
916 if (mountpoint && mountpoint[0] != '/') {
917 (void) fprintf(stderr, gettext("invalid mountpoint "
918 "'%s': must be an absolute path, 'legacy', or "
919 "'none'\n"), mountpoint);
923 if (mountpoint == NULL) {
925 (void) snprintf(buf, sizeof (buf), "%s/%s",
928 (void) snprintf(buf, sizeof (buf), "/%s",
932 (void) snprintf(buf, sizeof (buf), "%s%s",
933 altroot, mountpoint);
935 (void) snprintf(buf, sizeof (buf), "%s",
939 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
940 (void) fprintf(stderr, gettext("mountpoint '%s' : "
941 "%s\n"), buf, strerror(errno));
942 (void) fprintf(stderr, gettext("use '-m' "
943 "option to provide a different default\n"));
948 while (count < 3 && readdir(dirp) != NULL)
950 (void) closedir(dirp);
953 (void) fprintf(stderr, gettext("mountpoint "
954 "'%s' exists and is not empty\n"), buf);
955 (void) fprintf(stderr, gettext("use '-m' "
956 "option to provide a "
957 "different default\n"));
965 * For a dry run invocation, print out a basic message and run
966 * through all the vdevs in the list and print out in an
967 * appropriate hierarchy.
969 (void) printf(gettext("would create '%s' with the "
970 "following layout:\n\n"), poolname);
972 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
973 if (num_logs(nvroot) > 0)
974 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
979 * Hand off to libzfs.
981 if (enable_all_pool_feat) {
983 for (i = 0; i < SPA_FEATURES; i++) {
984 char propname[MAXPATHLEN];
985 zfeature_info_t *feat = &spa_feature_table[i];
987 (void) snprintf(propname, sizeof (propname),
988 "feature@%s", feat->fi_uname);
991 * Skip feature if user specified it manually
992 * on the command line.
994 if (nvlist_exists(props, propname))
997 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
998 &props, B_TRUE) != 0)
1002 if (zpool_create(g_zfs, poolname,
1003 nvroot, props, fsprops) == 0) {
1004 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1005 ZFS_TYPE_FILESYSTEM);
1007 if (mountpoint != NULL)
1008 verify(zfs_prop_set(pool,
1010 ZFS_PROP_MOUNTPOINT),
1012 if (zfs_mount(pool, NULL, 0) == 0)
1013 ret = zfs_shareall(pool);
1016 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1017 (void) fprintf(stderr, gettext("pool name may have "
1023 nvlist_free(nvroot);
1024 nvlist_free(fsprops);
1028 nvlist_free(fsprops);
1035 * zpool destroy <pool>
1037 * -f Forcefully unmount any datasets
1039 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1042 zpool_do_destroy(int argc, char **argv)
1044 boolean_t force = B_FALSE;
1047 zpool_handle_t *zhp;
1051 while ((c = getopt(argc, argv, "f")) != -1) {
1057 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1066 /* check arguments */
1068 (void) fprintf(stderr, gettext("missing pool argument\n"));
1072 (void) fprintf(stderr, gettext("too many arguments\n"));
1078 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1080 * As a special case, check for use of '/' in the name, and
1081 * direct the user to use 'zfs destroy' instead.
1083 if (strchr(pool, '/') != NULL)
1084 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1085 "destroy a dataset\n"));
1089 if (zpool_disable_datasets(zhp, force) != 0) {
1090 (void) fprintf(stderr, gettext("could not destroy '%s': "
1091 "could not unmount datasets\n"), zpool_get_name(zhp));
1095 /* The history must be logged as part of the export */
1096 log_history = B_FALSE;
1098 ret = (zpool_destroy(zhp, history_str) != 0);
1106 * zpool export [-f] <pool> ...
1108 * -f Forcefully unmount datasets
1110 * Export the given pools. By default, the command will attempt to cleanly
1111 * unmount any active datasets within the pool. If the '-f' flag is specified,
1112 * then the datasets will be forcefully unmounted.
1115 zpool_do_export(int argc, char **argv)
1117 boolean_t force = B_FALSE;
1118 boolean_t hardforce = B_FALSE;
1120 zpool_handle_t *zhp;
1125 while ((c = getopt(argc, argv, "fF")) != -1) {
1134 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1143 /* check arguments */
1145 (void) fprintf(stderr, gettext("missing pool argument\n"));
1150 for (i = 0; i < argc; i++) {
1151 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1156 if (zpool_disable_datasets(zhp, force) != 0) {
1162 /* The history must be logged as part of the export */
1163 log_history = B_FALSE;
1166 if (zpool_export_force(zhp, history_str) != 0)
1168 } else if (zpool_export(zhp, force, history_str) != 0) {
1179 * Given a vdev configuration, determine the maximum width needed for the device
1183 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1185 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1190 if (strlen(name) + depth > max)
1191 max = strlen(name) + depth;
1195 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1196 &child, &children) == 0) {
1197 for (c = 0; c < children; c++)
1198 if ((ret = max_width(zhp, child[c], depth + 2,
1203 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1204 &child, &children) == 0) {
1205 for (c = 0; c < children; c++)
1206 if ((ret = max_width(zhp, child[c], depth + 2,
1211 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1212 &child, &children) == 0) {
1213 for (c = 0; c < children; c++)
1214 if ((ret = max_width(zhp, child[c], depth + 2,
1223 typedef struct spare_cbdata {
1225 zpool_handle_t *cb_zhp;
1229 find_vdev(nvlist_t *nv, uint64_t search)
1235 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1239 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1240 &child, &children) == 0) {
1241 for (c = 0; c < children; c++)
1242 if (find_vdev(child[c], search))
1250 find_spare(zpool_handle_t *zhp, void *data)
1252 spare_cbdata_t *cbp = data;
1253 nvlist_t *config, *nvroot;
1255 config = zpool_get_config(zhp, NULL);
1256 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1259 if (find_vdev(nvroot, cbp->cb_guid)) {
1269 * Print out configuration state as requested by status_callback.
1272 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1273 int namewidth, int depth, boolean_t isspare)
1277 pool_scan_stat_t *ps = NULL;
1279 char rbuf[6], wbuf[6], cbuf[6];
1281 uint64_t notpresent;
1285 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1286 &child, &children) != 0)
1289 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1290 (uint64_t **)&vs, &c) == 0);
1292 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1295 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1298 if (vs->vs_aux == VDEV_AUX_SPARED)
1300 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1304 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1308 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1309 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1310 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1311 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1314 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1315 ¬present) == 0 ||
1316 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1318 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1319 (void) printf(" was %s", path);
1320 } else if (vs->vs_aux != 0) {
1323 switch (vs->vs_aux) {
1324 case VDEV_AUX_OPEN_FAILED:
1325 (void) printf(gettext("cannot open"));
1328 case VDEV_AUX_BAD_GUID_SUM:
1329 (void) printf(gettext("missing device"));
1332 case VDEV_AUX_NO_REPLICAS:
1333 (void) printf(gettext("insufficient replicas"));
1336 case VDEV_AUX_VERSION_NEWER:
1337 (void) printf(gettext("newer version"));
1340 case VDEV_AUX_UNSUP_FEAT:
1341 (void) printf(gettext("unsupported feature(s)"));
1344 case VDEV_AUX_SPARED:
1345 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1347 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1348 if (strcmp(zpool_get_name(cb.cb_zhp),
1349 zpool_get_name(zhp)) == 0)
1350 (void) printf(gettext("currently in "
1353 (void) printf(gettext("in use by "
1355 zpool_get_name(cb.cb_zhp));
1356 zpool_close(cb.cb_zhp);
1358 (void) printf(gettext("currently in use"));
1362 case VDEV_AUX_ERR_EXCEEDED:
1363 (void) printf(gettext("too many errors"));
1366 case VDEV_AUX_IO_FAILURE:
1367 (void) printf(gettext("experienced I/O failures"));
1370 case VDEV_AUX_BAD_LOG:
1371 (void) printf(gettext("bad intent log"));
1374 case VDEV_AUX_EXTERNAL:
1375 (void) printf(gettext("external device fault"));
1378 case VDEV_AUX_SPLIT_POOL:
1379 (void) printf(gettext("split into new pool"));
1383 (void) printf(gettext("corrupted data"));
1388 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1389 (uint64_t **)&ps, &c);
1391 if (ps && ps->pss_state == DSS_SCANNING &&
1392 vs->vs_scan_processed != 0 && children == 0) {
1393 (void) printf(gettext(" (%s)"),
1394 (ps->pss_func == POOL_SCAN_RESILVER) ?
1395 "resilvering" : "repairing");
1398 (void) printf("\n");
1400 for (c = 0; c < children; c++) {
1401 uint64_t islog = B_FALSE, ishole = B_FALSE;
1403 /* Don't print logs or holes here */
1404 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1406 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1408 if (islog || ishole)
1410 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1411 print_status_config(zhp, vname, child[c],
1412 namewidth, depth + 2, isspare);
1419 * Print the configuration of an exported pool. Iterate over all vdevs in the
1420 * pool, printing out the name and status for each one.
1423 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1430 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1431 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1432 strcmp(type, VDEV_TYPE_HOLE) == 0)
1435 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1436 (uint64_t **)&vs, &c) == 0);
1438 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1439 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1441 if (vs->vs_aux != 0) {
1444 switch (vs->vs_aux) {
1445 case VDEV_AUX_OPEN_FAILED:
1446 (void) printf(gettext("cannot open"));
1449 case VDEV_AUX_BAD_GUID_SUM:
1450 (void) printf(gettext("missing device"));
1453 case VDEV_AUX_NO_REPLICAS:
1454 (void) printf(gettext("insufficient replicas"));
1457 case VDEV_AUX_VERSION_NEWER:
1458 (void) printf(gettext("newer version"));
1461 case VDEV_AUX_UNSUP_FEAT:
1462 (void) printf(gettext("unsupported feature(s)"));
1465 case VDEV_AUX_ERR_EXCEEDED:
1466 (void) printf(gettext("too many errors"));
1470 (void) printf(gettext("corrupted data"));
1474 (void) printf("\n");
1476 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1477 &child, &children) != 0)
1480 for (c = 0; c < children; c++) {
1481 uint64_t is_log = B_FALSE;
1483 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1488 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1489 print_import_config(vname, child[c], namewidth, depth + 2);
1493 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1494 &child, &children) == 0) {
1495 (void) printf(gettext("\tcache\n"));
1496 for (c = 0; c < children; c++) {
1497 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1498 (void) printf("\t %s\n", vname);
1503 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1504 &child, &children) == 0) {
1505 (void) printf(gettext("\tspares\n"));
1506 for (c = 0; c < children; c++) {
1507 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1508 (void) printf("\t %s\n", vname);
1516 * Logs are recorded as top level vdevs in the main pool child array
1517 * but with "is_log" set to 1. We use either print_status_config() or
1518 * print_import_config() to print the top level logs then any log
1519 * children (eg mirrored slogs) are printed recursively - which
1520 * works because only the top level vdev is marked "is_log"
1523 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1528 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1532 (void) printf(gettext("\tlogs\n"));
1534 for (c = 0; c < children; c++) {
1535 uint64_t is_log = B_FALSE;
1538 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1542 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1544 print_status_config(zhp, name, child[c], namewidth,
1547 print_import_config(name, child[c], namewidth, 2);
1553 * Display the status for the given pool.
1556 show_import(nvlist_t *config)
1558 uint64_t pool_state;
1570 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1572 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1574 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1576 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1579 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1580 (uint64_t **)&vs, &vsc) == 0);
1581 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1583 reason = zpool_import_status(config, &msgid);
1585 (void) printf(gettext(" pool: %s\n"), name);
1586 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1587 (void) printf(gettext(" state: %s"), health);
1588 if (pool_state == POOL_STATE_DESTROYED)
1589 (void) printf(gettext(" (DESTROYED)"));
1590 (void) printf("\n");
1593 case ZPOOL_STATUS_MISSING_DEV_R:
1594 case ZPOOL_STATUS_MISSING_DEV_NR:
1595 case ZPOOL_STATUS_BAD_GUID_SUM:
1596 (void) printf(gettext(" status: One or more devices are "
1597 "missing from the system.\n"));
1600 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1601 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1602 (void) printf(gettext(" status: One or more devices contains "
1603 "corrupted data.\n"));
1606 case ZPOOL_STATUS_CORRUPT_DATA:
1608 gettext(" status: The pool data is corrupted.\n"));
1611 case ZPOOL_STATUS_OFFLINE_DEV:
1612 (void) printf(gettext(" status: One or more devices "
1613 "are offlined.\n"));
1616 case ZPOOL_STATUS_CORRUPT_POOL:
1617 (void) printf(gettext(" status: The pool metadata is "
1621 case ZPOOL_STATUS_VERSION_OLDER:
1622 (void) printf(gettext(" status: The pool is formatted using a "
1623 "legacy on-disk version.\n"));
1626 case ZPOOL_STATUS_VERSION_NEWER:
1627 (void) printf(gettext(" status: The pool is formatted using an "
1628 "incompatible version.\n"));
1631 case ZPOOL_STATUS_FEAT_DISABLED:
1632 (void) printf(gettext(" status: Some supported features are "
1633 "not enabled on the pool.\n"));
1636 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1637 (void) printf(gettext("status: The pool uses the following "
1638 "feature(s) not supported on this sytem:\n"));
1639 zpool_print_unsup_feat(config);
1642 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1643 (void) printf(gettext("status: The pool can only be accessed "
1644 "in read-only mode on this system. It\n\tcannot be "
1645 "accessed in read-write mode because it uses the "
1646 "following\n\tfeature(s) not supported on this system:\n"));
1647 zpool_print_unsup_feat(config);
1650 case ZPOOL_STATUS_HOSTID_MISMATCH:
1651 (void) printf(gettext(" status: The pool was last accessed by "
1652 "another system.\n"));
1655 case ZPOOL_STATUS_FAULTED_DEV_R:
1656 case ZPOOL_STATUS_FAULTED_DEV_NR:
1657 (void) printf(gettext(" status: One or more devices are "
1661 case ZPOOL_STATUS_BAD_LOG:
1662 (void) printf(gettext(" status: An intent log record cannot be "
1666 case ZPOOL_STATUS_RESILVERING:
1667 (void) printf(gettext(" status: One or more devices were being "
1673 * No other status can be seen when importing pools.
1675 assert(reason == ZPOOL_STATUS_OK);
1679 * Print out an action according to the overall state of the pool.
1681 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1682 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1683 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1684 (void) printf(gettext(" action: The pool can be "
1685 "imported using its name or numeric identifier, "
1686 "though\n\tsome features will not be available "
1687 "without an explicit 'zpool upgrade'.\n"));
1688 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1689 (void) printf(gettext(" action: The pool can be "
1690 "imported using its name or numeric "
1691 "identifier and\n\tthe '-f' flag.\n"));
1693 (void) printf(gettext(" action: The pool can be "
1694 "imported using its name or numeric "
1697 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1698 (void) printf(gettext(" action: The pool can be imported "
1699 "despite missing or damaged devices. The\n\tfault "
1700 "tolerance of the pool may be compromised if imported.\n"));
1703 case ZPOOL_STATUS_VERSION_NEWER:
1704 (void) printf(gettext(" action: The pool cannot be "
1705 "imported. Access the pool on a system running "
1706 "newer\n\tsoftware, or recreate the pool from "
1709 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1710 (void) printf(gettext("action: The pool cannot be "
1711 "imported. Access the pool on a system that "
1712 "supports\n\tthe required feature(s), or recreate "
1713 "the pool from backup.\n"));
1715 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1716 (void) printf(gettext("action: The pool cannot be "
1717 "imported in read-write mode. Import the pool "
1719 "\t\"-o readonly=on\", access the pool on a system "
1720 "that supports the\n\trequired feature(s), or "
1721 "recreate the pool from backup.\n"));
1723 case ZPOOL_STATUS_MISSING_DEV_R:
1724 case ZPOOL_STATUS_MISSING_DEV_NR:
1725 case ZPOOL_STATUS_BAD_GUID_SUM:
1726 (void) printf(gettext(" action: The pool cannot be "
1727 "imported. Attach the missing\n\tdevices and try "
1731 (void) printf(gettext(" action: The pool cannot be "
1732 "imported due to damaged devices or data.\n"));
1736 /* Print the comment attached to the pool. */
1737 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1738 (void) printf(gettext("comment: %s\n"), comment);
1741 * If the state is "closed" or "can't open", and the aux state
1742 * is "corrupt data":
1744 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1745 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1746 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1747 if (pool_state == POOL_STATE_DESTROYED)
1748 (void) printf(gettext("\tThe pool was destroyed, "
1749 "but can be imported using the '-Df' flags.\n"));
1750 else if (pool_state != POOL_STATE_EXPORTED)
1751 (void) printf(gettext("\tThe pool may be active on "
1752 "another system, but can be imported using\n\t"
1753 "the '-f' flag.\n"));
1757 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1760 (void) printf(gettext(" config:\n\n"));
1762 namewidth = max_width(NULL, nvroot, 0, 0);
1766 print_import_config(name, nvroot, namewidth, 0);
1767 if (num_logs(nvroot) > 0)
1768 print_logs(NULL, nvroot, namewidth, B_FALSE);
1770 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1771 (void) printf(gettext("\n\tAdditional devices are known to "
1772 "be part of this pool, though their\n\texact "
1773 "configuration cannot be determined.\n"));
1778 * Perform the import for the given configuration. This passes the heavy
1779 * lifting off to zpool_import_props(), and then mounts the datasets contained
1783 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1784 nvlist_t *props, int flags)
1786 zpool_handle_t *zhp;
1791 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1794 verify(nvlist_lookup_uint64(config,
1795 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1796 verify(nvlist_lookup_uint64(config,
1797 ZPOOL_CONFIG_VERSION, &version) == 0);
1798 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1799 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1800 "is formatted using an unsupported ZFS version\n"), name);
1802 } else if (state != POOL_STATE_EXPORTED &&
1803 !(flags & ZFS_IMPORT_ANY_HOST)) {
1806 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1808 if ((unsigned long)hostid != gethostid()) {
1813 verify(nvlist_lookup_string(config,
1814 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1815 verify(nvlist_lookup_uint64(config,
1816 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1818 (void) fprintf(stderr, gettext("cannot import "
1819 "'%s': pool may be in use from other "
1820 "system, it was last accessed by %s "
1821 "(hostid: 0x%lx) on %s"), name, hostname,
1822 (unsigned long)hostid,
1823 asctime(localtime(&t)));
1824 (void) fprintf(stderr, gettext("use '-f' to "
1825 "import anyway\n"));
1829 (void) fprintf(stderr, gettext("cannot import '%s': "
1830 "pool may be in use from other system\n"), name);
1831 (void) fprintf(stderr, gettext("use '-f' to import "
1837 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1840 if (newname != NULL)
1841 name = (char *)newname;
1843 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1846 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1847 !(flags & ZFS_IMPORT_ONLY) &&
1848 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1858 * zpool import [-d dir] [-D]
1859 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1860 * [-d dir | -c cachefile] [-f] -a
1861 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1862 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1864 * -c Read pool information from a cachefile instead of searching
1867 * -d Scan in a specific directory, other than /dev/dsk. More than
1868 * one directory can be specified using multiple '-d' options.
1870 * -D Scan for previously destroyed pools or import all or only
1871 * specified destroyed pools.
1873 * -R Temporarily import the pool, with all mountpoints relative to
1874 * the given root. The pool will remain exported when the machine
1877 * -V Import even in the presence of faulted vdevs. This is an
1878 * intentionally undocumented option for testing purposes, and
1879 * treats the pool configuration as complete, leaving any bad
1880 * vdevs in the FAULTED state. In other words, it does verbatim
1883 * -f Force import, even if it appears that the pool is active.
1885 * -F Attempt rewind if necessary.
1887 * -n See if rewind would work, but don't actually rewind.
1889 * -N Import the pool but don't mount datasets.
1891 * -T Specify a starting txg to use for import. This option is
1892 * intentionally undocumented option for testing purposes.
1894 * -a Import all pools found.
1896 * -o Set property=value and/or temporary mount options (without '=').
1898 * The import command scans for pools to import, and import pools based on pool
1899 * name and GUID. The pool can also be renamed as part of the import process.
1902 zpool_do_import(int argc, char **argv)
1904 char **searchdirs = NULL;
1908 nvlist_t *pools = NULL;
1909 boolean_t do_all = B_FALSE;
1910 boolean_t do_destroyed = B_FALSE;
1911 char *mntopts = NULL;
1914 uint64_t searchguid = 0;
1915 char *searchname = NULL;
1917 nvlist_t *found_config;
1918 nvlist_t *policy = NULL;
1919 nvlist_t *props = NULL;
1921 int flags = ZFS_IMPORT_NORMAL;
1922 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1923 boolean_t dryrun = B_FALSE;
1924 boolean_t do_rewind = B_FALSE;
1925 boolean_t xtreme_rewind = B_FALSE;
1926 uint64_t pool_state, txg = -1ULL;
1927 char *cachefile = NULL;
1928 importargs_t idata = { 0 };
1932 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1941 if (searchdirs == NULL) {
1942 searchdirs = safe_malloc(sizeof (char *));
1944 char **tmp = safe_malloc((nsearch + 1) *
1946 bcopy(searchdirs, tmp, nsearch *
1951 searchdirs[nsearch++] = optarg;
1954 do_destroyed = B_TRUE;
1957 flags |= ZFS_IMPORT_ANY_HOST;
1963 flags |= ZFS_IMPORT_MISSING_LOG;
1969 flags |= ZFS_IMPORT_ONLY;
1972 if ((propval = strchr(optarg, '=')) != NULL) {
1975 if (add_prop_list(optarg, propval,
1983 if (add_prop_list(zpool_prop_to_name(
1984 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1986 if (nvlist_lookup_string(props,
1987 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1990 if (add_prop_list(zpool_prop_to_name(
1991 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1996 txg = strtoull(optarg, &endptr, 10);
1997 if (errno != 0 || *endptr != '\0') {
1998 (void) fprintf(stderr,
1999 gettext("invalid txg value\n"));
2002 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2005 flags |= ZFS_IMPORT_VERBATIM;
2008 xtreme_rewind = B_TRUE;
2011 (void) fprintf(stderr, gettext("missing argument for "
2012 "'%c' option\n"), optopt);
2016 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2025 if (cachefile && nsearch != 0) {
2026 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2030 if ((dryrun || xtreme_rewind) && !do_rewind) {
2031 (void) fprintf(stderr,
2032 gettext("-n or -X only meaningful with -F\n"));
2036 rewind_policy = ZPOOL_TRY_REWIND;
2038 rewind_policy = ZPOOL_DO_REWIND;
2040 rewind_policy |= ZPOOL_EXTREME_REWIND;
2042 /* In the future, we can capture further policy and include it here */
2043 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2044 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2045 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2048 if (searchdirs == NULL) {
2049 searchdirs = safe_malloc(sizeof (char *));
2050 searchdirs[0] = "/dev";
2054 /* check argument count */
2057 (void) fprintf(stderr, gettext("too many arguments\n"));
2062 (void) fprintf(stderr, gettext("too many arguments\n"));
2067 * Check for the SYS_CONFIG privilege. We do this explicitly
2068 * here because otherwise any attempt to discover pools will
2071 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2072 (void) fprintf(stderr, gettext("cannot "
2073 "discover pools: permission denied\n"));
2075 nvlist_free(policy);
2081 * Depending on the arguments given, we do one of the following:
2083 * <none> Iterate through all pools and display information about
2086 * -a Iterate through all pools and try to import each one.
2088 * <id> Find the pool that corresponds to the given GUID/pool
2089 * name and import that one.
2091 * -D Above options applies only to destroyed pools.
2097 searchguid = strtoull(argv[0], &endptr, 10);
2098 if (errno != 0 || *endptr != '\0')
2099 searchname = argv[0];
2100 found_config = NULL;
2103 * User specified a name or guid. Ensure it's unique.
2105 idata.unique = B_TRUE;
2109 idata.path = searchdirs;
2110 idata.paths = nsearch;
2111 idata.poolname = searchname;
2112 idata.guid = searchguid;
2113 idata.cachefile = cachefile;
2115 pools = zpool_search_import(g_zfs, &idata);
2117 if (pools != NULL && idata.exists &&
2118 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2119 (void) fprintf(stderr, gettext("cannot import '%s': "
2120 "a pool with that name already exists\n"),
2122 (void) fprintf(stderr, gettext("use the form '%s "
2123 "<pool | id> <newpool>' to give it a new name\n"),
2126 } else if (pools == NULL && idata.exists) {
2127 (void) fprintf(stderr, gettext("cannot import '%s': "
2128 "a pool with that name is already created/imported,\n"),
2130 (void) fprintf(stderr, gettext("and no additional pools "
2131 "with that name were found\n"));
2133 } else if (pools == NULL) {
2135 (void) fprintf(stderr, gettext("cannot import '%s': "
2136 "no such pool available\n"), argv[0]);
2143 nvlist_free(policy);
2148 * At this point we have a list of import candidate configs. Even if
2149 * we were searching by pool name or guid, we still need to
2150 * post-process the list to deal with pool state and possible
2156 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2158 verify(nvpair_value_nvlist(elem, &config) == 0);
2160 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2162 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2164 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2167 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2174 (void) printf("\n");
2177 err |= do_import(config, NULL, mntopts,
2180 show_import(config);
2182 } else if (searchname != NULL) {
2186 * We are searching for a pool based on name.
2188 verify(nvlist_lookup_string(config,
2189 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2191 if (strcmp(name, searchname) == 0) {
2192 if (found_config != NULL) {
2193 (void) fprintf(stderr, gettext(
2194 "cannot import '%s': more than "
2195 "one matching pool\n"), searchname);
2196 (void) fprintf(stderr, gettext(
2197 "import by numeric ID instead\n"));
2200 found_config = config;
2206 * Search for a pool by guid.
2208 verify(nvlist_lookup_uint64(config,
2209 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2211 if (guid == searchguid)
2212 found_config = config;
2217 * If we were searching for a specific pool, verify that we found a
2218 * pool, and then do the import.
2220 if (argc != 0 && err == 0) {
2221 if (found_config == NULL) {
2222 (void) fprintf(stderr, gettext("cannot import '%s': "
2223 "no such pool available\n"), argv[0]);
2226 err |= do_import(found_config, argc == 1 ? NULL :
2227 argv[1], mntopts, props, flags);
2232 * If we were just looking for pools, report an error if none were
2235 if (argc == 0 && first)
2236 (void) fprintf(stderr,
2237 gettext("no pools available to import\n"));
2242 nvlist_free(policy);
2245 return (err ? 1 : 0);
2248 typedef struct iostat_cbdata {
2249 boolean_t cb_verbose;
2252 zpool_list_t *cb_list;
2256 print_iostat_separator(iostat_cbdata_t *cb)
2260 for (i = 0; i < cb->cb_namewidth; i++)
2262 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2266 print_iostat_header(iostat_cbdata_t *cb)
2268 (void) printf("%*s capacity operations bandwidth\n",
2269 cb->cb_namewidth, "");
2270 (void) printf("%-*s alloc free read write read write\n",
2271 cb->cb_namewidth, "pool");
2272 print_iostat_separator(cb);
2276 * Display a single statistic.
2279 print_one_stat(uint64_t value)
2283 zfs_nicenum(value, buf, sizeof (buf));
2284 (void) printf(" %5s", buf);
2288 * Print out all the statistics for the given vdev. This can either be the
2289 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2290 * is a verbose output, and we don't want to display the toplevel pool stats.
2293 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2294 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2296 nvlist_t **oldchild, **newchild;
2298 vdev_stat_t *oldvs, *newvs;
2299 vdev_stat_t zerovs = { 0 };
2304 if (oldnv != NULL) {
2305 verify(nvlist_lookup_uint64_array(oldnv,
2306 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2311 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2312 (uint64_t **)&newvs, &c) == 0);
2314 if (strlen(name) + depth > cb->cb_namewidth)
2315 (void) printf("%*s%s", depth, "", name);
2317 (void) printf("%*s%s%*s", depth, "", name,
2318 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2320 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2325 scale = (double)NANOSEC / tdelta;
2327 /* only toplevel vdevs have capacity stats */
2328 if (newvs->vs_space == 0) {
2329 (void) printf(" - -");
2331 print_one_stat(newvs->vs_alloc);
2332 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2335 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2336 oldvs->vs_ops[ZIO_TYPE_READ])));
2338 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2339 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2341 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2342 oldvs->vs_bytes[ZIO_TYPE_READ])));
2344 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2345 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2347 (void) printf("\n");
2349 if (!cb->cb_verbose)
2352 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2353 &newchild, &children) != 0)
2356 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2357 &oldchild, &c) != 0)
2360 for (c = 0; c < children; c++) {
2361 uint64_t ishole = B_FALSE, islog = B_FALSE;
2363 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2366 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2369 if (ishole || islog)
2372 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2373 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2374 newchild[c], cb, depth + 2);
2379 * Log device section
2382 if (num_logs(newnv) > 0) {
2383 (void) printf("%-*s - - - - - "
2384 "-\n", cb->cb_namewidth, "logs");
2386 for (c = 0; c < children; c++) {
2387 uint64_t islog = B_FALSE;
2388 (void) nvlist_lookup_uint64(newchild[c],
2389 ZPOOL_CONFIG_IS_LOG, &islog);
2392 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2394 print_vdev_stats(zhp, vname, oldnv ?
2395 oldchild[c] : NULL, newchild[c],
2404 * Include level 2 ARC devices in iostat output
2406 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2407 &newchild, &children) != 0)
2410 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2411 &oldchild, &c) != 0)
2415 (void) printf("%-*s - - - - - "
2416 "-\n", cb->cb_namewidth, "cache");
2417 for (c = 0; c < children; c++) {
2418 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2420 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2421 newchild[c], cb, depth + 2);
2428 refresh_iostat(zpool_handle_t *zhp, void *data)
2430 iostat_cbdata_t *cb = data;
2434 * If the pool has disappeared, remove it from the list and continue.
2436 if (zpool_refresh_stats(zhp, &missing) != 0)
2440 pool_list_remove(cb->cb_list, zhp);
2446 * Callback to print out the iostats for the given pool.
2449 print_iostat(zpool_handle_t *zhp, void *data)
2451 iostat_cbdata_t *cb = data;
2452 nvlist_t *oldconfig, *newconfig;
2453 nvlist_t *oldnvroot, *newnvroot;
2455 newconfig = zpool_get_config(zhp, &oldconfig);
2457 if (cb->cb_iteration == 1)
2460 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2463 if (oldconfig == NULL)
2466 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2470 * Print out the statistics for the pool.
2472 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2475 print_iostat_separator(cb);
2481 get_namewidth(zpool_handle_t *zhp, void *data)
2483 iostat_cbdata_t *cb = data;
2484 nvlist_t *config, *nvroot;
2486 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2487 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2489 if (!cb->cb_verbose)
2490 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2492 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2497 * The width must fall into the range [10,38]. The upper limit is the
2498 * maximum we can have and still fit in 80 columns.
2500 if (cb->cb_namewidth < 10)
2501 cb->cb_namewidth = 10;
2502 if (cb->cb_namewidth > 38)
2503 cb->cb_namewidth = 38;
2509 * Parse the input string, get the 'interval' and 'count' value if there is one.
2512 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2515 unsigned long interval = 0, count = 0;
2516 int argc = *argcp, errno;
2519 * Determine if the last argument is an integer or a pool name
2521 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2525 interval = strtoul(argv[argc - 1], &end, 10);
2527 if (*end == '\0' && errno == 0) {
2528 if (interval == 0) {
2529 (void) fprintf(stderr, gettext("interval "
2530 "cannot be zero\n"));
2534 * Ignore the last parameter
2539 * If this is not a valid number, just plow on. The
2540 * user will get a more informative error message later
2548 * If the last argument is also an integer, then we have both a count
2551 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2556 interval = strtoul(argv[argc - 1], &end, 10);
2558 if (*end == '\0' && errno == 0) {
2559 if (interval == 0) {
2560 (void) fprintf(stderr, gettext("interval "
2561 "cannot be zero\n"));
2566 * Ignore the last parameter
2580 get_timestamp_arg(char c)
2583 timestamp_fmt = UDATE;
2585 timestamp_fmt = DDATE;
2591 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2593 * -v Display statistics for individual vdevs
2594 * -T Display a timestamp in date(1) or Unix format
2596 * This command can be tricky because we want to be able to deal with pool
2597 * creation/destruction as well as vdev configuration changes. The bulk of this
2598 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2599 * on pool_list_update() to detect the addition of new pools. Configuration
2600 * changes are all handled within libzfs.
2603 zpool_do_iostat(int argc, char **argv)
2608 unsigned long interval = 0, count = 0;
2610 boolean_t verbose = B_FALSE;
2614 while ((c = getopt(argc, argv, "T:v")) != -1) {
2617 get_timestamp_arg(*optarg);
2623 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2632 get_interval_count(&argc, argv, &interval, &count);
2635 * Construct the list of all interesting pools.
2638 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2641 if (pool_list_count(list) == 0 && argc != 0) {
2642 pool_list_free(list);
2646 if (pool_list_count(list) == 0 && interval == 0) {
2647 pool_list_free(list);
2648 (void) fprintf(stderr, gettext("no pools available\n"));
2653 * Enter the main iostat loop.
2656 cb.cb_verbose = verbose;
2657 cb.cb_iteration = 0;
2658 cb.cb_namewidth = 0;
2661 pool_list_update(list);
2663 if ((npools = pool_list_count(list)) == 0)
2667 * Refresh all statistics. This is done as an explicit step
2668 * before calculating the maximum name width, so that any
2669 * configuration changes are properly accounted for.
2671 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2674 * Iterate over all pools to determine the maximum width
2675 * for the pool / device name column across all pools.
2677 cb.cb_namewidth = 0;
2678 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2680 if (timestamp_fmt != NODATE)
2681 print_timestamp(timestamp_fmt);
2684 * If it's the first time, or verbose mode, print the header.
2686 if (++cb.cb_iteration == 1 || verbose)
2687 print_iostat_header(&cb);
2689 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2692 * If there's more than one pool, and we're not in verbose mode
2693 * (which prints a separator for us), then print a separator.
2695 if (npools > 1 && !verbose)
2696 print_iostat_separator(&cb);
2699 (void) printf("\n");
2702 * Flush the output so that redirection to a file isn't buffered
2705 (void) fflush(stdout);
2710 if (count != 0 && --count == 0)
2713 (void) sleep(interval);
2716 pool_list_free(list);
2721 typedef struct list_cbdata {
2722 boolean_t cb_verbose;
2724 boolean_t cb_scripted;
2725 zprop_list_t *cb_proplist;
2729 * Given a list of columns to display, output appropriate headers for each one.
2732 print_header(list_cbdata_t *cb)
2734 zprop_list_t *pl = cb->cb_proplist;
2735 char headerbuf[ZPOOL_MAXPROPLEN];
2737 boolean_t first = B_TRUE;
2738 boolean_t right_justify;
2741 for (; pl != NULL; pl = pl->pl_next) {
2742 width = pl->pl_width;
2743 if (first && cb->cb_verbose) {
2745 * Reset the width to accommodate the verbose listing
2748 width = cb->cb_namewidth;
2756 right_justify = B_FALSE;
2757 if (pl->pl_prop != ZPROP_INVAL) {
2758 header = zpool_prop_column_name(pl->pl_prop);
2759 right_justify = zpool_prop_align_right(pl->pl_prop);
2763 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2764 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2765 headerbuf[i] = '\0';
2769 if (pl->pl_next == NULL && !right_justify)
2770 (void) printf("%s", header);
2771 else if (right_justify)
2772 (void) printf("%*s", width, header);
2774 (void) printf("%-*s", width, header);
2778 (void) printf("\n");
2782 * Given a pool and a list of properties, print out all the properties according
2783 * to the described layout.
2786 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2788 zprop_list_t *pl = cb->cb_proplist;
2789 boolean_t first = B_TRUE;
2790 char property[ZPOOL_MAXPROPLEN];
2792 boolean_t right_justify;
2795 for (; pl != NULL; pl = pl->pl_next) {
2797 width = pl->pl_width;
2798 if (first && cb->cb_verbose) {
2800 * Reset the width to accommodate the verbose listing
2803 width = cb->cb_namewidth;
2807 if (cb->cb_scripted)
2808 (void) printf("\t");
2815 right_justify = B_FALSE;
2816 if (pl->pl_prop != ZPROP_INVAL) {
2817 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2818 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2820 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2821 sizeof (property), NULL) != 0)
2826 right_justify = zpool_prop_align_right(pl->pl_prop);
2827 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2828 zpool_prop_unsupported(pl->pl_user_prop)) &&
2829 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2830 sizeof (property)) == 0) {
2838 * If this is being called in scripted mode, or if this is the
2839 * last column and it is left-justified, don't include a width
2842 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2843 (void) printf("%s", propstr);
2844 else if (right_justify)
2845 (void) printf("%*s", width, propstr);
2847 (void) printf("%-*s", width, propstr);
2850 (void) printf("\n");
2854 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2858 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2860 zfs_nicenum(value, propval, sizeof (propval));
2862 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2863 (void) strlcpy(propval, "-", sizeof (propval));
2866 (void) printf("\t%s", propval);
2868 (void) printf(" %*s", width, propval);
2872 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2873 list_cbdata_t *cb, int depth)
2879 boolean_t scripted = cb->cb_scripted;
2881 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2882 (uint64_t **)&vs, &c) == 0);
2886 (void) printf("\t%s", name);
2887 else if (strlen(name) + depth > cb->cb_namewidth)
2888 (void) printf("%*s%s", depth, "", name);
2890 (void) printf("%*s%s%*s", depth, "", name,
2891 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2893 /* only toplevel vdevs have capacity stats */
2894 if (vs->vs_space == 0) {
2896 (void) printf("\t-\t-\t-");
2898 (void) printf(" - - -");
2900 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2902 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2904 print_one_column(ZPOOL_PROP_FREE,
2905 vs->vs_space - vs->vs_alloc, scripted);
2907 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2909 (void) printf("\n");
2912 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2913 &child, &children) != 0)
2916 for (c = 0; c < children; c++) {
2917 uint64_t ishole = B_FALSE;
2919 if (nvlist_lookup_uint64(child[c],
2920 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2923 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2924 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2929 * Include level 2 ARC devices in iostat output
2931 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2932 &child, &children) != 0)
2936 (void) printf("%-*s - - - - - "
2937 "-\n", cb->cb_namewidth, "cache");
2938 for (c = 0; c < children; c++) {
2939 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2941 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2949 * Generic callback function to list a pool.
2952 list_callback(zpool_handle_t *zhp, void *data)
2954 list_cbdata_t *cbp = data;
2958 config = zpool_get_config(zhp, NULL);
2960 print_pool(zhp, cbp);
2961 if (!cbp->cb_verbose)
2964 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2966 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2972 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2974 * -H Scripted mode. Don't display headers, and separate properties
2976 * -o List of properties to display. Defaults to
2977 * "name,size,allocated,free,capacity,health,altroot"
2978 * -T Display a timestamp in date(1) or Unix format
2980 * List all pools in the system, whether or not they're healthy. Output space
2981 * statistics for each one, as well as health status summary.
2984 zpool_do_list(int argc, char **argv)
2988 list_cbdata_t cb = { 0 };
2989 static char default_props[] =
2990 "name,size,allocated,free,capacity,dedupratio,"
2992 char *props = default_props;
2993 unsigned long interval = 0, count = 0;
2995 boolean_t first = B_TRUE;
2998 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3001 cb.cb_scripted = B_TRUE;
3007 get_timestamp_arg(*optarg);
3010 cb.cb_verbose = B_TRUE;
3013 (void) fprintf(stderr, gettext("missing argument for "
3014 "'%c' option\n"), optopt);
3018 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3027 get_interval_count(&argc, argv, &interval, &count);
3029 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3032 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3035 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3036 (void) printf(gettext("no pools available\n"));
3037 zprop_free_list(cb.cb_proplist);
3042 pool_list_update(list);
3044 if (pool_list_count(list) == 0)
3047 cb.cb_namewidth = 0;
3048 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3050 if (timestamp_fmt != NODATE)
3051 print_timestamp(timestamp_fmt);
3053 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3057 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3062 if (count != 0 && --count == 0)
3065 (void) sleep(interval);
3068 zprop_free_list(cb.cb_proplist);
3073 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3080 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3081 &child, &children) != 0) {
3082 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3083 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3084 name += sizeof(_PATH_DEV) - 1;
3085 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3086 path += sizeof(_PATH_DEV) - 1;
3087 if (strcmp(name, path) == 0)
3092 for (c = 0; c < children; c++)
3093 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3100 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3102 boolean_t force = B_FALSE;
3105 char *poolname, *old_disk, *new_disk;
3106 zpool_handle_t *zhp;
3110 while ((c = getopt(argc, argv, "f")) != -1) {
3116 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3125 /* get pool name and check number of arguments */
3127 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3134 (void) fprintf(stderr,
3135 gettext("missing <device> specification\n"));
3143 (void) fprintf(stderr,
3144 gettext("missing <new_device> specification\n"));
3147 new_disk = old_disk;
3157 (void) fprintf(stderr, gettext("too many arguments\n"));
3161 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3164 if (zpool_get_config(zhp, NULL) == NULL) {
3165 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3171 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3173 if (nvroot == NULL) {
3178 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3180 nvlist_free(nvroot);
3187 * zpool replace [-f] <pool> <device> <new_device>
3189 * -f Force attach, even if <new_device> appears to be in use.
3191 * Replace <device> with <new_device>.
3195 zpool_do_replace(int argc, char **argv)
3197 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3201 * zpool attach [-f] <pool> <device> <new_device>
3203 * -f Force attach, even if <new_device> appears to be in use.
3205 * Attach <new_device> to the mirror containing <device>. If <device> is not
3206 * part of a mirror, then <device> will be transformed into a mirror of
3207 * <device> and <new_device>. In either case, <new_device> will begin life
3208 * with a DTL of [0, now], and will immediately begin to resilver itself.
3211 zpool_do_attach(int argc, char **argv)
3213 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3217 * zpool detach [-f] <pool> <device>
3219 * -f Force detach of <device>, even if DTLs argue against it
3220 * (not supported yet)
3222 * Detach a device from a mirror. The operation will be refused if <device>
3223 * is the last device in the mirror, or if the DTLs indicate that this device
3224 * has the only valid copy of some data.
3228 zpool_do_detach(int argc, char **argv)
3231 char *poolname, *path;
3232 zpool_handle_t *zhp;
3236 while ((c = getopt(argc, argv, "f")) != -1) {
3240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3249 /* get pool name and check number of arguments */
3251 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3256 (void) fprintf(stderr,
3257 gettext("missing <device> specification\n"));
3264 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3267 ret = zpool_vdev_detach(zhp, path);
3275 * zpool split [-n] [-o prop=val] ...
3277 * [-R altroot] <pool> <newpool> [<device> ...]
3279 * -n Do not split the pool, but display the resulting layout if
3280 * it were to be split.
3281 * -o Set property=value, or set mount options.
3282 * -R Mount the split-off pool under an alternate root.
3284 * Splits the named pool and gives it the new pool name. Devices to be split
3285 * off may be listed, provided that no more than one device is specified
3286 * per top-level vdev mirror. The newly split pool is left in an exported
3287 * state unless -R is specified.
3289 * Restrictions: the top-level of the pool pool must only be made up of
3290 * mirrors; all devices in the pool must be healthy; no device may be
3291 * undergoing a resilvering operation.
3294 zpool_do_split(int argc, char **argv)
3296 char *srcpool, *newpool, *propval;
3297 char *mntopts = NULL;
3300 zpool_handle_t *zhp;
3301 nvlist_t *config, *props = NULL;
3303 flags.dryrun = B_FALSE;
3304 flags.import = B_FALSE;
3307 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3310 flags.import = B_TRUE;
3312 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3313 &props, B_TRUE) != 0) {
3320 flags.dryrun = B_TRUE;
3323 if ((propval = strchr(optarg, '=')) != NULL) {
3326 if (add_prop_list(optarg, propval,
3327 &props, B_TRUE) != 0) {
3337 (void) fprintf(stderr, gettext("missing argument for "
3338 "'%c' option\n"), optopt);
3342 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3349 if (!flags.import && mntopts != NULL) {
3350 (void) fprintf(stderr, gettext("setting mntopts is only "
3351 "valid when importing the pool\n"));
3359 (void) fprintf(stderr, gettext("Missing pool name\n"));
3363 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3373 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3376 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3377 if (config == NULL) {
3381 (void) printf(gettext("would create '%s' with the "
3382 "following layout:\n\n"), newpool);
3383 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3385 nvlist_free(config);
3390 if (ret != 0 || flags.dryrun || !flags.import)
3394 * The split was successful. Now we need to open the new
3395 * pool and import it.
3397 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3399 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3400 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3402 (void) fprintf(stderr, gettext("Split was successful, but "
3403 "the datasets could not all be mounted\n"));
3404 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3405 "different altroot\n"), "zpool import");
3415 * zpool online <pool> <device> ...
3418 zpool_do_online(int argc, char **argv)
3422 zpool_handle_t *zhp;
3424 vdev_state_t newstate;
3428 while ((c = getopt(argc, argv, "et")) != -1) {
3431 flags |= ZFS_ONLINE_EXPAND;
3435 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3444 /* get pool name and check number of arguments */
3446 (void) fprintf(stderr, gettext("missing pool name\n"));
3450 (void) fprintf(stderr, gettext("missing device name\n"));
3456 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3459 for (i = 1; i < argc; i++) {
3460 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3461 if (newstate != VDEV_STATE_HEALTHY) {
3462 (void) printf(gettext("warning: device '%s' "
3463 "onlined, but remains in faulted state\n"),
3465 if (newstate == VDEV_STATE_FAULTED)
3466 (void) printf(gettext("use 'zpool "
3467 "clear' to restore a faulted "
3470 (void) printf(gettext("use 'zpool "
3471 "replace' to replace devices "
3472 "that are no longer present\n"));
3485 * zpool offline [-ft] <pool> <device> ...
3487 * -f Force the device into the offline state, even if doing
3488 * so would appear to compromise pool availability.
3489 * (not supported yet)
3491 * -t Only take the device off-line temporarily. The offline
3492 * state will not be persistent across reboots.
3496 zpool_do_offline(int argc, char **argv)
3500 zpool_handle_t *zhp;
3502 boolean_t istmp = B_FALSE;
3505 while ((c = getopt(argc, argv, "ft")) != -1) {
3512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3521 /* get pool name and check number of arguments */
3523 (void) fprintf(stderr, gettext("missing pool name\n"));
3527 (void) fprintf(stderr, gettext("missing device name\n"));
3533 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3536 for (i = 1; i < argc; i++) {
3537 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3547 * zpool clear <pool> [device]
3549 * Clear all errors associated with a pool or a particular device.
3552 zpool_do_clear(int argc, char **argv)
3556 boolean_t dryrun = B_FALSE;
3557 boolean_t do_rewind = B_FALSE;
3558 boolean_t xtreme_rewind = B_FALSE;
3559 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3560 nvlist_t *policy = NULL;
3561 zpool_handle_t *zhp;
3562 char *pool, *device;
3565 while ((c = getopt(argc, argv, "FnX")) != -1) {
3574 xtreme_rewind = B_TRUE;
3577 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3587 (void) fprintf(stderr, gettext("missing pool name\n"));
3592 (void) fprintf(stderr, gettext("too many arguments\n"));
3596 if ((dryrun || xtreme_rewind) && !do_rewind) {
3597 (void) fprintf(stderr,
3598 gettext("-n or -X only meaningful with -F\n"));
3602 rewind_policy = ZPOOL_TRY_REWIND;
3604 rewind_policy = ZPOOL_DO_REWIND;
3606 rewind_policy |= ZPOOL_EXTREME_REWIND;
3608 /* In future, further rewind policy choices can be passed along here */
3609 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3610 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3614 device = argc == 2 ? argv[1] : NULL;
3616 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3617 nvlist_free(policy);
3621 if (zpool_clear(zhp, device, policy) != 0)
3626 nvlist_free(policy);
3632 * zpool reguid <pool>
3635 zpool_do_reguid(int argc, char **argv)
3639 zpool_handle_t *zhp;
3643 while ((c = getopt(argc, argv, "")) != -1) {
3646 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3655 /* get pool name and check number of arguments */
3657 (void) fprintf(stderr, gettext("missing pool name\n"));
3662 (void) fprintf(stderr, gettext("too many arguments\n"));
3667 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3670 ret = zpool_reguid(zhp);
3678 * zpool reopen <pool>
3680 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3682 * NOTE: This command is currently undocumented. If the command is ever
3683 * exposed then the appropriate usage() messages will need to be made.
3686 zpool_do_reopen(int argc, char **argv)
3689 zpool_handle_t *zhp;
3699 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3702 ret = zpool_reopen(zhp);
3707 typedef struct scrub_cbdata {
3714 scrub_callback(zpool_handle_t *zhp, void *data)
3716 scrub_cbdata_t *cb = data;
3720 * Ignore faulted pools.
3722 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3723 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3724 "currently unavailable\n"), zpool_get_name(zhp));
3728 err = zpool_scan(zhp, cb->cb_type);
3734 * zpool scrub [-s] <pool> ...
3736 * -s Stop. Stops any in-progress scrub.
3739 zpool_do_scrub(int argc, char **argv)
3744 cb.cb_type = POOL_SCAN_SCRUB;
3747 while ((c = getopt(argc, argv, "s")) != -1) {
3750 cb.cb_type = POOL_SCAN_NONE;
3753 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3765 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3769 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3772 typedef struct status_cbdata {
3774 boolean_t cb_allpools;
3775 boolean_t cb_verbose;
3776 boolean_t cb_explain;
3778 boolean_t cb_dedup_stats;
3782 * Print out detailed scrub status.
3785 print_scan_status(pool_scan_stat_t *ps)
3788 uint64_t elapsed, mins_left, hours_left;
3789 uint64_t pass_exam, examined, total;
3791 double fraction_done;
3792 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3794 (void) printf(gettext(" scan: "));
3796 /* If there's never been a scan, there's not much to say. */
3797 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3798 ps->pss_func >= POOL_SCAN_FUNCS) {
3799 (void) printf(gettext("none requested\n"));
3803 start = ps->pss_start_time;
3804 end = ps->pss_end_time;
3805 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3807 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3808 ps->pss_func == POOL_SCAN_RESILVER);
3810 * Scan is finished or canceled.
3812 if (ps->pss_state == DSS_FINISHED) {
3813 uint64_t minutes_taken = (end - start) / 60;
3816 if (ps->pss_func == POOL_SCAN_SCRUB) {
3817 fmt = gettext("scrub repaired %s in %lluh%um with "
3818 "%llu errors on %s");
3819 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3820 fmt = gettext("resilvered %s in %lluh%um with "
3821 "%llu errors on %s");
3824 (void) printf(fmt, processed_buf,
3825 (u_longlong_t)(minutes_taken / 60),
3826 (uint_t)(minutes_taken % 60),
3827 (u_longlong_t)ps->pss_errors,
3828 ctime((time_t *)&end));
3830 } else if (ps->pss_state == DSS_CANCELED) {
3831 if (ps->pss_func == POOL_SCAN_SCRUB) {
3832 (void) printf(gettext("scrub canceled on %s"),
3834 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3835 (void) printf(gettext("resilver canceled on %s"),
3841 assert(ps->pss_state == DSS_SCANNING);
3844 * Scan is in progress.
3846 if (ps->pss_func == POOL_SCAN_SCRUB) {
3847 (void) printf(gettext("scrub in progress since %s"),
3849 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3850 (void) printf(gettext("resilver in progress since %s"),
3854 examined = ps->pss_examined ? ps->pss_examined : 1;
3855 total = ps->pss_to_examine;
3856 fraction_done = (double)examined / total;
3858 /* elapsed time for this pass */
3859 elapsed = time(NULL) - ps->pss_pass_start;
3860 elapsed = elapsed ? elapsed : 1;
3861 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3862 rate = pass_exam / elapsed;
3863 rate = rate ? rate : 1;
3864 mins_left = ((total - examined) / rate) / 60;
3865 hours_left = mins_left / 60;
3867 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3868 zfs_nicenum(total, total_buf, sizeof (total_buf));
3869 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3872 * do not print estimated time if hours_left is more than 30 days
3874 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3875 examined_buf, total_buf, rate_buf);
3876 if (hours_left < (30 * 24)) {
3877 (void) printf(gettext(", %lluh%um to go\n"),
3878 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3880 (void) printf(gettext(
3881 ", (scan is slow, no estimated time)\n"));
3884 if (ps->pss_func == POOL_SCAN_RESILVER) {
3885 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3886 processed_buf, 100 * fraction_done);
3887 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3888 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3889 processed_buf, 100 * fraction_done);
3894 print_error_log(zpool_handle_t *zhp)
3896 nvlist_t *nverrlist = NULL;
3899 size_t len = MAXPATHLEN * 2;
3901 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3902 (void) printf("errors: List of errors unavailable "
3903 "(insufficient privileges)\n");
3907 (void) printf("errors: Permanent errors have been "
3908 "detected in the following files:\n\n");
3910 pathname = safe_malloc(len);
3912 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3914 uint64_t dsobj, obj;
3916 verify(nvpair_value_nvlist(elem, &nv) == 0);
3917 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3919 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3921 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3922 (void) printf("%7s %s\n", "", pathname);
3925 nvlist_free(nverrlist);
3929 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3938 (void) printf(gettext("\tspares\n"));
3940 for (i = 0; i < nspares; i++) {
3941 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3942 print_status_config(zhp, name, spares[i],
3943 namewidth, 2, B_TRUE);
3949 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3958 (void) printf(gettext("\tcache\n"));
3960 for (i = 0; i < nl2cache; i++) {
3961 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3962 print_status_config(zhp, name, l2cache[i],
3963 namewidth, 2, B_FALSE);
3969 print_dedup_stats(nvlist_t *config)
3971 ddt_histogram_t *ddh;
3977 * If the pool was faulted then we may not have been able to
3978 * obtain the config. Otherwise, if have anything in the dedup
3979 * table continue processing the stats.
3981 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3982 (uint64_t **)&ddo, &c) != 0)
3985 (void) printf("\n");
3986 (void) printf(gettext(" dedup: "));
3987 if (ddo->ddo_count == 0) {
3988 (void) printf(gettext("no DDT entries\n"));
3992 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3993 (u_longlong_t)ddo->ddo_count,
3994 (u_longlong_t)ddo->ddo_dspace,
3995 (u_longlong_t)ddo->ddo_mspace);
3997 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3998 (uint64_t **)&dds, &c) == 0);
3999 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4000 (uint64_t **)&ddh, &c) == 0);
4001 zpool_dump_ddt(dds, ddh);
4005 * Display a summary of pool status. Displays a summary such as:
4009 * reason: One or more devices ...
4010 * see: http://illumos.org/msg/ZFS-xxxx-01
4016 * When given the '-v' option, we print out the complete config. If the '-e'
4017 * option is specified, then we print out error rate information as well.
4020 status_callback(zpool_handle_t *zhp, void *data)
4022 status_cbdata_t *cbp = data;
4023 nvlist_t *config, *nvroot;
4030 config = zpool_get_config(zhp, NULL);
4031 reason = zpool_get_status(zhp, &msgid);
4036 * If we were given 'zpool status -x', only report those pools with
4039 if (cbp->cb_explain &&
4040 (reason == ZPOOL_STATUS_OK ||
4041 reason == ZPOOL_STATUS_VERSION_OLDER ||
4042 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4043 if (!cbp->cb_allpools) {
4044 (void) printf(gettext("pool '%s' is healthy\n"),
4045 zpool_get_name(zhp));
4047 cbp->cb_first = B_FALSE;
4053 cbp->cb_first = B_FALSE;
4055 (void) printf("\n");
4057 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4059 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4060 (uint64_t **)&vs, &c) == 0);
4061 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4063 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4064 (void) printf(gettext(" state: %s\n"), health);
4067 case ZPOOL_STATUS_MISSING_DEV_R:
4068 (void) printf(gettext("status: One or more devices could not "
4069 "be opened. Sufficient replicas exist for\n\tthe pool to "
4070 "continue functioning in a degraded state.\n"));
4071 (void) printf(gettext("action: Attach the missing device and "
4072 "online it using 'zpool online'.\n"));
4075 case ZPOOL_STATUS_MISSING_DEV_NR:
4076 (void) printf(gettext("status: One or more devices could not "
4077 "be opened. There are insufficient\n\treplicas for the "
4078 "pool to continue functioning.\n"));
4079 (void) printf(gettext("action: Attach the missing device and "
4080 "online it using 'zpool online'.\n"));
4083 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4084 (void) printf(gettext("status: One or more devices could not "
4085 "be used because the label is missing or\n\tinvalid. "
4086 "Sufficient replicas exist for the pool to continue\n\t"
4087 "functioning in a degraded state.\n"));
4088 (void) printf(gettext("action: Replace the device using "
4089 "'zpool replace'.\n"));
4092 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4093 (void) printf(gettext("status: One or more devices could not "
4094 "be used because the label is missing \n\tor invalid. "
4095 "There are insufficient replicas for the pool to "
4096 "continue\n\tfunctioning.\n"));
4097 zpool_explain_recover(zpool_get_handle(zhp),
4098 zpool_get_name(zhp), reason, config);
4101 case ZPOOL_STATUS_FAILING_DEV:
4102 (void) printf(gettext("status: One or more devices has "
4103 "experienced an unrecoverable error. An\n\tattempt was "
4104 "made to correct the error. Applications are "
4106 (void) printf(gettext("action: Determine if the device needs "
4107 "to be replaced, and clear the errors\n\tusing "
4108 "'zpool clear' or replace the device with 'zpool "
4112 case ZPOOL_STATUS_OFFLINE_DEV:
4113 (void) printf(gettext("status: One or more devices has "
4114 "been taken offline by the administrator.\n\tSufficient "
4115 "replicas exist for the pool to continue functioning in "
4116 "a\n\tdegraded state.\n"));
4117 (void) printf(gettext("action: Online the device using "
4118 "'zpool online' or replace the device with\n\t'zpool "
4122 case ZPOOL_STATUS_REMOVED_DEV:
4123 (void) printf(gettext("status: One or more devices has "
4124 "been removed by the administrator.\n\tSufficient "
4125 "replicas exist for the pool to continue functioning in "
4126 "a\n\tdegraded state.\n"));
4127 (void) printf(gettext("action: Online the device using "
4128 "'zpool online' or replace the device with\n\t'zpool "
4132 case ZPOOL_STATUS_RESILVERING:
4133 (void) printf(gettext("status: One or more devices is "
4134 "currently being resilvered. The pool will\n\tcontinue "
4135 "to function, possibly in a degraded state.\n"));
4136 (void) printf(gettext("action: Wait for the resilver to "
4140 case ZPOOL_STATUS_CORRUPT_DATA:
4141 (void) printf(gettext("status: One or more devices has "
4142 "experienced an error resulting in data\n\tcorruption. "
4143 "Applications may be affected.\n"));
4144 (void) printf(gettext("action: Restore the file in question "
4145 "if possible. Otherwise restore the\n\tentire pool from "
4149 case ZPOOL_STATUS_CORRUPT_POOL:
4150 (void) printf(gettext("status: The pool metadata is corrupted "
4151 "and the pool cannot be opened.\n"));
4152 zpool_explain_recover(zpool_get_handle(zhp),
4153 zpool_get_name(zhp), reason, config);
4156 case ZPOOL_STATUS_VERSION_OLDER:
4157 (void) printf(gettext("status: The pool is formatted using a "
4158 "legacy on-disk format. The pool can\n\tstill be used, "
4159 "but some features are unavailable.\n"));
4160 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4161 "upgrade'. Once this is done, the\n\tpool will no longer "
4162 "be accessible on software that does not support feature\n"
4166 case ZPOOL_STATUS_VERSION_NEWER:
4167 (void) printf(gettext("status: The pool has been upgraded to a "
4168 "newer, incompatible on-disk version.\n\tThe pool cannot "
4169 "be accessed on this system.\n"));
4170 (void) printf(gettext("action: Access the pool from a system "
4171 "running more recent software, or\n\trestore the pool from "
4175 case ZPOOL_STATUS_FEAT_DISABLED:
4176 (void) printf(gettext("status: Some supported features are not "
4177 "enabled on the pool. The pool can\n\tstill be used, but "
4178 "some features are unavailable.\n"));
4179 (void) printf(gettext("action: Enable all features using "
4180 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4181 "longer be accessible by software that does not support\n\t"
4182 "the features. See zpool-features(7) for details.\n"));
4185 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4186 (void) printf(gettext("status: The pool cannot be accessed on "
4187 "this system because it uses the\n\tfollowing feature(s) "
4188 "not supported on this system:\n"));
4189 zpool_print_unsup_feat(config);
4190 (void) printf("\n");
4191 (void) printf(gettext("action: Access the pool from a system "
4192 "that supports the required feature(s),\n\tor restore the "
4193 "pool from backup.\n"));
4196 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4197 (void) printf(gettext("status: The pool can only be accessed "
4198 "in read-only mode on this system. It\n\tcannot be "
4199 "accessed in read-write mode because it uses the "
4200 "following\n\tfeature(s) not supported on this system:\n"));
4201 zpool_print_unsup_feat(config);
4202 (void) printf("\n");
4203 (void) printf(gettext("action: The pool cannot be accessed in "
4204 "read-write mode. Import the pool with\n"
4205 "\t\"-o readonly=on\", access the pool from a system that "
4206 "supports the\n\trequired feature(s), or restore the "
4207 "pool from backup.\n"));
4210 case ZPOOL_STATUS_FAULTED_DEV_R:
4211 (void) printf(gettext("status: One or more devices are "
4212 "faulted in response to persistent errors.\n\tSufficient "
4213 "replicas exist for the pool to continue functioning "
4214 "in a\n\tdegraded state.\n"));
4215 (void) printf(gettext("action: Replace the faulted device, "
4216 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4219 case ZPOOL_STATUS_FAULTED_DEV_NR:
4220 (void) printf(gettext("status: One or more devices are "
4221 "faulted in response to persistent errors. There are "
4222 "insufficient replicas for the pool to\n\tcontinue "
4224 (void) printf(gettext("action: Destroy and re-create the pool "
4225 "from a backup source. Manually marking the device\n"
4226 "\trepaired using 'zpool clear' may allow some data "
4227 "to be recovered.\n"));
4230 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4231 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4232 (void) printf(gettext("status: One or more devices are "
4233 "faulted in response to IO failures.\n"));
4234 (void) printf(gettext("action: Make sure the affected devices "
4235 "are connected, then run 'zpool clear'.\n"));
4238 case ZPOOL_STATUS_BAD_LOG:
4239 (void) printf(gettext("status: An intent log record "
4240 "could not be read.\n"
4241 "\tWaiting for adminstrator intervention to fix the "
4242 "faulted pool.\n"));
4243 (void) printf(gettext("action: Either restore the affected "
4244 "device(s) and run 'zpool online',\n"
4245 "\tor ignore the intent log records by running "
4246 "'zpool clear'.\n"));
4251 * The remaining errors can't actually be generated, yet.
4253 assert(reason == ZPOOL_STATUS_OK);
4257 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4260 if (config != NULL) {
4263 nvlist_t **spares, **l2cache;
4264 uint_t nspares, nl2cache;
4265 pool_scan_stat_t *ps = NULL;
4267 (void) nvlist_lookup_uint64_array(nvroot,
4268 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4269 print_scan_status(ps);
4271 namewidth = max_width(zhp, nvroot, 0, 0);
4275 (void) printf(gettext("config:\n\n"));
4276 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4277 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4278 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4279 namewidth, 0, B_FALSE);
4281 if (num_logs(nvroot) > 0)
4282 print_logs(zhp, nvroot, namewidth, B_TRUE);
4283 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4284 &l2cache, &nl2cache) == 0)
4285 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4287 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4288 &spares, &nspares) == 0)
4289 print_spares(zhp, spares, nspares, namewidth);
4291 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4293 nvlist_t *nverrlist = NULL;
4296 * If the approximate error count is small, get a
4297 * precise count by fetching the entire log and
4298 * uniquifying the results.
4300 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4301 zpool_get_errlog(zhp, &nverrlist) == 0) {
4306 while ((elem = nvlist_next_nvpair(nverrlist,
4311 nvlist_free(nverrlist);
4313 (void) printf("\n");
4316 (void) printf(gettext("errors: No known data "
4318 else if (!cbp->cb_verbose)
4319 (void) printf(gettext("errors: %llu data "
4320 "errors, use '-v' for a list\n"),
4321 (u_longlong_t)nerr);
4323 print_error_log(zhp);
4326 if (cbp->cb_dedup_stats)
4327 print_dedup_stats(config);
4329 (void) printf(gettext("config: The configuration cannot be "
4337 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4339 * -v Display complete error logs
4340 * -x Display only pools with potential problems
4341 * -D Display dedup status (undocumented)
4342 * -T Display a timestamp in date(1) or Unix format
4344 * Describes the health status of all pools or some subset.
4347 zpool_do_status(int argc, char **argv)
4351 unsigned long interval = 0, count = 0;
4352 status_cbdata_t cb = { 0 };
4355 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4358 cb.cb_verbose = B_TRUE;
4361 cb.cb_explain = B_TRUE;
4364 cb.cb_dedup_stats = B_TRUE;
4367 get_timestamp_arg(*optarg);
4370 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4379 get_interval_count(&argc, argv, &interval, &count);
4382 cb.cb_allpools = B_TRUE;
4384 cb.cb_first = B_TRUE;
4387 if (timestamp_fmt != NODATE)
4388 print_timestamp(timestamp_fmt);
4390 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4391 status_callback, &cb);
4393 if (argc == 0 && cb.cb_count == 0)
4394 (void) printf(gettext("no pools available\n"));
4395 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4396 (void) printf(gettext("all pools are healthy\n"));
4404 if (count != 0 && --count == 0)
4407 (void) sleep(interval);
4413 typedef struct upgrade_cbdata {
4415 char cb_poolname[ZPOOL_MAXNAMELEN];
4417 uint64_t cb_version;
4423 is_root_pool(zpool_handle_t *zhp)
4425 static struct statfs sfs;
4426 static char *poolname = NULL;
4427 static boolean_t stated = B_FALSE;
4432 if (statfs("/", &sfs) == -1) {
4433 (void) fprintf(stderr,
4434 "Unable to stat root file system: %s.\n",
4438 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4440 poolname = sfs.f_mntfromname;
4441 if ((slash = strchr(poolname, '/')) != NULL)
4444 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4448 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4450 if (poolname[0] == '\0' && is_root_pool(zhp))
4451 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4453 #endif /* FreeBSD */
4456 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4460 uint64_t oldversion;
4462 config = zpool_get_config(zhp, NULL);
4463 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4466 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4467 assert(oldversion < version);
4469 ret = zpool_upgrade(zhp, version);
4473 if (version >= SPA_VERSION_FEATURES) {
4474 (void) printf(gettext("Successfully upgraded "
4475 "'%s' from version %llu to feature flags.\n"),
4476 zpool_get_name(zhp), oldversion);
4478 (void) printf(gettext("Successfully upgraded "
4479 "'%s' from version %llu to version %llu.\n"),
4480 zpool_get_name(zhp), oldversion, version);
4487 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4490 boolean_t firstff = B_TRUE;
4491 nvlist_t *enabled = zpool_get_features(zhp);
4494 for (i = 0; i < SPA_FEATURES; i++) {
4495 const char *fname = spa_feature_table[i].fi_uname;
4496 const char *fguid = spa_feature_table[i].fi_guid;
4497 if (!nvlist_exists(enabled, fguid)) {
4499 verify(-1 != asprintf(&propname, "feature@%s", fname));
4500 ret = zpool_set_prop(zhp, propname,
4501 ZFS_FEATURE_ENABLED);
4509 (void) printf(gettext("Enabled the "
4510 "following features on '%s':\n"),
4511 zpool_get_name(zhp));
4514 (void) printf(gettext(" %s\n"), fname);
4525 upgrade_cb(zpool_handle_t *zhp, void *arg)
4527 upgrade_cbdata_t *cbp = arg;
4530 boolean_t printnl = B_FALSE;
4533 config = zpool_get_config(zhp, NULL);
4534 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4537 assert(SPA_VERSION_IS_SUPPORTED(version));
4539 if (version < cbp->cb_version) {
4540 cbp->cb_first = B_FALSE;
4541 ret = upgrade_version(zhp, cbp->cb_version);
4545 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4546 sizeof(cbp->cb_poolname));
4547 #endif /* ___FreeBSD__ */
4552 * If they did "zpool upgrade -a", then we could
4553 * be doing ioctls to different pools. We need
4554 * to log this history once to each pool, and bypass
4555 * the normal history logging that happens in main().
4557 (void) zpool_log_history(g_zfs, history_str);
4558 log_history = B_FALSE;
4562 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4564 ret = upgrade_enable_all(zhp, &count);
4569 cbp->cb_first = B_FALSE;
4572 * If they did "zpool upgrade -a", then we could
4573 * be doing ioctls to different pools. We need
4574 * to log this history once to each pool, and bypass
4575 * the normal history logging that happens in main().
4577 (void) zpool_log_history(g_zfs, history_str);
4578 log_history = B_FALSE;
4583 (void) printf(gettext("\n"));
4590 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4592 upgrade_cbdata_t *cbp = arg;
4596 config = zpool_get_config(zhp, NULL);
4597 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4600 assert(SPA_VERSION_IS_SUPPORTED(version));
4602 if (version < SPA_VERSION_FEATURES) {
4603 if (cbp->cb_first) {
4604 (void) printf(gettext("The following pools are "
4605 "formatted with legacy version numbers and can\n"
4606 "be upgraded to use feature flags. After "
4607 "being upgraded, these pools\nwill no "
4608 "longer be accessible by software that does not "
4609 "support feature\nflags.\n\n"));
4610 (void) printf(gettext("VER POOL\n"));
4611 (void) printf(gettext("--- ------------\n"));
4612 cbp->cb_first = B_FALSE;
4615 (void) printf("%2llu %s\n", (u_longlong_t)version,
4616 zpool_get_name(zhp));
4623 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4625 upgrade_cbdata_t *cbp = arg;
4629 config = zpool_get_config(zhp, NULL);
4630 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4633 if (version >= SPA_VERSION_FEATURES) {
4635 boolean_t poolfirst = B_TRUE;
4636 nvlist_t *enabled = zpool_get_features(zhp);
4638 for (i = 0; i < SPA_FEATURES; i++) {
4639 const char *fguid = spa_feature_table[i].fi_guid;
4640 const char *fname = spa_feature_table[i].fi_uname;
4641 if (!nvlist_exists(enabled, fguid)) {
4642 if (cbp->cb_first) {
4643 (void) printf(gettext("\nSome "
4644 "supported features are not "
4645 "enabled on the following pools. "
4646 "Once a\nfeature is enabled the "
4647 "pool may become incompatible with "
4648 "software\nthat does not support "
4650 "zpool-features(7) for "
4652 (void) printf(gettext("POOL "
4654 (void) printf(gettext("------"
4656 cbp->cb_first = B_FALSE;
4660 (void) printf(gettext("%s\n"),
4661 zpool_get_name(zhp));
4662 poolfirst = B_FALSE;
4665 (void) printf(gettext(" %s\n"), fname);
4675 upgrade_one(zpool_handle_t *zhp, void *data)
4677 boolean_t printnl = B_FALSE;
4678 upgrade_cbdata_t *cbp = data;
4679 uint64_t cur_version;
4682 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4683 (void) printf(gettext("'log' is now a reserved word\n"
4684 "Pool 'log' must be renamed using export and import"
4689 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4690 if (cur_version > cbp->cb_version) {
4691 (void) printf(gettext("Pool '%s' is already formatted "
4692 "using more current version '%llu'.\n\n"),
4693 zpool_get_name(zhp), cur_version);
4697 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4698 (void) printf(gettext("Pool '%s' is already formatted "
4699 "using version %llu.\n\n"), zpool_get_name(zhp),
4704 if (cur_version != cbp->cb_version) {
4706 ret = upgrade_version(zhp, cbp->cb_version);
4710 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4711 sizeof(cbp->cb_poolname));
4712 #endif /* ___FreeBSD__ */
4715 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4717 ret = upgrade_enable_all(zhp, &count);
4724 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4725 sizeof(cbp->cb_poolname));
4726 #endif /* __FreeBSD __*/
4727 } else if (cur_version == SPA_VERSION) {
4728 (void) printf(gettext("Pool '%s' already has all "
4729 "supported features enabled.\n"),
4730 zpool_get_name(zhp));
4735 (void) printf(gettext("\n"));
4744 * zpool upgrade [-V version] <-a | pool ...>
4746 * With no arguments, display downrev'd ZFS pool available for upgrade.
4747 * Individual pools can be upgraded by specifying the pool, and '-a' will
4748 * upgrade all pools.
4751 zpool_do_upgrade(int argc, char **argv)
4754 upgrade_cbdata_t cb = { 0 };
4756 boolean_t showversions = B_FALSE;
4757 boolean_t upgradeall = B_FALSE;
4762 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4765 upgradeall = B_TRUE;
4768 showversions = B_TRUE;
4771 cb.cb_version = strtoll(optarg, &end, 10);
4773 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4774 (void) fprintf(stderr,
4775 gettext("invalid version '%s'\n"), optarg);
4780 (void) fprintf(stderr, gettext("missing argument for "
4781 "'%c' option\n"), optopt);
4785 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4796 if (cb.cb_version == 0) {
4797 cb.cb_version = SPA_VERSION;
4798 } else if (!upgradeall && argc == 0) {
4799 (void) fprintf(stderr, gettext("-V option is "
4800 "incompatible with other arguments\n"));
4805 if (upgradeall || argc != 0) {
4806 (void) fprintf(stderr, gettext("-v option is "
4807 "incompatible with other arguments\n"));
4810 } else if (upgradeall) {
4812 (void) fprintf(stderr, gettext("-a option should not "
4813 "be used along with a pool name\n"));
4818 (void) printf(gettext("This system supports ZFS pool feature "
4823 (void) printf(gettext("The following features are "
4825 (void) printf(gettext("FEAT DESCRIPTION\n"));
4826 (void) printf("----------------------------------------------"
4827 "---------------\n");
4828 for (i = 0; i < SPA_FEATURES; i++) {
4829 zfeature_info_t *fi = &spa_feature_table[i];
4830 const char *ro = fi->fi_can_readonly ?
4831 " (read-only compatible)" : "";
4833 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4834 (void) printf(" %s\n", fi->fi_desc);
4836 (void) printf("\n");
4838 (void) printf(gettext("The following legacy versions are also "
4840 (void) printf(gettext("VER DESCRIPTION\n"));
4841 (void) printf("--- -----------------------------------------"
4842 "---------------\n");
4843 (void) printf(gettext(" 1 Initial ZFS version\n"));
4844 (void) printf(gettext(" 2 Ditto blocks "
4845 "(replicated metadata)\n"));
4846 (void) printf(gettext(" 3 Hot spares and double parity "
4848 (void) printf(gettext(" 4 zpool history\n"));
4849 (void) printf(gettext(" 5 Compression using the gzip "
4851 (void) printf(gettext(" 6 bootfs pool property\n"));
4852 (void) printf(gettext(" 7 Separate intent log devices\n"));
4853 (void) printf(gettext(" 8 Delegated administration\n"));
4854 (void) printf(gettext(" 9 refquota and refreservation "
4856 (void) printf(gettext(" 10 Cache devices\n"));
4857 (void) printf(gettext(" 11 Improved scrub performance\n"));
4858 (void) printf(gettext(" 12 Snapshot properties\n"));
4859 (void) printf(gettext(" 13 snapused property\n"));
4860 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4861 (void) printf(gettext(" 15 user/group space accounting\n"));
4862 (void) printf(gettext(" 16 stmf property support\n"));
4863 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4864 (void) printf(gettext(" 18 Snapshot user holds\n"));
4865 (void) printf(gettext(" 19 Log device removal\n"));
4866 (void) printf(gettext(" 20 Compression using zle "
4867 "(zero-length encoding)\n"));
4868 (void) printf(gettext(" 21 Deduplication\n"));
4869 (void) printf(gettext(" 22 Received properties\n"));
4870 (void) printf(gettext(" 23 Slim ZIL\n"));
4871 (void) printf(gettext(" 24 System attributes\n"));
4872 (void) printf(gettext(" 25 Improved scrub stats\n"));
4873 (void) printf(gettext(" 26 Improved snapshot deletion "
4875 (void) printf(gettext(" 27 Improved snapshot creation "
4877 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4878 (void) printf(gettext("\nFor more information on a particular "
4879 "version, including supported releases,\n"));
4880 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4881 } else if (argc == 0 && upgradeall) {
4882 cb.cb_first = B_TRUE;
4883 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4884 if (ret == 0 && cb.cb_first) {
4885 if (cb.cb_version == SPA_VERSION) {
4886 (void) printf(gettext("All pools are already "
4887 "formatted using feature flags.\n\n"));
4888 (void) printf(gettext("Every feature flags "
4889 "pool already has all supported features "
4892 (void) printf(gettext("All pools are already "
4893 "formatted with version %llu or higher.\n"),
4897 } else if (argc == 0) {
4898 cb.cb_first = B_TRUE;
4899 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4903 (void) printf(gettext("All pools are formatted "
4904 "using feature flags.\n\n"));
4906 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4907 "for a list of available legacy versions.\n"));
4910 cb.cb_first = B_TRUE;
4911 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4915 (void) printf(gettext("Every feature flags pool has "
4916 "all supported features enabled.\n"));
4918 (void) printf(gettext("\n"));
4921 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4925 if (cb.cb_poolname[0] != '\0') {
4927 "If you boot from pool '%s', don't forget to update boot code.\n"
4928 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4929 "the following command will do it:\n"
4931 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4938 typedef struct hist_cbdata {
4945 * Print out the command history for a specific pool.
4948 get_history_one(zpool_handle_t *zhp, void *data)
4954 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4956 cb->first = B_FALSE;
4958 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4960 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4963 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4964 &records, &numrecords) == 0);
4965 for (i = 0; i < numrecords; i++) {
4966 nvlist_t *rec = records[i];
4969 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
4973 tsec = fnvlist_lookup_uint64(records[i],
4975 (void) localtime_r(&tsec, &t);
4976 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4979 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
4980 (void) printf("%s %s", tbuf,
4981 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
4982 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
4984 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
4987 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
4988 (void) printf("%s unrecognized record:\n",
4990 dump_nvlist(rec, 4);
4993 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
4994 zfs_history_event_names[ievent],
4995 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
4996 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
4997 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5000 (void) printf("%s [txg:%lld] %s", tbuf,
5001 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5002 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5003 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5004 (void) printf(" %s (%llu)",
5005 fnvlist_lookup_string(rec,
5007 fnvlist_lookup_uint64(rec,
5010 (void) printf(" %s", fnvlist_lookup_string(rec,
5011 ZPOOL_HIST_INT_STR));
5012 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5015 (void) printf("%s ioctl %s\n", tbuf,
5016 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5017 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5018 (void) printf(" input:\n");
5019 dump_nvlist(fnvlist_lookup_nvlist(rec,
5020 ZPOOL_HIST_INPUT_NVL), 8);
5022 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5023 (void) printf(" output:\n");
5024 dump_nvlist(fnvlist_lookup_nvlist(rec,
5025 ZPOOL_HIST_OUTPUT_NVL), 8);
5030 (void) printf("%s unrecognized record:\n", tbuf);
5031 dump_nvlist(rec, 4);
5035 (void) printf("\n");
5038 (void) printf(" [");
5039 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5040 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5041 struct passwd *pwd = getpwuid(who);
5042 (void) printf("user %d ", (int)who);
5044 (void) printf("(%s) ", pwd->pw_name);
5046 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5047 (void) printf("on %s",
5048 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5050 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5051 (void) printf(":%s",
5052 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5055 (void) printf("\n");
5057 (void) printf("\n");
5064 * zpool history <pool>
5066 * Displays the history of commands that modified pools.
5069 zpool_do_history(int argc, char **argv)
5071 hist_cbdata_t cbdata = { 0 };
5075 cbdata.first = B_TRUE;
5077 while ((c = getopt(argc, argv, "li")) != -1) {
5080 cbdata.longfmt = B_TRUE;
5083 cbdata.internal = B_TRUE;
5086 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5094 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5097 if (argc == 0 && cbdata.first == B_TRUE) {
5098 (void) printf(gettext("no pools available\n"));
5106 get_callback(zpool_handle_t *zhp, void *data)
5108 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5109 char value[MAXNAMELEN];
5110 zprop_source_t srctype;
5113 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5116 * Skip the special fake placeholder. This will also skip
5117 * over the name property when 'all' is specified.
5119 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5120 pl == cbp->cb_proplist)
5123 if (pl->pl_prop == ZPROP_INVAL &&
5124 (zpool_prop_feature(pl->pl_user_prop) ||
5125 zpool_prop_unsupported(pl->pl_user_prop))) {
5126 srctype = ZPROP_SRC_LOCAL;
5128 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5129 value, sizeof (value)) == 0) {
5130 zprop_print_one_property(zpool_get_name(zhp),
5131 cbp, pl->pl_user_prop, value, srctype,
5135 if (zpool_get_prop(zhp, pl->pl_prop, value,
5136 sizeof (value), &srctype) != 0)
5139 zprop_print_one_property(zpool_get_name(zhp), cbp,
5140 zpool_prop_to_name(pl->pl_prop), value, srctype,
5148 zpool_do_get(int argc, char **argv)
5150 zprop_get_cbdata_t cb = { 0 };
5151 zprop_list_t fake_name = { 0 };
5155 (void) fprintf(stderr, gettext("missing property "
5160 cb.cb_first = B_TRUE;
5161 cb.cb_sources = ZPROP_SRC_ALL;
5162 cb.cb_columns[0] = GET_COL_NAME;
5163 cb.cb_columns[1] = GET_COL_PROPERTY;
5164 cb.cb_columns[2] = GET_COL_VALUE;
5165 cb.cb_columns[3] = GET_COL_SOURCE;
5166 cb.cb_type = ZFS_TYPE_POOL;
5168 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5169 ZFS_TYPE_POOL) != 0)
5172 if (cb.cb_proplist != NULL) {
5173 fake_name.pl_prop = ZPOOL_PROP_NAME;
5174 fake_name.pl_width = strlen(gettext("NAME"));
5175 fake_name.pl_next = cb.cb_proplist;
5176 cb.cb_proplist = &fake_name;
5179 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5182 if (cb.cb_proplist == &fake_name)
5183 zprop_free_list(fake_name.pl_next);
5185 zprop_free_list(cb.cb_proplist);
5190 typedef struct set_cbdata {
5193 boolean_t cb_any_successful;
5197 set_callback(zpool_handle_t *zhp, void *data)
5200 set_cbdata_t *cb = (set_cbdata_t *)data;
5202 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5205 cb->cb_any_successful = B_TRUE;
5211 zpool_do_set(int argc, char **argv)
5213 set_cbdata_t cb = { 0 };
5216 if (argc > 1 && argv[1][0] == '-') {
5217 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5223 (void) fprintf(stderr, gettext("missing property=value "
5229 (void) fprintf(stderr, gettext("missing pool name\n"));
5234 (void) fprintf(stderr, gettext("too many pool names\n"));
5238 cb.cb_propname = argv[1];
5239 cb.cb_value = strchr(cb.cb_propname, '=');
5240 if (cb.cb_value == NULL) {
5241 (void) fprintf(stderr, gettext("missing value in "
5242 "property=value argument\n"));
5246 *(cb.cb_value) = '\0';
5249 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5256 find_command_idx(char *command, int *idx)
5260 for (i = 0; i < NCOMMAND; i++) {
5261 if (command_table[i].name == NULL)
5264 if (strcmp(command, command_table[i].name) == 0) {
5273 main(int argc, char **argv)
5279 (void) setlocale(LC_ALL, "");
5280 (void) textdomain(TEXT_DOMAIN);
5282 if ((g_zfs = libzfs_init()) == NULL) {
5283 (void) fprintf(stderr, gettext("internal error: failed to "
5284 "initialize ZFS library\n"));
5288 libzfs_print_on_error(g_zfs, B_TRUE);
5293 * Make sure the user has specified some command.
5296 (void) fprintf(stderr, gettext("missing command\n"));
5305 if (strcmp(cmdname, "-?") == 0)
5308 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5311 * Run the appropriate command.
5313 if (find_command_idx(cmdname, &i) == 0) {
5314 current_command = &command_table[i];
5315 ret = command_table[i].func(argc - 1, argv + 1);
5316 } else if (strchr(cmdname, '=')) {
5317 verify(find_command_idx("set", &i) == 0);
5318 current_command = &command_table[i];
5319 ret = command_table[i].func(argc, argv);
5320 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5322 * 'freeze' is a vile debugging abomination, so we treat
5325 zfs_cmd_t zc = { 0 };
5326 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
5327 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
5329 (void) fprintf(stderr, gettext("unrecognized "
5330 "command '%s'\n"), cmdname);
5334 if (ret == 0 && log_history)
5335 (void) zpool_log_history(g_zfs, history_str);
5340 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5341 * for the purposes of running ::findleaks.
5343 if (getenv("ZFS_ABORT") != NULL) {
5344 (void) printf("dumping core by request\n");