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.
45 #include <sys/fs/zfs.h>
50 #include "zpool_util.h"
51 #include "zfs_comutil.h"
53 #include "statcommon.h"
55 static int zpool_do_create(int, char **);
56 static int zpool_do_destroy(int, char **);
58 static int zpool_do_add(int, char **);
59 static int zpool_do_remove(int, char **);
60 static int zpool_do_labelclear(int, char **);
62 static int zpool_do_list(int, char **);
63 static int zpool_do_iostat(int, char **);
64 static int zpool_do_status(int, char **);
66 static int zpool_do_online(int, char **);
67 static int zpool_do_offline(int, char **);
68 static int zpool_do_clear(int, char **);
70 static int zpool_do_attach(int, char **);
71 static int zpool_do_detach(int, char **);
72 static int zpool_do_replace(int, char **);
73 static int zpool_do_split(int, char **);
75 static int zpool_do_scrub(int, char **);
77 static int zpool_do_import(int, char **);
78 static int zpool_do_export(int, char **);
80 static int zpool_do_upgrade(int, char **);
82 static int zpool_do_history(int, char **);
84 static int zpool_do_get(int, char **);
85 static int zpool_do_set(int, char **);
88 * These libumem hooks provide a reasonable set of defaults for the allocator's
89 * debugging facilities.
94 _umem_debug_init(void)
96 return ("default,verbose"); /* $UMEM_DEBUG setting */
100 _umem_logging_init(void)
102 return ("fail,contents"); /* $UMEM_LOGGING setting */
132 typedef struct zpool_command {
134 int (*func)(int, char **);
139 * Master command table. Each ZFS command has a name, associated function, and
140 * usage message. The usage messages need to be internationalized, so we have
141 * to have a function to return the usage message based on a command index.
143 * These commands are organized according to how they are displayed in the usage
144 * message. An empty command (one with a NULL name) indicates an empty line in
145 * the generic usage message.
147 static zpool_command_t command_table[] = {
148 { "create", zpool_do_create, HELP_CREATE },
149 { "destroy", zpool_do_destroy, HELP_DESTROY },
151 { "add", zpool_do_add, HELP_ADD },
152 { "remove", zpool_do_remove, HELP_REMOVE },
154 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
156 { "list", zpool_do_list, HELP_LIST },
157 { "iostat", zpool_do_iostat, HELP_IOSTAT },
158 { "status", zpool_do_status, HELP_STATUS },
160 { "online", zpool_do_online, HELP_ONLINE },
161 { "offline", zpool_do_offline, HELP_OFFLINE },
162 { "clear", zpool_do_clear, HELP_CLEAR },
164 { "attach", zpool_do_attach, HELP_ATTACH },
165 { "detach", zpool_do_detach, HELP_DETACH },
166 { "replace", zpool_do_replace, HELP_REPLACE },
167 { "split", zpool_do_split, HELP_SPLIT },
169 { "scrub", zpool_do_scrub, HELP_SCRUB },
171 { "import", zpool_do_import, HELP_IMPORT },
172 { "export", zpool_do_export, HELP_EXPORT },
173 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
175 { "history", zpool_do_history, HELP_HISTORY },
176 { "get", zpool_do_get, HELP_GET },
177 { "set", zpool_do_set, HELP_SET },
180 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
182 zpool_command_t *current_command;
183 static char history_str[HIS_MAX_RECORD_LEN];
185 static uint_t timestamp_fmt = NODATE;
188 get_usage(zpool_help_t idx) {
191 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
193 return (gettext("\tattach [-f] <pool> <device> "
196 return (gettext("\tclear [-nF] <pool> [device]\n"));
198 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
199 "\t [-O file-system-property=value] ... \n"
200 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
202 return (gettext("\tdestroy [-f] <pool>\n"));
204 return (gettext("\tdetach <pool> <device>\n"));
206 return (gettext("\texport [-f] <pool> ...\n"));
208 return (gettext("\thistory [-il] [<pool>] ...\n"));
210 return (gettext("\timport [-d dir] [-D]\n"
211 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
212 "\timport [-o mntopts] [-o property=value] ... \n"
213 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
214 "[-R root] [-F [-n]] -a\n"
215 "\timport [-o mntopts] [-o property=value] ... \n"
216 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
217 "[-R root] [-F [-n]]\n"
218 "\t <pool | id> [newpool]\n"));
220 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
222 case HELP_LABELCLEAR:
223 return (gettext("\tlabelclear [-f] <vdev>\n"));
225 return (gettext("\tlist [-H] [-o property[,...]] "
226 "[-T d|u] [pool] ... [interval [count]]\n"));
228 return (gettext("\toffline [-t] <pool> <device> ...\n"));
230 return (gettext("\tonline <pool> <device> ...\n"));
232 return (gettext("\treplace [-f] <pool> <device> "
235 return (gettext("\tremove <pool> <device> ...\n"));
237 return (gettext("\tscrub [-s] <pool> ...\n"));
239 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
242 return (gettext("\tupgrade\n"
244 "\tupgrade [-V version] <-a | pool ...>\n"));
246 return (gettext("\tget <\"all\" | property[,...]> "
249 return (gettext("\tset <property=value> <pool> \n"));
251 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
252 "\t [-o property=value] <pool> <newpool> "
253 "[<device> ...]\n"));
262 * Callback routine that will print out a pool property value.
265 print_prop_cb(int prop, void *cb)
269 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
271 if (zpool_prop_readonly(prop))
272 (void) fprintf(fp, " NO ");
274 (void) fprintf(fp, " YES ");
276 if (zpool_prop_values(prop) == NULL)
277 (void) fprintf(fp, "-\n");
279 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
285 * Display usage message. If we're inside a command, display only the usage for
286 * that command. Otherwise, iterate over the entire command table and display
287 * a complete usage message.
290 usage(boolean_t requested)
292 FILE *fp = requested ? stdout : stderr;
294 if (current_command == NULL) {
297 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
299 gettext("where 'command' is one of the following:\n\n"));
301 for (i = 0; i < NCOMMAND; i++) {
302 if (command_table[i].name == NULL)
303 (void) fprintf(fp, "\n");
305 (void) fprintf(fp, "%s",
306 get_usage(command_table[i].usage));
309 (void) fprintf(fp, gettext("usage:\n"));
310 (void) fprintf(fp, "%s", get_usage(current_command->usage));
313 if (current_command != NULL &&
314 ((strcmp(current_command->name, "set") == 0) ||
315 (strcmp(current_command->name, "get") == 0) ||
316 (strcmp(current_command->name, "list") == 0))) {
319 gettext("\nthe following properties are supported:\n"));
321 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
322 "PROPERTY", "EDIT", "VALUES");
324 /* Iterate over all properties */
325 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
330 * See comments at end of main().
332 if (getenv("ZFS_ABORT") != NULL) {
333 (void) printf("dumping core by request\n");
337 exit(requested ? 0 : 2);
341 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
342 boolean_t print_logs)
349 (void) printf("\t%*s%s\n", indent, "", name);
351 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
352 &child, &children) != 0)
355 for (c = 0; c < children; c++) {
356 uint64_t is_log = B_FALSE;
358 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
360 if ((is_log && !print_logs) || (!is_log && print_logs))
363 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
364 print_vdev_tree(zhp, vname, child[c], indent + 2,
371 * Add a property pair (name, string-value) into a property nvlist.
374 add_prop_list(const char *propname, char *propval, nvlist_t **props,
377 zpool_prop_t prop = ZPROP_INVAL;
383 if (*props == NULL &&
384 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
385 (void) fprintf(stderr,
386 gettext("internal error: out of memory\n"));
393 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
394 (void) fprintf(stderr, gettext("property '%s' is "
395 "not a valid pool property\n"), propname);
398 normnm = zpool_prop_to_name(prop);
400 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
401 normnm = zfs_prop_to_name(fprop);
407 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
408 prop != ZPOOL_PROP_CACHEFILE) {
409 (void) fprintf(stderr, gettext("property '%s' "
410 "specified multiple times\n"), propname);
414 if (nvlist_add_string(proplist, normnm, propval) != 0) {
415 (void) fprintf(stderr, gettext("internal "
416 "error: out of memory\n"));
424 * zpool add [-fn] <pool> <vdev> ...
426 * -f Force addition of devices, even if they appear in use
427 * -n Do not add the devices, but display the resulting layout if
428 * they were to be added.
430 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
431 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
435 zpool_do_add(int argc, char **argv)
437 boolean_t force = B_FALSE;
438 boolean_t dryrun = B_FALSE;
447 while ((c = getopt(argc, argv, "fn")) != -1) {
456 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
465 /* get pool name and check number of arguments */
467 (void) fprintf(stderr, gettext("missing pool name argument\n"));
471 (void) fprintf(stderr, gettext("missing vdev specification\n"));
480 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
483 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
484 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
490 /* pass off to get_vdev_spec for processing */
491 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
493 if (nvroot == NULL) {
499 nvlist_t *poolnvroot;
501 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
504 (void) printf(gettext("would update '%s' to the following "
505 "configuration:\n"), zpool_get_name(zhp));
507 /* print original main pool and new tree */
508 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
509 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
511 /* Do the same for the logs */
512 if (num_logs(poolnvroot) > 0) {
513 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
514 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
515 } else if (num_logs(nvroot) > 0) {
516 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
521 ret = (zpool_add(zhp, nvroot) != 0);
531 * zpool remove <pool> <vdev> ...
533 * Removes the given vdev from the pool. Currently, this supports removing
534 * spares, cache, and log devices from the pool.
537 zpool_do_remove(int argc, char **argv)
546 /* get pool name and check number of arguments */
548 (void) fprintf(stderr, gettext("missing pool name argument\n"));
552 (void) fprintf(stderr, gettext("missing device\n"));
558 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
561 for (i = 1; i < argc; i++) {
562 if (zpool_vdev_remove(zhp, argv[i]) != 0)
570 * zpool labelclear <vdev>
572 * Verifies that the vdev is not active and zeros out the label information
576 zpool_do_labelclear(int argc, char **argv)
579 int c, fd = -1, ret = 0;
581 boolean_t inuse = B_FALSE;
582 boolean_t force = B_FALSE;
585 while ((c = getopt(argc, argv, "f")) != -1) {
591 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
602 (void) fprintf(stderr, gettext("missing vdev device name\n"));
607 if ((fd = open(vdev, O_RDWR)) < 0) {
608 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
613 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
617 (void) fprintf(stderr,
618 gettext("Unable to determine pool state for %s\n"
619 "Use -f to force the clearing any label data\n"), vdev);
627 case POOL_STATE_ACTIVE:
628 case POOL_STATE_SPARE:
629 case POOL_STATE_L2CACHE:
630 (void) fprintf(stderr,
631 gettext("labelclear operation failed.\n"
632 "\tVdev %s is a member (%s), of pool \"%s\".\n"
633 "\tTo remove label information from this device, export or destroy\n"
634 "\tthe pool, or remove %s from the configuration of this pool\n"
635 "\tand retry the labelclear operation\n"),
636 vdev, zpool_pool_state_to_name(state), name, vdev);
640 case POOL_STATE_EXPORTED:
644 (void) fprintf(stderr,
645 gettext("labelclear operation failed.\n"
646 "\tVdev %s is a member of the exported pool \"%s\".\n"
647 "\tUse \"zpool labelclear -f %s\" to force the removal of label\n"
653 case POOL_STATE_POTENTIALLY_ACTIVE:
657 (void) fprintf(stderr,
658 gettext("labelclear operation failed.\n"
659 "\tVdev %s is a member of the pool \"%s\".\n"
660 "\tThis pool is unknown to this system, but may be active on\n"
661 "\tanother system. Use \'zpool labelclear -f %s\' to force the\n"
662 "\tremoval of label information.\n"),
667 case POOL_STATE_DESTROYED:
668 /* inuse should never be set for a destoryed pool... */
674 if (zpool_clear_label(fd) != 0) {
675 (void) fprintf(stderr,
676 gettext("Label clear failed on vdev %s\n"), vdev);
689 * zpool create [-fn] [-o property=value] ...
690 * [-O file-system-property=value] ...
691 * [-R root] [-m mountpoint] <pool> <dev> ...
693 * -f Force creation, even if devices appear in use
694 * -n Do not create the pool, but display the resulting layout if it
695 * were to be created.
696 * -R Create a pool under an alternate root
697 * -m Set default mountpoint for the root dataset. By default it's
699 * -o Set property=value.
700 * -O Set fsproperty=value in the pool's root file system
702 * Creates the named pool according to the given vdev specification. The
703 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
704 * we get the nvlist back from get_vdev_spec(), we either print out the contents
705 * (if '-n' was specified), or pass it to libzfs to do the creation.
708 zpool_do_create(int argc, char **argv)
710 boolean_t force = B_FALSE;
711 boolean_t dryrun = B_FALSE;
713 nvlist_t *nvroot = NULL;
716 char *altroot = NULL;
717 char *mountpoint = NULL;
718 nvlist_t *fsprops = NULL;
719 nvlist_t *props = NULL;
723 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
733 if (add_prop_list(zpool_prop_to_name(
734 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
736 if (nvlist_lookup_string(props,
737 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
740 if (add_prop_list(zpool_prop_to_name(
741 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
748 if ((propval = strchr(optarg, '=')) == NULL) {
749 (void) fprintf(stderr, gettext("missing "
750 "'=' for -o option\n"));
756 if (add_prop_list(optarg, propval, &props, B_TRUE))
760 if ((propval = strchr(optarg, '=')) == NULL) {
761 (void) fprintf(stderr, gettext("missing "
762 "'=' for -O option\n"));
768 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
772 (void) fprintf(stderr, gettext("missing argument for "
773 "'%c' option\n"), optopt);
776 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
785 /* get pool name and check number of arguments */
787 (void) fprintf(stderr, gettext("missing pool name argument\n"));
791 (void) fprintf(stderr, gettext("missing vdev specification\n"));
798 * As a special case, check for use of '/' in the name, and direct the
799 * user to use 'zfs create' instead.
801 if (strchr(poolname, '/') != NULL) {
802 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
803 "character '/' in pool name\n"), poolname);
804 (void) fprintf(stderr, gettext("use 'zfs create' to "
805 "create a dataset\n"));
809 /* pass off to get_vdev_spec for bulk processing */
810 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
815 /* make_root_vdev() allows 0 toplevel children if there are spares */
816 if (!zfs_allocatable_devs(nvroot)) {
817 (void) fprintf(stderr, gettext("invalid vdev "
818 "specification: at least one toplevel vdev must be "
824 if (altroot != NULL && altroot[0] != '/') {
825 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
826 "must be an absolute path\n"), altroot);
831 * Check the validity of the mountpoint and direct the user to use the
832 * '-m' mountpoint option if it looks like its in use.
834 if (mountpoint == NULL ||
835 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
836 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
837 char buf[MAXPATHLEN];
840 if (mountpoint && mountpoint[0] != '/') {
841 (void) fprintf(stderr, gettext("invalid mountpoint "
842 "'%s': must be an absolute path, 'legacy', or "
843 "'none'\n"), mountpoint);
847 if (mountpoint == NULL) {
849 (void) snprintf(buf, sizeof (buf), "%s/%s",
852 (void) snprintf(buf, sizeof (buf), "/%s",
856 (void) snprintf(buf, sizeof (buf), "%s%s",
857 altroot, mountpoint);
859 (void) snprintf(buf, sizeof (buf), "%s",
863 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
864 (void) fprintf(stderr, gettext("mountpoint '%s' : "
865 "%s\n"), buf, strerror(errno));
866 (void) fprintf(stderr, gettext("use '-m' "
867 "option to provide a different default\n"));
872 while (count < 3 && readdir(dirp) != NULL)
874 (void) closedir(dirp);
877 (void) fprintf(stderr, gettext("mountpoint "
878 "'%s' exists and is not empty\n"), buf);
879 (void) fprintf(stderr, gettext("use '-m' "
880 "option to provide a "
881 "different default\n"));
889 * For a dry run invocation, print out a basic message and run
890 * through all the vdevs in the list and print out in an
891 * appropriate hierarchy.
893 (void) printf(gettext("would create '%s' with the "
894 "following layout:\n\n"), poolname);
896 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
897 if (num_logs(nvroot) > 0)
898 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
903 * Hand off to libzfs.
905 if (zpool_create(g_zfs, poolname,
906 nvroot, props, fsprops) == 0) {
907 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
908 ZFS_TYPE_FILESYSTEM);
910 if (mountpoint != NULL)
911 verify(zfs_prop_set(pool,
913 ZFS_PROP_MOUNTPOINT),
915 if (zfs_mount(pool, NULL, 0) == 0)
916 ret = zfs_shareall(pool);
919 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
920 (void) fprintf(stderr, gettext("pool name may have "
927 nvlist_free(fsprops);
931 nvlist_free(fsprops);
938 * zpool destroy <pool>
940 * -f Forcefully unmount any datasets
942 * Destroy the given pool. Automatically unmounts any datasets in the pool.
945 zpool_do_destroy(int argc, char **argv)
947 boolean_t force = B_FALSE;
954 while ((c = getopt(argc, argv, "f")) != -1) {
960 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
969 /* check arguments */
971 (void) fprintf(stderr, gettext("missing pool argument\n"));
975 (void) fprintf(stderr, gettext("too many arguments\n"));
981 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
983 * As a special case, check for use of '/' in the name, and
984 * direct the user to use 'zfs destroy' instead.
986 if (strchr(pool, '/') != NULL)
987 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
988 "destroy a dataset\n"));
992 if (zpool_disable_datasets(zhp, force) != 0) {
993 (void) fprintf(stderr, gettext("could not destroy '%s': "
994 "could not unmount datasets\n"), zpool_get_name(zhp));
998 ret = (zpool_destroy(zhp) != 0);
1006 * zpool export [-f] <pool> ...
1008 * -f Forcefully unmount datasets
1010 * Export the given pools. By default, the command will attempt to cleanly
1011 * unmount any active datasets within the pool. If the '-f' flag is specified,
1012 * then the datasets will be forcefully unmounted.
1015 zpool_do_export(int argc, char **argv)
1017 boolean_t force = B_FALSE;
1018 boolean_t hardforce = B_FALSE;
1020 zpool_handle_t *zhp;
1025 while ((c = getopt(argc, argv, "fF")) != -1) {
1034 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1043 /* check arguments */
1045 (void) fprintf(stderr, gettext("missing pool argument\n"));
1050 for (i = 0; i < argc; i++) {
1051 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1056 if (zpool_disable_datasets(zhp, force) != 0) {
1063 if (zpool_export_force(zhp) != 0)
1065 } else if (zpool_export(zhp, force) != 0) {
1076 * Given a vdev configuration, determine the maximum width needed for the device
1080 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1082 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1087 if (strlen(name) + depth > max)
1088 max = strlen(name) + depth;
1092 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1093 &child, &children) == 0) {
1094 for (c = 0; c < children; c++)
1095 if ((ret = max_width(zhp, child[c], depth + 2,
1100 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1101 &child, &children) == 0) {
1102 for (c = 0; c < children; c++)
1103 if ((ret = max_width(zhp, child[c], depth + 2,
1108 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1109 &child, &children) == 0) {
1110 for (c = 0; c < children; c++)
1111 if ((ret = max_width(zhp, child[c], depth + 2,
1120 typedef struct spare_cbdata {
1122 zpool_handle_t *cb_zhp;
1126 find_vdev(nvlist_t *nv, uint64_t search)
1132 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1136 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1137 &child, &children) == 0) {
1138 for (c = 0; c < children; c++)
1139 if (find_vdev(child[c], search))
1147 find_spare(zpool_handle_t *zhp, void *data)
1149 spare_cbdata_t *cbp = data;
1150 nvlist_t *config, *nvroot;
1152 config = zpool_get_config(zhp, NULL);
1153 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1156 if (find_vdev(nvroot, cbp->cb_guid)) {
1166 * Print out configuration state as requested by status_callback.
1169 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1170 int namewidth, int depth, boolean_t isspare)
1174 pool_scan_stat_t *ps = NULL;
1176 char rbuf[6], wbuf[6], cbuf[6];
1178 uint64_t notpresent;
1182 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1183 &child, &children) != 0)
1186 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1187 (uint64_t **)&vs, &c) == 0);
1189 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1192 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1195 if (vs->vs_aux == VDEV_AUX_SPARED)
1197 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1201 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1205 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1206 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1207 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1208 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1211 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1212 ¬present) == 0 ||
1213 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1215 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1216 (void) printf(" was %s", path);
1217 } else if (vs->vs_aux != 0) {
1220 switch (vs->vs_aux) {
1221 case VDEV_AUX_OPEN_FAILED:
1222 (void) printf(gettext("cannot open"));
1225 case VDEV_AUX_BAD_GUID_SUM:
1226 (void) printf(gettext("missing device"));
1229 case VDEV_AUX_NO_REPLICAS:
1230 (void) printf(gettext("insufficient replicas"));
1233 case VDEV_AUX_VERSION_NEWER:
1234 (void) printf(gettext("newer version"));
1237 case VDEV_AUX_SPARED:
1238 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1240 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1241 if (strcmp(zpool_get_name(cb.cb_zhp),
1242 zpool_get_name(zhp)) == 0)
1243 (void) printf(gettext("currently in "
1246 (void) printf(gettext("in use by "
1248 zpool_get_name(cb.cb_zhp));
1249 zpool_close(cb.cb_zhp);
1251 (void) printf(gettext("currently in use"));
1255 case VDEV_AUX_ERR_EXCEEDED:
1256 (void) printf(gettext("too many errors"));
1259 case VDEV_AUX_IO_FAILURE:
1260 (void) printf(gettext("experienced I/O failures"));
1263 case VDEV_AUX_BAD_LOG:
1264 (void) printf(gettext("bad intent log"));
1267 case VDEV_AUX_EXTERNAL:
1268 (void) printf(gettext("external device fault"));
1271 case VDEV_AUX_SPLIT_POOL:
1272 (void) printf(gettext("split into new pool"));
1276 (void) printf(gettext("corrupted data"));
1281 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1282 (uint64_t **)&ps, &c);
1284 if (ps && ps->pss_state == DSS_SCANNING &&
1285 vs->vs_scan_processed != 0 && children == 0) {
1286 (void) printf(gettext(" (%s)"),
1287 (ps->pss_func == POOL_SCAN_RESILVER) ?
1288 "resilvering" : "repairing");
1291 (void) printf("\n");
1293 for (c = 0; c < children; c++) {
1294 uint64_t islog = B_FALSE, ishole = B_FALSE;
1296 /* Don't print logs or holes here */
1297 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1299 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1301 if (islog || ishole)
1303 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1304 print_status_config(zhp, vname, child[c],
1305 namewidth, depth + 2, isspare);
1312 * Print the configuration of an exported pool. Iterate over all vdevs in the
1313 * pool, printing out the name and status for each one.
1316 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1323 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1324 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1325 strcmp(type, VDEV_TYPE_HOLE) == 0)
1328 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1329 (uint64_t **)&vs, &c) == 0);
1331 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1332 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1334 if (vs->vs_aux != 0) {
1337 switch (vs->vs_aux) {
1338 case VDEV_AUX_OPEN_FAILED:
1339 (void) printf(gettext("cannot open"));
1342 case VDEV_AUX_BAD_GUID_SUM:
1343 (void) printf(gettext("missing device"));
1346 case VDEV_AUX_NO_REPLICAS:
1347 (void) printf(gettext("insufficient replicas"));
1350 case VDEV_AUX_VERSION_NEWER:
1351 (void) printf(gettext("newer version"));
1354 case VDEV_AUX_ERR_EXCEEDED:
1355 (void) printf(gettext("too many errors"));
1359 (void) printf(gettext("corrupted data"));
1363 (void) printf("\n");
1365 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1366 &child, &children) != 0)
1369 for (c = 0; c < children; c++) {
1370 uint64_t is_log = B_FALSE;
1372 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1377 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1378 print_import_config(vname, child[c], namewidth, depth + 2);
1382 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1383 &child, &children) == 0) {
1384 (void) printf(gettext("\tcache\n"));
1385 for (c = 0; c < children; c++) {
1386 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1387 (void) printf("\t %s\n", vname);
1392 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1393 &child, &children) == 0) {
1394 (void) printf(gettext("\tspares\n"));
1395 for (c = 0; c < children; c++) {
1396 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1397 (void) printf("\t %s\n", vname);
1405 * Logs are recorded as top level vdevs in the main pool child array
1406 * but with "is_log" set to 1. We use either print_status_config() or
1407 * print_import_config() to print the top level logs then any log
1408 * children (eg mirrored slogs) are printed recursively - which
1409 * works because only the top level vdev is marked "is_log"
1412 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1417 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1421 (void) printf(gettext("\tlogs\n"));
1423 for (c = 0; c < children; c++) {
1424 uint64_t is_log = B_FALSE;
1427 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1431 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1433 print_status_config(zhp, name, child[c], namewidth,
1436 print_import_config(name, child[c], namewidth, 2);
1442 * Display the status for the given pool.
1445 show_import(nvlist_t *config)
1447 uint64_t pool_state;
1458 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1460 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1462 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1464 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1467 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1468 (uint64_t **)&vs, &vsc) == 0);
1469 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1471 reason = zpool_import_status(config, &msgid);
1473 (void) printf(gettext(" pool: %s\n"), name);
1474 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1475 (void) printf(gettext(" state: %s"), health);
1476 if (pool_state == POOL_STATE_DESTROYED)
1477 (void) printf(gettext(" (DESTROYED)"));
1478 (void) printf("\n");
1481 case ZPOOL_STATUS_MISSING_DEV_R:
1482 case ZPOOL_STATUS_MISSING_DEV_NR:
1483 case ZPOOL_STATUS_BAD_GUID_SUM:
1484 (void) printf(gettext("status: One or more devices are missing "
1485 "from the system.\n"));
1488 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1489 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1490 (void) printf(gettext("status: One or more devices contains "
1491 "corrupted data.\n"));
1494 case ZPOOL_STATUS_CORRUPT_DATA:
1495 (void) printf(gettext("status: The pool data is corrupted.\n"));
1498 case ZPOOL_STATUS_OFFLINE_DEV:
1499 (void) printf(gettext("status: One or more devices "
1500 "are offlined.\n"));
1503 case ZPOOL_STATUS_CORRUPT_POOL:
1504 (void) printf(gettext("status: The pool metadata is "
1508 case ZPOOL_STATUS_VERSION_OLDER:
1509 (void) printf(gettext("status: The pool is formatted using an "
1510 "older on-disk version.\n"));
1513 case ZPOOL_STATUS_VERSION_NEWER:
1514 (void) printf(gettext("status: The pool is formatted using an "
1515 "incompatible version.\n"));
1518 case ZPOOL_STATUS_HOSTID_MISMATCH:
1519 (void) printf(gettext("status: The pool was last accessed by "
1520 "another system.\n"));
1523 case ZPOOL_STATUS_FAULTED_DEV_R:
1524 case ZPOOL_STATUS_FAULTED_DEV_NR:
1525 (void) printf(gettext("status: One or more devices are "
1529 case ZPOOL_STATUS_BAD_LOG:
1530 (void) printf(gettext("status: An intent log record cannot be "
1534 case ZPOOL_STATUS_RESILVERING:
1535 (void) printf(gettext("status: One or more devices were being "
1541 * No other status can be seen when importing pools.
1543 assert(reason == ZPOOL_STATUS_OK);
1547 * Print out an action according to the overall state of the pool.
1549 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1550 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1551 (void) printf(gettext("action: The pool can be "
1552 "imported using its name or numeric identifier, "
1553 "though\n\tsome features will not be available "
1554 "without an explicit 'zpool upgrade'.\n"));
1555 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1556 (void) printf(gettext("action: The pool can be "
1557 "imported using its name or numeric "
1558 "identifier and\n\tthe '-f' flag.\n"));
1560 (void) printf(gettext("action: The pool can be "
1561 "imported using its name or numeric "
1563 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1564 (void) printf(gettext("action: The pool can be imported "
1565 "despite missing or damaged devices. The\n\tfault "
1566 "tolerance of the pool may be compromised if imported.\n"));
1569 case ZPOOL_STATUS_VERSION_NEWER:
1570 (void) printf(gettext("action: The pool cannot be "
1571 "imported. Access the pool on a system running "
1572 "newer\n\tsoftware, or recreate the pool from "
1575 case ZPOOL_STATUS_MISSING_DEV_R:
1576 case ZPOOL_STATUS_MISSING_DEV_NR:
1577 case ZPOOL_STATUS_BAD_GUID_SUM:
1578 (void) printf(gettext("action: The pool cannot be "
1579 "imported. Attach the missing\n\tdevices and try "
1583 (void) printf(gettext("action: The pool cannot be "
1584 "imported due to damaged devices or data.\n"));
1589 * If the state is "closed" or "can't open", and the aux state
1590 * is "corrupt data":
1592 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1593 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1594 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1595 if (pool_state == POOL_STATE_DESTROYED)
1596 (void) printf(gettext("\tThe pool was destroyed, "
1597 "but can be imported using the '-Df' flags.\n"));
1598 else if (pool_state != POOL_STATE_EXPORTED)
1599 (void) printf(gettext("\tThe pool may be active on "
1600 "another system, but can be imported using\n\t"
1601 "the '-f' flag.\n"));
1605 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1608 (void) printf(gettext("config:\n\n"));
1610 namewidth = max_width(NULL, nvroot, 0, 0);
1614 print_import_config(name, nvroot, namewidth, 0);
1615 if (num_logs(nvroot) > 0)
1616 print_logs(NULL, nvroot, namewidth, B_FALSE);
1618 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1619 (void) printf(gettext("\n\tAdditional devices are known to "
1620 "be part of this pool, though their\n\texact "
1621 "configuration cannot be determined.\n"));
1626 * Perform the import for the given configuration. This passes the heavy
1627 * lifting off to zpool_import_props(), and then mounts the datasets contained
1631 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1632 nvlist_t *props, int flags)
1634 zpool_handle_t *zhp;
1639 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1642 verify(nvlist_lookup_uint64(config,
1643 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1644 verify(nvlist_lookup_uint64(config,
1645 ZPOOL_CONFIG_VERSION, &version) == 0);
1646 if (version > SPA_VERSION) {
1647 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1648 "is formatted using a newer ZFS version\n"), name);
1650 } else if (state != POOL_STATE_EXPORTED &&
1651 !(flags & ZFS_IMPORT_ANY_HOST)) {
1654 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1656 if ((unsigned long)hostid != gethostid()) {
1661 verify(nvlist_lookup_string(config,
1662 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1663 verify(nvlist_lookup_uint64(config,
1664 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1666 (void) fprintf(stderr, gettext("cannot import "
1667 "'%s': pool may be in use from other "
1668 "system, it was last accessed by %s "
1669 "(hostid: 0x%lx) on %s"), name, hostname,
1670 (unsigned long)hostid,
1671 asctime(localtime(&t)));
1672 (void) fprintf(stderr, gettext("use '-f' to "
1673 "import anyway\n"));
1677 (void) fprintf(stderr, gettext("cannot import '%s': "
1678 "pool may be in use from other system\n"), name);
1679 (void) fprintf(stderr, gettext("use '-f' to import "
1685 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1688 if (newname != NULL)
1689 name = (char *)newname;
1691 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1694 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1695 !(flags & ZFS_IMPORT_ONLY) &&
1696 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1706 * zpool import [-d dir] [-D]
1707 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1708 * [-d dir | -c cachefile] [-f] -a
1709 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1710 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1712 * -c Read pool information from a cachefile instead of searching
1715 * -d Scan in a specific directory, other than /dev/dsk. More than
1716 * one directory can be specified using multiple '-d' options.
1718 * -D Scan for previously destroyed pools or import all or only
1719 * specified destroyed pools.
1721 * -R Temporarily import the pool, with all mountpoints relative to
1722 * the given root. The pool will remain exported when the machine
1725 * -V Import even in the presence of faulted vdevs. This is an
1726 * intentionally undocumented option for testing purposes, and
1727 * treats the pool configuration as complete, leaving any bad
1728 * vdevs in the FAULTED state. In other words, it does verbatim
1731 * -f Force import, even if it appears that the pool is active.
1733 * -F Attempt rewind if necessary.
1735 * -n See if rewind would work, but don't actually rewind.
1737 * -N Import the pool but don't mount datasets.
1739 * -T Specify a starting txg to use for import. This option is
1740 * intentionally undocumented option for testing purposes.
1742 * -a Import all pools found.
1744 * -o Set property=value and/or temporary mount options (without '=').
1746 * The import command scans for pools to import, and import pools based on pool
1747 * name and GUID. The pool can also be renamed as part of the import process.
1750 zpool_do_import(int argc, char **argv)
1752 char **searchdirs = NULL;
1756 nvlist_t *pools = NULL;
1757 boolean_t do_all = B_FALSE;
1758 boolean_t do_destroyed = B_FALSE;
1759 char *mntopts = NULL;
1762 uint64_t searchguid = 0;
1763 char *searchname = NULL;
1765 nvlist_t *found_config;
1766 nvlist_t *policy = NULL;
1767 nvlist_t *props = NULL;
1769 int flags = ZFS_IMPORT_NORMAL;
1770 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1771 boolean_t dryrun = B_FALSE;
1772 boolean_t do_rewind = B_FALSE;
1773 boolean_t xtreme_rewind = B_FALSE;
1774 uint64_t pool_state, txg = -1ULL;
1775 char *cachefile = NULL;
1776 importargs_t idata = { 0 };
1780 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1789 if (searchdirs == NULL) {
1790 searchdirs = safe_malloc(sizeof (char *));
1792 char **tmp = safe_malloc((nsearch + 1) *
1794 bcopy(searchdirs, tmp, nsearch *
1799 searchdirs[nsearch++] = optarg;
1802 do_destroyed = B_TRUE;
1805 flags |= ZFS_IMPORT_ANY_HOST;
1811 flags |= ZFS_IMPORT_MISSING_LOG;
1817 flags |= ZFS_IMPORT_ONLY;
1820 if ((propval = strchr(optarg, '=')) != NULL) {
1823 if (add_prop_list(optarg, propval,
1831 if (add_prop_list(zpool_prop_to_name(
1832 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1834 if (nvlist_lookup_string(props,
1835 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1838 if (add_prop_list(zpool_prop_to_name(
1839 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1844 txg = strtoull(optarg, &endptr, 10);
1845 if (errno != 0 || *endptr != '\0') {
1846 (void) fprintf(stderr,
1847 gettext("invalid txg value\n"));
1850 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1853 flags |= ZFS_IMPORT_VERBATIM;
1856 xtreme_rewind = B_TRUE;
1859 (void) fprintf(stderr, gettext("missing argument for "
1860 "'%c' option\n"), optopt);
1864 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1873 if (cachefile && nsearch != 0) {
1874 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1878 if ((dryrun || xtreme_rewind) && !do_rewind) {
1879 (void) fprintf(stderr,
1880 gettext("-n or -X only meaningful with -F\n"));
1884 rewind_policy = ZPOOL_TRY_REWIND;
1886 rewind_policy = ZPOOL_DO_REWIND;
1888 rewind_policy |= ZPOOL_EXTREME_REWIND;
1890 /* In the future, we can capture further policy and include it here */
1891 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1892 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1893 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1896 if (searchdirs == NULL) {
1897 searchdirs = safe_malloc(sizeof (char *));
1898 searchdirs[0] = "/dev/dsk";
1902 /* check argument count */
1905 (void) fprintf(stderr, gettext("too many arguments\n"));
1910 (void) fprintf(stderr, gettext("too many arguments\n"));
1915 * Check for the SYS_CONFIG privilege. We do this explicitly
1916 * here because otherwise any attempt to discover pools will
1919 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1920 (void) fprintf(stderr, gettext("cannot "
1921 "discover pools: permission denied\n"));
1923 nvlist_free(policy);
1929 * Depending on the arguments given, we do one of the following:
1931 * <none> Iterate through all pools and display information about
1934 * -a Iterate through all pools and try to import each one.
1936 * <id> Find the pool that corresponds to the given GUID/pool
1937 * name and import that one.
1939 * -D Above options applies only to destroyed pools.
1945 searchguid = strtoull(argv[0], &endptr, 10);
1946 if (errno != 0 || *endptr != '\0')
1947 searchname = argv[0];
1948 found_config = NULL;
1951 * User specified a name or guid. Ensure it's unique.
1953 idata.unique = B_TRUE;
1957 idata.path = searchdirs;
1958 idata.paths = nsearch;
1959 idata.poolname = searchname;
1960 idata.guid = searchguid;
1961 idata.cachefile = cachefile;
1963 pools = zpool_search_import(g_zfs, &idata);
1965 if (pools != NULL && idata.exists &&
1966 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1967 (void) fprintf(stderr, gettext("cannot import '%s': "
1968 "a pool with that name already exists\n"),
1970 (void) fprintf(stderr, gettext("use the form '%s "
1971 "<pool | id> <newpool>' to give it a new name\n"),
1974 } else if (pools == NULL && idata.exists) {
1975 (void) fprintf(stderr, gettext("cannot import '%s': "
1976 "a pool with that name is already created/imported,\n"),
1978 (void) fprintf(stderr, gettext("and no additional pools "
1979 "with that name were found\n"));
1981 } else if (pools == NULL) {
1983 (void) fprintf(stderr, gettext("cannot import '%s': "
1984 "no such pool available\n"), argv[0]);
1991 nvlist_free(policy);
1996 * At this point we have a list of import candidate configs. Even if
1997 * we were searching by pool name or guid, we still need to
1998 * post-process the list to deal with pool state and possible
2004 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2006 verify(nvpair_value_nvlist(elem, &config) == 0);
2008 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2010 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2012 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2015 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2022 (void) printf("\n");
2025 err |= do_import(config, NULL, mntopts,
2028 show_import(config);
2030 } else if (searchname != NULL) {
2034 * We are searching for a pool based on name.
2036 verify(nvlist_lookup_string(config,
2037 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2039 if (strcmp(name, searchname) == 0) {
2040 if (found_config != NULL) {
2041 (void) fprintf(stderr, gettext(
2042 "cannot import '%s': more than "
2043 "one matching pool\n"), searchname);
2044 (void) fprintf(stderr, gettext(
2045 "import by numeric ID instead\n"));
2048 found_config = config;
2054 * Search for a pool by guid.
2056 verify(nvlist_lookup_uint64(config,
2057 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2059 if (guid == searchguid)
2060 found_config = config;
2065 * If we were searching for a specific pool, verify that we found a
2066 * pool, and then do the import.
2068 if (argc != 0 && err == 0) {
2069 if (found_config == NULL) {
2070 (void) fprintf(stderr, gettext("cannot import '%s': "
2071 "no such pool available\n"), argv[0]);
2074 err |= do_import(found_config, argc == 1 ? NULL :
2075 argv[1], mntopts, props, flags);
2080 * If we were just looking for pools, report an error if none were
2083 if (argc == 0 && first)
2084 (void) fprintf(stderr,
2085 gettext("no pools available to import\n"));
2090 nvlist_free(policy);
2093 return (err ? 1 : 0);
2096 typedef struct iostat_cbdata {
2097 zpool_list_t *cb_list;
2104 print_iostat_separator(iostat_cbdata_t *cb)
2108 for (i = 0; i < cb->cb_namewidth; i++)
2110 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2114 print_iostat_header(iostat_cbdata_t *cb)
2116 (void) printf("%*s capacity operations bandwidth\n",
2117 cb->cb_namewidth, "");
2118 (void) printf("%-*s alloc free read write read write\n",
2119 cb->cb_namewidth, "pool");
2120 print_iostat_separator(cb);
2124 * Display a single statistic.
2127 print_one_stat(uint64_t value)
2131 zfs_nicenum(value, buf, sizeof (buf));
2132 (void) printf(" %5s", buf);
2136 * Print out all the statistics for the given vdev. This can either be the
2137 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2138 * is a verbose output, and we don't want to display the toplevel pool stats.
2141 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2142 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2144 nvlist_t **oldchild, **newchild;
2146 vdev_stat_t *oldvs, *newvs;
2147 vdev_stat_t zerovs = { 0 };
2152 if (oldnv != NULL) {
2153 verify(nvlist_lookup_uint64_array(oldnv,
2154 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2159 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2160 (uint64_t **)&newvs, &c) == 0);
2162 if (strlen(name) + depth > cb->cb_namewidth)
2163 (void) printf("%*s%s", depth, "", name);
2165 (void) printf("%*s%s%*s", depth, "", name,
2166 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2168 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2173 scale = (double)NANOSEC / tdelta;
2175 /* only toplevel vdevs have capacity stats */
2176 if (newvs->vs_space == 0) {
2177 (void) printf(" - -");
2179 print_one_stat(newvs->vs_alloc);
2180 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2183 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2184 oldvs->vs_ops[ZIO_TYPE_READ])));
2186 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2187 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2189 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2190 oldvs->vs_bytes[ZIO_TYPE_READ])));
2192 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2193 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2195 (void) printf("\n");
2197 if (!cb->cb_verbose)
2200 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2201 &newchild, &children) != 0)
2204 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2205 &oldchild, &c) != 0)
2208 for (c = 0; c < children; c++) {
2209 uint64_t ishole = B_FALSE;
2211 if (nvlist_lookup_uint64(newchild[c],
2212 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2215 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2216 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2217 newchild[c], cb, depth + 2);
2222 * Include level 2 ARC devices in iostat output
2224 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2225 &newchild, &children) != 0)
2228 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2229 &oldchild, &c) != 0)
2233 (void) printf("%-*s - - - - - "
2234 "-\n", cb->cb_namewidth, "cache");
2235 for (c = 0; c < children; c++) {
2236 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2238 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2239 newchild[c], cb, depth + 2);
2246 refresh_iostat(zpool_handle_t *zhp, void *data)
2248 iostat_cbdata_t *cb = data;
2252 * If the pool has disappeared, remove it from the list and continue.
2254 if (zpool_refresh_stats(zhp, &missing) != 0)
2258 pool_list_remove(cb->cb_list, zhp);
2264 * Callback to print out the iostats for the given pool.
2267 print_iostat(zpool_handle_t *zhp, void *data)
2269 iostat_cbdata_t *cb = data;
2270 nvlist_t *oldconfig, *newconfig;
2271 nvlist_t *oldnvroot, *newnvroot;
2273 newconfig = zpool_get_config(zhp, &oldconfig);
2275 if (cb->cb_iteration == 1)
2278 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2281 if (oldconfig == NULL)
2284 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2288 * Print out the statistics for the pool.
2290 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2293 print_iostat_separator(cb);
2299 get_namewidth(zpool_handle_t *zhp, void *data)
2301 iostat_cbdata_t *cb = data;
2302 nvlist_t *config, *nvroot;
2304 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2305 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2307 if (!cb->cb_verbose)
2308 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2310 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2314 * The width must fall into the range [10,38]. The upper limit is the
2315 * maximum we can have and still fit in 80 columns.
2317 if (cb->cb_namewidth < 10)
2318 cb->cb_namewidth = 10;
2319 if (cb->cb_namewidth > 38)
2320 cb->cb_namewidth = 38;
2326 * Parse the input string, get the 'interval' and 'count' value if there is one.
2329 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2332 unsigned long interval = 0, count = 0;
2333 int argc = *argcp, errno;
2336 * Determine if the last argument is an integer or a pool name
2338 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2342 interval = strtoul(argv[argc - 1], &end, 10);
2344 if (*end == '\0' && errno == 0) {
2345 if (interval == 0) {
2346 (void) fprintf(stderr, gettext("interval "
2347 "cannot be zero\n"));
2351 * Ignore the last parameter
2356 * If this is not a valid number, just plow on. The
2357 * user will get a more informative error message later
2365 * If the last argument is also an integer, then we have both a count
2368 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2373 interval = strtoul(argv[argc - 1], &end, 10);
2375 if (*end == '\0' && errno == 0) {
2376 if (interval == 0) {
2377 (void) fprintf(stderr, gettext("interval "
2378 "cannot be zero\n"));
2383 * Ignore the last parameter
2397 get_timestamp_arg(char c)
2400 timestamp_fmt = UDATE;
2402 timestamp_fmt = DDATE;
2408 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2410 * -v Display statistics for individual vdevs
2411 * -T Display a timestamp in date(1) or Unix format
2413 * This command can be tricky because we want to be able to deal with pool
2414 * creation/destruction as well as vdev configuration changes. The bulk of this
2415 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2416 * on pool_list_update() to detect the addition of new pools. Configuration
2417 * changes are all handled within libzfs.
2420 zpool_do_iostat(int argc, char **argv)
2425 unsigned long interval = 0, count = 0;
2427 boolean_t verbose = B_FALSE;
2431 while ((c = getopt(argc, argv, "T:v")) != -1) {
2434 get_timestamp_arg(*optarg);
2440 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2449 get_interval_count(&argc, argv, &interval, &count);
2452 * Construct the list of all interesting pools.
2455 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2458 if (pool_list_count(list) == 0 && argc != 0) {
2459 pool_list_free(list);
2463 if (pool_list_count(list) == 0 && interval == 0) {
2464 pool_list_free(list);
2465 (void) fprintf(stderr, gettext("no pools available\n"));
2470 * Enter the main iostat loop.
2473 cb.cb_verbose = verbose;
2474 cb.cb_iteration = 0;
2475 cb.cb_namewidth = 0;
2478 pool_list_update(list);
2480 if ((npools = pool_list_count(list)) == 0)
2484 * Refresh all statistics. This is done as an explicit step
2485 * before calculating the maximum name width, so that any
2486 * configuration changes are properly accounted for.
2488 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2491 * Iterate over all pools to determine the maximum width
2492 * for the pool / device name column across all pools.
2494 cb.cb_namewidth = 0;
2495 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2497 if (timestamp_fmt != NODATE)
2498 print_timestamp(timestamp_fmt);
2501 * If it's the first time, or verbose mode, print the header.
2503 if (++cb.cb_iteration == 1 || verbose)
2504 print_iostat_header(&cb);
2506 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2509 * If there's more than one pool, and we're not in verbose mode
2510 * (which prints a separator for us), then print a separator.
2512 if (npools > 1 && !verbose)
2513 print_iostat_separator(&cb);
2516 (void) printf("\n");
2519 * Flush the output so that redirection to a file isn't buffered
2522 (void) fflush(stdout);
2527 if (count != 0 && --count == 0)
2530 (void) sleep(interval);
2533 pool_list_free(list);
2538 typedef struct list_cbdata {
2539 boolean_t cb_scripted;
2541 zprop_list_t *cb_proplist;
2545 * Given a list of columns to display, output appropriate headers for each one.
2548 print_header(zprop_list_t *pl)
2551 boolean_t first = B_TRUE;
2552 boolean_t right_justify;
2554 for (; pl != NULL; pl = pl->pl_next) {
2555 if (pl->pl_prop == ZPROP_INVAL)
2563 header = zpool_prop_column_name(pl->pl_prop);
2564 right_justify = zpool_prop_align_right(pl->pl_prop);
2566 if (pl->pl_next == NULL && !right_justify)
2567 (void) printf("%s", header);
2568 else if (right_justify)
2569 (void) printf("%*s", pl->pl_width, header);
2571 (void) printf("%-*s", pl->pl_width, header);
2574 (void) printf("\n");
2578 * Given a pool and a list of properties, print out all the properties according
2579 * to the described layout.
2582 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2584 boolean_t first = B_TRUE;
2585 char property[ZPOOL_MAXPROPLEN];
2587 boolean_t right_justify;
2590 for (; pl != NULL; pl = pl->pl_next) {
2593 (void) printf("\t");
2600 right_justify = B_FALSE;
2601 if (pl->pl_prop != ZPROP_INVAL) {
2602 if (zpool_get_prop(zhp, pl->pl_prop, property,
2603 sizeof (property), NULL) != 0)
2608 right_justify = zpool_prop_align_right(pl->pl_prop);
2613 width = pl->pl_width;
2616 * If this is being called in scripted mode, or if this is the
2617 * last column and it is left-justified, don't include a width
2620 if (scripted || (pl->pl_next == NULL && !right_justify))
2621 (void) printf("%s", propstr);
2622 else if (right_justify)
2623 (void) printf("%*s", width, propstr);
2625 (void) printf("%-*s", width, propstr);
2628 (void) printf("\n");
2632 * Generic callback function to list a pool.
2635 list_callback(zpool_handle_t *zhp, void *data)
2637 list_cbdata_t *cbp = data;
2639 if (cbp->cb_first) {
2640 if (!cbp->cb_scripted)
2641 print_header(cbp->cb_proplist);
2642 cbp->cb_first = B_FALSE;
2645 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2651 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2653 * -H Scripted mode. Don't display headers, and separate properties
2655 * -o List of properties to display. Defaults to
2656 * "name,size,allocated,free,capacity,health,altroot"
2657 * -T Display a timestamp in date(1) or Unix format
2659 * List all pools in the system, whether or not they're healthy. Output space
2660 * statistics for each one, as well as health status summary.
2663 zpool_do_list(int argc, char **argv)
2667 list_cbdata_t cb = { 0 };
2668 static char default_props[] =
2669 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2670 char *props = default_props;
2671 unsigned long interval = 0, count = 0;
2674 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2677 cb.cb_scripted = B_TRUE;
2683 get_timestamp_arg(*optarg);
2686 (void) fprintf(stderr, gettext("missing argument for "
2687 "'%c' option\n"), optopt);
2691 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2700 get_interval_count(&argc, argv, &interval, &count);
2702 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2705 cb.cb_first = B_TRUE;
2709 if (timestamp_fmt != NODATE)
2710 print_timestamp(timestamp_fmt);
2712 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2713 list_callback, &cb);
2715 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2716 (void) printf(gettext("no pools available\n"));
2717 zprop_free_list(cb.cb_proplist);
2724 if (count != 0 && --count == 0)
2727 (void) sleep(interval);
2730 zprop_free_list(cb.cb_proplist);
2735 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2742 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2743 &child, &children) != 0) {
2744 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2745 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2746 name += sizeof(_PATH_DEV) - 1;
2747 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2748 path += sizeof(_PATH_DEV) - 1;
2749 if (strcmp(name, path) == 0)
2754 for (c = 0; c < children; c++)
2755 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2762 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2764 boolean_t force = B_FALSE;
2767 char *poolname, *old_disk, *new_disk;
2768 zpool_handle_t *zhp;
2772 while ((c = getopt(argc, argv, "f")) != -1) {
2778 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2787 /* get pool name and check number of arguments */
2789 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2796 (void) fprintf(stderr,
2797 gettext("missing <device> specification\n"));
2805 (void) fprintf(stderr,
2806 gettext("missing <new_device> specification\n"));
2809 new_disk = old_disk;
2819 (void) fprintf(stderr, gettext("too many arguments\n"));
2823 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2826 if (zpool_get_config(zhp, NULL) == NULL) {
2827 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2833 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2835 if (nvroot == NULL) {
2840 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2842 nvlist_free(nvroot);
2849 * zpool replace [-f] <pool> <device> <new_device>
2851 * -f Force attach, even if <new_device> appears to be in use.
2853 * Replace <device> with <new_device>.
2857 zpool_do_replace(int argc, char **argv)
2859 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2863 * zpool attach [-f] <pool> <device> <new_device>
2865 * -f Force attach, even if <new_device> appears to be in use.
2867 * Attach <new_device> to the mirror containing <device>. If <device> is not
2868 * part of a mirror, then <device> will be transformed into a mirror of
2869 * <device> and <new_device>. In either case, <new_device> will begin life
2870 * with a DTL of [0, now], and will immediately begin to resilver itself.
2873 zpool_do_attach(int argc, char **argv)
2875 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2879 * zpool detach [-f] <pool> <device>
2881 * -f Force detach of <device>, even if DTLs argue against it
2882 * (not supported yet)
2884 * Detach a device from a mirror. The operation will be refused if <device>
2885 * is the last device in the mirror, or if the DTLs indicate that this device
2886 * has the only valid copy of some data.
2890 zpool_do_detach(int argc, char **argv)
2893 char *poolname, *path;
2894 zpool_handle_t *zhp;
2898 while ((c = getopt(argc, argv, "f")) != -1) {
2902 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2911 /* get pool name and check number of arguments */
2913 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2918 (void) fprintf(stderr,
2919 gettext("missing <device> specification\n"));
2926 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2929 ret = zpool_vdev_detach(zhp, path);
2937 * zpool split [-n] [-o prop=val] ...
2939 * [-R altroot] <pool> <newpool> [<device> ...]
2941 * -n Do not split the pool, but display the resulting layout if
2942 * it were to be split.
2943 * -o Set property=value, or set mount options.
2944 * -R Mount the split-off pool under an alternate root.
2946 * Splits the named pool and gives it the new pool name. Devices to be split
2947 * off may be listed, provided that no more than one device is specified
2948 * per top-level vdev mirror. The newly split pool is left in an exported
2949 * state unless -R is specified.
2951 * Restrictions: the top-level of the pool pool must only be made up of
2952 * mirrors; all devices in the pool must be healthy; no device may be
2953 * undergoing a resilvering operation.
2956 zpool_do_split(int argc, char **argv)
2958 char *srcpool, *newpool, *propval;
2959 char *mntopts = NULL;
2962 zpool_handle_t *zhp;
2963 nvlist_t *config, *props = NULL;
2965 flags.dryrun = B_FALSE;
2966 flags.import = B_FALSE;
2969 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
2972 flags.import = B_TRUE;
2974 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
2975 &props, B_TRUE) != 0) {
2982 flags.dryrun = B_TRUE;
2985 if ((propval = strchr(optarg, '=')) != NULL) {
2988 if (add_prop_list(optarg, propval,
2989 &props, B_TRUE) != 0) {
2999 (void) fprintf(stderr, gettext("missing argument for "
3000 "'%c' option\n"), optopt);
3004 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3011 if (!flags.import && mntopts != NULL) {
3012 (void) fprintf(stderr, gettext("setting mntopts is only "
3013 "valid when importing the pool\n"));
3021 (void) fprintf(stderr, gettext("Missing pool name\n"));
3025 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3035 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3038 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3039 if (config == NULL) {
3043 (void) printf(gettext("would create '%s' with the "
3044 "following layout:\n\n"), newpool);
3045 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3047 nvlist_free(config);
3052 if (ret != 0 || flags.dryrun || !flags.import)
3056 * The split was successful. Now we need to open the new
3057 * pool and import it.
3059 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3061 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3062 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3064 (void) fprintf(stderr, gettext("Split was succssful, but "
3065 "the datasets could not all be mounted\n"));
3066 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3067 "different altroot\n"), "zpool import");
3077 * zpool online <pool> <device> ...
3080 zpool_do_online(int argc, char **argv)
3084 zpool_handle_t *zhp;
3086 vdev_state_t newstate;
3090 while ((c = getopt(argc, argv, "et")) != -1) {
3093 flags |= ZFS_ONLINE_EXPAND;
3097 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3106 /* get pool name and check number of arguments */
3108 (void) fprintf(stderr, gettext("missing pool name\n"));
3112 (void) fprintf(stderr, gettext("missing device name\n"));
3118 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3121 for (i = 1; i < argc; i++) {
3122 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3123 if (newstate != VDEV_STATE_HEALTHY) {
3124 (void) printf(gettext("warning: device '%s' "
3125 "onlined, but remains in faulted state\n"),
3127 if (newstate == VDEV_STATE_FAULTED)
3128 (void) printf(gettext("use 'zpool "
3129 "clear' to restore a faulted "
3132 (void) printf(gettext("use 'zpool "
3133 "replace' to replace devices "
3134 "that are no longer present\n"));
3147 * zpool offline [-ft] <pool> <device> ...
3149 * -f Force the device into the offline state, even if doing
3150 * so would appear to compromise pool availability.
3151 * (not supported yet)
3153 * -t Only take the device off-line temporarily. The offline
3154 * state will not be persistent across reboots.
3158 zpool_do_offline(int argc, char **argv)
3162 zpool_handle_t *zhp;
3164 boolean_t istmp = B_FALSE;
3167 while ((c = getopt(argc, argv, "ft")) != -1) {
3174 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3183 /* get pool name and check number of arguments */
3185 (void) fprintf(stderr, gettext("missing pool name\n"));
3189 (void) fprintf(stderr, gettext("missing device name\n"));
3195 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3198 for (i = 1; i < argc; i++) {
3199 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3209 * zpool clear <pool> [device]
3211 * Clear all errors associated with a pool or a particular device.
3214 zpool_do_clear(int argc, char **argv)
3218 boolean_t dryrun = B_FALSE;
3219 boolean_t do_rewind = B_FALSE;
3220 boolean_t xtreme_rewind = B_FALSE;
3221 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3222 nvlist_t *policy = NULL;
3223 zpool_handle_t *zhp;
3224 char *pool, *device;
3227 while ((c = getopt(argc, argv, "FnX")) != -1) {
3236 xtreme_rewind = B_TRUE;
3239 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3249 (void) fprintf(stderr, gettext("missing pool name\n"));
3254 (void) fprintf(stderr, gettext("too many arguments\n"));
3258 if ((dryrun || xtreme_rewind) && !do_rewind) {
3259 (void) fprintf(stderr,
3260 gettext("-n or -X only meaningful with -F\n"));
3264 rewind_policy = ZPOOL_TRY_REWIND;
3266 rewind_policy = ZPOOL_DO_REWIND;
3268 rewind_policy |= ZPOOL_EXTREME_REWIND;
3270 /* In future, further rewind policy choices can be passed along here */
3271 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3272 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3276 device = argc == 2 ? argv[1] : NULL;
3278 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3279 nvlist_free(policy);
3283 if (zpool_clear(zhp, device, policy) != 0)
3288 nvlist_free(policy);
3293 typedef struct scrub_cbdata {
3300 scrub_callback(zpool_handle_t *zhp, void *data)
3302 scrub_cbdata_t *cb = data;
3306 * Ignore faulted pools.
3308 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3309 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3310 "currently unavailable\n"), zpool_get_name(zhp));
3314 err = zpool_scan(zhp, cb->cb_type);
3320 * zpool scrub [-s] <pool> ...
3322 * -s Stop. Stops any in-progress scrub.
3325 zpool_do_scrub(int argc, char **argv)
3330 cb.cb_type = POOL_SCAN_SCRUB;
3333 while ((c = getopt(argc, argv, "s")) != -1) {
3336 cb.cb_type = POOL_SCAN_NONE;
3339 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3351 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3355 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3358 typedef struct status_cbdata {
3360 boolean_t cb_allpools;
3361 boolean_t cb_verbose;
3362 boolean_t cb_explain;
3364 boolean_t cb_dedup_stats;
3368 * Print out detailed scrub status.
3371 print_scan_status(pool_scan_stat_t *ps)
3374 uint64_t elapsed, mins_left, hours_left;
3375 uint64_t pass_exam, examined, total;
3377 double fraction_done;
3378 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3380 (void) printf(gettext(" scan: "));
3382 /* If there's never been a scan, there's not much to say. */
3383 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3384 ps->pss_func >= POOL_SCAN_FUNCS) {
3385 (void) printf(gettext("none requested\n"));
3389 start = ps->pss_start_time;
3390 end = ps->pss_end_time;
3391 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3393 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3394 ps->pss_func == POOL_SCAN_RESILVER);
3396 * Scan is finished or canceled.
3398 if (ps->pss_state == DSS_FINISHED) {
3399 uint64_t minutes_taken = (end - start) / 60;
3402 if (ps->pss_func == POOL_SCAN_SCRUB) {
3403 fmt = gettext("scrub repaired %s in %lluh%um with "
3404 "%llu errors on %s");
3405 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3406 fmt = gettext("resilvered %s in %lluh%um with "
3407 "%llu errors on %s");
3410 (void) printf(fmt, processed_buf,
3411 (u_longlong_t)(minutes_taken / 60),
3412 (uint_t)(minutes_taken % 60),
3413 (u_longlong_t)ps->pss_errors,
3414 ctime((time_t *)&end));
3416 } else if (ps->pss_state == DSS_CANCELED) {
3417 if (ps->pss_func == POOL_SCAN_SCRUB) {
3418 (void) printf(gettext("scrub canceled on %s"),
3420 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3421 (void) printf(gettext("resilver canceled on %s"),
3427 assert(ps->pss_state == DSS_SCANNING);
3430 * Scan is in progress.
3432 if (ps->pss_func == POOL_SCAN_SCRUB) {
3433 (void) printf(gettext("scrub in progress since %s"),
3435 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3436 (void) printf(gettext("resilver in progress since %s"),
3440 examined = ps->pss_examined ? ps->pss_examined : 1;
3441 total = ps->pss_to_examine;
3442 fraction_done = (double)examined / total;
3444 /* elapsed time for this pass */
3445 elapsed = time(NULL) - ps->pss_pass_start;
3446 elapsed = elapsed ? elapsed : 1;
3447 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3448 rate = pass_exam / elapsed;
3449 rate = rate ? rate : 1;
3450 mins_left = ((total - examined) / rate) / 60;
3451 hours_left = mins_left / 60;
3453 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3454 zfs_nicenum(total, total_buf, sizeof (total_buf));
3455 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3458 * do not print estimated time if hours_left is more than 30 days
3460 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3461 examined_buf, total_buf, rate_buf);
3462 if (hours_left < (30 * 24)) {
3463 (void) printf(gettext(", %lluh%um to go\n"),
3464 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3466 (void) printf(gettext(
3467 ", (scan is slow, no estimated time)\n"));
3470 if (ps->pss_func == POOL_SCAN_RESILVER) {
3471 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3472 processed_buf, 100 * fraction_done);
3473 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3474 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3475 processed_buf, 100 * fraction_done);
3480 print_error_log(zpool_handle_t *zhp)
3482 nvlist_t *nverrlist = NULL;
3485 size_t len = MAXPATHLEN * 2;
3487 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3488 (void) printf("errors: List of errors unavailable "
3489 "(insufficient privileges)\n");
3493 (void) printf("errors: Permanent errors have been "
3494 "detected in the following files:\n\n");
3496 pathname = safe_malloc(len);
3498 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3500 uint64_t dsobj, obj;
3502 verify(nvpair_value_nvlist(elem, &nv) == 0);
3503 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3505 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3507 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3508 (void) printf("%7s %s\n", "", pathname);
3511 nvlist_free(nverrlist);
3515 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3524 (void) printf(gettext("\tspares\n"));
3526 for (i = 0; i < nspares; i++) {
3527 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3528 print_status_config(zhp, name, spares[i],
3529 namewidth, 2, B_TRUE);
3535 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3544 (void) printf(gettext("\tcache\n"));
3546 for (i = 0; i < nl2cache; i++) {
3547 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3548 print_status_config(zhp, name, l2cache[i],
3549 namewidth, 2, B_FALSE);
3555 print_dedup_stats(nvlist_t *config)
3557 ddt_histogram_t *ddh;
3563 * If the pool was faulted then we may not have been able to
3564 * obtain the config. Otherwise, if have anything in the dedup
3565 * table continue processing the stats.
3567 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3568 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3571 (void) printf("\n");
3572 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3573 (u_longlong_t)ddo->ddo_count,
3574 (u_longlong_t)ddo->ddo_dspace,
3575 (u_longlong_t)ddo->ddo_mspace);
3577 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3578 (uint64_t **)&dds, &c) == 0);
3579 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3580 (uint64_t **)&ddh, &c) == 0);
3581 zpool_dump_ddt(dds, ddh);
3585 * Display a summary of pool status. Displays a summary such as:
3589 * reason: One or more devices ...
3590 * see: http://www.sun.com/msg/ZFS-xxxx-01
3596 * When given the '-v' option, we print out the complete config. If the '-e'
3597 * option is specified, then we print out error rate information as well.
3600 status_callback(zpool_handle_t *zhp, void *data)
3602 status_cbdata_t *cbp = data;
3603 nvlist_t *config, *nvroot;
3610 config = zpool_get_config(zhp, NULL);
3611 reason = zpool_get_status(zhp, &msgid);
3616 * If we were given 'zpool status -x', only report those pools with
3619 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3620 if (!cbp->cb_allpools) {
3621 (void) printf(gettext("pool '%s' is healthy\n"),
3622 zpool_get_name(zhp));
3624 cbp->cb_first = B_FALSE;
3630 cbp->cb_first = B_FALSE;
3632 (void) printf("\n");
3634 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3636 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3637 (uint64_t **)&vs, &c) == 0);
3638 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3640 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3641 (void) printf(gettext(" state: %s\n"), health);
3644 case ZPOOL_STATUS_MISSING_DEV_R:
3645 (void) printf(gettext("status: One or more devices could not "
3646 "be opened. Sufficient replicas exist for\n\tthe pool to "
3647 "continue functioning in a degraded state.\n"));
3648 (void) printf(gettext("action: Attach the missing device and "
3649 "online it using 'zpool online'.\n"));
3652 case ZPOOL_STATUS_MISSING_DEV_NR:
3653 (void) printf(gettext("status: One or more devices could not "
3654 "be opened. There are insufficient\n\treplicas for the "
3655 "pool to continue functioning.\n"));
3656 (void) printf(gettext("action: Attach the missing device and "
3657 "online it using 'zpool online'.\n"));
3660 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3661 (void) printf(gettext("status: One or more devices could not "
3662 "be used because the label is missing or\n\tinvalid. "
3663 "Sufficient replicas exist for the pool to continue\n\t"
3664 "functioning in a degraded state.\n"));
3665 (void) printf(gettext("action: Replace the device using "
3666 "'zpool replace'.\n"));
3669 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3670 (void) printf(gettext("status: One or more devices could not "
3671 "be used because the label is missing \n\tor invalid. "
3672 "There are insufficient replicas for the pool to "
3673 "continue\n\tfunctioning.\n"));
3674 zpool_explain_recover(zpool_get_handle(zhp),
3675 zpool_get_name(zhp), reason, config);
3678 case ZPOOL_STATUS_FAILING_DEV:
3679 (void) printf(gettext("status: One or more devices has "
3680 "experienced an unrecoverable error. An\n\tattempt was "
3681 "made to correct the error. Applications are "
3683 (void) printf(gettext("action: Determine if the device needs "
3684 "to be replaced, and clear the errors\n\tusing "
3685 "'zpool clear' or replace the device with 'zpool "
3689 case ZPOOL_STATUS_OFFLINE_DEV:
3690 (void) printf(gettext("status: One or more devices has "
3691 "been taken offline by the administrator.\n\tSufficient "
3692 "replicas exist for the pool to continue functioning in "
3693 "a\n\tdegraded state.\n"));
3694 (void) printf(gettext("action: Online the device using "
3695 "'zpool online' or replace the device with\n\t'zpool "
3699 case ZPOOL_STATUS_REMOVED_DEV:
3700 (void) printf(gettext("status: One or more devices has "
3701 "been removed by the administrator.\n\tSufficient "
3702 "replicas exist for the pool to continue functioning in "
3703 "a\n\tdegraded state.\n"));
3704 (void) printf(gettext("action: Online the device using "
3705 "'zpool online' or replace the device with\n\t'zpool "
3709 case ZPOOL_STATUS_RESILVERING:
3710 (void) printf(gettext("status: One or more devices is "
3711 "currently being resilvered. The pool will\n\tcontinue "
3712 "to function, possibly in a degraded state.\n"));
3713 (void) printf(gettext("action: Wait for the resilver to "
3717 case ZPOOL_STATUS_CORRUPT_DATA:
3718 (void) printf(gettext("status: One or more devices has "
3719 "experienced an error resulting in data\n\tcorruption. "
3720 "Applications may be affected.\n"));
3721 (void) printf(gettext("action: Restore the file in question "
3722 "if possible. Otherwise restore the\n\tentire pool from "
3726 case ZPOOL_STATUS_CORRUPT_POOL:
3727 (void) printf(gettext("status: The pool metadata is corrupted "
3728 "and the pool cannot be opened.\n"));
3729 zpool_explain_recover(zpool_get_handle(zhp),
3730 zpool_get_name(zhp), reason, config);
3733 case ZPOOL_STATUS_VERSION_OLDER:
3734 (void) printf(gettext("status: The pool is formatted using an "
3735 "older on-disk format. The pool can\n\tstill be used, but "
3736 "some features are unavailable.\n"));
3737 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3738 "upgrade'. Once this is done, the\n\tpool will no longer "
3739 "be accessible on older software versions.\n"));
3742 case ZPOOL_STATUS_VERSION_NEWER:
3743 (void) printf(gettext("status: The pool has been upgraded to a "
3744 "newer, incompatible on-disk version.\n\tThe pool cannot "
3745 "be accessed on this system.\n"));
3746 (void) printf(gettext("action: Access the pool from a system "
3747 "running more recent software, or\n\trestore the pool from "
3751 case ZPOOL_STATUS_FAULTED_DEV_R:
3752 (void) printf(gettext("status: One or more devices are "
3753 "faulted in response to persistent errors.\n\tSufficient "
3754 "replicas exist for the pool to continue functioning "
3755 "in a\n\tdegraded state.\n"));
3756 (void) printf(gettext("action: Replace the faulted device, "
3757 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3760 case ZPOOL_STATUS_FAULTED_DEV_NR:
3761 (void) printf(gettext("status: One or more devices are "
3762 "faulted in response to persistent errors. There are "
3763 "insufficient replicas for the pool to\n\tcontinue "
3765 (void) printf(gettext("action: Destroy and re-create the pool "
3766 "from a backup source. Manually marking the device\n"
3767 "\trepaired using 'zpool clear' may allow some data "
3768 "to be recovered.\n"));
3771 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3772 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3773 (void) printf(gettext("status: One or more devices are "
3774 "faulted in response to IO failures.\n"));
3775 (void) printf(gettext("action: Make sure the affected devices "
3776 "are connected, then run 'zpool clear'.\n"));
3779 case ZPOOL_STATUS_BAD_LOG:
3780 (void) printf(gettext("status: An intent log record "
3781 "could not be read.\n"
3782 "\tWaiting for adminstrator intervention to fix the "
3783 "faulted pool.\n"));
3784 (void) printf(gettext("action: Either restore the affected "
3785 "device(s) and run 'zpool online',\n"
3786 "\tor ignore the intent log records by running "
3787 "'zpool clear'.\n"));
3792 * The remaining errors can't actually be generated, yet.
3794 assert(reason == ZPOOL_STATUS_OK);
3798 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3801 if (config != NULL) {
3804 nvlist_t **spares, **l2cache;
3805 uint_t nspares, nl2cache;
3806 pool_scan_stat_t *ps = NULL;
3808 (void) nvlist_lookup_uint64_array(nvroot,
3809 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3810 print_scan_status(ps);
3812 namewidth = max_width(zhp, nvroot, 0, 0);
3816 (void) printf(gettext("config:\n\n"));
3817 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3818 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3819 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3820 namewidth, 0, B_FALSE);
3822 if (num_logs(nvroot) > 0)
3823 print_logs(zhp, nvroot, namewidth, B_TRUE);
3824 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3825 &l2cache, &nl2cache) == 0)
3826 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3828 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3829 &spares, &nspares) == 0)
3830 print_spares(zhp, spares, nspares, namewidth);
3832 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3834 nvlist_t *nverrlist = NULL;
3837 * If the approximate error count is small, get a
3838 * precise count by fetching the entire log and
3839 * uniquifying the results.
3841 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3842 zpool_get_errlog(zhp, &nverrlist) == 0) {
3847 while ((elem = nvlist_next_nvpair(nverrlist,
3852 nvlist_free(nverrlist);
3854 (void) printf("\n");
3857 (void) printf(gettext("errors: No known data "
3859 else if (!cbp->cb_verbose)
3860 (void) printf(gettext("errors: %llu data "
3861 "errors, use '-v' for a list\n"),
3862 (u_longlong_t)nerr);
3864 print_error_log(zhp);
3867 if (cbp->cb_dedup_stats)
3868 print_dedup_stats(config);
3870 (void) printf(gettext("config: The configuration cannot be "
3878 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3880 * -v Display complete error logs
3881 * -x Display only pools with potential problems
3882 * -D Display dedup status (undocumented)
3883 * -T Display a timestamp in date(1) or Unix format
3885 * Describes the health status of all pools or some subset.
3888 zpool_do_status(int argc, char **argv)
3892 unsigned long interval = 0, count = 0;
3893 status_cbdata_t cb = { 0 };
3896 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3899 cb.cb_verbose = B_TRUE;
3902 cb.cb_explain = B_TRUE;
3905 cb.cb_dedup_stats = B_TRUE;
3908 get_timestamp_arg(*optarg);
3911 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3920 get_interval_count(&argc, argv, &interval, &count);
3923 cb.cb_allpools = B_TRUE;
3925 cb.cb_first = B_TRUE;
3928 if (timestamp_fmt != NODATE)
3929 print_timestamp(timestamp_fmt);
3931 ret = for_each_pool(argc, argv, B_TRUE, NULL,
3932 status_callback, &cb);
3934 if (argc == 0 && cb.cb_count == 0)
3935 (void) printf(gettext("no pools available\n"));
3936 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3937 (void) printf(gettext("all pools are healthy\n"));
3945 if (count != 0 && --count == 0)
3948 (void) sleep(interval);
3954 typedef struct upgrade_cbdata {
3958 char cb_poolname[ZPOOL_MAXNAMELEN];
3960 uint64_t cb_version;
3965 is_root_pool(zpool_handle_t *zhp)
3967 static struct statfs sfs;
3968 static char *poolname = NULL;
3969 static boolean_t stated = B_FALSE;
3974 if (statfs("/", &sfs) == -1) {
3975 (void) fprintf(stderr,
3976 "Unable to stat root file system: %s.\n",
3980 if (strcmp(sfs.f_fstypename, "zfs") != 0)
3982 poolname = sfs.f_mntfromname;
3983 if ((slash = strchr(poolname, '/')) != NULL)
3986 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
3990 upgrade_cb(zpool_handle_t *zhp, void *arg)
3992 upgrade_cbdata_t *cbp = arg;
3997 config = zpool_get_config(zhp, NULL);
3998 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4001 if (!cbp->cb_newer && version < SPA_VERSION) {
4003 if (cbp->cb_first) {
4004 (void) printf(gettext("The following pools are "
4005 "out of date, and can be upgraded. After "
4006 "being\nupgraded, these pools will no "
4007 "longer be accessible by older software "
4009 (void) printf(gettext("VER POOL\n"));
4010 (void) printf(gettext("--- ------------\n"));
4011 cbp->cb_first = B_FALSE;
4014 (void) printf("%2llu %s\n", (u_longlong_t)version,
4015 zpool_get_name(zhp));
4017 cbp->cb_first = B_FALSE;
4018 ret = zpool_upgrade(zhp, cbp->cb_version);
4020 (void) printf(gettext("Successfully upgraded "
4021 "'%s'\n\n"), zpool_get_name(zhp));
4022 if (cbp->cb_poolname[0] == '\0' &&
4023 is_root_pool(zhp)) {
4024 (void) strlcpy(cbp->cb_poolname,
4025 zpool_get_name(zhp),
4026 sizeof(cbp->cb_poolname));
4030 } else if (cbp->cb_newer && version > SPA_VERSION) {
4031 assert(!cbp->cb_all);
4033 if (cbp->cb_first) {
4034 (void) printf(gettext("The following pools are "
4035 "formatted using a newer software version and\n"
4036 "cannot be accessed on the current system.\n\n"));
4037 (void) printf(gettext("VER POOL\n"));
4038 (void) printf(gettext("--- ------------\n"));
4039 cbp->cb_first = B_FALSE;
4042 (void) printf("%2llu %s\n", (u_longlong_t)version,
4043 zpool_get_name(zhp));
4052 upgrade_one(zpool_handle_t *zhp, void *data)
4054 upgrade_cbdata_t *cbp = data;
4055 uint64_t cur_version;
4058 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4059 (void) printf(gettext("'log' is now a reserved word\n"
4060 "Pool 'log' must be renamed using export and import"
4065 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4066 if (cur_version > cbp->cb_version) {
4067 (void) printf(gettext("Pool '%s' is already formatted "
4068 "using more current version '%llu'.\n"),
4069 zpool_get_name(zhp), cur_version);
4072 if (cur_version == cbp->cb_version) {
4073 (void) printf(gettext("Pool '%s' is already formatted "
4074 "using the current version.\n"), zpool_get_name(zhp));
4078 ret = zpool_upgrade(zhp, cbp->cb_version);
4081 (void) printf(gettext("Successfully upgraded '%s' "
4082 "from version %llu to version %llu\n\n"),
4083 zpool_get_name(zhp), (u_longlong_t)cur_version,
4084 (u_longlong_t)cbp->cb_version);
4085 if (cbp->cb_poolname[0] == '\0' && is_root_pool(zhp)) {
4086 (void) strlcpy(cbp->cb_poolname, zpool_get_name(zhp),
4087 sizeof(cbp->cb_poolname));
4097 * zpool upgrade [-V version] <-a | pool ...>
4099 * With no arguments, display downrev'd ZFS pool available for upgrade.
4100 * Individual pools can be upgraded by specifying the pool, and '-a' will
4101 * upgrade all pools.
4104 zpool_do_upgrade(int argc, char **argv)
4107 upgrade_cbdata_t cb = { 0 };
4109 boolean_t showversions = B_FALSE;
4114 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4120 showversions = B_TRUE;
4123 cb.cb_version = strtoll(optarg, &end, 10);
4124 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4125 cb.cb_version < SPA_VERSION_1) {
4126 (void) fprintf(stderr,
4127 gettext("invalid version '%s'\n"), optarg);
4132 (void) fprintf(stderr, gettext("missing argument for "
4133 "'%c' option\n"), optopt);
4137 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4148 if (cb.cb_version == 0) {
4149 cb.cb_version = SPA_VERSION;
4150 } else if (!cb.cb_all && argc == 0) {
4151 (void) fprintf(stderr, gettext("-V option is "
4152 "incompatible with other arguments\n"));
4157 if (cb.cb_all || argc != 0) {
4158 (void) fprintf(stderr, gettext("-v option is "
4159 "incompatible with other arguments\n"));
4162 } else if (cb.cb_all) {
4164 (void) fprintf(stderr, gettext("-a option should not "
4165 "be used along with a pool name\n"));
4170 (void) printf(gettext("This system is currently running "
4171 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4172 cb.cb_first = B_TRUE;
4174 (void) printf(gettext("The following versions are "
4176 (void) printf(gettext("VER DESCRIPTION\n"));
4177 (void) printf("--- -----------------------------------------"
4178 "---------------\n");
4179 (void) printf(gettext(" 1 Initial ZFS version\n"));
4180 (void) printf(gettext(" 2 Ditto blocks "
4181 "(replicated metadata)\n"));
4182 (void) printf(gettext(" 3 Hot spares and double parity "
4184 (void) printf(gettext(" 4 zpool history\n"));
4185 (void) printf(gettext(" 5 Compression using the gzip "
4187 (void) printf(gettext(" 6 bootfs pool property\n"));
4188 (void) printf(gettext(" 7 Separate intent log devices\n"));
4189 (void) printf(gettext(" 8 Delegated administration\n"));
4190 (void) printf(gettext(" 9 refquota and refreservation "
4192 (void) printf(gettext(" 10 Cache devices\n"));
4193 (void) printf(gettext(" 11 Improved scrub performance\n"));
4194 (void) printf(gettext(" 12 Snapshot properties\n"));
4195 (void) printf(gettext(" 13 snapused property\n"));
4196 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4197 (void) printf(gettext(" 15 user/group space accounting\n"));
4198 (void) printf(gettext(" 16 stmf property support\n"));
4199 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4200 (void) printf(gettext(" 18 Snapshot user holds\n"));
4201 (void) printf(gettext(" 19 Log device removal\n"));
4202 (void) printf(gettext(" 20 Compression using zle "
4203 "(zero-length encoding)\n"));
4204 (void) printf(gettext(" 21 Deduplication\n"));
4205 (void) printf(gettext(" 22 Received properties\n"));
4206 (void) printf(gettext(" 23 Slim ZIL\n"));
4207 (void) printf(gettext(" 24 System attributes\n"));
4208 (void) printf(gettext(" 25 Improved scrub stats\n"));
4209 (void) printf(gettext(" 26 Improved snapshot deletion "
4211 (void) printf(gettext(" 27 Improved snapshot creation "
4213 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4214 (void) printf(gettext("\nFor more information on a particular "
4215 "version, including supported releases,\n"));
4216 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4217 } else if (argc == 0) {
4220 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4221 notfound = cb.cb_first;
4223 if (!cb.cb_all && ret == 0) {
4225 (void) printf("\n");
4226 cb.cb_first = B_TRUE;
4227 cb.cb_newer = B_TRUE;
4228 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4231 (void) printf("\n");
4237 (void) printf(gettext("All pools are formatted "
4238 "using this version.\n"));
4239 else if (!cb.cb_all)
4240 (void) printf(gettext("Use 'zpool upgrade -v' "
4241 "for a list of available versions and "
4242 "their associated\nfeatures.\n"));
4245 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4249 if (cb.cb_poolname[0] != '\0') {
4251 "If you boot from pool '%s', don't forget to update boot code.\n"
4252 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4253 "the following command will do it:\n"
4255 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4262 typedef struct hist_cbdata {
4269 * Print out the command history for a specific pool.
4272 get_history_one(zpool_handle_t *zhp, void *data)
4288 char internalstr[MAXPATHLEN];
4289 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4293 cb->first = B_FALSE;
4295 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4297 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4300 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4301 &records, &numrecords) == 0);
4302 for (i = 0; i < numrecords; i++) {
4303 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4307 /* is it an internal event or a standard event? */
4308 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4310 if (cb->internal == 0)
4313 if (nvlist_lookup_uint64(records[i],
4314 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4316 verify(nvlist_lookup_uint64(records[i],
4317 ZPOOL_HIST_TXG, &txg) == 0);
4318 verify(nvlist_lookup_string(records[i],
4319 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4320 if (ievent >= LOG_END)
4322 (void) snprintf(internalstr,
4323 sizeof (internalstr),
4324 "[internal %s txg:%lld] %s",
4325 zfs_history_event_names[ievent], txg,
4327 cmdstr = internalstr;
4330 (void) localtime_r(&tsec, &t);
4331 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4332 (void) printf("%s %s", tbuf, cmdstr);
4335 (void) printf("\n");
4338 (void) printf(" [");
4339 if (nvlist_lookup_uint64(records[i],
4340 ZPOOL_HIST_WHO, &who) == 0) {
4341 pwd = getpwuid((uid_t)who);
4343 (void) printf("user %s on",
4346 (void) printf("user %d on",
4349 (void) printf(gettext("no info]\n"));
4352 if (nvlist_lookup_string(records[i],
4353 ZPOOL_HIST_HOST, &hostname) == 0) {
4354 (void) printf(" %s", hostname);
4356 if (nvlist_lookup_string(records[i],
4357 ZPOOL_HIST_ZONE, &zonename) == 0) {
4358 (void) printf(":%s", zonename);
4362 (void) printf("\n");
4364 (void) printf("\n");
4371 * zpool history <pool>
4373 * Displays the history of commands that modified pools.
4378 zpool_do_history(int argc, char **argv)
4380 hist_cbdata_t cbdata = { 0 };
4384 cbdata.first = B_TRUE;
4386 while ((c = getopt(argc, argv, "li")) != -1) {
4392 cbdata.internal = 1;
4395 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4403 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4406 if (argc == 0 && cbdata.first == B_TRUE) {
4407 (void) printf(gettext("no pools available\n"));
4415 get_callback(zpool_handle_t *zhp, void *data)
4417 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4418 char value[MAXNAMELEN];
4419 zprop_source_t srctype;
4422 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4425 * Skip the special fake placeholder. This will also skip
4426 * over the name property when 'all' is specified.
4428 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4429 pl == cbp->cb_proplist)
4432 if (zpool_get_prop(zhp, pl->pl_prop,
4433 value, sizeof (value), &srctype) != 0)
4436 zprop_print_one_property(zpool_get_name(zhp), cbp,
4437 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4444 zpool_do_get(int argc, char **argv)
4446 zprop_get_cbdata_t cb = { 0 };
4447 zprop_list_t fake_name = { 0 };
4453 cb.cb_first = B_TRUE;
4454 cb.cb_sources = ZPROP_SRC_ALL;
4455 cb.cb_columns[0] = GET_COL_NAME;
4456 cb.cb_columns[1] = GET_COL_PROPERTY;
4457 cb.cb_columns[2] = GET_COL_VALUE;
4458 cb.cb_columns[3] = GET_COL_SOURCE;
4459 cb.cb_type = ZFS_TYPE_POOL;
4461 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4462 ZFS_TYPE_POOL) != 0)
4465 if (cb.cb_proplist != NULL) {
4466 fake_name.pl_prop = ZPOOL_PROP_NAME;
4467 fake_name.pl_width = strlen(gettext("NAME"));
4468 fake_name.pl_next = cb.cb_proplist;
4469 cb.cb_proplist = &fake_name;
4472 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4475 if (cb.cb_proplist == &fake_name)
4476 zprop_free_list(fake_name.pl_next);
4478 zprop_free_list(cb.cb_proplist);
4483 typedef struct set_cbdata {
4486 boolean_t cb_any_successful;
4490 set_callback(zpool_handle_t *zhp, void *data)
4493 set_cbdata_t *cb = (set_cbdata_t *)data;
4495 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4498 cb->cb_any_successful = B_TRUE;
4504 zpool_do_set(int argc, char **argv)
4506 set_cbdata_t cb = { 0 };
4509 if (argc > 1 && argv[1][0] == '-') {
4510 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4516 (void) fprintf(stderr, gettext("missing property=value "
4522 (void) fprintf(stderr, gettext("missing pool name\n"));
4527 (void) fprintf(stderr, gettext("too many pool names\n"));
4531 cb.cb_propname = argv[1];
4532 cb.cb_value = strchr(cb.cb_propname, '=');
4533 if (cb.cb_value == NULL) {
4534 (void) fprintf(stderr, gettext("missing value in "
4535 "property=value argument\n"));
4539 *(cb.cb_value) = '\0';
4542 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4549 find_command_idx(char *command, int *idx)
4553 for (i = 0; i < NCOMMAND; i++) {
4554 if (command_table[i].name == NULL)
4557 if (strcmp(command, command_table[i].name) == 0) {
4566 main(int argc, char **argv)
4572 (void) setlocale(LC_ALL, "");
4573 (void) textdomain(TEXT_DOMAIN);
4575 if ((g_zfs = libzfs_init()) == NULL) {
4576 (void) fprintf(stderr, gettext("internal error: failed to "
4577 "initialize ZFS library\n"));
4581 libzfs_print_on_error(g_zfs, B_TRUE);
4586 * Make sure the user has specified some command.
4589 (void) fprintf(stderr, gettext("missing command\n"));
4598 if (strcmp(cmdname, "-?") == 0)
4601 zpool_set_history_str("zpool", argc, argv, history_str);
4602 verify(zpool_stage_history(g_zfs, history_str) == 0);
4605 * Run the appropriate command.
4607 if (find_command_idx(cmdname, &i) == 0) {
4608 current_command = &command_table[i];
4609 ret = command_table[i].func(argc - 1, argv + 1);
4610 } else if (strchr(cmdname, '=')) {
4611 verify(find_command_idx("set", &i) == 0);
4612 current_command = &command_table[i];
4613 ret = command_table[i].func(argc, argv);
4614 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4616 * 'freeze' is a vile debugging abomination, so we treat
4620 int fd = open(ZFS_DEV, O_RDWR);
4621 (void) strcpy((void *)buf, argv[2]);
4622 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4624 (void) fprintf(stderr, gettext("unrecognized "
4625 "command '%s'\n"), cmdname);
4632 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4633 * for the purposes of running ::findleaks.
4635 if (getenv("ZFS_ABORT") != NULL) {
4636 (void) printf("dumping core by request\n");