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_LOAD_REQUEST_TXG, txg) != 0 ||
2361 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
2362 rewind_policy) != 0)
2365 if (searchdirs == NULL) {
2366 searchdirs = safe_malloc(sizeof (char *));
2367 searchdirs[0] = "/dev";
2371 /* check argument count */
2374 (void) fprintf(stderr, gettext("too many arguments\n"));
2379 (void) fprintf(stderr, gettext("too many arguments\n"));
2384 * Check for the SYS_CONFIG privilege. We do this explicitly
2385 * here because otherwise any attempt to discover pools will
2388 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2389 (void) fprintf(stderr, gettext("cannot "
2390 "discover pools: permission denied\n"));
2392 nvlist_free(policy);
2398 * Depending on the arguments given, we do one of the following:
2400 * <none> Iterate through all pools and display information about
2403 * -a Iterate through all pools and try to import each one.
2405 * <id> Find the pool that corresponds to the given GUID/pool
2406 * name and import that one.
2408 * -D Above options applies only to destroyed pools.
2414 searchguid = strtoull(argv[0], &endptr, 10);
2415 if (errno != 0 || *endptr != '\0') {
2416 searchname = argv[0];
2419 found_config = NULL;
2422 * User specified a name or guid. Ensure it's unique.
2424 idata.unique = B_TRUE;
2428 idata.path = searchdirs;
2429 idata.paths = nsearch;
2430 idata.poolname = searchname;
2431 idata.guid = searchguid;
2432 idata.cachefile = cachefile;
2433 idata.policy = policy;
2435 pools = zpool_search_import(g_zfs, &idata);
2437 if (pools != NULL && idata.exists &&
2438 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2439 (void) fprintf(stderr, gettext("cannot import '%s': "
2440 "a pool with that name already exists\n"),
2442 (void) fprintf(stderr, gettext("use the form '%s "
2443 "<pool | id> <newpool>' to give it a new name\n"),
2446 } else if (pools == NULL && idata.exists) {
2447 (void) fprintf(stderr, gettext("cannot import '%s': "
2448 "a pool with that name is already created/imported,\n"),
2450 (void) fprintf(stderr, gettext("and no additional pools "
2451 "with that name were found\n"));
2453 } else if (pools == NULL) {
2455 (void) fprintf(stderr, gettext("cannot import '%s': "
2456 "no such pool available\n"), argv[0]);
2463 nvlist_free(policy);
2468 * At this point we have a list of import candidate configs. Even if
2469 * we were searching by pool name or guid, we still need to
2470 * post-process the list to deal with pool state and possible
2476 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2478 verify(nvpair_value_nvlist(elem, &config) == 0);
2480 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2482 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2484 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2487 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
2494 (void) printf("\n");
2497 err |= do_import(config, NULL, mntopts,
2500 show_import(config);
2502 } else if (searchname != NULL) {
2506 * We are searching for a pool based on name.
2508 verify(nvlist_lookup_string(config,
2509 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2511 if (strcmp(name, searchname) == 0) {
2512 if (found_config != NULL) {
2513 (void) fprintf(stderr, gettext(
2514 "cannot import '%s': more than "
2515 "one matching pool\n"), searchname);
2516 (void) fprintf(stderr, gettext(
2517 "import by numeric ID instead\n"));
2520 found_config = config;
2526 * Search for a pool by guid.
2528 verify(nvlist_lookup_uint64(config,
2529 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2531 if (guid == searchguid)
2532 found_config = config;
2537 * If we were searching for a specific pool, verify that we found a
2538 * pool, and then do the import.
2540 if (argc != 0 && err == 0) {
2541 if (found_config == NULL) {
2542 (void) fprintf(stderr, gettext("cannot import '%s': "
2543 "no such pool available\n"), argv[0]);
2546 err |= do_import(found_config, argc == 1 ? NULL :
2547 argv[1], mntopts, props, flags);
2552 * If we were just looking for pools, report an error if none were
2555 if (argc == 0 && first)
2556 (void) fprintf(stderr,
2557 gettext("no pools available to import\n"));
2562 nvlist_free(policy);
2565 return (err ? 1 : 0);
2568 typedef struct iostat_cbdata {
2569 boolean_t cb_verbose;
2572 zpool_list_t *cb_list;
2576 print_iostat_separator(iostat_cbdata_t *cb)
2580 for (i = 0; i < cb->cb_namewidth; i++)
2582 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2586 print_iostat_header(iostat_cbdata_t *cb)
2588 (void) printf("%*s capacity operations bandwidth\n",
2589 cb->cb_namewidth, "");
2590 (void) printf("%-*s alloc free read write read write\n",
2591 cb->cb_namewidth, "pool");
2592 print_iostat_separator(cb);
2596 * Display a single statistic.
2599 print_one_stat(uint64_t value)
2603 zfs_nicenum(value, buf, sizeof (buf));
2604 (void) printf(" %5s", buf);
2608 * Print out all the statistics for the given vdev. This can either be the
2609 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2610 * is a verbose output, and we don't want to display the toplevel pool stats.
2613 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2614 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2616 nvlist_t **oldchild, **newchild;
2618 vdev_stat_t *oldvs, *newvs;
2619 vdev_stat_t zerovs = { 0 };
2624 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
2627 if (oldnv != NULL) {
2628 verify(nvlist_lookup_uint64_array(oldnv,
2629 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2634 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2635 (uint64_t **)&newvs, &c) == 0);
2637 if (strlen(name) + depth > cb->cb_namewidth)
2638 (void) printf("%*s%s", depth, "", name);
2640 (void) printf("%*s%s%*s", depth, "", name,
2641 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2643 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2648 scale = (double)NANOSEC / tdelta;
2650 /* only toplevel vdevs have capacity stats */
2651 if (newvs->vs_space == 0) {
2652 (void) printf(" - -");
2654 print_one_stat(newvs->vs_alloc);
2655 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2658 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2659 oldvs->vs_ops[ZIO_TYPE_READ])));
2661 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2662 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2664 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2665 oldvs->vs_bytes[ZIO_TYPE_READ])));
2667 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2668 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2670 (void) printf("\n");
2672 if (!cb->cb_verbose)
2675 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2676 &newchild, &children) != 0)
2679 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2680 &oldchild, &c) != 0)
2683 for (c = 0; c < children; c++) {
2684 uint64_t ishole = B_FALSE, islog = B_FALSE;
2686 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2689 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2692 if (ishole || islog)
2695 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2696 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2697 newchild[c], cb, depth + 2);
2702 * Log device section
2705 if (num_logs(newnv) > 0) {
2706 (void) printf("%-*s - - - - - "
2707 "-\n", cb->cb_namewidth, "logs");
2709 for (c = 0; c < children; c++) {
2710 uint64_t islog = B_FALSE;
2711 (void) nvlist_lookup_uint64(newchild[c],
2712 ZPOOL_CONFIG_IS_LOG, &islog);
2715 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2717 print_vdev_stats(zhp, vname, oldnv ?
2718 oldchild[c] : NULL, newchild[c],
2727 * Include level 2 ARC devices in iostat output
2729 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2730 &newchild, &children) != 0)
2733 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2734 &oldchild, &c) != 0)
2738 (void) printf("%-*s - - - - - "
2739 "-\n", cb->cb_namewidth, "cache");
2740 for (c = 0; c < children; c++) {
2741 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2743 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2744 newchild[c], cb, depth + 2);
2751 refresh_iostat(zpool_handle_t *zhp, void *data)
2753 iostat_cbdata_t *cb = data;
2757 * If the pool has disappeared, remove it from the list and continue.
2759 if (zpool_refresh_stats(zhp, &missing) != 0)
2763 pool_list_remove(cb->cb_list, zhp);
2769 * Callback to print out the iostats for the given pool.
2772 print_iostat(zpool_handle_t *zhp, void *data)
2774 iostat_cbdata_t *cb = data;
2775 nvlist_t *oldconfig, *newconfig;
2776 nvlist_t *oldnvroot, *newnvroot;
2778 newconfig = zpool_get_config(zhp, &oldconfig);
2780 if (cb->cb_iteration == 1)
2783 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2786 if (oldconfig == NULL)
2789 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2793 * Print out the statistics for the pool.
2795 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2798 print_iostat_separator(cb);
2804 get_namewidth(zpool_handle_t *zhp, void *data)
2806 iostat_cbdata_t *cb = data;
2807 nvlist_t *config, *nvroot;
2809 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2810 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2812 if (!cb->cb_verbose)
2813 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2815 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2820 * The width must fall into the range [10,38]. The upper limit is the
2821 * maximum we can have and still fit in 80 columns.
2823 if (cb->cb_namewidth < 10)
2824 cb->cb_namewidth = 10;
2825 if (cb->cb_namewidth > 38)
2826 cb->cb_namewidth = 38;
2832 * Parse the input string, get the 'interval' and 'count' value if there is one.
2835 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2838 unsigned long interval = 0, count = 0;
2839 int argc = *argcp, errno;
2842 * Determine if the last argument is an integer or a pool name
2844 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2848 interval = strtoul(argv[argc - 1], &end, 10);
2850 if (*end == '\0' && errno == 0) {
2851 if (interval == 0) {
2852 (void) fprintf(stderr, gettext("interval "
2853 "cannot be zero\n"));
2857 * Ignore the last parameter
2862 * If this is not a valid number, just plow on. The
2863 * user will get a more informative error message later
2871 * If the last argument is also an integer, then we have both a count
2874 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2879 interval = strtoul(argv[argc - 1], &end, 10);
2881 if (*end == '\0' && errno == 0) {
2882 if (interval == 0) {
2883 (void) fprintf(stderr, gettext("interval "
2884 "cannot be zero\n"));
2889 * Ignore the last parameter
2903 get_timestamp_arg(char c)
2906 timestamp_fmt = UDATE;
2908 timestamp_fmt = DDATE;
2914 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2916 * -v Display statistics for individual vdevs
2917 * -T Display a timestamp in date(1) or Unix format
2919 * This command can be tricky because we want to be able to deal with pool
2920 * creation/destruction as well as vdev configuration changes. The bulk of this
2921 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2922 * on pool_list_update() to detect the addition of new pools. Configuration
2923 * changes are all handled within libzfs.
2926 zpool_do_iostat(int argc, char **argv)
2931 unsigned long interval = 0, count = 0;
2933 boolean_t verbose = B_FALSE;
2937 while ((c = getopt(argc, argv, "T:v")) != -1) {
2940 get_timestamp_arg(*optarg);
2946 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2955 get_interval_count(&argc, argv, &interval, &count);
2958 * Construct the list of all interesting pools.
2961 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2964 if (pool_list_count(list) == 0 && argc != 0) {
2965 pool_list_free(list);
2969 if (pool_list_count(list) == 0 && interval == 0) {
2970 pool_list_free(list);
2971 (void) fprintf(stderr, gettext("no pools available\n"));
2976 * Enter the main iostat loop.
2979 cb.cb_verbose = verbose;
2980 cb.cb_iteration = 0;
2981 cb.cb_namewidth = 0;
2984 pool_list_update(list);
2986 if ((npools = pool_list_count(list)) == 0)
2990 * Refresh all statistics. This is done as an explicit step
2991 * before calculating the maximum name width, so that any
2992 * configuration changes are properly accounted for.
2994 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2997 * Iterate over all pools to determine the maximum width
2998 * for the pool / device name column across all pools.
3000 cb.cb_namewidth = 0;
3001 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3003 if (timestamp_fmt != NODATE)
3004 print_timestamp(timestamp_fmt);
3007 * If it's the first time, or verbose mode, print the header.
3009 if (++cb.cb_iteration == 1 || verbose)
3010 print_iostat_header(&cb);
3012 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
3015 * If there's more than one pool, and we're not in verbose mode
3016 * (which prints a separator for us), then print a separator.
3018 if (npools > 1 && !verbose)
3019 print_iostat_separator(&cb);
3022 (void) printf("\n");
3025 * Flush the output so that redirection to a file isn't buffered
3028 (void) fflush(stdout);
3033 if (count != 0 && --count == 0)
3036 (void) sleep(interval);
3039 pool_list_free(list);
3044 typedef struct list_cbdata {
3045 boolean_t cb_verbose;
3047 boolean_t cb_scripted;
3048 zprop_list_t *cb_proplist;
3049 boolean_t cb_literal;
3053 * Given a list of columns to display, output appropriate headers for each one.
3056 print_header(list_cbdata_t *cb)
3058 zprop_list_t *pl = cb->cb_proplist;
3059 char headerbuf[ZPOOL_MAXPROPLEN];
3061 boolean_t first = B_TRUE;
3062 boolean_t right_justify;
3065 for (; pl != NULL; pl = pl->pl_next) {
3066 width = pl->pl_width;
3067 if (first && cb->cb_verbose) {
3069 * Reset the width to accommodate the verbose listing
3072 width = cb->cb_namewidth;
3080 right_justify = B_FALSE;
3081 if (pl->pl_prop != ZPROP_INVAL) {
3082 header = zpool_prop_column_name(pl->pl_prop);
3083 right_justify = zpool_prop_align_right(pl->pl_prop);
3087 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3088 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3089 headerbuf[i] = '\0';
3093 if (pl->pl_next == NULL && !right_justify)
3094 (void) printf("%s", header);
3095 else if (right_justify)
3096 (void) printf("%*s", width, header);
3098 (void) printf("%-*s", width, header);
3102 (void) printf("\n");
3106 * Given a pool and a list of properties, print out all the properties according
3107 * to the described layout.
3110 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
3112 zprop_list_t *pl = cb->cb_proplist;
3113 boolean_t first = B_TRUE;
3114 char property[ZPOOL_MAXPROPLEN];
3116 boolean_t right_justify;
3119 for (; pl != NULL; pl = pl->pl_next) {
3121 width = pl->pl_width;
3122 if (first && cb->cb_verbose) {
3124 * Reset the width to accommodate the verbose listing
3127 width = cb->cb_namewidth;
3131 if (cb->cb_scripted)
3132 (void) printf("\t");
3139 right_justify = B_FALSE;
3140 if (pl->pl_prop != ZPROP_INVAL) {
3141 if (zpool_get_prop(zhp, pl->pl_prop, property,
3142 sizeof (property), NULL, cb->cb_literal) != 0)
3147 right_justify = zpool_prop_align_right(pl->pl_prop);
3148 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
3149 zpool_prop_unsupported(pl->pl_user_prop)) &&
3150 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3151 sizeof (property)) == 0) {
3159 * If this is being called in scripted mode, or if this is the
3160 * last column and it is left-justified, don't include a width
3163 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3164 (void) printf("%s", propstr);
3165 else if (right_justify)
3166 (void) printf("%*s", width, propstr);
3168 (void) printf("%-*s", width, propstr);
3171 (void) printf("\n");
3175 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3180 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3183 case ZPOOL_PROP_EXPANDSZ:
3184 case ZPOOL_PROP_CHECKPOINT:
3186 (void) strlcpy(propval, "-", sizeof (propval));
3188 zfs_nicenum(value, propval, sizeof (propval));
3190 case ZPOOL_PROP_FRAGMENTATION:
3191 if (value == ZFS_FRAG_INVALID) {
3192 (void) strlcpy(propval, "-", sizeof (propval));
3194 (void) snprintf(propval, sizeof (propval), "%llu%%",
3198 case ZPOOL_PROP_CAPACITY:
3199 (void) snprintf(propval, sizeof (propval), "%llu%%", value);
3202 zfs_nicenum(value, propval, sizeof (propval));
3206 (void) strlcpy(propval, "-", sizeof (propval));
3209 (void) printf("\t%s", propval);
3211 (void) printf(" %*s", width, propval);
3215 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3216 list_cbdata_t *cb, int depth)
3222 boolean_t scripted = cb->cb_scripted;
3223 uint64_t islog = B_FALSE;
3224 boolean_t haslog = B_FALSE;
3225 char *dashes = "%-*s - - - - - -\n";
3227 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3228 (uint64_t **)&vs, &c) == 0);
3231 boolean_t toplevel = (vs->vs_space != 0);
3234 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
3238 (void) printf("\t%s", name);
3239 else if (strlen(name) + depth > cb->cb_namewidth)
3240 (void) printf("%*s%s", depth, "", name);
3242 (void) printf("%*s%s%*s", depth, "", name,
3243 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3246 * Print the properties for the individual vdevs. Some
3247 * properties are only applicable to toplevel vdevs. The
3248 * 'toplevel' boolean value is passed to the print_one_column()
3249 * to indicate that the value is valid.
3251 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3253 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3255 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3256 scripted, toplevel);
3257 print_one_column(ZPOOL_PROP_CHECKPOINT,
3258 vs->vs_checkpoint_space, scripted, toplevel);
3259 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3261 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3262 vs->vs_fragmentation, scripted,
3263 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3264 cap = (vs->vs_space == 0) ? 0 :
3265 (vs->vs_alloc * 100 / vs->vs_space);
3266 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3267 (void) printf("\n");
3270 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3271 &child, &children) != 0)
3274 for (c = 0; c < children; c++) {
3275 uint64_t ishole = B_FALSE;
3277 if (nvlist_lookup_uint64(child[c],
3278 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3281 if (nvlist_lookup_uint64(child[c],
3282 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
3287 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3288 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3292 if (haslog == B_TRUE) {
3293 /* LINTED E_SEC_PRINTF_VAR_FMT */
3294 (void) printf(dashes, cb->cb_namewidth, "log");
3295 for (c = 0; c < children; c++) {
3296 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3297 &islog) != 0 || !islog)
3299 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3300 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3305 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3306 &child, &children) == 0 && children > 0) {
3307 /* LINTED E_SEC_PRINTF_VAR_FMT */
3308 (void) printf(dashes, cb->cb_namewidth, "cache");
3309 for (c = 0; c < children; c++) {
3310 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3311 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3316 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
3317 &children) == 0 && children > 0) {
3318 /* LINTED E_SEC_PRINTF_VAR_FMT */
3319 (void) printf(dashes, cb->cb_namewidth, "spare");
3320 for (c = 0; c < children; c++) {
3321 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3322 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3330 * Generic callback function to list a pool.
3333 list_callback(zpool_handle_t *zhp, void *data)
3335 list_cbdata_t *cbp = data;
3339 config = zpool_get_config(zhp, NULL);
3341 print_pool(zhp, cbp);
3342 if (!cbp->cb_verbose)
3345 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3347 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3353 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3355 * -H Scripted mode. Don't display headers, and separate properties
3357 * -o List of properties to display. Defaults to
3358 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3359 * "dedupratio,health,altroot"
3360 * -p Diplay values in parsable (exact) format.
3361 * -T Display a timestamp in date(1) or Unix format
3363 * List all pools in the system, whether or not they're healthy. Output space
3364 * statistics for each one, as well as health status summary.
3367 zpool_do_list(int argc, char **argv)
3371 list_cbdata_t cb = { 0 };
3372 static char default_props[] =
3373 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
3374 "capacity,dedupratio,health,altroot";
3375 char *props = default_props;
3376 unsigned long interval = 0, count = 0;
3378 boolean_t first = B_TRUE;
3381 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3384 cb.cb_scripted = B_TRUE;
3390 cb.cb_literal = B_TRUE;
3393 get_timestamp_arg(*optarg);
3396 cb.cb_verbose = B_TRUE;
3399 (void) fprintf(stderr, gettext("missing argument for "
3400 "'%c' option\n"), optopt);
3404 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3413 get_interval_count(&argc, argv, &interval, &count);
3415 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3419 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3423 if (pool_list_count(list) == 0)
3426 cb.cb_namewidth = 0;
3427 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3429 if (timestamp_fmt != NODATE)
3430 print_timestamp(timestamp_fmt);
3432 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3436 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3441 if (count != 0 && --count == 0)
3444 pool_list_free(list);
3445 (void) sleep(interval);
3448 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3449 (void) printf(gettext("no pools available\n"));
3453 pool_list_free(list);
3454 zprop_free_list(cb.cb_proplist);
3459 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3461 boolean_t force = B_FALSE;
3464 char *poolname, *old_disk, *new_disk;
3465 zpool_handle_t *zhp;
3466 zpool_boot_label_t boot_type;
3471 while ((c = getopt(argc, argv, "f")) != -1) {
3477 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3486 /* get pool name and check number of arguments */
3488 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3495 (void) fprintf(stderr,
3496 gettext("missing <device> specification\n"));
3504 (void) fprintf(stderr,
3505 gettext("missing <new_device> specification\n"));
3508 new_disk = old_disk;
3518 (void) fprintf(stderr, gettext("too many arguments\n"));
3522 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3525 if (zpool_get_config(zhp, NULL) == NULL) {
3526 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3532 if (zpool_is_bootable(zhp))
3533 boot_type = ZPOOL_COPY_BOOT_LABEL;
3535 boot_type = ZPOOL_NO_BOOT_LABEL;
3537 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3538 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3539 boot_type, boot_size, argc, argv);
3540 if (nvroot == NULL) {
3545 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3547 nvlist_free(nvroot);
3554 * zpool replace [-f] <pool> <device> <new_device>
3556 * -f Force attach, even if <new_device> appears to be in use.
3558 * Replace <device> with <new_device>.
3562 zpool_do_replace(int argc, char **argv)
3564 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3568 * zpool attach [-f] <pool> <device> <new_device>
3570 * -f Force attach, even if <new_device> appears to be in use.
3572 * Attach <new_device> to the mirror containing <device>. If <device> is not
3573 * part of a mirror, then <device> will be transformed into a mirror of
3574 * <device> and <new_device>. In either case, <new_device> will begin life
3575 * with a DTL of [0, now], and will immediately begin to resilver itself.
3578 zpool_do_attach(int argc, char **argv)
3580 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3584 * zpool detach [-f] <pool> <device>
3586 * -f Force detach of <device>, even if DTLs argue against it
3587 * (not supported yet)
3589 * Detach a device from a mirror. The operation will be refused if <device>
3590 * is the last device in the mirror, or if the DTLs indicate that this device
3591 * has the only valid copy of some data.
3595 zpool_do_detach(int argc, char **argv)
3598 char *poolname, *path;
3599 zpool_handle_t *zhp;
3603 while ((c = getopt(argc, argv, "f")) != -1) {
3607 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3616 /* get pool name and check number of arguments */
3618 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3623 (void) fprintf(stderr,
3624 gettext("missing <device> specification\n"));
3631 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3634 ret = zpool_vdev_detach(zhp, path);
3642 * zpool split [-n] [-o prop=val] ...
3644 * [-R altroot] <pool> <newpool> [<device> ...]
3646 * -n Do not split the pool, but display the resulting layout if
3647 * it were to be split.
3648 * -o Set property=value, or set mount options.
3649 * -R Mount the split-off pool under an alternate root.
3651 * Splits the named pool and gives it the new pool name. Devices to be split
3652 * off may be listed, provided that no more than one device is specified
3653 * per top-level vdev mirror. The newly split pool is left in an exported
3654 * state unless -R is specified.
3656 * Restrictions: the top-level of the pool pool must only be made up of
3657 * mirrors; all devices in the pool must be healthy; no device may be
3658 * undergoing a resilvering operation.
3661 zpool_do_split(int argc, char **argv)
3663 char *srcpool, *newpool, *propval;
3664 char *mntopts = NULL;
3667 zpool_handle_t *zhp;
3668 nvlist_t *config, *props = NULL;
3670 flags.dryrun = B_FALSE;
3671 flags.import = B_FALSE;
3674 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3677 flags.import = B_TRUE;
3679 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3680 &props, B_TRUE) != 0) {
3686 flags.dryrun = B_TRUE;
3689 if ((propval = strchr(optarg, '=')) != NULL) {
3692 if (add_prop_list(optarg, propval,
3693 &props, B_TRUE) != 0) {
3702 (void) fprintf(stderr, gettext("missing argument for "
3703 "'%c' option\n"), optopt);
3707 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3714 if (!flags.import && mntopts != NULL) {
3715 (void) fprintf(stderr, gettext("setting mntopts is only "
3716 "valid when importing the pool\n"));
3724 (void) fprintf(stderr, gettext("Missing pool name\n"));
3728 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3738 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3741 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3742 if (config == NULL) {
3746 (void) printf(gettext("would create '%s' with the "
3747 "following layout:\n\n"), newpool);
3748 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3750 nvlist_free(config);
3755 if (ret != 0 || flags.dryrun || !flags.import)
3759 * The split was successful. Now we need to open the new
3760 * pool and import it.
3762 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3764 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3765 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3767 (void) fprintf(stderr, gettext("Split was successful, but "
3768 "the datasets could not all be mounted\n"));
3769 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3770 "different altroot\n"), "zpool import");
3780 * zpool online <pool> <device> ...
3783 zpool_do_online(int argc, char **argv)
3787 zpool_handle_t *zhp;
3789 vdev_state_t newstate;
3793 while ((c = getopt(argc, argv, "et")) != -1) {
3796 flags |= ZFS_ONLINE_EXPAND;
3800 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3809 /* get pool name and check number of arguments */
3811 (void) fprintf(stderr, gettext("missing pool name\n"));
3815 (void) fprintf(stderr, gettext("missing device name\n"));
3821 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3824 for (i = 1; i < argc; i++) {
3825 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3826 if (newstate != VDEV_STATE_HEALTHY) {
3827 (void) printf(gettext("warning: device '%s' "
3828 "onlined, but remains in faulted state\n"),
3830 if (newstate == VDEV_STATE_FAULTED)
3831 (void) printf(gettext("use 'zpool "
3832 "clear' to restore a faulted "
3835 (void) printf(gettext("use 'zpool "
3836 "replace' to replace devices "
3837 "that are no longer present\n"));
3850 * zpool offline [-ft] <pool> <device> ...
3852 * -f Force the device into the offline state, even if doing
3853 * so would appear to compromise pool availability.
3854 * (not supported yet)
3856 * -t Only take the device off-line temporarily. The offline
3857 * state will not be persistent across reboots.
3861 zpool_do_offline(int argc, char **argv)
3865 zpool_handle_t *zhp;
3867 boolean_t istmp = B_FALSE;
3870 while ((c = getopt(argc, argv, "ft")) != -1) {
3877 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3886 /* get pool name and check number of arguments */
3888 (void) fprintf(stderr, gettext("missing pool name\n"));
3892 (void) fprintf(stderr, gettext("missing device name\n"));
3898 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3901 for (i = 1; i < argc; i++) {
3902 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3912 * zpool clear <pool> [device]
3914 * Clear all errors associated with a pool or a particular device.
3917 zpool_do_clear(int argc, char **argv)
3921 boolean_t dryrun = B_FALSE;
3922 boolean_t do_rewind = B_FALSE;
3923 boolean_t xtreme_rewind = B_FALSE;
3924 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3925 nvlist_t *policy = NULL;
3926 zpool_handle_t *zhp;
3927 char *pool, *device;
3930 while ((c = getopt(argc, argv, "FnX")) != -1) {
3939 xtreme_rewind = B_TRUE;
3942 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3952 (void) fprintf(stderr, gettext("missing pool name\n"));
3957 (void) fprintf(stderr, gettext("too many arguments\n"));
3961 if ((dryrun || xtreme_rewind) && !do_rewind) {
3962 (void) fprintf(stderr,
3963 gettext("-n or -X only meaningful with -F\n"));
3967 rewind_policy = ZPOOL_TRY_REWIND;
3969 rewind_policy = ZPOOL_DO_REWIND;
3971 rewind_policy |= ZPOOL_EXTREME_REWIND;
3973 /* In future, further rewind policy choices can be passed along here */
3974 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3975 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
3976 rewind_policy) != 0) {
3981 device = argc == 2 ? argv[1] : NULL;
3983 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3984 nvlist_free(policy);
3988 if (zpool_clear(zhp, device, policy) != 0)
3993 nvlist_free(policy);
3999 * zpool reguid <pool>
4002 zpool_do_reguid(int argc, char **argv)
4006 zpool_handle_t *zhp;
4010 while ((c = getopt(argc, argv, "")) != -1) {
4013 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4022 /* get pool name and check number of arguments */
4024 (void) fprintf(stderr, gettext("missing pool name\n"));
4029 (void) fprintf(stderr, gettext("too many arguments\n"));
4034 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4037 ret = zpool_reguid(zhp);
4045 * zpool reopen <pool>
4047 * Reopen the pool so that the kernel can update the sizes of all vdevs.
4050 zpool_do_reopen(int argc, char **argv)
4054 zpool_handle_t *zhp;
4058 while ((c = getopt(argc, argv, "")) != -1) {
4061 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4071 (void) fprintf(stderr, gettext("missing pool name\n"));
4076 (void) fprintf(stderr, gettext("too many arguments\n"));
4081 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
4084 ret = zpool_reopen(zhp);
4089 typedef struct scrub_cbdata {
4093 pool_scrub_cmd_t cb_scrub_cmd;
4097 zpool_has_checkpoint(zpool_handle_t *zhp)
4099 nvlist_t *config, *nvroot;
4101 config = zpool_get_config(zhp, NULL);
4103 if (config != NULL) {
4104 pool_checkpoint_stat_t *pcs = NULL;
4107 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
4108 (void) nvlist_lookup_uint64_array(nvroot,
4109 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
4111 if (pcs == NULL || pcs->pcs_state == CS_NONE)
4114 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
4115 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
4123 scrub_callback(zpool_handle_t *zhp, void *data)
4125 scrub_cbdata_t *cb = data;
4129 * Ignore faulted pools.
4131 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4132 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
4133 "currently unavailable\n"), zpool_get_name(zhp));
4137 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
4139 if (err == 0 && zpool_has_checkpoint(zhp) &&
4140 cb->cb_type == POOL_SCAN_SCRUB) {
4141 (void) printf(gettext("warning: will not scrub state that "
4142 "belongs to the checkpoint of pool '%s'\n"),
4143 zpool_get_name(zhp));
4150 * zpool scrub [-s | -p] <pool> ...
4152 * -s Stop. Stops any in-progress scrub.
4153 * -p Pause. Pause in-progress scrub.
4156 zpool_do_scrub(int argc, char **argv)
4161 cb.cb_type = POOL_SCAN_SCRUB;
4162 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
4165 while ((c = getopt(argc, argv, "sp")) != -1) {
4168 cb.cb_type = POOL_SCAN_NONE;
4171 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
4174 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4180 if (cb.cb_type == POOL_SCAN_NONE &&
4181 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
4182 (void) fprintf(stderr, gettext("invalid option combination: "
4183 "-s and -p are mutually exclusive\n"));
4193 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4197 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4200 typedef struct status_cbdata {
4202 boolean_t cb_allpools;
4203 boolean_t cb_verbose;
4204 boolean_t cb_explain;
4206 boolean_t cb_dedup_stats;
4210 * Print out detailed scrub status.
4213 print_scan_status(pool_scan_stat_t *ps)
4215 time_t start, end, pause;
4216 uint64_t elapsed, mins_left, hours_left;
4217 uint64_t pass_exam, examined, total;
4219 double fraction_done;
4220 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4222 (void) printf(gettext(" scan: "));
4224 /* If there's never been a scan, there's not much to say. */
4225 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
4226 ps->pss_func >= POOL_SCAN_FUNCS) {
4227 (void) printf(gettext("none requested\n"));
4231 start = ps->pss_start_time;
4232 end = ps->pss_end_time;
4233 pause = ps->pss_pass_scrub_pause;
4234 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4236 assert(ps->pss_func == POOL_SCAN_SCRUB ||
4237 ps->pss_func == POOL_SCAN_RESILVER);
4239 * Scan is finished or canceled.
4241 if (ps->pss_state == DSS_FINISHED) {
4242 uint64_t minutes_taken = (end - start) / 60;
4245 if (ps->pss_func == POOL_SCAN_SCRUB) {
4246 fmt = gettext("scrub repaired %s in %lluh%um with "
4247 "%llu errors on %s");
4248 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4249 fmt = gettext("resilvered %s in %lluh%um with "
4250 "%llu errors on %s");
4253 (void) printf(fmt, processed_buf,
4254 (u_longlong_t)(minutes_taken / 60),
4255 (uint_t)(minutes_taken % 60),
4256 (u_longlong_t)ps->pss_errors,
4257 ctime((time_t *)&end));
4259 } else if (ps->pss_state == DSS_CANCELED) {
4260 if (ps->pss_func == POOL_SCAN_SCRUB) {
4261 (void) printf(gettext("scrub canceled on %s"),
4263 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4264 (void) printf(gettext("resilver canceled on %s"),
4270 assert(ps->pss_state == DSS_SCANNING);
4273 * Scan is in progress.
4275 if (ps->pss_func == POOL_SCAN_SCRUB) {
4277 (void) printf(gettext("scrub in progress since %s"),
4281 struct tm *p = localtime(&pause);
4282 (void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
4283 (void) printf(gettext("scrub paused since %s\n"), buf);
4284 (void) printf(gettext("\tscrub started on %s"),
4287 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4288 (void) printf(gettext("resilver in progress since %s"),
4292 examined = ps->pss_examined ? ps->pss_examined : 1;
4293 total = ps->pss_to_examine;
4294 fraction_done = (double)examined / total;
4296 /* elapsed time for this pass */
4297 elapsed = time(NULL) - ps->pss_pass_start;
4298 elapsed -= ps->pss_pass_scrub_spent_paused;
4299 elapsed = elapsed ? elapsed : 1;
4300 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4301 rate = pass_exam / elapsed;
4302 rate = rate ? rate : 1;
4303 mins_left = ((total - examined) / rate) / 60;
4304 hours_left = mins_left / 60;
4306 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4307 zfs_nicenum(total, total_buf, sizeof (total_buf));
4310 * do not print estimated time if hours_left is more than 30 days
4311 * or we have a paused scrub
4314 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4315 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
4316 examined_buf, total_buf, rate_buf);
4317 if (hours_left < (30 * 24)) {
4318 (void) printf(gettext(", %lluh%um to go\n"),
4319 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4321 (void) printf(gettext(
4322 ", (scan is slow, no estimated time)\n"));
4325 (void) printf(gettext("\t%s scanned out of %s\n"),
4326 examined_buf, total_buf);
4329 if (ps->pss_func == POOL_SCAN_RESILVER) {
4330 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4331 processed_buf, 100 * fraction_done);
4332 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4333 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4334 processed_buf, 100 * fraction_done);
4339 * As we don't scrub checkpointed blocks, we want to warn the
4340 * user that we skipped scanning some blocks if a checkpoint exists
4341 * or existed at any time during the scan.
4344 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
4346 if (ps == NULL || pcs == NULL)
4349 if (pcs->pcs_state == CS_NONE ||
4350 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
4353 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
4355 if (ps->pss_state == DSS_NONE)
4358 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
4359 ps->pss_end_time < pcs->pcs_start_time)
4362 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
4363 (void) printf(gettext(" scan warning: skipped blocks "
4364 "that are only referenced by the checkpoint.\n"));
4366 assert(ps->pss_state == DSS_SCANNING);
4367 (void) printf(gettext(" scan warning: skipping blocks "
4368 "that are only referenced by the checkpoint.\n"));
4373 * Print out detailed removal status.
4376 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
4378 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4380 nvlist_t *config, *nvroot;
4385 if (prs == NULL || prs->prs_state == DSS_NONE)
4389 * Determine name of vdev.
4391 config = zpool_get_config(zhp, NULL);
4392 nvroot = fnvlist_lookup_nvlist(config,
4393 ZPOOL_CONFIG_VDEV_TREE);
4394 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4395 &child, &children) == 0);
4396 assert(prs->prs_removing_vdev < children);
4397 vdev_name = zpool_vdev_name(g_zfs, zhp,
4398 child[prs->prs_removing_vdev], B_TRUE);
4400 (void) printf(gettext("remove: "));
4402 start = prs->prs_start_time;
4403 end = prs->prs_end_time;
4404 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
4407 * Removal is finished or canceled.
4409 if (prs->prs_state == DSS_FINISHED) {
4410 uint64_t minutes_taken = (end - start) / 60;
4412 (void) printf(gettext("Removal of vdev %llu copied %s "
4413 "in %lluh%um, completed on %s"),
4414 (longlong_t)prs->prs_removing_vdev,
4416 (u_longlong_t)(minutes_taken / 60),
4417 (uint_t)(minutes_taken % 60),
4418 ctime((time_t *)&end));
4419 } else if (prs->prs_state == DSS_CANCELED) {
4420 (void) printf(gettext("Removal of %s canceled on %s"),
4421 vdev_name, ctime(&end));
4423 uint64_t copied, total, elapsed, mins_left, hours_left;
4424 double fraction_done;
4427 assert(prs->prs_state == DSS_SCANNING);
4430 * Removal is in progress.
4432 (void) printf(gettext(
4433 "Evacuation of %s in progress since %s"),
4434 vdev_name, ctime(&start));
4436 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
4437 total = prs->prs_to_copy;
4438 fraction_done = (double)copied / total;
4440 /* elapsed time for this pass */
4441 elapsed = time(NULL) - prs->prs_start_time;
4442 elapsed = elapsed > 0 ? elapsed : 1;
4443 rate = copied / elapsed;
4444 rate = rate > 0 ? rate : 1;
4445 mins_left = ((total - copied) / rate) / 60;
4446 hours_left = mins_left / 60;
4448 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
4449 zfs_nicenum(total, total_buf, sizeof (total_buf));
4450 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4453 * do not print estimated time if hours_left is more than
4456 (void) printf(gettext(" %s copied out of %s at %s/s, "
4458 examined_buf, total_buf, rate_buf, 100 * fraction_done);
4459 if (hours_left < (30 * 24)) {
4460 (void) printf(gettext(", %lluh%um to go\n"),
4461 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4463 (void) printf(gettext(
4464 ", (copy is slow, no estimated time)\n"));
4468 if (prs->prs_mapping_memory > 0) {
4470 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
4471 (void) printf(gettext(" %s memory used for "
4472 "removed device mappings\n"),
4478 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
4483 if (pcs == NULL || pcs->pcs_state == CS_NONE)
4486 (void) printf(gettext("checkpoint: "));
4488 start = pcs->pcs_start_time;
4489 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
4491 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
4492 char *date = ctime(&start);
4495 * ctime() adds a newline at the end of the generated
4496 * string, thus the weird format specifier and the
4497 * strlen() call used to chop it off from the output.
4499 (void) printf(gettext("created %.*s, consumes %s\n"),
4500 strlen(date) - 1, date, space_buf);
4504 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
4506 (void) printf(gettext("discarding, %s remaining.\n"),
4511 print_error_log(zpool_handle_t *zhp)
4513 nvlist_t *nverrlist = NULL;
4516 size_t len = MAXPATHLEN * 2;
4518 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4519 (void) printf("errors: List of errors unavailable "
4520 "(insufficient privileges)\n");
4524 (void) printf("errors: Permanent errors have been "
4525 "detected in the following files:\n\n");
4527 pathname = safe_malloc(len);
4529 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4531 uint64_t dsobj, obj;
4533 verify(nvpair_value_nvlist(elem, &nv) == 0);
4534 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4536 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4538 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4539 (void) printf("%7s %s\n", "", pathname);
4542 nvlist_free(nverrlist);
4546 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4555 (void) printf(gettext("\tspares\n"));
4557 for (i = 0; i < nspares; i++) {
4558 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4559 print_status_config(zhp, name, spares[i],
4560 namewidth, 2, B_TRUE);
4566 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4575 (void) printf(gettext("\tcache\n"));
4577 for (i = 0; i < nl2cache; i++) {
4578 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4579 print_status_config(zhp, name, l2cache[i],
4580 namewidth, 2, B_FALSE);
4586 print_dedup_stats(nvlist_t *config)
4588 ddt_histogram_t *ddh;
4594 * If the pool was faulted then we may not have been able to
4595 * obtain the config. Otherwise, if we have anything in the dedup
4596 * table continue processing the stats.
4598 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4599 (uint64_t **)&ddo, &c) != 0)
4602 (void) printf("\n");
4603 (void) printf(gettext(" dedup: "));
4604 if (ddo->ddo_count == 0) {
4605 (void) printf(gettext("no DDT entries\n"));
4609 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4610 (u_longlong_t)ddo->ddo_count,
4611 (u_longlong_t)ddo->ddo_dspace,
4612 (u_longlong_t)ddo->ddo_mspace);
4614 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4615 (uint64_t **)&dds, &c) == 0);
4616 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4617 (uint64_t **)&ddh, &c) == 0);
4618 zpool_dump_ddt(dds, ddh);
4622 * Display a summary of pool status. Displays a summary such as:
4626 * reason: One or more devices ...
4627 * see: http://illumos.org/msg/ZFS-xxxx-01
4633 * When given the '-v' option, we print out the complete config. If the '-e'
4634 * option is specified, then we print out error rate information as well.
4637 status_callback(zpool_handle_t *zhp, void *data)
4639 status_cbdata_t *cbp = data;
4640 nvlist_t *config, *nvroot;
4647 config = zpool_get_config(zhp, NULL);
4648 reason = zpool_get_status(zhp, &msgid);
4653 * If we were given 'zpool status -x', only report those pools with
4656 if (cbp->cb_explain &&
4657 (reason == ZPOOL_STATUS_OK ||
4658 reason == ZPOOL_STATUS_VERSION_OLDER ||
4659 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4660 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4661 if (!cbp->cb_allpools) {
4662 (void) printf(gettext("pool '%s' is healthy\n"),
4663 zpool_get_name(zhp));
4665 cbp->cb_first = B_FALSE;
4671 cbp->cb_first = B_FALSE;
4673 (void) printf("\n");
4675 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
4676 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4677 (uint64_t **)&vs, &c) == 0);
4678 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4680 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4681 (void) printf(gettext(" state: %s\n"), health);
4684 case ZPOOL_STATUS_MISSING_DEV_R:
4685 (void) printf(gettext("status: One or more devices could not "
4686 "be opened. Sufficient replicas exist for\n\tthe pool to "
4687 "continue functioning in a degraded state.\n"));
4688 (void) printf(gettext("action: Attach the missing device and "
4689 "online it using 'zpool online'.\n"));
4692 case ZPOOL_STATUS_MISSING_DEV_NR:
4693 (void) printf(gettext("status: One or more devices could not "
4694 "be opened. There are insufficient\n\treplicas for the "
4695 "pool to continue functioning.\n"));
4696 (void) printf(gettext("action: Attach the missing device and "
4697 "online it using 'zpool online'.\n"));
4700 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4701 (void) printf(gettext("status: One or more devices could not "
4702 "be used because the label is missing or\n\tinvalid. "
4703 "Sufficient replicas exist for the pool to continue\n\t"
4704 "functioning in a degraded state.\n"));
4705 (void) printf(gettext("action: Replace the device using "
4706 "'zpool replace'.\n"));
4709 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4710 (void) printf(gettext("status: One or more devices could not "
4711 "be used because the label is missing \n\tor invalid. "
4712 "There are insufficient replicas for the pool to "
4713 "continue\n\tfunctioning.\n"));
4714 zpool_explain_recover(zpool_get_handle(zhp),
4715 zpool_get_name(zhp), reason, config);
4718 case ZPOOL_STATUS_FAILING_DEV:
4719 (void) printf(gettext("status: One or more devices has "
4720 "experienced an unrecoverable error. An\n\tattempt was "
4721 "made to correct the error. Applications are "
4723 (void) printf(gettext("action: Determine if the device needs "
4724 "to be replaced, and clear the errors\n\tusing "
4725 "'zpool clear' or replace the device with 'zpool "
4729 case ZPOOL_STATUS_OFFLINE_DEV:
4730 (void) printf(gettext("status: One or more devices has "
4731 "been taken offline by the administrator.\n\tSufficient "
4732 "replicas exist for the pool to continue functioning in "
4733 "a\n\tdegraded state.\n"));
4734 (void) printf(gettext("action: Online the device using "
4735 "'zpool online' or replace the device with\n\t'zpool "
4739 case ZPOOL_STATUS_REMOVED_DEV:
4740 (void) printf(gettext("status: One or more devices has "
4741 "been removed by the administrator.\n\tSufficient "
4742 "replicas exist for the pool to continue functioning in "
4743 "a\n\tdegraded state.\n"));
4744 (void) printf(gettext("action: Online the device using "
4745 "'zpool online' or replace the device with\n\t'zpool "
4749 case ZPOOL_STATUS_RESILVERING:
4750 (void) printf(gettext("status: One or more devices is "
4751 "currently being resilvered. The pool will\n\tcontinue "
4752 "to function, possibly in a degraded state.\n"));
4753 (void) printf(gettext("action: Wait for the resilver to "
4757 case ZPOOL_STATUS_CORRUPT_DATA:
4758 (void) printf(gettext("status: One or more devices has "
4759 "experienced an error resulting in data\n\tcorruption. "
4760 "Applications may be affected.\n"));
4761 (void) printf(gettext("action: Restore the file in question "
4762 "if possible. Otherwise restore the\n\tentire pool from "
4766 case ZPOOL_STATUS_CORRUPT_POOL:
4767 (void) printf(gettext("status: The pool metadata is corrupted "
4768 "and the pool cannot be opened.\n"));
4769 zpool_explain_recover(zpool_get_handle(zhp),
4770 zpool_get_name(zhp), reason, config);
4773 case ZPOOL_STATUS_VERSION_OLDER:
4774 (void) printf(gettext("status: The pool is formatted using a "
4775 "legacy on-disk format. The pool can\n\tstill be used, "
4776 "but some features are unavailable.\n"));
4777 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4778 "upgrade'. Once this is done, the\n\tpool will no longer "
4779 "be accessible on software that does not support feature\n"
4783 case ZPOOL_STATUS_VERSION_NEWER:
4784 (void) printf(gettext("status: The pool has been upgraded to a "
4785 "newer, incompatible on-disk version.\n\tThe pool cannot "
4786 "be accessed on this system.\n"));
4787 (void) printf(gettext("action: Access the pool from a system "
4788 "running more recent software, or\n\trestore the pool from "
4792 case ZPOOL_STATUS_FEAT_DISABLED:
4793 (void) printf(gettext("status: Some supported features are not "
4794 "enabled on the pool. The pool can\n\tstill be used, but "
4795 "some features are unavailable.\n"));
4796 (void) printf(gettext("action: Enable all features using "
4797 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4798 "longer be accessible by software that does not support\n\t"
4799 "the features. See zpool-features(7) for details.\n"));
4802 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4803 (void) printf(gettext("status: The pool cannot be accessed on "
4804 "this system because it uses the\n\tfollowing feature(s) "
4805 "not supported on this system:\n"));
4806 zpool_print_unsup_feat(config);
4807 (void) printf("\n");
4808 (void) printf(gettext("action: Access the pool from a system "
4809 "that supports the required feature(s),\n\tor restore the "
4810 "pool from backup.\n"));
4813 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4814 (void) printf(gettext("status: The pool can only be accessed "
4815 "in read-only mode on this system. It\n\tcannot be "
4816 "accessed in read-write mode because it uses the "
4817 "following\n\tfeature(s) not supported on this system:\n"));
4818 zpool_print_unsup_feat(config);
4819 (void) printf("\n");
4820 (void) printf(gettext("action: The pool cannot be accessed in "
4821 "read-write mode. Import the pool with\n"
4822 "\t\"-o readonly=on\", access the pool from a system that "
4823 "supports the\n\trequired feature(s), or restore the "
4824 "pool from backup.\n"));
4827 case ZPOOL_STATUS_FAULTED_DEV_R:
4828 (void) printf(gettext("status: One or more devices are "
4829 "faulted in response to persistent errors.\n\tSufficient "
4830 "replicas exist for the pool to continue functioning "
4831 "in a\n\tdegraded state.\n"));
4832 (void) printf(gettext("action: Replace the faulted device, "
4833 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4836 case ZPOOL_STATUS_FAULTED_DEV_NR:
4837 (void) printf(gettext("status: One or more devices are "
4838 "faulted in response to persistent errors. There are "
4839 "insufficient replicas for the pool to\n\tcontinue "
4841 (void) printf(gettext("action: Destroy and re-create the pool "
4842 "from a backup source. Manually marking the device\n"
4843 "\trepaired using 'zpool clear' may allow some data "
4844 "to be recovered.\n"));
4847 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4848 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4849 (void) printf(gettext("status: One or more devices are "
4850 "faulted in response to IO failures.\n"));
4851 (void) printf(gettext("action: Make sure the affected devices "
4852 "are connected, then run 'zpool clear'.\n"));
4855 case ZPOOL_STATUS_BAD_LOG:
4856 (void) printf(gettext("status: An intent log record "
4857 "could not be read.\n"
4858 "\tWaiting for adminstrator intervention to fix the "
4859 "faulted pool.\n"));
4860 (void) printf(gettext("action: Either restore the affected "
4861 "device(s) and run 'zpool online',\n"
4862 "\tor ignore the intent log records by running "
4863 "'zpool clear'.\n"));
4866 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
4867 (void) printf(gettext("status: One or more devices are "
4868 "configured to use a non-native block size.\n"
4869 "\tExpect reduced performance.\n"));
4870 (void) printf(gettext("action: Replace affected devices with "
4871 "devices that support the\n\tconfigured block size, or "
4872 "migrate data to a properly configured\n\tpool.\n"));
4877 * The remaining errors can't actually be generated, yet.
4879 assert(reason == ZPOOL_STATUS_OK);
4883 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4886 if (config != NULL) {
4889 nvlist_t **spares, **l2cache;
4890 uint_t nspares, nl2cache;
4891 pool_checkpoint_stat_t *pcs = NULL;
4892 pool_scan_stat_t *ps = NULL;
4893 pool_removal_stat_t *prs = NULL;
4895 (void) nvlist_lookup_uint64_array(nvroot,
4896 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
4897 (void) nvlist_lookup_uint64_array(nvroot,
4898 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4899 (void) nvlist_lookup_uint64_array(nvroot,
4900 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
4902 print_scan_status(ps);
4903 print_checkpoint_scan_warning(ps, pcs);
4904 print_removal_status(zhp, prs);
4905 print_checkpoint_status(pcs);
4907 namewidth = max_width(zhp, nvroot, 0, 0);
4911 (void) printf(gettext("config:\n\n"));
4912 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4913 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4914 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4915 namewidth, 0, B_FALSE);
4917 if (num_logs(nvroot) > 0)
4918 print_logs(zhp, nvroot, namewidth, B_TRUE);
4919 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4920 &l2cache, &nl2cache) == 0)
4921 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4923 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4924 &spares, &nspares) == 0)
4925 print_spares(zhp, spares, nspares, namewidth);
4927 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4929 nvlist_t *nverrlist = NULL;
4932 * If the approximate error count is small, get a
4933 * precise count by fetching the entire log and
4934 * uniquifying the results.
4936 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4937 zpool_get_errlog(zhp, &nverrlist) == 0) {
4942 while ((elem = nvlist_next_nvpair(nverrlist,
4947 nvlist_free(nverrlist);
4949 (void) printf("\n");
4952 (void) printf(gettext("errors: No known data "
4954 else if (!cbp->cb_verbose)
4955 (void) printf(gettext("errors: %llu data "
4956 "errors, use '-v' for a list\n"),
4957 (u_longlong_t)nerr);
4959 print_error_log(zhp);
4962 if (cbp->cb_dedup_stats)
4963 print_dedup_stats(config);
4965 (void) printf(gettext("config: The configuration cannot be "
4973 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4975 * -v Display complete error logs
4976 * -x Display only pools with potential problems
4977 * -D Display dedup status (undocumented)
4978 * -T Display a timestamp in date(1) or Unix format
4980 * Describes the health status of all pools or some subset.
4983 zpool_do_status(int argc, char **argv)
4987 unsigned long interval = 0, count = 0;
4988 status_cbdata_t cb = { 0 };
4991 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4994 cb.cb_verbose = B_TRUE;
4997 cb.cb_explain = B_TRUE;
5000 cb.cb_dedup_stats = B_TRUE;
5003 get_timestamp_arg(*optarg);
5006 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5015 get_interval_count(&argc, argv, &interval, &count);
5018 cb.cb_allpools = B_TRUE;
5020 cb.cb_first = B_TRUE;
5023 if (timestamp_fmt != NODATE)
5024 print_timestamp(timestamp_fmt);
5026 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5027 status_callback, &cb);
5029 if (argc == 0 && cb.cb_count == 0)
5030 (void) printf(gettext("no pools available\n"));
5031 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
5032 (void) printf(gettext("all pools are healthy\n"));
5040 if (count != 0 && --count == 0)
5043 (void) sleep(interval);
5049 typedef struct upgrade_cbdata {
5051 boolean_t cb_unavail;
5052 char cb_poolname[ZFS_MAX_DATASET_NAME_LEN];
5054 uint64_t cb_version;
5060 is_root_pool(zpool_handle_t *zhp)
5062 static struct statfs sfs;
5063 static char *poolname = NULL;
5064 static boolean_t stated = B_FALSE;
5069 if (statfs("/", &sfs) == -1) {
5070 (void) fprintf(stderr,
5071 "Unable to stat root file system: %s.\n",
5075 if (strcmp(sfs.f_fstypename, "zfs") != 0)
5077 poolname = sfs.f_mntfromname;
5078 if ((slash = strchr(poolname, '/')) != NULL)
5081 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
5085 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
5088 if (poolname[0] == '\0' && is_root_pool(zhp))
5089 (void) strlcpy(poolname, zpool_get_name(zhp), size);
5091 #endif /* FreeBSD */
5094 upgrade_version(zpool_handle_t *zhp, uint64_t version)
5098 uint64_t oldversion;
5100 config = zpool_get_config(zhp, NULL);
5101 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5104 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
5105 assert(oldversion < version);
5107 ret = zpool_upgrade(zhp, version);
5111 if (version >= SPA_VERSION_FEATURES) {
5112 (void) printf(gettext("Successfully upgraded "
5113 "'%s' from version %llu to feature flags.\n"),
5114 zpool_get_name(zhp), oldversion);
5116 (void) printf(gettext("Successfully upgraded "
5117 "'%s' from version %llu to version %llu.\n"),
5118 zpool_get_name(zhp), oldversion, version);
5125 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
5128 boolean_t firstff = B_TRUE;
5129 nvlist_t *enabled = zpool_get_features(zhp);
5132 for (i = 0; i < SPA_FEATURES; i++) {
5133 const char *fname = spa_feature_table[i].fi_uname;
5134 const char *fguid = spa_feature_table[i].fi_guid;
5135 if (!nvlist_exists(enabled, fguid)) {
5137 verify(-1 != asprintf(&propname, "feature@%s", fname));
5138 ret = zpool_set_prop(zhp, propname,
5139 ZFS_FEATURE_ENABLED);
5147 (void) printf(gettext("Enabled the "
5148 "following features on '%s':\n"),
5149 zpool_get_name(zhp));
5152 (void) printf(gettext(" %s\n"), fname);
5163 upgrade_cb(zpool_handle_t *zhp, void *arg)
5165 upgrade_cbdata_t *cbp = arg;
5168 boolean_t printnl = B_FALSE;
5171 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5172 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5173 "currently unavailable.\n\n"), zpool_get_name(zhp));
5174 cbp->cb_unavail = B_TRUE;
5175 /* Allow iteration to continue. */
5179 config = zpool_get_config(zhp, NULL);
5180 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5183 assert(SPA_VERSION_IS_SUPPORTED(version));
5185 if (version < cbp->cb_version) {
5186 cbp->cb_first = B_FALSE;
5187 ret = upgrade_version(zhp, cbp->cb_version);
5191 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5192 sizeof(cbp->cb_poolname));
5193 #endif /* __FreeBSD__ */
5198 * If they did "zpool upgrade -a", then we could
5199 * be doing ioctls to different pools. We need
5200 * to log this history once to each pool, and bypass
5201 * the normal history logging that happens in main().
5203 (void) zpool_log_history(g_zfs, history_str);
5204 log_history = B_FALSE;
5208 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5210 ret = upgrade_enable_all(zhp, &count);
5215 cbp->cb_first = B_FALSE;
5218 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5219 sizeof(cbp->cb_poolname));
5220 #endif /* __FreeBSD__ */
5222 * If they did "zpool upgrade -a", then we could
5223 * be doing ioctls to different pools. We need
5224 * to log this history once to each pool, and bypass
5225 * the normal history logging that happens in main().
5227 (void) zpool_log_history(g_zfs, history_str);
5228 log_history = B_FALSE;
5233 (void) printf(gettext("\n"));
5240 upgrade_list_unavail(zpool_handle_t *zhp, void *arg)
5242 upgrade_cbdata_t *cbp = arg;
5244 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5245 if (cbp->cb_first) {
5246 (void) fprintf(stderr, gettext("The following pools "
5247 "are unavailable and cannot be upgraded as this "
5249 (void) fprintf(stderr, gettext("POOL\n"));
5250 (void) fprintf(stderr, gettext("------------\n"));
5251 cbp->cb_first = B_FALSE;
5253 (void) printf(gettext("%s\n"), zpool_get_name(zhp));
5254 cbp->cb_unavail = B_TRUE;
5260 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
5262 upgrade_cbdata_t *cbp = arg;
5266 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5268 * This will have been reported by upgrade_list_unavail so
5269 * just allow iteration to continue.
5271 cbp->cb_unavail = B_TRUE;
5275 config = zpool_get_config(zhp, NULL);
5276 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5279 assert(SPA_VERSION_IS_SUPPORTED(version));
5281 if (version < SPA_VERSION_FEATURES) {
5282 if (cbp->cb_first) {
5283 (void) printf(gettext("The following pools are "
5284 "formatted with legacy version numbers and can\n"
5285 "be upgraded to use feature flags. After "
5286 "being upgraded, these pools\nwill no "
5287 "longer be accessible by software that does not "
5288 "support feature\nflags.\n\n"));
5289 (void) printf(gettext("VER POOL\n"));
5290 (void) printf(gettext("--- ------------\n"));
5291 cbp->cb_first = B_FALSE;
5294 (void) printf("%2llu %s\n", (u_longlong_t)version,
5295 zpool_get_name(zhp));
5302 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
5304 upgrade_cbdata_t *cbp = arg;
5308 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5310 * This will have been reported by upgrade_list_unavail so
5311 * just allow iteration to continue.
5313 cbp->cb_unavail = B_TRUE;
5317 config = zpool_get_config(zhp, NULL);
5318 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5321 if (version >= SPA_VERSION_FEATURES) {
5323 boolean_t poolfirst = B_TRUE;
5324 nvlist_t *enabled = zpool_get_features(zhp);
5326 for (i = 0; i < SPA_FEATURES; i++) {
5327 const char *fguid = spa_feature_table[i].fi_guid;
5328 const char *fname = spa_feature_table[i].fi_uname;
5329 if (!nvlist_exists(enabled, fguid)) {
5330 if (cbp->cb_first) {
5331 (void) printf(gettext("\nSome "
5332 "supported features are not "
5333 "enabled on the following pools. "
5334 "Once a\nfeature is enabled the "
5335 "pool may become incompatible with "
5336 "software\nthat does not support "
5338 "zpool-features(7) for "
5340 (void) printf(gettext("POOL "
5342 (void) printf(gettext("------"
5344 cbp->cb_first = B_FALSE;
5348 (void) printf(gettext("%s\n"),
5349 zpool_get_name(zhp));
5350 poolfirst = B_FALSE;
5353 (void) printf(gettext(" %s\n"), fname);
5363 upgrade_one(zpool_handle_t *zhp, void *data)
5365 boolean_t printnl = B_FALSE;
5366 upgrade_cbdata_t *cbp = data;
5367 uint64_t cur_version;
5370 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5371 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5372 "is currently unavailable.\n\n"), zpool_get_name(zhp));
5373 cbp->cb_unavail = B_TRUE;
5377 if (strcmp("log", zpool_get_name(zhp)) == 0) {
5378 (void) printf(gettext("'log' is now a reserved word\n"
5379 "Pool 'log' must be renamed using export and import"
5380 " to upgrade.\n\n"));
5384 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5385 if (cur_version > cbp->cb_version) {
5386 (void) printf(gettext("Pool '%s' is already formatted "
5387 "using more current version '%llu'.\n\n"),
5388 zpool_get_name(zhp), cur_version);
5392 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
5393 (void) printf(gettext("Pool '%s' is already formatted "
5394 "using version %llu.\n\n"), zpool_get_name(zhp),
5399 if (cur_version != cbp->cb_version) {
5401 ret = upgrade_version(zhp, cbp->cb_version);
5405 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5406 sizeof(cbp->cb_poolname));
5407 #endif /* __FreeBSD__ */
5410 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5412 ret = upgrade_enable_all(zhp, &count);
5419 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5420 sizeof(cbp->cb_poolname));
5421 #endif /* __FreeBSD __*/
5422 } else if (cur_version == SPA_VERSION) {
5423 (void) printf(gettext("Pool '%s' already has all "
5424 "supported features enabled.\n\n"),
5425 zpool_get_name(zhp));
5430 (void) printf(gettext("\n"));
5439 * zpool upgrade [-V version] <-a | pool ...>
5441 * With no arguments, display downrev'd ZFS pool available for upgrade.
5442 * Individual pools can be upgraded by specifying the pool, and '-a' will
5443 * upgrade all pools.
5446 zpool_do_upgrade(int argc, char **argv)
5449 upgrade_cbdata_t cb = { 0 };
5451 boolean_t showversions = B_FALSE;
5452 boolean_t upgradeall = B_FALSE;
5457 while ((c = getopt(argc, argv, ":avV:")) != -1) {
5460 upgradeall = B_TRUE;
5463 showversions = B_TRUE;
5466 cb.cb_version = strtoll(optarg, &end, 10);
5468 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5469 (void) fprintf(stderr,
5470 gettext("invalid version '%s'\n"), optarg);
5475 (void) fprintf(stderr, gettext("missing argument for "
5476 "'%c' option\n"), optopt);
5480 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5491 if (cb.cb_version == 0) {
5492 cb.cb_version = SPA_VERSION;
5493 } else if (!upgradeall && argc == 0) {
5494 (void) fprintf(stderr, gettext("-V option is "
5495 "incompatible with other arguments\n"));
5500 if (upgradeall || argc != 0) {
5501 (void) fprintf(stderr, gettext("-v option is "
5502 "incompatible with other arguments\n"));
5505 } else if (upgradeall) {
5507 (void) fprintf(stderr, gettext("-a option should not "
5508 "be used along with a pool name\n"));
5513 (void) printf(gettext("This system supports ZFS pool feature "
5518 (void) printf(gettext("The following features are "
5520 (void) printf(gettext("FEAT DESCRIPTION\n"));
5521 (void) printf("----------------------------------------------"
5522 "---------------\n");
5523 for (i = 0; i < SPA_FEATURES; i++) {
5524 zfeature_info_t *fi = &spa_feature_table[i];
5526 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
5527 " (read-only compatible)" : "";
5529 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5530 (void) printf(" %s\n", fi->fi_desc);
5532 (void) printf("\n");
5534 (void) printf(gettext("The following legacy versions are also "
5536 (void) printf(gettext("VER DESCRIPTION\n"));
5537 (void) printf("--- -----------------------------------------"
5538 "---------------\n");
5539 (void) printf(gettext(" 1 Initial ZFS version\n"));
5540 (void) printf(gettext(" 2 Ditto blocks "
5541 "(replicated metadata)\n"));
5542 (void) printf(gettext(" 3 Hot spares and double parity "
5544 (void) printf(gettext(" 4 zpool history\n"));
5545 (void) printf(gettext(" 5 Compression using the gzip "
5547 (void) printf(gettext(" 6 bootfs pool property\n"));
5548 (void) printf(gettext(" 7 Separate intent log devices\n"));
5549 (void) printf(gettext(" 8 Delegated administration\n"));
5550 (void) printf(gettext(" 9 refquota and refreservation "
5552 (void) printf(gettext(" 10 Cache devices\n"));
5553 (void) printf(gettext(" 11 Improved scrub performance\n"));
5554 (void) printf(gettext(" 12 Snapshot properties\n"));
5555 (void) printf(gettext(" 13 snapused property\n"));
5556 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5557 (void) printf(gettext(" 15 user/group space accounting\n"));
5558 (void) printf(gettext(" 16 stmf property support\n"));
5559 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5560 (void) printf(gettext(" 18 Snapshot user holds\n"));
5561 (void) printf(gettext(" 19 Log device removal\n"));
5562 (void) printf(gettext(" 20 Compression using zle "
5563 "(zero-length encoding)\n"));
5564 (void) printf(gettext(" 21 Deduplication\n"));
5565 (void) printf(gettext(" 22 Received properties\n"));
5566 (void) printf(gettext(" 23 Slim ZIL\n"));
5567 (void) printf(gettext(" 24 System attributes\n"));
5568 (void) printf(gettext(" 25 Improved scrub stats\n"));
5569 (void) printf(gettext(" 26 Improved snapshot deletion "
5571 (void) printf(gettext(" 27 Improved snapshot creation "
5573 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5574 (void) printf(gettext("\nFor more information on a particular "
5575 "version, including supported releases,\n"));
5576 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5577 } else if (argc == 0 && upgradeall) {
5578 cb.cb_first = B_TRUE;
5579 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5580 if (ret == 0 && cb.cb_first) {
5581 if (cb.cb_version == SPA_VERSION) {
5582 (void) printf(gettext("All %spools are already "
5583 "formatted using feature flags.\n\n"),
5584 cb.cb_unavail ? gettext("available ") : "");
5585 (void) printf(gettext("Every %sfeature flags "
5586 "pool already has all supported features "
5588 cb.cb_unavail ? gettext("available ") : "");
5590 (void) printf(gettext("All pools are already "
5591 "formatted with version %llu or higher.\n"),
5595 } else if (argc == 0) {
5596 cb.cb_first = B_TRUE;
5597 ret = zpool_iter(g_zfs, upgrade_list_unavail, &cb);
5601 (void) fprintf(stderr, "\n");
5604 cb.cb_first = B_TRUE;
5605 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5609 (void) printf(gettext("All %spools are formatted using "
5610 "feature flags.\n\n"), cb.cb_unavail ?
5611 gettext("available ") : "");
5613 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5614 "for a list of available legacy versions.\n"));
5617 cb.cb_first = B_TRUE;
5618 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5622 (void) printf(gettext("Every %sfeature flags pool has "
5623 "all supported features enabled.\n"),
5624 cb.cb_unavail ? gettext("available ") : "");
5626 (void) printf(gettext("\n"));
5629 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5633 if (cb.cb_poolname[0] != '\0') {
5635 "If you boot from pool '%s', don't forget to update boot code.\n"
5636 "Assuming you use GPT partitioning and da0 is your boot disk\n"
5637 "the following command will do it:\n"
5639 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5646 typedef struct hist_cbdata {
5653 * Print out the command history for a specific pool.
5656 get_history_one(zpool_handle_t *zhp, void *data)
5662 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5664 cb->first = B_FALSE;
5666 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5668 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5671 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5672 &records, &numrecords) == 0);
5673 for (i = 0; i < numrecords; i++) {
5674 nvlist_t *rec = records[i];
5677 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5681 tsec = fnvlist_lookup_uint64(records[i],
5683 (void) localtime_r(&tsec, &t);
5684 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5687 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5688 (void) printf("%s %s", tbuf,
5689 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5690 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5692 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5695 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5696 (void) printf("%s unrecognized record:\n",
5698 dump_nvlist(rec, 4);
5701 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5702 zfs_history_event_names[ievent],
5703 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5704 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5705 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5708 (void) printf("%s [txg:%lld] %s", tbuf,
5709 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5710 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5711 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5712 (void) printf(" %s (%llu)",
5713 fnvlist_lookup_string(rec,
5715 fnvlist_lookup_uint64(rec,
5718 (void) printf(" %s", fnvlist_lookup_string(rec,
5719 ZPOOL_HIST_INT_STR));
5720 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5723 (void) printf("%s ioctl %s\n", tbuf,
5724 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5725 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5726 (void) printf(" input:\n");
5727 dump_nvlist(fnvlist_lookup_nvlist(rec,
5728 ZPOOL_HIST_INPUT_NVL), 8);
5730 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5731 (void) printf(" output:\n");
5732 dump_nvlist(fnvlist_lookup_nvlist(rec,
5733 ZPOOL_HIST_OUTPUT_NVL), 8);
5735 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
5736 (void) printf(" errno: %lld\n",
5737 fnvlist_lookup_int64(rec,
5743 (void) printf("%s unrecognized record:\n", tbuf);
5744 dump_nvlist(rec, 4);
5748 (void) printf("\n");
5751 (void) printf(" [");
5752 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5753 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5754 struct passwd *pwd = getpwuid(who);
5755 (void) printf("user %d ", (int)who);
5757 (void) printf("(%s) ", pwd->pw_name);
5759 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5760 (void) printf("on %s",
5761 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5763 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5764 (void) printf(":%s",
5765 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5768 (void) printf("\n");
5770 (void) printf("\n");
5777 * zpool history <pool>
5779 * Displays the history of commands that modified pools.
5782 zpool_do_history(int argc, char **argv)
5784 hist_cbdata_t cbdata = { 0 };
5788 cbdata.first = B_TRUE;
5790 while ((c = getopt(argc, argv, "li")) != -1) {
5793 cbdata.longfmt = B_TRUE;
5796 cbdata.internal = B_TRUE;
5799 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5807 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5810 if (argc == 0 && cbdata.first == B_TRUE) {
5811 (void) printf(gettext("no pools available\n"));
5819 get_callback(zpool_handle_t *zhp, void *data)
5821 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5822 char value[MAXNAMELEN];
5823 zprop_source_t srctype;
5826 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5829 * Skip the special fake placeholder. This will also skip
5830 * over the name property when 'all' is specified.
5832 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5833 pl == cbp->cb_proplist)
5836 if (pl->pl_prop == ZPROP_INVAL &&
5837 (zpool_prop_feature(pl->pl_user_prop) ||
5838 zpool_prop_unsupported(pl->pl_user_prop))) {
5839 srctype = ZPROP_SRC_LOCAL;
5841 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5842 value, sizeof (value)) == 0) {
5843 zprop_print_one_property(zpool_get_name(zhp),
5844 cbp, pl->pl_user_prop, value, srctype,
5848 if (zpool_get_prop(zhp, pl->pl_prop, value,
5849 sizeof (value), &srctype, cbp->cb_literal) != 0)
5852 zprop_print_one_property(zpool_get_name(zhp), cbp,
5853 zpool_prop_to_name(pl->pl_prop), value, srctype,
5861 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5863 * -H Scripted mode. Don't display headers, and separate properties
5865 * -o List of columns to display. Defaults to
5866 * "name,property,value,source".
5867 * -p Diplay values in parsable (exact) format.
5869 * Get properties of pools in the system. Output space statistics
5870 * for each one as well as other attributes.
5873 zpool_do_get(int argc, char **argv)
5875 zprop_get_cbdata_t cb = { 0 };
5876 zprop_list_t fake_name = { 0 };
5881 cb.cb_first = B_TRUE;
5884 * Set up default columns and sources.
5886 cb.cb_sources = ZPROP_SRC_ALL;
5887 cb.cb_columns[0] = GET_COL_NAME;
5888 cb.cb_columns[1] = GET_COL_PROPERTY;
5889 cb.cb_columns[2] = GET_COL_VALUE;
5890 cb.cb_columns[3] = GET_COL_SOURCE;
5891 cb.cb_type = ZFS_TYPE_POOL;
5894 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5897 cb.cb_literal = B_TRUE;
5900 cb.cb_scripted = B_TRUE;
5903 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5905 while (*optarg != '\0') {
5906 static char *col_subopts[] =
5907 { "name", "property", "value", "source",
5910 if (i == ZFS_GET_NCOLS) {
5911 (void) fprintf(stderr, gettext("too "
5912 "many fields given to -o "
5917 switch (getsubopt(&optarg, col_subopts,
5920 cb.cb_columns[i++] = GET_COL_NAME;
5923 cb.cb_columns[i++] = GET_COL_PROPERTY;
5926 cb.cb_columns[i++] = GET_COL_VALUE;
5929 cb.cb_columns[i++] = GET_COL_SOURCE;
5933 (void) fprintf(stderr,
5934 gettext("\"all\" conflicts "
5935 "with specific fields "
5936 "given to -o option\n"));
5939 cb.cb_columns[0] = GET_COL_NAME;
5940 cb.cb_columns[1] = GET_COL_PROPERTY;
5941 cb.cb_columns[2] = GET_COL_VALUE;
5942 cb.cb_columns[3] = GET_COL_SOURCE;
5946 (void) fprintf(stderr,
5947 gettext("invalid column name "
5948 "'%s'\n"), suboptarg);
5954 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5964 (void) fprintf(stderr, gettext("missing property "
5969 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5970 ZFS_TYPE_POOL) != 0)
5976 if (cb.cb_proplist != NULL) {
5977 fake_name.pl_prop = ZPOOL_PROP_NAME;
5978 fake_name.pl_width = strlen(gettext("NAME"));
5979 fake_name.pl_next = cb.cb_proplist;
5980 cb.cb_proplist = &fake_name;
5983 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5986 if (cb.cb_proplist == &fake_name)
5987 zprop_free_list(fake_name.pl_next);
5989 zprop_free_list(cb.cb_proplist);
5994 typedef struct set_cbdata {
5997 boolean_t cb_any_successful;
6001 set_callback(zpool_handle_t *zhp, void *data)
6004 set_cbdata_t *cb = (set_cbdata_t *)data;
6006 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
6009 cb->cb_any_successful = B_TRUE;
6015 zpool_do_set(int argc, char **argv)
6017 set_cbdata_t cb = { 0 };
6020 if (argc > 1 && argv[1][0] == '-') {
6021 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6027 (void) fprintf(stderr, gettext("missing property=value "
6033 (void) fprintf(stderr, gettext("missing pool name\n"));
6038 (void) fprintf(stderr, gettext("too many pool names\n"));
6042 cb.cb_propname = argv[1];
6043 cb.cb_value = strchr(cb.cb_propname, '=');
6044 if (cb.cb_value == NULL) {
6045 (void) fprintf(stderr, gettext("missing value in "
6046 "property=value argument\n"));
6050 *(cb.cb_value) = '\0';
6053 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
6060 find_command_idx(char *command, int *idx)
6064 for (i = 0; i < NCOMMAND; i++) {
6065 if (command_table[i].name == NULL)
6068 if (strcmp(command, command_table[i].name) == 0) {
6077 main(int argc, char **argv)
6083 (void) setlocale(LC_ALL, "");
6084 (void) textdomain(TEXT_DOMAIN);
6086 if ((g_zfs = libzfs_init()) == NULL) {
6087 (void) fprintf(stderr, gettext("internal error: failed to "
6088 "initialize ZFS library\n"));
6092 libzfs_print_on_error(g_zfs, B_TRUE);
6097 * Make sure the user has specified some command.
6100 (void) fprintf(stderr, gettext("missing command\n"));
6109 if (strcmp(cmdname, "-?") == 0)
6112 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6115 * Run the appropriate command.
6117 if (find_command_idx(cmdname, &i) == 0) {
6118 current_command = &command_table[i];
6119 ret = command_table[i].func(argc - 1, argv + 1);
6120 } else if (strchr(cmdname, '=')) {
6121 verify(find_command_idx("set", &i) == 0);
6122 current_command = &command_table[i];
6123 ret = command_table[i].func(argc, argv);
6124 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
6126 * 'freeze' is a vile debugging abomination, so we treat
6129 zfs_cmd_t zc = { 0 };
6130 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
6131 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
6133 (void) fprintf(stderr, gettext("unrecognized "
6134 "command '%s'\n"), cmdname);
6138 if (ret == 0 && log_history)
6139 (void) zpool_log_history(g_zfs, history_str);
6144 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6145 * for the purposes of running ::findleaks.
6147 if (getenv("ZFS_ABORT") != NULL) {
6148 (void) printf("dumping core by request\n");