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 (c) 2011, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
26 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
28 * Copyright 2016 Nexenta Systems, Inc.
29 * Copyright (c) 2017 Datto Inc.
50 #include <sys/fs/zfs.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 #include "statcommon.h"
61 static int zpool_do_create(int, char **);
62 static int zpool_do_destroy(int, char **);
64 static int zpool_do_add(int, char **);
65 static int zpool_do_remove(int, char **);
66 static int zpool_do_labelclear(int, char **);
68 static int zpool_do_list(int, char **);
69 static int zpool_do_iostat(int, char **);
70 static int zpool_do_status(int, char **);
72 static int zpool_do_online(int, char **);
73 static int zpool_do_offline(int, char **);
74 static int zpool_do_clear(int, char **);
75 static int zpool_do_reopen(int, char **);
77 static int zpool_do_reguid(int, char **);
79 static int zpool_do_attach(int, char **);
80 static int zpool_do_detach(int, char **);
81 static int zpool_do_replace(int, char **);
82 static int zpool_do_split(int, char **);
84 static int zpool_do_scrub(int, char **);
86 static int zpool_do_import(int, char **);
87 static int zpool_do_export(int, char **);
89 static int zpool_do_upgrade(int, char **);
91 static int zpool_do_history(int, char **);
93 static int zpool_do_get(int, char **);
94 static int zpool_do_set(int, char **);
97 * These libumem hooks provide a reasonable set of defaults for the allocator's
98 * debugging facilities.
103 _umem_debug_init(void)
105 return ("default,verbose"); /* $UMEM_DEBUG setting */
109 _umem_logging_init(void)
111 return ("fail,contents"); /* $UMEM_LOGGING setting */
143 typedef struct zpool_command
{
145 int (*func
)(int, char **);
150 * Master command table. Each ZFS command has a name, associated function, and
151 * usage message. The usage messages need to be internationalized, so we have
152 * to have a function to return the usage message based on a command index.
154 * These commands are organized according to how they are displayed in the usage
155 * message. An empty command (one with a NULL name) indicates an empty line in
156 * the generic usage message.
158 static zpool_command_t command_table
[] = {
159 { "create", zpool_do_create
, HELP_CREATE
},
160 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
162 { "add", zpool_do_add
, HELP_ADD
},
163 { "remove", zpool_do_remove
, HELP_REMOVE
},
165 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
167 { "list", zpool_do_list
, HELP_LIST
},
168 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
169 { "status", zpool_do_status
, HELP_STATUS
},
171 { "online", zpool_do_online
, HELP_ONLINE
},
172 { "offline", zpool_do_offline
, HELP_OFFLINE
},
173 { "clear", zpool_do_clear
, HELP_CLEAR
},
174 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
176 { "attach", zpool_do_attach
, HELP_ATTACH
},
177 { "detach", zpool_do_detach
, HELP_DETACH
},
178 { "replace", zpool_do_replace
, HELP_REPLACE
},
179 { "split", zpool_do_split
, HELP_SPLIT
},
181 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
183 { "import", zpool_do_import
, HELP_IMPORT
},
184 { "export", zpool_do_export
, HELP_EXPORT
},
185 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
186 { "reguid", zpool_do_reguid
, HELP_REGUID
},
188 { "history", zpool_do_history
, HELP_HISTORY
},
189 { "get", zpool_do_get
, HELP_GET
},
190 { "set", zpool_do_set
, HELP_SET
},
193 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
195 static zpool_command_t
*current_command
;
196 static char history_str
[HIS_MAX_RECORD_LEN
];
197 static boolean_t log_history
= B_TRUE
;
198 static uint_t timestamp_fmt
= NODATE
;
201 get_usage(zpool_help_t idx
)
205 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
207 return (gettext("\tattach [-f] <pool> <device> "
210 return (gettext("\tclear [-nF] <pool> [device]\n"));
212 return (gettext("\tcreate [-fnd] [-B] "
213 "[-o property=value] ... \n"
214 "\t [-O file-system-property=value] ... \n"
215 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
217 return (gettext("\tdestroy [-f] <pool>\n"));
219 return (gettext("\tdetach <pool> <device>\n"));
221 return (gettext("\texport [-f] <pool> ...\n"));
223 return (gettext("\thistory [-il] [<pool>] ...\n"));
225 return (gettext("\timport [-d dir] [-D]\n"
226 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
227 "\timport [-o mntopts] [-o property=value] ... \n"
228 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
229 "[-R root] [-F [-n]] -a\n"
230 "\timport [-o mntopts] [-o property=value] ... \n"
231 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
232 "[-R root] [-F [-n]]\n"
233 "\t <pool | id> [newpool]\n"));
235 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
237 case HELP_LABELCLEAR
:
238 return (gettext("\tlabelclear [-f] <vdev>\n"));
240 return (gettext("\tlist [-Hp] [-o property[,...]] "
241 "[-T d|u] [pool] ... [interval [count]]\n"));
243 return (gettext("\toffline [-t] <pool> <device> ...\n"));
245 return (gettext("\tonline <pool> <device> ...\n"));
247 return (gettext("\treplace [-f] <pool> <device> "
250 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
252 return (gettext("\treopen <pool>\n"));
254 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
256 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
259 return (gettext("\tupgrade\n"
261 "\tupgrade [-V version] <-a | pool ...>\n"));
263 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
264 "<\"all\" | property[,...]> <pool> ...\n"));
266 return (gettext("\tset <property=value> <pool> \n"));
268 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
269 "\t [-o property=value] <pool> <newpool> "
270 "[<device> ...]\n"));
272 return (gettext("\treguid <pool>\n"));
281 * Callback routine that will print out a pool property value.
284 print_prop_cb(int prop
, void *cb
)
288 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
290 if (zpool_prop_readonly(prop
))
291 (void) fprintf(fp
, " NO ");
293 (void) fprintf(fp
, " YES ");
295 if (zpool_prop_values(prop
) == NULL
)
296 (void) fprintf(fp
, "-\n");
298 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
304 * Display usage message. If we're inside a command, display only the usage for
305 * that command. Otherwise, iterate over the entire command table and display
306 * a complete usage message.
309 usage(boolean_t requested
)
311 FILE *fp
= requested
? stdout
: stderr
;
313 if (current_command
== NULL
) {
316 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
318 gettext("where 'command' is one of the following:\n\n"));
320 for (i
= 0; i
< NCOMMAND
; i
++) {
321 if (command_table
[i
].name
== NULL
)
322 (void) fprintf(fp
, "\n");
324 (void) fprintf(fp
, "%s",
325 get_usage(command_table
[i
].usage
));
328 (void) fprintf(fp
, gettext("usage:\n"));
329 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
332 if (current_command
!= NULL
&&
333 ((strcmp(current_command
->name
, "set") == 0) ||
334 (strcmp(current_command
->name
, "get") == 0) ||
335 (strcmp(current_command
->name
, "list") == 0))) {
338 gettext("\nthe following properties are supported:\n"));
340 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
341 "PROPERTY", "EDIT", "VALUES");
343 /* Iterate over all properties */
344 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
347 (void) fprintf(fp
, "\t%-15s ", "feature@...");
348 (void) fprintf(fp
, "YES disabled | enabled | active\n");
350 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
351 "appended with a feature name.\nSee zpool-features(5).\n"));
355 * See comments at end of main().
357 if (getenv("ZFS_ABORT") != NULL
) {
358 (void) printf("dumping core by request\n");
362 exit(requested
? 0 : 2);
366 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
367 boolean_t print_logs
)
374 (void) printf("\t%*s%s\n", indent
, "", name
);
376 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
377 &child
, &children
) != 0)
380 for (c
= 0; c
< children
; c
++) {
381 uint64_t is_log
= B_FALSE
;
383 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
385 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
388 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
389 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
396 prop_list_contains_feature(nvlist_t
*proplist
)
399 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
400 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
401 if (zpool_prop_feature(nvpair_name(nvp
)))
408 * Add a property pair (name, string-value) into a property nvlist.
411 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
414 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
420 if (*props
== NULL
&&
421 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
422 (void) fprintf(stderr
,
423 gettext("internal error: out of memory\n"));
430 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
432 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
433 !zpool_prop_feature(propname
)) {
434 (void) fprintf(stderr
, gettext("property '%s' is "
435 "not a valid pool property\n"), propname
);
440 * feature@ properties and version should not be specified
443 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
444 nvlist_exists(proplist
, vname
)) ||
445 (prop
== ZPOOL_PROP_VERSION
&&
446 prop_list_contains_feature(proplist
))) {
447 (void) fprintf(stderr
, gettext("'feature@' and "
448 "'version' properties cannot be specified "
454 if (zpool_prop_feature(propname
))
457 normnm
= zpool_prop_to_name(prop
);
459 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
460 normnm
= zfs_prop_to_name(fprop
);
466 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
467 prop
!= ZPOOL_PROP_CACHEFILE
) {
468 (void) fprintf(stderr
, gettext("property '%s' "
469 "specified multiple times\n"), propname
);
473 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
474 (void) fprintf(stderr
, gettext("internal "
475 "error: out of memory\n"));
483 * zpool add [-fn] <pool> <vdev> ...
485 * -f Force addition of devices, even if they appear in use
486 * -n Do not add the devices, but display the resulting layout if
487 * they were to be added.
489 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
490 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
494 zpool_do_add(int argc
, char **argv
)
496 boolean_t force
= B_FALSE
;
497 boolean_t dryrun
= B_FALSE
;
501 zpool_boot_label_t boot_type
;
508 while ((c
= getopt(argc
, argv
, "fn")) != -1) {
517 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
526 /* get pool name and check number of arguments */
528 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
532 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
541 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
544 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
545 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
551 if (zpool_is_bootable(zhp
))
552 boot_type
= ZPOOL_COPY_BOOT_LABEL
;
554 boot_type
= ZPOOL_NO_BOOT_LABEL
;
556 /* pass off to get_vdev_spec for processing */
557 boot_size
= zpool_get_prop_int(zhp
, ZPOOL_PROP_BOOTSIZE
, NULL
);
558 nvroot
= make_root_vdev(zhp
, force
, !force
, B_FALSE
, dryrun
,
559 boot_type
, boot_size
, argc
, argv
);
560 if (nvroot
== NULL
) {
566 nvlist_t
*poolnvroot
;
568 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
571 (void) printf(gettext("would update '%s' to the following "
572 "configuration:\n"), zpool_get_name(zhp
));
574 /* print original main pool and new tree */
575 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
576 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
578 /* Do the same for the logs */
579 if (num_logs(poolnvroot
) > 0) {
580 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
581 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
582 } else if (num_logs(nvroot
) > 0) {
583 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
588 ret
= (zpool_add(zhp
, nvroot
) != 0);
598 * zpool remove <pool> <vdev> ...
600 * Removes the given vdev from the pool.
603 zpool_do_remove(int argc
, char **argv
)
608 boolean_t stop
= B_FALSE
;
609 boolean_t noop
= B_FALSE
;
610 boolean_t parsable
= B_FALSE
;
614 while ((c
= getopt(argc
, argv
, "nps")) != -1) {
626 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
635 /* get pool name and check number of arguments */
637 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
643 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
647 (void) fprintf(stderr
, gettext("stop request ignored\n"));
653 (void) fprintf(stderr
, gettext("too many arguments\n"));
656 if (zpool_vdev_remove_cancel(zhp
) != 0)
660 (void) fprintf(stderr
, gettext("missing device\n"));
664 for (i
= 1; i
< argc
; i
++) {
668 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
674 (void) printf("%s %llu\n",
678 zfs_nicenum(size
, valstr
,
680 (void) printf("Memory that will be "
681 "used after removing %s: %s\n",
685 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
695 * zpool labelclear [-f] <vdev>
697 * -f Force clearing the label for the vdevs which are members of
698 * the exported or foreign pools.
700 * Verifies that the vdev is not active and zeros out the label information
704 zpool_do_labelclear(int argc
, char **argv
)
706 char vdev
[MAXPATHLEN
];
712 boolean_t inuse
= B_FALSE
;
713 boolean_t force
= B_FALSE
;
716 while ((c
= getopt(argc
, argv
, "f")) != -1) {
722 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
733 (void) fprintf(stderr
, gettext("missing vdev name\n"));
737 (void) fprintf(stderr
, gettext("too many arguments\n"));
742 * Check if we were given absolute path and use it as is.
743 * Otherwise if the provided vdev name doesn't point to a file,
744 * try prepending dsk path and appending s0.
746 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
747 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
750 (void) snprintf(vdev
, sizeof (vdev
), "%s/%s",
751 ZFS_DISK_ROOT
, argv
[0]);
752 if ((s
= strrchr(argv
[0], 's')) == NULL
||
754 (void) strlcat(vdev
, "s0", sizeof (vdev
));
755 if (stat(vdev
, &st
) != 0) {
756 (void) fprintf(stderr
, gettext(
757 "failed to find device %s, try specifying absolute "
758 "path instead\n"), argv
[0]);
763 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
764 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
765 vdev
, strerror(errno
));
769 if (zpool_read_label(fd
, &config
) != 0) {
770 (void) fprintf(stderr
,
771 gettext("failed to read label from %s\n"), vdev
);
776 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
778 (void) fprintf(stderr
,
779 gettext("failed to check state for %s\n"), vdev
);
788 case POOL_STATE_ACTIVE
:
789 case POOL_STATE_SPARE
:
790 case POOL_STATE_L2CACHE
:
791 (void) fprintf(stderr
, gettext(
792 "%s is a member (%s) of pool \"%s\"\n"),
793 vdev
, zpool_pool_state_to_name(state
), name
);
797 case POOL_STATE_EXPORTED
:
800 (void) fprintf(stderr
, gettext(
801 "use '-f' to override the following error:\n"
802 "%s is a member of exported pool \"%s\"\n"),
807 case POOL_STATE_POTENTIALLY_ACTIVE
:
810 (void) fprintf(stderr
, gettext(
811 "use '-f' to override the following error:\n"
812 "%s is a member of potentially active pool \"%s\"\n"),
817 case POOL_STATE_DESTROYED
:
818 /* inuse should never be set for a destroyed pool */
824 ret
= zpool_clear_label(fd
);
826 (void) fprintf(stderr
,
827 gettext("failed to clear label for %s\n"), vdev
);
838 * zpool create [-fnd] [-B] [-o property=value] ...
839 * [-O file-system-property=value] ...
840 * [-R root] [-m mountpoint] <pool> <dev> ...
842 * -B Create boot partition.
843 * -f Force creation, even if devices appear in use
844 * -n Do not create the pool, but display the resulting layout if it
845 * were to be created.
846 * -R Create a pool under an alternate root
847 * -m Set default mountpoint for the root dataset. By default it's
849 * -o Set property=value.
850 * -d Don't automatically enable all supported pool features
851 * (individual features can be enabled with -o).
852 * -O Set fsproperty=value in the pool's root file system
854 * Creates the named pool according to the given vdev specification. The
855 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
856 * we get the nvlist back from get_vdev_spec(), we either print out the contents
857 * (if '-n' was specified), or pass it to libzfs to do the creation.
860 #define SYSTEM256 (256 * 1024 * 1024)
862 zpool_do_create(int argc
, char **argv
)
864 boolean_t force
= B_FALSE
;
865 boolean_t dryrun
= B_FALSE
;
866 boolean_t enable_all_pool_feat
= B_TRUE
;
867 zpool_boot_label_t boot_type
= ZPOOL_NO_BOOT_LABEL
;
868 uint64_t boot_size
= 0;
870 nvlist_t
*nvroot
= NULL
;
873 char *altroot
= NULL
;
874 char *mountpoint
= NULL
;
875 nvlist_t
*fsprops
= NULL
;
876 nvlist_t
*props
= NULL
;
880 while ((c
= getopt(argc
, argv
, ":fndBR:m:o:O:")) != -1) {
889 enable_all_pool_feat
= B_FALSE
;
893 * We should create the system partition.
894 * Also make sure the size is set.
896 boot_type
= ZPOOL_CREATE_BOOT_LABEL
;
898 boot_size
= SYSTEM256
;
902 if (add_prop_list(zpool_prop_to_name(
903 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
905 if (nvlist_lookup_string(props
,
906 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
909 if (add_prop_list(zpool_prop_to_name(
910 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
914 /* Equivalent to -O mountpoint=optarg */
918 if ((propval
= strchr(optarg
, '=')) == NULL
) {
919 (void) fprintf(stderr
, gettext("missing "
920 "'=' for -o option\n"));
926 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
930 * Get bootsize value for make_root_vdev().
932 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_BOOTSIZE
) {
933 if (zfs_nicestrtonum(g_zfs
, propval
,
934 &boot_size
) < 0 || boot_size
== 0) {
935 (void) fprintf(stderr
,
936 gettext("bad boot partition size "
937 "'%s': %s\n"), propval
,
938 libzfs_error_description(g_zfs
));
944 * If the user is creating a pool that doesn't support
945 * feature flags, don't enable any features.
947 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
951 ver
= strtoull(propval
, &end
, 10);
953 ver
< SPA_VERSION_FEATURES
) {
954 enable_all_pool_feat
= B_FALSE
;
957 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
961 if ((propval
= strchr(optarg
, '=')) == NULL
) {
962 (void) fprintf(stderr
, gettext("missing "
963 "'=' for -O option\n"));
970 * Mountpoints are checked and then added later.
971 * Uniquely among properties, they can be specified
972 * more than once, to avoid conflict with -m.
974 if (0 == strcmp(optarg
,
975 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
976 mountpoint
= propval
;
977 } else if (add_prop_list(optarg
, propval
, &fsprops
,
983 (void) fprintf(stderr
, gettext("missing argument for "
984 "'%c' option\n"), optopt
);
987 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
996 /* get pool name and check number of arguments */
998 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1002 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1009 * As a special case, check for use of '/' in the name, and direct the
1010 * user to use 'zfs create' instead.
1012 if (strchr(poolname
, '/') != NULL
) {
1013 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1014 "character '/' in pool name\n"), poolname
);
1015 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1016 "create a dataset\n"));
1021 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
1022 * and not set otherwise.
1024 if (boot_type
== ZPOOL_CREATE_BOOT_LABEL
) {
1025 const char *propname
;
1026 char *strptr
, *buf
= NULL
;
1029 propname
= zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE
);
1030 if (nvlist_lookup_string(props
, propname
, &strptr
) != 0) {
1031 (void) asprintf(&buf
, "%" PRIu64
, boot_size
);
1033 (void) fprintf(stderr
,
1034 gettext("internal error: out of memory\n"));
1037 rv
= add_prop_list(propname
, buf
, &props
, B_TRUE
);
1043 const char *propname
;
1046 propname
= zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE
);
1047 if (nvlist_lookup_string(props
, propname
, &strptr
) == 0) {
1048 (void) fprintf(stderr
, gettext("error: setting boot "
1049 "partition size requires option '-B'\n"));
1054 /* pass off to get_vdev_spec for bulk processing */
1055 nvroot
= make_root_vdev(NULL
, force
, !force
, B_FALSE
, dryrun
,
1056 boot_type
, boot_size
, argc
- 1, argv
+ 1);
1060 /* make_root_vdev() allows 0 toplevel children if there are spares */
1061 if (!zfs_allocatable_devs(nvroot
)) {
1062 (void) fprintf(stderr
, gettext("invalid vdev "
1063 "specification: at least one toplevel vdev must be "
1068 if (altroot
!= NULL
&& altroot
[0] != '/') {
1069 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1070 "must be an absolute path\n"), altroot
);
1075 * Check the validity of the mountpoint and direct the user to use the
1076 * '-m' mountpoint option if it looks like its in use.
1078 if (mountpoint
== NULL
||
1079 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1080 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1081 char buf
[MAXPATHLEN
];
1084 if (mountpoint
&& mountpoint
[0] != '/') {
1085 (void) fprintf(stderr
, gettext("invalid mountpoint "
1086 "'%s': must be an absolute path, 'legacy', or "
1087 "'none'\n"), mountpoint
);
1091 if (mountpoint
== NULL
) {
1092 if (altroot
!= NULL
)
1093 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1096 (void) snprintf(buf
, sizeof (buf
), "/%s",
1099 if (altroot
!= NULL
)
1100 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1101 altroot
, mountpoint
);
1103 (void) snprintf(buf
, sizeof (buf
), "%s",
1107 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1108 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1109 "%s\n"), buf
, strerror(errno
));
1110 (void) fprintf(stderr
, gettext("use '-m' "
1111 "option to provide a different default\n"));
1116 while (count
< 3 && readdir(dirp
) != NULL
)
1118 (void) closedir(dirp
);
1121 (void) fprintf(stderr
, gettext("mountpoint "
1122 "'%s' exists and is not empty\n"), buf
);
1123 (void) fprintf(stderr
, gettext("use '-m' "
1124 "option to provide a "
1125 "different default\n"));
1132 * Now that the mountpoint's validity has been checked, ensure that
1133 * the property is set appropriately prior to creating the pool.
1135 if (mountpoint
!= NULL
) {
1136 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1137 mountpoint
, &fsprops
, B_FALSE
);
1145 * For a dry run invocation, print out a basic message and run
1146 * through all the vdevs in the list and print out in an
1147 * appropriate hierarchy.
1149 (void) printf(gettext("would create '%s' with the "
1150 "following layout:\n\n"), poolname
);
1152 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
1153 if (num_logs(nvroot
) > 0)
1154 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
1159 * Hand off to libzfs.
1161 if (enable_all_pool_feat
) {
1163 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1164 char propname
[MAXPATHLEN
];
1165 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1167 (void) snprintf(propname
, sizeof (propname
),
1168 "feature@%s", feat
->fi_uname
);
1171 * Skip feature if user specified it manually
1172 * on the command line.
1174 if (nvlist_exists(props
, propname
))
1177 ret
= add_prop_list(propname
,
1178 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1185 if (zpool_create(g_zfs
, poolname
,
1186 nvroot
, props
, fsprops
) == 0) {
1187 zfs_handle_t
*pool
= zfs_open(g_zfs
, poolname
,
1188 ZFS_TYPE_FILESYSTEM
);
1190 if (zfs_mount(pool
, NULL
, 0) == 0)
1191 ret
= zfs_shareall(pool
);
1194 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1195 (void) fprintf(stderr
, gettext("pool name may have "
1201 nvlist_free(nvroot
);
1202 nvlist_free(fsprops
);
1206 nvlist_free(fsprops
);
1213 * zpool destroy <pool>
1215 * -f Forcefully unmount any datasets
1217 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1220 zpool_do_destroy(int argc
, char **argv
)
1222 boolean_t force
= B_FALSE
;
1225 zpool_handle_t
*zhp
;
1229 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1235 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1244 /* check arguments */
1246 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1250 (void) fprintf(stderr
, gettext("too many arguments\n"));
1256 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1258 * As a special case, check for use of '/' in the name, and
1259 * direct the user to use 'zfs destroy' instead.
1261 if (strchr(pool
, '/') != NULL
)
1262 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1263 "destroy a dataset\n"));
1267 if (zpool_disable_datasets(zhp
, force
) != 0) {
1268 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1269 "could not unmount datasets\n"), zpool_get_name(zhp
));
1273 /* The history must be logged as part of the export */
1274 log_history
= B_FALSE
;
1276 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1284 * zpool export [-f] <pool> ...
1286 * -f Forcefully unmount datasets
1288 * Export the given pools. By default, the command will attempt to cleanly
1289 * unmount any active datasets within the pool. If the '-f' flag is specified,
1290 * then the datasets will be forcefully unmounted.
1293 zpool_do_export(int argc
, char **argv
)
1295 boolean_t force
= B_FALSE
;
1296 boolean_t hardforce
= B_FALSE
;
1298 zpool_handle_t
*zhp
;
1303 while ((c
= getopt(argc
, argv
, "fF")) != -1) {
1312 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1321 /* check arguments */
1323 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1328 for (i
= 0; i
< argc
; i
++) {
1329 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) == NULL
) {
1334 if (zpool_disable_datasets(zhp
, force
) != 0) {
1340 /* The history must be logged as part of the export */
1341 log_history
= B_FALSE
;
1344 if (zpool_export_force(zhp
, history_str
) != 0)
1346 } else if (zpool_export(zhp
, force
, history_str
) != 0) {
1357 * Given a vdev configuration, determine the maximum width needed for the device
1361 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1363 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1368 if (strlen(name
) + depth
> max
)
1369 max
= strlen(name
) + depth
;
1373 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1374 &child
, &children
) == 0) {
1375 for (c
= 0; c
< children
; c
++)
1376 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1381 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1382 &child
, &children
) == 0) {
1383 for (c
= 0; c
< children
; c
++)
1384 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1389 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1390 &child
, &children
) == 0) {
1391 for (c
= 0; c
< children
; c
++)
1392 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1401 typedef struct spare_cbdata
{
1403 zpool_handle_t
*cb_zhp
;
1407 find_vdev(nvlist_t
*nv
, uint64_t search
)
1413 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1417 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1418 &child
, &children
) == 0) {
1419 for (c
= 0; c
< children
; c
++)
1420 if (find_vdev(child
[c
], search
))
1428 find_spare(zpool_handle_t
*zhp
, void *data
)
1430 spare_cbdata_t
*cbp
= data
;
1431 nvlist_t
*config
, *nvroot
;
1433 config
= zpool_get_config(zhp
, NULL
);
1434 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1437 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1447 * Print out configuration state as requested by status_callback.
1450 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1451 int namewidth
, int depth
, boolean_t isspare
)
1455 pool_scan_stat_t
*ps
= NULL
;
1457 char rbuf
[6], wbuf
[6], cbuf
[6];
1459 uint64_t notpresent
;
1464 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1465 &child
, &children
) != 0)
1468 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1469 (uint64_t **)&vs
, &c
) == 0);
1471 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1473 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
1476 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1479 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1482 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1484 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1488 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1492 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1493 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1494 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1495 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1498 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1499 ¬present
) == 0) {
1501 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1502 (void) printf(" was %s", path
);
1503 } else if (vs
->vs_aux
!= 0) {
1506 switch (vs
->vs_aux
) {
1507 case VDEV_AUX_OPEN_FAILED
:
1508 (void) printf(gettext("cannot open"));
1511 case VDEV_AUX_BAD_GUID_SUM
:
1512 (void) printf(gettext("missing device"));
1515 case VDEV_AUX_NO_REPLICAS
:
1516 (void) printf(gettext("insufficient replicas"));
1519 case VDEV_AUX_VERSION_NEWER
:
1520 (void) printf(gettext("newer version"));
1523 case VDEV_AUX_UNSUP_FEAT
:
1524 (void) printf(gettext("unsupported feature(s)"));
1527 case VDEV_AUX_SPARED
:
1528 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1530 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1531 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1532 zpool_get_name(zhp
)) == 0)
1533 (void) printf(gettext("currently in "
1536 (void) printf(gettext("in use by "
1538 zpool_get_name(cb
.cb_zhp
));
1539 zpool_close(cb
.cb_zhp
);
1541 (void) printf(gettext("currently in use"));
1545 case VDEV_AUX_ERR_EXCEEDED
:
1546 (void) printf(gettext("too many errors"));
1549 case VDEV_AUX_IO_FAILURE
:
1550 (void) printf(gettext("experienced I/O failures"));
1553 case VDEV_AUX_BAD_LOG
:
1554 (void) printf(gettext("bad intent log"));
1557 case VDEV_AUX_EXTERNAL
:
1558 (void) printf(gettext("external device fault"));
1561 case VDEV_AUX_SPLIT_POOL
:
1562 (void) printf(gettext("split into new pool"));
1565 case VDEV_AUX_CHILDREN_OFFLINE
:
1566 (void) printf(gettext("all children offline"));
1570 (void) printf(gettext("corrupted data"));
1575 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1576 (uint64_t **)&ps
, &c
);
1578 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1579 vs
->vs_scan_processed
!= 0 && children
== 0) {
1580 (void) printf(gettext(" (%s)"),
1581 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1582 "resilvering" : "repairing");
1585 (void) printf("\n");
1587 for (c
= 0; c
< children
; c
++) {
1588 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1590 /* Don't print logs or holes here */
1591 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1593 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1595 if (islog
|| ishole
)
1597 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1598 print_status_config(zhp
, vname
, child
[c
],
1599 namewidth
, depth
+ 2, isspare
);
1606 * Print the configuration of an exported pool. Iterate over all vdevs in the
1607 * pool, printing out the name and status for each one.
1610 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1617 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1618 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1619 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1622 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1623 (uint64_t **)&vs
, &c
) == 0);
1625 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1626 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1628 if (vs
->vs_aux
!= 0) {
1631 switch (vs
->vs_aux
) {
1632 case VDEV_AUX_OPEN_FAILED
:
1633 (void) printf(gettext("cannot open"));
1636 case VDEV_AUX_BAD_GUID_SUM
:
1637 (void) printf(gettext("missing device"));
1640 case VDEV_AUX_NO_REPLICAS
:
1641 (void) printf(gettext("insufficient replicas"));
1644 case VDEV_AUX_VERSION_NEWER
:
1645 (void) printf(gettext("newer version"));
1648 case VDEV_AUX_UNSUP_FEAT
:
1649 (void) printf(gettext("unsupported feature(s)"));
1652 case VDEV_AUX_ERR_EXCEEDED
:
1653 (void) printf(gettext("too many errors"));
1656 case VDEV_AUX_CHILDREN_OFFLINE
:
1657 (void) printf(gettext("all children offline"));
1661 (void) printf(gettext("corrupted data"));
1665 (void) printf("\n");
1667 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1668 &child
, &children
) != 0)
1671 for (c
= 0; c
< children
; c
++) {
1672 uint64_t is_log
= B_FALSE
;
1674 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1679 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1680 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1684 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1685 &child
, &children
) == 0) {
1686 (void) printf(gettext("\tcache\n"));
1687 for (c
= 0; c
< children
; c
++) {
1688 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1689 (void) printf("\t %s\n", vname
);
1694 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1695 &child
, &children
) == 0) {
1696 (void) printf(gettext("\tspares\n"));
1697 for (c
= 0; c
< children
; c
++) {
1698 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1699 (void) printf("\t %s\n", vname
);
1707 * Logs are recorded as top level vdevs in the main pool child array
1708 * but with "is_log" set to 1. We use either print_status_config() or
1709 * print_import_config() to print the top level logs then any log
1710 * children (eg mirrored slogs) are printed recursively - which
1711 * works because only the top level vdev is marked "is_log"
1714 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1719 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1723 (void) printf(gettext("\tlogs\n"));
1725 for (c
= 0; c
< children
; c
++) {
1726 uint64_t is_log
= B_FALSE
;
1729 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1733 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1735 print_status_config(zhp
, name
, child
[c
], namewidth
,
1738 print_import_config(name
, child
[c
], namewidth
, 2);
1744 * Display the status for the given pool.
1747 show_import(nvlist_t
*config
)
1749 uint64_t pool_state
;
1761 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1763 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1765 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1767 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1770 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1771 (uint64_t **)&vs
, &vsc
) == 0);
1772 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1774 reason
= zpool_import_status(config
, &msgid
);
1776 (void) printf(gettext(" pool: %s\n"), name
);
1777 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1778 (void) printf(gettext(" state: %s"), health
);
1779 if (pool_state
== POOL_STATE_DESTROYED
)
1780 (void) printf(gettext(" (DESTROYED)"));
1781 (void) printf("\n");
1784 case ZPOOL_STATUS_MISSING_DEV_R
:
1785 case ZPOOL_STATUS_MISSING_DEV_NR
:
1786 case ZPOOL_STATUS_BAD_GUID_SUM
:
1787 (void) printf(gettext(" status: One or more devices are "
1788 "missing from the system.\n"));
1791 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1792 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1793 (void) printf(gettext(" status: One or more devices contains "
1794 "corrupted data.\n"));
1797 case ZPOOL_STATUS_CORRUPT_DATA
:
1799 gettext(" status: The pool data is corrupted.\n"));
1802 case ZPOOL_STATUS_OFFLINE_DEV
:
1803 (void) printf(gettext(" status: One or more devices "
1804 "are offlined.\n"));
1807 case ZPOOL_STATUS_CORRUPT_POOL
:
1808 (void) printf(gettext(" status: The pool metadata is "
1812 case ZPOOL_STATUS_VERSION_OLDER
:
1813 (void) printf(gettext(" status: The pool is formatted using a "
1814 "legacy on-disk version.\n"));
1817 case ZPOOL_STATUS_VERSION_NEWER
:
1818 (void) printf(gettext(" status: The pool is formatted using an "
1819 "incompatible version.\n"));
1822 case ZPOOL_STATUS_FEAT_DISABLED
:
1823 (void) printf(gettext(" status: Some supported features are "
1824 "not enabled on the pool.\n"));
1827 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1828 (void) printf(gettext("status: The pool uses the following "
1829 "feature(s) not supported on this system:\n"));
1830 zpool_print_unsup_feat(config
);
1833 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1834 (void) printf(gettext("status: The pool can only be accessed "
1835 "in read-only mode on this system. It\n\tcannot be "
1836 "accessed in read-write mode because it uses the "
1837 "following\n\tfeature(s) not supported on this system:\n"));
1838 zpool_print_unsup_feat(config
);
1841 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1842 (void) printf(gettext(" status: The pool was last accessed by "
1843 "another system.\n"));
1846 case ZPOOL_STATUS_FAULTED_DEV_R
:
1847 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1848 (void) printf(gettext(" status: One or more devices are "
1852 case ZPOOL_STATUS_BAD_LOG
:
1853 (void) printf(gettext(" status: An intent log record cannot be "
1857 case ZPOOL_STATUS_RESILVERING
:
1858 (void) printf(gettext(" status: One or more devices were being "
1864 * No other status can be seen when importing pools.
1866 assert(reason
== ZPOOL_STATUS_OK
);
1870 * Print out an action according to the overall state of the pool.
1872 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1873 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1874 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1875 (void) printf(gettext(" action: The pool can be "
1876 "imported using its name or numeric identifier, "
1877 "though\n\tsome features will not be available "
1878 "without an explicit 'zpool upgrade'.\n"));
1879 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1880 (void) printf(gettext(" action: The pool can be "
1881 "imported using its name or numeric "
1882 "identifier and\n\tthe '-f' flag.\n"));
1884 (void) printf(gettext(" action: The pool can be "
1885 "imported using its name or numeric "
1888 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1889 (void) printf(gettext(" action: The pool can be imported "
1890 "despite missing or damaged devices. The\n\tfault "
1891 "tolerance of the pool may be compromised if imported.\n"));
1894 case ZPOOL_STATUS_VERSION_NEWER
:
1895 (void) printf(gettext(" action: The pool cannot be "
1896 "imported. Access the pool on a system running "
1897 "newer\n\tsoftware, or recreate the pool from "
1900 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1901 (void) printf(gettext("action: The pool cannot be "
1902 "imported. Access the pool on a system that "
1903 "supports\n\tthe required feature(s), or recreate "
1904 "the pool from backup.\n"));
1906 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1907 (void) printf(gettext("action: The pool cannot be "
1908 "imported in read-write mode. Import the pool "
1910 "\t\"-o readonly=on\", access the pool on a system "
1911 "that supports the\n\trequired feature(s), or "
1912 "recreate the pool from backup.\n"));
1914 case ZPOOL_STATUS_MISSING_DEV_R
:
1915 case ZPOOL_STATUS_MISSING_DEV_NR
:
1916 case ZPOOL_STATUS_BAD_GUID_SUM
:
1917 (void) printf(gettext(" action: The pool cannot be "
1918 "imported. Attach the missing\n\tdevices and try "
1922 (void) printf(gettext(" action: The pool cannot be "
1923 "imported due to damaged devices or data.\n"));
1927 /* Print the comment attached to the pool. */
1928 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
1929 (void) printf(gettext("comment: %s\n"), comment
);
1932 * If the state is "closed" or "can't open", and the aux state
1933 * is "corrupt data":
1935 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
1936 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
1937 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
1938 if (pool_state
== POOL_STATE_DESTROYED
)
1939 (void) printf(gettext("\tThe pool was destroyed, "
1940 "but can be imported using the '-Df' flags.\n"));
1941 else if (pool_state
!= POOL_STATE_EXPORTED
)
1942 (void) printf(gettext("\tThe pool may be active on "
1943 "another system, but can be imported using\n\t"
1944 "the '-f' flag.\n"));
1948 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1951 (void) printf(gettext(" config:\n\n"));
1953 namewidth
= max_width(NULL
, nvroot
, 0, 0);
1957 print_import_config(name
, nvroot
, namewidth
, 0);
1958 if (num_logs(nvroot
) > 0)
1959 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
1961 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
1962 (void) printf(gettext("\n\tAdditional devices are known to "
1963 "be part of this pool, though their\n\texact "
1964 "configuration cannot be determined.\n"));
1969 * Perform the import for the given configuration. This passes the heavy
1970 * lifting off to zpool_import_props(), and then mounts the datasets contained
1974 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
1975 nvlist_t
*props
, int flags
)
1977 zpool_handle_t
*zhp
;
1982 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1985 verify(nvlist_lookup_uint64(config
,
1986 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
1987 verify(nvlist_lookup_uint64(config
,
1988 ZPOOL_CONFIG_VERSION
, &version
) == 0);
1989 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1990 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
1991 "is formatted using an unsupported ZFS version\n"), name
);
1993 } else if (state
!= POOL_STATE_EXPORTED
&&
1994 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
1997 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
1999 if ((unsigned long)hostid
!= gethostid()) {
2004 verify(nvlist_lookup_string(config
,
2005 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2006 verify(nvlist_lookup_uint64(config
,
2007 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
2009 (void) fprintf(stderr
, gettext("cannot import "
2010 "'%s': pool may be in use from other "
2011 "system, it was last accessed by %s "
2012 "(hostid: 0x%lx) on %s"), name
, hostname
,
2013 (unsigned long)hostid
,
2014 asctime(localtime(&t
)));
2015 (void) fprintf(stderr
, gettext("use '-f' to "
2016 "import anyway\n"));
2020 (void) fprintf(stderr
, gettext("cannot import '%s': "
2021 "pool may be in use from other system\n"), name
);
2022 (void) fprintf(stderr
, gettext("use '-f' to import "
2028 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2031 if (newname
!= NULL
)
2032 name
= (char *)newname
;
2034 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2037 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2038 !(flags
& ZFS_IMPORT_ONLY
) &&
2039 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2049 * zpool import [-d dir] [-D]
2050 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2051 * [-d dir | -c cachefile] [-f] -a
2052 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2053 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2055 * -c Read pool information from a cachefile instead of searching
2058 * -d Scan in a specific directory, other than /dev/dsk. More than
2059 * one directory can be specified using multiple '-d' options.
2061 * -D Scan for previously destroyed pools or import all or only
2062 * specified destroyed pools.
2064 * -R Temporarily import the pool, with all mountpoints relative to
2065 * the given root. The pool will remain exported when the machine
2068 * -V Import even in the presence of faulted vdevs. This is an
2069 * intentionally undocumented option for testing purposes, and
2070 * treats the pool configuration as complete, leaving any bad
2071 * vdevs in the FAULTED state. In other words, it does verbatim
2074 * -f Force import, even if it appears that the pool is active.
2076 * -F Attempt rewind if necessary.
2078 * -n See if rewind would work, but don't actually rewind.
2080 * -N Import the pool but don't mount datasets.
2082 * -T Specify a starting txg to use for import. This option is
2083 * intentionally undocumented option for testing purposes.
2085 * -a Import all pools found.
2087 * -o Set property=value and/or temporary mount options (without '=').
2089 * The import command scans for pools to import, and import pools based on pool
2090 * name and GUID. The pool can also be renamed as part of the import process.
2093 zpool_do_import(int argc
, char **argv
)
2095 char **searchdirs
= NULL
;
2099 nvlist_t
*pools
= NULL
;
2100 boolean_t do_all
= B_FALSE
;
2101 boolean_t do_destroyed
= B_FALSE
;
2102 char *mntopts
= NULL
;
2105 uint64_t searchguid
= 0;
2106 char *searchname
= NULL
;
2108 nvlist_t
*found_config
;
2109 nvlist_t
*policy
= NULL
;
2110 nvlist_t
*props
= NULL
;
2112 int flags
= ZFS_IMPORT_NORMAL
;
2113 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2114 boolean_t dryrun
= B_FALSE
;
2115 boolean_t do_rewind
= B_FALSE
;
2116 boolean_t xtreme_rewind
= B_FALSE
;
2117 uint64_t pool_state
, txg
= -1ULL;
2118 char *cachefile
= NULL
;
2119 importargs_t idata
= { 0 };
2123 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
2132 if (searchdirs
== NULL
) {
2133 searchdirs
= safe_malloc(sizeof (char *));
2135 char **tmp
= safe_malloc((nsearch
+ 1) *
2137 bcopy(searchdirs
, tmp
, nsearch
*
2142 searchdirs
[nsearch
++] = optarg
;
2145 do_destroyed
= B_TRUE
;
2148 flags
|= ZFS_IMPORT_ANY_HOST
;
2154 flags
|= ZFS_IMPORT_MISSING_LOG
;
2160 flags
|= ZFS_IMPORT_ONLY
;
2163 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2166 if (add_prop_list(optarg
, propval
,
2174 if (add_prop_list(zpool_prop_to_name(
2175 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2177 if (nvlist_lookup_string(props
,
2178 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
2181 if (add_prop_list(zpool_prop_to_name(
2182 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2187 txg
= strtoull(optarg
, &endptr
, 0);
2188 if (errno
!= 0 || *endptr
!= '\0') {
2189 (void) fprintf(stderr
,
2190 gettext("invalid txg value\n"));
2193 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2196 flags
|= ZFS_IMPORT_VERBATIM
;
2199 xtreme_rewind
= B_TRUE
;
2202 (void) fprintf(stderr
, gettext("missing argument for "
2203 "'%c' option\n"), optopt
);
2207 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2216 if (cachefile
&& nsearch
!= 0) {
2217 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2221 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2222 (void) fprintf(stderr
,
2223 gettext("-n or -X only meaningful with -F\n"));
2227 rewind_policy
= ZPOOL_TRY_REWIND
;
2229 rewind_policy
= ZPOOL_DO_REWIND
;
2231 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2233 /* In the future, we can capture further policy and include it here */
2234 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2235 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2236 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2239 if (searchdirs
== NULL
) {
2240 searchdirs
= safe_malloc(sizeof (char *));
2241 searchdirs
[0] = ZFS_DISK_ROOT
;
2245 /* check argument count */
2248 (void) fprintf(stderr
, gettext("too many arguments\n"));
2253 (void) fprintf(stderr
, gettext("too many arguments\n"));
2258 * Check for the SYS_CONFIG privilege. We do this explicitly
2259 * here because otherwise any attempt to discover pools will
2262 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
2263 (void) fprintf(stderr
, gettext("cannot "
2264 "discover pools: permission denied\n"));
2266 nvlist_free(policy
);
2272 * Depending on the arguments given, we do one of the following:
2274 * <none> Iterate through all pools and display information about
2277 * -a Iterate through all pools and try to import each one.
2279 * <id> Find the pool that corresponds to the given GUID/pool
2280 * name and import that one.
2282 * -D Above options applies only to destroyed pools.
2288 searchguid
= strtoull(argv
[0], &endptr
, 10);
2289 if (errno
!= 0 || *endptr
!= '\0') {
2290 searchname
= argv
[0];
2293 found_config
= NULL
;
2296 * User specified a name or guid. Ensure it's unique.
2298 idata
.unique
= B_TRUE
;
2302 idata
.path
= searchdirs
;
2303 idata
.paths
= nsearch
;
2304 idata
.poolname
= searchname
;
2305 idata
.guid
= searchguid
;
2306 idata
.cachefile
= cachefile
;
2307 idata
.policy
= policy
;
2309 pools
= zpool_search_import(g_zfs
, &idata
);
2311 if (pools
!= NULL
&& idata
.exists
&&
2312 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2313 (void) fprintf(stderr
, gettext("cannot import '%s': "
2314 "a pool with that name already exists\n"),
2316 (void) fprintf(stderr
, gettext("use the form '%s "
2317 "<pool | id> <newpool>' to give it a new name\n"),
2320 } else if (pools
== NULL
&& idata
.exists
) {
2321 (void) fprintf(stderr
, gettext("cannot import '%s': "
2322 "a pool with that name is already created/imported,\n"),
2324 (void) fprintf(stderr
, gettext("and no additional pools "
2325 "with that name were found\n"));
2327 } else if (pools
== NULL
) {
2329 (void) fprintf(stderr
, gettext("cannot import '%s': "
2330 "no such pool available\n"), argv
[0]);
2337 nvlist_free(policy
);
2342 * At this point we have a list of import candidate configs. Even if
2343 * we were searching by pool name or guid, we still need to
2344 * post-process the list to deal with pool state and possible
2350 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2352 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2354 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2356 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2358 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2361 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2368 (void) printf("\n");
2371 err
|= do_import(config
, NULL
, mntopts
,
2374 show_import(config
);
2376 } else if (searchname
!= NULL
) {
2380 * We are searching for a pool based on name.
2382 verify(nvlist_lookup_string(config
,
2383 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2385 if (strcmp(name
, searchname
) == 0) {
2386 if (found_config
!= NULL
) {
2387 (void) fprintf(stderr
, gettext(
2388 "cannot import '%s': more than "
2389 "one matching pool\n"), searchname
);
2390 (void) fprintf(stderr
, gettext(
2391 "import by numeric ID instead\n"));
2394 found_config
= config
;
2400 * Search for a pool by guid.
2402 verify(nvlist_lookup_uint64(config
,
2403 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2405 if (guid
== searchguid
)
2406 found_config
= config
;
2411 * If we were searching for a specific pool, verify that we found a
2412 * pool, and then do the import.
2414 if (argc
!= 0 && err
== 0) {
2415 if (found_config
== NULL
) {
2416 (void) fprintf(stderr
, gettext("cannot import '%s': "
2417 "no such pool available\n"), argv
[0]);
2420 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2421 argv
[1], mntopts
, props
, flags
);
2426 * If we were just looking for pools, report an error if none were
2429 if (argc
== 0 && first
)
2430 (void) fprintf(stderr
,
2431 gettext("no pools available to import\n"));
2436 nvlist_free(policy
);
2439 return (err
? 1 : 0);
2442 typedef struct iostat_cbdata
{
2443 boolean_t cb_verbose
;
2446 zpool_list_t
*cb_list
;
2450 print_iostat_separator(iostat_cbdata_t
*cb
)
2454 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2456 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2460 print_iostat_header(iostat_cbdata_t
*cb
)
2462 (void) printf("%*s capacity operations bandwidth\n",
2463 cb
->cb_namewidth
, "");
2464 (void) printf("%-*s alloc free read write read write\n",
2465 cb
->cb_namewidth
, "pool");
2466 print_iostat_separator(cb
);
2470 * Display a single statistic.
2473 print_one_stat(uint64_t value
)
2477 zfs_nicenum(value
, buf
, sizeof (buf
));
2478 (void) printf(" %5s", buf
);
2482 * Print out all the statistics for the given vdev. This can either be the
2483 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2484 * is a verbose output, and we don't want to display the toplevel pool stats.
2487 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2488 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2490 nvlist_t
**oldchild
, **newchild
;
2492 vdev_stat_t
*oldvs
, *newvs
;
2493 vdev_stat_t zerovs
= { 0 };
2498 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
2501 if (oldnv
!= NULL
) {
2502 verify(nvlist_lookup_uint64_array(oldnv
,
2503 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2508 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2509 (uint64_t **)&newvs
, &c
) == 0);
2511 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2512 (void) printf("%*s%s", depth
, "", name
);
2514 (void) printf("%*s%s%*s", depth
, "", name
,
2515 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2517 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2522 scale
= (double)NANOSEC
/ tdelta
;
2524 /* only toplevel vdevs have capacity stats */
2525 if (newvs
->vs_space
== 0) {
2526 (void) printf(" - -");
2528 print_one_stat(newvs
->vs_alloc
);
2529 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2532 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2533 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2535 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2536 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2538 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2539 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2541 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2542 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2544 (void) printf("\n");
2546 if (!cb
->cb_verbose
)
2549 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2550 &newchild
, &children
) != 0)
2553 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2554 &oldchild
, &c
) != 0)
2557 for (c
= 0; c
< children
; c
++) {
2558 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2560 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2563 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2566 if (ishole
|| islog
)
2569 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2570 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2571 newchild
[c
], cb
, depth
+ 2);
2576 * Log device section
2579 if (num_logs(newnv
) > 0) {
2580 (void) printf("%-*s - - - - - "
2581 "-\n", cb
->cb_namewidth
, "logs");
2583 for (c
= 0; c
< children
; c
++) {
2584 uint64_t islog
= B_FALSE
;
2585 (void) nvlist_lookup_uint64(newchild
[c
],
2586 ZPOOL_CONFIG_IS_LOG
, &islog
);
2589 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2591 print_vdev_stats(zhp
, vname
, oldnv
?
2592 oldchild
[c
] : NULL
, newchild
[c
],
2601 * Include level 2 ARC devices in iostat output
2603 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2604 &newchild
, &children
) != 0)
2607 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2608 &oldchild
, &c
) != 0)
2612 (void) printf("%-*s - - - - - "
2613 "-\n", cb
->cb_namewidth
, "cache");
2614 for (c
= 0; c
< children
; c
++) {
2615 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2617 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2618 newchild
[c
], cb
, depth
+ 2);
2625 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2627 iostat_cbdata_t
*cb
= data
;
2631 * If the pool has disappeared, remove it from the list and continue.
2633 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2637 pool_list_remove(cb
->cb_list
, zhp
);
2643 * Callback to print out the iostats for the given pool.
2646 print_iostat(zpool_handle_t
*zhp
, void *data
)
2648 iostat_cbdata_t
*cb
= data
;
2649 nvlist_t
*oldconfig
, *newconfig
;
2650 nvlist_t
*oldnvroot
, *newnvroot
;
2652 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2654 if (cb
->cb_iteration
== 1)
2657 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2660 if (oldconfig
== NULL
)
2663 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2667 * Print out the statistics for the pool.
2669 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2672 print_iostat_separator(cb
);
2678 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2680 iostat_cbdata_t
*cb
= data
;
2681 nvlist_t
*config
, *nvroot
;
2683 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2684 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2686 if (!cb
->cb_verbose
)
2687 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2689 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2694 * The width must fall into the range [10,38]. The upper limit is the
2695 * maximum we can have and still fit in 80 columns.
2697 if (cb
->cb_namewidth
< 10)
2698 cb
->cb_namewidth
= 10;
2699 if (cb
->cb_namewidth
> 38)
2700 cb
->cb_namewidth
= 38;
2706 * Parse the input string, get the 'interval' and 'count' value if there is one.
2709 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2712 unsigned long interval
= 0, count
= 0;
2713 int argc
= *argcp
, errno
;
2716 * Determine if the last argument is an integer or a pool name
2718 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2722 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2724 if (*end
== '\0' && errno
== 0) {
2725 if (interval
== 0) {
2726 (void) fprintf(stderr
, gettext("interval "
2727 "cannot be zero\n"));
2731 * Ignore the last parameter
2736 * If this is not a valid number, just plow on. The
2737 * user will get a more informative error message later
2745 * If the last argument is also an integer, then we have both a count
2748 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2753 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2755 if (*end
== '\0' && errno
== 0) {
2756 if (interval
== 0) {
2757 (void) fprintf(stderr
, gettext("interval "
2758 "cannot be zero\n"));
2763 * Ignore the last parameter
2777 get_timestamp_arg(char c
)
2780 timestamp_fmt
= UDATE
;
2782 timestamp_fmt
= DDATE
;
2788 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2790 * -v Display statistics for individual vdevs
2791 * -T Display a timestamp in date(1) or Unix format
2793 * This command can be tricky because we want to be able to deal with pool
2794 * creation/destruction as well as vdev configuration changes. The bulk of this
2795 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2796 * on pool_list_update() to detect the addition of new pools. Configuration
2797 * changes are all handled within libzfs.
2800 zpool_do_iostat(int argc
, char **argv
)
2805 unsigned long interval
= 0, count
= 0;
2807 boolean_t verbose
= B_FALSE
;
2811 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2814 get_timestamp_arg(*optarg
);
2820 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2829 get_interval_count(&argc
, argv
, &interval
, &count
);
2832 * Construct the list of all interesting pools.
2835 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
2838 if (pool_list_count(list
) == 0 && argc
!= 0) {
2839 pool_list_free(list
);
2843 if (pool_list_count(list
) == 0 && interval
== 0) {
2844 pool_list_free(list
);
2845 (void) fprintf(stderr
, gettext("no pools available\n"));
2850 * Enter the main iostat loop.
2853 cb
.cb_verbose
= verbose
;
2854 cb
.cb_iteration
= 0;
2855 cb
.cb_namewidth
= 0;
2858 pool_list_update(list
);
2860 if ((npools
= pool_list_count(list
)) == 0)
2864 * Refresh all statistics. This is done as an explicit step
2865 * before calculating the maximum name width, so that any
2866 * configuration changes are properly accounted for.
2868 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
, &cb
);
2871 * Iterate over all pools to determine the maximum width
2872 * for the pool / device name column across all pools.
2874 cb
.cb_namewidth
= 0;
2875 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
2877 if (timestamp_fmt
!= NODATE
)
2878 print_timestamp(timestamp_fmt
);
2881 * If it's the first time, or verbose mode, print the header.
2883 if (++cb
.cb_iteration
== 1 || verbose
)
2884 print_iostat_header(&cb
);
2886 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
2889 * If there's more than one pool, and we're not in verbose mode
2890 * (which prints a separator for us), then print a separator.
2892 if (npools
> 1 && !verbose
)
2893 print_iostat_separator(&cb
);
2896 (void) printf("\n");
2899 * Flush the output so that redirection to a file isn't buffered
2902 (void) fflush(stdout
);
2907 if (count
!= 0 && --count
== 0)
2910 (void) sleep(interval
);
2913 pool_list_free(list
);
2918 typedef struct list_cbdata
{
2919 boolean_t cb_verbose
;
2921 boolean_t cb_scripted
;
2922 zprop_list_t
*cb_proplist
;
2923 boolean_t cb_literal
;
2927 * Given a list of columns to display, output appropriate headers for each one.
2930 print_header(list_cbdata_t
*cb
)
2932 zprop_list_t
*pl
= cb
->cb_proplist
;
2933 char headerbuf
[ZPOOL_MAXPROPLEN
];
2935 boolean_t first
= B_TRUE
;
2936 boolean_t right_justify
;
2939 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2940 width
= pl
->pl_width
;
2941 if (first
&& cb
->cb_verbose
) {
2943 * Reset the width to accommodate the verbose listing
2946 width
= cb
->cb_namewidth
;
2954 right_justify
= B_FALSE
;
2955 if (pl
->pl_prop
!= ZPROP_INVAL
) {
2956 header
= zpool_prop_column_name(pl
->pl_prop
);
2957 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
2961 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
2962 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
2963 headerbuf
[i
] = '\0';
2967 if (pl
->pl_next
== NULL
&& !right_justify
)
2968 (void) printf("%s", header
);
2969 else if (right_justify
)
2970 (void) printf("%*s", width
, header
);
2972 (void) printf("%-*s", width
, header
);
2976 (void) printf("\n");
2980 * Given a pool and a list of properties, print out all the properties according
2981 * to the described layout.
2984 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
2986 zprop_list_t
*pl
= cb
->cb_proplist
;
2987 boolean_t first
= B_TRUE
;
2988 char property
[ZPOOL_MAXPROPLEN
];
2990 boolean_t right_justify
;
2993 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
2995 width
= pl
->pl_width
;
2996 if (first
&& cb
->cb_verbose
) {
2998 * Reset the width to accommodate the verbose listing
3001 width
= cb
->cb_namewidth
;
3005 if (cb
->cb_scripted
)
3006 (void) printf("\t");
3013 right_justify
= B_FALSE
;
3014 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3015 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
3016 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
3021 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3022 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
3023 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
3024 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
3025 sizeof (property
)) == 0) {
3033 * If this is being called in scripted mode, or if this is the
3034 * last column and it is left-justified, don't include a width
3037 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3038 (void) printf("%s", propstr
);
3039 else if (right_justify
)
3040 (void) printf("%*s", width
, propstr
);
3042 (void) printf("%-*s", width
, propstr
);
3045 (void) printf("\n");
3049 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
3054 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3057 case ZPOOL_PROP_EXPANDSZ
:
3059 (void) strlcpy(propval
, "-", sizeof (propval
));
3061 zfs_nicenum(value
, propval
, sizeof (propval
));
3063 case ZPOOL_PROP_FRAGMENTATION
:
3064 if (value
== ZFS_FRAG_INVALID
) {
3065 (void) strlcpy(propval
, "-", sizeof (propval
));
3067 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3071 case ZPOOL_PROP_CAPACITY
:
3072 (void) snprintf(propval
, sizeof (propval
), "%llu%%", value
);
3075 zfs_nicenum(value
, propval
, sizeof (propval
));
3079 (void) strlcpy(propval
, "-", sizeof (propval
));
3082 (void) printf("\t%s", propval
);
3084 (void) printf(" %*s", width
, propval
);
3088 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3089 list_cbdata_t
*cb
, int depth
)
3095 boolean_t scripted
= cb
->cb_scripted
;
3096 uint64_t islog
= B_FALSE
;
3097 boolean_t haslog
= B_FALSE
;
3098 char *dashes
= "%-*s - - - - - -\n";
3100 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3101 (uint64_t **)&vs
, &c
) == 0);
3104 boolean_t toplevel
= (vs
->vs_space
!= 0);
3107 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
3111 (void) printf("\t%s", name
);
3112 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3113 (void) printf("%*s%s", depth
, "", name
);
3115 (void) printf("%*s%s%*s", depth
, "", name
,
3116 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3119 * Print the properties for the individual vdevs. Some
3120 * properties are only applicable to toplevel vdevs. The
3121 * 'toplevel' boolean value is passed to the print_one_column()
3122 * to indicate that the value is valid.
3124 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
3126 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
3128 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
3129 scripted
, toplevel
);
3130 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
3132 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
3133 vs
->vs_fragmentation
, scripted
,
3134 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
3135 cap
= (vs
->vs_space
== 0) ? 0 :
3136 (vs
->vs_alloc
* 100 / vs
->vs_space
);
3137 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
3138 (void) printf("\n");
3141 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3142 &child
, &children
) != 0)
3145 for (c
= 0; c
< children
; c
++) {
3146 uint64_t ishole
= B_FALSE
;
3148 if (nvlist_lookup_uint64(child
[c
],
3149 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3152 if (nvlist_lookup_uint64(child
[c
],
3153 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
3158 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3159 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3163 if (haslog
== B_TRUE
) {
3164 /* LINTED E_SEC_PRINTF_VAR_FMT */
3165 (void) printf(dashes
, cb
->cb_namewidth
, "log");
3166 for (c
= 0; c
< children
; c
++) {
3167 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3168 &islog
) != 0 || !islog
)
3170 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3171 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3176 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3177 &child
, &children
) == 0 && children
> 0) {
3178 /* LINTED E_SEC_PRINTF_VAR_FMT */
3179 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
3180 for (c
= 0; c
< children
; c
++) {
3181 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3182 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3187 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
3188 &children
) == 0 && children
> 0) {
3189 /* LINTED E_SEC_PRINTF_VAR_FMT */
3190 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
3191 for (c
= 0; c
< children
; c
++) {
3192 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3193 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3201 * Generic callback function to list a pool.
3204 list_callback(zpool_handle_t
*zhp
, void *data
)
3206 list_cbdata_t
*cbp
= data
;
3210 config
= zpool_get_config(zhp
, NULL
);
3212 print_pool(zhp
, cbp
);
3213 if (!cbp
->cb_verbose
)
3216 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3218 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3224 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3226 * -H Scripted mode. Don't display headers, and separate properties
3228 * -o List of properties to display. Defaults to
3229 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3230 * "dedupratio,health,altroot"
3231 * -p Diplay values in parsable (exact) format.
3232 * -T Display a timestamp in date(1) or Unix format
3234 * List all pools in the system, whether or not they're healthy. Output space
3235 * statistics for each one, as well as health status summary.
3238 zpool_do_list(int argc
, char **argv
)
3242 list_cbdata_t cb
= { 0 };
3243 static char default_props
[] =
3244 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3245 "dedupratio,health,altroot";
3246 char *props
= default_props
;
3247 unsigned long interval
= 0, count
= 0;
3249 boolean_t first
= B_TRUE
;
3252 while ((c
= getopt(argc
, argv
, ":Ho:pT:v")) != -1) {
3255 cb
.cb_scripted
= B_TRUE
;
3261 cb
.cb_literal
= B_TRUE
;
3264 get_timestamp_arg(*optarg
);
3267 cb
.cb_verbose
= B_TRUE
;
3270 (void) fprintf(stderr
, gettext("missing argument for "
3271 "'%c' option\n"), optopt
);
3275 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3284 get_interval_count(&argc
, argv
, &interval
, &count
);
3286 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3290 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
3294 if (pool_list_count(list
) == 0)
3297 cb
.cb_namewidth
= 0;
3298 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
3300 if (timestamp_fmt
!= NODATE
)
3301 print_timestamp(timestamp_fmt
);
3303 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3307 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3312 if (count
!= 0 && --count
== 0)
3315 pool_list_free(list
);
3316 (void) sleep(interval
);
3319 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3320 (void) printf(gettext("no pools available\n"));
3324 pool_list_free(list
);
3325 zprop_free_list(cb
.cb_proplist
);
3330 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3332 boolean_t force
= B_FALSE
;
3335 char *poolname
, *old_disk
, *new_disk
;
3336 zpool_handle_t
*zhp
;
3337 zpool_boot_label_t boot_type
;
3342 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3348 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3357 /* get pool name and check number of arguments */
3359 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3366 (void) fprintf(stderr
,
3367 gettext("missing <device> specification\n"));
3375 (void) fprintf(stderr
,
3376 gettext("missing <new_device> specification\n"));
3379 new_disk
= old_disk
;
3389 (void) fprintf(stderr
, gettext("too many arguments\n"));
3393 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3396 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3397 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3403 if (zpool_is_bootable(zhp
))
3404 boot_type
= ZPOOL_COPY_BOOT_LABEL
;
3406 boot_type
= ZPOOL_NO_BOOT_LABEL
;
3408 boot_size
= zpool_get_prop_int(zhp
, ZPOOL_PROP_BOOTSIZE
, NULL
);
3409 nvroot
= make_root_vdev(zhp
, force
, B_FALSE
, replacing
, B_FALSE
,
3410 boot_type
, boot_size
, argc
, argv
);
3411 if (nvroot
== NULL
) {
3416 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3418 nvlist_free(nvroot
);
3425 * zpool replace [-f] <pool> <device> <new_device>
3427 * -f Force attach, even if <new_device> appears to be in use.
3429 * Replace <device> with <new_device>.
3433 zpool_do_replace(int argc
, char **argv
)
3435 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3439 * zpool attach [-f] <pool> <device> <new_device>
3441 * -f Force attach, even if <new_device> appears to be in use.
3443 * Attach <new_device> to the mirror containing <device>. If <device> is not
3444 * part of a mirror, then <device> will be transformed into a mirror of
3445 * <device> and <new_device>. In either case, <new_device> will begin life
3446 * with a DTL of [0, now], and will immediately begin to resilver itself.
3449 zpool_do_attach(int argc
, char **argv
)
3451 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3455 * zpool detach [-f] <pool> <device>
3457 * -f Force detach of <device>, even if DTLs argue against it
3458 * (not supported yet)
3460 * Detach a device from a mirror. The operation will be refused if <device>
3461 * is the last device in the mirror, or if the DTLs indicate that this device
3462 * has the only valid copy of some data.
3466 zpool_do_detach(int argc
, char **argv
)
3469 char *poolname
, *path
;
3470 zpool_handle_t
*zhp
;
3474 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3478 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3487 /* get pool name and check number of arguments */
3489 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3494 (void) fprintf(stderr
,
3495 gettext("missing <device> specification\n"));
3502 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3505 ret
= zpool_vdev_detach(zhp
, path
);
3513 * zpool split [-n] [-o prop=val] ...
3515 * [-R altroot] <pool> <newpool> [<device> ...]
3517 * -n Do not split the pool, but display the resulting layout if
3518 * it were to be split.
3519 * -o Set property=value, or set mount options.
3520 * -R Mount the split-off pool under an alternate root.
3522 * Splits the named pool and gives it the new pool name. Devices to be split
3523 * off may be listed, provided that no more than one device is specified
3524 * per top-level vdev mirror. The newly split pool is left in an exported
3525 * state unless -R is specified.
3527 * Restrictions: the top-level of the pool pool must only be made up of
3528 * mirrors; all devices in the pool must be healthy; no device may be
3529 * undergoing a resilvering operation.
3532 zpool_do_split(int argc
, char **argv
)
3534 char *srcpool
, *newpool
, *propval
;
3535 char *mntopts
= NULL
;
3538 zpool_handle_t
*zhp
;
3539 nvlist_t
*config
, *props
= NULL
;
3541 flags
.dryrun
= B_FALSE
;
3542 flags
.import
= B_FALSE
;
3545 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3548 flags
.import
= B_TRUE
;
3550 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3551 &props
, B_TRUE
) != 0) {
3557 flags
.dryrun
= B_TRUE
;
3560 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3563 if (add_prop_list(optarg
, propval
,
3564 &props
, B_TRUE
) != 0) {
3573 (void) fprintf(stderr
, gettext("missing argument for "
3574 "'%c' option\n"), optopt
);
3578 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3585 if (!flags
.import
&& mntopts
!= NULL
) {
3586 (void) fprintf(stderr
, gettext("setting mntopts is only "
3587 "valid when importing the pool\n"));
3595 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3599 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3609 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3612 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3613 if (config
== NULL
) {
3617 (void) printf(gettext("would create '%s' with the "
3618 "following layout:\n\n"), newpool
);
3619 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3621 nvlist_free(config
);
3626 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3630 * The split was successful. Now we need to open the new
3631 * pool and import it.
3633 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3635 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3636 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3638 (void) fprintf(stderr
, gettext("Split was successful, but "
3639 "the datasets could not all be mounted\n"));
3640 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3641 "different altroot\n"), "zpool import");
3651 * zpool online <pool> <device> ...
3654 zpool_do_online(int argc
, char **argv
)
3658 zpool_handle_t
*zhp
;
3660 vdev_state_t newstate
;
3664 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3667 flags
|= ZFS_ONLINE_EXPAND
;
3671 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3680 /* get pool name and check number of arguments */
3682 (void) fprintf(stderr
, gettext("missing pool name\n"));
3686 (void) fprintf(stderr
, gettext("missing device name\n"));
3692 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3695 for (i
= 1; i
< argc
; i
++) {
3696 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3697 if (newstate
!= VDEV_STATE_HEALTHY
) {
3698 (void) printf(gettext("warning: device '%s' "
3699 "onlined, but remains in faulted state\n"),
3701 if (newstate
== VDEV_STATE_FAULTED
)
3702 (void) printf(gettext("use 'zpool "
3703 "clear' to restore a faulted "
3706 (void) printf(gettext("use 'zpool "
3707 "replace' to replace devices "
3708 "that are no longer present\n"));
3721 * zpool offline [-ft] <pool> <device> ...
3723 * -f Force the device into the offline state, even if doing
3724 * so would appear to compromise pool availability.
3725 * (not supported yet)
3727 * -t Only take the device off-line temporarily. The offline
3728 * state will not be persistent across reboots.
3732 zpool_do_offline(int argc
, char **argv
)
3736 zpool_handle_t
*zhp
;
3738 boolean_t istmp
= B_FALSE
;
3741 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3748 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3757 /* get pool name and check number of arguments */
3759 (void) fprintf(stderr
, gettext("missing pool name\n"));
3763 (void) fprintf(stderr
, gettext("missing device name\n"));
3769 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3772 for (i
= 1; i
< argc
; i
++) {
3773 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3783 * zpool clear <pool> [device]
3785 * Clear all errors associated with a pool or a particular device.
3788 zpool_do_clear(int argc
, char **argv
)
3792 boolean_t dryrun
= B_FALSE
;
3793 boolean_t do_rewind
= B_FALSE
;
3794 boolean_t xtreme_rewind
= B_FALSE
;
3795 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3796 nvlist_t
*policy
= NULL
;
3797 zpool_handle_t
*zhp
;
3798 char *pool
, *device
;
3801 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3810 xtreme_rewind
= B_TRUE
;
3813 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3823 (void) fprintf(stderr
, gettext("missing pool name\n"));
3828 (void) fprintf(stderr
, gettext("too many arguments\n"));
3832 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3833 (void) fprintf(stderr
,
3834 gettext("-n or -X only meaningful with -F\n"));
3838 rewind_policy
= ZPOOL_TRY_REWIND
;
3840 rewind_policy
= ZPOOL_DO_REWIND
;
3842 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3844 /* In future, further rewind policy choices can be passed along here */
3845 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3846 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
3850 device
= argc
== 2 ? argv
[1] : NULL
;
3852 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
3853 nvlist_free(policy
);
3857 if (zpool_clear(zhp
, device
, policy
) != 0)
3862 nvlist_free(policy
);
3868 * zpool reguid <pool>
3871 zpool_do_reguid(int argc
, char **argv
)
3875 zpool_handle_t
*zhp
;
3879 while ((c
= getopt(argc
, argv
, "")) != -1) {
3882 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3891 /* get pool name and check number of arguments */
3893 (void) fprintf(stderr
, gettext("missing pool name\n"));
3898 (void) fprintf(stderr
, gettext("too many arguments\n"));
3903 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3906 ret
= zpool_reguid(zhp
);
3914 * zpool reopen <pool>
3916 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3919 zpool_do_reopen(int argc
, char **argv
)
3923 zpool_handle_t
*zhp
;
3927 while ((c
= getopt(argc
, argv
, "")) != -1) {
3930 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3940 (void) fprintf(stderr
, gettext("missing pool name\n"));
3945 (void) fprintf(stderr
, gettext("too many arguments\n"));
3950 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
3953 ret
= zpool_reopen(zhp
);
3958 typedef struct scrub_cbdata
{
3962 pool_scrub_cmd_t cb_scrub_cmd
;
3966 scrub_callback(zpool_handle_t
*zhp
, void *data
)
3968 scrub_cbdata_t
*cb
= data
;
3972 * Ignore faulted pools.
3974 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
3975 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
3976 "currently unavailable\n"), zpool_get_name(zhp
));
3980 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
3986 * zpool scrub [-s | -p] <pool> ...
3988 * -s Stop. Stops any in-progress scrub.
3989 * -p Pause. Pause in-progress scrub.
3992 zpool_do_scrub(int argc
, char **argv
)
3997 cb
.cb_type
= POOL_SCAN_SCRUB
;
3998 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
4001 while ((c
= getopt(argc
, argv
, "sp")) != -1) {
4004 cb
.cb_type
= POOL_SCAN_NONE
;
4007 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
4010 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4016 if (cb
.cb_type
== POOL_SCAN_NONE
&&
4017 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
4018 (void) fprintf(stderr
, gettext("invalid option combination: "
4019 "-s and -p are mutually exclusive\n"));
4029 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4033 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
4036 typedef struct status_cbdata
{
4038 boolean_t cb_allpools
;
4039 boolean_t cb_verbose
;
4040 boolean_t cb_explain
;
4042 boolean_t cb_dedup_stats
;
4046 * Print out detailed scrub status.
4049 print_scan_status(pool_scan_stat_t
*ps
)
4051 time_t start
, end
, pause
;
4052 uint64_t elapsed
, mins_left
, hours_left
;
4053 uint64_t pass_exam
, examined
, total
;
4055 double fraction_done
;
4056 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4058 (void) printf(gettext(" scan: "));
4060 /* If there's never been a scan, there's not much to say. */
4061 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
4062 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
4063 (void) printf(gettext("none requested\n"));
4067 start
= ps
->pss_start_time
;
4068 end
= ps
->pss_end_time
;
4069 pause
= ps
->pss_pass_scrub_pause
;
4070 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
4072 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
4073 ps
->pss_func
== POOL_SCAN_RESILVER
);
4075 * Scan is finished or canceled.
4077 if (ps
->pss_state
== DSS_FINISHED
) {
4078 uint64_t minutes_taken
= (end
- start
) / 60;
4081 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4082 fmt
= gettext("scrub repaired %s in %lluh%um with "
4083 "%llu errors on %s");
4084 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4085 fmt
= gettext("resilvered %s in %lluh%um with "
4086 "%llu errors on %s");
4089 (void) printf(fmt
, processed_buf
,
4090 (u_longlong_t
)(minutes_taken
/ 60),
4091 (uint_t
)(minutes_taken
% 60),
4092 (u_longlong_t
)ps
->pss_errors
,
4093 ctime((time_t *)&end
));
4095 } else if (ps
->pss_state
== DSS_CANCELED
) {
4096 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4097 (void) printf(gettext("scrub canceled on %s"),
4099 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4100 (void) printf(gettext("resilver canceled on %s"),
4106 assert(ps
->pss_state
== DSS_SCANNING
);
4109 * Scan is in progress.
4111 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4113 (void) printf(gettext("scrub in progress since %s"),
4117 struct tm
*p
= localtime(&pause
);
4118 (void) strftime(buf
, sizeof (buf
), "%a %b %e %T %Y", p
);
4119 (void) printf(gettext("scrub paused since %s\n"), buf
);
4120 (void) printf(gettext("\tscrub started on %s"),
4123 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4124 (void) printf(gettext("resilver in progress since %s"),
4128 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
4129 total
= ps
->pss_to_examine
;
4130 fraction_done
= (double)examined
/ total
;
4132 /* elapsed time for this pass */
4133 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4134 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
4135 elapsed
= elapsed
? elapsed
: 1;
4136 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4137 rate
= pass_exam
/ elapsed
;
4138 rate
= rate
? rate
: 1;
4139 mins_left
= ((total
- examined
) / rate
) / 60;
4140 hours_left
= mins_left
/ 60;
4142 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4143 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4146 * do not print estimated time if hours_left is more than 30 days
4147 * or we have a paused scrub
4150 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4151 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
4152 examined_buf
, total_buf
, rate_buf
);
4153 if (hours_left
< (30 * 24)) {
4154 (void) printf(gettext(", %lluh%um to go\n"),
4155 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4157 (void) printf(gettext(
4158 ", (scan is slow, no estimated time)\n"));
4161 (void) printf(gettext("\t%s scanned out of %s\n"),
4162 examined_buf
, total_buf
);
4165 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4166 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4167 processed_buf
, 100 * fraction_done
);
4168 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4169 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4170 processed_buf
, 100 * fraction_done
);
4175 * Print out detailed removal status.
4178 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
4180 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4182 nvlist_t
*config
, *nvroot
;
4187 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
4191 * Determine name of vdev.
4193 config
= zpool_get_config(zhp
, NULL
);
4194 nvroot
= fnvlist_lookup_nvlist(config
,
4195 ZPOOL_CONFIG_VDEV_TREE
);
4196 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
4197 &child
, &children
) == 0);
4198 assert(prs
->prs_removing_vdev
< children
);
4199 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
4200 child
[prs
->prs_removing_vdev
], B_TRUE
);
4202 (void) printf(gettext("remove: "));
4204 start
= prs
->prs_start_time
;
4205 end
= prs
->prs_end_time
;
4206 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
4209 * Removal is finished or canceled.
4211 if (prs
->prs_state
== DSS_FINISHED
) {
4212 uint64_t minutes_taken
= (end
- start
) / 60;
4214 (void) printf(gettext("Removal of vdev %llu copied %s "
4215 "in %lluh%um, completed on %s"),
4216 (longlong_t
)prs
->prs_removing_vdev
,
4218 (u_longlong_t
)(minutes_taken
/ 60),
4219 (uint_t
)(minutes_taken
% 60),
4220 ctime((time_t *)&end
));
4221 } else if (prs
->prs_state
== DSS_CANCELED
) {
4222 (void) printf(gettext("Removal of %s canceled on %s"),
4223 vdev_name
, ctime(&end
));
4225 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
4226 double fraction_done
;
4229 assert(prs
->prs_state
== DSS_SCANNING
);
4232 * Removal is in progress.
4234 (void) printf(gettext(
4235 "Evacuation of %s in progress since %s"),
4236 vdev_name
, ctime(&start
));
4238 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
4239 total
= prs
->prs_to_copy
;
4240 fraction_done
= (double)copied
/ total
;
4242 /* elapsed time for this pass */
4243 elapsed
= time(NULL
) - prs
->prs_start_time
;
4244 elapsed
= elapsed
> 0 ? elapsed
: 1;
4245 rate
= copied
/ elapsed
;
4246 rate
= rate
> 0 ? rate
: 1;
4247 mins_left
= ((total
- copied
) / rate
) / 60;
4248 hours_left
= mins_left
/ 60;
4250 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
4251 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4252 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4255 * do not print estimated time if hours_left is more than
4258 (void) printf(gettext(" %s copied out of %s at %s/s, "
4260 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
4261 if (hours_left
< (30 * 24)) {
4262 (void) printf(gettext(", %lluh%um to go\n"),
4263 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4265 (void) printf(gettext(
4266 ", (copy is slow, no estimated time)\n"));
4270 if (prs
->prs_mapping_memory
> 0) {
4272 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
4273 (void) printf(gettext(" %s memory used for "
4274 "removed device mappings\n"),
4280 print_error_log(zpool_handle_t
*zhp
)
4282 nvlist_t
*nverrlist
= NULL
;
4285 size_t len
= MAXPATHLEN
* 2;
4287 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4288 (void) printf("errors: List of errors unavailable "
4289 "(insufficient privileges)\n");
4293 (void) printf("errors: Permanent errors have been "
4294 "detected in the following files:\n\n");
4296 pathname
= safe_malloc(len
);
4298 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4300 uint64_t dsobj
, obj
;
4302 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4303 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4305 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4307 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4308 (void) printf("%7s %s\n", "", pathname
);
4311 nvlist_free(nverrlist
);
4315 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4324 (void) printf(gettext("\tspares\n"));
4326 for (i
= 0; i
< nspares
; i
++) {
4327 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
4328 print_status_config(zhp
, name
, spares
[i
],
4329 namewidth
, 2, B_TRUE
);
4335 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4344 (void) printf(gettext("\tcache\n"));
4346 for (i
= 0; i
< nl2cache
; i
++) {
4347 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
4348 print_status_config(zhp
, name
, l2cache
[i
],
4349 namewidth
, 2, B_FALSE
);
4355 print_dedup_stats(nvlist_t
*config
)
4357 ddt_histogram_t
*ddh
;
4363 * If the pool was faulted then we may not have been able to
4364 * obtain the config. Otherwise, if we have anything in the dedup
4365 * table continue processing the stats.
4367 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4368 (uint64_t **)&ddo
, &c
) != 0)
4371 (void) printf("\n");
4372 (void) printf(gettext(" dedup: "));
4373 if (ddo
->ddo_count
== 0) {
4374 (void) printf(gettext("no DDT entries\n"));
4378 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4379 (u_longlong_t
)ddo
->ddo_count
,
4380 (u_longlong_t
)ddo
->ddo_dspace
,
4381 (u_longlong_t
)ddo
->ddo_mspace
);
4383 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4384 (uint64_t **)&dds
, &c
) == 0);
4385 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4386 (uint64_t **)&ddh
, &c
) == 0);
4387 zpool_dump_ddt(dds
, ddh
);
4391 * Display a summary of pool status. Displays a summary such as:
4395 * reason: One or more devices ...
4396 * see: http://illumos.org/msg/ZFS-xxxx-01
4402 * When given the '-v' option, we print out the complete config. If the '-e'
4403 * option is specified, then we print out error rate information as well.
4406 status_callback(zpool_handle_t
*zhp
, void *data
)
4408 status_cbdata_t
*cbp
= data
;
4409 nvlist_t
*config
, *nvroot
;
4416 config
= zpool_get_config(zhp
, NULL
);
4417 reason
= zpool_get_status(zhp
, &msgid
);
4422 * If we were given 'zpool status -x', only report those pools with
4425 if (cbp
->cb_explain
&&
4426 (reason
== ZPOOL_STATUS_OK
||
4427 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4428 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4429 if (!cbp
->cb_allpools
) {
4430 (void) printf(gettext("pool '%s' is healthy\n"),
4431 zpool_get_name(zhp
));
4433 cbp
->cb_first
= B_FALSE
;
4439 cbp
->cb_first
= B_FALSE
;
4441 (void) printf("\n");
4443 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
4444 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4445 (uint64_t **)&vs
, &c
) == 0);
4446 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4448 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4449 (void) printf(gettext(" state: %s\n"), health
);
4452 case ZPOOL_STATUS_MISSING_DEV_R
:
4453 (void) printf(gettext("status: One or more devices could not "
4454 "be opened. Sufficient replicas exist for\n\tthe pool to "
4455 "continue functioning in a degraded state.\n"));
4456 (void) printf(gettext("action: Attach the missing device and "
4457 "online it using 'zpool online'.\n"));
4460 case ZPOOL_STATUS_MISSING_DEV_NR
:
4461 (void) printf(gettext("status: One or more devices could not "
4462 "be opened. There are insufficient\n\treplicas for the "
4463 "pool to continue functioning.\n"));
4464 (void) printf(gettext("action: Attach the missing device and "
4465 "online it using 'zpool online'.\n"));
4468 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4469 (void) printf(gettext("status: One or more devices could not "
4470 "be used because the label is missing or\n\tinvalid. "
4471 "Sufficient replicas exist for the pool to continue\n\t"
4472 "functioning in a degraded state.\n"));
4473 (void) printf(gettext("action: Replace the device using "
4474 "'zpool replace'.\n"));
4477 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4478 (void) printf(gettext("status: One or more devices could not "
4479 "be used because the label is missing \n\tor invalid. "
4480 "There are insufficient replicas for the pool to "
4481 "continue\n\tfunctioning.\n"));
4482 zpool_explain_recover(zpool_get_handle(zhp
),
4483 zpool_get_name(zhp
), reason
, config
);
4486 case ZPOOL_STATUS_FAILING_DEV
:
4487 (void) printf(gettext("status: One or more devices has "
4488 "experienced an unrecoverable error. An\n\tattempt was "
4489 "made to correct the error. Applications are "
4491 (void) printf(gettext("action: Determine if the device needs "
4492 "to be replaced, and clear the errors\n\tusing "
4493 "'zpool clear' or replace the device with 'zpool "
4497 case ZPOOL_STATUS_OFFLINE_DEV
:
4498 (void) printf(gettext("status: One or more devices has "
4499 "been taken offline by the administrator.\n\tSufficient "
4500 "replicas exist for the pool to continue functioning in "
4501 "a\n\tdegraded state.\n"));
4502 (void) printf(gettext("action: Online the device using "
4503 "'zpool online' or replace the device with\n\t'zpool "
4507 case ZPOOL_STATUS_REMOVED_DEV
:
4508 (void) printf(gettext("status: One or more devices has "
4509 "been removed by the administrator.\n\tSufficient "
4510 "replicas exist for the pool to continue functioning in "
4511 "a\n\tdegraded state.\n"));
4512 (void) printf(gettext("action: Online the device using "
4513 "'zpool online' or replace the device with\n\t'zpool "
4517 case ZPOOL_STATUS_RESILVERING
:
4518 (void) printf(gettext("status: One or more devices is "
4519 "currently being resilvered. The pool will\n\tcontinue "
4520 "to function, possibly in a degraded state.\n"));
4521 (void) printf(gettext("action: Wait for the resilver to "
4525 case ZPOOL_STATUS_CORRUPT_DATA
:
4526 (void) printf(gettext("status: One or more devices has "
4527 "experienced an error resulting in data\n\tcorruption. "
4528 "Applications may be affected.\n"));
4529 (void) printf(gettext("action: Restore the file in question "
4530 "if possible. Otherwise restore the\n\tentire pool from "
4534 case ZPOOL_STATUS_CORRUPT_POOL
:
4535 (void) printf(gettext("status: The pool metadata is corrupted "
4536 "and the pool cannot be opened.\n"));
4537 zpool_explain_recover(zpool_get_handle(zhp
),
4538 zpool_get_name(zhp
), reason
, config
);
4541 case ZPOOL_STATUS_VERSION_OLDER
:
4542 (void) printf(gettext("status: The pool is formatted using a "
4543 "legacy on-disk format. The pool can\n\tstill be used, "
4544 "but some features are unavailable.\n"));
4545 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4546 "upgrade'. Once this is done, the\n\tpool will no longer "
4547 "be accessible on software that does not support feature\n"
4551 case ZPOOL_STATUS_VERSION_NEWER
:
4552 (void) printf(gettext("status: The pool has been upgraded to a "
4553 "newer, incompatible on-disk version.\n\tThe pool cannot "
4554 "be accessed on this system.\n"));
4555 (void) printf(gettext("action: Access the pool from a system "
4556 "running more recent software, or\n\trestore the pool from "
4560 case ZPOOL_STATUS_FEAT_DISABLED
:
4561 (void) printf(gettext("status: Some supported features are not "
4562 "enabled on the pool. The pool can\n\tstill be used, but "
4563 "some features are unavailable.\n"));
4564 (void) printf(gettext("action: Enable all features using "
4565 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4566 "longer be accessible by software that does not support\n\t"
4567 "the features. See zpool-features(5) for details.\n"));
4570 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4571 (void) printf(gettext("status: The pool cannot be accessed on "
4572 "this system because it uses the\n\tfollowing feature(s) "
4573 "not supported on this system:\n"));
4574 zpool_print_unsup_feat(config
);
4575 (void) printf("\n");
4576 (void) printf(gettext("action: Access the pool from a system "
4577 "that supports the required feature(s),\n\tor restore the "
4578 "pool from backup.\n"));
4581 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4582 (void) printf(gettext("status: The pool can only be accessed "
4583 "in read-only mode on this system. It\n\tcannot be "
4584 "accessed in read-write mode because it uses the "
4585 "following\n\tfeature(s) not supported on this system:\n"));
4586 zpool_print_unsup_feat(config
);
4587 (void) printf("\n");
4588 (void) printf(gettext("action: The pool cannot be accessed in "
4589 "read-write mode. Import the pool with\n"
4590 "\t\"-o readonly=on\", access the pool from a system that "
4591 "supports the\n\trequired feature(s), or restore the "
4592 "pool from backup.\n"));
4595 case ZPOOL_STATUS_FAULTED_DEV_R
:
4596 (void) printf(gettext("status: One or more devices are "
4597 "faulted in response to persistent errors.\n\tSufficient "
4598 "replicas exist for the pool to continue functioning "
4599 "in a\n\tdegraded state.\n"));
4600 (void) printf(gettext("action: Replace the faulted device, "
4601 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4604 case ZPOOL_STATUS_FAULTED_DEV_NR
:
4605 (void) printf(gettext("status: One or more devices are "
4606 "faulted in response to persistent errors. There are "
4607 "insufficient replicas for the pool to\n\tcontinue "
4609 (void) printf(gettext("action: Destroy and re-create the pool "
4610 "from a backup source. Manually marking the device\n"
4611 "\trepaired using 'zpool clear' may allow some data "
4612 "to be recovered.\n"));
4615 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
4616 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
4617 (void) printf(gettext("status: One or more devices are "
4618 "faulted in response to IO failures.\n"));
4619 (void) printf(gettext("action: Make sure the affected devices "
4620 "are connected, then run 'zpool clear'.\n"));
4623 case ZPOOL_STATUS_BAD_LOG
:
4624 (void) printf(gettext("status: An intent log record "
4625 "could not be read.\n"
4626 "\tWaiting for adminstrator intervention to fix the "
4627 "faulted pool.\n"));
4628 (void) printf(gettext("action: Either restore the affected "
4629 "device(s) and run 'zpool online',\n"
4630 "\tor ignore the intent log records by running "
4631 "'zpool clear'.\n"));
4636 * The remaining errors can't actually be generated, yet.
4638 assert(reason
== ZPOOL_STATUS_OK
);
4642 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4645 if (config
!= NULL
) {
4648 nvlist_t
**spares
, **l2cache
;
4649 uint_t nspares
, nl2cache
;
4650 pool_scan_stat_t
*ps
= NULL
;
4651 pool_removal_stat_t
*prs
= NULL
;
4653 (void) nvlist_lookup_uint64_array(nvroot
,
4654 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
4655 print_scan_status(ps
);
4657 (void) nvlist_lookup_uint64_array(nvroot
,
4658 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
4659 print_removal_status(zhp
, prs
);
4661 namewidth
= max_width(zhp
, nvroot
, 0, 0);
4665 (void) printf(gettext("config:\n\n"));
4666 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
4667 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4668 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
4669 namewidth
, 0, B_FALSE
);
4671 if (num_logs(nvroot
) > 0)
4672 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
4673 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4674 &l2cache
, &nl2cache
) == 0)
4675 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
4677 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4678 &spares
, &nspares
) == 0)
4679 print_spares(zhp
, spares
, nspares
, namewidth
);
4681 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
4683 nvlist_t
*nverrlist
= NULL
;
4686 * If the approximate error count is small, get a
4687 * precise count by fetching the entire log and
4688 * uniquifying the results.
4690 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
4691 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
4696 while ((elem
= nvlist_next_nvpair(nverrlist
,
4701 nvlist_free(nverrlist
);
4703 (void) printf("\n");
4706 (void) printf(gettext("errors: No known data "
4708 else if (!cbp
->cb_verbose
)
4709 (void) printf(gettext("errors: %llu data "
4710 "errors, use '-v' for a list\n"),
4711 (u_longlong_t
)nerr
);
4713 print_error_log(zhp
);
4716 if (cbp
->cb_dedup_stats
)
4717 print_dedup_stats(config
);
4719 (void) printf(gettext("config: The configuration cannot be "
4727 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4729 * -v Display complete error logs
4730 * -x Display only pools with potential problems
4731 * -D Display dedup status (undocumented)
4732 * -T Display a timestamp in date(1) or Unix format
4734 * Describes the health status of all pools or some subset.
4737 zpool_do_status(int argc
, char **argv
)
4741 unsigned long interval
= 0, count
= 0;
4742 status_cbdata_t cb
= { 0 };
4745 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
4748 cb
.cb_verbose
= B_TRUE
;
4751 cb
.cb_explain
= B_TRUE
;
4754 cb
.cb_dedup_stats
= B_TRUE
;
4757 get_timestamp_arg(*optarg
);
4760 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4769 get_interval_count(&argc
, argv
, &interval
, &count
);
4772 cb
.cb_allpools
= B_TRUE
;
4774 cb
.cb_first
= B_TRUE
;
4777 if (timestamp_fmt
!= NODATE
)
4778 print_timestamp(timestamp_fmt
);
4780 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
4781 status_callback
, &cb
);
4783 if (argc
== 0 && cb
.cb_count
== 0)
4784 (void) printf(gettext("no pools available\n"));
4785 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
4786 (void) printf(gettext("all pools are healthy\n"));
4794 if (count
!= 0 && --count
== 0)
4797 (void) sleep(interval
);
4803 typedef struct upgrade_cbdata
{
4806 uint64_t cb_version
;
4811 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
4815 uint64_t oldversion
;
4817 config
= zpool_get_config(zhp
, NULL
);
4818 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4821 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
4822 assert(oldversion
< version
);
4824 ret
= zpool_upgrade(zhp
, version
);
4828 if (version
>= SPA_VERSION_FEATURES
) {
4829 (void) printf(gettext("Successfully upgraded "
4830 "'%s' from version %llu to feature flags.\n"),
4831 zpool_get_name(zhp
), oldversion
);
4833 (void) printf(gettext("Successfully upgraded "
4834 "'%s' from version %llu to version %llu.\n"),
4835 zpool_get_name(zhp
), oldversion
, version
);
4842 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
4845 boolean_t firstff
= B_TRUE
;
4846 nvlist_t
*enabled
= zpool_get_features(zhp
);
4849 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4850 const char *fname
= spa_feature_table
[i
].fi_uname
;
4851 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4852 if (!nvlist_exists(enabled
, fguid
)) {
4854 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
4855 ret
= zpool_set_prop(zhp
, propname
,
4856 ZFS_FEATURE_ENABLED
);
4864 (void) printf(gettext("Enabled the "
4865 "following features on '%s':\n"),
4866 zpool_get_name(zhp
));
4869 (void) printf(gettext(" %s\n"), fname
);
4880 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
4882 upgrade_cbdata_t
*cbp
= arg
;
4885 boolean_t printnl
= B_FALSE
;
4888 config
= zpool_get_config(zhp
, NULL
);
4889 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4892 assert(SPA_VERSION_IS_SUPPORTED(version
));
4894 if (version
< cbp
->cb_version
) {
4895 cbp
->cb_first
= B_FALSE
;
4896 ret
= upgrade_version(zhp
, cbp
->cb_version
);
4902 * If they did "zpool upgrade -a", then we could
4903 * be doing ioctls to different pools. We need
4904 * to log this history once to each pool, and bypass
4905 * the normal history logging that happens in main().
4907 (void) zpool_log_history(g_zfs
, history_str
);
4908 log_history
= B_FALSE
;
4911 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
4913 ret
= upgrade_enable_all(zhp
, &count
);
4918 cbp
->cb_first
= B_FALSE
;
4924 (void) printf(gettext("\n"));
4931 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
4933 upgrade_cbdata_t
*cbp
= arg
;
4937 config
= zpool_get_config(zhp
, NULL
);
4938 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4941 assert(SPA_VERSION_IS_SUPPORTED(version
));
4943 if (version
< SPA_VERSION_FEATURES
) {
4944 if (cbp
->cb_first
) {
4945 (void) printf(gettext("The following pools are "
4946 "formatted with legacy version numbers and can\n"
4947 "be upgraded to use feature flags. After "
4948 "being upgraded, these pools\nwill no "
4949 "longer be accessible by software that does not "
4950 "support feature\nflags.\n\n"));
4951 (void) printf(gettext("VER POOL\n"));
4952 (void) printf(gettext("--- ------------\n"));
4953 cbp
->cb_first
= B_FALSE
;
4956 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
4957 zpool_get_name(zhp
));
4964 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
4966 upgrade_cbdata_t
*cbp
= arg
;
4970 config
= zpool_get_config(zhp
, NULL
);
4971 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
4974 if (version
>= SPA_VERSION_FEATURES
) {
4976 boolean_t poolfirst
= B_TRUE
;
4977 nvlist_t
*enabled
= zpool_get_features(zhp
);
4979 for (i
= 0; i
< SPA_FEATURES
; i
++) {
4980 const char *fguid
= spa_feature_table
[i
].fi_guid
;
4981 const char *fname
= spa_feature_table
[i
].fi_uname
;
4982 if (!nvlist_exists(enabled
, fguid
)) {
4983 if (cbp
->cb_first
) {
4984 (void) printf(gettext("\nSome "
4985 "supported features are not "
4986 "enabled on the following pools. "
4987 "Once a\nfeature is enabled the "
4988 "pool may become incompatible with "
4989 "software\nthat does not support "
4991 "zpool-features(5) for "
4993 (void) printf(gettext("POOL "
4995 (void) printf(gettext("------"
4997 cbp
->cb_first
= B_FALSE
;
5001 (void) printf(gettext("%s\n"),
5002 zpool_get_name(zhp
));
5003 poolfirst
= B_FALSE
;
5006 (void) printf(gettext(" %s\n"), fname
);
5016 upgrade_one(zpool_handle_t
*zhp
, void *data
)
5018 boolean_t printnl
= B_FALSE
;
5019 upgrade_cbdata_t
*cbp
= data
;
5020 uint64_t cur_version
;
5023 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
5024 (void) printf(gettext("'log' is now a reserved word\n"
5025 "Pool 'log' must be renamed using export and import"
5030 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5031 if (cur_version
> cbp
->cb_version
) {
5032 (void) printf(gettext("Pool '%s' is already formatted "
5033 "using more current version '%llu'.\n\n"),
5034 zpool_get_name(zhp
), cur_version
);
5038 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
5039 (void) printf(gettext("Pool '%s' is already formatted "
5040 "using version %llu.\n\n"), zpool_get_name(zhp
),
5045 if (cur_version
!= cbp
->cb_version
) {
5047 ret
= upgrade_version(zhp
, cbp
->cb_version
);
5052 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
5054 ret
= upgrade_enable_all(zhp
, &count
);
5060 } else if (cur_version
== SPA_VERSION
) {
5061 (void) printf(gettext("Pool '%s' already has all "
5062 "supported features enabled.\n"),
5063 zpool_get_name(zhp
));
5068 (void) printf(gettext("\n"));
5077 * zpool upgrade [-V version] <-a | pool ...>
5079 * With no arguments, display downrev'd ZFS pool available for upgrade.
5080 * Individual pools can be upgraded by specifying the pool, and '-a' will
5081 * upgrade all pools.
5084 zpool_do_upgrade(int argc
, char **argv
)
5087 upgrade_cbdata_t cb
= { 0 };
5089 boolean_t showversions
= B_FALSE
;
5090 boolean_t upgradeall
= B_FALSE
;
5095 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
5098 upgradeall
= B_TRUE
;
5101 showversions
= B_TRUE
;
5104 cb
.cb_version
= strtoll(optarg
, &end
, 10);
5106 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
5107 (void) fprintf(stderr
,
5108 gettext("invalid version '%s'\n"), optarg
);
5113 (void) fprintf(stderr
, gettext("missing argument for "
5114 "'%c' option\n"), optopt
);
5118 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5129 if (cb
.cb_version
== 0) {
5130 cb
.cb_version
= SPA_VERSION
;
5131 } else if (!upgradeall
&& argc
== 0) {
5132 (void) fprintf(stderr
, gettext("-V option is "
5133 "incompatible with other arguments\n"));
5138 if (upgradeall
|| argc
!= 0) {
5139 (void) fprintf(stderr
, gettext("-v option is "
5140 "incompatible with other arguments\n"));
5143 } else if (upgradeall
) {
5145 (void) fprintf(stderr
, gettext("-a option should not "
5146 "be used along with a pool name\n"));
5151 (void) printf(gettext("This system supports ZFS pool feature "
5156 (void) printf(gettext("The following features are "
5158 (void) printf(gettext("FEAT DESCRIPTION\n"));
5159 (void) printf("----------------------------------------------"
5160 "---------------\n");
5161 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5162 zfeature_info_t
*fi
= &spa_feature_table
[i
];
5164 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
5165 " (read-only compatible)" : "";
5167 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
5168 (void) printf(" %s\n", fi
->fi_desc
);
5170 (void) printf("\n");
5172 (void) printf(gettext("The following legacy versions are also "
5174 (void) printf(gettext("VER DESCRIPTION\n"));
5175 (void) printf("--- -----------------------------------------"
5176 "---------------\n");
5177 (void) printf(gettext(" 1 Initial ZFS version\n"));
5178 (void) printf(gettext(" 2 Ditto blocks "
5179 "(replicated metadata)\n"));
5180 (void) printf(gettext(" 3 Hot spares and double parity "
5182 (void) printf(gettext(" 4 zpool history\n"));
5183 (void) printf(gettext(" 5 Compression using the gzip "
5185 (void) printf(gettext(" 6 bootfs pool property\n"));
5186 (void) printf(gettext(" 7 Separate intent log devices\n"));
5187 (void) printf(gettext(" 8 Delegated administration\n"));
5188 (void) printf(gettext(" 9 refquota and refreservation "
5190 (void) printf(gettext(" 10 Cache devices\n"));
5191 (void) printf(gettext(" 11 Improved scrub performance\n"));
5192 (void) printf(gettext(" 12 Snapshot properties\n"));
5193 (void) printf(gettext(" 13 snapused property\n"));
5194 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5195 (void) printf(gettext(" 15 user/group space accounting\n"));
5196 (void) printf(gettext(" 16 stmf property support\n"));
5197 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5198 (void) printf(gettext(" 18 Snapshot user holds\n"));
5199 (void) printf(gettext(" 19 Log device removal\n"));
5200 (void) printf(gettext(" 20 Compression using zle "
5201 "(zero-length encoding)\n"));
5202 (void) printf(gettext(" 21 Deduplication\n"));
5203 (void) printf(gettext(" 22 Received properties\n"));
5204 (void) printf(gettext(" 23 Slim ZIL\n"));
5205 (void) printf(gettext(" 24 System attributes\n"));
5206 (void) printf(gettext(" 25 Improved scrub stats\n"));
5207 (void) printf(gettext(" 26 Improved snapshot deletion "
5209 (void) printf(gettext(" 27 Improved snapshot creation "
5211 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5212 (void) printf(gettext("\nFor more information on a particular "
5213 "version, including supported releases,\n"));
5214 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5215 } else if (argc
== 0 && upgradeall
) {
5216 cb
.cb_first
= B_TRUE
;
5217 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5218 if (ret
== 0 && cb
.cb_first
) {
5219 if (cb
.cb_version
== SPA_VERSION
) {
5220 (void) printf(gettext("All pools are already "
5221 "formatted using feature flags.\n\n"));
5222 (void) printf(gettext("Every feature flags "
5223 "pool already has all supported features "
5226 (void) printf(gettext("All pools are already "
5227 "formatted with version %llu or higher.\n"),
5231 } else if (argc
== 0) {
5232 cb
.cb_first
= B_TRUE
;
5233 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5237 (void) printf(gettext("All pools are formatted "
5238 "using feature flags.\n\n"));
5240 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5241 "for a list of available legacy versions.\n"));
5244 cb
.cb_first
= B_TRUE
;
5245 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5249 (void) printf(gettext("Every feature flags pool has "
5250 "all supported features enabled.\n"));
5252 (void) printf(gettext("\n"));
5255 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5262 typedef struct hist_cbdata
{
5269 * Print out the command history for a specific pool.
5272 get_history_one(zpool_handle_t
*zhp
, void *data
)
5278 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5280 cb
->first
= B_FALSE
;
5282 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5284 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5287 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5288 &records
, &numrecords
) == 0);
5289 for (i
= 0; i
< numrecords
; i
++) {
5290 nvlist_t
*rec
= records
[i
];
5293 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5297 tsec
= fnvlist_lookup_uint64(records
[i
],
5299 (void) localtime_r(&tsec
, &t
);
5300 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5303 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5304 (void) printf("%s %s", tbuf
,
5305 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5306 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5308 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5311 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5312 (void) printf("%s unrecognized record:\n",
5314 dump_nvlist(rec
, 4);
5317 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5318 zfs_history_event_names
[ievent
],
5319 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
5320 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5321 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5324 (void) printf("%s [txg:%lld] %s", tbuf
,
5325 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
5326 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5327 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5328 (void) printf(" %s (%llu)",
5329 fnvlist_lookup_string(rec
,
5331 fnvlist_lookup_uint64(rec
,
5334 (void) printf(" %s", fnvlist_lookup_string(rec
,
5335 ZPOOL_HIST_INT_STR
));
5336 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5339 (void) printf("%s ioctl %s\n", tbuf
,
5340 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5341 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5342 (void) printf(" input:\n");
5343 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5344 ZPOOL_HIST_INPUT_NVL
), 8);
5346 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5347 (void) printf(" output:\n");
5348 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5349 ZPOOL_HIST_OUTPUT_NVL
), 8);
5351 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
5352 (void) printf(" errno: %lld\n",
5353 fnvlist_lookup_int64(rec
,
5359 (void) printf("%s unrecognized record:\n", tbuf
);
5360 dump_nvlist(rec
, 4);
5364 (void) printf("\n");
5367 (void) printf(" [");
5368 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5369 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5370 struct passwd
*pwd
= getpwuid(who
);
5371 (void) printf("user %d ", (int)who
);
5373 (void) printf("(%s) ", pwd
->pw_name
);
5375 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5376 (void) printf("on %s",
5377 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5379 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5380 (void) printf(":%s",
5381 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5384 (void) printf("\n");
5386 (void) printf("\n");
5393 * zpool history <pool>
5395 * Displays the history of commands that modified pools.
5398 zpool_do_history(int argc
, char **argv
)
5400 hist_cbdata_t cbdata
= { 0 };
5404 cbdata
.first
= B_TRUE
;
5406 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5409 cbdata
.longfmt
= B_TRUE
;
5412 cbdata
.internal
= B_TRUE
;
5415 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5423 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5426 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5427 (void) printf(gettext("no pools available\n"));
5435 get_callback(zpool_handle_t
*zhp
, void *data
)
5437 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5438 char value
[MAXNAMELEN
];
5439 zprop_source_t srctype
;
5442 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5445 * Skip the special fake placeholder. This will also skip
5446 * over the name property when 'all' is specified.
5448 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5449 pl
== cbp
->cb_proplist
)
5452 if (pl
->pl_prop
== ZPROP_INVAL
&&
5453 (zpool_prop_feature(pl
->pl_user_prop
) ||
5454 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5455 srctype
= ZPROP_SRC_LOCAL
;
5457 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5458 value
, sizeof (value
)) == 0) {
5459 zprop_print_one_property(zpool_get_name(zhp
),
5460 cbp
, pl
->pl_user_prop
, value
, srctype
,
5464 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
5465 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5468 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5469 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5477 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5479 * -H Scripted mode. Don't display headers, and separate properties
5481 * -o List of columns to display. Defaults to
5482 * "name,property,value,source".
5483 * -p Diplay values in parsable (exact) format.
5485 * Get properties of pools in the system. Output space statistics
5486 * for each one as well as other attributes.
5489 zpool_do_get(int argc
, char **argv
)
5491 zprop_get_cbdata_t cb
= { 0 };
5492 zprop_list_t fake_name
= { 0 };
5497 cb
.cb_first
= B_TRUE
;
5500 * Set up default columns and sources.
5502 cb
.cb_sources
= ZPROP_SRC_ALL
;
5503 cb
.cb_columns
[0] = GET_COL_NAME
;
5504 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5505 cb
.cb_columns
[2] = GET_COL_VALUE
;
5506 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5507 cb
.cb_type
= ZFS_TYPE_POOL
;
5510 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
5513 cb
.cb_literal
= B_TRUE
;
5516 cb
.cb_scripted
= B_TRUE
;
5519 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
5521 while (*optarg
!= '\0') {
5522 static char *col_subopts
[] =
5523 { "name", "property", "value", "source",
5526 if (i
== ZFS_GET_NCOLS
) {
5527 (void) fprintf(stderr
, gettext("too "
5528 "many fields given to -o "
5533 switch (getsubopt(&optarg
, col_subopts
,
5536 cb
.cb_columns
[i
++] = GET_COL_NAME
;
5539 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
5542 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
5545 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
5549 (void) fprintf(stderr
,
5550 gettext("\"all\" conflicts "
5551 "with specific fields "
5552 "given to -o option\n"));
5555 cb
.cb_columns
[0] = GET_COL_NAME
;
5556 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5557 cb
.cb_columns
[2] = GET_COL_VALUE
;
5558 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5562 (void) fprintf(stderr
,
5563 gettext("invalid column name "
5570 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5580 (void) fprintf(stderr
, gettext("missing property "
5585 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
5586 ZFS_TYPE_POOL
) != 0)
5592 if (cb
.cb_proplist
!= NULL
) {
5593 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5594 fake_name
.pl_width
= strlen(gettext("NAME"));
5595 fake_name
.pl_next
= cb
.cb_proplist
;
5596 cb
.cb_proplist
= &fake_name
;
5599 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
5602 if (cb
.cb_proplist
== &fake_name
)
5603 zprop_free_list(fake_name
.pl_next
);
5605 zprop_free_list(cb
.cb_proplist
);
5610 typedef struct set_cbdata
{
5613 boolean_t cb_any_successful
;
5617 set_callback(zpool_handle_t
*zhp
, void *data
)
5620 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
5622 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
5625 cb
->cb_any_successful
= B_TRUE
;
5631 zpool_do_set(int argc
, char **argv
)
5633 set_cbdata_t cb
= { 0 };
5636 if (argc
> 1 && argv
[1][0] == '-') {
5637 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5643 (void) fprintf(stderr
, gettext("missing property=value "
5649 (void) fprintf(stderr
, gettext("missing pool name\n"));
5654 (void) fprintf(stderr
, gettext("too many pool names\n"));
5658 cb
.cb_propname
= argv
[1];
5659 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
5660 if (cb
.cb_value
== NULL
) {
5661 (void) fprintf(stderr
, gettext("missing value in "
5662 "property=value argument\n"));
5666 *(cb
.cb_value
) = '\0';
5669 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
5676 find_command_idx(char *command
, int *idx
)
5680 for (i
= 0; i
< NCOMMAND
; i
++) {
5681 if (command_table
[i
].name
== NULL
)
5684 if (strcmp(command
, command_table
[i
].name
) == 0) {
5693 main(int argc
, char **argv
)
5699 (void) setlocale(LC_ALL
, "");
5700 (void) textdomain(TEXT_DOMAIN
);
5702 if ((g_zfs
= libzfs_init()) == NULL
) {
5703 (void) fprintf(stderr
, gettext("internal error: failed to "
5704 "initialize ZFS library\n"));
5708 libzfs_print_on_error(g_zfs
, B_TRUE
);
5713 * Make sure the user has specified some command.
5716 (void) fprintf(stderr
, gettext("missing command\n"));
5725 if (strcmp(cmdname
, "-?") == 0)
5728 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
5731 * Run the appropriate command.
5733 if (find_command_idx(cmdname
, &i
) == 0) {
5734 current_command
= &command_table
[i
];
5735 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
5736 } else if (strchr(cmdname
, '=')) {
5737 verify(find_command_idx("set", &i
) == 0);
5738 current_command
= &command_table
[i
];
5739 ret
= command_table
[i
].func(argc
, argv
);
5740 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
5742 * 'freeze' is a vile debugging abomination, so we treat
5746 int fd
= open(ZFS_DEV
, O_RDWR
);
5747 (void) strcpy((void *)buf
, argv
[2]);
5748 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
5750 (void) fprintf(stderr
, gettext("unrecognized "
5751 "command '%s'\n"), cmdname
);
5755 if (ret
== 0 && log_history
)
5756 (void) zpool_log_history(g_zfs
, history_str
);
5761 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5762 * for the purposes of running ::findleaks.
5764 if (getenv("ZFS_ABORT") != NULL
) {
5765 (void) printf("dumping core by request\n");