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.
31 * Copyright (c) 2017, Intel Corporation.
55 #include <sys/fs/zfs.h>
57 #include <sys/debug.h>
61 #include "zpool_util.h"
62 #include "zfs_comutil.h"
63 #include "zfeature_common.h"
65 #include "statcommon.h"
67 libzfs_handle_t *g_zfs;
69 static int zpool_do_create(int, char **);
70 static int zpool_do_destroy(int, char **);
72 static int zpool_do_add(int, char **);
73 static int zpool_do_remove(int, char **);
74 static int zpool_do_labelclear(int, char **);
76 static int zpool_do_checkpoint(int, char **);
78 static int zpool_do_list(int, char **);
79 static int zpool_do_iostat(int, char **);
80 static int zpool_do_status(int, char **);
82 static int zpool_do_online(int, char **);
83 static int zpool_do_offline(int, char **);
84 static int zpool_do_clear(int, char **);
85 static int zpool_do_reopen(int, char **);
87 static int zpool_do_reguid(int, char **);
89 static int zpool_do_attach(int, char **);
90 static int zpool_do_detach(int, char **);
91 static int zpool_do_replace(int, char **);
92 static int zpool_do_split(int, char **);
94 static int zpool_do_initialize(int, char **);
95 static int zpool_do_scrub(int, char **);
97 static int zpool_do_import(int, char **);
98 static int zpool_do_export(int, char **);
100 static int zpool_do_upgrade(int, char **);
102 static int zpool_do_history(int, char **);
104 static int zpool_do_get(int, char **);
105 static int zpool_do_set(int, char **);
107 static int zpool_do_sync(int, char **);
110 * These libumem hooks provide a reasonable set of defaults for the allocator's
111 * debugging facilities.
116 _umem_debug_init(void)
118 return ("default,verbose"); /* $UMEM_DEBUG setting */
122 _umem_logging_init(void)
124 return ("fail,contents"); /* $UMEM_LOGGING setting */
159 typedef struct zpool_command {
161 int (*func)(int, char **);
166 * Master command table. Each ZFS command has a name, associated function, and
167 * usage message. The usage messages need to be internationalized, so we have
168 * to have a function to return the usage message based on a command index.
170 * These commands are organized according to how they are displayed in the usage
171 * message. An empty command (one with a NULL name) indicates an empty line in
172 * the generic usage message.
174 static zpool_command_t command_table[] = {
175 { "create", zpool_do_create, HELP_CREATE },
176 { "destroy", zpool_do_destroy, HELP_DESTROY },
178 { "add", zpool_do_add, HELP_ADD },
179 { "remove", zpool_do_remove, HELP_REMOVE },
181 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
183 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
185 { "list", zpool_do_list, HELP_LIST },
186 { "iostat", zpool_do_iostat, HELP_IOSTAT },
187 { "status", zpool_do_status, HELP_STATUS },
189 { "online", zpool_do_online, HELP_ONLINE },
190 { "offline", zpool_do_offline, HELP_OFFLINE },
191 { "clear", zpool_do_clear, HELP_CLEAR },
192 { "reopen", zpool_do_reopen, HELP_REOPEN },
194 { "attach", zpool_do_attach, HELP_ATTACH },
195 { "detach", zpool_do_detach, HELP_DETACH },
196 { "replace", zpool_do_replace, HELP_REPLACE },
197 { "split", zpool_do_split, HELP_SPLIT },
199 { "initialize", zpool_do_initialize, HELP_INITIALIZE },
200 { "scrub", zpool_do_scrub, HELP_SCRUB },
202 { "import", zpool_do_import, HELP_IMPORT },
203 { "export", zpool_do_export, HELP_EXPORT },
204 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
205 { "reguid", zpool_do_reguid, HELP_REGUID },
207 { "history", zpool_do_history, HELP_HISTORY },
208 { "get", zpool_do_get, HELP_GET },
209 { "set", zpool_do_set, HELP_SET },
210 { "sync", zpool_do_sync, HELP_SYNC },
213 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
215 #define VDEV_ALLOC_CLASS_LOGS "logs"
217 static zpool_command_t *current_command;
218 static char history_str[HIS_MAX_RECORD_LEN];
219 static boolean_t log_history = B_TRUE;
220 static uint_t timestamp_fmt = NODATE;
223 get_usage(zpool_help_t idx)
227 return (gettext("\tadd [-fgLnP] <pool> <vdev> ...\n"));
229 return (gettext("\tattach [-f] <pool> <device> "
232 return (gettext("\tclear [-nF] <pool> [device]\n"));
234 return (gettext("\tcreate [-fnd] [-B] "
235 "[-o property=value] ... \n"
236 "\t [-O file-system-property=value] ...\n"
237 "\t [-m mountpoint] [-R root] [-t tempname] "
238 "<pool> <vdev> ...\n"));
239 case HELP_CHECKPOINT:
240 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
242 return (gettext("\tdestroy [-f] <pool>\n"));
244 return (gettext("\tdetach <pool> <device>\n"));
246 return (gettext("\texport [-f] <pool> ...\n"));
248 return (gettext("\thistory [-il] [<pool>] ...\n"));
250 return (gettext("\timport [-d dir] [-D]\n"
251 "\timport [-o mntopts] [-o property=value] ... \n"
252 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
253 "[-R root] [-F [-n]] -a\n"
254 "\timport [-o mntopts] [-o property=value] ... \n"
255 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
256 "[-R root] [-F [-n]] [-t]\n"
257 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
259 return (gettext("\tiostat [-gLPv] [-T d|u] [pool] ... "
260 "[interval [count]]\n"));
261 case HELP_LABELCLEAR:
262 return (gettext("\tlabelclear [-f] <vdev>\n"));
264 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
265 "[-T d|u] [pool] ... [interval [count]]\n"));
267 return (gettext("\toffline [-t] <pool> <device> ...\n"));
269 return (gettext("\tonline [-e] <pool> <device> ...\n"));
271 return (gettext("\treplace [-f] <pool> <device> "
274 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
276 return (gettext("\treopen <pool>\n"));
277 case HELP_INITIALIZE:
278 return (gettext("\tinitialize [-cs] <pool> [<device> ...]\n"));
280 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
282 return (gettext("\tstatus [-DgLPvx] [-T d|u] [pool] ... "
283 "[interval [count]]\n"));
285 return (gettext("\tupgrade [-v]\n"
286 "\tupgrade [-V version] <-a | pool ...>\n"));
288 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
289 "<\"all\" | property[,...]> <pool> ...\n"));
291 return (gettext("\tset <property=value> <pool> \n"));
293 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
294 "\t [-o property=value] <pool> <newpool> "
295 "[<device> ...]\n"));
297 return (gettext("\treguid <pool>\n"));
299 return (gettext("\tsync [pool] ...\n"));
308 * Callback routine that will print out a pool property value.
311 print_prop_cb(int prop, void *cb)
315 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop));
317 if (zpool_prop_readonly(prop))
318 (void) fprintf(fp, " NO ");
320 (void) fprintf(fp, " YES ");
322 if (zpool_prop_values(prop) == NULL)
323 (void) fprintf(fp, "-\n");
325 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
331 * Display usage message. If we're inside a command, display only the usage for
332 * that command. Otherwise, iterate over the entire command table and display
333 * a complete usage message.
336 usage(boolean_t requested)
338 FILE *fp = requested ? stdout : stderr;
340 if (current_command == NULL) {
343 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
345 gettext("where 'command' is one of the following:\n\n"));
347 for (i = 0; i < NCOMMAND; i++) {
348 if (command_table[i].name == NULL)
349 (void) fprintf(fp, "\n");
351 (void) fprintf(fp, "%s",
352 get_usage(command_table[i].usage));
355 (void) fprintf(fp, gettext("usage:\n"));
356 (void) fprintf(fp, "%s", get_usage(current_command->usage));
359 if (current_command != NULL &&
360 ((strcmp(current_command->name, "set") == 0) ||
361 (strcmp(current_command->name, "get") == 0) ||
362 (strcmp(current_command->name, "list") == 0))) {
365 gettext("\nthe following properties are supported:\n"));
367 (void) fprintf(fp, "\n\t%-19s %s %s\n\n",
368 "PROPERTY", "EDIT", "VALUES");
370 /* Iterate over all properties */
371 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
374 (void) fprintf(fp, "\t%-19s ", "feature@...");
375 (void) fprintf(fp, "YES disabled | enabled | active\n");
377 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
378 "appended with a feature name.\nSee zpool-features(7).\n"));
382 * See comments at end of main().
384 if (getenv("ZFS_ABORT") != NULL) {
385 (void) printf("dumping core by request\n");
389 exit(requested ? 0 : 2);
393 * print a pool vdev config for dry runs
396 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
397 const char *match, int name_flags)
402 boolean_t printed = B_FALSE;
404 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
405 &child, &children) != 0) {
407 (void) printf("\t%*s%s\n", indent, "", name);
411 for (c = 0; c < children; c++) {
412 uint64_t is_log = B_FALSE;
415 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
418 class = VDEV_ALLOC_BIAS_LOG;
419 (void) nvlist_lookup_string(child[c],
420 ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
421 if (strcmp(match, class) != 0)
424 if (!printed && name != NULL) {
425 (void) printf("\t%*s%s\n", indent, "", name);
428 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
429 print_vdev_tree(zhp, vname, child[c], indent + 2, "",
436 prop_list_contains_feature(nvlist_t *proplist)
439 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
440 nvp = nvlist_next_nvpair(proplist, nvp)) {
441 if (zpool_prop_feature(nvpair_name(nvp)))
448 * Add a property pair (name, string-value) into a property nvlist.
451 add_prop_list(const char *propname, char *propval, nvlist_t **props,
454 zpool_prop_t prop = ZPROP_INVAL;
460 if (*props == NULL &&
461 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
462 (void) fprintf(stderr,
463 gettext("internal error: out of memory\n"));
470 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
472 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
473 !zpool_prop_feature(propname)) {
474 (void) fprintf(stderr, gettext("property '%s' is "
475 "not a valid pool property\n"), propname);
480 * feature@ properties and version should not be specified
483 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
484 nvlist_exists(proplist, vname)) ||
485 (prop == ZPOOL_PROP_VERSION &&
486 prop_list_contains_feature(proplist))) {
487 (void) fprintf(stderr, gettext("'feature@' and "
488 "'version' properties cannot be specified "
494 if (zpool_prop_feature(propname))
497 normnm = zpool_prop_to_name(prop);
499 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
500 normnm = zfs_prop_to_name(fprop);
506 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
507 prop != ZPOOL_PROP_CACHEFILE) {
508 (void) fprintf(stderr, gettext("property '%s' "
509 "specified multiple times\n"), propname);
513 if (nvlist_add_string(proplist, normnm, propval) != 0) {
514 (void) fprintf(stderr, gettext("internal "
515 "error: out of memory\n"));
523 * Set a default property pair (name, string-value) in a property nvlist
526 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
531 if (nvlist_lookup_string(*props, propname, &pval) == 0)
534 return (add_prop_list(propname, propval, props, poolprop));
538 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
540 * -f Force addition of devices, even if they appear in use
541 * -g Display guid for individual vdev name.
542 * -L Follow links when resolving vdev path name.
543 * -n Do not add the devices, but display the resulting layout if
544 * they were to be added.
545 * -P Display full path for vdev name.
547 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
548 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
552 zpool_do_add(int argc, char **argv)
554 boolean_t force = B_FALSE;
555 boolean_t dryrun = B_FALSE;
560 zpool_boot_label_t boot_type;
567 while ((c = getopt(argc, argv, "fgLnP")) != -1) {
573 name_flags |= VDEV_NAME_GUID;
576 name_flags |= VDEV_NAME_FOLLOW_LINKS;
582 name_flags |= VDEV_NAME_PATH;
585 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
594 /* get pool name and check number of arguments */
596 (void) fprintf(stderr, gettext("missing pool name argument\n"));
600 (void) fprintf(stderr, gettext("missing vdev specification\n"));
609 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
612 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
613 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
619 if (zpool_is_bootable(zhp))
620 boot_type = ZPOOL_COPY_BOOT_LABEL;
622 boot_type = ZPOOL_NO_BOOT_LABEL;
624 /* pass off to get_vdev_spec for processing */
625 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
626 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
627 boot_type, boot_size, argc, argv);
628 if (nvroot == NULL) {
634 nvlist_t *poolnvroot;
636 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
639 (void) printf(gettext("would update '%s' to the following "
640 "configuration:\n"), zpool_get_name(zhp));
642 /* print original main pool and new tree */
643 print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
644 name_flags | VDEV_NAME_TYPE_ID);
645 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
647 /* print other classes: 'dedup', 'special', and 'log' */
648 print_vdev_tree(zhp, "dedup", poolnvroot, 0,
649 VDEV_ALLOC_BIAS_DEDUP, name_flags);
650 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_DEDUP,
653 print_vdev_tree(zhp, "special", poolnvroot, 0,
654 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
655 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_SPECIAL,
658 print_vdev_tree(zhp, "logs", poolnvroot, 0, VDEV_ALLOC_BIAS_LOG,
660 print_vdev_tree(zhp, NULL, nvroot, 0, VDEV_ALLOC_BIAS_LOG,
665 ret = (zpool_add(zhp, nvroot) != 0);
675 * zpool remove <pool> <vdev> ...
677 * Removes the given vdev from the pool.
680 zpool_do_remove(int argc, char **argv)
685 boolean_t stop = B_FALSE;
686 boolean_t noop = B_FALSE;
687 boolean_t parsable = B_FALSE;
691 while ((c = getopt(argc, argv, "nps")) != -1) {
703 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
712 /* get pool name and check number of arguments */
714 (void) fprintf(stderr, gettext("missing pool name argument\n"));
720 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
724 (void) fprintf(stderr, gettext("stop request ignored\n"));
730 (void) fprintf(stderr, gettext("too many arguments\n"));
733 if (zpool_vdev_remove_cancel(zhp) != 0)
737 (void) fprintf(stderr, gettext("missing device\n"));
741 for (i = 1; i < argc; i++) {
745 if (zpool_vdev_indirect_size(zhp, argv[i],
751 (void) printf("%s %llu\n",
755 zfs_nicenum(size, valstr,
757 (void) printf("Memory that will be "
758 "used after removing %s: %s\n",
762 if (zpool_vdev_remove(zhp, argv[i]) != 0)
772 * zpool labelclear [-f] <vdev>
774 * -f Force clearing the label for the vdevs which are members of
775 * the exported or foreign pools.
777 * Verifies that the vdev is not active and zeros out the label information
781 zpool_do_labelclear(int argc, char **argv)
783 char vdev[MAXPATHLEN];
789 boolean_t inuse = B_FALSE;
790 boolean_t force = B_FALSE;
793 while ((c = getopt(argc, argv, "f")) != -1) {
799 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
810 (void) fprintf(stderr, gettext("missing vdev name\n"));
814 (void) fprintf(stderr, gettext("too many arguments\n"));
819 * Check if we were given absolute path and use it as is.
820 * Otherwise if the provided vdev name doesn't point to a file,
821 * try prepending dsk path and appending s0.
823 (void) strlcpy(vdev, argv[0], sizeof (vdev));
824 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
827 (void) snprintf(vdev, sizeof (vdev), "%s/%s",
829 ZFS_DISK_ROOT, argv[0]);
830 if ((s = strrchr(argv[0], 's')) == NULL ||
832 (void) strlcat(vdev, "s0", sizeof (vdev));
836 if (stat(vdev, &st) != 0) {
837 (void) fprintf(stderr, gettext(
838 "failed to find device %s, try specifying absolute "
839 "path instead\n"), argv[0]);
844 if ((fd = open(vdev, O_RDWR)) < 0) {
845 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
846 vdev, strerror(errno));
850 if (zpool_read_label(fd, &config) != 0) {
851 (void) fprintf(stderr,
852 gettext("failed to read label from %s\n"), vdev);
857 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
859 (void) fprintf(stderr,
860 gettext("failed to check state for %s\n"), vdev);
869 case POOL_STATE_ACTIVE:
870 case POOL_STATE_SPARE:
871 case POOL_STATE_L2CACHE:
872 (void) fprintf(stderr, gettext(
873 "%s is a member (%s) of pool \"%s\"\n"),
874 vdev, zpool_pool_state_to_name(state), name);
878 case POOL_STATE_EXPORTED:
881 (void) fprintf(stderr, gettext(
882 "use '-f' to override the following error:\n"
883 "%s is a member of exported pool \"%s\"\n"),
888 case POOL_STATE_POTENTIALLY_ACTIVE:
891 (void) fprintf(stderr, gettext(
892 "use '-f' to override the following error:\n"
893 "%s is a member of potentially active pool \"%s\"\n"),
898 case POOL_STATE_DESTROYED:
899 /* inuse should never be set for a destroyed pool */
905 ret = zpool_clear_label(fd);
907 (void) fprintf(stderr,
908 gettext("failed to clear label for %s\n"), vdev);
919 * zpool create [-fnd] [-B] [-o property=value] ...
920 * [-O file-system-property=value] ...
921 * [-R root] [-m mountpoint] [-t tempname] <pool> <dev> ...
923 * -B Create boot partition.
924 * -f Force creation, even if devices appear in use
925 * -n Do not create the pool, but display the resulting layout if it
926 * were to be created.
927 * -R Create a pool under an alternate root
928 * -m Set default mountpoint for the root dataset. By default it's
930 * -t Use the temporary name until the pool is exported.
931 * -o Set property=value.
932 * -d Don't automatically enable all supported pool features
933 * (individual features can be enabled with -o).
934 * -O Set fsproperty=value in the pool's root file system
936 * Creates the named pool according to the given vdev specification. The
937 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
938 * we get the nvlist back from get_vdev_spec(), we either print out the contents
939 * (if '-n' was specified), or pass it to libzfs to do the creation.
942 #define SYSTEM256 (256 * 1024 * 1024)
944 zpool_do_create(int argc, char **argv)
946 boolean_t force = B_FALSE;
947 boolean_t dryrun = B_FALSE;
948 boolean_t enable_all_pool_feat = B_TRUE;
949 zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
950 uint64_t boot_size = 0;
952 nvlist_t *nvroot = NULL;
956 char *altroot = NULL;
957 char *mountpoint = NULL;
958 nvlist_t *fsprops = NULL;
959 nvlist_t *props = NULL;
963 while ((c = getopt(argc, argv, ":fndBR:m:o:O:t:")) != -1) {
972 enable_all_pool_feat = B_FALSE;
977 * We should create the system partition.
978 * Also make sure the size is set.
980 boot_type = ZPOOL_CREATE_BOOT_LABEL;
982 boot_size = SYSTEM256;
985 (void) fprintf(stderr,
986 gettext("option '%c' is not supported\n"),
992 if (add_prop_list(zpool_prop_to_name(
993 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
995 if (add_prop_list_default(zpool_prop_to_name(
996 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1000 /* Equivalent to -O mountpoint=optarg */
1001 mountpoint = optarg;
1004 if ((propval = strchr(optarg, '=')) == NULL) {
1005 (void) fprintf(stderr, gettext("missing "
1006 "'=' for -o option\n"));
1012 if (add_prop_list(optarg, propval, &props, B_TRUE))
1016 * Get bootsize value for make_root_vdev().
1018 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_BOOTSIZE) {
1019 if (zfs_nicestrtonum(g_zfs, propval,
1020 &boot_size) < 0 || boot_size == 0) {
1021 (void) fprintf(stderr,
1022 gettext("bad boot partition size "
1023 "'%s': %s\n"), propval,
1024 libzfs_error_description(g_zfs));
1030 * If the user is creating a pool that doesn't support
1031 * feature flags, don't enable any features.
1033 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1037 ver = strtoull(propval, &end, 10);
1039 ver < SPA_VERSION_FEATURES) {
1040 enable_all_pool_feat = B_FALSE;
1043 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1047 if ((propval = strchr(optarg, '=')) == NULL) {
1048 (void) fprintf(stderr, gettext("missing "
1049 "'=' for -O option\n"));
1056 * Mountpoints are checked and then added later.
1057 * Uniquely among properties, they can be specified
1058 * more than once, to avoid conflict with -m.
1060 if (0 == strcmp(optarg,
1061 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1062 mountpoint = propval;
1063 } else if (add_prop_list(optarg, propval, &fsprops,
1070 * Sanity check temporary pool name.
1072 if (strchr(optarg, '/') != NULL) {
1073 (void) fprintf(stderr, gettext("cannot create "
1074 "'%s': invalid character '/' in temporary "
1076 (void) fprintf(stderr, gettext("use 'zfs "
1077 "create' to create a dataset\n"));
1081 if (add_prop_list(zpool_prop_to_name(
1082 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1084 if (add_prop_list_default(zpool_prop_to_name(
1085 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1090 (void) fprintf(stderr, gettext("missing argument for "
1091 "'%c' option\n"), optopt);
1094 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1103 /* get pool name and check number of arguments */
1105 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1109 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1116 * As a special case, check for use of '/' in the name, and direct the
1117 * user to use 'zfs create' instead.
1119 if (strchr(poolname, '/') != NULL) {
1120 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1121 "character '/' in pool name\n"), poolname);
1122 (void) fprintf(stderr, gettext("use 'zfs create' to "
1123 "create a dataset\n"));
1128 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
1129 * and not set otherwise.
1131 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
1132 const char *propname;
1133 char *strptr, *buf = NULL;
1136 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
1137 if (nvlist_lookup_string(props, propname, &strptr) != 0) {
1138 (void) asprintf(&buf, "%" PRIu64, boot_size);
1140 (void) fprintf(stderr,
1141 gettext("internal error: out of memory\n"));
1144 rv = add_prop_list(propname, buf, &props, B_TRUE);
1150 const char *propname;
1153 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
1154 if (nvlist_lookup_string(props, propname, &strptr) == 0) {
1155 (void) fprintf(stderr, gettext("error: setting boot "
1156 "partition size requires option '-B'\n"));
1161 /* pass off to get_vdev_spec for bulk processing */
1162 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
1163 boot_type, boot_size, argc - 1, argv + 1);
1167 /* make_root_vdev() allows 0 toplevel children if there are spares */
1168 if (!zfs_allocatable_devs(nvroot)) {
1169 (void) fprintf(stderr, gettext("invalid vdev "
1170 "specification: at least one toplevel vdev must be "
1175 if (altroot != NULL && altroot[0] != '/') {
1176 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1177 "must be an absolute path\n"), altroot);
1182 * Check the validity of the mountpoint and direct the user to use the
1183 * '-m' mountpoint option if it looks like its in use.
1184 * Ignore the checks if the '-f' option is given.
1186 if (!force && (mountpoint == NULL ||
1187 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1188 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) {
1189 char buf[MAXPATHLEN];
1192 if (mountpoint && mountpoint[0] != '/') {
1193 (void) fprintf(stderr, gettext("invalid mountpoint "
1194 "'%s': must be an absolute path, 'legacy', or "
1195 "'none'\n"), mountpoint);
1199 if (mountpoint == NULL) {
1200 if (altroot != NULL)
1201 (void) snprintf(buf, sizeof (buf), "%s/%s",
1204 (void) snprintf(buf, sizeof (buf), "/%s",
1207 if (altroot != NULL)
1208 (void) snprintf(buf, sizeof (buf), "%s%s",
1209 altroot, mountpoint);
1211 (void) snprintf(buf, sizeof (buf), "%s",
1215 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1216 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1217 "%s\n"), buf, strerror(errno));
1218 (void) fprintf(stderr, gettext("use '-m' "
1219 "option to provide a different default\n"));
1224 while (count < 3 && readdir(dirp) != NULL)
1226 (void) closedir(dirp);
1229 (void) fprintf(stderr, gettext("mountpoint "
1230 "'%s' exists and is not empty\n"), buf);
1231 (void) fprintf(stderr, gettext("use '-m' "
1232 "option to provide a "
1233 "different default\n"));
1240 * Now that the mountpoint's validity has been checked, ensure that
1241 * the property is set appropriately prior to creating the pool.
1243 if (mountpoint != NULL) {
1244 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1245 mountpoint, &fsprops, B_FALSE);
1253 * For a dry run invocation, print out a basic message and run
1254 * through all the vdevs in the list and print out in an
1255 * appropriate hierarchy.
1257 (void) printf(gettext("would create '%s' with the "
1258 "following layout:\n\n"), poolname);
1260 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
1261 print_vdev_tree(NULL, "dedup", nvroot, 0,
1262 VDEV_ALLOC_BIAS_DEDUP, 0);
1263 print_vdev_tree(NULL, "special", nvroot, 0,
1264 VDEV_ALLOC_BIAS_SPECIAL, 0);
1265 print_vdev_tree(NULL, "logs", nvroot, 0,
1266 VDEV_ALLOC_BIAS_LOG, 0);
1271 * Hand off to libzfs.
1273 if (enable_all_pool_feat) {
1275 for (i = 0; i < SPA_FEATURES; i++) {
1276 char propname[MAXPATHLEN];
1277 zfeature_info_t *feat = &spa_feature_table[i];
1279 (void) snprintf(propname, sizeof (propname),
1280 "feature@%s", feat->fi_uname);
1283 * Skip feature if user specified it manually
1284 * on the command line.
1286 if (nvlist_exists(props, propname))
1289 ret = add_prop_list(propname,
1290 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1297 if (zpool_create(g_zfs, poolname,
1298 nvroot, props, fsprops) == 0) {
1299 zfs_handle_t *pool = zfs_open(g_zfs,
1300 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1302 if (zfs_mount(pool, NULL, 0) == 0)
1303 ret = zfs_shareall(pool);
1306 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1307 (void) fprintf(stderr, gettext("pool name may have "
1313 nvlist_free(nvroot);
1314 nvlist_free(fsprops);
1318 nvlist_free(fsprops);
1325 * zpool destroy <pool>
1327 * -f Forcefully unmount any datasets
1329 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1332 zpool_do_destroy(int argc, char **argv)
1334 boolean_t force = B_FALSE;
1337 zpool_handle_t *zhp;
1341 while ((c = getopt(argc, argv, "f")) != -1) {
1347 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1356 /* check arguments */
1358 (void) fprintf(stderr, gettext("missing pool argument\n"));
1362 (void) fprintf(stderr, gettext("too many arguments\n"));
1368 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1370 * As a special case, check for use of '/' in the name, and
1371 * direct the user to use 'zfs destroy' instead.
1373 if (strchr(pool, '/') != NULL)
1374 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1375 "destroy a dataset\n"));
1379 if (zpool_disable_datasets(zhp, force) != 0) {
1380 (void) fprintf(stderr, gettext("could not destroy '%s': "
1381 "could not unmount datasets\n"), zpool_get_name(zhp));
1385 /* The history must be logged as part of the export */
1386 log_history = B_FALSE;
1388 ret = (zpool_destroy(zhp, history_str) != 0);
1396 * zpool export [-f] <pool> ...
1398 * -f Forcefully unmount datasets
1400 * Export the given pools. By default, the command will attempt to cleanly
1401 * unmount any active datasets within the pool. If the '-f' flag is specified,
1402 * then the datasets will be forcefully unmounted.
1405 zpool_do_export(int argc, char **argv)
1407 boolean_t force = B_FALSE;
1408 boolean_t hardforce = B_FALSE;
1410 zpool_handle_t *zhp;
1415 while ((c = getopt(argc, argv, "fF")) != -1) {
1424 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1433 /* check arguments */
1435 (void) fprintf(stderr, gettext("missing pool argument\n"));
1440 for (i = 0; i < argc; i++) {
1441 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1446 if (zpool_disable_datasets(zhp, force) != 0) {
1452 /* The history must be logged as part of the export */
1453 log_history = B_FALSE;
1456 if (zpool_export_force(zhp, history_str) != 0)
1458 } else if (zpool_export(zhp, force, history_str) != 0) {
1469 * Given a vdev configuration, determine the maximum width needed for the device
1473 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1481 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags | VDEV_NAME_TYPE_ID);
1482 if (strlen(name) + depth > max)
1483 max = strlen(name) + depth;
1487 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1488 &child, &children) == 0) {
1489 for (c = 0; c < children; c++)
1490 if ((ret = max_width(zhp, child[c], depth + 2,
1491 max, name_flags)) > max)
1495 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1496 &child, &children) == 0) {
1497 for (c = 0; c < children; c++)
1498 if ((ret = max_width(zhp, child[c], depth + 2,
1499 max, name_flags)) > max)
1503 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1504 &child, &children) == 0) {
1505 for (c = 0; c < children; c++)
1506 if ((ret = max_width(zhp, child[c], depth + 2,
1507 max, name_flags)) > max)
1514 typedef struct spare_cbdata {
1516 zpool_handle_t *cb_zhp;
1520 find_vdev(nvlist_t *nv, uint64_t search)
1526 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1530 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1531 &child, &children) == 0) {
1532 for (c = 0; c < children; c++)
1533 if (find_vdev(child[c], search))
1541 find_spare(zpool_handle_t *zhp, void *data)
1543 spare_cbdata_t *cbp = data;
1544 nvlist_t *config, *nvroot;
1546 config = zpool_get_config(zhp, NULL);
1547 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1550 if (find_vdev(nvroot, cbp->cb_guid)) {
1559 typedef struct status_cbdata {
1563 boolean_t cb_allpools;
1564 boolean_t cb_verbose;
1565 boolean_t cb_explain;
1567 boolean_t cb_dedup_stats;
1568 boolean_t cb_print_status;
1572 * Print out configuration state as requested by status_callback.
1575 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
1576 nvlist_t *nv, int depth, boolean_t isspare)
1579 uint_t c, vsc, children;
1580 pool_scan_stat_t *ps = NULL;
1582 char rbuf[6], wbuf[6], cbuf[6];
1584 uint64_t notpresent;
1586 spare_cbdata_t spare_cb;
1590 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1591 &child, &children) != 0)
1594 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1595 (uint64_t **)&vs, &vsc) == 0);
1597 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1599 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
1602 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1605 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1608 if (vs->vs_aux == VDEV_AUX_SPARED)
1610 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1614 (void) printf("\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
1618 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1619 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1620 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1621 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1624 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1625 ¬present) == 0 ||
1626 vs->vs_state <= VDEV_STATE_CANT_OPEN) {
1628 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0)
1629 (void) printf(" was %s", path);
1630 } else if (vs->vs_aux != 0) {
1633 switch (vs->vs_aux) {
1634 case VDEV_AUX_OPEN_FAILED:
1635 (void) printf(gettext("cannot open"));
1638 case VDEV_AUX_BAD_GUID_SUM:
1639 (void) printf(gettext("missing device"));
1642 case VDEV_AUX_NO_REPLICAS:
1643 (void) printf(gettext("insufficient replicas"));
1646 case VDEV_AUX_VERSION_NEWER:
1647 (void) printf(gettext("newer version"));
1650 case VDEV_AUX_UNSUP_FEAT:
1651 (void) printf(gettext("unsupported feature(s)"));
1654 case VDEV_AUX_ASHIFT_TOO_BIG:
1655 (void) printf(gettext("unsupported minimum blocksize"));
1658 case VDEV_AUX_SPARED:
1659 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1660 &spare_cb.cb_guid) == 0);
1661 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1662 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1663 zpool_get_name(zhp)) == 0)
1664 (void) printf(gettext("currently in "
1667 (void) printf(gettext("in use by "
1669 zpool_get_name(spare_cb.cb_zhp));
1670 zpool_close(spare_cb.cb_zhp);
1672 (void) printf(gettext("currently in use"));
1676 case VDEV_AUX_ERR_EXCEEDED:
1677 (void) printf(gettext("too many errors"));
1680 case VDEV_AUX_IO_FAILURE:
1681 (void) printf(gettext("experienced I/O failures"));
1684 case VDEV_AUX_BAD_LOG:
1685 (void) printf(gettext("bad intent log"));
1688 case VDEV_AUX_EXTERNAL:
1689 (void) printf(gettext("external device fault"));
1692 case VDEV_AUX_SPLIT_POOL:
1693 (void) printf(gettext("split into new pool"));
1696 case VDEV_AUX_ACTIVE:
1697 (void) printf(gettext("currently in use"));
1700 case VDEV_AUX_CHILDREN_OFFLINE:
1701 (void) printf(gettext("all children offline"));
1705 (void) printf(gettext("corrupted data"));
1708 } else if (children == 0 && !isspare &&
1709 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
1710 vs->vs_configured_ashift < vs->vs_physical_ashift) {
1712 gettext(" block size: %dB configured, %dB native"),
1713 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
1716 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1717 (uint64_t **)&ps, &c);
1719 if (ps != NULL && ps->pss_state == DSS_SCANNING &&
1720 vs->vs_scan_processed != 0 && children == 0) {
1721 (void) printf(gettext(" (%s)"),
1722 (ps->pss_func == POOL_SCAN_RESILVER) ?
1723 "resilvering" : "repairing");
1726 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
1727 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
1728 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
1729 !vs->vs_scan_removing) {
1732 struct tm zaction_ts;
1734 time_t t = vs->vs_initialize_action_time;
1735 int initialize_pct = 100;
1736 if (vs->vs_initialize_state != VDEV_INITIALIZE_COMPLETE) {
1737 initialize_pct = (vs->vs_initialize_bytes_done * 100 /
1738 (vs->vs_initialize_bytes_est + 1));
1741 (void) localtime_r(&t, &zaction_ts);
1742 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
1744 switch (vs->vs_initialize_state) {
1745 case VDEV_INITIALIZE_SUSPENDED:
1746 (void) snprintf(zbuf, sizeof (zbuf),
1747 ", suspended, started at %s", tbuf);
1749 case VDEV_INITIALIZE_ACTIVE:
1750 (void) snprintf(zbuf, sizeof (zbuf),
1751 ", started at %s", tbuf);
1753 case VDEV_INITIALIZE_COMPLETE:
1754 (void) snprintf(zbuf, sizeof (zbuf),
1755 ", completed at %s", tbuf);
1759 (void) printf(gettext(" (%d%% initialized%s)"),
1760 initialize_pct, zbuf);
1763 (void) printf("\n");
1765 for (c = 0; c < children; c++) {
1766 uint64_t islog = B_FALSE, ishole = B_FALSE;
1768 /* Don't print logs or holes here */
1769 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1771 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1773 if (islog || ishole)
1775 /* Only print normal classes here */
1776 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
1779 vname = zpool_vdev_name(g_zfs, zhp, child[c],
1780 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1781 print_status_config(zhp, cb, vname, child[c], depth + 2,
1788 * Print the configuration of an exported pool. Iterate over all vdevs in the
1789 * pool, printing out the name and status for each one.
1792 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
1800 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1801 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1802 strcmp(type, VDEV_TYPE_HOLE) == 0)
1805 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1806 (uint64_t **)&vs, &c) == 0);
1808 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
1809 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1811 if (vs->vs_aux != 0) {
1814 switch (vs->vs_aux) {
1815 case VDEV_AUX_OPEN_FAILED:
1816 (void) printf(gettext("cannot open"));
1819 case VDEV_AUX_BAD_GUID_SUM:
1820 (void) printf(gettext("missing device"));
1823 case VDEV_AUX_NO_REPLICAS:
1824 (void) printf(gettext("insufficient replicas"));
1827 case VDEV_AUX_VERSION_NEWER:
1828 (void) printf(gettext("newer version"));
1831 case VDEV_AUX_UNSUP_FEAT:
1832 (void) printf(gettext("unsupported feature(s)"));
1835 case VDEV_AUX_ERR_EXCEEDED:
1836 (void) printf(gettext("too many errors"));
1839 case VDEV_AUX_ACTIVE:
1840 (void) printf(gettext("currently in use"));
1843 case VDEV_AUX_CHILDREN_OFFLINE:
1844 (void) printf(gettext("all children offline"));
1848 (void) printf(gettext("corrupted data"));
1852 (void) printf("\n");
1854 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1855 &child, &children) != 0)
1858 for (c = 0; c < children; c++) {
1859 uint64_t is_log = B_FALSE;
1861 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1865 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
1868 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1869 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1870 print_import_config(cb, vname, child[c], depth + 2);
1874 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1875 &child, &children) == 0) {
1876 (void) printf(gettext("\tcache\n"));
1877 for (c = 0; c < children; c++) {
1878 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1880 (void) printf("\t %s\n", vname);
1885 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1886 &child, &children) == 0) {
1887 (void) printf(gettext("\tspares\n"));
1888 for (c = 0; c < children; c++) {
1889 vname = zpool_vdev_name(g_zfs, NULL, child[c],
1891 (void) printf("\t %s\n", vname);
1898 * Print specialized class vdevs.
1900 * These are recorded as top level vdevs in the main pool child array
1901 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
1902 * print_status_config() or print_import_config() to print the top level
1903 * class vdevs then any of their children (eg mirrored slogs) are printed
1904 * recursively - which works because only the top level vdev is marked.
1907 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
1912 boolean_t printed = B_FALSE;
1914 assert(zhp != NULL || !cb->cb_verbose);
1916 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1920 for (c = 0; c < children; c++) {
1921 uint64_t is_log = B_FALSE;
1925 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1929 bias = VDEV_ALLOC_CLASS_LOGS;
1931 (void) nvlist_lookup_string(child[c],
1932 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
1933 (void) nvlist_lookup_string(child[c],
1934 ZPOOL_CONFIG_TYPE, &type);
1937 if (bias == NULL || strcmp(bias, class) != 0)
1939 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
1943 (void) printf("\t%s\t\n", gettext(class));
1947 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
1948 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
1949 if (cb->cb_print_status)
1950 print_status_config(zhp, cb, name, child[c], 2,
1953 print_import_config(cb, name, child[c], 2);
1959 * Display the status for the given pool.
1962 show_import(nvlist_t *config)
1964 uint64_t pool_state;
1968 uint64_t hostid = 0;
1970 char *hostname = "unknown";
1971 nvlist_t *nvroot, *nvinfo;
1976 status_cbdata_t cb = { 0 };
1978 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1980 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1982 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1984 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1987 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1988 (uint64_t **)&vs, &vsc) == 0);
1989 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1991 reason = zpool_import_status(config, &msgid);
1993 (void) printf(gettext(" pool: %s\n"), name);
1994 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1995 (void) printf(gettext(" state: %s"), health);
1996 if (pool_state == POOL_STATE_DESTROYED)
1997 (void) printf(gettext(" (DESTROYED)"));
1998 (void) printf("\n");
2001 case ZPOOL_STATUS_MISSING_DEV_R:
2002 case ZPOOL_STATUS_MISSING_DEV_NR:
2003 case ZPOOL_STATUS_BAD_GUID_SUM:
2004 (void) printf(gettext(" status: One or more devices are "
2005 "missing from the system.\n"));
2008 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2009 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2010 (void) printf(gettext(" status: One or more devices contains "
2011 "corrupted data.\n"));
2014 case ZPOOL_STATUS_CORRUPT_DATA:
2016 gettext(" status: The pool data is corrupted.\n"));
2019 case ZPOOL_STATUS_OFFLINE_DEV:
2020 (void) printf(gettext(" status: One or more devices "
2021 "are offlined.\n"));
2024 case ZPOOL_STATUS_CORRUPT_POOL:
2025 (void) printf(gettext(" status: The pool metadata is "
2029 case ZPOOL_STATUS_VERSION_OLDER:
2030 (void) printf(gettext(" status: The pool is formatted using a "
2031 "legacy on-disk version.\n"));
2034 case ZPOOL_STATUS_VERSION_NEWER:
2035 (void) printf(gettext(" status: The pool is formatted using an "
2036 "incompatible version.\n"));
2039 case ZPOOL_STATUS_FEAT_DISABLED:
2040 (void) printf(gettext(" status: Some supported features are "
2041 "not enabled on the pool.\n"));
2044 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2045 (void) printf(gettext("status: The pool uses the following "
2046 "feature(s) not supported on this system:\n"));
2047 zpool_print_unsup_feat(config);
2050 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2051 (void) printf(gettext("status: The pool can only be accessed "
2052 "in read-only mode on this system. It\n\tcannot be "
2053 "accessed in read-write mode because it uses the "
2054 "following\n\tfeature(s) not supported on this system:\n"));
2055 zpool_print_unsup_feat(config);
2058 case ZPOOL_STATUS_HOSTID_ACTIVE:
2059 (void) printf(gettext(" status: The pool is currently "
2060 "imported by another system.\n"));
2063 case ZPOOL_STATUS_HOSTID_REQUIRED:
2064 (void) printf(gettext(" status: The pool has the "
2065 "multihost property on. It cannot\n\tbe safely imported "
2066 "when the system hostid is not set.\n"));
2069 case ZPOOL_STATUS_HOSTID_MISMATCH:
2070 (void) printf(gettext(" status: The pool was last accessed by "
2071 "another system.\n"));
2074 case ZPOOL_STATUS_FAULTED_DEV_R:
2075 case ZPOOL_STATUS_FAULTED_DEV_NR:
2076 (void) printf(gettext(" status: One or more devices are "
2080 case ZPOOL_STATUS_BAD_LOG:
2081 (void) printf(gettext(" status: An intent log record cannot be "
2085 case ZPOOL_STATUS_RESILVERING:
2086 (void) printf(gettext(" status: One or more devices were being "
2090 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
2091 (void) printf(gettext("status: One or more devices were "
2092 "configured to use a non-native block size.\n"
2093 "\tExpect reduced performance.\n"));
2098 * No other status can be seen when importing pools.
2100 assert(reason == ZPOOL_STATUS_OK);
2104 * Print out an action according to the overall state of the pool.
2106 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2107 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2108 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2109 (void) printf(gettext(" action: The pool can be "
2110 "imported using its name or numeric identifier, "
2111 "though\n\tsome features will not be available "
2112 "without an explicit 'zpool upgrade'.\n"));
2113 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2114 (void) printf(gettext(" action: The pool can be "
2115 "imported using its name or numeric "
2116 "identifier and\n\tthe '-f' flag.\n"));
2118 (void) printf(gettext(" action: The pool can be "
2119 "imported using its name or numeric "
2122 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2123 (void) printf(gettext(" action: The pool can be imported "
2124 "despite missing or damaged devices. The\n\tfault "
2125 "tolerance of the pool may be compromised if imported.\n"));
2128 case ZPOOL_STATUS_VERSION_NEWER:
2129 (void) printf(gettext(" action: The pool cannot be "
2130 "imported. Access the pool on a system running "
2131 "newer\n\tsoftware, or recreate the pool from "
2134 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2135 (void) printf(gettext("action: The pool cannot be "
2136 "imported. Access the pool on a system that "
2137 "supports\n\tthe required feature(s), or recreate "
2138 "the pool from backup.\n"));
2140 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2141 (void) printf(gettext("action: The pool cannot be "
2142 "imported in read-write mode. Import the pool "
2144 "\t\"-o readonly=on\", access the pool on a system "
2145 "that supports the\n\trequired feature(s), or "
2146 "recreate the pool from backup.\n"));
2148 case ZPOOL_STATUS_MISSING_DEV_R:
2149 case ZPOOL_STATUS_MISSING_DEV_NR:
2150 case ZPOOL_STATUS_BAD_GUID_SUM:
2151 (void) printf(gettext(" action: The pool cannot be "
2152 "imported. Attach the missing\n\tdevices and try "
2155 case ZPOOL_STATUS_HOSTID_ACTIVE:
2156 VERIFY0(nvlist_lookup_nvlist(config,
2157 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
2159 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2160 hostname = fnvlist_lookup_string(nvinfo,
2161 ZPOOL_CONFIG_MMP_HOSTNAME);
2163 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2164 hostid = fnvlist_lookup_uint64(nvinfo,
2165 ZPOOL_CONFIG_MMP_HOSTID);
2167 (void) printf(gettext(" action: The pool must be "
2168 "exported from %s (hostid=%lx)\n\tbefore it "
2169 "can be safely imported.\n"), hostname,
2170 (unsigned long) hostid);
2172 case ZPOOL_STATUS_HOSTID_REQUIRED:
2173 (void) printf(gettext(" action: Check the SMF "
2174 "svc:/system/hostid service.\n"));
2177 (void) printf(gettext(" action: The pool cannot be "
2178 "imported due to damaged devices or data.\n"));
2182 /* Print the comment attached to the pool. */
2183 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2184 (void) printf(gettext("comment: %s\n"), comment);
2187 * If the state is "closed" or "can't open", and the aux state
2188 * is "corrupt data":
2190 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
2191 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
2192 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
2193 if (pool_state == POOL_STATE_DESTROYED)
2194 (void) printf(gettext("\tThe pool was destroyed, "
2195 "but can be imported using the '-Df' flags.\n"));
2196 else if (pool_state != POOL_STATE_EXPORTED)
2197 (void) printf(gettext("\tThe pool may be active on "
2198 "another system, but can be imported using\n\t"
2199 "the '-f' flag.\n"));
2203 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
2206 (void) printf(gettext(" config:\n\n"));
2208 cb.cb_namewidth = max_width(NULL, nvroot, 0, 0, 0);
2209 if (cb.cb_namewidth < 10)
2210 cb.cb_namewidth = 10;
2212 print_import_config(&cb, name, nvroot, 0);
2214 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
2215 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
2216 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
2218 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
2219 (void) printf(gettext("\n\tAdditional devices are known to "
2220 "be part of this pool, though their\n\texact "
2221 "configuration cannot be determined.\n"));
2226 zfs_force_import_required(nvlist_t *config)
2229 uint64_t hostid = 0;
2232 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
2233 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
2235 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
2238 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2239 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
2240 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
2241 ZPOOL_CONFIG_MMP_STATE);
2243 if (mmp_state != MMP_STATE_INACTIVE)
2251 * Perform the import for the given configuration. This passes the heavy
2252 * lifting off to zpool_import_props(), and then mounts the datasets contained
2256 do_import(nvlist_t *config, const char *newname, const char *mntopts,
2257 nvlist_t *props, int flags)
2259 zpool_handle_t *zhp;
2263 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2264 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2266 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2267 (void) fprintf(stderr, gettext("cannot import '%s': pool "
2268 "is formatted using an unsupported ZFS version\n"), name);
2270 } else if (zfs_force_import_required(config) &&
2271 !(flags & ZFS_IMPORT_ANY_HOST)) {
2272 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
2275 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2276 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
2277 mmp_state = fnvlist_lookup_uint64(nvinfo,
2278 ZPOOL_CONFIG_MMP_STATE);
2280 if (mmp_state == MMP_STATE_ACTIVE) {
2281 char *hostname = "<unknown>";
2282 uint64_t hostid = 0;
2284 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
2285 hostname = fnvlist_lookup_string(nvinfo,
2286 ZPOOL_CONFIG_MMP_HOSTNAME);
2288 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
2289 hostid = fnvlist_lookup_uint64(nvinfo,
2290 ZPOOL_CONFIG_MMP_HOSTID);
2292 (void) fprintf(stderr, gettext("cannot import '%s': "
2293 "pool is imported on %s (hostid: "
2294 "0x%lx)\nExport the pool on the other system, "
2295 "then run 'zpool import'.\n"),
2296 name, hostname, (unsigned long) hostid);
2297 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2298 (void) fprintf(stderr, gettext("Cannot import '%s': "
2299 "pool has the multihost property on and the\n"
2300 "system's hostid is not set.\n"), name);
2302 char *hostname = "<unknown>";
2303 uint64_t timestamp = 0;
2304 uint64_t hostid = 0;
2306 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
2307 hostname = fnvlist_lookup_string(config,
2308 ZPOOL_CONFIG_HOSTNAME);
2310 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
2311 timestamp = fnvlist_lookup_uint64(config,
2312 ZPOOL_CONFIG_TIMESTAMP);
2314 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
2315 hostid = fnvlist_lookup_uint64(config,
2316 ZPOOL_CONFIG_HOSTID);
2318 (void) fprintf(stderr, gettext("cannot import '%s': "
2319 "pool was previously in use from another system.\n"
2320 "Last accessed by %s (hostid=%lx) at %s"
2321 "The pool can be imported, use 'zpool import -f' "
2322 "to import the pool.\n"), name, hostname,
2323 (unsigned long)hostid, ctime((time_t *)×tamp));
2330 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
2333 if (newname != NULL)
2334 name = (char *)newname;
2336 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2339 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2340 !(flags & ZFS_IMPORT_ONLY) &&
2341 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2351 * zpool checkpoint <pool>
2352 * checkpoint --discard <pool>
2354 * -d Discard the checkpoint from a checkpointed
2357 * Checkpoints the specified pool, by taking a "snapshot" of its
2358 * current state. A pool can only have one checkpoint at a time.
2361 zpool_do_checkpoint(int argc, char **argv)
2365 zpool_handle_t *zhp;
2368 struct option long_options[] = {
2369 {"discard", no_argument, NULL, 'd'},
2374 while ((c = getopt_long(argc, argv, ":d", long_options, NULL)) != -1) {
2380 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2390 (void) fprintf(stderr, gettext("missing pool argument\n"));
2395 (void) fprintf(stderr, gettext("too many arguments\n"));
2401 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
2402 /* As a special case, check for use of '/' in the name */
2403 if (strchr(pool, '/') != NULL)
2404 (void) fprintf(stderr, gettext("'zpool checkpoint' "
2405 "doesn't work on datasets. To save the state "
2406 "of a dataset from a specific point in time "
2407 "please use 'zfs snapshot'\n"));
2412 err = (zpool_discard_checkpoint(zhp) != 0);
2414 err = (zpool_checkpoint(zhp) != 0);
2421 #define CHECKPOINT_OPT 1024
2424 * zpool import [-d dir] [-D]
2425 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2426 * [-d dir | -c cachefile] [-f] -a
2427 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2428 * [-d dir | -c cachefile] [-f] [-n] [-F] [-t]
2429 * <pool | id> [newpool]
2431 * -c Read pool information from a cachefile instead of searching
2434 * -d Scan in a specific directory, other than /dev/dsk. More than
2435 * one directory can be specified using multiple '-d' options.
2437 * -D Scan for previously destroyed pools or import all or only
2438 * specified destroyed pools.
2440 * -R Temporarily import the pool, with all mountpoints relative to
2441 * the given root. The pool will remain exported when the machine
2444 * -V Import even in the presence of faulted vdevs. This is an
2445 * intentionally undocumented option for testing purposes, and
2446 * treats the pool configuration as complete, leaving any bad
2447 * vdevs in the FAULTED state. In other words, it does verbatim
2450 * -f Force import, even if it appears that the pool is active.
2452 * -F Attempt rewind if necessary.
2454 * -n See if rewind would work, but don't actually rewind.
2456 * -N Import the pool but don't mount datasets.
2458 * -t Use newpool as a temporary pool name instead of renaming
2461 * -T Specify a starting txg to use for import. This option is
2462 * intentionally undocumented option for testing purposes.
2464 * -a Import all pools found.
2466 * -o Set property=value and/or temporary mount options (without '=').
2468 * --rewind-to-checkpoint
2469 * Import the pool and revert back to the checkpoint.
2471 * The import command scans for pools to import, and import pools based on pool
2472 * name and GUID. The pool can also be renamed as part of the import process.
2475 zpool_do_import(int argc, char **argv)
2477 char **searchdirs = NULL;
2481 nvlist_t *pools = NULL;
2482 boolean_t do_all = B_FALSE;
2483 boolean_t do_destroyed = B_FALSE;
2484 char *mntopts = NULL;
2487 uint64_t searchguid = 0;
2488 char *searchname = NULL;
2490 nvlist_t *found_config;
2491 nvlist_t *policy = NULL;
2492 nvlist_t *props = NULL;
2494 int flags = ZFS_IMPORT_NORMAL;
2495 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2496 boolean_t dryrun = B_FALSE;
2497 boolean_t do_rewind = B_FALSE;
2498 boolean_t xtreme_rewind = B_FALSE;
2499 uint64_t pool_state, txg = -1ULL;
2500 char *cachefile = NULL;
2501 importargs_t idata = { 0 };
2505 struct option long_options[] = {
2506 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
2511 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFmnNo:rR:tT:VX",
2512 long_options, NULL)) != -1) {
2521 if (searchdirs == NULL) {
2522 searchdirs = safe_malloc(sizeof (char *));
2524 char **tmp = safe_malloc((nsearch + 1) *
2526 bcopy(searchdirs, tmp, nsearch *
2531 searchdirs[nsearch++] = optarg;
2534 do_destroyed = B_TRUE;
2537 flags |= ZFS_IMPORT_ANY_HOST;
2543 flags |= ZFS_IMPORT_MISSING_LOG;
2549 flags |= ZFS_IMPORT_ONLY;
2552 if ((propval = strchr(optarg, '=')) != NULL) {
2555 if (add_prop_list(optarg, propval,
2563 if (add_prop_list(zpool_prop_to_name(
2564 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2566 if (add_prop_list_default(zpool_prop_to_name(
2567 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2571 flags |= ZFS_IMPORT_TEMP_NAME;
2572 if (add_prop_list_default(zpool_prop_to_name(
2573 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2578 txg = strtoull(optarg, &endptr, 0);
2579 if (errno != 0 || *endptr != '\0') {
2580 (void) fprintf(stderr,
2581 gettext("invalid txg value\n"));
2584 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2587 flags |= ZFS_IMPORT_VERBATIM;
2590 xtreme_rewind = B_TRUE;
2592 case CHECKPOINT_OPT:
2593 flags |= ZFS_IMPORT_CHECKPOINT;
2596 (void) fprintf(stderr, gettext("missing argument for "
2597 "'%c' option\n"), optopt);
2601 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2610 if (cachefile && nsearch != 0) {
2611 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2615 if ((dryrun || xtreme_rewind) && !do_rewind) {
2616 (void) fprintf(stderr,
2617 gettext("-n or -X only meaningful with -F\n"));
2621 rewind_policy = ZPOOL_TRY_REWIND;
2623 rewind_policy = ZPOOL_DO_REWIND;
2625 rewind_policy |= ZPOOL_EXTREME_REWIND;
2627 /* In the future, we can capture further policy and include it here */
2628 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2629 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
2630 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
2631 rewind_policy) != 0)
2634 if (searchdirs == NULL) {
2635 searchdirs = safe_malloc(sizeof (char *));
2636 searchdirs[0] = "/dev";
2640 /* check argument count */
2643 (void) fprintf(stderr, gettext("too many arguments\n"));
2648 (void) fprintf(stderr, gettext("too many arguments\n"));
2653 * Check for the SYS_CONFIG privilege. We do this explicitly
2654 * here because otherwise any attempt to discover pools will
2657 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2658 (void) fprintf(stderr, gettext("cannot "
2659 "discover pools: permission denied\n"));
2661 nvlist_free(policy);
2667 * Depending on the arguments given, we do one of the following:
2669 * <none> Iterate through all pools and display information about
2672 * -a Iterate through all pools and try to import each one.
2674 * <id> Find the pool that corresponds to the given GUID/pool
2675 * name and import that one.
2677 * -D Above options applies only to destroyed pools.
2683 searchguid = strtoull(argv[0], &endptr, 10);
2684 if (errno != 0 || *endptr != '\0') {
2685 searchname = argv[0];
2688 found_config = NULL;
2691 * User specified a name or guid. Ensure it's unique.
2693 idata.unique = B_TRUE;
2697 idata.path = searchdirs;
2698 idata.paths = nsearch;
2699 idata.poolname = searchname;
2700 idata.guid = searchguid;
2701 idata.cachefile = cachefile;
2702 idata.policy = policy;
2704 pools = zpool_search_import(g_zfs, &idata);
2706 if (pools != NULL && idata.exists &&
2707 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2708 (void) fprintf(stderr, gettext("cannot import '%s': "
2709 "a pool with that name already exists\n"),
2711 (void) fprintf(stderr, gettext("use the form 'zpool import "
2712 "[-t] <pool | id> <newpool>' to give it a new temporary "
2713 "or permanent name\n"));
2715 } else if (pools == NULL && idata.exists) {
2716 (void) fprintf(stderr, gettext("cannot import '%s': "
2717 "a pool with that name is already created/imported,\n"),
2719 (void) fprintf(stderr, gettext("and no additional pools "
2720 "with that name were found\n"));
2722 } else if (pools == NULL) {
2724 (void) fprintf(stderr, gettext("cannot import '%s': "
2725 "no such pool available\n"), argv[0]);
2732 nvlist_free(policy);
2737 * At this point we have a list of import candidate configs. Even if
2738 * we were searching by pool name or guid, we still need to
2739 * post-process the list to deal with pool state and possible
2745 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2747 verify(nvpair_value_nvlist(elem, &config) == 0);
2749 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2751 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2753 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2756 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
2763 (void) printf("\n");
2766 err |= do_import(config, NULL, mntopts,
2769 show_import(config);
2771 } else if (searchname != NULL) {
2775 * We are searching for a pool based on name.
2777 verify(nvlist_lookup_string(config,
2778 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2780 if (strcmp(name, searchname) == 0) {
2781 if (found_config != NULL) {
2782 (void) fprintf(stderr, gettext(
2783 "cannot import '%s': more than "
2784 "one matching pool\n"), searchname);
2785 (void) fprintf(stderr, gettext(
2786 "import by numeric ID instead\n"));
2789 found_config = config;
2795 * Search for a pool by guid.
2797 verify(nvlist_lookup_uint64(config,
2798 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2800 if (guid == searchguid)
2801 found_config = config;
2806 * If we were searching for a specific pool, verify that we found a
2807 * pool, and then do the import.
2809 if (argc != 0 && err == 0) {
2810 if (found_config == NULL) {
2811 (void) fprintf(stderr, gettext("cannot import '%s': "
2812 "no such pool available\n"), argv[0]);
2815 err |= do_import(found_config, argc == 1 ? NULL :
2816 argv[1], mntopts, props, flags);
2821 * If we were just looking for pools, report an error if none were
2824 if (argc == 0 && first)
2825 (void) fprintf(stderr,
2826 gettext("no pools available to import\n"));
2831 nvlist_free(policy);
2834 return (err ? 1 : 0);
2838 * zpool sync [-f] [pool] ...
2840 * -f (undocumented) force uberblock (and config including zpool cache file)
2843 * Sync the specified pool(s).
2844 * Without arguments "zpool sync" will sync all pools.
2845 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2849 zpool_do_sync(int argc, char **argv)
2852 boolean_t force = B_FALSE;
2855 while ((ret = getopt(argc, argv, "f")) != -1) {
2861 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2870 /* if argc == 0 we will execute zpool_sync_one on all pools */
2871 ret = for_each_pool(argc, argv, B_FALSE, NULL, zpool_sync_one, &force);
2876 typedef struct iostat_cbdata {
2877 boolean_t cb_verbose;
2881 boolean_t cb_scripted;
2882 zpool_list_t *cb_list;
2886 print_iostat_separator(iostat_cbdata_t *cb)
2890 for (i = 0; i < cb->cb_namewidth; i++)
2892 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2896 print_iostat_header(iostat_cbdata_t *cb)
2898 (void) printf("%*s capacity operations bandwidth\n",
2899 cb->cb_namewidth, "");
2900 (void) printf("%-*s alloc free read write read write\n",
2901 cb->cb_namewidth, "pool");
2902 print_iostat_separator(cb);
2906 * Display a single statistic.
2909 print_one_stat(uint64_t value)
2913 zfs_nicenum(value, buf, sizeof (buf));
2914 (void) printf(" %5s", buf);
2917 static const char *class_name[] = {
2918 VDEV_ALLOC_BIAS_DEDUP,
2919 VDEV_ALLOC_BIAS_SPECIAL,
2920 VDEV_ALLOC_CLASS_LOGS
2924 * Print out all the statistics for the given vdev. This can either be the
2925 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2926 * is a verbose output, and we don't want to display the toplevel pool stats.
2928 * Returns the number of stat lines printed.
2931 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2932 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2934 nvlist_t **oldchild, **newchild;
2936 vdev_stat_t *oldvs, *newvs;
2937 vdev_stat_t zerovs = { 0 };
2943 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
2946 if (oldnv != NULL) {
2947 verify(nvlist_lookup_uint64_array(oldnv,
2948 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2953 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2954 (uint64_t **)&newvs, &c) == 0);
2956 if (strlen(name) + depth > cb->cb_namewidth)
2957 (void) printf("%*s%s", depth, "", name);
2959 (void) printf("%*s%s%*s", depth, "", name,
2960 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2962 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2967 scale = (double)NANOSEC / tdelta;
2969 /* only toplevel vdevs have capacity stats */
2970 if (newvs->vs_space == 0) {
2971 (void) printf(" - -");
2973 print_one_stat(newvs->vs_alloc);
2974 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2977 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2978 oldvs->vs_ops[ZIO_TYPE_READ])));
2980 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2981 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2983 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2984 oldvs->vs_bytes[ZIO_TYPE_READ])));
2986 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2987 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2989 (void) printf("\n");
2991 if (!cb->cb_verbose)
2994 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2995 &newchild, &children) != 0)
2998 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2999 &oldchild, &c) != 0)
3003 * print normal top-level devices
3005 for (c = 0; c < children; c++) {
3006 uint64_t ishole = B_FALSE, islog = B_FALSE;
3008 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
3011 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
3014 if (ishole || islog)
3017 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
3020 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3022 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3023 newchild[c], cb, depth + 2);
3028 * print all other top-level devices
3030 for (uint_t n = 0; n < 3; n++) {
3031 for (c = 0; c < children; c++) {
3032 uint64_t islog = B_FALSE;
3036 (void) nvlist_lookup_uint64(newchild[c],
3037 ZPOOL_CONFIG_IS_LOG, &islog);
3039 bias = VDEV_ALLOC_CLASS_LOGS;
3041 (void) nvlist_lookup_string(newchild[c],
3042 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
3043 (void) nvlist_lookup_string(newchild[c],
3044 ZPOOL_CONFIG_TYPE, &type);
3046 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
3048 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
3051 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3053 ret += print_vdev_stats(zhp, vname, oldnv ?
3054 oldchild[c] : NULL, newchild[c], cb, depth + 2);
3061 * Include level 2 ARC devices in iostat output
3063 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
3064 &newchild, &children) != 0)
3067 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
3068 &oldchild, &c) != 0)
3072 (void) printf("%-*s - - - - - "
3073 "-\n", cb->cb_namewidth, "cache");
3074 for (c = 0; c < children; c++) {
3075 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
3077 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
3078 newchild[c], cb, depth + 2);
3087 refresh_iostat(zpool_handle_t *zhp, void *data)
3089 iostat_cbdata_t *cb = data;
3093 * If the pool has disappeared, remove it from the list and continue.
3095 if (zpool_refresh_stats(zhp, &missing) != 0)
3099 pool_list_remove(cb->cb_list, zhp);
3105 * Callback to print out the iostats for the given pool.
3108 print_iostat(zpool_handle_t *zhp, void *data)
3110 iostat_cbdata_t *cb = data;
3111 nvlist_t *oldconfig, *newconfig;
3112 nvlist_t *oldnvroot, *newnvroot;
3114 newconfig = zpool_get_config(zhp, &oldconfig);
3116 if (cb->cb_iteration == 1)
3119 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
3122 if (oldconfig == NULL)
3125 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
3129 * Print out the statistics for the pool.
3131 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
3134 print_iostat_separator(cb);
3140 get_namewidth(zpool_handle_t *zhp, void *data)
3142 iostat_cbdata_t *cb = data;
3143 nvlist_t *config, *nvroot;
3145 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
3146 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3148 if (!cb->cb_verbose)
3149 cb->cb_namewidth = strlen(zpool_get_name(zhp));
3151 cb->cb_namewidth = max_width(zhp, nvroot, 0,
3152 cb->cb_namewidth, cb->cb_name_flags);
3156 * The width must fall into the range [10,38]. The upper limit is the
3157 * maximum we can have and still fit in 80 columns.
3159 if (cb->cb_namewidth < 10)
3160 cb->cb_namewidth = 10;
3161 if (cb->cb_namewidth > 38)
3162 cb->cb_namewidth = 38;
3168 * Parse the input string, get the 'interval' and 'count' value if there is one.
3171 get_interval_count(int *argcp, char **argv, unsigned long *iv,
3174 unsigned long interval = 0, count = 0;
3175 int argc = *argcp, errno;
3178 * Determine if the last argument is an integer or a pool name
3180 if (argc > 0 && isdigit(argv[argc - 1][0])) {
3184 interval = strtoul(argv[argc - 1], &end, 10);
3186 if (*end == '\0' && errno == 0) {
3187 if (interval == 0) {
3188 (void) fprintf(stderr, gettext("interval "
3189 "cannot be zero\n"));
3193 * Ignore the last parameter
3198 * If this is not a valid number, just plow on. The
3199 * user will get a more informative error message later
3207 * If the last argument is also an integer, then we have both a count
3210 if (argc > 0 && isdigit(argv[argc - 1][0])) {
3215 interval = strtoul(argv[argc - 1], &end, 10);
3217 if (*end == '\0' && errno == 0) {
3218 if (interval == 0) {
3219 (void) fprintf(stderr, gettext("interval "
3220 "cannot be zero\n"));
3225 * Ignore the last parameter
3239 get_timestamp_arg(char c)
3242 timestamp_fmt = UDATE;
3244 timestamp_fmt = DDATE;
3250 * zpool iostat [-gLPv] [-T d|u] [pool] ... [interval [count]]
3252 * -g Display guid for individual vdev name.
3253 * -L Follow links when resolving vdev path name.
3254 * -P Display full path for vdev name.
3255 * -v Display statistics for individual vdevs
3256 * -T Display a timestamp in date(1) or Unix format
3258 * This command can be tricky because we want to be able to deal with pool
3259 * creation/destruction as well as vdev configuration changes. The bulk of this
3260 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
3261 * on pool_list_update() to detect the addition of new pools. Configuration
3262 * changes are all handled within libzfs.
3265 zpool_do_iostat(int argc, char **argv)
3270 unsigned long interval = 0, count = 0;
3272 boolean_t verbose = B_FALSE;
3273 boolean_t guid = B_FALSE;
3274 boolean_t follow_links = B_FALSE;
3275 boolean_t full_name = B_FALSE;
3276 iostat_cbdata_t cb = { 0 };
3279 while ((c = getopt(argc, argv, "gLPT:v")) != -1) {
3285 follow_links = B_TRUE;
3291 get_timestamp_arg(*optarg);
3297 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3306 get_interval_count(&argc, argv, &interval, &count);
3309 * Construct the list of all interesting pools.
3312 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
3315 if (pool_list_count(list) == 0 && argc != 0) {
3316 pool_list_free(list);
3320 if (pool_list_count(list) == 0 && interval == 0) {
3321 pool_list_free(list);
3322 (void) fprintf(stderr, gettext("no pools available\n"));
3327 * Enter the main iostat loop.
3330 cb.cb_verbose = verbose;
3332 cb.cb_name_flags |= VDEV_NAME_GUID;
3334 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
3336 cb.cb_name_flags |= VDEV_NAME_PATH;
3337 cb.cb_iteration = 0;
3338 cb.cb_namewidth = 0;
3341 pool_list_update(list);
3343 if ((npools = pool_list_count(list)) == 0)
3347 * Refresh all statistics. This is done as an explicit step
3348 * before calculating the maximum name width, so that any
3349 * configuration changes are properly accounted for.
3351 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
3354 * Iterate over all pools to determine the maximum width
3355 * for the pool / device name column across all pools.
3357 cb.cb_namewidth = 0;
3358 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3360 if (timestamp_fmt != NODATE)
3361 print_timestamp(timestamp_fmt);
3364 * If it's the first time, or verbose mode, print the header.
3366 if (++cb.cb_iteration == 1 || verbose)
3367 print_iostat_header(&cb);
3369 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
3372 * If there's more than one pool, and we're not in verbose mode
3373 * (which prints a separator for us), then print a separator.
3375 if (npools > 1 && !verbose)
3376 print_iostat_separator(&cb);
3379 (void) printf("\n");
3382 * Flush the output so that redirection to a file isn't buffered
3385 (void) fflush(stdout);
3390 if (count != 0 && --count == 0)
3393 (void) sleep(interval);
3396 pool_list_free(list);
3401 typedef struct list_cbdata {
3402 boolean_t cb_verbose;
3405 boolean_t cb_scripted;
3406 zprop_list_t *cb_proplist;
3407 boolean_t cb_literal;
3412 * Given a list of columns to display, output appropriate headers for each one.
3415 print_header(list_cbdata_t *cb)
3417 zprop_list_t *pl = cb->cb_proplist;
3418 char headerbuf[ZPOOL_MAXPROPLEN];
3420 boolean_t first = B_TRUE;
3421 boolean_t right_justify;
3424 for (; pl != NULL; pl = pl->pl_next) {
3425 width = pl->pl_width;
3426 if (first && cb->cb_verbose) {
3428 * Reset the width to accommodate the verbose listing
3431 width = cb->cb_namewidth;
3439 right_justify = B_FALSE;
3440 if (pl->pl_prop != ZPROP_INVAL) {
3441 header = zpool_prop_column_name(pl->pl_prop);
3442 right_justify = zpool_prop_align_right(pl->pl_prop);
3446 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3447 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3448 headerbuf[i] = '\0';
3452 if (pl->pl_next == NULL && !right_justify)
3453 (void) printf("%s", header);
3454 else if (right_justify)
3455 (void) printf("%*s", width, header);
3457 (void) printf("%-*s", width, header);
3461 (void) printf("\n");
3465 * Given a pool and a list of properties, print out all the properties according
3466 * to the described layout. Used by zpool_do_list().
3469 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
3471 zprop_list_t *pl = cb->cb_proplist;
3472 boolean_t first = B_TRUE;
3473 char property[ZPOOL_MAXPROPLEN];
3475 boolean_t right_justify;
3478 for (; pl != NULL; pl = pl->pl_next) {
3480 width = pl->pl_width;
3481 if (first && cb->cb_verbose) {
3483 * Reset the width to accommodate the verbose listing
3486 width = cb->cb_namewidth;
3490 if (cb->cb_scripted)
3491 (void) printf("\t");
3498 right_justify = B_FALSE;
3499 if (pl->pl_prop != ZPROP_INVAL) {
3500 if (zpool_get_prop(zhp, pl->pl_prop, property,
3501 sizeof (property), NULL, cb->cb_literal) != 0)
3506 right_justify = zpool_prop_align_right(pl->pl_prop);
3507 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
3508 zpool_prop_unsupported(pl->pl_user_prop)) &&
3509 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3510 sizeof (property)) == 0) {
3518 * If this is being called in scripted mode, or if this is the
3519 * last column and it is left-justified, don't include a width
3522 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3523 (void) printf("%s", propstr);
3524 else if (right_justify)
3525 (void) printf("%*s", width, propstr);
3527 (void) printf("%-*s", width, propstr);
3530 (void) printf("\n");
3534 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3539 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3542 case ZPOOL_PROP_EXPANDSZ:
3543 case ZPOOL_PROP_CHECKPOINT:
3545 (void) strlcpy(propval, "-", sizeof (propval));
3547 zfs_nicenum(value, propval, sizeof (propval));
3549 case ZPOOL_PROP_FRAGMENTATION:
3550 if (value == ZFS_FRAG_INVALID) {
3551 (void) strlcpy(propval, "-", sizeof (propval));
3553 (void) snprintf(propval, sizeof (propval), "%llu%%",
3557 case ZPOOL_PROP_CAPACITY:
3558 (void) snprintf(propval, sizeof (propval),
3559 value < 1000 ? "%1.2f%%" : value < 10000 ?
3560 "%2.1f%%" : "%3.0f%%", value / 100.0);
3563 zfs_nicenum(value, propval, sizeof (propval));
3567 (void) strlcpy(propval, "-", sizeof (propval));
3570 (void) printf("\t%s", propval);
3572 (void) printf(" %*s", width, propval);
3576 * print static default line per vdev
3579 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3580 list_cbdata_t *cb, int depth)
3586 boolean_t scripted = cb->cb_scripted;
3587 uint64_t islog = B_FALSE;
3588 char *dashes = "%-*s - - - - - -\n";
3590 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3591 (uint64_t **)&vs, &c) == 0);
3594 boolean_t toplevel = (vs->vs_space != 0);
3597 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
3601 (void) printf("\t%s", name);
3602 else if (strlen(name) + depth > cb->cb_namewidth)
3603 (void) printf("%*s%s", depth, "", name);
3605 (void) printf("%*s%s%*s", depth, "", name,
3606 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3609 * Print the properties for the individual vdevs. Some
3610 * properties are only applicable to toplevel vdevs. The
3611 * 'toplevel' boolean value is passed to the print_one_column()
3612 * to indicate that the value is valid.
3614 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3616 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3618 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3619 scripted, toplevel);
3620 print_one_column(ZPOOL_PROP_CHECKPOINT,
3621 vs->vs_checkpoint_space, scripted, toplevel);
3622 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3624 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3625 vs->vs_fragmentation, scripted,
3626 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3627 cap = (vs->vs_space == 0) ? 0 :
3628 (vs->vs_alloc * 10000 / vs->vs_space);
3629 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3630 (void) printf("\n");
3633 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3634 &child, &children) != 0)
3637 /* list the normal vdevs first */
3638 for (c = 0; c < children; c++) {
3639 uint64_t ishole = B_FALSE;
3641 if (nvlist_lookup_uint64(child[c],
3642 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3645 if (nvlist_lookup_uint64(child[c],
3646 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
3649 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
3652 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3654 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3658 /* list the classes: 'logs', 'dedup', and 'special' */
3659 for (uint_t n = 0; n < 3; n++) {
3660 boolean_t printed = B_FALSE;
3662 for (c = 0; c < children; c++) {
3666 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3667 &islog) == 0 && islog) {
3668 bias = VDEV_ALLOC_CLASS_LOGS;
3670 (void) nvlist_lookup_string(child[c],
3671 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
3672 (void) nvlist_lookup_string(child[c],
3673 ZPOOL_CONFIG_TYPE, &type);
3675 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
3677 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
3681 /* LINTED E_SEC_PRINTF_VAR_FMT */
3682 (void) printf(dashes, cb->cb_namewidth,
3686 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3688 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3693 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3694 &child, &children) == 0 && children > 0) {
3695 /* LINTED E_SEC_PRINTF_VAR_FMT */
3696 (void) printf(dashes, cb->cb_namewidth, "cache");
3697 for (c = 0; c < children; c++) {
3698 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3700 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3705 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
3706 &children) == 0 && children > 0) {
3707 /* LINTED E_SEC_PRINTF_VAR_FMT */
3708 (void) printf(dashes, cb->cb_namewidth, "spare");
3709 for (c = 0; c < children; c++) {
3710 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3712 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3719 * Generic callback function to list a pool.
3722 list_callback(zpool_handle_t *zhp, void *data)
3724 list_cbdata_t *cbp = data;
3728 config = zpool_get_config(zhp, NULL);
3730 if (cbp->cb_verbose) {
3731 config = zpool_get_config(zhp, NULL);
3733 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3737 if (cbp->cb_verbose)
3738 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
3739 cbp->cb_name_flags);
3741 print_pool(zhp, cbp);
3743 if (cbp->cb_verbose)
3744 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3750 * zpool list [-gHLP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3752 * -g Display guid for individual vdev name.
3753 * -H Scripted mode. Don't display headers, and separate properties
3755 * -L Follow links when resolving vdev path name.
3756 * -o List of properties to display. Defaults to
3757 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3758 * "dedupratio,health,altroot"
3759 * -p Diplay values in parsable (exact) format.
3760 * -P Display full path for vdev name.
3761 * -T Display a timestamp in date(1) or Unix format
3763 * List all pools in the system, whether or not they're healthy. Output space
3764 * statistics for each one, as well as health status summary.
3767 zpool_do_list(int argc, char **argv)
3771 list_cbdata_t cb = { 0 };
3772 static char default_props[] =
3773 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
3774 "capacity,dedupratio,health,altroot";
3775 char *props = default_props;
3776 unsigned long interval = 0, count = 0;
3778 boolean_t first = B_TRUE;
3781 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
3784 cb.cb_name_flags |= VDEV_NAME_GUID;
3787 cb.cb_scripted = B_TRUE;
3790 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
3796 cb.cb_name_flags |= VDEV_NAME_PATH;
3799 cb.cb_literal = B_TRUE;
3802 get_timestamp_arg(*optarg);
3805 cb.cb_verbose = B_TRUE;
3806 cb.cb_namewidth = 8; /* 8 until precalc is avail */
3809 (void) fprintf(stderr, gettext("missing argument for "
3810 "'%c' option\n"), optopt);
3814 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3823 get_interval_count(&argc, argv, &interval, &count);
3825 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3829 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3833 if (pool_list_count(list) == 0)
3836 cb.cb_namewidth = 0;
3837 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3839 if (timestamp_fmt != NODATE)
3840 print_timestamp(timestamp_fmt);
3842 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3846 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3851 if (count != 0 && --count == 0)
3854 pool_list_free(list);
3855 (void) sleep(interval);
3858 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3859 (void) printf(gettext("no pools available\n"));
3863 pool_list_free(list);
3864 zprop_free_list(cb.cb_proplist);
3869 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3871 boolean_t force = B_FALSE;
3874 char *poolname, *old_disk, *new_disk;
3875 zpool_handle_t *zhp;
3876 zpool_boot_label_t boot_type;
3881 while ((c = getopt(argc, argv, "f")) != -1) {
3887 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3896 /* get pool name and check number of arguments */
3898 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3905 (void) fprintf(stderr,
3906 gettext("missing <device> specification\n"));
3914 (void) fprintf(stderr,
3915 gettext("missing <new_device> specification\n"));
3918 new_disk = old_disk;
3928 (void) fprintf(stderr, gettext("too many arguments\n"));
3932 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3935 if (zpool_get_config(zhp, NULL) == NULL) {
3936 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3942 if (zpool_is_bootable(zhp))
3943 boot_type = ZPOOL_COPY_BOOT_LABEL;
3945 boot_type = ZPOOL_NO_BOOT_LABEL;
3947 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3948 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3949 boot_type, boot_size, argc, argv);
3950 if (nvroot == NULL) {
3955 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3957 nvlist_free(nvroot);
3964 * zpool replace [-f] <pool> <device> <new_device>
3966 * -f Force attach, even if <new_device> appears to be in use.
3968 * Replace <device> with <new_device>.
3972 zpool_do_replace(int argc, char **argv)
3974 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3978 * zpool attach [-f] <pool> <device> <new_device>
3980 * -f Force attach, even if <new_device> appears to be in use.
3982 * Attach <new_device> to the mirror containing <device>. If <device> is not
3983 * part of a mirror, then <device> will be transformed into a mirror of
3984 * <device> and <new_device>. In either case, <new_device> will begin life
3985 * with a DTL of [0, now], and will immediately begin to resilver itself.
3988 zpool_do_attach(int argc, char **argv)
3990 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3994 * zpool detach [-f] <pool> <device>
3996 * -f Force detach of <device>, even if DTLs argue against it
3997 * (not supported yet)
3999 * Detach a device from a mirror. The operation will be refused if <device>
4000 * is the last device in the mirror, or if the DTLs indicate that this device
4001 * has the only valid copy of some data.
4005 zpool_do_detach(int argc, char **argv)
4008 char *poolname, *path;
4009 zpool_handle_t *zhp;
4013 while ((c = getopt(argc, argv, "f")) != -1) {
4017 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4026 /* get pool name and check number of arguments */
4028 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4033 (void) fprintf(stderr,
4034 gettext("missing <device> specification\n"));
4041 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4044 ret = zpool_vdev_detach(zhp, path);
4052 * zpool split [-gLnP] [-o prop=val] ...
4054 * [-R altroot] <pool> <newpool> [<device> ...]
4056 * -g Display guid for individual vdev name.
4057 * -L Follow links when resolving vdev path name.
4058 * -n Do not split the pool, but display the resulting layout if
4059 * it were to be split.
4060 * -o Set property=value, or set mount options.
4061 * -P Display full path for vdev name.
4062 * -R Mount the split-off pool under an alternate root.
4064 * Splits the named pool and gives it the new pool name. Devices to be split
4065 * off may be listed, provided that no more than one device is specified
4066 * per top-level vdev mirror. The newly split pool is left in an exported
4067 * state unless -R is specified.
4069 * Restrictions: the top-level of the pool pool must only be made up of
4070 * mirrors; all devices in the pool must be healthy; no device may be
4071 * undergoing a resilvering operation.
4074 zpool_do_split(int argc, char **argv)
4076 char *srcpool, *newpool, *propval;
4077 char *mntopts = NULL;
4080 zpool_handle_t *zhp;
4081 nvlist_t *config, *props = NULL;
4083 flags.dryrun = B_FALSE;
4084 flags.import = B_FALSE;
4085 flags.name_flags = 0;
4088 while ((c = getopt(argc, argv, ":gLR:no:P")) != -1) {
4091 flags.name_flags |= VDEV_NAME_GUID;
4094 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
4097 flags.import = B_TRUE;
4099 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
4100 &props, B_TRUE) != 0) {
4106 flags.dryrun = B_TRUE;
4109 if ((propval = strchr(optarg, '=')) != NULL) {
4112 if (add_prop_list(optarg, propval,
4113 &props, B_TRUE) != 0) {
4122 flags.name_flags |= VDEV_NAME_PATH;
4125 (void) fprintf(stderr, gettext("missing argument for "
4126 "'%c' option\n"), optopt);
4130 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4137 if (!flags.import && mntopts != NULL) {
4138 (void) fprintf(stderr, gettext("setting mntopts is only "
4139 "valid when importing the pool\n"));
4147 (void) fprintf(stderr, gettext("Missing pool name\n"));
4151 (void) fprintf(stderr, gettext("Missing new pool name\n"));
4161 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
4164 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
4165 if (config == NULL) {
4169 (void) printf(gettext("would create '%s' with the "
4170 "following layout:\n\n"), newpool);
4171 print_vdev_tree(NULL, newpool, config, 0, "",
4174 nvlist_free(config);
4179 if (ret != 0 || flags.dryrun || !flags.import)
4183 * The split was successful. Now we need to open the new
4184 * pool and import it.
4186 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
4188 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
4189 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
4191 (void) fprintf(stderr, gettext("Split was successful, but "
4192 "the datasets could not all be mounted\n"));
4193 (void) fprintf(stderr, gettext("Try doing '%s' with a "
4194 "different altroot\n"), "zpool import");
4204 * zpool online <pool> <device> ...
4207 zpool_do_online(int argc, char **argv)
4211 zpool_handle_t *zhp;
4213 vdev_state_t newstate;
4217 while ((c = getopt(argc, argv, "et")) != -1) {
4220 flags |= ZFS_ONLINE_EXPAND;
4224 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4233 /* get pool name and check number of arguments */
4235 (void) fprintf(stderr, gettext("missing pool name\n"));
4239 (void) fprintf(stderr, gettext("missing device name\n"));
4245 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4248 for (i = 1; i < argc; i++) {
4249 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
4250 if (newstate != VDEV_STATE_HEALTHY) {
4251 (void) printf(gettext("warning: device '%s' "
4252 "onlined, but remains in faulted state\n"),
4254 if (newstate == VDEV_STATE_FAULTED)
4255 (void) printf(gettext("use 'zpool "
4256 "clear' to restore a faulted "
4259 (void) printf(gettext("use 'zpool "
4260 "replace' to replace devices "
4261 "that are no longer present\n"));
4274 * zpool offline [-ft] <pool> <device> ...
4276 * -f Force the device into the offline state, even if doing
4277 * so would appear to compromise pool availability.
4278 * (not supported yet)
4280 * -t Only take the device off-line temporarily. The offline
4281 * state will not be persistent across reboots.
4285 zpool_do_offline(int argc, char **argv)
4289 zpool_handle_t *zhp;
4291 boolean_t istmp = B_FALSE;
4294 while ((c = getopt(argc, argv, "ft")) != -1) {
4301 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4310 /* get pool name and check number of arguments */
4312 (void) fprintf(stderr, gettext("missing pool name\n"));
4316 (void) fprintf(stderr, gettext("missing device name\n"));
4322 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4325 for (i = 1; i < argc; i++) {
4326 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
4336 * zpool clear <pool> [device]
4338 * Clear all errors associated with a pool or a particular device.
4341 zpool_do_clear(int argc, char **argv)
4345 boolean_t dryrun = B_FALSE;
4346 boolean_t do_rewind = B_FALSE;
4347 boolean_t xtreme_rewind = B_FALSE;
4348 uint32_t rewind_policy = ZPOOL_NO_REWIND;
4349 nvlist_t *policy = NULL;
4350 zpool_handle_t *zhp;
4351 char *pool, *device;
4354 while ((c = getopt(argc, argv, "FnX")) != -1) {
4363 xtreme_rewind = B_TRUE;
4366 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4376 (void) fprintf(stderr, gettext("missing pool name\n"));
4381 (void) fprintf(stderr, gettext("too many arguments\n"));
4385 if ((dryrun || xtreme_rewind) && !do_rewind) {
4386 (void) fprintf(stderr,
4387 gettext("-n or -X only meaningful with -F\n"));
4391 rewind_policy = ZPOOL_TRY_REWIND;
4393 rewind_policy = ZPOOL_DO_REWIND;
4395 rewind_policy |= ZPOOL_EXTREME_REWIND;
4397 /* In future, further rewind policy choices can be passed along here */
4398 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
4399 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
4400 rewind_policy) != 0) {
4405 device = argc == 2 ? argv[1] : NULL;
4407 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
4408 nvlist_free(policy);
4412 if (zpool_clear(zhp, device, policy) != 0)
4417 nvlist_free(policy);
4423 * zpool reguid <pool>
4426 zpool_do_reguid(int argc, char **argv)
4430 zpool_handle_t *zhp;
4434 while ((c = getopt(argc, argv, "")) != -1) {
4437 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4446 /* get pool name and check number of arguments */
4448 (void) fprintf(stderr, gettext("missing pool name\n"));
4453 (void) fprintf(stderr, gettext("too many arguments\n"));
4458 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4461 ret = zpool_reguid(zhp);
4469 * zpool reopen <pool>
4471 * Reopen the pool so that the kernel can update the sizes of all vdevs.
4474 zpool_do_reopen(int argc, char **argv)
4478 zpool_handle_t *zhp;
4482 while ((c = getopt(argc, argv, "")) != -1) {
4485 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4495 (void) fprintf(stderr, gettext("missing pool name\n"));
4500 (void) fprintf(stderr, gettext("too many arguments\n"));
4505 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
4508 ret = zpool_reopen(zhp);
4513 typedef struct scrub_cbdata {
4517 pool_scrub_cmd_t cb_scrub_cmd;
4521 zpool_has_checkpoint(zpool_handle_t *zhp)
4523 nvlist_t *config, *nvroot;
4525 config = zpool_get_config(zhp, NULL);
4527 if (config != NULL) {
4528 pool_checkpoint_stat_t *pcs = NULL;
4531 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
4532 (void) nvlist_lookup_uint64_array(nvroot,
4533 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
4535 if (pcs == NULL || pcs->pcs_state == CS_NONE)
4538 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
4539 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
4547 scrub_callback(zpool_handle_t *zhp, void *data)
4549 scrub_cbdata_t *cb = data;
4553 * Ignore faulted pools.
4555 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
4556 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
4557 "currently unavailable\n"), zpool_get_name(zhp));
4561 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
4563 if (err == 0 && zpool_has_checkpoint(zhp) &&
4564 cb->cb_type == POOL_SCAN_SCRUB) {
4565 (void) printf(gettext("warning: will not scrub state that "
4566 "belongs to the checkpoint of pool '%s'\n"),
4567 zpool_get_name(zhp));
4574 * zpool scrub [-s | -p] <pool> ...
4576 * -s Stop. Stops any in-progress scrub.
4577 * -p Pause. Pause in-progress scrub.
4580 zpool_do_scrub(int argc, char **argv)
4585 cb.cb_type = POOL_SCAN_SCRUB;
4586 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
4589 while ((c = getopt(argc, argv, "sp")) != -1) {
4592 cb.cb_type = POOL_SCAN_NONE;
4595 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
4598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4604 if (cb.cb_type == POOL_SCAN_NONE &&
4605 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
4606 (void) fprintf(stderr, gettext("invalid option combination: "
4607 "-s and -p are mutually exclusive\n"));
4617 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4621 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4625 zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
4627 uint_t children = 0;
4631 (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4634 if (children == 0) {
4635 char *path = zpool_vdev_name(g_zfs, zhp, nvroot, B_FALSE);
4636 fnvlist_add_boolean(res, path);
4641 for (i = 0; i < children; i++) {
4642 zpool_collect_leaves(zhp, child[i], res);
4647 * zpool initialize [-cs] <pool> [<vdev> ...]
4648 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
4649 * if none specified.
4651 * -c Cancel. Ends active initializing.
4652 * -s Suspend. Initializing can then be restarted with no flags.
4655 zpool_do_initialize(int argc, char **argv)
4659 zpool_handle_t *zhp;
4663 struct option long_options[] = {
4664 {"cancel", no_argument, NULL, 'c'},
4665 {"suspend", no_argument, NULL, 's'},
4669 pool_initialize_func_t cmd_type = POOL_INITIALIZE_DO;
4670 while ((c = getopt_long(argc, argv, "cs", long_options, NULL)) != -1) {
4673 if (cmd_type != POOL_INITIALIZE_DO) {
4674 (void) fprintf(stderr, gettext("-c cannot be "
4675 "combined with other options\n"));
4678 cmd_type = POOL_INITIALIZE_CANCEL;
4681 if (cmd_type != POOL_INITIALIZE_DO) {
4682 (void) fprintf(stderr, gettext("-s cannot be "
4683 "combined with other options\n"));
4686 cmd_type = POOL_INITIALIZE_SUSPEND;
4690 (void) fprintf(stderr,
4691 gettext("invalid option '%c'\n"), optopt);
4693 (void) fprintf(stderr,
4694 gettext("invalid option '%s'\n"),
4705 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4711 zhp = zpool_open(g_zfs, poolname);
4715 vdevs = fnvlist_alloc();
4717 /* no individual leaf vdevs specified, so add them all */
4718 nvlist_t *config = zpool_get_config(zhp, NULL);
4719 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
4720 ZPOOL_CONFIG_VDEV_TREE);
4721 zpool_collect_leaves(zhp, nvroot, vdevs);
4724 for (i = 1; i < argc; i++) {
4725 fnvlist_add_boolean(vdevs, argv[i]);
4729 err = zpool_initialize(zhp, cmd_type, vdevs);
4731 fnvlist_free(vdevs);
4738 * Print out detailed scrub status.
4741 print_scan_status(pool_scan_stat_t *ps)
4743 time_t start, end, pause;
4744 uint64_t total_secs_left;
4745 uint64_t elapsed, secs_left, mins_left, hours_left, days_left;
4746 uint64_t pass_scanned, scanned, pass_issued, issued, total;
4747 uint_t scan_rate, issue_rate;
4748 double fraction_done;
4749 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
4750 char srate_buf[7], irate_buf[7];
4752 (void) printf(gettext(" scan: "));
4754 /* If there's never been a scan, there's not much to say. */
4755 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
4756 ps->pss_func >= POOL_SCAN_FUNCS) {
4757 (void) printf(gettext("none requested\n"));
4761 start = ps->pss_start_time;
4762 end = ps->pss_end_time;
4763 pause = ps->pss_pass_scrub_pause;
4765 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4767 assert(ps->pss_func == POOL_SCAN_SCRUB ||
4768 ps->pss_func == POOL_SCAN_RESILVER);
4770 /* Scan is finished or canceled. */
4771 if (ps->pss_state == DSS_FINISHED) {
4772 total_secs_left = end - start;
4773 days_left = total_secs_left / 60 / 60 / 24;
4774 hours_left = (total_secs_left / 60 / 60) % 24;
4775 mins_left = (total_secs_left / 60) % 60;
4776 secs_left = (total_secs_left % 60);
4778 if (ps->pss_func == POOL_SCAN_SCRUB) {
4779 (void) printf(gettext("scrub repaired %s "
4780 "in %llu days %02llu:%02llu:%02llu "
4781 "with %llu errors on %s"), processed_buf,
4782 (u_longlong_t)days_left, (u_longlong_t)hours_left,
4783 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
4784 (u_longlong_t)ps->pss_errors, ctime(&end));
4785 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4786 (void) printf(gettext("resilvered %s "
4787 "in %llu days %02llu:%02llu:%02llu "
4788 "with %llu errors on %s"), processed_buf,
4789 (u_longlong_t)days_left, (u_longlong_t)hours_left,
4790 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
4791 (u_longlong_t)ps->pss_errors, ctime(&end));
4796 } else if (ps->pss_state == DSS_CANCELED) {
4797 if (ps->pss_func == POOL_SCAN_SCRUB) {
4798 (void) printf(gettext("scrub canceled on %s"),
4800 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4801 (void) printf(gettext("resilver canceled on %s"),
4807 assert(ps->pss_state == DSS_SCANNING);
4809 /* Scan is in progress. Resilvers can't be paused. */
4810 if (ps->pss_func == POOL_SCAN_SCRUB) {
4812 (void) printf(gettext("scrub in progress since %s"),
4815 (void) printf(gettext("scrub paused since %s"),
4817 (void) printf(gettext("\tscrub started on %s"),
4820 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4821 (void) printf(gettext("resilver in progress since %s"),
4825 scanned = ps->pss_examined;
4826 pass_scanned = ps->pss_pass_exam;
4827 issued = ps->pss_issued;
4828 pass_issued = ps->pss_pass_issued;
4829 total = ps->pss_to_examine;
4831 /* we are only done with a block once we have issued the IO for it */
4832 fraction_done = (double)issued / total;
4834 /* elapsed time for this pass, rounding up to 1 if it's 0 */
4835 elapsed = time(NULL) - ps->pss_pass_start;
4836 elapsed -= ps->pss_pass_scrub_spent_paused;
4837 elapsed = (elapsed != 0) ? elapsed : 1;
4839 scan_rate = pass_scanned / elapsed;
4840 issue_rate = pass_issued / elapsed;
4841 total_secs_left = (issue_rate != 0) ?
4842 ((total - issued) / issue_rate) : UINT64_MAX;
4844 days_left = total_secs_left / 60 / 60 / 24;
4845 hours_left = (total_secs_left / 60 / 60) % 24;
4846 mins_left = (total_secs_left / 60) % 60;
4847 secs_left = (total_secs_left % 60);
4849 /* format all of the numbers we will be reporting */
4850 zfs_nicenum(scanned, scanned_buf, sizeof (scanned_buf));
4851 zfs_nicenum(issued, issued_buf, sizeof (issued_buf));
4852 zfs_nicenum(total, total_buf, sizeof (total_buf));
4853 zfs_nicenum(scan_rate, srate_buf, sizeof (srate_buf));
4854 zfs_nicenum(issue_rate, irate_buf, sizeof (irate_buf));
4856 /* doo not print estimated time if we have a paused scrub */
4858 (void) printf(gettext("\t%s scanned at %s/s, "
4859 "%s issued at %s/s, %s total\n"),
4860 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
4862 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
4863 scanned_buf, issued_buf, total_buf);
4866 if (ps->pss_func == POOL_SCAN_RESILVER) {
4867 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
4868 processed_buf, 100 * fraction_done);
4869 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4870 (void) printf(gettext("\t%s repaired, %.2f%% done"),
4871 processed_buf, 100 * fraction_done);
4875 if (issue_rate >= 10 * 1024 * 1024) {
4876 (void) printf(gettext(", %llu days "
4877 "%02llu:%02llu:%02llu to go\n"),
4878 (u_longlong_t)days_left, (u_longlong_t)hours_left,
4879 (u_longlong_t)mins_left, (u_longlong_t)secs_left);
4881 (void) printf(gettext(", no estimated "
4882 "completion time\n"));
4885 (void) printf(gettext("\n"));
4890 * As we don't scrub checkpointed blocks, we want to warn the
4891 * user that we skipped scanning some blocks if a checkpoint exists
4892 * or existed at any time during the scan.
4895 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
4897 if (ps == NULL || pcs == NULL)
4900 if (pcs->pcs_state == CS_NONE ||
4901 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
4904 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
4906 if (ps->pss_state == DSS_NONE)
4909 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
4910 ps->pss_end_time < pcs->pcs_start_time)
4913 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
4914 (void) printf(gettext(" scan warning: skipped blocks "
4915 "that are only referenced by the checkpoint.\n"));
4917 assert(ps->pss_state == DSS_SCANNING);
4918 (void) printf(gettext(" scan warning: skipping blocks "
4919 "that are only referenced by the checkpoint.\n"));
4924 * Print out detailed removal status.
4927 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
4929 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4931 nvlist_t *config, *nvroot;
4936 if (prs == NULL || prs->prs_state == DSS_NONE)
4940 * Determine name of vdev.
4942 config = zpool_get_config(zhp, NULL);
4943 nvroot = fnvlist_lookup_nvlist(config,
4944 ZPOOL_CONFIG_VDEV_TREE);
4945 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4946 &child, &children) == 0);
4947 assert(prs->prs_removing_vdev < children);
4948 vdev_name = zpool_vdev_name(g_zfs, zhp,
4949 child[prs->prs_removing_vdev], B_TRUE);
4951 (void) printf(gettext("remove: "));
4953 start = prs->prs_start_time;
4954 end = prs->prs_end_time;
4955 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
4958 * Removal is finished or canceled.
4960 if (prs->prs_state == DSS_FINISHED) {
4961 uint64_t minutes_taken = (end - start) / 60;
4963 (void) printf(gettext("Removal of vdev %llu copied %s "
4964 "in %lluh%um, completed on %s"),
4965 (longlong_t)prs->prs_removing_vdev,
4967 (u_longlong_t)(minutes_taken / 60),
4968 (uint_t)(minutes_taken % 60),
4969 ctime((time_t *)&end));
4970 } else if (prs->prs_state == DSS_CANCELED) {
4971 (void) printf(gettext("Removal of %s canceled on %s"),
4972 vdev_name, ctime(&end));
4974 uint64_t copied, total, elapsed, mins_left, hours_left;
4975 double fraction_done;
4978 assert(prs->prs_state == DSS_SCANNING);
4981 * Removal is in progress.
4983 (void) printf(gettext(
4984 "Evacuation of %s in progress since %s"),
4985 vdev_name, ctime(&start));
4987 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
4988 total = prs->prs_to_copy;
4989 fraction_done = (double)copied / total;
4991 /* elapsed time for this pass */
4992 elapsed = time(NULL) - prs->prs_start_time;
4993 elapsed = elapsed > 0 ? elapsed : 1;
4994 rate = copied / elapsed;
4995 rate = rate > 0 ? rate : 1;
4996 mins_left = ((total - copied) / rate) / 60;
4997 hours_left = mins_left / 60;
4999 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
5000 zfs_nicenum(total, total_buf, sizeof (total_buf));
5001 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
5004 * do not print estimated time if hours_left is more than
5007 (void) printf(gettext(" %s copied out of %s at %s/s, "
5009 examined_buf, total_buf, rate_buf, 100 * fraction_done);
5010 if (hours_left < (30 * 24)) {
5011 (void) printf(gettext(", %lluh%um to go\n"),
5012 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
5014 (void) printf(gettext(
5015 ", (copy is slow, no estimated time)\n"));
5019 if (prs->prs_mapping_memory > 0) {
5021 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
5022 (void) printf(gettext(" %s memory used for "
5023 "removed device mappings\n"),
5029 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
5034 if (pcs == NULL || pcs->pcs_state == CS_NONE)
5037 (void) printf(gettext("checkpoint: "));
5039 start = pcs->pcs_start_time;
5040 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
5042 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
5043 char *date = ctime(&start);
5046 * ctime() adds a newline at the end of the generated
5047 * string, thus the weird format specifier and the
5048 * strlen() call used to chop it off from the output.
5050 (void) printf(gettext("created %.*s, consumes %s\n"),
5051 strlen(date) - 1, date, space_buf);
5055 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
5057 (void) printf(gettext("discarding, %s remaining.\n"),
5062 print_error_log(zpool_handle_t *zhp)
5064 nvlist_t *nverrlist = NULL;
5067 size_t len = MAXPATHLEN * 2;
5069 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
5070 (void) printf("errors: List of errors unavailable "
5071 "(insufficient privileges)\n");
5075 (void) printf("errors: Permanent errors have been "
5076 "detected in the following files:\n\n");
5078 pathname = safe_malloc(len);
5080 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
5082 uint64_t dsobj, obj;
5084 verify(nvpair_value_nvlist(elem, &nv) == 0);
5085 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
5087 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
5089 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
5090 (void) printf("%7s %s\n", "", pathname);
5093 nvlist_free(nverrlist);
5097 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
5106 (void) printf(gettext("\tspares\n"));
5108 for (i = 0; i < nspares; i++) {
5109 name = zpool_vdev_name(g_zfs, zhp, spares[i],
5111 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE);
5117 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
5126 (void) printf(gettext("\tcache\n"));
5128 for (i = 0; i < nl2cache; i++) {
5129 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
5131 print_status_config(zhp, cb, name, l2cache[i], 2, B_FALSE);
5137 print_dedup_stats(nvlist_t *config)
5139 ddt_histogram_t *ddh;
5145 * If the pool was faulted then we may not have been able to
5146 * obtain the config. Otherwise, if we have anything in the dedup
5147 * table continue processing the stats.
5149 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
5150 (uint64_t **)&ddo, &c) != 0)
5153 (void) printf("\n");
5154 (void) printf(gettext(" dedup: "));
5155 if (ddo->ddo_count == 0) {
5156 (void) printf(gettext("no DDT entries\n"));
5160 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
5161 (u_longlong_t)ddo->ddo_count,
5162 (u_longlong_t)ddo->ddo_dspace,
5163 (u_longlong_t)ddo->ddo_mspace);
5165 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
5166 (uint64_t **)&dds, &c) == 0);
5167 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
5168 (uint64_t **)&ddh, &c) == 0);
5169 zpool_dump_ddt(dds, ddh);
5173 * Display a summary of pool status. Displays a summary such as:
5177 * reason: One or more devices ...
5178 * see: http://illumos.org/msg/ZFS-xxxx-01
5184 * When given the '-v' option, we print out the complete config. If the '-e'
5185 * option is specified, then we print out error rate information as well.
5188 status_callback(zpool_handle_t *zhp, void *data)
5190 status_cbdata_t *cbp = data;
5191 nvlist_t *config, *nvroot;
5198 config = zpool_get_config(zhp, NULL);
5199 reason = zpool_get_status(zhp, &msgid);
5204 * If we were given 'zpool status -x', only report those pools with
5207 if (cbp->cb_explain &&
5208 (reason == ZPOOL_STATUS_OK ||
5209 reason == ZPOOL_STATUS_VERSION_OLDER ||
5210 reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT ||
5211 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
5212 if (!cbp->cb_allpools) {
5213 (void) printf(gettext("pool '%s' is healthy\n"),
5214 zpool_get_name(zhp));
5216 cbp->cb_first = B_FALSE;
5222 cbp->cb_first = B_FALSE;
5224 (void) printf("\n");
5226 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
5227 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
5228 (uint64_t **)&vs, &c) == 0);
5229 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
5231 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
5232 (void) printf(gettext(" state: %s\n"), health);
5235 case ZPOOL_STATUS_MISSING_DEV_R:
5236 (void) printf(gettext("status: One or more devices could not "
5237 "be opened. Sufficient replicas exist for\n\tthe pool to "
5238 "continue functioning in a degraded state.\n"));
5239 (void) printf(gettext("action: Attach the missing device and "
5240 "online it using 'zpool online'.\n"));
5243 case ZPOOL_STATUS_MISSING_DEV_NR:
5244 (void) printf(gettext("status: One or more devices could not "
5245 "be opened. There are insufficient\n\treplicas for the "
5246 "pool to continue functioning.\n"));
5247 (void) printf(gettext("action: Attach the missing device and "
5248 "online it using 'zpool online'.\n"));
5251 case ZPOOL_STATUS_CORRUPT_LABEL_R:
5252 (void) printf(gettext("status: One or more devices could not "
5253 "be used because the label is missing or\n\tinvalid. "
5254 "Sufficient replicas exist for the pool to continue\n\t"
5255 "functioning in a degraded state.\n"));
5256 (void) printf(gettext("action: Replace the device using "
5257 "'zpool replace'.\n"));
5260 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
5261 (void) printf(gettext("status: One or more devices could not "
5262 "be used because the label is missing \n\tor invalid. "
5263 "There are insufficient replicas for the pool to "
5264 "continue\n\tfunctioning.\n"));
5265 zpool_explain_recover(zpool_get_handle(zhp),
5266 zpool_get_name(zhp), reason, config);
5269 case ZPOOL_STATUS_FAILING_DEV:
5270 (void) printf(gettext("status: One or more devices has "
5271 "experienced an unrecoverable error. An\n\tattempt was "
5272 "made to correct the error. Applications are "
5274 (void) printf(gettext("action: Determine if the device needs "
5275 "to be replaced, and clear the errors\n\tusing "
5276 "'zpool clear' or replace the device with 'zpool "
5280 case ZPOOL_STATUS_OFFLINE_DEV:
5281 (void) printf(gettext("status: One or more devices has "
5282 "been taken offline by the administrator.\n\tSufficient "
5283 "replicas exist for the pool to continue functioning in "
5284 "a\n\tdegraded state.\n"));
5285 (void) printf(gettext("action: Online the device using "
5286 "'zpool online' or replace the device with\n\t'zpool "
5290 case ZPOOL_STATUS_REMOVED_DEV:
5291 (void) printf(gettext("status: One or more devices has "
5292 "been removed by the administrator.\n\tSufficient "
5293 "replicas exist for the pool to continue functioning in "
5294 "a\n\tdegraded state.\n"));
5295 (void) printf(gettext("action: Online the device using "
5296 "'zpool online' or replace the device with\n\t'zpool "
5300 case ZPOOL_STATUS_RESILVERING:
5301 (void) printf(gettext("status: One or more devices is "
5302 "currently being resilvered. The pool will\n\tcontinue "
5303 "to function, possibly in a degraded state.\n"));
5304 (void) printf(gettext("action: Wait for the resilver to "
5308 case ZPOOL_STATUS_CORRUPT_DATA:
5309 (void) printf(gettext("status: One or more devices has "
5310 "experienced an error resulting in data\n\tcorruption. "
5311 "Applications may be affected.\n"));
5312 (void) printf(gettext("action: Restore the file in question "
5313 "if possible. Otherwise restore the\n\tentire pool from "
5317 case ZPOOL_STATUS_CORRUPT_POOL:
5318 (void) printf(gettext("status: The pool metadata is corrupted "
5319 "and the pool cannot be opened.\n"));
5320 zpool_explain_recover(zpool_get_handle(zhp),
5321 zpool_get_name(zhp), reason, config);
5324 case ZPOOL_STATUS_VERSION_OLDER:
5325 (void) printf(gettext("status: The pool is formatted using a "
5326 "legacy on-disk format. The pool can\n\tstill be used, "
5327 "but some features are unavailable.\n"));
5328 (void) printf(gettext("action: Upgrade the pool using 'zpool "
5329 "upgrade'. Once this is done, the\n\tpool will no longer "
5330 "be accessible on software that does not support feature\n"
5334 case ZPOOL_STATUS_VERSION_NEWER:
5335 (void) printf(gettext("status: The pool has been upgraded to a "
5336 "newer, incompatible on-disk version.\n\tThe pool cannot "
5337 "be accessed on this system.\n"));
5338 (void) printf(gettext("action: Access the pool from a system "
5339 "running more recent software, or\n\trestore the pool from "
5343 case ZPOOL_STATUS_FEAT_DISABLED:
5344 (void) printf(gettext("status: Some supported features are not "
5345 "enabled on the pool. The pool can\n\tstill be used, but "
5346 "some features are unavailable.\n"));
5347 (void) printf(gettext("action: Enable all features using "
5348 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
5349 "longer be accessible by software that does not support\n\t"
5350 "the features. See zpool-features(7) for details.\n"));
5353 case ZPOOL_STATUS_UNSUP_FEAT_READ:
5354 (void) printf(gettext("status: The pool cannot be accessed on "
5355 "this system because it uses the\n\tfollowing feature(s) "
5356 "not supported on this system:\n"));
5357 zpool_print_unsup_feat(config);
5358 (void) printf("\n");
5359 (void) printf(gettext("action: Access the pool from a system "
5360 "that supports the required feature(s),\n\tor restore the "
5361 "pool from backup.\n"));
5364 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
5365 (void) printf(gettext("status: The pool can only be accessed "
5366 "in read-only mode on this system. It\n\tcannot be "
5367 "accessed in read-write mode because it uses the "
5368 "following\n\tfeature(s) not supported on this system:\n"));
5369 zpool_print_unsup_feat(config);
5370 (void) printf("\n");
5371 (void) printf(gettext("action: The pool cannot be accessed in "
5372 "read-write mode. Import the pool with\n"
5373 "\t\"-o readonly=on\", access the pool from a system that "
5374 "supports the\n\trequired feature(s), or restore the "
5375 "pool from backup.\n"));
5378 case ZPOOL_STATUS_FAULTED_DEV_R:
5379 (void) printf(gettext("status: One or more devices are "
5380 "faulted in response to persistent errors.\n\tSufficient "
5381 "replicas exist for the pool to continue functioning "
5382 "in a\n\tdegraded state.\n"));
5383 (void) printf(gettext("action: Replace the faulted device, "
5384 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
5387 case ZPOOL_STATUS_FAULTED_DEV_NR:
5388 (void) printf(gettext("status: One or more devices are "
5389 "faulted in response to persistent errors. There are "
5390 "insufficient replicas for the pool to\n\tcontinue "
5392 (void) printf(gettext("action: Destroy and re-create the pool "
5393 "from a backup source. Manually marking the device\n"
5394 "\trepaired using 'zpool clear' may allow some data "
5395 "to be recovered.\n"));
5398 case ZPOOL_STATUS_IO_FAILURE_MMP:
5399 (void) printf(gettext("status: The pool is suspended because "
5400 "multihost writes failed or were delayed;\n\tanother "
5401 "system could import the pool undetected.\n"));
5402 (void) printf(gettext("action: Make sure the pool's devices "
5403 "are connected, then reboot your system and\n\timport the "
5407 case ZPOOL_STATUS_IO_FAILURE_WAIT:
5408 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
5409 (void) printf(gettext("status: One or more devices are "
5410 "faulted in response to IO failures.\n"));
5411 (void) printf(gettext("action: Make sure the affected devices "
5412 "are connected, then run 'zpool clear'.\n"));
5415 case ZPOOL_STATUS_BAD_LOG:
5416 (void) printf(gettext("status: An intent log record "
5417 "could not be read.\n"
5418 "\tWaiting for adminstrator intervention to fix the "
5419 "faulted pool.\n"));
5420 (void) printf(gettext("action: Either restore the affected "
5421 "device(s) and run 'zpool online',\n"
5422 "\tor ignore the intent log records by running "
5423 "'zpool clear'.\n"));
5426 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
5427 (void) printf(gettext("status: One or more devices are "
5428 "configured to use a non-native block size.\n"
5429 "\tExpect reduced performance.\n"));
5430 (void) printf(gettext("action: Replace affected devices with "
5431 "devices that support the\n\tconfigured block size, or "
5432 "migrate data to a properly configured\n\tpool.\n"));
5437 * The remaining errors can't actually be generated, yet.
5439 assert(reason == ZPOOL_STATUS_OK);
5443 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
5446 if (config != NULL) {
5448 nvlist_t **spares, **l2cache;
5449 uint_t nspares, nl2cache;
5450 pool_checkpoint_stat_t *pcs = NULL;
5451 pool_scan_stat_t *ps = NULL;
5452 pool_removal_stat_t *prs = NULL;
5454 (void) nvlist_lookup_uint64_array(nvroot,
5455 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
5456 (void) nvlist_lookup_uint64_array(nvroot,
5457 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
5458 (void) nvlist_lookup_uint64_array(nvroot,
5459 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
5461 print_scan_status(ps);
5462 print_checkpoint_scan_warning(ps, pcs);
5463 print_removal_status(zhp, prs);
5464 print_checkpoint_status(pcs);
5466 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
5467 cbp->cb_name_flags);
5468 if (cbp->cb_namewidth < 10)
5469 cbp->cb_namewidth = 10;
5471 (void) printf(gettext("config:\n\n"));
5472 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"),
5473 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
5476 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
5479 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
5480 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
5481 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
5483 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
5484 &l2cache, &nl2cache) == 0)
5485 print_l2cache(zhp, cbp, l2cache, nl2cache);
5487 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
5488 &spares, &nspares) == 0)
5489 print_spares(zhp, cbp, spares, nspares);
5491 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
5493 nvlist_t *nverrlist = NULL;
5496 * If the approximate error count is small, get a
5497 * precise count by fetching the entire log and
5498 * uniquifying the results.
5500 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
5501 zpool_get_errlog(zhp, &nverrlist) == 0) {
5506 while ((elem = nvlist_next_nvpair(nverrlist,
5511 nvlist_free(nverrlist);
5513 (void) printf("\n");
5516 (void) printf(gettext("errors: No known data "
5518 else if (!cbp->cb_verbose)
5519 (void) printf(gettext("errors: %llu data "
5520 "errors, use '-v' for a list\n"),
5521 (u_longlong_t)nerr);
5523 print_error_log(zhp);
5526 if (cbp->cb_dedup_stats)
5527 print_dedup_stats(config);
5529 (void) printf(gettext("config: The configuration cannot be "
5537 * zpool status [-gLPvx] [-T d|u] [pool] ... [interval [count]]
5539 * -g Display guid for individual vdev name.
5540 * -L Follow links when resolving vdev path name.
5541 * -P Display full path for vdev name.
5542 * -v Display complete error logs
5543 * -x Display only pools with potential problems
5544 * -D Display dedup status (undocumented)
5545 * -T Display a timestamp in date(1) or Unix format
5547 * Describes the health status of all pools or some subset.
5550 zpool_do_status(int argc, char **argv)
5554 unsigned long interval = 0, count = 0;
5555 status_cbdata_t cb = { 0 };
5558 while ((c = getopt(argc, argv, "gLPvxDT:")) != -1) {
5561 cb.cb_name_flags |= VDEV_NAME_GUID;
5564 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5567 cb.cb_name_flags |= VDEV_NAME_PATH;
5570 cb.cb_verbose = B_TRUE;
5573 cb.cb_explain = B_TRUE;
5576 cb.cb_dedup_stats = B_TRUE;
5579 get_timestamp_arg(*optarg);
5582 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5591 get_interval_count(&argc, argv, &interval, &count);
5594 cb.cb_allpools = B_TRUE;
5596 cb.cb_first = B_TRUE;
5597 cb.cb_print_status = B_TRUE;
5600 if (timestamp_fmt != NODATE)
5601 print_timestamp(timestamp_fmt);
5603 ret = for_each_pool(argc, argv, B_TRUE, NULL,
5604 status_callback, &cb);
5606 if (argc == 0 && cb.cb_count == 0)
5607 (void) printf(gettext("no pools available\n"));
5608 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
5609 (void) printf(gettext("all pools are healthy\n"));
5617 if (count != 0 && --count == 0)
5620 (void) sleep(interval);
5626 typedef struct upgrade_cbdata {
5628 boolean_t cb_unavail;
5629 char cb_poolname[ZFS_MAX_DATASET_NAME_LEN];
5631 uint64_t cb_version;
5637 is_root_pool(zpool_handle_t *zhp)
5639 static struct statfs sfs;
5640 static char *poolname = NULL;
5641 static boolean_t stated = B_FALSE;
5646 if (statfs("/", &sfs) == -1) {
5647 (void) fprintf(stderr,
5648 "Unable to stat root file system: %s.\n",
5652 if (strcmp(sfs.f_fstypename, "zfs") != 0)
5654 poolname = sfs.f_mntfromname;
5655 if ((slash = strchr(poolname, '/')) != NULL)
5658 return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0);
5662 root_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size)
5665 if (poolname[0] == '\0' && is_root_pool(zhp))
5666 (void) strlcpy(poolname, zpool_get_name(zhp), size);
5668 #endif /* FreeBSD */
5671 upgrade_version(zpool_handle_t *zhp, uint64_t version)
5675 uint64_t oldversion;
5677 config = zpool_get_config(zhp, NULL);
5678 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5681 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
5682 assert(oldversion < version);
5684 ret = zpool_upgrade(zhp, version);
5688 if (version >= SPA_VERSION_FEATURES) {
5689 (void) printf(gettext("Successfully upgraded "
5690 "'%s' from version %llu to feature flags.\n"),
5691 zpool_get_name(zhp), oldversion);
5693 (void) printf(gettext("Successfully upgraded "
5694 "'%s' from version %llu to version %llu.\n"),
5695 zpool_get_name(zhp), oldversion, version);
5702 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
5705 boolean_t firstff = B_TRUE;
5706 nvlist_t *enabled = zpool_get_features(zhp);
5709 for (i = 0; i < SPA_FEATURES; i++) {
5710 const char *fname = spa_feature_table[i].fi_uname;
5711 const char *fguid = spa_feature_table[i].fi_guid;
5712 if (!nvlist_exists(enabled, fguid)) {
5714 verify(-1 != asprintf(&propname, "feature@%s", fname));
5715 ret = zpool_set_prop(zhp, propname,
5716 ZFS_FEATURE_ENABLED);
5724 (void) printf(gettext("Enabled the "
5725 "following features on '%s':\n"),
5726 zpool_get_name(zhp));
5729 (void) printf(gettext(" %s\n"), fname);
5740 upgrade_cb(zpool_handle_t *zhp, void *arg)
5742 upgrade_cbdata_t *cbp = arg;
5745 boolean_t printnl = B_FALSE;
5748 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5749 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5750 "currently unavailable.\n\n"), zpool_get_name(zhp));
5751 cbp->cb_unavail = B_TRUE;
5752 /* Allow iteration to continue. */
5756 config = zpool_get_config(zhp, NULL);
5757 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5760 assert(SPA_VERSION_IS_SUPPORTED(version));
5762 if (version < cbp->cb_version) {
5763 cbp->cb_first = B_FALSE;
5764 ret = upgrade_version(zhp, cbp->cb_version);
5768 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5769 sizeof(cbp->cb_poolname));
5770 #endif /* __FreeBSD__ */
5775 * If they did "zpool upgrade -a", then we could
5776 * be doing ioctls to different pools. We need
5777 * to log this history once to each pool, and bypass
5778 * the normal history logging that happens in main().
5780 (void) zpool_log_history(g_zfs, history_str);
5781 log_history = B_FALSE;
5785 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5787 ret = upgrade_enable_all(zhp, &count);
5792 cbp->cb_first = B_FALSE;
5795 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5796 sizeof(cbp->cb_poolname));
5797 #endif /* __FreeBSD__ */
5799 * If they did "zpool upgrade -a", then we could
5800 * be doing ioctls to different pools. We need
5801 * to log this history once to each pool, and bypass
5802 * the normal history logging that happens in main().
5804 (void) zpool_log_history(g_zfs, history_str);
5805 log_history = B_FALSE;
5810 (void) printf(gettext("\n"));
5817 upgrade_list_unavail(zpool_handle_t *zhp, void *arg)
5819 upgrade_cbdata_t *cbp = arg;
5821 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5822 if (cbp->cb_first) {
5823 (void) fprintf(stderr, gettext("The following pools "
5824 "are unavailable and cannot be upgraded as this "
5826 (void) fprintf(stderr, gettext("POOL\n"));
5827 (void) fprintf(stderr, gettext("------------\n"));
5828 cbp->cb_first = B_FALSE;
5830 (void) printf(gettext("%s\n"), zpool_get_name(zhp));
5831 cbp->cb_unavail = B_TRUE;
5837 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
5839 upgrade_cbdata_t *cbp = arg;
5843 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5845 * This will have been reported by upgrade_list_unavail so
5846 * just allow iteration to continue.
5848 cbp->cb_unavail = B_TRUE;
5852 config = zpool_get_config(zhp, NULL);
5853 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5856 assert(SPA_VERSION_IS_SUPPORTED(version));
5858 if (version < SPA_VERSION_FEATURES) {
5859 if (cbp->cb_first) {
5860 (void) printf(gettext("The following pools are "
5861 "formatted with legacy version numbers and can\n"
5862 "be upgraded to use feature flags. After "
5863 "being upgraded, these pools\nwill no "
5864 "longer be accessible by software that does not "
5865 "support feature\nflags.\n\n"));
5866 (void) printf(gettext("VER POOL\n"));
5867 (void) printf(gettext("--- ------------\n"));
5868 cbp->cb_first = B_FALSE;
5871 (void) printf("%2llu %s\n", (u_longlong_t)version,
5872 zpool_get_name(zhp));
5879 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
5881 upgrade_cbdata_t *cbp = arg;
5885 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5887 * This will have been reported by upgrade_list_unavail so
5888 * just allow iteration to continue.
5890 cbp->cb_unavail = B_TRUE;
5894 config = zpool_get_config(zhp, NULL);
5895 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
5898 if (version >= SPA_VERSION_FEATURES) {
5900 boolean_t poolfirst = B_TRUE;
5901 nvlist_t *enabled = zpool_get_features(zhp);
5903 for (i = 0; i < SPA_FEATURES; i++) {
5904 const char *fguid = spa_feature_table[i].fi_guid;
5905 const char *fname = spa_feature_table[i].fi_uname;
5906 if (!nvlist_exists(enabled, fguid)) {
5907 if (cbp->cb_first) {
5908 (void) printf(gettext("\nSome "
5909 "supported features are not "
5910 "enabled on the following pools. "
5911 "Once a\nfeature is enabled the "
5912 "pool may become incompatible with "
5913 "software\nthat does not support "
5915 "zpool-features(7) for "
5917 (void) printf(gettext("POOL "
5919 (void) printf(gettext("------"
5921 cbp->cb_first = B_FALSE;
5925 (void) printf(gettext("%s\n"),
5926 zpool_get_name(zhp));
5927 poolfirst = B_FALSE;
5930 (void) printf(gettext(" %s\n"), fname);
5940 upgrade_one(zpool_handle_t *zhp, void *data)
5942 boolean_t printnl = B_FALSE;
5943 upgrade_cbdata_t *cbp = data;
5944 uint64_t cur_version;
5947 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5948 (void) fprintf(stderr, gettext("cannot upgrade '%s': pool is "
5949 "is currently unavailable.\n\n"), zpool_get_name(zhp));
5950 cbp->cb_unavail = B_TRUE;
5954 if (strcmp("log", zpool_get_name(zhp)) == 0) {
5955 (void) printf(gettext("'log' is now a reserved word\n"
5956 "Pool 'log' must be renamed using export and import"
5957 " to upgrade.\n\n"));
5961 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5962 if (cur_version > cbp->cb_version) {
5963 (void) printf(gettext("Pool '%s' is already formatted "
5964 "using more current version '%llu'.\n\n"),
5965 zpool_get_name(zhp), cur_version);
5969 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
5970 (void) printf(gettext("Pool '%s' is already formatted "
5971 "using version %llu.\n\n"), zpool_get_name(zhp),
5976 if (cur_version != cbp->cb_version) {
5978 ret = upgrade_version(zhp, cbp->cb_version);
5982 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5983 sizeof(cbp->cb_poolname));
5984 #endif /* __FreeBSD__ */
5987 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
5989 ret = upgrade_enable_all(zhp, &count);
5996 root_pool_upgrade_check(zhp, cbp->cb_poolname,
5997 sizeof(cbp->cb_poolname));
5998 #endif /* __FreeBSD __*/
5999 } else if (cur_version == SPA_VERSION) {
6000 (void) printf(gettext("Pool '%s' already has all "
6001 "supported features enabled.\n\n"),
6002 zpool_get_name(zhp));
6007 (void) printf(gettext("\n"));
6016 * zpool upgrade [-V version] <-a | pool ...>
6018 * With no arguments, display downrev'd ZFS pool available for upgrade.
6019 * Individual pools can be upgraded by specifying the pool, and '-a' will
6020 * upgrade all pools.
6023 zpool_do_upgrade(int argc, char **argv)
6026 upgrade_cbdata_t cb = { 0 };
6028 boolean_t showversions = B_FALSE;
6029 boolean_t upgradeall = B_FALSE;
6034 while ((c = getopt(argc, argv, ":avV:")) != -1) {
6037 upgradeall = B_TRUE;
6040 showversions = B_TRUE;
6043 cb.cb_version = strtoll(optarg, &end, 10);
6045 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
6046 (void) fprintf(stderr,
6047 gettext("invalid version '%s'\n"), optarg);
6052 (void) fprintf(stderr, gettext("missing argument for "
6053 "'%c' option\n"), optopt);
6057 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6068 if (cb.cb_version == 0) {
6069 cb.cb_version = SPA_VERSION;
6070 } else if (!upgradeall && argc == 0) {
6071 (void) fprintf(stderr, gettext("-V option is "
6072 "incompatible with other arguments\n"));
6077 if (upgradeall || argc != 0) {
6078 (void) fprintf(stderr, gettext("-v option is "
6079 "incompatible with other arguments\n"));
6082 } else if (upgradeall) {
6084 (void) fprintf(stderr, gettext("-a option should not "
6085 "be used along with a pool name\n"));
6090 (void) printf(gettext("This system supports ZFS pool feature "
6095 (void) printf(gettext("The following features are "
6097 (void) printf(gettext("FEAT DESCRIPTION\n"));
6098 (void) printf("----------------------------------------------"
6099 "---------------\n");
6100 for (i = 0; i < SPA_FEATURES; i++) {
6101 zfeature_info_t *fi = &spa_feature_table[i];
6103 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
6104 " (read-only compatible)" : "";
6106 (void) printf("%-37s%s\n", fi->fi_uname, ro);
6107 (void) printf(" %s\n", fi->fi_desc);
6109 (void) printf("\n");
6111 (void) printf(gettext("The following legacy versions are also "
6113 (void) printf(gettext("VER DESCRIPTION\n"));
6114 (void) printf("--- -----------------------------------------"
6115 "---------------\n");
6116 (void) printf(gettext(" 1 Initial ZFS version\n"));
6117 (void) printf(gettext(" 2 Ditto blocks "
6118 "(replicated metadata)\n"));
6119 (void) printf(gettext(" 3 Hot spares and double parity "
6121 (void) printf(gettext(" 4 zpool history\n"));
6122 (void) printf(gettext(" 5 Compression using the gzip "
6124 (void) printf(gettext(" 6 bootfs pool property\n"));
6125 (void) printf(gettext(" 7 Separate intent log devices\n"));
6126 (void) printf(gettext(" 8 Delegated administration\n"));
6127 (void) printf(gettext(" 9 refquota and refreservation "
6129 (void) printf(gettext(" 10 Cache devices\n"));
6130 (void) printf(gettext(" 11 Improved scrub performance\n"));
6131 (void) printf(gettext(" 12 Snapshot properties\n"));
6132 (void) printf(gettext(" 13 snapused property\n"));
6133 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
6134 (void) printf(gettext(" 15 user/group space accounting\n"));
6135 (void) printf(gettext(" 16 stmf property support\n"));
6136 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
6137 (void) printf(gettext(" 18 Snapshot user holds\n"));
6138 (void) printf(gettext(" 19 Log device removal\n"));
6139 (void) printf(gettext(" 20 Compression using zle "
6140 "(zero-length encoding)\n"));
6141 (void) printf(gettext(" 21 Deduplication\n"));
6142 (void) printf(gettext(" 22 Received properties\n"));
6143 (void) printf(gettext(" 23 Slim ZIL\n"));
6144 (void) printf(gettext(" 24 System attributes\n"));
6145 (void) printf(gettext(" 25 Improved scrub stats\n"));
6146 (void) printf(gettext(" 26 Improved snapshot deletion "
6148 (void) printf(gettext(" 27 Improved snapshot creation "
6150 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
6151 (void) printf(gettext("\nFor more information on a particular "
6152 "version, including supported releases,\n"));
6153 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
6154 } else if (argc == 0 && upgradeall) {
6155 cb.cb_first = B_TRUE;
6156 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
6157 if (ret == 0 && cb.cb_first) {
6158 if (cb.cb_version == SPA_VERSION) {
6159 (void) printf(gettext("All %spools are already "
6160 "formatted using feature flags.\n\n"),
6161 cb.cb_unavail ? gettext("available ") : "");
6162 (void) printf(gettext("Every %sfeature flags "
6163 "pool already has all supported features "
6165 cb.cb_unavail ? gettext("available ") : "");
6167 (void) printf(gettext("All pools are already "
6168 "formatted with version %llu or higher.\n"),
6172 } else if (argc == 0) {
6173 cb.cb_first = B_TRUE;
6174 ret = zpool_iter(g_zfs, upgrade_list_unavail, &cb);
6178 (void) fprintf(stderr, "\n");
6181 cb.cb_first = B_TRUE;
6182 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
6186 (void) printf(gettext("All %spools are formatted using "
6187 "feature flags.\n\n"), cb.cb_unavail ?
6188 gettext("available ") : "");
6190 (void) printf(gettext("\nUse 'zpool upgrade -v' "
6191 "for a list of available legacy versions.\n"));
6194 cb.cb_first = B_TRUE;
6195 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
6199 (void) printf(gettext("Every %sfeature flags pool has "
6200 "all supported features enabled.\n"),
6201 cb.cb_unavail ? gettext("available ") : "");
6203 (void) printf(gettext("\n"));
6206 ret = for_each_pool(argc, argv, B_TRUE, NULL,
6210 if (cb.cb_poolname[0] != '\0') {
6212 "If you boot from pool '%s', don't forget to update boot code.\n"
6213 "Assuming you use GPT partitioning and da0 is your boot disk\n"
6214 "the following command will do it:\n"
6216 "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n",
6223 typedef struct hist_cbdata {
6230 * Print out the command history for a specific pool.
6233 get_history_one(zpool_handle_t *zhp, void *data)
6239 hist_cbdata_t *cb = (hist_cbdata_t *)data;
6241 cb->first = B_FALSE;
6243 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
6245 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
6248 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
6249 &records, &numrecords) == 0);
6250 for (i = 0; i < numrecords; i++) {
6251 nvlist_t *rec = records[i];
6254 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
6258 tsec = fnvlist_lookup_uint64(records[i],
6260 (void) localtime_r(&tsec, &t);
6261 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
6264 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
6265 (void) printf("%s %s", tbuf,
6266 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
6267 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
6269 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
6272 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
6273 (void) printf("%s unrecognized record:\n",
6275 dump_nvlist(rec, 4);
6278 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
6279 zfs_history_event_names[ievent],
6280 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
6281 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
6282 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
6285 (void) printf("%s [txg:%lld] %s", tbuf,
6286 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
6287 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
6288 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
6289 (void) printf(" %s (%llu)",
6290 fnvlist_lookup_string(rec,
6292 fnvlist_lookup_uint64(rec,
6295 (void) printf(" %s", fnvlist_lookup_string(rec,
6296 ZPOOL_HIST_INT_STR));
6297 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
6300 (void) printf("%s ioctl %s\n", tbuf,
6301 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
6302 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
6303 (void) printf(" input:\n");
6304 dump_nvlist(fnvlist_lookup_nvlist(rec,
6305 ZPOOL_HIST_INPUT_NVL), 8);
6307 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
6308 (void) printf(" output:\n");
6309 dump_nvlist(fnvlist_lookup_nvlist(rec,
6310 ZPOOL_HIST_OUTPUT_NVL), 8);
6312 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
6313 (void) printf(" errno: %lld\n",
6314 fnvlist_lookup_int64(rec,
6320 (void) printf("%s unrecognized record:\n", tbuf);
6321 dump_nvlist(rec, 4);
6325 (void) printf("\n");
6328 (void) printf(" [");
6329 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
6330 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
6331 struct passwd *pwd = getpwuid(who);
6332 (void) printf("user %d ", (int)who);
6334 (void) printf("(%s) ", pwd->pw_name);
6336 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
6337 (void) printf("on %s",
6338 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
6340 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
6341 (void) printf(":%s",
6342 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
6345 (void) printf("\n");
6347 (void) printf("\n");
6354 * zpool history <pool>
6356 * Displays the history of commands that modified pools.
6359 zpool_do_history(int argc, char **argv)
6361 hist_cbdata_t cbdata = { 0 };
6365 cbdata.first = B_TRUE;
6367 while ((c = getopt(argc, argv, "li")) != -1) {
6370 cbdata.longfmt = B_TRUE;
6373 cbdata.internal = B_TRUE;
6376 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6384 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
6387 if (argc == 0 && cbdata.first == B_TRUE) {
6388 (void) printf(gettext("no pools available\n"));
6396 get_callback(zpool_handle_t *zhp, void *data)
6398 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
6399 char value[MAXNAMELEN];
6400 zprop_source_t srctype;
6403 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
6406 * Skip the special fake placeholder. This will also skip
6407 * over the name property when 'all' is specified.
6409 if (pl->pl_prop == ZPOOL_PROP_NAME &&
6410 pl == cbp->cb_proplist)
6413 if (pl->pl_prop == ZPROP_INVAL &&
6414 (zpool_prop_feature(pl->pl_user_prop) ||
6415 zpool_prop_unsupported(pl->pl_user_prop))) {
6416 srctype = ZPROP_SRC_LOCAL;
6418 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
6419 value, sizeof (value)) == 0) {
6420 zprop_print_one_property(zpool_get_name(zhp),
6421 cbp, pl->pl_user_prop, value, srctype,
6425 if (zpool_get_prop(zhp, pl->pl_prop, value,
6426 sizeof (value), &srctype, cbp->cb_literal) != 0)
6429 zprop_print_one_property(zpool_get_name(zhp), cbp,
6430 zpool_prop_to_name(pl->pl_prop), value, srctype,
6438 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
6440 * -H Scripted mode. Don't display headers, and separate properties
6442 * -o List of columns to display. Defaults to
6443 * "name,property,value,source".
6444 * -p Diplay values in parsable (exact) format.
6446 * Get properties of pools in the system. Output space statistics
6447 * for each one as well as other attributes.
6450 zpool_do_get(int argc, char **argv)
6452 zprop_get_cbdata_t cb = { 0 };
6453 zprop_list_t fake_name = { 0 };
6458 cb.cb_first = B_TRUE;
6461 * Set up default columns and sources.
6463 cb.cb_sources = ZPROP_SRC_ALL;
6464 cb.cb_columns[0] = GET_COL_NAME;
6465 cb.cb_columns[1] = GET_COL_PROPERTY;
6466 cb.cb_columns[2] = GET_COL_VALUE;
6467 cb.cb_columns[3] = GET_COL_SOURCE;
6468 cb.cb_type = ZFS_TYPE_POOL;
6471 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
6474 cb.cb_literal = B_TRUE;
6477 cb.cb_scripted = B_TRUE;
6480 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
6482 while (*optarg != '\0') {
6483 static char *col_subopts[] =
6484 { "name", "property", "value", "source",
6487 if (i == ZFS_GET_NCOLS) {
6488 (void) fprintf(stderr, gettext("too "
6489 "many fields given to -o "
6494 switch (getsubopt(&optarg, col_subopts,
6497 cb.cb_columns[i++] = GET_COL_NAME;
6500 cb.cb_columns[i++] = GET_COL_PROPERTY;
6503 cb.cb_columns[i++] = GET_COL_VALUE;
6506 cb.cb_columns[i++] = GET_COL_SOURCE;
6510 (void) fprintf(stderr,
6511 gettext("\"all\" conflicts "
6512 "with specific fields "
6513 "given to -o option\n"));
6516 cb.cb_columns[0] = GET_COL_NAME;
6517 cb.cb_columns[1] = GET_COL_PROPERTY;
6518 cb.cb_columns[2] = GET_COL_VALUE;
6519 cb.cb_columns[3] = GET_COL_SOURCE;
6523 (void) fprintf(stderr,
6524 gettext("invalid column name "
6525 "'%s'\n"), suboptarg);
6531 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6541 (void) fprintf(stderr, gettext("missing property "
6546 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
6547 ZFS_TYPE_POOL) != 0)
6553 if (cb.cb_proplist != NULL) {
6554 fake_name.pl_prop = ZPOOL_PROP_NAME;
6555 fake_name.pl_width = strlen(gettext("NAME"));
6556 fake_name.pl_next = cb.cb_proplist;
6557 cb.cb_proplist = &fake_name;
6560 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
6563 if (cb.cb_proplist == &fake_name)
6564 zprop_free_list(fake_name.pl_next);
6566 zprop_free_list(cb.cb_proplist);
6571 typedef struct set_cbdata {
6574 boolean_t cb_any_successful;
6578 set_callback(zpool_handle_t *zhp, void *data)
6581 set_cbdata_t *cb = (set_cbdata_t *)data;
6583 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
6586 cb->cb_any_successful = B_TRUE;
6592 zpool_do_set(int argc, char **argv)
6594 set_cbdata_t cb = { 0 };
6597 if (argc > 1 && argv[1][0] == '-') {
6598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6604 (void) fprintf(stderr, gettext("missing property=value "
6610 (void) fprintf(stderr, gettext("missing pool name\n"));
6615 (void) fprintf(stderr, gettext("too many pool names\n"));
6619 cb.cb_propname = argv[1];
6620 cb.cb_value = strchr(cb.cb_propname, '=');
6621 if (cb.cb_value == NULL) {
6622 (void) fprintf(stderr, gettext("missing value in "
6623 "property=value argument\n"));
6627 *(cb.cb_value) = '\0';
6630 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
6637 find_command_idx(char *command, int *idx)
6641 for (i = 0; i < NCOMMAND; i++) {
6642 if (command_table[i].name == NULL)
6645 if (strcmp(command, command_table[i].name) == 0) {
6654 main(int argc, char **argv)
6660 (void) setlocale(LC_ALL, "");
6661 (void) textdomain(TEXT_DOMAIN);
6663 if ((g_zfs = libzfs_init()) == NULL) {
6664 (void) fprintf(stderr, gettext("internal error: failed to "
6665 "initialize ZFS library\n"));
6669 libzfs_print_on_error(g_zfs, B_TRUE);
6674 * Make sure the user has specified some command.
6677 (void) fprintf(stderr, gettext("missing command\n"));
6686 if (strcmp(cmdname, "-?") == 0)
6689 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6692 * Run the appropriate command.
6694 if (find_command_idx(cmdname, &i) == 0) {
6695 current_command = &command_table[i];
6696 ret = command_table[i].func(argc - 1, argv + 1);
6697 } else if (strchr(cmdname, '=')) {
6698 verify(find_command_idx("set", &i) == 0);
6699 current_command = &command_table[i];
6700 ret = command_table[i].func(argc, argv);
6701 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
6703 * 'freeze' is a vile debugging abomination, so we treat
6706 zfs_cmd_t zc = { 0 };
6707 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
6708 return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc));
6710 (void) fprintf(stderr, gettext("unrecognized "
6711 "command '%s'\n"), cmdname);
6715 if (ret == 0 && log_history)
6716 (void) zpool_log_history(g_zfs, history_str);
6721 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6722 * for the purposes of running ::findleaks.
6724 if (getenv("ZFS_ABORT") != NULL) {
6725 (void) printf("dumping core by request\n");