4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
26 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
30 * Copyright (c) 2017 Datto Inc.
54 #include <sys/fs/zfs.h>
59 #include "zpool_util.h"
60 #include "zfs_comutil.h"
61 #include "zfeature_common.h"
63 #include "statcommon.h"
65 static int zpool_do_create(int, char **);
66 static int zpool_do_destroy(int, char **);
68 static int zpool_do_add(int, char **);
69 static int zpool_do_remove(int, char **);
70 static int zpool_do_labelclear(int, char **);
72 static int zpool_do_checkpoint(int, char **);
74 static int zpool_do_list(int, char **);
75 static int zpool_do_iostat(int, char **);
76 static int zpool_do_status(int, char **);
78 static int zpool_do_online(int, char **);
79 static int zpool_do_offline(int, char **);
80 static int zpool_do_clear(int, char **);
81 static int zpool_do_reopen(int, char **);
83 static int zpool_do_reguid(int, char **);
85 static int zpool_do_attach(int, char **);
86 static int zpool_do_detach(int, char **);
87 static int zpool_do_replace(int, char **);
88 static int zpool_do_split(int, char **);
90 static int zpool_do_scrub(int, char **);
92 static int zpool_do_import(int, char **);
93 static int zpool_do_export(int, char **);
95 static int zpool_do_upgrade(int, char **);
97 static int zpool_do_history(int, char **);
99 static int zpool_do_get(int, char **);
100 static int zpool_do_set(int, char **);
103 * These libumem hooks provide a reasonable set of defaults for the allocator's
104 * debugging facilities.
109 _umem_debug_init(void)
111 return ("default,verbose"); /* $UMEM_DEBUG setting */
115 _umem_logging_init(void)
117 return ("fail,contents"); /* $UMEM_LOGGING setting */
150 typedef struct zpool_command {
152 int (*func)(int, char **);
157 * Master command table. Each ZFS command has a name, associated function, and
158 * usage message. The usage messages need to be internationalized, so we have
159 * to have a function to return the usage message based on a command index.
161 * These commands are organized according to how they are displayed in the usage
162 * message. An empty command (one with a NULL name) indicates an empty line in
163 * the generic usage message.
165 static zpool_command_t command_table[] = {
166 { "create", zpool_do_create, HELP_CREATE },
167 { "destroy", zpool_do_destroy, HELP_DESTROY },
169 { "add", zpool_do_add, HELP_ADD },
170 { "remove", zpool_do_remove, HELP_REMOVE },
172 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
174 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
176 { "list", zpool_do_list, HELP_LIST },
177 { "iostat", zpool_do_iostat, HELP_IOSTAT },
178 { "status", zpool_do_status, HELP_STATUS },
180 { "online", zpool_do_online, HELP_ONLINE },
181 { "offline", zpool_do_offline, HELP_OFFLINE },
182 { "clear", zpool_do_clear, HELP_CLEAR },
183 { "reopen", zpool_do_reopen, HELP_REOPEN },
185 { "attach", zpool_do_attach, HELP_ATTACH },
186 { "detach", zpool_do_detach, HELP_DETACH },
187 { "replace", zpool_do_replace, HELP_REPLACE },
188 { "split", zpool_do_split, HELP_SPLIT },
190 { "scrub", zpool_do_scrub, HELP_SCRUB },
192 { "import", zpool_do_import, HELP_IMPORT },
193 { "export", zpool_do_export, HELP_EXPORT },
194 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
195 { "reguid", zpool_do_reguid, HELP_REGUID },
197 { "history", zpool_do_history, HELP_HISTORY },
198 { "get", zpool_do_get, HELP_GET },
199 { "set", zpool_do_set, HELP_SET },
202 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
204 static zpool_command_t *current_command;
205 static char history_str[HIS_MAX_RECORD_LEN];
206 static boolean_t log_history = B_TRUE;
207 static uint_t timestamp_fmt = NODATE;
210 get_usage(zpool_help_t idx)
214 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
216 return (gettext("\tattach [-f] <pool> <device> "
219 return (gettext("\tclear [-nF] <pool> [device]\n"));
221 return (gettext("\tcreate [-fnd] [-B] "
222 "[-o property=value] ... \n"
223 "\t [-O file-system-property=value] ... \n"
224 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
225 case HELP_CHECKPOINT:
226 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
228 return (gettext("\tdestroy [-f] <pool>\n"));
230 return (gettext("\tdetach <pool> <device>\n"));
232 return (gettext("\texport [-f] <pool> ...\n"));
234 return (gettext("\thistory [-il] [<pool>] ...\n"));
236 return (gettext("\timport [-d dir] [-D]\n"
237 "\timport [-o mntopts] [-o property=value] ... \n"
238 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
239 "[-R root] [-F [-n]] -a\n"
240 "\timport [-o mntopts] [-o property=value] ... \n"
241 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
242 "[-R root] [-F [-n]]\n"
243 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
245 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
247 case HELP_LABELCLEAR:
248 return (gettext("\tlabelclear [-f] <vdev>\n"));
250 return (gettext("\tlist [-Hpv] [-o property[,...]] "
251 "[-T d|u] [pool] ... [interval [count]]\n"));
253 return (gettext("\toffline [-t] <pool> <device> ...\n"));
255 return (gettext("\tonline [-e] <pool> <device> ...\n"));
257 return (gettext("\treplace [-f] <pool> <device> "
260 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
262 return (gettext("\treopen <pool>\n"));
264 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
266 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
269 return (gettext("\tupgrade [-v]\n"
270 "\tupgrade [-V version] <-a | pool ...>\n"));
272 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
273 "<\"all\" | property[,...]> <pool> ...\n"));
275 return (gettext("\tset <property=value> <pool> \n"));
277 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
278 "\t [-o property=value] <pool> <newpool> "
279 "[<device> ...]\n"));
281 return (gettext("\treguid <pool>\n"));
290 * Callback routine that will print out a pool property value.
293 print_prop_cb(int prop, void *cb)
297 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
299 if (zpool_prop_readonly(prop))
300 (void) fprintf(fp, " NO ");
302 (void) fprintf(fp, " YES ");
304 if (zpool_prop_values(prop) == NULL)
305 (void) fprintf(fp, "-\n");
307 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
313 * Display usage message. If we're inside a command, display only the usage for
314 * that command. Otherwise, iterate over the entire command table and display
315 * a complete usage message.
318 usage(boolean_t requested)
320 FILE *fp = requested ? stdout : stderr;
322 if (current_command == NULL) {
325 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
327 gettext("where 'command' is one of the following:\n\n"));
329 for (i = 0; i < NCOMMAND; i++) {
330 if (command_table[i].name == NULL)
331 (void) fprintf(fp, "\n");
333 (void) fprintf(fp, "%s",
334 get_usage(command_table[i].usage));
337 (void) fprintf(fp, gettext("usage:\n"));
338 (void) fprintf(fp, "%s", get_usage(current_command->usage));
341 if (current_command != NULL &&
342 ((strcmp(current_command->name, "set") == 0) ||
343 (strcmp(current_command->name, "get") == 0) ||
344 (strcmp(current_command->name, "list") == 0))) {
347 gettext("\nthe following properties are supported:\n"));
349 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
350 "PROPERTY", "EDIT", "VALUES");
352 /* Iterate over all properties */
353 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
356 (void) fprintf(fp, "\t%-15s ", "feature@...");
357 (void) fprintf(fp, "YES disabled | enabled | active\n");
359 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
360 "appended with a feature name.\nSee zpool-features(7).\n"));
364 * See comments at end of main().
366 if (getenv("ZFS_ABORT") != NULL) {
367 (void) printf("dumping core by request\n");
371 exit(requested ? 0 : 2);
375 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
376 boolean_t print_logs)
383 (void) printf("\t%*s%s\n", indent, "", name);
385 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
386 &child, &children) != 0)
389 for (c = 0; c < children; c++) {
390 uint64_t is_log = B_FALSE;
392 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
394 if ((is_log && !print_logs) || (!is_log && print_logs))
397 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
398 print_vdev_tree(zhp, vname, child[c], indent + 2,
405 prop_list_contains_feature(nvlist_t *proplist)
408 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
409 nvp = nvlist_next_nvpair(proplist, nvp)) {
410 if (zpool_prop_feature(nvpair_name(nvp)))
417 * Add a property pair (name, string-value) into a property nvlist.
420 add_prop_list(const char *propname, char *propval, nvlist_t **props,
423 zpool_prop_t prop = ZPROP_INVAL;
429 if (*props == NULL &&
430 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
431 (void) fprintf(stderr,
432 gettext("internal error: out of memory\n"));
439 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
441 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
442 !zpool_prop_feature(propname)) {
443 (void) fprintf(stderr, gettext("property '%s' is "
444 "not a valid pool property\n"), propname);
449 * feature@ properties and version should not be specified
452 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
453 nvlist_exists(proplist, vname)) ||
454 (prop == ZPOOL_PROP_VERSION &&
455 prop_list_contains_feature(proplist))) {
456 (void) fprintf(stderr, gettext("'feature@' and "
457 "'version' properties cannot be specified "
463 if (zpool_prop_feature(propname))
466 normnm = zpool_prop_to_name(prop);
468 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
469 normnm = zfs_prop_to_name(fprop);
475 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
476 prop != ZPOOL_PROP_CACHEFILE) {
477 (void) fprintf(stderr, gettext("property '%s' "
478 "specified multiple times\n"), propname);
482 if (nvlist_add_string(proplist, normnm, propval) != 0) {
483 (void) fprintf(stderr, gettext("internal "
484 "error: out of memory\n"));
492 * zpool add [-fn] <pool> <vdev> ...
494 * -f Force addition of devices, even if they appear in use
495 * -n Do not add the devices, but display the resulting layout if
496 * they were to be added.
498 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
499 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
503 zpool_do_add(int argc, char **argv)
505 boolean_t force = B_FALSE;
506 boolean_t dryrun = B_FALSE;
510 zpool_boot_label_t boot_type;
517 while ((c = getopt(argc, argv, "fn")) != -1) {
526 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
535 /* get pool name and check number of arguments */
537 (void) fprintf(stderr, gettext("missing pool name argument\n"));
541 (void) fprintf(stderr, gettext("missing vdev specification\n"));
550 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
553 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
554 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
560 if (zpool_is_bootable(zhp))
561 boot_type = ZPOOL_COPY_BOOT_LABEL;
563 boot_type = ZPOOL_NO_BOOT_LABEL;
565 /* pass off to get_vdev_spec for processing */
566 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
567 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
568 boot_type, boot_size, argc, argv);
569 if (nvroot == NULL) {
575 nvlist_t *poolnvroot;
577 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
580 (void) printf(gettext("would update '%s' to the following "
581 "configuration:\n"), zpool_get_name(zhp));
583 /* print original main pool and new tree */
584 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
585 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
587 /* Do the same for the logs */
588 if (num_logs(poolnvroot) > 0) {
589 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
590 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
591 } else if (num_logs(nvroot) > 0) {
592 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
597 ret = (zpool_add(zhp, nvroot) != 0);
607 * zpool remove <pool> <vdev> ...
609 * Removes the given vdev from the pool.
612 zpool_do_remove(int argc, char **argv)
617 boolean_t stop = B_FALSE;
618 boolean_t noop = B_FALSE;
619 boolean_t parsable = B_FALSE;
623 while ((c = getopt(argc, argv, "nps")) != -1) {
635 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
644 /* get pool name and check number of arguments */
646 (void) fprintf(stderr, gettext("missing pool name argument\n"));
652 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
656 (void) fprintf(stderr, gettext("stop request ignored\n"));
662 (void) fprintf(stderr, gettext("too many arguments\n"));
665 if (zpool_vdev_remove_cancel(zhp) != 0)
669 (void) fprintf(stderr, gettext("missing device\n"));
673 for (i = 1; i < argc; i++) {
677 if (zpool_vdev_indirect_size(zhp, argv[i],
683 (void) printf("%s %llu\n",
687 zfs_nicenum(size, valstr,
689 (void) printf("Memory that will be "
690 "used after removing %s: %s\n",
694 if (zpool_vdev_remove(zhp, argv[i]) != 0)
704 * zpool labelclear [-f] <vdev>
706 * -f Force clearing the label for the vdevs which are members of
707 * the exported or foreign pools.
709 * Verifies that the vdev is not active and zeros out the label information
713 zpool_do_labelclear(int argc, char **argv)
715 char vdev[MAXPATHLEN];
721 boolean_t inuse = B_FALSE;
722 boolean_t force = B_FALSE;
725 while ((c = getopt(argc, argv, "f")) != -1) {
731 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
742 (void) fprintf(stderr, gettext("missing vdev name\n"));
746 (void) fprintf(stderr, gettext("too many arguments\n"));
751 * Check if we were given absolute path and use it as is.
752 * Otherwise if the provided vdev name doesn't point to a file,
753 * try prepending dsk path and appending s0.
755 (void) strlcpy(vdev, argv[0], sizeof (vdev));
756 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
759 (void) snprintf(vdev, sizeof (vdev), "%s/%s",
761 ZFS_DISK_ROOT, argv[0]);
762 if ((s = strrchr(argv[0], 's')) == NULL ||
764 (void) strlcat(vdev, "s0", sizeof (vdev));
768 if (stat(vdev, &st) != 0) {
769 (void) fprintf(stderr, gettext(
770 "failed to find device %s, try specifying absolute "
771 "path instead\n"), argv[0]);
776 if ((fd = open(vdev, O_RDWR)) < 0) {
777 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
778 vdev, strerror(errno));
782 if (zpool_read_label(fd, &config) != 0) {
783 (void) fprintf(stderr,
784 gettext("failed to read label from %s\n"), vdev);
789 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
791 (void) fprintf(stderr,
792 gettext("failed to check state for %s\n"), vdev);
801 case POOL_STATE_ACTIVE:
802 case POOL_STATE_SPARE:
803 case POOL_STATE_L2CACHE:
804 (void) fprintf(stderr, gettext(
805 "%s is a member (%s) of pool \"%s\"\n"),
806 vdev, zpool_pool_state_to_name(state), name);
810 case POOL_STATE_EXPORTED:
813 (void) fprintf(stderr, gettext(
814 "use '-f' to override the following error:\n"
815 "%s is a member of exported pool \"%s\"\n"),
820 case POOL_STATE_POTENTIALLY_ACTIVE:
823 (void) fprintf(stderr, gettext(
824 "use '-f' to override the following error:\n"
825 "%s is a member of potentially active pool \"%s\"\n"),
830 case POOL_STATE_DESTROYED:
831 /* inuse should never be set for a destroyed pool */
837 ret = zpool_clear_label(fd);
839 (void) fprintf(stderr,
840 gettext("failed to clear label for %s\n"), vdev);
851 * zpool create [-fnd] [-B] [-o property=value] ...
852 * [-O file-system-property=value] ...
853 * [-R root] [-m mountpoint] <pool> <dev> ...
855 * -B Create boot partition.
856 * -f Force creation, even if devices appear in use
857 * -n Do not create the pool, but display the resulting layout if it
858 * were to be created.
859 * -R Create a pool under an alternate root
860 * -m Set default mountpoint for the root dataset. By default it's
862 * -o Set property=value.
863 * -d Don't automatically enable all supported pool features
864 * (individual features can be enabled with -o).
865 * -O Set fsproperty=value in the pool's root file system
867 * Creates the named pool according to the given vdev specification. The
868 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
869 * we get the nvlist back from get_vdev_spec(), we either print out the contents
870 * (if '-n' was specified), or pass it to libzfs to do the creation.
873 #define SYSTEM256 (256 * 1024 * 1024)
875 zpool_do_create(int argc, char **argv)
877 boolean_t force = B_FALSE;
878 boolean_t dryrun = B_FALSE;
879 boolean_t enable_all_pool_feat = B_TRUE;
880 zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
881 uint64_t boot_size = 0;
883 nvlist_t *nvroot = NULL;
886 char *altroot = NULL;
887 char *mountpoint = NULL;
888 nvlist_t *fsprops = NULL;
889 nvlist_t *props = NULL;
893 while ((c = getopt(argc, argv, ":fndBR:m:o:O:")) != -1) {
902 enable_all_pool_feat = B_FALSE;
907 * We should create the system partition.
908 * Also make sure the size is set.
910 boot_type = ZPOOL_CREATE_BOOT_LABEL;
912 boot_size = SYSTEM256;
915 (void) fprintf(stderr,
916 gettext("option '%c' is not supported\n"),
922 if (add_prop_list(zpool_prop_to_name(
923 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
925 if (nvlist_lookup_string(props,
926 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
929 if (add_prop_list(zpool_prop_to_name(
930 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
934 /* Equivalent to -O mountpoint=optarg */
938 if ((propval = strchr(optarg, '=')) == NULL) {
939 (void) fprintf(stderr, gettext("missing "
940 "'=' for -o option\n"));
946 if (add_prop_list(optarg, propval, &props, B_TRUE))
950 * Get bootsize value for make_root_vdev().
952 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_BOOTSIZE) {
953 if (zfs_nicestrtonum(g_zfs, propval,
954 &boot_size) < 0 || boot_size == 0) {
955 (void) fprintf(stderr,
956 gettext("bad boot partition size "
957 "'%s': %s\n"), propval,
958 libzfs_error_description(g_zfs));
964 * If the user is creating a pool that doesn't support
965 * feature flags, don't enable any features.
967 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
971 ver = strtoull(propval, &end, 10);
973 ver < SPA_VERSION_FEATURES) {
974 enable_all_pool_feat = B_FALSE;
977 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
981 if ((propval = strchr(optarg, '=')) == NULL) {
982 (void) fprintf(stderr, gettext("missing "
983 "'=' for -O option\n"));
990 * Mountpoints are checked and then added later.
991 * Uniquely among properties, they can be specified
992 * more than once, to avoid conflict with -m.
994 if (0 == strcmp(optarg,
995 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
996 mountpoint = propval;
997 } else if (add_prop_list(optarg, propval, &fsprops,
1003 (void) fprintf(stderr, gettext("missing argument for "
1004 "'%c' option\n"), optopt);
1007 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1016 /* get pool name and check number of arguments */
1018 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1022 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1029 * As a special case, check for use of '/' in the name, and direct the
1030 * user to use 'zfs create' instead.
1032 if (strchr(poolname, '/') != NULL) {
1033 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1034 "character '/' in pool name\n"), poolname);
1035 (void) fprintf(stderr, gettext("use 'zfs create' to "
1036 "create a dataset\n"));
1041 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
1042 * and not set otherwise.
1044 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
1045 const char *propname;
1046 char *strptr, *buf = NULL;
1049 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
1050 if (nvlist_lookup_string(props, propname, &strptr) != 0) {
1051 (void) asprintf(&buf, "%" PRIu64, boot_size);
1053 (void) fprintf(stderr,
1054 gettext("internal error: out of memory\n"));
1057 rv = add_prop_list(propname, buf, &props, B_TRUE);
1063 const char *propname;
1066 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
1067 if (nvlist_lookup_string(props, propname, &strptr) == 0) {
1068 (void) fprintf(stderr, gettext("error: setting boot "
1069 "partition size requires option '-B'\n"));
1074 /* pass off to get_vdev_spec for bulk processing */
1075 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
1076 boot_type, boot_size, argc - 1, argv + 1);
1080 /* make_root_vdev() allows 0 toplevel children if there are spares */
1081 if (!zfs_allocatable_devs(nvroot)) {
1082 (void) fprintf(stderr, gettext("invalid vdev "
1083 "specification: at least one toplevel vdev must be "
1088 if (altroot != NULL && altroot[0] != '/') {
1089 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1090 "must be an absolute path\n"), altroot);
1095 * Check the validity of the mountpoint and direct the user to use the
1096 * '-m' mountpoint option if it looks like its in use.
1097 * Ignore the checks if the '-f' option is given.
1099 if (!force && (mountpoint == NULL ||
1100 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1101 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
1102 char buf[MAXPATHLEN];
1105 if (mountpoint && mountpoint[0] != '/') {
1106 (void) fprintf(stderr, gettext("invalid mountpoint "
1107 "'%s': must be an absolute path, 'legacy', or "
1108 "'none'\n"), mountpoint);
1112 if (mountpoint == NULL) {
1113 if (altroot != NULL)
1114 (void) snprintf(buf, sizeof (buf), "%s/%s",
1117 (void) snprintf(buf, sizeof (buf), "/%s",
1120 if (altroot != NULL)
1121 (void) snprintf(buf, sizeof (buf), "%s%s",
1122 altroot, mountpoint);
1124 (void) snprintf(buf, sizeof (buf), "%s",
1128 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1129 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1130 "%s\n"), buf, strerror(errno));
1131 (void) fprintf(stderr, gettext("use '-m' "
1132 "option to provide a different default\n"));
1137 while (count < 3 && readdir(dirp) != NULL)
1139 (void) closedir(dirp);
1142 (void) fprintf(stderr, gettext("mountpoint "
1143 "'%s' exists and is not empty\n"), buf);
1144 (void) fprintf(stderr, gettext("use '-m' "
1145 "option to provide a "
1146 "different default\n"));
1153 * Now that the mountpoint's validity has been checked, ensure that
1154 * the property is set appropriately prior to creating the pool.
1156 if (mountpoint != NULL) {
1157 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1158 mountpoint, &fsprops, B_FALSE);
1166 * For a dry run invocation, print out a basic message and run
1167 * through all the vdevs in the list and print out in an
1168 * appropriate hierarchy.
1170 (void) printf(gettext("would create '%s' with the "
1171 "following layout:\n\n"), poolname);
1173 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1174 if (num_logs(nvroot) > 0)
1175 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1180 * Hand off to libzfs.
1182 if (enable_all_pool_feat) {
1184 for (i = 0; i < SPA_FEATURES; i++) {
1185 char propname[MAXPATHLEN];
1186 zfeature_info_t *feat = &spa_feature_table[i];
1188 (void) snprintf(propname, sizeof (propname),
1189 "feature@%s", feat->fi_uname);
1192 * Skip feature if user specified it manually
1193 * on the command line.
1195 if (nvlist_exists(props, propname))
1198 ret = add_prop_list(propname,
1199 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1206 if (zpool_create(g_zfs, poolname,
1207 nvroot, props, fsprops) == 0) {
1208 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1209 ZFS_TYPE_FILESYSTEM);
1211 if (zfs_mount(pool, NULL, 0) == 0)
1212 ret = zfs_shareall(pool);
1215 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1216 (void) fprintf(stderr, gettext("pool name may have "
1222 nvlist_free(nvroot);
1223 nvlist_free(fsprops);
1227 nvlist_free(fsprops);
1234 * zpool destroy <pool>
1236 * -f Forcefully unmount any datasets
1238 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1241 zpool_do_destroy(int argc, char **argv)
1243 boolean_t force = B_FALSE;
1246 zpool_handle_t *zhp;
1250 while ((c = getopt(argc, argv, "f")) != -1) {
1256 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1265 /* check arguments */
1267 (void) fprintf(stderr, gettext("missing pool argument\n"));
1271 (void) fprintf(stderr, gettext("too many arguments\n"));
1277 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1279 * As a special case, check for use of '/' in the name, and
1280 * direct the user to use 'zfs destroy' instead.
1282 if (strchr(pool, '/') != NULL)
1283 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1284 "destroy a dataset\n"));
1288 if (zpool_disable_datasets(zhp, force) != 0) {
1289 (void) fprintf(stderr, gettext("could not destroy '%s': "
1290 "could not unmount datasets\n"), zpool_get_name(zhp));
1294 /* The history must be logged as part of the export */
1295 log_history = B_FALSE;
1297 ret = (zpool_destroy(zhp, history_str) != 0);
1305 * zpool export [-f] <pool> ...
1307 * -f Forcefully unmount datasets
1309 * Export the given pools. By default, the command will attempt to cleanly
1310 * unmount any active datasets within the pool. If the '-f' flag is specified,
1311 * then the datasets will be forcefully unmounted.
1314 zpool_do_export(int argc, char **argv)
1316 boolean_t force = B_FALSE;
1317 boolean_t hardforce = B_FALSE;
1319 zpool_handle_t *zhp;
1324 while ((c = getopt(argc, argv, "fF")) != -1) {
1333 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1342 /* check arguments */
1344 (void) fprintf(stderr, gettext("missing pool argument\n"));
1349 for (i = 0; i < argc; i++) {
1350 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1355 if (zpool_disable_datasets(zhp, force) != 0) {
1361 /* The history must be logged as part of the export */
1362 log_history = B_FALSE;
1365 if (zpool_export_force(zhp, history_str) != 0)
1367 } else if (zpool_export(zhp, force, history_str) != 0) {
1378 * Given a vdev configuration, determine the maximum width needed for the device
1382 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1384 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1389 if (strlen(name) + depth > max)
1390 max = strlen(name) + depth;
1394 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1395 &child, &children) == 0) {
1396 for (c = 0; c < children; c++)
1397 if ((ret = max_width(zhp, child[c], depth + 2,
1402 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1403 &child, &children) == 0) {
1404 for (c = 0; c < children; c++)
1405 if ((ret = max_width(zhp, child[c], depth + 2,
1410 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1411 &child, &children) == 0) {
1412 for (c = 0; c < children; c++)
1413 if ((ret = max_width(zhp, child[c], depth + 2,
1422 typedef struct spare_cbdata {
1424 zpool_handle_t *cb_zhp;
1428 find_vdev(nvlist_t *nv, uint64_t search)
1434 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1438 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1439 &child, &children) == 0) {
1440 for (c = 0; c < children; c++)
1441 if (find_vdev(child[c], search))
1449 find_spare(zpool_handle_t *zhp, void *data)
1451 spare_cbdata_t *cbp = data;
1452 nvlist_t *config, *nvroot;
1454 config = zpool_get_config(zhp, NULL);
1455 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1458 if (find_vdev(nvroot, cbp->cb_guid)) {
1468 * Print out configuration state as requested by status_callback.
1471 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1472 int namewidth, int depth, boolean_t isspare)
1475 uint_t c, vsc, children;
1476 pool_scan_stat_t *ps = NULL;
1478 char rbuf[6], wbuf[6], cbuf[6];
1480 uint64_t notpresent;
1486 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1487 &child, &children) != 0)
1490 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1491 (uint64_t **)&vs, &vsc) == 0);
1493 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1495 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
1498 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1501 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1504 if (vs->vs_aux == VDEV_AUX_SPARED)
1506 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1510 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1514 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1515 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1516 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1517 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1520 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1521 ¬present) == 0 ||
1522 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1524 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1525 (void) printf(" was %s", path);
1526 } else if (vs->vs_aux != 0) {
1529 switch (vs->vs_aux) {
1530 case VDEV_AUX_OPEN_FAILED:
1531 (void) printf(gettext("cannot open"));
1534 case VDEV_AUX_BAD_GUID_SUM:
1535 (void) printf(gettext("missing device"));
1538 case VDEV_AUX_NO_REPLICAS:
1539 (void) printf(gettext("insufficient replicas"));
1542 case VDEV_AUX_VERSION_NEWER:
1543 (void) printf(gettext("newer version"));
1546 case VDEV_AUX_UNSUP_FEAT:
1547 (void) printf(gettext("unsupported feature(s)"));
1550 case VDEV_AUX_ASHIFT_TOO_BIG:
1551 (void) printf(gettext("unsupported minimum blocksize"));
1554 case VDEV_AUX_SPARED:
1555 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1557 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1558 if (strcmp(zpool_get_name(cb.cb_zhp),
1559 zpool_get_name(zhp)) == 0)
1560 (void) printf(gettext("currently in "
1563 (void) printf(gettext("in use by "
1565 zpool_get_name(cb.cb_zhp));
1566 zpool_close(cb.cb_zhp);
1568 (void) printf(gettext("currently in use"));
1572 case VDEV_AUX_ERR_EXCEEDED:
1573 (void) printf(gettext("too many errors"));
1576 case VDEV_AUX_IO_FAILURE:
1577 (void) printf(gettext("experienced I/O failures"));
1580 case VDEV_AUX_BAD_LOG:
1581 (void) printf(gettext("bad intent log"));
1584 case VDEV_AUX_EXTERNAL:
1585 (void) printf(gettext("external device fault"));
1588 case VDEV_AUX_SPLIT_POOL:
1589 (void) printf(gettext("split into new pool"));
1592 case VDEV_AUX_CHILDREN_OFFLINE:
1593 (void) printf(gettext("all children offline"));
1597 (void) printf(gettext("corrupted data"));
1600 } else if (children == 0 && !isspare &&
1601 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1602 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1604 gettext(" block size: %dB configured, %dB native"),
1605 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1608 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1609 (uint64_t **)&ps, &c);
1611 if (ps && ps->pss_state == DSS_SCANNING &&
1612 vs->vs_scan_processed != 0 && children == 0) {
1613 (void) printf(gettext(" (%s)"),
1614 (ps->pss_func == POOL_SCAN_RESILVER) ?
1615 "resilvering" : "repairing");
1618 (void) printf("\n");
1620 for (c = 0; c < children; c++) {
1621 uint64_t islog = B_FALSE, ishole = B_FALSE;
1623 /* Don't print logs or holes here */
1624 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1626 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1628 if (islog || ishole)
1630 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1631 print_status_config(zhp, vname, child[c],
1632 namewidth, depth + 2, isspare);
1639 * Print the configuration of an exported pool. Iterate over all vdevs in the
1640 * pool, printing out the name and status for each one.
1643 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1650 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1651 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1652 strcmp(type, VDEV_TYPE_HOLE) == 0)
1655 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1656 (uint64_t **)&vs, &c) == 0);
1658 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1659 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1661 if (vs->vs_aux != 0) {
1664 switch (vs->vs_aux) {
1665 case VDEV_AUX_OPEN_FAILED:
1666 (void) printf(gettext("cannot open"));
1669 case VDEV_AUX_BAD_GUID_SUM:
1670 (void) printf(gettext("missing device"));
1673 case VDEV_AUX_NO_REPLICAS:
1674 (void) printf(gettext("insufficient replicas"));
1677 case VDEV_AUX_VERSION_NEWER:
1678 (void) printf(gettext("newer version"));
1681 case VDEV_AUX_UNSUP_FEAT:
1682 (void) printf(gettext("unsupported feature(s)"));
1685 case VDEV_AUX_ERR_EXCEEDED:
1686 (void) printf(gettext("too many errors"));
1689 case VDEV_AUX_CHILDREN_OFFLINE:
1690 (void) printf(gettext("all children offline"));
1694 (void) printf(gettext("corrupted data"));
1698 (void) printf("\n");
1700 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1701 &child, &children) != 0)
1704 for (c = 0; c < children; c++) {
1705 uint64_t is_log = B_FALSE;
1707 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1712 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1713 print_import_config(vname, child[c], namewidth, depth + 2);
1717 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1718 &child, &children) == 0) {
1719 (void) printf(gettext("\tcache\n"));
1720 for (c = 0; c < children; c++) {
1721 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1722 (void) printf("\t %s\n", vname);
1727 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1728 &child, &children) == 0) {
1729 (void) printf(gettext("\tspares\n"));
1730 for (c = 0; c < children; c++) {
1731 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1732 (void) printf("\t %s\n", vname);
1740 * Logs are recorded as top level vdevs in the main pool child array
1741 * but with "is_log" set to 1. We use either print_status_config() or
1742 * print_import_config() to print the top level logs then any log
1743 * children (eg mirrored slogs) are printed recursively - which
1744 * works because only the top level vdev is marked "is_log"
1747 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1752 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1756 (void) printf(gettext("\tlogs\n"));
1758 for (c = 0; c < children; c++) {
1759 uint64_t is_log = B_FALSE;
1762 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1766 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1768 print_status_config(zhp, name, child[c], namewidth,
1771 print_import_config(name, child[c], namewidth, 2);
1777 * Display the status for the given pool.
1780 show_import(nvlist_t *config)
1782 uint64_t pool_state;
1794 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1796 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1798 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1800 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1803 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1804 (uint64_t **)&vs, &vsc) == 0);
1805 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1807 reason = zpool_import_status(config, &msgid);
1809 (void) printf(gettext(" pool: %s\n"), name);
1810 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1811 (void) printf(gettext(" state: %s"), health);
1812 if (pool_state == POOL_STATE_DESTROYED)
1813 (void) printf(gettext(" (DESTROYED)"));
1814 (void) printf("\n");
1817 case ZPOOL_STATUS_MISSING_DEV_R:
1818 case ZPOOL_STATUS_MISSING_DEV_NR:
1819 case ZPOOL_STATUS_BAD_GUID_SUM:
1820 (void) printf(gettext(" status: One or more devices are "
1821 "missing from the system.\n"));
1824 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1825 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1826 (void) printf(gettext(" status: One or more devices contains "
1827 "corrupted data.\n"));
1830 case ZPOOL_STATUS_CORRUPT_DATA:
1832 gettext(" status: The pool data is corrupted.\n"));
1835 case ZPOOL_STATUS_OFFLINE_DEV:
1836 (void) printf(gettext(" status: One or more devices "
1837 "are offlined.\n"));
1840 case ZPOOL_STATUS_CORRUPT_POOL:
1841 (void) printf(gettext(" status: The pool metadata is "
1845 case ZPOOL_STATUS_VERSION_OLDER:
1846 (void) printf(gettext(" status: The pool is formatted using a "
1847 "legacy on-disk version.\n"));
1850 case ZPOOL_STATUS_VERSION_NEWER:
1851 (void) printf(gettext(" status: The pool is formatted using an "
1852 "incompatible version.\n"));
1855 case ZPOOL_STATUS_FEAT_DISABLED:
1856 (void) printf(gettext(" status: Some supported features are "
1857 "not enabled on the pool.\n"));
1860 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1861 (void) printf(gettext("status: The pool uses the following "
1862 "feature(s) not supported on this system:\n"));
1863 zpool_print_unsup_feat(config);
1866 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1867 (void) printf(gettext("status: The pool can only be accessed "
1868 "in read-only mode on this system. It\n\tcannot be "
1869 "accessed in read-write mode because it uses the "
1870 "following\n\tfeature(s) not supported on this system:\n"));
1871 zpool_print_unsup_feat(config);
1874 case ZPOOL_STATUS_HOSTID_MISMATCH:
1875 (void) printf(gettext(" status: The pool was last accessed by "
1876 "another system.\n"));
1879 case ZPOOL_STATUS_FAULTED_DEV_R:
1880 case ZPOOL_STATUS_FAULTED_DEV_NR:
1881 (void) printf(gettext(" status: One or more devices are "
1885 case ZPOOL_STATUS_BAD_LOG:
1886 (void) printf(gettext(" status: An intent log record cannot be "
1890 case ZPOOL_STATUS_RESILVERING:
1891 (void) printf(gettext(" status: One or more devices were being "
1895 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
1896 (void) printf(gettext("status: One or more devices were "
1897 "configured to use a non-native block size.\n"
1898 "\tExpect reduced performance.\n"));
1903 * No other status can be seen when importing pools.
1905 assert(reason == ZPOOL_STATUS_OK);
1909 * Print out an action according to the overall state of the pool.
1911 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1912 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1913 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1914 (void) printf(gettext(" action: The pool can be "
1915 "imported using its name or numeric identifier, "
1916 "though\n\tsome features will not be available "
1917 "without an explicit 'zpool upgrade'.\n"));
1918 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1919 (void) printf(gettext(" action: The pool can be "
1920 "imported using its name or numeric "
1921 "identifier and\n\tthe '-f' flag.\n"));
1923 (void) printf(gettext(" action: The pool can be "
1924 "imported using its name or numeric "
1927 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1928 (void) printf(gettext(" action: The pool can be imported "
1929 "despite missing or damaged devices. The\n\tfault "
1930 "tolerance of the pool may be compromised if imported.\n"));
1933 case ZPOOL_STATUS_VERSION_NEWER:
1934 (void) printf(gettext(" action: The pool cannot be "
1935 "imported. Access the pool on a system running "
1936 "newer\n\tsoftware, or recreate the pool from "
1939 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1940 (void) printf(gettext("action: The pool cannot be "
1941 "imported. Access the pool on a system that "
1942 "supports\n\tthe required feature(s), or recreate "
1943 "the pool from backup.\n"));
1945 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1946 (void) printf(gettext("action: The pool cannot be "
1947 "imported in read-write mode. Import the pool "
1949 "\t\"-o readonly=on\", access the pool on a system "
1950 "that supports the\n\trequired feature(s), or "
1951 "recreate the pool from backup.\n"));
1953 case ZPOOL_STATUS_MISSING_DEV_R:
1954 case ZPOOL_STATUS_MISSING_DEV_NR:
1955 case ZPOOL_STATUS_BAD_GUID_SUM:
1956 (void) printf(gettext(" action: The pool cannot be "
1957 "imported. Attach the missing\n\tdevices and try "
1961 (void) printf(gettext(" action: The pool cannot be "
1962 "imported due to damaged devices or data.\n"));
1966 /* Print the comment attached to the pool. */
1967 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1968 (void) printf(gettext("comment: %s\n"), comment);
1971 * If the state is "closed" or "can't open", and the aux state
1972 * is "corrupt data":
1974 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1975 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1976 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1977 if (pool_state == POOL_STATE_DESTROYED)
1978 (void) printf(gettext("\tThe pool was destroyed, "
1979 "but can be imported using the '-Df' flags.\n"));
1980 else if (pool_state != POOL_STATE_EXPORTED)
1981 (void) printf(gettext("\tThe pool may be active on "
1982 "another system, but can be imported using\n\t"
1983 "the '-f' flag.\n"));
1987 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1990 (void) printf(gettext(" config:\n\n"));
1992 namewidth = max_width(NULL, nvroot, 0, 0);
1996 print_import_config(name, nvroot, namewidth, 0);
1997 if (num_logs(nvroot) > 0)
1998 print_logs(NULL, nvroot, namewidth, B_FALSE);
2000 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2001 (void) printf(gettext("\n\tAdditional devices are known to "
2002 "be part of this pool, though their\n\texact "
2003 "configuration cannot be determined.\n"));
2008 * Perform the import for the given configuration. This passes the heavy
2009 * lifting off to zpool_import_props(), and then mounts the datasets contained
2013 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2014 nvlist_t *props, int flags)
2016 zpool_handle_t *zhp;
2021 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2024 verify(nvlist_lookup_uint64(config,
2025 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
2026 verify(nvlist_lookup_uint64(config,
2027 ZPOOL_CONFIG_VERSION, &version) == 0);
2028 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2029 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2030 "is formatted using an unsupported ZFS version\n"), name);
2032 } else if (state != POOL_STATE_EXPORTED &&
2033 !(flags & ZFS_IMPORT_ANY_HOST)) {
2036 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
2038 if ((unsigned long)hostid != gethostid()) {
2043 verify(nvlist_lookup_string(config,
2044 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2045 verify(nvlist_lookup_uint64(config,
2046 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
2048 (void) fprintf(stderr, gettext("cannot import "
2049 "'%s': pool may be in use from other "
2050 "system, it was last accessed by %s "
2051 "(hostid: 0x%lx) on %s"), name, hostname,
2052 (unsigned long)hostid,
2053 asctime(localtime(&t)));
2054 (void) fprintf(stderr, gettext("use '-f' to "
2055 "import anyway\n"));
2059 (void) fprintf(stderr, gettext("cannot import '%s': "
2060 "pool may be in use from other system\n"), name);
2061 (void) fprintf(stderr, gettext("use '-f' to import "
2067 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2070 if (newname != NULL)
2071 name = (char *)newname;
2073 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2076 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2077 !(flags & ZFS_IMPORT_ONLY) &&
2078 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2088 * zpool checkpoint <pool>
2089 * checkpoint --discard <pool>
2091 * -d Discard the checkpoint from a checkpointed
2094 * Checkpoints the specified pool, by taking a "snapshot" of its
2095 * current state. A pool can only have one checkpoint at a time.
2098 zpool_do_checkpoint(int argc, char **argv)
2102 zpool_handle_t *zhp;
2105 struct option long_options[] = {
2106 {"discard", no_argument, NULL, 'd'},
2111 while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
2117 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2127 (void) fprintf(stderr, gettext("missing pool argument\n"));
2132 (void) fprintf(stderr, gettext("too many arguments\n"));
2138 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
2139 /* As a special case, check for use of '/' in the name */
2140 if (strchr(pool, '/') != NULL)
2141 (void) fprintf(stderr, gettext("'zpool checkpoint' "
2142 "doesn't work on datasets. To save the state "
2143 "of a dataset from a specific point in time "
2144 "please use 'zfs snapshot'\n"));
2149 err = (zpool_discard_checkpoint(zhp) != 0);
2151 err = (zpool_checkpoint(zhp) != 0);
2158 #define CHECKPOINT_OPT 1024
2161 * zpool import [-d dir] [-D]
2162 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2163 * [-d dir | -c cachefile] [-f] -a
2164 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2165 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2167 * -c Read pool information from a cachefile instead of searching
2170 * -d Scan in a specific directory, other than /dev/dsk. More than
2171 * one directory can be specified using multiple '-d' options.
2173 * -D Scan for previously destroyed pools or import all or only
2174 * specified destroyed pools.
2176 * -R Temporarily import the pool, with all mountpoints relative to
2177 * the given root. The pool will remain exported when the machine
2180 * -V Import even in the presence of faulted vdevs. This is an
2181 * intentionally undocumented option for testing purposes, and
2182 * treats the pool configuration as complete, leaving any bad
2183 * vdevs in the FAULTED state. In other words, it does verbatim
2186 * -f Force import, even if it appears that the pool is active.
2188 * -F Attempt rewind if necessary.
2190 * -n See if rewind would work, but don't actually rewind.
2192 * -N Import the pool but don't mount datasets.
2194 * -T Specify a starting txg to use for import. This option is
2195 * intentionally undocumented option for testing purposes.
2197 * -a Import all pools found.
2199 * -o Set property=value and/or temporary mount options (without '=').
2201 * --rewind-to-checkpoint
2202 * Import the pool and revert back to the checkpoint.
2204 * The import command scans for pools to import, and import pools based on pool
2205 * name and GUID. The pool can also be renamed as part of the import process.
2208 zpool_do_import(int argc, char **argv)
2210 char **searchdirs = NULL;
2214 nvlist_t *pools = NULL;
2215 boolean_t do_all = B_FALSE;
2216 boolean_t do_destroyed = B_FALSE;
2217 char *mntopts = NULL;
2220 uint64_t searchguid = 0;
2221 char *searchname = NULL;
2223 nvlist_t *found_config;
2224 nvlist_t *policy = NULL;
2225 nvlist_t *props = NULL;
2227 int flags = ZFS_IMPORT_NORMAL;
2228 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2229 boolean_t dryrun = B_FALSE;
2230 boolean_t do_rewind = B_FALSE;
2231 boolean_t xtreme_rewind = B_FALSE;
2232 uint64_t pool_state, txg = -1ULL;
2233 char *cachefile = NULL;
2234 importargs_t idata = { 0 };
2238 struct option long_options[] = {
2239 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
2244 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX",
2245 long_options, NULL)) != -1) {
2254 if (searchdirs == NULL) {
2255 searchdirs = safe_malloc(sizeof (char *));
2257 char **tmp = safe_malloc((nsearch + 1) *
2259 bcopy(searchdirs, tmp, nsearch *
2264 searchdirs[nsearch++] = optarg;
2267 do_destroyed = B_TRUE;
2270 flags |= ZFS_IMPORT_ANY_HOST;
2276 flags |= ZFS_IMPORT_MISSING_LOG;
2282 flags |= ZFS_IMPORT_ONLY;
2285 if ((propval = strchr(optarg, '=')) != NULL) {
2288 if (add_prop_list(optarg, propval,
2296 if (add_prop_list(zpool_prop_to_name(
2297 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2299 if (nvlist_lookup_string(props,
2300 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2303 if (add_prop_list(zpool_prop_to_name(
2304 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2309 txg = strtoull(optarg, &endptr, 0);
2310 if (errno != 0 || *endptr != '\0') {
2311 (void) fprintf(stderr,
2312 gettext("invalid txg value\n"));
2315 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2318 flags |= ZFS_IMPORT_VERBATIM;
2321 xtreme_rewind = B_TRUE;
2323 case CHECKPOINT_OPT:
2324 flags |= ZFS_IMPORT_CHECKPOINT;
2327 (void) fprintf(stderr, gettext("missing argument for "
2328 "'%c' option\n"), optopt);
2332 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2341 if (cachefile && nsearch != 0) {
2342 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2346 if ((dryrun || xtreme_rewind) && !do_rewind) {
2347 (void) fprintf(stderr,
2348 gettext("-n or -X only meaningful with -F\n"));
2352 rewind_policy = ZPOOL_TRY_REWIND;
2354 rewind_policy = ZPOOL_DO_REWIND;
2356 rewind_policy |= ZPOOL_EXTREME_REWIND;
2358 /* In the future, we can capture further policy and include it here */
2359 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2360 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2361 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2364 if (searchdirs == NULL) {
2365 searchdirs = safe_malloc(sizeof (char *));
2366 searchdirs[0] = "/dev";
2370 /* check argument count */
2373 (void) fprintf(stderr, gettext("too many arguments\n"));
2378 (void) fprintf(stderr, gettext("too many arguments\n"));
2383 * Check for the SYS_CONFIG privilege. We do this explicitly
2384 * here because otherwise any attempt to discover pools will
2387 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2388 (void) fprintf(stderr, gettext("cannot "
2389 "discover pools: permission denied\n"));
2391 nvlist_free(policy);
2397 * Depending on the arguments given, we do one of the following:
2399 * <none> Iterate through all pools and display information about
2402 * -a Iterate through all pools and try to import each one.
2404 * <id> Find the pool that corresponds to the given GUID/pool
2405 * name and import that one.
2407 * -D Above options applies only to destroyed pools.
2413 searchguid = strtoull(argv[0], &endptr, 10);
2414 if (errno != 0 || *endptr != '\0') {
2415 searchname = argv[0];
2418 found_config = NULL;
2421 * User specified a name or guid. Ensure it's unique.
2423 idata.unique = B_TRUE;
2427 idata.path = searchdirs;
2428 idata.paths = nsearch;
2429 idata.poolname = searchname;
2430 idata.guid = searchguid;
2431 idata.cachefile = cachefile;
2432 idata.policy = policy;
2434 pools = zpool_search_import(g_zfs, &idata);
2436 if (pools != NULL && idata.exists &&
2437 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2438 (void) fprintf(stderr, gettext("cannot import '%s': "
2439 "a pool with that name already exists\n"),
2441 (void) fprintf(stderr, gettext("use the form '%s "
2442 "<pool | id> <newpool>' to give it a new name\n"),
2445 } else if (pools == NULL && idata.exists) {
2446 (void) fprintf(stderr, gettext("cannot import '%s': "
2447 "a pool with that name is already created/imported,\n"),
2449 (void) fprintf(stderr, gettext("and no additional pools "
2450 "with that name were found\n"));
2452 } else if (pools == NULL) {
2454 (void) fprintf(stderr, gettext("cannot import '%s': "
2455 "no such pool available\n"), argv[0]);
2462 nvlist_free(policy);
2467 * At this point we have a list of import candidate configs. Even if
2468 * we were searching by pool name or guid, we still need to
2469 * post-process the list to deal with pool state and possible
2475 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2477 verify(nvpair_value_nvlist(elem, &config) == 0);
2479 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2481 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2483 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2486 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2493 (void) printf("\n");
2496 err |= do_import(config, NULL, mntopts,
2499 show_import(config);
2501 } else if (searchname != NULL) {
2505 * We are searching for a pool based on name.
2507 verify(nvlist_lookup_string(config,
2508 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2510 if (strcmp(name, searchname) == 0) {
2511 if (found_config != NULL) {
2512 (void) fprintf(stderr, gettext(
2513 "cannot import '%s': more than "
2514 "one matching pool\n"), searchname);
2515 (void) fprintf(stderr, gettext(
2516 "import by numeric ID instead\n"));
2519 found_config = config;
2525 * Search for a pool by guid.
2527 verify(nvlist_lookup_uint64(config,
2528 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2530 if (guid == searchguid)
2531 found_config = config;
2536 * If we were searching for a specific pool, verify that we found a
2537 * pool, and then do the import.
2539 if (argc != 0 && err == 0) {
2540 if (found_config == NULL) {
2541 (void) fprintf(stderr, gettext("cannot import '%s': "
2542 "no such pool available\n"), argv[0]);
2545 err |= do_import(found_config, argc == 1 ? NULL :
2546 argv[1], mntopts, props, flags);
2551 * If we were just looking for pools, report an error if none were
2554 if (argc == 0 && first)
2555 (void) fprintf(stderr,
2556 gettext("no pools available to import\n"));
2561 nvlist_free(policy);
2564 return (err ? 1 : 0);
2567 typedef struct iostat_cbdata {
2568 boolean_t cb_verbose;
2571 zpool_list_t *cb_list;
2575 print_iostat_separator(iostat_cbdata_t *cb)
2579 for (i = 0; i < cb->cb_namewidth; i++)
2581 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2585 print_iostat_header(iostat_cbdata_t *cb)
2587 (void) printf("%*s capacity operations bandwidth\n",
2588 cb->cb_namewidth, "");
2589 (void) printf("%-*s alloc free read write read write\n",
2590 cb->cb_namewidth, "pool");
2591 print_iostat_separator(cb);
2595 * Display a single statistic.
2598 print_one_stat(uint64_t value)
2602 zfs_nicenum(value, buf, sizeof (buf));
2603 (void) printf(" %5s", buf);
2607 * Print out all the statistics for the given vdev. This can either be the
2608 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2609 * is a verbose output, and we don't want to display the toplevel pool stats.
2612 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2613 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2615 nvlist_t **oldchild, **newchild;
2617 vdev_stat_t *oldvs, *newvs;
2618 vdev_stat_t zerovs = { 0 };
2623 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
2626 if (oldnv != NULL) {
2627 verify(nvlist_lookup_uint64_array(oldnv,
2628 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2633 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2634 (uint64_t **)&newvs, &c) == 0);
2636 if (strlen(name) + depth > cb->cb_namewidth)
2637 (void) printf("%*s%s", depth, "", name);
2639 (void) printf("%*s%s%*s", depth, "", name,
2640 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2642 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2647 scale = (double)NANOSEC / tdelta;
2649 /* only toplevel vdevs have capacity stats */
2650 if (newvs->vs_space == 0) {
2651 (void) printf(" - -");
2653 print_one_stat(newvs->vs_alloc);
2654 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2657 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2658 oldvs->vs_ops[ZIO_TYPE_READ])));
2660 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2661 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2663 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2664 oldvs->vs_bytes[ZIO_TYPE_READ])));
2666 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2667 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2669 (void) printf("\n");
2671 if (!cb->cb_verbose)
2674 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2675 &newchild, &children) != 0)
2678 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2679 &oldchild, &c) != 0)
2682 for (c = 0; c < children; c++) {
2683 uint64_t ishole = B_FALSE, islog = B_FALSE;
2685 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2688 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2691 if (ishole || islog)
2694 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2695 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2696 newchild[c], cb, depth + 2);
2701 * Log device section
2704 if (num_logs(newnv) > 0) {
2705 (void) printf("%-*s - - - - - "
2706 "-\n", cb->cb_namewidth, "logs");
2708 for (c = 0; c < children; c++) {
2709 uint64_t islog = B_FALSE;
2710 (void) nvlist_lookup_uint64(newchild[c],
2711 ZPOOL_CONFIG_IS_LOG, &islog);
2714 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2716 print_vdev_stats(zhp, vname, oldnv ?
2717 oldchild[c] : NULL, newchild[c],
2726 * Include level 2 ARC devices in iostat output
2728 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2729 &newchild, &children) != 0)
2732 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2733 &oldchild, &c) != 0)
2737 (void) printf("%-*s - - - - - "
2738 "-\n", cb->cb_namewidth, "cache");
2739 for (c = 0; c < children; c++) {
2740 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2742 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2743 newchild[c], cb, depth + 2);
2750 refresh_iostat(zpool_handle_t *zhp, void *data)
2752 iostat_cbdata_t *cb = data;
2756 * If the pool has disappeared, remove it from the list and continue.
2758 if (zpool_refresh_stats(zhp, &missing) != 0)
2762 pool_list_remove(cb->cb_list, zhp);
2768 * Callback to print out the iostats for the given pool.
2771 print_iostat(zpool_handle_t *zhp, void *data)
2773 iostat_cbdata_t *cb = data;
2774 nvlist_t *oldconfig, *newconfig;
2775 nvlist_t *oldnvroot, *newnvroot;
2777 newconfig = zpool_get_config(zhp, &oldconfig);
2779 if (cb->cb_iteration == 1)
2782 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2785 if (oldconfig == NULL)
2788 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2792 * Print out the statistics for the pool.
2794 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2797 print_iostat_separator(cb);
2803 get_namewidth(zpool_handle_t *zhp, void *data)
2805 iostat_cbdata_t *cb = data;
2806 nvlist_t *config, *nvroot;
2808 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2809 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2811 if (!cb->cb_verbose)
2812 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2814 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2819 * The width must fall into the range [10,38]. The upper limit is the
2820 * maximum we can have and still fit in 80 columns.
2822 if (cb->cb_namewidth < 10)
2823 cb->cb_namewidth = 10;
2824 if (cb->cb_namewidth > 38)
2825 cb->cb_namewidth = 38;
2831 * Parse the input string, get the 'interval' and 'count' value if there is one.
2834 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2837 unsigned long interval = 0, count = 0;
2838 int argc = *argcp, errno;
2841 * Determine if the last argument is an integer or a pool name
2843 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2847 interval = strtoul(argv[argc - 1], &end, 10);
2849 if (*end == '\0' && errno == 0) {
2850 if (interval == 0) {
2851 (void) fprintf(stderr, gettext("interval "
2852 "cannot be zero\n"));
2856 * Ignore the last parameter
2861 * If this is not a valid number, just plow on. The
2862 * user will get a more informative error message later
2870 * If the last argument is also an integer, then we have both a count
2873 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2878 interval = strtoul(argv[argc - 1], &end, 10);
2880 if (*end == '\0' && errno == 0) {
2881 if (interval == 0) {
2882 (void) fprintf(stderr, gettext("interval "
2883 "cannot be zero\n"));
2888 * Ignore the last parameter
2902 get_timestamp_arg(char c)
2905 timestamp_fmt = UDATE;
2907 timestamp_fmt = DDATE;
2913 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2915 * -v Display statistics for individual vdevs
2916 * -T Display a timestamp in date(1) or Unix format
2918 * This command can be tricky because we want to be able to deal with pool
2919 * creation/destruction as well as vdev configuration changes. The bulk of this
2920 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2921 * on pool_list_update() to detect the addition of new pools. Configuration
2922 * changes are all handled within libzfs.
2925 zpool_do_iostat(int argc, char **argv)
2930 unsigned long interval = 0, count = 0;
2932 boolean_t verbose = B_FALSE;
2936 while ((c = getopt(argc, argv, "T:v")) != -1) {
2939 get_timestamp_arg(*optarg);
2945 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2954 get_interval_count(&argc, argv, &interval, &count);
2957 * Construct the list of all interesting pools.
2960 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2963 if (pool_list_count(list) == 0 && argc != 0) {
2964 pool_list_free(list);
2968 if (pool_list_count(list) == 0 && interval == 0) {
2969 pool_list_free(list);
2970 (void) fprintf(stderr, gettext("no pools available\n"));
2975 * Enter the main iostat loop.
2978 cb.cb_verbose = verbose;
2979 cb.cb_iteration = 0;
2980 cb.cb_namewidth = 0;
2983 pool_list_update(list);
2985 if ((npools = pool_list_count(list)) == 0)
2989 * Refresh all statistics. This is done as an explicit step
2990 * before calculating the maximum name width, so that any
2991 * configuration changes are properly accounted for.
2993 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2996 * Iterate over all pools to determine the maximum width
2997 * for the pool / device name column across all pools.
2999 cb.cb_namewidth = 0;
3000 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3002 if (timestamp_fmt != NODATE)
3003 print_timestamp(timestamp_fmt);
3006 * If it's the first time, or verbose mode, print the header.
3008 if (++cb.cb_iteration == 1 || verbose)
3009 print_iostat_header(&cb);
3011 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
3014 * If there's more than one pool, and we're not in verbose mode
3015 * (which prints a separator for us), then print a separator.
3017 if (npools > 1 && !verbose)
3018 print_iostat_separator(&cb);
3021 (void) printf("\n");
3024 * Flush the output so that redirection to a file isn't buffered
3027 (void) fflush(stdout);
3032 if (count != 0 && --count == 0)
3035 (void) sleep(interval);
3038 pool_list_free(list);
3043 typedef struct list_cbdata {
3044 boolean_t cb_verbose;
3046 boolean_t cb_scripted;
3047 zprop_list_t *cb_proplist;
3048 boolean_t cb_literal;
3052 * Given a list of columns to display, output appropriate headers for each one.
3055 print_header(list_cbdata_t *cb)
3057 zprop_list_t *pl = cb->cb_proplist;
3058 char headerbuf[ZPOOL_MAXPROPLEN];
3060 boolean_t first = B_TRUE;
3061 boolean_t right_justify;
3064 for (; pl != NULL; pl = pl->pl_next) {
3065 width = pl->pl_width;
3066 if (first && cb->cb_verbose) {
3068 * Reset the width to accommodate the verbose listing
3071 width = cb->cb_namewidth;
3079 right_justify = B_FALSE;
3080 if (pl->pl_prop != ZPROP_INVAL) {
3081 header = zpool_prop_column_name(pl->pl_prop);
3082 right_justify = zpool_prop_align_right(pl->pl_prop);
3086 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3087 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3088 headerbuf[i] = '\0';
3092 if (pl->pl_next == NULL && !right_justify)
3093 (void) printf("%s", header);
3094 else if (right_justify)
3095 (void) printf("%*s", width, header);
3097 (void) printf("%-*s", width, header);
3101 (void) printf("\n");
3105 * Given a pool and a list of properties, print out all the properties according
3106 * to the described layout.
3109 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
3111 zprop_list_t *pl = cb->cb_proplist;
3112 boolean_t first = B_TRUE;
3113 char property[ZPOOL_MAXPROPLEN];
3115 boolean_t right_justify;
3118 for (; pl != NULL; pl = pl->pl_next) {
3120 width = pl->pl_width;
3121 if (first && cb->cb_verbose) {
3123 * Reset the width to accommodate the verbose listing
3126 width = cb->cb_namewidth;
3130 if (cb->cb_scripted)
3131 (void) printf("\t");
3138 right_justify = B_FALSE;
3139 if (pl->pl_prop != ZPROP_INVAL) {
3140 if (zpool_get_prop(zhp, pl->pl_prop, property,
3141 sizeof (property), NULL, cb->cb_literal) != 0)
3146 right_justify = zpool_prop_align_right(pl->pl_prop);
3147 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
3148 zpool_prop_unsupported(pl->pl_user_prop)) &&
3149 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3150 sizeof (property)) == 0) {
3158 * If this is being called in scripted mode, or if this is the
3159 * last column and it is left-justified, don't include a width
3162 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3163 (void) printf("%s", propstr);
3164 else if (right_justify)
3165 (void) printf("%*s", width, propstr);
3167 (void) printf("%-*s", width, propstr);
3170 (void) printf("\n");
3174 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3179 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3182 case ZPOOL_PROP_EXPANDSZ:
3183 case ZPOOL_PROP_CHECKPOINT:
3185 (void) strlcpy(propval, "-", sizeof (propval));
3187 zfs_nicenum(value, propval, sizeof (propval));
3189 case ZPOOL_PROP_FRAGMENTATION:
3190 if (value == ZFS_FRAG_INVALID) {
3191 (void) strlcpy(propval, "-", sizeof (propval));
3193 (void) snprintf(propval, sizeof (propval), "%llu%%",
3197 case ZPOOL_PROP_CAPACITY:
3198 (void) snprintf(propval, sizeof (propval), "%llu%%", value);
3201 zfs_nicenum(value, propval, sizeof (propval));
3205 (void) strlcpy(propval, "-", sizeof (propval));
3208 (void) printf("\t%s", propval);
3210 (void) printf(" %*s", width, propval);
3214 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3215 list_cbdata_t *cb, int depth)
3221 boolean_t scripted = cb->cb_scripted;
3222 uint64_t islog = B_FALSE;
3223 boolean_t haslog = B_FALSE;
3224 char *dashes = "%-*s - - - - - -\n";
3226 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3227 (uint64_t **)&vs, &c) == 0);
3230 boolean_t toplevel = (vs->vs_space != 0);
3233 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
3237 (void) printf("\t%s", name);
3238 else if (strlen(name) + depth > cb->cb_namewidth)
3239 (void) printf("%*s%s", depth, "", name);
3241 (void) printf("%*s%s%*s", depth, "", name,
3242 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3245 * Print the properties for the individual vdevs. Some
3246 * properties are only applicable to toplevel vdevs. The
3247 * 'toplevel' boolean value is passed to the print_one_column()
3248 * to indicate that the value is valid.
3250 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3252 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3254 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3255 scripted, toplevel);
3256 print_one_column(ZPOOL_PROP_CHECKPOINT,
3257 vs->vs_checkpoint_space, scripted, toplevel);
3258 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3260 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3261 vs->vs_fragmentation, scripted,
3262 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3263 cap = (vs->vs_space == 0) ? 0 :
3264 (vs->vs_alloc * 100 / vs->vs_space);
3265 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3266 (void) printf("\n");
3269 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3270 &child, &children) != 0)
3273 for (c = 0; c < children; c++) {
3274 uint64_t ishole = B_FALSE;
3276 if (nvlist_lookup_uint64(child[c],
3277 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3280 if (nvlist_lookup_uint64(child[c],
3281 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
3286 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3287 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3291 if (haslog == B_TRUE) {
3292 /* LINTED E_SEC_PRINTF_VAR_FMT */
3293 (void) printf(dashes, cb->cb_namewidth, "log");
3294 for (c = 0; c < children; c++) {
3295 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3296 &islog) != 0 || !islog)
3298 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3299 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3304 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3305 &child, &children) == 0 && children > 0) {
3306 /* LINTED E_SEC_PRINTF_VAR_FMT */
3307 (void) printf(dashes, cb->cb_namewidth, "cache");
3308 for (c = 0; c < children; c++) {
3309 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3310 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3315 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
3316 &children) == 0 && children > 0) {
3317 /* LINTED E_SEC_PRINTF_VAR_FMT */
3318 (void) printf(dashes, cb->cb_namewidth, "spare");
3319 for (c = 0; c < children; c++) {
3320 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3321 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3329 * Generic callback function to list a pool.
3332 list_callback(zpool_handle_t *zhp, void *data)
3334 list_cbdata_t *cbp = data;
3338 config = zpool_get_config(zhp, NULL);
3340 print_pool(zhp, cbp);
3341 if (!cbp->cb_verbose)
3344 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3346 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3352 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3354 * -H Scripted mode. Don't display headers, and separate properties
3356 * -o List of properties to display. Defaults to
3357 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3358 * "dedupratio,health,altroot"
3359 * -p Diplay values in parsable (exact) format.
3360 * -T Display a timestamp in date(1) or Unix format
3362 * List all pools in the system, whether or not they're healthy. Output space
3363 * statistics for each one, as well as health status summary.
3366 zpool_do_list(int argc, char **argv)
3370 list_cbdata_t cb = { 0 };
3371 static char default_props[] =
3372 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
3373 "capacity,dedupratio,health,altroot";
3374 char *props = default_props;
3375 unsigned long interval = 0, count = 0;
3377 boolean_t first = B_TRUE;
3380 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3383 cb.cb_scripted = B_TRUE;
3389 cb.cb_literal = B_TRUE;
3392 get_timestamp_arg(*optarg);
3395 cb.cb_verbose = B_TRUE;
3398 (void) fprintf(stderr, gettext("missing argument for "
3399 "'%c' option\n"), optopt);
3403 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3412 get_interval_count(&argc, argv, &interval, &count);
3414 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3418 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3422 if (pool_list_count(list) == 0)
3425 cb.cb_namewidth = 0;
3426 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3428 if (timestamp_fmt != NODATE)
3429 print_timestamp(timestamp_fmt);
3431 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3435 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3440 if (count != 0 && --count == 0)
3443 pool_list_free(list);
3444 (void) sleep(interval);
3447 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3448 (void) printf(gettext("no pools available\n"));
3452 pool_list_free(list);
3453 zprop_free_list(cb.cb_proplist);
3458 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3460 boolean_t force = B_FALSE;
3463 char *poolname, *old_disk, *new_disk;
3464 zpool_handle_t *zhp;
3465 zpool_boot_label_t boot_type;
3470 while ((c = getopt(argc, argv, "f")) != -1) {
3476 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3485 /* get pool name and check number of arguments */
3487 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3494 (void) fprintf(stderr,
3495 gettext("missing <device> specification\n"));
3503 (void) fprintf(stderr,
3504 gettext("missing <new_device> specification\n"));
3507 new_disk = old_disk;
3517 (void) fprintf(stderr, gettext("too many arguments\n"));
3521 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3524 if (zpool_get_config(zhp, NULL) == NULL) {
3525 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3531 if (zpool_is_bootable(zhp))
3532 boot_type = ZPOOL_COPY_BOOT_LABEL;
3534 boot_type = ZPOOL_NO_BOOT_LABEL;
3536 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3537 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3538 boot_type, boot_size, argc, argv);
3539 if (nvroot == NULL) {
3544 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3546 nvlist_free(nvroot);
3553 * zpool replace [-f] <pool> <device> <new_device>
3555 * -f Force attach, even if <new_device> appears to be in use.
3557 * Replace <device> with <new_device>.
3561 zpool_do_replace(int argc, char **argv)
3563 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3567 * zpool attach [-f] <pool> <device> <new_device>
3569 * -f Force attach, even if <new_device> appears to be in use.
3571 * Attach <new_device> to the mirror containing <device>. If <device> is not
3572 * part of a mirror, then <device> will be transformed into a mirror of
3573 * <device> and <new_device>. In either case, <new_device> will begin life
3574 * with a DTL of [0, now], and will immediately begin to resilver itself.
3577 zpool_do_attach(int argc, char **argv)
3579 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3583 * zpool detach [-f] <pool> <device>
3585 * -f Force detach of <device>, even if DTLs argue against it
3586 * (not supported yet)
3588 * Detach a device from a mirror. The operation will be refused if <device>
3589 * is the last device in the mirror, or if the DTLs indicate that this device
3590 * has the only valid copy of some data.
3594 zpool_do_detach(int argc, char **argv)
3597 char *poolname, *path;
3598 zpool_handle_t *zhp;
3602 while ((c = getopt(argc, argv, "f")) != -1) {
3606 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3615 /* get pool name and check number of arguments */
3617 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3622 (void) fprintf(stderr,
3623 gettext("missing <device> specification\n"));
3630 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3633 ret = zpool_vdev_detach(zhp, path);
3641 * zpool split [-n] [-o prop=val] ...
3643 * [-R altroot] <pool> <newpool> [<device> ...]
3645 * -n Do not split the pool, but display the resulting layout if
3646 * it were to be split.
3647 * -o Set property=value, or set mount options.
3648 * -R Mount the split-off pool under an alternate root.
3650 * Splits the named pool and gives it the new pool name. Devices to be split
3651 * off may be listed, provided that no more than one device is specified
3652 * per top-level vdev mirror. The newly split pool is left in an exported
3653 * state unless -R is specified.
3655 * Restrictions: the top-level of the pool pool must only be made up of
3656 * mirrors; all devices in the pool must be healthy; no device may be
3657 * undergoing a resilvering operation.
3660 zpool_do_split(int argc, char **argv)
3662 char *srcpool, *newpool, *propval;
3663 char *mntopts = NULL;
3666 zpool_handle_t *zhp;
3667 nvlist_t *config, *props = NULL;
3669 flags.dryrun = B_FALSE;
3670 flags.import = B_FALSE;
3673 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3676 flags.import = B_TRUE;
3678 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3679 &props, B_TRUE) != 0) {
3685 flags.dryrun = B_TRUE;
3688 if ((propval = strchr(optarg, '=')) != NULL) {
3691 if (add_prop_list(optarg, propval,
3692 &props, B_TRUE) != 0) {
3701 (void) fprintf(stderr, gettext("missing argument for "
3702 "'%c' option\n"), optopt);
3706 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3713 if (!flags.import && mntopts != NULL) {
3714 (void) fprintf(stderr, gettext("setting mntopts is only "
3715 "valid when importing the pool\n"));
3723 (void) fprintf(stderr, gettext("Missing pool name\n"));
3727 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3737 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3740 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3741 if (config == NULL) {
3745 (void) printf(gettext("would create '%s' with the "
3746 "following layout:\n\n"), newpool);
3747 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3749 nvlist_free(config);
3754 if (ret != 0 || flags.dryrun || !flags.import)
3758 * The split was successful. Now we need to open the new
3759 * pool and import it.
3761 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3763 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3764 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3766 (void) fprintf(stderr, gettext("Split was successful, but "
3767 "the datasets could not all be mounted\n"));
3768 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3769 "different altroot\n"), "zpool import");
3779 * zpool online <pool> <device> ...
3782 zpool_do_online(int argc, char **argv)
3786 zpool_handle_t *zhp;
3788 vdev_state_t newstate;
3792 while ((c = getopt(argc, argv, "et")) != -1) {
3795 flags |= ZFS_ONLINE_EXPAND;
3799 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3808 /* get pool name and check number of arguments */
3810 (void) fprintf(stderr, gettext("missing pool name\n"));
3814 (void) fprintf(stderr, gettext("missing device name\n"));
3820 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3823 for (i = 1; i < argc; i++) {
3824 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3825 if (newstate != VDEV_STATE_HEALTHY) {
3826 (void) printf(gettext("warning: device '%s' "
3827 "onlined, but remains in faulted state\n"),
3829 if (newstate == VDEV_STATE_FAULTED)
3830 (void) printf(gettext("use 'zpool "
3831 "clear' to restore a faulted "
3834 (void) printf(gettext("use 'zpool "
3835 "replace' to replace devices "
3836 "that are no longer present\n"));
3849 * zpool offline [-ft] <pool> <device> ...
3851 * -f Force the device into the offline state, even if doing
3852 * so would appear to compromise pool availability.
3853 * (not supported yet)
3855 * -t Only take the device off-line temporarily. The offline
3856 * state will not be persistent across reboots.
3860 zpool_do_offline(int argc, char **argv)
3864 zpool_handle_t *zhp;
3866 boolean_t istmp = B_FALSE;
3869 while ((c = getopt(argc, argv, "ft")) != -1) {
3876 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3885 /* get pool name and check number of arguments */
3887 (void) fprintf(stderr, gettext("missing pool name\n"));
3891 (void) fprintf(stderr, gettext("missing device name\n"));
3897 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3900 for (i = 1; i < argc; i++) {
3901 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3911 * zpool clear <pool> [device]
3913 * Clear all errors associated with a pool or a particular device.
3916 zpool_do_clear(int argc, char **argv)
3920 boolean_t dryrun = B_FALSE;
3921 boolean_t do_rewind = B_FALSE;
3922 boolean_t xtreme_rewind = B_FALSE;
3923 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3924 nvlist_t *policy = NULL;
3925 zpool_handle_t *zhp;
3926 char *pool, *device;
3929 while ((c = getopt(argc, argv, "FnX")) != -1) {
3938 xtreme_rewind = B_TRUE;
3941 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3951 (void) fprintf(stderr, gettext("missing pool name\n"));
3956 (void) fprintf(stderr, gettext("too many arguments\n"));
3960 if ((dryrun || xtreme_rewind) && !do_rewind) {
3961 (void) fprintf(stderr,
3962 gettext("-n or -X only meaningful with -F\n"));
3966 rewind_policy = ZPOOL_TRY_REWIND;
3968 rewind_policy = ZPOOL_DO_REWIND;
3970 rewind_policy |= ZPOOL_EXTREME_REWIND;
3972 /* In future, further rewind policy choices can be passed along here */
3973 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3974 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3978 device = argc == 2 ? argv[1] : NULL;
3980 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3981 nvlist_free(policy);
3985 if (zpool_clear(zhp, device, policy) != 0)
3990 nvlist_free(policy);
3996 * zpool reguid <pool>
3999 zpool_do_reguid(int argc, char **argv)
4003 zpool_handle_t *zhp;
4007 while ((c = getopt(argc, argv, "")) != -1) {
4010 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4019 /* get pool name and check number of arguments */
4021 (void) fprintf(stderr, gettext("missing pool name\n"));
4026 (void) fprintf(stderr, gettext("too many arguments\n"));
4031 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4034 ret = zpool_reguid(zhp);
4042 * zpool reopen <pool>
4044 * Reopen the pool so that the kernel can update the sizes of all vdevs.
4047 zpool_do_reopen(int argc, char **argv)
4051 zpool_handle_t *zhp;
4055 while ((c = getopt(argc, argv, "")) != -1) {
4058 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4068 (void) fprintf(stderr, gettext("missing pool name\n"));
4073 (void) fprintf(stderr, gettext("too many arguments\n"));
4078 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
4081 ret = zpool_reopen(zhp);
4086 typedef struct scrub_cbdata {
4090 pool_scrub_cmd_t cb_scrub_cmd;
4094 zpool_has_checkpoint(zpool_handle_t *zhp)
4096 nvlist_t *config, *nvroot;
4098 config = zpool_get_config(zhp, NULL);
4100 if (config != NULL) {
4101 pool_checkpoint_stat_t *pcs = NULL;
4104 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
4105 (void) nvlist_lookup_uint64_array(nvroot,
4106 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
4108 if (pcs == NULL || pcs->pcs_state == CS_NONE)
4111 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
4112 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
4120 scrub_callback(zpool_handle_t *zhp, void *data)
4122 scrub_cbdata_t *cb = data;
4126 * Ignore faulted pools.
4128 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4129 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
4130 "currently unavailable\n"), zpool_get_name(zhp));
4134 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
4136 if (err == 0 && zpool_has_checkpoint(zhp) &&
4137 cb->cb_type == POOL_SCAN_SCRUB) {
4138 (void) printf(gettext("warning: will not scrub state that "
4139 "belongs to the checkpoint of pool '%s'\n"),
4140 zpool_get_name(zhp));
4147 * zpool scrub [-s | -p] <pool> ...
4149 * -s Stop. Stops any in-progress scrub.
4150 * -p Pause. Pause in-progress scrub.
4153 zpool_do_scrub(int argc, char **argv)
4158 cb.cb_type = POOL_SCAN_SCRUB;
4159 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
4162 while ((c = getopt(argc, argv, "sp")) != -1) {
4165 cb.cb_type = POOL_SCAN_NONE;
4168 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
4171 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4177 if (cb.cb_type == POOL_SCAN_NONE &&
4178 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
4179 (void) fprintf(stderr, gettext("invalid option combination: "
4180 "-s and -p are mutually exclusive\n"));
4190 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4194 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4197 typedef struct status_cbdata {
4199 boolean_t cb_allpools;
4200 boolean_t cb_verbose;
4201 boolean_t cb_explain;
4203 boolean_t cb_dedup_stats;
4207 * Print out detailed scrub status.
4210 print_scan_status(pool_scan_stat_t *ps)
4212 time_t start, end, pause;
4213 uint64_t elapsed, mins_left, hours_left;
4214 uint64_t pass_exam, examined, total;
4216 double fraction_done;
4217 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4219 (void) printf(gettext(" scan: "));
4221 /* If there's never been a scan, there's not much to say. */
4222 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
4223 ps->pss_func >= POOL_SCAN_FUNCS) {
4224 (void) printf(gettext("none requested\n"));
4228 start = ps->pss_start_time;
4229 end = ps->pss_end_time;
4230 pause = ps->pss_pass_scrub_pause;
4231 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4233 assert(ps->pss_func == POOL_SCAN_SCRUB ||
4234 ps->pss_func == POOL_SCAN_RESILVER);
4236 * Scan is finished or canceled.
4238 if (ps->pss_state == DSS_FINISHED) {
4239 uint64_t minutes_taken = (end - start) / 60;
4242 if (ps->pss_func == POOL_SCAN_SCRUB) {
4243 fmt = gettext("scrub repaired %s in %lluh%um with "
4244 "%llu errors on %s");
4245 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4246 fmt = gettext("resilvered %s in %lluh%um with "
4247 "%llu errors on %s");
4250 (void) printf(fmt, processed_buf,
4251 (u_longlong_t)(minutes_taken / 60),
4252 (uint_t)(minutes_taken % 60),
4253 (u_longlong_t)ps->pss_errors,
4254 ctime((time_t *)&end));
4256 } else if (ps->pss_state == DSS_CANCELED) {
4257 if (ps->pss_func == POOL_SCAN_SCRUB) {
4258 (void) printf(gettext("scrub canceled on %s"),
4260 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4261 (void) printf(gettext("resilver canceled on %s"),
4267 assert(ps->pss_state == DSS_SCANNING);
4270 * Scan is in progress.
4272 if (ps->pss_func == POOL_SCAN_SCRUB) {
4274 (void) printf(gettext("scrub in progress since %s"),
4278 struct tm *p = localtime(&pause);
4279 (void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
4280 (void) printf(gettext("scrub paused since %s\n"), buf);
4281 (void) printf(gettext("\tscrub started on %s"),
4284 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4285 (void) printf(gettext("resilver in progress since %s"),
4289 examined = ps->pss_examined ? ps->pss_examined : 1;
4290 total = ps->pss_to_examine;
4291 fraction_done = (double)examined / total;
4293 /* elapsed time for this pass */
4294 elapsed = time(NULL) - ps->pss_pass_start;
4295 elapsed -= ps->pss_pass_scrub_spent_paused;
4296 elapsed = elapsed ? elapsed : 1;
4297 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4298 rate = pass_exam / elapsed;
4299 rate = rate ? rate : 1;
4300 mins_left = ((total - examined) / rate) / 60;
4301 hours_left = mins_left / 60;
4303 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4304 zfs_nicenum(total, total_buf, sizeof (total_buf));
4307 * do not print estimated time if hours_left is more than 30 days
4308 * or we have a paused scrub
4311 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4312 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
4313 examined_buf, total_buf, rate_buf);
4314 if (hours_left < (30 * 24)) {
4315 (void) printf(gettext(", %lluh%um to go\n"),
4316 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4318 (void) printf(gettext(
4319 ", (scan is slow, no estimated time)\n"));
4322 (void) printf(gettext("\t%s scanned out of %s\n"),
4323 examined_buf, total_buf);
4326 if (ps->pss_func == POOL_SCAN_RESILVER) {
4327 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4328 processed_buf, 100 * fraction_done);
4329 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4330 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4331 processed_buf, 100 * fraction_done);
4336 * As we don't scrub checkpointed blocks, we want to warn the
4337 * user that we skipped scanning some blocks if a checkpoint exists
4338 * or existed at any time during the scan.
4341 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
4343 if (ps == NULL || pcs == NULL)
4346 if (pcs->pcs_state == CS_NONE ||
4347 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
4350 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
4352 if (ps->pss_state == DSS_NONE)
4355 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
4356 ps->pss_end_time < pcs->pcs_start_time)
4359 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
4360 (void) printf(gettext(" scan warning: skipped blocks "
4361 "that are only referenced by the checkpoint.\n"));
4363 assert(ps->pss_state == DSS_SCANNING);
4364 (void) printf(gettext(" scan warning: skipping blocks "
4365 "that are only referenced by the checkpoint.\n"));
4370 * Print out detailed removal status.
4373 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
4375 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4377 nvlist_t *config, *nvroot;
4382 if (prs == NULL || prs->prs_state == DSS_NONE)
4386 * Determine name of vdev.
4388 config = zpool_get_config(zhp, NULL);
4389 nvroot = fnvlist_lookup_nvlist(config,
4390 ZPOOL_CONFIG_VDEV_TREE);
4391 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4392 &child, &children) == 0);
4393 assert(prs->prs_removing_vdev < children);
4394 vdev_name = zpool_vdev_name(g_zfs, zhp,
4395 child[prs->prs_removing_vdev], B_TRUE);
4397 (void) printf(gettext("remove: "));
4399 start = prs->prs_start_time;
4400 end = prs->prs_end_time;
4401 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
4404 * Removal is finished or canceled.
4406 if (prs->prs_state == DSS_FINISHED) {
4407 uint64_t minutes_taken = (end - start) / 60;
4409 (void) printf(gettext("Removal of vdev %llu copied %s "
4410 "in %lluh%um, completed on %s"),
4411 (longlong_t)prs->prs_removing_vdev,
4413 (u_longlong_t)(minutes_taken / 60),
4414 (uint_t)(minutes_taken % 60),
4415 ctime((time_t *)&end));
4416 } else if (prs->prs_state == DSS_CANCELED) {
4417 (void) printf(gettext("Removal of %s canceled on %s"),
4418 vdev_name, ctime(&end));
4420 uint64_t copied, total, elapsed, mins_left, hours_left;
4421 double fraction_done;
4424 assert(prs->prs_state == DSS_SCANNING);
4427 * Removal is in progress.
4429 (void) printf(gettext(
4430 "Evacuation of %s in progress since %s"),
4431 vdev_name, ctime(&start));
4433 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
4434 total = prs->prs_to_copy;
4435 fraction_done = (double)copied / total;
4437 /* elapsed time for this pass */
4438 elapsed = time(NULL) - prs->prs_start_time;
4439 elapsed = elapsed > 0 ? elapsed : 1;
4440 rate = copied / elapsed;
4441 rate = rate > 0 ? rate : 1;
4442 mins_left = ((total - copied) / rate) / 60;
4443 hours_left = mins_left / 60;
4445 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
4446 zfs_nicenum(total, total_buf, sizeof (total_buf));
4447 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4450 * do not print estimated time if hours_left is more than
4453 (void) printf(gettext(" %s copied out of %s at %s/s, "
4455 examined_buf, total_buf, rate_buf, 100 * fraction_done);
4456 if (hours_left < (30 * 24)) {
4457 (void) printf(gettext(", %lluh%um to go\n"),
4458 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4460 (void) printf(gettext(
4461 ", (copy is slow, no estimated time)\n"));
4465 if (prs->prs_mapping_memory > 0) {
4467 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
4468 (void) printf(gettext(" %s memory used for "
4469 "removed device mappings\n"),
4475 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
4480 if (pcs == NULL || pcs->pcs_state == CS_NONE)
4483 (void) printf(gettext("checkpoint: "));
4485 start = pcs->pcs_start_time;
4486 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
4488 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
4489 char *date = ctime(&start);
4492 * ctime() adds a newline at the end of the generated
4493 * string, thus the weird format specifier and the
4494 * strlen() call used to chop it off from the output.
4496 (void) printf(gettext("created %.*s, consumes %s\n"),
4497 strlen(date) - 1, date, space_buf);
4501 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
4503 (void) printf(gettext("discarding, %s remaining.\n"),
4508 print_error_log(zpool_handle_t *zhp)
4510 nvlist_t *nverrlist = NULL;
4513 size_t len = MAXPATHLEN * 2;
4515 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4516 (void) printf("errors: List of errors unavailable "
4517 "(insufficient privileges)\n");
4521 (void) printf("errors: Permanent errors have been "
4522 "detected in the following files:\n\n");
4524 pathname = safe_malloc(len);
4526 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4528 uint64_t dsobj, obj;
4530 verify(nvpair_value_nvlist(elem, &nv) == 0);
4531 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4533 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4535 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4536 (void) printf("%7s %s\n", "", pathname);
4539 nvlist_free(nverrlist);
4543 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4552 (void) printf(gettext("\tspares\n"));
4554 for (i = 0; i < nspares; i++) {
4555 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4556 print_status_config(zhp, name, spares[i],
4557 namewidth, 2, B_TRUE);
4563 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4572 (void) printf(gettext("\tcache\n"));
4574 for (i = 0; i < nl2cache; i++) {
4575 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4576 print_status_config(zhp, name, l2cache[i],
4577 namewidth, 2, B_FALSE);
4583 print_dedup_stats(nvlist_t *config)
4585 ddt_histogram_t *ddh;
4591 * If the pool was faulted then we may not have been able to
4592 * obtain the config. Otherwise, if we have anything in the dedup
4593 * table continue processing the stats.
4595 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4596 (uint64_t **)&ddo, &c) != 0)
4599 (void) printf("\n");
4600 (void) printf(gettext(" dedup: "));
4601 if (ddo->ddo_count == 0) {
4602 (void) printf(gettext("no DDT entries\n"));
4606 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4607 (u_longlong_t)ddo->ddo_count,
4608 (u_longlong_t)ddo->ddo_dspace,
4609 (u_longlong_t)ddo->ddo_mspace);
4611 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4612 (uint64_t **)&dds, &c) == 0);
4613 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4614 (uint64_t **)&ddh, &c) == 0);
4615 zpool_dump_ddt(dds, ddh);
4619 * Display a summary of pool status. Displays a summary such as:
4623 * reason: One or more devices ...
4624 * see: http://illumos.org/msg/ZFS-xxxx-01
4630 * When given the '-v' option, we print out the complete config. If the '-e'
4631 * option is specified, then we print out error rate information as well.
4634 status_callback(zpool_handle_t *zhp, void *data)
4636 status_cbdata_t *cbp = data;
4637 nvlist_t *config, *nvroot;
4644 config = zpool_get_config(zhp, NULL);
4645 reason = zpool_get_status(zhp, &msgid);
4650 * If we were given 'zpool status -x', only report those pools with
4653 if (cbp->cb_explain &&
4654 (reason == ZPOOL_STATUS_OK ||
4655 reason == ZPOOL_STATUS_VERSION_OLDER ||
4656 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4657 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4658 if (!cbp->cb_allpools) {
4659 (void) printf(gettext("pool '%s' is healthy\n"),
4660 zpool_get_name(zhp));
4662 cbp->cb_first = B_FALSE;
4668 cbp->cb_first = B_FALSE;
4670 (void) printf("\n");
4672 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
4673 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4674 (uint64_t **)&vs, &c) == 0);
4675 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4677 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4678 (void) printf(gettext(" state: %s\n"), health);
4681 case ZPOOL_STATUS_MISSING_DEV_R:
4682 (void) printf(gettext("status: One or more devices could not "
4683 "be opened. Sufficient replicas exist for\n\tthe pool to "
4684 "continue functioning in a degraded state.\n"));
4685 (void) printf(gettext("action: Attach the missing device and "
4686 "online it using 'zpool online'.\n"));
4689 case ZPOOL_STATUS_MISSING_DEV_NR:
4690 (void) printf(gettext("status: One or more devices could not "
4691 "be opened. There are insufficient\n\treplicas for the "
4692 "pool to continue functioning.\n"));
4693 (void) printf(gettext("action: Attach the missing device and "
4694 "online it using 'zpool online'.\n"));
4697 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4698 (void) printf(gettext("status: One or more devices could not "
4699 "be used because the label is missing or\n\tinvalid. "
4700 "Sufficient replicas exist for the pool to continue\n\t"
4701 "functioning in a degraded state.\n"));
4702 (void) printf(gettext("action: Replace the device using "
4703 "'zpool replace'.\n"));
4706 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4707 (void) printf(gettext("status: One or more devices could not "
4708 "be used because the label is missing \n\tor invalid. "
4709 "There are insufficient replicas for the pool to "
4710 "continue\n\tfunctioning.\n"));
4711 zpool_explain_recover(zpool_get_handle(zhp),
4712 zpool_get_name(zhp), reason, config);
4715 case ZPOOL_STATUS_FAILING_DEV:
4716 (void) printf(gettext("status: One or more devices has "
4717 "experienced an unrecoverable error. An\n\tattempt was "
4718 "made to correct the error. Applications are "
4720 (void) printf(gettext("action: Determine if the device needs "
4721 "to be replaced, and clear the errors\n\tusing "
4722 "'zpool clear' or replace the device with 'zpool "
4726 case ZPOOL_STATUS_OFFLINE_DEV:
4727 (void) printf(gettext("status: One or more devices has "
4728 "been taken offline by the administrator.\n\tSufficient "
4729 "replicas exist for the pool to continue functioning in "
4730 "a\n\tdegraded state.\n"));
4731 (void) printf(gettext("action: Online the device using "
4732 "'zpool online' or replace the device with\n\t'zpool "
4736 case ZPOOL_STATUS_REMOVED_DEV:
4737 (void) printf(gettext("status: One or more devices has "
4738 "been removed by the administrator.\n\tSufficient "
4739 "replicas exist for the pool to continue functioning in "
4740 "a\n\tdegraded state.\n"));
4741 (void) printf(gettext("action: Online the device using "
4742 "'zpool online' or replace the device with\n\t'zpool "
4746 case ZPOOL_STATUS_RESILVERING:
4747 (void) printf(gettext("status: One or more devices is "
4748 "currently being resilvered. The pool will\n\tcontinue "
4749 "to function, possibly in a degraded state.\n"));
4750 (void) printf(gettext("action: Wait for the resilver to "
4754 case ZPOOL_STATUS_CORRUPT_DATA:
4755 (void) printf(gettext("status: One or more devices has "
4756 "experienced an error resulting in data\n\tcorruption. "
4757 "Applications may be affected.\n"));
4758 (void) printf(gettext("action: Restore the file in question "
4759 "if possible. Otherwise restore the\n\tentire pool from "
4763 case ZPOOL_STATUS_CORRUPT_POOL:
4764 (void) printf(gettext("status: The pool metadata is corrupted "
4765 "and the pool cannot be opened.\n"));
4766 zpool_explain_recover(zpool_get_handle(zhp),
4767 zpool_get_name(zhp), reason, config);
4770 case ZPOOL_STATUS_VERSION_OLDER:
4771 (void) printf(gettext("status: The pool is formatted using a "
4772 "legacy on-disk format. The pool can\n\tstill be used, "
4773 "but some features are unavailable.\n"));
4774 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4775 "upgrade'. Once this is done, the\n\tpool will no longer "
4776 "be accessible on software that does not support feature\n"
4780 case ZPOOL_STATUS_VERSION_NEWER:
4781 (void) printf(gettext("status: The pool has been upgraded to a "
4782 "newer, incompatible on-disk version.\n\tThe pool cannot "
4783 "be accessed on this system.\n"));
4784 (void) printf(gettext("action: Access the pool from a system "
4785 "running more recent software, or\n\trestore the pool from "
4789 case ZPOOL_STATUS_FEAT_DISABLED:
4790 (void) printf(gettext("status: Some supported features are not "
4791 "enabled on the pool. The pool can\n\tstill be used, but "
4792 "some features are unavailable.\n"));
4793 (void) printf(gettext("action: Enable all features using "
4794 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4795 "longer be accessible by software that does not support\n\t"
4796 "the features. See zpool-features(7) for details.\n"));
4799 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4800 (void) printf(gettext("status: The pool cannot be accessed on "
4801 "this system because it uses the\n\tfollowing feature(s) "
4802 "not supported on this system:\n"));
4803 zpool_print_unsup_feat(config);
4804 (void) printf("\n");
4805 (void) printf(gettext("action: Access the pool from a system "
4806 "that supports the required feature(s),\n\tor restore the "
4807 "pool from backup.\n"));
4810 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4811 (void) printf(gettext("status: The pool can only be accessed "
4812 "in read-only mode on this system. It\n\tcannot be "
4813 "accessed in read-write mode because it uses the "
4814 "following\n\tfeature(s) not supported on this system:\n"));
4815 zpool_print_unsup_feat(config);
4816 (void) printf("\n");
4817 (void) printf(gettext("action: The pool cannot be accessed in "
4818 "read-write mode. Import the pool with\n"
4819 "\t\"-o readonly=on\", access the pool from a system that "
4820 "supports the\n\trequired feature(s), or restore the "
4821 "pool from backup.\n"));
4824 case ZPOOL_STATUS_FAULTED_DEV_R:
4825 (void) printf(gettext("status: One or more devices are "
4826 "faulted in response to persistent errors.\n\tSufficient "
4827 "replicas exist for the pool to continue functioning "
4828 "in a\n\tdegraded state.\n"));
4829 (void) printf(gettext("action: Replace the faulted device, "
4830 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4833 case ZPOOL_STATUS_FAULTED_DEV_NR:
4834 (void) printf(gettext("status: One or more devices are "
4835 "faulted in response to persistent errors. There are "
4836 "insufficient replicas for the pool to\n\tcontinue "
4838 (void) printf(gettext("action: Destroy and re-create the pool "
4839 "from a backup source. Manually marking the device\n"
4840 "\trepaired using 'zpool clear' may allow some data "
4841 "to be recovered.\n"));
4844 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4845 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4846 (void) printf(gettext("status: One or more devices are "
4847 "faulted in response to IO failures.\n"));
4848 (void) printf(gettext("action: Make sure the affected devices "
4849 "are connected, then run 'zpool clear'.\n"));
4852 case ZPOOL_STATUS_BAD_LOG:
4853 (void) printf(gettext("status: An intent log record "
4854 "could not be read.\n"
4855 "\tWaiting for adminstrator intervention to fix the "
4856 "faulted pool.\n"));
4857 (void) printf(gettext("action: Either restore the affected "
4858 "device(s) and run 'zpool online',\n"
4859 "\tor ignore the intent log records by running "
4860 "'zpool clear'.\n"));
4863 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4864 (void) printf(gettext("status: One or more devices are "
4865 "configured to use a non-native block size.\n"
4866 "\tExpect reduced performance.\n"));
4867 (void) printf(gettext("action: Replace affected devices with "
4868 "devices that support the\n\tconfigured block size, or "
4869 "migrate data to a properly configured\n\tpool.\n"));
4874 * The remaining errors can't actually be generated, yet.
4876 assert(reason == ZPOOL_STATUS_OK);
4880 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4883 if (config != NULL) {
4886 nvlist_t **spares, **l2cache;
4887 uint_t nspares, nl2cache;
4888 pool_checkpoint_stat_t *pcs = NULL;
4889 pool_scan_stat_t *ps = NULL;
4890 pool_removal_stat_t *prs = NULL;
4892 (void) nvlist_lookup_uint64_array(nvroot,
4893 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
4894 (void) nvlist_lookup_uint64_array(nvroot,
4895 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4896 (void) nvlist_lookup_uint64_array(nvroot,
4897 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
4899 print_scan_status(ps);
4900 print_checkpoint_scan_warning(ps, pcs);
4901 print_removal_status(zhp, prs);
4902 print_checkpoint_status(pcs);
4904 namewidth = max_width(zhp, nvroot, 0, 0);
4908 (void) printf(gettext("config:\n\n"));
4909 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4910 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4911 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4912 namewidth, 0, B_FALSE);
4914 if (num_logs(nvroot) > 0)
4915 print_logs(zhp, nvroot, namewidth, B_TRUE);
4916 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4917 &l2cache, &nl2cache) == 0)
4918 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4920 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4921 &spares, &nspares) == 0)
4922 print_spares(zhp, spares, nspares, namewidth);
4924 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4926 nvlist_t *nverrlist = NULL;
4929 * If the approximate error count is small, get a
4930 * precise count by fetching the entire log and
4931 * uniquifying the results.
4933 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4934 zpool_get_errlog(zhp, &nverrlist) == 0) {
4939 while ((elem = nvlist_next_nvpair(nverrlist,
4944 nvlist_free(nverrlist);
4946 (void) printf("\n");
4949 (void) printf(gettext("errors: No known data "
4951 else if (!cbp->cb_verbose)
4952 (void) printf(gettext("errors: %llu data "
4953 "errors, use '-v' for a list\n"),
4954 (u_longlong_t)nerr);
4956 print_error_log(zhp);
4959 if (cbp->cb_dedup_stats)
4960 print_dedup_stats(config);
4962 (void) printf(gettext("config: The configuration cannot be "
4970 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4972 * -v Display complete error logs
4973 * -x Display only pools with potential problems
4974 * -D Display dedup status (undocumented)
4975 * -T Display a timestamp in date(1) or Unix format
4977 * Describes the health status of all pools or some subset.
4980 zpool_do_status(int argc, char **argv)
4984 unsigned long interval = 0, count = 0;
4985 status_cbdata_t cb = { 0 };
4988 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4991 cb.cb_verbose = B_TRUE;
4994 cb.cb_explain = B_TRUE;
4997 cb.cb_dedup_stats = B_TRUE;
5000 get_timestamp_arg(*optarg);
5003 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5012 get_interval_count(&argc, argv, &interval, &count);
5015 cb.cb_allpools = B_TRUE;
5017 cb.cb_first = B_TRUE;
5020 if (timestamp_fmt != NODATE)
5021 print_timestamp(timestamp_fmt);
5023 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5024 status_callback, &cb);
5026 if (argc == 0 && cb.cb_count == 0)
5027 (void) printf(gettext("no pools available\n"));
5028 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
5029 (void) printf(gettext("all pools are healthy\n"));
5037 if (count != 0 && --count == 0)
5040 (void) sleep(interval);
5046 typedef struct upgrade_cbdata {
5048 boolean_t cb_unavail;
5049 char cb_poolname[ZFS_MAX_DATASET_NAME_LEN];
5051 uint64_t cb_version;
5057 is_root_pool(zpool_handle_t *zhp)
5059 static struct statfs sfs;
5060 static char *poolname = NULL;
5061 static boolean_t stated = B_FALSE;
5066 if (statfs("/", &sfs) == -1) {
5067 (void) fprintf(stderr,
5068 "Unable to stat root file system: %s.\n",
5072 if (strcmp(sfs.f_fstypename, "zfs") != 0)
5074 poolname = sfs.f_mntfromname;
5075 if ((slash = strchr(poolname, '/')) != NULL)
5078 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
5082 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
5085 if (poolname[0] == '\0' && is_root_pool(zhp))
5086 (void) strlcpy(poolname, zpool_get_name(zhp), size);
5088 #endif /* FreeBSD */
5091 upgrade_version(zpool_handle_t *zhp, uint64_t version)
5095 uint64_t oldversion;
5097 config = zpool_get_config(zhp, NULL);
5098 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5101 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
5102 assert(oldversion < version);
5104 ret = zpool_upgrade(zhp, version);
5108 if (version >= SPA_VERSION_FEATURES) {
5109 (void) printf(gettext("Successfully upgraded "
5110 "'%s' from version %llu to feature flags.\n"),
5111 zpool_get_name(zhp), oldversion);
5113 (void) printf(gettext("Successfully upgraded "
5114 "'%s' from version %llu to version %llu.\n"),
5115 zpool_get_name(zhp), oldversion, version);
5122 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
5125 boolean_t firstff = B_TRUE;
5126 nvlist_t *enabled = zpool_get_features(zhp);
5129 for (i = 0; i < SPA_FEATURES; i++) {
5130 const char *fname = spa_feature_table[i].fi_uname;
5131 const char *fguid = spa_feature_table[i].fi_guid;
5132 if (!nvlist_exists(enabled, fguid)) {
5134 verify(-1 != asprintf(&propname, "feature@%s", fname));
5135 ret = zpool_set_prop(zhp, propname,
5136 ZFS_FEATURE_ENABLED);
5144 (void) printf(gettext("Enabled the "
5145 "following features on '%s':\n"),
5146 zpool_get_name(zhp));
5149 (void) printf(gettext(" %s\n"), fname);
5160 upgrade_cb(zpool_handle_t *zhp, void *arg)
5162 upgrade_cbdata_t *cbp = arg;
5165 boolean_t printnl = B_FALSE;
5168 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5169 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5170 "currently unavailable.\n\n"), zpool_get_name(zhp));
5171 cbp->cb_unavail = B_TRUE;
5172 /* Allow iteration to continue. */
5176 config = zpool_get_config(zhp, NULL);
5177 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5180 assert(SPA_VERSION_IS_SUPPORTED(version));
5182 if (version < cbp->cb_version) {
5183 cbp->cb_first = B_FALSE;
5184 ret = upgrade_version(zhp, cbp->cb_version);
5188 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5189 sizeof(cbp->cb_poolname));
5190 #endif /* __FreeBSD__ */
5195 * If they did "zpool upgrade -a", then we could
5196 * be doing ioctls to different pools. We need
5197 * to log this history once to each pool, and bypass
5198 * the normal history logging that happens in main().
5200 (void) zpool_log_history(g_zfs, history_str);
5201 log_history = B_FALSE;
5205 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5207 ret = upgrade_enable_all(zhp, &count);
5212 cbp->cb_first = B_FALSE;
5215 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5216 sizeof(cbp->cb_poolname));
5217 #endif /* __FreeBSD__ */
5219 * If they did "zpool upgrade -a", then we could
5220 * be doing ioctls to different pools. We need
5221 * to log this history once to each pool, and bypass
5222 * the normal history logging that happens in main().
5224 (void) zpool_log_history(g_zfs, history_str);
5225 log_history = B_FALSE;
5230 (void) printf(gettext("\n"));
5237 upgrade_list_unavail(zpool_handle_t *zhp, void *arg)
5239 upgrade_cbdata_t *cbp = arg;
5241 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5242 if (cbp->cb_first) {
5243 (void) fprintf(stderr, gettext("The following pools "
5244 "are unavailable and cannot be upgraded as this "
5246 (void) fprintf(stderr, gettext("POOL\n"));
5247 (void) fprintf(stderr, gettext("------------\n"));
5248 cbp->cb_first = B_FALSE;
5250 (void) printf(gettext("%s\n"), zpool_get_name(zhp));
5251 cbp->cb_unavail = B_TRUE;
5257 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
5259 upgrade_cbdata_t *cbp = arg;
5263 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5265 * This will have been reported by upgrade_list_unavail so
5266 * just allow iteration to continue.
5268 cbp->cb_unavail = B_TRUE;
5272 config = zpool_get_config(zhp, NULL);
5273 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5276 assert(SPA_VERSION_IS_SUPPORTED(version));
5278 if (version < SPA_VERSION_FEATURES) {
5279 if (cbp->cb_first) {
5280 (void) printf(gettext("The following pools are "
5281 "formatted with legacy version numbers and can\n"
5282 "be upgraded to use feature flags. After "
5283 "being upgraded, these pools\nwill no "
5284 "longer be accessible by software that does not "
5285 "support feature\nflags.\n\n"));
5286 (void) printf(gettext("VER POOL\n"));
5287 (void) printf(gettext("--- ------------\n"));
5288 cbp->cb_first = B_FALSE;
5291 (void) printf("%2llu %s\n", (u_longlong_t)version,
5292 zpool_get_name(zhp));
5299 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
5301 upgrade_cbdata_t *cbp = arg;
5305 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5307 * This will have been reported by upgrade_list_unavail so
5308 * just allow iteration to continue.
5310 cbp->cb_unavail = B_TRUE;
5314 config = zpool_get_config(zhp, NULL);
5315 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5318 if (version >= SPA_VERSION_FEATURES) {
5320 boolean_t poolfirst = B_TRUE;
5321 nvlist_t *enabled = zpool_get_features(zhp);
5323 for (i = 0; i < SPA_FEATURES; i++) {
5324 const char *fguid = spa_feature_table[i].fi_guid;
5325 const char *fname = spa_feature_table[i].fi_uname;
5326 if (!nvlist_exists(enabled, fguid)) {
5327 if (cbp->cb_first) {
5328 (void) printf(gettext("\nSome "
5329 "supported features are not "
5330 "enabled on the following pools. "
5331 "Once a\nfeature is enabled the "
5332 "pool may become incompatible with "
5333 "software\nthat does not support "
5335 "zpool-features(7) for "
5337 (void) printf(gettext("POOL "
5339 (void) printf(gettext("------"
5341 cbp->cb_first = B_FALSE;
5345 (void) printf(gettext("%s\n"),
5346 zpool_get_name(zhp));
5347 poolfirst = B_FALSE;
5350 (void) printf(gettext(" %s\n"), fname);
5360 upgrade_one(zpool_handle_t *zhp, void *data)
5362 boolean_t printnl = B_FALSE;
5363 upgrade_cbdata_t *cbp = data;
5364 uint64_t cur_version;
5367 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5368 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5369 "is currently unavailable.\n\n"), zpool_get_name(zhp));
5370 cbp->cb_unavail = B_TRUE;
5374 if (strcmp("log", zpool_get_name(zhp)) == 0) {
5375 (void) printf(gettext("'log' is now a reserved word\n"
5376 "Pool 'log' must be renamed using export and import"
5377 " to upgrade.\n\n"));
5381 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5382 if (cur_version > cbp->cb_version) {
5383 (void) printf(gettext("Pool '%s' is already formatted "
5384 "using more current version '%llu'.\n\n"),
5385 zpool_get_name(zhp), cur_version);
5389 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
5390 (void) printf(gettext("Pool '%s' is already formatted "
5391 "using version %llu.\n\n"), zpool_get_name(zhp),
5396 if (cur_version != cbp->cb_version) {
5398 ret = upgrade_version(zhp, cbp->cb_version);
5402 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5403 sizeof(cbp->cb_poolname));
5404 #endif /* __FreeBSD__ */
5407 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5409 ret = upgrade_enable_all(zhp, &count);
5416 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5417 sizeof(cbp->cb_poolname));
5418 #endif /* __FreeBSD __*/
5419 } else if (cur_version == SPA_VERSION) {
5420 (void) printf(gettext("Pool '%s' already has all "
5421 "supported features enabled.\n\n"),
5422 zpool_get_name(zhp));
5427 (void) printf(gettext("\n"));
5436 * zpool upgrade [-V version] <-a | pool ...>
5438 * With no arguments, display downrev'd ZFS pool available for upgrade.
5439 * Individual pools can be upgraded by specifying the pool, and '-a' will
5440 * upgrade all pools.
5443 zpool_do_upgrade(int argc, char **argv)
5446 upgrade_cbdata_t cb = { 0 };
5448 boolean_t showversions = B_FALSE;
5449 boolean_t upgradeall = B_FALSE;
5454 while ((c = getopt(argc, argv, ":avV:")) != -1) {
5457 upgradeall = B_TRUE;
5460 showversions = B_TRUE;
5463 cb.cb_version = strtoll(optarg, &end, 10);
5465 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5466 (void) fprintf(stderr,
5467 gettext("invalid version '%s'\n"), optarg);
5472 (void) fprintf(stderr, gettext("missing argument for "
5473 "'%c' option\n"), optopt);
5477 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5488 if (cb.cb_version == 0) {
5489 cb.cb_version = SPA_VERSION;
5490 } else if (!upgradeall && argc == 0) {
5491 (void) fprintf(stderr, gettext("-V option is "
5492 "incompatible with other arguments\n"));
5497 if (upgradeall || argc != 0) {
5498 (void) fprintf(stderr, gettext("-v option is "
5499 "incompatible with other arguments\n"));
5502 } else if (upgradeall) {
5504 (void) fprintf(stderr, gettext("-a option should not "
5505 "be used along with a pool name\n"));
5510 (void) printf(gettext("This system supports ZFS pool feature "
5515 (void) printf(gettext("The following features are "
5517 (void) printf(gettext("FEAT DESCRIPTION\n"));
5518 (void) printf("----------------------------------------------"
5519 "---------------\n");
5520 for (i = 0; i < SPA_FEATURES; i++) {
5521 zfeature_info_t *fi = &spa_feature_table[i];
5523 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
5524 " (read-only compatible)" : "";
5526 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5527 (void) printf(" %s\n", fi->fi_desc);
5529 (void) printf("\n");
5531 (void) printf(gettext("The following legacy versions are also "
5533 (void) printf(gettext("VER DESCRIPTION\n"));
5534 (void) printf("--- -----------------------------------------"
5535 "---------------\n");
5536 (void) printf(gettext(" 1 Initial ZFS version\n"));
5537 (void) printf(gettext(" 2 Ditto blocks "
5538 "(replicated metadata)\n"));
5539 (void) printf(gettext(" 3 Hot spares and double parity "
5541 (void) printf(gettext(" 4 zpool history\n"));
5542 (void) printf(gettext(" 5 Compression using the gzip "
5544 (void) printf(gettext(" 6 bootfs pool property\n"));
5545 (void) printf(gettext(" 7 Separate intent log devices\n"));
5546 (void) printf(gettext(" 8 Delegated administration\n"));
5547 (void) printf(gettext(" 9 refquota and refreservation "
5549 (void) printf(gettext(" 10 Cache devices\n"));
5550 (void) printf(gettext(" 11 Improved scrub performance\n"));
5551 (void) printf(gettext(" 12 Snapshot properties\n"));
5552 (void) printf(gettext(" 13 snapused property\n"));
5553 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5554 (void) printf(gettext(" 15 user/group space accounting\n"));
5555 (void) printf(gettext(" 16 stmf property support\n"));
5556 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5557 (void) printf(gettext(" 18 Snapshot user holds\n"));
5558 (void) printf(gettext(" 19 Log device removal\n"));
5559 (void) printf(gettext(" 20 Compression using zle "
5560 "(zero-length encoding)\n"));
5561 (void) printf(gettext(" 21 Deduplication\n"));
5562 (void) printf(gettext(" 22 Received properties\n"));
5563 (void) printf(gettext(" 23 Slim ZIL\n"));
5564 (void) printf(gettext(" 24 System attributes\n"));
5565 (void) printf(gettext(" 25 Improved scrub stats\n"));
5566 (void) printf(gettext(" 26 Improved snapshot deletion "
5568 (void) printf(gettext(" 27 Improved snapshot creation "
5570 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5571 (void) printf(gettext("\nFor more information on a particular "
5572 "version, including supported releases,\n"));
5573 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5574 } else if (argc == 0 && upgradeall) {
5575 cb.cb_first = B_TRUE;
5576 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5577 if (ret == 0 && cb.cb_first) {
5578 if (cb.cb_version == SPA_VERSION) {
5579 (void) printf(gettext("All %spools are already "
5580 "formatted using feature flags.\n\n"),
5581 cb.cb_unavail ? gettext("available ") : "");
5582 (void) printf(gettext("Every %sfeature flags "
5583 "pool already has all supported features "
5585 cb.cb_unavail ? gettext("available ") : "");
5587 (void) printf(gettext("All pools are already "
5588 "formatted with version %llu or higher.\n"),
5592 } else if (argc == 0) {
5593 cb.cb_first = B_TRUE;
5594 ret = zpool_iter(g_zfs, upgrade_list_unavail, &cb);
5598 (void) fprintf(stderr, "\n");
5601 cb.cb_first = B_TRUE;
5602 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5606 (void) printf(gettext("All %spools are formatted using "
5607 "feature flags.\n\n"), cb.cb_unavail ?
5608 gettext("available ") : "");
5610 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5611 "for a list of available legacy versions.\n"));
5614 cb.cb_first = B_TRUE;
5615 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5619 (void) printf(gettext("Every %sfeature flags pool has "
5620 "all supported features enabled.\n"),
5621 cb.cb_unavail ? gettext("available ") : "");
5623 (void) printf(gettext("\n"));
5626 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5630 if (cb.cb_poolname[0] != '\0') {
5632 "If you boot from pool '%s', don't forget to update boot code.\n"
5633 "Assuming you use GPT partitioning and da0 is your boot disk\n"
5634 "the following command will do it:\n"
5636 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5643 typedef struct hist_cbdata {
5650 * Print out the command history for a specific pool.
5653 get_history_one(zpool_handle_t *zhp, void *data)
5659 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5661 cb->first = B_FALSE;
5663 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5665 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5668 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5669 &records, &numrecords) == 0);
5670 for (i = 0; i < numrecords; i++) {
5671 nvlist_t *rec = records[i];
5674 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5678 tsec = fnvlist_lookup_uint64(records[i],
5680 (void) localtime_r(&tsec, &t);
5681 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5684 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5685 (void) printf("%s %s", tbuf,
5686 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5687 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5689 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5692 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5693 (void) printf("%s unrecognized record:\n",
5695 dump_nvlist(rec, 4);
5698 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5699 zfs_history_event_names[ievent],
5700 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5701 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5702 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5705 (void) printf("%s [txg:%lld] %s", tbuf,
5706 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5707 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5708 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5709 (void) printf(" %s (%llu)",
5710 fnvlist_lookup_string(rec,
5712 fnvlist_lookup_uint64(rec,
5715 (void) printf(" %s", fnvlist_lookup_string(rec,
5716 ZPOOL_HIST_INT_STR));
5717 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5720 (void) printf("%s ioctl %s\n", tbuf,
5721 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5722 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5723 (void) printf(" input:\n");
5724 dump_nvlist(fnvlist_lookup_nvlist(rec,
5725 ZPOOL_HIST_INPUT_NVL), 8);
5727 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5728 (void) printf(" output:\n");
5729 dump_nvlist(fnvlist_lookup_nvlist(rec,
5730 ZPOOL_HIST_OUTPUT_NVL), 8);
5732 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
5733 (void) printf(" errno: %lld\n",
5734 fnvlist_lookup_int64(rec,
5740 (void) printf("%s unrecognized record:\n", tbuf);
5741 dump_nvlist(rec, 4);
5745 (void) printf("\n");
5748 (void) printf(" [");
5749 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5750 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5751 struct passwd *pwd = getpwuid(who);
5752 (void) printf("user %d ", (int)who);
5754 (void) printf("(%s) ", pwd->pw_name);
5756 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5757 (void) printf("on %s",
5758 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5760 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5761 (void) printf(":%s",
5762 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5765 (void) printf("\n");
5767 (void) printf("\n");
5774 * zpool history <pool>
5776 * Displays the history of commands that modified pools.
5779 zpool_do_history(int argc, char **argv)
5781 hist_cbdata_t cbdata = { 0 };
5785 cbdata.first = B_TRUE;
5787 while ((c = getopt(argc, argv, "li")) != -1) {
5790 cbdata.longfmt = B_TRUE;
5793 cbdata.internal = B_TRUE;
5796 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5804 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5807 if (argc == 0 && cbdata.first == B_TRUE) {
5808 (void) printf(gettext("no pools available\n"));
5816 get_callback(zpool_handle_t *zhp, void *data)
5818 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5819 char value[MAXNAMELEN];
5820 zprop_source_t srctype;
5823 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5826 * Skip the special fake placeholder. This will also skip
5827 * over the name property when 'all' is specified.
5829 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5830 pl == cbp->cb_proplist)
5833 if (pl->pl_prop == ZPROP_INVAL &&
5834 (zpool_prop_feature(pl->pl_user_prop) ||
5835 zpool_prop_unsupported(pl->pl_user_prop))) {
5836 srctype = ZPROP_SRC_LOCAL;
5838 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5839 value, sizeof (value)) == 0) {
5840 zprop_print_one_property(zpool_get_name(zhp),
5841 cbp, pl->pl_user_prop, value, srctype,
5845 if (zpool_get_prop(zhp, pl->pl_prop, value,
5846 sizeof (value), &srctype, cbp->cb_literal) != 0)
5849 zprop_print_one_property(zpool_get_name(zhp), cbp,
5850 zpool_prop_to_name(pl->pl_prop), value, srctype,
5858 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5860 * -H Scripted mode. Don't display headers, and separate properties
5862 * -o List of columns to display. Defaults to
5863 * "name,property,value,source".
5864 * -p Diplay values in parsable (exact) format.
5866 * Get properties of pools in the system. Output space statistics
5867 * for each one as well as other attributes.
5870 zpool_do_get(int argc, char **argv)
5872 zprop_get_cbdata_t cb = { 0 };
5873 zprop_list_t fake_name = { 0 };
5878 cb.cb_first = B_TRUE;
5881 * Set up default columns and sources.
5883 cb.cb_sources = ZPROP_SRC_ALL;
5884 cb.cb_columns[0] = GET_COL_NAME;
5885 cb.cb_columns[1] = GET_COL_PROPERTY;
5886 cb.cb_columns[2] = GET_COL_VALUE;
5887 cb.cb_columns[3] = GET_COL_SOURCE;
5888 cb.cb_type = ZFS_TYPE_POOL;
5891 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5894 cb.cb_literal = B_TRUE;
5897 cb.cb_scripted = B_TRUE;
5900 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5902 while (*optarg != '\0') {
5903 static char *col_subopts[] =
5904 { "name", "property", "value", "source",
5907 if (i == ZFS_GET_NCOLS) {
5908 (void) fprintf(stderr, gettext("too "
5909 "many fields given to -o "
5914 switch (getsubopt(&optarg, col_subopts,
5917 cb.cb_columns[i++] = GET_COL_NAME;
5920 cb.cb_columns[i++] = GET_COL_PROPERTY;
5923 cb.cb_columns[i++] = GET_COL_VALUE;
5926 cb.cb_columns[i++] = GET_COL_SOURCE;
5930 (void) fprintf(stderr,
5931 gettext("\"all\" conflicts "
5932 "with specific fields "
5933 "given to -o option\n"));
5936 cb.cb_columns[0] = GET_COL_NAME;
5937 cb.cb_columns[1] = GET_COL_PROPERTY;
5938 cb.cb_columns[2] = GET_COL_VALUE;
5939 cb.cb_columns[3] = GET_COL_SOURCE;
5943 (void) fprintf(stderr,
5944 gettext("invalid column name "
5945 "'%s'\n"), suboptarg);
5951 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5961 (void) fprintf(stderr, gettext("missing property "
5966 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5967 ZFS_TYPE_POOL) != 0)
5973 if (cb.cb_proplist != NULL) {
5974 fake_name.pl_prop = ZPOOL_PROP_NAME;
5975 fake_name.pl_width = strlen(gettext("NAME"));
5976 fake_name.pl_next = cb.cb_proplist;
5977 cb.cb_proplist = &fake_name;
5980 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5983 if (cb.cb_proplist == &fake_name)
5984 zprop_free_list(fake_name.pl_next);
5986 zprop_free_list(cb.cb_proplist);
5991 typedef struct set_cbdata {
5994 boolean_t cb_any_successful;
5998 set_callback(zpool_handle_t *zhp, void *data)
6001 set_cbdata_t *cb = (set_cbdata_t *)data;
6003 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
6006 cb->cb_any_successful = B_TRUE;
6012 zpool_do_set(int argc, char **argv)
6014 set_cbdata_t cb = { 0 };
6017 if (argc > 1 && argv[1][0] == '-') {
6018 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6024 (void) fprintf(stderr, gettext("missing property=value "
6030 (void) fprintf(stderr, gettext("missing pool name\n"));
6035 (void) fprintf(stderr, gettext("too many pool names\n"));
6039 cb.cb_propname = argv[1];
6040 cb.cb_value = strchr(cb.cb_propname, '=');
6041 if (cb.cb_value == NULL) {
6042 (void) fprintf(stderr, gettext("missing value in "
6043 "property=value argument\n"));
6047 *(cb.cb_value) = '\0';
6050 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
6057 find_command_idx(char *command, int *idx)
6061 for (i = 0; i < NCOMMAND; i++) {
6062 if (command_table[i].name == NULL)
6065 if (strcmp(command, command_table[i].name) == 0) {
6074 main(int argc, char **argv)
6080 (void) setlocale(LC_ALL, "");
6081 (void) textdomain(TEXT_DOMAIN);
6083 if ((g_zfs = libzfs_init()) == NULL) {
6084 (void) fprintf(stderr, gettext("internal error: failed to "
6085 "initialize ZFS library\n"));
6089 libzfs_print_on_error(g_zfs, B_TRUE);
6094 * Make sure the user has specified some command.
6097 (void) fprintf(stderr, gettext("missing command\n"));
6106 if (strcmp(cmdname, "-?") == 0)
6109 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6112 * Run the appropriate command.
6114 if (find_command_idx(cmdname, &i) == 0) {
6115 current_command = &command_table[i];
6116 ret = command_table[i].func(argc - 1, argv + 1);
6117 } else if (strchr(cmdname, '=')) {
6118 verify(find_command_idx("set", &i) == 0);
6119 current_command = &command_table[i];
6120 ret = command_table[i].func(argc, argv);
6121 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
6123 * 'freeze' is a vile debugging abomination, so we treat
6126 zfs_cmd_t zc = { 0 };
6127 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
6128 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
6130 (void) fprintf(stderr, gettext("unrecognized "
6131 "command '%s'\n"), cmdname);
6135 if (ret == 0 && log_history)
6136 (void) zpool_log_history(g_zfs, history_str);
6141 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6142 * for the purposes of running ::findleaks.
6144 if (getenv("ZFS_ABORT") != NULL) {
6145 (void) printf("dumping core by request\n");