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.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
50 #include <sys/fs/zfs.h>
52 #include <sys/fm/fs/zfs.h>
53 #include <sys/fm/util.h>
54 #include <sys/fm/protocol.h>
55 #include <sys/zfs_ioctl.h>
61 #include "zpool_util.h"
62 #include "zfs_comutil.h"
63 #include "zfeature_common.h"
65 #include "statcommon.h"
67 static int zpool_do_create(int, char **);
68 static int zpool_do_destroy(int, char **);
70 static int zpool_do_add(int, char **);
71 static int zpool_do_remove(int, char **);
72 static int zpool_do_labelclear(int, char **);
74 static int zpool_do_list(int, char **);
75 static int zpool_do_iostat(int, char **);
76 static int zpool_do_status(int, char **);
78 static int zpool_do_online(int, char **);
79 static int zpool_do_offline(int, char **);
80 static int zpool_do_clear(int, char **);
81 static int zpool_do_reopen(int, char **);
83 static int zpool_do_reguid(int, char **);
85 static int zpool_do_attach(int, char **);
86 static int zpool_do_detach(int, char **);
87 static int zpool_do_replace(int, char **);
88 static int zpool_do_split(int, char **);
90 static int zpool_do_scrub(int, char **);
92 static int zpool_do_import(int, char **);
93 static int zpool_do_export(int, char **);
95 static int zpool_do_upgrade(int, char **);
97 static int zpool_do_history(int, char **);
98 static int zpool_do_events(int, char **);
100 static int zpool_do_get(int, char **);
101 static int zpool_do_set(int, char **);
104 * These libumem hooks provide a reasonable set of defaults for the allocator's
105 * debugging facilities.
110 _umem_debug_init(void)
112 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 _umem_logging_init(void)
118 return ("fail,contents"); /* $UMEM_LOGGING setting */
152 * Flags for stats to display with "zpool iostats"
160 IOS_COUNT
, /* always last element */
163 /* iostat_type entries as bitmasks */
164 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
165 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
166 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
167 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
168 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
170 /* Mask of all the histo bits */
171 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
174 * Lookup table for iostat flags to nvlist names. Basically a list
175 * of all the nvlists a flag requires. Also specifies the order in
176 * which data gets printed in zpool iostat.
178 static const char *vsx_type_to_nvlist
[IOS_COUNT
][11] = {
180 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
181 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
182 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
183 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
184 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
185 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
186 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
187 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
188 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
191 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
192 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
193 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
194 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
197 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
198 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
199 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
200 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
201 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
204 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
205 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
206 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
207 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
208 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
209 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
210 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
211 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
212 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
213 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
219 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
220 * Right now, only one histo bit is ever set at one time, so we can
221 * just do a highbit64(a)
223 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
225 typedef struct zpool_command
{
227 int (*func
)(int, char **);
232 * Master command table. Each ZFS command has a name, associated function, and
233 * usage message. The usage messages need to be internationalized, so we have
234 * to have a function to return the usage message based on a command index.
236 * These commands are organized according to how they are displayed in the usage
237 * message. An empty command (one with a NULL name) indicates an empty line in
238 * the generic usage message.
240 static zpool_command_t command_table
[] = {
241 { "create", zpool_do_create
, HELP_CREATE
},
242 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
244 { "add", zpool_do_add
, HELP_ADD
},
245 { "remove", zpool_do_remove
, HELP_REMOVE
},
247 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
249 { "list", zpool_do_list
, HELP_LIST
},
250 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
251 { "status", zpool_do_status
, HELP_STATUS
},
253 { "online", zpool_do_online
, HELP_ONLINE
},
254 { "offline", zpool_do_offline
, HELP_OFFLINE
},
255 { "clear", zpool_do_clear
, HELP_CLEAR
},
256 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
258 { "attach", zpool_do_attach
, HELP_ATTACH
},
259 { "detach", zpool_do_detach
, HELP_DETACH
},
260 { "replace", zpool_do_replace
, HELP_REPLACE
},
261 { "split", zpool_do_split
, HELP_SPLIT
},
263 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
265 { "import", zpool_do_import
, HELP_IMPORT
},
266 { "export", zpool_do_export
, HELP_EXPORT
},
267 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
268 { "reguid", zpool_do_reguid
, HELP_REGUID
},
270 { "history", zpool_do_history
, HELP_HISTORY
},
271 { "events", zpool_do_events
, HELP_EVENTS
},
273 { "get", zpool_do_get
, HELP_GET
},
274 { "set", zpool_do_set
, HELP_SET
},
277 #define NCOMMAND (ARRAY_SIZE(command_table))
279 static zpool_command_t
*current_command
;
280 static char history_str
[HIS_MAX_RECORD_LEN
];
281 static boolean_t log_history
= B_TRUE
;
282 static uint_t timestamp_fmt
= NODATE
;
285 get_usage(zpool_help_t idx
)
289 return (gettext("\tadd [-fgLnP] [-o property=value] "
290 "<pool> <vdev> ...\n"));
292 return (gettext("\tattach [-f] [-o property=value] "
293 "<pool> <device> <new-device>\n"));
295 return (gettext("\tclear [-nF] <pool> [device]\n"));
297 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
298 "\t [-O file-system-property=value] ... \n"
299 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
301 return (gettext("\tdestroy [-f] <pool>\n"));
303 return (gettext("\tdetach <pool> <device>\n"));
305 return (gettext("\texport [-af] <pool> ...\n"));
307 return (gettext("\thistory [-il] [<pool>] ...\n"));
309 return (gettext("\timport [-d dir] [-D]\n"
310 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
311 "\timport [-o mntopts] [-o property=value] ... \n"
312 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
313 "[-R root] [-F [-n]] -a\n"
314 "\timport [-o mntopts] [-o property=value] ... \n"
315 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
316 "[-R root] [-F [-n]]\n"
317 "\t <pool | id> [newpool]\n"));
319 return (gettext("\tiostat [[[-c [script1,script2,...]"
320 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
321 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
322 " [interval [count]]\n"));
323 case HELP_LABELCLEAR
:
324 return (gettext("\tlabelclear [-f] <vdev>\n"));
326 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
327 "[-T d|u] [pool] ... [interval [count]]\n"));
329 return (gettext("\toffline [-t] <pool> <device> ...\n"));
331 return (gettext("\tonline <pool> <device> ...\n"));
333 return (gettext("\treplace [-f] [-o property=value] "
334 "<pool> <device> [new-device]\n"));
336 return (gettext("\tremove <pool> <device> ...\n"));
338 return (gettext("\treopen <pool>\n"));
340 return (gettext("\tscrub [-s] <pool> ...\n"));
342 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
343 "[-T d|u] [pool] ... [interval [count]]\n"));
345 return (gettext("\tupgrade\n"
347 "\tupgrade [-V version] <-a | pool ...>\n"));
349 return (gettext("\tevents [-vHfc]\n"));
351 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
352 "<\"all\" | property[,...]> <pool> ...\n"));
354 return (gettext("\tset <property=value> <pool> \n"));
356 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
357 "\t [-o property=value] <pool> <newpool> "
358 "[<device> ...]\n"));
360 return (gettext("\treguid <pool>\n"));
369 * Callback routine that will print out a pool property value.
372 print_prop_cb(int prop
, void *cb
)
376 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
378 if (zpool_prop_readonly(prop
))
379 (void) fprintf(fp
, " NO ");
381 (void) fprintf(fp
, " YES ");
383 if (zpool_prop_values(prop
) == NULL
)
384 (void) fprintf(fp
, "-\n");
386 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
392 * Display usage message. If we're inside a command, display only the usage for
393 * that command. Otherwise, iterate over the entire command table and display
394 * a complete usage message.
397 usage(boolean_t requested
)
399 FILE *fp
= requested
? stdout
: stderr
;
401 if (current_command
== NULL
) {
404 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
406 gettext("where 'command' is one of the following:\n\n"));
408 for (i
= 0; i
< NCOMMAND
; i
++) {
409 if (command_table
[i
].name
== NULL
)
410 (void) fprintf(fp
, "\n");
412 (void) fprintf(fp
, "%s",
413 get_usage(command_table
[i
].usage
));
416 (void) fprintf(fp
, gettext("usage:\n"));
417 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
420 if (current_command
!= NULL
&&
421 ((strcmp(current_command
->name
, "set") == 0) ||
422 (strcmp(current_command
->name
, "get") == 0) ||
423 (strcmp(current_command
->name
, "list") == 0))) {
426 gettext("\nthe following properties are supported:\n"));
428 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
429 "PROPERTY", "EDIT", "VALUES");
431 /* Iterate over all properties */
432 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
435 (void) fprintf(fp
, "\t%-15s ", "feature@...");
436 (void) fprintf(fp
, "YES disabled | enabled | active\n");
438 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
439 "appended with a feature name.\nSee zpool-features(5).\n"));
443 * See comments at end of main().
445 if (getenv("ZFS_ABORT") != NULL
) {
446 (void) printf("dumping core by request\n");
450 exit(requested
? 0 : 2);
454 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
455 boolean_t print_logs
, int name_flags
)
462 (void) printf("\t%*s%s\n", indent
, "", name
);
464 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
465 &child
, &children
) != 0)
468 for (c
= 0; c
< children
; c
++) {
469 uint64_t is_log
= B_FALSE
;
471 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
473 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
476 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
477 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
478 B_FALSE
, name_flags
);
484 prop_list_contains_feature(nvlist_t
*proplist
)
487 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
488 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
489 if (zpool_prop_feature(nvpair_name(nvp
)))
496 * Add a property pair (name, string-value) into a property nvlist.
499 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
502 zpool_prop_t prop
= ZPROP_INVAL
;
508 if (*props
== NULL
&&
509 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
510 (void) fprintf(stderr
,
511 gettext("internal error: out of memory\n"));
518 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
520 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
521 !zpool_prop_feature(propname
)) {
522 (void) fprintf(stderr
, gettext("property '%s' is "
523 "not a valid pool property\n"), propname
);
528 * feature@ properties and version should not be specified
531 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
532 nvlist_exists(proplist
, vname
)) ||
533 (prop
== ZPOOL_PROP_VERSION
&&
534 prop_list_contains_feature(proplist
))) {
535 (void) fprintf(stderr
, gettext("'feature@' and "
536 "'version' properties cannot be specified "
542 if (zpool_prop_feature(propname
))
545 normnm
= zpool_prop_to_name(prop
);
547 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
548 normnm
= zfs_prop_to_name(fprop
);
554 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
555 prop
!= ZPOOL_PROP_CACHEFILE
) {
556 (void) fprintf(stderr
, gettext("property '%s' "
557 "specified multiple times\n"), propname
);
561 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
562 (void) fprintf(stderr
, gettext("internal "
563 "error: out of memory\n"));
571 * Set a default property pair (name, string-value) in a property nvlist
574 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
579 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
582 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
586 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
588 * -f Force addition of devices, even if they appear in use
589 * -g Display guid for individual vdev name.
590 * -L Follow links when resolving vdev path name.
591 * -n Do not add the devices, but display the resulting layout if
592 * they were to be added.
593 * -o Set property=value.
594 * -P Display full path for vdev name.
596 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
597 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
601 zpool_do_add(int argc
, char **argv
)
603 boolean_t force
= B_FALSE
;
604 boolean_t dryrun
= B_FALSE
;
612 nvlist_t
*props
= NULL
;
616 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
622 name_flags
|= VDEV_NAME_GUID
;
625 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
631 if ((propval
= strchr(optarg
, '=')) == NULL
) {
632 (void) fprintf(stderr
, gettext("missing "
633 "'=' for -o option\n"));
639 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
640 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
644 name_flags
|= VDEV_NAME_PATH
;
647 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
656 /* get pool name and check number of arguments */
658 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
662 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
671 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
674 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
675 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
681 /* unless manually specified use "ashift" pool property (if set) */
682 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
685 char strval
[ZPOOL_MAXPROPLEN
];
687 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
688 if (src
!= ZPROP_SRC_DEFAULT
) {
689 (void) sprintf(strval
, "%" PRId32
, intval
);
690 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
691 &props
, B_TRUE
) == 0);
695 /* pass off to get_vdev_spec for processing */
696 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
698 if (nvroot
== NULL
) {
704 nvlist_t
*poolnvroot
;
706 uint_t l2children
, c
;
708 boolean_t hadcache
= B_FALSE
;
710 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
713 (void) printf(gettext("would update '%s' to the following "
714 "configuration:\n"), zpool_get_name(zhp
));
716 /* print original main pool and new tree */
717 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
,
719 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
, name_flags
);
721 /* Do the same for the logs */
722 if (num_logs(poolnvroot
) > 0) {
723 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
,
725 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
,
727 } else if (num_logs(nvroot
) > 0) {
728 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
,
732 /* Do the same for the caches */
733 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
734 &l2child
, &l2children
) == 0 && l2children
) {
736 (void) printf(gettext("\tcache\n"));
737 for (c
= 0; c
< l2children
; c
++) {
738 vname
= zpool_vdev_name(g_zfs
, NULL
,
739 l2child
[c
], name_flags
);
740 (void) printf("\t %s\n", vname
);
744 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
745 &l2child
, &l2children
) == 0 && l2children
) {
747 (void) printf(gettext("\tcache\n"));
748 for (c
= 0; c
< l2children
; c
++) {
749 vname
= zpool_vdev_name(g_zfs
, NULL
,
750 l2child
[c
], name_flags
);
751 (void) printf("\t %s\n", vname
);
758 ret
= (zpool_add(zhp
, nvroot
) != 0);
769 * zpool remove <pool> <vdev> ...
771 * Removes the given vdev from the pool. Currently, this supports removing
772 * spares, cache, and log devices from the pool.
775 zpool_do_remove(int argc
, char **argv
)
779 zpool_handle_t
*zhp
= NULL
;
784 /* get pool name and check number of arguments */
786 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
790 (void) fprintf(stderr
, gettext("missing device\n"));
796 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
799 for (i
= 1; i
< argc
; i
++) {
800 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
809 * zpool labelclear [-f] <vdev>
811 * -f Force clearing the label for the vdevs which are members of
812 * the exported or foreign pools.
814 * Verifies that the vdev is not active and zeros out the label information
818 zpool_do_labelclear(int argc
, char **argv
)
820 char vdev
[MAXPATHLEN
];
823 int c
, fd
= -1, ret
= 0;
826 boolean_t inuse
= B_FALSE
;
827 boolean_t force
= B_FALSE
;
830 while ((c
= getopt(argc
, argv
, "f")) != -1) {
836 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
847 (void) fprintf(stderr
, gettext("missing vdev name\n"));
851 (void) fprintf(stderr
, gettext("too many arguments\n"));
856 * Check if we were given absolute path and use it as is.
857 * Otherwise if the provided vdev name doesn't point to a file,
858 * try prepending expected disk paths and partition numbers.
860 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
861 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
864 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
865 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
866 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
870 if (error
|| (stat(vdev
, &st
) != 0)) {
871 (void) fprintf(stderr
, gettext(
872 "failed to find device %s, try specifying absolute "
873 "path instead\n"), argv
[0]);
878 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
879 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
880 vdev
, strerror(errno
));
884 if (ioctl(fd
, BLKFLSBUF
) != 0)
885 (void) fprintf(stderr
, gettext("failed to invalidate "
886 "cache for %s: %s\n"), vdev
, strerror(errno
));
888 if (zpool_read_label(fd
, &config
, NULL
) != 0 || config
== NULL
) {
889 (void) fprintf(stderr
,
890 gettext("failed to check state for %s\n"), vdev
);
895 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
897 (void) fprintf(stderr
,
898 gettext("failed to check state for %s\n"), vdev
);
907 case POOL_STATE_ACTIVE
:
908 case POOL_STATE_SPARE
:
909 case POOL_STATE_L2CACHE
:
910 (void) fprintf(stderr
, gettext(
911 "%s is a member (%s) of pool \"%s\"\n"),
912 vdev
, zpool_pool_state_to_name(state
), name
);
916 case POOL_STATE_EXPORTED
:
919 (void) fprintf(stderr
, gettext(
920 "use '-f' to override the following error:\n"
921 "%s is a member of exported pool \"%s\"\n"),
926 case POOL_STATE_POTENTIALLY_ACTIVE
:
929 (void) fprintf(stderr
, gettext(
930 "use '-f' to override the following error:\n"
931 "%s is a member of potentially active pool \"%s\"\n"),
936 case POOL_STATE_DESTROYED
:
937 /* inuse should never be set for a destroyed pool */
943 ret
= zpool_clear_label(fd
);
945 (void) fprintf(stderr
,
946 gettext("failed to clear label for %s\n"), vdev
);
957 * zpool create [-fnd] [-o property=value] ...
958 * [-O file-system-property=value] ...
959 * [-R root] [-m mountpoint] <pool> <dev> ...
961 * -f Force creation, even if devices appear in use
962 * -n Do not create the pool, but display the resulting layout if it
963 * were to be created.
964 * -R Create a pool under an alternate root
965 * -m Set default mountpoint for the root dataset. By default it's
967 * -o Set property=value.
968 * -o Set feature@feature=enabled|disabled.
969 * -d Don't automatically enable all supported pool features
970 * (individual features can be enabled with -o).
971 * -O Set fsproperty=value in the pool's root file system
973 * Creates the named pool according to the given vdev specification. The
974 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
975 * we get the nvlist back from get_vdev_spec(), we either print out the contents
976 * (if '-n' was specified), or pass it to libzfs to do the creation.
979 zpool_do_create(int argc
, char **argv
)
981 boolean_t force
= B_FALSE
;
982 boolean_t dryrun
= B_FALSE
;
983 boolean_t enable_all_pool_feat
= B_TRUE
;
985 nvlist_t
*nvroot
= NULL
;
989 char *altroot
= NULL
;
990 char *mountpoint
= NULL
;
991 nvlist_t
*fsprops
= NULL
;
992 nvlist_t
*props
= NULL
;
996 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1005 enable_all_pool_feat
= B_FALSE
;
1009 if (add_prop_list(zpool_prop_to_name(
1010 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1012 if (add_prop_list_default(zpool_prop_to_name(
1013 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1017 /* Equivalent to -O mountpoint=optarg */
1018 mountpoint
= optarg
;
1021 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1022 (void) fprintf(stderr
, gettext("missing "
1023 "'=' for -o option\n"));
1029 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1033 * If the user is creating a pool that doesn't support
1034 * feature flags, don't enable any features.
1036 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1040 ver
= strtoull(propval
, &end
, 10);
1042 ver
< SPA_VERSION_FEATURES
) {
1043 enable_all_pool_feat
= B_FALSE
;
1046 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1050 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1051 (void) fprintf(stderr
, gettext("missing "
1052 "'=' for -O option\n"));
1059 * Mountpoints are checked and then added later.
1060 * Uniquely among properties, they can be specified
1061 * more than once, to avoid conflict with -m.
1063 if (0 == strcmp(optarg
,
1064 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1065 mountpoint
= propval
;
1066 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1073 * Sanity check temporary pool name.
1075 if (strchr(optarg
, '/') != NULL
) {
1076 (void) fprintf(stderr
, gettext("cannot create "
1077 "'%s': invalid character '/' in temporary "
1079 (void) fprintf(stderr
, gettext("use 'zfs "
1080 "create' to create a dataset\n"));
1084 if (add_prop_list(zpool_prop_to_name(
1085 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1087 if (add_prop_list_default(zpool_prop_to_name(
1088 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1093 (void) fprintf(stderr
, gettext("missing argument for "
1094 "'%c' option\n"), optopt
);
1097 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1106 /* get pool name and check number of arguments */
1108 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1112 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1119 * As a special case, check for use of '/' in the name, and direct the
1120 * user to use 'zfs create' instead.
1122 if (strchr(poolname
, '/') != NULL
) {
1123 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1124 "character '/' in pool name\n"), poolname
);
1125 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1126 "create a dataset\n"));
1130 /* pass off to get_vdev_spec for bulk processing */
1131 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1132 argc
- 1, argv
+ 1);
1136 /* make_root_vdev() allows 0 toplevel children if there are spares */
1137 if (!zfs_allocatable_devs(nvroot
)) {
1138 (void) fprintf(stderr
, gettext("invalid vdev "
1139 "specification: at least one toplevel vdev must be "
1144 if (altroot
!= NULL
&& altroot
[0] != '/') {
1145 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1146 "must be an absolute path\n"), altroot
);
1151 * Check the validity of the mountpoint and direct the user to use the
1152 * '-m' mountpoint option if it looks like its in use.
1154 if (mountpoint
== NULL
||
1155 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1156 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1157 char buf
[MAXPATHLEN
];
1160 if (mountpoint
&& mountpoint
[0] != '/') {
1161 (void) fprintf(stderr
, gettext("invalid mountpoint "
1162 "'%s': must be an absolute path, 'legacy', or "
1163 "'none'\n"), mountpoint
);
1167 if (mountpoint
== NULL
) {
1168 if (altroot
!= NULL
)
1169 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1172 (void) snprintf(buf
, sizeof (buf
), "/%s",
1175 if (altroot
!= NULL
)
1176 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1177 altroot
, mountpoint
);
1179 (void) snprintf(buf
, sizeof (buf
), "%s",
1183 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1184 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1185 "%s\n"), buf
, strerror(errno
));
1186 (void) fprintf(stderr
, gettext("use '-m' "
1187 "option to provide a different default\n"));
1192 while (count
< 3 && readdir(dirp
) != NULL
)
1194 (void) closedir(dirp
);
1197 (void) fprintf(stderr
, gettext("mountpoint "
1198 "'%s' exists and is not empty\n"), buf
);
1199 (void) fprintf(stderr
, gettext("use '-m' "
1200 "option to provide a "
1201 "different default\n"));
1208 * Now that the mountpoint's validity has been checked, ensure that
1209 * the property is set appropriately prior to creating the pool.
1211 if (mountpoint
!= NULL
) {
1212 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1213 mountpoint
, &fsprops
, B_FALSE
);
1221 * For a dry run invocation, print out a basic message and run
1222 * through all the vdevs in the list and print out in an
1223 * appropriate hierarchy.
1225 (void) printf(gettext("would create '%s' with the "
1226 "following layout:\n\n"), poolname
);
1228 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1229 if (num_logs(nvroot
) > 0)
1230 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1235 * Hand off to libzfs.
1238 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1239 char propname
[MAXPATHLEN
];
1241 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1243 (void) snprintf(propname
, sizeof (propname
),
1244 "feature@%s", feat
->fi_uname
);
1247 * Only features contained in props will be enabled:
1248 * remove from the nvlist every ZFS_FEATURE_DISABLED
1249 * value and add every missing ZFS_FEATURE_ENABLED if
1250 * enable_all_pool_feat is set.
1252 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1253 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1254 (void) nvlist_remove_all(props
,
1256 } else if (enable_all_pool_feat
) {
1257 ret
= add_prop_list(propname
,
1258 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1265 if (zpool_create(g_zfs
, poolname
,
1266 nvroot
, props
, fsprops
) == 0) {
1267 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1268 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1270 if (zfs_mount(pool
, NULL
, 0) == 0)
1271 ret
= zfs_shareall(pool
);
1274 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1275 (void) fprintf(stderr
, gettext("pool name may have "
1281 nvlist_free(nvroot
);
1282 nvlist_free(fsprops
);
1286 nvlist_free(fsprops
);
1293 * zpool destroy <pool>
1295 * -f Forcefully unmount any datasets
1297 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1300 zpool_do_destroy(int argc
, char **argv
)
1302 boolean_t force
= B_FALSE
;
1305 zpool_handle_t
*zhp
;
1309 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1315 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1324 /* check arguments */
1326 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1330 (void) fprintf(stderr
, gettext("too many arguments\n"));
1336 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1338 * As a special case, check for use of '/' in the name, and
1339 * direct the user to use 'zfs destroy' instead.
1341 if (strchr(pool
, '/') != NULL
)
1342 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1343 "destroy a dataset\n"));
1347 if (zpool_disable_datasets(zhp
, force
) != 0) {
1348 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1349 "could not unmount datasets\n"), zpool_get_name(zhp
));
1354 /* The history must be logged as part of the export */
1355 log_history
= B_FALSE
;
1357 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1364 typedef struct export_cbdata
{
1366 boolean_t hardforce
;
1373 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1375 export_cbdata_t
*cb
= data
;
1377 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1380 /* The history must be logged as part of the export */
1381 log_history
= B_FALSE
;
1383 if (cb
->hardforce
) {
1384 if (zpool_export_force(zhp
, history_str
) != 0)
1386 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1394 * zpool export [-f] <pool> ...
1396 * -a Export all pools
1397 * -f Forcefully unmount datasets
1399 * Export the given pools. By default, the command will attempt to cleanly
1400 * unmount any active datasets within the pool. If the '-f' flag is specified,
1401 * then the datasets will be forcefully unmounted.
1404 zpool_do_export(int argc
, char **argv
)
1407 boolean_t do_all
= B_FALSE
;
1408 boolean_t force
= B_FALSE
;
1409 boolean_t hardforce
= B_FALSE
;
1413 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1425 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1432 cb
.hardforce
= hardforce
;
1438 (void) fprintf(stderr
, gettext("too many arguments\n"));
1442 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1443 zpool_export_one
, &cb
));
1446 /* check arguments */
1448 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1452 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1458 * Given a vdev configuration, determine the maximum width needed for the device
1462 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1470 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1471 if (strlen(name
) + depth
> max
)
1472 max
= strlen(name
) + depth
;
1476 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1477 &child
, &children
) == 0) {
1478 for (c
= 0; c
< children
; c
++)
1479 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1480 max
, name_flags
)) > max
)
1484 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1485 &child
, &children
) == 0) {
1486 for (c
= 0; c
< children
; c
++)
1487 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1488 max
, name_flags
)) > max
)
1492 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1493 &child
, &children
) == 0) {
1494 for (c
= 0; c
< children
; c
++)
1495 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1496 max
, name_flags
)) > max
)
1503 typedef struct spare_cbdata
{
1505 zpool_handle_t
*cb_zhp
;
1509 find_vdev(nvlist_t
*nv
, uint64_t search
)
1515 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1519 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1520 &child
, &children
) == 0) {
1521 for (c
= 0; c
< children
; c
++)
1522 if (find_vdev(child
[c
], search
))
1530 find_spare(zpool_handle_t
*zhp
, void *data
)
1532 spare_cbdata_t
*cbp
= data
;
1533 nvlist_t
*config
, *nvroot
;
1535 config
= zpool_get_config(zhp
, NULL
);
1536 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1539 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1548 typedef struct status_cbdata
{
1552 boolean_t cb_allpools
;
1553 boolean_t cb_verbose
;
1554 boolean_t cb_explain
;
1556 boolean_t cb_dedup_stats
;
1557 boolean_t cb_print_status
;
1558 vdev_cmd_data_list_t
*vcdl
;
1561 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1563 is_blank_str(char *str
)
1565 while (str
!= NULL
&& *str
!= '\0') {
1573 /* Print command output lines for specific vdev in a specific pool */
1575 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1577 vdev_cmd_data_t
*data
;
1581 for (i
= 0; i
< vcdl
->count
; i
++) {
1582 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1583 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1584 /* Not the vdev we're looking for */
1588 data
= &vcdl
->data
[i
];
1589 /* Print out all the output values for this vdev */
1590 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1592 /* Does this vdev have values for this column? */
1593 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1594 if (strcmp(data
->cols
[k
],
1595 vcdl
->uniq_cols
[j
]) == 0) {
1596 /* yes it does, record the value */
1597 val
= data
->lines
[k
];
1602 * Mark empty values with dashes to make output
1605 if (is_blank_str(val
))
1608 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1609 if (j
< vcdl
->uniq_cols_cnt
- 1)
1613 /* Print out any values that aren't in a column at the end */
1614 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1615 /* Did we have any columns? If so print a spacer. */
1616 if (vcdl
->uniq_cols_cnt
> 0)
1619 val
= data
->lines
[j
];
1620 printf("%s", val
? val
: "");
1627 * Print out configuration state as requested by status_callback.
1630 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1631 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1635 pool_scan_stat_t
*ps
= NULL
;
1637 char rbuf
[6], wbuf
[6], cbuf
[6];
1639 uint64_t notpresent
;
1640 spare_cbdata_t spare_cb
;
1644 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1645 &child
, &children
) != 0)
1648 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1649 (uint64_t **)&vs
, &c
) == 0);
1651 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1654 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1657 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1659 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1663 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1667 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1668 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1669 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1670 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1673 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1674 ¬present
) == 0) {
1675 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1676 (void) printf(" was %s", path
);
1677 } else if (vs
->vs_aux
!= 0) {
1680 switch (vs
->vs_aux
) {
1681 case VDEV_AUX_OPEN_FAILED
:
1682 (void) printf(gettext("cannot open"));
1685 case VDEV_AUX_BAD_GUID_SUM
:
1686 (void) printf(gettext("missing device"));
1689 case VDEV_AUX_NO_REPLICAS
:
1690 (void) printf(gettext("insufficient replicas"));
1693 case VDEV_AUX_VERSION_NEWER
:
1694 (void) printf(gettext("newer version"));
1697 case VDEV_AUX_UNSUP_FEAT
:
1698 (void) printf(gettext("unsupported feature(s)"));
1701 case VDEV_AUX_SPARED
:
1702 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1703 &spare_cb
.cb_guid
) == 0);
1704 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1705 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1706 zpool_get_name(zhp
)) == 0)
1707 (void) printf(gettext("currently in "
1710 (void) printf(gettext("in use by "
1712 zpool_get_name(spare_cb
.cb_zhp
));
1713 zpool_close(spare_cb
.cb_zhp
);
1715 (void) printf(gettext("currently in use"));
1719 case VDEV_AUX_ERR_EXCEEDED
:
1720 (void) printf(gettext("too many errors"));
1723 case VDEV_AUX_IO_FAILURE
:
1724 (void) printf(gettext("experienced I/O failures"));
1727 case VDEV_AUX_BAD_LOG
:
1728 (void) printf(gettext("bad intent log"));
1731 case VDEV_AUX_EXTERNAL
:
1732 (void) printf(gettext("external device fault"));
1735 case VDEV_AUX_SPLIT_POOL
:
1736 (void) printf(gettext("split into new pool"));
1740 (void) printf(gettext("corrupted data"));
1745 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1746 (uint64_t **)&ps
, &c
);
1748 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1749 vs
->vs_scan_processed
!= 0 && children
== 0) {
1750 (void) printf(gettext(" (%s)"),
1751 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1752 "resilvering" : "repairing");
1755 if (cb
->vcdl
!= NULL
) {
1756 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1758 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1762 (void) printf("\n");
1764 for (c
= 0; c
< children
; c
++) {
1765 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1767 /* Don't print logs or holes here */
1768 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1770 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1772 if (islog
|| ishole
)
1774 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1775 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1776 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1783 * Print the configuration of an exported pool. Iterate over all vdevs in the
1784 * pool, printing out the name and status for each one.
1787 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1795 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1796 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1797 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1800 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1801 (uint64_t **)&vs
, &c
) == 0);
1803 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1804 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1806 if (vs
->vs_aux
!= 0) {
1809 switch (vs
->vs_aux
) {
1810 case VDEV_AUX_OPEN_FAILED
:
1811 (void) printf(gettext("cannot open"));
1814 case VDEV_AUX_BAD_GUID_SUM
:
1815 (void) printf(gettext("missing device"));
1818 case VDEV_AUX_NO_REPLICAS
:
1819 (void) printf(gettext("insufficient replicas"));
1822 case VDEV_AUX_VERSION_NEWER
:
1823 (void) printf(gettext("newer version"));
1826 case VDEV_AUX_UNSUP_FEAT
:
1827 (void) printf(gettext("unsupported feature(s)"));
1830 case VDEV_AUX_ERR_EXCEEDED
:
1831 (void) printf(gettext("too many errors"));
1835 (void) printf(gettext("corrupted data"));
1839 (void) printf("\n");
1841 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1842 &child
, &children
) != 0)
1845 for (c
= 0; c
< children
; c
++) {
1846 uint64_t is_log
= B_FALSE
;
1848 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1853 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1854 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1855 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
1859 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1860 &child
, &children
) == 0) {
1861 (void) printf(gettext("\tcache\n"));
1862 for (c
= 0; c
< children
; c
++) {
1863 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1865 (void) printf("\t %s\n", vname
);
1870 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1871 &child
, &children
) == 0) {
1872 (void) printf(gettext("\tspares\n"));
1873 for (c
= 0; c
< children
; c
++) {
1874 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1876 (void) printf("\t %s\n", vname
);
1884 * Logs are recorded as top level vdevs in the main pool child array
1885 * but with "is_log" set to 1. We use either print_status_config() or
1886 * print_import_config() to print the top level logs then any log
1887 * children (eg mirrored slogs) are printed recursively - which
1888 * works because only the top level vdev is marked "is_log"
1891 print_logs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
)
1896 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1900 (void) printf(gettext("\tlogs\n"));
1902 for (c
= 0; c
< children
; c
++) {
1903 uint64_t is_log
= B_FALSE
;
1906 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1910 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1911 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1912 if (cb
->cb_print_status
)
1913 print_status_config(zhp
, cb
, name
, child
[c
], 2,
1916 print_import_config(cb
, name
, child
[c
], 2);
1922 * Display the status for the given pool.
1925 show_import(nvlist_t
*config
)
1927 uint64_t pool_state
;
1933 zpool_status_t reason
;
1934 zpool_errata_t errata
;
1938 status_cbdata_t cb
= { 0 };
1940 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1942 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1944 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1946 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1949 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1950 (uint64_t **)&vs
, &vsc
) == 0);
1951 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1953 reason
= zpool_import_status(config
, &msgid
, &errata
);
1955 (void) printf(gettext(" pool: %s\n"), name
);
1956 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1957 (void) printf(gettext(" state: %s"), health
);
1958 if (pool_state
== POOL_STATE_DESTROYED
)
1959 (void) printf(gettext(" (DESTROYED)"));
1960 (void) printf("\n");
1963 case ZPOOL_STATUS_MISSING_DEV_R
:
1964 case ZPOOL_STATUS_MISSING_DEV_NR
:
1965 case ZPOOL_STATUS_BAD_GUID_SUM
:
1966 (void) printf(gettext(" status: One or more devices are "
1967 "missing from the system.\n"));
1970 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1971 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1972 (void) printf(gettext(" status: One or more devices contains "
1973 "corrupted data.\n"));
1976 case ZPOOL_STATUS_CORRUPT_DATA
:
1978 gettext(" status: The pool data is corrupted.\n"));
1981 case ZPOOL_STATUS_OFFLINE_DEV
:
1982 (void) printf(gettext(" status: One or more devices "
1983 "are offlined.\n"));
1986 case ZPOOL_STATUS_CORRUPT_POOL
:
1987 (void) printf(gettext(" status: The pool metadata is "
1991 case ZPOOL_STATUS_VERSION_OLDER
:
1992 (void) printf(gettext(" status: The pool is formatted using a "
1993 "legacy on-disk version.\n"));
1996 case ZPOOL_STATUS_VERSION_NEWER
:
1997 (void) printf(gettext(" status: The pool is formatted using an "
1998 "incompatible version.\n"));
2001 case ZPOOL_STATUS_FEAT_DISABLED
:
2002 (void) printf(gettext(" status: Some supported features are "
2003 "not enabled on the pool.\n"));
2006 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2007 (void) printf(gettext("status: The pool uses the following "
2008 "feature(s) not supported on this system:\n"));
2009 zpool_print_unsup_feat(config
);
2012 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2013 (void) printf(gettext("status: The pool can only be accessed "
2014 "in read-only mode on this system. It\n\tcannot be "
2015 "accessed in read-write mode because it uses the "
2016 "following\n\tfeature(s) not supported on this system:\n"));
2017 zpool_print_unsup_feat(config
);
2020 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2021 (void) printf(gettext(" status: The pool was last accessed by "
2022 "another system.\n"));
2025 case ZPOOL_STATUS_FAULTED_DEV_R
:
2026 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2027 (void) printf(gettext(" status: One or more devices are "
2031 case ZPOOL_STATUS_BAD_LOG
:
2032 (void) printf(gettext(" status: An intent log record cannot be "
2036 case ZPOOL_STATUS_RESILVERING
:
2037 (void) printf(gettext(" status: One or more devices were being "
2041 case ZPOOL_STATUS_ERRATA
:
2042 (void) printf(gettext(" status: Errata #%d detected.\n"),
2048 * No other status can be seen when importing pools.
2050 assert(reason
== ZPOOL_STATUS_OK
);
2054 * Print out an action according to the overall state of the pool.
2056 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2057 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2058 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2059 (void) printf(gettext(" action: The pool can be "
2060 "imported using its name or numeric identifier, "
2061 "though\n\tsome features will not be available "
2062 "without an explicit 'zpool upgrade'.\n"));
2063 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2064 (void) printf(gettext(" action: The pool can be "
2065 "imported using its name or numeric "
2066 "identifier and\n\tthe '-f' flag.\n"));
2067 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2069 case ZPOOL_ERRATA_NONE
:
2072 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2073 (void) printf(gettext(" action: The pool can "
2074 "be imported using its name or numeric "
2075 "identifier,\n\thowever there is a compat"
2076 "ibility issue which should be corrected"
2077 "\n\tby running 'zpool scrub'\n"));
2080 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2081 (void) printf(gettext(" action: The pool can"
2082 "not be imported with this version of ZFS "
2083 "due to\n\tan active asynchronous destroy. "
2084 "Revert to an earlier version\n\tand "
2085 "allow the destroy to complete before "
2091 * All errata must contain an action message.
2096 (void) printf(gettext(" action: The pool can be "
2097 "imported using its name or numeric "
2100 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2101 (void) printf(gettext(" action: The pool can be imported "
2102 "despite missing or damaged devices. The\n\tfault "
2103 "tolerance of the pool may be compromised if imported.\n"));
2106 case ZPOOL_STATUS_VERSION_NEWER
:
2107 (void) printf(gettext(" action: The pool cannot be "
2108 "imported. Access the pool on a system running "
2109 "newer\n\tsoftware, or recreate the pool from "
2112 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2113 (void) printf(gettext("action: The pool cannot be "
2114 "imported. Access the pool on a system that "
2115 "supports\n\tthe required feature(s), or recreate "
2116 "the pool from backup.\n"));
2118 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2119 (void) printf(gettext("action: The pool cannot be "
2120 "imported in read-write mode. Import the pool "
2122 "\t\"-o readonly=on\", access the pool on a system "
2123 "that supports the\n\trequired feature(s), or "
2124 "recreate the pool from backup.\n"));
2126 case ZPOOL_STATUS_MISSING_DEV_R
:
2127 case ZPOOL_STATUS_MISSING_DEV_NR
:
2128 case ZPOOL_STATUS_BAD_GUID_SUM
:
2129 (void) printf(gettext(" action: The pool cannot be "
2130 "imported. Attach the missing\n\tdevices and try "
2134 (void) printf(gettext(" action: The pool cannot be "
2135 "imported due to damaged devices or data.\n"));
2139 /* Print the comment attached to the pool. */
2140 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2141 (void) printf(gettext("comment: %s\n"), comment
);
2144 * If the state is "closed" or "can't open", and the aux state
2145 * is "corrupt data":
2147 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2148 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2149 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2150 if (pool_state
== POOL_STATE_DESTROYED
)
2151 (void) printf(gettext("\tThe pool was destroyed, "
2152 "but can be imported using the '-Df' flags.\n"));
2153 else if (pool_state
!= POOL_STATE_EXPORTED
)
2154 (void) printf(gettext("\tThe pool may be active on "
2155 "another system, but can be imported using\n\t"
2156 "the '-f' flag.\n"));
2160 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2163 (void) printf(gettext(" config:\n\n"));
2165 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, 0, VDEV_NAME_TYPE_ID
);
2166 if (cb
.cb_namewidth
< 10)
2167 cb
.cb_namewidth
= 10;
2169 print_import_config(&cb
, name
, nvroot
, 0);
2170 if (num_logs(nvroot
) > 0)
2171 print_logs(NULL
, &cb
, nvroot
);
2173 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2174 (void) printf(gettext("\n\tAdditional devices are known to "
2175 "be part of this pool, though their\n\texact "
2176 "configuration cannot be determined.\n"));
2181 * Perform the import for the given configuration. This passes the heavy
2182 * lifting off to zpool_import_props(), and then mounts the datasets contained
2186 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2187 nvlist_t
*props
, int flags
)
2189 zpool_handle_t
*zhp
;
2194 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2197 verify(nvlist_lookup_uint64(config
,
2198 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
2199 verify(nvlist_lookup_uint64(config
,
2200 ZPOOL_CONFIG_VERSION
, &version
) == 0);
2201 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2202 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2203 "is formatted using an unsupported ZFS version\n"), name
);
2205 } else if (state
!= POOL_STATE_EXPORTED
&&
2206 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2207 uint64_t hostid
= 0;
2208 unsigned long system_hostid
= get_system_hostid();
2210 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
2213 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
) {
2218 verify(nvlist_lookup_string(config
,
2219 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2220 verify(nvlist_lookup_uint64(config
,
2221 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
2223 (void) fprintf(stderr
, gettext("cannot import "
2224 "'%s': pool may be in use from other "
2225 "system, it was last accessed by %s "
2226 "(hostid: 0x%lx) on %s"), name
, hostname
,
2227 (unsigned long)hostid
,
2228 asctime(localtime(&t
)));
2229 (void) fprintf(stderr
, gettext("use '-f' to "
2230 "import anyway\n"));
2235 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2238 if (newname
!= NULL
)
2239 name
= (char *)newname
;
2241 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2244 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2245 !(flags
& ZFS_IMPORT_ONLY
) &&
2246 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2256 * zpool import [-d dir] [-D]
2257 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2258 * [-d dir | -c cachefile] [-f] -a
2259 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2260 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2262 * -c Read pool information from a cachefile instead of searching
2265 * -d Scan in a specific directory, other than /dev/. More than
2266 * one directory can be specified using multiple '-d' options.
2268 * -D Scan for previously destroyed pools or import all or only
2269 * specified destroyed pools.
2271 * -R Temporarily import the pool, with all mountpoints relative to
2272 * the given root. The pool will remain exported when the machine
2275 * -V Import even in the presence of faulted vdevs. This is an
2276 * intentionally undocumented option for testing purposes, and
2277 * treats the pool configuration as complete, leaving any bad
2278 * vdevs in the FAULTED state. In other words, it does verbatim
2281 * -f Force import, even if it appears that the pool is active.
2283 * -F Attempt rewind if necessary.
2285 * -n See if rewind would work, but don't actually rewind.
2287 * -N Import the pool but don't mount datasets.
2289 * -T Specify a starting txg to use for import. This option is
2290 * intentionally undocumented option for testing purposes.
2292 * -a Import all pools found.
2294 * -o Set property=value and/or temporary mount options (without '=').
2296 * -s Scan using the default search path, the libblkid cache will
2299 * The import command scans for pools to import, and import pools based on pool
2300 * name and GUID. The pool can also be renamed as part of the import process.
2303 zpool_do_import(int argc
, char **argv
)
2305 char **searchdirs
= NULL
;
2306 char *env
, *envdup
= NULL
;
2310 nvlist_t
*pools
= NULL
;
2311 boolean_t do_all
= B_FALSE
;
2312 boolean_t do_destroyed
= B_FALSE
;
2313 char *mntopts
= NULL
;
2316 uint64_t searchguid
= 0;
2317 char *searchname
= NULL
;
2319 nvlist_t
*found_config
;
2320 nvlist_t
*policy
= NULL
;
2321 nvlist_t
*props
= NULL
;
2323 int flags
= ZFS_IMPORT_NORMAL
;
2324 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2325 boolean_t dryrun
= B_FALSE
;
2326 boolean_t do_rewind
= B_FALSE
;
2327 boolean_t xtreme_rewind
= B_FALSE
;
2328 boolean_t do_scan
= B_FALSE
;
2329 uint64_t pool_state
, txg
= -1ULL;
2330 char *cachefile
= NULL
;
2331 importargs_t idata
= { 0 };
2335 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2344 if (searchdirs
== NULL
) {
2345 searchdirs
= safe_malloc(sizeof (char *));
2347 char **tmp
= safe_malloc((nsearch
+ 1) *
2349 bcopy(searchdirs
, tmp
, nsearch
*
2354 searchdirs
[nsearch
++] = optarg
;
2357 do_destroyed
= B_TRUE
;
2360 flags
|= ZFS_IMPORT_ANY_HOST
;
2366 flags
|= ZFS_IMPORT_MISSING_LOG
;
2372 flags
|= ZFS_IMPORT_ONLY
;
2375 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2378 if (add_prop_list(optarg
, propval
,
2386 if (add_prop_list(zpool_prop_to_name(
2387 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2389 if (add_prop_list_default(zpool_prop_to_name(
2390 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2397 flags
|= ZFS_IMPORT_TEMP_NAME
;
2398 if (add_prop_list_default(zpool_prop_to_name(
2399 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2405 txg
= strtoull(optarg
, &endptr
, 0);
2406 if (errno
!= 0 || *endptr
!= '\0') {
2407 (void) fprintf(stderr
,
2408 gettext("invalid txg value\n"));
2411 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2414 flags
|= ZFS_IMPORT_VERBATIM
;
2417 xtreme_rewind
= B_TRUE
;
2420 (void) fprintf(stderr
, gettext("missing argument for "
2421 "'%c' option\n"), optopt
);
2425 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2434 if (cachefile
&& nsearch
!= 0) {
2435 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2439 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2440 (void) fprintf(stderr
,
2441 gettext("-n or -X only meaningful with -F\n"));
2445 rewind_policy
= ZPOOL_TRY_REWIND
;
2447 rewind_policy
= ZPOOL_DO_REWIND
;
2449 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2451 /* In the future, we can capture further policy and include it here */
2452 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2453 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2454 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2457 /* check argument count */
2460 (void) fprintf(stderr
, gettext("too many arguments\n"));
2465 (void) fprintf(stderr
, gettext("too many arguments\n"));
2471 * Check for the effective uid. We do this explicitly here because
2472 * otherwise any attempt to discover pools will silently fail.
2474 if (argc
== 0 && geteuid() != 0) {
2475 (void) fprintf(stderr
, gettext("cannot "
2476 "discover pools: permission denied\n"));
2477 if (searchdirs
!= NULL
)
2481 nvlist_free(policy
);
2486 * Depending on the arguments given, we do one of the following:
2488 * <none> Iterate through all pools and display information about
2491 * -a Iterate through all pools and try to import each one.
2493 * <id> Find the pool that corresponds to the given GUID/pool
2494 * name and import that one.
2496 * -D Above options applies only to destroyed pools.
2502 searchguid
= strtoull(argv
[0], &endptr
, 10);
2503 if (errno
!= 0 || *endptr
!= '\0') {
2504 searchname
= argv
[0];
2507 found_config
= NULL
;
2510 * User specified a name or guid. Ensure it's unique.
2512 idata
.unique
= B_TRUE
;
2516 * Check the environment for the preferred search path.
2518 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2521 envdup
= strdup(env
);
2523 dir
= strtok(envdup
, ":");
2524 while (dir
!= NULL
) {
2525 if (searchdirs
== NULL
) {
2526 searchdirs
= safe_malloc(sizeof (char *));
2528 char **tmp
= safe_malloc((nsearch
+ 1) *
2530 bcopy(searchdirs
, tmp
, nsearch
*
2535 searchdirs
[nsearch
++] = dir
;
2536 dir
= strtok(NULL
, ":");
2540 idata
.path
= searchdirs
;
2541 idata
.paths
= nsearch
;
2542 idata
.poolname
= searchname
;
2543 idata
.guid
= searchguid
;
2544 idata
.cachefile
= cachefile
;
2545 idata
.scan
= do_scan
;
2548 * Under Linux the zpool_find_import_impl() function leverages the
2549 * taskq implementation to parallelize device scanning. It is
2550 * therefore necessary to initialize this functionality for the
2551 * duration of the zpool_search_import() function.
2554 pools
= zpool_search_import(g_zfs
, &idata
);
2557 if (pools
!= NULL
&& idata
.exists
&&
2558 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2559 (void) fprintf(stderr
, gettext("cannot import '%s': "
2560 "a pool with that name already exists\n"),
2562 (void) fprintf(stderr
, gettext("use the form '%s "
2563 "<pool | id> <newpool>' to give it a new name\n"),
2566 } else if (pools
== NULL
&& idata
.exists
) {
2567 (void) fprintf(stderr
, gettext("cannot import '%s': "
2568 "a pool with that name is already created/imported,\n"),
2570 (void) fprintf(stderr
, gettext("and no additional pools "
2571 "with that name were found\n"));
2573 } else if (pools
== NULL
) {
2575 (void) fprintf(stderr
, gettext("cannot import '%s': "
2576 "no such pool available\n"), argv
[0]);
2582 if (searchdirs
!= NULL
)
2586 nvlist_free(policy
);
2593 * At this point we have a list of import candidate configs. Even if
2594 * we were searching by pool name or guid, we still need to
2595 * post-process the list to deal with pool state and possible
2601 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2603 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2605 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2607 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2609 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2612 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2619 (void) printf("\n");
2622 err
|= do_import(config
, NULL
, mntopts
,
2625 show_import(config
);
2627 } else if (searchname
!= NULL
) {
2631 * We are searching for a pool based on name.
2633 verify(nvlist_lookup_string(config
,
2634 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2636 if (strcmp(name
, searchname
) == 0) {
2637 if (found_config
!= NULL
) {
2638 (void) fprintf(stderr
, gettext(
2639 "cannot import '%s': more than "
2640 "one matching pool\n"), searchname
);
2641 (void) fprintf(stderr
, gettext(
2642 "import by numeric ID instead\n"));
2645 found_config
= config
;
2651 * Search for a pool by guid.
2653 verify(nvlist_lookup_uint64(config
,
2654 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2656 if (guid
== searchguid
)
2657 found_config
= config
;
2662 * If we were searching for a specific pool, verify that we found a
2663 * pool, and then do the import.
2665 if (argc
!= 0 && err
== 0) {
2666 if (found_config
== NULL
) {
2667 (void) fprintf(stderr
, gettext("cannot import '%s': "
2668 "no such pool available\n"), argv
[0]);
2671 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2672 argv
[1], mntopts
, props
, flags
);
2677 * If we were just looking for pools, report an error if none were
2680 if (argc
== 0 && first
)
2681 (void) fprintf(stderr
,
2682 gettext("no pools available to import\n"));
2687 nvlist_free(policy
);
2688 if (searchdirs
!= NULL
)
2693 return (err
? 1 : 0);
2696 typedef struct iostat_cbdata
{
2701 char **cb_vdev_names
; /* Only show these vdevs */
2702 unsigned int cb_vdev_names_count
;
2703 boolean_t cb_verbose
;
2704 boolean_t cb_literal
;
2705 boolean_t cb_scripted
;
2706 zpool_list_t
*cb_list
;
2707 vdev_cmd_data_list_t
*vcdl
;
2711 typedef struct name_and_columns
{
2712 const char *name
; /* Column name */
2713 unsigned int columns
; /* Center name to this number of columns */
2714 } name_and_columns_t
;
2716 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2718 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
2720 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2722 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2723 {"asyncq_wait", 2}, {"scrub"}},
2724 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
2725 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2727 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
2728 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
2729 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
2730 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
2734 /* Shorthand - if "columns" field not set, default to 1 column */
2735 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
2737 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2739 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2740 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
2741 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2742 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
2743 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2744 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
2745 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2746 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
2749 static const char *histo_to_title
[] = {
2750 [IOS_L_HISTO
] = "latency",
2751 [IOS_RQ_HISTO
] = "req_size",
2755 * Return the number of labels in a null-terminated name_and_columns_t
2760 label_array_len(const name_and_columns_t
*labels
)
2764 while (labels
[i
].name
)
2771 * Return the number of strings in a null-terminated string array.
2774 * const char foo[] = {"bar", "baz", NULL}
2779 str_array_len(const char *array
[])
2790 * Return a default column width for default/latency/queue columns. This does
2791 * not include histograms, which have their columns autosized.
2794 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
2796 unsigned long column_width
= 5; /* Normal niceprint */
2797 static unsigned long widths
[] = {
2799 * Choose some sane default column sizes for printing the
2802 [IOS_DEFAULT
] = 15, /* 1PB capacity */
2803 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
2804 [IOS_QUEUES
] = 6, /* 1M queue entries */
2808 column_width
= widths
[type
];
2810 return (column_width
);
2814 * Print the column labels, i.e:
2816 * capacity operations bandwidth
2817 * alloc free read write read write ...
2819 * If force_column_width is set, use it for the column width. If not set, use
2820 * the default column width.
2823 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2824 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
2827 unsigned int text_start
, rw_column_width
, spaces_to_end
;
2828 uint64_t flags
= cb
->cb_flags
;
2830 unsigned int column_width
= force_column_width
;
2832 /* For each bit set in flags */
2833 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
2834 idx
= lowbit64(f
) - 1;
2835 if (!force_column_width
)
2836 column_width
= default_column_width(cb
, idx
);
2837 /* Print our top labels centered over "read write" label. */
2838 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
2839 const char *name
= labels
[idx
][i
].name
;
2841 * We treat labels[][].columns == 0 as shorthand
2842 * for one column. It makes writing out the label
2843 * tables more concise.
2845 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
2846 unsigned int slen
= strlen(name
);
2848 rw_column_width
= (column_width
* columns
) +
2849 (2 * (columns
- 1));
2851 text_start
= (int)((rw_column_width
)/columns
-
2854 printf(" "); /* Two spaces between columns */
2856 /* Space from beginning of column to label */
2857 for (s
= 0; s
< text_start
; s
++)
2862 /* Print space after label to end of column */
2863 spaces_to_end
= rw_column_width
- text_start
- slen
;
2864 for (s
= 0; s
< spaces_to_end
; s
++)
2873 * print_cmd_columns - Print custom column titles from -c
2875 * If the user specified the "zpool status|iostat -c" then print their custom
2876 * column titles in the header. For example, print_cmd_columns() would print
2877 * the " col1 col2" part of this:
2879 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
2881 * capacity operations bandwidth
2882 * pool alloc free read write read write col1 col2
2883 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
2884 * mypool 269K 1008M 0 0 107 946
2885 * mirror 269K 1008M 0 0 107 946
2886 * sdb - - 0 0 102 473 val1 val2
2887 * sdc - - 0 0 5 473 val1 val2
2888 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
2891 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
2894 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
2896 if (vcdl
->count
== 0 || data
== NULL
)
2900 * Each vdev cmd should have the same column names unless the user did
2901 * something weird with their cmd. Just take the column names from the
2902 * first vdev and assume it works for all of them.
2904 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
2907 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
2910 printf("%*s", vcdl
->uniq_cols_width
[i
],
2911 vcdl
->uniq_cols
[i
]);
2918 * Utility function to print out a line of dashes like:
2920 * -------------------------------- ----- ----- ----- ----- -----
2922 * ...or a dashed named-row line like:
2928 * @force_column_width If non-zero, use the value as the column width.
2929 * Otherwise use the default column widths.
2931 * @name: Print a dashed named-row line starting
2932 * with @name. Otherwise, print a regular
2936 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2940 unsigned int namewidth
;
2941 uint64_t flags
= cb
->cb_flags
;
2944 const name_and_columns_t
*labels
;
2948 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
2949 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
2950 } else if (cb
->cb_vdev_names_count
) {
2956 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
2957 name
? strlen(name
) : 0);
2961 printf("%-*s", namewidth
, name
);
2963 for (i
= 0; i
< namewidth
; i
++)
2967 /* For each bit in flags */
2968 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
2969 unsigned int column_width
;
2970 idx
= lowbit64(f
) - 1;
2971 if (force_column_width
)
2972 column_width
= force_column_width
;
2974 column_width
= default_column_width(cb
, idx
);
2976 labels
= iostat_bottom_labels
[idx
];
2977 for (i
= 0; i
< label_array_len(labels
); i
++) {
2979 printf(" %*s-", column_width
- 1, " ");
2981 printf(" %.*s", column_width
,
2982 "--------------------");
2989 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
2990 unsigned int force_column_width
)
2992 print_iostat_dashes(cb
, force_column_width
, NULL
);
2996 print_iostat_separator(iostat_cbdata_t
*cb
)
2998 print_iostat_separator_impl(cb
, 0);
3002 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3003 const char *histo_vdev_name
)
3005 unsigned int namewidth
;
3008 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3009 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3010 } else if (cb
->cb_vdev_names_count
) {
3016 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3017 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3019 if (histo_vdev_name
)
3020 printf("%-*s", namewidth
, histo_vdev_name
);
3022 printf("%*s", namewidth
, "");
3025 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3028 printf("%-*s", namewidth
, title
);
3030 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3031 if (cb
->vcdl
!= NULL
)
3032 print_cmd_columns(cb
->vcdl
, 0);
3036 print_iostat_separator_impl(cb
, force_column_width
);
3038 if (cb
->vcdl
!= NULL
)
3039 print_cmd_columns(cb
->vcdl
, 1);
3045 print_iostat_header(iostat_cbdata_t
*cb
)
3047 print_iostat_header_impl(cb
, 0, NULL
);
3052 * Display a single statistic.
3055 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3056 unsigned int column_size
, boolean_t scripted
)
3060 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3063 printf("\t%s", buf
);
3065 printf(" %*s", column_size
, buf
);
3069 * Calculate the default vdev stats
3071 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3072 * stats into calcvs.
3075 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
3076 vdev_stat_t
*calcvs
)
3080 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
3081 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
3082 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
3084 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
3085 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
3089 * Internal representation of the extended iostats data.
3091 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3092 * or single uint64_t's. We make both look like arrays to make them easier
3093 * to process. In order to make single uint64_t's look like arrays, we set
3094 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3095 * we can just use *data and count.
3099 uint_t count
; /* Number of entries in data[] */
3100 uint64_t __data
; /* Only used when data is a single uint64_t */
3104 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
3108 for (i
= 0; i
< len
; i
++)
3109 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
3115 * Helper function to lookup a uint64_t array or uint64_t value and store its
3116 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3117 * it look like a one element array to make it easier to process.
3120 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
3121 struct stat_array
*nva
)
3126 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
3127 switch (nvpair_type(tmp
)) {
3128 case DATA_TYPE_UINT64_ARRAY
:
3129 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
3131 case DATA_TYPE_UINT64
:
3132 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
3133 nva
->data
= &nva
->__data
;
3137 /* Not a uint64_t */
3146 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3147 * subtract them, and return the results in a newly allocated stat_array.
3148 * You must free the returned array after you are done with it with
3149 * free_calc_stats().
3151 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3154 static struct stat_array
*
3155 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3158 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3159 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3161 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3163 /* Extract our extended stats nvlist from the main list */
3164 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3167 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3171 newnva
= safe_malloc(alloc_size
);
3172 oldnva
= safe_malloc(alloc_size
);
3173 calcnva
= safe_malloc(alloc_size
);
3175 for (j
= 0; j
< len
; j
++) {
3176 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3178 calcnva
[j
].count
= newnva
[j
].count
;
3179 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3180 calcnva
[j
].data
= safe_malloc(alloc_size
);
3181 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3184 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3186 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3187 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3196 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3199 for (i
= 0; i
< len
; i
++)
3206 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3207 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3213 enum zfs_nicenum_format format
;
3214 unsigned int buckets
;
3215 unsigned int start_bucket
;
3218 format
= ZFS_NICENUM_RAW
;
3220 format
= ZFS_NICENUM_1024
;
3222 /* All these histos are the same size, so just use nva[0].count */
3223 buckets
= nva
[0].count
;
3225 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3226 /* Start at 512 - req size should never be lower than this */
3232 for (j
= start_bucket
; j
< buckets
; j
++) {
3233 /* Print histogram bucket label */
3234 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3235 /* Ending range of this bucket */
3236 val
= (1UL << (j
+ 1)) - 1;
3237 zfs_nicetime(val
, buf
, sizeof (buf
));
3239 /* Request size (starting range of bucket) */
3241 zfs_nicenum(val
, buf
, sizeof (buf
));
3244 if (cb
->cb_scripted
)
3245 printf("%llu", (u_longlong_t
)val
);
3247 printf("%-*s", namewidth
, buf
);
3249 /* Print the values on the line */
3250 for (i
= 0; i
< len
; i
++) {
3251 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3252 column_width
, cb
->cb_scripted
);
3259 print_solid_separator(unsigned int length
)
3267 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3268 nvlist_t
*newnv
, double scale
, const char *name
)
3270 unsigned int column_width
;
3271 unsigned int namewidth
;
3272 unsigned int entire_width
;
3273 enum iostat_type type
;
3274 struct stat_array
*nva
;
3276 unsigned int names_len
;
3278 /* What type of histo are we? */
3279 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3281 /* Get NULL-terminated array of nvlist names for our histo */
3282 names
= vsx_type_to_nvlist
[type
];
3283 names_len
= str_array_len(names
); /* num of names */
3285 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3287 if (cb
->cb_literal
) {
3288 column_width
= MAX(5,
3289 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3294 namewidth
= MAX(cb
->cb_namewidth
,
3295 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3298 * Calculate the entire line width of what we're printing. The
3299 * +2 is for the two spaces between columns:
3303 /* |___| <---------- column_width */
3305 /* |__________| <--- entire_width */
3307 entire_width
= namewidth
+ (column_width
+ 2) *
3308 label_array_len(iostat_bottom_labels
[type
]);
3310 if (cb
->cb_scripted
)
3311 printf("%s\n", name
);
3313 print_iostat_header_impl(cb
, column_width
, name
);
3315 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3318 free_calc_stats(nva
, names_len
);
3319 if (!cb
->cb_scripted
)
3320 print_solid_separator(entire_width
);
3324 * Calculate the average latency of a power-of-two latency histogram
3327 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3330 uint64_t count
= 0, total
= 0;
3332 for (i
= 0; i
< buckets
; i
++) {
3334 * Our buckets are power-of-two latency ranges. Use the
3335 * midpoint latency of each bucket to calculate the average.
3343 if (histo
[i
] != 0) {
3344 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3349 /* Prevent divide by zero */
3350 return (count
== 0 ? 0 : total
/ count
);
3354 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3355 nvlist_t
*newnv
, double scale
)
3359 const char *names
[] = {
3360 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3361 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3362 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3363 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3364 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3365 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3366 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3367 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3368 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3369 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3372 struct stat_array
*nva
;
3374 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3375 enum zfs_nicenum_format format
;
3377 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3380 format
= ZFS_NICENUM_RAW
;
3382 format
= ZFS_NICENUM_1024
;
3384 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3385 val
= nva
[i
].data
[0] * scale
;
3386 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3389 free_calc_stats(nva
, ARRAY_SIZE(names
));
3393 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3394 nvlist_t
*newnv
, double scale
)
3398 const char *names
[] = {
3399 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3400 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3401 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3402 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3403 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3404 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3405 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3406 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3407 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3409 struct stat_array
*nva
;
3411 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3412 enum zfs_nicenum_format format
;
3414 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3417 format
= ZFS_NICENUM_RAW
;
3419 format
= ZFS_NICENUM_TIME
;
3421 /* Print our avg latencies on the line */
3422 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3423 /* Compute average latency for a latency histo */
3424 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
) * scale
;
3425 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3427 free_calc_stats(nva
, ARRAY_SIZE(names
));
3431 * Print default statistics (capacity/operations/bandwidth)
3434 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3436 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3437 enum zfs_nicenum_format format
;
3438 char na
; /* char to print for "not applicable" values */
3440 if (cb
->cb_literal
) {
3441 format
= ZFS_NICENUM_RAW
;
3444 format
= ZFS_NICENUM_1024
;
3448 /* only toplevel vdevs have capacity stats */
3449 if (vs
->vs_space
== 0) {
3450 if (cb
->cb_scripted
)
3451 printf("\t%c\t%c", na
, na
);
3453 printf(" %*c %*c", column_width
, na
, column_width
,
3456 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3458 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3459 column_width
, cb
->cb_scripted
);
3462 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3463 format
, column_width
, cb
->cb_scripted
);
3464 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3465 format
, column_width
, cb
->cb_scripted
);
3466 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3467 format
, column_width
, cb
->cb_scripted
);
3468 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3469 format
, column_width
, cb
->cb_scripted
);
3473 * Print out all the statistics for the given vdev. This can either be the
3474 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3475 * is a verbose output, and we don't want to display the toplevel pool stats.
3477 * Returns the number of stat lines printed.
3480 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3481 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3483 nvlist_t
**oldchild
, **newchild
;
3485 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3486 vdev_stat_t zerovs
= { 0 };
3493 calcvs
= safe_malloc(sizeof (*calcvs
));
3495 if (oldnv
!= NULL
) {
3496 verify(nvlist_lookup_uint64_array(oldnv
,
3497 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3502 /* Do we only want to see a specific vdev? */
3503 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3504 /* Yes we do. Is this the vdev? */
3505 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3507 * This is our vdev. Since it is the only vdev we
3508 * will be displaying, make depth = 0 so that it
3509 * doesn't get indented.
3516 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
3517 /* Couldn't match the name */
3522 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
3523 (uint64_t **)&newvs
, &c
) == 0);
3526 * Print the vdev name unless it's is a histogram. Histograms
3527 * display the vdev name in the header itself.
3529 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3530 if (cb
->cb_scripted
) {
3533 if (strlen(name
) + depth
> cb
->cb_namewidth
)
3534 (void) printf("%*s%s", depth
, "", name
);
3536 (void) printf("%*s%s%*s", depth
, "", name
,
3537 (int)(cb
->cb_namewidth
- strlen(name
) -
3542 /* Calculate our scaling factor */
3543 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
3544 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3546 * If we specify printing histograms with no time interval, then
3547 * print the histogram numbers over the entire lifetime of the
3555 scale
= (double)NANOSEC
/ tdelta
;
3558 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
3559 calc_default_iostats(oldvs
, newvs
, calcvs
);
3560 print_iostat_default(calcvs
, cb
, scale
);
3562 if (cb
->cb_flags
& IOS_LATENCY_M
)
3563 print_iostat_latency(cb
, oldnv
, newnv
, scale
);
3564 if (cb
->cb_flags
& IOS_QUEUES_M
)
3565 print_iostat_queues(cb
, oldnv
, newnv
, scale
);
3566 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3568 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
3571 if (cb
->vcdl
!= NULL
) {
3573 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
3576 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3580 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3589 if (!cb
->cb_verbose
)
3592 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
3593 &newchild
, &children
) != 0)
3596 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
3597 &oldchild
, &c
) != 0)
3600 for (c
= 0; c
< children
; c
++) {
3601 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
3603 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
3606 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
3609 if (ishole
|| islog
)
3612 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3614 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
3615 newchild
[c
], cb
, depth
+ 2);
3620 * Log device section
3623 if (num_logs(newnv
) > 0) {
3624 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3625 !cb
->cb_vdev_names
) {
3626 print_iostat_dashes(cb
, 0, "logs");
3630 for (c
= 0; c
< children
; c
++) {
3631 uint64_t islog
= B_FALSE
;
3632 (void) nvlist_lookup_uint64(newchild
[c
],
3633 ZPOOL_CONFIG_IS_LOG
, &islog
);
3636 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3638 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
3639 oldchild
[c
] : NULL
, newchild
[c
],
3648 * Include level 2 ARC devices in iostat output
3650 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
3651 &newchild
, &children
) != 0)
3654 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
3655 &oldchild
, &c
) != 0)
3659 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3660 !cb
->cb_vdev_names
) {
3661 print_iostat_dashes(cb
, 0, "cache");
3665 for (c
= 0; c
< children
; c
++) {
3666 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3668 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
3669 : NULL
, newchild
[c
], cb
, depth
+ 2);
3678 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
3680 iostat_cbdata_t
*cb
= data
;
3684 * If the pool has disappeared, remove it from the list and continue.
3686 if (zpool_refresh_stats(zhp
, &missing
) != 0)
3690 pool_list_remove(cb
->cb_list
, zhp
);
3696 * Callback to print out the iostats for the given pool.
3699 print_iostat(zpool_handle_t
*zhp
, void *data
)
3701 iostat_cbdata_t
*cb
= data
;
3702 nvlist_t
*oldconfig
, *newconfig
;
3703 nvlist_t
*oldnvroot
, *newnvroot
;
3706 newconfig
= zpool_get_config(zhp
, &oldconfig
);
3708 if (cb
->cb_iteration
== 1)
3711 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3714 if (oldconfig
== NULL
)
3717 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3720 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
3722 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
3723 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
3724 print_iostat_separator(cb
);
3725 if (cb
->vcdl
!= NULL
) {
3726 print_cmd_columns(cb
->vcdl
, 1);
3741 if (isatty(STDOUT_FILENO
)) {
3742 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
3744 columns
= ws
.ws_col
;
3753 get_namewidth(zpool_handle_t
*zhp
, void *data
)
3755 iostat_cbdata_t
*cb
= data
;
3756 nvlist_t
*config
, *nvroot
;
3759 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
3760 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3762 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
3763 if (!cb
->cb_verbose
)
3764 cb
->cb_namewidth
= poolname_len
;
3766 cb
->cb_namewidth
= MAX(poolname_len
,
3767 max_width(zhp
, nvroot
, 0, cb
->cb_namewidth
,
3768 cb
->cb_name_flags
));
3771 * The width must be at least 10, but may be as large as the
3772 * column width - 42 so that we can still fit in one line.
3774 columns
= get_columns();
3776 if (cb
->cb_namewidth
< 10)
3777 cb
->cb_namewidth
= 10;
3778 if (cb
->cb_namewidth
> columns
- 42)
3779 cb
->cb_namewidth
= columns
- 42;
3785 * Parse the input string, get the 'interval' and 'count' value if there is one.
3788 get_interval_count(int *argcp
, char **argv
, float *iv
,
3792 unsigned long count
= 0;
3796 * Determine if the last argument is an integer or a pool name
3798 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3802 interval
= strtof(argv
[argc
- 1], &end
);
3804 if (*end
== '\0' && errno
== 0) {
3805 if (interval
== 0) {
3806 (void) fprintf(stderr
, gettext("interval "
3807 "cannot be zero\n"));
3811 * Ignore the last parameter
3816 * If this is not a valid number, just plow on. The
3817 * user will get a more informative error message later
3825 * If the last argument is also an integer, then we have both a count
3828 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3833 interval
= strtof(argv
[argc
- 1], &end
);
3835 if (*end
== '\0' && errno
== 0) {
3836 if (interval
== 0) {
3837 (void) fprintf(stderr
, gettext("interval "
3838 "cannot be zero\n"));
3843 * Ignore the last parameter
3857 get_timestamp_arg(char c
)
3860 timestamp_fmt
= UDATE
;
3862 timestamp_fmt
= DDATE
;
3868 * Return stat flags that are supported by all pools by both the module and
3869 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
3870 * It will get ANDed down until only the flags that are supported on all pools
3874 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
3876 uint64_t *mask
= data
;
3877 nvlist_t
*config
, *nvroot
, *nvx
;
3881 config
= zpool_get_config(zhp
, NULL
);
3882 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3885 /* Default stats are always supported, but for completeness.. */
3886 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
3887 flags
|= IOS_DEFAULT_M
;
3889 /* Get our extended stats nvlist from the main list */
3890 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3893 * No extended stats; they're probably running an older
3894 * module. No big deal, we support that too.
3899 /* For each extended stat, make sure all its nvpairs are supported */
3900 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
3901 if (!vsx_type_to_nvlist
[j
][0])
3904 /* Start off by assuming the flag is supported, then check */
3905 flags
|= (1ULL << j
);
3906 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
3907 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
3908 /* flag isn't supported */
3909 flags
= flags
& ~(1ULL << j
);
3915 *mask
= *mask
& flags
;
3920 * Return a bitmask of stats that are supported on all pools by both the module
3924 get_stat_flags(zpool_list_t
*list
)
3929 * get_stat_flags_cb() will lop off bits from "mask" until only the
3930 * flags that are supported on all pools remain.
3932 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
3937 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
3940 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
3942 iostat_cbdata_t
*cb
= cb_data
;
3946 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
3948 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
3949 ret
= 1; /* match */
3956 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
3959 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
3961 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
3965 * Check if vdevs are in a pool
3967 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
3968 * return 0. If pool_name is NULL, then search all pools.
3971 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
3972 iostat_cbdata_t
*cb
)
3979 if ((argc
== 0) || !*argv
)
3985 /* Temporarily hijack cb_vdev_names for a second... */
3986 tmp_name
= cb
->cb_vdev_names
;
3988 /* Go though our list of prospective vdev names */
3989 for (i
= 0; i
< argc
; i
++) {
3990 cb
->cb_vdev_names
= argv
+ i
;
3992 /* Is this name a vdev in our pools? */
3993 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4001 cb
->cb_vdev_names
= tmp_name
;
4007 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4010 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4017 * Do we have a pool named *name? If so, return 1, otherwise 0.
4022 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4025 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4027 are_all_pools(int argc
, char **argv
)
4029 if ((argc
== 0) || !*argv
)
4033 if (!is_pool(argv
[argc
]))
4040 * Helper function to print out vdev/pool names we can't resolve. Used for an
4044 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
4045 iostat_cbdata_t
*cb
)
4050 for (i
= 0; i
< argc
; i
++) {
4054 str
= gettext("pool");
4055 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
4056 str
= gettext("vdev in this pool");
4057 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
4058 str
= gettext("vdev in another pool");
4060 str
= gettext("unknown");
4062 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
4067 * Same as get_interval_count(), but with additional checks to not misinterpret
4068 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4072 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
4073 unsigned long *count
, iostat_cbdata_t
*cb
)
4075 char **tmpargv
= argv
;
4076 int argc_for_interval
= 0;
4078 /* Is the last arg an interval value? Or a guid? */
4079 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
4081 * The last arg is not a guid, so it's probably an
4084 argc_for_interval
++;
4087 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
4089 * The 2nd to last arg is not a guid, so it's probably
4090 * an interval value.
4092 argc_for_interval
++;
4096 /* Point to our list of possible intervals */
4097 tmpargv
= &argv
[*argc
- argc_for_interval
];
4099 *argc
= *argc
- argc_for_interval
;
4100 get_interval_count(&argc_for_interval
, tmpargv
,
4105 * Floating point sleep(). Allows you to pass in a floating point value for
4111 struct timespec req
;
4112 req
.tv_sec
= floor(sec
);
4113 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
4114 nanosleep(&req
, NULL
);
4118 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4121 * name: Short name of the script ('iostat').
4122 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4125 print_zpool_script_help(char *name
, char *path
)
4127 char *argv
[] = {path
, "-h", NULL
};
4128 char **lines
= NULL
;
4132 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
4134 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0)
4137 for (int i
= 0; i
< lines_cnt
; i
++)
4138 if (!is_blank_str(lines
[i
]))
4139 printf(" %-14s %s\n", name
, lines
[i
]);
4141 libzfs_free_str_array(lines
, lines_cnt
);
4146 * Go though the list of all the zpool status/iostat -c scripts, run their
4147 * help option (-h), and print out the results.
4150 print_zpool_script_list(void)
4154 char fullpath
[MAXPATHLEN
];
4155 struct stat dir_stat
;
4157 if ((dir
= opendir(ZPOOL_SCRIPTS_DIR
)) != NULL
) {
4159 /* print all the files and directories within directory */
4160 while ((ent
= readdir(dir
)) != NULL
) {
4161 sprintf(fullpath
, "%s/%s", ZPOOL_SCRIPTS_DIR
,
4164 /* Print the scripts */
4165 if (stat(fullpath
, &dir_stat
) == 0)
4166 if (dir_stat
.st_mode
& S_IXUSR
&&
4167 S_ISREG(dir_stat
.st_mode
))
4168 print_zpool_script_help(ent
->d_name
,
4174 fprintf(stderr
, gettext("Can't open %s scripts dir\n"),
4180 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4181 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4182 * [interval [count]]
4184 * -c CMD For each vdev, run command CMD
4185 * -g Display guid for individual vdev name.
4186 * -L Follow links when resolving vdev path name.
4187 * -P Display full path for vdev name.
4188 * -v Display statistics for individual vdevs
4190 * -p Display values in parsable (exact) format.
4191 * -H Scripted mode. Don't display headers, and separate properties
4193 * -l Display average latency
4194 * -q Display queue depths
4195 * -w Display latency histograms
4196 * -r Display request size histogram
4197 * -T Display a timestamp in date(1) or Unix format
4199 * This command can be tricky because we want to be able to deal with pool
4200 * creation/destruction as well as vdev configuration changes. The bulk of this
4201 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4202 * on pool_list_update() to detect the addition of new pools. Configuration
4203 * changes are all handled within libzfs.
4206 zpool_do_iostat(int argc
, char **argv
)
4212 unsigned long count
= 0;
4214 boolean_t verbose
= B_FALSE
;
4215 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
4216 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
4217 boolean_t omit_since_boot
= B_FALSE
;
4218 boolean_t guid
= B_FALSE
;
4219 boolean_t follow_links
= B_FALSE
;
4220 boolean_t full_name
= B_FALSE
;
4221 iostat_cbdata_t cb
= { 0 };
4224 /* Used for printing error message */
4225 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4226 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4228 uint64_t unsupported_flags
;
4231 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4236 gettext("Can't set -c flag twice\n"));
4239 if ((getuid() <= 0 || geteuid() <= 0) &&
4240 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4241 fprintf(stderr
, gettext(
4242 "Can't run -c with root privileges "
4243 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4253 follow_links
= B_TRUE
;
4259 get_timestamp_arg(*optarg
);
4283 omit_since_boot
= B_TRUE
;
4289 if (optopt
== 'c') {
4290 fprintf(stderr
, gettext(
4291 "Current scripts in %s:\n"),
4293 print_zpool_script_list();
4297 gettext("invalid option '%c'\n"), optopt
);
4306 cb
.cb_literal
= parsable
;
4307 cb
.cb_scripted
= scripted
;
4310 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4312 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4314 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4315 cb
.cb_iteration
= 0;
4316 cb
.cb_namewidth
= 0;
4317 cb
.cb_verbose
= verbose
;
4319 /* Get our interval and count values (if any) */
4321 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4324 get_interval_count(&argc
, argv
, &interval
, &count
);
4328 /* No args, so just print the defaults. */
4329 } else if (are_all_pools(argc
, argv
)) {
4330 /* All the args are pool names */
4331 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4332 /* All the args are vdevs */
4333 cb
.cb_vdev_names
= argv
;
4334 cb
.cb_vdev_names_count
= argc
;
4335 argc
= 0; /* No pools to process */
4336 } else if (are_all_pools(1, argv
)) {
4337 /* The first arg is a pool name */
4338 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4339 /* ...and the rest are vdev names */
4340 cb
.cb_vdev_names
= argv
+ 1;
4341 cb
.cb_vdev_names_count
= argc
- 1;
4342 argc
= 1; /* One pool to process */
4344 fprintf(stderr
, gettext("Expected either a list of "));
4345 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4346 fprintf(stderr
, " \"%s\", ", argv
[0]);
4347 fprintf(stderr
, gettext("but got:\n"));
4348 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4350 fprintf(stderr
, "\n");
4356 * The args don't make sense. The first arg isn't a pool name,
4357 * nor are all the args vdevs.
4359 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4360 fprintf(stderr
, "\n");
4364 if (cb
.cb_vdev_names_count
!= 0) {
4366 * If user specified vdevs, it implies verbose.
4368 cb
.cb_verbose
= B_TRUE
;
4372 * Construct the list of all interesting pools.
4375 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4378 if (pool_list_count(list
) == 0 && argc
!= 0) {
4379 pool_list_free(list
);
4383 if (pool_list_count(list
) == 0 && interval
== 0) {
4384 pool_list_free(list
);
4385 (void) fprintf(stderr
, gettext("no pools available\n"));
4389 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
4390 pool_list_free(list
);
4391 (void) fprintf(stderr
,
4392 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4397 if (l_histo
&& rq_histo
) {
4398 pool_list_free(list
);
4399 (void) fprintf(stderr
,
4400 gettext("Only one of [-r|-w] can be passed at a time\n"));
4406 * Enter the main iostat loop.
4412 * Histograms tables look out of place when you try to display
4413 * them with the other stats, so make a rule that you can only
4414 * print histograms by themselves.
4416 cb
.cb_flags
= IOS_L_HISTO_M
;
4417 } else if (rq_histo
) {
4418 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4420 cb
.cb_flags
= IOS_DEFAULT_M
;
4422 cb
.cb_flags
|= IOS_LATENCY_M
;
4424 cb
.cb_flags
|= IOS_QUEUES_M
;
4428 * See if the module supports all the stats we want to display.
4430 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4431 if (unsupported_flags
) {
4435 gettext("The loaded zfs module doesn't support:"));
4437 /* for each bit set in unsupported_flags */
4438 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4439 idx
= lowbit64(f
) - 1;
4440 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
4443 fprintf(stderr
, ". Try running a newer module.\n");
4444 pool_list_free(list
);
4450 if ((npools
= pool_list_count(list
)) == 0)
4451 (void) fprintf(stderr
, gettext("no pools available\n"));
4454 * If this is the first iteration and -y was supplied
4455 * we skip any printing.
4457 boolean_t skip
= (omit_since_boot
&&
4458 cb
.cb_iteration
== 0);
4461 * Refresh all statistics. This is done as an
4462 * explicit step before calculating the maximum name
4463 * width, so that any * configuration changes are
4464 * properly accounted for.
4466 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
4470 * Iterate over all pools to determine the maximum width
4471 * for the pool / device name column across all pools.
4473 cb
.cb_namewidth
= 0;
4474 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
4477 if (timestamp_fmt
!= NODATE
)
4478 print_timestamp(timestamp_fmt
);
4480 if (cmd
!= NULL
&& cb
.cb_verbose
&&
4481 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
4482 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
4483 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
4484 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
4490 * If it's the first time and we're not skipping it,
4491 * or either skip or verbose mode, print the header.
4493 * The histogram code explicitly prints its header on
4494 * every vdev, so skip this for histograms.
4496 if (((++cb
.cb_iteration
== 1 && !skip
) ||
4497 (skip
!= verbose
)) &&
4498 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
4500 print_iostat_header(&cb
);
4503 (void) fsleep(interval
);
4508 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
4511 * If there's more than one pool, and we're not in
4512 * verbose mode (which prints a separator for us),
4513 * then print a separator.
4515 * In addition, if we're printing specific vdevs then
4516 * we also want an ending separator.
4518 if (((npools
> 1 && !verbose
&&
4519 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
4520 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
4521 cb
.cb_vdev_names_count
)) &&
4523 print_iostat_separator(&cb
);
4524 if (cb
.vcdl
!= NULL
)
4525 print_cmd_columns(cb
.vcdl
, 1);
4529 if (cb
.vcdl
!= NULL
)
4530 free_vdev_cmd_data_list(cb
.vcdl
);
4535 * Flush the output so that redirection to a file isn't buffered
4538 (void) fflush(stdout
);
4543 if (count
!= 0 && --count
== 0)
4546 (void) fsleep(interval
);
4549 pool_list_free(list
);
4554 typedef struct list_cbdata
{
4555 boolean_t cb_verbose
;
4558 boolean_t cb_scripted
;
4559 zprop_list_t
*cb_proplist
;
4560 boolean_t cb_literal
;
4564 * Given a list of columns to display, output appropriate headers for each one.
4567 print_header(list_cbdata_t
*cb
)
4569 zprop_list_t
*pl
= cb
->cb_proplist
;
4570 char headerbuf
[ZPOOL_MAXPROPLEN
];
4572 boolean_t first
= B_TRUE
;
4573 boolean_t right_justify
;
4576 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4577 width
= pl
->pl_width
;
4578 if (first
&& cb
->cb_verbose
) {
4580 * Reset the width to accommodate the verbose listing
4583 width
= cb
->cb_namewidth
;
4591 right_justify
= B_FALSE
;
4592 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4593 header
= zpool_prop_column_name(pl
->pl_prop
);
4594 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4598 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
4599 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
4600 headerbuf
[i
] = '\0';
4604 if (pl
->pl_next
== NULL
&& !right_justify
)
4605 (void) printf("%s", header
);
4606 else if (right_justify
)
4607 (void) printf("%*s", (int)width
, header
);
4609 (void) printf("%-*s", (int)width
, header
);
4612 (void) printf("\n");
4616 * Given a pool and a list of properties, print out all the properties according
4617 * to the described layout.
4620 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
4622 zprop_list_t
*pl
= cb
->cb_proplist
;
4623 boolean_t first
= B_TRUE
;
4624 char property
[ZPOOL_MAXPROPLEN
];
4626 boolean_t right_justify
;
4629 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4631 width
= pl
->pl_width
;
4632 if (first
&& cb
->cb_verbose
) {
4634 * Reset the width to accommodate the verbose listing
4637 width
= cb
->cb_namewidth
;
4641 if (cb
->cb_scripted
)
4642 (void) printf("\t");
4649 right_justify
= B_FALSE
;
4650 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4651 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
4652 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
4657 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4658 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
4659 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
4660 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
4661 sizeof (property
)) == 0) {
4669 * If this is being called in scripted mode, or if this is the
4670 * last column and it is left-justified, don't include a width
4673 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
4674 (void) printf("%s", propstr
);
4675 else if (right_justify
)
4676 (void) printf("%*s", (int)width
, propstr
);
4678 (void) printf("%-*s", (int)width
, propstr
);
4681 (void) printf("\n");
4685 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
4686 boolean_t valid
, enum zfs_nicenum_format format
)
4690 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
4693 case ZPOOL_PROP_EXPANDSZ
:
4695 (void) strlcpy(propval
, "-", sizeof (propval
));
4697 zfs_nicenum_format(value
, propval
, sizeof (propval
),
4700 case ZPOOL_PROP_FRAGMENTATION
:
4701 if (value
== ZFS_FRAG_INVALID
) {
4702 (void) strlcpy(propval
, "-", sizeof (propval
));
4703 } else if (format
== ZFS_NICENUM_RAW
) {
4704 (void) snprintf(propval
, sizeof (propval
), "%llu",
4705 (unsigned long long)value
);
4707 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4708 (unsigned long long)value
);
4711 case ZPOOL_PROP_CAPACITY
:
4712 if (format
== ZFS_NICENUM_RAW
)
4713 (void) snprintf(propval
, sizeof (propval
), "%llu",
4714 (unsigned long long)value
);
4716 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4717 (unsigned long long)value
);
4720 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
4724 (void) strlcpy(propval
, "-", sizeof (propval
));
4727 (void) printf("\t%s", propval
);
4729 (void) printf(" %*s", (int)width
, propval
);
4733 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
4734 list_cbdata_t
*cb
, int depth
)
4740 boolean_t scripted
= cb
->cb_scripted
;
4741 uint64_t islog
= B_FALSE
;
4742 boolean_t haslog
= B_FALSE
;
4743 char *dashes
= "%-*s - - - - - -\n";
4745 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
4746 (uint64_t **)&vs
, &c
) == 0);
4749 boolean_t toplevel
= (vs
->vs_space
!= 0);
4751 enum zfs_nicenum_format format
;
4754 format
= ZFS_NICENUM_RAW
;
4756 format
= ZFS_NICENUM_1024
;
4759 (void) printf("\t%s", name
);
4760 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
4761 (void) printf("%*s%s", depth
, "", name
);
4763 (void) printf("%*s%s%*s", depth
, "", name
,
4764 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
4767 * Print the properties for the individual vdevs. Some
4768 * properties are only applicable to toplevel vdevs. The
4769 * 'toplevel' boolean value is passed to the print_one_column()
4770 * to indicate that the value is valid.
4772 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
4774 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
4776 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
4777 scripted
, toplevel
, format
);
4778 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
4780 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
4781 vs
->vs_fragmentation
, scripted
,
4782 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
4784 cap
= (vs
->vs_space
== 0) ? 0 :
4785 (vs
->vs_alloc
* 100 / vs
->vs_space
);
4786 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
,
4788 (void) printf("\n");
4791 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
4792 &child
, &children
) != 0)
4795 for (c
= 0; c
< children
; c
++) {
4796 uint64_t ishole
= B_FALSE
;
4798 if (nvlist_lookup_uint64(child
[c
],
4799 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
4802 if (nvlist_lookup_uint64(child
[c
],
4803 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
4808 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4810 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4814 if (haslog
== B_TRUE
) {
4815 /* LINTED E_SEC_PRINTF_VAR_FMT */
4816 (void) printf(dashes
, cb
->cb_namewidth
, "log");
4817 for (c
= 0; c
< children
; c
++) {
4818 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
4819 &islog
) != 0 || !islog
)
4821 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4823 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4828 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
4829 &child
, &children
) == 0 && children
> 0) {
4830 /* LINTED E_SEC_PRINTF_VAR_FMT */
4831 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
4832 for (c
= 0; c
< children
; c
++) {
4833 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4835 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4840 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
4841 &children
) == 0 && children
> 0) {
4842 /* LINTED E_SEC_PRINTF_VAR_FMT */
4843 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
4844 for (c
= 0; c
< children
; c
++) {
4845 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4847 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4855 * Generic callback function to list a pool.
4858 list_callback(zpool_handle_t
*zhp
, void *data
)
4860 list_cbdata_t
*cbp
= data
;
4864 config
= zpool_get_config(zhp
, NULL
);
4866 print_pool(zhp
, cbp
);
4867 if (!cbp
->cb_verbose
)
4870 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4872 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
4878 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
4880 * -g Display guid for individual vdev name.
4881 * -H Scripted mode. Don't display headers, and separate properties
4883 * -L Follow links when resolving vdev path name.
4884 * -o List of properties to display. Defaults to
4885 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
4886 * "dedupratio,health,altroot"
4887 * -p Display values in parsable (exact) format.
4888 * -P Display full path for vdev name.
4889 * -T Display a timestamp in date(1) or Unix format
4891 * List all pools in the system, whether or not they're healthy. Output space
4892 * statistics for each one, as well as health status summary.
4895 zpool_do_list(int argc
, char **argv
)
4899 list_cbdata_t cb
= { 0 };
4900 static char default_props
[] =
4901 "name,size,allocated,free,expandsize,fragmentation,capacity,"
4902 "dedupratio,health,altroot";
4903 char *props
= default_props
;
4905 unsigned long count
= 0;
4907 boolean_t first
= B_TRUE
;
4910 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
4913 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4916 cb
.cb_scripted
= B_TRUE
;
4919 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4925 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4928 cb
.cb_literal
= B_TRUE
;
4931 get_timestamp_arg(*optarg
);
4934 cb
.cb_verbose
= B_TRUE
;
4937 (void) fprintf(stderr
, gettext("missing argument for "
4938 "'%c' option\n"), optopt
);
4942 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4951 get_interval_count(&argc
, argv
, &interval
, &count
);
4953 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
4957 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
4961 if (pool_list_count(list
) == 0)
4964 if (timestamp_fmt
!= NODATE
)
4965 print_timestamp(timestamp_fmt
);
4967 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
4971 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
4976 if (count
!= 0 && --count
== 0)
4979 pool_list_free(list
);
4980 (void) fsleep(interval
);
4983 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
4984 (void) printf(gettext("no pools available\n"));
4988 pool_list_free(list
);
4989 zprop_free_list(cb
.cb_proplist
);
4994 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
4996 boolean_t force
= B_FALSE
;
4999 char *poolname
, *old_disk
, *new_disk
;
5000 zpool_handle_t
*zhp
;
5001 nvlist_t
*props
= NULL
;
5006 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
5012 if ((propval
= strchr(optarg
, '=')) == NULL
) {
5013 (void) fprintf(stderr
, gettext("missing "
5014 "'=' for -o option\n"));
5020 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
5021 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
5025 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5034 /* get pool name and check number of arguments */
5036 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5043 (void) fprintf(stderr
,
5044 gettext("missing <device> specification\n"));
5052 (void) fprintf(stderr
,
5053 gettext("missing <new_device> specification\n"));
5056 new_disk
= old_disk
;
5066 (void) fprintf(stderr
, gettext("too many arguments\n"));
5070 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
5075 if (zpool_get_config(zhp
, NULL
) == NULL
) {
5076 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
5083 /* unless manually specified use "ashift" pool property (if set) */
5084 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
5087 char strval
[ZPOOL_MAXPROPLEN
];
5089 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
5090 if (src
!= ZPROP_SRC_DEFAULT
) {
5091 (void) sprintf(strval
, "%" PRId32
, intval
);
5092 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
5093 &props
, B_TRUE
) == 0);
5097 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
5099 if (nvroot
== NULL
) {
5105 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
5108 nvlist_free(nvroot
);
5115 * zpool replace [-f] <pool> <device> <new_device>
5117 * -f Force attach, even if <new_device> appears to be in use.
5119 * Replace <device> with <new_device>.
5123 zpool_do_replace(int argc
, char **argv
)
5125 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
5129 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5131 * -f Force attach, even if <new_device> appears to be in use.
5132 * -o Set property=value.
5134 * Attach <new_device> to the mirror containing <device>. If <device> is not
5135 * part of a mirror, then <device> will be transformed into a mirror of
5136 * <device> and <new_device>. In either case, <new_device> will begin life
5137 * with a DTL of [0, now], and will immediately begin to resilver itself.
5140 zpool_do_attach(int argc
, char **argv
)
5142 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
5146 * zpool detach [-f] <pool> <device>
5148 * -f Force detach of <device>, even if DTLs argue against it
5149 * (not supported yet)
5151 * Detach a device from a mirror. The operation will be refused if <device>
5152 * is the last device in the mirror, or if the DTLs indicate that this device
5153 * has the only valid copy of some data.
5157 zpool_do_detach(int argc
, char **argv
)
5160 char *poolname
, *path
;
5161 zpool_handle_t
*zhp
;
5165 while ((c
= getopt(argc
, argv
, "f")) != -1) {
5169 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5178 /* get pool name and check number of arguments */
5180 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5185 (void) fprintf(stderr
,
5186 gettext("missing <device> specification\n"));
5193 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5196 ret
= zpool_vdev_detach(zhp
, path
);
5204 * zpool split [-gLnP] [-o prop=val] ...
5206 * [-R altroot] <pool> <newpool> [<device> ...]
5208 * -g Display guid for individual vdev name.
5209 * -L Follow links when resolving vdev path name.
5210 * -n Do not split the pool, but display the resulting layout if
5211 * it were to be split.
5212 * -o Set property=value, or set mount options.
5213 * -P Display full path for vdev name.
5214 * -R Mount the split-off pool under an alternate root.
5216 * Splits the named pool and gives it the new pool name. Devices to be split
5217 * off may be listed, provided that no more than one device is specified
5218 * per top-level vdev mirror. The newly split pool is left in an exported
5219 * state unless -R is specified.
5221 * Restrictions: the top-level of the pool pool must only be made up of
5222 * mirrors; all devices in the pool must be healthy; no device may be
5223 * undergoing a resilvering operation.
5226 zpool_do_split(int argc
, char **argv
)
5228 char *srcpool
, *newpool
, *propval
;
5229 char *mntopts
= NULL
;
5232 zpool_handle_t
*zhp
;
5233 nvlist_t
*config
, *props
= NULL
;
5235 flags
.dryrun
= B_FALSE
;
5236 flags
.import
= B_FALSE
;
5237 flags
.name_flags
= 0;
5240 while ((c
= getopt(argc
, argv
, ":gLR:no:P")) != -1) {
5243 flags
.name_flags
|= VDEV_NAME_GUID
;
5246 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5249 flags
.import
= B_TRUE
;
5251 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
5252 &props
, B_TRUE
) != 0) {
5258 flags
.dryrun
= B_TRUE
;
5261 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5264 if (add_prop_list(optarg
, propval
,
5265 &props
, B_TRUE
) != 0) {
5274 flags
.name_flags
|= VDEV_NAME_PATH
;
5277 (void) fprintf(stderr
, gettext("missing argument for "
5278 "'%c' option\n"), optopt
);
5282 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5289 if (!flags
.import
&& mntopts
!= NULL
) {
5290 (void) fprintf(stderr
, gettext("setting mntopts is only "
5291 "valid when importing the pool\n"));
5299 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5303 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5313 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5318 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5319 if (config
== NULL
) {
5323 (void) printf(gettext("would create '%s' with the "
5324 "following layout:\n\n"), newpool
);
5325 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
5332 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5333 nvlist_free(config
);
5339 * The split was successful. Now we need to open the new
5340 * pool and import it.
5342 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5343 nvlist_free(config
);
5347 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
5348 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
5350 (void) fprintf(stderr
, gettext("Split was successful, but "
5351 "the datasets could not all be mounted\n"));
5352 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
5353 "different altroot\n"), "zpool import");
5356 nvlist_free(config
);
5365 * zpool online <pool> <device> ...
5368 zpool_do_online(int argc
, char **argv
)
5372 zpool_handle_t
*zhp
;
5374 vdev_state_t newstate
;
5378 while ((c
= getopt(argc
, argv
, "et")) != -1) {
5381 flags
|= ZFS_ONLINE_EXPAND
;
5385 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5394 /* get pool name and check number of arguments */
5396 (void) fprintf(stderr
, gettext("missing pool name\n"));
5400 (void) fprintf(stderr
, gettext("missing device name\n"));
5406 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5409 for (i
= 1; i
< argc
; i
++) {
5410 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
5411 if (newstate
!= VDEV_STATE_HEALTHY
) {
5412 (void) printf(gettext("warning: device '%s' "
5413 "onlined, but remains in faulted state\n"),
5415 if (newstate
== VDEV_STATE_FAULTED
)
5416 (void) printf(gettext("use 'zpool "
5417 "clear' to restore a faulted "
5420 (void) printf(gettext("use 'zpool "
5421 "replace' to replace devices "
5422 "that are no longer present\n"));
5435 * zpool offline [-ft] <pool> <device> ...
5437 * -f Force the device into the offline state, even if doing
5438 * so would appear to compromise pool availability.
5439 * (not supported yet)
5441 * -t Only take the device off-line temporarily. The offline
5442 * state will not be persistent across reboots.
5446 zpool_do_offline(int argc
, char **argv
)
5450 zpool_handle_t
*zhp
;
5452 boolean_t istmp
= B_FALSE
;
5455 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
5462 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5471 /* get pool name and check number of arguments */
5473 (void) fprintf(stderr
, gettext("missing pool name\n"));
5477 (void) fprintf(stderr
, gettext("missing device name\n"));
5483 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5486 for (i
= 1; i
< argc
; i
++) {
5487 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
5497 * zpool clear <pool> [device]
5499 * Clear all errors associated with a pool or a particular device.
5502 zpool_do_clear(int argc
, char **argv
)
5506 boolean_t dryrun
= B_FALSE
;
5507 boolean_t do_rewind
= B_FALSE
;
5508 boolean_t xtreme_rewind
= B_FALSE
;
5509 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
5510 nvlist_t
*policy
= NULL
;
5511 zpool_handle_t
*zhp
;
5512 char *pool
, *device
;
5515 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
5524 xtreme_rewind
= B_TRUE
;
5527 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5537 (void) fprintf(stderr
, gettext("missing pool name\n"));
5542 (void) fprintf(stderr
, gettext("too many arguments\n"));
5546 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
5547 (void) fprintf(stderr
,
5548 gettext("-n or -X only meaningful with -F\n"));
5552 rewind_policy
= ZPOOL_TRY_REWIND
;
5554 rewind_policy
= ZPOOL_DO_REWIND
;
5556 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
5558 /* In future, further rewind policy choices can be passed along here */
5559 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
5560 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
5564 device
= argc
== 2 ? argv
[1] : NULL
;
5566 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
5567 nvlist_free(policy
);
5571 if (zpool_clear(zhp
, device
, policy
) != 0)
5576 nvlist_free(policy
);
5582 * zpool reguid <pool>
5585 zpool_do_reguid(int argc
, char **argv
)
5589 zpool_handle_t
*zhp
;
5593 while ((c
= getopt(argc
, argv
, "")) != -1) {
5596 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5605 /* get pool name and check number of arguments */
5607 (void) fprintf(stderr
, gettext("missing pool name\n"));
5612 (void) fprintf(stderr
, gettext("too many arguments\n"));
5617 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5620 ret
= zpool_reguid(zhp
);
5628 * zpool reopen <pool>
5630 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5633 zpool_do_reopen(int argc
, char **argv
)
5637 zpool_handle_t
*zhp
;
5641 while ((c
= getopt(argc
, argv
, "")) != -1) {
5644 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5654 (void) fprintf(stderr
, gettext("missing pool name\n"));
5659 (void) fprintf(stderr
, gettext("too many arguments\n"));
5664 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
5667 ret
= zpool_reopen(zhp
);
5672 typedef struct scrub_cbdata
{
5679 scrub_callback(zpool_handle_t
*zhp
, void *data
)
5681 scrub_cbdata_t
*cb
= data
;
5685 * Ignore faulted pools.
5687 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5688 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
5689 "currently unavailable\n"), zpool_get_name(zhp
));
5693 err
= zpool_scan(zhp
, cb
->cb_type
);
5699 * zpool scrub [-s] <pool> ...
5701 * -s Stop. Stops any in-progress scrub.
5704 zpool_do_scrub(int argc
, char **argv
)
5709 cb
.cb_type
= POOL_SCAN_SCRUB
;
5712 while ((c
= getopt(argc
, argv
, "s")) != -1) {
5715 cb
.cb_type
= POOL_SCAN_NONE
;
5718 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5730 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5734 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
5738 * Print out detailed scrub status.
5741 print_scan_status(pool_scan_stat_t
*ps
)
5744 uint64_t elapsed
, mins_left
, hours_left
;
5745 uint64_t pass_exam
, examined
, total
;
5747 double fraction_done
;
5748 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
5750 (void) printf(gettext(" scan: "));
5752 /* If there's never been a scan, there's not much to say. */
5753 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
5754 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
5755 (void) printf(gettext("none requested\n"));
5759 start
= ps
->pss_start_time
;
5760 end
= ps
->pss_end_time
;
5761 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
5763 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
5764 ps
->pss_func
== POOL_SCAN_RESILVER
);
5766 * Scan is finished or canceled.
5768 if (ps
->pss_state
== DSS_FINISHED
) {
5769 uint64_t minutes_taken
= (end
- start
) / 60;
5772 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5773 fmt
= gettext("scrub repaired %s in %lluh%um with "
5774 "%llu errors on %s");
5775 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5776 fmt
= gettext("resilvered %s in %lluh%um with "
5777 "%llu errors on %s");
5780 (void) printf(fmt
, processed_buf
,
5781 (u_longlong_t
)(minutes_taken
/ 60),
5782 (uint_t
)(minutes_taken
% 60),
5783 (u_longlong_t
)ps
->pss_errors
,
5784 ctime((time_t *)&end
));
5786 } else if (ps
->pss_state
== DSS_CANCELED
) {
5787 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5788 (void) printf(gettext("scrub canceled on %s"),
5790 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5791 (void) printf(gettext("resilver canceled on %s"),
5797 assert(ps
->pss_state
== DSS_SCANNING
);
5800 * Scan is in progress.
5802 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5803 (void) printf(gettext("scrub in progress since %s"),
5805 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5806 (void) printf(gettext("resilver in progress since %s"),
5810 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
5811 total
= ps
->pss_to_examine
;
5812 fraction_done
= (double)examined
/ total
;
5814 /* elapsed time for this pass */
5815 elapsed
= time(NULL
) - ps
->pss_pass_start
;
5816 elapsed
= elapsed
? elapsed
: 1;
5817 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
5818 rate
= pass_exam
/ elapsed
;
5819 rate
= rate
? rate
: 1;
5820 mins_left
= ((total
- examined
) / rate
) / 60;
5821 hours_left
= mins_left
/ 60;
5823 zfs_nicebytes(examined
, examined_buf
, sizeof (examined_buf
));
5824 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
5825 zfs_nicebytes(rate
, rate_buf
, sizeof (rate_buf
));
5828 * do not print estimated time if hours_left is more than 30 days
5830 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
5831 examined_buf
, total_buf
, rate_buf
);
5832 if (hours_left
< (30 * 24)) {
5833 (void) printf(gettext(", %lluh%um to go\n"),
5834 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
5836 (void) printf(gettext(
5837 ", (scan is slow, no estimated time)\n"));
5840 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5841 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
5842 processed_buf
, 100 * fraction_done
);
5843 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5844 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
5845 processed_buf
, 100 * fraction_done
);
5850 print_error_log(zpool_handle_t
*zhp
)
5852 nvlist_t
*nverrlist
= NULL
;
5855 size_t len
= MAXPATHLEN
* 2;
5857 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
5860 (void) printf("errors: Permanent errors have been "
5861 "detected in the following files:\n\n");
5863 pathname
= safe_malloc(len
);
5865 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
5867 uint64_t dsobj
, obj
;
5869 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
5870 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
5872 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
5874 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
5875 (void) printf("%7s %s\n", "", pathname
);
5878 nvlist_free(nverrlist
);
5882 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
5891 (void) printf(gettext("\tspares\n"));
5893 for (i
= 0; i
< nspares
; i
++) {
5894 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
5896 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
5902 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
5911 (void) printf(gettext("\tcache\n"));
5913 for (i
= 0; i
< nl2cache
; i
++) {
5914 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
5916 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
5922 print_dedup_stats(nvlist_t
*config
)
5924 ddt_histogram_t
*ddh
;
5928 char dspace
[6], mspace
[6];
5931 * If the pool was faulted then we may not have been able to
5932 * obtain the config. Otherwise, if we have anything in the dedup
5933 * table continue processing the stats.
5935 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
5936 (uint64_t **)&ddo
, &c
) != 0)
5939 (void) printf("\n");
5940 (void) printf(gettext(" dedup: "));
5941 if (ddo
->ddo_count
== 0) {
5942 (void) printf(gettext("no DDT entries\n"));
5946 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
5947 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
5948 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
5949 (u_longlong_t
)ddo
->ddo_count
,
5953 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
5954 (uint64_t **)&dds
, &c
) == 0);
5955 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
5956 (uint64_t **)&ddh
, &c
) == 0);
5957 zpool_dump_ddt(dds
, ddh
);
5961 * Display a summary of pool status. Displays a summary such as:
5965 * reason: One or more devices ...
5966 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
5972 * When given the '-v' option, we print out the complete config. If the '-e'
5973 * option is specified, then we print out error rate information as well.
5976 status_callback(zpool_handle_t
*zhp
, void *data
)
5978 status_cbdata_t
*cbp
= data
;
5979 nvlist_t
*config
, *nvroot
;
5981 zpool_status_t reason
;
5982 zpool_errata_t errata
;
5987 config
= zpool_get_config(zhp
, NULL
);
5988 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
5993 * If we were given 'zpool status -x', only report those pools with
5996 if (cbp
->cb_explain
&&
5997 (reason
== ZPOOL_STATUS_OK
||
5998 reason
== ZPOOL_STATUS_VERSION_OLDER
||
5999 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
6000 if (!cbp
->cb_allpools
) {
6001 (void) printf(gettext("pool '%s' is healthy\n"),
6002 zpool_get_name(zhp
));
6004 cbp
->cb_first
= B_FALSE
;
6010 cbp
->cb_first
= B_FALSE
;
6012 (void) printf("\n");
6014 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6016 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
6017 (uint64_t **)&vs
, &c
) == 0);
6018 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6020 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
6021 (void) printf(gettext(" state: %s\n"), health
);
6024 case ZPOOL_STATUS_MISSING_DEV_R
:
6025 (void) printf(gettext("status: One or more devices could not "
6026 "be opened. Sufficient replicas exist for\n\tthe pool to "
6027 "continue functioning in a degraded state.\n"));
6028 (void) printf(gettext("action: Attach the missing device and "
6029 "online it using 'zpool online'.\n"));
6032 case ZPOOL_STATUS_MISSING_DEV_NR
:
6033 (void) printf(gettext("status: One or more devices could not "
6034 "be opened. There are insufficient\n\treplicas for the "
6035 "pool to continue functioning.\n"));
6036 (void) printf(gettext("action: Attach the missing device and "
6037 "online it using 'zpool online'.\n"));
6040 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
6041 (void) printf(gettext("status: One or more devices could not "
6042 "be used because the label is missing or\n\tinvalid. "
6043 "Sufficient replicas exist for the pool to continue\n\t"
6044 "functioning in a degraded state.\n"));
6045 (void) printf(gettext("action: Replace the device using "
6046 "'zpool replace'.\n"));
6049 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
6050 (void) printf(gettext("status: One or more devices could not "
6051 "be used because the label is missing \n\tor invalid. "
6052 "There are insufficient replicas for the pool to "
6053 "continue\n\tfunctioning.\n"));
6054 zpool_explain_recover(zpool_get_handle(zhp
),
6055 zpool_get_name(zhp
), reason
, config
);
6058 case ZPOOL_STATUS_FAILING_DEV
:
6059 (void) printf(gettext("status: One or more devices has "
6060 "experienced an unrecoverable error. An\n\tattempt was "
6061 "made to correct the error. Applications are "
6063 (void) printf(gettext("action: Determine if the device needs "
6064 "to be replaced, and clear the errors\n\tusing "
6065 "'zpool clear' or replace the device with 'zpool "
6069 case ZPOOL_STATUS_OFFLINE_DEV
:
6070 (void) printf(gettext("status: One or more devices has "
6071 "been taken offline by the administrator.\n\tSufficient "
6072 "replicas exist for the pool to continue functioning in "
6073 "a\n\tdegraded state.\n"));
6074 (void) printf(gettext("action: Online the device using "
6075 "'zpool online' or replace the device with\n\t'zpool "
6079 case ZPOOL_STATUS_REMOVED_DEV
:
6080 (void) printf(gettext("status: One or more devices has "
6081 "been removed by the administrator.\n\tSufficient "
6082 "replicas exist for the pool to continue functioning in "
6083 "a\n\tdegraded state.\n"));
6084 (void) printf(gettext("action: Online the device using "
6085 "'zpool online' or replace the device with\n\t'zpool "
6089 case ZPOOL_STATUS_RESILVERING
:
6090 (void) printf(gettext("status: One or more devices is "
6091 "currently being resilvered. The pool will\n\tcontinue "
6092 "to function, possibly in a degraded state.\n"));
6093 (void) printf(gettext("action: Wait for the resilver to "
6097 case ZPOOL_STATUS_CORRUPT_DATA
:
6098 (void) printf(gettext("status: One or more devices has "
6099 "experienced an error resulting in data\n\tcorruption. "
6100 "Applications may be affected.\n"));
6101 (void) printf(gettext("action: Restore the file in question "
6102 "if possible. Otherwise restore the\n\tentire pool from "
6106 case ZPOOL_STATUS_CORRUPT_POOL
:
6107 (void) printf(gettext("status: The pool metadata is corrupted "
6108 "and the pool cannot be opened.\n"));
6109 zpool_explain_recover(zpool_get_handle(zhp
),
6110 zpool_get_name(zhp
), reason
, config
);
6113 case ZPOOL_STATUS_VERSION_OLDER
:
6114 (void) printf(gettext("status: The pool is formatted using a "
6115 "legacy on-disk format. The pool can\n\tstill be used, "
6116 "but some features are unavailable.\n"));
6117 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6118 "upgrade'. Once this is done, the\n\tpool will no longer "
6119 "be accessible on software that does not support\n\t"
6120 "feature flags.\n"));
6123 case ZPOOL_STATUS_VERSION_NEWER
:
6124 (void) printf(gettext("status: The pool has been upgraded to a "
6125 "newer, incompatible on-disk version.\n\tThe pool cannot "
6126 "be accessed on this system.\n"));
6127 (void) printf(gettext("action: Access the pool from a system "
6128 "running more recent software, or\n\trestore the pool from "
6132 case ZPOOL_STATUS_FEAT_DISABLED
:
6133 (void) printf(gettext("status: Some supported features are not "
6134 "enabled on the pool. The pool can\n\tstill be used, but "
6135 "some features are unavailable.\n"));
6136 (void) printf(gettext("action: Enable all features using "
6137 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6138 "longer be accessible by software that does not support\n\t"
6139 "the features. See zpool-features(5) for details.\n"));
6142 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
6143 (void) printf(gettext("status: The pool cannot be accessed on "
6144 "this system because it uses the\n\tfollowing feature(s) "
6145 "not supported on this system:\n"));
6146 zpool_print_unsup_feat(config
);
6147 (void) printf("\n");
6148 (void) printf(gettext("action: Access the pool from a system "
6149 "that supports the required feature(s),\n\tor restore the "
6150 "pool from backup.\n"));
6153 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
6154 (void) printf(gettext("status: The pool can only be accessed "
6155 "in read-only mode on this system. It\n\tcannot be "
6156 "accessed in read-write mode because it uses the "
6157 "following\n\tfeature(s) not supported on this system:\n"));
6158 zpool_print_unsup_feat(config
);
6159 (void) printf("\n");
6160 (void) printf(gettext("action: The pool cannot be accessed in "
6161 "read-write mode. Import the pool with\n"
6162 "\t\"-o readonly=on\", access the pool from a system that "
6163 "supports the\n\trequired feature(s), or restore the "
6164 "pool from backup.\n"));
6167 case ZPOOL_STATUS_FAULTED_DEV_R
:
6168 (void) printf(gettext("status: One or more devices are "
6169 "faulted in response to persistent errors.\n\tSufficient "
6170 "replicas exist for the pool to continue functioning "
6171 "in a\n\tdegraded state.\n"));
6172 (void) printf(gettext("action: Replace the faulted device, "
6173 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6176 case ZPOOL_STATUS_FAULTED_DEV_NR
:
6177 (void) printf(gettext("status: One or more devices are "
6178 "faulted in response to persistent errors. There are "
6179 "insufficient replicas for the pool to\n\tcontinue "
6181 (void) printf(gettext("action: Destroy and re-create the pool "
6182 "from a backup source. Manually marking the device\n"
6183 "\trepaired using 'zpool clear' may allow some data "
6184 "to be recovered.\n"));
6187 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
6188 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
6189 (void) printf(gettext("status: One or more devices are "
6190 "faulted in response to IO failures.\n"));
6191 (void) printf(gettext("action: Make sure the affected devices "
6192 "are connected, then run 'zpool clear'.\n"));
6195 case ZPOOL_STATUS_BAD_LOG
:
6196 (void) printf(gettext("status: An intent log record "
6197 "could not be read.\n"
6198 "\tWaiting for administrator intervention to fix the "
6199 "faulted pool.\n"));
6200 (void) printf(gettext("action: Either restore the affected "
6201 "device(s) and run 'zpool online',\n"
6202 "\tor ignore the intent log records by running "
6203 "'zpool clear'.\n"));
6206 case ZPOOL_STATUS_HOSTID_MISMATCH
:
6207 (void) printf(gettext("status: Mismatch between pool hostid "
6208 "and system hostid on imported pool.\n\tThis pool was "
6209 "previously imported into a system with a different "
6210 "hostid,\n\tand then was verbatim imported into this "
6212 (void) printf(gettext("action: Export this pool on all systems "
6213 "on which it is imported.\n"
6214 "\tThen import it to correct the mismatch.\n"));
6217 case ZPOOL_STATUS_ERRATA
:
6218 (void) printf(gettext("status: Errata #%d detected.\n"),
6222 case ZPOOL_ERRATA_NONE
:
6225 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
6226 (void) printf(gettext("action: To correct the issue "
6227 "run 'zpool scrub'.\n"));
6232 * All errata which allow the pool to be imported
6233 * must contain an action message.
6241 * The remaining errors can't actually be generated, yet.
6243 assert(reason
== ZPOOL_STATUS_OK
);
6247 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6250 if (config
!= NULL
) {
6252 nvlist_t
**spares
, **l2cache
;
6253 uint_t nspares
, nl2cache
;
6254 pool_scan_stat_t
*ps
= NULL
;
6256 (void) nvlist_lookup_uint64_array(nvroot
,
6257 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
6258 print_scan_status(ps
);
6260 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
6261 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6262 if (cbp
->cb_namewidth
< 10)
6263 cbp
->cb_namewidth
= 10;
6265 (void) printf(gettext("config:\n\n"));
6266 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6267 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
6270 if (cbp
->vcdl
!= NULL
)
6271 print_cmd_columns(cbp
->vcdl
, 0);
6274 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
6277 if (num_logs(nvroot
) > 0)
6278 print_logs(zhp
, cbp
, nvroot
);
6279 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6280 &l2cache
, &nl2cache
) == 0)
6281 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
6283 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6284 &spares
, &nspares
) == 0)
6285 print_spares(zhp
, cbp
, spares
, nspares
);
6287 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
6289 nvlist_t
*nverrlist
= NULL
;
6292 * If the approximate error count is small, get a
6293 * precise count by fetching the entire log and
6294 * uniquifying the results.
6296 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
6297 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
6302 while ((elem
= nvlist_next_nvpair(nverrlist
,
6307 nvlist_free(nverrlist
);
6309 (void) printf("\n");
6312 (void) printf(gettext("errors: No known data "
6314 else if (!cbp
->cb_verbose
)
6315 (void) printf(gettext("errors: %llu data "
6316 "errors, use '-v' for a list\n"),
6317 (u_longlong_t
)nerr
);
6319 print_error_log(zhp
);
6322 if (cbp
->cb_dedup_stats
)
6323 print_dedup_stats(config
);
6325 (void) printf(gettext("config: The configuration cannot be "
6333 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6334 * [interval [count]]
6336 * -c CMD For each vdev, run command CMD
6337 * -g Display guid for individual vdev name.
6338 * -L Follow links when resolving vdev path name.
6339 * -P Display full path for vdev name.
6340 * -v Display complete error logs
6341 * -x Display only pools with potential problems
6342 * -D Display dedup status (undocumented)
6343 * -T Display a timestamp in date(1) or Unix format
6345 * Describes the health status of all pools or some subset.
6348 zpool_do_status(int argc
, char **argv
)
6353 unsigned long count
= 0;
6354 status_cbdata_t cb
= { 0 };
6358 while ((c
= getopt(argc
, argv
, "c:gLPvxDT:")) != -1) {
6363 gettext("Can't set -c flag twice\n"));
6366 if ((getuid() <= 0 || geteuid() <= 0) &&
6367 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6368 fprintf(stderr
, gettext(
6369 "Can't run -c with root privileges "
6370 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6376 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6379 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6382 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6385 cb
.cb_verbose
= B_TRUE
;
6388 cb
.cb_explain
= B_TRUE
;
6391 cb
.cb_dedup_stats
= B_TRUE
;
6394 get_timestamp_arg(*optarg
);
6397 if (optopt
== 'c') {
6398 fprintf(stderr
, gettext(
6399 "Current scripts in %s:\n"),
6401 print_zpool_script_list();
6405 gettext("invalid option '%c'\n"), optopt
);
6414 get_interval_count(&argc
, argv
, &interval
, &count
);
6417 cb
.cb_allpools
= B_TRUE
;
6419 cb
.cb_first
= B_TRUE
;
6420 cb
.cb_print_status
= B_TRUE
;
6423 if (timestamp_fmt
!= NODATE
)
6424 print_timestamp(timestamp_fmt
);
6427 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
6430 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
6431 status_callback
, &cb
);
6433 if (cb
.vcdl
!= NULL
)
6434 free_vdev_cmd_data_list(cb
.vcdl
);
6436 if (argc
== 0 && cb
.cb_count
== 0)
6437 (void) fprintf(stderr
, gettext("no pools available\n"));
6438 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
6439 (void) printf(gettext("all pools are healthy\n"));
6447 if (count
!= 0 && --count
== 0)
6450 (void) fsleep(interval
);
6456 typedef struct upgrade_cbdata
{
6459 uint64_t cb_version
;
6464 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
6466 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
6467 int *count
= (int *)unsupp_fs
;
6469 if (zfs_version
> ZPL_VERSION
) {
6470 (void) printf(gettext("%s (v%d) is not supported by this "
6471 "implementation of ZFS.\n"),
6472 zfs_get_name(zhp
), zfs_version
);
6476 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
6484 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
6488 uint64_t oldversion
;
6491 config
= zpool_get_config(zhp
, NULL
);
6492 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6495 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
6496 assert(oldversion
< version
);
6498 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
6503 (void) fprintf(stderr
, gettext("Upgrade not performed due "
6504 "to %d unsupported filesystems (max v%d).\n"),
6505 unsupp_fs
, (int)ZPL_VERSION
);
6509 ret
= zpool_upgrade(zhp
, version
);
6513 if (version
>= SPA_VERSION_FEATURES
) {
6514 (void) printf(gettext("Successfully upgraded "
6515 "'%s' from version %llu to feature flags.\n"),
6516 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
6518 (void) printf(gettext("Successfully upgraded "
6519 "'%s' from version %llu to version %llu.\n"),
6520 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
6521 (u_longlong_t
)version
);
6528 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
6531 boolean_t firstff
= B_TRUE
;
6532 nvlist_t
*enabled
= zpool_get_features(zhp
);
6535 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6536 const char *fname
= spa_feature_table
[i
].fi_uname
;
6537 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6538 if (!nvlist_exists(enabled
, fguid
)) {
6540 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
6541 ret
= zpool_set_prop(zhp
, propname
,
6542 ZFS_FEATURE_ENABLED
);
6550 (void) printf(gettext("Enabled the "
6551 "following features on '%s':\n"),
6552 zpool_get_name(zhp
));
6555 (void) printf(gettext(" %s\n"), fname
);
6566 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
6568 upgrade_cbdata_t
*cbp
= arg
;
6571 boolean_t printnl
= B_FALSE
;
6574 config
= zpool_get_config(zhp
, NULL
);
6575 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6578 assert(SPA_VERSION_IS_SUPPORTED(version
));
6580 if (version
< cbp
->cb_version
) {
6581 cbp
->cb_first
= B_FALSE
;
6582 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6588 * If they did "zpool upgrade -a", then we could
6589 * be doing ioctls to different pools. We need
6590 * to log this history once to each pool, and bypass
6591 * the normal history logging that happens in main().
6593 (void) zpool_log_history(g_zfs
, history_str
);
6594 log_history
= B_FALSE
;
6597 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6599 ret
= upgrade_enable_all(zhp
, &count
);
6604 cbp
->cb_first
= B_FALSE
;
6610 (void) printf(gettext("\n"));
6617 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
6619 upgrade_cbdata_t
*cbp
= arg
;
6623 config
= zpool_get_config(zhp
, NULL
);
6624 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6627 assert(SPA_VERSION_IS_SUPPORTED(version
));
6629 if (version
< SPA_VERSION_FEATURES
) {
6630 if (cbp
->cb_first
) {
6631 (void) printf(gettext("The following pools are "
6632 "formatted with legacy version numbers and can\n"
6633 "be upgraded to use feature flags. After "
6634 "being upgraded, these pools\nwill no "
6635 "longer be accessible by software that does not "
6636 "support feature\nflags.\n\n"));
6637 (void) printf(gettext("VER POOL\n"));
6638 (void) printf(gettext("--- ------------\n"));
6639 cbp
->cb_first
= B_FALSE
;
6642 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
6643 zpool_get_name(zhp
));
6650 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
6652 upgrade_cbdata_t
*cbp
= arg
;
6656 config
= zpool_get_config(zhp
, NULL
);
6657 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6660 if (version
>= SPA_VERSION_FEATURES
) {
6662 boolean_t poolfirst
= B_TRUE
;
6663 nvlist_t
*enabled
= zpool_get_features(zhp
);
6665 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6666 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6667 const char *fname
= spa_feature_table
[i
].fi_uname
;
6668 if (!nvlist_exists(enabled
, fguid
)) {
6669 if (cbp
->cb_first
) {
6670 (void) printf(gettext("\nSome "
6671 "supported features are not "
6672 "enabled on the following pools. "
6673 "Once a\nfeature is enabled the "
6674 "pool may become incompatible with "
6675 "software\nthat does not support "
6677 "zpool-features(5) for "
6679 (void) printf(gettext("POOL "
6681 (void) printf(gettext("------"
6683 cbp
->cb_first
= B_FALSE
;
6687 (void) printf(gettext("%s\n"),
6688 zpool_get_name(zhp
));
6689 poolfirst
= B_FALSE
;
6692 (void) printf(gettext(" %s\n"), fname
);
6695 * If they did "zpool upgrade -a", then we could
6696 * be doing ioctls to different pools. We need
6697 * to log this history once to each pool, and bypass
6698 * the normal history logging that happens in main().
6700 (void) zpool_log_history(g_zfs
, history_str
);
6701 log_history
= B_FALSE
;
6710 upgrade_one(zpool_handle_t
*zhp
, void *data
)
6712 boolean_t printnl
= B_FALSE
;
6713 upgrade_cbdata_t
*cbp
= data
;
6714 uint64_t cur_version
;
6717 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
6718 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
6719 "Pool 'log' must be renamed using export and import"
6724 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
6725 if (cur_version
> cbp
->cb_version
) {
6726 (void) printf(gettext("Pool '%s' is already formatted "
6727 "using more current version '%llu'.\n\n"),
6728 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
6732 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
6733 (void) printf(gettext("Pool '%s' is already formatted "
6734 "using version %llu.\n\n"), zpool_get_name(zhp
),
6735 (u_longlong_t
)cbp
->cb_version
);
6739 if (cur_version
!= cbp
->cb_version
) {
6741 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6746 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6748 ret
= upgrade_enable_all(zhp
, &count
);
6754 } else if (cur_version
== SPA_VERSION
) {
6755 (void) printf(gettext("Pool '%s' already has all "
6756 "supported features enabled.\n"),
6757 zpool_get_name(zhp
));
6762 (void) printf(gettext("\n"));
6771 * zpool upgrade [-V version] <-a | pool ...>
6773 * With no arguments, display downrev'd ZFS pool available for upgrade.
6774 * Individual pools can be upgraded by specifying the pool, and '-a' will
6775 * upgrade all pools.
6778 zpool_do_upgrade(int argc
, char **argv
)
6781 upgrade_cbdata_t cb
= { 0 };
6783 boolean_t showversions
= B_FALSE
;
6784 boolean_t upgradeall
= B_FALSE
;
6789 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
6792 upgradeall
= B_TRUE
;
6795 showversions
= B_TRUE
;
6798 cb
.cb_version
= strtoll(optarg
, &end
, 10);
6800 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
6801 (void) fprintf(stderr
,
6802 gettext("invalid version '%s'\n"), optarg
);
6807 (void) fprintf(stderr
, gettext("missing argument for "
6808 "'%c' option\n"), optopt
);
6812 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6823 if (cb
.cb_version
== 0) {
6824 cb
.cb_version
= SPA_VERSION
;
6825 } else if (!upgradeall
&& argc
== 0) {
6826 (void) fprintf(stderr
, gettext("-V option is "
6827 "incompatible with other arguments\n"));
6832 if (upgradeall
|| argc
!= 0) {
6833 (void) fprintf(stderr
, gettext("-v option is "
6834 "incompatible with other arguments\n"));
6837 } else if (upgradeall
) {
6839 (void) fprintf(stderr
, gettext("-a option should not "
6840 "be used along with a pool name\n"));
6845 (void) printf(gettext("This system supports ZFS pool feature "
6850 (void) printf(gettext("The following features are "
6852 (void) printf(gettext("FEAT DESCRIPTION\n"));
6853 (void) printf("----------------------------------------------"
6854 "---------------\n");
6855 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6856 zfeature_info_t
*fi
= &spa_feature_table
[i
];
6858 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
6859 " (read-only compatible)" : "";
6861 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
6862 (void) printf(" %s\n", fi
->fi_desc
);
6864 (void) printf("\n");
6866 (void) printf(gettext("The following legacy versions are also "
6868 (void) printf(gettext("VER DESCRIPTION\n"));
6869 (void) printf("--- -----------------------------------------"
6870 "---------------\n");
6871 (void) printf(gettext(" 1 Initial ZFS version\n"));
6872 (void) printf(gettext(" 2 Ditto blocks "
6873 "(replicated metadata)\n"));
6874 (void) printf(gettext(" 3 Hot spares and double parity "
6876 (void) printf(gettext(" 4 zpool history\n"));
6877 (void) printf(gettext(" 5 Compression using the gzip "
6879 (void) printf(gettext(" 6 bootfs pool property\n"));
6880 (void) printf(gettext(" 7 Separate intent log devices\n"));
6881 (void) printf(gettext(" 8 Delegated administration\n"));
6882 (void) printf(gettext(" 9 refquota and refreservation "
6884 (void) printf(gettext(" 10 Cache devices\n"));
6885 (void) printf(gettext(" 11 Improved scrub performance\n"));
6886 (void) printf(gettext(" 12 Snapshot properties\n"));
6887 (void) printf(gettext(" 13 snapused property\n"));
6888 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
6889 (void) printf(gettext(" 15 user/group space accounting\n"));
6890 (void) printf(gettext(" 16 stmf property support\n"));
6891 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
6892 (void) printf(gettext(" 18 Snapshot user holds\n"));
6893 (void) printf(gettext(" 19 Log device removal\n"));
6894 (void) printf(gettext(" 20 Compression using zle "
6895 "(zero-length encoding)\n"));
6896 (void) printf(gettext(" 21 Deduplication\n"));
6897 (void) printf(gettext(" 22 Received properties\n"));
6898 (void) printf(gettext(" 23 Slim ZIL\n"));
6899 (void) printf(gettext(" 24 System attributes\n"));
6900 (void) printf(gettext(" 25 Improved scrub stats\n"));
6901 (void) printf(gettext(" 26 Improved snapshot deletion "
6903 (void) printf(gettext(" 27 Improved snapshot creation "
6905 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
6906 (void) printf(gettext("\nFor more information on a particular "
6907 "version, including supported releases,\n"));
6908 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
6909 } else if (argc
== 0 && upgradeall
) {
6910 cb
.cb_first
= B_TRUE
;
6911 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
6912 if (ret
== 0 && cb
.cb_first
) {
6913 if (cb
.cb_version
== SPA_VERSION
) {
6914 (void) printf(gettext("All pools are already "
6915 "formatted using feature flags.\n\n"));
6916 (void) printf(gettext("Every feature flags "
6917 "pool already has all supported features "
6920 (void) printf(gettext("All pools are already "
6921 "formatted with version %llu or higher.\n"),
6922 (u_longlong_t
)cb
.cb_version
);
6925 } else if (argc
== 0) {
6926 cb
.cb_first
= B_TRUE
;
6927 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
6931 (void) printf(gettext("All pools are formatted "
6932 "using feature flags.\n\n"));
6934 (void) printf(gettext("\nUse 'zpool upgrade -v' "
6935 "for a list of available legacy versions.\n"));
6938 cb
.cb_first
= B_TRUE
;
6939 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
6943 (void) printf(gettext("Every feature flags pool has "
6944 "all supported features enabled.\n"));
6946 (void) printf(gettext("\n"));
6949 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
6956 typedef struct hist_cbdata
{
6963 * Print out the command history for a specific pool.
6966 get_history_one(zpool_handle_t
*zhp
, void *data
)
6972 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
6974 cb
->first
= B_FALSE
;
6976 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
6978 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
6981 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
6982 &records
, &numrecords
) == 0);
6983 for (i
= 0; i
< numrecords
; i
++) {
6984 nvlist_t
*rec
= records
[i
];
6987 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
6991 tsec
= fnvlist_lookup_uint64(records
[i
],
6993 (void) localtime_r(&tsec
, &t
);
6994 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
6997 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
6998 (void) printf("%s %s", tbuf
,
6999 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
7000 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
7002 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
7005 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
7006 (void) printf("%s unrecognized record:\n",
7008 dump_nvlist(rec
, 4);
7011 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
7012 zfs_history_event_names
[ievent
],
7013 (longlong_t
)fnvlist_lookup_uint64(
7014 rec
, ZPOOL_HIST_TXG
),
7015 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
7016 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
7019 (void) printf("%s [txg:%lld] %s", tbuf
,
7020 (longlong_t
)fnvlist_lookup_uint64(
7021 rec
, ZPOOL_HIST_TXG
),
7022 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
7023 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
7024 (void) printf(" %s (%llu)",
7025 fnvlist_lookup_string(rec
,
7027 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
7030 (void) printf(" %s", fnvlist_lookup_string(rec
,
7031 ZPOOL_HIST_INT_STR
));
7032 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
7035 (void) printf("%s ioctl %s\n", tbuf
,
7036 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
7037 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
7038 (void) printf(" input:\n");
7039 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7040 ZPOOL_HIST_INPUT_NVL
), 8);
7042 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
7043 (void) printf(" output:\n");
7044 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7045 ZPOOL_HIST_OUTPUT_NVL
), 8);
7050 (void) printf("%s unrecognized record:\n", tbuf
);
7051 dump_nvlist(rec
, 4);
7055 (void) printf("\n");
7058 (void) printf(" [");
7059 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
7060 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
7061 struct passwd
*pwd
= getpwuid(who
);
7062 (void) printf("user %d ", (int)who
);
7064 (void) printf("(%s) ", pwd
->pw_name
);
7066 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
7067 (void) printf("on %s",
7068 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
7070 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
7071 (void) printf(":%s",
7072 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
7076 (void) printf("\n");
7078 (void) printf("\n");
7085 * zpool history <pool>
7087 * Displays the history of commands that modified pools.
7090 zpool_do_history(int argc
, char **argv
)
7092 hist_cbdata_t cbdata
= { 0 };
7096 cbdata
.first
= B_TRUE
;
7098 while ((c
= getopt(argc
, argv
, "li")) != -1) {
7101 cbdata
.longfmt
= B_TRUE
;
7104 cbdata
.internal
= B_TRUE
;
7107 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7115 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
7118 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
7119 (void) fprintf(stderr
, gettext("no pools available\n"));
7126 typedef struct ev_opts
{
7134 zpool_do_events_short(nvlist_t
*nvl
)
7136 char ctime_str
[26], str
[32], *ptr
;
7140 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
7141 memset(str
, ' ', 32);
7142 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
7143 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
7144 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
7145 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
7146 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
7147 (void) printf(gettext("%s "), str
);
7149 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
7150 (void) printf(gettext("%s\n"), ptr
);
7154 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
7158 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
7159 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
7161 data_type_t type
= nvpair_type(nvp
);
7162 const char *name
= nvpair_name(nvp
);
7172 printf(gettext("%*s%s = "), depth
, "", name
);
7175 case DATA_TYPE_BOOLEAN
:
7176 printf(gettext("%s"), "1");
7179 case DATA_TYPE_BOOLEAN_VALUE
:
7180 (void) nvpair_value_boolean_value(nvp
, &b
);
7181 printf(gettext("%s"), b
? "1" : "0");
7184 case DATA_TYPE_BYTE
:
7185 (void) nvpair_value_byte(nvp
, &i8
);
7186 printf(gettext("0x%x"), i8
);
7189 case DATA_TYPE_INT8
:
7190 (void) nvpair_value_int8(nvp
, (void *)&i8
);
7191 printf(gettext("0x%x"), i8
);
7194 case DATA_TYPE_UINT8
:
7195 (void) nvpair_value_uint8(nvp
, &i8
);
7196 printf(gettext("0x%x"), i8
);
7199 case DATA_TYPE_INT16
:
7200 (void) nvpair_value_int16(nvp
, (void *)&i16
);
7201 printf(gettext("0x%x"), i16
);
7204 case DATA_TYPE_UINT16
:
7205 (void) nvpair_value_uint16(nvp
, &i16
);
7206 printf(gettext("0x%x"), i16
);
7209 case DATA_TYPE_INT32
:
7210 (void) nvpair_value_int32(nvp
, (void *)&i32
);
7211 printf(gettext("0x%x"), i32
);
7214 case DATA_TYPE_UINT32
:
7215 (void) nvpair_value_uint32(nvp
, &i32
);
7216 printf(gettext("0x%x"), i32
);
7219 case DATA_TYPE_INT64
:
7220 (void) nvpair_value_int64(nvp
, (void *)&i64
);
7221 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7224 case DATA_TYPE_UINT64
:
7225 (void) nvpair_value_uint64(nvp
, &i64
);
7227 * translate vdev state values to readable
7228 * strings to aide zpool events consumers
7231 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
7233 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
7234 printf(gettext("\"%s\" (0x%llx)"),
7235 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
7238 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7242 case DATA_TYPE_HRTIME
:
7243 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
7244 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7247 case DATA_TYPE_STRING
:
7248 (void) nvpair_value_string(nvp
, &str
);
7249 printf(gettext("\"%s\""), str
? str
: "<NULL>");
7252 case DATA_TYPE_NVLIST
:
7253 printf(gettext("(embedded nvlist)\n"));
7254 (void) nvpair_value_nvlist(nvp
, &cnv
);
7255 zpool_do_events_nvprint(cnv
, depth
+ 8);
7256 printf(gettext("%*s(end %s)"), depth
, "", name
);
7259 case DATA_TYPE_NVLIST_ARRAY
: {
7263 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
7264 printf(gettext("(%d embedded nvlists)\n"), nelem
);
7265 for (i
= 0; i
< nelem
; i
++) {
7266 printf(gettext("%*s%s[%d] = %s\n"),
7267 depth
, "", name
, i
, "(embedded nvlist)");
7268 zpool_do_events_nvprint(val
[i
], depth
+ 8);
7269 printf(gettext("%*s(end %s[%i])\n"),
7270 depth
, "", name
, i
);
7272 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
7276 case DATA_TYPE_INT8_ARRAY
: {
7280 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
7281 for (i
= 0; i
< nelem
; i
++)
7282 printf(gettext("0x%x "), val
[i
]);
7287 case DATA_TYPE_UINT8_ARRAY
: {
7291 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
7292 for (i
= 0; i
< nelem
; i
++)
7293 printf(gettext("0x%x "), val
[i
]);
7298 case DATA_TYPE_INT16_ARRAY
: {
7302 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
7303 for (i
= 0; i
< nelem
; i
++)
7304 printf(gettext("0x%x "), val
[i
]);
7309 case DATA_TYPE_UINT16_ARRAY
: {
7313 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
7314 for (i
= 0; i
< nelem
; i
++)
7315 printf(gettext("0x%x "), val
[i
]);
7320 case DATA_TYPE_INT32_ARRAY
: {
7324 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
7325 for (i
= 0; i
< nelem
; i
++)
7326 printf(gettext("0x%x "), val
[i
]);
7331 case DATA_TYPE_UINT32_ARRAY
: {
7335 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
7336 for (i
= 0; i
< nelem
; i
++)
7337 printf(gettext("0x%x "), val
[i
]);
7342 case DATA_TYPE_INT64_ARRAY
: {
7346 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
7347 for (i
= 0; i
< nelem
; i
++)
7348 printf(gettext("0x%llx "),
7349 (u_longlong_t
)val
[i
]);
7354 case DATA_TYPE_UINT64_ARRAY
: {
7358 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
7359 for (i
= 0; i
< nelem
; i
++)
7360 printf(gettext("0x%llx "),
7361 (u_longlong_t
)val
[i
]);
7366 case DATA_TYPE_STRING_ARRAY
: {
7370 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
7371 for (i
= 0; i
< nelem
; i
++)
7372 printf(gettext("\"%s\" "),
7373 str
[i
] ? str
[i
] : "<NULL>");
7378 case DATA_TYPE_BOOLEAN_ARRAY
:
7379 case DATA_TYPE_BYTE_ARRAY
:
7380 case DATA_TYPE_DOUBLE
:
7381 case DATA_TYPE_UNKNOWN
:
7382 printf(gettext("<unknown>"));
7386 printf(gettext("\n"));
7391 zpool_do_events_next(ev_opts_t
*opts
)
7394 int zevent_fd
, ret
, dropped
;
7396 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
7397 VERIFY(zevent_fd
>= 0);
7399 if (!opts
->scripted
)
7400 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7403 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
7404 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
7405 if (ret
|| nvl
== NULL
)
7409 (void) printf(gettext("dropped %d events\n"), dropped
);
7411 zpool_do_events_short(nvl
);
7413 if (opts
->verbose
) {
7414 zpool_do_events_nvprint(nvl
, 8);
7415 printf(gettext("\n"));
7417 (void) fflush(stdout
);
7422 VERIFY(0 == close(zevent_fd
));
7428 zpool_do_events_clear(ev_opts_t
*opts
)
7432 ret
= zpool_events_clear(g_zfs
, &count
);
7434 (void) printf(gettext("cleared %d events\n"), count
);
7440 * zpool events [-vfc]
7442 * Displays events logs by ZFS.
7445 zpool_do_events(int argc
, char **argv
)
7447 ev_opts_t opts
= { 0 };
7452 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
7467 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7476 ret
= zpool_do_events_clear(&opts
);
7478 ret
= zpool_do_events_next(&opts
);
7484 get_callback(zpool_handle_t
*zhp
, void *data
)
7486 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
7487 char value
[MAXNAMELEN
];
7488 zprop_source_t srctype
;
7491 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
7494 * Skip the special fake placeholder. This will also skip
7495 * over the name property when 'all' is specified.
7497 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
7498 pl
== cbp
->cb_proplist
)
7501 if (pl
->pl_prop
== ZPROP_INVAL
&&
7502 (zpool_prop_feature(pl
->pl_user_prop
) ||
7503 zpool_prop_unsupported(pl
->pl_user_prop
))) {
7504 srctype
= ZPROP_SRC_LOCAL
;
7506 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
7507 value
, sizeof (value
)) == 0) {
7508 zprop_print_one_property(zpool_get_name(zhp
),
7509 cbp
, pl
->pl_user_prop
, value
, srctype
,
7513 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
7514 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
7517 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
7518 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
7526 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7528 * -H Scripted mode. Don't display headers, and separate properties
7530 * -o List of columns to display. Defaults to
7531 * "name,property,value,source".
7532 * -p Display values in parsable (exact) format.
7534 * Get properties of pools in the system. Output space statistics
7535 * for each one as well as other attributes.
7538 zpool_do_get(int argc
, char **argv
)
7540 zprop_get_cbdata_t cb
= { 0 };
7541 zprop_list_t fake_name
= { 0 };
7546 cb
.cb_first
= B_TRUE
;
7549 * Set up default columns and sources.
7551 cb
.cb_sources
= ZPROP_SRC_ALL
;
7552 cb
.cb_columns
[0] = GET_COL_NAME
;
7553 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7554 cb
.cb_columns
[2] = GET_COL_VALUE
;
7555 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7556 cb
.cb_type
= ZFS_TYPE_POOL
;
7559 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
7562 cb
.cb_literal
= B_TRUE
;
7565 cb
.cb_scripted
= B_TRUE
;
7568 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
7570 while (*optarg
!= '\0') {
7571 static char *col_subopts
[] =
7572 { "name", "property", "value", "source",
7575 if (i
== ZFS_GET_NCOLS
) {
7576 (void) fprintf(stderr
, gettext("too "
7577 "many fields given to -o "
7582 switch (getsubopt(&optarg
, col_subopts
,
7585 cb
.cb_columns
[i
++] = GET_COL_NAME
;
7588 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
7591 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
7594 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
7598 (void) fprintf(stderr
,
7599 gettext("\"all\" conflicts "
7600 "with specific fields "
7601 "given to -o option\n"));
7604 cb
.cb_columns
[0] = GET_COL_NAME
;
7605 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7606 cb
.cb_columns
[2] = GET_COL_VALUE
;
7607 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7611 (void) fprintf(stderr
,
7612 gettext("invalid column name "
7619 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7629 (void) fprintf(stderr
, gettext("missing property "
7634 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
7635 ZFS_TYPE_POOL
) != 0)
7641 if (cb
.cb_proplist
!= NULL
) {
7642 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
7643 fake_name
.pl_width
= strlen(gettext("NAME"));
7644 fake_name
.pl_next
= cb
.cb_proplist
;
7645 cb
.cb_proplist
= &fake_name
;
7648 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
7651 if (cb
.cb_proplist
== &fake_name
)
7652 zprop_free_list(fake_name
.pl_next
);
7654 zprop_free_list(cb
.cb_proplist
);
7659 typedef struct set_cbdata
{
7662 boolean_t cb_any_successful
;
7666 set_callback(zpool_handle_t
*zhp
, void *data
)
7669 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
7671 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
7674 cb
->cb_any_successful
= B_TRUE
;
7680 zpool_do_set(int argc
, char **argv
)
7682 set_cbdata_t cb
= { 0 };
7685 if (argc
> 1 && argv
[1][0] == '-') {
7686 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7692 (void) fprintf(stderr
, gettext("missing property=value "
7698 (void) fprintf(stderr
, gettext("missing pool name\n"));
7703 (void) fprintf(stderr
, gettext("too many pool names\n"));
7707 cb
.cb_propname
= argv
[1];
7708 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
7709 if (cb
.cb_value
== NULL
) {
7710 (void) fprintf(stderr
, gettext("missing value in "
7711 "property=value argument\n"));
7715 *(cb
.cb_value
) = '\0';
7718 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
7725 find_command_idx(char *command
, int *idx
)
7729 for (i
= 0; i
< NCOMMAND
; i
++) {
7730 if (command_table
[i
].name
== NULL
)
7733 if (strcmp(command
, command_table
[i
].name
) == 0) {
7742 main(int argc
, char **argv
)
7748 (void) setlocale(LC_ALL
, "");
7749 (void) textdomain(TEXT_DOMAIN
);
7752 dprintf_setup(&argc
, argv
);
7757 * Make sure the user has specified some command.
7760 (void) fprintf(stderr
, gettext("missing command\n"));
7769 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
7772 if ((g_zfs
= libzfs_init()) == NULL
) {
7773 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7777 libzfs_print_on_error(g_zfs
, B_TRUE
);
7779 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7782 * Run the appropriate command.
7784 if (find_command_idx(cmdname
, &i
) == 0) {
7785 current_command
= &command_table
[i
];
7786 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7787 } else if (strchr(cmdname
, '=')) {
7788 verify(find_command_idx("set", &i
) == 0);
7789 current_command
= &command_table
[i
];
7790 ret
= command_table
[i
].func(argc
, argv
);
7791 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
7793 * 'freeze' is a vile debugging abomination, so we treat
7797 int fd
= open(ZFS_DEV
, O_RDWR
);
7798 (void) strlcpy((void *)buf
, argv
[2], sizeof (buf
));
7799 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
7801 (void) fprintf(stderr
, gettext("unrecognized "
7802 "command '%s'\n"), cmdname
);
7807 if (ret
== 0 && log_history
)
7808 (void) zpool_log_history(g_zfs
, history_str
);
7813 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7814 * for the purposes of running ::findleaks.
7816 if (getenv("ZFS_ABORT") != NULL
) {
7817 (void) printf("dumping core by request\n");