4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
50 #include <sys/fs/zfs.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 #include "statcommon.h"
61 static int zpool_do_create(int, char **);
62 static int zpool_do_destroy(int, char **);
64 static int zpool_do_add(int, char **);
65 static int zpool_do_remove(int, char **);
66 static int zpool_do_labelclear(int, char **);
68 static int zpool_do_list(int, char **);
69 static int zpool_do_iostat(int, char **);
70 static int zpool_do_status(int, char **);
72 static int zpool_do_online(int, char **);
73 static int zpool_do_offline(int, char **);
74 static int zpool_do_clear(int, char **);
75 static int zpool_do_reopen(int, char **);
77 static int zpool_do_reguid(int, char **);
79 static int zpool_do_attach(int, char **);
80 static int zpool_do_detach(int, char **);
81 static int zpool_do_replace(int, char **);
82 static int zpool_do_split(int, char **);
84 static int zpool_do_scrub(int, char **);
86 static int zpool_do_import(int, char **);
87 static int zpool_do_export(int, char **);
89 static int zpool_do_upgrade(int, char **);
91 static int zpool_do_history(int, char **);
93 static int zpool_do_get(int, char **);
94 static int zpool_do_set(int, char **);
97 * These libumem hooks provide a reasonable set of defaults for the allocator's
98 * debugging facilities.
103 _umem_debug_init(void)
105 return ("default,verbose"); /* $UMEM_DEBUG setting */
109 _umem_logging_init(void)
111 return ("fail,contents"); /* $UMEM_LOGGING setting */
143 typedef struct zpool_command {
145 int (*func)(int, char **);
150 * Master command table. Each ZFS command has a name, associated function, and
151 * usage message. The usage messages need to be internationalized, so we have
152 * to have a function to return the usage message based on a command index.
154 * These commands are organized according to how they are displayed in the usage
155 * message. An empty command (one with a NULL name) indicates an empty line in
156 * the generic usage message.
158 static zpool_command_t command_table[] = {
159 { "create", zpool_do_create, HELP_CREATE },
160 { "destroy", zpool_do_destroy, HELP_DESTROY },
162 { "add", zpool_do_add, HELP_ADD },
163 { "remove", zpool_do_remove, HELP_REMOVE },
165 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
167 { "list", zpool_do_list, HELP_LIST },
168 { "iostat", zpool_do_iostat, HELP_IOSTAT },
169 { "status", zpool_do_status, HELP_STATUS },
171 { "online", zpool_do_online, HELP_ONLINE },
172 { "offline", zpool_do_offline, HELP_OFFLINE },
173 { "clear", zpool_do_clear, HELP_CLEAR },
174 { "reopen", zpool_do_reopen, HELP_REOPEN },
176 { "attach", zpool_do_attach, HELP_ATTACH },
177 { "detach", zpool_do_detach, HELP_DETACH },
178 { "replace", zpool_do_replace, HELP_REPLACE },
179 { "split", zpool_do_split, HELP_SPLIT },
181 { "scrub", zpool_do_scrub, HELP_SCRUB },
183 { "import", zpool_do_import, HELP_IMPORT },
184 { "export", zpool_do_export, HELP_EXPORT },
185 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
186 { "reguid", zpool_do_reguid, HELP_REGUID },
188 { "history", zpool_do_history, HELP_HISTORY },
189 { "get", zpool_do_get, HELP_GET },
190 { "set", zpool_do_set, HELP_SET },
193 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
195 zpool_command_t *current_command;
196 static char history_str[HIS_MAX_RECORD_LEN];
198 static uint_t timestamp_fmt = NODATE;
201 get_usage(zpool_help_t idx) {
204 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
206 return (gettext("\tattach [-f] <pool> <device> "
209 return (gettext("\tclear [-nF] <pool> [device]\n"));
211 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
212 "\t [-O file-system-property=value] ... \n"
213 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
215 return (gettext("\tdestroy [-f] <pool>\n"));
217 return (gettext("\tdetach <pool> <device>\n"));
219 return (gettext("\texport [-f] <pool> ...\n"));
221 return (gettext("\thistory [-il] [<pool>] ...\n"));
223 return (gettext("\timport [-d dir] [-D]\n"
224 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
225 "\timport [-o mntopts] [-o property=value] ... \n"
226 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
227 "[-R root] [-F [-n]] -a\n"
228 "\timport [-o mntopts] [-o property=value] ... \n"
229 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
230 "[-R root] [-F [-n]]\n"
231 "\t <pool | id> [newpool]\n"));
233 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
235 case HELP_LABELCLEAR:
236 return (gettext("\tlabelclear [-f] <vdev>\n"));
238 return (gettext("\tlist [-Hv] [-o property[,...]] "
239 "[-T d|u] [pool] ... [interval [count]]\n"));
241 return (gettext("\toffline [-t] <pool> <device> ...\n"));
243 return (gettext("\tonline [-e] <pool> <device> ...\n"));
245 return (gettext("\treplace [-f] <pool> <device> "
248 return (gettext("\tremove <pool> <device> ...\n"));
250 return (""); /* Undocumented command */
252 return (gettext("\tscrub [-s] <pool> ...\n"));
254 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
257 return (gettext("\tupgrade [-v]\n"
258 "\tupgrade [-V version] <-a | pool ...>\n"));
260 return (gettext("\tget <\"all\" | property[,...]> "
263 return (gettext("\tset <property=value> <pool> \n"));
265 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
266 "\t [-o property=value] <pool> <newpool> "
267 "[<device> ...]\n"));
269 return (gettext("\treguid <pool>\n"));
278 * Callback routine that will print out a pool property value.
281 print_prop_cb(int prop, void *cb)
285 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
287 if (zpool_prop_readonly(prop))
288 (void) fprintf(fp, " NO ");
290 (void) fprintf(fp, " YES ");
292 if (zpool_prop_values(prop) == NULL)
293 (void) fprintf(fp, "-\n");
295 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
301 * Display usage message. If we're inside a command, display only the usage for
302 * that command. Otherwise, iterate over the entire command table and display
303 * a complete usage message.
306 usage(boolean_t requested)
308 FILE *fp = requested ? stdout : stderr;
310 if (current_command == NULL) {
313 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
315 gettext("where 'command' is one of the following:\n\n"));
317 for (i = 0; i < NCOMMAND; i++) {
318 if (command_table[i].name == NULL)
319 (void) fprintf(fp, "\n");
321 (void) fprintf(fp, "%s",
322 get_usage(command_table[i].usage));
325 (void) fprintf(fp, gettext("usage:\n"));
326 (void) fprintf(fp, "%s", get_usage(current_command->usage));
329 if (current_command != NULL &&
330 ((strcmp(current_command->name, "set") == 0) ||
331 (strcmp(current_command->name, "get") == 0) ||
332 (strcmp(current_command->name, "list") == 0))) {
335 gettext("\nthe following properties are supported:\n"));
337 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
338 "PROPERTY", "EDIT", "VALUES");
340 /* Iterate over all properties */
341 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
344 (void) fprintf(fp, "\t%-15s ", "feature@...");
345 (void) fprintf(fp, "YES disabled | enabled | active\n");
347 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
348 "appended with a feature name.\nSee zpool-features(7).\n"));
352 * See comments at end of main().
354 if (getenv("ZFS_ABORT") != NULL) {
355 (void) printf("dumping core by request\n");
359 exit(requested ? 0 : 2);
363 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
364 boolean_t print_logs)
371 (void) printf("\t%*s%s\n", indent, "", name);
373 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
374 &child, &children) != 0)
377 for (c = 0; c < children; c++) {
378 uint64_t is_log = B_FALSE;
380 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
382 if ((is_log && !print_logs) || (!is_log && print_logs))
385 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
386 print_vdev_tree(zhp, vname, child[c], indent + 2,
393 prop_list_contains_feature(nvlist_t *proplist)
396 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
397 nvp = nvlist_next_nvpair(proplist, nvp)) {
398 if (zpool_prop_feature(nvpair_name(nvp)))
405 * Add a property pair (name, string-value) into a property nvlist.
408 add_prop_list(const char *propname, char *propval, nvlist_t **props,
411 zpool_prop_t prop = ZPROP_INVAL;
417 if (*props == NULL &&
418 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
419 (void) fprintf(stderr,
420 gettext("internal error: out of memory\n"));
427 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
429 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
430 !zpool_prop_feature(propname)) {
431 (void) fprintf(stderr, gettext("property '%s' is "
432 "not a valid pool property\n"), propname);
437 * feature@ properties and version should not be specified
440 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
441 nvlist_exists(proplist, vname)) ||
442 (prop == ZPOOL_PROP_VERSION &&
443 prop_list_contains_feature(proplist))) {
444 (void) fprintf(stderr, gettext("'feature@' and "
445 "'version' properties cannot be specified "
451 if (zpool_prop_feature(propname))
454 normnm = zpool_prop_to_name(prop);
456 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
457 normnm = zfs_prop_to_name(fprop);
463 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
464 prop != ZPOOL_PROP_CACHEFILE) {
465 (void) fprintf(stderr, gettext("property '%s' "
466 "specified multiple times\n"), propname);
470 if (nvlist_add_string(proplist, normnm, propval) != 0) {
471 (void) fprintf(stderr, gettext("internal "
472 "error: out of memory\n"));
480 * zpool add [-fn] <pool> <vdev> ...
482 * -f Force addition of devices, even if they appear in use
483 * -n Do not add the devices, but display the resulting layout if
484 * they were to be added.
486 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
487 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
491 zpool_do_add(int argc, char **argv)
493 boolean_t force = B_FALSE;
494 boolean_t dryrun = B_FALSE;
503 while ((c = getopt(argc, argv, "fn")) != -1) {
512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
521 /* get pool name and check number of arguments */
523 (void) fprintf(stderr, gettext("missing pool name argument\n"));
527 (void) fprintf(stderr, gettext("missing vdev specification\n"));
536 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
539 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
540 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
546 /* pass off to get_vdev_spec for processing */
547 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
549 if (nvroot == NULL) {
555 nvlist_t *poolnvroot;
557 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
560 (void) printf(gettext("would update '%s' to the following "
561 "configuration:\n"), zpool_get_name(zhp));
563 /* print original main pool and new tree */
564 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
565 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
567 /* Do the same for the logs */
568 if (num_logs(poolnvroot) > 0) {
569 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
570 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
571 } else if (num_logs(nvroot) > 0) {
572 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
577 ret = (zpool_add(zhp, nvroot) != 0);
587 * zpool remove <pool> <vdev> ...
589 * Removes the given vdev from the pool. Currently, this supports removing
590 * spares, cache, and log devices from the pool.
593 zpool_do_remove(int argc, char **argv)
602 /* get pool name and check number of arguments */
604 (void) fprintf(stderr, gettext("missing pool name argument\n"));
608 (void) fprintf(stderr, gettext("missing device\n"));
614 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
617 for (i = 1; i < argc; i++) {
618 if (zpool_vdev_remove(zhp, argv[i]) != 0)
626 * zpool labelclear <vdev>
628 * Verifies that the vdev is not active and zeros out the label information
632 zpool_do_labelclear(int argc, char **argv)
635 int c, fd = -1, ret = 0;
637 boolean_t inuse = B_FALSE;
638 boolean_t force = B_FALSE;
641 while ((c = getopt(argc, argv, "f")) != -1) {
647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
658 (void) fprintf(stderr, gettext("missing vdev device name\n"));
663 if ((fd = open(vdev, O_RDWR)) < 0) {
664 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
669 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
673 (void) fprintf(stderr,
674 gettext("Unable to determine pool state for %s\n"
675 "Use -f to force the clearing any label data\n"), vdev);
683 case POOL_STATE_ACTIVE:
684 case POOL_STATE_SPARE:
685 case POOL_STATE_L2CACHE:
686 (void) fprintf(stderr,
687 gettext("labelclear operation failed.\n"
688 "\tVdev %s is a member (%s), of pool \"%s\".\n"
689 "\tTo remove label information from this device, export or destroy\n"
690 "\tthe pool, or remove %s from the configuration of this pool\n"
691 "\tand retry the labelclear operation\n"),
692 vdev, zpool_pool_state_to_name(state), name, vdev);
696 case POOL_STATE_EXPORTED:
700 (void) fprintf(stderr,
701 gettext("labelclear operation failed.\n"
702 "\tVdev %s is a member of the exported pool \"%s\".\n"
703 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
709 case POOL_STATE_POTENTIALLY_ACTIVE:
713 (void) fprintf(stderr,
714 gettext("labelclear operation failed.\n"
715 "\tVdev %s is a member of the pool \"%s\".\n"
716 "\tThis pool is unknown to this system, but may be active on\n"
717 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
718 "\tremoval of label information.\n"),
723 case POOL_STATE_DESTROYED:
724 /* inuse should never be set for a destoryed pool... */
730 if (zpool_clear_label(fd) != 0) {
731 (void) fprintf(stderr,
732 gettext("Label clear failed on vdev %s\n"), vdev);
745 * zpool create [-fnd] [-o property=value] ...
746 * [-O file-system-property=value] ...
747 * [-R root] [-m mountpoint] <pool> <dev> ...
749 * -f Force creation, even if devices appear in use
750 * -n Do not create the pool, but display the resulting layout if it
751 * were to be created.
752 * -R Create a pool under an alternate root
753 * -m Set default mountpoint for the root dataset. By default it's
755 * -o Set property=value.
756 * -d Don't automatically enable all supported pool features
757 * (individual features can be enabled with -o).
758 * -O Set fsproperty=value in the pool's root file system
760 * Creates the named pool according to the given vdev specification. The
761 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
762 * we get the nvlist back from get_vdev_spec(), we either print out the contents
763 * (if '-n' was specified), or pass it to libzfs to do the creation.
766 zpool_do_create(int argc, char **argv)
768 boolean_t force = B_FALSE;
769 boolean_t dryrun = B_FALSE;
770 boolean_t enable_all_pool_feat = B_TRUE;
772 nvlist_t *nvroot = NULL;
775 char *altroot = NULL;
776 char *mountpoint = NULL;
777 nvlist_t *fsprops = NULL;
778 nvlist_t *props = NULL;
782 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
791 enable_all_pool_feat = B_FALSE;
795 if (add_prop_list(zpool_prop_to_name(
796 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
798 if (nvlist_lookup_string(props,
799 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
802 if (add_prop_list(zpool_prop_to_name(
803 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
810 if ((propval = strchr(optarg, '=')) == NULL) {
811 (void) fprintf(stderr, gettext("missing "
812 "'=' for -o option\n"));
818 if (add_prop_list(optarg, propval, &props, B_TRUE))
822 * If the user is creating a pool that doesn't support
823 * feature flags, don't enable any features.
825 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
829 ver = strtoull(propval, &end, 10);
831 ver < SPA_VERSION_FEATURES) {
832 enable_all_pool_feat = B_FALSE;
837 if ((propval = strchr(optarg, '=')) == NULL) {
838 (void) fprintf(stderr, gettext("missing "
839 "'=' for -O option\n"));
845 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
849 (void) fprintf(stderr, gettext("missing argument for "
850 "'%c' option\n"), optopt);
853 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
862 /* get pool name and check number of arguments */
864 (void) fprintf(stderr, gettext("missing pool name argument\n"));
868 (void) fprintf(stderr, gettext("missing vdev specification\n"));
875 * As a special case, check for use of '/' in the name, and direct the
876 * user to use 'zfs create' instead.
878 if (strchr(poolname, '/') != NULL) {
879 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
880 "character '/' in pool name\n"), poolname);
881 (void) fprintf(stderr, gettext("use 'zfs create' to "
882 "create a dataset\n"));
886 /* pass off to get_vdev_spec for bulk processing */
887 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
892 /* make_root_vdev() allows 0 toplevel children if there are spares */
893 if (!zfs_allocatable_devs(nvroot)) {
894 (void) fprintf(stderr, gettext("invalid vdev "
895 "specification: at least one toplevel vdev must be "
900 if (altroot != NULL && altroot[0] != '/') {
901 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
902 "must be an absolute path\n"), altroot);
907 * Check the validity of the mountpoint and direct the user to use the
908 * '-m' mountpoint option if it looks like its in use.
910 if (mountpoint == NULL ||
911 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
912 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
913 char buf[MAXPATHLEN];
916 if (mountpoint && mountpoint[0] != '/') {
917 (void) fprintf(stderr, gettext("invalid mountpoint "
918 "'%s': must be an absolute path, 'legacy', or "
919 "'none'\n"), mountpoint);
923 if (mountpoint == NULL) {
925 (void) snprintf(buf, sizeof (buf), "%s/%s",
928 (void) snprintf(buf, sizeof (buf), "/%s",
932 (void) snprintf(buf, sizeof (buf), "%s%s",
933 altroot, mountpoint);
935 (void) snprintf(buf, sizeof (buf), "%s",
939 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
940 (void) fprintf(stderr, gettext("mountpoint '%s' : "
941 "%s\n"), buf, strerror(errno));
942 (void) fprintf(stderr, gettext("use '-m' "
943 "option to provide a different default\n"));
948 while (count < 3 && readdir(dirp) != NULL)
950 (void) closedir(dirp);
953 (void) fprintf(stderr, gettext("mountpoint "
954 "'%s' exists and is not empty\n"), buf);
955 (void) fprintf(stderr, gettext("use '-m' "
956 "option to provide a "
957 "different default\n"));
965 * For a dry run invocation, print out a basic message and run
966 * through all the vdevs in the list and print out in an
967 * appropriate hierarchy.
969 (void) printf(gettext("would create '%s' with the "
970 "following layout:\n\n"), poolname);
972 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
973 if (num_logs(nvroot) > 0)
974 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
979 * Hand off to libzfs.
981 if (enable_all_pool_feat) {
983 for (i = 0; i < SPA_FEATURES; i++) {
984 char propname[MAXPATHLEN];
985 zfeature_info_t *feat = &spa_feature_table[i];
987 (void) snprintf(propname, sizeof (propname),
988 "feature@%s", feat->fi_uname);
991 * Skip feature if user specified it manually
992 * on the command line.
994 if (nvlist_exists(props, propname))
997 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
998 &props, B_TRUE) != 0)
1002 if (zpool_create(g_zfs, poolname,
1003 nvroot, props, fsprops) == 0) {
1004 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1005 ZFS_TYPE_FILESYSTEM);
1007 if (mountpoint != NULL)
1008 verify(zfs_prop_set(pool,
1010 ZFS_PROP_MOUNTPOINT),
1012 if (zfs_mount(pool, NULL, 0) == 0)
1013 ret = zfs_shareall(pool);
1016 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1017 (void) fprintf(stderr, gettext("pool name may have "
1023 nvlist_free(nvroot);
1024 nvlist_free(fsprops);
1028 nvlist_free(fsprops);
1035 * zpool destroy <pool>
1037 * -f Forcefully unmount any datasets
1039 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1042 zpool_do_destroy(int argc, char **argv)
1044 boolean_t force = B_FALSE;
1047 zpool_handle_t *zhp;
1051 while ((c = getopt(argc, argv, "f")) != -1) {
1057 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1066 /* check arguments */
1068 (void) fprintf(stderr, gettext("missing pool argument\n"));
1072 (void) fprintf(stderr, gettext("too many arguments\n"));
1078 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1080 * As a special case, check for use of '/' in the name, and
1081 * direct the user to use 'zfs destroy' instead.
1083 if (strchr(pool, '/') != NULL)
1084 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1085 "destroy a dataset\n"));
1089 if (zpool_disable_datasets(zhp, force) != 0) {
1090 (void) fprintf(stderr, gettext("could not destroy '%s': "
1091 "could not unmount datasets\n"), zpool_get_name(zhp));
1095 ret = (zpool_destroy(zhp) != 0);
1103 * zpool export [-f] <pool> ...
1105 * -f Forcefully unmount datasets
1107 * Export the given pools. By default, the command will attempt to cleanly
1108 * unmount any active datasets within the pool. If the '-f' flag is specified,
1109 * then the datasets will be forcefully unmounted.
1112 zpool_do_export(int argc, char **argv)
1114 boolean_t force = B_FALSE;
1115 boolean_t hardforce = B_FALSE;
1117 zpool_handle_t *zhp;
1122 while ((c = getopt(argc, argv, "fF")) != -1) {
1131 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1140 /* check arguments */
1142 (void) fprintf(stderr, gettext("missing pool argument\n"));
1147 for (i = 0; i < argc; i++) {
1148 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1153 if (zpool_disable_datasets(zhp, force) != 0) {
1160 if (zpool_export_force(zhp) != 0)
1162 } else if (zpool_export(zhp, force) != 0) {
1173 * Given a vdev configuration, determine the maximum width needed for the device
1177 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1179 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1184 if (strlen(name) + depth > max)
1185 max = strlen(name) + depth;
1189 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1190 &child, &children) == 0) {
1191 for (c = 0; c < children; c++)
1192 if ((ret = max_width(zhp, child[c], depth + 2,
1197 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1198 &child, &children) == 0) {
1199 for (c = 0; c < children; c++)
1200 if ((ret = max_width(zhp, child[c], depth + 2,
1205 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1206 &child, &children) == 0) {
1207 for (c = 0; c < children; c++)
1208 if ((ret = max_width(zhp, child[c], depth + 2,
1217 typedef struct spare_cbdata {
1219 zpool_handle_t *cb_zhp;
1223 find_vdev(nvlist_t *nv, uint64_t search)
1229 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1233 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1234 &child, &children) == 0) {
1235 for (c = 0; c < children; c++)
1236 if (find_vdev(child[c], search))
1244 find_spare(zpool_handle_t *zhp, void *data)
1246 spare_cbdata_t *cbp = data;
1247 nvlist_t *config, *nvroot;
1249 config = zpool_get_config(zhp, NULL);
1250 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1253 if (find_vdev(nvroot, cbp->cb_guid)) {
1263 * Print out configuration state as requested by status_callback.
1266 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1267 int namewidth, int depth, boolean_t isspare)
1271 pool_scan_stat_t *ps = NULL;
1273 char rbuf[6], wbuf[6], cbuf[6];
1275 uint64_t notpresent;
1279 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1280 &child, &children) != 0)
1283 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1284 (uint64_t **)&vs, &c) == 0);
1286 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1289 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1292 if (vs->vs_aux == VDEV_AUX_SPARED)
1294 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1298 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1302 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1303 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1304 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1305 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1308 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1309 ¬present) == 0 ||
1310 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1312 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1313 (void) printf(" was %s", path);
1314 } else if (vs->vs_aux != 0) {
1317 switch (vs->vs_aux) {
1318 case VDEV_AUX_OPEN_FAILED:
1319 (void) printf(gettext("cannot open"));
1322 case VDEV_AUX_BAD_GUID_SUM:
1323 (void) printf(gettext("missing device"));
1326 case VDEV_AUX_NO_REPLICAS:
1327 (void) printf(gettext("insufficient replicas"));
1330 case VDEV_AUX_VERSION_NEWER:
1331 (void) printf(gettext("newer version"));
1334 case VDEV_AUX_UNSUP_FEAT:
1335 (void) printf(gettext("unsupported feature(s)"));
1338 case VDEV_AUX_SPARED:
1339 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1341 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1342 if (strcmp(zpool_get_name(cb.cb_zhp),
1343 zpool_get_name(zhp)) == 0)
1344 (void) printf(gettext("currently in "
1347 (void) printf(gettext("in use by "
1349 zpool_get_name(cb.cb_zhp));
1350 zpool_close(cb.cb_zhp);
1352 (void) printf(gettext("currently in use"));
1356 case VDEV_AUX_ERR_EXCEEDED:
1357 (void) printf(gettext("too many errors"));
1360 case VDEV_AUX_IO_FAILURE:
1361 (void) printf(gettext("experienced I/O failures"));
1364 case VDEV_AUX_BAD_LOG:
1365 (void) printf(gettext("bad intent log"));
1368 case VDEV_AUX_EXTERNAL:
1369 (void) printf(gettext("external device fault"));
1372 case VDEV_AUX_SPLIT_POOL:
1373 (void) printf(gettext("split into new pool"));
1377 (void) printf(gettext("corrupted data"));
1382 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1383 (uint64_t **)&ps, &c);
1385 if (ps && ps->pss_state == DSS_SCANNING &&
1386 vs->vs_scan_processed != 0 && children == 0) {
1387 (void) printf(gettext(" (%s)"),
1388 (ps->pss_func == POOL_SCAN_RESILVER) ?
1389 "resilvering" : "repairing");
1392 (void) printf("\n");
1394 for (c = 0; c < children; c++) {
1395 uint64_t islog = B_FALSE, ishole = B_FALSE;
1397 /* Don't print logs or holes here */
1398 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1400 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1402 if (islog || ishole)
1404 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1405 print_status_config(zhp, vname, child[c],
1406 namewidth, depth + 2, isspare);
1413 * Print the configuration of an exported pool. Iterate over all vdevs in the
1414 * pool, printing out the name and status for each one.
1417 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1424 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1425 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1426 strcmp(type, VDEV_TYPE_HOLE) == 0)
1429 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1430 (uint64_t **)&vs, &c) == 0);
1432 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1433 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1435 if (vs->vs_aux != 0) {
1438 switch (vs->vs_aux) {
1439 case VDEV_AUX_OPEN_FAILED:
1440 (void) printf(gettext("cannot open"));
1443 case VDEV_AUX_BAD_GUID_SUM:
1444 (void) printf(gettext("missing device"));
1447 case VDEV_AUX_NO_REPLICAS:
1448 (void) printf(gettext("insufficient replicas"));
1451 case VDEV_AUX_VERSION_NEWER:
1452 (void) printf(gettext("newer version"));
1455 case VDEV_AUX_UNSUP_FEAT:
1456 (void) printf(gettext("unsupported feature(s)"));
1459 case VDEV_AUX_ERR_EXCEEDED:
1460 (void) printf(gettext("too many errors"));
1464 (void) printf(gettext("corrupted data"));
1468 (void) printf("\n");
1470 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1471 &child, &children) != 0)
1474 for (c = 0; c < children; c++) {
1475 uint64_t is_log = B_FALSE;
1477 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1482 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1483 print_import_config(vname, child[c], namewidth, depth + 2);
1487 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1488 &child, &children) == 0) {
1489 (void) printf(gettext("\tcache\n"));
1490 for (c = 0; c < children; c++) {
1491 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1492 (void) printf("\t %s\n", vname);
1497 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1498 &child, &children) == 0) {
1499 (void) printf(gettext("\tspares\n"));
1500 for (c = 0; c < children; c++) {
1501 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1502 (void) printf("\t %s\n", vname);
1510 * Logs are recorded as top level vdevs in the main pool child array
1511 * but with "is_log" set to 1. We use either print_status_config() or
1512 * print_import_config() to print the top level logs then any log
1513 * children (eg mirrored slogs) are printed recursively - which
1514 * works because only the top level vdev is marked "is_log"
1517 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1522 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1526 (void) printf(gettext("\tlogs\n"));
1528 for (c = 0; c < children; c++) {
1529 uint64_t is_log = B_FALSE;
1532 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1536 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1538 print_status_config(zhp, name, child[c], namewidth,
1541 print_import_config(name, child[c], namewidth, 2);
1547 * Display the status for the given pool.
1550 show_import(nvlist_t *config)
1552 uint64_t pool_state;
1564 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1566 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1568 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1570 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1573 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1574 (uint64_t **)&vs, &vsc) == 0);
1575 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1577 reason = zpool_import_status(config, &msgid);
1579 (void) printf(gettext(" pool: %s\n"), name);
1580 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1581 (void) printf(gettext(" state: %s"), health);
1582 if (pool_state == POOL_STATE_DESTROYED)
1583 (void) printf(gettext(" (DESTROYED)"));
1584 (void) printf("\n");
1587 case ZPOOL_STATUS_MISSING_DEV_R:
1588 case ZPOOL_STATUS_MISSING_DEV_NR:
1589 case ZPOOL_STATUS_BAD_GUID_SUM:
1590 (void) printf(gettext(" status: One or more devices are "
1591 "missing from the system.\n"));
1594 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1595 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1596 (void) printf(gettext(" status: One or more devices contains "
1597 "corrupted data.\n"));
1600 case ZPOOL_STATUS_CORRUPT_DATA:
1602 gettext(" status: The pool data is corrupted.\n"));
1605 case ZPOOL_STATUS_OFFLINE_DEV:
1606 (void) printf(gettext(" status: One or more devices "
1607 "are offlined.\n"));
1610 case ZPOOL_STATUS_CORRUPT_POOL:
1611 (void) printf(gettext(" status: The pool metadata is "
1615 case ZPOOL_STATUS_VERSION_OLDER:
1616 (void) printf(gettext(" status: The pool is formatted using a "
1617 "legacy on-disk version.\n"));
1620 case ZPOOL_STATUS_VERSION_NEWER:
1621 (void) printf(gettext(" status: The pool is formatted using an "
1622 "incompatible version.\n"));
1625 case ZPOOL_STATUS_FEAT_DISABLED:
1626 (void) printf(gettext(" status: Some supported features are "
1627 "not enabled on the pool.\n"));
1630 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1631 (void) printf(gettext("status: The pool uses the following "
1632 "feature(s) not supported on this sytem:\n"));
1633 zpool_print_unsup_feat(config);
1636 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1637 (void) printf(gettext("status: The pool can only be accessed "
1638 "in read-only mode on this system. It\n\tcannot be "
1639 "accessed in read-write mode because it uses the "
1640 "following\n\tfeature(s) not supported on this system:\n"));
1641 zpool_print_unsup_feat(config);
1644 case ZPOOL_STATUS_HOSTID_MISMATCH:
1645 (void) printf(gettext(" status: The pool was last accessed by "
1646 "another system.\n"));
1649 case ZPOOL_STATUS_FAULTED_DEV_R:
1650 case ZPOOL_STATUS_FAULTED_DEV_NR:
1651 (void) printf(gettext(" status: One or more devices are "
1655 case ZPOOL_STATUS_BAD_LOG:
1656 (void) printf(gettext(" status: An intent log record cannot be "
1660 case ZPOOL_STATUS_RESILVERING:
1661 (void) printf(gettext(" status: One or more devices were being "
1667 * No other status can be seen when importing pools.
1669 assert(reason == ZPOOL_STATUS_OK);
1673 * Print out an action according to the overall state of the pool.
1675 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1676 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1677 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1678 (void) printf(gettext(" action: The pool can be "
1679 "imported using its name or numeric identifier, "
1680 "though\n\tsome features will not be available "
1681 "without an explicit 'zpool upgrade'.\n"));
1682 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1683 (void) printf(gettext(" action: The pool can be "
1684 "imported using its name or numeric "
1685 "identifier and\n\tthe '-f' flag.\n"));
1687 (void) printf(gettext(" action: The pool can be "
1688 "imported using its name or numeric "
1691 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1692 (void) printf(gettext(" action: The pool can be imported "
1693 "despite missing or damaged devices. The\n\tfault "
1694 "tolerance of the pool may be compromised if imported.\n"));
1697 case ZPOOL_STATUS_VERSION_NEWER:
1698 (void) printf(gettext(" action: The pool cannot be "
1699 "imported. Access the pool on a system running "
1700 "newer\n\tsoftware, or recreate the pool from "
1703 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1704 (void) printf(gettext("action: The pool cannot be "
1705 "imported. Access the pool on a system that "
1706 "supports\n\tthe required feature(s), or recreate "
1707 "the pool from backup.\n"));
1709 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1710 (void) printf(gettext("action: The pool cannot be "
1711 "imported in read-write mode. Import the pool "
1713 "\t\"-o readonly=on\", access the pool on a system "
1714 "that supports the\n\trequired feature(s), or "
1715 "recreate the pool from backup.\n"));
1717 case ZPOOL_STATUS_MISSING_DEV_R:
1718 case ZPOOL_STATUS_MISSING_DEV_NR:
1719 case ZPOOL_STATUS_BAD_GUID_SUM:
1720 (void) printf(gettext(" action: The pool cannot be "
1721 "imported. Attach the missing\n\tdevices and try "
1725 (void) printf(gettext(" action: The pool cannot be "
1726 "imported due to damaged devices or data.\n"));
1730 /* Print the comment attached to the pool. */
1731 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1732 (void) printf(gettext("comment: %s\n"), comment);
1735 * If the state is "closed" or "can't open", and the aux state
1736 * is "corrupt data":
1738 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1739 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1740 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1741 if (pool_state == POOL_STATE_DESTROYED)
1742 (void) printf(gettext("\tThe pool was destroyed, "
1743 "but can be imported using the '-Df' flags.\n"));
1744 else if (pool_state != POOL_STATE_EXPORTED)
1745 (void) printf(gettext("\tThe pool may be active on "
1746 "another system, but can be imported using\n\t"
1747 "the '-f' flag.\n"));
1751 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1754 (void) printf(gettext(" config:\n\n"));
1756 namewidth = max_width(NULL, nvroot, 0, 0);
1760 print_import_config(name, nvroot, namewidth, 0);
1761 if (num_logs(nvroot) > 0)
1762 print_logs(NULL, nvroot, namewidth, B_FALSE);
1764 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1765 (void) printf(gettext("\n\tAdditional devices are known to "
1766 "be part of this pool, though their\n\texact "
1767 "configuration cannot be determined.\n"));
1772 * Perform the import for the given configuration. This passes the heavy
1773 * lifting off to zpool_import_props(), and then mounts the datasets contained
1777 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1778 nvlist_t *props, int flags)
1780 zpool_handle_t *zhp;
1785 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1788 verify(nvlist_lookup_uint64(config,
1789 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1790 verify(nvlist_lookup_uint64(config,
1791 ZPOOL_CONFIG_VERSION, &version) == 0);
1792 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1793 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1794 "is formatted using an unsupported ZFS version\n"), name);
1796 } else if (state != POOL_STATE_EXPORTED &&
1797 !(flags & ZFS_IMPORT_ANY_HOST)) {
1800 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1802 if ((unsigned long)hostid != gethostid()) {
1807 verify(nvlist_lookup_string(config,
1808 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1809 verify(nvlist_lookup_uint64(config,
1810 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1812 (void) fprintf(stderr, gettext("cannot import "
1813 "'%s': pool may be in use from other "
1814 "system, it was last accessed by %s "
1815 "(hostid: 0x%lx) on %s"), name, hostname,
1816 (unsigned long)hostid,
1817 asctime(localtime(&t)));
1818 (void) fprintf(stderr, gettext("use '-f' to "
1819 "import anyway\n"));
1823 (void) fprintf(stderr, gettext("cannot import '%s': "
1824 "pool may be in use from other system\n"), name);
1825 (void) fprintf(stderr, gettext("use '-f' to import "
1831 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1834 if (newname != NULL)
1835 name = (char *)newname;
1837 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1840 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1841 !(flags & ZFS_IMPORT_ONLY) &&
1842 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1852 * zpool import [-d dir] [-D]
1853 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1854 * [-d dir | -c cachefile] [-f] -a
1855 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1856 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1858 * -c Read pool information from a cachefile instead of searching
1861 * -d Scan in a specific directory, other than /dev/dsk. More than
1862 * one directory can be specified using multiple '-d' options.
1864 * -D Scan for previously destroyed pools or import all or only
1865 * specified destroyed pools.
1867 * -R Temporarily import the pool, with all mountpoints relative to
1868 * the given root. The pool will remain exported when the machine
1871 * -V Import even in the presence of faulted vdevs. This is an
1872 * intentionally undocumented option for testing purposes, and
1873 * treats the pool configuration as complete, leaving any bad
1874 * vdevs in the FAULTED state. In other words, it does verbatim
1877 * -f Force import, even if it appears that the pool is active.
1879 * -F Attempt rewind if necessary.
1881 * -n See if rewind would work, but don't actually rewind.
1883 * -N Import the pool but don't mount datasets.
1885 * -T Specify a starting txg to use for import. This option is
1886 * intentionally undocumented option for testing purposes.
1888 * -a Import all pools found.
1890 * -o Set property=value and/or temporary mount options (without '=').
1892 * The import command scans for pools to import, and import pools based on pool
1893 * name and GUID. The pool can also be renamed as part of the import process.
1896 zpool_do_import(int argc, char **argv)
1898 char **searchdirs = NULL;
1902 nvlist_t *pools = NULL;
1903 boolean_t do_all = B_FALSE;
1904 boolean_t do_destroyed = B_FALSE;
1905 char *mntopts = NULL;
1908 uint64_t searchguid = 0;
1909 char *searchname = NULL;
1911 nvlist_t *found_config;
1912 nvlist_t *policy = NULL;
1913 nvlist_t *props = NULL;
1915 int flags = ZFS_IMPORT_NORMAL;
1916 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1917 boolean_t dryrun = B_FALSE;
1918 boolean_t do_rewind = B_FALSE;
1919 boolean_t xtreme_rewind = B_FALSE;
1920 uint64_t pool_state, txg = -1ULL;
1921 char *cachefile = NULL;
1922 importargs_t idata = { 0 };
1926 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1935 if (searchdirs == NULL) {
1936 searchdirs = safe_malloc(sizeof (char *));
1938 char **tmp = safe_malloc((nsearch + 1) *
1940 bcopy(searchdirs, tmp, nsearch *
1945 searchdirs[nsearch++] = optarg;
1948 do_destroyed = B_TRUE;
1951 flags |= ZFS_IMPORT_ANY_HOST;
1957 flags |= ZFS_IMPORT_MISSING_LOG;
1963 flags |= ZFS_IMPORT_ONLY;
1966 if ((propval = strchr(optarg, '=')) != NULL) {
1969 if (add_prop_list(optarg, propval,
1977 if (add_prop_list(zpool_prop_to_name(
1978 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1980 if (nvlist_lookup_string(props,
1981 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1984 if (add_prop_list(zpool_prop_to_name(
1985 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1990 txg = strtoull(optarg, &endptr, 10);
1991 if (errno != 0 || *endptr != '\0') {
1992 (void) fprintf(stderr,
1993 gettext("invalid txg value\n"));
1996 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1999 flags |= ZFS_IMPORT_VERBATIM;
2002 xtreme_rewind = B_TRUE;
2005 (void) fprintf(stderr, gettext("missing argument for "
2006 "'%c' option\n"), optopt);
2010 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2019 if (cachefile && nsearch != 0) {
2020 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2024 if ((dryrun || xtreme_rewind) && !do_rewind) {
2025 (void) fprintf(stderr,
2026 gettext("-n or -X only meaningful with -F\n"));
2030 rewind_policy = ZPOOL_TRY_REWIND;
2032 rewind_policy = ZPOOL_DO_REWIND;
2034 rewind_policy |= ZPOOL_EXTREME_REWIND;
2036 /* In the future, we can capture further policy and include it here */
2037 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2038 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2039 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2042 if (searchdirs == NULL) {
2043 searchdirs = safe_malloc(sizeof (char *));
2044 searchdirs[0] = "/dev";
2048 /* check argument count */
2051 (void) fprintf(stderr, gettext("too many arguments\n"));
2056 (void) fprintf(stderr, gettext("too many arguments\n"));
2061 * Check for the SYS_CONFIG privilege. We do this explicitly
2062 * here because otherwise any attempt to discover pools will
2065 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2066 (void) fprintf(stderr, gettext("cannot "
2067 "discover pools: permission denied\n"));
2069 nvlist_free(policy);
2075 * Depending on the arguments given, we do one of the following:
2077 * <none> Iterate through all pools and display information about
2080 * -a Iterate through all pools and try to import each one.
2082 * <id> Find the pool that corresponds to the given GUID/pool
2083 * name and import that one.
2085 * -D Above options applies only to destroyed pools.
2091 searchguid = strtoull(argv[0], &endptr, 10);
2092 if (errno != 0 || *endptr != '\0')
2093 searchname = argv[0];
2094 found_config = NULL;
2097 * User specified a name or guid. Ensure it's unique.
2099 idata.unique = B_TRUE;
2103 idata.path = searchdirs;
2104 idata.paths = nsearch;
2105 idata.poolname = searchname;
2106 idata.guid = searchguid;
2107 idata.cachefile = cachefile;
2109 pools = zpool_search_import(g_zfs, &idata);
2111 if (pools != NULL && idata.exists &&
2112 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2113 (void) fprintf(stderr, gettext("cannot import '%s': "
2114 "a pool with that name already exists\n"),
2116 (void) fprintf(stderr, gettext("use the form '%s "
2117 "<pool | id> <newpool>' to give it a new name\n"),
2120 } else if (pools == NULL && idata.exists) {
2121 (void) fprintf(stderr, gettext("cannot import '%s': "
2122 "a pool with that name is already created/imported,\n"),
2124 (void) fprintf(stderr, gettext("and no additional pools "
2125 "with that name were found\n"));
2127 } else if (pools == NULL) {
2129 (void) fprintf(stderr, gettext("cannot import '%s': "
2130 "no such pool available\n"), argv[0]);
2137 nvlist_free(policy);
2142 * At this point we have a list of import candidate configs. Even if
2143 * we were searching by pool name or guid, we still need to
2144 * post-process the list to deal with pool state and possible
2150 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2152 verify(nvpair_value_nvlist(elem, &config) == 0);
2154 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2156 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2158 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2161 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2168 (void) printf("\n");
2171 err |= do_import(config, NULL, mntopts,
2174 show_import(config);
2176 } else if (searchname != NULL) {
2180 * We are searching for a pool based on name.
2182 verify(nvlist_lookup_string(config,
2183 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2185 if (strcmp(name, searchname) == 0) {
2186 if (found_config != NULL) {
2187 (void) fprintf(stderr, gettext(
2188 "cannot import '%s': more than "
2189 "one matching pool\n"), searchname);
2190 (void) fprintf(stderr, gettext(
2191 "import by numeric ID instead\n"));
2194 found_config = config;
2200 * Search for a pool by guid.
2202 verify(nvlist_lookup_uint64(config,
2203 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2205 if (guid == searchguid)
2206 found_config = config;
2211 * If we were searching for a specific pool, verify that we found a
2212 * pool, and then do the import.
2214 if (argc != 0 && err == 0) {
2215 if (found_config == NULL) {
2216 (void) fprintf(stderr, gettext("cannot import '%s': "
2217 "no such pool available\n"), argv[0]);
2220 err |= do_import(found_config, argc == 1 ? NULL :
2221 argv[1], mntopts, props, flags);
2226 * If we were just looking for pools, report an error if none were
2229 if (argc == 0 && first)
2230 (void) fprintf(stderr,
2231 gettext("no pools available to import\n"));
2236 nvlist_free(policy);
2239 return (err ? 1 : 0);
2242 typedef struct iostat_cbdata {
2243 boolean_t cb_verbose;
2246 zpool_list_t *cb_list;
2250 print_iostat_separator(iostat_cbdata_t *cb)
2254 for (i = 0; i < cb->cb_namewidth; i++)
2256 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2260 print_iostat_header(iostat_cbdata_t *cb)
2262 (void) printf("%*s capacity operations bandwidth\n",
2263 cb->cb_namewidth, "");
2264 (void) printf("%-*s alloc free read write read write\n",
2265 cb->cb_namewidth, "pool");
2266 print_iostat_separator(cb);
2270 * Display a single statistic.
2273 print_one_stat(uint64_t value)
2277 zfs_nicenum(value, buf, sizeof (buf));
2278 (void) printf(" %5s", buf);
2282 * Print out all the statistics for the given vdev. This can either be the
2283 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2284 * is a verbose output, and we don't want to display the toplevel pool stats.
2287 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2288 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2290 nvlist_t **oldchild, **newchild;
2292 vdev_stat_t *oldvs, *newvs;
2293 vdev_stat_t zerovs = { 0 };
2298 if (oldnv != NULL) {
2299 verify(nvlist_lookup_uint64_array(oldnv,
2300 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2305 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2306 (uint64_t **)&newvs, &c) == 0);
2308 if (strlen(name) + depth > cb->cb_namewidth)
2309 (void) printf("%*s%s", depth, "", name);
2311 (void) printf("%*s%s%*s", depth, "", name,
2312 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2314 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2319 scale = (double)NANOSEC / tdelta;
2321 /* only toplevel vdevs have capacity stats */
2322 if (newvs->vs_space == 0) {
2323 (void) printf(" - -");
2325 print_one_stat(newvs->vs_alloc);
2326 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2329 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2330 oldvs->vs_ops[ZIO_TYPE_READ])));
2332 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2333 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2335 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2336 oldvs->vs_bytes[ZIO_TYPE_READ])));
2338 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2339 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2341 (void) printf("\n");
2343 if (!cb->cb_verbose)
2346 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2347 &newchild, &children) != 0)
2350 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2351 &oldchild, &c) != 0)
2354 for (c = 0; c < children; c++) {
2355 uint64_t ishole = B_FALSE, islog = B_FALSE;
2357 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2360 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2363 if (ishole || islog)
2366 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2367 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2368 newchild[c], cb, depth + 2);
2373 * Log device section
2376 if (num_logs(newnv) > 0) {
2377 (void) printf("%-*s - - - - - "
2378 "-\n", cb->cb_namewidth, "logs");
2380 for (c = 0; c < children; c++) {
2381 uint64_t islog = B_FALSE;
2382 (void) nvlist_lookup_uint64(newchild[c],
2383 ZPOOL_CONFIG_IS_LOG, &islog);
2386 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2388 print_vdev_stats(zhp, vname, oldnv ?
2389 oldchild[c] : NULL, newchild[c],
2398 * Include level 2 ARC devices in iostat output
2400 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2401 &newchild, &children) != 0)
2404 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2405 &oldchild, &c) != 0)
2409 (void) printf("%-*s - - - - - "
2410 "-\n", cb->cb_namewidth, "cache");
2411 for (c = 0; c < children; c++) {
2412 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2414 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2415 newchild[c], cb, depth + 2);
2422 refresh_iostat(zpool_handle_t *zhp, void *data)
2424 iostat_cbdata_t *cb = data;
2428 * If the pool has disappeared, remove it from the list and continue.
2430 if (zpool_refresh_stats(zhp, &missing) != 0)
2434 pool_list_remove(cb->cb_list, zhp);
2440 * Callback to print out the iostats for the given pool.
2443 print_iostat(zpool_handle_t *zhp, void *data)
2445 iostat_cbdata_t *cb = data;
2446 nvlist_t *oldconfig, *newconfig;
2447 nvlist_t *oldnvroot, *newnvroot;
2449 newconfig = zpool_get_config(zhp, &oldconfig);
2451 if (cb->cb_iteration == 1)
2454 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2457 if (oldconfig == NULL)
2460 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2464 * Print out the statistics for the pool.
2466 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2469 print_iostat_separator(cb);
2475 get_namewidth(zpool_handle_t *zhp, void *data)
2477 iostat_cbdata_t *cb = data;
2478 nvlist_t *config, *nvroot;
2480 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2481 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2483 if (!cb->cb_verbose)
2484 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2486 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2491 * The width must fall into the range [10,38]. The upper limit is the
2492 * maximum we can have and still fit in 80 columns.
2494 if (cb->cb_namewidth < 10)
2495 cb->cb_namewidth = 10;
2496 if (cb->cb_namewidth > 38)
2497 cb->cb_namewidth = 38;
2503 * Parse the input string, get the 'interval' and 'count' value if there is one.
2506 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2509 unsigned long interval = 0, count = 0;
2510 int argc = *argcp, errno;
2513 * Determine if the last argument is an integer or a pool name
2515 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2519 interval = strtoul(argv[argc - 1], &end, 10);
2521 if (*end == '\0' && errno == 0) {
2522 if (interval == 0) {
2523 (void) fprintf(stderr, gettext("interval "
2524 "cannot be zero\n"));
2528 * Ignore the last parameter
2533 * If this is not a valid number, just plow on. The
2534 * user will get a more informative error message later
2542 * If the last argument is also an integer, then we have both a count
2545 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2550 interval = strtoul(argv[argc - 1], &end, 10);
2552 if (*end == '\0' && errno == 0) {
2553 if (interval == 0) {
2554 (void) fprintf(stderr, gettext("interval "
2555 "cannot be zero\n"));
2560 * Ignore the last parameter
2574 get_timestamp_arg(char c)
2577 timestamp_fmt = UDATE;
2579 timestamp_fmt = DDATE;
2585 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2587 * -v Display statistics for individual vdevs
2588 * -T Display a timestamp in date(1) or Unix format
2590 * This command can be tricky because we want to be able to deal with pool
2591 * creation/destruction as well as vdev configuration changes. The bulk of this
2592 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2593 * on pool_list_update() to detect the addition of new pools. Configuration
2594 * changes are all handled within libzfs.
2597 zpool_do_iostat(int argc, char **argv)
2602 unsigned long interval = 0, count = 0;
2604 boolean_t verbose = B_FALSE;
2608 while ((c = getopt(argc, argv, "T:v")) != -1) {
2611 get_timestamp_arg(*optarg);
2617 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2626 get_interval_count(&argc, argv, &interval, &count);
2629 * Construct the list of all interesting pools.
2632 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2635 if (pool_list_count(list) == 0 && argc != 0) {
2636 pool_list_free(list);
2640 if (pool_list_count(list) == 0 && interval == 0) {
2641 pool_list_free(list);
2642 (void) fprintf(stderr, gettext("no pools available\n"));
2647 * Enter the main iostat loop.
2650 cb.cb_verbose = verbose;
2651 cb.cb_iteration = 0;
2652 cb.cb_namewidth = 0;
2655 pool_list_update(list);
2657 if ((npools = pool_list_count(list)) == 0)
2661 * Refresh all statistics. This is done as an explicit step
2662 * before calculating the maximum name width, so that any
2663 * configuration changes are properly accounted for.
2665 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2668 * Iterate over all pools to determine the maximum width
2669 * for the pool / device name column across all pools.
2671 cb.cb_namewidth = 0;
2672 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2674 if (timestamp_fmt != NODATE)
2675 print_timestamp(timestamp_fmt);
2678 * If it's the first time, or verbose mode, print the header.
2680 if (++cb.cb_iteration == 1 || verbose)
2681 print_iostat_header(&cb);
2683 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2686 * If there's more than one pool, and we're not in verbose mode
2687 * (which prints a separator for us), then print a separator.
2689 if (npools > 1 && !verbose)
2690 print_iostat_separator(&cb);
2693 (void) printf("\n");
2696 * Flush the output so that redirection to a file isn't buffered
2699 (void) fflush(stdout);
2704 if (count != 0 && --count == 0)
2707 (void) sleep(interval);
2710 pool_list_free(list);
2715 typedef struct list_cbdata {
2716 boolean_t cb_verbose;
2718 boolean_t cb_scripted;
2719 zprop_list_t *cb_proplist;
2723 * Given a list of columns to display, output appropriate headers for each one.
2726 print_header(list_cbdata_t *cb)
2728 zprop_list_t *pl = cb->cb_proplist;
2729 char headerbuf[ZPOOL_MAXPROPLEN];
2731 boolean_t first = B_TRUE;
2732 boolean_t right_justify;
2735 for (; pl != NULL; pl = pl->pl_next) {
2736 width = pl->pl_width;
2737 if (first && cb->cb_verbose) {
2739 * Reset the width to accommodate the verbose listing
2742 width = cb->cb_namewidth;
2750 right_justify = B_FALSE;
2751 if (pl->pl_prop != ZPROP_INVAL) {
2752 header = zpool_prop_column_name(pl->pl_prop);
2753 right_justify = zpool_prop_align_right(pl->pl_prop);
2757 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2758 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2759 headerbuf[i] = '\0';
2763 if (pl->pl_next == NULL && !right_justify)
2764 (void) printf("%s", header);
2765 else if (right_justify)
2766 (void) printf("%*s", width, header);
2768 (void) printf("%-*s", width, header);
2772 (void) printf("\n");
2776 * Given a pool and a list of properties, print out all the properties according
2777 * to the described layout.
2780 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2782 zprop_list_t *pl = cb->cb_proplist;
2783 boolean_t first = B_TRUE;
2784 char property[ZPOOL_MAXPROPLEN];
2786 boolean_t right_justify;
2789 for (; pl != NULL; pl = pl->pl_next) {
2791 width = pl->pl_width;
2792 if (first && cb->cb_verbose) {
2794 * Reset the width to accommodate the verbose listing
2797 width = cb->cb_namewidth;
2801 if (cb->cb_scripted)
2802 (void) printf("\t");
2809 right_justify = B_FALSE;
2810 if (pl->pl_prop != ZPROP_INVAL) {
2811 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2812 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2814 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2815 sizeof (property), NULL) != 0)
2820 right_justify = zpool_prop_align_right(pl->pl_prop);
2821 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2822 zpool_prop_unsupported(pl->pl_user_prop)) &&
2823 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2824 sizeof (property)) == 0) {
2832 * If this is being called in scripted mode, or if this is the
2833 * last column and it is left-justified, don't include a width
2836 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2837 (void) printf("%s", propstr);
2838 else if (right_justify)
2839 (void) printf("%*s", width, propstr);
2841 (void) printf("%-*s", width, propstr);
2844 (void) printf("\n");
2848 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2852 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2854 zfs_nicenum(value, propval, sizeof (propval));
2856 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2857 (void) strlcpy(propval, "-", sizeof (propval));
2860 (void) printf("\t%s", propval);
2862 (void) printf(" %*s", width, propval);
2866 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2867 list_cbdata_t *cb, int depth)
2873 boolean_t scripted = cb->cb_scripted;
2875 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2876 (uint64_t **)&vs, &c) == 0);
2880 (void) printf("\t%s", name);
2881 else if (strlen(name) + depth > cb->cb_namewidth)
2882 (void) printf("%*s%s", depth, "", name);
2884 (void) printf("%*s%s%*s", depth, "", name,
2885 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2887 /* only toplevel vdevs have capacity stats */
2888 if (vs->vs_space == 0) {
2890 (void) printf("\t-\t-\t-");
2892 (void) printf(" - - -");
2894 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2896 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2898 print_one_column(ZPOOL_PROP_FREE,
2899 vs->vs_space - vs->vs_alloc, scripted);
2901 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2903 (void) printf("\n");
2906 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2907 &child, &children) != 0)
2910 for (c = 0; c < children; c++) {
2911 uint64_t ishole = B_FALSE;
2913 if (nvlist_lookup_uint64(child[c],
2914 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2917 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2918 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2923 * Include level 2 ARC devices in iostat output
2925 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2926 &child, &children) != 0)
2930 (void) printf("%-*s - - - - - "
2931 "-\n", cb->cb_namewidth, "cache");
2932 for (c = 0; c < children; c++) {
2933 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2935 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2943 * Generic callback function to list a pool.
2946 list_callback(zpool_handle_t *zhp, void *data)
2948 list_cbdata_t *cbp = data;
2952 config = zpool_get_config(zhp, NULL);
2954 print_pool(zhp, cbp);
2955 if (!cbp->cb_verbose)
2958 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2960 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2966 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2968 * -H Scripted mode. Don't display headers, and separate properties
2970 * -o List of properties to display. Defaults to
2971 * "name,size,allocated,free,capacity,health,altroot"
2972 * -T Display a timestamp in date(1) or Unix format
2974 * List all pools in the system, whether or not they're healthy. Output space
2975 * statistics for each one, as well as health status summary.
2978 zpool_do_list(int argc, char **argv)
2982 list_cbdata_t cb = { 0 };
2983 static char default_props[] =
2984 "name,size,allocated,free,capacity,dedupratio,"
2986 char *props = default_props;
2987 unsigned long interval = 0, count = 0;
2989 boolean_t first = B_TRUE;
2992 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2995 cb.cb_scripted = B_TRUE;
3001 get_timestamp_arg(*optarg);
3004 cb.cb_verbose = B_TRUE;
3007 (void) fprintf(stderr, gettext("missing argument for "
3008 "'%c' option\n"), optopt);
3012 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3021 get_interval_count(&argc, argv, &interval, &count);
3023 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3026 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3029 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3030 (void) printf(gettext("no pools available\n"));
3031 zprop_free_list(cb.cb_proplist);
3036 pool_list_update(list);
3038 if (pool_list_count(list) == 0)
3041 cb.cb_namewidth = 0;
3042 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3044 if (timestamp_fmt != NODATE)
3045 print_timestamp(timestamp_fmt);
3047 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3051 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3056 if (count != 0 && --count == 0)
3059 (void) sleep(interval);
3062 zprop_free_list(cb.cb_proplist);
3067 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3074 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3075 &child, &children) != 0) {
3076 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3077 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3078 name += sizeof(_PATH_DEV) - 1;
3079 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3080 path += sizeof(_PATH_DEV) - 1;
3081 if (strcmp(name, path) == 0)
3086 for (c = 0; c < children; c++)
3087 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3094 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3096 boolean_t force = B_FALSE;
3099 char *poolname, *old_disk, *new_disk;
3100 zpool_handle_t *zhp;
3104 while ((c = getopt(argc, argv, "f")) != -1) {
3110 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3119 /* get pool name and check number of arguments */
3121 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3128 (void) fprintf(stderr,
3129 gettext("missing <device> specification\n"));
3137 (void) fprintf(stderr,
3138 gettext("missing <new_device> specification\n"));
3141 new_disk = old_disk;
3151 (void) fprintf(stderr, gettext("too many arguments\n"));
3155 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3158 if (zpool_get_config(zhp, NULL) == NULL) {
3159 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3165 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3167 if (nvroot == NULL) {
3172 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3174 nvlist_free(nvroot);
3181 * zpool replace [-f] <pool> <device> <new_device>
3183 * -f Force attach, even if <new_device> appears to be in use.
3185 * Replace <device> with <new_device>.
3189 zpool_do_replace(int argc, char **argv)
3191 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3195 * zpool attach [-f] <pool> <device> <new_device>
3197 * -f Force attach, even if <new_device> appears to be in use.
3199 * Attach <new_device> to the mirror containing <device>. If <device> is not
3200 * part of a mirror, then <device> will be transformed into a mirror of
3201 * <device> and <new_device>. In either case, <new_device> will begin life
3202 * with a DTL of [0, now], and will immediately begin to resilver itself.
3205 zpool_do_attach(int argc, char **argv)
3207 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3211 * zpool detach [-f] <pool> <device>
3213 * -f Force detach of <device>, even if DTLs argue against it
3214 * (not supported yet)
3216 * Detach a device from a mirror. The operation will be refused if <device>
3217 * is the last device in the mirror, or if the DTLs indicate that this device
3218 * has the only valid copy of some data.
3222 zpool_do_detach(int argc, char **argv)
3225 char *poolname, *path;
3226 zpool_handle_t *zhp;
3230 while ((c = getopt(argc, argv, "f")) != -1) {
3234 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3243 /* get pool name and check number of arguments */
3245 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3250 (void) fprintf(stderr,
3251 gettext("missing <device> specification\n"));
3258 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3261 ret = zpool_vdev_detach(zhp, path);
3269 * zpool split [-n] [-o prop=val] ...
3271 * [-R altroot] <pool> <newpool> [<device> ...]
3273 * -n Do not split the pool, but display the resulting layout if
3274 * it were to be split.
3275 * -o Set property=value, or set mount options.
3276 * -R Mount the split-off pool under an alternate root.
3278 * Splits the named pool and gives it the new pool name. Devices to be split
3279 * off may be listed, provided that no more than one device is specified
3280 * per top-level vdev mirror. The newly split pool is left in an exported
3281 * state unless -R is specified.
3283 * Restrictions: the top-level of the pool pool must only be made up of
3284 * mirrors; all devices in the pool must be healthy; no device may be
3285 * undergoing a resilvering operation.
3288 zpool_do_split(int argc, char **argv)
3290 char *srcpool, *newpool, *propval;
3291 char *mntopts = NULL;
3294 zpool_handle_t *zhp;
3295 nvlist_t *config, *props = NULL;
3297 flags.dryrun = B_FALSE;
3298 flags.import = B_FALSE;
3301 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3304 flags.import = B_TRUE;
3306 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3307 &props, B_TRUE) != 0) {
3314 flags.dryrun = B_TRUE;
3317 if ((propval = strchr(optarg, '=')) != NULL) {
3320 if (add_prop_list(optarg, propval,
3321 &props, B_TRUE) != 0) {
3331 (void) fprintf(stderr, gettext("missing argument for "
3332 "'%c' option\n"), optopt);
3336 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3343 if (!flags.import && mntopts != NULL) {
3344 (void) fprintf(stderr, gettext("setting mntopts is only "
3345 "valid when importing the pool\n"));
3353 (void) fprintf(stderr, gettext("Missing pool name\n"));
3357 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3367 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3370 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3371 if (config == NULL) {
3375 (void) printf(gettext("would create '%s' with the "
3376 "following layout:\n\n"), newpool);
3377 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3379 nvlist_free(config);
3384 if (ret != 0 || flags.dryrun || !flags.import)
3388 * The split was successful. Now we need to open the new
3389 * pool and import it.
3391 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3393 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3394 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3396 (void) fprintf(stderr, gettext("Split was successful, but "
3397 "the datasets could not all be mounted\n"));
3398 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3399 "different altroot\n"), "zpool import");
3409 * zpool online <pool> <device> ...
3412 zpool_do_online(int argc, char **argv)
3416 zpool_handle_t *zhp;
3418 vdev_state_t newstate;
3422 while ((c = getopt(argc, argv, "et")) != -1) {
3425 flags |= ZFS_ONLINE_EXPAND;
3429 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3438 /* get pool name and check number of arguments */
3440 (void) fprintf(stderr, gettext("missing pool name\n"));
3444 (void) fprintf(stderr, gettext("missing device name\n"));
3450 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3453 for (i = 1; i < argc; i++) {
3454 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3455 if (newstate != VDEV_STATE_HEALTHY) {
3456 (void) printf(gettext("warning: device '%s' "
3457 "onlined, but remains in faulted state\n"),
3459 if (newstate == VDEV_STATE_FAULTED)
3460 (void) printf(gettext("use 'zpool "
3461 "clear' to restore a faulted "
3464 (void) printf(gettext("use 'zpool "
3465 "replace' to replace devices "
3466 "that are no longer present\n"));
3479 * zpool offline [-ft] <pool> <device> ...
3481 * -f Force the device into the offline state, even if doing
3482 * so would appear to compromise pool availability.
3483 * (not supported yet)
3485 * -t Only take the device off-line temporarily. The offline
3486 * state will not be persistent across reboots.
3490 zpool_do_offline(int argc, char **argv)
3494 zpool_handle_t *zhp;
3496 boolean_t istmp = B_FALSE;
3499 while ((c = getopt(argc, argv, "ft")) != -1) {
3506 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3515 /* get pool name and check number of arguments */
3517 (void) fprintf(stderr, gettext("missing pool name\n"));
3521 (void) fprintf(stderr, gettext("missing device name\n"));
3527 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3530 for (i = 1; i < argc; i++) {
3531 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3541 * zpool clear <pool> [device]
3543 * Clear all errors associated with a pool or a particular device.
3546 zpool_do_clear(int argc, char **argv)
3550 boolean_t dryrun = B_FALSE;
3551 boolean_t do_rewind = B_FALSE;
3552 boolean_t xtreme_rewind = B_FALSE;
3553 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3554 nvlist_t *policy = NULL;
3555 zpool_handle_t *zhp;
3556 char *pool, *device;
3559 while ((c = getopt(argc, argv, "FnX")) != -1) {
3568 xtreme_rewind = B_TRUE;
3571 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3581 (void) fprintf(stderr, gettext("missing pool name\n"));
3586 (void) fprintf(stderr, gettext("too many arguments\n"));
3590 if ((dryrun || xtreme_rewind) && !do_rewind) {
3591 (void) fprintf(stderr,
3592 gettext("-n or -X only meaningful with -F\n"));
3596 rewind_policy = ZPOOL_TRY_REWIND;
3598 rewind_policy = ZPOOL_DO_REWIND;
3600 rewind_policy |= ZPOOL_EXTREME_REWIND;
3602 /* In future, further rewind policy choices can be passed along here */
3603 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3604 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3608 device = argc == 2 ? argv[1] : NULL;
3610 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3611 nvlist_free(policy);
3615 if (zpool_clear(zhp, device, policy) != 0)
3620 nvlist_free(policy);
3626 * zpool reguid <pool>
3629 zpool_do_reguid(int argc, char **argv)
3633 zpool_handle_t *zhp;
3637 while ((c = getopt(argc, argv, "")) != -1) {
3640 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3649 /* get pool name and check number of arguments */
3651 (void) fprintf(stderr, gettext("missing pool name\n"));
3656 (void) fprintf(stderr, gettext("too many arguments\n"));
3661 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3664 ret = zpool_reguid(zhp);
3672 * zpool reopen <pool>
3674 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3676 * NOTE: This command is currently undocumented. If the command is ever
3677 * exposed then the appropriate usage() messages will need to be made.
3680 zpool_do_reopen(int argc, char **argv)
3683 zpool_handle_t *zhp;
3693 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3696 ret = zpool_reopen(zhp);
3701 typedef struct scrub_cbdata {
3708 scrub_callback(zpool_handle_t *zhp, void *data)
3710 scrub_cbdata_t *cb = data;
3714 * Ignore faulted pools.
3716 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3717 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3718 "currently unavailable\n"), zpool_get_name(zhp));
3722 err = zpool_scan(zhp, cb->cb_type);
3728 * zpool scrub [-s] <pool> ...
3730 * -s Stop. Stops any in-progress scrub.
3733 zpool_do_scrub(int argc, char **argv)
3738 cb.cb_type = POOL_SCAN_SCRUB;
3741 while ((c = getopt(argc, argv, "s")) != -1) {
3744 cb.cb_type = POOL_SCAN_NONE;
3747 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3759 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3763 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3766 typedef struct status_cbdata {
3768 boolean_t cb_allpools;
3769 boolean_t cb_verbose;
3770 boolean_t cb_explain;
3772 boolean_t cb_dedup_stats;
3776 * Print out detailed scrub status.
3779 print_scan_status(pool_scan_stat_t *ps)
3782 uint64_t elapsed, mins_left, hours_left;
3783 uint64_t pass_exam, examined, total;
3785 double fraction_done;
3786 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3788 (void) printf(gettext(" scan: "));
3790 /* If there's never been a scan, there's not much to say. */
3791 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3792 ps->pss_func >= POOL_SCAN_FUNCS) {
3793 (void) printf(gettext("none requested\n"));
3797 start = ps->pss_start_time;
3798 end = ps->pss_end_time;
3799 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3801 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3802 ps->pss_func == POOL_SCAN_RESILVER);
3804 * Scan is finished or canceled.
3806 if (ps->pss_state == DSS_FINISHED) {
3807 uint64_t minutes_taken = (end - start) / 60;
3810 if (ps->pss_func == POOL_SCAN_SCRUB) {
3811 fmt = gettext("scrub repaired %s in %lluh%um with "
3812 "%llu errors on %s");
3813 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3814 fmt = gettext("resilvered %s in %lluh%um with "
3815 "%llu errors on %s");
3818 (void) printf(fmt, processed_buf,
3819 (u_longlong_t)(minutes_taken / 60),
3820 (uint_t)(minutes_taken % 60),
3821 (u_longlong_t)ps->pss_errors,
3822 ctime((time_t *)&end));
3824 } else if (ps->pss_state == DSS_CANCELED) {
3825 if (ps->pss_func == POOL_SCAN_SCRUB) {
3826 (void) printf(gettext("scrub canceled on %s"),
3828 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3829 (void) printf(gettext("resilver canceled on %s"),
3835 assert(ps->pss_state == DSS_SCANNING);
3838 * Scan is in progress.
3840 if (ps->pss_func == POOL_SCAN_SCRUB) {
3841 (void) printf(gettext("scrub in progress since %s"),
3843 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3844 (void) printf(gettext("resilver in progress since %s"),
3848 examined = ps->pss_examined ? ps->pss_examined : 1;
3849 total = ps->pss_to_examine;
3850 fraction_done = (double)examined / total;
3852 /* elapsed time for this pass */
3853 elapsed = time(NULL) - ps->pss_pass_start;
3854 elapsed = elapsed ? elapsed : 1;
3855 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3856 rate = pass_exam / elapsed;
3857 rate = rate ? rate : 1;
3858 mins_left = ((total - examined) / rate) / 60;
3859 hours_left = mins_left / 60;
3861 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3862 zfs_nicenum(total, total_buf, sizeof (total_buf));
3863 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3866 * do not print estimated time if hours_left is more than 30 days
3868 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3869 examined_buf, total_buf, rate_buf);
3870 if (hours_left < (30 * 24)) {
3871 (void) printf(gettext(", %lluh%um to go\n"),
3872 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3874 (void) printf(gettext(
3875 ", (scan is slow, no estimated time)\n"));
3878 if (ps->pss_func == POOL_SCAN_RESILVER) {
3879 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3880 processed_buf, 100 * fraction_done);
3881 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3882 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3883 processed_buf, 100 * fraction_done);
3888 print_error_log(zpool_handle_t *zhp)
3890 nvlist_t *nverrlist = NULL;
3893 size_t len = MAXPATHLEN * 2;
3895 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3896 (void) printf("errors: List of errors unavailable "
3897 "(insufficient privileges)\n");
3901 (void) printf("errors: Permanent errors have been "
3902 "detected in the following files:\n\n");
3904 pathname = safe_malloc(len);
3906 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3908 uint64_t dsobj, obj;
3910 verify(nvpair_value_nvlist(elem, &nv) == 0);
3911 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3913 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3915 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3916 (void) printf("%7s %s\n", "", pathname);
3919 nvlist_free(nverrlist);
3923 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3932 (void) printf(gettext("\tspares\n"));
3934 for (i = 0; i < nspares; i++) {
3935 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3936 print_status_config(zhp, name, spares[i],
3937 namewidth, 2, B_TRUE);
3943 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3952 (void) printf(gettext("\tcache\n"));
3954 for (i = 0; i < nl2cache; i++) {
3955 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3956 print_status_config(zhp, name, l2cache[i],
3957 namewidth, 2, B_FALSE);
3963 print_dedup_stats(nvlist_t *config)
3965 ddt_histogram_t *ddh;
3971 * If the pool was faulted then we may not have been able to
3972 * obtain the config. Otherwise, if have anything in the dedup
3973 * table continue processing the stats.
3975 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3976 (uint64_t **)&ddo, &c) != 0)
3979 (void) printf("\n");
3980 (void) printf(gettext(" dedup: "));
3981 if (ddo->ddo_count == 0) {
3982 (void) printf(gettext("no DDT entries\n"));
3986 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3987 (u_longlong_t)ddo->ddo_count,
3988 (u_longlong_t)ddo->ddo_dspace,
3989 (u_longlong_t)ddo->ddo_mspace);
3991 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3992 (uint64_t **)&dds, &c) == 0);
3993 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3994 (uint64_t **)&ddh, &c) == 0);
3995 zpool_dump_ddt(dds, ddh);
3999 * Display a summary of pool status. Displays a summary such as:
4003 * reason: One or more devices ...
4004 * see: http://illumos.org/msg/ZFS-xxxx-01
4010 * When given the '-v' option, we print out the complete config. If the '-e'
4011 * option is specified, then we print out error rate information as well.
4014 status_callback(zpool_handle_t *zhp, void *data)
4016 status_cbdata_t *cbp = data;
4017 nvlist_t *config, *nvroot;
4024 config = zpool_get_config(zhp, NULL);
4025 reason = zpool_get_status(zhp, &msgid);
4030 * If we were given 'zpool status -x', only report those pools with
4033 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
4034 if (!cbp->cb_allpools) {
4035 (void) printf(gettext("pool '%s' is healthy\n"),
4036 zpool_get_name(zhp));
4038 cbp->cb_first = B_FALSE;
4044 cbp->cb_first = B_FALSE;
4046 (void) printf("\n");
4048 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4050 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4051 (uint64_t **)&vs, &c) == 0);
4052 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4054 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4055 (void) printf(gettext(" state: %s\n"), health);
4058 case ZPOOL_STATUS_MISSING_DEV_R:
4059 (void) printf(gettext("status: One or more devices could not "
4060 "be opened. Sufficient replicas exist for\n\tthe pool to "
4061 "continue functioning in a degraded state.\n"));
4062 (void) printf(gettext("action: Attach the missing device and "
4063 "online it using 'zpool online'.\n"));
4066 case ZPOOL_STATUS_MISSING_DEV_NR:
4067 (void) printf(gettext("status: One or more devices could not "
4068 "be opened. There are insufficient\n\treplicas for the "
4069 "pool to continue functioning.\n"));
4070 (void) printf(gettext("action: Attach the missing device and "
4071 "online it using 'zpool online'.\n"));
4074 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4075 (void) printf(gettext("status: One or more devices could not "
4076 "be used because the label is missing or\n\tinvalid. "
4077 "Sufficient replicas exist for the pool to continue\n\t"
4078 "functioning in a degraded state.\n"));
4079 (void) printf(gettext("action: Replace the device using "
4080 "'zpool replace'.\n"));
4083 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4084 (void) printf(gettext("status: One or more devices could not "
4085 "be used because the label is missing \n\tor invalid. "
4086 "There are insufficient replicas for the pool to "
4087 "continue\n\tfunctioning.\n"));
4088 zpool_explain_recover(zpool_get_handle(zhp),
4089 zpool_get_name(zhp), reason, config);
4092 case ZPOOL_STATUS_FAILING_DEV:
4093 (void) printf(gettext("status: One or more devices has "
4094 "experienced an unrecoverable error. An\n\tattempt was "
4095 "made to correct the error. Applications are "
4097 (void) printf(gettext("action: Determine if the device needs "
4098 "to be replaced, and clear the errors\n\tusing "
4099 "'zpool clear' or replace the device with 'zpool "
4103 case ZPOOL_STATUS_OFFLINE_DEV:
4104 (void) printf(gettext("status: One or more devices has "
4105 "been taken offline by the administrator.\n\tSufficient "
4106 "replicas exist for the pool to continue functioning in "
4107 "a\n\tdegraded state.\n"));
4108 (void) printf(gettext("action: Online the device using "
4109 "'zpool online' or replace the device with\n\t'zpool "
4113 case ZPOOL_STATUS_REMOVED_DEV:
4114 (void) printf(gettext("status: One or more devices has "
4115 "been removed by the administrator.\n\tSufficient "
4116 "replicas exist for the pool to continue functioning in "
4117 "a\n\tdegraded state.\n"));
4118 (void) printf(gettext("action: Online the device using "
4119 "'zpool online' or replace the device with\n\t'zpool "
4123 case ZPOOL_STATUS_RESILVERING:
4124 (void) printf(gettext("status: One or more devices is "
4125 "currently being resilvered. The pool will\n\tcontinue "
4126 "to function, possibly in a degraded state.\n"));
4127 (void) printf(gettext("action: Wait for the resilver to "
4131 case ZPOOL_STATUS_CORRUPT_DATA:
4132 (void) printf(gettext("status: One or more devices has "
4133 "experienced an error resulting in data\n\tcorruption. "
4134 "Applications may be affected.\n"));
4135 (void) printf(gettext("action: Restore the file in question "
4136 "if possible. Otherwise restore the\n\tentire pool from "
4140 case ZPOOL_STATUS_CORRUPT_POOL:
4141 (void) printf(gettext("status: The pool metadata is corrupted "
4142 "and the pool cannot be opened.\n"));
4143 zpool_explain_recover(zpool_get_handle(zhp),
4144 zpool_get_name(zhp), reason, config);
4147 case ZPOOL_STATUS_VERSION_OLDER:
4148 (void) printf(gettext("status: The pool is formatted using a "
4149 "legacy on-disk format. The pool can\n\tstill be used, "
4150 "but some features are unavailable.\n"));
4151 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4152 "upgrade'. Once this is done, the\n\tpool will no longer "
4153 "be accessible on software that does not support feature\n"
4157 case ZPOOL_STATUS_VERSION_NEWER:
4158 (void) printf(gettext("status: The pool has been upgraded to a "
4159 "newer, incompatible on-disk version.\n\tThe pool cannot "
4160 "be accessed on this system.\n"));
4161 (void) printf(gettext("action: Access the pool from a system "
4162 "running more recent software, or\n\trestore the pool from "
4166 case ZPOOL_STATUS_FEAT_DISABLED:
4167 (void) printf(gettext("status: Some supported features are not "
4168 "enabled on the pool. The pool can\n\tstill be used, but "
4169 "some features are unavailable.\n"));
4170 (void) printf(gettext("action: Enable all features using "
4171 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4172 "longer be accessible by software that does not support\n\t"
4173 "the features. See zpool-features(7) for details.\n"));
4176 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4177 (void) printf(gettext("status: The pool cannot be accessed on "
4178 "this system because it uses the\n\tfollowing feature(s) "
4179 "not supported on this system:\n"));
4180 zpool_print_unsup_feat(config);
4181 (void) printf("\n");
4182 (void) printf(gettext("action: Access the pool from a system "
4183 "that supports the required feature(s),\n\tor restore the "
4184 "pool from backup.\n"));
4187 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4188 (void) printf(gettext("status: The pool can only be accessed "
4189 "in read-only mode on this system. It\n\tcannot be "
4190 "accessed in read-write mode because it uses the "
4191 "following\n\tfeature(s) not supported on this system:\n"));
4192 zpool_print_unsup_feat(config);
4193 (void) printf("\n");
4194 (void) printf(gettext("action: The pool cannot be accessed in "
4195 "read-write mode. Import the pool with\n"
4196 "\t\"-o readonly=on\", access the pool from a system that "
4197 "supports the\n\trequired feature(s), or restore the "
4198 "pool from backup.\n"));
4201 case ZPOOL_STATUS_FAULTED_DEV_R:
4202 (void) printf(gettext("status: One or more devices are "
4203 "faulted in response to persistent errors.\n\tSufficient "
4204 "replicas exist for the pool to continue functioning "
4205 "in a\n\tdegraded state.\n"));
4206 (void) printf(gettext("action: Replace the faulted device, "
4207 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4210 case ZPOOL_STATUS_FAULTED_DEV_NR:
4211 (void) printf(gettext("status: One or more devices are "
4212 "faulted in response to persistent errors. There are "
4213 "insufficient replicas for the pool to\n\tcontinue "
4215 (void) printf(gettext("action: Destroy and re-create the pool "
4216 "from a backup source. Manually marking the device\n"
4217 "\trepaired using 'zpool clear' may allow some data "
4218 "to be recovered.\n"));
4221 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4222 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4223 (void) printf(gettext("status: One or more devices are "
4224 "faulted in response to IO failures.\n"));
4225 (void) printf(gettext("action: Make sure the affected devices "
4226 "are connected, then run 'zpool clear'.\n"));
4229 case ZPOOL_STATUS_BAD_LOG:
4230 (void) printf(gettext("status: An intent log record "
4231 "could not be read.\n"
4232 "\tWaiting for adminstrator intervention to fix the "
4233 "faulted pool.\n"));
4234 (void) printf(gettext("action: Either restore the affected "
4235 "device(s) and run 'zpool online',\n"
4236 "\tor ignore the intent log records by running "
4237 "'zpool clear'.\n"));
4242 * The remaining errors can't actually be generated, yet.
4244 assert(reason == ZPOOL_STATUS_OK);
4248 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4251 if (config != NULL) {
4254 nvlist_t **spares, **l2cache;
4255 uint_t nspares, nl2cache;
4256 pool_scan_stat_t *ps = NULL;
4258 (void) nvlist_lookup_uint64_array(nvroot,
4259 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4260 print_scan_status(ps);
4262 namewidth = max_width(zhp, nvroot, 0, 0);
4266 (void) printf(gettext("config:\n\n"));
4267 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4268 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4269 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4270 namewidth, 0, B_FALSE);
4272 if (num_logs(nvroot) > 0)
4273 print_logs(zhp, nvroot, namewidth, B_TRUE);
4274 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4275 &l2cache, &nl2cache) == 0)
4276 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4278 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4279 &spares, &nspares) == 0)
4280 print_spares(zhp, spares, nspares, namewidth);
4282 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4284 nvlist_t *nverrlist = NULL;
4287 * If the approximate error count is small, get a
4288 * precise count by fetching the entire log and
4289 * uniquifying the results.
4291 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4292 zpool_get_errlog(zhp, &nverrlist) == 0) {
4297 while ((elem = nvlist_next_nvpair(nverrlist,
4302 nvlist_free(nverrlist);
4304 (void) printf("\n");
4307 (void) printf(gettext("errors: No known data "
4309 else if (!cbp->cb_verbose)
4310 (void) printf(gettext("errors: %llu data "
4311 "errors, use '-v' for a list\n"),
4312 (u_longlong_t)nerr);
4314 print_error_log(zhp);
4317 if (cbp->cb_dedup_stats)
4318 print_dedup_stats(config);
4320 (void) printf(gettext("config: The configuration cannot be "
4328 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4330 * -v Display complete error logs
4331 * -x Display only pools with potential problems
4332 * -D Display dedup status (undocumented)
4333 * -T Display a timestamp in date(1) or Unix format
4335 * Describes the health status of all pools or some subset.
4338 zpool_do_status(int argc, char **argv)
4342 unsigned long interval = 0, count = 0;
4343 status_cbdata_t cb = { 0 };
4346 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4349 cb.cb_verbose = B_TRUE;
4352 cb.cb_explain = B_TRUE;
4355 cb.cb_dedup_stats = B_TRUE;
4358 get_timestamp_arg(*optarg);
4361 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4370 get_interval_count(&argc, argv, &interval, &count);
4373 cb.cb_allpools = B_TRUE;
4375 cb.cb_first = B_TRUE;
4378 if (timestamp_fmt != NODATE)
4379 print_timestamp(timestamp_fmt);
4381 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4382 status_callback, &cb);
4384 if (argc == 0 && cb.cb_count == 0)
4385 (void) printf(gettext("no pools available\n"));
4386 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4387 (void) printf(gettext("all pools are healthy\n"));
4395 if (count != 0 && --count == 0)
4398 (void) sleep(interval);
4404 typedef struct upgrade_cbdata {
4406 char cb_poolname[ZPOOL_MAXNAMELEN];
4408 uint64_t cb_version;
4414 is_root_pool(zpool_handle_t *zhp)
4416 static struct statfs sfs;
4417 static char *poolname = NULL;
4418 static boolean_t stated = B_FALSE;
4423 if (statfs("/", &sfs) == -1) {
4424 (void) fprintf(stderr,
4425 "Unable to stat root file system: %s.\n",
4429 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4431 poolname = sfs.f_mntfromname;
4432 if ((slash = strchr(poolname, '/')) != NULL)
4435 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4439 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4441 if (poolname[0] == '\0' && is_root_pool(zhp))
4442 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4444 #endif /* FreeBSD */
4447 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4451 uint64_t oldversion;
4453 config = zpool_get_config(zhp, NULL);
4454 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4457 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4458 assert(oldversion < version);
4460 ret = zpool_upgrade(zhp, version);
4464 if (version >= SPA_VERSION_FEATURES) {
4465 (void) printf(gettext("Successfully upgraded "
4466 "'%s' from version %llu to feature flags.\n"),
4467 zpool_get_name(zhp), oldversion);
4469 (void) printf(gettext("Successfully upgraded "
4470 "'%s' from version %llu to version %llu.\n"),
4471 zpool_get_name(zhp), oldversion, version);
4478 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4481 boolean_t firstff = B_TRUE;
4482 nvlist_t *enabled = zpool_get_features(zhp);
4485 for (i = 0; i < SPA_FEATURES; i++) {
4486 const char *fname = spa_feature_table[i].fi_uname;
4487 const char *fguid = spa_feature_table[i].fi_guid;
4488 if (!nvlist_exists(enabled, fguid)) {
4490 verify(-1 != asprintf(&propname, "feature@%s", fname));
4491 ret = zpool_set_prop(zhp, propname,
4492 ZFS_FEATURE_ENABLED);
4500 (void) printf(gettext("Enabled the "
4501 "following features on '%s':\n"),
4502 zpool_get_name(zhp));
4505 (void) printf(gettext(" %s\n"), fname);
4516 upgrade_cb(zpool_handle_t *zhp, void *arg)
4518 upgrade_cbdata_t *cbp = arg;
4521 boolean_t printnl = B_FALSE;
4524 config = zpool_get_config(zhp, NULL);
4525 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4528 assert(SPA_VERSION_IS_SUPPORTED(version));
4530 if (version < cbp->cb_version) {
4531 cbp->cb_first = B_FALSE;
4532 ret = upgrade_version(zhp, cbp->cb_version);
4536 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4537 sizeof(cbp->cb_poolname));
4538 #endif /* ___FreeBSD__ */
4543 * If they did "zpool upgrade -a", then we could
4544 * be doing ioctls to different pools. We need
4545 * to log this history once to each pool, and bypass
4546 * the normal history logging that happens in main().
4548 (void) zpool_log_history(g_zfs, history_str);
4549 log_history = B_FALSE;
4553 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4555 ret = upgrade_enable_all(zhp, &count);
4560 cbp->cb_first = B_FALSE;
4566 (void) printf(gettext("\n"));
4573 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4575 upgrade_cbdata_t *cbp = arg;
4579 config = zpool_get_config(zhp, NULL);
4580 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4583 assert(SPA_VERSION_IS_SUPPORTED(version));
4585 if (version < SPA_VERSION_FEATURES) {
4586 if (cbp->cb_first) {
4587 (void) printf(gettext("The following pools are "
4588 "formatted with legacy version numbers and can\n"
4589 "be upgraded to use feature flags. After "
4590 "being upgraded, these pools\nwill no "
4591 "longer be accessible by software that does not "
4592 "support feature\nflags.\n\n"));
4593 (void) printf(gettext("VER POOL\n"));
4594 (void) printf(gettext("--- ------------\n"));
4595 cbp->cb_first = B_FALSE;
4598 (void) printf("%2llu %s\n", (u_longlong_t)version,
4599 zpool_get_name(zhp));
4606 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4608 upgrade_cbdata_t *cbp = arg;
4612 config = zpool_get_config(zhp, NULL);
4613 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4616 if (version >= SPA_VERSION_FEATURES) {
4618 boolean_t poolfirst = B_TRUE;
4619 nvlist_t *enabled = zpool_get_features(zhp);
4621 for (i = 0; i < SPA_FEATURES; i++) {
4622 const char *fguid = spa_feature_table[i].fi_guid;
4623 const char *fname = spa_feature_table[i].fi_uname;
4624 if (!nvlist_exists(enabled, fguid)) {
4625 if (cbp->cb_first) {
4626 (void) printf(gettext("\nSome "
4627 "supported features are not "
4628 "enabled on the following pools. "
4629 "Once a\nfeature is enabled the "
4630 "pool may become incompatible with "
4631 "software\nthat does not support "
4633 "zpool-features(7) for "
4635 (void) printf(gettext("POOL "
4637 (void) printf(gettext("------"
4639 cbp->cb_first = B_FALSE;
4643 (void) printf(gettext("%s\n"),
4644 zpool_get_name(zhp));
4645 poolfirst = B_FALSE;
4648 (void) printf(gettext(" %s\n"), fname);
4658 upgrade_one(zpool_handle_t *zhp, void *data)
4660 boolean_t printnl = B_FALSE;
4661 upgrade_cbdata_t *cbp = data;
4662 uint64_t cur_version;
4665 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4666 (void) printf(gettext("'log' is now a reserved word\n"
4667 "Pool 'log' must be renamed using export and import"
4672 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4673 if (cur_version > cbp->cb_version) {
4674 (void) printf(gettext("Pool '%s' is already formatted "
4675 "using more current version '%llu'.\n\n"),
4676 zpool_get_name(zhp), cur_version);
4680 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4681 (void) printf(gettext("Pool '%s' is already formatted "
4682 "using version %llu.\n\n"), zpool_get_name(zhp),
4687 if (cur_version != cbp->cb_version) {
4689 ret = upgrade_version(zhp, cbp->cb_version);
4693 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4694 sizeof(cbp->cb_poolname));
4695 #endif /* ___FreeBSD__ */
4698 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4700 ret = upgrade_enable_all(zhp, &count);
4707 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4708 sizeof(cbp->cb_poolname));
4709 #endif /* __FreeBSD __*/
4710 } else if (cur_version == SPA_VERSION) {
4711 (void) printf(gettext("Pool '%s' already has all "
4712 "supported features enabled.\n"),
4713 zpool_get_name(zhp));
4718 (void) printf(gettext("\n"));
4727 * zpool upgrade [-V version] <-a | pool ...>
4729 * With no arguments, display downrev'd ZFS pool available for upgrade.
4730 * Individual pools can be upgraded by specifying the pool, and '-a' will
4731 * upgrade all pools.
4734 zpool_do_upgrade(int argc, char **argv)
4737 upgrade_cbdata_t cb = { 0 };
4739 boolean_t showversions = B_FALSE;
4740 boolean_t upgradeall = B_FALSE;
4745 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4748 upgradeall = B_TRUE;
4751 showversions = B_TRUE;
4754 cb.cb_version = strtoll(optarg, &end, 10);
4756 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4757 (void) fprintf(stderr,
4758 gettext("invalid version '%s'\n"), optarg);
4763 (void) fprintf(stderr, gettext("missing argument for "
4764 "'%c' option\n"), optopt);
4768 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4779 if (cb.cb_version == 0) {
4780 cb.cb_version = SPA_VERSION;
4781 } else if (!upgradeall && argc == 0) {
4782 (void) fprintf(stderr, gettext("-V option is "
4783 "incompatible with other arguments\n"));
4788 if (upgradeall || argc != 0) {
4789 (void) fprintf(stderr, gettext("-v option is "
4790 "incompatible with other arguments\n"));
4793 } else if (upgradeall) {
4795 (void) fprintf(stderr, gettext("-a option should not "
4796 "be used along with a pool name\n"));
4801 (void) printf(gettext("This system supports ZFS pool feature "
4806 (void) printf(gettext("The following features are "
4808 (void) printf(gettext("FEAT DESCRIPTION\n"));
4809 (void) printf("----------------------------------------------"
4810 "---------------\n");
4811 for (i = 0; i < SPA_FEATURES; i++) {
4812 zfeature_info_t *fi = &spa_feature_table[i];
4813 const char *ro = fi->fi_can_readonly ?
4814 " (read-only compatible)" : "";
4816 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4817 (void) printf(" %s\n", fi->fi_desc);
4819 (void) printf("\n");
4821 (void) printf(gettext("The following legacy versions are also "
4823 (void) printf(gettext("VER DESCRIPTION\n"));
4824 (void) printf("--- -----------------------------------------"
4825 "---------------\n");
4826 (void) printf(gettext(" 1 Initial ZFS version\n"));
4827 (void) printf(gettext(" 2 Ditto blocks "
4828 "(replicated metadata)\n"));
4829 (void) printf(gettext(" 3 Hot spares and double parity "
4831 (void) printf(gettext(" 4 zpool history\n"));
4832 (void) printf(gettext(" 5 Compression using the gzip "
4834 (void) printf(gettext(" 6 bootfs pool property\n"));
4835 (void) printf(gettext(" 7 Separate intent log devices\n"));
4836 (void) printf(gettext(" 8 Delegated administration\n"));
4837 (void) printf(gettext(" 9 refquota and refreservation "
4839 (void) printf(gettext(" 10 Cache devices\n"));
4840 (void) printf(gettext(" 11 Improved scrub performance\n"));
4841 (void) printf(gettext(" 12 Snapshot properties\n"));
4842 (void) printf(gettext(" 13 snapused property\n"));
4843 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4844 (void) printf(gettext(" 15 user/group space accounting\n"));
4845 (void) printf(gettext(" 16 stmf property support\n"));
4846 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4847 (void) printf(gettext(" 18 Snapshot user holds\n"));
4848 (void) printf(gettext(" 19 Log device removal\n"));
4849 (void) printf(gettext(" 20 Compression using zle "
4850 "(zero-length encoding)\n"));
4851 (void) printf(gettext(" 21 Deduplication\n"));
4852 (void) printf(gettext(" 22 Received properties\n"));
4853 (void) printf(gettext(" 23 Slim ZIL\n"));
4854 (void) printf(gettext(" 24 System attributes\n"));
4855 (void) printf(gettext(" 25 Improved scrub stats\n"));
4856 (void) printf(gettext(" 26 Improved snapshot deletion "
4858 (void) printf(gettext(" 27 Improved snapshot creation "
4860 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4861 (void) printf(gettext("\nFor more information on a particular "
4862 "version, including supported releases,\n"));
4863 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4864 } else if (argc == 0 && upgradeall) {
4865 cb.cb_first = B_TRUE;
4866 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4867 if (ret == 0 && cb.cb_first) {
4868 if (cb.cb_version == SPA_VERSION) {
4869 (void) printf(gettext("All pools are already "
4870 "formatted using feature flags.\n\n"));
4871 (void) printf(gettext("Every feature flags "
4872 "pool already has all supported features "
4875 (void) printf(gettext("All pools are already "
4876 "formatted with version %llu or higher.\n"),
4880 } else if (argc == 0) {
4881 cb.cb_first = B_TRUE;
4882 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4886 (void) printf(gettext("All pools are formatted "
4887 "using feature flags.\n\n"));
4889 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4890 "for a list of available legacy versions.\n"));
4893 cb.cb_first = B_TRUE;
4894 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4898 (void) printf(gettext("Every feature flags pool has "
4899 "all supported features enabled.\n"));
4901 (void) printf(gettext("\n"));
4904 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4908 if (cb.cb_poolname[0] != '\0') {
4910 "If you boot from pool '%s', don't forget to update boot code.\n"
4911 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4912 "the following command will do it:\n"
4914 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4921 typedef struct hist_cbdata {
4928 * Print out the command history for a specific pool.
4931 get_history_one(zpool_handle_t *zhp, void *data)
4947 char internalstr[MAXPATHLEN];
4948 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4952 cb->first = B_FALSE;
4954 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4956 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4959 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4960 &records, &numrecords) == 0);
4961 for (i = 0; i < numrecords; i++) {
4962 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4966 /* is it an internal event or a standard event? */
4967 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4969 if (cb->internal == 0)
4972 if (nvlist_lookup_uint64(records[i],
4973 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4975 verify(nvlist_lookup_uint64(records[i],
4976 ZPOOL_HIST_TXG, &txg) == 0);
4977 verify(nvlist_lookup_string(records[i],
4978 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4979 if (ievent >= LOG_END)
4981 (void) snprintf(internalstr,
4982 sizeof (internalstr),
4983 "[internal %s txg:%lld] %s",
4984 zfs_history_event_names[ievent], txg,
4986 cmdstr = internalstr;
4989 (void) localtime_r(&tsec, &t);
4990 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4991 (void) printf("%s %s", tbuf, cmdstr);
4994 (void) printf("\n");
4997 (void) printf(" [");
4998 if (nvlist_lookup_uint64(records[i],
4999 ZPOOL_HIST_WHO, &who) == 0) {
5000 pwd = getpwuid((uid_t)who);
5002 (void) printf("user %s on",
5005 (void) printf("user %d on",
5008 (void) printf(gettext("no info]\n"));
5011 if (nvlist_lookup_string(records[i],
5012 ZPOOL_HIST_HOST, &hostname) == 0) {
5013 (void) printf(" %s", hostname);
5015 if (nvlist_lookup_string(records[i],
5016 ZPOOL_HIST_ZONE, &zonename) == 0) {
5017 (void) printf(":%s", zonename);
5021 (void) printf("\n");
5023 (void) printf("\n");
5030 * zpool history <pool>
5032 * Displays the history of commands that modified pools.
5037 zpool_do_history(int argc, char **argv)
5039 hist_cbdata_t cbdata = { 0 };
5043 cbdata.first = B_TRUE;
5045 while ((c = getopt(argc, argv, "li")) != -1) {
5051 cbdata.internal = 1;
5054 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5062 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5065 if (argc == 0 && cbdata.first == B_TRUE) {
5066 (void) printf(gettext("no pools available\n"));
5074 get_callback(zpool_handle_t *zhp, void *data)
5076 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5077 char value[MAXNAMELEN];
5078 zprop_source_t srctype;
5081 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5084 * Skip the special fake placeholder. This will also skip
5085 * over the name property when 'all' is specified.
5087 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5088 pl == cbp->cb_proplist)
5091 if (pl->pl_prop == ZPROP_INVAL &&
5092 (zpool_prop_feature(pl->pl_user_prop) ||
5093 zpool_prop_unsupported(pl->pl_user_prop))) {
5094 srctype = ZPROP_SRC_LOCAL;
5096 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5097 value, sizeof (value)) == 0) {
5098 zprop_print_one_property(zpool_get_name(zhp),
5099 cbp, pl->pl_user_prop, value, srctype,
5103 if (zpool_get_prop(zhp, pl->pl_prop, value,
5104 sizeof (value), &srctype) != 0)
5107 zprop_print_one_property(zpool_get_name(zhp), cbp,
5108 zpool_prop_to_name(pl->pl_prop), value, srctype,
5116 zpool_do_get(int argc, char **argv)
5118 zprop_get_cbdata_t cb = { 0 };
5119 zprop_list_t fake_name = { 0 };
5123 (void) fprintf(stderr, gettext("missing property "
5128 cb.cb_first = B_TRUE;
5129 cb.cb_sources = ZPROP_SRC_ALL;
5130 cb.cb_columns[0] = GET_COL_NAME;
5131 cb.cb_columns[1] = GET_COL_PROPERTY;
5132 cb.cb_columns[2] = GET_COL_VALUE;
5133 cb.cb_columns[3] = GET_COL_SOURCE;
5134 cb.cb_type = ZFS_TYPE_POOL;
5136 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5137 ZFS_TYPE_POOL) != 0)
5140 if (cb.cb_proplist != NULL) {
5141 fake_name.pl_prop = ZPOOL_PROP_NAME;
5142 fake_name.pl_width = strlen(gettext("NAME"));
5143 fake_name.pl_next = cb.cb_proplist;
5144 cb.cb_proplist = &fake_name;
5147 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5150 if (cb.cb_proplist == &fake_name)
5151 zprop_free_list(fake_name.pl_next);
5153 zprop_free_list(cb.cb_proplist);
5158 typedef struct set_cbdata {
5161 boolean_t cb_any_successful;
5165 set_callback(zpool_handle_t *zhp, void *data)
5168 set_cbdata_t *cb = (set_cbdata_t *)data;
5170 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5173 cb->cb_any_successful = B_TRUE;
5179 zpool_do_set(int argc, char **argv)
5181 set_cbdata_t cb = { 0 };
5184 if (argc > 1 && argv[1][0] == '-') {
5185 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5191 (void) fprintf(stderr, gettext("missing property=value "
5197 (void) fprintf(stderr, gettext("missing pool name\n"));
5202 (void) fprintf(stderr, gettext("too many pool names\n"));
5206 cb.cb_propname = argv[1];
5207 cb.cb_value = strchr(cb.cb_propname, '=');
5208 if (cb.cb_value == NULL) {
5209 (void) fprintf(stderr, gettext("missing value in "
5210 "property=value argument\n"));
5214 *(cb.cb_value) = '\0';
5217 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5224 find_command_idx(char *command, int *idx)
5228 for (i = 0; i < NCOMMAND; i++) {
5229 if (command_table[i].name == NULL)
5232 if (strcmp(command, command_table[i].name) == 0) {
5241 main(int argc, char **argv)
5247 (void) setlocale(LC_ALL, "");
5248 (void) textdomain(TEXT_DOMAIN);
5250 if ((g_zfs = libzfs_init()) == NULL) {
5251 (void) fprintf(stderr, gettext("internal error: failed to "
5252 "initialize ZFS library\n"));
5256 libzfs_print_on_error(g_zfs, B_TRUE);
5261 * Make sure the user has specified some command.
5264 (void) fprintf(stderr, gettext("missing command\n"));
5273 if (strcmp(cmdname, "-?") == 0)
5276 zpool_set_history_str("zpool", argc, argv, history_str);
5277 verify(zpool_stage_history(g_zfs, history_str) == 0);
5280 * Run the appropriate command.
5282 if (find_command_idx(cmdname, &i) == 0) {
5283 current_command = &command_table[i];
5284 ret = command_table[i].func(argc - 1, argv + 1);
5285 } else if (strchr(cmdname, '=')) {
5286 verify(find_command_idx("set", &i) == 0);
5287 current_command = &command_table[i];
5288 ret = command_table[i].func(argc, argv);
5289 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5291 * 'freeze' is a vile debugging abomination, so we treat
5295 int fd = open(ZFS_DEV, O_RDWR);
5296 (void) strcpy((void *)buf, argv[2]);
5297 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5299 (void) fprintf(stderr, gettext("unrecognized "
5300 "command '%s'\n"), cmdname);
5307 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5308 * for the purposes of running ::findleaks.
5310 if (getenv("ZFS_ABORT") != NULL) {
5311 (void) printf("dumping core by request\n");