Print this page
1693 persistent 'comment' field for a zpool
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/cmd/zpool/zpool_main.c
+++ new/usr/src/cmd/zpool/zpool_main.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 26 */
27 27
28 28 #include <assert.h>
29 29 #include <ctype.h>
30 30 #include <dirent.h>
31 31 #include <errno.h>
32 32 #include <fcntl.h>
33 33 #include <libgen.h>
34 34 #include <libintl.h>
35 35 #include <libuutil.h>
36 36 #include <locale.h>
37 37 #include <stdio.h>
38 38 #include <stdlib.h>
39 39 #include <string.h>
40 40 #include <strings.h>
41 41 #include <unistd.h>
42 42 #include <priv.h>
43 43 #include <pwd.h>
44 44 #include <zone.h>
45 45 #include <sys/fs/zfs.h>
46 46 #include <sys/stat.h>
47 47
48 48 #include <libzfs.h>
49 49
50 50 #include "zpool_util.h"
51 51 #include "zfs_comutil.h"
52 52
53 53 #include "statcommon.h"
54 54
55 55 static int zpool_do_create(int, char **);
56 56 static int zpool_do_destroy(int, char **);
57 57
58 58 static int zpool_do_add(int, char **);
59 59 static int zpool_do_remove(int, char **);
60 60
61 61 static int zpool_do_list(int, char **);
62 62 static int zpool_do_iostat(int, char **);
63 63 static int zpool_do_status(int, char **);
64 64
65 65 static int zpool_do_online(int, char **);
66 66 static int zpool_do_offline(int, char **);
67 67 static int zpool_do_clear(int, char **);
68 68
69 69 static int zpool_do_reguid(int, char **);
70 70
71 71 static int zpool_do_attach(int, char **);
72 72 static int zpool_do_detach(int, char **);
73 73 static int zpool_do_replace(int, char **);
74 74 static int zpool_do_split(int, char **);
75 75
76 76 static int zpool_do_scrub(int, char **);
77 77
78 78 static int zpool_do_import(int, char **);
79 79 static int zpool_do_export(int, char **);
80 80
81 81 static int zpool_do_upgrade(int, char **);
82 82
83 83 static int zpool_do_history(int, char **);
84 84
85 85 static int zpool_do_get(int, char **);
86 86 static int zpool_do_set(int, char **);
87 87
88 88 /*
89 89 * These libumem hooks provide a reasonable set of defaults for the allocator's
90 90 * debugging facilities.
91 91 */
92 92
93 93 #ifdef DEBUG
94 94 const char *
95 95 _umem_debug_init(void)
96 96 {
97 97 return ("default,verbose"); /* $UMEM_DEBUG setting */
98 98 }
99 99
100 100 const char *
101 101 _umem_logging_init(void)
102 102 {
103 103 return ("fail,contents"); /* $UMEM_LOGGING setting */
104 104 }
105 105 #endif
106 106
107 107 typedef enum {
108 108 HELP_ADD,
109 109 HELP_ATTACH,
110 110 HELP_CLEAR,
111 111 HELP_CREATE,
112 112 HELP_DESTROY,
113 113 HELP_DETACH,
114 114 HELP_EXPORT,
115 115 HELP_HISTORY,
116 116 HELP_IMPORT,
117 117 HELP_IOSTAT,
118 118 HELP_LIST,
119 119 HELP_OFFLINE,
120 120 HELP_ONLINE,
121 121 HELP_REPLACE,
122 122 HELP_REMOVE,
123 123 HELP_SCRUB,
124 124 HELP_STATUS,
125 125 HELP_UPGRADE,
126 126 HELP_GET,
127 127 HELP_SET,
128 128 HELP_SPLIT,
129 129 HELP_REGUID
130 130 } zpool_help_t;
131 131
132 132
133 133 typedef struct zpool_command {
134 134 const char *name;
135 135 int (*func)(int, char **);
136 136 zpool_help_t usage;
137 137 } zpool_command_t;
138 138
139 139 /*
140 140 * Master command table. Each ZFS command has a name, associated function, and
141 141 * usage message. The usage messages need to be internationalized, so we have
142 142 * to have a function to return the usage message based on a command index.
143 143 *
144 144 * These commands are organized according to how they are displayed in the usage
145 145 * message. An empty command (one with a NULL name) indicates an empty line in
146 146 * the generic usage message.
147 147 */
148 148 static zpool_command_t command_table[] = {
149 149 { "create", zpool_do_create, HELP_CREATE },
150 150 { "destroy", zpool_do_destroy, HELP_DESTROY },
151 151 { NULL },
152 152 { "add", zpool_do_add, HELP_ADD },
153 153 { "remove", zpool_do_remove, HELP_REMOVE },
154 154 { NULL },
155 155 { "list", zpool_do_list, HELP_LIST },
156 156 { "iostat", zpool_do_iostat, HELP_IOSTAT },
157 157 { "status", zpool_do_status, HELP_STATUS },
158 158 { NULL },
159 159 { "online", zpool_do_online, HELP_ONLINE },
160 160 { "offline", zpool_do_offline, HELP_OFFLINE },
161 161 { "clear", zpool_do_clear, HELP_CLEAR },
162 162 { NULL },
163 163 { "attach", zpool_do_attach, HELP_ATTACH },
164 164 { "detach", zpool_do_detach, HELP_DETACH },
165 165 { "replace", zpool_do_replace, HELP_REPLACE },
166 166 { "split", zpool_do_split, HELP_SPLIT },
167 167 { NULL },
168 168 { "scrub", zpool_do_scrub, HELP_SCRUB },
169 169 { NULL },
170 170 { "import", zpool_do_import, HELP_IMPORT },
171 171 { "export", zpool_do_export, HELP_EXPORT },
172 172 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
173 173 { "reguid", zpool_do_reguid, HELP_REGUID },
174 174 { NULL },
175 175 { "history", zpool_do_history, HELP_HISTORY },
176 176 { "get", zpool_do_get, HELP_GET },
177 177 { "set", zpool_do_set, HELP_SET },
178 178 };
179 179
180 180 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
181 181
182 182 zpool_command_t *current_command;
183 183 static char history_str[HIS_MAX_RECORD_LEN];
184 184
185 185 static uint_t timestamp_fmt = NODATE;
186 186
187 187 static const char *
188 188 get_usage(zpool_help_t idx) {
189 189 switch (idx) {
190 190 case HELP_ADD:
191 191 return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
192 192 case HELP_ATTACH:
193 193 return (gettext("\tattach [-f] <pool> <device> "
194 194 "<new-device>\n"));
195 195 case HELP_CLEAR:
196 196 return (gettext("\tclear [-nF] <pool> [device]\n"));
197 197 case HELP_CREATE:
198 198 return (gettext("\tcreate [-fn] [-o property=value] ... \n"
199 199 "\t [-O file-system-property=value] ... \n"
200 200 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
201 201 case HELP_DESTROY:
202 202 return (gettext("\tdestroy [-f] <pool>\n"));
203 203 case HELP_DETACH:
204 204 return (gettext("\tdetach <pool> <device>\n"));
205 205 case HELP_EXPORT:
206 206 return (gettext("\texport [-f] <pool> ...\n"));
207 207 case HELP_HISTORY:
208 208 return (gettext("\thistory [-il] [<pool>] ...\n"));
209 209 case HELP_IMPORT:
210 210 return (gettext("\timport [-d dir] [-D]\n"
211 211 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
212 212 "\timport [-o mntopts] [-o property=value] ... \n"
213 213 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
214 214 "[-R root] [-F [-n]] -a\n"
215 215 "\timport [-o mntopts] [-o property=value] ... \n"
216 216 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
217 217 "[-R root] [-F [-n]]\n"
218 218 "\t <pool | id> [newpool]\n"));
219 219 case HELP_IOSTAT:
220 220 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
221 221 "[count]]\n"));
222 222 case HELP_LIST:
223 223 return (gettext("\tlist [-H] [-o property[,...]] "
224 224 "[-T d|u] [pool] ... [interval [count]]\n"));
225 225 case HELP_OFFLINE:
226 226 return (gettext("\toffline [-t] <pool> <device> ...\n"));
227 227 case HELP_ONLINE:
228 228 return (gettext("\tonline <pool> <device> ...\n"));
229 229 case HELP_REPLACE:
230 230 return (gettext("\treplace [-f] <pool> <device> "
231 231 "[new-device]\n"));
232 232 case HELP_REMOVE:
233 233 return (gettext("\tremove <pool> <device> ...\n"));
234 234 case HELP_SCRUB:
235 235 return (gettext("\tscrub [-s] <pool> ...\n"));
236 236 case HELP_STATUS:
237 237 return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval "
238 238 "[count]]\n"));
239 239 case HELP_UPGRADE:
240 240 return (gettext("\tupgrade\n"
241 241 "\tupgrade -v\n"
242 242 "\tupgrade [-V version] <-a | pool ...>\n"));
243 243 case HELP_GET:
244 244 return (gettext("\tget <\"all\" | property[,...]> "
245 245 "<pool> ...\n"));
246 246 case HELP_SET:
247 247 return (gettext("\tset <property=value> <pool> \n"));
248 248 case HELP_SPLIT:
249 249 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
250 250 "\t [-o property=value] <pool> <newpool> "
251 251 "[<device> ...]\n"));
252 252 case HELP_REGUID:
253 253 return (gettext("\treguid <pool>\n"));
254 254 }
255 255
256 256 abort();
257 257 /* NOTREACHED */
258 258 }
259 259
260 260
261 261 /*
262 262 * Callback routine that will print out a pool property value.
263 263 */
264 264 static int
265 265 print_prop_cb(int prop, void *cb)
266 266 {
267 267 FILE *fp = cb;
268 268
269 269 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
270 270
271 271 if (zpool_prop_readonly(prop))
272 272 (void) fprintf(fp, " NO ");
273 273 else
274 274 (void) fprintf(fp, " YES ");
275 275
276 276 if (zpool_prop_values(prop) == NULL)
277 277 (void) fprintf(fp, "-\n");
278 278 else
279 279 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
280 280
281 281 return (ZPROP_CONT);
282 282 }
283 283
284 284 /*
285 285 * Display usage message. If we're inside a command, display only the usage for
286 286 * that command. Otherwise, iterate over the entire command table and display
287 287 * a complete usage message.
288 288 */
289 289 void
290 290 usage(boolean_t requested)
291 291 {
292 292 FILE *fp = requested ? stdout : stderr;
293 293
294 294 if (current_command == NULL) {
295 295 int i;
296 296
297 297 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
298 298 (void) fprintf(fp,
299 299 gettext("where 'command' is one of the following:\n\n"));
300 300
301 301 for (i = 0; i < NCOMMAND; i++) {
302 302 if (command_table[i].name == NULL)
303 303 (void) fprintf(fp, "\n");
304 304 else
305 305 (void) fprintf(fp, "%s",
306 306 get_usage(command_table[i].usage));
307 307 }
308 308 } else {
309 309 (void) fprintf(fp, gettext("usage:\n"));
310 310 (void) fprintf(fp, "%s", get_usage(current_command->usage));
311 311 }
312 312
313 313 if (current_command != NULL &&
314 314 ((strcmp(current_command->name, "set") == 0) ||
315 315 (strcmp(current_command->name, "get") == 0) ||
316 316 (strcmp(current_command->name, "list") == 0))) {
317 317
318 318 (void) fprintf(fp,
319 319 gettext("\nthe following properties are supported:\n"));
320 320
321 321 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
322 322 "PROPERTY", "EDIT", "VALUES");
323 323
324 324 /* Iterate over all properties */
325 325 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
326 326 ZFS_TYPE_POOL);
327 327 }
328 328
329 329 /*
330 330 * See comments at end of main().
331 331 */
332 332 if (getenv("ZFS_ABORT") != NULL) {
333 333 (void) printf("dumping core by request\n");
334 334 abort();
335 335 }
336 336
337 337 exit(requested ? 0 : 2);
338 338 }
339 339
340 340 void
341 341 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
342 342 boolean_t print_logs)
343 343 {
344 344 nvlist_t **child;
345 345 uint_t c, children;
346 346 char *vname;
347 347
348 348 if (name != NULL)
349 349 (void) printf("\t%*s%s\n", indent, "", name);
350 350
351 351 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
352 352 &child, &children) != 0)
353 353 return;
354 354
355 355 for (c = 0; c < children; c++) {
356 356 uint64_t is_log = B_FALSE;
357 357
358 358 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
359 359 &is_log);
360 360 if ((is_log && !print_logs) || (!is_log && print_logs))
361 361 continue;
362 362
363 363 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
364 364 print_vdev_tree(zhp, vname, child[c], indent + 2,
365 365 B_FALSE);
366 366 free(vname);
367 367 }
368 368 }
369 369
370 370 /*
371 371 * Add a property pair (name, string-value) into a property nvlist.
372 372 */
373 373 static int
374 374 add_prop_list(const char *propname, char *propval, nvlist_t **props,
375 375 boolean_t poolprop)
376 376 {
377 377 zpool_prop_t prop = ZPROP_INVAL;
378 378 zfs_prop_t fprop;
379 379 nvlist_t *proplist;
380 380 const char *normnm;
381 381 char *strval;
382 382
383 383 if (*props == NULL &&
384 384 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
385 385 (void) fprintf(stderr,
386 386 gettext("internal error: out of memory\n"));
387 387 return (1);
388 388 }
389 389
390 390 proplist = *props;
391 391
392 392 if (poolprop) {
393 393 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
394 394 (void) fprintf(stderr, gettext("property '%s' is "
395 395 "not a valid pool property\n"), propname);
396 396 return (2);
397 397 }
398 398 normnm = zpool_prop_to_name(prop);
399 399 } else {
400 400 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
401 401 normnm = zfs_prop_to_name(fprop);
402 402 } else {
403 403 normnm = propname;
404 404 }
405 405 }
406 406
407 407 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
408 408 prop != ZPOOL_PROP_CACHEFILE) {
409 409 (void) fprintf(stderr, gettext("property '%s' "
410 410 "specified multiple times\n"), propname);
411 411 return (2);
412 412 }
413 413
414 414 if (nvlist_add_string(proplist, normnm, propval) != 0) {
415 415 (void) fprintf(stderr, gettext("internal "
416 416 "error: out of memory\n"));
417 417 return (1);
418 418 }
419 419
420 420 return (0);
421 421 }
422 422
423 423 /*
424 424 * zpool add [-fn] <pool> <vdev> ...
425 425 *
426 426 * -f Force addition of devices, even if they appear in use
427 427 * -n Do not add the devices, but display the resulting layout if
428 428 * they were to be added.
429 429 *
430 430 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
431 431 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
432 432 * libzfs.
433 433 */
434 434 int
435 435 zpool_do_add(int argc, char **argv)
436 436 {
437 437 boolean_t force = B_FALSE;
438 438 boolean_t dryrun = B_FALSE;
439 439 int c;
440 440 nvlist_t *nvroot;
441 441 char *poolname;
442 442 int ret;
443 443 zpool_handle_t *zhp;
444 444 nvlist_t *config;
445 445
446 446 /* check options */
447 447 while ((c = getopt(argc, argv, "fn")) != -1) {
448 448 switch (c) {
449 449 case 'f':
450 450 force = B_TRUE;
451 451 break;
452 452 case 'n':
453 453 dryrun = B_TRUE;
454 454 break;
455 455 case '?':
456 456 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
457 457 optopt);
458 458 usage(B_FALSE);
459 459 }
460 460 }
461 461
462 462 argc -= optind;
463 463 argv += optind;
464 464
465 465 /* get pool name and check number of arguments */
466 466 if (argc < 1) {
467 467 (void) fprintf(stderr, gettext("missing pool name argument\n"));
468 468 usage(B_FALSE);
469 469 }
470 470 if (argc < 2) {
471 471 (void) fprintf(stderr, gettext("missing vdev specification\n"));
472 472 usage(B_FALSE);
473 473 }
474 474
475 475 poolname = argv[0];
476 476
477 477 argc--;
478 478 argv++;
479 479
480 480 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
481 481 return (1);
482 482
483 483 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
484 484 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
485 485 poolname);
486 486 zpool_close(zhp);
487 487 return (1);
488 488 }
489 489
490 490 /* pass off to get_vdev_spec for processing */
491 491 nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun,
492 492 argc, argv);
493 493 if (nvroot == NULL) {
494 494 zpool_close(zhp);
495 495 return (1);
496 496 }
497 497
498 498 if (dryrun) {
499 499 nvlist_t *poolnvroot;
500 500
501 501 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
502 502 &poolnvroot) == 0);
503 503
504 504 (void) printf(gettext("would update '%s' to the following "
505 505 "configuration:\n"), zpool_get_name(zhp));
506 506
507 507 /* print original main pool and new tree */
508 508 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
509 509 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
510 510
511 511 /* Do the same for the logs */
512 512 if (num_logs(poolnvroot) > 0) {
513 513 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
514 514 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
515 515 } else if (num_logs(nvroot) > 0) {
516 516 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
517 517 }
518 518
519 519 ret = 0;
520 520 } else {
521 521 ret = (zpool_add(zhp, nvroot) != 0);
522 522 }
523 523
524 524 nvlist_free(nvroot);
525 525 zpool_close(zhp);
526 526
527 527 return (ret);
528 528 }
529 529
530 530 /*
531 531 * zpool remove <pool> <vdev> ...
532 532 *
533 533 * Removes the given vdev from the pool. Currently, this supports removing
534 534 * spares, cache, and log devices from the pool.
535 535 */
536 536 int
537 537 zpool_do_remove(int argc, char **argv)
538 538 {
539 539 char *poolname;
540 540 int i, ret = 0;
541 541 zpool_handle_t *zhp;
542 542
543 543 argc--;
544 544 argv++;
545 545
546 546 /* get pool name and check number of arguments */
547 547 if (argc < 1) {
548 548 (void) fprintf(stderr, gettext("missing pool name argument\n"));
549 549 usage(B_FALSE);
550 550 }
551 551 if (argc < 2) {
552 552 (void) fprintf(stderr, gettext("missing device\n"));
553 553 usage(B_FALSE);
554 554 }
555 555
556 556 poolname = argv[0];
557 557
558 558 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
559 559 return (1);
560 560
561 561 for (i = 1; i < argc; i++) {
562 562 if (zpool_vdev_remove(zhp, argv[i]) != 0)
563 563 ret = 1;
564 564 }
565 565
566 566 return (ret);
567 567 }
568 568
569 569 /*
570 570 * zpool create [-fn] [-o property=value] ...
571 571 * [-O file-system-property=value] ...
572 572 * [-R root] [-m mountpoint] <pool> <dev> ...
573 573 *
574 574 * -f Force creation, even if devices appear in use
575 575 * -n Do not create the pool, but display the resulting layout if it
576 576 * were to be created.
577 577 * -R Create a pool under an alternate root
578 578 * -m Set default mountpoint for the root dataset. By default it's
579 579 * '/<pool>'
580 580 * -o Set property=value.
581 581 * -O Set fsproperty=value in the pool's root file system
582 582 *
583 583 * Creates the named pool according to the given vdev specification. The
584 584 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
585 585 * we get the nvlist back from get_vdev_spec(), we either print out the contents
586 586 * (if '-n' was specified), or pass it to libzfs to do the creation.
587 587 */
588 588 int
589 589 zpool_do_create(int argc, char **argv)
590 590 {
591 591 boolean_t force = B_FALSE;
592 592 boolean_t dryrun = B_FALSE;
593 593 int c;
594 594 nvlist_t *nvroot = NULL;
595 595 char *poolname;
596 596 int ret = 1;
597 597 char *altroot = NULL;
598 598 char *mountpoint = NULL;
599 599 nvlist_t *fsprops = NULL;
600 600 nvlist_t *props = NULL;
601 601 char *propval;
602 602
603 603 /* check options */
604 604 while ((c = getopt(argc, argv, ":fnR:m:o:O:")) != -1) {
605 605 switch (c) {
606 606 case 'f':
607 607 force = B_TRUE;
608 608 break;
609 609 case 'n':
610 610 dryrun = B_TRUE;
611 611 break;
612 612 case 'R':
613 613 altroot = optarg;
614 614 if (add_prop_list(zpool_prop_to_name(
615 615 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
616 616 goto errout;
617 617 if (nvlist_lookup_string(props,
618 618 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
619 619 &propval) == 0)
620 620 break;
621 621 if (add_prop_list(zpool_prop_to_name(
622 622 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
623 623 goto errout;
624 624 break;
625 625 case 'm':
626 626 mountpoint = optarg;
627 627 break;
628 628 case 'o':
629 629 if ((propval = strchr(optarg, '=')) == NULL) {
630 630 (void) fprintf(stderr, gettext("missing "
631 631 "'=' for -o option\n"));
632 632 goto errout;
633 633 }
634 634 *propval = '\0';
635 635 propval++;
636 636
637 637 if (add_prop_list(optarg, propval, &props, B_TRUE))
638 638 goto errout;
639 639 break;
640 640 case 'O':
641 641 if ((propval = strchr(optarg, '=')) == NULL) {
642 642 (void) fprintf(stderr, gettext("missing "
643 643 "'=' for -O option\n"));
644 644 goto errout;
645 645 }
646 646 *propval = '\0';
647 647 propval++;
648 648
649 649 if (add_prop_list(optarg, propval, &fsprops, B_FALSE))
650 650 goto errout;
651 651 break;
652 652 case ':':
653 653 (void) fprintf(stderr, gettext("missing argument for "
654 654 "'%c' option\n"), optopt);
655 655 goto badusage;
656 656 case '?':
657 657 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
658 658 optopt);
659 659 goto badusage;
660 660 }
661 661 }
662 662
663 663 argc -= optind;
664 664 argv += optind;
665 665
666 666 /* get pool name and check number of arguments */
667 667 if (argc < 1) {
668 668 (void) fprintf(stderr, gettext("missing pool name argument\n"));
669 669 goto badusage;
670 670 }
671 671 if (argc < 2) {
672 672 (void) fprintf(stderr, gettext("missing vdev specification\n"));
673 673 goto badusage;
674 674 }
675 675
676 676 poolname = argv[0];
677 677
678 678 /*
679 679 * As a special case, check for use of '/' in the name, and direct the
680 680 * user to use 'zfs create' instead.
681 681 */
682 682 if (strchr(poolname, '/') != NULL) {
683 683 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
684 684 "character '/' in pool name\n"), poolname);
685 685 (void) fprintf(stderr, gettext("use 'zfs create' to "
686 686 "create a dataset\n"));
687 687 goto errout;
688 688 }
689 689
690 690 /* pass off to get_vdev_spec for bulk processing */
691 691 nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun,
692 692 argc - 1, argv + 1);
693 693 if (nvroot == NULL)
694 694 goto errout;
695 695
696 696 /* make_root_vdev() allows 0 toplevel children if there are spares */
697 697 if (!zfs_allocatable_devs(nvroot)) {
698 698 (void) fprintf(stderr, gettext("invalid vdev "
699 699 "specification: at least one toplevel vdev must be "
700 700 "specified\n"));
701 701 goto errout;
702 702 }
703 703
704 704
705 705 if (altroot != NULL && altroot[0] != '/') {
706 706 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
707 707 "must be an absolute path\n"), altroot);
708 708 goto errout;
709 709 }
710 710
711 711 /*
712 712 * Check the validity of the mountpoint and direct the user to use the
713 713 * '-m' mountpoint option if it looks like its in use.
714 714 */
715 715 if (mountpoint == NULL ||
716 716 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
717 717 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
718 718 char buf[MAXPATHLEN];
719 719 DIR *dirp;
720 720
721 721 if (mountpoint && mountpoint[0] != '/') {
722 722 (void) fprintf(stderr, gettext("invalid mountpoint "
723 723 "'%s': must be an absolute path, 'legacy', or "
724 724 "'none'\n"), mountpoint);
725 725 goto errout;
726 726 }
727 727
728 728 if (mountpoint == NULL) {
729 729 if (altroot != NULL)
730 730 (void) snprintf(buf, sizeof (buf), "%s/%s",
731 731 altroot, poolname);
732 732 else
733 733 (void) snprintf(buf, sizeof (buf), "/%s",
734 734 poolname);
735 735 } else {
736 736 if (altroot != NULL)
737 737 (void) snprintf(buf, sizeof (buf), "%s%s",
738 738 altroot, mountpoint);
739 739 else
740 740 (void) snprintf(buf, sizeof (buf), "%s",
741 741 mountpoint);
742 742 }
743 743
744 744 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
745 745 (void) fprintf(stderr, gettext("mountpoint '%s' : "
746 746 "%s\n"), buf, strerror(errno));
747 747 (void) fprintf(stderr, gettext("use '-m' "
748 748 "option to provide a different default\n"));
749 749 goto errout;
750 750 } else if (dirp) {
751 751 int count = 0;
752 752
753 753 while (count < 3 && readdir(dirp) != NULL)
754 754 count++;
755 755 (void) closedir(dirp);
756 756
757 757 if (count > 2) {
758 758 (void) fprintf(stderr, gettext("mountpoint "
759 759 "'%s' exists and is not empty\n"), buf);
760 760 (void) fprintf(stderr, gettext("use '-m' "
761 761 "option to provide a "
762 762 "different default\n"));
763 763 goto errout;
764 764 }
765 765 }
766 766 }
767 767
768 768 if (dryrun) {
769 769 /*
770 770 * For a dry run invocation, print out a basic message and run
771 771 * through all the vdevs in the list and print out in an
772 772 * appropriate hierarchy.
773 773 */
774 774 (void) printf(gettext("would create '%s' with the "
775 775 "following layout:\n\n"), poolname);
776 776
777 777 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
778 778 if (num_logs(nvroot) > 0)
779 779 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
780 780
781 781 ret = 0;
782 782 } else {
783 783 /*
784 784 * Hand off to libzfs.
785 785 */
786 786 if (zpool_create(g_zfs, poolname,
787 787 nvroot, props, fsprops) == 0) {
788 788 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
789 789 ZFS_TYPE_FILESYSTEM);
790 790 if (pool != NULL) {
791 791 if (mountpoint != NULL)
792 792 verify(zfs_prop_set(pool,
793 793 zfs_prop_to_name(
794 794 ZFS_PROP_MOUNTPOINT),
795 795 mountpoint) == 0);
796 796 if (zfs_mount(pool, NULL, 0) == 0)
797 797 ret = zfs_shareall(pool);
798 798 zfs_close(pool);
799 799 }
800 800 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
801 801 (void) fprintf(stderr, gettext("pool name may have "
802 802 "been omitted\n"));
803 803 }
804 804 }
805 805
806 806 errout:
807 807 nvlist_free(nvroot);
808 808 nvlist_free(fsprops);
809 809 nvlist_free(props);
810 810 return (ret);
811 811 badusage:
812 812 nvlist_free(fsprops);
813 813 nvlist_free(props);
814 814 usage(B_FALSE);
815 815 return (2);
816 816 }
817 817
818 818 /*
819 819 * zpool destroy <pool>
820 820 *
821 821 * -f Forcefully unmount any datasets
822 822 *
823 823 * Destroy the given pool. Automatically unmounts any datasets in the pool.
824 824 */
825 825 int
826 826 zpool_do_destroy(int argc, char **argv)
827 827 {
828 828 boolean_t force = B_FALSE;
829 829 int c;
830 830 char *pool;
831 831 zpool_handle_t *zhp;
832 832 int ret;
833 833
834 834 /* check options */
835 835 while ((c = getopt(argc, argv, "f")) != -1) {
836 836 switch (c) {
837 837 case 'f':
838 838 force = B_TRUE;
839 839 break;
840 840 case '?':
841 841 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
842 842 optopt);
843 843 usage(B_FALSE);
844 844 }
845 845 }
846 846
847 847 argc -= optind;
848 848 argv += optind;
849 849
850 850 /* check arguments */
851 851 if (argc < 1) {
852 852 (void) fprintf(stderr, gettext("missing pool argument\n"));
853 853 usage(B_FALSE);
854 854 }
855 855 if (argc > 1) {
856 856 (void) fprintf(stderr, gettext("too many arguments\n"));
857 857 usage(B_FALSE);
858 858 }
859 859
860 860 pool = argv[0];
861 861
862 862 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
863 863 /*
864 864 * As a special case, check for use of '/' in the name, and
865 865 * direct the user to use 'zfs destroy' instead.
866 866 */
867 867 if (strchr(pool, '/') != NULL)
868 868 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
869 869 "destroy a dataset\n"));
870 870 return (1);
871 871 }
872 872
873 873 if (zpool_disable_datasets(zhp, force) != 0) {
874 874 (void) fprintf(stderr, gettext("could not destroy '%s': "
875 875 "could not unmount datasets\n"), zpool_get_name(zhp));
876 876 return (1);
877 877 }
878 878
879 879 ret = (zpool_destroy(zhp) != 0);
880 880
881 881 zpool_close(zhp);
882 882
883 883 return (ret);
884 884 }
885 885
886 886 /*
887 887 * zpool export [-f] <pool> ...
888 888 *
889 889 * -f Forcefully unmount datasets
890 890 *
891 891 * Export the given pools. By default, the command will attempt to cleanly
892 892 * unmount any active datasets within the pool. If the '-f' flag is specified,
893 893 * then the datasets will be forcefully unmounted.
894 894 */
895 895 int
896 896 zpool_do_export(int argc, char **argv)
897 897 {
898 898 boolean_t force = B_FALSE;
899 899 boolean_t hardforce = B_FALSE;
900 900 int c;
901 901 zpool_handle_t *zhp;
902 902 int ret;
903 903 int i;
904 904
905 905 /* check options */
906 906 while ((c = getopt(argc, argv, "fF")) != -1) {
907 907 switch (c) {
908 908 case 'f':
909 909 force = B_TRUE;
910 910 break;
911 911 case 'F':
912 912 hardforce = B_TRUE;
913 913 break;
914 914 case '?':
915 915 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
916 916 optopt);
917 917 usage(B_FALSE);
918 918 }
919 919 }
920 920
921 921 argc -= optind;
922 922 argv += optind;
923 923
924 924 /* check arguments */
925 925 if (argc < 1) {
926 926 (void) fprintf(stderr, gettext("missing pool argument\n"));
927 927 usage(B_FALSE);
928 928 }
929 929
930 930 ret = 0;
931 931 for (i = 0; i < argc; i++) {
932 932 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
933 933 ret = 1;
934 934 continue;
935 935 }
936 936
937 937 if (zpool_disable_datasets(zhp, force) != 0) {
938 938 ret = 1;
939 939 zpool_close(zhp);
940 940 continue;
941 941 }
942 942
943 943 if (hardforce) {
944 944 if (zpool_export_force(zhp) != 0)
945 945 ret = 1;
946 946 } else if (zpool_export(zhp, force) != 0) {
947 947 ret = 1;
948 948 }
949 949
950 950 zpool_close(zhp);
951 951 }
952 952
953 953 return (ret);
954 954 }
955 955
956 956 /*
957 957 * Given a vdev configuration, determine the maximum width needed for the device
958 958 * name column.
959 959 */
960 960 static int
961 961 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
962 962 {
963 963 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
964 964 nvlist_t **child;
965 965 uint_t c, children;
966 966 int ret;
967 967
968 968 if (strlen(name) + depth > max)
969 969 max = strlen(name) + depth;
970 970
971 971 free(name);
972 972
973 973 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
974 974 &child, &children) == 0) {
975 975 for (c = 0; c < children; c++)
976 976 if ((ret = max_width(zhp, child[c], depth + 2,
977 977 max)) > max)
978 978 max = ret;
979 979 }
980 980
981 981 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
982 982 &child, &children) == 0) {
983 983 for (c = 0; c < children; c++)
984 984 if ((ret = max_width(zhp, child[c], depth + 2,
985 985 max)) > max)
986 986 max = ret;
987 987 }
988 988
989 989 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
990 990 &child, &children) == 0) {
991 991 for (c = 0; c < children; c++)
992 992 if ((ret = max_width(zhp, child[c], depth + 2,
993 993 max)) > max)
994 994 max = ret;
995 995 }
996 996
997 997
998 998 return (max);
999 999 }
1000 1000
1001 1001 typedef struct spare_cbdata {
1002 1002 uint64_t cb_guid;
1003 1003 zpool_handle_t *cb_zhp;
1004 1004 } spare_cbdata_t;
1005 1005
1006 1006 static boolean_t
1007 1007 find_vdev(nvlist_t *nv, uint64_t search)
1008 1008 {
1009 1009 uint64_t guid;
1010 1010 nvlist_t **child;
1011 1011 uint_t c, children;
1012 1012
1013 1013 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1014 1014 search == guid)
1015 1015 return (B_TRUE);
1016 1016
1017 1017 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1018 1018 &child, &children) == 0) {
1019 1019 for (c = 0; c < children; c++)
1020 1020 if (find_vdev(child[c], search))
1021 1021 return (B_TRUE);
1022 1022 }
1023 1023
1024 1024 return (B_FALSE);
1025 1025 }
1026 1026
1027 1027 static int
1028 1028 find_spare(zpool_handle_t *zhp, void *data)
1029 1029 {
1030 1030 spare_cbdata_t *cbp = data;
1031 1031 nvlist_t *config, *nvroot;
1032 1032
1033 1033 config = zpool_get_config(zhp, NULL);
1034 1034 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1035 1035 &nvroot) == 0);
1036 1036
1037 1037 if (find_vdev(nvroot, cbp->cb_guid)) {
1038 1038 cbp->cb_zhp = zhp;
1039 1039 return (1);
1040 1040 }
1041 1041
1042 1042 zpool_close(zhp);
1043 1043 return (0);
1044 1044 }
1045 1045
1046 1046 /*
1047 1047 * Print out configuration state as requested by status_callback.
1048 1048 */
1049 1049 void
1050 1050 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1051 1051 int namewidth, int depth, boolean_t isspare)
1052 1052 {
1053 1053 nvlist_t **child;
1054 1054 uint_t c, children;
1055 1055 pool_scan_stat_t *ps = NULL;
1056 1056 vdev_stat_t *vs;
1057 1057 char rbuf[6], wbuf[6], cbuf[6];
1058 1058 char *vname;
1059 1059 uint64_t notpresent;
1060 1060 spare_cbdata_t cb;
1061 1061 char *state;
1062 1062
1063 1063 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1064 1064 &child, &children) != 0)
1065 1065 children = 0;
1066 1066
1067 1067 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1068 1068 (uint64_t **)&vs, &c) == 0);
1069 1069
1070 1070 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1071 1071 if (isspare) {
1072 1072 /*
1073 1073 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1074 1074 * online drives.
1075 1075 */
1076 1076 if (vs->vs_aux == VDEV_AUX_SPARED)
1077 1077 state = "INUSE";
1078 1078 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1079 1079 state = "AVAIL";
1080 1080 }
1081 1081
1082 1082 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1083 1083 name, state);
1084 1084
1085 1085 if (!isspare) {
1086 1086 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1087 1087 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1088 1088 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1089 1089 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1090 1090 }
1091 1091
1092 1092 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1093 1093 ¬present) == 0) {
1094 1094 char *path;
1095 1095 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1096 1096 (void) printf(" was %s", path);
1097 1097 } else if (vs->vs_aux != 0) {
1098 1098 (void) printf(" ");
1099 1099
1100 1100 switch (vs->vs_aux) {
1101 1101 case VDEV_AUX_OPEN_FAILED:
1102 1102 (void) printf(gettext("cannot open"));
1103 1103 break;
1104 1104
1105 1105 case VDEV_AUX_BAD_GUID_SUM:
1106 1106 (void) printf(gettext("missing device"));
1107 1107 break;
1108 1108
1109 1109 case VDEV_AUX_NO_REPLICAS:
1110 1110 (void) printf(gettext("insufficient replicas"));
1111 1111 break;
1112 1112
1113 1113 case VDEV_AUX_VERSION_NEWER:
1114 1114 (void) printf(gettext("newer version"));
1115 1115 break;
1116 1116
1117 1117 case VDEV_AUX_SPARED:
1118 1118 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1119 1119 &cb.cb_guid) == 0);
1120 1120 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1121 1121 if (strcmp(zpool_get_name(cb.cb_zhp),
1122 1122 zpool_get_name(zhp)) == 0)
1123 1123 (void) printf(gettext("currently in "
1124 1124 "use"));
1125 1125 else
1126 1126 (void) printf(gettext("in use by "
1127 1127 "pool '%s'"),
1128 1128 zpool_get_name(cb.cb_zhp));
1129 1129 zpool_close(cb.cb_zhp);
1130 1130 } else {
1131 1131 (void) printf(gettext("currently in use"));
1132 1132 }
1133 1133 break;
1134 1134
1135 1135 case VDEV_AUX_ERR_EXCEEDED:
1136 1136 (void) printf(gettext("too many errors"));
1137 1137 break;
1138 1138
1139 1139 case VDEV_AUX_IO_FAILURE:
1140 1140 (void) printf(gettext("experienced I/O failures"));
1141 1141 break;
1142 1142
1143 1143 case VDEV_AUX_BAD_LOG:
1144 1144 (void) printf(gettext("bad intent log"));
1145 1145 break;
1146 1146
1147 1147 case VDEV_AUX_EXTERNAL:
1148 1148 (void) printf(gettext("external device fault"));
1149 1149 break;
1150 1150
1151 1151 case VDEV_AUX_SPLIT_POOL:
1152 1152 (void) printf(gettext("split into new pool"));
1153 1153 break;
1154 1154
1155 1155 default:
1156 1156 (void) printf(gettext("corrupted data"));
1157 1157 break;
1158 1158 }
1159 1159 }
1160 1160
1161 1161 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1162 1162 (uint64_t **)&ps, &c);
1163 1163
1164 1164 if (ps && ps->pss_state == DSS_SCANNING &&
1165 1165 vs->vs_scan_processed != 0 && children == 0) {
1166 1166 (void) printf(gettext(" (%s)"),
1167 1167 (ps->pss_func == POOL_SCAN_RESILVER) ?
1168 1168 "resilvering" : "repairing");
1169 1169 }
1170 1170
1171 1171 (void) printf("\n");
1172 1172
1173 1173 for (c = 0; c < children; c++) {
1174 1174 uint64_t islog = B_FALSE, ishole = B_FALSE;
1175 1175
1176 1176 /* Don't print logs or holes here */
1177 1177 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1178 1178 &islog);
1179 1179 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1180 1180 &ishole);
1181 1181 if (islog || ishole)
1182 1182 continue;
1183 1183 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1184 1184 print_status_config(zhp, vname, child[c],
1185 1185 namewidth, depth + 2, isspare);
1186 1186 free(vname);
1187 1187 }
1188 1188 }
1189 1189
1190 1190
1191 1191 /*
1192 1192 * Print the configuration of an exported pool. Iterate over all vdevs in the
1193 1193 * pool, printing out the name and status for each one.
1194 1194 */
1195 1195 void
1196 1196 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1197 1197 {
1198 1198 nvlist_t **child;
1199 1199 uint_t c, children;
1200 1200 vdev_stat_t *vs;
1201 1201 char *type, *vname;
1202 1202
1203 1203 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1204 1204 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1205 1205 strcmp(type, VDEV_TYPE_HOLE) == 0)
1206 1206 return;
1207 1207
1208 1208 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1209 1209 (uint64_t **)&vs, &c) == 0);
1210 1210
1211 1211 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1212 1212 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1213 1213
1214 1214 if (vs->vs_aux != 0) {
1215 1215 (void) printf(" ");
1216 1216
1217 1217 switch (vs->vs_aux) {
1218 1218 case VDEV_AUX_OPEN_FAILED:
1219 1219 (void) printf(gettext("cannot open"));
1220 1220 break;
1221 1221
1222 1222 case VDEV_AUX_BAD_GUID_SUM:
1223 1223 (void) printf(gettext("missing device"));
1224 1224 break;
1225 1225
1226 1226 case VDEV_AUX_NO_REPLICAS:
1227 1227 (void) printf(gettext("insufficient replicas"));
1228 1228 break;
1229 1229
1230 1230 case VDEV_AUX_VERSION_NEWER:
1231 1231 (void) printf(gettext("newer version"));
1232 1232 break;
1233 1233
1234 1234 case VDEV_AUX_ERR_EXCEEDED:
1235 1235 (void) printf(gettext("too many errors"));
1236 1236 break;
1237 1237
1238 1238 default:
1239 1239 (void) printf(gettext("corrupted data"));
1240 1240 break;
1241 1241 }
1242 1242 }
1243 1243 (void) printf("\n");
1244 1244
1245 1245 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1246 1246 &child, &children) != 0)
1247 1247 return;
1248 1248
1249 1249 for (c = 0; c < children; c++) {
1250 1250 uint64_t is_log = B_FALSE;
1251 1251
1252 1252 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1253 1253 &is_log);
1254 1254 if (is_log)
1255 1255 continue;
1256 1256
1257 1257 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1258 1258 print_import_config(vname, child[c], namewidth, depth + 2);
1259 1259 free(vname);
1260 1260 }
1261 1261
1262 1262 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1263 1263 &child, &children) == 0) {
1264 1264 (void) printf(gettext("\tcache\n"));
1265 1265 for (c = 0; c < children; c++) {
1266 1266 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1267 1267 (void) printf("\t %s\n", vname);
1268 1268 free(vname);
1269 1269 }
1270 1270 }
1271 1271
1272 1272 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1273 1273 &child, &children) == 0) {
1274 1274 (void) printf(gettext("\tspares\n"));
1275 1275 for (c = 0; c < children; c++) {
1276 1276 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1277 1277 (void) printf("\t %s\n", vname);
1278 1278 free(vname);
1279 1279 }
1280 1280 }
1281 1281 }
1282 1282
1283 1283 /*
1284 1284 * Print log vdevs.
1285 1285 * Logs are recorded as top level vdevs in the main pool child array
1286 1286 * but with "is_log" set to 1. We use either print_status_config() or
1287 1287 * print_import_config() to print the top level logs then any log
1288 1288 * children (eg mirrored slogs) are printed recursively - which
1289 1289 * works because only the top level vdev is marked "is_log"
1290 1290 */
1291 1291 static void
1292 1292 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1293 1293 {
1294 1294 uint_t c, children;
1295 1295 nvlist_t **child;
1296 1296
1297 1297 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1298 1298 &children) != 0)
1299 1299 return;
1300 1300
1301 1301 (void) printf(gettext("\tlogs\n"));
1302 1302
1303 1303 for (c = 0; c < children; c++) {
1304 1304 uint64_t is_log = B_FALSE;
1305 1305 char *name;
1306 1306
1307 1307 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1308 1308 &is_log);
1309 1309 if (!is_log)
1310 1310 continue;
1311 1311 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1312 1312 if (verbose)
1313 1313 print_status_config(zhp, name, child[c], namewidth,
1314 1314 2, B_FALSE);
1315 1315 else
1316 1316 print_import_config(name, child[c], namewidth, 2);
1317 1317 free(name);
1318 1318 }
1319 1319 }
1320 1320
1321 1321 /*
1322 1322 * Display the status for the given pool.
1323 1323 */
1324 1324 static void
1325 1325 show_import(nvlist_t *config)
1326 1326 {
|
↓ open down ↓ |
1326 lines elided |
↑ open up ↑ |
1327 1327 uint64_t pool_state;
1328 1328 vdev_stat_t *vs;
1329 1329 char *name;
1330 1330 uint64_t guid;
1331 1331 char *msgid;
1332 1332 nvlist_t *nvroot;
1333 1333 int reason;
1334 1334 const char *health;
1335 1335 uint_t vsc;
1336 1336 int namewidth;
1337 + char *comment;
1337 1338
1338 1339 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1339 1340 &name) == 0);
1340 1341 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1341 1342 &guid) == 0);
1342 1343 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1343 1344 &pool_state) == 0);
1344 1345 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1345 1346 &nvroot) == 0);
1346 1347
1347 1348 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1348 1349 (uint64_t **)&vs, &vsc) == 0);
1349 1350 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1350 1351
1351 1352 reason = zpool_import_status(config, &msgid);
1352 1353
1353 - (void) printf(gettext(" pool: %s\n"), name);
1354 - (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1355 - (void) printf(gettext(" state: %s"), health);
1354 + (void) printf(gettext(" pool: %s\n"), name);
1355 + (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1356 + (void) printf(gettext(" state: %s"), health);
1356 1357 if (pool_state == POOL_STATE_DESTROYED)
1357 1358 (void) printf(gettext(" (DESTROYED)"));
1358 1359 (void) printf("\n");
1359 1360
1360 1361 switch (reason) {
1361 1362 case ZPOOL_STATUS_MISSING_DEV_R:
1362 1363 case ZPOOL_STATUS_MISSING_DEV_NR:
1363 1364 case ZPOOL_STATUS_BAD_GUID_SUM:
1364 - (void) printf(gettext("status: One or more devices are missing "
1365 - "from the system.\n"));
1365 + (void) printf(gettext(" status: One or more devices are "
1366 + "missing from the system.\n"));
1366 1367 break;
1367 1368
1368 1369 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1369 1370 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1370 - (void) printf(gettext("status: One or more devices contains "
1371 + (void) printf(gettext(" status: One or more devices contains "
1371 1372 "corrupted data.\n"));
1372 1373 break;
1373 1374
1374 1375 case ZPOOL_STATUS_CORRUPT_DATA:
1375 - (void) printf(gettext("status: The pool data is corrupted.\n"));
1376 + (void) printf(
1377 + gettext(" status: The pool data is corrupted.\n"));
1376 1378 break;
1377 1379
1378 1380 case ZPOOL_STATUS_OFFLINE_DEV:
1379 - (void) printf(gettext("status: One or more devices "
1381 + (void) printf(gettext(" status: One or more devices "
1380 1382 "are offlined.\n"));
1381 1383 break;
1382 1384
1383 1385 case ZPOOL_STATUS_CORRUPT_POOL:
1384 - (void) printf(gettext("status: The pool metadata is "
1386 + (void) printf(gettext(" status: The pool metadata is "
1385 1387 "corrupted.\n"));
1386 1388 break;
1387 1389
1388 1390 case ZPOOL_STATUS_VERSION_OLDER:
1389 - (void) printf(gettext("status: The pool is formatted using an "
1391 + (void) printf(gettext(" status: The pool is formatted using an "
1390 1392 "older on-disk version.\n"));
1391 1393 break;
1392 1394
1393 1395 case ZPOOL_STATUS_VERSION_NEWER:
1394 - (void) printf(gettext("status: The pool is formatted using an "
1396 + (void) printf(gettext(" status: The pool is formatted using an "
1395 1397 "incompatible version.\n"));
1396 1398 break;
1397 1399
1398 1400 case ZPOOL_STATUS_HOSTID_MISMATCH:
1399 - (void) printf(gettext("status: The pool was last accessed by "
1401 + (void) printf(gettext(" status: The pool was last accessed by "
1400 1402 "another system.\n"));
1401 1403 break;
1402 1404
1403 1405 case ZPOOL_STATUS_FAULTED_DEV_R:
1404 1406 case ZPOOL_STATUS_FAULTED_DEV_NR:
1405 - (void) printf(gettext("status: One or more devices are "
1407 + (void) printf(gettext(" status: One or more devices are "
1406 1408 "faulted.\n"));
1407 1409 break;
1408 1410
1409 1411 case ZPOOL_STATUS_BAD_LOG:
1410 - (void) printf(gettext("status: An intent log record cannot be "
1412 + (void) printf(gettext(" status: An intent log record cannot be "
1411 1413 "read.\n"));
1412 1414 break;
1413 1415
1414 1416 case ZPOOL_STATUS_RESILVERING:
1415 - (void) printf(gettext("status: One or more devices were being "
1417 + (void) printf(gettext(" status: One or more devices were being "
1416 1418 "resilvered.\n"));
1417 1419 break;
1418 1420
1419 1421 default:
1420 1422 /*
1421 1423 * No other status can be seen when importing pools.
1422 1424 */
1423 1425 assert(reason == ZPOOL_STATUS_OK);
1424 1426 }
1425 1427
1426 1428 /*
1427 1429 * Print out an action according to the overall state of the pool.
1428 1430 */
1429 1431 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1430 1432 if (reason == ZPOOL_STATUS_VERSION_OLDER)
1431 - (void) printf(gettext("action: The pool can be "
1433 + (void) printf(gettext(" action: The pool can be "
1432 1434 "imported using its name or numeric identifier, "
1433 1435 "though\n\tsome features will not be available "
1434 1436 "without an explicit 'zpool upgrade'.\n"));
1435 1437 else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
1436 - (void) printf(gettext("action: The pool can be "
1438 + (void) printf(gettext(" action: The pool can be "
1437 1439 "imported using its name or numeric "
1438 1440 "identifier and\n\tthe '-f' flag.\n"));
1439 1441 else
1440 - (void) printf(gettext("action: The pool can be "
1442 + (void) printf(gettext(" action: The pool can be "
1441 1443 "imported using its name or numeric "
1442 1444 "identifier.\n"));
1443 1445 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1444 - (void) printf(gettext("action: The pool can be imported "
1446 + (void) printf(gettext(" action: The pool can be imported "
1445 1447 "despite missing or damaged devices. The\n\tfault "
1446 1448 "tolerance of the pool may be compromised if imported.\n"));
1447 1449 } else {
1448 1450 switch (reason) {
1449 1451 case ZPOOL_STATUS_VERSION_NEWER:
1450 - (void) printf(gettext("action: The pool cannot be "
1452 + (void) printf(gettext(" action: The pool cannot be "
1451 1453 "imported. Access the pool on a system running "
1452 1454 "newer\n\tsoftware, or recreate the pool from "
1453 1455 "backup.\n"));
1454 1456 break;
1455 1457 case ZPOOL_STATUS_MISSING_DEV_R:
1456 1458 case ZPOOL_STATUS_MISSING_DEV_NR:
1457 1459 case ZPOOL_STATUS_BAD_GUID_SUM:
1458 - (void) printf(gettext("action: The pool cannot be "
1460 + (void) printf(gettext(" action: The pool cannot be "
1459 1461 "imported. Attach the missing\n\tdevices and try "
1460 1462 "again.\n"));
1461 1463 break;
1462 1464 default:
1463 - (void) printf(gettext("action: The pool cannot be "
1465 + (void) printf(gettext(" action: The pool cannot be "
1464 1466 "imported due to damaged devices or data.\n"));
1465 1467 }
1466 1468 }
1467 1469
1470 + /* Print the comment attached to the pool. */
1471 + if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1472 + (void) printf(gettext("comment: %s\n"), comment);
1473 +
1468 1474 /*
1469 1475 * If the state is "closed" or "can't open", and the aux state
1470 1476 * is "corrupt data":
1471 1477 */
1472 1478 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1473 1479 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1474 1480 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1475 1481 if (pool_state == POOL_STATE_DESTROYED)
1476 1482 (void) printf(gettext("\tThe pool was destroyed, "
1477 1483 "but can be imported using the '-Df' flags.\n"));
1478 1484 else if (pool_state != POOL_STATE_EXPORTED)
1479 1485 (void) printf(gettext("\tThe pool may be active on "
1480 1486 "another system, but can be imported using\n\t"
1481 1487 "the '-f' flag.\n"));
1482 1488 }
1483 1489
1484 1490 if (msgid != NULL)
1485 1491 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
1486 1492 msgid);
1487 1493
1488 - (void) printf(gettext("config:\n\n"));
1494 + (void) printf(gettext(" config:\n\n"));
1489 1495
1490 1496 namewidth = max_width(NULL, nvroot, 0, 0);
1491 1497 if (namewidth < 10)
1492 1498 namewidth = 10;
1493 1499
1494 1500 print_import_config(name, nvroot, namewidth, 0);
1495 1501 if (num_logs(nvroot) > 0)
1496 1502 print_logs(NULL, nvroot, namewidth, B_FALSE);
1497 1503
1498 1504 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1499 1505 (void) printf(gettext("\n\tAdditional devices are known to "
1500 1506 "be part of this pool, though their\n\texact "
1501 1507 "configuration cannot be determined.\n"));
1502 1508 }
1503 1509 }
1504 1510
1505 1511 /*
1506 1512 * Perform the import for the given configuration. This passes the heavy
1507 1513 * lifting off to zpool_import_props(), and then mounts the datasets contained
1508 1514 * within the pool.
1509 1515 */
1510 1516 static int
1511 1517 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1512 1518 nvlist_t *props, int flags)
1513 1519 {
1514 1520 zpool_handle_t *zhp;
1515 1521 char *name;
1516 1522 uint64_t state;
1517 1523 uint64_t version;
1518 1524
1519 1525 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1520 1526 &name) == 0);
1521 1527
1522 1528 verify(nvlist_lookup_uint64(config,
1523 1529 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1524 1530 verify(nvlist_lookup_uint64(config,
1525 1531 ZPOOL_CONFIG_VERSION, &version) == 0);
1526 1532 if (version > SPA_VERSION) {
1527 1533 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1528 1534 "is formatted using a newer ZFS version\n"), name);
1529 1535 return (1);
1530 1536 } else if (state != POOL_STATE_EXPORTED &&
1531 1537 !(flags & ZFS_IMPORT_ANY_HOST)) {
1532 1538 uint64_t hostid;
1533 1539
1534 1540 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1535 1541 &hostid) == 0) {
1536 1542 if ((unsigned long)hostid != gethostid()) {
1537 1543 char *hostname;
1538 1544 uint64_t timestamp;
1539 1545 time_t t;
1540 1546
1541 1547 verify(nvlist_lookup_string(config,
1542 1548 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1543 1549 verify(nvlist_lookup_uint64(config,
1544 1550 ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0);
1545 1551 t = timestamp;
1546 1552 (void) fprintf(stderr, gettext("cannot import "
1547 1553 "'%s': pool may be in use from other "
1548 1554 "system, it was last accessed by %s "
1549 1555 "(hostid: 0x%lx) on %s"), name, hostname,
1550 1556 (unsigned long)hostid,
1551 1557 asctime(localtime(&t)));
1552 1558 (void) fprintf(stderr, gettext("use '-f' to "
1553 1559 "import anyway\n"));
1554 1560 return (1);
1555 1561 }
1556 1562 } else {
1557 1563 (void) fprintf(stderr, gettext("cannot import '%s': "
1558 1564 "pool may be in use from other system\n"), name);
1559 1565 (void) fprintf(stderr, gettext("use '-f' to import "
1560 1566 "anyway\n"));
1561 1567 return (1);
1562 1568 }
1563 1569 }
1564 1570
1565 1571 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1566 1572 return (1);
1567 1573
1568 1574 if (newname != NULL)
1569 1575 name = (char *)newname;
1570 1576
1571 1577 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1572 1578 return (1);
1573 1579
1574 1580 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1575 1581 !(flags & ZFS_IMPORT_ONLY) &&
1576 1582 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1577 1583 zpool_close(zhp);
1578 1584 return (1);
1579 1585 }
1580 1586
1581 1587 zpool_close(zhp);
1582 1588 return (0);
1583 1589 }
1584 1590
1585 1591 /*
1586 1592 * zpool import [-d dir] [-D]
1587 1593 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1588 1594 * [-d dir | -c cachefile] [-f] -a
1589 1595 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1590 1596 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1591 1597 *
1592 1598 * -c Read pool information from a cachefile instead of searching
1593 1599 * devices.
1594 1600 *
1595 1601 * -d Scan in a specific directory, other than /dev/dsk. More than
1596 1602 * one directory can be specified using multiple '-d' options.
1597 1603 *
1598 1604 * -D Scan for previously destroyed pools or import all or only
1599 1605 * specified destroyed pools.
1600 1606 *
1601 1607 * -R Temporarily import the pool, with all mountpoints relative to
1602 1608 * the given root. The pool will remain exported when the machine
1603 1609 * is rebooted.
1604 1610 *
1605 1611 * -V Import even in the presence of faulted vdevs. This is an
1606 1612 * intentionally undocumented option for testing purposes, and
1607 1613 * treats the pool configuration as complete, leaving any bad
1608 1614 * vdevs in the FAULTED state. In other words, it does verbatim
1609 1615 * import.
1610 1616 *
1611 1617 * -f Force import, even if it appears that the pool is active.
1612 1618 *
1613 1619 * -F Attempt rewind if necessary.
1614 1620 *
1615 1621 * -n See if rewind would work, but don't actually rewind.
1616 1622 *
1617 1623 * -N Import the pool but don't mount datasets.
1618 1624 *
1619 1625 * -T Specify a starting txg to use for import. This option is
1620 1626 * intentionally undocumented option for testing purposes.
1621 1627 *
1622 1628 * -a Import all pools found.
1623 1629 *
1624 1630 * -o Set property=value and/or temporary mount options (without '=').
1625 1631 *
1626 1632 * The import command scans for pools to import, and import pools based on pool
1627 1633 * name and GUID. The pool can also be renamed as part of the import process.
1628 1634 */
1629 1635 int
1630 1636 zpool_do_import(int argc, char **argv)
1631 1637 {
1632 1638 char **searchdirs = NULL;
1633 1639 int nsearch = 0;
1634 1640 int c;
1635 1641 int err = 0;
1636 1642 nvlist_t *pools = NULL;
1637 1643 boolean_t do_all = B_FALSE;
1638 1644 boolean_t do_destroyed = B_FALSE;
1639 1645 char *mntopts = NULL;
1640 1646 nvpair_t *elem;
1641 1647 nvlist_t *config;
1642 1648 uint64_t searchguid = 0;
1643 1649 char *searchname = NULL;
1644 1650 char *propval;
1645 1651 nvlist_t *found_config;
1646 1652 nvlist_t *policy = NULL;
1647 1653 nvlist_t *props = NULL;
1648 1654 boolean_t first;
1649 1655 int flags = ZFS_IMPORT_NORMAL;
1650 1656 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1651 1657 boolean_t dryrun = B_FALSE;
1652 1658 boolean_t do_rewind = B_FALSE;
1653 1659 boolean_t xtreme_rewind = B_FALSE;
1654 1660 uint64_t pool_state, txg = -1ULL;
1655 1661 char *cachefile = NULL;
1656 1662 importargs_t idata = { 0 };
1657 1663 char *endptr;
1658 1664
1659 1665 /* check options */
1660 1666 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1661 1667 switch (c) {
1662 1668 case 'a':
1663 1669 do_all = B_TRUE;
1664 1670 break;
1665 1671 case 'c':
1666 1672 cachefile = optarg;
1667 1673 break;
1668 1674 case 'd':
1669 1675 if (searchdirs == NULL) {
1670 1676 searchdirs = safe_malloc(sizeof (char *));
1671 1677 } else {
1672 1678 char **tmp = safe_malloc((nsearch + 1) *
1673 1679 sizeof (char *));
1674 1680 bcopy(searchdirs, tmp, nsearch *
1675 1681 sizeof (char *));
1676 1682 free(searchdirs);
1677 1683 searchdirs = tmp;
1678 1684 }
1679 1685 searchdirs[nsearch++] = optarg;
1680 1686 break;
1681 1687 case 'D':
1682 1688 do_destroyed = B_TRUE;
1683 1689 break;
1684 1690 case 'f':
1685 1691 flags |= ZFS_IMPORT_ANY_HOST;
1686 1692 break;
1687 1693 case 'F':
1688 1694 do_rewind = B_TRUE;
1689 1695 break;
1690 1696 case 'm':
1691 1697 flags |= ZFS_IMPORT_MISSING_LOG;
1692 1698 break;
1693 1699 case 'n':
1694 1700 dryrun = B_TRUE;
1695 1701 break;
1696 1702 case 'N':
1697 1703 flags |= ZFS_IMPORT_ONLY;
1698 1704 break;
1699 1705 case 'o':
1700 1706 if ((propval = strchr(optarg, '=')) != NULL) {
1701 1707 *propval = '\0';
1702 1708 propval++;
1703 1709 if (add_prop_list(optarg, propval,
1704 1710 &props, B_TRUE))
1705 1711 goto error;
1706 1712 } else {
1707 1713 mntopts = optarg;
1708 1714 }
1709 1715 break;
1710 1716 case 'R':
1711 1717 if (add_prop_list(zpool_prop_to_name(
1712 1718 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1713 1719 goto error;
1714 1720 if (nvlist_lookup_string(props,
1715 1721 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
1716 1722 &propval) == 0)
1717 1723 break;
1718 1724 if (add_prop_list(zpool_prop_to_name(
1719 1725 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
1720 1726 goto error;
1721 1727 break;
1722 1728 case 'T':
1723 1729 errno = 0;
1724 1730 txg = strtoull(optarg, &endptr, 10);
1725 1731 if (errno != 0 || *endptr != '\0') {
1726 1732 (void) fprintf(stderr,
1727 1733 gettext("invalid txg value\n"));
1728 1734 usage(B_FALSE);
1729 1735 }
1730 1736 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
1731 1737 break;
1732 1738 case 'V':
1733 1739 flags |= ZFS_IMPORT_VERBATIM;
1734 1740 break;
1735 1741 case 'X':
1736 1742 xtreme_rewind = B_TRUE;
1737 1743 break;
1738 1744 case ':':
1739 1745 (void) fprintf(stderr, gettext("missing argument for "
1740 1746 "'%c' option\n"), optopt);
1741 1747 usage(B_FALSE);
1742 1748 break;
1743 1749 case '?':
1744 1750 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1745 1751 optopt);
1746 1752 usage(B_FALSE);
1747 1753 }
1748 1754 }
1749 1755
1750 1756 argc -= optind;
1751 1757 argv += optind;
1752 1758
1753 1759 if (cachefile && nsearch != 0) {
1754 1760 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
1755 1761 usage(B_FALSE);
1756 1762 }
1757 1763
1758 1764 if ((dryrun || xtreme_rewind) && !do_rewind) {
1759 1765 (void) fprintf(stderr,
1760 1766 gettext("-n or -X only meaningful with -F\n"));
1761 1767 usage(B_FALSE);
1762 1768 }
1763 1769 if (dryrun)
1764 1770 rewind_policy = ZPOOL_TRY_REWIND;
1765 1771 else if (do_rewind)
1766 1772 rewind_policy = ZPOOL_DO_REWIND;
1767 1773 if (xtreme_rewind)
1768 1774 rewind_policy |= ZPOOL_EXTREME_REWIND;
1769 1775
1770 1776 /* In the future, we can capture further policy and include it here */
1771 1777 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
1772 1778 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
1773 1779 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
1774 1780 goto error;
1775 1781
1776 1782 if (searchdirs == NULL) {
1777 1783 searchdirs = safe_malloc(sizeof (char *));
1778 1784 searchdirs[0] = "/dev/dsk";
1779 1785 nsearch = 1;
1780 1786 }
1781 1787
1782 1788 /* check argument count */
1783 1789 if (do_all) {
1784 1790 if (argc != 0) {
1785 1791 (void) fprintf(stderr, gettext("too many arguments\n"));
1786 1792 usage(B_FALSE);
1787 1793 }
1788 1794 } else {
1789 1795 if (argc > 2) {
1790 1796 (void) fprintf(stderr, gettext("too many arguments\n"));
1791 1797 usage(B_FALSE);
1792 1798 }
1793 1799
1794 1800 /*
1795 1801 * Check for the SYS_CONFIG privilege. We do this explicitly
1796 1802 * here because otherwise any attempt to discover pools will
1797 1803 * silently fail.
1798 1804 */
1799 1805 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
1800 1806 (void) fprintf(stderr, gettext("cannot "
1801 1807 "discover pools: permission denied\n"));
1802 1808 free(searchdirs);
1803 1809 nvlist_free(policy);
1804 1810 return (1);
1805 1811 }
1806 1812 }
1807 1813
1808 1814 /*
1809 1815 * Depending on the arguments given, we do one of the following:
1810 1816 *
1811 1817 * <none> Iterate through all pools and display information about
1812 1818 * each one.
1813 1819 *
1814 1820 * -a Iterate through all pools and try to import each one.
1815 1821 *
1816 1822 * <id> Find the pool that corresponds to the given GUID/pool
1817 1823 * name and import that one.
1818 1824 *
1819 1825 * -D Above options applies only to destroyed pools.
1820 1826 */
1821 1827 if (argc != 0) {
1822 1828 char *endptr;
1823 1829
1824 1830 errno = 0;
1825 1831 searchguid = strtoull(argv[0], &endptr, 10);
1826 1832 if (errno != 0 || *endptr != '\0')
1827 1833 searchname = argv[0];
1828 1834 found_config = NULL;
1829 1835
1830 1836 /*
1831 1837 * User specified a name or guid. Ensure it's unique.
1832 1838 */
1833 1839 idata.unique = B_TRUE;
1834 1840 }
1835 1841
1836 1842
1837 1843 idata.path = searchdirs;
1838 1844 idata.paths = nsearch;
1839 1845 idata.poolname = searchname;
1840 1846 idata.guid = searchguid;
1841 1847 idata.cachefile = cachefile;
1842 1848
1843 1849 pools = zpool_search_import(g_zfs, &idata);
1844 1850
1845 1851 if (pools != NULL && idata.exists &&
1846 1852 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
1847 1853 (void) fprintf(stderr, gettext("cannot import '%s': "
1848 1854 "a pool with that name already exists\n"),
1849 1855 argv[0]);
1850 1856 (void) fprintf(stderr, gettext("use the form '%s "
1851 1857 "<pool | id> <newpool>' to give it a new name\n"),
1852 1858 "zpool import");
1853 1859 err = 1;
1854 1860 } else if (pools == NULL && idata.exists) {
1855 1861 (void) fprintf(stderr, gettext("cannot import '%s': "
1856 1862 "a pool with that name is already created/imported,\n"),
1857 1863 argv[0]);
1858 1864 (void) fprintf(stderr, gettext("and no additional pools "
1859 1865 "with that name were found\n"));
1860 1866 err = 1;
1861 1867 } else if (pools == NULL) {
1862 1868 if (argc != 0) {
1863 1869 (void) fprintf(stderr, gettext("cannot import '%s': "
1864 1870 "no such pool available\n"), argv[0]);
1865 1871 }
1866 1872 err = 1;
1867 1873 }
1868 1874
1869 1875 if (err == 1) {
1870 1876 free(searchdirs);
1871 1877 nvlist_free(policy);
1872 1878 return (1);
1873 1879 }
1874 1880
1875 1881 /*
1876 1882 * At this point we have a list of import candidate configs. Even if
1877 1883 * we were searching by pool name or guid, we still need to
1878 1884 * post-process the list to deal with pool state and possible
1879 1885 * duplicate names.
1880 1886 */
1881 1887 err = 0;
1882 1888 elem = NULL;
1883 1889 first = B_TRUE;
1884 1890 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1885 1891
1886 1892 verify(nvpair_value_nvlist(elem, &config) == 0);
1887 1893
1888 1894 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1889 1895 &pool_state) == 0);
1890 1896 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
1891 1897 continue;
1892 1898 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
1893 1899 continue;
1894 1900
1895 1901 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
1896 1902 policy) == 0);
1897 1903
1898 1904 if (argc == 0) {
1899 1905 if (first)
1900 1906 first = B_FALSE;
1901 1907 else if (!do_all)
1902 1908 (void) printf("\n");
1903 1909
1904 1910 if (do_all) {
1905 1911 err |= do_import(config, NULL, mntopts,
1906 1912 props, flags);
1907 1913 } else {
1908 1914 show_import(config);
1909 1915 }
1910 1916 } else if (searchname != NULL) {
1911 1917 char *name;
1912 1918
1913 1919 /*
1914 1920 * We are searching for a pool based on name.
1915 1921 */
1916 1922 verify(nvlist_lookup_string(config,
1917 1923 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
1918 1924
1919 1925 if (strcmp(name, searchname) == 0) {
1920 1926 if (found_config != NULL) {
1921 1927 (void) fprintf(stderr, gettext(
1922 1928 "cannot import '%s': more than "
1923 1929 "one matching pool\n"), searchname);
1924 1930 (void) fprintf(stderr, gettext(
1925 1931 "import by numeric ID instead\n"));
1926 1932 err = B_TRUE;
1927 1933 }
1928 1934 found_config = config;
1929 1935 }
1930 1936 } else {
1931 1937 uint64_t guid;
1932 1938
1933 1939 /*
1934 1940 * Search for a pool by guid.
1935 1941 */
1936 1942 verify(nvlist_lookup_uint64(config,
1937 1943 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
1938 1944
1939 1945 if (guid == searchguid)
1940 1946 found_config = config;
1941 1947 }
1942 1948 }
1943 1949
1944 1950 /*
1945 1951 * If we were searching for a specific pool, verify that we found a
1946 1952 * pool, and then do the import.
1947 1953 */
1948 1954 if (argc != 0 && err == 0) {
1949 1955 if (found_config == NULL) {
1950 1956 (void) fprintf(stderr, gettext("cannot import '%s': "
1951 1957 "no such pool available\n"), argv[0]);
1952 1958 err = B_TRUE;
1953 1959 } else {
1954 1960 err |= do_import(found_config, argc == 1 ? NULL :
1955 1961 argv[1], mntopts, props, flags);
1956 1962 }
1957 1963 }
1958 1964
1959 1965 /*
1960 1966 * If we were just looking for pools, report an error if none were
1961 1967 * found.
1962 1968 */
1963 1969 if (argc == 0 && first)
1964 1970 (void) fprintf(stderr,
1965 1971 gettext("no pools available to import\n"));
1966 1972
1967 1973 error:
1968 1974 nvlist_free(props);
1969 1975 nvlist_free(pools);
1970 1976 nvlist_free(policy);
1971 1977 free(searchdirs);
1972 1978
1973 1979 return (err ? 1 : 0);
1974 1980 }
1975 1981
1976 1982 typedef struct iostat_cbdata {
1977 1983 zpool_list_t *cb_list;
1978 1984 int cb_verbose;
1979 1985 int cb_iteration;
1980 1986 int cb_namewidth;
1981 1987 } iostat_cbdata_t;
1982 1988
1983 1989 static void
1984 1990 print_iostat_separator(iostat_cbdata_t *cb)
1985 1991 {
1986 1992 int i = 0;
1987 1993
1988 1994 for (i = 0; i < cb->cb_namewidth; i++)
1989 1995 (void) printf("-");
1990 1996 (void) printf(" ----- ----- ----- ----- ----- -----\n");
1991 1997 }
1992 1998
1993 1999 static void
1994 2000 print_iostat_header(iostat_cbdata_t *cb)
1995 2001 {
1996 2002 (void) printf("%*s capacity operations bandwidth\n",
1997 2003 cb->cb_namewidth, "");
1998 2004 (void) printf("%-*s alloc free read write read write\n",
1999 2005 cb->cb_namewidth, "pool");
2000 2006 print_iostat_separator(cb);
2001 2007 }
2002 2008
2003 2009 /*
2004 2010 * Display a single statistic.
2005 2011 */
2006 2012 static void
2007 2013 print_one_stat(uint64_t value)
2008 2014 {
2009 2015 char buf[64];
2010 2016
2011 2017 zfs_nicenum(value, buf, sizeof (buf));
2012 2018 (void) printf(" %5s", buf);
2013 2019 }
2014 2020
2015 2021 /*
2016 2022 * Print out all the statistics for the given vdev. This can either be the
2017 2023 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2018 2024 * is a verbose output, and we don't want to display the toplevel pool stats.
2019 2025 */
2020 2026 void
2021 2027 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2022 2028 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2023 2029 {
2024 2030 nvlist_t **oldchild, **newchild;
2025 2031 uint_t c, children;
2026 2032 vdev_stat_t *oldvs, *newvs;
2027 2033 vdev_stat_t zerovs = { 0 };
2028 2034 uint64_t tdelta;
2029 2035 double scale;
2030 2036 char *vname;
2031 2037
2032 2038 if (oldnv != NULL) {
2033 2039 verify(nvlist_lookup_uint64_array(oldnv,
2034 2040 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2035 2041 } else {
2036 2042 oldvs = &zerovs;
2037 2043 }
2038 2044
2039 2045 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2040 2046 (uint64_t **)&newvs, &c) == 0);
2041 2047
2042 2048 if (strlen(name) + depth > cb->cb_namewidth)
2043 2049 (void) printf("%*s%s", depth, "", name);
2044 2050 else
2045 2051 (void) printf("%*s%s%*s", depth, "", name,
2046 2052 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2047 2053
2048 2054 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2049 2055
2050 2056 if (tdelta == 0)
2051 2057 scale = 1.0;
2052 2058 else
2053 2059 scale = (double)NANOSEC / tdelta;
2054 2060
2055 2061 /* only toplevel vdevs have capacity stats */
2056 2062 if (newvs->vs_space == 0) {
2057 2063 (void) printf(" - -");
2058 2064 } else {
2059 2065 print_one_stat(newvs->vs_alloc);
2060 2066 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2061 2067 }
2062 2068
2063 2069 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2064 2070 oldvs->vs_ops[ZIO_TYPE_READ])));
2065 2071
2066 2072 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2067 2073 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2068 2074
2069 2075 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2070 2076 oldvs->vs_bytes[ZIO_TYPE_READ])));
2071 2077
2072 2078 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2073 2079 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2074 2080
2075 2081 (void) printf("\n");
2076 2082
2077 2083 if (!cb->cb_verbose)
2078 2084 return;
2079 2085
2080 2086 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2081 2087 &newchild, &children) != 0)
2082 2088 return;
2083 2089
2084 2090 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2085 2091 &oldchild, &c) != 0)
2086 2092 return;
2087 2093
2088 2094 for (c = 0; c < children; c++) {
2089 2095 uint64_t ishole = B_FALSE, islog = B_FALSE;
2090 2096
2091 2097 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2092 2098 &ishole);
2093 2099
2094 2100 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2095 2101 &islog);
2096 2102
2097 2103 if (ishole || islog)
2098 2104 continue;
2099 2105
2100 2106 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2101 2107 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2102 2108 newchild[c], cb, depth + 2);
2103 2109 free(vname);
2104 2110 }
2105 2111
2106 2112 /*
2107 2113 * Log device section
2108 2114 */
2109 2115
2110 2116 if (num_logs(newnv) > 0) {
2111 2117 (void) printf("%-*s - - - - - "
2112 2118 "-\n", cb->cb_namewidth, "logs");
2113 2119
2114 2120 for (c = 0; c < children; c++) {
2115 2121 uint64_t islog = B_FALSE;
2116 2122 (void) nvlist_lookup_uint64(newchild[c],
2117 2123 ZPOOL_CONFIG_IS_LOG, &islog);
2118 2124
2119 2125 if (islog) {
2120 2126 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2121 2127 B_FALSE);
2122 2128 print_vdev_stats(zhp, vname, oldnv ?
2123 2129 oldchild[c] : NULL, newchild[c],
2124 2130 cb, depth + 2);
2125 2131 free(vname);
2126 2132 }
2127 2133 }
2128 2134
2129 2135 }
2130 2136
2131 2137 /*
2132 2138 * Include level 2 ARC devices in iostat output
2133 2139 */
2134 2140 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2135 2141 &newchild, &children) != 0)
2136 2142 return;
2137 2143
2138 2144 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2139 2145 &oldchild, &c) != 0)
2140 2146 return;
2141 2147
2142 2148 if (children > 0) {
2143 2149 (void) printf("%-*s - - - - - "
2144 2150 "-\n", cb->cb_namewidth, "cache");
2145 2151 for (c = 0; c < children; c++) {
2146 2152 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2147 2153 B_FALSE);
2148 2154 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2149 2155 newchild[c], cb, depth + 2);
2150 2156 free(vname);
2151 2157 }
2152 2158 }
2153 2159 }
2154 2160
2155 2161 static int
2156 2162 refresh_iostat(zpool_handle_t *zhp, void *data)
2157 2163 {
2158 2164 iostat_cbdata_t *cb = data;
2159 2165 boolean_t missing;
2160 2166
2161 2167 /*
2162 2168 * If the pool has disappeared, remove it from the list and continue.
2163 2169 */
2164 2170 if (zpool_refresh_stats(zhp, &missing) != 0)
2165 2171 return (-1);
2166 2172
2167 2173 if (missing)
2168 2174 pool_list_remove(cb->cb_list, zhp);
2169 2175
2170 2176 return (0);
2171 2177 }
2172 2178
2173 2179 /*
2174 2180 * Callback to print out the iostats for the given pool.
2175 2181 */
2176 2182 int
2177 2183 print_iostat(zpool_handle_t *zhp, void *data)
2178 2184 {
2179 2185 iostat_cbdata_t *cb = data;
2180 2186 nvlist_t *oldconfig, *newconfig;
2181 2187 nvlist_t *oldnvroot, *newnvroot;
2182 2188
2183 2189 newconfig = zpool_get_config(zhp, &oldconfig);
2184 2190
2185 2191 if (cb->cb_iteration == 1)
2186 2192 oldconfig = NULL;
2187 2193
2188 2194 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2189 2195 &newnvroot) == 0);
2190 2196
2191 2197 if (oldconfig == NULL)
2192 2198 oldnvroot = NULL;
2193 2199 else
2194 2200 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2195 2201 &oldnvroot) == 0);
2196 2202
2197 2203 /*
2198 2204 * Print out the statistics for the pool.
2199 2205 */
2200 2206 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2201 2207
2202 2208 if (cb->cb_verbose)
2203 2209 print_iostat_separator(cb);
2204 2210
2205 2211 return (0);
2206 2212 }
2207 2213
2208 2214 int
2209 2215 get_namewidth(zpool_handle_t *zhp, void *data)
2210 2216 {
2211 2217 iostat_cbdata_t *cb = data;
2212 2218 nvlist_t *config, *nvroot;
2213 2219
2214 2220 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2215 2221 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2216 2222 &nvroot) == 0);
2217 2223 if (!cb->cb_verbose)
2218 2224 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2219 2225 else
2220 2226 cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
2221 2227 }
2222 2228
2223 2229 /*
2224 2230 * The width must fall into the range [10,38]. The upper limit is the
2225 2231 * maximum we can have and still fit in 80 columns.
2226 2232 */
2227 2233 if (cb->cb_namewidth < 10)
2228 2234 cb->cb_namewidth = 10;
2229 2235 if (cb->cb_namewidth > 38)
2230 2236 cb->cb_namewidth = 38;
2231 2237
2232 2238 return (0);
2233 2239 }
2234 2240
2235 2241 /*
2236 2242 * Parse the input string, get the 'interval' and 'count' value if there is one.
2237 2243 */
2238 2244 static void
2239 2245 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2240 2246 unsigned long *cnt)
2241 2247 {
2242 2248 unsigned long interval = 0, count = 0;
2243 2249 int argc = *argcp, errno;
2244 2250
2245 2251 /*
2246 2252 * Determine if the last argument is an integer or a pool name
2247 2253 */
2248 2254 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2249 2255 char *end;
2250 2256
2251 2257 errno = 0;
2252 2258 interval = strtoul(argv[argc - 1], &end, 10);
2253 2259
2254 2260 if (*end == '\0' && errno == 0) {
2255 2261 if (interval == 0) {
2256 2262 (void) fprintf(stderr, gettext("interval "
2257 2263 "cannot be zero\n"));
2258 2264 usage(B_FALSE);
2259 2265 }
2260 2266 /*
2261 2267 * Ignore the last parameter
2262 2268 */
2263 2269 argc--;
2264 2270 } else {
2265 2271 /*
2266 2272 * If this is not a valid number, just plow on. The
2267 2273 * user will get a more informative error message later
2268 2274 * on.
2269 2275 */
2270 2276 interval = 0;
2271 2277 }
2272 2278 }
2273 2279
2274 2280 /*
2275 2281 * If the last argument is also an integer, then we have both a count
2276 2282 * and an interval.
2277 2283 */
2278 2284 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2279 2285 char *end;
2280 2286
2281 2287 errno = 0;
2282 2288 count = interval;
2283 2289 interval = strtoul(argv[argc - 1], &end, 10);
2284 2290
2285 2291 if (*end == '\0' && errno == 0) {
2286 2292 if (interval == 0) {
2287 2293 (void) fprintf(stderr, gettext("interval "
2288 2294 "cannot be zero\n"));
2289 2295 usage(B_FALSE);
2290 2296 }
2291 2297
2292 2298 /*
2293 2299 * Ignore the last parameter
2294 2300 */
2295 2301 argc--;
2296 2302 } else {
2297 2303 interval = 0;
2298 2304 }
2299 2305 }
2300 2306
2301 2307 *iv = interval;
2302 2308 *cnt = count;
2303 2309 *argcp = argc;
2304 2310 }
2305 2311
2306 2312 static void
2307 2313 get_timestamp_arg(char c)
2308 2314 {
2309 2315 if (c == 'u')
2310 2316 timestamp_fmt = UDATE;
2311 2317 else if (c == 'd')
2312 2318 timestamp_fmt = DDATE;
2313 2319 else
2314 2320 usage(B_FALSE);
2315 2321 }
2316 2322
2317 2323 /*
2318 2324 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2319 2325 *
2320 2326 * -v Display statistics for individual vdevs
2321 2327 * -T Display a timestamp in date(1) or Unix format
2322 2328 *
2323 2329 * This command can be tricky because we want to be able to deal with pool
2324 2330 * creation/destruction as well as vdev configuration changes. The bulk of this
2325 2331 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2326 2332 * on pool_list_update() to detect the addition of new pools. Configuration
2327 2333 * changes are all handled within libzfs.
2328 2334 */
2329 2335 int
2330 2336 zpool_do_iostat(int argc, char **argv)
2331 2337 {
2332 2338 int c;
2333 2339 int ret;
2334 2340 int npools;
2335 2341 unsigned long interval = 0, count = 0;
2336 2342 zpool_list_t *list;
2337 2343 boolean_t verbose = B_FALSE;
2338 2344 iostat_cbdata_t cb;
2339 2345
2340 2346 /* check options */
2341 2347 while ((c = getopt(argc, argv, "T:v")) != -1) {
2342 2348 switch (c) {
2343 2349 case 'T':
2344 2350 get_timestamp_arg(*optarg);
2345 2351 break;
2346 2352 case 'v':
2347 2353 verbose = B_TRUE;
2348 2354 break;
2349 2355 case '?':
2350 2356 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2351 2357 optopt);
2352 2358 usage(B_FALSE);
2353 2359 }
2354 2360 }
2355 2361
2356 2362 argc -= optind;
2357 2363 argv += optind;
2358 2364
2359 2365 get_interval_count(&argc, argv, &interval, &count);
2360 2366
2361 2367 /*
2362 2368 * Construct the list of all interesting pools.
2363 2369 */
2364 2370 ret = 0;
2365 2371 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2366 2372 return (1);
2367 2373
2368 2374 if (pool_list_count(list) == 0 && argc != 0) {
2369 2375 pool_list_free(list);
2370 2376 return (1);
2371 2377 }
2372 2378
2373 2379 if (pool_list_count(list) == 0 && interval == 0) {
2374 2380 pool_list_free(list);
2375 2381 (void) fprintf(stderr, gettext("no pools available\n"));
2376 2382 return (1);
2377 2383 }
2378 2384
2379 2385 /*
2380 2386 * Enter the main iostat loop.
2381 2387 */
2382 2388 cb.cb_list = list;
2383 2389 cb.cb_verbose = verbose;
2384 2390 cb.cb_iteration = 0;
2385 2391 cb.cb_namewidth = 0;
2386 2392
2387 2393 for (;;) {
2388 2394 pool_list_update(list);
2389 2395
2390 2396 if ((npools = pool_list_count(list)) == 0)
2391 2397 break;
2392 2398
2393 2399 /*
2394 2400 * Refresh all statistics. This is done as an explicit step
2395 2401 * before calculating the maximum name width, so that any
2396 2402 * configuration changes are properly accounted for.
2397 2403 */
2398 2404 (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
2399 2405
2400 2406 /*
2401 2407 * Iterate over all pools to determine the maximum width
2402 2408 * for the pool / device name column across all pools.
2403 2409 */
2404 2410 cb.cb_namewidth = 0;
2405 2411 (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
2406 2412
2407 2413 if (timestamp_fmt != NODATE)
2408 2414 print_timestamp(timestamp_fmt);
2409 2415
2410 2416 /*
2411 2417 * If it's the first time, or verbose mode, print the header.
2412 2418 */
2413 2419 if (++cb.cb_iteration == 1 || verbose)
2414 2420 print_iostat_header(&cb);
2415 2421
2416 2422 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2417 2423
2418 2424 /*
2419 2425 * If there's more than one pool, and we're not in verbose mode
2420 2426 * (which prints a separator for us), then print a separator.
2421 2427 */
2422 2428 if (npools > 1 && !verbose)
2423 2429 print_iostat_separator(&cb);
2424 2430
2425 2431 if (verbose)
2426 2432 (void) printf("\n");
2427 2433
2428 2434 /*
2429 2435 * Flush the output so that redirection to a file isn't buffered
2430 2436 * indefinitely.
2431 2437 */
2432 2438 (void) fflush(stdout);
2433 2439
2434 2440 if (interval == 0)
2435 2441 break;
2436 2442
2437 2443 if (count != 0 && --count == 0)
2438 2444 break;
2439 2445
2440 2446 (void) sleep(interval);
2441 2447 }
2442 2448
2443 2449 pool_list_free(list);
2444 2450
2445 2451 return (ret);
2446 2452 }
2447 2453
2448 2454 typedef struct list_cbdata {
2449 2455 boolean_t cb_scripted;
2450 2456 boolean_t cb_first;
2451 2457 zprop_list_t *cb_proplist;
2452 2458 } list_cbdata_t;
2453 2459
2454 2460 /*
2455 2461 * Given a list of columns to display, output appropriate headers for each one.
2456 2462 */
2457 2463 static void
2458 2464 print_header(zprop_list_t *pl)
2459 2465 {
2460 2466 const char *header;
2461 2467 boolean_t first = B_TRUE;
2462 2468 boolean_t right_justify;
2463 2469
2464 2470 for (; pl != NULL; pl = pl->pl_next) {
2465 2471 if (pl->pl_prop == ZPROP_INVAL)
2466 2472 continue;
2467 2473
2468 2474 if (!first)
2469 2475 (void) printf(" ");
2470 2476 else
2471 2477 first = B_FALSE;
2472 2478
2473 2479 header = zpool_prop_column_name(pl->pl_prop);
2474 2480 right_justify = zpool_prop_align_right(pl->pl_prop);
2475 2481
2476 2482 if (pl->pl_next == NULL && !right_justify)
2477 2483 (void) printf("%s", header);
2478 2484 else if (right_justify)
2479 2485 (void) printf("%*s", pl->pl_width, header);
2480 2486 else
2481 2487 (void) printf("%-*s", pl->pl_width, header);
2482 2488 }
2483 2489
2484 2490 (void) printf("\n");
2485 2491 }
2486 2492
2487 2493 /*
2488 2494 * Given a pool and a list of properties, print out all the properties according
2489 2495 * to the described layout.
2490 2496 */
2491 2497 static void
2492 2498 print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
2493 2499 {
2494 2500 boolean_t first = B_TRUE;
2495 2501 char property[ZPOOL_MAXPROPLEN];
2496 2502 char *propstr;
2497 2503 boolean_t right_justify;
2498 2504 int width;
2499 2505
2500 2506 for (; pl != NULL; pl = pl->pl_next) {
2501 2507 if (!first) {
2502 2508 if (scripted)
2503 2509 (void) printf("\t");
2504 2510 else
2505 2511 (void) printf(" ");
2506 2512 } else {
2507 2513 first = B_FALSE;
2508 2514 }
2509 2515
2510 2516 right_justify = B_FALSE;
2511 2517 if (pl->pl_prop != ZPROP_INVAL) {
2512 2518 if (zpool_get_prop(zhp, pl->pl_prop, property,
2513 2519 sizeof (property), NULL) != 0)
2514 2520 propstr = "-";
2515 2521 else
2516 2522 propstr = property;
2517 2523
2518 2524 right_justify = zpool_prop_align_right(pl->pl_prop);
2519 2525 } else {
2520 2526 propstr = "-";
2521 2527 }
2522 2528
2523 2529 width = pl->pl_width;
2524 2530
2525 2531 /*
2526 2532 * If this is being called in scripted mode, or if this is the
2527 2533 * last column and it is left-justified, don't include a width
2528 2534 * format specifier.
2529 2535 */
2530 2536 if (scripted || (pl->pl_next == NULL && !right_justify))
2531 2537 (void) printf("%s", propstr);
2532 2538 else if (right_justify)
2533 2539 (void) printf("%*s", width, propstr);
2534 2540 else
2535 2541 (void) printf("%-*s", width, propstr);
2536 2542 }
2537 2543
2538 2544 (void) printf("\n");
2539 2545 }
2540 2546
2541 2547 /*
2542 2548 * Generic callback function to list a pool.
2543 2549 */
2544 2550 int
2545 2551 list_callback(zpool_handle_t *zhp, void *data)
2546 2552 {
2547 2553 list_cbdata_t *cbp = data;
2548 2554
2549 2555 if (cbp->cb_first) {
2550 2556 if (!cbp->cb_scripted)
2551 2557 print_header(cbp->cb_proplist);
2552 2558 cbp->cb_first = B_FALSE;
2553 2559 }
2554 2560
2555 2561 print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
2556 2562
2557 2563 return (0);
2558 2564 }
2559 2565
2560 2566 /*
2561 2567 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
2562 2568 *
2563 2569 * -H Scripted mode. Don't display headers, and separate properties
2564 2570 * by a single tab.
2565 2571 * -o List of properties to display. Defaults to
2566 2572 * "name,size,allocated,free,capacity,health,altroot"
2567 2573 * -T Display a timestamp in date(1) or Unix format
2568 2574 *
2569 2575 * List all pools in the system, whether or not they're healthy. Output space
2570 2576 * statistics for each one, as well as health status summary.
2571 2577 */
2572 2578 int
2573 2579 zpool_do_list(int argc, char **argv)
2574 2580 {
2575 2581 int c;
2576 2582 int ret;
2577 2583 list_cbdata_t cb = { 0 };
2578 2584 static char default_props[] =
2579 2585 "name,size,allocated,free,capacity,dedupratio,health,altroot";
2580 2586 char *props = default_props;
2581 2587 unsigned long interval = 0, count = 0;
2582 2588
2583 2589 /* check options */
2584 2590 while ((c = getopt(argc, argv, ":Ho:T:")) != -1) {
2585 2591 switch (c) {
2586 2592 case 'H':
2587 2593 cb.cb_scripted = B_TRUE;
2588 2594 break;
2589 2595 case 'o':
2590 2596 props = optarg;
2591 2597 break;
2592 2598 case 'T':
2593 2599 get_timestamp_arg(*optarg);
2594 2600 break;
2595 2601 case ':':
2596 2602 (void) fprintf(stderr, gettext("missing argument for "
2597 2603 "'%c' option\n"), optopt);
2598 2604 usage(B_FALSE);
2599 2605 break;
2600 2606 case '?':
2601 2607 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2602 2608 optopt);
2603 2609 usage(B_FALSE);
2604 2610 }
2605 2611 }
2606 2612
2607 2613 argc -= optind;
2608 2614 argv += optind;
2609 2615
2610 2616 get_interval_count(&argc, argv, &interval, &count);
2611 2617
2612 2618 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
2613 2619 usage(B_FALSE);
2614 2620
2615 2621 cb.cb_first = B_TRUE;
2616 2622
2617 2623 for (;;) {
2618 2624
2619 2625 if (timestamp_fmt != NODATE)
2620 2626 print_timestamp(timestamp_fmt);
2621 2627
2622 2628 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
2623 2629 list_callback, &cb);
2624 2630
2625 2631 if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
2626 2632 (void) printf(gettext("no pools available\n"));
2627 2633 zprop_free_list(cb.cb_proplist);
2628 2634 return (0);
2629 2635 }
2630 2636
2631 2637 if (interval == 0)
2632 2638 break;
2633 2639
2634 2640 if (count != 0 && --count == 0)
2635 2641 break;
2636 2642
2637 2643 (void) sleep(interval);
2638 2644 }
2639 2645
2640 2646 zprop_free_list(cb.cb_proplist);
2641 2647 return (ret);
2642 2648 }
2643 2649
2644 2650 static nvlist_t *
2645 2651 zpool_get_vdev_by_name(nvlist_t *nv, char *name)
2646 2652 {
2647 2653 nvlist_t **child;
2648 2654 uint_t c, children;
2649 2655 nvlist_t *match;
2650 2656 char *path;
2651 2657
2652 2658 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2653 2659 &child, &children) != 0) {
2654 2660 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2655 2661 if (strncmp(name, "/dev/dsk/", 9) == 0)
2656 2662 name += 9;
2657 2663 if (strncmp(path, "/dev/dsk/", 9) == 0)
2658 2664 path += 9;
2659 2665 if (strcmp(name, path) == 0)
2660 2666 return (nv);
2661 2667 return (NULL);
2662 2668 }
2663 2669
2664 2670 for (c = 0; c < children; c++)
2665 2671 if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
2666 2672 return (match);
2667 2673
2668 2674 return (NULL);
2669 2675 }
2670 2676
2671 2677 static int
2672 2678 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
2673 2679 {
2674 2680 boolean_t force = B_FALSE;
2675 2681 int c;
2676 2682 nvlist_t *nvroot;
2677 2683 char *poolname, *old_disk, *new_disk;
2678 2684 zpool_handle_t *zhp;
2679 2685 int ret;
2680 2686
2681 2687 /* check options */
2682 2688 while ((c = getopt(argc, argv, "f")) != -1) {
2683 2689 switch (c) {
2684 2690 case 'f':
2685 2691 force = B_TRUE;
2686 2692 break;
2687 2693 case '?':
2688 2694 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2689 2695 optopt);
2690 2696 usage(B_FALSE);
2691 2697 }
2692 2698 }
2693 2699
2694 2700 argc -= optind;
2695 2701 argv += optind;
2696 2702
2697 2703 /* get pool name and check number of arguments */
2698 2704 if (argc < 1) {
2699 2705 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2700 2706 usage(B_FALSE);
2701 2707 }
2702 2708
2703 2709 poolname = argv[0];
2704 2710
2705 2711 if (argc < 2) {
2706 2712 (void) fprintf(stderr,
2707 2713 gettext("missing <device> specification\n"));
2708 2714 usage(B_FALSE);
2709 2715 }
2710 2716
2711 2717 old_disk = argv[1];
2712 2718
2713 2719 if (argc < 3) {
2714 2720 if (!replacing) {
2715 2721 (void) fprintf(stderr,
2716 2722 gettext("missing <new_device> specification\n"));
2717 2723 usage(B_FALSE);
2718 2724 }
2719 2725 new_disk = old_disk;
2720 2726 argc -= 1;
2721 2727 argv += 1;
2722 2728 } else {
2723 2729 new_disk = argv[2];
2724 2730 argc -= 2;
2725 2731 argv += 2;
2726 2732 }
2727 2733
2728 2734 if (argc > 1) {
2729 2735 (void) fprintf(stderr, gettext("too many arguments\n"));
2730 2736 usage(B_FALSE);
2731 2737 }
2732 2738
2733 2739 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2734 2740 return (1);
2735 2741
2736 2742 if (zpool_get_config(zhp, NULL) == NULL) {
2737 2743 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
2738 2744 poolname);
2739 2745 zpool_close(zhp);
2740 2746 return (1);
2741 2747 }
2742 2748
2743 2749 nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE,
2744 2750 argc, argv);
2745 2751 if (nvroot == NULL) {
2746 2752 zpool_close(zhp);
2747 2753 return (1);
2748 2754 }
2749 2755
2750 2756 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
2751 2757
2752 2758 nvlist_free(nvroot);
2753 2759 zpool_close(zhp);
2754 2760
2755 2761 return (ret);
2756 2762 }
2757 2763
2758 2764 /*
2759 2765 * zpool replace [-f] <pool> <device> <new_device>
2760 2766 *
2761 2767 * -f Force attach, even if <new_device> appears to be in use.
2762 2768 *
2763 2769 * Replace <device> with <new_device>.
2764 2770 */
2765 2771 /* ARGSUSED */
2766 2772 int
2767 2773 zpool_do_replace(int argc, char **argv)
2768 2774 {
2769 2775 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
2770 2776 }
2771 2777
2772 2778 /*
2773 2779 * zpool attach [-f] <pool> <device> <new_device>
2774 2780 *
2775 2781 * -f Force attach, even if <new_device> appears to be in use.
2776 2782 *
2777 2783 * Attach <new_device> to the mirror containing <device>. If <device> is not
2778 2784 * part of a mirror, then <device> will be transformed into a mirror of
2779 2785 * <device> and <new_device>. In either case, <new_device> will begin life
2780 2786 * with a DTL of [0, now], and will immediately begin to resilver itself.
2781 2787 */
2782 2788 int
2783 2789 zpool_do_attach(int argc, char **argv)
2784 2790 {
2785 2791 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
2786 2792 }
2787 2793
2788 2794 /*
2789 2795 * zpool detach [-f] <pool> <device>
2790 2796 *
2791 2797 * -f Force detach of <device>, even if DTLs argue against it
2792 2798 * (not supported yet)
2793 2799 *
2794 2800 * Detach a device from a mirror. The operation will be refused if <device>
2795 2801 * is the last device in the mirror, or if the DTLs indicate that this device
2796 2802 * has the only valid copy of some data.
2797 2803 */
2798 2804 /* ARGSUSED */
2799 2805 int
2800 2806 zpool_do_detach(int argc, char **argv)
2801 2807 {
2802 2808 int c;
2803 2809 char *poolname, *path;
2804 2810 zpool_handle_t *zhp;
2805 2811 int ret;
2806 2812
2807 2813 /* check options */
2808 2814 while ((c = getopt(argc, argv, "f")) != -1) {
2809 2815 switch (c) {
2810 2816 case 'f':
2811 2817 case '?':
2812 2818 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2813 2819 optopt);
2814 2820 usage(B_FALSE);
2815 2821 }
2816 2822 }
2817 2823
2818 2824 argc -= optind;
2819 2825 argv += optind;
2820 2826
2821 2827 /* get pool name and check number of arguments */
2822 2828 if (argc < 1) {
2823 2829 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2824 2830 usage(B_FALSE);
2825 2831 }
2826 2832
2827 2833 if (argc < 2) {
2828 2834 (void) fprintf(stderr,
2829 2835 gettext("missing <device> specification\n"));
2830 2836 usage(B_FALSE);
2831 2837 }
2832 2838
2833 2839 poolname = argv[0];
2834 2840 path = argv[1];
2835 2841
2836 2842 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
2837 2843 return (1);
2838 2844
2839 2845 ret = zpool_vdev_detach(zhp, path);
2840 2846
2841 2847 zpool_close(zhp);
2842 2848
2843 2849 return (ret);
2844 2850 }
2845 2851
2846 2852 /*
2847 2853 * zpool split [-n] [-o prop=val] ...
2848 2854 * [-o mntopt] ...
2849 2855 * [-R altroot] <pool> <newpool> [<device> ...]
2850 2856 *
2851 2857 * -n Do not split the pool, but display the resulting layout if
2852 2858 * it were to be split.
2853 2859 * -o Set property=value, or set mount options.
2854 2860 * -R Mount the split-off pool under an alternate root.
2855 2861 *
2856 2862 * Splits the named pool and gives it the new pool name. Devices to be split
2857 2863 * off may be listed, provided that no more than one device is specified
2858 2864 * per top-level vdev mirror. The newly split pool is left in an exported
2859 2865 * state unless -R is specified.
2860 2866 *
2861 2867 * Restrictions: the top-level of the pool pool must only be made up of
2862 2868 * mirrors; all devices in the pool must be healthy; no device may be
2863 2869 * undergoing a resilvering operation.
2864 2870 */
2865 2871 int
2866 2872 zpool_do_split(int argc, char **argv)
2867 2873 {
2868 2874 char *srcpool, *newpool, *propval;
2869 2875 char *mntopts = NULL;
2870 2876 splitflags_t flags;
2871 2877 int c, ret = 0;
2872 2878 zpool_handle_t *zhp;
2873 2879 nvlist_t *config, *props = NULL;
2874 2880
2875 2881 flags.dryrun = B_FALSE;
2876 2882 flags.import = B_FALSE;
2877 2883
2878 2884 /* check options */
2879 2885 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
2880 2886 switch (c) {
2881 2887 case 'R':
2882 2888 flags.import = B_TRUE;
2883 2889 if (add_prop_list(
2884 2890 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
2885 2891 &props, B_TRUE) != 0) {
2886 2892 if (props)
2887 2893 nvlist_free(props);
2888 2894 usage(B_FALSE);
2889 2895 }
2890 2896 break;
2891 2897 case 'n':
2892 2898 flags.dryrun = B_TRUE;
2893 2899 break;
2894 2900 case 'o':
2895 2901 if ((propval = strchr(optarg, '=')) != NULL) {
2896 2902 *propval = '\0';
2897 2903 propval++;
2898 2904 if (add_prop_list(optarg, propval,
2899 2905 &props, B_TRUE) != 0) {
2900 2906 if (props)
2901 2907 nvlist_free(props);
2902 2908 usage(B_FALSE);
2903 2909 }
2904 2910 } else {
2905 2911 mntopts = optarg;
2906 2912 }
2907 2913 break;
2908 2914 case ':':
2909 2915 (void) fprintf(stderr, gettext("missing argument for "
2910 2916 "'%c' option\n"), optopt);
2911 2917 usage(B_FALSE);
2912 2918 break;
2913 2919 case '?':
2914 2920 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2915 2921 optopt);
2916 2922 usage(B_FALSE);
2917 2923 break;
2918 2924 }
2919 2925 }
2920 2926
2921 2927 if (!flags.import && mntopts != NULL) {
2922 2928 (void) fprintf(stderr, gettext("setting mntopts is only "
2923 2929 "valid when importing the pool\n"));
2924 2930 usage(B_FALSE);
2925 2931 }
2926 2932
2927 2933 argc -= optind;
2928 2934 argv += optind;
2929 2935
2930 2936 if (argc < 1) {
2931 2937 (void) fprintf(stderr, gettext("Missing pool name\n"));
2932 2938 usage(B_FALSE);
2933 2939 }
2934 2940 if (argc < 2) {
2935 2941 (void) fprintf(stderr, gettext("Missing new pool name\n"));
2936 2942 usage(B_FALSE);
2937 2943 }
2938 2944
2939 2945 srcpool = argv[0];
2940 2946 newpool = argv[1];
2941 2947
2942 2948 argc -= 2;
2943 2949 argv += 2;
2944 2950
2945 2951 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
2946 2952 return (1);
2947 2953
2948 2954 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
2949 2955 if (config == NULL) {
2950 2956 ret = 1;
2951 2957 } else {
2952 2958 if (flags.dryrun) {
2953 2959 (void) printf(gettext("would create '%s' with the "
2954 2960 "following layout:\n\n"), newpool);
2955 2961 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
2956 2962 }
2957 2963 nvlist_free(config);
2958 2964 }
2959 2965
2960 2966 zpool_close(zhp);
2961 2967
2962 2968 if (ret != 0 || flags.dryrun || !flags.import)
2963 2969 return (ret);
2964 2970
2965 2971 /*
2966 2972 * The split was successful. Now we need to open the new
2967 2973 * pool and import it.
2968 2974 */
2969 2975 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
2970 2976 return (1);
2971 2977 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2972 2978 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2973 2979 ret = 1;
2974 2980 (void) fprintf(stderr, gettext("Split was succssful, but "
2975 2981 "the datasets could not all be mounted\n"));
2976 2982 (void) fprintf(stderr, gettext("Try doing '%s' with a "
2977 2983 "different altroot\n"), "zpool import");
2978 2984 }
2979 2985 zpool_close(zhp);
2980 2986
2981 2987 return (ret);
2982 2988 }
2983 2989
2984 2990
2985 2991
2986 2992 /*
2987 2993 * zpool online <pool> <device> ...
2988 2994 */
2989 2995 int
2990 2996 zpool_do_online(int argc, char **argv)
2991 2997 {
2992 2998 int c, i;
2993 2999 char *poolname;
2994 3000 zpool_handle_t *zhp;
2995 3001 int ret = 0;
2996 3002 vdev_state_t newstate;
2997 3003 int flags = 0;
2998 3004
2999 3005 /* check options */
3000 3006 while ((c = getopt(argc, argv, "et")) != -1) {
3001 3007 switch (c) {
3002 3008 case 'e':
3003 3009 flags |= ZFS_ONLINE_EXPAND;
3004 3010 break;
3005 3011 case 't':
3006 3012 case '?':
3007 3013 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3008 3014 optopt);
3009 3015 usage(B_FALSE);
3010 3016 }
3011 3017 }
3012 3018
3013 3019 argc -= optind;
3014 3020 argv += optind;
3015 3021
3016 3022 /* get pool name and check number of arguments */
3017 3023 if (argc < 1) {
3018 3024 (void) fprintf(stderr, gettext("missing pool name\n"));
3019 3025 usage(B_FALSE);
3020 3026 }
3021 3027 if (argc < 2) {
3022 3028 (void) fprintf(stderr, gettext("missing device name\n"));
3023 3029 usage(B_FALSE);
3024 3030 }
3025 3031
3026 3032 poolname = argv[0];
3027 3033
3028 3034 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3029 3035 return (1);
3030 3036
3031 3037 for (i = 1; i < argc; i++) {
3032 3038 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3033 3039 if (newstate != VDEV_STATE_HEALTHY) {
3034 3040 (void) printf(gettext("warning: device '%s' "
3035 3041 "onlined, but remains in faulted state\n"),
3036 3042 argv[i]);
3037 3043 if (newstate == VDEV_STATE_FAULTED)
3038 3044 (void) printf(gettext("use 'zpool "
3039 3045 "clear' to restore a faulted "
3040 3046 "device\n"));
3041 3047 else
3042 3048 (void) printf(gettext("use 'zpool "
3043 3049 "replace' to replace devices "
3044 3050 "that are no longer present\n"));
3045 3051 }
3046 3052 } else {
3047 3053 ret = 1;
3048 3054 }
3049 3055 }
3050 3056
3051 3057 zpool_close(zhp);
3052 3058
3053 3059 return (ret);
3054 3060 }
3055 3061
3056 3062 /*
3057 3063 * zpool offline [-ft] <pool> <device> ...
3058 3064 *
3059 3065 * -f Force the device into the offline state, even if doing
3060 3066 * so would appear to compromise pool availability.
3061 3067 * (not supported yet)
3062 3068 *
3063 3069 * -t Only take the device off-line temporarily. The offline
3064 3070 * state will not be persistent across reboots.
3065 3071 */
3066 3072 /* ARGSUSED */
3067 3073 int
3068 3074 zpool_do_offline(int argc, char **argv)
3069 3075 {
3070 3076 int c, i;
3071 3077 char *poolname;
3072 3078 zpool_handle_t *zhp;
3073 3079 int ret = 0;
3074 3080 boolean_t istmp = B_FALSE;
3075 3081
3076 3082 /* check options */
3077 3083 while ((c = getopt(argc, argv, "ft")) != -1) {
3078 3084 switch (c) {
3079 3085 case 't':
3080 3086 istmp = B_TRUE;
3081 3087 break;
3082 3088 case 'f':
3083 3089 case '?':
3084 3090 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3085 3091 optopt);
3086 3092 usage(B_FALSE);
3087 3093 }
3088 3094 }
3089 3095
3090 3096 argc -= optind;
3091 3097 argv += optind;
3092 3098
3093 3099 /* get pool name and check number of arguments */
3094 3100 if (argc < 1) {
3095 3101 (void) fprintf(stderr, gettext("missing pool name\n"));
3096 3102 usage(B_FALSE);
3097 3103 }
3098 3104 if (argc < 2) {
3099 3105 (void) fprintf(stderr, gettext("missing device name\n"));
3100 3106 usage(B_FALSE);
3101 3107 }
3102 3108
3103 3109 poolname = argv[0];
3104 3110
3105 3111 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3106 3112 return (1);
3107 3113
3108 3114 for (i = 1; i < argc; i++) {
3109 3115 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3110 3116 ret = 1;
3111 3117 }
3112 3118
3113 3119 zpool_close(zhp);
3114 3120
3115 3121 return (ret);
3116 3122 }
3117 3123
3118 3124 /*
3119 3125 * zpool clear <pool> [device]
3120 3126 *
3121 3127 * Clear all errors associated with a pool or a particular device.
3122 3128 */
3123 3129 int
3124 3130 zpool_do_clear(int argc, char **argv)
3125 3131 {
3126 3132 int c;
3127 3133 int ret = 0;
3128 3134 boolean_t dryrun = B_FALSE;
3129 3135 boolean_t do_rewind = B_FALSE;
3130 3136 boolean_t xtreme_rewind = B_FALSE;
3131 3137 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3132 3138 nvlist_t *policy = NULL;
3133 3139 zpool_handle_t *zhp;
3134 3140 char *pool, *device;
3135 3141
3136 3142 /* check options */
3137 3143 while ((c = getopt(argc, argv, "FnX")) != -1) {
3138 3144 switch (c) {
3139 3145 case 'F':
3140 3146 do_rewind = B_TRUE;
3141 3147 break;
3142 3148 case 'n':
3143 3149 dryrun = B_TRUE;
3144 3150 break;
3145 3151 case 'X':
3146 3152 xtreme_rewind = B_TRUE;
3147 3153 break;
3148 3154 case '?':
3149 3155 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3150 3156 optopt);
3151 3157 usage(B_FALSE);
3152 3158 }
3153 3159 }
3154 3160
3155 3161 argc -= optind;
3156 3162 argv += optind;
3157 3163
3158 3164 if (argc < 1) {
3159 3165 (void) fprintf(stderr, gettext("missing pool name\n"));
3160 3166 usage(B_FALSE);
3161 3167 }
3162 3168
3163 3169 if (argc > 2) {
3164 3170 (void) fprintf(stderr, gettext("too many arguments\n"));
3165 3171 usage(B_FALSE);
3166 3172 }
3167 3173
3168 3174 if ((dryrun || xtreme_rewind) && !do_rewind) {
3169 3175 (void) fprintf(stderr,
3170 3176 gettext("-n or -X only meaningful with -F\n"));
3171 3177 usage(B_FALSE);
3172 3178 }
3173 3179 if (dryrun)
3174 3180 rewind_policy = ZPOOL_TRY_REWIND;
3175 3181 else if (do_rewind)
3176 3182 rewind_policy = ZPOOL_DO_REWIND;
3177 3183 if (xtreme_rewind)
3178 3184 rewind_policy |= ZPOOL_EXTREME_REWIND;
3179 3185
3180 3186 /* In future, further rewind policy choices can be passed along here */
3181 3187 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3182 3188 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3183 3189 return (1);
3184 3190
3185 3191 pool = argv[0];
3186 3192 device = argc == 2 ? argv[1] : NULL;
3187 3193
3188 3194 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3189 3195 nvlist_free(policy);
3190 3196 return (1);
3191 3197 }
3192 3198
3193 3199 if (zpool_clear(zhp, device, policy) != 0)
3194 3200 ret = 1;
3195 3201
3196 3202 zpool_close(zhp);
3197 3203
3198 3204 nvlist_free(policy);
3199 3205
3200 3206 return (ret);
3201 3207 }
3202 3208
3203 3209 /*
3204 3210 * zpool reguid <pool>
3205 3211 */
3206 3212 int
3207 3213 zpool_do_reguid(int argc, char **argv)
3208 3214 {
3209 3215 int c;
3210 3216 char *poolname;
3211 3217 zpool_handle_t *zhp;
3212 3218 int ret = 0;
3213 3219
3214 3220 /* check options */
3215 3221 while ((c = getopt(argc, argv, "")) != -1) {
3216 3222 switch (c) {
3217 3223 case '?':
3218 3224 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3219 3225 optopt);
3220 3226 usage(B_FALSE);
3221 3227 }
3222 3228 }
3223 3229
3224 3230 argc -= optind;
3225 3231 argv += optind;
3226 3232
3227 3233 /* get pool name and check number of arguments */
3228 3234 if (argc < 1) {
3229 3235 (void) fprintf(stderr, gettext("missing pool name\n"));
3230 3236 usage(B_FALSE);
3231 3237 }
3232 3238
3233 3239 if (argc > 1) {
3234 3240 (void) fprintf(stderr, gettext("too many arguments\n"));
3235 3241 usage(B_FALSE);
3236 3242 }
3237 3243
3238 3244 poolname = argv[0];
3239 3245 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3240 3246 return (1);
3241 3247
3242 3248 ret = zpool_reguid(zhp);
3243 3249
3244 3250 zpool_close(zhp);
3245 3251 return (ret);
3246 3252 }
3247 3253
3248 3254
3249 3255 typedef struct scrub_cbdata {
3250 3256 int cb_type;
3251 3257 int cb_argc;
3252 3258 char **cb_argv;
3253 3259 } scrub_cbdata_t;
3254 3260
3255 3261 int
3256 3262 scrub_callback(zpool_handle_t *zhp, void *data)
3257 3263 {
3258 3264 scrub_cbdata_t *cb = data;
3259 3265 int err;
3260 3266
3261 3267 /*
3262 3268 * Ignore faulted pools.
3263 3269 */
3264 3270 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3265 3271 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3266 3272 "currently unavailable\n"), zpool_get_name(zhp));
3267 3273 return (1);
3268 3274 }
3269 3275
3270 3276 err = zpool_scan(zhp, cb->cb_type);
3271 3277
3272 3278 return (err != 0);
3273 3279 }
3274 3280
3275 3281 /*
3276 3282 * zpool scrub [-s] <pool> ...
3277 3283 *
3278 3284 * -s Stop. Stops any in-progress scrub.
3279 3285 */
3280 3286 int
3281 3287 zpool_do_scrub(int argc, char **argv)
3282 3288 {
3283 3289 int c;
3284 3290 scrub_cbdata_t cb;
3285 3291
3286 3292 cb.cb_type = POOL_SCAN_SCRUB;
3287 3293
3288 3294 /* check options */
3289 3295 while ((c = getopt(argc, argv, "s")) != -1) {
3290 3296 switch (c) {
3291 3297 case 's':
3292 3298 cb.cb_type = POOL_SCAN_NONE;
3293 3299 break;
3294 3300 case '?':
3295 3301 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3296 3302 optopt);
3297 3303 usage(B_FALSE);
3298 3304 }
3299 3305 }
3300 3306
3301 3307 cb.cb_argc = argc;
3302 3308 cb.cb_argv = argv;
3303 3309 argc -= optind;
3304 3310 argv += optind;
3305 3311
3306 3312 if (argc < 1) {
3307 3313 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3308 3314 usage(B_FALSE);
3309 3315 }
3310 3316
3311 3317 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3312 3318 }
3313 3319
3314 3320 typedef struct status_cbdata {
3315 3321 int cb_count;
3316 3322 boolean_t cb_allpools;
3317 3323 boolean_t cb_verbose;
3318 3324 boolean_t cb_explain;
3319 3325 boolean_t cb_first;
3320 3326 boolean_t cb_dedup_stats;
3321 3327 } status_cbdata_t;
3322 3328
3323 3329 /*
3324 3330 * Print out detailed scrub status.
3325 3331 */
3326 3332 void
3327 3333 print_scan_status(pool_scan_stat_t *ps)
3328 3334 {
3329 3335 time_t start, end;
3330 3336 uint64_t elapsed, mins_left, hours_left;
3331 3337 uint64_t pass_exam, examined, total;
3332 3338 uint_t rate;
3333 3339 double fraction_done;
3334 3340 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3335 3341
3336 3342 (void) printf(gettext(" scan: "));
3337 3343
3338 3344 /* If there's never been a scan, there's not much to say. */
3339 3345 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3340 3346 ps->pss_func >= POOL_SCAN_FUNCS) {
3341 3347 (void) printf(gettext("none requested\n"));
3342 3348 return;
3343 3349 }
3344 3350
3345 3351 start = ps->pss_start_time;
3346 3352 end = ps->pss_end_time;
3347 3353 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3348 3354
3349 3355 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3350 3356 ps->pss_func == POOL_SCAN_RESILVER);
3351 3357 /*
3352 3358 * Scan is finished or canceled.
3353 3359 */
3354 3360 if (ps->pss_state == DSS_FINISHED) {
3355 3361 uint64_t minutes_taken = (end - start) / 60;
3356 3362 char *fmt;
3357 3363
3358 3364 if (ps->pss_func == POOL_SCAN_SCRUB) {
3359 3365 fmt = gettext("scrub repaired %s in %lluh%um with "
3360 3366 "%llu errors on %s");
3361 3367 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3362 3368 fmt = gettext("resilvered %s in %lluh%um with "
3363 3369 "%llu errors on %s");
3364 3370 }
3365 3371 /* LINTED */
3366 3372 (void) printf(fmt, processed_buf,
3367 3373 (u_longlong_t)(minutes_taken / 60),
3368 3374 (uint_t)(minutes_taken % 60),
3369 3375 (u_longlong_t)ps->pss_errors,
3370 3376 ctime((time_t *)&end));
3371 3377 return;
3372 3378 } else if (ps->pss_state == DSS_CANCELED) {
3373 3379 if (ps->pss_func == POOL_SCAN_SCRUB) {
3374 3380 (void) printf(gettext("scrub canceled on %s"),
3375 3381 ctime(&end));
3376 3382 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3377 3383 (void) printf(gettext("resilver canceled on %s"),
3378 3384 ctime(&end));
3379 3385 }
3380 3386 return;
3381 3387 }
3382 3388
3383 3389 assert(ps->pss_state == DSS_SCANNING);
3384 3390
3385 3391 /*
3386 3392 * Scan is in progress.
3387 3393 */
3388 3394 if (ps->pss_func == POOL_SCAN_SCRUB) {
3389 3395 (void) printf(gettext("scrub in progress since %s"),
3390 3396 ctime(&start));
3391 3397 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3392 3398 (void) printf(gettext("resilver in progress since %s"),
3393 3399 ctime(&start));
3394 3400 }
3395 3401
3396 3402 examined = ps->pss_examined ? ps->pss_examined : 1;
3397 3403 total = ps->pss_to_examine;
3398 3404 fraction_done = (double)examined / total;
3399 3405
3400 3406 /* elapsed time for this pass */
3401 3407 elapsed = time(NULL) - ps->pss_pass_start;
3402 3408 elapsed = elapsed ? elapsed : 1;
3403 3409 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3404 3410 rate = pass_exam / elapsed;
3405 3411 rate = rate ? rate : 1;
3406 3412 mins_left = ((total - examined) / rate) / 60;
3407 3413 hours_left = mins_left / 60;
3408 3414
3409 3415 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3410 3416 zfs_nicenum(total, total_buf, sizeof (total_buf));
3411 3417 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3412 3418
3413 3419 /*
3414 3420 * do not print estimated time if hours_left is more than 30 days
3415 3421 */
3416 3422 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3417 3423 examined_buf, total_buf, rate_buf);
3418 3424 if (hours_left < (30 * 24)) {
3419 3425 (void) printf(gettext(", %lluh%um to go\n"),
3420 3426 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3421 3427 } else {
3422 3428 (void) printf(gettext(
3423 3429 ", (scan is slow, no estimated time)\n"));
3424 3430 }
3425 3431
3426 3432 if (ps->pss_func == POOL_SCAN_RESILVER) {
3427 3433 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3428 3434 processed_buf, 100 * fraction_done);
3429 3435 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3430 3436 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3431 3437 processed_buf, 100 * fraction_done);
3432 3438 }
3433 3439 }
3434 3440
3435 3441 static void
3436 3442 print_error_log(zpool_handle_t *zhp)
3437 3443 {
3438 3444 nvlist_t *nverrlist = NULL;
3439 3445 nvpair_t *elem;
3440 3446 char *pathname;
3441 3447 size_t len = MAXPATHLEN * 2;
3442 3448
3443 3449 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
3444 3450 (void) printf("errors: List of errors unavailable "
3445 3451 "(insufficient privileges)\n");
3446 3452 return;
3447 3453 }
3448 3454
3449 3455 (void) printf("errors: Permanent errors have been "
3450 3456 "detected in the following files:\n\n");
3451 3457
3452 3458 pathname = safe_malloc(len);
3453 3459 elem = NULL;
3454 3460 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
3455 3461 nvlist_t *nv;
3456 3462 uint64_t dsobj, obj;
3457 3463
3458 3464 verify(nvpair_value_nvlist(elem, &nv) == 0);
3459 3465 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
3460 3466 &dsobj) == 0);
3461 3467 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
3462 3468 &obj) == 0);
3463 3469 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
3464 3470 (void) printf("%7s %s\n", "", pathname);
3465 3471 }
3466 3472 free(pathname);
3467 3473 nvlist_free(nverrlist);
3468 3474 }
3469 3475
3470 3476 static void
3471 3477 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
3472 3478 int namewidth)
3473 3479 {
3474 3480 uint_t i;
3475 3481 char *name;
3476 3482
3477 3483 if (nspares == 0)
3478 3484 return;
3479 3485
3480 3486 (void) printf(gettext("\tspares\n"));
3481 3487
3482 3488 for (i = 0; i < nspares; i++) {
3483 3489 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
3484 3490 print_status_config(zhp, name, spares[i],
3485 3491 namewidth, 2, B_TRUE);
3486 3492 free(name);
3487 3493 }
3488 3494 }
3489 3495
3490 3496 static void
3491 3497 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
3492 3498 int namewidth)
3493 3499 {
3494 3500 uint_t i;
3495 3501 char *name;
3496 3502
3497 3503 if (nl2cache == 0)
3498 3504 return;
3499 3505
3500 3506 (void) printf(gettext("\tcache\n"));
3501 3507
3502 3508 for (i = 0; i < nl2cache; i++) {
3503 3509 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
3504 3510 print_status_config(zhp, name, l2cache[i],
3505 3511 namewidth, 2, B_FALSE);
3506 3512 free(name);
3507 3513 }
3508 3514 }
3509 3515
3510 3516 static void
3511 3517 print_dedup_stats(nvlist_t *config)
3512 3518 {
3513 3519 ddt_histogram_t *ddh;
3514 3520 ddt_stat_t *dds;
3515 3521 ddt_object_t *ddo;
3516 3522 uint_t c;
3517 3523
3518 3524 /*
3519 3525 * If the pool was faulted then we may not have been able to
3520 3526 * obtain the config. Otherwise, if have anything in the dedup
3521 3527 * table continue processing the stats.
3522 3528 */
3523 3529 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
3524 3530 (uint64_t **)&ddo, &c) != 0)
3525 3531 return;
3526 3532
3527 3533 (void) printf("\n");
3528 3534 (void) printf(gettext(" dedup: "));
3529 3535 if (ddo->ddo_count == 0) {
3530 3536 (void) printf(gettext("no DDT entries\n"));
3531 3537 return;
3532 3538 }
3533 3539
3534 3540 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
3535 3541 (u_longlong_t)ddo->ddo_count,
3536 3542 (u_longlong_t)ddo->ddo_dspace,
3537 3543 (u_longlong_t)ddo->ddo_mspace);
3538 3544
3539 3545 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
3540 3546 (uint64_t **)&dds, &c) == 0);
3541 3547 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
3542 3548 (uint64_t **)&ddh, &c) == 0);
3543 3549 zpool_dump_ddt(dds, ddh);
3544 3550 }
3545 3551
3546 3552 /*
3547 3553 * Display a summary of pool status. Displays a summary such as:
3548 3554 *
3549 3555 * pool: tank
3550 3556 * status: DEGRADED
3551 3557 * reason: One or more devices ...
3552 3558 * see: http://www.sun.com/msg/ZFS-xxxx-01
3553 3559 * config:
3554 3560 * mirror DEGRADED
3555 3561 * c1t0d0 OK
3556 3562 * c2t0d0 UNAVAIL
3557 3563 *
3558 3564 * When given the '-v' option, we print out the complete config. If the '-e'
3559 3565 * option is specified, then we print out error rate information as well.
3560 3566 */
3561 3567 int
3562 3568 status_callback(zpool_handle_t *zhp, void *data)
3563 3569 {
3564 3570 status_cbdata_t *cbp = data;
3565 3571 nvlist_t *config, *nvroot;
3566 3572 char *msgid;
3567 3573 int reason;
3568 3574 const char *health;
3569 3575 uint_t c;
3570 3576 vdev_stat_t *vs;
3571 3577
3572 3578 config = zpool_get_config(zhp, NULL);
3573 3579 reason = zpool_get_status(zhp, &msgid);
3574 3580
3575 3581 cbp->cb_count++;
3576 3582
3577 3583 /*
3578 3584 * If we were given 'zpool status -x', only report those pools with
3579 3585 * problems.
3580 3586 */
3581 3587 if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
3582 3588 if (!cbp->cb_allpools) {
3583 3589 (void) printf(gettext("pool '%s' is healthy\n"),
3584 3590 zpool_get_name(zhp));
3585 3591 if (cbp->cb_first)
3586 3592 cbp->cb_first = B_FALSE;
3587 3593 }
3588 3594 return (0);
3589 3595 }
3590 3596
3591 3597 if (cbp->cb_first)
3592 3598 cbp->cb_first = B_FALSE;
3593 3599 else
3594 3600 (void) printf("\n");
3595 3601
3596 3602 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3597 3603 &nvroot) == 0);
3598 3604 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3599 3605 (uint64_t **)&vs, &c) == 0);
3600 3606 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3601 3607
3602 3608 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
3603 3609 (void) printf(gettext(" state: %s\n"), health);
3604 3610
3605 3611 switch (reason) {
3606 3612 case ZPOOL_STATUS_MISSING_DEV_R:
3607 3613 (void) printf(gettext("status: One or more devices could not "
3608 3614 "be opened. Sufficient replicas exist for\n\tthe pool to "
3609 3615 "continue functioning in a degraded state.\n"));
3610 3616 (void) printf(gettext("action: Attach the missing device and "
3611 3617 "online it using 'zpool online'.\n"));
3612 3618 break;
3613 3619
3614 3620 case ZPOOL_STATUS_MISSING_DEV_NR:
3615 3621 (void) printf(gettext("status: One or more devices could not "
3616 3622 "be opened. There are insufficient\n\treplicas for the "
3617 3623 "pool to continue functioning.\n"));
3618 3624 (void) printf(gettext("action: Attach the missing device and "
3619 3625 "online it using 'zpool online'.\n"));
3620 3626 break;
3621 3627
3622 3628 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3623 3629 (void) printf(gettext("status: One or more devices could not "
3624 3630 "be used because the label is missing or\n\tinvalid. "
3625 3631 "Sufficient replicas exist for the pool to continue\n\t"
3626 3632 "functioning in a degraded state.\n"));
3627 3633 (void) printf(gettext("action: Replace the device using "
3628 3634 "'zpool replace'.\n"));
3629 3635 break;
3630 3636
3631 3637 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3632 3638 (void) printf(gettext("status: One or more devices could not "
3633 3639 "be used because the label is missing \n\tor invalid. "
3634 3640 "There are insufficient replicas for the pool to "
3635 3641 "continue\n\tfunctioning.\n"));
3636 3642 zpool_explain_recover(zpool_get_handle(zhp),
3637 3643 zpool_get_name(zhp), reason, config);
3638 3644 break;
3639 3645
3640 3646 case ZPOOL_STATUS_FAILING_DEV:
3641 3647 (void) printf(gettext("status: One or more devices has "
3642 3648 "experienced an unrecoverable error. An\n\tattempt was "
3643 3649 "made to correct the error. Applications are "
3644 3650 "unaffected.\n"));
3645 3651 (void) printf(gettext("action: Determine if the device needs "
3646 3652 "to be replaced, and clear the errors\n\tusing "
3647 3653 "'zpool clear' or replace the device with 'zpool "
3648 3654 "replace'.\n"));
3649 3655 break;
3650 3656
3651 3657 case ZPOOL_STATUS_OFFLINE_DEV:
3652 3658 (void) printf(gettext("status: One or more devices has "
3653 3659 "been taken offline by the administrator.\n\tSufficient "
3654 3660 "replicas exist for the pool to continue functioning in "
3655 3661 "a\n\tdegraded state.\n"));
3656 3662 (void) printf(gettext("action: Online the device using "
3657 3663 "'zpool online' or replace the device with\n\t'zpool "
3658 3664 "replace'.\n"));
3659 3665 break;
3660 3666
3661 3667 case ZPOOL_STATUS_REMOVED_DEV:
3662 3668 (void) printf(gettext("status: One or more devices has "
3663 3669 "been removed by the administrator.\n\tSufficient "
3664 3670 "replicas exist for the pool to continue functioning in "
3665 3671 "a\n\tdegraded state.\n"));
3666 3672 (void) printf(gettext("action: Online the device using "
3667 3673 "'zpool online' or replace the device with\n\t'zpool "
3668 3674 "replace'.\n"));
3669 3675 break;
3670 3676
3671 3677 case ZPOOL_STATUS_RESILVERING:
3672 3678 (void) printf(gettext("status: One or more devices is "
3673 3679 "currently being resilvered. The pool will\n\tcontinue "
3674 3680 "to function, possibly in a degraded state.\n"));
3675 3681 (void) printf(gettext("action: Wait for the resilver to "
3676 3682 "complete.\n"));
3677 3683 break;
3678 3684
3679 3685 case ZPOOL_STATUS_CORRUPT_DATA:
3680 3686 (void) printf(gettext("status: One or more devices has "
3681 3687 "experienced an error resulting in data\n\tcorruption. "
3682 3688 "Applications may be affected.\n"));
3683 3689 (void) printf(gettext("action: Restore the file in question "
3684 3690 "if possible. Otherwise restore the\n\tentire pool from "
3685 3691 "backup.\n"));
3686 3692 break;
3687 3693
3688 3694 case ZPOOL_STATUS_CORRUPT_POOL:
3689 3695 (void) printf(gettext("status: The pool metadata is corrupted "
3690 3696 "and the pool cannot be opened.\n"));
3691 3697 zpool_explain_recover(zpool_get_handle(zhp),
3692 3698 zpool_get_name(zhp), reason, config);
3693 3699 break;
3694 3700
3695 3701 case ZPOOL_STATUS_VERSION_OLDER:
3696 3702 (void) printf(gettext("status: The pool is formatted using an "
3697 3703 "older on-disk format. The pool can\n\tstill be used, but "
3698 3704 "some features are unavailable.\n"));
3699 3705 (void) printf(gettext("action: Upgrade the pool using 'zpool "
3700 3706 "upgrade'. Once this is done, the\n\tpool will no longer "
3701 3707 "be accessible on older software versions.\n"));
3702 3708 break;
3703 3709
3704 3710 case ZPOOL_STATUS_VERSION_NEWER:
3705 3711 (void) printf(gettext("status: The pool has been upgraded to a "
3706 3712 "newer, incompatible on-disk version.\n\tThe pool cannot "
3707 3713 "be accessed on this system.\n"));
3708 3714 (void) printf(gettext("action: Access the pool from a system "
3709 3715 "running more recent software, or\n\trestore the pool from "
3710 3716 "backup.\n"));
3711 3717 break;
3712 3718
3713 3719 case ZPOOL_STATUS_FAULTED_DEV_R:
3714 3720 (void) printf(gettext("status: One or more devices are "
3715 3721 "faulted in response to persistent errors.\n\tSufficient "
3716 3722 "replicas exist for the pool to continue functioning "
3717 3723 "in a\n\tdegraded state.\n"));
3718 3724 (void) printf(gettext("action: Replace the faulted device, "
3719 3725 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
3720 3726 break;
3721 3727
3722 3728 case ZPOOL_STATUS_FAULTED_DEV_NR:
3723 3729 (void) printf(gettext("status: One or more devices are "
3724 3730 "faulted in response to persistent errors. There are "
3725 3731 "insufficient replicas for the pool to\n\tcontinue "
3726 3732 "functioning.\n"));
3727 3733 (void) printf(gettext("action: Destroy and re-create the pool "
3728 3734 "from a backup source. Manually marking the device\n"
3729 3735 "\trepaired using 'zpool clear' may allow some data "
3730 3736 "to be recovered.\n"));
3731 3737 break;
3732 3738
3733 3739 case ZPOOL_STATUS_IO_FAILURE_WAIT:
3734 3740 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
3735 3741 (void) printf(gettext("status: One or more devices are "
3736 3742 "faulted in response to IO failures.\n"));
3737 3743 (void) printf(gettext("action: Make sure the affected devices "
3738 3744 "are connected, then run 'zpool clear'.\n"));
3739 3745 break;
3740 3746
3741 3747 case ZPOOL_STATUS_BAD_LOG:
3742 3748 (void) printf(gettext("status: An intent log record "
3743 3749 "could not be read.\n"
3744 3750 "\tWaiting for adminstrator intervention to fix the "
3745 3751 "faulted pool.\n"));
3746 3752 (void) printf(gettext("action: Either restore the affected "
3747 3753 "device(s) and run 'zpool online',\n"
3748 3754 "\tor ignore the intent log records by running "
3749 3755 "'zpool clear'.\n"));
3750 3756 break;
3751 3757
3752 3758 default:
3753 3759 /*
3754 3760 * The remaining errors can't actually be generated, yet.
3755 3761 */
3756 3762 assert(reason == ZPOOL_STATUS_OK);
3757 3763 }
3758 3764
3759 3765 if (msgid != NULL)
3760 3766 (void) printf(gettext(" see: http://www.sun.com/msg/%s\n"),
3761 3767 msgid);
3762 3768
3763 3769 if (config != NULL) {
3764 3770 int namewidth;
3765 3771 uint64_t nerr;
3766 3772 nvlist_t **spares, **l2cache;
3767 3773 uint_t nspares, nl2cache;
3768 3774 pool_scan_stat_t *ps = NULL;
3769 3775
3770 3776 (void) nvlist_lookup_uint64_array(nvroot,
3771 3777 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
3772 3778 print_scan_status(ps);
3773 3779
3774 3780 namewidth = max_width(zhp, nvroot, 0, 0);
3775 3781 if (namewidth < 10)
3776 3782 namewidth = 10;
3777 3783
3778 3784 (void) printf(gettext("config:\n\n"));
3779 3785 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
3780 3786 "NAME", "STATE", "READ", "WRITE", "CKSUM");
3781 3787 print_status_config(zhp, zpool_get_name(zhp), nvroot,
3782 3788 namewidth, 0, B_FALSE);
3783 3789
3784 3790 if (num_logs(nvroot) > 0)
3785 3791 print_logs(zhp, nvroot, namewidth, B_TRUE);
3786 3792 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3787 3793 &l2cache, &nl2cache) == 0)
3788 3794 print_l2cache(zhp, l2cache, nl2cache, namewidth);
3789 3795
3790 3796 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3791 3797 &spares, &nspares) == 0)
3792 3798 print_spares(zhp, spares, nspares, namewidth);
3793 3799
3794 3800 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
3795 3801 &nerr) == 0) {
3796 3802 nvlist_t *nverrlist = NULL;
3797 3803
3798 3804 /*
3799 3805 * If the approximate error count is small, get a
3800 3806 * precise count by fetching the entire log and
3801 3807 * uniquifying the results.
3802 3808 */
3803 3809 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3804 3810 zpool_get_errlog(zhp, &nverrlist) == 0) {
3805 3811 nvpair_t *elem;
3806 3812
3807 3813 elem = NULL;
3808 3814 nerr = 0;
3809 3815 while ((elem = nvlist_next_nvpair(nverrlist,
3810 3816 elem)) != NULL) {
3811 3817 nerr++;
3812 3818 }
3813 3819 }
3814 3820 nvlist_free(nverrlist);
3815 3821
3816 3822 (void) printf("\n");
3817 3823
3818 3824 if (nerr == 0)
3819 3825 (void) printf(gettext("errors: No known data "
3820 3826 "errors\n"));
3821 3827 else if (!cbp->cb_verbose)
3822 3828 (void) printf(gettext("errors: %llu data "
3823 3829 "errors, use '-v' for a list\n"),
3824 3830 (u_longlong_t)nerr);
3825 3831 else
3826 3832 print_error_log(zhp);
3827 3833 }
3828 3834
3829 3835 if (cbp->cb_dedup_stats)
3830 3836 print_dedup_stats(config);
3831 3837 } else {
3832 3838 (void) printf(gettext("config: The configuration cannot be "
3833 3839 "determined.\n"));
3834 3840 }
3835 3841
3836 3842 return (0);
3837 3843 }
3838 3844
3839 3845 /*
3840 3846 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
3841 3847 *
3842 3848 * -v Display complete error logs
3843 3849 * -x Display only pools with potential problems
3844 3850 * -D Display dedup status (undocumented)
3845 3851 * -T Display a timestamp in date(1) or Unix format
3846 3852 *
3847 3853 * Describes the health status of all pools or some subset.
3848 3854 */
3849 3855 int
3850 3856 zpool_do_status(int argc, char **argv)
3851 3857 {
3852 3858 int c;
3853 3859 int ret;
3854 3860 unsigned long interval = 0, count = 0;
3855 3861 status_cbdata_t cb = { 0 };
3856 3862
3857 3863 /* check options */
3858 3864 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
3859 3865 switch (c) {
3860 3866 case 'v':
3861 3867 cb.cb_verbose = B_TRUE;
3862 3868 break;
3863 3869 case 'x':
3864 3870 cb.cb_explain = B_TRUE;
3865 3871 break;
3866 3872 case 'D':
3867 3873 cb.cb_dedup_stats = B_TRUE;
3868 3874 break;
3869 3875 case 'T':
3870 3876 get_timestamp_arg(*optarg);
3871 3877 break;
3872 3878 case '?':
3873 3879 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3874 3880 optopt);
3875 3881 usage(B_FALSE);
3876 3882 }
3877 3883 }
3878 3884
3879 3885 argc -= optind;
3880 3886 argv += optind;
3881 3887
3882 3888 get_interval_count(&argc, argv, &interval, &count);
3883 3889
3884 3890 if (argc == 0)
3885 3891 cb.cb_allpools = B_TRUE;
3886 3892
3887 3893 cb.cb_first = B_TRUE;
3888 3894
3889 3895 for (;;) {
3890 3896 if (timestamp_fmt != NODATE)
3891 3897 print_timestamp(timestamp_fmt);
3892 3898
3893 3899 ret = for_each_pool(argc, argv, B_TRUE, NULL,
3894 3900 status_callback, &cb);
3895 3901
3896 3902 if (argc == 0 && cb.cb_count == 0)
3897 3903 (void) printf(gettext("no pools available\n"));
3898 3904 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
3899 3905 (void) printf(gettext("all pools are healthy\n"));
3900 3906
3901 3907 if (ret != 0)
3902 3908 return (ret);
3903 3909
3904 3910 if (interval == 0)
3905 3911 break;
3906 3912
3907 3913 if (count != 0 && --count == 0)
3908 3914 break;
3909 3915
3910 3916 (void) sleep(interval);
3911 3917 }
3912 3918
3913 3919 return (0);
3914 3920 }
3915 3921
3916 3922 typedef struct upgrade_cbdata {
3917 3923 int cb_all;
3918 3924 int cb_first;
3919 3925 int cb_newer;
3920 3926 int cb_argc;
3921 3927 uint64_t cb_version;
3922 3928 char **cb_argv;
3923 3929 } upgrade_cbdata_t;
3924 3930
3925 3931 static int
3926 3932 upgrade_cb(zpool_handle_t *zhp, void *arg)
3927 3933 {
3928 3934 upgrade_cbdata_t *cbp = arg;
3929 3935 nvlist_t *config;
3930 3936 uint64_t version;
3931 3937 int ret = 0;
3932 3938
3933 3939 config = zpool_get_config(zhp, NULL);
3934 3940 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3935 3941 &version) == 0);
3936 3942
3937 3943 if (!cbp->cb_newer && version < SPA_VERSION) {
3938 3944 if (!cbp->cb_all) {
3939 3945 if (cbp->cb_first) {
3940 3946 (void) printf(gettext("The following pools are "
3941 3947 "out of date, and can be upgraded. After "
3942 3948 "being\nupgraded, these pools will no "
3943 3949 "longer be accessible by older software "
3944 3950 "versions.\n\n"));
3945 3951 (void) printf(gettext("VER POOL\n"));
3946 3952 (void) printf(gettext("--- ------------\n"));
3947 3953 cbp->cb_first = B_FALSE;
3948 3954 }
3949 3955
3950 3956 (void) printf("%2llu %s\n", (u_longlong_t)version,
3951 3957 zpool_get_name(zhp));
3952 3958 } else {
3953 3959 cbp->cb_first = B_FALSE;
3954 3960 ret = zpool_upgrade(zhp, cbp->cb_version);
3955 3961 if (!ret) {
3956 3962 (void) printf(gettext("Successfully upgraded "
3957 3963 "'%s'\n\n"), zpool_get_name(zhp));
3958 3964 }
3959 3965 }
3960 3966 } else if (cbp->cb_newer && version > SPA_VERSION) {
3961 3967 assert(!cbp->cb_all);
3962 3968
3963 3969 if (cbp->cb_first) {
3964 3970 (void) printf(gettext("The following pools are "
3965 3971 "formatted using a newer software version and\n"
3966 3972 "cannot be accessed on the current system.\n\n"));
3967 3973 (void) printf(gettext("VER POOL\n"));
3968 3974 (void) printf(gettext("--- ------------\n"));
3969 3975 cbp->cb_first = B_FALSE;
3970 3976 }
3971 3977
3972 3978 (void) printf("%2llu %s\n", (u_longlong_t)version,
3973 3979 zpool_get_name(zhp));
3974 3980 }
3975 3981
3976 3982 zpool_close(zhp);
3977 3983 return (ret);
3978 3984 }
3979 3985
3980 3986 /* ARGSUSED */
3981 3987 static int
3982 3988 upgrade_one(zpool_handle_t *zhp, void *data)
3983 3989 {
3984 3990 upgrade_cbdata_t *cbp = data;
3985 3991 uint64_t cur_version;
3986 3992 int ret;
3987 3993
3988 3994 if (strcmp("log", zpool_get_name(zhp)) == 0) {
3989 3995 (void) printf(gettext("'log' is now a reserved word\n"
3990 3996 "Pool 'log' must be renamed using export and import"
3991 3997 " to upgrade.\n"));
3992 3998 return (1);
3993 3999 }
3994 4000
3995 4001 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3996 4002 if (cur_version > cbp->cb_version) {
3997 4003 (void) printf(gettext("Pool '%s' is already formatted "
3998 4004 "using more current version '%llu'.\n"),
3999 4005 zpool_get_name(zhp), cur_version);
4000 4006 return (0);
4001 4007 }
4002 4008 if (cur_version == cbp->cb_version) {
4003 4009 (void) printf(gettext("Pool '%s' is already formatted "
4004 4010 "using the current version.\n"), zpool_get_name(zhp));
4005 4011 return (0);
4006 4012 }
4007 4013
4008 4014 ret = zpool_upgrade(zhp, cbp->cb_version);
4009 4015
4010 4016 if (!ret) {
4011 4017 (void) printf(gettext("Successfully upgraded '%s' "
4012 4018 "from version %llu to version %llu\n\n"),
4013 4019 zpool_get_name(zhp), (u_longlong_t)cur_version,
4014 4020 (u_longlong_t)cbp->cb_version);
4015 4021 }
4016 4022
4017 4023 return (ret != 0);
4018 4024 }
4019 4025
4020 4026 /*
4021 4027 * zpool upgrade
4022 4028 * zpool upgrade -v
4023 4029 * zpool upgrade [-V version] <-a | pool ...>
4024 4030 *
4025 4031 * With no arguments, display downrev'd ZFS pool available for upgrade.
4026 4032 * Individual pools can be upgraded by specifying the pool, and '-a' will
4027 4033 * upgrade all pools.
4028 4034 */
4029 4035 int
4030 4036 zpool_do_upgrade(int argc, char **argv)
4031 4037 {
4032 4038 int c;
4033 4039 upgrade_cbdata_t cb = { 0 };
4034 4040 int ret = 0;
4035 4041 boolean_t showversions = B_FALSE;
4036 4042 char *end;
4037 4043
4038 4044
4039 4045 /* check options */
4040 4046 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4041 4047 switch (c) {
4042 4048 case 'a':
4043 4049 cb.cb_all = B_TRUE;
4044 4050 break;
4045 4051 case 'v':
4046 4052 showversions = B_TRUE;
4047 4053 break;
4048 4054 case 'V':
4049 4055 cb.cb_version = strtoll(optarg, &end, 10);
4050 4056 if (*end != '\0' || cb.cb_version > SPA_VERSION ||
4051 4057 cb.cb_version < SPA_VERSION_1) {
4052 4058 (void) fprintf(stderr,
4053 4059 gettext("invalid version '%s'\n"), optarg);
4054 4060 usage(B_FALSE);
4055 4061 }
4056 4062 break;
4057 4063 case ':':
4058 4064 (void) fprintf(stderr, gettext("missing argument for "
4059 4065 "'%c' option\n"), optopt);
4060 4066 usage(B_FALSE);
4061 4067 break;
4062 4068 case '?':
4063 4069 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4064 4070 optopt);
4065 4071 usage(B_FALSE);
4066 4072 }
4067 4073 }
4068 4074
4069 4075 cb.cb_argc = argc;
4070 4076 cb.cb_argv = argv;
4071 4077 argc -= optind;
4072 4078 argv += optind;
4073 4079
4074 4080 if (cb.cb_version == 0) {
4075 4081 cb.cb_version = SPA_VERSION;
4076 4082 } else if (!cb.cb_all && argc == 0) {
4077 4083 (void) fprintf(stderr, gettext("-V option is "
4078 4084 "incompatible with other arguments\n"));
4079 4085 usage(B_FALSE);
4080 4086 }
4081 4087
4082 4088 if (showversions) {
4083 4089 if (cb.cb_all || argc != 0) {
4084 4090 (void) fprintf(stderr, gettext("-v option is "
4085 4091 "incompatible with other arguments\n"));
4086 4092 usage(B_FALSE);
4087 4093 }
4088 4094 } else if (cb.cb_all) {
4089 4095 if (argc != 0) {
4090 4096 (void) fprintf(stderr, gettext("-a option should not "
4091 4097 "be used along with a pool name\n"));
4092 4098 usage(B_FALSE);
4093 4099 }
4094 4100 }
4095 4101
4096 4102 (void) printf(gettext("This system is currently running "
4097 4103 "ZFS pool version %llu.\n\n"), SPA_VERSION);
4098 4104 cb.cb_first = B_TRUE;
4099 4105 if (showversions) {
4100 4106 (void) printf(gettext("The following versions are "
4101 4107 "supported:\n\n"));
4102 4108 (void) printf(gettext("VER DESCRIPTION\n"));
4103 4109 (void) printf("--- -----------------------------------------"
4104 4110 "---------------\n");
4105 4111 (void) printf(gettext(" 1 Initial ZFS version\n"));
4106 4112 (void) printf(gettext(" 2 Ditto blocks "
4107 4113 "(replicated metadata)\n"));
4108 4114 (void) printf(gettext(" 3 Hot spares and double parity "
4109 4115 "RAID-Z\n"));
4110 4116 (void) printf(gettext(" 4 zpool history\n"));
4111 4117 (void) printf(gettext(" 5 Compression using the gzip "
4112 4118 "algorithm\n"));
4113 4119 (void) printf(gettext(" 6 bootfs pool property\n"));
4114 4120 (void) printf(gettext(" 7 Separate intent log devices\n"));
4115 4121 (void) printf(gettext(" 8 Delegated administration\n"));
4116 4122 (void) printf(gettext(" 9 refquota and refreservation "
4117 4123 "properties\n"));
4118 4124 (void) printf(gettext(" 10 Cache devices\n"));
4119 4125 (void) printf(gettext(" 11 Improved scrub performance\n"));
4120 4126 (void) printf(gettext(" 12 Snapshot properties\n"));
4121 4127 (void) printf(gettext(" 13 snapused property\n"));
4122 4128 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4123 4129 (void) printf(gettext(" 15 user/group space accounting\n"));
4124 4130 (void) printf(gettext(" 16 stmf property support\n"));
4125 4131 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4126 4132 (void) printf(gettext(" 18 Snapshot user holds\n"));
4127 4133 (void) printf(gettext(" 19 Log device removal\n"));
4128 4134 (void) printf(gettext(" 20 Compression using zle "
4129 4135 "(zero-length encoding)\n"));
4130 4136 (void) printf(gettext(" 21 Deduplication\n"));
4131 4137 (void) printf(gettext(" 22 Received properties\n"));
4132 4138 (void) printf(gettext(" 23 Slim ZIL\n"));
4133 4139 (void) printf(gettext(" 24 System attributes\n"));
4134 4140 (void) printf(gettext(" 25 Improved scrub stats\n"));
4135 4141 (void) printf(gettext(" 26 Improved snapshot deletion "
4136 4142 "performance\n"));
4137 4143 (void) printf(gettext(" 27 Improved snapshot creation "
4138 4144 "performance\n"));
4139 4145 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4140 4146 (void) printf(gettext("\nFor more information on a particular "
4141 4147 "version, including supported releases,\n"));
4142 4148 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4143 4149 } else if (argc == 0) {
4144 4150 int notfound;
4145 4151
4146 4152 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4147 4153 notfound = cb.cb_first;
4148 4154
4149 4155 if (!cb.cb_all && ret == 0) {
4150 4156 if (!cb.cb_first)
4151 4157 (void) printf("\n");
4152 4158 cb.cb_first = B_TRUE;
4153 4159 cb.cb_newer = B_TRUE;
4154 4160 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4155 4161 if (!cb.cb_first) {
4156 4162 notfound = B_FALSE;
4157 4163 (void) printf("\n");
4158 4164 }
4159 4165 }
4160 4166
4161 4167 if (ret == 0) {
4162 4168 if (notfound)
4163 4169 (void) printf(gettext("All pools are formatted "
4164 4170 "using this version.\n"));
4165 4171 else if (!cb.cb_all)
4166 4172 (void) printf(gettext("Use 'zpool upgrade -v' "
4167 4173 "for a list of available versions and "
4168 4174 "their associated\nfeatures.\n"));
4169 4175 }
4170 4176 } else {
4171 4177 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4172 4178 upgrade_one, &cb);
4173 4179 }
4174 4180
4175 4181 return (ret);
4176 4182 }
4177 4183
4178 4184 typedef struct hist_cbdata {
4179 4185 boolean_t first;
4180 4186 int longfmt;
4181 4187 int internal;
4182 4188 } hist_cbdata_t;
4183 4189
4184 4190 /*
4185 4191 * Print out the command history for a specific pool.
4186 4192 */
4187 4193 static int
4188 4194 get_history_one(zpool_handle_t *zhp, void *data)
4189 4195 {
4190 4196 nvlist_t *nvhis;
4191 4197 nvlist_t **records;
4192 4198 uint_t numrecords;
4193 4199 char *cmdstr;
4194 4200 char *pathstr;
4195 4201 uint64_t dst_time;
4196 4202 time_t tsec;
4197 4203 struct tm t;
4198 4204 char tbuf[30];
4199 4205 int ret, i;
4200 4206 uint64_t who;
4201 4207 struct passwd *pwd;
4202 4208 char *hostname;
4203 4209 char *zonename;
4204 4210 char internalstr[MAXPATHLEN];
4205 4211 hist_cbdata_t *cb = (hist_cbdata_t *)data;
4206 4212 uint64_t txg;
4207 4213 uint64_t ievent;
4208 4214
4209 4215 cb->first = B_FALSE;
4210 4216
4211 4217 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
4212 4218
4213 4219 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
4214 4220 return (ret);
4215 4221
4216 4222 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
4217 4223 &records, &numrecords) == 0);
4218 4224 for (i = 0; i < numrecords; i++) {
4219 4225 if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4220 4226 &dst_time) != 0)
4221 4227 continue;
4222 4228
4223 4229 /* is it an internal event or a standard event? */
4224 4230 if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
4225 4231 &cmdstr) != 0) {
4226 4232 if (cb->internal == 0)
4227 4233 continue;
4228 4234
4229 4235 if (nvlist_lookup_uint64(records[i],
4230 4236 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
4231 4237 continue;
4232 4238 verify(nvlist_lookup_uint64(records[i],
4233 4239 ZPOOL_HIST_TXG, &txg) == 0);
4234 4240 verify(nvlist_lookup_string(records[i],
4235 4241 ZPOOL_HIST_INT_STR, &pathstr) == 0);
4236 4242 if (ievent >= LOG_END)
4237 4243 continue;
4238 4244 (void) snprintf(internalstr,
4239 4245 sizeof (internalstr),
4240 4246 "[internal %s txg:%lld] %s",
4241 4247 zfs_history_event_names[ievent], txg,
4242 4248 pathstr);
4243 4249 cmdstr = internalstr;
4244 4250 }
4245 4251 tsec = dst_time;
4246 4252 (void) localtime_r(&tsec, &t);
4247 4253 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
4248 4254 (void) printf("%s %s", tbuf, cmdstr);
4249 4255
4250 4256 if (!cb->longfmt) {
4251 4257 (void) printf("\n");
4252 4258 continue;
4253 4259 }
4254 4260 (void) printf(" [");
4255 4261 if (nvlist_lookup_uint64(records[i],
4256 4262 ZPOOL_HIST_WHO, &who) == 0) {
4257 4263 pwd = getpwuid((uid_t)who);
4258 4264 if (pwd)
4259 4265 (void) printf("user %s on",
4260 4266 pwd->pw_name);
4261 4267 else
4262 4268 (void) printf("user %d on",
4263 4269 (int)who);
4264 4270 } else {
4265 4271 (void) printf(gettext("no info]\n"));
4266 4272 continue;
4267 4273 }
4268 4274 if (nvlist_lookup_string(records[i],
4269 4275 ZPOOL_HIST_HOST, &hostname) == 0) {
4270 4276 (void) printf(" %s", hostname);
4271 4277 }
4272 4278 if (nvlist_lookup_string(records[i],
4273 4279 ZPOOL_HIST_ZONE, &zonename) == 0) {
4274 4280 (void) printf(":%s", zonename);
4275 4281 }
4276 4282
4277 4283 (void) printf("]");
4278 4284 (void) printf("\n");
4279 4285 }
4280 4286 (void) printf("\n");
4281 4287 nvlist_free(nvhis);
4282 4288
4283 4289 return (ret);
4284 4290 }
4285 4291
4286 4292 /*
4287 4293 * zpool history <pool>
4288 4294 *
4289 4295 * Displays the history of commands that modified pools.
4290 4296 */
4291 4297
4292 4298
4293 4299 int
4294 4300 zpool_do_history(int argc, char **argv)
4295 4301 {
4296 4302 hist_cbdata_t cbdata = { 0 };
4297 4303 int ret;
4298 4304 int c;
4299 4305
4300 4306 cbdata.first = B_TRUE;
4301 4307 /* check options */
4302 4308 while ((c = getopt(argc, argv, "li")) != -1) {
4303 4309 switch (c) {
4304 4310 case 'l':
4305 4311 cbdata.longfmt = 1;
4306 4312 break;
4307 4313 case 'i':
4308 4314 cbdata.internal = 1;
4309 4315 break;
4310 4316 case '?':
4311 4317 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4312 4318 optopt);
4313 4319 usage(B_FALSE);
4314 4320 }
4315 4321 }
4316 4322 argc -= optind;
4317 4323 argv += optind;
4318 4324
4319 4325 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
4320 4326 &cbdata);
4321 4327
4322 4328 if (argc == 0 && cbdata.first == B_TRUE) {
4323 4329 (void) printf(gettext("no pools available\n"));
4324 4330 return (0);
4325 4331 }
4326 4332
4327 4333 return (ret);
4328 4334 }
4329 4335
4330 4336 static int
4331 4337 get_callback(zpool_handle_t *zhp, void *data)
4332 4338 {
4333 4339 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
4334 4340 char value[MAXNAMELEN];
4335 4341 zprop_source_t srctype;
4336 4342 zprop_list_t *pl;
4337 4343
4338 4344 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
4339 4345
4340 4346 /*
4341 4347 * Skip the special fake placeholder. This will also skip
4342 4348 * over the name property when 'all' is specified.
4343 4349 */
4344 4350 if (pl->pl_prop == ZPOOL_PROP_NAME &&
4345 4351 pl == cbp->cb_proplist)
4346 4352 continue;
4347 4353
4348 4354 if (zpool_get_prop(zhp, pl->pl_prop,
4349 4355 value, sizeof (value), &srctype) != 0)
4350 4356 continue;
4351 4357
4352 4358 zprop_print_one_property(zpool_get_name(zhp), cbp,
4353 4359 zpool_prop_to_name(pl->pl_prop), value, srctype, NULL,
4354 4360 NULL);
4355 4361 }
4356 4362 return (0);
4357 4363 }
4358 4364
4359 4365 int
4360 4366 zpool_do_get(int argc, char **argv)
4361 4367 {
4362 4368 zprop_get_cbdata_t cb = { 0 };
4363 4369 zprop_list_t fake_name = { 0 };
4364 4370 int ret;
4365 4371
4366 4372 if (argc < 3)
4367 4373 usage(B_FALSE);
4368 4374
4369 4375 cb.cb_first = B_TRUE;
4370 4376 cb.cb_sources = ZPROP_SRC_ALL;
4371 4377 cb.cb_columns[0] = GET_COL_NAME;
4372 4378 cb.cb_columns[1] = GET_COL_PROPERTY;
4373 4379 cb.cb_columns[2] = GET_COL_VALUE;
4374 4380 cb.cb_columns[3] = GET_COL_SOURCE;
4375 4381 cb.cb_type = ZFS_TYPE_POOL;
4376 4382
4377 4383 if (zprop_get_list(g_zfs, argv[1], &cb.cb_proplist,
4378 4384 ZFS_TYPE_POOL) != 0)
4379 4385 usage(B_FALSE);
4380 4386
4381 4387 if (cb.cb_proplist != NULL) {
4382 4388 fake_name.pl_prop = ZPOOL_PROP_NAME;
4383 4389 fake_name.pl_width = strlen(gettext("NAME"));
4384 4390 fake_name.pl_next = cb.cb_proplist;
4385 4391 cb.cb_proplist = &fake_name;
4386 4392 }
4387 4393
4388 4394 ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
4389 4395 get_callback, &cb);
4390 4396
4391 4397 if (cb.cb_proplist == &fake_name)
4392 4398 zprop_free_list(fake_name.pl_next);
4393 4399 else
4394 4400 zprop_free_list(cb.cb_proplist);
4395 4401
4396 4402 return (ret);
4397 4403 }
4398 4404
4399 4405 typedef struct set_cbdata {
4400 4406 char *cb_propname;
4401 4407 char *cb_value;
4402 4408 boolean_t cb_any_successful;
4403 4409 } set_cbdata_t;
4404 4410
4405 4411 int
4406 4412 set_callback(zpool_handle_t *zhp, void *data)
4407 4413 {
4408 4414 int error;
4409 4415 set_cbdata_t *cb = (set_cbdata_t *)data;
4410 4416
4411 4417 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
4412 4418
4413 4419 if (!error)
4414 4420 cb->cb_any_successful = B_TRUE;
4415 4421
4416 4422 return (error);
4417 4423 }
4418 4424
4419 4425 int
4420 4426 zpool_do_set(int argc, char **argv)
4421 4427 {
4422 4428 set_cbdata_t cb = { 0 };
4423 4429 int error;
4424 4430
4425 4431 if (argc > 1 && argv[1][0] == '-') {
4426 4432 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4427 4433 argv[1][1]);
4428 4434 usage(B_FALSE);
4429 4435 }
4430 4436
4431 4437 if (argc < 2) {
4432 4438 (void) fprintf(stderr, gettext("missing property=value "
4433 4439 "argument\n"));
4434 4440 usage(B_FALSE);
4435 4441 }
4436 4442
4437 4443 if (argc < 3) {
4438 4444 (void) fprintf(stderr, gettext("missing pool name\n"));
4439 4445 usage(B_FALSE);
4440 4446 }
4441 4447
4442 4448 if (argc > 3) {
4443 4449 (void) fprintf(stderr, gettext("too many pool names\n"));
4444 4450 usage(B_FALSE);
4445 4451 }
4446 4452
4447 4453 cb.cb_propname = argv[1];
4448 4454 cb.cb_value = strchr(cb.cb_propname, '=');
4449 4455 if (cb.cb_value == NULL) {
4450 4456 (void) fprintf(stderr, gettext("missing value in "
4451 4457 "property=value argument\n"));
4452 4458 usage(B_FALSE);
4453 4459 }
4454 4460
4455 4461 *(cb.cb_value) = '\0';
4456 4462 cb.cb_value++;
4457 4463
4458 4464 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
4459 4465 set_callback, &cb);
4460 4466
4461 4467 return (error);
4462 4468 }
4463 4469
4464 4470 static int
4465 4471 find_command_idx(char *command, int *idx)
4466 4472 {
4467 4473 int i;
4468 4474
4469 4475 for (i = 0; i < NCOMMAND; i++) {
4470 4476 if (command_table[i].name == NULL)
4471 4477 continue;
4472 4478
4473 4479 if (strcmp(command, command_table[i].name) == 0) {
4474 4480 *idx = i;
4475 4481 return (0);
4476 4482 }
4477 4483 }
4478 4484 return (1);
4479 4485 }
4480 4486
4481 4487 int
4482 4488 main(int argc, char **argv)
4483 4489 {
4484 4490 int ret;
4485 4491 int i;
4486 4492 char *cmdname;
4487 4493
4488 4494 (void) setlocale(LC_ALL, "");
4489 4495 (void) textdomain(TEXT_DOMAIN);
4490 4496
4491 4497 if ((g_zfs = libzfs_init()) == NULL) {
4492 4498 (void) fprintf(stderr, gettext("internal error: failed to "
4493 4499 "initialize ZFS library\n"));
4494 4500 return (1);
4495 4501 }
4496 4502
4497 4503 libzfs_print_on_error(g_zfs, B_TRUE);
4498 4504
4499 4505 opterr = 0;
4500 4506
4501 4507 /*
4502 4508 * Make sure the user has specified some command.
4503 4509 */
4504 4510 if (argc < 2) {
4505 4511 (void) fprintf(stderr, gettext("missing command\n"));
4506 4512 usage(B_FALSE);
4507 4513 }
4508 4514
4509 4515 cmdname = argv[1];
4510 4516
4511 4517 /*
4512 4518 * Special case '-?'
4513 4519 */
4514 4520 if (strcmp(cmdname, "-?") == 0)
4515 4521 usage(B_TRUE);
4516 4522
4517 4523 zpool_set_history_str("zpool", argc, argv, history_str);
4518 4524 verify(zpool_stage_history(g_zfs, history_str) == 0);
4519 4525
4520 4526 /*
4521 4527 * Run the appropriate command.
4522 4528 */
4523 4529 if (find_command_idx(cmdname, &i) == 0) {
4524 4530 current_command = &command_table[i];
4525 4531 ret = command_table[i].func(argc - 1, argv + 1);
4526 4532 } else if (strchr(cmdname, '=')) {
4527 4533 verify(find_command_idx("set", &i) == 0);
4528 4534 current_command = &command_table[i];
4529 4535 ret = command_table[i].func(argc, argv);
4530 4536 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
4531 4537 /*
4532 4538 * 'freeze' is a vile debugging abomination, so we treat
4533 4539 * it as such.
4534 4540 */
4535 4541 char buf[16384];
4536 4542 int fd = open(ZFS_DEV, O_RDWR);
4537 4543 (void) strcpy((void *)buf, argv[2]);
4538 4544 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4539 4545 } else {
4540 4546 (void) fprintf(stderr, gettext("unrecognized "
4541 4547 "command '%s'\n"), cmdname);
4542 4548 usage(B_FALSE);
4543 4549 }
4544 4550
4545 4551 libzfs_fini(g_zfs);
4546 4552
4547 4553 /*
4548 4554 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
4549 4555 * for the purposes of running ::findleaks.
4550 4556 */
4551 4557 if (getenv("ZFS_ABORT") != NULL) {
4552 4558 (void) printf("dumping core by request\n");
4553 4559 abort();
4554 4560 }
4555 4561
4556 4562 return (ret);
4557 4563 }
|
↓ open down ↓ |
3059 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX