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, 2014 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
49 #include <sys/fs/zfs.h>
51 #include <sys/fm/util.h>
52 #include <sys/fm/protocol.h>
53 #include <sys/zfs_ioctl.h>
57 #include "zpool_util.h"
58 #include "zfs_comutil.h"
59 #include "zfeature_common.h"
61 #include "statcommon.h"
63 static int zpool_do_create(int, char **);
64 static int zpool_do_destroy(int, char **);
66 static int zpool_do_add(int, char **);
67 static int zpool_do_remove(int, char **);
68 static int zpool_do_labelclear(int, char **);
70 static int zpool_do_list(int, char **);
71 static int zpool_do_iostat(int, char **);
72 static int zpool_do_status(int, char **);
74 static int zpool_do_online(int, char **);
75 static int zpool_do_offline(int, char **);
76 static int zpool_do_clear(int, char **);
77 static int zpool_do_reopen(int, char **);
79 static int zpool_do_reguid(int, char **);
81 static int zpool_do_attach(int, char **);
82 static int zpool_do_detach(int, char **);
83 static int zpool_do_replace(int, char **);
84 static int zpool_do_split(int, char **);
86 static int zpool_do_scrub(int, char **);
88 static int zpool_do_import(int, char **);
89 static int zpool_do_export(int, char **);
91 static int zpool_do_upgrade(int, char **);
93 static int zpool_do_history(int, char **);
94 static int zpool_do_events(int, char **);
96 static int zpool_do_get(int, char **);
97 static int zpool_do_set(int, char **);
100 * These libumem hooks provide a reasonable set of defaults for the allocator's
101 * debugging facilities.
106 _umem_debug_init(void)
108 return ("default,verbose"); /* $UMEM_DEBUG setting */
112 _umem_logging_init(void)
114 return ("fail,contents"); /* $UMEM_LOGGING setting */
147 typedef struct zpool_command
{
149 int (*func
)(int, char **);
154 * Master command table. Each ZFS command has a name, associated function, and
155 * usage message. The usage messages need to be internationalized, so we have
156 * to have a function to return the usage message based on a command index.
158 * These commands are organized according to how they are displayed in the usage
159 * message. An empty command (one with a NULL name) indicates an empty line in
160 * the generic usage message.
162 static zpool_command_t command_table
[] = {
163 { "create", zpool_do_create
, HELP_CREATE
},
164 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
166 { "add", zpool_do_add
, HELP_ADD
},
167 { "remove", zpool_do_remove
, HELP_REMOVE
},
169 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
171 { "list", zpool_do_list
, HELP_LIST
},
172 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
173 { "status", zpool_do_status
, HELP_STATUS
},
175 { "online", zpool_do_online
, HELP_ONLINE
},
176 { "offline", zpool_do_offline
, HELP_OFFLINE
},
177 { "clear", zpool_do_clear
, HELP_CLEAR
},
178 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
180 { "attach", zpool_do_attach
, HELP_ATTACH
},
181 { "detach", zpool_do_detach
, HELP_DETACH
},
182 { "replace", zpool_do_replace
, HELP_REPLACE
},
183 { "split", zpool_do_split
, HELP_SPLIT
},
185 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
187 { "import", zpool_do_import
, HELP_IMPORT
},
188 { "export", zpool_do_export
, HELP_EXPORT
},
189 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
190 { "reguid", zpool_do_reguid
, HELP_REGUID
},
192 { "history", zpool_do_history
, HELP_HISTORY
},
193 { "events", zpool_do_events
, HELP_EVENTS
},
195 { "get", zpool_do_get
, HELP_GET
},
196 { "set", zpool_do_set
, HELP_SET
},
199 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
201 static zpool_command_t
*current_command
;
202 static char history_str
[HIS_MAX_RECORD_LEN
];
203 static boolean_t log_history
= B_TRUE
;
204 static uint_t timestamp_fmt
= NODATE
;
207 get_usage(zpool_help_t idx
) {
210 return (gettext("\tadd [-fn] [-o property=value] "
211 "<pool> <vdev> ...\n"));
213 return (gettext("\tattach [-f] [-o property=value] "
214 "<pool> <device> <new-device>\n"));
216 return (gettext("\tclear [-nF] <pool> [device]\n"));
218 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
219 "\t [-O file-system-property=value] ... \n"
220 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
222 return (gettext("\tdestroy [-f] <pool>\n"));
224 return (gettext("\tdetach <pool> <device>\n"));
226 return (gettext("\texport [-af] <pool> ...\n"));
228 return (gettext("\thistory [-il] [<pool>] ...\n"));
230 return (gettext("\timport [-d dir] [-D]\n"
231 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
232 "\timport [-o mntopts] [-o property=value] ... \n"
233 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
234 "[-R root] [-F [-n]] -a\n"
235 "\timport [-o mntopts] [-o property=value] ... \n"
236 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
237 "[-R root] [-F [-n]]\n"
238 "\t <pool | id> [newpool]\n"));
240 return (gettext("\tiostat [-v] [-T d|u] [-y] [pool] ... "
241 "[interval [count]]\n"));
242 case HELP_LABELCLEAR
:
243 return (gettext("\tlabelclear [-f] <vdev>\n"));
245 return (gettext("\tlist [-Hv] [-o property[,...]] "
246 "[-T d|u] [pool] ... [interval [count]]\n"));
248 return (gettext("\toffline [-t] <pool> <device> ...\n"));
250 return (gettext("\tonline <pool> <device> ...\n"));
252 return (gettext("\treplace [-f] [-o property=value] "
253 "<pool> <device> [new-device]\n"));
255 return (gettext("\tremove <pool> <device> ...\n"));
257 return (gettext("\treopen <pool>\n"));
259 return (gettext("\tscrub [-s] <pool> ...\n"));
261 return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
264 return (gettext("\tupgrade\n"
266 "\tupgrade [-V version] <-a | pool ...>\n"));
268 return (gettext("\tevents [-vHfc]\n"));
270 return (gettext("\tget [-pH] <\"all\" | property[,...]> "
273 return (gettext("\tset <property=value> <pool> \n"));
275 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
276 "\t [-o property=value] <pool> <newpool> "
277 "[<device> ...]\n"));
279 return (gettext("\treguid <pool>\n"));
288 * Callback routine that will print out a pool property value.
291 print_prop_cb(int prop
, void *cb
)
295 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
297 if (zpool_prop_readonly(prop
))
298 (void) fprintf(fp
, " NO ");
300 (void) fprintf(fp
, " YES ");
302 if (zpool_prop_values(prop
) == NULL
)
303 (void) fprintf(fp
, "-\n");
305 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
311 * Display usage message. If we're inside a command, display only the usage for
312 * that command. Otherwise, iterate over the entire command table and display
313 * a complete usage message.
316 usage(boolean_t requested
)
318 FILE *fp
= requested
? stdout
: stderr
;
320 if (current_command
== NULL
) {
323 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
325 gettext("where 'command' is one of the following:\n\n"));
327 for (i
= 0; i
< NCOMMAND
; i
++) {
328 if (command_table
[i
].name
== NULL
)
329 (void) fprintf(fp
, "\n");
331 (void) fprintf(fp
, "%s",
332 get_usage(command_table
[i
].usage
));
335 (void) fprintf(fp
, gettext("usage:\n"));
336 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
339 if (current_command
!= NULL
&&
340 ((strcmp(current_command
->name
, "set") == 0) ||
341 (strcmp(current_command
->name
, "get") == 0) ||
342 (strcmp(current_command
->name
, "list") == 0))) {
345 gettext("\nthe following properties are supported:\n"));
347 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
348 "PROPERTY", "EDIT", "VALUES");
350 /* Iterate over all properties */
351 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
354 (void) fprintf(fp
, "\t%-15s ", "feature@...");
355 (void) fprintf(fp
, "YES disabled | enabled | active\n");
357 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
358 "appended with a feature name.\nSee zpool-features(5).\n"));
362 * See comments at end of main().
364 if (getenv("ZFS_ABORT") != NULL
) {
365 (void) printf("dumping core by request\n");
369 exit(requested
? 0 : 2);
373 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
374 boolean_t print_logs
)
381 (void) printf("\t%*s%s\n", indent
, "", name
);
383 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
384 &child
, &children
) != 0)
387 for (c
= 0; c
< children
; c
++) {
388 uint64_t is_log
= B_FALSE
;
390 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
392 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
395 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
396 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
403 prop_list_contains_feature(nvlist_t
*proplist
)
406 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
407 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
408 if (zpool_prop_feature(nvpair_name(nvp
)))
415 * Add a property pair (name, string-value) into a property nvlist.
418 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
421 zpool_prop_t prop
= ZPROP_INVAL
;
427 if (*props
== NULL
&&
428 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
429 (void) fprintf(stderr
,
430 gettext("internal error: out of memory\n"));
437 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
439 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
440 !zpool_prop_feature(propname
)) {
441 (void) fprintf(stderr
, gettext("property '%s' is "
442 "not a valid pool property\n"), propname
);
447 * feature@ properties and version should not be specified
450 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
451 nvlist_exists(proplist
, vname
)) ||
452 (prop
== ZPOOL_PROP_VERSION
&&
453 prop_list_contains_feature(proplist
))) {
454 (void) fprintf(stderr
, gettext("'feature@' and "
455 "'version' properties cannot be specified "
461 if (zpool_prop_feature(propname
))
464 normnm
= zpool_prop_to_name(prop
);
466 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
467 normnm
= zfs_prop_to_name(fprop
);
473 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
474 prop
!= ZPOOL_PROP_CACHEFILE
) {
475 (void) fprintf(stderr
, gettext("property '%s' "
476 "specified multiple times\n"), propname
);
480 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
481 (void) fprintf(stderr
, gettext("internal "
482 "error: out of memory\n"));
490 * Set a default property pair (name, string-value) in a property nvlist
493 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
498 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
501 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
505 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
507 * -f Force addition of devices, even if they appear in use
508 * -n Do not add the devices, but display the resulting layout if
509 * they were to be added.
510 * -o Set property=value.
512 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
513 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
517 zpool_do_add(int argc
, char **argv
)
519 boolean_t force
= B_FALSE
;
520 boolean_t dryrun
= B_FALSE
;
527 nvlist_t
*props
= NULL
;
531 while ((c
= getopt(argc
, argv
, "fno:")) != -1) {
540 if ((propval
= strchr(optarg
, '=')) == NULL
) {
541 (void) fprintf(stderr
, gettext("missing "
542 "'=' for -o option\n"));
548 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
549 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
553 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
562 /* get pool name and check number of arguments */
564 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
568 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
577 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
580 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
581 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
587 /* pass off to get_vdev_spec for processing */
588 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
590 if (nvroot
== NULL
) {
596 nvlist_t
*poolnvroot
;
598 uint_t l2children
, c
;
600 boolean_t hadcache
= B_FALSE
;
602 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
605 (void) printf(gettext("would update '%s' to the following "
606 "configuration:\n"), zpool_get_name(zhp
));
608 /* print original main pool and new tree */
609 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
610 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
612 /* Do the same for the logs */
613 if (num_logs(poolnvroot
) > 0) {
614 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
615 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
616 } else if (num_logs(nvroot
) > 0) {
617 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
620 /* Do the same for the caches */
621 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
622 &l2child
, &l2children
) == 0 && l2children
) {
624 (void) printf(gettext("\tcache\n"));
625 for (c
= 0; c
< l2children
; c
++) {
626 vname
= zpool_vdev_name(g_zfs
, NULL
,
627 l2child
[c
], B_FALSE
);
628 (void) printf("\t %s\n", vname
);
632 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
633 &l2child
, &l2children
) == 0 && l2children
) {
635 (void) printf(gettext("\tcache\n"));
636 for (c
= 0; c
< l2children
; c
++) {
637 vname
= zpool_vdev_name(g_zfs
, NULL
,
638 l2child
[c
], B_FALSE
);
639 (void) printf("\t %s\n", vname
);
646 ret
= (zpool_add(zhp
, nvroot
) != 0);
657 * zpool remove <pool> <vdev> ...
659 * Removes the given vdev from the pool. Currently, this supports removing
660 * spares, cache, and log devices from the pool.
663 zpool_do_remove(int argc
, char **argv
)
672 /* get pool name and check number of arguments */
674 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
678 (void) fprintf(stderr
, gettext("missing device\n"));
684 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
687 for (i
= 1; i
< argc
; i
++) {
688 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
696 * zpool labelclear <vdev>
698 * Verifies that the vdev is not active and zeros out the label information
702 zpool_do_labelclear(int argc
, char **argv
)
705 int c
, fd
= -1, ret
= 0;
707 boolean_t inuse
= B_FALSE
;
708 boolean_t force
= B_FALSE
;
711 while ((c
= getopt(argc
, argv
, "f")) != -1) {
717 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
728 (void) fprintf(stderr
, gettext("missing vdev device name\n"));
733 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
734 (void) fprintf(stderr
, gettext("Unable to open %s\n"), vdev
);
739 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0) {
743 (void) fprintf(stderr
,
744 gettext("Unable to determine pool state for %s\n"
745 "Use -f to force the clearing any label data\n"), vdev
);
753 case POOL_STATE_ACTIVE
:
754 case POOL_STATE_SPARE
:
755 case POOL_STATE_L2CACHE
:
756 (void) fprintf(stderr
,
757 gettext("labelclear operation failed.\n"
758 "\tVdev %s is a member (%s), of pool \"%s\".\n"
759 "\tTo remove label information from this device, "
760 "export or destroy\n\tthe pool, or remove %s from "
761 "the configuration of this pool\n\tand retry the "
762 "labelclear operation.\n"),
763 vdev
, zpool_pool_state_to_name(state
), name
, vdev
);
767 case POOL_STATE_EXPORTED
:
771 (void) fprintf(stderr
,
772 gettext("labelclear operation failed.\n\tVdev "
773 "%s is a member of the exported pool \"%s\".\n"
774 "\tUse \"zpool labelclear -f %s\" to force the "
775 "removal of label\n\tinformation.\n"),
780 case POOL_STATE_POTENTIALLY_ACTIVE
:
784 (void) fprintf(stderr
,
785 gettext("labelclear operation failed.\n"
786 "\tVdev %s is a member of the pool \"%s\".\n"
787 "\tThis pool is unknown to this system, but may "
788 "be active on\n\tanother system. Use "
789 "\'zpool labelclear -f %s\' to force the\n"
790 "\tremoval of label information.\n"),
795 case POOL_STATE_DESTROYED
:
796 /* inuse should never be set for a destroyed pool... */
802 if (zpool_clear_label(fd
) != 0) {
803 (void) fprintf(stderr
,
804 gettext("Label clear failed on vdev %s\n"), vdev
);
817 * zpool create [-fnd] [-o property=value] ...
818 * [-O file-system-property=value] ...
819 * [-R root] [-m mountpoint] <pool> <dev> ...
821 * -f Force creation, even if devices appear in use
822 * -n Do not create the pool, but display the resulting layout if it
823 * were to be created.
824 * -R Create a pool under an alternate root
825 * -m Set default mountpoint for the root dataset. By default it's
827 * -o Set property=value.
828 * -d Don't automatically enable all supported pool features
829 * (individual features can be enabled with -o).
830 * -O Set fsproperty=value in the pool's root file system
832 * Creates the named pool according to the given vdev specification. The
833 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
834 * we get the nvlist back from get_vdev_spec(), we either print out the contents
835 * (if '-n' was specified), or pass it to libzfs to do the creation.
838 zpool_do_create(int argc
, char **argv
)
840 boolean_t force
= B_FALSE
;
841 boolean_t dryrun
= B_FALSE
;
842 boolean_t enable_all_pool_feat
= B_TRUE
;
844 nvlist_t
*nvroot
= NULL
;
848 char *altroot
= NULL
;
849 char *mountpoint
= NULL
;
850 nvlist_t
*fsprops
= NULL
;
851 nvlist_t
*props
= NULL
;
855 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
864 enable_all_pool_feat
= B_FALSE
;
868 if (add_prop_list(zpool_prop_to_name(
869 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
871 if (add_prop_list_default(zpool_prop_to_name(
872 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
876 /* Equivalent to -O mountpoint=optarg */
880 if ((propval
= strchr(optarg
, '=')) == NULL
) {
881 (void) fprintf(stderr
, gettext("missing "
882 "'=' for -o option\n"));
888 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
892 * If the user is creating a pool that doesn't support
893 * feature flags, don't enable any features.
895 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
899 ver
= strtoull(propval
, &end
, 10);
901 ver
< SPA_VERSION_FEATURES
) {
902 enable_all_pool_feat
= B_FALSE
;
907 if ((propval
= strchr(optarg
, '=')) == NULL
) {
908 (void) fprintf(stderr
, gettext("missing "
909 "'=' for -O option\n"));
916 * Mountpoints are checked and then added later.
917 * Uniquely among properties, they can be specified
918 * more than once, to avoid conflict with -m.
920 if (0 == strcmp(optarg
,
921 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
922 mountpoint
= propval
;
923 } else if (add_prop_list(optarg
, propval
, &fsprops
,
930 * Sanity check temporary pool name.
932 if (strchr(optarg
, '/') != NULL
) {
933 (void) fprintf(stderr
, gettext("cannot create "
934 "'%s': invalid character '/' in temporary "
936 (void) fprintf(stderr
, gettext("use 'zfs "
937 "create' to create a dataset\n"));
941 if (add_prop_list(zpool_prop_to_name(
942 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
944 if (add_prop_list_default(zpool_prop_to_name(
945 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
950 (void) fprintf(stderr
, gettext("missing argument for "
951 "'%c' option\n"), optopt
);
954 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
963 /* get pool name and check number of arguments */
965 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
969 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
976 * As a special case, check for use of '/' in the name, and direct the
977 * user to use 'zfs create' instead.
979 if (strchr(poolname
, '/') != NULL
) {
980 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
981 "character '/' in pool name\n"), poolname
);
982 (void) fprintf(stderr
, gettext("use 'zfs create' to "
983 "create a dataset\n"));
987 /* pass off to get_vdev_spec for bulk processing */
988 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
993 /* make_root_vdev() allows 0 toplevel children if there are spares */
994 if (!zfs_allocatable_devs(nvroot
)) {
995 (void) fprintf(stderr
, gettext("invalid vdev "
996 "specification: at least one toplevel vdev must be "
1001 if (altroot
!= NULL
&& altroot
[0] != '/') {
1002 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1003 "must be an absolute path\n"), altroot
);
1008 * Check the validity of the mountpoint and direct the user to use the
1009 * '-m' mountpoint option if it looks like its in use.
1011 if (mountpoint
== NULL
||
1012 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1013 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1014 char buf
[MAXPATHLEN
];
1017 if (mountpoint
&& mountpoint
[0] != '/') {
1018 (void) fprintf(stderr
, gettext("invalid mountpoint "
1019 "'%s': must be an absolute path, 'legacy', or "
1020 "'none'\n"), mountpoint
);
1024 if (mountpoint
== NULL
) {
1025 if (altroot
!= NULL
)
1026 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1029 (void) snprintf(buf
, sizeof (buf
), "/%s",
1032 if (altroot
!= NULL
)
1033 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1034 altroot
, mountpoint
);
1036 (void) snprintf(buf
, sizeof (buf
), "%s",
1040 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1041 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1042 "%s\n"), buf
, strerror(errno
));
1043 (void) fprintf(stderr
, gettext("use '-m' "
1044 "option to provide a different default\n"));
1049 while (count
< 3 && readdir(dirp
) != NULL
)
1051 (void) closedir(dirp
);
1054 (void) fprintf(stderr
, gettext("mountpoint "
1055 "'%s' exists and is not empty\n"), buf
);
1056 (void) fprintf(stderr
, gettext("use '-m' "
1057 "option to provide a "
1058 "different default\n"));
1065 * Now that the mountpoint's validity has been checked, ensure that
1066 * the property is set appropriately prior to creating the pool.
1068 if (mountpoint
!= NULL
) {
1069 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1070 mountpoint
, &fsprops
, B_FALSE
);
1078 * For a dry run invocation, print out a basic message and run
1079 * through all the vdevs in the list and print out in an
1080 * appropriate hierarchy.
1082 (void) printf(gettext("would create '%s' with the "
1083 "following layout:\n\n"), poolname
);
1085 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
1086 if (num_logs(nvroot
) > 0)
1087 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
1092 * Hand off to libzfs.
1094 if (enable_all_pool_feat
) {
1096 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1097 char propname
[MAXPATHLEN
];
1098 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1100 (void) snprintf(propname
, sizeof (propname
),
1101 "feature@%s", feat
->fi_uname
);
1104 * Skip feature if user specified it manually
1105 * on the command line.
1107 if (nvlist_exists(props
, propname
))
1110 ret
= add_prop_list(propname
,
1111 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1118 if (zpool_create(g_zfs
, poolname
,
1119 nvroot
, props
, fsprops
) == 0) {
1120 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1121 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1123 if (zfs_mount(pool
, NULL
, 0) == 0)
1124 ret
= zfs_shareall(pool
);
1127 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1128 (void) fprintf(stderr
, gettext("pool name may have "
1134 nvlist_free(nvroot
);
1135 nvlist_free(fsprops
);
1139 nvlist_free(fsprops
);
1146 * zpool destroy <pool>
1148 * -f Forcefully unmount any datasets
1150 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1153 zpool_do_destroy(int argc
, char **argv
)
1155 boolean_t force
= B_FALSE
;
1158 zpool_handle_t
*zhp
;
1162 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1168 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1177 /* check arguments */
1179 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1183 (void) fprintf(stderr
, gettext("too many arguments\n"));
1189 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1191 * As a special case, check for use of '/' in the name, and
1192 * direct the user to use 'zfs destroy' instead.
1194 if (strchr(pool
, '/') != NULL
)
1195 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1196 "destroy a dataset\n"));
1200 if (zpool_disable_datasets(zhp
, force
) != 0) {
1201 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1202 "could not unmount datasets\n"), zpool_get_name(zhp
));
1206 /* The history must be logged as part of the export */
1207 log_history
= B_FALSE
;
1209 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1216 typedef struct export_cbdata
{
1218 boolean_t hardforce
;
1225 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1227 export_cbdata_t
*cb
= data
;
1229 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1232 /* The history must be logged as part of the export */
1233 log_history
= B_FALSE
;
1235 if (cb
->hardforce
) {
1236 if (zpool_export_force(zhp
, history_str
) != 0)
1238 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1246 * zpool export [-f] <pool> ...
1248 * -a Export all pools
1249 * -f Forcefully unmount datasets
1251 * Export the given pools. By default, the command will attempt to cleanly
1252 * unmount any active datasets within the pool. If the '-f' flag is specified,
1253 * then the datasets will be forcefully unmounted.
1256 zpool_do_export(int argc
, char **argv
)
1259 boolean_t do_all
= B_FALSE
;
1260 boolean_t force
= B_FALSE
;
1261 boolean_t hardforce
= B_FALSE
;
1265 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1277 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1284 cb
.hardforce
= hardforce
;
1290 (void) fprintf(stderr
, gettext("too many arguments\n"));
1294 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1295 zpool_export_one
, &cb
));
1298 /* check arguments */
1300 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1304 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1310 * Given a vdev configuration, determine the maximum width needed for the device
1314 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1316 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1321 if (strlen(name
) + depth
> max
)
1322 max
= strlen(name
) + depth
;
1326 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1327 &child
, &children
) == 0) {
1328 for (c
= 0; c
< children
; c
++)
1329 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1334 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1335 &child
, &children
) == 0) {
1336 for (c
= 0; c
< children
; c
++)
1337 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1342 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1343 &child
, &children
) == 0) {
1344 for (c
= 0; c
< children
; c
++)
1345 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1354 typedef struct spare_cbdata
{
1356 zpool_handle_t
*cb_zhp
;
1360 find_vdev(nvlist_t
*nv
, uint64_t search
)
1366 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1370 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1371 &child
, &children
) == 0) {
1372 for (c
= 0; c
< children
; c
++)
1373 if (find_vdev(child
[c
], search
))
1381 find_spare(zpool_handle_t
*zhp
, void *data
)
1383 spare_cbdata_t
*cbp
= data
;
1384 nvlist_t
*config
, *nvroot
;
1386 config
= zpool_get_config(zhp
, NULL
);
1387 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1390 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1400 * Print out configuration state as requested by status_callback.
1403 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1404 int namewidth
, int depth
, boolean_t isspare
)
1408 pool_scan_stat_t
*ps
= NULL
;
1410 char rbuf
[6], wbuf
[6], cbuf
[6];
1412 uint64_t notpresent
;
1416 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1417 &child
, &children
) != 0)
1420 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1421 (uint64_t **)&vs
, &c
) == 0);
1423 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1426 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1429 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1431 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1435 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1439 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1440 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1441 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1442 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1445 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1446 ¬present
) == 0) {
1448 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1449 (void) printf(" was %s", path
);
1450 } else if (vs
->vs_aux
!= 0) {
1453 switch (vs
->vs_aux
) {
1454 case VDEV_AUX_OPEN_FAILED
:
1455 (void) printf(gettext("cannot open"));
1458 case VDEV_AUX_BAD_GUID_SUM
:
1459 (void) printf(gettext("missing device"));
1462 case VDEV_AUX_NO_REPLICAS
:
1463 (void) printf(gettext("insufficient replicas"));
1466 case VDEV_AUX_VERSION_NEWER
:
1467 (void) printf(gettext("newer version"));
1470 case VDEV_AUX_UNSUP_FEAT
:
1471 (void) printf(gettext("unsupported feature(s)"));
1474 case VDEV_AUX_SPARED
:
1475 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1477 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1478 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1479 zpool_get_name(zhp
)) == 0)
1480 (void) printf(gettext("currently in "
1483 (void) printf(gettext("in use by "
1485 zpool_get_name(cb
.cb_zhp
));
1486 zpool_close(cb
.cb_zhp
);
1488 (void) printf(gettext("currently in use"));
1492 case VDEV_AUX_ERR_EXCEEDED
:
1493 (void) printf(gettext("too many errors"));
1496 case VDEV_AUX_IO_FAILURE
:
1497 (void) printf(gettext("experienced I/O failures"));
1500 case VDEV_AUX_BAD_LOG
:
1501 (void) printf(gettext("bad intent log"));
1504 case VDEV_AUX_EXTERNAL
:
1505 (void) printf(gettext("external device fault"));
1508 case VDEV_AUX_SPLIT_POOL
:
1509 (void) printf(gettext("split into new pool"));
1513 (void) printf(gettext("corrupted data"));
1518 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1519 (uint64_t **)&ps
, &c
);
1521 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1522 vs
->vs_scan_processed
!= 0 && children
== 0) {
1523 (void) printf(gettext(" (%s)"),
1524 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1525 "resilvering" : "repairing");
1528 (void) printf("\n");
1530 for (c
= 0; c
< children
; c
++) {
1531 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1533 /* Don't print logs or holes here */
1534 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1536 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1538 if (islog
|| ishole
)
1540 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1541 print_status_config(zhp
, vname
, child
[c
],
1542 namewidth
, depth
+ 2, isspare
);
1549 * Print the configuration of an exported pool. Iterate over all vdevs in the
1550 * pool, printing out the name and status for each one.
1553 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1560 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1561 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1562 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1565 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1566 (uint64_t **)&vs
, &c
) == 0);
1568 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1569 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1571 if (vs
->vs_aux
!= 0) {
1574 switch (vs
->vs_aux
) {
1575 case VDEV_AUX_OPEN_FAILED
:
1576 (void) printf(gettext("cannot open"));
1579 case VDEV_AUX_BAD_GUID_SUM
:
1580 (void) printf(gettext("missing device"));
1583 case VDEV_AUX_NO_REPLICAS
:
1584 (void) printf(gettext("insufficient replicas"));
1587 case VDEV_AUX_VERSION_NEWER
:
1588 (void) printf(gettext("newer version"));
1591 case VDEV_AUX_UNSUP_FEAT
:
1592 (void) printf(gettext("unsupported feature(s)"));
1595 case VDEV_AUX_ERR_EXCEEDED
:
1596 (void) printf(gettext("too many errors"));
1600 (void) printf(gettext("corrupted data"));
1604 (void) printf("\n");
1606 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1607 &child
, &children
) != 0)
1610 for (c
= 0; c
< children
; c
++) {
1611 uint64_t is_log
= B_FALSE
;
1613 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1618 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1619 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1623 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1624 &child
, &children
) == 0) {
1625 (void) printf(gettext("\tcache\n"));
1626 for (c
= 0; c
< children
; c
++) {
1627 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1628 (void) printf("\t %s\n", vname
);
1633 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1634 &child
, &children
) == 0) {
1635 (void) printf(gettext("\tspares\n"));
1636 for (c
= 0; c
< children
; c
++) {
1637 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1638 (void) printf("\t %s\n", vname
);
1646 * Logs are recorded as top level vdevs in the main pool child array
1647 * but with "is_log" set to 1. We use either print_status_config() or
1648 * print_import_config() to print the top level logs then any log
1649 * children (eg mirrored slogs) are printed recursively - which
1650 * works because only the top level vdev is marked "is_log"
1653 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1658 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1662 (void) printf(gettext("\tlogs\n"));
1664 for (c
= 0; c
< children
; c
++) {
1665 uint64_t is_log
= B_FALSE
;
1668 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1672 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1674 print_status_config(zhp
, name
, child
[c
], namewidth
,
1677 print_import_config(name
, child
[c
], namewidth
, 2);
1683 * Display the status for the given pool.
1686 show_import(nvlist_t
*config
)
1688 uint64_t pool_state
;
1694 zpool_status_t reason
;
1695 zpool_errata_t errata
;
1701 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1703 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1705 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1707 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1710 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1711 (uint64_t **)&vs
, &vsc
) == 0);
1712 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1714 reason
= zpool_import_status(config
, &msgid
, &errata
);
1716 (void) printf(gettext(" pool: %s\n"), name
);
1717 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1718 (void) printf(gettext(" state: %s"), health
);
1719 if (pool_state
== POOL_STATE_DESTROYED
)
1720 (void) printf(gettext(" (DESTROYED)"));
1721 (void) printf("\n");
1724 case ZPOOL_STATUS_MISSING_DEV_R
:
1725 case ZPOOL_STATUS_MISSING_DEV_NR
:
1726 case ZPOOL_STATUS_BAD_GUID_SUM
:
1727 (void) printf(gettext(" status: One or more devices are "
1728 "missing from the system.\n"));
1731 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1732 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1733 (void) printf(gettext(" status: One or more devices contains "
1734 "corrupted data.\n"));
1737 case ZPOOL_STATUS_CORRUPT_DATA
:
1739 gettext(" status: The pool data is corrupted.\n"));
1742 case ZPOOL_STATUS_OFFLINE_DEV
:
1743 (void) printf(gettext(" status: One or more devices "
1744 "are offlined.\n"));
1747 case ZPOOL_STATUS_CORRUPT_POOL
:
1748 (void) printf(gettext(" status: The pool metadata is "
1752 case ZPOOL_STATUS_VERSION_OLDER
:
1753 (void) printf(gettext(" status: The pool is formatted using a "
1754 "legacy on-disk version.\n"));
1757 case ZPOOL_STATUS_VERSION_NEWER
:
1758 (void) printf(gettext(" status: The pool is formatted using an "
1759 "incompatible version.\n"));
1762 case ZPOOL_STATUS_FEAT_DISABLED
:
1763 (void) printf(gettext(" status: Some supported features are "
1764 "not enabled on the pool.\n"));
1767 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1768 (void) printf(gettext("status: The pool uses the following "
1769 "feature(s) not supported on this sytem:\n"));
1770 zpool_print_unsup_feat(config
);
1773 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1774 (void) printf(gettext("status: The pool can only be accessed "
1775 "in read-only mode on this system. It\n\tcannot be "
1776 "accessed in read-write mode because it uses the "
1777 "following\n\tfeature(s) not supported on this system:\n"));
1778 zpool_print_unsup_feat(config
);
1781 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1782 (void) printf(gettext(" status: The pool was last accessed by "
1783 "another system.\n"));
1786 case ZPOOL_STATUS_FAULTED_DEV_R
:
1787 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1788 (void) printf(gettext(" status: One or more devices are "
1792 case ZPOOL_STATUS_BAD_LOG
:
1793 (void) printf(gettext(" status: An intent log record cannot be "
1797 case ZPOOL_STATUS_RESILVERING
:
1798 (void) printf(gettext(" status: One or more devices were being "
1802 case ZPOOL_STATUS_ERRATA
:
1803 (void) printf(gettext(" status: Errata #%d detected.\n"),
1809 * No other status can be seen when importing pools.
1811 assert(reason
== ZPOOL_STATUS_OK
);
1815 * Print out an action according to the overall state of the pool.
1817 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1818 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1819 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1820 (void) printf(gettext(" action: The pool can be "
1821 "imported using its name or numeric identifier, "
1822 "though\n\tsome features will not be available "
1823 "without an explicit 'zpool upgrade'.\n"));
1824 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1825 (void) printf(gettext(" action: The pool can be "
1826 "imported using its name or numeric "
1827 "identifier and\n\tthe '-f' flag.\n"));
1828 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
1830 case ZPOOL_ERRATA_NONE
:
1833 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
1834 (void) printf(gettext(" action: The pool can "
1835 "be imported using its name or numeric "
1836 "identifier,\n\thowever there is a compat"
1837 "ibility issue which should be corrected"
1838 "\n\tby running 'zpool scrub'\n"));
1841 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
1842 (void) printf(gettext(" action: The pool can"
1843 "not be imported with this version of ZFS "
1844 "due to\n\tan active asynchronous destroy. "
1845 "Revert to an earlier version\n\tand "
1846 "allow the destroy to complete before "
1852 * All errata must contain an action message.
1857 (void) printf(gettext(" action: The pool can be "
1858 "imported using its name or numeric "
1861 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1862 (void) printf(gettext(" action: The pool can be imported "
1863 "despite missing or damaged devices. The\n\tfault "
1864 "tolerance of the pool may be compromised if imported.\n"));
1867 case ZPOOL_STATUS_VERSION_NEWER
:
1868 (void) printf(gettext(" action: The pool cannot be "
1869 "imported. Access the pool on a system running "
1870 "newer\n\tsoftware, or recreate the pool from "
1873 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1874 (void) printf(gettext("action: The pool cannot be "
1875 "imported. Access the pool on a system that "
1876 "supports\n\tthe required feature(s), or recreate "
1877 "the pool from backup.\n"));
1879 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1880 (void) printf(gettext("action: The pool cannot be "
1881 "imported in read-write mode. Import the pool "
1883 "\t\"-o readonly=on\", access the pool on a system "
1884 "that supports the\n\trequired feature(s), or "
1885 "recreate the pool from backup.\n"));
1887 case ZPOOL_STATUS_MISSING_DEV_R
:
1888 case ZPOOL_STATUS_MISSING_DEV_NR
:
1889 case ZPOOL_STATUS_BAD_GUID_SUM
:
1890 (void) printf(gettext(" action: The pool cannot be "
1891 "imported. Attach the missing\n\tdevices and try "
1895 (void) printf(gettext(" action: The pool cannot be "
1896 "imported due to damaged devices or data.\n"));
1900 /* Print the comment attached to the pool. */
1901 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1902 (void) printf(gettext("comment: %s\n"), comment
);
1905 * If the state is "closed" or "can't open", and the aux state
1906 * is "corrupt data":
1908 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1909 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1910 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1911 if (pool_state
== POOL_STATE_DESTROYED
)
1912 (void) printf(gettext("\tThe pool was destroyed, "
1913 "but can be imported using the '-Df' flags.\n"));
1914 else if (pool_state
!= POOL_STATE_EXPORTED
)
1915 (void) printf(gettext("\tThe pool may be active on "
1916 "another system, but can be imported using\n\t"
1917 "the '-f' flag.\n"));
1921 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1924 (void) printf(gettext(" config:\n\n"));
1926 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1930 print_import_config(name
, nvroot
, namewidth
, 0);
1931 if (num_logs(nvroot
) > 0)
1932 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1934 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1935 (void) printf(gettext("\n\tAdditional devices are known to "
1936 "be part of this pool, though their\n\texact "
1937 "configuration cannot be determined.\n"));
1942 * Perform the import for the given configuration. This passes the heavy
1943 * lifting off to zpool_import_props(), and then mounts the datasets contained
1947 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1948 nvlist_t
*props
, int flags
)
1950 zpool_handle_t
*zhp
;
1955 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1958 verify(nvlist_lookup_uint64(config
,
1959 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1960 verify(nvlist_lookup_uint64(config
,
1961 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1962 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1963 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1964 "is formatted using an unsupported ZFS version\n"), name
);
1966 } else if (state
!= POOL_STATE_EXPORTED
&&
1967 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1968 uint64_t hostid
= 0;
1969 unsigned long system_hostid
= get_system_hostid();
1971 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1974 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
) {
1979 verify(nvlist_lookup_string(config
,
1980 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1981 verify(nvlist_lookup_uint64(config
,
1982 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
1984 (void) fprintf(stderr
, gettext("cannot import "
1985 "'%s': pool may be in use from other "
1986 "system, it was last accessed by %s "
1987 "(hostid: 0x%lx) on %s"), name
, hostname
,
1988 (unsigned long)hostid
,
1989 asctime(localtime(&t
)));
1990 (void) fprintf(stderr
, gettext("use '-f' to "
1991 "import anyway\n"));
1996 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
1999 if (newname
!= NULL
)
2000 name
= (char *)newname
;
2002 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2005 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2006 !(flags
& ZFS_IMPORT_ONLY
) &&
2007 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2017 * zpool import [-d dir] [-D]
2018 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2019 * [-d dir | -c cachefile] [-f] -a
2020 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2021 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2023 * -c Read pool information from a cachefile instead of searching
2026 * -d Scan in a specific directory, other than /dev/. More than
2027 * one directory can be specified using multiple '-d' options.
2029 * -D Scan for previously destroyed pools or import all or only
2030 * specified destroyed pools.
2032 * -R Temporarily import the pool, with all mountpoints relative to
2033 * the given root. The pool will remain exported when the machine
2036 * -V Import even in the presence of faulted vdevs. This is an
2037 * intentionally undocumented option for testing purposes, and
2038 * treats the pool configuration as complete, leaving any bad
2039 * vdevs in the FAULTED state. In other words, it does verbatim
2042 * -f Force import, even if it appears that the pool is active.
2044 * -F Attempt rewind if necessary.
2046 * -n See if rewind would work, but don't actually rewind.
2048 * -N Import the pool but don't mount datasets.
2050 * -T Specify a starting txg to use for import. This option is
2051 * intentionally undocumented option for testing purposes.
2053 * -a Import all pools found.
2055 * -o Set property=value and/or temporary mount options (without '=').
2057 * The import command scans for pools to import, and import pools based on pool
2058 * name and GUID. The pool can also be renamed as part of the import process.
2061 zpool_do_import(int argc
, char **argv
)
2063 char **searchdirs
= NULL
;
2064 char *env
, *envdup
= NULL
;
2068 nvlist_t
*pools
= NULL
;
2069 boolean_t do_all
= B_FALSE
;
2070 boolean_t do_destroyed
= B_FALSE
;
2071 char *mntopts
= NULL
;
2074 uint64_t searchguid
= 0;
2075 char *searchname
= NULL
;
2077 nvlist_t
*found_config
;
2078 nvlist_t
*policy
= NULL
;
2079 nvlist_t
*props
= NULL
;
2081 int flags
= ZFS_IMPORT_NORMAL
;
2082 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2083 boolean_t dryrun
= B_FALSE
;
2084 boolean_t do_rewind
= B_FALSE
;
2085 boolean_t xtreme_rewind
= B_FALSE
;
2086 uint64_t pool_state
, txg
= -1ULL;
2087 char *cachefile
= NULL
;
2088 importargs_t idata
= { 0 };
2092 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2101 if (searchdirs
== NULL
) {
2102 searchdirs
= safe_malloc(sizeof (char *));
2104 char **tmp
= safe_malloc((nsearch
+ 1) *
2106 bcopy(searchdirs
, tmp
, nsearch
*
2111 searchdirs
[nsearch
++] = optarg
;
2114 do_destroyed
= B_TRUE
;
2117 flags
|= ZFS_IMPORT_ANY_HOST
;
2123 flags
|= ZFS_IMPORT_MISSING_LOG
;
2129 flags
|= ZFS_IMPORT_ONLY
;
2132 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2135 if (add_prop_list(optarg
, propval
,
2143 if (add_prop_list(zpool_prop_to_name(
2144 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2146 if (add_prop_list_default(zpool_prop_to_name(
2147 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2151 flags
|= ZFS_IMPORT_TEMP_NAME
;
2152 if (add_prop_list_default(zpool_prop_to_name(
2153 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2159 txg
= strtoull(optarg
, &endptr
, 0);
2160 if (errno
!= 0 || *endptr
!= '\0') {
2161 (void) fprintf(stderr
,
2162 gettext("invalid txg value\n"));
2165 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2168 flags
|= ZFS_IMPORT_VERBATIM
;
2171 xtreme_rewind
= B_TRUE
;
2174 (void) fprintf(stderr
, gettext("missing argument for "
2175 "'%c' option\n"), optopt
);
2179 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2188 if (cachefile
&& nsearch
!= 0) {
2189 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2193 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2194 (void) fprintf(stderr
,
2195 gettext("-n or -X only meaningful with -F\n"));
2199 rewind_policy
= ZPOOL_TRY_REWIND
;
2201 rewind_policy
= ZPOOL_DO_REWIND
;
2203 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2205 /* In the future, we can capture further policy and include it here */
2206 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2207 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2208 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2211 /* check argument count */
2214 (void) fprintf(stderr
, gettext("too many arguments\n"));
2219 (void) fprintf(stderr
, gettext("too many arguments\n"));
2224 * Check for the SYS_CONFIG privilege. We do this explicitly
2225 * here because otherwise any attempt to discover pools will
2228 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
2229 (void) fprintf(stderr
, gettext("cannot "
2230 "discover pools: permission denied\n"));
2231 if (searchdirs
!= NULL
)
2234 nvlist_free(policy
);
2240 * Depending on the arguments given, we do one of the following:
2242 * <none> Iterate through all pools and display information about
2245 * -a Iterate through all pools and try to import each one.
2247 * <id> Find the pool that corresponds to the given GUID/pool
2248 * name and import that one.
2250 * -D Above options applies only to destroyed pools.
2256 searchguid
= strtoull(argv
[0], &endptr
, 10);
2257 if (errno
!= 0 || *endptr
!= '\0') {
2258 searchname
= argv
[0];
2261 found_config
= NULL
;
2264 * User specified a name or guid. Ensure it's unique.
2266 idata
.unique
= B_TRUE
;
2270 * Check the environment for the preferred search path.
2272 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2275 envdup
= strdup(env
);
2277 dir
= strtok(envdup
, ":");
2278 while (dir
!= NULL
) {
2279 if (searchdirs
== NULL
) {
2280 searchdirs
= safe_malloc(sizeof (char *));
2282 char **tmp
= safe_malloc((nsearch
+ 1) *
2284 bcopy(searchdirs
, tmp
, nsearch
*
2289 searchdirs
[nsearch
++] = dir
;
2290 dir
= strtok(NULL
, ":");
2294 idata
.path
= searchdirs
;
2295 idata
.paths
= nsearch
;
2296 idata
.poolname
= searchname
;
2297 idata
.guid
= searchguid
;
2298 idata
.cachefile
= cachefile
;
2300 pools
= zpool_search_import(g_zfs
, &idata
);
2302 if (pools
!= NULL
&& idata
.exists
&&
2303 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2304 (void) fprintf(stderr
, gettext("cannot import '%s': "
2305 "a pool with that name already exists\n"),
2307 (void) fprintf(stderr
, gettext("use the form '%s "
2308 "<pool | id> <newpool>' to give it a new name\n"),
2311 } else if (pools
== NULL
&& idata
.exists
) {
2312 (void) fprintf(stderr
, gettext("cannot import '%s': "
2313 "a pool with that name is already created/imported,\n"),
2315 (void) fprintf(stderr
, gettext("and no additional pools "
2316 "with that name were found\n"));
2318 } else if (pools
== NULL
) {
2320 (void) fprintf(stderr
, gettext("cannot import '%s': "
2321 "no such pool available\n"), argv
[0]);
2327 if (searchdirs
!= NULL
)
2331 nvlist_free(policy
);
2336 * At this point we have a list of import candidate configs. Even if
2337 * we were searching by pool name or guid, we still need to
2338 * post-process the list to deal with pool state and possible
2344 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2346 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2348 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2350 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2352 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2355 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2362 (void) printf("\n");
2365 err
|= do_import(config
, NULL
, mntopts
,
2368 show_import(config
);
2370 } else if (searchname
!= NULL
) {
2374 * We are searching for a pool based on name.
2376 verify(nvlist_lookup_string(config
,
2377 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2379 if (strcmp(name
, searchname
) == 0) {
2380 if (found_config
!= NULL
) {
2381 (void) fprintf(stderr
, gettext(
2382 "cannot import '%s': more than "
2383 "one matching pool\n"), searchname
);
2384 (void) fprintf(stderr
, gettext(
2385 "import by numeric ID instead\n"));
2388 found_config
= config
;
2394 * Search for a pool by guid.
2396 verify(nvlist_lookup_uint64(config
,
2397 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2399 if (guid
== searchguid
)
2400 found_config
= config
;
2405 * If we were searching for a specific pool, verify that we found a
2406 * pool, and then do the import.
2408 if (argc
!= 0 && err
== 0) {
2409 if (found_config
== NULL
) {
2410 (void) fprintf(stderr
, gettext("cannot import '%s': "
2411 "no such pool available\n"), argv
[0]);
2414 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2415 argv
[1], mntopts
, props
, flags
);
2420 * If we were just looking for pools, report an error if none were
2423 if (argc
== 0 && first
)
2424 (void) fprintf(stderr
,
2425 gettext("no pools available to import\n"));
2430 nvlist_free(policy
);
2431 if (searchdirs
!= NULL
)
2436 return (err
? 1 : 0);
2439 typedef struct iostat_cbdata
{
2440 boolean_t cb_verbose
;
2443 zpool_list_t
*cb_list
;
2447 print_iostat_separator(iostat_cbdata_t
*cb
)
2451 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2453 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2457 print_iostat_header(iostat_cbdata_t
*cb
)
2459 (void) printf("%*s capacity operations bandwidth\n",
2460 cb
->cb_namewidth
, "");
2461 (void) printf("%-*s alloc free read write read write\n",
2462 cb
->cb_namewidth
, "pool");
2463 print_iostat_separator(cb
);
2467 * Display a single statistic.
2470 print_one_stat(uint64_t value
)
2474 zfs_nicenum(value
, buf
, sizeof (buf
));
2475 (void) printf(" %5s", buf
);
2479 * Print out all the statistics for the given vdev. This can either be the
2480 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2481 * is a verbose output, and we don't want to display the toplevel pool stats.
2484 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2485 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2487 nvlist_t
**oldchild
, **newchild
;
2489 vdev_stat_t
*oldvs
, *newvs
;
2490 vdev_stat_t zerovs
= { 0 };
2495 if (oldnv
!= NULL
) {
2496 verify(nvlist_lookup_uint64_array(oldnv
,
2497 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2502 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2503 (uint64_t **)&newvs
, &c
) == 0);
2505 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2506 (void) printf("%*s%s", depth
, "", name
);
2508 (void) printf("%*s%s%*s", depth
, "", name
,
2509 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2511 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2516 scale
= (double)NANOSEC
/ tdelta
;
2518 /* only toplevel vdevs have capacity stats */
2519 if (newvs
->vs_space
== 0) {
2520 (void) printf(" - -");
2522 print_one_stat(newvs
->vs_alloc
);
2523 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2526 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2527 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2529 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2530 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2532 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2533 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2535 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2536 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2538 (void) printf("\n");
2540 if (!cb
->cb_verbose
)
2543 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2544 &newchild
, &children
) != 0)
2547 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2548 &oldchild
, &c
) != 0)
2551 for (c
= 0; c
< children
; c
++) {
2552 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2554 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2557 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2560 if (ishole
|| islog
)
2563 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2564 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2565 newchild
[c
], cb
, depth
+ 2);
2570 * Log device section
2573 if (num_logs(newnv
) > 0) {
2574 (void) printf("%-*s - - - - - "
2575 "-\n", cb
->cb_namewidth
, "logs");
2577 for (c
= 0; c
< children
; c
++) {
2578 uint64_t islog
= B_FALSE
;
2579 (void) nvlist_lookup_uint64(newchild
[c
],
2580 ZPOOL_CONFIG_IS_LOG
, &islog
);
2583 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2585 print_vdev_stats(zhp
, vname
, oldnv
?
2586 oldchild
[c
] : NULL
, newchild
[c
],
2595 * Include level 2 ARC devices in iostat output
2597 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2598 &newchild
, &children
) != 0)
2601 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2602 &oldchild
, &c
) != 0)
2606 (void) printf("%-*s - - - - - "
2607 "-\n", cb
->cb_namewidth
, "cache");
2608 for (c
= 0; c
< children
; c
++) {
2609 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2611 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2612 newchild
[c
], cb
, depth
+ 2);
2619 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2621 iostat_cbdata_t
*cb
= data
;
2625 * If the pool has disappeared, remove it from the list and continue.
2627 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2631 pool_list_remove(cb
->cb_list
, zhp
);
2637 * Callback to print out the iostats for the given pool.
2640 print_iostat(zpool_handle_t
*zhp
, void *data
)
2642 iostat_cbdata_t
*cb
= data
;
2643 nvlist_t
*oldconfig
, *newconfig
;
2644 nvlist_t
*oldnvroot
, *newnvroot
;
2646 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2648 if (cb
->cb_iteration
== 1)
2651 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2654 if (oldconfig
== NULL
)
2657 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2661 * Print out the statistics for the pool.
2663 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2666 print_iostat_separator(cb
);
2678 if (isatty(STDOUT_FILENO
)) {
2679 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
2681 columns
= ws
.ws_col
;
2690 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2692 iostat_cbdata_t
*cb
= data
;
2693 nvlist_t
*config
, *nvroot
;
2696 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2697 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2699 if (!cb
->cb_verbose
)
2700 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2702 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2707 * The width must be at least 10, but may be as large as the
2708 * column width - 42 so that we can still fit in one line.
2710 columns
= get_columns();
2712 if (cb
->cb_namewidth
< 10)
2713 cb
->cb_namewidth
= 10;
2714 if (cb
->cb_namewidth
> columns
- 42)
2715 cb
->cb_namewidth
= columns
- 42;
2721 * Parse the input string, get the 'interval' and 'count' value if there is one.
2724 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2727 unsigned long interval
= 0, count
= 0;
2731 * Determine if the last argument is an integer or a pool name
2733 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2737 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2739 if (*end
== '\0' && errno
== 0) {
2740 if (interval
== 0) {
2741 (void) fprintf(stderr
, gettext("interval "
2742 "cannot be zero\n"));
2746 * Ignore the last parameter
2751 * If this is not a valid number, just plow on. The
2752 * user will get a more informative error message later
2760 * If the last argument is also an integer, then we have both a count
2763 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2768 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2770 if (*end
== '\0' && errno
== 0) {
2771 if (interval
== 0) {
2772 (void) fprintf(stderr
, gettext("interval "
2773 "cannot be zero\n"));
2778 * Ignore the last parameter
2792 get_timestamp_arg(char c
)
2795 timestamp_fmt
= UDATE
;
2797 timestamp_fmt
= DDATE
;
2803 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2805 * -v Display statistics for individual vdevs
2806 * -T Display a timestamp in date(1) or Unix format
2808 * This command can be tricky because we want to be able to deal with pool
2809 * creation/destruction as well as vdev configuration changes. The bulk of this
2810 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2811 * on pool_list_update() to detect the addition of new pools. Configuration
2812 * changes are all handled within libzfs.
2815 zpool_do_iostat(int argc
, char **argv
)
2820 unsigned long interval
= 0, count
= 0;
2822 boolean_t verbose
= B_FALSE
;
2823 boolean_t omit_since_boot
= B_FALSE
;
2827 while ((c
= getopt(argc
, argv
, "T:vy")) != -1) {
2830 get_timestamp_arg(*optarg
);
2836 omit_since_boot
= B_TRUE
;
2839 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2848 get_interval_count(&argc
, argv
, &interval
, &count
);
2851 * Construct the list of all interesting pools.
2854 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2857 if (pool_list_count(list
) == 0 && argc
!= 0) {
2858 pool_list_free(list
);
2862 if (pool_list_count(list
) == 0 && interval
== 0) {
2863 pool_list_free(list
);
2864 (void) fprintf(stderr
, gettext("no pools available\n"));
2869 * Enter the main iostat loop.
2872 cb
.cb_verbose
= verbose
;
2873 cb
.cb_iteration
= 0;
2874 cb
.cb_namewidth
= 0;
2877 if ((npools
= pool_list_count(list
)) == 0)
2878 (void) fprintf(stderr
, gettext("no pools available\n"));
2881 * If this is the first iteration and -y was supplied
2882 * we skip any printing.
2884 boolean_t skip
= (omit_since_boot
&&
2885 cb
.cb_iteration
== 0);
2888 * Refresh all statistics. This is done as an
2889 * explicit step before calculating the maximum name
2890 * width, so that any * configuration changes are
2891 * properly accounted for.
2893 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
2897 * Iterate over all pools to determine the maximum width
2898 * for the pool / device name column across all pools.
2900 cb
.cb_namewidth
= 0;
2901 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
2904 if (timestamp_fmt
!= NODATE
)
2905 print_timestamp(timestamp_fmt
);
2908 * If it's the first time and we're not skipping it,
2909 * or either skip or verbose mode, print the header.
2911 if ((++cb
.cb_iteration
== 1 && !skip
) ||
2913 print_iostat_header(&cb
);
2916 (void) sleep(interval
);
2920 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2923 * If there's more than one pool, and we're not in
2924 * verbose mode (which prints a separator for us),
2925 * then print a separator.
2927 if (npools
> 1 && !verbose
)
2928 print_iostat_separator(&cb
);
2931 (void) printf("\n");
2935 * Flush the output so that redirection to a file isn't buffered
2938 (void) fflush(stdout
);
2943 if (count
!= 0 && --count
== 0)
2946 (void) sleep(interval
);
2949 pool_list_free(list
);
2954 typedef struct list_cbdata
{
2955 boolean_t cb_verbose
;
2957 boolean_t cb_scripted
;
2958 zprop_list_t
*cb_proplist
;
2962 * Given a list of columns to display, output appropriate headers for each one.
2965 print_header(list_cbdata_t
*cb
)
2967 zprop_list_t
*pl
= cb
->cb_proplist
;
2968 char headerbuf
[ZPOOL_MAXPROPLEN
];
2970 boolean_t first
= B_TRUE
;
2971 boolean_t right_justify
;
2974 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2975 width
= pl
->pl_width
;
2976 if (first
&& cb
->cb_verbose
) {
2978 * Reset the width to accommodate the verbose listing
2981 width
= cb
->cb_namewidth
;
2989 right_justify
= B_FALSE
;
2990 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2991 header
= zpool_prop_column_name(pl
->pl_prop
);
2992 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2996 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2997 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2998 headerbuf
[i
] = '\0';
3002 if (pl
->pl_next
== NULL
&& !right_justify
)
3003 (void) printf("%s", header
);
3004 else if (right_justify
)
3005 (void) printf("%*s", (int)width
, header
);
3007 (void) printf("%-*s", (int)width
, header
);
3010 (void) printf("\n");
3014 * Given a pool and a list of properties, print out all the properties according
3015 * to the described layout.
3018 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
3020 zprop_list_t
*pl
= cb
->cb_proplist
;
3021 boolean_t first
= B_TRUE
;
3022 char property
[ZPOOL_MAXPROPLEN
];
3024 boolean_t right_justify
;
3027 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3029 width
= pl
->pl_width
;
3030 if (first
&& cb
->cb_verbose
) {
3032 * Reset the width to accommodate the verbose listing
3035 width
= cb
->cb_namewidth
;
3039 if (cb
->cb_scripted
)
3040 (void) printf("\t");
3047 right_justify
= B_FALSE
;
3048 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3049 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
3050 sizeof (property
), NULL
) != 0)
3055 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3056 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
3057 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
3058 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
3059 sizeof (property
)) == 0) {
3067 * If this is being called in scripted mode, or if this is the
3068 * last column and it is left-justified, don't include a width
3071 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3072 (void) printf("%s", propstr
);
3073 else if (right_justify
)
3074 (void) printf("%*s", (int)width
, propstr
);
3076 (void) printf("%-*s", (int)width
, propstr
);
3079 (void) printf("\n");
3083 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
3088 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3091 case ZPOOL_PROP_EXPANDSZ
:
3093 (void) strlcpy(propval
, "-", sizeof (propval
));
3095 zfs_nicenum(value
, propval
, sizeof (propval
));
3097 case ZPOOL_PROP_FRAGMENTATION
:
3098 if (value
== ZFS_FRAG_INVALID
) {
3099 (void) strlcpy(propval
, "-", sizeof (propval
));
3101 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3102 (unsigned long long)value
);
3105 case ZPOOL_PROP_CAPACITY
:
3106 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3107 (unsigned long long)value
);
3110 zfs_nicenum(value
, propval
, sizeof (propval
));
3114 (void) strlcpy(propval
, "-", sizeof (propval
));
3117 (void) printf("\t%s", propval
);
3119 (void) printf(" %*s", (int)width
, propval
);
3123 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3124 list_cbdata_t
*cb
, int depth
)
3130 boolean_t scripted
= cb
->cb_scripted
;
3132 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3133 (uint64_t **)&vs
, &c
) == 0);
3136 boolean_t toplevel
= (vs
->vs_space
!= 0);
3140 (void) printf("\t%s", name
);
3141 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3142 (void) printf("%*s%s", depth
, "", name
);
3144 (void) printf("%*s%s%*s", depth
, "", name
,
3145 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3148 * Print the properties for the individual vdevs. Some
3149 * properties are only applicable to toplevel vdevs. The
3150 * 'toplevel' boolean value is passed to the print_one_column()
3151 * to indicate that the value is valid.
3153 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
3155 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
3157 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
3158 scripted
, toplevel
);
3159 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
3161 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
3162 vs
->vs_fragmentation
, scripted
,
3163 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
3164 cap
= (vs
->vs_space
== 0) ? 0 :
3165 (vs
->vs_alloc
* 100 / vs
->vs_space
);
3166 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
3167 (void) printf("\n");
3170 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3171 &child
, &children
) != 0)
3174 for (c
= 0; c
< children
; c
++) {
3175 uint64_t ishole
= B_FALSE
;
3177 if (nvlist_lookup_uint64(child
[c
],
3178 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3181 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3182 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3187 * Include level 2 ARC devices in iostat output
3189 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3190 &child
, &children
) != 0)
3194 (void) printf("%-*s - - - - - "
3195 "-\n", cb
->cb_namewidth
, "cache");
3196 for (c
= 0; c
< children
; c
++) {
3197 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3199 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3207 * Generic callback function to list a pool.
3210 list_callback(zpool_handle_t
*zhp
, void *data
)
3212 list_cbdata_t
*cbp
= data
;
3216 config
= zpool_get_config(zhp
, NULL
);
3218 print_pool(zhp
, cbp
);
3219 if (!cbp
->cb_verbose
)
3222 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3224 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3230 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3232 * -H Scripted mode. Don't display headers, and separate properties
3234 * -o List of properties to display. Defaults to
3235 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3236 * "dedupratio,health,altroot"
3237 * -T Display a timestamp in date(1) or Unix format
3239 * List all pools in the system, whether or not they're healthy. Output space
3240 * statistics for each one, as well as health status summary.
3243 zpool_do_list(int argc
, char **argv
)
3247 list_cbdata_t cb
= { 0 };
3248 static char default_props
[] =
3249 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3250 "dedupratio,health,altroot";
3251 char *props
= default_props
;
3252 unsigned long interval
= 0, count
= 0;
3254 boolean_t first
= B_TRUE
;
3257 while ((c
= getopt(argc
, argv
, ":Ho:T:v")) != -1) {
3260 cb
.cb_scripted
= B_TRUE
;
3266 get_timestamp_arg(*optarg
);
3269 cb
.cb_verbose
= B_TRUE
;
3272 (void) fprintf(stderr
, gettext("missing argument for "
3273 "'%c' option\n"), optopt
);
3277 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3286 get_interval_count(&argc
, argv
, &interval
, &count
);
3288 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3292 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
3296 if (pool_list_count(list
) == 0)
3299 if (timestamp_fmt
!= NODATE
)
3300 print_timestamp(timestamp_fmt
);
3302 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3306 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3311 if (count
!= 0 && --count
== 0)
3314 pool_list_free(list
);
3315 (void) sleep(interval
);
3318 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3319 (void) printf(gettext("no pools available\n"));
3323 pool_list_free(list
);
3324 zprop_free_list(cb
.cb_proplist
);
3329 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3331 boolean_t force
= B_FALSE
;
3334 char *poolname
, *old_disk
, *new_disk
;
3335 zpool_handle_t
*zhp
;
3336 nvlist_t
*props
= NULL
;
3341 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
3347 if ((propval
= strchr(optarg
, '=')) == NULL
) {
3348 (void) fprintf(stderr
, gettext("missing "
3349 "'=' for -o option\n"));
3355 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
3356 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
3360 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3369 /* get pool name and check number of arguments */
3371 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3378 (void) fprintf(stderr
,
3379 gettext("missing <device> specification\n"));
3387 (void) fprintf(stderr
,
3388 gettext("missing <new_device> specification\n"));
3391 new_disk
= old_disk
;
3401 (void) fprintf(stderr
, gettext("too many arguments\n"));
3405 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3408 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3409 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3415 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
3417 if (nvroot
== NULL
) {
3422 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3424 nvlist_free(nvroot
);
3431 * zpool replace [-f] <pool> <device> <new_device>
3433 * -f Force attach, even if <new_device> appears to be in use.
3435 * Replace <device> with <new_device>.
3439 zpool_do_replace(int argc
, char **argv
)
3441 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3445 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3447 * -f Force attach, even if <new_device> appears to be in use.
3448 * -o Set property=value.
3450 * Attach <new_device> to the mirror containing <device>. If <device> is not
3451 * part of a mirror, then <device> will be transformed into a mirror of
3452 * <device> and <new_device>. In either case, <new_device> will begin life
3453 * with a DTL of [0, now], and will immediately begin to resilver itself.
3456 zpool_do_attach(int argc
, char **argv
)
3458 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3462 * zpool detach [-f] <pool> <device>
3464 * -f Force detach of <device>, even if DTLs argue against it
3465 * (not supported yet)
3467 * Detach a device from a mirror. The operation will be refused if <device>
3468 * is the last device in the mirror, or if the DTLs indicate that this device
3469 * has the only valid copy of some data.
3473 zpool_do_detach(int argc
, char **argv
)
3476 char *poolname
, *path
;
3477 zpool_handle_t
*zhp
;
3481 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3485 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3494 /* get pool name and check number of arguments */
3496 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3501 (void) fprintf(stderr
,
3502 gettext("missing <device> specification\n"));
3509 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3512 ret
= zpool_vdev_detach(zhp
, path
);
3520 * zpool split [-n] [-o prop=val] ...
3522 * [-R altroot] <pool> <newpool> [<device> ...]
3524 * -n Do not split the pool, but display the resulting layout if
3525 * it were to be split.
3526 * -o Set property=value, or set mount options.
3527 * -R Mount the split-off pool under an alternate root.
3529 * Splits the named pool and gives it the new pool name. Devices to be split
3530 * off may be listed, provided that no more than one device is specified
3531 * per top-level vdev mirror. The newly split pool is left in an exported
3532 * state unless -R is specified.
3534 * Restrictions: the top-level of the pool pool must only be made up of
3535 * mirrors; all devices in the pool must be healthy; no device may be
3536 * undergoing a resilvering operation.
3539 zpool_do_split(int argc
, char **argv
)
3541 char *srcpool
, *newpool
, *propval
;
3542 char *mntopts
= NULL
;
3545 zpool_handle_t
*zhp
;
3546 nvlist_t
*config
, *props
= NULL
;
3548 flags
.dryrun
= B_FALSE
;
3549 flags
.import
= B_FALSE
;
3552 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3555 flags
.import
= B_TRUE
;
3557 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3558 &props
, B_TRUE
) != 0) {
3565 flags
.dryrun
= B_TRUE
;
3568 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3571 if (add_prop_list(optarg
, propval
,
3572 &props
, B_TRUE
) != 0) {
3582 (void) fprintf(stderr
, gettext("missing argument for "
3583 "'%c' option\n"), optopt
);
3587 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3594 if (!flags
.import
&& mntopts
!= NULL
) {
3595 (void) fprintf(stderr
, gettext("setting mntopts is only "
3596 "valid when importing the pool\n"));
3604 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3608 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3618 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3621 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3622 if (config
== NULL
) {
3626 (void) printf(gettext("would create '%s' with the "
3627 "following layout:\n\n"), newpool
);
3628 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3630 nvlist_free(config
);
3635 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3639 * The split was successful. Now we need to open the new
3640 * pool and import it.
3642 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3644 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3645 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3647 (void) fprintf(stderr
, gettext("Split was successful, but "
3648 "the datasets could not all be mounted\n"));
3649 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3650 "different altroot\n"), "zpool import");
3660 * zpool online <pool> <device> ...
3663 zpool_do_online(int argc
, char **argv
)
3667 zpool_handle_t
*zhp
;
3669 vdev_state_t newstate
;
3673 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3676 flags
|= ZFS_ONLINE_EXPAND
;
3680 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3689 /* get pool name and check number of arguments */
3691 (void) fprintf(stderr
, gettext("missing pool name\n"));
3695 (void) fprintf(stderr
, gettext("missing device name\n"));
3701 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3704 for (i
= 1; i
< argc
; i
++) {
3705 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3706 if (newstate
!= VDEV_STATE_HEALTHY
) {
3707 (void) printf(gettext("warning: device '%s' "
3708 "onlined, but remains in faulted state\n"),
3710 if (newstate
== VDEV_STATE_FAULTED
)
3711 (void) printf(gettext("use 'zpool "
3712 "clear' to restore a faulted "
3715 (void) printf(gettext("use 'zpool "
3716 "replace' to replace devices "
3717 "that are no longer present\n"));
3730 * zpool offline [-ft] <pool> <device> ...
3732 * -f Force the device into the offline state, even if doing
3733 * so would appear to compromise pool availability.
3734 * (not supported yet)
3736 * -t Only take the device off-line temporarily. The offline
3737 * state will not be persistent across reboots.
3741 zpool_do_offline(int argc
, char **argv
)
3745 zpool_handle_t
*zhp
;
3747 boolean_t istmp
= B_FALSE
;
3750 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3757 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3766 /* get pool name and check number of arguments */
3768 (void) fprintf(stderr
, gettext("missing pool name\n"));
3772 (void) fprintf(stderr
, gettext("missing device name\n"));
3778 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3781 for (i
= 1; i
< argc
; i
++) {
3782 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3792 * zpool clear <pool> [device]
3794 * Clear all errors associated with a pool or a particular device.
3797 zpool_do_clear(int argc
, char **argv
)
3801 boolean_t dryrun
= B_FALSE
;
3802 boolean_t do_rewind
= B_FALSE
;
3803 boolean_t xtreme_rewind
= B_FALSE
;
3804 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3805 nvlist_t
*policy
= NULL
;
3806 zpool_handle_t
*zhp
;
3807 char *pool
, *device
;
3810 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3819 xtreme_rewind
= B_TRUE
;
3822 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3832 (void) fprintf(stderr
, gettext("missing pool name\n"));
3837 (void) fprintf(stderr
, gettext("too many arguments\n"));
3841 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3842 (void) fprintf(stderr
,
3843 gettext("-n or -X only meaningful with -F\n"));
3847 rewind_policy
= ZPOOL_TRY_REWIND
;
3849 rewind_policy
= ZPOOL_DO_REWIND
;
3851 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3853 /* In future, further rewind policy choices can be passed along here */
3854 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3855 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3859 device
= argc
== 2 ? argv
[1] : NULL
;
3861 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3862 nvlist_free(policy
);
3866 if (zpool_clear(zhp
, device
, policy
) != 0)
3871 nvlist_free(policy
);
3877 * zpool reguid <pool>
3880 zpool_do_reguid(int argc
, char **argv
)
3884 zpool_handle_t
*zhp
;
3888 while ((c
= getopt(argc
, argv
, "")) != -1) {
3891 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3900 /* get pool name and check number of arguments */
3902 (void) fprintf(stderr
, gettext("missing pool name\n"));
3907 (void) fprintf(stderr
, gettext("too many arguments\n"));
3912 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3915 ret
= zpool_reguid(zhp
);
3923 * zpool reopen <pool>
3925 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3928 zpool_do_reopen(int argc
, char **argv
)
3932 zpool_handle_t
*zhp
;
3936 while ((c
= getopt(argc
, argv
, "")) != -1) {
3939 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3949 (void) fprintf(stderr
, gettext("missing pool name\n"));
3954 (void) fprintf(stderr
, gettext("too many arguments\n"));
3959 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3962 ret
= zpool_reopen(zhp
);
3967 typedef struct scrub_cbdata
{
3974 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3976 scrub_cbdata_t
*cb
= data
;
3980 * Ignore faulted pools.
3982 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3983 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3984 "currently unavailable\n"), zpool_get_name(zhp
));
3988 err
= zpool_scan(zhp
, cb
->cb_type
);
3994 * zpool scrub [-s] <pool> ...
3996 * -s Stop. Stops any in-progress scrub.
3999 zpool_do_scrub(int argc
, char **argv
)
4004 cb
.cb_type
= POOL_SCAN_SCRUB
;
4007 while ((c
= getopt(argc
, argv
, "s")) != -1) {
4010 cb
.cb_type
= POOL_SCAN_NONE
;
4013 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4025 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4029 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
4032 typedef struct status_cbdata
{
4034 boolean_t cb_allpools
;
4035 boolean_t cb_verbose
;
4036 boolean_t cb_explain
;
4038 boolean_t cb_dedup_stats
;
4042 * Print out detailed scrub status.
4045 print_scan_status(pool_scan_stat_t
*ps
)
4048 uint64_t elapsed
, mins_left
, hours_left
;
4049 uint64_t pass_exam
, examined
, total
;
4051 double fraction_done
;
4052 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4054 (void) printf(gettext(" scan: "));
4056 /* If there's never been a scan, there's not much to say. */
4057 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
4058 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
4059 (void) printf(gettext("none requested\n"));
4063 start
= ps
->pss_start_time
;
4064 end
= ps
->pss_end_time
;
4065 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
4067 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
4068 ps
->pss_func
== POOL_SCAN_RESILVER
);
4070 * Scan is finished or canceled.
4072 if (ps
->pss_state
== DSS_FINISHED
) {
4073 uint64_t minutes_taken
= (end
- start
) / 60;
4076 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4077 fmt
= gettext("scrub repaired %s in %lluh%um with "
4078 "%llu errors on %s");
4079 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4080 fmt
= gettext("resilvered %s in %lluh%um with "
4081 "%llu errors on %s");
4084 (void) printf(fmt
, processed_buf
,
4085 (u_longlong_t
)(minutes_taken
/ 60),
4086 (uint_t
)(minutes_taken
% 60),
4087 (u_longlong_t
)ps
->pss_errors
,
4088 ctime((time_t *)&end
));
4090 } else if (ps
->pss_state
== DSS_CANCELED
) {
4091 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4092 (void) printf(gettext("scrub canceled on %s"),
4094 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4095 (void) printf(gettext("resilver canceled on %s"),
4101 assert(ps
->pss_state
== DSS_SCANNING
);
4104 * Scan is in progress.
4106 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4107 (void) printf(gettext("scrub in progress since %s"),
4109 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4110 (void) printf(gettext("resilver in progress since %s"),
4114 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
4115 total
= ps
->pss_to_examine
;
4116 fraction_done
= (double)examined
/ total
;
4118 /* elapsed time for this pass */
4119 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4120 elapsed
= elapsed
? elapsed
: 1;
4121 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4122 rate
= pass_exam
/ elapsed
;
4123 rate
= rate
? rate
: 1;
4124 mins_left
= ((total
- examined
) / rate
) / 60;
4125 hours_left
= mins_left
/ 60;
4127 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4128 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4129 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4132 * do not print estimated time if hours_left is more than 30 days
4134 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
4135 examined_buf
, total_buf
, rate_buf
);
4136 if (hours_left
< (30 * 24)) {
4137 (void) printf(gettext(", %lluh%um to go\n"),
4138 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4140 (void) printf(gettext(
4141 ", (scan is slow, no estimated time)\n"));
4144 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4145 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
4146 processed_buf
, 100 * fraction_done
);
4147 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4148 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
4149 processed_buf
, 100 * fraction_done
);
4154 print_error_log(zpool_handle_t
*zhp
)
4156 nvlist_t
*nverrlist
= NULL
;
4159 size_t len
= MAXPATHLEN
* 2;
4161 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4162 (void) printf("errors: List of errors unavailable "
4163 "(insufficient privileges)\n");
4167 (void) printf("errors: Permanent errors have been "
4168 "detected in the following files:\n\n");
4170 pathname
= safe_malloc(len
);
4172 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4174 uint64_t dsobj
, obj
;
4176 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4177 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4179 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4181 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4182 (void) printf("%7s %s\n", "", pathname
);
4185 nvlist_free(nverrlist
);
4189 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4198 (void) printf(gettext("\tspares\n"));
4200 for (i
= 0; i
< nspares
; i
++) {
4201 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
4202 print_status_config(zhp
, name
, spares
[i
],
4203 namewidth
, 2, B_TRUE
);
4209 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4218 (void) printf(gettext("\tcache\n"));
4220 for (i
= 0; i
< nl2cache
; i
++) {
4221 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
4222 print_status_config(zhp
, name
, l2cache
[i
],
4223 namewidth
, 2, B_FALSE
);
4229 print_dedup_stats(nvlist_t
*config
)
4231 ddt_histogram_t
*ddh
;
4237 * If the pool was faulted then we may not have been able to
4238 * obtain the config. Otherwise, if we have anything in the dedup
4239 * table continue processing the stats.
4241 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4242 (uint64_t **)&ddo
, &c
) != 0)
4245 (void) printf("\n");
4246 (void) printf(gettext(" dedup: "));
4247 if (ddo
->ddo_count
== 0) {
4248 (void) printf(gettext("no DDT entries\n"));
4252 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4253 (u_longlong_t
)ddo
->ddo_count
,
4254 (u_longlong_t
)ddo
->ddo_dspace
,
4255 (u_longlong_t
)ddo
->ddo_mspace
);
4257 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4258 (uint64_t **)&dds
, &c
) == 0);
4259 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4260 (uint64_t **)&ddh
, &c
) == 0);
4261 zpool_dump_ddt(dds
, ddh
);
4265 * Display a summary of pool status. Displays a summary such as:
4269 * reason: One or more devices ...
4270 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4276 * When given the '-v' option, we print out the complete config. If the '-e'
4277 * option is specified, then we print out error rate information as well.
4280 status_callback(zpool_handle_t
*zhp
, void *data
)
4282 status_cbdata_t
*cbp
= data
;
4283 nvlist_t
*config
, *nvroot
;
4285 zpool_status_t reason
;
4286 zpool_errata_t errata
;
4291 config
= zpool_get_config(zhp
, NULL
);
4292 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
4297 * If we were given 'zpool status -x', only report those pools with
4300 if (cbp
->cb_explain
&&
4301 (reason
== ZPOOL_STATUS_OK
||
4302 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4303 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4304 if (!cbp
->cb_allpools
) {
4305 (void) printf(gettext("pool '%s' is healthy\n"),
4306 zpool_get_name(zhp
));
4308 cbp
->cb_first
= B_FALSE
;
4314 cbp
->cb_first
= B_FALSE
;
4316 (void) printf("\n");
4318 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4320 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4321 (uint64_t **)&vs
, &c
) == 0);
4322 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4324 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4325 (void) printf(gettext(" state: %s\n"), health
);
4328 case ZPOOL_STATUS_MISSING_DEV_R
:
4329 (void) printf(gettext("status: One or more devices could not "
4330 "be opened. Sufficient replicas exist for\n\tthe pool to "
4331 "continue functioning in a degraded state.\n"));
4332 (void) printf(gettext("action: Attach the missing device and "
4333 "online it using 'zpool online'.\n"));
4336 case ZPOOL_STATUS_MISSING_DEV_NR
:
4337 (void) printf(gettext("status: One or more devices could not "
4338 "be opened. There are insufficient\n\treplicas for the "
4339 "pool to continue functioning.\n"));
4340 (void) printf(gettext("action: Attach the missing device and "
4341 "online it using 'zpool online'.\n"));
4344 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4345 (void) printf(gettext("status: One or more devices could not "
4346 "be used because the label is missing or\n\tinvalid. "
4347 "Sufficient replicas exist for the pool to continue\n\t"
4348 "functioning in a degraded state.\n"));
4349 (void) printf(gettext("action: Replace the device using "
4350 "'zpool replace'.\n"));
4353 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4354 (void) printf(gettext("status: One or more devices could not "
4355 "be used because the label is missing \n\tor invalid. "
4356 "There are insufficient replicas for the pool to "
4357 "continue\n\tfunctioning.\n"));
4358 zpool_explain_recover(zpool_get_handle(zhp
),
4359 zpool_get_name(zhp
), reason
, config
);
4362 case ZPOOL_STATUS_FAILING_DEV
:
4363 (void) printf(gettext("status: One or more devices has "
4364 "experienced an unrecoverable error. An\n\tattempt was "
4365 "made to correct the error. Applications are "
4367 (void) printf(gettext("action: Determine if the device needs "
4368 "to be replaced, and clear the errors\n\tusing "
4369 "'zpool clear' or replace the device with 'zpool "
4373 case ZPOOL_STATUS_OFFLINE_DEV
:
4374 (void) printf(gettext("status: One or more devices has "
4375 "been taken offline by the administrator.\n\tSufficient "
4376 "replicas exist for the pool to continue functioning in "
4377 "a\n\tdegraded state.\n"));
4378 (void) printf(gettext("action: Online the device using "
4379 "'zpool online' or replace the device with\n\t'zpool "
4383 case ZPOOL_STATUS_REMOVED_DEV
:
4384 (void) printf(gettext("status: One or more devices has "
4385 "been removed by the administrator.\n\tSufficient "
4386 "replicas exist for the pool to continue functioning in "
4387 "a\n\tdegraded state.\n"));
4388 (void) printf(gettext("action: Online the device using "
4389 "'zpool online' or replace the device with\n\t'zpool "
4393 case ZPOOL_STATUS_RESILVERING
:
4394 (void) printf(gettext("status: One or more devices is "
4395 "currently being resilvered. The pool will\n\tcontinue "
4396 "to function, possibly in a degraded state.\n"));
4397 (void) printf(gettext("action: Wait for the resilver to "
4401 case ZPOOL_STATUS_CORRUPT_DATA
:
4402 (void) printf(gettext("status: One or more devices has "
4403 "experienced an error resulting in data\n\tcorruption. "
4404 "Applications may be affected.\n"));
4405 (void) printf(gettext("action: Restore the file in question "
4406 "if possible. Otherwise restore the\n\tentire pool from "
4410 case ZPOOL_STATUS_CORRUPT_POOL
:
4411 (void) printf(gettext("status: The pool metadata is corrupted "
4412 "and the pool cannot be opened.\n"));
4413 zpool_explain_recover(zpool_get_handle(zhp
),
4414 zpool_get_name(zhp
), reason
, config
);
4417 case ZPOOL_STATUS_VERSION_OLDER
:
4418 (void) printf(gettext("status: The pool is formatted using a "
4419 "legacy on-disk format. The pool can\n\tstill be used, "
4420 "but some features are unavailable.\n"));
4421 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4422 "upgrade'. Once this is done, the\n\tpool will no longer "
4423 "be accessible on software that does not support\n\t"
4424 "feature flags.\n"));
4427 case ZPOOL_STATUS_VERSION_NEWER
:
4428 (void) printf(gettext("status: The pool has been upgraded to a "
4429 "newer, incompatible on-disk version.\n\tThe pool cannot "
4430 "be accessed on this system.\n"));
4431 (void) printf(gettext("action: Access the pool from a system "
4432 "running more recent software, or\n\trestore the pool from "
4436 case ZPOOL_STATUS_FEAT_DISABLED
:
4437 (void) printf(gettext("status: Some supported features are not "
4438 "enabled on the pool. The pool can\n\tstill be used, but "
4439 "some features are unavailable.\n"));
4440 (void) printf(gettext("action: Enable all features using "
4441 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4442 "longer be accessible by software that does not support\n\t"
4443 "the features. See zpool-features(5) for details.\n"));
4446 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4447 (void) printf(gettext("status: The pool cannot be accessed on "
4448 "this system because it uses the\n\tfollowing feature(s) "
4449 "not supported on this system:\n"));
4450 zpool_print_unsup_feat(config
);
4451 (void) printf("\n");
4452 (void) printf(gettext("action: Access the pool from a system "
4453 "that supports the required feature(s),\n\tor restore the "
4454 "pool from backup.\n"));
4457 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4458 (void) printf(gettext("status: The pool can only be accessed "
4459 "in read-only mode on this system. It\n\tcannot be "
4460 "accessed in read-write mode because it uses the "
4461 "following\n\tfeature(s) not supported on this system:\n"));
4462 zpool_print_unsup_feat(config
);
4463 (void) printf("\n");
4464 (void) printf(gettext("action: The pool cannot be accessed in "
4465 "read-write mode. Import the pool with\n"
4466 "\t\"-o readonly=on\", access the pool from a system that "
4467 "supports the\n\trequired feature(s), or restore the "
4468 "pool from backup.\n"));
4471 case ZPOOL_STATUS_FAULTED_DEV_R
:
4472 (void) printf(gettext("status: One or more devices are "
4473 "faulted in response to persistent errors.\n\tSufficient "
4474 "replicas exist for the pool to continue functioning "
4475 "in a\n\tdegraded state.\n"));
4476 (void) printf(gettext("action: Replace the faulted device, "
4477 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4480 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4481 (void) printf(gettext("status: One or more devices are "
4482 "faulted in response to persistent errors. There are "
4483 "insufficient replicas for the pool to\n\tcontinue "
4485 (void) printf(gettext("action: Destroy and re-create the pool "
4486 "from a backup source. Manually marking the device\n"
4487 "\trepaired using 'zpool clear' may allow some data "
4488 "to be recovered.\n"));
4491 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4492 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4493 (void) printf(gettext("status: One or more devices are "
4494 "faulted in response to IO failures.\n"));
4495 (void) printf(gettext("action: Make sure the affected devices "
4496 "are connected, then run 'zpool clear'.\n"));
4499 case ZPOOL_STATUS_BAD_LOG
:
4500 (void) printf(gettext("status: An intent log record "
4501 "could not be read.\n"
4502 "\tWaiting for adminstrator intervention to fix the "
4503 "faulted pool.\n"));
4504 (void) printf(gettext("action: Either restore the affected "
4505 "device(s) and run 'zpool online',\n"
4506 "\tor ignore the intent log records by running "
4507 "'zpool clear'.\n"));
4510 case ZPOOL_STATUS_HOSTID_MISMATCH
:
4511 (void) printf(gettext("status: Mismatch between pool hostid "
4512 "and system hostid on imported pool.\n\tThis pool was "
4513 "previously imported into a system with a different "
4514 "hostid,\n\tand then was verbatim imported into this "
4516 (void) printf(gettext("action: Export this pool on all systems "
4517 "on which it is imported.\n"
4518 "\tThen import it to correct the mismatch.\n"));
4521 case ZPOOL_STATUS_ERRATA
:
4522 (void) printf(gettext("status: Errata #%d detected.\n"),
4526 case ZPOOL_ERRATA_NONE
:
4529 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
4530 (void) printf(gettext("action: To correct the issue "
4531 "run 'zpool scrub'.\n"));
4536 * All errata which allow the pool to be imported
4537 * must contain an action message.
4545 * The remaining errors can't actually be generated, yet.
4547 assert(reason
== ZPOOL_STATUS_OK
);
4551 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4554 if (config
!= NULL
) {
4557 nvlist_t
**spares
, **l2cache
;
4558 uint_t nspares
, nl2cache
;
4559 pool_scan_stat_t
*ps
= NULL
;
4561 (void) nvlist_lookup_uint64_array(nvroot
,
4562 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4563 print_scan_status(ps
);
4565 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4569 (void) printf(gettext("config:\n\n"));
4570 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4571 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4572 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4573 namewidth
, 0, B_FALSE
);
4575 if (num_logs(nvroot
) > 0)
4576 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4577 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4578 &l2cache
, &nl2cache
) == 0)
4579 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4581 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4582 &spares
, &nspares
) == 0)
4583 print_spares(zhp
, spares
, nspares
, namewidth
);
4585 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4587 nvlist_t
*nverrlist
= NULL
;
4590 * If the approximate error count is small, get a
4591 * precise count by fetching the entire log and
4592 * uniquifying the results.
4594 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4595 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4600 while ((elem
= nvlist_next_nvpair(nverrlist
,
4605 nvlist_free(nverrlist
);
4607 (void) printf("\n");
4610 (void) printf(gettext("errors: No known data "
4612 else if (!cbp
->cb_verbose
)
4613 (void) printf(gettext("errors: %llu data "
4614 "errors, use '-v' for a list\n"),
4615 (u_longlong_t
)nerr
);
4617 print_error_log(zhp
);
4620 if (cbp
->cb_dedup_stats
)
4621 print_dedup_stats(config
);
4623 (void) printf(gettext("config: The configuration cannot be "
4631 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4633 * -v Display complete error logs
4634 * -x Display only pools with potential problems
4635 * -D Display dedup status (undocumented)
4636 * -T Display a timestamp in date(1) or Unix format
4638 * Describes the health status of all pools or some subset.
4641 zpool_do_status(int argc
, char **argv
)
4645 unsigned long interval
= 0, count
= 0;
4646 status_cbdata_t cb
= { 0 };
4649 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4652 cb
.cb_verbose
= B_TRUE
;
4655 cb
.cb_explain
= B_TRUE
;
4658 cb
.cb_dedup_stats
= B_TRUE
;
4661 get_timestamp_arg(*optarg
);
4664 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4673 get_interval_count(&argc
, argv
, &interval
, &count
);
4676 cb
.cb_allpools
= B_TRUE
;
4678 cb
.cb_first
= B_TRUE
;
4681 if (timestamp_fmt
!= NODATE
)
4682 print_timestamp(timestamp_fmt
);
4684 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4685 status_callback
, &cb
);
4687 if (argc
== 0 && cb
.cb_count
== 0)
4688 (void) fprintf(stderr
, gettext("no pools available\n"));
4689 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4690 (void) printf(gettext("all pools are healthy\n"));
4698 if (count
!= 0 && --count
== 0)
4701 (void) sleep(interval
);
4707 typedef struct upgrade_cbdata
{
4710 uint64_t cb_version
;
4715 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
4717 int zfs_version
= (int) zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
4718 int *count
= (int *)unsupp_fs
;
4720 if (zfs_version
> ZPL_VERSION
) {
4721 (void) printf(gettext("%s (v%d) is not supported by this "
4722 "implementation of ZFS.\n"),
4723 zfs_get_name(zhp
), zfs_version
);
4727 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
4735 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4739 uint64_t oldversion
;
4742 config
= zpool_get_config(zhp
, NULL
);
4743 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4746 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4747 assert(oldversion
< version
);
4749 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
4754 (void) fprintf(stderr
, gettext("Upgrade not performed due "
4755 "to %d unsupported filesystems (max v%d).\n"),
4756 unsupp_fs
, (int) ZPL_VERSION
);
4760 ret
= zpool_upgrade(zhp
, version
);
4764 if (version
>= SPA_VERSION_FEATURES
) {
4765 (void) printf(gettext("Successfully upgraded "
4766 "'%s' from version %llu to feature flags.\n"),
4767 zpool_get_name(zhp
), (u_longlong_t
) oldversion
);
4769 (void) printf(gettext("Successfully upgraded "
4770 "'%s' from version %llu to version %llu.\n"),
4771 zpool_get_name(zhp
), (u_longlong_t
) oldversion
,
4772 (u_longlong_t
) version
);
4779 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4782 boolean_t firstff
= B_TRUE
;
4783 nvlist_t
*enabled
= zpool_get_features(zhp
);
4786 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4787 const char *fname
= spa_feature_table
[i
].fi_uname
;
4788 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4789 if (!nvlist_exists(enabled
, fguid
)) {
4791 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4792 ret
= zpool_set_prop(zhp
, propname
,
4793 ZFS_FEATURE_ENABLED
);
4801 (void) printf(gettext("Enabled the "
4802 "following features on '%s':\n"),
4803 zpool_get_name(zhp
));
4806 (void) printf(gettext(" %s\n"), fname
);
4817 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4819 upgrade_cbdata_t
*cbp
= arg
;
4822 boolean_t printnl
= B_FALSE
;
4825 config
= zpool_get_config(zhp
, NULL
);
4826 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4829 assert(SPA_VERSION_IS_SUPPORTED(version
));
4831 if (version
< cbp
->cb_version
) {
4832 cbp
->cb_first
= B_FALSE
;
4833 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4839 * If they did "zpool upgrade -a", then we could
4840 * be doing ioctls to different pools. We need
4841 * to log this history once to each pool, and bypass
4842 * the normal history logging that happens in main().
4844 (void) zpool_log_history(g_zfs
, history_str
);
4845 log_history
= B_FALSE
;
4848 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4850 ret
= upgrade_enable_all(zhp
, &count
);
4855 cbp
->cb_first
= B_FALSE
;
4861 (void) printf(gettext("\n"));
4868 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4870 upgrade_cbdata_t
*cbp
= arg
;
4874 config
= zpool_get_config(zhp
, NULL
);
4875 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4878 assert(SPA_VERSION_IS_SUPPORTED(version
));
4880 if (version
< SPA_VERSION_FEATURES
) {
4881 if (cbp
->cb_first
) {
4882 (void) printf(gettext("The following pools are "
4883 "formatted with legacy version numbers and can\n"
4884 "be upgraded to use feature flags. After "
4885 "being upgraded, these pools\nwill no "
4886 "longer be accessible by software that does not "
4887 "support feature\nflags.\n\n"));
4888 (void) printf(gettext("VER POOL\n"));
4889 (void) printf(gettext("--- ------------\n"));
4890 cbp
->cb_first
= B_FALSE
;
4893 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4894 zpool_get_name(zhp
));
4901 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4903 upgrade_cbdata_t
*cbp
= arg
;
4907 config
= zpool_get_config(zhp
, NULL
);
4908 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4911 if (version
>= SPA_VERSION_FEATURES
) {
4913 boolean_t poolfirst
= B_TRUE
;
4914 nvlist_t
*enabled
= zpool_get_features(zhp
);
4916 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4917 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4918 const char *fname
= spa_feature_table
[i
].fi_uname
;
4919 if (!nvlist_exists(enabled
, fguid
)) {
4920 if (cbp
->cb_first
) {
4921 (void) printf(gettext("\nSome "
4922 "supported features are not "
4923 "enabled on the following pools. "
4924 "Once a\nfeature is enabled the "
4925 "pool may become incompatible with "
4926 "software\nthat does not support "
4928 "zpool-features(5) for "
4930 (void) printf(gettext("POOL "
4932 (void) printf(gettext("------"
4934 cbp
->cb_first
= B_FALSE
;
4938 (void) printf(gettext("%s\n"),
4939 zpool_get_name(zhp
));
4940 poolfirst
= B_FALSE
;
4943 (void) printf(gettext(" %s\n"), fname
);
4946 * If they did "zpool upgrade -a", then we could
4947 * be doing ioctls to different pools. We need
4948 * to log this history once to each pool, and bypass
4949 * the normal history logging that happens in main().
4951 (void) zpool_log_history(g_zfs
, history_str
);
4952 log_history
= B_FALSE
;
4961 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4963 boolean_t printnl
= B_FALSE
;
4964 upgrade_cbdata_t
*cbp
= data
;
4965 uint64_t cur_version
;
4968 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4969 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
4970 "Pool 'log' must be renamed using export and import"
4975 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4976 if (cur_version
> cbp
->cb_version
) {
4977 (void) printf(gettext("Pool '%s' is already formatted "
4978 "using more current version '%llu'.\n\n"),
4979 zpool_get_name(zhp
), (u_longlong_t
) cur_version
);
4983 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
4984 (void) printf(gettext("Pool '%s' is already formatted "
4985 "using version %llu.\n\n"), zpool_get_name(zhp
),
4986 (u_longlong_t
) cbp
->cb_version
);
4990 if (cur_version
!= cbp
->cb_version
) {
4992 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4997 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4999 ret
= upgrade_enable_all(zhp
, &count
);
5005 } else if (cur_version
== SPA_VERSION
) {
5006 (void) printf(gettext("Pool '%s' already has all "
5007 "supported features enabled.\n"),
5008 zpool_get_name(zhp
));
5013 (void) printf(gettext("\n"));
5022 * zpool upgrade [-V version] <-a | pool ...>
5024 * With no arguments, display downrev'd ZFS pool available for upgrade.
5025 * Individual pools can be upgraded by specifying the pool, and '-a' will
5026 * upgrade all pools.
5029 zpool_do_upgrade(int argc
, char **argv
)
5032 upgrade_cbdata_t cb
= { 0 };
5034 boolean_t showversions
= B_FALSE
;
5035 boolean_t upgradeall
= B_FALSE
;
5040 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
5043 upgradeall
= B_TRUE
;
5046 showversions
= B_TRUE
;
5049 cb
.cb_version
= strtoll(optarg
, &end
, 10);
5051 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
5052 (void) fprintf(stderr
,
5053 gettext("invalid version '%s'\n"), optarg
);
5058 (void) fprintf(stderr
, gettext("missing argument for "
5059 "'%c' option\n"), optopt
);
5063 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5074 if (cb
.cb_version
== 0) {
5075 cb
.cb_version
= SPA_VERSION
;
5076 } else if (!upgradeall
&& argc
== 0) {
5077 (void) fprintf(stderr
, gettext("-V option is "
5078 "incompatible with other arguments\n"));
5083 if (upgradeall
|| argc
!= 0) {
5084 (void) fprintf(stderr
, gettext("-v option is "
5085 "incompatible with other arguments\n"));
5088 } else if (upgradeall
) {
5090 (void) fprintf(stderr
, gettext("-a option should not "
5091 "be used along with a pool name\n"));
5096 (void) printf(gettext("This system supports ZFS pool feature "
5101 (void) printf(gettext("The following features are "
5103 (void) printf(gettext("FEAT DESCRIPTION\n"));
5104 (void) printf("----------------------------------------------"
5105 "---------------\n");
5106 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5107 zfeature_info_t
*fi
= &spa_feature_table
[i
];
5108 const char *ro
= fi
->fi_can_readonly
?
5109 " (read-only compatible)" : "";
5111 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
5112 (void) printf(" %s\n", fi
->fi_desc
);
5114 (void) printf("\n");
5116 (void) printf(gettext("The following legacy versions are also "
5118 (void) printf(gettext("VER DESCRIPTION\n"));
5119 (void) printf("--- -----------------------------------------"
5120 "---------------\n");
5121 (void) printf(gettext(" 1 Initial ZFS version\n"));
5122 (void) printf(gettext(" 2 Ditto blocks "
5123 "(replicated metadata)\n"));
5124 (void) printf(gettext(" 3 Hot spares and double parity "
5126 (void) printf(gettext(" 4 zpool history\n"));
5127 (void) printf(gettext(" 5 Compression using the gzip "
5129 (void) printf(gettext(" 6 bootfs pool property\n"));
5130 (void) printf(gettext(" 7 Separate intent log devices\n"));
5131 (void) printf(gettext(" 8 Delegated administration\n"));
5132 (void) printf(gettext(" 9 refquota and refreservation "
5134 (void) printf(gettext(" 10 Cache devices\n"));
5135 (void) printf(gettext(" 11 Improved scrub performance\n"));
5136 (void) printf(gettext(" 12 Snapshot properties\n"));
5137 (void) printf(gettext(" 13 snapused property\n"));
5138 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5139 (void) printf(gettext(" 15 user/group space accounting\n"));
5140 (void) printf(gettext(" 16 stmf property support\n"));
5141 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5142 (void) printf(gettext(" 18 Snapshot user holds\n"));
5143 (void) printf(gettext(" 19 Log device removal\n"));
5144 (void) printf(gettext(" 20 Compression using zle "
5145 "(zero-length encoding)\n"));
5146 (void) printf(gettext(" 21 Deduplication\n"));
5147 (void) printf(gettext(" 22 Received properties\n"));
5148 (void) printf(gettext(" 23 Slim ZIL\n"));
5149 (void) printf(gettext(" 24 System attributes\n"));
5150 (void) printf(gettext(" 25 Improved scrub stats\n"));
5151 (void) printf(gettext(" 26 Improved snapshot deletion "
5153 (void) printf(gettext(" 27 Improved snapshot creation "
5155 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5156 (void) printf(gettext("\nFor more information on a particular "
5157 "version, including supported releases,\n"));
5158 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5159 } else if (argc
== 0 && upgradeall
) {
5160 cb
.cb_first
= B_TRUE
;
5161 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5162 if (ret
== 0 && cb
.cb_first
) {
5163 if (cb
.cb_version
== SPA_VERSION
) {
5164 (void) printf(gettext("All pools are already "
5165 "formatted using feature flags.\n\n"));
5166 (void) printf(gettext("Every feature flags "
5167 "pool already has all supported features "
5170 (void) printf(gettext("All pools are already "
5171 "formatted with version %llu or higher.\n"),
5172 (u_longlong_t
) cb
.cb_version
);
5175 } else if (argc
== 0) {
5176 cb
.cb_first
= B_TRUE
;
5177 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5181 (void) printf(gettext("All pools are formatted "
5182 "using feature flags.\n\n"));
5184 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5185 "for a list of available legacy versions.\n"));
5188 cb
.cb_first
= B_TRUE
;
5189 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5193 (void) printf(gettext("Every feature flags pool has "
5194 "all supported features enabled.\n"));
5196 (void) printf(gettext("\n"));
5199 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5206 typedef struct hist_cbdata
{
5213 * Print out the command history for a specific pool.
5216 get_history_one(zpool_handle_t
*zhp
, void *data
)
5222 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5224 cb
->first
= B_FALSE
;
5226 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5228 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5231 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5232 &records
, &numrecords
) == 0);
5233 for (i
= 0; i
< numrecords
; i
++) {
5234 nvlist_t
*rec
= records
[i
];
5237 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5241 tsec
= fnvlist_lookup_uint64(records
[i
],
5243 (void) localtime_r(&tsec
, &t
);
5244 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5247 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5248 (void) printf("%s %s", tbuf
,
5249 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5250 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5252 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5255 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5256 (void) printf("%s unrecognized record:\n",
5258 dump_nvlist(rec
, 4);
5261 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5262 zfs_history_event_names
[ievent
],
5263 (longlong_t
) fnvlist_lookup_uint64(
5264 rec
, ZPOOL_HIST_TXG
),
5265 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5266 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5269 (void) printf("%s [txg:%lld] %s", tbuf
,
5270 (longlong_t
) fnvlist_lookup_uint64(
5271 rec
, ZPOOL_HIST_TXG
),
5272 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5273 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5274 (void) printf(" %s (%llu)",
5275 fnvlist_lookup_string(rec
,
5277 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
5280 (void) printf(" %s", fnvlist_lookup_string(rec
,
5281 ZPOOL_HIST_INT_STR
));
5282 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5285 (void) printf("%s ioctl %s\n", tbuf
,
5286 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5287 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5288 (void) printf(" input:\n");
5289 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5290 ZPOOL_HIST_INPUT_NVL
), 8);
5292 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5293 (void) printf(" output:\n");
5294 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5295 ZPOOL_HIST_OUTPUT_NVL
), 8);
5300 (void) printf("%s unrecognized record:\n", tbuf
);
5301 dump_nvlist(rec
, 4);
5305 (void) printf("\n");
5308 (void) printf(" [");
5309 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5310 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5311 struct passwd
*pwd
= getpwuid(who
);
5312 (void) printf("user %d ", (int)who
);
5314 (void) printf("(%s) ", pwd
->pw_name
);
5316 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5317 (void) printf("on %s",
5318 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5320 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5321 (void) printf(":%s",
5322 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5326 (void) printf("\n");
5328 (void) printf("\n");
5335 * zpool history <pool>
5337 * Displays the history of commands that modified pools.
5340 zpool_do_history(int argc
, char **argv
)
5342 hist_cbdata_t cbdata
= { 0 };
5346 cbdata
.first
= B_TRUE
;
5348 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5351 cbdata
.longfmt
= B_TRUE
;
5354 cbdata
.internal
= B_TRUE
;
5357 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5365 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5368 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5369 (void) fprintf(stderr
, gettext("no pools available\n"));
5376 typedef struct ev_opts
{
5384 zpool_do_events_short(nvlist_t
*nvl
)
5386 char ctime_str
[26], str
[32], *ptr
;
5390 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
5391 memset(str
, ' ', 32);
5392 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
5393 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
5394 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
5395 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
5396 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
5397 (void) printf(gettext("%s "), str
);
5399 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
5400 (void) printf(gettext("%s\n"), ptr
);
5404 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
5408 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
5409 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
5411 data_type_t type
= nvpair_type(nvp
);
5412 const char *name
= nvpair_name(nvp
);
5422 printf(gettext("%*s%s = "), depth
, "", name
);
5425 case DATA_TYPE_BOOLEAN
:
5426 printf(gettext("%s"), "1");
5429 case DATA_TYPE_BOOLEAN_VALUE
:
5430 (void) nvpair_value_boolean_value(nvp
, &b
);
5431 printf(gettext("%s"), b
? "1" : "0");
5434 case DATA_TYPE_BYTE
:
5435 (void) nvpair_value_byte(nvp
, &i8
);
5436 printf(gettext("0x%x"), i8
);
5439 case DATA_TYPE_INT8
:
5440 (void) nvpair_value_int8(nvp
, (void *)&i8
);
5441 printf(gettext("0x%x"), i8
);
5444 case DATA_TYPE_UINT8
:
5445 (void) nvpair_value_uint8(nvp
, &i8
);
5446 printf(gettext("0x%x"), i8
);
5449 case DATA_TYPE_INT16
:
5450 (void) nvpair_value_int16(nvp
, (void *)&i16
);
5451 printf(gettext("0x%x"), i16
);
5454 case DATA_TYPE_UINT16
:
5455 (void) nvpair_value_uint16(nvp
, &i16
);
5456 printf(gettext("0x%x"), i16
);
5459 case DATA_TYPE_INT32
:
5460 (void) nvpair_value_int32(nvp
, (void *)&i32
);
5461 printf(gettext("0x%x"), i32
);
5464 case DATA_TYPE_UINT32
:
5465 (void) nvpair_value_uint32(nvp
, &i32
);
5466 printf(gettext("0x%x"), i32
);
5469 case DATA_TYPE_INT64
:
5470 (void) nvpair_value_int64(nvp
, (void *)&i64
);
5471 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5474 case DATA_TYPE_UINT64
:
5475 (void) nvpair_value_uint64(nvp
, &i64
);
5476 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5479 case DATA_TYPE_HRTIME
:
5480 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
5481 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5484 case DATA_TYPE_STRING
:
5485 (void) nvpair_value_string(nvp
, &str
);
5486 printf(gettext("\"%s\""), str
? str
: "<NULL>");
5489 case DATA_TYPE_NVLIST
:
5490 printf(gettext("(embedded nvlist)\n"));
5491 (void) nvpair_value_nvlist(nvp
, &cnv
);
5492 zpool_do_events_nvprint(cnv
, depth
+ 8);
5493 printf(gettext("%*s(end %s)"), depth
, "", name
);
5496 case DATA_TYPE_NVLIST_ARRAY
: {
5500 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
5501 printf(gettext("(%d embedded nvlists)\n"), nelem
);
5502 for (i
= 0; i
< nelem
; i
++) {
5503 printf(gettext("%*s%s[%d] = %s\n"),
5504 depth
, "", name
, i
, "(embedded nvlist)");
5505 zpool_do_events_nvprint(val
[i
], depth
+ 8);
5506 printf(gettext("%*s(end %s[%i])\n"),
5507 depth
, "", name
, i
);
5509 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
5513 case DATA_TYPE_INT8_ARRAY
: {
5517 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
5518 for (i
= 0; i
< nelem
; i
++)
5519 printf(gettext("0x%x "), val
[i
]);
5524 case DATA_TYPE_UINT8_ARRAY
: {
5528 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
5529 for (i
= 0; i
< nelem
; i
++)
5530 printf(gettext("0x%x "), val
[i
]);
5535 case DATA_TYPE_INT16_ARRAY
: {
5539 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
5540 for (i
= 0; i
< nelem
; i
++)
5541 printf(gettext("0x%x "), val
[i
]);
5546 case DATA_TYPE_UINT16_ARRAY
: {
5550 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
5551 for (i
= 0; i
< nelem
; i
++)
5552 printf(gettext("0x%x "), val
[i
]);
5557 case DATA_TYPE_INT32_ARRAY
: {
5561 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
5562 for (i
= 0; i
< nelem
; i
++)
5563 printf(gettext("0x%x "), val
[i
]);
5568 case DATA_TYPE_UINT32_ARRAY
: {
5572 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
5573 for (i
= 0; i
< nelem
; i
++)
5574 printf(gettext("0x%x "), val
[i
]);
5579 case DATA_TYPE_INT64_ARRAY
: {
5583 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
5584 for (i
= 0; i
< nelem
; i
++)
5585 printf(gettext("0x%llx "),
5586 (u_longlong_t
)val
[i
]);
5591 case DATA_TYPE_UINT64_ARRAY
: {
5595 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
5596 for (i
= 0; i
< nelem
; i
++)
5597 printf(gettext("0x%llx "),
5598 (u_longlong_t
)val
[i
]);
5603 case DATA_TYPE_STRING_ARRAY
: {
5607 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
5608 for (i
= 0; i
< nelem
; i
++)
5609 printf(gettext("\"%s\" "),
5610 str
[i
] ? str
[i
] : "<NULL>");
5615 case DATA_TYPE_BOOLEAN_ARRAY
:
5616 case DATA_TYPE_BYTE_ARRAY
:
5617 case DATA_TYPE_DOUBLE
:
5618 case DATA_TYPE_UNKNOWN
:
5619 printf(gettext("<unknown>"));
5623 printf(gettext("\n"));
5628 zpool_do_events_next(ev_opts_t
*opts
)
5631 int zevent_fd
, ret
, dropped
;
5633 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
5634 VERIFY(zevent_fd
>= 0);
5636 if (!opts
->scripted
)
5637 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5640 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
5641 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
5642 if (ret
|| nvl
== NULL
)
5646 (void) printf(gettext("dropped %d events\n"), dropped
);
5648 zpool_do_events_short(nvl
);
5650 if (opts
->verbose
) {
5651 zpool_do_events_nvprint(nvl
, 8);
5652 printf(gettext("\n"));
5654 (void) fflush(stdout
);
5659 VERIFY(0 == close(zevent_fd
));
5665 zpool_do_events_clear(ev_opts_t
*opts
)
5669 ret
= zpool_events_clear(g_zfs
, &count
);
5671 (void) printf(gettext("cleared %d events\n"), count
);
5677 * zpool events [-vfc]
5679 * Displays events logs by ZFS.
5682 zpool_do_events(int argc
, char **argv
)
5684 ev_opts_t opts
= { 0 };
5689 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
5704 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5713 ret
= zpool_do_events_clear(&opts
);
5715 ret
= zpool_do_events_next(&opts
);
5721 get_callback(zpool_handle_t
*zhp
, void *data
)
5723 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5724 char value
[MAXNAMELEN
];
5725 zprop_source_t srctype
;
5728 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5731 * Skip the special fake placeholder. This will also skip
5732 * over the name property when 'all' is specified.
5734 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5735 pl
== cbp
->cb_proplist
)
5738 if (pl
->pl_prop
== ZPROP_INVAL
&&
5739 (zpool_prop_feature(pl
->pl_user_prop
) ||
5740 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5741 srctype
= ZPROP_SRC_LOCAL
;
5743 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5744 value
, sizeof (value
)) == 0) {
5745 zprop_print_one_property(zpool_get_name(zhp
),
5746 cbp
, pl
->pl_user_prop
, value
, srctype
,
5750 if (zpool_get_prop_literal(zhp
, pl
->pl_prop
, value
,
5751 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5754 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5755 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5763 zpool_do_get(int argc
, char **argv
)
5765 zprop_get_cbdata_t cb
= { 0 };
5766 zprop_list_t fake_name
= { 0 };
5770 while ((c
= getopt(argc
, argv
, "pH")) != -1) {
5773 cb
.cb_literal
= B_TRUE
;
5777 cb
.cb_scripted
= B_TRUE
;
5781 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5791 (void) fprintf(stderr
, gettext("missing property "
5796 cb
.cb_first
= B_TRUE
;
5797 cb
.cb_sources
= ZPROP_SRC_ALL
;
5798 cb
.cb_columns
[0] = GET_COL_NAME
;
5799 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5800 cb
.cb_columns
[2] = GET_COL_VALUE
;
5801 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5802 cb
.cb_type
= ZFS_TYPE_POOL
;
5804 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5810 if (cb
.cb_proplist
!= NULL
) {
5811 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5812 fake_name
.pl_width
= strlen(gettext("NAME"));
5813 fake_name
.pl_next
= cb
.cb_proplist
;
5814 cb
.cb_proplist
= &fake_name
;
5817 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5820 if (cb
.cb_proplist
== &fake_name
)
5821 zprop_free_list(fake_name
.pl_next
);
5823 zprop_free_list(cb
.cb_proplist
);
5828 typedef struct set_cbdata
{
5831 boolean_t cb_any_successful
;
5835 set_callback(zpool_handle_t
*zhp
, void *data
)
5838 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5840 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5843 cb
->cb_any_successful
= B_TRUE
;
5849 zpool_do_set(int argc
, char **argv
)
5851 set_cbdata_t cb
= { 0 };
5854 if (argc
> 1 && argv
[1][0] == '-') {
5855 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5861 (void) fprintf(stderr
, gettext("missing property=value "
5867 (void) fprintf(stderr
, gettext("missing pool name\n"));
5872 (void) fprintf(stderr
, gettext("too many pool names\n"));
5876 cb
.cb_propname
= argv
[1];
5877 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5878 if (cb
.cb_value
== NULL
) {
5879 (void) fprintf(stderr
, gettext("missing value in "
5880 "property=value argument\n"));
5884 *(cb
.cb_value
) = '\0';
5887 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5894 find_command_idx(char *command
, int *idx
)
5898 for (i
= 0; i
< NCOMMAND
; i
++) {
5899 if (command_table
[i
].name
== NULL
)
5902 if (strcmp(command
, command_table
[i
].name
) == 0) {
5911 main(int argc
, char **argv
)
5917 (void) setlocale(LC_ALL
, "");
5918 (void) textdomain(TEXT_DOMAIN
);
5920 dprintf_setup(&argc
, argv
);
5925 * Make sure the user has specified some command.
5928 (void) fprintf(stderr
, gettext("missing command\n"));
5937 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
5940 if ((g_zfs
= libzfs_init()) == NULL
) {
5941 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
5945 libzfs_print_on_error(g_zfs
, B_TRUE
);
5947 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5950 * Run the appropriate command.
5952 if (find_command_idx(cmdname
, &i
) == 0) {
5953 current_command
= &command_table
[i
];
5954 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5955 } else if (strchr(cmdname
, '=')) {
5956 verify(find_command_idx("set", &i
) == 0);
5957 current_command
= &command_table
[i
];
5958 ret
= command_table
[i
].func(argc
, argv
);
5959 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5961 * 'freeze' is a vile debugging abomination, so we treat
5965 int fd
= open(ZFS_DEV
, O_RDWR
);
5966 (void) strcpy((void *)buf
, argv
[2]);
5967 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5969 (void) fprintf(stderr
, gettext("unrecognized "
5970 "command '%s'\n"), cmdname
);
5975 if (ret
== 0 && log_history
)
5976 (void) zpool_log_history(g_zfs
, history_str
);
5981 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5982 * for the purposes of running ::findleaks.
5984 if (getenv("ZFS_ABORT") != NULL
) {
5985 (void) printf("dumping core by request\n");