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) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
47 #include <sys/fs/zfs.h>
52 #include "zpool_util.h"
53 #include "zfs_comutil.h"
54 #include "zfeature_common.h"
56 #include "statcommon.h"
58 static int zpool_do_create(int, char **);
59 static int zpool_do_destroy(int, char **);
61 static int zpool_do_add(int, char **);
62 static int zpool_do_remove(int, char **);
64 static int zpool_do_list(int, char **);
65 static int zpool_do_iostat(int, char **);
66 static int zpool_do_status(int, char **);
68 static int zpool_do_online(int, char **);
69 static int zpool_do_offline(int, char **);
70 static int zpool_do_clear(int, char **);
71 static int zpool_do_reopen(int, char **);
73 static int zpool_do_reguid(int, char **);
75 static int zpool_do_attach(int, char **);
76 static int zpool_do_detach(int, char **);
77 static int zpool_do_replace(int, char **);
78 static int zpool_do_split(int, char **);
80 static int zpool_do_scrub(int, char **);
82 static int zpool_do_import(int, char **);
83 static int zpool_do_export(int, char **);
85 static int zpool_do_upgrade(int, char **);
87 static int zpool_do_history(int, char **);
89 static int zpool_do_get(int, char **);
90 static int zpool_do_set(int, char **);
93 * These libumem hooks provide a reasonable set of defaults for the allocator's
94 * debugging facilities.
99 _umem_debug_init(void)
101 return ("default,verbose"); /* $UMEM_DEBUG setting */
105 _umem_logging_init(void)
107 return ("fail,contents"); /* $UMEM_LOGGING setting */
138 typedef struct zpool_command
{
140 int (*func
)(int, char **);
145 * Master command table. Each ZFS command has a name, associated function, and
146 * usage message. The usage messages need to be internationalized, so we have
147 * to have a function to return the usage message based on a command index.
149 * These commands are organized according to how they are displayed in the usage
150 * message. An empty command (one with a NULL name) indicates an empty line in
151 * the generic usage message.
153 static zpool_command_t command_table
[] = {
154 { "create", zpool_do_create
, HELP_CREATE
},
155 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
157 { "add", zpool_do_add
, HELP_ADD
},
158 { "remove", zpool_do_remove
, HELP_REMOVE
},
160 { "list", zpool_do_list
, HELP_LIST
},
161 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
162 { "status", zpool_do_status
, HELP_STATUS
},
164 { "online", zpool_do_online
, HELP_ONLINE
},
165 { "offline", zpool_do_offline
, HELP_OFFLINE
},
166 { "clear", zpool_do_clear
, HELP_CLEAR
},
167 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
169 { "attach", zpool_do_attach
, HELP_ATTACH
},
170 { "detach", zpool_do_detach
, HELP_DETACH
},
171 { "replace", zpool_do_replace
, HELP_REPLACE
},
172 { "split", zpool_do_split
, HELP_SPLIT
},
174 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
176 { "import", zpool_do_import
, HELP_IMPORT
},
177 { "export", zpool_do_export
, HELP_EXPORT
},
178 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
179 { "reguid", zpool_do_reguid
, HELP_REGUID
},
181 { "history", zpool_do_history
, HELP_HISTORY
},
182 { "get", zpool_do_get
, HELP_GET
},
183 { "set", zpool_do_set
, HELP_SET
},
186 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
188 static zpool_command_t
*current_command
;
189 static char history_str
[HIS_MAX_RECORD_LEN
];
190 static boolean_t log_history
= B_TRUE
;
191 static uint_t timestamp_fmt
= NODATE
;
194 get_usage(zpool_help_t idx
) {
197 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
199 return (gettext("\tattach [-f] <pool> <device> "
202 return (gettext("\tclear [-nF] <pool> [device]\n"));
204 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
205 "\t [-O file-system-property=value] ... \n"
206 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
208 return (gettext("\tdestroy [-f] <pool>\n"));
210 return (gettext("\tdetach <pool> <device>\n"));
212 return (gettext("\texport [-f] <pool> ...\n"));
214 return (gettext("\thistory [-il] [<pool>] ...\n"));
216 return (gettext("\timport [-d dir] [-D]\n"
217 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
218 "\timport [-o mntopts] [-o property=value] ... \n"
219 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
220 "[-R root] [-F [-n]] -a\n"
221 "\timport [-o mntopts] [-o property=value] ... \n"
222 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
223 "[-R root] [-F [-n]]\n"
224 "\t <pool | id> [newpool]\n"));
226 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
229 return (gettext("\tlist [-H] [-o property[,...]] "
230 "[-T d|u] [pool] ... [interval [count]]\n"));
232 return (gettext("\toffline [-t] <pool> <device> ...\n"));
234 return (gettext("\tonline <pool> <device> ...\n"));
236 return (gettext("\treplace [-f] <pool> <device> "
239 return (gettext("\tremove <pool> <device> ...\n"));
241 return (""); /* Undocumented command */
243 return (gettext("\tscrub [-s] <pool> ...\n"));
245 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
248 return (gettext("\tupgrade\n"
250 "\tupgrade [-V version] <-a | pool ...>\n"));
252 return (gettext("\tget <\"all\" | property[,...]> "
255 return (gettext("\tset <property=value> <pool> \n"));
257 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
258 "\t [-o property=value] <pool> <newpool> "
259 "[<device> ...]\n"));
261 return (gettext("\treguid <pool>\n"));
270 * Callback routine that will print out a pool property value.
273 print_prop_cb(int prop
, void *cb
)
277 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
279 if (zpool_prop_readonly(prop
))
280 (void) fprintf(fp
, " NO ");
282 (void) fprintf(fp
, " YES ");
284 if (zpool_prop_values(prop
) == NULL
)
285 (void) fprintf(fp
, "-\n");
287 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
293 * Display usage message. If we're inside a command, display only the usage for
294 * that command. Otherwise, iterate over the entire command table and display
295 * a complete usage message.
298 usage(boolean_t requested
)
300 FILE *fp
= requested
? stdout
: stderr
;
302 if (current_command
== NULL
) {
305 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
307 gettext("where 'command' is one of the following:\n\n"));
309 for (i
= 0; i
< NCOMMAND
; i
++) {
310 if (command_table
[i
].name
== NULL
)
311 (void) fprintf(fp
, "\n");
313 (void) fprintf(fp
, "%s",
314 get_usage(command_table
[i
].usage
));
317 (void) fprintf(fp
, gettext("usage:\n"));
318 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
321 if (current_command
!= NULL
&&
322 ((strcmp(current_command
->name
, "set") == 0) ||
323 (strcmp(current_command
->name
, "get") == 0) ||
324 (strcmp(current_command
->name
, "list") == 0))) {
327 gettext("\nthe following properties are supported:\n"));
329 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
330 "PROPERTY", "EDIT", "VALUES");
332 /* Iterate over all properties */
333 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
336 (void) fprintf(fp
, "\t%-15s ", "feature@...");
337 (void) fprintf(fp
, "YES disabled | enabled | active\n");
339 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
340 "appended with a feature name.\nSee zpool-features(5).\n"));
344 * See comments at end of main().
346 if (getenv("ZFS_ABORT") != NULL
) {
347 (void) printf("dumping core by request\n");
351 exit(requested
? 0 : 2);
355 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
356 boolean_t print_logs
)
363 (void) printf("\t%*s%s\n", indent
, "", name
);
365 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
366 &child
, &children
) != 0)
369 for (c
= 0; c
< children
; c
++) {
370 uint64_t is_log
= B_FALSE
;
372 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
374 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
377 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
378 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
385 * Add a property pair (name, string-value) into a property nvlist.
388 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
391 zpool_prop_t prop
= ZPROP_INVAL
;
397 if (*props
== NULL
&&
398 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
399 (void) fprintf(stderr
,
400 gettext("internal error: out of memory\n"));
407 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
408 !zpool_prop_feature(propname
)) {
409 (void) fprintf(stderr
, gettext("property '%s' is "
410 "not a valid pool property\n"), propname
);
413 if (zpool_prop_feature(propname
))
416 normnm
= zpool_prop_to_name(prop
);
418 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
419 normnm
= zfs_prop_to_name(fprop
);
425 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
426 prop
!= ZPOOL_PROP_CACHEFILE
) {
427 (void) fprintf(stderr
, gettext("property '%s' "
428 "specified multiple times\n"), propname
);
432 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
433 (void) fprintf(stderr
, gettext("internal "
434 "error: out of memory\n"));
442 * zpool add [-fn] <pool> <vdev> ...
444 * -f Force addition of devices, even if they appear in use
445 * -n Do not add the devices, but display the resulting layout if
446 * they were to be added.
448 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
449 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
453 zpool_do_add(int argc
, char **argv
)
455 boolean_t force
= B_FALSE
;
456 boolean_t dryrun
= B_FALSE
;
465 while ((c
= getopt(argc
, argv
, "fn")) != -1) {
474 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
483 /* get pool name and check number of arguments */
485 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
489 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
498 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
501 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
502 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
508 /* pass off to get_vdev_spec for processing */
509 nvroot
= make_root_vdev(zhp
, force
, !force
, B_FALSE
, dryrun
,
511 if (nvroot
== NULL
) {
517 nvlist_t
*poolnvroot
;
519 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
522 (void) printf(gettext("would update '%s' to the following "
523 "configuration:\n"), zpool_get_name(zhp
));
525 /* print original main pool and new tree */
526 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
527 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
529 /* Do the same for the logs */
530 if (num_logs(poolnvroot
) > 0) {
531 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
532 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
533 } else if (num_logs(nvroot
) > 0) {
534 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
539 ret
= (zpool_add(zhp
, nvroot
) != 0);
549 * zpool remove <pool> <vdev> ...
551 * Removes the given vdev from the pool. Currently, this supports removing
552 * spares, cache, and log devices from the pool.
555 zpool_do_remove(int argc
, char **argv
)
564 /* get pool name and check number of arguments */
566 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
570 (void) fprintf(stderr
, gettext("missing device\n"));
576 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
579 for (i
= 1; i
< argc
; i
++) {
580 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
588 * zpool create [-fnd] [-o property=value] ...
589 * [-O file-system-property=value] ...
590 * [-R root] [-m mountpoint] <pool> <dev> ...
592 * -f Force creation, even if devices appear in use
593 * -n Do not create the pool, but display the resulting layout if it
594 * were to be created.
595 * -R Create a pool under an alternate root
596 * -m Set default mountpoint for the root dataset. By default it's
598 * -o Set property=value.
599 * -d Don't automatically enable all supported pool features
600 * (individual features can be enabled with -o).
601 * -O Set fsproperty=value in the pool's root file system
603 * Creates the named pool according to the given vdev specification. The
604 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
605 * we get the nvlist back from get_vdev_spec(), we either print out the contents
606 * (if '-n' was specified), or pass it to libzfs to do the creation.
609 zpool_do_create(int argc
, char **argv
)
611 boolean_t force
= B_FALSE
;
612 boolean_t dryrun
= B_FALSE
;
613 boolean_t enable_all_pool_feat
= B_TRUE
;
615 nvlist_t
*nvroot
= NULL
;
618 char *altroot
= NULL
;
619 char *mountpoint
= NULL
;
620 nvlist_t
*fsprops
= NULL
;
621 nvlist_t
*props
= NULL
;
625 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:")) != -1) {
634 enable_all_pool_feat
= B_FALSE
;
638 if (add_prop_list(zpool_prop_to_name(
639 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
641 if (nvlist_lookup_string(props
,
642 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
645 if (add_prop_list(zpool_prop_to_name(
646 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
653 if ((propval
= strchr(optarg
, '=')) == NULL
) {
654 (void) fprintf(stderr
, gettext("missing "
655 "'=' for -o option\n"));
661 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
665 * If the user is creating a pool that doesn't support
666 * feature flags, don't enable any features.
668 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
672 ver
= strtoull(propval
, &end
, 10);
674 ver
< SPA_VERSION_FEATURES
) {
675 enable_all_pool_feat
= B_FALSE
;
680 if ((propval
= strchr(optarg
, '=')) == NULL
) {
681 (void) fprintf(stderr
, gettext("missing "
682 "'=' for -O option\n"));
688 if (add_prop_list(optarg
, propval
, &fsprops
, B_FALSE
))
692 (void) fprintf(stderr
, gettext("missing argument for "
693 "'%c' option\n"), optopt
);
696 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
705 /* get pool name and check number of arguments */
707 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
711 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
718 * As a special case, check for use of '/' in the name, and direct the
719 * user to use 'zfs create' instead.
721 if (strchr(poolname
, '/') != NULL
) {
722 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
723 "character '/' in pool name\n"), poolname
);
724 (void) fprintf(stderr
, gettext("use 'zfs create' to "
725 "create a dataset\n"));
729 /* pass off to get_vdev_spec for bulk processing */
730 nvroot
= make_root_vdev(NULL
, force
, !force
, B_FALSE
, dryrun
,
735 /* make_root_vdev() allows 0 toplevel children if there are spares */
736 if (!zfs_allocatable_devs(nvroot
)) {
737 (void) fprintf(stderr
, gettext("invalid vdev "
738 "specification: at least one toplevel vdev must be "
743 if (altroot
!= NULL
&& altroot
[0] != '/') {
744 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
745 "must be an absolute path\n"), altroot
);
750 * Check the validity of the mountpoint and direct the user to use the
751 * '-m' mountpoint option if it looks like its in use.
753 if (mountpoint
== NULL
||
754 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
755 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
756 char buf
[MAXPATHLEN
];
759 if (mountpoint
&& mountpoint
[0] != '/') {
760 (void) fprintf(stderr
, gettext("invalid mountpoint "
761 "'%s': must be an absolute path, 'legacy', or "
762 "'none'\n"), mountpoint
);
766 if (mountpoint
== NULL
) {
768 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
771 (void) snprintf(buf
, sizeof (buf
), "/%s",
775 (void) snprintf(buf
, sizeof (buf
), "%s%s",
776 altroot
, mountpoint
);
778 (void) snprintf(buf
, sizeof (buf
), "%s",
782 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
783 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
784 "%s\n"), buf
, strerror(errno
));
785 (void) fprintf(stderr
, gettext("use '-m' "
786 "option to provide a different default\n"));
791 while (count
< 3 && readdir(dirp
) != NULL
)
793 (void) closedir(dirp
);
796 (void) fprintf(stderr
, gettext("mountpoint "
797 "'%s' exists and is not empty\n"), buf
);
798 (void) fprintf(stderr
, gettext("use '-m' "
799 "option to provide a "
800 "different default\n"));
808 * For a dry run invocation, print out a basic message and run
809 * through all the vdevs in the list and print out in an
810 * appropriate hierarchy.
812 (void) printf(gettext("would create '%s' with the "
813 "following layout:\n\n"), poolname
);
815 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
816 if (num_logs(nvroot
) > 0)
817 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
822 * Hand off to libzfs.
824 if (enable_all_pool_feat
) {
826 for (i
= 0; i
< SPA_FEATURES
; i
++) {
827 char propname
[MAXPATHLEN
];
828 zfeature_info_t
*feat
= &spa_feature_table
[i
];
830 (void) snprintf(propname
, sizeof (propname
),
831 "feature@%s", feat
->fi_uname
);
834 * Skip feature if user specified it manually
835 * on the command line.
837 if (nvlist_exists(props
, propname
))
840 if (add_prop_list(propname
, ZFS_FEATURE_ENABLED
,
841 &props
, B_TRUE
) != 0)
845 if (zpool_create(g_zfs
, poolname
,
846 nvroot
, props
, fsprops
) == 0) {
847 zfs_handle_t
*pool
= zfs_open(g_zfs
, poolname
,
848 ZFS_TYPE_FILESYSTEM
);
850 if (mountpoint
!= NULL
)
851 verify(zfs_prop_set(pool
,
853 ZFS_PROP_MOUNTPOINT
),
855 if (zfs_mount(pool
, NULL
, 0) == 0)
856 ret
= zfs_shareall(pool
);
859 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
860 (void) fprintf(stderr
, gettext("pool name may have "
867 nvlist_free(fsprops
);
871 nvlist_free(fsprops
);
878 * zpool destroy <pool>
880 * -f Forcefully unmount any datasets
882 * Destroy the given pool. Automatically unmounts any datasets in the pool.
885 zpool_do_destroy(int argc
, char **argv
)
887 boolean_t force
= B_FALSE
;
894 while ((c
= getopt(argc
, argv
, "f")) != -1) {
900 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
909 /* check arguments */
911 (void) fprintf(stderr
, gettext("missing pool argument\n"));
915 (void) fprintf(stderr
, gettext("too many arguments\n"));
921 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
923 * As a special case, check for use of '/' in the name, and
924 * direct the user to use 'zfs destroy' instead.
926 if (strchr(pool
, '/') != NULL
)
927 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
928 "destroy a dataset\n"));
932 if (zpool_disable_datasets(zhp
, force
) != 0) {
933 (void) fprintf(stderr
, gettext("could not destroy '%s': "
934 "could not unmount datasets\n"), zpool_get_name(zhp
));
938 /* The history must be logged as part of the export */
939 log_history
= B_FALSE
;
941 ret
= (zpool_destroy(zhp
, history_str
) != 0);
949 * zpool export [-f] <pool> ...
951 * -f Forcefully unmount datasets
953 * Export the given pools. By default, the command will attempt to cleanly
954 * unmount any active datasets within the pool. If the '-f' flag is specified,
955 * then the datasets will be forcefully unmounted.
958 zpool_do_export(int argc
, char **argv
)
960 boolean_t force
= B_FALSE
;
961 boolean_t hardforce
= B_FALSE
;
968 while ((c
= getopt(argc
, argv
, "fF")) != -1) {
977 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
986 /* check arguments */
988 (void) fprintf(stderr
, gettext("missing pool argument\n"));
993 for (i
= 0; i
< argc
; i
++) {
994 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) == NULL
) {
999 if (zpool_disable_datasets(zhp
, force
) != 0) {
1005 /* The history must be logged as part of the export */
1006 log_history
= B_FALSE
;
1009 if (zpool_export_force(zhp
, history_str
) != 0)
1011 } else if (zpool_export(zhp
, force
, history_str
) != 0) {
1022 * Given a vdev configuration, determine the maximum width needed for the device
1026 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1028 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1033 if (strlen(name
) + depth
> max
)
1034 max
= strlen(name
) + depth
;
1038 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1039 &child
, &children
) == 0) {
1040 for (c
= 0; c
< children
; c
++)
1041 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1046 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1047 &child
, &children
) == 0) {
1048 for (c
= 0; c
< children
; c
++)
1049 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1054 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1055 &child
, &children
) == 0) {
1056 for (c
= 0; c
< children
; c
++)
1057 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1066 typedef struct spare_cbdata
{
1068 zpool_handle_t
*cb_zhp
;
1072 find_vdev(nvlist_t
*nv
, uint64_t search
)
1078 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1082 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1083 &child
, &children
) == 0) {
1084 for (c
= 0; c
< children
; c
++)
1085 if (find_vdev(child
[c
], search
))
1093 find_spare(zpool_handle_t
*zhp
, void *data
)
1095 spare_cbdata_t
*cbp
= data
;
1096 nvlist_t
*config
, *nvroot
;
1098 config
= zpool_get_config(zhp
, NULL
);
1099 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1102 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1112 * Print out configuration state as requested by status_callback.
1115 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1116 int namewidth
, int depth
, boolean_t isspare
)
1120 pool_scan_stat_t
*ps
= NULL
;
1122 char rbuf
[6], wbuf
[6], cbuf
[6];
1124 uint64_t notpresent
;
1128 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1129 &child
, &children
) != 0)
1132 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1133 (uint64_t **)&vs
, &c
) == 0);
1135 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1138 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1141 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1143 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1147 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1151 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1152 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1153 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1154 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1157 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1158 ¬present
) == 0) {
1160 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1161 (void) printf(" was %s", path
);
1162 } else if (vs
->vs_aux
!= 0) {
1165 switch (vs
->vs_aux
) {
1166 case VDEV_AUX_OPEN_FAILED
:
1167 (void) printf(gettext("cannot open"));
1170 case VDEV_AUX_BAD_GUID_SUM
:
1171 (void) printf(gettext("missing device"));
1174 case VDEV_AUX_NO_REPLICAS
:
1175 (void) printf(gettext("insufficient replicas"));
1178 case VDEV_AUX_VERSION_NEWER
:
1179 (void) printf(gettext("newer version"));
1182 case VDEV_AUX_UNSUP_FEAT
:
1183 (void) printf(gettext("unsupported feature(s)"));
1186 case VDEV_AUX_SPARED
:
1187 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1189 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1190 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1191 zpool_get_name(zhp
)) == 0)
1192 (void) printf(gettext("currently in "
1195 (void) printf(gettext("in use by "
1197 zpool_get_name(cb
.cb_zhp
));
1198 zpool_close(cb
.cb_zhp
);
1200 (void) printf(gettext("currently in use"));
1204 case VDEV_AUX_ERR_EXCEEDED
:
1205 (void) printf(gettext("too many errors"));
1208 case VDEV_AUX_IO_FAILURE
:
1209 (void) printf(gettext("experienced I/O failures"));
1212 case VDEV_AUX_BAD_LOG
:
1213 (void) printf(gettext("bad intent log"));
1216 case VDEV_AUX_EXTERNAL
:
1217 (void) printf(gettext("external device fault"));
1220 case VDEV_AUX_SPLIT_POOL
:
1221 (void) printf(gettext("split into new pool"));
1225 (void) printf(gettext("corrupted data"));
1230 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1231 (uint64_t **)&ps
, &c
);
1233 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1234 vs
->vs_scan_processed
!= 0 && children
== 0) {
1235 (void) printf(gettext(" (%s)"),
1236 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1237 "resilvering" : "repairing");
1240 (void) printf("\n");
1242 for (c
= 0; c
< children
; c
++) {
1243 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1245 /* Don't print logs or holes here */
1246 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1248 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1250 if (islog
|| ishole
)
1252 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1253 print_status_config(zhp
, vname
, child
[c
],
1254 namewidth
, depth
+ 2, isspare
);
1261 * Print the configuration of an exported pool. Iterate over all vdevs in the
1262 * pool, printing out the name and status for each one.
1265 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1272 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1273 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1274 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1277 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1278 (uint64_t **)&vs
, &c
) == 0);
1280 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1281 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1283 if (vs
->vs_aux
!= 0) {
1286 switch (vs
->vs_aux
) {
1287 case VDEV_AUX_OPEN_FAILED
:
1288 (void) printf(gettext("cannot open"));
1291 case VDEV_AUX_BAD_GUID_SUM
:
1292 (void) printf(gettext("missing device"));
1295 case VDEV_AUX_NO_REPLICAS
:
1296 (void) printf(gettext("insufficient replicas"));
1299 case VDEV_AUX_VERSION_NEWER
:
1300 (void) printf(gettext("newer version"));
1303 case VDEV_AUX_UNSUP_FEAT
:
1304 (void) printf(gettext("unsupported feature(s)"));
1307 case VDEV_AUX_ERR_EXCEEDED
:
1308 (void) printf(gettext("too many errors"));
1312 (void) printf(gettext("corrupted data"));
1316 (void) printf("\n");
1318 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1319 &child
, &children
) != 0)
1322 for (c
= 0; c
< children
; c
++) {
1323 uint64_t is_log
= B_FALSE
;
1325 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1330 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1331 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1335 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1336 &child
, &children
) == 0) {
1337 (void) printf(gettext("\tcache\n"));
1338 for (c
= 0; c
< children
; c
++) {
1339 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1340 (void) printf("\t %s\n", vname
);
1345 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1346 &child
, &children
) == 0) {
1347 (void) printf(gettext("\tspares\n"));
1348 for (c
= 0; c
< children
; c
++) {
1349 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1350 (void) printf("\t %s\n", vname
);
1358 * Logs are recorded as top level vdevs in the main pool child array
1359 * but with "is_log" set to 1. We use either print_status_config() or
1360 * print_import_config() to print the top level logs then any log
1361 * children (eg mirrored slogs) are printed recursively - which
1362 * works because only the top level vdev is marked "is_log"
1365 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1370 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1374 (void) printf(gettext("\tlogs\n"));
1376 for (c
= 0; c
< children
; c
++) {
1377 uint64_t is_log
= B_FALSE
;
1380 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1384 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1386 print_status_config(zhp
, name
, child
[c
], namewidth
,
1389 print_import_config(name
, child
[c
], namewidth
, 2);
1395 * Display the status for the given pool.
1398 show_import(nvlist_t
*config
)
1400 uint64_t pool_state
;
1412 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1414 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1416 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1418 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1421 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1422 (uint64_t **)&vs
, &vsc
) == 0);
1423 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1425 reason
= zpool_import_status(config
, &msgid
);
1427 (void) printf(gettext(" pool: %s\n"), name
);
1428 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1429 (void) printf(gettext(" state: %s"), health
);
1430 if (pool_state
== POOL_STATE_DESTROYED
)
1431 (void) printf(gettext(" (DESTROYED)"));
1432 (void) printf("\n");
1435 case ZPOOL_STATUS_MISSING_DEV_R
:
1436 case ZPOOL_STATUS_MISSING_DEV_NR
:
1437 case ZPOOL_STATUS_BAD_GUID_SUM
:
1438 (void) printf(gettext(" status: One or more devices are "
1439 "missing from the system.\n"));
1442 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1443 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1444 (void) printf(gettext(" status: One or more devices contains "
1445 "corrupted data.\n"));
1448 case ZPOOL_STATUS_CORRUPT_DATA
:
1450 gettext(" status: The pool data is corrupted.\n"));
1453 case ZPOOL_STATUS_OFFLINE_DEV
:
1454 (void) printf(gettext(" status: One or more devices "
1455 "are offlined.\n"));
1458 case ZPOOL_STATUS_CORRUPT_POOL
:
1459 (void) printf(gettext(" status: The pool metadata is "
1463 case ZPOOL_STATUS_VERSION_OLDER
:
1464 (void) printf(gettext(" status: The pool is formatted using an "
1465 "older on-disk version.\n"));
1468 case ZPOOL_STATUS_VERSION_NEWER
:
1469 (void) printf(gettext(" status: The pool is formatted using an "
1470 "incompatible version.\n"));
1473 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1474 (void) printf(gettext("status: The pool uses the following "
1475 "feature(s) not supported on this sytem:\n"));
1476 zpool_print_unsup_feat(config
);
1479 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1480 (void) printf(gettext("status: The pool can only be accessed "
1481 "in read-only mode on this system. It\n\tcannot be "
1482 "accessed in read-write mode because it uses the "
1483 "following\n\tfeature(s) not supported on this system:\n"));
1484 zpool_print_unsup_feat(config
);
1487 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1488 (void) printf(gettext(" status: The pool was last accessed by "
1489 "another system.\n"));
1492 case ZPOOL_STATUS_FAULTED_DEV_R
:
1493 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1494 (void) printf(gettext(" status: One or more devices are "
1498 case ZPOOL_STATUS_BAD_LOG
:
1499 (void) printf(gettext(" status: An intent log record cannot be "
1503 case ZPOOL_STATUS_RESILVERING
:
1504 (void) printf(gettext(" status: One or more devices were being "
1510 * No other status can be seen when importing pools.
1512 assert(reason
== ZPOOL_STATUS_OK
);
1516 * Print out an action according to the overall state of the pool.
1518 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1519 if (reason
== ZPOOL_STATUS_VERSION_OLDER
)
1520 (void) printf(gettext(" action: The pool can be "
1521 "imported using its name or numeric identifier, "
1522 "though\n\tsome features will not be available "
1523 "without an explicit 'zpool upgrade'.\n"));
1524 else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
)
1525 (void) printf(gettext(" action: The pool can be "
1526 "imported using its name or numeric "
1527 "identifier and\n\tthe '-f' flag.\n"));
1529 (void) printf(gettext(" action: The pool can be "
1530 "imported using its name or numeric "
1532 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1533 (void) printf(gettext(" action: The pool can be imported "
1534 "despite missing or damaged devices. The\n\tfault "
1535 "tolerance of the pool may be compromised if imported.\n"));
1538 case ZPOOL_STATUS_VERSION_NEWER
:
1539 (void) printf(gettext(" action: The pool cannot be "
1540 "imported. Access the pool on a system running "
1541 "newer\n\tsoftware, or recreate the pool from "
1544 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1545 (void) printf(gettext("action: The pool cannot be "
1546 "imported. Access the pool on a system that "
1547 "supports\n\tthe required feature(s), or recreate "
1548 "the pool from backup.\n"));
1550 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1551 (void) printf(gettext("action: The pool cannot be "
1552 "imported in read-write mode. Import the pool "
1554 "\t\"-o readonly=on\", access the pool on a system "
1555 "that supports the\n\trequired feature(s), or "
1556 "recreate the pool from backup.\n"));
1558 case ZPOOL_STATUS_MISSING_DEV_R
:
1559 case ZPOOL_STATUS_MISSING_DEV_NR
:
1560 case ZPOOL_STATUS_BAD_GUID_SUM
:
1561 (void) printf(gettext(" action: The pool cannot be "
1562 "imported. Attach the missing\n\tdevices and try "
1566 (void) printf(gettext(" action: The pool cannot be "
1567 "imported due to damaged devices or data.\n"));
1571 /* Print the comment attached to the pool. */
1572 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1573 (void) printf(gettext("comment: %s\n"), comment
);
1576 * If the state is "closed" or "can't open", and the aux state
1577 * is "corrupt data":
1579 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1580 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1581 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1582 if (pool_state
== POOL_STATE_DESTROYED
)
1583 (void) printf(gettext("\tThe pool was destroyed, "
1584 "but can be imported using the '-Df' flags.\n"));
1585 else if (pool_state
!= POOL_STATE_EXPORTED
)
1586 (void) printf(gettext("\tThe pool may be active on "
1587 "another system, but can be imported using\n\t"
1588 "the '-f' flag.\n"));
1592 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1595 (void) printf(gettext(" config:\n\n"));
1597 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1601 print_import_config(name
, nvroot
, namewidth
, 0);
1602 if (num_logs(nvroot
) > 0)
1603 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1605 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1606 (void) printf(gettext("\n\tAdditional devices are known to "
1607 "be part of this pool, though their\n\texact "
1608 "configuration cannot be determined.\n"));
1613 * Perform the import for the given configuration. This passes the heavy
1614 * lifting off to zpool_import_props(), and then mounts the datasets contained
1618 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1619 nvlist_t
*props
, int flags
)
1621 zpool_handle_t
*zhp
;
1626 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1629 verify(nvlist_lookup_uint64(config
,
1630 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1631 verify(nvlist_lookup_uint64(config
,
1632 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1633 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1634 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1635 "is formatted using an unsupported ZFS version\n"), name
);
1637 } else if (state
!= POOL_STATE_EXPORTED
&&
1638 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1641 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1643 if ((unsigned long)hostid
!= gethostid()) {
1648 verify(nvlist_lookup_string(config
,
1649 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1650 verify(nvlist_lookup_uint64(config
,
1651 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
1653 (void) fprintf(stderr
, gettext("cannot import "
1654 "'%s': pool may be in use from other "
1655 "system, it was last accessed by %s "
1656 "(hostid: 0x%lx) on %s"), name
, hostname
,
1657 (unsigned long)hostid
,
1658 asctime(localtime(&t
)));
1659 (void) fprintf(stderr
, gettext("use '-f' to "
1660 "import anyway\n"));
1664 (void) fprintf(stderr
, gettext("cannot import '%s': "
1665 "pool may be in use from other system\n"), name
);
1666 (void) fprintf(stderr
, gettext("use '-f' to import "
1672 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
1675 if (newname
!= NULL
)
1676 name
= (char *)newname
;
1678 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
1681 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
1682 !(flags
& ZFS_IMPORT_ONLY
) &&
1683 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
1693 * zpool import [-d dir] [-D]
1694 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1695 * [-d dir | -c cachefile] [-f] -a
1696 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1697 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1699 * -c Read pool information from a cachefile instead of searching
1702 * -d Scan in a specific directory, other than /dev/dsk. More than
1703 * one directory can be specified using multiple '-d' options.
1705 * -D Scan for previously destroyed pools or import all or only
1706 * specified destroyed pools.
1708 * -R Temporarily import the pool, with all mountpoints relative to
1709 * the given root. The pool will remain exported when the machine
1712 * -V Import even in the presence of faulted vdevs. This is an
1713 * intentionally undocumented option for testing purposes, and
1714 * treats the pool configuration as complete, leaving any bad
1715 * vdevs in the FAULTED state. In other words, it does verbatim
1718 * -f Force import, even if it appears that the pool is active.
1720 * -F Attempt rewind if necessary.
1722 * -n See if rewind would work, but don't actually rewind.
1724 * -N Import the pool but don't mount datasets.
1726 * -T Specify a starting txg to use for import. This option is
1727 * intentionally undocumented option for testing purposes.
1729 * -a Import all pools found.
1731 * -o Set property=value and/or temporary mount options (without '=').
1733 * The import command scans for pools to import, and import pools based on pool
1734 * name and GUID. The pool can also be renamed as part of the import process.
1737 zpool_do_import(int argc
, char **argv
)
1739 char **searchdirs
= NULL
;
1743 nvlist_t
*pools
= NULL
;
1744 boolean_t do_all
= B_FALSE
;
1745 boolean_t do_destroyed
= B_FALSE
;
1746 char *mntopts
= NULL
;
1749 uint64_t searchguid
= 0;
1750 char *searchname
= NULL
;
1752 nvlist_t
*found_config
;
1753 nvlist_t
*policy
= NULL
;
1754 nvlist_t
*props
= NULL
;
1756 int flags
= ZFS_IMPORT_NORMAL
;
1757 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
1758 boolean_t dryrun
= B_FALSE
;
1759 boolean_t do_rewind
= B_FALSE
;
1760 boolean_t xtreme_rewind
= B_FALSE
;
1761 uint64_t pool_state
, txg
= -1ULL;
1762 char *cachefile
= NULL
;
1763 importargs_t idata
= { 0 };
1767 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1776 if (searchdirs
== NULL
) {
1777 searchdirs
= safe_malloc(sizeof (char *));
1779 char **tmp
= safe_malloc((nsearch
+ 1) *
1781 bcopy(searchdirs
, tmp
, nsearch
*
1786 searchdirs
[nsearch
++] = optarg
;
1789 do_destroyed
= B_TRUE
;
1792 flags
|= ZFS_IMPORT_ANY_HOST
;
1798 flags
|= ZFS_IMPORT_MISSING_LOG
;
1804 flags
|= ZFS_IMPORT_ONLY
;
1807 if ((propval
= strchr(optarg
, '=')) != NULL
) {
1810 if (add_prop_list(optarg
, propval
,
1818 if (add_prop_list(zpool_prop_to_name(
1819 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1821 if (nvlist_lookup_string(props
,
1822 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
1825 if (add_prop_list(zpool_prop_to_name(
1826 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1831 txg
= strtoull(optarg
, &endptr
, 10);
1832 if (errno
!= 0 || *endptr
!= '\0') {
1833 (void) fprintf(stderr
,
1834 gettext("invalid txg value\n"));
1837 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
1840 flags
|= ZFS_IMPORT_VERBATIM
;
1843 xtreme_rewind
= B_TRUE
;
1846 (void) fprintf(stderr
, gettext("missing argument for "
1847 "'%c' option\n"), optopt
);
1851 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1860 if (cachefile
&& nsearch
!= 0) {
1861 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
1865 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
1866 (void) fprintf(stderr
,
1867 gettext("-n or -X only meaningful with -F\n"));
1871 rewind_policy
= ZPOOL_TRY_REWIND
;
1873 rewind_policy
= ZPOOL_DO_REWIND
;
1875 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
1877 /* In the future, we can capture further policy and include it here */
1878 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
1879 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
1880 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
1883 if (searchdirs
== NULL
) {
1884 searchdirs
= safe_malloc(sizeof (char *));
1885 searchdirs
[0] = "/dev/dsk";
1889 /* check argument count */
1892 (void) fprintf(stderr
, gettext("too many arguments\n"));
1897 (void) fprintf(stderr
, gettext("too many arguments\n"));
1902 * Check for the SYS_CONFIG privilege. We do this explicitly
1903 * here because otherwise any attempt to discover pools will
1906 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
1907 (void) fprintf(stderr
, gettext("cannot "
1908 "discover pools: permission denied\n"));
1910 nvlist_free(policy
);
1916 * Depending on the arguments given, we do one of the following:
1918 * <none> Iterate through all pools and display information about
1921 * -a Iterate through all pools and try to import each one.
1923 * <id> Find the pool that corresponds to the given GUID/pool
1924 * name and import that one.
1926 * -D Above options applies only to destroyed pools.
1932 searchguid
= strtoull(argv
[0], &endptr
, 10);
1933 if (errno
!= 0 || *endptr
!= '\0')
1934 searchname
= argv
[0];
1935 found_config
= NULL
;
1938 * User specified a name or guid. Ensure it's unique.
1940 idata
.unique
= B_TRUE
;
1944 idata
.path
= searchdirs
;
1945 idata
.paths
= nsearch
;
1946 idata
.poolname
= searchname
;
1947 idata
.guid
= searchguid
;
1948 idata
.cachefile
= cachefile
;
1950 pools
= zpool_search_import(g_zfs
, &idata
);
1952 if (pools
!= NULL
&& idata
.exists
&&
1953 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
1954 (void) fprintf(stderr
, gettext("cannot import '%s': "
1955 "a pool with that name already exists\n"),
1957 (void) fprintf(stderr
, gettext("use the form '%s "
1958 "<pool | id> <newpool>' to give it a new name\n"),
1961 } else if (pools
== NULL
&& idata
.exists
) {
1962 (void) fprintf(stderr
, gettext("cannot import '%s': "
1963 "a pool with that name is already created/imported,\n"),
1965 (void) fprintf(stderr
, gettext("and no additional pools "
1966 "with that name were found\n"));
1968 } else if (pools
== NULL
) {
1970 (void) fprintf(stderr
, gettext("cannot import '%s': "
1971 "no such pool available\n"), argv
[0]);
1978 nvlist_free(policy
);
1983 * At this point we have a list of import candidate configs. Even if
1984 * we were searching by pool name or guid, we still need to
1985 * post-process the list to deal with pool state and possible
1991 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
1993 verify(nvpair_value_nvlist(elem
, &config
) == 0);
1995 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1997 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
1999 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2002 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2009 (void) printf("\n");
2012 err
|= do_import(config
, NULL
, mntopts
,
2015 show_import(config
);
2017 } else if (searchname
!= NULL
) {
2021 * We are searching for a pool based on name.
2023 verify(nvlist_lookup_string(config
,
2024 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2026 if (strcmp(name
, searchname
) == 0) {
2027 if (found_config
!= NULL
) {
2028 (void) fprintf(stderr
, gettext(
2029 "cannot import '%s': more than "
2030 "one matching pool\n"), searchname
);
2031 (void) fprintf(stderr
, gettext(
2032 "import by numeric ID instead\n"));
2035 found_config
= config
;
2041 * Search for a pool by guid.
2043 verify(nvlist_lookup_uint64(config
,
2044 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2046 if (guid
== searchguid
)
2047 found_config
= config
;
2052 * If we were searching for a specific pool, verify that we found a
2053 * pool, and then do the import.
2055 if (argc
!= 0 && err
== 0) {
2056 if (found_config
== NULL
) {
2057 (void) fprintf(stderr
, gettext("cannot import '%s': "
2058 "no such pool available\n"), argv
[0]);
2061 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2062 argv
[1], mntopts
, props
, flags
);
2067 * If we were just looking for pools, report an error if none were
2070 if (argc
== 0 && first
)
2071 (void) fprintf(stderr
,
2072 gettext("no pools available to import\n"));
2077 nvlist_free(policy
);
2080 return (err
? 1 : 0);
2083 typedef struct iostat_cbdata
{
2084 boolean_t cb_verbose
;
2087 zpool_list_t
*cb_list
;
2091 print_iostat_separator(iostat_cbdata_t
*cb
)
2095 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2097 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2101 print_iostat_header(iostat_cbdata_t
*cb
)
2103 (void) printf("%*s capacity operations bandwidth\n",
2104 cb
->cb_namewidth
, "");
2105 (void) printf("%-*s alloc free read write read write\n",
2106 cb
->cb_namewidth
, "pool");
2107 print_iostat_separator(cb
);
2111 * Display a single statistic.
2114 print_one_stat(uint64_t value
)
2118 zfs_nicenum(value
, buf
, sizeof (buf
));
2119 (void) printf(" %5s", buf
);
2123 * Print out all the statistics for the given vdev. This can either be the
2124 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2125 * is a verbose output, and we don't want to display the toplevel pool stats.
2128 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2129 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2131 nvlist_t
**oldchild
, **newchild
;
2133 vdev_stat_t
*oldvs
, *newvs
;
2134 vdev_stat_t zerovs
= { 0 };
2139 if (oldnv
!= NULL
) {
2140 verify(nvlist_lookup_uint64_array(oldnv
,
2141 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2146 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2147 (uint64_t **)&newvs
, &c
) == 0);
2149 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2150 (void) printf("%*s%s", depth
, "", name
);
2152 (void) printf("%*s%s%*s", depth
, "", name
,
2153 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2155 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2160 scale
= (double)NANOSEC
/ tdelta
;
2162 /* only toplevel vdevs have capacity stats */
2163 if (newvs
->vs_space
== 0) {
2164 (void) printf(" - -");
2166 print_one_stat(newvs
->vs_alloc
);
2167 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2170 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2171 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2173 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2174 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2176 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2177 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2179 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2180 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2182 (void) printf("\n");
2184 if (!cb
->cb_verbose
)
2187 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2188 &newchild
, &children
) != 0)
2191 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2192 &oldchild
, &c
) != 0)
2195 for (c
= 0; c
< children
; c
++) {
2196 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2198 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2201 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2204 if (ishole
|| islog
)
2207 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2208 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2209 newchild
[c
], cb
, depth
+ 2);
2214 * Log device section
2217 if (num_logs(newnv
) > 0) {
2218 (void) printf("%-*s - - - - - "
2219 "-\n", cb
->cb_namewidth
, "logs");
2221 for (c
= 0; c
< children
; c
++) {
2222 uint64_t islog
= B_FALSE
;
2223 (void) nvlist_lookup_uint64(newchild
[c
],
2224 ZPOOL_CONFIG_IS_LOG
, &islog
);
2227 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2229 print_vdev_stats(zhp
, vname
, oldnv
?
2230 oldchild
[c
] : NULL
, newchild
[c
],
2239 * Include level 2 ARC devices in iostat output
2241 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2242 &newchild
, &children
) != 0)
2245 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2246 &oldchild
, &c
) != 0)
2250 (void) printf("%-*s - - - - - "
2251 "-\n", cb
->cb_namewidth
, "cache");
2252 for (c
= 0; c
< children
; c
++) {
2253 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2255 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2256 newchild
[c
], cb
, depth
+ 2);
2263 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2265 iostat_cbdata_t
*cb
= data
;
2269 * If the pool has disappeared, remove it from the list and continue.
2271 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2275 pool_list_remove(cb
->cb_list
, zhp
);
2281 * Callback to print out the iostats for the given pool.
2284 print_iostat(zpool_handle_t
*zhp
, void *data
)
2286 iostat_cbdata_t
*cb
= data
;
2287 nvlist_t
*oldconfig
, *newconfig
;
2288 nvlist_t
*oldnvroot
, *newnvroot
;
2290 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2292 if (cb
->cb_iteration
== 1)
2295 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2298 if (oldconfig
== NULL
)
2301 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2305 * Print out the statistics for the pool.
2307 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2310 print_iostat_separator(cb
);
2316 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2318 iostat_cbdata_t
*cb
= data
;
2319 nvlist_t
*config
, *nvroot
;
2321 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2322 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2324 if (!cb
->cb_verbose
)
2325 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2327 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2332 * The width must fall into the range [10,38]. The upper limit is the
2333 * maximum we can have and still fit in 80 columns.
2335 if (cb
->cb_namewidth
< 10)
2336 cb
->cb_namewidth
= 10;
2337 if (cb
->cb_namewidth
> 38)
2338 cb
->cb_namewidth
= 38;
2344 * Parse the input string, get the 'interval' and 'count' value if there is one.
2347 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2350 unsigned long interval
= 0, count
= 0;
2351 int argc
= *argcp
, errno
;
2354 * Determine if the last argument is an integer or a pool name
2356 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2360 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2362 if (*end
== '\0' && errno
== 0) {
2363 if (interval
== 0) {
2364 (void) fprintf(stderr
, gettext("interval "
2365 "cannot be zero\n"));
2369 * Ignore the last parameter
2374 * If this is not a valid number, just plow on. The
2375 * user will get a more informative error message later
2383 * If the last argument is also an integer, then we have both a count
2386 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2391 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2393 if (*end
== '\0' && errno
== 0) {
2394 if (interval
== 0) {
2395 (void) fprintf(stderr
, gettext("interval "
2396 "cannot be zero\n"));
2401 * Ignore the last parameter
2415 get_timestamp_arg(char c
)
2418 timestamp_fmt
= UDATE
;
2420 timestamp_fmt
= DDATE
;
2426 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2428 * -v Display statistics for individual vdevs
2429 * -T Display a timestamp in date(1) or Unix format
2431 * This command can be tricky because we want to be able to deal with pool
2432 * creation/destruction as well as vdev configuration changes. The bulk of this
2433 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2434 * on pool_list_update() to detect the addition of new pools. Configuration
2435 * changes are all handled within libzfs.
2438 zpool_do_iostat(int argc
, char **argv
)
2443 unsigned long interval
= 0, count
= 0;
2445 boolean_t verbose
= B_FALSE
;
2449 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2452 get_timestamp_arg(*optarg
);
2458 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2467 get_interval_count(&argc
, argv
, &interval
, &count
);
2470 * Construct the list of all interesting pools.
2473 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2476 if (pool_list_count(list
) == 0 && argc
!= 0) {
2477 pool_list_free(list
);
2481 if (pool_list_count(list
) == 0 && interval
== 0) {
2482 pool_list_free(list
);
2483 (void) fprintf(stderr
, gettext("no pools available\n"));
2488 * Enter the main iostat loop.
2491 cb
.cb_verbose
= verbose
;
2492 cb
.cb_iteration
= 0;
2493 cb
.cb_namewidth
= 0;
2496 pool_list_update(list
);
2498 if ((npools
= pool_list_count(list
)) == 0)
2502 * Refresh all statistics. This is done as an explicit step
2503 * before calculating the maximum name width, so that any
2504 * configuration changes are properly accounted for.
2506 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
, &cb
);
2509 * Iterate over all pools to determine the maximum width
2510 * for the pool / device name column across all pools.
2512 cb
.cb_namewidth
= 0;
2513 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
2515 if (timestamp_fmt
!= NODATE
)
2516 print_timestamp(timestamp_fmt
);
2519 * If it's the first time, or verbose mode, print the header.
2521 if (++cb
.cb_iteration
== 1 || verbose
)
2522 print_iostat_header(&cb
);
2524 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2527 * If there's more than one pool, and we're not in verbose mode
2528 * (which prints a separator for us), then print a separator.
2530 if (npools
> 1 && !verbose
)
2531 print_iostat_separator(&cb
);
2534 (void) printf("\n");
2537 * Flush the output so that redirection to a file isn't buffered
2540 (void) fflush(stdout
);
2545 if (count
!= 0 && --count
== 0)
2548 (void) sleep(interval
);
2551 pool_list_free(list
);
2556 typedef struct list_cbdata
{
2557 boolean_t cb_verbose
;
2559 boolean_t cb_scripted
;
2560 zprop_list_t
*cb_proplist
;
2564 * Given a list of columns to display, output appropriate headers for each one.
2567 print_header(list_cbdata_t
*cb
)
2569 zprop_list_t
*pl
= cb
->cb_proplist
;
2570 char headerbuf
[ZPOOL_MAXPROPLEN
];
2572 boolean_t first
= B_TRUE
;
2573 boolean_t right_justify
;
2576 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2577 width
= pl
->pl_width
;
2578 if (first
&& cb
->cb_verbose
) {
2580 * Reset the width to accommodate the verbose listing
2583 width
= cb
->cb_namewidth
;
2591 right_justify
= B_FALSE
;
2592 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2593 header
= zpool_prop_column_name(pl
->pl_prop
);
2594 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2598 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2599 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2600 headerbuf
[i
] = '\0';
2604 if (pl
->pl_next
== NULL
&& !right_justify
)
2605 (void) printf("%s", header
);
2606 else if (right_justify
)
2607 (void) printf("%*s", width
, header
);
2609 (void) printf("%-*s", width
, header
);
2613 (void) printf("\n");
2617 * Given a pool and a list of properties, print out all the properties according
2618 * to the described layout.
2621 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
2623 zprop_list_t
*pl
= cb
->cb_proplist
;
2624 boolean_t first
= B_TRUE
;
2625 char property
[ZPOOL_MAXPROPLEN
];
2627 boolean_t right_justify
;
2630 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2632 width
= pl
->pl_width
;
2633 if (first
&& cb
->cb_verbose
) {
2635 * Reset the width to accommodate the verbose listing
2638 width
= cb
->cb_namewidth
;
2642 if (cb
->cb_scripted
)
2643 (void) printf("\t");
2650 right_justify
= B_FALSE
;
2651 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2652 if (pl
->pl_prop
== ZPOOL_PROP_EXPANDSZ
&&
2653 zpool_get_prop_int(zhp
, pl
->pl_prop
, NULL
) == 0)
2655 else if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
2656 sizeof (property
), NULL
) != 0)
2661 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2662 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
2663 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
2664 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
2665 sizeof (property
)) == 0) {
2673 * If this is being called in scripted mode, or if this is the
2674 * last column and it is left-justified, don't include a width
2677 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2678 (void) printf("%s", propstr
);
2679 else if (right_justify
)
2680 (void) printf("%*s", width
, propstr
);
2682 (void) printf("%-*s", width
, propstr
);
2685 (void) printf("\n");
2689 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
)
2693 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
2695 zfs_nicenum(value
, propval
, sizeof (propval
));
2697 if (prop
== ZPOOL_PROP_EXPANDSZ
&& value
== 0)
2698 (void) strlcpy(propval
, "-", sizeof (propval
));
2701 (void) printf("\t%s", propval
);
2703 (void) printf(" %*s", width
, propval
);
2707 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
2708 list_cbdata_t
*cb
, int depth
)
2714 boolean_t scripted
= cb
->cb_scripted
;
2716 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2717 (uint64_t **)&vs
, &c
) == 0);
2721 (void) printf("\t%s", name
);
2722 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
2723 (void) printf("%*s%s", depth
, "", name
);
2725 (void) printf("%*s%s%*s", depth
, "", name
,
2726 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2728 /* only toplevel vdevs have capacity stats */
2729 if (vs
->vs_space
== 0) {
2731 (void) printf("\t-\t-\t-");
2733 (void) printf(" - - -");
2735 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
,
2737 print_one_column(ZPOOL_PROP_CAPACITY
, vs
->vs_alloc
,
2739 print_one_column(ZPOOL_PROP_FREE
,
2740 vs
->vs_space
- vs
->vs_alloc
, scripted
);
2742 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
,
2744 (void) printf("\n");
2747 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2748 &child
, &children
) != 0)
2751 for (c
= 0; c
< children
; c
++) {
2752 uint64_t ishole
= B_FALSE
;
2754 if (nvlist_lookup_uint64(child
[c
],
2755 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
2758 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
2759 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2764 * Include level 2 ARC devices in iostat output
2766 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2767 &child
, &children
) != 0)
2771 (void) printf("%-*s - - - - - "
2772 "-\n", cb
->cb_namewidth
, "cache");
2773 for (c
= 0; c
< children
; c
++) {
2774 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2776 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2784 * Generic callback function to list a pool.
2787 list_callback(zpool_handle_t
*zhp
, void *data
)
2789 list_cbdata_t
*cbp
= data
;
2793 config
= zpool_get_config(zhp
, NULL
);
2795 print_pool(zhp
, cbp
);
2796 if (!cbp
->cb_verbose
)
2799 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2801 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
2807 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2809 * -H Scripted mode. Don't display headers, and separate properties
2811 * -o List of properties to display. Defaults to
2812 * "name,size,allocated,free,capacity,health,altroot"
2813 * -T Display a timestamp in date(1) or Unix format
2815 * List all pools in the system, whether or not they're healthy. Output space
2816 * statistics for each one, as well as health status summary.
2819 zpool_do_list(int argc
, char **argv
)
2823 list_cbdata_t cb
= { 0 };
2824 static char default_props
[] =
2825 "name,size,allocated,free,expandsize,capacity,dedupratio,"
2827 char *props
= default_props
;
2828 unsigned long interval
= 0, count
= 0;
2830 boolean_t first
= B_TRUE
;
2833 while ((c
= getopt(argc
, argv
, ":Ho:T:v")) != -1) {
2836 cb
.cb_scripted
= B_TRUE
;
2842 get_timestamp_arg(*optarg
);
2845 cb
.cb_verbose
= B_TRUE
;
2848 (void) fprintf(stderr
, gettext("missing argument for "
2849 "'%c' option\n"), optopt
);
2853 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2862 get_interval_count(&argc
, argv
, &interval
, &count
);
2864 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
2867 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
, &ret
)) == NULL
)
2870 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
2871 (void) printf(gettext("no pools available\n"));
2872 zprop_free_list(cb
.cb_proplist
);
2877 pool_list_update(list
);
2879 if (pool_list_count(list
) == 0)
2882 cb
.cb_namewidth
= 0;
2883 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
2885 if (timestamp_fmt
!= NODATE
)
2886 print_timestamp(timestamp_fmt
);
2888 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
2892 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
2897 if (count
!= 0 && --count
== 0)
2900 (void) sleep(interval
);
2903 zprop_free_list(cb
.cb_proplist
);
2908 zpool_get_vdev_by_name(nvlist_t
*nv
, char *name
)
2915 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2916 &child
, &children
) != 0) {
2917 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2918 if (strncmp(name
, "/dev/dsk/", 9) == 0)
2920 if (strncmp(path
, "/dev/dsk/", 9) == 0)
2922 if (strcmp(name
, path
) == 0)
2927 for (c
= 0; c
< children
; c
++)
2928 if ((match
= zpool_get_vdev_by_name(child
[c
], name
)) != NULL
)
2935 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
2937 boolean_t force
= B_FALSE
;
2940 char *poolname
, *old_disk
, *new_disk
;
2941 zpool_handle_t
*zhp
;
2945 while ((c
= getopt(argc
, argv
, "f")) != -1) {
2951 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2960 /* get pool name and check number of arguments */
2962 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
2969 (void) fprintf(stderr
,
2970 gettext("missing <device> specification\n"));
2978 (void) fprintf(stderr
,
2979 gettext("missing <new_device> specification\n"));
2982 new_disk
= old_disk
;
2992 (void) fprintf(stderr
, gettext("too many arguments\n"));
2996 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
2999 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3000 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3006 nvroot
= make_root_vdev(zhp
, force
, B_FALSE
, replacing
, B_FALSE
,
3008 if (nvroot
== NULL
) {
3013 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3015 nvlist_free(nvroot
);
3022 * zpool replace [-f] <pool> <device> <new_device>
3024 * -f Force attach, even if <new_device> appears to be in use.
3026 * Replace <device> with <new_device>.
3030 zpool_do_replace(int argc
, char **argv
)
3032 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3036 * zpool attach [-f] <pool> <device> <new_device>
3038 * -f Force attach, even if <new_device> appears to be in use.
3040 * Attach <new_device> to the mirror containing <device>. If <device> is not
3041 * part of a mirror, then <device> will be transformed into a mirror of
3042 * <device> and <new_device>. In either case, <new_device> will begin life
3043 * with a DTL of [0, now], and will immediately begin to resilver itself.
3046 zpool_do_attach(int argc
, char **argv
)
3048 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3052 * zpool detach [-f] <pool> <device>
3054 * -f Force detach of <device>, even if DTLs argue against it
3055 * (not supported yet)
3057 * Detach a device from a mirror. The operation will be refused if <device>
3058 * is the last device in the mirror, or if the DTLs indicate that this device
3059 * has the only valid copy of some data.
3063 zpool_do_detach(int argc
, char **argv
)
3066 char *poolname
, *path
;
3067 zpool_handle_t
*zhp
;
3071 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3075 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3084 /* get pool name and check number of arguments */
3086 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3091 (void) fprintf(stderr
,
3092 gettext("missing <device> specification\n"));
3099 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3102 ret
= zpool_vdev_detach(zhp
, path
);
3110 * zpool split [-n] [-o prop=val] ...
3112 * [-R altroot] <pool> <newpool> [<device> ...]
3114 * -n Do not split the pool, but display the resulting layout if
3115 * it were to be split.
3116 * -o Set property=value, or set mount options.
3117 * -R Mount the split-off pool under an alternate root.
3119 * Splits the named pool and gives it the new pool name. Devices to be split
3120 * off may be listed, provided that no more than one device is specified
3121 * per top-level vdev mirror. The newly split pool is left in an exported
3122 * state unless -R is specified.
3124 * Restrictions: the top-level of the pool pool must only be made up of
3125 * mirrors; all devices in the pool must be healthy; no device may be
3126 * undergoing a resilvering operation.
3129 zpool_do_split(int argc
, char **argv
)
3131 char *srcpool
, *newpool
, *propval
;
3132 char *mntopts
= NULL
;
3135 zpool_handle_t
*zhp
;
3136 nvlist_t
*config
, *props
= NULL
;
3138 flags
.dryrun
= B_FALSE
;
3139 flags
.import
= B_FALSE
;
3142 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3145 flags
.import
= B_TRUE
;
3147 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3148 &props
, B_TRUE
) != 0) {
3155 flags
.dryrun
= B_TRUE
;
3158 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3161 if (add_prop_list(optarg
, propval
,
3162 &props
, B_TRUE
) != 0) {
3172 (void) fprintf(stderr
, gettext("missing argument for "
3173 "'%c' option\n"), optopt
);
3177 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3184 if (!flags
.import
&& mntopts
!= NULL
) {
3185 (void) fprintf(stderr
, gettext("setting mntopts is only "
3186 "valid when importing the pool\n"));
3194 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3198 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3208 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3211 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3212 if (config
== NULL
) {
3216 (void) printf(gettext("would create '%s' with the "
3217 "following layout:\n\n"), newpool
);
3218 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3220 nvlist_free(config
);
3225 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3229 * The split was successful. Now we need to open the new
3230 * pool and import it.
3232 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3234 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3235 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3237 (void) fprintf(stderr
, gettext("Split was succssful, but "
3238 "the datasets could not all be mounted\n"));
3239 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3240 "different altroot\n"), "zpool import");
3250 * zpool online <pool> <device> ...
3253 zpool_do_online(int argc
, char **argv
)
3257 zpool_handle_t
*zhp
;
3259 vdev_state_t newstate
;
3263 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3266 flags
|= ZFS_ONLINE_EXPAND
;
3270 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3279 /* get pool name and check number of arguments */
3281 (void) fprintf(stderr
, gettext("missing pool name\n"));
3285 (void) fprintf(stderr
, gettext("missing device name\n"));
3291 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3294 for (i
= 1; i
< argc
; i
++) {
3295 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3296 if (newstate
!= VDEV_STATE_HEALTHY
) {
3297 (void) printf(gettext("warning: device '%s' "
3298 "onlined, but remains in faulted state\n"),
3300 if (newstate
== VDEV_STATE_FAULTED
)
3301 (void) printf(gettext("use 'zpool "
3302 "clear' to restore a faulted "
3305 (void) printf(gettext("use 'zpool "
3306 "replace' to replace devices "
3307 "that are no longer present\n"));
3320 * zpool offline [-ft] <pool> <device> ...
3322 * -f Force the device into the offline state, even if doing
3323 * so would appear to compromise pool availability.
3324 * (not supported yet)
3326 * -t Only take the device off-line temporarily. The offline
3327 * state will not be persistent across reboots.
3331 zpool_do_offline(int argc
, char **argv
)
3335 zpool_handle_t
*zhp
;
3337 boolean_t istmp
= B_FALSE
;
3340 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3347 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3356 /* get pool name and check number of arguments */
3358 (void) fprintf(stderr
, gettext("missing pool name\n"));
3362 (void) fprintf(stderr
, gettext("missing device name\n"));
3368 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3371 for (i
= 1; i
< argc
; i
++) {
3372 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3382 * zpool clear <pool> [device]
3384 * Clear all errors associated with a pool or a particular device.
3387 zpool_do_clear(int argc
, char **argv
)
3391 boolean_t dryrun
= B_FALSE
;
3392 boolean_t do_rewind
= B_FALSE
;
3393 boolean_t xtreme_rewind
= B_FALSE
;
3394 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3395 nvlist_t
*policy
= NULL
;
3396 zpool_handle_t
*zhp
;
3397 char *pool
, *device
;
3400 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3409 xtreme_rewind
= B_TRUE
;
3412 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3422 (void) fprintf(stderr
, gettext("missing pool name\n"));
3427 (void) fprintf(stderr
, gettext("too many arguments\n"));
3431 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3432 (void) fprintf(stderr
,
3433 gettext("-n or -X only meaningful with -F\n"));
3437 rewind_policy
= ZPOOL_TRY_REWIND
;
3439 rewind_policy
= ZPOOL_DO_REWIND
;
3441 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3443 /* In future, further rewind policy choices can be passed along here */
3444 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3445 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3449 device
= argc
== 2 ? argv
[1] : NULL
;
3451 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3452 nvlist_free(policy
);
3456 if (zpool_clear(zhp
, device
, policy
) != 0)
3461 nvlist_free(policy
);
3467 * zpool reguid <pool>
3470 zpool_do_reguid(int argc
, char **argv
)
3474 zpool_handle_t
*zhp
;
3478 while ((c
= getopt(argc
, argv
, "")) != -1) {
3481 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3490 /* get pool name and check number of arguments */
3492 (void) fprintf(stderr
, gettext("missing pool name\n"));
3497 (void) fprintf(stderr
, gettext("too many arguments\n"));
3502 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3505 ret
= zpool_reguid(zhp
);
3513 * zpool reopen <pool>
3515 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3517 * NOTE: This command is currently undocumented. If the command is ever
3518 * exposed then the appropriate usage() messages will need to be made.
3521 zpool_do_reopen(int argc
, char **argv
)
3524 zpool_handle_t
*zhp
;
3534 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3537 ret
= zpool_reopen(zhp
);
3542 typedef struct scrub_cbdata
{
3549 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3551 scrub_cbdata_t
*cb
= data
;
3555 * Ignore faulted pools.
3557 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3558 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3559 "currently unavailable\n"), zpool_get_name(zhp
));
3563 err
= zpool_scan(zhp
, cb
->cb_type
);
3569 * zpool scrub [-s] <pool> ...
3571 * -s Stop. Stops any in-progress scrub.
3574 zpool_do_scrub(int argc
, char **argv
)
3579 cb
.cb_type
= POOL_SCAN_SCRUB
;
3582 while ((c
= getopt(argc
, argv
, "s")) != -1) {
3585 cb
.cb_type
= POOL_SCAN_NONE
;
3588 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3600 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3604 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
3607 typedef struct status_cbdata
{
3609 boolean_t cb_allpools
;
3610 boolean_t cb_verbose
;
3611 boolean_t cb_explain
;
3613 boolean_t cb_dedup_stats
;
3617 * Print out detailed scrub status.
3620 print_scan_status(pool_scan_stat_t
*ps
)
3623 uint64_t elapsed
, mins_left
, hours_left
;
3624 uint64_t pass_exam
, examined
, total
;
3626 double fraction_done
;
3627 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
3629 (void) printf(gettext(" scan: "));
3631 /* If there's never been a scan, there's not much to say. */
3632 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
3633 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
3634 (void) printf(gettext("none requested\n"));
3638 start
= ps
->pss_start_time
;
3639 end
= ps
->pss_end_time
;
3640 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
3642 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
3643 ps
->pss_func
== POOL_SCAN_RESILVER
);
3645 * Scan is finished or canceled.
3647 if (ps
->pss_state
== DSS_FINISHED
) {
3648 uint64_t minutes_taken
= (end
- start
) / 60;
3651 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3652 fmt
= gettext("scrub repaired %s in %lluh%um with "
3653 "%llu errors on %s");
3654 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3655 fmt
= gettext("resilvered %s in %lluh%um with "
3656 "%llu errors on %s");
3659 (void) printf(fmt
, processed_buf
,
3660 (u_longlong_t
)(minutes_taken
/ 60),
3661 (uint_t
)(minutes_taken
% 60),
3662 (u_longlong_t
)ps
->pss_errors
,
3663 ctime((time_t *)&end
));
3665 } else if (ps
->pss_state
== DSS_CANCELED
) {
3666 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3667 (void) printf(gettext("scrub canceled on %s"),
3669 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3670 (void) printf(gettext("resilver canceled on %s"),
3676 assert(ps
->pss_state
== DSS_SCANNING
);
3679 * Scan is in progress.
3681 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3682 (void) printf(gettext("scrub in progress since %s"),
3684 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3685 (void) printf(gettext("resilver in progress since %s"),
3689 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
3690 total
= ps
->pss_to_examine
;
3691 fraction_done
= (double)examined
/ total
;
3693 /* elapsed time for this pass */
3694 elapsed
= time(NULL
) - ps
->pss_pass_start
;
3695 elapsed
= elapsed
? elapsed
: 1;
3696 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
3697 rate
= pass_exam
/ elapsed
;
3698 rate
= rate
? rate
: 1;
3699 mins_left
= ((total
- examined
) / rate
) / 60;
3700 hours_left
= mins_left
/ 60;
3702 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
3703 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
3704 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
3707 * do not print estimated time if hours_left is more than 30 days
3709 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3710 examined_buf
, total_buf
, rate_buf
);
3711 if (hours_left
< (30 * 24)) {
3712 (void) printf(gettext(", %lluh%um to go\n"),
3713 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
3715 (void) printf(gettext(
3716 ", (scan is slow, no estimated time)\n"));
3719 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3720 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3721 processed_buf
, 100 * fraction_done
);
3722 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3723 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3724 processed_buf
, 100 * fraction_done
);
3729 print_error_log(zpool_handle_t
*zhp
)
3731 nvlist_t
*nverrlist
= NULL
;
3734 size_t len
= MAXPATHLEN
* 2;
3736 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
3737 (void) printf("errors: List of errors unavailable "
3738 "(insufficient privileges)\n");
3742 (void) printf("errors: Permanent errors have been "
3743 "detected in the following files:\n\n");
3745 pathname
= safe_malloc(len
);
3747 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
3749 uint64_t dsobj
, obj
;
3751 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
3752 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
3754 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
3756 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
3757 (void) printf("%7s %s\n", "", pathname
);
3760 nvlist_free(nverrlist
);
3764 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
3773 (void) printf(gettext("\tspares\n"));
3775 for (i
= 0; i
< nspares
; i
++) {
3776 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
3777 print_status_config(zhp
, name
, spares
[i
],
3778 namewidth
, 2, B_TRUE
);
3784 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
3793 (void) printf(gettext("\tcache\n"));
3795 for (i
= 0; i
< nl2cache
; i
++) {
3796 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
3797 print_status_config(zhp
, name
, l2cache
[i
],
3798 namewidth
, 2, B_FALSE
);
3804 print_dedup_stats(nvlist_t
*config
)
3806 ddt_histogram_t
*ddh
;
3812 * If the pool was faulted then we may not have been able to
3813 * obtain the config. Otherwise, if we have anything in the dedup
3814 * table continue processing the stats.
3816 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
3817 (uint64_t **)&ddo
, &c
) != 0)
3820 (void) printf("\n");
3821 (void) printf(gettext(" dedup: "));
3822 if (ddo
->ddo_count
== 0) {
3823 (void) printf(gettext("no DDT entries\n"));
3827 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3828 (u_longlong_t
)ddo
->ddo_count
,
3829 (u_longlong_t
)ddo
->ddo_dspace
,
3830 (u_longlong_t
)ddo
->ddo_mspace
);
3832 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
3833 (uint64_t **)&dds
, &c
) == 0);
3834 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
3835 (uint64_t **)&ddh
, &c
) == 0);
3836 zpool_dump_ddt(dds
, ddh
);
3840 * Display a summary of pool status. Displays a summary such as:
3844 * reason: One or more devices ...
3845 * see: http://illumos.org/msg/ZFS-xxxx-01
3851 * When given the '-v' option, we print out the complete config. If the '-e'
3852 * option is specified, then we print out error rate information as well.
3855 status_callback(zpool_handle_t
*zhp
, void *data
)
3857 status_cbdata_t
*cbp
= data
;
3858 nvlist_t
*config
, *nvroot
;
3865 config
= zpool_get_config(zhp
, NULL
);
3866 reason
= zpool_get_status(zhp
, &msgid
);
3871 * If we were given 'zpool status -x', only report those pools with
3874 if (reason
== ZPOOL_STATUS_OK
&& cbp
->cb_explain
) {
3875 if (!cbp
->cb_allpools
) {
3876 (void) printf(gettext("pool '%s' is healthy\n"),
3877 zpool_get_name(zhp
));
3879 cbp
->cb_first
= B_FALSE
;
3885 cbp
->cb_first
= B_FALSE
;
3887 (void) printf("\n");
3889 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3891 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
3892 (uint64_t **)&vs
, &c
) == 0);
3893 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
3895 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
3896 (void) printf(gettext(" state: %s\n"), health
);
3899 case ZPOOL_STATUS_MISSING_DEV_R
:
3900 (void) printf(gettext("status: One or more devices could not "
3901 "be opened. Sufficient replicas exist for\n\tthe pool to "
3902 "continue functioning in a degraded state.\n"));
3903 (void) printf(gettext("action: Attach the missing device and "
3904 "online it using 'zpool online'.\n"));
3907 case ZPOOL_STATUS_MISSING_DEV_NR
:
3908 (void) printf(gettext("status: One or more devices could not "
3909 "be opened. There are insufficient\n\treplicas for the "
3910 "pool to continue functioning.\n"));
3911 (void) printf(gettext("action: Attach the missing device and "
3912 "online it using 'zpool online'.\n"));
3915 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
3916 (void) printf(gettext("status: One or more devices could not "
3917 "be used because the label is missing or\n\tinvalid. "
3918 "Sufficient replicas exist for the pool to continue\n\t"
3919 "functioning in a degraded state.\n"));
3920 (void) printf(gettext("action: Replace the device using "
3921 "'zpool replace'.\n"));
3924 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
3925 (void) printf(gettext("status: One or more devices could not "
3926 "be used because the label is missing \n\tor invalid. "
3927 "There are insufficient replicas for the pool to "
3928 "continue\n\tfunctioning.\n"));
3929 zpool_explain_recover(zpool_get_handle(zhp
),
3930 zpool_get_name(zhp
), reason
, config
);
3933 case ZPOOL_STATUS_FAILING_DEV
:
3934 (void) printf(gettext("status: One or more devices has "
3935 "experienced an unrecoverable error. An\n\tattempt was "
3936 "made to correct the error. Applications are "
3938 (void) printf(gettext("action: Determine if the device needs "
3939 "to be replaced, and clear the errors\n\tusing "
3940 "'zpool clear' or replace the device with 'zpool "
3944 case ZPOOL_STATUS_OFFLINE_DEV
:
3945 (void) printf(gettext("status: One or more devices has "
3946 "been taken offline by the administrator.\n\tSufficient "
3947 "replicas exist for the pool to continue functioning in "
3948 "a\n\tdegraded state.\n"));
3949 (void) printf(gettext("action: Online the device using "
3950 "'zpool online' or replace the device with\n\t'zpool "
3954 case ZPOOL_STATUS_REMOVED_DEV
:
3955 (void) printf(gettext("status: One or more devices has "
3956 "been removed by the administrator.\n\tSufficient "
3957 "replicas exist for the pool to continue functioning in "
3958 "a\n\tdegraded state.\n"));
3959 (void) printf(gettext("action: Online the device using "
3960 "'zpool online' or replace the device with\n\t'zpool "
3964 case ZPOOL_STATUS_RESILVERING
:
3965 (void) printf(gettext("status: One or more devices is "
3966 "currently being resilvered. The pool will\n\tcontinue "
3967 "to function, possibly in a degraded state.\n"));
3968 (void) printf(gettext("action: Wait for the resilver to "
3972 case ZPOOL_STATUS_CORRUPT_DATA
:
3973 (void) printf(gettext("status: One or more devices has "
3974 "experienced an error resulting in data\n\tcorruption. "
3975 "Applications may be affected.\n"));
3976 (void) printf(gettext("action: Restore the file in question "
3977 "if possible. Otherwise restore the\n\tentire pool from "
3981 case ZPOOL_STATUS_CORRUPT_POOL
:
3982 (void) printf(gettext("status: The pool metadata is corrupted "
3983 "and the pool cannot be opened.\n"));
3984 zpool_explain_recover(zpool_get_handle(zhp
),
3985 zpool_get_name(zhp
), reason
, config
);
3988 case ZPOOL_STATUS_VERSION_OLDER
:
3989 (void) printf(gettext("status: The pool is formatted using an "
3990 "older on-disk format. The pool can\n\tstill be used, but "
3991 "some features are unavailable.\n"));
3992 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3993 "upgrade'. Once this is done, the\n\tpool will no longer "
3994 "be accessible on older software versions.\n"));
3997 case ZPOOL_STATUS_VERSION_NEWER
:
3998 (void) printf(gettext("status: The pool has been upgraded to a "
3999 "newer, incompatible on-disk version.\n\tThe pool cannot "
4000 "be accessed on this system.\n"));
4001 (void) printf(gettext("action: Access the pool from a system "
4002 "running more recent software, or\n\trestore the pool from "
4006 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4007 (void) printf(gettext("status: The pool cannot be accessed on "
4008 "this system because it uses the\n\tfollowing feature(s) "
4009 "not supported on this system:\n"));
4010 zpool_print_unsup_feat(config
);
4011 (void) printf("\n");
4012 (void) printf(gettext("action: Access the pool from a system "
4013 "that supports the required feature(s),\n\tor restore the "
4014 "pool from backup.\n"));
4017 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4018 (void) printf(gettext("status: The pool can only be accessed "
4019 "in read-only mode on this system. It\n\tcannot be "
4020 "accessed in read-write mode because it uses the "
4021 "following\n\tfeature(s) not supported on this system:\n"));
4022 zpool_print_unsup_feat(config
);
4023 (void) printf("\n");
4024 (void) printf(gettext("action: The pool cannot be accessed in "
4025 "read-write mode. Import the pool with\n"
4026 "\t\"-o readonly=on\", access the pool from a system that "
4027 "supports the\n\trequired feature(s), or restore the "
4028 "pool from backup.\n"));
4031 case ZPOOL_STATUS_FAULTED_DEV_R
:
4032 (void) printf(gettext("status: One or more devices are "
4033 "faulted in response to persistent errors.\n\tSufficient "
4034 "replicas exist for the pool to continue functioning "
4035 "in a\n\tdegraded state.\n"));
4036 (void) printf(gettext("action: Replace the faulted device, "
4037 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4040 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4041 (void) printf(gettext("status: One or more devices are "
4042 "faulted in response to persistent errors. There are "
4043 "insufficient replicas for the pool to\n\tcontinue "
4045 (void) printf(gettext("action: Destroy and re-create the pool "
4046 "from a backup source. Manually marking the device\n"
4047 "\trepaired using 'zpool clear' may allow some data "
4048 "to be recovered.\n"));
4051 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4052 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4053 (void) printf(gettext("status: One or more devices are "
4054 "faulted in response to IO failures.\n"));
4055 (void) printf(gettext("action: Make sure the affected devices "
4056 "are connected, then run 'zpool clear'.\n"));
4059 case ZPOOL_STATUS_BAD_LOG
:
4060 (void) printf(gettext("status: An intent log record "
4061 "could not be read.\n"
4062 "\tWaiting for adminstrator intervention to fix the "
4063 "faulted pool.\n"));
4064 (void) printf(gettext("action: Either restore the affected "
4065 "device(s) and run 'zpool online',\n"
4066 "\tor ignore the intent log records by running "
4067 "'zpool clear'.\n"));
4072 * The remaining errors can't actually be generated, yet.
4074 assert(reason
== ZPOOL_STATUS_OK
);
4078 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4081 if (config
!= NULL
) {
4084 nvlist_t
**spares
, **l2cache
;
4085 uint_t nspares
, nl2cache
;
4086 pool_scan_stat_t
*ps
= NULL
;
4088 (void) nvlist_lookup_uint64_array(nvroot
,
4089 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4090 print_scan_status(ps
);
4092 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4096 (void) printf(gettext("config:\n\n"));
4097 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4098 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4099 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4100 namewidth
, 0, B_FALSE
);
4102 if (num_logs(nvroot
) > 0)
4103 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4104 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4105 &l2cache
, &nl2cache
) == 0)
4106 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4108 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4109 &spares
, &nspares
) == 0)
4110 print_spares(zhp
, spares
, nspares
, namewidth
);
4112 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4114 nvlist_t
*nverrlist
= NULL
;
4117 * If the approximate error count is small, get a
4118 * precise count by fetching the entire log and
4119 * uniquifying the results.
4121 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4122 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4127 while ((elem
= nvlist_next_nvpair(nverrlist
,
4132 nvlist_free(nverrlist
);
4134 (void) printf("\n");
4137 (void) printf(gettext("errors: No known data "
4139 else if (!cbp
->cb_verbose
)
4140 (void) printf(gettext("errors: %llu data "
4141 "errors, use '-v' for a list\n"),
4142 (u_longlong_t
)nerr
);
4144 print_error_log(zhp
);
4147 if (cbp
->cb_dedup_stats
)
4148 print_dedup_stats(config
);
4150 (void) printf(gettext("config: The configuration cannot be "
4158 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4160 * -v Display complete error logs
4161 * -x Display only pools with potential problems
4162 * -D Display dedup status (undocumented)
4163 * -T Display a timestamp in date(1) or Unix format
4165 * Describes the health status of all pools or some subset.
4168 zpool_do_status(int argc
, char **argv
)
4172 unsigned long interval
= 0, count
= 0;
4173 status_cbdata_t cb
= { 0 };
4176 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4179 cb
.cb_verbose
= B_TRUE
;
4182 cb
.cb_explain
= B_TRUE
;
4185 cb
.cb_dedup_stats
= B_TRUE
;
4188 get_timestamp_arg(*optarg
);
4191 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4200 get_interval_count(&argc
, argv
, &interval
, &count
);
4203 cb
.cb_allpools
= B_TRUE
;
4205 cb
.cb_first
= B_TRUE
;
4208 if (timestamp_fmt
!= NODATE
)
4209 print_timestamp(timestamp_fmt
);
4211 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4212 status_callback
, &cb
);
4214 if (argc
== 0 && cb
.cb_count
== 0)
4215 (void) printf(gettext("no pools available\n"));
4216 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4217 (void) printf(gettext("all pools are healthy\n"));
4225 if (count
!= 0 && --count
== 0)
4228 (void) sleep(interval
);
4234 typedef struct upgrade_cbdata
{
4239 uint64_t cb_version
;
4244 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4246 upgrade_cbdata_t
*cbp
= arg
;
4251 config
= zpool_get_config(zhp
, NULL
);
4252 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4255 if (!cbp
->cb_newer
&& SPA_VERSION_IS_SUPPORTED(version
) &&
4256 version
!= SPA_VERSION
) {
4258 if (cbp
->cb_first
) {
4259 (void) printf(gettext("The following pools are "
4260 "out of date, and can be upgraded. After "
4261 "being\nupgraded, these pools will no "
4262 "longer be accessible by older software "
4264 (void) printf(gettext("VER POOL\n"));
4265 (void) printf(gettext("--- ------------\n"));
4266 cbp
->cb_first
= B_FALSE
;
4269 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4270 zpool_get_name(zhp
));
4272 cbp
->cb_first
= B_FALSE
;
4273 ret
= zpool_upgrade(zhp
, cbp
->cb_version
);
4275 (void) printf(gettext("Successfully upgraded "
4276 "'%s'\n\n"), zpool_get_name(zhp
));
4279 * If they did "zpool upgrade -a", then we could
4280 * be doing ioctls to different pools. We need
4281 * to log this history once to each pool, and bypass
4282 * the normal history logging that happens in main().
4284 (void) zpool_log_history(g_zfs
, history_str
);
4285 log_history
= B_FALSE
;
4287 } else if (cbp
->cb_newer
&& !SPA_VERSION_IS_SUPPORTED(version
)) {
4288 assert(!cbp
->cb_all
);
4290 if (cbp
->cb_first
) {
4291 (void) printf(gettext("The following pools are "
4292 "formatted using an unsupported software version "
4293 "and\ncannot be accessed on the current "
4295 (void) printf(gettext("VER POOL\n"));
4296 (void) printf(gettext("--- ------------\n"));
4297 cbp
->cb_first
= B_FALSE
;
4300 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4301 zpool_get_name(zhp
));
4310 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4312 upgrade_cbdata_t
*cbp
= data
;
4313 uint64_t cur_version
;
4316 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4317 (void) printf(gettext("'log' is now a reserved word\n"
4318 "Pool 'log' must be renamed using export and import"
4323 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4324 if (cur_version
> cbp
->cb_version
) {
4325 (void) printf(gettext("Pool '%s' is already formatted "
4326 "using more current version '%llu'.\n"),
4327 zpool_get_name(zhp
), cur_version
);
4330 if (cur_version
== cbp
->cb_version
) {
4331 (void) printf(gettext("Pool '%s' is already formatted "
4332 "using the current version.\n"), zpool_get_name(zhp
));
4336 ret
= zpool_upgrade(zhp
, cbp
->cb_version
);
4339 (void) printf(gettext("Successfully upgraded '%s' "
4340 "from version %llu to version %llu\n\n"),
4341 zpool_get_name(zhp
), (u_longlong_t
)cur_version
,
4342 (u_longlong_t
)cbp
->cb_version
);
4351 * zpool upgrade [-V version] <-a | pool ...>
4353 * With no arguments, display downrev'd ZFS pool available for upgrade.
4354 * Individual pools can be upgraded by specifying the pool, and '-a' will
4355 * upgrade all pools.
4358 zpool_do_upgrade(int argc
, char **argv
)
4361 upgrade_cbdata_t cb
= { 0 };
4363 boolean_t showversions
= B_FALSE
;
4368 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
4374 showversions
= B_TRUE
;
4377 cb
.cb_version
= strtoll(optarg
, &end
, 10);
4379 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
4380 (void) fprintf(stderr
,
4381 gettext("invalid version '%s'\n"), optarg
);
4386 (void) fprintf(stderr
, gettext("missing argument for "
4387 "'%c' option\n"), optopt
);
4391 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4402 if (cb
.cb_version
== 0) {
4403 cb
.cb_version
= SPA_VERSION
;
4404 } else if (!cb
.cb_all
&& argc
== 0) {
4405 (void) fprintf(stderr
, gettext("-V option is "
4406 "incompatible with other arguments\n"));
4411 if (cb
.cb_all
|| argc
!= 0) {
4412 (void) fprintf(stderr
, gettext("-v option is "
4413 "incompatible with other arguments\n"));
4416 } else if (cb
.cb_all
) {
4418 (void) fprintf(stderr
, gettext("-a option should not "
4419 "be used along with a pool name\n"));
4424 (void) printf(gettext("This system supports ZFS pool feature "
4426 cb
.cb_first
= B_TRUE
;
4428 (void) printf(gettext("The following versions are "
4430 (void) printf(gettext("VER DESCRIPTION\n"));
4431 (void) printf("--- -----------------------------------------"
4432 "---------------\n");
4433 (void) printf(gettext(" 1 Initial ZFS version\n"));
4434 (void) printf(gettext(" 2 Ditto blocks "
4435 "(replicated metadata)\n"));
4436 (void) printf(gettext(" 3 Hot spares and double parity "
4438 (void) printf(gettext(" 4 zpool history\n"));
4439 (void) printf(gettext(" 5 Compression using the gzip "
4441 (void) printf(gettext(" 6 bootfs pool property\n"));
4442 (void) printf(gettext(" 7 Separate intent log devices\n"));
4443 (void) printf(gettext(" 8 Delegated administration\n"));
4444 (void) printf(gettext(" 9 refquota and refreservation "
4446 (void) printf(gettext(" 10 Cache devices\n"));
4447 (void) printf(gettext(" 11 Improved scrub performance\n"));
4448 (void) printf(gettext(" 12 Snapshot properties\n"));
4449 (void) printf(gettext(" 13 snapused property\n"));
4450 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4451 (void) printf(gettext(" 15 user/group space accounting\n"));
4452 (void) printf(gettext(" 16 stmf property support\n"));
4453 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4454 (void) printf(gettext(" 18 Snapshot user holds\n"));
4455 (void) printf(gettext(" 19 Log device removal\n"));
4456 (void) printf(gettext(" 20 Compression using zle "
4457 "(zero-length encoding)\n"));
4458 (void) printf(gettext(" 21 Deduplication\n"));
4459 (void) printf(gettext(" 22 Received properties\n"));
4460 (void) printf(gettext(" 23 Slim ZIL\n"));
4461 (void) printf(gettext(" 24 System attributes\n"));
4462 (void) printf(gettext(" 25 Improved scrub stats\n"));
4463 (void) printf(gettext(" 26 Improved snapshot deletion "
4465 (void) printf(gettext(" 27 Improved snapshot creation "
4467 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4468 (void) printf(gettext("\nFor more information on a particular "
4469 "version, including supported releases,\n"));
4470 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4471 } else if (argc
== 0) {
4474 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
4475 notfound
= cb
.cb_first
;
4477 if (!cb
.cb_all
&& ret
== 0) {
4479 (void) printf("\n");
4480 cb
.cb_first
= B_TRUE
;
4481 cb
.cb_newer
= B_TRUE
;
4482 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
4485 (void) printf("\n");
4491 (void) printf(gettext("All pools are formatted "
4492 "using this version.\n"));
4493 else if (!cb
.cb_all
)
4494 (void) printf(gettext("Use 'zpool upgrade -v' "
4495 "for a list of available versions and "
4496 "their associated\nfeatures.\n"));
4499 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
4506 typedef struct hist_cbdata
{
4513 * Print out the command history for a specific pool.
4516 get_history_one(zpool_handle_t
*zhp
, void *data
)
4522 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
4524 cb
->first
= B_FALSE
;
4526 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
4528 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
4531 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
4532 &records
, &numrecords
) == 0);
4533 for (i
= 0; i
< numrecords
; i
++) {
4534 nvlist_t
*rec
= records
[i
];
4537 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
4541 tsec
= fnvlist_lookup_uint64(records
[i
],
4543 (void) localtime_r(&tsec
, &t
);
4544 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
4547 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
4548 (void) printf("%s %s", tbuf
,
4549 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
4550 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
4552 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
4555 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
4556 (void) printf("%s unrecognized record:\n",
4558 dump_nvlist(rec
, 4);
4561 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
4562 zfs_history_event_names
[ievent
],
4563 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
4564 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
4565 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
4568 (void) printf("%s [txg:%lld] %s", tbuf
,
4569 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
4570 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
4571 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
4572 (void) printf(" %s (%llu)",
4573 fnvlist_lookup_string(rec
,
4575 fnvlist_lookup_uint64(rec
,
4578 (void) printf(" %s", fnvlist_lookup_string(rec
,
4579 ZPOOL_HIST_INT_STR
));
4580 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
4583 (void) printf("%s ioctl %s\n", tbuf
,
4584 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
4585 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
4586 (void) printf(" input:\n");
4587 dump_nvlist(fnvlist_lookup_nvlist(rec
,
4588 ZPOOL_HIST_INPUT_NVL
), 8);
4590 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
4591 (void) printf(" output:\n");
4592 dump_nvlist(fnvlist_lookup_nvlist(rec
,
4593 ZPOOL_HIST_OUTPUT_NVL
), 8);
4598 (void) printf("%s unrecognized record:\n", tbuf
);
4599 dump_nvlist(rec
, 4);
4603 (void) printf("\n");
4606 (void) printf(" [");
4607 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
4608 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
4609 struct passwd
*pwd
= getpwuid(who
);
4610 (void) printf("user %d ", (int)who
);
4612 (void) printf("(%s) ", pwd
->pw_name
);
4614 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
4615 (void) printf("on %s",
4616 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
4618 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
4619 (void) printf(":%s",
4620 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
4623 (void) printf("\n");
4625 (void) printf("\n");
4632 * zpool history <pool>
4634 * Displays the history of commands that modified pools.
4637 zpool_do_history(int argc
, char **argv
)
4639 hist_cbdata_t cbdata
= { 0 };
4643 cbdata
.first
= B_TRUE
;
4645 while ((c
= getopt(argc
, argv
, "li")) != -1) {
4648 cbdata
.longfmt
= B_TRUE
;
4651 cbdata
.internal
= B_TRUE
;
4654 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4662 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
4665 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
4666 (void) printf(gettext("no pools available\n"));
4674 get_callback(zpool_handle_t
*zhp
, void *data
)
4676 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
4677 char value
[MAXNAMELEN
];
4678 zprop_source_t srctype
;
4681 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
4684 * Skip the special fake placeholder. This will also skip
4685 * over the name property when 'all' is specified.
4687 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
4688 pl
== cbp
->cb_proplist
)
4691 if (pl
->pl_prop
== ZPROP_INVAL
&&
4692 (zpool_prop_feature(pl
->pl_user_prop
) ||
4693 zpool_prop_unsupported(pl
->pl_user_prop
))) {
4694 srctype
= ZPROP_SRC_LOCAL
;
4696 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
4697 value
, sizeof (value
)) == 0) {
4698 zprop_print_one_property(zpool_get_name(zhp
),
4699 cbp
, pl
->pl_user_prop
, value
, srctype
,
4703 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
4704 sizeof (value
), &srctype
) != 0)
4707 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
4708 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
4716 zpool_do_get(int argc
, char **argv
)
4718 zprop_get_cbdata_t cb
= { 0 };
4719 zprop_list_t fake_name
= { 0 };
4723 (void) fprintf(stderr
, gettext("missing property "
4728 cb
.cb_first
= B_TRUE
;
4729 cb
.cb_sources
= ZPROP_SRC_ALL
;
4730 cb
.cb_columns
[0] = GET_COL_NAME
;
4731 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
4732 cb
.cb_columns
[2] = GET_COL_VALUE
;
4733 cb
.cb_columns
[3] = GET_COL_SOURCE
;
4734 cb
.cb_type
= ZFS_TYPE_POOL
;
4736 if (zprop_get_list(g_zfs
, argv
[1], &cb
.cb_proplist
,
4737 ZFS_TYPE_POOL
) != 0)
4740 if (cb
.cb_proplist
!= NULL
) {
4741 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
4742 fake_name
.pl_width
= strlen(gettext("NAME"));
4743 fake_name
.pl_next
= cb
.cb_proplist
;
4744 cb
.cb_proplist
= &fake_name
;
4747 ret
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, &cb
.cb_proplist
,
4750 if (cb
.cb_proplist
== &fake_name
)
4751 zprop_free_list(fake_name
.pl_next
);
4753 zprop_free_list(cb
.cb_proplist
);
4758 typedef struct set_cbdata
{
4761 boolean_t cb_any_successful
;
4765 set_callback(zpool_handle_t
*zhp
, void *data
)
4768 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
4770 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
4773 cb
->cb_any_successful
= B_TRUE
;
4779 zpool_do_set(int argc
, char **argv
)
4781 set_cbdata_t cb
= { 0 };
4784 if (argc
> 1 && argv
[1][0] == '-') {
4785 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4791 (void) fprintf(stderr
, gettext("missing property=value "
4797 (void) fprintf(stderr
, gettext("missing pool name\n"));
4802 (void) fprintf(stderr
, gettext("too many pool names\n"));
4806 cb
.cb_propname
= argv
[1];
4807 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
4808 if (cb
.cb_value
== NULL
) {
4809 (void) fprintf(stderr
, gettext("missing value in "
4810 "property=value argument\n"));
4814 *(cb
.cb_value
) = '\0';
4817 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
4824 find_command_idx(char *command
, int *idx
)
4828 for (i
= 0; i
< NCOMMAND
; i
++) {
4829 if (command_table
[i
].name
== NULL
)
4832 if (strcmp(command
, command_table
[i
].name
) == 0) {
4841 main(int argc
, char **argv
)
4847 (void) setlocale(LC_ALL
, "");
4848 (void) textdomain(TEXT_DOMAIN
);
4850 if ((g_zfs
= libzfs_init()) == NULL
) {
4851 (void) fprintf(stderr
, gettext("internal error: failed to "
4852 "initialize ZFS library\n"));
4856 libzfs_print_on_error(g_zfs
, B_TRUE
);
4861 * Make sure the user has specified some command.
4864 (void) fprintf(stderr
, gettext("missing command\n"));
4873 if (strcmp(cmdname
, "-?") == 0)
4876 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
4879 * Run the appropriate command.
4881 if (find_command_idx(cmdname
, &i
) == 0) {
4882 current_command
= &command_table
[i
];
4883 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
4884 } else if (strchr(cmdname
, '=')) {
4885 verify(find_command_idx("set", &i
) == 0);
4886 current_command
= &command_table
[i
];
4887 ret
= command_table
[i
].func(argc
, argv
);
4888 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
4890 * 'freeze' is a vile debugging abomination, so we treat
4894 int fd
= open(ZFS_DEV
, O_RDWR
);
4895 (void) strcpy((void *)buf
, argv
[2]);
4896 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
4898 (void) fprintf(stderr
, gettext("unrecognized "
4899 "command '%s'\n"), cmdname
);
4903 if (ret
== 0 && log_history
)
4904 (void) zpool_log_history(g_zfs
, history_str
);
4909 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4910 * for the purposes of running ::findleaks.
4912 if (getenv("ZFS_ABORT") != NULL
) {
4913 (void) printf("dumping core by request\n");