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.
48 #include <sys/fs/zfs.h>
50 #include <sys/fm/util.h>
51 #include <sys/fm/protocol.h>
52 #include <sys/zfs_ioctl.h>
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 #include "statcommon.h"
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
69 static int zpool_do_list(int, char **);
70 static int zpool_do_iostat(int, char **);
71 static int zpool_do_status(int, char **);
73 static int zpool_do_online(int, char **);
74 static int zpool_do_offline(int, char **);
75 static int zpool_do_clear(int, char **);
76 static int zpool_do_reopen(int, char **);
78 static int zpool_do_reguid(int, char **);
80 static int zpool_do_attach(int, char **);
81 static int zpool_do_detach(int, char **);
82 static int zpool_do_replace(int, char **);
83 static int zpool_do_split(int, char **);
85 static int zpool_do_scrub(int, char **);
87 static int zpool_do_import(int, char **);
88 static int zpool_do_export(int, char **);
90 static int zpool_do_upgrade(int, char **);
92 static int zpool_do_history(int, char **);
93 static int zpool_do_events(int, char **);
95 static int zpool_do_get(int, char **);
96 static int zpool_do_set(int, char **);
99 * These libumem hooks provide a reasonable set of defaults for the allocator's
100 * debugging facilities.
105 _umem_debug_init(void)
107 return ("default,verbose"); /* $UMEM_DEBUG setting */
111 _umem_logging_init(void)
113 return ("fail,contents"); /* $UMEM_LOGGING setting */
146 typedef struct zpool_command
{
148 int (*func
)(int, char **);
153 * Master command table. Each ZFS command has a name, associated function, and
154 * usage message. The usage messages need to be internationalized, so we have
155 * to have a function to return the usage message based on a command index.
157 * These commands are organized according to how they are displayed in the usage
158 * message. An empty command (one with a NULL name) indicates an empty line in
159 * the generic usage message.
161 static zpool_command_t command_table
[] = {
162 { "create", zpool_do_create
, HELP_CREATE
},
163 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
165 { "add", zpool_do_add
, HELP_ADD
},
166 { "remove", zpool_do_remove
, HELP_REMOVE
},
168 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
170 { "list", zpool_do_list
, HELP_LIST
},
171 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
172 { "status", zpool_do_status
, HELP_STATUS
},
174 { "online", zpool_do_online
, HELP_ONLINE
},
175 { "offline", zpool_do_offline
, HELP_OFFLINE
},
176 { "clear", zpool_do_clear
, HELP_CLEAR
},
177 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
179 { "attach", zpool_do_attach
, HELP_ATTACH
},
180 { "detach", zpool_do_detach
, HELP_DETACH
},
181 { "replace", zpool_do_replace
, HELP_REPLACE
},
182 { "split", zpool_do_split
, HELP_SPLIT
},
184 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
186 { "import", zpool_do_import
, HELP_IMPORT
},
187 { "export", zpool_do_export
, HELP_EXPORT
},
188 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
189 { "reguid", zpool_do_reguid
, HELP_REGUID
},
191 { "history", zpool_do_history
, HELP_HISTORY
},
192 { "events", zpool_do_events
, HELP_EVENTS
},
194 { "get", zpool_do_get
, HELP_GET
},
195 { "set", zpool_do_set
, HELP_SET
},
198 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
200 static zpool_command_t
*current_command
;
201 static char history_str
[HIS_MAX_RECORD_LEN
];
202 static boolean_t log_history
= B_TRUE
;
203 static uint_t timestamp_fmt
= NODATE
;
206 get_usage(zpool_help_t idx
) {
209 return (gettext("\tadd [-fn] [-o property=value] "
210 "<pool> <vdev> ...\n"));
212 return (gettext("\tattach [-f] [-o property=value] "
213 "<pool> <device> <new-device>\n"));
215 return (gettext("\tclear [-nF] <pool> [device]\n"));
217 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
218 "\t [-O file-system-property=value] ... \n"
219 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
221 return (gettext("\tdestroy [-f] <pool>\n"));
223 return (gettext("\tdetach <pool> <device>\n"));
225 return (gettext("\texport [-af] <pool> ...\n"));
227 return (gettext("\thistory [-il] [<pool>] ...\n"));
229 return (gettext("\timport [-d dir] [-D]\n"
230 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
231 "\timport [-o mntopts] [-o property=value] ... \n"
232 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
233 "[-R root] [-F [-n]] -a\n"
234 "\timport [-o mntopts] [-o property=value] ... \n"
235 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
236 "[-R root] [-F [-n]]\n"
237 "\t <pool | id> [newpool]\n"));
239 return (gettext("\tiostat [-v] [-T d|u] [-y] [pool] ... "
240 "[interval [count]]\n"));
241 case HELP_LABELCLEAR
:
242 return (gettext("\tlabelclear [-f] <vdev>\n"));
244 return (gettext("\tlist [-Hv] [-o property[,...]] "
245 "[-T d|u] [pool] ... [interval [count]]\n"));
247 return (gettext("\toffline [-t] <pool> <device> ...\n"));
249 return (gettext("\tonline <pool> <device> ...\n"));
251 return (gettext("\treplace [-f] [-o property=value] "
252 "<pool> <device> [new-device]\n"));
254 return (gettext("\tremove <pool> <device> ...\n"));
256 return (gettext("\treopen <pool>\n"));
258 return (gettext("\tscrub [-s] <pool> ...\n"));
260 return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
263 return (gettext("\tupgrade\n"
265 "\tupgrade [-V version] <-a | pool ...>\n"));
267 return (gettext("\tevents [-vHfc]\n"));
269 return (gettext("\tget [-pH] <\"all\" | property[,...]> "
272 return (gettext("\tset <property=value> <pool> \n"));
274 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
275 "\t [-o property=value] <pool> <newpool> "
276 "[<device> ...]\n"));
278 return (gettext("\treguid <pool>\n"));
287 * Callback routine that will print out a pool property value.
290 print_prop_cb(int prop
, void *cb
)
294 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
296 if (zpool_prop_readonly(prop
))
297 (void) fprintf(fp
, " NO ");
299 (void) fprintf(fp
, " YES ");
301 if (zpool_prop_values(prop
) == NULL
)
302 (void) fprintf(fp
, "-\n");
304 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
310 * Display usage message. If we're inside a command, display only the usage for
311 * that command. Otherwise, iterate over the entire command table and display
312 * a complete usage message.
315 usage(boolean_t requested
)
317 FILE *fp
= requested
? stdout
: stderr
;
319 if (current_command
== NULL
) {
322 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
324 gettext("where 'command' is one of the following:\n\n"));
326 for (i
= 0; i
< NCOMMAND
; i
++) {
327 if (command_table
[i
].name
== NULL
)
328 (void) fprintf(fp
, "\n");
330 (void) fprintf(fp
, "%s",
331 get_usage(command_table
[i
].usage
));
334 (void) fprintf(fp
, gettext("usage:\n"));
335 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
338 if (current_command
!= NULL
&&
339 ((strcmp(current_command
->name
, "set") == 0) ||
340 (strcmp(current_command
->name
, "get") == 0) ||
341 (strcmp(current_command
->name
, "list") == 0))) {
344 gettext("\nthe following properties are supported:\n"));
346 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
347 "PROPERTY", "EDIT", "VALUES");
349 /* Iterate over all properties */
350 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
353 (void) fprintf(fp
, "\t%-15s ", "feature@...");
354 (void) fprintf(fp
, "YES disabled | enabled | active\n");
356 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
357 "appended with a feature name.\nSee zpool-features(5).\n"));
361 * See comments at end of main().
363 if (getenv("ZFS_ABORT") != NULL
) {
364 (void) printf("dumping core by request\n");
368 exit(requested
? 0 : 2);
372 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
373 boolean_t print_logs
)
380 (void) printf("\t%*s%s\n", indent
, "", name
);
382 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
383 &child
, &children
) != 0)
386 for (c
= 0; c
< children
; c
++) {
387 uint64_t is_log
= B_FALSE
;
389 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
391 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
394 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
395 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
402 prop_list_contains_feature(nvlist_t
*proplist
)
405 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
406 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
407 if (zpool_prop_feature(nvpair_name(nvp
)))
414 * Add a property pair (name, string-value) into a property nvlist.
417 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
420 zpool_prop_t prop
= ZPROP_INVAL
;
426 if (*props
== NULL
&&
427 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
428 (void) fprintf(stderr
,
429 gettext("internal error: out of memory\n"));
436 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
438 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
439 !zpool_prop_feature(propname
)) {
440 (void) fprintf(stderr
, gettext("property '%s' is "
441 "not a valid pool property\n"), propname
);
446 * feature@ properties and version should not be specified
449 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
450 nvlist_exists(proplist
, vname
)) ||
451 (prop
== ZPOOL_PROP_VERSION
&&
452 prop_list_contains_feature(proplist
))) {
453 (void) fprintf(stderr
, gettext("'feature@' and "
454 "'version' properties cannot be specified "
460 if (zpool_prop_feature(propname
))
463 normnm
= zpool_prop_to_name(prop
);
465 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
466 normnm
= zfs_prop_to_name(fprop
);
472 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
473 prop
!= ZPOOL_PROP_CACHEFILE
) {
474 (void) fprintf(stderr
, gettext("property '%s' "
475 "specified multiple times\n"), propname
);
479 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
480 (void) fprintf(stderr
, gettext("internal "
481 "error: out of memory\n"));
489 * Set a default property pair (name, string-value) in a property nvlist
492 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
497 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
500 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
504 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
506 * -f Force addition of devices, even if they appear in use
507 * -n Do not add the devices, but display the resulting layout if
508 * they were to be added.
509 * -o Set property=value.
511 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
512 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
516 zpool_do_add(int argc
, char **argv
)
518 boolean_t force
= B_FALSE
;
519 boolean_t dryrun
= B_FALSE
;
526 nvlist_t
*props
= NULL
;
530 while ((c
= getopt(argc
, argv
, "fno:")) != -1) {
539 if ((propval
= strchr(optarg
, '=')) == NULL
) {
540 (void) fprintf(stderr
, gettext("missing "
541 "'=' for -o option\n"));
547 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
548 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
552 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
561 /* get pool name and check number of arguments */
563 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
567 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
576 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
579 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
580 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
586 /* pass off to get_vdev_spec for processing */
587 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
589 if (nvroot
== NULL
) {
595 nvlist_t
*poolnvroot
;
597 uint_t l2children
, c
;
599 boolean_t hadcache
= B_FALSE
;
601 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
604 (void) printf(gettext("would update '%s' to the following "
605 "configuration:\n"), zpool_get_name(zhp
));
607 /* print original main pool and new tree */
608 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
609 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
611 /* Do the same for the logs */
612 if (num_logs(poolnvroot
) > 0) {
613 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
614 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
615 } else if (num_logs(nvroot
) > 0) {
616 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
619 /* Do the same for the caches */
620 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
621 &l2child
, &l2children
) == 0 && l2children
) {
623 (void) printf(gettext("\tcache\n"));
624 for (c
= 0; c
< l2children
; c
++) {
625 vname
= zpool_vdev_name(g_zfs
, NULL
,
626 l2child
[c
], B_FALSE
);
627 (void) printf("\t %s\n", vname
);
631 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
632 &l2child
, &l2children
) == 0 && l2children
) {
634 (void) printf(gettext("\tcache\n"));
635 for (c
= 0; c
< l2children
; c
++) {
636 vname
= zpool_vdev_name(g_zfs
, NULL
,
637 l2child
[c
], B_FALSE
);
638 (void) printf("\t %s\n", vname
);
645 ret
= (zpool_add(zhp
, nvroot
) != 0);
656 * zpool remove <pool> <vdev> ...
658 * Removes the given vdev from the pool. Currently, this supports removing
659 * spares, cache, and log devices from the pool.
662 zpool_do_remove(int argc
, char **argv
)
671 /* get pool name and check number of arguments */
673 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
677 (void) fprintf(stderr
, gettext("missing device\n"));
683 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
686 for (i
= 1; i
< argc
; i
++) {
687 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
695 * zpool labelclear <vdev>
697 * Verifies that the vdev is not active and zeros out the label information
701 zpool_do_labelclear(int argc
, char **argv
)
704 int c
, fd
= -1, ret
= 0;
706 boolean_t inuse
= B_FALSE
;
707 boolean_t force
= B_FALSE
;
710 while ((c
= getopt(argc
, argv
, "f")) != -1) {
716 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
727 (void) fprintf(stderr
, gettext("missing vdev device name\n"));
732 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
733 (void) fprintf(stderr
, gettext("Unable to open %s\n"), vdev
);
738 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0) {
742 (void) fprintf(stderr
,
743 gettext("Unable to determine pool state for %s\n"
744 "Use -f to force the clearing any label data\n"), vdev
);
752 case POOL_STATE_ACTIVE
:
753 case POOL_STATE_SPARE
:
754 case POOL_STATE_L2CACHE
:
755 (void) fprintf(stderr
,
756 gettext("labelclear operation failed.\n"
757 "\tVdev %s is a member (%s), of pool \"%s\".\n"
758 "\tTo remove label information from this device, "
759 "export or destroy\n\tthe pool, or remove %s from "
760 "the configuration of this pool\n\tand retry the "
761 "labelclear operation.\n"),
762 vdev
, zpool_pool_state_to_name(state
), name
, vdev
);
766 case POOL_STATE_EXPORTED
:
770 (void) fprintf(stderr
,
771 gettext("labelclear operation failed.\n\tVdev "
772 "%s is a member of the exported pool \"%s\".\n"
773 "\tUse \"zpool labelclear -f %s\" to force the "
774 "removal of label\n\tinformation.\n"),
779 case POOL_STATE_POTENTIALLY_ACTIVE
:
783 (void) fprintf(stderr
,
784 gettext("labelclear operation failed.\n"
785 "\tVdev %s is a member of the pool \"%s\".\n"
786 "\tThis pool is unknown to this system, but may "
787 "be active on\n\tanother system. Use "
788 "\'zpool labelclear -f %s\' to force the\n"
789 "\tremoval of label information.\n"),
794 case POOL_STATE_DESTROYED
:
795 /* inuse should never be set for a destroyed pool... */
801 if (zpool_clear_label(fd
) != 0) {
802 (void) fprintf(stderr
,
803 gettext("Label clear failed on vdev %s\n"), vdev
);
816 * zpool create [-fnd] [-o property=value] ...
817 * [-O file-system-property=value] ...
818 * [-R root] [-m mountpoint] <pool> <dev> ...
820 * -f Force creation, even if devices appear in use
821 * -n Do not create the pool, but display the resulting layout if it
822 * were to be created.
823 * -R Create a pool under an alternate root
824 * -m Set default mountpoint for the root dataset. By default it's
826 * -o Set property=value.
827 * -d Don't automatically enable all supported pool features
828 * (individual features can be enabled with -o).
829 * -O Set fsproperty=value in the pool's root file system
831 * Creates the named pool according to the given vdev specification. The
832 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
833 * we get the nvlist back from get_vdev_spec(), we either print out the contents
834 * (if '-n' was specified), or pass it to libzfs to do the creation.
837 zpool_do_create(int argc
, char **argv
)
839 boolean_t force
= B_FALSE
;
840 boolean_t dryrun
= B_FALSE
;
841 boolean_t enable_all_pool_feat
= B_TRUE
;
843 nvlist_t
*nvroot
= NULL
;
847 char *altroot
= NULL
;
848 char *mountpoint
= NULL
;
849 nvlist_t
*fsprops
= NULL
;
850 nvlist_t
*props
= NULL
;
854 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
863 enable_all_pool_feat
= B_FALSE
;
867 if (add_prop_list(zpool_prop_to_name(
868 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
870 if (add_prop_list_default(zpool_prop_to_name(
871 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
875 /* Equivalent to -O mountpoint=optarg */
879 if ((propval
= strchr(optarg
, '=')) == NULL
) {
880 (void) fprintf(stderr
, gettext("missing "
881 "'=' for -o option\n"));
887 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
891 * If the user is creating a pool that doesn't support
892 * feature flags, don't enable any features.
894 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
898 ver
= strtoull(propval
, &end
, 10);
900 ver
< SPA_VERSION_FEATURES
) {
901 enable_all_pool_feat
= B_FALSE
;
906 if ((propval
= strchr(optarg
, '=')) == NULL
) {
907 (void) fprintf(stderr
, gettext("missing "
908 "'=' for -O option\n"));
915 * Mountpoints are checked and then added later.
916 * Uniquely among properties, they can be specified
917 * more than once, to avoid conflict with -m.
919 if (0 == strcmp(optarg
,
920 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
921 mountpoint
= propval
;
922 } else if (add_prop_list(optarg
, propval
, &fsprops
,
929 * Sanity check temporary pool name.
931 if (strchr(optarg
, '/') != NULL
) {
932 (void) fprintf(stderr
, gettext("cannot create "
933 "'%s': invalid character '/' in temporary "
935 (void) fprintf(stderr
, gettext("use 'zfs "
936 "create' to create a dataset\n"));
940 if (add_prop_list(zpool_prop_to_name(
941 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
943 if (add_prop_list_default(zpool_prop_to_name(
944 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
949 (void) fprintf(stderr
, gettext("missing argument for "
950 "'%c' option\n"), optopt
);
953 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
962 /* get pool name and check number of arguments */
964 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
968 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
975 * As a special case, check for use of '/' in the name, and direct the
976 * user to use 'zfs create' instead.
978 if (strchr(poolname
, '/') != NULL
) {
979 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
980 "character '/' in pool name\n"), poolname
);
981 (void) fprintf(stderr
, gettext("use 'zfs create' to "
982 "create a dataset\n"));
986 /* pass off to get_vdev_spec for bulk processing */
987 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
992 /* make_root_vdev() allows 0 toplevel children if there are spares */
993 if (!zfs_allocatable_devs(nvroot
)) {
994 (void) fprintf(stderr
, gettext("invalid vdev "
995 "specification: at least one toplevel vdev must be "
1000 if (altroot
!= NULL
&& altroot
[0] != '/') {
1001 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1002 "must be an absolute path\n"), altroot
);
1007 * Check the validity of the mountpoint and direct the user to use the
1008 * '-m' mountpoint option if it looks like its in use.
1010 if (mountpoint
== NULL
||
1011 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1012 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1013 char buf
[MAXPATHLEN
];
1016 if (mountpoint
&& mountpoint
[0] != '/') {
1017 (void) fprintf(stderr
, gettext("invalid mountpoint "
1018 "'%s': must be an absolute path, 'legacy', or "
1019 "'none'\n"), mountpoint
);
1023 if (mountpoint
== NULL
) {
1024 if (altroot
!= NULL
)
1025 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1028 (void) snprintf(buf
, sizeof (buf
), "/%s",
1031 if (altroot
!= NULL
)
1032 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1033 altroot
, mountpoint
);
1035 (void) snprintf(buf
, sizeof (buf
), "%s",
1039 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1040 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1041 "%s\n"), buf
, strerror(errno
));
1042 (void) fprintf(stderr
, gettext("use '-m' "
1043 "option to provide a different default\n"));
1048 while (count
< 3 && readdir(dirp
) != NULL
)
1050 (void) closedir(dirp
);
1053 (void) fprintf(stderr
, gettext("mountpoint "
1054 "'%s' exists and is not empty\n"), buf
);
1055 (void) fprintf(stderr
, gettext("use '-m' "
1056 "option to provide a "
1057 "different default\n"));
1064 * Now that the mountpoint's validity has been checked, ensure that
1065 * the property is set appropriately prior to creating the pool.
1067 if (mountpoint
!= NULL
) {
1068 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1069 mountpoint
, &fsprops
, B_FALSE
);
1077 * For a dry run invocation, print out a basic message and run
1078 * through all the vdevs in the list and print out in an
1079 * appropriate hierarchy.
1081 (void) printf(gettext("would create '%s' with the "
1082 "following layout:\n\n"), poolname
);
1084 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
1085 if (num_logs(nvroot
) > 0)
1086 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
1091 * Hand off to libzfs.
1093 if (enable_all_pool_feat
) {
1095 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1096 char propname
[MAXPATHLEN
];
1097 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1099 (void) snprintf(propname
, sizeof (propname
),
1100 "feature@%s", feat
->fi_uname
);
1103 * Skip feature if user specified it manually
1104 * on the command line.
1106 if (nvlist_exists(props
, propname
))
1109 ret
= add_prop_list(propname
,
1110 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1117 if (zpool_create(g_zfs
, poolname
,
1118 nvroot
, props
, fsprops
) == 0) {
1119 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1120 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1122 if (zfs_mount(pool
, NULL
, 0) == 0)
1123 ret
= zfs_shareall(pool
);
1126 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1127 (void) fprintf(stderr
, gettext("pool name may have "
1133 nvlist_free(nvroot
);
1134 nvlist_free(fsprops
);
1138 nvlist_free(fsprops
);
1145 * zpool destroy <pool>
1147 * -f Forcefully unmount any datasets
1149 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1152 zpool_do_destroy(int argc
, char **argv
)
1154 boolean_t force
= B_FALSE
;
1157 zpool_handle_t
*zhp
;
1161 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1167 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1176 /* check arguments */
1178 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1182 (void) fprintf(stderr
, gettext("too many arguments\n"));
1188 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1190 * As a special case, check for use of '/' in the name, and
1191 * direct the user to use 'zfs destroy' instead.
1193 if (strchr(pool
, '/') != NULL
)
1194 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1195 "destroy a dataset\n"));
1199 if (zpool_disable_datasets(zhp
, force
) != 0) {
1200 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1201 "could not unmount datasets\n"), zpool_get_name(zhp
));
1205 /* The history must be logged as part of the export */
1206 log_history
= B_FALSE
;
1208 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1215 typedef struct export_cbdata
{
1217 boolean_t hardforce
;
1224 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1226 export_cbdata_t
*cb
= data
;
1228 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1231 /* The history must be logged as part of the export */
1232 log_history
= B_FALSE
;
1234 if (cb
->hardforce
) {
1235 if (zpool_export_force(zhp
, history_str
) != 0)
1237 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1245 * zpool export [-f] <pool> ...
1247 * -a Export all pools
1248 * -f Forcefully unmount datasets
1250 * Export the given pools. By default, the command will attempt to cleanly
1251 * unmount any active datasets within the pool. If the '-f' flag is specified,
1252 * then the datasets will be forcefully unmounted.
1255 zpool_do_export(int argc
, char **argv
)
1258 boolean_t do_all
= B_FALSE
;
1259 boolean_t force
= B_FALSE
;
1260 boolean_t hardforce
= B_FALSE
;
1264 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1276 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1283 cb
.hardforce
= hardforce
;
1289 (void) fprintf(stderr
, gettext("too many arguments\n"));
1293 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1294 zpool_export_one
, &cb
));
1297 /* check arguments */
1299 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1303 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1309 * Given a vdev configuration, determine the maximum width needed for the device
1313 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1315 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1320 if (strlen(name
) + depth
> max
)
1321 max
= strlen(name
) + depth
;
1325 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1326 &child
, &children
) == 0) {
1327 for (c
= 0; c
< children
; c
++)
1328 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1333 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1334 &child
, &children
) == 0) {
1335 for (c
= 0; c
< children
; c
++)
1336 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1341 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1342 &child
, &children
) == 0) {
1343 for (c
= 0; c
< children
; c
++)
1344 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1353 typedef struct spare_cbdata
{
1355 zpool_handle_t
*cb_zhp
;
1359 find_vdev(nvlist_t
*nv
, uint64_t search
)
1365 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1369 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1370 &child
, &children
) == 0) {
1371 for (c
= 0; c
< children
; c
++)
1372 if (find_vdev(child
[c
], search
))
1380 find_spare(zpool_handle_t
*zhp
, void *data
)
1382 spare_cbdata_t
*cbp
= data
;
1383 nvlist_t
*config
, *nvroot
;
1385 config
= zpool_get_config(zhp
, NULL
);
1386 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1389 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1399 * Print out configuration state as requested by status_callback.
1402 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1403 int namewidth
, int depth
, boolean_t isspare
)
1407 pool_scan_stat_t
*ps
= NULL
;
1409 char rbuf
[6], wbuf
[6], cbuf
[6];
1411 uint64_t notpresent
;
1415 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1416 &child
, &children
) != 0)
1419 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1420 (uint64_t **)&vs
, &c
) == 0);
1422 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1425 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1428 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1430 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1434 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1438 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1439 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1440 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1441 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1444 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1445 ¬present
) == 0) {
1447 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1448 (void) printf(" was %s", path
);
1449 } else if (vs
->vs_aux
!= 0) {
1452 switch (vs
->vs_aux
) {
1453 case VDEV_AUX_OPEN_FAILED
:
1454 (void) printf(gettext("cannot open"));
1457 case VDEV_AUX_BAD_GUID_SUM
:
1458 (void) printf(gettext("missing device"));
1461 case VDEV_AUX_NO_REPLICAS
:
1462 (void) printf(gettext("insufficient replicas"));
1465 case VDEV_AUX_VERSION_NEWER
:
1466 (void) printf(gettext("newer version"));
1469 case VDEV_AUX_UNSUP_FEAT
:
1470 (void) printf(gettext("unsupported feature(s)"));
1473 case VDEV_AUX_SPARED
:
1474 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1476 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1477 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1478 zpool_get_name(zhp
)) == 0)
1479 (void) printf(gettext("currently in "
1482 (void) printf(gettext("in use by "
1484 zpool_get_name(cb
.cb_zhp
));
1485 zpool_close(cb
.cb_zhp
);
1487 (void) printf(gettext("currently in use"));
1491 case VDEV_AUX_ERR_EXCEEDED
:
1492 (void) printf(gettext("too many errors"));
1495 case VDEV_AUX_IO_FAILURE
:
1496 (void) printf(gettext("experienced I/O failures"));
1499 case VDEV_AUX_BAD_LOG
:
1500 (void) printf(gettext("bad intent log"));
1503 case VDEV_AUX_EXTERNAL
:
1504 (void) printf(gettext("external device fault"));
1507 case VDEV_AUX_SPLIT_POOL
:
1508 (void) printf(gettext("split into new pool"));
1512 (void) printf(gettext("corrupted data"));
1517 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1518 (uint64_t **)&ps
, &c
);
1520 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1521 vs
->vs_scan_processed
!= 0 && children
== 0) {
1522 (void) printf(gettext(" (%s)"),
1523 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1524 "resilvering" : "repairing");
1527 (void) printf("\n");
1529 for (c
= 0; c
< children
; c
++) {
1530 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1532 /* Don't print logs or holes here */
1533 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1535 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1537 if (islog
|| ishole
)
1539 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1540 print_status_config(zhp
, vname
, child
[c
],
1541 namewidth
, depth
+ 2, isspare
);
1548 * Print the configuration of an exported pool. Iterate over all vdevs in the
1549 * pool, printing out the name and status for each one.
1552 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1559 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1560 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1561 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1564 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1565 (uint64_t **)&vs
, &c
) == 0);
1567 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1568 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1570 if (vs
->vs_aux
!= 0) {
1573 switch (vs
->vs_aux
) {
1574 case VDEV_AUX_OPEN_FAILED
:
1575 (void) printf(gettext("cannot open"));
1578 case VDEV_AUX_BAD_GUID_SUM
:
1579 (void) printf(gettext("missing device"));
1582 case VDEV_AUX_NO_REPLICAS
:
1583 (void) printf(gettext("insufficient replicas"));
1586 case VDEV_AUX_VERSION_NEWER
:
1587 (void) printf(gettext("newer version"));
1590 case VDEV_AUX_UNSUP_FEAT
:
1591 (void) printf(gettext("unsupported feature(s)"));
1594 case VDEV_AUX_ERR_EXCEEDED
:
1595 (void) printf(gettext("too many errors"));
1599 (void) printf(gettext("corrupted data"));
1603 (void) printf("\n");
1605 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1606 &child
, &children
) != 0)
1609 for (c
= 0; c
< children
; c
++) {
1610 uint64_t is_log
= B_FALSE
;
1612 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1617 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1618 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1622 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1623 &child
, &children
) == 0) {
1624 (void) printf(gettext("\tcache\n"));
1625 for (c
= 0; c
< children
; c
++) {
1626 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1627 (void) printf("\t %s\n", vname
);
1632 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1633 &child
, &children
) == 0) {
1634 (void) printf(gettext("\tspares\n"));
1635 for (c
= 0; c
< children
; c
++) {
1636 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1637 (void) printf("\t %s\n", vname
);
1645 * Logs are recorded as top level vdevs in the main pool child array
1646 * but with "is_log" set to 1. We use either print_status_config() or
1647 * print_import_config() to print the top level logs then any log
1648 * children (eg mirrored slogs) are printed recursively - which
1649 * works because only the top level vdev is marked "is_log"
1652 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1657 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1661 (void) printf(gettext("\tlogs\n"));
1663 for (c
= 0; c
< children
; c
++) {
1664 uint64_t is_log
= B_FALSE
;
1667 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1671 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1673 print_status_config(zhp
, name
, child
[c
], namewidth
,
1676 print_import_config(name
, child
[c
], namewidth
, 2);
1682 * Display the status for the given pool.
1685 show_import(nvlist_t
*config
)
1687 uint64_t pool_state
;
1693 zpool_status_t reason
;
1694 zpool_errata_t errata
;
1700 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1702 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1704 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1706 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1709 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1710 (uint64_t **)&vs
, &vsc
) == 0);
1711 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1713 reason
= zpool_import_status(config
, &msgid
, &errata
);
1715 (void) printf(gettext(" pool: %s\n"), name
);
1716 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1717 (void) printf(gettext(" state: %s"), health
);
1718 if (pool_state
== POOL_STATE_DESTROYED
)
1719 (void) printf(gettext(" (DESTROYED)"));
1720 (void) printf("\n");
1723 case ZPOOL_STATUS_MISSING_DEV_R
:
1724 case ZPOOL_STATUS_MISSING_DEV_NR
:
1725 case ZPOOL_STATUS_BAD_GUID_SUM
:
1726 (void) printf(gettext(" status: One or more devices are "
1727 "missing from the system.\n"));
1730 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1731 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1732 (void) printf(gettext(" status: One or more devices contains "
1733 "corrupted data.\n"));
1736 case ZPOOL_STATUS_CORRUPT_DATA
:
1738 gettext(" status: The pool data is corrupted.\n"));
1741 case ZPOOL_STATUS_OFFLINE_DEV
:
1742 (void) printf(gettext(" status: One or more devices "
1743 "are offlined.\n"));
1746 case ZPOOL_STATUS_CORRUPT_POOL
:
1747 (void) printf(gettext(" status: The pool metadata is "
1751 case ZPOOL_STATUS_VERSION_OLDER
:
1752 (void) printf(gettext(" status: The pool is formatted using a "
1753 "legacy on-disk version.\n"));
1756 case ZPOOL_STATUS_VERSION_NEWER
:
1757 (void) printf(gettext(" status: The pool is formatted using an "
1758 "incompatible version.\n"));
1761 case ZPOOL_STATUS_FEAT_DISABLED
:
1762 (void) printf(gettext(" status: Some supported features are "
1763 "not enabled on the pool.\n"));
1766 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1767 (void) printf(gettext("status: The pool uses the following "
1768 "feature(s) not supported on this sytem:\n"));
1769 zpool_print_unsup_feat(config
);
1772 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1773 (void) printf(gettext("status: The pool can only be accessed "
1774 "in read-only mode on this system. It\n\tcannot be "
1775 "accessed in read-write mode because it uses the "
1776 "following\n\tfeature(s) not supported on this system:\n"));
1777 zpool_print_unsup_feat(config
);
1780 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1781 (void) printf(gettext(" status: The pool was last accessed by "
1782 "another system.\n"));
1785 case ZPOOL_STATUS_FAULTED_DEV_R
:
1786 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1787 (void) printf(gettext(" status: One or more devices are "
1791 case ZPOOL_STATUS_BAD_LOG
:
1792 (void) printf(gettext(" status: An intent log record cannot be "
1796 case ZPOOL_STATUS_RESILVERING
:
1797 (void) printf(gettext(" status: One or more devices were being "
1801 case ZPOOL_STATUS_ERRATA
:
1802 (void) printf(gettext(" status: Errata #%d detected.\n"),
1808 * No other status can be seen when importing pools.
1810 assert(reason
== ZPOOL_STATUS_OK
);
1814 * Print out an action according to the overall state of the pool.
1816 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1817 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1818 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1819 (void) printf(gettext(" action: The pool can be "
1820 "imported using its name or numeric identifier, "
1821 "though\n\tsome features will not be available "
1822 "without an explicit 'zpool upgrade'.\n"));
1823 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1824 (void) printf(gettext(" action: The pool can be "
1825 "imported using its name or numeric "
1826 "identifier and\n\tthe '-f' flag.\n"));
1827 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
1829 case ZPOOL_ERRATA_NONE
:
1832 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
1833 (void) printf(gettext(" action: The pool can "
1834 "be imported using its name or numeric "
1835 "identifier,\n\thowever there is a compat"
1836 "ibility issue which should be corrected"
1837 "\n\tby running 'zpool scrub'\n"));
1840 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
1841 (void) printf(gettext(" action: The pool can"
1842 "not be imported with this version of ZFS "
1843 "due to\n\tan active asynchronous destroy. "
1844 "Revert to an earlier version\n\tand "
1845 "allow the destroy to complete before "
1851 * All errata must contain an action message.
1856 (void) printf(gettext(" action: The pool can be "
1857 "imported using its name or numeric "
1860 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1861 (void) printf(gettext(" action: The pool can be imported "
1862 "despite missing or damaged devices. The\n\tfault "
1863 "tolerance of the pool may be compromised if imported.\n"));
1866 case ZPOOL_STATUS_VERSION_NEWER
:
1867 (void) printf(gettext(" action: The pool cannot be "
1868 "imported. Access the pool on a system running "
1869 "newer\n\tsoftware, or recreate the pool from "
1872 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1873 (void) printf(gettext("action: The pool cannot be "
1874 "imported. Access the pool on a system that "
1875 "supports\n\tthe required feature(s), or recreate "
1876 "the pool from backup.\n"));
1878 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1879 (void) printf(gettext("action: The pool cannot be "
1880 "imported in read-write mode. Import the pool "
1882 "\t\"-o readonly=on\", access the pool on a system "
1883 "that supports the\n\trequired feature(s), or "
1884 "recreate the pool from backup.\n"));
1886 case ZPOOL_STATUS_MISSING_DEV_R
:
1887 case ZPOOL_STATUS_MISSING_DEV_NR
:
1888 case ZPOOL_STATUS_BAD_GUID_SUM
:
1889 (void) printf(gettext(" action: The pool cannot be "
1890 "imported. Attach the missing\n\tdevices and try "
1894 (void) printf(gettext(" action: The pool cannot be "
1895 "imported due to damaged devices or data.\n"));
1899 /* Print the comment attached to the pool. */
1900 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1901 (void) printf(gettext("comment: %s\n"), comment
);
1904 * If the state is "closed" or "can't open", and the aux state
1905 * is "corrupt data":
1907 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1908 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1909 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1910 if (pool_state
== POOL_STATE_DESTROYED
)
1911 (void) printf(gettext("\tThe pool was destroyed, "
1912 "but can be imported using the '-Df' flags.\n"));
1913 else if (pool_state
!= POOL_STATE_EXPORTED
)
1914 (void) printf(gettext("\tThe pool may be active on "
1915 "another system, but can be imported using\n\t"
1916 "the '-f' flag.\n"));
1920 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1923 (void) printf(gettext(" config:\n\n"));
1925 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1929 print_import_config(name
, nvroot
, namewidth
, 0);
1930 if (num_logs(nvroot
) > 0)
1931 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1933 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1934 (void) printf(gettext("\n\tAdditional devices are known to "
1935 "be part of this pool, though their\n\texact "
1936 "configuration cannot be determined.\n"));
1941 * Perform the import for the given configuration. This passes the heavy
1942 * lifting off to zpool_import_props(), and then mounts the datasets contained
1946 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1947 nvlist_t
*props
, int flags
)
1949 zpool_handle_t
*zhp
;
1954 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1957 verify(nvlist_lookup_uint64(config
,
1958 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1959 verify(nvlist_lookup_uint64(config
,
1960 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1961 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1962 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1963 "is formatted using an unsupported ZFS version\n"), name
);
1965 } else if (state
!= POOL_STATE_EXPORTED
&&
1966 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1967 uint64_t hostid
= 0;
1968 unsigned long system_hostid
= get_system_hostid();
1970 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1973 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
) {
1978 verify(nvlist_lookup_string(config
,
1979 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1980 verify(nvlist_lookup_uint64(config
,
1981 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
1983 (void) fprintf(stderr
, gettext("cannot import "
1984 "'%s': pool may be in use from other "
1985 "system, it was last accessed by %s "
1986 "(hostid: 0x%lx) on %s"), name
, hostname
,
1987 (unsigned long)hostid
,
1988 asctime(localtime(&t
)));
1989 (void) fprintf(stderr
, gettext("use '-f' to "
1990 "import anyway\n"));
1995 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
1998 if (newname
!= NULL
)
1999 name
= (char *)newname
;
2001 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2004 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2005 !(flags
& ZFS_IMPORT_ONLY
) &&
2006 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2016 * zpool import [-d dir] [-D]
2017 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2018 * [-d dir | -c cachefile] [-f] -a
2019 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2020 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2022 * -c Read pool information from a cachefile instead of searching
2025 * -d Scan in a specific directory, other than /dev/. More than
2026 * one directory can be specified using multiple '-d' options.
2028 * -D Scan for previously destroyed pools or import all or only
2029 * specified destroyed pools.
2031 * -R Temporarily import the pool, with all mountpoints relative to
2032 * the given root. The pool will remain exported when the machine
2035 * -V Import even in the presence of faulted vdevs. This is an
2036 * intentionally undocumented option for testing purposes, and
2037 * treats the pool configuration as complete, leaving any bad
2038 * vdevs in the FAULTED state. In other words, it does verbatim
2041 * -f Force import, even if it appears that the pool is active.
2043 * -F Attempt rewind if necessary.
2045 * -n See if rewind would work, but don't actually rewind.
2047 * -N Import the pool but don't mount datasets.
2049 * -T Specify a starting txg to use for import. This option is
2050 * intentionally undocumented option for testing purposes.
2052 * -a Import all pools found.
2054 * -o Set property=value and/or temporary mount options (without '=').
2056 * The import command scans for pools to import, and import pools based on pool
2057 * name and GUID. The pool can also be renamed as part of the import process.
2060 zpool_do_import(int argc
, char **argv
)
2062 char **searchdirs
= NULL
;
2063 char *env
, *envdup
= NULL
;
2067 nvlist_t
*pools
= NULL
;
2068 boolean_t do_all
= B_FALSE
;
2069 boolean_t do_destroyed
= B_FALSE
;
2070 char *mntopts
= NULL
;
2073 uint64_t searchguid
= 0;
2074 char *searchname
= NULL
;
2076 nvlist_t
*found_config
;
2077 nvlist_t
*policy
= NULL
;
2078 nvlist_t
*props
= NULL
;
2080 int flags
= ZFS_IMPORT_NORMAL
;
2081 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2082 boolean_t dryrun
= B_FALSE
;
2083 boolean_t do_rewind
= B_FALSE
;
2084 boolean_t xtreme_rewind
= B_FALSE
;
2085 uint64_t pool_state
, txg
= -1ULL;
2086 char *cachefile
= NULL
;
2087 importargs_t idata
= { 0 };
2091 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2100 if (searchdirs
== NULL
) {
2101 searchdirs
= safe_malloc(sizeof (char *));
2103 char **tmp
= safe_malloc((nsearch
+ 1) *
2105 bcopy(searchdirs
, tmp
, nsearch
*
2110 searchdirs
[nsearch
++] = optarg
;
2113 do_destroyed
= B_TRUE
;
2116 flags
|= ZFS_IMPORT_ANY_HOST
;
2122 flags
|= ZFS_IMPORT_MISSING_LOG
;
2128 flags
|= ZFS_IMPORT_ONLY
;
2131 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2134 if (add_prop_list(optarg
, propval
,
2142 if (add_prop_list(zpool_prop_to_name(
2143 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2145 if (add_prop_list_default(zpool_prop_to_name(
2146 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2150 flags
|= ZFS_IMPORT_TEMP_NAME
;
2151 if (add_prop_list_default(zpool_prop_to_name(
2152 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2158 txg
= strtoull(optarg
, &endptr
, 0);
2159 if (errno
!= 0 || *endptr
!= '\0') {
2160 (void) fprintf(stderr
,
2161 gettext("invalid txg value\n"));
2164 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2167 flags
|= ZFS_IMPORT_VERBATIM
;
2170 xtreme_rewind
= B_TRUE
;
2173 (void) fprintf(stderr
, gettext("missing argument for "
2174 "'%c' option\n"), optopt
);
2178 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2187 if (cachefile
&& nsearch
!= 0) {
2188 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2192 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2193 (void) fprintf(stderr
,
2194 gettext("-n or -X only meaningful with -F\n"));
2198 rewind_policy
= ZPOOL_TRY_REWIND
;
2200 rewind_policy
= ZPOOL_DO_REWIND
;
2202 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2204 /* In the future, we can capture further policy and include it here */
2205 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2206 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2207 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2210 /* check argument count */
2213 (void) fprintf(stderr
, gettext("too many arguments\n"));
2218 (void) fprintf(stderr
, gettext("too many arguments\n"));
2223 * Check for the SYS_CONFIG privilege. We do this explicitly
2224 * here because otherwise any attempt to discover pools will
2227 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
2228 (void) fprintf(stderr
, gettext("cannot "
2229 "discover pools: permission denied\n"));
2230 if (searchdirs
!= NULL
)
2233 nvlist_free(policy
);
2239 * Depending on the arguments given, we do one of the following:
2241 * <none> Iterate through all pools and display information about
2244 * -a Iterate through all pools and try to import each one.
2246 * <id> Find the pool that corresponds to the given GUID/pool
2247 * name and import that one.
2249 * -D Above options applies only to destroyed pools.
2255 searchguid
= strtoull(argv
[0], &endptr
, 10);
2256 if (errno
!= 0 || *endptr
!= '\0')
2257 searchname
= argv
[0];
2258 found_config
= NULL
;
2261 * User specified a name or guid. Ensure it's unique.
2263 idata
.unique
= B_TRUE
;
2267 * Check the environment for the preferred search path.
2269 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2272 envdup
= strdup(env
);
2274 dir
= strtok(envdup
, ":");
2275 while (dir
!= NULL
) {
2276 if (searchdirs
== NULL
) {
2277 searchdirs
= safe_malloc(sizeof (char *));
2279 char **tmp
= safe_malloc((nsearch
+ 1) *
2281 bcopy(searchdirs
, tmp
, nsearch
*
2286 searchdirs
[nsearch
++] = dir
;
2287 dir
= strtok(NULL
, ":");
2291 idata
.path
= searchdirs
;
2292 idata
.paths
= nsearch
;
2293 idata
.poolname
= searchname
;
2294 idata
.guid
= searchguid
;
2295 idata
.cachefile
= cachefile
;
2297 pools
= zpool_search_import(g_zfs
, &idata
);
2299 if (pools
!= NULL
&& idata
.exists
&&
2300 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2301 (void) fprintf(stderr
, gettext("cannot import '%s': "
2302 "a pool with that name already exists\n"),
2304 (void) fprintf(stderr
, gettext("use the form '%s "
2305 "<pool | id> <newpool>' to give it a new name\n"),
2308 } else if (pools
== NULL
&& idata
.exists
) {
2309 (void) fprintf(stderr
, gettext("cannot import '%s': "
2310 "a pool with that name is already created/imported,\n"),
2312 (void) fprintf(stderr
, gettext("and no additional pools "
2313 "with that name were found\n"));
2315 } else if (pools
== NULL
) {
2317 (void) fprintf(stderr
, gettext("cannot import '%s': "
2318 "no such pool available\n"), argv
[0]);
2324 if (searchdirs
!= NULL
)
2328 nvlist_free(policy
);
2333 * At this point we have a list of import candidate configs. Even if
2334 * we were searching by pool name or guid, we still need to
2335 * post-process the list to deal with pool state and possible
2341 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2343 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2345 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2347 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2349 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2352 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2359 (void) printf("\n");
2362 err
|= do_import(config
, NULL
, mntopts
,
2365 show_import(config
);
2367 } else if (searchname
!= NULL
) {
2371 * We are searching for a pool based on name.
2373 verify(nvlist_lookup_string(config
,
2374 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2376 if (strcmp(name
, searchname
) == 0) {
2377 if (found_config
!= NULL
) {
2378 (void) fprintf(stderr
, gettext(
2379 "cannot import '%s': more than "
2380 "one matching pool\n"), searchname
);
2381 (void) fprintf(stderr
, gettext(
2382 "import by numeric ID instead\n"));
2385 found_config
= config
;
2391 * Search for a pool by guid.
2393 verify(nvlist_lookup_uint64(config
,
2394 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2396 if (guid
== searchguid
)
2397 found_config
= config
;
2402 * If we were searching for a specific pool, verify that we found a
2403 * pool, and then do the import.
2405 if (argc
!= 0 && err
== 0) {
2406 if (found_config
== NULL
) {
2407 (void) fprintf(stderr
, gettext("cannot import '%s': "
2408 "no such pool available\n"), argv
[0]);
2411 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2412 argv
[1], mntopts
, props
, flags
);
2417 * If we were just looking for pools, report an error if none were
2420 if (argc
== 0 && first
)
2421 (void) fprintf(stderr
,
2422 gettext("no pools available to import\n"));
2427 nvlist_free(policy
);
2428 if (searchdirs
!= NULL
)
2433 return (err
? 1 : 0);
2436 typedef struct iostat_cbdata
{
2437 boolean_t cb_verbose
;
2440 zpool_list_t
*cb_list
;
2444 print_iostat_separator(iostat_cbdata_t
*cb
)
2448 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2450 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2454 print_iostat_header(iostat_cbdata_t
*cb
)
2456 (void) printf("%*s capacity operations bandwidth\n",
2457 cb
->cb_namewidth
, "");
2458 (void) printf("%-*s alloc free read write read write\n",
2459 cb
->cb_namewidth
, "pool");
2460 print_iostat_separator(cb
);
2464 * Display a single statistic.
2467 print_one_stat(uint64_t value
)
2471 zfs_nicenum(value
, buf
, sizeof (buf
));
2472 (void) printf(" %5s", buf
);
2476 * Print out all the statistics for the given vdev. This can either be the
2477 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2478 * is a verbose output, and we don't want to display the toplevel pool stats.
2481 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2482 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2484 nvlist_t
**oldchild
, **newchild
;
2486 vdev_stat_t
*oldvs
, *newvs
;
2487 vdev_stat_t zerovs
= { 0 };
2492 if (oldnv
!= NULL
) {
2493 verify(nvlist_lookup_uint64_array(oldnv
,
2494 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2499 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2500 (uint64_t **)&newvs
, &c
) == 0);
2502 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2503 (void) printf("%*s%s", depth
, "", name
);
2505 (void) printf("%*s%s%*s", depth
, "", name
,
2506 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2508 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2513 scale
= (double)NANOSEC
/ tdelta
;
2515 /* only toplevel vdevs have capacity stats */
2516 if (newvs
->vs_space
== 0) {
2517 (void) printf(" - -");
2519 print_one_stat(newvs
->vs_alloc
);
2520 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2523 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2524 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2526 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2527 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2529 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2530 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2532 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2533 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2535 (void) printf("\n");
2537 if (!cb
->cb_verbose
)
2540 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2541 &newchild
, &children
) != 0)
2544 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2545 &oldchild
, &c
) != 0)
2548 for (c
= 0; c
< children
; c
++) {
2549 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2551 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2554 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2557 if (ishole
|| islog
)
2560 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2561 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2562 newchild
[c
], cb
, depth
+ 2);
2567 * Log device section
2570 if (num_logs(newnv
) > 0) {
2571 (void) printf("%-*s - - - - - "
2572 "-\n", cb
->cb_namewidth
, "logs");
2574 for (c
= 0; c
< children
; c
++) {
2575 uint64_t islog
= B_FALSE
;
2576 (void) nvlist_lookup_uint64(newchild
[c
],
2577 ZPOOL_CONFIG_IS_LOG
, &islog
);
2580 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2582 print_vdev_stats(zhp
, vname
, oldnv
?
2583 oldchild
[c
] : NULL
, newchild
[c
],
2592 * Include level 2 ARC devices in iostat output
2594 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2595 &newchild
, &children
) != 0)
2598 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2599 &oldchild
, &c
) != 0)
2603 (void) printf("%-*s - - - - - "
2604 "-\n", cb
->cb_namewidth
, "cache");
2605 for (c
= 0; c
< children
; c
++) {
2606 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2608 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2609 newchild
[c
], cb
, depth
+ 2);
2616 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2618 iostat_cbdata_t
*cb
= data
;
2622 * If the pool has disappeared, remove it from the list and continue.
2624 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2628 pool_list_remove(cb
->cb_list
, zhp
);
2634 * Callback to print out the iostats for the given pool.
2637 print_iostat(zpool_handle_t
*zhp
, void *data
)
2639 iostat_cbdata_t
*cb
= data
;
2640 nvlist_t
*oldconfig
, *newconfig
;
2641 nvlist_t
*oldnvroot
, *newnvroot
;
2643 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2645 if (cb
->cb_iteration
== 1)
2648 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2651 if (oldconfig
== NULL
)
2654 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2658 * Print out the statistics for the pool.
2660 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2663 print_iostat_separator(cb
);
2675 if (isatty(STDOUT_FILENO
)) {
2676 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
2678 columns
= ws
.ws_col
;
2687 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2689 iostat_cbdata_t
*cb
= data
;
2690 nvlist_t
*config
, *nvroot
;
2693 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2694 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2696 if (!cb
->cb_verbose
)
2697 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2699 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2704 * The width must be at least 10, but may be as large as the
2705 * column width - 42 so that we can still fit in one line.
2707 columns
= get_columns();
2709 if (cb
->cb_namewidth
< 10)
2710 cb
->cb_namewidth
= 10;
2711 if (cb
->cb_namewidth
> columns
- 42)
2712 cb
->cb_namewidth
= columns
- 42;
2718 * Parse the input string, get the 'interval' and 'count' value if there is one.
2721 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2724 unsigned long interval
= 0, count
= 0;
2728 * Determine if the last argument is an integer or a pool name
2730 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2734 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2736 if (*end
== '\0' && errno
== 0) {
2737 if (interval
== 0) {
2738 (void) fprintf(stderr
, gettext("interval "
2739 "cannot be zero\n"));
2743 * Ignore the last parameter
2748 * If this is not a valid number, just plow on. The
2749 * user will get a more informative error message later
2757 * If the last argument is also an integer, then we have both a count
2760 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2765 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2767 if (*end
== '\0' && errno
== 0) {
2768 if (interval
== 0) {
2769 (void) fprintf(stderr
, gettext("interval "
2770 "cannot be zero\n"));
2775 * Ignore the last parameter
2789 get_timestamp_arg(char c
)
2792 timestamp_fmt
= UDATE
;
2794 timestamp_fmt
= DDATE
;
2800 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2802 * -v Display statistics for individual vdevs
2803 * -T Display a timestamp in date(1) or Unix format
2805 * This command can be tricky because we want to be able to deal with pool
2806 * creation/destruction as well as vdev configuration changes. The bulk of this
2807 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2808 * on pool_list_update() to detect the addition of new pools. Configuration
2809 * changes are all handled within libzfs.
2812 zpool_do_iostat(int argc
, char **argv
)
2817 unsigned long interval
= 0, count
= 0;
2819 boolean_t verbose
= B_FALSE
;
2820 boolean_t omit_since_boot
= B_FALSE
;
2824 while ((c
= getopt(argc
, argv
, "T:vy")) != -1) {
2827 get_timestamp_arg(*optarg
);
2833 omit_since_boot
= B_TRUE
;
2836 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2845 get_interval_count(&argc
, argv
, &interval
, &count
);
2848 * Construct the list of all interesting pools.
2851 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2854 if (pool_list_count(list
) == 0 && argc
!= 0) {
2855 pool_list_free(list
);
2859 if (pool_list_count(list
) == 0 && interval
== 0) {
2860 pool_list_free(list
);
2861 (void) fprintf(stderr
, gettext("no pools available\n"));
2866 * Enter the main iostat loop.
2869 cb
.cb_verbose
= verbose
;
2870 cb
.cb_iteration
= 0;
2871 cb
.cb_namewidth
= 0;
2874 if ((npools
= pool_list_count(list
)) == 0)
2875 (void) fprintf(stderr
, gettext("no pools available\n"));
2878 * If this is the first iteration and -y was supplied
2879 * we skip any printing.
2881 boolean_t skip
= (omit_since_boot
&&
2882 cb
.cb_iteration
== 0);
2885 * Refresh all statistics. This is done as an
2886 * explicit step before calculating the maximum name
2887 * width, so that any * configuration changes are
2888 * properly accounted for.
2890 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
2894 * Iterate over all pools to determine the maximum width
2895 * for the pool / device name column across all pools.
2897 cb
.cb_namewidth
= 0;
2898 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
2901 if (timestamp_fmt
!= NODATE
)
2902 print_timestamp(timestamp_fmt
);
2905 * If it's the first time and we're not skipping it,
2906 * or either skip or verbose mode, print the header.
2908 if ((++cb
.cb_iteration
== 1 && !skip
) ||
2910 print_iostat_header(&cb
);
2913 (void) sleep(interval
);
2917 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2920 * If there's more than one pool, and we're not in
2921 * verbose mode (which prints a separator for us),
2922 * then print a separator.
2924 if (npools
> 1 && !verbose
)
2925 print_iostat_separator(&cb
);
2928 (void) printf("\n");
2932 * Flush the output so that redirection to a file isn't buffered
2935 (void) fflush(stdout
);
2940 if (count
!= 0 && --count
== 0)
2943 (void) sleep(interval
);
2946 pool_list_free(list
);
2951 typedef struct list_cbdata
{
2952 boolean_t cb_verbose
;
2954 boolean_t cb_scripted
;
2955 zprop_list_t
*cb_proplist
;
2959 * Given a list of columns to display, output appropriate headers for each one.
2962 print_header(list_cbdata_t
*cb
)
2964 zprop_list_t
*pl
= cb
->cb_proplist
;
2965 char headerbuf
[ZPOOL_MAXPROPLEN
];
2967 boolean_t first
= B_TRUE
;
2968 boolean_t right_justify
;
2971 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2972 width
= pl
->pl_width
;
2973 if (first
&& cb
->cb_verbose
) {
2975 * Reset the width to accommodate the verbose listing
2978 width
= cb
->cb_namewidth
;
2986 right_justify
= B_FALSE
;
2987 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2988 header
= zpool_prop_column_name(pl
->pl_prop
);
2989 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2993 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2994 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2995 headerbuf
[i
] = '\0';
2999 if (pl
->pl_next
== NULL
&& !right_justify
)
3000 (void) printf("%s", header
);
3001 else if (right_justify
)
3002 (void) printf("%*s", (int)width
, header
);
3004 (void) printf("%-*s", (int)width
, header
);
3007 (void) printf("\n");
3011 * Given a pool and a list of properties, print out all the properties according
3012 * to the described layout.
3015 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
3017 zprop_list_t
*pl
= cb
->cb_proplist
;
3018 boolean_t first
= B_TRUE
;
3019 char property
[ZPOOL_MAXPROPLEN
];
3021 boolean_t right_justify
;
3024 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3026 width
= pl
->pl_width
;
3027 if (first
&& cb
->cb_verbose
) {
3029 * Reset the width to accommodate the verbose listing
3032 width
= cb
->cb_namewidth
;
3036 if (cb
->cb_scripted
)
3037 (void) printf("\t");
3044 right_justify
= B_FALSE
;
3045 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3046 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
3047 sizeof (property
), NULL
) != 0)
3052 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3053 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
3054 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
3055 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
3056 sizeof (property
)) == 0) {
3064 * If this is being called in scripted mode, or if this is the
3065 * last column and it is left-justified, don't include a width
3068 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3069 (void) printf("%s", propstr
);
3070 else if (right_justify
)
3071 (void) printf("%*s", (int)width
, propstr
);
3073 (void) printf("%-*s", (int)width
, propstr
);
3076 (void) printf("\n");
3080 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
3085 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3088 case ZPOOL_PROP_EXPANDSZ
:
3090 (void) strlcpy(propval
, "-", sizeof (propval
));
3092 zfs_nicenum(value
, propval
, sizeof (propval
));
3094 case ZPOOL_PROP_FRAGMENTATION
:
3095 if (value
== ZFS_FRAG_INVALID
) {
3096 (void) strlcpy(propval
, "-", sizeof (propval
));
3098 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3099 (unsigned long long)value
);
3102 case ZPOOL_PROP_CAPACITY
:
3103 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3104 (unsigned long long)value
);
3107 zfs_nicenum(value
, propval
, sizeof (propval
));
3111 (void) strlcpy(propval
, "-", sizeof (propval
));
3114 (void) printf("\t%s", propval
);
3116 (void) printf(" %*s", (int)width
, propval
);
3120 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3121 list_cbdata_t
*cb
, int depth
)
3127 boolean_t scripted
= cb
->cb_scripted
;
3129 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3130 (uint64_t **)&vs
, &c
) == 0);
3133 boolean_t toplevel
= (vs
->vs_space
!= 0);
3137 (void) printf("\t%s", name
);
3138 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3139 (void) printf("%*s%s", depth
, "", name
);
3141 (void) printf("%*s%s%*s", depth
, "", name
,
3142 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3145 * Print the properties for the individual vdevs. Some
3146 * properties are only applicable to toplevel vdevs. The
3147 * 'toplevel' boolean value is passed to the print_one_column()
3148 * to indicate that the value is valid.
3150 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
3152 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
3154 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
3155 scripted
, toplevel
);
3156 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
3158 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
3159 vs
->vs_fragmentation
, scripted
,
3160 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
3161 cap
= (vs
->vs_space
== 0) ? 0 :
3162 (vs
->vs_alloc
* 100 / vs
->vs_space
);
3163 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
3164 (void) printf("\n");
3167 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3168 &child
, &children
) != 0)
3171 for (c
= 0; c
< children
; c
++) {
3172 uint64_t ishole
= B_FALSE
;
3174 if (nvlist_lookup_uint64(child
[c
],
3175 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3178 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3179 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3184 * Include level 2 ARC devices in iostat output
3186 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3187 &child
, &children
) != 0)
3191 (void) printf("%-*s - - - - - "
3192 "-\n", cb
->cb_namewidth
, "cache");
3193 for (c
= 0; c
< children
; c
++) {
3194 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3196 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3204 * Generic callback function to list a pool.
3207 list_callback(zpool_handle_t
*zhp
, void *data
)
3209 list_cbdata_t
*cbp
= data
;
3213 config
= zpool_get_config(zhp
, NULL
);
3215 print_pool(zhp
, cbp
);
3216 if (!cbp
->cb_verbose
)
3219 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3221 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3227 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3229 * -H Scripted mode. Don't display headers, and separate properties
3231 * -o List of properties to display. Defaults to
3232 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3233 * "dedupratio,health,altroot"
3234 * -T Display a timestamp in date(1) or Unix format
3236 * List all pools in the system, whether or not they're healthy. Output space
3237 * statistics for each one, as well as health status summary.
3240 zpool_do_list(int argc
, char **argv
)
3244 list_cbdata_t cb
= { 0 };
3245 static char default_props
[] =
3246 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3247 "dedupratio,health,altroot";
3248 char *props
= default_props
;
3249 unsigned long interval
= 0, count
= 0;
3251 boolean_t first
= B_TRUE
;
3254 while ((c
= getopt(argc
, argv
, ":Ho:T:v")) != -1) {
3257 cb
.cb_scripted
= B_TRUE
;
3263 get_timestamp_arg(*optarg
);
3266 cb
.cb_verbose
= B_TRUE
;
3269 (void) fprintf(stderr
, gettext("missing argument for "
3270 "'%c' option\n"), optopt
);
3274 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3283 get_interval_count(&argc
, argv
, &interval
, &count
);
3285 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3289 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
3293 if (pool_list_count(list
) == 0)
3296 if (timestamp_fmt
!= NODATE
)
3297 print_timestamp(timestamp_fmt
);
3299 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3303 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3308 if (count
!= 0 && --count
== 0)
3311 pool_list_free(list
);
3312 (void) sleep(interval
);
3315 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3316 (void) printf(gettext("no pools available\n"));
3320 pool_list_free(list
);
3321 zprop_free_list(cb
.cb_proplist
);
3326 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3328 boolean_t force
= B_FALSE
;
3331 char *poolname
, *old_disk
, *new_disk
;
3332 zpool_handle_t
*zhp
;
3333 nvlist_t
*props
= NULL
;
3338 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
3344 if ((propval
= strchr(optarg
, '=')) == NULL
) {
3345 (void) fprintf(stderr
, gettext("missing "
3346 "'=' for -o option\n"));
3352 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
3353 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
3357 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3366 /* get pool name and check number of arguments */
3368 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3375 (void) fprintf(stderr
,
3376 gettext("missing <device> specification\n"));
3384 (void) fprintf(stderr
,
3385 gettext("missing <new_device> specification\n"));
3388 new_disk
= old_disk
;
3398 (void) fprintf(stderr
, gettext("too many arguments\n"));
3402 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3405 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3406 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3412 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
3414 if (nvroot
== NULL
) {
3419 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3421 nvlist_free(nvroot
);
3428 * zpool replace [-f] <pool> <device> <new_device>
3430 * -f Force attach, even if <new_device> appears to be in use.
3432 * Replace <device> with <new_device>.
3436 zpool_do_replace(int argc
, char **argv
)
3438 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3442 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3444 * -f Force attach, even if <new_device> appears to be in use.
3445 * -o Set property=value.
3447 * Attach <new_device> to the mirror containing <device>. If <device> is not
3448 * part of a mirror, then <device> will be transformed into a mirror of
3449 * <device> and <new_device>. In either case, <new_device> will begin life
3450 * with a DTL of [0, now], and will immediately begin to resilver itself.
3453 zpool_do_attach(int argc
, char **argv
)
3455 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3459 * zpool detach [-f] <pool> <device>
3461 * -f Force detach of <device>, even if DTLs argue against it
3462 * (not supported yet)
3464 * Detach a device from a mirror. The operation will be refused if <device>
3465 * is the last device in the mirror, or if the DTLs indicate that this device
3466 * has the only valid copy of some data.
3470 zpool_do_detach(int argc
, char **argv
)
3473 char *poolname
, *path
;
3474 zpool_handle_t
*zhp
;
3478 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3482 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3491 /* get pool name and check number of arguments */
3493 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3498 (void) fprintf(stderr
,
3499 gettext("missing <device> specification\n"));
3506 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3509 ret
= zpool_vdev_detach(zhp
, path
);
3517 * zpool split [-n] [-o prop=val] ...
3519 * [-R altroot] <pool> <newpool> [<device> ...]
3521 * -n Do not split the pool, but display the resulting layout if
3522 * it were to be split.
3523 * -o Set property=value, or set mount options.
3524 * -R Mount the split-off pool under an alternate root.
3526 * Splits the named pool and gives it the new pool name. Devices to be split
3527 * off may be listed, provided that no more than one device is specified
3528 * per top-level vdev mirror. The newly split pool is left in an exported
3529 * state unless -R is specified.
3531 * Restrictions: the top-level of the pool pool must only be made up of
3532 * mirrors; all devices in the pool must be healthy; no device may be
3533 * undergoing a resilvering operation.
3536 zpool_do_split(int argc
, char **argv
)
3538 char *srcpool
, *newpool
, *propval
;
3539 char *mntopts
= NULL
;
3542 zpool_handle_t
*zhp
;
3543 nvlist_t
*config
, *props
= NULL
;
3545 flags
.dryrun
= B_FALSE
;
3546 flags
.import
= B_FALSE
;
3549 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3552 flags
.import
= B_TRUE
;
3554 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3555 &props
, B_TRUE
) != 0) {
3562 flags
.dryrun
= B_TRUE
;
3565 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3568 if (add_prop_list(optarg
, propval
,
3569 &props
, B_TRUE
) != 0) {
3579 (void) fprintf(stderr
, gettext("missing argument for "
3580 "'%c' option\n"), optopt
);
3584 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3591 if (!flags
.import
&& mntopts
!= NULL
) {
3592 (void) fprintf(stderr
, gettext("setting mntopts is only "
3593 "valid when importing the pool\n"));
3601 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3605 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3615 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3618 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3619 if (config
== NULL
) {
3623 (void) printf(gettext("would create '%s' with the "
3624 "following layout:\n\n"), newpool
);
3625 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3627 nvlist_free(config
);
3632 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3636 * The split was successful. Now we need to open the new
3637 * pool and import it.
3639 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3641 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3642 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3644 (void) fprintf(stderr
, gettext("Split was successful, but "
3645 "the datasets could not all be mounted\n"));
3646 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3647 "different altroot\n"), "zpool import");
3657 * zpool online <pool> <device> ...
3660 zpool_do_online(int argc
, char **argv
)
3664 zpool_handle_t
*zhp
;
3666 vdev_state_t newstate
;
3670 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3673 flags
|= ZFS_ONLINE_EXPAND
;
3677 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3686 /* get pool name and check number of arguments */
3688 (void) fprintf(stderr
, gettext("missing pool name\n"));
3692 (void) fprintf(stderr
, gettext("missing device name\n"));
3698 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3701 for (i
= 1; i
< argc
; i
++) {
3702 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3703 if (newstate
!= VDEV_STATE_HEALTHY
) {
3704 (void) printf(gettext("warning: device '%s' "
3705 "onlined, but remains in faulted state\n"),
3707 if (newstate
== VDEV_STATE_FAULTED
)
3708 (void) printf(gettext("use 'zpool "
3709 "clear' to restore a faulted "
3712 (void) printf(gettext("use 'zpool "
3713 "replace' to replace devices "
3714 "that are no longer present\n"));
3727 * zpool offline [-ft] <pool> <device> ...
3729 * -f Force the device into the offline state, even if doing
3730 * so would appear to compromise pool availability.
3731 * (not supported yet)
3733 * -t Only take the device off-line temporarily. The offline
3734 * state will not be persistent across reboots.
3738 zpool_do_offline(int argc
, char **argv
)
3742 zpool_handle_t
*zhp
;
3744 boolean_t istmp
= B_FALSE
;
3747 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3754 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3763 /* get pool name and check number of arguments */
3765 (void) fprintf(stderr
, gettext("missing pool name\n"));
3769 (void) fprintf(stderr
, gettext("missing device name\n"));
3775 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3778 for (i
= 1; i
< argc
; i
++) {
3779 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3789 * zpool clear <pool> [device]
3791 * Clear all errors associated with a pool or a particular device.
3794 zpool_do_clear(int argc
, char **argv
)
3798 boolean_t dryrun
= B_FALSE
;
3799 boolean_t do_rewind
= B_FALSE
;
3800 boolean_t xtreme_rewind
= B_FALSE
;
3801 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3802 nvlist_t
*policy
= NULL
;
3803 zpool_handle_t
*zhp
;
3804 char *pool
, *device
;
3807 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3816 xtreme_rewind
= B_TRUE
;
3819 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3829 (void) fprintf(stderr
, gettext("missing pool name\n"));
3834 (void) fprintf(stderr
, gettext("too many arguments\n"));
3838 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3839 (void) fprintf(stderr
,
3840 gettext("-n or -X only meaningful with -F\n"));
3844 rewind_policy
= ZPOOL_TRY_REWIND
;
3846 rewind_policy
= ZPOOL_DO_REWIND
;
3848 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3850 /* In future, further rewind policy choices can be passed along here */
3851 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3852 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3856 device
= argc
== 2 ? argv
[1] : NULL
;
3858 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3859 nvlist_free(policy
);
3863 if (zpool_clear(zhp
, device
, policy
) != 0)
3868 nvlist_free(policy
);
3874 * zpool reguid <pool>
3877 zpool_do_reguid(int argc
, char **argv
)
3881 zpool_handle_t
*zhp
;
3885 while ((c
= getopt(argc
, argv
, "")) != -1) {
3888 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3897 /* get pool name and check number of arguments */
3899 (void) fprintf(stderr
, gettext("missing pool name\n"));
3904 (void) fprintf(stderr
, gettext("too many arguments\n"));
3909 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3912 ret
= zpool_reguid(zhp
);
3920 * zpool reopen <pool>
3922 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3925 zpool_do_reopen(int argc
, char **argv
)
3929 zpool_handle_t
*zhp
;
3933 while ((c
= getopt(argc
, argv
, "")) != -1) {
3936 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3946 (void) fprintf(stderr
, gettext("missing pool name\n"));
3951 (void) fprintf(stderr
, gettext("too many arguments\n"));
3956 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3959 ret
= zpool_reopen(zhp
);
3964 typedef struct scrub_cbdata
{
3971 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3973 scrub_cbdata_t
*cb
= data
;
3977 * Ignore faulted pools.
3979 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3980 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3981 "currently unavailable\n"), zpool_get_name(zhp
));
3985 err
= zpool_scan(zhp
, cb
->cb_type
);
3991 * zpool scrub [-s] <pool> ...
3993 * -s Stop. Stops any in-progress scrub.
3996 zpool_do_scrub(int argc
, char **argv
)
4001 cb
.cb_type
= POOL_SCAN_SCRUB
;
4004 while ((c
= getopt(argc
, argv
, "s")) != -1) {
4007 cb
.cb_type
= POOL_SCAN_NONE
;
4010 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4022 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4026 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
4029 typedef struct status_cbdata
{
4031 boolean_t cb_allpools
;
4032 boolean_t cb_verbose
;
4033 boolean_t cb_explain
;
4035 boolean_t cb_dedup_stats
;
4039 * Print out detailed scrub status.
4042 print_scan_status(pool_scan_stat_t
*ps
)
4045 uint64_t elapsed
, mins_left
, hours_left
;
4046 uint64_t pass_exam
, examined
, total
;
4048 double fraction_done
;
4049 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4051 (void) printf(gettext(" scan: "));
4053 /* If there's never been a scan, there's not much to say. */
4054 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
4055 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
4056 (void) printf(gettext("none requested\n"));
4060 start
= ps
->pss_start_time
;
4061 end
= ps
->pss_end_time
;
4062 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
4064 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
4065 ps
->pss_func
== POOL_SCAN_RESILVER
);
4067 * Scan is finished or canceled.
4069 if (ps
->pss_state
== DSS_FINISHED
) {
4070 uint64_t minutes_taken
= (end
- start
) / 60;
4073 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4074 fmt
= gettext("scrub repaired %s in %lluh%um with "
4075 "%llu errors on %s");
4076 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4077 fmt
= gettext("resilvered %s in %lluh%um with "
4078 "%llu errors on %s");
4081 (void) printf(fmt
, processed_buf
,
4082 (u_longlong_t
)(minutes_taken
/ 60),
4083 (uint_t
)(minutes_taken
% 60),
4084 (u_longlong_t
)ps
->pss_errors
,
4085 ctime((time_t *)&end
));
4087 } else if (ps
->pss_state
== DSS_CANCELED
) {
4088 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4089 (void) printf(gettext("scrub canceled on %s"),
4091 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4092 (void) printf(gettext("resilver canceled on %s"),
4098 assert(ps
->pss_state
== DSS_SCANNING
);
4101 * Scan is in progress.
4103 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4104 (void) printf(gettext("scrub in progress since %s"),
4106 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4107 (void) printf(gettext("resilver in progress since %s"),
4111 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
4112 total
= ps
->pss_to_examine
;
4113 fraction_done
= (double)examined
/ total
;
4115 /* elapsed time for this pass */
4116 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4117 elapsed
= elapsed
? elapsed
: 1;
4118 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4119 rate
= pass_exam
/ elapsed
;
4120 rate
= rate
? rate
: 1;
4121 mins_left
= ((total
- examined
) / rate
) / 60;
4122 hours_left
= mins_left
/ 60;
4124 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4125 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4126 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4129 * do not print estimated time if hours_left is more than 30 days
4131 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4132 examined_buf
, total_buf
, rate_buf
);
4133 if (hours_left
< (30 * 24)) {
4134 (void) printf(gettext(", %lluh%um to go\n"),
4135 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4137 (void) printf(gettext(
4138 ", (scan is slow, no estimated time)\n"));
4141 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4142 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4143 processed_buf
, 100 * fraction_done
);
4144 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4145 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4146 processed_buf
, 100 * fraction_done
);
4151 print_error_log(zpool_handle_t
*zhp
)
4153 nvlist_t
*nverrlist
= NULL
;
4156 size_t len
= MAXPATHLEN
* 2;
4158 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4159 (void) printf("errors: List of errors unavailable "
4160 "(insufficient privileges)\n");
4164 (void) printf("errors: Permanent errors have been "
4165 "detected in the following files:\n\n");
4167 pathname
= safe_malloc(len
);
4169 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4171 uint64_t dsobj
, obj
;
4173 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4174 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4176 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4178 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4179 (void) printf("%7s %s\n", "", pathname
);
4182 nvlist_free(nverrlist
);
4186 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4195 (void) printf(gettext("\tspares\n"));
4197 for (i
= 0; i
< nspares
; i
++) {
4198 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
4199 print_status_config(zhp
, name
, spares
[i
],
4200 namewidth
, 2, B_TRUE
);
4206 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4215 (void) printf(gettext("\tcache\n"));
4217 for (i
= 0; i
< nl2cache
; i
++) {
4218 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
4219 print_status_config(zhp
, name
, l2cache
[i
],
4220 namewidth
, 2, B_FALSE
);
4226 print_dedup_stats(nvlist_t
*config
)
4228 ddt_histogram_t
*ddh
;
4234 * If the pool was faulted then we may not have been able to
4235 * obtain the config. Otherwise, if we have anything in the dedup
4236 * table continue processing the stats.
4238 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4239 (uint64_t **)&ddo
, &c
) != 0)
4242 (void) printf("\n");
4243 (void) printf(gettext(" dedup: "));
4244 if (ddo
->ddo_count
== 0) {
4245 (void) printf(gettext("no DDT entries\n"));
4249 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4250 (u_longlong_t
)ddo
->ddo_count
,
4251 (u_longlong_t
)ddo
->ddo_dspace
,
4252 (u_longlong_t
)ddo
->ddo_mspace
);
4254 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4255 (uint64_t **)&dds
, &c
) == 0);
4256 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4257 (uint64_t **)&ddh
, &c
) == 0);
4258 zpool_dump_ddt(dds
, ddh
);
4262 * Display a summary of pool status. Displays a summary such as:
4266 * reason: One or more devices ...
4267 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4273 * When given the '-v' option, we print out the complete config. If the '-e'
4274 * option is specified, then we print out error rate information as well.
4277 status_callback(zpool_handle_t
*zhp
, void *data
)
4279 status_cbdata_t
*cbp
= data
;
4280 nvlist_t
*config
, *nvroot
;
4282 zpool_status_t reason
;
4283 zpool_errata_t errata
;
4288 config
= zpool_get_config(zhp
, NULL
);
4289 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
4294 * If we were given 'zpool status -x', only report those pools with
4297 if (cbp
->cb_explain
&&
4298 (reason
== ZPOOL_STATUS_OK
||
4299 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4300 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4301 if (!cbp
->cb_allpools
) {
4302 (void) printf(gettext("pool '%s' is healthy\n"),
4303 zpool_get_name(zhp
));
4305 cbp
->cb_first
= B_FALSE
;
4311 cbp
->cb_first
= B_FALSE
;
4313 (void) printf("\n");
4315 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4317 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4318 (uint64_t **)&vs
, &c
) == 0);
4319 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4321 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4322 (void) printf(gettext(" state: %s\n"), health
);
4325 case ZPOOL_STATUS_MISSING_DEV_R
:
4326 (void) printf(gettext("status: One or more devices could not "
4327 "be opened. Sufficient replicas exist for\n\tthe pool to "
4328 "continue functioning in a degraded state.\n"));
4329 (void) printf(gettext("action: Attach the missing device and "
4330 "online it using 'zpool online'.\n"));
4333 case ZPOOL_STATUS_MISSING_DEV_NR
:
4334 (void) printf(gettext("status: One or more devices could not "
4335 "be opened. There are insufficient\n\treplicas for the "
4336 "pool to continue functioning.\n"));
4337 (void) printf(gettext("action: Attach the missing device and "
4338 "online it using 'zpool online'.\n"));
4341 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4342 (void) printf(gettext("status: One or more devices could not "
4343 "be used because the label is missing or\n\tinvalid. "
4344 "Sufficient replicas exist for the pool to continue\n\t"
4345 "functioning in a degraded state.\n"));
4346 (void) printf(gettext("action: Replace the device using "
4347 "'zpool replace'.\n"));
4350 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4351 (void) printf(gettext("status: One or more devices could not "
4352 "be used because the label is missing \n\tor invalid. "
4353 "There are insufficient replicas for the pool to "
4354 "continue\n\tfunctioning.\n"));
4355 zpool_explain_recover(zpool_get_handle(zhp
),
4356 zpool_get_name(zhp
), reason
, config
);
4359 case ZPOOL_STATUS_FAILING_DEV
:
4360 (void) printf(gettext("status: One or more devices has "
4361 "experienced an unrecoverable error. An\n\tattempt was "
4362 "made to correct the error. Applications are "
4364 (void) printf(gettext("action: Determine if the device needs "
4365 "to be replaced, and clear the errors\n\tusing "
4366 "'zpool clear' or replace the device with 'zpool "
4370 case ZPOOL_STATUS_OFFLINE_DEV
:
4371 (void) printf(gettext("status: One or more devices has "
4372 "been taken offline by the administrator.\n\tSufficient "
4373 "replicas exist for the pool to continue functioning in "
4374 "a\n\tdegraded state.\n"));
4375 (void) printf(gettext("action: Online the device using "
4376 "'zpool online' or replace the device with\n\t'zpool "
4380 case ZPOOL_STATUS_REMOVED_DEV
:
4381 (void) printf(gettext("status: One or more devices has "
4382 "been removed by the administrator.\n\tSufficient "
4383 "replicas exist for the pool to continue functioning in "
4384 "a\n\tdegraded state.\n"));
4385 (void) printf(gettext("action: Online the device using "
4386 "'zpool online' or replace the device with\n\t'zpool "
4390 case ZPOOL_STATUS_RESILVERING
:
4391 (void) printf(gettext("status: One or more devices is "
4392 "currently being resilvered. The pool will\n\tcontinue "
4393 "to function, possibly in a degraded state.\n"));
4394 (void) printf(gettext("action: Wait for the resilver to "
4398 case ZPOOL_STATUS_CORRUPT_DATA
:
4399 (void) printf(gettext("status: One or more devices has "
4400 "experienced an error resulting in data\n\tcorruption. "
4401 "Applications may be affected.\n"));
4402 (void) printf(gettext("action: Restore the file in question "
4403 "if possible. Otherwise restore the\n\tentire pool from "
4407 case ZPOOL_STATUS_CORRUPT_POOL
:
4408 (void) printf(gettext("status: The pool metadata is corrupted "
4409 "and the pool cannot be opened.\n"));
4410 zpool_explain_recover(zpool_get_handle(zhp
),
4411 zpool_get_name(zhp
), reason
, config
);
4414 case ZPOOL_STATUS_VERSION_OLDER
:
4415 (void) printf(gettext("status: The pool is formatted using a "
4416 "legacy on-disk format. The pool can\n\tstill be used, "
4417 "but some features are unavailable.\n"));
4418 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4419 "upgrade'. Once this is done, the\n\tpool will no longer "
4420 "be accessible on software that does not support\n\t"
4421 "feature flags.\n"));
4424 case ZPOOL_STATUS_VERSION_NEWER
:
4425 (void) printf(gettext("status: The pool has been upgraded to a "
4426 "newer, incompatible on-disk version.\n\tThe pool cannot "
4427 "be accessed on this system.\n"));
4428 (void) printf(gettext("action: Access the pool from a system "
4429 "running more recent software, or\n\trestore the pool from "
4433 case ZPOOL_STATUS_FEAT_DISABLED
:
4434 (void) printf(gettext("status: Some supported features are not "
4435 "enabled on the pool. The pool can\n\tstill be used, but "
4436 "some features are unavailable.\n"));
4437 (void) printf(gettext("action: Enable all features using "
4438 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4439 "longer be accessible by software that does not support\n\t"
4440 "the features. See zpool-features(5) for details.\n"));
4443 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4444 (void) printf(gettext("status: The pool cannot be accessed on "
4445 "this system because it uses the\n\tfollowing feature(s) "
4446 "not supported on this system:\n"));
4447 zpool_print_unsup_feat(config
);
4448 (void) printf("\n");
4449 (void) printf(gettext("action: Access the pool from a system "
4450 "that supports the required feature(s),\n\tor restore the "
4451 "pool from backup.\n"));
4454 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4455 (void) printf(gettext("status: The pool can only be accessed "
4456 "in read-only mode on this system. It\n\tcannot be "
4457 "accessed in read-write mode because it uses the "
4458 "following\n\tfeature(s) not supported on this system:\n"));
4459 zpool_print_unsup_feat(config
);
4460 (void) printf("\n");
4461 (void) printf(gettext("action: The pool cannot be accessed in "
4462 "read-write mode. Import the pool with\n"
4463 "\t\"-o readonly=on\", access the pool from a system that "
4464 "supports the\n\trequired feature(s), or restore the "
4465 "pool from backup.\n"));
4468 case ZPOOL_STATUS_FAULTED_DEV_R
:
4469 (void) printf(gettext("status: One or more devices are "
4470 "faulted in response to persistent errors.\n\tSufficient "
4471 "replicas exist for the pool to continue functioning "
4472 "in a\n\tdegraded state.\n"));
4473 (void) printf(gettext("action: Replace the faulted device, "
4474 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4477 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4478 (void) printf(gettext("status: One or more devices are "
4479 "faulted in response to persistent errors. There are "
4480 "insufficient replicas for the pool to\n\tcontinue "
4482 (void) printf(gettext("action: Destroy and re-create the pool "
4483 "from a backup source. Manually marking the device\n"
4484 "\trepaired using 'zpool clear' may allow some data "
4485 "to be recovered.\n"));
4488 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4489 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4490 (void) printf(gettext("status: One or more devices are "
4491 "faulted in response to IO failures.\n"));
4492 (void) printf(gettext("action: Make sure the affected devices "
4493 "are connected, then run 'zpool clear'.\n"));
4496 case ZPOOL_STATUS_BAD_LOG
:
4497 (void) printf(gettext("status: An intent log record "
4498 "could not be read.\n"
4499 "\tWaiting for adminstrator intervention to fix the "
4500 "faulted pool.\n"));
4501 (void) printf(gettext("action: Either restore the affected "
4502 "device(s) and run 'zpool online',\n"
4503 "\tor ignore the intent log records by running "
4504 "'zpool clear'.\n"));
4507 case ZPOOL_STATUS_HOSTID_MISMATCH
:
4508 (void) printf(gettext("status: Mismatch between pool hostid "
4509 "and system hostid on imported pool.\n\tThis pool was "
4510 "previously imported into a system with a different "
4511 "hostid,\n\tand then was verbatim imported into this "
4513 (void) printf(gettext("action: Export this pool on all systems "
4514 "on which it is imported.\n"
4515 "\tThen import it to correct the mismatch.\n"));
4518 case ZPOOL_STATUS_ERRATA
:
4519 (void) printf(gettext("status: Errata #%d detected.\n"),
4523 case ZPOOL_ERRATA_NONE
:
4526 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
4527 (void) printf(gettext("action: To correct the issue "
4528 "run 'zpool scrub'.\n"));
4533 * All errata which allow the pool to be imported
4534 * must contain an action message.
4542 * The remaining errors can't actually be generated, yet.
4544 assert(reason
== ZPOOL_STATUS_OK
);
4548 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4551 if (config
!= NULL
) {
4554 nvlist_t
**spares
, **l2cache
;
4555 uint_t nspares
, nl2cache
;
4556 pool_scan_stat_t
*ps
= NULL
;
4558 (void) nvlist_lookup_uint64_array(nvroot
,
4559 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4560 print_scan_status(ps
);
4562 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4566 (void) printf(gettext("config:\n\n"));
4567 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4568 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4569 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4570 namewidth
, 0, B_FALSE
);
4572 if (num_logs(nvroot
) > 0)
4573 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4574 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4575 &l2cache
, &nl2cache
) == 0)
4576 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4578 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4579 &spares
, &nspares
) == 0)
4580 print_spares(zhp
, spares
, nspares
, namewidth
);
4582 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4584 nvlist_t
*nverrlist
= NULL
;
4587 * If the approximate error count is small, get a
4588 * precise count by fetching the entire log and
4589 * uniquifying the results.
4591 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4592 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4597 while ((elem
= nvlist_next_nvpair(nverrlist
,
4602 nvlist_free(nverrlist
);
4604 (void) printf("\n");
4607 (void) printf(gettext("errors: No known data "
4609 else if (!cbp
->cb_verbose
)
4610 (void) printf(gettext("errors: %llu data "
4611 "errors, use '-v' for a list\n"),
4612 (u_longlong_t
)nerr
);
4614 print_error_log(zhp
);
4617 if (cbp
->cb_dedup_stats
)
4618 print_dedup_stats(config
);
4620 (void) printf(gettext("config: The configuration cannot be "
4628 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4630 * -v Display complete error logs
4631 * -x Display only pools with potential problems
4632 * -D Display dedup status (undocumented)
4633 * -T Display a timestamp in date(1) or Unix format
4635 * Describes the health status of all pools or some subset.
4638 zpool_do_status(int argc
, char **argv
)
4642 unsigned long interval
= 0, count
= 0;
4643 status_cbdata_t cb
= { 0 };
4646 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4649 cb
.cb_verbose
= B_TRUE
;
4652 cb
.cb_explain
= B_TRUE
;
4655 cb
.cb_dedup_stats
= B_TRUE
;
4658 get_timestamp_arg(*optarg
);
4661 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4670 get_interval_count(&argc
, argv
, &interval
, &count
);
4673 cb
.cb_allpools
= B_TRUE
;
4675 cb
.cb_first
= B_TRUE
;
4678 if (timestamp_fmt
!= NODATE
)
4679 print_timestamp(timestamp_fmt
);
4681 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4682 status_callback
, &cb
);
4684 if (argc
== 0 && cb
.cb_count
== 0)
4685 (void) fprintf(stderr
, gettext("no pools available\n"));
4686 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4687 (void) printf(gettext("all pools are healthy\n"));
4695 if (count
!= 0 && --count
== 0)
4698 (void) sleep(interval
);
4704 typedef struct upgrade_cbdata
{
4707 uint64_t cb_version
;
4712 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
4714 int zfs_version
= (int) zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
4715 int *count
= (int *)unsupp_fs
;
4717 if (zfs_version
> ZPL_VERSION
) {
4718 (void) printf(gettext("%s (v%d) is not supported by this "
4719 "implementation of ZFS.\n"),
4720 zfs_get_name(zhp
), zfs_version
);
4724 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
4732 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4736 uint64_t oldversion
;
4739 config
= zpool_get_config(zhp
, NULL
);
4740 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4743 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4744 assert(oldversion
< version
);
4746 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
4751 (void) fprintf(stderr
, gettext("Upgrade not performed due "
4752 "to %d unsupported filesystems (max v%d).\n"),
4753 unsupp_fs
, (int) ZPL_VERSION
);
4757 ret
= zpool_upgrade(zhp
, version
);
4761 if (version
>= SPA_VERSION_FEATURES
) {
4762 (void) printf(gettext("Successfully upgraded "
4763 "'%s' from version %llu to feature flags.\n"),
4764 zpool_get_name(zhp
), (u_longlong_t
) oldversion
);
4766 (void) printf(gettext("Successfully upgraded "
4767 "'%s' from version %llu to version %llu.\n"),
4768 zpool_get_name(zhp
), (u_longlong_t
) oldversion
,
4769 (u_longlong_t
) version
);
4776 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4779 boolean_t firstff
= B_TRUE
;
4780 nvlist_t
*enabled
= zpool_get_features(zhp
);
4783 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4784 const char *fname
= spa_feature_table
[i
].fi_uname
;
4785 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4786 if (!nvlist_exists(enabled
, fguid
)) {
4788 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4789 ret
= zpool_set_prop(zhp
, propname
,
4790 ZFS_FEATURE_ENABLED
);
4798 (void) printf(gettext("Enabled the "
4799 "following features on '%s':\n"),
4800 zpool_get_name(zhp
));
4803 (void) printf(gettext(" %s\n"), fname
);
4814 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4816 upgrade_cbdata_t
*cbp
= arg
;
4819 boolean_t printnl
= B_FALSE
;
4822 config
= zpool_get_config(zhp
, NULL
);
4823 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4826 assert(SPA_VERSION_IS_SUPPORTED(version
));
4828 if (version
< cbp
->cb_version
) {
4829 cbp
->cb_first
= B_FALSE
;
4830 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4836 * If they did "zpool upgrade -a", then we could
4837 * be doing ioctls to different pools. We need
4838 * to log this history once to each pool, and bypass
4839 * the normal history logging that happens in main().
4841 (void) zpool_log_history(g_zfs
, history_str
);
4842 log_history
= B_FALSE
;
4845 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4847 ret
= upgrade_enable_all(zhp
, &count
);
4852 cbp
->cb_first
= B_FALSE
;
4858 (void) printf(gettext("\n"));
4865 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4867 upgrade_cbdata_t
*cbp
= arg
;
4871 config
= zpool_get_config(zhp
, NULL
);
4872 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4875 assert(SPA_VERSION_IS_SUPPORTED(version
));
4877 if (version
< SPA_VERSION_FEATURES
) {
4878 if (cbp
->cb_first
) {
4879 (void) printf(gettext("The following pools are "
4880 "formatted with legacy version numbers and can\n"
4881 "be upgraded to use feature flags. After "
4882 "being upgraded, these pools\nwill no "
4883 "longer be accessible by software that does not "
4884 "support feature\nflags.\n\n"));
4885 (void) printf(gettext("VER POOL\n"));
4886 (void) printf(gettext("--- ------------\n"));
4887 cbp
->cb_first
= B_FALSE
;
4890 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4891 zpool_get_name(zhp
));
4898 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4900 upgrade_cbdata_t
*cbp
= arg
;
4904 config
= zpool_get_config(zhp
, NULL
);
4905 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4908 if (version
>= SPA_VERSION_FEATURES
) {
4910 boolean_t poolfirst
= B_TRUE
;
4911 nvlist_t
*enabled
= zpool_get_features(zhp
);
4913 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4914 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4915 const char *fname
= spa_feature_table
[i
].fi_uname
;
4916 if (!nvlist_exists(enabled
, fguid
)) {
4917 if (cbp
->cb_first
) {
4918 (void) printf(gettext("\nSome "
4919 "supported features are not "
4920 "enabled on the following pools. "
4921 "Once a\nfeature is enabled the "
4922 "pool may become incompatible with "
4923 "software\nthat does not support "
4925 "zpool-features(5) for "
4927 (void) printf(gettext("POOL "
4929 (void) printf(gettext("------"
4931 cbp
->cb_first
= B_FALSE
;
4935 (void) printf(gettext("%s\n"),
4936 zpool_get_name(zhp
));
4937 poolfirst
= B_FALSE
;
4940 (void) printf(gettext(" %s\n"), fname
);
4943 * If they did "zpool upgrade -a", then we could
4944 * be doing ioctls to different pools. We need
4945 * to log this history once to each pool, and bypass
4946 * the normal history logging that happens in main().
4948 (void) zpool_log_history(g_zfs
, history_str
);
4949 log_history
= B_FALSE
;
4958 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4960 boolean_t printnl
= B_FALSE
;
4961 upgrade_cbdata_t
*cbp
= data
;
4962 uint64_t cur_version
;
4965 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4966 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
4967 "Pool 'log' must be renamed using export and import"
4972 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4973 if (cur_version
> cbp
->cb_version
) {
4974 (void) printf(gettext("Pool '%s' is already formatted "
4975 "using more current version '%llu'.\n\n"),
4976 zpool_get_name(zhp
), (u_longlong_t
) cur_version
);
4980 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
4981 (void) printf(gettext("Pool '%s' is already formatted "
4982 "using version %llu.\n\n"), zpool_get_name(zhp
),
4983 (u_longlong_t
) cbp
->cb_version
);
4987 if (cur_version
!= cbp
->cb_version
) {
4989 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4994 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4996 ret
= upgrade_enable_all(zhp
, &count
);
5002 } else if (cur_version
== SPA_VERSION
) {
5003 (void) printf(gettext("Pool '%s' already has all "
5004 "supported features enabled.\n"),
5005 zpool_get_name(zhp
));
5010 (void) printf(gettext("\n"));
5019 * zpool upgrade [-V version] <-a | pool ...>
5021 * With no arguments, display downrev'd ZFS pool available for upgrade.
5022 * Individual pools can be upgraded by specifying the pool, and '-a' will
5023 * upgrade all pools.
5026 zpool_do_upgrade(int argc
, char **argv
)
5029 upgrade_cbdata_t cb
= { 0 };
5031 boolean_t showversions
= B_FALSE
;
5032 boolean_t upgradeall
= B_FALSE
;
5037 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
5040 upgradeall
= B_TRUE
;
5043 showversions
= B_TRUE
;
5046 cb
.cb_version
= strtoll(optarg
, &end
, 10);
5048 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
5049 (void) fprintf(stderr
,
5050 gettext("invalid version '%s'\n"), optarg
);
5055 (void) fprintf(stderr
, gettext("missing argument for "
5056 "'%c' option\n"), optopt
);
5060 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5071 if (cb
.cb_version
== 0) {
5072 cb
.cb_version
= SPA_VERSION
;
5073 } else if (!upgradeall
&& argc
== 0) {
5074 (void) fprintf(stderr
, gettext("-V option is "
5075 "incompatible with other arguments\n"));
5080 if (upgradeall
|| argc
!= 0) {
5081 (void) fprintf(stderr
, gettext("-v option is "
5082 "incompatible with other arguments\n"));
5085 } else if (upgradeall
) {
5087 (void) fprintf(stderr
, gettext("-a option should not "
5088 "be used along with a pool name\n"));
5093 (void) printf(gettext("This system supports ZFS pool feature "
5098 (void) printf(gettext("The following features are "
5100 (void) printf(gettext("FEAT DESCRIPTION\n"));
5101 (void) printf("----------------------------------------------"
5102 "---------------\n");
5103 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5104 zfeature_info_t
*fi
= &spa_feature_table
[i
];
5105 const char *ro
= fi
->fi_can_readonly
?
5106 " (read-only compatible)" : "";
5108 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
5109 (void) printf(" %s\n", fi
->fi_desc
);
5111 (void) printf("\n");
5113 (void) printf(gettext("The following legacy versions are also "
5115 (void) printf(gettext("VER DESCRIPTION\n"));
5116 (void) printf("--- -----------------------------------------"
5117 "---------------\n");
5118 (void) printf(gettext(" 1 Initial ZFS version\n"));
5119 (void) printf(gettext(" 2 Ditto blocks "
5120 "(replicated metadata)\n"));
5121 (void) printf(gettext(" 3 Hot spares and double parity "
5123 (void) printf(gettext(" 4 zpool history\n"));
5124 (void) printf(gettext(" 5 Compression using the gzip "
5126 (void) printf(gettext(" 6 bootfs pool property\n"));
5127 (void) printf(gettext(" 7 Separate intent log devices\n"));
5128 (void) printf(gettext(" 8 Delegated administration\n"));
5129 (void) printf(gettext(" 9 refquota and refreservation "
5131 (void) printf(gettext(" 10 Cache devices\n"));
5132 (void) printf(gettext(" 11 Improved scrub performance\n"));
5133 (void) printf(gettext(" 12 Snapshot properties\n"));
5134 (void) printf(gettext(" 13 snapused property\n"));
5135 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5136 (void) printf(gettext(" 15 user/group space accounting\n"));
5137 (void) printf(gettext(" 16 stmf property support\n"));
5138 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5139 (void) printf(gettext(" 18 Snapshot user holds\n"));
5140 (void) printf(gettext(" 19 Log device removal\n"));
5141 (void) printf(gettext(" 20 Compression using zle "
5142 "(zero-length encoding)\n"));
5143 (void) printf(gettext(" 21 Deduplication\n"));
5144 (void) printf(gettext(" 22 Received properties\n"));
5145 (void) printf(gettext(" 23 Slim ZIL\n"));
5146 (void) printf(gettext(" 24 System attributes\n"));
5147 (void) printf(gettext(" 25 Improved scrub stats\n"));
5148 (void) printf(gettext(" 26 Improved snapshot deletion "
5150 (void) printf(gettext(" 27 Improved snapshot creation "
5152 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5153 (void) printf(gettext("\nFor more information on a particular "
5154 "version, including supported releases,\n"));
5155 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5156 } else if (argc
== 0 && upgradeall
) {
5157 cb
.cb_first
= B_TRUE
;
5158 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5159 if (ret
== 0 && cb
.cb_first
) {
5160 if (cb
.cb_version
== SPA_VERSION
) {
5161 (void) printf(gettext("All pools are already "
5162 "formatted using feature flags.\n\n"));
5163 (void) printf(gettext("Every feature flags "
5164 "pool already has all supported features "
5167 (void) printf(gettext("All pools are already "
5168 "formatted with version %llu or higher.\n"),
5169 (u_longlong_t
) cb
.cb_version
);
5172 } else if (argc
== 0) {
5173 cb
.cb_first
= B_TRUE
;
5174 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5178 (void) printf(gettext("All pools are formatted "
5179 "using feature flags.\n\n"));
5181 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5182 "for a list of available legacy versions.\n"));
5185 cb
.cb_first
= B_TRUE
;
5186 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5190 (void) printf(gettext("Every feature flags pool has "
5191 "all supported features enabled.\n"));
5193 (void) printf(gettext("\n"));
5196 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5203 typedef struct hist_cbdata
{
5210 * Print out the command history for a specific pool.
5213 get_history_one(zpool_handle_t
*zhp
, void *data
)
5219 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5221 cb
->first
= B_FALSE
;
5223 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5225 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5228 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5229 &records
, &numrecords
) == 0);
5230 for (i
= 0; i
< numrecords
; i
++) {
5231 nvlist_t
*rec
= records
[i
];
5234 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5238 tsec
= fnvlist_lookup_uint64(records
[i
],
5240 (void) localtime_r(&tsec
, &t
);
5241 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5244 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5245 (void) printf("%s %s", tbuf
,
5246 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5247 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5249 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5252 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5253 (void) printf("%s unrecognized record:\n",
5255 dump_nvlist(rec
, 4);
5258 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5259 zfs_history_event_names
[ievent
],
5260 (longlong_t
) fnvlist_lookup_uint64(
5261 rec
, ZPOOL_HIST_TXG
),
5262 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5263 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5266 (void) printf("%s [txg:%lld] %s", tbuf
,
5267 (longlong_t
) fnvlist_lookup_uint64(
5268 rec
, ZPOOL_HIST_TXG
),
5269 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5270 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5271 (void) printf(" %s (%llu)",
5272 fnvlist_lookup_string(rec
,
5274 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
5277 (void) printf(" %s", fnvlist_lookup_string(rec
,
5278 ZPOOL_HIST_INT_STR
));
5279 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5282 (void) printf("%s ioctl %s\n", tbuf
,
5283 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5284 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5285 (void) printf(" input:\n");
5286 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5287 ZPOOL_HIST_INPUT_NVL
), 8);
5289 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5290 (void) printf(" output:\n");
5291 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5292 ZPOOL_HIST_OUTPUT_NVL
), 8);
5297 (void) printf("%s unrecognized record:\n", tbuf
);
5298 dump_nvlist(rec
, 4);
5302 (void) printf("\n");
5305 (void) printf(" [");
5306 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5307 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5308 struct passwd
*pwd
= getpwuid(who
);
5309 (void) printf("user %d ", (int)who
);
5311 (void) printf("(%s) ", pwd
->pw_name
);
5313 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5314 (void) printf("on %s",
5315 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5317 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5318 (void) printf(":%s",
5319 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5323 (void) printf("\n");
5325 (void) printf("\n");
5332 * zpool history <pool>
5334 * Displays the history of commands that modified pools.
5337 zpool_do_history(int argc
, char **argv
)
5339 hist_cbdata_t cbdata
= { 0 };
5343 cbdata
.first
= B_TRUE
;
5345 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5348 cbdata
.longfmt
= B_TRUE
;
5351 cbdata
.internal
= B_TRUE
;
5354 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5362 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5365 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5366 (void) fprintf(stderr
, gettext("no pools available\n"));
5373 typedef struct ev_opts
{
5381 zpool_do_events_short(nvlist_t
*nvl
)
5383 char ctime_str
[26], str
[32], *ptr
;
5387 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
5388 memset(str
, ' ', 32);
5389 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
5390 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
5391 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
5392 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
5393 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
5394 (void) printf(gettext("%s "), str
);
5396 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
5397 (void) printf(gettext("%s\n"), ptr
);
5401 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
5405 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
5406 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
5408 data_type_t type
= nvpair_type(nvp
);
5409 const char *name
= nvpair_name(nvp
);
5419 printf(gettext("%*s%s = "), depth
, "", name
);
5422 case DATA_TYPE_BOOLEAN
:
5423 printf(gettext("%s"), "1");
5426 case DATA_TYPE_BOOLEAN_VALUE
:
5427 (void) nvpair_value_boolean_value(nvp
, &b
);
5428 printf(gettext("%s"), b
? "1" : "0");
5431 case DATA_TYPE_BYTE
:
5432 (void) nvpair_value_byte(nvp
, &i8
);
5433 printf(gettext("0x%x"), i8
);
5436 case DATA_TYPE_INT8
:
5437 (void) nvpair_value_int8(nvp
, (void *)&i8
);
5438 printf(gettext("0x%x"), i8
);
5441 case DATA_TYPE_UINT8
:
5442 (void) nvpair_value_uint8(nvp
, &i8
);
5443 printf(gettext("0x%x"), i8
);
5446 case DATA_TYPE_INT16
:
5447 (void) nvpair_value_int16(nvp
, (void *)&i16
);
5448 printf(gettext("0x%x"), i16
);
5451 case DATA_TYPE_UINT16
:
5452 (void) nvpair_value_uint16(nvp
, &i16
);
5453 printf(gettext("0x%x"), i16
);
5456 case DATA_TYPE_INT32
:
5457 (void) nvpair_value_int32(nvp
, (void *)&i32
);
5458 printf(gettext("0x%x"), i32
);
5461 case DATA_TYPE_UINT32
:
5462 (void) nvpair_value_uint32(nvp
, &i32
);
5463 printf(gettext("0x%x"), i32
);
5466 case DATA_TYPE_INT64
:
5467 (void) nvpair_value_int64(nvp
, (void *)&i64
);
5468 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5471 case DATA_TYPE_UINT64
:
5472 (void) nvpair_value_uint64(nvp
, &i64
);
5473 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5476 case DATA_TYPE_HRTIME
:
5477 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
5478 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5481 case DATA_TYPE_STRING
:
5482 (void) nvpair_value_string(nvp
, &str
);
5483 printf(gettext("\"%s\""), str
? str
: "<NULL>");
5486 case DATA_TYPE_NVLIST
:
5487 printf(gettext("(embedded nvlist)\n"));
5488 (void) nvpair_value_nvlist(nvp
, &cnv
);
5489 zpool_do_events_nvprint(cnv
, depth
+ 8);
5490 printf(gettext("%*s(end %s)"), depth
, "", name
);
5493 case DATA_TYPE_NVLIST_ARRAY
: {
5497 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
5498 printf(gettext("(%d embedded nvlists)\n"), nelem
);
5499 for (i
= 0; i
< nelem
; i
++) {
5500 printf(gettext("%*s%s[%d] = %s\n"),
5501 depth
, "", name
, i
, "(embedded nvlist)");
5502 zpool_do_events_nvprint(val
[i
], depth
+ 8);
5503 printf(gettext("%*s(end %s[%i])\n"),
5504 depth
, "", name
, i
);
5506 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
5510 case DATA_TYPE_INT8_ARRAY
: {
5514 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
5515 for (i
= 0; i
< nelem
; i
++)
5516 printf(gettext("0x%x "), val
[i
]);
5521 case DATA_TYPE_UINT8_ARRAY
: {
5525 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
5526 for (i
= 0; i
< nelem
; i
++)
5527 printf(gettext("0x%x "), val
[i
]);
5532 case DATA_TYPE_INT16_ARRAY
: {
5536 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
5537 for (i
= 0; i
< nelem
; i
++)
5538 printf(gettext("0x%x "), val
[i
]);
5543 case DATA_TYPE_UINT16_ARRAY
: {
5547 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
5548 for (i
= 0; i
< nelem
; i
++)
5549 printf(gettext("0x%x "), val
[i
]);
5554 case DATA_TYPE_INT32_ARRAY
: {
5558 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
5559 for (i
= 0; i
< nelem
; i
++)
5560 printf(gettext("0x%x "), val
[i
]);
5565 case DATA_TYPE_UINT32_ARRAY
: {
5569 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
5570 for (i
= 0; i
< nelem
; i
++)
5571 printf(gettext("0x%x "), val
[i
]);
5576 case DATA_TYPE_INT64_ARRAY
: {
5580 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
5581 for (i
= 0; i
< nelem
; i
++)
5582 printf(gettext("0x%llx "),
5583 (u_longlong_t
)val
[i
]);
5588 case DATA_TYPE_UINT64_ARRAY
: {
5592 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
5593 for (i
= 0; i
< nelem
; i
++)
5594 printf(gettext("0x%llx "),
5595 (u_longlong_t
)val
[i
]);
5600 case DATA_TYPE_STRING_ARRAY
: {
5604 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
5605 for (i
= 0; i
< nelem
; i
++)
5606 printf(gettext("\"%s\" "),
5607 str
[i
] ? str
[i
] : "<NULL>");
5612 case DATA_TYPE_BOOLEAN_ARRAY
:
5613 case DATA_TYPE_BYTE_ARRAY
:
5614 case DATA_TYPE_DOUBLE
:
5615 case DATA_TYPE_UNKNOWN
:
5616 printf(gettext("<unknown>"));
5620 printf(gettext("\n"));
5625 zpool_do_events_next(ev_opts_t
*opts
)
5628 int zevent_fd
, ret
, dropped
;
5630 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
5631 VERIFY(zevent_fd
>= 0);
5633 if (!opts
->scripted
)
5634 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5637 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
5638 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
5639 if (ret
|| nvl
== NULL
)
5643 (void) printf(gettext("dropped %d events\n"), dropped
);
5645 zpool_do_events_short(nvl
);
5647 if (opts
->verbose
) {
5648 zpool_do_events_nvprint(nvl
, 8);
5649 printf(gettext("\n"));
5651 (void) fflush(stdout
);
5656 VERIFY(0 == close(zevent_fd
));
5662 zpool_do_events_clear(ev_opts_t
*opts
)
5666 ret
= zpool_events_clear(g_zfs
, &count
);
5668 (void) printf(gettext("cleared %d events\n"), count
);
5674 * zpool events [-vfc]
5676 * Displays events logs by ZFS.
5679 zpool_do_events(int argc
, char **argv
)
5681 ev_opts_t opts
= { 0 };
5686 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
5701 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5710 ret
= zpool_do_events_clear(&opts
);
5712 ret
= zpool_do_events_next(&opts
);
5718 get_callback(zpool_handle_t
*zhp
, void *data
)
5720 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5721 char value
[MAXNAMELEN
];
5722 zprop_source_t srctype
;
5725 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5728 * Skip the special fake placeholder. This will also skip
5729 * over the name property when 'all' is specified.
5731 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5732 pl
== cbp
->cb_proplist
)
5735 if (pl
->pl_prop
== ZPROP_INVAL
&&
5736 (zpool_prop_feature(pl
->pl_user_prop
) ||
5737 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5738 srctype
= ZPROP_SRC_LOCAL
;
5740 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5741 value
, sizeof (value
)) == 0) {
5742 zprop_print_one_property(zpool_get_name(zhp
),
5743 cbp
, pl
->pl_user_prop
, value
, srctype
,
5747 if (zpool_get_prop_literal(zhp
, pl
->pl_prop
, value
,
5748 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5751 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5752 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5760 zpool_do_get(int argc
, char **argv
)
5762 zprop_get_cbdata_t cb
= { 0 };
5763 zprop_list_t fake_name
= { 0 };
5767 while ((c
= getopt(argc
, argv
, "pH")) != -1) {
5770 cb
.cb_literal
= B_TRUE
;
5774 cb
.cb_scripted
= B_TRUE
;
5778 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5788 (void) fprintf(stderr
, gettext("missing property "
5793 cb
.cb_first
= B_TRUE
;
5794 cb
.cb_sources
= ZPROP_SRC_ALL
;
5795 cb
.cb_columns
[0] = GET_COL_NAME
;
5796 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5797 cb
.cb_columns
[2] = GET_COL_VALUE
;
5798 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5799 cb
.cb_type
= ZFS_TYPE_POOL
;
5801 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5807 if (cb
.cb_proplist
!= NULL
) {
5808 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5809 fake_name
.pl_width
= strlen(gettext("NAME"));
5810 fake_name
.pl_next
= cb
.cb_proplist
;
5811 cb
.cb_proplist
= &fake_name
;
5814 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5817 if (cb
.cb_proplist
== &fake_name
)
5818 zprop_free_list(fake_name
.pl_next
);
5820 zprop_free_list(cb
.cb_proplist
);
5825 typedef struct set_cbdata
{
5828 boolean_t cb_any_successful
;
5832 set_callback(zpool_handle_t
*zhp
, void *data
)
5835 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5837 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5840 cb
->cb_any_successful
= B_TRUE
;
5846 zpool_do_set(int argc
, char **argv
)
5848 set_cbdata_t cb
= { 0 };
5851 if (argc
> 1 && argv
[1][0] == '-') {
5852 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5858 (void) fprintf(stderr
, gettext("missing property=value "
5864 (void) fprintf(stderr
, gettext("missing pool name\n"));
5869 (void) fprintf(stderr
, gettext("too many pool names\n"));
5873 cb
.cb_propname
= argv
[1];
5874 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5875 if (cb
.cb_value
== NULL
) {
5876 (void) fprintf(stderr
, gettext("missing value in "
5877 "property=value argument\n"));
5881 *(cb
.cb_value
) = '\0';
5884 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5891 find_command_idx(char *command
, int *idx
)
5895 for (i
= 0; i
< NCOMMAND
; i
++) {
5896 if (command_table
[i
].name
== NULL
)
5899 if (strcmp(command
, command_table
[i
].name
) == 0) {
5908 main(int argc
, char **argv
)
5914 (void) setlocale(LC_ALL
, "");
5915 (void) textdomain(TEXT_DOMAIN
);
5917 dprintf_setup(&argc
, argv
);
5922 * Make sure the user has specified some command.
5925 (void) fprintf(stderr
, gettext("missing command\n"));
5934 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
5937 if ((g_zfs
= libzfs_init()) == NULL
) {
5938 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
5942 libzfs_print_on_error(g_zfs
, B_TRUE
);
5944 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5947 * Run the appropriate command.
5949 if (find_command_idx(cmdname
, &i
) == 0) {
5950 current_command
= &command_table
[i
];
5951 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5952 } else if (strchr(cmdname
, '=')) {
5953 verify(find_command_idx("set", &i
) == 0);
5954 current_command
= &command_table
[i
];
5955 ret
= command_table
[i
].func(argc
, argv
);
5956 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5958 * 'freeze' is a vile debugging abomination, so we treat
5962 int fd
= open(ZFS_DEV
, O_RDWR
);
5963 (void) strcpy((void *)buf
, argv
[2]);
5964 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5966 (void) fprintf(stderr
, gettext("unrecognized "
5967 "command '%s'\n"), cmdname
);
5972 if (ret
== 0 && log_history
)
5973 (void) zpool_log_history(g_zfs
, history_str
);
5978 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5979 * for the purposes of running ::findleaks.
5981 if (getenv("ZFS_ABORT") != NULL
) {
5982 (void) printf("dumping core by request\n");