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"),
860 /* Compatiblity with FreeBSD 9.0 and 9.1: Use version 28 if unspecified */
861 if (nvlist_lookup_string(props,
862 zpool_prop_to_name(ZPOOL_PROP_VERSION),
864 if (add_prop_list(zpool_prop_to_name(
865 ZPOOL_PROP_VERSION), "28", &props, B_TRUE))
867 enable_all_pool_feat = B_FALSE;
869 #endif /* __FreeBSD__ */
874 /* get pool name and check number of arguments */
876 (void) fprintf(stderr, gettext("missing pool name argument\n"));
880 (void) fprintf(stderr, gettext("missing vdev specification\n"));
887 * As a special case, check for use of '/' in the name, and direct the
888 * user to use 'zfs create' instead.
890 if (strchr(poolname, '/') != NULL) {
891 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
892 "character '/' in pool name\n"), poolname);
893 (void) fprintf(stderr, gettext("use 'zfs create' to "
894 "create a dataset\n"));
898 /* pass off to get_vdev_spec for bulk processing */
899 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
904 /* make_root_vdev() allows 0 toplevel children if there are spares */
905 if (!zfs_allocatable_devs(nvroot)) {
906 (void) fprintf(stderr, gettext("invalid vdev "
907 "specification: at least one toplevel vdev must be "
912 if (altroot != NULL && altroot[0] != '/') {
913 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
914 "must be an absolute path\n"), altroot);
919 * Check the validity of the mountpoint and direct the user to use the
920 * '-m' mountpoint option if it looks like its in use.
922 if (mountpoint == NULL ||
923 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
924 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
925 char buf[MAXPATHLEN];
928 if (mountpoint && mountpoint[0] != '/') {
929 (void) fprintf(stderr, gettext("invalid mountpoint "
930 "'%s': must be an absolute path, 'legacy', or "
931 "'none'\n"), mountpoint);
935 if (mountpoint == NULL) {
937 (void) snprintf(buf, sizeof (buf), "%s/%s",
940 (void) snprintf(buf, sizeof (buf), "/%s",
944 (void) snprintf(buf, sizeof (buf), "%s%s",
945 altroot, mountpoint);
947 (void) snprintf(buf, sizeof (buf), "%s",
951 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
952 (void) fprintf(stderr, gettext("mountpoint '%s' : "
953 "%s\n"), buf, strerror(errno));
954 (void) fprintf(stderr, gettext("use '-m' "
955 "option to provide a different default\n"));
960 while (count < 3 && readdir(dirp) != NULL)
962 (void) closedir(dirp);
965 (void) fprintf(stderr, gettext("mountpoint "
966 "'%s' exists and is not empty\n"), buf);
967 (void) fprintf(stderr, gettext("use '-m' "
968 "option to provide a "
969 "different default\n"));
977 * For a dry run invocation, print out a basic message and run
978 * through all the vdevs in the list and print out in an
979 * appropriate hierarchy.
981 (void) printf(gettext("would create '%s' with the "
982 "following layout:\n\n"), poolname);
984 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
985 if (num_logs(nvroot) > 0)
986 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
991 * Hand off to libzfs.
993 if (enable_all_pool_feat) {
995 for (i = 0; i < SPA_FEATURES; i++) {
996 char propname[MAXPATHLEN];
997 zfeature_info_t *feat = &spa_feature_table[i];
999 (void) snprintf(propname, sizeof (propname),
1000 "feature@%s", feat->fi_uname);
1003 * Skip feature if user specified it manually
1004 * on the command line.
1006 if (nvlist_exists(props, propname))
1009 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
1010 &props, B_TRUE) != 0)
1014 if (zpool_create(g_zfs, poolname,
1015 nvroot, props, fsprops) == 0) {
1016 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1017 ZFS_TYPE_FILESYSTEM);
1019 if (mountpoint != NULL)
1020 verify(zfs_prop_set(pool,
1022 ZFS_PROP_MOUNTPOINT),
1024 if (zfs_mount(pool, NULL, 0) == 0)
1025 ret = zfs_shareall(pool);
1028 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1029 (void) fprintf(stderr, gettext("pool name may have "
1035 nvlist_free(nvroot);
1036 nvlist_free(fsprops);
1040 nvlist_free(fsprops);
1047 * zpool destroy <pool>
1049 * -f Forcefully unmount any datasets
1051 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1054 zpool_do_destroy(int argc, char **argv)
1056 boolean_t force = B_FALSE;
1059 zpool_handle_t *zhp;
1063 while ((c = getopt(argc, argv, "f")) != -1) {
1069 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1078 /* check arguments */
1080 (void) fprintf(stderr, gettext("missing pool argument\n"));
1084 (void) fprintf(stderr, gettext("too many arguments\n"));
1090 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1092 * As a special case, check for use of '/' in the name, and
1093 * direct the user to use 'zfs destroy' instead.
1095 if (strchr(pool, '/') != NULL)
1096 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1097 "destroy a dataset\n"));
1101 if (zpool_disable_datasets(zhp, force) != 0) {
1102 (void) fprintf(stderr, gettext("could not destroy '%s': "
1103 "could not unmount datasets\n"), zpool_get_name(zhp));
1107 ret = (zpool_destroy(zhp) != 0);
1115 * zpool export [-f] <pool> ...
1117 * -f Forcefully unmount datasets
1119 * Export the given pools. By default, the command will attempt to cleanly
1120 * unmount any active datasets within the pool. If the '-f' flag is specified,
1121 * then the datasets will be forcefully unmounted.
1124 zpool_do_export(int argc, char **argv)
1126 boolean_t force = B_FALSE;
1127 boolean_t hardforce = B_FALSE;
1129 zpool_handle_t *zhp;
1134 while ((c = getopt(argc, argv, "fF")) != -1) {
1143 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1152 /* check arguments */
1154 (void) fprintf(stderr, gettext("missing pool argument\n"));
1159 for (i = 0; i < argc; i++) {
1160 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1165 if (zpool_disable_datasets(zhp, force) != 0) {
1172 if (zpool_export_force(zhp) != 0)
1174 } else if (zpool_export(zhp, force) != 0) {
1185 * Given a vdev configuration, determine the maximum width needed for the device
1189 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1191 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1196 if (strlen(name) + depth > max)
1197 max = strlen(name) + depth;
1201 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1202 &child, &children) == 0) {
1203 for (c = 0; c < children; c++)
1204 if ((ret = max_width(zhp, child[c], depth + 2,
1209 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1210 &child, &children) == 0) {
1211 for (c = 0; c < children; c++)
1212 if ((ret = max_width(zhp, child[c], depth + 2,
1217 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1218 &child, &children) == 0) {
1219 for (c = 0; c < children; c++)
1220 if ((ret = max_width(zhp, child[c], depth + 2,
1229 typedef struct spare_cbdata {
1231 zpool_handle_t *cb_zhp;
1235 find_vdev(nvlist_t *nv, uint64_t search)
1241 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1245 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1246 &child, &children) == 0) {
1247 for (c = 0; c < children; c++)
1248 if (find_vdev(child[c], search))
1256 find_spare(zpool_handle_t *zhp, void *data)
1258 spare_cbdata_t *cbp = data;
1259 nvlist_t *config, *nvroot;
1261 config = zpool_get_config(zhp, NULL);
1262 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1265 if (find_vdev(nvroot, cbp->cb_guid)) {
1275 * Print out configuration state as requested by status_callback.
1278 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1279 int namewidth, int depth, boolean_t isspare)
1283 pool_scan_stat_t *ps = NULL;
1285 char rbuf[6], wbuf[6], cbuf[6];
1287 uint64_t notpresent;
1291 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1292 &child, &children) != 0)
1295 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1296 (uint64_t **)&vs, &c) == 0);
1298 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1301 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1304 if (vs->vs_aux == VDEV_AUX_SPARED)
1306 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1310 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1314 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1315 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1316 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1317 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1320 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1321 ¬present) == 0 ||
1322 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1324 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1325 (void) printf(" was %s", path);
1326 } else if (vs->vs_aux != 0) {
1329 switch (vs->vs_aux) {
1330 case VDEV_AUX_OPEN_FAILED:
1331 (void) printf(gettext("cannot open"));
1334 case VDEV_AUX_BAD_GUID_SUM:
1335 (void) printf(gettext("missing device"));
1338 case VDEV_AUX_NO_REPLICAS:
1339 (void) printf(gettext("insufficient replicas"));
1342 case VDEV_AUX_VERSION_NEWER:
1343 (void) printf(gettext("newer version"));
1346 case VDEV_AUX_UNSUP_FEAT:
1347 (void) printf(gettext("unsupported feature(s)"));
1350 case VDEV_AUX_SPARED:
1351 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1353 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1354 if (strcmp(zpool_get_name(cb.cb_zhp),
1355 zpool_get_name(zhp)) == 0)
1356 (void) printf(gettext("currently in "
1359 (void) printf(gettext("in use by "
1361 zpool_get_name(cb.cb_zhp));
1362 zpool_close(cb.cb_zhp);
1364 (void) printf(gettext("currently in use"));
1368 case VDEV_AUX_ERR_EXCEEDED:
1369 (void) printf(gettext("too many errors"));
1372 case VDEV_AUX_IO_FAILURE:
1373 (void) printf(gettext("experienced I/O failures"));
1376 case VDEV_AUX_BAD_LOG:
1377 (void) printf(gettext("bad intent log"));
1380 case VDEV_AUX_EXTERNAL:
1381 (void) printf(gettext("external device fault"));
1384 case VDEV_AUX_SPLIT_POOL:
1385 (void) printf(gettext("split into new pool"));
1389 (void) printf(gettext("corrupted data"));
1394 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1395 (uint64_t **)&ps, &c);
1397 if (ps && ps->pss_state == DSS_SCANNING &&
1398 vs->vs_scan_processed != 0 && children == 0) {
1399 (void) printf(gettext(" (%s)"),
1400 (ps->pss_func == POOL_SCAN_RESILVER) ?
1401 "resilvering" : "repairing");
1404 (void) printf("\n");
1406 for (c = 0; c < children; c++) {
1407 uint64_t islog = B_FALSE, ishole = B_FALSE;
1409 /* Don't print logs or holes here */
1410 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1412 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1414 if (islog || ishole)
1416 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1417 print_status_config(zhp, vname, child[c],
1418 namewidth, depth + 2, isspare);
1425 * Print the configuration of an exported pool. Iterate over all vdevs in the
1426 * pool, printing out the name and status for each one.
1429 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1436 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1437 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1438 strcmp(type, VDEV_TYPE_HOLE) == 0)
1441 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1442 (uint64_t **)&vs, &c) == 0);
1444 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1445 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1447 if (vs->vs_aux != 0) {
1450 switch (vs->vs_aux) {
1451 case VDEV_AUX_OPEN_FAILED:
1452 (void) printf(gettext("cannot open"));
1455 case VDEV_AUX_BAD_GUID_SUM:
1456 (void) printf(gettext("missing device"));
1459 case VDEV_AUX_NO_REPLICAS:
1460 (void) printf(gettext("insufficient replicas"));
1463 case VDEV_AUX_VERSION_NEWER:
1464 (void) printf(gettext("newer version"));
1467 case VDEV_AUX_UNSUP_FEAT:
1468 (void) printf(gettext("unsupported feature(s)"));
1471 case VDEV_AUX_ERR_EXCEEDED:
1472 (void) printf(gettext("too many errors"));
1476 (void) printf(gettext("corrupted data"));
1480 (void) printf("\n");
1482 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1483 &child, &children) != 0)
1486 for (c = 0; c < children; c++) {
1487 uint64_t is_log = B_FALSE;
1489 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1494 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1495 print_import_config(vname, child[c], namewidth, depth + 2);
1499 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1500 &child, &children) == 0) {
1501 (void) printf(gettext("\tcache\n"));
1502 for (c = 0; c < children; c++) {
1503 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1504 (void) printf("\t %s\n", vname);
1509 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1510 &child, &children) == 0) {
1511 (void) printf(gettext("\tspares\n"));
1512 for (c = 0; c < children; c++) {
1513 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1514 (void) printf("\t %s\n", vname);
1522 * Logs are recorded as top level vdevs in the main pool child array
1523 * but with "is_log" set to 1. We use either print_status_config() or
1524 * print_import_config() to print the top level logs then any log
1525 * children (eg mirrored slogs) are printed recursively - which
1526 * works because only the top level vdev is marked "is_log"
1529 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1534 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1538 (void) printf(gettext("\tlogs\n"));
1540 for (c = 0; c < children; c++) {
1541 uint64_t is_log = B_FALSE;
1544 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1548 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1550 print_status_config(zhp, name, child[c], namewidth,
1553 print_import_config(name, child[c], namewidth, 2);
1559 * Display the status for the given pool.
1562 show_import(nvlist_t *config)
1564 uint64_t pool_state;
1576 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1578 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1580 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1582 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1585 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1586 (uint64_t **)&vs, &vsc) == 0);
1587 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1589 reason = zpool_import_status(config, &msgid);
1591 (void) printf(gettext(" pool: %s\n"), name);
1592 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1593 (void) printf(gettext(" state: %s"), health);
1594 if (pool_state == POOL_STATE_DESTROYED)
1595 (void) printf(gettext(" (DESTROYED)"));
1596 (void) printf("\n");
1599 case ZPOOL_STATUS_MISSING_DEV_R:
1600 case ZPOOL_STATUS_MISSING_DEV_NR:
1601 case ZPOOL_STATUS_BAD_GUID_SUM:
1602 (void) printf(gettext(" status: One or more devices are "
1603 "missing from the system.\n"));
1606 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1607 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1608 (void) printf(gettext(" status: One or more devices contains "
1609 "corrupted data.\n"));
1612 case ZPOOL_STATUS_CORRUPT_DATA:
1614 gettext(" status: The pool data is corrupted.\n"));
1617 case ZPOOL_STATUS_OFFLINE_DEV:
1618 (void) printf(gettext(" status: One or more devices "
1619 "are offlined.\n"));
1622 case ZPOOL_STATUS_CORRUPT_POOL:
1623 (void) printf(gettext(" status: The pool metadata is "
1627 case ZPOOL_STATUS_VERSION_OLDER:
1628 (void) printf(gettext(" status: The pool is formatted using a "
1629 "legacy on-disk version.\n"));
1632 case ZPOOL_STATUS_VERSION_NEWER:
1633 (void) printf(gettext(" status: The pool is formatted using an "
1634 "incompatible version.\n"));
1637 case ZPOOL_STATUS_FEAT_DISABLED:
1638 (void) printf(gettext(" status: Some supported features are "
1639 "not enabled on the pool.\n"));
1642 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1643 (void) printf(gettext("status: The pool uses the following "
1644 "feature(s) not supported on this sytem:\n"));
1645 zpool_print_unsup_feat(config);
1648 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1649 (void) printf(gettext("status: The pool can only be accessed "
1650 "in read-only mode on this system. It\n\tcannot be "
1651 "accessed in read-write mode because it uses the "
1652 "following\n\tfeature(s) not supported on this system:\n"));
1653 zpool_print_unsup_feat(config);
1656 case ZPOOL_STATUS_HOSTID_MISMATCH:
1657 (void) printf(gettext(" status: The pool was last accessed by "
1658 "another system.\n"));
1661 case ZPOOL_STATUS_FAULTED_DEV_R:
1662 case ZPOOL_STATUS_FAULTED_DEV_NR:
1663 (void) printf(gettext(" status: One or more devices are "
1667 case ZPOOL_STATUS_BAD_LOG:
1668 (void) printf(gettext(" status: An intent log record cannot be "
1672 case ZPOOL_STATUS_RESILVERING:
1673 (void) printf(gettext(" status: One or more devices were being "
1679 * No other status can be seen when importing pools.
1681 assert(reason == ZPOOL_STATUS_OK);
1685 * Print out an action according to the overall state of the pool.
1687 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1688 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1689 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1690 (void) printf(gettext(" action: The pool can be "
1691 "imported using its name or numeric identifier, "
1692 "though\n\tsome features will not be available "
1693 "without an explicit 'zpool upgrade'.\n"));
1694 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1695 (void) printf(gettext(" action: The pool can be "
1696 "imported using its name or numeric "
1697 "identifier and\n\tthe '-f' flag.\n"));
1699 (void) printf(gettext(" action: The pool can be "
1700 "imported using its name or numeric "
1703 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1704 (void) printf(gettext(" action: The pool can be imported "
1705 "despite missing or damaged devices. The\n\tfault "
1706 "tolerance of the pool may be compromised if imported.\n"));
1709 case ZPOOL_STATUS_VERSION_NEWER:
1710 (void) printf(gettext(" action: The pool cannot be "
1711 "imported. Access the pool on a system running "
1712 "newer\n\tsoftware, or recreate the pool from "
1715 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1716 (void) printf(gettext("action: The pool cannot be "
1717 "imported. Access the pool on a system that "
1718 "supports\n\tthe required feature(s), or recreate "
1719 "the pool from backup.\n"));
1721 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1722 (void) printf(gettext("action: The pool cannot be "
1723 "imported in read-write mode. Import the pool "
1725 "\t\"-o readonly=on\", access the pool on a system "
1726 "that supports the\n\trequired feature(s), or "
1727 "recreate the pool from backup.\n"));
1729 case ZPOOL_STATUS_MISSING_DEV_R:
1730 case ZPOOL_STATUS_MISSING_DEV_NR:
1731 case ZPOOL_STATUS_BAD_GUID_SUM:
1732 (void) printf(gettext(" action: The pool cannot be "
1733 "imported. Attach the missing\n\tdevices and try "
1737 (void) printf(gettext(" action: The pool cannot be "
1738 "imported due to damaged devices or data.\n"));
1742 /* Print the comment attached to the pool. */
1743 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1744 (void) printf(gettext("comment: %s\n"), comment);
1747 * If the state is "closed" or "can't open", and the aux state
1748 * is "corrupt data":
1750 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1751 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1752 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1753 if (pool_state == POOL_STATE_DESTROYED)
1754 (void) printf(gettext("\tThe pool was destroyed, "
1755 "but can be imported using the '-Df' flags.\n"));
1756 else if (pool_state != POOL_STATE_EXPORTED)
1757 (void) printf(gettext("\tThe pool may be active on "
1758 "another system, but can be imported using\n\t"
1759 "the '-f' flag.\n"));
1763 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1766 (void) printf(gettext(" config:\n\n"));
1768 namewidth = max_width(NULL, nvroot, 0, 0);
1772 print_import_config(name, nvroot, namewidth, 0);
1773 if (num_logs(nvroot) > 0)
1774 print_logs(NULL, nvroot, namewidth, B_FALSE);
1776 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1777 (void) printf(gettext("\n\tAdditional devices are known to "
1778 "be part of this pool, though their\n\texact "
1779 "configuration cannot be determined.\n"));
1784 * Perform the import for the given configuration. This passes the heavy
1785 * lifting off to zpool_import_props(), and then mounts the datasets contained
1789 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1790 nvlist_t *props, int flags)
1792 zpool_handle_t *zhp;
1797 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1800 verify(nvlist_lookup_uint64(config,
1801 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1802 verify(nvlist_lookup_uint64(config,
1803 ZPOOL_CONFIG_VERSION, &version) == 0);
1804 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1805 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1806 "is formatted using an unsupported ZFS version\n"), name);
1808 } else if (state != POOL_STATE_EXPORTED &&
1809 !(flags & ZFS_IMPORT_ANY_HOST)) {
1812 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1814 if ((unsigned long)hostid != gethostid()) {
1819 verify(nvlist_lookup_string(config,
1820 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1821 verify(nvlist_lookup_uint64(config,
1822 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1824 (void) fprintf(stderr, gettext("cannot import "
1825 "'%s': pool may be in use from other "
1826 "system, it was last accessed by %s "
1827 "(hostid: 0x%lx) on %s"), name, hostname,
1828 (unsigned long)hostid,
1829 asctime(localtime(&t)));
1830 (void) fprintf(stderr, gettext("use '-f' to "
1831 "import anyway\n"));
1835 (void) fprintf(stderr, gettext("cannot import '%s': "
1836 "pool may be in use from other system\n"), name);
1837 (void) fprintf(stderr, gettext("use '-f' to import "
1843 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1846 if (newname != NULL)
1847 name = (char *)newname;
1849 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1852 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1853 !(flags & ZFS_IMPORT_ONLY) &&
1854 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1864 * zpool import [-d dir] [-D]
1865 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1866 * [-d dir | -c cachefile] [-f] -a
1867 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1868 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1870 * -c Read pool information from a cachefile instead of searching
1873 * -d Scan in a specific directory, other than /dev/dsk. More than
1874 * one directory can be specified using multiple '-d' options.
1876 * -D Scan for previously destroyed pools or import all or only
1877 * specified destroyed pools.
1879 * -R Temporarily import the pool, with all mountpoints relative to
1880 * the given root. The pool will remain exported when the machine
1883 * -V Import even in the presence of faulted vdevs. This is an
1884 * intentionally undocumented option for testing purposes, and
1885 * treats the pool configuration as complete, leaving any bad
1886 * vdevs in the FAULTED state. In other words, it does verbatim
1889 * -f Force import, even if it appears that the pool is active.
1891 * -F Attempt rewind if necessary.
1893 * -n See if rewind would work, but don't actually rewind.
1895 * -N Import the pool but don't mount datasets.
1897 * -T Specify a starting txg to use for import. This option is
1898 * intentionally undocumented option for testing purposes.
1900 * -a Import all pools found.
1902 * -o Set property=value and/or temporary mount options (without '=').
1904 * The import command scans for pools to import, and import pools based on pool
1905 * name and GUID. The pool can also be renamed as part of the import process.
1908 zpool_do_import(int argc, char **argv)
1910 char **searchdirs = NULL;
1914 nvlist_t *pools = NULL;
1915 boolean_t do_all = B_FALSE;
1916 boolean_t do_destroyed = B_FALSE;
1917 char *mntopts = NULL;
1920 uint64_t searchguid = 0;
1921 char *searchname = NULL;
1923 nvlist_t *found_config;
1924 nvlist_t *policy = NULL;
1925 nvlist_t *props = NULL;
1927 int flags = ZFS_IMPORT_NORMAL;
1928 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1929 boolean_t dryrun = B_FALSE;
1930 boolean_t do_rewind = B_FALSE;
1931 boolean_t xtreme_rewind = B_FALSE;
1932 uint64_t pool_state, txg = -1ULL;
1933 char *cachefile = NULL;
1934 importargs_t idata = { 0 };
1938 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1947 if (searchdirs == NULL) {
1948 searchdirs = safe_malloc(sizeof (char *));
1950 char **tmp = safe_malloc((nsearch + 1) *
1952 bcopy(searchdirs, tmp, nsearch *
1957 searchdirs[nsearch++] = optarg;
1960 do_destroyed = B_TRUE;
1963 flags |= ZFS_IMPORT_ANY_HOST;
1969 flags |= ZFS_IMPORT_MISSING_LOG;
1975 flags |= ZFS_IMPORT_ONLY;
1978 if ((propval = strchr(optarg, '=')) != NULL) {
1981 if (add_prop_list(optarg, propval,
1989 if (add_prop_list(zpool_prop_to_name(
1990 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1992 if (nvlist_lookup_string(props,
1993 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1996 if (add_prop_list(zpool_prop_to_name(
1997 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2002 txg = strtoull(optarg, &endptr, 10);
2003 if (errno != 0 || *endptr != '\0') {
2004 (void) fprintf(stderr,
2005 gettext("invalid txg value\n"));
2008 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2011 flags |= ZFS_IMPORT_VERBATIM;
2014 xtreme_rewind = B_TRUE;
2017 (void) fprintf(stderr, gettext("missing argument for "
2018 "'%c' option\n"), optopt);
2022 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2031 if (cachefile && nsearch != 0) {
2032 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2036 if ((dryrun || xtreme_rewind) && !do_rewind) {
2037 (void) fprintf(stderr,
2038 gettext("-n or -X only meaningful with -F\n"));
2042 rewind_policy = ZPOOL_TRY_REWIND;
2044 rewind_policy = ZPOOL_DO_REWIND;
2046 rewind_policy |= ZPOOL_EXTREME_REWIND;
2048 /* In the future, we can capture further policy and include it here */
2049 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2050 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2051 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2054 if (searchdirs == NULL) {
2055 searchdirs = safe_malloc(sizeof (char *));
2056 searchdirs[0] = "/dev";
2060 /* check argument count */
2063 (void) fprintf(stderr, gettext("too many arguments\n"));
2068 (void) fprintf(stderr, gettext("too many arguments\n"));
2073 * Check for the SYS_CONFIG privilege. We do this explicitly
2074 * here because otherwise any attempt to discover pools will
2077 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2078 (void) fprintf(stderr, gettext("cannot "
2079 "discover pools: permission denied\n"));
2081 nvlist_free(policy);
2087 * Depending on the arguments given, we do one of the following:
2089 * <none> Iterate through all pools and display information about
2092 * -a Iterate through all pools and try to import each one.
2094 * <id> Find the pool that corresponds to the given GUID/pool
2095 * name and import that one.
2097 * -D Above options applies only to destroyed pools.
2103 searchguid = strtoull(argv[0], &endptr, 10);
2104 if (errno != 0 || *endptr != '\0')
2105 searchname = argv[0];
2106 found_config = NULL;
2109 * User specified a name or guid. Ensure it's unique.
2111 idata.unique = B_TRUE;
2115 idata.path = searchdirs;
2116 idata.paths = nsearch;
2117 idata.poolname = searchname;
2118 idata.guid = searchguid;
2119 idata.cachefile = cachefile;
2121 pools = zpool_search_import(g_zfs, &idata);
2123 if (pools != NULL && idata.exists &&
2124 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2125 (void) fprintf(stderr, gettext("cannot import '%s': "
2126 "a pool with that name already exists\n"),
2128 (void) fprintf(stderr, gettext("use the form '%s "
2129 "<pool | id> <newpool>' to give it a new name\n"),
2132 } else if (pools == NULL && idata.exists) {
2133 (void) fprintf(stderr, gettext("cannot import '%s': "
2134 "a pool with that name is already created/imported,\n"),
2136 (void) fprintf(stderr, gettext("and no additional pools "
2137 "with that name were found\n"));
2139 } else if (pools == NULL) {
2141 (void) fprintf(stderr, gettext("cannot import '%s': "
2142 "no such pool available\n"), argv[0]);
2149 nvlist_free(policy);
2154 * At this point we have a list of import candidate configs. Even if
2155 * we were searching by pool name or guid, we still need to
2156 * post-process the list to deal with pool state and possible
2162 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2164 verify(nvpair_value_nvlist(elem, &config) == 0);
2166 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2168 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2170 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2173 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2180 (void) printf("\n");
2183 err |= do_import(config, NULL, mntopts,
2186 show_import(config);
2188 } else if (searchname != NULL) {
2192 * We are searching for a pool based on name.
2194 verify(nvlist_lookup_string(config,
2195 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2197 if (strcmp(name, searchname) == 0) {
2198 if (found_config != NULL) {
2199 (void) fprintf(stderr, gettext(
2200 "cannot import '%s': more than "
2201 "one matching pool\n"), searchname);
2202 (void) fprintf(stderr, gettext(
2203 "import by numeric ID instead\n"));
2206 found_config = config;
2212 * Search for a pool by guid.
2214 verify(nvlist_lookup_uint64(config,
2215 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2217 if (guid == searchguid)
2218 found_config = config;
2223 * If we were searching for a specific pool, verify that we found a
2224 * pool, and then do the import.
2226 if (argc != 0 && err == 0) {
2227 if (found_config == NULL) {
2228 (void) fprintf(stderr, gettext("cannot import '%s': "
2229 "no such pool available\n"), argv[0]);
2232 err |= do_import(found_config, argc == 1 ? NULL :
2233 argv[1], mntopts, props, flags);
2238 * If we were just looking for pools, report an error if none were
2241 if (argc == 0 && first)
2242 (void) fprintf(stderr,
2243 gettext("no pools available to import\n"));
2248 nvlist_free(policy);
2251 return (err ? 1 : 0);
2254 typedef struct iostat_cbdata {
2255 boolean_t cb_verbose;
2258 zpool_list_t *cb_list;
2262 print_iostat_separator(iostat_cbdata_t *cb)
2266 for (i = 0; i < cb->cb_namewidth; i++)
2268 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2272 print_iostat_header(iostat_cbdata_t *cb)
2274 (void) printf("%*s capacity operations bandwidth\n",
2275 cb->cb_namewidth, "");
2276 (void) printf("%-*s alloc free read write read write\n",
2277 cb->cb_namewidth, "pool");
2278 print_iostat_separator(cb);
2282 * Display a single statistic.
2285 print_one_stat(uint64_t value)
2289 zfs_nicenum(value, buf, sizeof (buf));
2290 (void) printf(" %5s", buf);
2294 * Print out all the statistics for the given vdev. This can either be the
2295 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2296 * is a verbose output, and we don't want to display the toplevel pool stats.
2299 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2300 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2302 nvlist_t **oldchild, **newchild;
2304 vdev_stat_t *oldvs, *newvs;
2305 vdev_stat_t zerovs = { 0 };
2310 if (oldnv != NULL) {
2311 verify(nvlist_lookup_uint64_array(oldnv,
2312 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2317 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2318 (uint64_t **)&newvs, &c) == 0);
2320 if (strlen(name) + depth > cb->cb_namewidth)
2321 (void) printf("%*s%s", depth, "", name);
2323 (void) printf("%*s%s%*s", depth, "", name,
2324 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2326 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2331 scale = (double)NANOSEC / tdelta;
2333 /* only toplevel vdevs have capacity stats */
2334 if (newvs->vs_space == 0) {
2335 (void) printf(" - -");
2337 print_one_stat(newvs->vs_alloc);
2338 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2341 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2342 oldvs->vs_ops[ZIO_TYPE_READ])));
2344 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2345 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2347 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2348 oldvs->vs_bytes[ZIO_TYPE_READ])));
2350 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2351 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2353 (void) printf("\n");
2355 if (!cb->cb_verbose)
2358 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2359 &newchild, &children) != 0)
2362 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2363 &oldchild, &c) != 0)
2366 for (c = 0; c < children; c++) {
2367 uint64_t ishole = B_FALSE, islog = B_FALSE;
2369 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2372 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2375 if (ishole || islog)
2378 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2379 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2380 newchild[c], cb, depth + 2);
2385 * Log device section
2388 if (num_logs(newnv) > 0) {
2389 (void) printf("%-*s - - - - - "
2390 "-\n", cb->cb_namewidth, "logs");
2392 for (c = 0; c < children; c++) {
2393 uint64_t islog = B_FALSE;
2394 (void) nvlist_lookup_uint64(newchild[c],
2395 ZPOOL_CONFIG_IS_LOG, &islog);
2398 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2400 print_vdev_stats(zhp, vname, oldnv ?
2401 oldchild[c] : NULL, newchild[c],
2410 * Include level 2 ARC devices in iostat output
2412 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2413 &newchild, &children) != 0)
2416 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2417 &oldchild, &c) != 0)
2421 (void) printf("%-*s - - - - - "
2422 "-\n", cb->cb_namewidth, "cache");
2423 for (c = 0; c < children; c++) {
2424 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2426 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2427 newchild[c], cb, depth + 2);
2434 refresh_iostat(zpool_handle_t *zhp, void *data)
2436 iostat_cbdata_t *cb = data;
2440 * If the pool has disappeared, remove it from the list and continue.
2442 if (zpool_refresh_stats(zhp, &missing) != 0)
2446 pool_list_remove(cb->cb_list, zhp);
2452 * Callback to print out the iostats for the given pool.
2455 print_iostat(zpool_handle_t *zhp, void *data)
2457 iostat_cbdata_t *cb = data;
2458 nvlist_t *oldconfig, *newconfig;
2459 nvlist_t *oldnvroot, *newnvroot;
2461 newconfig = zpool_get_config(zhp, &oldconfig);
2463 if (cb->cb_iteration == 1)
2466 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2469 if (oldconfig == NULL)
2472 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2476 * Print out the statistics for the pool.
2478 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2481 print_iostat_separator(cb);
2487 get_namewidth(zpool_handle_t *zhp, void *data)
2489 iostat_cbdata_t *cb = data;
2490 nvlist_t *config, *nvroot;
2492 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2493 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2495 if (!cb->cb_verbose)
2496 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2498 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2503 * The width must fall into the range [10,38]. The upper limit is the
2504 * maximum we can have and still fit in 80 columns.
2506 if (cb->cb_namewidth < 10)
2507 cb->cb_namewidth = 10;
2508 if (cb->cb_namewidth > 38)
2509 cb->cb_namewidth = 38;
2515 * Parse the input string, get the 'interval' and 'count' value if there is one.
2518 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2521 unsigned long interval = 0, count = 0;
2522 int argc = *argcp, errno;
2525 * Determine if the last argument is an integer or a pool name
2527 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2531 interval = strtoul(argv[argc - 1], &end, 10);
2533 if (*end == '\0' && errno == 0) {
2534 if (interval == 0) {
2535 (void) fprintf(stderr, gettext("interval "
2536 "cannot be zero\n"));
2540 * Ignore the last parameter
2545 * If this is not a valid number, just plow on. The
2546 * user will get a more informative error message later
2554 * If the last argument is also an integer, then we have both a count
2557 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2562 interval = strtoul(argv[argc - 1], &end, 10);
2564 if (*end == '\0' && errno == 0) {
2565 if (interval == 0) {
2566 (void) fprintf(stderr, gettext("interval "
2567 "cannot be zero\n"));
2572 * Ignore the last parameter
2586 get_timestamp_arg(char c)
2589 timestamp_fmt = UDATE;
2591 timestamp_fmt = DDATE;
2597 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2599 * -v Display statistics for individual vdevs
2600 * -T Display a timestamp in date(1) or Unix format
2602 * This command can be tricky because we want to be able to deal with pool
2603 * creation/destruction as well as vdev configuration changes. The bulk of this
2604 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2605 * on pool_list_update() to detect the addition of new pools. Configuration
2606 * changes are all handled within libzfs.
2609 zpool_do_iostat(int argc, char **argv)
2614 unsigned long interval = 0, count = 0;
2616 boolean_t verbose = B_FALSE;
2620 while ((c = getopt(argc, argv, "T:v")) != -1) {
2623 get_timestamp_arg(*optarg);
2629 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2638 get_interval_count(&argc, argv, &interval, &count);
2641 * Construct the list of all interesting pools.
2644 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2647 if (pool_list_count(list) == 0 && argc != 0) {
2648 pool_list_free(list);
2652 if (pool_list_count(list) == 0 && interval == 0) {
2653 pool_list_free(list);
2654 (void) fprintf(stderr, gettext("no pools available\n"));
2659 * Enter the main iostat loop.
2662 cb.cb_verbose = verbose;
2663 cb.cb_iteration = 0;
2664 cb.cb_namewidth = 0;
2667 pool_list_update(list);
2669 if ((npools = pool_list_count(list)) == 0)
2673 * Refresh all statistics. This is done as an explicit step
2674 * before calculating the maximum name width, so that any
2675 * configuration changes are properly accounted for.
2677 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2680 * Iterate over all pools to determine the maximum width
2681 * for the pool / device name column across all pools.
2683 cb.cb_namewidth = 0;
2684 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2686 if (timestamp_fmt != NODATE)
2687 print_timestamp(timestamp_fmt);
2690 * If it's the first time, or verbose mode, print the header.
2692 if (++cb.cb_iteration == 1 || verbose)
2693 print_iostat_header(&cb);
2695 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2698 * If there's more than one pool, and we're not in verbose mode
2699 * (which prints a separator for us), then print a separator.
2701 if (npools > 1 && !verbose)
2702 print_iostat_separator(&cb);
2705 (void) printf("\n");
2708 * Flush the output so that redirection to a file isn't buffered
2711 (void) fflush(stdout);
2716 if (count != 0 && --count == 0)
2719 (void) sleep(interval);
2722 pool_list_free(list);
2727 typedef struct list_cbdata {
2728 boolean_t cb_verbose;
2730 boolean_t cb_scripted;
2731 zprop_list_t *cb_proplist;
2735 * Given a list of columns to display, output appropriate headers for each one.
2738 print_header(list_cbdata_t *cb)
2740 zprop_list_t *pl = cb->cb_proplist;
2741 char headerbuf[ZPOOL_MAXPROPLEN];
2743 boolean_t first = B_TRUE;
2744 boolean_t right_justify;
2747 for (; pl != NULL; pl = pl->pl_next) {
2748 width = pl->pl_width;
2749 if (first && cb->cb_verbose) {
2751 * Reset the width to accommodate the verbose listing
2754 width = cb->cb_namewidth;
2762 right_justify = B_FALSE;
2763 if (pl->pl_prop != ZPROP_INVAL) {
2764 header = zpool_prop_column_name(pl->pl_prop);
2765 right_justify = zpool_prop_align_right(pl->pl_prop);
2769 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2770 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2771 headerbuf[i] = '\0';
2775 if (pl->pl_next == NULL && !right_justify)
2776 (void) printf("%s", header);
2777 else if (right_justify)
2778 (void) printf("%*s", width, header);
2780 (void) printf("%-*s", width, header);
2784 (void) printf("\n");
2788 * Given a pool and a list of properties, print out all the properties according
2789 * to the described layout.
2792 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2794 zprop_list_t *pl = cb->cb_proplist;
2795 boolean_t first = B_TRUE;
2796 char property[ZPOOL_MAXPROPLEN];
2798 boolean_t right_justify;
2801 for (; pl != NULL; pl = pl->pl_next) {
2803 width = pl->pl_width;
2804 if (first && cb->cb_verbose) {
2806 * Reset the width to accommodate the verbose listing
2809 width = cb->cb_namewidth;
2813 if (cb->cb_scripted)
2814 (void) printf("\t");
2821 right_justify = B_FALSE;
2822 if (pl->pl_prop != ZPROP_INVAL) {
2823 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2824 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2826 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2827 sizeof (property), NULL) != 0)
2832 right_justify = zpool_prop_align_right(pl->pl_prop);
2833 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2834 zpool_prop_unsupported(pl->pl_user_prop)) &&
2835 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2836 sizeof (property)) == 0) {
2844 * If this is being called in scripted mode, or if this is the
2845 * last column and it is left-justified, don't include a width
2848 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2849 (void) printf("%s", propstr);
2850 else if (right_justify)
2851 (void) printf("%*s", width, propstr);
2853 (void) printf("%-*s", width, propstr);
2856 (void) printf("\n");
2860 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2864 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2866 zfs_nicenum(value, propval, sizeof (propval));
2868 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2869 (void) strlcpy(propval, "-", sizeof (propval));
2872 (void) printf("\t%s", propval);
2874 (void) printf(" %*s", width, propval);
2878 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2879 list_cbdata_t *cb, int depth)
2885 boolean_t scripted = cb->cb_scripted;
2887 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2888 (uint64_t **)&vs, &c) == 0);
2892 (void) printf("\t%s", name);
2893 else if (strlen(name) + depth > cb->cb_namewidth)
2894 (void) printf("%*s%s", depth, "", name);
2896 (void) printf("%*s%s%*s", depth, "", name,
2897 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2899 /* only toplevel vdevs have capacity stats */
2900 if (vs->vs_space == 0) {
2902 (void) printf("\t-\t-\t-");
2904 (void) printf(" - - -");
2906 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2908 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2910 print_one_column(ZPOOL_PROP_FREE,
2911 vs->vs_space - vs->vs_alloc, scripted);
2913 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2915 (void) printf("\n");
2918 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2919 &child, &children) != 0)
2922 for (c = 0; c < children; c++) {
2923 uint64_t ishole = B_FALSE;
2925 if (nvlist_lookup_uint64(child[c],
2926 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2929 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2930 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2935 * Include level 2 ARC devices in iostat output
2937 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2938 &child, &children) != 0)
2942 (void) printf("%-*s - - - - - "
2943 "-\n", cb->cb_namewidth, "cache");
2944 for (c = 0; c < children; c++) {
2945 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2947 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2955 * Generic callback function to list a pool.
2958 list_callback(zpool_handle_t *zhp, void *data)
2960 list_cbdata_t *cbp = data;
2964 config = zpool_get_config(zhp, NULL);
2966 print_pool(zhp, cbp);
2967 if (!cbp->cb_verbose)
2970 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2972 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2978 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2980 * -H Scripted mode. Don't display headers, and separate properties
2982 * -o List of properties to display. Defaults to
2983 * "name,size,allocated,free,capacity,health,altroot"
2984 * -T Display a timestamp in date(1) or Unix format
2986 * List all pools in the system, whether or not they're healthy. Output space
2987 * statistics for each one, as well as health status summary.
2990 zpool_do_list(int argc, char **argv)
2994 list_cbdata_t cb = { 0 };
2995 static char default_props[] =
2996 "name,size,allocated,free,capacity,dedupratio,"
2998 char *props = default_props;
2999 unsigned long interval = 0, count = 0;
3001 boolean_t first = B_TRUE;
3004 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3007 cb.cb_scripted = B_TRUE;
3013 get_timestamp_arg(*optarg);
3016 cb.cb_verbose = B_TRUE;
3019 (void) fprintf(stderr, gettext("missing argument for "
3020 "'%c' option\n"), optopt);
3024 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3033 get_interval_count(&argc, argv, &interval, &count);
3035 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3038 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3041 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3042 (void) printf(gettext("no pools available\n"));
3043 zprop_free_list(cb.cb_proplist);
3048 pool_list_update(list);
3050 if (pool_list_count(list) == 0)
3053 cb.cb_namewidth = 0;
3054 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3056 if (timestamp_fmt != NODATE)
3057 print_timestamp(timestamp_fmt);
3059 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3063 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3068 if (count != 0 && --count == 0)
3071 (void) sleep(interval);
3074 zprop_free_list(cb.cb_proplist);
3079 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
3086 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3087 &child, &children) != 0) {
3088 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3089 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3090 name += sizeof(_PATH_DEV) - 1;
3091 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3092 path += sizeof(_PATH_DEV) - 1;
3093 if (strcmp(name, path) == 0)
3098 for (c = 0; c < children; c++)
3099 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
3106 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3108 boolean_t force = B_FALSE;
3111 char *poolname, *old_disk, *new_disk;
3112 zpool_handle_t *zhp;
3116 while ((c = getopt(argc, argv, "f")) != -1) {
3122 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3131 /* get pool name and check number of arguments */
3133 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3140 (void) fprintf(stderr,
3141 gettext("missing <device> specification\n"));
3149 (void) fprintf(stderr,
3150 gettext("missing <new_device> specification\n"));
3153 new_disk = old_disk;
3163 (void) fprintf(stderr, gettext("too many arguments\n"));
3167 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3170 if (zpool_get_config(zhp, NULL) == NULL) {
3171 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3177 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3179 if (nvroot == NULL) {
3184 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3186 nvlist_free(nvroot);
3193 * zpool replace [-f] <pool> <device> <new_device>
3195 * -f Force attach, even if <new_device> appears to be in use.
3197 * Replace <device> with <new_device>.
3201 zpool_do_replace(int argc, char **argv)
3203 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3207 * zpool attach [-f] <pool> <device> <new_device>
3209 * -f Force attach, even if <new_device> appears to be in use.
3211 * Attach <new_device> to the mirror containing <device>. If <device> is not
3212 * part of a mirror, then <device> will be transformed into a mirror of
3213 * <device> and <new_device>. In either case, <new_device> will begin life
3214 * with a DTL of [0, now], and will immediately begin to resilver itself.
3217 zpool_do_attach(int argc, char **argv)
3219 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3223 * zpool detach [-f] <pool> <device>
3225 * -f Force detach of <device>, even if DTLs argue against it
3226 * (not supported yet)
3228 * Detach a device from a mirror. The operation will be refused if <device>
3229 * is the last device in the mirror, or if the DTLs indicate that this device
3230 * has the only valid copy of some data.
3234 zpool_do_detach(int argc, char **argv)
3237 char *poolname, *path;
3238 zpool_handle_t *zhp;
3242 while ((c = getopt(argc, argv, "f")) != -1) {
3246 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3255 /* get pool name and check number of arguments */
3257 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3262 (void) fprintf(stderr,
3263 gettext("missing <device> specification\n"));
3270 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3273 ret = zpool_vdev_detach(zhp, path);
3281 * zpool split [-n] [-o prop=val] ...
3283 * [-R altroot] <pool> <newpool> [<device> ...]
3285 * -n Do not split the pool, but display the resulting layout if
3286 * it were to be split.
3287 * -o Set property=value, or set mount options.
3288 * -R Mount the split-off pool under an alternate root.
3290 * Splits the named pool and gives it the new pool name. Devices to be split
3291 * off may be listed, provided that no more than one device is specified
3292 * per top-level vdev mirror. The newly split pool is left in an exported
3293 * state unless -R is specified.
3295 * Restrictions: the top-level of the pool pool must only be made up of
3296 * mirrors; all devices in the pool must be healthy; no device may be
3297 * undergoing a resilvering operation.
3300 zpool_do_split(int argc, char **argv)
3302 char *srcpool, *newpool, *propval;
3303 char *mntopts = NULL;
3306 zpool_handle_t *zhp;
3307 nvlist_t *config, *props = NULL;
3309 flags.dryrun = B_FALSE;
3310 flags.import = B_FALSE;
3313 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3316 flags.import = B_TRUE;
3318 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3319 &props, B_TRUE) != 0) {
3326 flags.dryrun = B_TRUE;
3329 if ((propval = strchr(optarg, '=')) != NULL) {
3332 if (add_prop_list(optarg, propval,
3333 &props, B_TRUE) != 0) {
3343 (void) fprintf(stderr, gettext("missing argument for "
3344 "'%c' option\n"), optopt);
3348 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3355 if (!flags.import && mntopts != NULL) {
3356 (void) fprintf(stderr, gettext("setting mntopts is only "
3357 "valid when importing the pool\n"));
3365 (void) fprintf(stderr, gettext("Missing pool name\n"));
3369 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3379 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3382 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3383 if (config == NULL) {
3387 (void) printf(gettext("would create '%s' with the "
3388 "following layout:\n\n"), newpool);
3389 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3391 nvlist_free(config);
3396 if (ret != 0 || flags.dryrun || !flags.import)
3400 * The split was successful. Now we need to open the new
3401 * pool and import it.
3403 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3405 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3406 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3408 (void) fprintf(stderr, gettext("Split was successful, but "
3409 "the datasets could not all be mounted\n"));
3410 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3411 "different altroot\n"), "zpool import");
3421 * zpool online <pool> <device> ...
3424 zpool_do_online(int argc, char **argv)
3428 zpool_handle_t *zhp;
3430 vdev_state_t newstate;
3434 while ((c = getopt(argc, argv, "et")) != -1) {
3437 flags |= ZFS_ONLINE_EXPAND;
3441 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3450 /* get pool name and check number of arguments */
3452 (void) fprintf(stderr, gettext("missing pool name\n"));
3456 (void) fprintf(stderr, gettext("missing device name\n"));
3462 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3465 for (i = 1; i < argc; i++) {
3466 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3467 if (newstate != VDEV_STATE_HEALTHY) {
3468 (void) printf(gettext("warning: device '%s' "
3469 "onlined, but remains in faulted state\n"),
3471 if (newstate == VDEV_STATE_FAULTED)
3472 (void) printf(gettext("use 'zpool "
3473 "clear' to restore a faulted "
3476 (void) printf(gettext("use 'zpool "
3477 "replace' to replace devices "
3478 "that are no longer present\n"));
3491 * zpool offline [-ft] <pool> <device> ...
3493 * -f Force the device into the offline state, even if doing
3494 * so would appear to compromise pool availability.
3495 * (not supported yet)
3497 * -t Only take the device off-line temporarily. The offline
3498 * state will not be persistent across reboots.
3502 zpool_do_offline(int argc, char **argv)
3506 zpool_handle_t *zhp;
3508 boolean_t istmp = B_FALSE;
3511 while ((c = getopt(argc, argv, "ft")) != -1) {
3518 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3527 /* get pool name and check number of arguments */
3529 (void) fprintf(stderr, gettext("missing pool name\n"));
3533 (void) fprintf(stderr, gettext("missing device name\n"));
3539 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3542 for (i = 1; i < argc; i++) {
3543 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3553 * zpool clear <pool> [device]
3555 * Clear all errors associated with a pool or a particular device.
3558 zpool_do_clear(int argc, char **argv)
3562 boolean_t dryrun = B_FALSE;
3563 boolean_t do_rewind = B_FALSE;
3564 boolean_t xtreme_rewind = B_FALSE;
3565 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3566 nvlist_t *policy = NULL;
3567 zpool_handle_t *zhp;
3568 char *pool, *device;
3571 while ((c = getopt(argc, argv, "FnX")) != -1) {
3580 xtreme_rewind = B_TRUE;
3583 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3593 (void) fprintf(stderr, gettext("missing pool name\n"));
3598 (void) fprintf(stderr, gettext("too many arguments\n"));
3602 if ((dryrun || xtreme_rewind) && !do_rewind) {
3603 (void) fprintf(stderr,
3604 gettext("-n or -X only meaningful with -F\n"));
3608 rewind_policy = ZPOOL_TRY_REWIND;
3610 rewind_policy = ZPOOL_DO_REWIND;
3612 rewind_policy |= ZPOOL_EXTREME_REWIND;
3614 /* In future, further rewind policy choices can be passed along here */
3615 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3616 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3620 device = argc == 2 ? argv[1] : NULL;
3622 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3623 nvlist_free(policy);
3627 if (zpool_clear(zhp, device, policy) != 0)
3632 nvlist_free(policy);
3638 * zpool reguid <pool>
3641 zpool_do_reguid(int argc, char **argv)
3645 zpool_handle_t *zhp;
3649 while ((c = getopt(argc, argv, "")) != -1) {
3652 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3661 /* get pool name and check number of arguments */
3663 (void) fprintf(stderr, gettext("missing pool name\n"));
3668 (void) fprintf(stderr, gettext("too many arguments\n"));
3673 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3676 ret = zpool_reguid(zhp);
3684 * zpool reopen <pool>
3686 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3688 * NOTE: This command is currently undocumented. If the command is ever
3689 * exposed then the appropriate usage() messages will need to be made.
3692 zpool_do_reopen(int argc, char **argv)
3695 zpool_handle_t *zhp;
3705 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3708 ret = zpool_reopen(zhp);
3713 typedef struct scrub_cbdata {
3720 scrub_callback(zpool_handle_t *zhp, void *data)
3722 scrub_cbdata_t *cb = data;
3726 * Ignore faulted pools.
3728 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3729 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3730 "currently unavailable\n"), zpool_get_name(zhp));
3734 err = zpool_scan(zhp, cb->cb_type);
3740 * zpool scrub [-s] <pool> ...
3742 * -s Stop. Stops any in-progress scrub.
3745 zpool_do_scrub(int argc, char **argv)
3750 cb.cb_type = POOL_SCAN_SCRUB;
3753 while ((c = getopt(argc, argv, "s")) != -1) {
3756 cb.cb_type = POOL_SCAN_NONE;
3759 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3771 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3775 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3778 typedef struct status_cbdata {
3780 boolean_t cb_allpools;
3781 boolean_t cb_verbose;
3782 boolean_t cb_explain;
3784 boolean_t cb_dedup_stats;
3788 * Print out detailed scrub status.
3791 print_scan_status(pool_scan_stat_t *ps)
3794 uint64_t elapsed, mins_left, hours_left;
3795 uint64_t pass_exam, examined, total;
3797 double fraction_done;
3798 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3800 (void) printf(gettext(" scan: "));
3802 /* If there's never been a scan, there's not much to say. */
3803 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3804 ps->pss_func >= POOL_SCAN_FUNCS) {
3805 (void) printf(gettext("none requested\n"));
3809 start = ps->pss_start_time;
3810 end = ps->pss_end_time;
3811 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3813 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3814 ps->pss_func == POOL_SCAN_RESILVER);
3816 * Scan is finished or canceled.
3818 if (ps->pss_state == DSS_FINISHED) {
3819 uint64_t minutes_taken = (end - start) / 60;
3822 if (ps->pss_func == POOL_SCAN_SCRUB) {
3823 fmt = gettext("scrub repaired %s in %lluh%um with "
3824 "%llu errors on %s");
3825 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3826 fmt = gettext("resilvered %s in %lluh%um with "
3827 "%llu errors on %s");
3830 (void) printf(fmt, processed_buf,
3831 (u_longlong_t)(minutes_taken / 60),
3832 (uint_t)(minutes_taken % 60),
3833 (u_longlong_t)ps->pss_errors,
3834 ctime((time_t *)&end));
3836 } else if (ps->pss_state == DSS_CANCELED) {
3837 if (ps->pss_func == POOL_SCAN_SCRUB) {
3838 (void) printf(gettext("scrub canceled on %s"),
3840 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3841 (void) printf(gettext("resilver canceled on %s"),
3847 assert(ps->pss_state == DSS_SCANNING);
3850 * Scan is in progress.
3852 if (ps->pss_func == POOL_SCAN_SCRUB) {
3853 (void) printf(gettext("scrub in progress since %s"),
3855 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3856 (void) printf(gettext("resilver in progress since %s"),
3860 examined = ps->pss_examined ? ps->pss_examined : 1;
3861 total = ps->pss_to_examine;
3862 fraction_done = (double)examined / total;
3864 /* elapsed time for this pass */
3865 elapsed = time(NULL) - ps->pss_pass_start;
3866 elapsed = elapsed ? elapsed : 1;
3867 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3868 rate = pass_exam / elapsed;
3869 rate = rate ? rate : 1;
3870 mins_left = ((total - examined) / rate) / 60;
3871 hours_left = mins_left / 60;
3873 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3874 zfs_nicenum(total, total_buf, sizeof (total_buf));
3875 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3878 * do not print estimated time if hours_left is more than 30 days
3880 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3881 examined_buf, total_buf, rate_buf);
3882 if (hours_left < (30 * 24)) {
3883 (void) printf(gettext(", %lluh%um to go\n"),
3884 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3886 (void) printf(gettext(
3887 ", (scan is slow, no estimated time)\n"));
3890 if (ps->pss_func == POOL_SCAN_RESILVER) {
3891 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3892 processed_buf, 100 * fraction_done);
3893 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3894 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3895 processed_buf, 100 * fraction_done);
3900 print_error_log(zpool_handle_t *zhp)
3902 nvlist_t *nverrlist = NULL;
3905 size_t len = MAXPATHLEN * 2;
3907 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3908 (void) printf("errors: List of errors unavailable "
3909 "(insufficient privileges)\n");
3913 (void) printf("errors: Permanent errors have been "
3914 "detected in the following files:\n\n");
3916 pathname = safe_malloc(len);
3918 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3920 uint64_t dsobj, obj;
3922 verify(nvpair_value_nvlist(elem, &nv) == 0);
3923 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3925 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3927 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3928 (void) printf("%7s %s\n", "", pathname);
3931 nvlist_free(nverrlist);
3935 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3944 (void) printf(gettext("\tspares\n"));
3946 for (i = 0; i < nspares; i++) {
3947 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3948 print_status_config(zhp, name, spares[i],
3949 namewidth, 2, B_TRUE);
3955 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3964 (void) printf(gettext("\tcache\n"));
3966 for (i = 0; i < nl2cache; i++) {
3967 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3968 print_status_config(zhp, name, l2cache[i],
3969 namewidth, 2, B_FALSE);
3975 print_dedup_stats(nvlist_t *config)
3977 ddt_histogram_t *ddh;
3983 * If the pool was faulted then we may not have been able to
3984 * obtain the config. Otherwise, if have anything in the dedup
3985 * table continue processing the stats.
3987 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3988 (uint64_t **)&ddo, &c) != 0)
3991 (void) printf("\n");
3992 (void) printf(gettext(" dedup: "));
3993 if (ddo->ddo_count == 0) {
3994 (void) printf(gettext("no DDT entries\n"));
3998 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3999 (u_longlong_t)ddo->ddo_count,
4000 (u_longlong_t)ddo->ddo_dspace,
4001 (u_longlong_t)ddo->ddo_mspace);
4003 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4004 (uint64_t **)&dds, &c) == 0);
4005 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4006 (uint64_t **)&ddh, &c) == 0);
4007 zpool_dump_ddt(dds, ddh);
4011 * Display a summary of pool status. Displays a summary such as:
4015 * reason: One or more devices ...
4016 * see: http://illumos.org/msg/ZFS-xxxx-01
4022 * When given the '-v' option, we print out the complete config. If the '-e'
4023 * option is specified, then we print out error rate information as well.
4026 status_callback(zpool_handle_t *zhp, void *data)
4028 status_cbdata_t *cbp = data;
4029 nvlist_t *config, *nvroot;
4036 config = zpool_get_config(zhp, NULL);
4037 reason = zpool_get_status(zhp, &msgid);
4042 * If we were given 'zpool status -x', only report those pools with
4045 if (cbp->cb_explain &&
4046 (reason == ZPOOL_STATUS_OK ||
4047 reason == ZPOOL_STATUS_VERSION_OLDER ||
4048 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4049 if (!cbp->cb_allpools) {
4050 (void) printf(gettext("pool '%s' is healthy\n"),
4051 zpool_get_name(zhp));
4053 cbp->cb_first = B_FALSE;
4059 cbp->cb_first = B_FALSE;
4061 (void) printf("\n");
4063 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4065 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4066 (uint64_t **)&vs, &c) == 0);
4067 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4069 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4070 (void) printf(gettext(" state: %s\n"), health);
4073 case ZPOOL_STATUS_MISSING_DEV_R:
4074 (void) printf(gettext("status: One or more devices could not "
4075 "be opened. Sufficient replicas exist for\n\tthe pool to "
4076 "continue functioning in a degraded state.\n"));
4077 (void) printf(gettext("action: Attach the missing device and "
4078 "online it using 'zpool online'.\n"));
4081 case ZPOOL_STATUS_MISSING_DEV_NR:
4082 (void) printf(gettext("status: One or more devices could not "
4083 "be opened. There are insufficient\n\treplicas for the "
4084 "pool to continue functioning.\n"));
4085 (void) printf(gettext("action: Attach the missing device and "
4086 "online it using 'zpool online'.\n"));
4089 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4090 (void) printf(gettext("status: One or more devices could not "
4091 "be used because the label is missing or\n\tinvalid. "
4092 "Sufficient replicas exist for the pool to continue\n\t"
4093 "functioning in a degraded state.\n"));
4094 (void) printf(gettext("action: Replace the device using "
4095 "'zpool replace'.\n"));
4098 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4099 (void) printf(gettext("status: One or more devices could not "
4100 "be used because the label is missing \n\tor invalid. "
4101 "There are insufficient replicas for the pool to "
4102 "continue\n\tfunctioning.\n"));
4103 zpool_explain_recover(zpool_get_handle(zhp),
4104 zpool_get_name(zhp), reason, config);
4107 case ZPOOL_STATUS_FAILING_DEV:
4108 (void) printf(gettext("status: One or more devices has "
4109 "experienced an unrecoverable error. An\n\tattempt was "
4110 "made to correct the error. Applications are "
4112 (void) printf(gettext("action: Determine if the device needs "
4113 "to be replaced, and clear the errors\n\tusing "
4114 "'zpool clear' or replace the device with 'zpool "
4118 case ZPOOL_STATUS_OFFLINE_DEV:
4119 (void) printf(gettext("status: One or more devices has "
4120 "been taken offline by the administrator.\n\tSufficient "
4121 "replicas exist for the pool to continue functioning in "
4122 "a\n\tdegraded state.\n"));
4123 (void) printf(gettext("action: Online the device using "
4124 "'zpool online' or replace the device with\n\t'zpool "
4128 case ZPOOL_STATUS_REMOVED_DEV:
4129 (void) printf(gettext("status: One or more devices has "
4130 "been removed by the administrator.\n\tSufficient "
4131 "replicas exist for the pool to continue functioning in "
4132 "a\n\tdegraded state.\n"));
4133 (void) printf(gettext("action: Online the device using "
4134 "'zpool online' or replace the device with\n\t'zpool "
4138 case ZPOOL_STATUS_RESILVERING:
4139 (void) printf(gettext("status: One or more devices is "
4140 "currently being resilvered. The pool will\n\tcontinue "
4141 "to function, possibly in a degraded state.\n"));
4142 (void) printf(gettext("action: Wait for the resilver to "
4146 case ZPOOL_STATUS_CORRUPT_DATA:
4147 (void) printf(gettext("status: One or more devices has "
4148 "experienced an error resulting in data\n\tcorruption. "
4149 "Applications may be affected.\n"));
4150 (void) printf(gettext("action: Restore the file in question "
4151 "if possible. Otherwise restore the\n\tentire pool from "
4155 case ZPOOL_STATUS_CORRUPT_POOL:
4156 (void) printf(gettext("status: The pool metadata is corrupted "
4157 "and the pool cannot be opened.\n"));
4158 zpool_explain_recover(zpool_get_handle(zhp),
4159 zpool_get_name(zhp), reason, config);
4162 case ZPOOL_STATUS_VERSION_OLDER:
4163 (void) printf(gettext("status: The pool is formatted using a "
4164 "legacy on-disk format. The pool can\n\tstill be used, "
4165 "but some features are unavailable.\n"));
4166 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4167 "upgrade'. Once this is done, the\n\tpool will no longer "
4168 "be accessible on software that does not support feature\n"
4172 case ZPOOL_STATUS_VERSION_NEWER:
4173 (void) printf(gettext("status: The pool has been upgraded to a "
4174 "newer, incompatible on-disk version.\n\tThe pool cannot "
4175 "be accessed on this system.\n"));
4176 (void) printf(gettext("action: Access the pool from a system "
4177 "running more recent software, or\n\trestore the pool from "
4181 case ZPOOL_STATUS_FEAT_DISABLED:
4182 (void) printf(gettext("status: Some supported features are not "
4183 "enabled on the pool. The pool can\n\tstill be used, but "
4184 "some features are unavailable.\n"));
4185 (void) printf(gettext("action: Enable all features using "
4186 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4187 "longer be accessible by software that does not support\n\t"
4188 "the features. See zpool-features(7) for details.\n"));
4191 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4192 (void) printf(gettext("status: The pool cannot be accessed on "
4193 "this system because it uses the\n\tfollowing feature(s) "
4194 "not supported on this system:\n"));
4195 zpool_print_unsup_feat(config);
4196 (void) printf("\n");
4197 (void) printf(gettext("action: Access the pool from a system "
4198 "that supports the required feature(s),\n\tor restore the "
4199 "pool from backup.\n"));
4202 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4203 (void) printf(gettext("status: The pool can only be accessed "
4204 "in read-only mode on this system. It\n\tcannot be "
4205 "accessed in read-write mode because it uses the "
4206 "following\n\tfeature(s) not supported on this system:\n"));
4207 zpool_print_unsup_feat(config);
4208 (void) printf("\n");
4209 (void) printf(gettext("action: The pool cannot be accessed in "
4210 "read-write mode. Import the pool with\n"
4211 "\t\"-o readonly=on\", access the pool from a system that "
4212 "supports the\n\trequired feature(s), or restore the "
4213 "pool from backup.\n"));
4216 case ZPOOL_STATUS_FAULTED_DEV_R:
4217 (void) printf(gettext("status: One or more devices are "
4218 "faulted in response to persistent errors.\n\tSufficient "
4219 "replicas exist for the pool to continue functioning "
4220 "in a\n\tdegraded state.\n"));
4221 (void) printf(gettext("action: Replace the faulted device, "
4222 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4225 case ZPOOL_STATUS_FAULTED_DEV_NR:
4226 (void) printf(gettext("status: One or more devices are "
4227 "faulted in response to persistent errors. There are "
4228 "insufficient replicas for the pool to\n\tcontinue "
4230 (void) printf(gettext("action: Destroy and re-create the pool "
4231 "from a backup source. Manually marking the device\n"
4232 "\trepaired using 'zpool clear' may allow some data "
4233 "to be recovered.\n"));
4236 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4237 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4238 (void) printf(gettext("status: One or more devices are "
4239 "faulted in response to IO failures.\n"));
4240 (void) printf(gettext("action: Make sure the affected devices "
4241 "are connected, then run 'zpool clear'.\n"));
4244 case ZPOOL_STATUS_BAD_LOG:
4245 (void) printf(gettext("status: An intent log record "
4246 "could not be read.\n"
4247 "\tWaiting for adminstrator intervention to fix the "
4248 "faulted pool.\n"));
4249 (void) printf(gettext("action: Either restore the affected "
4250 "device(s) and run 'zpool online',\n"
4251 "\tor ignore the intent log records by running "
4252 "'zpool clear'.\n"));
4257 * The remaining errors can't actually be generated, yet.
4259 assert(reason == ZPOOL_STATUS_OK);
4263 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4266 if (config != NULL) {
4269 nvlist_t **spares, **l2cache;
4270 uint_t nspares, nl2cache;
4271 pool_scan_stat_t *ps = NULL;
4273 (void) nvlist_lookup_uint64_array(nvroot,
4274 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4275 print_scan_status(ps);
4277 namewidth = max_width(zhp, nvroot, 0, 0);
4281 (void) printf(gettext("config:\n\n"));
4282 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4283 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4284 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4285 namewidth, 0, B_FALSE);
4287 if (num_logs(nvroot) > 0)
4288 print_logs(zhp, nvroot, namewidth, B_TRUE);
4289 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4290 &l2cache, &nl2cache) == 0)
4291 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4293 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4294 &spares, &nspares) == 0)
4295 print_spares(zhp, spares, nspares, namewidth);
4297 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4299 nvlist_t *nverrlist = NULL;
4302 * If the approximate error count is small, get a
4303 * precise count by fetching the entire log and
4304 * uniquifying the results.
4306 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4307 zpool_get_errlog(zhp, &nverrlist) == 0) {
4312 while ((elem = nvlist_next_nvpair(nverrlist,
4317 nvlist_free(nverrlist);
4319 (void) printf("\n");
4322 (void) printf(gettext("errors: No known data "
4324 else if (!cbp->cb_verbose)
4325 (void) printf(gettext("errors: %llu data "
4326 "errors, use '-v' for a list\n"),
4327 (u_longlong_t)nerr);
4329 print_error_log(zhp);
4332 if (cbp->cb_dedup_stats)
4333 print_dedup_stats(config);
4335 (void) printf(gettext("config: The configuration cannot be "
4343 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4345 * -v Display complete error logs
4346 * -x Display only pools with potential problems
4347 * -D Display dedup status (undocumented)
4348 * -T Display a timestamp in date(1) or Unix format
4350 * Describes the health status of all pools or some subset.
4353 zpool_do_status(int argc, char **argv)
4357 unsigned long interval = 0, count = 0;
4358 status_cbdata_t cb = { 0 };
4361 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4364 cb.cb_verbose = B_TRUE;
4367 cb.cb_explain = B_TRUE;
4370 cb.cb_dedup_stats = B_TRUE;
4373 get_timestamp_arg(*optarg);
4376 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4385 get_interval_count(&argc, argv, &interval, &count);
4388 cb.cb_allpools = B_TRUE;
4390 cb.cb_first = B_TRUE;
4393 if (timestamp_fmt != NODATE)
4394 print_timestamp(timestamp_fmt);
4396 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4397 status_callback, &cb);
4399 if (argc == 0 && cb.cb_count == 0)
4400 (void) printf(gettext("no pools available\n"));
4401 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4402 (void) printf(gettext("all pools are healthy\n"));
4410 if (count != 0 && --count == 0)
4413 (void) sleep(interval);
4419 typedef struct upgrade_cbdata {
4421 char cb_poolname[ZPOOL_MAXNAMELEN];
4423 uint64_t cb_version;
4429 is_root_pool(zpool_handle_t *zhp)
4431 static struct statfs sfs;
4432 static char *poolname = NULL;
4433 static boolean_t stated = B_FALSE;
4438 if (statfs("/", &sfs) == -1) {
4439 (void) fprintf(stderr,
4440 "Unable to stat root file system: %s.\n",
4444 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4446 poolname = sfs.f_mntfromname;
4447 if ((slash = strchr(poolname, '/')) != NULL)
4450 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4454 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) {
4456 if (poolname[0] == '\0' && is_root_pool(zhp))
4457 (void) strlcpy(poolname, zpool_get_name(zhp), size);
4459 #endif /* FreeBSD */
4462 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4466 uint64_t oldversion;
4468 config = zpool_get_config(zhp, NULL);
4469 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4472 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4473 assert(oldversion < version);
4475 ret = zpool_upgrade(zhp, version);
4479 if (version >= SPA_VERSION_FEATURES) {
4480 (void) printf(gettext("Successfully upgraded "
4481 "'%s' from version %llu to feature flags.\n"),
4482 zpool_get_name(zhp), oldversion);
4484 (void) printf(gettext("Successfully upgraded "
4485 "'%s' from version %llu to version %llu.\n"),
4486 zpool_get_name(zhp), oldversion, version);
4493 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4496 boolean_t firstff = B_TRUE;
4497 nvlist_t *enabled = zpool_get_features(zhp);
4500 for (i = 0; i < SPA_FEATURES; i++) {
4501 const char *fname = spa_feature_table[i].fi_uname;
4502 const char *fguid = spa_feature_table[i].fi_guid;
4503 if (!nvlist_exists(enabled, fguid)) {
4505 verify(-1 != asprintf(&propname, "feature@%s", fname));
4506 ret = zpool_set_prop(zhp, propname,
4507 ZFS_FEATURE_ENABLED);
4515 (void) printf(gettext("Enabled the "
4516 "following features on '%s':\n"),
4517 zpool_get_name(zhp));
4520 (void) printf(gettext(" %s\n"), fname);
4531 upgrade_cb(zpool_handle_t *zhp, void *arg)
4533 upgrade_cbdata_t *cbp = arg;
4536 boolean_t printnl = B_FALSE;
4539 config = zpool_get_config(zhp, NULL);
4540 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4543 assert(SPA_VERSION_IS_SUPPORTED(version));
4545 if (version < cbp->cb_version) {
4546 cbp->cb_first = B_FALSE;
4547 ret = upgrade_version(zhp, cbp->cb_version);
4551 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4552 sizeof(cbp->cb_poolname));
4553 #endif /* ___FreeBSD__ */
4558 * If they did "zpool upgrade -a", then we could
4559 * be doing ioctls to different pools. We need
4560 * to log this history once to each pool, and bypass
4561 * the normal history logging that happens in main().
4563 (void) zpool_log_history(g_zfs, history_str);
4564 log_history = B_FALSE;
4568 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4570 ret = upgrade_enable_all(zhp, &count);
4575 cbp->cb_first = B_FALSE;
4581 (void) printf(gettext("\n"));
4588 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4590 upgrade_cbdata_t *cbp = arg;
4594 config = zpool_get_config(zhp, NULL);
4595 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4598 assert(SPA_VERSION_IS_SUPPORTED(version));
4600 if (version < SPA_VERSION_FEATURES) {
4601 if (cbp->cb_first) {
4602 (void) printf(gettext("The following pools are "
4603 "formatted with legacy version numbers and can\n"
4604 "be upgraded to use feature flags. After "
4605 "being upgraded, these pools\nwill no "
4606 "longer be accessible by software that does not "
4607 "support feature\nflags.\n\n"));
4608 (void) printf(gettext("VER POOL\n"));
4609 (void) printf(gettext("--- ------------\n"));
4610 cbp->cb_first = B_FALSE;
4613 (void) printf("%2llu %s\n", (u_longlong_t)version,
4614 zpool_get_name(zhp));
4621 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4623 upgrade_cbdata_t *cbp = arg;
4627 config = zpool_get_config(zhp, NULL);
4628 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4631 if (version >= SPA_VERSION_FEATURES) {
4633 boolean_t poolfirst = B_TRUE;
4634 nvlist_t *enabled = zpool_get_features(zhp);
4636 for (i = 0; i < SPA_FEATURES; i++) {
4637 const char *fguid = spa_feature_table[i].fi_guid;
4638 const char *fname = spa_feature_table[i].fi_uname;
4639 if (!nvlist_exists(enabled, fguid)) {
4640 if (cbp->cb_first) {
4641 (void) printf(gettext("\nSome "
4642 "supported features are not "
4643 "enabled on the following pools. "
4644 "Once a\nfeature is enabled the "
4645 "pool may become incompatible with "
4646 "software\nthat does not support "
4648 "zpool-features(7) for "
4650 (void) printf(gettext("POOL "
4652 (void) printf(gettext("------"
4654 cbp->cb_first = B_FALSE;
4658 (void) printf(gettext("%s\n"),
4659 zpool_get_name(zhp));
4660 poolfirst = B_FALSE;
4663 (void) printf(gettext(" %s\n"), fname);
4673 upgrade_one(zpool_handle_t *zhp, void *data)
4675 boolean_t printnl = B_FALSE;
4676 upgrade_cbdata_t *cbp = data;
4677 uint64_t cur_version;
4680 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4681 (void) printf(gettext("'log' is now a reserved word\n"
4682 "Pool 'log' must be renamed using export and import"
4687 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4688 if (cur_version > cbp->cb_version) {
4689 (void) printf(gettext("Pool '%s' is already formatted "
4690 "using more current version '%llu'.\n\n"),
4691 zpool_get_name(zhp), cur_version);
4695 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4696 (void) printf(gettext("Pool '%s' is already formatted "
4697 "using version %llu.\n\n"), zpool_get_name(zhp),
4702 if (cur_version != cbp->cb_version) {
4704 ret = upgrade_version(zhp, cbp->cb_version);
4708 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4709 sizeof(cbp->cb_poolname));
4710 #endif /* ___FreeBSD__ */
4713 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4715 ret = upgrade_enable_all(zhp, &count);
4722 root_pool_upgrade_check(zhp, cbp->cb_poolname,
4723 sizeof(cbp->cb_poolname));
4724 #endif /* __FreeBSD __*/
4725 } else if (cur_version == SPA_VERSION) {
4726 (void) printf(gettext("Pool '%s' already has all "
4727 "supported features enabled.\n"),
4728 zpool_get_name(zhp));
4733 (void) printf(gettext("\n"));
4742 * zpool upgrade [-V version] <-a | pool ...>
4744 * With no arguments, display downrev'd ZFS pool available for upgrade.
4745 * Individual pools can be upgraded by specifying the pool, and '-a' will
4746 * upgrade all pools.
4749 zpool_do_upgrade(int argc, char **argv)
4752 upgrade_cbdata_t cb = { 0 };
4754 boolean_t showversions = B_FALSE;
4755 boolean_t upgradeall = B_FALSE;
4760 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4763 upgradeall = B_TRUE;
4766 showversions = B_TRUE;
4769 cb.cb_version = strtoll(optarg, &end, 10);
4771 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4772 (void) fprintf(stderr,
4773 gettext("invalid version '%s'\n"), optarg);
4778 (void) fprintf(stderr, gettext("missing argument for "
4779 "'%c' option\n"), optopt);
4783 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4794 if (cb.cb_version == 0) {
4795 cb.cb_version = SPA_VERSION;
4796 } else if (!upgradeall && argc == 0) {
4797 (void) fprintf(stderr, gettext("-V option is "
4798 "incompatible with other arguments\n"));
4803 if (upgradeall || argc != 0) {
4804 (void) fprintf(stderr, gettext("-v option is "
4805 "incompatible with other arguments\n"));
4808 } else if (upgradeall) {
4810 (void) fprintf(stderr, gettext("-a option should not "
4811 "be used along with a pool name\n"));
4816 (void) printf(gettext("This system supports ZFS pool feature "
4821 (void) printf(gettext("The following features are "
4823 (void) printf(gettext("FEAT DESCRIPTION\n"));
4824 (void) printf("----------------------------------------------"
4825 "---------------\n");
4826 for (i = 0; i < SPA_FEATURES; i++) {
4827 zfeature_info_t *fi = &spa_feature_table[i];
4828 const char *ro = fi->fi_can_readonly ?
4829 " (read-only compatible)" : "";
4831 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4832 (void) printf(" %s\n", fi->fi_desc);
4834 (void) printf("\n");
4836 (void) printf(gettext("The following legacy versions are also "
4838 (void) printf(gettext("VER DESCRIPTION\n"));
4839 (void) printf("--- -----------------------------------------"
4840 "---------------\n");
4841 (void) printf(gettext(" 1 Initial ZFS version\n"));
4842 (void) printf(gettext(" 2 Ditto blocks "
4843 "(replicated metadata)\n"));
4844 (void) printf(gettext(" 3 Hot spares and double parity "
4846 (void) printf(gettext(" 4 zpool history\n"));
4847 (void) printf(gettext(" 5 Compression using the gzip "
4849 (void) printf(gettext(" 6 bootfs pool property\n"));
4850 (void) printf(gettext(" 7 Separate intent log devices\n"));
4851 (void) printf(gettext(" 8 Delegated administration\n"));
4852 (void) printf(gettext(" 9 refquota and refreservation "
4854 (void) printf(gettext(" 10 Cache devices\n"));
4855 (void) printf(gettext(" 11 Improved scrub performance\n"));
4856 (void) printf(gettext(" 12 Snapshot properties\n"));
4857 (void) printf(gettext(" 13 snapused property\n"));
4858 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4859 (void) printf(gettext(" 15 user/group space accounting\n"));
4860 (void) printf(gettext(" 16 stmf property support\n"));
4861 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4862 (void) printf(gettext(" 18 Snapshot user holds\n"));
4863 (void) printf(gettext(" 19 Log device removal\n"));
4864 (void) printf(gettext(" 20 Compression using zle "
4865 "(zero-length encoding)\n"));
4866 (void) printf(gettext(" 21 Deduplication\n"));
4867 (void) printf(gettext(" 22 Received properties\n"));
4868 (void) printf(gettext(" 23 Slim ZIL\n"));
4869 (void) printf(gettext(" 24 System attributes\n"));
4870 (void) printf(gettext(" 25 Improved scrub stats\n"));
4871 (void) printf(gettext(" 26 Improved snapshot deletion "
4873 (void) printf(gettext(" 27 Improved snapshot creation "
4875 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4876 (void) printf(gettext("\nFor more information on a particular "
4877 "version, including supported releases,\n"));
4878 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4879 } else if (argc == 0 && upgradeall) {
4880 cb.cb_first = B_TRUE;
4881 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4882 if (ret == 0 && cb.cb_first) {
4883 if (cb.cb_version == SPA_VERSION) {
4884 (void) printf(gettext("All pools are already "
4885 "formatted using feature flags.\n\n"));
4886 (void) printf(gettext("Every feature flags "
4887 "pool already has all supported features "
4890 (void) printf(gettext("All pools are already "
4891 "formatted with version %llu or higher.\n"),
4895 } else if (argc == 0) {
4896 cb.cb_first = B_TRUE;
4897 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4901 (void) printf(gettext("All pools are formatted "
4902 "using feature flags.\n\n"));
4904 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4905 "for a list of available legacy versions.\n"));
4908 cb.cb_first = B_TRUE;
4909 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4913 (void) printf(gettext("Every feature flags pool has "
4914 "all supported features enabled.\n"));
4916 (void) printf(gettext("\n"));
4919 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4923 if (cb.cb_poolname[0] != '\0') {
4925 "If you boot from pool '%s', don't forget to update boot code.\n"
4926 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4927 "the following command will do it:\n"
4929 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4936 typedef struct hist_cbdata {
4943 * Print out the command history for a specific pool.
4946 get_history_one(zpool_handle_t *zhp, void *data)
4962 char internalstr[MAXPATHLEN];
4963 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4967 cb->first = B_FALSE;
4969 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4971 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4974 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4975 &records, &numrecords) == 0);
4976 for (i = 0; i < numrecords; i++) {
4977 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4981 /* is it an internal event or a standard event? */
4982 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4984 if (cb->internal == 0)
4987 if (nvlist_lookup_uint64(records[i],
4988 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4990 verify(nvlist_lookup_uint64(records[i],
4991 ZPOOL_HIST_TXG, &txg) == 0);
4992 verify(nvlist_lookup_string(records[i],
4993 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4994 if (ievent >= LOG_END)
4996 (void) snprintf(internalstr,
4997 sizeof (internalstr),
4998 "[internal %s txg:%lld] %s",
4999 zfs_history_event_names[ievent], txg,
5001 cmdstr = internalstr;
5004 (void) localtime_r(&tsec, &t);
5005 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5006 (void) printf("%s %s", tbuf, cmdstr);
5009 (void) printf("\n");
5012 (void) printf(" [");
5013 if (nvlist_lookup_uint64(records[i],
5014 ZPOOL_HIST_WHO, &who) == 0) {
5015 pwd = getpwuid((uid_t)who);
5017 (void) printf("user %s on",
5020 (void) printf("user %d on",
5023 (void) printf(gettext("no info]\n"));
5026 if (nvlist_lookup_string(records[i],
5027 ZPOOL_HIST_HOST, &hostname) == 0) {
5028 (void) printf(" %s", hostname);
5030 if (nvlist_lookup_string(records[i],
5031 ZPOOL_HIST_ZONE, &zonename) == 0) {
5032 (void) printf(":%s", zonename);
5036 (void) printf("\n");
5038 (void) printf("\n");
5045 * zpool history <pool>
5047 * Displays the history of commands that modified pools.
5052 zpool_do_history(int argc, char **argv)
5054 hist_cbdata_t cbdata = { 0 };
5058 cbdata.first = B_TRUE;
5060 while ((c = getopt(argc, argv, "li")) != -1) {
5066 cbdata.internal = 1;
5069 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5077 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5080 if (argc == 0 && cbdata.first == B_TRUE) {
5081 (void) printf(gettext("no pools available\n"));
5089 get_callback(zpool_handle_t *zhp, void *data)
5091 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5092 char value[MAXNAMELEN];
5093 zprop_source_t srctype;
5096 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5099 * Skip the special fake placeholder. This will also skip
5100 * over the name property when 'all' is specified.
5102 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5103 pl == cbp->cb_proplist)
5106 if (pl->pl_prop == ZPROP_INVAL &&
5107 (zpool_prop_feature(pl->pl_user_prop) ||
5108 zpool_prop_unsupported(pl->pl_user_prop))) {
5109 srctype = ZPROP_SRC_LOCAL;
5111 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5112 value, sizeof (value)) == 0) {
5113 zprop_print_one_property(zpool_get_name(zhp),
5114 cbp, pl->pl_user_prop, value, srctype,
5118 if (zpool_get_prop(zhp, pl->pl_prop, value,
5119 sizeof (value), &srctype) != 0)
5122 zprop_print_one_property(zpool_get_name(zhp), cbp,
5123 zpool_prop_to_name(pl->pl_prop), value, srctype,
5131 zpool_do_get(int argc, char **argv)
5133 zprop_get_cbdata_t cb = { 0 };
5134 zprop_list_t fake_name = { 0 };
5138 (void) fprintf(stderr, gettext("missing property "
5143 cb.cb_first = B_TRUE;
5144 cb.cb_sources = ZPROP_SRC_ALL;
5145 cb.cb_columns[0] = GET_COL_NAME;
5146 cb.cb_columns[1] = GET_COL_PROPERTY;
5147 cb.cb_columns[2] = GET_COL_VALUE;
5148 cb.cb_columns[3] = GET_COL_SOURCE;
5149 cb.cb_type = ZFS_TYPE_POOL;
5151 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
5152 ZFS_TYPE_POOL) != 0)
5155 if (cb.cb_proplist != NULL) {
5156 fake_name.pl_prop = ZPOOL_PROP_NAME;
5157 fake_name.pl_width = strlen(gettext("NAME"));
5158 fake_name.pl_next = cb.cb_proplist;
5159 cb.cb_proplist = &fake_name;
5162 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
5165 if (cb.cb_proplist == &fake_name)
5166 zprop_free_list(fake_name.pl_next);
5168 zprop_free_list(cb.cb_proplist);
5173 typedef struct set_cbdata {
5176 boolean_t cb_any_successful;
5180 set_callback(zpool_handle_t *zhp, void *data)
5183 set_cbdata_t *cb = (set_cbdata_t *)data;
5185 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5188 cb->cb_any_successful = B_TRUE;
5194 zpool_do_set(int argc, char **argv)
5196 set_cbdata_t cb = { 0 };
5199 if (argc > 1 && argv[1][0] == '-') {
5200 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5206 (void) fprintf(stderr, gettext("missing property=value "
5212 (void) fprintf(stderr, gettext("missing pool name\n"));
5217 (void) fprintf(stderr, gettext("too many pool names\n"));
5221 cb.cb_propname = argv[1];
5222 cb.cb_value = strchr(cb.cb_propname, '=');
5223 if (cb.cb_value == NULL) {
5224 (void) fprintf(stderr, gettext("missing value in "
5225 "property=value argument\n"));
5229 *(cb.cb_value) = '\0';
5232 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5239 find_command_idx(char *command, int *idx)
5243 for (i = 0; i < NCOMMAND; i++) {
5244 if (command_table[i].name == NULL)
5247 if (strcmp(command, command_table[i].name) == 0) {
5256 main(int argc, char **argv)
5262 (void) setlocale(LC_ALL, "");
5263 (void) textdomain(TEXT_DOMAIN);
5265 if ((g_zfs = libzfs_init()) == NULL) {
5266 (void) fprintf(stderr, gettext("internal error: failed to "
5267 "initialize ZFS library\n"));
5271 libzfs_print_on_error(g_zfs, B_TRUE);
5276 * Make sure the user has specified some command.
5279 (void) fprintf(stderr, gettext("missing command\n"));
5288 if (strcmp(cmdname, "-?") == 0)
5291 zpool_set_history_str("zpool", argc, argv, history_str);
5292 verify(zpool_stage_history(g_zfs, history_str) == 0);
5295 * Run the appropriate command.
5297 if (find_command_idx(cmdname, &i) == 0) {
5298 current_command = &command_table[i];
5299 ret = command_table[i].func(argc - 1, argv + 1);
5300 } else if (strchr(cmdname, '=')) {
5301 verify(find_command_idx("set", &i) == 0);
5302 current_command = &command_table[i];
5303 ret = command_table[i].func(argc, argv);
5304 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5306 * 'freeze' is a vile debugging abomination, so we treat
5310 int fd = open(ZFS_DEV, O_RDWR);
5311 (void) strcpy((void *)buf, argv[2]);
5312 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5314 (void) fprintf(stderr, gettext("unrecognized "
5315 "command '%s'\n"), cmdname);
5322 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5323 * for the purposes of running ::findleaks.
5325 if (getenv("ZFS_ABORT") != NULL) {
5326 (void) printf("dumping core by request\n");