2882 implement libzfs_core
[unleashed.git] / usr / src / cmd / zpool / zpool_main.c
blob3ecdbec9684df8ec207b7241ead295660a995dd3
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 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
29 #include <assert.h>
30 #include <ctype.h>
31 #include <dirent.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <libgen.h>
35 #include <libintl.h>
36 #include <libuutil.h>
37 #include <locale.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <priv.h>
44 #include <pwd.h>
45 #include <zone.h>
46 #include <zfs_prop.h>
47 #include <sys/fs/zfs.h>
48 #include <sys/stat.h>
50 #include <libzfs.h>
52 #include "zpool_util.h"
53 #include "zfs_comutil.h"
54 #include "zfeature_common.h"
56 #include "statcommon.h"
58 static int zpool_do_create(int, char **);
59 static int zpool_do_destroy(int, char **);
61 static int zpool_do_add(int, char **);
62 static int zpool_do_remove(int, char **);
64 static int zpool_do_list(int, char **);
65 static int zpool_do_iostat(int, char **);
66 static int zpool_do_status(int, char **);
68 static int zpool_do_online(int, char **);
69 static int zpool_do_offline(int, char **);
70 static int zpool_do_clear(int, char **);
71 static int zpool_do_reopen(int, char **);
73 static int zpool_do_reguid(int, char **);
75 static int zpool_do_attach(int, char **);
76 static int zpool_do_detach(int, char **);
77 static int zpool_do_replace(int, char **);
78 static int zpool_do_split(int, char **);
80 static int zpool_do_scrub(int, char **);
82 static int zpool_do_import(int, char **);
83 static int zpool_do_export(int, char **);
85 static int zpool_do_upgrade(int, char **);
87 static int zpool_do_history(int, char **);
89 static int zpool_do_get(int, char **);
90 static int zpool_do_set(int, char **);
93 * These libumem hooks provide a reasonable set of defaults for the allocator's
94 * debugging facilities.
97 #ifdef DEBUG
98 const char *
99 _umem_debug_init(void)
101 return ("default,verbose"); /* $UMEM_DEBUG setting */
104 const char *
105 _umem_logging_init(void)
107 return ("fail,contents"); /* $UMEM_LOGGING setting */
109 #endif
111 typedef enum {
112 HELP_ADD,
113 HELP_ATTACH,
114 HELP_CLEAR,
115 HELP_CREATE,
116 HELP_DESTROY,
117 HELP_DETACH,
118 HELP_EXPORT,
119 HELP_HISTORY,
120 HELP_IMPORT,
121 HELP_IOSTAT,
122 HELP_LIST,
123 HELP_OFFLINE,
124 HELP_ONLINE,
125 HELP_REPLACE,
126 HELP_REMOVE,
127 HELP_SCRUB,
128 HELP_STATUS,
129 HELP_UPGRADE,
130 HELP_GET,
131 HELP_SET,
132 HELP_SPLIT,
133 HELP_REGUID,
134 HELP_REOPEN
135 } zpool_help_t;
138 typedef struct zpool_command {
139 const char *name;
140 int (*func)(int, char **);
141 zpool_help_t usage;
142 } zpool_command_t;
145 * Master command table. Each ZFS command has a name, associated function, and
146 * usage message. The usage messages need to be internationalized, so we have
147 * to have a function to return the usage message based on a command index.
149 * These commands are organized according to how they are displayed in the usage
150 * message. An empty command (one with a NULL name) indicates an empty line in
151 * the generic usage message.
153 static zpool_command_t command_table[] = {
154 { "create", zpool_do_create, HELP_CREATE },
155 { "destroy", zpool_do_destroy, HELP_DESTROY },
156 { NULL },
157 { "add", zpool_do_add, HELP_ADD },
158 { "remove", zpool_do_remove, HELP_REMOVE },
159 { NULL },
160 { "list", zpool_do_list, HELP_LIST },
161 { "iostat", zpool_do_iostat, HELP_IOSTAT },
162 { "status", zpool_do_status, HELP_STATUS },
163 { NULL },
164 { "online", zpool_do_online, HELP_ONLINE },
165 { "offline", zpool_do_offline, HELP_OFFLINE },
166 { "clear", zpool_do_clear, HELP_CLEAR },
167 { "reopen", zpool_do_reopen, HELP_REOPEN },
168 { NULL },
169 { "attach", zpool_do_attach, HELP_ATTACH },
170 { "detach", zpool_do_detach, HELP_DETACH },
171 { "replace", zpool_do_replace, HELP_REPLACE },
172 { "split", zpool_do_split, HELP_SPLIT },
173 { NULL },
174 { "scrub", zpool_do_scrub, HELP_SCRUB },
175 { NULL },
176 { "import", zpool_do_import, HELP_IMPORT },
177 { "export", zpool_do_export, HELP_EXPORT },
178 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
179 { "reguid", zpool_do_reguid, HELP_REGUID },
180 { NULL },
181 { "history", zpool_do_history, HELP_HISTORY },
182 { "get", zpool_do_get, HELP_GET },
183 { "set", zpool_do_set, HELP_SET },
186 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
188 static zpool_command_t *current_command;
189 static char history_str[HIS_MAX_RECORD_LEN];
190 static boolean_t log_history = B_TRUE;
191 static uint_t timestamp_fmt = NODATE;
193 static const char *
194 get_usage(zpool_help_t idx) {
195 switch (idx) {
196 case HELP_ADD:
197 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
198 case HELP_ATTACH:
199 return (gettext("\tattach [-f] <pool> <device> "
200 "<new-device>\n"));
201 case HELP_CLEAR:
202 return (gettext("\tclear [-nF] <pool> [device]\n"));
203 case HELP_CREATE:
204 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
205 "\t [-O file-system-property=value] ... \n"
206 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
207 case HELP_DESTROY:
208 return (gettext("\tdestroy [-f] <pool>\n"));
209 case HELP_DETACH:
210 return (gettext("\tdetach <pool> <device>\n"));
211 case HELP_EXPORT:
212 return (gettext("\texport [-f] <pool> ...\n"));
213 case HELP_HISTORY:
214 return (gettext("\thistory [-il] [<pool>] ...\n"));
215 case HELP_IMPORT:
216 return (gettext("\timport [-d dir] [-D]\n"
217 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
218 "\timport [-o mntopts] [-o property=value] ... \n"
219 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
220 "[-R root] [-F [-n]] -a\n"
221 "\timport [-o mntopts] [-o property=value] ... \n"
222 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
223 "[-R root] [-F [-n]]\n"
224 "\t <pool | id> [newpool]\n"));
225 case HELP_IOSTAT:
226 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
227 "[count]]\n"));
228 case HELP_LIST:
229 return (gettext("\tlist [-H] [-o property[,...]] "
230 "[-T d|u] [pool] ... [interval [count]]\n"));
231 case HELP_OFFLINE:
232 return (gettext("\toffline [-t] <pool> <device> ...\n"));
233 case HELP_ONLINE:
234 return (gettext("\tonline <pool> <device> ...\n"));
235 case HELP_REPLACE:
236 return (gettext("\treplace [-f] <pool> <device> "
237 "[new-device]\n"));
238 case HELP_REMOVE:
239 return (gettext("\tremove <pool> <device> ...\n"));
240 case HELP_REOPEN:
241 return (""); /* Undocumented command */
242 case HELP_SCRUB:
243 return (gettext("\tscrub [-s] <pool> ...\n"));
244 case HELP_STATUS:
245 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
246 "[count]]\n"));
247 case HELP_UPGRADE:
248 return (gettext("\tupgrade\n"
249 "\tupgrade -v\n"
250 "\tupgrade [-V version] <-a | pool ...>\n"));
251 case HELP_GET:
252 return (gettext("\tget <\"all\" | property[,...]> "
253 "<pool> ...\n"));
254 case HELP_SET:
255 return (gettext("\tset <property=value> <pool> \n"));
256 case HELP_SPLIT:
257 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
258 "\t [-o property=value] <pool> <newpool> "
259 "[<device> ...]\n"));
260 case HELP_REGUID:
261 return (gettext("\treguid <pool>\n"));
264 abort();
265 /* NOTREACHED */
270 * Callback routine that will print out a pool property value.
272 static int
273 print_prop_cb(int prop, void *cb)
275 FILE *fp = cb;
277 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
279 if (zpool_prop_readonly(prop))
280 (void) fprintf(fp, " NO ");
281 else
282 (void) fprintf(fp, " YES ");
284 if (zpool_prop_values(prop) == NULL)
285 (void) fprintf(fp, "-\n");
286 else
287 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
289 return (ZPROP_CONT);
293 * Display usage message. If we're inside a command, display only the usage for
294 * that command. Otherwise, iterate over the entire command table and display
295 * a complete usage message.
297 void
298 usage(boolean_t requested)
300 FILE *fp = requested ? stdout : stderr;
302 if (current_command == NULL) {
303 int i;
305 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
306 (void) fprintf(fp,
307 gettext("where 'command' is one of the following:\n\n"));
309 for (i = 0; i < NCOMMAND; i++) {
310 if (command_table[i].name == NULL)
311 (void) fprintf(fp, "\n");
312 else
313 (void) fprintf(fp, "%s",
314 get_usage(command_table[i].usage));
316 } else {
317 (void) fprintf(fp, gettext("usage:\n"));
318 (void) fprintf(fp, "%s", get_usage(current_command->usage));
321 if (current_command != NULL &&
322 ((strcmp(current_command->name, "set") == 0) ||
323 (strcmp(current_command->name, "get") == 0) ||
324 (strcmp(current_command->name, "list") == 0))) {
326 (void) fprintf(fp,
327 gettext("\nthe following properties are supported:\n"));
329 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
330 "PROPERTY", "EDIT", "VALUES");
332 /* Iterate over all properties */
333 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
334 ZFS_TYPE_POOL);
336 (void) fprintf(fp, "\t%-15s ", "feature@...");
337 (void) fprintf(fp, "YES disabled | enabled | active\n");
339 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
340 "appended with a feature name.\nSee zpool-features(5).\n"));
344 * See comments at end of main().
346 if (getenv("ZFS_ABORT") != NULL) {
347 (void) printf("dumping core by request\n");
348 abort();
351 exit(requested ? 0 : 2);
354 void
355 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
356 boolean_t print_logs)
358 nvlist_t **child;
359 uint_t c, children;
360 char *vname;
362 if (name != NULL)
363 (void) printf("\t%*s%s\n", indent, "", name);
365 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
366 &child, &children) != 0)
367 return;
369 for (c = 0; c < children; c++) {
370 uint64_t is_log = B_FALSE;
372 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
373 &is_log);
374 if ((is_log && !print_logs) || (!is_log && print_logs))
375 continue;
377 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
378 print_vdev_tree(zhp, vname, child[c], indent + 2,
379 B_FALSE);
380 free(vname);
385 * Add a property pair (name, string-value) into a property nvlist.
387 static int
388 add_prop_list(const char *propname, char *propval, nvlist_t **props,
389 boolean_t poolprop)
391 zpool_prop_t prop = ZPROP_INVAL;
392 zfs_prop_t fprop;
393 nvlist_t *proplist;
394 const char *normnm;
395 char *strval;
397 if (*props == NULL &&
398 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
399 (void) fprintf(stderr,
400 gettext("internal error: out of memory\n"));
401 return (1);
404 proplist = *props;
406 if (poolprop) {
407 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
408 !zpool_prop_feature(propname)) {
409 (void) fprintf(stderr, gettext("property '%s' is "
410 "not a valid pool property\n"), propname);
411 return (2);
413 if (zpool_prop_feature(propname))
414 normnm = propname;
415 else
416 normnm = zpool_prop_to_name(prop);
417 } else {
418 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
419 normnm = zfs_prop_to_name(fprop);
420 } else {
421 normnm = propname;
425 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
426 prop != ZPOOL_PROP_CACHEFILE) {
427 (void) fprintf(stderr, gettext("property '%s' "
428 "specified multiple times\n"), propname);
429 return (2);
432 if (nvlist_add_string(proplist, normnm, propval) != 0) {
433 (void) fprintf(stderr, gettext("internal "
434 "error: out of memory\n"));
435 return (1);
438 return (0);
442 * zpool add [-fn] <pool> <vdev> ...
444 * -f Force addition of devices, even if they appear in use
445 * -n Do not add the devices, but display the resulting layout if
446 * they were to be added.
448 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
449 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
450 * libzfs.
453 zpool_do_add(int argc, char **argv)
455 boolean_t force = B_FALSE;
456 boolean_t dryrun = B_FALSE;
457 int c;
458 nvlist_t *nvroot;
459 char *poolname;
460 int ret;
461 zpool_handle_t *zhp;
462 nvlist_t *config;
464 /* check options */
465 while ((c = getopt(argc, argv, "fn")) != -1) {
466 switch (c) {
467 case 'f':
468 force = B_TRUE;
469 break;
470 case 'n':
471 dryrun = B_TRUE;
472 break;
473 case '?':
474 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
475 optopt);
476 usage(B_FALSE);
480 argc -= optind;
481 argv += optind;
483 /* get pool name and check number of arguments */
484 if (argc < 1) {
485 (void) fprintf(stderr, gettext("missing pool name argument\n"));
486 usage(B_FALSE);
488 if (argc < 2) {
489 (void) fprintf(stderr, gettext("missing vdev specification\n"));
490 usage(B_FALSE);
493 poolname = argv[0];
495 argc--;
496 argv++;
498 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
499 return (1);
501 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
502 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
503 poolname);
504 zpool_close(zhp);
505 return (1);
508 /* pass off to get_vdev_spec for processing */
509 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
510 argc, argv);
511 if (nvroot == NULL) {
512 zpool_close(zhp);
513 return (1);
516 if (dryrun) {
517 nvlist_t *poolnvroot;
519 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
520 &poolnvroot) == 0);
522 (void) printf(gettext("would update '%s' to the following "
523 "configuration:\n"), zpool_get_name(zhp));
525 /* print original main pool and new tree */
526 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
527 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
529 /* Do the same for the logs */
530 if (num_logs(poolnvroot) > 0) {
531 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
532 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
533 } else if (num_logs(nvroot) > 0) {
534 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
537 ret = 0;
538 } else {
539 ret = (zpool_add(zhp, nvroot) != 0);
542 nvlist_free(nvroot);
543 zpool_close(zhp);
545 return (ret);
549 * zpool remove <pool> <vdev> ...
551 * Removes the given vdev from the pool. Currently, this supports removing
552 * spares, cache, and log devices from the pool.
555 zpool_do_remove(int argc, char **argv)
557 char *poolname;
558 int i, ret = 0;
559 zpool_handle_t *zhp;
561 argc--;
562 argv++;
564 /* get pool name and check number of arguments */
565 if (argc < 1) {
566 (void) fprintf(stderr, gettext("missing pool name argument\n"));
567 usage(B_FALSE);
569 if (argc < 2) {
570 (void) fprintf(stderr, gettext("missing device\n"));
571 usage(B_FALSE);
574 poolname = argv[0];
576 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
577 return (1);
579 for (i = 1; i < argc; i++) {
580 if (zpool_vdev_remove(zhp, argv[i]) != 0)
581 ret = 1;
584 return (ret);
588 * zpool create [-fnd] [-o property=value] ...
589 * [-O file-system-property=value] ...
590 * [-R root] [-m mountpoint] <pool> <dev> ...
592 * -f Force creation, even if devices appear in use
593 * -n Do not create the pool, but display the resulting layout if it
594 * were to be created.
595 * -R Create a pool under an alternate root
596 * -m Set default mountpoint for the root dataset. By default it's
597 * '/<pool>'
598 * -o Set property=value.
599 * -d Don't automatically enable all supported pool features
600 * (individual features can be enabled with -o).
601 * -O Set fsproperty=value in the pool's root file system
603 * Creates the named pool according to the given vdev specification. The
604 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
605 * we get the nvlist back from get_vdev_spec(), we either print out the contents
606 * (if '-n' was specified), or pass it to libzfs to do the creation.
609 zpool_do_create(int argc, char **argv)
611 boolean_t force = B_FALSE;
612 boolean_t dryrun = B_FALSE;
613 boolean_t enable_all_pool_feat = B_TRUE;
614 int c;
615 nvlist_t *nvroot = NULL;
616 char *poolname;
617 int ret = 1;
618 char *altroot = NULL;
619 char *mountpoint = NULL;
620 nvlist_t *fsprops = NULL;
621 nvlist_t *props = NULL;
622 char *propval;
624 /* check options */
625 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
626 switch (c) {
627 case 'f':
628 force = B_TRUE;
629 break;
630 case 'n':
631 dryrun = B_TRUE;
632 break;
633 case 'd':
634 enable_all_pool_feat = B_FALSE;
635 break;
636 case 'R':
637 altroot = optarg;
638 if (add_prop_list(zpool_prop_to_name(
639 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
640 goto errout;
641 if (nvlist_lookup_string(props,
642 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
643 &propval) == 0)
644 break;
645 if (add_prop_list(zpool_prop_to_name(
646 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
647 goto errout;
648 break;
649 case 'm':
650 mountpoint = optarg;
651 break;
652 case 'o':
653 if ((propval = strchr(optarg, '=')) == NULL) {
654 (void) fprintf(stderr, gettext("missing "
655 "'=' for -o option\n"));
656 goto errout;
658 *propval = '\0';
659 propval++;
661 if (add_prop_list(optarg, propval, &props, B_TRUE))
662 goto errout;
665 * If the user is creating a pool that doesn't support
666 * feature flags, don't enable any features.
668 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
669 char *end;
670 u_longlong_t ver;
672 ver = strtoull(propval, &end, 10);
673 if (*end == '\0' &&
674 ver < SPA_VERSION_FEATURES) {
675 enable_all_pool_feat = B_FALSE;
678 break;
679 case 'O':
680 if ((propval = strchr(optarg, '=')) == NULL) {
681 (void) fprintf(stderr, gettext("missing "
682 "'=' for -O option\n"));
683 goto errout;
685 *propval = '\0';
686 propval++;
688 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
689 goto errout;
690 break;
691 case ':':
692 (void) fprintf(stderr, gettext("missing argument for "
693 "'%c' option\n"), optopt);
694 goto badusage;
695 case '?':
696 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
697 optopt);
698 goto badusage;
702 argc -= optind;
703 argv += optind;
705 /* get pool name and check number of arguments */
706 if (argc < 1) {
707 (void) fprintf(stderr, gettext("missing pool name argument\n"));
708 goto badusage;
710 if (argc < 2) {
711 (void) fprintf(stderr, gettext("missing vdev specification\n"));
712 goto badusage;
715 poolname = argv[0];
718 * As a special case, check for use of '/' in the name, and direct the
719 * user to use 'zfs create' instead.
721 if (strchr(poolname, '/') != NULL) {
722 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
723 "character '/' in pool name\n"), poolname);
724 (void) fprintf(stderr, gettext("use 'zfs create' to "
725 "create a dataset\n"));
726 goto errout;
729 /* pass off to get_vdev_spec for bulk processing */
730 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
731 argc - 1, argv + 1);
732 if (nvroot == NULL)
733 goto errout;
735 /* make_root_vdev() allows 0 toplevel children if there are spares */
736 if (!zfs_allocatable_devs(nvroot)) {
737 (void) fprintf(stderr, gettext("invalid vdev "
738 "specification: at least one toplevel vdev must be "
739 "specified\n"));
740 goto errout;
743 if (altroot != NULL && altroot[0] != '/') {
744 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
745 "must be an absolute path\n"), altroot);
746 goto errout;
750 * Check the validity of the mountpoint and direct the user to use the
751 * '-m' mountpoint option if it looks like its in use.
753 if (mountpoint == NULL ||
754 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
755 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
756 char buf[MAXPATHLEN];
757 DIR *dirp;
759 if (mountpoint && mountpoint[0] != '/') {
760 (void) fprintf(stderr, gettext("invalid mountpoint "
761 "'%s': must be an absolute path, 'legacy', or "
762 "'none'\n"), mountpoint);
763 goto errout;
766 if (mountpoint == NULL) {
767 if (altroot != NULL)
768 (void) snprintf(buf, sizeof (buf), "%s/%s",
769 altroot, poolname);
770 else
771 (void) snprintf(buf, sizeof (buf), "/%s",
772 poolname);
773 } else {
774 if (altroot != NULL)
775 (void) snprintf(buf, sizeof (buf), "%s%s",
776 altroot, mountpoint);
777 else
778 (void) snprintf(buf, sizeof (buf), "%s",
779 mountpoint);
782 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
783 (void) fprintf(stderr, gettext("mountpoint '%s' : "
784 "%s\n"), buf, strerror(errno));
785 (void) fprintf(stderr, gettext("use '-m' "
786 "option to provide a different default\n"));
787 goto errout;
788 } else if (dirp) {
789 int count = 0;
791 while (count < 3 && readdir(dirp) != NULL)
792 count++;
793 (void) closedir(dirp);
795 if (count > 2) {
796 (void) fprintf(stderr, gettext("mountpoint "
797 "'%s' exists and is not empty\n"), buf);
798 (void) fprintf(stderr, gettext("use '-m' "
799 "option to provide a "
800 "different default\n"));
801 goto errout;
806 if (dryrun) {
808 * For a dry run invocation, print out a basic message and run
809 * through all the vdevs in the list and print out in an
810 * appropriate hierarchy.
812 (void) printf(gettext("would create '%s' with the "
813 "following layout:\n\n"), poolname);
815 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
816 if (num_logs(nvroot) > 0)
817 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
819 ret = 0;
820 } else {
822 * Hand off to libzfs.
824 if (enable_all_pool_feat) {
825 int i;
826 for (i = 0; i < SPA_FEATURES; i++) {
827 char propname[MAXPATHLEN];
828 zfeature_info_t *feat = &spa_feature_table[i];
830 (void) snprintf(propname, sizeof (propname),
831 "feature@%s", feat->fi_uname);
834 * Skip feature if user specified it manually
835 * on the command line.
837 if (nvlist_exists(props, propname))
838 continue;
840 if (add_prop_list(propname, ZFS_FEATURE_ENABLED,
841 &props, B_TRUE) != 0)
842 goto errout;
845 if (zpool_create(g_zfs, poolname,
846 nvroot, props, fsprops) == 0) {
847 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
848 ZFS_TYPE_FILESYSTEM);
849 if (pool != NULL) {
850 if (mountpoint != NULL)
851 verify(zfs_prop_set(pool,
852 zfs_prop_to_name(
853 ZFS_PROP_MOUNTPOINT),
854 mountpoint) == 0);
855 if (zfs_mount(pool, NULL, 0) == 0)
856 ret = zfs_shareall(pool);
857 zfs_close(pool);
859 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
860 (void) fprintf(stderr, gettext("pool name may have "
861 "been omitted\n"));
865 errout:
866 nvlist_free(nvroot);
867 nvlist_free(fsprops);
868 nvlist_free(props);
869 return (ret);
870 badusage:
871 nvlist_free(fsprops);
872 nvlist_free(props);
873 usage(B_FALSE);
874 return (2);
878 * zpool destroy <pool>
880 * -f Forcefully unmount any datasets
882 * Destroy the given pool. Automatically unmounts any datasets in the pool.
885 zpool_do_destroy(int argc, char **argv)
887 boolean_t force = B_FALSE;
888 int c;
889 char *pool;
890 zpool_handle_t *zhp;
891 int ret;
893 /* check options */
894 while ((c = getopt(argc, argv, "f")) != -1) {
895 switch (c) {
896 case 'f':
897 force = B_TRUE;
898 break;
899 case '?':
900 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
901 optopt);
902 usage(B_FALSE);
906 argc -= optind;
907 argv += optind;
909 /* check arguments */
910 if (argc < 1) {
911 (void) fprintf(stderr, gettext("missing pool argument\n"));
912 usage(B_FALSE);
914 if (argc > 1) {
915 (void) fprintf(stderr, gettext("too many arguments\n"));
916 usage(B_FALSE);
919 pool = argv[0];
921 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
923 * As a special case, check for use of '/' in the name, and
924 * direct the user to use 'zfs destroy' instead.
926 if (strchr(pool, '/') != NULL)
927 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
928 "destroy a dataset\n"));
929 return (1);
932 if (zpool_disable_datasets(zhp, force) != 0) {
933 (void) fprintf(stderr, gettext("could not destroy '%s': "
934 "could not unmount datasets\n"), zpool_get_name(zhp));
935 return (1);
938 /* The history must be logged as part of the export */
939 log_history = B_FALSE;
941 ret = (zpool_destroy(zhp, history_str) != 0);
943 zpool_close(zhp);
945 return (ret);
949 * zpool export [-f] <pool> ...
951 * -f Forcefully unmount datasets
953 * Export the given pools. By default, the command will attempt to cleanly
954 * unmount any active datasets within the pool. If the '-f' flag is specified,
955 * then the datasets will be forcefully unmounted.
958 zpool_do_export(int argc, char **argv)
960 boolean_t force = B_FALSE;
961 boolean_t hardforce = B_FALSE;
962 int c;
963 zpool_handle_t *zhp;
964 int ret;
965 int i;
967 /* check options */
968 while ((c = getopt(argc, argv, "fF")) != -1) {
969 switch (c) {
970 case 'f':
971 force = B_TRUE;
972 break;
973 case 'F':
974 hardforce = B_TRUE;
975 break;
976 case '?':
977 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
978 optopt);
979 usage(B_FALSE);
983 argc -= optind;
984 argv += optind;
986 /* check arguments */
987 if (argc < 1) {
988 (void) fprintf(stderr, gettext("missing pool argument\n"));
989 usage(B_FALSE);
992 ret = 0;
993 for (i = 0; i < argc; i++) {
994 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
995 ret = 1;
996 continue;
999 if (zpool_disable_datasets(zhp, force) != 0) {
1000 ret = 1;
1001 zpool_close(zhp);
1002 continue;
1005 /* The history must be logged as part of the export */
1006 log_history = B_FALSE;
1008 if (hardforce) {
1009 if (zpool_export_force(zhp, history_str) != 0)
1010 ret = 1;
1011 } else if (zpool_export(zhp, force, history_str) != 0) {
1012 ret = 1;
1015 zpool_close(zhp);
1018 return (ret);
1022 * Given a vdev configuration, determine the maximum width needed for the device
1023 * name column.
1025 static int
1026 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1028 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1029 nvlist_t **child;
1030 uint_t c, children;
1031 int ret;
1033 if (strlen(name) + depth > max)
1034 max = strlen(name) + depth;
1036 free(name);
1038 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1039 &child, &children) == 0) {
1040 for (c = 0; c < children; c++)
1041 if ((ret = max_width(zhp, child[c], depth + 2,
1042 max)) > max)
1043 max = ret;
1046 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1047 &child, &children) == 0) {
1048 for (c = 0; c < children; c++)
1049 if ((ret = max_width(zhp, child[c], depth + 2,
1050 max)) > max)
1051 max = ret;
1054 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1055 &child, &children) == 0) {
1056 for (c = 0; c < children; c++)
1057 if ((ret = max_width(zhp, child[c], depth + 2,
1058 max)) > max)
1059 max = ret;
1063 return (max);
1066 typedef struct spare_cbdata {
1067 uint64_t cb_guid;
1068 zpool_handle_t *cb_zhp;
1069 } spare_cbdata_t;
1071 static boolean_t
1072 find_vdev(nvlist_t *nv, uint64_t search)
1074 uint64_t guid;
1075 nvlist_t **child;
1076 uint_t c, children;
1078 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1079 search == guid)
1080 return (B_TRUE);
1082 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1083 &child, &children) == 0) {
1084 for (c = 0; c < children; c++)
1085 if (find_vdev(child[c], search))
1086 return (B_TRUE);
1089 return (B_FALSE);
1092 static int
1093 find_spare(zpool_handle_t *zhp, void *data)
1095 spare_cbdata_t *cbp = data;
1096 nvlist_t *config, *nvroot;
1098 config = zpool_get_config(zhp, NULL);
1099 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1100 &nvroot) == 0);
1102 if (find_vdev(nvroot, cbp->cb_guid)) {
1103 cbp->cb_zhp = zhp;
1104 return (1);
1107 zpool_close(zhp);
1108 return (0);
1112 * Print out configuration state as requested by status_callback.
1114 void
1115 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1116 int namewidth, int depth, boolean_t isspare)
1118 nvlist_t **child;
1119 uint_t c, children;
1120 pool_scan_stat_t *ps = NULL;
1121 vdev_stat_t *vs;
1122 char rbuf[6], wbuf[6], cbuf[6];
1123 char *vname;
1124 uint64_t notpresent;
1125 spare_cbdata_t cb;
1126 char *state;
1128 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1129 &child, &children) != 0)
1130 children = 0;
1132 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1133 (uint64_t **)&vs, &c) == 0);
1135 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1136 if (isspare) {
1138 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1139 * online drives.
1141 if (vs->vs_aux == VDEV_AUX_SPARED)
1142 state = "INUSE";
1143 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1144 state = "AVAIL";
1147 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1148 name, state);
1150 if (!isspare) {
1151 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1152 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1153 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1154 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1157 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1158 &notpresent) == 0) {
1159 char *path;
1160 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1161 (void) printf(" was %s", path);
1162 } else if (vs->vs_aux != 0) {
1163 (void) printf(" ");
1165 switch (vs->vs_aux) {
1166 case VDEV_AUX_OPEN_FAILED:
1167 (void) printf(gettext("cannot open"));
1168 break;
1170 case VDEV_AUX_BAD_GUID_SUM:
1171 (void) printf(gettext("missing device"));
1172 break;
1174 case VDEV_AUX_NO_REPLICAS:
1175 (void) printf(gettext("insufficient replicas"));
1176 break;
1178 case VDEV_AUX_VERSION_NEWER:
1179 (void) printf(gettext("newer version"));
1180 break;
1182 case VDEV_AUX_UNSUP_FEAT:
1183 (void) printf(gettext("unsupported feature(s)"));
1184 break;
1186 case VDEV_AUX_SPARED:
1187 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1188 &cb.cb_guid) == 0);
1189 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1190 if (strcmp(zpool_get_name(cb.cb_zhp),
1191 zpool_get_name(zhp)) == 0)
1192 (void) printf(gettext("currently in "
1193 "use"));
1194 else
1195 (void) printf(gettext("in use by "
1196 "pool '%s'"),
1197 zpool_get_name(cb.cb_zhp));
1198 zpool_close(cb.cb_zhp);
1199 } else {
1200 (void) printf(gettext("currently in use"));
1202 break;
1204 case VDEV_AUX_ERR_EXCEEDED:
1205 (void) printf(gettext("too many errors"));
1206 break;
1208 case VDEV_AUX_IO_FAILURE:
1209 (void) printf(gettext("experienced I/O failures"));
1210 break;
1212 case VDEV_AUX_BAD_LOG:
1213 (void) printf(gettext("bad intent log"));
1214 break;
1216 case VDEV_AUX_EXTERNAL:
1217 (void) printf(gettext("external device fault"));
1218 break;
1220 case VDEV_AUX_SPLIT_POOL:
1221 (void) printf(gettext("split into new pool"));
1222 break;
1224 default:
1225 (void) printf(gettext("corrupted data"));
1226 break;
1230 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1231 (uint64_t **)&ps, &c);
1233 if (ps && ps->pss_state == DSS_SCANNING &&
1234 vs->vs_scan_processed != 0 && children == 0) {
1235 (void) printf(gettext(" (%s)"),
1236 (ps->pss_func == POOL_SCAN_RESILVER) ?
1237 "resilvering" : "repairing");
1240 (void) printf("\n");
1242 for (c = 0; c < children; c++) {
1243 uint64_t islog = B_FALSE, ishole = B_FALSE;
1245 /* Don't print logs or holes here */
1246 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1247 &islog);
1248 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1249 &ishole);
1250 if (islog || ishole)
1251 continue;
1252 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1253 print_status_config(zhp, vname, child[c],
1254 namewidth, depth + 2, isspare);
1255 free(vname);
1261 * Print the configuration of an exported pool. Iterate over all vdevs in the
1262 * pool, printing out the name and status for each one.
1264 void
1265 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1267 nvlist_t **child;
1268 uint_t c, children;
1269 vdev_stat_t *vs;
1270 char *type, *vname;
1272 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1273 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1274 strcmp(type, VDEV_TYPE_HOLE) == 0)
1275 return;
1277 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1278 (uint64_t **)&vs, &c) == 0);
1280 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1281 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1283 if (vs->vs_aux != 0) {
1284 (void) printf(" ");
1286 switch (vs->vs_aux) {
1287 case VDEV_AUX_OPEN_FAILED:
1288 (void) printf(gettext("cannot open"));
1289 break;
1291 case VDEV_AUX_BAD_GUID_SUM:
1292 (void) printf(gettext("missing device"));
1293 break;
1295 case VDEV_AUX_NO_REPLICAS:
1296 (void) printf(gettext("insufficient replicas"));
1297 break;
1299 case VDEV_AUX_VERSION_NEWER:
1300 (void) printf(gettext("newer version"));
1301 break;
1303 case VDEV_AUX_UNSUP_FEAT:
1304 (void) printf(gettext("unsupported feature(s)"));
1305 break;
1307 case VDEV_AUX_ERR_EXCEEDED:
1308 (void) printf(gettext("too many errors"));
1309 break;
1311 default:
1312 (void) printf(gettext("corrupted data"));
1313 break;
1316 (void) printf("\n");
1318 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1319 &child, &children) != 0)
1320 return;
1322 for (c = 0; c < children; c++) {
1323 uint64_t is_log = B_FALSE;
1325 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1326 &is_log);
1327 if (is_log)
1328 continue;
1330 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1331 print_import_config(vname, child[c], namewidth, depth + 2);
1332 free(vname);
1335 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1336 &child, &children) == 0) {
1337 (void) printf(gettext("\tcache\n"));
1338 for (c = 0; c < children; c++) {
1339 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1340 (void) printf("\t %s\n", vname);
1341 free(vname);
1345 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1346 &child, &children) == 0) {
1347 (void) printf(gettext("\tspares\n"));
1348 for (c = 0; c < children; c++) {
1349 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1350 (void) printf("\t %s\n", vname);
1351 free(vname);
1357 * Print log vdevs.
1358 * Logs are recorded as top level vdevs in the main pool child array
1359 * but with "is_log" set to 1. We use either print_status_config() or
1360 * print_import_config() to print the top level logs then any log
1361 * children (eg mirrored slogs) are printed recursively - which
1362 * works because only the top level vdev is marked "is_log"
1364 static void
1365 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1367 uint_t c, children;
1368 nvlist_t **child;
1370 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1371 &children) != 0)
1372 return;
1374 (void) printf(gettext("\tlogs\n"));
1376 for (c = 0; c < children; c++) {
1377 uint64_t is_log = B_FALSE;
1378 char *name;
1380 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1381 &is_log);
1382 if (!is_log)
1383 continue;
1384 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1385 if (verbose)
1386 print_status_config(zhp, name, child[c], namewidth,
1387 2, B_FALSE);
1388 else
1389 print_import_config(name, child[c], namewidth, 2);
1390 free(name);
1395 * Display the status for the given pool.
1397 static void
1398 show_import(nvlist_t *config)
1400 uint64_t pool_state;
1401 vdev_stat_t *vs;
1402 char *name;
1403 uint64_t guid;
1404 char *msgid;
1405 nvlist_t *nvroot;
1406 int reason;
1407 const char *health;
1408 uint_t vsc;
1409 int namewidth;
1410 char *comment;
1412 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1413 &name) == 0);
1414 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1415 &guid) == 0);
1416 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1417 &pool_state) == 0);
1418 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1419 &nvroot) == 0);
1421 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1422 (uint64_t **)&vs, &vsc) == 0);
1423 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1425 reason = zpool_import_status(config, &msgid);
1427 (void) printf(gettext(" pool: %s\n"), name);
1428 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1429 (void) printf(gettext(" state: %s"), health);
1430 if (pool_state == POOL_STATE_DESTROYED)
1431 (void) printf(gettext(" (DESTROYED)"));
1432 (void) printf("\n");
1434 switch (reason) {
1435 case ZPOOL_STATUS_MISSING_DEV_R:
1436 case ZPOOL_STATUS_MISSING_DEV_NR:
1437 case ZPOOL_STATUS_BAD_GUID_SUM:
1438 (void) printf(gettext(" status: One or more devices are "
1439 "missing from the system.\n"));
1440 break;
1442 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1443 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1444 (void) printf(gettext(" status: One or more devices contains "
1445 "corrupted data.\n"));
1446 break;
1448 case ZPOOL_STATUS_CORRUPT_DATA:
1449 (void) printf(
1450 gettext(" status: The pool data is corrupted.\n"));
1451 break;
1453 case ZPOOL_STATUS_OFFLINE_DEV:
1454 (void) printf(gettext(" status: One or more devices "
1455 "are offlined.\n"));
1456 break;
1458 case ZPOOL_STATUS_CORRUPT_POOL:
1459 (void) printf(gettext(" status: The pool metadata is "
1460 "corrupted.\n"));
1461 break;
1463 case ZPOOL_STATUS_VERSION_OLDER:
1464 (void) printf(gettext(" status: The pool is formatted using an "
1465 "older on-disk version.\n"));
1466 break;
1468 case ZPOOL_STATUS_VERSION_NEWER:
1469 (void) printf(gettext(" status: The pool is formatted using an "
1470 "incompatible version.\n"));
1471 break;
1473 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1474 (void) printf(gettext("status: The pool uses the following "
1475 "feature(s) not supported on this sytem:\n"));
1476 zpool_print_unsup_feat(config);
1477 break;
1479 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1480 (void) printf(gettext("status: The pool can only be accessed "
1481 "in read-only mode on this system. It\n\tcannot be "
1482 "accessed in read-write mode because it uses the "
1483 "following\n\tfeature(s) not supported on this system:\n"));
1484 zpool_print_unsup_feat(config);
1485 break;
1487 case ZPOOL_STATUS_HOSTID_MISMATCH:
1488 (void) printf(gettext(" status: The pool was last accessed by "
1489 "another system.\n"));
1490 break;
1492 case ZPOOL_STATUS_FAULTED_DEV_R:
1493 case ZPOOL_STATUS_FAULTED_DEV_NR:
1494 (void) printf(gettext(" status: One or more devices are "
1495 "faulted.\n"));
1496 break;
1498 case ZPOOL_STATUS_BAD_LOG:
1499 (void) printf(gettext(" status: An intent log record cannot be "
1500 "read.\n"));
1501 break;
1503 case ZPOOL_STATUS_RESILVERING:
1504 (void) printf(gettext(" status: One or more devices were being "
1505 "resilvered.\n"));
1506 break;
1508 default:
1510 * No other status can be seen when importing pools.
1512 assert(reason == ZPOOL_STATUS_OK);
1516 * Print out an action according to the overall state of the pool.
1518 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1519 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1520 (void) printf(gettext(" action: The pool can be "
1521 "imported using its name or numeric identifier, "
1522 "though\n\tsome features will not be available "
1523 "without an explicit 'zpool upgrade'.\n"));
1524 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1525 (void) printf(gettext(" action: The pool can be "
1526 "imported using its name or numeric "
1527 "identifier and\n\tthe '-f' flag.\n"));
1528 else
1529 (void) printf(gettext(" action: The pool can be "
1530 "imported using its name or numeric "
1531 "identifier.\n"));
1532 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1533 (void) printf(gettext(" action: The pool can be imported "
1534 "despite missing or damaged devices. The\n\tfault "
1535 "tolerance of the pool may be compromised if imported.\n"));
1536 } else {
1537 switch (reason) {
1538 case ZPOOL_STATUS_VERSION_NEWER:
1539 (void) printf(gettext(" action: The pool cannot be "
1540 "imported. Access the pool on a system running "
1541 "newer\n\tsoftware, or recreate the pool from "
1542 "backup.\n"));
1543 break;
1544 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1545 (void) printf(gettext("action: The pool cannot be "
1546 "imported. Access the pool on a system that "
1547 "supports\n\tthe required feature(s), or recreate "
1548 "the pool from backup.\n"));
1549 break;
1550 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1551 (void) printf(gettext("action: The pool cannot be "
1552 "imported in read-write mode. Import the pool "
1553 "with\n"
1554 "\t\"-o readonly=on\", access the pool on a system "
1555 "that supports the\n\trequired feature(s), or "
1556 "recreate the pool from backup.\n"));
1557 break;
1558 case ZPOOL_STATUS_MISSING_DEV_R:
1559 case ZPOOL_STATUS_MISSING_DEV_NR:
1560 case ZPOOL_STATUS_BAD_GUID_SUM:
1561 (void) printf(gettext(" action: The pool cannot be "
1562 "imported. Attach the missing\n\tdevices and try "
1563 "again.\n"));
1564 break;
1565 default:
1566 (void) printf(gettext(" action: The pool cannot be "
1567 "imported due to damaged devices or data.\n"));
1571 /* Print the comment attached to the pool. */
1572 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1573 (void) printf(gettext("comment: %s\n"), comment);
1576 * If the state is "closed" or "can't open", and the aux state
1577 * is "corrupt data":
1579 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1580 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1581 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1582 if (pool_state == POOL_STATE_DESTROYED)
1583 (void) printf(gettext("\tThe pool was destroyed, "
1584 "but can be imported using the '-Df' flags.\n"));
1585 else if (pool_state != POOL_STATE_EXPORTED)
1586 (void) printf(gettext("\tThe pool may be active on "
1587 "another system, but can be imported using\n\t"
1588 "the '-f' flag.\n"));
1591 if (msgid != NULL)
1592 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
1593 msgid);
1595 (void) printf(gettext(" config:\n\n"));
1597 namewidth = max_width(NULL, nvroot, 0, 0);
1598 if (namewidth < 10)
1599 namewidth = 10;
1601 print_import_config(name, nvroot, namewidth, 0);
1602 if (num_logs(nvroot) > 0)
1603 print_logs(NULL, nvroot, namewidth, B_FALSE);
1605 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1606 (void) printf(gettext("\n\tAdditional devices are known to "
1607 "be part of this pool, though their\n\texact "
1608 "configuration cannot be determined.\n"));
1613 * Perform the import for the given configuration. This passes the heavy
1614 * lifting off to zpool_import_props(), and then mounts the datasets contained
1615 * within the pool.
1617 static int
1618 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1619 nvlist_t *props, int flags)
1621 zpool_handle_t *zhp;
1622 char *name;
1623 uint64_t state;
1624 uint64_t version;
1626 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1627 &name) == 0);
1629 verify(nvlist_lookup_uint64(config,
1630 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1631 verify(nvlist_lookup_uint64(config,
1632 ZPOOL_CONFIG_VERSION, &version) == 0);
1633 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1634 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1635 "is formatted using an unsupported ZFS version\n"), name);
1636 return (1);
1637 } else if (state != POOL_STATE_EXPORTED &&
1638 !(flags & ZFS_IMPORT_ANY_HOST)) {
1639 uint64_t hostid;
1641 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1642 &hostid) == 0) {
1643 if ((unsigned long)hostid != gethostid()) {
1644 char *hostname;
1645 uint64_t timestamp;
1646 time_t t;
1648 verify(nvlist_lookup_string(config,
1649 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1650 verify(nvlist_lookup_uint64(config,
1651 ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1652 t = timestamp;
1653 (void) fprintf(stderr, gettext("cannot import "
1654 "'%s': pool may be in use from other "
1655 "system, it was last accessed by %s "
1656 "(hostid: 0x%lx) on %s"), name, hostname,
1657 (unsigned long)hostid,
1658 asctime(localtime(&t)));
1659 (void) fprintf(stderr, gettext("use '-f' to "
1660 "import anyway\n"));
1661 return (1);
1663 } else {
1664 (void) fprintf(stderr, gettext("cannot import '%s': "
1665 "pool may be in use from other system\n"), name);
1666 (void) fprintf(stderr, gettext("use '-f' to import "
1667 "anyway\n"));
1668 return (1);
1672 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1673 return (1);
1675 if (newname != NULL)
1676 name = (char *)newname;
1678 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1679 return (1);
1681 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1682 !(flags & ZFS_IMPORT_ONLY) &&
1683 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1684 zpool_close(zhp);
1685 return (1);
1688 zpool_close(zhp);
1689 return (0);
1693 * zpool import [-d dir] [-D]
1694 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1695 * [-d dir | -c cachefile] [-f] -a
1696 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1697 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1699 * -c Read pool information from a cachefile instead of searching
1700 * devices.
1702 * -d Scan in a specific directory, other than /dev/dsk. More than
1703 * one directory can be specified using multiple '-d' options.
1705 * -D Scan for previously destroyed pools or import all or only
1706 * specified destroyed pools.
1708 * -R Temporarily import the pool, with all mountpoints relative to
1709 * the given root. The pool will remain exported when the machine
1710 * is rebooted.
1712 * -V Import even in the presence of faulted vdevs. This is an
1713 * intentionally undocumented option for testing purposes, and
1714 * treats the pool configuration as complete, leaving any bad
1715 * vdevs in the FAULTED state. In other words, it does verbatim
1716 * import.
1718 * -f Force import, even if it appears that the pool is active.
1720 * -F Attempt rewind if necessary.
1722 * -n See if rewind would work, but don't actually rewind.
1724 * -N Import the pool but don't mount datasets.
1726 * -T Specify a starting txg to use for import. This option is
1727 * intentionally undocumented option for testing purposes.
1729 * -a Import all pools found.
1731 * -o Set property=value and/or temporary mount options (without '=').
1733 * The import command scans for pools to import, and import pools based on pool
1734 * name and GUID. The pool can also be renamed as part of the import process.
1737 zpool_do_import(int argc, char **argv)
1739 char **searchdirs = NULL;
1740 int nsearch = 0;
1741 int c;
1742 int err = 0;
1743 nvlist_t *pools = NULL;
1744 boolean_t do_all = B_FALSE;
1745 boolean_t do_destroyed = B_FALSE;
1746 char *mntopts = NULL;
1747 nvpair_t *elem;
1748 nvlist_t *config;
1749 uint64_t searchguid = 0;
1750 char *searchname = NULL;
1751 char *propval;
1752 nvlist_t *found_config;
1753 nvlist_t *policy = NULL;
1754 nvlist_t *props = NULL;
1755 boolean_t first;
1756 int flags = ZFS_IMPORT_NORMAL;
1757 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1758 boolean_t dryrun = B_FALSE;
1759 boolean_t do_rewind = B_FALSE;
1760 boolean_t xtreme_rewind = B_FALSE;
1761 uint64_t pool_state, txg = -1ULL;
1762 char *cachefile = NULL;
1763 importargs_t idata = { 0 };
1764 char *endptr;
1766 /* check options */
1767 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1768 switch (c) {
1769 case 'a':
1770 do_all = B_TRUE;
1771 break;
1772 case 'c':
1773 cachefile = optarg;
1774 break;
1775 case 'd':
1776 if (searchdirs == NULL) {
1777 searchdirs = safe_malloc(sizeof (char *));
1778 } else {
1779 char **tmp = safe_malloc((nsearch + 1) *
1780 sizeof (char *));
1781 bcopy(searchdirs, tmp, nsearch *
1782 sizeof (char *));
1783 free(searchdirs);
1784 searchdirs = tmp;
1786 searchdirs[nsearch++] = optarg;
1787 break;
1788 case 'D':
1789 do_destroyed = B_TRUE;
1790 break;
1791 case 'f':
1792 flags |= ZFS_IMPORT_ANY_HOST;
1793 break;
1794 case 'F':
1795 do_rewind = B_TRUE;
1796 break;
1797 case 'm':
1798 flags |= ZFS_IMPORT_MISSING_LOG;
1799 break;
1800 case 'n':
1801 dryrun = B_TRUE;
1802 break;
1803 case 'N':
1804 flags |= ZFS_IMPORT_ONLY;
1805 break;
1806 case 'o':
1807 if ((propval = strchr(optarg, '=')) != NULL) {
1808 *propval = '\0';
1809 propval++;
1810 if (add_prop_list(optarg, propval,
1811 &props, B_TRUE))
1812 goto error;
1813 } else {
1814 mntopts = optarg;
1816 break;
1817 case 'R':
1818 if (add_prop_list(zpool_prop_to_name(
1819 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1820 goto error;
1821 if (nvlist_lookup_string(props,
1822 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1823 &propval) == 0)
1824 break;
1825 if (add_prop_list(zpool_prop_to_name(
1826 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1827 goto error;
1828 break;
1829 case 'T':
1830 errno = 0;
1831 txg = strtoull(optarg, &endptr, 10);
1832 if (errno != 0 || *endptr != '\0') {
1833 (void) fprintf(stderr,
1834 gettext("invalid txg value\n"));
1835 usage(B_FALSE);
1837 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1838 break;
1839 case 'V':
1840 flags |= ZFS_IMPORT_VERBATIM;
1841 break;
1842 case 'X':
1843 xtreme_rewind = B_TRUE;
1844 break;
1845 case ':':
1846 (void) fprintf(stderr, gettext("missing argument for "
1847 "'%c' option\n"), optopt);
1848 usage(B_FALSE);
1849 break;
1850 case '?':
1851 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1852 optopt);
1853 usage(B_FALSE);
1857 argc -= optind;
1858 argv += optind;
1860 if (cachefile && nsearch != 0) {
1861 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1862 usage(B_FALSE);
1865 if ((dryrun || xtreme_rewind) && !do_rewind) {
1866 (void) fprintf(stderr,
1867 gettext("-n or -X only meaningful with -F\n"));
1868 usage(B_FALSE);
1870 if (dryrun)
1871 rewind_policy = ZPOOL_TRY_REWIND;
1872 else if (do_rewind)
1873 rewind_policy = ZPOOL_DO_REWIND;
1874 if (xtreme_rewind)
1875 rewind_policy |= ZPOOL_EXTREME_REWIND;
1877 /* In the future, we can capture further policy and include it here */
1878 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1879 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1880 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1881 goto error;
1883 if (searchdirs == NULL) {
1884 searchdirs = safe_malloc(sizeof (char *));
1885 searchdirs[0] = "/dev/dsk";
1886 nsearch = 1;
1889 /* check argument count */
1890 if (do_all) {
1891 if (argc != 0) {
1892 (void) fprintf(stderr, gettext("too many arguments\n"));
1893 usage(B_FALSE);
1895 } else {
1896 if (argc > 2) {
1897 (void) fprintf(stderr, gettext("too many arguments\n"));
1898 usage(B_FALSE);
1902 * Check for the SYS_CONFIG privilege. We do this explicitly
1903 * here because otherwise any attempt to discover pools will
1904 * silently fail.
1906 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1907 (void) fprintf(stderr, gettext("cannot "
1908 "discover pools: permission denied\n"));
1909 free(searchdirs);
1910 nvlist_free(policy);
1911 return (1);
1916 * Depending on the arguments given, we do one of the following:
1918 * <none> Iterate through all pools and display information about
1919 * each one.
1921 * -a Iterate through all pools and try to import each one.
1923 * <id> Find the pool that corresponds to the given GUID/pool
1924 * name and import that one.
1926 * -D Above options applies only to destroyed pools.
1928 if (argc != 0) {
1929 char *endptr;
1931 errno = 0;
1932 searchguid = strtoull(argv[0], &endptr, 10);
1933 if (errno != 0 || *endptr != '\0')
1934 searchname = argv[0];
1935 found_config = NULL;
1938 * User specified a name or guid. Ensure it's unique.
1940 idata.unique = B_TRUE;
1944 idata.path = searchdirs;
1945 idata.paths = nsearch;
1946 idata.poolname = searchname;
1947 idata.guid = searchguid;
1948 idata.cachefile = cachefile;
1950 pools = zpool_search_import(g_zfs, &idata);
1952 if (pools != NULL && idata.exists &&
1953 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1954 (void) fprintf(stderr, gettext("cannot import '%s': "
1955 "a pool with that name already exists\n"),
1956 argv[0]);
1957 (void) fprintf(stderr, gettext("use the form '%s "
1958 "<pool | id> <newpool>' to give it a new name\n"),
1959 "zpool import");
1960 err = 1;
1961 } else if (pools == NULL && idata.exists) {
1962 (void) fprintf(stderr, gettext("cannot import '%s': "
1963 "a pool with that name is already created/imported,\n"),
1964 argv[0]);
1965 (void) fprintf(stderr, gettext("and no additional pools "
1966 "with that name were found\n"));
1967 err = 1;
1968 } else if (pools == NULL) {
1969 if (argc != 0) {
1970 (void) fprintf(stderr, gettext("cannot import '%s': "
1971 "no such pool available\n"), argv[0]);
1973 err = 1;
1976 if (err == 1) {
1977 free(searchdirs);
1978 nvlist_free(policy);
1979 return (1);
1983 * At this point we have a list of import candidate configs. Even if
1984 * we were searching by pool name or guid, we still need to
1985 * post-process the list to deal with pool state and possible
1986 * duplicate names.
1988 err = 0;
1989 elem = NULL;
1990 first = B_TRUE;
1991 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1993 verify(nvpair_value_nvlist(elem, &config) == 0);
1995 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1996 &pool_state) == 0);
1997 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1998 continue;
1999 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2000 continue;
2002 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2003 policy) == 0);
2005 if (argc == 0) {
2006 if (first)
2007 first = B_FALSE;
2008 else if (!do_all)
2009 (void) printf("\n");
2011 if (do_all) {
2012 err |= do_import(config, NULL, mntopts,
2013 props, flags);
2014 } else {
2015 show_import(config);
2017 } else if (searchname != NULL) {
2018 char *name;
2021 * We are searching for a pool based on name.
2023 verify(nvlist_lookup_string(config,
2024 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2026 if (strcmp(name, searchname) == 0) {
2027 if (found_config != NULL) {
2028 (void) fprintf(stderr, gettext(
2029 "cannot import '%s': more than "
2030 "one matching pool\n"), searchname);
2031 (void) fprintf(stderr, gettext(
2032 "import by numeric ID instead\n"));
2033 err = B_TRUE;
2035 found_config = config;
2037 } else {
2038 uint64_t guid;
2041 * Search for a pool by guid.
2043 verify(nvlist_lookup_uint64(config,
2044 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2046 if (guid == searchguid)
2047 found_config = config;
2052 * If we were searching for a specific pool, verify that we found a
2053 * pool, and then do the import.
2055 if (argc != 0 && err == 0) {
2056 if (found_config == NULL) {
2057 (void) fprintf(stderr, gettext("cannot import '%s': "
2058 "no such pool available\n"), argv[0]);
2059 err = B_TRUE;
2060 } else {
2061 err |= do_import(found_config, argc == 1 ? NULL :
2062 argv[1], mntopts, props, flags);
2067 * If we were just looking for pools, report an error if none were
2068 * found.
2070 if (argc == 0 && first)
2071 (void) fprintf(stderr,
2072 gettext("no pools available to import\n"));
2074 error:
2075 nvlist_free(props);
2076 nvlist_free(pools);
2077 nvlist_free(policy);
2078 free(searchdirs);
2080 return (err ? 1 : 0);
2083 typedef struct iostat_cbdata {
2084 boolean_t cb_verbose;
2085 int cb_namewidth;
2086 int cb_iteration;
2087 zpool_list_t *cb_list;
2088 } iostat_cbdata_t;
2090 static void
2091 print_iostat_separator(iostat_cbdata_t *cb)
2093 int i = 0;
2095 for (i = 0; i < cb->cb_namewidth; i++)
2096 (void) printf("-");
2097 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2100 static void
2101 print_iostat_header(iostat_cbdata_t *cb)
2103 (void) printf("%*s capacity operations bandwidth\n",
2104 cb->cb_namewidth, "");
2105 (void) printf("%-*s alloc free read write read write\n",
2106 cb->cb_namewidth, "pool");
2107 print_iostat_separator(cb);
2111 * Display a single statistic.
2113 static void
2114 print_one_stat(uint64_t value)
2116 char buf[64];
2118 zfs_nicenum(value, buf, sizeof (buf));
2119 (void) printf(" %5s", buf);
2123 * Print out all the statistics for the given vdev. This can either be the
2124 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2125 * is a verbose output, and we don't want to display the toplevel pool stats.
2127 void
2128 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2129 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2131 nvlist_t **oldchild, **newchild;
2132 uint_t c, children;
2133 vdev_stat_t *oldvs, *newvs;
2134 vdev_stat_t zerovs = { 0 };
2135 uint64_t tdelta;
2136 double scale;
2137 char *vname;
2139 if (oldnv != NULL) {
2140 verify(nvlist_lookup_uint64_array(oldnv,
2141 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2142 } else {
2143 oldvs = &zerovs;
2146 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2147 (uint64_t **)&newvs, &c) == 0);
2149 if (strlen(name) + depth > cb->cb_namewidth)
2150 (void) printf("%*s%s", depth, "", name);
2151 else
2152 (void) printf("%*s%s%*s", depth, "", name,
2153 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2155 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2157 if (tdelta == 0)
2158 scale = 1.0;
2159 else
2160 scale = (double)NANOSEC / tdelta;
2162 /* only toplevel vdevs have capacity stats */
2163 if (newvs->vs_space == 0) {
2164 (void) printf(" - -");
2165 } else {
2166 print_one_stat(newvs->vs_alloc);
2167 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2170 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2171 oldvs->vs_ops[ZIO_TYPE_READ])));
2173 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2174 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2176 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2177 oldvs->vs_bytes[ZIO_TYPE_READ])));
2179 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2180 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2182 (void) printf("\n");
2184 if (!cb->cb_verbose)
2185 return;
2187 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2188 &newchild, &children) != 0)
2189 return;
2191 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2192 &oldchild, &c) != 0)
2193 return;
2195 for (c = 0; c < children; c++) {
2196 uint64_t ishole = B_FALSE, islog = B_FALSE;
2198 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2199 &ishole);
2201 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2202 &islog);
2204 if (ishole || islog)
2205 continue;
2207 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2208 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2209 newchild[c], cb, depth + 2);
2210 free(vname);
2214 * Log device section
2217 if (num_logs(newnv) > 0) {
2218 (void) printf("%-*s - - - - - "
2219 "-\n", cb->cb_namewidth, "logs");
2221 for (c = 0; c < children; c++) {
2222 uint64_t islog = B_FALSE;
2223 (void) nvlist_lookup_uint64(newchild[c],
2224 ZPOOL_CONFIG_IS_LOG, &islog);
2226 if (islog) {
2227 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2228 B_FALSE);
2229 print_vdev_stats(zhp, vname, oldnv ?
2230 oldchild[c] : NULL, newchild[c],
2231 cb, depth + 2);
2232 free(vname);
2239 * Include level 2 ARC devices in iostat output
2241 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2242 &newchild, &children) != 0)
2243 return;
2245 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2246 &oldchild, &c) != 0)
2247 return;
2249 if (children > 0) {
2250 (void) printf("%-*s - - - - - "
2251 "-\n", cb->cb_namewidth, "cache");
2252 for (c = 0; c < children; c++) {
2253 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2254 B_FALSE);
2255 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2256 newchild[c], cb, depth + 2);
2257 free(vname);
2262 static int
2263 refresh_iostat(zpool_handle_t *zhp, void *data)
2265 iostat_cbdata_t *cb = data;
2266 boolean_t missing;
2269 * If the pool has disappeared, remove it from the list and continue.
2271 if (zpool_refresh_stats(zhp, &missing) != 0)
2272 return (-1);
2274 if (missing)
2275 pool_list_remove(cb->cb_list, zhp);
2277 return (0);
2281 * Callback to print out the iostats for the given pool.
2284 print_iostat(zpool_handle_t *zhp, void *data)
2286 iostat_cbdata_t *cb = data;
2287 nvlist_t *oldconfig, *newconfig;
2288 nvlist_t *oldnvroot, *newnvroot;
2290 newconfig = zpool_get_config(zhp, &oldconfig);
2292 if (cb->cb_iteration == 1)
2293 oldconfig = NULL;
2295 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2296 &newnvroot) == 0);
2298 if (oldconfig == NULL)
2299 oldnvroot = NULL;
2300 else
2301 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2302 &oldnvroot) == 0);
2305 * Print out the statistics for the pool.
2307 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2309 if (cb->cb_verbose)
2310 print_iostat_separator(cb);
2312 return (0);
2316 get_namewidth(zpool_handle_t *zhp, void *data)
2318 iostat_cbdata_t *cb = data;
2319 nvlist_t *config, *nvroot;
2321 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2322 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2323 &nvroot) == 0);
2324 if (!cb->cb_verbose)
2325 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2326 else
2327 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2328 cb->cb_namewidth);
2332 * The width must fall into the range [10,38]. The upper limit is the
2333 * maximum we can have and still fit in 80 columns.
2335 if (cb->cb_namewidth < 10)
2336 cb->cb_namewidth = 10;
2337 if (cb->cb_namewidth > 38)
2338 cb->cb_namewidth = 38;
2340 return (0);
2344 * Parse the input string, get the 'interval' and 'count' value if there is one.
2346 static void
2347 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2348 unsigned long *cnt)
2350 unsigned long interval = 0, count = 0;
2351 int argc = *argcp, errno;
2354 * Determine if the last argument is an integer or a pool name
2356 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2357 char *end;
2359 errno = 0;
2360 interval = strtoul(argv[argc - 1], &end, 10);
2362 if (*end == '\0' && errno == 0) {
2363 if (interval == 0) {
2364 (void) fprintf(stderr, gettext("interval "
2365 "cannot be zero\n"));
2366 usage(B_FALSE);
2369 * Ignore the last parameter
2371 argc--;
2372 } else {
2374 * If this is not a valid number, just plow on. The
2375 * user will get a more informative error message later
2376 * on.
2378 interval = 0;
2383 * If the last argument is also an integer, then we have both a count
2384 * and an interval.
2386 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2387 char *end;
2389 errno = 0;
2390 count = interval;
2391 interval = strtoul(argv[argc - 1], &end, 10);
2393 if (*end == '\0' && errno == 0) {
2394 if (interval == 0) {
2395 (void) fprintf(stderr, gettext("interval "
2396 "cannot be zero\n"));
2397 usage(B_FALSE);
2401 * Ignore the last parameter
2403 argc--;
2404 } else {
2405 interval = 0;
2409 *iv = interval;
2410 *cnt = count;
2411 *argcp = argc;
2414 static void
2415 get_timestamp_arg(char c)
2417 if (c == 'u')
2418 timestamp_fmt = UDATE;
2419 else if (c == 'd')
2420 timestamp_fmt = DDATE;
2421 else
2422 usage(B_FALSE);
2426 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2428 * -v Display statistics for individual vdevs
2429 * -T Display a timestamp in date(1) or Unix format
2431 * This command can be tricky because we want to be able to deal with pool
2432 * creation/destruction as well as vdev configuration changes. The bulk of this
2433 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2434 * on pool_list_update() to detect the addition of new pools. Configuration
2435 * changes are all handled within libzfs.
2438 zpool_do_iostat(int argc, char **argv)
2440 int c;
2441 int ret;
2442 int npools;
2443 unsigned long interval = 0, count = 0;
2444 zpool_list_t *list;
2445 boolean_t verbose = B_FALSE;
2446 iostat_cbdata_t cb;
2448 /* check options */
2449 while ((c = getopt(argc, argv, "T:v")) != -1) {
2450 switch (c) {
2451 case 'T':
2452 get_timestamp_arg(*optarg);
2453 break;
2454 case 'v':
2455 verbose = B_TRUE;
2456 break;
2457 case '?':
2458 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2459 optopt);
2460 usage(B_FALSE);
2464 argc -= optind;
2465 argv += optind;
2467 get_interval_count(&argc, argv, &interval, &count);
2470 * Construct the list of all interesting pools.
2472 ret = 0;
2473 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2474 return (1);
2476 if (pool_list_count(list) == 0 && argc != 0) {
2477 pool_list_free(list);
2478 return (1);
2481 if (pool_list_count(list) == 0 && interval == 0) {
2482 pool_list_free(list);
2483 (void) fprintf(stderr, gettext("no pools available\n"));
2484 return (1);
2488 * Enter the main iostat loop.
2490 cb.cb_list = list;
2491 cb.cb_verbose = verbose;
2492 cb.cb_iteration = 0;
2493 cb.cb_namewidth = 0;
2495 for (;;) {
2496 pool_list_update(list);
2498 if ((npools = pool_list_count(list)) == 0)
2499 break;
2502 * Refresh all statistics. This is done as an explicit step
2503 * before calculating the maximum name width, so that any
2504 * configuration changes are properly accounted for.
2506 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2509 * Iterate over all pools to determine the maximum width
2510 * for the pool / device name column across all pools.
2512 cb.cb_namewidth = 0;
2513 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2515 if (timestamp_fmt != NODATE)
2516 print_timestamp(timestamp_fmt);
2519 * If it's the first time, or verbose mode, print the header.
2521 if (++cb.cb_iteration == 1 || verbose)
2522 print_iostat_header(&cb);
2524 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2527 * If there's more than one pool, and we're not in verbose mode
2528 * (which prints a separator for us), then print a separator.
2530 if (npools > 1 && !verbose)
2531 print_iostat_separator(&cb);
2533 if (verbose)
2534 (void) printf("\n");
2537 * Flush the output so that redirection to a file isn't buffered
2538 * indefinitely.
2540 (void) fflush(stdout);
2542 if (interval == 0)
2543 break;
2545 if (count != 0 && --count == 0)
2546 break;
2548 (void) sleep(interval);
2551 pool_list_free(list);
2553 return (ret);
2556 typedef struct list_cbdata {
2557 boolean_t cb_verbose;
2558 int cb_namewidth;
2559 boolean_t cb_scripted;
2560 zprop_list_t *cb_proplist;
2561 } list_cbdata_t;
2564 * Given a list of columns to display, output appropriate headers for each one.
2566 static void
2567 print_header(list_cbdata_t *cb)
2569 zprop_list_t *pl = cb->cb_proplist;
2570 char headerbuf[ZPOOL_MAXPROPLEN];
2571 const char *header;
2572 boolean_t first = B_TRUE;
2573 boolean_t right_justify;
2574 size_t width = 0;
2576 for (; pl != NULL; pl = pl->pl_next) {
2577 width = pl->pl_width;
2578 if (first && cb->cb_verbose) {
2580 * Reset the width to accommodate the verbose listing
2581 * of devices.
2583 width = cb->cb_namewidth;
2586 if (!first)
2587 (void) printf(" ");
2588 else
2589 first = B_FALSE;
2591 right_justify = B_FALSE;
2592 if (pl->pl_prop != ZPROP_INVAL) {
2593 header = zpool_prop_column_name(pl->pl_prop);
2594 right_justify = zpool_prop_align_right(pl->pl_prop);
2595 } else {
2596 int i;
2598 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2599 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2600 headerbuf[i] = '\0';
2601 header = headerbuf;
2604 if (pl->pl_next == NULL && !right_justify)
2605 (void) printf("%s", header);
2606 else if (right_justify)
2607 (void) printf("%*s", width, header);
2608 else
2609 (void) printf("%-*s", width, header);
2613 (void) printf("\n");
2617 * Given a pool and a list of properties, print out all the properties according
2618 * to the described layout.
2620 static void
2621 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2623 zprop_list_t *pl = cb->cb_proplist;
2624 boolean_t first = B_TRUE;
2625 char property[ZPOOL_MAXPROPLEN];
2626 char *propstr;
2627 boolean_t right_justify;
2628 size_t width;
2630 for (; pl != NULL; pl = pl->pl_next) {
2632 width = pl->pl_width;
2633 if (first && cb->cb_verbose) {
2635 * Reset the width to accommodate the verbose listing
2636 * of devices.
2638 width = cb->cb_namewidth;
2641 if (!first) {
2642 if (cb->cb_scripted)
2643 (void) printf("\t");
2644 else
2645 (void) printf(" ");
2646 } else {
2647 first = B_FALSE;
2650 right_justify = B_FALSE;
2651 if (pl->pl_prop != ZPROP_INVAL) {
2652 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2653 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2654 propstr = "-";
2655 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2656 sizeof (property), NULL) != 0)
2657 propstr = "-";
2658 else
2659 propstr = property;
2661 right_justify = zpool_prop_align_right(pl->pl_prop);
2662 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2663 zpool_prop_unsupported(pl->pl_user_prop)) &&
2664 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2665 sizeof (property)) == 0) {
2666 propstr = property;
2667 } else {
2668 propstr = "-";
2673 * If this is being called in scripted mode, or if this is the
2674 * last column and it is left-justified, don't include a width
2675 * format specifier.
2677 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2678 (void) printf("%s", propstr);
2679 else if (right_justify)
2680 (void) printf("%*s", width, propstr);
2681 else
2682 (void) printf("%-*s", width, propstr);
2685 (void) printf("\n");
2688 static void
2689 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2691 char propval[64];
2692 boolean_t fixed;
2693 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2695 zfs_nicenum(value, propval, sizeof (propval));
2697 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2698 (void) strlcpy(propval, "-", sizeof (propval));
2700 if (scripted)
2701 (void) printf("\t%s", propval);
2702 else
2703 (void) printf(" %*s", width, propval);
2706 void
2707 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2708 list_cbdata_t *cb, int depth)
2710 nvlist_t **child;
2711 vdev_stat_t *vs;
2712 uint_t c, children;
2713 char *vname;
2714 boolean_t scripted = cb->cb_scripted;
2716 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2717 (uint64_t **)&vs, &c) == 0);
2719 if (name != NULL) {
2720 if (scripted)
2721 (void) printf("\t%s", name);
2722 else if (strlen(name) + depth > cb->cb_namewidth)
2723 (void) printf("%*s%s", depth, "", name);
2724 else
2725 (void) printf("%*s%s%*s", depth, "", name,
2726 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2728 /* only toplevel vdevs have capacity stats */
2729 if (vs->vs_space == 0) {
2730 if (scripted)
2731 (void) printf("\t-\t-\t-");
2732 else
2733 (void) printf(" - - -");
2734 } else {
2735 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
2736 scripted);
2737 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
2738 scripted);
2739 print_one_column(ZPOOL_PROP_FREE,
2740 vs->vs_space - vs->vs_alloc, scripted);
2742 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
2743 scripted);
2744 (void) printf("\n");
2747 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2748 &child, &children) != 0)
2749 return;
2751 for (c = 0; c < children; c++) {
2752 uint64_t ishole = B_FALSE;
2754 if (nvlist_lookup_uint64(child[c],
2755 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
2756 continue;
2758 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
2759 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2760 free(vname);
2764 * Include level 2 ARC devices in iostat output
2766 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2767 &child, &children) != 0)
2768 return;
2770 if (children > 0) {
2771 (void) printf("%-*s - - - - - "
2772 "-\n", cb->cb_namewidth, "cache");
2773 for (c = 0; c < children; c++) {
2774 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2775 B_FALSE);
2776 print_list_stats(zhp, vname, child[c], cb, depth + 2);
2777 free(vname);
2784 * Generic callback function to list a pool.
2787 list_callback(zpool_handle_t *zhp, void *data)
2789 list_cbdata_t *cbp = data;
2790 nvlist_t *config;
2791 nvlist_t *nvroot;
2793 config = zpool_get_config(zhp, NULL);
2795 print_pool(zhp, cbp);
2796 if (!cbp->cb_verbose)
2797 return (0);
2799 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2800 &nvroot) == 0);
2801 print_list_stats(zhp, NULL, nvroot, cbp, 0);
2803 return (0);
2807 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2809 * -H Scripted mode. Don't display headers, and separate properties
2810 * by a single tab.
2811 * -o List of properties to display. Defaults to
2812 * "name,size,allocated,free,capacity,health,altroot"
2813 * -T Display a timestamp in date(1) or Unix format
2815 * List all pools in the system, whether or not they're healthy. Output space
2816 * statistics for each one, as well as health status summary.
2819 zpool_do_list(int argc, char **argv)
2821 int c;
2822 int ret;
2823 list_cbdata_t cb = { 0 };
2824 static char default_props[] =
2825 "name,size,allocated,free,expandsize,capacity,dedupratio,"
2826 "health,altroot";
2827 char *props = default_props;
2828 unsigned long interval = 0, count = 0;
2829 zpool_list_t *list;
2830 boolean_t first = B_TRUE;
2832 /* check options */
2833 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
2834 switch (c) {
2835 case 'H':
2836 cb.cb_scripted = B_TRUE;
2837 break;
2838 case 'o':
2839 props = optarg;
2840 break;
2841 case 'T':
2842 get_timestamp_arg(*optarg);
2843 break;
2844 case 'v':
2845 cb.cb_verbose = B_TRUE;
2846 break;
2847 case ':':
2848 (void) fprintf(stderr, gettext("missing argument for "
2849 "'%c' option\n"), optopt);
2850 usage(B_FALSE);
2851 break;
2852 case '?':
2853 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2854 optopt);
2855 usage(B_FALSE);
2859 argc -= optind;
2860 argv += optind;
2862 get_interval_count(&argc, argv, &interval, &count);
2864 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2865 usage(B_FALSE);
2867 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
2868 return (1);
2870 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
2871 (void) printf(gettext("no pools available\n"));
2872 zprop_free_list(cb.cb_proplist);
2873 return (0);
2876 for (;;) {
2877 pool_list_update(list);
2879 if (pool_list_count(list) == 0)
2880 break;
2882 cb.cb_namewidth = 0;
2883 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2885 if (timestamp_fmt != NODATE)
2886 print_timestamp(timestamp_fmt);
2888 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
2889 print_header(&cb);
2890 first = B_FALSE;
2892 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
2894 if (interval == 0)
2895 break;
2897 if (count != 0 && --count == 0)
2898 break;
2900 (void) sleep(interval);
2903 zprop_free_list(cb.cb_proplist);
2904 return (ret);
2907 static nvlist_t *
2908 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2910 nvlist_t **child;
2911 uint_t c, children;
2912 nvlist_t *match;
2913 char *path;
2915 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2916 &child, &children) != 0) {
2917 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2918 if (strncmp(name, "/dev/dsk/", 9) == 0)
2919 name += 9;
2920 if (strncmp(path, "/dev/dsk/", 9) == 0)
2921 path += 9;
2922 if (strcmp(name, path) == 0)
2923 return (nv);
2924 return (NULL);
2927 for (c = 0; c < children; c++)
2928 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2929 return (match);
2931 return (NULL);
2934 static int
2935 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2937 boolean_t force = B_FALSE;
2938 int c;
2939 nvlist_t *nvroot;
2940 char *poolname, *old_disk, *new_disk;
2941 zpool_handle_t *zhp;
2942 int ret;
2944 /* check options */
2945 while ((c = getopt(argc, argv, "f")) != -1) {
2946 switch (c) {
2947 case 'f':
2948 force = B_TRUE;
2949 break;
2950 case '?':
2951 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2952 optopt);
2953 usage(B_FALSE);
2957 argc -= optind;
2958 argv += optind;
2960 /* get pool name and check number of arguments */
2961 if (argc < 1) {
2962 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2963 usage(B_FALSE);
2966 poolname = argv[0];
2968 if (argc < 2) {
2969 (void) fprintf(stderr,
2970 gettext("missing <device> specification\n"));
2971 usage(B_FALSE);
2974 old_disk = argv[1];
2976 if (argc < 3) {
2977 if (!replacing) {
2978 (void) fprintf(stderr,
2979 gettext("missing <new_device> specification\n"));
2980 usage(B_FALSE);
2982 new_disk = old_disk;
2983 argc -= 1;
2984 argv += 1;
2985 } else {
2986 new_disk = argv[2];
2987 argc -= 2;
2988 argv += 2;
2991 if (argc > 1) {
2992 (void) fprintf(stderr, gettext("too many arguments\n"));
2993 usage(B_FALSE);
2996 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2997 return (1);
2999 if (zpool_get_config(zhp, NULL) == NULL) {
3000 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3001 poolname);
3002 zpool_close(zhp);
3003 return (1);
3006 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
3007 argc, argv);
3008 if (nvroot == NULL) {
3009 zpool_close(zhp);
3010 return (1);
3013 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3015 nvlist_free(nvroot);
3016 zpool_close(zhp);
3018 return (ret);
3022 * zpool replace [-f] <pool> <device> <new_device>
3024 * -f Force attach, even if <new_device> appears to be in use.
3026 * Replace <device> with <new_device>.
3028 /* ARGSUSED */
3030 zpool_do_replace(int argc, char **argv)
3032 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3036 * zpool attach [-f] <pool> <device> <new_device>
3038 * -f Force attach, even if <new_device> appears to be in use.
3040 * Attach <new_device> to the mirror containing <device>. If <device> is not
3041 * part of a mirror, then <device> will be transformed into a mirror of
3042 * <device> and <new_device>. In either case, <new_device> will begin life
3043 * with a DTL of [0, now], and will immediately begin to resilver itself.
3046 zpool_do_attach(int argc, char **argv)
3048 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3052 * zpool detach [-f] <pool> <device>
3054 * -f Force detach of <device>, even if DTLs argue against it
3055 * (not supported yet)
3057 * Detach a device from a mirror. The operation will be refused if <device>
3058 * is the last device in the mirror, or if the DTLs indicate that this device
3059 * has the only valid copy of some data.
3061 /* ARGSUSED */
3063 zpool_do_detach(int argc, char **argv)
3065 int c;
3066 char *poolname, *path;
3067 zpool_handle_t *zhp;
3068 int ret;
3070 /* check options */
3071 while ((c = getopt(argc, argv, "f")) != -1) {
3072 switch (c) {
3073 case 'f':
3074 case '?':
3075 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3076 optopt);
3077 usage(B_FALSE);
3081 argc -= optind;
3082 argv += optind;
3084 /* get pool name and check number of arguments */
3085 if (argc < 1) {
3086 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3087 usage(B_FALSE);
3090 if (argc < 2) {
3091 (void) fprintf(stderr,
3092 gettext("missing <device> specification\n"));
3093 usage(B_FALSE);
3096 poolname = argv[0];
3097 path = argv[1];
3099 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3100 return (1);
3102 ret = zpool_vdev_detach(zhp, path);
3104 zpool_close(zhp);
3106 return (ret);
3110 * zpool split [-n] [-o prop=val] ...
3111 * [-o mntopt] ...
3112 * [-R altroot] <pool> <newpool> [<device> ...]
3114 * -n Do not split the pool, but display the resulting layout if
3115 * it were to be split.
3116 * -o Set property=value, or set mount options.
3117 * -R Mount the split-off pool under an alternate root.
3119 * Splits the named pool and gives it the new pool name. Devices to be split
3120 * off may be listed, provided that no more than one device is specified
3121 * per top-level vdev mirror. The newly split pool is left in an exported
3122 * state unless -R is specified.
3124 * Restrictions: the top-level of the pool pool must only be made up of
3125 * mirrors; all devices in the pool must be healthy; no device may be
3126 * undergoing a resilvering operation.
3129 zpool_do_split(int argc, char **argv)
3131 char *srcpool, *newpool, *propval;
3132 char *mntopts = NULL;
3133 splitflags_t flags;
3134 int c, ret = 0;
3135 zpool_handle_t *zhp;
3136 nvlist_t *config, *props = NULL;
3138 flags.dryrun = B_FALSE;
3139 flags.import = B_FALSE;
3141 /* check options */
3142 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3143 switch (c) {
3144 case 'R':
3145 flags.import = B_TRUE;
3146 if (add_prop_list(
3147 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3148 &props, B_TRUE) != 0) {
3149 if (props)
3150 nvlist_free(props);
3151 usage(B_FALSE);
3153 break;
3154 case 'n':
3155 flags.dryrun = B_TRUE;
3156 break;
3157 case 'o':
3158 if ((propval = strchr(optarg, '=')) != NULL) {
3159 *propval = '\0';
3160 propval++;
3161 if (add_prop_list(optarg, propval,
3162 &props, B_TRUE) != 0) {
3163 if (props)
3164 nvlist_free(props);
3165 usage(B_FALSE);
3167 } else {
3168 mntopts = optarg;
3170 break;
3171 case ':':
3172 (void) fprintf(stderr, gettext("missing argument for "
3173 "'%c' option\n"), optopt);
3174 usage(B_FALSE);
3175 break;
3176 case '?':
3177 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3178 optopt);
3179 usage(B_FALSE);
3180 break;
3184 if (!flags.import && mntopts != NULL) {
3185 (void) fprintf(stderr, gettext("setting mntopts is only "
3186 "valid when importing the pool\n"));
3187 usage(B_FALSE);
3190 argc -= optind;
3191 argv += optind;
3193 if (argc < 1) {
3194 (void) fprintf(stderr, gettext("Missing pool name\n"));
3195 usage(B_FALSE);
3197 if (argc < 2) {
3198 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3199 usage(B_FALSE);
3202 srcpool = argv[0];
3203 newpool = argv[1];
3205 argc -= 2;
3206 argv += 2;
3208 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3209 return (1);
3211 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3212 if (config == NULL) {
3213 ret = 1;
3214 } else {
3215 if (flags.dryrun) {
3216 (void) printf(gettext("would create '%s' with the "
3217 "following layout:\n\n"), newpool);
3218 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3220 nvlist_free(config);
3223 zpool_close(zhp);
3225 if (ret != 0 || flags.dryrun || !flags.import)
3226 return (ret);
3229 * The split was successful. Now we need to open the new
3230 * pool and import it.
3232 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3233 return (1);
3234 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3235 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3236 ret = 1;
3237 (void) fprintf(stderr, gettext("Split was succssful, but "
3238 "the datasets could not all be mounted\n"));
3239 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3240 "different altroot\n"), "zpool import");
3242 zpool_close(zhp);
3244 return (ret);
3250 * zpool online <pool> <device> ...
3253 zpool_do_online(int argc, char **argv)
3255 int c, i;
3256 char *poolname;
3257 zpool_handle_t *zhp;
3258 int ret = 0;
3259 vdev_state_t newstate;
3260 int flags = 0;
3262 /* check options */
3263 while ((c = getopt(argc, argv, "et")) != -1) {
3264 switch (c) {
3265 case 'e':
3266 flags |= ZFS_ONLINE_EXPAND;
3267 break;
3268 case 't':
3269 case '?':
3270 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3271 optopt);
3272 usage(B_FALSE);
3276 argc -= optind;
3277 argv += optind;
3279 /* get pool name and check number of arguments */
3280 if (argc < 1) {
3281 (void) fprintf(stderr, gettext("missing pool name\n"));
3282 usage(B_FALSE);
3284 if (argc < 2) {
3285 (void) fprintf(stderr, gettext("missing device name\n"));
3286 usage(B_FALSE);
3289 poolname = argv[0];
3291 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3292 return (1);
3294 for (i = 1; i < argc; i++) {
3295 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3296 if (newstate != VDEV_STATE_HEALTHY) {
3297 (void) printf(gettext("warning: device '%s' "
3298 "onlined, but remains in faulted state\n"),
3299 argv[i]);
3300 if (newstate == VDEV_STATE_FAULTED)
3301 (void) printf(gettext("use 'zpool "
3302 "clear' to restore a faulted "
3303 "device\n"));
3304 else
3305 (void) printf(gettext("use 'zpool "
3306 "replace' to replace devices "
3307 "that are no longer present\n"));
3309 } else {
3310 ret = 1;
3314 zpool_close(zhp);
3316 return (ret);
3320 * zpool offline [-ft] <pool> <device> ...
3322 * -f Force the device into the offline state, even if doing
3323 * so would appear to compromise pool availability.
3324 * (not supported yet)
3326 * -t Only take the device off-line temporarily. The offline
3327 * state will not be persistent across reboots.
3329 /* ARGSUSED */
3331 zpool_do_offline(int argc, char **argv)
3333 int c, i;
3334 char *poolname;
3335 zpool_handle_t *zhp;
3336 int ret = 0;
3337 boolean_t istmp = B_FALSE;
3339 /* check options */
3340 while ((c = getopt(argc, argv, "ft")) != -1) {
3341 switch (c) {
3342 case 't':
3343 istmp = B_TRUE;
3344 break;
3345 case 'f':
3346 case '?':
3347 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3348 optopt);
3349 usage(B_FALSE);
3353 argc -= optind;
3354 argv += optind;
3356 /* get pool name and check number of arguments */
3357 if (argc < 1) {
3358 (void) fprintf(stderr, gettext("missing pool name\n"));
3359 usage(B_FALSE);
3361 if (argc < 2) {
3362 (void) fprintf(stderr, gettext("missing device name\n"));
3363 usage(B_FALSE);
3366 poolname = argv[0];
3368 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3369 return (1);
3371 for (i = 1; i < argc; i++) {
3372 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3373 ret = 1;
3376 zpool_close(zhp);
3378 return (ret);
3382 * zpool clear <pool> [device]
3384 * Clear all errors associated with a pool or a particular device.
3387 zpool_do_clear(int argc, char **argv)
3389 int c;
3390 int ret = 0;
3391 boolean_t dryrun = B_FALSE;
3392 boolean_t do_rewind = B_FALSE;
3393 boolean_t xtreme_rewind = B_FALSE;
3394 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3395 nvlist_t *policy = NULL;
3396 zpool_handle_t *zhp;
3397 char *pool, *device;
3399 /* check options */
3400 while ((c = getopt(argc, argv, "FnX")) != -1) {
3401 switch (c) {
3402 case 'F':
3403 do_rewind = B_TRUE;
3404 break;
3405 case 'n':
3406 dryrun = B_TRUE;
3407 break;
3408 case 'X':
3409 xtreme_rewind = B_TRUE;
3410 break;
3411 case '?':
3412 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3413 optopt);
3414 usage(B_FALSE);
3418 argc -= optind;
3419 argv += optind;
3421 if (argc < 1) {
3422 (void) fprintf(stderr, gettext("missing pool name\n"));
3423 usage(B_FALSE);
3426 if (argc > 2) {
3427 (void) fprintf(stderr, gettext("too many arguments\n"));
3428 usage(B_FALSE);
3431 if ((dryrun || xtreme_rewind) && !do_rewind) {
3432 (void) fprintf(stderr,
3433 gettext("-n or -X only meaningful with -F\n"));
3434 usage(B_FALSE);
3436 if (dryrun)
3437 rewind_policy = ZPOOL_TRY_REWIND;
3438 else if (do_rewind)
3439 rewind_policy = ZPOOL_DO_REWIND;
3440 if (xtreme_rewind)
3441 rewind_policy |= ZPOOL_EXTREME_REWIND;
3443 /* In future, further rewind policy choices can be passed along here */
3444 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3445 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3446 return (1);
3448 pool = argv[0];
3449 device = argc == 2 ? argv[1] : NULL;
3451 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3452 nvlist_free(policy);
3453 return (1);
3456 if (zpool_clear(zhp, device, policy) != 0)
3457 ret = 1;
3459 zpool_close(zhp);
3461 nvlist_free(policy);
3463 return (ret);
3467 * zpool reguid <pool>
3470 zpool_do_reguid(int argc, char **argv)
3472 int c;
3473 char *poolname;
3474 zpool_handle_t *zhp;
3475 int ret = 0;
3477 /* check options */
3478 while ((c = getopt(argc, argv, "")) != -1) {
3479 switch (c) {
3480 case '?':
3481 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3482 optopt);
3483 usage(B_FALSE);
3487 argc -= optind;
3488 argv += optind;
3490 /* get pool name and check number of arguments */
3491 if (argc < 1) {
3492 (void) fprintf(stderr, gettext("missing pool name\n"));
3493 usage(B_FALSE);
3496 if (argc > 1) {
3497 (void) fprintf(stderr, gettext("too many arguments\n"));
3498 usage(B_FALSE);
3501 poolname = argv[0];
3502 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3503 return (1);
3505 ret = zpool_reguid(zhp);
3507 zpool_close(zhp);
3508 return (ret);
3513 * zpool reopen <pool>
3515 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3517 * NOTE: This command is currently undocumented. If the command is ever
3518 * exposed then the appropriate usage() messages will need to be made.
3521 zpool_do_reopen(int argc, char **argv)
3523 int ret = 0;
3524 zpool_handle_t *zhp;
3525 char *pool;
3527 argc--;
3528 argv++;
3530 if (argc != 1)
3531 return (2);
3533 pool = argv[0];
3534 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3535 return (1);
3537 ret = zpool_reopen(zhp);
3538 zpool_close(zhp);
3539 return (ret);
3542 typedef struct scrub_cbdata {
3543 int cb_type;
3544 int cb_argc;
3545 char **cb_argv;
3546 } scrub_cbdata_t;
3549 scrub_callback(zpool_handle_t *zhp, void *data)
3551 scrub_cbdata_t *cb = data;
3552 int err;
3555 * Ignore faulted pools.
3557 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3558 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3559 "currently unavailable\n"), zpool_get_name(zhp));
3560 return (1);
3563 err = zpool_scan(zhp, cb->cb_type);
3565 return (err != 0);
3569 * zpool scrub [-s] <pool> ...
3571 * -s Stop. Stops any in-progress scrub.
3574 zpool_do_scrub(int argc, char **argv)
3576 int c;
3577 scrub_cbdata_t cb;
3579 cb.cb_type = POOL_SCAN_SCRUB;
3581 /* check options */
3582 while ((c = getopt(argc, argv, "s")) != -1) {
3583 switch (c) {
3584 case 's':
3585 cb.cb_type = POOL_SCAN_NONE;
3586 break;
3587 case '?':
3588 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3589 optopt);
3590 usage(B_FALSE);
3594 cb.cb_argc = argc;
3595 cb.cb_argv = argv;
3596 argc -= optind;
3597 argv += optind;
3599 if (argc < 1) {
3600 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3601 usage(B_FALSE);
3604 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3607 typedef struct status_cbdata {
3608 int cb_count;
3609 boolean_t cb_allpools;
3610 boolean_t cb_verbose;
3611 boolean_t cb_explain;
3612 boolean_t cb_first;
3613 boolean_t cb_dedup_stats;
3614 } status_cbdata_t;
3617 * Print out detailed scrub status.
3619 void
3620 print_scan_status(pool_scan_stat_t *ps)
3622 time_t start, end;
3623 uint64_t elapsed, mins_left, hours_left;
3624 uint64_t pass_exam, examined, total;
3625 uint_t rate;
3626 double fraction_done;
3627 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3629 (void) printf(gettext(" scan: "));
3631 /* If there's never been a scan, there's not much to say. */
3632 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3633 ps->pss_func >= POOL_SCAN_FUNCS) {
3634 (void) printf(gettext("none requested\n"));
3635 return;
3638 start = ps->pss_start_time;
3639 end = ps->pss_end_time;
3640 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3642 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3643 ps->pss_func == POOL_SCAN_RESILVER);
3645 * Scan is finished or canceled.
3647 if (ps->pss_state == DSS_FINISHED) {
3648 uint64_t minutes_taken = (end - start) / 60;
3649 char *fmt;
3651 if (ps->pss_func == POOL_SCAN_SCRUB) {
3652 fmt = gettext("scrub repaired %s in %lluh%um with "
3653 "%llu errors on %s");
3654 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3655 fmt = gettext("resilvered %s in %lluh%um with "
3656 "%llu errors on %s");
3658 /* LINTED */
3659 (void) printf(fmt, processed_buf,
3660 (u_longlong_t)(minutes_taken / 60),
3661 (uint_t)(minutes_taken % 60),
3662 (u_longlong_t)ps->pss_errors,
3663 ctime((time_t *)&end));
3664 return;
3665 } else if (ps->pss_state == DSS_CANCELED) {
3666 if (ps->pss_func == POOL_SCAN_SCRUB) {
3667 (void) printf(gettext("scrub canceled on %s"),
3668 ctime(&end));
3669 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3670 (void) printf(gettext("resilver canceled on %s"),
3671 ctime(&end));
3673 return;
3676 assert(ps->pss_state == DSS_SCANNING);
3679 * Scan is in progress.
3681 if (ps->pss_func == POOL_SCAN_SCRUB) {
3682 (void) printf(gettext("scrub in progress since %s"),
3683 ctime(&start));
3684 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3685 (void) printf(gettext("resilver in progress since %s"),
3686 ctime(&start));
3689 examined = ps->pss_examined ? ps->pss_examined : 1;
3690 total = ps->pss_to_examine;
3691 fraction_done = (double)examined / total;
3693 /* elapsed time for this pass */
3694 elapsed = time(NULL) - ps->pss_pass_start;
3695 elapsed = elapsed ? elapsed : 1;
3696 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3697 rate = pass_exam / elapsed;
3698 rate = rate ? rate : 1;
3699 mins_left = ((total - examined) / rate) / 60;
3700 hours_left = mins_left / 60;
3702 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3703 zfs_nicenum(total, total_buf, sizeof (total_buf));
3704 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3707 * do not print estimated time if hours_left is more than 30 days
3709 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3710 examined_buf, total_buf, rate_buf);
3711 if (hours_left < (30 * 24)) {
3712 (void) printf(gettext(", %lluh%um to go\n"),
3713 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3714 } else {
3715 (void) printf(gettext(
3716 ", (scan is slow, no estimated time)\n"));
3719 if (ps->pss_func == POOL_SCAN_RESILVER) {
3720 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3721 processed_buf, 100 * fraction_done);
3722 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3723 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3724 processed_buf, 100 * fraction_done);
3728 static void
3729 print_error_log(zpool_handle_t *zhp)
3731 nvlist_t *nverrlist = NULL;
3732 nvpair_t *elem;
3733 char *pathname;
3734 size_t len = MAXPATHLEN * 2;
3736 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3737 (void) printf("errors: List of errors unavailable "
3738 "(insufficient privileges)\n");
3739 return;
3742 (void) printf("errors: Permanent errors have been "
3743 "detected in the following files:\n\n");
3745 pathname = safe_malloc(len);
3746 elem = NULL;
3747 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3748 nvlist_t *nv;
3749 uint64_t dsobj, obj;
3751 verify(nvpair_value_nvlist(elem, &nv) == 0);
3752 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3753 &dsobj) == 0);
3754 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3755 &obj) == 0);
3756 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3757 (void) printf("%7s %s\n", "", pathname);
3759 free(pathname);
3760 nvlist_free(nverrlist);
3763 static void
3764 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3765 int namewidth)
3767 uint_t i;
3768 char *name;
3770 if (nspares == 0)
3771 return;
3773 (void) printf(gettext("\tspares\n"));
3775 for (i = 0; i < nspares; i++) {
3776 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3777 print_status_config(zhp, name, spares[i],
3778 namewidth, 2, B_TRUE);
3779 free(name);
3783 static void
3784 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3785 int namewidth)
3787 uint_t i;
3788 char *name;
3790 if (nl2cache == 0)
3791 return;
3793 (void) printf(gettext("\tcache\n"));
3795 for (i = 0; i < nl2cache; i++) {
3796 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3797 print_status_config(zhp, name, l2cache[i],
3798 namewidth, 2, B_FALSE);
3799 free(name);
3803 static void
3804 print_dedup_stats(nvlist_t *config)
3806 ddt_histogram_t *ddh;
3807 ddt_stat_t *dds;
3808 ddt_object_t *ddo;
3809 uint_t c;
3812 * If the pool was faulted then we may not have been able to
3813 * obtain the config. Otherwise, if we have anything in the dedup
3814 * table continue processing the stats.
3816 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3817 (uint64_t **)&ddo, &c) != 0)
3818 return;
3820 (void) printf("\n");
3821 (void) printf(gettext(" dedup: "));
3822 if (ddo->ddo_count == 0) {
3823 (void) printf(gettext("no DDT entries\n"));
3824 return;
3827 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3828 (u_longlong_t)ddo->ddo_count,
3829 (u_longlong_t)ddo->ddo_dspace,
3830 (u_longlong_t)ddo->ddo_mspace);
3832 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3833 (uint64_t **)&dds, &c) == 0);
3834 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3835 (uint64_t **)&ddh, &c) == 0);
3836 zpool_dump_ddt(dds, ddh);
3840 * Display a summary of pool status. Displays a summary such as:
3842 * pool: tank
3843 * status: DEGRADED
3844 * reason: One or more devices ...
3845 * see: http://illumos.org/msg/ZFS-xxxx-01
3846 * config:
3847 * mirror DEGRADED
3848 * c1t0d0 OK
3849 * c2t0d0 UNAVAIL
3851 * When given the '-v' option, we print out the complete config. If the '-e'
3852 * option is specified, then we print out error rate information as well.
3855 status_callback(zpool_handle_t *zhp, void *data)
3857 status_cbdata_t *cbp = data;
3858 nvlist_t *config, *nvroot;
3859 char *msgid;
3860 int reason;
3861 const char *health;
3862 uint_t c;
3863 vdev_stat_t *vs;
3865 config = zpool_get_config(zhp, NULL);
3866 reason = zpool_get_status(zhp, &msgid);
3868 cbp->cb_count++;
3871 * If we were given 'zpool status -x', only report those pools with
3872 * problems.
3874 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3875 if (!cbp->cb_allpools) {
3876 (void) printf(gettext("pool '%s' is healthy\n"),
3877 zpool_get_name(zhp));
3878 if (cbp->cb_first)
3879 cbp->cb_first = B_FALSE;
3881 return (0);
3884 if (cbp->cb_first)
3885 cbp->cb_first = B_FALSE;
3886 else
3887 (void) printf("\n");
3889 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3890 &nvroot) == 0);
3891 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3892 (uint64_t **)&vs, &c) == 0);
3893 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3895 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3896 (void) printf(gettext(" state: %s\n"), health);
3898 switch (reason) {
3899 case ZPOOL_STATUS_MISSING_DEV_R:
3900 (void) printf(gettext("status: One or more devices could not "
3901 "be opened. Sufficient replicas exist for\n\tthe pool to "
3902 "continue functioning in a degraded state.\n"));
3903 (void) printf(gettext("action: Attach the missing device and "
3904 "online it using 'zpool online'.\n"));
3905 break;
3907 case ZPOOL_STATUS_MISSING_DEV_NR:
3908 (void) printf(gettext("status: One or more devices could not "
3909 "be opened. There are insufficient\n\treplicas for the "
3910 "pool to continue functioning.\n"));
3911 (void) printf(gettext("action: Attach the missing device and "
3912 "online it using 'zpool online'.\n"));
3913 break;
3915 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3916 (void) printf(gettext("status: One or more devices could not "
3917 "be used because the label is missing or\n\tinvalid. "
3918 "Sufficient replicas exist for the pool to continue\n\t"
3919 "functioning in a degraded state.\n"));
3920 (void) printf(gettext("action: Replace the device using "
3921 "'zpool replace'.\n"));
3922 break;
3924 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3925 (void) printf(gettext("status: One or more devices could not "
3926 "be used because the label is missing \n\tor invalid. "
3927 "There are insufficient replicas for the pool to "
3928 "continue\n\tfunctioning.\n"));
3929 zpool_explain_recover(zpool_get_handle(zhp),
3930 zpool_get_name(zhp), reason, config);
3931 break;
3933 case ZPOOL_STATUS_FAILING_DEV:
3934 (void) printf(gettext("status: One or more devices has "
3935 "experienced an unrecoverable error. An\n\tattempt was "
3936 "made to correct the error. Applications are "
3937 "unaffected.\n"));
3938 (void) printf(gettext("action: Determine if the device needs "
3939 "to be replaced, and clear the errors\n\tusing "
3940 "'zpool clear' or replace the device with 'zpool "
3941 "replace'.\n"));
3942 break;
3944 case ZPOOL_STATUS_OFFLINE_DEV:
3945 (void) printf(gettext("status: One or more devices has "
3946 "been taken offline by the administrator.\n\tSufficient "
3947 "replicas exist for the pool to continue functioning in "
3948 "a\n\tdegraded state.\n"));
3949 (void) printf(gettext("action: Online the device using "
3950 "'zpool online' or replace the device with\n\t'zpool "
3951 "replace'.\n"));
3952 break;
3954 case ZPOOL_STATUS_REMOVED_DEV:
3955 (void) printf(gettext("status: One or more devices has "
3956 "been removed by the administrator.\n\tSufficient "
3957 "replicas exist for the pool to continue functioning in "
3958 "a\n\tdegraded state.\n"));
3959 (void) printf(gettext("action: Online the device using "
3960 "'zpool online' or replace the device with\n\t'zpool "
3961 "replace'.\n"));
3962 break;
3964 case ZPOOL_STATUS_RESILVERING:
3965 (void) printf(gettext("status: One or more devices is "
3966 "currently being resilvered. The pool will\n\tcontinue "
3967 "to function, possibly in a degraded state.\n"));
3968 (void) printf(gettext("action: Wait for the resilver to "
3969 "complete.\n"));
3970 break;
3972 case ZPOOL_STATUS_CORRUPT_DATA:
3973 (void) printf(gettext("status: One or more devices has "
3974 "experienced an error resulting in data\n\tcorruption. "
3975 "Applications may be affected.\n"));
3976 (void) printf(gettext("action: Restore the file in question "
3977 "if possible. Otherwise restore the\n\tentire pool from "
3978 "backup.\n"));
3979 break;
3981 case ZPOOL_STATUS_CORRUPT_POOL:
3982 (void) printf(gettext("status: The pool metadata is corrupted "
3983 "and the pool cannot be opened.\n"));
3984 zpool_explain_recover(zpool_get_handle(zhp),
3985 zpool_get_name(zhp), reason, config);
3986 break;
3988 case ZPOOL_STATUS_VERSION_OLDER:
3989 (void) printf(gettext("status: The pool is formatted using an "
3990 "older on-disk format. The pool can\n\tstill be used, but "
3991 "some features are unavailable.\n"));
3992 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3993 "upgrade'. Once this is done, the\n\tpool will no longer "
3994 "be accessible on older software versions.\n"));
3995 break;
3997 case ZPOOL_STATUS_VERSION_NEWER:
3998 (void) printf(gettext("status: The pool has been upgraded to a "
3999 "newer, incompatible on-disk version.\n\tThe pool cannot "
4000 "be accessed on this system.\n"));
4001 (void) printf(gettext("action: Access the pool from a system "
4002 "running more recent software, or\n\trestore the pool from "
4003 "backup.\n"));
4004 break;
4006 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4007 (void) printf(gettext("status: The pool cannot be accessed on "
4008 "this system because it uses the\n\tfollowing feature(s) "
4009 "not supported on this system:\n"));
4010 zpool_print_unsup_feat(config);
4011 (void) printf("\n");
4012 (void) printf(gettext("action: Access the pool from a system "
4013 "that supports the required feature(s),\n\tor restore the "
4014 "pool from backup.\n"));
4015 break;
4017 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4018 (void) printf(gettext("status: The pool can only be accessed "
4019 "in read-only mode on this system. It\n\tcannot be "
4020 "accessed in read-write mode because it uses the "
4021 "following\n\tfeature(s) not supported on this system:\n"));
4022 zpool_print_unsup_feat(config);
4023 (void) printf("\n");
4024 (void) printf(gettext("action: The pool cannot be accessed in "
4025 "read-write mode. Import the pool with\n"
4026 "\t\"-o readonly=on\", access the pool from a system that "
4027 "supports the\n\trequired feature(s), or restore the "
4028 "pool from backup.\n"));
4029 break;
4031 case ZPOOL_STATUS_FAULTED_DEV_R:
4032 (void) printf(gettext("status: One or more devices are "
4033 "faulted in response to persistent errors.\n\tSufficient "
4034 "replicas exist for the pool to continue functioning "
4035 "in a\n\tdegraded state.\n"));
4036 (void) printf(gettext("action: Replace the faulted device, "
4037 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4038 break;
4040 case ZPOOL_STATUS_FAULTED_DEV_NR:
4041 (void) printf(gettext("status: One or more devices are "
4042 "faulted in response to persistent errors. There are "
4043 "insufficient replicas for the pool to\n\tcontinue "
4044 "functioning.\n"));
4045 (void) printf(gettext("action: Destroy and re-create the pool "
4046 "from a backup source. Manually marking the device\n"
4047 "\trepaired using 'zpool clear' may allow some data "
4048 "to be recovered.\n"));
4049 break;
4051 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4052 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4053 (void) printf(gettext("status: One or more devices are "
4054 "faulted in response to IO failures.\n"));
4055 (void) printf(gettext("action: Make sure the affected devices "
4056 "are connected, then run 'zpool clear'.\n"));
4057 break;
4059 case ZPOOL_STATUS_BAD_LOG:
4060 (void) printf(gettext("status: An intent log record "
4061 "could not be read.\n"
4062 "\tWaiting for adminstrator intervention to fix the "
4063 "faulted pool.\n"));
4064 (void) printf(gettext("action: Either restore the affected "
4065 "device(s) and run 'zpool online',\n"
4066 "\tor ignore the intent log records by running "
4067 "'zpool clear'.\n"));
4068 break;
4070 default:
4072 * The remaining errors can't actually be generated, yet.
4074 assert(reason == ZPOOL_STATUS_OK);
4077 if (msgid != NULL)
4078 (void) printf(gettext(" see: http://illumos.org/msg/%s\n"),
4079 msgid);
4081 if (config != NULL) {
4082 int namewidth;
4083 uint64_t nerr;
4084 nvlist_t **spares, **l2cache;
4085 uint_t nspares, nl2cache;
4086 pool_scan_stat_t *ps = NULL;
4088 (void) nvlist_lookup_uint64_array(nvroot,
4089 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4090 print_scan_status(ps);
4092 namewidth = max_width(zhp, nvroot, 0, 0);
4093 if (namewidth < 10)
4094 namewidth = 10;
4096 (void) printf(gettext("config:\n\n"));
4097 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4098 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4099 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4100 namewidth, 0, B_FALSE);
4102 if (num_logs(nvroot) > 0)
4103 print_logs(zhp, nvroot, namewidth, B_TRUE);
4104 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4105 &l2cache, &nl2cache) == 0)
4106 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4108 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4109 &spares, &nspares) == 0)
4110 print_spares(zhp, spares, nspares, namewidth);
4112 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4113 &nerr) == 0) {
4114 nvlist_t *nverrlist = NULL;
4117 * If the approximate error count is small, get a
4118 * precise count by fetching the entire log and
4119 * uniquifying the results.
4121 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4122 zpool_get_errlog(zhp, &nverrlist) == 0) {
4123 nvpair_t *elem;
4125 elem = NULL;
4126 nerr = 0;
4127 while ((elem = nvlist_next_nvpair(nverrlist,
4128 elem)) != NULL) {
4129 nerr++;
4132 nvlist_free(nverrlist);
4134 (void) printf("\n");
4136 if (nerr == 0)
4137 (void) printf(gettext("errors: No known data "
4138 "errors\n"));
4139 else if (!cbp->cb_verbose)
4140 (void) printf(gettext("errors: %llu data "
4141 "errors, use '-v' for a list\n"),
4142 (u_longlong_t)nerr);
4143 else
4144 print_error_log(zhp);
4147 if (cbp->cb_dedup_stats)
4148 print_dedup_stats(config);
4149 } else {
4150 (void) printf(gettext("config: The configuration cannot be "
4151 "determined.\n"));
4154 return (0);
4158 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4160 * -v Display complete error logs
4161 * -x Display only pools with potential problems
4162 * -D Display dedup status (undocumented)
4163 * -T Display a timestamp in date(1) or Unix format
4165 * Describes the health status of all pools or some subset.
4168 zpool_do_status(int argc, char **argv)
4170 int c;
4171 int ret;
4172 unsigned long interval = 0, count = 0;
4173 status_cbdata_t cb = { 0 };
4175 /* check options */
4176 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4177 switch (c) {
4178 case 'v':
4179 cb.cb_verbose = B_TRUE;
4180 break;
4181 case 'x':
4182 cb.cb_explain = B_TRUE;
4183 break;
4184 case 'D':
4185 cb.cb_dedup_stats = B_TRUE;
4186 break;
4187 case 'T':
4188 get_timestamp_arg(*optarg);
4189 break;
4190 case '?':
4191 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4192 optopt);
4193 usage(B_FALSE);
4197 argc -= optind;
4198 argv += optind;
4200 get_interval_count(&argc, argv, &interval, &count);
4202 if (argc == 0)
4203 cb.cb_allpools = B_TRUE;
4205 cb.cb_first = B_TRUE;
4207 for (;;) {
4208 if (timestamp_fmt != NODATE)
4209 print_timestamp(timestamp_fmt);
4211 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4212 status_callback, &cb);
4214 if (argc == 0 && cb.cb_count == 0)
4215 (void) printf(gettext("no pools available\n"));
4216 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4217 (void) printf(gettext("all pools are healthy\n"));
4219 if (ret != 0)
4220 return (ret);
4222 if (interval == 0)
4223 break;
4225 if (count != 0 && --count == 0)
4226 break;
4228 (void) sleep(interval);
4231 return (0);
4234 typedef struct upgrade_cbdata {
4235 int cb_all;
4236 int cb_first;
4237 int cb_newer;
4238 int cb_argc;
4239 uint64_t cb_version;
4240 char **cb_argv;
4241 } upgrade_cbdata_t;
4243 static int
4244 upgrade_cb(zpool_handle_t *zhp, void *arg)
4246 upgrade_cbdata_t *cbp = arg;
4247 nvlist_t *config;
4248 uint64_t version;
4249 int ret = 0;
4251 config = zpool_get_config(zhp, NULL);
4252 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4253 &version) == 0);
4255 if (!cbp->cb_newer && SPA_VERSION_IS_SUPPORTED(version) &&
4256 version != SPA_VERSION) {
4257 if (!cbp->cb_all) {
4258 if (cbp->cb_first) {
4259 (void) printf(gettext("The following pools are "
4260 "out of date, and can be upgraded. After "
4261 "being\nupgraded, these pools will no "
4262 "longer be accessible by older software "
4263 "versions.\n\n"));
4264 (void) printf(gettext("VER POOL\n"));
4265 (void) printf(gettext("--- ------------\n"));
4266 cbp->cb_first = B_FALSE;
4269 (void) printf("%2llu %s\n", (u_longlong_t)version,
4270 zpool_get_name(zhp));
4271 } else {
4272 cbp->cb_first = B_FALSE;
4273 ret = zpool_upgrade(zhp, cbp->cb_version);
4274 if (!ret) {
4275 (void) printf(gettext("Successfully upgraded "
4276 "'%s'\n\n"), zpool_get_name(zhp));
4279 * If they did "zpool upgrade -a", then we could
4280 * be doing ioctls to different pools. We need
4281 * to log this history once to each pool, and bypass
4282 * the normal history logging that happens in main().
4284 (void) zpool_log_history(g_zfs, history_str);
4285 log_history = B_FALSE;
4287 } else if (cbp->cb_newer && !SPA_VERSION_IS_SUPPORTED(version)) {
4288 assert(!cbp->cb_all);
4290 if (cbp->cb_first) {
4291 (void) printf(gettext("The following pools are "
4292 "formatted using an unsupported software version "
4293 "and\ncannot be accessed on the current "
4294 "system.\n\n"));
4295 (void) printf(gettext("VER POOL\n"));
4296 (void) printf(gettext("--- ------------\n"));
4297 cbp->cb_first = B_FALSE;
4300 (void) printf("%2llu %s\n", (u_longlong_t)version,
4301 zpool_get_name(zhp));
4304 zpool_close(zhp);
4305 return (ret);
4308 /* ARGSUSED */
4309 static int
4310 upgrade_one(zpool_handle_t *zhp, void *data)
4312 upgrade_cbdata_t *cbp = data;
4313 uint64_t cur_version;
4314 int ret;
4316 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4317 (void) printf(gettext("'log' is now a reserved word\n"
4318 "Pool 'log' must be renamed using export and import"
4319 " to upgrade.\n"));
4320 return (1);
4323 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4324 if (cur_version > cbp->cb_version) {
4325 (void) printf(gettext("Pool '%s' is already formatted "
4326 "using more current version '%llu'.\n"),
4327 zpool_get_name(zhp), cur_version);
4328 return (0);
4330 if (cur_version == cbp->cb_version) {
4331 (void) printf(gettext("Pool '%s' is already formatted "
4332 "using the current version.\n"), zpool_get_name(zhp));
4333 return (0);
4336 ret = zpool_upgrade(zhp, cbp->cb_version);
4338 if (!ret) {
4339 (void) printf(gettext("Successfully upgraded '%s' "
4340 "from version %llu to version %llu\n\n"),
4341 zpool_get_name(zhp), (u_longlong_t)cur_version,
4342 (u_longlong_t)cbp->cb_version);
4345 return (ret != 0);
4349 * zpool upgrade
4350 * zpool upgrade -v
4351 * zpool upgrade [-V version] <-a | pool ...>
4353 * With no arguments, display downrev'd ZFS pool available for upgrade.
4354 * Individual pools can be upgraded by specifying the pool, and '-a' will
4355 * upgrade all pools.
4358 zpool_do_upgrade(int argc, char **argv)
4360 int c;
4361 upgrade_cbdata_t cb = { 0 };
4362 int ret = 0;
4363 boolean_t showversions = B_FALSE;
4364 char *end;
4367 /* check options */
4368 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4369 switch (c) {
4370 case 'a':
4371 cb.cb_all = B_TRUE;
4372 break;
4373 case 'v':
4374 showversions = B_TRUE;
4375 break;
4376 case 'V':
4377 cb.cb_version = strtoll(optarg, &end, 10);
4378 if (*end != '\0' ||
4379 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4380 (void) fprintf(stderr,
4381 gettext("invalid version '%s'\n"), optarg);
4382 usage(B_FALSE);
4384 break;
4385 case ':':
4386 (void) fprintf(stderr, gettext("missing argument for "
4387 "'%c' option\n"), optopt);
4388 usage(B_FALSE);
4389 break;
4390 case '?':
4391 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4392 optopt);
4393 usage(B_FALSE);
4397 cb.cb_argc = argc;
4398 cb.cb_argv = argv;
4399 argc -= optind;
4400 argv += optind;
4402 if (cb.cb_version == 0) {
4403 cb.cb_version = SPA_VERSION;
4404 } else if (!cb.cb_all && argc == 0) {
4405 (void) fprintf(stderr, gettext("-V option is "
4406 "incompatible with other arguments\n"));
4407 usage(B_FALSE);
4410 if (showversions) {
4411 if (cb.cb_all || argc != 0) {
4412 (void) fprintf(stderr, gettext("-v option is "
4413 "incompatible with other arguments\n"));
4414 usage(B_FALSE);
4416 } else if (cb.cb_all) {
4417 if (argc != 0) {
4418 (void) fprintf(stderr, gettext("-a option should not "
4419 "be used along with a pool name\n"));
4420 usage(B_FALSE);
4424 (void) printf(gettext("This system supports ZFS pool feature "
4425 "flags.\n\n"));
4426 cb.cb_first = B_TRUE;
4427 if (showversions) {
4428 (void) printf(gettext("The following versions are "
4429 "supported:\n\n"));
4430 (void) printf(gettext("VER DESCRIPTION\n"));
4431 (void) printf("--- -----------------------------------------"
4432 "---------------\n");
4433 (void) printf(gettext(" 1 Initial ZFS version\n"));
4434 (void) printf(gettext(" 2 Ditto blocks "
4435 "(replicated metadata)\n"));
4436 (void) printf(gettext(" 3 Hot spares and double parity "
4437 "RAID-Z\n"));
4438 (void) printf(gettext(" 4 zpool history\n"));
4439 (void) printf(gettext(" 5 Compression using the gzip "
4440 "algorithm\n"));
4441 (void) printf(gettext(" 6 bootfs pool property\n"));
4442 (void) printf(gettext(" 7 Separate intent log devices\n"));
4443 (void) printf(gettext(" 8 Delegated administration\n"));
4444 (void) printf(gettext(" 9 refquota and refreservation "
4445 "properties\n"));
4446 (void) printf(gettext(" 10 Cache devices\n"));
4447 (void) printf(gettext(" 11 Improved scrub performance\n"));
4448 (void) printf(gettext(" 12 Snapshot properties\n"));
4449 (void) printf(gettext(" 13 snapused property\n"));
4450 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4451 (void) printf(gettext(" 15 user/group space accounting\n"));
4452 (void) printf(gettext(" 16 stmf property support\n"));
4453 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4454 (void) printf(gettext(" 18 Snapshot user holds\n"));
4455 (void) printf(gettext(" 19 Log device removal\n"));
4456 (void) printf(gettext(" 20 Compression using zle "
4457 "(zero-length encoding)\n"));
4458 (void) printf(gettext(" 21 Deduplication\n"));
4459 (void) printf(gettext(" 22 Received properties\n"));
4460 (void) printf(gettext(" 23 Slim ZIL\n"));
4461 (void) printf(gettext(" 24 System attributes\n"));
4462 (void) printf(gettext(" 25 Improved scrub stats\n"));
4463 (void) printf(gettext(" 26 Improved snapshot deletion "
4464 "performance\n"));
4465 (void) printf(gettext(" 27 Improved snapshot creation "
4466 "performance\n"));
4467 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4468 (void) printf(gettext("\nFor more information on a particular "
4469 "version, including supported releases,\n"));
4470 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4471 } else if (argc == 0) {
4472 int notfound;
4474 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4475 notfound = cb.cb_first;
4477 if (!cb.cb_all && ret == 0) {
4478 if (!cb.cb_first)
4479 (void) printf("\n");
4480 cb.cb_first = B_TRUE;
4481 cb.cb_newer = B_TRUE;
4482 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4483 if (!cb.cb_first) {
4484 notfound = B_FALSE;
4485 (void) printf("\n");
4489 if (ret == 0) {
4490 if (notfound)
4491 (void) printf(gettext("All pools are formatted "
4492 "using this version.\n"));
4493 else if (!cb.cb_all)
4494 (void) printf(gettext("Use 'zpool upgrade -v' "
4495 "for a list of available versions and "
4496 "their associated\nfeatures.\n"));
4498 } else {
4499 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4500 upgrade_one, &cb);
4503 return (ret);
4506 typedef struct hist_cbdata {
4507 boolean_t first;
4508 boolean_t longfmt;
4509 boolean_t internal;
4510 } hist_cbdata_t;
4513 * Print out the command history for a specific pool.
4515 static int
4516 get_history_one(zpool_handle_t *zhp, void *data)
4518 nvlist_t *nvhis;
4519 nvlist_t **records;
4520 uint_t numrecords;
4521 int ret, i;
4522 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4524 cb->first = B_FALSE;
4526 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4528 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4529 return (ret);
4531 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4532 &records, &numrecords) == 0);
4533 for (i = 0; i < numrecords; i++) {
4534 nvlist_t *rec = records[i];
4535 char tbuf[30] = "";
4537 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
4538 time_t tsec;
4539 struct tm t;
4541 tsec = fnvlist_lookup_uint64(records[i],
4542 ZPOOL_HIST_TIME);
4543 (void) localtime_r(&tsec, &t);
4544 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4547 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
4548 (void) printf("%s %s", tbuf,
4549 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
4550 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
4551 int ievent =
4552 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
4553 if (!cb->internal)
4554 continue;
4555 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
4556 (void) printf("%s unrecognized record:\n",
4557 tbuf);
4558 dump_nvlist(rec, 4);
4559 continue;
4561 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
4562 zfs_history_event_names[ievent],
4563 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
4564 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
4565 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
4566 if (!cb->internal)
4567 continue;
4568 (void) printf("%s [txg:%lld] %s", tbuf,
4569 fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
4570 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
4571 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
4572 (void) printf(" %s (%llu)",
4573 fnvlist_lookup_string(rec,
4574 ZPOOL_HIST_DSNAME),
4575 fnvlist_lookup_uint64(rec,
4576 ZPOOL_HIST_DSID));
4578 (void) printf(" %s", fnvlist_lookup_string(rec,
4579 ZPOOL_HIST_INT_STR));
4580 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
4581 if (!cb->internal)
4582 continue;
4583 (void) printf("%s ioctl %s\n", tbuf,
4584 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
4585 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
4586 (void) printf(" input:\n");
4587 dump_nvlist(fnvlist_lookup_nvlist(rec,
4588 ZPOOL_HIST_INPUT_NVL), 8);
4590 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
4591 (void) printf(" output:\n");
4592 dump_nvlist(fnvlist_lookup_nvlist(rec,
4593 ZPOOL_HIST_OUTPUT_NVL), 8);
4595 } else {
4596 if (!cb->internal)
4597 continue;
4598 (void) printf("%s unrecognized record:\n", tbuf);
4599 dump_nvlist(rec, 4);
4602 if (!cb->longfmt) {
4603 (void) printf("\n");
4604 continue;
4606 (void) printf(" [");
4607 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
4608 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
4609 struct passwd *pwd = getpwuid(who);
4610 (void) printf("user %d ", (int)who);
4611 if (pwd != NULL)
4612 (void) printf("(%s) ", pwd->pw_name);
4614 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
4615 (void) printf("on %s",
4616 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
4618 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
4619 (void) printf(":%s",
4620 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
4622 (void) printf("]");
4623 (void) printf("\n");
4625 (void) printf("\n");
4626 nvlist_free(nvhis);
4628 return (ret);
4632 * zpool history <pool>
4634 * Displays the history of commands that modified pools.
4637 zpool_do_history(int argc, char **argv)
4639 hist_cbdata_t cbdata = { 0 };
4640 int ret;
4641 int c;
4643 cbdata.first = B_TRUE;
4644 /* check options */
4645 while ((c = getopt(argc, argv, "li")) != -1) {
4646 switch (c) {
4647 case 'l':
4648 cbdata.longfmt = B_TRUE;
4649 break;
4650 case 'i':
4651 cbdata.internal = B_TRUE;
4652 break;
4653 case '?':
4654 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4655 optopt);
4656 usage(B_FALSE);
4659 argc -= optind;
4660 argv += optind;
4662 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4663 &cbdata);
4665 if (argc == 0 && cbdata.first == B_TRUE) {
4666 (void) printf(gettext("no pools available\n"));
4667 return (0);
4670 return (ret);
4673 static int
4674 get_callback(zpool_handle_t *zhp, void *data)
4676 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4677 char value[MAXNAMELEN];
4678 zprop_source_t srctype;
4679 zprop_list_t *pl;
4681 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4684 * Skip the special fake placeholder. This will also skip
4685 * over the name property when 'all' is specified.
4687 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4688 pl == cbp->cb_proplist)
4689 continue;
4691 if (pl->pl_prop == ZPROP_INVAL &&
4692 (zpool_prop_feature(pl->pl_user_prop) ||
4693 zpool_prop_unsupported(pl->pl_user_prop))) {
4694 srctype = ZPROP_SRC_LOCAL;
4696 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
4697 value, sizeof (value)) == 0) {
4698 zprop_print_one_property(zpool_get_name(zhp),
4699 cbp, pl->pl_user_prop, value, srctype,
4700 NULL, NULL);
4702 } else {
4703 if (zpool_get_prop(zhp, pl->pl_prop, value,
4704 sizeof (value), &srctype) != 0)
4705 continue;
4707 zprop_print_one_property(zpool_get_name(zhp), cbp,
4708 zpool_prop_to_name(pl->pl_prop), value, srctype,
4709 NULL, NULL);
4712 return (0);
4716 zpool_do_get(int argc, char **argv)
4718 zprop_get_cbdata_t cb = { 0 };
4719 zprop_list_t fake_name = { 0 };
4720 int ret;
4722 if (argc < 2) {
4723 (void) fprintf(stderr, gettext("missing property "
4724 "argument\n"));
4725 usage(B_FALSE);
4728 cb.cb_first = B_TRUE;
4729 cb.cb_sources = ZPROP_SRC_ALL;
4730 cb.cb_columns[0] = GET_COL_NAME;
4731 cb.cb_columns[1] = GET_COL_PROPERTY;
4732 cb.cb_columns[2] = GET_COL_VALUE;
4733 cb.cb_columns[3] = GET_COL_SOURCE;
4734 cb.cb_type = ZFS_TYPE_POOL;
4736 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4737 ZFS_TYPE_POOL) != 0)
4738 usage(B_FALSE);
4740 if (cb.cb_proplist != NULL) {
4741 fake_name.pl_prop = ZPOOL_PROP_NAME;
4742 fake_name.pl_width = strlen(gettext("NAME"));
4743 fake_name.pl_next = cb.cb_proplist;
4744 cb.cb_proplist = &fake_name;
4747 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4748 get_callback, &cb);
4750 if (cb.cb_proplist == &fake_name)
4751 zprop_free_list(fake_name.pl_next);
4752 else
4753 zprop_free_list(cb.cb_proplist);
4755 return (ret);
4758 typedef struct set_cbdata {
4759 char *cb_propname;
4760 char *cb_value;
4761 boolean_t cb_any_successful;
4762 } set_cbdata_t;
4765 set_callback(zpool_handle_t *zhp, void *data)
4767 int error;
4768 set_cbdata_t *cb = (set_cbdata_t *)data;
4770 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4772 if (!error)
4773 cb->cb_any_successful = B_TRUE;
4775 return (error);
4779 zpool_do_set(int argc, char **argv)
4781 set_cbdata_t cb = { 0 };
4782 int error;
4784 if (argc > 1 && argv[1][0] == '-') {
4785 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4786 argv[1][1]);
4787 usage(B_FALSE);
4790 if (argc < 2) {
4791 (void) fprintf(stderr, gettext("missing property=value "
4792 "argument\n"));
4793 usage(B_FALSE);
4796 if (argc < 3) {
4797 (void) fprintf(stderr, gettext("missing pool name\n"));
4798 usage(B_FALSE);
4801 if (argc > 3) {
4802 (void) fprintf(stderr, gettext("too many pool names\n"));
4803 usage(B_FALSE);
4806 cb.cb_propname = argv[1];
4807 cb.cb_value = strchr(cb.cb_propname, '=');
4808 if (cb.cb_value == NULL) {
4809 (void) fprintf(stderr, gettext("missing value in "
4810 "property=value argument\n"));
4811 usage(B_FALSE);
4814 *(cb.cb_value) = '\0';
4815 cb.cb_value++;
4817 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4818 set_callback, &cb);
4820 return (error);
4823 static int
4824 find_command_idx(char *command, int *idx)
4826 int i;
4828 for (i = 0; i < NCOMMAND; i++) {
4829 if (command_table[i].name == NULL)
4830 continue;
4832 if (strcmp(command, command_table[i].name) == 0) {
4833 *idx = i;
4834 return (0);
4837 return (1);
4841 main(int argc, char **argv)
4843 int ret;
4844 int i;
4845 char *cmdname;
4847 (void) setlocale(LC_ALL, "");
4848 (void) textdomain(TEXT_DOMAIN);
4850 if ((g_zfs = libzfs_init()) == NULL) {
4851 (void) fprintf(stderr, gettext("internal error: failed to "
4852 "initialize ZFS library\n"));
4853 return (1);
4856 libzfs_print_on_error(g_zfs, B_TRUE);
4858 opterr = 0;
4861 * Make sure the user has specified some command.
4863 if (argc < 2) {
4864 (void) fprintf(stderr, gettext("missing command\n"));
4865 usage(B_FALSE);
4868 cmdname = argv[1];
4871 * Special case '-?'
4873 if (strcmp(cmdname, "-?") == 0)
4874 usage(B_TRUE);
4876 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
4879 * Run the appropriate command.
4881 if (find_command_idx(cmdname, &i) == 0) {
4882 current_command = &command_table[i];
4883 ret = command_table[i].func(argc - 1, argv + 1);
4884 } else if (strchr(cmdname, '=')) {
4885 verify(find_command_idx("set", &i) == 0);
4886 current_command = &command_table[i];
4887 ret = command_table[i].func(argc, argv);
4888 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4890 * 'freeze' is a vile debugging abomination, so we treat
4891 * it as such.
4893 char buf[16384];
4894 int fd = open(ZFS_DEV, O_RDWR);
4895 (void) strcpy((void *)buf, argv[2]);
4896 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4897 } else {
4898 (void) fprintf(stderr, gettext("unrecognized "
4899 "command '%s'\n"), cmdname);
4900 usage(B_FALSE);
4903 if (ret == 0 && log_history)
4904 (void) zpool_log_history(g_zfs, history_str);
4906 libzfs_fini(g_zfs);
4909 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4910 * for the purposes of running ::findleaks.
4912 if (getenv("ZFS_ABORT") != NULL) {
4913 (void) printf("dumping core by request\n");
4914 abort();
4917 return (ret);