4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
50 #include <sys/fs/zfs.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
58 #include "statcommon.h"
60 static int zpool_do_create(int, char **);
61 static int zpool_do_destroy(int, char **);
63 static int zpool_do_add(int, char **);
64 static int zpool_do_remove(int, char **);
65 static int zpool_do_labelclear(int, char **);
67 static int zpool_do_list(int, char **);
68 static int zpool_do_iostat(int, char **);
69 static int zpool_do_status(int, char **);
71 static int zpool_do_online(int, char **);
72 static int zpool_do_offline(int, char **);
73 static int zpool_do_clear(int, char **);
74 static int zpool_do_reopen(int, char **);
76 static int zpool_do_reguid(int, char **);
78 static int zpool_do_attach(int, char **);
79 static int zpool_do_detach(int, char **);
80 static int zpool_do_replace(int, char **);
81 static int zpool_do_split(int, char **);
83 static int zpool_do_scrub(int, char **);
85 static int zpool_do_import(int, char **);
86 static int zpool_do_export(int, char **);
88 static int zpool_do_upgrade(int, char **);
90 static int zpool_do_history(int, char **);
92 static int zpool_do_get(int, char **);
93 static int zpool_do_set(int, char **);
96 * These libumem hooks provide a reasonable set of defaults for the allocator's
97 * debugging facilities.
102 _umem_debug_init(void)
104 return ("default,verbose"); /* $UMEM_DEBUG setting */
108 _umem_logging_init(void)
110 return ("fail,contents"); /* $UMEM_LOGGING setting */
142 typedef struct zpool_command {
144 int (*func)(int, char **);
149 * Master command table. Each ZFS command has a name, associated function, and
150 * usage message. The usage messages need to be internationalized, so we have
151 * to have a function to return the usage message based on a command index.
153 * These commands are organized according to how they are displayed in the usage
154 * message. An empty command (one with a NULL name) indicates an empty line in
155 * the generic usage message.
157 static zpool_command_t command_table[] = {
158 { "create", zpool_do_create, HELP_CREATE },
159 { "destroy", zpool_do_destroy, HELP_DESTROY },
161 { "add", zpool_do_add, HELP_ADD },
162 { "remove", zpool_do_remove, HELP_REMOVE },
164 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
166 { "list", zpool_do_list, HELP_LIST },
167 { "iostat", zpool_do_iostat, HELP_IOSTAT },
168 { "status", zpool_do_status, HELP_STATUS },
170 { "online", zpool_do_online, HELP_ONLINE },
171 { "offline", zpool_do_offline, HELP_OFFLINE },
172 { "clear", zpool_do_clear, HELP_CLEAR },
173 { "reopen", zpool_do_reopen, HELP_REOPEN },
175 { "attach", zpool_do_attach, HELP_ATTACH },
176 { "detach", zpool_do_detach, HELP_DETACH },
177 { "replace", zpool_do_replace, HELP_REPLACE },
178 { "split", zpool_do_split, HELP_SPLIT },
180 { "scrub", zpool_do_scrub, HELP_SCRUB },
182 { "import", zpool_do_import, HELP_IMPORT },
183 { "export", zpool_do_export, HELP_EXPORT },
184 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
185 { "reguid", zpool_do_reguid, HELP_REGUID },
187 { "history", zpool_do_history, HELP_HISTORY },
188 { "get", zpool_do_get, HELP_GET },
189 { "set", zpool_do_set, HELP_SET },
192 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
194 zpool_command_t *current_command;
195 static char history_str[HIS_MAX_RECORD_LEN];
197 static uint_t timestamp_fmt = NODATE;
200 get_usage(zpool_help_t idx) {
203 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
205 return (gettext("\tattach [-f] <pool> <device> "
208 return (gettext("\tclear [-nF] <pool> [device]\n"));
210 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
211 "\t [-O file-system-property=value] ... \n"
212 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
214 return (gettext("\tdestroy [-f] <pool>\n"));
216 return (gettext("\tdetach <pool> <device>\n"));
218 return (gettext("\texport [-f] <pool> ...\n"));
220 return (gettext("\thistory [-il] [<pool>] ...\n"));
222 return (gettext("\timport [-d dir] [-D]\n"
223 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
224 "\timport [-o mntopts] [-o property=value] ... \n"
225 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
226 "[-R root] [-F [-n]] -a\n"
227 "\timport [-o mntopts] [-o property=value] ... \n"
228 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
229 "[-R root] [-F [-n]]\n"
230 "\t <pool | id> [newpool]\n"));
232 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
234 case HELP_LABELCLEAR:
235 return (gettext("\tlabelclear [-f] <vdev>\n"));
237 return (gettext("\tlist [-H] [-o property[,...]] "
238 "[-T d|u] [pool] ... [interval [count]]\n"));
240 return (gettext("\toffline [-t] <pool> <device> ...\n"));
242 return (gettext("\tonline [-e] <pool> <device> ...\n"));
244 return (gettext("\treplace [-f] <pool> <device> "
247 return (gettext("\tremove <pool> <device> ...\n"));
249 return (""); /* Undocumented command */
251 return (gettext("\tscrub [-s] <pool> ...\n"));
253 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
256 return (gettext("\tupgrade [-v]\n"
257 "\tupgrade [-V version] <-a | pool ...>\n"));
259 return (gettext("\tget <\"all\" | property[,...]> "
262 return (gettext("\tset <property=value> <pool> \n"));
264 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
265 "\t [-o property=value] <pool> <newpool> "
266 "[<device> ...]\n"));
268 return (gettext("\treguid <pool>\n"));
277 * Callback routine that will print out a pool property value.
280 print_prop_cb(int prop, void *cb)
284 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
286 if (zpool_prop_readonly(prop))
287 (void) fprintf(fp, " NO ");
289 (void) fprintf(fp, " YES ");
291 if (zpool_prop_values(prop) == NULL)
292 (void) fprintf(fp, "-\n");
294 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
300 * Display usage message. If we're inside a command, display only the usage for
301 * that command. Otherwise, iterate over the entire command table and display
302 * a complete usage message.
305 usage(boolean_t requested)
307 FILE *fp = requested ? stdout : stderr;
309 if (current_command == NULL) {
312 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
314 gettext("where 'command' is one of the following:\n\n"));
316 for (i = 0; i < NCOMMAND; i++) {
317 if (command_table[i].name == NULL)
318 (void) fprintf(fp, "\n");
320 (void) fprintf(fp, "%s",
321 get_usage(command_table[i].usage));
324 (void) fprintf(fp, gettext("usage:\n"));
325 (void) fprintf(fp, "%s", get_usage(current_command->usage));
328 if (current_command != NULL &&
329 ((strcmp(current_command->name, "set") == 0) ||
330 (strcmp(current_command->name, "get") == 0) ||
331 (strcmp(current_command->name, "list") == 0))) {
334 gettext("\nthe following properties are supported:\n"));
336 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
337 "PROPERTY", "EDIT", "VALUES");
339 /* Iterate over all properties */
340 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
345 * See comments at end of main().
347 if (getenv("ZFS_ABORT") != NULL) {
348 (void) printf("dumping core by request\n");
352 exit(requested ? 0 : 2);
356 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
357 boolean_t print_logs)
364 (void) printf("\t%*s%s\n", indent, "", name);
366 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
367 &child, &children) != 0)
370 for (c = 0; c < children; c++) {
371 uint64_t is_log = B_FALSE;
373 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
375 if ((is_log && !print_logs) || (!is_log && print_logs))
378 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
379 print_vdev_tree(zhp, vname, child[c], indent + 2,
386 * Add a property pair (name, string-value) into a property nvlist.
389 add_prop_list(const char *propname, char *propval, nvlist_t **props,
392 zpool_prop_t prop = ZPROP_INVAL;
398 if (*props == NULL &&
399 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
400 (void) fprintf(stderr,
401 gettext("internal error: out of memory\n"));
408 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
409 (void) fprintf(stderr, gettext("property '%s' is "
410 "not a valid pool property\n"), propname);
413 normnm = zpool_prop_to_name(prop);
415 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
416 normnm = zfs_prop_to_name(fprop);
422 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
423 prop != ZPOOL_PROP_CACHEFILE) {
424 (void) fprintf(stderr, gettext("property '%s' "
425 "specified multiple times\n"), propname);
429 if (nvlist_add_string(proplist, normnm, propval) != 0) {
430 (void) fprintf(stderr, gettext("internal "
431 "error: out of memory\n"));
439 * zpool add [-fn] <pool> <vdev> ...
441 * -f Force addition of devices, even if they appear in use
442 * -n Do not add the devices, but display the resulting layout if
443 * they were to be added.
445 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
446 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
450 zpool_do_add(int argc, char **argv)
452 boolean_t force = B_FALSE;
453 boolean_t dryrun = B_FALSE;
462 while ((c = getopt(argc, argv, "fn")) != -1) {
471 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
480 /* get pool name and check number of arguments */
482 (void) fprintf(stderr, gettext("missing pool name argument\n"));
486 (void) fprintf(stderr, gettext("missing vdev specification\n"));
495 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
498 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
499 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
505 /* pass off to get_vdev_spec for processing */
506 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
508 if (nvroot == NULL) {
514 nvlist_t *poolnvroot;
516 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
519 (void) printf(gettext("would update '%s' to the following "
520 "configuration:\n"), zpool_get_name(zhp));
522 /* print original main pool and new tree */
523 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
524 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
526 /* Do the same for the logs */
527 if (num_logs(poolnvroot) > 0) {
528 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
529 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
530 } else if (num_logs(nvroot) > 0) {
531 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
536 ret = (zpool_add(zhp, nvroot) != 0);
546 * zpool remove <pool> <vdev> ...
548 * Removes the given vdev from the pool. Currently, this supports removing
549 * spares, cache, and log devices from the pool.
552 zpool_do_remove(int argc, char **argv)
561 /* get pool name and check number of arguments */
563 (void) fprintf(stderr, gettext("missing pool name argument\n"));
567 (void) fprintf(stderr, gettext("missing device\n"));
573 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
576 for (i = 1; i < argc; i++) {
577 if (zpool_vdev_remove(zhp, argv[i]) != 0)
585 * zpool labelclear <vdev>
587 * Verifies that the vdev is not active and zeros out the label information
591 zpool_do_labelclear(int argc, char **argv)
594 int c, fd = -1, ret = 0;
596 boolean_t inuse = B_FALSE;
597 boolean_t force = B_FALSE;
600 while ((c = getopt(argc, argv, "f")) != -1) {
606 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
617 (void) fprintf(stderr, gettext("missing vdev device name\n"));
622 if ((fd = open(vdev, O_RDWR)) < 0) {
623 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
628 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
632 (void) fprintf(stderr,
633 gettext("Unable to determine pool state for %s\n"
634 "Use -f to force the clearing any label data\n"), vdev);
642 case POOL_STATE_ACTIVE:
643 case POOL_STATE_SPARE:
644 case POOL_STATE_L2CACHE:
645 (void) fprintf(stderr,
646 gettext("labelclear operation failed.\n"
647 "\tVdev %s is a member (%s), of pool \"%s\".\n"
648 "\tTo remove label information from this device, export or destroy\n"
649 "\tthe pool, or remove %s from the configuration of this pool\n"
650 "\tand retry the labelclear operation\n"),
651 vdev, zpool_pool_state_to_name(state), name, vdev);
655 case POOL_STATE_EXPORTED:
659 (void) fprintf(stderr,
660 gettext("labelclear operation failed.\n"
661 "\tVdev %s is a member of the exported pool \"%s\".\n"
662 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
668 case POOL_STATE_POTENTIALLY_ACTIVE:
672 (void) fprintf(stderr,
673 gettext("labelclear operation failed.\n"
674 "\tVdev %s is a member of the pool \"%s\".\n"
675 "\tThis pool is unknown to this system, but may be active on\n"
676 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
677 "\tremoval of label information.\n"),
682 case POOL_STATE_DESTROYED:
683 /* inuse should never be set for a destoryed pool... */
689 if (zpool_clear_label(fd) != 0) {
690 (void) fprintf(stderr,
691 gettext("Label clear failed on vdev %s\n"), vdev);
704 * zpool create [-fn] [-o property=value] ...
705 * [-O file-system-property=value] ...
706 * [-R root] [-m mountpoint] <pool> <dev> ...
708 * -f Force creation, even if devices appear in use
709 * -n Do not create the pool, but display the resulting layout if it
710 * were to be created.
711 * -R Create a pool under an alternate root
712 * -m Set default mountpoint for the root dataset. By default it's
714 * -o Set property=value.
715 * -O Set fsproperty=value in the pool's root file system
717 * Creates the named pool according to the given vdev specification. The
718 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
719 * we get the nvlist back from get_vdev_spec(), we either print out the contents
720 * (if '-n' was specified), or pass it to libzfs to do the creation.
723 zpool_do_create(int argc, char **argv)
725 boolean_t force = B_FALSE;
726 boolean_t dryrun = B_FALSE;
728 nvlist_t *nvroot = NULL;
731 char *altroot = NULL;
732 char *mountpoint = NULL;
733 nvlist_t *fsprops = NULL;
734 nvlist_t *props = NULL;
738 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
748 if (add_prop_list(zpool_prop_to_name(
749 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
751 if (nvlist_lookup_string(props,
752 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
755 if (add_prop_list(zpool_prop_to_name(
756 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
763 if ((propval = strchr(optarg, '=')) == NULL) {
764 (void) fprintf(stderr, gettext("missing "
765 "'=' for -o option\n"));
771 if (add_prop_list(optarg, propval, &props, B_TRUE))
775 if ((propval = strchr(optarg, '=')) == NULL) {
776 (void) fprintf(stderr, gettext("missing "
777 "'=' for -O option\n"));
783 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
787 (void) fprintf(stderr, gettext("missing argument for "
788 "'%c' option\n"), optopt);
791 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
800 /* get pool name and check number of arguments */
802 (void) fprintf(stderr, gettext("missing pool name argument\n"));
806 (void) fprintf(stderr, gettext("missing vdev specification\n"));
813 * As a special case, check for use of '/' in the name, and direct the
814 * user to use 'zfs create' instead.
816 if (strchr(poolname, '/') != NULL) {
817 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
818 "character '/' in pool name\n"), poolname);
819 (void) fprintf(stderr, gettext("use 'zfs create' to "
820 "create a dataset\n"));
824 /* pass off to get_vdev_spec for bulk processing */
825 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
830 /* make_root_vdev() allows 0 toplevel children if there are spares */
831 if (!zfs_allocatable_devs(nvroot)) {
832 (void) fprintf(stderr, gettext("invalid vdev "
833 "specification: at least one toplevel vdev must be "
839 if (altroot != NULL && altroot[0] != '/') {
840 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
841 "must be an absolute path\n"), altroot);
846 * Check the validity of the mountpoint and direct the user to use the
847 * '-m' mountpoint option if it looks like its in use.
849 if (mountpoint == NULL ||
850 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
851 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
852 char buf[MAXPATHLEN];
855 if (mountpoint && mountpoint[0] != '/') {
856 (void) fprintf(stderr, gettext("invalid mountpoint "
857 "'%s': must be an absolute path, 'legacy', or "
858 "'none'\n"), mountpoint);
862 if (mountpoint == NULL) {
864 (void) snprintf(buf, sizeof (buf), "%s/%s",
867 (void) snprintf(buf, sizeof (buf), "/%s",
871 (void) snprintf(buf, sizeof (buf), "%s%s",
872 altroot, mountpoint);
874 (void) snprintf(buf, sizeof (buf), "%s",
878 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
879 (void) fprintf(stderr, gettext("mountpoint '%s' : "
880 "%s\n"), buf, strerror(errno));
881 (void) fprintf(stderr, gettext("use '-m' "
882 "option to provide a different default\n"));
887 while (count < 3 && readdir(dirp) != NULL)
889 (void) closedir(dirp);
892 (void) fprintf(stderr, gettext("mountpoint "
893 "'%s' exists and is not empty\n"), buf);
894 (void) fprintf(stderr, gettext("use '-m' "
895 "option to provide a "
896 "different default\n"));
904 * For a dry run invocation, print out a basic message and run
905 * through all the vdevs in the list and print out in an
906 * appropriate hierarchy.
908 (void) printf(gettext("would create '%s' with the "
909 "following layout:\n\n"), poolname);
911 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
912 if (num_logs(nvroot) > 0)
913 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
918 * Hand off to libzfs.
920 if (zpool_create(g_zfs, poolname,
921 nvroot, props, fsprops) == 0) {
922 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
923 ZFS_TYPE_FILESYSTEM);
925 if (mountpoint != NULL)
926 verify(zfs_prop_set(pool,
928 ZFS_PROP_MOUNTPOINT),
930 if (zfs_mount(pool, NULL, 0) == 0)
931 ret = zfs_shareall(pool);
934 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
935 (void) fprintf(stderr, gettext("pool name may have "
942 nvlist_free(fsprops);
946 nvlist_free(fsprops);
953 * zpool destroy <pool>
955 * -f Forcefully unmount any datasets
957 * Destroy the given pool. Automatically unmounts any datasets in the pool.
960 zpool_do_destroy(int argc, char **argv)
962 boolean_t force = B_FALSE;
969 while ((c = getopt(argc, argv, "f")) != -1) {
975 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
984 /* check arguments */
986 (void) fprintf(stderr, gettext("missing pool argument\n"));
990 (void) fprintf(stderr, gettext("too many arguments\n"));
996 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
998 * As a special case, check for use of '/' in the name, and
999 * direct the user to use 'zfs destroy' instead.
1001 if (strchr(pool, '/') != NULL)
1002 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1003 "destroy a dataset\n"));
1007 if (zpool_disable_datasets(zhp, force) != 0) {
1008 (void) fprintf(stderr, gettext("could not destroy '%s': "
1009 "could not unmount datasets\n"), zpool_get_name(zhp));
1013 ret = (zpool_destroy(zhp) != 0);
1021 * zpool export [-f] <pool> ...
1023 * -f Forcefully unmount datasets
1025 * Export the given pools. By default, the command will attempt to cleanly
1026 * unmount any active datasets within the pool. If the '-f' flag is specified,
1027 * then the datasets will be forcefully unmounted.
1030 zpool_do_export(int argc, char **argv)
1032 boolean_t force = B_FALSE;
1033 boolean_t hardforce = B_FALSE;
1035 zpool_handle_t *zhp;
1040 while ((c = getopt(argc, argv, "fF")) != -1) {
1049 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1058 /* check arguments */
1060 (void) fprintf(stderr, gettext("missing pool argument\n"));
1065 for (i = 0; i < argc; i++) {
1066 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1071 if (zpool_disable_datasets(zhp, force) != 0) {
1078 if (zpool_export_force(zhp) != 0)
1080 } else if (zpool_export(zhp, force) != 0) {
1091 * Given a vdev configuration, determine the maximum width needed for the device
1095 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1097 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1102 if (strlen(name) + depth > max)
1103 max = strlen(name) + depth;
1107 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1108 &child, &children) == 0) {
1109 for (c = 0; c < children; c++)
1110 if ((ret = max_width(zhp, child[c], depth + 2,
1115 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1116 &child, &children) == 0) {
1117 for (c = 0; c < children; c++)
1118 if ((ret = max_width(zhp, child[c], depth + 2,
1123 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1124 &child, &children) == 0) {
1125 for (c = 0; c < children; c++)
1126 if ((ret = max_width(zhp, child[c], depth + 2,
1135 typedef struct spare_cbdata {
1137 zpool_handle_t *cb_zhp;
1141 find_vdev(nvlist_t *nv, uint64_t search)
1147 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1151 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1152 &child, &children) == 0) {
1153 for (c = 0; c < children; c++)
1154 if (find_vdev(child[c], search))
1162 find_spare(zpool_handle_t *zhp, void *data)
1164 spare_cbdata_t *cbp = data;
1165 nvlist_t *config, *nvroot;
1167 config = zpool_get_config(zhp, NULL);
1168 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1171 if (find_vdev(nvroot, cbp->cb_guid)) {
1181 * Print out configuration state as requested by status_callback.
1184 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1185 int namewidth, int depth, boolean_t isspare)
1189 pool_scan_stat_t *ps = NULL;
1191 char rbuf[6], wbuf[6], cbuf[6];
1193 uint64_t notpresent;
1197 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1198 &child, &children) != 0)
1201 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1202 (uint64_t **)&vs, &c) == 0);
1204 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1207 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1210 if (vs->vs_aux == VDEV_AUX_SPARED)
1212 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1216 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1220 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1221 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1222 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1223 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1226 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1227 ¬present) == 0 ||
1228 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1230 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1231 (void) printf(" was %s", path);
1232 } else if (vs->vs_aux != 0) {
1235 switch (vs->vs_aux) {
1236 case VDEV_AUX_OPEN_FAILED:
1237 (void) printf(gettext("cannot open"));
1240 case VDEV_AUX_BAD_GUID_SUM:
1241 (void) printf(gettext("missing device"));
1244 case VDEV_AUX_NO_REPLICAS:
1245 (void) printf(gettext("insufficient replicas"));
1248 case VDEV_AUX_VERSION_NEWER:
1249 (void) printf(gettext("newer version"));
1252 case VDEV_AUX_SPARED:
1253 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1255 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1256 if (strcmp(zpool_get_name(cb.cb_zhp),
1257 zpool_get_name(zhp)) == 0)
1258 (void) printf(gettext("currently in "
1261 (void) printf(gettext("in use by "
1263 zpool_get_name(cb.cb_zhp));
1264 zpool_close(cb.cb_zhp);
1266 (void) printf(gettext("currently in use"));
1270 case VDEV_AUX_ERR_EXCEEDED:
1271 (void) printf(gettext("too many errors"));
1274 case VDEV_AUX_IO_FAILURE:
1275 (void) printf(gettext("experienced I/O failures"));
1278 case VDEV_AUX_BAD_LOG:
1279 (void) printf(gettext("bad intent log"));
1282 case VDEV_AUX_EXTERNAL:
1283 (void) printf(gettext("external device fault"));
1286 case VDEV_AUX_SPLIT_POOL:
1287 (void) printf(gettext("split into new pool"));
1291 (void) printf(gettext("corrupted data"));
1296 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1297 (uint64_t **)&ps, &c);
1299 if (ps && ps->pss_state == DSS_SCANNING &&
1300 vs->vs_scan_processed != 0 && children == 0) {
1301 (void) printf(gettext(" (%s)"),
1302 (ps->pss_func == POOL_SCAN_RESILVER) ?
1303 "resilvering" : "repairing");
1306 (void) printf("\n");
1308 for (c = 0; c < children; c++) {
1309 uint64_t islog = B_FALSE, ishole = B_FALSE;
1311 /* Don't print logs or holes here */
1312 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1314 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1316 if (islog || ishole)
1318 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1319 print_status_config(zhp, vname, child[c],
1320 namewidth, depth + 2, isspare);
1327 * Print the configuration of an exported pool. Iterate over all vdevs in the
1328 * pool, printing out the name and status for each one.
1331 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1338 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1339 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1340 strcmp(type, VDEV_TYPE_HOLE) == 0)
1343 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1344 (uint64_t **)&vs, &c) == 0);
1346 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1347 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1349 if (vs->vs_aux != 0) {
1352 switch (vs->vs_aux) {
1353 case VDEV_AUX_OPEN_FAILED:
1354 (void) printf(gettext("cannot open"));
1357 case VDEV_AUX_BAD_GUID_SUM:
1358 (void) printf(gettext("missing device"));
1361 case VDEV_AUX_NO_REPLICAS:
1362 (void) printf(gettext("insufficient replicas"));
1365 case VDEV_AUX_VERSION_NEWER:
1366 (void) printf(gettext("newer version"));
1369 case VDEV_AUX_ERR_EXCEEDED:
1370 (void) printf(gettext("too many errors"));
1374 (void) printf(gettext("corrupted data"));
1378 (void) printf("\n");
1380 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1381 &child, &children) != 0)
1384 for (c = 0; c < children; c++) {
1385 uint64_t is_log = B_FALSE;
1387 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1392 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1393 print_import_config(vname, child[c], namewidth, depth + 2);
1397 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1398 &child, &children) == 0) {
1399 (void) printf(gettext("\tcache\n"));
1400 for (c = 0; c < children; c++) {
1401 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1402 (void) printf("\t %s\n", vname);
1407 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1408 &child, &children) == 0) {
1409 (void) printf(gettext("\tspares\n"));
1410 for (c = 0; c < children; c++) {
1411 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1412 (void) printf("\t %s\n", vname);
1420 * Logs are recorded as top level vdevs in the main pool child array
1421 * but with "is_log" set to 1. We use either print_status_config() or
1422 * print_import_config() to print the top level logs then any log
1423 * children (eg mirrored slogs) are printed recursively - which
1424 * works because only the top level vdev is marked "is_log"
1427 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1432 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1436 (void) printf(gettext("\tlogs\n"));
1438 for (c = 0; c < children; c++) {
1439 uint64_t is_log = B_FALSE;
1442 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1446 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1448 print_status_config(zhp, name, child[c], namewidth,
1451 print_import_config(name, child[c], namewidth, 2);
1457 * Display the status for the given pool.
1460 show_import(nvlist_t *config)
1462 uint64_t pool_state;
1474 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1476 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1478 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1480 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1483 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1484 (uint64_t **)&vs, &vsc) == 0);
1485 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1487 reason = zpool_import_status(config, &msgid);
1489 (void) printf(gettext(" pool: %s\n"), name);
1490 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1491 (void) printf(gettext(" state: %s"), health);
1492 if (pool_state == POOL_STATE_DESTROYED)
1493 (void) printf(gettext(" (DESTROYED)"));
1494 (void) printf("\n");
1497 case ZPOOL_STATUS_MISSING_DEV_R:
1498 case ZPOOL_STATUS_MISSING_DEV_NR:
1499 case ZPOOL_STATUS_BAD_GUID_SUM:
1500 (void) printf(gettext(" status: One or more devices are "
1501 "missing from the system.\n"));
1504 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1505 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1506 (void) printf(gettext(" status: One or more devices contains "
1507 "corrupted data.\n"));
1510 case ZPOOL_STATUS_CORRUPT_DATA:
1512 gettext(" status: The pool data is corrupted.\n"));
1515 case ZPOOL_STATUS_OFFLINE_DEV:
1516 (void) printf(gettext(" status: One or more devices "
1517 "are offlined.\n"));
1520 case ZPOOL_STATUS_CORRUPT_POOL:
1521 (void) printf(gettext(" status: The pool metadata is "
1525 case ZPOOL_STATUS_VERSION_OLDER:
1526 (void) printf(gettext(" status: The pool is formatted using an "
1527 "older on-disk version.\n"));
1530 case ZPOOL_STATUS_VERSION_NEWER:
1531 (void) printf(gettext(" status: The pool is formatted using an "
1532 "incompatible version.\n"));
1535 case ZPOOL_STATUS_HOSTID_MISMATCH:
1536 (void) printf(gettext(" status: The pool was last accessed by "
1537 "another system.\n"));
1540 case ZPOOL_STATUS_FAULTED_DEV_R:
1541 case ZPOOL_STATUS_FAULTED_DEV_NR:
1542 (void) printf(gettext(" status: One or more devices are "
1546 case ZPOOL_STATUS_BAD_LOG:
1547 (void) printf(gettext(" status: An intent log record cannot be "
1551 case ZPOOL_STATUS_RESILVERING:
1552 (void) printf(gettext(" status: One or more devices were being "
1558 * No other status can be seen when importing pools.
1560 assert(reason == ZPOOL_STATUS_OK);
1564 * Print out an action according to the overall state of the pool.
1566 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1567 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1568 (void) printf(gettext(" action: The pool can be "
1569 "imported using its name or numeric identifier, "
1570 "though\n\tsome features will not be available "
1571 "without an explicit 'zpool upgrade'.\n"));
1572 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1573 (void) printf(gettext(" action: The pool can be "
1574 "imported using its name or numeric "
1575 "identifier and\n\tthe '-f' flag.\n"));
1577 (void) printf(gettext(" action: The pool can be "
1578 "imported using its name or numeric "
1580 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1581 (void) printf(gettext(" action: The pool can be imported "
1582 "despite missing or damaged devices. The\n\tfault "
1583 "tolerance of the pool may be compromised if imported.\n"));
1586 case ZPOOL_STATUS_VERSION_NEWER:
1587 (void) printf(gettext(" action: The pool cannot be "
1588 "imported. Access the pool on a system running "
1589 "newer\n\tsoftware, or recreate the pool from "
1592 case ZPOOL_STATUS_MISSING_DEV_R:
1593 case ZPOOL_STATUS_MISSING_DEV_NR:
1594 case ZPOOL_STATUS_BAD_GUID_SUM:
1595 (void) printf(gettext(" action: The pool cannot be "
1596 "imported. Attach the missing\n\tdevices and try "
1600 (void) printf(gettext(" action: The pool cannot be "
1601 "imported due to damaged devices or data.\n"));
1605 /* Print the comment attached to the pool. */
1606 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1607 (void) printf(gettext("comment: %s\n"), comment);
1610 * If the state is "closed" or "can't open", and the aux state
1611 * is "corrupt data":
1613 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1614 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1615 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1616 if (pool_state == POOL_STATE_DESTROYED)
1617 (void) printf(gettext("\tThe pool was destroyed, "
1618 "but can be imported using the '-Df' flags.\n"));
1619 else if (pool_state != POOL_STATE_EXPORTED)
1620 (void) printf(gettext("\tThe pool may be active on "
1621 "another system, but can be imported using\n\t"
1622 "the '-f' flag.\n"));
1626 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1629 (void) printf(gettext(" config:\n\n"));
1631 namewidth = max_width(NULL, nvroot, 0, 0);
1635 print_import_config(name, nvroot, namewidth, 0);
1636 if (num_logs(nvroot) > 0)
1637 print_logs(NULL, nvroot, namewidth, B_FALSE);
1639 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1640 (void) printf(gettext("\n\tAdditional devices are known to "
1641 "be part of this pool, though their\n\texact "
1642 "configuration cannot be determined.\n"));
1647 * Perform the import for the given configuration. This passes the heavy
1648 * lifting off to zpool_import_props(), and then mounts the datasets contained
1652 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1653 nvlist_t *props, int flags)
1655 zpool_handle_t *zhp;
1660 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1663 verify(nvlist_lookup_uint64(config,
1664 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1665 verify(nvlist_lookup_uint64(config,
1666 ZPOOL_CONFIG_VERSION, &version) == 0);
1667 if (version > SPA_VERSION) {
1668 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1669 "is formatted using a newer ZFS version\n"), name);
1671 } else if (state != POOL_STATE_EXPORTED &&
1672 !(flags & ZFS_IMPORT_ANY_HOST)) {
1675 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1677 if ((unsigned long)hostid != gethostid()) {
1682 verify(nvlist_lookup_string(config,
1683 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1684 verify(nvlist_lookup_uint64(config,
1685 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1687 (void) fprintf(stderr, gettext("cannot import "
1688 "'%s': pool may be in use from other "
1689 "system, it was last accessed by %s "
1690 "(hostid: 0x%lx) on %s"), name, hostname,
1691 (unsigned long)hostid,
1692 asctime(localtime(&t)));
1693 (void) fprintf(stderr, gettext("use '-f' to "
1694 "import anyway\n"));
1698 (void) fprintf(stderr, gettext("cannot import '%s': "
1699 "pool may be in use from other system\n"), name);
1700 (void) fprintf(stderr, gettext("use '-f' to import "
1706 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1709 if (newname != NULL)
1710 name = (char *)newname;
1712 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1715 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1716 !(flags & ZFS_IMPORT_ONLY) &&
1717 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1727 * zpool import [-d dir] [-D]
1728 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1729 * [-d dir | -c cachefile] [-f] -a
1730 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1731 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1733 * -c Read pool information from a cachefile instead of searching
1736 * -d Scan in a specific directory, other than /dev/dsk. More than
1737 * one directory can be specified using multiple '-d' options.
1739 * -D Scan for previously destroyed pools or import all or only
1740 * specified destroyed pools.
1742 * -R Temporarily import the pool, with all mountpoints relative to
1743 * the given root. The pool will remain exported when the machine
1746 * -V Import even in the presence of faulted vdevs. This is an
1747 * intentionally undocumented option for testing purposes, and
1748 * treats the pool configuration as complete, leaving any bad
1749 * vdevs in the FAULTED state. In other words, it does verbatim
1752 * -f Force import, even if it appears that the pool is active.
1754 * -F Attempt rewind if necessary.
1756 * -n See if rewind would work, but don't actually rewind.
1758 * -N Import the pool but don't mount datasets.
1760 * -T Specify a starting txg to use for import. This option is
1761 * intentionally undocumented option for testing purposes.
1763 * -a Import all pools found.
1765 * -o Set property=value and/or temporary mount options (without '=').
1767 * The import command scans for pools to import, and import pools based on pool
1768 * name and GUID. The pool can also be renamed as part of the import process.
1771 zpool_do_import(int argc, char **argv)
1773 char **searchdirs = NULL;
1777 nvlist_t *pools = NULL;
1778 boolean_t do_all = B_FALSE;
1779 boolean_t do_destroyed = B_FALSE;
1780 char *mntopts = NULL;
1783 uint64_t searchguid = 0;
1784 char *searchname = NULL;
1786 nvlist_t *found_config;
1787 nvlist_t *policy = NULL;
1788 nvlist_t *props = NULL;
1790 int flags = ZFS_IMPORT_NORMAL;
1791 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1792 boolean_t dryrun = B_FALSE;
1793 boolean_t do_rewind = B_FALSE;
1794 boolean_t xtreme_rewind = B_FALSE;
1795 uint64_t pool_state, txg = -1ULL;
1796 char *cachefile = NULL;
1797 importargs_t idata = { 0 };
1801 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1810 if (searchdirs == NULL) {
1811 searchdirs = safe_malloc(sizeof (char *));
1813 char **tmp = safe_malloc((nsearch + 1) *
1815 bcopy(searchdirs, tmp, nsearch *
1820 searchdirs[nsearch++] = optarg;
1823 do_destroyed = B_TRUE;
1826 flags |= ZFS_IMPORT_ANY_HOST;
1832 flags |= ZFS_IMPORT_MISSING_LOG;
1838 flags |= ZFS_IMPORT_ONLY;
1841 if ((propval = strchr(optarg, '=')) != NULL) {
1844 if (add_prop_list(optarg, propval,
1852 if (add_prop_list(zpool_prop_to_name(
1853 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1855 if (nvlist_lookup_string(props,
1856 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1859 if (add_prop_list(zpool_prop_to_name(
1860 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1865 txg = strtoull(optarg, &endptr, 10);
1866 if (errno != 0 || *endptr != '\0') {
1867 (void) fprintf(stderr,
1868 gettext("invalid txg value\n"));
1871 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1874 flags |= ZFS_IMPORT_VERBATIM;
1877 xtreme_rewind = B_TRUE;
1880 (void) fprintf(stderr, gettext("missing argument for "
1881 "'%c' option\n"), optopt);
1885 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1894 if (cachefile && nsearch != 0) {
1895 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1899 if ((dryrun || xtreme_rewind) && !do_rewind) {
1900 (void) fprintf(stderr,
1901 gettext("-n or -X only meaningful with -F\n"));
1905 rewind_policy = ZPOOL_TRY_REWIND;
1907 rewind_policy = ZPOOL_DO_REWIND;
1909 rewind_policy |= ZPOOL_EXTREME_REWIND;
1911 /* In the future, we can capture further policy and include it here */
1912 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1913 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1914 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1917 if (searchdirs == NULL) {
1918 searchdirs = safe_malloc(sizeof (char *));
1919 searchdirs[0] = "/dev";
1923 /* check argument count */
1926 (void) fprintf(stderr, gettext("too many arguments\n"));
1931 (void) fprintf(stderr, gettext("too many arguments\n"));
1936 * Check for the SYS_CONFIG privilege. We do this explicitly
1937 * here because otherwise any attempt to discover pools will
1940 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1941 (void) fprintf(stderr, gettext("cannot "
1942 "discover pools: permission denied\n"));
1944 nvlist_free(policy);
1950 * Depending on the arguments given, we do one of the following:
1952 * <none> Iterate through all pools and display information about
1955 * -a Iterate through all pools and try to import each one.
1957 * <id> Find the pool that corresponds to the given GUID/pool
1958 * name and import that one.
1960 * -D Above options applies only to destroyed pools.
1966 searchguid = strtoull(argv[0], &endptr, 10);
1967 if (errno != 0 || *endptr != '\0')
1968 searchname = argv[0];
1969 found_config = NULL;
1972 * User specified a name or guid. Ensure it's unique.
1974 idata.unique = B_TRUE;
1978 idata.path = searchdirs;
1979 idata.paths = nsearch;
1980 idata.poolname = searchname;
1981 idata.guid = searchguid;
1982 idata.cachefile = cachefile;
1984 pools = zpool_search_import(g_zfs, &idata);
1986 if (pools != NULL && idata.exists &&
1987 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1988 (void) fprintf(stderr, gettext("cannot import '%s': "
1989 "a pool with that name already exists\n"),
1991 (void) fprintf(stderr, gettext("use the form '%s "
1992 "<pool | id> <newpool>' to give it a new name\n"),
1995 } else if (pools == NULL && idata.exists) {
1996 (void) fprintf(stderr, gettext("cannot import '%s': "
1997 "a pool with that name is already created/imported,\n"),
1999 (void) fprintf(stderr, gettext("and no additional pools "
2000 "with that name were found\n"));
2002 } else if (pools == NULL) {
2004 (void) fprintf(stderr, gettext("cannot import '%s': "
2005 "no such pool available\n"), argv[0]);
2012 nvlist_free(policy);
2017 * At this point we have a list of import candidate configs. Even if
2018 * we were searching by pool name or guid, we still need to
2019 * post-process the list to deal with pool state and possible
2025 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2027 verify(nvpair_value_nvlist(elem, &config) == 0);
2029 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2031 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2033 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2036 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2043 (void) printf("\n");
2046 err |= do_import(config, NULL, mntopts,
2049 show_import(config);
2051 } else if (searchname != NULL) {
2055 * We are searching for a pool based on name.
2057 verify(nvlist_lookup_string(config,
2058 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2060 if (strcmp(name, searchname) == 0) {
2061 if (found_config != NULL) {
2062 (void) fprintf(stderr, gettext(
2063 "cannot import '%s': more than "
2064 "one matching pool\n"), searchname);
2065 (void) fprintf(stderr, gettext(
2066 "import by numeric ID instead\n"));
2069 found_config = config;
2075 * Search for a pool by guid.
2077 verify(nvlist_lookup_uint64(config,
2078 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2080 if (guid == searchguid)
2081 found_config = config;
2086 * If we were searching for a specific pool, verify that we found a
2087 * pool, and then do the import.
2089 if (argc != 0 && err == 0) {
2090 if (found_config == NULL) {
2091 (void) fprintf(stderr, gettext("cannot import '%s': "
2092 "no such pool available\n"), argv[0]);
2095 err |= do_import(found_config, argc == 1 ? NULL :
2096 argv[1], mntopts, props, flags);
2101 * If we were just looking for pools, report an error if none were
2104 if (argc == 0 && first)
2105 (void) fprintf(stderr,
2106 gettext("no pools available to import\n"));
2111 nvlist_free(policy);
2114 return (err ? 1 : 0);
2117 typedef struct iostat_cbdata {
2118 boolean_t cb_verbose;
2121 zpool_list_t *cb_list;
2125 print_iostat_separator(iostat_cbdata_t *cb)
2129 for (i = 0; i < cb->cb_namewidth; i++)
2131 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2135 print_iostat_header(iostat_cbdata_t *cb)
2137 (void) printf("%*s capacity operations bandwidth\n",
2138 cb->cb_namewidth, "");
2139 (void) printf("%-*s alloc free read write read write\n",
2140 cb->cb_namewidth, "pool");
2141 print_iostat_separator(cb);
2145 * Display a single statistic.
2148 print_one_stat(uint64_t value)
2152 zfs_nicenum(value, buf, sizeof (buf));
2153 (void) printf(" %5s", buf);
2157 * Print out all the statistics for the given vdev. This can either be the
2158 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2159 * is a verbose output, and we don't want to display the toplevel pool stats.
2162 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2163 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2165 nvlist_t **oldchild, **newchild;
2167 vdev_stat_t *oldvs, *newvs;
2168 vdev_stat_t zerovs = { 0 };
2173 if (oldnv != NULL) {
2174 verify(nvlist_lookup_uint64_array(oldnv,
2175 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2180 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2181 (uint64_t **)&newvs, &c) == 0);
2183 if (strlen(name) + depth > cb->cb_namewidth)
2184 (void) printf("%*s%s", depth, "", name);
2186 (void) printf("%*s%s%*s", depth, "", name,
2187 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2189 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2194 scale = (double)NANOSEC / tdelta;
2196 /* only toplevel vdevs have capacity stats */
2197 if (newvs->vs_space == 0) {
2198 (void) printf(" - -");
2200 print_one_stat(newvs->vs_alloc);
2201 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2204 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2205 oldvs->vs_ops[ZIO_TYPE_READ])));
2207 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2208 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2210 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2211 oldvs->vs_bytes[ZIO_TYPE_READ])));
2213 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2214 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2216 (void) printf("\n");
2218 if (!cb->cb_verbose)
2221 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2222 &newchild, &children) != 0)
2225 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2226 &oldchild, &c) != 0)
2229 for (c = 0; c < children; c++) {
2230 uint64_t ishole = B_FALSE, islog = B_FALSE;
2232 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2235 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2238 if (ishole || islog)
2241 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2242 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2243 newchild[c], cb, depth + 2);
2248 * Log device section
2251 if (num_logs(newnv) > 0) {
2252 (void) printf("%-*s - - - - - "
2253 "-\n", cb->cb_namewidth, "logs");
2255 for (c = 0; c < children; c++) {
2256 uint64_t islog = B_FALSE;
2257 (void) nvlist_lookup_uint64(newchild[c],
2258 ZPOOL_CONFIG_IS_LOG, &islog);
2261 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2263 print_vdev_stats(zhp, vname, oldnv ?
2264 oldchild[c] : NULL, newchild[c],
2273 * Include level 2 ARC devices in iostat output
2275 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2276 &newchild, &children) != 0)
2279 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2280 &oldchild, &c) != 0)
2284 (void) printf("%-*s - - - - - "
2285 "-\n", cb->cb_namewidth, "cache");
2286 for (c = 0; c < children; c++) {
2287 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2289 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2290 newchild[c], cb, depth + 2);
2297 refresh_iostat(zpool_handle_t *zhp, void *data)
2299 iostat_cbdata_t *cb = data;
2303 * If the pool has disappeared, remove it from the list and continue.
2305 if (zpool_refresh_stats(zhp, &missing) != 0)
2309 pool_list_remove(cb->cb_list, zhp);
2315 * Callback to print out the iostats for the given pool.
2318 print_iostat(zpool_handle_t *zhp, void *data)
2320 iostat_cbdata_t *cb = data;
2321 nvlist_t *oldconfig, *newconfig;
2322 nvlist_t *oldnvroot, *newnvroot;
2324 newconfig = zpool_get_config(zhp, &oldconfig);
2326 if (cb->cb_iteration == 1)
2329 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2332 if (oldconfig == NULL)
2335 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2339 * Print out the statistics for the pool.
2341 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2344 print_iostat_separator(cb);
2350 get_namewidth(zpool_handle_t *zhp, void *data)
2352 iostat_cbdata_t *cb = data;
2353 nvlist_t *config, *nvroot;
2355 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2356 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2358 if (!cb->cb_verbose)
2359 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2361 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2366 * The width must fall into the range [10,38]. The upper limit is the
2367 * maximum we can have and still fit in 80 columns.
2369 if (cb->cb_namewidth < 10)
2370 cb->cb_namewidth = 10;
2371 if (cb->cb_namewidth > 38)
2372 cb->cb_namewidth = 38;
2378 * Parse the input string, get the 'interval' and 'count' value if there is one.
2381 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2384 unsigned long interval = 0, count = 0;
2385 int argc = *argcp, errno;
2388 * Determine if the last argument is an integer or a pool name
2390 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2394 interval = strtoul(argv[argc - 1], &end, 10);
2396 if (*end == '\0' && errno == 0) {
2397 if (interval == 0) {
2398 (void) fprintf(stderr, gettext("interval "
2399 "cannot be zero\n"));
2403 * Ignore the last parameter
2408 * If this is not a valid number, just plow on. The
2409 * user will get a more informative error message later
2417 * If the last argument is also an integer, then we have both a count
2420 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2425 interval = strtoul(argv[argc - 1], &end, 10);
2427 if (*end == '\0' && errno == 0) {
2428 if (interval == 0) {
2429 (void) fprintf(stderr, gettext("interval "
2430 "cannot be zero\n"));
2435 * Ignore the last parameter
2449 get_timestamp_arg(char c)
2452 timestamp_fmt = UDATE;
2454 timestamp_fmt = DDATE;
2460 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2462 * -v Display statistics for individual vdevs
2463 * -T Display a timestamp in date(1) or Unix format
2465 * This command can be tricky because we want to be able to deal with pool
2466 * creation/destruction as well as vdev configuration changes. The bulk of this
2467 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2468 * on pool_list_update() to detect the addition of new pools. Configuration
2469 * changes are all handled within libzfs.
2472 zpool_do_iostat(int argc, char **argv)
2477 unsigned long interval = 0, count = 0;
2479 boolean_t verbose = B_FALSE;
2483 while ((c = getopt(argc, argv, "T:v")) != -1) {
2486 get_timestamp_arg(*optarg);
2492 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2501 get_interval_count(&argc, argv, &interval, &count);
2504 * Construct the list of all interesting pools.
2507 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2510 if (pool_list_count(list) == 0 && argc != 0) {
2511 pool_list_free(list);
2515 if (pool_list_count(list) == 0 && interval == 0) {
2516 pool_list_free(list);
2517 (void) fprintf(stderr, gettext("no pools available\n"));
2522 * Enter the main iostat loop.
2525 cb.cb_verbose = verbose;
2526 cb.cb_iteration = 0;
2527 cb.cb_namewidth = 0;
2530 pool_list_update(list);
2532 if ((npools = pool_list_count(list)) == 0)
2536 * Refresh all statistics. This is done as an explicit step
2537 * before calculating the maximum name width, so that any
2538 * configuration changes are properly accounted for.
2540 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2543 * Iterate over all pools to determine the maximum width
2544 * for the pool / device name column across all pools.
2546 cb.cb_namewidth = 0;
2547 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2549 if (timestamp_fmt != NODATE)
2550 print_timestamp(timestamp_fmt);
2553 * If it's the first time, or verbose mode, print the header.
2555 if (++cb.cb_iteration == 1 || verbose)
2556 print_iostat_header(&cb);
2558 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2561 * If there's more than one pool, and we're not in verbose mode
2562 * (which prints a separator for us), then print a separator.
2564 if (npools > 1 && !verbose)
2565 print_iostat_separator(&cb);
2568 (void) printf("\n");
2571 * Flush the output so that redirection to a file isn't buffered
2574 (void) fflush(stdout);
2579 if (count != 0 && --count == 0)
2582 (void) sleep(interval);
2585 pool_list_free(list);
2590 typedef struct list_cbdata {
2591 boolean_t cb_verbose;
2593 boolean_t cb_scripted;
2594 zprop_list_t *cb_proplist;
2598 * Given a list of columns to display, output appropriate headers for each one.
2601 print_header(list_cbdata_t *cb)
2603 zprop_list_t *pl = cb->cb_proplist;
2605 boolean_t first = B_TRUE;
2606 boolean_t right_justify;
2609 for (; pl != NULL; pl = pl->pl_next) {
2610 if (pl->pl_prop == ZPROP_INVAL)
2613 width = pl->pl_width;
2614 if (first && cb->cb_verbose) {
2616 * Reset the width to accommodate the verbose listing
2619 width = cb->cb_namewidth;
2627 header = zpool_prop_column_name(pl->pl_prop);
2628 right_justify = zpool_prop_align_right(pl->pl_prop);
2630 if (pl->pl_next == NULL && !right_justify)
2631 (void) printf("%s", header);
2632 else if (right_justify)
2633 (void) printf("%*s", width, header);
2635 (void) printf("%-*s", width, header);
2639 (void) printf("\n");
2643 * Given a pool and a list of properties, print out all the properties according
2644 * to the described layout.
2647 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2649 zprop_list_t *pl = cb->cb_proplist;
2650 boolean_t first = B_TRUE;
2651 char property[ZPOOL_MAXPROPLEN];
2653 boolean_t right_justify;
2656 for (; pl != NULL; pl = pl->pl_next) {
2658 width = pl->pl_width;
2659 if (first && cb->cb_verbose) {
2661 * Reset the width to accommodate the verbose listing
2664 width = cb->cb_namewidth;
2668 if (cb->cb_scripted)
2669 (void) printf("\t");
2676 right_justify = B_FALSE;
2677 if (pl->pl_prop != ZPROP_INVAL) {
2678 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2679 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2681 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2682 sizeof (property), NULL) != 0)
2687 right_justify = zpool_prop_align_right(pl->pl_prop);
2694 * If this is being called in scripted mode, or if this is the
2695 * last column and it is left-justified, don't include a width
2698 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2699 (void) printf("%s", propstr);
2700 else if (right_justify)
2701 (void) printf("%*s", width, propstr);
2703 (void) printf("%-*s", width, propstr);
2706 (void) printf("\n");
2710 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2714 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2716 zfs_nicenum(value, propval, sizeof (propval));
2718 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2719 (void) strlcpy(propval, "-", sizeof (propval));
2722 (void) printf("\t%s", propval);
2724 (void) printf(" %*s", width, propval);
2728 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2729 list_cbdata_t *cb, int depth)
2735 boolean_t scripted = cb->cb_scripted;
2737 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2738 (uint64_t **)&vs, &c) == 0);
2742 (void) printf("\t%s", name);
2743 else if (strlen(name) + depth > cb->cb_namewidth)
2744 (void) printf("%*s%s", depth, "", name);
2746 (void) printf("%*s%s%*s", depth, "", name,
2747 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2749 /* only toplevel vdevs have capacity stats */
2750 if (vs->vs_space == 0) {
2752 (void) printf("\t-\t-\t-");
2754 (void) printf(" - - -");
2756 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2758 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2760 print_one_column(ZPOOL_PROP_FREE,
2761 vs->vs_space - vs->vs_alloc, scripted);
2763 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2765 (void) printf("\n");
2768 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2769 &child, &children) != 0)
2772 for (c = 0; c < children; c++) {
2773 uint64_t ishole = B_FALSE;
2775 if (nvlist_lookup_uint64(child[c],
2776 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2779 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2780 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2785 * Include level 2 ARC devices in iostat output
2787 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2788 &child, &children) != 0)
2792 (void) printf("%-*s - - - - - "
2793 "-\n", cb->cb_namewidth, "cache");
2794 for (c = 0; c < children; c++) {
2795 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2797 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2805 * Generic callback function to list a pool.
2808 list_callback(zpool_handle_t *zhp, void *data)
2810 list_cbdata_t *cbp = data;
2814 config = zpool_get_config(zhp, NULL);
2816 print_pool(zhp, cbp);
2817 if (!cbp->cb_verbose)
2820 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2822 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2828 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2830 * -H Scripted mode. Don't display headers, and separate properties
2832 * -o List of properties to display. Defaults to
2833 * "name,size,allocated,free,capacity,health,altroot"
2834 * -T Display a timestamp in date(1) or Unix format
2836 * List all pools in the system, whether or not they're healthy. Output space
2837 * statistics for each one, as well as health status summary.
2840 zpool_do_list(int argc, char **argv)
2844 list_cbdata_t cb = { 0 };
2845 static char default_props[] =
2846 "name,size,allocated,free,capacity,dedupratio,"
2848 char *props = default_props;
2849 unsigned long interval = 0, count = 0;
2851 boolean_t first = B_TRUE;
2854 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2857 cb.cb_scripted = B_TRUE;
2863 get_timestamp_arg(*optarg);
2866 cb.cb_verbose = B_TRUE;
2869 (void) fprintf(stderr, gettext("missing argument for "
2870 "'%c' option\n"), optopt);
2874 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2883 get_interval_count(&argc, argv, &interval, &count);
2885 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2888 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
2891 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2892 (void) printf(gettext("no pools available\n"));
2893 zprop_free_list(cb.cb_proplist);
2898 pool_list_update(list);
2900 if (pool_list_count(list) == 0)
2903 cb.cb_namewidth = 0;
2904 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2906 if (timestamp_fmt != NODATE)
2907 print_timestamp(timestamp_fmt);
2909 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
2913 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
2918 if (count != 0 && --count == 0)
2921 (void) sleep(interval);
2924 zprop_free_list(cb.cb_proplist);
2929 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2936 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2937 &child, &children) != 0) {
2938 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2939 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2940 name += sizeof(_PATH_DEV) - 1;
2941 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2942 path += sizeof(_PATH_DEV) - 1;
2943 if (strcmp(name, path) == 0)
2948 for (c = 0; c < children; c++)
2949 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2956 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2958 boolean_t force = B_FALSE;
2961 char *poolname, *old_disk, *new_disk;
2962 zpool_handle_t *zhp;
2966 while ((c = getopt(argc, argv, "f")) != -1) {
2972 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2981 /* get pool name and check number of arguments */
2983 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2990 (void) fprintf(stderr,
2991 gettext("missing <device> specification\n"));
2999 (void) fprintf(stderr,
3000 gettext("missing <new_device> specification\n"));
3003 new_disk = old_disk;
3013 (void) fprintf(stderr, gettext("too many arguments\n"));
3017 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3020 if (zpool_get_config(zhp, NULL) == NULL) {
3021 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3027 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3029 if (nvroot == NULL) {
3034 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3036 nvlist_free(nvroot);
3043 * zpool replace [-f] <pool> <device> <new_device>
3045 * -f Force attach, even if <new_device> appears to be in use.
3047 * Replace <device> with <new_device>.
3051 zpool_do_replace(int argc, char **argv)
3053 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3057 * zpool attach [-f] <pool> <device> <new_device>
3059 * -f Force attach, even if <new_device> appears to be in use.
3061 * Attach <new_device> to the mirror containing <device>. If <device> is not
3062 * part of a mirror, then <device> will be transformed into a mirror of
3063 * <device> and <new_device>. In either case, <new_device> will begin life
3064 * with a DTL of [0, now], and will immediately begin to resilver itself.
3067 zpool_do_attach(int argc, char **argv)
3069 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3073 * zpool detach [-f] <pool> <device>
3075 * -f Force detach of <device>, even if DTLs argue against it
3076 * (not supported yet)
3078 * Detach a device from a mirror. The operation will be refused if <device>
3079 * is the last device in the mirror, or if the DTLs indicate that this device
3080 * has the only valid copy of some data.
3084 zpool_do_detach(int argc, char **argv)
3087 char *poolname, *path;
3088 zpool_handle_t *zhp;
3092 while ((c = getopt(argc, argv, "f")) != -1) {
3096 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3105 /* get pool name and check number of arguments */
3107 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3112 (void) fprintf(stderr,
3113 gettext("missing <device> specification\n"));
3120 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3123 ret = zpool_vdev_detach(zhp, path);
3131 * zpool split [-n] [-o prop=val] ...
3133 * [-R altroot] <pool> <newpool> [<device> ...]
3135 * -n Do not split the pool, but display the resulting layout if
3136 * it were to be split.
3137 * -o Set property=value, or set mount options.
3138 * -R Mount the split-off pool under an alternate root.
3140 * Splits the named pool and gives it the new pool name. Devices to be split
3141 * off may be listed, provided that no more than one device is specified
3142 * per top-level vdev mirror. The newly split pool is left in an exported
3143 * state unless -R is specified.
3145 * Restrictions: the top-level of the pool pool must only be made up of
3146 * mirrors; all devices in the pool must be healthy; no device may be
3147 * undergoing a resilvering operation.
3150 zpool_do_split(int argc, char **argv)
3152 char *srcpool, *newpool, *propval;
3153 char *mntopts = NULL;
3156 zpool_handle_t *zhp;
3157 nvlist_t *config, *props = NULL;
3159 flags.dryrun = B_FALSE;
3160 flags.import = B_FALSE;
3163 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3166 flags.import = B_TRUE;
3168 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3169 &props, B_TRUE) != 0) {
3176 flags.dryrun = B_TRUE;
3179 if ((propval = strchr(optarg, '=')) != NULL) {
3182 if (add_prop_list(optarg, propval,
3183 &props, B_TRUE) != 0) {
3193 (void) fprintf(stderr, gettext("missing argument for "
3194 "'%c' option\n"), optopt);
3198 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3205 if (!flags.import && mntopts != NULL) {
3206 (void) fprintf(stderr, gettext("setting mntopts is only "
3207 "valid when importing the pool\n"));
3215 (void) fprintf(stderr, gettext("Missing pool name\n"));
3219 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3229 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3232 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3233 if (config == NULL) {
3237 (void) printf(gettext("would create '%s' with the "
3238 "following layout:\n\n"), newpool);
3239 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3241 nvlist_free(config);
3246 if (ret != 0 || flags.dryrun || !flags.import)
3250 * The split was successful. Now we need to open the new
3251 * pool and import it.
3253 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3255 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3256 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3258 (void) fprintf(stderr, gettext("Split was succssful, but "
3259 "the datasets could not all be mounted\n"));
3260 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3261 "different altroot\n"), "zpool import");
3271 * zpool online <pool> <device> ...
3274 zpool_do_online(int argc, char **argv)
3278 zpool_handle_t *zhp;
3280 vdev_state_t newstate;
3284 while ((c = getopt(argc, argv, "et")) != -1) {
3287 flags |= ZFS_ONLINE_EXPAND;
3291 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3300 /* get pool name and check number of arguments */
3302 (void) fprintf(stderr, gettext("missing pool name\n"));
3306 (void) fprintf(stderr, gettext("missing device name\n"));
3312 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3315 for (i = 1; i < argc; i++) {
3316 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3317 if (newstate != VDEV_STATE_HEALTHY) {
3318 (void) printf(gettext("warning: device '%s' "
3319 "onlined, but remains in faulted state\n"),
3321 if (newstate == VDEV_STATE_FAULTED)
3322 (void) printf(gettext("use 'zpool "
3323 "clear' to restore a faulted "
3326 (void) printf(gettext("use 'zpool "
3327 "replace' to replace devices "
3328 "that are no longer present\n"));
3341 * zpool offline [-ft] <pool> <device> ...
3343 * -f Force the device into the offline state, even if doing
3344 * so would appear to compromise pool availability.
3345 * (not supported yet)
3347 * -t Only take the device off-line temporarily. The offline
3348 * state will not be persistent across reboots.
3352 zpool_do_offline(int argc, char **argv)
3356 zpool_handle_t *zhp;
3358 boolean_t istmp = B_FALSE;
3361 while ((c = getopt(argc, argv, "ft")) != -1) {
3368 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3377 /* get pool name and check number of arguments */
3379 (void) fprintf(stderr, gettext("missing pool name\n"));
3383 (void) fprintf(stderr, gettext("missing device name\n"));
3389 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3392 for (i = 1; i < argc; i++) {
3393 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3403 * zpool clear <pool> [device]
3405 * Clear all errors associated with a pool or a particular device.
3408 zpool_do_clear(int argc, char **argv)
3412 boolean_t dryrun = B_FALSE;
3413 boolean_t do_rewind = B_FALSE;
3414 boolean_t xtreme_rewind = B_FALSE;
3415 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3416 nvlist_t *policy = NULL;
3417 zpool_handle_t *zhp;
3418 char *pool, *device;
3421 while ((c = getopt(argc, argv, "FnX")) != -1) {
3430 xtreme_rewind = B_TRUE;
3433 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3443 (void) fprintf(stderr, gettext("missing pool name\n"));
3448 (void) fprintf(stderr, gettext("too many arguments\n"));
3452 if ((dryrun || xtreme_rewind) && !do_rewind) {
3453 (void) fprintf(stderr,
3454 gettext("-n or -X only meaningful with -F\n"));
3458 rewind_policy = ZPOOL_TRY_REWIND;
3460 rewind_policy = ZPOOL_DO_REWIND;
3462 rewind_policy |= ZPOOL_EXTREME_REWIND;
3464 /* In future, further rewind policy choices can be passed along here */
3465 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3466 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3470 device = argc == 2 ? argv[1] : NULL;
3472 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3473 nvlist_free(policy);
3477 if (zpool_clear(zhp, device, policy) != 0)
3482 nvlist_free(policy);
3488 * zpool reguid <pool>
3491 zpool_do_reguid(int argc, char **argv)
3495 zpool_handle_t *zhp;
3499 while ((c = getopt(argc, argv, "")) != -1) {
3502 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3511 /* get pool name and check number of arguments */
3513 (void) fprintf(stderr, gettext("missing pool name\n"));
3518 (void) fprintf(stderr, gettext("too many arguments\n"));
3523 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3526 ret = zpool_reguid(zhp);
3534 * zpool reopen <pool>
3536 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3538 * NOTE: This command is currently undocumented. If the command is ever
3539 * exposed then the appropriate usage() messages will need to be made.
3542 zpool_do_reopen(int argc, char **argv)
3545 zpool_handle_t *zhp;
3555 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3558 ret = zpool_reopen(zhp);
3563 typedef struct scrub_cbdata {
3570 scrub_callback(zpool_handle_t *zhp, void *data)
3572 scrub_cbdata_t *cb = data;
3576 * Ignore faulted pools.
3578 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3579 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3580 "currently unavailable\n"), zpool_get_name(zhp));
3584 err = zpool_scan(zhp, cb->cb_type);
3590 * zpool scrub [-s] <pool> ...
3592 * -s Stop. Stops any in-progress scrub.
3595 zpool_do_scrub(int argc, char **argv)
3600 cb.cb_type = POOL_SCAN_SCRUB;
3603 while ((c = getopt(argc, argv, "s")) != -1) {
3606 cb.cb_type = POOL_SCAN_NONE;
3609 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3621 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3625 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3628 typedef struct status_cbdata {
3630 boolean_t cb_allpools;
3631 boolean_t cb_verbose;
3632 boolean_t cb_explain;
3634 boolean_t cb_dedup_stats;
3638 * Print out detailed scrub status.
3641 print_scan_status(pool_scan_stat_t *ps)
3644 uint64_t elapsed, mins_left, hours_left;
3645 uint64_t pass_exam, examined, total;
3647 double fraction_done;
3648 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3650 (void) printf(gettext(" scan: "));
3652 /* If there's never been a scan, there's not much to say. */
3653 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3654 ps->pss_func >= POOL_SCAN_FUNCS) {
3655 (void) printf(gettext("none requested\n"));
3659 start = ps->pss_start_time;
3660 end = ps->pss_end_time;
3661 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3663 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3664 ps->pss_func == POOL_SCAN_RESILVER);
3666 * Scan is finished or canceled.
3668 if (ps->pss_state == DSS_FINISHED) {
3669 uint64_t minutes_taken = (end - start) / 60;
3672 if (ps->pss_func == POOL_SCAN_SCRUB) {
3673 fmt = gettext("scrub repaired %s in %lluh%um with "
3674 "%llu errors on %s");
3675 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3676 fmt = gettext("resilvered %s in %lluh%um with "
3677 "%llu errors on %s");
3680 (void) printf(fmt, processed_buf,
3681 (u_longlong_t)(minutes_taken / 60),
3682 (uint_t)(minutes_taken % 60),
3683 (u_longlong_t)ps->pss_errors,
3684 ctime((time_t *)&end));
3686 } else if (ps->pss_state == DSS_CANCELED) {
3687 if (ps->pss_func == POOL_SCAN_SCRUB) {
3688 (void) printf(gettext("scrub canceled on %s"),
3690 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3691 (void) printf(gettext("resilver canceled on %s"),
3697 assert(ps->pss_state == DSS_SCANNING);
3700 * Scan is in progress.
3702 if (ps->pss_func == POOL_SCAN_SCRUB) {
3703 (void) printf(gettext("scrub in progress since %s"),
3705 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3706 (void) printf(gettext("resilver in progress since %s"),
3710 examined = ps->pss_examined ? ps->pss_examined : 1;
3711 total = ps->pss_to_examine;
3712 fraction_done = (double)examined / total;
3714 /* elapsed time for this pass */
3715 elapsed = time(NULL) - ps->pss_pass_start;
3716 elapsed = elapsed ? elapsed : 1;
3717 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3718 rate = pass_exam / elapsed;
3719 rate = rate ? rate : 1;
3720 mins_left = ((total - examined) / rate) / 60;
3721 hours_left = mins_left / 60;
3723 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3724 zfs_nicenum(total, total_buf, sizeof (total_buf));
3725 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3728 * do not print estimated time if hours_left is more than 30 days
3730 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3731 examined_buf, total_buf, rate_buf);
3732 if (hours_left < (30 * 24)) {
3733 (void) printf(gettext(", %lluh%um to go\n"),
3734 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3736 (void) printf(gettext(
3737 ", (scan is slow, no estimated time)\n"));
3740 if (ps->pss_func == POOL_SCAN_RESILVER) {
3741 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3742 processed_buf, 100 * fraction_done);
3743 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3744 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3745 processed_buf, 100 * fraction_done);
3750 print_error_log(zpool_handle_t *zhp)
3752 nvlist_t *nverrlist = NULL;
3755 size_t len = MAXPATHLEN * 2;
3757 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3758 (void) printf("errors: List of errors unavailable "
3759 "(insufficient privileges)\n");
3763 (void) printf("errors: Permanent errors have been "
3764 "detected in the following files:\n\n");
3766 pathname = safe_malloc(len);
3768 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3770 uint64_t dsobj, obj;
3772 verify(nvpair_value_nvlist(elem, &nv) == 0);
3773 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3775 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3777 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3778 (void) printf("%7s %s\n", "", pathname);
3781 nvlist_free(nverrlist);
3785 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3794 (void) printf(gettext("\tspares\n"));
3796 for (i = 0; i < nspares; i++) {
3797 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3798 print_status_config(zhp, name, spares[i],
3799 namewidth, 2, B_TRUE);
3805 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3814 (void) printf(gettext("\tcache\n"));
3816 for (i = 0; i < nl2cache; i++) {
3817 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3818 print_status_config(zhp, name, l2cache[i],
3819 namewidth, 2, B_FALSE);
3825 print_dedup_stats(nvlist_t *config)
3827 ddt_histogram_t *ddh;
3833 * If the pool was faulted then we may not have been able to
3834 * obtain the config. Otherwise, if have anything in the dedup
3835 * table continue processing the stats.
3837 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3838 (uint64_t **)&ddo, &c) != 0)
3841 (void) printf("\n");
3842 (void) printf(gettext(" dedup: "));
3843 if (ddo->ddo_count == 0) {
3844 (void) printf(gettext("no DDT entries\n"));
3848 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3849 (u_longlong_t)ddo->ddo_count,
3850 (u_longlong_t)ddo->ddo_dspace,
3851 (u_longlong_t)ddo->ddo_mspace);
3853 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3854 (uint64_t **)&dds, &c) == 0);
3855 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3856 (uint64_t **)&ddh, &c) == 0);
3857 zpool_dump_ddt(dds, ddh);
3861 * Display a summary of pool status. Displays a summary such as:
3865 * reason: One or more devices ...
3866 * see: http://illumos.org/msg/ZFS-xxxx-01
3872 * When given the '-v' option, we print out the complete config. If the '-e'
3873 * option is specified, then we print out error rate information as well.
3876 status_callback(zpool_handle_t *zhp, void *data)
3878 status_cbdata_t *cbp = data;
3879 nvlist_t *config, *nvroot;
3886 config = zpool_get_config(zhp, NULL);
3887 reason = zpool_get_status(zhp, &msgid);
3892 * If we were given 'zpool status -x', only report those pools with
3895 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3896 if (!cbp->cb_allpools) {
3897 (void) printf(gettext("pool '%s' is healthy\n"),
3898 zpool_get_name(zhp));
3900 cbp->cb_first = B_FALSE;
3906 cbp->cb_first = B_FALSE;
3908 (void) printf("\n");
3910 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3912 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3913 (uint64_t **)&vs, &c) == 0);
3914 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3916 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3917 (void) printf(gettext(" state: %s\n"), health);
3920 case ZPOOL_STATUS_MISSING_DEV_R:
3921 (void) printf(gettext("status: One or more devices could not "
3922 "be opened. Sufficient replicas exist for\n\tthe pool to "
3923 "continue functioning in a degraded state.\n"));
3924 (void) printf(gettext("action: Attach the missing device and "
3925 "online it using 'zpool online'.\n"));
3928 case ZPOOL_STATUS_MISSING_DEV_NR:
3929 (void) printf(gettext("status: One or more devices could not "
3930 "be opened. There are insufficient\n\treplicas for the "
3931 "pool to continue functioning.\n"));
3932 (void) printf(gettext("action: Attach the missing device and "
3933 "online it using 'zpool online'.\n"));
3936 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3937 (void) printf(gettext("status: One or more devices could not "
3938 "be used because the label is missing or\n\tinvalid. "
3939 "Sufficient replicas exist for the pool to continue\n\t"
3940 "functioning in a degraded state.\n"));
3941 (void) printf(gettext("action: Replace the device using "
3942 "'zpool replace'.\n"));
3945 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3946 (void) printf(gettext("status: One or more devices could not "
3947 "be used because the label is missing \n\tor invalid. "
3948 "There are insufficient replicas for the pool to "
3949 "continue\n\tfunctioning.\n"));
3950 zpool_explain_recover(zpool_get_handle(zhp),
3951 zpool_get_name(zhp), reason, config);
3954 case ZPOOL_STATUS_FAILING_DEV:
3955 (void) printf(gettext("status: One or more devices has "
3956 "experienced an unrecoverable error. An\n\tattempt was "
3957 "made to correct the error. Applications are "
3959 (void) printf(gettext("action: Determine if the device needs "
3960 "to be replaced, and clear the errors\n\tusing "
3961 "'zpool clear' or replace the device with 'zpool "
3965 case ZPOOL_STATUS_OFFLINE_DEV:
3966 (void) printf(gettext("status: One or more devices has "
3967 "been taken offline by the administrator.\n\tSufficient "
3968 "replicas exist for the pool to continue functioning in "
3969 "a\n\tdegraded state.\n"));
3970 (void) printf(gettext("action: Online the device using "
3971 "'zpool online' or replace the device with\n\t'zpool "
3975 case ZPOOL_STATUS_REMOVED_DEV:
3976 (void) printf(gettext("status: One or more devices has "
3977 "been removed by the administrator.\n\tSufficient "
3978 "replicas exist for the pool to continue functioning in "
3979 "a\n\tdegraded state.\n"));
3980 (void) printf(gettext("action: Online the device using "
3981 "'zpool online' or replace the device with\n\t'zpool "
3985 case ZPOOL_STATUS_RESILVERING:
3986 (void) printf(gettext("status: One or more devices is "
3987 "currently being resilvered. The pool will\n\tcontinue "
3988 "to function, possibly in a degraded state.\n"));
3989 (void) printf(gettext("action: Wait for the resilver to "
3993 case ZPOOL_STATUS_CORRUPT_DATA:
3994 (void) printf(gettext("status: One or more devices has "
3995 "experienced an error resulting in data\n\tcorruption. "
3996 "Applications may be affected.\n"));
3997 (void) printf(gettext("action: Restore the file in question "
3998 "if possible. Otherwise restore the\n\tentire pool from "
4002 case ZPOOL_STATUS_CORRUPT_POOL:
4003 (void) printf(gettext("status: The pool metadata is corrupted "
4004 "and the pool cannot be opened.\n"));
4005 zpool_explain_recover(zpool_get_handle(zhp),
4006 zpool_get_name(zhp), reason, config);
4009 case ZPOOL_STATUS_VERSION_OLDER:
4010 (void) printf(gettext("status: The pool is formatted using an "
4011 "older on-disk format. The pool can\n\tstill be used, but "
4012 "some features are unavailable.\n"));
4013 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4014 "upgrade'. Once this is done, the\n\tpool will no longer "
4015 "be accessible on older software versions.\n"));
4018 case ZPOOL_STATUS_VERSION_NEWER:
4019 (void) printf(gettext("status: The pool has been upgraded to a "
4020 "newer, incompatible on-disk version.\n\tThe pool cannot "
4021 "be accessed on this system.\n"));
4022 (void) printf(gettext("action: Access the pool from a system "
4023 "running more recent software, or\n\trestore the pool from "
4027 case ZPOOL_STATUS_FAULTED_DEV_R:
4028 (void) printf(gettext("status: One or more devices are "
4029 "faulted in response to persistent errors.\n\tSufficient "
4030 "replicas exist for the pool to continue functioning "
4031 "in a\n\tdegraded state.\n"));
4032 (void) printf(gettext("action: Replace the faulted device, "
4033 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4036 case ZPOOL_STATUS_FAULTED_DEV_NR:
4037 (void) printf(gettext("status: One or more devices are "
4038 "faulted in response to persistent errors. There are "
4039 "insufficient replicas for the pool to\n\tcontinue "
4041 (void) printf(gettext("action: Destroy and re-create the pool "
4042 "from a backup source. Manually marking the device\n"
4043 "\trepaired using 'zpool clear' may allow some data "
4044 "to be recovered.\n"));
4047 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4048 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4049 (void) printf(gettext("status: One or more devices are "
4050 "faulted in response to IO failures.\n"));
4051 (void) printf(gettext("action: Make sure the affected devices "
4052 "are connected, then run 'zpool clear'.\n"));
4055 case ZPOOL_STATUS_BAD_LOG:
4056 (void) printf(gettext("status: An intent log record "
4057 "could not be read.\n"
4058 "\tWaiting for adminstrator intervention to fix the "
4059 "faulted pool.\n"));
4060 (void) printf(gettext("action: Either restore the affected "
4061 "device(s) and run 'zpool online',\n"
4062 "\tor ignore the intent log records by running "
4063 "'zpool clear'.\n"));
4068 * The remaining errors can't actually be generated, yet.
4070 assert(reason == ZPOOL_STATUS_OK);
4074 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4077 if (config != NULL) {
4080 nvlist_t **spares, **l2cache;
4081 uint_t nspares, nl2cache;
4082 pool_scan_stat_t *ps = NULL;
4084 (void) nvlist_lookup_uint64_array(nvroot,
4085 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4086 print_scan_status(ps);
4088 namewidth = max_width(zhp, nvroot, 0, 0);
4092 (void) printf(gettext("config:\n\n"));
4093 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4094 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4095 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4096 namewidth, 0, B_FALSE);
4098 if (num_logs(nvroot) > 0)
4099 print_logs(zhp, nvroot, namewidth, B_TRUE);
4100 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4101 &l2cache, &nl2cache) == 0)
4102 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4104 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4105 &spares, &nspares) == 0)
4106 print_spares(zhp, spares, nspares, namewidth);
4108 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4110 nvlist_t *nverrlist = NULL;
4113 * If the approximate error count is small, get a
4114 * precise count by fetching the entire log and
4115 * uniquifying the results.
4117 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4118 zpool_get_errlog(zhp, &nverrlist) == 0) {
4123 while ((elem = nvlist_next_nvpair(nverrlist,
4128 nvlist_free(nverrlist);
4130 (void) printf("\n");
4133 (void) printf(gettext("errors: No known data "
4135 else if (!cbp->cb_verbose)
4136 (void) printf(gettext("errors: %llu data "
4137 "errors, use '-v' for a list\n"),
4138 (u_longlong_t)nerr);
4140 print_error_log(zhp);
4143 if (cbp->cb_dedup_stats)
4144 print_dedup_stats(config);
4146 (void) printf(gettext("config: The configuration cannot be "
4154 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4156 * -v Display complete error logs
4157 * -x Display only pools with potential problems
4158 * -D Display dedup status (undocumented)
4159 * -T Display a timestamp in date(1) or Unix format
4161 * Describes the health status of all pools or some subset.
4164 zpool_do_status(int argc, char **argv)
4168 unsigned long interval = 0, count = 0;
4169 status_cbdata_t cb = { 0 };
4172 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4175 cb.cb_verbose = B_TRUE;
4178 cb.cb_explain = B_TRUE;
4181 cb.cb_dedup_stats = B_TRUE;
4184 get_timestamp_arg(*optarg);
4187 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4196 get_interval_count(&argc, argv, &interval, &count);
4199 cb.cb_allpools = B_TRUE;
4201 cb.cb_first = B_TRUE;
4204 if (timestamp_fmt != NODATE)
4205 print_timestamp(timestamp_fmt);
4207 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4208 status_callback, &cb);
4210 if (argc == 0 && cb.cb_count == 0)
4211 (void) printf(gettext("no pools available\n"));
4212 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4213 (void) printf(gettext("all pools are healthy\n"));
4221 if (count != 0 && --count == 0)
4224 (void) sleep(interval);
4230 typedef struct upgrade_cbdata {
4234 char cb_poolname[ZPOOL_MAXNAMELEN];
4236 uint64_t cb_version;
4241 is_root_pool(zpool_handle_t *zhp)
4243 static struct statfs sfs;
4244 static char *poolname = NULL;
4245 static boolean_t stated = B_FALSE;
4250 if (statfs("/", &sfs) == -1) {
4251 (void) fprintf(stderr,
4252 "Unable to stat root file system: %s.\n",
4256 if (strcmp(sfs.f_fstypename, "zfs") != 0)
4258 poolname = sfs.f_mntfromname;
4259 if ((slash = strchr(poolname, '/')) != NULL)
4262 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
4266 upgrade_cb(zpool_handle_t *zhp, void *arg)
4268 upgrade_cbdata_t *cbp = arg;
4273 config = zpool_get_config(zhp, NULL);
4274 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4277 if (!cbp->cb_newer && version < SPA_VERSION) {
4279 if (cbp->cb_first) {
4280 (void) printf(gettext("The following pools are "
4281 "out of date, and can be upgraded. After "
4282 "being\nupgraded, these pools will no "
4283 "longer be accessible by older software "
4285 (void) printf(gettext("VER POOL\n"));
4286 (void) printf(gettext("--- ------------\n"));
4287 cbp->cb_first = B_FALSE;
4290 (void) printf("%2llu %s\n", (u_longlong_t)version,
4291 zpool_get_name(zhp));
4293 cbp->cb_first = B_FALSE;
4294 ret = zpool_upgrade(zhp, cbp->cb_version);
4296 (void) printf(gettext("Successfully upgraded "
4297 "'%s'\n\n"), zpool_get_name(zhp));
4298 if (cbp->cb_poolname[0] == '\0' &&
4299 is_root_pool(zhp)) {
4300 (void) strlcpy(cbp->cb_poolname,
4301 zpool_get_name(zhp),
4302 sizeof(cbp->cb_poolname));
4306 } else if (cbp->cb_newer && version > SPA_VERSION) {
4307 assert(!cbp->cb_all);
4309 if (cbp->cb_first) {
4310 (void) printf(gettext("The following pools are "
4311 "formatted using a newer software version and\n"
4312 "cannot be accessed on the current system.\n\n"));
4313 (void) printf(gettext("VER POOL\n"));
4314 (void) printf(gettext("--- ------------\n"));
4315 cbp->cb_first = B_FALSE;
4318 (void) printf("%2llu %s\n", (u_longlong_t)version,
4319 zpool_get_name(zhp));
4328 upgrade_one(zpool_handle_t *zhp, void *data)
4330 upgrade_cbdata_t *cbp = data;
4331 uint64_t cur_version;
4334 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4335 (void) printf(gettext("'log' is now a reserved word\n"
4336 "Pool 'log' must be renamed using export and import"
4341 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4342 if (cur_version > cbp->cb_version) {
4343 (void) printf(gettext("Pool '%s' is already formatted "
4344 "using more current version '%llu'.\n"),
4345 zpool_get_name(zhp), cur_version);
4348 if (cur_version == cbp->cb_version) {
4349 (void) printf(gettext("Pool '%s' is already formatted "
4350 "using the current version.\n"), zpool_get_name(zhp));
4354 ret = zpool_upgrade(zhp, cbp->cb_version);
4357 (void) printf(gettext("Successfully upgraded '%s' "
4358 "from version %llu to version %llu\n\n"),
4359 zpool_get_name(zhp), (u_longlong_t)cur_version,
4360 (u_longlong_t)cbp->cb_version);
4361 if (cbp->cb_poolname[0] == '\0' && is_root_pool(zhp)) {
4362 (void) strlcpy(cbp->cb_poolname, zpool_get_name(zhp),
4363 sizeof(cbp->cb_poolname));
4373 * zpool upgrade [-V version] <-a | pool ...>
4375 * With no arguments, display downrev'd ZFS pool available for upgrade.
4376 * Individual pools can be upgraded by specifying the pool, and '-a' will
4377 * upgrade all pools.
4380 zpool_do_upgrade(int argc, char **argv)
4383 upgrade_cbdata_t cb = { 0 };
4385 boolean_t showversions = B_FALSE;
4390 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4396 showversions = B_TRUE;
4399 cb.cb_version = strtoll(optarg, &end, 10);
4400 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4401 cb.cb_version < SPA_VERSION_1) {
4402 (void) fprintf(stderr,
4403 gettext("invalid version '%s'\n"), optarg);
4408 (void) fprintf(stderr, gettext("missing argument for "
4409 "'%c' option\n"), optopt);
4413 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4424 if (cb.cb_version == 0) {
4425 cb.cb_version = SPA_VERSION;
4426 } else if (!cb.cb_all && argc == 0) {
4427 (void) fprintf(stderr, gettext("-V option is "
4428 "incompatible with other arguments\n"));
4433 if (cb.cb_all || argc != 0) {
4434 (void) fprintf(stderr, gettext("-v option is "
4435 "incompatible with other arguments\n"));
4438 } else if (cb.cb_all) {
4440 (void) fprintf(stderr, gettext("-a option should not "
4441 "be used along with a pool name\n"));
4446 (void) printf(gettext("This system is currently running "
4447 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4448 cb.cb_first = B_TRUE;
4450 (void) printf(gettext("The following versions are "
4452 (void) printf(gettext("VER DESCRIPTION\n"));
4453 (void) printf("--- -----------------------------------------"
4454 "---------------\n");
4455 (void) printf(gettext(" 1 Initial ZFS version\n"));
4456 (void) printf(gettext(" 2 Ditto blocks "
4457 "(replicated metadata)\n"));
4458 (void) printf(gettext(" 3 Hot spares and double parity "
4460 (void) printf(gettext(" 4 zpool history\n"));
4461 (void) printf(gettext(" 5 Compression using the gzip "
4463 (void) printf(gettext(" 6 bootfs pool property\n"));
4464 (void) printf(gettext(" 7 Separate intent log devices\n"));
4465 (void) printf(gettext(" 8 Delegated administration\n"));
4466 (void) printf(gettext(" 9 refquota and refreservation "
4468 (void) printf(gettext(" 10 Cache devices\n"));
4469 (void) printf(gettext(" 11 Improved scrub performance\n"));
4470 (void) printf(gettext(" 12 Snapshot properties\n"));
4471 (void) printf(gettext(" 13 snapused property\n"));
4472 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4473 (void) printf(gettext(" 15 user/group space accounting\n"));
4474 (void) printf(gettext(" 16 stmf property support\n"));
4475 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4476 (void) printf(gettext(" 18 Snapshot user holds\n"));
4477 (void) printf(gettext(" 19 Log device removal\n"));
4478 (void) printf(gettext(" 20 Compression using zle "
4479 "(zero-length encoding)\n"));
4480 (void) printf(gettext(" 21 Deduplication\n"));
4481 (void) printf(gettext(" 22 Received properties\n"));
4482 (void) printf(gettext(" 23 Slim ZIL\n"));
4483 (void) printf(gettext(" 24 System attributes\n"));
4484 (void) printf(gettext(" 25 Improved scrub stats\n"));
4485 (void) printf(gettext(" 26 Improved snapshot deletion "
4487 (void) printf(gettext(" 27 Improved snapshot creation "
4489 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4490 (void) printf(gettext("\nFor more information on a particular "
4491 "version, including supported releases,\n"));
4492 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4493 } else if (argc == 0) {
4496 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4497 notfound = cb.cb_first;
4499 if (!cb.cb_all && ret == 0) {
4501 (void) printf("\n");
4502 cb.cb_first = B_TRUE;
4503 cb.cb_newer = B_TRUE;
4504 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4507 (void) printf("\n");
4513 (void) printf(gettext("All pools are formatted "
4514 "using this version.\n"));
4515 else if (!cb.cb_all)
4516 (void) printf(gettext("Use 'zpool upgrade -v' "
4517 "for a list of available versions and "
4518 "their associated\nfeatures.\n"));
4521 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4525 if (cb.cb_poolname[0] != '\0') {
4527 "If you boot from pool '%s', don't forget to update boot code.\n"
4528 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4529 "the following command will do it:\n"
4531 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4538 typedef struct hist_cbdata {
4545 * Print out the command history for a specific pool.
4548 get_history_one(zpool_handle_t *zhp, void *data)
4564 char internalstr[MAXPATHLEN];
4565 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4569 cb->first = B_FALSE;
4571 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4573 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4576 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4577 &records, &numrecords) == 0);
4578 for (i = 0; i < numrecords; i++) {
4579 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4583 /* is it an internal event or a standard event? */
4584 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4586 if (cb->internal == 0)
4589 if (nvlist_lookup_uint64(records[i],
4590 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4592 verify(nvlist_lookup_uint64(records[i],
4593 ZPOOL_HIST_TXG, &txg) == 0);
4594 verify(nvlist_lookup_string(records[i],
4595 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4596 if (ievent >= LOG_END)
4598 (void) snprintf(internalstr,
4599 sizeof (internalstr),
4600 "[internal %s txg:%lld] %s",
4601 zfs_history_event_names[ievent], txg,
4603 cmdstr = internalstr;
4606 (void) localtime_r(&tsec, &t);
4607 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4608 (void) printf("%s %s", tbuf, cmdstr);
4611 (void) printf("\n");
4614 (void) printf(" [");
4615 if (nvlist_lookup_uint64(records[i],
4616 ZPOOL_HIST_WHO, &who) == 0) {
4617 pwd = getpwuid((uid_t)who);
4619 (void) printf("user %s on",
4622 (void) printf("user %d on",
4625 (void) printf(gettext("no info]\n"));
4628 if (nvlist_lookup_string(records[i],
4629 ZPOOL_HIST_HOST, &hostname) == 0) {
4630 (void) printf(" %s", hostname);
4632 if (nvlist_lookup_string(records[i],
4633 ZPOOL_HIST_ZONE, &zonename) == 0) {
4634 (void) printf(":%s", zonename);
4638 (void) printf("\n");
4640 (void) printf("\n");
4647 * zpool history <pool>
4649 * Displays the history of commands that modified pools.
4654 zpool_do_history(int argc, char **argv)
4656 hist_cbdata_t cbdata = { 0 };
4660 cbdata.first = B_TRUE;
4662 while ((c = getopt(argc, argv, "li")) != -1) {
4668 cbdata.internal = 1;
4671 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4679 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4682 if (argc == 0 && cbdata.first == B_TRUE) {
4683 (void) printf(gettext("no pools available\n"));
4691 get_callback(zpool_handle_t *zhp, void *data)
4693 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4694 char value[MAXNAMELEN];
4695 zprop_source_t srctype;
4698 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4701 * Skip the special fake placeholder. This will also skip
4702 * over the name property when 'all' is specified.
4704 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4705 pl == cbp->cb_proplist)
4708 if (zpool_get_prop(zhp, pl->pl_prop,
4709 value, sizeof (value), &srctype) != 0)
4712 zprop_print_one_property(zpool_get_name(zhp), cbp,
4713 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4720 zpool_do_get(int argc, char **argv)
4722 zprop_get_cbdata_t cb = { 0 };
4723 zprop_list_t fake_name = { 0 };
4729 cb.cb_first = B_TRUE;
4730 cb.cb_sources = ZPROP_SRC_ALL;
4731 cb.cb_columns[0] = GET_COL_NAME;
4732 cb.cb_columns[1] = GET_COL_PROPERTY;
4733 cb.cb_columns[2] = GET_COL_VALUE;
4734 cb.cb_columns[3] = GET_COL_SOURCE;
4735 cb.cb_type = ZFS_TYPE_POOL;
4737 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4738 ZFS_TYPE_POOL) != 0)
4741 if (cb.cb_proplist != NULL) {
4742 fake_name.pl_prop = ZPOOL_PROP_NAME;
4743 fake_name.pl_width = strlen(gettext("NAME"));
4744 fake_name.pl_next = cb.cb_proplist;
4745 cb.cb_proplist = &fake_name;
4748 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4751 if (cb.cb_proplist == &fake_name)
4752 zprop_free_list(fake_name.pl_next);
4754 zprop_free_list(cb.cb_proplist);
4759 typedef struct set_cbdata {
4762 boolean_t cb_any_successful;
4766 set_callback(zpool_handle_t *zhp, void *data)
4769 set_cbdata_t *cb = (set_cbdata_t *)data;
4771 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4774 cb->cb_any_successful = B_TRUE;
4780 zpool_do_set(int argc, char **argv)
4782 set_cbdata_t cb = { 0 };
4785 if (argc > 1 && argv[1][0] == '-') {
4786 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4792 (void) fprintf(stderr, gettext("missing property=value "
4798 (void) fprintf(stderr, gettext("missing pool name\n"));
4803 (void) fprintf(stderr, gettext("too many pool names\n"));
4807 cb.cb_propname = argv[1];
4808 cb.cb_value = strchr(cb.cb_propname, '=');
4809 if (cb.cb_value == NULL) {
4810 (void) fprintf(stderr, gettext("missing value in "
4811 "property=value argument\n"));
4815 *(cb.cb_value) = '\0';
4818 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4825 find_command_idx(char *command, int *idx)
4829 for (i = 0; i < NCOMMAND; i++) {
4830 if (command_table[i].name == NULL)
4833 if (strcmp(command, command_table[i].name) == 0) {
4842 main(int argc, char **argv)
4848 (void) setlocale(LC_ALL, "");
4849 (void) textdomain(TEXT_DOMAIN);
4851 if ((g_zfs = libzfs_init()) == NULL) {
4852 (void) fprintf(stderr, gettext("internal error: failed to "
4853 "initialize ZFS library\n"));
4857 libzfs_print_on_error(g_zfs, B_TRUE);
4862 * Make sure the user has specified some command.
4865 (void) fprintf(stderr, gettext("missing command\n"));
4874 if (strcmp(cmdname, "-?") == 0)
4877 zpool_set_history_str("zpool", argc, argv, history_str);
4878 verify(zpool_stage_history(g_zfs, history_str) == 0);
4881 * Run the appropriate command.
4883 if (find_command_idx(cmdname, &i) == 0) {
4884 current_command = &command_table[i];
4885 ret = command_table[i].func(argc - 1, argv + 1);
4886 } else if (strchr(cmdname, '=')) {
4887 verify(find_command_idx("set", &i) == 0);
4888 current_command = &command_table[i];
4889 ret = command_table[i].func(argc, argv);
4890 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4892 * 'freeze' is a vile debugging abomination, so we treat
4896 int fd = open(ZFS_DEV, O_RDWR);
4897 (void) strcpy((void *)buf, argv[2]);
4898 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4900 (void) fprintf(stderr, gettext("unrecognized "
4901 "command '%s'\n"), cmdname);
4908 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4909 * for the purposes of running ::findleaks.
4911 if (getenv("ZFS_ABORT") != NULL) {
4912 (void) printf("dumping core by request\n");