4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
48 #include <sys/fs/zfs.h>
50 #include <sys/fm/util.h>
51 #include <sys/fm/protocol.h>
52 #include <sys/zfs_ioctl.h>
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 #include "statcommon.h"
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
69 static int zpool_do_list(int, char **);
70 static int zpool_do_iostat(int, char **);
71 static int zpool_do_status(int, char **);
73 static int zpool_do_online(int, char **);
74 static int zpool_do_offline(int, char **);
75 static int zpool_do_clear(int, char **);
76 static int zpool_do_reopen(int, char **);
78 static int zpool_do_reguid(int, char **);
80 static int zpool_do_attach(int, char **);
81 static int zpool_do_detach(int, char **);
82 static int zpool_do_replace(int, char **);
83 static int zpool_do_split(int, char **);
85 static int zpool_do_scrub(int, char **);
87 static int zpool_do_import(int, char **);
88 static int zpool_do_export(int, char **);
90 static int zpool_do_upgrade(int, char **);
92 static int zpool_do_history(int, char **);
93 static int zpool_do_events(int, char **);
95 static int zpool_do_get(int, char **);
96 static int zpool_do_set(int, char **);
99 * These libumem hooks provide a reasonable set of defaults for the allocator's
100 * debugging facilities.
105 _umem_debug_init(void)
107 return ("default,verbose"); /* $UMEM_DEBUG setting */
111 _umem_logging_init(void)
113 return ("fail,contents"); /* $UMEM_LOGGING setting */
146 typedef struct zpool_command
{
148 int (*func
)(int, char **);
153 * Master command table. Each ZFS command has a name, associated function, and
154 * usage message. The usage messages need to be internationalized, so we have
155 * to have a function to return the usage message based on a command index.
157 * These commands are organized according to how they are displayed in the usage
158 * message. An empty command (one with a NULL name) indicates an empty line in
159 * the generic usage message.
161 static zpool_command_t command_table
[] = {
162 { "create", zpool_do_create
, HELP_CREATE
},
163 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
165 { "add", zpool_do_add
, HELP_ADD
},
166 { "remove", zpool_do_remove
, HELP_REMOVE
},
168 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
170 { "list", zpool_do_list
, HELP_LIST
},
171 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
172 { "status", zpool_do_status
, HELP_STATUS
},
174 { "online", zpool_do_online
, HELP_ONLINE
},
175 { "offline", zpool_do_offline
, HELP_OFFLINE
},
176 { "clear", zpool_do_clear
, HELP_CLEAR
},
177 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
179 { "attach", zpool_do_attach
, HELP_ATTACH
},
180 { "detach", zpool_do_detach
, HELP_DETACH
},
181 { "replace", zpool_do_replace
, HELP_REPLACE
},
182 { "split", zpool_do_split
, HELP_SPLIT
},
184 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
186 { "import", zpool_do_import
, HELP_IMPORT
},
187 { "export", zpool_do_export
, HELP_EXPORT
},
188 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
189 { "reguid", zpool_do_reguid
, HELP_REGUID
},
191 { "history", zpool_do_history
, HELP_HISTORY
},
192 { "events", zpool_do_events
, HELP_EVENTS
},
194 { "get", zpool_do_get
, HELP_GET
},
195 { "set", zpool_do_set
, HELP_SET
},
198 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
200 static zpool_command_t
*current_command
;
201 static char history_str
[HIS_MAX_RECORD_LEN
];
202 static boolean_t log_history
= B_TRUE
;
203 static uint_t timestamp_fmt
= NODATE
;
206 get_usage(zpool_help_t idx
) {
209 return (gettext("\tadd [-fn] [-o property=value] "
210 "<pool> <vdev> ...\n"));
212 return (gettext("\tattach [-f] [-o property=value] "
213 "<pool> <device> <new-device>\n"));
215 return (gettext("\tclear [-nF] <pool> [device]\n"));
217 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
218 "\t [-O file-system-property=value] ... \n"
219 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
221 return (gettext("\tdestroy [-f] <pool>\n"));
223 return (gettext("\tdetach <pool> <device>\n"));
225 return (gettext("\texport [-f] <pool> ...\n"));
227 return (gettext("\thistory [-il] [<pool>] ...\n"));
229 return (gettext("\timport [-d dir] [-D]\n"
230 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
231 "\timport [-o mntopts] [-o property=value] ... \n"
232 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
233 "[-R root] [-F [-n]] -a\n"
234 "\timport [-o mntopts] [-o property=value] ... \n"
235 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
236 "[-R root] [-F [-n]]\n"
237 "\t <pool | id> [newpool]\n"));
239 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
241 case HELP_LABELCLEAR
:
242 return (gettext("\tlabelclear [-f] <vdev>\n"));
244 return (gettext("\tlist [-Hv] [-o property[,...]] "
245 "[-T d|u] [pool] ... [interval [count]]\n"));
247 return (gettext("\toffline [-t] <pool> <device> ...\n"));
249 return (gettext("\tonline <pool> <device> ...\n"));
251 return (gettext("\treplace [-f] [-o property=value] "
252 "<pool> <device> [new-device]\n"));
254 return (gettext("\tremove <pool> <device> ...\n"));
256 return (gettext("\treopen <pool>\n"));
258 return (gettext("\tscrub [-s] <pool> ...\n"));
260 return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
263 return (gettext("\tupgrade\n"
265 "\tupgrade [-V version] <-a | pool ...>\n"));
267 return (gettext("\tevents [-vHfc]\n"));
269 return (gettext("\tget [-pH] <\"all\" | property[,...]> "
272 return (gettext("\tset <property=value> <pool> \n"));
274 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
275 "\t [-o property=value] <pool> <newpool> "
276 "[<device> ...]\n"));
278 return (gettext("\treguid <pool>\n"));
287 * Callback routine that will print out a pool property value.
290 print_prop_cb(int prop
, void *cb
)
294 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
296 if (zpool_prop_readonly(prop
))
297 (void) fprintf(fp
, " NO ");
299 (void) fprintf(fp
, " YES ");
301 if (zpool_prop_values(prop
) == NULL
)
302 (void) fprintf(fp
, "-\n");
304 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
310 * Display usage message. If we're inside a command, display only the usage for
311 * that command. Otherwise, iterate over the entire command table and display
312 * a complete usage message.
315 usage(boolean_t requested
)
317 FILE *fp
= requested
? stdout
: stderr
;
319 if (current_command
== NULL
) {
322 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
324 gettext("where 'command' is one of the following:\n\n"));
326 for (i
= 0; i
< NCOMMAND
; i
++) {
327 if (command_table
[i
].name
== NULL
)
328 (void) fprintf(fp
, "\n");
330 (void) fprintf(fp
, "%s",
331 get_usage(command_table
[i
].usage
));
334 (void) fprintf(fp
, gettext("usage:\n"));
335 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
338 if (current_command
!= NULL
&&
339 ((strcmp(current_command
->name
, "set") == 0) ||
340 (strcmp(current_command
->name
, "get") == 0) ||
341 (strcmp(current_command
->name
, "list") == 0))) {
344 gettext("\nthe following properties are supported:\n"));
346 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
347 "PROPERTY", "EDIT", "VALUES");
349 /* Iterate over all properties */
350 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
353 (void) fprintf(fp
, "\t%-15s ", "feature@...");
354 (void) fprintf(fp
, "YES disabled | enabled | active\n");
356 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
357 "appended with a feature name.\nSee zpool-features(5).\n"));
361 * See comments at end of main().
363 if (getenv("ZFS_ABORT") != NULL
) {
364 (void) printf("dumping core by request\n");
368 exit(requested
? 0 : 2);
372 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
373 boolean_t print_logs
)
380 (void) printf("\t%*s%s\n", indent
, "", name
);
382 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
383 &child
, &children
) != 0)
386 for (c
= 0; c
< children
; c
++) {
387 uint64_t is_log
= B_FALSE
;
389 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
391 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
394 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
395 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
402 prop_list_contains_feature(nvlist_t
*proplist
)
405 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
406 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
407 if (zpool_prop_feature(nvpair_name(nvp
)))
414 * Add a property pair (name, string-value) into a property nvlist.
417 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
420 zpool_prop_t prop
= ZPROP_INVAL
;
426 if (*props
== NULL
&&
427 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
428 (void) fprintf(stderr
,
429 gettext("internal error: out of memory\n"));
436 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
438 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
439 !zpool_prop_feature(propname
)) {
440 (void) fprintf(stderr
, gettext("property '%s' is "
441 "not a valid pool property\n"), propname
);
446 * feature@ properties and version should not be specified
449 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
450 nvlist_exists(proplist
, vname
)) ||
451 (prop
== ZPOOL_PROP_VERSION
&&
452 prop_list_contains_feature(proplist
))) {
453 (void) fprintf(stderr
, gettext("'feature@' and "
454 "'version' properties cannot be specified "
460 if (zpool_prop_feature(propname
))
463 normnm
= zpool_prop_to_name(prop
);
465 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
466 normnm
= zfs_prop_to_name(fprop
);
472 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
473 prop
!= ZPOOL_PROP_CACHEFILE
) {
474 (void) fprintf(stderr
, gettext("property '%s' "
475 "specified multiple times\n"), propname
);
479 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
480 (void) fprintf(stderr
, gettext("internal "
481 "error: out of memory\n"));
489 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
491 * -f Force addition of devices, even if they appear in use
492 * -n Do not add the devices, but display the resulting layout if
493 * they were to be added.
494 * -o Set property=value.
496 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
497 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
501 zpool_do_add(int argc
, char **argv
)
503 boolean_t force
= B_FALSE
;
504 boolean_t dryrun
= B_FALSE
;
511 nvlist_t
*props
= NULL
;
515 while ((c
= getopt(argc
, argv
, "fno:")) != -1) {
524 if ((propval
= strchr(optarg
, '=')) == NULL
) {
525 (void) fprintf(stderr
, gettext("missing "
526 "'=' for -o option\n"));
532 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
533 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
537 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
546 /* get pool name and check number of arguments */
548 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
552 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
561 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
564 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
565 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
571 /* pass off to get_vdev_spec for processing */
572 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
574 if (nvroot
== NULL
) {
580 nvlist_t
*poolnvroot
;
582 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
585 (void) printf(gettext("would update '%s' to the following "
586 "configuration:\n"), zpool_get_name(zhp
));
588 /* print original main pool and new tree */
589 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
590 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
592 /* Do the same for the logs */
593 if (num_logs(poolnvroot
) > 0) {
594 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
595 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
596 } else if (num_logs(nvroot
) > 0) {
597 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
602 ret
= (zpool_add(zhp
, nvroot
) != 0);
613 * zpool remove <pool> <vdev> ...
615 * Removes the given vdev from the pool. Currently, this supports removing
616 * spares, cache, and log devices from the pool.
619 zpool_do_remove(int argc
, char **argv
)
628 /* get pool name and check number of arguments */
630 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
634 (void) fprintf(stderr
, gettext("missing device\n"));
640 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
643 for (i
= 1; i
< argc
; i
++) {
644 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
652 * zpool labelclear <vdev>
654 * Verifies that the vdev is not active and zeros out the label information
658 zpool_do_labelclear(int argc
, char **argv
)
661 int c
, fd
= -1, ret
= 0;
663 boolean_t inuse
= B_FALSE
;
664 boolean_t force
= B_FALSE
;
667 while ((c
= getopt(argc
, argv
, "f")) != -1) {
673 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
684 (void) fprintf(stderr
, gettext("missing vdev device name\n"));
689 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
690 (void) fprintf(stderr
, gettext("Unable to open %s\n"), vdev
);
695 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0) {
699 (void) fprintf(stderr
,
700 gettext("Unable to determine pool state for %s\n"
701 "Use -f to force the clearing any label data\n"), vdev
);
709 case POOL_STATE_ACTIVE
:
710 case POOL_STATE_SPARE
:
711 case POOL_STATE_L2CACHE
:
712 (void) fprintf(stderr
,
713 gettext("labelclear operation failed.\n"
714 "\tVdev %s is a member (%s), of pool \"%s\".\n"
715 "\tTo remove label information from this device, "
716 "export or destroy\n\tthe pool, or remove %s from "
717 "the configuration of this pool\n\tand retry the "
718 "labelclear operation.\n"),
719 vdev
, zpool_pool_state_to_name(state
), name
, vdev
);
723 case POOL_STATE_EXPORTED
:
727 (void) fprintf(stderr
,
728 gettext("labelclear operation failed.\n\tVdev "
729 "%s is a member of the exported pool \"%s\".\n"
730 "\tUse \"zpool labelclear -f %s\" to force the "
731 "removal of label\n\tinformation.\n"),
736 case POOL_STATE_POTENTIALLY_ACTIVE
:
740 (void) fprintf(stderr
,
741 gettext("labelclear operation failed.\n"
742 "\tVdev %s is a member of the pool \"%s\".\n"
743 "\tThis pool is unknown to this system, but may "
744 "be active on\n\tanother system. Use "
745 "\'zpool labelclear -f %s\' to force the\n"
746 "\tremoval of label information.\n"),
751 case POOL_STATE_DESTROYED
:
752 /* inuse should never be set for a destroyed pool... */
758 if (zpool_clear_label(fd
) != 0) {
759 (void) fprintf(stderr
,
760 gettext("Label clear failed on vdev %s\n"), vdev
);
773 * zpool create [-fnd] [-o property=value] ...
774 * [-O file-system-property=value] ...
775 * [-R root] [-m mountpoint] <pool> <dev> ...
777 * -f Force creation, even if devices appear in use
778 * -n Do not create the pool, but display the resulting layout if it
779 * were to be created.
780 * -R Create a pool under an alternate root
781 * -m Set default mountpoint for the root dataset. By default it's
783 * -o Set property=value.
784 * -d Don't automatically enable all supported pool features
785 * (individual features can be enabled with -o).
786 * -O Set fsproperty=value in the pool's root file system
788 * Creates the named pool according to the given vdev specification. The
789 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
790 * we get the nvlist back from get_vdev_spec(), we either print out the contents
791 * (if '-n' was specified), or pass it to libzfs to do the creation.
794 zpool_do_create(int argc
, char **argv
)
796 boolean_t force
= B_FALSE
;
797 boolean_t dryrun
= B_FALSE
;
798 boolean_t enable_all_pool_feat
= B_TRUE
;
800 nvlist_t
*nvroot
= NULL
;
803 char *altroot
= NULL
;
804 char *mountpoint
= NULL
;
805 nvlist_t
*fsprops
= NULL
;
806 nvlist_t
*props
= NULL
;
810 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:")) != -1) {
819 enable_all_pool_feat
= B_FALSE
;
823 if (add_prop_list(zpool_prop_to_name(
824 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
826 if (nvlist_lookup_string(props
,
827 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
830 if (add_prop_list(zpool_prop_to_name(
831 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
835 /* Equivalent to -O mountpoint=optarg */
839 if ((propval
= strchr(optarg
, '=')) == NULL
) {
840 (void) fprintf(stderr
, gettext("missing "
841 "'=' for -o option\n"));
847 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
851 * If the user is creating a pool that doesn't support
852 * feature flags, don't enable any features.
854 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
858 ver
= strtoull(propval
, &end
, 10);
860 ver
< SPA_VERSION_FEATURES
) {
861 enable_all_pool_feat
= B_FALSE
;
866 if ((propval
= strchr(optarg
, '=')) == NULL
) {
867 (void) fprintf(stderr
, gettext("missing "
868 "'=' for -O option\n"));
875 * Mountpoints are checked and then added later.
876 * Uniquely among properties, they can be specified
877 * more than once, to avoid conflict with -m.
879 if (0 == strcmp(optarg
,
880 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
881 mountpoint
= propval
;
882 } else if (add_prop_list(optarg
, propval
, &fsprops
,
888 (void) fprintf(stderr
, gettext("missing argument for "
889 "'%c' option\n"), optopt
);
892 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
901 /* get pool name and check number of arguments */
903 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
907 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
914 * As a special case, check for use of '/' in the name, and direct the
915 * user to use 'zfs create' instead.
917 if (strchr(poolname
, '/') != NULL
) {
918 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
919 "character '/' in pool name\n"), poolname
);
920 (void) fprintf(stderr
, gettext("use 'zfs create' to "
921 "create a dataset\n"));
925 /* pass off to get_vdev_spec for bulk processing */
926 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
931 /* make_root_vdev() allows 0 toplevel children if there are spares */
932 if (!zfs_allocatable_devs(nvroot
)) {
933 (void) fprintf(stderr
, gettext("invalid vdev "
934 "specification: at least one toplevel vdev must be "
939 if (altroot
!= NULL
&& altroot
[0] != '/') {
940 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
941 "must be an absolute path\n"), altroot
);
946 * Check the validity of the mountpoint and direct the user to use the
947 * '-m' mountpoint option if it looks like its in use.
949 if (mountpoint
== NULL
||
950 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
951 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
952 char buf
[MAXPATHLEN
];
955 if (mountpoint
&& mountpoint
[0] != '/') {
956 (void) fprintf(stderr
, gettext("invalid mountpoint "
957 "'%s': must be an absolute path, 'legacy', or "
958 "'none'\n"), mountpoint
);
962 if (mountpoint
== NULL
) {
964 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
967 (void) snprintf(buf
, sizeof (buf
), "/%s",
971 (void) snprintf(buf
, sizeof (buf
), "%s%s",
972 altroot
, mountpoint
);
974 (void) snprintf(buf
, sizeof (buf
), "%s",
978 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
979 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
980 "%s\n"), buf
, strerror(errno
));
981 (void) fprintf(stderr
, gettext("use '-m' "
982 "option to provide a different default\n"));
987 while (count
< 3 && readdir(dirp
) != NULL
)
989 (void) closedir(dirp
);
992 (void) fprintf(stderr
, gettext("mountpoint "
993 "'%s' exists and is not empty\n"), buf
);
994 (void) fprintf(stderr
, gettext("use '-m' "
995 "option to provide a "
996 "different default\n"));
1003 * Now that the mountpoint's validity has been checked, ensure that
1004 * the property is set appropriately prior to creating the pool.
1006 if (mountpoint
!= NULL
) {
1007 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1008 mountpoint
, &fsprops
, B_FALSE
);
1016 * For a dry run invocation, print out a basic message and run
1017 * through all the vdevs in the list and print out in an
1018 * appropriate hierarchy.
1020 (void) printf(gettext("would create '%s' with the "
1021 "following layout:\n\n"), poolname
);
1023 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
1024 if (num_logs(nvroot
) > 0)
1025 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
1030 * Hand off to libzfs.
1032 if (enable_all_pool_feat
) {
1034 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1035 char propname
[MAXPATHLEN
];
1036 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1038 (void) snprintf(propname
, sizeof (propname
),
1039 "feature@%s", feat
->fi_uname
);
1042 * Skip feature if user specified it manually
1043 * on the command line.
1045 if (nvlist_exists(props
, propname
))
1048 ret
= add_prop_list(propname
,
1049 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1056 if (zpool_create(g_zfs
, poolname
,
1057 nvroot
, props
, fsprops
) == 0) {
1058 zfs_handle_t
*pool
= zfs_open(g_zfs
, poolname
,
1059 ZFS_TYPE_FILESYSTEM
);
1061 if (zfs_mount(pool
, NULL
, 0) == 0)
1062 ret
= zfs_shareall(pool
);
1065 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1066 (void) fprintf(stderr
, gettext("pool name may have "
1072 nvlist_free(nvroot
);
1073 nvlist_free(fsprops
);
1077 nvlist_free(fsprops
);
1084 * zpool destroy <pool>
1086 * -f Forcefully unmount any datasets
1088 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1091 zpool_do_destroy(int argc
, char **argv
)
1093 boolean_t force
= B_FALSE
;
1096 zpool_handle_t
*zhp
;
1100 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1106 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1115 /* check arguments */
1117 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1121 (void) fprintf(stderr
, gettext("too many arguments\n"));
1127 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1129 * As a special case, check for use of '/' in the name, and
1130 * direct the user to use 'zfs destroy' instead.
1132 if (strchr(pool
, '/') != NULL
)
1133 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1134 "destroy a dataset\n"));
1138 if (zpool_disable_datasets(zhp
, force
) != 0) {
1139 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1140 "could not unmount datasets\n"), zpool_get_name(zhp
));
1144 /* The history must be logged as part of the export */
1145 log_history
= B_FALSE
;
1147 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1155 * zpool export [-f] <pool> ...
1157 * -f Forcefully unmount datasets
1159 * Export the given pools. By default, the command will attempt to cleanly
1160 * unmount any active datasets within the pool. If the '-f' flag is specified,
1161 * then the datasets will be forcefully unmounted.
1164 zpool_do_export(int argc
, char **argv
)
1166 boolean_t force
= B_FALSE
;
1167 boolean_t hardforce
= B_FALSE
;
1169 zpool_handle_t
*zhp
;
1174 while ((c
= getopt(argc
, argv
, "fF")) != -1) {
1183 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1192 /* check arguments */
1194 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1199 for (i
= 0; i
< argc
; i
++) {
1200 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) == NULL
) {
1205 if (zpool_disable_datasets(zhp
, force
) != 0) {
1211 /* The history must be logged as part of the export */
1212 log_history
= B_FALSE
;
1215 if (zpool_export_force(zhp
, history_str
) != 0)
1217 } else if (zpool_export(zhp
, force
, history_str
) != 0) {
1228 * Given a vdev configuration, determine the maximum width needed for the device
1232 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1234 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1239 if (strlen(name
) + depth
> max
)
1240 max
= strlen(name
) + depth
;
1244 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1245 &child
, &children
) == 0) {
1246 for (c
= 0; c
< children
; c
++)
1247 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1252 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1253 &child
, &children
) == 0) {
1254 for (c
= 0; c
< children
; c
++)
1255 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1260 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1261 &child
, &children
) == 0) {
1262 for (c
= 0; c
< children
; c
++)
1263 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1272 typedef struct spare_cbdata
{
1274 zpool_handle_t
*cb_zhp
;
1278 find_vdev(nvlist_t
*nv
, uint64_t search
)
1284 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1288 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1289 &child
, &children
) == 0) {
1290 for (c
= 0; c
< children
; c
++)
1291 if (find_vdev(child
[c
], search
))
1299 find_spare(zpool_handle_t
*zhp
, void *data
)
1301 spare_cbdata_t
*cbp
= data
;
1302 nvlist_t
*config
, *nvroot
;
1304 config
= zpool_get_config(zhp
, NULL
);
1305 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1308 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1318 * Print out configuration state as requested by status_callback.
1321 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1322 int namewidth
, int depth
, boolean_t isspare
)
1326 pool_scan_stat_t
*ps
= NULL
;
1328 char rbuf
[6], wbuf
[6], cbuf
[6];
1330 uint64_t notpresent
;
1334 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1335 &child
, &children
) != 0)
1338 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1339 (uint64_t **)&vs
, &c
) == 0);
1341 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1344 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1347 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1349 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1353 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1357 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1358 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1359 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1360 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1363 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1364 ¬present
) == 0) {
1366 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1367 (void) printf(" was %s", path
);
1368 } else if (vs
->vs_aux
!= 0) {
1371 switch (vs
->vs_aux
) {
1372 case VDEV_AUX_OPEN_FAILED
:
1373 (void) printf(gettext("cannot open"));
1376 case VDEV_AUX_BAD_GUID_SUM
:
1377 (void) printf(gettext("missing device"));
1380 case VDEV_AUX_NO_REPLICAS
:
1381 (void) printf(gettext("insufficient replicas"));
1384 case VDEV_AUX_VERSION_NEWER
:
1385 (void) printf(gettext("newer version"));
1388 case VDEV_AUX_UNSUP_FEAT
:
1389 (void) printf(gettext("unsupported feature(s)"));
1392 case VDEV_AUX_SPARED
:
1393 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1395 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1396 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1397 zpool_get_name(zhp
)) == 0)
1398 (void) printf(gettext("currently in "
1401 (void) printf(gettext("in use by "
1403 zpool_get_name(cb
.cb_zhp
));
1404 zpool_close(cb
.cb_zhp
);
1406 (void) printf(gettext("currently in use"));
1410 case VDEV_AUX_ERR_EXCEEDED
:
1411 (void) printf(gettext("too many errors"));
1414 case VDEV_AUX_IO_FAILURE
:
1415 (void) printf(gettext("experienced I/O failures"));
1418 case VDEV_AUX_BAD_LOG
:
1419 (void) printf(gettext("bad intent log"));
1422 case VDEV_AUX_EXTERNAL
:
1423 (void) printf(gettext("external device fault"));
1426 case VDEV_AUX_SPLIT_POOL
:
1427 (void) printf(gettext("split into new pool"));
1431 (void) printf(gettext("corrupted data"));
1436 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1437 (uint64_t **)&ps
, &c
);
1439 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1440 vs
->vs_scan_processed
!= 0 && children
== 0) {
1441 (void) printf(gettext(" (%s)"),
1442 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1443 "resilvering" : "repairing");
1446 (void) printf("\n");
1448 for (c
= 0; c
< children
; c
++) {
1449 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1451 /* Don't print logs or holes here */
1452 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1454 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1456 if (islog
|| ishole
)
1458 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1459 print_status_config(zhp
, vname
, child
[c
],
1460 namewidth
, depth
+ 2, isspare
);
1467 * Print the configuration of an exported pool. Iterate over all vdevs in the
1468 * pool, printing out the name and status for each one.
1471 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1478 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1479 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1480 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1483 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1484 (uint64_t **)&vs
, &c
) == 0);
1486 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1487 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1489 if (vs
->vs_aux
!= 0) {
1492 switch (vs
->vs_aux
) {
1493 case VDEV_AUX_OPEN_FAILED
:
1494 (void) printf(gettext("cannot open"));
1497 case VDEV_AUX_BAD_GUID_SUM
:
1498 (void) printf(gettext("missing device"));
1501 case VDEV_AUX_NO_REPLICAS
:
1502 (void) printf(gettext("insufficient replicas"));
1505 case VDEV_AUX_VERSION_NEWER
:
1506 (void) printf(gettext("newer version"));
1509 case VDEV_AUX_UNSUP_FEAT
:
1510 (void) printf(gettext("unsupported feature(s)"));
1513 case VDEV_AUX_ERR_EXCEEDED
:
1514 (void) printf(gettext("too many errors"));
1518 (void) printf(gettext("corrupted data"));
1522 (void) printf("\n");
1524 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1525 &child
, &children
) != 0)
1528 for (c
= 0; c
< children
; c
++) {
1529 uint64_t is_log
= B_FALSE
;
1531 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1536 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1537 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1541 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1542 &child
, &children
) == 0) {
1543 (void) printf(gettext("\tcache\n"));
1544 for (c
= 0; c
< children
; c
++) {
1545 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1546 (void) printf("\t %s\n", vname
);
1551 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1552 &child
, &children
) == 0) {
1553 (void) printf(gettext("\tspares\n"));
1554 for (c
= 0; c
< children
; c
++) {
1555 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1556 (void) printf("\t %s\n", vname
);
1564 * Logs are recorded as top level vdevs in the main pool child array
1565 * but with "is_log" set to 1. We use either print_status_config() or
1566 * print_import_config() to print the top level logs then any log
1567 * children (eg mirrored slogs) are printed recursively - which
1568 * works because only the top level vdev is marked "is_log"
1571 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1576 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1580 (void) printf(gettext("\tlogs\n"));
1582 for (c
= 0; c
< children
; c
++) {
1583 uint64_t is_log
= B_FALSE
;
1586 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1590 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1592 print_status_config(zhp
, name
, child
[c
], namewidth
,
1595 print_import_config(name
, child
[c
], namewidth
, 2);
1601 * Display the status for the given pool.
1604 show_import(nvlist_t
*config
)
1606 uint64_t pool_state
;
1612 zpool_status_t reason
;
1613 zpool_errata_t errata
;
1619 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1621 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1623 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1625 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1628 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1629 (uint64_t **)&vs
, &vsc
) == 0);
1630 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1632 reason
= zpool_import_status(config
, &msgid
, &errata
);
1634 (void) printf(gettext(" pool: %s\n"), name
);
1635 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1636 (void) printf(gettext(" state: %s"), health
);
1637 if (pool_state
== POOL_STATE_DESTROYED
)
1638 (void) printf(gettext(" (DESTROYED)"));
1639 (void) printf("\n");
1642 case ZPOOL_STATUS_MISSING_DEV_R
:
1643 case ZPOOL_STATUS_MISSING_DEV_NR
:
1644 case ZPOOL_STATUS_BAD_GUID_SUM
:
1645 (void) printf(gettext(" status: One or more devices are "
1646 "missing from the system.\n"));
1649 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1650 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1651 (void) printf(gettext(" status: One or more devices contains "
1652 "corrupted data.\n"));
1655 case ZPOOL_STATUS_CORRUPT_DATA
:
1657 gettext(" status: The pool data is corrupted.\n"));
1660 case ZPOOL_STATUS_OFFLINE_DEV
:
1661 (void) printf(gettext(" status: One or more devices "
1662 "are offlined.\n"));
1665 case ZPOOL_STATUS_CORRUPT_POOL
:
1666 (void) printf(gettext(" status: The pool metadata is "
1670 case ZPOOL_STATUS_VERSION_OLDER
:
1671 (void) printf(gettext(" status: The pool is formatted using a "
1672 "legacy on-disk version.\n"));
1675 case ZPOOL_STATUS_VERSION_NEWER
:
1676 (void) printf(gettext(" status: The pool is formatted using an "
1677 "incompatible version.\n"));
1680 case ZPOOL_STATUS_FEAT_DISABLED
:
1681 (void) printf(gettext(" status: Some supported features are "
1682 "not enabled on the pool.\n"));
1685 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1686 (void) printf(gettext("status: The pool uses the following "
1687 "feature(s) not supported on this sytem:\n"));
1688 zpool_print_unsup_feat(config
);
1691 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1692 (void) printf(gettext("status: The pool can only be accessed "
1693 "in read-only mode on this system. It\n\tcannot be "
1694 "accessed in read-write mode because it uses the "
1695 "following\n\tfeature(s) not supported on this system:\n"));
1696 zpool_print_unsup_feat(config
);
1699 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1700 (void) printf(gettext(" status: The pool was last accessed by "
1701 "another system.\n"));
1704 case ZPOOL_STATUS_FAULTED_DEV_R
:
1705 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1706 (void) printf(gettext(" status: One or more devices are "
1710 case ZPOOL_STATUS_BAD_LOG
:
1711 (void) printf(gettext(" status: An intent log record cannot be "
1715 case ZPOOL_STATUS_RESILVERING
:
1716 (void) printf(gettext(" status: One or more devices were being "
1720 case ZPOOL_STATUS_ERRATA
:
1721 (void) printf(gettext(" status: Errata #%d detected.\n"),
1727 * No other status can be seen when importing pools.
1729 assert(reason
== ZPOOL_STATUS_OK
);
1733 * Print out an action according to the overall state of the pool.
1735 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1736 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1737 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1738 (void) printf(gettext(" action: The pool can be "
1739 "imported using its name or numeric identifier, "
1740 "though\n\tsome features will not be available "
1741 "without an explicit 'zpool upgrade'.\n"));
1742 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1743 (void) printf(gettext(" action: The pool can be "
1744 "imported using its name or numeric "
1745 "identifier and\n\tthe '-f' flag.\n"));
1746 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
1748 case ZPOOL_ERRATA_NONE
:
1751 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
1752 (void) printf(gettext(" action: The pool can "
1753 "be imported using its name or numeric "
1754 "identifier,\n\thowever there is a compat"
1755 "ibility issue which should be corrected"
1756 "\n\tby running 'zpool scrub'\n"));
1759 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
1760 (void) printf(gettext(" action: The pool can"
1761 "not be imported with this version of ZFS "
1762 "due to\n\tan active asynchronous destroy. "
1763 "Revert to an earlier version\n\tand "
1764 "allow the destroy to complete before "
1770 * All errata must contain an action message.
1775 (void) printf(gettext(" action: The pool can be "
1776 "imported using its name or numeric "
1779 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1780 (void) printf(gettext(" action: The pool can be imported "
1781 "despite missing or damaged devices. The\n\tfault "
1782 "tolerance of the pool may be compromised if imported.\n"));
1785 case ZPOOL_STATUS_VERSION_NEWER
:
1786 (void) printf(gettext(" action: The pool cannot be "
1787 "imported. Access the pool on a system running "
1788 "newer\n\tsoftware, or recreate the pool from "
1791 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1792 (void) printf(gettext("action: The pool cannot be "
1793 "imported. Access the pool on a system that "
1794 "supports\n\tthe required feature(s), or recreate "
1795 "the pool from backup.\n"));
1797 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1798 (void) printf(gettext("action: The pool cannot be "
1799 "imported in read-write mode. Import the pool "
1801 "\t\"-o readonly=on\", access the pool on a system "
1802 "that supports the\n\trequired feature(s), or "
1803 "recreate the pool from backup.\n"));
1805 case ZPOOL_STATUS_MISSING_DEV_R
:
1806 case ZPOOL_STATUS_MISSING_DEV_NR
:
1807 case ZPOOL_STATUS_BAD_GUID_SUM
:
1808 (void) printf(gettext(" action: The pool cannot be "
1809 "imported. Attach the missing\n\tdevices and try "
1813 (void) printf(gettext(" action: The pool cannot be "
1814 "imported due to damaged devices or data.\n"));
1818 /* Print the comment attached to the pool. */
1819 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1820 (void) printf(gettext("comment: %s\n"), comment
);
1823 * If the state is "closed" or "can't open", and the aux state
1824 * is "corrupt data":
1826 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1827 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1828 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1829 if (pool_state
== POOL_STATE_DESTROYED
)
1830 (void) printf(gettext("\tThe pool was destroyed, "
1831 "but can be imported using the '-Df' flags.\n"));
1832 else if (pool_state
!= POOL_STATE_EXPORTED
)
1833 (void) printf(gettext("\tThe pool may be active on "
1834 "another system, but can be imported using\n\t"
1835 "the '-f' flag.\n"));
1839 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1842 (void) printf(gettext(" config:\n\n"));
1844 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1848 print_import_config(name
, nvroot
, namewidth
, 0);
1849 if (num_logs(nvroot
) > 0)
1850 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1852 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1853 (void) printf(gettext("\n\tAdditional devices are known to "
1854 "be part of this pool, though their\n\texact "
1855 "configuration cannot be determined.\n"));
1860 * Perform the import for the given configuration. This passes the heavy
1861 * lifting off to zpool_import_props(), and then mounts the datasets contained
1865 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1866 nvlist_t
*props
, int flags
)
1868 zpool_handle_t
*zhp
;
1873 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1876 verify(nvlist_lookup_uint64(config
,
1877 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1878 verify(nvlist_lookup_uint64(config
,
1879 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1880 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1881 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1882 "is formatted using an unsupported ZFS version\n"), name
);
1884 } else if (state
!= POOL_STATE_EXPORTED
&&
1885 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1888 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1890 unsigned long system_hostid
= gethostid() & 0xffffffff;
1892 if ((unsigned long)hostid
!= system_hostid
) {
1897 verify(nvlist_lookup_string(config
,
1898 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
1899 verify(nvlist_lookup_uint64(config
,
1900 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
1902 (void) fprintf(stderr
, gettext("cannot import "
1903 "'%s': pool may be in use from other "
1904 "system, it was last accessed by %s "
1905 "(hostid: 0x%lx) on %s"), name
, hostname
,
1906 (unsigned long)hostid
,
1907 asctime(localtime(&t
)));
1908 (void) fprintf(stderr
, gettext("use '-f' to "
1909 "import anyway\n"));
1913 (void) fprintf(stderr
, gettext("cannot import '%s': "
1914 "pool may be in use from other system\n"), name
);
1915 (void) fprintf(stderr
, gettext("use '-f' to import "
1921 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
1924 if (newname
!= NULL
)
1925 name
= (char *)newname
;
1927 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
1930 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
1931 !(flags
& ZFS_IMPORT_ONLY
) &&
1932 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
1942 * zpool import [-d dir] [-D]
1943 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1944 * [-d dir | -c cachefile] [-f] -a
1945 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1946 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1948 * -c Read pool information from a cachefile instead of searching
1951 * -d Scan in a specific directory, other than /dev/. More than
1952 * one directory can be specified using multiple '-d' options.
1954 * -D Scan for previously destroyed pools or import all or only
1955 * specified destroyed pools.
1957 * -R Temporarily import the pool, with all mountpoints relative to
1958 * the given root. The pool will remain exported when the machine
1961 * -V Import even in the presence of faulted vdevs. This is an
1962 * intentionally undocumented option for testing purposes, and
1963 * treats the pool configuration as complete, leaving any bad
1964 * vdevs in the FAULTED state. In other words, it does verbatim
1967 * -f Force import, even if it appears that the pool is active.
1969 * -F Attempt rewind if necessary.
1971 * -n See if rewind would work, but don't actually rewind.
1973 * -N Import the pool but don't mount datasets.
1975 * -T Specify a starting txg to use for import. This option is
1976 * intentionally undocumented option for testing purposes.
1978 * -a Import all pools found.
1980 * -o Set property=value and/or temporary mount options (without '=').
1982 * The import command scans for pools to import, and import pools based on pool
1983 * name and GUID. The pool can also be renamed as part of the import process.
1986 zpool_do_import(int argc
, char **argv
)
1988 char **searchdirs
= NULL
;
1989 char *env
, *envdup
= NULL
;
1993 nvlist_t
*pools
= NULL
;
1994 boolean_t do_all
= B_FALSE
;
1995 boolean_t do_destroyed
= B_FALSE
;
1996 char *mntopts
= NULL
;
1999 uint64_t searchguid
= 0;
2000 char *searchname
= NULL
;
2002 nvlist_t
*found_config
;
2003 nvlist_t
*policy
= NULL
;
2004 nvlist_t
*props
= NULL
;
2006 int flags
= ZFS_IMPORT_NORMAL
;
2007 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2008 boolean_t dryrun
= B_FALSE
;
2009 boolean_t do_rewind
= B_FALSE
;
2010 boolean_t xtreme_rewind
= B_FALSE
;
2011 uint64_t pool_state
, txg
= -1ULL;
2012 char *cachefile
= NULL
;
2013 importargs_t idata
= { 0 };
2017 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2026 if (searchdirs
== NULL
) {
2027 searchdirs
= safe_malloc(sizeof (char *));
2029 char **tmp
= safe_malloc((nsearch
+ 1) *
2031 bcopy(searchdirs
, tmp
, nsearch
*
2036 searchdirs
[nsearch
++] = optarg
;
2039 do_destroyed
= B_TRUE
;
2042 flags
|= ZFS_IMPORT_ANY_HOST
;
2048 flags
|= ZFS_IMPORT_MISSING_LOG
;
2054 flags
|= ZFS_IMPORT_ONLY
;
2057 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2060 if (add_prop_list(optarg
, propval
,
2068 if (add_prop_list(zpool_prop_to_name(
2069 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2071 if (nvlist_lookup_string(props
,
2072 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
2075 if (add_prop_list(zpool_prop_to_name(
2076 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2080 flags
|= ZFS_IMPORT_TEMP_NAME
;
2085 txg
= strtoull(optarg
, &endptr
, 10);
2086 if (errno
!= 0 || *endptr
!= '\0') {
2087 (void) fprintf(stderr
,
2088 gettext("invalid txg value\n"));
2091 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2094 flags
|= ZFS_IMPORT_VERBATIM
;
2097 xtreme_rewind
= B_TRUE
;
2100 (void) fprintf(stderr
, gettext("missing argument for "
2101 "'%c' option\n"), optopt
);
2105 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2114 if (cachefile
&& nsearch
!= 0) {
2115 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2119 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2120 (void) fprintf(stderr
,
2121 gettext("-n or -X only meaningful with -F\n"));
2125 rewind_policy
= ZPOOL_TRY_REWIND
;
2127 rewind_policy
= ZPOOL_DO_REWIND
;
2129 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2131 /* In the future, we can capture further policy and include it here */
2132 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2133 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2134 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2137 /* check argument count */
2140 (void) fprintf(stderr
, gettext("too many arguments\n"));
2145 (void) fprintf(stderr
, gettext("too many arguments\n"));
2150 * Check for the SYS_CONFIG privilege. We do this explicitly
2151 * here because otherwise any attempt to discover pools will
2154 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
2155 (void) fprintf(stderr
, gettext("cannot "
2156 "discover pools: permission denied\n"));
2157 if (searchdirs
!= NULL
)
2160 nvlist_free(policy
);
2166 * Depending on the arguments given, we do one of the following:
2168 * <none> Iterate through all pools and display information about
2171 * -a Iterate through all pools and try to import each one.
2173 * <id> Find the pool that corresponds to the given GUID/pool
2174 * name and import that one.
2176 * -D Above options applies only to destroyed pools.
2182 searchguid
= strtoull(argv
[0], &endptr
, 10);
2183 if (errno
!= 0 || *endptr
!= '\0')
2184 searchname
= argv
[0];
2185 found_config
= NULL
;
2188 * User specified a name or guid. Ensure it's unique.
2190 idata
.unique
= B_TRUE
;
2194 * Check the environment for the preferred search path.
2196 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2199 envdup
= strdup(env
);
2201 dir
= strtok(envdup
, ":");
2202 while (dir
!= NULL
) {
2203 if (searchdirs
== NULL
) {
2204 searchdirs
= safe_malloc(sizeof (char *));
2206 char **tmp
= safe_malloc((nsearch
+ 1) *
2208 bcopy(searchdirs
, tmp
, nsearch
*
2213 searchdirs
[nsearch
++] = dir
;
2214 dir
= strtok(NULL
, ":");
2218 idata
.path
= searchdirs
;
2219 idata
.paths
= nsearch
;
2220 idata
.poolname
= searchname
;
2221 idata
.guid
= searchguid
;
2222 idata
.cachefile
= cachefile
;
2224 pools
= zpool_search_import(g_zfs
, &idata
);
2226 if (pools
!= NULL
&& idata
.exists
&&
2227 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2228 (void) fprintf(stderr
, gettext("cannot import '%s': "
2229 "a pool with that name already exists\n"),
2231 (void) fprintf(stderr
, gettext("use the form '%s "
2232 "<pool | id> <newpool>' to give it a new name\n"),
2235 } else if (pools
== NULL
&& idata
.exists
) {
2236 (void) fprintf(stderr
, gettext("cannot import '%s': "
2237 "a pool with that name is already created/imported,\n"),
2239 (void) fprintf(stderr
, gettext("and no additional pools "
2240 "with that name were found\n"));
2242 } else if (pools
== NULL
) {
2244 (void) fprintf(stderr
, gettext("cannot import '%s': "
2245 "no such pool available\n"), argv
[0]);
2251 if (searchdirs
!= NULL
)
2255 nvlist_free(policy
);
2260 * At this point we have a list of import candidate configs. Even if
2261 * we were searching by pool name or guid, we still need to
2262 * post-process the list to deal with pool state and possible
2268 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2270 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2272 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2274 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2276 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2279 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2286 (void) printf("\n");
2289 err
|= do_import(config
, NULL
, mntopts
,
2292 show_import(config
);
2294 } else if (searchname
!= NULL
) {
2298 * We are searching for a pool based on name.
2300 verify(nvlist_lookup_string(config
,
2301 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2303 if (strcmp(name
, searchname
) == 0) {
2304 if (found_config
!= NULL
) {
2305 (void) fprintf(stderr
, gettext(
2306 "cannot import '%s': more than "
2307 "one matching pool\n"), searchname
);
2308 (void) fprintf(stderr
, gettext(
2309 "import by numeric ID instead\n"));
2312 found_config
= config
;
2318 * Search for a pool by guid.
2320 verify(nvlist_lookup_uint64(config
,
2321 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2323 if (guid
== searchguid
)
2324 found_config
= config
;
2329 * If we were searching for a specific pool, verify that we found a
2330 * pool, and then do the import.
2332 if (argc
!= 0 && err
== 0) {
2333 if (found_config
== NULL
) {
2334 (void) fprintf(stderr
, gettext("cannot import '%s': "
2335 "no such pool available\n"), argv
[0]);
2338 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2339 argv
[1], mntopts
, props
, flags
);
2344 * If we were just looking for pools, report an error if none were
2347 if (argc
== 0 && first
)
2348 (void) fprintf(stderr
,
2349 gettext("no pools available to import\n"));
2354 nvlist_free(policy
);
2355 if (searchdirs
!= NULL
)
2360 return (err
? 1 : 0);
2363 typedef struct iostat_cbdata
{
2364 boolean_t cb_verbose
;
2367 zpool_list_t
*cb_list
;
2371 print_iostat_separator(iostat_cbdata_t
*cb
)
2375 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2377 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2381 print_iostat_header(iostat_cbdata_t
*cb
)
2383 (void) printf("%*s capacity operations bandwidth\n",
2384 cb
->cb_namewidth
, "");
2385 (void) printf("%-*s alloc free read write read write\n",
2386 cb
->cb_namewidth
, "pool");
2387 print_iostat_separator(cb
);
2391 * Display a single statistic.
2394 print_one_stat(uint64_t value
)
2398 zfs_nicenum(value
, buf
, sizeof (buf
));
2399 (void) printf(" %5s", buf
);
2403 * Print out all the statistics for the given vdev. This can either be the
2404 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2405 * is a verbose output, and we don't want to display the toplevel pool stats.
2408 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2409 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2411 nvlist_t
**oldchild
, **newchild
;
2413 vdev_stat_t
*oldvs
, *newvs
;
2414 vdev_stat_t zerovs
= { 0 };
2419 if (oldnv
!= NULL
) {
2420 verify(nvlist_lookup_uint64_array(oldnv
,
2421 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2426 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2427 (uint64_t **)&newvs
, &c
) == 0);
2429 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2430 (void) printf("%*s%s", depth
, "", name
);
2432 (void) printf("%*s%s%*s", depth
, "", name
,
2433 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2435 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2440 scale
= (double)NANOSEC
/ tdelta
;
2442 /* only toplevel vdevs have capacity stats */
2443 if (newvs
->vs_space
== 0) {
2444 (void) printf(" - -");
2446 print_one_stat(newvs
->vs_alloc
);
2447 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2450 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2451 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2453 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2454 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2456 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2457 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2459 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2460 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2462 (void) printf("\n");
2464 if (!cb
->cb_verbose
)
2467 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2468 &newchild
, &children
) != 0)
2471 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2472 &oldchild
, &c
) != 0)
2475 for (c
= 0; c
< children
; c
++) {
2476 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2478 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2481 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2484 if (ishole
|| islog
)
2487 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2488 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2489 newchild
[c
], cb
, depth
+ 2);
2494 * Log device section
2497 if (num_logs(newnv
) > 0) {
2498 (void) printf("%-*s - - - - - "
2499 "-\n", cb
->cb_namewidth
, "logs");
2501 for (c
= 0; c
< children
; c
++) {
2502 uint64_t islog
= B_FALSE
;
2503 (void) nvlist_lookup_uint64(newchild
[c
],
2504 ZPOOL_CONFIG_IS_LOG
, &islog
);
2507 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2509 print_vdev_stats(zhp
, vname
, oldnv
?
2510 oldchild
[c
] : NULL
, newchild
[c
],
2519 * Include level 2 ARC devices in iostat output
2521 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2522 &newchild
, &children
) != 0)
2525 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2526 &oldchild
, &c
) != 0)
2530 (void) printf("%-*s - - - - - "
2531 "-\n", cb
->cb_namewidth
, "cache");
2532 for (c
= 0; c
< children
; c
++) {
2533 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2535 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2536 newchild
[c
], cb
, depth
+ 2);
2543 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2545 iostat_cbdata_t
*cb
= data
;
2549 * If the pool has disappeared, remove it from the list and continue.
2551 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2555 pool_list_remove(cb
->cb_list
, zhp
);
2561 * Callback to print out the iostats for the given pool.
2564 print_iostat(zpool_handle_t
*zhp
, void *data
)
2566 iostat_cbdata_t
*cb
= data
;
2567 nvlist_t
*oldconfig
, *newconfig
;
2568 nvlist_t
*oldnvroot
, *newnvroot
;
2570 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2572 if (cb
->cb_iteration
== 1)
2575 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2578 if (oldconfig
== NULL
)
2581 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2585 * Print out the statistics for the pool.
2587 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2590 print_iostat_separator(cb
);
2602 if (isatty(STDOUT_FILENO
)) {
2603 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
2605 columns
= ws
.ws_col
;
2614 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2616 iostat_cbdata_t
*cb
= data
;
2617 nvlist_t
*config
, *nvroot
;
2620 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2621 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2623 if (!cb
->cb_verbose
)
2624 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2626 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2631 * The width must be at least 10, but may be as large as the
2632 * column width - 42 so that we can still fit in one line.
2634 columns
= get_columns();
2636 if (cb
->cb_namewidth
< 10)
2637 cb
->cb_namewidth
= 10;
2638 if (cb
->cb_namewidth
> columns
- 42)
2639 cb
->cb_namewidth
= columns
- 42;
2645 * Parse the input string, get the 'interval' and 'count' value if there is one.
2648 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2651 unsigned long interval
= 0, count
= 0;
2655 * Determine if the last argument is an integer or a pool name
2657 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2661 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2663 if (*end
== '\0' && errno
== 0) {
2664 if (interval
== 0) {
2665 (void) fprintf(stderr
, gettext("interval "
2666 "cannot be zero\n"));
2670 * Ignore the last parameter
2675 * If this is not a valid number, just plow on. The
2676 * user will get a more informative error message later
2684 * If the last argument is also an integer, then we have both a count
2687 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2692 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2694 if (*end
== '\0' && errno
== 0) {
2695 if (interval
== 0) {
2696 (void) fprintf(stderr
, gettext("interval "
2697 "cannot be zero\n"));
2702 * Ignore the last parameter
2716 get_timestamp_arg(char c
)
2719 timestamp_fmt
= UDATE
;
2721 timestamp_fmt
= DDATE
;
2727 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2729 * -v Display statistics for individual vdevs
2730 * -T Display a timestamp in date(1) or Unix format
2732 * This command can be tricky because we want to be able to deal with pool
2733 * creation/destruction as well as vdev configuration changes. The bulk of this
2734 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2735 * on pool_list_update() to detect the addition of new pools. Configuration
2736 * changes are all handled within libzfs.
2739 zpool_do_iostat(int argc
, char **argv
)
2744 unsigned long interval
= 0, count
= 0;
2746 boolean_t verbose
= B_FALSE
;
2750 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2753 get_timestamp_arg(*optarg
);
2759 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2768 get_interval_count(&argc
, argv
, &interval
, &count
);
2771 * Construct the list of all interesting pools.
2774 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2777 if (pool_list_count(list
) == 0 && argc
!= 0) {
2778 pool_list_free(list
);
2782 if (pool_list_count(list
) == 0 && interval
== 0) {
2783 pool_list_free(list
);
2784 (void) fprintf(stderr
, gettext("no pools available\n"));
2789 * Enter the main iostat loop.
2792 cb
.cb_verbose
= verbose
;
2793 cb
.cb_iteration
= 0;
2794 cb
.cb_namewidth
= 0;
2797 pool_list_update(list
);
2799 if ((npools
= pool_list_count(list
)) == 0)
2800 (void) fprintf(stderr
, gettext("no pools available\n"));
2803 * Refresh all statistics. This is done as an
2804 * explicit step before calculating the maximum name
2805 * width, so that any * configuration changes are
2806 * properly accounted for.
2808 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
2812 * Iterate over all pools to determine the maximum width
2813 * for the pool / device name column across all pools.
2815 cb
.cb_namewidth
= 0;
2816 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
2819 if (timestamp_fmt
!= NODATE
)
2820 print_timestamp(timestamp_fmt
);
2823 * If it's the first time, or verbose mode, print the
2826 if (++cb
.cb_iteration
== 1 || verbose
)
2827 print_iostat_header(&cb
);
2829 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2832 * If there's more than one pool, and we're not in
2833 * verbose mode (which prints a separator for us),
2834 * then print a separator.
2836 if (npools
> 1 && !verbose
)
2837 print_iostat_separator(&cb
);
2840 (void) printf("\n");
2844 * Flush the output so that redirection to a file isn't buffered
2847 (void) fflush(stdout
);
2852 if (count
!= 0 && --count
== 0)
2855 (void) sleep(interval
);
2858 pool_list_free(list
);
2863 typedef struct list_cbdata
{
2864 boolean_t cb_verbose
;
2866 boolean_t cb_scripted
;
2867 zprop_list_t
*cb_proplist
;
2871 * Given a list of columns to display, output appropriate headers for each one.
2874 print_header(list_cbdata_t
*cb
)
2876 zprop_list_t
*pl
= cb
->cb_proplist
;
2877 char headerbuf
[ZPOOL_MAXPROPLEN
];
2879 boolean_t first
= B_TRUE
;
2880 boolean_t right_justify
;
2883 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2884 width
= pl
->pl_width
;
2885 if (first
&& cb
->cb_verbose
) {
2887 * Reset the width to accommodate the verbose listing
2890 width
= cb
->cb_namewidth
;
2898 right_justify
= B_FALSE
;
2899 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2900 header
= zpool_prop_column_name(pl
->pl_prop
);
2901 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2905 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2906 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2907 headerbuf
[i
] = '\0';
2911 if (pl
->pl_next
== NULL
&& !right_justify
)
2912 (void) printf("%s", header
);
2913 else if (right_justify
)
2914 (void) printf("%*s", (int)width
, header
);
2916 (void) printf("%-*s", (int)width
, header
);
2919 (void) printf("\n");
2923 * Given a pool and a list of properties, print out all the properties according
2924 * to the described layout.
2927 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
2929 zprop_list_t
*pl
= cb
->cb_proplist
;
2930 boolean_t first
= B_TRUE
;
2931 char property
[ZPOOL_MAXPROPLEN
];
2933 boolean_t right_justify
;
2936 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2938 width
= pl
->pl_width
;
2939 if (first
&& cb
->cb_verbose
) {
2941 * Reset the width to accommodate the verbose listing
2944 width
= cb
->cb_namewidth
;
2948 if (cb
->cb_scripted
)
2949 (void) printf("\t");
2956 right_justify
= B_FALSE
;
2957 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2958 if (pl
->pl_prop
== ZPOOL_PROP_EXPANDSZ
&&
2959 zpool_get_prop_int(zhp
, pl
->pl_prop
, NULL
) == 0)
2961 else if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
2962 sizeof (property
), NULL
) != 0)
2967 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2968 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
2969 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
2970 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
2971 sizeof (property
)) == 0) {
2979 * If this is being called in scripted mode, or if this is the
2980 * last column and it is left-justified, don't include a width
2983 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
2984 (void) printf("%s", propstr
);
2985 else if (right_justify
)
2986 (void) printf("%*s", (int)width
, propstr
);
2988 (void) printf("%-*s", (int)width
, propstr
);
2991 (void) printf("\n");
2995 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
)
2999 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3001 zfs_nicenum(value
, propval
, sizeof (propval
));
3003 if (prop
== ZPOOL_PROP_EXPANDSZ
&& value
== 0)
3004 (void) strlcpy(propval
, "-", sizeof (propval
));
3007 (void) printf("\t%s", propval
);
3009 (void) printf(" %*s", (int)width
, propval
);
3013 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3014 list_cbdata_t
*cb
, int depth
)
3020 boolean_t scripted
= cb
->cb_scripted
;
3022 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3023 (uint64_t **)&vs
, &c
) == 0);
3027 (void) printf("\t%s", name
);
3028 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3029 (void) printf("%*s%s", depth
, "", name
);
3031 (void) printf("%*s%s%*s", depth
, "", name
,
3032 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3034 /* only toplevel vdevs have capacity stats */
3035 if (vs
->vs_space
== 0) {
3037 (void) printf("\t-\t-\t-");
3039 (void) printf(" - - -");
3041 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
,
3043 print_one_column(ZPOOL_PROP_CAPACITY
, vs
->vs_alloc
,
3045 print_one_column(ZPOOL_PROP_FREE
,
3046 vs
->vs_space
- vs
->vs_alloc
, scripted
);
3048 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
,
3050 (void) printf("\n");
3053 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3054 &child
, &children
) != 0)
3057 for (c
= 0; c
< children
; c
++) {
3058 uint64_t ishole
= B_FALSE
;
3060 if (nvlist_lookup_uint64(child
[c
],
3061 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3064 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3065 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3070 * Include level 2 ARC devices in iostat output
3072 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3073 &child
, &children
) != 0)
3077 (void) printf("%-*s - - - - - "
3078 "-\n", cb
->cb_namewidth
, "cache");
3079 for (c
= 0; c
< children
; c
++) {
3080 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
3082 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3090 * Generic callback function to list a pool.
3093 list_callback(zpool_handle_t
*zhp
, void *data
)
3095 list_cbdata_t
*cbp
= data
;
3099 config
= zpool_get_config(zhp
, NULL
);
3101 print_pool(zhp
, cbp
);
3102 if (!cbp
->cb_verbose
)
3105 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3107 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3113 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3115 * -H Scripted mode. Don't display headers, and separate properties
3117 * -o List of properties to display. Defaults to
3118 * "name,size,allocated,free,capacity,health,altroot"
3119 * -T Display a timestamp in date(1) or Unix format
3121 * List all pools in the system, whether or not they're healthy. Output space
3122 * statistics for each one, as well as health status summary.
3125 zpool_do_list(int argc
, char **argv
)
3129 list_cbdata_t cb
= { 0 };
3130 static char default_props
[] =
3131 "name,size,allocated,free,capacity,dedupratio,"
3133 char *props
= default_props
;
3134 unsigned long interval
= 0, count
= 0;
3136 boolean_t first
= B_TRUE
;
3139 while ((c
= getopt(argc
, argv
, ":Ho:T:v")) != -1) {
3142 cb
.cb_scripted
= B_TRUE
;
3148 get_timestamp_arg(*optarg
);
3151 cb
.cb_verbose
= B_TRUE
;
3154 (void) fprintf(stderr
, gettext("missing argument for "
3155 "'%c' option\n"), optopt
);
3159 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3168 get_interval_count(&argc
, argv
, &interval
, &count
);
3170 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3173 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
, &ret
)) == NULL
)
3176 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3177 (void) printf(gettext("no pools available\n"));
3178 zprop_free_list(cb
.cb_proplist
);
3183 pool_list_update(list
);
3185 if (pool_list_count(list
) == 0)
3188 if (timestamp_fmt
!= NODATE
)
3189 print_timestamp(timestamp_fmt
);
3191 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3195 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3200 if (count
!= 0 && --count
== 0)
3203 (void) sleep(interval
);
3206 zprop_free_list(cb
.cb_proplist
);
3211 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3213 boolean_t force
= B_FALSE
;
3216 char *poolname
, *old_disk
, *new_disk
;
3217 zpool_handle_t
*zhp
;
3218 nvlist_t
*props
= NULL
;
3223 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
3229 if ((propval
= strchr(optarg
, '=')) == NULL
) {
3230 (void) fprintf(stderr
, gettext("missing "
3231 "'=' for -o option\n"));
3237 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
3238 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
3242 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3251 /* get pool name and check number of arguments */
3253 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3260 (void) fprintf(stderr
,
3261 gettext("missing <device> specification\n"));
3269 (void) fprintf(stderr
,
3270 gettext("missing <new_device> specification\n"));
3273 new_disk
= old_disk
;
3283 (void) fprintf(stderr
, gettext("too many arguments\n"));
3287 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3290 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3291 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3297 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
3299 if (nvroot
== NULL
) {
3304 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3306 nvlist_free(nvroot
);
3313 * zpool replace [-f] <pool> <device> <new_device>
3315 * -f Force attach, even if <new_device> appears to be in use.
3317 * Replace <device> with <new_device>.
3321 zpool_do_replace(int argc
, char **argv
)
3323 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3327 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3329 * -f Force attach, even if <new_device> appears to be in use.
3330 * -o Set property=value.
3332 * Attach <new_device> to the mirror containing <device>. If <device> is not
3333 * part of a mirror, then <device> will be transformed into a mirror of
3334 * <device> and <new_device>. In either case, <new_device> will begin life
3335 * with a DTL of [0, now], and will immediately begin to resilver itself.
3338 zpool_do_attach(int argc
, char **argv
)
3340 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3344 * zpool detach [-f] <pool> <device>
3346 * -f Force detach of <device>, even if DTLs argue against it
3347 * (not supported yet)
3349 * Detach a device from a mirror. The operation will be refused if <device>
3350 * is the last device in the mirror, or if the DTLs indicate that this device
3351 * has the only valid copy of some data.
3355 zpool_do_detach(int argc
, char **argv
)
3358 char *poolname
, *path
;
3359 zpool_handle_t
*zhp
;
3363 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3367 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3376 /* get pool name and check number of arguments */
3378 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3383 (void) fprintf(stderr
,
3384 gettext("missing <device> specification\n"));
3391 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3394 ret
= zpool_vdev_detach(zhp
, path
);
3402 * zpool split [-n] [-o prop=val] ...
3404 * [-R altroot] <pool> <newpool> [<device> ...]
3406 * -n Do not split the pool, but display the resulting layout if
3407 * it were to be split.
3408 * -o Set property=value, or set mount options.
3409 * -R Mount the split-off pool under an alternate root.
3411 * Splits the named pool and gives it the new pool name. Devices to be split
3412 * off may be listed, provided that no more than one device is specified
3413 * per top-level vdev mirror. The newly split pool is left in an exported
3414 * state unless -R is specified.
3416 * Restrictions: the top-level of the pool pool must only be made up of
3417 * mirrors; all devices in the pool must be healthy; no device may be
3418 * undergoing a resilvering operation.
3421 zpool_do_split(int argc
, char **argv
)
3423 char *srcpool
, *newpool
, *propval
;
3424 char *mntopts
= NULL
;
3427 zpool_handle_t
*zhp
;
3428 nvlist_t
*config
, *props
= NULL
;
3430 flags
.dryrun
= B_FALSE
;
3431 flags
.import
= B_FALSE
;
3434 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3437 flags
.import
= B_TRUE
;
3439 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3440 &props
, B_TRUE
) != 0) {
3447 flags
.dryrun
= B_TRUE
;
3450 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3453 if (add_prop_list(optarg
, propval
,
3454 &props
, B_TRUE
) != 0) {
3464 (void) fprintf(stderr
, gettext("missing argument for "
3465 "'%c' option\n"), optopt
);
3469 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3476 if (!flags
.import
&& mntopts
!= NULL
) {
3477 (void) fprintf(stderr
, gettext("setting mntopts is only "
3478 "valid when importing the pool\n"));
3486 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3490 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3500 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3503 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3504 if (config
== NULL
) {
3508 (void) printf(gettext("would create '%s' with the "
3509 "following layout:\n\n"), newpool
);
3510 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3512 nvlist_free(config
);
3517 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3521 * The split was successful. Now we need to open the new
3522 * pool and import it.
3524 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3526 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3527 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3529 (void) fprintf(stderr
, gettext("Split was successful, but "
3530 "the datasets could not all be mounted\n"));
3531 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3532 "different altroot\n"), "zpool import");
3542 * zpool online <pool> <device> ...
3545 zpool_do_online(int argc
, char **argv
)
3549 zpool_handle_t
*zhp
;
3551 vdev_state_t newstate
;
3555 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3558 flags
|= ZFS_ONLINE_EXPAND
;
3562 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3571 /* get pool name and check number of arguments */
3573 (void) fprintf(stderr
, gettext("missing pool name\n"));
3577 (void) fprintf(stderr
, gettext("missing device name\n"));
3583 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3586 for (i
= 1; i
< argc
; i
++) {
3587 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3588 if (newstate
!= VDEV_STATE_HEALTHY
) {
3589 (void) printf(gettext("warning: device '%s' "
3590 "onlined, but remains in faulted state\n"),
3592 if (newstate
== VDEV_STATE_FAULTED
)
3593 (void) printf(gettext("use 'zpool "
3594 "clear' to restore a faulted "
3597 (void) printf(gettext("use 'zpool "
3598 "replace' to replace devices "
3599 "that are no longer present\n"));
3612 * zpool offline [-ft] <pool> <device> ...
3614 * -f Force the device into the offline state, even if doing
3615 * so would appear to compromise pool availability.
3616 * (not supported yet)
3618 * -t Only take the device off-line temporarily. The offline
3619 * state will not be persistent across reboots.
3623 zpool_do_offline(int argc
, char **argv
)
3627 zpool_handle_t
*zhp
;
3629 boolean_t istmp
= B_FALSE
;
3632 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3639 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3648 /* get pool name and check number of arguments */
3650 (void) fprintf(stderr
, gettext("missing pool name\n"));
3654 (void) fprintf(stderr
, gettext("missing device name\n"));
3660 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3663 for (i
= 1; i
< argc
; i
++) {
3664 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3674 * zpool clear <pool> [device]
3676 * Clear all errors associated with a pool or a particular device.
3679 zpool_do_clear(int argc
, char **argv
)
3683 boolean_t dryrun
= B_FALSE
;
3684 boolean_t do_rewind
= B_FALSE
;
3685 boolean_t xtreme_rewind
= B_FALSE
;
3686 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3687 nvlist_t
*policy
= NULL
;
3688 zpool_handle_t
*zhp
;
3689 char *pool
, *device
;
3692 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3701 xtreme_rewind
= B_TRUE
;
3704 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3714 (void) fprintf(stderr
, gettext("missing pool name\n"));
3719 (void) fprintf(stderr
, gettext("too many arguments\n"));
3723 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3724 (void) fprintf(stderr
,
3725 gettext("-n or -X only meaningful with -F\n"));
3729 rewind_policy
= ZPOOL_TRY_REWIND
;
3731 rewind_policy
= ZPOOL_DO_REWIND
;
3733 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3735 /* In future, further rewind policy choices can be passed along here */
3736 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3737 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3741 device
= argc
== 2 ? argv
[1] : NULL
;
3743 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3744 nvlist_free(policy
);
3748 if (zpool_clear(zhp
, device
, policy
) != 0)
3753 nvlist_free(policy
);
3759 * zpool reguid <pool>
3762 zpool_do_reguid(int argc
, char **argv
)
3766 zpool_handle_t
*zhp
;
3770 while ((c
= getopt(argc
, argv
, "")) != -1) {
3773 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3782 /* get pool name and check number of arguments */
3784 (void) fprintf(stderr
, gettext("missing pool name\n"));
3789 (void) fprintf(stderr
, gettext("too many arguments\n"));
3794 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3797 ret
= zpool_reguid(zhp
);
3805 * zpool reopen <pool>
3807 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3810 zpool_do_reopen(int argc
, char **argv
)
3814 zpool_handle_t
*zhp
;
3818 while ((c
= getopt(argc
, argv
, "")) != -1) {
3821 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3831 (void) fprintf(stderr
, gettext("missing pool name\n"));
3836 (void) fprintf(stderr
, gettext("too many arguments\n"));
3841 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3844 ret
= zpool_reopen(zhp
);
3849 typedef struct scrub_cbdata
{
3856 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3858 scrub_cbdata_t
*cb
= data
;
3862 * Ignore faulted pools.
3864 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3865 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3866 "currently unavailable\n"), zpool_get_name(zhp
));
3870 err
= zpool_scan(zhp
, cb
->cb_type
);
3876 * zpool scrub [-s] <pool> ...
3878 * -s Stop. Stops any in-progress scrub.
3881 zpool_do_scrub(int argc
, char **argv
)
3886 cb
.cb_type
= POOL_SCAN_SCRUB
;
3889 while ((c
= getopt(argc
, argv
, "s")) != -1) {
3892 cb
.cb_type
= POOL_SCAN_NONE
;
3895 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3907 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3911 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
3914 typedef struct status_cbdata
{
3916 boolean_t cb_allpools
;
3917 boolean_t cb_verbose
;
3918 boolean_t cb_explain
;
3920 boolean_t cb_dedup_stats
;
3924 * Print out detailed scrub status.
3927 print_scan_status(pool_scan_stat_t
*ps
)
3930 uint64_t elapsed
, mins_left
, hours_left
;
3931 uint64_t pass_exam
, examined
, total
;
3933 double fraction_done
;
3934 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
3936 (void) printf(gettext(" scan: "));
3938 /* If there's never been a scan, there's not much to say. */
3939 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
3940 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
3941 (void) printf(gettext("none requested\n"));
3945 start
= ps
->pss_start_time
;
3946 end
= ps
->pss_end_time
;
3947 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
3949 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
3950 ps
->pss_func
== POOL_SCAN_RESILVER
);
3952 * Scan is finished or canceled.
3954 if (ps
->pss_state
== DSS_FINISHED
) {
3955 uint64_t minutes_taken
= (end
- start
) / 60;
3958 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3959 fmt
= gettext("scrub repaired %s in %lluh%um with "
3960 "%llu errors on %s");
3961 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3962 fmt
= gettext("resilvered %s in %lluh%um with "
3963 "%llu errors on %s");
3966 (void) printf(fmt
, processed_buf
,
3967 (u_longlong_t
)(minutes_taken
/ 60),
3968 (uint_t
)(minutes_taken
% 60),
3969 (u_longlong_t
)ps
->pss_errors
,
3970 ctime((time_t *)&end
));
3972 } else if (ps
->pss_state
== DSS_CANCELED
) {
3973 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3974 (void) printf(gettext("scrub canceled on %s"),
3976 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3977 (void) printf(gettext("resilver canceled on %s"),
3983 assert(ps
->pss_state
== DSS_SCANNING
);
3986 * Scan is in progress.
3988 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
3989 (void) printf(gettext("scrub in progress since %s"),
3991 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
3992 (void) printf(gettext("resilver in progress since %s"),
3996 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
3997 total
= ps
->pss_to_examine
;
3998 fraction_done
= (double)examined
/ total
;
4000 /* elapsed time for this pass */
4001 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4002 elapsed
= elapsed
? elapsed
: 1;
4003 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4004 rate
= pass_exam
/ elapsed
;
4005 rate
= rate
? rate
: 1;
4006 mins_left
= ((total
- examined
) / rate
) / 60;
4007 hours_left
= mins_left
/ 60;
4009 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4010 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4011 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4014 * do not print estimated time if hours_left is more than 30 days
4016 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4017 examined_buf
, total_buf
, rate_buf
);
4018 if (hours_left
< (30 * 24)) {
4019 (void) printf(gettext(", %lluh%um to go\n"),
4020 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4022 (void) printf(gettext(
4023 ", (scan is slow, no estimated time)\n"));
4026 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4027 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4028 processed_buf
, 100 * fraction_done
);
4029 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4030 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4031 processed_buf
, 100 * fraction_done
);
4036 print_error_log(zpool_handle_t
*zhp
)
4038 nvlist_t
*nverrlist
= NULL
;
4041 size_t len
= MAXPATHLEN
* 2;
4043 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4044 (void) printf("errors: List of errors unavailable "
4045 "(insufficient privileges)\n");
4049 (void) printf("errors: Permanent errors have been "
4050 "detected in the following files:\n\n");
4052 pathname
= safe_malloc(len
);
4054 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4056 uint64_t dsobj
, obj
;
4058 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4059 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4061 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4063 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4064 (void) printf("%7s %s\n", "", pathname
);
4067 nvlist_free(nverrlist
);
4071 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4080 (void) printf(gettext("\tspares\n"));
4082 for (i
= 0; i
< nspares
; i
++) {
4083 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
4084 print_status_config(zhp
, name
, spares
[i
],
4085 namewidth
, 2, B_TRUE
);
4091 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4100 (void) printf(gettext("\tcache\n"));
4102 for (i
= 0; i
< nl2cache
; i
++) {
4103 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
4104 print_status_config(zhp
, name
, l2cache
[i
],
4105 namewidth
, 2, B_FALSE
);
4111 print_dedup_stats(nvlist_t
*config
)
4113 ddt_histogram_t
*ddh
;
4119 * If the pool was faulted then we may not have been able to
4120 * obtain the config. Otherwise, if we have anything in the dedup
4121 * table continue processing the stats.
4123 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4124 (uint64_t **)&ddo
, &c
) != 0)
4127 (void) printf("\n");
4128 (void) printf(gettext(" dedup: "));
4129 if (ddo
->ddo_count
== 0) {
4130 (void) printf(gettext("no DDT entries\n"));
4134 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4135 (u_longlong_t
)ddo
->ddo_count
,
4136 (u_longlong_t
)ddo
->ddo_dspace
,
4137 (u_longlong_t
)ddo
->ddo_mspace
);
4139 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4140 (uint64_t **)&dds
, &c
) == 0);
4141 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4142 (uint64_t **)&ddh
, &c
) == 0);
4143 zpool_dump_ddt(dds
, ddh
);
4147 * Display a summary of pool status. Displays a summary such as:
4151 * reason: One or more devices ...
4152 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4158 * When given the '-v' option, we print out the complete config. If the '-e'
4159 * option is specified, then we print out error rate information as well.
4162 status_callback(zpool_handle_t
*zhp
, void *data
)
4164 status_cbdata_t
*cbp
= data
;
4165 nvlist_t
*config
, *nvroot
;
4167 zpool_status_t reason
;
4168 zpool_errata_t errata
;
4173 config
= zpool_get_config(zhp
, NULL
);
4174 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
4179 * If we were given 'zpool status -x', only report those pools with
4182 if (cbp
->cb_explain
&&
4183 (reason
== ZPOOL_STATUS_OK
||
4184 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4185 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4186 if (!cbp
->cb_allpools
) {
4187 (void) printf(gettext("pool '%s' is healthy\n"),
4188 zpool_get_name(zhp
));
4190 cbp
->cb_first
= B_FALSE
;
4196 cbp
->cb_first
= B_FALSE
;
4198 (void) printf("\n");
4200 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4202 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4203 (uint64_t **)&vs
, &c
) == 0);
4204 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4206 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4207 (void) printf(gettext(" state: %s\n"), health
);
4210 case ZPOOL_STATUS_MISSING_DEV_R
:
4211 (void) printf(gettext("status: One or more devices could not "
4212 "be opened. Sufficient replicas exist for\n\tthe pool to "
4213 "continue functioning in a degraded state.\n"));
4214 (void) printf(gettext("action: Attach the missing device and "
4215 "online it using 'zpool online'.\n"));
4218 case ZPOOL_STATUS_MISSING_DEV_NR
:
4219 (void) printf(gettext("status: One or more devices could not "
4220 "be opened. There are insufficient\n\treplicas for the "
4221 "pool to continue functioning.\n"));
4222 (void) printf(gettext("action: Attach the missing device and "
4223 "online it using 'zpool online'.\n"));
4226 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4227 (void) printf(gettext("status: One or more devices could not "
4228 "be used because the label is missing or\n\tinvalid. "
4229 "Sufficient replicas exist for the pool to continue\n\t"
4230 "functioning in a degraded state.\n"));
4231 (void) printf(gettext("action: Replace the device using "
4232 "'zpool replace'.\n"));
4235 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4236 (void) printf(gettext("status: One or more devices could not "
4237 "be used because the label is missing \n\tor invalid. "
4238 "There are insufficient replicas for the pool to "
4239 "continue\n\tfunctioning.\n"));
4240 zpool_explain_recover(zpool_get_handle(zhp
),
4241 zpool_get_name(zhp
), reason
, config
);
4244 case ZPOOL_STATUS_FAILING_DEV
:
4245 (void) printf(gettext("status: One or more devices has "
4246 "experienced an unrecoverable error. An\n\tattempt was "
4247 "made to correct the error. Applications are "
4249 (void) printf(gettext("action: Determine if the device needs "
4250 "to be replaced, and clear the errors\n\tusing "
4251 "'zpool clear' or replace the device with 'zpool "
4255 case ZPOOL_STATUS_OFFLINE_DEV
:
4256 (void) printf(gettext("status: One or more devices has "
4257 "been taken offline by the administrator.\n\tSufficient "
4258 "replicas exist for the pool to continue functioning in "
4259 "a\n\tdegraded state.\n"));
4260 (void) printf(gettext("action: Online the device using "
4261 "'zpool online' or replace the device with\n\t'zpool "
4265 case ZPOOL_STATUS_REMOVED_DEV
:
4266 (void) printf(gettext("status: One or more devices has "
4267 "been removed by the administrator.\n\tSufficient "
4268 "replicas exist for the pool to continue functioning in "
4269 "a\n\tdegraded state.\n"));
4270 (void) printf(gettext("action: Online the device using "
4271 "'zpool online' or replace the device with\n\t'zpool "
4275 case ZPOOL_STATUS_RESILVERING
:
4276 (void) printf(gettext("status: One or more devices is "
4277 "currently being resilvered. The pool will\n\tcontinue "
4278 "to function, possibly in a degraded state.\n"));
4279 (void) printf(gettext("action: Wait for the resilver to "
4283 case ZPOOL_STATUS_CORRUPT_DATA
:
4284 (void) printf(gettext("status: One or more devices has "
4285 "experienced an error resulting in data\n\tcorruption. "
4286 "Applications may be affected.\n"));
4287 (void) printf(gettext("action: Restore the file in question "
4288 "if possible. Otherwise restore the\n\tentire pool from "
4292 case ZPOOL_STATUS_CORRUPT_POOL
:
4293 (void) printf(gettext("status: The pool metadata is corrupted "
4294 "and the pool cannot be opened.\n"));
4295 zpool_explain_recover(zpool_get_handle(zhp
),
4296 zpool_get_name(zhp
), reason
, config
);
4299 case ZPOOL_STATUS_VERSION_OLDER
:
4300 (void) printf(gettext("status: The pool is formatted using a "
4301 "legacy on-disk format. The pool can\n\tstill be used, "
4302 "but some features are unavailable.\n"));
4303 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4304 "upgrade'. Once this is done, the\n\tpool will no longer "
4305 "be accessible on software that does not support\n\t"
4306 "feature flags.\n"));
4309 case ZPOOL_STATUS_VERSION_NEWER
:
4310 (void) printf(gettext("status: The pool has been upgraded to a "
4311 "newer, incompatible on-disk version.\n\tThe pool cannot "
4312 "be accessed on this system.\n"));
4313 (void) printf(gettext("action: Access the pool from a system "
4314 "running more recent software, or\n\trestore the pool from "
4318 case ZPOOL_STATUS_FEAT_DISABLED
:
4319 (void) printf(gettext("status: Some supported features are not "
4320 "enabled on the pool. The pool can\n\tstill be used, but "
4321 "some features are unavailable.\n"));
4322 (void) printf(gettext("action: Enable all features using "
4323 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4324 "longer be accessible by software that does not support\n\t"
4325 "the features. See zpool-features(5) for details.\n"));
4328 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4329 (void) printf(gettext("status: The pool cannot be accessed on "
4330 "this system because it uses the\n\tfollowing feature(s) "
4331 "not supported on this system:\n"));
4332 zpool_print_unsup_feat(config
);
4333 (void) printf("\n");
4334 (void) printf(gettext("action: Access the pool from a system "
4335 "that supports the required feature(s),\n\tor restore the "
4336 "pool from backup.\n"));
4339 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4340 (void) printf(gettext("status: The pool can only be accessed "
4341 "in read-only mode on this system. It\n\tcannot be "
4342 "accessed in read-write mode because it uses the "
4343 "following\n\tfeature(s) not supported on this system:\n"));
4344 zpool_print_unsup_feat(config
);
4345 (void) printf("\n");
4346 (void) printf(gettext("action: The pool cannot be accessed in "
4347 "read-write mode. Import the pool with\n"
4348 "\t\"-o readonly=on\", access the pool from a system that "
4349 "supports the\n\trequired feature(s), or restore the "
4350 "pool from backup.\n"));
4353 case ZPOOL_STATUS_FAULTED_DEV_R
:
4354 (void) printf(gettext("status: One or more devices are "
4355 "faulted in response to persistent errors.\n\tSufficient "
4356 "replicas exist for the pool to continue functioning "
4357 "in a\n\tdegraded state.\n"));
4358 (void) printf(gettext("action: Replace the faulted device, "
4359 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4362 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4363 (void) printf(gettext("status: One or more devices are "
4364 "faulted in response to persistent errors. There are "
4365 "insufficient replicas for the pool to\n\tcontinue "
4367 (void) printf(gettext("action: Destroy and re-create the pool "
4368 "from a backup source. Manually marking the device\n"
4369 "\trepaired using 'zpool clear' may allow some data "
4370 "to be recovered.\n"));
4373 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4374 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4375 (void) printf(gettext("status: One or more devices are "
4376 "faulted in response to IO failures.\n"));
4377 (void) printf(gettext("action: Make sure the affected devices "
4378 "are connected, then run 'zpool clear'.\n"));
4381 case ZPOOL_STATUS_BAD_LOG
:
4382 (void) printf(gettext("status: An intent log record "
4383 "could not be read.\n"
4384 "\tWaiting for adminstrator intervention to fix the "
4385 "faulted pool.\n"));
4386 (void) printf(gettext("action: Either restore the affected "
4387 "device(s) and run 'zpool online',\n"
4388 "\tor ignore the intent log records by running "
4389 "'zpool clear'.\n"));
4392 case ZPOOL_STATUS_HOSTID_MISMATCH
:
4393 (void) printf(gettext("status: Mismatch between pool hostid "
4394 "and system hostid on imported pool.\n\tThis pool was "
4395 "previously imported into a system with a different "
4396 "hostid,\n\tand then was verbatim imported into this "
4398 (void) printf(gettext("action: Export this pool on all systems "
4399 "on which it is imported.\n"
4400 "\tThen import it to correct the mismatch.\n"));
4403 case ZPOOL_STATUS_ERRATA
:
4404 (void) printf(gettext("status: Errata #%d detected.\n"),
4408 case ZPOOL_ERRATA_NONE
:
4411 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
4412 (void) printf(gettext("action: To correct the issue "
4413 "run 'zpool scrub'.\n"));
4418 * All errata which allow the pool to be imported
4419 * must contain an action message.
4427 * The remaining errors can't actually be generated, yet.
4429 assert(reason
== ZPOOL_STATUS_OK
);
4433 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4436 if (config
!= NULL
) {
4439 nvlist_t
**spares
, **l2cache
;
4440 uint_t nspares
, nl2cache
;
4441 pool_scan_stat_t
*ps
= NULL
;
4443 (void) nvlist_lookup_uint64_array(nvroot
,
4444 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4445 print_scan_status(ps
);
4447 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4451 (void) printf(gettext("config:\n\n"));
4452 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4453 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4454 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4455 namewidth
, 0, B_FALSE
);
4457 if (num_logs(nvroot
) > 0)
4458 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4459 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4460 &l2cache
, &nl2cache
) == 0)
4461 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4463 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4464 &spares
, &nspares
) == 0)
4465 print_spares(zhp
, spares
, nspares
, namewidth
);
4467 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4469 nvlist_t
*nverrlist
= NULL
;
4472 * If the approximate error count is small, get a
4473 * precise count by fetching the entire log and
4474 * uniquifying the results.
4476 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4477 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4482 while ((elem
= nvlist_next_nvpair(nverrlist
,
4487 nvlist_free(nverrlist
);
4489 (void) printf("\n");
4492 (void) printf(gettext("errors: No known data "
4494 else if (!cbp
->cb_verbose
)
4495 (void) printf(gettext("errors: %llu data "
4496 "errors, use '-v' for a list\n"),
4497 (u_longlong_t
)nerr
);
4499 print_error_log(zhp
);
4502 if (cbp
->cb_dedup_stats
)
4503 print_dedup_stats(config
);
4505 (void) printf(gettext("config: The configuration cannot be "
4513 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4515 * -v Display complete error logs
4516 * -x Display only pools with potential problems
4517 * -D Display dedup status (undocumented)
4518 * -T Display a timestamp in date(1) or Unix format
4520 * Describes the health status of all pools or some subset.
4523 zpool_do_status(int argc
, char **argv
)
4527 unsigned long interval
= 0, count
= 0;
4528 status_cbdata_t cb
= { 0 };
4531 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4534 cb
.cb_verbose
= B_TRUE
;
4537 cb
.cb_explain
= B_TRUE
;
4540 cb
.cb_dedup_stats
= B_TRUE
;
4543 get_timestamp_arg(*optarg
);
4546 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4555 get_interval_count(&argc
, argv
, &interval
, &count
);
4558 cb
.cb_allpools
= B_TRUE
;
4560 cb
.cb_first
= B_TRUE
;
4563 if (timestamp_fmt
!= NODATE
)
4564 print_timestamp(timestamp_fmt
);
4566 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4567 status_callback
, &cb
);
4569 if (argc
== 0 && cb
.cb_count
== 0)
4570 (void) fprintf(stderr
, gettext("no pools available\n"));
4571 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4572 (void) printf(gettext("all pools are healthy\n"));
4580 if (count
!= 0 && --count
== 0)
4583 (void) sleep(interval
);
4589 typedef struct upgrade_cbdata
{
4592 uint64_t cb_version
;
4597 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4601 uint64_t oldversion
;
4603 config
= zpool_get_config(zhp
, NULL
);
4604 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4607 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4608 assert(oldversion
< version
);
4610 ret
= zpool_upgrade(zhp
, version
);
4614 if (version
>= SPA_VERSION_FEATURES
) {
4615 (void) printf(gettext("Successfully upgraded "
4616 "'%s' from version %llu to feature flags.\n"),
4617 zpool_get_name(zhp
), (u_longlong_t
) oldversion
);
4619 (void) printf(gettext("Successfully upgraded "
4620 "'%s' from version %llu to version %llu.\n"),
4621 zpool_get_name(zhp
), (u_longlong_t
) oldversion
,
4622 (u_longlong_t
) version
);
4629 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4632 boolean_t firstff
= B_TRUE
;
4633 nvlist_t
*enabled
= zpool_get_features(zhp
);
4636 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4637 const char *fname
= spa_feature_table
[i
].fi_uname
;
4638 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4639 if (!nvlist_exists(enabled
, fguid
)) {
4641 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4642 ret
= zpool_set_prop(zhp
, propname
,
4643 ZFS_FEATURE_ENABLED
);
4651 (void) printf(gettext("Enabled the "
4652 "following features on '%s':\n"),
4653 zpool_get_name(zhp
));
4656 (void) printf(gettext(" %s\n"), fname
);
4667 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4669 upgrade_cbdata_t
*cbp
= arg
;
4672 boolean_t printnl
= B_FALSE
;
4675 config
= zpool_get_config(zhp
, NULL
);
4676 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4679 assert(SPA_VERSION_IS_SUPPORTED(version
));
4681 if (version
< cbp
->cb_version
) {
4682 cbp
->cb_first
= B_FALSE
;
4683 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4689 * If they did "zpool upgrade -a", then we could
4690 * be doing ioctls to different pools. We need
4691 * to log this history once to each pool, and bypass
4692 * the normal history logging that happens in main().
4694 (void) zpool_log_history(g_zfs
, history_str
);
4695 log_history
= B_FALSE
;
4698 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4700 ret
= upgrade_enable_all(zhp
, &count
);
4705 cbp
->cb_first
= B_FALSE
;
4711 (void) printf(gettext("\n"));
4718 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4720 upgrade_cbdata_t
*cbp
= arg
;
4724 config
= zpool_get_config(zhp
, NULL
);
4725 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4728 assert(SPA_VERSION_IS_SUPPORTED(version
));
4730 if (version
< SPA_VERSION_FEATURES
) {
4731 if (cbp
->cb_first
) {
4732 (void) printf(gettext("The following pools are "
4733 "formatted with legacy version numbers and can\n"
4734 "be upgraded to use feature flags. After "
4735 "being upgraded, these pools\nwill no "
4736 "longer be accessible by software that does not "
4737 "support feature\nflags.\n\n"));
4738 (void) printf(gettext("VER POOL\n"));
4739 (void) printf(gettext("--- ------------\n"));
4740 cbp
->cb_first
= B_FALSE
;
4743 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4744 zpool_get_name(zhp
));
4751 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4753 upgrade_cbdata_t
*cbp
= arg
;
4757 config
= zpool_get_config(zhp
, NULL
);
4758 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4761 if (version
>= SPA_VERSION_FEATURES
) {
4763 boolean_t poolfirst
= B_TRUE
;
4764 nvlist_t
*enabled
= zpool_get_features(zhp
);
4766 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4767 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4768 const char *fname
= spa_feature_table
[i
].fi_uname
;
4769 if (!nvlist_exists(enabled
, fguid
)) {
4770 if (cbp
->cb_first
) {
4771 (void) printf(gettext("\nSome "
4772 "supported features are not "
4773 "enabled on the following pools. "
4774 "Once a\nfeature is enabled the "
4775 "pool may become incompatible with "
4776 "software\nthat does not support "
4778 "zpool-features(5) for "
4780 (void) printf(gettext("POOL "
4782 (void) printf(gettext("------"
4784 cbp
->cb_first
= B_FALSE
;
4788 (void) printf(gettext("%s\n"),
4789 zpool_get_name(zhp
));
4790 poolfirst
= B_FALSE
;
4793 (void) printf(gettext(" %s\n"), fname
);
4796 * If they did "zpool upgrade -a", then we could
4797 * be doing ioctls to different pools. We need
4798 * to log this history once to each pool, and bypass
4799 * the normal history logging that happens in main().
4801 (void) zpool_log_history(g_zfs
, history_str
);
4802 log_history
= B_FALSE
;
4811 upgrade_one(zpool_handle_t
*zhp
, void *data
)
4813 boolean_t printnl
= B_FALSE
;
4814 upgrade_cbdata_t
*cbp
= data
;
4815 uint64_t cur_version
;
4818 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
4819 (void) printf(gettext("'log' is now a reserved word\n"
4820 "Pool 'log' must be renamed using export and import"
4825 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4826 if (cur_version
> cbp
->cb_version
) {
4827 (void) printf(gettext("Pool '%s' is already formatted "
4828 "using more current version '%llu'.\n\n"),
4829 zpool_get_name(zhp
), (u_longlong_t
) cur_version
);
4833 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
4834 (void) printf(gettext("Pool '%s' is already formatted "
4835 "using version %llu.\n\n"), zpool_get_name(zhp
),
4836 (u_longlong_t
) cbp
->cb_version
);
4840 if (cur_version
!= cbp
->cb_version
) {
4842 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4847 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4849 ret
= upgrade_enable_all(zhp
, &count
);
4855 } else if (cur_version
== SPA_VERSION
) {
4856 (void) printf(gettext("Pool '%s' already has all "
4857 "supported features enabled.\n"),
4858 zpool_get_name(zhp
));
4863 (void) printf(gettext("\n"));
4872 * zpool upgrade [-V version] <-a | pool ...>
4874 * With no arguments, display downrev'd ZFS pool available for upgrade.
4875 * Individual pools can be upgraded by specifying the pool, and '-a' will
4876 * upgrade all pools.
4879 zpool_do_upgrade(int argc
, char **argv
)
4882 upgrade_cbdata_t cb
= { 0 };
4884 boolean_t showversions
= B_FALSE
;
4885 boolean_t upgradeall
= B_FALSE
;
4890 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
4893 upgradeall
= B_TRUE
;
4896 showversions
= B_TRUE
;
4899 cb
.cb_version
= strtoll(optarg
, &end
, 10);
4901 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
4902 (void) fprintf(stderr
,
4903 gettext("invalid version '%s'\n"), optarg
);
4908 (void) fprintf(stderr
, gettext("missing argument for "
4909 "'%c' option\n"), optopt
);
4913 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4924 if (cb
.cb_version
== 0) {
4925 cb
.cb_version
= SPA_VERSION
;
4926 } else if (!upgradeall
&& argc
== 0) {
4927 (void) fprintf(stderr
, gettext("-V option is "
4928 "incompatible with other arguments\n"));
4933 if (upgradeall
|| argc
!= 0) {
4934 (void) fprintf(stderr
, gettext("-v option is "
4935 "incompatible with other arguments\n"));
4938 } else if (upgradeall
) {
4940 (void) fprintf(stderr
, gettext("-a option should not "
4941 "be used along with a pool name\n"));
4946 (void) printf(gettext("This system supports ZFS pool feature "
4951 (void) printf(gettext("The following features are "
4953 (void) printf(gettext("FEAT DESCRIPTION\n"));
4954 (void) printf("----------------------------------------------"
4955 "---------------\n");
4956 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4957 zfeature_info_t
*fi
= &spa_feature_table
[i
];
4958 const char *ro
= fi
->fi_can_readonly
?
4959 " (read-only compatible)" : "";
4961 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
4962 (void) printf(" %s\n", fi
->fi_desc
);
4964 (void) printf("\n");
4966 (void) printf(gettext("The following legacy versions are also "
4968 (void) printf(gettext("VER DESCRIPTION\n"));
4969 (void) printf("--- -----------------------------------------"
4970 "---------------\n");
4971 (void) printf(gettext(" 1 Initial ZFS version\n"));
4972 (void) printf(gettext(" 2 Ditto blocks "
4973 "(replicated metadata)\n"));
4974 (void) printf(gettext(" 3 Hot spares and double parity "
4976 (void) printf(gettext(" 4 zpool history\n"));
4977 (void) printf(gettext(" 5 Compression using the gzip "
4979 (void) printf(gettext(" 6 bootfs pool property\n"));
4980 (void) printf(gettext(" 7 Separate intent log devices\n"));
4981 (void) printf(gettext(" 8 Delegated administration\n"));
4982 (void) printf(gettext(" 9 refquota and refreservation "
4984 (void) printf(gettext(" 10 Cache devices\n"));
4985 (void) printf(gettext(" 11 Improved scrub performance\n"));
4986 (void) printf(gettext(" 12 Snapshot properties\n"));
4987 (void) printf(gettext(" 13 snapused property\n"));
4988 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4989 (void) printf(gettext(" 15 user/group space accounting\n"));
4990 (void) printf(gettext(" 16 stmf property support\n"));
4991 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4992 (void) printf(gettext(" 18 Snapshot user holds\n"));
4993 (void) printf(gettext(" 19 Log device removal\n"));
4994 (void) printf(gettext(" 20 Compression using zle "
4995 "(zero-length encoding)\n"));
4996 (void) printf(gettext(" 21 Deduplication\n"));
4997 (void) printf(gettext(" 22 Received properties\n"));
4998 (void) printf(gettext(" 23 Slim ZIL\n"));
4999 (void) printf(gettext(" 24 System attributes\n"));
5000 (void) printf(gettext(" 25 Improved scrub stats\n"));
5001 (void) printf(gettext(" 26 Improved snapshot deletion "
5003 (void) printf(gettext(" 27 Improved snapshot creation "
5005 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5006 (void) printf(gettext("\nFor more information on a particular "
5007 "version, including supported releases,\n"));
5008 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5009 } else if (argc
== 0 && upgradeall
) {
5010 cb
.cb_first
= B_TRUE
;
5011 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5012 if (ret
== 0 && cb
.cb_first
) {
5013 if (cb
.cb_version
== SPA_VERSION
) {
5014 (void) printf(gettext("All pools are already "
5015 "formatted using feature flags.\n\n"));
5016 (void) printf(gettext("Every feature flags "
5017 "pool already has all supported features "
5020 (void) printf(gettext("All pools are already "
5021 "formatted with version %llu or higher.\n"),
5022 (u_longlong_t
) cb
.cb_version
);
5025 } else if (argc
== 0) {
5026 cb
.cb_first
= B_TRUE
;
5027 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5031 (void) printf(gettext("All pools are formatted "
5032 "using feature flags.\n\n"));
5034 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5035 "for a list of available legacy versions.\n"));
5038 cb
.cb_first
= B_TRUE
;
5039 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5043 (void) printf(gettext("Every feature flags pool has "
5044 "all supported features enabled.\n"));
5046 (void) printf(gettext("\n"));
5049 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5056 typedef struct hist_cbdata
{
5063 * Print out the command history for a specific pool.
5066 get_history_one(zpool_handle_t
*zhp
, void *data
)
5072 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5074 cb
->first
= B_FALSE
;
5076 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5078 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5081 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5082 &records
, &numrecords
) == 0);
5083 for (i
= 0; i
< numrecords
; i
++) {
5084 nvlist_t
*rec
= records
[i
];
5087 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5091 tsec
= fnvlist_lookup_uint64(records
[i
],
5093 (void) localtime_r(&tsec
, &t
);
5094 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5097 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5098 (void) printf("%s %s", tbuf
,
5099 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5100 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5102 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5105 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5106 (void) printf("%s unrecognized record:\n",
5108 dump_nvlist(rec
, 4);
5111 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5112 zfs_history_event_names
[ievent
],
5113 (longlong_t
) fnvlist_lookup_uint64(
5114 rec
, ZPOOL_HIST_TXG
),
5115 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5116 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5119 (void) printf("%s [txg:%lld] %s", tbuf
,
5120 (longlong_t
) fnvlist_lookup_uint64(
5121 rec
, ZPOOL_HIST_TXG
),
5122 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5123 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5124 (void) printf(" %s (%llu)",
5125 fnvlist_lookup_string(rec
,
5127 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
5130 (void) printf(" %s", fnvlist_lookup_string(rec
,
5131 ZPOOL_HIST_INT_STR
));
5132 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5135 (void) printf("%s ioctl %s\n", tbuf
,
5136 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5137 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5138 (void) printf(" input:\n");
5139 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5140 ZPOOL_HIST_INPUT_NVL
), 8);
5142 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5143 (void) printf(" output:\n");
5144 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5145 ZPOOL_HIST_OUTPUT_NVL
), 8);
5150 (void) printf("%s unrecognized record:\n", tbuf
);
5151 dump_nvlist(rec
, 4);
5155 (void) printf("\n");
5158 (void) printf(" [");
5159 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5160 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5161 struct passwd
*pwd
= getpwuid(who
);
5162 (void) printf("user %d ", (int)who
);
5164 (void) printf("(%s) ", pwd
->pw_name
);
5166 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5167 (void) printf("on %s",
5168 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5170 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5171 (void) printf(":%s",
5172 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5176 (void) printf("\n");
5178 (void) printf("\n");
5185 * zpool history <pool>
5187 * Displays the history of commands that modified pools.
5190 zpool_do_history(int argc
, char **argv
)
5192 hist_cbdata_t cbdata
= { 0 };
5196 cbdata
.first
= B_TRUE
;
5198 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5201 cbdata
.longfmt
= B_TRUE
;
5204 cbdata
.internal
= B_TRUE
;
5207 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5215 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5218 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5219 (void) fprintf(stderr
, gettext("no pools available\n"));
5226 typedef struct ev_opts
{
5234 zpool_do_events_short(nvlist_t
*nvl
)
5236 char ctime_str
[26], str
[32], *ptr
;
5240 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
5241 memset(str
, ' ', 32);
5242 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
5243 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
5244 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
5245 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
5246 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
5247 (void) printf(gettext("%s "), str
);
5249 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
5250 (void) printf(gettext("%s\n"), ptr
);
5254 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
5258 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
5259 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
5261 data_type_t type
= nvpair_type(nvp
);
5262 const char *name
= nvpair_name(nvp
);
5272 printf(gettext("%*s%s = "), depth
, "", name
);
5275 case DATA_TYPE_BOOLEAN
:
5276 printf(gettext("%s"), "1");
5279 case DATA_TYPE_BOOLEAN_VALUE
:
5280 (void) nvpair_value_boolean_value(nvp
, &b
);
5281 printf(gettext("%s"), b
? "1" : "0");
5284 case DATA_TYPE_BYTE
:
5285 (void) nvpair_value_byte(nvp
, &i8
);
5286 printf(gettext("0x%x"), i8
);
5289 case DATA_TYPE_INT8
:
5290 (void) nvpair_value_int8(nvp
, (void *)&i8
);
5291 printf(gettext("0x%x"), i8
);
5294 case DATA_TYPE_UINT8
:
5295 (void) nvpair_value_uint8(nvp
, &i8
);
5296 printf(gettext("0x%x"), i8
);
5299 case DATA_TYPE_INT16
:
5300 (void) nvpair_value_int16(nvp
, (void *)&i16
);
5301 printf(gettext("0x%x"), i16
);
5304 case DATA_TYPE_UINT16
:
5305 (void) nvpair_value_uint16(nvp
, &i16
);
5306 printf(gettext("0x%x"), i16
);
5309 case DATA_TYPE_INT32
:
5310 (void) nvpair_value_int32(nvp
, (void *)&i32
);
5311 printf(gettext("0x%x"), i32
);
5314 case DATA_TYPE_UINT32
:
5315 (void) nvpair_value_uint32(nvp
, &i32
);
5316 printf(gettext("0x%x"), i32
);
5319 case DATA_TYPE_INT64
:
5320 (void) nvpair_value_int64(nvp
, (void *)&i64
);
5321 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5324 case DATA_TYPE_UINT64
:
5325 (void) nvpair_value_uint64(nvp
, &i64
);
5326 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5329 case DATA_TYPE_HRTIME
:
5330 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
5331 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
5334 case DATA_TYPE_STRING
:
5335 (void) nvpair_value_string(nvp
, &str
);
5336 printf(gettext("\"%s\""), str
? str
: "<NULL>");
5339 case DATA_TYPE_NVLIST
:
5340 printf(gettext("(embedded nvlist)\n"));
5341 (void) nvpair_value_nvlist(nvp
, &cnv
);
5342 zpool_do_events_nvprint(cnv
, depth
+ 8);
5343 printf(gettext("%*s(end %s)"), depth
, "", name
);
5346 case DATA_TYPE_NVLIST_ARRAY
: {
5350 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
5351 printf(gettext("(%d embedded nvlists)\n"), nelem
);
5352 for (i
= 0; i
< nelem
; i
++) {
5353 printf(gettext("%*s%s[%d] = %s\n"),
5354 depth
, "", name
, i
, "(embedded nvlist)");
5355 zpool_do_events_nvprint(val
[i
], depth
+ 8);
5356 printf(gettext("%*s(end %s[%i])\n"),
5357 depth
, "", name
, i
);
5359 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
5363 case DATA_TYPE_INT8_ARRAY
: {
5367 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
5368 for (i
= 0; i
< nelem
; i
++)
5369 printf(gettext("0x%x "), val
[i
]);
5374 case DATA_TYPE_UINT8_ARRAY
: {
5378 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
5379 for (i
= 0; i
< nelem
; i
++)
5380 printf(gettext("0x%x "), val
[i
]);
5385 case DATA_TYPE_INT16_ARRAY
: {
5389 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
5390 for (i
= 0; i
< nelem
; i
++)
5391 printf(gettext("0x%x "), val
[i
]);
5396 case DATA_TYPE_UINT16_ARRAY
: {
5400 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
5401 for (i
= 0; i
< nelem
; i
++)
5402 printf(gettext("0x%x "), val
[i
]);
5407 case DATA_TYPE_INT32_ARRAY
: {
5411 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
5412 for (i
= 0; i
< nelem
; i
++)
5413 printf(gettext("0x%x "), val
[i
]);
5418 case DATA_TYPE_UINT32_ARRAY
: {
5422 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
5423 for (i
= 0; i
< nelem
; i
++)
5424 printf(gettext("0x%x "), val
[i
]);
5429 case DATA_TYPE_INT64_ARRAY
: {
5433 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
5434 for (i
= 0; i
< nelem
; i
++)
5435 printf(gettext("0x%llx "),
5436 (u_longlong_t
)val
[i
]);
5441 case DATA_TYPE_UINT64_ARRAY
: {
5445 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
5446 for (i
= 0; i
< nelem
; i
++)
5447 printf(gettext("0x%llx "),
5448 (u_longlong_t
)val
[i
]);
5453 case DATA_TYPE_STRING_ARRAY
: {
5457 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
5458 for (i
= 0; i
< nelem
; i
++)
5459 printf(gettext("\"%s\" "),
5460 str
[i
] ? str
[i
] : "<NULL>");
5465 case DATA_TYPE_BOOLEAN_ARRAY
:
5466 case DATA_TYPE_BYTE_ARRAY
:
5467 case DATA_TYPE_DOUBLE
:
5468 case DATA_TYPE_UNKNOWN
:
5469 printf(gettext("<unknown>"));
5473 printf(gettext("\n"));
5478 zpool_do_events_next(ev_opts_t
*opts
)
5481 int zevent_fd
, ret
, dropped
;
5483 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
5484 VERIFY(zevent_fd
>= 0);
5486 if (!opts
->scripted
)
5487 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5490 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
5491 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
5492 if (ret
|| nvl
== NULL
)
5496 (void) printf(gettext("dropped %d events\n"), dropped
);
5498 zpool_do_events_short(nvl
);
5500 if (opts
->verbose
) {
5501 zpool_do_events_nvprint(nvl
, 8);
5502 printf(gettext("\n"));
5504 (void) fflush(stdout
);
5509 VERIFY(0 == close(zevent_fd
));
5515 zpool_do_events_clear(ev_opts_t
*opts
)
5519 ret
= zpool_events_clear(g_zfs
, &count
);
5521 (void) printf(gettext("cleared %d events\n"), count
);
5527 * zpool events [-vfc]
5529 * Displays events logs by ZFS.
5532 zpool_do_events(int argc
, char **argv
)
5534 ev_opts_t opts
= { 0 };
5539 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
5554 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5563 ret
= zpool_do_events_clear(&opts
);
5565 ret
= zpool_do_events_next(&opts
);
5571 get_callback(zpool_handle_t
*zhp
, void *data
)
5573 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5574 char value
[MAXNAMELEN
];
5575 zprop_source_t srctype
;
5578 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5581 * Skip the special fake placeholder. This will also skip
5582 * over the name property when 'all' is specified.
5584 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5585 pl
== cbp
->cb_proplist
)
5588 if (pl
->pl_prop
== ZPROP_INVAL
&&
5589 (zpool_prop_feature(pl
->pl_user_prop
) ||
5590 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5591 srctype
= ZPROP_SRC_LOCAL
;
5593 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5594 value
, sizeof (value
)) == 0) {
5595 zprop_print_one_property(zpool_get_name(zhp
),
5596 cbp
, pl
->pl_user_prop
, value
, srctype
,
5600 if (zpool_get_prop_literal(zhp
, pl
->pl_prop
, value
,
5601 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5604 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5605 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5613 zpool_do_get(int argc
, char **argv
)
5615 zprop_get_cbdata_t cb
= { 0 };
5616 zprop_list_t fake_name
= { 0 };
5620 while ((c
= getopt(argc
, argv
, "pH")) != -1) {
5623 cb
.cb_literal
= B_TRUE
;
5627 cb
.cb_scripted
= B_TRUE
;
5631 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5641 (void) fprintf(stderr
, gettext("missing property "
5646 cb
.cb_first
= B_TRUE
;
5647 cb
.cb_sources
= ZPROP_SRC_ALL
;
5648 cb
.cb_columns
[0] = GET_COL_NAME
;
5649 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5650 cb
.cb_columns
[2] = GET_COL_VALUE
;
5651 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5652 cb
.cb_type
= ZFS_TYPE_POOL
;
5654 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5660 if (cb
.cb_proplist
!= NULL
) {
5661 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5662 fake_name
.pl_width
= strlen(gettext("NAME"));
5663 fake_name
.pl_next
= cb
.cb_proplist
;
5664 cb
.cb_proplist
= &fake_name
;
5667 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5670 if (cb
.cb_proplist
== &fake_name
)
5671 zprop_free_list(fake_name
.pl_next
);
5673 zprop_free_list(cb
.cb_proplist
);
5678 typedef struct set_cbdata
{
5681 boolean_t cb_any_successful
;
5685 set_callback(zpool_handle_t
*zhp
, void *data
)
5688 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5690 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5693 cb
->cb_any_successful
= B_TRUE
;
5699 zpool_do_set(int argc
, char **argv
)
5701 set_cbdata_t cb
= { 0 };
5704 if (argc
> 1 && argv
[1][0] == '-') {
5705 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5711 (void) fprintf(stderr
, gettext("missing property=value "
5717 (void) fprintf(stderr
, gettext("missing pool name\n"));
5722 (void) fprintf(stderr
, gettext("too many pool names\n"));
5726 cb
.cb_propname
= argv
[1];
5727 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5728 if (cb
.cb_value
== NULL
) {
5729 (void) fprintf(stderr
, gettext("missing value in "
5730 "property=value argument\n"));
5734 *(cb
.cb_value
) = '\0';
5737 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5744 find_command_idx(char *command
, int *idx
)
5748 for (i
= 0; i
< NCOMMAND
; i
++) {
5749 if (command_table
[i
].name
== NULL
)
5752 if (strcmp(command
, command_table
[i
].name
) == 0) {
5761 main(int argc
, char **argv
)
5767 (void) setlocale(LC_ALL
, "");
5768 (void) textdomain(TEXT_DOMAIN
);
5773 * Make sure the user has specified some command.
5776 (void) fprintf(stderr
, gettext("missing command\n"));
5785 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
5788 if ((g_zfs
= libzfs_init()) == NULL
)
5791 libzfs_print_on_error(g_zfs
, B_TRUE
);
5793 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5796 * Run the appropriate command.
5798 if (find_command_idx(cmdname
, &i
) == 0) {
5799 current_command
= &command_table
[i
];
5800 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5801 } else if (strchr(cmdname
, '=')) {
5802 verify(find_command_idx("set", &i
) == 0);
5803 current_command
= &command_table
[i
];
5804 ret
= command_table
[i
].func(argc
, argv
);
5805 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5807 * 'freeze' is a vile debugging abomination, so we treat
5811 int fd
= open(ZFS_DEV
, O_RDWR
);
5812 (void) strcpy((void *)buf
, argv
[2]);
5813 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5815 (void) fprintf(stderr
, gettext("unrecognized "
5816 "command '%s'\n"), cmdname
);
5821 if (ret
== 0 && log_history
)
5822 (void) zpool_log_history(g_zfs
, history_str
);
5827 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5828 * for the purposes of running ::findleaks.
5830 if (getenv("ZFS_ABORT") != NULL
) {
5831 (void) printf("dumping core by request\n");