8567 Inconsistent return value in zpool_read_label
[unleashed.git] / usr / src / cmd / zpool / zpool_main.c
blobd3af687dc4f1b88a4b3c1214d8657ad94ff99130
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2016 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.
32 #include <assert.h>
33 #include <ctype.h>
34 #include <dirent.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <libgen.h>
38 #include <libintl.h>
39 #include <libuutil.h>
40 #include <locale.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <strings.h>
45 #include <unistd.h>
46 #include <priv.h>
47 #include <pwd.h>
48 #include <zone.h>
49 #include <zfs_prop.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/stat.h>
53 #include <libzfs.h>
55 #include "zpool_util.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 #include "statcommon.h"
61 static int zpool_do_create(int, char **);
62 static int zpool_do_destroy(int, char **);
64 static int zpool_do_add(int, char **);
65 static int zpool_do_remove(int, char **);
66 static int zpool_do_labelclear(int, char **);
68 static int zpool_do_list(int, char **);
69 static int zpool_do_iostat(int, char **);
70 static int zpool_do_status(int, char **);
72 static int zpool_do_online(int, char **);
73 static int zpool_do_offline(int, char **);
74 static int zpool_do_clear(int, char **);
75 static int zpool_do_reopen(int, char **);
77 static int zpool_do_reguid(int, char **);
79 static int zpool_do_attach(int, char **);
80 static int zpool_do_detach(int, char **);
81 static int zpool_do_replace(int, char **);
82 static int zpool_do_split(int, char **);
84 static int zpool_do_scrub(int, char **);
86 static int zpool_do_import(int, char **);
87 static int zpool_do_export(int, char **);
89 static int zpool_do_upgrade(int, char **);
91 static int zpool_do_history(int, char **);
93 static int zpool_do_get(int, char **);
94 static int zpool_do_set(int, char **);
97 * These libumem hooks provide a reasonable set of defaults for the allocator's
98 * debugging facilities.
101 #ifdef DEBUG
102 const char *
103 _umem_debug_init(void)
105 return ("default,verbose"); /* $UMEM_DEBUG setting */
108 const char *
109 _umem_logging_init(void)
111 return ("fail,contents"); /* $UMEM_LOGGING setting */
113 #endif
115 typedef enum {
116 HELP_ADD,
117 HELP_ATTACH,
118 HELP_CLEAR,
119 HELP_CREATE,
120 HELP_DESTROY,
121 HELP_DETACH,
122 HELP_EXPORT,
123 HELP_HISTORY,
124 HELP_IMPORT,
125 HELP_IOSTAT,
126 HELP_LABELCLEAR,
127 HELP_LIST,
128 HELP_OFFLINE,
129 HELP_ONLINE,
130 HELP_REPLACE,
131 HELP_REMOVE,
132 HELP_SCRUB,
133 HELP_STATUS,
134 HELP_UPGRADE,
135 HELP_GET,
136 HELP_SET,
137 HELP_SPLIT,
138 HELP_REGUID,
139 HELP_REOPEN
140 } zpool_help_t;
143 typedef struct zpool_command {
144 const char *name;
145 int (*func)(int, char **);
146 zpool_help_t usage;
147 } zpool_command_t;
150 * Master command table. Each ZFS command has a name, associated function, and
151 * usage message. The usage messages need to be internationalized, so we have
152 * to have a function to return the usage message based on a command index.
154 * These commands are organized according to how they are displayed in the usage
155 * message. An empty command (one with a NULL name) indicates an empty line in
156 * the generic usage message.
158 static zpool_command_t command_table[] = {
159 { "create", zpool_do_create, HELP_CREATE },
160 { "destroy", zpool_do_destroy, HELP_DESTROY },
161 { NULL },
162 { "add", zpool_do_add, HELP_ADD },
163 { "remove", zpool_do_remove, HELP_REMOVE },
164 { NULL },
165 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
166 { NULL },
167 { "list", zpool_do_list, HELP_LIST },
168 { "iostat", zpool_do_iostat, HELP_IOSTAT },
169 { "status", zpool_do_status, HELP_STATUS },
170 { NULL },
171 { "online", zpool_do_online, HELP_ONLINE },
172 { "offline", zpool_do_offline, HELP_OFFLINE },
173 { "clear", zpool_do_clear, HELP_CLEAR },
174 { "reopen", zpool_do_reopen, HELP_REOPEN },
175 { NULL },
176 { "attach", zpool_do_attach, HELP_ATTACH },
177 { "detach", zpool_do_detach, HELP_DETACH },
178 { "replace", zpool_do_replace, HELP_REPLACE },
179 { "split", zpool_do_split, HELP_SPLIT },
180 { NULL },
181 { "scrub", zpool_do_scrub, HELP_SCRUB },
182 { NULL },
183 { "import", zpool_do_import, HELP_IMPORT },
184 { "export", zpool_do_export, HELP_EXPORT },
185 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
186 { "reguid", zpool_do_reguid, HELP_REGUID },
187 { NULL },
188 { "history", zpool_do_history, HELP_HISTORY },
189 { "get", zpool_do_get, HELP_GET },
190 { "set", zpool_do_set, HELP_SET },
193 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
195 static zpool_command_t *current_command;
196 static char history_str[HIS_MAX_RECORD_LEN];
197 static boolean_t log_history = B_TRUE;
198 static uint_t timestamp_fmt = NODATE;
200 static const char *
201 get_usage(zpool_help_t idx)
203 switch (idx) {
204 case HELP_ADD:
205 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
206 case HELP_ATTACH:
207 return (gettext("\tattach [-f] <pool> <device> "
208 "<new-device>\n"));
209 case HELP_CLEAR:
210 return (gettext("\tclear [-nF] <pool> [device]\n"));
211 case HELP_CREATE:
212 return (gettext("\tcreate [-fnd] [-B] "
213 "[-o property=value] ... \n"
214 "\t [-O file-system-property=value] ... \n"
215 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
216 case HELP_DESTROY:
217 return (gettext("\tdestroy [-f] <pool>\n"));
218 case HELP_DETACH:
219 return (gettext("\tdetach <pool> <device>\n"));
220 case HELP_EXPORT:
221 return (gettext("\texport [-f] <pool> ...\n"));
222 case HELP_HISTORY:
223 return (gettext("\thistory [-il] [<pool>] ...\n"));
224 case HELP_IMPORT:
225 return (gettext("\timport [-d dir] [-D]\n"
226 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
227 "\timport [-o mntopts] [-o property=value] ... \n"
228 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
229 "[-R root] [-F [-n]] -a\n"
230 "\timport [-o mntopts] [-o property=value] ... \n"
231 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
232 "[-R root] [-F [-n]]\n"
233 "\t <pool | id> [newpool]\n"));
234 case HELP_IOSTAT:
235 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
236 "[count]]\n"));
237 case HELP_LABELCLEAR:
238 return (gettext("\tlabelclear [-f] <vdev>\n"));
239 case HELP_LIST:
240 return (gettext("\tlist [-Hp] [-o property[,...]] "
241 "[-T d|u] [pool] ... [interval [count]]\n"));
242 case HELP_OFFLINE:
243 return (gettext("\toffline [-t] <pool> <device> ...\n"));
244 case HELP_ONLINE:
245 return (gettext("\tonline <pool> <device> ...\n"));
246 case HELP_REPLACE:
247 return (gettext("\treplace [-f] <pool> <device> "
248 "[new-device]\n"));
249 case HELP_REMOVE:
250 return (gettext("\tremove <pool> <device> ...\n"));
251 case HELP_REOPEN:
252 return (gettext("\treopen <pool>\n"));
253 case HELP_SCRUB:
254 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
255 case HELP_STATUS:
256 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
257 "[count]]\n"));
258 case HELP_UPGRADE:
259 return (gettext("\tupgrade\n"
260 "\tupgrade -v\n"
261 "\tupgrade [-V version] <-a | pool ...>\n"));
262 case HELP_GET:
263 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
264 "<\"all\" | property[,...]> <pool> ...\n"));
265 case HELP_SET:
266 return (gettext("\tset <property=value> <pool> \n"));
267 case HELP_SPLIT:
268 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
269 "\t [-o property=value] <pool> <newpool> "
270 "[<device> ...]\n"));
271 case HELP_REGUID:
272 return (gettext("\treguid <pool>\n"));
275 abort();
276 /* NOTREACHED */
281 * Callback routine that will print out a pool property value.
283 static int
284 print_prop_cb(int prop, void *cb)
286 FILE *fp = cb;
288 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
290 if (zpool_prop_readonly(prop))
291 (void) fprintf(fp, " NO ");
292 else
293 (void) fprintf(fp, " YES ");
295 if (zpool_prop_values(prop) == NULL)
296 (void) fprintf(fp, "-\n");
297 else
298 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
300 return (ZPROP_CONT);
304 * Display usage message. If we're inside a command, display only the usage for
305 * that command. Otherwise, iterate over the entire command table and display
306 * a complete usage message.
308 void
309 usage(boolean_t requested)
311 FILE *fp = requested ? stdout : stderr;
313 if (current_command == NULL) {
314 int i;
316 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
317 (void) fprintf(fp,
318 gettext("where 'command' is one of the following:\n\n"));
320 for (i = 0; i < NCOMMAND; i++) {
321 if (command_table[i].name == NULL)
322 (void) fprintf(fp, "\n");
323 else
324 (void) fprintf(fp, "%s",
325 get_usage(command_table[i].usage));
327 } else {
328 (void) fprintf(fp, gettext("usage:\n"));
329 (void) fprintf(fp, "%s", get_usage(current_command->usage));
332 if (current_command != NULL &&
333 ((strcmp(current_command->name, "set") == 0) ||
334 (strcmp(current_command->name, "get") == 0) ||
335 (strcmp(current_command->name, "list") == 0))) {
337 (void) fprintf(fp,
338 gettext("\nthe following properties are supported:\n"));
340 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
341 "PROPERTY", "EDIT", "VALUES");
343 /* Iterate over all properties */
344 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
345 ZFS_TYPE_POOL);
347 (void) fprintf(fp, "\t%-15s ", "feature@...");
348 (void) fprintf(fp, "YES disabled | enabled | active\n");
350 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
351 "appended with a feature name.\nSee zpool-features(5).\n"));
355 * See comments at end of main().
357 if (getenv("ZFS_ABORT") != NULL) {
358 (void) printf("dumping core by request\n");
359 abort();
362 exit(requested ? 0 : 2);
365 void
366 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
367 boolean_t print_logs)
369 nvlist_t **child;
370 uint_t c, children;
371 char *vname;
373 if (name != NULL)
374 (void) printf("\t%*s%s\n", indent, "", name);
376 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
377 &child, &children) != 0)
378 return;
380 for (c = 0; c < children; c++) {
381 uint64_t is_log = B_FALSE;
383 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
384 &is_log);
385 if ((is_log && !print_logs) || (!is_log && print_logs))
386 continue;
388 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
389 print_vdev_tree(zhp, vname, child[c], indent + 2,
390 B_FALSE);
391 free(vname);
395 static boolean_t
396 prop_list_contains_feature(nvlist_t *proplist)
398 nvpair_t *nvp;
399 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
400 nvp = nvlist_next_nvpair(proplist, nvp)) {
401 if (zpool_prop_feature(nvpair_name(nvp)))
402 return (B_TRUE);
404 return (B_FALSE);
408 * Add a property pair (name, string-value) into a property nvlist.
410 static int
411 add_prop_list(const char *propname, char *propval, nvlist_t **props,
412 boolean_t poolprop)
414 zpool_prop_t prop = ZPROP_INVAL;
415 zfs_prop_t fprop;
416 nvlist_t *proplist;
417 const char *normnm;
418 char *strval;
420 if (*props == NULL &&
421 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
422 (void) fprintf(stderr,
423 gettext("internal error: out of memory\n"));
424 return (1);
427 proplist = *props;
429 if (poolprop) {
430 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
432 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
433 !zpool_prop_feature(propname)) {
434 (void) fprintf(stderr, gettext("property '%s' is "
435 "not a valid pool property\n"), propname);
436 return (2);
440 * feature@ properties and version should not be specified
441 * at the same time.
443 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
444 nvlist_exists(proplist, vname)) ||
445 (prop == ZPOOL_PROP_VERSION &&
446 prop_list_contains_feature(proplist))) {
447 (void) fprintf(stderr, gettext("'feature@' and "
448 "'version' properties cannot be specified "
449 "together\n"));
450 return (2);
454 if (zpool_prop_feature(propname))
455 normnm = propname;
456 else
457 normnm = zpool_prop_to_name(prop);
458 } else {
459 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
460 normnm = zfs_prop_to_name(fprop);
461 } else {
462 normnm = propname;
466 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
467 prop != ZPOOL_PROP_CACHEFILE) {
468 (void) fprintf(stderr, gettext("property '%s' "
469 "specified multiple times\n"), propname);
470 return (2);
473 if (nvlist_add_string(proplist, normnm, propval) != 0) {
474 (void) fprintf(stderr, gettext("internal "
475 "error: out of memory\n"));
476 return (1);
479 return (0);
483 * zpool add [-fn] <pool> <vdev> ...
485 * -f Force addition of devices, even if they appear in use
486 * -n Do not add the devices, but display the resulting layout if
487 * they were to be added.
489 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
490 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
491 * libzfs.
494 zpool_do_add(int argc, char **argv)
496 boolean_t force = B_FALSE;
497 boolean_t dryrun = B_FALSE;
498 int c;
499 nvlist_t *nvroot;
500 char *poolname;
501 zpool_boot_label_t boot_type;
502 uint64_t boot_size;
503 int ret;
504 zpool_handle_t *zhp;
505 nvlist_t *config;
507 /* check options */
508 while ((c = getopt(argc, argv, "fn")) != -1) {
509 switch (c) {
510 case 'f':
511 force = B_TRUE;
512 break;
513 case 'n':
514 dryrun = B_TRUE;
515 break;
516 case '?':
517 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
518 optopt);
519 usage(B_FALSE);
523 argc -= optind;
524 argv += optind;
526 /* get pool name and check number of arguments */
527 if (argc < 1) {
528 (void) fprintf(stderr, gettext("missing pool name argument\n"));
529 usage(B_FALSE);
531 if (argc < 2) {
532 (void) fprintf(stderr, gettext("missing vdev specification\n"));
533 usage(B_FALSE);
536 poolname = argv[0];
538 argc--;
539 argv++;
541 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
542 return (1);
544 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
545 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
546 poolname);
547 zpool_close(zhp);
548 return (1);
551 if (zpool_is_bootable(zhp))
552 boot_type = ZPOOL_COPY_BOOT_LABEL;
553 else
554 boot_type = ZPOOL_NO_BOOT_LABEL;
556 /* pass off to get_vdev_spec for processing */
557 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
558 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
559 boot_type, boot_size, argc, argv);
560 if (nvroot == NULL) {
561 zpool_close(zhp);
562 return (1);
565 if (dryrun) {
566 nvlist_t *poolnvroot;
568 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
569 &poolnvroot) == 0);
571 (void) printf(gettext("would update '%s' to the following "
572 "configuration:\n"), zpool_get_name(zhp));
574 /* print original main pool and new tree */
575 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
576 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
578 /* Do the same for the logs */
579 if (num_logs(poolnvroot) > 0) {
580 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
581 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
582 } else if (num_logs(nvroot) > 0) {
583 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
586 ret = 0;
587 } else {
588 ret = (zpool_add(zhp, nvroot) != 0);
591 nvlist_free(nvroot);
592 zpool_close(zhp);
594 return (ret);
598 * zpool remove <pool> <vdev> ...
600 * Removes the given vdev from the pool. Currently, this supports removing
601 * spares, cache, and log devices from the pool.
604 zpool_do_remove(int argc, char **argv)
606 char *poolname;
607 int i, ret = 0;
608 zpool_handle_t *zhp;
610 argc--;
611 argv++;
613 /* get pool name and check number of arguments */
614 if (argc < 1) {
615 (void) fprintf(stderr, gettext("missing pool name argument\n"));
616 usage(B_FALSE);
618 if (argc < 2) {
619 (void) fprintf(stderr, gettext("missing device\n"));
620 usage(B_FALSE);
623 poolname = argv[0];
625 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
626 return (1);
628 for (i = 1; i < argc; i++) {
629 if (zpool_vdev_remove(zhp, argv[i]) != 0)
630 ret = 1;
633 return (ret);
637 * zpool labelclear [-f] <vdev>
639 * -f Force clearing the label for the vdevs which are members of
640 * the exported or foreign pools.
642 * Verifies that the vdev is not active and zeros out the label information
643 * on the device.
646 zpool_do_labelclear(int argc, char **argv)
648 char vdev[MAXPATHLEN];
649 char *name = NULL;
650 struct stat st;
651 int c, fd, ret = 0;
652 nvlist_t *config;
653 pool_state_t state;
654 boolean_t inuse = B_FALSE;
655 boolean_t force = B_FALSE;
657 /* check options */
658 while ((c = getopt(argc, argv, "f")) != -1) {
659 switch (c) {
660 case 'f':
661 force = B_TRUE;
662 break;
663 default:
664 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
665 optopt);
666 usage(B_FALSE);
670 argc -= optind;
671 argv += optind;
673 /* get vdev name */
674 if (argc < 1) {
675 (void) fprintf(stderr, gettext("missing vdev name\n"));
676 usage(B_FALSE);
678 if (argc > 1) {
679 (void) fprintf(stderr, gettext("too many arguments\n"));
680 usage(B_FALSE);
684 * Check if we were given absolute path and use it as is.
685 * Otherwise if the provided vdev name doesn't point to a file,
686 * try prepending dsk path and appending s0.
688 (void) strlcpy(vdev, argv[0], sizeof (vdev));
689 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
690 char *s;
692 (void) snprintf(vdev, sizeof (vdev), "%s/%s",
693 ZFS_DISK_ROOT, argv[0]);
694 if ((s = strrchr(argv[0], 's')) == NULL ||
695 !isdigit(*(s + 1)))
696 (void) strlcat(vdev, "s0", sizeof (vdev));
697 if (stat(vdev, &st) != 0) {
698 (void) fprintf(stderr, gettext(
699 "failed to find device %s, try specifying absolute "
700 "path instead\n"), argv[0]);
701 return (1);
705 if ((fd = open(vdev, O_RDWR)) < 0) {
706 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
707 vdev, strerror(errno));
708 return (1);
711 if (zpool_read_label(fd, &config) != 0) {
712 (void) fprintf(stderr,
713 gettext("failed to read label from %s\n"), vdev);
714 return (1);
716 nvlist_free(config);
718 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
719 if (ret != 0) {
720 (void) fprintf(stderr,
721 gettext("failed to check state for %s\n"), vdev);
722 return (1);
725 if (!inuse)
726 goto wipe_label;
728 switch (state) {
729 default:
730 case POOL_STATE_ACTIVE:
731 case POOL_STATE_SPARE:
732 case POOL_STATE_L2CACHE:
733 (void) fprintf(stderr, gettext(
734 "%s is a member (%s) of pool \"%s\"\n"),
735 vdev, zpool_pool_state_to_name(state), name);
736 ret = 1;
737 goto errout;
739 case POOL_STATE_EXPORTED:
740 if (force)
741 break;
742 (void) fprintf(stderr, gettext(
743 "use '-f' to override the following error:\n"
744 "%s is a member of exported pool \"%s\"\n"),
745 vdev, name);
746 ret = 1;
747 goto errout;
749 case POOL_STATE_POTENTIALLY_ACTIVE:
750 if (force)
751 break;
752 (void) fprintf(stderr, gettext(
753 "use '-f' to override the following error:\n"
754 "%s is a member of potentially active pool \"%s\"\n"),
755 vdev, name);
756 ret = 1;
757 goto errout;
759 case POOL_STATE_DESTROYED:
760 /* inuse should never be set for a destroyed pool */
761 assert(0);
762 break;
765 wipe_label:
766 ret = zpool_clear_label(fd);
767 if (ret != 0) {
768 (void) fprintf(stderr,
769 gettext("failed to clear label for %s\n"), vdev);
772 errout:
773 free(name);
774 (void) close(fd);
776 return (ret);
780 * zpool create [-fnd] [-B] [-o property=value] ...
781 * [-O file-system-property=value] ...
782 * [-R root] [-m mountpoint] <pool> <dev> ...
784 * -B Create boot partition.
785 * -f Force creation, even if devices appear in use
786 * -n Do not create the pool, but display the resulting layout if it
787 * were to be created.
788 * -R Create a pool under an alternate root
789 * -m Set default mountpoint for the root dataset. By default it's
790 * '/<pool>'
791 * -o Set property=value.
792 * -d Don't automatically enable all supported pool features
793 * (individual features can be enabled with -o).
794 * -O Set fsproperty=value in the pool's root file system
796 * Creates the named pool according to the given vdev specification. The
797 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
798 * we get the nvlist back from get_vdev_spec(), we either print out the contents
799 * (if '-n' was specified), or pass it to libzfs to do the creation.
802 #define SYSTEM256 (256 * 1024 * 1024)
804 zpool_do_create(int argc, char **argv)
806 boolean_t force = B_FALSE;
807 boolean_t dryrun = B_FALSE;
808 boolean_t enable_all_pool_feat = B_TRUE;
809 zpool_boot_label_t boot_type = ZPOOL_NO_BOOT_LABEL;
810 uint64_t boot_size = 0;
811 int c;
812 nvlist_t *nvroot = NULL;
813 char *poolname;
814 int ret = 1;
815 char *altroot = NULL;
816 char *mountpoint = NULL;
817 nvlist_t *fsprops = NULL;
818 nvlist_t *props = NULL;
819 char *propval;
821 /* check options */
822 while ((c = getopt(argc, argv, ":fndBR:m:o:O:")) != -1) {
823 switch (c) {
824 case 'f':
825 force = B_TRUE;
826 break;
827 case 'n':
828 dryrun = B_TRUE;
829 break;
830 case 'd':
831 enable_all_pool_feat = B_FALSE;
832 break;
833 case 'B':
835 * We should create the system partition.
836 * Also make sure the size is set.
838 boot_type = ZPOOL_CREATE_BOOT_LABEL;
839 if (boot_size == 0)
840 boot_size = SYSTEM256;
841 break;
842 case 'R':
843 altroot = optarg;
844 if (add_prop_list(zpool_prop_to_name(
845 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
846 goto errout;
847 if (nvlist_lookup_string(props,
848 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
849 &propval) == 0)
850 break;
851 if (add_prop_list(zpool_prop_to_name(
852 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
853 goto errout;
854 break;
855 case 'm':
856 /* Equivalent to -O mountpoint=optarg */
857 mountpoint = optarg;
858 break;
859 case 'o':
860 if ((propval = strchr(optarg, '=')) == NULL) {
861 (void) fprintf(stderr, gettext("missing "
862 "'=' for -o option\n"));
863 goto errout;
865 *propval = '\0';
866 propval++;
868 if (add_prop_list(optarg, propval, &props, B_TRUE))
869 goto errout;
872 * Get bootsize value for make_root_vdev().
874 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_BOOTSIZE) {
875 if (zfs_nicestrtonum(g_zfs, propval,
876 &boot_size) < 0 || boot_size == 0) {
877 (void) fprintf(stderr,
878 gettext("bad boot partition size "
879 "'%s': %s\n"), propval,
880 libzfs_error_description(g_zfs));
881 goto errout;
886 * If the user is creating a pool that doesn't support
887 * feature flags, don't enable any features.
889 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
890 char *end;
891 u_longlong_t ver;
893 ver = strtoull(propval, &end, 10);
894 if (*end == '\0' &&
895 ver < SPA_VERSION_FEATURES) {
896 enable_all_pool_feat = B_FALSE;
899 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
900 altroot = propval;
901 break;
902 case 'O':
903 if ((propval = strchr(optarg, '=')) == NULL) {
904 (void) fprintf(stderr, gettext("missing "
905 "'=' for -O option\n"));
906 goto errout;
908 *propval = '\0';
909 propval++;
912 * Mountpoints are checked and then added later.
913 * Uniquely among properties, they can be specified
914 * more than once, to avoid conflict with -m.
916 if (0 == strcmp(optarg,
917 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
918 mountpoint = propval;
919 } else if (add_prop_list(optarg, propval, &fsprops,
920 B_FALSE)) {
921 goto errout;
923 break;
924 case ':':
925 (void) fprintf(stderr, gettext("missing argument for "
926 "'%c' option\n"), optopt);
927 goto badusage;
928 case '?':
929 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
930 optopt);
931 goto badusage;
935 argc -= optind;
936 argv += optind;
938 /* get pool name and check number of arguments */
939 if (argc < 1) {
940 (void) fprintf(stderr, gettext("missing pool name argument\n"));
941 goto badusage;
943 if (argc < 2) {
944 (void) fprintf(stderr, gettext("missing vdev specification\n"));
945 goto badusage;
948 poolname = argv[0];
951 * As a special case, check for use of '/' in the name, and direct the
952 * user to use 'zfs create' instead.
954 if (strchr(poolname, '/') != NULL) {
955 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
956 "character '/' in pool name\n"), poolname);
957 (void) fprintf(stderr, gettext("use 'zfs create' to "
958 "create a dataset\n"));
959 goto errout;
963 * Make sure the bootsize is set when ZPOOL_CREATE_BOOT_LABEL is used,
964 * and not set otherwise.
966 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
967 const char *propname;
968 char *strptr, *buf = NULL;
969 int rv;
971 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
972 if (nvlist_lookup_string(props, propname, &strptr) != 0) {
973 (void) asprintf(&buf, "%" PRIu64, boot_size);
974 if (buf == NULL) {
975 (void) fprintf(stderr,
976 gettext("internal error: out of memory\n"));
977 goto errout;
979 rv = add_prop_list(propname, buf, &props, B_TRUE);
980 free(buf);
981 if (rv != 0)
982 goto errout;
984 } else {
985 const char *propname;
986 char *strptr;
988 propname = zpool_prop_to_name(ZPOOL_PROP_BOOTSIZE);
989 if (nvlist_lookup_string(props, propname, &strptr) == 0) {
990 (void) fprintf(stderr, gettext("error: setting boot "
991 "partition size requires option '-B'\n"));
992 goto errout;
996 /* pass off to get_vdev_spec for bulk processing */
997 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
998 boot_type, boot_size, argc - 1, argv + 1);
999 if (nvroot == NULL)
1000 goto errout;
1002 /* make_root_vdev() allows 0 toplevel children if there are spares */
1003 if (!zfs_allocatable_devs(nvroot)) {
1004 (void) fprintf(stderr, gettext("invalid vdev "
1005 "specification: at least one toplevel vdev must be "
1006 "specified\n"));
1007 goto errout;
1010 if (altroot != NULL && altroot[0] != '/') {
1011 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1012 "must be an absolute path\n"), altroot);
1013 goto errout;
1017 * Check the validity of the mountpoint and direct the user to use the
1018 * '-m' mountpoint option if it looks like its in use.
1020 if (mountpoint == NULL ||
1021 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1022 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1023 char buf[MAXPATHLEN];
1024 DIR *dirp;
1026 if (mountpoint && mountpoint[0] != '/') {
1027 (void) fprintf(stderr, gettext("invalid mountpoint "
1028 "'%s': must be an absolute path, 'legacy', or "
1029 "'none'\n"), mountpoint);
1030 goto errout;
1033 if (mountpoint == NULL) {
1034 if (altroot != NULL)
1035 (void) snprintf(buf, sizeof (buf), "%s/%s",
1036 altroot, poolname);
1037 else
1038 (void) snprintf(buf, sizeof (buf), "/%s",
1039 poolname);
1040 } else {
1041 if (altroot != NULL)
1042 (void) snprintf(buf, sizeof (buf), "%s%s",
1043 altroot, mountpoint);
1044 else
1045 (void) snprintf(buf, sizeof (buf), "%s",
1046 mountpoint);
1049 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1050 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1051 "%s\n"), buf, strerror(errno));
1052 (void) fprintf(stderr, gettext("use '-m' "
1053 "option to provide a different default\n"));
1054 goto errout;
1055 } else if (dirp) {
1056 int count = 0;
1058 while (count < 3 && readdir(dirp) != NULL)
1059 count++;
1060 (void) closedir(dirp);
1062 if (count > 2) {
1063 (void) fprintf(stderr, gettext("mountpoint "
1064 "'%s' exists and is not empty\n"), buf);
1065 (void) fprintf(stderr, gettext("use '-m' "
1066 "option to provide a "
1067 "different default\n"));
1068 goto errout;
1074 * Now that the mountpoint's validity has been checked, ensure that
1075 * the property is set appropriately prior to creating the pool.
1077 if (mountpoint != NULL) {
1078 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1079 mountpoint, &fsprops, B_FALSE);
1080 if (ret != 0)
1081 goto errout;
1084 ret = 1;
1085 if (dryrun) {
1087 * For a dry run invocation, print out a basic message and run
1088 * through all the vdevs in the list and print out in an
1089 * appropriate hierarchy.
1091 (void) printf(gettext("would create '%s' with the "
1092 "following layout:\n\n"), poolname);
1094 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1095 if (num_logs(nvroot) > 0)
1096 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1098 ret = 0;
1099 } else {
1101 * Hand off to libzfs.
1103 if (enable_all_pool_feat) {
1104 spa_feature_t i;
1105 for (i = 0; i < SPA_FEATURES; i++) {
1106 char propname[MAXPATHLEN];
1107 zfeature_info_t *feat = &spa_feature_table[i];
1109 (void) snprintf(propname, sizeof (propname),
1110 "feature@%s", feat->fi_uname);
1113 * Skip feature if user specified it manually
1114 * on the command line.
1116 if (nvlist_exists(props, propname))
1117 continue;
1119 ret = add_prop_list(propname,
1120 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1121 if (ret != 0)
1122 goto errout;
1126 ret = 1;
1127 if (zpool_create(g_zfs, poolname,
1128 nvroot, props, fsprops) == 0) {
1129 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1130 ZFS_TYPE_FILESYSTEM);
1131 if (pool != NULL) {
1132 if (zfs_mount(pool, NULL, 0) == 0)
1133 ret = zfs_shareall(pool);
1134 zfs_close(pool);
1136 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1137 (void) fprintf(stderr, gettext("pool name may have "
1138 "been omitted\n"));
1142 errout:
1143 nvlist_free(nvroot);
1144 nvlist_free(fsprops);
1145 nvlist_free(props);
1146 return (ret);
1147 badusage:
1148 nvlist_free(fsprops);
1149 nvlist_free(props);
1150 usage(B_FALSE);
1151 return (2);
1155 * zpool destroy <pool>
1157 * -f Forcefully unmount any datasets
1159 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1162 zpool_do_destroy(int argc, char **argv)
1164 boolean_t force = B_FALSE;
1165 int c;
1166 char *pool;
1167 zpool_handle_t *zhp;
1168 int ret;
1170 /* check options */
1171 while ((c = getopt(argc, argv, "f")) != -1) {
1172 switch (c) {
1173 case 'f':
1174 force = B_TRUE;
1175 break;
1176 case '?':
1177 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1178 optopt);
1179 usage(B_FALSE);
1183 argc -= optind;
1184 argv += optind;
1186 /* check arguments */
1187 if (argc < 1) {
1188 (void) fprintf(stderr, gettext("missing pool argument\n"));
1189 usage(B_FALSE);
1191 if (argc > 1) {
1192 (void) fprintf(stderr, gettext("too many arguments\n"));
1193 usage(B_FALSE);
1196 pool = argv[0];
1198 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1200 * As a special case, check for use of '/' in the name, and
1201 * direct the user to use 'zfs destroy' instead.
1203 if (strchr(pool, '/') != NULL)
1204 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1205 "destroy a dataset\n"));
1206 return (1);
1209 if (zpool_disable_datasets(zhp, force) != 0) {
1210 (void) fprintf(stderr, gettext("could not destroy '%s': "
1211 "could not unmount datasets\n"), zpool_get_name(zhp));
1212 return (1);
1215 /* The history must be logged as part of the export */
1216 log_history = B_FALSE;
1218 ret = (zpool_destroy(zhp, history_str) != 0);
1220 zpool_close(zhp);
1222 return (ret);
1226 * zpool export [-f] <pool> ...
1228 * -f Forcefully unmount datasets
1230 * Export the given pools. By default, the command will attempt to cleanly
1231 * unmount any active datasets within the pool. If the '-f' flag is specified,
1232 * then the datasets will be forcefully unmounted.
1235 zpool_do_export(int argc, char **argv)
1237 boolean_t force = B_FALSE;
1238 boolean_t hardforce = B_FALSE;
1239 int c;
1240 zpool_handle_t *zhp;
1241 int ret;
1242 int i;
1244 /* check options */
1245 while ((c = getopt(argc, argv, "fF")) != -1) {
1246 switch (c) {
1247 case 'f':
1248 force = B_TRUE;
1249 break;
1250 case 'F':
1251 hardforce = B_TRUE;
1252 break;
1253 case '?':
1254 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1255 optopt);
1256 usage(B_FALSE);
1260 argc -= optind;
1261 argv += optind;
1263 /* check arguments */
1264 if (argc < 1) {
1265 (void) fprintf(stderr, gettext("missing pool argument\n"));
1266 usage(B_FALSE);
1269 ret = 0;
1270 for (i = 0; i < argc; i++) {
1271 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1272 ret = 1;
1273 continue;
1276 if (zpool_disable_datasets(zhp, force) != 0) {
1277 ret = 1;
1278 zpool_close(zhp);
1279 continue;
1282 /* The history must be logged as part of the export */
1283 log_history = B_FALSE;
1285 if (hardforce) {
1286 if (zpool_export_force(zhp, history_str) != 0)
1287 ret = 1;
1288 } else if (zpool_export(zhp, force, history_str) != 0) {
1289 ret = 1;
1292 zpool_close(zhp);
1295 return (ret);
1299 * Given a vdev configuration, determine the maximum width needed for the device
1300 * name column.
1302 static int
1303 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1305 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1306 nvlist_t **child;
1307 uint_t c, children;
1308 int ret;
1310 if (strlen(name) + depth > max)
1311 max = strlen(name) + depth;
1313 free(name);
1315 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1316 &child, &children) == 0) {
1317 for (c = 0; c < children; c++)
1318 if ((ret = max_width(zhp, child[c], depth + 2,
1319 max)) > max)
1320 max = ret;
1323 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1324 &child, &children) == 0) {
1325 for (c = 0; c < children; c++)
1326 if ((ret = max_width(zhp, child[c], depth + 2,
1327 max)) > max)
1328 max = ret;
1331 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1332 &child, &children) == 0) {
1333 for (c = 0; c < children; c++)
1334 if ((ret = max_width(zhp, child[c], depth + 2,
1335 max)) > max)
1336 max = ret;
1340 return (max);
1343 typedef struct spare_cbdata {
1344 uint64_t cb_guid;
1345 zpool_handle_t *cb_zhp;
1346 } spare_cbdata_t;
1348 static boolean_t
1349 find_vdev(nvlist_t *nv, uint64_t search)
1351 uint64_t guid;
1352 nvlist_t **child;
1353 uint_t c, children;
1355 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1356 search == guid)
1357 return (B_TRUE);
1359 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1360 &child, &children) == 0) {
1361 for (c = 0; c < children; c++)
1362 if (find_vdev(child[c], search))
1363 return (B_TRUE);
1366 return (B_FALSE);
1369 static int
1370 find_spare(zpool_handle_t *zhp, void *data)
1372 spare_cbdata_t *cbp = data;
1373 nvlist_t *config, *nvroot;
1375 config = zpool_get_config(zhp, NULL);
1376 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1377 &nvroot) == 0);
1379 if (find_vdev(nvroot, cbp->cb_guid)) {
1380 cbp->cb_zhp = zhp;
1381 return (1);
1384 zpool_close(zhp);
1385 return (0);
1389 * Print out configuration state as requested by status_callback.
1391 void
1392 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1393 int namewidth, int depth, boolean_t isspare)
1395 nvlist_t **child;
1396 uint_t c, children;
1397 pool_scan_stat_t *ps = NULL;
1398 vdev_stat_t *vs;
1399 char rbuf[6], wbuf[6], cbuf[6];
1400 char *vname;
1401 uint64_t notpresent;
1402 spare_cbdata_t cb;
1403 const char *state;
1405 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1406 &child, &children) != 0)
1407 children = 0;
1409 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1410 (uint64_t **)&vs, &c) == 0);
1412 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1413 if (isspare) {
1415 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1416 * online drives.
1418 if (vs->vs_aux == VDEV_AUX_SPARED)
1419 state = "INUSE";
1420 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1421 state = "AVAIL";
1424 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1425 name, state);
1427 if (!isspare) {
1428 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1429 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1430 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1431 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1434 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1435 &notpresent) == 0) {
1436 char *path;
1437 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1438 (void) printf(" was %s", path);
1439 } else if (vs->vs_aux != 0) {
1440 (void) printf(" ");
1442 switch (vs->vs_aux) {
1443 case VDEV_AUX_OPEN_FAILED:
1444 (void) printf(gettext("cannot open"));
1445 break;
1447 case VDEV_AUX_BAD_GUID_SUM:
1448 (void) printf(gettext("missing device"));
1449 break;
1451 case VDEV_AUX_NO_REPLICAS:
1452 (void) printf(gettext("insufficient replicas"));
1453 break;
1455 case VDEV_AUX_VERSION_NEWER:
1456 (void) printf(gettext("newer version"));
1457 break;
1459 case VDEV_AUX_UNSUP_FEAT:
1460 (void) printf(gettext("unsupported feature(s)"));
1461 break;
1463 case VDEV_AUX_SPARED:
1464 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1465 &cb.cb_guid) == 0);
1466 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1467 if (strcmp(zpool_get_name(cb.cb_zhp),
1468 zpool_get_name(zhp)) == 0)
1469 (void) printf(gettext("currently in "
1470 "use"));
1471 else
1472 (void) printf(gettext("in use by "
1473 "pool '%s'"),
1474 zpool_get_name(cb.cb_zhp));
1475 zpool_close(cb.cb_zhp);
1476 } else {
1477 (void) printf(gettext("currently in use"));
1479 break;
1481 case VDEV_AUX_ERR_EXCEEDED:
1482 (void) printf(gettext("too many errors"));
1483 break;
1485 case VDEV_AUX_IO_FAILURE:
1486 (void) printf(gettext("experienced I/O failures"));
1487 break;
1489 case VDEV_AUX_BAD_LOG:
1490 (void) printf(gettext("bad intent log"));
1491 break;
1493 case VDEV_AUX_EXTERNAL:
1494 (void) printf(gettext("external device fault"));
1495 break;
1497 case VDEV_AUX_SPLIT_POOL:
1498 (void) printf(gettext("split into new pool"));
1499 break;
1501 default:
1502 (void) printf(gettext("corrupted data"));
1503 break;
1507 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1508 (uint64_t **)&ps, &c);
1510 if (ps && ps->pss_state == DSS_SCANNING &&
1511 vs->vs_scan_processed != 0 && children == 0) {
1512 (void) printf(gettext(" (%s)"),
1513 (ps->pss_func == POOL_SCAN_RESILVER) ?
1514 "resilvering" : "repairing");
1517 (void) printf("\n");
1519 for (c = 0; c < children; c++) {
1520 uint64_t islog = B_FALSE, ishole = B_FALSE;
1522 /* Don't print logs or holes here */
1523 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1524 &islog);
1525 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1526 &ishole);
1527 if (islog || ishole)
1528 continue;
1529 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1530 print_status_config(zhp, vname, child[c],
1531 namewidth, depth + 2, isspare);
1532 free(vname);
1538 * Print the configuration of an exported pool. Iterate over all vdevs in the
1539 * pool, printing out the name and status for each one.
1541 void
1542 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1544 nvlist_t **child;
1545 uint_t c, children;
1546 vdev_stat_t *vs;
1547 char *type, *vname;
1549 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1550 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1551 strcmp(type, VDEV_TYPE_HOLE) == 0)
1552 return;
1554 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1555 (uint64_t **)&vs, &c) == 0);
1557 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1558 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1560 if (vs->vs_aux != 0) {
1561 (void) printf(" ");
1563 switch (vs->vs_aux) {
1564 case VDEV_AUX_OPEN_FAILED:
1565 (void) printf(gettext("cannot open"));
1566 break;
1568 case VDEV_AUX_BAD_GUID_SUM:
1569 (void) printf(gettext("missing device"));
1570 break;
1572 case VDEV_AUX_NO_REPLICAS:
1573 (void) printf(gettext("insufficient replicas"));
1574 break;
1576 case VDEV_AUX_VERSION_NEWER:
1577 (void) printf(gettext("newer version"));
1578 break;
1580 case VDEV_AUX_UNSUP_FEAT:
1581 (void) printf(gettext("unsupported feature(s)"));
1582 break;
1584 case VDEV_AUX_ERR_EXCEEDED:
1585 (void) printf(gettext("too many errors"));
1586 break;
1588 default:
1589 (void) printf(gettext("corrupted data"));
1590 break;
1593 (void) printf("\n");
1595 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1596 &child, &children) != 0)
1597 return;
1599 for (c = 0; c < children; c++) {
1600 uint64_t is_log = B_FALSE;
1602 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1603 &is_log);
1604 if (is_log)
1605 continue;
1607 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1608 print_import_config(vname, child[c], namewidth, depth + 2);
1609 free(vname);
1612 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1613 &child, &children) == 0) {
1614 (void) printf(gettext("\tcache\n"));
1615 for (c = 0; c < children; c++) {
1616 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1617 (void) printf("\t %s\n", vname);
1618 free(vname);
1622 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1623 &child, &children) == 0) {
1624 (void) printf(gettext("\tspares\n"));
1625 for (c = 0; c < children; c++) {
1626 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1627 (void) printf("\t %s\n", vname);
1628 free(vname);
1634 * Print log vdevs.
1635 * Logs are recorded as top level vdevs in the main pool child array
1636 * but with "is_log" set to 1. We use either print_status_config() or
1637 * print_import_config() to print the top level logs then any log
1638 * children (eg mirrored slogs) are printed recursively - which
1639 * works because only the top level vdev is marked "is_log"
1641 static void
1642 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1644 uint_t c, children;
1645 nvlist_t **child;
1647 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1648 &children) != 0)
1649 return;
1651 (void) printf(gettext("\tlogs\n"));
1653 for (c = 0; c < children; c++) {
1654 uint64_t is_log = B_FALSE;
1655 char *name;
1657 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1658 &is_log);
1659 if (!is_log)
1660 continue;
1661 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1662 if (verbose)
1663 print_status_config(zhp, name, child[c], namewidth,
1664 2, B_FALSE);
1665 else
1666 print_import_config(name, child[c], namewidth, 2);
1667 free(name);
1672 * Display the status for the given pool.
1674 static void
1675 show_import(nvlist_t *config)
1677 uint64_t pool_state;
1678 vdev_stat_t *vs;
1679 char *name;
1680 uint64_t guid;
1681 char *msgid;
1682 nvlist_t *nvroot;
1683 int reason;
1684 const char *health;
1685 uint_t vsc;
1686 int namewidth;
1687 char *comment;
1689 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1690 &name) == 0);
1691 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1692 &guid) == 0);
1693 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1694 &pool_state) == 0);
1695 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1696 &nvroot) == 0);
1698 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1699 (uint64_t **)&vs, &vsc) == 0);
1700 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1702 reason = zpool_import_status(config, &msgid);
1704 (void) printf(gettext(" pool: %s\n"), name);
1705 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1706 (void) printf(gettext(" state: %s"), health);
1707 if (pool_state == POOL_STATE_DESTROYED)
1708 (void) printf(gettext(" (DESTROYED)"));
1709 (void) printf("\n");
1711 switch (reason) {
1712 case ZPOOL_STATUS_MISSING_DEV_R:
1713 case ZPOOL_STATUS_MISSING_DEV_NR:
1714 case ZPOOL_STATUS_BAD_GUID_SUM:
1715 (void) printf(gettext(" status: One or more devices are "
1716 "missing from the system.\n"));
1717 break;
1719 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1720 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1721 (void) printf(gettext(" status: One or more devices contains "
1722 "corrupted data.\n"));
1723 break;
1725 case ZPOOL_STATUS_CORRUPT_DATA:
1726 (void) printf(
1727 gettext(" status: The pool data is corrupted.\n"));
1728 break;
1730 case ZPOOL_STATUS_OFFLINE_DEV:
1731 (void) printf(gettext(" status: One or more devices "
1732 "are offlined.\n"));
1733 break;
1735 case ZPOOL_STATUS_CORRUPT_POOL:
1736 (void) printf(gettext(" status: The pool metadata is "
1737 "corrupted.\n"));
1738 break;
1740 case ZPOOL_STATUS_VERSION_OLDER:
1741 (void) printf(gettext(" status: The pool is formatted using a "
1742 "legacy on-disk version.\n"));
1743 break;
1745 case ZPOOL_STATUS_VERSION_NEWER:
1746 (void) printf(gettext(" status: The pool is formatted using an "
1747 "incompatible version.\n"));
1748 break;
1750 case ZPOOL_STATUS_FEAT_DISABLED:
1751 (void) printf(gettext(" status: Some supported features are "
1752 "not enabled on the pool.\n"));
1753 break;
1755 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1756 (void) printf(gettext("status: The pool uses the following "
1757 "feature(s) not supported on this sytem:\n"));
1758 zpool_print_unsup_feat(config);
1759 break;
1761 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1762 (void) printf(gettext("status: The pool can only be accessed "
1763 "in read-only mode on this system. It\n\tcannot be "
1764 "accessed in read-write mode because it uses the "
1765 "following\n\tfeature(s) not supported on this system:\n"));
1766 zpool_print_unsup_feat(config);
1767 break;
1769 case ZPOOL_STATUS_HOSTID_MISMATCH:
1770 (void) printf(gettext(" status: The pool was last accessed by "
1771 "another system.\n"));
1772 break;
1774 case ZPOOL_STATUS_FAULTED_DEV_R:
1775 case ZPOOL_STATUS_FAULTED_DEV_NR:
1776 (void) printf(gettext(" status: One or more devices are "
1777 "faulted.\n"));
1778 break;
1780 case ZPOOL_STATUS_BAD_LOG:
1781 (void) printf(gettext(" status: An intent log record cannot be "
1782 "read.\n"));
1783 break;
1785 case ZPOOL_STATUS_RESILVERING:
1786 (void) printf(gettext(" status: One or more devices were being "
1787 "resilvered.\n"));
1788 break;
1790 default:
1792 * No other status can be seen when importing pools.
1794 assert(reason == ZPOOL_STATUS_OK);
1798 * Print out an action according to the overall state of the pool.
1800 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1801 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1802 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1803 (void) printf(gettext(" action: The pool can be "
1804 "imported using its name or numeric identifier, "
1805 "though\n\tsome features will not be available "
1806 "without an explicit 'zpool upgrade'.\n"));
1807 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1808 (void) printf(gettext(" action: The pool can be "
1809 "imported using its name or numeric "
1810 "identifier and\n\tthe '-f' flag.\n"));
1811 } else {
1812 (void) printf(gettext(" action: The pool can be "
1813 "imported using its name or numeric "
1814 "identifier.\n"));
1816 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1817 (void) printf(gettext(" action: The pool can be imported "
1818 "despite missing or damaged devices. The\n\tfault "
1819 "tolerance of the pool may be compromised if imported.\n"));
1820 } else {
1821 switch (reason) {
1822 case ZPOOL_STATUS_VERSION_NEWER:
1823 (void) printf(gettext(" action: The pool cannot be "
1824 "imported. Access the pool on a system running "
1825 "newer\n\tsoftware, or recreate the pool from "
1826 "backup.\n"));
1827 break;
1828 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1829 (void) printf(gettext("action: The pool cannot be "
1830 "imported. Access the pool on a system that "
1831 "supports\n\tthe required feature(s), or recreate "
1832 "the pool from backup.\n"));
1833 break;
1834 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1835 (void) printf(gettext("action: The pool cannot be "
1836 "imported in read-write mode. Import the pool "
1837 "with\n"
1838 "\t\"-o readonly=on\", access the pool on a system "
1839 "that supports the\n\trequired feature(s), or "
1840 "recreate the pool from backup.\n"));
1841 break;
1842 case ZPOOL_STATUS_MISSING_DEV_R:
1843 case ZPOOL_STATUS_MISSING_DEV_NR:
1844 case ZPOOL_STATUS_BAD_GUID_SUM:
1845 (void) printf(gettext(" action: The pool cannot be "
1846 "imported. Attach the missing\n\tdevices and try "
1847 "again.\n"));
1848 break;
1849 default:
1850 (void) printf(gettext(" action: The pool cannot be "
1851 "imported due to damaged devices or data.\n"));
1855 /* Print the comment attached to the pool. */
1856 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1857 (void) printf(gettext("comment: %s\n"), comment);
1860 * If the state is "closed" or "can't open", and the aux state
1861 * is "corrupt data":
1863 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1864 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1865 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1866 if (pool_state == POOL_STATE_DESTROYED)
1867 (void) printf(gettext("\tThe pool was destroyed, "
1868 "but can be imported using the '-Df' flags.\n"));
1869 else if (pool_state != POOL_STATE_EXPORTED)
1870 (void) printf(gettext("\tThe pool may be active on "
1871 "another system, but can be imported using\n\t"
1872 "the '-f' flag.\n"));
1875 if (msgid != NULL)
1876 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1877 msgid);
1879 (void) printf(gettext(" config:\n\n"));
1881 namewidth = max_width(NULL, nvroot, 0, 0);
1882 if (namewidth < 10)
1883 namewidth = 10;
1885 print_import_config(name, nvroot, namewidth, 0);
1886 if (num_logs(nvroot) > 0)
1887 print_logs(NULL, nvroot, namewidth, B_FALSE);
1889 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1890 (void) printf(gettext("\n\tAdditional devices are known to "
1891 "be part of this pool, though their\n\texact "
1892 "configuration cannot be determined.\n"));
1897 * Perform the import for the given configuration. This passes the heavy
1898 * lifting off to zpool_import_props(), and then mounts the datasets contained
1899 * within the pool.
1901 static int
1902 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1903 nvlist_t *props, int flags)
1905 zpool_handle_t *zhp;
1906 char *name;
1907 uint64_t state;
1908 uint64_t version;
1910 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1911 &name) == 0);
1913 verify(nvlist_lookup_uint64(config,
1914 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1915 verify(nvlist_lookup_uint64(config,
1916 ZPOOL_CONFIG_VERSION, &version) == 0);
1917 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1918 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1919 "is formatted using an unsupported ZFS version\n"), name);
1920 return (1);
1921 } else if (state != POOL_STATE_EXPORTED &&
1922 !(flags & ZFS_IMPORT_ANY_HOST)) {
1923 uint64_t hostid;
1925 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1926 &hostid) == 0) {
1927 if ((unsigned long)hostid != gethostid()) {
1928 char *hostname;
1929 uint64_t timestamp;
1930 time_t t;
1932 verify(nvlist_lookup_string(config,
1933 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1934 verify(nvlist_lookup_uint64(config,
1935 ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1936 t = timestamp;
1937 (void) fprintf(stderr, gettext("cannot import "
1938 "'%s': pool may be in use from other "
1939 "system, it was last accessed by %s "
1940 "(hostid: 0x%lx) on %s"), name, hostname,
1941 (unsigned long)hostid,
1942 asctime(localtime(&t)));
1943 (void) fprintf(stderr, gettext("use '-f' to "
1944 "import anyway\n"));
1945 return (1);
1947 } else {
1948 (void) fprintf(stderr, gettext("cannot import '%s': "
1949 "pool may be in use from other system\n"), name);
1950 (void) fprintf(stderr, gettext("use '-f' to import "
1951 "anyway\n"));
1952 return (1);
1956 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1957 return (1);
1959 if (newname != NULL)
1960 name = (char *)newname;
1962 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1963 return (1);
1965 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1966 !(flags & ZFS_IMPORT_ONLY) &&
1967 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1968 zpool_close(zhp);
1969 return (1);
1972 zpool_close(zhp);
1973 return (0);
1977 * zpool import [-d dir] [-D]
1978 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1979 * [-d dir | -c cachefile] [-f] -a
1980 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1981 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1983 * -c Read pool information from a cachefile instead of searching
1984 * devices.
1986 * -d Scan in a specific directory, other than /dev/dsk. More than
1987 * one directory can be specified using multiple '-d' options.
1989 * -D Scan for previously destroyed pools or import all or only
1990 * specified destroyed pools.
1992 * -R Temporarily import the pool, with all mountpoints relative to
1993 * the given root. The pool will remain exported when the machine
1994 * is rebooted.
1996 * -V Import even in the presence of faulted vdevs. This is an
1997 * intentionally undocumented option for testing purposes, and
1998 * treats the pool configuration as complete, leaving any bad
1999 * vdevs in the FAULTED state. In other words, it does verbatim
2000 * import.
2002 * -f Force import, even if it appears that the pool is active.
2004 * -F Attempt rewind if necessary.
2006 * -n See if rewind would work, but don't actually rewind.
2008 * -N Import the pool but don't mount datasets.
2010 * -T Specify a starting txg to use for import. This option is
2011 * intentionally undocumented option for testing purposes.
2013 * -a Import all pools found.
2015 * -o Set property=value and/or temporary mount options (without '=').
2017 * The import command scans for pools to import, and import pools based on pool
2018 * name and GUID. The pool can also be renamed as part of the import process.
2021 zpool_do_import(int argc, char **argv)
2023 char **searchdirs = NULL;
2024 int nsearch = 0;
2025 int c;
2026 int err = 0;
2027 nvlist_t *pools = NULL;
2028 boolean_t do_all = B_FALSE;
2029 boolean_t do_destroyed = B_FALSE;
2030 char *mntopts = NULL;
2031 nvpair_t *elem;
2032 nvlist_t *config;
2033 uint64_t searchguid = 0;
2034 char *searchname = NULL;
2035 char *propval;
2036 nvlist_t *found_config;
2037 nvlist_t *policy = NULL;
2038 nvlist_t *props = NULL;
2039 boolean_t first;
2040 int flags = ZFS_IMPORT_NORMAL;
2041 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2042 boolean_t dryrun = B_FALSE;
2043 boolean_t do_rewind = B_FALSE;
2044 boolean_t xtreme_rewind = B_FALSE;
2045 uint64_t pool_state, txg = -1ULL;
2046 char *cachefile = NULL;
2047 importargs_t idata = { 0 };
2048 char *endptr;
2050 /* check options */
2051 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
2052 switch (c) {
2053 case 'a':
2054 do_all = B_TRUE;
2055 break;
2056 case 'c':
2057 cachefile = optarg;
2058 break;
2059 case 'd':
2060 if (searchdirs == NULL) {
2061 searchdirs = safe_malloc(sizeof (char *));
2062 } else {
2063 char **tmp = safe_malloc((nsearch + 1) *
2064 sizeof (char *));
2065 bcopy(searchdirs, tmp, nsearch *
2066 sizeof (char *));
2067 free(searchdirs);
2068 searchdirs = tmp;
2070 searchdirs[nsearch++] = optarg;
2071 break;
2072 case 'D':
2073 do_destroyed = B_TRUE;
2074 break;
2075 case 'f':
2076 flags |= ZFS_IMPORT_ANY_HOST;
2077 break;
2078 case 'F':
2079 do_rewind = B_TRUE;
2080 break;
2081 case 'm':
2082 flags |= ZFS_IMPORT_MISSING_LOG;
2083 break;
2084 case 'n':
2085 dryrun = B_TRUE;
2086 break;
2087 case 'N':
2088 flags |= ZFS_IMPORT_ONLY;
2089 break;
2090 case 'o':
2091 if ((propval = strchr(optarg, '=')) != NULL) {
2092 *propval = '\0';
2093 propval++;
2094 if (add_prop_list(optarg, propval,
2095 &props, B_TRUE))
2096 goto error;
2097 } else {
2098 mntopts = optarg;
2100 break;
2101 case 'R':
2102 if (add_prop_list(zpool_prop_to_name(
2103 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2104 goto error;
2105 if (nvlist_lookup_string(props,
2106 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2107 &propval) == 0)
2108 break;
2109 if (add_prop_list(zpool_prop_to_name(
2110 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2111 goto error;
2112 break;
2113 case 'T':
2114 errno = 0;
2115 txg = strtoull(optarg, &endptr, 0);
2116 if (errno != 0 || *endptr != '\0') {
2117 (void) fprintf(stderr,
2118 gettext("invalid txg value\n"));
2119 usage(B_FALSE);
2121 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2122 break;
2123 case 'V':
2124 flags |= ZFS_IMPORT_VERBATIM;
2125 break;
2126 case 'X':
2127 xtreme_rewind = B_TRUE;
2128 break;
2129 case ':':
2130 (void) fprintf(stderr, gettext("missing argument for "
2131 "'%c' option\n"), optopt);
2132 usage(B_FALSE);
2133 break;
2134 case '?':
2135 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2136 optopt);
2137 usage(B_FALSE);
2141 argc -= optind;
2142 argv += optind;
2144 if (cachefile && nsearch != 0) {
2145 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2146 usage(B_FALSE);
2149 if ((dryrun || xtreme_rewind) && !do_rewind) {
2150 (void) fprintf(stderr,
2151 gettext("-n or -X only meaningful with -F\n"));
2152 usage(B_FALSE);
2154 if (dryrun)
2155 rewind_policy = ZPOOL_TRY_REWIND;
2156 else if (do_rewind)
2157 rewind_policy = ZPOOL_DO_REWIND;
2158 if (xtreme_rewind)
2159 rewind_policy |= ZPOOL_EXTREME_REWIND;
2161 /* In the future, we can capture further policy and include it here */
2162 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2163 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2164 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2165 goto error;
2167 if (searchdirs == NULL) {
2168 searchdirs = safe_malloc(sizeof (char *));
2169 searchdirs[0] = ZFS_DISK_ROOT;
2170 nsearch = 1;
2173 /* check argument count */
2174 if (do_all) {
2175 if (argc != 0) {
2176 (void) fprintf(stderr, gettext("too many arguments\n"));
2177 usage(B_FALSE);
2179 } else {
2180 if (argc > 2) {
2181 (void) fprintf(stderr, gettext("too many arguments\n"));
2182 usage(B_FALSE);
2186 * Check for the SYS_CONFIG privilege. We do this explicitly
2187 * here because otherwise any attempt to discover pools will
2188 * silently fail.
2190 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2191 (void) fprintf(stderr, gettext("cannot "
2192 "discover pools: permission denied\n"));
2193 free(searchdirs);
2194 nvlist_free(policy);
2195 return (1);
2200 * Depending on the arguments given, we do one of the following:
2202 * <none> Iterate through all pools and display information about
2203 * each one.
2205 * -a Iterate through all pools and try to import each one.
2207 * <id> Find the pool that corresponds to the given GUID/pool
2208 * name and import that one.
2210 * -D Above options applies only to destroyed pools.
2212 if (argc != 0) {
2213 char *endptr;
2215 errno = 0;
2216 searchguid = strtoull(argv[0], &endptr, 10);
2217 if (errno != 0 || *endptr != '\0') {
2218 searchname = argv[0];
2219 searchguid = 0;
2221 found_config = NULL;
2224 * User specified a name or guid. Ensure it's unique.
2226 idata.unique = B_TRUE;
2230 idata.path = searchdirs;
2231 idata.paths = nsearch;
2232 idata.poolname = searchname;
2233 idata.guid = searchguid;
2234 idata.cachefile = cachefile;
2236 pools = zpool_search_import(g_zfs, &idata);
2238 if (pools != NULL && idata.exists &&
2239 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2240 (void) fprintf(stderr, gettext("cannot import '%s': "
2241 "a pool with that name already exists\n"),
2242 argv[0]);
2243 (void) fprintf(stderr, gettext("use the form '%s "
2244 "<pool | id> <newpool>' to give it a new name\n"),
2245 "zpool import");
2246 err = 1;
2247 } else if (pools == NULL && idata.exists) {
2248 (void) fprintf(stderr, gettext("cannot import '%s': "
2249 "a pool with that name is already created/imported,\n"),
2250 argv[0]);
2251 (void) fprintf(stderr, gettext("and no additional pools "
2252 "with that name were found\n"));
2253 err = 1;
2254 } else if (pools == NULL) {
2255 if (argc != 0) {
2256 (void) fprintf(stderr, gettext("cannot import '%s': "
2257 "no such pool available\n"), argv[0]);
2259 err = 1;
2262 if (err == 1) {
2263 free(searchdirs);
2264 nvlist_free(policy);
2265 return (1);
2269 * At this point we have a list of import candidate configs. Even if
2270 * we were searching by pool name or guid, we still need to
2271 * post-process the list to deal with pool state and possible
2272 * duplicate names.
2274 err = 0;
2275 elem = NULL;
2276 first = B_TRUE;
2277 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2279 verify(nvpair_value_nvlist(elem, &config) == 0);
2281 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2282 &pool_state) == 0);
2283 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2284 continue;
2285 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2286 continue;
2288 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2289 policy) == 0);
2291 if (argc == 0) {
2292 if (first)
2293 first = B_FALSE;
2294 else if (!do_all)
2295 (void) printf("\n");
2297 if (do_all) {
2298 err |= do_import(config, NULL, mntopts,
2299 props, flags);
2300 } else {
2301 show_import(config);
2303 } else if (searchname != NULL) {
2304 char *name;
2307 * We are searching for a pool based on name.
2309 verify(nvlist_lookup_string(config,
2310 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2312 if (strcmp(name, searchname) == 0) {
2313 if (found_config != NULL) {
2314 (void) fprintf(stderr, gettext(
2315 "cannot import '%s': more than "
2316 "one matching pool\n"), searchname);
2317 (void) fprintf(stderr, gettext(
2318 "import by numeric ID instead\n"));
2319 err = B_TRUE;
2321 found_config = config;
2323 } else {
2324 uint64_t guid;
2327 * Search for a pool by guid.
2329 verify(nvlist_lookup_uint64(config,
2330 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2332 if (guid == searchguid)
2333 found_config = config;
2338 * If we were searching for a specific pool, verify that we found a
2339 * pool, and then do the import.
2341 if (argc != 0 && err == 0) {
2342 if (found_config == NULL) {
2343 (void) fprintf(stderr, gettext("cannot import '%s': "
2344 "no such pool available\n"), argv[0]);
2345 err = B_TRUE;
2346 } else {
2347 err |= do_import(found_config, argc == 1 ? NULL :
2348 argv[1], mntopts, props, flags);
2353 * If we were just looking for pools, report an error if none were
2354 * found.
2356 if (argc == 0 && first)
2357 (void) fprintf(stderr,
2358 gettext("no pools available to import\n"));
2360 error:
2361 nvlist_free(props);
2362 nvlist_free(pools);
2363 nvlist_free(policy);
2364 free(searchdirs);
2366 return (err ? 1 : 0);
2369 typedef struct iostat_cbdata {
2370 boolean_t cb_verbose;
2371 int cb_namewidth;
2372 int cb_iteration;
2373 zpool_list_t *cb_list;
2374 } iostat_cbdata_t;
2376 static void
2377 print_iostat_separator(iostat_cbdata_t *cb)
2379 int i = 0;
2381 for (i = 0; i < cb->cb_namewidth; i++)
2382 (void) printf("-");
2383 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2386 static void
2387 print_iostat_header(iostat_cbdata_t *cb)
2389 (void) printf("%*s capacity operations bandwidth\n",
2390 cb->cb_namewidth, "");
2391 (void) printf("%-*s alloc free read write read write\n",
2392 cb->cb_namewidth, "pool");
2393 print_iostat_separator(cb);
2397 * Display a single statistic.
2399 static void
2400 print_one_stat(uint64_t value)
2402 char buf[64];
2404 zfs_nicenum(value, buf, sizeof (buf));
2405 (void) printf(" %5s", buf);
2409 * Print out all the statistics for the given vdev. This can either be the
2410 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2411 * is a verbose output, and we don't want to display the toplevel pool stats.
2413 void
2414 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2415 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2417 nvlist_t **oldchild, **newchild;
2418 uint_t c, children;
2419 vdev_stat_t *oldvs, *newvs;
2420 vdev_stat_t zerovs = { 0 };
2421 uint64_t tdelta;
2422 double scale;
2423 char *vname;
2425 if (oldnv != NULL) {
2426 verify(nvlist_lookup_uint64_array(oldnv,
2427 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2428 } else {
2429 oldvs = &zerovs;
2432 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2433 (uint64_t **)&newvs, &c) == 0);
2435 if (strlen(name) + depth > cb->cb_namewidth)
2436 (void) printf("%*s%s", depth, "", name);
2437 else
2438 (void) printf("%*s%s%*s", depth, "", name,
2439 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2441 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2443 if (tdelta == 0)
2444 scale = 1.0;
2445 else
2446 scale = (double)NANOSEC / tdelta;
2448 /* only toplevel vdevs have capacity stats */
2449 if (newvs->vs_space == 0) {
2450 (void) printf(" - -");
2451 } else {
2452 print_one_stat(newvs->vs_alloc);
2453 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2456 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2457 oldvs->vs_ops[ZIO_TYPE_READ])));
2459 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2460 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2462 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2463 oldvs->vs_bytes[ZIO_TYPE_READ])));
2465 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2466 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2468 (void) printf("\n");
2470 if (!cb->cb_verbose)
2471 return;
2473 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2474 &newchild, &children) != 0)
2475 return;
2477 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2478 &oldchild, &c) != 0)
2479 return;
2481 for (c = 0; c < children; c++) {
2482 uint64_t ishole = B_FALSE, islog = B_FALSE;
2484 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2485 &ishole);
2487 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2488 &islog);
2490 if (ishole || islog)
2491 continue;
2493 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2494 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2495 newchild[c], cb, depth + 2);
2496 free(vname);
2500 * Log device section
2503 if (num_logs(newnv) > 0) {
2504 (void) printf("%-*s - - - - - "
2505 "-\n", cb->cb_namewidth, "logs");
2507 for (c = 0; c < children; c++) {
2508 uint64_t islog = B_FALSE;
2509 (void) nvlist_lookup_uint64(newchild[c],
2510 ZPOOL_CONFIG_IS_LOG, &islog);
2512 if (islog) {
2513 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2514 B_FALSE);
2515 print_vdev_stats(zhp, vname, oldnv ?
2516 oldchild[c] : NULL, newchild[c],
2517 cb, depth + 2);
2518 free(vname);
2525 * Include level 2 ARC devices in iostat output
2527 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2528 &newchild, &children) != 0)
2529 return;
2531 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2532 &oldchild, &c) != 0)
2533 return;
2535 if (children > 0) {
2536 (void) printf("%-*s - - - - - "
2537 "-\n", cb->cb_namewidth, "cache");
2538 for (c = 0; c < children; c++) {
2539 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2540 B_FALSE);
2541 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2542 newchild[c], cb, depth + 2);
2543 free(vname);
2548 static int
2549 refresh_iostat(zpool_handle_t *zhp, void *data)
2551 iostat_cbdata_t *cb = data;
2552 boolean_t missing;
2555 * If the pool has disappeared, remove it from the list and continue.
2557 if (zpool_refresh_stats(zhp, &missing) != 0)
2558 return (-1);
2560 if (missing)
2561 pool_list_remove(cb->cb_list, zhp);
2563 return (0);
2567 * Callback to print out the iostats for the given pool.
2570 print_iostat(zpool_handle_t *zhp, void *data)
2572 iostat_cbdata_t *cb = data;
2573 nvlist_t *oldconfig, *newconfig;
2574 nvlist_t *oldnvroot, *newnvroot;
2576 newconfig = zpool_get_config(zhp, &oldconfig);
2578 if (cb->cb_iteration == 1)
2579 oldconfig = NULL;
2581 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2582 &newnvroot) == 0);
2584 if (oldconfig == NULL)
2585 oldnvroot = NULL;
2586 else
2587 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2588 &oldnvroot) == 0);
2591 * Print out the statistics for the pool.
2593 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2595 if (cb->cb_verbose)
2596 print_iostat_separator(cb);
2598 return (0);
2602 get_namewidth(zpool_handle_t *zhp, void *data)
2604 iostat_cbdata_t *cb = data;
2605 nvlist_t *config, *nvroot;
2607 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2608 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2609 &nvroot) == 0);
2610 if (!cb->cb_verbose)
2611 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2612 else
2613 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2614 cb->cb_namewidth);
2618 * The width must fall into the range [10,38]. The upper limit is the
2619 * maximum we can have and still fit in 80 columns.
2621 if (cb->cb_namewidth < 10)
2622 cb->cb_namewidth = 10;
2623 if (cb->cb_namewidth > 38)
2624 cb->cb_namewidth = 38;
2626 return (0);
2630 * Parse the input string, get the 'interval' and 'count' value if there is one.
2632 static void
2633 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2634 unsigned long *cnt)
2636 unsigned long interval = 0, count = 0;
2637 int argc = *argcp, errno;
2640 * Determine if the last argument is an integer or a pool name
2642 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2643 char *end;
2645 errno = 0;
2646 interval = strtoul(argv[argc - 1], &end, 10);
2648 if (*end == '\0' && errno == 0) {
2649 if (interval == 0) {
2650 (void) fprintf(stderr, gettext("interval "
2651 "cannot be zero\n"));
2652 usage(B_FALSE);
2655 * Ignore the last parameter
2657 argc--;
2658 } else {
2660 * If this is not a valid number, just plow on. The
2661 * user will get a more informative error message later
2662 * on.
2664 interval = 0;
2669 * If the last argument is also an integer, then we have both a count
2670 * and an interval.
2672 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2673 char *end;
2675 errno = 0;
2676 count = interval;
2677 interval = strtoul(argv[argc - 1], &end, 10);
2679 if (*end == '\0' && errno == 0) {
2680 if (interval == 0) {
2681 (void) fprintf(stderr, gettext("interval "
2682 "cannot be zero\n"));
2683 usage(B_FALSE);
2687 * Ignore the last parameter
2689 argc--;
2690 } else {
2691 interval = 0;
2695 *iv = interval;
2696 *cnt = count;
2697 *argcp = argc;
2700 static void
2701 get_timestamp_arg(char c)
2703 if (c == 'u')
2704 timestamp_fmt = UDATE;
2705 else if (c == 'd')
2706 timestamp_fmt = DDATE;
2707 else
2708 usage(B_FALSE);
2712 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2714 * -v Display statistics for individual vdevs
2715 * -T Display a timestamp in date(1) or Unix format
2717 * This command can be tricky because we want to be able to deal with pool
2718 * creation/destruction as well as vdev configuration changes. The bulk of this
2719 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2720 * on pool_list_update() to detect the addition of new pools. Configuration
2721 * changes are all handled within libzfs.
2724 zpool_do_iostat(int argc, char **argv)
2726 int c;
2727 int ret;
2728 int npools;
2729 unsigned long interval = 0, count = 0;
2730 zpool_list_t *list;
2731 boolean_t verbose = B_FALSE;
2732 iostat_cbdata_t cb;
2734 /* check options */
2735 while ((c = getopt(argc, argv, "T:v")) != -1) {
2736 switch (c) {
2737 case 'T':
2738 get_timestamp_arg(*optarg);
2739 break;
2740 case 'v':
2741 verbose = B_TRUE;
2742 break;
2743 case '?':
2744 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2745 optopt);
2746 usage(B_FALSE);
2750 argc -= optind;
2751 argv += optind;
2753 get_interval_count(&argc, argv, &interval, &count);
2756 * Construct the list of all interesting pools.
2758 ret = 0;
2759 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2760 return (1);
2762 if (pool_list_count(list) == 0 && argc != 0) {
2763 pool_list_free(list);
2764 return (1);
2767 if (pool_list_count(list) == 0 && interval == 0) {
2768 pool_list_free(list);
2769 (void) fprintf(stderr, gettext("no pools available\n"));
2770 return (1);
2774 * Enter the main iostat loop.
2776 cb.cb_list = list;
2777 cb.cb_verbose = verbose;
2778 cb.cb_iteration = 0;
2779 cb.cb_namewidth = 0;
2781 for (;;) {
2782 pool_list_update(list);
2784 if ((npools = pool_list_count(list)) == 0)
2785 break;
2788 * Refresh all statistics. This is done as an explicit step
2789 * before calculating the maximum name width, so that any
2790 * configuration changes are properly accounted for.
2792 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2795 * Iterate over all pools to determine the maximum width
2796 * for the pool / device name column across all pools.
2798 cb.cb_namewidth = 0;
2799 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2801 if (timestamp_fmt != NODATE)
2802 print_timestamp(timestamp_fmt);
2805 * If it's the first time, or verbose mode, print the header.
2807 if (++cb.cb_iteration == 1 || verbose)
2808 print_iostat_header(&cb);
2810 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2813 * If there's more than one pool, and we're not in verbose mode
2814 * (which prints a separator for us), then print a separator.
2816 if (npools > 1 && !verbose)
2817 print_iostat_separator(&cb);
2819 if (verbose)
2820 (void) printf("\n");
2823 * Flush the output so that redirection to a file isn't buffered
2824 * indefinitely.
2826 (void) fflush(stdout);
2828 if (interval == 0)
2829 break;
2831 if (count != 0 && --count == 0)
2832 break;
2834 (void) sleep(interval);
2837 pool_list_free(list);
2839 return (ret);
2842 typedef struct list_cbdata {
2843 boolean_t cb_verbose;
2844 int cb_namewidth;
2845 boolean_t cb_scripted;
2846 zprop_list_t *cb_proplist;
2847 boolean_t cb_literal;
2848 } list_cbdata_t;
2851 * Given a list of columns to display, output appropriate headers for each one.
2853 static void
2854 print_header(list_cbdata_t *cb)
2856 zprop_list_t *pl = cb->cb_proplist;
2857 char headerbuf[ZPOOL_MAXPROPLEN];
2858 const char *header;
2859 boolean_t first = B_TRUE;
2860 boolean_t right_justify;
2861 size_t width = 0;
2863 for (; pl != NULL; pl = pl->pl_next) {
2864 width = pl->pl_width;
2865 if (first && cb->cb_verbose) {
2867 * Reset the width to accommodate the verbose listing
2868 * of devices.
2870 width = cb->cb_namewidth;
2873 if (!first)
2874 (void) printf(" ");
2875 else
2876 first = B_FALSE;
2878 right_justify = B_FALSE;
2879 if (pl->pl_prop != ZPROP_INVAL) {
2880 header = zpool_prop_column_name(pl->pl_prop);
2881 right_justify = zpool_prop_align_right(pl->pl_prop);
2882 } else {
2883 int i;
2885 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2886 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2887 headerbuf[i] = '\0';
2888 header = headerbuf;
2891 if (pl->pl_next == NULL && !right_justify)
2892 (void) printf("%s", header);
2893 else if (right_justify)
2894 (void) printf("%*s", width, header);
2895 else
2896 (void) printf("%-*s", width, header);
2900 (void) printf("\n");
2904 * Given a pool and a list of properties, print out all the properties according
2905 * to the described layout.
2907 static void
2908 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2910 zprop_list_t *pl = cb->cb_proplist;
2911 boolean_t first = B_TRUE;
2912 char property[ZPOOL_MAXPROPLEN];
2913 char *propstr;
2914 boolean_t right_justify;
2915 size_t width;
2917 for (; pl != NULL; pl = pl->pl_next) {
2919 width = pl->pl_width;
2920 if (first && cb->cb_verbose) {
2922 * Reset the width to accommodate the verbose listing
2923 * of devices.
2925 width = cb->cb_namewidth;
2928 if (!first) {
2929 if (cb->cb_scripted)
2930 (void) printf("\t");
2931 else
2932 (void) printf(" ");
2933 } else {
2934 first = B_FALSE;
2937 right_justify = B_FALSE;
2938 if (pl->pl_prop != ZPROP_INVAL) {
2939 if (zpool_get_prop(zhp, pl->pl_prop, property,
2940 sizeof (property), NULL, cb->cb_literal) != 0)
2941 propstr = "-";
2942 else
2943 propstr = property;
2945 right_justify = zpool_prop_align_right(pl->pl_prop);
2946 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2947 zpool_prop_unsupported(pl->pl_user_prop)) &&
2948 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2949 sizeof (property)) == 0) {
2950 propstr = property;
2951 } else {
2952 propstr = "-";
2957 * If this is being called in scripted mode, or if this is the
2958 * last column and it is left-justified, don't include a width
2959 * format specifier.
2961 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2962 (void) printf("%s", propstr);
2963 else if (right_justify)
2964 (void) printf("%*s", width, propstr);
2965 else
2966 (void) printf("%-*s", width, propstr);
2969 (void) printf("\n");
2972 static void
2973 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
2974 boolean_t valid)
2976 char propval[64];
2977 boolean_t fixed;
2978 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2980 switch (prop) {
2981 case ZPOOL_PROP_EXPANDSZ:
2982 if (value == 0)
2983 (void) strlcpy(propval, "-", sizeof (propval));
2984 else
2985 zfs_nicenum(value, propval, sizeof (propval));
2986 break;
2987 case ZPOOL_PROP_FRAGMENTATION:
2988 if (value == ZFS_FRAG_INVALID) {
2989 (void) strlcpy(propval, "-", sizeof (propval));
2990 } else {
2991 (void) snprintf(propval, sizeof (propval), "%llu%%",
2992 value);
2994 break;
2995 case ZPOOL_PROP_CAPACITY:
2996 (void) snprintf(propval, sizeof (propval), "%llu%%", value);
2997 break;
2998 default:
2999 zfs_nicenum(value, propval, sizeof (propval));
3002 if (!valid)
3003 (void) strlcpy(propval, "-", sizeof (propval));
3005 if (scripted)
3006 (void) printf("\t%s", propval);
3007 else
3008 (void) printf(" %*s", width, propval);
3011 void
3012 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3013 list_cbdata_t *cb, int depth)
3015 nvlist_t **child;
3016 vdev_stat_t *vs;
3017 uint_t c, children;
3018 char *vname;
3019 boolean_t scripted = cb->cb_scripted;
3020 uint64_t islog = B_FALSE;
3021 boolean_t haslog = B_FALSE;
3022 char *dashes = "%-*s - - - - - -\n";
3024 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3025 (uint64_t **)&vs, &c) == 0);
3027 if (name != NULL) {
3028 boolean_t toplevel = (vs->vs_space != 0);
3029 uint64_t cap;
3031 if (scripted)
3032 (void) printf("\t%s", name);
3033 else if (strlen(name) + depth > cb->cb_namewidth)
3034 (void) printf("%*s%s", depth, "", name);
3035 else
3036 (void) printf("%*s%s%*s", depth, "", name,
3037 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3040 * Print the properties for the individual vdevs. Some
3041 * properties are only applicable to toplevel vdevs. The
3042 * 'toplevel' boolean value is passed to the print_one_column()
3043 * to indicate that the value is valid.
3045 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3046 toplevel);
3047 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3048 toplevel);
3049 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3050 scripted, toplevel);
3051 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3052 B_TRUE);
3053 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3054 vs->vs_fragmentation, scripted,
3055 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3056 cap = (vs->vs_space == 0) ? 0 :
3057 (vs->vs_alloc * 100 / vs->vs_space);
3058 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3059 (void) printf("\n");
3062 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3063 &child, &children) != 0)
3064 return;
3066 for (c = 0; c < children; c++) {
3067 uint64_t ishole = B_FALSE;
3069 if (nvlist_lookup_uint64(child[c],
3070 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3071 continue;
3073 if (nvlist_lookup_uint64(child[c],
3074 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog) {
3075 haslog = B_TRUE;
3076 continue;
3079 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3080 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3081 free(vname);
3084 if (haslog == B_TRUE) {
3085 /* LINTED E_SEC_PRINTF_VAR_FMT */
3086 (void) printf(dashes, cb->cb_namewidth, "log");
3087 for (c = 0; c < children; c++) {
3088 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3089 &islog) != 0 || !islog)
3090 continue;
3091 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3092 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3093 free(vname);
3097 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3098 &child, &children) == 0 && children > 0) {
3099 /* LINTED E_SEC_PRINTF_VAR_FMT */
3100 (void) printf(dashes, cb->cb_namewidth, "cache");
3101 for (c = 0; c < children; c++) {
3102 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3103 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3104 free(vname);
3108 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
3109 &children) == 0 && children > 0) {
3110 /* LINTED E_SEC_PRINTF_VAR_FMT */
3111 (void) printf(dashes, cb->cb_namewidth, "spare");
3112 for (c = 0; c < children; c++) {
3113 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3114 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3115 free(vname);
3122 * Generic callback function to list a pool.
3125 list_callback(zpool_handle_t *zhp, void *data)
3127 list_cbdata_t *cbp = data;
3128 nvlist_t *config;
3129 nvlist_t *nvroot;
3131 config = zpool_get_config(zhp, NULL);
3133 print_pool(zhp, cbp);
3134 if (!cbp->cb_verbose)
3135 return (0);
3137 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3138 &nvroot) == 0);
3139 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3141 return (0);
3145 * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3147 * -H Scripted mode. Don't display headers, and separate properties
3148 * by a single tab.
3149 * -o List of properties to display. Defaults to
3150 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3151 * "dedupratio,health,altroot"
3152 * -p Diplay values in parsable (exact) format.
3153 * -T Display a timestamp in date(1) or Unix format
3155 * List all pools in the system, whether or not they're healthy. Output space
3156 * statistics for each one, as well as health status summary.
3159 zpool_do_list(int argc, char **argv)
3161 int c;
3162 int ret;
3163 list_cbdata_t cb = { 0 };
3164 static char default_props[] =
3165 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3166 "dedupratio,health,altroot";
3167 char *props = default_props;
3168 unsigned long interval = 0, count = 0;
3169 zpool_list_t *list;
3170 boolean_t first = B_TRUE;
3172 /* check options */
3173 while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) {
3174 switch (c) {
3175 case 'H':
3176 cb.cb_scripted = B_TRUE;
3177 break;
3178 case 'o':
3179 props = optarg;
3180 break;
3181 case 'p':
3182 cb.cb_literal = B_TRUE;
3183 break;
3184 case 'T':
3185 get_timestamp_arg(*optarg);
3186 break;
3187 case 'v':
3188 cb.cb_verbose = B_TRUE;
3189 break;
3190 case ':':
3191 (void) fprintf(stderr, gettext("missing argument for "
3192 "'%c' option\n"), optopt);
3193 usage(B_FALSE);
3194 break;
3195 case '?':
3196 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3197 optopt);
3198 usage(B_FALSE);
3202 argc -= optind;
3203 argv += optind;
3205 get_interval_count(&argc, argv, &interval, &count);
3207 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3208 usage(B_FALSE);
3210 for (;;) {
3211 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
3212 &ret)) == NULL)
3213 return (1);
3215 if (pool_list_count(list) == 0)
3216 break;
3218 cb.cb_namewidth = 0;
3219 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
3221 if (timestamp_fmt != NODATE)
3222 print_timestamp(timestamp_fmt);
3224 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3225 print_header(&cb);
3226 first = B_FALSE;
3228 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3230 if (interval == 0)
3231 break;
3233 if (count != 0 && --count == 0)
3234 break;
3236 pool_list_free(list);
3237 (void) sleep(interval);
3240 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3241 (void) printf(gettext("no pools available\n"));
3242 ret = 0;
3245 pool_list_free(list);
3246 zprop_free_list(cb.cb_proplist);
3247 return (ret);
3250 static int
3251 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3253 boolean_t force = B_FALSE;
3254 int c;
3255 nvlist_t *nvroot;
3256 char *poolname, *old_disk, *new_disk;
3257 zpool_handle_t *zhp;
3258 zpool_boot_label_t boot_type;
3259 uint64_t boot_size;
3260 int ret;
3262 /* check options */
3263 while ((c = getopt(argc, argv, "f")) != -1) {
3264 switch (c) {
3265 case 'f':
3266 force = B_TRUE;
3267 break;
3268 case '?':
3269 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3270 optopt);
3271 usage(B_FALSE);
3275 argc -= optind;
3276 argv += optind;
3278 /* get pool name and check number of arguments */
3279 if (argc < 1) {
3280 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3281 usage(B_FALSE);
3284 poolname = argv[0];
3286 if (argc < 2) {
3287 (void) fprintf(stderr,
3288 gettext("missing <device> specification\n"));
3289 usage(B_FALSE);
3292 old_disk = argv[1];
3294 if (argc < 3) {
3295 if (!replacing) {
3296 (void) fprintf(stderr,
3297 gettext("missing <new_device> specification\n"));
3298 usage(B_FALSE);
3300 new_disk = old_disk;
3301 argc -= 1;
3302 argv += 1;
3303 } else {
3304 new_disk = argv[2];
3305 argc -= 2;
3306 argv += 2;
3309 if (argc > 1) {
3310 (void) fprintf(stderr, gettext("too many arguments\n"));
3311 usage(B_FALSE);
3314 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3315 return (1);
3317 if (zpool_get_config(zhp, NULL) == NULL) {
3318 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3319 poolname);
3320 zpool_close(zhp);
3321 return (1);
3324 if (zpool_is_bootable(zhp))
3325 boot_type = ZPOOL_COPY_BOOT_LABEL;
3326 else
3327 boot_type = ZPOOL_NO_BOOT_LABEL;
3329 boot_size = zpool_get_prop_int(zhp, ZPOOL_PROP_BOOTSIZE, NULL);
3330 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3331 boot_type, boot_size, argc, argv);
3332 if (nvroot == NULL) {
3333 zpool_close(zhp);
3334 return (1);
3337 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3339 nvlist_free(nvroot);
3340 zpool_close(zhp);
3342 return (ret);
3346 * zpool replace [-f] <pool> <device> <new_device>
3348 * -f Force attach, even if <new_device> appears to be in use.
3350 * Replace <device> with <new_device>.
3352 /* ARGSUSED */
3354 zpool_do_replace(int argc, char **argv)
3356 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3360 * zpool attach [-f] <pool> <device> <new_device>
3362 * -f Force attach, even if <new_device> appears to be in use.
3364 * Attach <new_device> to the mirror containing <device>. If <device> is not
3365 * part of a mirror, then <device> will be transformed into a mirror of
3366 * <device> and <new_device>. In either case, <new_device> will begin life
3367 * with a DTL of [0, now], and will immediately begin to resilver itself.
3370 zpool_do_attach(int argc, char **argv)
3372 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3376 * zpool detach [-f] <pool> <device>
3378 * -f Force detach of <device>, even if DTLs argue against it
3379 * (not supported yet)
3381 * Detach a device from a mirror. The operation will be refused if <device>
3382 * is the last device in the mirror, or if the DTLs indicate that this device
3383 * has the only valid copy of some data.
3385 /* ARGSUSED */
3387 zpool_do_detach(int argc, char **argv)
3389 int c;
3390 char *poolname, *path;
3391 zpool_handle_t *zhp;
3392 int ret;
3394 /* check options */
3395 while ((c = getopt(argc, argv, "f")) != -1) {
3396 switch (c) {
3397 case 'f':
3398 case '?':
3399 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3400 optopt);
3401 usage(B_FALSE);
3405 argc -= optind;
3406 argv += optind;
3408 /* get pool name and check number of arguments */
3409 if (argc < 1) {
3410 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3411 usage(B_FALSE);
3414 if (argc < 2) {
3415 (void) fprintf(stderr,
3416 gettext("missing <device> specification\n"));
3417 usage(B_FALSE);
3420 poolname = argv[0];
3421 path = argv[1];
3423 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3424 return (1);
3426 ret = zpool_vdev_detach(zhp, path);
3428 zpool_close(zhp);
3430 return (ret);
3434 * zpool split [-n] [-o prop=val] ...
3435 * [-o mntopt] ...
3436 * [-R altroot] <pool> <newpool> [<device> ...]
3438 * -n Do not split the pool, but display the resulting layout if
3439 * it were to be split.
3440 * -o Set property=value, or set mount options.
3441 * -R Mount the split-off pool under an alternate root.
3443 * Splits the named pool and gives it the new pool name. Devices to be split
3444 * off may be listed, provided that no more than one device is specified
3445 * per top-level vdev mirror. The newly split pool is left in an exported
3446 * state unless -R is specified.
3448 * Restrictions: the top-level of the pool pool must only be made up of
3449 * mirrors; all devices in the pool must be healthy; no device may be
3450 * undergoing a resilvering operation.
3453 zpool_do_split(int argc, char **argv)
3455 char *srcpool, *newpool, *propval;
3456 char *mntopts = NULL;
3457 splitflags_t flags;
3458 int c, ret = 0;
3459 zpool_handle_t *zhp;
3460 nvlist_t *config, *props = NULL;
3462 flags.dryrun = B_FALSE;
3463 flags.import = B_FALSE;
3465 /* check options */
3466 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3467 switch (c) {
3468 case 'R':
3469 flags.import = B_TRUE;
3470 if (add_prop_list(
3471 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3472 &props, B_TRUE) != 0) {
3473 nvlist_free(props);
3474 usage(B_FALSE);
3476 break;
3477 case 'n':
3478 flags.dryrun = B_TRUE;
3479 break;
3480 case 'o':
3481 if ((propval = strchr(optarg, '=')) != NULL) {
3482 *propval = '\0';
3483 propval++;
3484 if (add_prop_list(optarg, propval,
3485 &props, B_TRUE) != 0) {
3486 nvlist_free(props);
3487 usage(B_FALSE);
3489 } else {
3490 mntopts = optarg;
3492 break;
3493 case ':':
3494 (void) fprintf(stderr, gettext("missing argument for "
3495 "'%c' option\n"), optopt);
3496 usage(B_FALSE);
3497 break;
3498 case '?':
3499 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3500 optopt);
3501 usage(B_FALSE);
3502 break;
3506 if (!flags.import && mntopts != NULL) {
3507 (void) fprintf(stderr, gettext("setting mntopts is only "
3508 "valid when importing the pool\n"));
3509 usage(B_FALSE);
3512 argc -= optind;
3513 argv += optind;
3515 if (argc < 1) {
3516 (void) fprintf(stderr, gettext("Missing pool name\n"));
3517 usage(B_FALSE);
3519 if (argc < 2) {
3520 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3521 usage(B_FALSE);
3524 srcpool = argv[0];
3525 newpool = argv[1];
3527 argc -= 2;
3528 argv += 2;
3530 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3531 return (1);
3533 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3534 if (config == NULL) {
3535 ret = 1;
3536 } else {
3537 if (flags.dryrun) {
3538 (void) printf(gettext("would create '%s' with the "
3539 "following layout:\n\n"), newpool);
3540 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3542 nvlist_free(config);
3545 zpool_close(zhp);
3547 if (ret != 0 || flags.dryrun || !flags.import)
3548 return (ret);
3551 * The split was successful. Now we need to open the new
3552 * pool and import it.
3554 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3555 return (1);
3556 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3557 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3558 ret = 1;
3559 (void) fprintf(stderr, gettext("Split was successful, but "
3560 "the datasets could not all be mounted\n"));
3561 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3562 "different altroot\n"), "zpool import");
3564 zpool_close(zhp);
3566 return (ret);
3572 * zpool online <pool> <device> ...
3575 zpool_do_online(int argc, char **argv)
3577 int c, i;
3578 char *poolname;
3579 zpool_handle_t *zhp;
3580 int ret = 0;
3581 vdev_state_t newstate;
3582 int flags = 0;
3584 /* check options */
3585 while ((c = getopt(argc, argv, "et")) != -1) {
3586 switch (c) {
3587 case 'e':
3588 flags |= ZFS_ONLINE_EXPAND;
3589 break;
3590 case 't':
3591 case '?':
3592 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3593 optopt);
3594 usage(B_FALSE);
3598 argc -= optind;
3599 argv += optind;
3601 /* get pool name and check number of arguments */
3602 if (argc < 1) {
3603 (void) fprintf(stderr, gettext("missing pool name\n"));
3604 usage(B_FALSE);
3606 if (argc < 2) {
3607 (void) fprintf(stderr, gettext("missing device name\n"));
3608 usage(B_FALSE);
3611 poolname = argv[0];
3613 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3614 return (1);
3616 for (i = 1; i < argc; i++) {
3617 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3618 if (newstate != VDEV_STATE_HEALTHY) {
3619 (void) printf(gettext("warning: device '%s' "
3620 "onlined, but remains in faulted state\n"),
3621 argv[i]);
3622 if (newstate == VDEV_STATE_FAULTED)
3623 (void) printf(gettext("use 'zpool "
3624 "clear' to restore a faulted "
3625 "device\n"));
3626 else
3627 (void) printf(gettext("use 'zpool "
3628 "replace' to replace devices "
3629 "that are no longer present\n"));
3631 } else {
3632 ret = 1;
3636 zpool_close(zhp);
3638 return (ret);
3642 * zpool offline [-ft] <pool> <device> ...
3644 * -f Force the device into the offline state, even if doing
3645 * so would appear to compromise pool availability.
3646 * (not supported yet)
3648 * -t Only take the device off-line temporarily. The offline
3649 * state will not be persistent across reboots.
3651 /* ARGSUSED */
3653 zpool_do_offline(int argc, char **argv)
3655 int c, i;
3656 char *poolname;
3657 zpool_handle_t *zhp;
3658 int ret = 0;
3659 boolean_t istmp = B_FALSE;
3661 /* check options */
3662 while ((c = getopt(argc, argv, "ft")) != -1) {
3663 switch (c) {
3664 case 't':
3665 istmp = B_TRUE;
3666 break;
3667 case 'f':
3668 case '?':
3669 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3670 optopt);
3671 usage(B_FALSE);
3675 argc -= optind;
3676 argv += optind;
3678 /* get pool name and check number of arguments */
3679 if (argc < 1) {
3680 (void) fprintf(stderr, gettext("missing pool name\n"));
3681 usage(B_FALSE);
3683 if (argc < 2) {
3684 (void) fprintf(stderr, gettext("missing device name\n"));
3685 usage(B_FALSE);
3688 poolname = argv[0];
3690 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3691 return (1);
3693 for (i = 1; i < argc; i++) {
3694 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3695 ret = 1;
3698 zpool_close(zhp);
3700 return (ret);
3704 * zpool clear <pool> [device]
3706 * Clear all errors associated with a pool or a particular device.
3709 zpool_do_clear(int argc, char **argv)
3711 int c;
3712 int ret = 0;
3713 boolean_t dryrun = B_FALSE;
3714 boolean_t do_rewind = B_FALSE;
3715 boolean_t xtreme_rewind = B_FALSE;
3716 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3717 nvlist_t *policy = NULL;
3718 zpool_handle_t *zhp;
3719 char *pool, *device;
3721 /* check options */
3722 while ((c = getopt(argc, argv, "FnX")) != -1) {
3723 switch (c) {
3724 case 'F':
3725 do_rewind = B_TRUE;
3726 break;
3727 case 'n':
3728 dryrun = B_TRUE;
3729 break;
3730 case 'X':
3731 xtreme_rewind = B_TRUE;
3732 break;
3733 case '?':
3734 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3735 optopt);
3736 usage(B_FALSE);
3740 argc -= optind;
3741 argv += optind;
3743 if (argc < 1) {
3744 (void) fprintf(stderr, gettext("missing pool name\n"));
3745 usage(B_FALSE);
3748 if (argc > 2) {
3749 (void) fprintf(stderr, gettext("too many arguments\n"));
3750 usage(B_FALSE);
3753 if ((dryrun || xtreme_rewind) && !do_rewind) {
3754 (void) fprintf(stderr,
3755 gettext("-n or -X only meaningful with -F\n"));
3756 usage(B_FALSE);
3758 if (dryrun)
3759 rewind_policy = ZPOOL_TRY_REWIND;
3760 else if (do_rewind)
3761 rewind_policy = ZPOOL_DO_REWIND;
3762 if (xtreme_rewind)
3763 rewind_policy |= ZPOOL_EXTREME_REWIND;
3765 /* In future, further rewind policy choices can be passed along here */
3766 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3767 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3768 return (1);
3770 pool = argv[0];
3771 device = argc == 2 ? argv[1] : NULL;
3773 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3774 nvlist_free(policy);
3775 return (1);
3778 if (zpool_clear(zhp, device, policy) != 0)
3779 ret = 1;
3781 zpool_close(zhp);
3783 nvlist_free(policy);
3785 return (ret);
3789 * zpool reguid <pool>
3792 zpool_do_reguid(int argc, char **argv)
3794 int c;
3795 char *poolname;
3796 zpool_handle_t *zhp;
3797 int ret = 0;
3799 /* check options */
3800 while ((c = getopt(argc, argv, "")) != -1) {
3801 switch (c) {
3802 case '?':
3803 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3804 optopt);
3805 usage(B_FALSE);
3809 argc -= optind;
3810 argv += optind;
3812 /* get pool name and check number of arguments */
3813 if (argc < 1) {
3814 (void) fprintf(stderr, gettext("missing pool name\n"));
3815 usage(B_FALSE);
3818 if (argc > 1) {
3819 (void) fprintf(stderr, gettext("too many arguments\n"));
3820 usage(B_FALSE);
3823 poolname = argv[0];
3824 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3825 return (1);
3827 ret = zpool_reguid(zhp);
3829 zpool_close(zhp);
3830 return (ret);
3835 * zpool reopen <pool>
3837 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3840 zpool_do_reopen(int argc, char **argv)
3842 int c;
3843 int ret = 0;
3844 zpool_handle_t *zhp;
3845 char *pool;
3847 /* check options */
3848 while ((c = getopt(argc, argv, "")) != -1) {
3849 switch (c) {
3850 case '?':
3851 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3852 optopt);
3853 usage(B_FALSE);
3857 argc--;
3858 argv++;
3860 if (argc < 1) {
3861 (void) fprintf(stderr, gettext("missing pool name\n"));
3862 usage(B_FALSE);
3865 if (argc > 1) {
3866 (void) fprintf(stderr, gettext("too many arguments\n"));
3867 usage(B_FALSE);
3870 pool = argv[0];
3871 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3872 return (1);
3874 ret = zpool_reopen(zhp);
3875 zpool_close(zhp);
3876 return (ret);
3879 typedef struct scrub_cbdata {
3880 int cb_type;
3881 int cb_argc;
3882 char **cb_argv;
3883 pool_scrub_cmd_t cb_scrub_cmd;
3884 } scrub_cbdata_t;
3887 scrub_callback(zpool_handle_t *zhp, void *data)
3889 scrub_cbdata_t *cb = data;
3890 int err;
3893 * Ignore faulted pools.
3895 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3896 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3897 "currently unavailable\n"), zpool_get_name(zhp));
3898 return (1);
3901 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
3903 return (err != 0);
3907 * zpool scrub [-s | -p] <pool> ...
3909 * -s Stop. Stops any in-progress scrub.
3910 * -p Pause. Pause in-progress scrub.
3913 zpool_do_scrub(int argc, char **argv)
3915 int c;
3916 scrub_cbdata_t cb;
3918 cb.cb_type = POOL_SCAN_SCRUB;
3919 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
3921 /* check options */
3922 while ((c = getopt(argc, argv, "sp")) != -1) {
3923 switch (c) {
3924 case 's':
3925 cb.cb_type = POOL_SCAN_NONE;
3926 break;
3927 case 'p':
3928 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
3929 break;
3930 case '?':
3931 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3932 optopt);
3933 usage(B_FALSE);
3937 if (cb.cb_type == POOL_SCAN_NONE &&
3938 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
3939 (void) fprintf(stderr, gettext("invalid option combination: "
3940 "-s and -p are mutually exclusive\n"));
3941 usage(B_FALSE);
3944 cb.cb_argc = argc;
3945 cb.cb_argv = argv;
3946 argc -= optind;
3947 argv += optind;
3949 if (argc < 1) {
3950 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3951 usage(B_FALSE);
3954 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3957 typedef struct status_cbdata {
3958 int cb_count;
3959 boolean_t cb_allpools;
3960 boolean_t cb_verbose;
3961 boolean_t cb_explain;
3962 boolean_t cb_first;
3963 boolean_t cb_dedup_stats;
3964 } status_cbdata_t;
3967 * Print out detailed scrub status.
3969 void
3970 print_scan_status(pool_scan_stat_t *ps)
3972 time_t start, end, pause;
3973 uint64_t elapsed, mins_left, hours_left;
3974 uint64_t pass_exam, examined, total;
3975 uint_t rate;
3976 double fraction_done;
3977 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3979 (void) printf(gettext(" scan: "));
3981 /* If there's never been a scan, there's not much to say. */
3982 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3983 ps->pss_func >= POOL_SCAN_FUNCS) {
3984 (void) printf(gettext("none requested\n"));
3985 return;
3988 start = ps->pss_start_time;
3989 end = ps->pss_end_time;
3990 pause = ps->pss_pass_scrub_pause;
3991 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3993 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3994 ps->pss_func == POOL_SCAN_RESILVER);
3996 * Scan is finished or canceled.
3998 if (ps->pss_state == DSS_FINISHED) {
3999 uint64_t minutes_taken = (end - start) / 60;
4000 char *fmt = NULL;
4002 if (ps->pss_func == POOL_SCAN_SCRUB) {
4003 fmt = gettext("scrub repaired %s in %lluh%um with "
4004 "%llu errors on %s");
4005 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4006 fmt = gettext("resilvered %s in %lluh%um with "
4007 "%llu errors on %s");
4009 /* LINTED */
4010 (void) printf(fmt, processed_buf,
4011 (u_longlong_t)(minutes_taken / 60),
4012 (uint_t)(minutes_taken % 60),
4013 (u_longlong_t)ps->pss_errors,
4014 ctime((time_t *)&end));
4015 return;
4016 } else if (ps->pss_state == DSS_CANCELED) {
4017 if (ps->pss_func == POOL_SCAN_SCRUB) {
4018 (void) printf(gettext("scrub canceled on %s"),
4019 ctime(&end));
4020 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4021 (void) printf(gettext("resilver canceled on %s"),
4022 ctime(&end));
4024 return;
4027 assert(ps->pss_state == DSS_SCANNING);
4030 * Scan is in progress.
4032 if (ps->pss_func == POOL_SCAN_SCRUB) {
4033 if (pause == 0) {
4034 (void) printf(gettext("scrub in progress since %s"),
4035 ctime(&start));
4036 } else {
4037 char buf[32];
4038 struct tm *p = localtime(&pause);
4039 (void) strftime(buf, sizeof (buf), "%a %b %e %T %Y", p);
4040 (void) printf(gettext("scrub paused since %s\n"), buf);
4041 (void) printf(gettext("\tscrub started on %s"),
4042 ctime(&start));
4044 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4045 (void) printf(gettext("resilver in progress since %s"),
4046 ctime(&start));
4049 examined = ps->pss_examined ? ps->pss_examined : 1;
4050 total = ps->pss_to_examine;
4051 fraction_done = (double)examined / total;
4053 /* elapsed time for this pass */
4054 elapsed = time(NULL) - ps->pss_pass_start;
4055 elapsed -= ps->pss_pass_scrub_spent_paused;
4056 elapsed = elapsed ? elapsed : 1;
4057 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4058 rate = pass_exam / elapsed;
4059 rate = rate ? rate : 1;
4060 mins_left = ((total - examined) / rate) / 60;
4061 hours_left = mins_left / 60;
4063 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4064 zfs_nicenum(total, total_buf, sizeof (total_buf));
4067 * do not print estimated time if hours_left is more than 30 days
4068 * or we have a paused scrub
4070 if (pause == 0) {
4071 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4072 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
4073 examined_buf, total_buf, rate_buf);
4074 if (hours_left < (30 * 24)) {
4075 (void) printf(gettext(", %lluh%um to go\n"),
4076 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4077 } else {
4078 (void) printf(gettext(
4079 ", (scan is slow, no estimated time)\n"));
4081 } else {
4082 (void) printf(gettext("\t%s scanned out of %s\n"),
4083 examined_buf, total_buf);
4086 if (ps->pss_func == POOL_SCAN_RESILVER) {
4087 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4088 processed_buf, 100 * fraction_done);
4089 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4090 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4091 processed_buf, 100 * fraction_done);
4095 static void
4096 print_error_log(zpool_handle_t *zhp)
4098 nvlist_t *nverrlist = NULL;
4099 nvpair_t *elem;
4100 char *pathname;
4101 size_t len = MAXPATHLEN * 2;
4103 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4104 (void) printf("errors: List of errors unavailable "
4105 "(insufficient privileges)\n");
4106 return;
4109 (void) printf("errors: Permanent errors have been "
4110 "detected in the following files:\n\n");
4112 pathname = safe_malloc(len);
4113 elem = NULL;
4114 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4115 nvlist_t *nv;
4116 uint64_t dsobj, obj;
4118 verify(nvpair_value_nvlist(elem, &nv) == 0);
4119 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4120 &dsobj) == 0);
4121 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4122 &obj) == 0);
4123 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4124 (void) printf("%7s %s\n", "", pathname);
4126 free(pathname);
4127 nvlist_free(nverrlist);
4130 static void
4131 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4132 int namewidth)
4134 uint_t i;
4135 char *name;
4137 if (nspares == 0)
4138 return;
4140 (void) printf(gettext("\tspares\n"));
4142 for (i = 0; i < nspares; i++) {
4143 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4144 print_status_config(zhp, name, spares[i],
4145 namewidth, 2, B_TRUE);
4146 free(name);
4150 static void
4151 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4152 int namewidth)
4154 uint_t i;
4155 char *name;
4157 if (nl2cache == 0)
4158 return;
4160 (void) printf(gettext("\tcache\n"));
4162 for (i = 0; i < nl2cache; i++) {
4163 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4164 print_status_config(zhp, name, l2cache[i],
4165 namewidth, 2, B_FALSE);
4166 free(name);
4170 static void
4171 print_dedup_stats(nvlist_t *config)
4173 ddt_histogram_t *ddh;
4174 ddt_stat_t *dds;
4175 ddt_object_t *ddo;
4176 uint_t c;
4179 * If the pool was faulted then we may not have been able to
4180 * obtain the config. Otherwise, if we have anything in the dedup
4181 * table continue processing the stats.
4183 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4184 (uint64_t **)&ddo, &c) != 0)
4185 return;
4187 (void) printf("\n");
4188 (void) printf(gettext(" dedup: "));
4189 if (ddo->ddo_count == 0) {
4190 (void) printf(gettext("no DDT entries\n"));
4191 return;
4194 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4195 (u_longlong_t)ddo->ddo_count,
4196 (u_longlong_t)ddo->ddo_dspace,
4197 (u_longlong_t)ddo->ddo_mspace);
4199 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4200 (uint64_t **)&dds, &c) == 0);
4201 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4202 (uint64_t **)&ddh, &c) == 0);
4203 zpool_dump_ddt(dds, ddh);
4207 * Display a summary of pool status. Displays a summary such as:
4209 * pool: tank
4210 * status: DEGRADED
4211 * reason: One or more devices ...
4212 * see: http://illumos.org/msg/ZFS-xxxx-01
4213 * config:
4214 * mirror DEGRADED
4215 * c1t0d0 OK
4216 * c2t0d0 UNAVAIL
4218 * When given the '-v' option, we print out the complete config. If the '-e'
4219 * option is specified, then we print out error rate information as well.
4222 status_callback(zpool_handle_t *zhp, void *data)
4224 status_cbdata_t *cbp = data;
4225 nvlist_t *config, *nvroot;
4226 char *msgid;
4227 int reason;
4228 const char *health;
4229 uint_t c;
4230 vdev_stat_t *vs;
4232 config = zpool_get_config(zhp, NULL);
4233 reason = zpool_get_status(zhp, &msgid);
4235 cbp->cb_count++;
4238 * If we were given 'zpool status -x', only report those pools with
4239 * problems.
4241 if (cbp->cb_explain &&
4242 (reason == ZPOOL_STATUS_OK ||
4243 reason == ZPOOL_STATUS_VERSION_OLDER ||
4244 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4245 if (!cbp->cb_allpools) {
4246 (void) printf(gettext("pool '%s' is healthy\n"),
4247 zpool_get_name(zhp));
4248 if (cbp->cb_first)
4249 cbp->cb_first = B_FALSE;
4251 return (0);
4254 if (cbp->cb_first)
4255 cbp->cb_first = B_FALSE;
4256 else
4257 (void) printf("\n");
4259 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4260 &nvroot) == 0);
4261 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4262 (uint64_t **)&vs, &c) == 0);
4263 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4265 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4266 (void) printf(gettext(" state: %s\n"), health);
4268 switch (reason) {
4269 case ZPOOL_STATUS_MISSING_DEV_R:
4270 (void) printf(gettext("status: One or more devices could not "
4271 "be opened. Sufficient replicas exist for\n\tthe pool to "
4272 "continue functioning in a degraded state.\n"));
4273 (void) printf(gettext("action: Attach the missing device and "
4274 "online it using 'zpool online'.\n"));
4275 break;
4277 case ZPOOL_STATUS_MISSING_DEV_NR:
4278 (void) printf(gettext("status: One or more devices could not "
4279 "be opened. There are insufficient\n\treplicas for the "
4280 "pool to continue functioning.\n"));
4281 (void) printf(gettext("action: Attach the missing device and "
4282 "online it using 'zpool online'.\n"));
4283 break;
4285 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4286 (void) printf(gettext("status: One or more devices could not "
4287 "be used because the label is missing or\n\tinvalid. "
4288 "Sufficient replicas exist for the pool to continue\n\t"
4289 "functioning in a degraded state.\n"));
4290 (void) printf(gettext("action: Replace the device using "
4291 "'zpool replace'.\n"));
4292 break;
4294 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4295 (void) printf(gettext("status: One or more devices could not "
4296 "be used because the label is missing \n\tor invalid. "
4297 "There are insufficient replicas for the pool to "
4298 "continue\n\tfunctioning.\n"));
4299 zpool_explain_recover(zpool_get_handle(zhp),
4300 zpool_get_name(zhp), reason, config);
4301 break;
4303 case ZPOOL_STATUS_FAILING_DEV:
4304 (void) printf(gettext("status: One or more devices has "
4305 "experienced an unrecoverable error. An\n\tattempt was "
4306 "made to correct the error. Applications are "
4307 "unaffected.\n"));
4308 (void) printf(gettext("action: Determine if the device needs "
4309 "to be replaced, and clear the errors\n\tusing "
4310 "'zpool clear' or replace the device with 'zpool "
4311 "replace'.\n"));
4312 break;
4314 case ZPOOL_STATUS_OFFLINE_DEV:
4315 (void) printf(gettext("status: One or more devices has "
4316 "been taken offline by the administrator.\n\tSufficient "
4317 "replicas exist for the pool to continue functioning in "
4318 "a\n\tdegraded state.\n"));
4319 (void) printf(gettext("action: Online the device using "
4320 "'zpool online' or replace the device with\n\t'zpool "
4321 "replace'.\n"));
4322 break;
4324 case ZPOOL_STATUS_REMOVED_DEV:
4325 (void) printf(gettext("status: One or more devices has "
4326 "been removed by the administrator.\n\tSufficient "
4327 "replicas exist for the pool to continue functioning in "
4328 "a\n\tdegraded state.\n"));
4329 (void) printf(gettext("action: Online the device using "
4330 "'zpool online' or replace the device with\n\t'zpool "
4331 "replace'.\n"));
4332 break;
4334 case ZPOOL_STATUS_RESILVERING:
4335 (void) printf(gettext("status: One or more devices is "
4336 "currently being resilvered. The pool will\n\tcontinue "
4337 "to function, possibly in a degraded state.\n"));
4338 (void) printf(gettext("action: Wait for the resilver to "
4339 "complete.\n"));
4340 break;
4342 case ZPOOL_STATUS_CORRUPT_DATA:
4343 (void) printf(gettext("status: One or more devices has "
4344 "experienced an error resulting in data\n\tcorruption. "
4345 "Applications may be affected.\n"));
4346 (void) printf(gettext("action: Restore the file in question "
4347 "if possible. Otherwise restore the\n\tentire pool from "
4348 "backup.\n"));
4349 break;
4351 case ZPOOL_STATUS_CORRUPT_POOL:
4352 (void) printf(gettext("status: The pool metadata is corrupted "
4353 "and the pool cannot be opened.\n"));
4354 zpool_explain_recover(zpool_get_handle(zhp),
4355 zpool_get_name(zhp), reason, config);
4356 break;
4358 case ZPOOL_STATUS_VERSION_OLDER:
4359 (void) printf(gettext("status: The pool is formatted using a "
4360 "legacy on-disk format. The pool can\n\tstill be used, "
4361 "but some features are unavailable.\n"));
4362 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4363 "upgrade'. Once this is done, the\n\tpool will no longer "
4364 "be accessible on software that does not support feature\n"
4365 "\tflags.\n"));
4366 break;
4368 case ZPOOL_STATUS_VERSION_NEWER:
4369 (void) printf(gettext("status: The pool has been upgraded to a "
4370 "newer, incompatible on-disk version.\n\tThe pool cannot "
4371 "be accessed on this system.\n"));
4372 (void) printf(gettext("action: Access the pool from a system "
4373 "running more recent software, or\n\trestore the pool from "
4374 "backup.\n"));
4375 break;
4377 case ZPOOL_STATUS_FEAT_DISABLED:
4378 (void) printf(gettext("status: Some supported features are not "
4379 "enabled on the pool. The pool can\n\tstill be used, but "
4380 "some features are unavailable.\n"));
4381 (void) printf(gettext("action: Enable all features using "
4382 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4383 "longer be accessible by software that does not support\n\t"
4384 "the features. See zpool-features(5) for details.\n"));
4385 break;
4387 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4388 (void) printf(gettext("status: The pool cannot be accessed on "
4389 "this system because it uses the\n\tfollowing feature(s) "
4390 "not supported on this system:\n"));
4391 zpool_print_unsup_feat(config);
4392 (void) printf("\n");
4393 (void) printf(gettext("action: Access the pool from a system "
4394 "that supports the required feature(s),\n\tor restore the "
4395 "pool from backup.\n"));
4396 break;
4398 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4399 (void) printf(gettext("status: The pool can only be accessed "
4400 "in read-only mode on this system. It\n\tcannot be "
4401 "accessed in read-write mode because it uses the "
4402 "following\n\tfeature(s) not supported on this system:\n"));
4403 zpool_print_unsup_feat(config);
4404 (void) printf("\n");
4405 (void) printf(gettext("action: The pool cannot be accessed in "
4406 "read-write mode. Import the pool with\n"
4407 "\t\"-o readonly=on\", access the pool from a system that "
4408 "supports the\n\trequired feature(s), or restore the "
4409 "pool from backup.\n"));
4410 break;
4412 case ZPOOL_STATUS_FAULTED_DEV_R:
4413 (void) printf(gettext("status: One or more devices are "
4414 "faulted in response to persistent errors.\n\tSufficient "
4415 "replicas exist for the pool to continue functioning "
4416 "in a\n\tdegraded state.\n"));
4417 (void) printf(gettext("action: Replace the faulted device, "
4418 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4419 break;
4421 case ZPOOL_STATUS_FAULTED_DEV_NR:
4422 (void) printf(gettext("status: One or more devices are "
4423 "faulted in response to persistent errors. There are "
4424 "insufficient replicas for the pool to\n\tcontinue "
4425 "functioning.\n"));
4426 (void) printf(gettext("action: Destroy and re-create the pool "
4427 "from a backup source. Manually marking the device\n"
4428 "\trepaired using 'zpool clear' may allow some data "
4429 "to be recovered.\n"));
4430 break;
4432 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4433 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4434 (void) printf(gettext("status: One or more devices are "
4435 "faulted in response to IO failures.\n"));
4436 (void) printf(gettext("action: Make sure the affected devices "
4437 "are connected, then run 'zpool clear'.\n"));
4438 break;
4440 case ZPOOL_STATUS_BAD_LOG:
4441 (void) printf(gettext("status: An intent log record "
4442 "could not be read.\n"
4443 "\tWaiting for adminstrator intervention to fix the "
4444 "faulted pool.\n"));
4445 (void) printf(gettext("action: Either restore the affected "
4446 "device(s) and run 'zpool online',\n"
4447 "\tor ignore the intent log records by running "
4448 "'zpool clear'.\n"));
4449 break;
4451 default:
4453 * The remaining errors can't actually be generated, yet.
4455 assert(reason == ZPOOL_STATUS_OK);
4458 if (msgid != NULL)
4459 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4460 msgid);
4462 if (config != NULL) {
4463 int namewidth;
4464 uint64_t nerr;
4465 nvlist_t **spares, **l2cache;
4466 uint_t nspares, nl2cache;
4467 pool_scan_stat_t *ps = NULL;
4469 (void) nvlist_lookup_uint64_array(nvroot,
4470 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4471 print_scan_status(ps);
4473 namewidth = max_width(zhp, nvroot, 0, 0);
4474 if (namewidth < 10)
4475 namewidth = 10;
4477 (void) printf(gettext("config:\n\n"));
4478 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4479 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4480 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4481 namewidth, 0, B_FALSE);
4483 if (num_logs(nvroot) > 0)
4484 print_logs(zhp, nvroot, namewidth, B_TRUE);
4485 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4486 &l2cache, &nl2cache) == 0)
4487 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4489 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4490 &spares, &nspares) == 0)
4491 print_spares(zhp, spares, nspares, namewidth);
4493 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4494 &nerr) == 0) {
4495 nvlist_t *nverrlist = NULL;
4498 * If the approximate error count is small, get a
4499 * precise count by fetching the entire log and
4500 * uniquifying the results.
4502 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4503 zpool_get_errlog(zhp, &nverrlist) == 0) {
4504 nvpair_t *elem;
4506 elem = NULL;
4507 nerr = 0;
4508 while ((elem = nvlist_next_nvpair(nverrlist,
4509 elem)) != NULL) {
4510 nerr++;
4513 nvlist_free(nverrlist);
4515 (void) printf("\n");
4517 if (nerr == 0)
4518 (void) printf(gettext("errors: No known data "
4519 "errors\n"));
4520 else if (!cbp->cb_verbose)
4521 (void) printf(gettext("errors: %llu data "
4522 "errors, use '-v' for a list\n"),
4523 (u_longlong_t)nerr);
4524 else
4525 print_error_log(zhp);
4528 if (cbp->cb_dedup_stats)
4529 print_dedup_stats(config);
4530 } else {
4531 (void) printf(gettext("config: The configuration cannot be "
4532 "determined.\n"));
4535 return (0);
4539 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4541 * -v Display complete error logs
4542 * -x Display only pools with potential problems
4543 * -D Display dedup status (undocumented)
4544 * -T Display a timestamp in date(1) or Unix format
4546 * Describes the health status of all pools or some subset.
4549 zpool_do_status(int argc, char **argv)
4551 int c;
4552 int ret;
4553 unsigned long interval = 0, count = 0;
4554 status_cbdata_t cb = { 0 };
4556 /* check options */
4557 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4558 switch (c) {
4559 case 'v':
4560 cb.cb_verbose = B_TRUE;
4561 break;
4562 case 'x':
4563 cb.cb_explain = B_TRUE;
4564 break;
4565 case 'D':
4566 cb.cb_dedup_stats = B_TRUE;
4567 break;
4568 case 'T':
4569 get_timestamp_arg(*optarg);
4570 break;
4571 case '?':
4572 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4573 optopt);
4574 usage(B_FALSE);
4578 argc -= optind;
4579 argv += optind;
4581 get_interval_count(&argc, argv, &interval, &count);
4583 if (argc == 0)
4584 cb.cb_allpools = B_TRUE;
4586 cb.cb_first = B_TRUE;
4588 for (;;) {
4589 if (timestamp_fmt != NODATE)
4590 print_timestamp(timestamp_fmt);
4592 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4593 status_callback, &cb);
4595 if (argc == 0 && cb.cb_count == 0)
4596 (void) printf(gettext("no pools available\n"));
4597 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4598 (void) printf(gettext("all pools are healthy\n"));
4600 if (ret != 0)
4601 return (ret);
4603 if (interval == 0)
4604 break;
4606 if (count != 0 && --count == 0)
4607 break;
4609 (void) sleep(interval);
4612 return (0);
4615 typedef struct upgrade_cbdata {
4616 int cb_first;
4617 int cb_argc;
4618 uint64_t cb_version;
4619 char **cb_argv;
4620 } upgrade_cbdata_t;
4622 static int
4623 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4625 int ret;
4626 nvlist_t *config;
4627 uint64_t oldversion;
4629 config = zpool_get_config(zhp, NULL);
4630 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4631 &oldversion) == 0);
4633 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4634 assert(oldversion < version);
4636 ret = zpool_upgrade(zhp, version);
4637 if (ret != 0)
4638 return (ret);
4640 if (version >= SPA_VERSION_FEATURES) {
4641 (void) printf(gettext("Successfully upgraded "
4642 "'%s' from version %llu to feature flags.\n"),
4643 zpool_get_name(zhp), oldversion);
4644 } else {
4645 (void) printf(gettext("Successfully upgraded "
4646 "'%s' from version %llu to version %llu.\n"),
4647 zpool_get_name(zhp), oldversion, version);
4650 return (0);
4653 static int
4654 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4656 int i, ret, count;
4657 boolean_t firstff = B_TRUE;
4658 nvlist_t *enabled = zpool_get_features(zhp);
4660 count = 0;
4661 for (i = 0; i < SPA_FEATURES; i++) {
4662 const char *fname = spa_feature_table[i].fi_uname;
4663 const char *fguid = spa_feature_table[i].fi_guid;
4664 if (!nvlist_exists(enabled, fguid)) {
4665 char *propname;
4666 verify(-1 != asprintf(&propname, "feature@%s", fname));
4667 ret = zpool_set_prop(zhp, propname,
4668 ZFS_FEATURE_ENABLED);
4669 if (ret != 0) {
4670 free(propname);
4671 return (ret);
4673 count++;
4675 if (firstff) {
4676 (void) printf(gettext("Enabled the "
4677 "following features on '%s':\n"),
4678 zpool_get_name(zhp));
4679 firstff = B_FALSE;
4681 (void) printf(gettext(" %s\n"), fname);
4682 free(propname);
4686 if (countp != NULL)
4687 *countp = count;
4688 return (0);
4691 static int
4692 upgrade_cb(zpool_handle_t *zhp, void *arg)
4694 upgrade_cbdata_t *cbp = arg;
4695 nvlist_t *config;
4696 uint64_t version;
4697 boolean_t printnl = B_FALSE;
4698 int ret;
4700 config = zpool_get_config(zhp, NULL);
4701 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4702 &version) == 0);
4704 assert(SPA_VERSION_IS_SUPPORTED(version));
4706 if (version < cbp->cb_version) {
4707 cbp->cb_first = B_FALSE;
4708 ret = upgrade_version(zhp, cbp->cb_version);
4709 if (ret != 0)
4710 return (ret);
4711 printnl = B_TRUE;
4714 * If they did "zpool upgrade -a", then we could
4715 * be doing ioctls to different pools. We need
4716 * to log this history once to each pool, and bypass
4717 * the normal history logging that happens in main().
4719 (void) zpool_log_history(g_zfs, history_str);
4720 log_history = B_FALSE;
4723 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4724 int count;
4725 ret = upgrade_enable_all(zhp, &count);
4726 if (ret != 0)
4727 return (ret);
4729 if (count > 0) {
4730 cbp->cb_first = B_FALSE;
4731 printnl = B_TRUE;
4735 if (printnl) {
4736 (void) printf(gettext("\n"));
4739 return (0);
4742 static int
4743 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4745 upgrade_cbdata_t *cbp = arg;
4746 nvlist_t *config;
4747 uint64_t version;
4749 config = zpool_get_config(zhp, NULL);
4750 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4751 &version) == 0);
4753 assert(SPA_VERSION_IS_SUPPORTED(version));
4755 if (version < SPA_VERSION_FEATURES) {
4756 if (cbp->cb_first) {
4757 (void) printf(gettext("The following pools are "
4758 "formatted with legacy version numbers and can\n"
4759 "be upgraded to use feature flags. After "
4760 "being upgraded, these pools\nwill no "
4761 "longer be accessible by software that does not "
4762 "support feature\nflags.\n\n"));
4763 (void) printf(gettext("VER POOL\n"));
4764 (void) printf(gettext("--- ------------\n"));
4765 cbp->cb_first = B_FALSE;
4768 (void) printf("%2llu %s\n", (u_longlong_t)version,
4769 zpool_get_name(zhp));
4772 return (0);
4775 static int
4776 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4778 upgrade_cbdata_t *cbp = arg;
4779 nvlist_t *config;
4780 uint64_t version;
4782 config = zpool_get_config(zhp, NULL);
4783 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4784 &version) == 0);
4786 if (version >= SPA_VERSION_FEATURES) {
4787 int i;
4788 boolean_t poolfirst = B_TRUE;
4789 nvlist_t *enabled = zpool_get_features(zhp);
4791 for (i = 0; i < SPA_FEATURES; i++) {
4792 const char *fguid = spa_feature_table[i].fi_guid;
4793 const char *fname = spa_feature_table[i].fi_uname;
4794 if (!nvlist_exists(enabled, fguid)) {
4795 if (cbp->cb_first) {
4796 (void) printf(gettext("\nSome "
4797 "supported features are not "
4798 "enabled on the following pools. "
4799 "Once a\nfeature is enabled the "
4800 "pool may become incompatible with "
4801 "software\nthat does not support "
4802 "the feature. See "
4803 "zpool-features(5) for "
4804 "details.\n\n"));
4805 (void) printf(gettext("POOL "
4806 "FEATURE\n"));
4807 (void) printf(gettext("------"
4808 "---------\n"));
4809 cbp->cb_first = B_FALSE;
4812 if (poolfirst) {
4813 (void) printf(gettext("%s\n"),
4814 zpool_get_name(zhp));
4815 poolfirst = B_FALSE;
4818 (void) printf(gettext(" %s\n"), fname);
4823 return (0);
4826 /* ARGSUSED */
4827 static int
4828 upgrade_one(zpool_handle_t *zhp, void *data)
4830 boolean_t printnl = B_FALSE;
4831 upgrade_cbdata_t *cbp = data;
4832 uint64_t cur_version;
4833 int ret;
4835 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4836 (void) printf(gettext("'log' is now a reserved word\n"
4837 "Pool 'log' must be renamed using export and import"
4838 " to upgrade.\n"));
4839 return (1);
4842 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4843 if (cur_version > cbp->cb_version) {
4844 (void) printf(gettext("Pool '%s' is already formatted "
4845 "using more current version '%llu'.\n\n"),
4846 zpool_get_name(zhp), cur_version);
4847 return (0);
4850 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4851 (void) printf(gettext("Pool '%s' is already formatted "
4852 "using version %llu.\n\n"), zpool_get_name(zhp),
4853 cbp->cb_version);
4854 return (0);
4857 if (cur_version != cbp->cb_version) {
4858 printnl = B_TRUE;
4859 ret = upgrade_version(zhp, cbp->cb_version);
4860 if (ret != 0)
4861 return (ret);
4864 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4865 int count = 0;
4866 ret = upgrade_enable_all(zhp, &count);
4867 if (ret != 0)
4868 return (ret);
4870 if (count != 0) {
4871 printnl = B_TRUE;
4872 } else if (cur_version == SPA_VERSION) {
4873 (void) printf(gettext("Pool '%s' already has all "
4874 "supported features enabled.\n"),
4875 zpool_get_name(zhp));
4879 if (printnl) {
4880 (void) printf(gettext("\n"));
4883 return (0);
4887 * zpool upgrade
4888 * zpool upgrade -v
4889 * zpool upgrade [-V version] <-a | pool ...>
4891 * With no arguments, display downrev'd ZFS pool available for upgrade.
4892 * Individual pools can be upgraded by specifying the pool, and '-a' will
4893 * upgrade all pools.
4896 zpool_do_upgrade(int argc, char **argv)
4898 int c;
4899 upgrade_cbdata_t cb = { 0 };
4900 int ret = 0;
4901 boolean_t showversions = B_FALSE;
4902 boolean_t upgradeall = B_FALSE;
4903 char *end;
4906 /* check options */
4907 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4908 switch (c) {
4909 case 'a':
4910 upgradeall = B_TRUE;
4911 break;
4912 case 'v':
4913 showversions = B_TRUE;
4914 break;
4915 case 'V':
4916 cb.cb_version = strtoll(optarg, &end, 10);
4917 if (*end != '\0' ||
4918 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4919 (void) fprintf(stderr,
4920 gettext("invalid version '%s'\n"), optarg);
4921 usage(B_FALSE);
4923 break;
4924 case ':':
4925 (void) fprintf(stderr, gettext("missing argument for "
4926 "'%c' option\n"), optopt);
4927 usage(B_FALSE);
4928 break;
4929 case '?':
4930 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4931 optopt);
4932 usage(B_FALSE);
4936 cb.cb_argc = argc;
4937 cb.cb_argv = argv;
4938 argc -= optind;
4939 argv += optind;
4941 if (cb.cb_version == 0) {
4942 cb.cb_version = SPA_VERSION;
4943 } else if (!upgradeall && argc == 0) {
4944 (void) fprintf(stderr, gettext("-V option is "
4945 "incompatible with other arguments\n"));
4946 usage(B_FALSE);
4949 if (showversions) {
4950 if (upgradeall || argc != 0) {
4951 (void) fprintf(stderr, gettext("-v option is "
4952 "incompatible with other arguments\n"));
4953 usage(B_FALSE);
4955 } else if (upgradeall) {
4956 if (argc != 0) {
4957 (void) fprintf(stderr, gettext("-a option should not "
4958 "be used along with a pool name\n"));
4959 usage(B_FALSE);
4963 (void) printf(gettext("This system supports ZFS pool feature "
4964 "flags.\n\n"));
4965 if (showversions) {
4966 int i;
4968 (void) printf(gettext("The following features are "
4969 "supported:\n\n"));
4970 (void) printf(gettext("FEAT DESCRIPTION\n"));
4971 (void) printf("----------------------------------------------"
4972 "---------------\n");
4973 for (i = 0; i < SPA_FEATURES; i++) {
4974 zfeature_info_t *fi = &spa_feature_table[i];
4975 const char *ro =
4976 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
4977 " (read-only compatible)" : "";
4979 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4980 (void) printf(" %s\n", fi->fi_desc);
4982 (void) printf("\n");
4984 (void) printf(gettext("The following legacy versions are also "
4985 "supported:\n\n"));
4986 (void) printf(gettext("VER DESCRIPTION\n"));
4987 (void) printf("--- -----------------------------------------"
4988 "---------------\n");
4989 (void) printf(gettext(" 1 Initial ZFS version\n"));
4990 (void) printf(gettext(" 2 Ditto blocks "
4991 "(replicated metadata)\n"));
4992 (void) printf(gettext(" 3 Hot spares and double parity "
4993 "RAID-Z\n"));
4994 (void) printf(gettext(" 4 zpool history\n"));
4995 (void) printf(gettext(" 5 Compression using the gzip "
4996 "algorithm\n"));
4997 (void) printf(gettext(" 6 bootfs pool property\n"));
4998 (void) printf(gettext(" 7 Separate intent log devices\n"));
4999 (void) printf(gettext(" 8 Delegated administration\n"));
5000 (void) printf(gettext(" 9 refquota and refreservation "
5001 "properties\n"));
5002 (void) printf(gettext(" 10 Cache devices\n"));
5003 (void) printf(gettext(" 11 Improved scrub performance\n"));
5004 (void) printf(gettext(" 12 Snapshot properties\n"));
5005 (void) printf(gettext(" 13 snapused property\n"));
5006 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5007 (void) printf(gettext(" 15 user/group space accounting\n"));
5008 (void) printf(gettext(" 16 stmf property support\n"));
5009 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5010 (void) printf(gettext(" 18 Snapshot user holds\n"));
5011 (void) printf(gettext(" 19 Log device removal\n"));
5012 (void) printf(gettext(" 20 Compression using zle "
5013 "(zero-length encoding)\n"));
5014 (void) printf(gettext(" 21 Deduplication\n"));
5015 (void) printf(gettext(" 22 Received properties\n"));
5016 (void) printf(gettext(" 23 Slim ZIL\n"));
5017 (void) printf(gettext(" 24 System attributes\n"));
5018 (void) printf(gettext(" 25 Improved scrub stats\n"));
5019 (void) printf(gettext(" 26 Improved snapshot deletion "
5020 "performance\n"));
5021 (void) printf(gettext(" 27 Improved snapshot creation "
5022 "performance\n"));
5023 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5024 (void) printf(gettext("\nFor more information on a particular "
5025 "version, including supported releases,\n"));
5026 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5027 } else if (argc == 0 && upgradeall) {
5028 cb.cb_first = B_TRUE;
5029 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5030 if (ret == 0 && cb.cb_first) {
5031 if (cb.cb_version == SPA_VERSION) {
5032 (void) printf(gettext("All pools are already "
5033 "formatted using feature flags.\n\n"));
5034 (void) printf(gettext("Every feature flags "
5035 "pool already has all supported features "
5036 "enabled.\n"));
5037 } else {
5038 (void) printf(gettext("All pools are already "
5039 "formatted with version %llu or higher.\n"),
5040 cb.cb_version);
5043 } else if (argc == 0) {
5044 cb.cb_first = B_TRUE;
5045 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5046 assert(ret == 0);
5048 if (cb.cb_first) {
5049 (void) printf(gettext("All pools are formatted "
5050 "using feature flags.\n\n"));
5051 } else {
5052 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5053 "for a list of available legacy versions.\n"));
5056 cb.cb_first = B_TRUE;
5057 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5058 assert(ret == 0);
5060 if (cb.cb_first) {
5061 (void) printf(gettext("Every feature flags pool has "
5062 "all supported features enabled.\n"));
5063 } else {
5064 (void) printf(gettext("\n"));
5066 } else {
5067 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5068 upgrade_one, &cb);
5071 return (ret);
5074 typedef struct hist_cbdata {
5075 boolean_t first;
5076 boolean_t longfmt;
5077 boolean_t internal;
5078 } hist_cbdata_t;
5081 * Print out the command history for a specific pool.
5083 static int
5084 get_history_one(zpool_handle_t *zhp, void *data)
5086 nvlist_t *nvhis;
5087 nvlist_t **records;
5088 uint_t numrecords;
5089 int ret, i;
5090 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5092 cb->first = B_FALSE;
5094 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5096 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5097 return (ret);
5099 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5100 &records, &numrecords) == 0);
5101 for (i = 0; i < numrecords; i++) {
5102 nvlist_t *rec = records[i];
5103 char tbuf[30] = "";
5105 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5106 time_t tsec;
5107 struct tm t;
5109 tsec = fnvlist_lookup_uint64(records[i],
5110 ZPOOL_HIST_TIME);
5111 (void) localtime_r(&tsec, &t);
5112 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5115 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5116 (void) printf("%s %s", tbuf,
5117 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5118 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5119 int ievent =
5120 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5121 if (!cb->internal)
5122 continue;
5123 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5124 (void) printf("%s unrecognized record:\n",
5125 tbuf);
5126 dump_nvlist(rec, 4);
5127 continue;
5129 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5130 zfs_history_event_names[ievent],
5131 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5132 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5133 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5134 if (!cb->internal)
5135 continue;
5136 (void) printf("%s [txg:%lld] %s", tbuf,
5137 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5138 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5139 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5140 (void) printf(" %s (%llu)",
5141 fnvlist_lookup_string(rec,
5142 ZPOOL_HIST_DSNAME),
5143 fnvlist_lookup_uint64(rec,
5144 ZPOOL_HIST_DSID));
5146 (void) printf(" %s", fnvlist_lookup_string(rec,
5147 ZPOOL_HIST_INT_STR));
5148 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5149 if (!cb->internal)
5150 continue;
5151 (void) printf("%s ioctl %s\n", tbuf,
5152 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5153 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5154 (void) printf(" input:\n");
5155 dump_nvlist(fnvlist_lookup_nvlist(rec,
5156 ZPOOL_HIST_INPUT_NVL), 8);
5158 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5159 (void) printf(" output:\n");
5160 dump_nvlist(fnvlist_lookup_nvlist(rec,
5161 ZPOOL_HIST_OUTPUT_NVL), 8);
5163 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
5164 (void) printf(" errno: %lld\n",
5165 fnvlist_lookup_int64(rec,
5166 ZPOOL_HIST_ERRNO));
5168 } else {
5169 if (!cb->internal)
5170 continue;
5171 (void) printf("%s unrecognized record:\n", tbuf);
5172 dump_nvlist(rec, 4);
5175 if (!cb->longfmt) {
5176 (void) printf("\n");
5177 continue;
5179 (void) printf(" [");
5180 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5181 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5182 struct passwd *pwd = getpwuid(who);
5183 (void) printf("user %d ", (int)who);
5184 if (pwd != NULL)
5185 (void) printf("(%s) ", pwd->pw_name);
5187 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5188 (void) printf("on %s",
5189 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5191 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5192 (void) printf(":%s",
5193 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5195 (void) printf("]");
5196 (void) printf("\n");
5198 (void) printf("\n");
5199 nvlist_free(nvhis);
5201 return (ret);
5205 * zpool history <pool>
5207 * Displays the history of commands that modified pools.
5210 zpool_do_history(int argc, char **argv)
5212 hist_cbdata_t cbdata = { 0 };
5213 int ret;
5214 int c;
5216 cbdata.first = B_TRUE;
5217 /* check options */
5218 while ((c = getopt(argc, argv, "li")) != -1) {
5219 switch (c) {
5220 case 'l':
5221 cbdata.longfmt = B_TRUE;
5222 break;
5223 case 'i':
5224 cbdata.internal = B_TRUE;
5225 break;
5226 case '?':
5227 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5228 optopt);
5229 usage(B_FALSE);
5232 argc -= optind;
5233 argv += optind;
5235 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5236 &cbdata);
5238 if (argc == 0 && cbdata.first == B_TRUE) {
5239 (void) printf(gettext("no pools available\n"));
5240 return (0);
5243 return (ret);
5246 static int
5247 get_callback(zpool_handle_t *zhp, void *data)
5249 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5250 char value[MAXNAMELEN];
5251 zprop_source_t srctype;
5252 zprop_list_t *pl;
5254 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5257 * Skip the special fake placeholder. This will also skip
5258 * over the name property when 'all' is specified.
5260 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5261 pl == cbp->cb_proplist)
5262 continue;
5264 if (pl->pl_prop == ZPROP_INVAL &&
5265 (zpool_prop_feature(pl->pl_user_prop) ||
5266 zpool_prop_unsupported(pl->pl_user_prop))) {
5267 srctype = ZPROP_SRC_LOCAL;
5269 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5270 value, sizeof (value)) == 0) {
5271 zprop_print_one_property(zpool_get_name(zhp),
5272 cbp, pl->pl_user_prop, value, srctype,
5273 NULL, NULL);
5275 } else {
5276 if (zpool_get_prop(zhp, pl->pl_prop, value,
5277 sizeof (value), &srctype, cbp->cb_literal) != 0)
5278 continue;
5280 zprop_print_one_property(zpool_get_name(zhp), cbp,
5281 zpool_prop_to_name(pl->pl_prop), value, srctype,
5282 NULL, NULL);
5285 return (0);
5289 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5291 * -H Scripted mode. Don't display headers, and separate properties
5292 * by a single tab.
5293 * -o List of columns to display. Defaults to
5294 * "name,property,value,source".
5295 * -p Diplay values in parsable (exact) format.
5297 * Get properties of pools in the system. Output space statistics
5298 * for each one as well as other attributes.
5301 zpool_do_get(int argc, char **argv)
5303 zprop_get_cbdata_t cb = { 0 };
5304 zprop_list_t fake_name = { 0 };
5305 int ret;
5306 int c, i;
5307 char *value;
5309 cb.cb_first = B_TRUE;
5312 * Set up default columns and sources.
5314 cb.cb_sources = ZPROP_SRC_ALL;
5315 cb.cb_columns[0] = GET_COL_NAME;
5316 cb.cb_columns[1] = GET_COL_PROPERTY;
5317 cb.cb_columns[2] = GET_COL_VALUE;
5318 cb.cb_columns[3] = GET_COL_SOURCE;
5319 cb.cb_type = ZFS_TYPE_POOL;
5321 /* check options */
5322 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
5323 switch (c) {
5324 case 'p':
5325 cb.cb_literal = B_TRUE;
5326 break;
5327 case 'H':
5328 cb.cb_scripted = B_TRUE;
5329 break;
5330 case 'o':
5331 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5332 i = 0;
5333 while (*optarg != '\0') {
5334 static char *col_subopts[] =
5335 { "name", "property", "value", "source",
5336 "all", NULL };
5338 if (i == ZFS_GET_NCOLS) {
5339 (void) fprintf(stderr, gettext("too "
5340 "many fields given to -o "
5341 "option\n"));
5342 usage(B_FALSE);
5345 switch (getsubopt(&optarg, col_subopts,
5346 &value)) {
5347 case 0:
5348 cb.cb_columns[i++] = GET_COL_NAME;
5349 break;
5350 case 1:
5351 cb.cb_columns[i++] = GET_COL_PROPERTY;
5352 break;
5353 case 2:
5354 cb.cb_columns[i++] = GET_COL_VALUE;
5355 break;
5356 case 3:
5357 cb.cb_columns[i++] = GET_COL_SOURCE;
5358 break;
5359 case 4:
5360 if (i > 0) {
5361 (void) fprintf(stderr,
5362 gettext("\"all\" conflicts "
5363 "with specific fields "
5364 "given to -o option\n"));
5365 usage(B_FALSE);
5367 cb.cb_columns[0] = GET_COL_NAME;
5368 cb.cb_columns[1] = GET_COL_PROPERTY;
5369 cb.cb_columns[2] = GET_COL_VALUE;
5370 cb.cb_columns[3] = GET_COL_SOURCE;
5371 i = ZFS_GET_NCOLS;
5372 break;
5373 default:
5374 (void) fprintf(stderr,
5375 gettext("invalid column name "
5376 "'%s'\n"), value);
5377 usage(B_FALSE);
5380 break;
5381 case '?':
5382 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5383 optopt);
5384 usage(B_FALSE);
5388 argc -= optind;
5389 argv += optind;
5391 if (argc < 1) {
5392 (void) fprintf(stderr, gettext("missing property "
5393 "argument\n"));
5394 usage(B_FALSE);
5397 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
5398 ZFS_TYPE_POOL) != 0)
5399 usage(B_FALSE);
5401 argc--;
5402 argv++;
5404 if (cb.cb_proplist != NULL) {
5405 fake_name.pl_prop = ZPOOL_PROP_NAME;
5406 fake_name.pl_width = strlen(gettext("NAME"));
5407 fake_name.pl_next = cb.cb_proplist;
5408 cb.cb_proplist = &fake_name;
5411 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5412 get_callback, &cb);
5414 if (cb.cb_proplist == &fake_name)
5415 zprop_free_list(fake_name.pl_next);
5416 else
5417 zprop_free_list(cb.cb_proplist);
5419 return (ret);
5422 typedef struct set_cbdata {
5423 char *cb_propname;
5424 char *cb_value;
5425 boolean_t cb_any_successful;
5426 } set_cbdata_t;
5429 set_callback(zpool_handle_t *zhp, void *data)
5431 int error;
5432 set_cbdata_t *cb = (set_cbdata_t *)data;
5434 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5436 if (!error)
5437 cb->cb_any_successful = B_TRUE;
5439 return (error);
5443 zpool_do_set(int argc, char **argv)
5445 set_cbdata_t cb = { 0 };
5446 int error;
5448 if (argc > 1 && argv[1][0] == '-') {
5449 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5450 argv[1][1]);
5451 usage(B_FALSE);
5454 if (argc < 2) {
5455 (void) fprintf(stderr, gettext("missing property=value "
5456 "argument\n"));
5457 usage(B_FALSE);
5460 if (argc < 3) {
5461 (void) fprintf(stderr, gettext("missing pool name\n"));
5462 usage(B_FALSE);
5465 if (argc > 3) {
5466 (void) fprintf(stderr, gettext("too many pool names\n"));
5467 usage(B_FALSE);
5470 cb.cb_propname = argv[1];
5471 cb.cb_value = strchr(cb.cb_propname, '=');
5472 if (cb.cb_value == NULL) {
5473 (void) fprintf(stderr, gettext("missing value in "
5474 "property=value argument\n"));
5475 usage(B_FALSE);
5478 *(cb.cb_value) = '\0';
5479 cb.cb_value++;
5481 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5482 set_callback, &cb);
5484 return (error);
5487 static int
5488 find_command_idx(char *command, int *idx)
5490 int i;
5492 for (i = 0; i < NCOMMAND; i++) {
5493 if (command_table[i].name == NULL)
5494 continue;
5496 if (strcmp(command, command_table[i].name) == 0) {
5497 *idx = i;
5498 return (0);
5501 return (1);
5505 main(int argc, char **argv)
5507 int ret = 0;
5508 int i;
5509 char *cmdname;
5511 (void) setlocale(LC_ALL, "");
5512 (void) textdomain(TEXT_DOMAIN);
5514 if ((g_zfs = libzfs_init()) == NULL) {
5515 (void) fprintf(stderr, gettext("internal error: failed to "
5516 "initialize ZFS library\n"));
5517 return (1);
5520 libzfs_print_on_error(g_zfs, B_TRUE);
5522 opterr = 0;
5525 * Make sure the user has specified some command.
5527 if (argc < 2) {
5528 (void) fprintf(stderr, gettext("missing command\n"));
5529 usage(B_FALSE);
5532 cmdname = argv[1];
5535 * Special case '-?'
5537 if (strcmp(cmdname, "-?") == 0)
5538 usage(B_TRUE);
5540 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5543 * Run the appropriate command.
5545 if (find_command_idx(cmdname, &i) == 0) {
5546 current_command = &command_table[i];
5547 ret = command_table[i].func(argc - 1, argv + 1);
5548 } else if (strchr(cmdname, '=')) {
5549 verify(find_command_idx("set", &i) == 0);
5550 current_command = &command_table[i];
5551 ret = command_table[i].func(argc, argv);
5552 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5554 * 'freeze' is a vile debugging abomination, so we treat
5555 * it as such.
5557 char buf[16384];
5558 int fd = open(ZFS_DEV, O_RDWR);
5559 (void) strcpy((void *)buf, argv[2]);
5560 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5561 } else {
5562 (void) fprintf(stderr, gettext("unrecognized "
5563 "command '%s'\n"), cmdname);
5564 usage(B_FALSE);
5567 if (ret == 0 && log_history)
5568 (void) zpool_log_history(g_zfs, history_str);
5570 libzfs_fini(g_zfs);
5573 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5574 * for the purposes of running ::findleaks.
5576 if (getenv("ZFS_ABORT") != NULL) {
5577 (void) printf("dumping core by request\n");
5578 abort();
5581 return (ret);