zpool_main.c revision 268621
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd 22168404Spjd/* 23219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24227497Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25259813Sdelphij * Copyright (c) 2013 by Delphix. All rights reserved. 26236145Smm * Copyright (c) 2012 by Frederik Wessels. All rights reserved. 27236155Smm * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 28254758Sdelphij * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved. 29168404Spjd */ 30168404Spjd 31168404Spjd#include <solaris.h> 32168404Spjd#include <assert.h> 33168404Spjd#include <ctype.h> 34168404Spjd#include <dirent.h> 35168404Spjd#include <errno.h> 36168404Spjd#include <fcntl.h> 37168404Spjd#include <libgen.h> 38168404Spjd#include <libintl.h> 39168404Spjd#include <libuutil.h> 40168404Spjd#include <locale.h> 41168404Spjd#include <stdio.h> 42168404Spjd#include <stdlib.h> 43168404Spjd#include <string.h> 44168404Spjd#include <strings.h> 45168404Spjd#include <unistd.h> 46168404Spjd#include <priv.h> 47185029Spjd#include <pwd.h> 48185029Spjd#include <zone.h> 49168404Spjd#include <sys/time.h> 50236155Smm#include <zfs_prop.h> 51168404Spjd#include <sys/fs/zfs.h> 52168404Spjd#include <sys/stat.h> 53168404Spjd 54168404Spjd#include <libzfs.h> 55168404Spjd 56168404Spjd#include "zpool_util.h" 57185029Spjd#include "zfs_comutil.h" 58236884Smm#include "zfeature_common.h" 59168404Spjd 60219089Spjd#include "statcommon.h" 61219089Spjd 62168404Spjdstatic int zpool_do_create(int, char **); 63168404Spjdstatic int zpool_do_destroy(int, char **); 64168404Spjd 65168404Spjdstatic int zpool_do_add(int, char **); 66168404Spjdstatic int zpool_do_remove(int, char **); 67224171Sgibbsstatic int zpool_do_labelclear(int, char **); 68168404Spjd 69168404Spjdstatic int zpool_do_list(int, char **); 70168404Spjdstatic int zpool_do_iostat(int, char **); 71168404Spjdstatic int zpool_do_status(int, char **); 72168404Spjd 73168404Spjdstatic int zpool_do_online(int, char **); 74168404Spjdstatic int zpool_do_offline(int, char **); 75168404Spjdstatic int zpool_do_clear(int, char **); 76236155Smmstatic int zpool_do_reopen(int, char **); 77168404Spjd 78228103Smmstatic int zpool_do_reguid(int, char **); 79228103Smm 80168404Spjdstatic int zpool_do_attach(int, char **); 81168404Spjdstatic int zpool_do_detach(int, char **); 82168404Spjdstatic int zpool_do_replace(int, char **); 83219089Spjdstatic int zpool_do_split(int, char **); 84168404Spjd 85168404Spjdstatic int zpool_do_scrub(int, char **); 86168404Spjd 87168404Spjdstatic int zpool_do_import(int, char **); 88168404Spjdstatic int zpool_do_export(int, char **); 89168404Spjd 90168404Spjdstatic int zpool_do_upgrade(int, char **); 91168404Spjd 92168404Spjdstatic int zpool_do_history(int, char **); 93168404Spjd 94168404Spjdstatic int zpool_do_get(int, char **); 95168404Spjdstatic int zpool_do_set(int, char **); 96168404Spjd 97168404Spjd/* 98168404Spjd * These libumem hooks provide a reasonable set of defaults for the allocator's 99168404Spjd * debugging facilities. 100168404Spjd */ 101185029Spjd 102185029Spjd#ifdef DEBUG 103168404Spjdconst char * 104168404Spjd_umem_debug_init(void) 105168404Spjd{ 106168404Spjd return ("default,verbose"); /* $UMEM_DEBUG setting */ 107168404Spjd} 108168404Spjd 109168404Spjdconst char * 110168404Spjd_umem_logging_init(void) 111168404Spjd{ 112168404Spjd return ("fail,contents"); /* $UMEM_LOGGING setting */ 113168404Spjd} 114185029Spjd#endif 115168404Spjd 116168404Spjdtypedef enum { 117168404Spjd HELP_ADD, 118168404Spjd HELP_ATTACH, 119168404Spjd HELP_CLEAR, 120168404Spjd HELP_CREATE, 121168404Spjd HELP_DESTROY, 122168404Spjd HELP_DETACH, 123168404Spjd HELP_EXPORT, 124168404Spjd HELP_HISTORY, 125168404Spjd HELP_IMPORT, 126168404Spjd HELP_IOSTAT, 127224171Sgibbs HELP_LABELCLEAR, 128168404Spjd HELP_LIST, 129168404Spjd HELP_OFFLINE, 130168404Spjd HELP_ONLINE, 131168404Spjd HELP_REPLACE, 132168404Spjd HELP_REMOVE, 133168404Spjd HELP_SCRUB, 134168404Spjd HELP_STATUS, 135168404Spjd HELP_UPGRADE, 136168404Spjd HELP_GET, 137219089Spjd HELP_SET, 138228103Smm HELP_SPLIT, 139236155Smm HELP_REGUID, 140236155Smm HELP_REOPEN 141168404Spjd} zpool_help_t; 142168404Spjd 143168404Spjd 144168404Spjdtypedef struct zpool_command { 145168404Spjd const char *name; 146168404Spjd int (*func)(int, char **); 147168404Spjd zpool_help_t usage; 148168404Spjd} zpool_command_t; 149168404Spjd 150168404Spjd/* 151168404Spjd * Master command table. Each ZFS command has a name, associated function, and 152168404Spjd * usage message. The usage messages need to be internationalized, so we have 153168404Spjd * to have a function to return the usage message based on a command index. 154168404Spjd * 155168404Spjd * These commands are organized according to how they are displayed in the usage 156168404Spjd * message. An empty command (one with a NULL name) indicates an empty line in 157168404Spjd * the generic usage message. 158168404Spjd */ 159168404Spjdstatic zpool_command_t command_table[] = { 160168404Spjd { "create", zpool_do_create, HELP_CREATE }, 161168404Spjd { "destroy", zpool_do_destroy, HELP_DESTROY }, 162168404Spjd { NULL }, 163168404Spjd { "add", zpool_do_add, HELP_ADD }, 164168404Spjd { "remove", zpool_do_remove, HELP_REMOVE }, 165168404Spjd { NULL }, 166224171Sgibbs { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR }, 167224171Sgibbs { NULL }, 168168404Spjd { "list", zpool_do_list, HELP_LIST }, 169168404Spjd { "iostat", zpool_do_iostat, HELP_IOSTAT }, 170168404Spjd { "status", zpool_do_status, HELP_STATUS }, 171168404Spjd { NULL }, 172168404Spjd { "online", zpool_do_online, HELP_ONLINE }, 173168404Spjd { "offline", zpool_do_offline, HELP_OFFLINE }, 174168404Spjd { "clear", zpool_do_clear, HELP_CLEAR }, 175236155Smm { "reopen", zpool_do_reopen, HELP_REOPEN }, 176168404Spjd { NULL }, 177168404Spjd { "attach", zpool_do_attach, HELP_ATTACH }, 178168404Spjd { "detach", zpool_do_detach, HELP_DETACH }, 179168404Spjd { "replace", zpool_do_replace, HELP_REPLACE }, 180219089Spjd { "split", zpool_do_split, HELP_SPLIT }, 181168404Spjd { NULL }, 182168404Spjd { "scrub", zpool_do_scrub, HELP_SCRUB }, 183168404Spjd { NULL }, 184168404Spjd { "import", zpool_do_import, HELP_IMPORT }, 185168404Spjd { "export", zpool_do_export, HELP_EXPORT }, 186168404Spjd { "upgrade", zpool_do_upgrade, HELP_UPGRADE }, 187228103Smm { "reguid", zpool_do_reguid, HELP_REGUID }, 188168404Spjd { NULL }, 189168404Spjd { "history", zpool_do_history, HELP_HISTORY }, 190168404Spjd { "get", zpool_do_get, HELP_GET }, 191168404Spjd { "set", zpool_do_set, HELP_SET }, 192168404Spjd}; 193168404Spjd 194168404Spjd#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 195168404Spjd 196248571Smmstatic zpool_command_t *current_command; 197185029Spjdstatic char history_str[HIS_MAX_RECORD_LEN]; 198248571Smmstatic boolean_t log_history = B_TRUE; 199219089Spjdstatic uint_t timestamp_fmt = NODATE; 200219089Spjd 201168404Spjdstatic const char * 202168404Spjdget_usage(zpool_help_t idx) { 203168404Spjd switch (idx) { 204168404Spjd case HELP_ADD: 205168404Spjd return (gettext("\tadd [-fn] <pool> <vdev> ...\n")); 206168404Spjd case HELP_ATTACH: 207168404Spjd return (gettext("\tattach [-f] <pool> <device> " 208185029Spjd "<new-device>\n")); 209168404Spjd case HELP_CLEAR: 210219089Spjd return (gettext("\tclear [-nF] <pool> [device]\n")); 211168404Spjd case HELP_CREATE: 212236884Smm return (gettext("\tcreate [-fnd] [-o property=value] ... \n" 213185029Spjd "\t [-O file-system-property=value] ... \n" 214185029Spjd "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n")); 215168404Spjd case HELP_DESTROY: 216168404Spjd return (gettext("\tdestroy [-f] <pool>\n")); 217168404Spjd case HELP_DETACH: 218168404Spjd return (gettext("\tdetach <pool> <device>\n")); 219168404Spjd case HELP_EXPORT: 220168404Spjd return (gettext("\texport [-f] <pool> ...\n")); 221168404Spjd case HELP_HISTORY: 222185029Spjd return (gettext("\thistory [-il] [<pool>] ...\n")); 223168404Spjd case HELP_IMPORT: 224168404Spjd return (gettext("\timport [-d dir] [-D]\n" 225219089Spjd "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n" 226185029Spjd "\timport [-o mntopts] [-o property=value] ... \n" 227219089Spjd "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 228219089Spjd "[-R root] [-F [-n]] -a\n" 229185029Spjd "\timport [-o mntopts] [-o property=value] ... \n" 230219089Spjd "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] " 231219089Spjd "[-R root] [-F [-n]]\n" 232219089Spjd "\t <pool | id> [newpool]\n")); 233168404Spjd case HELP_IOSTAT: 234219089Spjd return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval " 235168404Spjd "[count]]\n")); 236224171Sgibbs case HELP_LABELCLEAR: 237224171Sgibbs return (gettext("\tlabelclear [-f] <vdev>\n")); 238168404Spjd case HELP_LIST: 239263889Sdelphij return (gettext("\tlist [-Hpv] [-o property[,...]] " 240219089Spjd "[-T d|u] [pool] ... [interval [count]]\n")); 241168404Spjd case HELP_OFFLINE: 242168404Spjd return (gettext("\toffline [-t] <pool> <device> ...\n")); 243168404Spjd case HELP_ONLINE: 244228020Smm return (gettext("\tonline [-e] <pool> <device> ...\n")); 245168404Spjd case HELP_REPLACE: 246168404Spjd return (gettext("\treplace [-f] <pool> <device> " 247185029Spjd "[new-device]\n")); 248168404Spjd case HELP_REMOVE: 249185029Spjd return (gettext("\tremove <pool> <device> ...\n")); 250236155Smm case HELP_REOPEN: 251260138Sdelphij return (gettext("\treopen <pool>\n")); 252168404Spjd case HELP_SCRUB: 253168404Spjd return (gettext("\tscrub [-s] <pool> ...\n")); 254168404Spjd case HELP_STATUS: 255219089Spjd return (gettext("\tstatus [-vx] [-T d|u] [pool] ... [interval " 256219089Spjd "[count]]\n")); 257168404Spjd case HELP_UPGRADE: 258228020Smm return (gettext("\tupgrade [-v]\n" 259185029Spjd "\tupgrade [-V version] <-a | pool ...>\n")); 260168404Spjd case HELP_GET: 261263889Sdelphij return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] " 262263889Sdelphij "<\"all\" | property[,...]> <pool> ...\n")); 263168404Spjd case HELP_SET: 264168404Spjd return (gettext("\tset <property=value> <pool> \n")); 265219089Spjd case HELP_SPLIT: 266219089Spjd return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n" 267219089Spjd "\t [-o property=value] <pool> <newpool> " 268219089Spjd "[<device> ...]\n")); 269228103Smm case HELP_REGUID: 270228103Smm return (gettext("\treguid <pool>\n")); 271168404Spjd } 272168404Spjd 273168404Spjd abort(); 274168404Spjd /* NOTREACHED */ 275168404Spjd} 276168404Spjd 277168404Spjd 278168404Spjd/* 279168404Spjd * Callback routine that will print out a pool property value. 280168404Spjd */ 281185029Spjdstatic int 282185029Spjdprint_prop_cb(int prop, void *cb) 283168404Spjd{ 284168404Spjd FILE *fp = cb; 285168404Spjd 286219089Spjd (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop)); 287168404Spjd 288185029Spjd if (zpool_prop_readonly(prop)) 289185029Spjd (void) fprintf(fp, " NO "); 290185029Spjd else 291219089Spjd (void) fprintf(fp, " YES "); 292185029Spjd 293168404Spjd if (zpool_prop_values(prop) == NULL) 294168404Spjd (void) fprintf(fp, "-\n"); 295168404Spjd else 296168404Spjd (void) fprintf(fp, "%s\n", zpool_prop_values(prop)); 297168404Spjd 298185029Spjd return (ZPROP_CONT); 299168404Spjd} 300168404Spjd 301168404Spjd/* 302168404Spjd * Display usage message. If we're inside a command, display only the usage for 303168404Spjd * that command. Otherwise, iterate over the entire command table and display 304168404Spjd * a complete usage message. 305168404Spjd */ 306168404Spjdvoid 307168404Spjdusage(boolean_t requested) 308168404Spjd{ 309168404Spjd FILE *fp = requested ? stdout : stderr; 310168404Spjd 311168404Spjd if (current_command == NULL) { 312168404Spjd int i; 313168404Spjd 314168404Spjd (void) fprintf(fp, gettext("usage: zpool command args ...\n")); 315168404Spjd (void) fprintf(fp, 316168404Spjd gettext("where 'command' is one of the following:\n\n")); 317168404Spjd 318168404Spjd for (i = 0; i < NCOMMAND; i++) { 319168404Spjd if (command_table[i].name == NULL) 320168404Spjd (void) fprintf(fp, "\n"); 321168404Spjd else 322168404Spjd (void) fprintf(fp, "%s", 323168404Spjd get_usage(command_table[i].usage)); 324168404Spjd } 325168404Spjd } else { 326168404Spjd (void) fprintf(fp, gettext("usage:\n")); 327168404Spjd (void) fprintf(fp, "%s", get_usage(current_command->usage)); 328168404Spjd } 329168404Spjd 330168404Spjd if (current_command != NULL && 331168404Spjd ((strcmp(current_command->name, "set") == 0) || 332185029Spjd (strcmp(current_command->name, "get") == 0) || 333185029Spjd (strcmp(current_command->name, "list") == 0))) { 334168404Spjd 335168404Spjd (void) fprintf(fp, 336168404Spjd gettext("\nthe following properties are supported:\n")); 337168404Spjd 338219089Spjd (void) fprintf(fp, "\n\t%-15s %s %s\n\n", 339185029Spjd "PROPERTY", "EDIT", "VALUES"); 340168404Spjd 341168404Spjd /* Iterate over all properties */ 342185029Spjd (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE, 343185029Spjd ZFS_TYPE_POOL); 344236884Smm 345236884Smm (void) fprintf(fp, "\t%-15s ", "feature@..."); 346236884Smm (void) fprintf(fp, "YES disabled | enabled | active\n"); 347236884Smm 348236884Smm (void) fprintf(fp, gettext("\nThe feature@ properties must be " 349243014Smm "appended with a feature name.\nSee zpool-features(7).\n")); 350168404Spjd } 351168404Spjd 352168404Spjd /* 353168404Spjd * See comments at end of main(). 354168404Spjd */ 355168404Spjd if (getenv("ZFS_ABORT") != NULL) { 356168404Spjd (void) printf("dumping core by request\n"); 357168404Spjd abort(); 358168404Spjd } 359168404Spjd 360168404Spjd exit(requested ? 0 : 2); 361168404Spjd} 362168404Spjd 363168404Spjdvoid 364185029Spjdprint_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent, 365185029Spjd boolean_t print_logs) 366168404Spjd{ 367168404Spjd nvlist_t **child; 368168404Spjd uint_t c, children; 369168404Spjd char *vname; 370168404Spjd 371168404Spjd if (name != NULL) 372168404Spjd (void) printf("\t%*s%s\n", indent, "", name); 373168404Spjd 374168404Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 375168404Spjd &child, &children) != 0) 376168404Spjd return; 377168404Spjd 378168404Spjd for (c = 0; c < children; c++) { 379185029Spjd uint64_t is_log = B_FALSE; 380185029Spjd 381185029Spjd (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 382185029Spjd &is_log); 383185029Spjd if ((is_log && !print_logs) || (!is_log && print_logs)) 384185029Spjd continue; 385185029Spjd 386219089Spjd vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 387185029Spjd print_vdev_tree(zhp, vname, child[c], indent + 2, 388185029Spjd B_FALSE); 389168404Spjd free(vname); 390168404Spjd } 391168404Spjd} 392168404Spjd 393238926Smmstatic boolean_t 394238926Smmprop_list_contains_feature(nvlist_t *proplist) 395238926Smm{ 396238926Smm nvpair_t *nvp; 397238926Smm for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp; 398238926Smm nvp = nvlist_next_nvpair(proplist, nvp)) { 399238926Smm if (zpool_prop_feature(nvpair_name(nvp))) 400238926Smm return (B_TRUE); 401238926Smm } 402238926Smm return (B_FALSE); 403238926Smm} 404238926Smm 405168404Spjd/* 406185029Spjd * Add a property pair (name, string-value) into a property nvlist. 407185029Spjd */ 408185029Spjdstatic int 409185029Spjdadd_prop_list(const char *propname, char *propval, nvlist_t **props, 410185029Spjd boolean_t poolprop) 411185029Spjd{ 412185029Spjd zpool_prop_t prop = ZPROP_INVAL; 413185029Spjd zfs_prop_t fprop; 414185029Spjd nvlist_t *proplist; 415185029Spjd const char *normnm; 416185029Spjd char *strval; 417185029Spjd 418185029Spjd if (*props == NULL && 419185029Spjd nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) { 420185029Spjd (void) fprintf(stderr, 421185029Spjd gettext("internal error: out of memory\n")); 422185029Spjd return (1); 423185029Spjd } 424185029Spjd 425185029Spjd proplist = *props; 426185029Spjd 427185029Spjd if (poolprop) { 428238926Smm const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION); 429238926Smm 430236884Smm if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL && 431236884Smm !zpool_prop_feature(propname)) { 432185029Spjd (void) fprintf(stderr, gettext("property '%s' is " 433185029Spjd "not a valid pool property\n"), propname); 434185029Spjd return (2); 435185029Spjd } 436238926Smm 437238926Smm /* 438238926Smm * feature@ properties and version should not be specified 439238926Smm * at the same time. 440238926Smm */ 441238926Smm if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) && 442238926Smm nvlist_exists(proplist, vname)) || 443238926Smm (prop == ZPOOL_PROP_VERSION && 444238926Smm prop_list_contains_feature(proplist))) { 445238926Smm (void) fprintf(stderr, gettext("'feature@' and " 446238926Smm "'version' properties cannot be specified " 447238926Smm "together\n")); 448238926Smm return (2); 449238926Smm } 450238926Smm 451238926Smm 452236884Smm if (zpool_prop_feature(propname)) 453236884Smm normnm = propname; 454236884Smm else 455236884Smm normnm = zpool_prop_to_name(prop); 456185029Spjd } else { 457209962Smm if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 458209962Smm normnm = zfs_prop_to_name(fprop); 459209962Smm } else { 460209962Smm normnm = propname; 461185029Spjd } 462185029Spjd } 463185029Spjd 464185029Spjd if (nvlist_lookup_string(proplist, normnm, &strval) == 0 && 465185029Spjd prop != ZPOOL_PROP_CACHEFILE) { 466185029Spjd (void) fprintf(stderr, gettext("property '%s' " 467185029Spjd "specified multiple times\n"), propname); 468185029Spjd return (2); 469185029Spjd } 470185029Spjd 471185029Spjd if (nvlist_add_string(proplist, normnm, propval) != 0) { 472185029Spjd (void) fprintf(stderr, gettext("internal " 473185029Spjd "error: out of memory\n")); 474185029Spjd return (1); 475185029Spjd } 476185029Spjd 477185029Spjd return (0); 478185029Spjd} 479185029Spjd 480185029Spjd/* 481168404Spjd * zpool add [-fn] <pool> <vdev> ... 482168404Spjd * 483168404Spjd * -f Force addition of devices, even if they appear in use 484168404Spjd * -n Do not add the devices, but display the resulting layout if 485168404Spjd * they were to be added. 486168404Spjd * 487168404Spjd * Adds the given vdevs to 'pool'. As with create, the bulk of this work is 488168404Spjd * handled by get_vdev_spec(), which constructs the nvlist needed to pass to 489168404Spjd * libzfs. 490168404Spjd */ 491168404Spjdint 492168404Spjdzpool_do_add(int argc, char **argv) 493168404Spjd{ 494168404Spjd boolean_t force = B_FALSE; 495168404Spjd boolean_t dryrun = B_FALSE; 496168404Spjd int c; 497168404Spjd nvlist_t *nvroot; 498168404Spjd char *poolname; 499168404Spjd int ret; 500168404Spjd zpool_handle_t *zhp; 501168404Spjd nvlist_t *config; 502168404Spjd 503168404Spjd /* check options */ 504168404Spjd while ((c = getopt(argc, argv, "fn")) != -1) { 505168404Spjd switch (c) { 506168404Spjd case 'f': 507168404Spjd force = B_TRUE; 508168404Spjd break; 509168404Spjd case 'n': 510168404Spjd dryrun = B_TRUE; 511168404Spjd break; 512168404Spjd case '?': 513168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 514168404Spjd optopt); 515168404Spjd usage(B_FALSE); 516168404Spjd } 517168404Spjd } 518168404Spjd 519168404Spjd argc -= optind; 520168404Spjd argv += optind; 521168404Spjd 522168404Spjd /* get pool name and check number of arguments */ 523168404Spjd if (argc < 1) { 524168404Spjd (void) fprintf(stderr, gettext("missing pool name argument\n")); 525168404Spjd usage(B_FALSE); 526168404Spjd } 527168404Spjd if (argc < 2) { 528168404Spjd (void) fprintf(stderr, gettext("missing vdev specification\n")); 529168404Spjd usage(B_FALSE); 530168404Spjd } 531168404Spjd 532168404Spjd poolname = argv[0]; 533168404Spjd 534168404Spjd argc--; 535168404Spjd argv++; 536168404Spjd 537168404Spjd if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 538168404Spjd return (1); 539168404Spjd 540168404Spjd if ((config = zpool_get_config(zhp, NULL)) == NULL) { 541168404Spjd (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 542168404Spjd poolname); 543168404Spjd zpool_close(zhp); 544168404Spjd return (1); 545168404Spjd } 546168404Spjd 547168404Spjd /* pass off to get_vdev_spec for processing */ 548185029Spjd nvroot = make_root_vdev(zhp, force, !force, B_FALSE, dryrun, 549185029Spjd argc, argv); 550168404Spjd if (nvroot == NULL) { 551168404Spjd zpool_close(zhp); 552168404Spjd return (1); 553168404Spjd } 554168404Spjd 555168404Spjd if (dryrun) { 556168404Spjd nvlist_t *poolnvroot; 557168404Spjd 558168404Spjd verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 559168404Spjd &poolnvroot) == 0); 560168404Spjd 561168404Spjd (void) printf(gettext("would update '%s' to the following " 562168404Spjd "configuration:\n"), zpool_get_name(zhp)); 563168404Spjd 564185029Spjd /* print original main pool and new tree */ 565185029Spjd print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE); 566185029Spjd print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE); 567168404Spjd 568185029Spjd /* Do the same for the logs */ 569185029Spjd if (num_logs(poolnvroot) > 0) { 570185029Spjd print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE); 571185029Spjd print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE); 572185029Spjd } else if (num_logs(nvroot) > 0) { 573185029Spjd print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE); 574185029Spjd } 575185029Spjd 576168404Spjd ret = 0; 577168404Spjd } else { 578168404Spjd ret = (zpool_add(zhp, nvroot) != 0); 579168404Spjd } 580168404Spjd 581168404Spjd nvlist_free(nvroot); 582168404Spjd zpool_close(zhp); 583168404Spjd 584168404Spjd return (ret); 585168404Spjd} 586168404Spjd 587168404Spjd/* 588219089Spjd * zpool remove <pool> <vdev> ... 589168404Spjd * 590219089Spjd * Removes the given vdev from the pool. Currently, this supports removing 591219089Spjd * spares, cache, and log devices from the pool. 592168404Spjd */ 593168404Spjdint 594168404Spjdzpool_do_remove(int argc, char **argv) 595168404Spjd{ 596168404Spjd char *poolname; 597185029Spjd int i, ret = 0; 598168404Spjd zpool_handle_t *zhp; 599168404Spjd 600168404Spjd argc--; 601168404Spjd argv++; 602168404Spjd 603168404Spjd /* get pool name and check number of arguments */ 604168404Spjd if (argc < 1) { 605168404Spjd (void) fprintf(stderr, gettext("missing pool name argument\n")); 606168404Spjd usage(B_FALSE); 607168404Spjd } 608168404Spjd if (argc < 2) { 609168404Spjd (void) fprintf(stderr, gettext("missing device\n")); 610168404Spjd usage(B_FALSE); 611168404Spjd } 612168404Spjd 613168404Spjd poolname = argv[0]; 614168404Spjd 615168404Spjd if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 616168404Spjd return (1); 617168404Spjd 618185029Spjd for (i = 1; i < argc; i++) { 619185029Spjd if (zpool_vdev_remove(zhp, argv[i]) != 0) 620185029Spjd ret = 1; 621168404Spjd } 622168404Spjd 623168404Spjd return (ret); 624168404Spjd} 625168404Spjd 626168404Spjd/* 627224171Sgibbs * zpool labelclear <vdev> 628224171Sgibbs * 629224171Sgibbs * Verifies that the vdev is not active and zeros out the label information 630224171Sgibbs * on the device. 631224171Sgibbs */ 632224171Sgibbsint 633224171Sgibbszpool_do_labelclear(int argc, char **argv) 634224171Sgibbs{ 635224171Sgibbs char *vdev, *name; 636224171Sgibbs int c, fd = -1, ret = 0; 637224171Sgibbs pool_state_t state; 638224171Sgibbs boolean_t inuse = B_FALSE; 639224171Sgibbs boolean_t force = B_FALSE; 640224171Sgibbs 641224171Sgibbs /* check options */ 642224171Sgibbs while ((c = getopt(argc, argv, "f")) != -1) { 643224171Sgibbs switch (c) { 644224171Sgibbs case 'f': 645224171Sgibbs force = B_TRUE; 646224171Sgibbs break; 647224171Sgibbs default: 648224171Sgibbs (void) fprintf(stderr, gettext("invalid option '%c'\n"), 649224171Sgibbs optopt); 650224171Sgibbs usage(B_FALSE); 651224171Sgibbs } 652224171Sgibbs } 653224171Sgibbs 654224171Sgibbs argc -= optind; 655224171Sgibbs argv += optind; 656224171Sgibbs 657224171Sgibbs /* get vdev name */ 658224171Sgibbs if (argc < 1) { 659224171Sgibbs (void) fprintf(stderr, gettext("missing vdev device name\n")); 660224171Sgibbs usage(B_FALSE); 661224171Sgibbs } 662224171Sgibbs 663224171Sgibbs vdev = argv[0]; 664224171Sgibbs if ((fd = open(vdev, O_RDWR)) < 0) { 665224171Sgibbs (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev); 666224171Sgibbs return (B_FALSE); 667224171Sgibbs } 668224171Sgibbs 669224171Sgibbs name = NULL; 670224171Sgibbs if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) { 671224171Sgibbs if (force) 672224171Sgibbs goto wipe_label; 673224171Sgibbs 674224171Sgibbs (void) fprintf(stderr, 675224171Sgibbs gettext("Unable to determine pool state for %s\n" 676224171Sgibbs "Use -f to force the clearing any label data\n"), vdev); 677224171Sgibbs 678224171Sgibbs return (1); 679224171Sgibbs } 680224171Sgibbs 681224171Sgibbs if (inuse) { 682224171Sgibbs switch (state) { 683224171Sgibbs default: 684224171Sgibbs case POOL_STATE_ACTIVE: 685224171Sgibbs case POOL_STATE_SPARE: 686224171Sgibbs case POOL_STATE_L2CACHE: 687224171Sgibbs (void) fprintf(stderr, 688224171Sgibbsgettext("labelclear operation failed.\n" 689224171Sgibbs "\tVdev %s is a member (%s), of pool \"%s\".\n" 690224171Sgibbs "\tTo remove label information from this device, export or destroy\n" 691224171Sgibbs "\tthe pool, or remove %s from the configuration of this pool\n" 692224171Sgibbs "\tand retry the labelclear operation\n"), 693224171Sgibbs vdev, zpool_pool_state_to_name(state), name, vdev); 694224171Sgibbs ret = 1; 695224171Sgibbs goto errout; 696224171Sgibbs 697224171Sgibbs case POOL_STATE_EXPORTED: 698224171Sgibbs if (force) 699224171Sgibbs break; 700224171Sgibbs 701224171Sgibbs (void) fprintf(stderr, 702224171Sgibbsgettext("labelclear operation failed.\n" 703224171Sgibbs "\tVdev %s is a member of the exported pool \"%s\".\n" 704224171Sgibbs "\tUse \"zpool labelclear -f %s\" to force the removal of label\n" 705224171Sgibbs "\tinformation.\n"), 706224171Sgibbs vdev, name, vdev); 707224171Sgibbs ret = 1; 708224171Sgibbs goto errout; 709224171Sgibbs 710224171Sgibbs case POOL_STATE_POTENTIALLY_ACTIVE: 711224171Sgibbs if (force) 712224171Sgibbs break; 713224171Sgibbs 714224171Sgibbs (void) fprintf(stderr, 715224171Sgibbsgettext("labelclear operation failed.\n" 716224171Sgibbs "\tVdev %s is a member of the pool \"%s\".\n" 717224171Sgibbs "\tThis pool is unknown to this system, but may be active on\n" 718224171Sgibbs "\tanother system. Use \'zpool labelclear -f %s\' to force the\n" 719224171Sgibbs "\tremoval of label information.\n"), 720224171Sgibbs vdev, name, vdev); 721224171Sgibbs ret = 1; 722224171Sgibbs goto errout; 723224171Sgibbs 724224171Sgibbs case POOL_STATE_DESTROYED: 725224171Sgibbs /* inuse should never be set for a destoryed pool... */ 726224171Sgibbs break; 727224171Sgibbs } 728224171Sgibbs } 729224171Sgibbs 730224171Sgibbswipe_label: 731224171Sgibbs if (zpool_clear_label(fd) != 0) { 732224171Sgibbs (void) fprintf(stderr, 733224171Sgibbs gettext("Label clear failed on vdev %s\n"), vdev); 734224171Sgibbs ret = 1; 735224171Sgibbs } 736224171Sgibbs 737224171Sgibbserrout: 738224171Sgibbs close(fd); 739224171Sgibbs if (name != NULL) 740224171Sgibbs free(name); 741224171Sgibbs 742224171Sgibbs return (ret); 743224171Sgibbs} 744224171Sgibbs 745224171Sgibbs/* 746236884Smm * zpool create [-fnd] [-o property=value] ... 747185029Spjd * [-O file-system-property=value] ... 748185029Spjd * [-R root] [-m mountpoint] <pool> <dev> ... 749168404Spjd * 750168404Spjd * -f Force creation, even if devices appear in use 751168404Spjd * -n Do not create the pool, but display the resulting layout if it 752168404Spjd * were to be created. 753168404Spjd * -R Create a pool under an alternate root 754168404Spjd * -m Set default mountpoint for the root dataset. By default it's 755236884Smm * '/<pool>' 756185029Spjd * -o Set property=value. 757236884Smm * -d Don't automatically enable all supported pool features 758236884Smm * (individual features can be enabled with -o). 759185029Spjd * -O Set fsproperty=value in the pool's root file system 760168404Spjd * 761168404Spjd * Creates the named pool according to the given vdev specification. The 762168404Spjd * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once 763168404Spjd * we get the nvlist back from get_vdev_spec(), we either print out the contents 764168404Spjd * (if '-n' was specified), or pass it to libzfs to do the creation. 765168404Spjd */ 766168404Spjdint 767168404Spjdzpool_do_create(int argc, char **argv) 768168404Spjd{ 769168404Spjd boolean_t force = B_FALSE; 770168404Spjd boolean_t dryrun = B_FALSE; 771236884Smm boolean_t enable_all_pool_feat = B_TRUE; 772168404Spjd int c; 773185029Spjd nvlist_t *nvroot = NULL; 774168404Spjd char *poolname; 775185029Spjd int ret = 1; 776168404Spjd char *altroot = NULL; 777168404Spjd char *mountpoint = NULL; 778185029Spjd nvlist_t *fsprops = NULL; 779185029Spjd nvlist_t *props = NULL; 780185029Spjd char *propval; 781168404Spjd 782168404Spjd /* check options */ 783236884Smm while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) { 784168404Spjd switch (c) { 785168404Spjd case 'f': 786168404Spjd force = B_TRUE; 787168404Spjd break; 788168404Spjd case 'n': 789168404Spjd dryrun = B_TRUE; 790168404Spjd break; 791236884Smm case 'd': 792236884Smm enable_all_pool_feat = B_FALSE; 793236884Smm break; 794168404Spjd case 'R': 795168404Spjd altroot = optarg; 796185029Spjd if (add_prop_list(zpool_prop_to_name( 797185029Spjd ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 798185029Spjd goto errout; 799185029Spjd if (nvlist_lookup_string(props, 800185029Spjd zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 801185029Spjd &propval) == 0) 802185029Spjd break; 803185029Spjd if (add_prop_list(zpool_prop_to_name( 804185029Spjd ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 805185029Spjd goto errout; 806168404Spjd break; 807168404Spjd case 'm': 808251634Sdelphij /* Equivalent to -O mountpoint=optarg */ 809168404Spjd mountpoint = optarg; 810168404Spjd break; 811185029Spjd case 'o': 812185029Spjd if ((propval = strchr(optarg, '=')) == NULL) { 813185029Spjd (void) fprintf(stderr, gettext("missing " 814185029Spjd "'=' for -o option\n")); 815185029Spjd goto errout; 816185029Spjd } 817185029Spjd *propval = '\0'; 818185029Spjd propval++; 819185029Spjd 820185029Spjd if (add_prop_list(optarg, propval, &props, B_TRUE)) 821185029Spjd goto errout; 822236884Smm 823236884Smm /* 824236884Smm * If the user is creating a pool that doesn't support 825236884Smm * feature flags, don't enable any features. 826236884Smm */ 827236884Smm if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) { 828236884Smm char *end; 829236884Smm u_longlong_t ver; 830236884Smm 831236884Smm ver = strtoull(propval, &end, 10); 832236884Smm if (*end == '\0' && 833236884Smm ver < SPA_VERSION_FEATURES) { 834236884Smm enable_all_pool_feat = B_FALSE; 835236884Smm } 836236884Smm } 837185029Spjd break; 838185029Spjd case 'O': 839185029Spjd if ((propval = strchr(optarg, '=')) == NULL) { 840185029Spjd (void) fprintf(stderr, gettext("missing " 841185029Spjd "'=' for -O option\n")); 842185029Spjd goto errout; 843185029Spjd } 844185029Spjd *propval = '\0'; 845185029Spjd propval++; 846185029Spjd 847251634Sdelphij /* 848251634Sdelphij * Mountpoints are checked and then added later. 849251634Sdelphij * Uniquely among properties, they can be specified 850251634Sdelphij * more than once, to avoid conflict with -m. 851251634Sdelphij */ 852251634Sdelphij if (0 == strcmp(optarg, 853251634Sdelphij zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) { 854251634Sdelphij mountpoint = propval; 855251634Sdelphij } else if (add_prop_list(optarg, propval, &fsprops, 856251634Sdelphij B_FALSE)) { 857185029Spjd goto errout; 858251634Sdelphij } 859185029Spjd break; 860168404Spjd case ':': 861168404Spjd (void) fprintf(stderr, gettext("missing argument for " 862168404Spjd "'%c' option\n"), optopt); 863185029Spjd goto badusage; 864168404Spjd case '?': 865168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 866168404Spjd optopt); 867185029Spjd goto badusage; 868168404Spjd } 869168404Spjd } 870168404Spjd 871168404Spjd argc -= optind; 872168404Spjd argv += optind; 873168404Spjd 874168404Spjd /* get pool name and check number of arguments */ 875168404Spjd if (argc < 1) { 876168404Spjd (void) fprintf(stderr, gettext("missing pool name argument\n")); 877185029Spjd goto badusage; 878168404Spjd } 879168404Spjd if (argc < 2) { 880168404Spjd (void) fprintf(stderr, gettext("missing vdev specification\n")); 881185029Spjd goto badusage; 882168404Spjd } 883168404Spjd 884168404Spjd poolname = argv[0]; 885168404Spjd 886168404Spjd /* 887168404Spjd * As a special case, check for use of '/' in the name, and direct the 888168404Spjd * user to use 'zfs create' instead. 889168404Spjd */ 890168404Spjd if (strchr(poolname, '/') != NULL) { 891168404Spjd (void) fprintf(stderr, gettext("cannot create '%s': invalid " 892168404Spjd "character '/' in pool name\n"), poolname); 893168404Spjd (void) fprintf(stderr, gettext("use 'zfs create' to " 894168404Spjd "create a dataset\n")); 895185029Spjd goto errout; 896168404Spjd } 897168404Spjd 898168404Spjd /* pass off to get_vdev_spec for bulk processing */ 899185029Spjd nvroot = make_root_vdev(NULL, force, !force, B_FALSE, dryrun, 900185029Spjd argc - 1, argv + 1); 901168404Spjd if (nvroot == NULL) 902185029Spjd goto errout; 903168404Spjd 904168404Spjd /* make_root_vdev() allows 0 toplevel children if there are spares */ 905185029Spjd if (!zfs_allocatable_devs(nvroot)) { 906168404Spjd (void) fprintf(stderr, gettext("invalid vdev " 907168404Spjd "specification: at least one toplevel vdev must be " 908168404Spjd "specified\n")); 909185029Spjd goto errout; 910168404Spjd } 911168404Spjd 912168404Spjd if (altroot != NULL && altroot[0] != '/') { 913168404Spjd (void) fprintf(stderr, gettext("invalid alternate root '%s': " 914168404Spjd "must be an absolute path\n"), altroot); 915185029Spjd goto errout; 916168404Spjd } 917168404Spjd 918168404Spjd /* 919168404Spjd * Check the validity of the mountpoint and direct the user to use the 920168404Spjd * '-m' mountpoint option if it looks like its in use. 921244857Spjd * Ignore the checks if the '-f' option is given. 922168404Spjd */ 923244857Spjd if (!force && (mountpoint == NULL || 924168404Spjd (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && 925244857Spjd strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0))) { 926168404Spjd char buf[MAXPATHLEN]; 927185029Spjd DIR *dirp; 928168404Spjd 929168404Spjd if (mountpoint && mountpoint[0] != '/') { 930168404Spjd (void) fprintf(stderr, gettext("invalid mountpoint " 931168404Spjd "'%s': must be an absolute path, 'legacy', or " 932168404Spjd "'none'\n"), mountpoint); 933185029Spjd goto errout; 934168404Spjd } 935168404Spjd 936168404Spjd if (mountpoint == NULL) { 937168404Spjd if (altroot != NULL) 938168404Spjd (void) snprintf(buf, sizeof (buf), "%s/%s", 939168404Spjd altroot, poolname); 940168404Spjd else 941168404Spjd (void) snprintf(buf, sizeof (buf), "/%s", 942168404Spjd poolname); 943168404Spjd } else { 944168404Spjd if (altroot != NULL) 945168404Spjd (void) snprintf(buf, sizeof (buf), "%s%s", 946168404Spjd altroot, mountpoint); 947168404Spjd else 948168404Spjd (void) snprintf(buf, sizeof (buf), "%s", 949168404Spjd mountpoint); 950168404Spjd } 951168404Spjd 952185029Spjd if ((dirp = opendir(buf)) == NULL && errno != ENOENT) { 953185029Spjd (void) fprintf(stderr, gettext("mountpoint '%s' : " 954185029Spjd "%s\n"), buf, strerror(errno)); 955185029Spjd (void) fprintf(stderr, gettext("use '-m' " 956185029Spjd "option to provide a different default\n")); 957185029Spjd goto errout; 958185029Spjd } else if (dirp) { 959185029Spjd int count = 0; 960185029Spjd 961185029Spjd while (count < 3 && readdir(dirp) != NULL) 962185029Spjd count++; 963185029Spjd (void) closedir(dirp); 964185029Spjd 965185029Spjd if (count > 2) { 966168404Spjd (void) fprintf(stderr, gettext("mountpoint " 967168404Spjd "'%s' exists and is not empty\n"), buf); 968185029Spjd (void) fprintf(stderr, gettext("use '-m' " 969185029Spjd "option to provide a " 970185029Spjd "different default\n")); 971185029Spjd goto errout; 972185029Spjd } 973168404Spjd } 974168404Spjd } 975168404Spjd 976251634Sdelphij /* 977251634Sdelphij * Now that the mountpoint's validity has been checked, ensure that 978251634Sdelphij * the property is set appropriately prior to creating the pool. 979251634Sdelphij */ 980251634Sdelphij if (mountpoint != NULL) { 981251634Sdelphij ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 982251634Sdelphij mountpoint, &fsprops, B_FALSE); 983251634Sdelphij if (ret != 0) 984251634Sdelphij goto errout; 985251634Sdelphij } 986251634Sdelphij 987251634Sdelphij ret = 1; 988168404Spjd if (dryrun) { 989168404Spjd /* 990168404Spjd * For a dry run invocation, print out a basic message and run 991168404Spjd * through all the vdevs in the list and print out in an 992168404Spjd * appropriate hierarchy. 993168404Spjd */ 994168404Spjd (void) printf(gettext("would create '%s' with the " 995168404Spjd "following layout:\n\n"), poolname); 996168404Spjd 997185029Spjd print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE); 998185029Spjd if (num_logs(nvroot) > 0) 999185029Spjd print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE); 1000168404Spjd 1001168404Spjd ret = 0; 1002168404Spjd } else { 1003168404Spjd /* 1004168404Spjd * Hand off to libzfs. 1005168404Spjd */ 1006236884Smm if (enable_all_pool_feat) { 1007259813Sdelphij spa_feature_t i; 1008236884Smm for (i = 0; i < SPA_FEATURES; i++) { 1009236884Smm char propname[MAXPATHLEN]; 1010236884Smm zfeature_info_t *feat = &spa_feature_table[i]; 1011236884Smm 1012236884Smm (void) snprintf(propname, sizeof (propname), 1013236884Smm "feature@%s", feat->fi_uname); 1014236884Smm 1015236884Smm /* 1016236884Smm * Skip feature if user specified it manually 1017236884Smm * on the command line. 1018236884Smm */ 1019236884Smm if (nvlist_exists(props, propname)) 1020236884Smm continue; 1021236884Smm 1022251634Sdelphij ret = add_prop_list(propname, 1023251634Sdelphij ZFS_FEATURE_ENABLED, &props, B_TRUE); 1024251634Sdelphij if (ret != 0) 1025236884Smm goto errout; 1026236884Smm } 1027236884Smm } 1028251634Sdelphij 1029251634Sdelphij ret = 1; 1030185029Spjd if (zpool_create(g_zfs, poolname, 1031185029Spjd nvroot, props, fsprops) == 0) { 1032168404Spjd zfs_handle_t *pool = zfs_open(g_zfs, poolname, 1033168404Spjd ZFS_TYPE_FILESYSTEM); 1034168404Spjd if (pool != NULL) { 1035168404Spjd if (zfs_mount(pool, NULL, 0) == 0) 1036185029Spjd ret = zfs_shareall(pool); 1037168404Spjd zfs_close(pool); 1038168404Spjd } 1039168404Spjd } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) { 1040168404Spjd (void) fprintf(stderr, gettext("pool name may have " 1041168404Spjd "been omitted\n")); 1042168404Spjd } 1043168404Spjd } 1044168404Spjd 1045185029Spjderrout: 1046168404Spjd nvlist_free(nvroot); 1047185029Spjd nvlist_free(fsprops); 1048185029Spjd nvlist_free(props); 1049168404Spjd return (ret); 1050185029Spjdbadusage: 1051185029Spjd nvlist_free(fsprops); 1052185029Spjd nvlist_free(props); 1053185029Spjd usage(B_FALSE); 1054185029Spjd return (2); 1055168404Spjd} 1056168404Spjd 1057168404Spjd/* 1058168404Spjd * zpool destroy <pool> 1059168404Spjd * 1060168404Spjd * -f Forcefully unmount any datasets 1061168404Spjd * 1062168404Spjd * Destroy the given pool. Automatically unmounts any datasets in the pool. 1063168404Spjd */ 1064168404Spjdint 1065168404Spjdzpool_do_destroy(int argc, char **argv) 1066168404Spjd{ 1067168404Spjd boolean_t force = B_FALSE; 1068168404Spjd int c; 1069168404Spjd char *pool; 1070168404Spjd zpool_handle_t *zhp; 1071168404Spjd int ret; 1072168404Spjd 1073168404Spjd /* check options */ 1074168404Spjd while ((c = getopt(argc, argv, "f")) != -1) { 1075168404Spjd switch (c) { 1076168404Spjd case 'f': 1077168404Spjd force = B_TRUE; 1078168404Spjd break; 1079168404Spjd case '?': 1080168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1081168404Spjd optopt); 1082168404Spjd usage(B_FALSE); 1083168404Spjd } 1084168404Spjd } 1085168404Spjd 1086168404Spjd argc -= optind; 1087168404Spjd argv += optind; 1088168404Spjd 1089168404Spjd /* check arguments */ 1090168404Spjd if (argc < 1) { 1091168404Spjd (void) fprintf(stderr, gettext("missing pool argument\n")); 1092168404Spjd usage(B_FALSE); 1093168404Spjd } 1094168404Spjd if (argc > 1) { 1095168404Spjd (void) fprintf(stderr, gettext("too many arguments\n")); 1096168404Spjd usage(B_FALSE); 1097168404Spjd } 1098168404Spjd 1099168404Spjd pool = argv[0]; 1100168404Spjd 1101168404Spjd if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 1102168404Spjd /* 1103168404Spjd * As a special case, check for use of '/' in the name, and 1104168404Spjd * direct the user to use 'zfs destroy' instead. 1105168404Spjd */ 1106168404Spjd if (strchr(pool, '/') != NULL) 1107168404Spjd (void) fprintf(stderr, gettext("use 'zfs destroy' to " 1108168404Spjd "destroy a dataset\n")); 1109168404Spjd return (1); 1110168404Spjd } 1111168404Spjd 1112168404Spjd if (zpool_disable_datasets(zhp, force) != 0) { 1113168404Spjd (void) fprintf(stderr, gettext("could not destroy '%s': " 1114168404Spjd "could not unmount datasets\n"), zpool_get_name(zhp)); 1115168404Spjd return (1); 1116168404Spjd } 1117168404Spjd 1118248571Smm /* The history must be logged as part of the export */ 1119248571Smm log_history = B_FALSE; 1120168404Spjd 1121248571Smm ret = (zpool_destroy(zhp, history_str) != 0); 1122248571Smm 1123168404Spjd zpool_close(zhp); 1124168404Spjd 1125168404Spjd return (ret); 1126168404Spjd} 1127168404Spjd 1128168404Spjd/* 1129168404Spjd * zpool export [-f] <pool> ... 1130168404Spjd * 1131168404Spjd * -f Forcefully unmount datasets 1132168404Spjd * 1133168404Spjd * Export the given pools. By default, the command will attempt to cleanly 1134168404Spjd * unmount any active datasets within the pool. If the '-f' flag is specified, 1135168404Spjd * then the datasets will be forcefully unmounted. 1136168404Spjd */ 1137168404Spjdint 1138168404Spjdzpool_do_export(int argc, char **argv) 1139168404Spjd{ 1140168404Spjd boolean_t force = B_FALSE; 1141207670Smm boolean_t hardforce = B_FALSE; 1142168404Spjd int c; 1143168404Spjd zpool_handle_t *zhp; 1144168404Spjd int ret; 1145168404Spjd int i; 1146168404Spjd 1147168404Spjd /* check options */ 1148207670Smm while ((c = getopt(argc, argv, "fF")) != -1) { 1149168404Spjd switch (c) { 1150168404Spjd case 'f': 1151168404Spjd force = B_TRUE; 1152168404Spjd break; 1153207670Smm case 'F': 1154207670Smm hardforce = B_TRUE; 1155207670Smm break; 1156168404Spjd case '?': 1157168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1158168404Spjd optopt); 1159168404Spjd usage(B_FALSE); 1160168404Spjd } 1161168404Spjd } 1162168404Spjd 1163168404Spjd argc -= optind; 1164168404Spjd argv += optind; 1165168404Spjd 1166168404Spjd /* check arguments */ 1167168404Spjd if (argc < 1) { 1168168404Spjd (void) fprintf(stderr, gettext("missing pool argument\n")); 1169168404Spjd usage(B_FALSE); 1170168404Spjd } 1171168404Spjd 1172168404Spjd ret = 0; 1173168404Spjd for (i = 0; i < argc; i++) { 1174168404Spjd if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) { 1175168404Spjd ret = 1; 1176168404Spjd continue; 1177168404Spjd } 1178168404Spjd 1179168404Spjd if (zpool_disable_datasets(zhp, force) != 0) { 1180168404Spjd ret = 1; 1181168404Spjd zpool_close(zhp); 1182168404Spjd continue; 1183168404Spjd } 1184168404Spjd 1185248571Smm /* The history must be logged as part of the export */ 1186248571Smm log_history = B_FALSE; 1187248571Smm 1188207670Smm if (hardforce) { 1189248571Smm if (zpool_export_force(zhp, history_str) != 0) 1190207670Smm ret = 1; 1191248571Smm } else if (zpool_export(zhp, force, history_str) != 0) { 1192168404Spjd ret = 1; 1193207670Smm } 1194168404Spjd 1195168404Spjd zpool_close(zhp); 1196168404Spjd } 1197168404Spjd 1198168404Spjd return (ret); 1199168404Spjd} 1200168404Spjd 1201168404Spjd/* 1202168404Spjd * Given a vdev configuration, determine the maximum width needed for the device 1203168404Spjd * name column. 1204168404Spjd */ 1205168404Spjdstatic int 1206168404Spjdmax_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max) 1207168404Spjd{ 1208219089Spjd char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE); 1209168404Spjd nvlist_t **child; 1210168404Spjd uint_t c, children; 1211168404Spjd int ret; 1212168404Spjd 1213168404Spjd if (strlen(name) + depth > max) 1214168404Spjd max = strlen(name) + depth; 1215168404Spjd 1216168404Spjd free(name); 1217168404Spjd 1218168404Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1219168404Spjd &child, &children) == 0) { 1220168404Spjd for (c = 0; c < children; c++) 1221168404Spjd if ((ret = max_width(zhp, child[c], depth + 2, 1222168404Spjd max)) > max) 1223168404Spjd max = ret; 1224168404Spjd } 1225168404Spjd 1226185029Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1227185029Spjd &child, &children) == 0) { 1228185029Spjd for (c = 0; c < children; c++) 1229185029Spjd if ((ret = max_width(zhp, child[c], depth + 2, 1230185029Spjd max)) > max) 1231185029Spjd max = ret; 1232185029Spjd } 1233185029Spjd 1234168404Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1235168404Spjd &child, &children) == 0) { 1236168404Spjd for (c = 0; c < children; c++) 1237168404Spjd if ((ret = max_width(zhp, child[c], depth + 2, 1238168404Spjd max)) > max) 1239168404Spjd max = ret; 1240168404Spjd } 1241168404Spjd 1242168404Spjd 1243168404Spjd return (max); 1244168404Spjd} 1245168404Spjd 1246213197Smmtypedef struct spare_cbdata { 1247213197Smm uint64_t cb_guid; 1248213197Smm zpool_handle_t *cb_zhp; 1249213197Smm} spare_cbdata_t; 1250168404Spjd 1251213197Smmstatic boolean_t 1252213197Smmfind_vdev(nvlist_t *nv, uint64_t search) 1253213197Smm{ 1254213197Smm uint64_t guid; 1255213197Smm nvlist_t **child; 1256213197Smm uint_t c, children; 1257213197Smm 1258213197Smm if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 && 1259213197Smm search == guid) 1260213197Smm return (B_TRUE); 1261213197Smm 1262213197Smm if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1263213197Smm &child, &children) == 0) { 1264213197Smm for (c = 0; c < children; c++) 1265213197Smm if (find_vdev(child[c], search)) 1266213197Smm return (B_TRUE); 1267213197Smm } 1268213197Smm 1269213197Smm return (B_FALSE); 1270213197Smm} 1271213197Smm 1272213197Smmstatic int 1273213197Smmfind_spare(zpool_handle_t *zhp, void *data) 1274213197Smm{ 1275213197Smm spare_cbdata_t *cbp = data; 1276213197Smm nvlist_t *config, *nvroot; 1277213197Smm 1278213197Smm config = zpool_get_config(zhp, NULL); 1279213197Smm verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1280213197Smm &nvroot) == 0); 1281213197Smm 1282213197Smm if (find_vdev(nvroot, cbp->cb_guid)) { 1283213197Smm cbp->cb_zhp = zhp; 1284213197Smm return (1); 1285213197Smm } 1286213197Smm 1287213197Smm zpool_close(zhp); 1288213197Smm return (0); 1289213197Smm} 1290213197Smm 1291168404Spjd/* 1292213197Smm * Print out configuration state as requested by status_callback. 1293213197Smm */ 1294213197Smmvoid 1295213197Smmprint_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 1296213197Smm int namewidth, int depth, boolean_t isspare) 1297213197Smm{ 1298213197Smm nvlist_t **child; 1299254591Sgibbs uint_t c, vsc, children; 1300219089Spjd pool_scan_stat_t *ps = NULL; 1301213197Smm vdev_stat_t *vs; 1302219089Spjd char rbuf[6], wbuf[6], cbuf[6]; 1303213197Smm char *vname; 1304213197Smm uint64_t notpresent; 1305254591Sgibbs uint64_t ashift; 1306213197Smm spare_cbdata_t cb; 1307224169Sgibbs const char *state; 1308213197Smm 1309213197Smm if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1310213197Smm &child, &children) != 0) 1311213197Smm children = 0; 1312213197Smm 1313219089Spjd verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 1314254591Sgibbs (uint64_t **)&vs, &vsc) == 0); 1315219089Spjd 1316213197Smm state = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1317213197Smm if (isspare) { 1318213197Smm /* 1319213197Smm * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for 1320213197Smm * online drives. 1321213197Smm */ 1322213197Smm if (vs->vs_aux == VDEV_AUX_SPARED) 1323213197Smm state = "INUSE"; 1324213197Smm else if (vs->vs_state == VDEV_STATE_HEALTHY) 1325213197Smm state = "AVAIL"; 1326213197Smm } 1327213197Smm 1328213197Smm (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth, 1329213197Smm name, state); 1330213197Smm 1331213197Smm if (!isspare) { 1332213197Smm zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf)); 1333213197Smm zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf)); 1334213197Smm zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf)); 1335213197Smm (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf); 1336213197Smm } 1337213197Smm 1338213197Smm if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1339224170Sgibbs ¬present) == 0 || 1340224170Sgibbs vs->vs_state <= VDEV_STATE_CANT_OPEN) { 1341213197Smm char *path; 1342224170Sgibbs if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) 1343224170Sgibbs (void) printf(" was %s", path); 1344213197Smm } else if (vs->vs_aux != 0) { 1345213197Smm (void) printf(" "); 1346213197Smm 1347213197Smm switch (vs->vs_aux) { 1348213197Smm case VDEV_AUX_OPEN_FAILED: 1349213197Smm (void) printf(gettext("cannot open")); 1350213197Smm break; 1351213197Smm 1352213197Smm case VDEV_AUX_BAD_GUID_SUM: 1353213197Smm (void) printf(gettext("missing device")); 1354213197Smm break; 1355213197Smm 1356213197Smm case VDEV_AUX_NO_REPLICAS: 1357213197Smm (void) printf(gettext("insufficient replicas")); 1358213197Smm break; 1359213197Smm 1360213197Smm case VDEV_AUX_VERSION_NEWER: 1361213197Smm (void) printf(gettext("newer version")); 1362213197Smm break; 1363213197Smm 1364236884Smm case VDEV_AUX_UNSUP_FEAT: 1365236884Smm (void) printf(gettext("unsupported feature(s)")); 1366236884Smm break; 1367236884Smm 1368254591Sgibbs case VDEV_AUX_ASHIFT_TOO_BIG: 1369254591Sgibbs (void) printf(gettext("unsupported minimum blocksize")); 1370254591Sgibbs break; 1371254591Sgibbs 1372213197Smm case VDEV_AUX_SPARED: 1373213197Smm verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1374213197Smm &cb.cb_guid) == 0); 1375213197Smm if (zpool_iter(g_zfs, find_spare, &cb) == 1) { 1376213197Smm if (strcmp(zpool_get_name(cb.cb_zhp), 1377213197Smm zpool_get_name(zhp)) == 0) 1378213197Smm (void) printf(gettext("currently in " 1379213197Smm "use")); 1380213197Smm else 1381213197Smm (void) printf(gettext("in use by " 1382213197Smm "pool '%s'"), 1383213197Smm zpool_get_name(cb.cb_zhp)); 1384213197Smm zpool_close(cb.cb_zhp); 1385213197Smm } else { 1386213197Smm (void) printf(gettext("currently in use")); 1387213197Smm } 1388213197Smm break; 1389213197Smm 1390213197Smm case VDEV_AUX_ERR_EXCEEDED: 1391213197Smm (void) printf(gettext("too many errors")); 1392213197Smm break; 1393213197Smm 1394213197Smm case VDEV_AUX_IO_FAILURE: 1395213197Smm (void) printf(gettext("experienced I/O failures")); 1396213197Smm break; 1397213197Smm 1398213197Smm case VDEV_AUX_BAD_LOG: 1399213197Smm (void) printf(gettext("bad intent log")); 1400213197Smm break; 1401213197Smm 1402219089Spjd case VDEV_AUX_EXTERNAL: 1403219089Spjd (void) printf(gettext("external device fault")); 1404219089Spjd break; 1405219089Spjd 1406219089Spjd case VDEV_AUX_SPLIT_POOL: 1407219089Spjd (void) printf(gettext("split into new pool")); 1408219089Spjd break; 1409219089Spjd 1410213197Smm default: 1411213197Smm (void) printf(gettext("corrupted data")); 1412213197Smm break; 1413213197Smm } 1414254591Sgibbs } else if (children == 0 && !isspare && 1415254591Sgibbs VDEV_STAT_VALID(vs_physical_ashift, vsc) && 1416254591Sgibbs vs->vs_configured_ashift < vs->vs_physical_ashift) { 1417254591Sgibbs (void) printf( 1418254591Sgibbs gettext(" block size: %dB configured, %dB native"), 1419254591Sgibbs 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift); 1420213197Smm } 1421213197Smm 1422219089Spjd (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS, 1423219089Spjd (uint64_t **)&ps, &c); 1424219089Spjd 1425219089Spjd if (ps && ps->pss_state == DSS_SCANNING && 1426219089Spjd vs->vs_scan_processed != 0 && children == 0) { 1427219089Spjd (void) printf(gettext(" (%s)"), 1428219089Spjd (ps->pss_func == POOL_SCAN_RESILVER) ? 1429219089Spjd "resilvering" : "repairing"); 1430219089Spjd } 1431219089Spjd 1432213197Smm (void) printf("\n"); 1433213197Smm 1434213197Smm for (c = 0; c < children; c++) { 1435219089Spjd uint64_t islog = B_FALSE, ishole = B_FALSE; 1436213197Smm 1437219089Spjd /* Don't print logs or holes here */ 1438213197Smm (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1439219089Spjd &islog); 1440219089Spjd (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 1441219089Spjd &ishole); 1442219089Spjd if (islog || ishole) 1443213197Smm continue; 1444219089Spjd vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1445213197Smm print_status_config(zhp, vname, child[c], 1446213197Smm namewidth, depth + 2, isspare); 1447213197Smm free(vname); 1448213197Smm } 1449213197Smm} 1450213197Smm 1451213197Smm 1452213197Smm/* 1453168404Spjd * Print the configuration of an exported pool. Iterate over all vdevs in the 1454168404Spjd * pool, printing out the name and status for each one. 1455168404Spjd */ 1456168404Spjdvoid 1457213197Smmprint_import_config(const char *name, nvlist_t *nv, int namewidth, int depth) 1458168404Spjd{ 1459168404Spjd nvlist_t **child; 1460168404Spjd uint_t c, children; 1461168404Spjd vdev_stat_t *vs; 1462168404Spjd char *type, *vname; 1463168404Spjd 1464168404Spjd verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 1465219089Spjd if (strcmp(type, VDEV_TYPE_MISSING) == 0 || 1466219089Spjd strcmp(type, VDEV_TYPE_HOLE) == 0) 1467168404Spjd return; 1468168404Spjd 1469219089Spjd verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 1470168404Spjd (uint64_t **)&vs, &c) == 0); 1471168404Spjd 1472168404Spjd (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name); 1473185029Spjd (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux)); 1474168404Spjd 1475168404Spjd if (vs->vs_aux != 0) { 1476185029Spjd (void) printf(" "); 1477168404Spjd 1478168404Spjd switch (vs->vs_aux) { 1479168404Spjd case VDEV_AUX_OPEN_FAILED: 1480168404Spjd (void) printf(gettext("cannot open")); 1481168404Spjd break; 1482168404Spjd 1483168404Spjd case VDEV_AUX_BAD_GUID_SUM: 1484168404Spjd (void) printf(gettext("missing device")); 1485168404Spjd break; 1486168404Spjd 1487168404Spjd case VDEV_AUX_NO_REPLICAS: 1488168404Spjd (void) printf(gettext("insufficient replicas")); 1489168404Spjd break; 1490168404Spjd 1491168404Spjd case VDEV_AUX_VERSION_NEWER: 1492168404Spjd (void) printf(gettext("newer version")); 1493168404Spjd break; 1494168404Spjd 1495236884Smm case VDEV_AUX_UNSUP_FEAT: 1496236884Smm (void) printf(gettext("unsupported feature(s)")); 1497236884Smm break; 1498236884Smm 1499185029Spjd case VDEV_AUX_ERR_EXCEEDED: 1500185029Spjd (void) printf(gettext("too many errors")); 1501185029Spjd break; 1502185029Spjd 1503168404Spjd default: 1504168404Spjd (void) printf(gettext("corrupted data")); 1505168404Spjd break; 1506168404Spjd } 1507168404Spjd } 1508168404Spjd (void) printf("\n"); 1509168404Spjd 1510168404Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1511168404Spjd &child, &children) != 0) 1512168404Spjd return; 1513168404Spjd 1514168404Spjd for (c = 0; c < children; c++) { 1515185029Spjd uint64_t is_log = B_FALSE; 1516185029Spjd 1517185029Spjd (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1518185029Spjd &is_log); 1519213197Smm if (is_log) 1520185029Spjd continue; 1521185029Spjd 1522219089Spjd vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE); 1523213197Smm print_import_config(vname, child[c], namewidth, depth + 2); 1524168404Spjd free(vname); 1525168404Spjd } 1526168404Spjd 1527185029Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 1528185029Spjd &child, &children) == 0) { 1529185029Spjd (void) printf(gettext("\tcache\n")); 1530185029Spjd for (c = 0; c < children; c++) { 1531219089Spjd vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 1532185029Spjd (void) printf("\t %s\n", vname); 1533185029Spjd free(vname); 1534185029Spjd } 1535185029Spjd } 1536185029Spjd 1537168404Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 1538185029Spjd &child, &children) == 0) { 1539185029Spjd (void) printf(gettext("\tspares\n")); 1540185029Spjd for (c = 0; c < children; c++) { 1541219089Spjd vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE); 1542185029Spjd (void) printf("\t %s\n", vname); 1543185029Spjd free(vname); 1544185029Spjd } 1545168404Spjd } 1546168404Spjd} 1547168404Spjd 1548168404Spjd/* 1549213197Smm * Print log vdevs. 1550213197Smm * Logs are recorded as top level vdevs in the main pool child array 1551213197Smm * but with "is_log" set to 1. We use either print_status_config() or 1552213197Smm * print_import_config() to print the top level logs then any log 1553213197Smm * children (eg mirrored slogs) are printed recursively - which 1554213197Smm * works because only the top level vdev is marked "is_log" 1555213197Smm */ 1556213197Smmstatic void 1557213197Smmprint_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose) 1558213197Smm{ 1559213197Smm uint_t c, children; 1560213197Smm nvlist_t **child; 1561213197Smm 1562213197Smm if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, 1563213197Smm &children) != 0) 1564213197Smm return; 1565213197Smm 1566213197Smm (void) printf(gettext("\tlogs\n")); 1567213197Smm 1568213197Smm for (c = 0; c < children; c++) { 1569213197Smm uint64_t is_log = B_FALSE; 1570213197Smm char *name; 1571213197Smm 1572213197Smm (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 1573213197Smm &is_log); 1574213197Smm if (!is_log) 1575213197Smm continue; 1576219089Spjd name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE); 1577213197Smm if (verbose) 1578213197Smm print_status_config(zhp, name, child[c], namewidth, 1579213197Smm 2, B_FALSE); 1580213197Smm else 1581213197Smm print_import_config(name, child[c], namewidth, 2); 1582213197Smm free(name); 1583213197Smm } 1584213197Smm} 1585219089Spjd 1586213197Smm/* 1587168404Spjd * Display the status for the given pool. 1588168404Spjd */ 1589168404Spjdstatic void 1590168404Spjdshow_import(nvlist_t *config) 1591168404Spjd{ 1592168404Spjd uint64_t pool_state; 1593168404Spjd vdev_stat_t *vs; 1594168404Spjd char *name; 1595168404Spjd uint64_t guid; 1596168404Spjd char *msgid; 1597168404Spjd nvlist_t *nvroot; 1598168404Spjd int reason; 1599168404Spjd const char *health; 1600168404Spjd uint_t vsc; 1601168404Spjd int namewidth; 1602228103Smm char *comment; 1603168404Spjd 1604168404Spjd verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1605168404Spjd &name) == 0); 1606168404Spjd verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1607168404Spjd &guid) == 0); 1608168404Spjd verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1609168404Spjd &pool_state) == 0); 1610168404Spjd verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1611168404Spjd &nvroot) == 0); 1612168404Spjd 1613219089Spjd verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 1614168404Spjd (uint64_t **)&vs, &vsc) == 0); 1615185029Spjd health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 1616168404Spjd 1617168404Spjd reason = zpool_import_status(config, &msgid); 1618168404Spjd 1619228103Smm (void) printf(gettext(" pool: %s\n"), name); 1620228103Smm (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid); 1621228103Smm (void) printf(gettext(" state: %s"), health); 1622168404Spjd if (pool_state == POOL_STATE_DESTROYED) 1623168404Spjd (void) printf(gettext(" (DESTROYED)")); 1624168404Spjd (void) printf("\n"); 1625168404Spjd 1626168404Spjd switch (reason) { 1627168404Spjd case ZPOOL_STATUS_MISSING_DEV_R: 1628168404Spjd case ZPOOL_STATUS_MISSING_DEV_NR: 1629168404Spjd case ZPOOL_STATUS_BAD_GUID_SUM: 1630228103Smm (void) printf(gettext(" status: One or more devices are " 1631228103Smm "missing from the system.\n")); 1632168404Spjd break; 1633168404Spjd 1634168404Spjd case ZPOOL_STATUS_CORRUPT_LABEL_R: 1635168404Spjd case ZPOOL_STATUS_CORRUPT_LABEL_NR: 1636228103Smm (void) printf(gettext(" status: One or more devices contains " 1637168404Spjd "corrupted data.\n")); 1638168404Spjd break; 1639168404Spjd 1640168404Spjd case ZPOOL_STATUS_CORRUPT_DATA: 1641228103Smm (void) printf( 1642228103Smm gettext(" status: The pool data is corrupted.\n")); 1643168404Spjd break; 1644168404Spjd 1645168404Spjd case ZPOOL_STATUS_OFFLINE_DEV: 1646228103Smm (void) printf(gettext(" status: One or more devices " 1647168404Spjd "are offlined.\n")); 1648168404Spjd break; 1649168404Spjd 1650168404Spjd case ZPOOL_STATUS_CORRUPT_POOL: 1651228103Smm (void) printf(gettext(" status: The pool metadata is " 1652168404Spjd "corrupted.\n")); 1653168404Spjd break; 1654168404Spjd 1655168404Spjd case ZPOOL_STATUS_VERSION_OLDER: 1656238926Smm (void) printf(gettext(" status: The pool is formatted using a " 1657238926Smm "legacy on-disk version.\n")); 1658168404Spjd break; 1659168404Spjd 1660168404Spjd case ZPOOL_STATUS_VERSION_NEWER: 1661228103Smm (void) printf(gettext(" status: The pool is formatted using an " 1662168404Spjd "incompatible version.\n")); 1663168404Spjd break; 1664168404Spjd 1665238926Smm case ZPOOL_STATUS_FEAT_DISABLED: 1666238926Smm (void) printf(gettext(" status: Some supported features are " 1667238926Smm "not enabled on the pool.\n")); 1668238926Smm break; 1669238926Smm 1670236884Smm case ZPOOL_STATUS_UNSUP_FEAT_READ: 1671236884Smm (void) printf(gettext("status: The pool uses the following " 1672236884Smm "feature(s) not supported on this sytem:\n")); 1673236884Smm zpool_print_unsup_feat(config); 1674236884Smm break; 1675236884Smm 1676236884Smm case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1677236884Smm (void) printf(gettext("status: The pool can only be accessed " 1678236884Smm "in read-only mode on this system. It\n\tcannot be " 1679236884Smm "accessed in read-write mode because it uses the " 1680236884Smm "following\n\tfeature(s) not supported on this system:\n")); 1681236884Smm zpool_print_unsup_feat(config); 1682236884Smm break; 1683236884Smm 1684168498Spjd case ZPOOL_STATUS_HOSTID_MISMATCH: 1685228103Smm (void) printf(gettext(" status: The pool was last accessed by " 1686168498Spjd "another system.\n")); 1687168498Spjd break; 1688185029Spjd 1689185029Spjd case ZPOOL_STATUS_FAULTED_DEV_R: 1690185029Spjd case ZPOOL_STATUS_FAULTED_DEV_NR: 1691228103Smm (void) printf(gettext(" status: One or more devices are " 1692185029Spjd "faulted.\n")); 1693185029Spjd break; 1694185029Spjd 1695185029Spjd case ZPOOL_STATUS_BAD_LOG: 1696228103Smm (void) printf(gettext(" status: An intent log record cannot be " 1697185029Spjd "read.\n")); 1698185029Spjd break; 1699185029Spjd 1700219089Spjd case ZPOOL_STATUS_RESILVERING: 1701228103Smm (void) printf(gettext(" status: One or more devices were being " 1702219089Spjd "resilvered.\n")); 1703219089Spjd break; 1704219089Spjd 1705259131Sdelphij case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 1706259131Sdelphij (void) printf(gettext("status: One or more devices were " 1707259131Sdelphij "configured to use a non-native block size.\n" 1708259131Sdelphij "\tExpect reduced performance.\n")); 1709259131Sdelphij break; 1710259131Sdelphij 1711168404Spjd default: 1712168404Spjd /* 1713168404Spjd * No other status can be seen when importing pools. 1714168404Spjd */ 1715168404Spjd assert(reason == ZPOOL_STATUS_OK); 1716168404Spjd } 1717168404Spjd 1718168404Spjd /* 1719168404Spjd * Print out an action according to the overall state of the pool. 1720168404Spjd */ 1721168404Spjd if (vs->vs_state == VDEV_STATE_HEALTHY) { 1722238926Smm if (reason == ZPOOL_STATUS_VERSION_OLDER || 1723238926Smm reason == ZPOOL_STATUS_FEAT_DISABLED) { 1724228103Smm (void) printf(gettext(" action: The pool can be " 1725168404Spjd "imported using its name or numeric identifier, " 1726168404Spjd "though\n\tsome features will not be available " 1727168404Spjd "without an explicit 'zpool upgrade'.\n")); 1728238926Smm } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) { 1729228103Smm (void) printf(gettext(" action: The pool can be " 1730168498Spjd "imported using its name or numeric " 1731168498Spjd "identifier and\n\tthe '-f' flag.\n")); 1732238926Smm } else { 1733228103Smm (void) printf(gettext(" action: The pool can be " 1734168404Spjd "imported using its name or numeric " 1735168404Spjd "identifier.\n")); 1736238926Smm } 1737168404Spjd } else if (vs->vs_state == VDEV_STATE_DEGRADED) { 1738228103Smm (void) printf(gettext(" action: The pool can be imported " 1739168404Spjd "despite missing or damaged devices. The\n\tfault " 1740168404Spjd "tolerance of the pool may be compromised if imported.\n")); 1741168404Spjd } else { 1742168404Spjd switch (reason) { 1743168404Spjd case ZPOOL_STATUS_VERSION_NEWER: 1744228103Smm (void) printf(gettext(" action: The pool cannot be " 1745168404Spjd "imported. Access the pool on a system running " 1746168404Spjd "newer\n\tsoftware, or recreate the pool from " 1747168404Spjd "backup.\n")); 1748168404Spjd break; 1749236884Smm case ZPOOL_STATUS_UNSUP_FEAT_READ: 1750236884Smm (void) printf(gettext("action: The pool cannot be " 1751236884Smm "imported. Access the pool on a system that " 1752236884Smm "supports\n\tthe required feature(s), or recreate " 1753236884Smm "the pool from backup.\n")); 1754236884Smm break; 1755236884Smm case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 1756236884Smm (void) printf(gettext("action: The pool cannot be " 1757236884Smm "imported in read-write mode. Import the pool " 1758236884Smm "with\n" 1759236884Smm "\t\"-o readonly=on\", access the pool on a system " 1760236884Smm "that supports the\n\trequired feature(s), or " 1761236884Smm "recreate the pool from backup.\n")); 1762236884Smm break; 1763168404Spjd case ZPOOL_STATUS_MISSING_DEV_R: 1764168404Spjd case ZPOOL_STATUS_MISSING_DEV_NR: 1765168404Spjd case ZPOOL_STATUS_BAD_GUID_SUM: 1766228103Smm (void) printf(gettext(" action: The pool cannot be " 1767168404Spjd "imported. Attach the missing\n\tdevices and try " 1768168404Spjd "again.\n")); 1769168404Spjd break; 1770168404Spjd default: 1771228103Smm (void) printf(gettext(" action: The pool cannot be " 1772168404Spjd "imported due to damaged devices or data.\n")); 1773168404Spjd } 1774168404Spjd } 1775168404Spjd 1776228103Smm /* Print the comment attached to the pool. */ 1777228103Smm if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 1778228103Smm (void) printf(gettext("comment: %s\n"), comment); 1779228103Smm 1780168404Spjd /* 1781168404Spjd * If the state is "closed" or "can't open", and the aux state 1782168404Spjd * is "corrupt data": 1783168404Spjd */ 1784168404Spjd if (((vs->vs_state == VDEV_STATE_CLOSED) || 1785168404Spjd (vs->vs_state == VDEV_STATE_CANT_OPEN)) && 1786168404Spjd (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) { 1787168404Spjd if (pool_state == POOL_STATE_DESTROYED) 1788168404Spjd (void) printf(gettext("\tThe pool was destroyed, " 1789168404Spjd "but can be imported using the '-Df' flags.\n")); 1790168404Spjd else if (pool_state != POOL_STATE_EXPORTED) 1791168404Spjd (void) printf(gettext("\tThe pool may be active on " 1792185029Spjd "another system, but can be imported using\n\t" 1793168404Spjd "the '-f' flag.\n")); 1794168404Spjd } 1795168404Spjd 1796168404Spjd if (msgid != NULL) 1797236146Smm (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 1798168404Spjd msgid); 1799168404Spjd 1800228103Smm (void) printf(gettext(" config:\n\n")); 1801168404Spjd 1802168404Spjd namewidth = max_width(NULL, nvroot, 0, 0); 1803168404Spjd if (namewidth < 10) 1804168404Spjd namewidth = 10; 1805168404Spjd 1806213197Smm print_import_config(name, nvroot, namewidth, 0); 1807213197Smm if (num_logs(nvroot) > 0) 1808213197Smm print_logs(NULL, nvroot, namewidth, B_FALSE); 1809185029Spjd 1810168404Spjd if (reason == ZPOOL_STATUS_BAD_GUID_SUM) { 1811168404Spjd (void) printf(gettext("\n\tAdditional devices are known to " 1812168404Spjd "be part of this pool, though their\n\texact " 1813168404Spjd "configuration cannot be determined.\n")); 1814168404Spjd } 1815168404Spjd} 1816168404Spjd 1817168404Spjd/* 1818168404Spjd * Perform the import for the given configuration. This passes the heavy 1819185029Spjd * lifting off to zpool_import_props(), and then mounts the datasets contained 1820185029Spjd * within the pool. 1821168404Spjd */ 1822168404Spjdstatic int 1823168404Spjddo_import(nvlist_t *config, const char *newname, const char *mntopts, 1824219089Spjd nvlist_t *props, int flags) 1825168404Spjd{ 1826168404Spjd zpool_handle_t *zhp; 1827168404Spjd char *name; 1828168404Spjd uint64_t state; 1829168404Spjd uint64_t version; 1830168404Spjd 1831168404Spjd verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1832168404Spjd &name) == 0); 1833168404Spjd 1834168404Spjd verify(nvlist_lookup_uint64(config, 1835168404Spjd ZPOOL_CONFIG_POOL_STATE, &state) == 0); 1836168404Spjd verify(nvlist_lookup_uint64(config, 1837168404Spjd ZPOOL_CONFIG_VERSION, &version) == 0); 1838236884Smm if (!SPA_VERSION_IS_SUPPORTED(version)) { 1839168404Spjd (void) fprintf(stderr, gettext("cannot import '%s': pool " 1840236884Smm "is formatted using an unsupported ZFS version\n"), name); 1841168404Spjd return (1); 1842219089Spjd } else if (state != POOL_STATE_EXPORTED && 1843219089Spjd !(flags & ZFS_IMPORT_ANY_HOST)) { 1844168498Spjd uint64_t hostid; 1845168498Spjd 1846168498Spjd if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, 1847168498Spjd &hostid) == 0) { 1848168498Spjd if ((unsigned long)hostid != gethostid()) { 1849168498Spjd char *hostname; 1850168498Spjd uint64_t timestamp; 1851168498Spjd time_t t; 1852168498Spjd 1853168498Spjd verify(nvlist_lookup_string(config, 1854168498Spjd ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 1855168498Spjd verify(nvlist_lookup_uint64(config, 1856168498Spjd ZPOOL_CONFIG_TIMESTAMP, ×tamp) == 0); 1857168498Spjd t = timestamp; 1858168498Spjd (void) fprintf(stderr, gettext("cannot import " 1859168498Spjd "'%s': pool may be in use from other " 1860168498Spjd "system, it was last accessed by %s " 1861168498Spjd "(hostid: 0x%lx) on %s"), name, hostname, 1862168498Spjd (unsigned long)hostid, 1863168498Spjd asctime(localtime(&t))); 1864168498Spjd (void) fprintf(stderr, gettext("use '-f' to " 1865168498Spjd "import anyway\n")); 1866168498Spjd return (1); 1867168498Spjd } 1868168498Spjd } else { 1869168498Spjd (void) fprintf(stderr, gettext("cannot import '%s': " 1870168498Spjd "pool may be in use from other system\n"), name); 1871168498Spjd (void) fprintf(stderr, gettext("use '-f' to import " 1872168498Spjd "anyway\n")); 1873168498Spjd return (1); 1874168498Spjd } 1875168404Spjd } 1876168404Spjd 1877219089Spjd if (zpool_import_props(g_zfs, config, newname, props, flags) != 0) 1878168404Spjd return (1); 1879168404Spjd 1880168404Spjd if (newname != NULL) 1881168404Spjd name = (char *)newname; 1882168404Spjd 1883209962Smm if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL) 1884209962Smm return (1); 1885168404Spjd 1886209962Smm if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 1887219089Spjd !(flags & ZFS_IMPORT_ONLY) && 1888209962Smm zpool_enable_datasets(zhp, mntopts, 0) != 0) { 1889168404Spjd zpool_close(zhp); 1890168404Spjd return (1); 1891168404Spjd } 1892168404Spjd 1893168404Spjd zpool_close(zhp); 1894219089Spjd return (0); 1895168404Spjd} 1896168404Spjd 1897168404Spjd/* 1898168404Spjd * zpool import [-d dir] [-D] 1899185029Spjd * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 1900185029Spjd * [-d dir | -c cachefile] [-f] -a 1901185029Spjd * import [-o mntopts] [-o prop=value] ... [-R root] [-D] 1902219089Spjd * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool] 1903168404Spjd * 1904185029Spjd * -c Read pool information from a cachefile instead of searching 1905185029Spjd * devices. 1906185029Spjd * 1907168404Spjd * -d Scan in a specific directory, other than /dev/dsk. More than 1908168404Spjd * one directory can be specified using multiple '-d' options. 1909168404Spjd * 1910168404Spjd * -D Scan for previously destroyed pools or import all or only 1911168404Spjd * specified destroyed pools. 1912168404Spjd * 1913168404Spjd * -R Temporarily import the pool, with all mountpoints relative to 1914168404Spjd * the given root. The pool will remain exported when the machine 1915168404Spjd * is rebooted. 1916168404Spjd * 1917219089Spjd * -V Import even in the presence of faulted vdevs. This is an 1918185029Spjd * intentionally undocumented option for testing purposes, and 1919185029Spjd * treats the pool configuration as complete, leaving any bad 1920209962Smm * vdevs in the FAULTED state. In other words, it does verbatim 1921209962Smm * import. 1922185029Spjd * 1923219089Spjd * -f Force import, even if it appears that the pool is active. 1924219089Spjd * 1925219089Spjd * -F Attempt rewind if necessary. 1926219089Spjd * 1927219089Spjd * -n See if rewind would work, but don't actually rewind. 1928219089Spjd * 1929219089Spjd * -N Import the pool but don't mount datasets. 1930219089Spjd * 1931219089Spjd * -T Specify a starting txg to use for import. This option is 1932219089Spjd * intentionally undocumented option for testing purposes. 1933219089Spjd * 1934168404Spjd * -a Import all pools found. 1935168404Spjd * 1936185029Spjd * -o Set property=value and/or temporary mount options (without '='). 1937185029Spjd * 1938168404Spjd * The import command scans for pools to import, and import pools based on pool 1939168404Spjd * name and GUID. The pool can also be renamed as part of the import process. 1940168404Spjd */ 1941168404Spjdint 1942168404Spjdzpool_do_import(int argc, char **argv) 1943168404Spjd{ 1944168404Spjd char **searchdirs = NULL; 1945168404Spjd int nsearch = 0; 1946168404Spjd int c; 1947219089Spjd int err = 0; 1948185029Spjd nvlist_t *pools = NULL; 1949168404Spjd boolean_t do_all = B_FALSE; 1950168404Spjd boolean_t do_destroyed = B_FALSE; 1951168404Spjd char *mntopts = NULL; 1952168404Spjd nvpair_t *elem; 1953168404Spjd nvlist_t *config; 1954185029Spjd uint64_t searchguid = 0; 1955185029Spjd char *searchname = NULL; 1956185029Spjd char *propval; 1957168404Spjd nvlist_t *found_config; 1958219089Spjd nvlist_t *policy = NULL; 1959185029Spjd nvlist_t *props = NULL; 1960168404Spjd boolean_t first; 1961219089Spjd int flags = ZFS_IMPORT_NORMAL; 1962219089Spjd uint32_t rewind_policy = ZPOOL_NO_REWIND; 1963219089Spjd boolean_t dryrun = B_FALSE; 1964219089Spjd boolean_t do_rewind = B_FALSE; 1965219089Spjd boolean_t xtreme_rewind = B_FALSE; 1966219089Spjd uint64_t pool_state, txg = -1ULL; 1967185029Spjd char *cachefile = NULL; 1968219089Spjd importargs_t idata = { 0 }; 1969219089Spjd char *endptr; 1970168404Spjd 1971168404Spjd /* check options */ 1972263385Sdelphij while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:T:VX")) != -1) { 1973168404Spjd switch (c) { 1974168404Spjd case 'a': 1975168404Spjd do_all = B_TRUE; 1976168404Spjd break; 1977185029Spjd case 'c': 1978185029Spjd cachefile = optarg; 1979185029Spjd break; 1980168404Spjd case 'd': 1981168404Spjd if (searchdirs == NULL) { 1982168404Spjd searchdirs = safe_malloc(sizeof (char *)); 1983168404Spjd } else { 1984168404Spjd char **tmp = safe_malloc((nsearch + 1) * 1985168404Spjd sizeof (char *)); 1986168404Spjd bcopy(searchdirs, tmp, nsearch * 1987168404Spjd sizeof (char *)); 1988168404Spjd free(searchdirs); 1989168404Spjd searchdirs = tmp; 1990168404Spjd } 1991168404Spjd searchdirs[nsearch++] = optarg; 1992168404Spjd break; 1993168404Spjd case 'D': 1994168404Spjd do_destroyed = B_TRUE; 1995168404Spjd break; 1996168404Spjd case 'f': 1997219089Spjd flags |= ZFS_IMPORT_ANY_HOST; 1998168404Spjd break; 1999185029Spjd case 'F': 2000219089Spjd do_rewind = B_TRUE; 2001185029Spjd break; 2002219089Spjd case 'm': 2003219089Spjd flags |= ZFS_IMPORT_MISSING_LOG; 2004219089Spjd break; 2005219089Spjd case 'n': 2006219089Spjd dryrun = B_TRUE; 2007219089Spjd break; 2008219089Spjd case 'N': 2009219089Spjd flags |= ZFS_IMPORT_ONLY; 2010219089Spjd break; 2011168404Spjd case 'o': 2012185029Spjd if ((propval = strchr(optarg, '=')) != NULL) { 2013185029Spjd *propval = '\0'; 2014185029Spjd propval++; 2015185029Spjd if (add_prop_list(optarg, propval, 2016185029Spjd &props, B_TRUE)) 2017185029Spjd goto error; 2018185029Spjd } else { 2019185029Spjd mntopts = optarg; 2020185029Spjd } 2021168404Spjd break; 2022168404Spjd case 'R': 2023185029Spjd if (add_prop_list(zpool_prop_to_name( 2024185029Spjd ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE)) 2025185029Spjd goto error; 2026185029Spjd if (nvlist_lookup_string(props, 2027185029Spjd zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 2028185029Spjd &propval) == 0) 2029185029Spjd break; 2030185029Spjd if (add_prop_list(zpool_prop_to_name( 2031185029Spjd ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE)) 2032185029Spjd goto error; 2033168404Spjd break; 2034219089Spjd case 'T': 2035219089Spjd errno = 0; 2036219089Spjd txg = strtoull(optarg, &endptr, 10); 2037219089Spjd if (errno != 0 || *endptr != '\0') { 2038219089Spjd (void) fprintf(stderr, 2039219089Spjd gettext("invalid txg value\n")); 2040219089Spjd usage(B_FALSE); 2041219089Spjd } 2042219089Spjd rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND; 2043219089Spjd break; 2044219089Spjd case 'V': 2045219089Spjd flags |= ZFS_IMPORT_VERBATIM; 2046219089Spjd break; 2047219089Spjd case 'X': 2048219089Spjd xtreme_rewind = B_TRUE; 2049219089Spjd break; 2050168404Spjd case ':': 2051168404Spjd (void) fprintf(stderr, gettext("missing argument for " 2052168404Spjd "'%c' option\n"), optopt); 2053168404Spjd usage(B_FALSE); 2054168404Spjd break; 2055168404Spjd case '?': 2056168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2057168404Spjd optopt); 2058168404Spjd usage(B_FALSE); 2059168404Spjd } 2060168404Spjd } 2061168404Spjd 2062168404Spjd argc -= optind; 2063168404Spjd argv += optind; 2064168404Spjd 2065185029Spjd if (cachefile && nsearch != 0) { 2066185029Spjd (void) fprintf(stderr, gettext("-c is incompatible with -d\n")); 2067185029Spjd usage(B_FALSE); 2068185029Spjd } 2069185029Spjd 2070219089Spjd if ((dryrun || xtreme_rewind) && !do_rewind) { 2071219089Spjd (void) fprintf(stderr, 2072219089Spjd gettext("-n or -X only meaningful with -F\n")); 2073219089Spjd usage(B_FALSE); 2074219089Spjd } 2075219089Spjd if (dryrun) 2076219089Spjd rewind_policy = ZPOOL_TRY_REWIND; 2077219089Spjd else if (do_rewind) 2078219089Spjd rewind_policy = ZPOOL_DO_REWIND; 2079219089Spjd if (xtreme_rewind) 2080219089Spjd rewind_policy |= ZPOOL_EXTREME_REWIND; 2081219089Spjd 2082219089Spjd /* In the future, we can capture further policy and include it here */ 2083219089Spjd if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 2084219089Spjd nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 || 2085219089Spjd nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 2086219089Spjd goto error; 2087219089Spjd 2088168404Spjd if (searchdirs == NULL) { 2089168404Spjd searchdirs = safe_malloc(sizeof (char *)); 2090235478Savg searchdirs[0] = "/dev"; 2091168404Spjd nsearch = 1; 2092168404Spjd } 2093168404Spjd 2094168404Spjd /* check argument count */ 2095168404Spjd if (do_all) { 2096168404Spjd if (argc != 0) { 2097168404Spjd (void) fprintf(stderr, gettext("too many arguments\n")); 2098168404Spjd usage(B_FALSE); 2099168404Spjd } 2100168404Spjd } else { 2101168404Spjd if (argc > 2) { 2102168404Spjd (void) fprintf(stderr, gettext("too many arguments\n")); 2103168404Spjd usage(B_FALSE); 2104168404Spjd } 2105168404Spjd 2106168404Spjd /* 2107168404Spjd * Check for the SYS_CONFIG privilege. We do this explicitly 2108168404Spjd * here because otherwise any attempt to discover pools will 2109168404Spjd * silently fail. 2110168404Spjd */ 2111168404Spjd if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) { 2112168404Spjd (void) fprintf(stderr, gettext("cannot " 2113168404Spjd "discover pools: permission denied\n")); 2114168404Spjd free(searchdirs); 2115219089Spjd nvlist_free(policy); 2116168404Spjd return (1); 2117168404Spjd } 2118168404Spjd } 2119168404Spjd 2120168404Spjd /* 2121168404Spjd * Depending on the arguments given, we do one of the following: 2122168404Spjd * 2123168404Spjd * <none> Iterate through all pools and display information about 2124168404Spjd * each one. 2125168404Spjd * 2126168404Spjd * -a Iterate through all pools and try to import each one. 2127168404Spjd * 2128168404Spjd * <id> Find the pool that corresponds to the given GUID/pool 2129168404Spjd * name and import that one. 2130168404Spjd * 2131168404Spjd * -D Above options applies only to destroyed pools. 2132168404Spjd */ 2133168404Spjd if (argc != 0) { 2134168404Spjd char *endptr; 2135168404Spjd 2136168404Spjd errno = 0; 2137168404Spjd searchguid = strtoull(argv[0], &endptr, 10); 2138254758Sdelphij if (errno != 0 || *endptr != '\0') { 2139168404Spjd searchname = argv[0]; 2140254758Sdelphij searchguid = 0; 2141254758Sdelphij } 2142168404Spjd found_config = NULL; 2143168404Spjd 2144185029Spjd /* 2145219089Spjd * User specified a name or guid. Ensure it's unique. 2146185029Spjd */ 2147219089Spjd idata.unique = B_TRUE; 2148185029Spjd } 2149185029Spjd 2150219089Spjd 2151219089Spjd idata.path = searchdirs; 2152219089Spjd idata.paths = nsearch; 2153219089Spjd idata.poolname = searchname; 2154219089Spjd idata.guid = searchguid; 2155219089Spjd idata.cachefile = cachefile; 2156219089Spjd 2157219089Spjd pools = zpool_search_import(g_zfs, &idata); 2158219089Spjd 2159219089Spjd if (pools != NULL && idata.exists && 2160219089Spjd (argc == 1 || strcmp(argv[0], argv[1]) == 0)) { 2161219089Spjd (void) fprintf(stderr, gettext("cannot import '%s': " 2162219089Spjd "a pool with that name already exists\n"), 2163219089Spjd argv[0]); 2164219089Spjd (void) fprintf(stderr, gettext("use the form '%s " 2165219089Spjd "<pool | id> <newpool>' to give it a new name\n"), 2166219089Spjd "zpool import"); 2167219089Spjd err = 1; 2168219089Spjd } else if (pools == NULL && idata.exists) { 2169219089Spjd (void) fprintf(stderr, gettext("cannot import '%s': " 2170219089Spjd "a pool with that name is already created/imported,\n"), 2171219089Spjd argv[0]); 2172219089Spjd (void) fprintf(stderr, gettext("and no additional pools " 2173219089Spjd "with that name were found\n")); 2174219089Spjd err = 1; 2175219089Spjd } else if (pools == NULL) { 2176185029Spjd if (argc != 0) { 2177185029Spjd (void) fprintf(stderr, gettext("cannot import '%s': " 2178185029Spjd "no such pool available\n"), argv[0]); 2179185029Spjd } 2180219089Spjd err = 1; 2181219089Spjd } 2182219089Spjd 2183219089Spjd if (err == 1) { 2184185029Spjd free(searchdirs); 2185219089Spjd nvlist_free(policy); 2186185029Spjd return (1); 2187185029Spjd } 2188185029Spjd 2189185029Spjd /* 2190185029Spjd * At this point we have a list of import candidate configs. Even if 2191185029Spjd * we were searching by pool name or guid, we still need to 2192185029Spjd * post-process the list to deal with pool state and possible 2193185029Spjd * duplicate names. 2194185029Spjd */ 2195168404Spjd err = 0; 2196168404Spjd elem = NULL; 2197168404Spjd first = B_TRUE; 2198168404Spjd while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 2199168404Spjd 2200168404Spjd verify(nvpair_value_nvlist(elem, &config) == 0); 2201168404Spjd 2202168404Spjd verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 2203168404Spjd &pool_state) == 0); 2204168404Spjd if (!do_destroyed && pool_state == POOL_STATE_DESTROYED) 2205168404Spjd continue; 2206168404Spjd if (do_destroyed && pool_state != POOL_STATE_DESTROYED) 2207168404Spjd continue; 2208168404Spjd 2209219089Spjd verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY, 2210219089Spjd policy) == 0); 2211219089Spjd 2212168404Spjd if (argc == 0) { 2213168404Spjd if (first) 2214168404Spjd first = B_FALSE; 2215168404Spjd else if (!do_all) 2216168404Spjd (void) printf("\n"); 2217168404Spjd 2218219089Spjd if (do_all) { 2219168404Spjd err |= do_import(config, NULL, mntopts, 2220219089Spjd props, flags); 2221219089Spjd } else { 2222168404Spjd show_import(config); 2223219089Spjd } 2224168404Spjd } else if (searchname != NULL) { 2225168404Spjd char *name; 2226168404Spjd 2227168404Spjd /* 2228168404Spjd * We are searching for a pool based on name. 2229168404Spjd */ 2230168404Spjd verify(nvlist_lookup_string(config, 2231168404Spjd ZPOOL_CONFIG_POOL_NAME, &name) == 0); 2232168404Spjd 2233168404Spjd if (strcmp(name, searchname) == 0) { 2234168404Spjd if (found_config != NULL) { 2235168404Spjd (void) fprintf(stderr, gettext( 2236168404Spjd "cannot import '%s': more than " 2237168404Spjd "one matching pool\n"), searchname); 2238168404Spjd (void) fprintf(stderr, gettext( 2239168404Spjd "import by numeric ID instead\n")); 2240168404Spjd err = B_TRUE; 2241168404Spjd } 2242168404Spjd found_config = config; 2243168404Spjd } 2244168404Spjd } else { 2245168404Spjd uint64_t guid; 2246168404Spjd 2247168404Spjd /* 2248168404Spjd * Search for a pool by guid. 2249168404Spjd */ 2250168404Spjd verify(nvlist_lookup_uint64(config, 2251168404Spjd ZPOOL_CONFIG_POOL_GUID, &guid) == 0); 2252168404Spjd 2253168404Spjd if (guid == searchguid) 2254168404Spjd found_config = config; 2255168404Spjd } 2256168404Spjd } 2257168404Spjd 2258168404Spjd /* 2259168404Spjd * If we were searching for a specific pool, verify that we found a 2260168404Spjd * pool, and then do the import. 2261168404Spjd */ 2262168404Spjd if (argc != 0 && err == 0) { 2263168404Spjd if (found_config == NULL) { 2264168404Spjd (void) fprintf(stderr, gettext("cannot import '%s': " 2265168404Spjd "no such pool available\n"), argv[0]); 2266168404Spjd err = B_TRUE; 2267168404Spjd } else { 2268168404Spjd err |= do_import(found_config, argc == 1 ? NULL : 2269219089Spjd argv[1], mntopts, props, flags); 2270168404Spjd } 2271168404Spjd } 2272168404Spjd 2273168404Spjd /* 2274168404Spjd * If we were just looking for pools, report an error if none were 2275168404Spjd * found. 2276168404Spjd */ 2277168404Spjd if (argc == 0 && first) 2278168404Spjd (void) fprintf(stderr, 2279168404Spjd gettext("no pools available to import\n")); 2280168404Spjd 2281185029Spjderror: 2282185029Spjd nvlist_free(props); 2283168404Spjd nvlist_free(pools); 2284219089Spjd nvlist_free(policy); 2285168404Spjd free(searchdirs); 2286168404Spjd 2287168404Spjd return (err ? 1 : 0); 2288168404Spjd} 2289168404Spjd 2290168404Spjdtypedef struct iostat_cbdata { 2291236155Smm boolean_t cb_verbose; 2292236155Smm int cb_namewidth; 2293236155Smm int cb_iteration; 2294168404Spjd zpool_list_t *cb_list; 2295168404Spjd} iostat_cbdata_t; 2296168404Spjd 2297168404Spjdstatic void 2298168404Spjdprint_iostat_separator(iostat_cbdata_t *cb) 2299168404Spjd{ 2300168404Spjd int i = 0; 2301168404Spjd 2302168404Spjd for (i = 0; i < cb->cb_namewidth; i++) 2303168404Spjd (void) printf("-"); 2304168404Spjd (void) printf(" ----- ----- ----- ----- ----- -----\n"); 2305168404Spjd} 2306168404Spjd 2307168404Spjdstatic void 2308168404Spjdprint_iostat_header(iostat_cbdata_t *cb) 2309168404Spjd{ 2310168404Spjd (void) printf("%*s capacity operations bandwidth\n", 2311168404Spjd cb->cb_namewidth, ""); 2312219089Spjd (void) printf("%-*s alloc free read write read write\n", 2313168404Spjd cb->cb_namewidth, "pool"); 2314168404Spjd print_iostat_separator(cb); 2315168404Spjd} 2316168404Spjd 2317168404Spjd/* 2318168404Spjd * Display a single statistic. 2319168404Spjd */ 2320185029Spjdstatic void 2321168404Spjdprint_one_stat(uint64_t value) 2322168404Spjd{ 2323168404Spjd char buf[64]; 2324168404Spjd 2325168404Spjd zfs_nicenum(value, buf, sizeof (buf)); 2326168404Spjd (void) printf(" %5s", buf); 2327168404Spjd} 2328168404Spjd 2329168404Spjd/* 2330168404Spjd * Print out all the statistics for the given vdev. This can either be the 2331168404Spjd * toplevel configuration, or called recursively. If 'name' is NULL, then this 2332168404Spjd * is a verbose output, and we don't want to display the toplevel pool stats. 2333168404Spjd */ 2334168404Spjdvoid 2335168404Spjdprint_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv, 2336168404Spjd nvlist_t *newnv, iostat_cbdata_t *cb, int depth) 2337168404Spjd{ 2338168404Spjd nvlist_t **oldchild, **newchild; 2339168404Spjd uint_t c, children; 2340168404Spjd vdev_stat_t *oldvs, *newvs; 2341168404Spjd vdev_stat_t zerovs = { 0 }; 2342168404Spjd uint64_t tdelta; 2343168404Spjd double scale; 2344168404Spjd char *vname; 2345168404Spjd 2346168404Spjd if (oldnv != NULL) { 2347219089Spjd verify(nvlist_lookup_uint64_array(oldnv, 2348219089Spjd ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0); 2349168404Spjd } else { 2350168404Spjd oldvs = &zerovs; 2351168404Spjd } 2352168404Spjd 2353219089Spjd verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS, 2354168404Spjd (uint64_t **)&newvs, &c) == 0); 2355168404Spjd 2356168404Spjd if (strlen(name) + depth > cb->cb_namewidth) 2357168404Spjd (void) printf("%*s%s", depth, "", name); 2358168404Spjd else 2359168404Spjd (void) printf("%*s%s%*s", depth, "", name, 2360168404Spjd (int)(cb->cb_namewidth - strlen(name) - depth), ""); 2361168404Spjd 2362168404Spjd tdelta = newvs->vs_timestamp - oldvs->vs_timestamp; 2363168404Spjd 2364168404Spjd if (tdelta == 0) 2365168404Spjd scale = 1.0; 2366168404Spjd else 2367168404Spjd scale = (double)NANOSEC / tdelta; 2368168404Spjd 2369168404Spjd /* only toplevel vdevs have capacity stats */ 2370168404Spjd if (newvs->vs_space == 0) { 2371168404Spjd (void) printf(" - -"); 2372168404Spjd } else { 2373168404Spjd print_one_stat(newvs->vs_alloc); 2374168404Spjd print_one_stat(newvs->vs_space - newvs->vs_alloc); 2375168404Spjd } 2376168404Spjd 2377168404Spjd print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] - 2378168404Spjd oldvs->vs_ops[ZIO_TYPE_READ]))); 2379168404Spjd 2380168404Spjd print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] - 2381168404Spjd oldvs->vs_ops[ZIO_TYPE_WRITE]))); 2382168404Spjd 2383168404Spjd print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] - 2384168404Spjd oldvs->vs_bytes[ZIO_TYPE_READ]))); 2385168404Spjd 2386168404Spjd print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] - 2387168404Spjd oldvs->vs_bytes[ZIO_TYPE_WRITE]))); 2388168404Spjd 2389168404Spjd (void) printf("\n"); 2390168404Spjd 2391168404Spjd if (!cb->cb_verbose) 2392168404Spjd return; 2393168404Spjd 2394168404Spjd if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN, 2395168404Spjd &newchild, &children) != 0) 2396168404Spjd return; 2397168404Spjd 2398168404Spjd if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN, 2399168404Spjd &oldchild, &c) != 0) 2400168404Spjd return; 2401168404Spjd 2402168404Spjd for (c = 0; c < children; c++) { 2403227497Smm uint64_t ishole = B_FALSE, islog = B_FALSE; 2404219089Spjd 2405227497Smm (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE, 2406227497Smm &ishole); 2407227497Smm 2408227497Smm (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG, 2409227497Smm &islog); 2410227497Smm 2411227497Smm if (ishole || islog) 2412219089Spjd continue; 2413219089Spjd 2414219089Spjd vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE); 2415168404Spjd print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2416168404Spjd newchild[c], cb, depth + 2); 2417168404Spjd free(vname); 2418168404Spjd } 2419185029Spjd 2420185029Spjd /* 2421227497Smm * Log device section 2422227497Smm */ 2423227497Smm 2424227497Smm if (num_logs(newnv) > 0) { 2425227497Smm (void) printf("%-*s - - - - - " 2426227497Smm "-\n", cb->cb_namewidth, "logs"); 2427227497Smm 2428227497Smm for (c = 0; c < children; c++) { 2429227497Smm uint64_t islog = B_FALSE; 2430227497Smm (void) nvlist_lookup_uint64(newchild[c], 2431227497Smm ZPOOL_CONFIG_IS_LOG, &islog); 2432227497Smm 2433227497Smm if (islog) { 2434227497Smm vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 2435227497Smm B_FALSE); 2436227497Smm print_vdev_stats(zhp, vname, oldnv ? 2437227497Smm oldchild[c] : NULL, newchild[c], 2438227497Smm cb, depth + 2); 2439227497Smm free(vname); 2440227497Smm } 2441227497Smm } 2442227497Smm 2443227497Smm } 2444227497Smm 2445227497Smm /* 2446185029Spjd * Include level 2 ARC devices in iostat output 2447185029Spjd */ 2448185029Spjd if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE, 2449185029Spjd &newchild, &children) != 0) 2450185029Spjd return; 2451185029Spjd 2452185029Spjd if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE, 2453185029Spjd &oldchild, &c) != 0) 2454185029Spjd return; 2455185029Spjd 2456185029Spjd if (children > 0) { 2457185029Spjd (void) printf("%-*s - - - - - " 2458185029Spjd "-\n", cb->cb_namewidth, "cache"); 2459185029Spjd for (c = 0; c < children; c++) { 2460219089Spjd vname = zpool_vdev_name(g_zfs, zhp, newchild[c], 2461219089Spjd B_FALSE); 2462185029Spjd print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL, 2463185029Spjd newchild[c], cb, depth + 2); 2464185029Spjd free(vname); 2465185029Spjd } 2466185029Spjd } 2467168404Spjd} 2468168404Spjd 2469168404Spjdstatic int 2470168404Spjdrefresh_iostat(zpool_handle_t *zhp, void *data) 2471168404Spjd{ 2472168404Spjd iostat_cbdata_t *cb = data; 2473168404Spjd boolean_t missing; 2474168404Spjd 2475168404Spjd /* 2476168404Spjd * If the pool has disappeared, remove it from the list and continue. 2477168404Spjd */ 2478168404Spjd if (zpool_refresh_stats(zhp, &missing) != 0) 2479168404Spjd return (-1); 2480168404Spjd 2481168404Spjd if (missing) 2482168404Spjd pool_list_remove(cb->cb_list, zhp); 2483168404Spjd 2484168404Spjd return (0); 2485168404Spjd} 2486168404Spjd 2487168404Spjd/* 2488168404Spjd * Callback to print out the iostats for the given pool. 2489168404Spjd */ 2490168404Spjdint 2491168404Spjdprint_iostat(zpool_handle_t *zhp, void *data) 2492168404Spjd{ 2493168404Spjd iostat_cbdata_t *cb = data; 2494168404Spjd nvlist_t *oldconfig, *newconfig; 2495168404Spjd nvlist_t *oldnvroot, *newnvroot; 2496168404Spjd 2497168404Spjd newconfig = zpool_get_config(zhp, &oldconfig); 2498168404Spjd 2499168404Spjd if (cb->cb_iteration == 1) 2500168404Spjd oldconfig = NULL; 2501168404Spjd 2502168404Spjd verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE, 2503168404Spjd &newnvroot) == 0); 2504168404Spjd 2505168404Spjd if (oldconfig == NULL) 2506168404Spjd oldnvroot = NULL; 2507168404Spjd else 2508168404Spjd verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE, 2509168404Spjd &oldnvroot) == 0); 2510168404Spjd 2511168404Spjd /* 2512168404Spjd * Print out the statistics for the pool. 2513168404Spjd */ 2514168404Spjd print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0); 2515168404Spjd 2516168404Spjd if (cb->cb_verbose) 2517168404Spjd print_iostat_separator(cb); 2518168404Spjd 2519168404Spjd return (0); 2520168404Spjd} 2521168404Spjd 2522168404Spjdint 2523168404Spjdget_namewidth(zpool_handle_t *zhp, void *data) 2524168404Spjd{ 2525168404Spjd iostat_cbdata_t *cb = data; 2526168404Spjd nvlist_t *config, *nvroot; 2527168404Spjd 2528168404Spjd if ((config = zpool_get_config(zhp, NULL)) != NULL) { 2529168404Spjd verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2530168404Spjd &nvroot) == 0); 2531168404Spjd if (!cb->cb_verbose) 2532168404Spjd cb->cb_namewidth = strlen(zpool_get_name(zhp)); 2533168404Spjd else 2534236145Smm cb->cb_namewidth = max_width(zhp, nvroot, 0, 2535236145Smm cb->cb_namewidth); 2536168404Spjd } 2537168404Spjd 2538168404Spjd /* 2539168404Spjd * The width must fall into the range [10,38]. The upper limit is the 2540168404Spjd * maximum we can have and still fit in 80 columns. 2541168404Spjd */ 2542168404Spjd if (cb->cb_namewidth < 10) 2543168404Spjd cb->cb_namewidth = 10; 2544168404Spjd if (cb->cb_namewidth > 38) 2545168404Spjd cb->cb_namewidth = 38; 2546168404Spjd 2547168404Spjd return (0); 2548168404Spjd} 2549168404Spjd 2550168404Spjd/* 2551219089Spjd * Parse the input string, get the 'interval' and 'count' value if there is one. 2552168404Spjd */ 2553219089Spjdstatic void 2554219089Spjdget_interval_count(int *argcp, char **argv, unsigned long *iv, 2555219089Spjd unsigned long *cnt) 2556168404Spjd{ 2557168404Spjd unsigned long interval = 0, count = 0; 2558219089Spjd int argc = *argcp, errno; 2559168404Spjd 2560168404Spjd /* 2561168404Spjd * Determine if the last argument is an integer or a pool name 2562168404Spjd */ 2563168404Spjd if (argc > 0 && isdigit(argv[argc - 1][0])) { 2564168404Spjd char *end; 2565168404Spjd 2566168404Spjd errno = 0; 2567168404Spjd interval = strtoul(argv[argc - 1], &end, 10); 2568168404Spjd 2569168404Spjd if (*end == '\0' && errno == 0) { 2570168404Spjd if (interval == 0) { 2571168404Spjd (void) fprintf(stderr, gettext("interval " 2572168404Spjd "cannot be zero\n")); 2573168404Spjd usage(B_FALSE); 2574168404Spjd } 2575168404Spjd /* 2576168404Spjd * Ignore the last parameter 2577168404Spjd */ 2578168404Spjd argc--; 2579168404Spjd } else { 2580168404Spjd /* 2581168404Spjd * If this is not a valid number, just plow on. The 2582168404Spjd * user will get a more informative error message later 2583168404Spjd * on. 2584168404Spjd */ 2585168404Spjd interval = 0; 2586168404Spjd } 2587168404Spjd } 2588168404Spjd 2589168404Spjd /* 2590168404Spjd * If the last argument is also an integer, then we have both a count 2591219089Spjd * and an interval. 2592168404Spjd */ 2593168404Spjd if (argc > 0 && isdigit(argv[argc - 1][0])) { 2594168404Spjd char *end; 2595168404Spjd 2596168404Spjd errno = 0; 2597168404Spjd count = interval; 2598168404Spjd interval = strtoul(argv[argc - 1], &end, 10); 2599168404Spjd 2600168404Spjd if (*end == '\0' && errno == 0) { 2601168404Spjd if (interval == 0) { 2602168404Spjd (void) fprintf(stderr, gettext("interval " 2603168404Spjd "cannot be zero\n")); 2604168404Spjd usage(B_FALSE); 2605168404Spjd } 2606168404Spjd 2607168404Spjd /* 2608168404Spjd * Ignore the last parameter 2609168404Spjd */ 2610168404Spjd argc--; 2611168404Spjd } else { 2612168404Spjd interval = 0; 2613168404Spjd } 2614168404Spjd } 2615168404Spjd 2616219089Spjd *iv = interval; 2617219089Spjd *cnt = count; 2618219089Spjd *argcp = argc; 2619219089Spjd} 2620219089Spjd 2621219089Spjdstatic void 2622219089Spjdget_timestamp_arg(char c) 2623219089Spjd{ 2624219089Spjd if (c == 'u') 2625219089Spjd timestamp_fmt = UDATE; 2626219089Spjd else if (c == 'd') 2627219089Spjd timestamp_fmt = DDATE; 2628219089Spjd else 2629219089Spjd usage(B_FALSE); 2630219089Spjd} 2631219089Spjd 2632219089Spjd/* 2633219089Spjd * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]] 2634219089Spjd * 2635219089Spjd * -v Display statistics for individual vdevs 2636219089Spjd * -T Display a timestamp in date(1) or Unix format 2637219089Spjd * 2638219089Spjd * This command can be tricky because we want to be able to deal with pool 2639219089Spjd * creation/destruction as well as vdev configuration changes. The bulk of this 2640219089Spjd * processing is handled by the pool_list_* routines in zpool_iter.c. We rely 2641219089Spjd * on pool_list_update() to detect the addition of new pools. Configuration 2642219089Spjd * changes are all handled within libzfs. 2643219089Spjd */ 2644219089Spjdint 2645219089Spjdzpool_do_iostat(int argc, char **argv) 2646219089Spjd{ 2647219089Spjd int c; 2648219089Spjd int ret; 2649219089Spjd int npools; 2650219089Spjd unsigned long interval = 0, count = 0; 2651219089Spjd zpool_list_t *list; 2652219089Spjd boolean_t verbose = B_FALSE; 2653219089Spjd iostat_cbdata_t cb; 2654219089Spjd 2655219089Spjd /* check options */ 2656219089Spjd while ((c = getopt(argc, argv, "T:v")) != -1) { 2657219089Spjd switch (c) { 2658219089Spjd case 'T': 2659219089Spjd get_timestamp_arg(*optarg); 2660219089Spjd break; 2661219089Spjd case 'v': 2662219089Spjd verbose = B_TRUE; 2663219089Spjd break; 2664219089Spjd case '?': 2665219089Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2666219089Spjd optopt); 2667219089Spjd usage(B_FALSE); 2668219089Spjd } 2669219089Spjd } 2670219089Spjd 2671219089Spjd argc -= optind; 2672219089Spjd argv += optind; 2673219089Spjd 2674219089Spjd get_interval_count(&argc, argv, &interval, &count); 2675219089Spjd 2676168404Spjd /* 2677168404Spjd * Construct the list of all interesting pools. 2678168404Spjd */ 2679168404Spjd ret = 0; 2680168404Spjd if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL) 2681168404Spjd return (1); 2682168404Spjd 2683168404Spjd if (pool_list_count(list) == 0 && argc != 0) { 2684168404Spjd pool_list_free(list); 2685168404Spjd return (1); 2686168404Spjd } 2687168404Spjd 2688168404Spjd if (pool_list_count(list) == 0 && interval == 0) { 2689168404Spjd pool_list_free(list); 2690168404Spjd (void) fprintf(stderr, gettext("no pools available\n")); 2691168404Spjd return (1); 2692168404Spjd } 2693168404Spjd 2694168404Spjd /* 2695168404Spjd * Enter the main iostat loop. 2696168404Spjd */ 2697168404Spjd cb.cb_list = list; 2698168404Spjd cb.cb_verbose = verbose; 2699168404Spjd cb.cb_iteration = 0; 2700168404Spjd cb.cb_namewidth = 0; 2701168404Spjd 2702168404Spjd for (;;) { 2703168404Spjd pool_list_update(list); 2704168404Spjd 2705168404Spjd if ((npools = pool_list_count(list)) == 0) 2706168404Spjd break; 2707168404Spjd 2708168404Spjd /* 2709168404Spjd * Refresh all statistics. This is done as an explicit step 2710168404Spjd * before calculating the maximum name width, so that any 2711168404Spjd * configuration changes are properly accounted for. 2712168404Spjd */ 2713168404Spjd (void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb); 2714168404Spjd 2715168404Spjd /* 2716168404Spjd * Iterate over all pools to determine the maximum width 2717168404Spjd * for the pool / device name column across all pools. 2718168404Spjd */ 2719168404Spjd cb.cb_namewidth = 0; 2720168404Spjd (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 2721168404Spjd 2722219089Spjd if (timestamp_fmt != NODATE) 2723219089Spjd print_timestamp(timestamp_fmt); 2724219089Spjd 2725168404Spjd /* 2726168404Spjd * If it's the first time, or verbose mode, print the header. 2727168404Spjd */ 2728168404Spjd if (++cb.cb_iteration == 1 || verbose) 2729168404Spjd print_iostat_header(&cb); 2730168404Spjd 2731168404Spjd (void) pool_list_iter(list, B_FALSE, print_iostat, &cb); 2732168404Spjd 2733168404Spjd /* 2734168404Spjd * If there's more than one pool, and we're not in verbose mode 2735168404Spjd * (which prints a separator for us), then print a separator. 2736168404Spjd */ 2737168404Spjd if (npools > 1 && !verbose) 2738168404Spjd print_iostat_separator(&cb); 2739168404Spjd 2740168404Spjd if (verbose) 2741168404Spjd (void) printf("\n"); 2742168404Spjd 2743168404Spjd /* 2744168404Spjd * Flush the output so that redirection to a file isn't buffered 2745168404Spjd * indefinitely. 2746168404Spjd */ 2747168404Spjd (void) fflush(stdout); 2748168404Spjd 2749168404Spjd if (interval == 0) 2750168404Spjd break; 2751168404Spjd 2752168404Spjd if (count != 0 && --count == 0) 2753168404Spjd break; 2754168404Spjd 2755168404Spjd (void) sleep(interval); 2756168404Spjd } 2757168404Spjd 2758168404Spjd pool_list_free(list); 2759168404Spjd 2760168404Spjd return (ret); 2761168404Spjd} 2762168404Spjd 2763168404Spjdtypedef struct list_cbdata { 2764236155Smm boolean_t cb_verbose; 2765236155Smm int cb_namewidth; 2766168404Spjd boolean_t cb_scripted; 2767185029Spjd zprop_list_t *cb_proplist; 2768263889Sdelphij boolean_t cb_literal; 2769168404Spjd} list_cbdata_t; 2770168404Spjd 2771168404Spjd/* 2772168404Spjd * Given a list of columns to display, output appropriate headers for each one. 2773168404Spjd */ 2774185029Spjdstatic void 2775236155Smmprint_header(list_cbdata_t *cb) 2776168404Spjd{ 2777236155Smm zprop_list_t *pl = cb->cb_proplist; 2778236884Smm char headerbuf[ZPOOL_MAXPROPLEN]; 2779185029Spjd const char *header; 2780185029Spjd boolean_t first = B_TRUE; 2781185029Spjd boolean_t right_justify; 2782236155Smm size_t width = 0; 2783168404Spjd 2784185029Spjd for (; pl != NULL; pl = pl->pl_next) { 2785236155Smm width = pl->pl_width; 2786236155Smm if (first && cb->cb_verbose) { 2787236155Smm /* 2788236155Smm * Reset the width to accommodate the verbose listing 2789236155Smm * of devices. 2790236155Smm */ 2791236155Smm width = cb->cb_namewidth; 2792236155Smm } 2793236155Smm 2794185029Spjd if (!first) 2795168404Spjd (void) printf(" "); 2796168404Spjd else 2797185029Spjd first = B_FALSE; 2798168404Spjd 2799236884Smm right_justify = B_FALSE; 2800236884Smm if (pl->pl_prop != ZPROP_INVAL) { 2801236884Smm header = zpool_prop_column_name(pl->pl_prop); 2802236884Smm right_justify = zpool_prop_align_right(pl->pl_prop); 2803236884Smm } else { 2804236884Smm int i; 2805185029Spjd 2806236884Smm for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2807236884Smm headerbuf[i] = toupper(pl->pl_user_prop[i]); 2808236884Smm headerbuf[i] = '\0'; 2809236884Smm header = headerbuf; 2810236884Smm } 2811236884Smm 2812185029Spjd if (pl->pl_next == NULL && !right_justify) 2813185029Spjd (void) printf("%s", header); 2814185029Spjd else if (right_justify) 2815236155Smm (void) printf("%*s", width, header); 2816185029Spjd else 2817236155Smm (void) printf("%-*s", width, header); 2818236155Smm 2819168404Spjd } 2820168404Spjd 2821168404Spjd (void) printf("\n"); 2822168404Spjd} 2823168404Spjd 2824185029Spjd/* 2825185029Spjd * Given a pool and a list of properties, print out all the properties according 2826185029Spjd * to the described layout. 2827185029Spjd */ 2828185029Spjdstatic void 2829236155Smmprint_pool(zpool_handle_t *zhp, list_cbdata_t *cb) 2830168404Spjd{ 2831236155Smm zprop_list_t *pl = cb->cb_proplist; 2832185029Spjd boolean_t first = B_TRUE; 2833185029Spjd char property[ZPOOL_MAXPROPLEN]; 2834185029Spjd char *propstr; 2835185029Spjd boolean_t right_justify; 2836236155Smm size_t width; 2837168404Spjd 2838185029Spjd for (; pl != NULL; pl = pl->pl_next) { 2839236155Smm 2840236155Smm width = pl->pl_width; 2841236155Smm if (first && cb->cb_verbose) { 2842236155Smm /* 2843236155Smm * Reset the width to accommodate the verbose listing 2844236155Smm * of devices. 2845236155Smm */ 2846236155Smm width = cb->cb_namewidth; 2847236155Smm } 2848236155Smm 2849185029Spjd if (!first) { 2850236155Smm if (cb->cb_scripted) 2851168404Spjd (void) printf("\t"); 2852168404Spjd else 2853168404Spjd (void) printf(" "); 2854185029Spjd } else { 2855185029Spjd first = B_FALSE; 2856168404Spjd } 2857168404Spjd 2858185029Spjd right_justify = B_FALSE; 2859185029Spjd if (pl->pl_prop != ZPROP_INVAL) { 2860236155Smm if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ && 2861236155Smm zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0) 2862236155Smm propstr = "-"; 2863236155Smm else if (zpool_get_prop(zhp, pl->pl_prop, property, 2864263889Sdelphij sizeof (property), NULL, cb->cb_literal) != 0) 2865185029Spjd propstr = "-"; 2866168404Spjd else 2867185029Spjd propstr = property; 2868168404Spjd 2869185029Spjd right_justify = zpool_prop_align_right(pl->pl_prop); 2870236884Smm } else if ((zpool_prop_feature(pl->pl_user_prop) || 2871236884Smm zpool_prop_unsupported(pl->pl_user_prop)) && 2872236884Smm zpool_prop_get_feature(zhp, pl->pl_user_prop, property, 2873236884Smm sizeof (property)) == 0) { 2874236884Smm propstr = property; 2875185029Spjd } else { 2876185029Spjd propstr = "-"; 2877185029Spjd } 2878168404Spjd 2879168404Spjd 2880185029Spjd /* 2881185029Spjd * If this is being called in scripted mode, or if this is the 2882185029Spjd * last column and it is left-justified, don't include a width 2883185029Spjd * format specifier. 2884185029Spjd */ 2885236155Smm if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 2886185029Spjd (void) printf("%s", propstr); 2887185029Spjd else if (right_justify) 2888185029Spjd (void) printf("%*s", width, propstr); 2889185029Spjd else 2890185029Spjd (void) printf("%-*s", width, propstr); 2891185029Spjd } 2892168404Spjd 2893185029Spjd (void) printf("\n"); 2894185029Spjd} 2895168404Spjd 2896236155Smmstatic void 2897236155Smmprint_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted) 2898236155Smm{ 2899236155Smm char propval[64]; 2900236155Smm boolean_t fixed; 2901236155Smm size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL); 2902236155Smm 2903236155Smm zfs_nicenum(value, propval, sizeof (propval)); 2904236155Smm 2905236155Smm if (prop == ZPOOL_PROP_EXPANDSZ && value == 0) 2906236155Smm (void) strlcpy(propval, "-", sizeof (propval)); 2907236155Smm 2908236155Smm if (scripted) 2909236155Smm (void) printf("\t%s", propval); 2910236155Smm else 2911236155Smm (void) printf(" %*s", width, propval); 2912236155Smm} 2913236155Smm 2914236155Smmvoid 2915236155Smmprint_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv, 2916236155Smm list_cbdata_t *cb, int depth) 2917236155Smm{ 2918236155Smm nvlist_t **child; 2919236155Smm vdev_stat_t *vs; 2920236155Smm uint_t c, children; 2921236155Smm char *vname; 2922236155Smm boolean_t scripted = cb->cb_scripted; 2923236155Smm 2924236155Smm verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 2925236155Smm (uint64_t **)&vs, &c) == 0); 2926236155Smm 2927236155Smm if (name != NULL) { 2928236155Smm if (scripted) 2929236155Smm (void) printf("\t%s", name); 2930236155Smm else if (strlen(name) + depth > cb->cb_namewidth) 2931236155Smm (void) printf("%*s%s", depth, "", name); 2932236155Smm else 2933236155Smm (void) printf("%*s%s%*s", depth, "", name, 2934236155Smm (int)(cb->cb_namewidth - strlen(name) - depth), ""); 2935236155Smm 2936236155Smm /* only toplevel vdevs have capacity stats */ 2937236155Smm if (vs->vs_space == 0) { 2938236155Smm if (scripted) 2939236155Smm (void) printf("\t-\t-\t-"); 2940236155Smm else 2941236155Smm (void) printf(" - - -"); 2942236155Smm } else { 2943236155Smm print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, 2944236155Smm scripted); 2945236155Smm print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc, 2946236155Smm scripted); 2947236155Smm print_one_column(ZPOOL_PROP_FREE, 2948236155Smm vs->vs_space - vs->vs_alloc, scripted); 2949236155Smm } 2950236155Smm print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, 2951236155Smm scripted); 2952236155Smm (void) printf("\n"); 2953236155Smm } 2954236155Smm 2955236155Smm if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2956236155Smm &child, &children) != 0) 2957236155Smm return; 2958236155Smm 2959236155Smm for (c = 0; c < children; c++) { 2960236155Smm uint64_t ishole = B_FALSE; 2961236155Smm 2962236155Smm if (nvlist_lookup_uint64(child[c], 2963236155Smm ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole) 2964236155Smm continue; 2965236155Smm 2966236155Smm vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE); 2967236155Smm print_list_stats(zhp, vname, child[c], cb, depth + 2); 2968236155Smm free(vname); 2969236155Smm } 2970236155Smm 2971236155Smm /* 2972236155Smm * Include level 2 ARC devices in iostat output 2973236155Smm */ 2974236155Smm if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2975236155Smm &child, &children) != 0) 2976236155Smm return; 2977236155Smm 2978236155Smm if (children > 0) { 2979236155Smm (void) printf("%-*s - - - - - " 2980236155Smm "-\n", cb->cb_namewidth, "cache"); 2981236155Smm for (c = 0; c < children; c++) { 2982236155Smm vname = zpool_vdev_name(g_zfs, zhp, child[c], 2983236155Smm B_FALSE); 2984236155Smm print_list_stats(zhp, vname, child[c], cb, depth + 2); 2985236155Smm free(vname); 2986236155Smm } 2987236155Smm } 2988236155Smm} 2989236155Smm 2990236155Smm 2991185029Spjd/* 2992185029Spjd * Generic callback function to list a pool. 2993185029Spjd */ 2994185029Spjdint 2995185029Spjdlist_callback(zpool_handle_t *zhp, void *data) 2996185029Spjd{ 2997185029Spjd list_cbdata_t *cbp = data; 2998236155Smm nvlist_t *config; 2999236155Smm nvlist_t *nvroot; 3000168404Spjd 3001236155Smm config = zpool_get_config(zhp, NULL); 3002168404Spjd 3003236155Smm print_pool(zhp, cbp); 3004236155Smm if (!cbp->cb_verbose) 3005236155Smm return (0); 3006168404Spjd 3007236155Smm verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 3008236155Smm &nvroot) == 0); 3009236155Smm print_list_stats(zhp, NULL, nvroot, cbp, 0); 3010236155Smm 3011168404Spjd return (0); 3012168404Spjd} 3013168404Spjd 3014168404Spjd/* 3015263889Sdelphij * zpool list [-Hp] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]] 3016168404Spjd * 3017185029Spjd * -H Scripted mode. Don't display headers, and separate properties 3018185029Spjd * by a single tab. 3019185029Spjd * -o List of properties to display. Defaults to 3020219089Spjd * "name,size,allocated,free,capacity,health,altroot" 3021263889Sdelphij * -p Diplay values in parsable (exact) format. 3022219089Spjd * -T Display a timestamp in date(1) or Unix format 3023168404Spjd * 3024168404Spjd * List all pools in the system, whether or not they're healthy. Output space 3025168404Spjd * statistics for each one, as well as health status summary. 3026168404Spjd */ 3027168404Spjdint 3028168404Spjdzpool_do_list(int argc, char **argv) 3029168404Spjd{ 3030168404Spjd int c; 3031168404Spjd int ret; 3032168404Spjd list_cbdata_t cb = { 0 }; 3033185029Spjd static char default_props[] = 3034236155Smm "name,size,allocated,free,capacity,dedupratio," 3035236155Smm "health,altroot"; 3036185029Spjd char *props = default_props; 3037219089Spjd unsigned long interval = 0, count = 0; 3038236155Smm zpool_list_t *list; 3039236155Smm boolean_t first = B_TRUE; 3040168404Spjd 3041168404Spjd /* check options */ 3042263889Sdelphij while ((c = getopt(argc, argv, ":Ho:pT:v")) != -1) { 3043168404Spjd switch (c) { 3044168404Spjd case 'H': 3045168404Spjd cb.cb_scripted = B_TRUE; 3046168404Spjd break; 3047168404Spjd case 'o': 3048185029Spjd props = optarg; 3049168404Spjd break; 3050263889Sdelphij case 'p': 3051263889Sdelphij cb.cb_literal = B_TRUE; 3052263889Sdelphij break; 3053219089Spjd case 'T': 3054219089Spjd get_timestamp_arg(*optarg); 3055219089Spjd break; 3056236155Smm case 'v': 3057236155Smm cb.cb_verbose = B_TRUE; 3058236155Smm break; 3059168404Spjd case ':': 3060168404Spjd (void) fprintf(stderr, gettext("missing argument for " 3061168404Spjd "'%c' option\n"), optopt); 3062168404Spjd usage(B_FALSE); 3063168404Spjd break; 3064168404Spjd case '?': 3065168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3066168404Spjd optopt); 3067168404Spjd usage(B_FALSE); 3068168404Spjd } 3069168404Spjd } 3070168404Spjd 3071168404Spjd argc -= optind; 3072168404Spjd argv += optind; 3073168404Spjd 3074219089Spjd get_interval_count(&argc, argv, &interval, &count); 3075219089Spjd 3076185029Spjd if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0) 3077185029Spjd usage(B_FALSE); 3078168404Spjd 3079219089Spjd for (;;) { 3080268470Sdelphij if ((list = pool_list_get(argc, argv, &cb.cb_proplist, 3081268470Sdelphij &ret)) == NULL) 3082268470Sdelphij return (1); 3083168404Spjd 3084236155Smm if (pool_list_count(list) == 0) 3085236155Smm break; 3086236155Smm 3087236155Smm cb.cb_namewidth = 0; 3088236155Smm (void) pool_list_iter(list, B_FALSE, get_namewidth, &cb); 3089236155Smm 3090219089Spjd if (timestamp_fmt != NODATE) 3091219089Spjd print_timestamp(timestamp_fmt); 3092168404Spjd 3093236155Smm if (!cb.cb_scripted && (first || cb.cb_verbose)) { 3094236155Smm print_header(&cb); 3095236155Smm first = B_FALSE; 3096219089Spjd } 3097236155Smm ret = pool_list_iter(list, B_TRUE, list_callback, &cb); 3098219089Spjd 3099219089Spjd if (interval == 0) 3100219089Spjd break; 3101219089Spjd 3102219089Spjd if (count != 0 && --count == 0) 3103219089Spjd break; 3104219089Spjd 3105268470Sdelphij pool_list_free(list); 3106219089Spjd (void) sleep(interval); 3107168404Spjd } 3108168404Spjd 3109268470Sdelphij if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) { 3110268470Sdelphij (void) printf(gettext("no pools available\n")); 3111268470Sdelphij ret = 0; 3112268470Sdelphij } 3113268470Sdelphij 3114268470Sdelphij pool_list_free(list); 3115219089Spjd zprop_free_list(cb.cb_proplist); 3116168404Spjd return (ret); 3117168404Spjd} 3118168404Spjd 3119168404Spjdstatic nvlist_t * 3120168404Spjdzpool_get_vdev_by_name(nvlist_t *nv, char *name) 3121168404Spjd{ 3122168404Spjd nvlist_t **child; 3123168404Spjd uint_t c, children; 3124168404Spjd nvlist_t *match; 3125168404Spjd char *path; 3126168404Spjd 3127168404Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 3128168404Spjd &child, &children) != 0) { 3129168404Spjd verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0); 3130219089Spjd if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 3131219089Spjd name += sizeof(_PATH_DEV) - 1; 3132219089Spjd if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 3133219089Spjd path += sizeof(_PATH_DEV) - 1; 3134168404Spjd if (strcmp(name, path) == 0) 3135168404Spjd return (nv); 3136168404Spjd return (NULL); 3137168404Spjd } 3138168404Spjd 3139168404Spjd for (c = 0; c < children; c++) 3140168404Spjd if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL) 3141168404Spjd return (match); 3142168404Spjd 3143168404Spjd return (NULL); 3144168404Spjd} 3145168404Spjd 3146168404Spjdstatic int 3147168404Spjdzpool_do_attach_or_replace(int argc, char **argv, int replacing) 3148168404Spjd{ 3149168404Spjd boolean_t force = B_FALSE; 3150168404Spjd int c; 3151168404Spjd nvlist_t *nvroot; 3152168404Spjd char *poolname, *old_disk, *new_disk; 3153168404Spjd zpool_handle_t *zhp; 3154168404Spjd int ret; 3155168404Spjd 3156168404Spjd /* check options */ 3157168404Spjd while ((c = getopt(argc, argv, "f")) != -1) { 3158168404Spjd switch (c) { 3159168404Spjd case 'f': 3160168404Spjd force = B_TRUE; 3161168404Spjd break; 3162168404Spjd case '?': 3163168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3164168404Spjd optopt); 3165168404Spjd usage(B_FALSE); 3166168404Spjd } 3167168404Spjd } 3168168404Spjd 3169168404Spjd argc -= optind; 3170168404Spjd argv += optind; 3171168404Spjd 3172168404Spjd /* get pool name and check number of arguments */ 3173168404Spjd if (argc < 1) { 3174168404Spjd (void) fprintf(stderr, gettext("missing pool name argument\n")); 3175168404Spjd usage(B_FALSE); 3176168404Spjd } 3177168404Spjd 3178168404Spjd poolname = argv[0]; 3179168404Spjd 3180168404Spjd if (argc < 2) { 3181168404Spjd (void) fprintf(stderr, 3182168404Spjd gettext("missing <device> specification\n")); 3183168404Spjd usage(B_FALSE); 3184168404Spjd } 3185168404Spjd 3186168404Spjd old_disk = argv[1]; 3187168404Spjd 3188168404Spjd if (argc < 3) { 3189168404Spjd if (!replacing) { 3190168404Spjd (void) fprintf(stderr, 3191168404Spjd gettext("missing <new_device> specification\n")); 3192168404Spjd usage(B_FALSE); 3193168404Spjd } 3194168404Spjd new_disk = old_disk; 3195168404Spjd argc -= 1; 3196168404Spjd argv += 1; 3197168404Spjd } else { 3198168404Spjd new_disk = argv[2]; 3199168404Spjd argc -= 2; 3200168404Spjd argv += 2; 3201168404Spjd } 3202168404Spjd 3203168404Spjd if (argc > 1) { 3204168404Spjd (void) fprintf(stderr, gettext("too many arguments\n")); 3205168404Spjd usage(B_FALSE); 3206168404Spjd } 3207168404Spjd 3208168404Spjd if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3209168404Spjd return (1); 3210168404Spjd 3211185029Spjd if (zpool_get_config(zhp, NULL) == NULL) { 3212168404Spjd (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"), 3213168404Spjd poolname); 3214168404Spjd zpool_close(zhp); 3215168404Spjd return (1); 3216168404Spjd } 3217168404Spjd 3218185029Spjd nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, B_FALSE, 3219185029Spjd argc, argv); 3220168404Spjd if (nvroot == NULL) { 3221168404Spjd zpool_close(zhp); 3222168404Spjd return (1); 3223168404Spjd } 3224168404Spjd 3225168404Spjd ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing); 3226168404Spjd 3227168404Spjd nvlist_free(nvroot); 3228168404Spjd zpool_close(zhp); 3229168404Spjd 3230168404Spjd return (ret); 3231168404Spjd} 3232168404Spjd 3233168404Spjd/* 3234168404Spjd * zpool replace [-f] <pool> <device> <new_device> 3235168404Spjd * 3236168404Spjd * -f Force attach, even if <new_device> appears to be in use. 3237168404Spjd * 3238168404Spjd * Replace <device> with <new_device>. 3239168404Spjd */ 3240168404Spjd/* ARGSUSED */ 3241168404Spjdint 3242168404Spjdzpool_do_replace(int argc, char **argv) 3243168404Spjd{ 3244168404Spjd return (zpool_do_attach_or_replace(argc, argv, B_TRUE)); 3245168404Spjd} 3246168404Spjd 3247168404Spjd/* 3248168404Spjd * zpool attach [-f] <pool> <device> <new_device> 3249168404Spjd * 3250168404Spjd * -f Force attach, even if <new_device> appears to be in use. 3251168404Spjd * 3252168404Spjd * Attach <new_device> to the mirror containing <device>. If <device> is not 3253168404Spjd * part of a mirror, then <device> will be transformed into a mirror of 3254168404Spjd * <device> and <new_device>. In either case, <new_device> will begin life 3255168404Spjd * with a DTL of [0, now], and will immediately begin to resilver itself. 3256168404Spjd */ 3257168404Spjdint 3258168404Spjdzpool_do_attach(int argc, char **argv) 3259168404Spjd{ 3260168404Spjd return (zpool_do_attach_or_replace(argc, argv, B_FALSE)); 3261168404Spjd} 3262168404Spjd 3263168404Spjd/* 3264168404Spjd * zpool detach [-f] <pool> <device> 3265168404Spjd * 3266168404Spjd * -f Force detach of <device>, even if DTLs argue against it 3267168404Spjd * (not supported yet) 3268168404Spjd * 3269168404Spjd * Detach a device from a mirror. The operation will be refused if <device> 3270168404Spjd * is the last device in the mirror, or if the DTLs indicate that this device 3271168404Spjd * has the only valid copy of some data. 3272168404Spjd */ 3273168404Spjd/* ARGSUSED */ 3274168404Spjdint 3275168404Spjdzpool_do_detach(int argc, char **argv) 3276168404Spjd{ 3277168404Spjd int c; 3278168404Spjd char *poolname, *path; 3279168404Spjd zpool_handle_t *zhp; 3280168404Spjd int ret; 3281168404Spjd 3282168404Spjd /* check options */ 3283168404Spjd while ((c = getopt(argc, argv, "f")) != -1) { 3284168404Spjd switch (c) { 3285168404Spjd case 'f': 3286168404Spjd case '?': 3287168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3288168404Spjd optopt); 3289168404Spjd usage(B_FALSE); 3290168404Spjd } 3291168404Spjd } 3292168404Spjd 3293168404Spjd argc -= optind; 3294168404Spjd argv += optind; 3295168404Spjd 3296168404Spjd /* get pool name and check number of arguments */ 3297168404Spjd if (argc < 1) { 3298168404Spjd (void) fprintf(stderr, gettext("missing pool name argument\n")); 3299168404Spjd usage(B_FALSE); 3300168404Spjd } 3301168404Spjd 3302168404Spjd if (argc < 2) { 3303168404Spjd (void) fprintf(stderr, 3304168404Spjd gettext("missing <device> specification\n")); 3305168404Spjd usage(B_FALSE); 3306168404Spjd } 3307168404Spjd 3308168404Spjd poolname = argv[0]; 3309168404Spjd path = argv[1]; 3310168404Spjd 3311168404Spjd if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3312168404Spjd return (1); 3313168404Spjd 3314168404Spjd ret = zpool_vdev_detach(zhp, path); 3315168404Spjd 3316168404Spjd zpool_close(zhp); 3317168404Spjd 3318168404Spjd return (ret); 3319168404Spjd} 3320168404Spjd 3321168404Spjd/* 3322219089Spjd * zpool split [-n] [-o prop=val] ... 3323219089Spjd * [-o mntopt] ... 3324219089Spjd * [-R altroot] <pool> <newpool> [<device> ...] 3325219089Spjd * 3326219089Spjd * -n Do not split the pool, but display the resulting layout if 3327219089Spjd * it were to be split. 3328219089Spjd * -o Set property=value, or set mount options. 3329219089Spjd * -R Mount the split-off pool under an alternate root. 3330219089Spjd * 3331219089Spjd * Splits the named pool and gives it the new pool name. Devices to be split 3332219089Spjd * off may be listed, provided that no more than one device is specified 3333219089Spjd * per top-level vdev mirror. The newly split pool is left in an exported 3334219089Spjd * state unless -R is specified. 3335219089Spjd * 3336219089Spjd * Restrictions: the top-level of the pool pool must only be made up of 3337219089Spjd * mirrors; all devices in the pool must be healthy; no device may be 3338219089Spjd * undergoing a resilvering operation. 3339219089Spjd */ 3340219089Spjdint 3341219089Spjdzpool_do_split(int argc, char **argv) 3342219089Spjd{ 3343219089Spjd char *srcpool, *newpool, *propval; 3344219089Spjd char *mntopts = NULL; 3345219089Spjd splitflags_t flags; 3346219089Spjd int c, ret = 0; 3347219089Spjd zpool_handle_t *zhp; 3348219089Spjd nvlist_t *config, *props = NULL; 3349219089Spjd 3350219089Spjd flags.dryrun = B_FALSE; 3351219089Spjd flags.import = B_FALSE; 3352219089Spjd 3353219089Spjd /* check options */ 3354219089Spjd while ((c = getopt(argc, argv, ":R:no:")) != -1) { 3355219089Spjd switch (c) { 3356219089Spjd case 'R': 3357219089Spjd flags.import = B_TRUE; 3358219089Spjd if (add_prop_list( 3359219089Spjd zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg, 3360219089Spjd &props, B_TRUE) != 0) { 3361219089Spjd if (props) 3362219089Spjd nvlist_free(props); 3363219089Spjd usage(B_FALSE); 3364219089Spjd } 3365219089Spjd break; 3366219089Spjd case 'n': 3367219089Spjd flags.dryrun = B_TRUE; 3368219089Spjd break; 3369219089Spjd case 'o': 3370219089Spjd if ((propval = strchr(optarg, '=')) != NULL) { 3371219089Spjd *propval = '\0'; 3372219089Spjd propval++; 3373219089Spjd if (add_prop_list(optarg, propval, 3374219089Spjd &props, B_TRUE) != 0) { 3375219089Spjd if (props) 3376219089Spjd nvlist_free(props); 3377219089Spjd usage(B_FALSE); 3378219089Spjd } 3379219089Spjd } else { 3380219089Spjd mntopts = optarg; 3381219089Spjd } 3382219089Spjd break; 3383219089Spjd case ':': 3384219089Spjd (void) fprintf(stderr, gettext("missing argument for " 3385219089Spjd "'%c' option\n"), optopt); 3386219089Spjd usage(B_FALSE); 3387219089Spjd break; 3388219089Spjd case '?': 3389219089Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3390219089Spjd optopt); 3391219089Spjd usage(B_FALSE); 3392219089Spjd break; 3393219089Spjd } 3394219089Spjd } 3395219089Spjd 3396219089Spjd if (!flags.import && mntopts != NULL) { 3397219089Spjd (void) fprintf(stderr, gettext("setting mntopts is only " 3398219089Spjd "valid when importing the pool\n")); 3399219089Spjd usage(B_FALSE); 3400219089Spjd } 3401219089Spjd 3402219089Spjd argc -= optind; 3403219089Spjd argv += optind; 3404219089Spjd 3405219089Spjd if (argc < 1) { 3406219089Spjd (void) fprintf(stderr, gettext("Missing pool name\n")); 3407219089Spjd usage(B_FALSE); 3408219089Spjd } 3409219089Spjd if (argc < 2) { 3410219089Spjd (void) fprintf(stderr, gettext("Missing new pool name\n")); 3411219089Spjd usage(B_FALSE); 3412219089Spjd } 3413219089Spjd 3414219089Spjd srcpool = argv[0]; 3415219089Spjd newpool = argv[1]; 3416219089Spjd 3417219089Spjd argc -= 2; 3418219089Spjd argv += 2; 3419219089Spjd 3420219089Spjd if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) 3421219089Spjd return (1); 3422219089Spjd 3423219089Spjd config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv); 3424219089Spjd if (config == NULL) { 3425219089Spjd ret = 1; 3426219089Spjd } else { 3427219089Spjd if (flags.dryrun) { 3428219089Spjd (void) printf(gettext("would create '%s' with the " 3429219089Spjd "following layout:\n\n"), newpool); 3430219089Spjd print_vdev_tree(NULL, newpool, config, 0, B_FALSE); 3431219089Spjd } 3432219089Spjd nvlist_free(config); 3433219089Spjd } 3434219089Spjd 3435219089Spjd zpool_close(zhp); 3436219089Spjd 3437219089Spjd if (ret != 0 || flags.dryrun || !flags.import) 3438219089Spjd return (ret); 3439219089Spjd 3440219089Spjd /* 3441219089Spjd * The split was successful. Now we need to open the new 3442219089Spjd * pool and import it. 3443219089Spjd */ 3444219089Spjd if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) 3445219089Spjd return (1); 3446219089Spjd if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL && 3447219089Spjd zpool_enable_datasets(zhp, mntopts, 0) != 0) { 3448219089Spjd ret = 1; 3449240415Smm (void) fprintf(stderr, gettext("Split was successful, but " 3450219089Spjd "the datasets could not all be mounted\n")); 3451219089Spjd (void) fprintf(stderr, gettext("Try doing '%s' with a " 3452219089Spjd "different altroot\n"), "zpool import"); 3453219089Spjd } 3454219089Spjd zpool_close(zhp); 3455219089Spjd 3456219089Spjd return (ret); 3457219089Spjd} 3458219089Spjd 3459219089Spjd 3460219089Spjd 3461219089Spjd/* 3462168404Spjd * zpool online <pool> <device> ... 3463168404Spjd */ 3464168404Spjdint 3465168404Spjdzpool_do_online(int argc, char **argv) 3466168404Spjd{ 3467168404Spjd int c, i; 3468168404Spjd char *poolname; 3469168404Spjd zpool_handle_t *zhp; 3470168404Spjd int ret = 0; 3471185029Spjd vdev_state_t newstate; 3472219089Spjd int flags = 0; 3473168404Spjd 3474168404Spjd /* check options */ 3475219089Spjd while ((c = getopt(argc, argv, "et")) != -1) { 3476168404Spjd switch (c) { 3477219089Spjd case 'e': 3478219089Spjd flags |= ZFS_ONLINE_EXPAND; 3479219089Spjd break; 3480168404Spjd case 't': 3481168404Spjd case '?': 3482168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3483168404Spjd optopt); 3484168404Spjd usage(B_FALSE); 3485168404Spjd } 3486168404Spjd } 3487168404Spjd 3488168404Spjd argc -= optind; 3489168404Spjd argv += optind; 3490168404Spjd 3491168404Spjd /* get pool name and check number of arguments */ 3492168404Spjd if (argc < 1) { 3493168404Spjd (void) fprintf(stderr, gettext("missing pool name\n")); 3494168404Spjd usage(B_FALSE); 3495168404Spjd } 3496168404Spjd if (argc < 2) { 3497168404Spjd (void) fprintf(stderr, gettext("missing device name\n")); 3498168404Spjd usage(B_FALSE); 3499168404Spjd } 3500168404Spjd 3501168404Spjd poolname = argv[0]; 3502168404Spjd 3503168404Spjd if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3504168404Spjd return (1); 3505168404Spjd 3506185029Spjd for (i = 1; i < argc; i++) { 3507219089Spjd if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) { 3508185029Spjd if (newstate != VDEV_STATE_HEALTHY) { 3509185029Spjd (void) printf(gettext("warning: device '%s' " 3510185029Spjd "onlined, but remains in faulted state\n"), 3511185029Spjd argv[i]); 3512185029Spjd if (newstate == VDEV_STATE_FAULTED) 3513185029Spjd (void) printf(gettext("use 'zpool " 3514185029Spjd "clear' to restore a faulted " 3515185029Spjd "device\n")); 3516185029Spjd else 3517185029Spjd (void) printf(gettext("use 'zpool " 3518185029Spjd "replace' to replace devices " 3519185029Spjd "that are no longer present\n")); 3520185029Spjd } 3521185029Spjd } else { 3522168404Spjd ret = 1; 3523185029Spjd } 3524185029Spjd } 3525168404Spjd 3526168404Spjd zpool_close(zhp); 3527168404Spjd 3528168404Spjd return (ret); 3529168404Spjd} 3530168404Spjd 3531168404Spjd/* 3532168404Spjd * zpool offline [-ft] <pool> <device> ... 3533168404Spjd * 3534168404Spjd * -f Force the device into the offline state, even if doing 3535168404Spjd * so would appear to compromise pool availability. 3536168404Spjd * (not supported yet) 3537168404Spjd * 3538168404Spjd * -t Only take the device off-line temporarily. The offline 3539168404Spjd * state will not be persistent across reboots. 3540168404Spjd */ 3541168404Spjd/* ARGSUSED */ 3542168404Spjdint 3543168404Spjdzpool_do_offline(int argc, char **argv) 3544168404Spjd{ 3545168404Spjd int c, i; 3546168404Spjd char *poolname; 3547168404Spjd zpool_handle_t *zhp; 3548168404Spjd int ret = 0; 3549168404Spjd boolean_t istmp = B_FALSE; 3550168404Spjd 3551168404Spjd /* check options */ 3552168404Spjd while ((c = getopt(argc, argv, "ft")) != -1) { 3553168404Spjd switch (c) { 3554168404Spjd case 't': 3555168404Spjd istmp = B_TRUE; 3556168404Spjd break; 3557168404Spjd case 'f': 3558168404Spjd case '?': 3559168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3560168404Spjd optopt); 3561168404Spjd usage(B_FALSE); 3562168404Spjd } 3563168404Spjd } 3564168404Spjd 3565168404Spjd argc -= optind; 3566168404Spjd argv += optind; 3567168404Spjd 3568168404Spjd /* get pool name and check number of arguments */ 3569168404Spjd if (argc < 1) { 3570168404Spjd (void) fprintf(stderr, gettext("missing pool name\n")); 3571168404Spjd usage(B_FALSE); 3572168404Spjd } 3573168404Spjd if (argc < 2) { 3574168404Spjd (void) fprintf(stderr, gettext("missing device name\n")); 3575168404Spjd usage(B_FALSE); 3576168404Spjd } 3577168404Spjd 3578168404Spjd poolname = argv[0]; 3579168404Spjd 3580168404Spjd if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3581168404Spjd return (1); 3582168404Spjd 3583185029Spjd for (i = 1; i < argc; i++) { 3584185029Spjd if (zpool_vdev_offline(zhp, argv[i], istmp) != 0) 3585168404Spjd ret = 1; 3586185029Spjd } 3587168404Spjd 3588168404Spjd zpool_close(zhp); 3589168404Spjd 3590168404Spjd return (ret); 3591168404Spjd} 3592168404Spjd 3593168404Spjd/* 3594168404Spjd * zpool clear <pool> [device] 3595168404Spjd * 3596168404Spjd * Clear all errors associated with a pool or a particular device. 3597168404Spjd */ 3598168404Spjdint 3599168404Spjdzpool_do_clear(int argc, char **argv) 3600168404Spjd{ 3601219089Spjd int c; 3602168404Spjd int ret = 0; 3603219089Spjd boolean_t dryrun = B_FALSE; 3604219089Spjd boolean_t do_rewind = B_FALSE; 3605219089Spjd boolean_t xtreme_rewind = B_FALSE; 3606219089Spjd uint32_t rewind_policy = ZPOOL_NO_REWIND; 3607219089Spjd nvlist_t *policy = NULL; 3608168404Spjd zpool_handle_t *zhp; 3609168404Spjd char *pool, *device; 3610168404Spjd 3611219089Spjd /* check options */ 3612219089Spjd while ((c = getopt(argc, argv, "FnX")) != -1) { 3613219089Spjd switch (c) { 3614219089Spjd case 'F': 3615219089Spjd do_rewind = B_TRUE; 3616219089Spjd break; 3617219089Spjd case 'n': 3618219089Spjd dryrun = B_TRUE; 3619219089Spjd break; 3620219089Spjd case 'X': 3621219089Spjd xtreme_rewind = B_TRUE; 3622219089Spjd break; 3623219089Spjd case '?': 3624219089Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3625219089Spjd optopt); 3626219089Spjd usage(B_FALSE); 3627219089Spjd } 3628219089Spjd } 3629219089Spjd 3630219089Spjd argc -= optind; 3631219089Spjd argv += optind; 3632219089Spjd 3633219089Spjd if (argc < 1) { 3634168404Spjd (void) fprintf(stderr, gettext("missing pool name\n")); 3635168404Spjd usage(B_FALSE); 3636168404Spjd } 3637168404Spjd 3638219089Spjd if (argc > 2) { 3639168404Spjd (void) fprintf(stderr, gettext("too many arguments\n")); 3640168404Spjd usage(B_FALSE); 3641168404Spjd } 3642168404Spjd 3643219089Spjd if ((dryrun || xtreme_rewind) && !do_rewind) { 3644219089Spjd (void) fprintf(stderr, 3645219089Spjd gettext("-n or -X only meaningful with -F\n")); 3646219089Spjd usage(B_FALSE); 3647219089Spjd } 3648219089Spjd if (dryrun) 3649219089Spjd rewind_policy = ZPOOL_TRY_REWIND; 3650219089Spjd else if (do_rewind) 3651219089Spjd rewind_policy = ZPOOL_DO_REWIND; 3652219089Spjd if (xtreme_rewind) 3653219089Spjd rewind_policy |= ZPOOL_EXTREME_REWIND; 3654168404Spjd 3655219089Spjd /* In future, further rewind policy choices can be passed along here */ 3656219089Spjd if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 || 3657219089Spjd nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0) 3658168404Spjd return (1); 3659168404Spjd 3660219089Spjd pool = argv[0]; 3661219089Spjd device = argc == 2 ? argv[1] : NULL; 3662219089Spjd 3663219089Spjd if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) { 3664219089Spjd nvlist_free(policy); 3665219089Spjd return (1); 3666219089Spjd } 3667219089Spjd 3668219089Spjd if (zpool_clear(zhp, device, policy) != 0) 3669168404Spjd ret = 1; 3670168404Spjd 3671168404Spjd zpool_close(zhp); 3672168404Spjd 3673219089Spjd nvlist_free(policy); 3674219089Spjd 3675168404Spjd return (ret); 3676168404Spjd} 3677168404Spjd 3678228103Smm/* 3679228103Smm * zpool reguid <pool> 3680228103Smm */ 3681228103Smmint 3682228103Smmzpool_do_reguid(int argc, char **argv) 3683228103Smm{ 3684228103Smm int c; 3685228103Smm char *poolname; 3686228103Smm zpool_handle_t *zhp; 3687228103Smm int ret = 0; 3688228103Smm 3689228103Smm /* check options */ 3690228103Smm while ((c = getopt(argc, argv, "")) != -1) { 3691228103Smm switch (c) { 3692228103Smm case '?': 3693228103Smm (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3694228103Smm optopt); 3695228103Smm usage(B_FALSE); 3696228103Smm } 3697228103Smm } 3698228103Smm 3699228103Smm argc -= optind; 3700228103Smm argv += optind; 3701228103Smm 3702228103Smm /* get pool name and check number of arguments */ 3703228103Smm if (argc < 1) { 3704228103Smm (void) fprintf(stderr, gettext("missing pool name\n")); 3705228103Smm usage(B_FALSE); 3706228103Smm } 3707228103Smm 3708228103Smm if (argc > 1) { 3709228103Smm (void) fprintf(stderr, gettext("too many arguments\n")); 3710228103Smm usage(B_FALSE); 3711228103Smm } 3712228103Smm 3713228103Smm poolname = argv[0]; 3714228103Smm if ((zhp = zpool_open(g_zfs, poolname)) == NULL) 3715228103Smm return (1); 3716228103Smm 3717228103Smm ret = zpool_reguid(zhp); 3718228103Smm 3719228103Smm zpool_close(zhp); 3720228103Smm return (ret); 3721228103Smm} 3722228103Smm 3723228103Smm 3724236155Smm/* 3725236155Smm * zpool reopen <pool> 3726236155Smm * 3727236155Smm * Reopen the pool so that the kernel can update the sizes of all vdevs. 3728236155Smm */ 3729236155Smmint 3730236155Smmzpool_do_reopen(int argc, char **argv) 3731236155Smm{ 3732260138Sdelphij int c; 3733236155Smm int ret = 0; 3734236155Smm zpool_handle_t *zhp; 3735236155Smm char *pool; 3736236155Smm 3737260138Sdelphij /* check options */ 3738260138Sdelphij while ((c = getopt(argc, argv, "")) != -1) { 3739260138Sdelphij switch (c) { 3740260138Sdelphij case '?': 3741260138Sdelphij (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3742260138Sdelphij optopt); 3743260138Sdelphij usage(B_FALSE); 3744260138Sdelphij } 3745260138Sdelphij } 3746260138Sdelphij 3747236155Smm argc--; 3748236155Smm argv++; 3749236155Smm 3750260138Sdelphij if (argc < 1) { 3751260138Sdelphij (void) fprintf(stderr, gettext("missing pool name\n")); 3752260138Sdelphij usage(B_FALSE); 3753260138Sdelphij } 3754236155Smm 3755260138Sdelphij if (argc > 1) { 3756260138Sdelphij (void) fprintf(stderr, gettext("too many arguments\n")); 3757260138Sdelphij usage(B_FALSE); 3758260138Sdelphij } 3759260138Sdelphij 3760236155Smm pool = argv[0]; 3761236155Smm if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) 3762236155Smm return (1); 3763236155Smm 3764236155Smm ret = zpool_reopen(zhp); 3765236155Smm zpool_close(zhp); 3766236155Smm return (ret); 3767236155Smm} 3768236155Smm 3769168404Spjdtypedef struct scrub_cbdata { 3770168404Spjd int cb_type; 3771168404Spjd int cb_argc; 3772168404Spjd char **cb_argv; 3773168404Spjd} scrub_cbdata_t; 3774168404Spjd 3775168404Spjdint 3776168404Spjdscrub_callback(zpool_handle_t *zhp, void *data) 3777168404Spjd{ 3778168404Spjd scrub_cbdata_t *cb = data; 3779168404Spjd int err; 3780168404Spjd 3781168404Spjd /* 3782168404Spjd * Ignore faulted pools. 3783168404Spjd */ 3784168404Spjd if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 3785168404Spjd (void) fprintf(stderr, gettext("cannot scrub '%s': pool is " 3786168404Spjd "currently unavailable\n"), zpool_get_name(zhp)); 3787168404Spjd return (1); 3788168404Spjd } 3789168404Spjd 3790219089Spjd err = zpool_scan(zhp, cb->cb_type); 3791168404Spjd 3792168404Spjd return (err != 0); 3793168404Spjd} 3794168404Spjd 3795168404Spjd/* 3796168404Spjd * zpool scrub [-s] <pool> ... 3797168404Spjd * 3798168404Spjd * -s Stop. Stops any in-progress scrub. 3799168404Spjd */ 3800168404Spjdint 3801168404Spjdzpool_do_scrub(int argc, char **argv) 3802168404Spjd{ 3803168404Spjd int c; 3804168404Spjd scrub_cbdata_t cb; 3805168404Spjd 3806219089Spjd cb.cb_type = POOL_SCAN_SCRUB; 3807168404Spjd 3808168404Spjd /* check options */ 3809168404Spjd while ((c = getopt(argc, argv, "s")) != -1) { 3810168404Spjd switch (c) { 3811168404Spjd case 's': 3812219089Spjd cb.cb_type = POOL_SCAN_NONE; 3813168404Spjd break; 3814168404Spjd case '?': 3815168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3816168404Spjd optopt); 3817168404Spjd usage(B_FALSE); 3818168404Spjd } 3819168404Spjd } 3820168404Spjd 3821168404Spjd cb.cb_argc = argc; 3822168404Spjd cb.cb_argv = argv; 3823168404Spjd argc -= optind; 3824168404Spjd argv += optind; 3825168404Spjd 3826168404Spjd if (argc < 1) { 3827168404Spjd (void) fprintf(stderr, gettext("missing pool name argument\n")); 3828168404Spjd usage(B_FALSE); 3829168404Spjd } 3830168404Spjd 3831168404Spjd return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb)); 3832168404Spjd} 3833168404Spjd 3834168404Spjdtypedef struct status_cbdata { 3835168404Spjd int cb_count; 3836168404Spjd boolean_t cb_allpools; 3837168404Spjd boolean_t cb_verbose; 3838168404Spjd boolean_t cb_explain; 3839168404Spjd boolean_t cb_first; 3840219089Spjd boolean_t cb_dedup_stats; 3841168404Spjd} status_cbdata_t; 3842168404Spjd 3843168404Spjd/* 3844168404Spjd * Print out detailed scrub status. 3845168404Spjd */ 3846168404Spjdvoid 3847219089Spjdprint_scan_status(pool_scan_stat_t *ps) 3848168404Spjd{ 3849219089Spjd time_t start, end; 3850219089Spjd uint64_t elapsed, mins_left, hours_left; 3851219089Spjd uint64_t pass_exam, examined, total; 3852219089Spjd uint_t rate; 3853168404Spjd double fraction_done; 3854219089Spjd char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7]; 3855168404Spjd 3856226583Spjd (void) printf(gettext(" scan: ")); 3857168404Spjd 3858219089Spjd /* If there's never been a scan, there's not much to say. */ 3859219089Spjd if (ps == NULL || ps->pss_func == POOL_SCAN_NONE || 3860219089Spjd ps->pss_func >= POOL_SCAN_FUNCS) { 3861168404Spjd (void) printf(gettext("none requested\n")); 3862168404Spjd return; 3863168404Spjd } 3864168404Spjd 3865219089Spjd start = ps->pss_start_time; 3866219089Spjd end = ps->pss_end_time; 3867219089Spjd zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf)); 3868168404Spjd 3869219089Spjd assert(ps->pss_func == POOL_SCAN_SCRUB || 3870219089Spjd ps->pss_func == POOL_SCAN_RESILVER); 3871219089Spjd /* 3872219089Spjd * Scan is finished or canceled. 3873219089Spjd */ 3874219089Spjd if (ps->pss_state == DSS_FINISHED) { 3875219089Spjd uint64_t minutes_taken = (end - start) / 60; 3876219089Spjd char *fmt; 3877168404Spjd 3878219089Spjd if (ps->pss_func == POOL_SCAN_SCRUB) { 3879219089Spjd fmt = gettext("scrub repaired %s in %lluh%um with " 3880219089Spjd "%llu errors on %s"); 3881219089Spjd } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3882219089Spjd fmt = gettext("resilvered %s in %lluh%um with " 3883219089Spjd "%llu errors on %s"); 3884219089Spjd } 3885219089Spjd /* LINTED */ 3886219089Spjd (void) printf(fmt, processed_buf, 3887185029Spjd (u_longlong_t)(minutes_taken / 60), 3888185029Spjd (uint_t)(minutes_taken % 60), 3889219089Spjd (u_longlong_t)ps->pss_errors, 3890219089Spjd ctime((time_t *)&end)); 3891168404Spjd return; 3892219089Spjd } else if (ps->pss_state == DSS_CANCELED) { 3893219089Spjd if (ps->pss_func == POOL_SCAN_SCRUB) { 3894219089Spjd (void) printf(gettext("scrub canceled on %s"), 3895219089Spjd ctime(&end)); 3896219089Spjd } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3897219089Spjd (void) printf(gettext("resilver canceled on %s"), 3898219089Spjd ctime(&end)); 3899219089Spjd } 3900219089Spjd return; 3901168404Spjd } 3902168404Spjd 3903219089Spjd assert(ps->pss_state == DSS_SCANNING); 3904168404Spjd 3905219089Spjd /* 3906219089Spjd * Scan is in progress. 3907219089Spjd */ 3908219089Spjd if (ps->pss_func == POOL_SCAN_SCRUB) { 3909219089Spjd (void) printf(gettext("scrub in progress since %s"), 3910219089Spjd ctime(&start)); 3911219089Spjd } else if (ps->pss_func == POOL_SCAN_RESILVER) { 3912219089Spjd (void) printf(gettext("resilver in progress since %s"), 3913219089Spjd ctime(&start)); 3914219089Spjd } 3915219089Spjd 3916219089Spjd examined = ps->pss_examined ? ps->pss_examined : 1; 3917219089Spjd total = ps->pss_to_examine; 3918168404Spjd fraction_done = (double)examined / total; 3919168404Spjd 3920219089Spjd /* elapsed time for this pass */ 3921219089Spjd elapsed = time(NULL) - ps->pss_pass_start; 3922219089Spjd elapsed = elapsed ? elapsed : 1; 3923219089Spjd pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1; 3924219089Spjd rate = pass_exam / elapsed; 3925219089Spjd rate = rate ? rate : 1; 3926219089Spjd mins_left = ((total - examined) / rate) / 60; 3927219089Spjd hours_left = mins_left / 60; 3928219089Spjd 3929219089Spjd zfs_nicenum(examined, examined_buf, sizeof (examined_buf)); 3930219089Spjd zfs_nicenum(total, total_buf, sizeof (total_buf)); 3931219089Spjd zfs_nicenum(rate, rate_buf, sizeof (rate_buf)); 3932219089Spjd 3933219089Spjd /* 3934219089Spjd * do not print estimated time if hours_left is more than 30 days 3935219089Spjd */ 3936226583Spjd (void) printf(gettext(" %s scanned out of %s at %s/s"), 3937219089Spjd examined_buf, total_buf, rate_buf); 3938219089Spjd if (hours_left < (30 * 24)) { 3939219089Spjd (void) printf(gettext(", %lluh%um to go\n"), 3940219089Spjd (u_longlong_t)hours_left, (uint_t)(mins_left % 60)); 3941219089Spjd } else { 3942219089Spjd (void) printf(gettext( 3943219089Spjd ", (scan is slow, no estimated time)\n")); 3944219089Spjd } 3945219089Spjd 3946219089Spjd if (ps->pss_func == POOL_SCAN_RESILVER) { 3947226583Spjd (void) printf(gettext(" %s resilvered, %.2f%% done\n"), 3948219089Spjd processed_buf, 100 * fraction_done); 3949219089Spjd } else if (ps->pss_func == POOL_SCAN_SCRUB) { 3950226583Spjd (void) printf(gettext(" %s repaired, %.2f%% done\n"), 3951219089Spjd processed_buf, 100 * fraction_done); 3952219089Spjd } 3953168404Spjd} 3954168404Spjd 3955168404Spjdstatic void 3956168404Spjdprint_error_log(zpool_handle_t *zhp) 3957168404Spjd{ 3958185029Spjd nvlist_t *nverrlist = NULL; 3959168404Spjd nvpair_t *elem; 3960168404Spjd char *pathname; 3961168404Spjd size_t len = MAXPATHLEN * 2; 3962168404Spjd 3963168404Spjd if (zpool_get_errlog(zhp, &nverrlist) != 0) { 3964168404Spjd (void) printf("errors: List of errors unavailable " 3965168404Spjd "(insufficient privileges)\n"); 3966168404Spjd return; 3967168404Spjd } 3968168404Spjd 3969168404Spjd (void) printf("errors: Permanent errors have been " 3970168404Spjd "detected in the following files:\n\n"); 3971168404Spjd 3972168404Spjd pathname = safe_malloc(len); 3973168404Spjd elem = NULL; 3974168404Spjd while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) { 3975168404Spjd nvlist_t *nv; 3976168404Spjd uint64_t dsobj, obj; 3977168404Spjd 3978168404Spjd verify(nvpair_value_nvlist(elem, &nv) == 0); 3979168404Spjd verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET, 3980168404Spjd &dsobj) == 0); 3981168404Spjd verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT, 3982168404Spjd &obj) == 0); 3983168404Spjd zpool_obj_to_path(zhp, dsobj, obj, pathname, len); 3984168404Spjd (void) printf("%7s %s\n", "", pathname); 3985168404Spjd } 3986168404Spjd free(pathname); 3987168404Spjd nvlist_free(nverrlist); 3988168404Spjd} 3989168404Spjd 3990168404Spjdstatic void 3991168404Spjdprint_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares, 3992168404Spjd int namewidth) 3993168404Spjd{ 3994168404Spjd uint_t i; 3995168404Spjd char *name; 3996168404Spjd 3997168404Spjd if (nspares == 0) 3998168404Spjd return; 3999168404Spjd 4000168404Spjd (void) printf(gettext("\tspares\n")); 4001168404Spjd 4002168404Spjd for (i = 0; i < nspares; i++) { 4003219089Spjd name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE); 4004168404Spjd print_status_config(zhp, name, spares[i], 4005209962Smm namewidth, 2, B_TRUE); 4006168404Spjd free(name); 4007168404Spjd } 4008168404Spjd} 4009168404Spjd 4010185029Spjdstatic void 4011185029Spjdprint_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache, 4012185029Spjd int namewidth) 4013185029Spjd{ 4014185029Spjd uint_t i; 4015185029Spjd char *name; 4016185029Spjd 4017185029Spjd if (nl2cache == 0) 4018185029Spjd return; 4019185029Spjd 4020185029Spjd (void) printf(gettext("\tcache\n")); 4021185029Spjd 4022185029Spjd for (i = 0; i < nl2cache; i++) { 4023219089Spjd name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE); 4024185029Spjd print_status_config(zhp, name, l2cache[i], 4025209962Smm namewidth, 2, B_FALSE); 4026185029Spjd free(name); 4027185029Spjd } 4028185029Spjd} 4029185029Spjd 4030219089Spjdstatic void 4031219089Spjdprint_dedup_stats(nvlist_t *config) 4032219089Spjd{ 4033219089Spjd ddt_histogram_t *ddh; 4034219089Spjd ddt_stat_t *dds; 4035219089Spjd ddt_object_t *ddo; 4036219089Spjd uint_t c; 4037219089Spjd 4038219089Spjd /* 4039219089Spjd * If the pool was faulted then we may not have been able to 4040253441Sdelphij * obtain the config. Otherwise, if we have anything in the dedup 4041219089Spjd * table continue processing the stats. 4042219089Spjd */ 4043219089Spjd if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS, 4044227497Smm (uint64_t **)&ddo, &c) != 0) 4045219089Spjd return; 4046219089Spjd 4047219089Spjd (void) printf("\n"); 4048227497Smm (void) printf(gettext(" dedup: ")); 4049227497Smm if (ddo->ddo_count == 0) { 4050227497Smm (void) printf(gettext("no DDT entries\n")); 4051227497Smm return; 4052227497Smm } 4053227497Smm 4054219089Spjd (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n", 4055219089Spjd (u_longlong_t)ddo->ddo_count, 4056219089Spjd (u_longlong_t)ddo->ddo_dspace, 4057219089Spjd (u_longlong_t)ddo->ddo_mspace); 4058219089Spjd 4059219089Spjd verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS, 4060219089Spjd (uint64_t **)&dds, &c) == 0); 4061219089Spjd verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM, 4062219089Spjd (uint64_t **)&ddh, &c) == 0); 4063219089Spjd zpool_dump_ddt(dds, ddh); 4064219089Spjd} 4065219089Spjd 4066168404Spjd/* 4067168404Spjd * Display a summary of pool status. Displays a summary such as: 4068168404Spjd * 4069168404Spjd * pool: tank 4070168404Spjd * status: DEGRADED 4071168404Spjd * reason: One or more devices ... 4072236146Smm * see: http://illumos.org/msg/ZFS-xxxx-01 4073168404Spjd * config: 4074168404Spjd * mirror DEGRADED 4075168404Spjd * c1t0d0 OK 4076168404Spjd * c2t0d0 UNAVAIL 4077168404Spjd * 4078168404Spjd * When given the '-v' option, we print out the complete config. If the '-e' 4079168404Spjd * option is specified, then we print out error rate information as well. 4080168404Spjd */ 4081168404Spjdint 4082168404Spjdstatus_callback(zpool_handle_t *zhp, void *data) 4083168404Spjd{ 4084168404Spjd status_cbdata_t *cbp = data; 4085168404Spjd nvlist_t *config, *nvroot; 4086168404Spjd char *msgid; 4087168404Spjd int reason; 4088168404Spjd const char *health; 4089168404Spjd uint_t c; 4090168404Spjd vdev_stat_t *vs; 4091168404Spjd 4092168404Spjd config = zpool_get_config(zhp, NULL); 4093168404Spjd reason = zpool_get_status(zhp, &msgid); 4094168404Spjd 4095168404Spjd cbp->cb_count++; 4096168404Spjd 4097168404Spjd /* 4098168404Spjd * If we were given 'zpool status -x', only report those pools with 4099168404Spjd * problems. 4100168404Spjd */ 4101248267Smm if (cbp->cb_explain && 4102248267Smm (reason == ZPOOL_STATUS_OK || 4103248267Smm reason == ZPOOL_STATUS_VERSION_OLDER || 4104268621Ssmh reason == ZPOOL_STATUS_NON_NATIVE_ASHIFT || 4105248267Smm reason == ZPOOL_STATUS_FEAT_DISABLED)) { 4106168404Spjd if (!cbp->cb_allpools) { 4107168404Spjd (void) printf(gettext("pool '%s' is healthy\n"), 4108168404Spjd zpool_get_name(zhp)); 4109168404Spjd if (cbp->cb_first) 4110168404Spjd cbp->cb_first = B_FALSE; 4111168404Spjd } 4112168404Spjd return (0); 4113168404Spjd } 4114168404Spjd 4115168404Spjd if (cbp->cb_first) 4116168404Spjd cbp->cb_first = B_FALSE; 4117168404Spjd else 4118168404Spjd (void) printf("\n"); 4119168404Spjd 4120168404Spjd verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4121168404Spjd &nvroot) == 0); 4122219089Spjd verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 4123168404Spjd (uint64_t **)&vs, &c) == 0); 4124185029Spjd health = zpool_state_to_name(vs->vs_state, vs->vs_aux); 4125168404Spjd 4126168404Spjd (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp)); 4127168404Spjd (void) printf(gettext(" state: %s\n"), health); 4128168404Spjd 4129168404Spjd switch (reason) { 4130168404Spjd case ZPOOL_STATUS_MISSING_DEV_R: 4131168404Spjd (void) printf(gettext("status: One or more devices could not " 4132168404Spjd "be opened. Sufficient replicas exist for\n\tthe pool to " 4133168404Spjd "continue functioning in a degraded state.\n")); 4134168404Spjd (void) printf(gettext("action: Attach the missing device and " 4135168404Spjd "online it using 'zpool online'.\n")); 4136168404Spjd break; 4137168404Spjd 4138168404Spjd case ZPOOL_STATUS_MISSING_DEV_NR: 4139168404Spjd (void) printf(gettext("status: One or more devices could not " 4140168404Spjd "be opened. There are insufficient\n\treplicas for the " 4141168404Spjd "pool to continue functioning.\n")); 4142168404Spjd (void) printf(gettext("action: Attach the missing device and " 4143168404Spjd "online it using 'zpool online'.\n")); 4144168404Spjd break; 4145168404Spjd 4146168404Spjd case ZPOOL_STATUS_CORRUPT_LABEL_R: 4147168404Spjd (void) printf(gettext("status: One or more devices could not " 4148168404Spjd "be used because the label is missing or\n\tinvalid. " 4149168404Spjd "Sufficient replicas exist for the pool to continue\n\t" 4150168404Spjd "functioning in a degraded state.\n")); 4151168404Spjd (void) printf(gettext("action: Replace the device using " 4152168404Spjd "'zpool replace'.\n")); 4153168404Spjd break; 4154168404Spjd 4155168404Spjd case ZPOOL_STATUS_CORRUPT_LABEL_NR: 4156168404Spjd (void) printf(gettext("status: One or more devices could not " 4157168404Spjd "be used because the label is missing \n\tor invalid. " 4158168404Spjd "There are insufficient replicas for the pool to " 4159168404Spjd "continue\n\tfunctioning.\n")); 4160219089Spjd zpool_explain_recover(zpool_get_handle(zhp), 4161219089Spjd zpool_get_name(zhp), reason, config); 4162168404Spjd break; 4163168404Spjd 4164168404Spjd case ZPOOL_STATUS_FAILING_DEV: 4165168404Spjd (void) printf(gettext("status: One or more devices has " 4166168404Spjd "experienced an unrecoverable error. An\n\tattempt was " 4167168404Spjd "made to correct the error. Applications are " 4168168404Spjd "unaffected.\n")); 4169168404Spjd (void) printf(gettext("action: Determine if the device needs " 4170168404Spjd "to be replaced, and clear the errors\n\tusing " 4171168404Spjd "'zpool clear' or replace the device with 'zpool " 4172168404Spjd "replace'.\n")); 4173168404Spjd break; 4174168404Spjd 4175168404Spjd case ZPOOL_STATUS_OFFLINE_DEV: 4176168404Spjd (void) printf(gettext("status: One or more devices has " 4177168404Spjd "been taken offline by the administrator.\n\tSufficient " 4178168404Spjd "replicas exist for the pool to continue functioning in " 4179168404Spjd "a\n\tdegraded state.\n")); 4180168404Spjd (void) printf(gettext("action: Online the device using " 4181168404Spjd "'zpool online' or replace the device with\n\t'zpool " 4182168404Spjd "replace'.\n")); 4183168404Spjd break; 4184168404Spjd 4185219089Spjd case ZPOOL_STATUS_REMOVED_DEV: 4186219089Spjd (void) printf(gettext("status: One or more devices has " 4187219089Spjd "been removed by the administrator.\n\tSufficient " 4188219089Spjd "replicas exist for the pool to continue functioning in " 4189219089Spjd "a\n\tdegraded state.\n")); 4190219089Spjd (void) printf(gettext("action: Online the device using " 4191219089Spjd "'zpool online' or replace the device with\n\t'zpool " 4192219089Spjd "replace'.\n")); 4193219089Spjd break; 4194219089Spjd 4195168404Spjd case ZPOOL_STATUS_RESILVERING: 4196168404Spjd (void) printf(gettext("status: One or more devices is " 4197168404Spjd "currently being resilvered. The pool will\n\tcontinue " 4198168404Spjd "to function, possibly in a degraded state.\n")); 4199168404Spjd (void) printf(gettext("action: Wait for the resilver to " 4200168404Spjd "complete.\n")); 4201168404Spjd break; 4202168404Spjd 4203168404Spjd case ZPOOL_STATUS_CORRUPT_DATA: 4204168404Spjd (void) printf(gettext("status: One or more devices has " 4205168404Spjd "experienced an error resulting in data\n\tcorruption. " 4206168404Spjd "Applications may be affected.\n")); 4207168404Spjd (void) printf(gettext("action: Restore the file in question " 4208168404Spjd "if possible. Otherwise restore the\n\tentire pool from " 4209168404Spjd "backup.\n")); 4210168404Spjd break; 4211168404Spjd 4212168404Spjd case ZPOOL_STATUS_CORRUPT_POOL: 4213168404Spjd (void) printf(gettext("status: The pool metadata is corrupted " 4214168404Spjd "and the pool cannot be opened.\n")); 4215219089Spjd zpool_explain_recover(zpool_get_handle(zhp), 4216219089Spjd zpool_get_name(zhp), reason, config); 4217168404Spjd break; 4218168404Spjd 4219168404Spjd case ZPOOL_STATUS_VERSION_OLDER: 4220238926Smm (void) printf(gettext("status: The pool is formatted using a " 4221238926Smm "legacy on-disk format. The pool can\n\tstill be used, " 4222238926Smm "but some features are unavailable.\n")); 4223168404Spjd (void) printf(gettext("action: Upgrade the pool using 'zpool " 4224168404Spjd "upgrade'. Once this is done, the\n\tpool will no longer " 4225238926Smm "be accessible on software that does not support feature\n" 4226238926Smm "\tflags.\n")); 4227168404Spjd break; 4228168404Spjd 4229168404Spjd case ZPOOL_STATUS_VERSION_NEWER: 4230168404Spjd (void) printf(gettext("status: The pool has been upgraded to a " 4231168404Spjd "newer, incompatible on-disk version.\n\tThe pool cannot " 4232168404Spjd "be accessed on this system.\n")); 4233168404Spjd (void) printf(gettext("action: Access the pool from a system " 4234168404Spjd "running more recent software, or\n\trestore the pool from " 4235168404Spjd "backup.\n")); 4236168404Spjd break; 4237168404Spjd 4238238926Smm case ZPOOL_STATUS_FEAT_DISABLED: 4239238926Smm (void) printf(gettext("status: Some supported features are not " 4240238926Smm "enabled on the pool. The pool can\n\tstill be used, but " 4241238926Smm "some features are unavailable.\n")); 4242238926Smm (void) printf(gettext("action: Enable all features using " 4243238926Smm "'zpool upgrade'. Once this is done,\n\tthe pool may no " 4244238926Smm "longer be accessible by software that does not support\n\t" 4245243014Smm "the features. See zpool-features(7) for details.\n")); 4246238926Smm break; 4247238926Smm 4248236884Smm case ZPOOL_STATUS_UNSUP_FEAT_READ: 4249236884Smm (void) printf(gettext("status: The pool cannot be accessed on " 4250236884Smm "this system because it uses the\n\tfollowing feature(s) " 4251236884Smm "not supported on this system:\n")); 4252236884Smm zpool_print_unsup_feat(config); 4253236884Smm (void) printf("\n"); 4254236884Smm (void) printf(gettext("action: Access the pool from a system " 4255236884Smm "that supports the required feature(s),\n\tor restore the " 4256236884Smm "pool from backup.\n")); 4257236884Smm break; 4258236884Smm 4259236884Smm case ZPOOL_STATUS_UNSUP_FEAT_WRITE: 4260236884Smm (void) printf(gettext("status: The pool can only be accessed " 4261236884Smm "in read-only mode on this system. It\n\tcannot be " 4262236884Smm "accessed in read-write mode because it uses the " 4263236884Smm "following\n\tfeature(s) not supported on this system:\n")); 4264236884Smm zpool_print_unsup_feat(config); 4265236884Smm (void) printf("\n"); 4266236884Smm (void) printf(gettext("action: The pool cannot be accessed in " 4267236884Smm "read-write mode. Import the pool with\n" 4268236884Smm "\t\"-o readonly=on\", access the pool from a system that " 4269236884Smm "supports the\n\trequired feature(s), or restore the " 4270236884Smm "pool from backup.\n")); 4271236884Smm break; 4272236884Smm 4273185029Spjd case ZPOOL_STATUS_FAULTED_DEV_R: 4274185029Spjd (void) printf(gettext("status: One or more devices are " 4275185029Spjd "faulted in response to persistent errors.\n\tSufficient " 4276185029Spjd "replicas exist for the pool to continue functioning " 4277185029Spjd "in a\n\tdegraded state.\n")); 4278185029Spjd (void) printf(gettext("action: Replace the faulted device, " 4279185029Spjd "or use 'zpool clear' to mark the device\n\trepaired.\n")); 4280185029Spjd break; 4281185029Spjd 4282185029Spjd case ZPOOL_STATUS_FAULTED_DEV_NR: 4283185029Spjd (void) printf(gettext("status: One or more devices are " 4284185029Spjd "faulted in response to persistent errors. There are " 4285185029Spjd "insufficient replicas for the pool to\n\tcontinue " 4286185029Spjd "functioning.\n")); 4287185029Spjd (void) printf(gettext("action: Destroy and re-create the pool " 4288185029Spjd "from a backup source. Manually marking the device\n" 4289185029Spjd "\trepaired using 'zpool clear' may allow some data " 4290185029Spjd "to be recovered.\n")); 4291185029Spjd break; 4292185029Spjd 4293185029Spjd case ZPOOL_STATUS_IO_FAILURE_WAIT: 4294185029Spjd case ZPOOL_STATUS_IO_FAILURE_CONTINUE: 4295185029Spjd (void) printf(gettext("status: One or more devices are " 4296185029Spjd "faulted in response to IO failures.\n")); 4297185029Spjd (void) printf(gettext("action: Make sure the affected devices " 4298185029Spjd "are connected, then run 'zpool clear'.\n")); 4299185029Spjd break; 4300185029Spjd 4301185029Spjd case ZPOOL_STATUS_BAD_LOG: 4302185029Spjd (void) printf(gettext("status: An intent log record " 4303185029Spjd "could not be read.\n" 4304185029Spjd "\tWaiting for adminstrator intervention to fix the " 4305185029Spjd "faulted pool.\n")); 4306185029Spjd (void) printf(gettext("action: Either restore the affected " 4307185029Spjd "device(s) and run 'zpool online',\n" 4308185029Spjd "\tor ignore the intent log records by running " 4309185029Spjd "'zpool clear'.\n")); 4310185029Spjd break; 4311185029Spjd 4312254591Sgibbs case ZPOOL_STATUS_NON_NATIVE_ASHIFT: 4313254591Sgibbs (void) printf(gettext("status: One or more devices are " 4314254591Sgibbs "configured to use a non-native block size.\n" 4315254591Sgibbs "\tExpect reduced performance.\n")); 4316254591Sgibbs (void) printf(gettext("action: Replace affected devices with " 4317254591Sgibbs "devices that support the\n\tconfigured block size, or " 4318254591Sgibbs "migrate data to a properly configured\n\tpool.\n")); 4319254591Sgibbs break; 4320254591Sgibbs 4321168404Spjd default: 4322168404Spjd /* 4323168404Spjd * The remaining errors can't actually be generated, yet. 4324168404Spjd */ 4325168404Spjd assert(reason == ZPOOL_STATUS_OK); 4326168404Spjd } 4327168404Spjd 4328168404Spjd if (msgid != NULL) 4329236146Smm (void) printf(gettext(" see: http://illumos.org/msg/%s\n"), 4330168404Spjd msgid); 4331168404Spjd 4332168404Spjd if (config != NULL) { 4333168404Spjd int namewidth; 4334168404Spjd uint64_t nerr; 4335185029Spjd nvlist_t **spares, **l2cache; 4336185029Spjd uint_t nspares, nl2cache; 4337219089Spjd pool_scan_stat_t *ps = NULL; 4338168404Spjd 4339219089Spjd (void) nvlist_lookup_uint64_array(nvroot, 4340219089Spjd ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c); 4341219089Spjd print_scan_status(ps); 4342168404Spjd 4343168404Spjd namewidth = max_width(zhp, nvroot, 0, 0); 4344168404Spjd if (namewidth < 10) 4345168404Spjd namewidth = 10; 4346168404Spjd 4347168404Spjd (void) printf(gettext("config:\n\n")); 4348168404Spjd (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth, 4349168404Spjd "NAME", "STATE", "READ", "WRITE", "CKSUM"); 4350168404Spjd print_status_config(zhp, zpool_get_name(zhp), nvroot, 4351209962Smm namewidth, 0, B_FALSE); 4352209962Smm 4353185029Spjd if (num_logs(nvroot) > 0) 4354213197Smm print_logs(zhp, nvroot, namewidth, B_TRUE); 4355185029Spjd if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 4356185029Spjd &l2cache, &nl2cache) == 0) 4357185029Spjd print_l2cache(zhp, l2cache, nl2cache, namewidth); 4358185029Spjd 4359168404Spjd if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 4360168404Spjd &spares, &nspares) == 0) 4361168404Spjd print_spares(zhp, spares, nspares, namewidth); 4362168404Spjd 4363168404Spjd if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 4364168404Spjd &nerr) == 0) { 4365168404Spjd nvlist_t *nverrlist = NULL; 4366168404Spjd 4367168404Spjd /* 4368168404Spjd * If the approximate error count is small, get a 4369168404Spjd * precise count by fetching the entire log and 4370168404Spjd * uniquifying the results. 4371168404Spjd */ 4372185029Spjd if (nerr > 0 && nerr < 100 && !cbp->cb_verbose && 4373168404Spjd zpool_get_errlog(zhp, &nverrlist) == 0) { 4374168404Spjd nvpair_t *elem; 4375168404Spjd 4376168404Spjd elem = NULL; 4377168404Spjd nerr = 0; 4378168404Spjd while ((elem = nvlist_next_nvpair(nverrlist, 4379168404Spjd elem)) != NULL) { 4380168404Spjd nerr++; 4381168404Spjd } 4382168404Spjd } 4383168404Spjd nvlist_free(nverrlist); 4384168404Spjd 4385168404Spjd (void) printf("\n"); 4386168404Spjd 4387168404Spjd if (nerr == 0) 4388168404Spjd (void) printf(gettext("errors: No known data " 4389168404Spjd "errors\n")); 4390168404Spjd else if (!cbp->cb_verbose) 4391168404Spjd (void) printf(gettext("errors: %llu data " 4392168404Spjd "errors, use '-v' for a list\n"), 4393168404Spjd (u_longlong_t)nerr); 4394168404Spjd else 4395168404Spjd print_error_log(zhp); 4396168404Spjd } 4397219089Spjd 4398219089Spjd if (cbp->cb_dedup_stats) 4399219089Spjd print_dedup_stats(config); 4400168404Spjd } else { 4401168404Spjd (void) printf(gettext("config: The configuration cannot be " 4402168404Spjd "determined.\n")); 4403168404Spjd } 4404168404Spjd 4405168404Spjd return (0); 4406168404Spjd} 4407168404Spjd 4408168404Spjd/* 4409219089Spjd * zpool status [-vx] [-T d|u] [pool] ... [interval [count]] 4410168404Spjd * 4411168404Spjd * -v Display complete error logs 4412168404Spjd * -x Display only pools with potential problems 4413219089Spjd * -D Display dedup status (undocumented) 4414219089Spjd * -T Display a timestamp in date(1) or Unix format 4415168404Spjd * 4416168404Spjd * Describes the health status of all pools or some subset. 4417168404Spjd */ 4418168404Spjdint 4419168404Spjdzpool_do_status(int argc, char **argv) 4420168404Spjd{ 4421168404Spjd int c; 4422168404Spjd int ret; 4423219089Spjd unsigned long interval = 0, count = 0; 4424168404Spjd status_cbdata_t cb = { 0 }; 4425168404Spjd 4426168404Spjd /* check options */ 4427219089Spjd while ((c = getopt(argc, argv, "vxDT:")) != -1) { 4428168404Spjd switch (c) { 4429168404Spjd case 'v': 4430168404Spjd cb.cb_verbose = B_TRUE; 4431168404Spjd break; 4432168404Spjd case 'x': 4433168404Spjd cb.cb_explain = B_TRUE; 4434168404Spjd break; 4435219089Spjd case 'D': 4436219089Spjd cb.cb_dedup_stats = B_TRUE; 4437219089Spjd break; 4438219089Spjd case 'T': 4439219089Spjd get_timestamp_arg(*optarg); 4440219089Spjd break; 4441168404Spjd case '?': 4442168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4443168404Spjd optopt); 4444168404Spjd usage(B_FALSE); 4445168404Spjd } 4446168404Spjd } 4447168404Spjd 4448168404Spjd argc -= optind; 4449168404Spjd argv += optind; 4450168404Spjd 4451219089Spjd get_interval_count(&argc, argv, &interval, &count); 4452168404Spjd 4453168404Spjd if (argc == 0) 4454168404Spjd cb.cb_allpools = B_TRUE; 4455168404Spjd 4456219089Spjd cb.cb_first = B_TRUE; 4457168404Spjd 4458219089Spjd for (;;) { 4459219089Spjd if (timestamp_fmt != NODATE) 4460219089Spjd print_timestamp(timestamp_fmt); 4461168404Spjd 4462219089Spjd ret = for_each_pool(argc, argv, B_TRUE, NULL, 4463219089Spjd status_callback, &cb); 4464219089Spjd 4465219089Spjd if (argc == 0 && cb.cb_count == 0) 4466219089Spjd (void) printf(gettext("no pools available\n")); 4467219089Spjd else if (cb.cb_explain && cb.cb_first && cb.cb_allpools) 4468219089Spjd (void) printf(gettext("all pools are healthy\n")); 4469219089Spjd 4470219089Spjd if (ret != 0) 4471219089Spjd return (ret); 4472219089Spjd 4473219089Spjd if (interval == 0) 4474219089Spjd break; 4475219089Spjd 4476219089Spjd if (count != 0 && --count == 0) 4477219089Spjd break; 4478219089Spjd 4479219089Spjd (void) sleep(interval); 4480219089Spjd } 4481219089Spjd 4482219089Spjd return (0); 4483168404Spjd} 4484168404Spjd 4485168404Spjdtypedef struct upgrade_cbdata { 4486168404Spjd int cb_first; 4487212050Spjd char cb_poolname[ZPOOL_MAXNAMELEN]; 4488168404Spjd int cb_argc; 4489185029Spjd uint64_t cb_version; 4490168404Spjd char **cb_argv; 4491168404Spjd} upgrade_cbdata_t; 4492168404Spjd 4493238950Smm#ifdef __FreeBSD__ 4494168404Spjdstatic int 4495212050Spjdis_root_pool(zpool_handle_t *zhp) 4496212050Spjd{ 4497212050Spjd static struct statfs sfs; 4498212050Spjd static char *poolname = NULL; 4499212050Spjd static boolean_t stated = B_FALSE; 4500212050Spjd char *slash; 4501212050Spjd 4502212067Spjd if (!stated) { 4503212050Spjd stated = B_TRUE; 4504212050Spjd if (statfs("/", &sfs) == -1) { 4505212050Spjd (void) fprintf(stderr, 4506212050Spjd "Unable to stat root file system: %s.\n", 4507212050Spjd strerror(errno)); 4508212067Spjd return (0); 4509212050Spjd } 4510212050Spjd if (strcmp(sfs.f_fstypename, "zfs") != 0) 4511212067Spjd return (0); 4512212050Spjd poolname = sfs.f_mntfromname; 4513212050Spjd if ((slash = strchr(poolname, '/')) != NULL) 4514212050Spjd *slash = '\0'; 4515212050Spjd } 4516212050Spjd return (poolname != NULL && strcmp(poolname, zpool_get_name(zhp)) == 0); 4517212050Spjd} 4518212050Spjd 4519238950Smmstatic void 4520238950Smmroot_pool_upgrade_check(zpool_handle_t *zhp, char *poolname, int size) { 4521238950Smm 4522238950Smm if (poolname[0] == '\0' && is_root_pool(zhp)) 4523238950Smm (void) strlcpy(poolname, zpool_get_name(zhp), size); 4524238950Smm} 4525238950Smm#endif /* FreeBSD */ 4526238950Smm 4527212050Spjdstatic int 4528238926Smmupgrade_version(zpool_handle_t *zhp, uint64_t version) 4529238926Smm{ 4530238926Smm int ret; 4531238926Smm nvlist_t *config; 4532238926Smm uint64_t oldversion; 4533238926Smm 4534238926Smm config = zpool_get_config(zhp, NULL); 4535238926Smm verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4536238926Smm &oldversion) == 0); 4537238926Smm 4538238926Smm assert(SPA_VERSION_IS_SUPPORTED(oldversion)); 4539238926Smm assert(oldversion < version); 4540238926Smm 4541238926Smm ret = zpool_upgrade(zhp, version); 4542238926Smm if (ret != 0) 4543238926Smm return (ret); 4544238926Smm 4545238926Smm if (version >= SPA_VERSION_FEATURES) { 4546238926Smm (void) printf(gettext("Successfully upgraded " 4547238926Smm "'%s' from version %llu to feature flags.\n"), 4548238926Smm zpool_get_name(zhp), oldversion); 4549238926Smm } else { 4550238926Smm (void) printf(gettext("Successfully upgraded " 4551238926Smm "'%s' from version %llu to version %llu.\n"), 4552238926Smm zpool_get_name(zhp), oldversion, version); 4553238926Smm } 4554238926Smm 4555238926Smm return (0); 4556238926Smm} 4557238926Smm 4558238926Smmstatic int 4559238926Smmupgrade_enable_all(zpool_handle_t *zhp, int *countp) 4560238926Smm{ 4561238926Smm int i, ret, count; 4562238926Smm boolean_t firstff = B_TRUE; 4563238926Smm nvlist_t *enabled = zpool_get_features(zhp); 4564238926Smm 4565238926Smm count = 0; 4566238926Smm for (i = 0; i < SPA_FEATURES; i++) { 4567238926Smm const char *fname = spa_feature_table[i].fi_uname; 4568238926Smm const char *fguid = spa_feature_table[i].fi_guid; 4569238926Smm if (!nvlist_exists(enabled, fguid)) { 4570238926Smm char *propname; 4571238926Smm verify(-1 != asprintf(&propname, "feature@%s", fname)); 4572238926Smm ret = zpool_set_prop(zhp, propname, 4573238926Smm ZFS_FEATURE_ENABLED); 4574238926Smm if (ret != 0) { 4575238926Smm free(propname); 4576238926Smm return (ret); 4577238926Smm } 4578238926Smm count++; 4579238926Smm 4580238926Smm if (firstff) { 4581238926Smm (void) printf(gettext("Enabled the " 4582238926Smm "following features on '%s':\n"), 4583238926Smm zpool_get_name(zhp)); 4584238926Smm firstff = B_FALSE; 4585238926Smm } 4586238926Smm (void) printf(gettext(" %s\n"), fname); 4587238926Smm free(propname); 4588238926Smm } 4589238926Smm } 4590238926Smm 4591238926Smm if (countp != NULL) 4592238926Smm *countp = count; 4593238926Smm return (0); 4594238926Smm} 4595238926Smm 4596238926Smmstatic int 4597168404Spjdupgrade_cb(zpool_handle_t *zhp, void *arg) 4598168404Spjd{ 4599168404Spjd upgrade_cbdata_t *cbp = arg; 4600168404Spjd nvlist_t *config; 4601168404Spjd uint64_t version; 4602238926Smm boolean_t printnl = B_FALSE; 4603238926Smm int ret; 4604168404Spjd 4605168404Spjd config = zpool_get_config(zhp, NULL); 4606168404Spjd verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4607168404Spjd &version) == 0); 4608168404Spjd 4609238926Smm assert(SPA_VERSION_IS_SUPPORTED(version)); 4610168404Spjd 4611238926Smm if (version < cbp->cb_version) { 4612238926Smm cbp->cb_first = B_FALSE; 4613238926Smm ret = upgrade_version(zhp, cbp->cb_version); 4614238926Smm if (ret != 0) 4615238926Smm return (ret); 4616238926Smm#ifdef __FreeBSD__ 4617238950Smm root_pool_upgrade_check(zhp, cbp->cb_poolname, 4618238950Smm sizeof(cbp->cb_poolname)); 4619238926Smm#endif /* ___FreeBSD__ */ 4620238926Smm printnl = B_TRUE; 4621238926Smm 4622238926Smm#ifdef illumos 4623238926Smm /* 4624238926Smm * If they did "zpool upgrade -a", then we could 4625238926Smm * be doing ioctls to different pools. We need 4626238926Smm * to log this history once to each pool, and bypass 4627238926Smm * the normal history logging that happens in main(). 4628238926Smm */ 4629238926Smm (void) zpool_log_history(g_zfs, history_str); 4630238926Smm log_history = B_FALSE; 4631238926Smm#endif 4632238926Smm } 4633238926Smm 4634238926Smm if (cbp->cb_version >= SPA_VERSION_FEATURES) { 4635238926Smm int count; 4636238926Smm ret = upgrade_enable_all(zhp, &count); 4637238926Smm if (ret != 0) 4638238926Smm return (ret); 4639238926Smm 4640238926Smm if (count > 0) { 4641168404Spjd cbp->cb_first = B_FALSE; 4642238926Smm printnl = B_TRUE; 4643248571Smm /* 4644248571Smm * If they did "zpool upgrade -a", then we could 4645248571Smm * be doing ioctls to different pools. We need 4646248571Smm * to log this history once to each pool, and bypass 4647248571Smm * the normal history logging that happens in main(). 4648248571Smm */ 4649248571Smm (void) zpool_log_history(g_zfs, history_str); 4650248571Smm log_history = B_FALSE; 4651168404Spjd } 4652238926Smm } 4653168404Spjd 4654238926Smm if (printnl) { 4655238926Smm (void) printf(gettext("\n")); 4656238926Smm } 4657238926Smm 4658238926Smm return (0); 4659238926Smm} 4660238926Smm 4661238926Smmstatic int 4662238926Smmupgrade_list_older_cb(zpool_handle_t *zhp, void *arg) 4663238926Smm{ 4664238926Smm upgrade_cbdata_t *cbp = arg; 4665238926Smm nvlist_t *config; 4666238926Smm uint64_t version; 4667238926Smm 4668238926Smm config = zpool_get_config(zhp, NULL); 4669238926Smm verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4670238926Smm &version) == 0); 4671238926Smm 4672238926Smm assert(SPA_VERSION_IS_SUPPORTED(version)); 4673238926Smm 4674238926Smm if (version < SPA_VERSION_FEATURES) { 4675168404Spjd if (cbp->cb_first) { 4676168404Spjd (void) printf(gettext("The following pools are " 4677238926Smm "formatted with legacy version numbers and can\n" 4678238926Smm "be upgraded to use feature flags. After " 4679238926Smm "being upgraded, these pools\nwill no " 4680238926Smm "longer be accessible by software that does not " 4681238926Smm "support feature\nflags.\n\n")); 4682168404Spjd (void) printf(gettext("VER POOL\n")); 4683168404Spjd (void) printf(gettext("--- ------------\n")); 4684168404Spjd cbp->cb_first = B_FALSE; 4685168404Spjd } 4686168404Spjd 4687168404Spjd (void) printf("%2llu %s\n", (u_longlong_t)version, 4688168404Spjd zpool_get_name(zhp)); 4689168404Spjd } 4690168404Spjd 4691238926Smm return (0); 4692168404Spjd} 4693168404Spjd 4694238926Smmstatic int 4695238926Smmupgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg) 4696238926Smm{ 4697238926Smm upgrade_cbdata_t *cbp = arg; 4698238926Smm nvlist_t *config; 4699238926Smm uint64_t version; 4700238926Smm 4701238926Smm config = zpool_get_config(zhp, NULL); 4702238926Smm verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 4703238926Smm &version) == 0); 4704238926Smm 4705238926Smm if (version >= SPA_VERSION_FEATURES) { 4706238926Smm int i; 4707238926Smm boolean_t poolfirst = B_TRUE; 4708238926Smm nvlist_t *enabled = zpool_get_features(zhp); 4709238926Smm 4710238926Smm for (i = 0; i < SPA_FEATURES; i++) { 4711238926Smm const char *fguid = spa_feature_table[i].fi_guid; 4712238926Smm const char *fname = spa_feature_table[i].fi_uname; 4713238926Smm if (!nvlist_exists(enabled, fguid)) { 4714238926Smm if (cbp->cb_first) { 4715238926Smm (void) printf(gettext("\nSome " 4716238926Smm "supported features are not " 4717238926Smm "enabled on the following pools. " 4718238926Smm "Once a\nfeature is enabled the " 4719238926Smm "pool may become incompatible with " 4720238926Smm "software\nthat does not support " 4721238926Smm "the feature. See " 4722243014Smm "zpool-features(7) for " 4723238926Smm "details.\n\n")); 4724238926Smm (void) printf(gettext("POOL " 4725238926Smm "FEATURE\n")); 4726238926Smm (void) printf(gettext("------" 4727238926Smm "---------\n")); 4728238926Smm cbp->cb_first = B_FALSE; 4729238926Smm } 4730238926Smm 4731238926Smm if (poolfirst) { 4732238926Smm (void) printf(gettext("%s\n"), 4733238926Smm zpool_get_name(zhp)); 4734238926Smm poolfirst = B_FALSE; 4735238926Smm } 4736238926Smm 4737238926Smm (void) printf(gettext(" %s\n"), fname); 4738238926Smm } 4739238926Smm } 4740238926Smm } 4741238926Smm 4742238926Smm return (0); 4743238926Smm} 4744238926Smm 4745168404Spjd/* ARGSUSED */ 4746168404Spjdstatic int 4747168404Spjdupgrade_one(zpool_handle_t *zhp, void *data) 4748168404Spjd{ 4749238926Smm boolean_t printnl = B_FALSE; 4750185029Spjd upgrade_cbdata_t *cbp = data; 4751185029Spjd uint64_t cur_version; 4752168404Spjd int ret; 4753168404Spjd 4754185029Spjd if (strcmp("log", zpool_get_name(zhp)) == 0) { 4755185029Spjd (void) printf(gettext("'log' is now a reserved word\n" 4756185029Spjd "Pool 'log' must be renamed using export and import" 4757185029Spjd " to upgrade.\n")); 4758185029Spjd return (1); 4759185029Spjd } 4760168404Spjd 4761185029Spjd cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 4762185029Spjd if (cur_version > cbp->cb_version) { 4763168404Spjd (void) printf(gettext("Pool '%s' is already formatted " 4764238926Smm "using more current version '%llu'.\n\n"), 4765185029Spjd zpool_get_name(zhp), cur_version); 4766185029Spjd return (0); 4767185029Spjd } 4768238926Smm 4769238926Smm if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) { 4770185029Spjd (void) printf(gettext("Pool '%s' is already formatted " 4771238926Smm "using version %llu.\n\n"), zpool_get_name(zhp), 4772238926Smm cbp->cb_version); 4773168404Spjd return (0); 4774168404Spjd } 4775168404Spjd 4776238926Smm if (cur_version != cbp->cb_version) { 4777238926Smm printnl = B_TRUE; 4778238926Smm ret = upgrade_version(zhp, cbp->cb_version); 4779238950Smm if (ret != 0) 4780238950Smm return (ret); 4781238926Smm#ifdef __FreeBSD__ 4782238950Smm root_pool_upgrade_check(zhp, cbp->cb_poolname, 4783238950Smm sizeof(cbp->cb_poolname)); 4784238926Smm#endif /* ___FreeBSD__ */ 4785238926Smm } 4786168404Spjd 4787238926Smm if (cbp->cb_version >= SPA_VERSION_FEATURES) { 4788238926Smm int count = 0; 4789238926Smm ret = upgrade_enable_all(zhp, &count); 4790238926Smm if (ret != 0) 4791238926Smm return (ret); 4792238926Smm 4793238926Smm if (count != 0) { 4794238926Smm printnl = B_TRUE; 4795238950Smm#ifdef __FreeBSD__ 4796238951Smm root_pool_upgrade_check(zhp, cbp->cb_poolname, 4797238951Smm sizeof(cbp->cb_poolname)); 4798238950Smm#endif /* __FreeBSD __*/ 4799238926Smm } else if (cur_version == SPA_VERSION) { 4800238926Smm (void) printf(gettext("Pool '%s' already has all " 4801238926Smm "supported features enabled.\n"), 4802238926Smm zpool_get_name(zhp)); 4803238926Smm } 4804168404Spjd } 4805168404Spjd 4806238926Smm if (printnl) { 4807238926Smm (void) printf(gettext("\n")); 4808238926Smm } 4809238926Smm 4810238926Smm return (0); 4811168404Spjd} 4812168404Spjd 4813168404Spjd/* 4814168404Spjd * zpool upgrade 4815168404Spjd * zpool upgrade -v 4816185029Spjd * zpool upgrade [-V version] <-a | pool ...> 4817168404Spjd * 4818168404Spjd * With no arguments, display downrev'd ZFS pool available for upgrade. 4819168404Spjd * Individual pools can be upgraded by specifying the pool, and '-a' will 4820168404Spjd * upgrade all pools. 4821168404Spjd */ 4822168404Spjdint 4823168404Spjdzpool_do_upgrade(int argc, char **argv) 4824168404Spjd{ 4825168404Spjd int c; 4826168404Spjd upgrade_cbdata_t cb = { 0 }; 4827168404Spjd int ret = 0; 4828168404Spjd boolean_t showversions = B_FALSE; 4829238926Smm boolean_t upgradeall = B_FALSE; 4830185029Spjd char *end; 4831168404Spjd 4832185029Spjd 4833168404Spjd /* check options */ 4834219089Spjd while ((c = getopt(argc, argv, ":avV:")) != -1) { 4835168404Spjd switch (c) { 4836168404Spjd case 'a': 4837238926Smm upgradeall = B_TRUE; 4838168404Spjd break; 4839168404Spjd case 'v': 4840168404Spjd showversions = B_TRUE; 4841168404Spjd break; 4842185029Spjd case 'V': 4843185029Spjd cb.cb_version = strtoll(optarg, &end, 10); 4844236884Smm if (*end != '\0' || 4845236884Smm !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) { 4846185029Spjd (void) fprintf(stderr, 4847185029Spjd gettext("invalid version '%s'\n"), optarg); 4848185029Spjd usage(B_FALSE); 4849185029Spjd } 4850185029Spjd break; 4851219089Spjd case ':': 4852219089Spjd (void) fprintf(stderr, gettext("missing argument for " 4853219089Spjd "'%c' option\n"), optopt); 4854219089Spjd usage(B_FALSE); 4855219089Spjd break; 4856168404Spjd case '?': 4857168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4858168404Spjd optopt); 4859168404Spjd usage(B_FALSE); 4860168404Spjd } 4861168404Spjd } 4862168404Spjd 4863168404Spjd cb.cb_argc = argc; 4864168404Spjd cb.cb_argv = argv; 4865168404Spjd argc -= optind; 4866168404Spjd argv += optind; 4867168404Spjd 4868185029Spjd if (cb.cb_version == 0) { 4869185029Spjd cb.cb_version = SPA_VERSION; 4870238926Smm } else if (!upgradeall && argc == 0) { 4871185029Spjd (void) fprintf(stderr, gettext("-V option is " 4872185029Spjd "incompatible with other arguments\n")); 4873185029Spjd usage(B_FALSE); 4874185029Spjd } 4875185029Spjd 4876168404Spjd if (showversions) { 4877238926Smm if (upgradeall || argc != 0) { 4878168404Spjd (void) fprintf(stderr, gettext("-v option is " 4879168404Spjd "incompatible with other arguments\n")); 4880168404Spjd usage(B_FALSE); 4881168404Spjd } 4882238926Smm } else if (upgradeall) { 4883168404Spjd if (argc != 0) { 4884185029Spjd (void) fprintf(stderr, gettext("-a option should not " 4885185029Spjd "be used along with a pool name\n")); 4886168404Spjd usage(B_FALSE); 4887168404Spjd } 4888168404Spjd } 4889168404Spjd 4890236884Smm (void) printf(gettext("This system supports ZFS pool feature " 4891236884Smm "flags.\n\n")); 4892168404Spjd if (showversions) { 4893238926Smm int i; 4894238926Smm 4895238926Smm (void) printf(gettext("The following features are " 4896168404Spjd "supported:\n\n")); 4897238926Smm (void) printf(gettext("FEAT DESCRIPTION\n")); 4898238926Smm (void) printf("----------------------------------------------" 4899238926Smm "---------------\n"); 4900238926Smm for (i = 0; i < SPA_FEATURES; i++) { 4901238926Smm zfeature_info_t *fi = &spa_feature_table[i]; 4902238926Smm const char *ro = fi->fi_can_readonly ? 4903238926Smm " (read-only compatible)" : ""; 4904238926Smm 4905238926Smm (void) printf("%-37s%s\n", fi->fi_uname, ro); 4906238926Smm (void) printf(" %s\n", fi->fi_desc); 4907238926Smm } 4908238926Smm (void) printf("\n"); 4909238926Smm 4910238926Smm (void) printf(gettext("The following legacy versions are also " 4911238926Smm "supported:\n\n")); 4912168404Spjd (void) printf(gettext("VER DESCRIPTION\n")); 4913168404Spjd (void) printf("--- -----------------------------------------" 4914168404Spjd "---------------\n"); 4915168404Spjd (void) printf(gettext(" 1 Initial ZFS version\n")); 4916168404Spjd (void) printf(gettext(" 2 Ditto blocks " 4917168404Spjd "(replicated metadata)\n")); 4918168404Spjd (void) printf(gettext(" 3 Hot spares and double parity " 4919168404Spjd "RAID-Z\n")); 4920168404Spjd (void) printf(gettext(" 4 zpool history\n")); 4921168404Spjd (void) printf(gettext(" 5 Compression using the gzip " 4922168404Spjd "algorithm\n")); 4923185029Spjd (void) printf(gettext(" 6 bootfs pool property\n")); 4924185029Spjd (void) printf(gettext(" 7 Separate intent log devices\n")); 4925185029Spjd (void) printf(gettext(" 8 Delegated administration\n")); 4926185029Spjd (void) printf(gettext(" 9 refquota and refreservation " 4927185029Spjd "properties\n")); 4928185029Spjd (void) printf(gettext(" 10 Cache devices\n")); 4929185029Spjd (void) printf(gettext(" 11 Improved scrub performance\n")); 4930185029Spjd (void) printf(gettext(" 12 Snapshot properties\n")); 4931185029Spjd (void) printf(gettext(" 13 snapused property\n")); 4932209962Smm (void) printf(gettext(" 14 passthrough-x aclinherit\n")); 4933209962Smm (void) printf(gettext(" 15 user/group space accounting\n")); 4934219089Spjd (void) printf(gettext(" 16 stmf property support\n")); 4935219089Spjd (void) printf(gettext(" 17 Triple-parity RAID-Z\n")); 4936219089Spjd (void) printf(gettext(" 18 Snapshot user holds\n")); 4937219089Spjd (void) printf(gettext(" 19 Log device removal\n")); 4938219089Spjd (void) printf(gettext(" 20 Compression using zle " 4939219089Spjd "(zero-length encoding)\n")); 4940219089Spjd (void) printf(gettext(" 21 Deduplication\n")); 4941219089Spjd (void) printf(gettext(" 22 Received properties\n")); 4942219089Spjd (void) printf(gettext(" 23 Slim ZIL\n")); 4943219089Spjd (void) printf(gettext(" 24 System attributes\n")); 4944219089Spjd (void) printf(gettext(" 25 Improved scrub stats\n")); 4945219089Spjd (void) printf(gettext(" 26 Improved snapshot deletion " 4946219089Spjd "performance\n")); 4947219089Spjd (void) printf(gettext(" 27 Improved snapshot creation " 4948219089Spjd "performance\n")); 4949219089Spjd (void) printf(gettext(" 28 Multiple vdev replacements\n")); 4950219089Spjd (void) printf(gettext("\nFor more information on a particular " 4951219089Spjd "version, including supported releases,\n")); 4952219089Spjd (void) printf(gettext("see the ZFS Administration Guide.\n\n")); 4953238926Smm } else if (argc == 0 && upgradeall) { 4954238926Smm cb.cb_first = B_TRUE; 4955168404Spjd ret = zpool_iter(g_zfs, upgrade_cb, &cb); 4956238926Smm if (ret == 0 && cb.cb_first) { 4957238926Smm if (cb.cb_version == SPA_VERSION) { 4958238926Smm (void) printf(gettext("All pools are already " 4959238926Smm "formatted using feature flags.\n\n")); 4960238926Smm (void) printf(gettext("Every feature flags " 4961238926Smm "pool already has all supported features " 4962238926Smm "enabled.\n")); 4963238926Smm } else { 4964238926Smm (void) printf(gettext("All pools are already " 4965238926Smm "formatted with version %llu or higher.\n"), 4966238926Smm cb.cb_version); 4967168404Spjd } 4968168404Spjd } 4969238926Smm } else if (argc == 0) { 4970238926Smm cb.cb_first = B_TRUE; 4971238926Smm ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb); 4972238926Smm assert(ret == 0); 4973168404Spjd 4974238926Smm if (cb.cb_first) { 4975238926Smm (void) printf(gettext("All pools are formatted " 4976238926Smm "using feature flags.\n\n")); 4977238926Smm } else { 4978238926Smm (void) printf(gettext("\nUse 'zpool upgrade -v' " 4979238926Smm "for a list of available legacy versions.\n")); 4980168404Spjd } 4981238926Smm 4982238926Smm cb.cb_first = B_TRUE; 4983238926Smm ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb); 4984238926Smm assert(ret == 0); 4985238926Smm 4986238926Smm if (cb.cb_first) { 4987238926Smm (void) printf(gettext("Every feature flags pool has " 4988238926Smm "all supported features enabled.\n")); 4989238926Smm } else { 4990238926Smm (void) printf(gettext("\n")); 4991238926Smm } 4992168404Spjd } else { 4993168404Spjd ret = for_each_pool(argc, argv, B_FALSE, NULL, 4994168404Spjd upgrade_one, &cb); 4995168404Spjd } 4996168404Spjd 4997212050Spjd if (cb.cb_poolname[0] != '\0') { 4998212050Spjd (void) printf( 4999212050Spjd "If you boot from pool '%s', don't forget to update boot code.\n" 5000212050Spjd "Assuming you use GPT partitioning and da0 is your boot disk\n" 5001212050Spjd "the following command will do it:\n" 5002212050Spjd "\n" 5003212050Spjd "\tgpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 da0\n\n", 5004212050Spjd cb.cb_poolname); 5005212050Spjd } 5006212050Spjd 5007168404Spjd return (ret); 5008168404Spjd} 5009168404Spjd 5010185029Spjdtypedef struct hist_cbdata { 5011185029Spjd boolean_t first; 5012248571Smm boolean_t longfmt; 5013248571Smm boolean_t internal; 5014185029Spjd} hist_cbdata_t; 5015185029Spjd 5016168404Spjd/* 5017168404Spjd * Print out the command history for a specific pool. 5018168404Spjd */ 5019168404Spjdstatic int 5020168404Spjdget_history_one(zpool_handle_t *zhp, void *data) 5021168404Spjd{ 5022168404Spjd nvlist_t *nvhis; 5023168404Spjd nvlist_t **records; 5024168404Spjd uint_t numrecords; 5025168404Spjd int ret, i; 5026185029Spjd hist_cbdata_t *cb = (hist_cbdata_t *)data; 5027168404Spjd 5028185029Spjd cb->first = B_FALSE; 5029168404Spjd 5030168404Spjd (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp)); 5031168404Spjd 5032168404Spjd if ((ret = zpool_get_history(zhp, &nvhis)) != 0) 5033168404Spjd return (ret); 5034168404Spjd 5035168404Spjd verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD, 5036168404Spjd &records, &numrecords) == 0); 5037168404Spjd for (i = 0; i < numrecords; i++) { 5038248571Smm nvlist_t *rec = records[i]; 5039248571Smm char tbuf[30] = ""; 5040185029Spjd 5041248571Smm if (nvlist_exists(rec, ZPOOL_HIST_TIME)) { 5042248571Smm time_t tsec; 5043248571Smm struct tm t; 5044185029Spjd 5045248571Smm tsec = fnvlist_lookup_uint64(records[i], 5046248571Smm ZPOOL_HIST_TIME); 5047248571Smm (void) localtime_r(&tsec, &t); 5048248571Smm (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 5049248571Smm } 5050248571Smm 5051248571Smm if (nvlist_exists(rec, ZPOOL_HIST_CMD)) { 5052248571Smm (void) printf("%s %s", tbuf, 5053248571Smm fnvlist_lookup_string(rec, ZPOOL_HIST_CMD)); 5054248571Smm } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) { 5055248571Smm int ievent = 5056248571Smm fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT); 5057248571Smm if (!cb->internal) 5058185029Spjd continue; 5059248571Smm if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) { 5060248571Smm (void) printf("%s unrecognized record:\n", 5061248571Smm tbuf); 5062248571Smm dump_nvlist(rec, 4); 5063185029Spjd continue; 5064248571Smm } 5065248571Smm (void) printf("%s [internal %s txg:%lld] %s", tbuf, 5066248571Smm zfs_history_event_names[ievent], 5067248571Smm fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 5068248571Smm fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR)); 5069248571Smm } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) { 5070248571Smm if (!cb->internal) 5071248571Smm continue; 5072248571Smm (void) printf("%s [txg:%lld] %s", tbuf, 5073248571Smm fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG), 5074248571Smm fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME)); 5075248571Smm if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) { 5076248571Smm (void) printf(" %s (%llu)", 5077248571Smm fnvlist_lookup_string(rec, 5078248571Smm ZPOOL_HIST_DSNAME), 5079248571Smm fnvlist_lookup_uint64(rec, 5080248571Smm ZPOOL_HIST_DSID)); 5081248571Smm } 5082248571Smm (void) printf(" %s", fnvlist_lookup_string(rec, 5083248571Smm ZPOOL_HIST_INT_STR)); 5084248571Smm } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) { 5085248571Smm if (!cb->internal) 5086248571Smm continue; 5087248571Smm (void) printf("%s ioctl %s\n", tbuf, 5088248571Smm fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL)); 5089248571Smm if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) { 5090248571Smm (void) printf(" input:\n"); 5091248571Smm dump_nvlist(fnvlist_lookup_nvlist(rec, 5092248571Smm ZPOOL_HIST_INPUT_NVL), 8); 5093248571Smm } 5094248571Smm if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) { 5095248571Smm (void) printf(" output:\n"); 5096248571Smm dump_nvlist(fnvlist_lookup_nvlist(rec, 5097248571Smm ZPOOL_HIST_OUTPUT_NVL), 8); 5098248571Smm } 5099248571Smm } else { 5100248571Smm if (!cb->internal) 5101248571Smm continue; 5102248571Smm (void) printf("%s unrecognized record:\n", tbuf); 5103248571Smm dump_nvlist(rec, 4); 5104168404Spjd } 5105185029Spjd 5106185029Spjd if (!cb->longfmt) { 5107185029Spjd (void) printf("\n"); 5108185029Spjd continue; 5109185029Spjd } 5110185029Spjd (void) printf(" ["); 5111248571Smm if (nvlist_exists(rec, ZPOOL_HIST_WHO)) { 5112248571Smm uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO); 5113248571Smm struct passwd *pwd = getpwuid(who); 5114248571Smm (void) printf("user %d ", (int)who); 5115248571Smm if (pwd != NULL) 5116248571Smm (void) printf("(%s) ", pwd->pw_name); 5117185029Spjd } 5118248571Smm if (nvlist_exists(rec, ZPOOL_HIST_HOST)) { 5119248571Smm (void) printf("on %s", 5120248571Smm fnvlist_lookup_string(rec, ZPOOL_HIST_HOST)); 5121185029Spjd } 5122248571Smm if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) { 5123248571Smm (void) printf(":%s", 5124248571Smm fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE)); 5125185029Spjd } 5126185029Spjd (void) printf("]"); 5127185029Spjd (void) printf("\n"); 5128168404Spjd } 5129168404Spjd (void) printf("\n"); 5130168404Spjd nvlist_free(nvhis); 5131168404Spjd 5132168404Spjd return (ret); 5133168404Spjd} 5134168404Spjd 5135168404Spjd/* 5136168404Spjd * zpool history <pool> 5137168404Spjd * 5138168404Spjd * Displays the history of commands that modified pools. 5139168404Spjd */ 5140168404Spjdint 5141168404Spjdzpool_do_history(int argc, char **argv) 5142168404Spjd{ 5143185029Spjd hist_cbdata_t cbdata = { 0 }; 5144168404Spjd int ret; 5145185029Spjd int c; 5146168404Spjd 5147185029Spjd cbdata.first = B_TRUE; 5148185029Spjd /* check options */ 5149185029Spjd while ((c = getopt(argc, argv, "li")) != -1) { 5150185029Spjd switch (c) { 5151185029Spjd case 'l': 5152248571Smm cbdata.longfmt = B_TRUE; 5153185029Spjd break; 5154185029Spjd case 'i': 5155248571Smm cbdata.internal = B_TRUE; 5156185029Spjd break; 5157185029Spjd case '?': 5158185029Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5159185029Spjd optopt); 5160185029Spjd usage(B_FALSE); 5161185029Spjd } 5162185029Spjd } 5163168404Spjd argc -= optind; 5164168404Spjd argv += optind; 5165168404Spjd 5166168404Spjd ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one, 5167185029Spjd &cbdata); 5168168404Spjd 5169185029Spjd if (argc == 0 && cbdata.first == B_TRUE) { 5170168404Spjd (void) printf(gettext("no pools available\n")); 5171168404Spjd return (0); 5172168404Spjd } 5173168404Spjd 5174168404Spjd return (ret); 5175168404Spjd} 5176168404Spjd 5177168404Spjdstatic int 5178168404Spjdget_callback(zpool_handle_t *zhp, void *data) 5179168404Spjd{ 5180185029Spjd zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data; 5181168404Spjd char value[MAXNAMELEN]; 5182185029Spjd zprop_source_t srctype; 5183185029Spjd zprop_list_t *pl; 5184168404Spjd 5185168404Spjd for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 5186168404Spjd 5187168404Spjd /* 5188185029Spjd * Skip the special fake placeholder. This will also skip 5189185029Spjd * over the name property when 'all' is specified. 5190168404Spjd */ 5191185029Spjd if (pl->pl_prop == ZPOOL_PROP_NAME && 5192168404Spjd pl == cbp->cb_proplist) 5193168404Spjd continue; 5194168404Spjd 5195236884Smm if (pl->pl_prop == ZPROP_INVAL && 5196236884Smm (zpool_prop_feature(pl->pl_user_prop) || 5197236884Smm zpool_prop_unsupported(pl->pl_user_prop))) { 5198236884Smm srctype = ZPROP_SRC_LOCAL; 5199168404Spjd 5200236884Smm if (zpool_prop_get_feature(zhp, pl->pl_user_prop, 5201236884Smm value, sizeof (value)) == 0) { 5202236884Smm zprop_print_one_property(zpool_get_name(zhp), 5203236884Smm cbp, pl->pl_user_prop, value, srctype, 5204236884Smm NULL, NULL); 5205236884Smm } 5206236884Smm } else { 5207236884Smm if (zpool_get_prop(zhp, pl->pl_prop, value, 5208263889Sdelphij sizeof (value), &srctype, cbp->cb_literal) != 0) 5209236884Smm continue; 5210236884Smm 5211236884Smm zprop_print_one_property(zpool_get_name(zhp), cbp, 5212236884Smm zpool_prop_to_name(pl->pl_prop), value, srctype, 5213236884Smm NULL, NULL); 5214236884Smm } 5215168404Spjd } 5216168404Spjd return (0); 5217168404Spjd} 5218168404Spjd 5219263889Sdelphij/* 5220263889Sdelphij * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ... 5221263889Sdelphij * 5222263889Sdelphij * -H Scripted mode. Don't display headers, and separate properties 5223263889Sdelphij * by a single tab. 5224263889Sdelphij * -o List of columns to display. Defaults to 5225263889Sdelphij * "name,property,value,source". 5226263889Sdelphij * -p Diplay values in parsable (exact) format. 5227263889Sdelphij * 5228263889Sdelphij * Get properties of pools in the system. Output space statistics 5229263889Sdelphij * for each one as well as other attributes. 5230263889Sdelphij */ 5231168404Spjdint 5232168404Spjdzpool_do_get(int argc, char **argv) 5233168404Spjd{ 5234185029Spjd zprop_get_cbdata_t cb = { 0 }; 5235185029Spjd zprop_list_t fake_name = { 0 }; 5236168404Spjd int ret; 5237263889Sdelphij int c, i; 5238263889Sdelphij char *value; 5239168404Spjd 5240263889Sdelphij cb.cb_first = B_TRUE; 5241168404Spjd 5242263889Sdelphij /* 5243263889Sdelphij * Set up default columns and sources. 5244263889Sdelphij */ 5245185029Spjd cb.cb_sources = ZPROP_SRC_ALL; 5246168404Spjd cb.cb_columns[0] = GET_COL_NAME; 5247168404Spjd cb.cb_columns[1] = GET_COL_PROPERTY; 5248168404Spjd cb.cb_columns[2] = GET_COL_VALUE; 5249168404Spjd cb.cb_columns[3] = GET_COL_SOURCE; 5250185029Spjd cb.cb_type = ZFS_TYPE_POOL; 5251168404Spjd 5252263889Sdelphij /* check options */ 5253263889Sdelphij while ((c = getopt(argc, argv, ":Hpo:")) != -1) { 5254263889Sdelphij switch (c) { 5255263889Sdelphij case 'p': 5256263889Sdelphij cb.cb_literal = B_TRUE; 5257263889Sdelphij break; 5258263889Sdelphij case 'H': 5259263889Sdelphij cb.cb_scripted = B_TRUE; 5260263889Sdelphij break; 5261263889Sdelphij case 'o': 5262263889Sdelphij bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 5263263889Sdelphij i = 0; 5264263889Sdelphij while (*optarg != '\0') { 5265263889Sdelphij static char *col_subopts[] = 5266263889Sdelphij { "name", "property", "value", "source", 5267263889Sdelphij "all", NULL }; 5268263889Sdelphij 5269263889Sdelphij if (i == ZFS_GET_NCOLS) { 5270263889Sdelphij (void) fprintf(stderr, gettext("too " 5271263889Sdelphij "many fields given to -o " 5272263889Sdelphij "option\n")); 5273263889Sdelphij usage(B_FALSE); 5274263889Sdelphij } 5275263889Sdelphij 5276263889Sdelphij switch (getsubopt(&optarg, col_subopts, 5277263889Sdelphij &value)) { 5278263889Sdelphij case 0: 5279263889Sdelphij cb.cb_columns[i++] = GET_COL_NAME; 5280263889Sdelphij break; 5281263889Sdelphij case 1: 5282263889Sdelphij cb.cb_columns[i++] = GET_COL_PROPERTY; 5283263889Sdelphij break; 5284263889Sdelphij case 2: 5285263889Sdelphij cb.cb_columns[i++] = GET_COL_VALUE; 5286263889Sdelphij break; 5287263889Sdelphij case 3: 5288263889Sdelphij cb.cb_columns[i++] = GET_COL_SOURCE; 5289263889Sdelphij break; 5290263889Sdelphij case 4: 5291263889Sdelphij if (i > 0) { 5292263889Sdelphij (void) fprintf(stderr, 5293263889Sdelphij gettext("\"all\" conflicts " 5294263889Sdelphij "with specific fields " 5295263889Sdelphij "given to -o option\n")); 5296263889Sdelphij usage(B_FALSE); 5297263889Sdelphij } 5298263889Sdelphij cb.cb_columns[0] = GET_COL_NAME; 5299263889Sdelphij cb.cb_columns[1] = GET_COL_PROPERTY; 5300263889Sdelphij cb.cb_columns[2] = GET_COL_VALUE; 5301263889Sdelphij cb.cb_columns[3] = GET_COL_SOURCE; 5302263889Sdelphij i = ZFS_GET_NCOLS; 5303263889Sdelphij break; 5304263889Sdelphij default: 5305263889Sdelphij (void) fprintf(stderr, 5306263889Sdelphij gettext("invalid column name " 5307263889Sdelphij "'%s'\n"), value); 5308263889Sdelphij usage(B_FALSE); 5309263889Sdelphij } 5310263889Sdelphij } 5311263889Sdelphij break; 5312263889Sdelphij case '?': 5313263889Sdelphij (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5314263889Sdelphij optopt); 5315263889Sdelphij usage(B_FALSE); 5316263889Sdelphij } 5317263889Sdelphij } 5318263889Sdelphij 5319263889Sdelphij argc -= optind; 5320263889Sdelphij argv += optind; 5321263889Sdelphij 5322263889Sdelphij if (argc < 1) { 5323263889Sdelphij (void) fprintf(stderr, gettext("missing property " 5324263889Sdelphij "argument\n")); 5325263889Sdelphij usage(B_FALSE); 5326263889Sdelphij } 5327263889Sdelphij 5328263889Sdelphij if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, 5329185029Spjd ZFS_TYPE_POOL) != 0) 5330168404Spjd usage(B_FALSE); 5331168404Spjd 5332263889Sdelphij argc--; 5333263889Sdelphij argv++; 5334263889Sdelphij 5335168404Spjd if (cb.cb_proplist != NULL) { 5336185029Spjd fake_name.pl_prop = ZPOOL_PROP_NAME; 5337168404Spjd fake_name.pl_width = strlen(gettext("NAME")); 5338168404Spjd fake_name.pl_next = cb.cb_proplist; 5339168404Spjd cb.cb_proplist = &fake_name; 5340168404Spjd } 5341168404Spjd 5342263889Sdelphij ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, 5343168404Spjd get_callback, &cb); 5344168404Spjd 5345168404Spjd if (cb.cb_proplist == &fake_name) 5346185029Spjd zprop_free_list(fake_name.pl_next); 5347168404Spjd else 5348185029Spjd zprop_free_list(cb.cb_proplist); 5349168404Spjd 5350168404Spjd return (ret); 5351168404Spjd} 5352168404Spjd 5353168404Spjdtypedef struct set_cbdata { 5354168404Spjd char *cb_propname; 5355168404Spjd char *cb_value; 5356168404Spjd boolean_t cb_any_successful; 5357168404Spjd} set_cbdata_t; 5358168404Spjd 5359168404Spjdint 5360168404Spjdset_callback(zpool_handle_t *zhp, void *data) 5361168404Spjd{ 5362168404Spjd int error; 5363168404Spjd set_cbdata_t *cb = (set_cbdata_t *)data; 5364168404Spjd 5365168404Spjd error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value); 5366168404Spjd 5367168404Spjd if (!error) 5368168404Spjd cb->cb_any_successful = B_TRUE; 5369168404Spjd 5370168404Spjd return (error); 5371168404Spjd} 5372168404Spjd 5373168404Spjdint 5374168404Spjdzpool_do_set(int argc, char **argv) 5375168404Spjd{ 5376168404Spjd set_cbdata_t cb = { 0 }; 5377168404Spjd int error; 5378168404Spjd 5379168404Spjd if (argc > 1 && argv[1][0] == '-') { 5380168404Spjd (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5381168404Spjd argv[1][1]); 5382168404Spjd usage(B_FALSE); 5383168404Spjd } 5384168404Spjd 5385168404Spjd if (argc < 2) { 5386168404Spjd (void) fprintf(stderr, gettext("missing property=value " 5387168404Spjd "argument\n")); 5388168404Spjd usage(B_FALSE); 5389168404Spjd } 5390168404Spjd 5391168404Spjd if (argc < 3) { 5392168404Spjd (void) fprintf(stderr, gettext("missing pool name\n")); 5393168404Spjd usage(B_FALSE); 5394168404Spjd } 5395168404Spjd 5396168404Spjd if (argc > 3) { 5397168404Spjd (void) fprintf(stderr, gettext("too many pool names\n")); 5398168404Spjd usage(B_FALSE); 5399168404Spjd } 5400168404Spjd 5401168404Spjd cb.cb_propname = argv[1]; 5402168404Spjd cb.cb_value = strchr(cb.cb_propname, '='); 5403168404Spjd if (cb.cb_value == NULL) { 5404168404Spjd (void) fprintf(stderr, gettext("missing value in " 5405168404Spjd "property=value argument\n")); 5406168404Spjd usage(B_FALSE); 5407168404Spjd } 5408168404Spjd 5409168404Spjd *(cb.cb_value) = '\0'; 5410168404Spjd cb.cb_value++; 5411168404Spjd 5412168404Spjd error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL, 5413168404Spjd set_callback, &cb); 5414168404Spjd 5415168404Spjd return (error); 5416168404Spjd} 5417168404Spjd 5418168404Spjdstatic int 5419168404Spjdfind_command_idx(char *command, int *idx) 5420168404Spjd{ 5421168404Spjd int i; 5422168404Spjd 5423168404Spjd for (i = 0; i < NCOMMAND; i++) { 5424168404Spjd if (command_table[i].name == NULL) 5425168404Spjd continue; 5426168404Spjd 5427168404Spjd if (strcmp(command, command_table[i].name) == 0) { 5428168404Spjd *idx = i; 5429168404Spjd return (0); 5430168404Spjd } 5431168404Spjd } 5432168404Spjd return (1); 5433168404Spjd} 5434168404Spjd 5435168404Spjdint 5436168404Spjdmain(int argc, char **argv) 5437168404Spjd{ 5438168404Spjd int ret; 5439168404Spjd int i; 5440168404Spjd char *cmdname; 5441168404Spjd 5442168404Spjd (void) setlocale(LC_ALL, ""); 5443168404Spjd (void) textdomain(TEXT_DOMAIN); 5444168404Spjd 5445168404Spjd if ((g_zfs = libzfs_init()) == NULL) { 5446168404Spjd (void) fprintf(stderr, gettext("internal error: failed to " 5447168404Spjd "initialize ZFS library\n")); 5448168404Spjd return (1); 5449168404Spjd } 5450168404Spjd 5451168404Spjd libzfs_print_on_error(g_zfs, B_TRUE); 5452168404Spjd 5453168404Spjd opterr = 0; 5454168404Spjd 5455168404Spjd /* 5456168404Spjd * Make sure the user has specified some command. 5457168404Spjd */ 5458168404Spjd if (argc < 2) { 5459168404Spjd (void) fprintf(stderr, gettext("missing command\n")); 5460168404Spjd usage(B_FALSE); 5461168404Spjd } 5462168404Spjd 5463168404Spjd cmdname = argv[1]; 5464168404Spjd 5465168404Spjd /* 5466168404Spjd * Special case '-?' 5467168404Spjd */ 5468168404Spjd if (strcmp(cmdname, "-?") == 0) 5469168404Spjd usage(B_TRUE); 5470168404Spjd 5471248571Smm zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 5472185029Spjd 5473168404Spjd /* 5474168404Spjd * Run the appropriate command. 5475168404Spjd */ 5476168404Spjd if (find_command_idx(cmdname, &i) == 0) { 5477168404Spjd current_command = &command_table[i]; 5478168404Spjd ret = command_table[i].func(argc - 1, argv + 1); 5479185029Spjd } else if (strchr(cmdname, '=')) { 5480185029Spjd verify(find_command_idx("set", &i) == 0); 5481185029Spjd current_command = &command_table[i]; 5482185029Spjd ret = command_table[i].func(argc, argv); 5483185029Spjd } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) { 5484185029Spjd /* 5485185029Spjd * 'freeze' is a vile debugging abomination, so we treat 5486185029Spjd * it as such. 5487185029Spjd */ 5488252059Ssmh zfs_cmd_t zc = { 0 }; 5489252059Ssmh (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name)); 5490252059Ssmh return (!!zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc)); 5491185029Spjd } else { 5492168404Spjd (void) fprintf(stderr, gettext("unrecognized " 5493168404Spjd "command '%s'\n"), cmdname); 5494168404Spjd usage(B_FALSE); 5495168404Spjd } 5496168404Spjd 5497248571Smm if (ret == 0 && log_history) 5498248571Smm (void) zpool_log_history(g_zfs, history_str); 5499248571Smm 5500168404Spjd libzfs_fini(g_zfs); 5501168404Spjd 5502168404Spjd /* 5503168404Spjd * The 'ZFS_ABORT' environment variable causes us to dump core on exit 5504168404Spjd * for the purposes of running ::findleaks. 5505168404Spjd */ 5506168404Spjd if (getenv("ZFS_ABORT") != NULL) { 5507168404Spjd (void) printf("dumping core by request\n"); 5508168404Spjd abort(); 5509168404Spjd } 5510168404Spjd 5511168404Spjd return (ret); 5512168404Spjd} 5513