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 **);
61 static int zpool_do_list(int, char **);
62 static int zpool_do_iostat(int, char **);
63 static int zpool_do_status(int, char **);
65 static int zpool_do_online(int, char **);
66 static int zpool_do_offline(int, char **);
67 static int zpool_do_clear(int, char **);
69 static int zpool_do_attach(int, char **);
70 static int zpool_do_detach(int, char **);
71 static int zpool_do_replace(int, char **);
72 static int zpool_do_split(int, char **);
74 static int zpool_do_scrub(int, char **);
76 static int zpool_do_import(int, char **);
77 static int zpool_do_export(int, char **);
79 static int zpool_do_upgrade(int, char **);
81 static int zpool_do_history(int, char **);
83 static int zpool_do_get(int, char **);
84 static int zpool_do_set(int, char **);
87 * These libumem hooks provide a reasonable set of defaults for the allocator's
88 * debugging facilities.
93 _umem_debug_init(void)
95 return ("default,verbose"); /* $UMEM_DEBUG setting */
99 _umem_logging_init(void)
101 return ("fail,contents"); /* $UMEM_LOGGING setting */
130 typedef struct zpool_command {
132 int (*func)(int, char **);
137 * Master command table. Each ZFS command has a name, associated function, and
138 * usage message. The usage messages need to be internationalized, so we have
139 * to have a function to return the usage message based on a command index.
141 * These commands are organized according to how they are displayed in the usage
142 * message. An empty command (one with a NULL name) indicates an empty line in
143 * the generic usage message.
145 static zpool_command_t command_table[] = {
146 { "create", zpool_do_create, HELP_CREATE },
147 { "destroy", zpool_do_destroy, HELP_DESTROY },
149 { "add", zpool_do_add, HELP_ADD },
150 { "remove", zpool_do_remove, HELP_REMOVE },
152 { "list", zpool_do_list, HELP_LIST },
153 { "iostat", zpool_do_iostat, HELP_IOSTAT },
154 { "status", zpool_do_status, HELP_STATUS },
156 { "online", zpool_do_online, HELP_ONLINE },
157 { "offline", zpool_do_offline, HELP_OFFLINE },
158 { "clear", zpool_do_clear, HELP_CLEAR },
160 { "attach", zpool_do_attach, HELP_ATTACH },
161 { "detach", zpool_do_detach, HELP_DETACH },
162 { "replace", zpool_do_replace, HELP_REPLACE },
163 { "split", zpool_do_split, HELP_SPLIT },
165 { "scrub", zpool_do_scrub, HELP_SCRUB },
167 { "import", zpool_do_import, HELP_IMPORT },
168 { "export", zpool_do_export, HELP_EXPORT },
169 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
171 { "history", zpool_do_history, HELP_HISTORY },
172 { "get", zpool_do_get, HELP_GET },
173 { "set", zpool_do_set, HELP_SET },
176 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
178 zpool_command_t *current_command;
179 static char history_str[HIS_MAX_RECORD_LEN];
181 static uint_t timestamp_fmt = NODATE;
184 get_usage(zpool_help_t idx) {
187 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
189 return (gettext("\tattach [-f] <pool> <device> "
192 return (gettext("\tclear [-nF] <pool> [device]\n"));
194 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
195 "\t [-O file-system-property=value] ... \n"
196 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
198 return (gettext("\tdestroy [-f] <pool>\n"));
200 return (gettext("\tdetach <pool> <device>\n"));
202 return (gettext("\texport [-f] <pool> ...\n"));
204 return (gettext("\thistory [-il] [<pool>] ...\n"));
206 return (gettext("\timport [-d dir] [-D]\n"
207 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
208 "\timport [-o mntopts] [-o property=value] ... \n"
209 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
210 "[-R root] [-F [-n]] -a\n"
211 "\timport [-o mntopts] [-o property=value] ... \n"
212 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
213 "[-R root] [-F [-n]]\n"
214 "\t <pool | id> [newpool]\n"));
216 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
219 return (gettext("\tlist [-H] [-o property[,...]] "
220 "[-T d|u] [pool] ... [interval [count]]\n"));
222 return (gettext("\toffline [-t] <pool> <device> ...\n"));
224 return (gettext("\tonline <pool> <device> ...\n"));
226 return (gettext("\treplace [-f] <pool> <device> "
229 return (gettext("\tremove <pool> <device> ...\n"));
231 return (gettext("\tscrub [-s] <pool> ...\n"));
233 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
236 return (gettext("\tupgrade\n"
238 "\tupgrade [-V version] <-a | pool ...>\n"));
240 return (gettext("\tget <\"all\" | property[,...]> "
243 return (gettext("\tset <property=value> <pool> \n"));
245 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
246 "\t [-o property=value] <pool> <newpool> "
247 "[<device> ...]\n"));
256 * Callback routine that will print out a pool property value.
259 print_prop_cb(int prop, void *cb)
263 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
265 if (zpool_prop_readonly(prop))
266 (void) fprintf(fp, " NO ");
268 (void) fprintf(fp, " YES ");
270 if (zpool_prop_values(prop) == NULL)
271 (void) fprintf(fp, "-\n");
273 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
279 * Display usage message. If we're inside a command, display only the usage for
280 * that command. Otherwise, iterate over the entire command table and display
281 * a complete usage message.
284 usage(boolean_t requested)
286 FILE *fp = requested ? stdout : stderr;
288 if (current_command == NULL) {
291 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
293 gettext("where 'command' is one of the following:\n\n"));
295 for (i = 0; i < NCOMMAND; i++) {
296 if (command_table[i].name == NULL)
297 (void) fprintf(fp, "\n");
299 (void) fprintf(fp, "%s",
300 get_usage(command_table[i].usage));
303 (void) fprintf(fp, gettext("usage:\n"));
304 (void) fprintf(fp, "%s", get_usage(current_command->usage));
307 if (current_command != NULL &&
308 ((strcmp(current_command->name, "set") == 0) ||
309 (strcmp(current_command->name, "get") == 0) ||
310 (strcmp(current_command->name, "list") == 0))) {
313 gettext("\nthe following properties are supported:\n"));
315 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
316 "PROPERTY", "EDIT", "VALUES");
318 /* Iterate over all properties */
319 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
324 * See comments at end of main().
326 if (getenv("ZFS_ABORT") != NULL) {
327 (void) printf("dumping core by request\n");
331 exit(requested ? 0 : 2);
335 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
336 boolean_t print_logs)
343 (void) printf("\t%*s%s\n", indent, "", name);
345 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
346 &child, &children) != 0)
349 for (c = 0; c < children; c++) {
350 uint64_t is_log = B_FALSE;
352 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
354 if ((is_log && !print_logs) || (!is_log && print_logs))
357 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
358 print_vdev_tree(zhp, vname, child[c], indent + 2,
365 * Add a property pair (name, string-value) into a property nvlist.
368 add_prop_list(const char *propname, char *propval, nvlist_t **props,
371 zpool_prop_t prop = ZPROP_INVAL;
377 if (*props == NULL &&
378 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
379 (void) fprintf(stderr,
380 gettext("internal error: out of memory\n"));
387 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
388 (void) fprintf(stderr, gettext("property '%s' is "
389 "not a valid pool property\n"), propname);
392 normnm = zpool_prop_to_name(prop);
394 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
395 normnm = zfs_prop_to_name(fprop);
401 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
402 prop != ZPOOL_PROP_CACHEFILE) {
403 (void) fprintf(stderr, gettext("property '%s' "
404 "specified multiple times\n"), propname);
408 if (nvlist_add_string(proplist, normnm, propval) != 0) {
409 (void) fprintf(stderr, gettext("internal "
410 "error: out of memory\n"));
418 * zpool add [-fn] <pool> <vdev> ...
420 * -f Force addition of devices, even if they appear in use
421 * -n Do not add the devices, but display the resulting layout if
422 * they were to be added.
424 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
425 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
429 zpool_do_add(int argc, char **argv)
431 boolean_t force = B_FALSE;
432 boolean_t dryrun = B_FALSE;
441 while ((c = getopt(argc, argv, "fn")) != -1) {
450 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
459 /* get pool name and check number of arguments */
461 (void) fprintf(stderr, gettext("missing pool name argument\n"));
465 (void) fprintf(stderr, gettext("missing vdev specification\n"));
474 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
477 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
478 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
484 /* pass off to get_vdev_spec for processing */
485 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
487 if (nvroot == NULL) {
493 nvlist_t *poolnvroot;
495 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
498 (void) printf(gettext("would update '%s' to the following "
499 "configuration:\n"), zpool_get_name(zhp));
501 /* print original main pool and new tree */
502 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
503 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
505 /* Do the same for the logs */
506 if (num_logs(poolnvroot) > 0) {
507 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
508 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
509 } else if (num_logs(nvroot) > 0) {
510 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
515 ret = (zpool_add(zhp, nvroot) != 0);
525 * zpool remove <pool> <vdev> ...
527 * Removes the given vdev from the pool. Currently, this supports removing
528 * spares, cache, and log devices from the pool.
531 zpool_do_remove(int argc, char **argv)
540 /* get pool name and check number of arguments */
542 (void) fprintf(stderr, gettext("missing pool name argument\n"));
546 (void) fprintf(stderr, gettext("missing device\n"));
552 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
555 for (i = 1; i < argc; i++) {
556 if (zpool_vdev_remove(zhp, argv[i]) != 0)
564 * zpool create [-fn] [-o property=value] ...
565 * [-O file-system-property=value] ...
566 * [-R root] [-m mountpoint] <pool> <dev> ...
568 * -f Force creation, even if devices appear in use
569 * -n Do not create the pool, but display the resulting layout if it
570 * were to be created.
571 * -R Create a pool under an alternate root
572 * -m Set default mountpoint for the root dataset. By default it's
574 * -o Set property=value.
575 * -O Set fsproperty=value in the pool's root file system
577 * Creates the named pool according to the given vdev specification. The
578 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
579 * we get the nvlist back from get_vdev_spec(), we either print out the contents
580 * (if '-n' was specified), or pass it to libzfs to do the creation.
583 zpool_do_create(int argc, char **argv)
585 boolean_t force = B_FALSE;
586 boolean_t dryrun = B_FALSE;
588 nvlist_t *nvroot = NULL;
591 char *altroot = NULL;
592 char *mountpoint = NULL;
593 nvlist_t *fsprops = NULL;
594 nvlist_t *props = NULL;
598 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
608 if (add_prop_list(zpool_prop_to_name(
609 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
611 if (nvlist_lookup_string(props,
612 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
615 if (add_prop_list(zpool_prop_to_name(
616 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
623 if ((propval = strchr(optarg, '=')) == NULL) {
624 (void) fprintf(stderr, gettext("missing "
625 "'=' for -o option\n"));
631 if (add_prop_list(optarg, propval, &props, B_TRUE))
635 if ((propval = strchr(optarg, '=')) == NULL) {
636 (void) fprintf(stderr, gettext("missing "
637 "'=' for -O option\n"));
643 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
647 (void) fprintf(stderr, gettext("missing argument for "
648 "'%c' option\n"), optopt);
651 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
660 /* get pool name and check number of arguments */
662 (void) fprintf(stderr, gettext("missing pool name argument\n"));
666 (void) fprintf(stderr, gettext("missing vdev specification\n"));
673 * As a special case, check for use of '/' in the name, and direct the
674 * user to use 'zfs create' instead.
676 if (strchr(poolname, '/') != NULL) {
677 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
678 "character '/' in pool name\n"), poolname);
679 (void) fprintf(stderr, gettext("use 'zfs create' to "
680 "create a dataset\n"));
684 /* pass off to get_vdev_spec for bulk processing */
685 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
690 /* make_root_vdev() allows 0 toplevel children if there are spares */
691 if (!zfs_allocatable_devs(nvroot)) {
692 (void) fprintf(stderr, gettext("invalid vdev "
693 "specification: at least one toplevel vdev must be "
699 if (altroot != NULL && altroot[0] != '/') {
700 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
701 "must be an absolute path\n"), altroot);
706 * Check the validity of the mountpoint and direct the user to use the
707 * '-m' mountpoint option if it looks like its in use.
709 if (mountpoint == NULL ||
710 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
711 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
712 char buf[MAXPATHLEN];
715 if (mountpoint && mountpoint[0] != '/') {
716 (void) fprintf(stderr, gettext("invalid mountpoint "
717 "'%s': must be an absolute path, 'legacy', or "
718 "'none'\n"), mountpoint);
722 if (mountpoint == NULL) {
724 (void) snprintf(buf, sizeof (buf), "%s/%s",
727 (void) snprintf(buf, sizeof (buf), "/%s",
731 (void) snprintf(buf, sizeof (buf), "%s%s",
732 altroot, mountpoint);
734 (void) snprintf(buf, sizeof (buf), "%s",
738 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
739 (void) fprintf(stderr, gettext("mountpoint '%s' : "
740 "%s\n"), buf, strerror(errno));
741 (void) fprintf(stderr, gettext("use '-m' "
742 "option to provide a different default\n"));
747 while (count < 3 && readdir(dirp) != NULL)
749 (void) closedir(dirp);
752 (void) fprintf(stderr, gettext("mountpoint "
753 "'%s' exists and is not empty\n"), buf);
754 (void) fprintf(stderr, gettext("use '-m' "
755 "option to provide a "
756 "different default\n"));
764 * For a dry run invocation, print out a basic message and run
765 * through all the vdevs in the list and print out in an
766 * appropriate hierarchy.
768 (void) printf(gettext("would create '%s' with the "
769 "following layout:\n\n"), poolname);
771 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
772 if (num_logs(nvroot) > 0)
773 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
778 * Hand off to libzfs.
780 if (zpool_create(g_zfs, poolname,
781 nvroot, props, fsprops) == 0) {
782 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
783 ZFS_TYPE_FILESYSTEM);
785 if (mountpoint != NULL)
786 verify(zfs_prop_set(pool,
788 ZFS_PROP_MOUNTPOINT),
790 if (zfs_mount(pool, NULL, 0) == 0)
791 ret = zfs_shareall(pool);
794 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
795 (void) fprintf(stderr, gettext("pool name may have "
802 nvlist_free(fsprops);
806 nvlist_free(fsprops);
813 * zpool destroy <pool>
815 * -f Forcefully unmount any datasets
817 * Destroy the given pool. Automatically unmounts any datasets in the pool.
820 zpool_do_destroy(int argc, char **argv)
822 boolean_t force = B_FALSE;
829 while ((c = getopt(argc, argv, "f")) != -1) {
835 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
844 /* check arguments */
846 (void) fprintf(stderr, gettext("missing pool argument\n"));
850 (void) fprintf(stderr, gettext("too many arguments\n"));
856 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
858 * As a special case, check for use of '/' in the name, and
859 * direct the user to use 'zfs destroy' instead.
861 if (strchr(pool, '/') != NULL)
862 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
863 "destroy a dataset\n"));
867 if (zpool_disable_datasets(zhp, force) != 0) {
868 (void) fprintf(stderr, gettext("could not destroy '%s': "
869 "could not unmount datasets\n"), zpool_get_name(zhp));
873 ret = (zpool_destroy(zhp) != 0);
881 * zpool export [-f] <pool> ...
883 * -f Forcefully unmount datasets
885 * Export the given pools. By default, the command will attempt to cleanly
886 * unmount any active datasets within the pool. If the '-f' flag is specified,
887 * then the datasets will be forcefully unmounted.
890 zpool_do_export(int argc, char **argv)
892 boolean_t force = B_FALSE;
893 boolean_t hardforce = B_FALSE;
900 while ((c = getopt(argc, argv, "fF")) != -1) {
909 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
918 /* check arguments */
920 (void) fprintf(stderr, gettext("missing pool argument\n"));
925 for (i = 0; i < argc; i++) {
926 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
931 if (zpool_disable_datasets(zhp, force) != 0) {
938 if (zpool_export_force(zhp) != 0)
940 } else if (zpool_export(zhp, force) != 0) {
951 * Given a vdev configuration, determine the maximum width needed for the device
955 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
957 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
962 if (strlen(name) + depth > max)
963 max = strlen(name) + depth;
967 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
968 &child, &children) == 0) {
969 for (c = 0; c < children; c++)
970 if ((ret = max_width(zhp, child[c], depth + 2,
975 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
976 &child, &children) == 0) {
977 for (c = 0; c < children; c++)
978 if ((ret = max_width(zhp, child[c], depth + 2,
983 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
984 &child, &children) == 0) {
985 for (c = 0; c < children; c++)
986 if ((ret = max_width(zhp, child[c], depth + 2,
995 typedef struct spare_cbdata {
997 zpool_handle_t *cb_zhp;
1001 find_vdev(nvlist_t *nv, uint64_t search)
1007 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1011 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1012 &child, &children) == 0) {
1013 for (c = 0; c < children; c++)
1014 if (find_vdev(child[c], search))
1022 find_spare(zpool_handle_t *zhp, void *data)
1024 spare_cbdata_t *cbp = data;
1025 nvlist_t *config, *nvroot;
1027 config = zpool_get_config(zhp, NULL);
1028 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1031 if (find_vdev(nvroot, cbp->cb_guid)) {
1041 * Print out configuration state as requested by status_callback.
1044 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1045 int namewidth, int depth, boolean_t isspare)
1049 pool_scan_stat_t *ps = NULL;
1051 char rbuf[6], wbuf[6], cbuf[6];
1053 uint64_t notpresent;
1057 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1058 &child, &children) != 0)
1061 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1062 (uint64_t **)&vs, &c) == 0);
1064 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1067 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1070 if (vs->vs_aux == VDEV_AUX_SPARED)
1072 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1076 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1080 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1081 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1082 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1083 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1086 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1087 ¬present) == 0) {
1089 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1090 (void) printf(" was %s", path);
1091 } else if (vs->vs_aux != 0) {
1094 switch (vs->vs_aux) {
1095 case VDEV_AUX_OPEN_FAILED:
1096 (void) printf(gettext("cannot open"));
1099 case VDEV_AUX_BAD_GUID_SUM:
1100 (void) printf(gettext("missing device"));
1103 case VDEV_AUX_NO_REPLICAS:
1104 (void) printf(gettext("insufficient replicas"));
1107 case VDEV_AUX_VERSION_NEWER:
1108 (void) printf(gettext("newer version"));
1111 case VDEV_AUX_SPARED:
1112 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1114 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1115 if (strcmp(zpool_get_name(cb.cb_zhp),
1116 zpool_get_name(zhp)) == 0)
1117 (void) printf(gettext("currently in "
1120 (void) printf(gettext("in use by "
1122 zpool_get_name(cb.cb_zhp));
1123 zpool_close(cb.cb_zhp);
1125 (void) printf(gettext("currently in use"));
1129 case VDEV_AUX_ERR_EXCEEDED:
1130 (void) printf(gettext("too many errors"));
1133 case VDEV_AUX_IO_FAILURE:
1134 (void) printf(gettext("experienced I/O failures"));
1137 case VDEV_AUX_BAD_LOG:
1138 (void) printf(gettext("bad intent log"));
1141 case VDEV_AUX_EXTERNAL:
1142 (void) printf(gettext("external device fault"));
1145 case VDEV_AUX_SPLIT_POOL:
1146 (void) printf(gettext("split into new pool"));
1150 (void) printf(gettext("corrupted data"));
1155 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1156 (uint64_t **)&ps, &c);
1158 if (ps && ps->pss_state == DSS_SCANNING &&
1159 vs->vs_scan_processed != 0 && children == 0) {
1160 (void) printf(gettext(" (%s)"),
1161 (ps->pss_func == POOL_SCAN_RESILVER) ?
1162 "resilvering" : "repairing");
1165 (void) printf("\n");
1167 for (c = 0; c < children; c++) {
1168 uint64_t islog = B_FALSE, ishole = B_FALSE;
1170 /* Don't print logs or holes here */
1171 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1173 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1175 if (islog || ishole)
1177 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1178 print_status_config(zhp, vname, child[c],
1179 namewidth, depth + 2, isspare);
1186 * Print the configuration of an exported pool. Iterate over all vdevs in the
1187 * pool, printing out the name and status for each one.
1190 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1197 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1198 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1199 strcmp(type, VDEV_TYPE_HOLE) == 0)
1202 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1203 (uint64_t **)&vs, &c) == 0);
1205 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1206 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1208 if (vs->vs_aux != 0) {
1211 switch (vs->vs_aux) {
1212 case VDEV_AUX_OPEN_FAILED:
1213 (void) printf(gettext("cannot open"));
1216 case VDEV_AUX_BAD_GUID_SUM:
1217 (void) printf(gettext("missing device"));
1220 case VDEV_AUX_NO_REPLICAS:
1221 (void) printf(gettext("insufficient replicas"));
1224 case VDEV_AUX_VERSION_NEWER:
1225 (void) printf(gettext("newer version"));
1228 case VDEV_AUX_ERR_EXCEEDED:
1229 (void) printf(gettext("too many errors"));
1233 (void) printf(gettext("corrupted data"));
1237 (void) printf("\n");
1239 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1240 &child, &children) != 0)
1243 for (c = 0; c < children; c++) {
1244 uint64_t is_log = B_FALSE;
1246 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1251 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1252 print_import_config(vname, child[c], namewidth, depth + 2);
1256 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1257 &child, &children) == 0) {
1258 (void) printf(gettext("\tcache\n"));
1259 for (c = 0; c < children; c++) {
1260 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1261 (void) printf("\t %s\n", vname);
1266 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1267 &child, &children) == 0) {
1268 (void) printf(gettext("\tspares\n"));
1269 for (c = 0; c < children; c++) {
1270 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1271 (void) printf("\t %s\n", vname);
1279 * Logs are recorded as top level vdevs in the main pool child array
1280 * but with "is_log" set to 1. We use either print_status_config() or
1281 * print_import_config() to print the top level logs then any log
1282 * children (eg mirrored slogs) are printed recursively - which
1283 * works because only the top level vdev is marked "is_log"
1286 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1291 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1295 (void) printf(gettext("\tlogs\n"));
1297 for (c = 0; c < children; c++) {
1298 uint64_t is_log = B_FALSE;
1301 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1305 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1307 print_status_config(zhp, name, child[c], namewidth,
1310 print_import_config(name, child[c], namewidth, 2);
1316 * Display the status for the given pool.
1319 show_import(nvlist_t *config)
1321 uint64_t pool_state;
1332 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1334 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1336 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1338 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1341 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1342 (uint64_t **)&vs, &vsc) == 0);
1343 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1345 reason = zpool_import_status(config, &msgid);
1347 (void) printf(gettext(" pool: %s\n"), name);
1348 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1349 (void) printf(gettext(" state: %s"), health);
1350 if (pool_state == POOL_STATE_DESTROYED)
1351 (void) printf(gettext(" (DESTROYED)"));
1352 (void) printf("\n");
1355 case ZPOOL_STATUS_MISSING_DEV_R:
1356 case ZPOOL_STATUS_MISSING_DEV_NR:
1357 case ZPOOL_STATUS_BAD_GUID_SUM:
1358 (void) printf(gettext("status: One or more devices are missing "
1359 "from the system.\n"));
1362 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1363 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1364 (void) printf(gettext("status: One or more devices contains "
1365 "corrupted data.\n"));
1368 case ZPOOL_STATUS_CORRUPT_DATA:
1369 (void) printf(gettext("status: The pool data is corrupted.\n"));
1372 case ZPOOL_STATUS_OFFLINE_DEV:
1373 (void) printf(gettext("status: One or more devices "
1374 "are offlined.\n"));
1377 case ZPOOL_STATUS_CORRUPT_POOL:
1378 (void) printf(gettext("status: The pool metadata is "
1382 case ZPOOL_STATUS_VERSION_OLDER:
1383 (void) printf(gettext("status: The pool is formatted using an "
1384 "older on-disk version.\n"));
1387 case ZPOOL_STATUS_VERSION_NEWER:
1388 (void) printf(gettext("status: The pool is formatted using an "
1389 "incompatible version.\n"));
1392 case ZPOOL_STATUS_HOSTID_MISMATCH:
1393 (void) printf(gettext("status: The pool was last accessed by "
1394 "another system.\n"));
1397 case ZPOOL_STATUS_FAULTED_DEV_R:
1398 case ZPOOL_STATUS_FAULTED_DEV_NR:
1399 (void) printf(gettext("status: One or more devices are "
1403 case ZPOOL_STATUS_BAD_LOG:
1404 (void) printf(gettext("status: An intent log record cannot be "
1408 case ZPOOL_STATUS_RESILVERING:
1409 (void) printf(gettext("status: One or more devices were being "
1415 * No other status can be seen when importing pools.
1417 assert(reason == ZPOOL_STATUS_OK);
1421 * Print out an action according to the overall state of the pool.
1423 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1424 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1425 (void) printf(gettext("action: The pool can be "
1426 "imported using its name or numeric identifier, "
1427 "though\n\tsome features will not be available "
1428 "without an explicit 'zpool upgrade'.\n"));
1429 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1430 (void) printf(gettext("action: The pool can be "
1431 "imported using its name or numeric "
1432 "identifier and\n\tthe '-f' flag.\n"));
1434 (void) printf(gettext("action: The pool can be "
1435 "imported using its name or numeric "
1437 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1438 (void) printf(gettext("action: The pool can be imported "
1439 "despite missing or damaged devices. The\n\tfault "
1440 "tolerance of the pool may be compromised if imported.\n"));
1443 case ZPOOL_STATUS_VERSION_NEWER:
1444 (void) printf(gettext("action: The pool cannot be "
1445 "imported. Access the pool on a system running "
1446 "newer\n\tsoftware, or recreate the pool from "
1449 case ZPOOL_STATUS_MISSING_DEV_R:
1450 case ZPOOL_STATUS_MISSING_DEV_NR:
1451 case ZPOOL_STATUS_BAD_GUID_SUM:
1452 (void) printf(gettext("action: The pool cannot be "
1453 "imported. Attach the missing\n\tdevices and try "
1457 (void) printf(gettext("action: The pool cannot be "
1458 "imported due to damaged devices or data.\n"));
1463 * If the state is "closed" or "can't open", and the aux state
1464 * is "corrupt data":
1466 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1467 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1468 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1469 if (pool_state == POOL_STATE_DESTROYED)
1470 (void) printf(gettext("\tThe pool was destroyed, "
1471 "but can be imported using the '-Df' flags.\n"));
1472 else if (pool_state != POOL_STATE_EXPORTED)
1473 (void) printf(gettext("\tThe pool may be active on "
1474 "another system, but can be imported using\n\t"
1475 "the '-f' flag.\n"));
1479 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1482 (void) printf(gettext("config:\n\n"));
1484 namewidth = max_width(NULL, nvroot, 0, 0);
1488 print_import_config(name, nvroot, namewidth, 0);
1489 if (num_logs(nvroot) > 0)
1490 print_logs(NULL, nvroot, namewidth, B_FALSE);
1492 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1493 (void) printf(gettext("\n\tAdditional devices are known to "
1494 "be part of this pool, though their\n\texact "
1495 "configuration cannot be determined.\n"));
1500 * Perform the import for the given configuration. This passes the heavy
1501 * lifting off to zpool_import_props(), and then mounts the datasets contained
1505 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1506 nvlist_t *props, int flags)
1508 zpool_handle_t *zhp;
1513 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1516 verify(nvlist_lookup_uint64(config,
1517 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1518 verify(nvlist_lookup_uint64(config,
1519 ZPOOL_CONFIG_VERSION, &version) == 0);
1520 if (version > SPA_VERSION) {
1521 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1522 "is formatted using a newer ZFS version\n"), name);
1524 } else if (state != POOL_STATE_EXPORTED &&
1525 !(flags & ZFS_IMPORT_ANY_HOST)) {
1528 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1530 if ((unsigned long)hostid != gethostid()) {
1535 verify(nvlist_lookup_string(config,
1536 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1537 verify(nvlist_lookup_uint64(config,
1538 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1540 (void) fprintf(stderr, gettext("cannot import "
1541 "'%s': pool may be in use from other "
1542 "system, it was last accessed by %s "
1543 "(hostid: 0x%lx) on %s"), name, hostname,
1544 (unsigned long)hostid,
1545 asctime(localtime(&t)));
1546 (void) fprintf(stderr, gettext("use '-f' to "
1547 "import anyway\n"));
1551 (void) fprintf(stderr, gettext("cannot import '%s': "
1552 "pool may be in use from other system\n"), name);
1553 (void) fprintf(stderr, gettext("use '-f' to import "
1559 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1562 if (newname != NULL)
1563 name = (char *)newname;
1565 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1568 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1569 !(flags & ZFS_IMPORT_ONLY) &&
1570 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1580 * zpool import [-d dir] [-D]
1581 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1582 * [-d dir | -c cachefile] [-f] -a
1583 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1584 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1586 * -c Read pool information from a cachefile instead of searching
1589 * -d Scan in a specific directory, other than /dev/dsk. More than
1590 * one directory can be specified using multiple '-d' options.
1592 * -D Scan for previously destroyed pools or import all or only
1593 * specified destroyed pools.
1595 * -R Temporarily import the pool, with all mountpoints relative to
1596 * the given root. The pool will remain exported when the machine
1599 * -V Import even in the presence of faulted vdevs. This is an
1600 * intentionally undocumented option for testing purposes, and
1601 * treats the pool configuration as complete, leaving any bad
1602 * vdevs in the FAULTED state. In other words, it does verbatim
1605 * -f Force import, even if it appears that the pool is active.
1607 * -F Attempt rewind if necessary.
1609 * -n See if rewind would work, but don't actually rewind.
1611 * -N Import the pool but don't mount datasets.
1613 * -T Specify a starting txg to use for import. This option is
1614 * intentionally undocumented option for testing purposes.
1616 * -a Import all pools found.
1618 * -o Set property=value and/or temporary mount options (without '=').
1620 * The import command scans for pools to import, and import pools based on pool
1621 * name and GUID. The pool can also be renamed as part of the import process.
1624 zpool_do_import(int argc, char **argv)
1626 char **searchdirs = NULL;
1630 nvlist_t *pools = NULL;
1631 boolean_t do_all = B_FALSE;
1632 boolean_t do_destroyed = B_FALSE;
1633 char *mntopts = NULL;
1636 uint64_t searchguid = 0;
1637 char *searchname = NULL;
1639 nvlist_t *found_config;
1640 nvlist_t *policy = NULL;
1641 nvlist_t *props = NULL;
1643 int flags = ZFS_IMPORT_NORMAL;
1644 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1645 boolean_t dryrun = B_FALSE;
1646 boolean_t do_rewind = B_FALSE;
1647 boolean_t xtreme_rewind = B_FALSE;
1648 uint64_t pool_state, txg = -1ULL;
1649 char *cachefile = NULL;
1650 importargs_t idata = { 0 };
1654 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1663 if (searchdirs == NULL) {
1664 searchdirs = safe_malloc(sizeof (char *));
1666 char **tmp = safe_malloc((nsearch + 1) *
1668 bcopy(searchdirs, tmp, nsearch *
1673 searchdirs[nsearch++] = optarg;
1676 do_destroyed = B_TRUE;
1679 flags |= ZFS_IMPORT_ANY_HOST;
1685 flags |= ZFS_IMPORT_MISSING_LOG;
1691 flags |= ZFS_IMPORT_ONLY;
1694 if ((propval = strchr(optarg, '=')) != NULL) {
1697 if (add_prop_list(optarg, propval,
1705 if (add_prop_list(zpool_prop_to_name(
1706 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1708 if (nvlist_lookup_string(props,
1709 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1712 if (add_prop_list(zpool_prop_to_name(
1713 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1718 txg = strtoull(optarg, &endptr, 10);
1719 if (errno != 0 || *endptr != '\0') {
1720 (void) fprintf(stderr,
1721 gettext("invalid txg value\n"));
1724 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1727 flags |= ZFS_IMPORT_VERBATIM;
1730 xtreme_rewind = B_TRUE;
1733 (void) fprintf(stderr, gettext("missing argument for "
1734 "'%c' option\n"), optopt);
1738 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1747 if (cachefile && nsearch != 0) {
1748 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1752 if ((dryrun || xtreme_rewind) && !do_rewind) {
1753 (void) fprintf(stderr,
1754 gettext("-n or -X only meaningful with -F\n"));
1758 rewind_policy = ZPOOL_TRY_REWIND;
1760 rewind_policy = ZPOOL_DO_REWIND;
1762 rewind_policy |= ZPOOL_EXTREME_REWIND;
1764 /* In the future, we can capture further policy and include it here */
1765 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1766 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1767 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1770 if (searchdirs == NULL) {
1771 searchdirs = safe_malloc(sizeof (char *));
1772 searchdirs[0] = "/dev/dsk";
1776 /* check argument count */
1779 (void) fprintf(stderr, gettext("too many arguments\n"));
1784 (void) fprintf(stderr, gettext("too many arguments\n"));
1789 * Check for the SYS_CONFIG privilege. We do this explicitly
1790 * here because otherwise any attempt to discover pools will
1793 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1794 (void) fprintf(stderr, gettext("cannot "
1795 "discover pools: permission denied\n"));
1797 nvlist_free(policy);
1803 * Depending on the arguments given, we do one of the following:
1805 * <none> Iterate through all pools and display information about
1808 * -a Iterate through all pools and try to import each one.
1810 * <id> Find the pool that corresponds to the given GUID/pool
1811 * name and import that one.
1813 * -D Above options applies only to destroyed pools.
1819 searchguid = strtoull(argv[0], &endptr, 10);
1820 if (errno != 0 || *endptr != '\0')
1821 searchname = argv[0];
1822 found_config = NULL;
1825 * User specified a name or guid. Ensure it's unique.
1827 idata.unique = B_TRUE;
1831 idata.path = searchdirs;
1832 idata.paths = nsearch;
1833 idata.poolname = searchname;
1834 idata.guid = searchguid;
1835 idata.cachefile = cachefile;
1837 pools = zpool_search_import(g_zfs, &idata);
1839 if (pools != NULL && idata.exists &&
1840 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1841 (void) fprintf(stderr, gettext("cannot import '%s': "
1842 "a pool with that name already exists\n"),
1844 (void) fprintf(stderr, gettext("use the form '%s "
1845 "<pool | id> <newpool>' to give it a new name\n"),
1848 } else if (pools == NULL && idata.exists) {
1849 (void) fprintf(stderr, gettext("cannot import '%s': "
1850 "a pool with that name is already created/imported,\n"),
1852 (void) fprintf(stderr, gettext("and no additional pools "
1853 "with that name were found\n"));
1855 } else if (pools == NULL) {
1857 (void) fprintf(stderr, gettext("cannot import '%s': "
1858 "no such pool available\n"), argv[0]);
1865 nvlist_free(policy);
1870 * At this point we have a list of import candidate configs. Even if
1871 * we were searching by pool name or guid, we still need to
1872 * post-process the list to deal with pool state and possible
1878 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1880 verify(nvpair_value_nvlist(elem, &config) == 0);
1882 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1884 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1886 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1889 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1896 (void) printf("\n");
1899 err |= do_import(config, NULL, mntopts,
1902 show_import(config);
1904 } else if (searchname != NULL) {
1908 * We are searching for a pool based on name.
1910 verify(nvlist_lookup_string(config,
1911 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1913 if (strcmp(name, searchname) == 0) {
1914 if (found_config != NULL) {
1915 (void) fprintf(stderr, gettext(
1916 "cannot import '%s': more than "
1917 "one matching pool\n"), searchname);
1918 (void) fprintf(stderr, gettext(
1919 "import by numeric ID instead\n"));
1922 found_config = config;
1928 * Search for a pool by guid.
1930 verify(nvlist_lookup_uint64(config,
1931 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1933 if (guid == searchguid)
1934 found_config = config;
1939 * If we were searching for a specific pool, verify that we found a
1940 * pool, and then do the import.
1942 if (argc != 0 && err == 0) {
1943 if (found_config == NULL) {
1944 (void) fprintf(stderr, gettext("cannot import '%s': "
1945 "no such pool available\n"), argv[0]);
1948 err |= do_import(found_config, argc == 1 ? NULL :
1949 argv[1], mntopts, props, flags);
1954 * If we were just looking for pools, report an error if none were
1957 if (argc == 0 && first)
1958 (void) fprintf(stderr,
1959 gettext("no pools available to import\n"));
1964 nvlist_free(policy);
1967 return (err ? 1 : 0);
1970 typedef struct iostat_cbdata {
1971 zpool_list_t *cb_list;
1978 print_iostat_separator(iostat_cbdata_t *cb)
1982 for (i = 0; i < cb->cb_namewidth; i++)
1984 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1988 print_iostat_header(iostat_cbdata_t *cb)
1990 (void) printf("%*s capacity operations bandwidth\n",
1991 cb->cb_namewidth, "");
1992 (void) printf("%-*s alloc free read write read write\n",
1993 cb->cb_namewidth, "pool");
1994 print_iostat_separator(cb);
1998 * Display a single statistic.
2001 print_one_stat(uint64_t value)
2005 zfs_nicenum(value, buf, sizeof (buf));
2006 (void) printf(" %5s", buf);
2010 * Print out all the statistics for the given vdev. This can either be the
2011 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2012 * is a verbose output, and we don't want to display the toplevel pool stats.
2015 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2016 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2018 nvlist_t **oldchild, **newchild;
2020 vdev_stat_t *oldvs, *newvs;
2021 vdev_stat_t zerovs = { 0 };
2026 if (oldnv != NULL) {
2027 verify(nvlist_lookup_uint64_array(oldnv,
2028 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2033 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2034 (uint64_t **)&newvs, &c) == 0);
2036 if (strlen(name) + depth > cb->cb_namewidth)
2037 (void) printf("%*s%s", depth, "", name);
2039 (void) printf("%*s%s%*s", depth, "", name,
2040 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2042 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2047 scale = (double)NANOSEC / tdelta;
2049 /* only toplevel vdevs have capacity stats */
2050 if (newvs->vs_space == 0) {
2051 (void) printf(" - -");
2053 print_one_stat(newvs->vs_alloc);
2054 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2057 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2058 oldvs->vs_ops[ZIO_TYPE_READ])));
2060 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2061 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2063 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2064 oldvs->vs_bytes[ZIO_TYPE_READ])));
2066 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2067 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2069 (void) printf("\n");
2071 if (!cb->cb_verbose)
2074 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2075 &newchild, &children) != 0)
2078 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2079 &oldchild, &c) != 0)
2082 for (c = 0; c < children; c++) {
2083 uint64_t ishole = B_FALSE;
2085 if (nvlist_lookup_uint64(newchild[c],
2086 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2089 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2090 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2091 newchild[c], cb, depth + 2);
2096 * Include level 2 ARC devices in iostat output
2098 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2099 &newchild, &children) != 0)
2102 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2103 &oldchild, &c) != 0)
2107 (void) printf("%-*s - - - - - "
2108 "-\n", cb->cb_namewidth, "cache");
2109 for (c = 0; c < children; c++) {
2110 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2112 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2113 newchild[c], cb, depth + 2);
2120 refresh_iostat(zpool_handle_t *zhp, void *data)
2122 iostat_cbdata_t *cb = data;
2126 * If the pool has disappeared, remove it from the list and continue.
2128 if (zpool_refresh_stats(zhp, &missing) != 0)
2132 pool_list_remove(cb->cb_list, zhp);
2138 * Callback to print out the iostats for the given pool.
2141 print_iostat(zpool_handle_t *zhp, void *data)
2143 iostat_cbdata_t *cb = data;
2144 nvlist_t *oldconfig, *newconfig;
2145 nvlist_t *oldnvroot, *newnvroot;
2147 newconfig = zpool_get_config(zhp, &oldconfig);
2149 if (cb->cb_iteration == 1)
2152 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2155 if (oldconfig == NULL)
2158 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2162 * Print out the statistics for the pool.
2164 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2167 print_iostat_separator(cb);
2173 get_namewidth(zpool_handle_t *zhp, void *data)
2175 iostat_cbdata_t *cb = data;
2176 nvlist_t *config, *nvroot;
2178 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2179 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2181 if (!cb->cb_verbose)
2182 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2184 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2188 * The width must fall into the range [10,38]. The upper limit is the
2189 * maximum we can have and still fit in 80 columns.
2191 if (cb->cb_namewidth < 10)
2192 cb->cb_namewidth = 10;
2193 if (cb->cb_namewidth > 38)
2194 cb->cb_namewidth = 38;
2200 * Parse the input string, get the 'interval' and 'count' value if there is one.
2203 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2206 unsigned long interval = 0, count = 0;
2207 int argc = *argcp, errno;
2210 * Determine if the last argument is an integer or a pool name
2212 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2216 interval = strtoul(argv[argc - 1], &end, 10);
2218 if (*end == '\0' && errno == 0) {
2219 if (interval == 0) {
2220 (void) fprintf(stderr, gettext("interval "
2221 "cannot be zero\n"));
2225 * Ignore the last parameter
2230 * If this is not a valid number, just plow on. The
2231 * user will get a more informative error message later
2239 * If the last argument is also an integer, then we have both a count
2242 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2247 interval = strtoul(argv[argc - 1], &end, 10);
2249 if (*end == '\0' && errno == 0) {
2250 if (interval == 0) {
2251 (void) fprintf(stderr, gettext("interval "
2252 "cannot be zero\n"));
2257 * Ignore the last parameter
2271 get_timestamp_arg(char c)
2274 timestamp_fmt = UDATE;
2276 timestamp_fmt = DDATE;
2282 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2284 * -v Display statistics for individual vdevs
2285 * -T Display a timestamp in date(1) or Unix format
2287 * This command can be tricky because we want to be able to deal with pool
2288 * creation/destruction as well as vdev configuration changes. The bulk of this
2289 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2290 * on pool_list_update() to detect the addition of new pools. Configuration
2291 * changes are all handled within libzfs.
2294 zpool_do_iostat(int argc, char **argv)
2299 unsigned long interval = 0, count = 0;
2301 boolean_t verbose = B_FALSE;
2305 while ((c = getopt(argc, argv, "T:v")) != -1) {
2308 get_timestamp_arg(*optarg);
2314 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2323 get_interval_count(&argc, argv, &interval, &count);
2326 * Construct the list of all interesting pools.
2329 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2332 if (pool_list_count(list) == 0 && argc != 0) {
2333 pool_list_free(list);
2337 if (pool_list_count(list) == 0 && interval == 0) {
2338 pool_list_free(list);
2339 (void) fprintf(stderr, gettext("no pools available\n"));
2344 * Enter the main iostat loop.
2347 cb.cb_verbose = verbose;
2348 cb.cb_iteration = 0;
2349 cb.cb_namewidth = 0;
2352 pool_list_update(list);
2354 if ((npools = pool_list_count(list)) == 0)
2358 * Refresh all statistics. This is done as an explicit step
2359 * before calculating the maximum name width, so that any
2360 * configuration changes are properly accounted for.
2362 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2365 * Iterate over all pools to determine the maximum width
2366 * for the pool / device name column across all pools.
2368 cb.cb_namewidth = 0;
2369 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2371 if (timestamp_fmt != NODATE)
2372 print_timestamp(timestamp_fmt);
2375 * If it's the first time, or verbose mode, print the header.
2377 if (++cb.cb_iteration == 1 || verbose)
2378 print_iostat_header(&cb);
2380 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2383 * If there's more than one pool, and we're not in verbose mode
2384 * (which prints a separator for us), then print a separator.
2386 if (npools > 1 && !verbose)
2387 print_iostat_separator(&cb);
2390 (void) printf("\n");
2393 * Flush the output so that redirection to a file isn't buffered
2396 (void) fflush(stdout);
2401 if (count != 0 && --count == 0)
2404 (void) sleep(interval);
2407 pool_list_free(list);
2412 typedef struct list_cbdata {
2413 boolean_t cb_scripted;
2415 zprop_list_t *cb_proplist;
2419 * Given a list of columns to display, output appropriate headers for each one.
2422 print_header(zprop_list_t *pl)
2425 boolean_t first = B_TRUE;
2426 boolean_t right_justify;
2428 for (; pl != NULL; pl = pl->pl_next) {
2429 if (pl->pl_prop == ZPROP_INVAL)
2437 header = zpool_prop_column_name(pl->pl_prop);
2438 right_justify = zpool_prop_align_right(pl->pl_prop);
2440 if (pl->pl_next == NULL && !right_justify)
2441 (void) printf("%s", header);
2442 else if (right_justify)
2443 (void) printf("%*s", pl->pl_width, header);
2445 (void) printf("%-*s", pl->pl_width, header);
2448 (void) printf("\n");
2452 * Given a pool and a list of properties, print out all the properties according
2453 * to the described layout.
2456 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2458 boolean_t first = B_TRUE;
2459 char property[ZPOOL_MAXPROPLEN];
2461 boolean_t right_justify;
2464 for (; pl != NULL; pl = pl->pl_next) {
2467 (void) printf("\t");
2474 right_justify = B_FALSE;
2475 if (pl->pl_prop != ZPROP_INVAL) {
2476 if (zpool_get_prop(zhp, pl->pl_prop, property,
2477 sizeof (property), NULL) != 0)
2482 right_justify = zpool_prop_align_right(pl->pl_prop);
2487 width = pl->pl_width;
2490 * If this is being called in scripted mode, or if this is the
2491 * last column and it is left-justified, don't include a width
2494 if (scripted || (pl->pl_next == NULL && !right_justify))
2495 (void) printf("%s", propstr);
2496 else if (right_justify)
2497 (void) printf("%*s", width, propstr);
2499 (void) printf("%-*s", width, propstr);
2502 (void) printf("\n");
2506 * Generic callback function to list a pool.
2509 list_callback(zpool_handle_t *zhp, void *data)
2511 list_cbdata_t *cbp = data;
2513 if (cbp->cb_first) {
2514 if (!cbp->cb_scripted)
2515 print_header(cbp->cb_proplist);
2516 cbp->cb_first = B_FALSE;
2519 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2525 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2527 * -H Scripted mode. Don't display headers, and separate properties
2529 * -o List of properties to display. Defaults to
2530 * "name,size,allocated,free,capacity,health,altroot"
2531 * -T Display a timestamp in date(1) or Unix format
2533 * List all pools in the system, whether or not they're healthy. Output space
2534 * statistics for each one, as well as health status summary.
2537 zpool_do_list(int argc, char **argv)
2541 list_cbdata_t cb = { 0 };
2542 static char default_props[] =
2543 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2544 char *props = default_props;
2545 unsigned long interval = 0, count = 0;
2548 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2551 cb.cb_scripted = B_TRUE;
2557 get_timestamp_arg(*optarg);
2560 (void) fprintf(stderr, gettext("missing argument for "
2561 "'%c' option\n"), optopt);
2565 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2574 get_interval_count(&argc, argv, &interval, &count);
2576 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2579 cb.cb_first = B_TRUE;
2583 if (timestamp_fmt != NODATE)
2584 print_timestamp(timestamp_fmt);
2586 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2587 list_callback, &cb);
2589 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2590 (void) printf(gettext("no pools available\n"));
2591 zprop_free_list(cb.cb_proplist);
2598 if (count != 0 && --count == 0)
2601 (void) sleep(interval);
2604 zprop_free_list(cb.cb_proplist);
2609 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2616 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2617 &child, &children) != 0) {
2618 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2619 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2620 name += sizeof(_PATH_DEV) - 1;
2621 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2622 path += sizeof(_PATH_DEV) - 1;
2623 if (strcmp(name, path) == 0)
2628 for (c = 0; c < children; c++)
2629 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2636 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2638 boolean_t force = B_FALSE;
2641 char *poolname, *old_disk, *new_disk;
2642 zpool_handle_t *zhp;
2646 while ((c = getopt(argc, argv, "f")) != -1) {
2652 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2661 /* get pool name and check number of arguments */
2663 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2670 (void) fprintf(stderr,
2671 gettext("missing <device> specification\n"));
2679 (void) fprintf(stderr,
2680 gettext("missing <new_device> specification\n"));
2683 new_disk = old_disk;
2693 (void) fprintf(stderr, gettext("too many arguments\n"));
2697 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2700 if (zpool_get_config(zhp, NULL) == NULL) {
2701 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2707 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2709 if (nvroot == NULL) {
2714 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2716 nvlist_free(nvroot);
2723 * zpool replace [-f] <pool> <device> <new_device>
2725 * -f Force attach, even if <new_device> appears to be in use.
2727 * Replace <device> with <new_device>.
2731 zpool_do_replace(int argc, char **argv)
2733 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2737 * zpool attach [-f] <pool> <device> <new_device>
2739 * -f Force attach, even if <new_device> appears to be in use.
2741 * Attach <new_device> to the mirror containing <device>. If <device> is not
2742 * part of a mirror, then <device> will be transformed into a mirror of
2743 * <device> and <new_device>. In either case, <new_device> will begin life
2744 * with a DTL of [0, now], and will immediately begin to resilver itself.
2747 zpool_do_attach(int argc, char **argv)
2749 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2753 * zpool detach [-f] <pool> <device>
2755 * -f Force detach of <device>, even if DTLs argue against it
2756 * (not supported yet)
2758 * Detach a device from a mirror. The operation will be refused if <device>
2759 * is the last device in the mirror, or if the DTLs indicate that this device
2760 * has the only valid copy of some data.
2764 zpool_do_detach(int argc, char **argv)
2767 char *poolname, *path;
2768 zpool_handle_t *zhp;
2772 while ((c = getopt(argc, argv, "f")) != -1) {
2776 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2785 /* get pool name and check number of arguments */
2787 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2792 (void) fprintf(stderr,
2793 gettext("missing <device> specification\n"));
2800 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2803 ret = zpool_vdev_detach(zhp, path);
2811 * zpool split [-n] [-o prop=val] ...
2813 * [-R altroot] <pool> <newpool> [<device> ...]
2815 * -n Do not split the pool, but display the resulting layout if
2816 * it were to be split.
2817 * -o Set property=value, or set mount options.
2818 * -R Mount the split-off pool under an alternate root.
2820 * Splits the named pool and gives it the new pool name. Devices to be split
2821 * off may be listed, provided that no more than one device is specified
2822 * per top-level vdev mirror. The newly split pool is left in an exported
2823 * state unless -R is specified.
2825 * Restrictions: the top-level of the pool pool must only be made up of
2826 * mirrors; all devices in the pool must be healthy; no device may be
2827 * undergoing a resilvering operation.
2830 zpool_do_split(int argc, char **argv)
2832 char *srcpool, *newpool, *propval;
2833 char *mntopts = NULL;
2836 zpool_handle_t *zhp;
2837 nvlist_t *config, *props = NULL;
2839 flags.dryrun = B_FALSE;
2840 flags.import = B_FALSE;
2843 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
2846 flags.import = B_TRUE;
2848 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
2849 &props, B_TRUE) != 0) {
2856 flags.dryrun = B_TRUE;
2859 if ((propval = strchr(optarg, '=')) != NULL) {
2862 if (add_prop_list(optarg, propval,
2863 &props, B_TRUE) != 0) {
2873 (void) fprintf(stderr, gettext("missing argument for "
2874 "'%c' option\n"), optopt);
2878 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2885 if (!flags.import && mntopts != NULL) {
2886 (void) fprintf(stderr, gettext("setting mntopts is only "
2887 "valid when importing the pool\n"));
2895 (void) fprintf(stderr, gettext("Missing pool name\n"));
2899 (void) fprintf(stderr, gettext("Missing new pool name\n"));
2909 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
2912 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
2913 if (config == NULL) {
2917 (void) printf(gettext("would create '%s' with the "
2918 "following layout:\n\n"), newpool);
2919 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
2921 nvlist_free(config);
2926 if (ret != 0 || flags.dryrun || !flags.import)
2930 * The split was successful. Now we need to open the new
2931 * pool and import it.
2933 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
2935 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2936 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2938 (void) fprintf(stderr, gettext("Split was succssful, but "
2939 "the datasets could not all be mounted\n"));
2940 (void) fprintf(stderr, gettext("Try doing '%s' with a "
2941 "different altroot\n"), "zpool import");
2951 * zpool online <pool> <device> ...
2954 zpool_do_online(int argc, char **argv)
2958 zpool_handle_t *zhp;
2960 vdev_state_t newstate;
2964 while ((c = getopt(argc, argv, "et")) != -1) {
2967 flags |= ZFS_ONLINE_EXPAND;
2971 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2980 /* get pool name and check number of arguments */
2982 (void) fprintf(stderr, gettext("missing pool name\n"));
2986 (void) fprintf(stderr, gettext("missing device name\n"));
2992 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2995 for (i = 1; i < argc; i++) {
2996 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
2997 if (newstate != VDEV_STATE_HEALTHY) {
2998 (void) printf(gettext("warning: device '%s' "
2999 "onlined, but remains in faulted state\n"),
3001 if (newstate == VDEV_STATE_FAULTED)
3002 (void) printf(gettext("use 'zpool "
3003 "clear' to restore a faulted "
3006 (void) printf(gettext("use 'zpool "
3007 "replace' to replace devices "
3008 "that are no longer present\n"));
3021 * zpool offline [-ft] <pool> <device> ...
3023 * -f Force the device into the offline state, even if doing
3024 * so would appear to compromise pool availability.
3025 * (not supported yet)
3027 * -t Only take the device off-line temporarily. The offline
3028 * state will not be persistent across reboots.
3032 zpool_do_offline(int argc, char **argv)
3036 zpool_handle_t *zhp;
3038 boolean_t istmp = B_FALSE;
3041 while ((c = getopt(argc, argv, "ft")) != -1) {
3048 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3057 /* get pool name and check number of arguments */
3059 (void) fprintf(stderr, gettext("missing pool name\n"));
3063 (void) fprintf(stderr, gettext("missing device name\n"));
3069 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3072 for (i = 1; i < argc; i++) {
3073 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3083 * zpool clear <pool> [device]
3085 * Clear all errors associated with a pool or a particular device.
3088 zpool_do_clear(int argc, char **argv)
3092 boolean_t dryrun = B_FALSE;
3093 boolean_t do_rewind = B_FALSE;
3094 boolean_t xtreme_rewind = B_FALSE;
3095 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3096 nvlist_t *policy = NULL;
3097 zpool_handle_t *zhp;
3098 char *pool, *device;
3101 while ((c = getopt(argc, argv, "FnX")) != -1) {
3110 xtreme_rewind = B_TRUE;
3113 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3123 (void) fprintf(stderr, gettext("missing pool name\n"));
3128 (void) fprintf(stderr, gettext("too many arguments\n"));
3132 if ((dryrun || xtreme_rewind) && !do_rewind) {
3133 (void) fprintf(stderr,
3134 gettext("-n or -X only meaningful with -F\n"));
3138 rewind_policy = ZPOOL_TRY_REWIND;
3140 rewind_policy = ZPOOL_DO_REWIND;
3142 rewind_policy |= ZPOOL_EXTREME_REWIND;
3144 /* In future, further rewind policy choices can be passed along here */
3145 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3146 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3150 device = argc == 2 ? argv[1] : NULL;
3152 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3153 nvlist_free(policy);
3157 if (zpool_clear(zhp, device, policy) != 0)
3162 nvlist_free(policy);
3167 typedef struct scrub_cbdata {
3174 scrub_callback(zpool_handle_t *zhp, void *data)
3176 scrub_cbdata_t *cb = data;
3180 * Ignore faulted pools.
3182 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3183 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3184 "currently unavailable\n"), zpool_get_name(zhp));
3188 err = zpool_scan(zhp, cb->cb_type);
3194 * zpool scrub [-s] <pool> ...
3196 * -s Stop. Stops any in-progress scrub.
3199 zpool_do_scrub(int argc, char **argv)
3204 cb.cb_type = POOL_SCAN_SCRUB;
3207 while ((c = getopt(argc, argv, "s")) != -1) {
3210 cb.cb_type = POOL_SCAN_NONE;
3213 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3225 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3229 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3232 typedef struct status_cbdata {
3234 boolean_t cb_allpools;
3235 boolean_t cb_verbose;
3236 boolean_t cb_explain;
3238 boolean_t cb_dedup_stats;
3242 * Print out detailed scrub status.
3245 print_scan_status(pool_scan_stat_t *ps)
3248 uint64_t elapsed, mins_left, hours_left;
3249 uint64_t pass_exam, examined, total;
3251 double fraction_done;
3252 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3254 (void) printf(gettext(" scan: "));
3256 /* If there's never been a scan, there's not much to say. */
3257 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3258 ps->pss_func >= POOL_SCAN_FUNCS) {
3259 (void) printf(gettext("none requested\n"));
3263 start = ps->pss_start_time;
3264 end = ps->pss_end_time;
3265 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3267 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3268 ps->pss_func == POOL_SCAN_RESILVER);
3270 * Scan is finished or canceled.
3272 if (ps->pss_state == DSS_FINISHED) {
3273 uint64_t minutes_taken = (end - start) / 60;
3276 if (ps->pss_func == POOL_SCAN_SCRUB) {
3277 fmt = gettext("scrub repaired %s in %lluh%um with "
3278 "%llu errors on %s");
3279 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3280 fmt = gettext("resilvered %s in %lluh%um with "
3281 "%llu errors on %s");
3284 (void) printf(fmt, processed_buf,
3285 (u_longlong_t)(minutes_taken / 60),
3286 (uint_t)(minutes_taken % 60),
3287 (u_longlong_t)ps->pss_errors,
3288 ctime((time_t *)&end));
3290 } else if (ps->pss_state == DSS_CANCELED) {
3291 if (ps->pss_func == POOL_SCAN_SCRUB) {
3292 (void) printf(gettext("scrub canceled on %s"),
3294 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3295 (void) printf(gettext("resilver canceled on %s"),
3301 assert(ps->pss_state == DSS_SCANNING);
3304 * Scan is in progress.
3306 if (ps->pss_func == POOL_SCAN_SCRUB) {
3307 (void) printf(gettext("scrub in progress since %s"),
3309 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3310 (void) printf(gettext("resilver in progress since %s"),
3314 examined = ps->pss_examined ? ps->pss_examined : 1;
3315 total = ps->pss_to_examine;
3316 fraction_done = (double)examined / total;
3318 /* elapsed time for this pass */
3319 elapsed = time(NULL) - ps->pss_pass_start;
3320 elapsed = elapsed ? elapsed : 1;
3321 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3322 rate = pass_exam / elapsed;
3323 rate = rate ? rate : 1;
3324 mins_left = ((total - examined) / rate) / 60;
3325 hours_left = mins_left / 60;
3327 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3328 zfs_nicenum(total, total_buf, sizeof (total_buf));
3329 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3332 * do not print estimated time if hours_left is more than 30 days
3334 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3335 examined_buf, total_buf, rate_buf);
3336 if (hours_left < (30 * 24)) {
3337 (void) printf(gettext(", %lluh%um to go\n"),
3338 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3340 (void) printf(gettext(
3341 ", (scan is slow, no estimated time)\n"));
3344 if (ps->pss_func == POOL_SCAN_RESILVER) {
3345 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3346 processed_buf, 100 * fraction_done);
3347 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3348 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3349 processed_buf, 100 * fraction_done);
3354 print_error_log(zpool_handle_t *zhp)
3356 nvlist_t *nverrlist = NULL;
3359 size_t len = MAXPATHLEN * 2;
3361 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3362 (void) printf("errors: List of errors unavailable "
3363 "(insufficient privileges)\n");
3367 (void) printf("errors: Permanent errors have been "
3368 "detected in the following files:\n\n");
3370 pathname = safe_malloc(len);
3372 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3374 uint64_t dsobj, obj;
3376 verify(nvpair_value_nvlist(elem, &nv) == 0);
3377 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3379 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3381 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3382 (void) printf("%7s %s\n", "", pathname);
3385 nvlist_free(nverrlist);
3389 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3398 (void) printf(gettext("\tspares\n"));
3400 for (i = 0; i < nspares; i++) {
3401 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3402 print_status_config(zhp, name, spares[i],
3403 namewidth, 2, B_TRUE);
3409 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3418 (void) printf(gettext("\tcache\n"));
3420 for (i = 0; i < nl2cache; i++) {
3421 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3422 print_status_config(zhp, name, l2cache[i],
3423 namewidth, 2, B_FALSE);
3429 print_dedup_stats(nvlist_t *config)
3431 ddt_histogram_t *ddh;
3437 * If the pool was faulted then we may not have been able to
3438 * obtain the config. Otherwise, if have anything in the dedup
3439 * table continue processing the stats.
3441 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3442 (uint64_t **)&ddo, &c) != 0 || ddo->ddo_count == 0)
3445 (void) printf("\n");
3446 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3447 (u_longlong_t)ddo->ddo_count,
3448 (u_longlong_t)ddo->ddo_dspace,
3449 (u_longlong_t)ddo->ddo_mspace);
3451 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3452 (uint64_t **)&dds, &c) == 0);
3453 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3454 (uint64_t **)&ddh, &c) == 0);
3455 zpool_dump_ddt(dds, ddh);
3459 * Display a summary of pool status. Displays a summary such as:
3463 * reason: One or more devices ...
3464 * see: http://www.sun.com/msg/ZFS-xxxx-01
3470 * When given the '-v' option, we print out the complete config. If the '-e'
3471 * option is specified, then we print out error rate information as well.
3474 status_callback(zpool_handle_t *zhp, void *data)
3476 status_cbdata_t *cbp = data;
3477 nvlist_t *config, *nvroot;
3484 config = zpool_get_config(zhp, NULL);
3485 reason = zpool_get_status(zhp, &msgid);
3490 * If we were given 'zpool status -x', only report those pools with
3493 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3494 if (!cbp->cb_allpools) {
3495 (void) printf(gettext("pool '%s' is healthy\n"),
3496 zpool_get_name(zhp));
3498 cbp->cb_first = B_FALSE;
3504 cbp->cb_first = B_FALSE;
3506 (void) printf("\n");
3508 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3510 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3511 (uint64_t **)&vs, &c) == 0);
3512 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3514 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3515 (void) printf(gettext(" state: %s\n"), health);
3518 case ZPOOL_STATUS_MISSING_DEV_R:
3519 (void) printf(gettext("status: One or more devices could not "
3520 "be opened. Sufficient replicas exist for\n\tthe pool to "
3521 "continue functioning in a degraded state.\n"));
3522 (void) printf(gettext("action: Attach the missing device and "
3523 "online it using 'zpool online'.\n"));
3526 case ZPOOL_STATUS_MISSING_DEV_NR:
3527 (void) printf(gettext("status: One or more devices could not "
3528 "be opened. There are insufficient\n\treplicas for the "
3529 "pool to continue functioning.\n"));
3530 (void) printf(gettext("action: Attach the missing device and "
3531 "online it using 'zpool online'.\n"));
3534 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3535 (void) printf(gettext("status: One or more devices could not "
3536 "be used because the label is missing or\n\tinvalid. "
3537 "Sufficient replicas exist for the pool to continue\n\t"
3538 "functioning in a degraded state.\n"));
3539 (void) printf(gettext("action: Replace the device using "
3540 "'zpool replace'.\n"));
3543 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3544 (void) printf(gettext("status: One or more devices could not "
3545 "be used because the label is missing \n\tor invalid. "
3546 "There are insufficient replicas for the pool to "
3547 "continue\n\tfunctioning.\n"));
3548 zpool_explain_recover(zpool_get_handle(zhp),
3549 zpool_get_name(zhp), reason, config);
3552 case ZPOOL_STATUS_FAILING_DEV:
3553 (void) printf(gettext("status: One or more devices has "
3554 "experienced an unrecoverable error. An\n\tattempt was "
3555 "made to correct the error. Applications are "
3557 (void) printf(gettext("action: Determine if the device needs "
3558 "to be replaced, and clear the errors\n\tusing "
3559 "'zpool clear' or replace the device with 'zpool "
3563 case ZPOOL_STATUS_OFFLINE_DEV:
3564 (void) printf(gettext("status: One or more devices has "
3565 "been taken offline by the administrator.\n\tSufficient "
3566 "replicas exist for the pool to continue functioning in "
3567 "a\n\tdegraded state.\n"));
3568 (void) printf(gettext("action: Online the device using "
3569 "'zpool online' or replace the device with\n\t'zpool "
3573 case ZPOOL_STATUS_REMOVED_DEV:
3574 (void) printf(gettext("status: One or more devices has "
3575 "been removed by the administrator.\n\tSufficient "
3576 "replicas exist for the pool to continue functioning in "
3577 "a\n\tdegraded state.\n"));
3578 (void) printf(gettext("action: Online the device using "
3579 "'zpool online' or replace the device with\n\t'zpool "
3583 case ZPOOL_STATUS_RESILVERING:
3584 (void) printf(gettext("status: One or more devices is "
3585 "currently being resilvered. The pool will\n\tcontinue "
3586 "to function, possibly in a degraded state.\n"));
3587 (void) printf(gettext("action: Wait for the resilver to "
3591 case ZPOOL_STATUS_CORRUPT_DATA:
3592 (void) printf(gettext("status: One or more devices has "
3593 "experienced an error resulting in data\n\tcorruption. "
3594 "Applications may be affected.\n"));
3595 (void) printf(gettext("action: Restore the file in question "
3596 "if possible. Otherwise restore the\n\tentire pool from "
3600 case ZPOOL_STATUS_CORRUPT_POOL:
3601 (void) printf(gettext("status: The pool metadata is corrupted "
3602 "and the pool cannot be opened.\n"));
3603 zpool_explain_recover(zpool_get_handle(zhp),
3604 zpool_get_name(zhp), reason, config);
3607 case ZPOOL_STATUS_VERSION_OLDER:
3608 (void) printf(gettext("status: The pool is formatted using an "
3609 "older on-disk format. The pool can\n\tstill be used, but "
3610 "some features are unavailable.\n"));
3611 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3612 "upgrade'. Once this is done, the\n\tpool will no longer "
3613 "be accessible on older software versions.\n"));
3616 case ZPOOL_STATUS_VERSION_NEWER:
3617 (void) printf(gettext("status: The pool has been upgraded to a "
3618 "newer, incompatible on-disk version.\n\tThe pool cannot "
3619 "be accessed on this system.\n"));
3620 (void) printf(gettext("action: Access the pool from a system "
3621 "running more recent software, or\n\trestore the pool from "
3625 case ZPOOL_STATUS_FAULTED_DEV_R:
3626 (void) printf(gettext("status: One or more devices are "
3627 "faulted in response to persistent errors.\n\tSufficient "
3628 "replicas exist for the pool to continue functioning "
3629 "in a\n\tdegraded state.\n"));
3630 (void) printf(gettext("action: Replace the faulted device, "
3631 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3634 case ZPOOL_STATUS_FAULTED_DEV_NR:
3635 (void) printf(gettext("status: One or more devices are "
3636 "faulted in response to persistent errors. There are "
3637 "insufficient replicas for the pool to\n\tcontinue "
3639 (void) printf(gettext("action: Destroy and re-create the pool "
3640 "from a backup source. Manually marking the device\n"
3641 "\trepaired using 'zpool clear' may allow some data "
3642 "to be recovered.\n"));
3645 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3646 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3647 (void) printf(gettext("status: One or more devices are "
3648 "faulted in response to IO failures.\n"));
3649 (void) printf(gettext("action: Make sure the affected devices "
3650 "are connected, then run 'zpool clear'.\n"));
3653 case ZPOOL_STATUS_BAD_LOG:
3654 (void) printf(gettext("status: An intent log record "
3655 "could not be read.\n"
3656 "\tWaiting for adminstrator intervention to fix the "
3657 "faulted pool.\n"));
3658 (void) printf(gettext("action: Either restore the affected "
3659 "device(s) and run 'zpool online',\n"
3660 "\tor ignore the intent log records by running "
3661 "'zpool clear'.\n"));
3666 * The remaining errors can't actually be generated, yet.
3668 assert(reason == ZPOOL_STATUS_OK);
3672 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3675 if (config != NULL) {
3678 nvlist_t **spares, **l2cache;
3679 uint_t nspares, nl2cache;
3680 pool_scan_stat_t *ps = NULL;
3682 (void) nvlist_lookup_uint64_array(nvroot,
3683 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3684 print_scan_status(ps);
3686 namewidth = max_width(zhp, nvroot, 0, 0);
3690 (void) printf(gettext("config:\n\n"));
3691 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3692 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3693 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3694 namewidth, 0, B_FALSE);
3696 if (num_logs(nvroot) > 0)
3697 print_logs(zhp, nvroot, namewidth, B_TRUE);
3698 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3699 &l2cache, &nl2cache) == 0)
3700 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3702 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3703 &spares, &nspares) == 0)
3704 print_spares(zhp, spares, nspares, namewidth);
3706 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3708 nvlist_t *nverrlist = NULL;
3711 * If the approximate error count is small, get a
3712 * precise count by fetching the entire log and
3713 * uniquifying the results.
3715 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3716 zpool_get_errlog(zhp, &nverrlist) == 0) {
3721 while ((elem = nvlist_next_nvpair(nverrlist,
3726 nvlist_free(nverrlist);
3728 (void) printf("\n");
3731 (void) printf(gettext("errors: No known data "
3733 else if (!cbp->cb_verbose)
3734 (void) printf(gettext("errors: %llu data "
3735 "errors, use '-v' for a list\n"),
3736 (u_longlong_t)nerr);
3738 print_error_log(zhp);
3741 if (cbp->cb_dedup_stats)
3742 print_dedup_stats(config);
3744 (void) printf(gettext("config: The configuration cannot be "
3752 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3754 * -v Display complete error logs
3755 * -x Display only pools with potential problems
3756 * -D Display dedup status (undocumented)
3757 * -T Display a timestamp in date(1) or Unix format
3759 * Describes the health status of all pools or some subset.
3762 zpool_do_status(int argc, char **argv)
3766 unsigned long interval = 0, count = 0;
3767 status_cbdata_t cb = { 0 };
3770 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3773 cb.cb_verbose = B_TRUE;
3776 cb.cb_explain = B_TRUE;
3779 cb.cb_dedup_stats = B_TRUE;
3782 get_timestamp_arg(*optarg);
3785 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3794 get_interval_count(&argc, argv, &interval, &count);
3797 cb.cb_allpools = B_TRUE;
3799 cb.cb_first = B_TRUE;
3802 if (timestamp_fmt != NODATE)
3803 print_timestamp(timestamp_fmt);
3805 ret = for_each_pool(argc, argv, B_TRUE, NULL,
3806 status_callback, &cb);
3808 if (argc == 0 && cb.cb_count == 0)
3809 (void) printf(gettext("no pools available\n"));
3810 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3811 (void) printf(gettext("all pools are healthy\n"));
3819 if (count != 0 && --count == 0)
3822 (void) sleep(interval);
3828 typedef struct upgrade_cbdata {
3832 char cb_poolname[ZPOOL_MAXNAMELEN];
3834 uint64_t cb_version;
3839 is_root_pool(zpool_handle_t *zhp)
3841 static struct statfs sfs;
3842 static char *poolname = NULL;
3843 static boolean_t stated = B_FALSE;
3848 if (statfs("/", &sfs) == -1) {
3849 (void) fprintf(stderr,
3850 "Unable to stat root file system: %s.\n",
3854 if (strcmp(sfs.f_fstypename, "zfs") != 0)
3856 poolname = sfs.f_mntfromname;
3857 if ((slash = strchr(poolname, '/')) != NULL)
3860 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
3864 upgrade_cb(zpool_handle_t *zhp, void *arg)
3866 upgrade_cbdata_t *cbp = arg;
3871 config = zpool_get_config(zhp, NULL);
3872 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3875 if (!cbp->cb_newer && version < SPA_VERSION) {
3877 if (cbp->cb_first) {
3878 (void) printf(gettext("The following pools are "
3879 "out of date, and can be upgraded. After "
3880 "being\nupgraded, these pools will no "
3881 "longer be accessible by older software "
3883 (void) printf(gettext("VER POOL\n"));
3884 (void) printf(gettext("--- ------------\n"));
3885 cbp->cb_first = B_FALSE;
3888 (void) printf("%2llu %s\n", (u_longlong_t)version,
3889 zpool_get_name(zhp));
3891 cbp->cb_first = B_FALSE;
3892 ret = zpool_upgrade(zhp, cbp->cb_version);
3894 (void) printf(gettext("Successfully upgraded "
3895 "'%s'\n\n"), zpool_get_name(zhp));
3896 if (cbp->cb_poolname[0] == '\0' &&
3897 is_root_pool(zhp)) {
3898 (void) strlcpy(cbp->cb_poolname,
3899 zpool_get_name(zhp),
3900 sizeof(cbp->cb_poolname));
3904 } else if (cbp->cb_newer && version > SPA_VERSION) {
3905 assert(!cbp->cb_all);
3907 if (cbp->cb_first) {
3908 (void) printf(gettext("The following pools are "
3909 "formatted using a newer software version and\n"
3910 "cannot be accessed on the current system.\n\n"));
3911 (void) printf(gettext("VER POOL\n"));
3912 (void) printf(gettext("--- ------------\n"));
3913 cbp->cb_first = B_FALSE;
3916 (void) printf("%2llu %s\n", (u_longlong_t)version,
3917 zpool_get_name(zhp));
3926 upgrade_one(zpool_handle_t *zhp, void *data)
3928 upgrade_cbdata_t *cbp = data;
3929 uint64_t cur_version;
3932 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3933 (void) printf(gettext("'log' is now a reserved word\n"
3934 "Pool 'log' must be renamed using export and import"
3939 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3940 if (cur_version > cbp->cb_version) {
3941 (void) printf(gettext("Pool '%s' is already formatted "
3942 "using more current version '%llu'.\n"),
3943 zpool_get_name(zhp), cur_version);
3946 if (cur_version == cbp->cb_version) {
3947 (void) printf(gettext("Pool '%s' is already formatted "
3948 "using the current version.\n"), zpool_get_name(zhp));
3952 ret = zpool_upgrade(zhp, cbp->cb_version);
3955 (void) printf(gettext("Successfully upgraded '%s' "
3956 "from version %llu to version %llu\n\n"),
3957 zpool_get_name(zhp), (u_longlong_t)cur_version,
3958 (u_longlong_t)cbp->cb_version);
3959 if (cbp->cb_poolname[0] == '\0' && is_root_pool(zhp)) {
3960 (void) strlcpy(cbp->cb_poolname, zpool_get_name(zhp),
3961 sizeof(cbp->cb_poolname));
3971 * zpool upgrade [-V version] <-a | pool ...>
3973 * With no arguments, display downrev'd ZFS pool available for upgrade.
3974 * Individual pools can be upgraded by specifying the pool, and '-a' will
3975 * upgrade all pools.
3978 zpool_do_upgrade(int argc, char **argv)
3981 upgrade_cbdata_t cb = { 0 };
3983 boolean_t showversions = B_FALSE;
3988 while ((c = getopt(argc, argv, ":avV:")) != -1) {
3994 showversions = B_TRUE;
3997 cb.cb_version = strtoll(optarg, &end, 10);
3998 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
3999 cb.cb_version < SPA_VERSION_1) {
4000 (void) fprintf(stderr,
4001 gettext("invalid version '%s'\n"), optarg);
4006 (void) fprintf(stderr, gettext("missing argument for "
4007 "'%c' option\n"), optopt);
4011 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4022 if (cb.cb_version == 0) {
4023 cb.cb_version = SPA_VERSION;
4024 } else if (!cb.cb_all && argc == 0) {
4025 (void) fprintf(stderr, gettext("-V option is "
4026 "incompatible with other arguments\n"));
4031 if (cb.cb_all || argc != 0) {
4032 (void) fprintf(stderr, gettext("-v option is "
4033 "incompatible with other arguments\n"));
4036 } else if (cb.cb_all) {
4038 (void) fprintf(stderr, gettext("-a option should not "
4039 "be used along with a pool name\n"));
4044 (void) printf(gettext("This system is currently running "
4045 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4046 cb.cb_first = B_TRUE;
4048 (void) printf(gettext("The following versions are "
4050 (void) printf(gettext("VER DESCRIPTION\n"));
4051 (void) printf("--- -----------------------------------------"
4052 "---------------\n");
4053 (void) printf(gettext(" 1 Initial ZFS version\n"));
4054 (void) printf(gettext(" 2 Ditto blocks "
4055 "(replicated metadata)\n"));
4056 (void) printf(gettext(" 3 Hot spares and double parity "
4058 (void) printf(gettext(" 4 zpool history\n"));
4059 (void) printf(gettext(" 5 Compression using the gzip "
4061 (void) printf(gettext(" 6 bootfs pool property\n"));
4062 (void) printf(gettext(" 7 Separate intent log devices\n"));
4063 (void) printf(gettext(" 8 Delegated administration\n"));
4064 (void) printf(gettext(" 9 refquota and refreservation "
4066 (void) printf(gettext(" 10 Cache devices\n"));
4067 (void) printf(gettext(" 11 Improved scrub performance\n"));
4068 (void) printf(gettext(" 12 Snapshot properties\n"));
4069 (void) printf(gettext(" 13 snapused property\n"));
4070 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4071 (void) printf(gettext(" 15 user/group space accounting\n"));
4072 (void) printf(gettext(" 16 stmf property support\n"));
4073 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4074 (void) printf(gettext(" 18 Snapshot user holds\n"));
4075 (void) printf(gettext(" 19 Log device removal\n"));
4076 (void) printf(gettext(" 20 Compression using zle "
4077 "(zero-length encoding)\n"));
4078 (void) printf(gettext(" 21 Deduplication\n"));
4079 (void) printf(gettext(" 22 Received properties\n"));
4080 (void) printf(gettext(" 23 Slim ZIL\n"));
4081 (void) printf(gettext(" 24 System attributes\n"));
4082 (void) printf(gettext(" 25 Improved scrub stats\n"));
4083 (void) printf(gettext(" 26 Improved snapshot deletion "
4085 (void) printf(gettext(" 27 Improved snapshot creation "
4087 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4088 (void) printf(gettext("\nFor more information on a particular "
4089 "version, including supported releases,\n"));
4090 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4091 } else if (argc == 0) {
4094 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4095 notfound = cb.cb_first;
4097 if (!cb.cb_all && ret == 0) {
4099 (void) printf("\n");
4100 cb.cb_first = B_TRUE;
4101 cb.cb_newer = B_TRUE;
4102 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4105 (void) printf("\n");
4111 (void) printf(gettext("All pools are formatted "
4112 "using this version.\n"));
4113 else if (!cb.cb_all)
4114 (void) printf(gettext("Use 'zpool upgrade -v' "
4115 "for a list of available versions and "
4116 "their associated\nfeatures.\n"));
4119 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4123 if (cb.cb_poolname[0] != '\0') {
4125 "If you boot from pool '%s', don't forget to update boot code.\n"
4126 "Assuming you use GPT partitioning and da0 is your boot disk\n"
4127 "the following command will do it:\n"
4129 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
4136 typedef struct hist_cbdata {
4143 * Print out the command history for a specific pool.
4146 get_history_one(zpool_handle_t *zhp, void *data)
4162 char internalstr[MAXPATHLEN];
4163 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4167 cb->first = B_FALSE;
4169 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4171 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4174 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4175 &records, &numrecords) == 0);
4176 for (i = 0; i < numrecords; i++) {
4177 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4181 /* is it an internal event or a standard event? */
4182 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4184 if (cb->internal == 0)
4187 if (nvlist_lookup_uint64(records[i],
4188 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4190 verify(nvlist_lookup_uint64(records[i],
4191 ZPOOL_HIST_TXG, &txg) == 0);
4192 verify(nvlist_lookup_string(records[i],
4193 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4194 if (ievent >= LOG_END)
4196 (void) snprintf(internalstr,
4197 sizeof (internalstr),
4198 "[internal %s txg:%lld] %s",
4199 zfs_history_event_names[ievent], txg,
4201 cmdstr = internalstr;
4204 (void) localtime_r(&tsec, &t);
4205 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4206 (void) printf("%s %s", tbuf, cmdstr);
4209 (void) printf("\n");
4212 (void) printf(" [");
4213 if (nvlist_lookup_uint64(records[i],
4214 ZPOOL_HIST_WHO, &who) == 0) {
4215 pwd = getpwuid((uid_t)who);
4217 (void) printf("user %s on",
4220 (void) printf("user %d on",
4223 (void) printf(gettext("no info]\n"));
4226 if (nvlist_lookup_string(records[i],
4227 ZPOOL_HIST_HOST, &hostname) == 0) {
4228 (void) printf(" %s", hostname);
4230 if (nvlist_lookup_string(records[i],
4231 ZPOOL_HIST_ZONE, &zonename) == 0) {
4232 (void) printf(":%s", zonename);
4236 (void) printf("\n");
4238 (void) printf("\n");
4245 * zpool history <pool>
4247 * Displays the history of commands that modified pools.
4252 zpool_do_history(int argc, char **argv)
4254 hist_cbdata_t cbdata = { 0 };
4258 cbdata.first = B_TRUE;
4260 while ((c = getopt(argc, argv, "li")) != -1) {
4266 cbdata.internal = 1;
4269 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4277 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4280 if (argc == 0 && cbdata.first == B_TRUE) {
4281 (void) printf(gettext("no pools available\n"));
4289 get_callback(zpool_handle_t *zhp, void *data)
4291 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4292 char value[MAXNAMELEN];
4293 zprop_source_t srctype;
4296 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4299 * Skip the special fake placeholder. This will also skip
4300 * over the name property when 'all' is specified.
4302 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4303 pl == cbp->cb_proplist)
4306 if (zpool_get_prop(zhp, pl->pl_prop,
4307 value, sizeof (value), &srctype) != 0)
4310 zprop_print_one_property(zpool_get_name(zhp), cbp,
4311 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4318 zpool_do_get(int argc, char **argv)
4320 zprop_get_cbdata_t cb = { 0 };
4321 zprop_list_t fake_name = { 0 };
4327 cb.cb_first = B_TRUE;
4328 cb.cb_sources = ZPROP_SRC_ALL;
4329 cb.cb_columns[0] = GET_COL_NAME;
4330 cb.cb_columns[1] = GET_COL_PROPERTY;
4331 cb.cb_columns[2] = GET_COL_VALUE;
4332 cb.cb_columns[3] = GET_COL_SOURCE;
4333 cb.cb_type = ZFS_TYPE_POOL;
4335 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4336 ZFS_TYPE_POOL) != 0)
4339 if (cb.cb_proplist != NULL) {
4340 fake_name.pl_prop = ZPOOL_PROP_NAME;
4341 fake_name.pl_width = strlen(gettext("NAME"));
4342 fake_name.pl_next = cb.cb_proplist;
4343 cb.cb_proplist = &fake_name;
4346 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4349 if (cb.cb_proplist == &fake_name)
4350 zprop_free_list(fake_name.pl_next);
4352 zprop_free_list(cb.cb_proplist);
4357 typedef struct set_cbdata {
4360 boolean_t cb_any_successful;
4364 set_callback(zpool_handle_t *zhp, void *data)
4367 set_cbdata_t *cb = (set_cbdata_t *)data;
4369 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4372 cb->cb_any_successful = B_TRUE;
4378 zpool_do_set(int argc, char **argv)
4380 set_cbdata_t cb = { 0 };
4383 if (argc > 1 && argv[1][0] == '-') {
4384 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4390 (void) fprintf(stderr, gettext("missing property=value "
4396 (void) fprintf(stderr, gettext("missing pool name\n"));
4401 (void) fprintf(stderr, gettext("too many pool names\n"));
4405 cb.cb_propname = argv[1];
4406 cb.cb_value = strchr(cb.cb_propname, '=');
4407 if (cb.cb_value == NULL) {
4408 (void) fprintf(stderr, gettext("missing value in "
4409 "property=value argument\n"));
4413 *(cb.cb_value) = '\0';
4416 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4423 find_command_idx(char *command, int *idx)
4427 for (i = 0; i < NCOMMAND; i++) {
4428 if (command_table[i].name == NULL)
4431 if (strcmp(command, command_table[i].name) == 0) {
4440 main(int argc, char **argv)
4446 (void) setlocale(LC_ALL, "");
4447 (void) textdomain(TEXT_DOMAIN);
4449 if ((g_zfs = libzfs_init()) == NULL) {
4450 (void) fprintf(stderr, gettext("internal error: failed to "
4451 "initialize ZFS library\n"));
4455 libzfs_print_on_error(g_zfs, B_TRUE);
4460 * Make sure the user has specified some command.
4463 (void) fprintf(stderr, gettext("missing command\n"));
4472 if (strcmp(cmdname, "-?") == 0)
4475 zpool_set_history_str("zpool", argc, argv, history_str);
4476 verify(zpool_stage_history(g_zfs, history_str) == 0);
4479 * Run the appropriate command.
4481 if (find_command_idx(cmdname, &i) == 0) {
4482 current_command = &command_table[i];
4483 ret = command_table[i].func(argc - 1, argv + 1);
4484 } else if (strchr(cmdname, '=')) {
4485 verify(find_command_idx("set", &i) == 0);
4486 current_command = &command_table[i];
4487 ret = command_table[i].func(argc, argv);
4488 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4490 * 'freeze' is a vile debugging abomination, so we treat
4494 int fd = open(ZFS_DEV, O_RDWR);
4495 (void) strcpy((void *)buf, argv[2]);
4496 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4498 (void) fprintf(stderr, gettext("unrecognized "
4499 "command '%s'\n"), cmdname);
4506 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4507 * for the purposes of running ::findleaks.
4509 if (getenv("ZFS_ABORT") != NULL) {
4510 (void) printf("dumping core by request\n");