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