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 [-f] <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);
1216 * zpool export [-f] <pool> ...
1218 * -f Forcefully unmount datasets
1220 * Export the given pools. By default, the command will attempt to cleanly
1221 * unmount any active datasets within the pool. If the '-f' flag is specified,
1222 * then the datasets will be forcefully unmounted.
1225 zpool_do_export(int argc
, char **argv
)
1227 boolean_t force
= B_FALSE
;
1228 boolean_t hardforce
= B_FALSE
;
1230 zpool_handle_t
*zhp
;
1235 while ((c
= getopt(argc
, argv
, "fF")) != -1) {
1244 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1253 /* check arguments */
1255 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1260 for (i
= 0; i
< argc
; i
++) {
1261 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) == NULL
) {
1266 if (zpool_disable_datasets(zhp
, force
) != 0) {
1272 /* The history must be logged as part of the export */
1273 log_history
= B_FALSE
;
1276 if (zpool_export_force(zhp
, history_str
) != 0)
1278 } else if (zpool_export(zhp
, force
, history_str
) != 0) {
1289 * Given a vdev configuration, determine the maximum width needed for the device
1293 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1295 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1300 if (strlen(name
) + depth
> max
)
1301 max
= strlen(name
) + depth
;
1305 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1306 &child
, &children
) == 0) {
1307 for (c
= 0; c
< children
; c
++)
1308 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1313 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1314 &child
, &children
) == 0) {
1315 for (c
= 0; c
< children
; c
++)
1316 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1321 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1322 &child
, &children
) == 0) {
1323 for (c
= 0; c
< children
; c
++)
1324 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1333 typedef struct spare_cbdata
{
1335 zpool_handle_t
*cb_zhp
;
1339 find_vdev(nvlist_t
*nv
, uint64_t search
)
1345 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1349 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1350 &child
, &children
) == 0) {
1351 for (c
= 0; c
< children
; c
++)
1352 if (find_vdev(child
[c
], search
))
1360 find_spare(zpool_handle_t
*zhp
, void *data
)
1362 spare_cbdata_t
*cbp
= data
;
1363 nvlist_t
*config
, *nvroot
;
1365 config
= zpool_get_config(zhp
, NULL
);
1366 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1369 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1379 * Print out configuration state as requested by status_callback.
1382 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1383 int namewidth
, int depth
, boolean_t isspare
)
1387 pool_scan_stat_t
*ps
= NULL
;
1389 char rbuf
[6], wbuf
[6], cbuf
[6];
1391 uint64_t notpresent
;
1395 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1396 &child
, &children
) != 0)
1399 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1400 (uint64_t **)&vs
, &c
) == 0);
1402 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1405 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1408 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1410 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1414 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1418 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1419 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1420 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1421 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1424 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1425 ¬present
) == 0) {
1427 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1428 (void) printf(" was %s", path
);
1429 } else if (vs
->vs_aux
!= 0) {
1432 switch (vs
->vs_aux
) {
1433 case VDEV_AUX_OPEN_FAILED
:
1434 (void) printf(gettext("cannot open"));
1437 case VDEV_AUX_BAD_GUID_SUM
:
1438 (void) printf(gettext("missing device"));
1441 case VDEV_AUX_NO_REPLICAS
:
1442 (void) printf(gettext("insufficient replicas"));
1445 case VDEV_AUX_VERSION_NEWER
:
1446 (void) printf(gettext("newer version"));
1449 case VDEV_AUX_UNSUP_FEAT
:
1450 (void) printf(gettext("unsupported feature(s)"));
1453 case VDEV_AUX_SPARED
:
1454 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1456 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1457 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1458 zpool_get_name(zhp
)) == 0)
1459 (void) printf(gettext("currently in "
1462 (void) printf(gettext("in use by "
1464 zpool_get_name(cb
.cb_zhp
));
1465 zpool_close(cb
.cb_zhp
);
1467 (void) printf(gettext("currently in use"));
1471 case VDEV_AUX_ERR_EXCEEDED
:
1472 (void) printf(gettext("too many errors"));
1475 case VDEV_AUX_IO_FAILURE
:
1476 (void) printf(gettext("experienced I/O failures"));
1479 case VDEV_AUX_BAD_LOG
:
1480 (void) printf(gettext("bad intent log"));
1483 case VDEV_AUX_EXTERNAL
:
1484 (void) printf(gettext("external device fault"));
1487 case VDEV_AUX_SPLIT_POOL
:
1488 (void) printf(gettext("split into new pool"));
1492 (void) printf(gettext("corrupted data"));
1497 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1498 (uint64_t **)&ps
, &c
);
1500 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1501 vs
->vs_scan_processed
!= 0 && children
== 0) {
1502 (void) printf(gettext(" (%s)"),
1503 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1504 "resilvering" : "repairing");
1507 (void) printf("\n");
1509 for (c
= 0; c
< children
; c
++) {
1510 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1512 /* Don't print logs or holes here */
1513 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1515 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1517 if (islog
|| ishole
)
1519 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1520 print_status_config(zhp
, vname
, child
[c
],
1521 namewidth
, depth
+ 2, isspare
);
1528 * Print the configuration of an exported pool. Iterate over all vdevs in the
1529 * pool, printing out the name and status for each one.
1532 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1539 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1540 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1541 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1544 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1545 (uint64_t **)&vs
, &c
) == 0);
1547 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1548 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1550 if (vs
->vs_aux
!= 0) {
1553 switch (vs
->vs_aux
) {
1554 case VDEV_AUX_OPEN_FAILED
:
1555 (void) printf(gettext("cannot open"));
1558 case VDEV_AUX_BAD_GUID_SUM
:
1559 (void) printf(gettext("missing device"));
1562 case VDEV_AUX_NO_REPLICAS
:
1563 (void) printf(gettext("insufficient replicas"));
1566 case VDEV_AUX_VERSION_NEWER
:
1567 (void) printf(gettext("newer version"));
1570 case VDEV_AUX_UNSUP_FEAT
:
1571 (void) printf(gettext("unsupported feature(s)"));
1574 case VDEV_AUX_ERR_EXCEEDED
:
1575 (void) printf(gettext("too many errors"));
1579 (void) printf(gettext("corrupted data"));
1583 (void) printf("\n");
1585 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1586 &child
, &children
) != 0)
1589 for (c
= 0; c
< children
; c
++) {
1590 uint64_t is_log
= B_FALSE
;
1592 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1597 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1598 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1602 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1603 &child
, &children
) == 0) {
1604 (void) printf(gettext("\tcache\n"));
1605 for (c
= 0; c
< children
; c
++) {
1606 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1607 (void) printf("\t %s\n", vname
);
1612 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1613 &child
, &children
) == 0) {
1614 (void) printf(gettext("\tspares\n"));
1615 for (c
= 0; c
< children
; c
++) {
1616 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1617 (void) printf("\t %s\n", vname
);
1625 * Logs are recorded as top level vdevs in the main pool child array
1626 * but with "is_log" set to 1. We use either print_status_config() or
1627 * print_import_config() to print the top level logs then any log
1628 * children (eg mirrored slogs) are printed recursively - which
1629 * works because only the top level vdev is marked "is_log"
1632 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1637 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1641 (void) printf(gettext("\tlogs\n"));
1643 for (c
= 0; c
< children
; c
++) {
1644 uint64_t is_log
= B_FALSE
;
1647 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1651 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1653 print_status_config(zhp
, name
, child
[c
], namewidth
,
1656 print_import_config(name
, child
[c
], namewidth
, 2);
1662 * Display the status for the given pool.
1665 show_import(nvlist_t
*config
)
1667 uint64_t pool_state
;
1673 zpool_status_t reason
;
1674 zpool_errata_t errata
;
1680 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1682 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1684 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1686 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1689 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1690 (uint64_t **)&vs
, &vsc
) == 0);
1691 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1693 reason
= zpool_import_status(config
, &msgid
, &errata
);
1695 (void) printf(gettext(" pool: %s\n"), name
);
1696 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1697 (void) printf(gettext(" state: %s"), health
);
1698 if (pool_state
== POOL_STATE_DESTROYED
)
1699 (void) printf(gettext(" (DESTROYED)"));
1700 (void) printf("\n");
1703 case ZPOOL_STATUS_MISSING_DEV_R
:
1704 case ZPOOL_STATUS_MISSING_DEV_NR
:
1705 case ZPOOL_STATUS_BAD_GUID_SUM
:
1706 (void) printf(gettext(" status: One or more devices are "
1707 "missing from the system.\n"));
1710 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1711 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1712 (void) printf(gettext(" status: One or more devices contains "
1713 "corrupted data.\n"));
1716 case ZPOOL_STATUS_CORRUPT_DATA
:
1718 gettext(" status: The pool data is corrupted.\n"));
1721 case ZPOOL_STATUS_OFFLINE_DEV
:
1722 (void) printf(gettext(" status: One or more devices "
1723 "are offlined.\n"));
1726 case ZPOOL_STATUS_CORRUPT_POOL
:
1727 (void) printf(gettext(" status: The pool metadata is "
1731 case ZPOOL_STATUS_VERSION_OLDER
:
1732 (void) printf(gettext(" status: The pool is formatted using a "
1733 "legacy on-disk version.\n"));
1736 case ZPOOL_STATUS_VERSION_NEWER
:
1737 (void) printf(gettext(" status: The pool is formatted using an "
1738 "incompatible version.\n"));
1741 case ZPOOL_STATUS_FEAT_DISABLED
:
1742 (void) printf(gettext(" status: Some supported features are "
1743 "not enabled on the pool.\n"));
1746 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1747 (void) printf(gettext("status: The pool uses the following "
1748 "feature(s) not supported on this sytem:\n"));
1749 zpool_print_unsup_feat(config
);
1752 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1753 (void) printf(gettext("status: The pool can only be accessed "
1754 "in read-only mode on this system. It\n\tcannot be "
1755 "accessed in read-write mode because it uses the "
1756 "following\n\tfeature(s) not supported on this system:\n"));
1757 zpool_print_unsup_feat(config
);
1760 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1761 (void) printf(gettext(" status: The pool was last accessed by "
1762 "another system.\n"));
1765 case ZPOOL_STATUS_FAULTED_DEV_R
:
1766 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1767 (void) printf(gettext(" status: One or more devices are "
1771 case ZPOOL_STATUS_BAD_LOG
:
1772 (void) printf(gettext(" status: An intent log record cannot be "
1776 case ZPOOL_STATUS_RESILVERING
:
1777 (void) printf(gettext(" status: One or more devices were being "
1781 case ZPOOL_STATUS_ERRATA
:
1782 (void) printf(gettext(" status: Errata #%d detected.\n"),
1788 * No other status can be seen when importing pools.
1790 assert(reason
== ZPOOL_STATUS_OK
);
1794 * Print out an action according to the overall state of the pool.
1796 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1797 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1798 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1799 (void) printf(gettext(" action: The pool can be "
1800 "imported using its name or numeric identifier, "
1801 "though\n\tsome features will not be available "
1802 "without an explicit 'zpool upgrade'.\n"));
1803 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1804 (void) printf(gettext(" action: The pool can be "
1805 "imported using its name or numeric "
1806 "identifier and\n\tthe '-f' flag.\n"));
1807 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
1809 case ZPOOL_ERRATA_NONE
:
1812 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
1813 (void) printf(gettext(" action: The pool can "
1814 "be imported using its name or numeric "
1815 "identifier,\n\thowever there is a compat"
1816 "ibility issue which should be corrected"
1817 "\n\tby running 'zpool scrub'\n"));
1820 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
1821 (void) printf(gettext(" action: The pool can"
1822 "not be imported with this version of ZFS "
1823 "due to\n\tan active asynchronous destroy. "
1824 "Revert to an earlier version\n\tand "
1825 "allow the destroy to complete before "
1831 * All errata must contain an action message.
1836 (void) printf(gettext(" action: The pool can be "
1837 "imported using its name or numeric "
1840 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1841 (void) printf(gettext(" action: The pool can be imported "
1842 "despite missing or damaged devices. The\n\tfault "
1843 "tolerance of the pool may be compromised if imported.\n"));
1846 case ZPOOL_STATUS_VERSION_NEWER
:
1847 (void) printf(gettext(" action: The pool cannot be "
1848 "imported. Access the pool on a system running "
1849 "newer\n\tsoftware, or recreate the pool from "
1852 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1853 (void) printf(gettext("action: The pool cannot be "
1854 "imported. Access the pool on a system that "
1855 "supports\n\tthe required feature(s), or recreate "
1856 "the pool from backup.\n"));
1858 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1859 (void) printf(gettext("action: The pool cannot be "
1860 "imported in read-write mode. Import the pool "
1862 "\t\"-o readonly=on\", access the pool on a system "
1863 "that supports the\n\trequired feature(s), or "
1864 "recreate the pool from backup.\n"));
1866 case ZPOOL_STATUS_MISSING_DEV_R
:
1867 case ZPOOL_STATUS_MISSING_DEV_NR
:
1868 case ZPOOL_STATUS_BAD_GUID_SUM
:
1869 (void) printf(gettext(" action: The pool cannot be "
1870 "imported. Attach the missing\n\tdevices and try "
1874 (void) printf(gettext(" action: The pool cannot be "
1875 "imported due to damaged devices or data.\n"));
1879 /* Print the comment attached to the pool. */
1880 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1881 (void) printf(gettext("comment: %s\n"), comment
);
1884 * If the state is "closed" or "can't open", and the aux state
1885 * is "corrupt data":
1887 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1888 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1889 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1890 if (pool_state
== POOL_STATE_DESTROYED
)
1891 (void) printf(gettext("\tThe pool was destroyed, "
1892 "but can be imported using the '-Df' flags.\n"));
1893 else if (pool_state
!= POOL_STATE_EXPORTED
)
1894 (void) printf(gettext("\tThe pool may be active on "
1895 "another system, but can be imported using\n\t"
1896 "the '-f' flag.\n"));
1900 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1903 (void) printf(gettext(" config:\n\n"));
1905 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1909 print_import_config(name
, nvroot
, namewidth
, 0);
1910 if (num_logs(nvroot
) > 0)
1911 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1913 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1914 (void) printf(gettext("\n\tAdditional devices are known to "
1915 "be part of this pool, though their\n\texact "
1916 "configuration cannot be determined.\n"));
1921 * Perform the import for the given configuration. This passes the heavy
1922 * lifting off to zpool_import_props(), and then mounts the datasets contained
1926 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1927 nvlist_t
*props
, int flags
)
1929 zpool_handle_t
*zhp
;
1934 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1937 verify(nvlist_lookup_uint64(config
,
1938 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1939 verify(nvlist_lookup_uint64(config
,
1940 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1941 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1942 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1943 "is formatted using an unsupported ZFS version\n"), name
);
1945 } else if (state
!= POOL_STATE_EXPORTED
&&
1946 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1947 uint64_t hostid
= 0;
1948 unsigned long system_hostid
= get_system_hostid();
1950 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1953 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
) {
1958 verify(nvlist_lookup_string(config
,
1959 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1960 verify(nvlist_lookup_uint64(config
,
1961 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
1963 (void) fprintf(stderr
, gettext("cannot import "
1964 "'%s': pool may be in use from other "
1965 "system, it was last accessed by %s "
1966 "(hostid: 0x%lx) on %s"), name
, hostname
,
1967 (unsigned long)hostid
,
1968 asctime(localtime(&t
)));
1969 (void) fprintf(stderr
, gettext("use '-f' to "
1970 "import anyway\n"));
1975 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
1978 if (newname
!= NULL
)
1979 name
= (char *)newname
;
1981 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
1984 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
1985 !(flags
& ZFS_IMPORT_ONLY
) &&
1986 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
1996 * zpool import [-d dir] [-D]
1997 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1998 * [-d dir | -c cachefile] [-f] -a
1999 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2000 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2002 * -c Read pool information from a cachefile instead of searching
2005 * -d Scan in a specific directory, other than /dev/. More than
2006 * one directory can be specified using multiple '-d' options.
2008 * -D Scan for previously destroyed pools or import all or only
2009 * specified destroyed pools.
2011 * -R Temporarily import the pool, with all mountpoints relative to
2012 * the given root. The pool will remain exported when the machine
2015 * -V Import even in the presence of faulted vdevs. This is an
2016 * intentionally undocumented option for testing purposes, and
2017 * treats the pool configuration as complete, leaving any bad
2018 * vdevs in the FAULTED state. In other words, it does verbatim
2021 * -f Force import, even if it appears that the pool is active.
2023 * -F Attempt rewind if necessary.
2025 * -n See if rewind would work, but don't actually rewind.
2027 * -N Import the pool but don't mount datasets.
2029 * -T Specify a starting txg to use for import. This option is
2030 * intentionally undocumented option for testing purposes.
2032 * -a Import all pools found.
2034 * -o Set property=value and/or temporary mount options (without '=').
2036 * The import command scans for pools to import, and import pools based on pool
2037 * name and GUID. The pool can also be renamed as part of the import process.
2040 zpool_do_import(int argc
, char **argv
)
2042 char **searchdirs
= NULL
;
2043 char *env
, *envdup
= NULL
;
2047 nvlist_t
*pools
= NULL
;
2048 boolean_t do_all
= B_FALSE
;
2049 boolean_t do_destroyed
= B_FALSE
;
2050 char *mntopts
= NULL
;
2053 uint64_t searchguid
= 0;
2054 char *searchname
= NULL
;
2056 nvlist_t
*found_config
;
2057 nvlist_t
*policy
= NULL
;
2058 nvlist_t
*props
= NULL
;
2060 int flags
= ZFS_IMPORT_NORMAL
;
2061 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2062 boolean_t dryrun
= B_FALSE
;
2063 boolean_t do_rewind
= B_FALSE
;
2064 boolean_t xtreme_rewind
= B_FALSE
;
2065 uint64_t pool_state
, txg
= -1ULL;
2066 char *cachefile
= NULL
;
2067 importargs_t idata
= { 0 };
2071 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2080 if (searchdirs
== NULL
) {
2081 searchdirs
= safe_malloc(sizeof (char *));
2083 char **tmp
= safe_malloc((nsearch
+ 1) *
2085 bcopy(searchdirs
, tmp
, nsearch
*
2090 searchdirs
[nsearch
++] = optarg
;
2093 do_destroyed
= B_TRUE
;
2096 flags
|= ZFS_IMPORT_ANY_HOST
;
2102 flags
|= ZFS_IMPORT_MISSING_LOG
;
2108 flags
|= ZFS_IMPORT_ONLY
;
2111 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2114 if (add_prop_list(optarg
, propval
,
2122 if (add_prop_list(zpool_prop_to_name(
2123 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2125 if (add_prop_list_default(zpool_prop_to_name(
2126 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2130 flags
|= ZFS_IMPORT_TEMP_NAME
;
2131 if (add_prop_list_default(zpool_prop_to_name(
2132 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2138 txg
= strtoull(optarg
, &endptr
, 0);
2139 if (errno
!= 0 || *endptr
!= '\0') {
2140 (void) fprintf(stderr
,
2141 gettext("invalid txg value\n"));
2144 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2147 flags
|= ZFS_IMPORT_VERBATIM
;
2150 xtreme_rewind
= B_TRUE
;
2153 (void) fprintf(stderr
, gettext("missing argument for "
2154 "'%c' option\n"), optopt
);
2158 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2167 if (cachefile
&& nsearch
!= 0) {
2168 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2172 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2173 (void) fprintf(stderr
,
2174 gettext("-n or -X only meaningful with -F\n"));
2178 rewind_policy
= ZPOOL_TRY_REWIND
;
2180 rewind_policy
= ZPOOL_DO_REWIND
;
2182 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2184 /* In the future, we can capture further policy and include it here */
2185 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2186 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2187 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2190 /* check argument count */
2193 (void) fprintf(stderr
, gettext("too many arguments\n"));
2198 (void) fprintf(stderr
, gettext("too many arguments\n"));
2203 * Check for the SYS_CONFIG privilege. We do this explicitly
2204 * here because otherwise any attempt to discover pools will
2207 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
2208 (void) fprintf(stderr
, gettext("cannot "
2209 "discover pools: permission denied\n"));
2210 if (searchdirs
!= NULL
)
2213 nvlist_free(policy
);
2219 * Depending on the arguments given, we do one of the following:
2221 * <none> Iterate through all pools and display information about
2224 * -a Iterate through all pools and try to import each one.
2226 * <id> Find the pool that corresponds to the given GUID/pool
2227 * name and import that one.
2229 * -D Above options applies only to destroyed pools.
2235 searchguid
= strtoull(argv
[0], &endptr
, 10);
2236 if (errno
!= 0 || *endptr
!= '\0')
2237 searchname
= argv
[0];
2238 found_config
= NULL
;
2241 * User specified a name or guid. Ensure it's unique.
2243 idata
.unique
= B_TRUE
;
2247 * Check the environment for the preferred search path.
2249 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2252 envdup
= strdup(env
);
2254 dir
= strtok(envdup
, ":");
2255 while (dir
!= NULL
) {
2256 if (searchdirs
== NULL
) {
2257 searchdirs
= safe_malloc(sizeof (char *));
2259 char **tmp
= safe_malloc((nsearch
+ 1) *
2261 bcopy(searchdirs
, tmp
, nsearch
*
2266 searchdirs
[nsearch
++] = dir
;
2267 dir
= strtok(NULL
, ":");
2271 idata
.path
= searchdirs
;
2272 idata
.paths
= nsearch
;
2273 idata
.poolname
= searchname
;
2274 idata
.guid
= searchguid
;
2275 idata
.cachefile
= cachefile
;
2277 pools
= zpool_search_import(g_zfs
, &idata
);
2279 if (pools
!= NULL
&& idata
.exists
&&
2280 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2281 (void) fprintf(stderr
, gettext("cannot import '%s': "
2282 "a pool with that name already exists\n"),
2284 (void) fprintf(stderr
, gettext("use the form '%s "
2285 "<pool | id> <newpool>' to give it a new name\n"),
2288 } else if (pools
== NULL
&& idata
.exists
) {
2289 (void) fprintf(stderr
, gettext("cannot import '%s': "
2290 "a pool with that name is already created/imported,\n"),
2292 (void) fprintf(stderr
, gettext("and no additional pools "
2293 "with that name were found\n"));
2295 } else if (pools
== NULL
) {
2297 (void) fprintf(stderr
, gettext("cannot import '%s': "
2298 "no such pool available\n"), argv
[0]);
2304 if (searchdirs
!= NULL
)
2308 nvlist_free(policy
);
2313 * At this point we have a list of import candidate configs. Even if
2314 * we were searching by pool name or guid, we still need to
2315 * post-process the list to deal with pool state and possible
2321 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2323 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2325 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2327 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2329 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2332 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2339 (void) printf("\n");
2342 err
|= do_import(config
, NULL
, mntopts
,
2345 show_import(config
);
2347 } else if (searchname
!= NULL
) {
2351 * We are searching for a pool based on name.
2353 verify(nvlist_lookup_string(config
,
2354 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2356 if (strcmp(name
, searchname
) == 0) {
2357 if (found_config
!= NULL
) {
2358 (void) fprintf(stderr
, gettext(
2359 "cannot import '%s': more than "
2360 "one matching pool\n"), searchname
);
2361 (void) fprintf(stderr
, gettext(
2362 "import by numeric ID instead\n"));
2365 found_config
= config
;
2371 * Search for a pool by guid.
2373 verify(nvlist_lookup_uint64(config
,
2374 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2376 if (guid
== searchguid
)
2377 found_config
= config
;
2382 * If we were searching for a specific pool, verify that we found a
2383 * pool, and then do the import.
2385 if (argc
!= 0 && err
== 0) {
2386 if (found_config
== NULL
) {
2387 (void) fprintf(stderr
, gettext("cannot import '%s': "
2388 "no such pool available\n"), argv
[0]);
2391 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2392 argv
[1], mntopts
, props
, flags
);
2397 * If we were just looking for pools, report an error if none were
2400 if (argc
== 0 && first
)
2401 (void) fprintf(stderr
,
2402 gettext("no pools available to import\n"));
2407 nvlist_free(policy
);
2408 if (searchdirs
!= NULL
)
2413 return (err
? 1 : 0);
2416 typedef struct iostat_cbdata
{
2417 boolean_t cb_verbose
;
2420 zpool_list_t
*cb_list
;
2424 print_iostat_separator(iostat_cbdata_t
*cb
)
2428 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2430 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2434 print_iostat_header(iostat_cbdata_t
*cb
)
2436 (void) printf("%*s capacity operations bandwidth\n",
2437 cb
->cb_namewidth
, "");
2438 (void) printf("%-*s alloc free read write read write\n",
2439 cb
->cb_namewidth
, "pool");
2440 print_iostat_separator(cb
);
2444 * Display a single statistic.
2447 print_one_stat(uint64_t value
)
2451 zfs_nicenum(value
, buf
, sizeof (buf
));
2452 (void) printf(" %5s", buf
);
2456 * Print out all the statistics for the given vdev. This can either be the
2457 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2458 * is a verbose output, and we don't want to display the toplevel pool stats.
2461 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2462 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2464 nvlist_t
**oldchild
, **newchild
;
2466 vdev_stat_t
*oldvs
, *newvs
;
2467 vdev_stat_t zerovs
= { 0 };
2472 if (oldnv
!= NULL
) {
2473 verify(nvlist_lookup_uint64_array(oldnv
,
2474 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2479 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2480 (uint64_t **)&newvs
, &c
) == 0);
2482 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2483 (void) printf("%*s%s", depth
, "", name
);
2485 (void) printf("%*s%s%*s", depth
, "", name
,
2486 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2488 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2493 scale
= (double)NANOSEC
/ tdelta
;
2495 /* only toplevel vdevs have capacity stats */
2496 if (newvs
->vs_space
== 0) {
2497 (void) printf(" - -");
2499 print_one_stat(newvs
->vs_alloc
);
2500 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2503 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2504 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2506 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2507 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2509 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2510 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2512 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2513 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2515 (void) printf("\n");
2517 if (!cb
->cb_verbose
)
2520 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2521 &newchild
, &children
) != 0)
2524 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2525 &oldchild
, &c
) != 0)
2528 for (c
= 0; c
< children
; c
++) {
2529 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2531 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2534 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2537 if (ishole
|| islog
)
2540 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2541 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2542 newchild
[c
], cb
, depth
+ 2);
2547 * Log device section
2550 if (num_logs(newnv
) > 0) {
2551 (void) printf("%-*s - - - - - "
2552 "-\n", cb
->cb_namewidth
, "logs");
2554 for (c
= 0; c
< children
; c
++) {
2555 uint64_t islog
= B_FALSE
;
2556 (void) nvlist_lookup_uint64(newchild
[c
],
2557 ZPOOL_CONFIG_IS_LOG
, &islog
);
2560 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2562 print_vdev_stats(zhp
, vname
, oldnv
?
2563 oldchild
[c
] : NULL
, newchild
[c
],
2572 * Include level 2 ARC devices in iostat output
2574 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2575 &newchild
, &children
) != 0)
2578 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2579 &oldchild
, &c
) != 0)
2583 (void) printf("%-*s - - - - - "
2584 "-\n", cb
->cb_namewidth
, "cache");
2585 for (c
= 0; c
< children
; c
++) {
2586 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2588 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2589 newchild
[c
], cb
, depth
+ 2);
2596 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2598 iostat_cbdata_t
*cb
= data
;
2602 * If the pool has disappeared, remove it from the list and continue.
2604 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2608 pool_list_remove(cb
->cb_list
, zhp
);
2614 * Callback to print out the iostats for the given pool.
2617 print_iostat(zpool_handle_t
*zhp
, void *data
)
2619 iostat_cbdata_t
*cb
= data
;
2620 nvlist_t
*oldconfig
, *newconfig
;
2621 nvlist_t
*oldnvroot
, *newnvroot
;
2623 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2625 if (cb
->cb_iteration
== 1)
2628 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2631 if (oldconfig
== NULL
)
2634 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2638 * Print out the statistics for the pool.
2640 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2643 print_iostat_separator(cb
);
2655 if (isatty(STDOUT_FILENO
)) {
2656 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
2658 columns
= ws
.ws_col
;
2667 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2669 iostat_cbdata_t
*cb
= data
;
2670 nvlist_t
*config
, *nvroot
;
2673 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2674 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2676 if (!cb
->cb_verbose
)
2677 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2679 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2684 * The width must be at least 10, but may be as large as the
2685 * column width - 42 so that we can still fit in one line.
2687 columns
= get_columns();
2689 if (cb
->cb_namewidth
< 10)
2690 cb
->cb_namewidth
= 10;
2691 if (cb
->cb_namewidth
> columns
- 42)
2692 cb
->cb_namewidth
= columns
- 42;
2698 * Parse the input string, get the 'interval' and 'count' value if there is one.
2701 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2704 unsigned long interval
= 0, count
= 0;
2708 * Determine if the last argument is an integer or a pool name
2710 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2714 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2716 if (*end
== '\0' && errno
== 0) {
2717 if (interval
== 0) {
2718 (void) fprintf(stderr
, gettext("interval "
2719 "cannot be zero\n"));
2723 * Ignore the last parameter
2728 * If this is not a valid number, just plow on. The
2729 * user will get a more informative error message later
2737 * If the last argument is also an integer, then we have both a count
2740 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2745 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2747 if (*end
== '\0' && errno
== 0) {
2748 if (interval
== 0) {
2749 (void) fprintf(stderr
, gettext("interval "
2750 "cannot be zero\n"));
2755 * Ignore the last parameter
2769 get_timestamp_arg(char c
)
2772 timestamp_fmt
= UDATE
;
2774 timestamp_fmt
= DDATE
;
2780 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2782 * -v Display statistics for individual vdevs
2783 * -T Display a timestamp in date(1) or Unix format
2785 * This command can be tricky because we want to be able to deal with pool
2786 * creation/destruction as well as vdev configuration changes. The bulk of this
2787 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2788 * on pool_list_update() to detect the addition of new pools. Configuration
2789 * changes are all handled within libzfs.
2792 zpool_do_iostat(int argc
, char **argv
)
2797 unsigned long interval
= 0, count
= 0;
2799 boolean_t verbose
= B_FALSE
;
2803 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2806 get_timestamp_arg(*optarg
);
2812 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2821 get_interval_count(&argc
, argv
, &interval
, &count
);
2824 * Construct the list of all interesting pools.
2827 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2830 if (pool_list_count(list
) == 0 && argc
!= 0) {
2831 pool_list_free(list
);
2835 if (pool_list_count(list
) == 0 && interval
== 0) {
2836 pool_list_free(list
);
2837 (void) fprintf(stderr
, gettext("no pools available\n"));
2842 * Enter the main iostat loop.
2845 cb
.cb_verbose
= verbose
;
2846 cb
.cb_iteration
= 0;
2847 cb
.cb_namewidth
= 0;
2850 pool_list_update(list
);
2852 if ((npools
= pool_list_count(list
)) == 0)
2853 (void) fprintf(stderr
, gettext("no pools available\n"));
2856 * Refresh all statistics. This is done as an
2857 * explicit step before calculating the maximum name
2858 * width, so that any * configuration changes are
2859 * properly accounted for.
2861 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
2865 * Iterate over all pools to determine the maximum width
2866 * for the pool / device name column across all pools.
2868 cb
.cb_namewidth
= 0;
2869 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
2872 if (timestamp_fmt
!= NODATE
)
2873 print_timestamp(timestamp_fmt
);
2876 * If it's the first time, or verbose mode, print the
2879 if (++cb
.cb_iteration
== 1 || verbose
)
2880 print_iostat_header(&cb
);
2882 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2885 * If there's more than one pool, and we're not in
2886 * verbose mode (which prints a separator for us),
2887 * then print a separator.
2889 if (npools
> 1 && !verbose
)
2890 print_iostat_separator(&cb
);
2893 (void) printf("\n");
2897 * Flush the output so that redirection to a file isn't buffered
2900 (void) fflush(stdout
);
2905 if (count
!= 0 && --count
== 0)
2908 (void) sleep(interval
);
2911 pool_list_free(list
);
2916 typedef struct list_cbdata
{
2917 boolean_t cb_verbose
;
2919 boolean_t cb_scripted
;
2920 zprop_list_t
*cb_proplist
;
2924 * Given a list of columns to display, output appropriate headers for each one.
2927 print_header(list_cbdata_t
*cb
)
2929 zprop_list_t
*pl
= cb
->cb_proplist
;
2930 char headerbuf
[ZPOOL_MAXPROPLEN
];
2932 boolean_t first
= B_TRUE
;
2933 boolean_t right_justify
;
2936 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2937 width
= pl
->pl_width
;
2938 if (first
&& cb
->cb_verbose
) {
2940 * Reset the width to accommodate the verbose listing
2943 width
= cb
->cb_namewidth
;
2951 right_justify
= B_FALSE
;
2952 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2953 header
= zpool_prop_column_name(pl
->pl_prop
);
2954 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2958 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2959 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2960 headerbuf
[i
] = '\0';
2964 if (pl
->pl_next
== NULL
&& !right_justify
)
2965 (void) printf("%s", header
);
2966 else if (right_justify
)
2967 (void) printf("%*s", (int)width
, header
);
2969 (void) printf("%-*s", (int)width
, header
);
2972 (void) printf("\n");
2976 * Given a pool and a list of properties, print out all the properties according
2977 * to the described layout.
2980 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
2982 zprop_list_t
*pl
= cb
->cb_proplist
;
2983 boolean_t first
= B_TRUE
;
2984 char property
[ZPOOL_MAXPROPLEN
];
2986 boolean_t right_justify
;
2989 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2991 width
= pl
->pl_width
;
2992 if (first
&& cb
->cb_verbose
) {
2994 * Reset the width to accommodate the verbose listing
2997 width
= cb
->cb_namewidth
;
3001 if (cb
->cb_scripted
)
3002 (void) printf("\t");
3009 right_justify
= B_FALSE
;
3010 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3011 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
3012 sizeof (property
), NULL
) != 0)
3017 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3018 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
3019 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
3020 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
3021 sizeof (property
)) == 0) {
3029 * If this is being called in scripted mode, or if this is the
3030 * last column and it is left-justified, don't include a width
3033 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3034 (void) printf("%s", propstr
);
3035 else if (right_justify
)
3036 (void) printf("%*s", (int)width
, propstr
);
3038 (void) printf("%-*s", (int)width
, propstr
);
3041 (void) printf("\n");
3045 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
3050 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3053 case ZPOOL_PROP_EXPANDSZ
:
3055 (void) strlcpy(propval
, "-", sizeof (propval
));
3057 zfs_nicenum(value
, propval
, sizeof (propval
));
3059 case ZPOOL_PROP_FRAGMENTATION
:
3060 if (value
== ZFS_FRAG_INVALID
) {
3061 (void) strlcpy(propval
, "-", sizeof (propval
));
3063 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3064 (unsigned long long)value
);
3067 case ZPOOL_PROP_CAPACITY
:
3068 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3069 (unsigned long long)value
);
3072 zfs_nicenum(value
, propval
, sizeof (propval
));
3076 (void) strlcpy(propval
, "-", sizeof (propval
));
3079 (void) printf("\t%s", propval
);
3081 (void) printf(" %*s", (int)width
, propval
);
3085 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3086 list_cbdata_t
*cb
, int depth
)
3092 boolean_t scripted
= cb
->cb_scripted
;
3094 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3095 (uint64_t **)&vs
, &c
) == 0);
3098 boolean_t toplevel
= (vs
->vs_space
!= 0);
3102 (void) printf("\t%s", name
);
3103 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3104 (void) printf("%*s%s", depth
, "", name
);
3106 (void) printf("%*s%s%*s", depth
, "", name
,
3107 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3110 * Print the properties for the individual vdevs. Some
3111 * properties are only applicable to toplevel vdevs. The
3112 * 'toplevel' boolean value is passed to the print_one_column()
3113 * to indicate that the value is valid.
3115 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
3117 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
3119 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
3120 scripted
, toplevel
);
3121 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
3123 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
3124 vs
->vs_fragmentation
, scripted
,
3125 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
3126 cap
= (vs
->vs_space
== 0) ? 0 :
3127 (vs
->vs_alloc
* 100 / vs
->vs_space
);
3128 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
3129 (void) printf("\n");
3132 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3133 &child
, &children
) != 0)
3136 for (c
= 0; c
< children
; c
++) {
3137 uint64_t ishole
= B_FALSE
;
3139 if (nvlist_lookup_uint64(child
[c
],
3140 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3143 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3144 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3149 * Include level 2 ARC devices in iostat output
3151 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3152 &child
, &children
) != 0)
3156 (void) printf("%-*s - - - - - "
3157 "-\n", cb
->cb_namewidth
, "cache");
3158 for (c
= 0; c
< children
; c
++) {
3159 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3161 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3169 * Generic callback function to list a pool.
3172 list_callback(zpool_handle_t
*zhp
, void *data
)
3174 list_cbdata_t
*cbp
= data
;
3178 config
= zpool_get_config(zhp
, NULL
);
3180 print_pool(zhp
, cbp
);
3181 if (!cbp
->cb_verbose
)
3184 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3186 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3192 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3194 * -H Scripted mode. Don't display headers, and separate properties
3196 * -o List of properties to display. Defaults to
3197 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3198 * "dedupratio,health,altroot"
3199 * -T Display a timestamp in date(1) or Unix format
3201 * List all pools in the system, whether or not they're healthy. Output space
3202 * statistics for each one, as well as health status summary.
3205 zpool_do_list(int argc
, char **argv
)
3209 list_cbdata_t cb
= { 0 };
3210 static char default_props
[] =
3211 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3212 "dedupratio,health,altroot";
3213 char *props
= default_props
;
3214 unsigned long interval
= 0, count
= 0;
3216 boolean_t first
= B_TRUE
;
3219 while ((c
= getopt(argc
, argv
, ":Ho:T:v")) != -1) {
3222 cb
.cb_scripted
= B_TRUE
;
3228 get_timestamp_arg(*optarg
);
3231 cb
.cb_verbose
= B_TRUE
;
3234 (void) fprintf(stderr
, gettext("missing argument for "
3235 "'%c' option\n"), optopt
);
3239 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3248 get_interval_count(&argc
, argv
, &interval
, &count
);
3250 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3253 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
, &ret
)) == NULL
)
3256 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3257 (void) printf(gettext("no pools available\n"));
3258 zprop_free_list(cb
.cb_proplist
);
3263 pool_list_update(list
);
3265 if (pool_list_count(list
) == 0)
3268 if (timestamp_fmt
!= NODATE
)
3269 print_timestamp(timestamp_fmt
);
3271 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3275 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3280 if (count
!= 0 && --count
== 0)
3283 (void) sleep(interval
);
3286 zprop_free_list(cb
.cb_proplist
);
3291 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3293 boolean_t force
= B_FALSE
;
3296 char *poolname
, *old_disk
, *new_disk
;
3297 zpool_handle_t
*zhp
;
3298 nvlist_t
*props
= NULL
;
3303 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
3309 if ((propval
= strchr(optarg
, '=')) == NULL
) {
3310 (void) fprintf(stderr
, gettext("missing "
3311 "'=' for -o option\n"));
3317 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
3318 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
3322 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3331 /* get pool name and check number of arguments */
3333 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3340 (void) fprintf(stderr
,
3341 gettext("missing <device> specification\n"));
3349 (void) fprintf(stderr
,
3350 gettext("missing <new_device> specification\n"));
3353 new_disk
= old_disk
;
3363 (void) fprintf(stderr
, gettext("too many arguments\n"));
3367 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3370 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3371 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3377 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
3379 if (nvroot
== NULL
) {
3384 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3386 nvlist_free(nvroot
);
3393 * zpool replace [-f] <pool> <device> <new_device>
3395 * -f Force attach, even if <new_device> appears to be in use.
3397 * Replace <device> with <new_device>.
3401 zpool_do_replace(int argc
, char **argv
)
3403 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3407 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3409 * -f Force attach, even if <new_device> appears to be in use.
3410 * -o Set property=value.
3412 * Attach <new_device> to the mirror containing <device>. If <device> is not
3413 * part of a mirror, then <device> will be transformed into a mirror of
3414 * <device> and <new_device>. In either case, <new_device> will begin life
3415 * with a DTL of [0, now], and will immediately begin to resilver itself.
3418 zpool_do_attach(int argc
, char **argv
)
3420 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3424 * zpool detach [-f] <pool> <device>
3426 * -f Force detach of <device>, even if DTLs argue against it
3427 * (not supported yet)
3429 * Detach a device from a mirror. The operation will be refused if <device>
3430 * is the last device in the mirror, or if the DTLs indicate that this device
3431 * has the only valid copy of some data.
3435 zpool_do_detach(int argc
, char **argv
)
3438 char *poolname
, *path
;
3439 zpool_handle_t
*zhp
;
3443 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3447 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3456 /* get pool name and check number of arguments */
3458 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3463 (void) fprintf(stderr
,
3464 gettext("missing <device> specification\n"));
3471 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3474 ret
= zpool_vdev_detach(zhp
, path
);
3482 * zpool split [-n] [-o prop=val] ...
3484 * [-R altroot] <pool> <newpool> [<device> ...]
3486 * -n Do not split the pool, but display the resulting layout if
3487 * it were to be split.
3488 * -o Set property=value, or set mount options.
3489 * -R Mount the split-off pool under an alternate root.
3491 * Splits the named pool and gives it the new pool name. Devices to be split
3492 * off may be listed, provided that no more than one device is specified
3493 * per top-level vdev mirror. The newly split pool is left in an exported
3494 * state unless -R is specified.
3496 * Restrictions: the top-level of the pool pool must only be made up of
3497 * mirrors; all devices in the pool must be healthy; no device may be
3498 * undergoing a resilvering operation.
3501 zpool_do_split(int argc
, char **argv
)
3503 char *srcpool
, *newpool
, *propval
;
3504 char *mntopts
= NULL
;
3507 zpool_handle_t
*zhp
;
3508 nvlist_t
*config
, *props
= NULL
;
3510 flags
.dryrun
= B_FALSE
;
3511 flags
.import
= B_FALSE
;
3514 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3517 flags
.import
= B_TRUE
;
3519 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3520 &props
, B_TRUE
) != 0) {
3527 flags
.dryrun
= B_TRUE
;
3530 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3533 if (add_prop_list(optarg
, propval
,
3534 &props
, B_TRUE
) != 0) {
3544 (void) fprintf(stderr
, gettext("missing argument for "
3545 "'%c' option\n"), optopt
);
3549 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3556 if (!flags
.import
&& mntopts
!= NULL
) {
3557 (void) fprintf(stderr
, gettext("setting mntopts is only "
3558 "valid when importing the pool\n"));
3566 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3570 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3580 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3583 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3584 if (config
== NULL
) {
3588 (void) printf(gettext("would create '%s' with the "
3589 "following layout:\n\n"), newpool
);
3590 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3592 nvlist_free(config
);
3597 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3601 * The split was successful. Now we need to open the new
3602 * pool and import it.
3604 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3606 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3607 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3609 (void) fprintf(stderr
, gettext("Split was successful, but "
3610 "the datasets could not all be mounted\n"));
3611 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3612 "different altroot\n"), "zpool import");
3622 * zpool online <pool> <device> ...
3625 zpool_do_online(int argc
, char **argv
)
3629 zpool_handle_t
*zhp
;
3631 vdev_state_t newstate
;
3635 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3638 flags
|= ZFS_ONLINE_EXPAND
;
3642 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3651 /* get pool name and check number of arguments */
3653 (void) fprintf(stderr
, gettext("missing pool name\n"));
3657 (void) fprintf(stderr
, gettext("missing device name\n"));
3663 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3666 for (i
= 1; i
< argc
; i
++) {
3667 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3668 if (newstate
!= VDEV_STATE_HEALTHY
) {
3669 (void) printf(gettext("warning: device '%s' "
3670 "onlined, but remains in faulted state\n"),
3672 if (newstate
== VDEV_STATE_FAULTED
)
3673 (void) printf(gettext("use 'zpool "
3674 "clear' to restore a faulted "
3677 (void) printf(gettext("use 'zpool "
3678 "replace' to replace devices "
3679 "that are no longer present\n"));
3692 * zpool offline [-ft] <pool> <device> ...
3694 * -f Force the device into the offline state, even if doing
3695 * so would appear to compromise pool availability.
3696 * (not supported yet)
3698 * -t Only take the device off-line temporarily. The offline
3699 * state will not be persistent across reboots.
3703 zpool_do_offline(int argc
, char **argv
)
3707 zpool_handle_t
*zhp
;
3709 boolean_t istmp
= B_FALSE
;
3712 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3719 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3728 /* get pool name and check number of arguments */
3730 (void) fprintf(stderr
, gettext("missing pool name\n"));
3734 (void) fprintf(stderr
, gettext("missing device name\n"));
3740 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3743 for (i
= 1; i
< argc
; i
++) {
3744 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3754 * zpool clear <pool> [device]
3756 * Clear all errors associated with a pool or a particular device.
3759 zpool_do_clear(int argc
, char **argv
)
3763 boolean_t dryrun
= B_FALSE
;
3764 boolean_t do_rewind
= B_FALSE
;
3765 boolean_t xtreme_rewind
= B_FALSE
;
3766 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3767 nvlist_t
*policy
= NULL
;
3768 zpool_handle_t
*zhp
;
3769 char *pool
, *device
;
3772 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3781 xtreme_rewind
= B_TRUE
;
3784 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3794 (void) fprintf(stderr
, gettext("missing pool name\n"));
3799 (void) fprintf(stderr
, gettext("too many arguments\n"));
3803 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3804 (void) fprintf(stderr
,
3805 gettext("-n or -X only meaningful with -F\n"));
3809 rewind_policy
= ZPOOL_TRY_REWIND
;
3811 rewind_policy
= ZPOOL_DO_REWIND
;
3813 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3815 /* In future, further rewind policy choices can be passed along here */
3816 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3817 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3821 device
= argc
== 2 ? argv
[1] : NULL
;
3823 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3824 nvlist_free(policy
);
3828 if (zpool_clear(zhp
, device
, policy
) != 0)
3833 nvlist_free(policy
);
3839 * zpool reguid <pool>
3842 zpool_do_reguid(int argc
, char **argv
)
3846 zpool_handle_t
*zhp
;
3850 while ((c
= getopt(argc
, argv
, "")) != -1) {
3853 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3862 /* get pool name and check number of arguments */
3864 (void) fprintf(stderr
, gettext("missing pool name\n"));
3869 (void) fprintf(stderr
, gettext("too many arguments\n"));
3874 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3877 ret
= zpool_reguid(zhp
);
3885 * zpool reopen <pool>
3887 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3890 zpool_do_reopen(int argc
, char **argv
)
3894 zpool_handle_t
*zhp
;
3898 while ((c
= getopt(argc
, argv
, "")) != -1) {
3901 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3911 (void) fprintf(stderr
, gettext("missing pool name\n"));
3916 (void) fprintf(stderr
, gettext("too many arguments\n"));
3921 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3924 ret
= zpool_reopen(zhp
);
3929 typedef struct scrub_cbdata
{
3936 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3938 scrub_cbdata_t
*cb
= data
;
3942 * Ignore faulted pools.
3944 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3945 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3946 "currently unavailable\n"), zpool_get_name(zhp
));
3950 err
= zpool_scan(zhp
, cb
->cb_type
);
3956 * zpool scrub [-s] <pool> ...
3958 * -s Stop. Stops any in-progress scrub.
3961 zpool_do_scrub(int argc
, char **argv
)
3966 cb
.cb_type
= POOL_SCAN_SCRUB
;
3969 while ((c
= getopt(argc
, argv
, "s")) != -1) {
3972 cb
.cb_type
= POOL_SCAN_NONE
;
3975 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3987 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3991 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
3994 typedef struct status_cbdata
{
3996 boolean_t cb_allpools
;
3997 boolean_t cb_verbose
;
3998 boolean_t cb_explain
;
4000 boolean_t cb_dedup_stats
;
4004 * Print out detailed scrub status.
4007 print_scan_status(pool_scan_stat_t
*ps
)
4010 uint64_t elapsed
, mins_left
, hours_left
;
4011 uint64_t pass_exam
, examined
, total
;
4013 double fraction_done
;
4014 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4016 (void) printf(gettext(" scan: "));
4018 /* If there's never been a scan, there's not much to say. */
4019 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
4020 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
4021 (void) printf(gettext("none requested\n"));
4025 start
= ps
->pss_start_time
;
4026 end
= ps
->pss_end_time
;
4027 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
4029 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
4030 ps
->pss_func
== POOL_SCAN_RESILVER
);
4032 * Scan is finished or canceled.
4034 if (ps
->pss_state
== DSS_FINISHED
) {
4035 uint64_t minutes_taken
= (end
- start
) / 60;
4038 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4039 fmt
= gettext("scrub repaired %s in %lluh%um with "
4040 "%llu errors on %s");
4041 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4042 fmt
= gettext("resilvered %s in %lluh%um with "
4043 "%llu errors on %s");
4046 (void) printf(fmt
, processed_buf
,
4047 (u_longlong_t
)(minutes_taken
/ 60),
4048 (uint_t
)(minutes_taken
% 60),
4049 (u_longlong_t
)ps
->pss_errors
,
4050 ctime((time_t *)&end
));
4052 } else if (ps
->pss_state
== DSS_CANCELED
) {
4053 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4054 (void) printf(gettext("scrub canceled on %s"),
4056 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4057 (void) printf(gettext("resilver canceled on %s"),
4063 assert(ps
->pss_state
== DSS_SCANNING
);
4066 * Scan is in progress.
4068 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4069 (void) printf(gettext("scrub in progress since %s"),
4071 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4072 (void) printf(gettext("resilver in progress since %s"),
4076 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
4077 total
= ps
->pss_to_examine
;
4078 fraction_done
= (double)examined
/ total
;
4080 /* elapsed time for this pass */
4081 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4082 elapsed
= elapsed
? elapsed
: 1;
4083 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4084 rate
= pass_exam
/ elapsed
;
4085 rate
= rate
? rate
: 1;
4086 mins_left
= ((total
- examined
) / rate
) / 60;
4087 hours_left
= mins_left
/ 60;
4089 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4090 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4091 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4094 * do not print estimated time if hours_left is more than 30 days
4096 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4097 examined_buf
, total_buf
, rate_buf
);
4098 if (hours_left
< (30 * 24)) {
4099 (void) printf(gettext(", %lluh%um to go\n"),
4100 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4102 (void) printf(gettext(
4103 ", (scan is slow, no estimated time)\n"));
4106 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4107 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4108 processed_buf
, 100 * fraction_done
);
4109 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4110 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4111 processed_buf
, 100 * fraction_done
);
4116 print_error_log(zpool_handle_t
*zhp
)
4118 nvlist_t
*nverrlist
= NULL
;
4121 size_t len
= MAXPATHLEN
* 2;
4123 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4124 (void) printf("errors: List of errors unavailable "
4125 "(insufficient privileges)\n");
4129 (void) printf("errors: Permanent errors have been "
4130 "detected in the following files:\n\n");
4132 pathname
= safe_malloc(len
);
4134 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4136 uint64_t dsobj
, obj
;
4138 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4139 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4141 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4143 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4144 (void) printf("%7s %s\n", "", pathname
);
4147 nvlist_free(nverrlist
);
4151 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4160 (void) printf(gettext("\tspares\n"));
4162 for (i
= 0; i
< nspares
; i
++) {
4163 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
4164 print_status_config(zhp
, name
, spares
[i
],
4165 namewidth
, 2, B_TRUE
);
4171 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4180 (void) printf(gettext("\tcache\n"));
4182 for (i
= 0; i
< nl2cache
; i
++) {
4183 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
4184 print_status_config(zhp
, name
, l2cache
[i
],
4185 namewidth
, 2, B_FALSE
);
4191 print_dedup_stats(nvlist_t
*config
)
4193 ddt_histogram_t
*ddh
;
4199 * If the pool was faulted then we may not have been able to
4200 * obtain the config. Otherwise, if we have anything in the dedup
4201 * table continue processing the stats.
4203 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4204 (uint64_t **)&ddo
, &c
) != 0)
4207 (void) printf("\n");
4208 (void) printf(gettext(" dedup: "));
4209 if (ddo
->ddo_count
== 0) {
4210 (void) printf(gettext("no DDT entries\n"));
4214 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4215 (u_longlong_t
)ddo
->ddo_count
,
4216 (u_longlong_t
)ddo
->ddo_dspace
,
4217 (u_longlong_t
)ddo
->ddo_mspace
);
4219 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4220 (uint64_t **)&dds
, &c
) == 0);
4221 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4222 (uint64_t **)&ddh
, &c
) == 0);
4223 zpool_dump_ddt(dds
, ddh
);
4227 * Display a summary of pool status. Displays a summary such as:
4231 * reason: One or more devices ...
4232 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4238 * When given the '-v' option, we print out the complete config. If the '-e'
4239 * option is specified, then we print out error rate information as well.
4242 status_callback(zpool_handle_t
*zhp
, void *data
)
4244 status_cbdata_t
*cbp
= data
;
4245 nvlist_t
*config
, *nvroot
;
4247 zpool_status_t reason
;
4248 zpool_errata_t errata
;
4253 config
= zpool_get_config(zhp
, NULL
);
4254 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
4259 * If we were given 'zpool status -x', only report those pools with
4262 if (cbp
->cb_explain
&&
4263 (reason
== ZPOOL_STATUS_OK
||
4264 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4265 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4266 if (!cbp
->cb_allpools
) {
4267 (void) printf(gettext("pool '%s' is healthy\n"),
4268 zpool_get_name(zhp
));
4270 cbp
->cb_first
= B_FALSE
;
4276 cbp
->cb_first
= B_FALSE
;
4278 (void) printf("\n");
4280 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4282 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4283 (uint64_t **)&vs
, &c
) == 0);
4284 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4286 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4287 (void) printf(gettext(" state: %s\n"), health
);
4290 case ZPOOL_STATUS_MISSING_DEV_R
:
4291 (void) printf(gettext("status: One or more devices could not "
4292 "be opened. Sufficient replicas exist for\n\tthe pool to "
4293 "continue functioning in a degraded state.\n"));
4294 (void) printf(gettext("action: Attach the missing device and "
4295 "online it using 'zpool online'.\n"));
4298 case ZPOOL_STATUS_MISSING_DEV_NR
:
4299 (void) printf(gettext("status: One or more devices could not "
4300 "be opened. There are insufficient\n\treplicas for the "
4301 "pool to continue functioning.\n"));
4302 (void) printf(gettext("action: Attach the missing device and "
4303 "online it using 'zpool online'.\n"));
4306 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4307 (void) printf(gettext("status: One or more devices could not "
4308 "be used because the label is missing or\n\tinvalid. "
4309 "Sufficient replicas exist for the pool to continue\n\t"
4310 "functioning in a degraded state.\n"));
4311 (void) printf(gettext("action: Replace the device using "
4312 "'zpool replace'.\n"));
4315 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4316 (void) printf(gettext("status: One or more devices could not "
4317 "be used because the label is missing \n\tor invalid. "
4318 "There are insufficient replicas for the pool to "
4319 "continue\n\tfunctioning.\n"));
4320 zpool_explain_recover(zpool_get_handle(zhp
),
4321 zpool_get_name(zhp
), reason
, config
);
4324 case ZPOOL_STATUS_FAILING_DEV
:
4325 (void) printf(gettext("status: One or more devices has "
4326 "experienced an unrecoverable error. An\n\tattempt was "
4327 "made to correct the error. Applications are "
4329 (void) printf(gettext("action: Determine if the device needs "
4330 "to be replaced, and clear the errors\n\tusing "
4331 "'zpool clear' or replace the device with 'zpool "
4335 case ZPOOL_STATUS_OFFLINE_DEV
:
4336 (void) printf(gettext("status: One or more devices has "
4337 "been taken offline by the administrator.\n\tSufficient "
4338 "replicas exist for the pool to continue functioning in "
4339 "a\n\tdegraded state.\n"));
4340 (void) printf(gettext("action: Online the device using "
4341 "'zpool online' or replace the device with\n\t'zpool "
4345 case ZPOOL_STATUS_REMOVED_DEV
:
4346 (void) printf(gettext("status: One or more devices has "
4347 "been removed by the administrator.\n\tSufficient "
4348 "replicas exist for the pool to continue functioning in "
4349 "a\n\tdegraded state.\n"));
4350 (void) printf(gettext("action: Online the device using "
4351 "'zpool online' or replace the device with\n\t'zpool "
4355 case ZPOOL_STATUS_RESILVERING
:
4356 (void) printf(gettext("status: One or more devices is "
4357 "currently being resilvered. The pool will\n\tcontinue "
4358 "to function, possibly in a degraded state.\n"));
4359 (void) printf(gettext("action: Wait for the resilver to "
4363 case ZPOOL_STATUS_CORRUPT_DATA
:
4364 (void) printf(gettext("status: One or more devices has "
4365 "experienced an error resulting in data\n\tcorruption. "
4366 "Applications may be affected.\n"));
4367 (void) printf(gettext("action: Restore the file in question "
4368 "if possible. Otherwise restore the\n\tentire pool from "
4372 case ZPOOL_STATUS_CORRUPT_POOL
:
4373 (void) printf(gettext("status: The pool metadata is corrupted "
4374 "and the pool cannot be opened.\n"));
4375 zpool_explain_recover(zpool_get_handle(zhp
),
4376 zpool_get_name(zhp
), reason
, config
);
4379 case ZPOOL_STATUS_VERSION_OLDER
:
4380 (void) printf(gettext("status: The pool is formatted using a "
4381 "legacy on-disk format. The pool can\n\tstill be used, "
4382 "but some features are unavailable.\n"));
4383 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4384 "upgrade'. Once this is done, the\n\tpool will no longer "
4385 "be accessible on software that does not support\n\t"
4386 "feature flags.\n"));
4389 case ZPOOL_STATUS_VERSION_NEWER
:
4390 (void) printf(gettext("status: The pool has been upgraded to a "
4391 "newer, incompatible on-disk version.\n\tThe pool cannot "
4392 "be accessed on this system.\n"));
4393 (void) printf(gettext("action: Access the pool from a system "
4394 "running more recent software, or\n\trestore the pool from "
4398 case ZPOOL_STATUS_FEAT_DISABLED
:
4399 (void) printf(gettext("status: Some supported features are not "
4400 "enabled on the pool. The pool can\n\tstill be used, but "
4401 "some features are unavailable.\n"));
4402 (void) printf(gettext("action: Enable all features using "
4403 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4404 "longer be accessible by software that does not support\n\t"
4405 "the features. See zpool-features(5) for details.\n"));
4408 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4409 (void) printf(gettext("status: The pool cannot be accessed on "
4410 "this system because it uses the\n\tfollowing feature(s) "
4411 "not supported on this system:\n"));
4412 zpool_print_unsup_feat(config
);
4413 (void) printf("\n");
4414 (void) printf(gettext("action: Access the pool from a system "
4415 "that supports the required feature(s),\n\tor restore the "
4416 "pool from backup.\n"));
4419 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4420 (void) printf(gettext("status: The pool can only be accessed "
4421 "in read-only mode on this system. It\n\tcannot be "
4422 "accessed in read-write mode because it uses the "
4423 "following\n\tfeature(s) not supported on this system:\n"));
4424 zpool_print_unsup_feat(config
);
4425 (void) printf("\n");
4426 (void) printf(gettext("action: The pool cannot be accessed in "
4427 "read-write mode. Import the pool with\n"
4428 "\t\"-o readonly=on\", access the pool from a system that "
4429 "supports the\n\trequired feature(s), or restore the "
4430 "pool from backup.\n"));
4433 case ZPOOL_STATUS_FAULTED_DEV_R
:
4434 (void) printf(gettext("status: One or more devices are "
4435 "faulted in response to persistent errors.\n\tSufficient "
4436 "replicas exist for the pool to continue functioning "
4437 "in a\n\tdegraded state.\n"));
4438 (void) printf(gettext("action: Replace the faulted device, "
4439 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4442 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4443 (void) printf(gettext("status: One or more devices are "
4444 "faulted in response to persistent errors. There are "
4445 "insufficient replicas for the pool to\n\tcontinue "
4447 (void) printf(gettext("action: Destroy and re-create the pool "
4448 "from a backup source. Manually marking the device\n"
4449 "\trepaired using 'zpool clear' may allow some data "
4450 "to be recovered.\n"));
4453 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4454 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4455 (void) printf(gettext("status: One or more devices are "
4456 "faulted in response to IO failures.\n"));
4457 (void) printf(gettext("action: Make sure the affected devices "
4458 "are connected, then run 'zpool clear'.\n"));
4461 case ZPOOL_STATUS_BAD_LOG
:
4462 (void) printf(gettext("status: An intent log record "
4463 "could not be read.\n"
4464 "\tWaiting for adminstrator intervention to fix the "
4465 "faulted pool.\n"));
4466 (void) printf(gettext("action: Either restore the affected "
4467 "device(s) and run 'zpool online',\n"
4468 "\tor ignore the intent log records by running "
4469 "'zpool clear'.\n"));
4472 case ZPOOL_STATUS_HOSTID_MISMATCH
:
4473 (void) printf(gettext("status: Mismatch between pool hostid "
4474 "and system hostid on imported pool.\n\tThis pool was "
4475 "previously imported into a system with a different "
4476 "hostid,\n\tand then was verbatim imported into this "
4478 (void) printf(gettext("action: Export this pool on all systems "
4479 "on which it is imported.\n"
4480 "\tThen import it to correct the mismatch.\n"));
4483 case ZPOOL_STATUS_ERRATA
:
4484 (void) printf(gettext("status: Errata #%d detected.\n"),
4488 case ZPOOL_ERRATA_NONE
:
4491 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
4492 (void) printf(gettext("action: To correct the issue "
4493 "run 'zpool scrub'.\n"));
4498 * All errata which allow the pool to be imported
4499 * must contain an action message.
4507 * The remaining errors can't actually be generated, yet.
4509 assert(reason
== ZPOOL_STATUS_OK
);
4513 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4516 if (config
!= NULL
) {
4519 nvlist_t
**spares
, **l2cache
;
4520 uint_t nspares
, nl2cache
;
4521 pool_scan_stat_t
*ps
= NULL
;
4523 (void) nvlist_lookup_uint64_array(nvroot
,
4524 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4525 print_scan_status(ps
);
4527 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4531 (void) printf(gettext("config:\n\n"));
4532 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4533 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4534 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4535 namewidth
, 0, B_FALSE
);
4537 if (num_logs(nvroot
) > 0)
4538 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4539 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4540 &l2cache
, &nl2cache
) == 0)
4541 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4543 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4544 &spares
, &nspares
) == 0)
4545 print_spares(zhp
, spares
, nspares
, namewidth
);
4547 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4549 nvlist_t
*nverrlist
= NULL
;
4552 * If the approximate error count is small, get a
4553 * precise count by fetching the entire log and
4554 * uniquifying the results.
4556 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4557 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4562 while ((elem
= nvlist_next_nvpair(nverrlist
,
4567 nvlist_free(nverrlist
);
4569 (void) printf("\n");
4572 (void) printf(gettext("errors: No known data "
4574 else if (!cbp
->cb_verbose
)
4575 (void) printf(gettext("errors: %llu data "
4576 "errors, use '-v' for a list\n"),
4577 (u_longlong_t
)nerr
);
4579 print_error_log(zhp
);
4582 if (cbp
->cb_dedup_stats
)
4583 print_dedup_stats(config
);
4585 (void) printf(gettext("config: The configuration cannot be "
4593 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4595 * -v Display complete error logs
4596 * -x Display only pools with potential problems
4597 * -D Display dedup status (undocumented)
4598 * -T Display a timestamp in date(1) or Unix format
4600 * Describes the health status of all pools or some subset.
4603 zpool_do_status(int argc
, char **argv
)
4607 unsigned long interval
= 0, count
= 0;
4608 status_cbdata_t cb
= { 0 };
4611 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4614 cb
.cb_verbose
= B_TRUE
;
4617 cb
.cb_explain
= B_TRUE
;
4620 cb
.cb_dedup_stats
= B_TRUE
;
4623 get_timestamp_arg(*optarg
);
4626 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4635 get_interval_count(&argc
, argv
, &interval
, &count
);
4638 cb
.cb_allpools
= B_TRUE
;
4640 cb
.cb_first
= B_TRUE
;
4643 if (timestamp_fmt
!= NODATE
)
4644 print_timestamp(timestamp_fmt
);
4646 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4647 status_callback
, &cb
);
4649 if (argc
== 0 && cb
.cb_count
== 0)
4650 (void) fprintf(stderr
, gettext("no pools available\n"));
4651 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4652 (void) printf(gettext("all pools are healthy\n"));
4660 if (count
!= 0 && --count
== 0)
4663 (void) sleep(interval
);
4669 typedef struct upgrade_cbdata
{
4672 uint64_t cb_version
;
4677 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
4679 int zfs_version
= (int) zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
4680 int *count
= (int *)unsupp_fs
;
4682 if (zfs_version
> ZPL_VERSION
) {
4683 (void) printf(gettext("%s (v%d) is not supported by this "
4684 "implementation of ZFS.\n"),
4685 zfs_get_name(zhp
), zfs_version
);
4689 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
4697 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4701 uint64_t oldversion
;
4704 config
= zpool_get_config(zhp
, NULL
);
4705 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4708 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4709 assert(oldversion
< version
);
4711 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
4716 (void) fprintf(stderr
, gettext("Upgrade not performed due "
4717 "to %d unsupported filesystems (max v%d).\n"),
4718 unsupp_fs
, (int) ZPL_VERSION
);
4722 ret
= zpool_upgrade(zhp
, version
);
4726 if (version
>= SPA_VERSION_FEATURES
) {
4727 (void) printf(gettext("Successfully upgraded "
4728 "'%s' from version %llu to feature flags.\n"),
4729 zpool_get_name(zhp
), (u_longlong_t
) oldversion
);
4731 (void) printf(gettext("Successfully upgraded "
4732 "'%s' from version %llu to version %llu.\n"),
4733 zpool_get_name(zhp
), (u_longlong_t
) oldversion
,
4734 (u_longlong_t
) version
);
4741 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4744 boolean_t firstff
= B_TRUE
;
4745 nvlist_t
*enabled
= zpool_get_features(zhp
);
4748 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4749 const char *fname
= spa_feature_table
[i
].fi_uname
;
4750 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4751 if (!nvlist_exists(enabled
, fguid
)) {
4753 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4754 ret
= zpool_set_prop(zhp
, propname
,
4755 ZFS_FEATURE_ENABLED
);
4763 (void) printf(gettext("Enabled the "
4764 "following features on '%s':\n"),
4765 zpool_get_name(zhp
));
4768 (void) printf(gettext(" %s\n"), fname
);
4779 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4781 upgrade_cbdata_t
*cbp
= arg
;
4784 boolean_t printnl
= B_FALSE
;
4787 config
= zpool_get_config(zhp
, NULL
);
4788 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4791 assert(SPA_VERSION_IS_SUPPORTED(version
));
4793 if (version
< cbp
->cb_version
) {
4794 cbp
->cb_first
= B_FALSE
;
4795 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4801 * If they did "zpool upgrade -a", then we could
4802 * be doing ioctls to different pools. We need
4803 * to log this history once to each pool, and bypass
4804 * the normal history logging that happens in main().
4806 (void) zpool_log_history(g_zfs
, history_str
);
4807 log_history
= B_FALSE
;
4810 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4812 ret
= upgrade_enable_all(zhp
, &count
);
4817 cbp
->cb_first
= B_FALSE
;
4823 (void) printf(gettext("\n"));
4830 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4832 upgrade_cbdata_t
*cbp
= arg
;
4836 config
= zpool_get_config(zhp
, NULL
);
4837 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4840 assert(SPA_VERSION_IS_SUPPORTED(version
));
4842 if (version
< SPA_VERSION_FEATURES
) {
4843 if (cbp
->cb_first
) {
4844 (void) printf(gettext("The following pools are "
4845 "formatted with legacy version numbers and can\n"
4846 "be upgraded to use feature flags. After "
4847 "being upgraded, these pools\nwill no "
4848 "longer be accessible by software that does not "
4849 "support feature\nflags.\n\n"));
4850 (void) printf(gettext("VER POOL\n"));
4851 (void) printf(gettext("--- ------------\n"));
4852 cbp
->cb_first
= B_FALSE
;
4855 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4856 zpool_get_name(zhp
));
4863 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4865 upgrade_cbdata_t
*cbp
= arg
;
4869 config
= zpool_get_config(zhp
, NULL
);
4870 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4873 if (version
>= SPA_VERSION_FEATURES
) {
4875 boolean_t poolfirst
= B_TRUE
;
4876 nvlist_t
*enabled
= zpool_get_features(zhp
);
4878 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4879 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4880 const char *fname
= spa_feature_table
[i
].fi_uname
;
4881 if (!nvlist_exists(enabled
, fguid
)) {
4882 if (cbp
->cb_first
) {
4883 (void) printf(gettext("\nSome "
4884 "supported features are not "
4885 "enabled on the following pools. "
4886 "Once a\nfeature is enabled the "
4887 "pool may become incompatible with "
4888 "software\nthat does not support "
4890 "zpool-features(5) for "
4892 (void) printf(gettext("POOL "
4894 (void) printf(gettext("------"
4896 cbp
->cb_first
= B_FALSE
;
4900 (void) printf(gettext("%s\n"),
4901 zpool_get_name(zhp
));
4902 poolfirst
= B_FALSE
;
4905 (void) printf(gettext(" %s\n"), fname
);
4908 * If they did "zpool upgrade -a", then we could
4909 * be doing ioctls to different pools. We need
4910 * to log this history once to each pool, and bypass
4911 * the normal history logging that happens in main().
4913 (void) zpool_log_history(g_zfs
, history_str
);
4914 log_history
= B_FALSE
;
4923 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4925 boolean_t printnl
= B_FALSE
;
4926 upgrade_cbdata_t
*cbp
= data
;
4927 uint64_t cur_version
;
4930 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4931 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
4932 "Pool 'log' must be renamed using export and import"
4937 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4938 if (cur_version
> cbp
->cb_version
) {
4939 (void) printf(gettext("Pool '%s' is already formatted "
4940 "using more current version '%llu'.\n\n"),
4941 zpool_get_name(zhp
), (u_longlong_t
) cur_version
);
4945 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
4946 (void) printf(gettext("Pool '%s' is already formatted "
4947 "using version %llu.\n\n"), zpool_get_name(zhp
),
4948 (u_longlong_t
) cbp
->cb_version
);
4952 if (cur_version
!= cbp
->cb_version
) {
4954 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4959 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4961 ret
= upgrade_enable_all(zhp
, &count
);
4967 } else if (cur_version
== SPA_VERSION
) {
4968 (void) printf(gettext("Pool '%s' already has all "
4969 "supported features enabled.\n"),
4970 zpool_get_name(zhp
));
4975 (void) printf(gettext("\n"));
4984 * zpool upgrade [-V version] <-a | pool ...>
4986 * With no arguments, display downrev'd ZFS pool available for upgrade.
4987 * Individual pools can be upgraded by specifying the pool, and '-a' will
4988 * upgrade all pools.
4991 zpool_do_upgrade(int argc
, char **argv
)
4994 upgrade_cbdata_t cb
= { 0 };
4996 boolean_t showversions
= B_FALSE
;
4997 boolean_t upgradeall
= B_FALSE
;
5002 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
5005 upgradeall
= B_TRUE
;
5008 showversions
= B_TRUE
;
5011 cb
.cb_version
= strtoll(optarg
, &end
, 10);
5013 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
5014 (void) fprintf(stderr
,
5015 gettext("invalid version '%s'\n"), optarg
);
5020 (void) fprintf(stderr
, gettext("missing argument for "
5021 "'%c' option\n"), optopt
);
5025 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5036 if (cb
.cb_version
== 0) {
5037 cb
.cb_version
= SPA_VERSION
;
5038 } else if (!upgradeall
&& argc
== 0) {
5039 (void) fprintf(stderr
, gettext("-V option is "
5040 "incompatible with other arguments\n"));
5045 if (upgradeall
|| argc
!= 0) {
5046 (void) fprintf(stderr
, gettext("-v option is "
5047 "incompatible with other arguments\n"));
5050 } else if (upgradeall
) {
5052 (void) fprintf(stderr
, gettext("-a option should not "
5053 "be used along with a pool name\n"));
5058 (void) printf(gettext("This system supports ZFS pool feature "
5063 (void) printf(gettext("The following features are "
5065 (void) printf(gettext("FEAT DESCRIPTION\n"));
5066 (void) printf("----------------------------------------------"
5067 "---------------\n");
5068 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5069 zfeature_info_t
*fi
= &spa_feature_table
[i
];
5070 const char *ro
= fi
->fi_can_readonly
?
5071 " (read-only compatible)" : "";
5073 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
5074 (void) printf(" %s\n", fi
->fi_desc
);
5076 (void) printf("\n");
5078 (void) printf(gettext("The following legacy versions are also "
5080 (void) printf(gettext("VER DESCRIPTION\n"));
5081 (void) printf("--- -----------------------------------------"
5082 "---------------\n");
5083 (void) printf(gettext(" 1 Initial ZFS version\n"));
5084 (void) printf(gettext(" 2 Ditto blocks "
5085 "(replicated metadata)\n"));
5086 (void) printf(gettext(" 3 Hot spares and double parity "
5088 (void) printf(gettext(" 4 zpool history\n"));
5089 (void) printf(gettext(" 5 Compression using the gzip "
5091 (void) printf(gettext(" 6 bootfs pool property\n"));
5092 (void) printf(gettext(" 7 Separate intent log devices\n"));
5093 (void) printf(gettext(" 8 Delegated administration\n"));
5094 (void) printf(gettext(" 9 refquota and refreservation "
5096 (void) printf(gettext(" 10 Cache devices\n"));
5097 (void) printf(gettext(" 11 Improved scrub performance\n"));
5098 (void) printf(gettext(" 12 Snapshot properties\n"));
5099 (void) printf(gettext(" 13 snapused property\n"));
5100 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5101 (void) printf(gettext(" 15 user/group space accounting\n"));
5102 (void) printf(gettext(" 16 stmf property support\n"));
5103 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5104 (void) printf(gettext(" 18 Snapshot user holds\n"));
5105 (void) printf(gettext(" 19 Log device removal\n"));
5106 (void) printf(gettext(" 20 Compression using zle "
5107 "(zero-length encoding)\n"));
5108 (void) printf(gettext(" 21 Deduplication\n"));
5109 (void) printf(gettext(" 22 Received properties\n"));
5110 (void) printf(gettext(" 23 Slim ZIL\n"));
5111 (void) printf(gettext(" 24 System attributes\n"));
5112 (void) printf(gettext(" 25 Improved scrub stats\n"));
5113 (void) printf(gettext(" 26 Improved snapshot deletion "
5115 (void) printf(gettext(" 27 Improved snapshot creation "
5117 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5118 (void) printf(gettext("\nFor more information on a particular "
5119 "version, including supported releases,\n"));
5120 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5121 } else if (argc
== 0 && upgradeall
) {
5122 cb
.cb_first
= B_TRUE
;
5123 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5124 if (ret
== 0 && cb
.cb_first
) {
5125 if (cb
.cb_version
== SPA_VERSION
) {
5126 (void) printf(gettext("All pools are already "
5127 "formatted using feature flags.\n\n"));
5128 (void) printf(gettext("Every feature flags "
5129 "pool already has all supported features "
5132 (void) printf(gettext("All pools are already "
5133 "formatted with version %llu or higher.\n"),
5134 (u_longlong_t
) cb
.cb_version
);
5137 } else if (argc
== 0) {
5138 cb
.cb_first
= B_TRUE
;
5139 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5143 (void) printf(gettext("All pools are formatted "
5144 "using feature flags.\n\n"));
5146 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5147 "for a list of available legacy versions.\n"));
5150 cb
.cb_first
= B_TRUE
;
5151 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5155 (void) printf(gettext("Every feature flags pool has "
5156 "all supported features enabled.\n"));
5158 (void) printf(gettext("\n"));
5161 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5168 typedef struct hist_cbdata
{
5175 * Print out the command history for a specific pool.
5178 get_history_one(zpool_handle_t
*zhp
, void *data
)
5184 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5186 cb
->first
= B_FALSE
;
5188 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5190 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5193 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5194 &records
, &numrecords
) == 0);
5195 for (i
= 0; i
< numrecords
; i
++) {
5196 nvlist_t
*rec
= records
[i
];
5199 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5203 tsec
= fnvlist_lookup_uint64(records
[i
],
5205 (void) localtime_r(&tsec
, &t
);
5206 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5209 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5210 (void) printf("%s %s", tbuf
,
5211 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5212 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5214 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5217 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5218 (void) printf("%s unrecognized record:\n",
5220 dump_nvlist(rec
, 4);
5223 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5224 zfs_history_event_names
[ievent
],
5225 (longlong_t
) fnvlist_lookup_uint64(
5226 rec
, ZPOOL_HIST_TXG
),
5227 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5228 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5231 (void) printf("%s [txg:%lld] %s", tbuf
,
5232 (longlong_t
) fnvlist_lookup_uint64(
5233 rec
, ZPOOL_HIST_TXG
),
5234 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5235 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5236 (void) printf(" %s (%llu)",
5237 fnvlist_lookup_string(rec
,
5239 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
5242 (void) printf(" %s", fnvlist_lookup_string(rec
,
5243 ZPOOL_HIST_INT_STR
));
5244 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5247 (void) printf("%s ioctl %s\n", tbuf
,
5248 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5249 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5250 (void) printf(" input:\n");
5251 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5252 ZPOOL_HIST_INPUT_NVL
), 8);
5254 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5255 (void) printf(" output:\n");
5256 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5257 ZPOOL_HIST_OUTPUT_NVL
), 8);
5262 (void) printf("%s unrecognized record:\n", tbuf
);
5263 dump_nvlist(rec
, 4);
5267 (void) printf("\n");
5270 (void) printf(" [");
5271 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5272 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5273 struct passwd
*pwd
= getpwuid(who
);
5274 (void) printf("user %d ", (int)who
);
5276 (void) printf("(%s) ", pwd
->pw_name
);
5278 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5279 (void) printf("on %s",
5280 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5282 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5283 (void) printf(":%s",
5284 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5288 (void) printf("\n");
5290 (void) printf("\n");
5297 * zpool history <pool>
5299 * Displays the history of commands that modified pools.
5302 zpool_do_history(int argc
, char **argv
)
5304 hist_cbdata_t cbdata
= { 0 };
5308 cbdata
.first
= B_TRUE
;
5310 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5313 cbdata
.longfmt
= B_TRUE
;
5316 cbdata
.internal
= B_TRUE
;
5319 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5327 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5330 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5331 (void) fprintf(stderr
, gettext("no pools available\n"));
5338 typedef struct ev_opts
{
5346 zpool_do_events_short(nvlist_t
*nvl
)
5348 char ctime_str
[26], str
[32], *ptr
;
5352 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
5353 memset(str
, ' ', 32);
5354 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
5355 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
5356 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
5357 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
5358 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
5359 (void) printf(gettext("%s "), str
);
5361 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
5362 (void) printf(gettext("%s\n"), ptr
);
5366 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
5370 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
5371 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
5373 data_type_t type
= nvpair_type(nvp
);
5374 const char *name
= nvpair_name(nvp
);
5384 printf(gettext("%*s%s = "), depth
, "", name
);
5387 case DATA_TYPE_BOOLEAN
:
5388 printf(gettext("%s"), "1");
5391 case DATA_TYPE_BOOLEAN_VALUE
:
5392 (void) nvpair_value_boolean_value(nvp
, &b
);
5393 printf(gettext("%s"), b
? "1" : "0");
5396 case DATA_TYPE_BYTE
:
5397 (void) nvpair_value_byte(nvp
, &i8
);
5398 printf(gettext("0x%x"), i8
);
5401 case DATA_TYPE_INT8
:
5402 (void) nvpair_value_int8(nvp
, (void *)&i8
);
5403 printf(gettext("0x%x"), i8
);
5406 case DATA_TYPE_UINT8
:
5407 (void) nvpair_value_uint8(nvp
, &i8
);
5408 printf(gettext("0x%x"), i8
);
5411 case DATA_TYPE_INT16
:
5412 (void) nvpair_value_int16(nvp
, (void *)&i16
);
5413 printf(gettext("0x%x"), i16
);
5416 case DATA_TYPE_UINT16
:
5417 (void) nvpair_value_uint16(nvp
, &i16
);
5418 printf(gettext("0x%x"), i16
);
5421 case DATA_TYPE_INT32
:
5422 (void) nvpair_value_int32(nvp
, (void *)&i32
);
5423 printf(gettext("0x%x"), i32
);
5426 case DATA_TYPE_UINT32
:
5427 (void) nvpair_value_uint32(nvp
, &i32
);
5428 printf(gettext("0x%x"), i32
);
5431 case DATA_TYPE_INT64
:
5432 (void) nvpair_value_int64(nvp
, (void *)&i64
);
5433 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5436 case DATA_TYPE_UINT64
:
5437 (void) nvpair_value_uint64(nvp
, &i64
);
5438 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5441 case DATA_TYPE_HRTIME
:
5442 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
5443 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5446 case DATA_TYPE_STRING
:
5447 (void) nvpair_value_string(nvp
, &str
);
5448 printf(gettext("\"%s\""), str
? str
: "<NULL>");
5451 case DATA_TYPE_NVLIST
:
5452 printf(gettext("(embedded nvlist)\n"));
5453 (void) nvpair_value_nvlist(nvp
, &cnv
);
5454 zpool_do_events_nvprint(cnv
, depth
+ 8);
5455 printf(gettext("%*s(end %s)"), depth
, "", name
);
5458 case DATA_TYPE_NVLIST_ARRAY
: {
5462 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
5463 printf(gettext("(%d embedded nvlists)\n"), nelem
);
5464 for (i
= 0; i
< nelem
; i
++) {
5465 printf(gettext("%*s%s[%d] = %s\n"),
5466 depth
, "", name
, i
, "(embedded nvlist)");
5467 zpool_do_events_nvprint(val
[i
], depth
+ 8);
5468 printf(gettext("%*s(end %s[%i])\n"),
5469 depth
, "", name
, i
);
5471 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
5475 case DATA_TYPE_INT8_ARRAY
: {
5479 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
5480 for (i
= 0; i
< nelem
; i
++)
5481 printf(gettext("0x%x "), val
[i
]);
5486 case DATA_TYPE_UINT8_ARRAY
: {
5490 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
5491 for (i
= 0; i
< nelem
; i
++)
5492 printf(gettext("0x%x "), val
[i
]);
5497 case DATA_TYPE_INT16_ARRAY
: {
5501 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
5502 for (i
= 0; i
< nelem
; i
++)
5503 printf(gettext("0x%x "), val
[i
]);
5508 case DATA_TYPE_UINT16_ARRAY
: {
5512 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
5513 for (i
= 0; i
< nelem
; i
++)
5514 printf(gettext("0x%x "), val
[i
]);
5519 case DATA_TYPE_INT32_ARRAY
: {
5523 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
5524 for (i
= 0; i
< nelem
; i
++)
5525 printf(gettext("0x%x "), val
[i
]);
5530 case DATA_TYPE_UINT32_ARRAY
: {
5534 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
5535 for (i
= 0; i
< nelem
; i
++)
5536 printf(gettext("0x%x "), val
[i
]);
5541 case DATA_TYPE_INT64_ARRAY
: {
5545 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
5546 for (i
= 0; i
< nelem
; i
++)
5547 printf(gettext("0x%llx "),
5548 (u_longlong_t
)val
[i
]);
5553 case DATA_TYPE_UINT64_ARRAY
: {
5557 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
5558 for (i
= 0; i
< nelem
; i
++)
5559 printf(gettext("0x%llx "),
5560 (u_longlong_t
)val
[i
]);
5565 case DATA_TYPE_STRING_ARRAY
: {
5569 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
5570 for (i
= 0; i
< nelem
; i
++)
5571 printf(gettext("\"%s\" "),
5572 str
[i
] ? str
[i
] : "<NULL>");
5577 case DATA_TYPE_BOOLEAN_ARRAY
:
5578 case DATA_TYPE_BYTE_ARRAY
:
5579 case DATA_TYPE_DOUBLE
:
5580 case DATA_TYPE_UNKNOWN
:
5581 printf(gettext("<unknown>"));
5585 printf(gettext("\n"));
5590 zpool_do_events_next(ev_opts_t
*opts
)
5593 int zevent_fd
, ret
, dropped
;
5595 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
5596 VERIFY(zevent_fd
>= 0);
5598 if (!opts
->scripted
)
5599 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5602 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
5603 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
5604 if (ret
|| nvl
== NULL
)
5608 (void) printf(gettext("dropped %d events\n"), dropped
);
5610 zpool_do_events_short(nvl
);
5612 if (opts
->verbose
) {
5613 zpool_do_events_nvprint(nvl
, 8);
5614 printf(gettext("\n"));
5616 (void) fflush(stdout
);
5621 VERIFY(0 == close(zevent_fd
));
5627 zpool_do_events_clear(ev_opts_t
*opts
)
5631 ret
= zpool_events_clear(g_zfs
, &count
);
5633 (void) printf(gettext("cleared %d events\n"), count
);
5639 * zpool events [-vfc]
5641 * Displays events logs by ZFS.
5644 zpool_do_events(int argc
, char **argv
)
5646 ev_opts_t opts
= { 0 };
5651 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
5666 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5675 ret
= zpool_do_events_clear(&opts
);
5677 ret
= zpool_do_events_next(&opts
);
5683 get_callback(zpool_handle_t
*zhp
, void *data
)
5685 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5686 char value
[MAXNAMELEN
];
5687 zprop_source_t srctype
;
5690 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5693 * Skip the special fake placeholder. This will also skip
5694 * over the name property when 'all' is specified.
5696 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5697 pl
== cbp
->cb_proplist
)
5700 if (pl
->pl_prop
== ZPROP_INVAL
&&
5701 (zpool_prop_feature(pl
->pl_user_prop
) ||
5702 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5703 srctype
= ZPROP_SRC_LOCAL
;
5705 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5706 value
, sizeof (value
)) == 0) {
5707 zprop_print_one_property(zpool_get_name(zhp
),
5708 cbp
, pl
->pl_user_prop
, value
, srctype
,
5712 if (zpool_get_prop_literal(zhp
, pl
->pl_prop
, value
,
5713 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5716 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5717 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5725 zpool_do_get(int argc
, char **argv
)
5727 zprop_get_cbdata_t cb
= { 0 };
5728 zprop_list_t fake_name
= { 0 };
5732 while ((c
= getopt(argc
, argv
, "pH")) != -1) {
5735 cb
.cb_literal
= B_TRUE
;
5739 cb
.cb_scripted
= B_TRUE
;
5743 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5753 (void) fprintf(stderr
, gettext("missing property "
5758 cb
.cb_first
= B_TRUE
;
5759 cb
.cb_sources
= ZPROP_SRC_ALL
;
5760 cb
.cb_columns
[0] = GET_COL_NAME
;
5761 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5762 cb
.cb_columns
[2] = GET_COL_VALUE
;
5763 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5764 cb
.cb_type
= ZFS_TYPE_POOL
;
5766 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5772 if (cb
.cb_proplist
!= NULL
) {
5773 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5774 fake_name
.pl_width
= strlen(gettext("NAME"));
5775 fake_name
.pl_next
= cb
.cb_proplist
;
5776 cb
.cb_proplist
= &fake_name
;
5779 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5782 if (cb
.cb_proplist
== &fake_name
)
5783 zprop_free_list(fake_name
.pl_next
);
5785 zprop_free_list(cb
.cb_proplist
);
5790 typedef struct set_cbdata
{
5793 boolean_t cb_any_successful
;
5797 set_callback(zpool_handle_t
*zhp
, void *data
)
5800 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5802 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5805 cb
->cb_any_successful
= B_TRUE
;
5811 zpool_do_set(int argc
, char **argv
)
5813 set_cbdata_t cb
= { 0 };
5816 if (argc
> 1 && argv
[1][0] == '-') {
5817 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5823 (void) fprintf(stderr
, gettext("missing property=value "
5829 (void) fprintf(stderr
, gettext("missing pool name\n"));
5834 (void) fprintf(stderr
, gettext("too many pool names\n"));
5838 cb
.cb_propname
= argv
[1];
5839 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5840 if (cb
.cb_value
== NULL
) {
5841 (void) fprintf(stderr
, gettext("missing value in "
5842 "property=value argument\n"));
5846 *(cb
.cb_value
) = '\0';
5849 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5856 find_command_idx(char *command
, int *idx
)
5860 for (i
= 0; i
< NCOMMAND
; i
++) {
5861 if (command_table
[i
].name
== NULL
)
5864 if (strcmp(command
, command_table
[i
].name
) == 0) {
5873 main(int argc
, char **argv
)
5879 (void) setlocale(LC_ALL
, "");
5880 (void) textdomain(TEXT_DOMAIN
);
5885 * Make sure the user has specified some command.
5888 (void) fprintf(stderr
, gettext("missing command\n"));
5897 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
5900 if ((g_zfs
= libzfs_init()) == NULL
)
5903 libzfs_print_on_error(g_zfs
, B_TRUE
);
5905 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5908 * Run the appropriate command.
5910 if (find_command_idx(cmdname
, &i
) == 0) {
5911 current_command
= &command_table
[i
];
5912 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5913 } else if (strchr(cmdname
, '=')) {
5914 verify(find_command_idx("set", &i
) == 0);
5915 current_command
= &command_table
[i
];
5916 ret
= command_table
[i
].func(argc
, argv
);
5917 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5919 * 'freeze' is a vile debugging abomination, so we treat
5923 int fd
= open(ZFS_DEV
, O_RDWR
);
5924 (void) strcpy((void *)buf
, argv
[2]);
5925 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5927 (void) fprintf(stderr
, gettext("unrecognized "
5928 "command '%s'\n"), cmdname
);
5933 if (ret
== 0 && log_history
)
5934 (void) zpool_log_history(g_zfs
, history_str
);
5939 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5940 * for the purposes of running ::findleaks.
5942 if (getenv("ZFS_ABORT") != NULL
) {
5943 (void) printf("dumping core by request\n");