4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
48 #include <sys/fs/zfs.h>
53 #include "zpool_util.h"
54 #include "zfs_comutil.h"
55 #include "zfeature_common.h"
57 #include "statcommon.h"
59 static int zpool_do_create(int, char **);
60 static int zpool_do_destroy(int, char **);
62 static int zpool_do_add(int, char **);
63 static int zpool_do_remove(int, char **);
65 static int zpool_do_list(int, char **);
66 static int zpool_do_iostat(int, char **);
67 static int zpool_do_status(int, char **);
69 static int zpool_do_online(int, char **);
70 static int zpool_do_offline(int, char **);
71 static int zpool_do_clear(int, char **);
72 static int zpool_do_reopen(int, char **);
74 static int zpool_do_reguid(int, char **);
76 static int zpool_do_attach(int, char **);
77 static int zpool_do_detach(int, char **);
78 static int zpool_do_replace(int, char **);
79 static int zpool_do_split(int, char **);
81 static int zpool_do_scrub(int, char **);
83 static int zpool_do_import(int, char **);
84 static int zpool_do_export(int, char **);
86 static int zpool_do_upgrade(int, char **);
88 static int zpool_do_history(int, char **);
90 static int zpool_do_get(int, char **);
91 static int zpool_do_set(int, char **);
94 * These libumem hooks provide a reasonable set of defaults for the allocator's
95 * debugging facilities.
100 _umem_debug_init(void)
102 return ("default,verbose"); /* $UMEM_DEBUG setting */
106 _umem_logging_init(void)
108 return ("fail,contents"); /* $UMEM_LOGGING setting */
139 typedef struct zpool_command
{
141 int (*func
)(int, char **);
146 * Master command table. Each ZFS command has a name, associated function, and
147 * usage message. The usage messages need to be internationalized, so we have
148 * to have a function to return the usage message based on a command index.
150 * These commands are organized according to how they are displayed in the usage
151 * message. An empty command (one with a NULL name) indicates an empty line in
152 * the generic usage message.
154 static zpool_command_t command_table
[] = {
155 { "create", zpool_do_create
, HELP_CREATE
},
156 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
158 { "add", zpool_do_add
, HELP_ADD
},
159 { "remove", zpool_do_remove
, HELP_REMOVE
},
161 { "list", zpool_do_list
, HELP_LIST
},
162 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
163 { "status", zpool_do_status
, HELP_STATUS
},
165 { "online", zpool_do_online
, HELP_ONLINE
},
166 { "offline", zpool_do_offline
, HELP_OFFLINE
},
167 { "clear", zpool_do_clear
, HELP_CLEAR
},
168 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
170 { "attach", zpool_do_attach
, HELP_ATTACH
},
171 { "detach", zpool_do_detach
, HELP_DETACH
},
172 { "replace", zpool_do_replace
, HELP_REPLACE
},
173 { "split", zpool_do_split
, HELP_SPLIT
},
175 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
177 { "import", zpool_do_import
, HELP_IMPORT
},
178 { "export", zpool_do_export
, HELP_EXPORT
},
179 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
180 { "reguid", zpool_do_reguid
, HELP_REGUID
},
182 { "history", zpool_do_history
, HELP_HISTORY
},
183 { "get", zpool_do_get
, HELP_GET
},
184 { "set", zpool_do_set
, HELP_SET
},
187 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
189 static zpool_command_t
*current_command
;
190 static char history_str
[HIS_MAX_RECORD_LEN
];
191 static boolean_t log_history
= B_TRUE
;
192 static uint_t timestamp_fmt
= NODATE
;
195 get_usage(zpool_help_t idx
) {
198 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
200 return (gettext("\tattach [-f] <pool> <device> "
203 return (gettext("\tclear [-nF] <pool> [device]\n"));
205 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
206 "\t [-O file-system-property=value] ... \n"
207 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
209 return (gettext("\tdestroy [-f] <pool>\n"));
211 return (gettext("\tdetach <pool> <device>\n"));
213 return (gettext("\texport [-f] <pool> ...\n"));
215 return (gettext("\thistory [-il] [<pool>] ...\n"));
217 return (gettext("\timport [-d dir] [-D]\n"
218 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
219 "\timport [-o mntopts] [-o property=value] ... \n"
220 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
221 "[-R root] [-F [-n]] -a\n"
222 "\timport [-o mntopts] [-o property=value] ... \n"
223 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
224 "[-R root] [-F [-n]]\n"
225 "\t <pool | id> [newpool]\n"));
227 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
230 return (gettext("\tlist [-Hp] [-o property[,...]] "
231 "[-T d|u] [pool] ... [interval [count]]\n"));
233 return (gettext("\toffline [-t] <pool> <device> ...\n"));
235 return (gettext("\tonline <pool> <device> ...\n"));
237 return (gettext("\treplace [-f] <pool> <device> "
240 return (gettext("\tremove <pool> <device> ...\n"));
242 return (gettext("\treopen <pool>\n"));
244 return (gettext("\tscrub [-s] <pool> ...\n"));
246 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
249 return (gettext("\tupgrade\n"
251 "\tupgrade [-V version] <-a | pool ...>\n"));
253 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
254 "<\"all\" | property[,...]> <pool> ...\n"));
256 return (gettext("\tset <property=value> <pool> \n"));
258 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
259 "\t [-o property=value] <pool> <newpool> "
260 "[<device> ...]\n"));
262 return (gettext("\treguid <pool>\n"));
271 * Callback routine that will print out a pool property value.
274 print_prop_cb(int prop
, void *cb
)
278 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
280 if (zpool_prop_readonly(prop
))
281 (void) fprintf(fp
, " NO ");
283 (void) fprintf(fp
, " YES ");
285 if (zpool_prop_values(prop
) == NULL
)
286 (void) fprintf(fp
, "-\n");
288 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
294 * Display usage message. If we're inside a command, display only the usage for
295 * that command. Otherwise, iterate over the entire command table and display
296 * a complete usage message.
299 usage(boolean_t requested
)
301 FILE *fp
= requested
? stdout
: stderr
;
303 if (current_command
== NULL
) {
306 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
308 gettext("where 'command' is one of the following:\n\n"));
310 for (i
= 0; i
< NCOMMAND
; i
++) {
311 if (command_table
[i
].name
== NULL
)
312 (void) fprintf(fp
, "\n");
314 (void) fprintf(fp
, "%s",
315 get_usage(command_table
[i
].usage
));
318 (void) fprintf(fp
, gettext("usage:\n"));
319 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
322 if (current_command
!= NULL
&&
323 ((strcmp(current_command
->name
, "set") == 0) ||
324 (strcmp(current_command
->name
, "get") == 0) ||
325 (strcmp(current_command
->name
, "list") == 0))) {
328 gettext("\nthe following properties are supported:\n"));
330 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
331 "PROPERTY", "EDIT", "VALUES");
333 /* Iterate over all properties */
334 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
337 (void) fprintf(fp
, "\t%-15s ", "feature@...");
338 (void) fprintf(fp
, "YES disabled | enabled | active\n");
340 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
341 "appended with a feature name.\nSee zpool-features(5).\n"));
345 * See comments at end of main().
347 if (getenv("ZFS_ABORT") != NULL
) {
348 (void) printf("dumping core by request\n");
352 exit(requested
? 0 : 2);
356 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
357 boolean_t print_logs
)
364 (void) printf("\t%*s%s\n", indent
, "", name
);
366 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
367 &child
, &children
) != 0)
370 for (c
= 0; c
< children
; c
++) {
371 uint64_t is_log
= B_FALSE
;
373 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
375 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
378 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
379 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
386 prop_list_contains_feature(nvlist_t
*proplist
)
389 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
390 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
391 if (zpool_prop_feature(nvpair_name(nvp
)))
398 * Add a property pair (name, string-value) into a property nvlist.
401 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
404 zpool_prop_t prop
= ZPROP_INVAL
;
410 if (*props
== NULL
&&
411 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
412 (void) fprintf(stderr
,
413 gettext("internal error: out of memory\n"));
420 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
422 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
423 !zpool_prop_feature(propname
)) {
424 (void) fprintf(stderr
, gettext("property '%s' is "
425 "not a valid pool property\n"), propname
);
430 * feature@ properties and version should not be specified
433 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
434 nvlist_exists(proplist
, vname
)) ||
435 (prop
== ZPOOL_PROP_VERSION
&&
436 prop_list_contains_feature(proplist
))) {
437 (void) fprintf(stderr
, gettext("'feature@' and "
438 "'version' properties cannot be specified "
444 if (zpool_prop_feature(propname
))
447 normnm
= zpool_prop_to_name(prop
);
449 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
450 normnm
= zfs_prop_to_name(fprop
);
456 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
457 prop
!= ZPOOL_PROP_CACHEFILE
) {
458 (void) fprintf(stderr
, gettext("property '%s' "
459 "specified multiple times\n"), propname
);
463 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
464 (void) fprintf(stderr
, gettext("internal "
465 "error: out of memory\n"));
473 * zpool add [-fn] <pool> <vdev> ...
475 * -f Force addition of devices, even if they appear in use
476 * -n Do not add the devices, but display the resulting layout if
477 * they were to be added.
479 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
480 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
484 zpool_do_add(int argc
, char **argv
)
486 boolean_t force
= B_FALSE
;
487 boolean_t dryrun
= B_FALSE
;
496 while ((c
= getopt(argc
, argv
, "fn")) != -1) {
505 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
514 /* get pool name and check number of arguments */
516 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
520 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
529 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
532 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
533 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
539 /* pass off to get_vdev_spec for processing */
540 nvroot
= make_root_vdev(zhp
, force
, !force
, B_FALSE
, dryrun
,
542 if (nvroot
== NULL
) {
548 nvlist_t
*poolnvroot
;
550 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
553 (void) printf(gettext("would update '%s' to the following "
554 "configuration:\n"), zpool_get_name(zhp
));
556 /* print original main pool and new tree */
557 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
558 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
560 /* Do the same for the logs */
561 if (num_logs(poolnvroot
) > 0) {
562 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
563 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
564 } else if (num_logs(nvroot
) > 0) {
565 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
570 ret
= (zpool_add(zhp
, nvroot
) != 0);
580 * zpool remove <pool> <vdev> ...
582 * Removes the given vdev from the pool. Currently, this supports removing
583 * spares, cache, and log devices from the pool.
586 zpool_do_remove(int argc
, char **argv
)
595 /* get pool name and check number of arguments */
597 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
601 (void) fprintf(stderr
, gettext("missing device\n"));
607 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
610 for (i
= 1; i
< argc
; i
++) {
611 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
619 * zpool create [-fnd] [-o property=value] ...
620 * [-O file-system-property=value] ...
621 * [-R root] [-m mountpoint] <pool> <dev> ...
623 * -f Force creation, even if devices appear in use
624 * -n Do not create the pool, but display the resulting layout if it
625 * were to be created.
626 * -R Create a pool under an alternate root
627 * -m Set default mountpoint for the root dataset. By default it's
629 * -o Set property=value.
630 * -d Don't automatically enable all supported pool features
631 * (individual features can be enabled with -o).
632 * -O Set fsproperty=value in the pool's root file system
634 * Creates the named pool according to the given vdev specification. The
635 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
636 * we get the nvlist back from get_vdev_spec(), we either print out the contents
637 * (if '-n' was specified), or pass it to libzfs to do the creation.
640 zpool_do_create(int argc
, char **argv
)
642 boolean_t force
= B_FALSE
;
643 boolean_t dryrun
= B_FALSE
;
644 boolean_t enable_all_pool_feat
= B_TRUE
;
646 nvlist_t
*nvroot
= NULL
;
649 char *altroot
= NULL
;
650 char *mountpoint
= NULL
;
651 nvlist_t
*fsprops
= NULL
;
652 nvlist_t
*props
= NULL
;
656 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:")) != -1) {
665 enable_all_pool_feat
= B_FALSE
;
669 if (add_prop_list(zpool_prop_to_name(
670 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
672 if (nvlist_lookup_string(props
,
673 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
676 if (add_prop_list(zpool_prop_to_name(
677 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
681 /* Equivalent to -O mountpoint=optarg */
685 if ((propval
= strchr(optarg
, '=')) == NULL
) {
686 (void) fprintf(stderr
, gettext("missing "
687 "'=' for -o option\n"));
693 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
697 * If the user is creating a pool that doesn't support
698 * feature flags, don't enable any features.
700 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
704 ver
= strtoull(propval
, &end
, 10);
706 ver
< SPA_VERSION_FEATURES
) {
707 enable_all_pool_feat
= B_FALSE
;
712 if ((propval
= strchr(optarg
, '=')) == NULL
) {
713 (void) fprintf(stderr
, gettext("missing "
714 "'=' for -O option\n"));
721 * Mountpoints are checked and then added later.
722 * Uniquely among properties, they can be specified
723 * more than once, to avoid conflict with -m.
725 if (0 == strcmp(optarg
,
726 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
727 mountpoint
= propval
;
728 } else if (add_prop_list(optarg
, propval
, &fsprops
,
734 (void) fprintf(stderr
, gettext("missing argument for "
735 "'%c' option\n"), optopt
);
738 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
747 /* get pool name and check number of arguments */
749 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
753 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
760 * As a special case, check for use of '/' in the name, and direct the
761 * user to use 'zfs create' instead.
763 if (strchr(poolname
, '/') != NULL
) {
764 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
765 "character '/' in pool name\n"), poolname
);
766 (void) fprintf(stderr
, gettext("use 'zfs create' to "
767 "create a dataset\n"));
771 /* pass off to get_vdev_spec for bulk processing */
772 nvroot
= make_root_vdev(NULL
, force
, !force
, B_FALSE
, dryrun
,
777 /* make_root_vdev() allows 0 toplevel children if there are spares */
778 if (!zfs_allocatable_devs(nvroot
)) {
779 (void) fprintf(stderr
, gettext("invalid vdev "
780 "specification: at least one toplevel vdev must be "
785 if (altroot
!= NULL
&& altroot
[0] != '/') {
786 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
787 "must be an absolute path\n"), altroot
);
792 * Check the validity of the mountpoint and direct the user to use the
793 * '-m' mountpoint option if it looks like its in use.
795 if (mountpoint
== NULL
||
796 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
797 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
798 char buf
[MAXPATHLEN
];
801 if (mountpoint
&& mountpoint
[0] != '/') {
802 (void) fprintf(stderr
, gettext("invalid mountpoint "
803 "'%s': must be an absolute path, 'legacy', or "
804 "'none'\n"), mountpoint
);
808 if (mountpoint
== NULL
) {
810 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
813 (void) snprintf(buf
, sizeof (buf
), "/%s",
817 (void) snprintf(buf
, sizeof (buf
), "%s%s",
818 altroot
, mountpoint
);
820 (void) snprintf(buf
, sizeof (buf
), "%s",
824 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
825 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
826 "%s\n"), buf
, strerror(errno
));
827 (void) fprintf(stderr
, gettext("use '-m' "
828 "option to provide a different default\n"));
833 while (count
< 3 && readdir(dirp
) != NULL
)
835 (void) closedir(dirp
);
838 (void) fprintf(stderr
, gettext("mountpoint "
839 "'%s' exists and is not empty\n"), buf
);
840 (void) fprintf(stderr
, gettext("use '-m' "
841 "option to provide a "
842 "different default\n"));
849 * Now that the mountpoint's validity has been checked, ensure that
850 * the property is set appropriately prior to creating the pool.
852 if (mountpoint
!= NULL
) {
853 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
854 mountpoint
, &fsprops
, B_FALSE
);
862 * For a dry run invocation, print out a basic message and run
863 * through all the vdevs in the list and print out in an
864 * appropriate hierarchy.
866 (void) printf(gettext("would create '%s' with the "
867 "following layout:\n\n"), poolname
);
869 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
870 if (num_logs(nvroot
) > 0)
871 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
876 * Hand off to libzfs.
878 if (enable_all_pool_feat
) {
880 for (i
= 0; i
< SPA_FEATURES
; i
++) {
881 char propname
[MAXPATHLEN
];
882 zfeature_info_t
*feat
= &spa_feature_table
[i
];
884 (void) snprintf(propname
, sizeof (propname
),
885 "feature@%s", feat
->fi_uname
);
888 * Skip feature if user specified it manually
889 * on the command line.
891 if (nvlist_exists(props
, propname
))
894 ret
= add_prop_list(propname
,
895 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
902 if (zpool_create(g_zfs
, poolname
,
903 nvroot
, props
, fsprops
) == 0) {
904 zfs_handle_t
*pool
= zfs_open(g_zfs
, poolname
,
905 ZFS_TYPE_FILESYSTEM
);
907 if (zfs_mount(pool
, NULL
, 0) == 0)
908 ret
= zfs_shareall(pool
);
911 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
912 (void) fprintf(stderr
, gettext("pool name may have "
919 nvlist_free(fsprops
);
923 nvlist_free(fsprops
);
930 * zpool destroy <pool>
932 * -f Forcefully unmount any datasets
934 * Destroy the given pool. Automatically unmounts any datasets in the pool.
937 zpool_do_destroy(int argc
, char **argv
)
939 boolean_t force
= B_FALSE
;
946 while ((c
= getopt(argc
, argv
, "f")) != -1) {
952 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
961 /* check arguments */
963 (void) fprintf(stderr
, gettext("missing pool argument\n"));
967 (void) fprintf(stderr
, gettext("too many arguments\n"));
973 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
975 * As a special case, check for use of '/' in the name, and
976 * direct the user to use 'zfs destroy' instead.
978 if (strchr(pool
, '/') != NULL
)
979 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
980 "destroy a dataset\n"));
984 if (zpool_disable_datasets(zhp
, force
) != 0) {
985 (void) fprintf(stderr
, gettext("could not destroy '%s': "
986 "could not unmount datasets\n"), zpool_get_name(zhp
));
990 /* The history must be logged as part of the export */
991 log_history
= B_FALSE
;
993 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1001 * zpool export [-f] <pool> ...
1003 * -f Forcefully unmount datasets
1005 * Export the given pools. By default, the command will attempt to cleanly
1006 * unmount any active datasets within the pool. If the '-f' flag is specified,
1007 * then the datasets will be forcefully unmounted.
1010 zpool_do_export(int argc
, char **argv
)
1012 boolean_t force
= B_FALSE
;
1013 boolean_t hardforce
= B_FALSE
;
1015 zpool_handle_t
*zhp
;
1020 while ((c
= getopt(argc
, argv
, "fF")) != -1) {
1029 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1038 /* check arguments */
1040 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1045 for (i
= 0; i
< argc
; i
++) {
1046 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) == NULL
) {
1051 if (zpool_disable_datasets(zhp
, force
) != 0) {
1057 /* The history must be logged as part of the export */
1058 log_history
= B_FALSE
;
1061 if (zpool_export_force(zhp
, history_str
) != 0)
1063 } else if (zpool_export(zhp
, force
, history_str
) != 0) {
1074 * Given a vdev configuration, determine the maximum width needed for the device
1078 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1080 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1085 if (strlen(name
) + depth
> max
)
1086 max
= strlen(name
) + depth
;
1090 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1091 &child
, &children
) == 0) {
1092 for (c
= 0; c
< children
; c
++)
1093 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1098 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1099 &child
, &children
) == 0) {
1100 for (c
= 0; c
< children
; c
++)
1101 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1106 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1107 &child
, &children
) == 0) {
1108 for (c
= 0; c
< children
; c
++)
1109 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1118 typedef struct spare_cbdata
{
1120 zpool_handle_t
*cb_zhp
;
1124 find_vdev(nvlist_t
*nv
, uint64_t search
)
1130 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1134 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1135 &child
, &children
) == 0) {
1136 for (c
= 0; c
< children
; c
++)
1137 if (find_vdev(child
[c
], search
))
1145 find_spare(zpool_handle_t
*zhp
, void *data
)
1147 spare_cbdata_t
*cbp
= data
;
1148 nvlist_t
*config
, *nvroot
;
1150 config
= zpool_get_config(zhp
, NULL
);
1151 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1154 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1164 * Print out configuration state as requested by status_callback.
1167 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1168 int namewidth
, int depth
, boolean_t isspare
)
1172 pool_scan_stat_t
*ps
= NULL
;
1174 char rbuf
[6], wbuf
[6], cbuf
[6];
1176 uint64_t notpresent
;
1180 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1181 &child
, &children
) != 0)
1184 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1185 (uint64_t **)&vs
, &c
) == 0);
1187 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1190 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1193 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1195 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1199 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1203 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1204 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1205 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1206 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1209 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1210 ¬present
) == 0) {
1212 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1213 (void) printf(" was %s", path
);
1214 } else if (vs
->vs_aux
!= 0) {
1217 switch (vs
->vs_aux
) {
1218 case VDEV_AUX_OPEN_FAILED
:
1219 (void) printf(gettext("cannot open"));
1222 case VDEV_AUX_BAD_GUID_SUM
:
1223 (void) printf(gettext("missing device"));
1226 case VDEV_AUX_NO_REPLICAS
:
1227 (void) printf(gettext("insufficient replicas"));
1230 case VDEV_AUX_VERSION_NEWER
:
1231 (void) printf(gettext("newer version"));
1234 case VDEV_AUX_UNSUP_FEAT
:
1235 (void) printf(gettext("unsupported feature(s)"));
1238 case VDEV_AUX_SPARED
:
1239 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1241 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1242 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1243 zpool_get_name(zhp
)) == 0)
1244 (void) printf(gettext("currently in "
1247 (void) printf(gettext("in use by "
1249 zpool_get_name(cb
.cb_zhp
));
1250 zpool_close(cb
.cb_zhp
);
1252 (void) printf(gettext("currently in use"));
1256 case VDEV_AUX_ERR_EXCEEDED
:
1257 (void) printf(gettext("too many errors"));
1260 case VDEV_AUX_IO_FAILURE
:
1261 (void) printf(gettext("experienced I/O failures"));
1264 case VDEV_AUX_BAD_LOG
:
1265 (void) printf(gettext("bad intent log"));
1268 case VDEV_AUX_EXTERNAL
:
1269 (void) printf(gettext("external device fault"));
1272 case VDEV_AUX_SPLIT_POOL
:
1273 (void) printf(gettext("split into new pool"));
1277 (void) printf(gettext("corrupted data"));
1282 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1283 (uint64_t **)&ps
, &c
);
1285 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1286 vs
->vs_scan_processed
!= 0 && children
== 0) {
1287 (void) printf(gettext(" (%s)"),
1288 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1289 "resilvering" : "repairing");
1292 (void) printf("\n");
1294 for (c
= 0; c
< children
; c
++) {
1295 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1297 /* Don't print logs or holes here */
1298 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1300 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1302 if (islog
|| ishole
)
1304 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1305 print_status_config(zhp
, vname
, child
[c
],
1306 namewidth
, depth
+ 2, isspare
);
1313 * Print the configuration of an exported pool. Iterate over all vdevs in the
1314 * pool, printing out the name and status for each one.
1317 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1324 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1325 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1326 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1329 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1330 (uint64_t **)&vs
, &c
) == 0);
1332 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1333 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1335 if (vs
->vs_aux
!= 0) {
1338 switch (vs
->vs_aux
) {
1339 case VDEV_AUX_OPEN_FAILED
:
1340 (void) printf(gettext("cannot open"));
1343 case VDEV_AUX_BAD_GUID_SUM
:
1344 (void) printf(gettext("missing device"));
1347 case VDEV_AUX_NO_REPLICAS
:
1348 (void) printf(gettext("insufficient replicas"));
1351 case VDEV_AUX_VERSION_NEWER
:
1352 (void) printf(gettext("newer version"));
1355 case VDEV_AUX_UNSUP_FEAT
:
1356 (void) printf(gettext("unsupported feature(s)"));
1359 case VDEV_AUX_ERR_EXCEEDED
:
1360 (void) printf(gettext("too many errors"));
1364 (void) printf(gettext("corrupted data"));
1368 (void) printf("\n");
1370 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1371 &child
, &children
) != 0)
1374 for (c
= 0; c
< children
; c
++) {
1375 uint64_t is_log
= B_FALSE
;
1377 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1382 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1383 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1387 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1388 &child
, &children
) == 0) {
1389 (void) printf(gettext("\tcache\n"));
1390 for (c
= 0; c
< children
; c
++) {
1391 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1392 (void) printf("\t %s\n", vname
);
1397 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1398 &child
, &children
) == 0) {
1399 (void) printf(gettext("\tspares\n"));
1400 for (c
= 0; c
< children
; c
++) {
1401 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1402 (void) printf("\t %s\n", vname
);
1410 * Logs are recorded as top level vdevs in the main pool child array
1411 * but with "is_log" set to 1. We use either print_status_config() or
1412 * print_import_config() to print the top level logs then any log
1413 * children (eg mirrored slogs) are printed recursively - which
1414 * works because only the top level vdev is marked "is_log"
1417 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1422 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1426 (void) printf(gettext("\tlogs\n"));
1428 for (c
= 0; c
< children
; c
++) {
1429 uint64_t is_log
= B_FALSE
;
1432 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1436 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1438 print_status_config(zhp
, name
, child
[c
], namewidth
,
1441 print_import_config(name
, child
[c
], namewidth
, 2);
1447 * Display the status for the given pool.
1450 show_import(nvlist_t
*config
)
1452 uint64_t pool_state
;
1464 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1466 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1468 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1470 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1473 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1474 (uint64_t **)&vs
, &vsc
) == 0);
1475 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1477 reason
= zpool_import_status(config
, &msgid
);
1479 (void) printf(gettext(" pool: %s\n"), name
);
1480 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1481 (void) printf(gettext(" state: %s"), health
);
1482 if (pool_state
== POOL_STATE_DESTROYED
)
1483 (void) printf(gettext(" (DESTROYED)"));
1484 (void) printf("\n");
1487 case ZPOOL_STATUS_MISSING_DEV_R
:
1488 case ZPOOL_STATUS_MISSING_DEV_NR
:
1489 case ZPOOL_STATUS_BAD_GUID_SUM
:
1490 (void) printf(gettext(" status: One or more devices are "
1491 "missing from the system.\n"));
1494 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1495 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1496 (void) printf(gettext(" status: One or more devices contains "
1497 "corrupted data.\n"));
1500 case ZPOOL_STATUS_CORRUPT_DATA
:
1502 gettext(" status: The pool data is corrupted.\n"));
1505 case ZPOOL_STATUS_OFFLINE_DEV
:
1506 (void) printf(gettext(" status: One or more devices "
1507 "are offlined.\n"));
1510 case ZPOOL_STATUS_CORRUPT_POOL
:
1511 (void) printf(gettext(" status: The pool metadata is "
1515 case ZPOOL_STATUS_VERSION_OLDER
:
1516 (void) printf(gettext(" status: The pool is formatted using a "
1517 "legacy on-disk version.\n"));
1520 case ZPOOL_STATUS_VERSION_NEWER
:
1521 (void) printf(gettext(" status: The pool is formatted using an "
1522 "incompatible version.\n"));
1525 case ZPOOL_STATUS_FEAT_DISABLED
:
1526 (void) printf(gettext(" status: Some supported features are "
1527 "not enabled on the pool.\n"));
1530 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1531 (void) printf(gettext("status: The pool uses the following "
1532 "feature(s) not supported on this sytem:\n"));
1533 zpool_print_unsup_feat(config
);
1536 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1537 (void) printf(gettext("status: The pool can only be accessed "
1538 "in read-only mode on this system. It\n\tcannot be "
1539 "accessed in read-write mode because it uses the "
1540 "following\n\tfeature(s) not supported on this system:\n"));
1541 zpool_print_unsup_feat(config
);
1544 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1545 (void) printf(gettext(" status: The pool was last accessed by "
1546 "another system.\n"));
1549 case ZPOOL_STATUS_FAULTED_DEV_R
:
1550 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1551 (void) printf(gettext(" status: One or more devices are "
1555 case ZPOOL_STATUS_BAD_LOG
:
1556 (void) printf(gettext(" status: An intent log record cannot be "
1560 case ZPOOL_STATUS_RESILVERING
:
1561 (void) printf(gettext(" status: One or more devices were being "
1567 * No other status can be seen when importing pools.
1569 assert(reason
== ZPOOL_STATUS_OK
);
1573 * Print out an action according to the overall state of the pool.
1575 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1576 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1577 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1578 (void) printf(gettext(" action: The pool can be "
1579 "imported using its name or numeric identifier, "
1580 "though\n\tsome features will not be available "
1581 "without an explicit 'zpool upgrade'.\n"));
1582 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1583 (void) printf(gettext(" action: The pool can be "
1584 "imported using its name or numeric "
1585 "identifier and\n\tthe '-f' flag.\n"));
1587 (void) printf(gettext(" action: The pool can be "
1588 "imported using its name or numeric "
1591 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1592 (void) printf(gettext(" action: The pool can be imported "
1593 "despite missing or damaged devices. The\n\tfault "
1594 "tolerance of the pool may be compromised if imported.\n"));
1597 case ZPOOL_STATUS_VERSION_NEWER
:
1598 (void) printf(gettext(" action: The pool cannot be "
1599 "imported. Access the pool on a system running "
1600 "newer\n\tsoftware, or recreate the pool from "
1603 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1604 (void) printf(gettext("action: The pool cannot be "
1605 "imported. Access the pool on a system that "
1606 "supports\n\tthe required feature(s), or recreate "
1607 "the pool from backup.\n"));
1609 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1610 (void) printf(gettext("action: The pool cannot be "
1611 "imported in read-write mode. Import the pool "
1613 "\t\"-o readonly=on\", access the pool on a system "
1614 "that supports the\n\trequired feature(s), or "
1615 "recreate the pool from backup.\n"));
1617 case ZPOOL_STATUS_MISSING_DEV_R
:
1618 case ZPOOL_STATUS_MISSING_DEV_NR
:
1619 case ZPOOL_STATUS_BAD_GUID_SUM
:
1620 (void) printf(gettext(" action: The pool cannot be "
1621 "imported. Attach the missing\n\tdevices and try "
1625 (void) printf(gettext(" action: The pool cannot be "
1626 "imported due to damaged devices or data.\n"));
1630 /* Print the comment attached to the pool. */
1631 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1632 (void) printf(gettext("comment: %s\n"), comment
);
1635 * If the state is "closed" or "can't open", and the aux state
1636 * is "corrupt data":
1638 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1639 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1640 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1641 if (pool_state
== POOL_STATE_DESTROYED
)
1642 (void) printf(gettext("\tThe pool was destroyed, "
1643 "but can be imported using the '-Df' flags.\n"));
1644 else if (pool_state
!= POOL_STATE_EXPORTED
)
1645 (void) printf(gettext("\tThe pool may be active on "
1646 "another system, but can be imported using\n\t"
1647 "the '-f' flag.\n"));
1651 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1654 (void) printf(gettext(" config:\n\n"));
1656 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1660 print_import_config(name
, nvroot
, namewidth
, 0);
1661 if (num_logs(nvroot
) > 0)
1662 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1664 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1665 (void) printf(gettext("\n\tAdditional devices are known to "
1666 "be part of this pool, though their\n\texact "
1667 "configuration cannot be determined.\n"));
1672 * Perform the import for the given configuration. This passes the heavy
1673 * lifting off to zpool_import_props(), and then mounts the datasets contained
1677 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1678 nvlist_t
*props
, int flags
)
1680 zpool_handle_t
*zhp
;
1685 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1688 verify(nvlist_lookup_uint64(config
,
1689 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1690 verify(nvlist_lookup_uint64(config
,
1691 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1692 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1693 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1694 "is formatted using an unsupported ZFS version\n"), name
);
1696 } else if (state
!= POOL_STATE_EXPORTED
&&
1697 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1700 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1702 if ((unsigned long)hostid
!= gethostid()) {
1707 verify(nvlist_lookup_string(config
,
1708 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1709 verify(nvlist_lookup_uint64(config
,
1710 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
1712 (void) fprintf(stderr
, gettext("cannot import "
1713 "'%s': pool may be in use from other "
1714 "system, it was last accessed by %s "
1715 "(hostid: 0x%lx) on %s"), name
, hostname
,
1716 (unsigned long)hostid
,
1717 asctime(localtime(&t
)));
1718 (void) fprintf(stderr
, gettext("use '-f' to "
1719 "import anyway\n"));
1723 (void) fprintf(stderr
, gettext("cannot import '%s': "
1724 "pool may be in use from other system\n"), name
);
1725 (void) fprintf(stderr
, gettext("use '-f' to import "
1731 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
1734 if (newname
!= NULL
)
1735 name
= (char *)newname
;
1737 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
1740 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
1741 !(flags
& ZFS_IMPORT_ONLY
) &&
1742 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
1752 * zpool import [-d dir] [-D]
1753 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1754 * [-d dir | -c cachefile] [-f] -a
1755 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1756 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1758 * -c Read pool information from a cachefile instead of searching
1761 * -d Scan in a specific directory, other than /dev/dsk. More than
1762 * one directory can be specified using multiple '-d' options.
1764 * -D Scan for previously destroyed pools or import all or only
1765 * specified destroyed pools.
1767 * -R Temporarily import the pool, with all mountpoints relative to
1768 * the given root. The pool will remain exported when the machine
1771 * -V Import even in the presence of faulted vdevs. This is an
1772 * intentionally undocumented option for testing purposes, and
1773 * treats the pool configuration as complete, leaving any bad
1774 * vdevs in the FAULTED state. In other words, it does verbatim
1777 * -f Force import, even if it appears that the pool is active.
1779 * -F Attempt rewind if necessary.
1781 * -n See if rewind would work, but don't actually rewind.
1783 * -N Import the pool but don't mount datasets.
1785 * -T Specify a starting txg to use for import. This option is
1786 * intentionally undocumented option for testing purposes.
1788 * -a Import all pools found.
1790 * -o Set property=value and/or temporary mount options (without '=').
1792 * The import command scans for pools to import, and import pools based on pool
1793 * name and GUID. The pool can also be renamed as part of the import process.
1796 zpool_do_import(int argc
, char **argv
)
1798 char **searchdirs
= NULL
;
1802 nvlist_t
*pools
= NULL
;
1803 boolean_t do_all
= B_FALSE
;
1804 boolean_t do_destroyed
= B_FALSE
;
1805 char *mntopts
= NULL
;
1808 uint64_t searchguid
= 0;
1809 char *searchname
= NULL
;
1811 nvlist_t
*found_config
;
1812 nvlist_t
*policy
= NULL
;
1813 nvlist_t
*props
= NULL
;
1815 int flags
= ZFS_IMPORT_NORMAL
;
1816 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
1817 boolean_t dryrun
= B_FALSE
;
1818 boolean_t do_rewind
= B_FALSE
;
1819 boolean_t xtreme_rewind
= B_FALSE
;
1820 uint64_t pool_state
, txg
= -1ULL;
1821 char *cachefile
= NULL
;
1822 importargs_t idata
= { 0 };
1826 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1835 if (searchdirs
== NULL
) {
1836 searchdirs
= safe_malloc(sizeof (char *));
1838 char **tmp
= safe_malloc((nsearch
+ 1) *
1840 bcopy(searchdirs
, tmp
, nsearch
*
1845 searchdirs
[nsearch
++] = optarg
;
1848 do_destroyed
= B_TRUE
;
1851 flags
|= ZFS_IMPORT_ANY_HOST
;
1857 flags
|= ZFS_IMPORT_MISSING_LOG
;
1863 flags
|= ZFS_IMPORT_ONLY
;
1866 if ((propval
= strchr(optarg
, '=')) != NULL
) {
1869 if (add_prop_list(optarg
, propval
,
1877 if (add_prop_list(zpool_prop_to_name(
1878 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1880 if (nvlist_lookup_string(props
,
1881 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
1884 if (add_prop_list(zpool_prop_to_name(
1885 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1890 txg
= strtoull(optarg
, &endptr
, 0);
1891 if (errno
!= 0 || *endptr
!= '\0') {
1892 (void) fprintf(stderr
,
1893 gettext("invalid txg value\n"));
1896 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
1899 flags
|= ZFS_IMPORT_VERBATIM
;
1902 xtreme_rewind
= B_TRUE
;
1905 (void) fprintf(stderr
, gettext("missing argument for "
1906 "'%c' option\n"), optopt
);
1910 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1919 if (cachefile
&& nsearch
!= 0) {
1920 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
1924 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
1925 (void) fprintf(stderr
,
1926 gettext("-n or -X only meaningful with -F\n"));
1930 rewind_policy
= ZPOOL_TRY_REWIND
;
1932 rewind_policy
= ZPOOL_DO_REWIND
;
1934 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
1936 /* In the future, we can capture further policy and include it here */
1937 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
1938 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
1939 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
1942 if (searchdirs
== NULL
) {
1943 searchdirs
= safe_malloc(sizeof (char *));
1944 searchdirs
[0] = "/dev/dsk";
1948 /* check argument count */
1951 (void) fprintf(stderr
, gettext("too many arguments\n"));
1956 (void) fprintf(stderr
, gettext("too many arguments\n"));
1961 * Check for the SYS_CONFIG privilege. We do this explicitly
1962 * here because otherwise any attempt to discover pools will
1965 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
1966 (void) fprintf(stderr
, gettext("cannot "
1967 "discover pools: permission denied\n"));
1969 nvlist_free(policy
);
1975 * Depending on the arguments given, we do one of the following:
1977 * <none> Iterate through all pools and display information about
1980 * -a Iterate through all pools and try to import each one.
1982 * <id> Find the pool that corresponds to the given GUID/pool
1983 * name and import that one.
1985 * -D Above options applies only to destroyed pools.
1991 searchguid
= strtoull(argv
[0], &endptr
, 10);
1992 if (errno
!= 0 || *endptr
!= '\0') {
1993 searchname
= argv
[0];
1996 found_config
= NULL
;
1999 * User specified a name or guid. Ensure it's unique.
2001 idata
.unique
= B_TRUE
;
2005 idata
.path
= searchdirs
;
2006 idata
.paths
= nsearch
;
2007 idata
.poolname
= searchname
;
2008 idata
.guid
= searchguid
;
2009 idata
.cachefile
= cachefile
;
2011 pools
= zpool_search_import(g_zfs
, &idata
);
2013 if (pools
!= NULL
&& idata
.exists
&&
2014 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2015 (void) fprintf(stderr
, gettext("cannot import '%s': "
2016 "a pool with that name already exists\n"),
2018 (void) fprintf(stderr
, gettext("use the form '%s "
2019 "<pool | id> <newpool>' to give it a new name\n"),
2022 } else if (pools
== NULL
&& idata
.exists
) {
2023 (void) fprintf(stderr
, gettext("cannot import '%s': "
2024 "a pool with that name is already created/imported,\n"),
2026 (void) fprintf(stderr
, gettext("and no additional pools "
2027 "with that name were found\n"));
2029 } else if (pools
== NULL
) {
2031 (void) fprintf(stderr
, gettext("cannot import '%s': "
2032 "no such pool available\n"), argv
[0]);
2039 nvlist_free(policy
);
2044 * At this point we have a list of import candidate configs. Even if
2045 * we were searching by pool name or guid, we still need to
2046 * post-process the list to deal with pool state and possible
2052 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2054 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2056 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2058 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2060 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2063 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2070 (void) printf("\n");
2073 err
|= do_import(config
, NULL
, mntopts
,
2076 show_import(config
);
2078 } else if (searchname
!= NULL
) {
2082 * We are searching for a pool based on name.
2084 verify(nvlist_lookup_string(config
,
2085 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2087 if (strcmp(name
, searchname
) == 0) {
2088 if (found_config
!= NULL
) {
2089 (void) fprintf(stderr
, gettext(
2090 "cannot import '%s': more than "
2091 "one matching pool\n"), searchname
);
2092 (void) fprintf(stderr
, gettext(
2093 "import by numeric ID instead\n"));
2096 found_config
= config
;
2102 * Search for a pool by guid.
2104 verify(nvlist_lookup_uint64(config
,
2105 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2107 if (guid
== searchguid
)
2108 found_config
= config
;
2113 * If we were searching for a specific pool, verify that we found a
2114 * pool, and then do the import.
2116 if (argc
!= 0 && err
== 0) {
2117 if (found_config
== NULL
) {
2118 (void) fprintf(stderr
, gettext("cannot import '%s': "
2119 "no such pool available\n"), argv
[0]);
2122 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2123 argv
[1], mntopts
, props
, flags
);
2128 * If we were just looking for pools, report an error if none were
2131 if (argc
== 0 && first
)
2132 (void) fprintf(stderr
,
2133 gettext("no pools available to import\n"));
2138 nvlist_free(policy
);
2141 return (err
? 1 : 0);
2144 typedef struct iostat_cbdata
{
2145 boolean_t cb_verbose
;
2148 zpool_list_t
*cb_list
;
2152 print_iostat_separator(iostat_cbdata_t
*cb
)
2156 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2158 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2162 print_iostat_header(iostat_cbdata_t
*cb
)
2164 (void) printf("%*s capacity operations bandwidth\n",
2165 cb
->cb_namewidth
, "");
2166 (void) printf("%-*s alloc free read write read write\n",
2167 cb
->cb_namewidth
, "pool");
2168 print_iostat_separator(cb
);
2172 * Display a single statistic.
2175 print_one_stat(uint64_t value
)
2179 zfs_nicenum(value
, buf
, sizeof (buf
));
2180 (void) printf(" %5s", buf
);
2184 * Print out all the statistics for the given vdev. This can either be the
2185 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2186 * is a verbose output, and we don't want to display the toplevel pool stats.
2189 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2190 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2192 nvlist_t
**oldchild
, **newchild
;
2194 vdev_stat_t
*oldvs
, *newvs
;
2195 vdev_stat_t zerovs
= { 0 };
2200 if (oldnv
!= NULL
) {
2201 verify(nvlist_lookup_uint64_array(oldnv
,
2202 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2207 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2208 (uint64_t **)&newvs
, &c
) == 0);
2210 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2211 (void) printf("%*s%s", depth
, "", name
);
2213 (void) printf("%*s%s%*s", depth
, "", name
,
2214 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2216 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2221 scale
= (double)NANOSEC
/ tdelta
;
2223 /* only toplevel vdevs have capacity stats */
2224 if (newvs
->vs_space
== 0) {
2225 (void) printf(" - -");
2227 print_one_stat(newvs
->vs_alloc
);
2228 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2231 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2232 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2234 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2235 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2237 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2238 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2240 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2241 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2243 (void) printf("\n");
2245 if (!cb
->cb_verbose
)
2248 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2249 &newchild
, &children
) != 0)
2252 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2253 &oldchild
, &c
) != 0)
2256 for (c
= 0; c
< children
; c
++) {
2257 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2259 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2262 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2265 if (ishole
|| islog
)
2268 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2269 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2270 newchild
[c
], cb
, depth
+ 2);
2275 * Log device section
2278 if (num_logs(newnv
) > 0) {
2279 (void) printf("%-*s - - - - - "
2280 "-\n", cb
->cb_namewidth
, "logs");
2282 for (c
= 0; c
< children
; c
++) {
2283 uint64_t islog
= B_FALSE
;
2284 (void) nvlist_lookup_uint64(newchild
[c
],
2285 ZPOOL_CONFIG_IS_LOG
, &islog
);
2288 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2290 print_vdev_stats(zhp
, vname
, oldnv
?
2291 oldchild
[c
] : NULL
, newchild
[c
],
2300 * Include level 2 ARC devices in iostat output
2302 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2303 &newchild
, &children
) != 0)
2306 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2307 &oldchild
, &c
) != 0)
2311 (void) printf("%-*s - - - - - "
2312 "-\n", cb
->cb_namewidth
, "cache");
2313 for (c
= 0; c
< children
; c
++) {
2314 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2316 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2317 newchild
[c
], cb
, depth
+ 2);
2324 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2326 iostat_cbdata_t
*cb
= data
;
2330 * If the pool has disappeared, remove it from the list and continue.
2332 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2336 pool_list_remove(cb
->cb_list
, zhp
);
2342 * Callback to print out the iostats for the given pool.
2345 print_iostat(zpool_handle_t
*zhp
, void *data
)
2347 iostat_cbdata_t
*cb
= data
;
2348 nvlist_t
*oldconfig
, *newconfig
;
2349 nvlist_t
*oldnvroot
, *newnvroot
;
2351 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2353 if (cb
->cb_iteration
== 1)
2356 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2359 if (oldconfig
== NULL
)
2362 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2366 * Print out the statistics for the pool.
2368 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2371 print_iostat_separator(cb
);
2377 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2379 iostat_cbdata_t
*cb
= data
;
2380 nvlist_t
*config
, *nvroot
;
2382 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2383 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2385 if (!cb
->cb_verbose
)
2386 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2388 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2393 * The width must fall into the range [10,38]. The upper limit is the
2394 * maximum we can have and still fit in 80 columns.
2396 if (cb
->cb_namewidth
< 10)
2397 cb
->cb_namewidth
= 10;
2398 if (cb
->cb_namewidth
> 38)
2399 cb
->cb_namewidth
= 38;
2405 * Parse the input string, get the 'interval' and 'count' value if there is one.
2408 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2411 unsigned long interval
= 0, count
= 0;
2412 int argc
= *argcp
, errno
;
2415 * Determine if the last argument is an integer or a pool name
2417 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2421 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2423 if (*end
== '\0' && errno
== 0) {
2424 if (interval
== 0) {
2425 (void) fprintf(stderr
, gettext("interval "
2426 "cannot be zero\n"));
2430 * Ignore the last parameter
2435 * If this is not a valid number, just plow on. The
2436 * user will get a more informative error message later
2444 * If the last argument is also an integer, then we have both a count
2447 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2452 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2454 if (*end
== '\0' && errno
== 0) {
2455 if (interval
== 0) {
2456 (void) fprintf(stderr
, gettext("interval "
2457 "cannot be zero\n"));
2462 * Ignore the last parameter
2476 get_timestamp_arg(char c
)
2479 timestamp_fmt
= UDATE
;
2481 timestamp_fmt
= DDATE
;
2487 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2489 * -v Display statistics for individual vdevs
2490 * -T Display a timestamp in date(1) or Unix format
2492 * This command can be tricky because we want to be able to deal with pool
2493 * creation/destruction as well as vdev configuration changes. The bulk of this
2494 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2495 * on pool_list_update() to detect the addition of new pools. Configuration
2496 * changes are all handled within libzfs.
2499 zpool_do_iostat(int argc
, char **argv
)
2504 unsigned long interval
= 0, count
= 0;
2506 boolean_t verbose
= B_FALSE
;
2510 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2513 get_timestamp_arg(*optarg
);
2519 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2528 get_interval_count(&argc
, argv
, &interval
, &count
);
2531 * Construct the list of all interesting pools.
2534 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2537 if (pool_list_count(list
) == 0 && argc
!= 0) {
2538 pool_list_free(list
);
2542 if (pool_list_count(list
) == 0 && interval
== 0) {
2543 pool_list_free(list
);
2544 (void) fprintf(stderr
, gettext("no pools available\n"));
2549 * Enter the main iostat loop.
2552 cb
.cb_verbose
= verbose
;
2553 cb
.cb_iteration
= 0;
2554 cb
.cb_namewidth
= 0;
2557 pool_list_update(list
);
2559 if ((npools
= pool_list_count(list
)) == 0)
2563 * Refresh all statistics. This is done as an explicit step
2564 * before calculating the maximum name width, so that any
2565 * configuration changes are properly accounted for.
2567 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
, &cb
);
2570 * Iterate over all pools to determine the maximum width
2571 * for the pool / device name column across all pools.
2573 cb
.cb_namewidth
= 0;
2574 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
2576 if (timestamp_fmt
!= NODATE
)
2577 print_timestamp(timestamp_fmt
);
2580 * If it's the first time, or verbose mode, print the header.
2582 if (++cb
.cb_iteration
== 1 || verbose
)
2583 print_iostat_header(&cb
);
2585 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2588 * If there's more than one pool, and we're not in verbose mode
2589 * (which prints a separator for us), then print a separator.
2591 if (npools
> 1 && !verbose
)
2592 print_iostat_separator(&cb
);
2595 (void) printf("\n");
2598 * Flush the output so that redirection to a file isn't buffered
2601 (void) fflush(stdout
);
2606 if (count
!= 0 && --count
== 0)
2609 (void) sleep(interval
);
2612 pool_list_free(list
);
2617 typedef struct list_cbdata
{
2618 boolean_t cb_verbose
;
2620 boolean_t cb_scripted
;
2621 zprop_list_t
*cb_proplist
;
2622 boolean_t cb_literal
;
2626 * Given a list of columns to display, output appropriate headers for each one.
2629 print_header(list_cbdata_t
*cb
)
2631 zprop_list_t
*pl
= cb
->cb_proplist
;
2632 char headerbuf
[ZPOOL_MAXPROPLEN
];
2634 boolean_t first
= B_TRUE
;
2635 boolean_t right_justify
;
2638 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2639 width
= pl
->pl_width
;
2640 if (first
&& cb
->cb_verbose
) {
2642 * Reset the width to accommodate the verbose listing
2645 width
= cb
->cb_namewidth
;
2653 right_justify
= B_FALSE
;
2654 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2655 header
= zpool_prop_column_name(pl
->pl_prop
);
2656 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2660 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2661 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2662 headerbuf
[i
] = '\0';
2666 if (pl
->pl_next
== NULL
&& !right_justify
)
2667 (void) printf("%s", header
);
2668 else if (right_justify
)
2669 (void) printf("%*s", width
, header
);
2671 (void) printf("%-*s", width
, header
);
2675 (void) printf("\n");
2679 * Given a pool and a list of properties, print out all the properties according
2680 * to the described layout.
2683 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
2685 zprop_list_t
*pl
= cb
->cb_proplist
;
2686 boolean_t first
= B_TRUE
;
2687 char property
[ZPOOL_MAXPROPLEN
];
2689 boolean_t right_justify
;
2692 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2694 width
= pl
->pl_width
;
2695 if (first
&& cb
->cb_verbose
) {
2697 * Reset the width to accommodate the verbose listing
2700 width
= cb
->cb_namewidth
;
2704 if (cb
->cb_scripted
)
2705 (void) printf("\t");
2712 right_justify
= B_FALSE
;
2713 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2714 if (pl
->pl_prop
== ZPOOL_PROP_EXPANDSZ
&&
2715 zpool_get_prop_int(zhp
, pl
->pl_prop
, NULL
) == 0)
2717 else if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
2718 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
2723 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2724 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
2725 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
2726 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
2727 sizeof (property
)) == 0) {
2735 * If this is being called in scripted mode, or if this is the
2736 * last column and it is left-justified, don't include a width
2739 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2740 (void) printf("%s", propstr
);
2741 else if (right_justify
)
2742 (void) printf("%*s", width
, propstr
);
2744 (void) printf("%-*s", width
, propstr
);
2747 (void) printf("\n");
2751 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
)
2755 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
2758 if (prop
== ZPOOL_PROP_EXPANDSZ
&& value
== 0)
2759 (void) strlcpy(propval
, "-", sizeof (propval
));
2760 else if (prop
== ZPOOL_PROP_FRAGMENTATION
&& value
== ZFS_FRAG_INVALID
)
2761 (void) strlcpy(propval
, "-", sizeof (propval
));
2762 else if (prop
== ZPOOL_PROP_FRAGMENTATION
)
2763 (void) snprintf(propval
, sizeof (propval
), "%llu%%", value
);
2765 zfs_nicenum(value
, propval
, sizeof (propval
));
2768 (void) printf("\t%s", propval
);
2770 (void) printf(" %*s", width
, propval
);
2774 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
2775 list_cbdata_t
*cb
, int depth
)
2781 boolean_t scripted
= cb
->cb_scripted
;
2783 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2784 (uint64_t **)&vs
, &c
) == 0);
2788 (void) printf("\t%s", name
);
2789 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
2790 (void) printf("%*s%s", depth
, "", name
);
2792 (void) printf("%*s%s%*s", depth
, "", name
,
2793 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2795 /* only toplevel vdevs have capacity stats */
2796 if (vs
->vs_space
== 0) {
2798 (void) printf("\t-\t-\t-\t-");
2800 (void) printf(" - - - -");
2802 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
,
2804 print_one_column(ZPOOL_PROP_CAPACITY
, vs
->vs_alloc
,
2806 print_one_column(ZPOOL_PROP_FREE
,
2807 vs
->vs_space
- vs
->vs_alloc
, scripted
);
2808 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
2809 vs
->vs_fragmentation
, scripted
);
2811 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
,
2813 (void) printf("\n");
2816 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2817 &child
, &children
) != 0)
2820 for (c
= 0; c
< children
; c
++) {
2821 uint64_t ishole
= B_FALSE
;
2823 if (nvlist_lookup_uint64(child
[c
],
2824 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
2827 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
2828 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2833 * Include level 2 ARC devices in iostat output
2835 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2836 &child
, &children
) != 0)
2840 (void) printf("%-*s - - - - - "
2841 "-\n", cb
->cb_namewidth
, "cache");
2842 for (c
= 0; c
< children
; c
++) {
2843 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2845 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
2853 * Generic callback function to list a pool.
2856 list_callback(zpool_handle_t
*zhp
, void *data
)
2858 list_cbdata_t
*cbp
= data
;
2862 config
= zpool_get_config(zhp
, NULL
);
2864 print_pool(zhp
, cbp
);
2865 if (!cbp
->cb_verbose
)
2868 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2870 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
2876 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2878 * -H Scripted mode. Don't display headers, and separate properties
2880 * -o List of properties to display. Defaults to
2881 * "name,size,allocated,free,capacity,health,altroot"
2882 * -p Diplay values in parsable (exact) format.
2883 * -T Display a timestamp in date(1) or Unix format
2885 * List all pools in the system, whether or not they're healthy. Output space
2886 * statistics for each one, as well as health status summary.
2889 zpool_do_list(int argc
, char **argv
)
2893 list_cbdata_t cb
= { 0 };
2894 static char default_props
[] =
2895 "name,size,allocated,free,fragmentation,expandsize,capacity,"
2896 "dedupratio,health,altroot";
2897 char *props
= default_props
;
2898 unsigned long interval
= 0, count
= 0;
2900 boolean_t first
= B_TRUE
;
2903 while ((c
= getopt(argc
, argv
, ":Ho:pT:v")) != -1) {
2906 cb
.cb_scripted
= B_TRUE
;
2912 cb
.cb_literal
= B_TRUE
;
2915 get_timestamp_arg(*optarg
);
2918 cb
.cb_verbose
= B_TRUE
;
2921 (void) fprintf(stderr
, gettext("missing argument for "
2922 "'%c' option\n"), optopt
);
2926 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2935 get_interval_count(&argc
, argv
, &interval
, &count
);
2937 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
2941 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
2945 if (pool_list_count(list
) == 0)
2948 cb
.cb_namewidth
= 0;
2949 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
2951 if (timestamp_fmt
!= NODATE
)
2952 print_timestamp(timestamp_fmt
);
2954 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
2958 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
2963 if (count
!= 0 && --count
== 0)
2966 pool_list_free(list
);
2967 (void) sleep(interval
);
2970 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
2971 (void) printf(gettext("no pools available\n"));
2975 pool_list_free(list
);
2976 zprop_free_list(cb
.cb_proplist
);
2981 zpool_get_vdev_by_name(nvlist_t
*nv
, char *name
)
2988 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2989 &child
, &children
) != 0) {
2990 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2991 if (strncmp(name
, "/dev/dsk/", 9) == 0)
2993 if (strncmp(path
, "/dev/dsk/", 9) == 0)
2995 if (strcmp(name
, path
) == 0)
3000 for (c
= 0; c
< children
; c
++)
3001 if ((match
= zpool_get_vdev_by_name(child
[c
], name
)) != NULL
)
3008 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3010 boolean_t force
= B_FALSE
;
3013 char *poolname
, *old_disk
, *new_disk
;
3014 zpool_handle_t
*zhp
;
3018 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3024 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3033 /* get pool name and check number of arguments */
3035 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3042 (void) fprintf(stderr
,
3043 gettext("missing <device> specification\n"));
3051 (void) fprintf(stderr
,
3052 gettext("missing <new_device> specification\n"));
3055 new_disk
= old_disk
;
3065 (void) fprintf(stderr
, gettext("too many arguments\n"));
3069 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3072 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3073 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3079 nvroot
= make_root_vdev(zhp
, force
, B_FALSE
, replacing
, B_FALSE
,
3081 if (nvroot
== NULL
) {
3086 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3088 nvlist_free(nvroot
);
3095 * zpool replace [-f] <pool> <device> <new_device>
3097 * -f Force attach, even if <new_device> appears to be in use.
3099 * Replace <device> with <new_device>.
3103 zpool_do_replace(int argc
, char **argv
)
3105 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3109 * zpool attach [-f] <pool> <device> <new_device>
3111 * -f Force attach, even if <new_device> appears to be in use.
3113 * Attach <new_device> to the mirror containing <device>. If <device> is not
3114 * part of a mirror, then <device> will be transformed into a mirror of
3115 * <device> and <new_device>. In either case, <new_device> will begin life
3116 * with a DTL of [0, now], and will immediately begin to resilver itself.
3119 zpool_do_attach(int argc
, char **argv
)
3121 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3125 * zpool detach [-f] <pool> <device>
3127 * -f Force detach of <device>, even if DTLs argue against it
3128 * (not supported yet)
3130 * Detach a device from a mirror. The operation will be refused if <device>
3131 * is the last device in the mirror, or if the DTLs indicate that this device
3132 * has the only valid copy of some data.
3136 zpool_do_detach(int argc
, char **argv
)
3139 char *poolname
, *path
;
3140 zpool_handle_t
*zhp
;
3144 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3148 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3157 /* get pool name and check number of arguments */
3159 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3164 (void) fprintf(stderr
,
3165 gettext("missing <device> specification\n"));
3172 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3175 ret
= zpool_vdev_detach(zhp
, path
);
3183 * zpool split [-n] [-o prop=val] ...
3185 * [-R altroot] <pool> <newpool> [<device> ...]
3187 * -n Do not split the pool, but display the resulting layout if
3188 * it were to be split.
3189 * -o Set property=value, or set mount options.
3190 * -R Mount the split-off pool under an alternate root.
3192 * Splits the named pool and gives it the new pool name. Devices to be split
3193 * off may be listed, provided that no more than one device is specified
3194 * per top-level vdev mirror. The newly split pool is left in an exported
3195 * state unless -R is specified.
3197 * Restrictions: the top-level of the pool pool must only be made up of
3198 * mirrors; all devices in the pool must be healthy; no device may be
3199 * undergoing a resilvering operation.
3202 zpool_do_split(int argc
, char **argv
)
3204 char *srcpool
, *newpool
, *propval
;
3205 char *mntopts
= NULL
;
3208 zpool_handle_t
*zhp
;
3209 nvlist_t
*config
, *props
= NULL
;
3211 flags
.dryrun
= B_FALSE
;
3212 flags
.import
= B_FALSE
;
3215 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3218 flags
.import
= B_TRUE
;
3220 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3221 &props
, B_TRUE
) != 0) {
3228 flags
.dryrun
= B_TRUE
;
3231 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3234 if (add_prop_list(optarg
, propval
,
3235 &props
, B_TRUE
) != 0) {
3245 (void) fprintf(stderr
, gettext("missing argument for "
3246 "'%c' option\n"), optopt
);
3250 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3257 if (!flags
.import
&& mntopts
!= NULL
) {
3258 (void) fprintf(stderr
, gettext("setting mntopts is only "
3259 "valid when importing the pool\n"));
3267 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3271 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3281 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3284 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3285 if (config
== NULL
) {
3289 (void) printf(gettext("would create '%s' with the "
3290 "following layout:\n\n"), newpool
);
3291 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3293 nvlist_free(config
);
3298 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3302 * The split was successful. Now we need to open the new
3303 * pool and import it.
3305 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3307 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3308 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3310 (void) fprintf(stderr
, gettext("Split was successful, but "
3311 "the datasets could not all be mounted\n"));
3312 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3313 "different altroot\n"), "zpool import");
3323 * zpool online <pool> <device> ...
3326 zpool_do_online(int argc
, char **argv
)
3330 zpool_handle_t
*zhp
;
3332 vdev_state_t newstate
;
3336 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3339 flags
|= ZFS_ONLINE_EXPAND
;
3343 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3352 /* get pool name and check number of arguments */
3354 (void) fprintf(stderr
, gettext("missing pool name\n"));
3358 (void) fprintf(stderr
, gettext("missing device name\n"));
3364 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3367 for (i
= 1; i
< argc
; i
++) {
3368 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3369 if (newstate
!= VDEV_STATE_HEALTHY
) {
3370 (void) printf(gettext("warning: device '%s' "
3371 "onlined, but remains in faulted state\n"),
3373 if (newstate
== VDEV_STATE_FAULTED
)
3374 (void) printf(gettext("use 'zpool "
3375 "clear' to restore a faulted "
3378 (void) printf(gettext("use 'zpool "
3379 "replace' to replace devices "
3380 "that are no longer present\n"));
3393 * zpool offline [-ft] <pool> <device> ...
3395 * -f Force the device into the offline state, even if doing
3396 * so would appear to compromise pool availability.
3397 * (not supported yet)
3399 * -t Only take the device off-line temporarily. The offline
3400 * state will not be persistent across reboots.
3404 zpool_do_offline(int argc
, char **argv
)
3408 zpool_handle_t
*zhp
;
3410 boolean_t istmp
= B_FALSE
;
3413 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3420 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3429 /* get pool name and check number of arguments */
3431 (void) fprintf(stderr
, gettext("missing pool name\n"));
3435 (void) fprintf(stderr
, gettext("missing device name\n"));
3441 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3444 for (i
= 1; i
< argc
; i
++) {
3445 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3455 * zpool clear <pool> [device]
3457 * Clear all errors associated with a pool or a particular device.
3460 zpool_do_clear(int argc
, char **argv
)
3464 boolean_t dryrun
= B_FALSE
;
3465 boolean_t do_rewind
= B_FALSE
;
3466 boolean_t xtreme_rewind
= B_FALSE
;
3467 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3468 nvlist_t
*policy
= NULL
;
3469 zpool_handle_t
*zhp
;
3470 char *pool
, *device
;
3473 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3482 xtreme_rewind
= B_TRUE
;
3485 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3495 (void) fprintf(stderr
, gettext("missing pool name\n"));
3500 (void) fprintf(stderr
, gettext("too many arguments\n"));
3504 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3505 (void) fprintf(stderr
,
3506 gettext("-n or -X only meaningful with -F\n"));
3510 rewind_policy
= ZPOOL_TRY_REWIND
;
3512 rewind_policy
= ZPOOL_DO_REWIND
;
3514 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3516 /* In future, further rewind policy choices can be passed along here */
3517 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3518 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3522 device
= argc
== 2 ? argv
[1] : NULL
;
3524 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3525 nvlist_free(policy
);
3529 if (zpool_clear(zhp
, device
, policy
) != 0)
3534 nvlist_free(policy
);
3540 * zpool reguid <pool>
3543 zpool_do_reguid(int argc
, char **argv
)
3547 zpool_handle_t
*zhp
;
3551 while ((c
= getopt(argc
, argv
, "")) != -1) {
3554 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3563 /* get pool name and check number of arguments */
3565 (void) fprintf(stderr
, gettext("missing pool name\n"));
3570 (void) fprintf(stderr
, gettext("too many arguments\n"));
3575 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3578 ret
= zpool_reguid(zhp
);
3586 * zpool reopen <pool>
3588 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3591 zpool_do_reopen(int argc
, char **argv
)
3595 zpool_handle_t
*zhp
;
3599 while ((c
= getopt(argc
, argv
, "")) != -1) {
3602 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3612 (void) fprintf(stderr
, gettext("missing pool name\n"));
3617 (void) fprintf(stderr
, gettext("too many arguments\n"));
3622 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3625 ret
= zpool_reopen(zhp
);
3630 typedef struct scrub_cbdata
{
3637 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3639 scrub_cbdata_t
*cb
= data
;
3643 * Ignore faulted pools.
3645 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3646 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3647 "currently unavailable\n"), zpool_get_name(zhp
));
3651 err
= zpool_scan(zhp
, cb
->cb_type
);
3657 * zpool scrub [-s] <pool> ...
3659 * -s Stop. Stops any in-progress scrub.
3662 zpool_do_scrub(int argc
, char **argv
)
3667 cb
.cb_type
= POOL_SCAN_SCRUB
;
3670 while ((c
= getopt(argc
, argv
, "s")) != -1) {
3673 cb
.cb_type
= POOL_SCAN_NONE
;
3676 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3688 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3692 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
3695 typedef struct status_cbdata
{
3697 boolean_t cb_allpools
;
3698 boolean_t cb_verbose
;
3699 boolean_t cb_explain
;
3701 boolean_t cb_dedup_stats
;
3705 * Print out detailed scrub status.
3708 print_scan_status(pool_scan_stat_t
*ps
)
3711 uint64_t elapsed
, mins_left
, hours_left
;
3712 uint64_t pass_exam
, examined
, total
;
3714 double fraction_done
;
3715 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
3717 (void) printf(gettext(" scan: "));
3719 /* If there's never been a scan, there's not much to say. */
3720 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
3721 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
3722 (void) printf(gettext("none requested\n"));
3726 start
= ps
->pss_start_time
;
3727 end
= ps
->pss_end_time
;
3728 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
3730 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
3731 ps
->pss_func
== POOL_SCAN_RESILVER
);
3733 * Scan is finished or canceled.
3735 if (ps
->pss_state
== DSS_FINISHED
) {
3736 uint64_t minutes_taken
= (end
- start
) / 60;
3739 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3740 fmt
= gettext("scrub repaired %s in %lluh%um with "
3741 "%llu errors on %s");
3742 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3743 fmt
= gettext("resilvered %s in %lluh%um with "
3744 "%llu errors on %s");
3747 (void) printf(fmt
, processed_buf
,
3748 (u_longlong_t
)(minutes_taken
/ 60),
3749 (uint_t
)(minutes_taken
% 60),
3750 (u_longlong_t
)ps
->pss_errors
,
3751 ctime((time_t *)&end
));
3753 } else if (ps
->pss_state
== DSS_CANCELED
) {
3754 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3755 (void) printf(gettext("scrub canceled on %s"),
3757 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3758 (void) printf(gettext("resilver canceled on %s"),
3764 assert(ps
->pss_state
== DSS_SCANNING
);
3767 * Scan is in progress.
3769 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3770 (void) printf(gettext("scrub in progress since %s"),
3772 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3773 (void) printf(gettext("resilver in progress since %s"),
3777 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
3778 total
= ps
->pss_to_examine
;
3779 fraction_done
= (double)examined
/ total
;
3781 /* elapsed time for this pass */
3782 elapsed
= time(NULL
) - ps
->pss_pass_start
;
3783 elapsed
= elapsed
? elapsed
: 1;
3784 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
3785 rate
= pass_exam
/ elapsed
;
3786 rate
= rate
? rate
: 1;
3787 mins_left
= ((total
- examined
) / rate
) / 60;
3788 hours_left
= mins_left
/ 60;
3790 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
3791 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
3792 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
3795 * do not print estimated time if hours_left is more than 30 days
3797 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3798 examined_buf
, total_buf
, rate_buf
);
3799 if (hours_left
< (30 * 24)) {
3800 (void) printf(gettext(", %lluh%um to go\n"),
3801 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
3803 (void) printf(gettext(
3804 ", (scan is slow, no estimated time)\n"));
3807 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3808 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3809 processed_buf
, 100 * fraction_done
);
3810 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3811 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3812 processed_buf
, 100 * fraction_done
);
3817 print_error_log(zpool_handle_t
*zhp
)
3819 nvlist_t
*nverrlist
= NULL
;
3822 size_t len
= MAXPATHLEN
* 2;
3824 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
3825 (void) printf("errors: List of errors unavailable "
3826 "(insufficient privileges)\n");
3830 (void) printf("errors: Permanent errors have been "
3831 "detected in the following files:\n\n");
3833 pathname
= safe_malloc(len
);
3835 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
3837 uint64_t dsobj
, obj
;
3839 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
3840 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
3842 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
3844 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
3845 (void) printf("%7s %s\n", "", pathname
);
3848 nvlist_free(nverrlist
);
3852 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
3861 (void) printf(gettext("\tspares\n"));
3863 for (i
= 0; i
< nspares
; i
++) {
3864 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
3865 print_status_config(zhp
, name
, spares
[i
],
3866 namewidth
, 2, B_TRUE
);
3872 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
3881 (void) printf(gettext("\tcache\n"));
3883 for (i
= 0; i
< nl2cache
; i
++) {
3884 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
3885 print_status_config(zhp
, name
, l2cache
[i
],
3886 namewidth
, 2, B_FALSE
);
3892 print_dedup_stats(nvlist_t
*config
)
3894 ddt_histogram_t
*ddh
;
3900 * If the pool was faulted then we may not have been able to
3901 * obtain the config. Otherwise, if we have anything in the dedup
3902 * table continue processing the stats.
3904 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
3905 (uint64_t **)&ddo
, &c
) != 0)
3908 (void) printf("\n");
3909 (void) printf(gettext(" dedup: "));
3910 if (ddo
->ddo_count
== 0) {
3911 (void) printf(gettext("no DDT entries\n"));
3915 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3916 (u_longlong_t
)ddo
->ddo_count
,
3917 (u_longlong_t
)ddo
->ddo_dspace
,
3918 (u_longlong_t
)ddo
->ddo_mspace
);
3920 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
3921 (uint64_t **)&dds
, &c
) == 0);
3922 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
3923 (uint64_t **)&ddh
, &c
) == 0);
3924 zpool_dump_ddt(dds
, ddh
);
3928 * Display a summary of pool status. Displays a summary such as:
3932 * reason: One or more devices ...
3933 * see: http://illumos.org/msg/ZFS-xxxx-01
3939 * When given the '-v' option, we print out the complete config. If the '-e'
3940 * option is specified, then we print out error rate information as well.
3943 status_callback(zpool_handle_t
*zhp
, void *data
)
3945 status_cbdata_t
*cbp
= data
;
3946 nvlist_t
*config
, *nvroot
;
3953 config
= zpool_get_config(zhp
, NULL
);
3954 reason
= zpool_get_status(zhp
, &msgid
);
3959 * If we were given 'zpool status -x', only report those pools with
3962 if (cbp
->cb_explain
&&
3963 (reason
== ZPOOL_STATUS_OK
||
3964 reason
== ZPOOL_STATUS_VERSION_OLDER
||
3965 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
3966 if (!cbp
->cb_allpools
) {
3967 (void) printf(gettext("pool '%s' is healthy\n"),
3968 zpool_get_name(zhp
));
3970 cbp
->cb_first
= B_FALSE
;
3976 cbp
->cb_first
= B_FALSE
;
3978 (void) printf("\n");
3980 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3982 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
3983 (uint64_t **)&vs
, &c
) == 0);
3984 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
3986 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
3987 (void) printf(gettext(" state: %s\n"), health
);
3990 case ZPOOL_STATUS_MISSING_DEV_R
:
3991 (void) printf(gettext("status: One or more devices could not "
3992 "be opened. Sufficient replicas exist for\n\tthe pool to "
3993 "continue functioning in a degraded state.\n"));
3994 (void) printf(gettext("action: Attach the missing device and "
3995 "online it using 'zpool online'.\n"));
3998 case ZPOOL_STATUS_MISSING_DEV_NR
:
3999 (void) printf(gettext("status: One or more devices could not "
4000 "be opened. There are insufficient\n\treplicas for the "
4001 "pool to continue functioning.\n"));
4002 (void) printf(gettext("action: Attach the missing device and "
4003 "online it using 'zpool online'.\n"));
4006 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4007 (void) printf(gettext("status: One or more devices could not "
4008 "be used because the label is missing or\n\tinvalid. "
4009 "Sufficient replicas exist for the pool to continue\n\t"
4010 "functioning in a degraded state.\n"));
4011 (void) printf(gettext("action: Replace the device using "
4012 "'zpool replace'.\n"));
4015 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4016 (void) printf(gettext("status: One or more devices could not "
4017 "be used because the label is missing \n\tor invalid. "
4018 "There are insufficient replicas for the pool to "
4019 "continue\n\tfunctioning.\n"));
4020 zpool_explain_recover(zpool_get_handle(zhp
),
4021 zpool_get_name(zhp
), reason
, config
);
4024 case ZPOOL_STATUS_FAILING_DEV
:
4025 (void) printf(gettext("status: One or more devices has "
4026 "experienced an unrecoverable error. An\n\tattempt was "
4027 "made to correct the error. Applications are "
4029 (void) printf(gettext("action: Determine if the device needs "
4030 "to be replaced, and clear the errors\n\tusing "
4031 "'zpool clear' or replace the device with 'zpool "
4035 case ZPOOL_STATUS_OFFLINE_DEV
:
4036 (void) printf(gettext("status: One or more devices has "
4037 "been taken offline by the administrator.\n\tSufficient "
4038 "replicas exist for the pool to continue functioning in "
4039 "a\n\tdegraded state.\n"));
4040 (void) printf(gettext("action: Online the device using "
4041 "'zpool online' or replace the device with\n\t'zpool "
4045 case ZPOOL_STATUS_REMOVED_DEV
:
4046 (void) printf(gettext("status: One or more devices has "
4047 "been removed by the administrator.\n\tSufficient "
4048 "replicas exist for the pool to continue functioning in "
4049 "a\n\tdegraded state.\n"));
4050 (void) printf(gettext("action: Online the device using "
4051 "'zpool online' or replace the device with\n\t'zpool "
4055 case ZPOOL_STATUS_RESILVERING
:
4056 (void) printf(gettext("status: One or more devices is "
4057 "currently being resilvered. The pool will\n\tcontinue "
4058 "to function, possibly in a degraded state.\n"));
4059 (void) printf(gettext("action: Wait for the resilver to "
4063 case ZPOOL_STATUS_CORRUPT_DATA
:
4064 (void) printf(gettext("status: One or more devices has "
4065 "experienced an error resulting in data\n\tcorruption. "
4066 "Applications may be affected.\n"));
4067 (void) printf(gettext("action: Restore the file in question "
4068 "if possible. Otherwise restore the\n\tentire pool from "
4072 case ZPOOL_STATUS_CORRUPT_POOL
:
4073 (void) printf(gettext("status: The pool metadata is corrupted "
4074 "and the pool cannot be opened.\n"));
4075 zpool_explain_recover(zpool_get_handle(zhp
),
4076 zpool_get_name(zhp
), reason
, config
);
4079 case ZPOOL_STATUS_VERSION_OLDER
:
4080 (void) printf(gettext("status: The pool is formatted using a "
4081 "legacy on-disk format. The pool can\n\tstill be used, "
4082 "but some features are unavailable.\n"));
4083 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4084 "upgrade'. Once this is done, the\n\tpool will no longer "
4085 "be accessible on software that does not support feature\n"
4089 case ZPOOL_STATUS_VERSION_NEWER
:
4090 (void) printf(gettext("status: The pool has been upgraded to a "
4091 "newer, incompatible on-disk version.\n\tThe pool cannot "
4092 "be accessed on this system.\n"));
4093 (void) printf(gettext("action: Access the pool from a system "
4094 "running more recent software, or\n\trestore the pool from "
4098 case ZPOOL_STATUS_FEAT_DISABLED
:
4099 (void) printf(gettext("status: Some supported features are not "
4100 "enabled on the pool. The pool can\n\tstill be used, but "
4101 "some features are unavailable.\n"));
4102 (void) printf(gettext("action: Enable all features using "
4103 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4104 "longer be accessible by software that does not support\n\t"
4105 "the features. See zpool-features(5) for details.\n"));
4108 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4109 (void) printf(gettext("status: The pool cannot be accessed on "
4110 "this system because it uses the\n\tfollowing feature(s) "
4111 "not supported on this system:\n"));
4112 zpool_print_unsup_feat(config
);
4113 (void) printf("\n");
4114 (void) printf(gettext("action: Access the pool from a system "
4115 "that supports the required feature(s),\n\tor restore the "
4116 "pool from backup.\n"));
4119 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4120 (void) printf(gettext("status: The pool can only be accessed "
4121 "in read-only mode on this system. It\n\tcannot be "
4122 "accessed in read-write mode because it uses the "
4123 "following\n\tfeature(s) not supported on this system:\n"));
4124 zpool_print_unsup_feat(config
);
4125 (void) printf("\n");
4126 (void) printf(gettext("action: The pool cannot be accessed in "
4127 "read-write mode. Import the pool with\n"
4128 "\t\"-o readonly=on\", access the pool from a system that "
4129 "supports the\n\trequired feature(s), or restore the "
4130 "pool from backup.\n"));
4133 case ZPOOL_STATUS_FAULTED_DEV_R
:
4134 (void) printf(gettext("status: One or more devices are "
4135 "faulted in response to persistent errors.\n\tSufficient "
4136 "replicas exist for the pool to continue functioning "
4137 "in a\n\tdegraded state.\n"));
4138 (void) printf(gettext("action: Replace the faulted device, "
4139 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4142 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4143 (void) printf(gettext("status: One or more devices are "
4144 "faulted in response to persistent errors. There are "
4145 "insufficient replicas for the pool to\n\tcontinue "
4147 (void) printf(gettext("action: Destroy and re-create the pool "
4148 "from a backup source. Manually marking the device\n"
4149 "\trepaired using 'zpool clear' may allow some data "
4150 "to be recovered.\n"));
4153 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4154 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4155 (void) printf(gettext("status: One or more devices are "
4156 "faulted in response to IO failures.\n"));
4157 (void) printf(gettext("action: Make sure the affected devices "
4158 "are connected, then run 'zpool clear'.\n"));
4161 case ZPOOL_STATUS_BAD_LOG
:
4162 (void) printf(gettext("status: An intent log record "
4163 "could not be read.\n"
4164 "\tWaiting for adminstrator intervention to fix the "
4165 "faulted pool.\n"));
4166 (void) printf(gettext("action: Either restore the affected "
4167 "device(s) and run 'zpool online',\n"
4168 "\tor ignore the intent log records by running "
4169 "'zpool clear'.\n"));
4174 * The remaining errors can't actually be generated, yet.
4176 assert(reason
== ZPOOL_STATUS_OK
);
4180 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4183 if (config
!= NULL
) {
4186 nvlist_t
**spares
, **l2cache
;
4187 uint_t nspares
, nl2cache
;
4188 pool_scan_stat_t
*ps
= NULL
;
4190 (void) nvlist_lookup_uint64_array(nvroot
,
4191 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4192 print_scan_status(ps
);
4194 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4198 (void) printf(gettext("config:\n\n"));
4199 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4200 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4201 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4202 namewidth
, 0, B_FALSE
);
4204 if (num_logs(nvroot
) > 0)
4205 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4206 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4207 &l2cache
, &nl2cache
) == 0)
4208 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4210 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4211 &spares
, &nspares
) == 0)
4212 print_spares(zhp
, spares
, nspares
, namewidth
);
4214 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4216 nvlist_t
*nverrlist
= NULL
;
4219 * If the approximate error count is small, get a
4220 * precise count by fetching the entire log and
4221 * uniquifying the results.
4223 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4224 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4229 while ((elem
= nvlist_next_nvpair(nverrlist
,
4234 nvlist_free(nverrlist
);
4236 (void) printf("\n");
4239 (void) printf(gettext("errors: No known data "
4241 else if (!cbp
->cb_verbose
)
4242 (void) printf(gettext("errors: %llu data "
4243 "errors, use '-v' for a list\n"),
4244 (u_longlong_t
)nerr
);
4246 print_error_log(zhp
);
4249 if (cbp
->cb_dedup_stats
)
4250 print_dedup_stats(config
);
4252 (void) printf(gettext("config: The configuration cannot be "
4260 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4262 * -v Display complete error logs
4263 * -x Display only pools with potential problems
4264 * -D Display dedup status (undocumented)
4265 * -T Display a timestamp in date(1) or Unix format
4267 * Describes the health status of all pools or some subset.
4270 zpool_do_status(int argc
, char **argv
)
4274 unsigned long interval
= 0, count
= 0;
4275 status_cbdata_t cb
= { 0 };
4278 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4281 cb
.cb_verbose
= B_TRUE
;
4284 cb
.cb_explain
= B_TRUE
;
4287 cb
.cb_dedup_stats
= B_TRUE
;
4290 get_timestamp_arg(*optarg
);
4293 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4302 get_interval_count(&argc
, argv
, &interval
, &count
);
4305 cb
.cb_allpools
= B_TRUE
;
4307 cb
.cb_first
= B_TRUE
;
4310 if (timestamp_fmt
!= NODATE
)
4311 print_timestamp(timestamp_fmt
);
4313 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4314 status_callback
, &cb
);
4316 if (argc
== 0 && cb
.cb_count
== 0)
4317 (void) printf(gettext("no pools available\n"));
4318 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4319 (void) printf(gettext("all pools are healthy\n"));
4327 if (count
!= 0 && --count
== 0)
4330 (void) sleep(interval
);
4336 typedef struct upgrade_cbdata
{
4339 uint64_t cb_version
;
4344 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4348 uint64_t oldversion
;
4350 config
= zpool_get_config(zhp
, NULL
);
4351 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4354 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4355 assert(oldversion
< version
);
4357 ret
= zpool_upgrade(zhp
, version
);
4361 if (version
>= SPA_VERSION_FEATURES
) {
4362 (void) printf(gettext("Successfully upgraded "
4363 "'%s' from version %llu to feature flags.\n"),
4364 zpool_get_name(zhp
), oldversion
);
4366 (void) printf(gettext("Successfully upgraded "
4367 "'%s' from version %llu to version %llu.\n"),
4368 zpool_get_name(zhp
), oldversion
, version
);
4375 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4378 boolean_t firstff
= B_TRUE
;
4379 nvlist_t
*enabled
= zpool_get_features(zhp
);
4382 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4383 const char *fname
= spa_feature_table
[i
].fi_uname
;
4384 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4385 if (!nvlist_exists(enabled
, fguid
)) {
4387 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4388 ret
= zpool_set_prop(zhp
, propname
,
4389 ZFS_FEATURE_ENABLED
);
4397 (void) printf(gettext("Enabled the "
4398 "following features on '%s':\n"),
4399 zpool_get_name(zhp
));
4402 (void) printf(gettext(" %s\n"), fname
);
4413 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4415 upgrade_cbdata_t
*cbp
= arg
;
4418 boolean_t printnl
= B_FALSE
;
4421 config
= zpool_get_config(zhp
, NULL
);
4422 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4425 assert(SPA_VERSION_IS_SUPPORTED(version
));
4427 if (version
< cbp
->cb_version
) {
4428 cbp
->cb_first
= B_FALSE
;
4429 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4435 * If they did "zpool upgrade -a", then we could
4436 * be doing ioctls to different pools. We need
4437 * to log this history once to each pool, and bypass
4438 * the normal history logging that happens in main().
4440 (void) zpool_log_history(g_zfs
, history_str
);
4441 log_history
= B_FALSE
;
4444 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4446 ret
= upgrade_enable_all(zhp
, &count
);
4451 cbp
->cb_first
= B_FALSE
;
4457 (void) printf(gettext("\n"));
4464 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4466 upgrade_cbdata_t
*cbp
= arg
;
4470 config
= zpool_get_config(zhp
, NULL
);
4471 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4474 assert(SPA_VERSION_IS_SUPPORTED(version
));
4476 if (version
< SPA_VERSION_FEATURES
) {
4477 if (cbp
->cb_first
) {
4478 (void) printf(gettext("The following pools are "
4479 "formatted with legacy version numbers and can\n"
4480 "be upgraded to use feature flags. After "
4481 "being upgraded, these pools\nwill no "
4482 "longer be accessible by software that does not "
4483 "support feature\nflags.\n\n"));
4484 (void) printf(gettext("VER POOL\n"));
4485 (void) printf(gettext("--- ------------\n"));
4486 cbp
->cb_first
= B_FALSE
;
4489 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4490 zpool_get_name(zhp
));
4497 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4499 upgrade_cbdata_t
*cbp
= arg
;
4503 config
= zpool_get_config(zhp
, NULL
);
4504 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4507 if (version
>= SPA_VERSION_FEATURES
) {
4509 boolean_t poolfirst
= B_TRUE
;
4510 nvlist_t
*enabled
= zpool_get_features(zhp
);
4512 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4513 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4514 const char *fname
= spa_feature_table
[i
].fi_uname
;
4515 if (!nvlist_exists(enabled
, fguid
)) {
4516 if (cbp
->cb_first
) {
4517 (void) printf(gettext("\nSome "
4518 "supported features are not "
4519 "enabled on the following pools. "
4520 "Once a\nfeature is enabled the "
4521 "pool may become incompatible with "
4522 "software\nthat does not support "
4524 "zpool-features(5) for "
4526 (void) printf(gettext("POOL "
4528 (void) printf(gettext("------"
4530 cbp
->cb_first
= B_FALSE
;
4534 (void) printf(gettext("%s\n"),
4535 zpool_get_name(zhp
));
4536 poolfirst
= B_FALSE
;
4539 (void) printf(gettext(" %s\n"), fname
);
4549 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4551 boolean_t printnl
= B_FALSE
;
4552 upgrade_cbdata_t
*cbp
= data
;
4553 uint64_t cur_version
;
4556 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4557 (void) printf(gettext("'log' is now a reserved word\n"
4558 "Pool 'log' must be renamed using export and import"
4563 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4564 if (cur_version
> cbp
->cb_version
) {
4565 (void) printf(gettext("Pool '%s' is already formatted "
4566 "using more current version '%llu'.\n\n"),
4567 zpool_get_name(zhp
), cur_version
);
4571 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
4572 (void) printf(gettext("Pool '%s' is already formatted "
4573 "using version %llu.\n\n"), zpool_get_name(zhp
),
4578 if (cur_version
!= cbp
->cb_version
) {
4580 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4585 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4587 ret
= upgrade_enable_all(zhp
, &count
);
4593 } else if (cur_version
== SPA_VERSION
) {
4594 (void) printf(gettext("Pool '%s' already has all "
4595 "supported features enabled.\n"),
4596 zpool_get_name(zhp
));
4601 (void) printf(gettext("\n"));
4610 * zpool upgrade [-V version] <-a | pool ...>
4612 * With no arguments, display downrev'd ZFS pool available for upgrade.
4613 * Individual pools can be upgraded by specifying the pool, and '-a' will
4614 * upgrade all pools.
4617 zpool_do_upgrade(int argc
, char **argv
)
4620 upgrade_cbdata_t cb
= { 0 };
4622 boolean_t showversions
= B_FALSE
;
4623 boolean_t upgradeall
= B_FALSE
;
4628 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
4631 upgradeall
= B_TRUE
;
4634 showversions
= B_TRUE
;
4637 cb
.cb_version
= strtoll(optarg
, &end
, 10);
4639 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
4640 (void) fprintf(stderr
,
4641 gettext("invalid version '%s'\n"), optarg
);
4646 (void) fprintf(stderr
, gettext("missing argument for "
4647 "'%c' option\n"), optopt
);
4651 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4662 if (cb
.cb_version
== 0) {
4663 cb
.cb_version
= SPA_VERSION
;
4664 } else if (!upgradeall
&& argc
== 0) {
4665 (void) fprintf(stderr
, gettext("-V option is "
4666 "incompatible with other arguments\n"));
4671 if (upgradeall
|| argc
!= 0) {
4672 (void) fprintf(stderr
, gettext("-v option is "
4673 "incompatible with other arguments\n"));
4676 } else if (upgradeall
) {
4678 (void) fprintf(stderr
, gettext("-a option should not "
4679 "be used along with a pool name\n"));
4684 (void) printf(gettext("This system supports ZFS pool feature "
4689 (void) printf(gettext("The following features are "
4691 (void) printf(gettext("FEAT DESCRIPTION\n"));
4692 (void) printf("----------------------------------------------"
4693 "---------------\n");
4694 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4695 zfeature_info_t
*fi
= &spa_feature_table
[i
];
4696 const char *ro
= fi
->fi_can_readonly
?
4697 " (read-only compatible)" : "";
4699 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
4700 (void) printf(" %s\n", fi
->fi_desc
);
4702 (void) printf("\n");
4704 (void) printf(gettext("The following legacy versions are also "
4706 (void) printf(gettext("VER DESCRIPTION\n"));
4707 (void) printf("--- -----------------------------------------"
4708 "---------------\n");
4709 (void) printf(gettext(" 1 Initial ZFS version\n"));
4710 (void) printf(gettext(" 2 Ditto blocks "
4711 "(replicated metadata)\n"));
4712 (void) printf(gettext(" 3 Hot spares and double parity "
4714 (void) printf(gettext(" 4 zpool history\n"));
4715 (void) printf(gettext(" 5 Compression using the gzip "
4717 (void) printf(gettext(" 6 bootfs pool property\n"));
4718 (void) printf(gettext(" 7 Separate intent log devices\n"));
4719 (void) printf(gettext(" 8 Delegated administration\n"));
4720 (void) printf(gettext(" 9 refquota and refreservation "
4722 (void) printf(gettext(" 10 Cache devices\n"));
4723 (void) printf(gettext(" 11 Improved scrub performance\n"));
4724 (void) printf(gettext(" 12 Snapshot properties\n"));
4725 (void) printf(gettext(" 13 snapused property\n"));
4726 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4727 (void) printf(gettext(" 15 user/group space accounting\n"));
4728 (void) printf(gettext(" 16 stmf property support\n"));
4729 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4730 (void) printf(gettext(" 18 Snapshot user holds\n"));
4731 (void) printf(gettext(" 19 Log device removal\n"));
4732 (void) printf(gettext(" 20 Compression using zle "
4733 "(zero-length encoding)\n"));
4734 (void) printf(gettext(" 21 Deduplication\n"));
4735 (void) printf(gettext(" 22 Received properties\n"));
4736 (void) printf(gettext(" 23 Slim ZIL\n"));
4737 (void) printf(gettext(" 24 System attributes\n"));
4738 (void) printf(gettext(" 25 Improved scrub stats\n"));
4739 (void) printf(gettext(" 26 Improved snapshot deletion "
4741 (void) printf(gettext(" 27 Improved snapshot creation "
4743 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4744 (void) printf(gettext("\nFor more information on a particular "
4745 "version, including supported releases,\n"));
4746 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4747 } else if (argc
== 0 && upgradeall
) {
4748 cb
.cb_first
= B_TRUE
;
4749 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
4750 if (ret
== 0 && cb
.cb_first
) {
4751 if (cb
.cb_version
== SPA_VERSION
) {
4752 (void) printf(gettext("All pools are already "
4753 "formatted using feature flags.\n\n"));
4754 (void) printf(gettext("Every feature flags "
4755 "pool already has all supported features "
4758 (void) printf(gettext("All pools are already "
4759 "formatted with version %llu or higher.\n"),
4763 } else if (argc
== 0) {
4764 cb
.cb_first
= B_TRUE
;
4765 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
4769 (void) printf(gettext("All pools are formatted "
4770 "using feature flags.\n\n"));
4772 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4773 "for a list of available legacy versions.\n"));
4776 cb
.cb_first
= B_TRUE
;
4777 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
4781 (void) printf(gettext("Every feature flags pool has "
4782 "all supported features enabled.\n"));
4784 (void) printf(gettext("\n"));
4787 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
4794 typedef struct hist_cbdata
{
4801 * Print out the command history for a specific pool.
4804 get_history_one(zpool_handle_t
*zhp
, void *data
)
4810 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
4812 cb
->first
= B_FALSE
;
4814 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
4816 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
4819 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
4820 &records
, &numrecords
) == 0);
4821 for (i
= 0; i
< numrecords
; i
++) {
4822 nvlist_t
*rec
= records
[i
];
4825 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
4829 tsec
= fnvlist_lookup_uint64(records
[i
],
4831 (void) localtime_r(&tsec
, &t
);
4832 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
4835 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
4836 (void) printf("%s %s", tbuf
,
4837 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
4838 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
4840 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
4843 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
4844 (void) printf("%s unrecognized record:\n",
4846 dump_nvlist(rec
, 4);
4849 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
4850 zfs_history_event_names
[ievent
],
4851 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
4852 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
4853 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
4856 (void) printf("%s [txg:%lld] %s", tbuf
,
4857 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
4858 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
4859 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
4860 (void) printf(" %s (%llu)",
4861 fnvlist_lookup_string(rec
,
4863 fnvlist_lookup_uint64(rec
,
4866 (void) printf(" %s", fnvlist_lookup_string(rec
,
4867 ZPOOL_HIST_INT_STR
));
4868 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
4871 (void) printf("%s ioctl %s\n", tbuf
,
4872 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
4873 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
4874 (void) printf(" input:\n");
4875 dump_nvlist(fnvlist_lookup_nvlist(rec
,
4876 ZPOOL_HIST_INPUT_NVL
), 8);
4878 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
4879 (void) printf(" output:\n");
4880 dump_nvlist(fnvlist_lookup_nvlist(rec
,
4881 ZPOOL_HIST_OUTPUT_NVL
), 8);
4886 (void) printf("%s unrecognized record:\n", tbuf
);
4887 dump_nvlist(rec
, 4);
4891 (void) printf("\n");
4894 (void) printf(" [");
4895 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
4896 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
4897 struct passwd
*pwd
= getpwuid(who
);
4898 (void) printf("user %d ", (int)who
);
4900 (void) printf("(%s) ", pwd
->pw_name
);
4902 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
4903 (void) printf("on %s",
4904 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
4906 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
4907 (void) printf(":%s",
4908 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
4911 (void) printf("\n");
4913 (void) printf("\n");
4920 * zpool history <pool>
4922 * Displays the history of commands that modified pools.
4925 zpool_do_history(int argc
, char **argv
)
4927 hist_cbdata_t cbdata
= { 0 };
4931 cbdata
.first
= B_TRUE
;
4933 while ((c
= getopt(argc
, argv
, "li")) != -1) {
4936 cbdata
.longfmt
= B_TRUE
;
4939 cbdata
.internal
= B_TRUE
;
4942 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4950 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
4953 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
4954 (void) printf(gettext("no pools available\n"));
4962 get_callback(zpool_handle_t
*zhp
, void *data
)
4964 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
4965 char value
[MAXNAMELEN
];
4966 zprop_source_t srctype
;
4969 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
4972 * Skip the special fake placeholder. This will also skip
4973 * over the name property when 'all' is specified.
4975 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
4976 pl
== cbp
->cb_proplist
)
4979 if (pl
->pl_prop
== ZPROP_INVAL
&&
4980 (zpool_prop_feature(pl
->pl_user_prop
) ||
4981 zpool_prop_unsupported(pl
->pl_user_prop
))) {
4982 srctype
= ZPROP_SRC_LOCAL
;
4984 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
4985 value
, sizeof (value
)) == 0) {
4986 zprop_print_one_property(zpool_get_name(zhp
),
4987 cbp
, pl
->pl_user_prop
, value
, srctype
,
4991 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
4992 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
4995 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
4996 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5004 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5006 * -H Scripted mode. Don't display headers, and separate properties
5008 * -o List of columns to display. Defaults to
5009 * "name,property,value,source".
5010 * -p Diplay values in parsable (exact) format.
5012 * Get properties of pools in the system. Output space statistics
5013 * for each one as well as other attributes.
5016 zpool_do_get(int argc
, char **argv
)
5018 zprop_get_cbdata_t cb
= { 0 };
5019 zprop_list_t fake_name
= { 0 };
5024 cb
.cb_first
= B_TRUE
;
5027 * Set up default columns and sources.
5029 cb
.cb_sources
= ZPROP_SRC_ALL
;
5030 cb
.cb_columns
[0] = GET_COL_NAME
;
5031 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5032 cb
.cb_columns
[2] = GET_COL_VALUE
;
5033 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5034 cb
.cb_type
= ZFS_TYPE_POOL
;
5037 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
5040 cb
.cb_literal
= B_TRUE
;
5043 cb
.cb_scripted
= B_TRUE
;
5046 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
5048 while (*optarg
!= '\0') {
5049 static char *col_subopts
[] =
5050 { "name", "property", "value", "source",
5053 if (i
== ZFS_GET_NCOLS
) {
5054 (void) fprintf(stderr
, gettext("too "
5055 "many fields given to -o "
5060 switch (getsubopt(&optarg
, col_subopts
,
5063 cb
.cb_columns
[i
++] = GET_COL_NAME
;
5066 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
5069 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
5072 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
5076 (void) fprintf(stderr
,
5077 gettext("\"all\" conflicts "
5078 "with specific fields "
5079 "given to -o option\n"));
5082 cb
.cb_columns
[0] = GET_COL_NAME
;
5083 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5084 cb
.cb_columns
[2] = GET_COL_VALUE
;
5085 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5089 (void) fprintf(stderr
,
5090 gettext("invalid column name "
5097 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5107 (void) fprintf(stderr
, gettext("missing property "
5112 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
5113 ZFS_TYPE_POOL
) != 0)
5119 if (cb
.cb_proplist
!= NULL
) {
5120 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5121 fake_name
.pl_width
= strlen(gettext("NAME"));
5122 fake_name
.pl_next
= cb
.cb_proplist
;
5123 cb
.cb_proplist
= &fake_name
;
5126 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5129 if (cb
.cb_proplist
== &fake_name
)
5130 zprop_free_list(fake_name
.pl_next
);
5132 zprop_free_list(cb
.cb_proplist
);
5137 typedef struct set_cbdata
{
5140 boolean_t cb_any_successful
;
5144 set_callback(zpool_handle_t
*zhp
, void *data
)
5147 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5149 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5152 cb
->cb_any_successful
= B_TRUE
;
5158 zpool_do_set(int argc
, char **argv
)
5160 set_cbdata_t cb
= { 0 };
5163 if (argc
> 1 && argv
[1][0] == '-') {
5164 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5170 (void) fprintf(stderr
, gettext("missing property=value "
5176 (void) fprintf(stderr
, gettext("missing pool name\n"));
5181 (void) fprintf(stderr
, gettext("too many pool names\n"));
5185 cb
.cb_propname
= argv
[1];
5186 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5187 if (cb
.cb_value
== NULL
) {
5188 (void) fprintf(stderr
, gettext("missing value in "
5189 "property=value argument\n"));
5193 *(cb
.cb_value
) = '\0';
5196 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5203 find_command_idx(char *command
, int *idx
)
5207 for (i
= 0; i
< NCOMMAND
; i
++) {
5208 if (command_table
[i
].name
== NULL
)
5211 if (strcmp(command
, command_table
[i
].name
) == 0) {
5220 main(int argc
, char **argv
)
5226 (void) setlocale(LC_ALL
, "");
5227 (void) textdomain(TEXT_DOMAIN
);
5229 if ((g_zfs
= libzfs_init()) == NULL
) {
5230 (void) fprintf(stderr
, gettext("internal error: failed to "
5231 "initialize ZFS library\n"));
5235 libzfs_print_on_error(g_zfs
, B_TRUE
);
5240 * Make sure the user has specified some command.
5243 (void) fprintf(stderr
, gettext("missing command\n"));
5252 if (strcmp(cmdname
, "-?") == 0)
5255 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5258 * Run the appropriate command.
5260 if (find_command_idx(cmdname
, &i
) == 0) {
5261 current_command
= &command_table
[i
];
5262 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5263 } else if (strchr(cmdname
, '=')) {
5264 verify(find_command_idx("set", &i
) == 0);
5265 current_command
= &command_table
[i
];
5266 ret
= command_table
[i
].func(argc
, argv
);
5267 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5269 * 'freeze' is a vile debugging abomination, so we treat
5273 int fd
= open(ZFS_DEV
, O_RDWR
);
5274 (void) strcpy((void *)buf
, argv
[2]);
5275 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5277 (void) fprintf(stderr
, gettext("unrecognized "
5278 "command '%s'\n"), cmdname
);
5282 if (ret
== 0 && log_history
)
5283 (void) zpool_log_history(g_zfs
, history_str
);
5288 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5289 * for the purposes of running ::findleaks.
5291 if (getenv("ZFS_ABORT") != NULL
) {
5292 (void) printf("dumping core by request\n");