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 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
48 #include <sys/fs/zfs.h>
53 #include "zpool_util.h"
54 #include "zfs_comutil.h"
55 #include "zfeature_common.h"
57 #include "statcommon.h"
59 static int zpool_do_create(int, char **);
60 static int zpool_do_destroy(int, char **);
62 static int zpool_do_add(int, char **);
63 static int zpool_do_remove(int, char **);
65 static int zpool_do_list(int, char **);
66 static int zpool_do_iostat(int, char **);
67 static int zpool_do_status(int, char **);
69 static int zpool_do_online(int, char **);
70 static int zpool_do_offline(int, char **);
71 static int zpool_do_clear(int, char **);
72 static int zpool_do_reopen(int, char **);
74 static int zpool_do_reguid(int, char **);
76 static int zpool_do_attach(int, char **);
77 static int zpool_do_detach(int, char **);
78 static int zpool_do_replace(int, char **);
79 static int zpool_do_split(int, char **);
81 static int zpool_do_scrub(int, char **);
83 static int zpool_do_import(int, char **);
84 static int zpool_do_export(int, char **);
86 static int zpool_do_upgrade(int, char **);
88 static int zpool_do_history(int, char **);
90 static int zpool_do_get(int, char **);
91 static int zpool_do_set(int, char **);
94 * These libumem hooks provide a reasonable set of defaults for the allocator's
95 * debugging facilities.
100 _umem_debug_init(void)
102 return ("default,verbose"); /* $UMEM_DEBUG setting */
106 _umem_logging_init(void)
108 return ("fail,contents"); /* $UMEM_LOGGING setting */
139 typedef struct zpool_command
{
141 int (*func
)(int, char **);
146 * Master command table. Each ZFS command has a name, associated function, and
147 * usage message. The usage messages need to be internationalized, so we have
148 * to have a function to return the usage message based on a command index.
150 * These commands are organized according to how they are displayed in the usage
151 * message. An empty command (one with a NULL name) indicates an empty line in
152 * the generic usage message.
154 static zpool_command_t command_table
[] = {
155 { "create", zpool_do_create
, HELP_CREATE
},
156 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
158 { "add", zpool_do_add
, HELP_ADD
},
159 { "remove", zpool_do_remove
, HELP_REMOVE
},
161 { "list", zpool_do_list
, HELP_LIST
},
162 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
163 { "status", zpool_do_status
, HELP_STATUS
},
165 { "online", zpool_do_online
, HELP_ONLINE
},
166 { "offline", zpool_do_offline
, HELP_OFFLINE
},
167 { "clear", zpool_do_clear
, HELP_CLEAR
},
168 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
170 { "attach", zpool_do_attach
, HELP_ATTACH
},
171 { "detach", zpool_do_detach
, HELP_DETACH
},
172 { "replace", zpool_do_replace
, HELP_REPLACE
},
173 { "split", zpool_do_split
, HELP_SPLIT
},
175 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
177 { "import", zpool_do_import
, HELP_IMPORT
},
178 { "export", zpool_do_export
, HELP_EXPORT
},
179 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
180 { "reguid", zpool_do_reguid
, HELP_REGUID
},
182 { "history", zpool_do_history
, HELP_HISTORY
},
183 { "get", zpool_do_get
, HELP_GET
},
184 { "set", zpool_do_set
, HELP_SET
},
187 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
189 static zpool_command_t
*current_command
;
190 static char history_str
[HIS_MAX_RECORD_LEN
];
191 static boolean_t log_history
= B_TRUE
;
192 static uint_t timestamp_fmt
= NODATE
;
195 get_usage(zpool_help_t idx
)
199 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
201 return (gettext("\tattach [-f] <pool> <device> "
204 return (gettext("\tclear [-nF] <pool> [device]\n"));
206 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
207 "\t [-O file-system-property=value] ... \n"
208 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
210 return (gettext("\tdestroy [-f] <pool>\n"));
212 return (gettext("\tdetach <pool> <device>\n"));
214 return (gettext("\texport [-f] <pool> ...\n"));
216 return (gettext("\thistory [-il] [<pool>] ...\n"));
218 return (gettext("\timport [-d dir] [-D]\n"
219 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
220 "\timport [-o mntopts] [-o property=value] ... \n"
221 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
222 "[-R root] [-F [-n]] -a\n"
223 "\timport [-o mntopts] [-o property=value] ... \n"
224 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
225 "[-R root] [-F [-n]]\n"
226 "\t <pool | id> [newpool]\n"));
228 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
231 return (gettext("\tlist [-Hp] [-o property[,...]] "
232 "[-T d|u] [pool] ... [interval [count]]\n"));
234 return (gettext("\toffline [-t] <pool> <device> ...\n"));
236 return (gettext("\tonline <pool> <device> ...\n"));
238 return (gettext("\treplace [-f] <pool> <device> "
241 return (gettext("\tremove <pool> <device> ...\n"));
243 return (gettext("\treopen <pool>\n"));
245 return (gettext("\tscrub [-s] <pool> ...\n"));
247 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
250 return (gettext("\tupgrade\n"
252 "\tupgrade [-V version] <-a | pool ...>\n"));
254 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
255 "<\"all\" | property[,...]> <pool> ...\n"));
257 return (gettext("\tset <property=value> <pool> \n"));
259 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
260 "\t [-o property=value] <pool> <newpool> "
261 "[<device> ...]\n"));
263 return (gettext("\treguid <pool>\n"));
272 * Callback routine that will print out a pool property value.
275 print_prop_cb(int prop
, void *cb
)
279 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
281 if (zpool_prop_readonly(prop
))
282 (void) fprintf(fp
, " NO ");
284 (void) fprintf(fp
, " YES ");
286 if (zpool_prop_values(prop
) == NULL
)
287 (void) fprintf(fp
, "-\n");
289 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
295 * Display usage message. If we're inside a command, display only the usage for
296 * that command. Otherwise, iterate over the entire command table and display
297 * a complete usage message.
300 usage(boolean_t requested
)
302 FILE *fp
= requested
? stdout
: stderr
;
304 if (current_command
== NULL
) {
307 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
309 gettext("where 'command' is one of the following:\n\n"));
311 for (i
= 0; i
< NCOMMAND
; i
++) {
312 if (command_table
[i
].name
== NULL
)
313 (void) fprintf(fp
, "\n");
315 (void) fprintf(fp
, "%s",
316 get_usage(command_table
[i
].usage
));
319 (void) fprintf(fp
, gettext("usage:\n"));
320 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
323 if (current_command
!= NULL
&&
324 ((strcmp(current_command
->name
, "set") == 0) ||
325 (strcmp(current_command
->name
, "get") == 0) ||
326 (strcmp(current_command
->name
, "list") == 0))) {
329 gettext("\nthe following properties are supported:\n"));
331 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
332 "PROPERTY", "EDIT", "VALUES");
334 /* Iterate over all properties */
335 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
338 (void) fprintf(fp
, "\t%-15s ", "feature@...");
339 (void) fprintf(fp
, "YES disabled | enabled | active\n");
341 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
342 "appended with a feature name.\nSee zpool-features(5).\n"));
346 * See comments at end of main().
348 if (getenv("ZFS_ABORT") != NULL
) {
349 (void) printf("dumping core by request\n");
353 exit(requested
? 0 : 2);
357 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
358 boolean_t print_logs
)
365 (void) printf("\t%*s%s\n", indent
, "", name
);
367 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
368 &child
, &children
) != 0)
371 for (c
= 0; c
< children
; c
++) {
372 uint64_t is_log
= B_FALSE
;
374 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
376 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
379 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
380 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
387 prop_list_contains_feature(nvlist_t
*proplist
)
390 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
391 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
392 if (zpool_prop_feature(nvpair_name(nvp
)))
399 * Add a property pair (name, string-value) into a property nvlist.
402 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
405 zpool_prop_t prop
= ZPROP_INVAL
;
411 if (*props
== NULL
&&
412 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
413 (void) fprintf(stderr
,
414 gettext("internal error: out of memory\n"));
421 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
423 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
424 !zpool_prop_feature(propname
)) {
425 (void) fprintf(stderr
, gettext("property '%s' is "
426 "not a valid pool property\n"), propname
);
431 * feature@ properties and version should not be specified
434 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
435 nvlist_exists(proplist
, vname
)) ||
436 (prop
== ZPOOL_PROP_VERSION
&&
437 prop_list_contains_feature(proplist
))) {
438 (void) fprintf(stderr
, gettext("'feature@' and "
439 "'version' properties cannot be specified "
445 if (zpool_prop_feature(propname
))
448 normnm
= zpool_prop_to_name(prop
);
450 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
451 normnm
= zfs_prop_to_name(fprop
);
457 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
458 prop
!= ZPOOL_PROP_CACHEFILE
) {
459 (void) fprintf(stderr
, gettext("property '%s' "
460 "specified multiple times\n"), propname
);
464 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
465 (void) fprintf(stderr
, gettext("internal "
466 "error: out of memory\n"));
474 * zpool add [-fn] <pool> <vdev> ...
476 * -f Force addition of devices, even if they appear in use
477 * -n Do not add the devices, but display the resulting layout if
478 * they were to be added.
480 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
481 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
485 zpool_do_add(int argc
, char **argv
)
487 boolean_t force
= B_FALSE
;
488 boolean_t dryrun
= B_FALSE
;
497 while ((c
= getopt(argc
, argv
, "fn")) != -1) {
506 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
515 /* get pool name and check number of arguments */
517 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
521 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
530 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
533 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
534 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
540 /* pass off to get_vdev_spec for processing */
541 nvroot
= make_root_vdev(zhp
, force
, !force
, B_FALSE
, dryrun
,
543 if (nvroot
== NULL
) {
549 nvlist_t
*poolnvroot
;
551 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
554 (void) printf(gettext("would update '%s' to the following "
555 "configuration:\n"), zpool_get_name(zhp
));
557 /* print original main pool and new tree */
558 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
559 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
561 /* Do the same for the logs */
562 if (num_logs(poolnvroot
) > 0) {
563 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
564 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
565 } else if (num_logs(nvroot
) > 0) {
566 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
571 ret
= (zpool_add(zhp
, nvroot
) != 0);
581 * zpool remove <pool> <vdev> ...
583 * Removes the given vdev from the pool. Currently, this supports removing
584 * spares, cache, and log devices from the pool.
587 zpool_do_remove(int argc
, char **argv
)
596 /* get pool name and check number of arguments */
598 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
602 (void) fprintf(stderr
, gettext("missing device\n"));
608 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
611 for (i
= 1; i
< argc
; i
++) {
612 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
620 * zpool create [-fnd] [-o property=value] ...
621 * [-O file-system-property=value] ...
622 * [-R root] [-m mountpoint] <pool> <dev> ...
624 * -f Force creation, even if devices appear in use
625 * -n Do not create the pool, but display the resulting layout if it
626 * were to be created.
627 * -R Create a pool under an alternate root
628 * -m Set default mountpoint for the root dataset. By default it's
630 * -o Set property=value.
631 * -d Don't automatically enable all supported pool features
632 * (individual features can be enabled with -o).
633 * -O Set fsproperty=value in the pool's root file system
635 * Creates the named pool according to the given vdev specification. The
636 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
637 * we get the nvlist back from get_vdev_spec(), we either print out the contents
638 * (if '-n' was specified), or pass it to libzfs to do the creation.
641 zpool_do_create(int argc
, char **argv
)
643 boolean_t force
= B_FALSE
;
644 boolean_t dryrun
= B_FALSE
;
645 boolean_t enable_all_pool_feat
= B_TRUE
;
647 nvlist_t
*nvroot
= NULL
;
650 char *altroot
= NULL
;
651 char *mountpoint
= NULL
;
652 nvlist_t
*fsprops
= NULL
;
653 nvlist_t
*props
= NULL
;
657 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:")) != -1) {
666 enable_all_pool_feat
= B_FALSE
;
670 if (add_prop_list(zpool_prop_to_name(
671 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
673 if (nvlist_lookup_string(props
,
674 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
677 if (add_prop_list(zpool_prop_to_name(
678 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
682 /* Equivalent to -O mountpoint=optarg */
686 if ((propval
= strchr(optarg
, '=')) == NULL
) {
687 (void) fprintf(stderr
, gettext("missing "
688 "'=' for -o option\n"));
694 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
698 * If the user is creating a pool that doesn't support
699 * feature flags, don't enable any features.
701 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
705 ver
= strtoull(propval
, &end
, 10);
707 ver
< SPA_VERSION_FEATURES
) {
708 enable_all_pool_feat
= B_FALSE
;
711 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
715 if ((propval
= strchr(optarg
, '=')) == NULL
) {
716 (void) fprintf(stderr
, gettext("missing "
717 "'=' for -O option\n"));
724 * Mountpoints are checked and then added later.
725 * Uniquely among properties, they can be specified
726 * more than once, to avoid conflict with -m.
728 if (0 == strcmp(optarg
,
729 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
730 mountpoint
= propval
;
731 } else if (add_prop_list(optarg
, propval
, &fsprops
,
737 (void) fprintf(stderr
, gettext("missing argument for "
738 "'%c' option\n"), optopt
);
741 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
750 /* get pool name and check number of arguments */
752 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
756 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
763 * As a special case, check for use of '/' in the name, and direct the
764 * user to use 'zfs create' instead.
766 if (strchr(poolname
, '/') != NULL
) {
767 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
768 "character '/' in pool name\n"), poolname
);
769 (void) fprintf(stderr
, gettext("use 'zfs create' to "
770 "create a dataset\n"));
774 /* pass off to get_vdev_spec for bulk processing */
775 nvroot
= make_root_vdev(NULL
, force
, !force
, B_FALSE
, dryrun
,
780 /* make_root_vdev() allows 0 toplevel children if there are spares */
781 if (!zfs_allocatable_devs(nvroot
)) {
782 (void) fprintf(stderr
, gettext("invalid vdev "
783 "specification: at least one toplevel vdev must be "
788 if (altroot
!= NULL
&& altroot
[0] != '/') {
789 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
790 "must be an absolute path\n"), altroot
);
795 * Check the validity of the mountpoint and direct the user to use the
796 * '-m' mountpoint option if it looks like its in use.
798 if (mountpoint
== NULL
||
799 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
800 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
801 char buf
[MAXPATHLEN
];
804 if (mountpoint
&& mountpoint
[0] != '/') {
805 (void) fprintf(stderr
, gettext("invalid mountpoint "
806 "'%s': must be an absolute path, 'legacy', or "
807 "'none'\n"), mountpoint
);
811 if (mountpoint
== NULL
) {
813 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
816 (void) snprintf(buf
, sizeof (buf
), "/%s",
820 (void) snprintf(buf
, sizeof (buf
), "%s%s",
821 altroot
, mountpoint
);
823 (void) snprintf(buf
, sizeof (buf
), "%s",
827 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
828 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
829 "%s\n"), buf
, strerror(errno
));
830 (void) fprintf(stderr
, gettext("use '-m' "
831 "option to provide a different default\n"));
836 while (count
< 3 && readdir(dirp
) != NULL
)
838 (void) closedir(dirp
);
841 (void) fprintf(stderr
, gettext("mountpoint "
842 "'%s' exists and is not empty\n"), buf
);
843 (void) fprintf(stderr
, gettext("use '-m' "
844 "option to provide a "
845 "different default\n"));
852 * Now that the mountpoint's validity has been checked, ensure that
853 * the property is set appropriately prior to creating the pool.
855 if (mountpoint
!= NULL
) {
856 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
857 mountpoint
, &fsprops
, B_FALSE
);
865 * For a dry run invocation, print out a basic message and run
866 * through all the vdevs in the list and print out in an
867 * appropriate hierarchy.
869 (void) printf(gettext("would create '%s' with the "
870 "following layout:\n\n"), poolname
);
872 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
873 if (num_logs(nvroot
) > 0)
874 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
879 * Hand off to libzfs.
881 if (enable_all_pool_feat
) {
883 for (i
= 0; i
< SPA_FEATURES
; i
++) {
884 char propname
[MAXPATHLEN
];
885 zfeature_info_t
*feat
= &spa_feature_table
[i
];
887 (void) snprintf(propname
, sizeof (propname
),
888 "feature@%s", feat
->fi_uname
);
891 * Skip feature if user specified it manually
892 * on the command line.
894 if (nvlist_exists(props
, propname
))
897 ret
= add_prop_list(propname
,
898 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
905 if (zpool_create(g_zfs
, poolname
,
906 nvroot
, props
, fsprops
) == 0) {
907 zfs_handle_t
*pool
= zfs_open(g_zfs
, poolname
,
908 ZFS_TYPE_FILESYSTEM
);
910 if (zfs_mount(pool
, NULL
, 0) == 0)
911 ret
= zfs_shareall(pool
);
914 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
915 (void) fprintf(stderr
, gettext("pool name may have "
922 nvlist_free(fsprops
);
926 nvlist_free(fsprops
);
933 * zpool destroy <pool>
935 * -f Forcefully unmount any datasets
937 * Destroy the given pool. Automatically unmounts any datasets in the pool.
940 zpool_do_destroy(int argc
, char **argv
)
942 boolean_t force
= B_FALSE
;
949 while ((c
= getopt(argc
, argv
, "f")) != -1) {
955 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
964 /* check arguments */
966 (void) fprintf(stderr
, gettext("missing pool argument\n"));
970 (void) fprintf(stderr
, gettext("too many arguments\n"));
976 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
978 * As a special case, check for use of '/' in the name, and
979 * direct the user to use 'zfs destroy' instead.
981 if (strchr(pool
, '/') != NULL
)
982 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
983 "destroy a dataset\n"));
987 if (zpool_disable_datasets(zhp
, force
) != 0) {
988 (void) fprintf(stderr
, gettext("could not destroy '%s': "
989 "could not unmount datasets\n"), zpool_get_name(zhp
));
993 /* The history must be logged as part of the export */
994 log_history
= B_FALSE
;
996 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1004 * zpool export [-f] <pool> ...
1006 * -f Forcefully unmount datasets
1008 * Export the given pools. By default, the command will attempt to cleanly
1009 * unmount any active datasets within the pool. If the '-f' flag is specified,
1010 * then the datasets will be forcefully unmounted.
1013 zpool_do_export(int argc
, char **argv
)
1015 boolean_t force
= B_FALSE
;
1016 boolean_t hardforce
= B_FALSE
;
1018 zpool_handle_t
*zhp
;
1023 while ((c
= getopt(argc
, argv
, "fF")) != -1) {
1032 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1041 /* check arguments */
1043 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1048 for (i
= 0; i
< argc
; i
++) {
1049 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) == NULL
) {
1054 if (zpool_disable_datasets(zhp
, force
) != 0) {
1060 /* The history must be logged as part of the export */
1061 log_history
= B_FALSE
;
1064 if (zpool_export_force(zhp
, history_str
) != 0)
1066 } else if (zpool_export(zhp
, force
, history_str
) != 0) {
1077 * Given a vdev configuration, determine the maximum width needed for the device
1081 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1083 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1088 if (strlen(name
) + depth
> max
)
1089 max
= strlen(name
) + depth
;
1093 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1094 &child
, &children
) == 0) {
1095 for (c
= 0; c
< children
; c
++)
1096 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1101 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1102 &child
, &children
) == 0) {
1103 for (c
= 0; c
< children
; c
++)
1104 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1109 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1110 &child
, &children
) == 0) {
1111 for (c
= 0; c
< children
; c
++)
1112 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1121 typedef struct spare_cbdata
{
1123 zpool_handle_t
*cb_zhp
;
1127 find_vdev(nvlist_t
*nv
, uint64_t search
)
1133 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1137 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1138 &child
, &children
) == 0) {
1139 for (c
= 0; c
< children
; c
++)
1140 if (find_vdev(child
[c
], search
))
1148 find_spare(zpool_handle_t
*zhp
, void *data
)
1150 spare_cbdata_t
*cbp
= data
;
1151 nvlist_t
*config
, *nvroot
;
1153 config
= zpool_get_config(zhp
, NULL
);
1154 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1157 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1167 * Print out configuration state as requested by status_callback.
1170 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1171 int namewidth
, int depth
, boolean_t isspare
)
1175 pool_scan_stat_t
*ps
= NULL
;
1177 char rbuf
[6], wbuf
[6], cbuf
[6];
1179 uint64_t notpresent
;
1183 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1184 &child
, &children
) != 0)
1187 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1188 (uint64_t **)&vs
, &c
) == 0);
1190 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1193 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1196 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1198 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1202 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1206 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1207 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1208 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1209 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1212 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1213 ¬present
) == 0) {
1215 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1216 (void) printf(" was %s", path
);
1217 } else if (vs
->vs_aux
!= 0) {
1220 switch (vs
->vs_aux
) {
1221 case VDEV_AUX_OPEN_FAILED
:
1222 (void) printf(gettext("cannot open"));
1225 case VDEV_AUX_BAD_GUID_SUM
:
1226 (void) printf(gettext("missing device"));
1229 case VDEV_AUX_NO_REPLICAS
:
1230 (void) printf(gettext("insufficient replicas"));
1233 case VDEV_AUX_VERSION_NEWER
:
1234 (void) printf(gettext("newer version"));
1237 case VDEV_AUX_UNSUP_FEAT
:
1238 (void) printf(gettext("unsupported feature(s)"));
1241 case VDEV_AUX_SPARED
:
1242 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1244 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1245 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1246 zpool_get_name(zhp
)) == 0)
1247 (void) printf(gettext("currently in "
1250 (void) printf(gettext("in use by "
1252 zpool_get_name(cb
.cb_zhp
));
1253 zpool_close(cb
.cb_zhp
);
1255 (void) printf(gettext("currently in use"));
1259 case VDEV_AUX_ERR_EXCEEDED
:
1260 (void) printf(gettext("too many errors"));
1263 case VDEV_AUX_IO_FAILURE
:
1264 (void) printf(gettext("experienced I/O failures"));
1267 case VDEV_AUX_BAD_LOG
:
1268 (void) printf(gettext("bad intent log"));
1271 case VDEV_AUX_EXTERNAL
:
1272 (void) printf(gettext("external device fault"));
1275 case VDEV_AUX_SPLIT_POOL
:
1276 (void) printf(gettext("split into new pool"));
1280 (void) printf(gettext("corrupted data"));
1285 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1286 (uint64_t **)&ps
, &c
);
1288 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1289 vs
->vs_scan_processed
!= 0 && children
== 0) {
1290 (void) printf(gettext(" (%s)"),
1291 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1292 "resilvering" : "repairing");
1295 (void) printf("\n");
1297 for (c
= 0; c
< children
; c
++) {
1298 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1300 /* Don't print logs or holes here */
1301 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1303 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1305 if (islog
|| ishole
)
1307 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1308 print_status_config(zhp
, vname
, child
[c
],
1309 namewidth
, depth
+ 2, isspare
);
1316 * Print the configuration of an exported pool. Iterate over all vdevs in the
1317 * pool, printing out the name and status for each one.
1320 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1327 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1328 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1329 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1332 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1333 (uint64_t **)&vs
, &c
) == 0);
1335 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1336 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1338 if (vs
->vs_aux
!= 0) {
1341 switch (vs
->vs_aux
) {
1342 case VDEV_AUX_OPEN_FAILED
:
1343 (void) printf(gettext("cannot open"));
1346 case VDEV_AUX_BAD_GUID_SUM
:
1347 (void) printf(gettext("missing device"));
1350 case VDEV_AUX_NO_REPLICAS
:
1351 (void) printf(gettext("insufficient replicas"));
1354 case VDEV_AUX_VERSION_NEWER
:
1355 (void) printf(gettext("newer version"));
1358 case VDEV_AUX_UNSUP_FEAT
:
1359 (void) printf(gettext("unsupported feature(s)"));
1362 case VDEV_AUX_ERR_EXCEEDED
:
1363 (void) printf(gettext("too many errors"));
1367 (void) printf(gettext("corrupted data"));
1371 (void) printf("\n");
1373 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1374 &child
, &children
) != 0)
1377 for (c
= 0; c
< children
; c
++) {
1378 uint64_t is_log
= B_FALSE
;
1380 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1385 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1386 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1390 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1391 &child
, &children
) == 0) {
1392 (void) printf(gettext("\tcache\n"));
1393 for (c
= 0; c
< children
; c
++) {
1394 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1395 (void) printf("\t %s\n", vname
);
1400 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1401 &child
, &children
) == 0) {
1402 (void) printf(gettext("\tspares\n"));
1403 for (c
= 0; c
< children
; c
++) {
1404 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1405 (void) printf("\t %s\n", vname
);
1413 * Logs are recorded as top level vdevs in the main pool child array
1414 * but with "is_log" set to 1. We use either print_status_config() or
1415 * print_import_config() to print the top level logs then any log
1416 * children (eg mirrored slogs) are printed recursively - which
1417 * works because only the top level vdev is marked "is_log"
1420 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1425 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1429 (void) printf(gettext("\tlogs\n"));
1431 for (c
= 0; c
< children
; c
++) {
1432 uint64_t is_log
= B_FALSE
;
1435 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1439 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1441 print_status_config(zhp
, name
, child
[c
], namewidth
,
1444 print_import_config(name
, child
[c
], namewidth
, 2);
1450 * Display the status for the given pool.
1453 show_import(nvlist_t
*config
)
1455 uint64_t pool_state
;
1467 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1469 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1471 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1473 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1476 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1477 (uint64_t **)&vs
, &vsc
) == 0);
1478 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1480 reason
= zpool_import_status(config
, &msgid
);
1482 (void) printf(gettext(" pool: %s\n"), name
);
1483 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1484 (void) printf(gettext(" state: %s"), health
);
1485 if (pool_state
== POOL_STATE_DESTROYED
)
1486 (void) printf(gettext(" (DESTROYED)"));
1487 (void) printf("\n");
1490 case ZPOOL_STATUS_MISSING_DEV_R
:
1491 case ZPOOL_STATUS_MISSING_DEV_NR
:
1492 case ZPOOL_STATUS_BAD_GUID_SUM
:
1493 (void) printf(gettext(" status: One or more devices are "
1494 "missing from the system.\n"));
1497 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1498 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1499 (void) printf(gettext(" status: One or more devices contains "
1500 "corrupted data.\n"));
1503 case ZPOOL_STATUS_CORRUPT_DATA
:
1505 gettext(" status: The pool data is corrupted.\n"));
1508 case ZPOOL_STATUS_OFFLINE_DEV
:
1509 (void) printf(gettext(" status: One or more devices "
1510 "are offlined.\n"));
1513 case ZPOOL_STATUS_CORRUPT_POOL
:
1514 (void) printf(gettext(" status: The pool metadata is "
1518 case ZPOOL_STATUS_VERSION_OLDER
:
1519 (void) printf(gettext(" status: The pool is formatted using a "
1520 "legacy on-disk version.\n"));
1523 case ZPOOL_STATUS_VERSION_NEWER
:
1524 (void) printf(gettext(" status: The pool is formatted using an "
1525 "incompatible version.\n"));
1528 case ZPOOL_STATUS_FEAT_DISABLED
:
1529 (void) printf(gettext(" status: Some supported features are "
1530 "not enabled on the pool.\n"));
1533 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1534 (void) printf(gettext("status: The pool uses the following "
1535 "feature(s) not supported on this sytem:\n"));
1536 zpool_print_unsup_feat(config
);
1539 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1540 (void) printf(gettext("status: The pool can only be accessed "
1541 "in read-only mode on this system. It\n\tcannot be "
1542 "accessed in read-write mode because it uses the "
1543 "following\n\tfeature(s) not supported on this system:\n"));
1544 zpool_print_unsup_feat(config
);
1547 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1548 (void) printf(gettext(" status: The pool was last accessed by "
1549 "another system.\n"));
1552 case ZPOOL_STATUS_FAULTED_DEV_R
:
1553 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1554 (void) printf(gettext(" status: One or more devices are "
1558 case ZPOOL_STATUS_BAD_LOG
:
1559 (void) printf(gettext(" status: An intent log record cannot be "
1563 case ZPOOL_STATUS_RESILVERING
:
1564 (void) printf(gettext(" status: One or more devices were being "
1570 * No other status can be seen when importing pools.
1572 assert(reason
== ZPOOL_STATUS_OK
);
1576 * Print out an action according to the overall state of the pool.
1578 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1579 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1580 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1581 (void) printf(gettext(" action: The pool can be "
1582 "imported using its name or numeric identifier, "
1583 "though\n\tsome features will not be available "
1584 "without an explicit 'zpool upgrade'.\n"));
1585 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1586 (void) printf(gettext(" action: The pool can be "
1587 "imported using its name or numeric "
1588 "identifier and\n\tthe '-f' flag.\n"));
1590 (void) printf(gettext(" action: The pool can be "
1591 "imported using its name or numeric "
1594 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1595 (void) printf(gettext(" action: The pool can be imported "
1596 "despite missing or damaged devices. The\n\tfault "
1597 "tolerance of the pool may be compromised if imported.\n"));
1600 case ZPOOL_STATUS_VERSION_NEWER
:
1601 (void) printf(gettext(" action: The pool cannot be "
1602 "imported. Access the pool on a system running "
1603 "newer\n\tsoftware, or recreate the pool from "
1606 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1607 (void) printf(gettext("action: The pool cannot be "
1608 "imported. Access the pool on a system that "
1609 "supports\n\tthe required feature(s), or recreate "
1610 "the pool from backup.\n"));
1612 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1613 (void) printf(gettext("action: The pool cannot be "
1614 "imported in read-write mode. Import the pool "
1616 "\t\"-o readonly=on\", access the pool on a system "
1617 "that supports the\n\trequired feature(s), or "
1618 "recreate the pool from backup.\n"));
1620 case ZPOOL_STATUS_MISSING_DEV_R
:
1621 case ZPOOL_STATUS_MISSING_DEV_NR
:
1622 case ZPOOL_STATUS_BAD_GUID_SUM
:
1623 (void) printf(gettext(" action: The pool cannot be "
1624 "imported. Attach the missing\n\tdevices and try "
1628 (void) printf(gettext(" action: The pool cannot be "
1629 "imported due to damaged devices or data.\n"));
1633 /* Print the comment attached to the pool. */
1634 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1635 (void) printf(gettext("comment: %s\n"), comment
);
1638 * If the state is "closed" or "can't open", and the aux state
1639 * is "corrupt data":
1641 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1642 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1643 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1644 if (pool_state
== POOL_STATE_DESTROYED
)
1645 (void) printf(gettext("\tThe pool was destroyed, "
1646 "but can be imported using the '-Df' flags.\n"));
1647 else if (pool_state
!= POOL_STATE_EXPORTED
)
1648 (void) printf(gettext("\tThe pool may be active on "
1649 "another system, but can be imported using\n\t"
1650 "the '-f' flag.\n"));
1654 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1657 (void) printf(gettext(" config:\n\n"));
1659 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1663 print_import_config(name
, nvroot
, namewidth
, 0);
1664 if (num_logs(nvroot
) > 0)
1665 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1667 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1668 (void) printf(gettext("\n\tAdditional devices are known to "
1669 "be part of this pool, though their\n\texact "
1670 "configuration cannot be determined.\n"));
1675 * Perform the import for the given configuration. This passes the heavy
1676 * lifting off to zpool_import_props(), and then mounts the datasets contained
1680 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1681 nvlist_t
*props
, int flags
)
1683 zpool_handle_t
*zhp
;
1688 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1691 verify(nvlist_lookup_uint64(config
,
1692 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1693 verify(nvlist_lookup_uint64(config
,
1694 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1695 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1696 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1697 "is formatted using an unsupported ZFS version\n"), name
);
1699 } else if (state
!= POOL_STATE_EXPORTED
&&
1700 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1703 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1705 if ((unsigned long)hostid
!= gethostid()) {
1710 verify(nvlist_lookup_string(config
,
1711 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1712 verify(nvlist_lookup_uint64(config
,
1713 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
1715 (void) fprintf(stderr
, gettext("cannot import "
1716 "'%s': pool may be in use from other "
1717 "system, it was last accessed by %s "
1718 "(hostid: 0x%lx) on %s"), name
, hostname
,
1719 (unsigned long)hostid
,
1720 asctime(localtime(&t
)));
1721 (void) fprintf(stderr
, gettext("use '-f' to "
1722 "import anyway\n"));
1726 (void) fprintf(stderr
, gettext("cannot import '%s': "
1727 "pool may be in use from other system\n"), name
);
1728 (void) fprintf(stderr
, gettext("use '-f' to import "
1734 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
1737 if (newname
!= NULL
)
1738 name
= (char *)newname
;
1740 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
1743 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
1744 !(flags
& ZFS_IMPORT_ONLY
) &&
1745 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
1755 * zpool import [-d dir] [-D]
1756 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1757 * [-d dir | -c cachefile] [-f] -a
1758 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1759 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1761 * -c Read pool information from a cachefile instead of searching
1764 * -d Scan in a specific directory, other than /dev/dsk. More than
1765 * one directory can be specified using multiple '-d' options.
1767 * -D Scan for previously destroyed pools or import all or only
1768 * specified destroyed pools.
1770 * -R Temporarily import the pool, with all mountpoints relative to
1771 * the given root. The pool will remain exported when the machine
1774 * -V Import even in the presence of faulted vdevs. This is an
1775 * intentionally undocumented option for testing purposes, and
1776 * treats the pool configuration as complete, leaving any bad
1777 * vdevs in the FAULTED state. In other words, it does verbatim
1780 * -f Force import, even if it appears that the pool is active.
1782 * -F Attempt rewind if necessary.
1784 * -n See if rewind would work, but don't actually rewind.
1786 * -N Import the pool but don't mount datasets.
1788 * -T Specify a starting txg to use for import. This option is
1789 * intentionally undocumented option for testing purposes.
1791 * -a Import all pools found.
1793 * -o Set property=value and/or temporary mount options (without '=').
1795 * The import command scans for pools to import, and import pools based on pool
1796 * name and GUID. The pool can also be renamed as part of the import process.
1799 zpool_do_import(int argc
, char **argv
)
1801 char **searchdirs
= NULL
;
1805 nvlist_t
*pools
= NULL
;
1806 boolean_t do_all
= B_FALSE
;
1807 boolean_t do_destroyed
= B_FALSE
;
1808 char *mntopts
= NULL
;
1811 uint64_t searchguid
= 0;
1812 char *searchname
= NULL
;
1814 nvlist_t
*found_config
;
1815 nvlist_t
*policy
= NULL
;
1816 nvlist_t
*props
= NULL
;
1818 int flags
= ZFS_IMPORT_NORMAL
;
1819 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
1820 boolean_t dryrun
= B_FALSE
;
1821 boolean_t do_rewind
= B_FALSE
;
1822 boolean_t xtreme_rewind
= B_FALSE
;
1823 uint64_t pool_state
, txg
= -1ULL;
1824 char *cachefile
= NULL
;
1825 importargs_t idata
= { 0 };
1829 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1838 if (searchdirs
== NULL
) {
1839 searchdirs
= safe_malloc(sizeof (char *));
1841 char **tmp
= safe_malloc((nsearch
+ 1) *
1843 bcopy(searchdirs
, tmp
, nsearch
*
1848 searchdirs
[nsearch
++] = optarg
;
1851 do_destroyed
= B_TRUE
;
1854 flags
|= ZFS_IMPORT_ANY_HOST
;
1860 flags
|= ZFS_IMPORT_MISSING_LOG
;
1866 flags
|= ZFS_IMPORT_ONLY
;
1869 if ((propval
= strchr(optarg
, '=')) != NULL
) {
1872 if (add_prop_list(optarg
, propval
,
1880 if (add_prop_list(zpool_prop_to_name(
1881 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1883 if (nvlist_lookup_string(props
,
1884 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
1887 if (add_prop_list(zpool_prop_to_name(
1888 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1893 txg
= strtoull(optarg
, &endptr
, 0);
1894 if (errno
!= 0 || *endptr
!= '\0') {
1895 (void) fprintf(stderr
,
1896 gettext("invalid txg value\n"));
1899 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
1902 flags
|= ZFS_IMPORT_VERBATIM
;
1905 xtreme_rewind
= B_TRUE
;
1908 (void) fprintf(stderr
, gettext("missing argument for "
1909 "'%c' option\n"), optopt
);
1913 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1922 if (cachefile
&& nsearch
!= 0) {
1923 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
1927 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
1928 (void) fprintf(stderr
,
1929 gettext("-n or -X only meaningful with -F\n"));
1933 rewind_policy
= ZPOOL_TRY_REWIND
;
1935 rewind_policy
= ZPOOL_DO_REWIND
;
1937 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
1939 /* In the future, we can capture further policy and include it here */
1940 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
1941 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
1942 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
1945 if (searchdirs
== NULL
) {
1946 searchdirs
= safe_malloc(sizeof (char *));
1947 searchdirs
[0] = "/dev/dsk";
1951 /* check argument count */
1954 (void) fprintf(stderr
, gettext("too many arguments\n"));
1959 (void) fprintf(stderr
, gettext("too many arguments\n"));
1964 * Check for the SYS_CONFIG privilege. We do this explicitly
1965 * here because otherwise any attempt to discover pools will
1968 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
1969 (void) fprintf(stderr
, gettext("cannot "
1970 "discover pools: permission denied\n"));
1972 nvlist_free(policy
);
1978 * Depending on the arguments given, we do one of the following:
1980 * <none> Iterate through all pools and display information about
1983 * -a Iterate through all pools and try to import each one.
1985 * <id> Find the pool that corresponds to the given GUID/pool
1986 * name and import that one.
1988 * -D Above options applies only to destroyed pools.
1994 searchguid
= strtoull(argv
[0], &endptr
, 10);
1995 if (errno
!= 0 || *endptr
!= '\0') {
1996 searchname
= argv
[0];
1999 found_config
= NULL
;
2002 * User specified a name or guid. Ensure it's unique.
2004 idata
.unique
= B_TRUE
;
2008 idata
.path
= searchdirs
;
2009 idata
.paths
= nsearch
;
2010 idata
.poolname
= searchname
;
2011 idata
.guid
= searchguid
;
2012 idata
.cachefile
= cachefile
;
2014 pools
= zpool_search_import(g_zfs
, &idata
);
2016 if (pools
!= NULL
&& idata
.exists
&&
2017 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2018 (void) fprintf(stderr
, gettext("cannot import '%s': "
2019 "a pool with that name already exists\n"),
2021 (void) fprintf(stderr
, gettext("use the form '%s "
2022 "<pool | id> <newpool>' to give it a new name\n"),
2025 } else if (pools
== NULL
&& idata
.exists
) {
2026 (void) fprintf(stderr
, gettext("cannot import '%s': "
2027 "a pool with that name is already created/imported,\n"),
2029 (void) fprintf(stderr
, gettext("and no additional pools "
2030 "with that name were found\n"));
2032 } else if (pools
== NULL
) {
2034 (void) fprintf(stderr
, gettext("cannot import '%s': "
2035 "no such pool available\n"), argv
[0]);
2042 nvlist_free(policy
);
2047 * At this point we have a list of import candidate configs. Even if
2048 * we were searching by pool name or guid, we still need to
2049 * post-process the list to deal with pool state and possible
2055 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2057 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2059 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2061 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2063 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2066 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2073 (void) printf("\n");
2076 err
|= do_import(config
, NULL
, mntopts
,
2079 show_import(config
);
2081 } else if (searchname
!= NULL
) {
2085 * We are searching for a pool based on name.
2087 verify(nvlist_lookup_string(config
,
2088 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2090 if (strcmp(name
, searchname
) == 0) {
2091 if (found_config
!= NULL
) {
2092 (void) fprintf(stderr
, gettext(
2093 "cannot import '%s': more than "
2094 "one matching pool\n"), searchname
);
2095 (void) fprintf(stderr
, gettext(
2096 "import by numeric ID instead\n"));
2099 found_config
= config
;
2105 * Search for a pool by guid.
2107 verify(nvlist_lookup_uint64(config
,
2108 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2110 if (guid
== searchguid
)
2111 found_config
= config
;
2116 * If we were searching for a specific pool, verify that we found a
2117 * pool, and then do the import.
2119 if (argc
!= 0 && err
== 0) {
2120 if (found_config
== NULL
) {
2121 (void) fprintf(stderr
, gettext("cannot import '%s': "
2122 "no such pool available\n"), argv
[0]);
2125 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2126 argv
[1], mntopts
, props
, flags
);
2131 * If we were just looking for pools, report an error if none were
2134 if (argc
== 0 && first
)
2135 (void) fprintf(stderr
,
2136 gettext("no pools available to import\n"));
2141 nvlist_free(policy
);
2144 return (err
? 1 : 0);
2147 typedef struct iostat_cbdata
{
2148 boolean_t cb_verbose
;
2151 zpool_list_t
*cb_list
;
2155 print_iostat_separator(iostat_cbdata_t
*cb
)
2159 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2161 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2165 print_iostat_header(iostat_cbdata_t
*cb
)
2167 (void) printf("%*s capacity operations bandwidth\n",
2168 cb
->cb_namewidth
, "");
2169 (void) printf("%-*s alloc free read write read write\n",
2170 cb
->cb_namewidth
, "pool");
2171 print_iostat_separator(cb
);
2175 * Display a single statistic.
2178 print_one_stat(uint64_t value
)
2182 zfs_nicenum(value
, buf
, sizeof (buf
));
2183 (void) printf(" %5s", buf
);
2187 * Print out all the statistics for the given vdev. This can either be the
2188 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2189 * is a verbose output, and we don't want to display the toplevel pool stats.
2192 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2193 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2195 nvlist_t
**oldchild
, **newchild
;
2197 vdev_stat_t
*oldvs
, *newvs
;
2198 vdev_stat_t zerovs
= { 0 };
2203 if (oldnv
!= NULL
) {
2204 verify(nvlist_lookup_uint64_array(oldnv
,
2205 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2210 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2211 (uint64_t **)&newvs
, &c
) == 0);
2213 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2214 (void) printf("%*s%s", depth
, "", name
);
2216 (void) printf("%*s%s%*s", depth
, "", name
,
2217 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2219 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2224 scale
= (double)NANOSEC
/ tdelta
;
2226 /* only toplevel vdevs have capacity stats */
2227 if (newvs
->vs_space
== 0) {
2228 (void) printf(" - -");
2230 print_one_stat(newvs
->vs_alloc
);
2231 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2234 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2235 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2237 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2238 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2240 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2241 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2243 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2244 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2246 (void) printf("\n");
2248 if (!cb
->cb_verbose
)
2251 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2252 &newchild
, &children
) != 0)
2255 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2256 &oldchild
, &c
) != 0)
2259 for (c
= 0; c
< children
; c
++) {
2260 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2262 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2265 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2268 if (ishole
|| islog
)
2271 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2272 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2273 newchild
[c
], cb
, depth
+ 2);
2278 * Log device section
2281 if (num_logs(newnv
) > 0) {
2282 (void) printf("%-*s - - - - - "
2283 "-\n", cb
->cb_namewidth
, "logs");
2285 for (c
= 0; c
< children
; c
++) {
2286 uint64_t islog
= B_FALSE
;
2287 (void) nvlist_lookup_uint64(newchild
[c
],
2288 ZPOOL_CONFIG_IS_LOG
, &islog
);
2291 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2293 print_vdev_stats(zhp
, vname
, oldnv
?
2294 oldchild
[c
] : NULL
, newchild
[c
],
2303 * Include level 2 ARC devices in iostat output
2305 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2306 &newchild
, &children
) != 0)
2309 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2310 &oldchild
, &c
) != 0)
2314 (void) printf("%-*s - - - - - "
2315 "-\n", cb
->cb_namewidth
, "cache");
2316 for (c
= 0; c
< children
; c
++) {
2317 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2319 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2320 newchild
[c
], cb
, depth
+ 2);
2327 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2329 iostat_cbdata_t
*cb
= data
;
2333 * If the pool has disappeared, remove it from the list and continue.
2335 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2339 pool_list_remove(cb
->cb_list
, zhp
);
2345 * Callback to print out the iostats for the given pool.
2348 print_iostat(zpool_handle_t
*zhp
, void *data
)
2350 iostat_cbdata_t
*cb
= data
;
2351 nvlist_t
*oldconfig
, *newconfig
;
2352 nvlist_t
*oldnvroot
, *newnvroot
;
2354 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2356 if (cb
->cb_iteration
== 1)
2359 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2362 if (oldconfig
== NULL
)
2365 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2369 * Print out the statistics for the pool.
2371 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2374 print_iostat_separator(cb
);
2380 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2382 iostat_cbdata_t
*cb
= data
;
2383 nvlist_t
*config
, *nvroot
;
2385 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2386 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2388 if (!cb
->cb_verbose
)
2389 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2391 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2396 * The width must fall into the range [10,38]. The upper limit is the
2397 * maximum we can have and still fit in 80 columns.
2399 if (cb
->cb_namewidth
< 10)
2400 cb
->cb_namewidth
= 10;
2401 if (cb
->cb_namewidth
> 38)
2402 cb
->cb_namewidth
= 38;
2408 * Parse the input string, get the 'interval' and 'count' value if there is one.
2411 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2414 unsigned long interval
= 0, count
= 0;
2415 int argc
= *argcp
, errno
;
2418 * Determine if the last argument is an integer or a pool name
2420 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2424 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2426 if (*end
== '\0' && errno
== 0) {
2427 if (interval
== 0) {
2428 (void) fprintf(stderr
, gettext("interval "
2429 "cannot be zero\n"));
2433 * Ignore the last parameter
2438 * If this is not a valid number, just plow on. The
2439 * user will get a more informative error message later
2447 * If the last argument is also an integer, then we have both a count
2450 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2455 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2457 if (*end
== '\0' && errno
== 0) {
2458 if (interval
== 0) {
2459 (void) fprintf(stderr
, gettext("interval "
2460 "cannot be zero\n"));
2465 * Ignore the last parameter
2479 get_timestamp_arg(char c
)
2482 timestamp_fmt
= UDATE
;
2484 timestamp_fmt
= DDATE
;
2490 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2492 * -v Display statistics for individual vdevs
2493 * -T Display a timestamp in date(1) or Unix format
2495 * This command can be tricky because we want to be able to deal with pool
2496 * creation/destruction as well as vdev configuration changes. The bulk of this
2497 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2498 * on pool_list_update() to detect the addition of new pools. Configuration
2499 * changes are all handled within libzfs.
2502 zpool_do_iostat(int argc
, char **argv
)
2507 unsigned long interval
= 0, count
= 0;
2509 boolean_t verbose
= B_FALSE
;
2513 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2516 get_timestamp_arg(*optarg
);
2522 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2531 get_interval_count(&argc
, argv
, &interval
, &count
);
2534 * Construct the list of all interesting pools.
2537 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2540 if (pool_list_count(list
) == 0 && argc
!= 0) {
2541 pool_list_free(list
);
2545 if (pool_list_count(list
) == 0 && interval
== 0) {
2546 pool_list_free(list
);
2547 (void) fprintf(stderr
, gettext("no pools available\n"));
2552 * Enter the main iostat loop.
2555 cb
.cb_verbose
= verbose
;
2556 cb
.cb_iteration
= 0;
2557 cb
.cb_namewidth
= 0;
2560 pool_list_update(list
);
2562 if ((npools
= pool_list_count(list
)) == 0)
2566 * Refresh all statistics. This is done as an explicit step
2567 * before calculating the maximum name width, so that any
2568 * configuration changes are properly accounted for.
2570 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
, &cb
);
2573 * Iterate over all pools to determine the maximum width
2574 * for the pool / device name column across all pools.
2576 cb
.cb_namewidth
= 0;
2577 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
2579 if (timestamp_fmt
!= NODATE
)
2580 print_timestamp(timestamp_fmt
);
2583 * If it's the first time, or verbose mode, print the header.
2585 if (++cb
.cb_iteration
== 1 || verbose
)
2586 print_iostat_header(&cb
);
2588 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2591 * If there's more than one pool, and we're not in verbose mode
2592 * (which prints a separator for us), then print a separator.
2594 if (npools
> 1 && !verbose
)
2595 print_iostat_separator(&cb
);
2598 (void) printf("\n");
2601 * Flush the output so that redirection to a file isn't buffered
2604 (void) fflush(stdout
);
2609 if (count
!= 0 && --count
== 0)
2612 (void) sleep(interval
);
2615 pool_list_free(list
);
2620 typedef struct list_cbdata
{
2621 boolean_t cb_verbose
;
2623 boolean_t cb_scripted
;
2624 zprop_list_t
*cb_proplist
;
2625 boolean_t cb_literal
;
2629 * Given a list of columns to display, output appropriate headers for each one.
2632 print_header(list_cbdata_t
*cb
)
2634 zprop_list_t
*pl
= cb
->cb_proplist
;
2635 char headerbuf
[ZPOOL_MAXPROPLEN
];
2637 boolean_t first
= B_TRUE
;
2638 boolean_t right_justify
;
2641 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2642 width
= pl
->pl_width
;
2643 if (first
&& cb
->cb_verbose
) {
2645 * Reset the width to accommodate the verbose listing
2648 width
= cb
->cb_namewidth
;
2656 right_justify
= B_FALSE
;
2657 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2658 header
= zpool_prop_column_name(pl
->pl_prop
);
2659 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2663 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2664 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2665 headerbuf
[i
] = '\0';
2669 if (pl
->pl_next
== NULL
&& !right_justify
)
2670 (void) printf("%s", header
);
2671 else if (right_justify
)
2672 (void) printf("%*s", width
, header
);
2674 (void) printf("%-*s", width
, header
);
2678 (void) printf("\n");
2682 * Given a pool and a list of properties, print out all the properties according
2683 * to the described layout.
2686 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
2688 zprop_list_t
*pl
= cb
->cb_proplist
;
2689 boolean_t first
= B_TRUE
;
2690 char property
[ZPOOL_MAXPROPLEN
];
2692 boolean_t right_justify
;
2695 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2697 width
= pl
->pl_width
;
2698 if (first
&& cb
->cb_verbose
) {
2700 * Reset the width to accommodate the verbose listing
2703 width
= cb
->cb_namewidth
;
2707 if (cb
->cb_scripted
)
2708 (void) printf("\t");
2715 right_justify
= B_FALSE
;
2716 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2717 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
2718 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
2723 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2724 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
2725 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
2726 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
2727 sizeof (property
)) == 0) {
2735 * If this is being called in scripted mode, or if this is the
2736 * last column and it is left-justified, don't include a width
2739 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2740 (void) printf("%s", propstr
);
2741 else if (right_justify
)
2742 (void) printf("%*s", width
, propstr
);
2744 (void) printf("%-*s", width
, propstr
);
2747 (void) printf("\n");
2751 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
2756 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
2759 case ZPOOL_PROP_EXPANDSZ
:
2761 (void) strlcpy(propval
, "-", sizeof (propval
));
2763 zfs_nicenum(value
, propval
, sizeof (propval
));
2765 case ZPOOL_PROP_FRAGMENTATION
:
2766 if (value
== ZFS_FRAG_INVALID
) {
2767 (void) strlcpy(propval
, "-", sizeof (propval
));
2769 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
2773 case ZPOOL_PROP_CAPACITY
:
2774 (void) snprintf(propval
, sizeof (propval
), "%llu%%", value
);
2777 zfs_nicenum(value
, propval
, sizeof (propval
));
2781 (void) strlcpy(propval
, "-", sizeof (propval
));
2784 (void) printf("\t%s", propval
);
2786 (void) printf(" %*s", width
, propval
);
2790 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
2791 list_cbdata_t
*cb
, int depth
)
2797 boolean_t scripted
= cb
->cb_scripted
;
2798 uint64_t islog
= B_FALSE
;
2799 boolean_t haslog
= B_FALSE
;
2800 char *dashes
= "%-*s - - - - - -\n";
2802 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2803 (uint64_t **)&vs
, &c
) == 0);
2806 boolean_t toplevel
= (vs
->vs_space
!= 0);
2810 (void) printf("\t%s", name
);
2811 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
2812 (void) printf("%*s%s", depth
, "", name
);
2814 (void) printf("%*s%s%*s", depth
, "", name
,
2815 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2818 * Print the properties for the individual vdevs. Some
2819 * properties are only applicable to toplevel vdevs. The
2820 * 'toplevel' boolean value is passed to the print_one_column()
2821 * to indicate that the value is valid.
2823 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
2825 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
2827 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
2828 scripted
, toplevel
);
2829 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
2831 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
2832 vs
->vs_fragmentation
, scripted
,
2833 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
2834 cap
= (vs
->vs_space
== 0) ? 0 :
2835 (vs
->vs_alloc
* 100 / vs
->vs_space
);
2836 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
2837 (void) printf("\n");
2840 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2841 &child
, &children
) != 0)
2844 for (c
= 0; c
< children
; c
++) {
2845 uint64_t ishole
= B_FALSE
;
2847 if (nvlist_lookup_uint64(child
[c
],
2848 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
2851 if (nvlist_lookup_uint64(child
[c
],
2852 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
2857 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
2858 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2862 if (haslog
== B_TRUE
) {
2863 /* LINTED E_SEC_PRINTF_VAR_FMT */
2864 (void) printf(dashes
, cb
->cb_namewidth
, "log");
2865 for (c
= 0; c
< children
; c
++) {
2866 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2867 &islog
) != 0 || !islog
)
2869 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
2870 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2875 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2876 &child
, &children
) == 0 && children
> 0) {
2877 /* LINTED E_SEC_PRINTF_VAR_FMT */
2878 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
2879 for (c
= 0; c
< children
; c
++) {
2880 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
2881 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2886 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
2887 &children
) == 0 && children
> 0) {
2888 /* LINTED E_SEC_PRINTF_VAR_FMT */
2889 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
2890 for (c
= 0; c
< children
; c
++) {
2891 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
2892 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2900 * Generic callback function to list a pool.
2903 list_callback(zpool_handle_t
*zhp
, void *data
)
2905 list_cbdata_t
*cbp
= data
;
2909 config
= zpool_get_config(zhp
, NULL
);
2911 print_pool(zhp
, cbp
);
2912 if (!cbp
->cb_verbose
)
2915 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2917 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
2923 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2925 * -H Scripted mode. Don't display headers, and separate properties
2927 * -o List of properties to display. Defaults to
2928 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
2929 * "dedupratio,health,altroot"
2930 * -p Diplay values in parsable (exact) format.
2931 * -T Display a timestamp in date(1) or Unix format
2933 * List all pools in the system, whether or not they're healthy. Output space
2934 * statistics for each one, as well as health status summary.
2937 zpool_do_list(int argc
, char **argv
)
2941 list_cbdata_t cb
= { 0 };
2942 static char default_props
[] =
2943 "name,size,allocated,free,expandsize,fragmentation,capacity,"
2944 "dedupratio,health,altroot";
2945 char *props
= default_props
;
2946 unsigned long interval
= 0, count
= 0;
2948 boolean_t first
= B_TRUE
;
2951 while ((c
= getopt(argc
, argv
, ":Ho:pT:v")) != -1) {
2954 cb
.cb_scripted
= B_TRUE
;
2960 cb
.cb_literal
= B_TRUE
;
2963 get_timestamp_arg(*optarg
);
2966 cb
.cb_verbose
= B_TRUE
;
2969 (void) fprintf(stderr
, gettext("missing argument for "
2970 "'%c' option\n"), optopt
);
2974 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2983 get_interval_count(&argc
, argv
, &interval
, &count
);
2985 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
2989 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
2993 if (pool_list_count(list
) == 0)
2996 cb
.cb_namewidth
= 0;
2997 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
2999 if (timestamp_fmt
!= NODATE
)
3000 print_timestamp(timestamp_fmt
);
3002 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3006 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3011 if (count
!= 0 && --count
== 0)
3014 pool_list_free(list
);
3015 (void) sleep(interval
);
3018 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3019 (void) printf(gettext("no pools available\n"));
3023 pool_list_free(list
);
3024 zprop_free_list(cb
.cb_proplist
);
3029 zpool_get_vdev_by_name(nvlist_t
*nv
, char *name
)
3036 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3037 &child
, &children
) != 0) {
3038 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
3039 if (strncmp(name
, "/dev/dsk/", 9) == 0)
3041 if (strncmp(path
, "/dev/dsk/", 9) == 0)
3043 if (strcmp(name
, path
) == 0)
3048 for (c
= 0; c
< children
; c
++)
3049 if ((match
= zpool_get_vdev_by_name(child
[c
], name
)) != NULL
)
3056 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3058 boolean_t force
= B_FALSE
;
3061 char *poolname
, *old_disk
, *new_disk
;
3062 zpool_handle_t
*zhp
;
3066 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3072 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3081 /* get pool name and check number of arguments */
3083 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3090 (void) fprintf(stderr
,
3091 gettext("missing <device> specification\n"));
3099 (void) fprintf(stderr
,
3100 gettext("missing <new_device> specification\n"));
3103 new_disk
= old_disk
;
3113 (void) fprintf(stderr
, gettext("too many arguments\n"));
3117 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3120 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3121 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3127 nvroot
= make_root_vdev(zhp
, force
, B_FALSE
, replacing
, B_FALSE
,
3129 if (nvroot
== NULL
) {
3134 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3136 nvlist_free(nvroot
);
3143 * zpool replace [-f] <pool> <device> <new_device>
3145 * -f Force attach, even if <new_device> appears to be in use.
3147 * Replace <device> with <new_device>.
3151 zpool_do_replace(int argc
, char **argv
)
3153 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3157 * zpool attach [-f] <pool> <device> <new_device>
3159 * -f Force attach, even if <new_device> appears to be in use.
3161 * Attach <new_device> to the mirror containing <device>. If <device> is not
3162 * part of a mirror, then <device> will be transformed into a mirror of
3163 * <device> and <new_device>. In either case, <new_device> will begin life
3164 * with a DTL of [0, now], and will immediately begin to resilver itself.
3167 zpool_do_attach(int argc
, char **argv
)
3169 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3173 * zpool detach [-f] <pool> <device>
3175 * -f Force detach of <device>, even if DTLs argue against it
3176 * (not supported yet)
3178 * Detach a device from a mirror. The operation will be refused if <device>
3179 * is the last device in the mirror, or if the DTLs indicate that this device
3180 * has the only valid copy of some data.
3184 zpool_do_detach(int argc
, char **argv
)
3187 char *poolname
, *path
;
3188 zpool_handle_t
*zhp
;
3192 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3196 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3205 /* get pool name and check number of arguments */
3207 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3212 (void) fprintf(stderr
,
3213 gettext("missing <device> specification\n"));
3220 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3223 ret
= zpool_vdev_detach(zhp
, path
);
3231 * zpool split [-n] [-o prop=val] ...
3233 * [-R altroot] <pool> <newpool> [<device> ...]
3235 * -n Do not split the pool, but display the resulting layout if
3236 * it were to be split.
3237 * -o Set property=value, or set mount options.
3238 * -R Mount the split-off pool under an alternate root.
3240 * Splits the named pool and gives it the new pool name. Devices to be split
3241 * off may be listed, provided that no more than one device is specified
3242 * per top-level vdev mirror. The newly split pool is left in an exported
3243 * state unless -R is specified.
3245 * Restrictions: the top-level of the pool pool must only be made up of
3246 * mirrors; all devices in the pool must be healthy; no device may be
3247 * undergoing a resilvering operation.
3250 zpool_do_split(int argc
, char **argv
)
3252 char *srcpool
, *newpool
, *propval
;
3253 char *mntopts
= NULL
;
3256 zpool_handle_t
*zhp
;
3257 nvlist_t
*config
, *props
= NULL
;
3259 flags
.dryrun
= B_FALSE
;
3260 flags
.import
= B_FALSE
;
3263 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3266 flags
.import
= B_TRUE
;
3268 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3269 &props
, B_TRUE
) != 0) {
3276 flags
.dryrun
= B_TRUE
;
3279 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3282 if (add_prop_list(optarg
, propval
,
3283 &props
, B_TRUE
) != 0) {
3293 (void) fprintf(stderr
, gettext("missing argument for "
3294 "'%c' option\n"), optopt
);
3298 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3305 if (!flags
.import
&& mntopts
!= NULL
) {
3306 (void) fprintf(stderr
, gettext("setting mntopts is only "
3307 "valid when importing the pool\n"));
3315 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3319 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3329 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3332 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3333 if (config
== NULL
) {
3337 (void) printf(gettext("would create '%s' with the "
3338 "following layout:\n\n"), newpool
);
3339 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3341 nvlist_free(config
);
3346 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3350 * The split was successful. Now we need to open the new
3351 * pool and import it.
3353 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3355 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3356 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3358 (void) fprintf(stderr
, gettext("Split was successful, but "
3359 "the datasets could not all be mounted\n"));
3360 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3361 "different altroot\n"), "zpool import");
3371 * zpool online <pool> <device> ...
3374 zpool_do_online(int argc
, char **argv
)
3378 zpool_handle_t
*zhp
;
3380 vdev_state_t newstate
;
3384 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3387 flags
|= ZFS_ONLINE_EXPAND
;
3391 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3400 /* get pool name and check number of arguments */
3402 (void) fprintf(stderr
, gettext("missing pool name\n"));
3406 (void) fprintf(stderr
, gettext("missing device name\n"));
3412 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3415 for (i
= 1; i
< argc
; i
++) {
3416 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3417 if (newstate
!= VDEV_STATE_HEALTHY
) {
3418 (void) printf(gettext("warning: device '%s' "
3419 "onlined, but remains in faulted state\n"),
3421 if (newstate
== VDEV_STATE_FAULTED
)
3422 (void) printf(gettext("use 'zpool "
3423 "clear' to restore a faulted "
3426 (void) printf(gettext("use 'zpool "
3427 "replace' to replace devices "
3428 "that are no longer present\n"));
3441 * zpool offline [-ft] <pool> <device> ...
3443 * -f Force the device into the offline state, even if doing
3444 * so would appear to compromise pool availability.
3445 * (not supported yet)
3447 * -t Only take the device off-line temporarily. The offline
3448 * state will not be persistent across reboots.
3452 zpool_do_offline(int argc
, char **argv
)
3456 zpool_handle_t
*zhp
;
3458 boolean_t istmp
= B_FALSE
;
3461 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3468 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3477 /* get pool name and check number of arguments */
3479 (void) fprintf(stderr
, gettext("missing pool name\n"));
3483 (void) fprintf(stderr
, gettext("missing device name\n"));
3489 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3492 for (i
= 1; i
< argc
; i
++) {
3493 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3503 * zpool clear <pool> [device]
3505 * Clear all errors associated with a pool or a particular device.
3508 zpool_do_clear(int argc
, char **argv
)
3512 boolean_t dryrun
= B_FALSE
;
3513 boolean_t do_rewind
= B_FALSE
;
3514 boolean_t xtreme_rewind
= B_FALSE
;
3515 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3516 nvlist_t
*policy
= NULL
;
3517 zpool_handle_t
*zhp
;
3518 char *pool
, *device
;
3521 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3530 xtreme_rewind
= B_TRUE
;
3533 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3543 (void) fprintf(stderr
, gettext("missing pool name\n"));
3548 (void) fprintf(stderr
, gettext("too many arguments\n"));
3552 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3553 (void) fprintf(stderr
,
3554 gettext("-n or -X only meaningful with -F\n"));
3558 rewind_policy
= ZPOOL_TRY_REWIND
;
3560 rewind_policy
= ZPOOL_DO_REWIND
;
3562 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3564 /* In future, further rewind policy choices can be passed along here */
3565 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3566 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3570 device
= argc
== 2 ? argv
[1] : NULL
;
3572 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3573 nvlist_free(policy
);
3577 if (zpool_clear(zhp
, device
, policy
) != 0)
3582 nvlist_free(policy
);
3588 * zpool reguid <pool>
3591 zpool_do_reguid(int argc
, char **argv
)
3595 zpool_handle_t
*zhp
;
3599 while ((c
= getopt(argc
, argv
, "")) != -1) {
3602 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3611 /* get pool name and check number of arguments */
3613 (void) fprintf(stderr
, gettext("missing pool name\n"));
3618 (void) fprintf(stderr
, gettext("too many arguments\n"));
3623 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3626 ret
= zpool_reguid(zhp
);
3634 * zpool reopen <pool>
3636 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3639 zpool_do_reopen(int argc
, char **argv
)
3643 zpool_handle_t
*zhp
;
3647 while ((c
= getopt(argc
, argv
, "")) != -1) {
3650 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3660 (void) fprintf(stderr
, gettext("missing pool name\n"));
3665 (void) fprintf(stderr
, gettext("too many arguments\n"));
3670 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3673 ret
= zpool_reopen(zhp
);
3678 typedef struct scrub_cbdata
{
3685 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3687 scrub_cbdata_t
*cb
= data
;
3691 * Ignore faulted pools.
3693 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3694 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3695 "currently unavailable\n"), zpool_get_name(zhp
));
3699 err
= zpool_scan(zhp
, cb
->cb_type
);
3705 * zpool scrub [-s] <pool> ...
3707 * -s Stop. Stops any in-progress scrub.
3710 zpool_do_scrub(int argc
, char **argv
)
3715 cb
.cb_type
= POOL_SCAN_SCRUB
;
3718 while ((c
= getopt(argc
, argv
, "s")) != -1) {
3721 cb
.cb_type
= POOL_SCAN_NONE
;
3724 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3736 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3740 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
3743 typedef struct status_cbdata
{
3745 boolean_t cb_allpools
;
3746 boolean_t cb_verbose
;
3747 boolean_t cb_explain
;
3749 boolean_t cb_dedup_stats
;
3753 * Print out detailed scrub status.
3756 print_scan_status(pool_scan_stat_t
*ps
)
3759 uint64_t elapsed
, mins_left
, hours_left
;
3760 uint64_t pass_exam
, examined
, total
;
3762 double fraction_done
;
3763 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
3765 (void) printf(gettext(" scan: "));
3767 /* If there's never been a scan, there's not much to say. */
3768 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
3769 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
3770 (void) printf(gettext("none requested\n"));
3774 start
= ps
->pss_start_time
;
3775 end
= ps
->pss_end_time
;
3776 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
3778 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
3779 ps
->pss_func
== POOL_SCAN_RESILVER
);
3781 * Scan is finished or canceled.
3783 if (ps
->pss_state
== DSS_FINISHED
) {
3784 uint64_t minutes_taken
= (end
- start
) / 60;
3787 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3788 fmt
= gettext("scrub repaired %s in %lluh%um with "
3789 "%llu errors on %s");
3790 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3791 fmt
= gettext("resilvered %s in %lluh%um with "
3792 "%llu errors on %s");
3795 (void) printf(fmt
, processed_buf
,
3796 (u_longlong_t
)(minutes_taken
/ 60),
3797 (uint_t
)(minutes_taken
% 60),
3798 (u_longlong_t
)ps
->pss_errors
,
3799 ctime((time_t *)&end
));
3801 } else if (ps
->pss_state
== DSS_CANCELED
) {
3802 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3803 (void) printf(gettext("scrub canceled on %s"),
3805 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3806 (void) printf(gettext("resilver canceled on %s"),
3812 assert(ps
->pss_state
== DSS_SCANNING
);
3815 * Scan is in progress.
3817 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3818 (void) printf(gettext("scrub in progress since %s"),
3820 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3821 (void) printf(gettext("resilver in progress since %s"),
3825 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
3826 total
= ps
->pss_to_examine
;
3827 fraction_done
= (double)examined
/ total
;
3829 /* elapsed time for this pass */
3830 elapsed
= time(NULL
) - ps
->pss_pass_start
;
3831 elapsed
= elapsed
? elapsed
: 1;
3832 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
3833 rate
= pass_exam
/ elapsed
;
3834 rate
= rate
? rate
: 1;
3835 mins_left
= ((total
- examined
) / rate
) / 60;
3836 hours_left
= mins_left
/ 60;
3838 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
3839 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
3840 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
3843 * do not print estimated time if hours_left is more than 30 days
3845 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3846 examined_buf
, total_buf
, rate_buf
);
3847 if (hours_left
< (30 * 24)) {
3848 (void) printf(gettext(", %lluh%um to go\n"),
3849 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
3851 (void) printf(gettext(
3852 ", (scan is slow, no estimated time)\n"));
3855 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3856 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3857 processed_buf
, 100 * fraction_done
);
3858 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3859 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3860 processed_buf
, 100 * fraction_done
);
3865 print_error_log(zpool_handle_t
*zhp
)
3867 nvlist_t
*nverrlist
= NULL
;
3870 size_t len
= MAXPATHLEN
* 2;
3872 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
3873 (void) printf("errors: List of errors unavailable "
3874 "(insufficient privileges)\n");
3878 (void) printf("errors: Permanent errors have been "
3879 "detected in the following files:\n\n");
3881 pathname
= safe_malloc(len
);
3883 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
3885 uint64_t dsobj
, obj
;
3887 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
3888 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
3890 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
3892 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
3893 (void) printf("%7s %s\n", "", pathname
);
3896 nvlist_free(nverrlist
);
3900 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
3909 (void) printf(gettext("\tspares\n"));
3911 for (i
= 0; i
< nspares
; i
++) {
3912 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
3913 print_status_config(zhp
, name
, spares
[i
],
3914 namewidth
, 2, B_TRUE
);
3920 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
3929 (void) printf(gettext("\tcache\n"));
3931 for (i
= 0; i
< nl2cache
; i
++) {
3932 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
3933 print_status_config(zhp
, name
, l2cache
[i
],
3934 namewidth
, 2, B_FALSE
);
3940 print_dedup_stats(nvlist_t
*config
)
3942 ddt_histogram_t
*ddh
;
3948 * If the pool was faulted then we may not have been able to
3949 * obtain the config. Otherwise, if we have anything in the dedup
3950 * table continue processing the stats.
3952 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
3953 (uint64_t **)&ddo
, &c
) != 0)
3956 (void) printf("\n");
3957 (void) printf(gettext(" dedup: "));
3958 if (ddo
->ddo_count
== 0) {
3959 (void) printf(gettext("no DDT entries\n"));
3963 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3964 (u_longlong_t
)ddo
->ddo_count
,
3965 (u_longlong_t
)ddo
->ddo_dspace
,
3966 (u_longlong_t
)ddo
->ddo_mspace
);
3968 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
3969 (uint64_t **)&dds
, &c
) == 0);
3970 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
3971 (uint64_t **)&ddh
, &c
) == 0);
3972 zpool_dump_ddt(dds
, ddh
);
3976 * Display a summary of pool status. Displays a summary such as:
3980 * reason: One or more devices ...
3981 * see: http://illumos.org/msg/ZFS-xxxx-01
3987 * When given the '-v' option, we print out the complete config. If the '-e'
3988 * option is specified, then we print out error rate information as well.
3991 status_callback(zpool_handle_t
*zhp
, void *data
)
3993 status_cbdata_t
*cbp
= data
;
3994 nvlist_t
*config
, *nvroot
;
4001 config
= zpool_get_config(zhp
, NULL
);
4002 reason
= zpool_get_status(zhp
, &msgid
);
4007 * If we were given 'zpool status -x', only report those pools with
4010 if (cbp
->cb_explain
&&
4011 (reason
== ZPOOL_STATUS_OK
||
4012 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4013 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4014 if (!cbp
->cb_allpools
) {
4015 (void) printf(gettext("pool '%s' is healthy\n"),
4016 zpool_get_name(zhp
));
4018 cbp
->cb_first
= B_FALSE
;
4024 cbp
->cb_first
= B_FALSE
;
4026 (void) printf("\n");
4028 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4030 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4031 (uint64_t **)&vs
, &c
) == 0);
4032 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4034 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4035 (void) printf(gettext(" state: %s\n"), health
);
4038 case ZPOOL_STATUS_MISSING_DEV_R
:
4039 (void) printf(gettext("status: One or more devices could not "
4040 "be opened. Sufficient replicas exist for\n\tthe pool to "
4041 "continue functioning in a degraded state.\n"));
4042 (void) printf(gettext("action: Attach the missing device and "
4043 "online it using 'zpool online'.\n"));
4046 case ZPOOL_STATUS_MISSING_DEV_NR
:
4047 (void) printf(gettext("status: One or more devices could not "
4048 "be opened. There are insufficient\n\treplicas for the "
4049 "pool to continue functioning.\n"));
4050 (void) printf(gettext("action: Attach the missing device and "
4051 "online it using 'zpool online'.\n"));
4054 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4055 (void) printf(gettext("status: One or more devices could not "
4056 "be used because the label is missing or\n\tinvalid. "
4057 "Sufficient replicas exist for the pool to continue\n\t"
4058 "functioning in a degraded state.\n"));
4059 (void) printf(gettext("action: Replace the device using "
4060 "'zpool replace'.\n"));
4063 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4064 (void) printf(gettext("status: One or more devices could not "
4065 "be used because the label is missing \n\tor invalid. "
4066 "There are insufficient replicas for the pool to "
4067 "continue\n\tfunctioning.\n"));
4068 zpool_explain_recover(zpool_get_handle(zhp
),
4069 zpool_get_name(zhp
), reason
, config
);
4072 case ZPOOL_STATUS_FAILING_DEV
:
4073 (void) printf(gettext("status: One or more devices has "
4074 "experienced an unrecoverable error. An\n\tattempt was "
4075 "made to correct the error. Applications are "
4077 (void) printf(gettext("action: Determine if the device needs "
4078 "to be replaced, and clear the errors\n\tusing "
4079 "'zpool clear' or replace the device with 'zpool "
4083 case ZPOOL_STATUS_OFFLINE_DEV
:
4084 (void) printf(gettext("status: One or more devices has "
4085 "been taken offline by the administrator.\n\tSufficient "
4086 "replicas exist for the pool to continue functioning in "
4087 "a\n\tdegraded state.\n"));
4088 (void) printf(gettext("action: Online the device using "
4089 "'zpool online' or replace the device with\n\t'zpool "
4093 case ZPOOL_STATUS_REMOVED_DEV
:
4094 (void) printf(gettext("status: One or more devices has "
4095 "been removed by the administrator.\n\tSufficient "
4096 "replicas exist for the pool to continue functioning in "
4097 "a\n\tdegraded state.\n"));
4098 (void) printf(gettext("action: Online the device using "
4099 "'zpool online' or replace the device with\n\t'zpool "
4103 case ZPOOL_STATUS_RESILVERING
:
4104 (void) printf(gettext("status: One or more devices is "
4105 "currently being resilvered. The pool will\n\tcontinue "
4106 "to function, possibly in a degraded state.\n"));
4107 (void) printf(gettext("action: Wait for the resilver to "
4111 case ZPOOL_STATUS_CORRUPT_DATA
:
4112 (void) printf(gettext("status: One or more devices has "
4113 "experienced an error resulting in data\n\tcorruption. "
4114 "Applications may be affected.\n"));
4115 (void) printf(gettext("action: Restore the file in question "
4116 "if possible. Otherwise restore the\n\tentire pool from "
4120 case ZPOOL_STATUS_CORRUPT_POOL
:
4121 (void) printf(gettext("status: The pool metadata is corrupted "
4122 "and the pool cannot be opened.\n"));
4123 zpool_explain_recover(zpool_get_handle(zhp
),
4124 zpool_get_name(zhp
), reason
, config
);
4127 case ZPOOL_STATUS_VERSION_OLDER
:
4128 (void) printf(gettext("status: The pool is formatted using a "
4129 "legacy on-disk format. The pool can\n\tstill be used, "
4130 "but some features are unavailable.\n"));
4131 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4132 "upgrade'. Once this is done, the\n\tpool will no longer "
4133 "be accessible on software that does not support feature\n"
4137 case ZPOOL_STATUS_VERSION_NEWER
:
4138 (void) printf(gettext("status: The pool has been upgraded to a "
4139 "newer, incompatible on-disk version.\n\tThe pool cannot "
4140 "be accessed on this system.\n"));
4141 (void) printf(gettext("action: Access the pool from a system "
4142 "running more recent software, or\n\trestore the pool from "
4146 case ZPOOL_STATUS_FEAT_DISABLED
:
4147 (void) printf(gettext("status: Some supported features are not "
4148 "enabled on the pool. The pool can\n\tstill be used, but "
4149 "some features are unavailable.\n"));
4150 (void) printf(gettext("action: Enable all features using "
4151 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4152 "longer be accessible by software that does not support\n\t"
4153 "the features. See zpool-features(5) for details.\n"));
4156 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4157 (void) printf(gettext("status: The pool cannot be accessed on "
4158 "this system because it uses the\n\tfollowing feature(s) "
4159 "not supported on this system:\n"));
4160 zpool_print_unsup_feat(config
);
4161 (void) printf("\n");
4162 (void) printf(gettext("action: Access the pool from a system "
4163 "that supports the required feature(s),\n\tor restore the "
4164 "pool from backup.\n"));
4167 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4168 (void) printf(gettext("status: The pool can only be accessed "
4169 "in read-only mode on this system. It\n\tcannot be "
4170 "accessed in read-write mode because it uses the "
4171 "following\n\tfeature(s) not supported on this system:\n"));
4172 zpool_print_unsup_feat(config
);
4173 (void) printf("\n");
4174 (void) printf(gettext("action: The pool cannot be accessed in "
4175 "read-write mode. Import the pool with\n"
4176 "\t\"-o readonly=on\", access the pool from a system that "
4177 "supports the\n\trequired feature(s), or restore the "
4178 "pool from backup.\n"));
4181 case ZPOOL_STATUS_FAULTED_DEV_R
:
4182 (void) printf(gettext("status: One or more devices are "
4183 "faulted in response to persistent errors.\n\tSufficient "
4184 "replicas exist for the pool to continue functioning "
4185 "in a\n\tdegraded state.\n"));
4186 (void) printf(gettext("action: Replace the faulted device, "
4187 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4190 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4191 (void) printf(gettext("status: One or more devices are "
4192 "faulted in response to persistent errors. There are "
4193 "insufficient replicas for the pool to\n\tcontinue "
4195 (void) printf(gettext("action: Destroy and re-create the pool "
4196 "from a backup source. Manually marking the device\n"
4197 "\trepaired using 'zpool clear' may allow some data "
4198 "to be recovered.\n"));
4201 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4202 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4203 (void) printf(gettext("status: One or more devices are "
4204 "faulted in response to IO failures.\n"));
4205 (void) printf(gettext("action: Make sure the affected devices "
4206 "are connected, then run 'zpool clear'.\n"));
4209 case ZPOOL_STATUS_BAD_LOG
:
4210 (void) printf(gettext("status: An intent log record "
4211 "could not be read.\n"
4212 "\tWaiting for adminstrator intervention to fix the "
4213 "faulted pool.\n"));
4214 (void) printf(gettext("action: Either restore the affected "
4215 "device(s) and run 'zpool online',\n"
4216 "\tor ignore the intent log records by running "
4217 "'zpool clear'.\n"));
4222 * The remaining errors can't actually be generated, yet.
4224 assert(reason
== ZPOOL_STATUS_OK
);
4228 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4231 if (config
!= NULL
) {
4234 nvlist_t
**spares
, **l2cache
;
4235 uint_t nspares
, nl2cache
;
4236 pool_scan_stat_t
*ps
= NULL
;
4238 (void) nvlist_lookup_uint64_array(nvroot
,
4239 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4240 print_scan_status(ps
);
4242 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4246 (void) printf(gettext("config:\n\n"));
4247 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4248 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4249 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4250 namewidth
, 0, B_FALSE
);
4252 if (num_logs(nvroot
) > 0)
4253 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4254 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4255 &l2cache
, &nl2cache
) == 0)
4256 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4258 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4259 &spares
, &nspares
) == 0)
4260 print_spares(zhp
, spares
, nspares
, namewidth
);
4262 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4264 nvlist_t
*nverrlist
= NULL
;
4267 * If the approximate error count is small, get a
4268 * precise count by fetching the entire log and
4269 * uniquifying the results.
4271 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4272 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4277 while ((elem
= nvlist_next_nvpair(nverrlist
,
4282 nvlist_free(nverrlist
);
4284 (void) printf("\n");
4287 (void) printf(gettext("errors: No known data "
4289 else if (!cbp
->cb_verbose
)
4290 (void) printf(gettext("errors: %llu data "
4291 "errors, use '-v' for a list\n"),
4292 (u_longlong_t
)nerr
);
4294 print_error_log(zhp
);
4297 if (cbp
->cb_dedup_stats
)
4298 print_dedup_stats(config
);
4300 (void) printf(gettext("config: The configuration cannot be "
4308 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4310 * -v Display complete error logs
4311 * -x Display only pools with potential problems
4312 * -D Display dedup status (undocumented)
4313 * -T Display a timestamp in date(1) or Unix format
4315 * Describes the health status of all pools or some subset.
4318 zpool_do_status(int argc
, char **argv
)
4322 unsigned long interval
= 0, count
= 0;
4323 status_cbdata_t cb
= { 0 };
4326 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4329 cb
.cb_verbose
= B_TRUE
;
4332 cb
.cb_explain
= B_TRUE
;
4335 cb
.cb_dedup_stats
= B_TRUE
;
4338 get_timestamp_arg(*optarg
);
4341 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4350 get_interval_count(&argc
, argv
, &interval
, &count
);
4353 cb
.cb_allpools
= B_TRUE
;
4355 cb
.cb_first
= B_TRUE
;
4358 if (timestamp_fmt
!= NODATE
)
4359 print_timestamp(timestamp_fmt
);
4361 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4362 status_callback
, &cb
);
4364 if (argc
== 0 && cb
.cb_count
== 0)
4365 (void) printf(gettext("no pools available\n"));
4366 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4367 (void) printf(gettext("all pools are healthy\n"));
4375 if (count
!= 0 && --count
== 0)
4378 (void) sleep(interval
);
4384 typedef struct upgrade_cbdata
{
4387 uint64_t cb_version
;
4392 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4396 uint64_t oldversion
;
4398 config
= zpool_get_config(zhp
, NULL
);
4399 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4402 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4403 assert(oldversion
< version
);
4405 ret
= zpool_upgrade(zhp
, version
);
4409 if (version
>= SPA_VERSION_FEATURES
) {
4410 (void) printf(gettext("Successfully upgraded "
4411 "'%s' from version %llu to feature flags.\n"),
4412 zpool_get_name(zhp
), oldversion
);
4414 (void) printf(gettext("Successfully upgraded "
4415 "'%s' from version %llu to version %llu.\n"),
4416 zpool_get_name(zhp
), oldversion
, version
);
4423 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4426 boolean_t firstff
= B_TRUE
;
4427 nvlist_t
*enabled
= zpool_get_features(zhp
);
4430 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4431 const char *fname
= spa_feature_table
[i
].fi_uname
;
4432 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4433 if (!nvlist_exists(enabled
, fguid
)) {
4435 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4436 ret
= zpool_set_prop(zhp
, propname
,
4437 ZFS_FEATURE_ENABLED
);
4445 (void) printf(gettext("Enabled the "
4446 "following features on '%s':\n"),
4447 zpool_get_name(zhp
));
4450 (void) printf(gettext(" %s\n"), fname
);
4461 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4463 upgrade_cbdata_t
*cbp
= arg
;
4466 boolean_t printnl
= B_FALSE
;
4469 config
= zpool_get_config(zhp
, NULL
);
4470 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4473 assert(SPA_VERSION_IS_SUPPORTED(version
));
4475 if (version
< cbp
->cb_version
) {
4476 cbp
->cb_first
= B_FALSE
;
4477 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4483 * If they did "zpool upgrade -a", then we could
4484 * be doing ioctls to different pools. We need
4485 * to log this history once to each pool, and bypass
4486 * the normal history logging that happens in main().
4488 (void) zpool_log_history(g_zfs
, history_str
);
4489 log_history
= B_FALSE
;
4492 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4494 ret
= upgrade_enable_all(zhp
, &count
);
4499 cbp
->cb_first
= B_FALSE
;
4505 (void) printf(gettext("\n"));
4512 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4514 upgrade_cbdata_t
*cbp
= arg
;
4518 config
= zpool_get_config(zhp
, NULL
);
4519 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4522 assert(SPA_VERSION_IS_SUPPORTED(version
));
4524 if (version
< SPA_VERSION_FEATURES
) {
4525 if (cbp
->cb_first
) {
4526 (void) printf(gettext("The following pools are "
4527 "formatted with legacy version numbers and can\n"
4528 "be upgraded to use feature flags. After "
4529 "being upgraded, these pools\nwill no "
4530 "longer be accessible by software that does not "
4531 "support feature\nflags.\n\n"));
4532 (void) printf(gettext("VER POOL\n"));
4533 (void) printf(gettext("--- ------------\n"));
4534 cbp
->cb_first
= B_FALSE
;
4537 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4538 zpool_get_name(zhp
));
4545 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4547 upgrade_cbdata_t
*cbp
= arg
;
4551 config
= zpool_get_config(zhp
, NULL
);
4552 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4555 if (version
>= SPA_VERSION_FEATURES
) {
4557 boolean_t poolfirst
= B_TRUE
;
4558 nvlist_t
*enabled
= zpool_get_features(zhp
);
4560 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4561 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4562 const char *fname
= spa_feature_table
[i
].fi_uname
;
4563 if (!nvlist_exists(enabled
, fguid
)) {
4564 if (cbp
->cb_first
) {
4565 (void) printf(gettext("\nSome "
4566 "supported features are not "
4567 "enabled on the following pools. "
4568 "Once a\nfeature is enabled the "
4569 "pool may become incompatible with "
4570 "software\nthat does not support "
4572 "zpool-features(5) for "
4574 (void) printf(gettext("POOL "
4576 (void) printf(gettext("------"
4578 cbp
->cb_first
= B_FALSE
;
4582 (void) printf(gettext("%s\n"),
4583 zpool_get_name(zhp
));
4584 poolfirst
= B_FALSE
;
4587 (void) printf(gettext(" %s\n"), fname
);
4597 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4599 boolean_t printnl
= B_FALSE
;
4600 upgrade_cbdata_t
*cbp
= data
;
4601 uint64_t cur_version
;
4604 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4605 (void) printf(gettext("'log' is now a reserved word\n"
4606 "Pool 'log' must be renamed using export and import"
4611 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4612 if (cur_version
> cbp
->cb_version
) {
4613 (void) printf(gettext("Pool '%s' is already formatted "
4614 "using more current version '%llu'.\n\n"),
4615 zpool_get_name(zhp
), cur_version
);
4619 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
4620 (void) printf(gettext("Pool '%s' is already formatted "
4621 "using version %llu.\n\n"), zpool_get_name(zhp
),
4626 if (cur_version
!= cbp
->cb_version
) {
4628 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4633 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4635 ret
= upgrade_enable_all(zhp
, &count
);
4641 } else if (cur_version
== SPA_VERSION
) {
4642 (void) printf(gettext("Pool '%s' already has all "
4643 "supported features enabled.\n"),
4644 zpool_get_name(zhp
));
4649 (void) printf(gettext("\n"));
4658 * zpool upgrade [-V version] <-a | pool ...>
4660 * With no arguments, display downrev'd ZFS pool available for upgrade.
4661 * Individual pools can be upgraded by specifying the pool, and '-a' will
4662 * upgrade all pools.
4665 zpool_do_upgrade(int argc
, char **argv
)
4668 upgrade_cbdata_t cb
= { 0 };
4670 boolean_t showversions
= B_FALSE
;
4671 boolean_t upgradeall
= B_FALSE
;
4676 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
4679 upgradeall
= B_TRUE
;
4682 showversions
= B_TRUE
;
4685 cb
.cb_version
= strtoll(optarg
, &end
, 10);
4687 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
4688 (void) fprintf(stderr
,
4689 gettext("invalid version '%s'\n"), optarg
);
4694 (void) fprintf(stderr
, gettext("missing argument for "
4695 "'%c' option\n"), optopt
);
4699 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4710 if (cb
.cb_version
== 0) {
4711 cb
.cb_version
= SPA_VERSION
;
4712 } else if (!upgradeall
&& argc
== 0) {
4713 (void) fprintf(stderr
, gettext("-V option is "
4714 "incompatible with other arguments\n"));
4719 if (upgradeall
|| argc
!= 0) {
4720 (void) fprintf(stderr
, gettext("-v option is "
4721 "incompatible with other arguments\n"));
4724 } else if (upgradeall
) {
4726 (void) fprintf(stderr
, gettext("-a option should not "
4727 "be used along with a pool name\n"));
4732 (void) printf(gettext("This system supports ZFS pool feature "
4737 (void) printf(gettext("The following features are "
4739 (void) printf(gettext("FEAT DESCRIPTION\n"));
4740 (void) printf("----------------------------------------------"
4741 "---------------\n");
4742 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4743 zfeature_info_t
*fi
= &spa_feature_table
[i
];
4745 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
4746 " (read-only compatible)" : "";
4748 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
4749 (void) printf(" %s\n", fi
->fi_desc
);
4751 (void) printf("\n");
4753 (void) printf(gettext("The following legacy versions are also "
4755 (void) printf(gettext("VER DESCRIPTION\n"));
4756 (void) printf("--- -----------------------------------------"
4757 "---------------\n");
4758 (void) printf(gettext(" 1 Initial ZFS version\n"));
4759 (void) printf(gettext(" 2 Ditto blocks "
4760 "(replicated metadata)\n"));
4761 (void) printf(gettext(" 3 Hot spares and double parity "
4763 (void) printf(gettext(" 4 zpool history\n"));
4764 (void) printf(gettext(" 5 Compression using the gzip "
4766 (void) printf(gettext(" 6 bootfs pool property\n"));
4767 (void) printf(gettext(" 7 Separate intent log devices\n"));
4768 (void) printf(gettext(" 8 Delegated administration\n"));
4769 (void) printf(gettext(" 9 refquota and refreservation "
4771 (void) printf(gettext(" 10 Cache devices\n"));
4772 (void) printf(gettext(" 11 Improved scrub performance\n"));
4773 (void) printf(gettext(" 12 Snapshot properties\n"));
4774 (void) printf(gettext(" 13 snapused property\n"));
4775 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4776 (void) printf(gettext(" 15 user/group space accounting\n"));
4777 (void) printf(gettext(" 16 stmf property support\n"));
4778 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4779 (void) printf(gettext(" 18 Snapshot user holds\n"));
4780 (void) printf(gettext(" 19 Log device removal\n"));
4781 (void) printf(gettext(" 20 Compression using zle "
4782 "(zero-length encoding)\n"));
4783 (void) printf(gettext(" 21 Deduplication\n"));
4784 (void) printf(gettext(" 22 Received properties\n"));
4785 (void) printf(gettext(" 23 Slim ZIL\n"));
4786 (void) printf(gettext(" 24 System attributes\n"));
4787 (void) printf(gettext(" 25 Improved scrub stats\n"));
4788 (void) printf(gettext(" 26 Improved snapshot deletion "
4790 (void) printf(gettext(" 27 Improved snapshot creation "
4792 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4793 (void) printf(gettext("\nFor more information on a particular "
4794 "version, including supported releases,\n"));
4795 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4796 } else if (argc
== 0 && upgradeall
) {
4797 cb
.cb_first
= B_TRUE
;
4798 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
4799 if (ret
== 0 && cb
.cb_first
) {
4800 if (cb
.cb_version
== SPA_VERSION
) {
4801 (void) printf(gettext("All pools are already "
4802 "formatted using feature flags.\n\n"));
4803 (void) printf(gettext("Every feature flags "
4804 "pool already has all supported features "
4807 (void) printf(gettext("All pools are already "
4808 "formatted with version %llu or higher.\n"),
4812 } else if (argc
== 0) {
4813 cb
.cb_first
= B_TRUE
;
4814 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
4818 (void) printf(gettext("All pools are formatted "
4819 "using feature flags.\n\n"));
4821 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4822 "for a list of available legacy versions.\n"));
4825 cb
.cb_first
= B_TRUE
;
4826 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
4830 (void) printf(gettext("Every feature flags pool has "
4831 "all supported features enabled.\n"));
4833 (void) printf(gettext("\n"));
4836 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
4843 typedef struct hist_cbdata
{
4850 * Print out the command history for a specific pool.
4853 get_history_one(zpool_handle_t
*zhp
, void *data
)
4859 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
4861 cb
->first
= B_FALSE
;
4863 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
4865 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
4868 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
4869 &records
, &numrecords
) == 0);
4870 for (i
= 0; i
< numrecords
; i
++) {
4871 nvlist_t
*rec
= records
[i
];
4874 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
4878 tsec
= fnvlist_lookup_uint64(records
[i
],
4880 (void) localtime_r(&tsec
, &t
);
4881 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
4884 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
4885 (void) printf("%s %s", tbuf
,
4886 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
4887 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
4889 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
4892 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
4893 (void) printf("%s unrecognized record:\n",
4895 dump_nvlist(rec
, 4);
4898 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
4899 zfs_history_event_names
[ievent
],
4900 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
4901 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
4902 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
4905 (void) printf("%s [txg:%lld] %s", tbuf
,
4906 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
4907 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
4908 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
4909 (void) printf(" %s (%llu)",
4910 fnvlist_lookup_string(rec
,
4912 fnvlist_lookup_uint64(rec
,
4915 (void) printf(" %s", fnvlist_lookup_string(rec
,
4916 ZPOOL_HIST_INT_STR
));
4917 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
4920 (void) printf("%s ioctl %s\n", tbuf
,
4921 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
4922 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
4923 (void) printf(" input:\n");
4924 dump_nvlist(fnvlist_lookup_nvlist(rec
,
4925 ZPOOL_HIST_INPUT_NVL
), 8);
4927 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
4928 (void) printf(" output:\n");
4929 dump_nvlist(fnvlist_lookup_nvlist(rec
,
4930 ZPOOL_HIST_OUTPUT_NVL
), 8);
4935 (void) printf("%s unrecognized record:\n", tbuf
);
4936 dump_nvlist(rec
, 4);
4940 (void) printf("\n");
4943 (void) printf(" [");
4944 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
4945 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
4946 struct passwd
*pwd
= getpwuid(who
);
4947 (void) printf("user %d ", (int)who
);
4949 (void) printf("(%s) ", pwd
->pw_name
);
4951 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
4952 (void) printf("on %s",
4953 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
4955 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
4956 (void) printf(":%s",
4957 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
4960 (void) printf("\n");
4962 (void) printf("\n");
4969 * zpool history <pool>
4971 * Displays the history of commands that modified pools.
4974 zpool_do_history(int argc
, char **argv
)
4976 hist_cbdata_t cbdata
= { 0 };
4980 cbdata
.first
= B_TRUE
;
4982 while ((c
= getopt(argc
, argv
, "li")) != -1) {
4985 cbdata
.longfmt
= B_TRUE
;
4988 cbdata
.internal
= B_TRUE
;
4991 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4999 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5002 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5003 (void) printf(gettext("no pools available\n"));
5011 get_callback(zpool_handle_t
*zhp
, void *data
)
5013 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5014 char value
[MAXNAMELEN
];
5015 zprop_source_t srctype
;
5018 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5021 * Skip the special fake placeholder. This will also skip
5022 * over the name property when 'all' is specified.
5024 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5025 pl
== cbp
->cb_proplist
)
5028 if (pl
->pl_prop
== ZPROP_INVAL
&&
5029 (zpool_prop_feature(pl
->pl_user_prop
) ||
5030 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5031 srctype
= ZPROP_SRC_LOCAL
;
5033 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5034 value
, sizeof (value
)) == 0) {
5035 zprop_print_one_property(zpool_get_name(zhp
),
5036 cbp
, pl
->pl_user_prop
, value
, srctype
,
5040 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
5041 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5044 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5045 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5053 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5055 * -H Scripted mode. Don't display headers, and separate properties
5057 * -o List of columns to display. Defaults to
5058 * "name,property,value,source".
5059 * -p Diplay values in parsable (exact) format.
5061 * Get properties of pools in the system. Output space statistics
5062 * for each one as well as other attributes.
5065 zpool_do_get(int argc
, char **argv
)
5067 zprop_get_cbdata_t cb
= { 0 };
5068 zprop_list_t fake_name
= { 0 };
5073 cb
.cb_first
= B_TRUE
;
5076 * Set up default columns and sources.
5078 cb
.cb_sources
= ZPROP_SRC_ALL
;
5079 cb
.cb_columns
[0] = GET_COL_NAME
;
5080 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5081 cb
.cb_columns
[2] = GET_COL_VALUE
;
5082 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5083 cb
.cb_type
= ZFS_TYPE_POOL
;
5086 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
5089 cb
.cb_literal
= B_TRUE
;
5092 cb
.cb_scripted
= B_TRUE
;
5095 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
5097 while (*optarg
!= '\0') {
5098 static char *col_subopts
[] =
5099 { "name", "property", "value", "source",
5102 if (i
== ZFS_GET_NCOLS
) {
5103 (void) fprintf(stderr
, gettext("too "
5104 "many fields given to -o "
5109 switch (getsubopt(&optarg
, col_subopts
,
5112 cb
.cb_columns
[i
++] = GET_COL_NAME
;
5115 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
5118 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
5121 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
5125 (void) fprintf(stderr
,
5126 gettext("\"all\" conflicts "
5127 "with specific fields "
5128 "given to -o option\n"));
5131 cb
.cb_columns
[0] = GET_COL_NAME
;
5132 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5133 cb
.cb_columns
[2] = GET_COL_VALUE
;
5134 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5138 (void) fprintf(stderr
,
5139 gettext("invalid column name "
5146 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5156 (void) fprintf(stderr
, gettext("missing property "
5161 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
5162 ZFS_TYPE_POOL
) != 0)
5168 if (cb
.cb_proplist
!= NULL
) {
5169 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5170 fake_name
.pl_width
= strlen(gettext("NAME"));
5171 fake_name
.pl_next
= cb
.cb_proplist
;
5172 cb
.cb_proplist
= &fake_name
;
5175 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5178 if (cb
.cb_proplist
== &fake_name
)
5179 zprop_free_list(fake_name
.pl_next
);
5181 zprop_free_list(cb
.cb_proplist
);
5186 typedef struct set_cbdata
{
5189 boolean_t cb_any_successful
;
5193 set_callback(zpool_handle_t
*zhp
, void *data
)
5196 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5198 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5201 cb
->cb_any_successful
= B_TRUE
;
5207 zpool_do_set(int argc
, char **argv
)
5209 set_cbdata_t cb
= { 0 };
5212 if (argc
> 1 && argv
[1][0] == '-') {
5213 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5219 (void) fprintf(stderr
, gettext("missing property=value "
5225 (void) fprintf(stderr
, gettext("missing pool name\n"));
5230 (void) fprintf(stderr
, gettext("too many pool names\n"));
5234 cb
.cb_propname
= argv
[1];
5235 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5236 if (cb
.cb_value
== NULL
) {
5237 (void) fprintf(stderr
, gettext("missing value in "
5238 "property=value argument\n"));
5242 *(cb
.cb_value
) = '\0';
5245 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5252 find_command_idx(char *command
, int *idx
)
5256 for (i
= 0; i
< NCOMMAND
; i
++) {
5257 if (command_table
[i
].name
== NULL
)
5260 if (strcmp(command
, command_table
[i
].name
) == 0) {
5269 main(int argc
, char **argv
)
5275 (void) setlocale(LC_ALL
, "");
5276 (void) textdomain(TEXT_DOMAIN
);
5278 if ((g_zfs
= libzfs_init()) == NULL
) {
5279 (void) fprintf(stderr
, gettext("internal error: failed to "
5280 "initialize ZFS library\n"));
5284 libzfs_print_on_error(g_zfs
, B_TRUE
);
5289 * Make sure the user has specified some command.
5292 (void) fprintf(stderr
, gettext("missing command\n"));
5301 if (strcmp(cmdname
, "-?") == 0)
5304 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5307 * Run the appropriate command.
5309 if (find_command_idx(cmdname
, &i
) == 0) {
5310 current_command
= &command_table
[i
];
5311 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5312 } else if (strchr(cmdname
, '=')) {
5313 verify(find_command_idx("set", &i
) == 0);
5314 current_command
= &command_table
[i
];
5315 ret
= command_table
[i
].func(argc
, argv
);
5316 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5318 * 'freeze' is a vile debugging abomination, so we treat
5322 int fd
= open(ZFS_DEV
, O_RDWR
);
5323 (void) strcpy((void *)buf
, argv
[2]);
5324 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5326 (void) fprintf(stderr
, gettext("unrecognized "
5327 "command '%s'\n"), cmdname
);
5331 if (ret
== 0 && log_history
)
5332 (void) zpool_log_history(g_zfs
, history_str
);
5337 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5338 * for the purposes of running ::findleaks.
5340 if (getenv("ZFS_ABORT") != NULL
) {
5341 (void) printf("dumping core by request\n");