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_initialize(int, char **);
91 static int zpool_do_scrub(int, char **);
93 static int zpool_do_import(int, char **);
94 static int zpool_do_export(int, char **);
96 static int zpool_do_upgrade(int, char **);
98 static int zpool_do_history(int, char **);
100 static int zpool_do_get(int, char **);
101 static int zpool_do_set(int, char **);
104 * These libumem hooks provide a reasonable set of defaults for the allocator's
105 * debugging facilities.
110 _umem_debug_init(void)
112 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 _umem_logging_init(void)
118 return ("fail,contents"); /* $UMEM_LOGGING setting */
152 typedef struct zpool_command {
154 int (*func)(int, char **);
159 * Master command table. Each ZFS command has a name, associated function, and
160 * usage message. The usage messages need to be internationalized, so we have
161 * to have a function to return the usage message based on a command index.
163 * These commands are organized according to how they are displayed in the usage
164 * message. An empty command (one with a NULL name) indicates an empty line in
165 * the generic usage message.
167 static zpool_command_t command_table[] = {
168 { "create", zpool_do_create, HELP_CREATE },
169 { "destroy", zpool_do_destroy, HELP_DESTROY },
171 { "add", zpool_do_add, HELP_ADD },
172 { "remove", zpool_do_remove, HELP_REMOVE },
174 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
176 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
178 { "list", zpool_do_list, HELP_LIST },
179 { "iostat", zpool_do_iostat, HELP_IOSTAT },
180 { "status", zpool_do_status, HELP_STATUS },
182 { "online", zpool_do_online, HELP_ONLINE },
183 { "offline", zpool_do_offline, HELP_OFFLINE },
184 { "clear", zpool_do_clear, HELP_CLEAR },
185 { "reopen", zpool_do_reopen, HELP_REOPEN },
187 { "attach", zpool_do_attach, HELP_ATTACH },
188 { "detach", zpool_do_detach, HELP_DETACH },
189 { "replace", zpool_do_replace, HELP_REPLACE },
190 { "split", zpool_do_split, HELP_SPLIT },
192 { "initialize", zpool_do_initialize, HELP_INITIALIZE },
193 { "scrub", zpool_do_scrub, HELP_SCRUB },
195 { "import", zpool_do_import, HELP_IMPORT },
196 { "export", zpool_do_export, HELP_EXPORT },
197 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
198 { "reguid", zpool_do_reguid, HELP_REGUID },
200 { "history", zpool_do_history, HELP_HISTORY },
201 { "get", zpool_do_get, HELP_GET },
202 { "set", zpool_do_set, HELP_SET },
205 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
207 static zpool_command_t *current_command;
208 static char history_str[HIS_MAX_RECORD_LEN];
209 static boolean_t log_history = B_TRUE;
210 static uint_t timestamp_fmt = NODATE;
213 get_usage(zpool_help_t idx)
217 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
219 return (gettext("\tattach [-f] <pool> <device> "
222 return (gettext("\tclear [-nF] <pool> [device]\n"));
224 return (gettext("\tcreate [-fnd] [-B] "
225 "[-o property=value] ... \n"
226 "\t [-O file-system-property=value] ...\n"
227 "\t [-m mountpoint] [-R root] [-t tempname] "
228 "<pool> <vdev> ...\n"));
229 case HELP_CHECKPOINT:
230 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
232 return (gettext("\tdestroy [-f] <pool>\n"));
234 return (gettext("\tdetach <pool> <device>\n"));
236 return (gettext("\texport [-f] <pool> ...\n"));
238 return (gettext("\thistory [-il] [<pool>] ...\n"));
240 return (gettext("\timport [-d dir] [-D]\n"
241 "\timport [-o mntopts] [-o property=value] ... \n"
242 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
243 "[-R root] [-F [-n]] -a\n"
244 "\timport [-o mntopts] [-o property=value] ... \n"
245 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
246 "[-R root] [-F [-n]] [-t]\n"
247 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
249 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
251 case HELP_LABELCLEAR:
252 return (gettext("\tlabelclear [-f] <vdev>\n"));
254 return (gettext("\tlist [-Hpv] [-o property[,...]] "
255 "[-T d|u] [pool] ... [interval [count]]\n"));
257 return (gettext("\toffline [-t] <pool> <device> ...\n"));
259 return (gettext("\tonline [-e] <pool> <device> ...\n"));
261 return (gettext("\treplace [-f] <pool> <device> "
264 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
266 return (gettext("\treopen <pool>\n"));
267 case HELP_INITIALIZE:
268 return (gettext("\tinitialize [-cs] <pool> [<device> ...]\n"));
270 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
272 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
275 return (gettext("\tupgrade [-v]\n"
276 "\tupgrade [-V version] <-a | pool ...>\n"));
278 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
279 "<\"all\" | property[,...]> <pool> ...\n"));
281 return (gettext("\tset <property=value> <pool> \n"));
283 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
284 "\t [-o property=value] <pool> <newpool> "
285 "[<device> ...]\n"));
287 return (gettext("\treguid <pool>\n"));
296 * Callback routine that will print out a pool property value.
299 print_prop_cb(int prop, void *cb)
303 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
305 if (zpool_prop_readonly(prop))
306 (void) fprintf(fp, " NO ");
308 (void) fprintf(fp, " YES ");
310 if (zpool_prop_values(prop) == NULL)
311 (void) fprintf(fp, "-\n");
313 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
319 * Display usage message. If we're inside a command, display only the usage for
320 * that command. Otherwise, iterate over the entire command table and display
321 * a complete usage message.
324 usage(boolean_t requested)
326 FILE *fp = requested ? stdout : stderr;
328 if (current_command == NULL) {
331 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
333 gettext("where 'command' is one of the following:\n\n"));
335 for (i = 0; i < NCOMMAND; i++) {
336 if (command_table[i].name == NULL)
337 (void) fprintf(fp, "\n");
339 (void) fprintf(fp, "%s",
340 get_usage(command_table[i].usage));
343 (void) fprintf(fp, gettext("usage:\n"));
344 (void) fprintf(fp, "%s", get_usage(current_command->usage));
347 if (current_command != NULL &&
348 ((strcmp(current_command->name, "set") == 0) ||
349 (strcmp(current_command->name, "get") == 0) ||
350 (strcmp(current_command->name, "list") == 0))) {
353 gettext("\nthe following properties are supported:\n"));
355 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
356 "PROPERTY", "EDIT", "VALUES");
358 /* Iterate over all properties */
359 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
362 (void) fprintf(fp, "\t%-15s ", "feature@...");
363 (void) fprintf(fp, "YES disabled | enabled | active\n");
365 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
366 "appended with a feature name.\nSee zpool-features(7).\n"));
370 * See comments at end of main().
372 if (getenv("ZFS_ABORT") != NULL) {
373 (void) printf("dumping core by request\n");
377 exit(requested ? 0 : 2);
381 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
382 boolean_t print_logs)
389 (void) printf("\t%*s%s\n", indent, "", name);
391 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
392 &child, &children) != 0)
395 for (c = 0; c < children; c++) {
396 uint64_t is_log = B_FALSE;
398 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
400 if ((is_log && !print_logs) || (!is_log && print_logs))
403 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
404 print_vdev_tree(zhp, vname, child[c], indent + 2,
411 prop_list_contains_feature(nvlist_t *proplist)
414 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
415 nvp = nvlist_next_nvpair(proplist, nvp)) {
416 if (zpool_prop_feature(nvpair_name(nvp)))
423 * Add a property pair (name, string-value) into a property nvlist.
426 add_prop_list(const char *propname, char *propval, nvlist_t **props,
429 zpool_prop_t prop = ZPROP_INVAL;
435 if (*props == NULL &&
436 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
437 (void) fprintf(stderr,
438 gettext("internal error: out of memory\n"));
445 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
447 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
448 !zpool_prop_feature(propname)) {
449 (void) fprintf(stderr, gettext("property '%s' is "
450 "not a valid pool property\n"), propname);
455 * feature@ properties and version should not be specified
458 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
459 nvlist_exists(proplist, vname)) ||
460 (prop == ZPOOL_PROP_VERSION &&
461 prop_list_contains_feature(proplist))) {
462 (void) fprintf(stderr, gettext("'feature@' and "
463 "'version' properties cannot be specified "
469 if (zpool_prop_feature(propname))
472 normnm = zpool_prop_to_name(prop);
474 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
475 normnm = zfs_prop_to_name(fprop);
481 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
482 prop != ZPOOL_PROP_CACHEFILE) {
483 (void) fprintf(stderr, gettext("property '%s' "
484 "specified multiple times\n"), propname);
488 if (nvlist_add_string(proplist, normnm, propval) != 0) {
489 (void) fprintf(stderr, gettext("internal "
490 "error: out of memory\n"));
498 * Set a default property pair (name, string-value) in a property nvlist
501 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
506 if (nvlist_lookup_string(*props, propname, &pval) == 0)
509 return (add_prop_list(propname, propval, props, poolprop));
513 * zpool add [-fn] <pool> <vdev> ...
515 * -f Force addition of devices, even if they appear in use
516 * -n Do not add the devices, but display the resulting layout if
517 * they were to be added.
519 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
520 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
524 zpool_do_add(int argc, char **argv)
526 boolean_t force = B_FALSE;
527 boolean_t dryrun = B_FALSE;
531 zpool_boot_label_t boot_type;
538 while ((c = getopt(argc, argv, "fn")) != -1) {
547 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
556 /* get pool name and check number of arguments */
558 (void) fprintf(stderr, gettext("missing pool name argument\n"));
562 (void) fprintf(stderr, gettext("missing vdev specification\n"));
571 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
574 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
575 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
581 if (zpool_is_bootable(zhp))
582 boot_type = ZPOOL_COPY_BOOT_LABEL;
584 boot_type = ZPOOL_NO_BOOT_LABEL;
586 /* pass off to get_vdev_spec for processing */
587 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
588 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
589 boot_type, boot_size, argc, argv);
590 if (nvroot == NULL) {
596 nvlist_t *poolnvroot;
598 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
601 (void) printf(gettext("would update '%s' to the following "
602 "configuration:\n"), zpool_get_name(zhp));
604 /* print original main pool and new tree */
605 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
606 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
608 /* Do the same for the logs */
609 if (num_logs(poolnvroot) > 0) {
610 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
611 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
612 } else if (num_logs(nvroot) > 0) {
613 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
618 ret = (zpool_add(zhp, nvroot) != 0);
628 * zpool remove <pool> <vdev> ...
630 * Removes the given vdev from the pool.
633 zpool_do_remove(int argc, char **argv)
638 boolean_t stop = B_FALSE;
639 boolean_t noop = B_FALSE;
640 boolean_t parsable = B_FALSE;
644 while ((c = getopt(argc, argv, "nps")) != -1) {
656 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
665 /* get pool name and check number of arguments */
667 (void) fprintf(stderr, gettext("missing pool name argument\n"));
673 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
677 (void) fprintf(stderr, gettext("stop request ignored\n"));
683 (void) fprintf(stderr, gettext("too many arguments\n"));
686 if (zpool_vdev_remove_cancel(zhp) != 0)
690 (void) fprintf(stderr, gettext("missing device\n"));
694 for (i = 1; i < argc; i++) {
698 if (zpool_vdev_indirect_size(zhp, argv[i],
704 (void) printf("%s %llu\n",
708 zfs_nicenum(size, valstr,
710 (void) printf("Memory that will be "
711 "used after removing %s: %s\n",
715 if (zpool_vdev_remove(zhp, argv[i]) != 0)
725 * zpool labelclear [-f] <vdev>
727 * -f Force clearing the label for the vdevs which are members of
728 * the exported or foreign pools.
730 * Verifies that the vdev is not active and zeros out the label information
734 zpool_do_labelclear(int argc, char **argv)
736 char vdev[MAXPATHLEN];
742 boolean_t inuse = B_FALSE;
743 boolean_t force = B_FALSE;
746 while ((c = getopt(argc, argv, "f")) != -1) {
752 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
763 (void) fprintf(stderr, gettext("missing vdev name\n"));
767 (void) fprintf(stderr, gettext("too many arguments\n"));
772 * Check if we were given absolute path and use it as is.
773 * Otherwise if the provided vdev name doesn't point to a file,
774 * try prepending dsk path and appending s0.
776 (void) strlcpy(vdev, argv[0], sizeof (vdev));
777 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
780 (void) snprintf(vdev, sizeof (vdev), "%s/%s",
782 ZFS_DISK_ROOT, argv[0]);
783 if ((s = strrchr(argv[0], 's')) == NULL ||
785 (void) strlcat(vdev, "s0", sizeof (vdev));
789 if (stat(vdev, &st) != 0) {
790 (void) fprintf(stderr, gettext(
791 "failed to find device %s, try specifying absolute "
792 "path instead\n"), argv[0]);
797 if ((fd = open(vdev, O_RDWR)) < 0) {
798 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
799 vdev, strerror(errno));
803 if (zpool_read_label(fd, &config) != 0) {
804 (void) fprintf(stderr,
805 gettext("failed to read label from %s\n"), vdev);
810 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
812 (void) fprintf(stderr,
813 gettext("failed to check state for %s\n"), vdev);
822 case POOL_STATE_ACTIVE:
823 case POOL_STATE_SPARE:
824 case POOL_STATE_L2CACHE:
825 (void) fprintf(stderr, gettext(
826 "%s is a member (%s) of pool \"%s\"\n"),
827 vdev, zpool_pool_state_to_name(state), name);
831 case POOL_STATE_EXPORTED:
834 (void) fprintf(stderr, gettext(
835 "use '-f' to override the following error:\n"
836 "%s is a member of exported pool \"%s\"\n"),
841 case POOL_STATE_POTENTIALLY_ACTIVE:
844 (void) fprintf(stderr, gettext(
845 "use '-f' to override the following error:\n"
846 "%s is a member of potentially active pool \"%s\"\n"),
851 case POOL_STATE_DESTROYED:
852 /* inuse should never be set for a destroyed pool */
858 ret = zpool_clear_label(fd);
860 (void) fprintf(stderr,
861 gettext("failed to clear label for %s\n"), vdev);
872 * zpool create [-fnd] [-B] [-o property=value] ...
873 * [-O file-system-property=value] ...
874 * [-R root] [-m mountpoint] [-t tempname] <pool> <dev> ...
876 * -B Create boot partition.
877 * -f Force creation, even if devices appear in use
878 * -n Do not create the pool, but display the resulting layout if it
879 * were to be created.
880 * -R Create a pool under an alternate root
881 * -m Set default mountpoint for the root dataset. By default it's
883 * -t Use the temporary name until the pool is exported.
884 * -o Set property=value.
885 * -d Don't automatically enable all supported pool features
886 * (individual features can be enabled with -o).
887 * -O Set fsproperty=value in the pool's root file system
889 * Creates the named pool according to the given vdev specification. The
890 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
891 * we get the nvlist back from get_vdev_spec(), we either print out the contents
892 * (if '-n' was specified), or pass it to libzfs to do the creation.
895 #define SYSTEM256 (256 * 1024 * 1024)
897 zpool_do_create(int argc, char **argv)
899 boolean_t force = B_FALSE;
900 boolean_t dryrun = B_FALSE;
901 boolean_t enable_all_pool_feat = B_TRUE;
902 zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
903 uint64_t boot_size = 0;
905 nvlist_t *nvroot = NULL;
909 char *altroot = NULL;
910 char *mountpoint = NULL;
911 nvlist_t *fsprops = NULL;
912 nvlist_t *props = NULL;
916 while ((c = getopt(argc, argv, ":fndBR:m:o:O:t:")) != -1) {
925 enable_all_pool_feat = B_FALSE;
930 * We should create the system partition.
931 * Also make sure the size is set.
933 boot_type = ZPOOL_CREATE_BOOT_LABEL;
935 boot_size = SYSTEM256;
938 (void) fprintf(stderr,
939 gettext("option '%c' is not supported\n"),
945 if (add_prop_list(zpool_prop_to_name(
946 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
948 if (add_prop_list_default(zpool_prop_to_name(
949 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
953 /* Equivalent to -O mountpoint=optarg */
957 if ((propval = strchr(optarg, '=')) == NULL) {
958 (void) fprintf(stderr, gettext("missing "
959 "'=' for -o option\n"));
965 if (add_prop_list(optarg, propval, &props, B_TRUE))
969 * Get bootsize value for make_root_vdev().
971 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_BOOTSIZE) {
972 if (zfs_nicestrtonum(g_zfs, propval,
973 &boot_size) < 0 || boot_size == 0) {
974 (void) fprintf(stderr,
975 gettext("bad boot partition size "
976 "'%s': %s\n"), propval,
977 libzfs_error_description(g_zfs));
983 * If the user is creating a pool that doesn't support
984 * feature flags, don't enable any features.
986 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
990 ver = strtoull(propval, &end, 10);
992 ver < SPA_VERSION_FEATURES) {
993 enable_all_pool_feat = B_FALSE;
996 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1000 if ((propval = strchr(optarg, '=')) == NULL) {
1001 (void) fprintf(stderr, gettext("missing "
1002 "'=' for -O option\n"));
1009 * Mountpoints are checked and then added later.
1010 * Uniquely among properties, they can be specified
1011 * more than once, to avoid conflict with -m.
1013 if (0 == strcmp(optarg,
1014 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1015 mountpoint = propval;
1016 } else if (add_prop_list(optarg, propval, &fsprops,
1023 * Sanity check temporary pool name.
1025 if (strchr(optarg, '/') != NULL) {
1026 (void) fprintf(stderr, gettext("cannot create "
1027 "'%s': invalid character '/' in temporary "
1029 (void) fprintf(stderr, gettext("use 'zfs "
1030 "create' to create a dataset\n"));
1034 if (add_prop_list(zpool_prop_to_name(
1035 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1037 if (add_prop_list_default(zpool_prop_to_name(
1038 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1043 (void) fprintf(stderr, gettext("missing argument for "
1044 "'%c' option\n"), optopt);
1047 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1056 /* get pool name and check number of arguments */
1058 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1062 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1069 * As a special case, check for use of '/' in the name, and direct the
1070 * user to use 'zfs create' instead.
1072 if (strchr(poolname, '/') != NULL) {
1073 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1074 "character '/' in pool name\n"), poolname);
1075 (void) fprintf(stderr, gettext("use 'zfs create' to "
1076 "create a dataset\n"));
1081 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
1082 * and not set otherwise.
1084 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
1085 const char *propname;
1086 char *strptr, *buf = NULL;
1089 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
1090 if (nvlist_lookup_string(props, propname, &strptr) != 0) {
1091 (void) asprintf(&buf, "%" PRIu64, boot_size);
1093 (void) fprintf(stderr,
1094 gettext("internal error: out of memory\n"));
1097 rv = add_prop_list(propname, buf, &props, B_TRUE);
1103 const char *propname;
1106 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
1107 if (nvlist_lookup_string(props, propname, &strptr) == 0) {
1108 (void) fprintf(stderr, gettext("error: setting boot "
1109 "partition size requires option '-B'\n"));
1114 /* pass off to get_vdev_spec for bulk processing */
1115 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
1116 boot_type, boot_size, argc - 1, argv + 1);
1120 /* make_root_vdev() allows 0 toplevel children if there are spares */
1121 if (!zfs_allocatable_devs(nvroot)) {
1122 (void) fprintf(stderr, gettext("invalid vdev "
1123 "specification: at least one toplevel vdev must be "
1128 if (altroot != NULL && altroot[0] != '/') {
1129 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1130 "must be an absolute path\n"), altroot);
1135 * Check the validity of the mountpoint and direct the user to use the
1136 * '-m' mountpoint option if it looks like its in use.
1137 * Ignore the checks if the '-f' option is given.
1139 if (!force && (mountpoint == NULL ||
1140 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1141 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
1142 char buf[MAXPATHLEN];
1145 if (mountpoint && mountpoint[0] != '/') {
1146 (void) fprintf(stderr, gettext("invalid mountpoint "
1147 "'%s': must be an absolute path, 'legacy', or "
1148 "'none'\n"), mountpoint);
1152 if (mountpoint == NULL) {
1153 if (altroot != NULL)
1154 (void) snprintf(buf, sizeof (buf), "%s/%s",
1157 (void) snprintf(buf, sizeof (buf), "/%s",
1160 if (altroot != NULL)
1161 (void) snprintf(buf, sizeof (buf), "%s%s",
1162 altroot, mountpoint);
1164 (void) snprintf(buf, sizeof (buf), "%s",
1168 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1169 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1170 "%s\n"), buf, strerror(errno));
1171 (void) fprintf(stderr, gettext("use '-m' "
1172 "option to provide a different default\n"));
1177 while (count < 3 && readdir(dirp) != NULL)
1179 (void) closedir(dirp);
1182 (void) fprintf(stderr, gettext("mountpoint "
1183 "'%s' exists and is not empty\n"), buf);
1184 (void) fprintf(stderr, gettext("use '-m' "
1185 "option to provide a "
1186 "different default\n"));
1193 * Now that the mountpoint's validity has been checked, ensure that
1194 * the property is set appropriately prior to creating the pool.
1196 if (mountpoint != NULL) {
1197 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1198 mountpoint, &fsprops, B_FALSE);
1206 * For a dry run invocation, print out a basic message and run
1207 * through all the vdevs in the list and print out in an
1208 * appropriate hierarchy.
1210 (void) printf(gettext("would create '%s' with the "
1211 "following layout:\n\n"), poolname);
1213 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1214 if (num_logs(nvroot) > 0)
1215 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1220 * Hand off to libzfs.
1222 if (enable_all_pool_feat) {
1224 for (i = 0; i < SPA_FEATURES; i++) {
1225 char propname[MAXPATHLEN];
1226 zfeature_info_t *feat = &spa_feature_table[i];
1228 (void) snprintf(propname, sizeof (propname),
1229 "feature@%s", feat->fi_uname);
1232 * Skip feature if user specified it manually
1233 * on the command line.
1235 if (nvlist_exists(props, propname))
1238 ret = add_prop_list(propname,
1239 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1246 if (zpool_create(g_zfs, poolname,
1247 nvroot, props, fsprops) == 0) {
1248 zfs_handle_t *pool = zfs_open(g_zfs,
1249 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1251 if (zfs_mount(pool, NULL, 0) == 0)
1252 ret = zfs_shareall(pool);
1255 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1256 (void) fprintf(stderr, gettext("pool name may have "
1262 nvlist_free(nvroot);
1263 nvlist_free(fsprops);
1267 nvlist_free(fsprops);
1274 * zpool destroy <pool>
1276 * -f Forcefully unmount any datasets
1278 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1281 zpool_do_destroy(int argc, char **argv)
1283 boolean_t force = B_FALSE;
1286 zpool_handle_t *zhp;
1290 while ((c = getopt(argc, argv, "f")) != -1) {
1296 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1305 /* check arguments */
1307 (void) fprintf(stderr, gettext("missing pool argument\n"));
1311 (void) fprintf(stderr, gettext("too many arguments\n"));
1317 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1319 * As a special case, check for use of '/' in the name, and
1320 * direct the user to use 'zfs destroy' instead.
1322 if (strchr(pool, '/') != NULL)
1323 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1324 "destroy a dataset\n"));
1328 if (zpool_disable_datasets(zhp, force) != 0) {
1329 (void) fprintf(stderr, gettext("could not destroy '%s': "
1330 "could not unmount datasets\n"), zpool_get_name(zhp));
1334 /* The history must be logged as part of the export */
1335 log_history = B_FALSE;
1337 ret = (zpool_destroy(zhp, history_str) != 0);
1345 * zpool export [-f] <pool> ...
1347 * -f Forcefully unmount datasets
1349 * Export the given pools. By default, the command will attempt to cleanly
1350 * unmount any active datasets within the pool. If the '-f' flag is specified,
1351 * then the datasets will be forcefully unmounted.
1354 zpool_do_export(int argc, char **argv)
1356 boolean_t force = B_FALSE;
1357 boolean_t hardforce = B_FALSE;
1359 zpool_handle_t *zhp;
1364 while ((c = getopt(argc, argv, "fF")) != -1) {
1373 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1382 /* check arguments */
1384 (void) fprintf(stderr, gettext("missing pool argument\n"));
1389 for (i = 0; i < argc; i++) {
1390 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1395 if (zpool_disable_datasets(zhp, force) != 0) {
1401 /* The history must be logged as part of the export */
1402 log_history = B_FALSE;
1405 if (zpool_export_force(zhp, history_str) != 0)
1407 } else if (zpool_export(zhp, force, history_str) != 0) {
1418 * Given a vdev configuration, determine the maximum width needed for the device
1422 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1424 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1429 if (strlen(name) + depth > max)
1430 max = strlen(name) + depth;
1434 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1435 &child, &children) == 0) {
1436 for (c = 0; c < children; c++)
1437 if ((ret = max_width(zhp, child[c], depth + 2,
1442 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1443 &child, &children) == 0) {
1444 for (c = 0; c < children; c++)
1445 if ((ret = max_width(zhp, child[c], depth + 2,
1450 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1451 &child, &children) == 0) {
1452 for (c = 0; c < children; c++)
1453 if ((ret = max_width(zhp, child[c], depth + 2,
1462 typedef struct spare_cbdata {
1464 zpool_handle_t *cb_zhp;
1468 find_vdev(nvlist_t *nv, uint64_t search)
1474 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1478 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1479 &child, &children) == 0) {
1480 for (c = 0; c < children; c++)
1481 if (find_vdev(child[c], search))
1489 find_spare(zpool_handle_t *zhp, void *data)
1491 spare_cbdata_t *cbp = data;
1492 nvlist_t *config, *nvroot;
1494 config = zpool_get_config(zhp, NULL);
1495 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1498 if (find_vdev(nvroot, cbp->cb_guid)) {
1508 * Print out configuration state as requested by status_callback.
1511 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1512 int namewidth, int depth, boolean_t isspare)
1515 uint_t c, vsc, children;
1516 pool_scan_stat_t *ps = NULL;
1518 char rbuf[6], wbuf[6], cbuf[6];
1520 uint64_t notpresent;
1526 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1527 &child, &children) != 0)
1530 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1531 (uint64_t **)&vs, &vsc) == 0);
1533 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1535 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
1538 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1541 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1544 if (vs->vs_aux == VDEV_AUX_SPARED)
1546 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1550 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1554 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1555 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1556 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1557 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1560 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1561 ¬present) == 0 ||
1562 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1564 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1565 (void) printf(" was %s", path);
1566 } else if (vs->vs_aux != 0) {
1569 switch (vs->vs_aux) {
1570 case VDEV_AUX_OPEN_FAILED:
1571 (void) printf(gettext("cannot open"));
1574 case VDEV_AUX_BAD_GUID_SUM:
1575 (void) printf(gettext("missing device"));
1578 case VDEV_AUX_NO_REPLICAS:
1579 (void) printf(gettext("insufficient replicas"));
1582 case VDEV_AUX_VERSION_NEWER:
1583 (void) printf(gettext("newer version"));
1586 case VDEV_AUX_UNSUP_FEAT:
1587 (void) printf(gettext("unsupported feature(s)"));
1590 case VDEV_AUX_ASHIFT_TOO_BIG:
1591 (void) printf(gettext("unsupported minimum blocksize"));
1594 case VDEV_AUX_SPARED:
1595 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1597 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1598 if (strcmp(zpool_get_name(cb.cb_zhp),
1599 zpool_get_name(zhp)) == 0)
1600 (void) printf(gettext("currently in "
1603 (void) printf(gettext("in use by "
1605 zpool_get_name(cb.cb_zhp));
1606 zpool_close(cb.cb_zhp);
1608 (void) printf(gettext("currently in use"));
1612 case VDEV_AUX_ERR_EXCEEDED:
1613 (void) printf(gettext("too many errors"));
1616 case VDEV_AUX_IO_FAILURE:
1617 (void) printf(gettext("experienced I/O failures"));
1620 case VDEV_AUX_BAD_LOG:
1621 (void) printf(gettext("bad intent log"));
1624 case VDEV_AUX_EXTERNAL:
1625 (void) printf(gettext("external device fault"));
1628 case VDEV_AUX_SPLIT_POOL:
1629 (void) printf(gettext("split into new pool"));
1632 case VDEV_AUX_CHILDREN_OFFLINE:
1633 (void) printf(gettext("all children offline"));
1637 (void) printf(gettext("corrupted data"));
1640 } else if (children == 0 && !isspare &&
1641 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1642 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1644 gettext(" block size: %dB configured, %dB native"),
1645 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1648 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1649 (uint64_t **)&ps, &c);
1651 if (ps != NULL && ps->pss_state == DSS_SCANNING &&
1652 vs->vs_scan_processed != 0 && children == 0) {
1653 (void) printf(gettext(" (%s)"),
1654 (ps->pss_func == POOL_SCAN_RESILVER) ?
1655 "resilvering" : "repairing");
1658 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
1659 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
1660 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
1661 !vs->vs_scan_removing) {
1664 struct tm zaction_ts;
1666 time_t t = vs->vs_initialize_action_time;
1667 int initialize_pct = 100;
1668 if (vs->vs_initialize_state != VDEV_INITIALIZE_COMPLETE) {
1669 initialize_pct = (vs->vs_initialize_bytes_done * 100 /
1670 (vs->vs_initialize_bytes_est + 1));
1673 (void) localtime_r(&t, &zaction_ts);
1674 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
1676 switch (vs->vs_initialize_state) {
1677 case VDEV_INITIALIZE_SUSPENDED:
1678 (void) snprintf(zbuf, sizeof (zbuf),
1679 ", suspended, started at %s", tbuf);
1681 case VDEV_INITIALIZE_ACTIVE:
1682 (void) snprintf(zbuf, sizeof (zbuf),
1683 ", started at %s", tbuf);
1685 case VDEV_INITIALIZE_COMPLETE:
1686 (void) snprintf(zbuf, sizeof (zbuf),
1687 ", completed at %s", tbuf);
1691 (void) printf(gettext(" (%d%% initialized%s)"),
1692 initialize_pct, zbuf);
1695 (void) printf("\n");
1697 for (c = 0; c < children; c++) {
1698 uint64_t islog = B_FALSE, ishole = B_FALSE;
1700 /* Don't print logs or holes here */
1701 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1703 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1705 if (islog || ishole)
1707 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1708 print_status_config(zhp, vname, child[c],
1709 namewidth, depth + 2, isspare);
1716 * Print the configuration of an exported pool. Iterate over all vdevs in the
1717 * pool, printing out the name and status for each one.
1720 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1727 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1728 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1729 strcmp(type, VDEV_TYPE_HOLE) == 0)
1732 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1733 (uint64_t **)&vs, &c) == 0);
1735 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1736 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1738 if (vs->vs_aux != 0) {
1741 switch (vs->vs_aux) {
1742 case VDEV_AUX_OPEN_FAILED:
1743 (void) printf(gettext("cannot open"));
1746 case VDEV_AUX_BAD_GUID_SUM:
1747 (void) printf(gettext("missing device"));
1750 case VDEV_AUX_NO_REPLICAS:
1751 (void) printf(gettext("insufficient replicas"));
1754 case VDEV_AUX_VERSION_NEWER:
1755 (void) printf(gettext("newer version"));
1758 case VDEV_AUX_UNSUP_FEAT:
1759 (void) printf(gettext("unsupported feature(s)"));
1762 case VDEV_AUX_ERR_EXCEEDED:
1763 (void) printf(gettext("too many errors"));
1766 case VDEV_AUX_CHILDREN_OFFLINE:
1767 (void) printf(gettext("all children offline"));
1771 (void) printf(gettext("corrupted data"));
1775 (void) printf("\n");
1777 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1778 &child, &children) != 0)
1781 for (c = 0; c < children; c++) {
1782 uint64_t is_log = B_FALSE;
1784 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1789 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1790 print_import_config(vname, child[c], namewidth, depth + 2);
1794 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1795 &child, &children) == 0) {
1796 (void) printf(gettext("\tcache\n"));
1797 for (c = 0; c < children; c++) {
1798 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1799 (void) printf("\t %s\n", vname);
1804 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1805 &child, &children) == 0) {
1806 (void) printf(gettext("\tspares\n"));
1807 for (c = 0; c < children; c++) {
1808 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1809 (void) printf("\t %s\n", vname);
1817 * Logs are recorded as top level vdevs in the main pool child array
1818 * but with "is_log" set to 1. We use either print_status_config() or
1819 * print_import_config() to print the top level logs then any log
1820 * children (eg mirrored slogs) are printed recursively - which
1821 * works because only the top level vdev is marked "is_log"
1824 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1829 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1833 (void) printf(gettext("\tlogs\n"));
1835 for (c = 0; c < children; c++) {
1836 uint64_t is_log = B_FALSE;
1839 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1843 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1845 print_status_config(zhp, name, child[c], namewidth,
1848 print_import_config(name, child[c], namewidth, 2);
1854 * Display the status for the given pool.
1857 show_import(nvlist_t *config)
1859 uint64_t pool_state;
1871 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1873 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1875 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1877 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1880 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1881 (uint64_t **)&vs, &vsc) == 0);
1882 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1884 reason = zpool_import_status(config, &msgid);
1886 (void) printf(gettext(" pool: %s\n"), name);
1887 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1888 (void) printf(gettext(" state: %s"), health);
1889 if (pool_state == POOL_STATE_DESTROYED)
1890 (void) printf(gettext(" (DESTROYED)"));
1891 (void) printf("\n");
1894 case ZPOOL_STATUS_MISSING_DEV_R:
1895 case ZPOOL_STATUS_MISSING_DEV_NR:
1896 case ZPOOL_STATUS_BAD_GUID_SUM:
1897 (void) printf(gettext(" status: One or more devices are "
1898 "missing from the system.\n"));
1901 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1902 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1903 (void) printf(gettext(" status: One or more devices contains "
1904 "corrupted data.\n"));
1907 case ZPOOL_STATUS_CORRUPT_DATA:
1909 gettext(" status: The pool data is corrupted.\n"));
1912 case ZPOOL_STATUS_OFFLINE_DEV:
1913 (void) printf(gettext(" status: One or more devices "
1914 "are offlined.\n"));
1917 case ZPOOL_STATUS_CORRUPT_POOL:
1918 (void) printf(gettext(" status: The pool metadata is "
1922 case ZPOOL_STATUS_VERSION_OLDER:
1923 (void) printf(gettext(" status: The pool is formatted using a "
1924 "legacy on-disk version.\n"));
1927 case ZPOOL_STATUS_VERSION_NEWER:
1928 (void) printf(gettext(" status: The pool is formatted using an "
1929 "incompatible version.\n"));
1932 case ZPOOL_STATUS_FEAT_DISABLED:
1933 (void) printf(gettext(" status: Some supported features are "
1934 "not enabled on the pool.\n"));
1937 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1938 (void) printf(gettext("status: The pool uses the following "
1939 "feature(s) not supported on this system:\n"));
1940 zpool_print_unsup_feat(config);
1943 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1944 (void) printf(gettext("status: The pool can only be accessed "
1945 "in read-only mode on this system. It\n\tcannot be "
1946 "accessed in read-write mode because it uses the "
1947 "following\n\tfeature(s) not supported on this system:\n"));
1948 zpool_print_unsup_feat(config);
1951 case ZPOOL_STATUS_HOSTID_MISMATCH:
1952 (void) printf(gettext(" status: The pool was last accessed by "
1953 "another system.\n"));
1956 case ZPOOL_STATUS_FAULTED_DEV_R:
1957 case ZPOOL_STATUS_FAULTED_DEV_NR:
1958 (void) printf(gettext(" status: One or more devices are "
1962 case ZPOOL_STATUS_BAD_LOG:
1963 (void) printf(gettext(" status: An intent log record cannot be "
1967 case ZPOOL_STATUS_RESILVERING:
1968 (void) printf(gettext(" status: One or more devices were being "
1972 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
1973 (void) printf(gettext("status: One or more devices were "
1974 "configured to use a non-native block size.\n"
1975 "\tExpect reduced performance.\n"));
1980 * No other status can be seen when importing pools.
1982 assert(reason == ZPOOL_STATUS_OK);
1986 * Print out an action according to the overall state of the pool.
1988 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1989 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1990 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1991 (void) printf(gettext(" action: The pool can be "
1992 "imported using its name or numeric identifier, "
1993 "though\n\tsome features will not be available "
1994 "without an explicit 'zpool upgrade'.\n"));
1995 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1996 (void) printf(gettext(" action: The pool can be "
1997 "imported using its name or numeric "
1998 "identifier and\n\tthe '-f' flag.\n"));
2000 (void) printf(gettext(" action: The pool can be "
2001 "imported using its name or numeric "
2004 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2005 (void) printf(gettext(" action: The pool can be imported "
2006 "despite missing or damaged devices. The\n\tfault "
2007 "tolerance of the pool may be compromised if imported.\n"));
2010 case ZPOOL_STATUS_VERSION_NEWER:
2011 (void) printf(gettext(" action: The pool cannot be "
2012 "imported. Access the pool on a system running "
2013 "newer\n\tsoftware, or recreate the pool from "
2016 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2017 (void) printf(gettext("action: The pool cannot be "
2018 "imported. Access the pool on a system that "
2019 "supports\n\tthe required feature(s), or recreate "
2020 "the pool from backup.\n"));
2022 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2023 (void) printf(gettext("action: The pool cannot be "
2024 "imported in read-write mode. Import the pool "
2026 "\t\"-o readonly=on\", access the pool on a system "
2027 "that supports the\n\trequired feature(s), or "
2028 "recreate the pool from backup.\n"));
2030 case ZPOOL_STATUS_MISSING_DEV_R:
2031 case ZPOOL_STATUS_MISSING_DEV_NR:
2032 case ZPOOL_STATUS_BAD_GUID_SUM:
2033 (void) printf(gettext(" action: The pool cannot be "
2034 "imported. Attach the missing\n\tdevices and try "
2038 (void) printf(gettext(" action: The pool cannot be "
2039 "imported due to damaged devices or data.\n"));
2043 /* Print the comment attached to the pool. */
2044 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2045 (void) printf(gettext("comment: %s\n"), comment);
2048 * If the state is "closed" or "can't open", and the aux state
2049 * is "corrupt data":
2051 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2052 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2053 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2054 if (pool_state == POOL_STATE_DESTROYED)
2055 (void) printf(gettext("\tThe pool was destroyed, "
2056 "but can be imported using the '-Df' flags.\n"));
2057 else if (pool_state != POOL_STATE_EXPORTED)
2058 (void) printf(gettext("\tThe pool may be active on "
2059 "another system, but can be imported using\n\t"
2060 "the '-f' flag.\n"));
2064 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
2067 (void) printf(gettext(" config:\n\n"));
2069 namewidth = max_width(NULL, nvroot, 0, 0);
2073 print_import_config(name, nvroot, namewidth, 0);
2074 if (num_logs(nvroot) > 0)
2075 print_logs(NULL, nvroot, namewidth, B_FALSE);
2077 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2078 (void) printf(gettext("\n\tAdditional devices are known to "
2079 "be part of this pool, though their\n\texact "
2080 "configuration cannot be determined.\n"));
2085 * Perform the import for the given configuration. This passes the heavy
2086 * lifting off to zpool_import_props(), and then mounts the datasets contained
2090 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2091 nvlist_t *props, int flags)
2093 zpool_handle_t *zhp;
2098 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2101 verify(nvlist_lookup_uint64(config,
2102 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
2103 verify(nvlist_lookup_uint64(config,
2104 ZPOOL_CONFIG_VERSION, &version) == 0);
2105 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2106 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2107 "is formatted using an unsupported ZFS version\n"), name);
2109 } else if (state != POOL_STATE_EXPORTED &&
2110 !(flags & ZFS_IMPORT_ANY_HOST)) {
2113 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
2115 if ((unsigned long)hostid != gethostid()) {
2120 verify(nvlist_lookup_string(config,
2121 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2122 verify(nvlist_lookup_uint64(config,
2123 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
2125 (void) fprintf(stderr, gettext("cannot import "
2126 "'%s': pool may be in use from other "
2127 "system, it was last accessed by %s "
2128 "(hostid: 0x%lx) on %s"), name, hostname,
2129 (unsigned long)hostid,
2130 asctime(localtime(&t)));
2131 (void) fprintf(stderr, gettext("use '-f' to "
2132 "import anyway\n"));
2136 (void) fprintf(stderr, gettext("cannot import '%s': "
2137 "pool may be in use from other system\n"), name);
2138 (void) fprintf(stderr, gettext("use '-f' to import "
2144 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2147 if (newname != NULL)
2148 name = (char *)newname;
2150 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2153 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2154 !(flags & ZFS_IMPORT_ONLY) &&
2155 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2165 * zpool checkpoint <pool>
2166 * checkpoint --discard <pool>
2168 * -d Discard the checkpoint from a checkpointed
2171 * Checkpoints the specified pool, by taking a "snapshot" of its
2172 * current state. A pool can only have one checkpoint at a time.
2175 zpool_do_checkpoint(int argc, char **argv)
2179 zpool_handle_t *zhp;
2182 struct option long_options[] = {
2183 {"discard", no_argument, NULL, 'd'},
2188 while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
2194 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2204 (void) fprintf(stderr, gettext("missing pool argument\n"));
2209 (void) fprintf(stderr, gettext("too many arguments\n"));
2215 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
2216 /* As a special case, check for use of '/' in the name */
2217 if (strchr(pool, '/') != NULL)
2218 (void) fprintf(stderr, gettext("'zpool checkpoint' "
2219 "doesn't work on datasets. To save the state "
2220 "of a dataset from a specific point in time "
2221 "please use 'zfs snapshot'\n"));
2226 err = (zpool_discard_checkpoint(zhp) != 0);
2228 err = (zpool_checkpoint(zhp) != 0);
2235 #define CHECKPOINT_OPT 1024
2238 * zpool import [-d dir] [-D]
2239 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2240 * [-d dir | -c cachefile] [-f] -a
2241 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2242 * [-d dir | -c cachefile] [-f] [-n] [-F] [-t]
2243 * <pool | id> [newpool]
2245 * -c Read pool information from a cachefile instead of searching
2248 * -d Scan in a specific directory, other than /dev/dsk. More than
2249 * one directory can be specified using multiple '-d' options.
2251 * -D Scan for previously destroyed pools or import all or only
2252 * specified destroyed pools.
2254 * -R Temporarily import the pool, with all mountpoints relative to
2255 * the given root. The pool will remain exported when the machine
2258 * -V Import even in the presence of faulted vdevs. This is an
2259 * intentionally undocumented option for testing purposes, and
2260 * treats the pool configuration as complete, leaving any bad
2261 * vdevs in the FAULTED state. In other words, it does verbatim
2264 * -f Force import, even if it appears that the pool is active.
2266 * -F Attempt rewind if necessary.
2268 * -n See if rewind would work, but don't actually rewind.
2270 * -N Import the pool but don't mount datasets.
2272 * -t Use newpool as a temporary pool name instead of renaming
2275 * -T Specify a starting txg to use for import. This option is
2276 * intentionally undocumented option for testing purposes.
2278 * -a Import all pools found.
2280 * -o Set property=value and/or temporary mount options (without '=').
2282 * --rewind-to-checkpoint
2283 * Import the pool and revert back to the checkpoint.
2285 * The import command scans for pools to import, and import pools based on pool
2286 * name and GUID. The pool can also be renamed as part of the import process.
2289 zpool_do_import(int argc, char **argv)
2291 char **searchdirs = NULL;
2295 nvlist_t *pools = NULL;
2296 boolean_t do_all = B_FALSE;
2297 boolean_t do_destroyed = B_FALSE;
2298 char *mntopts = NULL;
2301 uint64_t searchguid = 0;
2302 char *searchname = NULL;
2304 nvlist_t *found_config;
2305 nvlist_t *policy = NULL;
2306 nvlist_t *props = NULL;
2308 int flags = ZFS_IMPORT_NORMAL;
2309 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2310 boolean_t dryrun = B_FALSE;
2311 boolean_t do_rewind = B_FALSE;
2312 boolean_t xtreme_rewind = B_FALSE;
2313 uint64_t pool_state, txg = -1ULL;
2314 char *cachefile = NULL;
2315 importargs_t idata = { 0 };
2319 struct option long_options[] = {
2320 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
2325 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFmnNo:rR:tT:VX",
2326 long_options, NULL)) != -1) {
2335 if (searchdirs == NULL) {
2336 searchdirs = safe_malloc(sizeof (char *));
2338 char **tmp = safe_malloc((nsearch + 1) *
2340 bcopy(searchdirs, tmp, nsearch *
2345 searchdirs[nsearch++] = optarg;
2348 do_destroyed = B_TRUE;
2351 flags |= ZFS_IMPORT_ANY_HOST;
2357 flags |= ZFS_IMPORT_MISSING_LOG;
2363 flags |= ZFS_IMPORT_ONLY;
2366 if ((propval = strchr(optarg, '=')) != NULL) {
2369 if (add_prop_list(optarg, propval,
2377 if (add_prop_list(zpool_prop_to_name(
2378 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2380 if (add_prop_list_default(zpool_prop_to_name(
2381 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2385 flags |= ZFS_IMPORT_TEMP_NAME;
2386 if (add_prop_list_default(zpool_prop_to_name(
2387 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2392 txg = strtoull(optarg, &endptr, 0);
2393 if (errno != 0 || *endptr != '\0') {
2394 (void) fprintf(stderr,
2395 gettext("invalid txg value\n"));
2398 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2401 flags |= ZFS_IMPORT_VERBATIM;
2404 xtreme_rewind = B_TRUE;
2406 case CHECKPOINT_OPT:
2407 flags |= ZFS_IMPORT_CHECKPOINT;
2410 (void) fprintf(stderr, gettext("missing argument for "
2411 "'%c' option\n"), optopt);
2415 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2424 if (cachefile && nsearch != 0) {
2425 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2429 if ((dryrun || xtreme_rewind) && !do_rewind) {
2430 (void) fprintf(stderr,
2431 gettext("-n or -X only meaningful with -F\n"));
2435 rewind_policy = ZPOOL_TRY_REWIND;
2437 rewind_policy = ZPOOL_DO_REWIND;
2439 rewind_policy |= ZPOOL_EXTREME_REWIND;
2441 /* In the future, we can capture further policy and include it here */
2442 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2443 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
2444 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
2445 rewind_policy) != 0)
2448 if (searchdirs == NULL) {
2449 searchdirs = safe_malloc(sizeof (char *));
2450 searchdirs[0] = "/dev";
2454 /* check argument count */
2457 (void) fprintf(stderr, gettext("too many arguments\n"));
2462 (void) fprintf(stderr, gettext("too many arguments\n"));
2467 * Check for the SYS_CONFIG privilege. We do this explicitly
2468 * here because otherwise any attempt to discover pools will
2471 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2472 (void) fprintf(stderr, gettext("cannot "
2473 "discover pools: permission denied\n"));
2475 nvlist_free(policy);
2481 * Depending on the arguments given, we do one of the following:
2483 * <none> Iterate through all pools and display information about
2486 * -a Iterate through all pools and try to import each one.
2488 * <id> Find the pool that corresponds to the given GUID/pool
2489 * name and import that one.
2491 * -D Above options applies only to destroyed pools.
2497 searchguid = strtoull(argv[0], &endptr, 10);
2498 if (errno != 0 || *endptr != '\0') {
2499 searchname = argv[0];
2502 found_config = NULL;
2505 * User specified a name or guid. Ensure it's unique.
2507 idata.unique = B_TRUE;
2511 idata.path = searchdirs;
2512 idata.paths = nsearch;
2513 idata.poolname = searchname;
2514 idata.guid = searchguid;
2515 idata.cachefile = cachefile;
2516 idata.policy = policy;
2518 pools = zpool_search_import(g_zfs, &idata);
2520 if (pools != NULL && idata.exists &&
2521 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2522 (void) fprintf(stderr, gettext("cannot import '%s': "
2523 "a pool with that name already exists\n"),
2525 (void) fprintf(stderr, gettext("use the form 'zpool import "
2526 "[-t] <pool | id> <newpool>' to give it a new temporary "
2527 "or permanent name\n"));
2529 } else if (pools == NULL && idata.exists) {
2530 (void) fprintf(stderr, gettext("cannot import '%s': "
2531 "a pool with that name is already created/imported,\n"),
2533 (void) fprintf(stderr, gettext("and no additional pools "
2534 "with that name were found\n"));
2536 } else if (pools == NULL) {
2538 (void) fprintf(stderr, gettext("cannot import '%s': "
2539 "no such pool available\n"), argv[0]);
2546 nvlist_free(policy);
2551 * At this point we have a list of import candidate configs. Even if
2552 * we were searching by pool name or guid, we still need to
2553 * post-process the list to deal with pool state and possible
2559 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2561 verify(nvpair_value_nvlist(elem, &config) == 0);
2563 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2565 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2567 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2570 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
2577 (void) printf("\n");
2580 err |= do_import(config, NULL, mntopts,
2583 show_import(config);
2585 } else if (searchname != NULL) {
2589 * We are searching for a pool based on name.
2591 verify(nvlist_lookup_string(config,
2592 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2594 if (strcmp(name, searchname) == 0) {
2595 if (found_config != NULL) {
2596 (void) fprintf(stderr, gettext(
2597 "cannot import '%s': more than "
2598 "one matching pool\n"), searchname);
2599 (void) fprintf(stderr, gettext(
2600 "import by numeric ID instead\n"));
2603 found_config = config;
2609 * Search for a pool by guid.
2611 verify(nvlist_lookup_uint64(config,
2612 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2614 if (guid == searchguid)
2615 found_config = config;
2620 * If we were searching for a specific pool, verify that we found a
2621 * pool, and then do the import.
2623 if (argc != 0 && err == 0) {
2624 if (found_config == NULL) {
2625 (void) fprintf(stderr, gettext("cannot import '%s': "
2626 "no such pool available\n"), argv[0]);
2629 err |= do_import(found_config, argc == 1 ? NULL :
2630 argv[1], mntopts, props, flags);
2635 * If we were just looking for pools, report an error if none were
2638 if (argc == 0 && first)
2639 (void) fprintf(stderr,
2640 gettext("no pools available to import\n"));
2645 nvlist_free(policy);
2648 return (err ? 1 : 0);
2651 typedef struct iostat_cbdata {
2652 boolean_t cb_verbose;
2655 zpool_list_t *cb_list;
2659 print_iostat_separator(iostat_cbdata_t *cb)
2663 for (i = 0; i < cb->cb_namewidth; i++)
2665 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2669 print_iostat_header(iostat_cbdata_t *cb)
2671 (void) printf("%*s capacity operations bandwidth\n",
2672 cb->cb_namewidth, "");
2673 (void) printf("%-*s alloc free read write read write\n",
2674 cb->cb_namewidth, "pool");
2675 print_iostat_separator(cb);
2679 * Display a single statistic.
2682 print_one_stat(uint64_t value)
2686 zfs_nicenum(value, buf, sizeof (buf));
2687 (void) printf(" %5s", buf);
2691 * Print out all the statistics for the given vdev. This can either be the
2692 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2693 * is a verbose output, and we don't want to display the toplevel pool stats.
2696 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2697 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2699 nvlist_t **oldchild, **newchild;
2701 vdev_stat_t *oldvs, *newvs;
2702 vdev_stat_t zerovs = { 0 };
2707 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
2710 if (oldnv != NULL) {
2711 verify(nvlist_lookup_uint64_array(oldnv,
2712 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2717 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2718 (uint64_t **)&newvs, &c) == 0);
2720 if (strlen(name) + depth > cb->cb_namewidth)
2721 (void) printf("%*s%s", depth, "", name);
2723 (void) printf("%*s%s%*s", depth, "", name,
2724 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2726 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2731 scale = (double)NANOSEC / tdelta;
2733 /* only toplevel vdevs have capacity stats */
2734 if (newvs->vs_space == 0) {
2735 (void) printf(" - -");
2737 print_one_stat(newvs->vs_alloc);
2738 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2741 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2742 oldvs->vs_ops[ZIO_TYPE_READ])));
2744 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2745 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2747 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2748 oldvs->vs_bytes[ZIO_TYPE_READ])));
2750 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2751 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2753 (void) printf("\n");
2755 if (!cb->cb_verbose)
2758 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2759 &newchild, &children) != 0)
2762 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2763 &oldchild, &c) != 0)
2766 for (c = 0; c < children; c++) {
2767 uint64_t ishole = B_FALSE, islog = B_FALSE;
2769 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2772 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2775 if (ishole || islog)
2778 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2779 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2780 newchild[c], cb, depth + 2);
2785 * Log device section
2788 if (num_logs(newnv) > 0) {
2789 (void) printf("%-*s - - - - - "
2790 "-\n", cb->cb_namewidth, "logs");
2792 for (c = 0; c < children; c++) {
2793 uint64_t islog = B_FALSE;
2794 (void) nvlist_lookup_uint64(newchild[c],
2795 ZPOOL_CONFIG_IS_LOG, &islog);
2798 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2800 print_vdev_stats(zhp, vname, oldnv ?
2801 oldchild[c] : NULL, newchild[c],
2810 * Include level 2 ARC devices in iostat output
2812 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2813 &newchild, &children) != 0)
2816 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2817 &oldchild, &c) != 0)
2821 (void) printf("%-*s - - - - - "
2822 "-\n", cb->cb_namewidth, "cache");
2823 for (c = 0; c < children; c++) {
2824 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2826 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2827 newchild[c], cb, depth + 2);
2834 refresh_iostat(zpool_handle_t *zhp, void *data)
2836 iostat_cbdata_t *cb = data;
2840 * If the pool has disappeared, remove it from the list and continue.
2842 if (zpool_refresh_stats(zhp, &missing) != 0)
2846 pool_list_remove(cb->cb_list, zhp);
2852 * Callback to print out the iostats for the given pool.
2855 print_iostat(zpool_handle_t *zhp, void *data)
2857 iostat_cbdata_t *cb = data;
2858 nvlist_t *oldconfig, *newconfig;
2859 nvlist_t *oldnvroot, *newnvroot;
2861 newconfig = zpool_get_config(zhp, &oldconfig);
2863 if (cb->cb_iteration == 1)
2866 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2869 if (oldconfig == NULL)
2872 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2876 * Print out the statistics for the pool.
2878 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2881 print_iostat_separator(cb);
2887 get_namewidth(zpool_handle_t *zhp, void *data)
2889 iostat_cbdata_t *cb = data;
2890 nvlist_t *config, *nvroot;
2892 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2893 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2895 if (!cb->cb_verbose)
2896 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2898 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2903 * The width must fall into the range [10,38]. The upper limit is the
2904 * maximum we can have and still fit in 80 columns.
2906 if (cb->cb_namewidth < 10)
2907 cb->cb_namewidth = 10;
2908 if (cb->cb_namewidth > 38)
2909 cb->cb_namewidth = 38;
2915 * Parse the input string, get the 'interval' and 'count' value if there is one.
2918 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2921 unsigned long interval = 0, count = 0;
2922 int argc = *argcp, errno;
2925 * Determine if the last argument is an integer or a pool name
2927 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2931 interval = strtoul(argv[argc - 1], &end, 10);
2933 if (*end == '\0' && errno == 0) {
2934 if (interval == 0) {
2935 (void) fprintf(stderr, gettext("interval "
2936 "cannot be zero\n"));
2940 * Ignore the last parameter
2945 * If this is not a valid number, just plow on. The
2946 * user will get a more informative error message later
2954 * If the last argument is also an integer, then we have both a count
2957 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2962 interval = strtoul(argv[argc - 1], &end, 10);
2964 if (*end == '\0' && errno == 0) {
2965 if (interval == 0) {
2966 (void) fprintf(stderr, gettext("interval "
2967 "cannot be zero\n"));
2972 * Ignore the last parameter
2986 get_timestamp_arg(char c)
2989 timestamp_fmt = UDATE;
2991 timestamp_fmt = DDATE;
2997 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2999 * -v Display statistics for individual vdevs
3000 * -T Display a timestamp in date(1) or Unix format
3002 * This command can be tricky because we want to be able to deal with pool
3003 * creation/destruction as well as vdev configuration changes. The bulk of this
3004 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
3005 * on pool_list_update() to detect the addition of new pools. Configuration
3006 * changes are all handled within libzfs.
3009 zpool_do_iostat(int argc, char **argv)
3014 unsigned long interval = 0, count = 0;
3016 boolean_t verbose = B_FALSE;
3020 while ((c = getopt(argc, argv, "T:v")) != -1) {
3023 get_timestamp_arg(*optarg);
3029 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3038 get_interval_count(&argc, argv, &interval, &count);
3041 * Construct the list of all interesting pools.
3044 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
3047 if (pool_list_count(list) == 0 && argc != 0) {
3048 pool_list_free(list);
3052 if (pool_list_count(list) == 0 && interval == 0) {
3053 pool_list_free(list);
3054 (void) fprintf(stderr, gettext("no pools available\n"));
3059 * Enter the main iostat loop.
3062 cb.cb_verbose = verbose;
3063 cb.cb_iteration = 0;
3064 cb.cb_namewidth = 0;
3067 pool_list_update(list);
3069 if ((npools = pool_list_count(list)) == 0)
3073 * Refresh all statistics. This is done as an explicit step
3074 * before calculating the maximum name width, so that any
3075 * configuration changes are properly accounted for.
3077 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
3080 * Iterate over all pools to determine the maximum width
3081 * for the pool / device name column across all pools.
3083 cb.cb_namewidth = 0;
3084 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3086 if (timestamp_fmt != NODATE)
3087 print_timestamp(timestamp_fmt);
3090 * If it's the first time, or verbose mode, print the header.
3092 if (++cb.cb_iteration == 1 || verbose)
3093 print_iostat_header(&cb);
3095 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
3098 * If there's more than one pool, and we're not in verbose mode
3099 * (which prints a separator for us), then print a separator.
3101 if (npools > 1 && !verbose)
3102 print_iostat_separator(&cb);
3105 (void) printf("\n");
3108 * Flush the output so that redirection to a file isn't buffered
3111 (void) fflush(stdout);
3116 if (count != 0 && --count == 0)
3119 (void) sleep(interval);
3122 pool_list_free(list);
3127 typedef struct list_cbdata {
3128 boolean_t cb_verbose;
3130 boolean_t cb_scripted;
3131 zprop_list_t *cb_proplist;
3132 boolean_t cb_literal;
3136 * Given a list of columns to display, output appropriate headers for each one.
3139 print_header(list_cbdata_t *cb)
3141 zprop_list_t *pl = cb->cb_proplist;
3142 char headerbuf[ZPOOL_MAXPROPLEN];
3144 boolean_t first = B_TRUE;
3145 boolean_t right_justify;
3148 for (; pl != NULL; pl = pl->pl_next) {
3149 width = pl->pl_width;
3150 if (first && cb->cb_verbose) {
3152 * Reset the width to accommodate the verbose listing
3155 width = cb->cb_namewidth;
3163 right_justify = B_FALSE;
3164 if (pl->pl_prop != ZPROP_INVAL) {
3165 header = zpool_prop_column_name(pl->pl_prop);
3166 right_justify = zpool_prop_align_right(pl->pl_prop);
3170 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3171 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3172 headerbuf[i] = '\0';
3176 if (pl->pl_next == NULL && !right_justify)
3177 (void) printf("%s", header);
3178 else if (right_justify)
3179 (void) printf("%*s", width, header);
3181 (void) printf("%-*s", width, header);
3185 (void) printf("\n");
3189 * Given a pool and a list of properties, print out all the properties according
3190 * to the described layout.
3193 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
3195 zprop_list_t *pl = cb->cb_proplist;
3196 boolean_t first = B_TRUE;
3197 char property[ZPOOL_MAXPROPLEN];
3199 boolean_t right_justify;
3202 for (; pl != NULL; pl = pl->pl_next) {
3204 width = pl->pl_width;
3205 if (first && cb->cb_verbose) {
3207 * Reset the width to accommodate the verbose listing
3210 width = cb->cb_namewidth;
3214 if (cb->cb_scripted)
3215 (void) printf("\t");
3222 right_justify = B_FALSE;
3223 if (pl->pl_prop != ZPROP_INVAL) {
3224 if (zpool_get_prop(zhp, pl->pl_prop, property,
3225 sizeof (property), NULL, cb->cb_literal) != 0)
3230 right_justify = zpool_prop_align_right(pl->pl_prop);
3231 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
3232 zpool_prop_unsupported(pl->pl_user_prop)) &&
3233 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3234 sizeof (property)) == 0) {
3242 * If this is being called in scripted mode, or if this is the
3243 * last column and it is left-justified, don't include a width
3246 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3247 (void) printf("%s", propstr);
3248 else if (right_justify)
3249 (void) printf("%*s", width, propstr);
3251 (void) printf("%-*s", width, propstr);
3254 (void) printf("\n");
3258 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3263 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3266 case ZPOOL_PROP_EXPANDSZ:
3267 case ZPOOL_PROP_CHECKPOINT:
3269 (void) strlcpy(propval, "-", sizeof (propval));
3271 zfs_nicenum(value, propval, sizeof (propval));
3273 case ZPOOL_PROP_FRAGMENTATION:
3274 if (value == ZFS_FRAG_INVALID) {
3275 (void) strlcpy(propval, "-", sizeof (propval));
3277 (void) snprintf(propval, sizeof (propval), "%llu%%",
3281 case ZPOOL_PROP_CAPACITY:
3282 (void) snprintf(propval, sizeof (propval), "%llu%%", value);
3285 zfs_nicenum(value, propval, sizeof (propval));
3289 (void) strlcpy(propval, "-", sizeof (propval));
3292 (void) printf("\t%s", propval);
3294 (void) printf(" %*s", width, propval);
3298 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3299 list_cbdata_t *cb, int depth)
3305 boolean_t scripted = cb->cb_scripted;
3306 uint64_t islog = B_FALSE;
3307 boolean_t haslog = B_FALSE;
3308 char *dashes = "%-*s - - - - - -\n";
3310 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3311 (uint64_t **)&vs, &c) == 0);
3314 boolean_t toplevel = (vs->vs_space != 0);
3317 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
3321 (void) printf("\t%s", name);
3322 else if (strlen(name) + depth > cb->cb_namewidth)
3323 (void) printf("%*s%s", depth, "", name);
3325 (void) printf("%*s%s%*s", depth, "", name,
3326 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3329 * Print the properties for the individual vdevs. Some
3330 * properties are only applicable to toplevel vdevs. The
3331 * 'toplevel' boolean value is passed to the print_one_column()
3332 * to indicate that the value is valid.
3334 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3336 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3338 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3339 scripted, toplevel);
3340 print_one_column(ZPOOL_PROP_CHECKPOINT,
3341 vs->vs_checkpoint_space, scripted, toplevel);
3342 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3344 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3345 vs->vs_fragmentation, scripted,
3346 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3347 cap = (vs->vs_space == 0) ? 0 :
3348 (vs->vs_alloc * 100 / vs->vs_space);
3349 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3350 (void) printf("\n");
3353 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3354 &child, &children) != 0)
3357 for (c = 0; c < children; c++) {
3358 uint64_t ishole = B_FALSE;
3360 if (nvlist_lookup_uint64(child[c],
3361 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3364 if (nvlist_lookup_uint64(child[c],
3365 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
3370 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3371 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3375 if (haslog == B_TRUE) {
3376 /* LINTED E_SEC_PRINTF_VAR_FMT */
3377 (void) printf(dashes, cb->cb_namewidth, "log");
3378 for (c = 0; c < children; c++) {
3379 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3380 &islog) != 0 || !islog)
3382 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3383 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3388 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3389 &child, &children) == 0 && children > 0) {
3390 /* LINTED E_SEC_PRINTF_VAR_FMT */
3391 (void) printf(dashes, cb->cb_namewidth, "cache");
3392 for (c = 0; c < children; c++) {
3393 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3394 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3399 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
3400 &children) == 0 && children > 0) {
3401 /* LINTED E_SEC_PRINTF_VAR_FMT */
3402 (void) printf(dashes, cb->cb_namewidth, "spare");
3403 for (c = 0; c < children; c++) {
3404 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3405 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3413 * Generic callback function to list a pool.
3416 list_callback(zpool_handle_t *zhp, void *data)
3418 list_cbdata_t *cbp = data;
3422 config = zpool_get_config(zhp, NULL);
3424 print_pool(zhp, cbp);
3425 if (!cbp->cb_verbose)
3428 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3430 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3436 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3438 * -H Scripted mode. Don't display headers, and separate properties
3440 * -o List of properties to display. Defaults to
3441 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3442 * "dedupratio,health,altroot"
3443 * -p Diplay values in parsable (exact) format.
3444 * -T Display a timestamp in date(1) or Unix format
3446 * List all pools in the system, whether or not they're healthy. Output space
3447 * statistics for each one, as well as health status summary.
3450 zpool_do_list(int argc, char **argv)
3454 list_cbdata_t cb = { 0 };
3455 static char default_props[] =
3456 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
3457 "capacity,dedupratio,health,altroot";
3458 char *props = default_props;
3459 unsigned long interval = 0, count = 0;
3461 boolean_t first = B_TRUE;
3464 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3467 cb.cb_scripted = B_TRUE;
3473 cb.cb_literal = B_TRUE;
3476 get_timestamp_arg(*optarg);
3479 cb.cb_verbose = B_TRUE;
3482 (void) fprintf(stderr, gettext("missing argument for "
3483 "'%c' option\n"), optopt);
3487 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3496 get_interval_count(&argc, argv, &interval, &count);
3498 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3502 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3506 if (pool_list_count(list) == 0)
3509 cb.cb_namewidth = 0;
3510 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3512 if (timestamp_fmt != NODATE)
3513 print_timestamp(timestamp_fmt);
3515 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3519 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3524 if (count != 0 && --count == 0)
3527 pool_list_free(list);
3528 (void) sleep(interval);
3531 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3532 (void) printf(gettext("no pools available\n"));
3536 pool_list_free(list);
3537 zprop_free_list(cb.cb_proplist);
3542 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3544 boolean_t force = B_FALSE;
3547 char *poolname, *old_disk, *new_disk;
3548 zpool_handle_t *zhp;
3549 zpool_boot_label_t boot_type;
3554 while ((c = getopt(argc, argv, "f")) != -1) {
3560 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3569 /* get pool name and check number of arguments */
3571 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3578 (void) fprintf(stderr,
3579 gettext("missing <device> specification\n"));
3587 (void) fprintf(stderr,
3588 gettext("missing <new_device> specification\n"));
3591 new_disk = old_disk;
3601 (void) fprintf(stderr, gettext("too many arguments\n"));
3605 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3608 if (zpool_get_config(zhp, NULL) == NULL) {
3609 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3615 if (zpool_is_bootable(zhp))
3616 boot_type = ZPOOL_COPY_BOOT_LABEL;
3618 boot_type = ZPOOL_NO_BOOT_LABEL;
3620 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3621 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3622 boot_type, boot_size, argc, argv);
3623 if (nvroot == NULL) {
3628 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3630 nvlist_free(nvroot);
3637 * zpool replace [-f] <pool> <device> <new_device>
3639 * -f Force attach, even if <new_device> appears to be in use.
3641 * Replace <device> with <new_device>.
3645 zpool_do_replace(int argc, char **argv)
3647 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3651 * zpool attach [-f] <pool> <device> <new_device>
3653 * -f Force attach, even if <new_device> appears to be in use.
3655 * Attach <new_device> to the mirror containing <device>. If <device> is not
3656 * part of a mirror, then <device> will be transformed into a mirror of
3657 * <device> and <new_device>. In either case, <new_device> will begin life
3658 * with a DTL of [0, now], and will immediately begin to resilver itself.
3661 zpool_do_attach(int argc, char **argv)
3663 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3667 * zpool detach [-f] <pool> <device>
3669 * -f Force detach of <device>, even if DTLs argue against it
3670 * (not supported yet)
3672 * Detach a device from a mirror. The operation will be refused if <device>
3673 * is the last device in the mirror, or if the DTLs indicate that this device
3674 * has the only valid copy of some data.
3678 zpool_do_detach(int argc, char **argv)
3681 char *poolname, *path;
3682 zpool_handle_t *zhp;
3686 while ((c = getopt(argc, argv, "f")) != -1) {
3690 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3699 /* get pool name and check number of arguments */
3701 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3706 (void) fprintf(stderr,
3707 gettext("missing <device> specification\n"));
3714 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3717 ret = zpool_vdev_detach(zhp, path);
3725 * zpool split [-n] [-o prop=val] ...
3727 * [-R altroot] <pool> <newpool> [<device> ...]
3729 * -n Do not split the pool, but display the resulting layout if
3730 * it were to be split.
3731 * -o Set property=value, or set mount options.
3732 * -R Mount the split-off pool under an alternate root.
3734 * Splits the named pool and gives it the new pool name. Devices to be split
3735 * off may be listed, provided that no more than one device is specified
3736 * per top-level vdev mirror. The newly split pool is left in an exported
3737 * state unless -R is specified.
3739 * Restrictions: the top-level of the pool pool must only be made up of
3740 * mirrors; all devices in the pool must be healthy; no device may be
3741 * undergoing a resilvering operation.
3744 zpool_do_split(int argc, char **argv)
3746 char *srcpool, *newpool, *propval;
3747 char *mntopts = NULL;
3750 zpool_handle_t *zhp;
3751 nvlist_t *config, *props = NULL;
3753 flags.dryrun = B_FALSE;
3754 flags.import = B_FALSE;
3757 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3760 flags.import = B_TRUE;
3762 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3763 &props, B_TRUE) != 0) {
3769 flags.dryrun = B_TRUE;
3772 if ((propval = strchr(optarg, '=')) != NULL) {
3775 if (add_prop_list(optarg, propval,
3776 &props, B_TRUE) != 0) {
3785 (void) fprintf(stderr, gettext("missing argument for "
3786 "'%c' option\n"), optopt);
3790 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3797 if (!flags.import && mntopts != NULL) {
3798 (void) fprintf(stderr, gettext("setting mntopts is only "
3799 "valid when importing the pool\n"));
3807 (void) fprintf(stderr, gettext("Missing pool name\n"));
3811 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3821 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3824 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3825 if (config == NULL) {
3829 (void) printf(gettext("would create '%s' with the "
3830 "following layout:\n\n"), newpool);
3831 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3833 nvlist_free(config);
3838 if (ret != 0 || flags.dryrun || !flags.import)
3842 * The split was successful. Now we need to open the new
3843 * pool and import it.
3845 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3847 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3848 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3850 (void) fprintf(stderr, gettext("Split was successful, but "
3851 "the datasets could not all be mounted\n"));
3852 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3853 "different altroot\n"), "zpool import");
3863 * zpool online <pool> <device> ...
3866 zpool_do_online(int argc, char **argv)
3870 zpool_handle_t *zhp;
3872 vdev_state_t newstate;
3876 while ((c = getopt(argc, argv, "et")) != -1) {
3879 flags |= ZFS_ONLINE_EXPAND;
3883 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3892 /* get pool name and check number of arguments */
3894 (void) fprintf(stderr, gettext("missing pool name\n"));
3898 (void) fprintf(stderr, gettext("missing device name\n"));
3904 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3907 for (i = 1; i < argc; i++) {
3908 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3909 if (newstate != VDEV_STATE_HEALTHY) {
3910 (void) printf(gettext("warning: device '%s' "
3911 "onlined, but remains in faulted state\n"),
3913 if (newstate == VDEV_STATE_FAULTED)
3914 (void) printf(gettext("use 'zpool "
3915 "clear' to restore a faulted "
3918 (void) printf(gettext("use 'zpool "
3919 "replace' to replace devices "
3920 "that are no longer present\n"));
3933 * zpool offline [-ft] <pool> <device> ...
3935 * -f Force the device into the offline state, even if doing
3936 * so would appear to compromise pool availability.
3937 * (not supported yet)
3939 * -t Only take the device off-line temporarily. The offline
3940 * state will not be persistent across reboots.
3944 zpool_do_offline(int argc, char **argv)
3948 zpool_handle_t *zhp;
3950 boolean_t istmp = B_FALSE;
3953 while ((c = getopt(argc, argv, "ft")) != -1) {
3960 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3969 /* get pool name and check number of arguments */
3971 (void) fprintf(stderr, gettext("missing pool name\n"));
3975 (void) fprintf(stderr, gettext("missing device name\n"));
3981 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3984 for (i = 1; i < argc; i++) {
3985 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3995 * zpool clear <pool> [device]
3997 * Clear all errors associated with a pool or a particular device.
4000 zpool_do_clear(int argc, char **argv)
4004 boolean_t dryrun = B_FALSE;
4005 boolean_t do_rewind = B_FALSE;
4006 boolean_t xtreme_rewind = B_FALSE;
4007 uint32_t rewind_policy = ZPOOL_NO_REWIND;
4008 nvlist_t *policy = NULL;
4009 zpool_handle_t *zhp;
4010 char *pool, *device;
4013 while ((c = getopt(argc, argv, "FnX")) != -1) {
4022 xtreme_rewind = B_TRUE;
4025 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4035 (void) fprintf(stderr, gettext("missing pool name\n"));
4040 (void) fprintf(stderr, gettext("too many arguments\n"));
4044 if ((dryrun || xtreme_rewind) && !do_rewind) {
4045 (void) fprintf(stderr,
4046 gettext("-n or -X only meaningful with -F\n"));
4050 rewind_policy = ZPOOL_TRY_REWIND;
4052 rewind_policy = ZPOOL_DO_REWIND;
4054 rewind_policy |= ZPOOL_EXTREME_REWIND;
4056 /* In future, further rewind policy choices can be passed along here */
4057 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
4058 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
4059 rewind_policy) != 0) {
4064 device = argc == 2 ? argv[1] : NULL;
4066 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
4067 nvlist_free(policy);
4071 if (zpool_clear(zhp, device, policy) != 0)
4076 nvlist_free(policy);
4082 * zpool reguid <pool>
4085 zpool_do_reguid(int argc, char **argv)
4089 zpool_handle_t *zhp;
4093 while ((c = getopt(argc, argv, "")) != -1) {
4096 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4105 /* get pool name and check number of arguments */
4107 (void) fprintf(stderr, gettext("missing pool name\n"));
4112 (void) fprintf(stderr, gettext("too many arguments\n"));
4117 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4120 ret = zpool_reguid(zhp);
4128 * zpool reopen <pool>
4130 * Reopen the pool so that the kernel can update the sizes of all vdevs.
4133 zpool_do_reopen(int argc, char **argv)
4137 zpool_handle_t *zhp;
4141 while ((c = getopt(argc, argv, "")) != -1) {
4144 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4154 (void) fprintf(stderr, gettext("missing pool name\n"));
4159 (void) fprintf(stderr, gettext("too many arguments\n"));
4164 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
4167 ret = zpool_reopen(zhp);
4172 typedef struct scrub_cbdata {
4176 pool_scrub_cmd_t cb_scrub_cmd;
4180 zpool_has_checkpoint(zpool_handle_t *zhp)
4182 nvlist_t *config, *nvroot;
4184 config = zpool_get_config(zhp, NULL);
4186 if (config != NULL) {
4187 pool_checkpoint_stat_t *pcs = NULL;
4190 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
4191 (void) nvlist_lookup_uint64_array(nvroot,
4192 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
4194 if (pcs == NULL || pcs->pcs_state == CS_NONE)
4197 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
4198 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
4206 scrub_callback(zpool_handle_t *zhp, void *data)
4208 scrub_cbdata_t *cb = data;
4212 * Ignore faulted pools.
4214 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4215 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
4216 "currently unavailable\n"), zpool_get_name(zhp));
4220 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
4222 if (err == 0 && zpool_has_checkpoint(zhp) &&
4223 cb->cb_type == POOL_SCAN_SCRUB) {
4224 (void) printf(gettext("warning: will not scrub state that "
4225 "belongs to the checkpoint of pool '%s'\n"),
4226 zpool_get_name(zhp));
4233 * zpool scrub [-s | -p] <pool> ...
4235 * -s Stop. Stops any in-progress scrub.
4236 * -p Pause. Pause in-progress scrub.
4239 zpool_do_scrub(int argc, char **argv)
4244 cb.cb_type = POOL_SCAN_SCRUB;
4245 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
4248 while ((c = getopt(argc, argv, "sp")) != -1) {
4251 cb.cb_type = POOL_SCAN_NONE;
4254 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
4257 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4263 if (cb.cb_type == POOL_SCAN_NONE &&
4264 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
4265 (void) fprintf(stderr, gettext("invalid option combination: "
4266 "-s and -p are mutually exclusive\n"));
4276 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4280 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4284 zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
4286 uint_t children = 0;
4290 (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4293 if (children == 0) {
4294 char *path = zpool_vdev_name(g_zfs, zhp, nvroot, B_FALSE);
4295 fnvlist_add_boolean(res, path);
4300 for (i = 0; i < children; i++) {
4301 zpool_collect_leaves(zhp, child[i], res);
4306 * zpool initialize [-cs] <pool> [<vdev> ...]
4307 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
4308 * if none specified.
4310 * -c Cancel. Ends active initializing.
4311 * -s Suspend. Initializing can then be restarted with no flags.
4314 zpool_do_initialize(int argc, char **argv)
4318 zpool_handle_t *zhp;
4322 struct option long_options[] = {
4323 {"cancel", no_argument, NULL, 'c'},
4324 {"suspend", no_argument, NULL, 's'},
4328 pool_initialize_func_t cmd_type = POOL_INITIALIZE_DO;
4329 while ((c = getopt_long(argc, argv, "cs", long_options, NULL)) != -1) {
4332 if (cmd_type != POOL_INITIALIZE_DO) {
4333 (void) fprintf(stderr, gettext("-c cannot be "
4334 "combined with other options\n"));
4337 cmd_type = POOL_INITIALIZE_CANCEL;
4340 if (cmd_type != POOL_INITIALIZE_DO) {
4341 (void) fprintf(stderr, gettext("-s cannot be "
4342 "combined with other options\n"));
4345 cmd_type = POOL_INITIALIZE_SUSPEND;
4349 (void) fprintf(stderr,
4350 gettext("invalid option '%c'\n"), optopt);
4352 (void) fprintf(stderr,
4353 gettext("invalid option '%s'\n"),
4364 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4370 zhp = zpool_open(g_zfs, poolname);
4374 vdevs = fnvlist_alloc();
4376 /* no individual leaf vdevs specified, so add them all */
4377 nvlist_t *config = zpool_get_config(zhp, NULL);
4378 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
4379 ZPOOL_CONFIG_VDEV_TREE);
4380 zpool_collect_leaves(zhp, nvroot, vdevs);
4383 for (i = 1; i < argc; i++) {
4384 fnvlist_add_boolean(vdevs, argv[i]);
4388 err = zpool_initialize(zhp, cmd_type, vdevs);
4390 fnvlist_free(vdevs);
4396 typedef struct status_cbdata {
4398 boolean_t cb_allpools;
4399 boolean_t cb_verbose;
4400 boolean_t cb_explain;
4402 boolean_t cb_dedup_stats;
4406 * Print out detailed scrub status.
4409 print_scan_status(pool_scan_stat_t *ps)
4411 time_t start, end, pause;
4412 uint64_t total_secs_left;
4413 uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
4414 uint64_t pass_scanned, scanned, pass_issued, issued, total;
4415 uint_t scan_rate, issue_rate;
4416 double fraction_done;
4417 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
4418 char srate_buf[7], irate_buf[7];
4420 (void) printf(gettext(" scan: "));
4422 /* If there's never been a scan, there's not much to say. */
4423 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
4424 ps->pss_func >= POOL_SCAN_FUNCS) {
4425 (void) printf(gettext("none requested\n"));
4429 start = ps->pss_start_time;
4430 end = ps->pss_end_time;
4431 pause = ps->pss_pass_scrub_pause;
4433 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4435 assert(ps->pss_func == POOL_SCAN_SCRUB ||
4436 ps->pss_func == POOL_SCAN_RESILVER);
4438 /* Scan is finished or canceled. */
4439 if (ps->pss_state == DSS_FINISHED) {
4440 total_secs_left = end - start;
4441 days_left = total_secs_left / 60 / 60 / 24;
4442 hours_left = (total_secs_left / 60 / 60) % 24;
4443 mins_left = (total_secs_left / 60) % 60;
4444 secs_left = (total_secs_left % 60);
4446 if (ps->pss_func == POOL_SCAN_SCRUB) {
4447 (void) printf(gettext("scrub repaired %s "
4448 "in %llu days %02llu:%02llu:%02llu "
4449 "with %llu errors on %s"), processed_buf,
4450 (u_longlong_t)days_left, (u_longlong_t)hours_left,
4451 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
4452 (u_longlong_t)ps->pss_errors, ctime(&end));
4453 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4454 (void) printf(gettext("resilvered %s "
4455 "in %llu days %02llu:%02llu:%02llu "
4456 "with %llu errors on %s"), processed_buf,
4457 (u_longlong_t)days_left, (u_longlong_t)hours_left,
4458 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
4459 (u_longlong_t)ps->pss_errors, ctime(&end));
4464 } else if (ps->pss_state == DSS_CANCELED) {
4465 if (ps->pss_func == POOL_SCAN_SCRUB) {
4466 (void) printf(gettext("scrub canceled on %s"),
4468 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4469 (void) printf(gettext("resilver canceled on %s"),
4475 assert(ps->pss_state == DSS_SCANNING);
4477 /* Scan is in progress. Resilvers can't be paused. */
4478 if (ps->pss_func == POOL_SCAN_SCRUB) {
4480 (void) printf(gettext("scrub in progress since %s"),
4483 (void) printf(gettext("scrub paused since %s"),
4485 (void) printf(gettext("\tscrub started on %s"),
4488 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4489 (void) printf(gettext("resilver in progress since %s"),
4493 scanned = ps->pss_examined;
4494 pass_scanned = ps->pss_pass_exam;
4495 issued = ps->pss_issued;
4496 pass_issued = ps->pss_pass_issued;
4497 total = ps->pss_to_examine;
4499 /* we are only done with a block once we have issued the IO for it */
4500 fraction_done = (double)issued / total;
4502 /* elapsed time for this pass, rounding up to 1 if it's 0 */
4503 elapsed = time(NULL) - ps->pss_pass_start;
4504 elapsed -= ps->pss_pass_scrub_spent_paused;
4505 elapsed = (elapsed != 0) ? elapsed : 1;
4507 scan_rate = pass_scanned / elapsed;
4508 issue_rate = pass_issued / elapsed;
4509 total_secs_left = (issue_rate != 0) ?
4510 ((total - issued) / issue_rate) : UINT64_MAX;
4512 days_left = total_secs_left / 60 / 60 / 24;
4513 hours_left = (total_secs_left / 60 / 60) % 24;
4514 mins_left = (total_secs_left / 60) % 60;
4515 secs_left = (total_secs_left % 60);
4517 /* format all of the numbers we will be reporting */
4518 zfs_nicenum(scanned, scanned_buf, sizeof (scanned_buf));
4519 zfs_nicenum(issued, issued_buf, sizeof (issued_buf));
4520 zfs_nicenum(total, total_buf, sizeof (total_buf));
4521 zfs_nicenum(scan_rate, srate_buf, sizeof (srate_buf));
4522 zfs_nicenum(issue_rate, irate_buf, sizeof (irate_buf));
4524 /* doo not print estimated time if we have a paused scrub */
4526 (void) printf(gettext("\t%s scanned at %s/s, "
4527 "%s issued at %s/s, %s total\n"),
4528 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
4530 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
4531 scanned_buf, issued_buf, total_buf);
4534 if (ps->pss_func == POOL_SCAN_RESILVER) {
4535 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
4536 processed_buf, 100 * fraction_done);
4537 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4538 (void) printf(gettext("\t%s repaired, %.2f%% done"),
4539 processed_buf, 100 * fraction_done);
4543 if (issue_rate >= 10 * 1024 * 1024) {
4544 (void) printf(gettext(", %llu days "
4545 "%02llu:%02llu:%02llu to go\n"),
4546 (u_longlong_t)days_left, (u_longlong_t)hours_left,
4547 (u_longlong_t)mins_left, (u_longlong_t)secs_left);
4549 (void) printf(gettext(", no estimated "
4550 "completion time\n"));
4553 (void) printf(gettext("\n"));
4558 * As we don't scrub checkpointed blocks, we want to warn the
4559 * user that we skipped scanning some blocks if a checkpoint exists
4560 * or existed at any time during the scan.
4563 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
4565 if (ps == NULL || pcs == NULL)
4568 if (pcs->pcs_state == CS_NONE ||
4569 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
4572 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
4574 if (ps->pss_state == DSS_NONE)
4577 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
4578 ps->pss_end_time < pcs->pcs_start_time)
4581 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
4582 (void) printf(gettext(" scan warning: skipped blocks "
4583 "that are only referenced by the checkpoint.\n"));
4585 assert(ps->pss_state == DSS_SCANNING);
4586 (void) printf(gettext(" scan warning: skipping blocks "
4587 "that are only referenced by the checkpoint.\n"));
4592 * Print out detailed removal status.
4595 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
4597 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4599 nvlist_t *config, *nvroot;
4604 if (prs == NULL || prs->prs_state == DSS_NONE)
4608 * Determine name of vdev.
4610 config = zpool_get_config(zhp, NULL);
4611 nvroot = fnvlist_lookup_nvlist(config,
4612 ZPOOL_CONFIG_VDEV_TREE);
4613 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4614 &child, &children) == 0);
4615 assert(prs->prs_removing_vdev < children);
4616 vdev_name = zpool_vdev_name(g_zfs, zhp,
4617 child[prs->prs_removing_vdev], B_TRUE);
4619 (void) printf(gettext("remove: "));
4621 start = prs->prs_start_time;
4622 end = prs->prs_end_time;
4623 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
4626 * Removal is finished or canceled.
4628 if (prs->prs_state == DSS_FINISHED) {
4629 uint64_t minutes_taken = (end - start) / 60;
4631 (void) printf(gettext("Removal of vdev %llu copied %s "
4632 "in %lluh%um, completed on %s"),
4633 (longlong_t)prs->prs_removing_vdev,
4635 (u_longlong_t)(minutes_taken / 60),
4636 (uint_t)(minutes_taken % 60),
4637 ctime((time_t *)&end));
4638 } else if (prs->prs_state == DSS_CANCELED) {
4639 (void) printf(gettext("Removal of %s canceled on %s"),
4640 vdev_name, ctime(&end));
4642 uint64_t copied, total, elapsed, mins_left, hours_left;
4643 double fraction_done;
4646 assert(prs->prs_state == DSS_SCANNING);
4649 * Removal is in progress.
4651 (void) printf(gettext(
4652 "Evacuation of %s in progress since %s"),
4653 vdev_name, ctime(&start));
4655 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
4656 total = prs->prs_to_copy;
4657 fraction_done = (double)copied / total;
4659 /* elapsed time for this pass */
4660 elapsed = time(NULL) - prs->prs_start_time;
4661 elapsed = elapsed > 0 ? elapsed : 1;
4662 rate = copied / elapsed;
4663 rate = rate > 0 ? rate : 1;
4664 mins_left = ((total - copied) / rate) / 60;
4665 hours_left = mins_left / 60;
4667 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
4668 zfs_nicenum(total, total_buf, sizeof (total_buf));
4669 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4672 * do not print estimated time if hours_left is more than
4675 (void) printf(gettext(" %s copied out of %s at %s/s, "
4677 examined_buf, total_buf, rate_buf, 100 * fraction_done);
4678 if (hours_left < (30 * 24)) {
4679 (void) printf(gettext(", %lluh%um to go\n"),
4680 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4682 (void) printf(gettext(
4683 ", (copy is slow, no estimated time)\n"));
4687 if (prs->prs_mapping_memory > 0) {
4689 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
4690 (void) printf(gettext(" %s memory used for "
4691 "removed device mappings\n"),
4697 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
4702 if (pcs == NULL || pcs->pcs_state == CS_NONE)
4705 (void) printf(gettext("checkpoint: "));
4707 start = pcs->pcs_start_time;
4708 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
4710 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
4711 char *date = ctime(&start);
4714 * ctime() adds a newline at the end of the generated
4715 * string, thus the weird format specifier and the
4716 * strlen() call used to chop it off from the output.
4718 (void) printf(gettext("created %.*s, consumes %s\n"),
4719 strlen(date) - 1, date, space_buf);
4723 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
4725 (void) printf(gettext("discarding, %s remaining.\n"),
4730 print_error_log(zpool_handle_t *zhp)
4732 nvlist_t *nverrlist = NULL;
4735 size_t len = MAXPATHLEN * 2;
4737 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4738 (void) printf("errors: List of errors unavailable "
4739 "(insufficient privileges)\n");
4743 (void) printf("errors: Permanent errors have been "
4744 "detected in the following files:\n\n");
4746 pathname = safe_malloc(len);
4748 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4750 uint64_t dsobj, obj;
4752 verify(nvpair_value_nvlist(elem, &nv) == 0);
4753 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4755 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4757 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4758 (void) printf("%7s %s\n", "", pathname);
4761 nvlist_free(nverrlist);
4765 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4774 (void) printf(gettext("\tspares\n"));
4776 for (i = 0; i < nspares; i++) {
4777 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4778 print_status_config(zhp, name, spares[i],
4779 namewidth, 2, B_TRUE);
4785 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4794 (void) printf(gettext("\tcache\n"));
4796 for (i = 0; i < nl2cache; i++) {
4797 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4798 print_status_config(zhp, name, l2cache[i],
4799 namewidth, 2, B_FALSE);
4805 print_dedup_stats(nvlist_t *config)
4807 ddt_histogram_t *ddh;
4813 * If the pool was faulted then we may not have been able to
4814 * obtain the config. Otherwise, if we have anything in the dedup
4815 * table continue processing the stats.
4817 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4818 (uint64_t **)&ddo, &c) != 0)
4821 (void) printf("\n");
4822 (void) printf(gettext(" dedup: "));
4823 if (ddo->ddo_count == 0) {
4824 (void) printf(gettext("no DDT entries\n"));
4828 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4829 (u_longlong_t)ddo->ddo_count,
4830 (u_longlong_t)ddo->ddo_dspace,
4831 (u_longlong_t)ddo->ddo_mspace);
4833 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4834 (uint64_t **)&dds, &c) == 0);
4835 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4836 (uint64_t **)&ddh, &c) == 0);
4837 zpool_dump_ddt(dds, ddh);
4841 * Display a summary of pool status. Displays a summary such as:
4845 * reason: One or more devices ...
4846 * see: http://illumos.org/msg/ZFS-xxxx-01
4852 * When given the '-v' option, we print out the complete config. If the '-e'
4853 * option is specified, then we print out error rate information as well.
4856 status_callback(zpool_handle_t *zhp, void *data)
4858 status_cbdata_t *cbp = data;
4859 nvlist_t *config, *nvroot;
4866 config = zpool_get_config(zhp, NULL);
4867 reason = zpool_get_status(zhp, &msgid);
4872 * If we were given 'zpool status -x', only report those pools with
4875 if (cbp->cb_explain &&
4876 (reason == ZPOOL_STATUS_OK ||
4877 reason == ZPOOL_STATUS_VERSION_OLDER ||
4878 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
4879 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4880 if (!cbp->cb_allpools) {
4881 (void) printf(gettext("pool '%s' is healthy\n"),
4882 zpool_get_name(zhp));
4884 cbp->cb_first = B_FALSE;
4890 cbp->cb_first = B_FALSE;
4892 (void) printf("\n");
4894 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
4895 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4896 (uint64_t **)&vs, &c) == 0);
4897 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4899 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4900 (void) printf(gettext(" state: %s\n"), health);
4903 case ZPOOL_STATUS_MISSING_DEV_R:
4904 (void) printf(gettext("status: One or more devices could not "
4905 "be opened. Sufficient replicas exist for\n\tthe pool to "
4906 "continue functioning in a degraded state.\n"));
4907 (void) printf(gettext("action: Attach the missing device and "
4908 "online it using 'zpool online'.\n"));
4911 case ZPOOL_STATUS_MISSING_DEV_NR:
4912 (void) printf(gettext("status: One or more devices could not "
4913 "be opened. There are insufficient\n\treplicas for the "
4914 "pool to continue functioning.\n"));
4915 (void) printf(gettext("action: Attach the missing device and "
4916 "online it using 'zpool online'.\n"));
4919 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4920 (void) printf(gettext("status: One or more devices could not "
4921 "be used because the label is missing or\n\tinvalid. "
4922 "Sufficient replicas exist for the pool to continue\n\t"
4923 "functioning in a degraded state.\n"));
4924 (void) printf(gettext("action: Replace the device using "
4925 "'zpool replace'.\n"));
4928 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4929 (void) printf(gettext("status: One or more devices could not "
4930 "be used because the label is missing \n\tor invalid. "
4931 "There are insufficient replicas for the pool to "
4932 "continue\n\tfunctioning.\n"));
4933 zpool_explain_recover(zpool_get_handle(zhp),
4934 zpool_get_name(zhp), reason, config);
4937 case ZPOOL_STATUS_FAILING_DEV:
4938 (void) printf(gettext("status: One or more devices has "
4939 "experienced an unrecoverable error. An\n\tattempt was "
4940 "made to correct the error. Applications are "
4942 (void) printf(gettext("action: Determine if the device needs "
4943 "to be replaced, and clear the errors\n\tusing "
4944 "'zpool clear' or replace the device with 'zpool "
4948 case ZPOOL_STATUS_OFFLINE_DEV:
4949 (void) printf(gettext("status: One or more devices has "
4950 "been taken offline by the administrator.\n\tSufficient "
4951 "replicas exist for the pool to continue functioning in "
4952 "a\n\tdegraded state.\n"));
4953 (void) printf(gettext("action: Online the device using "
4954 "'zpool online' or replace the device with\n\t'zpool "
4958 case ZPOOL_STATUS_REMOVED_DEV:
4959 (void) printf(gettext("status: One or more devices has "
4960 "been removed by the administrator.\n\tSufficient "
4961 "replicas exist for the pool to continue functioning in "
4962 "a\n\tdegraded state.\n"));
4963 (void) printf(gettext("action: Online the device using "
4964 "'zpool online' or replace the device with\n\t'zpool "
4968 case ZPOOL_STATUS_RESILVERING:
4969 (void) printf(gettext("status: One or more devices is "
4970 "currently being resilvered. The pool will\n\tcontinue "
4971 "to function, possibly in a degraded state.\n"));
4972 (void) printf(gettext("action: Wait for the resilver to "
4976 case ZPOOL_STATUS_CORRUPT_DATA:
4977 (void) printf(gettext("status: One or more devices has "
4978 "experienced an error resulting in data\n\tcorruption. "
4979 "Applications may be affected.\n"));
4980 (void) printf(gettext("action: Restore the file in question "
4981 "if possible. Otherwise restore the\n\tentire pool from "
4985 case ZPOOL_STATUS_CORRUPT_POOL:
4986 (void) printf(gettext("status: The pool metadata is corrupted "
4987 "and the pool cannot be opened.\n"));
4988 zpool_explain_recover(zpool_get_handle(zhp),
4989 zpool_get_name(zhp), reason, config);
4992 case ZPOOL_STATUS_VERSION_OLDER:
4993 (void) printf(gettext("status: The pool is formatted using a "
4994 "legacy on-disk format. The pool can\n\tstill be used, "
4995 "but some features are unavailable.\n"));
4996 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4997 "upgrade'. Once this is done, the\n\tpool will no longer "
4998 "be accessible on software that does not support feature\n"
5002 case ZPOOL_STATUS_VERSION_NEWER:
5003 (void) printf(gettext("status: The pool has been upgraded to a "
5004 "newer, incompatible on-disk version.\n\tThe pool cannot "
5005 "be accessed on this system.\n"));
5006 (void) printf(gettext("action: Access the pool from a system "
5007 "running more recent software, or\n\trestore the pool from "
5011 case ZPOOL_STATUS_FEAT_DISABLED:
5012 (void) printf(gettext("status: Some supported features are not "
5013 "enabled on the pool. The pool can\n\tstill be used, but "
5014 "some features are unavailable.\n"));
5015 (void) printf(gettext("action: Enable all features using "
5016 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
5017 "longer be accessible by software that does not support\n\t"
5018 "the features. See zpool-features(7) for details.\n"));
5021 case ZPOOL_STATUS_UNSUP_FEAT_READ:
5022 (void) printf(gettext("status: The pool cannot be accessed on "
5023 "this system because it uses the\n\tfollowing feature(s) "
5024 "not supported on this system:\n"));
5025 zpool_print_unsup_feat(config);
5026 (void) printf("\n");
5027 (void) printf(gettext("action: Access the pool from a system "
5028 "that supports the required feature(s),\n\tor restore the "
5029 "pool from backup.\n"));
5032 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
5033 (void) printf(gettext("status: The pool can only be accessed "
5034 "in read-only mode on this system. It\n\tcannot be "
5035 "accessed in read-write mode because it uses the "
5036 "following\n\tfeature(s) not supported on this system:\n"));
5037 zpool_print_unsup_feat(config);
5038 (void) printf("\n");
5039 (void) printf(gettext("action: The pool cannot be accessed in "
5040 "read-write mode. Import the pool with\n"
5041 "\t\"-o readonly=on\", access the pool from a system that "
5042 "supports the\n\trequired feature(s), or restore the "
5043 "pool from backup.\n"));
5046 case ZPOOL_STATUS_FAULTED_DEV_R:
5047 (void) printf(gettext("status: One or more devices are "
5048 "faulted in response to persistent errors.\n\tSufficient "
5049 "replicas exist for the pool to continue functioning "
5050 "in a\n\tdegraded state.\n"));
5051 (void) printf(gettext("action: Replace the faulted device, "
5052 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
5055 case ZPOOL_STATUS_FAULTED_DEV_NR:
5056 (void) printf(gettext("status: One or more devices are "
5057 "faulted in response to persistent errors. There are "
5058 "insufficient replicas for the pool to\n\tcontinue "
5060 (void) printf(gettext("action: Destroy and re-create the pool "
5061 "from a backup source. Manually marking the device\n"
5062 "\trepaired using 'zpool clear' may allow some data "
5063 "to be recovered.\n"));
5066 case ZPOOL_STATUS_IO_FAILURE_WAIT:
5067 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
5068 (void) printf(gettext("status: One or more devices are "
5069 "faulted in response to IO failures.\n"));
5070 (void) printf(gettext("action: Make sure the affected devices "
5071 "are connected, then run 'zpool clear'.\n"));
5074 case ZPOOL_STATUS_BAD_LOG:
5075 (void) printf(gettext("status: An intent log record "
5076 "could not be read.\n"
5077 "\tWaiting for adminstrator intervention to fix the "
5078 "faulted pool.\n"));
5079 (void) printf(gettext("action: Either restore the affected "
5080 "device(s) and run 'zpool online',\n"
5081 "\tor ignore the intent log records by running "
5082 "'zpool clear'.\n"));
5085 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
5086 (void) printf(gettext("status: One or more devices are "
5087 "configured to use a non-native block size.\n"
5088 "\tExpect reduced performance.\n"));
5089 (void) printf(gettext("action: Replace affected devices with "
5090 "devices that support the\n\tconfigured block size, or "
5091 "migrate data to a properly configured\n\tpool.\n"));
5096 * The remaining errors can't actually be generated, yet.
5098 assert(reason == ZPOOL_STATUS_OK);
5102 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
5105 if (config != NULL) {
5108 nvlist_t **spares, **l2cache;
5109 uint_t nspares, nl2cache;
5110 pool_checkpoint_stat_t *pcs = NULL;
5111 pool_scan_stat_t *ps = NULL;
5112 pool_removal_stat_t *prs = NULL;
5114 (void) nvlist_lookup_uint64_array(nvroot,
5115 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
5116 (void) nvlist_lookup_uint64_array(nvroot,
5117 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
5118 (void) nvlist_lookup_uint64_array(nvroot,
5119 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
5121 print_scan_status(ps);
5122 print_checkpoint_scan_warning(ps, pcs);
5123 print_removal_status(zhp, prs);
5124 print_checkpoint_status(pcs);
5126 namewidth = max_width(zhp, nvroot, 0, 0);
5130 (void) printf(gettext("config:\n\n"));
5131 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
5132 "NAME", "STATE", "READ", "WRITE", "CKSUM");
5133 print_status_config(zhp, zpool_get_name(zhp), nvroot,
5134 namewidth, 0, B_FALSE);
5136 if (num_logs(nvroot) > 0)
5137 print_logs(zhp, nvroot, namewidth, B_TRUE);
5138 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
5139 &l2cache, &nl2cache) == 0)
5140 print_l2cache(zhp, l2cache, nl2cache, namewidth);
5142 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
5143 &spares, &nspares) == 0)
5144 print_spares(zhp, spares, nspares, namewidth);
5146 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
5148 nvlist_t *nverrlist = NULL;
5151 * If the approximate error count is small, get a
5152 * precise count by fetching the entire log and
5153 * uniquifying the results.
5155 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
5156 zpool_get_errlog(zhp, &nverrlist) == 0) {
5161 while ((elem = nvlist_next_nvpair(nverrlist,
5166 nvlist_free(nverrlist);
5168 (void) printf("\n");
5171 (void) printf(gettext("errors: No known data "
5173 else if (!cbp->cb_verbose)
5174 (void) printf(gettext("errors: %llu data "
5175 "errors, use '-v' for a list\n"),
5176 (u_longlong_t)nerr);
5178 print_error_log(zhp);
5181 if (cbp->cb_dedup_stats)
5182 print_dedup_stats(config);
5184 (void) printf(gettext("config: The configuration cannot be "
5192 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
5194 * -v Display complete error logs
5195 * -x Display only pools with potential problems
5196 * -D Display dedup status (undocumented)
5197 * -T Display a timestamp in date(1) or Unix format
5199 * Describes the health status of all pools or some subset.
5202 zpool_do_status(int argc, char **argv)
5206 unsigned long interval = 0, count = 0;
5207 status_cbdata_t cb = { 0 };
5210 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
5213 cb.cb_verbose = B_TRUE;
5216 cb.cb_explain = B_TRUE;
5219 cb.cb_dedup_stats = B_TRUE;
5222 get_timestamp_arg(*optarg);
5225 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5234 get_interval_count(&argc, argv, &interval, &count);
5237 cb.cb_allpools = B_TRUE;
5239 cb.cb_first = B_TRUE;
5242 if (timestamp_fmt != NODATE)
5243 print_timestamp(timestamp_fmt);
5245 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5246 status_callback, &cb);
5248 if (argc == 0 && cb.cb_count == 0)
5249 (void) printf(gettext("no pools available\n"));
5250 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
5251 (void) printf(gettext("all pools are healthy\n"));
5259 if (count != 0 && --count == 0)
5262 (void) sleep(interval);
5268 typedef struct upgrade_cbdata {
5270 boolean_t cb_unavail;
5271 char cb_poolname[ZFS_MAX_DATASET_NAME_LEN];
5273 uint64_t cb_version;
5279 is_root_pool(zpool_handle_t *zhp)
5281 static struct statfs sfs;
5282 static char *poolname = NULL;
5283 static boolean_t stated = B_FALSE;
5288 if (statfs("/", &sfs) == -1) {
5289 (void) fprintf(stderr,
5290 "Unable to stat root file system: %s.\n",
5294 if (strcmp(sfs.f_fstypename, "zfs") != 0)
5296 poolname = sfs.f_mntfromname;
5297 if ((slash = strchr(poolname, '/')) != NULL)
5300 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
5304 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
5307 if (poolname[0] == '\0' && is_root_pool(zhp))
5308 (void) strlcpy(poolname, zpool_get_name(zhp), size);
5310 #endif /* FreeBSD */
5313 upgrade_version(zpool_handle_t *zhp, uint64_t version)
5317 uint64_t oldversion;
5319 config = zpool_get_config(zhp, NULL);
5320 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5323 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
5324 assert(oldversion < version);
5326 ret = zpool_upgrade(zhp, version);
5330 if (version >= SPA_VERSION_FEATURES) {
5331 (void) printf(gettext("Successfully upgraded "
5332 "'%s' from version %llu to feature flags.\n"),
5333 zpool_get_name(zhp), oldversion);
5335 (void) printf(gettext("Successfully upgraded "
5336 "'%s' from version %llu to version %llu.\n"),
5337 zpool_get_name(zhp), oldversion, version);
5344 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
5347 boolean_t firstff = B_TRUE;
5348 nvlist_t *enabled = zpool_get_features(zhp);
5351 for (i = 0; i < SPA_FEATURES; i++) {
5352 const char *fname = spa_feature_table[i].fi_uname;
5353 const char *fguid = spa_feature_table[i].fi_guid;
5354 if (!nvlist_exists(enabled, fguid)) {
5356 verify(-1 != asprintf(&propname, "feature@%s", fname));
5357 ret = zpool_set_prop(zhp, propname,
5358 ZFS_FEATURE_ENABLED);
5366 (void) printf(gettext("Enabled the "
5367 "following features on '%s':\n"),
5368 zpool_get_name(zhp));
5371 (void) printf(gettext(" %s\n"), fname);
5382 upgrade_cb(zpool_handle_t *zhp, void *arg)
5384 upgrade_cbdata_t *cbp = arg;
5387 boolean_t printnl = B_FALSE;
5390 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5391 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5392 "currently unavailable.\n\n"), zpool_get_name(zhp));
5393 cbp->cb_unavail = B_TRUE;
5394 /* Allow iteration to continue. */
5398 config = zpool_get_config(zhp, NULL);
5399 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5402 assert(SPA_VERSION_IS_SUPPORTED(version));
5404 if (version < cbp->cb_version) {
5405 cbp->cb_first = B_FALSE;
5406 ret = upgrade_version(zhp, cbp->cb_version);
5410 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5411 sizeof(cbp->cb_poolname));
5412 #endif /* __FreeBSD__ */
5417 * If they did "zpool upgrade -a", then we could
5418 * be doing ioctls to different pools. We need
5419 * to log this history once to each pool, and bypass
5420 * the normal history logging that happens in main().
5422 (void) zpool_log_history(g_zfs, history_str);
5423 log_history = B_FALSE;
5427 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5429 ret = upgrade_enable_all(zhp, &count);
5434 cbp->cb_first = B_FALSE;
5437 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5438 sizeof(cbp->cb_poolname));
5439 #endif /* __FreeBSD__ */
5441 * If they did "zpool upgrade -a", then we could
5442 * be doing ioctls to different pools. We need
5443 * to log this history once to each pool, and bypass
5444 * the normal history logging that happens in main().
5446 (void) zpool_log_history(g_zfs, history_str);
5447 log_history = B_FALSE;
5452 (void) printf(gettext("\n"));
5459 upgrade_list_unavail(zpool_handle_t *zhp, void *arg)
5461 upgrade_cbdata_t *cbp = arg;
5463 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5464 if (cbp->cb_first) {
5465 (void) fprintf(stderr, gettext("The following pools "
5466 "are unavailable and cannot be upgraded as this "
5468 (void) fprintf(stderr, gettext("POOL\n"));
5469 (void) fprintf(stderr, gettext("------------\n"));
5470 cbp->cb_first = B_FALSE;
5472 (void) printf(gettext("%s\n"), zpool_get_name(zhp));
5473 cbp->cb_unavail = B_TRUE;
5479 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
5481 upgrade_cbdata_t *cbp = arg;
5485 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5487 * This will have been reported by upgrade_list_unavail so
5488 * just allow iteration to continue.
5490 cbp->cb_unavail = B_TRUE;
5494 config = zpool_get_config(zhp, NULL);
5495 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5498 assert(SPA_VERSION_IS_SUPPORTED(version));
5500 if (version < SPA_VERSION_FEATURES) {
5501 if (cbp->cb_first) {
5502 (void) printf(gettext("The following pools are "
5503 "formatted with legacy version numbers and can\n"
5504 "be upgraded to use feature flags. After "
5505 "being upgraded, these pools\nwill no "
5506 "longer be accessible by software that does not "
5507 "support feature\nflags.\n\n"));
5508 (void) printf(gettext("VER POOL\n"));
5509 (void) printf(gettext("--- ------------\n"));
5510 cbp->cb_first = B_FALSE;
5513 (void) printf("%2llu %s\n", (u_longlong_t)version,
5514 zpool_get_name(zhp));
5521 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
5523 upgrade_cbdata_t *cbp = arg;
5527 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5529 * This will have been reported by upgrade_list_unavail so
5530 * just allow iteration to continue.
5532 cbp->cb_unavail = B_TRUE;
5536 config = zpool_get_config(zhp, NULL);
5537 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5540 if (version >= SPA_VERSION_FEATURES) {
5542 boolean_t poolfirst = B_TRUE;
5543 nvlist_t *enabled = zpool_get_features(zhp);
5545 for (i = 0; i < SPA_FEATURES; i++) {
5546 const char *fguid = spa_feature_table[i].fi_guid;
5547 const char *fname = spa_feature_table[i].fi_uname;
5548 if (!nvlist_exists(enabled, fguid)) {
5549 if (cbp->cb_first) {
5550 (void) printf(gettext("\nSome "
5551 "supported features are not "
5552 "enabled on the following pools. "
5553 "Once a\nfeature is enabled the "
5554 "pool may become incompatible with "
5555 "software\nthat does not support "
5557 "zpool-features(7) for "
5559 (void) printf(gettext("POOL "
5561 (void) printf(gettext("------"
5563 cbp->cb_first = B_FALSE;
5567 (void) printf(gettext("%s\n"),
5568 zpool_get_name(zhp));
5569 poolfirst = B_FALSE;
5572 (void) printf(gettext(" %s\n"), fname);
5582 upgrade_one(zpool_handle_t *zhp, void *data)
5584 boolean_t printnl = B_FALSE;
5585 upgrade_cbdata_t *cbp = data;
5586 uint64_t cur_version;
5589 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5590 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5591 "is currently unavailable.\n\n"), zpool_get_name(zhp));
5592 cbp->cb_unavail = B_TRUE;
5596 if (strcmp("log", zpool_get_name(zhp)) == 0) {
5597 (void) printf(gettext("'log' is now a reserved word\n"
5598 "Pool 'log' must be renamed using export and import"
5599 " to upgrade.\n\n"));
5603 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5604 if (cur_version > cbp->cb_version) {
5605 (void) printf(gettext("Pool '%s' is already formatted "
5606 "using more current version '%llu'.\n\n"),
5607 zpool_get_name(zhp), cur_version);
5611 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
5612 (void) printf(gettext("Pool '%s' is already formatted "
5613 "using version %llu.\n\n"), zpool_get_name(zhp),
5618 if (cur_version != cbp->cb_version) {
5620 ret = upgrade_version(zhp, cbp->cb_version);
5624 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5625 sizeof(cbp->cb_poolname));
5626 #endif /* __FreeBSD__ */
5629 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5631 ret = upgrade_enable_all(zhp, &count);
5638 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5639 sizeof(cbp->cb_poolname));
5640 #endif /* __FreeBSD __*/
5641 } else if (cur_version == SPA_VERSION) {
5642 (void) printf(gettext("Pool '%s' already has all "
5643 "supported features enabled.\n\n"),
5644 zpool_get_name(zhp));
5649 (void) printf(gettext("\n"));
5658 * zpool upgrade [-V version] <-a | pool ...>
5660 * With no arguments, display downrev'd ZFS pool available for upgrade.
5661 * Individual pools can be upgraded by specifying the pool, and '-a' will
5662 * upgrade all pools.
5665 zpool_do_upgrade(int argc, char **argv)
5668 upgrade_cbdata_t cb = { 0 };
5670 boolean_t showversions = B_FALSE;
5671 boolean_t upgradeall = B_FALSE;
5676 while ((c = getopt(argc, argv, ":avV:")) != -1) {
5679 upgradeall = B_TRUE;
5682 showversions = B_TRUE;
5685 cb.cb_version = strtoll(optarg, &end, 10);
5687 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5688 (void) fprintf(stderr,
5689 gettext("invalid version '%s'\n"), optarg);
5694 (void) fprintf(stderr, gettext("missing argument for "
5695 "'%c' option\n"), optopt);
5699 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5710 if (cb.cb_version == 0) {
5711 cb.cb_version = SPA_VERSION;
5712 } else if (!upgradeall && argc == 0) {
5713 (void) fprintf(stderr, gettext("-V option is "
5714 "incompatible with other arguments\n"));
5719 if (upgradeall || argc != 0) {
5720 (void) fprintf(stderr, gettext("-v option is "
5721 "incompatible with other arguments\n"));
5724 } else if (upgradeall) {
5726 (void) fprintf(stderr, gettext("-a option should not "
5727 "be used along with a pool name\n"));
5732 (void) printf(gettext("This system supports ZFS pool feature "
5737 (void) printf(gettext("The following features are "
5739 (void) printf(gettext("FEAT DESCRIPTION\n"));
5740 (void) printf("----------------------------------------------"
5741 "---------------\n");
5742 for (i = 0; i < SPA_FEATURES; i++) {
5743 zfeature_info_t *fi = &spa_feature_table[i];
5745 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
5746 " (read-only compatible)" : "";
5748 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5749 (void) printf(" %s\n", fi->fi_desc);
5751 (void) printf("\n");
5753 (void) printf(gettext("The following legacy versions are also "
5755 (void) printf(gettext("VER DESCRIPTION\n"));
5756 (void) printf("--- -----------------------------------------"
5757 "---------------\n");
5758 (void) printf(gettext(" 1 Initial ZFS version\n"));
5759 (void) printf(gettext(" 2 Ditto blocks "
5760 "(replicated metadata)\n"));
5761 (void) printf(gettext(" 3 Hot spares and double parity "
5763 (void) printf(gettext(" 4 zpool history\n"));
5764 (void) printf(gettext(" 5 Compression using the gzip "
5766 (void) printf(gettext(" 6 bootfs pool property\n"));
5767 (void) printf(gettext(" 7 Separate intent log devices\n"));
5768 (void) printf(gettext(" 8 Delegated administration\n"));
5769 (void) printf(gettext(" 9 refquota and refreservation "
5771 (void) printf(gettext(" 10 Cache devices\n"));
5772 (void) printf(gettext(" 11 Improved scrub performance\n"));
5773 (void) printf(gettext(" 12 Snapshot properties\n"));
5774 (void) printf(gettext(" 13 snapused property\n"));
5775 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5776 (void) printf(gettext(" 15 user/group space accounting\n"));
5777 (void) printf(gettext(" 16 stmf property support\n"));
5778 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5779 (void) printf(gettext(" 18 Snapshot user holds\n"));
5780 (void) printf(gettext(" 19 Log device removal\n"));
5781 (void) printf(gettext(" 20 Compression using zle "
5782 "(zero-length encoding)\n"));
5783 (void) printf(gettext(" 21 Deduplication\n"));
5784 (void) printf(gettext(" 22 Received properties\n"));
5785 (void) printf(gettext(" 23 Slim ZIL\n"));
5786 (void) printf(gettext(" 24 System attributes\n"));
5787 (void) printf(gettext(" 25 Improved scrub stats\n"));
5788 (void) printf(gettext(" 26 Improved snapshot deletion "
5790 (void) printf(gettext(" 27 Improved snapshot creation "
5792 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5793 (void) printf(gettext("\nFor more information on a particular "
5794 "version, including supported releases,\n"));
5795 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5796 } else if (argc == 0 && upgradeall) {
5797 cb.cb_first = B_TRUE;
5798 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5799 if (ret == 0 && cb.cb_first) {
5800 if (cb.cb_version == SPA_VERSION) {
5801 (void) printf(gettext("All %spools are already "
5802 "formatted using feature flags.\n\n"),
5803 cb.cb_unavail ? gettext("available ") : "");
5804 (void) printf(gettext("Every %sfeature flags "
5805 "pool already has all supported features "
5807 cb.cb_unavail ? gettext("available ") : "");
5809 (void) printf(gettext("All pools are already "
5810 "formatted with version %llu or higher.\n"),
5814 } else if (argc == 0) {
5815 cb.cb_first = B_TRUE;
5816 ret = zpool_iter(g_zfs, upgrade_list_unavail, &cb);
5820 (void) fprintf(stderr, "\n");
5823 cb.cb_first = B_TRUE;
5824 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5828 (void) printf(gettext("All %spools are formatted using "
5829 "feature flags.\n\n"), cb.cb_unavail ?
5830 gettext("available ") : "");
5832 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5833 "for a list of available legacy versions.\n"));
5836 cb.cb_first = B_TRUE;
5837 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5841 (void) printf(gettext("Every %sfeature flags pool has "
5842 "all supported features enabled.\n"),
5843 cb.cb_unavail ? gettext("available ") : "");
5845 (void) printf(gettext("\n"));
5848 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5852 if (cb.cb_poolname[0] != '\0') {
5854 "If you boot from pool '%s', don't forget to update boot code.\n"
5855 "Assuming you use GPT partitioning and da0 is your boot disk\n"
5856 "the following command will do it:\n"
5858 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
5865 typedef struct hist_cbdata {
5872 * Print out the command history for a specific pool.
5875 get_history_one(zpool_handle_t *zhp, void *data)
5881 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5883 cb->first = B_FALSE;
5885 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5887 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5890 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5891 &records, &numrecords) == 0);
5892 for (i = 0; i < numrecords; i++) {
5893 nvlist_t *rec = records[i];
5896 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5900 tsec = fnvlist_lookup_uint64(records[i],
5902 (void) localtime_r(&tsec, &t);
5903 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5906 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5907 (void) printf("%s %s", tbuf,
5908 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5909 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5911 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5914 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5915 (void) printf("%s unrecognized record:\n",
5917 dump_nvlist(rec, 4);
5920 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5921 zfs_history_event_names[ievent],
5922 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5923 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5924 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5927 (void) printf("%s [txg:%lld] %s", tbuf,
5928 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5929 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5930 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5931 (void) printf(" %s (%llu)",
5932 fnvlist_lookup_string(rec,
5934 fnvlist_lookup_uint64(rec,
5937 (void) printf(" %s", fnvlist_lookup_string(rec,
5938 ZPOOL_HIST_INT_STR));
5939 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5942 (void) printf("%s ioctl %s\n", tbuf,
5943 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5944 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5945 (void) printf(" input:\n");
5946 dump_nvlist(fnvlist_lookup_nvlist(rec,
5947 ZPOOL_HIST_INPUT_NVL), 8);
5949 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5950 (void) printf(" output:\n");
5951 dump_nvlist(fnvlist_lookup_nvlist(rec,
5952 ZPOOL_HIST_OUTPUT_NVL), 8);
5954 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
5955 (void) printf(" errno: %lld\n",
5956 fnvlist_lookup_int64(rec,
5962 (void) printf("%s unrecognized record:\n", tbuf);
5963 dump_nvlist(rec, 4);
5967 (void) printf("\n");
5970 (void) printf(" [");
5971 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5972 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5973 struct passwd *pwd = getpwuid(who);
5974 (void) printf("user %d ", (int)who);
5976 (void) printf("(%s) ", pwd->pw_name);
5978 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5979 (void) printf("on %s",
5980 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5982 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5983 (void) printf(":%s",
5984 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5987 (void) printf("\n");
5989 (void) printf("\n");
5996 * zpool history <pool>
5998 * Displays the history of commands that modified pools.
6001 zpool_do_history(int argc, char **argv)
6003 hist_cbdata_t cbdata = { 0 };
6007 cbdata.first = B_TRUE;
6009 while ((c = getopt(argc, argv, "li")) != -1) {
6012 cbdata.longfmt = B_TRUE;
6015 cbdata.internal = B_TRUE;
6018 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6026 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
6029 if (argc == 0 && cbdata.first == B_TRUE) {
6030 (void) printf(gettext("no pools available\n"));
6038 get_callback(zpool_handle_t *zhp, void *data)
6040 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
6041 char value[MAXNAMELEN];
6042 zprop_source_t srctype;
6045 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
6048 * Skip the special fake placeholder. This will also skip
6049 * over the name property when 'all' is specified.
6051 if (pl->pl_prop == ZPOOL_PROP_NAME &&
6052 pl == cbp->cb_proplist)
6055 if (pl->pl_prop == ZPROP_INVAL &&
6056 (zpool_prop_feature(pl->pl_user_prop) ||
6057 zpool_prop_unsupported(pl->pl_user_prop))) {
6058 srctype = ZPROP_SRC_LOCAL;
6060 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
6061 value, sizeof (value)) == 0) {
6062 zprop_print_one_property(zpool_get_name(zhp),
6063 cbp, pl->pl_user_prop, value, srctype,
6067 if (zpool_get_prop(zhp, pl->pl_prop, value,
6068 sizeof (value), &srctype, cbp->cb_literal) != 0)
6071 zprop_print_one_property(zpool_get_name(zhp), cbp,
6072 zpool_prop_to_name(pl->pl_prop), value, srctype,
6080 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
6082 * -H Scripted mode. Don't display headers, and separate properties
6084 * -o List of columns to display. Defaults to
6085 * "name,property,value,source".
6086 * -p Diplay values in parsable (exact) format.
6088 * Get properties of pools in the system. Output space statistics
6089 * for each one as well as other attributes.
6092 zpool_do_get(int argc, char **argv)
6094 zprop_get_cbdata_t cb = { 0 };
6095 zprop_list_t fake_name = { 0 };
6100 cb.cb_first = B_TRUE;
6103 * Set up default columns and sources.
6105 cb.cb_sources = ZPROP_SRC_ALL;
6106 cb.cb_columns[0] = GET_COL_NAME;
6107 cb.cb_columns[1] = GET_COL_PROPERTY;
6108 cb.cb_columns[2] = GET_COL_VALUE;
6109 cb.cb_columns[3] = GET_COL_SOURCE;
6110 cb.cb_type = ZFS_TYPE_POOL;
6113 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
6116 cb.cb_literal = B_TRUE;
6119 cb.cb_scripted = B_TRUE;
6122 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
6124 while (*optarg != '\0') {
6125 static char *col_subopts[] =
6126 { "name", "property", "value", "source",
6129 if (i == ZFS_GET_NCOLS) {
6130 (void) fprintf(stderr, gettext("too "
6131 "many fields given to -o "
6136 switch (getsubopt(&optarg, col_subopts,
6139 cb.cb_columns[i++] = GET_COL_NAME;
6142 cb.cb_columns[i++] = GET_COL_PROPERTY;
6145 cb.cb_columns[i++] = GET_COL_VALUE;
6148 cb.cb_columns[i++] = GET_COL_SOURCE;
6152 (void) fprintf(stderr,
6153 gettext("\"all\" conflicts "
6154 "with specific fields "
6155 "given to -o option\n"));
6158 cb.cb_columns[0] = GET_COL_NAME;
6159 cb.cb_columns[1] = GET_COL_PROPERTY;
6160 cb.cb_columns[2] = GET_COL_VALUE;
6161 cb.cb_columns[3] = GET_COL_SOURCE;
6165 (void) fprintf(stderr,
6166 gettext("invalid column name "
6167 "'%s'\n"), suboptarg);
6173 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6183 (void) fprintf(stderr, gettext("missing property "
6188 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
6189 ZFS_TYPE_POOL) != 0)
6195 if (cb.cb_proplist != NULL) {
6196 fake_name.pl_prop = ZPOOL_PROP_NAME;
6197 fake_name.pl_width = strlen(gettext("NAME"));
6198 fake_name.pl_next = cb.cb_proplist;
6199 cb.cb_proplist = &fake_name;
6202 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
6205 if (cb.cb_proplist == &fake_name)
6206 zprop_free_list(fake_name.pl_next);
6208 zprop_free_list(cb.cb_proplist);
6213 typedef struct set_cbdata {
6216 boolean_t cb_any_successful;
6220 set_callback(zpool_handle_t *zhp, void *data)
6223 set_cbdata_t *cb = (set_cbdata_t *)data;
6225 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
6228 cb->cb_any_successful = B_TRUE;
6234 zpool_do_set(int argc, char **argv)
6236 set_cbdata_t cb = { 0 };
6239 if (argc > 1 && argv[1][0] == '-') {
6240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6246 (void) fprintf(stderr, gettext("missing property=value "
6252 (void) fprintf(stderr, gettext("missing pool name\n"));
6257 (void) fprintf(stderr, gettext("too many pool names\n"));
6261 cb.cb_propname = argv[1];
6262 cb.cb_value = strchr(cb.cb_propname, '=');
6263 if (cb.cb_value == NULL) {
6264 (void) fprintf(stderr, gettext("missing value in "
6265 "property=value argument\n"));
6269 *(cb.cb_value) = '\0';
6272 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
6279 find_command_idx(char *command, int *idx)
6283 for (i = 0; i < NCOMMAND; i++) {
6284 if (command_table[i].name == NULL)
6287 if (strcmp(command, command_table[i].name) == 0) {
6296 main(int argc, char **argv)
6302 (void) setlocale(LC_ALL, "");
6303 (void) textdomain(TEXT_DOMAIN);
6305 if ((g_zfs = libzfs_init()) == NULL) {
6306 (void) fprintf(stderr, gettext("internal error: failed to "
6307 "initialize ZFS library\n"));
6311 libzfs_print_on_error(g_zfs, B_TRUE);
6316 * Make sure the user has specified some command.
6319 (void) fprintf(stderr, gettext("missing command\n"));
6328 if (strcmp(cmdname, "-?") == 0)
6331 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6334 * Run the appropriate command.
6336 if (find_command_idx(cmdname, &i) == 0) {
6337 current_command = &command_table[i];
6338 ret = command_table[i].func(argc - 1, argv + 1);
6339 } else if (strchr(cmdname, '=')) {
6340 verify(find_command_idx("set", &i) == 0);
6341 current_command = &command_table[i];
6342 ret = command_table[i].func(argc, argv);
6343 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
6345 * 'freeze' is a vile debugging abomination, so we treat
6348 zfs_cmd_t zc = { 0 };
6349 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
6350 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
6352 (void) fprintf(stderr, gettext("unrecognized "
6353 "command '%s'\n"), cmdname);
6357 if (ret == 0 && log_history)
6358 (void) zpool_log_history(g_zfs, history_str);
6363 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6364 * for the purposes of running ::findleaks.
6366 if (getenv("ZFS_ABORT") != NULL) {
6367 (void) printf("dumping core by request\n");