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] [pool] ... [interval "
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
;
2823 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2826 get_timestamp_arg(*optarg
);
2832 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2841 get_interval_count(&argc
, argv
, &interval
, &count
);
2844 * Construct the list of all interesting pools.
2847 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2850 if (pool_list_count(list
) == 0 && argc
!= 0) {
2851 pool_list_free(list
);
2855 if (pool_list_count(list
) == 0 && interval
== 0) {
2856 pool_list_free(list
);
2857 (void) fprintf(stderr
, gettext("no pools available\n"));
2862 * Enter the main iostat loop.
2865 cb
.cb_verbose
= verbose
;
2866 cb
.cb_iteration
= 0;
2867 cb
.cb_namewidth
= 0;
2870 pool_list_update(list
);
2872 if ((npools
= pool_list_count(list
)) == 0)
2873 (void) fprintf(stderr
, gettext("no pools available\n"));
2876 * Refresh all statistics. This is done as an
2877 * explicit step before calculating the maximum name
2878 * width, so that any * configuration changes are
2879 * properly accounted for.
2881 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
2885 * Iterate over all pools to determine the maximum width
2886 * for the pool / device name column across all pools.
2888 cb
.cb_namewidth
= 0;
2889 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
2892 if (timestamp_fmt
!= NODATE
)
2893 print_timestamp(timestamp_fmt
);
2896 * If it's the first time, or verbose mode, print the
2899 if (++cb
.cb_iteration
== 1 || verbose
)
2900 print_iostat_header(&cb
);
2902 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2905 * If there's more than one pool, and we're not in
2906 * verbose mode (which prints a separator for us),
2907 * then print a separator.
2909 if (npools
> 1 && !verbose
)
2910 print_iostat_separator(&cb
);
2913 (void) printf("\n");
2917 * Flush the output so that redirection to a file isn't buffered
2920 (void) fflush(stdout
);
2925 if (count
!= 0 && --count
== 0)
2928 (void) sleep(interval
);
2931 pool_list_free(list
);
2936 typedef struct list_cbdata
{
2937 boolean_t cb_verbose
;
2939 boolean_t cb_scripted
;
2940 zprop_list_t
*cb_proplist
;
2944 * Given a list of columns to display, output appropriate headers for each one.
2947 print_header(list_cbdata_t
*cb
)
2949 zprop_list_t
*pl
= cb
->cb_proplist
;
2950 char headerbuf
[ZPOOL_MAXPROPLEN
];
2952 boolean_t first
= B_TRUE
;
2953 boolean_t right_justify
;
2956 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2957 width
= pl
->pl_width
;
2958 if (first
&& cb
->cb_verbose
) {
2960 * Reset the width to accommodate the verbose listing
2963 width
= cb
->cb_namewidth
;
2971 right_justify
= B_FALSE
;
2972 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2973 header
= zpool_prop_column_name(pl
->pl_prop
);
2974 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2978 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2979 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2980 headerbuf
[i
] = '\0';
2984 if (pl
->pl_next
== NULL
&& !right_justify
)
2985 (void) printf("%s", header
);
2986 else if (right_justify
)
2987 (void) printf("%*s", (int)width
, header
);
2989 (void) printf("%-*s", (int)width
, header
);
2992 (void) printf("\n");
2996 * Given a pool and a list of properties, print out all the properties according
2997 * to the described layout.
3000 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
3002 zprop_list_t
*pl
= cb
->cb_proplist
;
3003 boolean_t first
= B_TRUE
;
3004 char property
[ZPOOL_MAXPROPLEN
];
3006 boolean_t right_justify
;
3009 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3011 width
= pl
->pl_width
;
3012 if (first
&& cb
->cb_verbose
) {
3014 * Reset the width to accommodate the verbose listing
3017 width
= cb
->cb_namewidth
;
3021 if (cb
->cb_scripted
)
3022 (void) printf("\t");
3029 right_justify
= B_FALSE
;
3030 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3031 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
3032 sizeof (property
), NULL
) != 0)
3037 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3038 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
3039 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
3040 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
3041 sizeof (property
)) == 0) {
3049 * If this is being called in scripted mode, or if this is the
3050 * last column and it is left-justified, don't include a width
3053 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3054 (void) printf("%s", propstr
);
3055 else if (right_justify
)
3056 (void) printf("%*s", (int)width
, propstr
);
3058 (void) printf("%-*s", (int)width
, propstr
);
3061 (void) printf("\n");
3065 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
3070 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3073 case ZPOOL_PROP_EXPANDSZ
:
3075 (void) strlcpy(propval
, "-", sizeof (propval
));
3077 zfs_nicenum(value
, propval
, sizeof (propval
));
3079 case ZPOOL_PROP_FRAGMENTATION
:
3080 if (value
== ZFS_FRAG_INVALID
) {
3081 (void) strlcpy(propval
, "-", sizeof (propval
));
3083 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3084 (unsigned long long)value
);
3087 case ZPOOL_PROP_CAPACITY
:
3088 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3089 (unsigned long long)value
);
3092 zfs_nicenum(value
, propval
, sizeof (propval
));
3096 (void) strlcpy(propval
, "-", sizeof (propval
));
3099 (void) printf("\t%s", propval
);
3101 (void) printf(" %*s", (int)width
, propval
);
3105 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3106 list_cbdata_t
*cb
, int depth
)
3112 boolean_t scripted
= cb
->cb_scripted
;
3114 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3115 (uint64_t **)&vs
, &c
) == 0);
3118 boolean_t toplevel
= (vs
->vs_space
!= 0);
3122 (void) printf("\t%s", name
);
3123 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3124 (void) printf("%*s%s", depth
, "", name
);
3126 (void) printf("%*s%s%*s", depth
, "", name
,
3127 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3130 * Print the properties for the individual vdevs. Some
3131 * properties are only applicable to toplevel vdevs. The
3132 * 'toplevel' boolean value is passed to the print_one_column()
3133 * to indicate that the value is valid.
3135 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
3137 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
3139 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
3140 scripted
, toplevel
);
3141 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
3143 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
3144 vs
->vs_fragmentation
, scripted
,
3145 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
3146 cap
= (vs
->vs_space
== 0) ? 0 :
3147 (vs
->vs_alloc
* 100 / vs
->vs_space
);
3148 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
3149 (void) printf("\n");
3152 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3153 &child
, &children
) != 0)
3156 for (c
= 0; c
< children
; c
++) {
3157 uint64_t ishole
= B_FALSE
;
3159 if (nvlist_lookup_uint64(child
[c
],
3160 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3163 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3164 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3169 * Include level 2 ARC devices in iostat output
3171 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3172 &child
, &children
) != 0)
3176 (void) printf("%-*s - - - - - "
3177 "-\n", cb
->cb_namewidth
, "cache");
3178 for (c
= 0; c
< children
; c
++) {
3179 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3181 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3189 * Generic callback function to list a pool.
3192 list_callback(zpool_handle_t
*zhp
, void *data
)
3194 list_cbdata_t
*cbp
= data
;
3198 config
= zpool_get_config(zhp
, NULL
);
3200 print_pool(zhp
, cbp
);
3201 if (!cbp
->cb_verbose
)
3204 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3206 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3212 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3214 * -H Scripted mode. Don't display headers, and separate properties
3216 * -o List of properties to display. Defaults to
3217 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3218 * "dedupratio,health,altroot"
3219 * -T Display a timestamp in date(1) or Unix format
3221 * List all pools in the system, whether or not they're healthy. Output space
3222 * statistics for each one, as well as health status summary.
3225 zpool_do_list(int argc
, char **argv
)
3229 list_cbdata_t cb
= { 0 };
3230 static char default_props
[] =
3231 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3232 "dedupratio,health,altroot";
3233 char *props
= default_props
;
3234 unsigned long interval
= 0, count
= 0;
3236 boolean_t first
= B_TRUE
;
3239 while ((c
= getopt(argc
, argv
, ":Ho:T:v")) != -1) {
3242 cb
.cb_scripted
= B_TRUE
;
3248 get_timestamp_arg(*optarg
);
3251 cb
.cb_verbose
= B_TRUE
;
3254 (void) fprintf(stderr
, gettext("missing argument for "
3255 "'%c' option\n"), optopt
);
3259 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3268 get_interval_count(&argc
, argv
, &interval
, &count
);
3270 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3273 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
, &ret
)) == NULL
)
3276 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3277 (void) printf(gettext("no pools available\n"));
3278 zprop_free_list(cb
.cb_proplist
);
3283 pool_list_update(list
);
3285 if (pool_list_count(list
) == 0)
3288 if (timestamp_fmt
!= NODATE
)
3289 print_timestamp(timestamp_fmt
);
3291 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3295 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3300 if (count
!= 0 && --count
== 0)
3303 (void) sleep(interval
);
3306 zprop_free_list(cb
.cb_proplist
);
3311 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3313 boolean_t force
= B_FALSE
;
3316 char *poolname
, *old_disk
, *new_disk
;
3317 zpool_handle_t
*zhp
;
3318 nvlist_t
*props
= NULL
;
3323 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
3329 if ((propval
= strchr(optarg
, '=')) == NULL
) {
3330 (void) fprintf(stderr
, gettext("missing "
3331 "'=' for -o option\n"));
3337 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
3338 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
3342 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3351 /* get pool name and check number of arguments */
3353 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3360 (void) fprintf(stderr
,
3361 gettext("missing <device> specification\n"));
3369 (void) fprintf(stderr
,
3370 gettext("missing <new_device> specification\n"));
3373 new_disk
= old_disk
;
3383 (void) fprintf(stderr
, gettext("too many arguments\n"));
3387 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3390 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3391 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3397 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
3399 if (nvroot
== NULL
) {
3404 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3406 nvlist_free(nvroot
);
3413 * zpool replace [-f] <pool> <device> <new_device>
3415 * -f Force attach, even if <new_device> appears to be in use.
3417 * Replace <device> with <new_device>.
3421 zpool_do_replace(int argc
, char **argv
)
3423 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3427 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3429 * -f Force attach, even if <new_device> appears to be in use.
3430 * -o Set property=value.
3432 * Attach <new_device> to the mirror containing <device>. If <device> is not
3433 * part of a mirror, then <device> will be transformed into a mirror of
3434 * <device> and <new_device>. In either case, <new_device> will begin life
3435 * with a DTL of [0, now], and will immediately begin to resilver itself.
3438 zpool_do_attach(int argc
, char **argv
)
3440 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3444 * zpool detach [-f] <pool> <device>
3446 * -f Force detach of <device>, even if DTLs argue against it
3447 * (not supported yet)
3449 * Detach a device from a mirror. The operation will be refused if <device>
3450 * is the last device in the mirror, or if the DTLs indicate that this device
3451 * has the only valid copy of some data.
3455 zpool_do_detach(int argc
, char **argv
)
3458 char *poolname
, *path
;
3459 zpool_handle_t
*zhp
;
3463 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3467 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3476 /* get pool name and check number of arguments */
3478 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3483 (void) fprintf(stderr
,
3484 gettext("missing <device> specification\n"));
3491 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3494 ret
= zpool_vdev_detach(zhp
, path
);
3502 * zpool split [-n] [-o prop=val] ...
3504 * [-R altroot] <pool> <newpool> [<device> ...]
3506 * -n Do not split the pool, but display the resulting layout if
3507 * it were to be split.
3508 * -o Set property=value, or set mount options.
3509 * -R Mount the split-off pool under an alternate root.
3511 * Splits the named pool and gives it the new pool name. Devices to be split
3512 * off may be listed, provided that no more than one device is specified
3513 * per top-level vdev mirror. The newly split pool is left in an exported
3514 * state unless -R is specified.
3516 * Restrictions: the top-level of the pool pool must only be made up of
3517 * mirrors; all devices in the pool must be healthy; no device may be
3518 * undergoing a resilvering operation.
3521 zpool_do_split(int argc
, char **argv
)
3523 char *srcpool
, *newpool
, *propval
;
3524 char *mntopts
= NULL
;
3527 zpool_handle_t
*zhp
;
3528 nvlist_t
*config
, *props
= NULL
;
3530 flags
.dryrun
= B_FALSE
;
3531 flags
.import
= B_FALSE
;
3534 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3537 flags
.import
= B_TRUE
;
3539 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3540 &props
, B_TRUE
) != 0) {
3547 flags
.dryrun
= B_TRUE
;
3550 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3553 if (add_prop_list(optarg
, propval
,
3554 &props
, B_TRUE
) != 0) {
3564 (void) fprintf(stderr
, gettext("missing argument for "
3565 "'%c' option\n"), optopt
);
3569 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3576 if (!flags
.import
&& mntopts
!= NULL
) {
3577 (void) fprintf(stderr
, gettext("setting mntopts is only "
3578 "valid when importing the pool\n"));
3586 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3590 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3600 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3603 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3604 if (config
== NULL
) {
3608 (void) printf(gettext("would create '%s' with the "
3609 "following layout:\n\n"), newpool
);
3610 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3612 nvlist_free(config
);
3617 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3621 * The split was successful. Now we need to open the new
3622 * pool and import it.
3624 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3626 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3627 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3629 (void) fprintf(stderr
, gettext("Split was successful, but "
3630 "the datasets could not all be mounted\n"));
3631 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3632 "different altroot\n"), "zpool import");
3642 * zpool online <pool> <device> ...
3645 zpool_do_online(int argc
, char **argv
)
3649 zpool_handle_t
*zhp
;
3651 vdev_state_t newstate
;
3655 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3658 flags
|= ZFS_ONLINE_EXPAND
;
3662 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3671 /* get pool name and check number of arguments */
3673 (void) fprintf(stderr
, gettext("missing pool name\n"));
3677 (void) fprintf(stderr
, gettext("missing device name\n"));
3683 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3686 for (i
= 1; i
< argc
; i
++) {
3687 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3688 if (newstate
!= VDEV_STATE_HEALTHY
) {
3689 (void) printf(gettext("warning: device '%s' "
3690 "onlined, but remains in faulted state\n"),
3692 if (newstate
== VDEV_STATE_FAULTED
)
3693 (void) printf(gettext("use 'zpool "
3694 "clear' to restore a faulted "
3697 (void) printf(gettext("use 'zpool "
3698 "replace' to replace devices "
3699 "that are no longer present\n"));
3712 * zpool offline [-ft] <pool> <device> ...
3714 * -f Force the device into the offline state, even if doing
3715 * so would appear to compromise pool availability.
3716 * (not supported yet)
3718 * -t Only take the device off-line temporarily. The offline
3719 * state will not be persistent across reboots.
3723 zpool_do_offline(int argc
, char **argv
)
3727 zpool_handle_t
*zhp
;
3729 boolean_t istmp
= B_FALSE
;
3732 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3739 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3748 /* get pool name and check number of arguments */
3750 (void) fprintf(stderr
, gettext("missing pool name\n"));
3754 (void) fprintf(stderr
, gettext("missing device name\n"));
3760 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3763 for (i
= 1; i
< argc
; i
++) {
3764 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3774 * zpool clear <pool> [device]
3776 * Clear all errors associated with a pool or a particular device.
3779 zpool_do_clear(int argc
, char **argv
)
3783 boolean_t dryrun
= B_FALSE
;
3784 boolean_t do_rewind
= B_FALSE
;
3785 boolean_t xtreme_rewind
= B_FALSE
;
3786 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3787 nvlist_t
*policy
= NULL
;
3788 zpool_handle_t
*zhp
;
3789 char *pool
, *device
;
3792 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3801 xtreme_rewind
= B_TRUE
;
3804 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3814 (void) fprintf(stderr
, gettext("missing pool name\n"));
3819 (void) fprintf(stderr
, gettext("too many arguments\n"));
3823 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3824 (void) fprintf(stderr
,
3825 gettext("-n or -X only meaningful with -F\n"));
3829 rewind_policy
= ZPOOL_TRY_REWIND
;
3831 rewind_policy
= ZPOOL_DO_REWIND
;
3833 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3835 /* In future, further rewind policy choices can be passed along here */
3836 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3837 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3841 device
= argc
== 2 ? argv
[1] : NULL
;
3843 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3844 nvlist_free(policy
);
3848 if (zpool_clear(zhp
, device
, policy
) != 0)
3853 nvlist_free(policy
);
3859 * zpool reguid <pool>
3862 zpool_do_reguid(int argc
, char **argv
)
3866 zpool_handle_t
*zhp
;
3870 while ((c
= getopt(argc
, argv
, "")) != -1) {
3873 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3882 /* get pool name and check number of arguments */
3884 (void) fprintf(stderr
, gettext("missing pool name\n"));
3889 (void) fprintf(stderr
, gettext("too many arguments\n"));
3894 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3897 ret
= zpool_reguid(zhp
);
3905 * zpool reopen <pool>
3907 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3910 zpool_do_reopen(int argc
, char **argv
)
3914 zpool_handle_t
*zhp
;
3918 while ((c
= getopt(argc
, argv
, "")) != -1) {
3921 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3931 (void) fprintf(stderr
, gettext("missing pool name\n"));
3936 (void) fprintf(stderr
, gettext("too many arguments\n"));
3941 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3944 ret
= zpool_reopen(zhp
);
3949 typedef struct scrub_cbdata
{
3956 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3958 scrub_cbdata_t
*cb
= data
;
3962 * Ignore faulted pools.
3964 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3965 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3966 "currently unavailable\n"), zpool_get_name(zhp
));
3970 err
= zpool_scan(zhp
, cb
->cb_type
);
3976 * zpool scrub [-s] <pool> ...
3978 * -s Stop. Stops any in-progress scrub.
3981 zpool_do_scrub(int argc
, char **argv
)
3986 cb
.cb_type
= POOL_SCAN_SCRUB
;
3989 while ((c
= getopt(argc
, argv
, "s")) != -1) {
3992 cb
.cb_type
= POOL_SCAN_NONE
;
3995 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4007 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4011 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
4014 typedef struct status_cbdata
{
4016 boolean_t cb_allpools
;
4017 boolean_t cb_verbose
;
4018 boolean_t cb_explain
;
4020 boolean_t cb_dedup_stats
;
4024 * Print out detailed scrub status.
4027 print_scan_status(pool_scan_stat_t
*ps
)
4030 uint64_t elapsed
, mins_left
, hours_left
;
4031 uint64_t pass_exam
, examined
, total
;
4033 double fraction_done
;
4034 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4036 (void) printf(gettext(" scan: "));
4038 /* If there's never been a scan, there's not much to say. */
4039 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
4040 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
4041 (void) printf(gettext("none requested\n"));
4045 start
= ps
->pss_start_time
;
4046 end
= ps
->pss_end_time
;
4047 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
4049 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
4050 ps
->pss_func
== POOL_SCAN_RESILVER
);
4052 * Scan is finished or canceled.
4054 if (ps
->pss_state
== DSS_FINISHED
) {
4055 uint64_t minutes_taken
= (end
- start
) / 60;
4058 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4059 fmt
= gettext("scrub repaired %s in %lluh%um with "
4060 "%llu errors on %s");
4061 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4062 fmt
= gettext("resilvered %s in %lluh%um with "
4063 "%llu errors on %s");
4066 (void) printf(fmt
, processed_buf
,
4067 (u_longlong_t
)(minutes_taken
/ 60),
4068 (uint_t
)(minutes_taken
% 60),
4069 (u_longlong_t
)ps
->pss_errors
,
4070 ctime((time_t *)&end
));
4072 } else if (ps
->pss_state
== DSS_CANCELED
) {
4073 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4074 (void) printf(gettext("scrub canceled on %s"),
4076 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4077 (void) printf(gettext("resilver canceled on %s"),
4083 assert(ps
->pss_state
== DSS_SCANNING
);
4086 * Scan is in progress.
4088 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4089 (void) printf(gettext("scrub in progress since %s"),
4091 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4092 (void) printf(gettext("resilver in progress since %s"),
4096 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
4097 total
= ps
->pss_to_examine
;
4098 fraction_done
= (double)examined
/ total
;
4100 /* elapsed time for this pass */
4101 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4102 elapsed
= elapsed
? elapsed
: 1;
4103 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4104 rate
= pass_exam
/ elapsed
;
4105 rate
= rate
? rate
: 1;
4106 mins_left
= ((total
- examined
) / rate
) / 60;
4107 hours_left
= mins_left
/ 60;
4109 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4110 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4111 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4114 * do not print estimated time if hours_left is more than 30 days
4116 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4117 examined_buf
, total_buf
, rate_buf
);
4118 if (hours_left
< (30 * 24)) {
4119 (void) printf(gettext(", %lluh%um to go\n"),
4120 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4122 (void) printf(gettext(
4123 ", (scan is slow, no estimated time)\n"));
4126 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4127 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4128 processed_buf
, 100 * fraction_done
);
4129 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4130 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4131 processed_buf
, 100 * fraction_done
);
4136 print_error_log(zpool_handle_t
*zhp
)
4138 nvlist_t
*nverrlist
= NULL
;
4141 size_t len
= MAXPATHLEN
* 2;
4143 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4144 (void) printf("errors: List of errors unavailable "
4145 "(insufficient privileges)\n");
4149 (void) printf("errors: Permanent errors have been "
4150 "detected in the following files:\n\n");
4152 pathname
= safe_malloc(len
);
4154 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4156 uint64_t dsobj
, obj
;
4158 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4159 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4161 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4163 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4164 (void) printf("%7s %s\n", "", pathname
);
4167 nvlist_free(nverrlist
);
4171 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4180 (void) printf(gettext("\tspares\n"));
4182 for (i
= 0; i
< nspares
; i
++) {
4183 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
4184 print_status_config(zhp
, name
, spares
[i
],
4185 namewidth
, 2, B_TRUE
);
4191 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4200 (void) printf(gettext("\tcache\n"));
4202 for (i
= 0; i
< nl2cache
; i
++) {
4203 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
4204 print_status_config(zhp
, name
, l2cache
[i
],
4205 namewidth
, 2, B_FALSE
);
4211 print_dedup_stats(nvlist_t
*config
)
4213 ddt_histogram_t
*ddh
;
4219 * If the pool was faulted then we may not have been able to
4220 * obtain the config. Otherwise, if we have anything in the dedup
4221 * table continue processing the stats.
4223 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4224 (uint64_t **)&ddo
, &c
) != 0)
4227 (void) printf("\n");
4228 (void) printf(gettext(" dedup: "));
4229 if (ddo
->ddo_count
== 0) {
4230 (void) printf(gettext("no DDT entries\n"));
4234 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4235 (u_longlong_t
)ddo
->ddo_count
,
4236 (u_longlong_t
)ddo
->ddo_dspace
,
4237 (u_longlong_t
)ddo
->ddo_mspace
);
4239 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4240 (uint64_t **)&dds
, &c
) == 0);
4241 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4242 (uint64_t **)&ddh
, &c
) == 0);
4243 zpool_dump_ddt(dds
, ddh
);
4247 * Display a summary of pool status. Displays a summary such as:
4251 * reason: One or more devices ...
4252 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4258 * When given the '-v' option, we print out the complete config. If the '-e'
4259 * option is specified, then we print out error rate information as well.
4262 status_callback(zpool_handle_t
*zhp
, void *data
)
4264 status_cbdata_t
*cbp
= data
;
4265 nvlist_t
*config
, *nvroot
;
4267 zpool_status_t reason
;
4268 zpool_errata_t errata
;
4273 config
= zpool_get_config(zhp
, NULL
);
4274 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
4279 * If we were given 'zpool status -x', only report those pools with
4282 if (cbp
->cb_explain
&&
4283 (reason
== ZPOOL_STATUS_OK
||
4284 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4285 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4286 if (!cbp
->cb_allpools
) {
4287 (void) printf(gettext("pool '%s' is healthy\n"),
4288 zpool_get_name(zhp
));
4290 cbp
->cb_first
= B_FALSE
;
4296 cbp
->cb_first
= B_FALSE
;
4298 (void) printf("\n");
4300 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4302 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4303 (uint64_t **)&vs
, &c
) == 0);
4304 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4306 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4307 (void) printf(gettext(" state: %s\n"), health
);
4310 case ZPOOL_STATUS_MISSING_DEV_R
:
4311 (void) printf(gettext("status: One or more devices could not "
4312 "be opened. Sufficient replicas exist for\n\tthe pool to "
4313 "continue functioning in a degraded state.\n"));
4314 (void) printf(gettext("action: Attach the missing device and "
4315 "online it using 'zpool online'.\n"));
4318 case ZPOOL_STATUS_MISSING_DEV_NR
:
4319 (void) printf(gettext("status: One or more devices could not "
4320 "be opened. There are insufficient\n\treplicas for the "
4321 "pool to continue functioning.\n"));
4322 (void) printf(gettext("action: Attach the missing device and "
4323 "online it using 'zpool online'.\n"));
4326 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4327 (void) printf(gettext("status: One or more devices could not "
4328 "be used because the label is missing or\n\tinvalid. "
4329 "Sufficient replicas exist for the pool to continue\n\t"
4330 "functioning in a degraded state.\n"));
4331 (void) printf(gettext("action: Replace the device using "
4332 "'zpool replace'.\n"));
4335 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4336 (void) printf(gettext("status: One or more devices could not "
4337 "be used because the label is missing \n\tor invalid. "
4338 "There are insufficient replicas for the pool to "
4339 "continue\n\tfunctioning.\n"));
4340 zpool_explain_recover(zpool_get_handle(zhp
),
4341 zpool_get_name(zhp
), reason
, config
);
4344 case ZPOOL_STATUS_FAILING_DEV
:
4345 (void) printf(gettext("status: One or more devices has "
4346 "experienced an unrecoverable error. An\n\tattempt was "
4347 "made to correct the error. Applications are "
4349 (void) printf(gettext("action: Determine if the device needs "
4350 "to be replaced, and clear the errors\n\tusing "
4351 "'zpool clear' or replace the device with 'zpool "
4355 case ZPOOL_STATUS_OFFLINE_DEV
:
4356 (void) printf(gettext("status: One or more devices has "
4357 "been taken offline by the administrator.\n\tSufficient "
4358 "replicas exist for the pool to continue functioning in "
4359 "a\n\tdegraded state.\n"));
4360 (void) printf(gettext("action: Online the device using "
4361 "'zpool online' or replace the device with\n\t'zpool "
4365 case ZPOOL_STATUS_REMOVED_DEV
:
4366 (void) printf(gettext("status: One or more devices has "
4367 "been removed by the administrator.\n\tSufficient "
4368 "replicas exist for the pool to continue functioning in "
4369 "a\n\tdegraded state.\n"));
4370 (void) printf(gettext("action: Online the device using "
4371 "'zpool online' or replace the device with\n\t'zpool "
4375 case ZPOOL_STATUS_RESILVERING
:
4376 (void) printf(gettext("status: One or more devices is "
4377 "currently being resilvered. The pool will\n\tcontinue "
4378 "to function, possibly in a degraded state.\n"));
4379 (void) printf(gettext("action: Wait for the resilver to "
4383 case ZPOOL_STATUS_CORRUPT_DATA
:
4384 (void) printf(gettext("status: One or more devices has "
4385 "experienced an error resulting in data\n\tcorruption. "
4386 "Applications may be affected.\n"));
4387 (void) printf(gettext("action: Restore the file in question "
4388 "if possible. Otherwise restore the\n\tentire pool from "
4392 case ZPOOL_STATUS_CORRUPT_POOL
:
4393 (void) printf(gettext("status: The pool metadata is corrupted "
4394 "and the pool cannot be opened.\n"));
4395 zpool_explain_recover(zpool_get_handle(zhp
),
4396 zpool_get_name(zhp
), reason
, config
);
4399 case ZPOOL_STATUS_VERSION_OLDER
:
4400 (void) printf(gettext("status: The pool is formatted using a "
4401 "legacy on-disk format. The pool can\n\tstill be used, "
4402 "but some features are unavailable.\n"));
4403 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4404 "upgrade'. Once this is done, the\n\tpool will no longer "
4405 "be accessible on software that does not support\n\t"
4406 "feature flags.\n"));
4409 case ZPOOL_STATUS_VERSION_NEWER
:
4410 (void) printf(gettext("status: The pool has been upgraded to a "
4411 "newer, incompatible on-disk version.\n\tThe pool cannot "
4412 "be accessed on this system.\n"));
4413 (void) printf(gettext("action: Access the pool from a system "
4414 "running more recent software, or\n\trestore the pool from "
4418 case ZPOOL_STATUS_FEAT_DISABLED
:
4419 (void) printf(gettext("status: Some supported features are not "
4420 "enabled on the pool. The pool can\n\tstill be used, but "
4421 "some features are unavailable.\n"));
4422 (void) printf(gettext("action: Enable all features using "
4423 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4424 "longer be accessible by software that does not support\n\t"
4425 "the features. See zpool-features(5) for details.\n"));
4428 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4429 (void) printf(gettext("status: The pool cannot be accessed on "
4430 "this system because it uses the\n\tfollowing feature(s) "
4431 "not supported on this system:\n"));
4432 zpool_print_unsup_feat(config
);
4433 (void) printf("\n");
4434 (void) printf(gettext("action: Access the pool from a system "
4435 "that supports the required feature(s),\n\tor restore the "
4436 "pool from backup.\n"));
4439 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4440 (void) printf(gettext("status: The pool can only be accessed "
4441 "in read-only mode on this system. It\n\tcannot be "
4442 "accessed in read-write mode because it uses the "
4443 "following\n\tfeature(s) not supported on this system:\n"));
4444 zpool_print_unsup_feat(config
);
4445 (void) printf("\n");
4446 (void) printf(gettext("action: The pool cannot be accessed in "
4447 "read-write mode. Import the pool with\n"
4448 "\t\"-o readonly=on\", access the pool from a system that "
4449 "supports the\n\trequired feature(s), or restore the "
4450 "pool from backup.\n"));
4453 case ZPOOL_STATUS_FAULTED_DEV_R
:
4454 (void) printf(gettext("status: One or more devices are "
4455 "faulted in response to persistent errors.\n\tSufficient "
4456 "replicas exist for the pool to continue functioning "
4457 "in a\n\tdegraded state.\n"));
4458 (void) printf(gettext("action: Replace the faulted device, "
4459 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4462 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4463 (void) printf(gettext("status: One or more devices are "
4464 "faulted in response to persistent errors. There are "
4465 "insufficient replicas for the pool to\n\tcontinue "
4467 (void) printf(gettext("action: Destroy and re-create the pool "
4468 "from a backup source. Manually marking the device\n"
4469 "\trepaired using 'zpool clear' may allow some data "
4470 "to be recovered.\n"));
4473 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4474 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4475 (void) printf(gettext("status: One or more devices are "
4476 "faulted in response to IO failures.\n"));
4477 (void) printf(gettext("action: Make sure the affected devices "
4478 "are connected, then run 'zpool clear'.\n"));
4481 case ZPOOL_STATUS_BAD_LOG
:
4482 (void) printf(gettext("status: An intent log record "
4483 "could not be read.\n"
4484 "\tWaiting for adminstrator intervention to fix the "
4485 "faulted pool.\n"));
4486 (void) printf(gettext("action: Either restore the affected "
4487 "device(s) and run 'zpool online',\n"
4488 "\tor ignore the intent log records by running "
4489 "'zpool clear'.\n"));
4492 case ZPOOL_STATUS_HOSTID_MISMATCH
:
4493 (void) printf(gettext("status: Mismatch between pool hostid "
4494 "and system hostid on imported pool.\n\tThis pool was "
4495 "previously imported into a system with a different "
4496 "hostid,\n\tand then was verbatim imported into this "
4498 (void) printf(gettext("action: Export this pool on all systems "
4499 "on which it is imported.\n"
4500 "\tThen import it to correct the mismatch.\n"));
4503 case ZPOOL_STATUS_ERRATA
:
4504 (void) printf(gettext("status: Errata #%d detected.\n"),
4508 case ZPOOL_ERRATA_NONE
:
4511 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
4512 (void) printf(gettext("action: To correct the issue "
4513 "run 'zpool scrub'.\n"));
4518 * All errata which allow the pool to be imported
4519 * must contain an action message.
4527 * The remaining errors can't actually be generated, yet.
4529 assert(reason
== ZPOOL_STATUS_OK
);
4533 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4536 if (config
!= NULL
) {
4539 nvlist_t
**spares
, **l2cache
;
4540 uint_t nspares
, nl2cache
;
4541 pool_scan_stat_t
*ps
= NULL
;
4543 (void) nvlist_lookup_uint64_array(nvroot
,
4544 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4545 print_scan_status(ps
);
4547 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4551 (void) printf(gettext("config:\n\n"));
4552 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4553 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4554 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4555 namewidth
, 0, B_FALSE
);
4557 if (num_logs(nvroot
) > 0)
4558 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4559 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4560 &l2cache
, &nl2cache
) == 0)
4561 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4563 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4564 &spares
, &nspares
) == 0)
4565 print_spares(zhp
, spares
, nspares
, namewidth
);
4567 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4569 nvlist_t
*nverrlist
= NULL
;
4572 * If the approximate error count is small, get a
4573 * precise count by fetching the entire log and
4574 * uniquifying the results.
4576 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4577 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4582 while ((elem
= nvlist_next_nvpair(nverrlist
,
4587 nvlist_free(nverrlist
);
4589 (void) printf("\n");
4592 (void) printf(gettext("errors: No known data "
4594 else if (!cbp
->cb_verbose
)
4595 (void) printf(gettext("errors: %llu data "
4596 "errors, use '-v' for a list\n"),
4597 (u_longlong_t
)nerr
);
4599 print_error_log(zhp
);
4602 if (cbp
->cb_dedup_stats
)
4603 print_dedup_stats(config
);
4605 (void) printf(gettext("config: The configuration cannot be "
4613 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4615 * -v Display complete error logs
4616 * -x Display only pools with potential problems
4617 * -D Display dedup status (undocumented)
4618 * -T Display a timestamp in date(1) or Unix format
4620 * Describes the health status of all pools or some subset.
4623 zpool_do_status(int argc
, char **argv
)
4627 unsigned long interval
= 0, count
= 0;
4628 status_cbdata_t cb
= { 0 };
4631 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4634 cb
.cb_verbose
= B_TRUE
;
4637 cb
.cb_explain
= B_TRUE
;
4640 cb
.cb_dedup_stats
= B_TRUE
;
4643 get_timestamp_arg(*optarg
);
4646 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4655 get_interval_count(&argc
, argv
, &interval
, &count
);
4658 cb
.cb_allpools
= B_TRUE
;
4660 cb
.cb_first
= B_TRUE
;
4663 if (timestamp_fmt
!= NODATE
)
4664 print_timestamp(timestamp_fmt
);
4666 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4667 status_callback
, &cb
);
4669 if (argc
== 0 && cb
.cb_count
== 0)
4670 (void) fprintf(stderr
, gettext("no pools available\n"));
4671 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4672 (void) printf(gettext("all pools are healthy\n"));
4680 if (count
!= 0 && --count
== 0)
4683 (void) sleep(interval
);
4689 typedef struct upgrade_cbdata
{
4692 uint64_t cb_version
;
4697 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
4699 int zfs_version
= (int) zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
4700 int *count
= (int *)unsupp_fs
;
4702 if (zfs_version
> ZPL_VERSION
) {
4703 (void) printf(gettext("%s (v%d) is not supported by this "
4704 "implementation of ZFS.\n"),
4705 zfs_get_name(zhp
), zfs_version
);
4709 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
4717 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4721 uint64_t oldversion
;
4724 config
= zpool_get_config(zhp
, NULL
);
4725 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4728 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4729 assert(oldversion
< version
);
4731 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
4736 (void) fprintf(stderr
, gettext("Upgrade not performed due "
4737 "to %d unsupported filesystems (max v%d).\n"),
4738 unsupp_fs
, (int) ZPL_VERSION
);
4742 ret
= zpool_upgrade(zhp
, version
);
4746 if (version
>= SPA_VERSION_FEATURES
) {
4747 (void) printf(gettext("Successfully upgraded "
4748 "'%s' from version %llu to feature flags.\n"),
4749 zpool_get_name(zhp
), (u_longlong_t
) oldversion
);
4751 (void) printf(gettext("Successfully upgraded "
4752 "'%s' from version %llu to version %llu.\n"),
4753 zpool_get_name(zhp
), (u_longlong_t
) oldversion
,
4754 (u_longlong_t
) version
);
4761 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4764 boolean_t firstff
= B_TRUE
;
4765 nvlist_t
*enabled
= zpool_get_features(zhp
);
4768 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4769 const char *fname
= spa_feature_table
[i
].fi_uname
;
4770 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4771 if (!nvlist_exists(enabled
, fguid
)) {
4773 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4774 ret
= zpool_set_prop(zhp
, propname
,
4775 ZFS_FEATURE_ENABLED
);
4783 (void) printf(gettext("Enabled the "
4784 "following features on '%s':\n"),
4785 zpool_get_name(zhp
));
4788 (void) printf(gettext(" %s\n"), fname
);
4799 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4801 upgrade_cbdata_t
*cbp
= arg
;
4804 boolean_t printnl
= B_FALSE
;
4807 config
= zpool_get_config(zhp
, NULL
);
4808 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4811 assert(SPA_VERSION_IS_SUPPORTED(version
));
4813 if (version
< cbp
->cb_version
) {
4814 cbp
->cb_first
= B_FALSE
;
4815 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4821 * If they did "zpool upgrade -a", then we could
4822 * be doing ioctls to different pools. We need
4823 * to log this history once to each pool, and bypass
4824 * the normal history logging that happens in main().
4826 (void) zpool_log_history(g_zfs
, history_str
);
4827 log_history
= B_FALSE
;
4830 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4832 ret
= upgrade_enable_all(zhp
, &count
);
4837 cbp
->cb_first
= B_FALSE
;
4843 (void) printf(gettext("\n"));
4850 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4852 upgrade_cbdata_t
*cbp
= arg
;
4856 config
= zpool_get_config(zhp
, NULL
);
4857 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4860 assert(SPA_VERSION_IS_SUPPORTED(version
));
4862 if (version
< SPA_VERSION_FEATURES
) {
4863 if (cbp
->cb_first
) {
4864 (void) printf(gettext("The following pools are "
4865 "formatted with legacy version numbers and can\n"
4866 "be upgraded to use feature flags. After "
4867 "being upgraded, these pools\nwill no "
4868 "longer be accessible by software that does not "
4869 "support feature\nflags.\n\n"));
4870 (void) printf(gettext("VER POOL\n"));
4871 (void) printf(gettext("--- ------------\n"));
4872 cbp
->cb_first
= B_FALSE
;
4875 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4876 zpool_get_name(zhp
));
4883 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4885 upgrade_cbdata_t
*cbp
= arg
;
4889 config
= zpool_get_config(zhp
, NULL
);
4890 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4893 if (version
>= SPA_VERSION_FEATURES
) {
4895 boolean_t poolfirst
= B_TRUE
;
4896 nvlist_t
*enabled
= zpool_get_features(zhp
);
4898 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4899 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4900 const char *fname
= spa_feature_table
[i
].fi_uname
;
4901 if (!nvlist_exists(enabled
, fguid
)) {
4902 if (cbp
->cb_first
) {
4903 (void) printf(gettext("\nSome "
4904 "supported features are not "
4905 "enabled on the following pools. "
4906 "Once a\nfeature is enabled the "
4907 "pool may become incompatible with "
4908 "software\nthat does not support "
4910 "zpool-features(5) for "
4912 (void) printf(gettext("POOL "
4914 (void) printf(gettext("------"
4916 cbp
->cb_first
= B_FALSE
;
4920 (void) printf(gettext("%s\n"),
4921 zpool_get_name(zhp
));
4922 poolfirst
= B_FALSE
;
4925 (void) printf(gettext(" %s\n"), fname
);
4928 * If they did "zpool upgrade -a", then we could
4929 * be doing ioctls to different pools. We need
4930 * to log this history once to each pool, and bypass
4931 * the normal history logging that happens in main().
4933 (void) zpool_log_history(g_zfs
, history_str
);
4934 log_history
= B_FALSE
;
4943 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4945 boolean_t printnl
= B_FALSE
;
4946 upgrade_cbdata_t
*cbp
= data
;
4947 uint64_t cur_version
;
4950 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4951 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
4952 "Pool 'log' must be renamed using export and import"
4957 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4958 if (cur_version
> cbp
->cb_version
) {
4959 (void) printf(gettext("Pool '%s' is already formatted "
4960 "using more current version '%llu'.\n\n"),
4961 zpool_get_name(zhp
), (u_longlong_t
) cur_version
);
4965 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
4966 (void) printf(gettext("Pool '%s' is already formatted "
4967 "using version %llu.\n\n"), zpool_get_name(zhp
),
4968 (u_longlong_t
) cbp
->cb_version
);
4972 if (cur_version
!= cbp
->cb_version
) {
4974 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4979 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4981 ret
= upgrade_enable_all(zhp
, &count
);
4987 } else if (cur_version
== SPA_VERSION
) {
4988 (void) printf(gettext("Pool '%s' already has all "
4989 "supported features enabled.\n"),
4990 zpool_get_name(zhp
));
4995 (void) printf(gettext("\n"));
5004 * zpool upgrade [-V version] <-a | pool ...>
5006 * With no arguments, display downrev'd ZFS pool available for upgrade.
5007 * Individual pools can be upgraded by specifying the pool, and '-a' will
5008 * upgrade all pools.
5011 zpool_do_upgrade(int argc
, char **argv
)
5014 upgrade_cbdata_t cb
= { 0 };
5016 boolean_t showversions
= B_FALSE
;
5017 boolean_t upgradeall
= B_FALSE
;
5022 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
5025 upgradeall
= B_TRUE
;
5028 showversions
= B_TRUE
;
5031 cb
.cb_version
= strtoll(optarg
, &end
, 10);
5033 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
5034 (void) fprintf(stderr
,
5035 gettext("invalid version '%s'\n"), optarg
);
5040 (void) fprintf(stderr
, gettext("missing argument for "
5041 "'%c' option\n"), optopt
);
5045 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5056 if (cb
.cb_version
== 0) {
5057 cb
.cb_version
= SPA_VERSION
;
5058 } else if (!upgradeall
&& argc
== 0) {
5059 (void) fprintf(stderr
, gettext("-V option is "
5060 "incompatible with other arguments\n"));
5065 if (upgradeall
|| argc
!= 0) {
5066 (void) fprintf(stderr
, gettext("-v option is "
5067 "incompatible with other arguments\n"));
5070 } else if (upgradeall
) {
5072 (void) fprintf(stderr
, gettext("-a option should not "
5073 "be used along with a pool name\n"));
5078 (void) printf(gettext("This system supports ZFS pool feature "
5083 (void) printf(gettext("The following features are "
5085 (void) printf(gettext("FEAT DESCRIPTION\n"));
5086 (void) printf("----------------------------------------------"
5087 "---------------\n");
5088 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5089 zfeature_info_t
*fi
= &spa_feature_table
[i
];
5090 const char *ro
= fi
->fi_can_readonly
?
5091 " (read-only compatible)" : "";
5093 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
5094 (void) printf(" %s\n", fi
->fi_desc
);
5096 (void) printf("\n");
5098 (void) printf(gettext("The following legacy versions are also "
5100 (void) printf(gettext("VER DESCRIPTION\n"));
5101 (void) printf("--- -----------------------------------------"
5102 "---------------\n");
5103 (void) printf(gettext(" 1 Initial ZFS version\n"));
5104 (void) printf(gettext(" 2 Ditto blocks "
5105 "(replicated metadata)\n"));
5106 (void) printf(gettext(" 3 Hot spares and double parity "
5108 (void) printf(gettext(" 4 zpool history\n"));
5109 (void) printf(gettext(" 5 Compression using the gzip "
5111 (void) printf(gettext(" 6 bootfs pool property\n"));
5112 (void) printf(gettext(" 7 Separate intent log devices\n"));
5113 (void) printf(gettext(" 8 Delegated administration\n"));
5114 (void) printf(gettext(" 9 refquota and refreservation "
5116 (void) printf(gettext(" 10 Cache devices\n"));
5117 (void) printf(gettext(" 11 Improved scrub performance\n"));
5118 (void) printf(gettext(" 12 Snapshot properties\n"));
5119 (void) printf(gettext(" 13 snapused property\n"));
5120 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5121 (void) printf(gettext(" 15 user/group space accounting\n"));
5122 (void) printf(gettext(" 16 stmf property support\n"));
5123 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5124 (void) printf(gettext(" 18 Snapshot user holds\n"));
5125 (void) printf(gettext(" 19 Log device removal\n"));
5126 (void) printf(gettext(" 20 Compression using zle "
5127 "(zero-length encoding)\n"));
5128 (void) printf(gettext(" 21 Deduplication\n"));
5129 (void) printf(gettext(" 22 Received properties\n"));
5130 (void) printf(gettext(" 23 Slim ZIL\n"));
5131 (void) printf(gettext(" 24 System attributes\n"));
5132 (void) printf(gettext(" 25 Improved scrub stats\n"));
5133 (void) printf(gettext(" 26 Improved snapshot deletion "
5135 (void) printf(gettext(" 27 Improved snapshot creation "
5137 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5138 (void) printf(gettext("\nFor more information on a particular "
5139 "version, including supported releases,\n"));
5140 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5141 } else if (argc
== 0 && upgradeall
) {
5142 cb
.cb_first
= B_TRUE
;
5143 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5144 if (ret
== 0 && cb
.cb_first
) {
5145 if (cb
.cb_version
== SPA_VERSION
) {
5146 (void) printf(gettext("All pools are already "
5147 "formatted using feature flags.\n\n"));
5148 (void) printf(gettext("Every feature flags "
5149 "pool already has all supported features "
5152 (void) printf(gettext("All pools are already "
5153 "formatted with version %llu or higher.\n"),
5154 (u_longlong_t
) cb
.cb_version
);
5157 } else if (argc
== 0) {
5158 cb
.cb_first
= B_TRUE
;
5159 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5163 (void) printf(gettext("All pools are formatted "
5164 "using feature flags.\n\n"));
5166 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5167 "for a list of available legacy versions.\n"));
5170 cb
.cb_first
= B_TRUE
;
5171 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5175 (void) printf(gettext("Every feature flags pool has "
5176 "all supported features enabled.\n"));
5178 (void) printf(gettext("\n"));
5181 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5188 typedef struct hist_cbdata
{
5195 * Print out the command history for a specific pool.
5198 get_history_one(zpool_handle_t
*zhp
, void *data
)
5204 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5206 cb
->first
= B_FALSE
;
5208 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5210 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5213 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5214 &records
, &numrecords
) == 0);
5215 for (i
= 0; i
< numrecords
; i
++) {
5216 nvlist_t
*rec
= records
[i
];
5219 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5223 tsec
= fnvlist_lookup_uint64(records
[i
],
5225 (void) localtime_r(&tsec
, &t
);
5226 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5229 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5230 (void) printf("%s %s", tbuf
,
5231 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5232 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5234 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5237 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5238 (void) printf("%s unrecognized record:\n",
5240 dump_nvlist(rec
, 4);
5243 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5244 zfs_history_event_names
[ievent
],
5245 (longlong_t
) fnvlist_lookup_uint64(
5246 rec
, ZPOOL_HIST_TXG
),
5247 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5248 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5251 (void) printf("%s [txg:%lld] %s", tbuf
,
5252 (longlong_t
) fnvlist_lookup_uint64(
5253 rec
, ZPOOL_HIST_TXG
),
5254 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5255 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5256 (void) printf(" %s (%llu)",
5257 fnvlist_lookup_string(rec
,
5259 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
5262 (void) printf(" %s", fnvlist_lookup_string(rec
,
5263 ZPOOL_HIST_INT_STR
));
5264 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5267 (void) printf("%s ioctl %s\n", tbuf
,
5268 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5269 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5270 (void) printf(" input:\n");
5271 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5272 ZPOOL_HIST_INPUT_NVL
), 8);
5274 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5275 (void) printf(" output:\n");
5276 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5277 ZPOOL_HIST_OUTPUT_NVL
), 8);
5282 (void) printf("%s unrecognized record:\n", tbuf
);
5283 dump_nvlist(rec
, 4);
5287 (void) printf("\n");
5290 (void) printf(" [");
5291 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5292 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5293 struct passwd
*pwd
= getpwuid(who
);
5294 (void) printf("user %d ", (int)who
);
5296 (void) printf("(%s) ", pwd
->pw_name
);
5298 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5299 (void) printf("on %s",
5300 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5302 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5303 (void) printf(":%s",
5304 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5308 (void) printf("\n");
5310 (void) printf("\n");
5317 * zpool history <pool>
5319 * Displays the history of commands that modified pools.
5322 zpool_do_history(int argc
, char **argv
)
5324 hist_cbdata_t cbdata
= { 0 };
5328 cbdata
.first
= B_TRUE
;
5330 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5333 cbdata
.longfmt
= B_TRUE
;
5336 cbdata
.internal
= B_TRUE
;
5339 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5347 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5350 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5351 (void) fprintf(stderr
, gettext("no pools available\n"));
5358 typedef struct ev_opts
{
5366 zpool_do_events_short(nvlist_t
*nvl
)
5368 char ctime_str
[26], str
[32], *ptr
;
5372 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
5373 memset(str
, ' ', 32);
5374 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
5375 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
5376 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
5377 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
5378 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
5379 (void) printf(gettext("%s "), str
);
5381 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
5382 (void) printf(gettext("%s\n"), ptr
);
5386 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
5390 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
5391 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
5393 data_type_t type
= nvpair_type(nvp
);
5394 const char *name
= nvpair_name(nvp
);
5404 printf(gettext("%*s%s = "), depth
, "", name
);
5407 case DATA_TYPE_BOOLEAN
:
5408 printf(gettext("%s"), "1");
5411 case DATA_TYPE_BOOLEAN_VALUE
:
5412 (void) nvpair_value_boolean_value(nvp
, &b
);
5413 printf(gettext("%s"), b
? "1" : "0");
5416 case DATA_TYPE_BYTE
:
5417 (void) nvpair_value_byte(nvp
, &i8
);
5418 printf(gettext("0x%x"), i8
);
5421 case DATA_TYPE_INT8
:
5422 (void) nvpair_value_int8(nvp
, (void *)&i8
);
5423 printf(gettext("0x%x"), i8
);
5426 case DATA_TYPE_UINT8
:
5427 (void) nvpair_value_uint8(nvp
, &i8
);
5428 printf(gettext("0x%x"), i8
);
5431 case DATA_TYPE_INT16
:
5432 (void) nvpair_value_int16(nvp
, (void *)&i16
);
5433 printf(gettext("0x%x"), i16
);
5436 case DATA_TYPE_UINT16
:
5437 (void) nvpair_value_uint16(nvp
, &i16
);
5438 printf(gettext("0x%x"), i16
);
5441 case DATA_TYPE_INT32
:
5442 (void) nvpair_value_int32(nvp
, (void *)&i32
);
5443 printf(gettext("0x%x"), i32
);
5446 case DATA_TYPE_UINT32
:
5447 (void) nvpair_value_uint32(nvp
, &i32
);
5448 printf(gettext("0x%x"), i32
);
5451 case DATA_TYPE_INT64
:
5452 (void) nvpair_value_int64(nvp
, (void *)&i64
);
5453 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5456 case DATA_TYPE_UINT64
:
5457 (void) nvpair_value_uint64(nvp
, &i64
);
5458 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5461 case DATA_TYPE_HRTIME
:
5462 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
5463 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5466 case DATA_TYPE_STRING
:
5467 (void) nvpair_value_string(nvp
, &str
);
5468 printf(gettext("\"%s\""), str
? str
: "<NULL>");
5471 case DATA_TYPE_NVLIST
:
5472 printf(gettext("(embedded nvlist)\n"));
5473 (void) nvpair_value_nvlist(nvp
, &cnv
);
5474 zpool_do_events_nvprint(cnv
, depth
+ 8);
5475 printf(gettext("%*s(end %s)"), depth
, "", name
);
5478 case DATA_TYPE_NVLIST_ARRAY
: {
5482 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
5483 printf(gettext("(%d embedded nvlists)\n"), nelem
);
5484 for (i
= 0; i
< nelem
; i
++) {
5485 printf(gettext("%*s%s[%d] = %s\n"),
5486 depth
, "", name
, i
, "(embedded nvlist)");
5487 zpool_do_events_nvprint(val
[i
], depth
+ 8);
5488 printf(gettext("%*s(end %s[%i])\n"),
5489 depth
, "", name
, i
);
5491 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
5495 case DATA_TYPE_INT8_ARRAY
: {
5499 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
5500 for (i
= 0; i
< nelem
; i
++)
5501 printf(gettext("0x%x "), val
[i
]);
5506 case DATA_TYPE_UINT8_ARRAY
: {
5510 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
5511 for (i
= 0; i
< nelem
; i
++)
5512 printf(gettext("0x%x "), val
[i
]);
5517 case DATA_TYPE_INT16_ARRAY
: {
5521 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
5522 for (i
= 0; i
< nelem
; i
++)
5523 printf(gettext("0x%x "), val
[i
]);
5528 case DATA_TYPE_UINT16_ARRAY
: {
5532 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
5533 for (i
= 0; i
< nelem
; i
++)
5534 printf(gettext("0x%x "), val
[i
]);
5539 case DATA_TYPE_INT32_ARRAY
: {
5543 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
5544 for (i
= 0; i
< nelem
; i
++)
5545 printf(gettext("0x%x "), val
[i
]);
5550 case DATA_TYPE_UINT32_ARRAY
: {
5554 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
5555 for (i
= 0; i
< nelem
; i
++)
5556 printf(gettext("0x%x "), val
[i
]);
5561 case DATA_TYPE_INT64_ARRAY
: {
5565 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
5566 for (i
= 0; i
< nelem
; i
++)
5567 printf(gettext("0x%llx "),
5568 (u_longlong_t
)val
[i
]);
5573 case DATA_TYPE_UINT64_ARRAY
: {
5577 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
5578 for (i
= 0; i
< nelem
; i
++)
5579 printf(gettext("0x%llx "),
5580 (u_longlong_t
)val
[i
]);
5585 case DATA_TYPE_STRING_ARRAY
: {
5589 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
5590 for (i
= 0; i
< nelem
; i
++)
5591 printf(gettext("\"%s\" "),
5592 str
[i
] ? str
[i
] : "<NULL>");
5597 case DATA_TYPE_BOOLEAN_ARRAY
:
5598 case DATA_TYPE_BYTE_ARRAY
:
5599 case DATA_TYPE_DOUBLE
:
5600 case DATA_TYPE_UNKNOWN
:
5601 printf(gettext("<unknown>"));
5605 printf(gettext("\n"));
5610 zpool_do_events_next(ev_opts_t
*opts
)
5613 int zevent_fd
, ret
, dropped
;
5615 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
5616 VERIFY(zevent_fd
>= 0);
5618 if (!opts
->scripted
)
5619 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5622 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
5623 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
5624 if (ret
|| nvl
== NULL
)
5628 (void) printf(gettext("dropped %d events\n"), dropped
);
5630 zpool_do_events_short(nvl
);
5632 if (opts
->verbose
) {
5633 zpool_do_events_nvprint(nvl
, 8);
5634 printf(gettext("\n"));
5636 (void) fflush(stdout
);
5641 VERIFY(0 == close(zevent_fd
));
5647 zpool_do_events_clear(ev_opts_t
*opts
)
5651 ret
= zpool_events_clear(g_zfs
, &count
);
5653 (void) printf(gettext("cleared %d events\n"), count
);
5659 * zpool events [-vfc]
5661 * Displays events logs by ZFS.
5664 zpool_do_events(int argc
, char **argv
)
5666 ev_opts_t opts
= { 0 };
5671 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
5686 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5695 ret
= zpool_do_events_clear(&opts
);
5697 ret
= zpool_do_events_next(&opts
);
5703 get_callback(zpool_handle_t
*zhp
, void *data
)
5705 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5706 char value
[MAXNAMELEN
];
5707 zprop_source_t srctype
;
5710 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5713 * Skip the special fake placeholder. This will also skip
5714 * over the name property when 'all' is specified.
5716 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5717 pl
== cbp
->cb_proplist
)
5720 if (pl
->pl_prop
== ZPROP_INVAL
&&
5721 (zpool_prop_feature(pl
->pl_user_prop
) ||
5722 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5723 srctype
= ZPROP_SRC_LOCAL
;
5725 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5726 value
, sizeof (value
)) == 0) {
5727 zprop_print_one_property(zpool_get_name(zhp
),
5728 cbp
, pl
->pl_user_prop
, value
, srctype
,
5732 if (zpool_get_prop_literal(zhp
, pl
->pl_prop
, value
,
5733 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5736 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5737 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5745 zpool_do_get(int argc
, char **argv
)
5747 zprop_get_cbdata_t cb
= { 0 };
5748 zprop_list_t fake_name
= { 0 };
5752 while ((c
= getopt(argc
, argv
, "pH")) != -1) {
5755 cb
.cb_literal
= B_TRUE
;
5759 cb
.cb_scripted
= B_TRUE
;
5763 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5773 (void) fprintf(stderr
, gettext("missing property "
5778 cb
.cb_first
= B_TRUE
;
5779 cb
.cb_sources
= ZPROP_SRC_ALL
;
5780 cb
.cb_columns
[0] = GET_COL_NAME
;
5781 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5782 cb
.cb_columns
[2] = GET_COL_VALUE
;
5783 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5784 cb
.cb_type
= ZFS_TYPE_POOL
;
5786 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5792 if (cb
.cb_proplist
!= NULL
) {
5793 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5794 fake_name
.pl_width
= strlen(gettext("NAME"));
5795 fake_name
.pl_next
= cb
.cb_proplist
;
5796 cb
.cb_proplist
= &fake_name
;
5799 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5802 if (cb
.cb_proplist
== &fake_name
)
5803 zprop_free_list(fake_name
.pl_next
);
5805 zprop_free_list(cb
.cb_proplist
);
5810 typedef struct set_cbdata
{
5813 boolean_t cb_any_successful
;
5817 set_callback(zpool_handle_t
*zhp
, void *data
)
5820 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5822 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5825 cb
->cb_any_successful
= B_TRUE
;
5831 zpool_do_set(int argc
, char **argv
)
5833 set_cbdata_t cb
= { 0 };
5836 if (argc
> 1 && argv
[1][0] == '-') {
5837 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5843 (void) fprintf(stderr
, gettext("missing property=value "
5849 (void) fprintf(stderr
, gettext("missing pool name\n"));
5854 (void) fprintf(stderr
, gettext("too many pool names\n"));
5858 cb
.cb_propname
= argv
[1];
5859 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5860 if (cb
.cb_value
== NULL
) {
5861 (void) fprintf(stderr
, gettext("missing value in "
5862 "property=value argument\n"));
5866 *(cb
.cb_value
) = '\0';
5869 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5876 find_command_idx(char *command
, int *idx
)
5880 for (i
= 0; i
< NCOMMAND
; i
++) {
5881 if (command_table
[i
].name
== NULL
)
5884 if (strcmp(command
, command_table
[i
].name
) == 0) {
5893 main(int argc
, char **argv
)
5899 (void) setlocale(LC_ALL
, "");
5900 (void) textdomain(TEXT_DOMAIN
);
5902 dprintf_setup(&argc
, argv
);
5907 * Make sure the user has specified some command.
5910 (void) fprintf(stderr
, gettext("missing command\n"));
5919 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
5922 if ((g_zfs
= libzfs_init()) == NULL
)
5925 libzfs_print_on_error(g_zfs
, B_TRUE
);
5927 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5930 * Run the appropriate command.
5932 if (find_command_idx(cmdname
, &i
) == 0) {
5933 current_command
= &command_table
[i
];
5934 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5935 } else if (strchr(cmdname
, '=')) {
5936 verify(find_command_idx("set", &i
) == 0);
5937 current_command
= &command_table
[i
];
5938 ret
= command_table
[i
].func(argc
, argv
);
5939 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5941 * 'freeze' is a vile debugging abomination, so we treat
5945 int fd
= open(ZFS_DEV
, O_RDWR
);
5946 (void) strcpy((void *)buf
, argv
[2]);
5947 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5949 (void) fprintf(stderr
, gettext("unrecognized "
5950 "command '%s'\n"), cmdname
);
5955 if (ret
== 0 && log_history
)
5956 (void) zpool_log_history(g_zfs
, history_str
);
5961 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5962 * for the purposes of running ::findleaks.
5964 if (getenv("ZFS_ABORT") != NULL
) {
5965 (void) printf("dumping core by request\n");