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.
51 #include <sys/fs/zfs.h>
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 #include "statcommon.h"
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
69 static int zpool_do_checkpoint(int, char **);
71 static int zpool_do_list(int, char **);
72 static int zpool_do_iostat(int, char **);
73 static int zpool_do_status(int, char **);
75 static int zpool_do_online(int, char **);
76 static int zpool_do_offline(int, char **);
77 static int zpool_do_clear(int, char **);
78 static int zpool_do_reopen(int, char **);
80 static int zpool_do_reguid(int, char **);
82 static int zpool_do_attach(int, char **);
83 static int zpool_do_detach(int, char **);
84 static int zpool_do_replace(int, char **);
85 static int zpool_do_split(int, char **);
87 static int zpool_do_initialize(int, char **);
88 static int zpool_do_scrub(int, char **);
90 static int zpool_do_import(int, char **);
91 static int zpool_do_export(int, char **);
93 static int zpool_do_upgrade(int, char **);
95 static int zpool_do_history(int, char **);
97 static int zpool_do_get(int, char **);
98 static int zpool_do_set(int, char **);
101 * These libumem hooks provide a reasonable set of defaults for the allocator's
102 * debugging facilities.
107 _umem_debug_init(void)
109 return ("default,verbose"); /* $UMEM_DEBUG setting */
113 _umem_logging_init(void)
115 return ("fail,contents"); /* $UMEM_LOGGING setting */
149 typedef struct zpool_command
{
151 int (*func
)(int, char **);
156 * Master command table. Each ZFS command has a name, associated function, and
157 * usage message. The usage messages need to be internationalized, so we have
158 * to have a function to return the usage message based on a command index.
160 * These commands are organized according to how they are displayed in the usage
161 * message. An empty command (one with a NULL name) indicates an empty line in
162 * the generic usage message.
164 static zpool_command_t command_table
[] = {
165 { "create", zpool_do_create
, HELP_CREATE
},
166 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
168 { "add", zpool_do_add
, HELP_ADD
},
169 { "remove", zpool_do_remove
, HELP_REMOVE
},
171 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
173 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
175 { "list", zpool_do_list
, HELP_LIST
},
176 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
177 { "status", zpool_do_status
, HELP_STATUS
},
179 { "online", zpool_do_online
, HELP_ONLINE
},
180 { "offline", zpool_do_offline
, HELP_OFFLINE
},
181 { "clear", zpool_do_clear
, HELP_CLEAR
},
182 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
184 { "attach", zpool_do_attach
, HELP_ATTACH
},
185 { "detach", zpool_do_detach
, HELP_DETACH
},
186 { "replace", zpool_do_replace
, HELP_REPLACE
},
187 { "split", zpool_do_split
, HELP_SPLIT
},
189 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
190 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
192 { "import", zpool_do_import
, HELP_IMPORT
},
193 { "export", zpool_do_export
, HELP_EXPORT
},
194 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
195 { "reguid", zpool_do_reguid
, HELP_REGUID
},
197 { "history", zpool_do_history
, HELP_HISTORY
},
198 { "get", zpool_do_get
, HELP_GET
},
199 { "set", zpool_do_set
, HELP_SET
},
202 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
204 static zpool_command_t
*current_command
;
205 static char history_str
[HIS_MAX_RECORD_LEN
];
206 static boolean_t log_history
= B_TRUE
;
207 static uint_t timestamp_fmt
= NODATE
;
210 get_usage(zpool_help_t idx
)
214 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
216 return (gettext("\tattach [-f] <pool> <device> "
219 return (gettext("\tclear [-nF] <pool> [device]\n"));
221 return (gettext("\tcreate [-fnd] [-B] "
222 "[-o property=value] ... \n"
223 "\t [-O file-system-property=value] ...\n"
224 "\t [-m mountpoint] [-R root] [-t tempname] "
225 "<pool> <vdev> ...\n"));
226 case HELP_CHECKPOINT
:
227 return (gettext("\tcheckpoint [--discard] <pool> ...\n"));
229 return (gettext("\tdestroy [-f] <pool>\n"));
231 return (gettext("\tdetach <pool> <device>\n"));
233 return (gettext("\texport [-f] <pool> ...\n"));
235 return (gettext("\thistory [-il] [<pool>] ...\n"));
237 return (gettext("\timport [-d dir] [-D]\n"
238 "\timport [-o mntopts] [-o property=value] ... \n"
239 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
240 "[-R root] [-F [-n]] -a\n"
241 "\timport [-o mntopts] [-o property=value] ... \n"
242 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
243 "[-R root] [-F [-n]] [-t]\n"
244 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
246 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
248 case HELP_LABELCLEAR
:
249 return (gettext("\tlabelclear [-f] <vdev>\n"));
251 return (gettext("\tlist [-Hp] [-o property[,...]] "
252 "[-T d|u] [pool] ... [interval [count]]\n"));
254 return (gettext("\toffline [-t] <pool> <device> ...\n"));
256 return (gettext("\tonline <pool> <device> ...\n"));
258 return (gettext("\treplace [-f] <pool> <device> "
261 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
263 return (gettext("\treopen <pool>\n"));
264 case HELP_INITIALIZE
:
265 return (gettext("\tinitialize [-cs] <pool> [<device> ...]\n"));
267 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
269 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
272 return (gettext("\tupgrade\n"
274 "\tupgrade [-V version] <-a | pool ...>\n"));
276 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
277 "<\"all\" | property[,...]> <pool> ...\n"));
279 return (gettext("\tset <property=value> <pool> \n"));
281 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
282 "\t [-o property=value] <pool> <newpool> "
283 "[<device> ...]\n"));
285 return (gettext("\treguid <pool>\n"));
294 * Callback routine that will print out a pool property value.
297 print_prop_cb(int prop
, void *cb
)
301 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
303 if (zpool_prop_readonly(prop
))
304 (void) fprintf(fp
, " NO ");
306 (void) fprintf(fp
, " YES ");
308 if (zpool_prop_values(prop
) == NULL
)
309 (void) fprintf(fp
, "-\n");
311 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
317 * Display usage message. If we're inside a command, display only the usage for
318 * that command. Otherwise, iterate over the entire command table and display
319 * a complete usage message.
322 usage(boolean_t requested
)
324 FILE *fp
= requested
? stdout
: stderr
;
326 if (current_command
== NULL
) {
329 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
331 gettext("where 'command' is one of the following:\n\n"));
333 for (i
= 0; i
< NCOMMAND
; i
++) {
334 if (command_table
[i
].name
== NULL
)
335 (void) fprintf(fp
, "\n");
337 (void) fprintf(fp
, "%s",
338 get_usage(command_table
[i
].usage
));
341 (void) fprintf(fp
, gettext("usage:\n"));
342 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
345 if (current_command
!= NULL
&&
346 ((strcmp(current_command
->name
, "set") == 0) ||
347 (strcmp(current_command
->name
, "get") == 0) ||
348 (strcmp(current_command
->name
, "list") == 0))) {
351 gettext("\nthe following properties are supported:\n"));
353 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
354 "PROPERTY", "EDIT", "VALUES");
356 /* Iterate over all properties */
357 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
360 (void) fprintf(fp
, "\t%-15s ", "feature@...");
361 (void) fprintf(fp
, "YES disabled | enabled | active\n");
363 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
364 "appended with a feature name.\nSee zpool-features(5).\n"));
368 * See comments at end of main().
370 if (getenv("ZFS_ABORT") != NULL
) {
371 (void) printf("dumping core by request\n");
375 exit(requested
? 0 : 2);
379 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
380 boolean_t print_logs
)
387 (void) printf("\t%*s%s\n", indent
, "", name
);
389 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
390 &child
, &children
) != 0)
393 for (c
= 0; c
< children
; c
++) {
394 uint64_t is_log
= B_FALSE
;
396 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
398 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
401 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
402 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
409 prop_list_contains_feature(nvlist_t
*proplist
)
412 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
413 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
414 if (zpool_prop_feature(nvpair_name(nvp
)))
421 * Add a property pair (name, string-value) into a property nvlist.
424 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
427 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
433 if (*props
== NULL
&&
434 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
435 (void) fprintf(stderr
,
436 gettext("internal error: out of memory\n"));
443 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
445 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
446 !zpool_prop_feature(propname
)) {
447 (void) fprintf(stderr
, gettext("property '%s' is "
448 "not a valid pool property\n"), propname
);
453 * feature@ properties and version should not be specified
456 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
457 nvlist_exists(proplist
, vname
)) ||
458 (prop
== ZPOOL_PROP_VERSION
&&
459 prop_list_contains_feature(proplist
))) {
460 (void) fprintf(stderr
, gettext("'feature@' and "
461 "'version' properties cannot be specified "
467 if (zpool_prop_feature(propname
))
470 normnm
= zpool_prop_to_name(prop
);
472 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
473 normnm
= zfs_prop_to_name(fprop
);
479 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
480 prop
!= ZPOOL_PROP_CACHEFILE
) {
481 (void) fprintf(stderr
, gettext("property '%s' "
482 "specified multiple times\n"), propname
);
486 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
487 (void) fprintf(stderr
, gettext("internal "
488 "error: out of memory\n"));
496 * Set a default property pair (name, string-value) in a property nvlist
499 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
504 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
507 return (add_prop_list(propname
, propval
, props
, poolprop
));
511 * zpool add [-fn] <pool> <vdev> ...
513 * -f Force addition of devices, even if they appear in use
514 * -n Do not add the devices, but display the resulting layout if
515 * they were to be added.
517 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
518 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
522 zpool_do_add(int argc
, char **argv
)
524 boolean_t force
= B_FALSE
;
525 boolean_t dryrun
= B_FALSE
;
529 zpool_boot_label_t boot_type
;
536 while ((c
= getopt(argc
, argv
, "fn")) != -1) {
545 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
554 /* get pool name and check number of arguments */
556 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
560 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
569 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
572 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
573 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
579 if (zpool_is_bootable(zhp
))
580 boot_type
= ZPOOL_COPY_BOOT_LABEL
;
582 boot_type
= ZPOOL_NO_BOOT_LABEL
;
584 /* pass off to get_vdev_spec for processing */
585 boot_size
= zpool_get_prop_int(zhp
, ZPOOL_PROP_BOOTSIZE
, NULL
);
586 nvroot
= make_root_vdev(zhp
, force
, !force
, B_FALSE
, dryrun
,
587 boot_type
, boot_size
, argc
, argv
);
588 if (nvroot
== NULL
) {
594 nvlist_t
*poolnvroot
;
596 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
599 (void) printf(gettext("would update '%s' to the following "
600 "configuration:\n"), zpool_get_name(zhp
));
602 /* print original main pool and new tree */
603 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
);
604 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
);
606 /* Do the same for the logs */
607 if (num_logs(poolnvroot
) > 0) {
608 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
);
609 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
);
610 } else if (num_logs(nvroot
) > 0) {
611 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
);
616 ret
= (zpool_add(zhp
, nvroot
) != 0);
626 * zpool remove <pool> <vdev> ...
628 * Removes the given vdev from the pool.
631 zpool_do_remove(int argc
, char **argv
)
636 boolean_t stop
= B_FALSE
;
637 boolean_t noop
= B_FALSE
;
638 boolean_t parsable
= B_FALSE
;
642 while ((c
= getopt(argc
, argv
, "nps")) != -1) {
654 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
663 /* get pool name and check number of arguments */
665 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
671 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
675 (void) fprintf(stderr
, gettext("stop request ignored\n"));
681 (void) fprintf(stderr
, gettext("too many arguments\n"));
684 if (zpool_vdev_remove_cancel(zhp
) != 0)
688 (void) fprintf(stderr
, gettext("missing device\n"));
692 for (i
= 1; i
< argc
; i
++) {
696 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
702 (void) printf("%s %llu\n",
706 zfs_nicenum(size
, valstr
,
708 (void) printf("Memory that will be "
709 "used after removing %s: %s\n",
713 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
723 * zpool labelclear [-f] <vdev>
725 * -f Force clearing the label for the vdevs which are members of
726 * the exported or foreign pools.
728 * Verifies that the vdev is not active and zeros out the label information
732 zpool_do_labelclear(int argc
, char **argv
)
734 char vdev
[MAXPATHLEN
];
740 boolean_t inuse
= B_FALSE
;
741 boolean_t force
= B_FALSE
;
744 while ((c
= getopt(argc
, argv
, "f")) != -1) {
750 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
761 (void) fprintf(stderr
, gettext("missing vdev name\n"));
765 (void) fprintf(stderr
, gettext("too many arguments\n"));
770 * Check if we were given absolute path and use it as is.
771 * Otherwise if the provided vdev name doesn't point to a file,
772 * try prepending dsk path and appending s0.
774 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
775 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
778 (void) snprintf(vdev
, sizeof (vdev
), "%s/%s",
779 ZFS_DISK_ROOT
, argv
[0]);
780 if ((s
= strrchr(argv
[0], 's')) == NULL
||
782 (void) strlcat(vdev
, "s0", sizeof (vdev
));
783 if (stat(vdev
, &st
) != 0) {
784 (void) fprintf(stderr
, gettext(
785 "failed to find device %s, try specifying absolute "
786 "path instead\n"), argv
[0]);
791 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
792 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
793 vdev
, strerror(errno
));
797 if (zpool_read_label(fd
, &config
) != 0) {
798 (void) fprintf(stderr
,
799 gettext("failed to read label from %s\n"), vdev
);
804 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
806 (void) fprintf(stderr
,
807 gettext("failed to check state for %s\n"), vdev
);
816 case POOL_STATE_ACTIVE
:
817 case POOL_STATE_SPARE
:
818 case POOL_STATE_L2CACHE
:
819 (void) fprintf(stderr
, gettext(
820 "%s is a member (%s) of pool \"%s\"\n"),
821 vdev
, zpool_pool_state_to_name(state
), name
);
825 case POOL_STATE_EXPORTED
:
828 (void) fprintf(stderr
, gettext(
829 "use '-f' to override the following error:\n"
830 "%s is a member of exported pool \"%s\"\n"),
835 case POOL_STATE_POTENTIALLY_ACTIVE
:
838 (void) fprintf(stderr
, gettext(
839 "use '-f' to override the following error:\n"
840 "%s is a member of potentially active pool \"%s\"\n"),
845 case POOL_STATE_DESTROYED
:
846 /* inuse should never be set for a destroyed pool */
852 ret
= zpool_clear_label(fd
);
854 (void) fprintf(stderr
,
855 gettext("failed to clear label for %s\n"), vdev
);
866 * zpool create [-fnd] [-B] [-o property=value] ...
867 * [-O file-system-property=value] ...
868 * [-R root] [-m mountpoint] [-t tempname] <pool> <dev> ...
870 * -B Create boot partition.
871 * -f Force creation, even if devices appear in use
872 * -n Do not create the pool, but display the resulting layout if it
873 * were to be created.
874 * -R Create a pool under an alternate root
875 * -m Set default mountpoint for the root dataset. By default it's
877 * -t Use the temporary name until the pool is exported.
878 * -o Set property=value.
879 * -d Don't automatically enable all supported pool features
880 * (individual features can be enabled with -o).
881 * -O Set fsproperty=value in the pool's root file system
883 * Creates the named pool according to the given vdev specification. The
884 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
885 * we get the nvlist back from get_vdev_spec(), we either print out the contents
886 * (if '-n' was specified), or pass it to libzfs to do the creation.
889 #define SYSTEM256 (256 * 1024 * 1024)
891 zpool_do_create(int argc
, char **argv
)
893 boolean_t force
= B_FALSE
;
894 boolean_t dryrun
= B_FALSE
;
895 boolean_t enable_all_pool_feat
= B_TRUE
;
896 zpool_boot_label_t boot_type
= ZPOOL_NO_BOOT_LABEL
;
897 uint64_t boot_size
= 0;
899 nvlist_t
*nvroot
= NULL
;
903 char *altroot
= NULL
;
904 char *mountpoint
= NULL
;
905 nvlist_t
*fsprops
= NULL
;
906 nvlist_t
*props
= NULL
;
910 while ((c
= getopt(argc
, argv
, ":fndBR:m:o:O:t:")) != -1) {
919 enable_all_pool_feat
= B_FALSE
;
923 * We should create the system partition.
924 * Also make sure the size is set.
926 boot_type
= ZPOOL_CREATE_BOOT_LABEL
;
928 boot_size
= SYSTEM256
;
932 if (add_prop_list(zpool_prop_to_name(
933 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
935 if (add_prop_list_default(zpool_prop_to_name(
936 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
940 /* Equivalent to -O mountpoint=optarg */
944 if ((propval
= strchr(optarg
, '=')) == NULL
) {
945 (void) fprintf(stderr
, gettext("missing "
946 "'=' for -o option\n"));
952 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
956 * Get bootsize value for make_root_vdev().
958 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_BOOTSIZE
) {
959 if (zfs_nicestrtonum(g_zfs
, propval
,
960 &boot_size
) < 0 || boot_size
== 0) {
961 (void) fprintf(stderr
,
962 gettext("bad boot partition size "
963 "'%s': %s\n"), propval
,
964 libzfs_error_description(g_zfs
));
970 * If the user is creating a pool that doesn't support
971 * feature flags, don't enable any features.
973 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
977 ver
= strtoull(propval
, &end
, 10);
979 ver
< SPA_VERSION_FEATURES
) {
980 enable_all_pool_feat
= B_FALSE
;
983 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
987 if ((propval
= strchr(optarg
, '=')) == NULL
) {
988 (void) fprintf(stderr
, gettext("missing "
989 "'=' for -O option\n"));
996 * Mountpoints are checked and then added later.
997 * Uniquely among properties, they can be specified
998 * more than once, to avoid conflict with -m.
1000 if (0 == strcmp(optarg
,
1001 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1002 mountpoint
= propval
;
1003 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1010 * Sanity check temporary pool name.
1012 if (strchr(optarg
, '/') != NULL
) {
1013 (void) fprintf(stderr
, gettext("cannot create "
1014 "'%s': invalid character '/' in temporary "
1016 (void) fprintf(stderr
, gettext("use 'zfs "
1017 "create' to create a dataset\n"));
1021 if (add_prop_list(zpool_prop_to_name(
1022 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1024 if (add_prop_list_default(zpool_prop_to_name(
1025 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1030 (void) fprintf(stderr
, gettext("missing argument for "
1031 "'%c' option\n"), optopt
);
1034 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1043 /* get pool name and check number of arguments */
1045 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1049 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1056 * As a special case, check for use of '/' in the name, and direct the
1057 * user to use 'zfs create' instead.
1059 if (strchr(poolname
, '/') != NULL
) {
1060 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1061 "character '/' in pool name\n"), poolname
);
1062 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1063 "create a dataset\n"));
1068 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
1069 * and not set otherwise.
1071 if (boot_type
== ZPOOL_CREATE_BOOT_LABEL
) {
1072 const char *propname
;
1073 char *strptr
, *buf
= NULL
;
1076 propname
= zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE
);
1077 if (nvlist_lookup_string(props
, propname
, &strptr
) != 0) {
1078 (void) asprintf(&buf
, "%" PRIu64
, boot_size
);
1080 (void) fprintf(stderr
,
1081 gettext("internal error: out of memory\n"));
1084 rv
= add_prop_list(propname
, buf
, &props
, B_TRUE
);
1090 const char *propname
;
1093 propname
= zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE
);
1094 if (nvlist_lookup_string(props
, propname
, &strptr
) == 0) {
1095 (void) fprintf(stderr
, gettext("error: setting boot "
1096 "partition size requires option '-B'\n"));
1101 /* pass off to get_vdev_spec for bulk processing */
1102 nvroot
= make_root_vdev(NULL
, force
, !force
, B_FALSE
, dryrun
,
1103 boot_type
, boot_size
, argc
- 1, argv
+ 1);
1107 /* make_root_vdev() allows 0 toplevel children if there are spares */
1108 if (!zfs_allocatable_devs(nvroot
)) {
1109 (void) fprintf(stderr
, gettext("invalid vdev "
1110 "specification: at least one toplevel vdev must be "
1115 if (altroot
!= NULL
&& altroot
[0] != '/') {
1116 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1117 "must be an absolute path\n"), altroot
);
1122 * Check the validity of the mountpoint and direct the user to use the
1123 * '-m' mountpoint option if it looks like its in use.
1125 if (mountpoint
== NULL
||
1126 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1127 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1128 char buf
[MAXPATHLEN
];
1131 if (mountpoint
&& mountpoint
[0] != '/') {
1132 (void) fprintf(stderr
, gettext("invalid mountpoint "
1133 "'%s': must be an absolute path, 'legacy', or "
1134 "'none'\n"), mountpoint
);
1138 if (mountpoint
== NULL
) {
1139 if (altroot
!= NULL
)
1140 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1143 (void) snprintf(buf
, sizeof (buf
), "/%s",
1146 if (altroot
!= NULL
)
1147 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1148 altroot
, mountpoint
);
1150 (void) snprintf(buf
, sizeof (buf
), "%s",
1154 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1155 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1156 "%s\n"), buf
, strerror(errno
));
1157 (void) fprintf(stderr
, gettext("use '-m' "
1158 "option to provide a different default\n"));
1163 while (count
< 3 && readdir(dirp
) != NULL
)
1165 (void) closedir(dirp
);
1168 (void) fprintf(stderr
, gettext("mountpoint "
1169 "'%s' exists and is not empty\n"), buf
);
1170 (void) fprintf(stderr
, gettext("use '-m' "
1171 "option to provide a "
1172 "different default\n"));
1179 * Now that the mountpoint's validity has been checked, ensure that
1180 * the property is set appropriately prior to creating the pool.
1182 if (mountpoint
!= NULL
) {
1183 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1184 mountpoint
, &fsprops
, B_FALSE
);
1192 * For a dry run invocation, print out a basic message and run
1193 * through all the vdevs in the list and print out in an
1194 * appropriate hierarchy.
1196 (void) printf(gettext("would create '%s' with the "
1197 "following layout:\n\n"), poolname
);
1199 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
);
1200 if (num_logs(nvroot
) > 0)
1201 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
);
1206 * Hand off to libzfs.
1208 if (enable_all_pool_feat
) {
1210 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1211 char propname
[MAXPATHLEN
];
1212 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1214 (void) snprintf(propname
, sizeof (propname
),
1215 "feature@%s", feat
->fi_uname
);
1218 * Skip feature if user specified it manually
1219 * on the command line.
1221 if (nvlist_exists(props
, propname
))
1224 ret
= add_prop_list(propname
,
1225 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1232 if (zpool_create(g_zfs
, poolname
,
1233 nvroot
, props
, fsprops
) == 0) {
1234 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1235 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1237 if (zfs_mount(pool
, NULL
, 0) == 0)
1238 ret
= zfs_shareall(pool
);
1241 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1242 (void) fprintf(stderr
, gettext("pool name may have "
1248 nvlist_free(nvroot
);
1249 nvlist_free(fsprops
);
1253 nvlist_free(fsprops
);
1260 * zpool destroy <pool>
1262 * -f Forcefully unmount any datasets
1264 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1267 zpool_do_destroy(int argc
, char **argv
)
1269 boolean_t force
= B_FALSE
;
1272 zpool_handle_t
*zhp
;
1276 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1282 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1291 /* check arguments */
1293 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1297 (void) fprintf(stderr
, gettext("too many arguments\n"));
1303 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1305 * As a special case, check for use of '/' in the name, and
1306 * direct the user to use 'zfs destroy' instead.
1308 if (strchr(pool
, '/') != NULL
)
1309 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1310 "destroy a dataset\n"));
1314 if (zpool_disable_datasets(zhp
, force
) != 0) {
1315 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1316 "could not unmount datasets\n"), zpool_get_name(zhp
));
1320 /* The history must be logged as part of the export */
1321 log_history
= B_FALSE
;
1323 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1331 * zpool export [-f] <pool> ...
1333 * -f Forcefully unmount datasets
1335 * Export the given pools. By default, the command will attempt to cleanly
1336 * unmount any active datasets within the pool. If the '-f' flag is specified,
1337 * then the datasets will be forcefully unmounted.
1340 zpool_do_export(int argc
, char **argv
)
1342 boolean_t force
= B_FALSE
;
1343 boolean_t hardforce
= B_FALSE
;
1345 zpool_handle_t
*zhp
;
1350 while ((c
= getopt(argc
, argv
, "fF")) != -1) {
1359 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1368 /* check arguments */
1370 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1375 for (i
= 0; i
< argc
; i
++) {
1376 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) == NULL
) {
1381 if (zpool_disable_datasets(zhp
, force
) != 0) {
1387 /* The history must be logged as part of the export */
1388 log_history
= B_FALSE
;
1391 if (zpool_export_force(zhp
, history_str
) != 0)
1393 } else if (zpool_export(zhp
, force
, history_str
) != 0) {
1404 * Given a vdev configuration, determine the maximum width needed for the device
1408 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
)
1410 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, B_TRUE
);
1415 if (strlen(name
) + depth
> max
)
1416 max
= strlen(name
) + depth
;
1420 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1421 &child
, &children
) == 0) {
1422 for (c
= 0; c
< children
; c
++)
1423 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1428 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1429 &child
, &children
) == 0) {
1430 for (c
= 0; c
< children
; c
++)
1431 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1436 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1437 &child
, &children
) == 0) {
1438 for (c
= 0; c
< children
; c
++)
1439 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1448 typedef struct spare_cbdata
{
1450 zpool_handle_t
*cb_zhp
;
1454 find_vdev(nvlist_t
*nv
, uint64_t search
)
1460 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1464 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1465 &child
, &children
) == 0) {
1466 for (c
= 0; c
< children
; c
++)
1467 if (find_vdev(child
[c
], search
))
1475 find_spare(zpool_handle_t
*zhp
, void *data
)
1477 spare_cbdata_t
*cbp
= data
;
1478 nvlist_t
*config
, *nvroot
;
1480 config
= zpool_get_config(zhp
, NULL
);
1481 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1484 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1494 * Print out configuration state as requested by status_callback.
1497 print_status_config(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
1498 int namewidth
, int depth
, boolean_t isspare
)
1502 pool_scan_stat_t
*ps
= NULL
;
1504 char rbuf
[6], wbuf
[6], cbuf
[6];
1506 uint64_t notpresent
;
1511 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1512 &child
, &children
) != 0)
1515 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1516 (uint64_t **)&vs
, &c
) == 0);
1518 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1520 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
1523 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1526 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1529 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1531 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1535 (void) printf("\t%*s%-*s %-8s", depth
, "", namewidth
- depth
,
1539 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1540 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1541 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1542 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1545 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1546 ¬present
) == 0) {
1548 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1549 (void) printf(" was %s", path
);
1550 } else if (vs
->vs_aux
!= 0) {
1553 switch (vs
->vs_aux
) {
1554 case VDEV_AUX_OPEN_FAILED
:
1555 (void) printf(gettext("cannot open"));
1558 case VDEV_AUX_BAD_GUID_SUM
:
1559 (void) printf(gettext("missing device"));
1562 case VDEV_AUX_NO_REPLICAS
:
1563 (void) printf(gettext("insufficient replicas"));
1566 case VDEV_AUX_VERSION_NEWER
:
1567 (void) printf(gettext("newer version"));
1570 case VDEV_AUX_UNSUP_FEAT
:
1571 (void) printf(gettext("unsupported feature(s)"));
1574 case VDEV_AUX_SPARED
:
1575 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1577 if (zpool_iter(g_zfs
, find_spare
, &cb
) == 1) {
1578 if (strcmp(zpool_get_name(cb
.cb_zhp
),
1579 zpool_get_name(zhp
)) == 0)
1580 (void) printf(gettext("currently in "
1583 (void) printf(gettext("in use by "
1585 zpool_get_name(cb
.cb_zhp
));
1586 zpool_close(cb
.cb_zhp
);
1588 (void) printf(gettext("currently in use"));
1592 case VDEV_AUX_ERR_EXCEEDED
:
1593 (void) printf(gettext("too many errors"));
1596 case VDEV_AUX_IO_FAILURE
:
1597 (void) printf(gettext("experienced I/O failures"));
1600 case VDEV_AUX_BAD_LOG
:
1601 (void) printf(gettext("bad intent log"));
1604 case VDEV_AUX_EXTERNAL
:
1605 (void) printf(gettext("external device fault"));
1608 case VDEV_AUX_SPLIT_POOL
:
1609 (void) printf(gettext("split into new pool"));
1612 case VDEV_AUX_CHILDREN_OFFLINE
:
1613 (void) printf(gettext("all children offline"));
1617 (void) printf(gettext("corrupted data"));
1622 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1623 (uint64_t **)&ps
, &c
);
1625 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1626 vs
->vs_scan_processed
!= 0 && children
== 0) {
1627 (void) printf(gettext(" (%s)"),
1628 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1629 "resilvering" : "repairing");
1632 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
1633 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
1634 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
1635 !vs
->vs_scan_removing
) {
1638 struct tm zaction_ts
;
1640 time_t t
= vs
->vs_initialize_action_time
;
1641 int initialize_pct
= 100;
1642 if (vs
->vs_initialize_state
!= VDEV_INITIALIZE_COMPLETE
) {
1643 initialize_pct
= (vs
->vs_initialize_bytes_done
* 100 /
1644 (vs
->vs_initialize_bytes_est
+ 1));
1647 (void) localtime_r(&t
, &zaction_ts
);
1648 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
1650 switch (vs
->vs_initialize_state
) {
1651 case VDEV_INITIALIZE_SUSPENDED
:
1652 (void) snprintf(zbuf
, sizeof (zbuf
),
1653 ", suspended, started at %s", tbuf
);
1655 case VDEV_INITIALIZE_ACTIVE
:
1656 (void) snprintf(zbuf
, sizeof (zbuf
),
1657 ", started at %s", tbuf
);
1659 case VDEV_INITIALIZE_COMPLETE
:
1660 (void) snprintf(zbuf
, sizeof (zbuf
),
1661 ", completed at %s", tbuf
);
1665 (void) printf(gettext(" (%d%% initialized%s)"),
1666 initialize_pct
, zbuf
);
1669 (void) printf("\n");
1671 for (c
= 0; c
< children
; c
++) {
1672 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1674 /* Don't print logs or holes here */
1675 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1677 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1679 if (islog
|| ishole
)
1681 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1682 print_status_config(zhp
, vname
, child
[c
],
1683 namewidth
, depth
+ 2, isspare
);
1690 * Print the configuration of an exported pool. Iterate over all vdevs in the
1691 * pool, printing out the name and status for each one.
1694 print_import_config(const char *name
, nvlist_t
*nv
, int namewidth
, int depth
)
1701 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1702 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1703 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1706 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1707 (uint64_t **)&vs
, &c
) == 0);
1709 (void) printf("\t%*s%-*s", depth
, "", namewidth
- depth
, name
);
1710 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1712 if (vs
->vs_aux
!= 0) {
1715 switch (vs
->vs_aux
) {
1716 case VDEV_AUX_OPEN_FAILED
:
1717 (void) printf(gettext("cannot open"));
1720 case VDEV_AUX_BAD_GUID_SUM
:
1721 (void) printf(gettext("missing device"));
1724 case VDEV_AUX_NO_REPLICAS
:
1725 (void) printf(gettext("insufficient replicas"));
1728 case VDEV_AUX_VERSION_NEWER
:
1729 (void) printf(gettext("newer version"));
1732 case VDEV_AUX_UNSUP_FEAT
:
1733 (void) printf(gettext("unsupported feature(s)"));
1736 case VDEV_AUX_ERR_EXCEEDED
:
1737 (void) printf(gettext("too many errors"));
1740 case VDEV_AUX_CHILDREN_OFFLINE
:
1741 (void) printf(gettext("all children offline"));
1745 (void) printf(gettext("corrupted data"));
1749 (void) printf("\n");
1751 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1752 &child
, &children
) != 0)
1755 for (c
= 0; c
< children
; c
++) {
1756 uint64_t is_log
= B_FALSE
;
1758 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1763 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_TRUE
);
1764 print_import_config(vname
, child
[c
], namewidth
, depth
+ 2);
1768 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1769 &child
, &children
) == 0) {
1770 (void) printf(gettext("\tcache\n"));
1771 for (c
= 0; c
< children
; c
++) {
1772 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1773 (void) printf("\t %s\n", vname
);
1778 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1779 &child
, &children
) == 0) {
1780 (void) printf(gettext("\tspares\n"));
1781 for (c
= 0; c
< children
; c
++) {
1782 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], B_FALSE
);
1783 (void) printf("\t %s\n", vname
);
1791 * Logs are recorded as top level vdevs in the main pool child array
1792 * but with "is_log" set to 1. We use either print_status_config() or
1793 * print_import_config() to print the top level logs then any log
1794 * children (eg mirrored slogs) are printed recursively - which
1795 * works because only the top level vdev is marked "is_log"
1798 print_logs(zpool_handle_t
*zhp
, nvlist_t
*nv
, int namewidth
, boolean_t verbose
)
1803 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1807 (void) printf(gettext("\tlogs\n"));
1809 for (c
= 0; c
< children
; c
++) {
1810 uint64_t is_log
= B_FALSE
;
1813 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1817 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_TRUE
);
1819 print_status_config(zhp
, name
, child
[c
], namewidth
,
1822 print_import_config(name
, child
[c
], namewidth
, 2);
1828 * Display the status for the given pool.
1831 show_import(nvlist_t
*config
)
1833 uint64_t pool_state
;
1845 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1847 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1849 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1851 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1854 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1855 (uint64_t **)&vs
, &vsc
) == 0);
1856 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1858 reason
= zpool_import_status(config
, &msgid
);
1860 (void) printf(gettext(" pool: %s\n"), name
);
1861 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1862 (void) printf(gettext(" state: %s"), health
);
1863 if (pool_state
== POOL_STATE_DESTROYED
)
1864 (void) printf(gettext(" (DESTROYED)"));
1865 (void) printf("\n");
1868 case ZPOOL_STATUS_MISSING_DEV_R
:
1869 case ZPOOL_STATUS_MISSING_DEV_NR
:
1870 case ZPOOL_STATUS_BAD_GUID_SUM
:
1871 (void) printf(gettext(" status: One or more devices are "
1872 "missing from the system.\n"));
1875 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1876 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1877 (void) printf(gettext(" status: One or more devices contains "
1878 "corrupted data.\n"));
1881 case ZPOOL_STATUS_CORRUPT_DATA
:
1883 gettext(" status: The pool data is corrupted.\n"));
1886 case ZPOOL_STATUS_OFFLINE_DEV
:
1887 (void) printf(gettext(" status: One or more devices "
1888 "are offlined.\n"));
1891 case ZPOOL_STATUS_CORRUPT_POOL
:
1892 (void) printf(gettext(" status: The pool metadata is "
1896 case ZPOOL_STATUS_VERSION_OLDER
:
1897 (void) printf(gettext(" status: The pool is formatted using a "
1898 "legacy on-disk version.\n"));
1901 case ZPOOL_STATUS_VERSION_NEWER
:
1902 (void) printf(gettext(" status: The pool is formatted using an "
1903 "incompatible version.\n"));
1906 case ZPOOL_STATUS_FEAT_DISABLED
:
1907 (void) printf(gettext(" status: Some supported features are "
1908 "not enabled on the pool.\n"));
1911 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1912 (void) printf(gettext("status: The pool uses the following "
1913 "feature(s) not supported on this system:\n"));
1914 zpool_print_unsup_feat(config
);
1917 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1918 (void) printf(gettext("status: The pool can only be accessed "
1919 "in read-only mode on this system. It\n\tcannot be "
1920 "accessed in read-write mode because it uses the "
1921 "following\n\tfeature(s) not supported on this system:\n"));
1922 zpool_print_unsup_feat(config
);
1925 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1926 (void) printf(gettext(" status: The pool was last accessed by "
1927 "another system.\n"));
1930 case ZPOOL_STATUS_FAULTED_DEV_R
:
1931 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1932 (void) printf(gettext(" status: One or more devices are "
1936 case ZPOOL_STATUS_BAD_LOG
:
1937 (void) printf(gettext(" status: An intent log record cannot be "
1941 case ZPOOL_STATUS_RESILVERING
:
1942 (void) printf(gettext(" status: One or more devices were being "
1948 * No other status can be seen when importing pools.
1950 assert(reason
== ZPOOL_STATUS_OK
);
1954 * Print out an action according to the overall state of the pool.
1956 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1957 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1958 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1959 (void) printf(gettext(" action: The pool can be "
1960 "imported using its name or numeric identifier, "
1961 "though\n\tsome features will not be available "
1962 "without an explicit 'zpool upgrade'.\n"));
1963 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1964 (void) printf(gettext(" action: The pool can be "
1965 "imported using its name or numeric "
1966 "identifier and\n\tthe '-f' flag.\n"));
1968 (void) printf(gettext(" action: The pool can be "
1969 "imported using its name or numeric "
1972 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
1973 (void) printf(gettext(" action: The pool can be imported "
1974 "despite missing or damaged devices. The\n\tfault "
1975 "tolerance of the pool may be compromised if imported.\n"));
1978 case ZPOOL_STATUS_VERSION_NEWER
:
1979 (void) printf(gettext(" action: The pool cannot be "
1980 "imported. Access the pool on a system running "
1981 "newer\n\tsoftware, or recreate the pool from "
1984 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1985 (void) printf(gettext("action: The pool cannot be "
1986 "imported. Access the pool on a system that "
1987 "supports\n\tthe required feature(s), or recreate "
1988 "the pool from backup.\n"));
1990 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1991 (void) printf(gettext("action: The pool cannot be "
1992 "imported in read-write mode. Import the pool "
1994 "\t\"-o readonly=on\", access the pool on a system "
1995 "that supports the\n\trequired feature(s), or "
1996 "recreate the pool from backup.\n"));
1998 case ZPOOL_STATUS_MISSING_DEV_R
:
1999 case ZPOOL_STATUS_MISSING_DEV_NR
:
2000 case ZPOOL_STATUS_BAD_GUID_SUM
:
2001 (void) printf(gettext(" action: The pool cannot be "
2002 "imported. Attach the missing\n\tdevices and try "
2006 (void) printf(gettext(" action: The pool cannot be "
2007 "imported due to damaged devices or data.\n"));
2011 /* Print the comment attached to the pool. */
2012 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2013 (void) printf(gettext("comment: %s\n"), comment
);
2016 * If the state is "closed" or "can't open", and the aux state
2017 * is "corrupt data":
2019 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2020 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2021 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2022 if (pool_state
== POOL_STATE_DESTROYED
)
2023 (void) printf(gettext("\tThe pool was destroyed, "
2024 "but can be imported using the '-Df' flags.\n"));
2025 else if (pool_state
!= POOL_STATE_EXPORTED
)
2026 (void) printf(gettext("\tThe pool may be active on "
2027 "another system, but can be imported using\n\t"
2028 "the '-f' flag.\n"));
2032 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
2035 (void) printf(gettext(" config:\n\n"));
2037 namewidth
= max_width(NULL
, nvroot
, 0, 0);
2041 print_import_config(name
, nvroot
, namewidth
, 0);
2042 if (num_logs(nvroot
) > 0)
2043 print_logs(NULL
, nvroot
, namewidth
, B_FALSE
);
2045 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2046 (void) printf(gettext("\n\tAdditional devices are known to "
2047 "be part of this pool, though their\n\texact "
2048 "configuration cannot be determined.\n"));
2053 * Perform the import for the given configuration. This passes the heavy
2054 * lifting off to zpool_import_props(), and then mounts the datasets contained
2058 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2059 nvlist_t
*props
, int flags
)
2061 zpool_handle_t
*zhp
;
2066 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2069 verify(nvlist_lookup_uint64(config
,
2070 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
2071 verify(nvlist_lookup_uint64(config
,
2072 ZPOOL_CONFIG_VERSION
, &version
) == 0);
2073 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2074 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2075 "is formatted using an unsupported ZFS version\n"), name
);
2077 } else if (state
!= POOL_STATE_EXPORTED
&&
2078 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2081 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
2083 if ((unsigned long)hostid
!= gethostid()) {
2088 verify(nvlist_lookup_string(config
,
2089 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2090 verify(nvlist_lookup_uint64(config
,
2091 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
2093 (void) fprintf(stderr
, gettext("cannot import "
2094 "'%s': pool may be in use from other "
2095 "system, it was last accessed by %s "
2096 "(hostid: 0x%lx) on %s"), name
, hostname
,
2097 (unsigned long)hostid
,
2098 asctime(localtime(&t
)));
2099 (void) fprintf(stderr
, gettext("use '-f' to "
2100 "import anyway\n"));
2104 (void) fprintf(stderr
, gettext("cannot import '%s': "
2105 "pool may be in use from other system\n"), name
);
2106 (void) fprintf(stderr
, gettext("use '-f' to import "
2112 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2115 if (newname
!= NULL
)
2116 name
= (char *)newname
;
2118 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2121 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2122 !(flags
& ZFS_IMPORT_ONLY
) &&
2123 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2133 * zpool checkpoint <pool>
2134 * checkpoint --discard <pool>
2136 * -d Discard the checkpoint from a checkpointed
2139 * Checkpoints the specified pool, by taking a "snapshot" of its
2140 * current state. A pool can only have one checkpoint at a time.
2143 zpool_do_checkpoint(int argc
, char **argv
)
2147 zpool_handle_t
*zhp
;
2150 struct option long_options
[] = {
2151 {"discard", no_argument
, NULL
, 'd'},
2156 while ((c
= getopt_long(argc
, argv
, ":d", long_options
, NULL
)) != -1) {
2162 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2172 (void) fprintf(stderr
, gettext("missing pool argument\n"));
2177 (void) fprintf(stderr
, gettext("too many arguments\n"));
2183 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
2184 /* As a special case, check for use of '/' in the name */
2185 if (strchr(pool
, '/') != NULL
)
2186 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
2187 "doesn't work on datasets. To save the state "
2188 "of a dataset from a specific point in time "
2189 "please use 'zfs snapshot'\n"));
2194 err
= (zpool_discard_checkpoint(zhp
) != 0);
2196 err
= (zpool_checkpoint(zhp
) != 0);
2203 #define CHECKPOINT_OPT 1024
2206 * zpool import [-d dir] [-D]
2207 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2208 * [-d dir | -c cachefile] [-f] -a
2209 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2210 * [-d dir | -c cachefile] [-f] [-n] [-F] [-t]
2211 * <pool | id> [newpool]
2213 * -c Read pool information from a cachefile instead of searching
2216 * -d Scan in a specific directory, other than /dev/dsk. More than
2217 * one directory can be specified using multiple '-d' options.
2219 * -D Scan for previously destroyed pools or import all or only
2220 * specified destroyed pools.
2222 * -R Temporarily import the pool, with all mountpoints relative to
2223 * the given root. The pool will remain exported when the machine
2226 * -V Import even in the presence of faulted vdevs. This is an
2227 * intentionally undocumented option for testing purposes, and
2228 * treats the pool configuration as complete, leaving any bad
2229 * vdevs in the FAULTED state. In other words, it does verbatim
2232 * -f Force import, even if it appears that the pool is active.
2234 * -F Attempt rewind if necessary.
2236 * -n See if rewind would work, but don't actually rewind.
2238 * -N Import the pool but don't mount datasets.
2240 * -t Use newpool as a temporary pool name instead of renaming
2243 * -T Specify a starting txg to use for import. This option is
2244 * intentionally undocumented option for testing purposes.
2246 * -a Import all pools found.
2248 * -o Set property=value and/or temporary mount options (without '=').
2250 * --rewind-to-checkpoint
2251 * Import the pool and revert back to the checkpoint.
2253 * The import command scans for pools to import, and import pools based on pool
2254 * name and GUID. The pool can also be renamed as part of the import process.
2257 zpool_do_import(int argc
, char **argv
)
2259 char **searchdirs
= NULL
;
2263 nvlist_t
*pools
= NULL
;
2264 boolean_t do_all
= B_FALSE
;
2265 boolean_t do_destroyed
= B_FALSE
;
2266 char *mntopts
= NULL
;
2269 uint64_t searchguid
= 0;
2270 char *searchname
= NULL
;
2272 nvlist_t
*found_config
;
2273 nvlist_t
*policy
= NULL
;
2274 nvlist_t
*props
= NULL
;
2276 int flags
= ZFS_IMPORT_NORMAL
;
2277 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2278 boolean_t dryrun
= B_FALSE
;
2279 boolean_t do_rewind
= B_FALSE
;
2280 boolean_t xtreme_rewind
= B_FALSE
;
2281 uint64_t pool_state
, txg
= -1ULL;
2282 char *cachefile
= NULL
;
2283 importargs_t idata
= { 0 };
2287 struct option long_options
[] = {
2288 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
2293 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFmnNo:rR:tT:VX",
2294 long_options
, NULL
)) != -1) {
2303 if (searchdirs
== NULL
) {
2304 searchdirs
= safe_malloc(sizeof (char *));
2306 char **tmp
= safe_malloc((nsearch
+ 1) *
2308 bcopy(searchdirs
, tmp
, nsearch
*
2313 searchdirs
[nsearch
++] = optarg
;
2316 do_destroyed
= B_TRUE
;
2319 flags
|= ZFS_IMPORT_ANY_HOST
;
2325 flags
|= ZFS_IMPORT_MISSING_LOG
;
2331 flags
|= ZFS_IMPORT_ONLY
;
2334 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2337 if (add_prop_list(optarg
, propval
,
2345 if (add_prop_list(zpool_prop_to_name(
2346 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2348 if (add_prop_list_default(zpool_prop_to_name(
2349 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2353 flags
|= ZFS_IMPORT_TEMP_NAME
;
2354 if (add_prop_list_default(zpool_prop_to_name(
2355 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2360 txg
= strtoull(optarg
, &endptr
, 0);
2361 if (errno
!= 0 || *endptr
!= '\0') {
2362 (void) fprintf(stderr
,
2363 gettext("invalid txg value\n"));
2366 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2369 flags
|= ZFS_IMPORT_VERBATIM
;
2372 xtreme_rewind
= B_TRUE
;
2374 case CHECKPOINT_OPT
:
2375 flags
|= ZFS_IMPORT_CHECKPOINT
;
2378 (void) fprintf(stderr
, gettext("missing argument for "
2379 "'%c' option\n"), optopt
);
2383 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2392 if (cachefile
&& nsearch
!= 0) {
2393 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2397 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2398 (void) fprintf(stderr
,
2399 gettext("-n or -X only meaningful with -F\n"));
2403 rewind_policy
= ZPOOL_TRY_REWIND
;
2405 rewind_policy
= ZPOOL_DO_REWIND
;
2407 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2409 /* In the future, we can capture further policy and include it here */
2410 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2411 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
2412 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
2413 rewind_policy
) != 0)
2416 if (searchdirs
== NULL
) {
2417 searchdirs
= safe_malloc(sizeof (char *));
2418 searchdirs
[0] = ZFS_DISK_ROOT
;
2422 /* check argument count */
2425 (void) fprintf(stderr
, gettext("too many arguments\n"));
2430 (void) fprintf(stderr
, gettext("too many arguments\n"));
2435 * Check for the SYS_CONFIG privilege. We do this explicitly
2436 * here because otherwise any attempt to discover pools will
2439 if (argc
== 0 && !priv_ineffect(PRIV_SYS_CONFIG
)) {
2440 (void) fprintf(stderr
, gettext("cannot "
2441 "discover pools: permission denied\n"));
2443 nvlist_free(policy
);
2449 * Depending on the arguments given, we do one of the following:
2451 * <none> Iterate through all pools and display information about
2454 * -a Iterate through all pools and try to import each one.
2456 * <id> Find the pool that corresponds to the given GUID/pool
2457 * name and import that one.
2459 * -D Above options applies only to destroyed pools.
2465 searchguid
= strtoull(argv
[0], &endptr
, 10);
2466 if (errno
!= 0 || *endptr
!= '\0') {
2467 searchname
= argv
[0];
2470 found_config
= NULL
;
2473 * User specified a name or guid. Ensure it's unique.
2475 idata
.unique
= B_TRUE
;
2479 idata
.path
= searchdirs
;
2480 idata
.paths
= nsearch
;
2481 idata
.poolname
= searchname
;
2482 idata
.guid
= searchguid
;
2483 idata
.cachefile
= cachefile
;
2484 idata
.policy
= policy
;
2486 pools
= zpool_search_import(g_zfs
, &idata
);
2488 if (pools
!= NULL
&& idata
.exists
&&
2489 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2490 (void) fprintf(stderr
, gettext("cannot import '%s': "
2491 "a pool with that name already exists\n"),
2493 (void) fprintf(stderr
, gettext("use the form 'zpool import "
2494 "[-t] <pool | id> <newpool>' to give it a new temporary "
2495 "or permanent name\n"));
2497 } else if (pools
== NULL
&& idata
.exists
) {
2498 (void) fprintf(stderr
, gettext("cannot import '%s': "
2499 "a pool with that name is already created/imported,\n"),
2501 (void) fprintf(stderr
, gettext("and no additional pools "
2502 "with that name were found\n"));
2504 } else if (pools
== NULL
) {
2506 (void) fprintf(stderr
, gettext("cannot import '%s': "
2507 "no such pool available\n"), argv
[0]);
2514 nvlist_free(policy
);
2519 * At this point we have a list of import candidate configs. Even if
2520 * we were searching by pool name or guid, we still need to
2521 * post-process the list to deal with pool state and possible
2527 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2529 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2531 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2533 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2535 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2538 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
2545 (void) printf("\n");
2548 err
|= do_import(config
, NULL
, mntopts
,
2551 show_import(config
);
2553 } else if (searchname
!= NULL
) {
2557 * We are searching for a pool based on name.
2559 verify(nvlist_lookup_string(config
,
2560 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2562 if (strcmp(name
, searchname
) == 0) {
2563 if (found_config
!= NULL
) {
2564 (void) fprintf(stderr
, gettext(
2565 "cannot import '%s': more than "
2566 "one matching pool\n"), searchname
);
2567 (void) fprintf(stderr
, gettext(
2568 "import by numeric ID instead\n"));
2571 found_config
= config
;
2577 * Search for a pool by guid.
2579 verify(nvlist_lookup_uint64(config
,
2580 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2582 if (guid
== searchguid
)
2583 found_config
= config
;
2588 * If we were searching for a specific pool, verify that we found a
2589 * pool, and then do the import.
2591 if (argc
!= 0 && err
== 0) {
2592 if (found_config
== NULL
) {
2593 (void) fprintf(stderr
, gettext("cannot import '%s': "
2594 "no such pool available\n"), argv
[0]);
2597 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2598 argv
[1], mntopts
, props
, flags
);
2603 * If we were just looking for pools, report an error if none were
2606 if (argc
== 0 && first
)
2607 (void) fprintf(stderr
,
2608 gettext("no pools available to import\n"));
2613 nvlist_free(policy
);
2616 return (err
? 1 : 0);
2619 typedef struct iostat_cbdata
{
2620 boolean_t cb_verbose
;
2623 zpool_list_t
*cb_list
;
2627 print_iostat_separator(iostat_cbdata_t
*cb
)
2631 for (i
= 0; i
< cb
->cb_namewidth
; i
++)
2633 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2637 print_iostat_header(iostat_cbdata_t
*cb
)
2639 (void) printf("%*s capacity operations bandwidth\n",
2640 cb
->cb_namewidth
, "");
2641 (void) printf("%-*s alloc free read write read write\n",
2642 cb
->cb_namewidth
, "pool");
2643 print_iostat_separator(cb
);
2647 * Display a single statistic.
2650 print_one_stat(uint64_t value
)
2654 zfs_nicenum(value
, buf
, sizeof (buf
));
2655 (void) printf(" %5s", buf
);
2659 * Print out all the statistics for the given vdev. This can either be the
2660 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2661 * is a verbose output, and we don't want to display the toplevel pool stats.
2664 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
2665 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
2667 nvlist_t
**oldchild
, **newchild
;
2669 vdev_stat_t
*oldvs
, *newvs
;
2670 vdev_stat_t zerovs
= { 0 };
2675 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
2678 if (oldnv
!= NULL
) {
2679 verify(nvlist_lookup_uint64_array(oldnv
,
2680 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
2685 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
2686 (uint64_t **)&newvs
, &c
) == 0);
2688 if (strlen(name
) + depth
> cb
->cb_namewidth
)
2689 (void) printf("%*s%s", depth
, "", name
);
2691 (void) printf("%*s%s%*s", depth
, "", name
,
2692 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
2694 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
2699 scale
= (double)NANOSEC
/ tdelta
;
2701 /* only toplevel vdevs have capacity stats */
2702 if (newvs
->vs_space
== 0) {
2703 (void) printf(" - -");
2705 print_one_stat(newvs
->vs_alloc
);
2706 print_one_stat(newvs
->vs_space
- newvs
->vs_alloc
);
2709 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_READ
] -
2710 oldvs
->vs_ops
[ZIO_TYPE_READ
])));
2712 print_one_stat((uint64_t)(scale
* (newvs
->vs_ops
[ZIO_TYPE_WRITE
] -
2713 oldvs
->vs_ops
[ZIO_TYPE_WRITE
])));
2715 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_READ
] -
2716 oldvs
->vs_bytes
[ZIO_TYPE_READ
])));
2718 print_one_stat((uint64_t)(scale
* (newvs
->vs_bytes
[ZIO_TYPE_WRITE
] -
2719 oldvs
->vs_bytes
[ZIO_TYPE_WRITE
])));
2721 (void) printf("\n");
2723 if (!cb
->cb_verbose
)
2726 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
2727 &newchild
, &children
) != 0)
2730 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
2731 &oldchild
, &c
) != 0)
2734 for (c
= 0; c
< children
; c
++) {
2735 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
2737 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
2740 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
2743 if (ishole
|| islog
)
2746 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
], B_FALSE
);
2747 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2748 newchild
[c
], cb
, depth
+ 2);
2753 * Log device section
2756 if (num_logs(newnv
) > 0) {
2757 (void) printf("%-*s - - - - - "
2758 "-\n", cb
->cb_namewidth
, "logs");
2760 for (c
= 0; c
< children
; c
++) {
2761 uint64_t islog
= B_FALSE
;
2762 (void) nvlist_lookup_uint64(newchild
[c
],
2763 ZPOOL_CONFIG_IS_LOG
, &islog
);
2766 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2768 print_vdev_stats(zhp
, vname
, oldnv
?
2769 oldchild
[c
] : NULL
, newchild
[c
],
2778 * Include level 2 ARC devices in iostat output
2780 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
2781 &newchild
, &children
) != 0)
2784 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
2785 &oldchild
, &c
) != 0)
2789 (void) printf("%-*s - - - - - "
2790 "-\n", cb
->cb_namewidth
, "cache");
2791 for (c
= 0; c
< children
; c
++) {
2792 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
2794 print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
2795 newchild
[c
], cb
, depth
+ 2);
2802 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
2804 iostat_cbdata_t
*cb
= data
;
2808 * If the pool has disappeared, remove it from the list and continue.
2810 if (zpool_refresh_stats(zhp
, &missing
) != 0)
2814 pool_list_remove(cb
->cb_list
, zhp
);
2820 * Callback to print out the iostats for the given pool.
2823 print_iostat(zpool_handle_t
*zhp
, void *data
)
2825 iostat_cbdata_t
*cb
= data
;
2826 nvlist_t
*oldconfig
, *newconfig
;
2827 nvlist_t
*oldnvroot
, *newnvroot
;
2829 newconfig
= zpool_get_config(zhp
, &oldconfig
);
2831 if (cb
->cb_iteration
== 1)
2834 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2837 if (oldconfig
== NULL
)
2840 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
2844 * Print out the statistics for the pool.
2846 print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
, cb
, 0);
2849 print_iostat_separator(cb
);
2855 get_namewidth(zpool_handle_t
*zhp
, void *data
)
2857 iostat_cbdata_t
*cb
= data
;
2858 nvlist_t
*config
, *nvroot
;
2860 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
2861 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2863 if (!cb
->cb_verbose
)
2864 cb
->cb_namewidth
= strlen(zpool_get_name(zhp
));
2866 cb
->cb_namewidth
= max_width(zhp
, nvroot
, 0,
2871 * The width must fall into the range [10,38]. The upper limit is the
2872 * maximum we can have and still fit in 80 columns.
2874 if (cb
->cb_namewidth
< 10)
2875 cb
->cb_namewidth
= 10;
2876 if (cb
->cb_namewidth
> 38)
2877 cb
->cb_namewidth
= 38;
2883 * Parse the input string, get the 'interval' and 'count' value if there is one.
2886 get_interval_count(int *argcp
, char **argv
, unsigned long *iv
,
2889 unsigned long interval
= 0, count
= 0;
2890 int argc
= *argcp
, errno
;
2893 * Determine if the last argument is an integer or a pool name
2895 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2899 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2901 if (*end
== '\0' && errno
== 0) {
2902 if (interval
== 0) {
2903 (void) fprintf(stderr
, gettext("interval "
2904 "cannot be zero\n"));
2908 * Ignore the last parameter
2913 * If this is not a valid number, just plow on. The
2914 * user will get a more informative error message later
2922 * If the last argument is also an integer, then we have both a count
2925 if (argc
> 0 && isdigit(argv
[argc
- 1][0])) {
2930 interval
= strtoul(argv
[argc
- 1], &end
, 10);
2932 if (*end
== '\0' && errno
== 0) {
2933 if (interval
== 0) {
2934 (void) fprintf(stderr
, gettext("interval "
2935 "cannot be zero\n"));
2940 * Ignore the last parameter
2954 get_timestamp_arg(char c
)
2957 timestamp_fmt
= UDATE
;
2959 timestamp_fmt
= DDATE
;
2965 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2967 * -v Display statistics for individual vdevs
2968 * -T Display a timestamp in date(1) or Unix format
2970 * This command can be tricky because we want to be able to deal with pool
2971 * creation/destruction as well as vdev configuration changes. The bulk of this
2972 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2973 * on pool_list_update() to detect the addition of new pools. Configuration
2974 * changes are all handled within libzfs.
2977 zpool_do_iostat(int argc
, char **argv
)
2982 unsigned long interval
= 0, count
= 0;
2984 boolean_t verbose
= B_FALSE
;
2988 while ((c
= getopt(argc
, argv
, "T:v")) != -1) {
2991 get_timestamp_arg(*optarg
);
2997 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3006 get_interval_count(&argc
, argv
, &interval
, &count
);
3009 * Construct the list of all interesting pools.
3012 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
3015 if (pool_list_count(list
) == 0 && argc
!= 0) {
3016 pool_list_free(list
);
3020 if (pool_list_count(list
) == 0 && interval
== 0) {
3021 pool_list_free(list
);
3022 (void) fprintf(stderr
, gettext("no pools available\n"));
3027 * Enter the main iostat loop.
3030 cb
.cb_verbose
= verbose
;
3031 cb
.cb_iteration
= 0;
3032 cb
.cb_namewidth
= 0;
3035 pool_list_update(list
);
3037 if ((npools
= pool_list_count(list
)) == 0)
3041 * Refresh all statistics. This is done as an explicit step
3042 * before calculating the maximum name width, so that any
3043 * configuration changes are properly accounted for.
3045 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
, &cb
);
3048 * Iterate over all pools to determine the maximum width
3049 * for the pool / device name column across all pools.
3051 cb
.cb_namewidth
= 0;
3052 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
3054 if (timestamp_fmt
!= NODATE
)
3055 print_timestamp(timestamp_fmt
);
3058 * If it's the first time, or verbose mode, print the header.
3060 if (++cb
.cb_iteration
== 1 || verbose
)
3061 print_iostat_header(&cb
);
3063 (void) pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
3066 * If there's more than one pool, and we're not in verbose mode
3067 * (which prints a separator for us), then print a separator.
3069 if (npools
> 1 && !verbose
)
3070 print_iostat_separator(&cb
);
3073 (void) printf("\n");
3076 * Flush the output so that redirection to a file isn't buffered
3079 (void) fflush(stdout
);
3084 if (count
!= 0 && --count
== 0)
3087 (void) sleep(interval
);
3090 pool_list_free(list
);
3095 typedef struct list_cbdata
{
3096 boolean_t cb_verbose
;
3098 boolean_t cb_scripted
;
3099 zprop_list_t
*cb_proplist
;
3100 boolean_t cb_literal
;
3104 * Given a list of columns to display, output appropriate headers for each one.
3107 print_header(list_cbdata_t
*cb
)
3109 zprop_list_t
*pl
= cb
->cb_proplist
;
3110 char headerbuf
[ZPOOL_MAXPROPLEN
];
3112 boolean_t first
= B_TRUE
;
3113 boolean_t right_justify
;
3116 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3117 width
= pl
->pl_width
;
3118 if (first
&& cb
->cb_verbose
) {
3120 * Reset the width to accommodate the verbose listing
3123 width
= cb
->cb_namewidth
;
3131 right_justify
= B_FALSE
;
3132 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3133 header
= zpool_prop_column_name(pl
->pl_prop
);
3134 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3138 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
3139 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
3140 headerbuf
[i
] = '\0';
3144 if (pl
->pl_next
== NULL
&& !right_justify
)
3145 (void) printf("%s", header
);
3146 else if (right_justify
)
3147 (void) printf("%*s", width
, header
);
3149 (void) printf("%-*s", width
, header
);
3153 (void) printf("\n");
3157 * Given a pool and a list of properties, print out all the properties according
3158 * to the described layout.
3161 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
3163 zprop_list_t
*pl
= cb
->cb_proplist
;
3164 boolean_t first
= B_TRUE
;
3165 char property
[ZPOOL_MAXPROPLEN
];
3167 boolean_t right_justify
;
3170 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
3172 width
= pl
->pl_width
;
3173 if (first
&& cb
->cb_verbose
) {
3175 * Reset the width to accommodate the verbose listing
3178 width
= cb
->cb_namewidth
;
3182 if (cb
->cb_scripted
)
3183 (void) printf("\t");
3190 right_justify
= B_FALSE
;
3191 if (pl
->pl_prop
!= ZPROP_INVAL
) {
3192 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
3193 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
3198 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
3199 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
3200 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
3201 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
3202 sizeof (property
)) == 0) {
3210 * If this is being called in scripted mode, or if this is the
3211 * last column and it is left-justified, don't include a width
3214 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
3215 (void) printf("%s", propstr
);
3216 else if (right_justify
)
3217 (void) printf("%*s", width
, propstr
);
3219 (void) printf("%-*s", width
, propstr
);
3222 (void) printf("\n");
3226 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
3231 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
3234 case ZPOOL_PROP_EXPANDSZ
:
3235 case ZPOOL_PROP_CHECKPOINT
:
3237 (void) strlcpy(propval
, "-", sizeof (propval
));
3239 zfs_nicenum(value
, propval
, sizeof (propval
));
3241 case ZPOOL_PROP_FRAGMENTATION
:
3242 if (value
== ZFS_FRAG_INVALID
) {
3243 (void) strlcpy(propval
, "-", sizeof (propval
));
3245 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
3249 case ZPOOL_PROP_CAPACITY
:
3250 (void) snprintf(propval
, sizeof (propval
), "%llu%%", value
);
3253 zfs_nicenum(value
, propval
, sizeof (propval
));
3257 (void) strlcpy(propval
, "-", sizeof (propval
));
3260 (void) printf("\t%s", propval
);
3262 (void) printf(" %*s", width
, propval
);
3266 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
3267 list_cbdata_t
*cb
, int depth
)
3273 boolean_t scripted
= cb
->cb_scripted
;
3274 uint64_t islog
= B_FALSE
;
3275 boolean_t haslog
= B_FALSE
;
3276 char *dashes
= "%-*s - - - - - -\n";
3278 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3279 (uint64_t **)&vs
, &c
) == 0);
3282 boolean_t toplevel
= (vs
->vs_space
!= 0);
3285 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
3289 (void) printf("\t%s", name
);
3290 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
3291 (void) printf("%*s%s", depth
, "", name
);
3293 (void) printf("%*s%s%*s", depth
, "", name
,
3294 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
3297 * Print the properties for the individual vdevs. Some
3298 * properties are only applicable to toplevel vdevs. The
3299 * 'toplevel' boolean value is passed to the print_one_column()
3300 * to indicate that the value is valid.
3302 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
3304 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
3306 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
3307 scripted
, toplevel
);
3308 print_one_column(ZPOOL_PROP_CHECKPOINT
,
3309 vs
->vs_checkpoint_space
, scripted
, toplevel
);
3310 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
3312 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
3313 vs
->vs_fragmentation
, scripted
,
3314 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
));
3315 cap
= (vs
->vs_space
== 0) ? 0 :
3316 (vs
->vs_alloc
* 100 / vs
->vs_space
);
3317 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
);
3318 (void) printf("\n");
3321 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3322 &child
, &children
) != 0)
3325 for (c
= 0; c
< children
; c
++) {
3326 uint64_t ishole
= B_FALSE
;
3328 if (nvlist_lookup_uint64(child
[c
],
3329 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
3332 if (nvlist_lookup_uint64(child
[c
],
3333 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
3338 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3339 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3343 if (haslog
== B_TRUE
) {
3344 /* LINTED E_SEC_PRINTF_VAR_FMT */
3345 (void) printf(dashes
, cb
->cb_namewidth
, "log");
3346 for (c
= 0; c
< children
; c
++) {
3347 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3348 &islog
) != 0 || !islog
)
3350 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3351 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3356 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3357 &child
, &children
) == 0 && children
> 0) {
3358 /* LINTED E_SEC_PRINTF_VAR_FMT */
3359 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
3360 for (c
= 0; c
< children
; c
++) {
3361 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3362 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3367 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
3368 &children
) == 0 && children
> 0) {
3369 /* LINTED E_SEC_PRINTF_VAR_FMT */
3370 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
3371 for (c
= 0; c
< children
; c
++) {
3372 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], B_FALSE
);
3373 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
3381 * Generic callback function to list a pool.
3384 list_callback(zpool_handle_t
*zhp
, void *data
)
3386 list_cbdata_t
*cbp
= data
;
3390 config
= zpool_get_config(zhp
, NULL
);
3392 print_pool(zhp
, cbp
);
3393 if (!cbp
->cb_verbose
)
3396 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3398 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
3404 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3406 * -H Scripted mode. Don't display headers, and separate properties
3408 * -o List of properties to display. Defaults to
3409 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3410 * "dedupratio,health,altroot"
3411 * -p Diplay values in parsable (exact) format.
3412 * -T Display a timestamp in date(1) or Unix format
3414 * List all pools in the system, whether or not they're healthy. Output space
3415 * statistics for each one, as well as health status summary.
3418 zpool_do_list(int argc
, char **argv
)
3422 list_cbdata_t cb
= { 0 };
3423 static char default_props
[] =
3424 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
3425 "capacity,dedupratio,health,altroot";
3426 char *props
= default_props
;
3427 unsigned long interval
= 0, count
= 0;
3429 boolean_t first
= B_TRUE
;
3432 while ((c
= getopt(argc
, argv
, ":Ho:pT:v")) != -1) {
3435 cb
.cb_scripted
= B_TRUE
;
3441 cb
.cb_literal
= B_TRUE
;
3444 get_timestamp_arg(*optarg
);
3447 cb
.cb_verbose
= B_TRUE
;
3450 (void) fprintf(stderr
, gettext("missing argument for "
3451 "'%c' option\n"), optopt
);
3455 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3464 get_interval_count(&argc
, argv
, &interval
, &count
);
3466 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
3470 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
3474 if (pool_list_count(list
) == 0)
3477 cb
.cb_namewidth
= 0;
3478 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
, &cb
);
3480 if (timestamp_fmt
!= NODATE
)
3481 print_timestamp(timestamp_fmt
);
3483 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
3487 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
3492 if (count
!= 0 && --count
== 0)
3495 pool_list_free(list
);
3496 (void) sleep(interval
);
3499 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
3500 (void) printf(gettext("no pools available\n"));
3504 pool_list_free(list
);
3505 zprop_free_list(cb
.cb_proplist
);
3510 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
3512 boolean_t force
= B_FALSE
;
3515 char *poolname
, *old_disk
, *new_disk
;
3516 zpool_handle_t
*zhp
;
3517 zpool_boot_label_t boot_type
;
3522 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3528 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3537 /* get pool name and check number of arguments */
3539 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3546 (void) fprintf(stderr
,
3547 gettext("missing <device> specification\n"));
3555 (void) fprintf(stderr
,
3556 gettext("missing <new_device> specification\n"));
3559 new_disk
= old_disk
;
3569 (void) fprintf(stderr
, gettext("too many arguments\n"));
3573 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3576 if (zpool_get_config(zhp
, NULL
) == NULL
) {
3577 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
3583 if (zpool_is_bootable(zhp
))
3584 boot_type
= ZPOOL_COPY_BOOT_LABEL
;
3586 boot_type
= ZPOOL_NO_BOOT_LABEL
;
3588 boot_size
= zpool_get_prop_int(zhp
, ZPOOL_PROP_BOOTSIZE
, NULL
);
3589 nvroot
= make_root_vdev(zhp
, force
, B_FALSE
, replacing
, B_FALSE
,
3590 boot_type
, boot_size
, argc
, argv
);
3591 if (nvroot
== NULL
) {
3596 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
3598 nvlist_free(nvroot
);
3605 * zpool replace [-f] <pool> <device> <new_device>
3607 * -f Force attach, even if <new_device> appears to be in use.
3609 * Replace <device> with <new_device>.
3613 zpool_do_replace(int argc
, char **argv
)
3615 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
3619 * zpool attach [-f] <pool> <device> <new_device>
3621 * -f Force attach, even if <new_device> appears to be in use.
3623 * Attach <new_device> to the mirror containing <device>. If <device> is not
3624 * part of a mirror, then <device> will be transformed into a mirror of
3625 * <device> and <new_device>. In either case, <new_device> will begin life
3626 * with a DTL of [0, now], and will immediately begin to resilver itself.
3629 zpool_do_attach(int argc
, char **argv
)
3631 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
3635 * zpool detach [-f] <pool> <device>
3637 * -f Force detach of <device>, even if DTLs argue against it
3638 * (not supported yet)
3640 * Detach a device from a mirror. The operation will be refused if <device>
3641 * is the last device in the mirror, or if the DTLs indicate that this device
3642 * has the only valid copy of some data.
3646 zpool_do_detach(int argc
, char **argv
)
3649 char *poolname
, *path
;
3650 zpool_handle_t
*zhp
;
3654 while ((c
= getopt(argc
, argv
, "f")) != -1) {
3658 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3667 /* get pool name and check number of arguments */
3669 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
3674 (void) fprintf(stderr
,
3675 gettext("missing <device> specification\n"));
3682 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3685 ret
= zpool_vdev_detach(zhp
, path
);
3693 * zpool split [-n] [-o prop=val] ...
3695 * [-R altroot] <pool> <newpool> [<device> ...]
3697 * -n Do not split the pool, but display the resulting layout if
3698 * it were to be split.
3699 * -o Set property=value, or set mount options.
3700 * -R Mount the split-off pool under an alternate root.
3702 * Splits the named pool and gives it the new pool name. Devices to be split
3703 * off may be listed, provided that no more than one device is specified
3704 * per top-level vdev mirror. The newly split pool is left in an exported
3705 * state unless -R is specified.
3707 * Restrictions: the top-level of the pool pool must only be made up of
3708 * mirrors; all devices in the pool must be healthy; no device may be
3709 * undergoing a resilvering operation.
3712 zpool_do_split(int argc
, char **argv
)
3714 char *srcpool
, *newpool
, *propval
;
3715 char *mntopts
= NULL
;
3718 zpool_handle_t
*zhp
;
3719 nvlist_t
*config
, *props
= NULL
;
3721 flags
.dryrun
= B_FALSE
;
3722 flags
.import
= B_FALSE
;
3725 while ((c
= getopt(argc
, argv
, ":R:no:")) != -1) {
3728 flags
.import
= B_TRUE
;
3730 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
3731 &props
, B_TRUE
) != 0) {
3737 flags
.dryrun
= B_TRUE
;
3740 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3743 if (add_prop_list(optarg
, propval
,
3744 &props
, B_TRUE
) != 0) {
3753 (void) fprintf(stderr
, gettext("missing argument for "
3754 "'%c' option\n"), optopt
);
3758 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3765 if (!flags
.import
&& mntopts
!= NULL
) {
3766 (void) fprintf(stderr
, gettext("setting mntopts is only "
3767 "valid when importing the pool\n"));
3775 (void) fprintf(stderr
, gettext("Missing pool name\n"));
3779 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
3789 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
)
3792 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
3793 if (config
== NULL
) {
3797 (void) printf(gettext("would create '%s' with the "
3798 "following layout:\n\n"), newpool
);
3799 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
);
3801 nvlist_free(config
);
3806 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
)
3810 * The split was successful. Now we need to open the new
3811 * pool and import it.
3813 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
)
3815 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3816 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3818 (void) fprintf(stderr
, gettext("Split was successful, but "
3819 "the datasets could not all be mounted\n"));
3820 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
3821 "different altroot\n"), "zpool import");
3831 * zpool online <pool> <device> ...
3834 zpool_do_online(int argc
, char **argv
)
3838 zpool_handle_t
*zhp
;
3840 vdev_state_t newstate
;
3844 while ((c
= getopt(argc
, argv
, "et")) != -1) {
3847 flags
|= ZFS_ONLINE_EXPAND
;
3851 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3860 /* get pool name and check number of arguments */
3862 (void) fprintf(stderr
, gettext("missing pool name\n"));
3866 (void) fprintf(stderr
, gettext("missing device name\n"));
3872 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3875 for (i
= 1; i
< argc
; i
++) {
3876 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
3877 if (newstate
!= VDEV_STATE_HEALTHY
) {
3878 (void) printf(gettext("warning: device '%s' "
3879 "onlined, but remains in faulted state\n"),
3881 if (newstate
== VDEV_STATE_FAULTED
)
3882 (void) printf(gettext("use 'zpool "
3883 "clear' to restore a faulted "
3886 (void) printf(gettext("use 'zpool "
3887 "replace' to replace devices "
3888 "that are no longer present\n"));
3901 * zpool offline [-ft] <pool> <device> ...
3903 * -f Force the device into the offline state, even if doing
3904 * so would appear to compromise pool availability.
3905 * (not supported yet)
3907 * -t Only take the device off-line temporarily. The offline
3908 * state will not be persistent across reboots.
3912 zpool_do_offline(int argc
, char **argv
)
3916 zpool_handle_t
*zhp
;
3918 boolean_t istmp
= B_FALSE
;
3921 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
3928 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3937 /* get pool name and check number of arguments */
3939 (void) fprintf(stderr
, gettext("missing pool name\n"));
3943 (void) fprintf(stderr
, gettext("missing device name\n"));
3949 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
3952 for (i
= 1; i
< argc
; i
++) {
3953 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
3963 * zpool clear <pool> [device]
3965 * Clear all errors associated with a pool or a particular device.
3968 zpool_do_clear(int argc
, char **argv
)
3972 boolean_t dryrun
= B_FALSE
;
3973 boolean_t do_rewind
= B_FALSE
;
3974 boolean_t xtreme_rewind
= B_FALSE
;
3975 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3976 nvlist_t
*policy
= NULL
;
3977 zpool_handle_t
*zhp
;
3978 char *pool
, *device
;
3981 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
3990 xtreme_rewind
= B_TRUE
;
3993 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4003 (void) fprintf(stderr
, gettext("missing pool name\n"));
4008 (void) fprintf(stderr
, gettext("too many arguments\n"));
4012 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
4013 (void) fprintf(stderr
,
4014 gettext("-n or -X only meaningful with -F\n"));
4018 rewind_policy
= ZPOOL_TRY_REWIND
;
4020 rewind_policy
= ZPOOL_DO_REWIND
;
4022 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
4024 /* In future, further rewind policy choices can be passed along here */
4025 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
4026 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
4027 rewind_policy
) != 0) {
4032 device
= argc
== 2 ? argv
[1] : NULL
;
4034 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
4035 nvlist_free(policy
);
4039 if (zpool_clear(zhp
, device
, policy
) != 0)
4044 nvlist_free(policy
);
4050 * zpool reguid <pool>
4053 zpool_do_reguid(int argc
, char **argv
)
4057 zpool_handle_t
*zhp
;
4061 while ((c
= getopt(argc
, argv
, "")) != -1) {
4064 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4073 /* get pool name and check number of arguments */
4075 (void) fprintf(stderr
, gettext("missing pool name\n"));
4080 (void) fprintf(stderr
, gettext("too many arguments\n"));
4085 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
4088 ret
= zpool_reguid(zhp
);
4096 * zpool reopen <pool>
4098 * Reopen the pool so that the kernel can update the sizes of all vdevs.
4101 zpool_do_reopen(int argc
, char **argv
)
4105 zpool_handle_t
*zhp
;
4109 while ((c
= getopt(argc
, argv
, "")) != -1) {
4112 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4122 (void) fprintf(stderr
, gettext("missing pool name\n"));
4127 (void) fprintf(stderr
, gettext("too many arguments\n"));
4132 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
4135 ret
= zpool_reopen(zhp
);
4140 typedef struct scrub_cbdata
{
4144 pool_scrub_cmd_t cb_scrub_cmd
;
4148 zpool_has_checkpoint(zpool_handle_t
*zhp
)
4150 nvlist_t
*config
, *nvroot
;
4152 config
= zpool_get_config(zhp
, NULL
);
4154 if (config
!= NULL
) {
4155 pool_checkpoint_stat_t
*pcs
= NULL
;
4158 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
4159 (void) nvlist_lookup_uint64_array(nvroot
,
4160 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
4162 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
4165 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
4166 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
4174 scrub_callback(zpool_handle_t
*zhp
, void *data
)
4176 scrub_cbdata_t
*cb
= data
;
4180 * Ignore faulted pools.
4182 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
4183 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
4184 "currently unavailable\n"), zpool_get_name(zhp
));
4188 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
4190 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
4191 cb
->cb_type
== POOL_SCAN_SCRUB
) {
4192 (void) printf(gettext("warning: will not scrub state that "
4193 "belongs to the checkpoint of pool '%s'\n"),
4194 zpool_get_name(zhp
));
4201 * zpool scrub [-s | -p] <pool> ...
4203 * -s Stop. Stops any in-progress scrub.
4204 * -p Pause. Pause in-progress scrub.
4207 zpool_do_scrub(int argc
, char **argv
)
4212 cb
.cb_type
= POOL_SCAN_SCRUB
;
4213 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
4216 while ((c
= getopt(argc
, argv
, "sp")) != -1) {
4219 cb
.cb_type
= POOL_SCAN_NONE
;
4222 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
4225 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4231 if (cb
.cb_type
== POOL_SCAN_NONE
&&
4232 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
4233 (void) fprintf(stderr
, gettext("invalid option combination: "
4234 "-s and -p are mutually exclusive\n"));
4244 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4248 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
4252 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
4254 uint_t children
= 0;
4258 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
4261 if (children
== 0) {
4262 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
, B_FALSE
);
4263 fnvlist_add_boolean(res
, path
);
4268 for (i
= 0; i
< children
; i
++) {
4269 zpool_collect_leaves(zhp
, child
[i
], res
);
4274 * zpool initialize [-cs] <pool> [<vdev> ...]
4275 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
4276 * if none specified.
4278 * -c Cancel. Ends active initializing.
4279 * -s Suspend. Initializing can then be restarted with no flags.
4282 zpool_do_initialize(int argc
, char **argv
)
4286 zpool_handle_t
*zhp
;
4290 struct option long_options
[] = {
4291 {"cancel", no_argument
, NULL
, 'c'},
4292 {"suspend", no_argument
, NULL
, 's'},
4296 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_DO
;
4297 while ((c
= getopt_long(argc
, argv
, "cs", long_options
, NULL
)) != -1) {
4300 if (cmd_type
!= POOL_INITIALIZE_DO
) {
4301 (void) fprintf(stderr
, gettext("-c cannot be "
4302 "combined with other options\n"));
4305 cmd_type
= POOL_INITIALIZE_CANCEL
;
4308 if (cmd_type
!= POOL_INITIALIZE_DO
) {
4309 (void) fprintf(stderr
, gettext("-s cannot be "
4310 "combined with other options\n"));
4313 cmd_type
= POOL_INITIALIZE_SUSPEND
;
4317 (void) fprintf(stderr
,
4318 gettext("invalid option '%c'\n"), optopt
);
4320 (void) fprintf(stderr
,
4321 gettext("invalid option '%s'\n"),
4332 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4338 zhp
= zpool_open(g_zfs
, poolname
);
4342 vdevs
= fnvlist_alloc();
4344 /* no individual leaf vdevs specified, so add them all */
4345 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
4346 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
4347 ZPOOL_CONFIG_VDEV_TREE
);
4348 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
4351 for (i
= 1; i
< argc
; i
++) {
4352 fnvlist_add_boolean(vdevs
, argv
[i
]);
4356 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
4358 fnvlist_free(vdevs
);
4364 typedef struct status_cbdata
{
4366 boolean_t cb_allpools
;
4367 boolean_t cb_verbose
;
4368 boolean_t cb_explain
;
4370 boolean_t cb_dedup_stats
;
4374 * Print out detailed scrub status.
4377 print_scan_status(pool_scan_stat_t
*ps
)
4379 time_t start
, end
, pause
;
4380 uint64_t elapsed
, mins_left
, hours_left
;
4381 uint64_t pass_exam
, examined
, total
;
4383 double fraction_done
;
4384 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4386 (void) printf(gettext(" scan: "));
4388 /* If there's never been a scan, there's not much to say. */
4389 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
4390 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
4391 (void) printf(gettext("none requested\n"));
4395 start
= ps
->pss_start_time
;
4396 end
= ps
->pss_end_time
;
4397 pause
= ps
->pss_pass_scrub_pause
;
4398 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
4400 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
4401 ps
->pss_func
== POOL_SCAN_RESILVER
);
4403 * Scan is finished or canceled.
4405 if (ps
->pss_state
== DSS_FINISHED
) {
4406 uint64_t minutes_taken
= (end
- start
) / 60;
4409 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4410 fmt
= gettext("scrub repaired %s in %lluh%um with "
4411 "%llu errors on %s");
4412 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4413 fmt
= gettext("resilvered %s in %lluh%um with "
4414 "%llu errors on %s");
4417 (void) printf(fmt
, processed_buf
,
4418 (u_longlong_t
)(minutes_taken
/ 60),
4419 (uint_t
)(minutes_taken
% 60),
4420 (u_longlong_t
)ps
->pss_errors
,
4421 ctime((time_t *)&end
));
4423 } else if (ps
->pss_state
== DSS_CANCELED
) {
4424 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4425 (void) printf(gettext("scrub canceled on %s"),
4427 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4428 (void) printf(gettext("resilver canceled on %s"),
4434 assert(ps
->pss_state
== DSS_SCANNING
);
4437 * Scan is in progress.
4439 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4441 (void) printf(gettext("scrub in progress since %s"),
4445 struct tm
*p
= localtime(&pause
);
4446 (void) strftime(buf
, sizeof (buf
), "%a %b %e %T %Y", p
);
4447 (void) printf(gettext("scrub paused since %s\n"), buf
);
4448 (void) printf(gettext("\tscrub started on %s"),
4451 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4452 (void) printf(gettext("resilver in progress since %s"),
4456 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
4457 total
= ps
->pss_to_examine
;
4458 fraction_done
= (double)examined
/ total
;
4460 /* elapsed time for this pass */
4461 elapsed
= time(NULL
) - ps
->pss_pass_start
;
4462 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
4463 elapsed
= elapsed
? elapsed
: 1;
4464 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
4465 rate
= pass_exam
/ elapsed
;
4466 rate
= rate
? rate
: 1;
4467 mins_left
= ((total
- examined
) / rate
) / 60;
4468 hours_left
= mins_left
/ 60;
4470 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
4471 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4474 * do not print estimated time if hours_left is more than 30 days
4475 * or we have a paused scrub
4478 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4479 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
4480 examined_buf
, total_buf
, rate_buf
);
4481 if (hours_left
< (30 * 24)) {
4482 (void) printf(gettext(", %lluh%um to go\n"),
4483 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4485 (void) printf(gettext(
4486 ", (scan is slow, no estimated time)\n"));
4489 (void) printf(gettext("\t%s scanned out of %s\n"),
4490 examined_buf
, total_buf
);
4493 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
4494 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4495 processed_buf
, 100 * fraction_done
);
4496 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
4497 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4498 processed_buf
, 100 * fraction_done
);
4503 * As we don't scrub checkpointed blocks, we want to warn the
4504 * user that we skipped scanning some blocks if a checkpoint exists
4505 * or existed at any time during the scan.
4508 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
4510 if (ps
== NULL
|| pcs
== NULL
)
4513 if (pcs
->pcs_state
== CS_NONE
||
4514 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
4517 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
4519 if (ps
->pss_state
== DSS_NONE
)
4522 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
4523 ps
->pss_end_time
< pcs
->pcs_start_time
)
4526 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
4527 (void) printf(gettext(" scan warning: skipped blocks "
4528 "that are only referenced by the checkpoint.\n"));
4530 assert(ps
->pss_state
== DSS_SCANNING
);
4531 (void) printf(gettext(" scan warning: skipping blocks "
4532 "that are only referenced by the checkpoint.\n"));
4537 * Print out detailed removal status.
4540 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
4542 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
4544 nvlist_t
*config
, *nvroot
;
4549 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
4553 * Determine name of vdev.
4555 config
= zpool_get_config(zhp
, NULL
);
4556 nvroot
= fnvlist_lookup_nvlist(config
,
4557 ZPOOL_CONFIG_VDEV_TREE
);
4558 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
4559 &child
, &children
) == 0);
4560 assert(prs
->prs_removing_vdev
< children
);
4561 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
4562 child
[prs
->prs_removing_vdev
], B_TRUE
);
4564 (void) printf(gettext("remove: "));
4566 start
= prs
->prs_start_time
;
4567 end
= prs
->prs_end_time
;
4568 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
4571 * Removal is finished or canceled.
4573 if (prs
->prs_state
== DSS_FINISHED
) {
4574 uint64_t minutes_taken
= (end
- start
) / 60;
4576 (void) printf(gettext("Removal of vdev %llu copied %s "
4577 "in %lluh%um, completed on %s"),
4578 (longlong_t
)prs
->prs_removing_vdev
,
4580 (u_longlong_t
)(minutes_taken
/ 60),
4581 (uint_t
)(minutes_taken
% 60),
4582 ctime((time_t *)&end
));
4583 } else if (prs
->prs_state
== DSS_CANCELED
) {
4584 (void) printf(gettext("Removal of %s canceled on %s"),
4585 vdev_name
, ctime(&end
));
4587 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
4588 double fraction_done
;
4591 assert(prs
->prs_state
== DSS_SCANNING
);
4594 * Removal is in progress.
4596 (void) printf(gettext(
4597 "Evacuation of %s in progress since %s"),
4598 vdev_name
, ctime(&start
));
4600 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
4601 total
= prs
->prs_to_copy
;
4602 fraction_done
= (double)copied
/ total
;
4604 /* elapsed time for this pass */
4605 elapsed
= time(NULL
) - prs
->prs_start_time
;
4606 elapsed
= elapsed
> 0 ? elapsed
: 1;
4607 rate
= copied
/ elapsed
;
4608 rate
= rate
> 0 ? rate
: 1;
4609 mins_left
= ((total
- copied
) / rate
) / 60;
4610 hours_left
= mins_left
/ 60;
4612 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
4613 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
4614 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
4617 * do not print estimated time if hours_left is more than
4620 (void) printf(gettext(" %s copied out of %s at %s/s, "
4622 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
4623 if (hours_left
< (30 * 24)) {
4624 (void) printf(gettext(", %lluh%um to go\n"),
4625 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
4627 (void) printf(gettext(
4628 ", (copy is slow, no estimated time)\n"));
4632 if (prs
->prs_mapping_memory
> 0) {
4634 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
4635 (void) printf(gettext(" %s memory used for "
4636 "removed device mappings\n"),
4642 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
4647 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
4650 (void) printf(gettext("checkpoint: "));
4652 start
= pcs
->pcs_start_time
;
4653 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
4655 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
4656 char *date
= ctime(&start
);
4659 * ctime() adds a newline at the end of the generated
4660 * string, thus the weird format specifier and the
4661 * strlen() call used to chop it off from the output.
4663 (void) printf(gettext("created %.*s, consumes %s\n"),
4664 strlen(date
) - 1, date
, space_buf
);
4668 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
4670 (void) printf(gettext("discarding, %s remaining.\n"),
4675 print_error_log(zpool_handle_t
*zhp
)
4677 nvlist_t
*nverrlist
= NULL
;
4680 size_t len
= MAXPATHLEN
* 2;
4682 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
4683 (void) printf("errors: List of errors unavailable "
4684 "(insufficient privileges)\n");
4688 (void) printf("errors: Permanent errors have been "
4689 "detected in the following files:\n\n");
4691 pathname
= safe_malloc(len
);
4693 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
4695 uint64_t dsobj
, obj
;
4697 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
4698 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
4700 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
4702 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
4703 (void) printf("%7s %s\n", "", pathname
);
4706 nvlist_free(nverrlist
);
4710 print_spares(zpool_handle_t
*zhp
, nvlist_t
**spares
, uint_t nspares
,
4719 (void) printf(gettext("\tspares\n"));
4721 for (i
= 0; i
< nspares
; i
++) {
4722 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
], B_FALSE
);
4723 print_status_config(zhp
, name
, spares
[i
],
4724 namewidth
, 2, B_TRUE
);
4730 print_l2cache(zpool_handle_t
*zhp
, nvlist_t
**l2cache
, uint_t nl2cache
,
4739 (void) printf(gettext("\tcache\n"));
4741 for (i
= 0; i
< nl2cache
; i
++) {
4742 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
], B_FALSE
);
4743 print_status_config(zhp
, name
, l2cache
[i
],
4744 namewidth
, 2, B_FALSE
);
4750 print_dedup_stats(nvlist_t
*config
)
4752 ddt_histogram_t
*ddh
;
4758 * If the pool was faulted then we may not have been able to
4759 * obtain the config. Otherwise, if we have anything in the dedup
4760 * table continue processing the stats.
4762 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
4763 (uint64_t **)&ddo
, &c
) != 0)
4766 (void) printf("\n");
4767 (void) printf(gettext(" dedup: "));
4768 if (ddo
->ddo_count
== 0) {
4769 (void) printf(gettext("no DDT entries\n"));
4773 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4774 (u_longlong_t
)ddo
->ddo_count
,
4775 (u_longlong_t
)ddo
->ddo_dspace
,
4776 (u_longlong_t
)ddo
->ddo_mspace
);
4778 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
4779 (uint64_t **)&dds
, &c
) == 0);
4780 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
4781 (uint64_t **)&ddh
, &c
) == 0);
4782 zpool_dump_ddt(dds
, ddh
);
4786 * Display a summary of pool status. Displays a summary such as:
4790 * reason: One or more devices ...
4791 * see: http://illumos.org/msg/ZFS-xxxx-01
4797 * When given the '-v' option, we print out the complete config. If the '-e'
4798 * option is specified, then we print out error rate information as well.
4801 status_callback(zpool_handle_t
*zhp
, void *data
)
4803 status_cbdata_t
*cbp
= data
;
4804 nvlist_t
*config
, *nvroot
;
4811 config
= zpool_get_config(zhp
, NULL
);
4812 reason
= zpool_get_status(zhp
, &msgid
);
4817 * If we were given 'zpool status -x', only report those pools with
4820 if (cbp
->cb_explain
&&
4821 (reason
== ZPOOL_STATUS_OK
||
4822 reason
== ZPOOL_STATUS_VERSION_OLDER
||
4823 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
4824 if (!cbp
->cb_allpools
) {
4825 (void) printf(gettext("pool '%s' is healthy\n"),
4826 zpool_get_name(zhp
));
4828 cbp
->cb_first
= B_FALSE
;
4834 cbp
->cb_first
= B_FALSE
;
4836 (void) printf("\n");
4838 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
4839 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
4840 (uint64_t **)&vs
, &c
) == 0);
4841 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
4843 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
4844 (void) printf(gettext(" state: %s\n"), health
);
4847 case ZPOOL_STATUS_MISSING_DEV_R
:
4848 (void) printf(gettext("status: One or more devices could not "
4849 "be opened. Sufficient replicas exist for\n\tthe pool to "
4850 "continue functioning in a degraded state.\n"));
4851 (void) printf(gettext("action: Attach the missing device and "
4852 "online it using 'zpool online'.\n"));
4855 case ZPOOL_STATUS_MISSING_DEV_NR
:
4856 (void) printf(gettext("status: One or more devices could not "
4857 "be opened. There are insufficient\n\treplicas for the "
4858 "pool to continue functioning.\n"));
4859 (void) printf(gettext("action: Attach the missing device and "
4860 "online it using 'zpool online'.\n"));
4863 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
4864 (void) printf(gettext("status: One or more devices could not "
4865 "be used because the label is missing or\n\tinvalid. "
4866 "Sufficient replicas exist for the pool to continue\n\t"
4867 "functioning in a degraded state.\n"));
4868 (void) printf(gettext("action: Replace the device using "
4869 "'zpool replace'.\n"));
4872 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
4873 (void) printf(gettext("status: One or more devices could not "
4874 "be used because the label is missing \n\tor invalid. "
4875 "There are insufficient replicas for the pool to "
4876 "continue\n\tfunctioning.\n"));
4877 zpool_explain_recover(zpool_get_handle(zhp
),
4878 zpool_get_name(zhp
), reason
, config
);
4881 case ZPOOL_STATUS_FAILING_DEV
:
4882 (void) printf(gettext("status: One or more devices has "
4883 "experienced an unrecoverable error. An\n\tattempt was "
4884 "made to correct the error. Applications are "
4886 (void) printf(gettext("action: Determine if the device needs "
4887 "to be replaced, and clear the errors\n\tusing "
4888 "'zpool clear' or replace the device with 'zpool "
4892 case ZPOOL_STATUS_OFFLINE_DEV
:
4893 (void) printf(gettext("status: One or more devices has "
4894 "been taken offline by the administrator.\n\tSufficient "
4895 "replicas exist for the pool to continue functioning in "
4896 "a\n\tdegraded state.\n"));
4897 (void) printf(gettext("action: Online the device using "
4898 "'zpool online' or replace the device with\n\t'zpool "
4902 case ZPOOL_STATUS_REMOVED_DEV
:
4903 (void) printf(gettext("status: One or more devices has "
4904 "been removed by the administrator.\n\tSufficient "
4905 "replicas exist for the pool to continue functioning in "
4906 "a\n\tdegraded state.\n"));
4907 (void) printf(gettext("action: Online the device using "
4908 "'zpool online' or replace the device with\n\t'zpool "
4912 case ZPOOL_STATUS_RESILVERING
:
4913 (void) printf(gettext("status: One or more devices is "
4914 "currently being resilvered. The pool will\n\tcontinue "
4915 "to function, possibly in a degraded state.\n"));
4916 (void) printf(gettext("action: Wait for the resilver to "
4920 case ZPOOL_STATUS_CORRUPT_DATA
:
4921 (void) printf(gettext("status: One or more devices has "
4922 "experienced an error resulting in data\n\tcorruption. "
4923 "Applications may be affected.\n"));
4924 (void) printf(gettext("action: Restore the file in question "
4925 "if possible. Otherwise restore the\n\tentire pool from "
4929 case ZPOOL_STATUS_CORRUPT_POOL
:
4930 (void) printf(gettext("status: The pool metadata is corrupted "
4931 "and the pool cannot be opened.\n"));
4932 zpool_explain_recover(zpool_get_handle(zhp
),
4933 zpool_get_name(zhp
), reason
, config
);
4936 case ZPOOL_STATUS_VERSION_OLDER
:
4937 (void) printf(gettext("status: The pool is formatted using a "
4938 "legacy on-disk format. The pool can\n\tstill be used, "
4939 "but some features are unavailable.\n"));
4940 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4941 "upgrade'. Once this is done, the\n\tpool will no longer "
4942 "be accessible on software that does not support feature\n"
4946 case ZPOOL_STATUS_VERSION_NEWER
:
4947 (void) printf(gettext("status: The pool has been upgraded to a "
4948 "newer, incompatible on-disk version.\n\tThe pool cannot "
4949 "be accessed on this system.\n"));
4950 (void) printf(gettext("action: Access the pool from a system "
4951 "running more recent software, or\n\trestore the pool from "
4955 case ZPOOL_STATUS_FEAT_DISABLED
:
4956 (void) printf(gettext("status: Some supported features are not "
4957 "enabled on the pool. The pool can\n\tstill be used, but "
4958 "some features are unavailable.\n"));
4959 (void) printf(gettext("action: Enable all features using "
4960 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4961 "longer be accessible by software that does not support\n\t"
4962 "the features. See zpool-features(5) for details.\n"));
4965 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
4966 (void) printf(gettext("status: The pool cannot be accessed on "
4967 "this system because it uses the\n\tfollowing feature(s) "
4968 "not supported on this system:\n"));
4969 zpool_print_unsup_feat(config
);
4970 (void) printf("\n");
4971 (void) printf(gettext("action: Access the pool from a system "
4972 "that supports the required feature(s),\n\tor restore the "
4973 "pool from backup.\n"));
4976 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
4977 (void) printf(gettext("status: The pool can only be accessed "
4978 "in read-only mode on this system. It\n\tcannot be "
4979 "accessed in read-write mode because it uses the "
4980 "following\n\tfeature(s) not supported on this system:\n"));
4981 zpool_print_unsup_feat(config
);
4982 (void) printf("\n");
4983 (void) printf(gettext("action: The pool cannot be accessed in "
4984 "read-write mode. Import the pool with\n"
4985 "\t\"-o readonly=on\", access the pool from a system that "
4986 "supports the\n\trequired feature(s), or restore the "
4987 "pool from backup.\n"));
4990 case ZPOOL_STATUS_FAULTED_DEV_R
:
4991 (void) printf(gettext("status: One or more devices are "
4992 "faulted in response to persistent errors.\n\tSufficient "
4993 "replicas exist for the pool to continue functioning "
4994 "in a\n\tdegraded state.\n"));
4995 (void) printf(gettext("action: Replace the faulted device, "
4996 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4999 case ZPOOL_STATUS_FAULTED_DEV_NR
:
5000 (void) printf(gettext("status: One or more devices are "
5001 "faulted in response to persistent errors. There are "
5002 "insufficient replicas for the pool to\n\tcontinue "
5004 (void) printf(gettext("action: Destroy and re-create the pool "
5005 "from a backup source. Manually marking the device\n"
5006 "\trepaired using 'zpool clear' may allow some data "
5007 "to be recovered.\n"));
5010 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
5011 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
5012 (void) printf(gettext("status: One or more devices are "
5013 "faulted in response to IO failures.\n"));
5014 (void) printf(gettext("action: Make sure the affected devices "
5015 "are connected, then run 'zpool clear'.\n"));
5018 case ZPOOL_STATUS_BAD_LOG
:
5019 (void) printf(gettext("status: An intent log record "
5020 "could not be read.\n"
5021 "\tWaiting for adminstrator intervention to fix the "
5022 "faulted pool.\n"));
5023 (void) printf(gettext("action: Either restore the affected "
5024 "device(s) and run 'zpool online',\n"
5025 "\tor ignore the intent log records by running "
5026 "'zpool clear'.\n"));
5031 * The remaining errors can't actually be generated, yet.
5033 assert(reason
== ZPOOL_STATUS_OK
);
5037 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
5040 if (config
!= NULL
) {
5043 nvlist_t
**spares
, **l2cache
;
5044 uint_t nspares
, nl2cache
;
5045 pool_checkpoint_stat_t
*pcs
= NULL
;
5046 pool_scan_stat_t
*ps
= NULL
;
5047 pool_removal_stat_t
*prs
= NULL
;
5049 (void) nvlist_lookup_uint64_array(nvroot
,
5050 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
5051 (void) nvlist_lookup_uint64_array(nvroot
,
5052 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
5053 (void) nvlist_lookup_uint64_array(nvroot
,
5054 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
5056 print_scan_status(ps
);
5057 print_checkpoint_scan_warning(ps
, pcs
);
5058 print_removal_status(zhp
, prs
);
5059 print_checkpoint_status(pcs
);
5061 namewidth
= max_width(zhp
, nvroot
, 0, 0);
5065 (void) printf(gettext("config:\n\n"));
5066 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth
,
5067 "NAME", "STATE", "READ", "WRITE", "CKSUM");
5068 print_status_config(zhp
, zpool_get_name(zhp
), nvroot
,
5069 namewidth
, 0, B_FALSE
);
5071 if (num_logs(nvroot
) > 0)
5072 print_logs(zhp
, nvroot
, namewidth
, B_TRUE
);
5073 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5074 &l2cache
, &nl2cache
) == 0)
5075 print_l2cache(zhp
, l2cache
, nl2cache
, namewidth
);
5077 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5078 &spares
, &nspares
) == 0)
5079 print_spares(zhp
, spares
, nspares
, namewidth
);
5081 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
5083 nvlist_t
*nverrlist
= NULL
;
5086 * If the approximate error count is small, get a
5087 * precise count by fetching the entire log and
5088 * uniquifying the results.
5090 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
5091 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
5096 while ((elem
= nvlist_next_nvpair(nverrlist
,
5101 nvlist_free(nverrlist
);
5103 (void) printf("\n");
5106 (void) printf(gettext("errors: No known data "
5108 else if (!cbp
->cb_verbose
)
5109 (void) printf(gettext("errors: %llu data "
5110 "errors, use '-v' for a list\n"),
5111 (u_longlong_t
)nerr
);
5113 print_error_log(zhp
);
5116 if (cbp
->cb_dedup_stats
)
5117 print_dedup_stats(config
);
5119 (void) printf(gettext("config: The configuration cannot be "
5127 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
5129 * -v Display complete error logs
5130 * -x Display only pools with potential problems
5131 * -D Display dedup status (undocumented)
5132 * -T Display a timestamp in date(1) or Unix format
5134 * Describes the health status of all pools or some subset.
5137 zpool_do_status(int argc
, char **argv
)
5141 unsigned long interval
= 0, count
= 0;
5142 status_cbdata_t cb
= { 0 };
5145 while ((c
= getopt(argc
, argv
, "vxDT:")) != -1) {
5148 cb
.cb_verbose
= B_TRUE
;
5151 cb
.cb_explain
= B_TRUE
;
5154 cb
.cb_dedup_stats
= B_TRUE
;
5157 get_timestamp_arg(*optarg
);
5160 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5169 get_interval_count(&argc
, argv
, &interval
, &count
);
5172 cb
.cb_allpools
= B_TRUE
;
5174 cb
.cb_first
= B_TRUE
;
5177 if (timestamp_fmt
!= NODATE
)
5178 print_timestamp(timestamp_fmt
);
5180 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
5181 status_callback
, &cb
);
5183 if (argc
== 0 && cb
.cb_count
== 0)
5184 (void) printf(gettext("no pools available\n"));
5185 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
5186 (void) printf(gettext("all pools are healthy\n"));
5194 if (count
!= 0 && --count
== 0)
5197 (void) sleep(interval
);
5203 typedef struct upgrade_cbdata
{
5206 uint64_t cb_version
;
5211 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
5215 uint64_t oldversion
;
5217 config
= zpool_get_config(zhp
, NULL
);
5218 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
5221 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
5222 assert(oldversion
< version
);
5224 ret
= zpool_upgrade(zhp
, version
);
5228 if (version
>= SPA_VERSION_FEATURES
) {
5229 (void) printf(gettext("Successfully upgraded "
5230 "'%s' from version %llu to feature flags.\n"),
5231 zpool_get_name(zhp
), oldversion
);
5233 (void) printf(gettext("Successfully upgraded "
5234 "'%s' from version %llu to version %llu.\n"),
5235 zpool_get_name(zhp
), oldversion
, version
);
5242 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
5245 boolean_t firstff
= B_TRUE
;
5246 nvlist_t
*enabled
= zpool_get_features(zhp
);
5249 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5250 const char *fname
= spa_feature_table
[i
].fi_uname
;
5251 const char *fguid
= spa_feature_table
[i
].fi_guid
;
5252 if (!nvlist_exists(enabled
, fguid
)) {
5254 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
5255 ret
= zpool_set_prop(zhp
, propname
,
5256 ZFS_FEATURE_ENABLED
);
5264 (void) printf(gettext("Enabled the "
5265 "following features on '%s':\n"),
5266 zpool_get_name(zhp
));
5269 (void) printf(gettext(" %s\n"), fname
);
5280 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
5282 upgrade_cbdata_t
*cbp
= arg
;
5285 boolean_t printnl
= B_FALSE
;
5288 config
= zpool_get_config(zhp
, NULL
);
5289 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
5292 assert(SPA_VERSION_IS_SUPPORTED(version
));
5294 if (version
< cbp
->cb_version
) {
5295 cbp
->cb_first
= B_FALSE
;
5296 ret
= upgrade_version(zhp
, cbp
->cb_version
);
5302 * If they did "zpool upgrade -a", then we could
5303 * be doing ioctls to different pools. We need
5304 * to log this history once to each pool, and bypass
5305 * the normal history logging that happens in main().
5307 (void) zpool_log_history(g_zfs
, history_str
);
5308 log_history
= B_FALSE
;
5311 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
5313 ret
= upgrade_enable_all(zhp
, &count
);
5318 cbp
->cb_first
= B_FALSE
;
5324 (void) printf(gettext("\n"));
5331 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
5333 upgrade_cbdata_t
*cbp
= arg
;
5337 config
= zpool_get_config(zhp
, NULL
);
5338 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
5341 assert(SPA_VERSION_IS_SUPPORTED(version
));
5343 if (version
< SPA_VERSION_FEATURES
) {
5344 if (cbp
->cb_first
) {
5345 (void) printf(gettext("The following pools are "
5346 "formatted with legacy version numbers and can\n"
5347 "be upgraded to use feature flags. After "
5348 "being upgraded, these pools\nwill no "
5349 "longer be accessible by software that does not "
5350 "support feature\nflags.\n\n"));
5351 (void) printf(gettext("VER POOL\n"));
5352 (void) printf(gettext("--- ------------\n"));
5353 cbp
->cb_first
= B_FALSE
;
5356 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
5357 zpool_get_name(zhp
));
5364 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
5366 upgrade_cbdata_t
*cbp
= arg
;
5370 config
= zpool_get_config(zhp
, NULL
);
5371 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
5374 if (version
>= SPA_VERSION_FEATURES
) {
5376 boolean_t poolfirst
= B_TRUE
;
5377 nvlist_t
*enabled
= zpool_get_features(zhp
);
5379 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5380 const char *fguid
= spa_feature_table
[i
].fi_guid
;
5381 const char *fname
= spa_feature_table
[i
].fi_uname
;
5382 if (!nvlist_exists(enabled
, fguid
)) {
5383 if (cbp
->cb_first
) {
5384 (void) printf(gettext("\nSome "
5385 "supported features are not "
5386 "enabled on the following pools. "
5387 "Once a\nfeature is enabled the "
5388 "pool may become incompatible with "
5389 "software\nthat does not support "
5391 "zpool-features(5) for "
5393 (void) printf(gettext("POOL "
5395 (void) printf(gettext("------"
5397 cbp
->cb_first
= B_FALSE
;
5401 (void) printf(gettext("%s\n"),
5402 zpool_get_name(zhp
));
5403 poolfirst
= B_FALSE
;
5406 (void) printf(gettext(" %s\n"), fname
);
5416 upgrade_one(zpool_handle_t
*zhp
, void *data
)
5418 boolean_t printnl
= B_FALSE
;
5419 upgrade_cbdata_t
*cbp
= data
;
5420 uint64_t cur_version
;
5423 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
5424 (void) printf(gettext("'log' is now a reserved word\n"
5425 "Pool 'log' must be renamed using export and import"
5430 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5431 if (cur_version
> cbp
->cb_version
) {
5432 (void) printf(gettext("Pool '%s' is already formatted "
5433 "using more current version '%llu'.\n\n"),
5434 zpool_get_name(zhp
), cur_version
);
5438 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
5439 (void) printf(gettext("Pool '%s' is already formatted "
5440 "using version %llu.\n\n"), zpool_get_name(zhp
),
5445 if (cur_version
!= cbp
->cb_version
) {
5447 ret
= upgrade_version(zhp
, cbp
->cb_version
);
5452 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
5454 ret
= upgrade_enable_all(zhp
, &count
);
5460 } else if (cur_version
== SPA_VERSION
) {
5461 (void) printf(gettext("Pool '%s' already has all "
5462 "supported features enabled.\n"),
5463 zpool_get_name(zhp
));
5468 (void) printf(gettext("\n"));
5477 * zpool upgrade [-V version] <-a | pool ...>
5479 * With no arguments, display downrev'd ZFS pool available for upgrade.
5480 * Individual pools can be upgraded by specifying the pool, and '-a' will
5481 * upgrade all pools.
5484 zpool_do_upgrade(int argc
, char **argv
)
5487 upgrade_cbdata_t cb
= { 0 };
5489 boolean_t showversions
= B_FALSE
;
5490 boolean_t upgradeall
= B_FALSE
;
5495 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
5498 upgradeall
= B_TRUE
;
5501 showversions
= B_TRUE
;
5504 cb
.cb_version
= strtoll(optarg
, &end
, 10);
5506 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
5507 (void) fprintf(stderr
,
5508 gettext("invalid version '%s'\n"), optarg
);
5513 (void) fprintf(stderr
, gettext("missing argument for "
5514 "'%c' option\n"), optopt
);
5518 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5529 if (cb
.cb_version
== 0) {
5530 cb
.cb_version
= SPA_VERSION
;
5531 } else if (!upgradeall
&& argc
== 0) {
5532 (void) fprintf(stderr
, gettext("-V option is "
5533 "incompatible with other arguments\n"));
5538 if (upgradeall
|| argc
!= 0) {
5539 (void) fprintf(stderr
, gettext("-v option is "
5540 "incompatible with other arguments\n"));
5543 } else if (upgradeall
) {
5545 (void) fprintf(stderr
, gettext("-a option should not "
5546 "be used along with a pool name\n"));
5551 (void) printf(gettext("This system supports ZFS pool feature "
5556 (void) printf(gettext("The following features are "
5558 (void) printf(gettext("FEAT DESCRIPTION\n"));
5559 (void) printf("----------------------------------------------"
5560 "---------------\n");
5561 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5562 zfeature_info_t
*fi
= &spa_feature_table
[i
];
5564 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
5565 " (read-only compatible)" : "";
5567 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
5568 (void) printf(" %s\n", fi
->fi_desc
);
5570 (void) printf("\n");
5572 (void) printf(gettext("The following legacy versions are also "
5574 (void) printf(gettext("VER DESCRIPTION\n"));
5575 (void) printf("--- -----------------------------------------"
5576 "---------------\n");
5577 (void) printf(gettext(" 1 Initial ZFS version\n"));
5578 (void) printf(gettext(" 2 Ditto blocks "
5579 "(replicated metadata)\n"));
5580 (void) printf(gettext(" 3 Hot spares and double parity "
5582 (void) printf(gettext(" 4 zpool history\n"));
5583 (void) printf(gettext(" 5 Compression using the gzip "
5585 (void) printf(gettext(" 6 bootfs pool property\n"));
5586 (void) printf(gettext(" 7 Separate intent log devices\n"));
5587 (void) printf(gettext(" 8 Delegated administration\n"));
5588 (void) printf(gettext(" 9 refquota and refreservation "
5590 (void) printf(gettext(" 10 Cache devices\n"));
5591 (void) printf(gettext(" 11 Improved scrub performance\n"));
5592 (void) printf(gettext(" 12 Snapshot properties\n"));
5593 (void) printf(gettext(" 13 snapused property\n"));
5594 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5595 (void) printf(gettext(" 15 user/group space accounting\n"));
5596 (void) printf(gettext(" 16 stmf property support\n"));
5597 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5598 (void) printf(gettext(" 18 Snapshot user holds\n"));
5599 (void) printf(gettext(" 19 Log device removal\n"));
5600 (void) printf(gettext(" 20 Compression using zle "
5601 "(zero-length encoding)\n"));
5602 (void) printf(gettext(" 21 Deduplication\n"));
5603 (void) printf(gettext(" 22 Received properties\n"));
5604 (void) printf(gettext(" 23 Slim ZIL\n"));
5605 (void) printf(gettext(" 24 System attributes\n"));
5606 (void) printf(gettext(" 25 Improved scrub stats\n"));
5607 (void) printf(gettext(" 26 Improved snapshot deletion "
5609 (void) printf(gettext(" 27 Improved snapshot creation "
5611 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5612 (void) printf(gettext("\nFor more information on a particular "
5613 "version, including supported releases,\n"));
5614 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5615 } else if (argc
== 0 && upgradeall
) {
5616 cb
.cb_first
= B_TRUE
;
5617 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
5618 if (ret
== 0 && cb
.cb_first
) {
5619 if (cb
.cb_version
== SPA_VERSION
) {
5620 (void) printf(gettext("All pools are already "
5621 "formatted using feature flags.\n\n"));
5622 (void) printf(gettext("Every feature flags "
5623 "pool already has all supported features "
5626 (void) printf(gettext("All pools are already "
5627 "formatted with version %llu or higher.\n"),
5631 } else if (argc
== 0) {
5632 cb
.cb_first
= B_TRUE
;
5633 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
5637 (void) printf(gettext("All pools are formatted "
5638 "using feature flags.\n\n"));
5640 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5641 "for a list of available legacy versions.\n"));
5644 cb
.cb_first
= B_TRUE
;
5645 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
5649 (void) printf(gettext("Every feature flags pool has "
5650 "all supported features enabled.\n"));
5652 (void) printf(gettext("\n"));
5655 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
5662 typedef struct hist_cbdata
{
5669 * Print out the command history for a specific pool.
5672 get_history_one(zpool_handle_t
*zhp
, void *data
)
5678 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
5680 cb
->first
= B_FALSE
;
5682 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
5684 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
5687 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
5688 &records
, &numrecords
) == 0);
5689 for (i
= 0; i
< numrecords
; i
++) {
5690 nvlist_t
*rec
= records
[i
];
5693 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
5697 tsec
= fnvlist_lookup_uint64(records
[i
],
5699 (void) localtime_r(&tsec
, &t
);
5700 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
5703 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
5704 (void) printf("%s %s", tbuf
,
5705 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
5706 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
5708 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
5711 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
5712 (void) printf("%s unrecognized record:\n",
5714 dump_nvlist(rec
, 4);
5717 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
5718 zfs_history_event_names
[ievent
],
5719 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
5720 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
5721 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
5724 (void) printf("%s [txg:%lld] %s", tbuf
,
5725 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_TXG
),
5726 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
5727 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
5728 (void) printf(" %s (%llu)",
5729 fnvlist_lookup_string(rec
,
5731 fnvlist_lookup_uint64(rec
,
5734 (void) printf(" %s", fnvlist_lookup_string(rec
,
5735 ZPOOL_HIST_INT_STR
));
5736 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
5739 (void) printf("%s ioctl %s\n", tbuf
,
5740 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
5741 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
5742 (void) printf(" input:\n");
5743 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5744 ZPOOL_HIST_INPUT_NVL
), 8);
5746 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
5747 (void) printf(" output:\n");
5748 dump_nvlist(fnvlist_lookup_nvlist(rec
,
5749 ZPOOL_HIST_OUTPUT_NVL
), 8);
5751 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
5752 (void) printf(" errno: %lld\n",
5753 fnvlist_lookup_int64(rec
,
5759 (void) printf("%s unrecognized record:\n", tbuf
);
5760 dump_nvlist(rec
, 4);
5764 (void) printf("\n");
5767 (void) printf(" [");
5768 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
5769 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
5770 struct passwd
*pwd
= getpwuid(who
);
5771 (void) printf("user %d ", (int)who
);
5773 (void) printf("(%s) ", pwd
->pw_name
);
5775 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
5776 (void) printf("on %s",
5777 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
5779 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
5780 (void) printf(":%s",
5781 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
5784 (void) printf("\n");
5786 (void) printf("\n");
5793 * zpool history <pool>
5795 * Displays the history of commands that modified pools.
5798 zpool_do_history(int argc
, char **argv
)
5800 hist_cbdata_t cbdata
= { 0 };
5804 cbdata
.first
= B_TRUE
;
5806 while ((c
= getopt(argc
, argv
, "li")) != -1) {
5809 cbdata
.longfmt
= B_TRUE
;
5812 cbdata
.internal
= B_TRUE
;
5815 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5823 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
5826 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
5827 (void) printf(gettext("no pools available\n"));
5835 get_callback(zpool_handle_t
*zhp
, void *data
)
5837 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
5838 char value
[MAXNAMELEN
];
5839 zprop_source_t srctype
;
5842 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
5845 * Skip the special fake placeholder. This will also skip
5846 * over the name property when 'all' is specified.
5848 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
5849 pl
== cbp
->cb_proplist
)
5852 if (pl
->pl_prop
== ZPROP_INVAL
&&
5853 (zpool_prop_feature(pl
->pl_user_prop
) ||
5854 zpool_prop_unsupported(pl
->pl_user_prop
))) {
5855 srctype
= ZPROP_SRC_LOCAL
;
5857 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
5858 value
, sizeof (value
)) == 0) {
5859 zprop_print_one_property(zpool_get_name(zhp
),
5860 cbp
, pl
->pl_user_prop
, value
, srctype
,
5864 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
5865 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
5868 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
5869 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
5877 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5879 * -H Scripted mode. Don't display headers, and separate properties
5881 * -o List of columns to display. Defaults to
5882 * "name,property,value,source".
5883 * -p Diplay values in parsable (exact) format.
5885 * Get properties of pools in the system. Output space statistics
5886 * for each one as well as other attributes.
5889 zpool_do_get(int argc
, char **argv
)
5891 zprop_get_cbdata_t cb
= { 0 };
5892 zprop_list_t fake_name
= { 0 };
5897 cb
.cb_first
= B_TRUE
;
5900 * Set up default columns and sources.
5902 cb
.cb_sources
= ZPROP_SRC_ALL
;
5903 cb
.cb_columns
[0] = GET_COL_NAME
;
5904 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5905 cb
.cb_columns
[2] = GET_COL_VALUE
;
5906 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5907 cb
.cb_type
= ZFS_TYPE_POOL
;
5910 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
5913 cb
.cb_literal
= B_TRUE
;
5916 cb
.cb_scripted
= B_TRUE
;
5919 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
5921 while (*optarg
!= '\0') {
5922 static char *col_subopts
[] =
5923 { "name", "property", "value", "source",
5926 if (i
== ZFS_GET_NCOLS
) {
5927 (void) fprintf(stderr
, gettext("too "
5928 "many fields given to -o "
5933 switch (getsubopt(&optarg
, col_subopts
,
5936 cb
.cb_columns
[i
++] = GET_COL_NAME
;
5939 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
5942 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
5945 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
5949 (void) fprintf(stderr
,
5950 gettext("\"all\" conflicts "
5951 "with specific fields "
5952 "given to -o option\n"));
5955 cb
.cb_columns
[0] = GET_COL_NAME
;
5956 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
5957 cb
.cb_columns
[2] = GET_COL_VALUE
;
5958 cb
.cb_columns
[3] = GET_COL_SOURCE
;
5962 (void) fprintf(stderr
,
5963 gettext("invalid column name "
5970 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5980 (void) fprintf(stderr
, gettext("missing property "
5985 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
5986 ZFS_TYPE_POOL
) != 0)
5992 if (cb
.cb_proplist
!= NULL
) {
5993 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
5994 fake_name
.pl_width
= strlen(gettext("NAME"));
5995 fake_name
.pl_next
= cb
.cb_proplist
;
5996 cb
.cb_proplist
= &fake_name
;
5999 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
6002 if (cb
.cb_proplist
== &fake_name
)
6003 zprop_free_list(fake_name
.pl_next
);
6005 zprop_free_list(cb
.cb_proplist
);
6010 typedef struct set_cbdata
{
6013 boolean_t cb_any_successful
;
6017 set_callback(zpool_handle_t
*zhp
, void *data
)
6020 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
6022 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
6025 cb
->cb_any_successful
= B_TRUE
;
6031 zpool_do_set(int argc
, char **argv
)
6033 set_cbdata_t cb
= { 0 };
6036 if (argc
> 1 && argv
[1][0] == '-') {
6037 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6043 (void) fprintf(stderr
, gettext("missing property=value "
6049 (void) fprintf(stderr
, gettext("missing pool name\n"));
6054 (void) fprintf(stderr
, gettext("too many pool names\n"));
6058 cb
.cb_propname
= argv
[1];
6059 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
6060 if (cb
.cb_value
== NULL
) {
6061 (void) fprintf(stderr
, gettext("missing value in "
6062 "property=value argument\n"));
6066 *(cb
.cb_value
) = '\0';
6069 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
6076 find_command_idx(char *command
, int *idx
)
6080 for (i
= 0; i
< NCOMMAND
; i
++) {
6081 if (command_table
[i
].name
== NULL
)
6084 if (strcmp(command
, command_table
[i
].name
) == 0) {
6093 main(int argc
, char **argv
)
6099 (void) setlocale(LC_ALL
, "");
6100 (void) textdomain(TEXT_DOMAIN
);
6102 if ((g_zfs
= libzfs_init()) == NULL
) {
6103 (void) fprintf(stderr
, gettext("internal error: failed to "
6104 "initialize ZFS library\n"));
6108 libzfs_print_on_error(g_zfs
, B_TRUE
);
6113 * Make sure the user has specified some command.
6116 (void) fprintf(stderr
, gettext("missing command\n"));
6125 if (strcmp(cmdname
, "-?") == 0)
6128 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
6131 * Run the appropriate command.
6133 if (find_command_idx(cmdname
, &i
) == 0) {
6134 current_command
= &command_table
[i
];
6135 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
6136 } else if (strchr(cmdname
, '=')) {
6137 verify(find_command_idx("set", &i
) == 0);
6138 current_command
= &command_table
[i
];
6139 ret
= command_table
[i
].func(argc
, argv
);
6140 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
6142 * 'freeze' is a vile debugging abomination, so we treat
6146 int fd
= open(ZFS_DEV
, O_RDWR
);
6147 (void) strcpy((void *)buf
, argv
[2]);
6148 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
6150 (void) fprintf(stderr
, gettext("unrecognized "
6151 "command '%s'\n"), cmdname
);
6155 if (ret
== 0 && log_history
)
6156 (void) zpool_log_history(g_zfs
, history_str
);
6161 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6162 * for the purposes of running ::findleaks.
6164 if (getenv("ZFS_ABORT") != NULL
) {
6165 (void) printf("dumping core by request\n");