4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
49 #include <sys/fs/zfs.h>
51 #include <sys/fm/util.h>
52 #include <sys/fm/protocol.h>
53 #include <sys/zfs_ioctl.h>
57 #include "zpool_util.h"
58 #include "zfs_comutil.h"
59 #include "zfeature_common.h"
61 #include "statcommon.h"
63 static int zpool_do_create(int, char **);
64 static int zpool_do_destroy(int, char **);
66 static int zpool_do_add(int, char **);
67 static int zpool_do_remove(int, char **);
68 static int zpool_do_labelclear(int, char **);
70 static int zpool_do_list(int, char **);
71 static int zpool_do_iostat(int, char **);
72 static int zpool_do_status(int, char **);
74 static int zpool_do_online(int, char **);
75 static int zpool_do_offline(int, char **);
76 static int zpool_do_clear(int, char **);
77 static int zpool_do_reopen(int, char **);
79 static int zpool_do_reguid(int, char **);
81 static int zpool_do_attach(int, char **);
82 static int zpool_do_detach(int, char **);
83 static int zpool_do_replace(int, char **);
84 static int zpool_do_split(int, char **);
86 static int zpool_do_scrub(int, char **);
88 static int zpool_do_import(int, char **);
89 static int zpool_do_export(int, char **);
91 static int zpool_do_upgrade(int, char **);
93 static int zpool_do_history(int, char **);
94 static int zpool_do_events(int, char **);
96 static int zpool_do_get(int, char **);
97 static int zpool_do_set(int, char **);
100 * These libumem hooks provide a reasonable set of defaults for the allocator's
101 * debugging facilities.
106 _umem_debug_init(void)
108 return ("default,verbose"); /* $UMEM_DEBUG setting */
112 _umem_logging_init(void)
114 return ("fail,contents"); /* $UMEM_LOGGING setting */
147 typedef struct zpool_command
{
149 int (*func
)(int, char **);
154 * Master command table. Each ZFS command has a name, associated function, and
155 * usage message. The usage messages need to be internationalized, so we have
156 * to have a function to return the usage message based on a command index.
158 * These commands are organized according to how they are displayed in the usage
159 * message. An empty command (one with a NULL name) indicates an empty line in
160 * the generic usage message.
162 static zpool_command_t command_table
[] = {
163 { "create", zpool_do_create
, HELP_CREATE
},
164 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
166 { "add", zpool_do_add
, HELP_ADD
},
167 { "remove", zpool_do_remove
, HELP_REMOVE
},
169 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
171 { "list", zpool_do_list
, HELP_LIST
},
172 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
173 { "status", zpool_do_status
, HELP_STATUS
},
175 { "online", zpool_do_online
, HELP_ONLINE
},
176 { "offline", zpool_do_offline
, HELP_OFFLINE
},
177 { "clear", zpool_do_clear
, HELP_CLEAR
},
178 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
180 { "attach", zpool_do_attach
, HELP_ATTACH
},
181 { "detach", zpool_do_detach
, HELP_DETACH
},
182 { "replace", zpool_do_replace
, HELP_REPLACE
},
183 { "split", zpool_do_split
, HELP_SPLIT
},
185 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
187 { "import", zpool_do_import
, HELP_IMPORT
},
188 { "export", zpool_do_export
, HELP_EXPORT
},
189 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
190 { "reguid", zpool_do_reguid
, HELP_REGUID
},
192 { "history", zpool_do_history
, HELP_HISTORY
},
193 { "events", zpool_do_events
, HELP_EVENTS
},
195 { "get", zpool_do_get
, HELP_GET
},
196 { "set", zpool_do_set
, HELP_SET
},
199 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
201 static zpool_command_t
*current_command
;
202 static char history_str
[HIS_MAX_RECORD_LEN
];
203 static boolean_t log_history
= B_TRUE
;
204 static uint_t timestamp_fmt
= NODATE
;
207 get_usage(zpool_help_t idx
) {
210 return (gettext("\tadd [-fgLnP] [-o property=value] "
211 "<pool> <vdev> ...\n"));
213 return (gettext("\tattach [-f] [-o property=value] "
214 "<pool> <device> <new-device>\n"));
216 return (gettext("\tclear [-nF] <pool> [device]\n"));
218 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
219 "\t [-O file-system-property=value] ... \n"
220 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
222 return (gettext("\tdestroy [-f] <pool>\n"));
224 return (gettext("\tdetach <pool> <device>\n"));
226 return (gettext("\texport [-af] <pool> ...\n"));
228 return (gettext("\thistory [-il] [<pool>] ...\n"));
230 return (gettext("\timport [-d dir] [-D]\n"
231 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
232 "\timport [-o mntopts] [-o property=value] ... \n"
233 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
234 "[-R root] [-F [-n]] -a\n"
235 "\timport [-o mntopts] [-o property=value] ... \n"
236 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
237 "[-R root] [-F [-n]]\n"
238 "\t <pool | id> [newpool]\n"));
240 return (gettext("\tiostat [-gLPvy] [-T d|u] [pool] ... "
241 "[interval [count]]\n"));
242 case HELP_LABELCLEAR
:
243 return (gettext("\tlabelclear [-f] <vdev>\n"));
245 return (gettext("\tlist [-gHLPv] [-o property[,...]] "
246 "[-T d|u] [pool] ... [interval [count]]\n"));
248 return (gettext("\toffline [-t] <pool> <device> ...\n"));
250 return (gettext("\tonline <pool> <device> ...\n"));
252 return (gettext("\treplace [-f] [-o property=value] "
253 "<pool> <device> [new-device]\n"));
255 return (gettext("\tremove <pool> <device> ...\n"));
257 return (gettext("\treopen <pool>\n"));
259 return (gettext("\tscrub [-s] <pool> ...\n"));
261 return (gettext("\tstatus [-gLPvxD] [-T d|u] [pool] ... "
262 "[interval [count]]\n"));
264 return (gettext("\tupgrade\n"
266 "\tupgrade [-V version] <-a | pool ...>\n"));
268 return (gettext("\tevents [-vHfc]\n"));
270 return (gettext("\tget [-pH] <\"all\" | property[,...]> "
273 return (gettext("\tset <property=value> <pool> \n"));
275 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
276 "\t [-o property=value] <pool> <newpool> "
277 "[<device> ...]\n"));
279 return (gettext("\treguid <pool>\n"));
288 * Callback routine that will print out a pool property value.
291 print_prop_cb(int prop
, void *cb
)
295 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
297 if (zpool_prop_readonly(prop
))
298 (void) fprintf(fp
, " NO ");
300 (void) fprintf(fp
, " YES ");
302 if (zpool_prop_values(prop
) == NULL
)
303 (void) fprintf(fp
, "-\n");
305 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
311 * Display usage message. If we're inside a command, display only the usage for
312 * that command. Otherwise, iterate over the entire command table and display
313 * a complete usage message.
316 usage(boolean_t requested
)
318 FILE *fp
= requested
? stdout
: stderr
;
320 if (current_command
== NULL
) {
323 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
325 gettext("where 'command' is one of the following:\n\n"));
327 for (i
= 0; i
< NCOMMAND
; i
++) {
328 if (command_table
[i
].name
== NULL
)
329 (void) fprintf(fp
, "\n");
331 (void) fprintf(fp
, "%s",
332 get_usage(command_table
[i
].usage
));
335 (void) fprintf(fp
, gettext("usage:\n"));
336 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
339 if (current_command
!= NULL
&&
340 ((strcmp(current_command
->name
, "set") == 0) ||
341 (strcmp(current_command
->name
, "get") == 0) ||
342 (strcmp(current_command
->name
, "list") == 0))) {
345 gettext("\nthe following properties are supported:\n"));
347 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
348 "PROPERTY", "EDIT", "VALUES");
350 /* Iterate over all properties */
351 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
354 (void) fprintf(fp
, "\t%-15s ", "feature@...");
355 (void) fprintf(fp
, "YES disabled | enabled | active\n");
357 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
358 "appended with a feature name.\nSee zpool-features(5).\n"));
362 * See comments at end of main().
364 if (getenv("ZFS_ABORT") != NULL
) {
365 (void) printf("dumping core by request\n");
369 exit(requested
? 0 : 2);
373 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
374 boolean_t print_logs
, int name_flags
)
381 (void) printf("\t%*s%s\n", indent
, "", name
);
383 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
384 &child
, &children
) != 0)
387 for (c
= 0; c
< children
; c
++) {
388 uint64_t is_log
= B_FALSE
;
390 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
392 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
395 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
396 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
397 B_FALSE
, name_flags
);
403 prop_list_contains_feature(nvlist_t
*proplist
)
406 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
407 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
408 if (zpool_prop_feature(nvpair_name(nvp
)))
415 * Add a property pair (name, string-value) into a property nvlist.
418 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
421 zpool_prop_t prop
= ZPROP_INVAL
;
427 if (*props
== NULL
&&
428 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
429 (void) fprintf(stderr
,
430 gettext("internal error: out of memory\n"));
437 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
439 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
440 !zpool_prop_feature(propname
)) {
441 (void) fprintf(stderr
, gettext("property '%s' is "
442 "not a valid pool property\n"), propname
);
447 * feature@ properties and version should not be specified
450 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
451 nvlist_exists(proplist
, vname
)) ||
452 (prop
== ZPOOL_PROP_VERSION
&&
453 prop_list_contains_feature(proplist
))) {
454 (void) fprintf(stderr
, gettext("'feature@' and "
455 "'version' properties cannot be specified "
461 if (zpool_prop_feature(propname
))
464 normnm
= zpool_prop_to_name(prop
);
466 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
467 normnm
= zfs_prop_to_name(fprop
);
473 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
474 prop
!= ZPOOL_PROP_CACHEFILE
) {
475 (void) fprintf(stderr
, gettext("property '%s' "
476 "specified multiple times\n"), propname
);
480 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
481 (void) fprintf(stderr
, gettext("internal "
482 "error: out of memory\n"));
490 * Set a default property pair (name, string-value) in a property nvlist
493 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
498 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
501 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
505 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
507 * -f Force addition of devices, even if they appear in use
508 * -g Display guid for individual vdev name.
509 * -L Follow links when resolving vdev path name.
510 * -n Do not add the devices, but display the resulting layout if
511 * they were to be added.
512 * -o Set property=value.
513 * -P Display full path for vdev name.
515 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
516 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
520 zpool_do_add(int argc
, char **argv
)
522 boolean_t force
= B_FALSE
;
523 boolean_t dryrun
= B_FALSE
;
531 nvlist_t
*props
= NULL
;
535 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
541 name_flags
|= VDEV_NAME_GUID
;
544 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
550 if ((propval
= strchr(optarg
, '=')) == NULL
) {
551 (void) fprintf(stderr
, gettext("missing "
552 "'=' for -o option\n"));
558 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
559 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
563 name_flags
|= VDEV_NAME_PATH
;
566 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
575 /* get pool name and check number of arguments */
577 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
581 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
590 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
593 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
594 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
600 /* pass off to get_vdev_spec for processing */
601 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
603 if (nvroot
== NULL
) {
609 nvlist_t
*poolnvroot
;
611 uint_t l2children
, c
;
613 boolean_t hadcache
= B_FALSE
;
615 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
618 (void) printf(gettext("would update '%s' to the following "
619 "configuration:\n"), zpool_get_name(zhp
));
621 /* print original main pool and new tree */
622 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
,
624 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
, name_flags
);
626 /* Do the same for the logs */
627 if (num_logs(poolnvroot
) > 0) {
628 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
,
630 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
,
632 } else if (num_logs(nvroot
) > 0) {
633 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
,
637 /* Do the same for the caches */
638 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
639 &l2child
, &l2children
) == 0 && l2children
) {
641 (void) printf(gettext("\tcache\n"));
642 for (c
= 0; c
< l2children
; c
++) {
643 vname
= zpool_vdev_name(g_zfs
, NULL
,
644 l2child
[c
], name_flags
);
645 (void) printf("\t %s\n", vname
);
649 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
650 &l2child
, &l2children
) == 0 && l2children
) {
652 (void) printf(gettext("\tcache\n"));
653 for (c
= 0; c
< l2children
; c
++) {
654 vname
= zpool_vdev_name(g_zfs
, NULL
,
655 l2child
[c
], name_flags
);
656 (void) printf("\t %s\n", vname
);
663 ret
= (zpool_add(zhp
, nvroot
) != 0);
674 * zpool remove <pool> <vdev> ...
676 * Removes the given vdev from the pool. Currently, this supports removing
677 * spares, cache, and log devices from the pool.
680 zpool_do_remove(int argc
, char **argv
)
689 /* get pool name and check number of arguments */
691 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
695 (void) fprintf(stderr
, gettext("missing device\n"));
701 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
704 for (i
= 1; i
< argc
; i
++) {
705 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
713 * zpool labelclear <vdev>
715 * Verifies that the vdev is not active and zeros out the label information
719 zpool_do_labelclear(int argc
, char **argv
)
722 int c
, fd
= -1, ret
= 0;
724 boolean_t inuse
= B_FALSE
;
725 boolean_t force
= B_FALSE
;
728 while ((c
= getopt(argc
, argv
, "f")) != -1) {
734 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
745 (void) fprintf(stderr
, gettext("missing vdev device name\n"));
750 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
751 (void) fprintf(stderr
, gettext("Unable to open %s\n"), vdev
);
756 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0) {
760 (void) fprintf(stderr
,
761 gettext("Unable to determine pool state for %s\n"
762 "Use -f to force the clearing any label data\n"), vdev
);
770 case POOL_STATE_ACTIVE
:
771 case POOL_STATE_SPARE
:
772 case POOL_STATE_L2CACHE
:
773 (void) fprintf(stderr
,
774 gettext("labelclear operation failed.\n"
775 "\tVdev %s is a member (%s), of pool \"%s\".\n"
776 "\tTo remove label information from this device, "
777 "export or destroy\n\tthe pool, or remove %s from "
778 "the configuration of this pool\n\tand retry the "
779 "labelclear operation.\n"),
780 vdev
, zpool_pool_state_to_name(state
), name
, vdev
);
784 case POOL_STATE_EXPORTED
:
788 (void) fprintf(stderr
,
789 gettext("labelclear operation failed.\n\tVdev "
790 "%s is a member of the exported pool \"%s\".\n"
791 "\tUse \"zpool labelclear -f %s\" to force the "
792 "removal of label\n\tinformation.\n"),
797 case POOL_STATE_POTENTIALLY_ACTIVE
:
801 (void) fprintf(stderr
,
802 gettext("labelclear operation failed.\n"
803 "\tVdev %s is a member of the pool \"%s\".\n"
804 "\tThis pool is unknown to this system, but may "
805 "be active on\n\tanother system. Use "
806 "\'zpool labelclear -f %s\' to force the\n"
807 "\tremoval of label information.\n"),
812 case POOL_STATE_DESTROYED
:
813 /* inuse should never be set for a destroyed pool... */
819 if (zpool_clear_label(fd
) != 0) {
820 (void) fprintf(stderr
,
821 gettext("Label clear failed on vdev %s\n"), vdev
);
834 * zpool create [-fnd] [-o property=value] ...
835 * [-O file-system-property=value] ...
836 * [-R root] [-m mountpoint] <pool> <dev> ...
838 * -f Force creation, even if devices appear in use
839 * -n Do not create the pool, but display the resulting layout if it
840 * were to be created.
841 * -R Create a pool under an alternate root
842 * -m Set default mountpoint for the root dataset. By default it's
844 * -o Set property=value.
845 * -d Don't automatically enable all supported pool features
846 * (individual features can be enabled with -o).
847 * -O Set fsproperty=value in the pool's root file system
849 * Creates the named pool according to the given vdev specification. The
850 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
851 * we get the nvlist back from get_vdev_spec(), we either print out the contents
852 * (if '-n' was specified), or pass it to libzfs to do the creation.
855 zpool_do_create(int argc
, char **argv
)
857 boolean_t force
= B_FALSE
;
858 boolean_t dryrun
= B_FALSE
;
859 boolean_t enable_all_pool_feat
= B_TRUE
;
861 nvlist_t
*nvroot
= NULL
;
865 char *altroot
= NULL
;
866 char *mountpoint
= NULL
;
867 nvlist_t
*fsprops
= NULL
;
868 nvlist_t
*props
= NULL
;
872 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
881 enable_all_pool_feat
= B_FALSE
;
885 if (add_prop_list(zpool_prop_to_name(
886 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
888 if (add_prop_list_default(zpool_prop_to_name(
889 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
893 /* Equivalent to -O mountpoint=optarg */
897 if ((propval
= strchr(optarg
, '=')) == NULL
) {
898 (void) fprintf(stderr
, gettext("missing "
899 "'=' for -o option\n"));
905 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
909 * If the user is creating a pool that doesn't support
910 * feature flags, don't enable any features.
912 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
916 ver
= strtoull(propval
, &end
, 10);
918 ver
< SPA_VERSION_FEATURES
) {
919 enable_all_pool_feat
= B_FALSE
;
924 if ((propval
= strchr(optarg
, '=')) == NULL
) {
925 (void) fprintf(stderr
, gettext("missing "
926 "'=' for -O option\n"));
933 * Mountpoints are checked and then added later.
934 * Uniquely among properties, they can be specified
935 * more than once, to avoid conflict with -m.
937 if (0 == strcmp(optarg
,
938 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
939 mountpoint
= propval
;
940 } else if (add_prop_list(optarg
, propval
, &fsprops
,
947 * Sanity check temporary pool name.
949 if (strchr(optarg
, '/') != NULL
) {
950 (void) fprintf(stderr
, gettext("cannot create "
951 "'%s': invalid character '/' in temporary "
953 (void) fprintf(stderr
, gettext("use 'zfs "
954 "create' to create a dataset\n"));
958 if (add_prop_list(zpool_prop_to_name(
959 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
961 if (add_prop_list_default(zpool_prop_to_name(
962 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
967 (void) fprintf(stderr
, gettext("missing argument for "
968 "'%c' option\n"), optopt
);
971 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
980 /* get pool name and check number of arguments */
982 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
986 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
993 * As a special case, check for use of '/' in the name, and direct the
994 * user to use 'zfs create' instead.
996 if (strchr(poolname
, '/') != NULL
) {
997 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
998 "character '/' in pool name\n"), poolname
);
999 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1000 "create a dataset\n"));
1004 /* pass off to get_vdev_spec for bulk processing */
1005 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1006 argc
- 1, argv
+ 1);
1010 /* make_root_vdev() allows 0 toplevel children if there are spares */
1011 if (!zfs_allocatable_devs(nvroot
)) {
1012 (void) fprintf(stderr
, gettext("invalid vdev "
1013 "specification: at least one toplevel vdev must be "
1018 if (altroot
!= NULL
&& altroot
[0] != '/') {
1019 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1020 "must be an absolute path\n"), altroot
);
1025 * Check the validity of the mountpoint and direct the user to use the
1026 * '-m' mountpoint option if it looks like its in use.
1028 if (mountpoint
== NULL
||
1029 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1030 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1031 char buf
[MAXPATHLEN
];
1034 if (mountpoint
&& mountpoint
[0] != '/') {
1035 (void) fprintf(stderr
, gettext("invalid mountpoint "
1036 "'%s': must be an absolute path, 'legacy', or "
1037 "'none'\n"), mountpoint
);
1041 if (mountpoint
== NULL
) {
1042 if (altroot
!= NULL
)
1043 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1046 (void) snprintf(buf
, sizeof (buf
), "/%s",
1049 if (altroot
!= NULL
)
1050 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1051 altroot
, mountpoint
);
1053 (void) snprintf(buf
, sizeof (buf
), "%s",
1057 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1058 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1059 "%s\n"), buf
, strerror(errno
));
1060 (void) fprintf(stderr
, gettext("use '-m' "
1061 "option to provide a different default\n"));
1066 while (count
< 3 && readdir(dirp
) != NULL
)
1068 (void) closedir(dirp
);
1071 (void) fprintf(stderr
, gettext("mountpoint "
1072 "'%s' exists and is not empty\n"), buf
);
1073 (void) fprintf(stderr
, gettext("use '-m' "
1074 "option to provide a "
1075 "different default\n"));
1082 * Now that the mountpoint's validity has been checked, ensure that
1083 * the property is set appropriately prior to creating the pool.
1085 if (mountpoint
!= NULL
) {
1086 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1087 mountpoint
, &fsprops
, B_FALSE
);
1095 * For a dry run invocation, print out a basic message and run
1096 * through all the vdevs in the list and print out in an
1097 * appropriate hierarchy.
1099 (void) printf(gettext("would create '%s' with the "
1100 "following layout:\n\n"), poolname
);
1102 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1103 if (num_logs(nvroot
) > 0)
1104 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1109 * Hand off to libzfs.
1111 if (enable_all_pool_feat
) {
1113 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1114 char propname
[MAXPATHLEN
];
1115 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1117 (void) snprintf(propname
, sizeof (propname
),
1118 "feature@%s", feat
->fi_uname
);
1121 * Skip feature if user specified it manually
1122 * on the command line.
1124 if (nvlist_exists(props
, propname
))
1127 ret
= add_prop_list(propname
,
1128 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1135 if (zpool_create(g_zfs
, poolname
,
1136 nvroot
, props
, fsprops
) == 0) {
1137 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1138 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1140 if (zfs_mount(pool
, NULL
, 0) == 0)
1141 ret
= zfs_shareall(pool
);
1144 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1145 (void) fprintf(stderr
, gettext("pool name may have "
1151 nvlist_free(nvroot
);
1152 nvlist_free(fsprops
);
1156 nvlist_free(fsprops
);
1163 * zpool destroy <pool>
1165 * -f Forcefully unmount any datasets
1167 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1170 zpool_do_destroy(int argc
, char **argv
)
1172 boolean_t force
= B_FALSE
;
1175 zpool_handle_t
*zhp
;
1179 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1185 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1194 /* check arguments */
1196 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1200 (void) fprintf(stderr
, gettext("too many arguments\n"));
1206 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1208 * As a special case, check for use of '/' in the name, and
1209 * direct the user to use 'zfs destroy' instead.
1211 if (strchr(pool
, '/') != NULL
)
1212 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1213 "destroy a dataset\n"));
1217 if (zpool_disable_datasets(zhp
, force
) != 0) {
1218 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1219 "could not unmount datasets\n"), zpool_get_name(zhp
));
1223 /* The history must be logged as part of the export */
1224 log_history
= B_FALSE
;
1226 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1233 typedef struct export_cbdata
{
1235 boolean_t hardforce
;
1242 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1244 export_cbdata_t
*cb
= data
;
1246 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1249 /* The history must be logged as part of the export */
1250 log_history
= B_FALSE
;
1252 if (cb
->hardforce
) {
1253 if (zpool_export_force(zhp
, history_str
) != 0)
1255 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1263 * zpool export [-f] <pool> ...
1265 * -a Export all pools
1266 * -f Forcefully unmount datasets
1268 * Export the given pools. By default, the command will attempt to cleanly
1269 * unmount any active datasets within the pool. If the '-f' flag is specified,
1270 * then the datasets will be forcefully unmounted.
1273 zpool_do_export(int argc
, char **argv
)
1276 boolean_t do_all
= B_FALSE
;
1277 boolean_t force
= B_FALSE
;
1278 boolean_t hardforce
= B_FALSE
;
1282 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1294 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1301 cb
.hardforce
= hardforce
;
1307 (void) fprintf(stderr
, gettext("too many arguments\n"));
1311 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1312 zpool_export_one
, &cb
));
1315 /* check arguments */
1317 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1321 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1327 * Given a vdev configuration, determine the maximum width needed for the device
1331 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1339 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
| VDEV_NAME_TYPE_ID
);
1340 if (strlen(name
) + depth
> max
)
1341 max
= strlen(name
) + depth
;
1345 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1346 &child
, &children
) == 0) {
1347 for (c
= 0; c
< children
; c
++)
1348 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1349 max
, name_flags
)) > max
)
1353 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1354 &child
, &children
) == 0) {
1355 for (c
= 0; c
< children
; c
++)
1356 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1357 max
, name_flags
)) > max
)
1361 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1362 &child
, &children
) == 0) {
1363 for (c
= 0; c
< children
; c
++)
1364 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1365 max
, name_flags
)) > max
)
1372 typedef struct spare_cbdata
{
1374 zpool_handle_t
*cb_zhp
;
1378 find_vdev(nvlist_t
*nv
, uint64_t search
)
1384 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1388 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1389 &child
, &children
) == 0) {
1390 for (c
= 0; c
< children
; c
++)
1391 if (find_vdev(child
[c
], search
))
1399 find_spare(zpool_handle_t
*zhp
, void *data
)
1401 spare_cbdata_t
*cbp
= data
;
1402 nvlist_t
*config
, *nvroot
;
1404 config
= zpool_get_config(zhp
, NULL
);
1405 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1408 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1418 * Print out configuration state as requested by status_callback.
1421 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1422 int namewidth
, int depth
, boolean_t isspare
, int name_flags
)
1426 pool_scan_stat_t
*ps
= NULL
;
1428 char rbuf
[6], wbuf
[6], cbuf
[6];
1430 uint64_t notpresent
;
1434 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1435 &child
, &children
) != 0)
1438 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1439 (uint64_t **)&vs
, &c
) == 0);
1441 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1444 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1447 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1449 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1453 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1457 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1458 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1459 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1460 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1463 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1464 ¬present
) == 0) {
1466 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1467 (void) printf(" was %s", path
);
1468 } else if (vs
->vs_aux
!= 0) {
1471 switch (vs
->vs_aux
) {
1472 case VDEV_AUX_OPEN_FAILED
:
1473 (void) printf(gettext("cannot open"));
1476 case VDEV_AUX_BAD_GUID_SUM
:
1477 (void) printf(gettext("missing device"));
1480 case VDEV_AUX_NO_REPLICAS
:
1481 (void) printf(gettext("insufficient replicas"));
1484 case VDEV_AUX_VERSION_NEWER
:
1485 (void) printf(gettext("newer version"));
1488 case VDEV_AUX_UNSUP_FEAT
:
1489 (void) printf(gettext("unsupported feature(s)"));
1492 case VDEV_AUX_SPARED
:
1493 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1495 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1496 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1497 zpool_get_name(zhp
)) == 0)
1498 (void) printf(gettext("currently in "
1501 (void) printf(gettext("in use by "
1503 zpool_get_name(cb
.cb_zhp
));
1504 zpool_close(cb
.cb_zhp
);
1506 (void) printf(gettext("currently in use"));
1510 case VDEV_AUX_ERR_EXCEEDED
:
1511 (void) printf(gettext("too many errors"));
1514 case VDEV_AUX_IO_FAILURE
:
1515 (void) printf(gettext("experienced I/O failures"));
1518 case VDEV_AUX_BAD_LOG
:
1519 (void) printf(gettext("bad intent log"));
1522 case VDEV_AUX_EXTERNAL
:
1523 (void) printf(gettext("external device fault"));
1526 case VDEV_AUX_SPLIT_POOL
:
1527 (void) printf(gettext("split into new pool"));
1531 (void) printf(gettext("corrupted data"));
1536 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1537 (uint64_t **)&ps
, &c
);
1539 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1540 vs
->vs_scan_processed
!= 0 && children
== 0) {
1541 (void) printf(gettext(" (%s)"),
1542 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1543 "resilvering" : "repairing");
1546 (void) printf("\n");
1548 for (c
= 0; c
< children
; c
++) {
1549 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1551 /* Don't print logs or holes here */
1552 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1554 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1556 if (islog
|| ishole
)
1558 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1559 name_flags
| VDEV_NAME_TYPE_ID
);
1560 print_status_config(zhp
, vname
, child
[c
],
1561 namewidth
, depth
+ 2, isspare
, name_flags
);
1567 * Print the configuration of an exported pool. Iterate over all vdevs in the
1568 * pool, printing out the name and status for each one.
1571 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
,
1579 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1580 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1581 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1584 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1585 (uint64_t **)&vs
, &c
) == 0);
1587 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1588 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1590 if (vs
->vs_aux
!= 0) {
1593 switch (vs
->vs_aux
) {
1594 case VDEV_AUX_OPEN_FAILED
:
1595 (void) printf(gettext("cannot open"));
1598 case VDEV_AUX_BAD_GUID_SUM
:
1599 (void) printf(gettext("missing device"));
1602 case VDEV_AUX_NO_REPLICAS
:
1603 (void) printf(gettext("insufficient replicas"));
1606 case VDEV_AUX_VERSION_NEWER
:
1607 (void) printf(gettext("newer version"));
1610 case VDEV_AUX_UNSUP_FEAT
:
1611 (void) printf(gettext("unsupported feature(s)"));
1614 case VDEV_AUX_ERR_EXCEEDED
:
1615 (void) printf(gettext("too many errors"));
1619 (void) printf(gettext("corrupted data"));
1623 (void) printf("\n");
1625 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1626 &child
, &children
) != 0)
1629 for (c
= 0; c
< children
; c
++) {
1630 uint64_t is_log
= B_FALSE
;
1632 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1637 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1638 name_flags
| VDEV_NAME_TYPE_ID
);
1639 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2,
1644 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1645 &child
, &children
) == 0) {
1646 (void) printf(gettext("\tcache\n"));
1647 for (c
= 0; c
< children
; c
++) {
1648 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1650 (void) printf("\t %s\n", vname
);
1655 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1656 &child
, &children
) == 0) {
1657 (void) printf(gettext("\tspares\n"));
1658 for (c
= 0; c
< children
; c
++) {
1659 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1661 (void) printf("\t %s\n", vname
);
1669 * Logs are recorded as top level vdevs in the main pool child array
1670 * but with "is_log" set to 1. We use either print_status_config() or
1671 * print_import_config() to print the top level logs then any log
1672 * children (eg mirrored slogs) are printed recursively - which
1673 * works because only the top level vdev is marked "is_log"
1676 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
,
1682 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1686 (void) printf(gettext("\tlogs\n"));
1688 for (c
= 0; c
< children
; c
++) {
1689 uint64_t is_log
= B_FALSE
;
1692 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1696 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1697 name_flags
| VDEV_NAME_TYPE_ID
);
1699 print_status_config(zhp
, name
, child
[c
], namewidth
,
1700 2, B_FALSE
, name_flags
);
1702 print_import_config(name
, child
[c
], namewidth
, 2,
1709 * Display the status for the given pool.
1712 show_import(nvlist_t
*config
)
1714 uint64_t pool_state
;
1720 zpool_status_t reason
;
1721 zpool_errata_t errata
;
1727 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1729 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1731 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1733 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1736 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1737 (uint64_t **)&vs
, &vsc
) == 0);
1738 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1740 reason
= zpool_import_status(config
, &msgid
, &errata
);
1742 (void) printf(gettext(" pool: %s\n"), name
);
1743 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1744 (void) printf(gettext(" state: %s"), health
);
1745 if (pool_state
== POOL_STATE_DESTROYED
)
1746 (void) printf(gettext(" (DESTROYED)"));
1747 (void) printf("\n");
1750 case ZPOOL_STATUS_MISSING_DEV_R
:
1751 case ZPOOL_STATUS_MISSING_DEV_NR
:
1752 case ZPOOL_STATUS_BAD_GUID_SUM
:
1753 (void) printf(gettext(" status: One or more devices are "
1754 "missing from the system.\n"));
1757 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1758 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1759 (void) printf(gettext(" status: One or more devices contains "
1760 "corrupted data.\n"));
1763 case ZPOOL_STATUS_CORRUPT_DATA
:
1765 gettext(" status: The pool data is corrupted.\n"));
1768 case ZPOOL_STATUS_OFFLINE_DEV
:
1769 (void) printf(gettext(" status: One or more devices "
1770 "are offlined.\n"));
1773 case ZPOOL_STATUS_CORRUPT_POOL
:
1774 (void) printf(gettext(" status: The pool metadata is "
1778 case ZPOOL_STATUS_VERSION_OLDER
:
1779 (void) printf(gettext(" status: The pool is formatted using a "
1780 "legacy on-disk version.\n"));
1783 case ZPOOL_STATUS_VERSION_NEWER
:
1784 (void) printf(gettext(" status: The pool is formatted using an "
1785 "incompatible version.\n"));
1788 case ZPOOL_STATUS_FEAT_DISABLED
:
1789 (void) printf(gettext(" status: Some supported features are "
1790 "not enabled on the pool.\n"));
1793 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1794 (void) printf(gettext("status: The pool uses the following "
1795 "feature(s) not supported on this sytem:\n"));
1796 zpool_print_unsup_feat(config
);
1799 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1800 (void) printf(gettext("status: The pool can only be accessed "
1801 "in read-only mode on this system. It\n\tcannot be "
1802 "accessed in read-write mode because it uses the "
1803 "following\n\tfeature(s) not supported on this system:\n"));
1804 zpool_print_unsup_feat(config
);
1807 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1808 (void) printf(gettext(" status: The pool was last accessed by "
1809 "another system.\n"));
1812 case ZPOOL_STATUS_FAULTED_DEV_R
:
1813 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1814 (void) printf(gettext(" status: One or more devices are "
1818 case ZPOOL_STATUS_BAD_LOG
:
1819 (void) printf(gettext(" status: An intent log record cannot be "
1823 case ZPOOL_STATUS_RESILVERING
:
1824 (void) printf(gettext(" status: One or more devices were being "
1828 case ZPOOL_STATUS_ERRATA
:
1829 (void) printf(gettext(" status: Errata #%d detected.\n"),
1835 * No other status can be seen when importing pools.
1837 assert(reason
== ZPOOL_STATUS_OK
);
1841 * Print out an action according to the overall state of the pool.
1843 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1844 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1845 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1846 (void) printf(gettext(" action: The pool can be "
1847 "imported using its name or numeric identifier, "
1848 "though\n\tsome features will not be available "
1849 "without an explicit 'zpool upgrade'.\n"));
1850 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1851 (void) printf(gettext(" action: The pool can be "
1852 "imported using its name or numeric "
1853 "identifier and\n\tthe '-f' flag.\n"));
1854 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
1856 case ZPOOL_ERRATA_NONE
:
1859 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
1860 (void) printf(gettext(" action: The pool can "
1861 "be imported using its name or numeric "
1862 "identifier,\n\thowever there is a compat"
1863 "ibility issue which should be corrected"
1864 "\n\tby running 'zpool scrub'\n"));
1867 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
1868 (void) printf(gettext(" action: The pool can"
1869 "not be imported with this version of ZFS "
1870 "due to\n\tan active asynchronous destroy. "
1871 "Revert to an earlier version\n\tand "
1872 "allow the destroy to complete before "
1878 * All errata must contain an action message.
1883 (void) printf(gettext(" action: The pool can be "
1884 "imported using its name or numeric "
1887 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1888 (void) printf(gettext(" action: The pool can be imported "
1889 "despite missing or damaged devices. The\n\tfault "
1890 "tolerance of the pool may be compromised if imported.\n"));
1893 case ZPOOL_STATUS_VERSION_NEWER
:
1894 (void) printf(gettext(" action: The pool cannot be "
1895 "imported. Access the pool on a system running "
1896 "newer\n\tsoftware, or recreate the pool from "
1899 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1900 (void) printf(gettext("action: The pool cannot be "
1901 "imported. Access the pool on a system that "
1902 "supports\n\tthe required feature(s), or recreate "
1903 "the pool from backup.\n"));
1905 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1906 (void) printf(gettext("action: The pool cannot be "
1907 "imported in read-write mode. Import the pool "
1909 "\t\"-o readonly=on\", access the pool on a system "
1910 "that supports the\n\trequired feature(s), or "
1911 "recreate the pool from backup.\n"));
1913 case ZPOOL_STATUS_MISSING_DEV_R
:
1914 case ZPOOL_STATUS_MISSING_DEV_NR
:
1915 case ZPOOL_STATUS_BAD_GUID_SUM
:
1916 (void) printf(gettext(" action: The pool cannot be "
1917 "imported. Attach the missing\n\tdevices and try "
1921 (void) printf(gettext(" action: The pool cannot be "
1922 "imported due to damaged devices or data.\n"));
1926 /* Print the comment attached to the pool. */
1927 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1928 (void) printf(gettext("comment: %s\n"), comment
);
1931 * If the state is "closed" or "can't open", and the aux state
1932 * is "corrupt data":
1934 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1935 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1936 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1937 if (pool_state
== POOL_STATE_DESTROYED
)
1938 (void) printf(gettext("\tThe pool was destroyed, "
1939 "but can be imported using the '-Df' flags.\n"));
1940 else if (pool_state
!= POOL_STATE_EXPORTED
)
1941 (void) printf(gettext("\tThe pool may be active on "
1942 "another system, but can be imported using\n\t"
1943 "the '-f' flag.\n"));
1947 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1950 (void) printf(gettext(" config:\n\n"));
1952 namewidth
= max_width(NULL
, nvroot
, 0, 0, 0);
1956 print_import_config(name
, nvroot
, namewidth
, 0, 0);
1957 if (num_logs(nvroot
) > 0)
1958 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
, 0);
1960 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1961 (void) printf(gettext("\n\tAdditional devices are known to "
1962 "be part of this pool, though their\n\texact "
1963 "configuration cannot be determined.\n"));
1968 * Perform the import for the given configuration. This passes the heavy
1969 * lifting off to zpool_import_props(), and then mounts the datasets contained
1973 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1974 nvlist_t
*props
, int flags
)
1976 zpool_handle_t
*zhp
;
1981 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1984 verify(nvlist_lookup_uint64(config
,
1985 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1986 verify(nvlist_lookup_uint64(config
,
1987 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1988 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1989 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1990 "is formatted using an unsupported ZFS version\n"), name
);
1992 } else if (state
!= POOL_STATE_EXPORTED
&&
1993 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1994 uint64_t hostid
= 0;
1995 unsigned long system_hostid
= get_system_hostid();
1997 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
2000 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
) {
2005 verify(nvlist_lookup_string(config
,
2006 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2007 verify(nvlist_lookup_uint64(config
,
2008 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
2010 (void) fprintf(stderr
, gettext("cannot import "
2011 "'%s': pool may be in use from other "
2012 "system, it was last accessed by %s "
2013 "(hostid: 0x%lx) on %s"), name
, hostname
,
2014 (unsigned long)hostid
,
2015 asctime(localtime(&t
)));
2016 (void) fprintf(stderr
, gettext("use '-f' to "
2017 "import anyway\n"));
2022 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2025 if (newname
!= NULL
)
2026 name
= (char *)newname
;
2028 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2031 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2032 !(flags
& ZFS_IMPORT_ONLY
) &&
2033 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2043 * zpool import [-d dir] [-D]
2044 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2045 * [-d dir | -c cachefile] [-f] -a
2046 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2047 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2049 * -c Read pool information from a cachefile instead of searching
2052 * -d Scan in a specific directory, other than /dev/. More than
2053 * one directory can be specified using multiple '-d' options.
2055 * -D Scan for previously destroyed pools or import all or only
2056 * specified destroyed pools.
2058 * -R Temporarily import the pool, with all mountpoints relative to
2059 * the given root. The pool will remain exported when the machine
2062 * -V Import even in the presence of faulted vdevs. This is an
2063 * intentionally undocumented option for testing purposes, and
2064 * treats the pool configuration as complete, leaving any bad
2065 * vdevs in the FAULTED state. In other words, it does verbatim
2068 * -f Force import, even if it appears that the pool is active.
2070 * -F Attempt rewind if necessary.
2072 * -n See if rewind would work, but don't actually rewind.
2074 * -N Import the pool but don't mount datasets.
2076 * -T Specify a starting txg to use for import. This option is
2077 * intentionally undocumented option for testing purposes.
2079 * -a Import all pools found.
2081 * -o Set property=value and/or temporary mount options (without '=').
2083 * -s Scan using the default search path, the libblkid cache will
2086 * The import command scans for pools to import, and import pools based on pool
2087 * name and GUID. The pool can also be renamed as part of the import process.
2090 zpool_do_import(int argc
, char **argv
)
2092 char **searchdirs
= NULL
;
2093 char *env
, *envdup
= NULL
;
2097 nvlist_t
*pools
= NULL
;
2098 boolean_t do_all
= B_FALSE
;
2099 boolean_t do_destroyed
= B_FALSE
;
2100 char *mntopts
= NULL
;
2103 uint64_t searchguid
= 0;
2104 char *searchname
= NULL
;
2106 nvlist_t
*found_config
;
2107 nvlist_t
*policy
= NULL
;
2108 nvlist_t
*props
= NULL
;
2110 int flags
= ZFS_IMPORT_NORMAL
;
2111 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2112 boolean_t dryrun
= B_FALSE
;
2113 boolean_t do_rewind
= B_FALSE
;
2114 boolean_t xtreme_rewind
= B_FALSE
;
2115 boolean_t do_scan
= B_FALSE
;
2116 uint64_t pool_state
, txg
= -1ULL;
2117 char *cachefile
= NULL
;
2118 importargs_t idata
= { 0 };
2122 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2131 if (searchdirs
== NULL
) {
2132 searchdirs
= safe_malloc(sizeof (char *));
2134 char **tmp
= safe_malloc((nsearch
+ 1) *
2136 bcopy(searchdirs
, tmp
, nsearch
*
2141 searchdirs
[nsearch
++] = optarg
;
2144 do_destroyed
= B_TRUE
;
2147 flags
|= ZFS_IMPORT_ANY_HOST
;
2153 flags
|= ZFS_IMPORT_MISSING_LOG
;
2159 flags
|= ZFS_IMPORT_ONLY
;
2162 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2165 if (add_prop_list(optarg
, propval
,
2173 if (add_prop_list(zpool_prop_to_name(
2174 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2176 if (add_prop_list_default(zpool_prop_to_name(
2177 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2184 flags
|= ZFS_IMPORT_TEMP_NAME
;
2185 if (add_prop_list_default(zpool_prop_to_name(
2186 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2192 txg
= strtoull(optarg
, &endptr
, 0);
2193 if (errno
!= 0 || *endptr
!= '\0') {
2194 (void) fprintf(stderr
,
2195 gettext("invalid txg value\n"));
2198 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2201 flags
|= ZFS_IMPORT_VERBATIM
;
2204 xtreme_rewind
= B_TRUE
;
2207 (void) fprintf(stderr
, gettext("missing argument for "
2208 "'%c' option\n"), optopt
);
2212 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2221 if (cachefile
&& nsearch
!= 0) {
2222 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2226 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2227 (void) fprintf(stderr
,
2228 gettext("-n or -X only meaningful with -F\n"));
2232 rewind_policy
= ZPOOL_TRY_REWIND
;
2234 rewind_policy
= ZPOOL_DO_REWIND
;
2236 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2238 /* In the future, we can capture further policy and include it here */
2239 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2240 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2241 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2244 /* check argument count */
2247 (void) fprintf(stderr
, gettext("too many arguments\n"));
2252 (void) fprintf(stderr
, gettext("too many arguments\n"));
2257 * Check for the SYS_CONFIG privilege. We do this explicitly
2258 * here because otherwise any attempt to discover pools will
2261 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
2262 (void) fprintf(stderr
, gettext("cannot "
2263 "discover pools: permission denied\n"));
2264 if (searchdirs
!= NULL
)
2267 nvlist_free(policy
);
2273 * Depending on the arguments given, we do one of the following:
2275 * <none> Iterate through all pools and display information about
2278 * -a Iterate through all pools and try to import each one.
2280 * <id> Find the pool that corresponds to the given GUID/pool
2281 * name and import that one.
2283 * -D Above options applies only to destroyed pools.
2289 searchguid
= strtoull(argv
[0], &endptr
, 10);
2290 if (errno
!= 0 || *endptr
!= '\0') {
2291 searchname
= argv
[0];
2294 found_config
= NULL
;
2297 * User specified a name or guid. Ensure it's unique.
2299 idata
.unique
= B_TRUE
;
2303 * Check the environment for the preferred search path.
2305 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2308 envdup
= strdup(env
);
2310 dir
= strtok(envdup
, ":");
2311 while (dir
!= NULL
) {
2312 if (searchdirs
== NULL
) {
2313 searchdirs
= safe_malloc(sizeof (char *));
2315 char **tmp
= safe_malloc((nsearch
+ 1) *
2317 bcopy(searchdirs
, tmp
, nsearch
*
2322 searchdirs
[nsearch
++] = dir
;
2323 dir
= strtok(NULL
, ":");
2327 idata
.path
= searchdirs
;
2328 idata
.paths
= nsearch
;
2329 idata
.poolname
= searchname
;
2330 idata
.guid
= searchguid
;
2331 idata
.cachefile
= cachefile
;
2332 idata
.scan
= do_scan
;
2335 * Under Linux the zpool_find_import_impl() function leverages the
2336 * taskq implementation to parallelize device scanning. It is
2337 * therefore necessary to initialize this functionality for the
2338 * duration of the zpool_search_import() function.
2341 pools
= zpool_search_import(g_zfs
, &idata
);
2344 if (pools
!= NULL
&& idata
.exists
&&
2345 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2346 (void) fprintf(stderr
, gettext("cannot import '%s': "
2347 "a pool with that name already exists\n"),
2349 (void) fprintf(stderr
, gettext("use the form '%s "
2350 "<pool | id> <newpool>' to give it a new name\n"),
2353 } else if (pools
== NULL
&& idata
.exists
) {
2354 (void) fprintf(stderr
, gettext("cannot import '%s': "
2355 "a pool with that name is already created/imported,\n"),
2357 (void) fprintf(stderr
, gettext("and no additional pools "
2358 "with that name were found\n"));
2360 } else if (pools
== NULL
) {
2362 (void) fprintf(stderr
, gettext("cannot import '%s': "
2363 "no such pool available\n"), argv
[0]);
2369 if (searchdirs
!= NULL
)
2373 nvlist_free(policy
);
2378 * At this point we have a list of import candidate configs. Even if
2379 * we were searching by pool name or guid, we still need to
2380 * post-process the list to deal with pool state and possible
2386 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2388 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2390 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2392 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2394 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2397 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2404 (void) printf("\n");
2407 err
|= do_import(config
, NULL
, mntopts
,
2410 show_import(config
);
2412 } else if (searchname
!= NULL
) {
2416 * We are searching for a pool based on name.
2418 verify(nvlist_lookup_string(config
,
2419 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2421 if (strcmp(name
, searchname
) == 0) {
2422 if (found_config
!= NULL
) {
2423 (void) fprintf(stderr
, gettext(
2424 "cannot import '%s': more than "
2425 "one matching pool\n"), searchname
);
2426 (void) fprintf(stderr
, gettext(
2427 "import by numeric ID instead\n"));
2430 found_config
= config
;
2436 * Search for a pool by guid.
2438 verify(nvlist_lookup_uint64(config
,
2439 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2441 if (guid
== searchguid
)
2442 found_config
= config
;
2447 * If we were searching for a specific pool, verify that we found a
2448 * pool, and then do the import.
2450 if (argc
!= 0 && err
== 0) {
2451 if (found_config
== NULL
) {
2452 (void) fprintf(stderr
, gettext("cannot import '%s': "
2453 "no such pool available\n"), argv
[0]);
2456 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2457 argv
[1], mntopts
, props
, flags
);
2462 * If we were just looking for pools, report an error if none were
2465 if (argc
== 0 && first
)
2466 (void) fprintf(stderr
,
2467 gettext("no pools available to import\n"));
2472 nvlist_free(policy
);
2473 if (searchdirs
!= NULL
)
2478 return (err
? 1 : 0);
2481 typedef struct iostat_cbdata
{
2482 boolean_t cb_verbose
;
2486 zpool_list_t
*cb_list
;
2490 print_iostat_separator(iostat_cbdata_t
*cb
)
2494 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2496 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2500 print_iostat_header(iostat_cbdata_t
*cb
)
2502 (void) printf("%*s capacity operations bandwidth\n",
2503 cb
->cb_namewidth
, "");
2504 (void) printf("%-*s alloc free read write read write\n",
2505 cb
->cb_namewidth
, "pool");
2506 print_iostat_separator(cb
);
2510 * Display a single statistic.
2513 print_one_stat(uint64_t value
)
2517 zfs_nicenum(value
, buf
, sizeof (buf
));
2518 (void) printf(" %5s", buf
);
2522 * Print out all the statistics for the given vdev. This can either be the
2523 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2524 * is a verbose output, and we don't want to display the toplevel pool stats.
2527 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2528 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2530 nvlist_t
**oldchild
, **newchild
;
2532 vdev_stat_t
*oldvs
, *newvs
;
2533 vdev_stat_t zerovs
= { 0 };
2538 if (oldnv
!= NULL
) {
2539 verify(nvlist_lookup_uint64_array(oldnv
,
2540 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2545 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2546 (uint64_t **)&newvs
, &c
) == 0);
2548 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2549 (void) printf("%*s%s", depth
, "", name
);
2551 (void) printf("%*s%s%*s", depth
, "", name
,
2552 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2554 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2559 scale
= (double)NANOSEC
/ tdelta
;
2561 /* only toplevel vdevs have capacity stats */
2562 if (newvs
->vs_space
== 0) {
2563 (void) printf(" - -");
2565 print_one_stat(newvs
->vs_alloc
);
2566 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2569 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2570 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2572 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2573 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2575 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2576 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2578 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2579 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2581 (void) printf("\n");
2583 if (!cb
->cb_verbose
)
2586 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2587 &newchild
, &children
) != 0)
2590 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2591 &oldchild
, &c
) != 0)
2594 for (c
= 0; c
< children
; c
++) {
2595 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2597 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2600 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2603 if (ishole
|| islog
)
2606 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2608 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2609 newchild
[c
], cb
, depth
+ 2);
2614 * Log device section
2617 if (num_logs(newnv
) > 0) {
2618 (void) printf("%-*s - - - - - "
2619 "-\n", cb
->cb_namewidth
, "logs");
2621 for (c
= 0; c
< children
; c
++) {
2622 uint64_t islog
= B_FALSE
;
2623 (void) nvlist_lookup_uint64(newchild
[c
],
2624 ZPOOL_CONFIG_IS_LOG
, &islog
);
2627 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2629 print_vdev_stats(zhp
, vname
, oldnv
?
2630 oldchild
[c
] : NULL
, newchild
[c
],
2639 * Include level 2 ARC devices in iostat output
2641 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2642 &newchild
, &children
) != 0)
2645 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2646 &oldchild
, &c
) != 0)
2650 (void) printf("%-*s - - - - - "
2651 "-\n", cb
->cb_namewidth
, "cache");
2652 for (c
= 0; c
< children
; c
++) {
2653 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2655 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2656 newchild
[c
], cb
, depth
+ 2);
2663 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2665 iostat_cbdata_t
*cb
= data
;
2669 * If the pool has disappeared, remove it from the list and continue.
2671 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2675 pool_list_remove(cb
->cb_list
, zhp
);
2681 * Callback to print out the iostats for the given pool.
2684 print_iostat(zpool_handle_t
*zhp
, void *data
)
2686 iostat_cbdata_t
*cb
= data
;
2687 nvlist_t
*oldconfig
, *newconfig
;
2688 nvlist_t
*oldnvroot
, *newnvroot
;
2690 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2692 if (cb
->cb_iteration
== 1)
2695 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2698 if (oldconfig
== NULL
)
2701 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2705 * Print out the statistics for the pool.
2707 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2710 print_iostat_separator(cb
);
2722 if (isatty(STDOUT_FILENO
)) {
2723 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
2725 columns
= ws
.ws_col
;
2734 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2736 iostat_cbdata_t
*cb
= data
;
2737 nvlist_t
*config
, *nvroot
;
2740 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2741 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2743 if (!cb
->cb_verbose
)
2744 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2746 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2747 cb
->cb_namewidth
, cb
->cb_name_flags
);
2751 * The width must be at least 10, but may be as large as the
2752 * column width - 42 so that we can still fit in one line.
2754 columns
= get_columns();
2756 if (cb
->cb_namewidth
< 10)
2757 cb
->cb_namewidth
= 10;
2758 if (cb
->cb_namewidth
> columns
- 42)
2759 cb
->cb_namewidth
= columns
- 42;
2765 * Parse the input string, get the 'interval' and 'count' value if there is one.
2768 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2771 unsigned long interval
= 0, count
= 0;
2775 * Determine if the last argument is an integer or a pool name
2777 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2781 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2783 if (*end
== '\0' && errno
== 0) {
2784 if (interval
== 0) {
2785 (void) fprintf(stderr
, gettext("interval "
2786 "cannot be zero\n"));
2790 * Ignore the last parameter
2795 * If this is not a valid number, just plow on. The
2796 * user will get a more informative error message later
2804 * If the last argument is also an integer, then we have both a count
2807 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2812 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2814 if (*end
== '\0' && errno
== 0) {
2815 if (interval
== 0) {
2816 (void) fprintf(stderr
, gettext("interval "
2817 "cannot be zero\n"));
2822 * Ignore the last parameter
2836 get_timestamp_arg(char c
)
2839 timestamp_fmt
= UDATE
;
2841 timestamp_fmt
= DDATE
;
2847 * zpool iostat [-gLPv] [-T d|u] [pool] ... [interval [count]]
2849 * -g Display guid for individual vdev name.
2850 * -L Follow links when resolving vdev path name.
2851 * -P Display full path for vdev name.
2852 * -v Display statistics for individual vdevs
2853 * -T Display a timestamp in date(1) or Unix format
2855 * This command can be tricky because we want to be able to deal with pool
2856 * creation/destruction as well as vdev configuration changes. The bulk of this
2857 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2858 * on pool_list_update() to detect the addition of new pools. Configuration
2859 * changes are all handled within libzfs.
2862 zpool_do_iostat(int argc
, char **argv
)
2867 unsigned long interval
= 0, count
= 0;
2869 boolean_t verbose
= B_FALSE
;
2870 boolean_t omit_since_boot
= B_FALSE
;
2871 boolean_t guid
= B_FALSE
;
2872 boolean_t follow_links
= B_FALSE
;
2873 boolean_t full_name
= B_FALSE
;
2874 iostat_cbdata_t cb
= { 0 };
2877 while ((c
= getopt(argc
, argv
, "gLPT:vy")) != -1) {
2883 follow_links
= B_TRUE
;
2889 get_timestamp_arg(*optarg
);
2895 omit_since_boot
= B_TRUE
;
2898 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2907 get_interval_count(&argc
, argv
, &interval
, &count
);
2910 * Construct the list of all interesting pools.
2913 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2916 if (pool_list_count(list
) == 0 && argc
!= 0) {
2917 pool_list_free(list
);
2921 if (pool_list_count(list
) == 0 && interval
== 0) {
2922 pool_list_free(list
);
2923 (void) fprintf(stderr
, gettext("no pools available\n"));
2928 * Enter the main iostat loop.
2931 cb
.cb_verbose
= verbose
;
2933 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
2935 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
2937 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
2938 cb
.cb_iteration
= 0;
2939 cb
.cb_namewidth
= 0;
2942 if ((npools
= pool_list_count(list
)) == 0)
2943 (void) fprintf(stderr
, gettext("no pools available\n"));
2946 * If this is the first iteration and -y was supplied
2947 * we skip any printing.
2949 boolean_t skip
= (omit_since_boot
&&
2950 cb
.cb_iteration
== 0);
2953 * Refresh all statistics. This is done as an
2954 * explicit step before calculating the maximum name
2955 * width, so that any * configuration changes are
2956 * properly accounted for.
2958 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
2962 * Iterate over all pools to determine the maximum width
2963 * for the pool / device name column across all pools.
2965 cb
.cb_namewidth
= 0;
2966 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
2969 if (timestamp_fmt
!= NODATE
)
2970 print_timestamp(timestamp_fmt
);
2973 * If it's the first time and we're not skipping it,
2974 * or either skip or verbose mode, print the header.
2976 if ((++cb
.cb_iteration
== 1 && !skip
) ||
2978 print_iostat_header(&cb
);
2981 (void) sleep(interval
);
2985 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2988 * If there's more than one pool, and we're not in
2989 * verbose mode (which prints a separator for us),
2990 * then print a separator.
2992 if (npools
> 1 && !verbose
)
2993 print_iostat_separator(&cb
);
2996 (void) printf("\n");
3000 * Flush the output so that redirection to a file isn't buffered
3003 (void) fflush(stdout
);
3008 if (count
!= 0 && --count
== 0)
3011 (void) sleep(interval
);
3014 pool_list_free(list
);
3019 typedef struct list_cbdata
{
3020 boolean_t cb_verbose
;
3023 boolean_t cb_scripted
;
3024 zprop_list_t
*cb_proplist
;
3028 * Given a list of columns to display, output appropriate headers for each one.
3031 print_header(list_cbdata_t
*cb
)
3033 zprop_list_t
*pl
= cb
->cb_proplist
;
3034 char headerbuf
[ZPOOL_MAXPROPLEN
];
3036 boolean_t first
= B_TRUE
;
3037 boolean_t right_justify
;
3040 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3041 width
= pl
->pl_width
;
3042 if (first
&& cb
->cb_verbose
) {
3044 * Reset the width to accommodate the verbose listing
3047 width
= cb
->cb_namewidth
;
3055 right_justify
= B_FALSE
;
3056 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3057 header
= zpool_prop_column_name(pl
->pl_prop
);
3058 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3062 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3063 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3064 headerbuf
[i
] = '\0';
3068 if (pl
->pl_next
== NULL
&& !right_justify
)
3069 (void) printf("%s", header
);
3070 else if (right_justify
)
3071 (void) printf("%*s", (int)width
, header
);
3073 (void) printf("%-*s", (int)width
, header
);
3076 (void) printf("\n");
3080 * Given a pool and a list of properties, print out all the properties according
3081 * to the described layout.
3084 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
3086 zprop_list_t
*pl
= cb
->cb_proplist
;
3087 boolean_t first
= B_TRUE
;
3088 char property
[ZPOOL_MAXPROPLEN
];
3090 boolean_t right_justify
;
3093 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3095 width
= pl
->pl_width
;
3096 if (first
&& cb
->cb_verbose
) {
3098 * Reset the width to accommodate the verbose listing
3101 width
= cb
->cb_namewidth
;
3105 if (cb
->cb_scripted
)
3106 (void) printf("\t");
3113 right_justify
= B_FALSE
;
3114 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3115 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
3116 sizeof (property
), NULL
) != 0)
3121 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3122 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
3123 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
3124 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
3125 sizeof (property
)) == 0) {
3133 * If this is being called in scripted mode, or if this is the
3134 * last column and it is left-justified, don't include a width
3137 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3138 (void) printf("%s", propstr
);
3139 else if (right_justify
)
3140 (void) printf("%*s", (int)width
, propstr
);
3142 (void) printf("%-*s", (int)width
, propstr
);
3145 (void) printf("\n");
3149 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
3154 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3157 case ZPOOL_PROP_EXPANDSZ
:
3159 (void) strlcpy(propval
, "-", sizeof (propval
));
3161 zfs_nicenum(value
, propval
, sizeof (propval
));
3163 case ZPOOL_PROP_FRAGMENTATION
:
3164 if (value
== ZFS_FRAG_INVALID
) {
3165 (void) strlcpy(propval
, "-", sizeof (propval
));
3167 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3168 (unsigned long long)value
);
3171 case ZPOOL_PROP_CAPACITY
:
3172 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3173 (unsigned long long)value
);
3176 zfs_nicenum(value
, propval
, sizeof (propval
));
3180 (void) strlcpy(propval
, "-", sizeof (propval
));
3183 (void) printf("\t%s", propval
);
3185 (void) printf(" %*s", (int)width
, propval
);
3189 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3190 list_cbdata_t
*cb
, int depth
)
3196 boolean_t scripted
= cb
->cb_scripted
;
3197 uint64_t islog
= B_FALSE
;
3198 boolean_t haslog
= B_FALSE
;
3199 char *dashes
= "%-*s - - - - - -\n";
3201 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3202 (uint64_t **)&vs
, &c
) == 0);
3205 boolean_t toplevel
= (vs
->vs_space
!= 0);
3209 (void) printf("\t%s", name
);
3210 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3211 (void) printf("%*s%s", depth
, "", name
);
3213 (void) printf("%*s%s%*s", depth
, "", name
,
3214 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3217 * Print the properties for the individual vdevs. Some
3218 * properties are only applicable to toplevel vdevs. The
3219 * 'toplevel' boolean value is passed to the print_one_column()
3220 * to indicate that the value is valid.
3222 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
3224 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
3226 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
3227 scripted
, toplevel
);
3228 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
3230 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
3231 vs
->vs_fragmentation
, scripted
,
3232 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
3233 cap
= (vs
->vs_space
== 0) ? 0 :
3234 (vs
->vs_alloc
* 100 / vs
->vs_space
);
3235 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
3236 (void) printf("\n");
3239 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3240 &child
, &children
) != 0)
3243 for (c
= 0; c
< children
; c
++) {
3244 uint64_t ishole
= B_FALSE
;
3246 if (nvlist_lookup_uint64(child
[c
],
3247 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3250 if (nvlist_lookup_uint64(child
[c
],
3251 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
3256 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3258 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3262 if (haslog
== B_TRUE
) {
3263 /* LINTED E_SEC_PRINTF_VAR_FMT */
3264 (void) printf(dashes
, cb
->cb_namewidth
, "log");
3265 for (c
= 0; c
< children
; c
++) {
3266 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3267 &islog
) != 0 || !islog
)
3269 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3271 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3276 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3277 &child
, &children
) == 0 && children
> 0) {
3278 /* LINTED E_SEC_PRINTF_VAR_FMT */
3279 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
3280 for (c
= 0; c
< children
; c
++) {
3281 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3283 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3288 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
3289 &children
) == 0 && children
> 0) {
3290 /* LINTED E_SEC_PRINTF_VAR_FMT */
3291 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
3292 for (c
= 0; c
< children
; c
++) {
3293 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3295 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3303 * Generic callback function to list a pool.
3306 list_callback(zpool_handle_t
*zhp
, void *data
)
3308 list_cbdata_t
*cbp
= data
;
3312 config
= zpool_get_config(zhp
, NULL
);
3314 print_pool(zhp
, cbp
);
3315 if (!cbp
->cb_verbose
)
3318 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3320 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3326 * zpool list [-gHLP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3328 * -g Display guid for individual vdev name.
3329 * -H Scripted mode. Don't display headers, and separate properties
3331 * -L Follow links when resolving vdev path name.
3332 * -o List of properties to display. Defaults to
3333 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3334 * "dedupratio,health,altroot"
3335 * -P Display full path for vdev name.
3336 * -T Display a timestamp in date(1) or Unix format
3338 * List all pools in the system, whether or not they're healthy. Output space
3339 * statistics for each one, as well as health status summary.
3342 zpool_do_list(int argc
, char **argv
)
3346 list_cbdata_t cb
= { 0 };
3347 static char default_props
[] =
3348 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3349 "dedupratio,health,altroot";
3350 char *props
= default_props
;
3351 unsigned long interval
= 0, count
= 0;
3353 boolean_t first
= B_TRUE
;
3356 while ((c
= getopt(argc
, argv
, ":gHLo:PT:v")) != -1) {
3359 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
3362 cb
.cb_scripted
= B_TRUE
;
3365 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3371 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
3374 get_timestamp_arg(*optarg
);
3377 cb
.cb_verbose
= B_TRUE
;
3380 (void) fprintf(stderr
, gettext("missing argument for "
3381 "'%c' option\n"), optopt
);
3385 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3394 get_interval_count(&argc
, argv
, &interval
, &count
);
3396 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3400 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
3404 if (pool_list_count(list
) == 0)
3407 if (timestamp_fmt
!= NODATE
)
3408 print_timestamp(timestamp_fmt
);
3410 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3414 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3419 if (count
!= 0 && --count
== 0)
3422 pool_list_free(list
);
3423 (void) sleep(interval
);
3426 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3427 (void) printf(gettext("no pools available\n"));
3431 pool_list_free(list
);
3432 zprop_free_list(cb
.cb_proplist
);
3437 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3439 boolean_t force
= B_FALSE
;
3442 char *poolname
, *old_disk
, *new_disk
;
3443 zpool_handle_t
*zhp
;
3444 nvlist_t
*props
= NULL
;
3449 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
3455 if ((propval
= strchr(optarg
, '=')) == NULL
) {
3456 (void) fprintf(stderr
, gettext("missing "
3457 "'=' for -o option\n"));
3463 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
3464 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
3468 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3477 /* get pool name and check number of arguments */
3479 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3486 (void) fprintf(stderr
,
3487 gettext("missing <device> specification\n"));
3495 (void) fprintf(stderr
,
3496 gettext("missing <new_device> specification\n"));
3499 new_disk
= old_disk
;
3509 (void) fprintf(stderr
, gettext("too many arguments\n"));
3513 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3516 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3517 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3523 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
3525 if (nvroot
== NULL
) {
3530 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3532 nvlist_free(nvroot
);
3539 * zpool replace [-f] <pool> <device> <new_device>
3541 * -f Force attach, even if <new_device> appears to be in use.
3543 * Replace <device> with <new_device>.
3547 zpool_do_replace(int argc
, char **argv
)
3549 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3553 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3555 * -f Force attach, even if <new_device> appears to be in use.
3556 * -o Set property=value.
3558 * Attach <new_device> to the mirror containing <device>. If <device> is not
3559 * part of a mirror, then <device> will be transformed into a mirror of
3560 * <device> and <new_device>. In either case, <new_device> will begin life
3561 * with a DTL of [0, now], and will immediately begin to resilver itself.
3564 zpool_do_attach(int argc
, char **argv
)
3566 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3570 * zpool detach [-f] <pool> <device>
3572 * -f Force detach of <device>, even if DTLs argue against it
3573 * (not supported yet)
3575 * Detach a device from a mirror. The operation will be refused if <device>
3576 * is the last device in the mirror, or if the DTLs indicate that this device
3577 * has the only valid copy of some data.
3581 zpool_do_detach(int argc
, char **argv
)
3584 char *poolname
, *path
;
3585 zpool_handle_t
*zhp
;
3589 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3593 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3602 /* get pool name and check number of arguments */
3604 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3609 (void) fprintf(stderr
,
3610 gettext("missing <device> specification\n"));
3617 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3620 ret
= zpool_vdev_detach(zhp
, path
);
3628 * zpool split [-gLnP] [-o prop=val] ...
3630 * [-R altroot] <pool> <newpool> [<device> ...]
3632 * -g Display guid for individual vdev name.
3633 * -L Follow links when resolving vdev path name.
3634 * -n Do not split the pool, but display the resulting layout if
3635 * it were to be split.
3636 * -o Set property=value, or set mount options.
3637 * -P Display full path for vdev name.
3638 * -R Mount the split-off pool under an alternate root.
3640 * Splits the named pool and gives it the new pool name. Devices to be split
3641 * off may be listed, provided that no more than one device is specified
3642 * per top-level vdev mirror. The newly split pool is left in an exported
3643 * state unless -R is specified.
3645 * Restrictions: the top-level of the pool pool must only be made up of
3646 * mirrors; all devices in the pool must be healthy; no device may be
3647 * undergoing a resilvering operation.
3650 zpool_do_split(int argc
, char **argv
)
3652 char *srcpool
, *newpool
, *propval
;
3653 char *mntopts
= NULL
;
3656 zpool_handle_t
*zhp
;
3657 nvlist_t
*config
, *props
= NULL
;
3659 flags
.dryrun
= B_FALSE
;
3660 flags
.import
= B_FALSE
;
3661 flags
.name_flags
= 0;
3664 while ((c
= getopt(argc
, argv
, ":gLR:no:P")) != -1) {
3667 flags
.name_flags
|= VDEV_NAME_GUID
;
3670 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3673 flags
.import
= B_TRUE
;
3675 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3676 &props
, B_TRUE
) != 0) {
3683 flags
.dryrun
= B_TRUE
;
3686 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3689 if (add_prop_list(optarg
, propval
,
3690 &props
, B_TRUE
) != 0) {
3700 flags
.name_flags
|= VDEV_NAME_PATH
;
3703 (void) fprintf(stderr
, gettext("missing argument for "
3704 "'%c' option\n"), optopt
);
3708 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3715 if (!flags
.import
&& mntopts
!= NULL
) {
3716 (void) fprintf(stderr
, gettext("setting mntopts is only "
3717 "valid when importing the pool\n"));
3725 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3729 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3739 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3742 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3743 if (config
== NULL
) {
3747 (void) printf(gettext("would create '%s' with the "
3748 "following layout:\n\n"), newpool
);
3749 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
3752 nvlist_free(config
);
3757 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3761 * The split was successful. Now we need to open the new
3762 * pool and import it.
3764 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3766 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3767 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3769 (void) fprintf(stderr
, gettext("Split was successful, but "
3770 "the datasets could not all be mounted\n"));
3771 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3772 "different altroot\n"), "zpool import");
3782 * zpool online <pool> <device> ...
3785 zpool_do_online(int argc
, char **argv
)
3789 zpool_handle_t
*zhp
;
3791 vdev_state_t newstate
;
3795 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3798 flags
|= ZFS_ONLINE_EXPAND
;
3802 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3811 /* get pool name and check number of arguments */
3813 (void) fprintf(stderr
, gettext("missing pool name\n"));
3817 (void) fprintf(stderr
, gettext("missing device name\n"));
3823 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3826 for (i
= 1; i
< argc
; i
++) {
3827 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3828 if (newstate
!= VDEV_STATE_HEALTHY
) {
3829 (void) printf(gettext("warning: device '%s' "
3830 "onlined, but remains in faulted state\n"),
3832 if (newstate
== VDEV_STATE_FAULTED
)
3833 (void) printf(gettext("use 'zpool "
3834 "clear' to restore a faulted "
3837 (void) printf(gettext("use 'zpool "
3838 "replace' to replace devices "
3839 "that are no longer present\n"));
3852 * zpool offline [-ft] <pool> <device> ...
3854 * -f Force the device into the offline state, even if doing
3855 * so would appear to compromise pool availability.
3856 * (not supported yet)
3858 * -t Only take the device off-line temporarily. The offline
3859 * state will not be persistent across reboots.
3863 zpool_do_offline(int argc
, char **argv
)
3867 zpool_handle_t
*zhp
;
3869 boolean_t istmp
= B_FALSE
;
3872 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3879 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3888 /* get pool name and check number of arguments */
3890 (void) fprintf(stderr
, gettext("missing pool name\n"));
3894 (void) fprintf(stderr
, gettext("missing device name\n"));
3900 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3903 for (i
= 1; i
< argc
; i
++) {
3904 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3914 * zpool clear <pool> [device]
3916 * Clear all errors associated with a pool or a particular device.
3919 zpool_do_clear(int argc
, char **argv
)
3923 boolean_t dryrun
= B_FALSE
;
3924 boolean_t do_rewind
= B_FALSE
;
3925 boolean_t xtreme_rewind
= B_FALSE
;
3926 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3927 nvlist_t
*policy
= NULL
;
3928 zpool_handle_t
*zhp
;
3929 char *pool
, *device
;
3932 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3941 xtreme_rewind
= B_TRUE
;
3944 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3954 (void) fprintf(stderr
, gettext("missing pool name\n"));
3959 (void) fprintf(stderr
, gettext("too many arguments\n"));
3963 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3964 (void) fprintf(stderr
,
3965 gettext("-n or -X only meaningful with -F\n"));
3969 rewind_policy
= ZPOOL_TRY_REWIND
;
3971 rewind_policy
= ZPOOL_DO_REWIND
;
3973 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3975 /* In future, further rewind policy choices can be passed along here */
3976 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3977 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3981 device
= argc
== 2 ? argv
[1] : NULL
;
3983 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3984 nvlist_free(policy
);
3988 if (zpool_clear(zhp
, device
, policy
) != 0)
3993 nvlist_free(policy
);
3999 * zpool reguid <pool>
4002 zpool_do_reguid(int argc
, char **argv
)
4006 zpool_handle_t
*zhp
;
4010 while ((c
= getopt(argc
, argv
, "")) != -1) {
4013 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4022 /* get pool name and check number of arguments */
4024 (void) fprintf(stderr
, gettext("missing pool name\n"));
4029 (void) fprintf(stderr
, gettext("too many arguments\n"));
4034 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
4037 ret
= zpool_reguid(zhp
);
4045 * zpool reopen <pool>
4047 * Reopen the pool so that the kernel can update the sizes of all vdevs.
4050 zpool_do_reopen(int argc
, char **argv
)
4054 zpool_handle_t
*zhp
;
4058 while ((c
= getopt(argc
, argv
, "")) != -1) {
4061 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4071 (void) fprintf(stderr
, gettext("missing pool name\n"));
4076 (void) fprintf(stderr
, gettext("too many arguments\n"));
4081 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
4084 ret
= zpool_reopen(zhp
);
4089 typedef struct scrub_cbdata
{
4096 scrub_callback(zpool_handle_t
*zhp
, void *data
)
4098 scrub_cbdata_t
*cb
= data
;
4102 * Ignore faulted pools.
4104 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
4105 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
4106 "currently unavailable\n"), zpool_get_name(zhp
));
4110 err
= zpool_scan(zhp
, cb
->cb_type
);
4116 * zpool scrub [-s] <pool> ...
4118 * -s Stop. Stops any in-progress scrub.
4121 zpool_do_scrub(int argc
, char **argv
)
4126 cb
.cb_type
= POOL_SCAN_SCRUB
;
4129 while ((c
= getopt(argc
, argv
, "s")) != -1) {
4132 cb
.cb_type
= POOL_SCAN_NONE
;
4135 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4147 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4151 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
4154 typedef struct status_cbdata
{
4157 boolean_t cb_allpools
;
4158 boolean_t cb_verbose
;
4159 boolean_t cb_explain
;
4161 boolean_t cb_dedup_stats
;
4165 * Print out detailed scrub status.
4168 print_scan_status(pool_scan_stat_t
*ps
)
4171 uint64_t elapsed
, mins_left
, hours_left
;
4172 uint64_t pass_exam
, examined
, total
;
4174 double fraction_done
;
4175 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4177 (void) printf(gettext(" scan: "));
4179 /* If there's never been a scan, there's not much to say. */
4180 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
4181 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
4182 (void) printf(gettext("none requested\n"));
4186 start
= ps
->pss_start_time
;
4187 end
= ps
->pss_end_time
;
4188 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
4190 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
4191 ps
->pss_func
== POOL_SCAN_RESILVER
);
4193 * Scan is finished or canceled.
4195 if (ps
->pss_state
== DSS_FINISHED
) {
4196 uint64_t minutes_taken
= (end
- start
) / 60;
4199 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4200 fmt
= gettext("scrub repaired %s in %lluh%um with "
4201 "%llu errors on %s");
4202 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4203 fmt
= gettext("resilvered %s in %lluh%um with "
4204 "%llu errors on %s");
4207 (void) printf(fmt
, processed_buf
,
4208 (u_longlong_t
)(minutes_taken
/ 60),
4209 (uint_t
)(minutes_taken
% 60),
4210 (u_longlong_t
)ps
->pss_errors
,
4211 ctime((time_t *)&end
));
4213 } else if (ps
->pss_state
== DSS_CANCELED
) {
4214 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4215 (void) printf(gettext("scrub canceled on %s"),
4217 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4218 (void) printf(gettext("resilver canceled on %s"),
4224 assert(ps
->pss_state
== DSS_SCANNING
);
4227 * Scan is in progress.
4229 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4230 (void) printf(gettext("scrub in progress since %s"),
4232 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4233 (void) printf(gettext("resilver in progress since %s"),
4237 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
4238 total
= ps
->pss_to_examine
;
4239 fraction_done
= (double)examined
/ total
;
4241 /* elapsed time for this pass */
4242 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4243 elapsed
= elapsed
? elapsed
: 1;
4244 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4245 rate
= pass_exam
/ elapsed
;
4246 rate
= rate
? rate
: 1;
4247 mins_left
= ((total
- examined
) / rate
) / 60;
4248 hours_left
= mins_left
/ 60;
4250 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4251 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4252 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4255 * do not print estimated time if hours_left is more than 30 days
4257 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
4258 examined_buf
, total_buf
, rate_buf
);
4259 if (hours_left
< (30 * 24)) {
4260 (void) printf(gettext(", %lluh%um to go\n"),
4261 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4263 (void) printf(gettext(
4264 ", (scan is slow, no estimated time)\n"));
4267 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4268 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
4269 processed_buf
, 100 * fraction_done
);
4270 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4271 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
4272 processed_buf
, 100 * fraction_done
);
4277 print_error_log(zpool_handle_t
*zhp
)
4279 nvlist_t
*nverrlist
= NULL
;
4282 size_t len
= MAXPATHLEN
* 2;
4284 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4285 (void) printf("errors: List of errors unavailable "
4286 "(insufficient privileges)\n");
4290 (void) printf("errors: Permanent errors have been "
4291 "detected in the following files:\n\n");
4293 pathname
= safe_malloc(len
);
4295 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4297 uint64_t dsobj
, obj
;
4299 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4300 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4302 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4304 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4305 (void) printf("%7s %s\n", "", pathname
);
4308 nvlist_free(nverrlist
);
4312 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4313 int namewidth
, int name_flags
)
4321 (void) printf(gettext("\tspares\n"));
4323 for (i
= 0; i
< nspares
; i
++) {
4324 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], name_flags
);
4325 print_status_config(zhp
, name
, spares
[i
],
4326 namewidth
, 2, B_TRUE
, name_flags
);
4332 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4333 int namewidth
, int name_flags
)
4341 (void) printf(gettext("\tcache\n"));
4343 for (i
= 0; i
< nl2cache
; i
++) {
4344 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], name_flags
);
4345 print_status_config(zhp
, name
, l2cache
[i
],
4346 namewidth
, 2, B_FALSE
, name_flags
);
4352 print_dedup_stats(nvlist_t
*config
)
4354 ddt_histogram_t
*ddh
;
4360 * If the pool was faulted then we may not have been able to
4361 * obtain the config. Otherwise, if we have anything in the dedup
4362 * table continue processing the stats.
4364 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4365 (uint64_t **)&ddo
, &c
) != 0)
4368 (void) printf("\n");
4369 (void) printf(gettext(" dedup: "));
4370 if (ddo
->ddo_count
== 0) {
4371 (void) printf(gettext("no DDT entries\n"));
4375 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4376 (u_longlong_t
)ddo
->ddo_count
,
4377 (u_longlong_t
)ddo
->ddo_dspace
,
4378 (u_longlong_t
)ddo
->ddo_mspace
);
4380 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4381 (uint64_t **)&dds
, &c
) == 0);
4382 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4383 (uint64_t **)&ddh
, &c
) == 0);
4384 zpool_dump_ddt(dds
, ddh
);
4388 * Display a summary of pool status. Displays a summary such as:
4392 * reason: One or more devices ...
4393 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4399 * When given the '-v' option, we print out the complete config. If the '-e'
4400 * option is specified, then we print out error rate information as well.
4403 status_callback(zpool_handle_t
*zhp
, void *data
)
4405 status_cbdata_t
*cbp
= data
;
4406 nvlist_t
*config
, *nvroot
;
4408 zpool_status_t reason
;
4409 zpool_errata_t errata
;
4414 config
= zpool_get_config(zhp
, NULL
);
4415 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
4420 * If we were given 'zpool status -x', only report those pools with
4423 if (cbp
->cb_explain
&&
4424 (reason
== ZPOOL_STATUS_OK
||
4425 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4426 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4427 if (!cbp
->cb_allpools
) {
4428 (void) printf(gettext("pool '%s' is healthy\n"),
4429 zpool_get_name(zhp
));
4431 cbp
->cb_first
= B_FALSE
;
4437 cbp
->cb_first
= B_FALSE
;
4439 (void) printf("\n");
4441 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4443 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4444 (uint64_t **)&vs
, &c
) == 0);
4445 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4447 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4448 (void) printf(gettext(" state: %s\n"), health
);
4451 case ZPOOL_STATUS_MISSING_DEV_R
:
4452 (void) printf(gettext("status: One or more devices could not "
4453 "be opened. Sufficient replicas exist for\n\tthe pool to "
4454 "continue functioning in a degraded state.\n"));
4455 (void) printf(gettext("action: Attach the missing device and "
4456 "online it using 'zpool online'.\n"));
4459 case ZPOOL_STATUS_MISSING_DEV_NR
:
4460 (void) printf(gettext("status: One or more devices could not "
4461 "be opened. There are insufficient\n\treplicas for the "
4462 "pool to continue functioning.\n"));
4463 (void) printf(gettext("action: Attach the missing device and "
4464 "online it using 'zpool online'.\n"));
4467 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4468 (void) printf(gettext("status: One or more devices could not "
4469 "be used because the label is missing or\n\tinvalid. "
4470 "Sufficient replicas exist for the pool to continue\n\t"
4471 "functioning in a degraded state.\n"));
4472 (void) printf(gettext("action: Replace the device using "
4473 "'zpool replace'.\n"));
4476 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4477 (void) printf(gettext("status: One or more devices could not "
4478 "be used because the label is missing \n\tor invalid. "
4479 "There are insufficient replicas for the pool to "
4480 "continue\n\tfunctioning.\n"));
4481 zpool_explain_recover(zpool_get_handle(zhp
),
4482 zpool_get_name(zhp
), reason
, config
);
4485 case ZPOOL_STATUS_FAILING_DEV
:
4486 (void) printf(gettext("status: One or more devices has "
4487 "experienced an unrecoverable error. An\n\tattempt was "
4488 "made to correct the error. Applications are "
4490 (void) printf(gettext("action: Determine if the device needs "
4491 "to be replaced, and clear the errors\n\tusing "
4492 "'zpool clear' or replace the device with 'zpool "
4496 case ZPOOL_STATUS_OFFLINE_DEV
:
4497 (void) printf(gettext("status: One or more devices has "
4498 "been taken offline by the administrator.\n\tSufficient "
4499 "replicas exist for the pool to continue functioning in "
4500 "a\n\tdegraded state.\n"));
4501 (void) printf(gettext("action: Online the device using "
4502 "'zpool online' or replace the device with\n\t'zpool "
4506 case ZPOOL_STATUS_REMOVED_DEV
:
4507 (void) printf(gettext("status: One or more devices has "
4508 "been removed by the administrator.\n\tSufficient "
4509 "replicas exist for the pool to continue functioning in "
4510 "a\n\tdegraded state.\n"));
4511 (void) printf(gettext("action: Online the device using "
4512 "'zpool online' or replace the device with\n\t'zpool "
4516 case ZPOOL_STATUS_RESILVERING
:
4517 (void) printf(gettext("status: One or more devices is "
4518 "currently being resilvered. The pool will\n\tcontinue "
4519 "to function, possibly in a degraded state.\n"));
4520 (void) printf(gettext("action: Wait for the resilver to "
4524 case ZPOOL_STATUS_CORRUPT_DATA
:
4525 (void) printf(gettext("status: One or more devices has "
4526 "experienced an error resulting in data\n\tcorruption. "
4527 "Applications may be affected.\n"));
4528 (void) printf(gettext("action: Restore the file in question "
4529 "if possible. Otherwise restore the\n\tentire pool from "
4533 case ZPOOL_STATUS_CORRUPT_POOL
:
4534 (void) printf(gettext("status: The pool metadata is corrupted "
4535 "and the pool cannot be opened.\n"));
4536 zpool_explain_recover(zpool_get_handle(zhp
),
4537 zpool_get_name(zhp
), reason
, config
);
4540 case ZPOOL_STATUS_VERSION_OLDER
:
4541 (void) printf(gettext("status: The pool is formatted using a "
4542 "legacy on-disk format. The pool can\n\tstill be used, "
4543 "but some features are unavailable.\n"));
4544 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4545 "upgrade'. Once this is done, the\n\tpool will no longer "
4546 "be accessible on software that does not support\n\t"
4547 "feature flags.\n"));
4550 case ZPOOL_STATUS_VERSION_NEWER
:
4551 (void) printf(gettext("status: The pool has been upgraded to a "
4552 "newer, incompatible on-disk version.\n\tThe pool cannot "
4553 "be accessed on this system.\n"));
4554 (void) printf(gettext("action: Access the pool from a system "
4555 "running more recent software, or\n\trestore the pool from "
4559 case ZPOOL_STATUS_FEAT_DISABLED
:
4560 (void) printf(gettext("status: Some supported features are not "
4561 "enabled on the pool. The pool can\n\tstill be used, but "
4562 "some features are unavailable.\n"));
4563 (void) printf(gettext("action: Enable all features using "
4564 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4565 "longer be accessible by software that does not support\n\t"
4566 "the features. See zpool-features(5) for details.\n"));
4569 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4570 (void) printf(gettext("status: The pool cannot be accessed on "
4571 "this system because it uses the\n\tfollowing feature(s) "
4572 "not supported on this system:\n"));
4573 zpool_print_unsup_feat(config
);
4574 (void) printf("\n");
4575 (void) printf(gettext("action: Access the pool from a system "
4576 "that supports the required feature(s),\n\tor restore the "
4577 "pool from backup.\n"));
4580 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4581 (void) printf(gettext("status: The pool can only be accessed "
4582 "in read-only mode on this system. It\n\tcannot be "
4583 "accessed in read-write mode because it uses the "
4584 "following\n\tfeature(s) not supported on this system:\n"));
4585 zpool_print_unsup_feat(config
);
4586 (void) printf("\n");
4587 (void) printf(gettext("action: The pool cannot be accessed in "
4588 "read-write mode. Import the pool with\n"
4589 "\t\"-o readonly=on\", access the pool from a system that "
4590 "supports the\n\trequired feature(s), or restore the "
4591 "pool from backup.\n"));
4594 case ZPOOL_STATUS_FAULTED_DEV_R
:
4595 (void) printf(gettext("status: One or more devices are "
4596 "faulted in response to persistent errors.\n\tSufficient "
4597 "replicas exist for the pool to continue functioning "
4598 "in a\n\tdegraded state.\n"));
4599 (void) printf(gettext("action: Replace the faulted device, "
4600 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4603 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4604 (void) printf(gettext("status: One or more devices are "
4605 "faulted in response to persistent errors. There are "
4606 "insufficient replicas for the pool to\n\tcontinue "
4608 (void) printf(gettext("action: Destroy and re-create the pool "
4609 "from a backup source. Manually marking the device\n"
4610 "\trepaired using 'zpool clear' may allow some data "
4611 "to be recovered.\n"));
4614 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4615 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4616 (void) printf(gettext("status: One or more devices are "
4617 "faulted in response to IO failures.\n"));
4618 (void) printf(gettext("action: Make sure the affected devices "
4619 "are connected, then run 'zpool clear'.\n"));
4622 case ZPOOL_STATUS_BAD_LOG
:
4623 (void) printf(gettext("status: An intent log record "
4624 "could not be read.\n"
4625 "\tWaiting for adminstrator intervention to fix the "
4626 "faulted pool.\n"));
4627 (void) printf(gettext("action: Either restore the affected "
4628 "device(s) and run 'zpool online',\n"
4629 "\tor ignore the intent log records by running "
4630 "'zpool clear'.\n"));
4633 case ZPOOL_STATUS_HOSTID_MISMATCH
:
4634 (void) printf(gettext("status: Mismatch between pool hostid "
4635 "and system hostid on imported pool.\n\tThis pool was "
4636 "previously imported into a system with a different "
4637 "hostid,\n\tand then was verbatim imported into this "
4639 (void) printf(gettext("action: Export this pool on all systems "
4640 "on which it is imported.\n"
4641 "\tThen import it to correct the mismatch.\n"));
4644 case ZPOOL_STATUS_ERRATA
:
4645 (void) printf(gettext("status: Errata #%d detected.\n"),
4649 case ZPOOL_ERRATA_NONE
:
4652 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
4653 (void) printf(gettext("action: To correct the issue "
4654 "run 'zpool scrub'.\n"));
4659 * All errata which allow the pool to be imported
4660 * must contain an action message.
4668 * The remaining errors can't actually be generated, yet.
4670 assert(reason
== ZPOOL_STATUS_OK
);
4674 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4677 if (config
!= NULL
) {
4680 nvlist_t
**spares
, **l2cache
;
4681 uint_t nspares
, nl2cache
;
4682 pool_scan_stat_t
*ps
= NULL
;
4684 (void) nvlist_lookup_uint64_array(nvroot
,
4685 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4686 print_scan_status(ps
);
4688 namewidth
= max_width(zhp
, nvroot
, 0, 0, cbp
->cb_name_flags
);
4692 (void) printf(gettext("config:\n\n"));
4693 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4694 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4695 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4696 namewidth
, 0, B_FALSE
, cbp
->cb_name_flags
);
4698 if (num_logs(nvroot
) > 0)
4699 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
,
4700 cbp
->cb_name_flags
);
4701 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4702 &l2cache
, &nl2cache
) == 0)
4703 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
,
4704 cbp
->cb_name_flags
);
4706 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4707 &spares
, &nspares
) == 0)
4708 print_spares(zhp
, spares
, nspares
, namewidth
,
4709 cbp
->cb_name_flags
);
4711 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4713 nvlist_t
*nverrlist
= NULL
;
4716 * If the approximate error count is small, get a
4717 * precise count by fetching the entire log and
4718 * uniquifying the results.
4720 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4721 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4726 while ((elem
= nvlist_next_nvpair(nverrlist
,
4731 nvlist_free(nverrlist
);
4733 (void) printf("\n");
4736 (void) printf(gettext("errors: No known data "
4738 else if (!cbp
->cb_verbose
)
4739 (void) printf(gettext("errors: %llu data "
4740 "errors, use '-v' for a list\n"),
4741 (u_longlong_t
)nerr
);
4743 print_error_log(zhp
);
4746 if (cbp
->cb_dedup_stats
)
4747 print_dedup_stats(config
);
4749 (void) printf(gettext("config: The configuration cannot be "
4757 * zpool status [-gLPvx] [-T d|u] [pool] ... [interval [count]]
4759 * -g Display guid for individual vdev name.
4760 * -L Follow links when resolving vdev path name.
4761 * -P Display full path for vdev name.
4762 * -v Display complete error logs
4763 * -x Display only pools with potential problems
4764 * -D Display dedup status (undocumented)
4765 * -T Display a timestamp in date(1) or Unix format
4767 * Describes the health status of all pools or some subset.
4770 zpool_do_status(int argc
, char **argv
)
4774 unsigned long interval
= 0, count
= 0;
4775 status_cbdata_t cb
= { 0 };
4778 while ((c
= getopt(argc
, argv
, "gLPvxDT:")) != -1) {
4781 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4784 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4787 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4790 cb
.cb_verbose
= B_TRUE
;
4793 cb
.cb_explain
= B_TRUE
;
4796 cb
.cb_dedup_stats
= B_TRUE
;
4799 get_timestamp_arg(*optarg
);
4802 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4811 get_interval_count(&argc
, argv
, &interval
, &count
);
4814 cb
.cb_allpools
= B_TRUE
;
4816 cb
.cb_first
= B_TRUE
;
4819 if (timestamp_fmt
!= NODATE
)
4820 print_timestamp(timestamp_fmt
);
4822 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4823 status_callback
, &cb
);
4825 if (argc
== 0 && cb
.cb_count
== 0)
4826 (void) fprintf(stderr
, gettext("no pools available\n"));
4827 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4828 (void) printf(gettext("all pools are healthy\n"));
4836 if (count
!= 0 && --count
== 0)
4839 (void) sleep(interval
);
4845 typedef struct upgrade_cbdata
{
4848 uint64_t cb_version
;
4853 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
4855 int zfs_version
= (int) zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
4856 int *count
= (int *)unsupp_fs
;
4858 if (zfs_version
> ZPL_VERSION
) {
4859 (void) printf(gettext("%s (v%d) is not supported by this "
4860 "implementation of ZFS.\n"),
4861 zfs_get_name(zhp
), zfs_version
);
4865 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
4873 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4877 uint64_t oldversion
;
4880 config
= zpool_get_config(zhp
, NULL
);
4881 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4884 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4885 assert(oldversion
< version
);
4887 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
4892 (void) fprintf(stderr
, gettext("Upgrade not performed due "
4893 "to %d unsupported filesystems (max v%d).\n"),
4894 unsupp_fs
, (int) ZPL_VERSION
);
4898 ret
= zpool_upgrade(zhp
, version
);
4902 if (version
>= SPA_VERSION_FEATURES
) {
4903 (void) printf(gettext("Successfully upgraded "
4904 "'%s' from version %llu to feature flags.\n"),
4905 zpool_get_name(zhp
), (u_longlong_t
) oldversion
);
4907 (void) printf(gettext("Successfully upgraded "
4908 "'%s' from version %llu to version %llu.\n"),
4909 zpool_get_name(zhp
), (u_longlong_t
) oldversion
,
4910 (u_longlong_t
) version
);
4917 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4920 boolean_t firstff
= B_TRUE
;
4921 nvlist_t
*enabled
= zpool_get_features(zhp
);
4924 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4925 const char *fname
= spa_feature_table
[i
].fi_uname
;
4926 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4927 if (!nvlist_exists(enabled
, fguid
)) {
4929 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4930 ret
= zpool_set_prop(zhp
, propname
,
4931 ZFS_FEATURE_ENABLED
);
4939 (void) printf(gettext("Enabled the "
4940 "following features on '%s':\n"),
4941 zpool_get_name(zhp
));
4944 (void) printf(gettext(" %s\n"), fname
);
4955 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4957 upgrade_cbdata_t
*cbp
= arg
;
4960 boolean_t printnl
= B_FALSE
;
4963 config
= zpool_get_config(zhp
, NULL
);
4964 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4967 assert(SPA_VERSION_IS_SUPPORTED(version
));
4969 if (version
< cbp
->cb_version
) {
4970 cbp
->cb_first
= B_FALSE
;
4971 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4977 * If they did "zpool upgrade -a", then we could
4978 * be doing ioctls to different pools. We need
4979 * to log this history once to each pool, and bypass
4980 * the normal history logging that happens in main().
4982 (void) zpool_log_history(g_zfs
, history_str
);
4983 log_history
= B_FALSE
;
4986 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4988 ret
= upgrade_enable_all(zhp
, &count
);
4993 cbp
->cb_first
= B_FALSE
;
4999 (void) printf(gettext("\n"));
5006 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
5008 upgrade_cbdata_t
*cbp
= arg
;
5012 config
= zpool_get_config(zhp
, NULL
);
5013 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
5016 assert(SPA_VERSION_IS_SUPPORTED(version
));
5018 if (version
< SPA_VERSION_FEATURES
) {
5019 if (cbp
->cb_first
) {
5020 (void) printf(gettext("The following pools are "
5021 "formatted with legacy version numbers and can\n"
5022 "be upgraded to use feature flags. After "
5023 "being upgraded, these pools\nwill no "
5024 "longer be accessible by software that does not "
5025 "support feature\nflags.\n\n"));
5026 (void) printf(gettext("VER POOL\n"));
5027 (void) printf(gettext("--- ------------\n"));
5028 cbp
->cb_first
= B_FALSE
;
5031 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
5032 zpool_get_name(zhp
));
5039 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
5041 upgrade_cbdata_t
*cbp
= arg
;
5045 config
= zpool_get_config(zhp
, NULL
);
5046 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
5049 if (version
>= SPA_VERSION_FEATURES
) {
5051 boolean_t poolfirst
= B_TRUE
;
5052 nvlist_t
*enabled
= zpool_get_features(zhp
);
5054 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5055 const char *fguid
= spa_feature_table
[i
].fi_guid
;
5056 const char *fname
= spa_feature_table
[i
].fi_uname
;
5057 if (!nvlist_exists(enabled
, fguid
)) {
5058 if (cbp
->cb_first
) {
5059 (void) printf(gettext("\nSome "
5060 "supported features are not "
5061 "enabled on the following pools. "
5062 "Once a\nfeature is enabled the "
5063 "pool may become incompatible with "
5064 "software\nthat does not support "
5066 "zpool-features(5) for "
5068 (void) printf(gettext("POOL "
5070 (void) printf(gettext("------"
5072 cbp
->cb_first
= B_FALSE
;
5076 (void) printf(gettext("%s\n"),
5077 zpool_get_name(zhp
));
5078 poolfirst
= B_FALSE
;
5081 (void) printf(gettext(" %s\n"), fname
);
5084 * If they did "zpool upgrade -a", then we could
5085 * be doing ioctls to different pools. We need
5086 * to log this history once to each pool, and bypass
5087 * the normal history logging that happens in main().
5089 (void) zpool_log_history(g_zfs
, history_str
);
5090 log_history
= B_FALSE
;
5099 upgrade_one(zpool_handle_t
*zhp
, void *data
)
5101 boolean_t printnl
= B_FALSE
;
5102 upgrade_cbdata_t
*cbp
= data
;
5103 uint64_t cur_version
;
5106 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
5107 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
5108 "Pool 'log' must be renamed using export and import"
5113 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5114 if (cur_version
> cbp
->cb_version
) {
5115 (void) printf(gettext("Pool '%s' is already formatted "
5116 "using more current version '%llu'.\n\n"),
5117 zpool_get_name(zhp
), (u_longlong_t
) cur_version
);
5121 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
5122 (void) printf(gettext("Pool '%s' is already formatted "
5123 "using version %llu.\n\n"), zpool_get_name(zhp
),
5124 (u_longlong_t
) cbp
->cb_version
);
5128 if (cur_version
!= cbp
->cb_version
) {
5130 ret
= upgrade_version(zhp
, cbp
->cb_version
);
5135 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
5137 ret
= upgrade_enable_all(zhp
, &count
);
5143 } else if (cur_version
== SPA_VERSION
) {
5144 (void) printf(gettext("Pool '%s' already has all "
5145 "supported features enabled.\n"),
5146 zpool_get_name(zhp
));
5151 (void) printf(gettext("\n"));
5160 * zpool upgrade [-V version] <-a | pool ...>
5162 * With no arguments, display downrev'd ZFS pool available for upgrade.
5163 * Individual pools can be upgraded by specifying the pool, and '-a' will
5164 * upgrade all pools.
5167 zpool_do_upgrade(int argc
, char **argv
)
5170 upgrade_cbdata_t cb
= { 0 };
5172 boolean_t showversions
= B_FALSE
;
5173 boolean_t upgradeall
= B_FALSE
;
5178 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
5181 upgradeall
= B_TRUE
;
5184 showversions
= B_TRUE
;
5187 cb
.cb_version
= strtoll(optarg
, &end
, 10);
5189 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
5190 (void) fprintf(stderr
,
5191 gettext("invalid version '%s'\n"), optarg
);
5196 (void) fprintf(stderr
, gettext("missing argument for "
5197 "'%c' option\n"), optopt
);
5201 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5212 if (cb
.cb_version
== 0) {
5213 cb
.cb_version
= SPA_VERSION
;
5214 } else if (!upgradeall
&& argc
== 0) {
5215 (void) fprintf(stderr
, gettext("-V option is "
5216 "incompatible with other arguments\n"));
5221 if (upgradeall
|| argc
!= 0) {
5222 (void) fprintf(stderr
, gettext("-v option is "
5223 "incompatible with other arguments\n"));
5226 } else if (upgradeall
) {
5228 (void) fprintf(stderr
, gettext("-a option should not "
5229 "be used along with a pool name\n"));
5234 (void) printf(gettext("This system supports ZFS pool feature "
5239 (void) printf(gettext("The following features are "
5241 (void) printf(gettext("FEAT DESCRIPTION\n"));
5242 (void) printf("----------------------------------------------"
5243 "---------------\n");
5244 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5245 zfeature_info_t
*fi
= &spa_feature_table
[i
];
5247 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
5248 " (read-only compatible)" : "";
5250 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
5251 (void) printf(" %s\n", fi
->fi_desc
);
5253 (void) printf("\n");
5255 (void) printf(gettext("The following legacy versions are also "
5257 (void) printf(gettext("VER DESCRIPTION\n"));
5258 (void) printf("--- -----------------------------------------"
5259 "---------------\n");
5260 (void) printf(gettext(" 1 Initial ZFS version\n"));
5261 (void) printf(gettext(" 2 Ditto blocks "
5262 "(replicated metadata)\n"));
5263 (void) printf(gettext(" 3 Hot spares and double parity "
5265 (void) printf(gettext(" 4 zpool history\n"));
5266 (void) printf(gettext(" 5 Compression using the gzip "
5268 (void) printf(gettext(" 6 bootfs pool property\n"));
5269 (void) printf(gettext(" 7 Separate intent log devices\n"));
5270 (void) printf(gettext(" 8 Delegated administration\n"));
5271 (void) printf(gettext(" 9 refquota and refreservation "
5273 (void) printf(gettext(" 10 Cache devices\n"));
5274 (void) printf(gettext(" 11 Improved scrub performance\n"));
5275 (void) printf(gettext(" 12 Snapshot properties\n"));
5276 (void) printf(gettext(" 13 snapused property\n"));
5277 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5278 (void) printf(gettext(" 15 user/group space accounting\n"));
5279 (void) printf(gettext(" 16 stmf property support\n"));
5280 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5281 (void) printf(gettext(" 18 Snapshot user holds\n"));
5282 (void) printf(gettext(" 19 Log device removal\n"));
5283 (void) printf(gettext(" 20 Compression using zle "
5284 "(zero-length encoding)\n"));
5285 (void) printf(gettext(" 21 Deduplication\n"));
5286 (void) printf(gettext(" 22 Received properties\n"));
5287 (void) printf(gettext(" 23 Slim ZIL\n"));
5288 (void) printf(gettext(" 24 System attributes\n"));
5289 (void) printf(gettext(" 25 Improved scrub stats\n"));
5290 (void) printf(gettext(" 26 Improved snapshot deletion "
5292 (void) printf(gettext(" 27 Improved snapshot creation "
5294 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5295 (void) printf(gettext("\nFor more information on a particular "
5296 "version, including supported releases,\n"));
5297 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5298 } else if (argc
== 0 && upgradeall
) {
5299 cb
.cb_first
= B_TRUE
;
5300 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5301 if (ret
== 0 && cb
.cb_first
) {
5302 if (cb
.cb_version
== SPA_VERSION
) {
5303 (void) printf(gettext("All pools are already "
5304 "formatted using feature flags.\n\n"));
5305 (void) printf(gettext("Every feature flags "
5306 "pool already has all supported features "
5309 (void) printf(gettext("All pools are already "
5310 "formatted with version %llu or higher.\n"),
5311 (u_longlong_t
) cb
.cb_version
);
5314 } else if (argc
== 0) {
5315 cb
.cb_first
= B_TRUE
;
5316 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5320 (void) printf(gettext("All pools are formatted "
5321 "using feature flags.\n\n"));
5323 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5324 "for a list of available legacy versions.\n"));
5327 cb
.cb_first
= B_TRUE
;
5328 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5332 (void) printf(gettext("Every feature flags pool has "
5333 "all supported features enabled.\n"));
5335 (void) printf(gettext("\n"));
5338 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5345 typedef struct hist_cbdata
{
5352 * Print out the command history for a specific pool.
5355 get_history_one(zpool_handle_t
*zhp
, void *data
)
5361 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5363 cb
->first
= B_FALSE
;
5365 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5367 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5370 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5371 &records
, &numrecords
) == 0);
5372 for (i
= 0; i
< numrecords
; i
++) {
5373 nvlist_t
*rec
= records
[i
];
5376 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5380 tsec
= fnvlist_lookup_uint64(records
[i
],
5382 (void) localtime_r(&tsec
, &t
);
5383 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5386 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5387 (void) printf("%s %s", tbuf
,
5388 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5389 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5391 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5394 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5395 (void) printf("%s unrecognized record:\n",
5397 dump_nvlist(rec
, 4);
5400 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5401 zfs_history_event_names
[ievent
],
5402 (longlong_t
) fnvlist_lookup_uint64(
5403 rec
, ZPOOL_HIST_TXG
),
5404 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5405 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5408 (void) printf("%s [txg:%lld] %s", tbuf
,
5409 (longlong_t
) fnvlist_lookup_uint64(
5410 rec
, ZPOOL_HIST_TXG
),
5411 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5412 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5413 (void) printf(" %s (%llu)",
5414 fnvlist_lookup_string(rec
,
5416 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
5419 (void) printf(" %s", fnvlist_lookup_string(rec
,
5420 ZPOOL_HIST_INT_STR
));
5421 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5424 (void) printf("%s ioctl %s\n", tbuf
,
5425 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5426 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5427 (void) printf(" input:\n");
5428 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5429 ZPOOL_HIST_INPUT_NVL
), 8);
5431 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5432 (void) printf(" output:\n");
5433 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5434 ZPOOL_HIST_OUTPUT_NVL
), 8);
5439 (void) printf("%s unrecognized record:\n", tbuf
);
5440 dump_nvlist(rec
, 4);
5444 (void) printf("\n");
5447 (void) printf(" [");
5448 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5449 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5450 struct passwd
*pwd
= getpwuid(who
);
5451 (void) printf("user %d ", (int)who
);
5453 (void) printf("(%s) ", pwd
->pw_name
);
5455 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5456 (void) printf("on %s",
5457 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5459 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5460 (void) printf(":%s",
5461 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5465 (void) printf("\n");
5467 (void) printf("\n");
5474 * zpool history <pool>
5476 * Displays the history of commands that modified pools.
5479 zpool_do_history(int argc
, char **argv
)
5481 hist_cbdata_t cbdata
= { 0 };
5485 cbdata
.first
= B_TRUE
;
5487 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5490 cbdata
.longfmt
= B_TRUE
;
5493 cbdata
.internal
= B_TRUE
;
5496 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5504 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5507 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5508 (void) fprintf(stderr
, gettext("no pools available\n"));
5515 typedef struct ev_opts
{
5523 zpool_do_events_short(nvlist_t
*nvl
)
5525 char ctime_str
[26], str
[32], *ptr
;
5529 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
5530 memset(str
, ' ', 32);
5531 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
5532 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
5533 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
5534 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
5535 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
5536 (void) printf(gettext("%s "), str
);
5538 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
5539 (void) printf(gettext("%s\n"), ptr
);
5543 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
5547 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
5548 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
5550 data_type_t type
= nvpair_type(nvp
);
5551 const char *name
= nvpair_name(nvp
);
5561 printf(gettext("%*s%s = "), depth
, "", name
);
5564 case DATA_TYPE_BOOLEAN
:
5565 printf(gettext("%s"), "1");
5568 case DATA_TYPE_BOOLEAN_VALUE
:
5569 (void) nvpair_value_boolean_value(nvp
, &b
);
5570 printf(gettext("%s"), b
? "1" : "0");
5573 case DATA_TYPE_BYTE
:
5574 (void) nvpair_value_byte(nvp
, &i8
);
5575 printf(gettext("0x%x"), i8
);
5578 case DATA_TYPE_INT8
:
5579 (void) nvpair_value_int8(nvp
, (void *)&i8
);
5580 printf(gettext("0x%x"), i8
);
5583 case DATA_TYPE_UINT8
:
5584 (void) nvpair_value_uint8(nvp
, &i8
);
5585 printf(gettext("0x%x"), i8
);
5588 case DATA_TYPE_INT16
:
5589 (void) nvpair_value_int16(nvp
, (void *)&i16
);
5590 printf(gettext("0x%x"), i16
);
5593 case DATA_TYPE_UINT16
:
5594 (void) nvpair_value_uint16(nvp
, &i16
);
5595 printf(gettext("0x%x"), i16
);
5598 case DATA_TYPE_INT32
:
5599 (void) nvpair_value_int32(nvp
, (void *)&i32
);
5600 printf(gettext("0x%x"), i32
);
5603 case DATA_TYPE_UINT32
:
5604 (void) nvpair_value_uint32(nvp
, &i32
);
5605 printf(gettext("0x%x"), i32
);
5608 case DATA_TYPE_INT64
:
5609 (void) nvpair_value_int64(nvp
, (void *)&i64
);
5610 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5613 case DATA_TYPE_UINT64
:
5614 (void) nvpair_value_uint64(nvp
, &i64
);
5615 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5618 case DATA_TYPE_HRTIME
:
5619 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
5620 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5623 case DATA_TYPE_STRING
:
5624 (void) nvpair_value_string(nvp
, &str
);
5625 printf(gettext("\"%s\""), str
? str
: "<NULL>");
5628 case DATA_TYPE_NVLIST
:
5629 printf(gettext("(embedded nvlist)\n"));
5630 (void) nvpair_value_nvlist(nvp
, &cnv
);
5631 zpool_do_events_nvprint(cnv
, depth
+ 8);
5632 printf(gettext("%*s(end %s)"), depth
, "", name
);
5635 case DATA_TYPE_NVLIST_ARRAY
: {
5639 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
5640 printf(gettext("(%d embedded nvlists)\n"), nelem
);
5641 for (i
= 0; i
< nelem
; i
++) {
5642 printf(gettext("%*s%s[%d] = %s\n"),
5643 depth
, "", name
, i
, "(embedded nvlist)");
5644 zpool_do_events_nvprint(val
[i
], depth
+ 8);
5645 printf(gettext("%*s(end %s[%i])\n"),
5646 depth
, "", name
, i
);
5648 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
5652 case DATA_TYPE_INT8_ARRAY
: {
5656 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
5657 for (i
= 0; i
< nelem
; i
++)
5658 printf(gettext("0x%x "), val
[i
]);
5663 case DATA_TYPE_UINT8_ARRAY
: {
5667 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
5668 for (i
= 0; i
< nelem
; i
++)
5669 printf(gettext("0x%x "), val
[i
]);
5674 case DATA_TYPE_INT16_ARRAY
: {
5678 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
5679 for (i
= 0; i
< nelem
; i
++)
5680 printf(gettext("0x%x "), val
[i
]);
5685 case DATA_TYPE_UINT16_ARRAY
: {
5689 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
5690 for (i
= 0; i
< nelem
; i
++)
5691 printf(gettext("0x%x "), val
[i
]);
5696 case DATA_TYPE_INT32_ARRAY
: {
5700 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
5701 for (i
= 0; i
< nelem
; i
++)
5702 printf(gettext("0x%x "), val
[i
]);
5707 case DATA_TYPE_UINT32_ARRAY
: {
5711 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
5712 for (i
= 0; i
< nelem
; i
++)
5713 printf(gettext("0x%x "), val
[i
]);
5718 case DATA_TYPE_INT64_ARRAY
: {
5722 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
5723 for (i
= 0; i
< nelem
; i
++)
5724 printf(gettext("0x%llx "),
5725 (u_longlong_t
)val
[i
]);
5730 case DATA_TYPE_UINT64_ARRAY
: {
5734 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
5735 for (i
= 0; i
< nelem
; i
++)
5736 printf(gettext("0x%llx "),
5737 (u_longlong_t
)val
[i
]);
5742 case DATA_TYPE_STRING_ARRAY
: {
5746 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
5747 for (i
= 0; i
< nelem
; i
++)
5748 printf(gettext("\"%s\" "),
5749 str
[i
] ? str
[i
] : "<NULL>");
5754 case DATA_TYPE_BOOLEAN_ARRAY
:
5755 case DATA_TYPE_BYTE_ARRAY
:
5756 case DATA_TYPE_DOUBLE
:
5757 case DATA_TYPE_UNKNOWN
:
5758 printf(gettext("<unknown>"));
5762 printf(gettext("\n"));
5767 zpool_do_events_next(ev_opts_t
*opts
)
5770 int zevent_fd
, ret
, dropped
;
5772 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
5773 VERIFY(zevent_fd
>= 0);
5775 if (!opts
->scripted
)
5776 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5779 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
5780 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
5781 if (ret
|| nvl
== NULL
)
5785 (void) printf(gettext("dropped %d events\n"), dropped
);
5787 zpool_do_events_short(nvl
);
5789 if (opts
->verbose
) {
5790 zpool_do_events_nvprint(nvl
, 8);
5791 printf(gettext("\n"));
5793 (void) fflush(stdout
);
5798 VERIFY(0 == close(zevent_fd
));
5804 zpool_do_events_clear(ev_opts_t
*opts
)
5808 ret
= zpool_events_clear(g_zfs
, &count
);
5810 (void) printf(gettext("cleared %d events\n"), count
);
5816 * zpool events [-vfc]
5818 * Displays events logs by ZFS.
5821 zpool_do_events(int argc
, char **argv
)
5823 ev_opts_t opts
= { 0 };
5828 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
5843 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5852 ret
= zpool_do_events_clear(&opts
);
5854 ret
= zpool_do_events_next(&opts
);
5860 get_callback(zpool_handle_t
*zhp
, void *data
)
5862 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5863 char value
[MAXNAMELEN
];
5864 zprop_source_t srctype
;
5867 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5870 * Skip the special fake placeholder. This will also skip
5871 * over the name property when 'all' is specified.
5873 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5874 pl
== cbp
->cb_proplist
)
5877 if (pl
->pl_prop
== ZPROP_INVAL
&&
5878 (zpool_prop_feature(pl
->pl_user_prop
) ||
5879 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5880 srctype
= ZPROP_SRC_LOCAL
;
5882 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5883 value
, sizeof (value
)) == 0) {
5884 zprop_print_one_property(zpool_get_name(zhp
),
5885 cbp
, pl
->pl_user_prop
, value
, srctype
,
5889 if (zpool_get_prop_literal(zhp
, pl
->pl_prop
, value
,
5890 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5893 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5894 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5902 zpool_do_get(int argc
, char **argv
)
5904 zprop_get_cbdata_t cb
= { 0 };
5905 zprop_list_t fake_name
= { 0 };
5909 while ((c
= getopt(argc
, argv
, "pH")) != -1) {
5912 cb
.cb_literal
= B_TRUE
;
5916 cb
.cb_scripted
= B_TRUE
;
5920 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5930 (void) fprintf(stderr
, gettext("missing property "
5935 cb
.cb_first
= B_TRUE
;
5936 cb
.cb_sources
= ZPROP_SRC_ALL
;
5937 cb
.cb_columns
[0] = GET_COL_NAME
;
5938 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5939 cb
.cb_columns
[2] = GET_COL_VALUE
;
5940 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5941 cb
.cb_type
= ZFS_TYPE_POOL
;
5943 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5949 if (cb
.cb_proplist
!= NULL
) {
5950 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5951 fake_name
.pl_width
= strlen(gettext("NAME"));
5952 fake_name
.pl_next
= cb
.cb_proplist
;
5953 cb
.cb_proplist
= &fake_name
;
5956 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5959 if (cb
.cb_proplist
== &fake_name
)
5960 zprop_free_list(fake_name
.pl_next
);
5962 zprop_free_list(cb
.cb_proplist
);
5967 typedef struct set_cbdata
{
5970 boolean_t cb_any_successful
;
5974 set_callback(zpool_handle_t
*zhp
, void *data
)
5977 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5979 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5982 cb
->cb_any_successful
= B_TRUE
;
5988 zpool_do_set(int argc
, char **argv
)
5990 set_cbdata_t cb
= { 0 };
5993 if (argc
> 1 && argv
[1][0] == '-') {
5994 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6000 (void) fprintf(stderr
, gettext("missing property=value "
6006 (void) fprintf(stderr
, gettext("missing pool name\n"));
6011 (void) fprintf(stderr
, gettext("too many pool names\n"));
6015 cb
.cb_propname
= argv
[1];
6016 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
6017 if (cb
.cb_value
== NULL
) {
6018 (void) fprintf(stderr
, gettext("missing value in "
6019 "property=value argument\n"));
6023 *(cb
.cb_value
) = '\0';
6026 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
6033 find_command_idx(char *command
, int *idx
)
6037 for (i
= 0; i
< NCOMMAND
; i
++) {
6038 if (command_table
[i
].name
== NULL
)
6041 if (strcmp(command
, command_table
[i
].name
) == 0) {
6050 main(int argc
, char **argv
)
6056 (void) setlocale(LC_ALL
, "");
6057 (void) textdomain(TEXT_DOMAIN
);
6060 dprintf_setup(&argc
, argv
);
6065 * Make sure the user has specified some command.
6068 (void) fprintf(stderr
, gettext("missing command\n"));
6077 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
6080 if ((g_zfs
= libzfs_init()) == NULL
) {
6081 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
6085 libzfs_print_on_error(g_zfs
, B_TRUE
);
6087 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6090 * Run the appropriate command.
6092 if (find_command_idx(cmdname
, &i
) == 0) {
6093 current_command
= &command_table
[i
];
6094 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6095 } else if (strchr(cmdname
, '=')) {
6096 verify(find_command_idx("set", &i
) == 0);
6097 current_command
= &command_table
[i
];
6098 ret
= command_table
[i
].func(argc
, argv
);
6099 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
6101 * 'freeze' is a vile debugging abomination, so we treat
6105 int fd
= open(ZFS_DEV
, O_RDWR
);
6106 (void) strcpy((void *)buf
, argv
[2]);
6107 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
6109 (void) fprintf(stderr
, gettext("unrecognized "
6110 "command '%s'\n"), cmdname
);
6115 if (ret
== 0 && log_history
)
6116 (void) zpool_log_history(g_zfs
, history_str
);
6121 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6122 * for the purposes of running ::findleaks.
6124 if (getenv("ZFS_ABORT") != NULL
) {
6125 (void) printf("dumping core by request\n");