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"),
860 /* Compatiblity with FreeBSD 9.0 and 9.1: Use version 28 if unspecified */
861 if (enable_all_pool_feat && !prop_list_contains_feature(props)) {
862 if (add_prop_list(zpool_prop_to_name(
863 ZPOOL_PROP_VERSION), "28", &props, B_TRUE))
865 enable_all_pool_feat = B_FALSE;
867 #endif /* __FreeBSD__ */
872 /* get pool name and check number of arguments */
874 (void) fprintf(stderr, gettext("missing pool name argument\n"));
878 (void) fprintf(stderr, gettext("missing vdev specification\n"));
885 * As a special case, check for use of '/' in the name, and direct the
886 * user to use 'zfs create' instead.
888 if (strchr(poolname, '/') != NULL) {
889 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
890 "character '/' in pool name\n"), poolname);
891 (void) fprintf(stderr, gettext("use 'zfs create' to "
892 "create a dataset\n"));
896 /* pass off to get_vdev_spec for bulk processing */
897 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
902 /* make_root_vdev() allows 0 toplevel children if there are spares */
903 if (!zfs_allocatable_devs(nvroot)) {
904 (void) fprintf(stderr, gettext("invalid vdev "
905 "specification: at least one toplevel vdev must be "
910 if (altroot != NULL && altroot[0] != '/') {
911 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
912 "must be an absolute path\n"), altroot);
917 * Check the validity of the mountpoint and direct the user to use the
918 * '-m' mountpoint option if it looks like its in use.
920 if (mountpoint == NULL ||
921 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
922 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
923 char buf[MAXPATHLEN];
926 if (mountpoint && mountpoint[0] != '/') {
927 (void) fprintf(stderr, gettext("invalid mountpoint "
928 "'%s': must be an absolute path, 'legacy', or "
929 "'none'\n"), mountpoint);
933 if (mountpoint == NULL) {
935 (void) snprintf(buf, sizeof (buf), "%s/%s",
938 (void) snprintf(buf, sizeof (buf), "/%s",
942 (void) snprintf(buf, sizeof (buf), "%s%s",
943 altroot, mountpoint);
945 (void) snprintf(buf, sizeof (buf), "%s",
949 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
950 (void) fprintf(stderr, gettext("mountpoint '%s' : "
951 "%s\n"), buf, strerror(errno));
952 (void) fprintf(stderr, gettext("use '-m' "
953 "option to provide a different default\n"));
958 while (count < 3 && readdir(dirp) != NULL)
960 (void) closedir(dirp);
963 (void) fprintf(stderr, gettext("mountpoint "
964 "'%s' exists and is not empty\n"), buf);
965 (void) fprintf(stderr, gettext("use '-m' "
966 "option to provide a "
967 "different default\n"));
975 * For a dry run invocation, print out a basic message and run
976 * through all the vdevs in the list and print out in an
977 * appropriate hierarchy.
979 (void) printf(gettext("would create '%s' with the "
980 "following layout:\n\n"), poolname);
982 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
983 if (num_logs(nvroot) > 0)
984 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
989 * Hand off to libzfs.
991 if (enable_all_pool_feat) {
993 for (i = 0; i < SPA_FEATURES; i++) {
994 char propname[MAXPATHLEN];
995 zfeature_info_t *feat = &spa_feature_table[i];
997 (void) snprintf(propname, sizeof (propname),
998 "feature@%s", feat->fi_uname);
1001 * Skip feature if user specified it manually
1002 * on the command line.
1004 if (nvlist_exists(props, propname))
1007 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
1008 &props, B_TRUE) != 0)
1012 if (zpool_create(g_zfs, poolname,
1013 nvroot, props, fsprops) == 0) {
1014 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1015 ZFS_TYPE_FILESYSTEM);
1017 if (mountpoint != NULL)
1018 verify(zfs_prop_set(pool,
1020 ZFS_PROP_MOUNTPOINT),
1022 if (zfs_mount(pool, NULL, 0) == 0)
1023 ret = zfs_shareall(pool);
1026 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1027 (void) fprintf(stderr, gettext("pool name may have "
1033 nvlist_free(nvroot);
1034 nvlist_free(fsprops);
1038 nvlist_free(fsprops);
1045 * zpool destroy <pool>
1047 * -f Forcefully unmount any datasets
1049 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1052 zpool_do_destroy(int argc, char **argv)
1054 boolean_t force = B_FALSE;
1057 zpool_handle_t *zhp;
1061 while ((c = getopt(argc, argv, "f")) != -1) {
1067 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1076 /* check arguments */
1078 (void) fprintf(stderr, gettext("missing pool argument\n"));
1082 (void) fprintf(stderr, gettext("too many arguments\n"));
1088 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1090 * As a special case, check for use of '/' in the name, and
1091 * direct the user to use 'zfs destroy' instead.
1093 if (strchr(pool, '/') != NULL)
1094 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1095 "destroy a dataset\n"));
1099 if (zpool_disable_datasets(zhp, force) != 0) {
1100 (void) fprintf(stderr, gettext("could not destroy '%s': "
1101 "could not unmount datasets\n"), zpool_get_name(zhp));
1105 /* The history must be logged as part of the export */
1106 log_history = B_FALSE;
1108 ret = (zpool_destroy(zhp, history_str) != 0);
1116 * zpool export [-f] <pool> ...
1118 * -f Forcefully unmount datasets
1120 * Export the given pools. By default, the command will attempt to cleanly
1121 * unmount any active datasets within the pool. If the '-f' flag is specified,
1122 * then the datasets will be forcefully unmounted.
1125 zpool_do_export(int argc, char **argv)
1127 boolean_t force = B_FALSE;
1128 boolean_t hardforce = B_FALSE;
1130 zpool_handle_t *zhp;
1135 while ((c = getopt(argc, argv, "fF")) != -1) {
1144 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1153 /* check arguments */
1155 (void) fprintf(stderr, gettext("missing pool argument\n"));
1160 for (i = 0; i < argc; i++) {
1161 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1166 if (zpool_disable_datasets(zhp, force) != 0) {
1172 /* The history must be logged as part of the export */
1173 log_history = B_FALSE;
1176 if (zpool_export_force(zhp, history_str) != 0)
1178 } else if (zpool_export(zhp, force, history_str) != 0) {
1189 * Given a vdev configuration, determine the maximum width needed for the device
1193 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1195 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1200 if (strlen(name) + depth > max)
1201 max = strlen(name) + depth;
1205 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1206 &child, &children) == 0) {
1207 for (c = 0; c < children; c++)
1208 if ((ret = max_width(zhp, child[c], depth + 2,
1213 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1214 &child, &children) == 0) {
1215 for (c = 0; c < children; c++)
1216 if ((ret = max_width(zhp, child[c], depth + 2,
1221 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1222 &child, &children) == 0) {
1223 for (c = 0; c < children; c++)
1224 if ((ret = max_width(zhp, child[c], depth + 2,
1233 typedef struct spare_cbdata {
1235 zpool_handle_t *cb_zhp;
1239 find_vdev(nvlist_t *nv, uint64_t search)
1245 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1249 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1250 &child, &children) == 0) {
1251 for (c = 0; c < children; c++)
1252 if (find_vdev(child[c], search))
1260 find_spare(zpool_handle_t *zhp, void *data)
1262 spare_cbdata_t *cbp = data;
1263 nvlist_t *config, *nvroot;
1265 config = zpool_get_config(zhp, NULL);
1266 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1269 if (find_vdev(nvroot, cbp->cb_guid)) {
1279 * Print out configuration state as requested by status_callback.
1282 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1283 int namewidth, int depth, boolean_t isspare)
1287 pool_scan_stat_t *ps = NULL;
1289 char rbuf[6], wbuf[6], cbuf[6];
1291 uint64_t notpresent;
1295 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1296 &child, &children) != 0)
1299 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1300 (uint64_t **)&vs, &c) == 0);
1302 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1305 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1308 if (vs->vs_aux == VDEV_AUX_SPARED)
1310 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1314 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1318 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1319 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1320 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1321 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1324 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1325 ¬present) == 0 ||
1326 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1328 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1329 (void) printf(" was %s", path);
1330 } else if (vs->vs_aux != 0) {
1333 switch (vs->vs_aux) {
1334 case VDEV_AUX_OPEN_FAILED:
1335 (void) printf(gettext("cannot open"));
1338 case VDEV_AUX_BAD_GUID_SUM:
1339 (void) printf(gettext("missing device"));
1342 case VDEV_AUX_NO_REPLICAS:
1343 (void) printf(gettext("insufficient replicas"));
1346 case VDEV_AUX_VERSION_NEWER:
1347 (void) printf(gettext("newer version"));
1350 case VDEV_AUX_UNSUP_FEAT:
1351 (void) printf(gettext("unsupported feature(s)"));
1354 case VDEV_AUX_SPARED:
1355 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1357 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1358 if (strcmp(zpool_get_name(cb.cb_zhp),
1359 zpool_get_name(zhp)) == 0)
1360 (void) printf(gettext("currently in "
1363 (void) printf(gettext("in use by "
1365 zpool_get_name(cb.cb_zhp));
1366 zpool_close(cb.cb_zhp);
1368 (void) printf(gettext("currently in use"));
1372 case VDEV_AUX_ERR_EXCEEDED:
1373 (void) printf(gettext("too many errors"));
1376 case VDEV_AUX_IO_FAILURE:
1377 (void) printf(gettext("experienced I/O failures"));
1380 case VDEV_AUX_BAD_LOG:
1381 (void) printf(gettext("bad intent log"));
1384 case VDEV_AUX_EXTERNAL:
1385 (void) printf(gettext("external device fault"));
1388 case VDEV_AUX_SPLIT_POOL:
1389 (void) printf(gettext("split into new pool"));
1393 (void) printf(gettext("corrupted data"));
1398 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1399 (uint64_t **)&ps, &c);
1401 if (ps && ps->pss_state == DSS_SCANNING &&
1402 vs->vs_scan_processed != 0 && children == 0) {
1403 (void) printf(gettext(" (%s)"),
1404 (ps->pss_func == POOL_SCAN_RESILVER) ?
1405 "resilvering" : "repairing");
1408 (void) printf("\n");
1410 for (c = 0; c < children; c++) {
1411 uint64_t islog = B_FALSE, ishole = B_FALSE;
1413 /* Don't print logs or holes here */
1414 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1416 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1418 if (islog || ishole)
1420 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1421 print_status_config(zhp, vname, child[c],
1422 namewidth, depth + 2, isspare);
1429 * Print the configuration of an exported pool. Iterate over all vdevs in the
1430 * pool, printing out the name and status for each one.
1433 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1440 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1441 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1442 strcmp(type, VDEV_TYPE_HOLE) == 0)
1445 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1446 (uint64_t **)&vs, &c) == 0);
1448 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1449 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1451 if (vs->vs_aux != 0) {
1454 switch (vs->vs_aux) {
1455 case VDEV_AUX_OPEN_FAILED:
1456 (void) printf(gettext("cannot open"));
1459 case VDEV_AUX_BAD_GUID_SUM:
1460 (void) printf(gettext("missing device"));
1463 case VDEV_AUX_NO_REPLICAS:
1464 (void) printf(gettext("insufficient replicas"));
1467 case VDEV_AUX_VERSION_NEWER:
1468 (void) printf(gettext("newer version"));
1471 case VDEV_AUX_UNSUP_FEAT:
1472 (void) printf(gettext("unsupported feature(s)"));
1475 case VDEV_AUX_ERR_EXCEEDED:
1476 (void) printf(gettext("too many errors"));
1480 (void) printf(gettext("corrupted data"));
1484 (void) printf("\n");
1486 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1487 &child, &children) != 0)
1490 for (c = 0; c < children; c++) {
1491 uint64_t is_log = B_FALSE;
1493 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1498 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1499 print_import_config(vname, child[c], namewidth, depth + 2);
1503 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1504 &child, &children) == 0) {
1505 (void) printf(gettext("\tcache\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);
1513 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1514 &child, &children) == 0) {
1515 (void) printf(gettext("\tspares\n"));
1516 for (c = 0; c < children; c++) {
1517 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1518 (void) printf("\t %s\n", vname);
1526 * Logs are recorded as top level vdevs in the main pool child array
1527 * but with "is_log" set to 1. We use either print_status_config() or
1528 * print_import_config() to print the top level logs then any log
1529 * children (eg mirrored slogs) are printed recursively - which
1530 * works because only the top level vdev is marked "is_log"
1533 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1538 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1542 (void) printf(gettext("\tlogs\n"));
1544 for (c = 0; c < children; c++) {
1545 uint64_t is_log = B_FALSE;
1548 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1552 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1554 print_status_config(zhp, name, child[c], namewidth,
1557 print_import_config(name, child[c], namewidth, 2);
1563 * Display the status for the given pool.
1566 show_import(nvlist_t *config)
1568 uint64_t pool_state;
1580 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1582 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1584 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1586 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1589 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1590 (uint64_t **)&vs, &vsc) == 0);
1591 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1593 reason = zpool_import_status(config, &msgid);
1595 (void) printf(gettext(" pool: %s\n"), name);
1596 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1597 (void) printf(gettext(" state: %s"), health);
1598 if (pool_state == POOL_STATE_DESTROYED)
1599 (void) printf(gettext(" (DESTROYED)"));
1600 (void) printf("\n");
1603 case ZPOOL_STATUS_MISSING_DEV_R:
1604 case ZPOOL_STATUS_MISSING_DEV_NR:
1605 case ZPOOL_STATUS_BAD_GUID_SUM:
1606 (void) printf(gettext(" status: One or more devices are "
1607 "missing from the system.\n"));
1610 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1611 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1612 (void) printf(gettext(" status: One or more devices contains "
1613 "corrupted data.\n"));
1616 case ZPOOL_STATUS_CORRUPT_DATA:
1618 gettext(" status: The pool data is corrupted.\n"));
1621 case ZPOOL_STATUS_OFFLINE_DEV:
1622 (void) printf(gettext(" status: One or more devices "
1623 "are offlined.\n"));
1626 case ZPOOL_STATUS_CORRUPT_POOL:
1627 (void) printf(gettext(" status: The pool metadata is "
1631 case ZPOOL_STATUS_VERSION_OLDER:
1632 (void) printf(gettext(" status: The pool is formatted using a "
1633 "legacy on-disk version.\n"));
1636 case ZPOOL_STATUS_VERSION_NEWER:
1637 (void) printf(gettext(" status: The pool is formatted using an "
1638 "incompatible version.\n"));
1641 case ZPOOL_STATUS_FEAT_DISABLED:
1642 (void) printf(gettext(" status: Some supported features are "
1643 "not enabled on the pool.\n"));
1646 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1647 (void) printf(gettext("status: The pool uses the following "
1648 "feature(s) not supported on this sytem:\n"));
1649 zpool_print_unsup_feat(config);
1652 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1653 (void) printf(gettext("status: The pool can only be accessed "
1654 "in read-only mode on this system. It\n\tcannot be "
1655 "accessed in read-write mode because it uses the "
1656 "following\n\tfeature(s) not supported on this system:\n"));
1657 zpool_print_unsup_feat(config);
1660 case ZPOOL_STATUS_HOSTID_MISMATCH:
1661 (void) printf(gettext(" status: The pool was last accessed by "
1662 "another system.\n"));
1665 case ZPOOL_STATUS_FAULTED_DEV_R:
1666 case ZPOOL_STATUS_FAULTED_DEV_NR:
1667 (void) printf(gettext(" status: One or more devices are "
1671 case ZPOOL_STATUS_BAD_LOG:
1672 (void) printf(gettext(" status: An intent log record cannot be "
1676 case ZPOOL_STATUS_RESILVERING:
1677 (void) printf(gettext(" status: One or more devices were being "
1683 * No other status can be seen when importing pools.
1685 assert(reason == ZPOOL_STATUS_OK);
1689 * Print out an action according to the overall state of the pool.
1691 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1692 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1693 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1694 (void) printf(gettext(" action: The pool can be "
1695 "imported using its name or numeric identifier, "
1696 "though\n\tsome features will not be available "
1697 "without an explicit 'zpool upgrade'.\n"));
1698 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1699 (void) printf(gettext(" action: The pool can be "
1700 "imported using its name or numeric "
1701 "identifier and\n\tthe '-f' flag.\n"));
1703 (void) printf(gettext(" action: The pool can be "
1704 "imported using its name or numeric "
1707 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1708 (void) printf(gettext(" action: The pool can be imported "
1709 "despite missing or damaged devices. The\n\tfault "
1710 "tolerance of the pool may be compromised if imported.\n"));
1713 case ZPOOL_STATUS_VERSION_NEWER:
1714 (void) printf(gettext(" action: The pool cannot be "
1715 "imported. Access the pool on a system running "
1716 "newer\n\tsoftware, or recreate the pool from "
1719 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1720 (void) printf(gettext("action: The pool cannot be "
1721 "imported. Access the pool on a system that "
1722 "supports\n\tthe required feature(s), or recreate "
1723 "the pool from backup.\n"));
1725 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1726 (void) printf(gettext("action: The pool cannot be "
1727 "imported in read-write mode. Import the pool "
1729 "\t\"-o readonly=on\", access the pool on a system "
1730 "that supports the\n\trequired feature(s), or "
1731 "recreate the pool from backup.\n"));
1733 case ZPOOL_STATUS_MISSING_DEV_R:
1734 case ZPOOL_STATUS_MISSING_DEV_NR:
1735 case ZPOOL_STATUS_BAD_GUID_SUM:
1736 (void) printf(gettext(" action: The pool cannot be "
1737 "imported. Attach the missing\n\tdevices and try "
1741 (void) printf(gettext(" action: The pool cannot be "
1742 "imported due to damaged devices or data.\n"));
1746 /* Print the comment attached to the pool. */
1747 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1748 (void) printf(gettext("comment: %s\n"), comment);
1751 * If the state is "closed" or "can't open", and the aux state
1752 * is "corrupt data":
1754 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1755 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1756 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1757 if (pool_state == POOL_STATE_DESTROYED)
1758 (void) printf(gettext("\tThe pool was destroyed, "
1759 "but can be imported using the '-Df' flags.\n"));
1760 else if (pool_state != POOL_STATE_EXPORTED)
1761 (void) printf(gettext("\tThe pool may be active on "
1762 "another system, but can be imported using\n\t"
1763 "the '-f' flag.\n"));
1767 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1770 (void) printf(gettext(" config:\n\n"));
1772 namewidth = max_width(NULL, nvroot, 0, 0);
1776 print_import_config(name, nvroot, namewidth, 0);
1777 if (num_logs(nvroot) > 0)
1778 print_logs(NULL, nvroot, namewidth, B_FALSE);
1780 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1781 (void) printf(gettext("\n\tAdditional devices are known to "
1782 "be part of this pool, though their\n\texact "
1783 "configuration cannot be determined.\n"));
1788 * Perform the import for the given configuration. This passes the heavy
1789 * lifting off to zpool_import_props(), and then mounts the datasets contained
1793 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1794 nvlist_t *props, int flags)
1796 zpool_handle_t *zhp;
1801 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1804 verify(nvlist_lookup_uint64(config,
1805 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1806 verify(nvlist_lookup_uint64(config,
1807 ZPOOL_CONFIG_VERSION, &version) == 0);
1808 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1809 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1810 "is formatted using an unsupported ZFS version\n"), name);
1812 } else if (state != POOL_STATE_EXPORTED &&
1813 !(flags & ZFS_IMPORT_ANY_HOST)) {
1816 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1818 if ((unsigned long)hostid != gethostid()) {
1823 verify(nvlist_lookup_string(config,
1824 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1825 verify(nvlist_lookup_uint64(config,
1826 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1828 (void) fprintf(stderr, gettext("cannot import "
1829 "'%s': pool may be in use from other "
1830 "system, it was last accessed by %s "
1831 "(hostid: 0x%lx) on %s"), name, hostname,
1832 (unsigned long)hostid,
1833 asctime(localtime(&t)));
1834 (void) fprintf(stderr, gettext("use '-f' to "
1835 "import anyway\n"));
1839 (void) fprintf(stderr, gettext("cannot import '%s': "
1840 "pool may be in use from other system\n"), name);
1841 (void) fprintf(stderr, gettext("use '-f' to import "
1847 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1850 if (newname != NULL)
1851 name = (char *)newname;
1853 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1856 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1857 !(flags & ZFS_IMPORT_ONLY) &&
1858 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1868 * zpool import [-d dir] [-D]
1869 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1870 * [-d dir | -c cachefile] [-f] -a
1871 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1872 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1874 * -c Read pool information from a cachefile instead of searching
1877 * -d Scan in a specific directory, other than /dev/dsk. More than
1878 * one directory can be specified using multiple '-d' options.
1880 * -D Scan for previously destroyed pools or import all or only
1881 * specified destroyed pools.
1883 * -R Temporarily import the pool, with all mountpoints relative to
1884 * the given root. The pool will remain exported when the machine
1887 * -V Import even in the presence of faulted vdevs. This is an
1888 * intentionally undocumented option for testing purposes, and
1889 * treats the pool configuration as complete, leaving any bad
1890 * vdevs in the FAULTED state. In other words, it does verbatim
1893 * -f Force import, even if it appears that the pool is active.
1895 * -F Attempt rewind if necessary.
1897 * -n See if rewind would work, but don't actually rewind.
1899 * -N Import the pool but don't mount datasets.
1901 * -T Specify a starting txg to use for import. This option is
1902 * intentionally undocumented option for testing purposes.
1904 * -a Import all pools found.
1906 * -o Set property=value and/or temporary mount options (without '=').
1908 * The import command scans for pools to import, and import pools based on pool
1909 * name and GUID. The pool can also be renamed as part of the import process.
1912 zpool_do_import(int argc, char **argv)
1914 char **searchdirs = NULL;
1918 nvlist_t *pools = NULL;
1919 boolean_t do_all = B_FALSE;
1920 boolean_t do_destroyed = B_FALSE;
1921 char *mntopts = NULL;
1924 uint64_t searchguid = 0;
1925 char *searchname = NULL;
1927 nvlist_t *found_config;
1928 nvlist_t *policy = NULL;
1929 nvlist_t *props = NULL;
1931 int flags = ZFS_IMPORT_NORMAL;
1932 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1933 boolean_t dryrun = B_FALSE;
1934 boolean_t do_rewind = B_FALSE;
1935 boolean_t xtreme_rewind = B_FALSE;
1936 uint64_t pool_state, txg = -1ULL;
1937 char *cachefile = NULL;
1938 importargs_t idata = { 0 };
1942 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1951 if (searchdirs == NULL) {
1952 searchdirs = safe_malloc(sizeof (char *));
1954 char **tmp = safe_malloc((nsearch + 1) *
1956 bcopy(searchdirs, tmp, nsearch *
1961 searchdirs[nsearch++] = optarg;
1964 do_destroyed = B_TRUE;
1967 flags |= ZFS_IMPORT_ANY_HOST;
1973 flags |= ZFS_IMPORT_MISSING_LOG;
1979 flags |= ZFS_IMPORT_ONLY;
1982 if ((propval = strchr(optarg, '=')) != NULL) {
1985 if (add_prop_list(optarg, propval,
1993 if (add_prop_list(zpool_prop_to_name(
1994 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1996 if (nvlist_lookup_string(props,
1997 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2000 if (add_prop_list(zpool_prop_to_name(
2001 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2006 txg = strtoull(optarg, &endptr, 10);
2007 if (errno != 0 || *endptr != '\0') {
2008 (void) fprintf(stderr,
2009 gettext("invalid txg value\n"));
2012 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2015 flags |= ZFS_IMPORT_VERBATIM;
2018 xtreme_rewind = B_TRUE;
2021 (void) fprintf(stderr, gettext("missing argument for "
2022 "'%c' option\n"), optopt);
2026 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2035 if (cachefile && nsearch != 0) {
2036 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2040 if ((dryrun || xtreme_rewind) && !do_rewind) {
2041 (void) fprintf(stderr,
2042 gettext("-n or -X only meaningful with -F\n"));
2046 rewind_policy = ZPOOL_TRY_REWIND;
2048 rewind_policy = ZPOOL_DO_REWIND;
2050 rewind_policy |= ZPOOL_EXTREME_REWIND;
2052 /* In the future, we can capture further policy and include it here */
2053 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2054 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2055 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2058 if (searchdirs == NULL) {
2059 searchdirs = safe_malloc(sizeof (char *));
2060 searchdirs[0] = "/dev";
2064 /* check argument count */
2067 (void) fprintf(stderr, gettext("too many arguments\n"));
2072 (void) fprintf(stderr, gettext("too many arguments\n"));
2077 * Check for the SYS_CONFIG privilege. We do this explicitly
2078 * here because otherwise any attempt to discover pools will
2081 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2082 (void) fprintf(stderr, gettext("cannot "
2083 "discover pools: permission denied\n"));
2085 nvlist_free(policy);
2091 * Depending on the arguments given, we do one of the following:
2093 * <none> Iterate through all pools and display information about
2096 * -a Iterate through all pools and try to import each one.
2098 * <id> Find the pool that corresponds to the given GUID/pool
2099 * name and import that one.
2101 * -D Above options applies only to destroyed pools.
2107 searchguid = strtoull(argv[0], &endptr, 10);
2108 if (errno != 0 || *endptr != '\0')
2109 searchname = argv[0];
2110 found_config = NULL;
2113 * User specified a name or guid. Ensure it's unique.
2115 idata.unique = B_TRUE;
2119 idata.path = searchdirs;
2120 idata.paths = nsearch;
2121 idata.poolname = searchname;
2122 idata.guid = searchguid;
2123 idata.cachefile = cachefile;
2125 pools = zpool_search_import(g_zfs, &idata);
2127 if (pools != NULL && idata.exists &&
2128 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2129 (void) fprintf(stderr, gettext("cannot import '%s': "
2130 "a pool with that name already exists\n"),
2132 (void) fprintf(stderr, gettext("use the form '%s "
2133 "<pool | id> <newpool>' to give it a new name\n"),
2136 } else if (pools == NULL && idata.exists) {
2137 (void) fprintf(stderr, gettext("cannot import '%s': "
2138 "a pool with that name is already created/imported,\n"),
2140 (void) fprintf(stderr, gettext("and no additional pools "
2141 "with that name were found\n"));
2143 } else if (pools == NULL) {
2145 (void) fprintf(stderr, gettext("cannot import '%s': "
2146 "no such pool available\n"), argv[0]);
2153 nvlist_free(policy);
2158 * At this point we have a list of import candidate configs. Even if
2159 * we were searching by pool name or guid, we still need to
2160 * post-process the list to deal with pool state and possible
2166 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2168 verify(nvpair_value_nvlist(elem, &config) == 0);
2170 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2172 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2174 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2177 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2184 (void) printf("\n");
2187 err |= do_import(config, NULL, mntopts,
2190 show_import(config);
2192 } else if (searchname != NULL) {
2196 * We are searching for a pool based on name.
2198 verify(nvlist_lookup_string(config,
2199 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2201 if (strcmp(name, searchname) == 0) {
2202 if (found_config != NULL) {
2203 (void) fprintf(stderr, gettext(
2204 "cannot import '%s': more than "
2205 "one matching pool\n"), searchname);
2206 (void) fprintf(stderr, gettext(
2207 "import by numeric ID instead\n"));
2210 found_config = config;
2216 * Search for a pool by guid.
2218 verify(nvlist_lookup_uint64(config,
2219 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2221 if (guid == searchguid)
2222 found_config = config;
2227 * If we were searching for a specific pool, verify that we found a
2228 * pool, and then do the import.
2230 if (argc != 0 && err == 0) {
2231 if (found_config == NULL) {
2232 (void) fprintf(stderr, gettext("cannot import '%s': "
2233 "no such pool available\n"), argv[0]);
2236 err |= do_import(found_config, argc == 1 ? NULL :
2237 argv[1], mntopts, props, flags);
2242 * If we were just looking for pools, report an error if none were
2245 if (argc == 0 && first)
2246 (void) fprintf(stderr,
2247 gettext("no pools available to import\n"));
2252 nvlist_free(policy);
2255 return (err ? 1 : 0);
2258 typedef struct iostat_cbdata {
2259 boolean_t cb_verbose;
2262 zpool_list_t *cb_list;
2266 print_iostat_separator(iostat_cbdata_t *cb)
2270 for (i = 0; i < cb->cb_namewidth; i++)
2272 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2276 print_iostat_header(iostat_cbdata_t *cb)
2278 (void) printf("%*s capacity operations bandwidth\n",
2279 cb->cb_namewidth, "");
2280 (void) printf("%-*s alloc free read write read write\n",
2281 cb->cb_namewidth, "pool");
2282 print_iostat_separator(cb);
2286 * Display a single statistic.
2289 print_one_stat(uint64_t value)
2293 zfs_nicenum(value, buf, sizeof (buf));
2294 (void) printf(" %5s", buf);
2298 * Print out all the statistics for the given vdev. This can either be the
2299 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2300 * is a verbose output, and we don't want to display the toplevel pool stats.
2303 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2304 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2306 nvlist_t **oldchild, **newchild;
2308 vdev_stat_t *oldvs, *newvs;
2309 vdev_stat_t zerovs = { 0 };
2314 if (oldnv != NULL) {
2315 verify(nvlist_lookup_uint64_array(oldnv,
2316 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2321 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2322 (uint64_t **)&newvs, &c) == 0);
2324 if (strlen(name) + depth > cb->cb_namewidth)
2325 (void) printf("%*s%s", depth, "", name);
2327 (void) printf("%*s%s%*s", depth, "", name,
2328 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2330 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2335 scale = (double)NANOSEC / tdelta;
2337 /* only toplevel vdevs have capacity stats */
2338 if (newvs->vs_space == 0) {
2339 (void) printf(" - -");
2341 print_one_stat(newvs->vs_alloc);
2342 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2345 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2346 oldvs->vs_ops[ZIO_TYPE_READ])));
2348 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2349 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2351 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2352 oldvs->vs_bytes[ZIO_TYPE_READ])));
2354 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2355 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2357 (void) printf("\n");
2359 if (!cb->cb_verbose)
2362 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2363 &newchild, &children) != 0)
2366 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2367 &oldchild, &c) != 0)
2370 for (c = 0; c < children; c++) {
2371 uint64_t ishole = B_FALSE, islog = B_FALSE;
2373 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2376 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2379 if (ishole || islog)
2382 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2383 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2384 newchild[c], cb, depth + 2);
2389 * Log device section
2392 if (num_logs(newnv) > 0) {
2393 (void) printf("%-*s - - - - - "
2394 "-\n", cb->cb_namewidth, "logs");
2396 for (c = 0; c < children; c++) {
2397 uint64_t islog = B_FALSE;
2398 (void) nvlist_lookup_uint64(newchild[c],
2399 ZPOOL_CONFIG_IS_LOG, &islog);
2402 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2404 print_vdev_stats(zhp, vname, oldnv ?
2405 oldchild[c] : NULL, newchild[c],
2414 * Include level 2 ARC devices in iostat output
2416 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2417 &newchild, &children) != 0)
2420 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2421 &oldchild, &c) != 0)
2425 (void) printf("%-*s - - - - - "
2426 "-\n", cb->cb_namewidth, "cache");
2427 for (c = 0; c < children; c++) {
2428 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2430 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2431 newchild[c], cb, depth + 2);
2438 refresh_iostat(zpool_handle_t *zhp, void *data)
2440 iostat_cbdata_t *cb = data;
2444 * If the pool has disappeared, remove it from the list and continue.
2446 if (zpool_refresh_stats(zhp, &missing) != 0)
2450 pool_list_remove(cb->cb_list, zhp);
2456 * Callback to print out the iostats for the given pool.
2459 print_iostat(zpool_handle_t *zhp, void *data)
2461 iostat_cbdata_t *cb = data;
2462 nvlist_t *oldconfig, *newconfig;
2463 nvlist_t *oldnvroot, *newnvroot;
2465 newconfig = zpool_get_config(zhp, &oldconfig);
2467 if (cb->cb_iteration == 1)
2470 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2473 if (oldconfig == NULL)
2476 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2480 * Print out the statistics for the pool.
2482 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2485 print_iostat_separator(cb);
2491 get_namewidth(zpool_handle_t *zhp, void *data)
2493 iostat_cbdata_t *cb = data;
2494 nvlist_t *config, *nvroot;
2496 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2497 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2499 if (!cb->cb_verbose)
2500 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2502 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2507 * The width must fall into the range [10,38]. The upper limit is the
2508 * maximum we can have and still fit in 80 columns.
2510 if (cb->cb_namewidth < 10)
2511 cb->cb_namewidth = 10;
2512 if (cb->cb_namewidth > 38)
2513 cb->cb_namewidth = 38;
2519 * Parse the input string, get the 'interval' and 'count' value if there is one.
2522 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2525 unsigned long interval = 0, count = 0;
2526 int argc = *argcp, errno;
2529 * Determine if the last argument is an integer or a pool name
2531 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2535 interval = strtoul(argv[argc - 1], &end, 10);
2537 if (*end == '\0' && errno == 0) {
2538 if (interval == 0) {
2539 (void) fprintf(stderr, gettext("interval "
2540 "cannot be zero\n"));
2544 * Ignore the last parameter
2549 * If this is not a valid number, just plow on. The
2550 * user will get a more informative error message later
2558 * If the last argument is also an integer, then we have both a count
2561 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2566 interval = strtoul(argv[argc - 1], &end, 10);
2568 if (*end == '\0' && errno == 0) {
2569 if (interval == 0) {
2570 (void) fprintf(stderr, gettext("interval "
2571 "cannot be zero\n"));
2576 * Ignore the last parameter
2590 get_timestamp_arg(char c)
2593 timestamp_fmt = UDATE;
2595 timestamp_fmt = DDATE;
2601 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2603 * -v Display statistics for individual vdevs
2604 * -T Display a timestamp in date(1) or Unix format
2606 * This command can be tricky because we want to be able to deal with pool
2607 * creation/destruction as well as vdev configuration changes. The bulk of this
2608 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2609 * on pool_list_update() to detect the addition of new pools. Configuration
2610 * changes are all handled within libzfs.
2613 zpool_do_iostat(int argc, char **argv)
2618 unsigned long interval = 0, count = 0;
2620 boolean_t verbose = B_FALSE;
2624 while ((c = getopt(argc, argv, "T:v")) != -1) {
2627 get_timestamp_arg(*optarg);
2633 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2642 get_interval_count(&argc, argv, &interval, &count);
2645 * Construct the list of all interesting pools.
2648 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2651 if (pool_list_count(list) == 0 && argc != 0) {
2652 pool_list_free(list);
2656 if (pool_list_count(list) == 0 && interval == 0) {
2657 pool_list_free(list);
2658 (void) fprintf(stderr, gettext("no pools available\n"));
2663 * Enter the main iostat loop.
2666 cb.cb_verbose = verbose;
2667 cb.cb_iteration = 0;
2668 cb.cb_namewidth = 0;
2671 pool_list_update(list);
2673 if ((npools = pool_list_count(list)) == 0)
2677 * Refresh all statistics. This is done as an explicit step
2678 * before calculating the maximum name width, so that any
2679 * configuration changes are properly accounted for.
2681 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2684 * Iterate over all pools to determine the maximum width
2685 * for the pool / device name column across all pools.
2687 cb.cb_namewidth = 0;
2688 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2690 if (timestamp_fmt != NODATE)
2691 print_timestamp(timestamp_fmt);
2694 * If it's the first time, or verbose mode, print the header.
2696 if (++cb.cb_iteration == 1 || verbose)
2697 print_iostat_header(&cb);
2699 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2702 * If there's more than one pool, and we're not in verbose mode
2703 * (which prints a separator for us), then print a separator.
2705 if (npools > 1 && !verbose)
2706 print_iostat_separator(&cb);
2709 (void) printf("\n");
2712 * Flush the output so that redirection to a file isn't buffered
2715 (void) fflush(stdout);
2720 if (count != 0 && --count == 0)
2723 (void) sleep(interval);
2726 pool_list_free(list);
2731 typedef struct list_cbdata {
2732 boolean_t cb_verbose;
2734 boolean_t cb_scripted;
2735 zprop_list_t *cb_proplist;
2739 * Given a list of columns to display, output appropriate headers for each one.
2742 print_header(list_cbdata_t *cb)
2744 zprop_list_t *pl = cb->cb_proplist;
2745 char headerbuf[ZPOOL_MAXPROPLEN];
2747 boolean_t first = B_TRUE;
2748 boolean_t right_justify;
2751 for (; pl != NULL; pl = pl->pl_next) {
2752 width = pl->pl_width;
2753 if (first && cb->cb_verbose) {
2755 * Reset the width to accommodate the verbose listing
2758 width = cb->cb_namewidth;
2766 right_justify = B_FALSE;
2767 if (pl->pl_prop != ZPROP_INVAL) {
2768 header = zpool_prop_column_name(pl->pl_prop);
2769 right_justify = zpool_prop_align_right(pl->pl_prop);
2773 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2774 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2775 headerbuf[i] = '\0';
2779 if (pl->pl_next == NULL && !right_justify)
2780 (void) printf("%s", header);
2781 else if (right_justify)
2782 (void) printf("%*s", width, header);
2784 (void) printf("%-*s", width, header);
2788 (void) printf("\n");
2792 * Given a pool and a list of properties, print out all the properties according
2793 * to the described layout.
2796 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2798 zprop_list_t *pl = cb->cb_proplist;
2799 boolean_t first = B_TRUE;
2800 char property[ZPOOL_MAXPROPLEN];
2802 boolean_t right_justify;
2805 for (; pl != NULL; pl = pl->pl_next) {
2807 width = pl->pl_width;
2808 if (first && cb->cb_verbose) {
2810 * Reset the width to accommodate the verbose listing
2813 width = cb->cb_namewidth;
2817 if (cb->cb_scripted)
2818 (void) printf("\t");
2825 right_justify = B_FALSE;
2826 if (pl->pl_prop != ZPROP_INVAL) {
2827 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2828 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2830 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2831 sizeof (property), NULL) != 0)
2836 right_justify = zpool_prop_align_right(pl->pl_prop);
2837 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2838 zpool_prop_unsupported(pl->pl_user_prop)) &&
2839 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2840 sizeof (property)) == 0) {
2848 * If this is being called in scripted mode, or if this is the
2849 * last column and it is left-justified, don't include a width
2852 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2853 (void) printf("%s", propstr);
2854 else if (right_justify)
2855 (void) printf("%*s", width, propstr);
2857 (void) printf("%-*s", width, propstr);
2860 (void) printf("\n");
2864 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2868 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2870 zfs_nicenum(value, propval, sizeof (propval));
2872 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2873 (void) strlcpy(propval, "-", sizeof (propval));
2876 (void) printf("\t%s", propval);
2878 (void) printf(" %*s", width, propval);
2882 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2883 list_cbdata_t *cb, int depth)
2889 boolean_t scripted = cb->cb_scripted;
2891 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2892 (uint64_t **)&vs, &c) == 0);
2896 (void) printf("\t%s", name);
2897 else if (strlen(name) + depth > cb->cb_namewidth)
2898 (void) printf("%*s%s", depth, "", name);
2900 (void) printf("%*s%s%*s", depth, "", name,
2901 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2903 /* only toplevel vdevs have capacity stats */
2904 if (vs->vs_space == 0) {
2906 (void) printf("\t-\t-\t-");
2908 (void) printf(" - - -");
2910 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2912 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2914 print_one_column(ZPOOL_PROP_FREE,
2915 vs->vs_space - vs->vs_alloc, scripted);
2917 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2919 (void) printf("\n");
2922 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2923 &child, &children) != 0)
2926 for (c = 0; c < children; c++) {
2927 uint64_t ishole = B_FALSE;
2929 if (nvlist_lookup_uint64(child[c],
2930 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2933 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2934 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2939 * Include level 2 ARC devices in iostat output
2941 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2942 &child, &children) != 0)
2946 (void) printf("%-*s - - - - - "
2947 "-\n", cb->cb_namewidth, "cache");
2948 for (c = 0; c < children; c++) {
2949 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2951 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2959 * Generic callback function to list a pool.
2962 list_callback(zpool_handle_t *zhp, void *data)
2964 list_cbdata_t *cbp = data;
2968 config = zpool_get_config(zhp, NULL);
2970 print_pool(zhp, cbp);
2971 if (!cbp->cb_verbose)
2974 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2976 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2982 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2984 * -H Scripted mode. Don't display headers, and separate properties
2986 * -o List of properties to display. Defaults to
2987 * "name,size,allocated,free,capacity,health,altroot"
2988 * -T Display a timestamp in date(1) or Unix format
2990 * List all pools in the system, whether or not they're healthy. Output space
2991 * statistics for each one, as well as health status summary.
2994 zpool_do_list(int argc, char **argv)
2998 list_cbdata_t cb = { 0 };
2999 static char default_props[] =
3000 "name,size,allocated,free,capacity,dedupratio,"
3002 char *props = default_props;
3003 unsigned long interval = 0, count = 0;
3005 boolean_t first = B_TRUE;
3008 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3011 cb.cb_scripted = B_TRUE;
3017 get_timestamp_arg(*optarg);
3020 cb.cb_verbose = B_TRUE;
3023 (void) fprintf(stderr, gettext("missing argument for "
3024 "'%c' option\n"), optopt);
3028 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3037 get_interval_count(&argc, argv, &interval, &count);
3039 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3042 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3045 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3046 (void) printf(gettext("no pools available\n"));
3047 zprop_free_list(cb.cb_proplist);
3052 pool_list_update(list);
3054 if (pool_list_count(list) == 0)
3057 cb.cb_namewidth = 0;
3058 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3060 if (timestamp_fmt != NODATE)
3061 print_timestamp(timestamp_fmt);
3063 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3067 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3072 if (count != 0 && --count == 0)
3075 (void) sleep(interval);
3078 zprop_free_list(cb.cb_proplist);
3083 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3090 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3091 &child, &children) != 0) {
3092 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3093 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3094 name += sizeof(_PATH_DEV) - 1;
3095 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3096 path += sizeof(_PATH_DEV) - 1;
3097 if (strcmp(name, path) == 0)
3102 for (c = 0; c < children; c++)
3103 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3110 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3112 boolean_t force = B_FALSE;
3115 char *poolname, *old_disk, *new_disk;
3116 zpool_handle_t *zhp;
3120 while ((c = getopt(argc, argv, "f")) != -1) {
3126 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3135 /* get pool name and check number of arguments */
3137 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3144 (void) fprintf(stderr,
3145 gettext("missing <device> specification\n"));
3153 (void) fprintf(stderr,
3154 gettext("missing <new_device> specification\n"));
3157 new_disk = old_disk;
3167 (void) fprintf(stderr, gettext("too many arguments\n"));
3171 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3174 if (zpool_get_config(zhp, NULL) == NULL) {
3175 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3181 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3183 if (nvroot == NULL) {
3188 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3190 nvlist_free(nvroot);
3197 * zpool replace [-f] <pool> <device> <new_device>
3199 * -f Force attach, even if <new_device> appears to be in use.
3201 * Replace <device> with <new_device>.
3205 zpool_do_replace(int argc, char **argv)
3207 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3211 * zpool attach [-f] <pool> <device> <new_device>
3213 * -f Force attach, even if <new_device> appears to be in use.
3215 * Attach <new_device> to the mirror containing <device>. If <device> is not
3216 * part of a mirror, then <device> will be transformed into a mirror of
3217 * <device> and <new_device>. In either case, <new_device> will begin life
3218 * with a DTL of [0, now], and will immediately begin to resilver itself.
3221 zpool_do_attach(int argc, char **argv)
3223 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3227 * zpool detach [-f] <pool> <device>
3229 * -f Force detach of <device>, even if DTLs argue against it
3230 * (not supported yet)
3232 * Detach a device from a mirror. The operation will be refused if <device>
3233 * is the last device in the mirror, or if the DTLs indicate that this device
3234 * has the only valid copy of some data.
3238 zpool_do_detach(int argc, char **argv)
3241 char *poolname, *path;
3242 zpool_handle_t *zhp;
3246 while ((c = getopt(argc, argv, "f")) != -1) {
3250 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3259 /* get pool name and check number of arguments */
3261 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3266 (void) fprintf(stderr,
3267 gettext("missing <device> specification\n"));
3274 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3277 ret = zpool_vdev_detach(zhp, path);
3285 * zpool split [-n] [-o prop=val] ...
3287 * [-R altroot] <pool> <newpool> [<device> ...]
3289 * -n Do not split the pool, but display the resulting layout if
3290 * it were to be split.
3291 * -o Set property=value, or set mount options.
3292 * -R Mount the split-off pool under an alternate root.
3294 * Splits the named pool and gives it the new pool name. Devices to be split
3295 * off may be listed, provided that no more than one device is specified
3296 * per top-level vdev mirror. The newly split pool is left in an exported
3297 * state unless -R is specified.
3299 * Restrictions: the top-level of the pool pool must only be made up of
3300 * mirrors; all devices in the pool must be healthy; no device may be
3301 * undergoing a resilvering operation.
3304 zpool_do_split(int argc, char **argv)
3306 char *srcpool, *newpool, *propval;
3307 char *mntopts = NULL;
3310 zpool_handle_t *zhp;
3311 nvlist_t *config, *props = NULL;
3313 flags.dryrun = B_FALSE;
3314 flags.import = B_FALSE;
3317 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3320 flags.import = B_TRUE;
3322 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3323 &props, B_TRUE) != 0) {
3330 flags.dryrun = B_TRUE;
3333 if ((propval = strchr(optarg, '=')) != NULL) {
3336 if (add_prop_list(optarg, propval,
3337 &props, B_TRUE) != 0) {
3347 (void) fprintf(stderr, gettext("missing argument for "
3348 "'%c' option\n"), optopt);
3352 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3359 if (!flags.import && mntopts != NULL) {
3360 (void) fprintf(stderr, gettext("setting mntopts is only "
3361 "valid when importing the pool\n"));
3369 (void) fprintf(stderr, gettext("Missing pool name\n"));
3373 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3383 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3386 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3387 if (config == NULL) {
3391 (void) printf(gettext("would create '%s' with the "
3392 "following layout:\n\n"), newpool);
3393 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3395 nvlist_free(config);
3400 if (ret != 0 || flags.dryrun || !flags.import)
3404 * The split was successful. Now we need to open the new
3405 * pool and import it.
3407 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3409 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3410 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3412 (void) fprintf(stderr, gettext("Split was successful, but "
3413 "the datasets could not all be mounted\n"));
3414 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3415 "different altroot\n"), "zpool import");
3425 * zpool online <pool> <device> ...
3428 zpool_do_online(int argc, char **argv)
3432 zpool_handle_t *zhp;
3434 vdev_state_t newstate;
3438 while ((c = getopt(argc, argv, "et")) != -1) {
3441 flags |= ZFS_ONLINE_EXPAND;
3445 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3454 /* get pool name and check number of arguments */
3456 (void) fprintf(stderr, gettext("missing pool name\n"));
3460 (void) fprintf(stderr, gettext("missing device name\n"));
3466 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3469 for (i = 1; i < argc; i++) {
3470 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3471 if (newstate != VDEV_STATE_HEALTHY) {
3472 (void) printf(gettext("warning: device '%s' "
3473 "onlined, but remains in faulted state\n"),
3475 if (newstate == VDEV_STATE_FAULTED)
3476 (void) printf(gettext("use 'zpool "
3477 "clear' to restore a faulted "
3480 (void) printf(gettext("use 'zpool "
3481 "replace' to replace devices "
3482 "that are no longer present\n"));
3495 * zpool offline [-ft] <pool> <device> ...
3497 * -f Force the device into the offline state, even if doing
3498 * so would appear to compromise pool availability.
3499 * (not supported yet)
3501 * -t Only take the device off-line temporarily. The offline
3502 * state will not be persistent across reboots.
3506 zpool_do_offline(int argc, char **argv)
3510 zpool_handle_t *zhp;
3512 boolean_t istmp = B_FALSE;
3515 while ((c = getopt(argc, argv, "ft")) != -1) {
3522 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3531 /* get pool name and check number of arguments */
3533 (void) fprintf(stderr, gettext("missing pool name\n"));
3537 (void) fprintf(stderr, gettext("missing device name\n"));
3543 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3546 for (i = 1; i < argc; i++) {
3547 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3557 * zpool clear <pool> [device]
3559 * Clear all errors associated with a pool or a particular device.
3562 zpool_do_clear(int argc, char **argv)
3566 boolean_t dryrun = B_FALSE;
3567 boolean_t do_rewind = B_FALSE;
3568 boolean_t xtreme_rewind = B_FALSE;
3569 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3570 nvlist_t *policy = NULL;
3571 zpool_handle_t *zhp;
3572 char *pool, *device;
3575 while ((c = getopt(argc, argv, "FnX")) != -1) {
3584 xtreme_rewind = B_TRUE;
3587 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3597 (void) fprintf(stderr, gettext("missing pool name\n"));
3602 (void) fprintf(stderr, gettext("too many arguments\n"));
3606 if ((dryrun || xtreme_rewind) && !do_rewind) {
3607 (void) fprintf(stderr,
3608 gettext("-n or -X only meaningful with -F\n"));
3612 rewind_policy = ZPOOL_TRY_REWIND;
3614 rewind_policy = ZPOOL_DO_REWIND;
3616 rewind_policy |= ZPOOL_EXTREME_REWIND;
3618 /* In future, further rewind policy choices can be passed along here */
3619 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3620 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3624 device = argc == 2 ? argv[1] : NULL;
3626 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3627 nvlist_free(policy);
3631 if (zpool_clear(zhp, device, policy) != 0)
3636 nvlist_free(policy);
3642 * zpool reguid <pool>
3645 zpool_do_reguid(int argc, char **argv)
3649 zpool_handle_t *zhp;
3653 while ((c = getopt(argc, argv, "")) != -1) {
3656 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3665 /* get pool name and check number of arguments */
3667 (void) fprintf(stderr, gettext("missing pool name\n"));
3672 (void) fprintf(stderr, gettext("too many arguments\n"));
3677 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3680 ret = zpool_reguid(zhp);
3688 * zpool reopen <pool>
3690 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3692 * NOTE: This command is currently undocumented. If the command is ever
3693 * exposed then the appropriate usage() messages will need to be made.
3696 zpool_do_reopen(int argc, char **argv)
3699 zpool_handle_t *zhp;
3709 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3712 ret = zpool_reopen(zhp);
3717 typedef struct scrub_cbdata {
3724 scrub_callback(zpool_handle_t *zhp, void *data)
3726 scrub_cbdata_t *cb = data;
3730 * Ignore faulted pools.
3732 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3733 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3734 "currently unavailable\n"), zpool_get_name(zhp));
3738 err = zpool_scan(zhp, cb->cb_type);
3744 * zpool scrub [-s] <pool> ...
3746 * -s Stop. Stops any in-progress scrub.
3749 zpool_do_scrub(int argc, char **argv)
3754 cb.cb_type = POOL_SCAN_SCRUB;
3757 while ((c = getopt(argc, argv, "s")) != -1) {
3760 cb.cb_type = POOL_SCAN_NONE;
3763 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3775 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3779 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3782 typedef struct status_cbdata {
3784 boolean_t cb_allpools;
3785 boolean_t cb_verbose;
3786 boolean_t cb_explain;
3788 boolean_t cb_dedup_stats;
3792 * Print out detailed scrub status.
3795 print_scan_status(pool_scan_stat_t *ps)
3798 uint64_t elapsed, mins_left, hours_left;
3799 uint64_t pass_exam, examined, total;
3801 double fraction_done;
3802 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3804 (void) printf(gettext(" scan: "));
3806 /* If there's never been a scan, there's not much to say. */
3807 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3808 ps->pss_func >= POOL_SCAN_FUNCS) {
3809 (void) printf(gettext("none requested\n"));
3813 start = ps->pss_start_time;
3814 end = ps->pss_end_time;
3815 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3817 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3818 ps->pss_func == POOL_SCAN_RESILVER);
3820 * Scan is finished or canceled.
3822 if (ps->pss_state == DSS_FINISHED) {
3823 uint64_t minutes_taken = (end - start) / 60;
3826 if (ps->pss_func == POOL_SCAN_SCRUB) {
3827 fmt = gettext("scrub repaired %s in %lluh%um with "
3828 "%llu errors on %s");
3829 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3830 fmt = gettext("resilvered %s in %lluh%um with "
3831 "%llu errors on %s");
3834 (void) printf(fmt, processed_buf,
3835 (u_longlong_t)(minutes_taken / 60),
3836 (uint_t)(minutes_taken % 60),
3837 (u_longlong_t)ps->pss_errors,
3838 ctime((time_t *)&end));
3840 } else if (ps->pss_state == DSS_CANCELED) {
3841 if (ps->pss_func == POOL_SCAN_SCRUB) {
3842 (void) printf(gettext("scrub canceled on %s"),
3844 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3845 (void) printf(gettext("resilver canceled on %s"),
3851 assert(ps->pss_state == DSS_SCANNING);
3854 * Scan is in progress.
3856 if (ps->pss_func == POOL_SCAN_SCRUB) {
3857 (void) printf(gettext("scrub in progress since %s"),
3859 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3860 (void) printf(gettext("resilver in progress since %s"),
3864 examined = ps->pss_examined ? ps->pss_examined : 1;
3865 total = ps->pss_to_examine;
3866 fraction_done = (double)examined / total;
3868 /* elapsed time for this pass */
3869 elapsed = time(NULL) - ps->pss_pass_start;
3870 elapsed = elapsed ? elapsed : 1;
3871 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3872 rate = pass_exam / elapsed;
3873 rate = rate ? rate : 1;
3874 mins_left = ((total - examined) / rate) / 60;
3875 hours_left = mins_left / 60;
3877 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3878 zfs_nicenum(total, total_buf, sizeof (total_buf));
3879 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3882 * do not print estimated time if hours_left is more than 30 days
3884 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3885 examined_buf, total_buf, rate_buf);
3886 if (hours_left < (30 * 24)) {
3887 (void) printf(gettext(", %lluh%um to go\n"),
3888 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3890 (void) printf(gettext(
3891 ", (scan is slow, no estimated time)\n"));
3894 if (ps->pss_func == POOL_SCAN_RESILVER) {
3895 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3896 processed_buf, 100 * fraction_done);
3897 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3898 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3899 processed_buf, 100 * fraction_done);
3904 print_error_log(zpool_handle_t *zhp)
3906 nvlist_t *nverrlist = NULL;
3909 size_t len = MAXPATHLEN * 2;
3911 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3912 (void) printf("errors: List of errors unavailable "
3913 "(insufficient privileges)\n");
3917 (void) printf("errors: Permanent errors have been "
3918 "detected in the following files:\n\n");
3920 pathname = safe_malloc(len);
3922 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3924 uint64_t dsobj, obj;
3926 verify(nvpair_value_nvlist(elem, &nv) == 0);
3927 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3929 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3931 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3932 (void) printf("%7s %s\n", "", pathname);
3935 nvlist_free(nverrlist);
3939 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3948 (void) printf(gettext("\tspares\n"));
3950 for (i = 0; i < nspares; i++) {
3951 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3952 print_status_config(zhp, name, spares[i],
3953 namewidth, 2, B_TRUE);
3959 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3968 (void) printf(gettext("\tcache\n"));
3970 for (i = 0; i < nl2cache; i++) {
3971 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3972 print_status_config(zhp, name, l2cache[i],
3973 namewidth, 2, B_FALSE);
3979 print_dedup_stats(nvlist_t *config)
3981 ddt_histogram_t *ddh;
3987 * If the pool was faulted then we may not have been able to
3988 * obtain the config. Otherwise, if have anything in the dedup
3989 * table continue processing the stats.
3991 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3992 (uint64_t **)&ddo, &c) != 0)
3995 (void) printf("\n");
3996 (void) printf(gettext(" dedup: "));
3997 if (ddo->ddo_count == 0) {
3998 (void) printf(gettext("no DDT entries\n"));
4002 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4003 (u_longlong_t)ddo->ddo_count,
4004 (u_longlong_t)ddo->ddo_dspace,
4005 (u_longlong_t)ddo->ddo_mspace);
4007 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4008 (uint64_t **)&dds, &c) == 0);
4009 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4010 (uint64_t **)&ddh, &c) == 0);
4011 zpool_dump_ddt(dds, ddh);
4015 * Display a summary of pool status. Displays a summary such as:
4019 * reason: One or more devices ...
4020 * see: http://illumos.org/msg/ZFS-xxxx-01
4026 * When given the '-v' option, we print out the complete config. If the '-e'
4027 * option is specified, then we print out error rate information as well.
4030 status_callback(zpool_handle_t *zhp, void *data)
4032 status_cbdata_t *cbp = data;
4033 nvlist_t *config, *nvroot;
4040 config = zpool_get_config(zhp, NULL);
4041 reason = zpool_get_status(zhp, &msgid);
4046 * If we were given 'zpool status -x', only report those pools with
4049 if (cbp->cb_explain &&
4050 (reason == ZPOOL_STATUS_OK ||
4051 reason == ZPOOL_STATUS_VERSION_OLDER ||
4052 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4053 if (!cbp->cb_allpools) {
4054 (void) printf(gettext("pool '%s' is healthy\n"),
4055 zpool_get_name(zhp));
4057 cbp->cb_first = B_FALSE;
4063 cbp->cb_first = B_FALSE;
4065 (void) printf("\n");
4067 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4069 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4070 (uint64_t **)&vs, &c) == 0);
4071 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4073 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4074 (void) printf(gettext(" state: %s\n"), health);
4077 case ZPOOL_STATUS_MISSING_DEV_R:
4078 (void) printf(gettext("status: One or more devices could not "
4079 "be opened. Sufficient replicas exist for\n\tthe pool to "
4080 "continue functioning in a degraded state.\n"));
4081 (void) printf(gettext("action: Attach the missing device and "
4082 "online it using 'zpool online'.\n"));
4085 case ZPOOL_STATUS_MISSING_DEV_NR:
4086 (void) printf(gettext("status: One or more devices could not "
4087 "be opened. There are insufficient\n\treplicas for the "
4088 "pool to continue functioning.\n"));
4089 (void) printf(gettext("action: Attach the missing device and "
4090 "online it using 'zpool online'.\n"));
4093 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4094 (void) printf(gettext("status: One or more devices could not "
4095 "be used because the label is missing or\n\tinvalid. "
4096 "Sufficient replicas exist for the pool to continue\n\t"
4097 "functioning in a degraded state.\n"));
4098 (void) printf(gettext("action: Replace the device using "
4099 "'zpool replace'.\n"));
4102 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4103 (void) printf(gettext("status: One or more devices could not "
4104 "be used because the label is missing \n\tor invalid. "
4105 "There are insufficient replicas for the pool to "
4106 "continue\n\tfunctioning.\n"));
4107 zpool_explain_recover(zpool_get_handle(zhp),
4108 zpool_get_name(zhp), reason, config);
4111 case ZPOOL_STATUS_FAILING_DEV:
4112 (void) printf(gettext("status: One or more devices has "
4113 "experienced an unrecoverable error. An\n\tattempt was "
4114 "made to correct the error. Applications are "
4116 (void) printf(gettext("action: Determine if the device needs "
4117 "to be replaced, and clear the errors\n\tusing "
4118 "'zpool clear' or replace the device with 'zpool "
4122 case ZPOOL_STATUS_OFFLINE_DEV:
4123 (void) printf(gettext("status: One or more devices has "
4124 "been taken offline 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_REMOVED_DEV:
4133 (void) printf(gettext("status: One or more devices has "
4134 "been removed by the administrator.\n\tSufficient "
4135 "replicas exist for the pool to continue functioning in "
4136 "a\n\tdegraded state.\n"));
4137 (void) printf(gettext("action: Online the device using "
4138 "'zpool online' or replace the device with\n\t'zpool "
4142 case ZPOOL_STATUS_RESILVERING:
4143 (void) printf(gettext("status: One or more devices is "
4144 "currently being resilvered. The pool will\n\tcontinue "
4145 "to function, possibly in a degraded state.\n"));
4146 (void) printf(gettext("action: Wait for the resilver to "
4150 case ZPOOL_STATUS_CORRUPT_DATA:
4151 (void) printf(gettext("status: One or more devices has "
4152 "experienced an error resulting in data\n\tcorruption. "
4153 "Applications may be affected.\n"));
4154 (void) printf(gettext("action: Restore the file in question "
4155 "if possible. Otherwise restore the\n\tentire pool from "
4159 case ZPOOL_STATUS_CORRUPT_POOL:
4160 (void) printf(gettext("status: The pool metadata is corrupted "
4161 "and the pool cannot be opened.\n"));
4162 zpool_explain_recover(zpool_get_handle(zhp),
4163 zpool_get_name(zhp), reason, config);
4166 case ZPOOL_STATUS_VERSION_OLDER:
4167 (void) printf(gettext("status: The pool is formatted using a "
4168 "legacy on-disk format. The pool can\n\tstill be used, "
4169 "but some features are unavailable.\n"));
4170 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4171 "upgrade'. Once this is done, the\n\tpool will no longer "
4172 "be accessible on software that does not support feature\n"
4176 case ZPOOL_STATUS_VERSION_NEWER:
4177 (void) printf(gettext("status: The pool has been upgraded to a "
4178 "newer, incompatible on-disk version.\n\tThe pool cannot "
4179 "be accessed on this system.\n"));
4180 (void) printf(gettext("action: Access the pool from a system "
4181 "running more recent software, or\n\trestore the pool from "
4185 case ZPOOL_STATUS_FEAT_DISABLED:
4186 (void) printf(gettext("status: Some supported features are not "
4187 "enabled on the pool. The pool can\n\tstill be used, but "
4188 "some features are unavailable.\n"));
4189 (void) printf(gettext("action: Enable all features using "
4190 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4191 "longer be accessible by software that does not support\n\t"
4192 "the features. See zpool-features(7) for details.\n"));
4195 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4196 (void) printf(gettext("status: The pool cannot be accessed on "
4197 "this system because it uses the\n\tfollowing feature(s) "
4198 "not supported on this system:\n"));
4199 zpool_print_unsup_feat(config);
4200 (void) printf("\n");
4201 (void) printf(gettext("action: Access the pool from a system "
4202 "that supports the required feature(s),\n\tor restore the "
4203 "pool from backup.\n"));
4206 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4207 (void) printf(gettext("status: The pool can only be accessed "
4208 "in read-only mode on this system. It\n\tcannot be "
4209 "accessed in read-write mode because it uses the "
4210 "following\n\tfeature(s) not supported on this system:\n"));
4211 zpool_print_unsup_feat(config);
4212 (void) printf("\n");
4213 (void) printf(gettext("action: The pool cannot be accessed in "
4214 "read-write mode. Import the pool with\n"
4215 "\t\"-o readonly=on\", access the pool from a system that "
4216 "supports the\n\trequired feature(s), or restore the "
4217 "pool from backup.\n"));
4220 case ZPOOL_STATUS_FAULTED_DEV_R:
4221 (void) printf(gettext("status: One or more devices are "
4222 "faulted in response to persistent errors.\n\tSufficient "
4223 "replicas exist for the pool to continue functioning "
4224 "in a\n\tdegraded state.\n"));
4225 (void) printf(gettext("action: Replace the faulted device, "
4226 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4229 case ZPOOL_STATUS_FAULTED_DEV_NR:
4230 (void) printf(gettext("status: One or more devices are "
4231 "faulted in response to persistent errors. There are "
4232 "insufficient replicas for the pool to\n\tcontinue "
4234 (void) printf(gettext("action: Destroy and re-create the pool "
4235 "from a backup source. Manually marking the device\n"
4236 "\trepaired using 'zpool clear' may allow some data "
4237 "to be recovered.\n"));
4240 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4241 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4242 (void) printf(gettext("status: One or more devices are "
4243 "faulted in response to IO failures.\n"));
4244 (void) printf(gettext("action: Make sure the affected devices "
4245 "are connected, then run 'zpool clear'.\n"));
4248 case ZPOOL_STATUS_BAD_LOG:
4249 (void) printf(gettext("status: An intent log record "
4250 "could not be read.\n"
4251 "\tWaiting for adminstrator intervention to fix the "
4252 "faulted pool.\n"));
4253 (void) printf(gettext("action: Either restore the affected "
4254 "device(s) and run 'zpool online',\n"
4255 "\tor ignore the intent log records by running "
4256 "'zpool clear'.\n"));
4261 * The remaining errors can't actually be generated, yet.
4263 assert(reason == ZPOOL_STATUS_OK);
4267 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4270 if (config != NULL) {
4273 nvlist_t **spares, **l2cache;
4274 uint_t nspares, nl2cache;
4275 pool_scan_stat_t *ps = NULL;
4277 (void) nvlist_lookup_uint64_array(nvroot,
4278 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4279 print_scan_status(ps);
4281 namewidth = max_width(zhp, nvroot, 0, 0);
4285 (void) printf(gettext("config:\n\n"));
4286 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4287 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4288 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4289 namewidth, 0, B_FALSE);
4291 if (num_logs(nvroot) > 0)
4292 print_logs(zhp, nvroot, namewidth, B_TRUE);
4293 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4294 &l2cache, &nl2cache) == 0)
4295 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4297 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4298 &spares, &nspares) == 0)
4299 print_spares(zhp, spares, nspares, namewidth);
4301 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4303 nvlist_t *nverrlist = NULL;
4306 * If the approximate error count is small, get a
4307 * precise count by fetching the entire log and
4308 * uniquifying the results.
4310 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4311 zpool_get_errlog(zhp, &nverrlist) == 0) {
4316 while ((elem = nvlist_next_nvpair(nverrlist,
4321 nvlist_free(nverrlist);
4323 (void) printf("\n");
4326 (void) printf(gettext("errors: No known data "
4328 else if (!cbp->cb_verbose)
4329 (void) printf(gettext("errors: %llu data "
4330 "errors, use '-v' for a list\n"),
4331 (u_longlong_t)nerr);
4333 print_error_log(zhp);
4336 if (cbp->cb_dedup_stats)
4337 print_dedup_stats(config);
4339 (void) printf(gettext("config: The configuration cannot be "
4347 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4349 * -v Display complete error logs
4350 * -x Display only pools with potential problems
4351 * -D Display dedup status (undocumented)
4352 * -T Display a timestamp in date(1) or Unix format
4354 * Describes the health status of all pools or some subset.
4357 zpool_do_status(int argc, char **argv)
4361 unsigned long interval = 0, count = 0;
4362 status_cbdata_t cb = { 0 };
4365 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4368 cb.cb_verbose = B_TRUE;
4371 cb.cb_explain = B_TRUE;
4374 cb.cb_dedup_stats = B_TRUE;
4377 get_timestamp_arg(*optarg);
4380 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4389 get_interval_count(&argc, argv, &interval, &count);
4392 cb.cb_allpools = B_TRUE;
4394 cb.cb_first = B_TRUE;
4397 if (timestamp_fmt != NODATE)
4398 print_timestamp(timestamp_fmt);
4400 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4401 status_callback, &cb);
4403 if (argc == 0 && cb.cb_count == 0)
4404 (void) printf(gettext("no pools available\n"));
4405 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4406 (void) printf(gettext("all pools are healthy\n"));
4414 if (count != 0 && --count == 0)
4417 (void) sleep(interval);
4423 typedef struct upgrade_cbdata {
4425 char cb_poolname[ZPOOL_MAXNAMELEN];
4427 uint64_t cb_version;
4433 is_root_pool(zpool_handle_t *zhp)
4435 static struct statfs sfs;
4436 static char *poolname = NULL;
4437 static boolean_t stated = B_FALSE;
4442 if (statfs("/", &sfs) == -1) {
4443 (void) fprintf(stderr,
4444 "Unable to stat root file system: %s.\n",
4448 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4450 poolname = sfs.f_mntfromname;
4451 if ((slash = strchr(poolname, '/')) != NULL)
4454 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4458 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4460 if (poolname[0] == '\0' && is_root_pool(zhp))
4461 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4463 #endif /* FreeBSD */
4466 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4470 uint64_t oldversion;
4472 config = zpool_get_config(zhp, NULL);
4473 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4476 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4477 assert(oldversion < version);
4479 ret = zpool_upgrade(zhp, version);
4483 if (version >= SPA_VERSION_FEATURES) {
4484 (void) printf(gettext("Successfully upgraded "
4485 "'%s' from version %llu to feature flags.\n"),
4486 zpool_get_name(zhp), oldversion);
4488 (void) printf(gettext("Successfully upgraded "
4489 "'%s' from version %llu to version %llu.\n"),
4490 zpool_get_name(zhp), oldversion, version);
4497 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4500 boolean_t firstff = B_TRUE;
4501 nvlist_t *enabled = zpool_get_features(zhp);
4504 for (i = 0; i < SPA_FEATURES; i++) {
4505 const char *fname = spa_feature_table[i].fi_uname;
4506 const char *fguid = spa_feature_table[i].fi_guid;
4507 if (!nvlist_exists(enabled, fguid)) {
4509 verify(-1 != asprintf(&propname, "feature@%s", fname));
4510 ret = zpool_set_prop(zhp, propname,
4511 ZFS_FEATURE_ENABLED);
4519 (void) printf(gettext("Enabled the "
4520 "following features on '%s':\n"),
4521 zpool_get_name(zhp));
4524 (void) printf(gettext(" %s\n"), fname);
4535 upgrade_cb(zpool_handle_t *zhp, void *arg)
4537 upgrade_cbdata_t *cbp = arg;
4540 boolean_t printnl = B_FALSE;
4543 config = zpool_get_config(zhp, NULL);
4544 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4547 assert(SPA_VERSION_IS_SUPPORTED(version));
4549 if (version < cbp->cb_version) {
4550 cbp->cb_first = B_FALSE;
4551 ret = upgrade_version(zhp, cbp->cb_version);
4555 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4556 sizeof(cbp->cb_poolname));
4557 #endif /* ___FreeBSD__ */
4562 * If they did "zpool upgrade -a", then we could
4563 * be doing ioctls to different pools. We need
4564 * to log this history once to each pool, and bypass
4565 * the normal history logging that happens in main().
4567 (void) zpool_log_history(g_zfs, history_str);
4568 log_history = B_FALSE;
4572 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4574 ret = upgrade_enable_all(zhp, &count);
4579 cbp->cb_first = B_FALSE;
4582 * If they did "zpool upgrade -a", then we could
4583 * be doing ioctls to different pools. We need
4584 * to log this history once to each pool, and bypass
4585 * the normal history logging that happens in main().
4587 (void) zpool_log_history(g_zfs, history_str);
4588 log_history = B_FALSE;
4593 (void) printf(gettext("\n"));
4600 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4602 upgrade_cbdata_t *cbp = arg;
4606 config = zpool_get_config(zhp, NULL);
4607 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4610 assert(SPA_VERSION_IS_SUPPORTED(version));
4612 if (version < SPA_VERSION_FEATURES) {
4613 if (cbp->cb_first) {
4614 (void) printf(gettext("The following pools are "
4615 "formatted with legacy version numbers and can\n"
4616 "be upgraded to use feature flags. After "
4617 "being upgraded, these pools\nwill no "
4618 "longer be accessible by software that does not "
4619 "support feature\nflags.\n\n"));
4620 (void) printf(gettext("VER POOL\n"));
4621 (void) printf(gettext("--- ------------\n"));
4622 cbp->cb_first = B_FALSE;
4625 (void) printf("%2llu %s\n", (u_longlong_t)version,
4626 zpool_get_name(zhp));
4633 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4635 upgrade_cbdata_t *cbp = arg;
4639 config = zpool_get_config(zhp, NULL);
4640 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4643 if (version >= SPA_VERSION_FEATURES) {
4645 boolean_t poolfirst = B_TRUE;
4646 nvlist_t *enabled = zpool_get_features(zhp);
4648 for (i = 0; i < SPA_FEATURES; i++) {
4649 const char *fguid = spa_feature_table[i].fi_guid;
4650 const char *fname = spa_feature_table[i].fi_uname;
4651 if (!nvlist_exists(enabled, fguid)) {
4652 if (cbp->cb_first) {
4653 (void) printf(gettext("\nSome "
4654 "supported features are not "
4655 "enabled on the following pools. "
4656 "Once a\nfeature is enabled the "
4657 "pool may become incompatible with "
4658 "software\nthat does not support "
4660 "zpool-features(7) for "
4662 (void) printf(gettext("POOL "
4664 (void) printf(gettext("------"
4666 cbp->cb_first = B_FALSE;
4670 (void) printf(gettext("%s\n"),
4671 zpool_get_name(zhp));
4672 poolfirst = B_FALSE;
4675 (void) printf(gettext(" %s\n"), fname);
4685 upgrade_one(zpool_handle_t *zhp, void *data)
4687 boolean_t printnl = B_FALSE;
4688 upgrade_cbdata_t *cbp = data;
4689 uint64_t cur_version;
4692 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4693 (void) printf(gettext("'log' is now a reserved word\n"
4694 "Pool 'log' must be renamed using export and import"
4699 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4700 if (cur_version > cbp->cb_version) {
4701 (void) printf(gettext("Pool '%s' is already formatted "
4702 "using more current version '%llu'.\n\n"),
4703 zpool_get_name(zhp), cur_version);
4707 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4708 (void) printf(gettext("Pool '%s' is already formatted "
4709 "using version %llu.\n\n"), zpool_get_name(zhp),
4714 if (cur_version != cbp->cb_version) {
4716 ret = upgrade_version(zhp, cbp->cb_version);
4720 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4721 sizeof(cbp->cb_poolname));
4722 #endif /* ___FreeBSD__ */
4725 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4727 ret = upgrade_enable_all(zhp, &count);
4734 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4735 sizeof(cbp->cb_poolname));
4736 #endif /* __FreeBSD __*/
4737 } else if (cur_version == SPA_VERSION) {
4738 (void) printf(gettext("Pool '%s' already has all "
4739 "supported features enabled.\n"),
4740 zpool_get_name(zhp));
4745 (void) printf(gettext("\n"));
4754 * zpool upgrade [-V version] <-a | pool ...>
4756 * With no arguments, display downrev'd ZFS pool available for upgrade.
4757 * Individual pools can be upgraded by specifying the pool, and '-a' will
4758 * upgrade all pools.
4761 zpool_do_upgrade(int argc, char **argv)
4764 upgrade_cbdata_t cb = { 0 };
4766 boolean_t showversions = B_FALSE;
4767 boolean_t upgradeall = B_FALSE;
4772 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4775 upgradeall = B_TRUE;
4778 showversions = B_TRUE;
4781 cb.cb_version = strtoll(optarg, &end, 10);
4783 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4784 (void) fprintf(stderr,
4785 gettext("invalid version '%s'\n"), optarg);
4790 (void) fprintf(stderr, gettext("missing argument for "
4791 "'%c' option\n"), optopt);
4795 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4806 if (cb.cb_version == 0) {
4807 cb.cb_version = SPA_VERSION;
4808 } else if (!upgradeall && argc == 0) {
4809 (void) fprintf(stderr, gettext("-V option is "
4810 "incompatible with other arguments\n"));
4815 if (upgradeall || argc != 0) {
4816 (void) fprintf(stderr, gettext("-v option is "
4817 "incompatible with other arguments\n"));
4820 } else if (upgradeall) {
4822 (void) fprintf(stderr, gettext("-a option should not "
4823 "be used along with a pool name\n"));
4828 (void) printf(gettext("This system supports ZFS pool feature "
4833 (void) printf(gettext("The following features are "
4835 (void) printf(gettext("FEAT DESCRIPTION\n"));
4836 (void) printf("----------------------------------------------"
4837 "---------------\n");
4838 for (i = 0; i < SPA_FEATURES; i++) {
4839 zfeature_info_t *fi = &spa_feature_table[i];
4840 const char *ro = fi->fi_can_readonly ?
4841 " (read-only compatible)" : "";
4843 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4844 (void) printf(" %s\n", fi->fi_desc);
4846 (void) printf("\n");
4848 (void) printf(gettext("The following legacy versions are also "
4850 (void) printf(gettext("VER DESCRIPTION\n"));
4851 (void) printf("--- -----------------------------------------"
4852 "---------------\n");
4853 (void) printf(gettext(" 1 Initial ZFS version\n"));
4854 (void) printf(gettext(" 2 Ditto blocks "
4855 "(replicated metadata)\n"));
4856 (void) printf(gettext(" 3 Hot spares and double parity "
4858 (void) printf(gettext(" 4 zpool history\n"));
4859 (void) printf(gettext(" 5 Compression using the gzip "
4861 (void) printf(gettext(" 6 bootfs pool property\n"));
4862 (void) printf(gettext(" 7 Separate intent log devices\n"));
4863 (void) printf(gettext(" 8 Delegated administration\n"));
4864 (void) printf(gettext(" 9 refquota and refreservation "
4866 (void) printf(gettext(" 10 Cache devices\n"));
4867 (void) printf(gettext(" 11 Improved scrub performance\n"));
4868 (void) printf(gettext(" 12 Snapshot properties\n"));
4869 (void) printf(gettext(" 13 snapused property\n"));
4870 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4871 (void) printf(gettext(" 15 user/group space accounting\n"));
4872 (void) printf(gettext(" 16 stmf property support\n"));
4873 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4874 (void) printf(gettext(" 18 Snapshot user holds\n"));
4875 (void) printf(gettext(" 19 Log device removal\n"));
4876 (void) printf(gettext(" 20 Compression using zle "
4877 "(zero-length encoding)\n"));
4878 (void) printf(gettext(" 21 Deduplication\n"));
4879 (void) printf(gettext(" 22 Received properties\n"));
4880 (void) printf(gettext(" 23 Slim ZIL\n"));
4881 (void) printf(gettext(" 24 System attributes\n"));
4882 (void) printf(gettext(" 25 Improved scrub stats\n"));
4883 (void) printf(gettext(" 26 Improved snapshot deletion "
4885 (void) printf(gettext(" 27 Improved snapshot creation "
4887 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4888 (void) printf(gettext("\nFor more information on a particular "
4889 "version, including supported releases,\n"));
4890 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4891 } else if (argc == 0 && upgradeall) {
4892 cb.cb_first = B_TRUE;
4893 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4894 if (ret == 0 && cb.cb_first) {
4895 if (cb.cb_version == SPA_VERSION) {
4896 (void) printf(gettext("All pools are already "
4897 "formatted using feature flags.\n\n"));
4898 (void) printf(gettext("Every feature flags "
4899 "pool already has all supported features "
4902 (void) printf(gettext("All pools are already "
4903 "formatted with version %llu or higher.\n"),
4907 } else if (argc == 0) {
4908 cb.cb_first = B_TRUE;
4909 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4913 (void) printf(gettext("All pools are formatted "
4914 "using feature flags.\n\n"));
4916 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4917 "for a list of available legacy versions.\n"));
4920 cb.cb_first = B_TRUE;
4921 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4925 (void) printf(gettext("Every feature flags pool has "
4926 "all supported features enabled.\n"));
4928 (void) printf(gettext("\n"));
4931 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4935 if (cb.cb_poolname[0] != '\0') {
4937 "If you boot from pool '%s', don't forget to update boot code.\n"
4938 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4939 "the following command will do it:\n"
4941 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4948 typedef struct hist_cbdata {
4955 * Print out the command history for a specific pool.
4958 get_history_one(zpool_handle_t *zhp, void *data)
4964 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4966 cb->first = B_FALSE;
4968 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4970 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4973 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4974 &records, &numrecords) == 0);
4975 for (i = 0; i < numrecords; i++) {
4976 nvlist_t *rec = records[i];
4979 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
4983 tsec = fnvlist_lookup_uint64(records[i],
4985 (void) localtime_r(&tsec, &t);
4986 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4989 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
4990 (void) printf("%s %s", tbuf,
4991 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
4992 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
4994 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
4997 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
4998 (void) printf("%s unrecognized record:\n",
5000 dump_nvlist(rec, 4);
5003 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5004 zfs_history_event_names[ievent],
5005 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5006 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5007 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5010 (void) printf("%s [txg:%lld] %s", tbuf,
5011 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5012 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5013 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5014 (void) printf(" %s (%llu)",
5015 fnvlist_lookup_string(rec,
5017 fnvlist_lookup_uint64(rec,
5020 (void) printf(" %s", fnvlist_lookup_string(rec,
5021 ZPOOL_HIST_INT_STR));
5022 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5025 (void) printf("%s ioctl %s\n", tbuf,
5026 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5027 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5028 (void) printf(" input:\n");
5029 dump_nvlist(fnvlist_lookup_nvlist(rec,
5030 ZPOOL_HIST_INPUT_NVL), 8);
5032 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5033 (void) printf(" output:\n");
5034 dump_nvlist(fnvlist_lookup_nvlist(rec,
5035 ZPOOL_HIST_OUTPUT_NVL), 8);
5040 (void) printf("%s unrecognized record:\n", tbuf);
5041 dump_nvlist(rec, 4);
5045 (void) printf("\n");
5048 (void) printf(" [");
5049 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5050 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5051 struct passwd *pwd = getpwuid(who);
5052 (void) printf("user %d ", (int)who);
5054 (void) printf("(%s) ", pwd->pw_name);
5056 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5057 (void) printf("on %s",
5058 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5060 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5061 (void) printf(":%s",
5062 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5065 (void) printf("\n");
5067 (void) printf("\n");
5074 * zpool history <pool>
5076 * Displays the history of commands that modified pools.
5079 zpool_do_history(int argc, char **argv)
5081 hist_cbdata_t cbdata = { 0 };
5085 cbdata.first = B_TRUE;
5087 while ((c = getopt(argc, argv, "li")) != -1) {
5090 cbdata.longfmt = B_TRUE;
5093 cbdata.internal = B_TRUE;
5096 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5104 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5107 if (argc == 0 && cbdata.first == B_TRUE) {
5108 (void) printf(gettext("no pools available\n"));
5116 get_callback(zpool_handle_t *zhp, void *data)
5118 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5119 char value[MAXNAMELEN];
5120 zprop_source_t srctype;
5123 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5126 * Skip the special fake placeholder. This will also skip
5127 * over the name property when 'all' is specified.
5129 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5130 pl == cbp->cb_proplist)
5133 if (pl->pl_prop == ZPROP_INVAL &&
5134 (zpool_prop_feature(pl->pl_user_prop) ||
5135 zpool_prop_unsupported(pl->pl_user_prop))) {
5136 srctype = ZPROP_SRC_LOCAL;
5138 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5139 value, sizeof (value)) == 0) {
5140 zprop_print_one_property(zpool_get_name(zhp),
5141 cbp, pl->pl_user_prop, value, srctype,
5145 if (zpool_get_prop(zhp, pl->pl_prop, value,
5146 sizeof (value), &srctype) != 0)
5149 zprop_print_one_property(zpool_get_name(zhp), cbp,
5150 zpool_prop_to_name(pl->pl_prop), value, srctype,
5158 zpool_do_get(int argc, char **argv)
5160 zprop_get_cbdata_t cb = { 0 };
5161 zprop_list_t fake_name = { 0 };
5165 (void) fprintf(stderr, gettext("missing property "
5170 cb.cb_first = B_TRUE;
5171 cb.cb_sources = ZPROP_SRC_ALL;
5172 cb.cb_columns[0] = GET_COL_NAME;
5173 cb.cb_columns[1] = GET_COL_PROPERTY;
5174 cb.cb_columns[2] = GET_COL_VALUE;
5175 cb.cb_columns[3] = GET_COL_SOURCE;
5176 cb.cb_type = ZFS_TYPE_POOL;
5178 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5179 ZFS_TYPE_POOL) != 0)
5182 if (cb.cb_proplist != NULL) {
5183 fake_name.pl_prop = ZPOOL_PROP_NAME;
5184 fake_name.pl_width = strlen(gettext("NAME"));
5185 fake_name.pl_next = cb.cb_proplist;
5186 cb.cb_proplist = &fake_name;
5189 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5192 if (cb.cb_proplist == &fake_name)
5193 zprop_free_list(fake_name.pl_next);
5195 zprop_free_list(cb.cb_proplist);
5200 typedef struct set_cbdata {
5203 boolean_t cb_any_successful;
5207 set_callback(zpool_handle_t *zhp, void *data)
5210 set_cbdata_t *cb = (set_cbdata_t *)data;
5212 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5215 cb->cb_any_successful = B_TRUE;
5221 zpool_do_set(int argc, char **argv)
5223 set_cbdata_t cb = { 0 };
5226 if (argc > 1 && argv[1][0] == '-') {
5227 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5233 (void) fprintf(stderr, gettext("missing property=value "
5239 (void) fprintf(stderr, gettext("missing pool name\n"));
5244 (void) fprintf(stderr, gettext("too many pool names\n"));
5248 cb.cb_propname = argv[1];
5249 cb.cb_value = strchr(cb.cb_propname, '=');
5250 if (cb.cb_value == NULL) {
5251 (void) fprintf(stderr, gettext("missing value in "
5252 "property=value argument\n"));
5256 *(cb.cb_value) = '\0';
5259 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5266 find_command_idx(char *command, int *idx)
5270 for (i = 0; i < NCOMMAND; i++) {
5271 if (command_table[i].name == NULL)
5274 if (strcmp(command, command_table[i].name) == 0) {
5283 main(int argc, char **argv)
5289 (void) setlocale(LC_ALL, "");
5290 (void) textdomain(TEXT_DOMAIN);
5292 if ((g_zfs = libzfs_init()) == NULL) {
5293 (void) fprintf(stderr, gettext("internal error: failed to "
5294 "initialize ZFS library\n"));
5298 libzfs_print_on_error(g_zfs, B_TRUE);
5303 * Make sure the user has specified some command.
5306 (void) fprintf(stderr, gettext("missing command\n"));
5315 if (strcmp(cmdname, "-?") == 0)
5318 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5321 * Run the appropriate command.
5323 if (find_command_idx(cmdname, &i) == 0) {
5324 current_command = &command_table[i];
5325 ret = command_table[i].func(argc - 1, argv + 1);
5326 } else if (strchr(cmdname, '=')) {
5327 verify(find_command_idx("set", &i) == 0);
5328 current_command = &command_table[i];
5329 ret = command_table[i].func(argc, argv);
5330 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5332 * 'freeze' is a vile debugging abomination, so we treat
5336 int fd = open(ZFS_DEV, O_RDWR);
5337 (void) strcpy((void *)buf, argv[2]);
5338 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5340 (void) fprintf(stderr, gettext("unrecognized "
5341 "command '%s'\n"), cmdname);
5345 if (ret == 0 && log_history)
5346 (void) zpool_log_history(g_zfs, history_str);
5351 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5352 * for the purposes of running ::findleaks.
5354 if (getenv("ZFS_ABORT") != NULL) {
5355 (void) printf("dumping core by request\n");