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 by Cyril Plisko. All rights reserved.
48 #include <sys/fs/zfs.h>
50 #include <sys/fm/util.h>
51 #include <sys/fm/protocol.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 **);
92 static int zpool_do_events(int, char **);
94 static int zpool_do_get(int, char **);
95 static int zpool_do_set(int, char **);
98 * These libumem hooks provide a reasonable set of defaults for the allocator's
99 * debugging facilities.
104 _umem_debug_init(void)
106 return ("default,verbose"); /* $UMEM_DEBUG setting */
110 _umem_logging_init(void)
112 return ("fail,contents"); /* $UMEM_LOGGING setting */
145 typedef struct zpool_command {
147 int (*func)(int, char **);
152 * Master command table. Each ZFS command has a name, associated function, and
153 * usage message. The usage messages need to be internationalized, so we have
154 * to have a function to return the usage message based on a command index.
156 * These commands are organized according to how they are displayed in the usage
157 * message. An empty command (one with a NULL name) indicates an empty line in
158 * the generic usage message.
160 static zpool_command_t command_table[] = {
161 { "create", zpool_do_create, HELP_CREATE },
162 { "destroy", zpool_do_destroy, HELP_DESTROY },
164 { "add", zpool_do_add, HELP_ADD },
165 { "remove", zpool_do_remove, HELP_REMOVE },
167 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
169 { "list", zpool_do_list, HELP_LIST },
170 { "iostat", zpool_do_iostat, HELP_IOSTAT },
171 { "status", zpool_do_status, HELP_STATUS },
173 { "online", zpool_do_online, HELP_ONLINE },
174 { "offline", zpool_do_offline, HELP_OFFLINE },
175 { "clear", zpool_do_clear, HELP_CLEAR },
176 { "reopen", zpool_do_reopen, HELP_REOPEN },
178 { "attach", zpool_do_attach, HELP_ATTACH },
179 { "detach", zpool_do_detach, HELP_DETACH },
180 { "replace", zpool_do_replace, HELP_REPLACE },
181 { "split", zpool_do_split, HELP_SPLIT },
183 { "scrub", zpool_do_scrub, HELP_SCRUB },
185 { "import", zpool_do_import, HELP_IMPORT },
186 { "export", zpool_do_export, HELP_EXPORT },
187 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
188 { "reguid", zpool_do_reguid, HELP_REGUID },
190 { "history", zpool_do_history, HELP_HISTORY },
191 { "events", zpool_do_events, HELP_EVENTS },
193 { "get", zpool_do_get, HELP_GET },
194 { "set", zpool_do_set, HELP_SET },
197 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
199 zpool_command_t *current_command;
200 static char history_str[HIS_MAX_RECORD_LEN];
202 static uint_t timestamp_fmt = NODATE;
205 get_usage(zpool_help_t idx) {
208 return (gettext("\tadd [-fn] [-o property=value] "
209 "<pool> <vdev> ...\n"));
211 return (gettext("\tattach [-f] [-o property=value] "
212 "<pool> <device> <new-device>\n"));
214 return (gettext("\tclear [-nF] <pool> [device]\n"));
216 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
217 "\t [-O file-system-property=value] ... \n"
218 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
220 return (gettext("\tdestroy [-f] <pool>\n"));
222 return (gettext("\tdetach <pool> <device>\n"));
224 return (gettext("\texport [-f] <pool> ...\n"));
226 return (gettext("\thistory [-il] [<pool>] ...\n"));
228 return (gettext("\timport [-d dir] [-D]\n"
229 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
230 "\timport [-o mntopts] [-o property=value] ... \n"
231 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
232 "[-R root] [-F [-n]] -a\n"
233 "\timport [-o mntopts] [-o property=value] ... \n"
234 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
235 "[-R root] [-F [-n]]\n"
236 "\t <pool | id> [newpool]\n"));
238 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
240 case HELP_LABELCLEAR:
241 return (gettext("\tlabelclear [-f] <vdev>\n"));
243 return (gettext("\tlist [-Hv] [-o property[,...]] "
244 "[-T d|u] [pool] ... [interval [count]]\n"));
246 return (gettext("\toffline [-t] <pool> <device> ...\n"));
248 return (gettext("\tonline <pool> <device> ...\n"));
250 return (gettext("\treplace [-f] <pool> <device> "
253 return (gettext("\tremove <pool> <device> ...\n"));
255 return (gettext("\treopen <pool>\n"));
257 return (gettext("\tscrub [-s] <pool> ...\n"));
259 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
262 return (gettext("\tupgrade\n"
264 "\tupgrade [-V version] <-a | pool ...>\n"));
266 return (gettext("\tevents [-vHfc]\n"));
268 return (gettext("\tget [-p] <\"all\" | property[,...]> "
271 return (gettext("\tset <property=value> <pool> \n"));
273 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
274 "\t [-o property=value] <pool> <newpool> "
275 "[<device> ...]\n"));
277 return (gettext("\treguid <pool>\n"));
286 * Callback routine that will print out a pool property value.
289 print_prop_cb(int prop, void *cb)
293 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
295 if (zpool_prop_readonly(prop))
296 (void) fprintf(fp, " NO ");
298 (void) fprintf(fp, " YES ");
300 if (zpool_prop_values(prop) == NULL)
301 (void) fprintf(fp, "-\n");
303 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
309 * Display usage message. If we're inside a command, display only the usage for
310 * that command. Otherwise, iterate over the entire command table and display
311 * a complete usage message.
314 usage(boolean_t requested)
316 FILE *fp = requested ? stdout : stderr;
318 if (current_command == NULL) {
321 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
323 gettext("where 'command' is one of the following:\n\n"));
325 for (i = 0; i < NCOMMAND; i++) {
326 if (command_table[i].name == NULL)
327 (void) fprintf(fp, "\n");
329 (void) fprintf(fp, "%s",
330 get_usage(command_table[i].usage));
333 (void) fprintf(fp, gettext("usage:\n"));
334 (void) fprintf(fp, "%s", get_usage(current_command->usage));
337 if (current_command != NULL &&
338 ((strcmp(current_command->name, "set") == 0) ||
339 (strcmp(current_command->name, "get") == 0) ||
340 (strcmp(current_command->name, "list") == 0))) {
343 gettext("\nthe following properties are supported:\n"));
345 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
346 "PROPERTY", "EDIT", "VALUES");
348 /* Iterate over all properties */
349 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
352 (void) fprintf(fp, "\t%-15s ", "feature@...");
353 (void) fprintf(fp, "YES disabled | enabled | active\n");
355 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
356 "appended with a feature name.\nSee zpool-features(5).\n"));
360 * See comments at end of main().
362 if (getenv("ZFS_ABORT") != NULL) {
363 (void) printf("dumping core by request\n");
367 exit(requested ? 0 : 2);
371 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
372 boolean_t print_logs)
379 (void) printf("\t%*s%s\n", indent, "", name);
381 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
382 &child, &children) != 0)
385 for (c = 0; c < children; c++) {
386 uint64_t is_log = B_FALSE;
388 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
390 if ((is_log && !print_logs) || (!is_log && print_logs))
393 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
394 print_vdev_tree(zhp, vname, child[c], indent + 2,
401 prop_list_contains_feature(nvlist_t *proplist)
404 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
405 nvp = nvlist_next_nvpair(proplist, nvp)) {
406 if (zpool_prop_feature(nvpair_name(nvp)))
413 * Add a property pair (name, string-value) into a property nvlist.
416 add_prop_list(const char *propname, char *propval, nvlist_t **props,
419 zpool_prop_t prop = ZPROP_INVAL;
425 if (*props == NULL &&
426 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
427 (void) fprintf(stderr,
428 gettext("internal error: out of memory\n"));
435 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
437 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
438 !zpool_prop_feature(propname)) {
439 (void) fprintf(stderr, gettext("property '%s' is "
440 "not a valid pool property\n"), propname);
445 * feature@ properties and version should not be specified
448 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
449 nvlist_exists(proplist, vname)) ||
450 (prop == ZPOOL_PROP_VERSION &&
451 prop_list_contains_feature(proplist))) {
452 (void) fprintf(stderr, gettext("'feature@' and "
453 "'version' properties cannot be specified "
459 if (zpool_prop_feature(propname))
462 normnm = zpool_prop_to_name(prop);
464 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
465 normnm = zfs_prop_to_name(fprop);
471 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
472 prop != ZPOOL_PROP_CACHEFILE) {
473 (void) fprintf(stderr, gettext("property '%s' "
474 "specified multiple times\n"), propname);
478 if (nvlist_add_string(proplist, normnm, propval) != 0) {
479 (void) fprintf(stderr, gettext("internal "
480 "error: out of memory\n"));
488 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
490 * -f Force addition of devices, even if they appear in use
491 * -n Do not add the devices, but display the resulting layout if
492 * they were to be added.
493 * -o Set property=value.
495 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
496 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
500 zpool_do_add(int argc, char **argv)
502 boolean_t force = B_FALSE;
503 boolean_t dryrun = B_FALSE;
510 nvlist_t *props = NULL;
514 while ((c = getopt(argc, argv, "fno:")) != -1) {
523 if ((propval = strchr(optarg, '=')) == NULL) {
524 (void) fprintf(stderr, gettext("missing "
525 "'=' for -o option\n"));
531 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
532 (add_prop_list(optarg, propval, &props, B_TRUE)))
536 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
545 /* get pool name and check number of arguments */
547 (void) fprintf(stderr, gettext("missing pool name argument\n"));
551 (void) fprintf(stderr, gettext("missing vdev specification\n"));
560 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
563 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
564 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
570 /* pass off to get_vdev_spec for processing */
571 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
573 if (nvroot == NULL) {
579 nvlist_t *poolnvroot;
581 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
584 (void) printf(gettext("would update '%s' to the following "
585 "configuration:\n"), zpool_get_name(zhp));
587 /* print original main pool and new tree */
588 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
589 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
591 /* Do the same for the logs */
592 if (num_logs(poolnvroot) > 0) {
593 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
594 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
595 } else if (num_logs(nvroot) > 0) {
596 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
601 ret = (zpool_add(zhp, nvroot) != 0);
612 * zpool remove <pool> <vdev> ...
614 * Removes the given vdev from the pool. Currently, this supports removing
615 * spares, cache, and log devices from the pool.
618 zpool_do_remove(int argc, char **argv)
627 /* get pool name and check number of arguments */
629 (void) fprintf(stderr, gettext("missing pool name argument\n"));
633 (void) fprintf(stderr, gettext("missing device\n"));
639 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
642 for (i = 1; i < argc; i++) {
643 if (zpool_vdev_remove(zhp, argv[i]) != 0)
651 * zpool labelclear <vdev>
653 * Verifies that the vdev is not active and zeros out the label information
657 zpool_do_labelclear(int argc, char **argv)
660 int c, fd = -1, ret = 0;
662 boolean_t inuse = B_FALSE;
663 boolean_t force = B_FALSE;
666 while ((c = getopt(argc, argv, "f")) != -1) {
672 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
683 (void) fprintf(stderr, gettext("missing vdev device name\n"));
688 if ((fd = open(vdev, O_RDWR)) < 0) {
689 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
694 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
698 (void) fprintf(stderr,
699 gettext("Unable to determine pool state for %s\n"
700 "Use -f to force the clearing any label data\n"), vdev);
708 case POOL_STATE_ACTIVE:
709 case POOL_STATE_SPARE:
710 case POOL_STATE_L2CACHE:
711 (void) fprintf(stderr,
712 gettext("labelclear operation failed.\n"
713 "\tVdev %s is a member (%s), of pool \"%s\".\n"
714 "\tTo remove label information from this device, "
715 "export or destroy\n\tthe pool, or remove %s from "
716 "the configuration of this pool\n\tand retry the "
717 "labelclear operation.\n"),
718 vdev, zpool_pool_state_to_name(state), name, vdev);
722 case POOL_STATE_EXPORTED:
726 (void) fprintf(stderr,
727 gettext("labelclear operation failed.\n\tVdev "
728 "%s is a member of the exported pool \"%s\".\n"
729 "\tUse \"zpool labelclear -f %s\" to force the "
730 "removal of label\n\tinformation.\n"),
735 case POOL_STATE_POTENTIALLY_ACTIVE:
739 (void) fprintf(stderr,
740 gettext("labelclear operation failed.\n"
741 "\tVdev %s is a member of the pool \"%s\".\n"
742 "\tThis pool is unknown to this system, but may "
743 "be active on\n\tanother system. Use "
744 "\'zpool labelclear -f %s\' to force the\n"
745 "\tremoval of label information.\n"),
750 case POOL_STATE_DESTROYED:
751 /* inuse should never be set for a destroyed pool... */
757 if (zpool_clear_label(fd) != 0) {
758 (void) fprintf(stderr,
759 gettext("Label clear failed on vdev %s\n"), vdev);
772 * zpool create [-fnd] [-o property=value] ...
773 * [-O file-system-property=value] ...
774 * [-R root] [-m mountpoint] <pool> <dev> ...
776 * -f Force creation, even if devices appear in use
777 * -n Do not create the pool, but display the resulting layout if it
778 * were to be created.
779 * -R Create a pool under an alternate root
780 * -m Set default mountpoint for the root dataset. By default it's
782 * -o Set property=value.
783 * -d Don't automatically enable all supported pool features
784 * (individual features can be enabled with -o).
785 * -O Set fsproperty=value in the pool's root file system
787 * Creates the named pool according to the given vdev specification. The
788 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
789 * we get the nvlist back from get_vdev_spec(), we either print out the contents
790 * (if '-n' was specified), or pass it to libzfs to do the creation.
793 zpool_do_create(int argc, char **argv)
795 boolean_t force = B_FALSE;
796 boolean_t dryrun = B_FALSE;
797 boolean_t enable_all_pool_feat = B_TRUE;
799 nvlist_t *nvroot = NULL;
802 char *altroot = NULL;
803 char *mountpoint = NULL;
804 nvlist_t *fsprops = NULL;
805 nvlist_t *props = NULL;
809 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
818 enable_all_pool_feat = B_FALSE;
822 if (add_prop_list(zpool_prop_to_name(
823 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
825 if (nvlist_lookup_string(props,
826 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
829 if (add_prop_list(zpool_prop_to_name(
830 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
837 if ((propval = strchr(optarg, '=')) == NULL) {
838 (void) fprintf(stderr, gettext("missing "
839 "'=' for -o option\n"));
845 if (add_prop_list(optarg, propval, &props, B_TRUE))
849 * If the user is creating a pool that doesn't support
850 * feature flags, don't enable any features.
852 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
856 ver = strtoull(propval, &end, 10);
858 ver < SPA_VERSION_FEATURES) {
859 enable_all_pool_feat = B_FALSE;
864 if ((propval = strchr(optarg, '=')) == NULL) {
865 (void) fprintf(stderr, gettext("missing "
866 "'=' for -O option\n"));
872 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
876 (void) fprintf(stderr, gettext("missing argument for "
877 "'%c' option\n"), optopt);
880 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
889 /* get pool name and check number of arguments */
891 (void) fprintf(stderr, gettext("missing pool name argument\n"));
895 (void) fprintf(stderr, gettext("missing vdev specification\n"));
902 * As a special case, check for use of '/' in the name, and direct the
903 * user to use 'zfs create' instead.
905 if (strchr(poolname, '/') != NULL) {
906 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
907 "character '/' in pool name\n"), poolname);
908 (void) fprintf(stderr, gettext("use 'zfs create' to "
909 "create a dataset\n"));
913 /* pass off to get_vdev_spec for bulk processing */
914 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
919 /* make_root_vdev() allows 0 toplevel children if there are spares */
920 if (!zfs_allocatable_devs(nvroot)) {
921 (void) fprintf(stderr, gettext("invalid vdev "
922 "specification: at least one toplevel vdev must be "
927 if (altroot != NULL && altroot[0] != '/') {
928 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
929 "must be an absolute path\n"), altroot);
934 * Check the validity of the mountpoint and direct the user to use the
935 * '-m' mountpoint option if it looks like its in use.
937 if (mountpoint == NULL ||
938 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
939 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
940 char buf[MAXPATHLEN];
943 if (mountpoint && mountpoint[0] != '/') {
944 (void) fprintf(stderr, gettext("invalid mountpoint "
945 "'%s': must be an absolute path, 'legacy', or "
946 "'none'\n"), mountpoint);
950 if (mountpoint == NULL) {
952 (void) snprintf(buf, sizeof (buf), "%s/%s",
955 (void) snprintf(buf, sizeof (buf), "/%s",
959 (void) snprintf(buf, sizeof (buf), "%s%s",
960 altroot, mountpoint);
962 (void) snprintf(buf, sizeof (buf), "%s",
966 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
967 (void) fprintf(stderr, gettext("mountpoint '%s' : "
968 "%s\n"), buf, strerror(errno));
969 (void) fprintf(stderr, gettext("use '-m' "
970 "option to provide a different default\n"));
975 while (count < 3 && readdir(dirp) != NULL)
977 (void) closedir(dirp);
980 (void) fprintf(stderr, gettext("mountpoint "
981 "'%s' exists and is not empty\n"), buf);
982 (void) fprintf(stderr, gettext("use '-m' "
983 "option to provide a "
984 "different default\n"));
992 * For a dry run invocation, print out a basic message and run
993 * through all the vdevs in the list and print out in an
994 * appropriate hierarchy.
996 (void) printf(gettext("would create '%s' with the "
997 "following layout:\n\n"), poolname);
999 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1000 if (num_logs(nvroot) > 0)
1001 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1006 * Hand off to libzfs.
1008 if (enable_all_pool_feat) {
1010 for (i = 0; i < SPA_FEATURES; i++) {
1011 char propname[MAXPATHLEN];
1012 zfeature_info_t *feat = &spa_feature_table[i];
1014 (void) snprintf(propname, sizeof (propname),
1015 "feature@%s", feat->fi_uname);
1018 * Skip feature if user specified it manually
1019 * on the command line.
1021 if (nvlist_exists(props, propname))
1024 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
1025 &props, B_TRUE) != 0)
1029 if (zpool_create(g_zfs, poolname,
1030 nvroot, props, fsprops) == 0) {
1031 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1032 ZFS_TYPE_FILESYSTEM);
1034 if (mountpoint != NULL)
1035 verify(zfs_prop_set(pool,
1037 ZFS_PROP_MOUNTPOINT),
1039 if (zfs_mount(pool, NULL, 0) == 0)
1040 ret = zfs_shareall(pool);
1043 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1044 (void) fprintf(stderr, gettext("pool name may have "
1050 nvlist_free(nvroot);
1051 nvlist_free(fsprops);
1055 nvlist_free(fsprops);
1062 * zpool destroy <pool>
1064 * -f Forcefully unmount any datasets
1066 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1069 zpool_do_destroy(int argc, char **argv)
1071 boolean_t force = B_FALSE;
1074 zpool_handle_t *zhp;
1078 while ((c = getopt(argc, argv, "f")) != -1) {
1084 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1093 /* check arguments */
1095 (void) fprintf(stderr, gettext("missing pool argument\n"));
1099 (void) fprintf(stderr, gettext("too many arguments\n"));
1105 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1107 * As a special case, check for use of '/' in the name, and
1108 * direct the user to use 'zfs destroy' instead.
1110 if (strchr(pool, '/') != NULL)
1111 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1112 "destroy a dataset\n"));
1116 if (zpool_disable_datasets(zhp, force) != 0) {
1117 (void) fprintf(stderr, gettext("could not destroy '%s': "
1118 "could not unmount datasets\n"), zpool_get_name(zhp));
1122 ret = (zpool_destroy(zhp) != 0);
1130 * zpool export [-f] <pool> ...
1132 * -f Forcefully unmount datasets
1134 * Export the given pools. By default, the command will attempt to cleanly
1135 * unmount any active datasets within the pool. If the '-f' flag is specified,
1136 * then the datasets will be forcefully unmounted.
1139 zpool_do_export(int argc, char **argv)
1141 boolean_t force = B_FALSE;
1142 boolean_t hardforce = B_FALSE;
1144 zpool_handle_t *zhp;
1149 while ((c = getopt(argc, argv, "fF")) != -1) {
1158 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1167 /* check arguments */
1169 (void) fprintf(stderr, gettext("missing pool argument\n"));
1174 for (i = 0; i < argc; i++) {
1175 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1180 if (zpool_disable_datasets(zhp, force) != 0) {
1187 if (zpool_export_force(zhp) != 0)
1189 } else if (zpool_export(zhp, force) != 0) {
1200 * Given a vdev configuration, determine the maximum width needed for the device
1204 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1206 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1211 if (strlen(name) + depth > max)
1212 max = strlen(name) + depth;
1216 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1217 &child, &children) == 0) {
1218 for (c = 0; c < children; c++)
1219 if ((ret = max_width(zhp, child[c], depth + 2,
1224 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1225 &child, &children) == 0) {
1226 for (c = 0; c < children; c++)
1227 if ((ret = max_width(zhp, child[c], depth + 2,
1232 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1233 &child, &children) == 0) {
1234 for (c = 0; c < children; c++)
1235 if ((ret = max_width(zhp, child[c], depth + 2,
1244 typedef struct spare_cbdata {
1246 zpool_handle_t *cb_zhp;
1250 find_vdev(nvlist_t *nv, uint64_t search)
1256 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1260 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1261 &child, &children) == 0) {
1262 for (c = 0; c < children; c++)
1263 if (find_vdev(child[c], search))
1271 find_spare(zpool_handle_t *zhp, void *data)
1273 spare_cbdata_t *cbp = data;
1274 nvlist_t *config, *nvroot;
1276 config = zpool_get_config(zhp, NULL);
1277 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1280 if (find_vdev(nvroot, cbp->cb_guid)) {
1290 * Print out configuration state as requested by status_callback.
1293 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1294 int namewidth, int depth, boolean_t isspare)
1298 pool_scan_stat_t *ps = NULL;
1300 char rbuf[6], wbuf[6], cbuf[6];
1302 uint64_t notpresent;
1306 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1307 &child, &children) != 0)
1310 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1311 (uint64_t **)&vs, &c) == 0);
1313 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1316 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1319 if (vs->vs_aux == VDEV_AUX_SPARED)
1321 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1325 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1329 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1330 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1331 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1332 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1335 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1336 ¬present) == 0) {
1338 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1339 (void) printf(" was %s", path);
1340 } else if (vs->vs_aux != 0) {
1343 switch (vs->vs_aux) {
1344 case VDEV_AUX_OPEN_FAILED:
1345 (void) printf(gettext("cannot open"));
1348 case VDEV_AUX_BAD_GUID_SUM:
1349 (void) printf(gettext("missing device"));
1352 case VDEV_AUX_NO_REPLICAS:
1353 (void) printf(gettext("insufficient replicas"));
1356 case VDEV_AUX_VERSION_NEWER:
1357 (void) printf(gettext("newer version"));
1360 case VDEV_AUX_UNSUP_FEAT:
1361 (void) printf(gettext("unsupported feature(s)"));
1364 case VDEV_AUX_SPARED:
1365 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1367 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1368 if (strcmp(zpool_get_name(cb.cb_zhp),
1369 zpool_get_name(zhp)) == 0)
1370 (void) printf(gettext("currently in "
1373 (void) printf(gettext("in use by "
1375 zpool_get_name(cb.cb_zhp));
1376 zpool_close(cb.cb_zhp);
1378 (void) printf(gettext("currently in use"));
1382 case VDEV_AUX_ERR_EXCEEDED:
1383 (void) printf(gettext("too many errors"));
1386 case VDEV_AUX_IO_FAILURE:
1387 (void) printf(gettext("experienced I/O failures"));
1390 case VDEV_AUX_BAD_LOG:
1391 (void) printf(gettext("bad intent log"));
1394 case VDEV_AUX_EXTERNAL:
1395 (void) printf(gettext("external device fault"));
1398 case VDEV_AUX_SPLIT_POOL:
1399 (void) printf(gettext("split into new pool"));
1403 (void) printf(gettext("corrupted data"));
1408 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1409 (uint64_t **)&ps, &c);
1411 if (ps && ps->pss_state == DSS_SCANNING &&
1412 vs->vs_scan_processed != 0 && children == 0) {
1413 (void) printf(gettext(" (%s)"),
1414 (ps->pss_func == POOL_SCAN_RESILVER) ?
1415 "resilvering" : "repairing");
1418 (void) printf("\n");
1420 for (c = 0; c < children; c++) {
1421 uint64_t islog = B_FALSE, ishole = B_FALSE;
1423 /* Don't print logs or holes here */
1424 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1426 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1428 if (islog || ishole)
1430 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1431 print_status_config(zhp, vname, child[c],
1432 namewidth, depth + 2, isspare);
1439 * Print the configuration of an exported pool. Iterate over all vdevs in the
1440 * pool, printing out the name and status for each one.
1443 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1450 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1451 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1452 strcmp(type, VDEV_TYPE_HOLE) == 0)
1455 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1456 (uint64_t **)&vs, &c) == 0);
1458 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1459 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1461 if (vs->vs_aux != 0) {
1464 switch (vs->vs_aux) {
1465 case VDEV_AUX_OPEN_FAILED:
1466 (void) printf(gettext("cannot open"));
1469 case VDEV_AUX_BAD_GUID_SUM:
1470 (void) printf(gettext("missing device"));
1473 case VDEV_AUX_NO_REPLICAS:
1474 (void) printf(gettext("insufficient replicas"));
1477 case VDEV_AUX_VERSION_NEWER:
1478 (void) printf(gettext("newer version"));
1481 case VDEV_AUX_UNSUP_FEAT:
1482 (void) printf(gettext("unsupported feature(s)"));
1485 case VDEV_AUX_ERR_EXCEEDED:
1486 (void) printf(gettext("too many errors"));
1490 (void) printf(gettext("corrupted data"));
1494 (void) printf("\n");
1496 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1497 &child, &children) != 0)
1500 for (c = 0; c < children; c++) {
1501 uint64_t is_log = B_FALSE;
1503 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1508 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1509 print_import_config(vname, child[c], namewidth, depth + 2);
1513 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1514 &child, &children) == 0) {
1515 (void) printf(gettext("\tcache\n"));
1516 for (c = 0; c < children; c++) {
1517 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1518 (void) printf("\t %s\n", vname);
1523 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1524 &child, &children) == 0) {
1525 (void) printf(gettext("\tspares\n"));
1526 for (c = 0; c < children; c++) {
1527 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1528 (void) printf("\t %s\n", vname);
1536 * Logs are recorded as top level vdevs in the main pool child array
1537 * but with "is_log" set to 1. We use either print_status_config() or
1538 * print_import_config() to print the top level logs then any log
1539 * children (eg mirrored slogs) are printed recursively - which
1540 * works because only the top level vdev is marked "is_log"
1543 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1548 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1552 (void) printf(gettext("\tlogs\n"));
1554 for (c = 0; c < children; c++) {
1555 uint64_t is_log = B_FALSE;
1558 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1562 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1564 print_status_config(zhp, name, child[c], namewidth,
1567 print_import_config(name, child[c], namewidth, 2);
1573 * Display the status for the given pool.
1576 show_import(nvlist_t *config)
1578 uint64_t pool_state;
1590 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1592 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1594 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1596 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1599 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1600 (uint64_t **)&vs, &vsc) == 0);
1601 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1603 reason = zpool_import_status(config, &msgid);
1605 (void) printf(gettext(" pool: %s\n"), name);
1606 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1607 (void) printf(gettext(" state: %s"), health);
1608 if (pool_state == POOL_STATE_DESTROYED)
1609 (void) printf(gettext(" (DESTROYED)"));
1610 (void) printf("\n");
1613 case ZPOOL_STATUS_MISSING_DEV_R:
1614 case ZPOOL_STATUS_MISSING_DEV_NR:
1615 case ZPOOL_STATUS_BAD_GUID_SUM:
1616 (void) printf(gettext(" status: One or more devices are "
1617 "missing from the system.\n"));
1620 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1621 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1622 (void) printf(gettext(" status: One or more devices contains "
1623 "corrupted data.\n"));
1626 case ZPOOL_STATUS_CORRUPT_DATA:
1628 gettext(" status: The pool data is corrupted.\n"));
1631 case ZPOOL_STATUS_OFFLINE_DEV:
1632 (void) printf(gettext(" status: One or more devices "
1633 "are offlined.\n"));
1636 case ZPOOL_STATUS_CORRUPT_POOL:
1637 (void) printf(gettext(" status: The pool metadata is "
1641 case ZPOOL_STATUS_VERSION_OLDER:
1642 (void) printf(gettext(" status: The pool is formatted using a "
1643 "legacy on-disk version.\n"));
1646 case ZPOOL_STATUS_VERSION_NEWER:
1647 (void) printf(gettext(" status: The pool is formatted using an "
1648 "incompatible version.\n"));
1651 case ZPOOL_STATUS_FEAT_DISABLED:
1652 (void) printf(gettext(" status: Some supported features are "
1653 "not enabled on the pool.\n"));
1656 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1657 (void) printf(gettext("status: The pool uses the following "
1658 "feature(s) not supported on this sytem:\n"));
1659 zpool_print_unsup_feat(config);
1662 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1663 (void) printf(gettext("status: The pool can only be accessed "
1664 "in read-only mode on this system. It\n\tcannot be "
1665 "accessed in read-write mode because it uses the "
1666 "following\n\tfeature(s) not supported on this system:\n"));
1667 zpool_print_unsup_feat(config);
1670 case ZPOOL_STATUS_HOSTID_MISMATCH:
1671 (void) printf(gettext(" status: The pool was last accessed by "
1672 "another system.\n"));
1675 case ZPOOL_STATUS_FAULTED_DEV_R:
1676 case ZPOOL_STATUS_FAULTED_DEV_NR:
1677 (void) printf(gettext(" status: One or more devices are "
1681 case ZPOOL_STATUS_BAD_LOG:
1682 (void) printf(gettext(" status: An intent log record cannot be "
1686 case ZPOOL_STATUS_RESILVERING:
1687 (void) printf(gettext(" status: One or more devices were being "
1693 * No other status can be seen when importing pools.
1695 assert(reason == ZPOOL_STATUS_OK);
1699 * Print out an action according to the overall state of the pool.
1701 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1702 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1703 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1704 (void) printf(gettext(" action: The pool can be "
1705 "imported using its name or numeric identifier, "
1706 "though\n\tsome features will not be available "
1707 "without an explicit 'zpool upgrade'.\n"));
1708 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1709 (void) printf(gettext(" action: The pool can be "
1710 "imported using its name or numeric "
1711 "identifier and\n\tthe '-f' flag.\n"));
1713 (void) printf(gettext(" action: The pool can be "
1714 "imported using its name or numeric "
1717 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1718 (void) printf(gettext(" action: The pool can be imported "
1719 "despite missing or damaged devices. The\n\tfault "
1720 "tolerance of the pool may be compromised if imported.\n"));
1723 case ZPOOL_STATUS_VERSION_NEWER:
1724 (void) printf(gettext(" action: The pool cannot be "
1725 "imported. Access the pool on a system running "
1726 "newer\n\tsoftware, or recreate the pool from "
1729 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1730 (void) printf(gettext("action: The pool cannot be "
1731 "imported. Access the pool on a system that "
1732 "supports\n\tthe required feature(s), or recreate "
1733 "the pool from backup.\n"));
1735 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1736 (void) printf(gettext("action: The pool cannot be "
1737 "imported in read-write mode. Import the pool "
1739 "\t\"-o readonly=on\", access the pool on a system "
1740 "that supports the\n\trequired feature(s), or "
1741 "recreate the pool from backup.\n"));
1743 case ZPOOL_STATUS_MISSING_DEV_R:
1744 case ZPOOL_STATUS_MISSING_DEV_NR:
1745 case ZPOOL_STATUS_BAD_GUID_SUM:
1746 (void) printf(gettext(" action: The pool cannot be "
1747 "imported. Attach the missing\n\tdevices and try "
1751 (void) printf(gettext(" action: The pool cannot be "
1752 "imported due to damaged devices or data.\n"));
1756 /* Print the comment attached to the pool. */
1757 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1758 (void) printf(gettext("comment: %s\n"), comment);
1761 * If the state is "closed" or "can't open", and the aux state
1762 * is "corrupt data":
1764 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1765 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1766 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1767 if (pool_state == POOL_STATE_DESTROYED)
1768 (void) printf(gettext("\tThe pool was destroyed, "
1769 "but can be imported using the '-Df' flags.\n"));
1770 else if (pool_state != POOL_STATE_EXPORTED)
1771 (void) printf(gettext("\tThe pool may be active on "
1772 "another system, but can be imported using\n\t"
1773 "the '-f' flag.\n"));
1777 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1780 (void) printf(gettext(" config:\n\n"));
1782 namewidth = max_width(NULL, nvroot, 0, 0);
1786 print_import_config(name, nvroot, namewidth, 0);
1787 if (num_logs(nvroot) > 0)
1788 print_logs(NULL, nvroot, namewidth, B_FALSE);
1790 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1791 (void) printf(gettext("\n\tAdditional devices are known to "
1792 "be part of this pool, though their\n\texact "
1793 "configuration cannot be determined.\n"));
1798 * Perform the import for the given configuration. This passes the heavy
1799 * lifting off to zpool_import_props(), and then mounts the datasets contained
1803 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1804 nvlist_t *props, int flags)
1806 zpool_handle_t *zhp;
1811 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1814 verify(nvlist_lookup_uint64(config,
1815 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1816 verify(nvlist_lookup_uint64(config,
1817 ZPOOL_CONFIG_VERSION, &version) == 0);
1818 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1819 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1820 "is formatted using an unsupported ZFS version\n"), name);
1822 } else if (state != POOL_STATE_EXPORTED &&
1823 !(flags & ZFS_IMPORT_ANY_HOST)) {
1826 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1828 unsigned long system_hostid = gethostid() & 0xffffffff;
1830 if ((unsigned long)hostid != system_hostid) {
1835 verify(nvlist_lookup_string(config,
1836 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1837 verify(nvlist_lookup_uint64(config,
1838 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1840 (void) fprintf(stderr, gettext("cannot import "
1841 "'%s': pool may be in use from other "
1842 "system, it was last accessed by %s "
1843 "(hostid: 0x%lx) on %s"), name, hostname,
1844 (unsigned long)hostid,
1845 asctime(localtime(&t)));
1846 (void) fprintf(stderr, gettext("use '-f' to "
1847 "import anyway\n"));
1851 (void) fprintf(stderr, gettext("cannot import '%s': "
1852 "pool may be in use from other system\n"), name);
1853 (void) fprintf(stderr, gettext("use '-f' to import "
1859 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1862 if (newname != NULL)
1863 name = (char *)newname;
1865 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1868 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1869 !(flags & ZFS_IMPORT_ONLY) &&
1870 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1880 * zpool import [-d dir] [-D]
1881 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1882 * [-d dir | -c cachefile] [-f] -a
1883 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1884 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1886 * -c Read pool information from a cachefile instead of searching
1889 * -d Scan in a specific directory, other than /dev/. More than
1890 * one directory can be specified using multiple '-d' options.
1892 * -D Scan for previously destroyed pools or import all or only
1893 * specified destroyed pools.
1895 * -R Temporarily import the pool, with all mountpoints relative to
1896 * the given root. The pool will remain exported when the machine
1899 * -V Import even in the presence of faulted vdevs. This is an
1900 * intentionally undocumented option for testing purposes, and
1901 * treats the pool configuration as complete, leaving any bad
1902 * vdevs in the FAULTED state. In other words, it does verbatim
1905 * -f Force import, even if it appears that the pool is active.
1907 * -F Attempt rewind if necessary.
1909 * -n See if rewind would work, but don't actually rewind.
1911 * -N Import the pool but don't mount datasets.
1913 * -T Specify a starting txg to use for import. This option is
1914 * intentionally undocumented option for testing purposes.
1916 * -a Import all pools found.
1918 * -o Set property=value and/or temporary mount options (without '=').
1920 * The import command scans for pools to import, and import pools based on pool
1921 * name and GUID. The pool can also be renamed as part of the import process.
1924 zpool_do_import(int argc, char **argv)
1926 char **searchdirs = NULL;
1927 char *env, *envdup = NULL;
1931 nvlist_t *pools = NULL;
1932 boolean_t do_all = B_FALSE;
1933 boolean_t do_destroyed = B_FALSE;
1934 char *mntopts = NULL;
1937 uint64_t searchguid = 0;
1938 char *searchname = NULL;
1940 nvlist_t *found_config;
1941 nvlist_t *policy = NULL;
1942 nvlist_t *props = NULL;
1944 int flags = ZFS_IMPORT_NORMAL;
1945 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1946 boolean_t dryrun = B_FALSE;
1947 boolean_t do_rewind = B_FALSE;
1948 boolean_t xtreme_rewind = B_FALSE;
1949 uint64_t pool_state, txg = -1ULL;
1950 char *cachefile = NULL;
1951 importargs_t idata = { 0 };
1955 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1964 if (searchdirs == NULL) {
1965 searchdirs = safe_malloc(sizeof (char *));
1967 char **tmp = safe_malloc((nsearch + 1) *
1969 bcopy(searchdirs, tmp, nsearch *
1974 searchdirs[nsearch++] = optarg;
1977 do_destroyed = B_TRUE;
1980 flags |= ZFS_IMPORT_ANY_HOST;
1986 flags |= ZFS_IMPORT_MISSING_LOG;
1992 flags |= ZFS_IMPORT_ONLY;
1995 if ((propval = strchr(optarg, '=')) != NULL) {
1998 if (add_prop_list(optarg, propval,
2006 if (add_prop_list(zpool_prop_to_name(
2007 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2009 if (nvlist_lookup_string(props,
2010 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2013 if (add_prop_list(zpool_prop_to_name(
2014 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2019 txg = strtoull(optarg, &endptr, 10);
2020 if (errno != 0 || *endptr != '\0') {
2021 (void) fprintf(stderr,
2022 gettext("invalid txg value\n"));
2025 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2028 flags |= ZFS_IMPORT_VERBATIM;
2031 xtreme_rewind = B_TRUE;
2034 (void) fprintf(stderr, gettext("missing argument for "
2035 "'%c' option\n"), optopt);
2039 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2048 if (cachefile && nsearch != 0) {
2049 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2053 if ((dryrun || xtreme_rewind) && !do_rewind) {
2054 (void) fprintf(stderr,
2055 gettext("-n or -X only meaningful with -F\n"));
2059 rewind_policy = ZPOOL_TRY_REWIND;
2061 rewind_policy = ZPOOL_DO_REWIND;
2063 rewind_policy |= ZPOOL_EXTREME_REWIND;
2065 /* In the future, we can capture further policy and include it here */
2066 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2067 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2068 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2071 /* check argument count */
2074 (void) fprintf(stderr, gettext("too many arguments\n"));
2079 (void) fprintf(stderr, gettext("too many arguments\n"));
2084 * Check for the SYS_CONFIG privilege. We do this explicitly
2085 * here because otherwise any attempt to discover pools will
2088 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2089 (void) fprintf(stderr, gettext("cannot "
2090 "discover pools: permission denied\n"));
2091 if (searchdirs != NULL)
2094 nvlist_free(policy);
2100 * Depending on the arguments given, we do one of the following:
2102 * <none> Iterate through all pools and display information about
2105 * -a Iterate through all pools and try to import each one.
2107 * <id> Find the pool that corresponds to the given GUID/pool
2108 * name and import that one.
2110 * -D Above options applies only to destroyed pools.
2116 searchguid = strtoull(argv[0], &endptr, 10);
2117 if (errno != 0 || *endptr != '\0')
2118 searchname = argv[0];
2119 found_config = NULL;
2122 * User specified a name or guid. Ensure it's unique.
2124 idata.unique = B_TRUE;
2128 * Check the environment for the preferred search path.
2130 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2133 envdup = strdup(env);
2135 dir = strtok(envdup, ":");
2136 while (dir != NULL) {
2137 if (searchdirs == NULL) {
2138 searchdirs = safe_malloc(sizeof (char *));
2140 char **tmp = safe_malloc((nsearch + 1) *
2142 bcopy(searchdirs, tmp, nsearch *
2147 searchdirs[nsearch++] = dir;
2148 dir = strtok(NULL, ":");
2152 idata.path = searchdirs;
2153 idata.paths = nsearch;
2154 idata.poolname = searchname;
2155 idata.guid = searchguid;
2156 idata.cachefile = cachefile;
2158 pools = zpool_search_import(g_zfs, &idata);
2160 if (pools != NULL && idata.exists &&
2161 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2162 (void) fprintf(stderr, gettext("cannot import '%s': "
2163 "a pool with that name already exists\n"),
2165 (void) fprintf(stderr, gettext("use the form '%s "
2166 "<pool | id> <newpool>' to give it a new name\n"),
2169 } else if (pools == NULL && idata.exists) {
2170 (void) fprintf(stderr, gettext("cannot import '%s': "
2171 "a pool with that name is already created/imported,\n"),
2173 (void) fprintf(stderr, gettext("and no additional pools "
2174 "with that name were found\n"));
2176 } else if (pools == NULL) {
2178 (void) fprintf(stderr, gettext("cannot import '%s': "
2179 "no such pool available\n"), argv[0]);
2185 if (searchdirs != NULL)
2189 nvlist_free(policy);
2194 * At this point we have a list of import candidate configs. Even if
2195 * we were searching by pool name or guid, we still need to
2196 * post-process the list to deal with pool state and possible
2202 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2204 verify(nvpair_value_nvlist(elem, &config) == 0);
2206 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2208 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2210 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2213 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2220 (void) printf("\n");
2223 err |= do_import(config, NULL, mntopts,
2226 show_import(config);
2228 } else if (searchname != NULL) {
2232 * We are searching for a pool based on name.
2234 verify(nvlist_lookup_string(config,
2235 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2237 if (strcmp(name, searchname) == 0) {
2238 if (found_config != NULL) {
2239 (void) fprintf(stderr, gettext(
2240 "cannot import '%s': more than "
2241 "one matching pool\n"), searchname);
2242 (void) fprintf(stderr, gettext(
2243 "import by numeric ID instead\n"));
2246 found_config = config;
2252 * Search for a pool by guid.
2254 verify(nvlist_lookup_uint64(config,
2255 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2257 if (guid == searchguid)
2258 found_config = config;
2263 * If we were searching for a specific pool, verify that we found a
2264 * pool, and then do the import.
2266 if (argc != 0 && err == 0) {
2267 if (found_config == NULL) {
2268 (void) fprintf(stderr, gettext("cannot import '%s': "
2269 "no such pool available\n"), argv[0]);
2272 err |= do_import(found_config, argc == 1 ? NULL :
2273 argv[1], mntopts, props, flags);
2278 * If we were just looking for pools, report an error if none were
2281 if (argc == 0 && first)
2282 (void) fprintf(stderr,
2283 gettext("no pools available to import\n"));
2288 nvlist_free(policy);
2289 if (searchdirs != NULL)
2294 return (err ? 1 : 0);
2297 typedef struct iostat_cbdata {
2298 boolean_t cb_verbose;
2301 zpool_list_t *cb_list;
2305 print_iostat_separator(iostat_cbdata_t *cb)
2309 for (i = 0; i < cb->cb_namewidth; i++)
2311 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2315 print_iostat_header(iostat_cbdata_t *cb)
2317 (void) printf("%*s capacity operations bandwidth\n",
2318 cb->cb_namewidth, "");
2319 (void) printf("%-*s alloc free read write read write\n",
2320 cb->cb_namewidth, "pool");
2321 print_iostat_separator(cb);
2325 * Display a single statistic.
2328 print_one_stat(uint64_t value)
2332 zfs_nicenum(value, buf, sizeof (buf));
2333 (void) printf(" %5s", buf);
2337 * Print out all the statistics for the given vdev. This can either be the
2338 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2339 * is a verbose output, and we don't want to display the toplevel pool stats.
2342 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2343 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2345 nvlist_t **oldchild, **newchild;
2347 vdev_stat_t *oldvs, *newvs;
2348 vdev_stat_t zerovs = { 0 };
2353 if (oldnv != NULL) {
2354 verify(nvlist_lookup_uint64_array(oldnv,
2355 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2360 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2361 (uint64_t **)&newvs, &c) == 0);
2363 if (strlen(name) + depth > cb->cb_namewidth)
2364 (void) printf("%*s%s", depth, "", name);
2366 (void) printf("%*s%s%*s", depth, "", name,
2367 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2369 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2374 scale = (double)NANOSEC / tdelta;
2376 /* only toplevel vdevs have capacity stats */
2377 if (newvs->vs_space == 0) {
2378 (void) printf(" - -");
2380 print_one_stat(newvs->vs_alloc);
2381 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2384 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2385 oldvs->vs_ops[ZIO_TYPE_READ])));
2387 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2388 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2390 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2391 oldvs->vs_bytes[ZIO_TYPE_READ])));
2393 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2394 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2396 (void) printf("\n");
2398 if (!cb->cb_verbose)
2401 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2402 &newchild, &children) != 0)
2405 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2406 &oldchild, &c) != 0)
2409 for (c = 0; c < children; c++) {
2410 uint64_t ishole = B_FALSE, islog = B_FALSE;
2412 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2415 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2418 if (ishole || islog)
2421 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2422 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2423 newchild[c], cb, depth + 2);
2428 * Log device section
2431 if (num_logs(newnv) > 0) {
2432 (void) printf("%-*s - - - - - "
2433 "-\n", cb->cb_namewidth, "logs");
2435 for (c = 0; c < children; c++) {
2436 uint64_t islog = B_FALSE;
2437 (void) nvlist_lookup_uint64(newchild[c],
2438 ZPOOL_CONFIG_IS_LOG, &islog);
2441 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2443 print_vdev_stats(zhp, vname, oldnv ?
2444 oldchild[c] : NULL, newchild[c],
2453 * Include level 2 ARC devices in iostat output
2455 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2456 &newchild, &children) != 0)
2459 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2460 &oldchild, &c) != 0)
2464 (void) printf("%-*s - - - - - "
2465 "-\n", cb->cb_namewidth, "cache");
2466 for (c = 0; c < children; c++) {
2467 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2469 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2470 newchild[c], cb, depth + 2);
2477 refresh_iostat(zpool_handle_t *zhp, void *data)
2479 iostat_cbdata_t *cb = data;
2483 * If the pool has disappeared, remove it from the list and continue.
2485 if (zpool_refresh_stats(zhp, &missing) != 0)
2489 pool_list_remove(cb->cb_list, zhp);
2495 * Callback to print out the iostats for the given pool.
2498 print_iostat(zpool_handle_t *zhp, void *data)
2500 iostat_cbdata_t *cb = data;
2501 nvlist_t *oldconfig, *newconfig;
2502 nvlist_t *oldnvroot, *newnvroot;
2504 newconfig = zpool_get_config(zhp, &oldconfig);
2506 if (cb->cb_iteration == 1)
2509 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2512 if (oldconfig == NULL)
2515 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2519 * Print out the statistics for the pool.
2521 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2524 print_iostat_separator(cb);
2536 if (isatty(STDOUT_FILENO)) {
2537 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2539 columns = ws.ws_col;
2548 get_namewidth(zpool_handle_t *zhp, void *data)
2550 iostat_cbdata_t *cb = data;
2551 nvlist_t *config, *nvroot;
2554 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2555 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2557 if (!cb->cb_verbose)
2558 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2560 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2565 * The width must be at least 10, but may be as large as the
2566 * column width - 42 so that we can still fit in one line.
2568 columns = get_columns();
2570 if (cb->cb_namewidth < 10)
2571 cb->cb_namewidth = 10;
2572 if (cb->cb_namewidth > columns - 42)
2573 cb->cb_namewidth = columns - 42;
2579 * Parse the input string, get the 'interval' and 'count' value if there is one.
2582 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2585 unsigned long interval = 0, count = 0;
2589 * Determine if the last argument is an integer or a pool name
2591 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2595 interval = strtoul(argv[argc - 1], &end, 10);
2597 if (*end == '\0' && errno == 0) {
2598 if (interval == 0) {
2599 (void) fprintf(stderr, gettext("interval "
2600 "cannot be zero\n"));
2604 * Ignore the last parameter
2609 * If this is not a valid number, just plow on. The
2610 * user will get a more informative error message later
2618 * If the last argument is also an integer, then we have both a count
2621 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2626 interval = strtoul(argv[argc - 1], &end, 10);
2628 if (*end == '\0' && errno == 0) {
2629 if (interval == 0) {
2630 (void) fprintf(stderr, gettext("interval "
2631 "cannot be zero\n"));
2636 * Ignore the last parameter
2650 get_timestamp_arg(char c)
2653 timestamp_fmt = UDATE;
2655 timestamp_fmt = DDATE;
2661 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2663 * -v Display statistics for individual vdevs
2664 * -T Display a timestamp in date(1) or Unix format
2666 * This command can be tricky because we want to be able to deal with pool
2667 * creation/destruction as well as vdev configuration changes. The bulk of this
2668 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2669 * on pool_list_update() to detect the addition of new pools. Configuration
2670 * changes are all handled within libzfs.
2673 zpool_do_iostat(int argc, char **argv)
2678 unsigned long interval = 0, count = 0;
2680 boolean_t verbose = B_FALSE;
2684 while ((c = getopt(argc, argv, "T:v")) != -1) {
2687 get_timestamp_arg(*optarg);
2693 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2702 get_interval_count(&argc, argv, &interval, &count);
2705 * Construct the list of all interesting pools.
2708 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2711 if (pool_list_count(list) == 0 && argc != 0) {
2712 pool_list_free(list);
2716 if (pool_list_count(list) == 0 && interval == 0) {
2717 pool_list_free(list);
2718 (void) fprintf(stderr, gettext("no pools available\n"));
2723 * Enter the main iostat loop.
2726 cb.cb_verbose = verbose;
2727 cb.cb_iteration = 0;
2728 cb.cb_namewidth = 0;
2731 pool_list_update(list);
2733 if ((npools = pool_list_count(list)) == 0)
2734 (void) fprintf(stderr, gettext("no pools available\n"));
2737 * Refresh all statistics. This is done as an
2738 * explicit step before calculating the maximum name
2739 * width, so that any * configuration changes are
2740 * properly accounted for.
2742 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2746 * Iterate over all pools to determine the maximum width
2747 * for the pool / device name column across all pools.
2749 cb.cb_namewidth = 0;
2750 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2753 if (timestamp_fmt != NODATE)
2754 print_timestamp(timestamp_fmt);
2757 * If it's the first time, or verbose mode, print the
2760 if (++cb.cb_iteration == 1 || verbose)
2761 print_iostat_header(&cb);
2763 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2766 * If there's more than one pool, and we're not in
2767 * verbose mode (which prints a separator for us),
2768 * then print a separator.
2770 if (npools > 1 && !verbose)
2771 print_iostat_separator(&cb);
2774 (void) printf("\n");
2778 * Flush the output so that redirection to a file isn't buffered
2781 (void) fflush(stdout);
2786 if (count != 0 && --count == 0)
2789 (void) sleep(interval);
2792 pool_list_free(list);
2797 typedef struct list_cbdata {
2798 boolean_t cb_verbose;
2800 boolean_t cb_scripted;
2801 zprop_list_t *cb_proplist;
2805 * Given a list of columns to display, output appropriate headers for each one.
2808 print_header(list_cbdata_t *cb)
2810 zprop_list_t *pl = cb->cb_proplist;
2811 char headerbuf[ZPOOL_MAXPROPLEN];
2813 boolean_t first = B_TRUE;
2814 boolean_t right_justify;
2817 for (; pl != NULL; pl = pl->pl_next) {
2818 width = pl->pl_width;
2819 if (first && cb->cb_verbose) {
2821 * Reset the width to accommodate the verbose listing
2824 width = cb->cb_namewidth;
2832 right_justify = B_FALSE;
2833 if (pl->pl_prop != ZPROP_INVAL) {
2834 header = zpool_prop_column_name(pl->pl_prop);
2835 right_justify = zpool_prop_align_right(pl->pl_prop);
2839 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2840 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2841 headerbuf[i] = '\0';
2845 if (pl->pl_next == NULL && !right_justify)
2846 (void) printf("%s", header);
2847 else if (right_justify)
2848 (void) printf("%*s", (int)width, header);
2850 (void) printf("%-*s", (int)width, header);
2853 (void) printf("\n");
2857 * Given a pool and a list of properties, print out all the properties according
2858 * to the described layout.
2861 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2863 zprop_list_t *pl = cb->cb_proplist;
2864 boolean_t first = B_TRUE;
2865 char property[ZPOOL_MAXPROPLEN];
2867 boolean_t right_justify;
2870 for (; pl != NULL; pl = pl->pl_next) {
2872 width = pl->pl_width;
2873 if (first && cb->cb_verbose) {
2875 * Reset the width to accommodate the verbose listing
2878 width = cb->cb_namewidth;
2882 if (cb->cb_scripted)
2883 (void) printf("\t");
2890 right_justify = B_FALSE;
2891 if (pl->pl_prop != ZPROP_INVAL) {
2892 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2893 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2895 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2896 sizeof (property), NULL) != 0)
2901 right_justify = zpool_prop_align_right(pl->pl_prop);
2902 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2903 zpool_prop_unsupported(pl->pl_user_prop)) &&
2904 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2905 sizeof (property)) == 0) {
2913 * If this is being called in scripted mode, or if this is the
2914 * last column and it is left-justified, don't include a width
2917 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2918 (void) printf("%s", propstr);
2919 else if (right_justify)
2920 (void) printf("%*s", (int)width, propstr);
2922 (void) printf("%-*s", (int)width, propstr);
2925 (void) printf("\n");
2929 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2933 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2935 zfs_nicenum(value, propval, sizeof (propval));
2937 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2938 (void) strlcpy(propval, "-", sizeof (propval));
2941 (void) printf("\t%s", propval);
2943 (void) printf(" %*s", (int)width, propval);
2947 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2948 list_cbdata_t *cb, int depth)
2954 boolean_t scripted = cb->cb_scripted;
2956 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2957 (uint64_t **)&vs, &c) == 0);
2961 (void) printf("\t%s", name);
2962 else if (strlen(name) + depth > cb->cb_namewidth)
2963 (void) printf("%*s%s", depth, "", name);
2965 (void) printf("%*s%s%*s", depth, "", name,
2966 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2968 /* only toplevel vdevs have capacity stats */
2969 if (vs->vs_space == 0) {
2971 (void) printf("\t-\t-\t-");
2973 (void) printf(" - - -");
2975 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2977 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2979 print_one_column(ZPOOL_PROP_FREE,
2980 vs->vs_space - vs->vs_alloc, scripted);
2982 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2984 (void) printf("\n");
2987 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2988 &child, &children) != 0)
2991 for (c = 0; c < children; c++) {
2992 uint64_t ishole = B_FALSE;
2994 if (nvlist_lookup_uint64(child[c],
2995 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2998 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2999 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3004 * Include level 2 ARC devices in iostat output
3006 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3007 &child, &children) != 0)
3011 (void) printf("%-*s - - - - - "
3012 "-\n", cb->cb_namewidth, "cache");
3013 for (c = 0; c < children; c++) {
3014 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3016 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3024 * Generic callback function to list a pool.
3027 list_callback(zpool_handle_t *zhp, void *data)
3029 list_cbdata_t *cbp = data;
3033 config = zpool_get_config(zhp, NULL);
3035 print_pool(zhp, cbp);
3036 if (!cbp->cb_verbose)
3039 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3041 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3047 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3049 * -H Scripted mode. Don't display headers, and separate properties
3051 * -o List of properties to display. Defaults to
3052 * "name,size,allocated,free,capacity,health,altroot"
3053 * -T Display a timestamp in date(1) or Unix format
3055 * List all pools in the system, whether or not they're healthy. Output space
3056 * statistics for each one, as well as health status summary.
3059 zpool_do_list(int argc, char **argv)
3063 list_cbdata_t cb = { 0 };
3064 static char default_props[] =
3065 "name,size,allocated,free,capacity,dedupratio,"
3067 char *props = default_props;
3068 unsigned long interval = 0, count = 0;
3070 boolean_t first = B_TRUE;
3073 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3076 cb.cb_scripted = B_TRUE;
3082 get_timestamp_arg(*optarg);
3085 cb.cb_verbose = B_TRUE;
3088 (void) fprintf(stderr, gettext("missing argument for "
3089 "'%c' option\n"), optopt);
3093 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3101 fprintf(stderr, "argc = %d\n", argc);
3102 for (c=0; c<argc; c++)
3103 fprintf(stderr, "argv[%d] = %s\n", argc, argv[c]);
3105 get_interval_count(&argc, argv, &interval, &count);
3107 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3110 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3113 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3114 (void) printf(gettext("no pools available\n"));
3115 zprop_free_list(cb.cb_proplist);
3120 pool_list_update(list);
3122 if (pool_list_count(list) == 0)
3125 if (timestamp_fmt != NODATE)
3126 print_timestamp(timestamp_fmt);
3128 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3132 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3137 if (count != 0 && --count == 0)
3140 (void) sleep(interval);
3143 zprop_free_list(cb.cb_proplist);
3148 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3150 boolean_t force = B_FALSE;
3153 char *poolname, *old_disk, *new_disk;
3154 zpool_handle_t *zhp;
3155 nvlist_t *props = NULL;
3160 while ((c = getopt(argc, argv, "fo:")) != -1) {
3166 if ((propval = strchr(optarg, '=')) == NULL) {
3167 (void) fprintf(stderr, gettext("missing "
3168 "'=' for -o option\n"));
3174 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3175 (add_prop_list(optarg, propval, &props, B_TRUE)))
3179 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3188 /* get pool name and check number of arguments */
3190 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3197 (void) fprintf(stderr,
3198 gettext("missing <device> specification\n"));
3206 (void) fprintf(stderr,
3207 gettext("missing <new_device> specification\n"));
3210 new_disk = old_disk;
3220 (void) fprintf(stderr, gettext("too many arguments\n"));
3224 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3227 if (zpool_get_config(zhp, NULL) == NULL) {
3228 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3234 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3236 if (nvroot == NULL) {
3241 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3243 nvlist_free(nvroot);
3250 * zpool replace [-f] <pool> <device> <new_device>
3252 * -f Force attach, even if <new_device> appears to be in use.
3254 * Replace <device> with <new_device>.
3258 zpool_do_replace(int argc, char **argv)
3260 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3264 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3266 * -f Force attach, even if <new_device> appears to be in use.
3267 * -o Set property=value.
3269 * Attach <new_device> to the mirror containing <device>. If <device> is not
3270 * part of a mirror, then <device> will be transformed into a mirror of
3271 * <device> and <new_device>. In either case, <new_device> will begin life
3272 * with a DTL of [0, now], and will immediately begin to resilver itself.
3275 zpool_do_attach(int argc, char **argv)
3277 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3281 * zpool detach [-f] <pool> <device>
3283 * -f Force detach of <device>, even if DTLs argue against it
3284 * (not supported yet)
3286 * Detach a device from a mirror. The operation will be refused if <device>
3287 * is the last device in the mirror, or if the DTLs indicate that this device
3288 * has the only valid copy of some data.
3292 zpool_do_detach(int argc, char **argv)
3295 char *poolname, *path;
3296 zpool_handle_t *zhp;
3300 while ((c = getopt(argc, argv, "f")) != -1) {
3304 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3313 /* get pool name and check number of arguments */
3315 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3320 (void) fprintf(stderr,
3321 gettext("missing <device> specification\n"));
3328 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3331 ret = zpool_vdev_detach(zhp, path);
3339 * zpool split [-n] [-o prop=val] ...
3341 * [-R altroot] <pool> <newpool> [<device> ...]
3343 * -n Do not split the pool, but display the resulting layout if
3344 * it were to be split.
3345 * -o Set property=value, or set mount options.
3346 * -R Mount the split-off pool under an alternate root.
3348 * Splits the named pool and gives it the new pool name. Devices to be split
3349 * off may be listed, provided that no more than one device is specified
3350 * per top-level vdev mirror. The newly split pool is left in an exported
3351 * state unless -R is specified.
3353 * Restrictions: the top-level of the pool pool must only be made up of
3354 * mirrors; all devices in the pool must be healthy; no device may be
3355 * undergoing a resilvering operation.
3358 zpool_do_split(int argc, char **argv)
3360 char *srcpool, *newpool, *propval;
3361 char *mntopts = NULL;
3364 zpool_handle_t *zhp;
3365 nvlist_t *config, *props = NULL;
3367 flags.dryrun = B_FALSE;
3368 flags.import = B_FALSE;
3371 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3374 flags.import = B_TRUE;
3376 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3377 &props, B_TRUE) != 0) {
3384 flags.dryrun = B_TRUE;
3387 if ((propval = strchr(optarg, '=')) != NULL) {
3390 if (add_prop_list(optarg, propval,
3391 &props, B_TRUE) != 0) {
3401 (void) fprintf(stderr, gettext("missing argument for "
3402 "'%c' option\n"), optopt);
3406 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3413 if (!flags.import && mntopts != NULL) {
3414 (void) fprintf(stderr, gettext("setting mntopts is only "
3415 "valid when importing the pool\n"));
3423 (void) fprintf(stderr, gettext("Missing pool name\n"));
3427 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3437 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3440 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3441 if (config == NULL) {
3445 (void) printf(gettext("would create '%s' with the "
3446 "following layout:\n\n"), newpool);
3447 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3449 nvlist_free(config);
3454 if (ret != 0 || flags.dryrun || !flags.import)
3458 * The split was successful. Now we need to open the new
3459 * pool and import it.
3461 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3463 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3464 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3466 (void) fprintf(stderr, gettext("Split was successful, but "
3467 "the datasets could not all be mounted\n"));
3468 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3469 "different altroot\n"), "zpool import");
3479 * zpool online <pool> <device> ...
3482 zpool_do_online(int argc, char **argv)
3486 zpool_handle_t *zhp;
3488 vdev_state_t newstate;
3492 while ((c = getopt(argc, argv, "et")) != -1) {
3495 flags |= ZFS_ONLINE_EXPAND;
3499 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3508 /* get pool name and check number of arguments */
3510 (void) fprintf(stderr, gettext("missing pool name\n"));
3514 (void) fprintf(stderr, gettext("missing device name\n"));
3520 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3523 for (i = 1; i < argc; i++) {
3524 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3525 if (newstate != VDEV_STATE_HEALTHY) {
3526 (void) printf(gettext("warning: device '%s' "
3527 "onlined, but remains in faulted state\n"),
3529 if (newstate == VDEV_STATE_FAULTED)
3530 (void) printf(gettext("use 'zpool "
3531 "clear' to restore a faulted "
3534 (void) printf(gettext("use 'zpool "
3535 "replace' to replace devices "
3536 "that are no longer present\n"));
3549 * zpool offline [-ft] <pool> <device> ...
3551 * -f Force the device into the offline state, even if doing
3552 * so would appear to compromise pool availability.
3553 * (not supported yet)
3555 * -t Only take the device off-line temporarily. The offline
3556 * state will not be persistent across reboots.
3560 zpool_do_offline(int argc, char **argv)
3564 zpool_handle_t *zhp;
3566 boolean_t istmp = B_FALSE;
3569 while ((c = getopt(argc, argv, "ft")) != -1) {
3576 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3585 /* get pool name and check number of arguments */
3587 (void) fprintf(stderr, gettext("missing pool name\n"));
3591 (void) fprintf(stderr, gettext("missing device name\n"));
3597 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3600 for (i = 1; i < argc; i++) {
3601 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3611 * zpool clear <pool> [device]
3613 * Clear all errors associated with a pool or a particular device.
3616 zpool_do_clear(int argc, char **argv)
3620 boolean_t dryrun = B_FALSE;
3621 boolean_t do_rewind = B_FALSE;
3622 boolean_t xtreme_rewind = B_FALSE;
3623 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3624 nvlist_t *policy = NULL;
3625 zpool_handle_t *zhp;
3626 char *pool, *device;
3629 while ((c = getopt(argc, argv, "FnX")) != -1) {
3638 xtreme_rewind = B_TRUE;
3641 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3651 (void) fprintf(stderr, gettext("missing pool name\n"));
3656 (void) fprintf(stderr, gettext("too many arguments\n"));
3660 if ((dryrun || xtreme_rewind) && !do_rewind) {
3661 (void) fprintf(stderr,
3662 gettext("-n or -X only meaningful with -F\n"));
3666 rewind_policy = ZPOOL_TRY_REWIND;
3668 rewind_policy = ZPOOL_DO_REWIND;
3670 rewind_policy |= ZPOOL_EXTREME_REWIND;
3672 /* In future, further rewind policy choices can be passed along here */
3673 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3674 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3678 device = argc == 2 ? argv[1] : NULL;
3680 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3681 nvlist_free(policy);
3685 if (zpool_clear(zhp, device, policy) != 0)
3690 nvlist_free(policy);
3696 * zpool reguid <pool>
3699 zpool_do_reguid(int argc, char **argv)
3703 zpool_handle_t *zhp;
3707 while ((c = getopt(argc, argv, "")) != -1) {
3710 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3719 /* get pool name and check number of arguments */
3721 (void) fprintf(stderr, gettext("missing pool name\n"));
3726 (void) fprintf(stderr, gettext("too many arguments\n"));
3731 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3734 ret = zpool_reguid(zhp);
3742 * zpool reopen <pool>
3744 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3747 zpool_do_reopen(int argc, char **argv)
3751 zpool_handle_t *zhp;
3755 while ((c = getopt(argc, argv, "")) != -1) {
3758 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3768 (void) fprintf(stderr, gettext("missing pool name\n"));
3773 (void) fprintf(stderr, gettext("too many arguments\n"));
3778 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3781 ret = zpool_reopen(zhp);
3786 typedef struct scrub_cbdata {
3793 scrub_callback(zpool_handle_t *zhp, void *data)
3795 scrub_cbdata_t *cb = data;
3799 * Ignore faulted pools.
3801 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3802 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3803 "currently unavailable\n"), zpool_get_name(zhp));
3807 err = zpool_scan(zhp, cb->cb_type);
3813 * zpool scrub [-s] <pool> ...
3815 * -s Stop. Stops any in-progress scrub.
3818 zpool_do_scrub(int argc, char **argv)
3823 cb.cb_type = POOL_SCAN_SCRUB;
3826 while ((c = getopt(argc, argv, "s")) != -1) {
3829 cb.cb_type = POOL_SCAN_NONE;
3832 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3844 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3848 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3851 typedef struct status_cbdata {
3853 boolean_t cb_allpools;
3854 boolean_t cb_verbose;
3855 boolean_t cb_explain;
3857 boolean_t cb_dedup_stats;
3861 * Print out detailed scrub status.
3864 print_scan_status(pool_scan_stat_t *ps)
3867 uint64_t elapsed, mins_left, hours_left;
3868 uint64_t pass_exam, examined, total;
3870 double fraction_done;
3871 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3873 (void) printf(gettext(" scan: "));
3875 /* If there's never been a scan, there's not much to say. */
3876 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3877 ps->pss_func >= POOL_SCAN_FUNCS) {
3878 (void) printf(gettext("none requested\n"));
3882 start = ps->pss_start_time;
3883 end = ps->pss_end_time;
3884 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3886 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3887 ps->pss_func == POOL_SCAN_RESILVER);
3889 * Scan is finished or canceled.
3891 if (ps->pss_state == DSS_FINISHED) {
3892 uint64_t minutes_taken = (end - start) / 60;
3895 if (ps->pss_func == POOL_SCAN_SCRUB) {
3896 fmt = gettext("scrub repaired %s in %lluh%um with "
3897 "%llu errors on %s");
3898 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3899 fmt = gettext("resilvered %s in %lluh%um with "
3900 "%llu errors on %s");
3903 (void) printf(fmt, processed_buf,
3904 (u_longlong_t)(minutes_taken / 60),
3905 (uint_t)(minutes_taken % 60),
3906 (u_longlong_t)ps->pss_errors,
3907 ctime((time_t *)&end));
3909 } else if (ps->pss_state == DSS_CANCELED) {
3910 if (ps->pss_func == POOL_SCAN_SCRUB) {
3911 (void) printf(gettext("scrub canceled on %s"),
3913 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3914 (void) printf(gettext("resilver canceled on %s"),
3920 assert(ps->pss_state == DSS_SCANNING);
3923 * Scan is in progress.
3925 if (ps->pss_func == POOL_SCAN_SCRUB) {
3926 (void) printf(gettext("scrub in progress since %s"),
3928 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3929 (void) printf(gettext("resilver in progress since %s"),
3933 examined = ps->pss_examined ? ps->pss_examined : 1;
3934 total = ps->pss_to_examine;
3935 fraction_done = (double)examined / total;
3937 /* elapsed time for this pass */
3938 elapsed = time(NULL) - ps->pss_pass_start;
3939 elapsed = elapsed ? elapsed : 1;
3940 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3941 rate = pass_exam / elapsed;
3942 rate = rate ? rate : 1;
3943 mins_left = ((total - examined) / rate) / 60;
3944 hours_left = mins_left / 60;
3946 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3947 zfs_nicenum(total, total_buf, sizeof (total_buf));
3948 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3951 * do not print estimated time if hours_left is more than 30 days
3953 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3954 examined_buf, total_buf, rate_buf);
3955 if (hours_left < (30 * 24)) {
3956 (void) printf(gettext(", %lluh%um to go\n"),
3957 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3959 (void) printf(gettext(
3960 ", (scan is slow, no estimated time)\n"));
3963 if (ps->pss_func == POOL_SCAN_RESILVER) {
3964 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3965 processed_buf, 100 * fraction_done);
3966 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3967 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3968 processed_buf, 100 * fraction_done);
3973 print_error_log(zpool_handle_t *zhp)
3975 nvlist_t *nverrlist = NULL;
3978 size_t len = MAXPATHLEN * 2;
3980 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3981 (void) printf("errors: List of errors unavailable "
3982 "(insufficient privileges)\n");
3986 (void) printf("errors: Permanent errors have been "
3987 "detected in the following files:\n\n");
3989 pathname = safe_malloc(len);
3991 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3993 uint64_t dsobj, obj;
3995 verify(nvpair_value_nvlist(elem, &nv) == 0);
3996 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3998 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4000 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4001 (void) printf("%7s %s\n", "", pathname);
4004 nvlist_free(nverrlist);
4008 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4017 (void) printf(gettext("\tspares\n"));
4019 for (i = 0; i < nspares; i++) {
4020 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4021 print_status_config(zhp, name, spares[i],
4022 namewidth, 2, B_TRUE);
4028 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4037 (void) printf(gettext("\tcache\n"));
4039 for (i = 0; i < nl2cache; i++) {
4040 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4041 print_status_config(zhp, name, l2cache[i],
4042 namewidth, 2, B_FALSE);
4048 print_dedup_stats(nvlist_t *config)
4050 ddt_histogram_t *ddh;
4056 * If the pool was faulted then we may not have been able to
4057 * obtain the config. Otherwise, if we have anything in the dedup
4058 * table continue processing the stats.
4060 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4061 (uint64_t **)&ddo, &c) != 0)
4064 (void) printf("\n");
4065 (void) printf(gettext(" dedup: "));
4066 if (ddo->ddo_count == 0) {
4067 (void) printf(gettext("no DDT entries\n"));
4071 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4072 (u_longlong_t)ddo->ddo_count,
4073 (u_longlong_t)ddo->ddo_dspace,
4074 (u_longlong_t)ddo->ddo_mspace);
4076 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4077 (uint64_t **)&dds, &c) == 0);
4078 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4079 (uint64_t **)&ddh, &c) == 0);
4080 zpool_dump_ddt(dds, ddh);
4084 * Display a summary of pool status. Displays a summary such as:
4088 * reason: One or more devices ...
4089 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4095 * When given the '-v' option, we print out the complete config. If the '-e'
4096 * option is specified, then we print out error rate information as well.
4099 status_callback(zpool_handle_t *zhp, void *data)
4101 status_cbdata_t *cbp = data;
4102 nvlist_t *config, *nvroot;
4109 config = zpool_get_config(zhp, NULL);
4110 reason = zpool_get_status(zhp, &msgid);
4115 * If we were given 'zpool status -x', only report those pools with
4118 if (cbp->cb_explain &&
4119 (reason == ZPOOL_STATUS_OK ||
4120 reason == ZPOOL_STATUS_VERSION_OLDER ||
4121 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4122 if (!cbp->cb_allpools) {
4123 (void) printf(gettext("pool '%s' is healthy\n"),
4124 zpool_get_name(zhp));
4126 cbp->cb_first = B_FALSE;
4132 cbp->cb_first = B_FALSE;
4134 (void) printf("\n");
4136 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4138 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4139 (uint64_t **)&vs, &c) == 0);
4140 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4142 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4143 (void) printf(gettext(" state: %s\n"), health);
4146 case ZPOOL_STATUS_MISSING_DEV_R:
4147 (void) printf(gettext("status: One or more devices could not "
4148 "be opened. Sufficient replicas exist for\n\tthe pool to "
4149 "continue functioning in a degraded state.\n"));
4150 (void) printf(gettext("action: Attach the missing device and "
4151 "online it using 'zpool online'.\n"));
4154 case ZPOOL_STATUS_MISSING_DEV_NR:
4155 (void) printf(gettext("status: One or more devices could not "
4156 "be opened. There are insufficient\n\treplicas for the "
4157 "pool to continue functioning.\n"));
4158 (void) printf(gettext("action: Attach the missing device and "
4159 "online it using 'zpool online'.\n"));
4162 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4163 (void) printf(gettext("status: One or more devices could not "
4164 "be used because the label is missing or\n\tinvalid. "
4165 "Sufficient replicas exist for the pool to continue\n\t"
4166 "functioning in a degraded state.\n"));
4167 (void) printf(gettext("action: Replace the device using "
4168 "'zpool replace'.\n"));
4171 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4172 (void) printf(gettext("status: One or more devices could not "
4173 "be used because the label is missing \n\tor invalid. "
4174 "There are insufficient replicas for the pool to "
4175 "continue\n\tfunctioning.\n"));
4176 zpool_explain_recover(zpool_get_handle(zhp),
4177 zpool_get_name(zhp), reason, config);
4180 case ZPOOL_STATUS_FAILING_DEV:
4181 (void) printf(gettext("status: One or more devices has "
4182 "experienced an unrecoverable error. An\n\tattempt was "
4183 "made to correct the error. Applications are "
4185 (void) printf(gettext("action: Determine if the device needs "
4186 "to be replaced, and clear the errors\n\tusing "
4187 "'zpool clear' or replace the device with 'zpool "
4191 case ZPOOL_STATUS_OFFLINE_DEV:
4192 (void) printf(gettext("status: One or more devices has "
4193 "been taken offline by the administrator.\n\tSufficient "
4194 "replicas exist for the pool to continue functioning in "
4195 "a\n\tdegraded state.\n"));
4196 (void) printf(gettext("action: Online the device using "
4197 "'zpool online' or replace the device with\n\t'zpool "
4201 case ZPOOL_STATUS_REMOVED_DEV:
4202 (void) printf(gettext("status: One or more devices has "
4203 "been removed by the administrator.\n\tSufficient "
4204 "replicas exist for the pool to continue functioning in "
4205 "a\n\tdegraded state.\n"));
4206 (void) printf(gettext("action: Online the device using "
4207 "'zpool online' or replace the device with\n\t'zpool "
4211 case ZPOOL_STATUS_RESILVERING:
4212 (void) printf(gettext("status: One or more devices is "
4213 "currently being resilvered. The pool will\n\tcontinue "
4214 "to function, possibly in a degraded state.\n"));
4215 (void) printf(gettext("action: Wait for the resilver to "
4219 case ZPOOL_STATUS_CORRUPT_DATA:
4220 (void) printf(gettext("status: One or more devices has "
4221 "experienced an error resulting in data\n\tcorruption. "
4222 "Applications may be affected.\n"));
4223 (void) printf(gettext("action: Restore the file in question "
4224 "if possible. Otherwise restore the\n\tentire pool from "
4228 case ZPOOL_STATUS_CORRUPT_POOL:
4229 (void) printf(gettext("status: The pool metadata is corrupted "
4230 "and the pool cannot be opened.\n"));
4231 zpool_explain_recover(zpool_get_handle(zhp),
4232 zpool_get_name(zhp), reason, config);
4235 case ZPOOL_STATUS_VERSION_OLDER:
4236 (void) printf(gettext("status: The pool is formatted using a "
4237 "legacy on-disk format. The pool can\n\tstill be used, "
4238 "but some features are unavailable.\n"));
4239 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4240 "upgrade'. Once this is done, the\n\tpool will no longer "
4241 "be accessible on software that does not support\n\t"
4242 "feature flags.\n"));
4245 case ZPOOL_STATUS_VERSION_NEWER:
4246 (void) printf(gettext("status: The pool has been upgraded to a "
4247 "newer, incompatible on-disk version.\n\tThe pool cannot "
4248 "be accessed on this system.\n"));
4249 (void) printf(gettext("action: Access the pool from a system "
4250 "running more recent software, or\n\trestore the pool from "
4254 case ZPOOL_STATUS_FEAT_DISABLED:
4255 (void) printf(gettext("status: Some supported features are not "
4256 "enabled on the pool. The pool can\n\tstill be used, but "
4257 "some features are unavailable.\n"));
4258 (void) printf(gettext("action: Enable all features using "
4259 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4260 "longer be accessible by software that does not support\n\t"
4261 "the features. See zpool-features(5) for details.\n"));
4264 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4265 (void) printf(gettext("status: The pool cannot be accessed on "
4266 "this system because it uses the\n\tfollowing feature(s) "
4267 "not supported on this system:\n"));
4268 zpool_print_unsup_feat(config);
4269 (void) printf("\n");
4270 (void) printf(gettext("action: Access the pool from a system "
4271 "that supports the required feature(s),\n\tor restore the "
4272 "pool from backup.\n"));
4275 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4276 (void) printf(gettext("status: The pool can only be accessed "
4277 "in read-only mode on this system. It\n\tcannot be "
4278 "accessed in read-write mode because it uses the "
4279 "following\n\tfeature(s) not supported on this system:\n"));
4280 zpool_print_unsup_feat(config);
4281 (void) printf("\n");
4282 (void) printf(gettext("action: The pool cannot be accessed in "
4283 "read-write mode. Import the pool with\n"
4284 "\t\"-o readonly=on\", access the pool from a system that "
4285 "supports the\n\trequired feature(s), or restore the "
4286 "pool from backup.\n"));
4289 case ZPOOL_STATUS_FAULTED_DEV_R:
4290 (void) printf(gettext("status: One or more devices are "
4291 "faulted in response to persistent errors.\n\tSufficient "
4292 "replicas exist for the pool to continue functioning "
4293 "in a\n\tdegraded state.\n"));
4294 (void) printf(gettext("action: Replace the faulted device, "
4295 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4298 case ZPOOL_STATUS_FAULTED_DEV_NR:
4299 (void) printf(gettext("status: One or more devices are "
4300 "faulted in response to persistent errors. There are "
4301 "insufficient replicas for the pool to\n\tcontinue "
4303 (void) printf(gettext("action: Destroy and re-create the pool "
4304 "from a backup source. Manually marking the device\n"
4305 "\trepaired using 'zpool clear' may allow some data "
4306 "to be recovered.\n"));
4309 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4310 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4311 (void) printf(gettext("status: One or more devices are "
4312 "faulted in response to IO failures.\n"));
4313 (void) printf(gettext("action: Make sure the affected devices "
4314 "are connected, then run 'zpool clear'.\n"));
4317 case ZPOOL_STATUS_BAD_LOG:
4318 (void) printf(gettext("status: An intent log record "
4319 "could not be read.\n"
4320 "\tWaiting for adminstrator intervention to fix the "
4321 "faulted pool.\n"));
4322 (void) printf(gettext("action: Either restore the affected "
4323 "device(s) and run 'zpool online',\n"
4324 "\tor ignore the intent log records by running "
4325 "'zpool clear'.\n"));
4330 * The remaining errors can't actually be generated, yet.
4332 assert(reason == ZPOOL_STATUS_OK);
4336 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4339 if (config != NULL) {
4342 nvlist_t **spares, **l2cache;
4343 uint_t nspares, nl2cache;
4344 pool_scan_stat_t *ps = NULL;
4346 (void) nvlist_lookup_uint64_array(nvroot,
4347 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4348 print_scan_status(ps);
4350 namewidth = max_width(zhp, nvroot, 0, 0);
4354 (void) printf(gettext("config:\n\n"));
4355 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4356 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4357 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4358 namewidth, 0, B_FALSE);
4360 if (num_logs(nvroot) > 0)
4361 print_logs(zhp, nvroot, namewidth, B_TRUE);
4362 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4363 &l2cache, &nl2cache) == 0)
4364 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4366 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4367 &spares, &nspares) == 0)
4368 print_spares(zhp, spares, nspares, namewidth);
4370 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4372 nvlist_t *nverrlist = NULL;
4375 * If the approximate error count is small, get a
4376 * precise count by fetching the entire log and
4377 * uniquifying the results.
4379 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4380 zpool_get_errlog(zhp, &nverrlist) == 0) {
4385 while ((elem = nvlist_next_nvpair(nverrlist,
4390 nvlist_free(nverrlist);
4392 (void) printf("\n");
4395 (void) printf(gettext("errors: No known data "
4397 else if (!cbp->cb_verbose)
4398 (void) printf(gettext("errors: %llu data "
4399 "errors, use '-v' for a list\n"),
4400 (u_longlong_t)nerr);
4402 print_error_log(zhp);
4405 if (cbp->cb_dedup_stats)
4406 print_dedup_stats(config);
4408 (void) printf(gettext("config: The configuration cannot be "
4416 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4418 * -v Display complete error logs
4419 * -x Display only pools with potential problems
4420 * -D Display dedup status (undocumented)
4421 * -T Display a timestamp in date(1) or Unix format
4423 * Describes the health status of all pools or some subset.
4426 zpool_do_status(int argc, char **argv)
4430 unsigned long interval = 0, count = 0;
4431 status_cbdata_t cb = { 0 };
4434 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4437 cb.cb_verbose = B_TRUE;
4440 cb.cb_explain = B_TRUE;
4443 cb.cb_dedup_stats = B_TRUE;
4446 get_timestamp_arg(*optarg);
4449 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4458 get_interval_count(&argc, argv, &interval, &count);
4461 cb.cb_allpools = B_TRUE;
4463 cb.cb_first = B_TRUE;
4466 if (timestamp_fmt != NODATE)
4467 print_timestamp(timestamp_fmt);
4469 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4470 status_callback, &cb);
4472 if (argc == 0 && cb.cb_count == 0)
4473 (void) fprintf(stderr, gettext("no pools available\n"));
4474 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4475 (void) printf(gettext("all pools are healthy\n"));
4483 if (count != 0 && --count == 0)
4486 (void) sleep(interval);
4492 typedef struct upgrade_cbdata {
4495 uint64_t cb_version;
4500 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4504 uint64_t oldversion;
4506 config = zpool_get_config(zhp, NULL);
4507 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4510 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4511 assert(oldversion < version);
4513 ret = zpool_upgrade(zhp, version);
4517 if (version >= SPA_VERSION_FEATURES) {
4518 (void) printf(gettext("Successfully upgraded "
4519 "'%s' from version %llu to feature flags.\n"),
4520 zpool_get_name(zhp), (u_longlong_t) oldversion);
4522 (void) printf(gettext("Successfully upgraded "
4523 "'%s' from version %llu to version %llu.\n"),
4524 zpool_get_name(zhp), (u_longlong_t) oldversion,
4525 (u_longlong_t) version);
4532 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4535 boolean_t firstff = B_TRUE;
4536 nvlist_t *enabled = zpool_get_features(zhp);
4539 for (i = 0; i < SPA_FEATURES; i++) {
4540 const char *fname = spa_feature_table[i].fi_uname;
4541 const char *fguid = spa_feature_table[i].fi_guid;
4542 if (!nvlist_exists(enabled, fguid)) {
4544 verify(-1 != asprintf(&propname, "feature@%s", fname));
4545 ret = zpool_set_prop(zhp, propname,
4546 ZFS_FEATURE_ENABLED);
4554 (void) printf(gettext("Enabled the "
4555 "following features on '%s':\n"),
4556 zpool_get_name(zhp));
4559 (void) printf(gettext(" %s\n"), fname);
4570 upgrade_cb(zpool_handle_t *zhp, void *arg)
4572 upgrade_cbdata_t *cbp = arg;
4575 boolean_t printnl = B_FALSE;
4578 config = zpool_get_config(zhp, NULL);
4579 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4582 assert(SPA_VERSION_IS_SUPPORTED(version));
4584 if (version < cbp->cb_version) {
4585 cbp->cb_first = B_FALSE;
4586 ret = upgrade_version(zhp, cbp->cb_version);
4593 * XXX: This code can be enabled when Illumos commit
4594 * 4445fffbbb1ea25fd0e9ea68b9380dd7a6709025 is merged.
4595 * It reworks the history logging among other things.
4599 * If they did "zpool upgrade -a", then we could
4600 * be doing ioctls to different pools. We need
4601 * to log this history once to each pool, and bypass
4602 * the normal history logging that happens in main().
4604 (void) zpool_log_history(g_zfs, history_str);
4605 log_history = B_FALSE;
4609 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4611 ret = upgrade_enable_all(zhp, &count);
4616 cbp->cb_first = B_FALSE;
4622 (void) printf(gettext("\n"));
4629 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4631 upgrade_cbdata_t *cbp = arg;
4635 config = zpool_get_config(zhp, NULL);
4636 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4639 assert(SPA_VERSION_IS_SUPPORTED(version));
4641 if (version < SPA_VERSION_FEATURES) {
4642 if (cbp->cb_first) {
4643 (void) printf(gettext("The following pools are "
4644 "formatted with legacy version numbers and can\n"
4645 "be upgraded to use feature flags. After "
4646 "being upgraded, these pools\nwill no "
4647 "longer be accessible by software that does not "
4648 "support feature\nflags.\n\n"));
4649 (void) printf(gettext("VER POOL\n"));
4650 (void) printf(gettext("--- ------------\n"));
4651 cbp->cb_first = B_FALSE;
4654 (void) printf("%2llu %s\n", (u_longlong_t)version,
4655 zpool_get_name(zhp));
4662 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4664 upgrade_cbdata_t *cbp = arg;
4668 config = zpool_get_config(zhp, NULL);
4669 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4672 if (version >= SPA_VERSION_FEATURES) {
4674 boolean_t poolfirst = B_TRUE;
4675 nvlist_t *enabled = zpool_get_features(zhp);
4677 for (i = 0; i < SPA_FEATURES; i++) {
4678 const char *fguid = spa_feature_table[i].fi_guid;
4679 const char *fname = spa_feature_table[i].fi_uname;
4680 if (!nvlist_exists(enabled, fguid)) {
4681 if (cbp->cb_first) {
4682 (void) printf(gettext("\nSome "
4683 "supported features are not "
4684 "enabled on the following pools. "
4685 "Once a\nfeature is enabled the "
4686 "pool may become incompatible with "
4687 "software\nthat does not support "
4689 "zpool-features(5) for "
4691 (void) printf(gettext("POOL "
4693 (void) printf(gettext("------"
4695 cbp->cb_first = B_FALSE;
4699 (void) printf(gettext("%s\n"),
4700 zpool_get_name(zhp));
4701 poolfirst = B_FALSE;
4704 (void) printf(gettext(" %s\n"), fname);
4714 upgrade_one(zpool_handle_t *zhp, void *data)
4716 boolean_t printnl = B_FALSE;
4717 upgrade_cbdata_t *cbp = data;
4718 uint64_t cur_version;
4721 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4722 (void) printf(gettext("'log' is now a reserved word\n"
4723 "Pool 'log' must be renamed using export and import"
4728 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4729 if (cur_version > cbp->cb_version) {
4730 (void) printf(gettext("Pool '%s' is already formatted "
4731 "using more current version '%llu'.\n\n"),
4732 zpool_get_name(zhp), (u_longlong_t) cur_version);
4736 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4737 (void) printf(gettext("Pool '%s' is already formatted "
4738 "using version %llu.\n\n"), zpool_get_name(zhp),
4739 (u_longlong_t) cbp->cb_version);
4743 if (cur_version != cbp->cb_version) {
4745 ret = upgrade_version(zhp, cbp->cb_version);
4750 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4752 ret = upgrade_enable_all(zhp, &count);
4758 } else if (cur_version == SPA_VERSION) {
4759 (void) printf(gettext("Pool '%s' already has all "
4760 "supported features enabled.\n"),
4761 zpool_get_name(zhp));
4766 (void) printf(gettext("\n"));
4775 * zpool upgrade [-V version] <-a | pool ...>
4777 * With no arguments, display downrev'd ZFS pool available for upgrade.
4778 * Individual pools can be upgraded by specifying the pool, and '-a' will
4779 * upgrade all pools.
4782 zpool_do_upgrade(int argc, char **argv)
4785 upgrade_cbdata_t cb = { 0 };
4787 boolean_t showversions = B_FALSE;
4788 boolean_t upgradeall = B_FALSE;
4793 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4796 upgradeall = B_TRUE;
4799 showversions = B_TRUE;
4802 cb.cb_version = strtoll(optarg, &end, 10);
4804 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4805 (void) fprintf(stderr,
4806 gettext("invalid version '%s'\n"), optarg);
4811 (void) fprintf(stderr, gettext("missing argument for "
4812 "'%c' option\n"), optopt);
4816 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4827 if (cb.cb_version == 0) {
4828 cb.cb_version = SPA_VERSION;
4829 } else if (!upgradeall && argc == 0) {
4830 (void) fprintf(stderr, gettext("-V option is "
4831 "incompatible with other arguments\n"));
4836 if (upgradeall || argc != 0) {
4837 (void) fprintf(stderr, gettext("-v option is "
4838 "incompatible with other arguments\n"));
4841 } else if (upgradeall) {
4843 (void) fprintf(stderr, gettext("-a option should not "
4844 "be used along with a pool name\n"));
4849 (void) printf(gettext("This system supports ZFS pool feature "
4854 (void) printf(gettext("The following features are "
4856 (void) printf(gettext("FEAT DESCRIPTION\n"));
4857 (void) printf("----------------------------------------------"
4858 "---------------\n");
4859 for (i = 0; i < SPA_FEATURES; i++) {
4860 zfeature_info_t *fi = &spa_feature_table[i];
4861 const char *ro = fi->fi_can_readonly ?
4862 " (read-only compatible)" : "";
4864 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4865 (void) printf(" %s\n", fi->fi_desc);
4867 (void) printf("\n");
4869 (void) printf(gettext("The following legacy versions are also "
4871 (void) printf(gettext("VER DESCRIPTION\n"));
4872 (void) printf("--- -----------------------------------------"
4873 "---------------\n");
4874 (void) printf(gettext(" 1 Initial ZFS version\n"));
4875 (void) printf(gettext(" 2 Ditto blocks "
4876 "(replicated metadata)\n"));
4877 (void) printf(gettext(" 3 Hot spares and double parity "
4879 (void) printf(gettext(" 4 zpool history\n"));
4880 (void) printf(gettext(" 5 Compression using the gzip "
4882 (void) printf(gettext(" 6 bootfs pool property\n"));
4883 (void) printf(gettext(" 7 Separate intent log devices\n"));
4884 (void) printf(gettext(" 8 Delegated administration\n"));
4885 (void) printf(gettext(" 9 refquota and refreservation "
4887 (void) printf(gettext(" 10 Cache devices\n"));
4888 (void) printf(gettext(" 11 Improved scrub performance\n"));
4889 (void) printf(gettext(" 12 Snapshot properties\n"));
4890 (void) printf(gettext(" 13 snapused property\n"));
4891 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4892 (void) printf(gettext(" 15 user/group space accounting\n"));
4893 (void) printf(gettext(" 16 stmf property support\n"));
4894 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4895 (void) printf(gettext(" 18 Snapshot user holds\n"));
4896 (void) printf(gettext(" 19 Log device removal\n"));
4897 (void) printf(gettext(" 20 Compression using zle "
4898 "(zero-length encoding)\n"));
4899 (void) printf(gettext(" 21 Deduplication\n"));
4900 (void) printf(gettext(" 22 Received properties\n"));
4901 (void) printf(gettext(" 23 Slim ZIL\n"));
4902 (void) printf(gettext(" 24 System attributes\n"));
4903 (void) printf(gettext(" 25 Improved scrub stats\n"));
4904 (void) printf(gettext(" 26 Improved snapshot deletion "
4906 (void) printf(gettext(" 27 Improved snapshot creation "
4908 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4909 (void) printf(gettext("\nFor more information on a particular "
4910 "version, including supported releases,\n"));
4911 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4912 } else if (argc == 0 && upgradeall) {
4913 cb.cb_first = B_TRUE;
4914 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4915 if (ret == 0 && cb.cb_first) {
4916 if (cb.cb_version == SPA_VERSION) {
4917 (void) printf(gettext("All pools are already "
4918 "formatted using feature flags.\n\n"));
4919 (void) printf(gettext("Every feature flags "
4920 "pool already has all supported features "
4923 (void) printf(gettext("All pools are already "
4924 "formatted with version %llu or higher.\n"),
4925 (u_longlong_t) cb.cb_version);
4928 } else if (argc == 0) {
4929 cb.cb_first = B_TRUE;
4930 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4934 (void) printf(gettext("All pools are formatted "
4935 "using feature flags.\n\n"));
4937 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4938 "for a list of available legacy versions.\n"));
4941 cb.cb_first = B_TRUE;
4942 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4946 (void) printf(gettext("Every feature flags pool has "
4947 "all supported features enabled.\n"));
4949 (void) printf(gettext("\n"));
4952 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4959 typedef struct hist_cbdata {
4966 * Print out the command history for a specific pool.
4969 get_history_one(zpool_handle_t *zhp, void *data)
4985 char internalstr[MAXPATHLEN];
4986 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4990 cb->first = B_FALSE;
4992 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4994 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4997 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4998 &records, &numrecords) == 0);
4999 for (i = 0; i < numrecords; i++) {
5000 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
5004 /* is it an internal event or a standard event? */
5005 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
5007 if (cb->internal == 0)
5010 if (nvlist_lookup_uint64(records[i],
5011 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
5013 verify(nvlist_lookup_uint64(records[i],
5014 ZPOOL_HIST_TXG, &txg) == 0);
5015 verify(nvlist_lookup_string(records[i],
5016 ZPOOL_HIST_INT_STR, &pathstr) == 0);
5017 if (ievent >= LOG_END)
5019 (void) snprintf(internalstr,
5020 sizeof (internalstr),
5021 "[internal %s txg:%llu] %s",
5022 zfs_history_event_names[ievent], (u_longlong_t)txg,
5024 cmdstr = internalstr;
5027 (void) localtime_r(&tsec, &t);
5028 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5029 (void) printf("%s %s", tbuf, cmdstr);
5032 (void) printf("\n");
5035 (void) printf(" [");
5036 if (nvlist_lookup_uint64(records[i],
5037 ZPOOL_HIST_WHO, &who) == 0) {
5038 pwd = getpwuid((uid_t)who);
5040 (void) printf("user %s on",
5043 (void) printf("user %d on",
5046 (void) printf(gettext("no info]\n"));
5049 if (nvlist_lookup_string(records[i],
5050 ZPOOL_HIST_HOST, &hostname) == 0) {
5051 (void) printf(" %s", hostname);
5053 if (nvlist_lookup_string(records[i],
5054 ZPOOL_HIST_ZONE, &zonename) == 0) {
5055 (void) printf(":%s", zonename);
5059 (void) printf("\n");
5061 (void) printf("\n");
5068 * zpool history <pool>
5070 * Displays the history of commands that modified pools.
5075 zpool_do_history(int argc, char **argv)
5077 hist_cbdata_t cbdata = { 0 };
5081 cbdata.first = B_TRUE;
5083 while ((c = getopt(argc, argv, "li")) != -1) {
5089 cbdata.internal = 1;
5092 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5100 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5103 if (argc == 0 && cbdata.first == B_TRUE) {
5104 (void) fprintf(stderr, gettext("no pools available\n"));
5111 typedef struct ev_opts {
5119 zpool_do_events_short(nvlist_t *nvl)
5121 char ctime_str[26], str[32], *ptr;
5125 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5126 memset(str, ' ', 32);
5127 (void) ctime_r((const time_t *)&tv[0], ctime_str);
5128 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
5129 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
5130 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
5131 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
5132 (void) printf(gettext("%s "), str);
5134 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5135 (void) printf(gettext("%s\n"), ptr);
5139 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5143 for (nvp = nvlist_next_nvpair(nvl, NULL);
5144 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5146 data_type_t type = nvpair_type(nvp);
5147 const char *name = nvpair_name(nvp);
5157 printf(gettext("%*s%s = "), depth, "", name);
5160 case DATA_TYPE_BOOLEAN:
5161 printf(gettext("%s"), "1");
5164 case DATA_TYPE_BOOLEAN_VALUE:
5165 (void) nvpair_value_boolean_value(nvp, &b);
5166 printf(gettext("%s"), b ? "1" : "0");
5169 case DATA_TYPE_BYTE:
5170 (void) nvpair_value_byte(nvp, &i8);
5171 printf(gettext("0x%x"), i8);
5174 case DATA_TYPE_INT8:
5175 (void) nvpair_value_int8(nvp, (void *)&i8);
5176 printf(gettext("0x%x"), i8);
5179 case DATA_TYPE_UINT8:
5180 (void) nvpair_value_uint8(nvp, &i8);
5181 printf(gettext("0x%x"), i8);
5184 case DATA_TYPE_INT16:
5185 (void) nvpair_value_int16(nvp, (void *)&i16);
5186 printf(gettext("0x%x"), i16);
5189 case DATA_TYPE_UINT16:
5190 (void) nvpair_value_uint16(nvp, &i16);
5191 printf(gettext("0x%x"), i16);
5194 case DATA_TYPE_INT32:
5195 (void) nvpair_value_int32(nvp, (void *)&i32);
5196 printf(gettext("0x%x"), i32);
5199 case DATA_TYPE_UINT32:
5200 (void) nvpair_value_uint32(nvp, &i32);
5201 printf(gettext("0x%x"), i32);
5204 case DATA_TYPE_INT64:
5205 (void) nvpair_value_int64(nvp, (void *)&i64);
5206 printf(gettext("0x%llx"), (u_longlong_t)i64);
5209 case DATA_TYPE_UINT64:
5210 (void) nvpair_value_uint64(nvp, &i64);
5211 printf(gettext("0x%llx"), (u_longlong_t)i64);
5214 case DATA_TYPE_HRTIME:
5215 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5216 printf(gettext("0x%llx"), (u_longlong_t)i64);
5219 case DATA_TYPE_STRING:
5220 (void) nvpair_value_string(nvp, &str);
5221 printf(gettext("\"%s\""), str ? str : "<NULL>");
5224 case DATA_TYPE_NVLIST:
5225 printf(gettext("(embedded nvlist)\n"));
5226 (void) nvpair_value_nvlist(nvp, &cnv);
5227 zpool_do_events_nvprint(cnv, depth + 8);
5228 printf(gettext("%*s(end %s)"), depth, "", name);
5231 case DATA_TYPE_NVLIST_ARRAY: {
5235 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5236 printf(gettext("(%d embedded nvlists)\n"), nelem);
5237 for (i = 0; i < nelem; i++) {
5238 printf(gettext("%*s%s[%d] = %s\n"),
5239 depth, "", name, i, "(embedded nvlist)");
5240 zpool_do_events_nvprint(val[i], depth + 8);
5241 printf(gettext("%*s(end %s[%i])\n"),
5242 depth, "", name, i);
5244 printf(gettext("%*s(end %s)\n"), depth, "", name);
5248 case DATA_TYPE_INT8_ARRAY: {
5252 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5253 for (i = 0; i < nelem; i++)
5254 printf(gettext("0x%x "), val[i]);
5259 case DATA_TYPE_UINT8_ARRAY: {
5263 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5264 for (i = 0; i < nelem; i++)
5265 printf(gettext("0x%x "), val[i]);
5270 case DATA_TYPE_INT16_ARRAY: {
5274 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5275 for (i = 0; i < nelem; i++)
5276 printf(gettext("0x%x "), val[i]);
5281 case DATA_TYPE_UINT16_ARRAY: {
5285 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5286 for (i = 0; i < nelem; i++)
5287 printf(gettext("0x%x "), val[i]);
5292 case DATA_TYPE_INT32_ARRAY: {
5296 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5297 for (i = 0; i < nelem; i++)
5298 printf(gettext("0x%x "), val[i]);
5303 case DATA_TYPE_UINT32_ARRAY: {
5307 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5308 for (i = 0; i < nelem; i++)
5309 printf(gettext("0x%x "), val[i]);
5314 case DATA_TYPE_INT64_ARRAY: {
5318 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5319 for (i = 0; i < nelem; i++)
5320 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
5325 case DATA_TYPE_UINT64_ARRAY: {
5329 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5330 for (i = 0; i < nelem; i++)
5331 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
5336 case DATA_TYPE_STRING_ARRAY:
5337 case DATA_TYPE_BOOLEAN_ARRAY:
5338 case DATA_TYPE_BYTE_ARRAY:
5339 case DATA_TYPE_DOUBLE:
5340 case DATA_TYPE_UNKNOWN:
5341 printf(gettext("<unknown>"));
5345 printf(gettext("\n"));
5350 zpool_do_events_next(ev_opts_t *opts)
5353 int cleanup_fd, ret, dropped;
5355 cleanup_fd = open(ZFS_DEV, O_RDWR);
5356 VERIFY(cleanup_fd >= 0);
5358 if (!opts->scripted)
5359 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5362 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5363 !!opts->follow, cleanup_fd);
5364 if (ret || nvl == NULL)
5368 (void) printf(gettext("dropped %d events\n"), dropped);
5370 zpool_do_events_short(nvl);
5372 if (opts->verbose) {
5373 zpool_do_events_nvprint(nvl, 8);
5374 printf(gettext("\n"));
5376 (void) fflush(stdout);
5381 VERIFY(0 == close(cleanup_fd));
5387 zpool_do_events_clear(ev_opts_t *opts)
5391 ret = zpool_events_clear(g_zfs, &count);
5393 (void) printf(gettext("cleared %d events\n"), count);
5399 * zpool events [-vfc]
5401 * Displays events logs by ZFS.
5404 zpool_do_events(int argc, char **argv)
5406 ev_opts_t opts = { 0 };
5411 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5426 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5435 ret = zpool_do_events_clear(&opts);
5437 ret = zpool_do_events_next(&opts);
5443 get_callback(zpool_handle_t *zhp, void *data)
5445 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5446 char value[MAXNAMELEN];
5447 zprop_source_t srctype;
5450 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5453 * Skip the special fake placeholder. This will also skip
5454 * over the name property when 'all' is specified.
5456 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5457 pl == cbp->cb_proplist)
5460 if (pl->pl_prop == ZPROP_INVAL &&
5461 (zpool_prop_feature(pl->pl_user_prop) ||
5462 zpool_prop_unsupported(pl->pl_user_prop))) {
5463 srctype = ZPROP_SRC_LOCAL;
5465 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5466 value, sizeof (value)) == 0) {
5467 zprop_print_one_property(zpool_get_name(zhp),
5468 cbp, pl->pl_user_prop, value, srctype,
5472 if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5473 sizeof (value), &srctype, cbp->cb_literal) != 0)
5476 zprop_print_one_property(zpool_get_name(zhp), cbp,
5477 zpool_prop_to_name(pl->pl_prop), value, srctype,
5485 zpool_do_get(int argc, char **argv)
5487 zprop_get_cbdata_t cb = { 0 };
5488 zprop_list_t fake_name = { 0 };
5492 while ((c = getopt(argc, argv, "p")) != -1) {
5495 cb.cb_literal = B_TRUE;
5499 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5509 (void) fprintf(stderr, gettext("missing property "
5514 cb.cb_first = B_TRUE;
5515 cb.cb_sources = ZPROP_SRC_ALL;
5516 cb.cb_columns[0] = GET_COL_NAME;
5517 cb.cb_columns[1] = GET_COL_PROPERTY;
5518 cb.cb_columns[2] = GET_COL_VALUE;
5519 cb.cb_columns[3] = GET_COL_SOURCE;
5520 cb.cb_type = ZFS_TYPE_POOL;
5522 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5523 ZFS_TYPE_POOL) != 0)
5526 if (cb.cb_proplist != NULL) {
5527 fake_name.pl_prop = ZPOOL_PROP_NAME;
5528 fake_name.pl_width = strlen(gettext("NAME"));
5529 fake_name.pl_next = cb.cb_proplist;
5530 cb.cb_proplist = &fake_name;
5533 ret = for_each_pool(argc - 1, argv + 1, B_TRUE, &cb.cb_proplist,
5536 if (cb.cb_proplist == &fake_name)
5537 zprop_free_list(fake_name.pl_next);
5539 zprop_free_list(cb.cb_proplist);
5544 typedef struct set_cbdata {
5547 boolean_t cb_any_successful;
5551 set_callback(zpool_handle_t *zhp, void *data)
5554 set_cbdata_t *cb = (set_cbdata_t *)data;
5556 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5559 cb->cb_any_successful = B_TRUE;
5565 zpool_do_set(int argc, char **argv)
5567 set_cbdata_t cb = { 0 };
5570 if (argc > 1 && argv[1][0] == '-') {
5571 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5577 (void) fprintf(stderr, gettext("missing property=value "
5583 (void) fprintf(stderr, gettext("missing pool name\n"));
5588 (void) fprintf(stderr, gettext("too many pool names\n"));
5592 cb.cb_propname = argv[1];
5593 cb.cb_value = strchr(cb.cb_propname, '=');
5594 if (cb.cb_value == NULL) {
5595 (void) fprintf(stderr, gettext("missing value in "
5596 "property=value argument\n"));
5600 *(cb.cb_value) = '\0';
5603 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5610 find_command_idx(char *command, int *idx)
5614 for (i = 0; i < NCOMMAND; i++) {
5615 if (command_table[i].name == NULL)
5618 if (strcmp(command, command_table[i].name) == 0) {
5627 main(int argc, char **argv)
5633 (void) setlocale(LC_ALL, "");
5634 (void) textdomain(TEXT_DOMAIN);
5639 * Make sure the user has specified some command.
5642 (void) fprintf(stderr, gettext("missing command\n"));
5651 if ((strcmp(cmdname, "-?") == 0) ||
5652 strcmp(cmdname, "--help") == 0)
5655 if ((g_zfs = libzfs_init()) == NULL)
5658 libzfs_print_on_error(g_zfs, B_TRUE);
5660 zpool_set_history_str("zpool", argc, argv, history_str);
5661 verify(zpool_stage_history(g_zfs, history_str) == 0);
5664 * Run the appropriate command.
5666 if (find_command_idx(cmdname, &i) == 0) {
5667 current_command = &command_table[i];
5668 ret = command_table[i].func(argc - 1, argv + 1);
5669 } else if (strchr(cmdname, '=')) {
5670 verify(find_command_idx("set", &i) == 0);
5671 current_command = &command_table[i];
5672 ret = command_table[i].func(argc, argv);
5673 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5675 * 'freeze' is a vile debugging abomination, so we treat
5679 int fd = open(ZFS_DEV, O_RDWR);
5680 (void) strcpy((void *)buf, argv[2]);
5681 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5683 (void) fprintf(stderr, gettext("unrecognized "
5684 "command '%s'\n"), cmdname);
5692 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5693 * for the purposes of running ::findleaks.
5695 if (getenv("ZFS_ABORT") != NULL) {
5696 (void) printf("dumping core by request\n");