usr/src/cmd/zpool/zpool_main.c
author ck153898
Mon, 29 Oct 2007 22:45:33 -0700
changeset 5378 111aa1baa84a
parent 5363 36eeffc5336d
child 5390 d93266868417
permissions -rw-r--r--
PSARC 2007/555 zfs fs-only quotas and reservations 6431277 want filesystem-only quotas 6483677 need immediate reservation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
     5
 * Common Development and Distribution License (the "License").
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
    21
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
3377
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3126
diff changeset
    23
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <assert.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <ctype.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <dirent.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <fcntl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <libgen.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <libintl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <libuutil.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <locale.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <stdio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <stdlib.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <string.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include <strings.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#include <unistd.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include <priv.h>
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
    44
#include <pwd.h>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
    45
#include <zone.h>
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
    46
#include <sys/fs/zfs.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
#include <sys/stat.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
#include <libzfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
#include "zpool_util.h"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
static int zpool_do_create(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
static int zpool_do_destroy(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
static int zpool_do_add(int, char **);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
    58
static int zpool_do_remove(int, char **);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
static int zpool_do_list(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
static int zpool_do_iostat(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
static int zpool_do_status(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
static int zpool_do_online(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
static int zpool_do_offline(int, char **);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
    66
static int zpool_do_clear(int, char **);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
static int zpool_do_attach(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
static int zpool_do_detach(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
static int zpool_do_replace(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
static int zpool_do_scrub(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
static int zpool_do_import(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
static int zpool_do_export(int, char **);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
    77
static int zpool_do_upgrade(int, char **);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
    78
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
    79
static int zpool_do_history(int, char **);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
    80
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
    81
static int zpool_do_get(int, char **);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
    82
static int zpool_do_set(int, char **);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
    83
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
 * These libumem hooks provide a reasonable set of defaults for the allocator's
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
 * debugging facilities.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
const char *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
    89
_umem_debug_init(void)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
	return ("default,verbose"); /* $UMEM_DEBUG setting */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
_umem_logging_init(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
	return ("fail,contents"); /* $UMEM_LOGGING setting */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   100
typedef enum {
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   101
	HELP_ADD,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   102
	HELP_ATTACH,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   103
	HELP_CLEAR,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   104
	HELP_CREATE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   105
	HELP_DESTROY,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   106
	HELP_DETACH,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   107
	HELP_EXPORT,
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
   108
	HELP_HISTORY,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   109
	HELP_IMPORT,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   110
	HELP_IOSTAT,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   111
	HELP_LIST,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   112
	HELP_OFFLINE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   113
	HELP_ONLINE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   114
	HELP_REPLACE,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   115
	HELP_REMOVE,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   116
	HELP_SCRUB,
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   117
	HELP_STATUS,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   118
	HELP_UPGRADE,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   119
	HELP_GET,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   120
	HELP_SET
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   121
} zpool_help_t;
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   122
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   123
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
typedef struct zpool_command {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
	const char	*name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
	int		(*func)(int, char **);
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   127
	zpool_help_t	usage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
} zpool_command_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
 * Master command table.  Each ZFS command has a name, associated function, and
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   132
 * usage message.  The usage messages need to be internationalized, so we have
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   133
 * to have a function to return the usage message based on a command index.
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   134
 *
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   135
 * These commands are organized according to how they are displayed in the usage
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   136
 * message.  An empty command (one with a NULL name) indicates an empty line in
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   137
 * the generic usage message.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
static zpool_command_t command_table[] = {
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   140
	{ "create",	zpool_do_create,	HELP_CREATE		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   141
	{ "destroy",	zpool_do_destroy,	HELP_DESTROY		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   143
	{ "add",	zpool_do_add,		HELP_ADD		},
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   144
	{ "remove",	zpool_do_remove,	HELP_REMOVE		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   146
	{ "list",	zpool_do_list,		HELP_LIST		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   147
	{ "iostat",	zpool_do_iostat,	HELP_IOSTAT		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   148
	{ "status",	zpool_do_status,	HELP_STATUS		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   150
	{ "online",	zpool_do_online,	HELP_ONLINE		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   151
	{ "offline",	zpool_do_offline,	HELP_OFFLINE		},
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   152
	{ "clear",	zpool_do_clear,		HELP_CLEAR		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   154
	{ "attach",	zpool_do_attach,	HELP_ATTACH		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   155
	{ "detach",	zpool_do_detach,	HELP_DETACH		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   156
	{ "replace",	zpool_do_replace,	HELP_REPLACE		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   158
	{ "scrub",	zpool_do_scrub,		HELP_SCRUB		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   159
	{ NULL },
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   160
	{ "import",	zpool_do_import,	HELP_IMPORT		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   161
	{ "export",	zpool_do_export,	HELP_EXPORT		},
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
   162
	{ "upgrade",	zpool_do_upgrade,	HELP_UPGRADE		},
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
   163
	{ NULL },
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   164
	{ "history",	zpool_do_history,	HELP_HISTORY		},
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   165
	{ "get",	zpool_do_get,		HELP_GET		},
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   166
	{ "set",	zpool_do_set,		HELP_SET		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   169
#define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
zpool_command_t *current_command;
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4849
diff changeset
   172
static char history_str[HIS_MAX_RECORD_LEN];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   173
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   174
static const char *
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   175
get_usage(zpool_help_t idx) {
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   176
	switch (idx) {
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   177
	case HELP_ADD:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   178
		return (gettext("\tadd [-fn] <pool> <vdev> ...\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   179
	case HELP_ATTACH:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   180
		return (gettext("\tattach [-f] <pool> <device> "
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4820
diff changeset
   181
		    "<new-device>\n"));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   182
	case HELP_CLEAR:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
   183
		return (gettext("\tclear <pool> [device]\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   184
	case HELP_CREATE:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   185
		return (gettext("\tcreate [-fn] [-o property=value] ... \n"
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   186
		    "\t    [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   187
	case HELP_DESTROY:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   188
		return (gettext("\tdestroy [-f] <pool>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   189
	case HELP_DETACH:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   190
		return (gettext("\tdetach <pool> <device>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   191
	case HELP_EXPORT:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   192
		return (gettext("\texport [-f] <pool> ...\n"));
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
   193
	case HELP_HISTORY:
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
   194
		return (gettext("\thistory [-il] [<pool>] ...\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   195
	case HELP_IMPORT:
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
   196
		return (gettext("\timport [-d dir] [-D]\n"
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   197
		    "\timport [-o mntopts] [-o property=value] ... \n"
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   198
		    "\t    [-d dir | -c cachefile] [-D] [-f] [-R root] -a\n"
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   199
		    "\timport [-o mntopts] [-o property=value] ... \n"
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   200
		    "\t    [-d dir | -c cachefile] [-D] [-f] [-R root] "
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   201
		    "<pool | id> [newpool]\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   202
	case HELP_IOSTAT:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   203
		return (gettext("\tiostat [-v] [pool] ... [interval "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   204
		    "[count]]\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   205
	case HELP_LIST:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   206
		return (gettext("\tlist [-H] [-o property[,...]] "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   207
		    "[pool] ...\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   208
	case HELP_OFFLINE:
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
   209
		return (gettext("\toffline [-t] <pool> <device> ...\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   210
	case HELP_ONLINE:
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
   211
		return (gettext("\tonline <pool> <device> ...\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   212
	case HELP_REPLACE:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   213
		return (gettext("\treplace [-f] <pool> <device> "
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4820
diff changeset
   214
		    "[new-device]\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   215
	case HELP_REMOVE:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   216
		return (gettext("\tremove <pool> <device>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   217
	case HELP_SCRUB:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   218
		return (gettext("\tscrub [-s] <pool> ...\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   219
	case HELP_STATUS:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   220
		return (gettext("\tstatus [-vx] [pool] ...\n"));
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   221
	case HELP_UPGRADE:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   222
		return (gettext("\tupgrade\n"
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   223
		    "\tupgrade -v\n"
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   224
		    "\tupgrade [-V version] <-a | pool ...>\n"));
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   225
	case HELP_GET:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4820
diff changeset
   226
		return (gettext("\tget <\"all\" | property[,...]> "
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   227
		    "<pool> ...\n"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   228
	case HELP_SET:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   229
		return (gettext("\tset <property=value> <pool> \n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   230
	}
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   231
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   232
	abort();
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   233
	/* NOTREACHED */
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   234
}
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   235
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
/*
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   238
 * Callback routine that will print out a pool property value.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   239
 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   240
static int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   241
print_prop_cb(int prop, void *cb)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   242
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   243
	FILE *fp = cb;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   244
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   245
	(void) fprintf(fp, "\t%-13s  ", zpool_prop_to_name(prop));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   246
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   247
	if (zpool_prop_readonly(prop))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   248
		(void) fprintf(fp, "  NO   ");
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   249
	else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   250
		(void) fprintf(fp, " YES    ");
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   251
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   252
	if (zpool_prop_values(prop) == NULL)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   253
		(void) fprintf(fp, "-\n");
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   254
	else
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   255
		(void) fprintf(fp, "%s\n", zpool_prop_values(prop));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   256
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   257
	return (ZPROP_CONT);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   258
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   259
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   260
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
 * Display usage message.  If we're inside a command, display only the usage for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
 * that command.  Otherwise, iterate over the entire command table and display
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
 * a complete usage message.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
void
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   266
usage(boolean_t requested)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
	FILE *fp = requested ? stdout : stderr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
	if (current_command == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
		int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
		(void) fprintf(fp, gettext("usage: zpool command args ...\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
		(void) fprintf(fp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
		    gettext("where 'command' is one of the following:\n\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
		for (i = 0; i < NCOMMAND; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
			if (command_table[i].name == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
				(void) fprintf(fp, "\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
				(void) fprintf(fp, "%s",
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   282
				    get_usage(command_table[i].usage));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   284
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
		(void) fprintf(fp, gettext("usage:\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1354
diff changeset
   286
		(void) fprintf(fp, "%s", get_usage(current_command->usage));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   289
	if (current_command != NULL &&
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   290
	    ((strcmp(current_command->name, "set") == 0) ||
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   291
	    (strcmp(current_command->name, "get") == 0) ||
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   292
	    (strcmp(current_command->name, "list") == 0))) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   293
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   294
		(void) fprintf(fp,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   295
		    gettext("\nthe following properties are supported:\n"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   296
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   297
		(void) fprintf(fp, "\n\t%-13s  %s  %s\n\n",
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   298
		    "PROPERTY", "EDIT", "VALUES");
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   299
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   300
		/* Iterate over all properties */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   301
		(void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   302
		    ZFS_TYPE_POOL);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   303
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   304
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   305
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   306
	 * See comments at end of main().
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   307
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   308
	if (getenv("ZFS_ABORT") != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   309
		(void) printf("dumping core by request\n");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   310
		abort();
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   311
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   312
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
	exit(requested ? 0 : 2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
void
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   317
print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   318
    boolean_t print_logs)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
	nvlist_t **child;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
	uint_t c, children;
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   322
	char *vname;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
	if (name != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
		(void) printf("\t%*s%s\n", indent, "", name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
	    &child, &children) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   331
	for (c = 0; c < children; c++) {
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   332
		uint64_t is_log = B_FALSE;
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   333
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   334
		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   335
		    &is_log);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   336
		if ((is_log && !print_logs) || (!is_log && print_logs))
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   337
			continue;
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   338
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   339
		vname = zpool_vdev_name(g_zfs, zhp, child[c]);
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   340
		print_vdev_tree(zhp, vname, child[c], indent + 2,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   341
		    B_FALSE);
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   342
		free(vname);
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   343
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   347
 * Add a property pair (name, string-value) into a property nvlist.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   348
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   349
static int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   350
add_prop_list(const char *propname, char *propval, nvlist_t **props)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   351
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   352
	char *strval;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   353
	nvlist_t *proplist;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   354
	zpool_prop_t prop;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   355
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   356
	if (*props == NULL &&
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   357
	    nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   358
		(void) fprintf(stderr,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   359
		    gettext("internal error: out of memory\n"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   360
		return (1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   361
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   362
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   363
	proplist = *props;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   364
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   365
	if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   366
		(void) fprintf(stderr, gettext("property '%s' is "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   367
		    "not a valid pool property\n"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   368
		return (2);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   369
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   370
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   371
	/* Use normalized property name for nvlist operations */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   372
	if (nvlist_lookup_string(proplist, zpool_prop_to_name(prop),
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   373
	    &strval) == 0 && prop != ZPOOL_PROP_CACHEFILE) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   374
		(void) fprintf(stderr, gettext("property '%s' "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   375
		    "specified multiple times\n"), propname);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   376
		return (2);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   377
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   378
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   379
	if (nvlist_add_string(proplist, zpool_prop_to_name(prop),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   380
	    propval) != 0) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   381
		(void) fprintf(stderr, gettext("internal "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   382
		    "error: out of memory\n"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   383
		return (1);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   384
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   385
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   386
	return (0);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   387
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   388
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   389
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
 * zpool add [-fn] <pool> <vdev> ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   392
 *	-f	Force addition of devices, even if they appear in use
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
 *	-n	Do not add the devices, but display the resulting layout if
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
 *		they were to be added.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
 * Adds the given vdevs to 'pool'.  As with create, the bulk of this work is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
 * libzfs.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
zpool_do_add(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   403
	boolean_t force = B_FALSE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   404
	boolean_t dryrun = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
	nvlist_t *nvroot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
	char *poolname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
	zpool_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
	nvlist_t *config;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
	while ((c = getopt(argc, argv, "fn")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
		case 'f':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   416
			force = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
		case 'n':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   419
			dryrun = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   424
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   425
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   426
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
	/* get pool name and check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   434
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
		(void) fprintf(stderr, gettext("missing vdev specification\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   438
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
	poolname = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
	argc--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
	argv++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   446
	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
   449
	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
		    poolname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   456
	/* pass off to get_vdev_spec for processing */
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 4221
diff changeset
   457
	nvroot = make_root_vdev(zhp, force, !force, B_FALSE, argc, argv);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
	if (nvroot == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
	if (dryrun) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
		nvlist_t *poolnvroot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
		    &poolnvroot) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   469
		(void) printf(gettext("would update '%s' to the following "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
		    "configuration:\n"), zpool_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   472
		/* print original main pool and new tree */
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   473
		print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   474
		print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   475
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   476
		/* Do the same for the logs */
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   477
		if (num_logs(poolnvroot) > 0) {
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   478
			print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   479
			print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   480
		} else if (num_logs(nvroot) > 0) {
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   481
			print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   482
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
		ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
		ret = (zpool_add(zhp, nvroot) != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   489
	nvlist_free(nvroot);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   490
	zpool_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   491
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   492
	return (ret);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   493
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   494
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   495
/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   496
 * zpool remove <pool> <vdev>
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   497
 *
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   498
 * Removes the given vdev from the pool.  Currently, this only supports removing
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   499
 * spares from the pool.  Eventually, we'll want to support removing leaf vdevs
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   500
 * (as an alias for 'detach') as well as toplevel vdevs.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   501
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   502
int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   503
zpool_do_remove(int argc, char **argv)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   504
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   505
	char *poolname;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   506
	int ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   507
	zpool_handle_t *zhp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   508
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   509
	argc--;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   510
	argv++;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   511
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   512
	/* get pool name and check number of arguments */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   513
	if (argc < 1) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   514
		(void) fprintf(stderr, gettext("missing pool name argument\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   515
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   516
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   517
	if (argc < 2) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   518
		(void) fprintf(stderr, gettext("missing device\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   519
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   520
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   521
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   522
	poolname = argv[0];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   523
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   524
	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   525
		return (1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   526
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   527
	ret = (zpool_vdev_remove(zhp, argv[1]) != 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   528
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   533
 * zpool create [-fn] [-o property=value] ... [-R root] [-m mountpoint]
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   534
 *		<pool> <dev> ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
 *	-f	Force creation, even if devices appear in use
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
 *	-n	Do not create the pool, but display the resulting layout if it
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
 *		were to be created.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
 *      -R	Create a pool under an alternate root
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
 *      -m	Set default mountpoint for the root dataset.  By default it's
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
 *      	'/<pool>'
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   542
 *	-o	Set property=value.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
 *
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   544
 * Creates the named pool according to the given vdev specification.  The
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c.  Once
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
 * we get the nvlist back from get_vdev_spec(), we either print out the contents
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
 * (if '-n' was specified), or pass it to libzfs to do the creation.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
zpool_do_create(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   552
	boolean_t force = B_FALSE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   553
	boolean_t dryrun = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
	int c;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   555
	nvlist_t *nvroot = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
	char *poolname;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   557
	int ret = 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
	char *altroot = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
	char *mountpoint = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   560
	nvlist_t **child;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   561
	uint_t children;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   562
	nvlist_t *props = NULL;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   563
	char *propval;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
	/* check options */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   566
	while ((c = getopt(argc, argv, ":fnR:m:o:")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
		case 'f':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   569
			force = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
		case 'n':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   572
			dryrun = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
		case 'R':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
			altroot = optarg;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   576
			if (add_prop_list(zpool_prop_to_name(
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   577
			    ZPOOL_PROP_ALTROOT), optarg, &props))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   578
				goto errout;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   579
			if (nvlist_lookup_string(props,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   580
			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   581
			    &propval) == 0)
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   582
				break;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   583
			if (add_prop_list(zpool_prop_to_name(
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   584
			    ZPOOL_PROP_CACHEFILE), "none", &props))
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   585
				goto errout;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
		case 'm':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
			mountpoint = optarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
			break;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   590
		case 'o':
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   591
			if ((propval = strchr(optarg, '=')) == NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   592
				(void) fprintf(stderr, gettext("missing "
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   593
				    "'=' for -o option\n"));
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   594
				goto errout;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   595
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   596
			*propval = '\0';
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   597
			propval++;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   598
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   599
			if (add_prop_list(optarg, propval, &props))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   600
				goto errout;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   601
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
			    "'%c' option\n"), optopt);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   605
			goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
			    optopt);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   609
			goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   616
	/* get pool name and check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
		(void) fprintf(stderr, gettext("missing pool name argument\n"));
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   619
		goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
		(void) fprintf(stderr, gettext("missing vdev specification\n"));
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   623
		goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
	poolname = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
	 * As a special case, check for use of '/' in the name, and direct the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
	 * user to use 'zfs create' instead.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
	if (strchr(poolname, '/') != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
		(void) fprintf(stderr, gettext("cannot create '%s': invalid "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
		    "character '/' in pool name\n"), poolname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
		(void) fprintf(stderr, gettext("use 'zfs create' to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
		    "create a dataset\n"));
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   637
		goto errout;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
	/* pass off to get_vdev_spec for bulk processing */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   641
	nvroot = make_root_vdev(NULL, force, !force, B_FALSE, argc - 1,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   642
	    argv + 1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
	if (nvroot == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   646
	/* make_root_vdev() allows 0 toplevel children if there are spares */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   647
	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   648
	    &child, &children) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   649
	if (children == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   650
		(void) fprintf(stderr, gettext("invalid vdev "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   651
		    "specification: at least one toplevel vdev must be "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   652
		    "specified\n"));
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   653
		goto errout;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   654
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   655
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   656
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
	if (altroot != NULL && altroot[0] != '/') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
		(void) fprintf(stderr, gettext("invalid alternate root '%s': "
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   659
		    "must be an absolute path\n"), altroot);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   660
		goto errout;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
	 * Check the validity of the mountpoint and direct the user to use the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
	 * '-m' mountpoint option if it looks like its in use.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
	if (mountpoint == NULL ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
	    (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
	    strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
		char buf[MAXPATHLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
		struct stat64 statbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
		if (mountpoint && mountpoint[0] != '/') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
			(void) fprintf(stderr, gettext("invalid mountpoint "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
			    "'%s': must be an absolute path, 'legacy', or "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
			    "'none'\n"), mountpoint);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   677
			goto errout;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
		if (mountpoint == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
			if (altroot != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
				(void) snprintf(buf, sizeof (buf), "%s/%s",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
				    altroot, poolname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
				(void) snprintf(buf, sizeof (buf), "/%s",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
				    poolname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
			if (altroot != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
				(void) snprintf(buf, sizeof (buf), "%s%s",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
				    altroot, mountpoint);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
				(void) snprintf(buf, sizeof (buf), "%s",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
				    mountpoint);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
		if (stat64(buf, &statbuf) == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
		    statbuf.st_nlink != 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
			if (mountpoint == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   699
				(void) fprintf(stderr, gettext("default "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
				    "mountpoint '%s' exists and is not "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
				    "empty\n"), buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
				(void) fprintf(stderr, gettext("mountpoint "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   704
				    "'%s' exists and is not empty\n"), buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
			(void) fprintf(stderr, gettext("use '-m' "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
			    "option to provide a different default\n"));
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   707
			goto errout;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
	if (dryrun) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
		 * For a dry run invocation, print out a basic message and run
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
		 * through all the vdevs in the list and print out in an
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
		 * appropriate hierarchy.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
		(void) printf(gettext("would create '%s' with the "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
		    "following layout:\n\n"), poolname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   720
		print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   721
		if (num_logs(nvroot) > 0)
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   722
			print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   723
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
		ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
		 * Hand off to libzfs.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
		 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   729
		if (zpool_create(g_zfs, poolname, nvroot, props) == 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   730
			zfs_handle_t *pool = zfs_open(g_zfs, poolname,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
			    ZFS_TYPE_FILESYSTEM);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
			if (pool != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
				if (mountpoint != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
					verify(zfs_prop_set(pool,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   735
					    zfs_prop_to_name(
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
   736
					    ZFS_PROP_MOUNTPOINT),
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
					    mountpoint) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
				if (zfs_mount(pool, NULL, 0) == 0)
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5320
diff changeset
   739
					ret = zfs_shareall(pool);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
				zfs_close(pool);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
			}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   742
		} else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   743
			(void) fprintf(stderr, gettext("pool name may have "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   744
			    "been omitted\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   748
errout:
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   749
	nvlist_free(nvroot);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
   750
	nvlist_free(props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
	return (ret);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   752
badusage:
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   753
	nvlist_free(props);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   754
	usage(B_FALSE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   755
	return (2);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
 * zpool destroy <pool>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
 * 	-f	Forcefully unmount any datasets
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
 * Destroy the given pool.  Automatically unmounts any datasets in the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
zpool_do_destroy(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   768
	boolean_t force = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
	char *pool;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
	zpool_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   775
	while ((c = getopt(argc, argv, "f")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
		case 'f':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   778
			force = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   783
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
	/* check arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
		(void) fprintf(stderr, gettext("missing pool argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   793
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   794
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   797
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   798
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   799
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
	pool = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   802
	if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   803
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
		 * As a special case, check for use of '/' in the name, and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
		 * direct the user to use 'zfs destroy' instead.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
		if (strchr(pool, '/') != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
			(void) fprintf(stderr, gettext("use 'zfs destroy' to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
			    "destroy a dataset\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
   813
	if (zpool_disable_datasets(zhp, force) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
		(void) fprintf(stderr, gettext("could not destroy '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   815
		    "could not unmount datasets\n"), zpool_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   819
	ret = (zpool_destroy(zhp) != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
	zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   823
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   827
 * zpool export [-f] <pool> ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   828
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
 *	-f	Forcefully unmount datasets
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
 *
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   831
 * Export the given pools.  By default, the command will attempt to cleanly
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
 * unmount any active datasets within the pool.  If the '-f' flag is specified,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   833
 * then the datasets will be forcefully unmounted.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
zpool_do_export(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   837
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   838
	boolean_t force = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   839
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   840
	zpool_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
	while ((c = getopt(argc, argv, "f")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
		case 'f':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   848
			force = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   851
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   853
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   859
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
	/* check arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
		(void) fprintf(stderr, gettext("missing pool argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   863
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
	ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
	for (i = 0; i < argc; i++) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   868
		if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
			ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
   873
		if (zpool_disable_datasets(zhp, force) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
			ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   875
			zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   876
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   878
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   879
		if (zpool_export(zhp) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   880
			ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   881
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   882
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   883
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
 * Given a vdev configuration, determine the maximum width needed for the device
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
 * name column.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   892
static int
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
   893
max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   895
	char *name = zpool_vdev_name(g_zfs, zhp, nv);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
	nvlist_t **child;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   897
	uint_t c, children;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   898
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
	if (strlen(name) + depth > max)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
		max = strlen(name) + depth;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   903
	free(name);
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   904
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   905
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   906
	    &child, &children) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   907
		for (c = 0; c < children; c++)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   908
			if ((ret = max_width(zhp, child[c], depth + 2,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   909
			    max)) > max)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   910
				max = ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   911
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   912
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   913
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   914
	    &child, &children) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   915
		for (c = 0; c < children; c++)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   916
			if ((ret = max_width(zhp, child[c], depth + 2,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   917
			    max)) > max)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   918
				max = ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   919
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   920
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   921
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   922
	return (max);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   923
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   924
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   925
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   926
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   927
 * Print the configuration of an exported pool.  Iterate over all vdevs in the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   928
 * pool, printing out the name and status for each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   930
void
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   931
print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   932
    boolean_t print_logs)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
	nvlist_t **child;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   935
	uint_t c, children;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
	vdev_stat_t *vs;
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   937
	char *type, *vname;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   938
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
	if (strcmp(type, VDEV_TYPE_MISSING) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   942
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   944
	    (uint64_t **)&vs, &c) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   945
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   946
	(void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   947
	(void) printf("  %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   948
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   949
	if (vs->vs_aux != 0) {
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
   950
		(void) printf("  ");
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   951
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   952
		switch (vs->vs_aux) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
		case VDEV_AUX_OPEN_FAILED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
			(void) printf(gettext("cannot open"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   955
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   956
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   957
		case VDEV_AUX_BAD_GUID_SUM:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   958
			(void) printf(gettext("missing device"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   959
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   961
		case VDEV_AUX_NO_REPLICAS:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   962
			(void) printf(gettext("insufficient replicas"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   963
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   965
		case VDEV_AUX_VERSION_NEWER:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   966
			(void) printf(gettext("newer version"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   967
			break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
   968
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
   969
		case VDEV_AUX_ERR_EXCEEDED:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
   970
			(void) printf(gettext("too many errors"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
   971
			break;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
   972
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   973
		default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   974
			(void) printf(gettext("corrupted data"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   975
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   976
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   977
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   978
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   979
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   980
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   981
	    &child, &children) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   982
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   983
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   984
	for (c = 0; c < children; c++) {
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   985
		uint64_t is_log = B_FALSE;
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   986
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   987
		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   988
		    &is_log);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   989
		if ((is_log && !print_logs) || (!is_log && print_logs))
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   990
			continue;
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   991
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   992
		vname = zpool_vdev_name(g_zfs, NULL, child[c]);
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   993
		print_import_config(vname, child[c],
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
   994
		    namewidth, depth + 2, B_FALSE);
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   995
		free(vname);
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
   996
	}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   997
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   998
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
   999
	    &child, &children) != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1000
		return;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1001
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1002
	(void) printf(gettext("\tspares\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1003
	for (c = 0; c < children; c++) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1004
		vname = zpool_vdev_name(g_zfs, NULL, child[c]);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1005
		(void) printf("\t  %s\n", vname);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1006
		free(vname);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1007
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1008
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
 * Display the status for the given pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1012
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1014
show_import(nvlist_t *config)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1015
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1016
	uint64_t pool_state;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1017
	vdev_stat_t *vs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1018
	char *name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
	uint64_t guid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1020
	char *msgid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1021
	nvlist_t *nvroot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1022
	int reason;
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1023
	const char *health;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1024
	uint_t vsc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1025
	int namewidth;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1026
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1027
	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1028
	    &name) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1029
	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1030
	    &guid) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1031
	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1032
	    &pool_state) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1033
	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1034
	    &nvroot) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1035
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1036
	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1037
	    (uint64_t **)&vs, &vsc) == 0);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1038
	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1039
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1040
	reason = zpool_import_status(config, &msgid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1041
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1042
	(void) printf(gettext("  pool: %s\n"), name);
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1043
	(void) printf(gettext("    id: %llu\n"), (u_longlong_t)guid);
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1044
	(void) printf(gettext(" state: %s"), health);
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1045
	if (pool_state == POOL_STATE_DESTROYED)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1046
		(void) printf(gettext(" (DESTROYED)"));
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1047
	(void) printf("\n");
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1048
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1049
	switch (reason) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1050
	case ZPOOL_STATUS_MISSING_DEV_R:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1051
	case ZPOOL_STATUS_MISSING_DEV_NR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1052
	case ZPOOL_STATUS_BAD_GUID_SUM:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1053
		(void) printf(gettext("status: One or more devices are missing "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1054
		    "from the system.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1055
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1056
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1057
	case ZPOOL_STATUS_CORRUPT_LABEL_R:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1058
	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1059
		(void) printf(gettext("status: One or more devices contains "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1060
		    "corrupted data.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1061
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1062
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
	case ZPOOL_STATUS_CORRUPT_DATA:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1064
		(void) printf(gettext("status: The pool data is corrupted.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1065
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1066
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  1067
	case ZPOOL_STATUS_OFFLINE_DEV:
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  1068
		(void) printf(gettext("status: One or more devices "
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  1069
		    "are offlined.\n"));
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  1070
		break;
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  1071
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1072
	case ZPOOL_STATUS_CORRUPT_POOL:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1073
		(void) printf(gettext("status: The pool metadata is "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1074
		    "corrupted.\n"));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1075
		break;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  1076
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1077
	case ZPOOL_STATUS_VERSION_OLDER:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1078
		(void) printf(gettext("status: The pool is formatted using an "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1079
		    "older on-disk version.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1080
		break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1081
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1082
	case ZPOOL_STATUS_VERSION_NEWER:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1083
		(void) printf(gettext("status: The pool is formatted using an "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1084
		    "incompatible version.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1085
		break;
3975
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1086
	case ZPOOL_STATUS_HOSTID_MISMATCH:
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1087
		(void) printf(gettext("status: The pool was last accessed by "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1088
		    "another system.\n"));
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1089
		break;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  1090
	case ZPOOL_STATUS_FAULTED_DEV_R:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  1091
	case ZPOOL_STATUS_FAULTED_DEV_NR:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  1092
		(void) printf(gettext("status: One or more devices are "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  1093
		    "faulted.\n"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  1094
		break;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  1095
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1096
	default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1097
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1098
		 * No other status can be seen when importing pools.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1099
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1100
		assert(reason == ZPOOL_STATUS_OK);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1101
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1102
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1103
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
	 * Print out an action according to the overall state of the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
	 */
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1106
	if (vs->vs_state == VDEV_STATE_HEALTHY) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1107
		if (reason == ZPOOL_STATUS_VERSION_OLDER)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1108
			(void) printf(gettext("action: The pool can be "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1109
			    "imported using its name or numeric identifier, "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1110
			    "though\n\tsome features will not be available "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1111
			    "without an explicit 'zpool upgrade'.\n"));
3975
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1112
		else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH)
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1113
			(void) printf(gettext("action: The pool can be "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1114
			    "imported using its name or numeric "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1115
			    "identifier and\n\tthe '-f' flag.\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1116
		else
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1117
			(void) printf(gettext("action: The pool can be "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1118
			    "imported using its name or numeric "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1119
			    "identifier.\n"));
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1120
	} else if (vs->vs_state == VDEV_STATE_DEGRADED) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1121
		(void) printf(gettext("action: The pool can be imported "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1122
		    "despite missing or damaged devices.  The\n\tfault "
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1123
		    "tolerance of the pool may be compromised if imported.\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1124
	} else {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1125
		switch (reason) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1126
		case ZPOOL_STATUS_VERSION_NEWER:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1127
			(void) printf(gettext("action: The pool cannot be "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1128
			    "imported.  Access the pool on a system running "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1129
			    "newer\n\tsoftware, or recreate the pool from "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1130
			    "backup.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1131
			break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1132
		case ZPOOL_STATUS_MISSING_DEV_R:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1133
		case ZPOOL_STATUS_MISSING_DEV_NR:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1134
		case ZPOOL_STATUS_BAD_GUID_SUM:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1135
			(void) printf(gettext("action: The pool cannot be "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1136
			    "imported. Attach the missing\n\tdevices and try "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
			    "again.\n"));
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1138
			break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1139
		default:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
			(void) printf(gettext("action: The pool cannot be "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
			    "imported due to damaged devices or data.\n"));
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1142
		}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1143
	}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1144
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1145
	/*
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1146
	 * If the state is "closed" or "can't open", and the aux state
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1147
	 * is "corrupt data":
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1148
	 */
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1149
	if (((vs->vs_state == VDEV_STATE_CLOSED) ||
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1150
	    (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1151
	    (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1152
		if (pool_state == POOL_STATE_DESTROYED)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1153
			(void) printf(gettext("\tThe pool was destroyed, "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1154
			    "but can be imported using the '-Df' flags.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1155
		else if (pool_state != POOL_STATE_EXPORTED)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1156
			(void) printf(gettext("\tThe pool may be active on "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1157
			    "on another system, but can be imported using\n\t"
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1158
			    "the '-f' flag.\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1159
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
	if (msgid != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1162
		(void) printf(gettext("   see: http://www.sun.com/msg/%s\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
		    msgid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
	(void) printf(gettext("config:\n\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  1167
	namewidth = max_width(NULL, nvroot, 0, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1168
	if (namewidth < 10)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1169
		namewidth = 10;
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1170
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1171
	print_import_config(name, nvroot, namewidth, 0, B_FALSE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1172
	if (num_logs(nvroot) > 0) {
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1173
		(void) printf(gettext("\tlogs\n"));
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1174
		print_import_config(name, nvroot, namewidth, 0, B_TRUE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  1175
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1176
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1177
	if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1178
		(void) printf(gettext("\n\tAdditional devices are known to "
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1179
		    "be part of this pool, though their\n\texact "
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  1180
		    "configuration cannot be determined.\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1184
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1185
 * Perform the import for the given configuration.  This passes the heavy
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1186
 * lifting off to zpool_import_props(), and then mounts the datasets contained
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1187
 * within the pool.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1188
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1189
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1190
do_import(nvlist_t *config, const char *newname, const char *mntopts,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1191
    int force, nvlist_t *props)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1192
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1193
	zpool_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1194
	char *name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
	uint64_t state;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1196
	uint64_t version;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1197
	int error = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1199
	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
	    &name) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1202
	verify(nvlist_lookup_uint64(config,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1203
	    ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1204
	verify(nvlist_lookup_uint64(config,
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1205
	    ZPOOL_CONFIG_VERSION, &version) == 0);
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1206
	if (version > SPA_VERSION) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1207
		(void) fprintf(stderr, gettext("cannot import '%s': pool "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1208
		    "is formatted using a newer ZFS version\n"), name);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1209
		return (1);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  1210
	} else if (state != POOL_STATE_EXPORTED && !force) {
3975
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1211
		uint64_t hostid;
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1212
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1213
		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1214
		    &hostid) == 0) {
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1215
			if ((unsigned long)hostid != gethostid()) {
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1216
				char *hostname;
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1217
				uint64_t timestamp;
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1218
				time_t t;
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1219
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1220
				verify(nvlist_lookup_string(config,
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1221
				    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1222
				verify(nvlist_lookup_uint64(config,
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1223
				    ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1224
				t = timestamp;
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1225
				(void) fprintf(stderr, gettext("cannot import "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1226
				    "'%s': pool may be in use from other "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1227
				    "system, it was last accessed by %s "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1228
				    "(hostid: 0x%lx) on %s"), name, hostname,
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1229
				    (unsigned long)hostid,
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1230
				    asctime(localtime(&t)));
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1231
				(void) fprintf(stderr, gettext("use '-f' to "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1232
				    "import anyway\n"));
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1233
				return (1);
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1234
			}
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1235
		} else {
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1236
			(void) fprintf(stderr, gettext("cannot import '%s': "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1237
			    "pool may be in use from other system\n"), name);
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1238
			(void) fprintf(stderr, gettext("use '-f' to import "
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1239
			    "anyway\n"));
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1240
			return (1);
6674f5d79069 6282725 hostname/hostid should be stored in the label
ek110237
parents: 3912
diff changeset
  1241
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1242
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1243
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1244
	if (zpool_import_props(g_zfs, config, newname, props) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1245
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1246
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1247
	if (newname != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1248
		name = (char *)newname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1249
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1250
	verify((zhp = zpool_open(g_zfs, name)) != NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1251
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  1252
	if (zpool_enable_datasets(zhp, mntopts, 0) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1253
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1254
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1255
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1256
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1257
	zpool_close(zhp);
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1258
	return (error);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1259
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1260
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1261
/*
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1262
 * zpool import [-d dir] [-D]
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1263
 *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1264
 *              [-d dir | -c cachefile] [-f] -a
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1265
 *       import [-o mntopts] [-o prop=value] ... [-R root] [-D]
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1266
 *              [-d dir | -c cachefile] [-f] <pool | id> [newpool]
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1267
 *
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1268
 *	 -c	Read pool information from a cachefile instead of searching
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1269
 *		devices.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1270
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1271
 *       -d	Scan in a specific directory, other than /dev/dsk.  More than
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1272
 *		one directory can be specified using multiple '-d' options.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1273
 *
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1274
 *       -D     Scan for previously destroyed pools or import all or only
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1275
 *              specified destroyed pools.
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1276
 *
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1277
 *       -R	Temporarily import the pool, with all mountpoints relative to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1278
 *		the given root.  The pool will remain exported when the machine
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1279
 *		is rebooted.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1280
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1281
 *       -f	Force import, even if it appears that the pool is active.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1282
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1283
 *       -a	Import all pools found.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1284
 *
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1285
 *       -o	Set property=value and/or temporary mount options (without '=').
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1286
 *
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1287
 * The import command scans for pools to import, and import pools based on pool
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1288
 * name and GUID.  The pool can also be renamed as part of the import process.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1289
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1290
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1291
zpool_do_import(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1292
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1293
	char **searchdirs = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1294
	int nsearch = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1295
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1296
	int err;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1297
	nvlist_t *pools = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1298
	boolean_t do_all = B_FALSE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1299
	boolean_t do_destroyed = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1300
	char *mntopts = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1301
	boolean_t do_force = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1302
	nvpair_t *elem;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1303
	nvlist_t *config;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1304
	uint64_t searchguid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1305
	char *searchname;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1306
	char *propval;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1307
	nvlist_t *found_config;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1308
	nvlist_t *props = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1309
	boolean_t first;
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1310
	uint64_t pool_state;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1311
	char *cachefile = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1312
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1313
	/* check options */
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1314
	while ((c = getopt(argc, argv, ":afc:d:Do:p:R:")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1315
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1316
		case 'a':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1317
			do_all = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1318
			break;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1319
		case 'c':
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1320
			cachefile = optarg;
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1321
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1322
		case 'd':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1323
			if (searchdirs == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1324
				searchdirs = safe_malloc(sizeof (char *));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1325
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1326
				char **tmp = safe_malloc((nsearch + 1) *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1327
				    sizeof (char *));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1328
				bcopy(searchdirs, tmp, nsearch *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1329
				    sizeof (char *));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
				free(searchdirs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1331
				searchdirs = tmp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1332
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1333
			searchdirs[nsearch++] = optarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1334
			break;
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1335
		case 'D':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1336
			do_destroyed = B_TRUE;
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1337
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1338
		case 'f':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1339
			do_force = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1340
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1341
		case 'o':
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1342
			if ((propval = strchr(optarg, '=')) != NULL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1343
				*propval = '\0';
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1344
				propval++;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1345
				if (add_prop_list(optarg, propval, &props))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1346
					goto error;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1347
			} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1348
				mntopts = optarg;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1349
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1350
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1351
		case 'R':
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1352
			if (add_prop_list(zpool_prop_to_name(
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1353
			    ZPOOL_PROP_ALTROOT), optarg, &props))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1354
				goto error;
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1355
			if (nvlist_lookup_string(props,
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1356
			    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1357
			    &propval) == 0)
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1358
				break;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1359
			if (add_prop_list(zpool_prop_to_name(
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1360
			    ZPOOL_PROP_CACHEFILE), "none", &props))
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1361
				goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1362
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1363
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1364
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1365
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1366
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1367
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1368
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1369
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1370
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1371
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1372
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1373
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1374
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1375
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1376
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1377
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1378
	if (cachefile && nsearch != 0) {
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1379
		(void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1380
		usage(B_FALSE);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1381
	}
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1382
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1383
	if (searchdirs == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1384
		searchdirs = safe_malloc(sizeof (char *));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1385
		searchdirs[0] = "/dev/dsk";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1386
		nsearch = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1387
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1388
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1389
	/* check argument count */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1390
	if (do_all) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1391
		if (argc != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1392
			(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1393
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1394
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1395
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1396
		if (argc > 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1397
			(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1398
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1399
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1400
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1401
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1402
		 * Check for the SYS_CONFIG privilege.  We do this explicitly
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1403
		 * here because otherwise any attempt to discover pools will
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1404
		 * silently fail.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1405
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1406
		if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1407
			(void) fprintf(stderr, gettext("cannot "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1408
			    "discover pools: permission denied\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1409
			free(searchdirs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1410
			return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1411
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1412
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1413
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1414
	if (cachefile)
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1415
		pools = zpool_find_import_cached(g_zfs, cachefile);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1416
	else
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1417
		pools = zpool_find_import(g_zfs, nsearch, searchdirs);
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1418
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1419
	if (pools == NULL) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1420
		free(searchdirs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1421
		return (1);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1422
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1423
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1424
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1425
	 * We now have a list of all available pools in the given directories.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1426
	 * Depending on the arguments given, we do one of the following:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1427
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1428
	 *	<none>	Iterate through all pools and display information about
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1429
	 *		each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1430
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1431
	 *	-a	Iterate through all pools and try to import each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1432
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1433
	 *	<id>	Find the pool that corresponds to the given GUID/pool
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1434
	 *		name and import that one.
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1435
	 *
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1436
	 *	-D	Above options applies only to destroyed pools.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1437
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1438
	if (argc != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1439
		char *endptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1440
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1441
		errno = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1442
		searchguid = strtoull(argv[0], &endptr, 10);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1443
		if (errno != 0 || *endptr != '\0')
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1444
			searchname = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1445
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1446
			searchname = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1447
		found_config = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1448
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1449
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1450
	err = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1451
	elem = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1452
	first = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1453
	while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1454
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1455
		verify(nvpair_value_nvlist(elem, &config) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1456
1631
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1457
		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1458
		    &pool_state) == 0);
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1459
		if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1460
			continue;
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1461
		if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1462
			continue;
39011550a5d7 PSARC/2006/082 zpool import destroyed pools
darrenm
parents: 1613
diff changeset
  1463
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1464
		if (argc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1465
			if (first)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1466
				first = B_FALSE;
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2142
diff changeset
  1467
			else if (!do_all)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1468
				(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1469
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1470
			if (do_all)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1471
				err |= do_import(config, NULL, mntopts,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1472
				    do_force, props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1473
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1474
				show_import(config);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1475
		} else if (searchname != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1476
			char *name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1477
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1478
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1479
			 * We are searching for a pool based on name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1480
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1481
			verify(nvlist_lookup_string(config,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1482
			    ZPOOL_CONFIG_POOL_NAME, &name) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1483
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1484
			if (strcmp(name, searchname) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1485
				if (found_config != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1486
					(void) fprintf(stderr, gettext(
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1487
					    "cannot import '%s': more than "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1488
					    "one matching pool\n"), searchname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1489
					(void) fprintf(stderr, gettext(
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1490
					    "import by numeric ID instead\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1491
					err = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1492
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1493
				found_config = config;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1494
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1495
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1496
			uint64_t guid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1497
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1498
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1499
			 * Search for a pool by guid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1500
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1501
			verify(nvlist_lookup_uint64(config,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1502
			    ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1503
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1504
			if (guid == searchguid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1505
				found_config = config;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1506
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1507
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1508
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1509
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1510
	 * If we were searching for a specific pool, verify that we found a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1511
	 * pool, and then do the import.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1512
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1513
	if (argc != 0 && err == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1514
		if (found_config == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1515
			(void) fprintf(stderr, gettext("cannot import '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1516
			    "no such pool available\n"), argv[0]);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1517
			err = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1518
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1519
			err |= do_import(found_config, argc == 1 ? NULL :
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1520
			    argv[1], mntopts, do_force, props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1521
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1522
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1523
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1524
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1525
	 * If we were just looking for pools, report an error if none were
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1526
	 * found.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1527
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1528
	if (argc == 0 && first)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1529
		(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1530
		    gettext("no pools available to import\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1531
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  1532
error:
5363
36eeffc5336d PSARC 2007/607 ZFS cachefile property
eschrock
parents: 5331
diff changeset
  1533
	nvlist_free(props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1534
	nvlist_free(pools);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1535
	free(searchdirs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1536
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1537
	return (err ? 1 : 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1538
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1539
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1540
typedef struct iostat_cbdata {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1541
	zpool_list_t *cb_list;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1542
	int cb_verbose;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1543
	int cb_iteration;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1544
	int cb_namewidth;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1545
} iostat_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1546
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1547
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1548
print_iostat_separator(iostat_cbdata_t *cb)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1549
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1550
	int i = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1551
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1552
	for (i = 0; i < cb->cb_namewidth; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1553
		(void) printf("-");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1554
	(void) printf("  -----  -----  -----  -----  -----  -----\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1555
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1556
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1557
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1558
print_iostat_header(iostat_cbdata_t *cb)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1559
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1560
	(void) printf("%*s     capacity     operations    bandwidth\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1561
	    cb->cb_namewidth, "");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1562
	(void) printf("%-*s   used  avail   read  write   read  write\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1563
	    cb->cb_namewidth, "pool");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1564
	print_iostat_separator(cb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1565
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1566
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1567
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1568
 * Display a single statistic.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1569
 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1570
static void
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1571
print_one_stat(uint64_t value)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1572
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1573
	char buf[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1575
	zfs_nicenum(value, buf, sizeof (buf));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1576
	(void) printf("  %5s", buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1577
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1578
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1579
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1580
 * Print out all the statistics for the given vdev.  This can either be the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1581
 * toplevel configuration, or called recursively.  If 'name' is NULL, then this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1582
 * is a verbose output, and we don't want to display the toplevel pool stats.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1583
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1584
void
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  1585
print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  1586
    nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1587
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1588
	nvlist_t **oldchild, **newchild;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1589
	uint_t c, children;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1590
	vdev_stat_t *oldvs, *newvs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1591
	vdev_stat_t zerovs = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1592
	uint64_t tdelta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1593
	double scale;
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  1594
	char *vname;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1595
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1596
	if (oldnv != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1597
		verify(nvlist_lookup_uint64_array(oldnv, ZPOOL_CONFIG_STATS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1598
		    (uint64_t **)&oldvs, &c) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1599
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1600
		oldvs = &zerovs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1601
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1602
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1603
	verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_STATS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1604
	    (uint64_t **)&newvs, &c) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1605
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1606
	if (strlen(name) + depth > cb->cb_namewidth)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1607
		(void) printf("%*s%s", depth, "", name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1608
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1609
		(void) printf("%*s%s%*s", depth, "", name,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1610
		    (int)(cb->cb_namewidth - strlen(name) - depth), "");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1611
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1612
	tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1613
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1614
	if (tdelta == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1615
		scale = 1.0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1616
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1617
		scale = (double)NANOSEC / tdelta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1618
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1619
	/* only toplevel vdevs have capacity stats */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1620
	if (newvs->vs_space == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1621
		(void) printf("      -      -");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1622
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1623
		print_one_stat(newvs->vs_alloc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1624
		print_one_stat(newvs->vs_space - newvs->vs_alloc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1625
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1626
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1627
	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1628
	    oldvs->vs_ops[ZIO_TYPE_READ])));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1629
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1630
	print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1631
	    oldvs->vs_ops[ZIO_TYPE_WRITE])));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1632
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1633
	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1634
	    oldvs->vs_bytes[ZIO_TYPE_READ])));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1635
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1636
	print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1637
	    oldvs->vs_bytes[ZIO_TYPE_WRITE])));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1638
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1639
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1640
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1641
	if (!cb->cb_verbose)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1642
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1643
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1644
	if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1645
	    &newchild, &children) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1646
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1647
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1648
	if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1649
	    &oldchild, &c) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1650
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1651
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  1652
	for (c = 0; c < children; c++) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1653
		vname = zpool_vdev_name(g_zfs, zhp, newchild[c]);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  1654
		print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  1655
		    newchild[c], cb, depth + 2);
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  1656
		free(vname);
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  1657
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1658
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1659
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1660
static int
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1661
refresh_iostat(zpool_handle_t *zhp, void *data)
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1662
{
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1663
	iostat_cbdata_t *cb = data;
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1664
	boolean_t missing;
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1665
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1666
	/*
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1667
	 * If the pool has disappeared, remove it from the list and continue.
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1668
	 */
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1669
	if (zpool_refresh_stats(zhp, &missing) != 0)
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1670
		return (-1);
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1671
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
  1672
	if (missing)
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1673
		pool_list_remove(cb->cb_list, zhp);
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1674
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1675
	return (0);
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1676
}
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1677
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1678
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1679
 * Callback to print out the iostats for the given pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1680
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1681
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1682
print_iostat(zpool_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1683
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1684
	iostat_cbdata_t *cb = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1685
	nvlist_t *oldconfig, *newconfig;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1686
	nvlist_t *oldnvroot, *newnvroot;
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1687
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1688
	newconfig = zpool_get_config(zhp, &oldconfig);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1689
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1690
	if (cb->cb_iteration == 1)
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1691
		oldconfig = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1692
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1693
	verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1694
	    &newnvroot) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1695
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1696
	if (oldconfig == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1697
		oldnvroot = NULL;
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1698
	else
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1699
		verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1700
		    &oldnvroot) == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1701
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1702
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1703
	 * Print out the statistics for the pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1704
	 */
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  1705
	print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1706
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1707
	if (cb->cb_verbose)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1708
		print_iostat_separator(cb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1709
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1710
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1711
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1712
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1713
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1714
get_namewidth(zpool_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1715
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1716
	iostat_cbdata_t *cb = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1717
	nvlist_t *config, *nvroot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1718
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1719
	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1720
		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1721
		    &nvroot) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1722
		if (!cb->cb_verbose)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1723
			cb->cb_namewidth = strlen(zpool_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1724
		else
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  1725
			cb->cb_namewidth = max_width(zhp, nvroot, 0, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1726
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1727
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1728
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1729
	 * The width must fall into the range [10,38].  The upper limit is the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1730
	 * maximum we can have and still fit in 80 columns.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1731
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1732
	if (cb->cb_namewidth < 10)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1733
		cb->cb_namewidth = 10;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1734
	if (cb->cb_namewidth > 38)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1735
		cb->cb_namewidth = 38;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1736
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1737
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1738
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1739
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1740
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1741
 * zpool iostat [-v] [pool] ... [interval [count]]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1742
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1743
 *	-v	Display statistics for individual vdevs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1744
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1745
 * This command can be tricky because we want to be able to deal with pool
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1746
 * creation/destruction as well as vdev configuration changes.  The bulk of this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1747
 * processing is handled by the pool_list_* routines in zpool_iter.c.  We rely
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1748
 * on pool_list_update() to detect the addition of new pools.  Configuration
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1749
 * changes are all handled within libzfs.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1750
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1751
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1752
zpool_do_iostat(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1753
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1754
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1755
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1756
	int npools;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1757
	unsigned long interval = 0, count = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1758
	zpool_list_t *list;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1759
	boolean_t verbose = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1760
	iostat_cbdata_t cb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1761
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1762
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1763
	while ((c = getopt(argc, argv, "v")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1764
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1765
		case 'v':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1766
			verbose = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1767
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1768
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1769
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1770
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1771
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1772
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1773
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1774
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1775
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1776
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1777
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1778
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1779
	 * Determine if the last argument is an integer or a pool name
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1780
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1781
	if (argc > 0 && isdigit(argv[argc - 1][0])) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1782
		char *end;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1783
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1784
		errno = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1785
		interval = strtoul(argv[argc - 1], &end, 10);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1786
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1787
		if (*end == '\0' && errno == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1788
			if (interval == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1789
				(void) fprintf(stderr, gettext("interval "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1790
				    "cannot be zero\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1791
				usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1792
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1793
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1794
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1795
			 * Ignore the last parameter
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1796
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1797
			argc--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1798
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1799
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1800
			 * If this is not a valid number, just plow on.  The
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1801
			 * user will get a more informative error message later
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1802
			 * on.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1803
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1804
			interval = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1805
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1806
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1807
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1808
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1809
	 * If the last argument is also an integer, then we have both a count
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1810
	 * and an integer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1811
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1812
	if (argc > 0 && isdigit(argv[argc - 1][0])) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1813
		char *end;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1814
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1815
		errno = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1816
		count = interval;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1817
		interval = strtoul(argv[argc - 1], &end, 10);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1818
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1819
		if (*end == '\0' && errno == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1820
			if (interval == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1821
				(void) fprintf(stderr, gettext("interval "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1822
				    "cannot be zero\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1823
				usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1824
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1825
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1826
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1827
			 * Ignore the last parameter
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1828
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1829
			argc--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1830
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1831
			interval = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1832
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1833
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1834
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1835
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1836
	 * Construct the list of all interesting pools.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1837
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1838
	ret = 0;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1839
	if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1840
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1841
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1842
	if (pool_list_count(list) == 0 && argc != 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1843
		pool_list_free(list);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1844
		return (1);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1845
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1846
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1847
	if (pool_list_count(list) == 0 && interval == 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1848
		pool_list_free(list);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1849
		(void) fprintf(stderr, gettext("no pools available\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1850
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1851
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1852
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1853
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1854
	 * Enter the main iostat loop.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1855
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1856
	cb.cb_list = list;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1857
	cb.cb_verbose = verbose;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1858
	cb.cb_iteration = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1859
	cb.cb_namewidth = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1860
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1861
	for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1862
		pool_list_update(list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1863
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1864
		if ((npools = pool_list_count(list)) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1865
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1866
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1867
		/*
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1868
		 * Refresh all statistics.  This is done as an explicit step
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1869
		 * before calculating the maximum name width, so that any
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1870
		 * configuration changes are properly accounted for.
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1871
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1872
		(void) pool_list_iter(list, B_FALSE, refresh_iostat, &cb);
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1873
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  1874
		/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1875
		 * Iterate over all pools to determine the maximum width
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1876
		 * for the pool / device name column across all pools.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1877
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1878
		cb.cb_namewidth = 0;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1879
		(void) pool_list_iter(list, B_FALSE, get_namewidth, &cb);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1880
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1881
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1882
		 * If it's the first time, or verbose mode, print the header.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1883
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1884
		if (++cb.cb_iteration == 1 || verbose)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1885
			print_iostat_header(&cb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1886
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1887
		(void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1888
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1889
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1890
		 * If there's more than one pool, and we're not in verbose mode
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1891
		 * (which prints a separator for us), then print a separator.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1892
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1893
		if (npools > 1 && !verbose)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1894
			print_iostat_separator(&cb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1895
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1896
		if (verbose)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1897
			(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1898
3377
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3126
diff changeset
  1899
		/*
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3126
diff changeset
  1900
		 * Flush the output so that redirection to a file isn't buffered
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3126
diff changeset
  1901
		 * indefinitely.
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3126
diff changeset
  1902
		 */
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3126
diff changeset
  1903
		(void) fflush(stdout);
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3126
diff changeset
  1904
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1905
		if (interval == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1906
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1907
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1908
		if (count != 0 && --count == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1909
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1910
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1911
		(void) sleep(interval);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1912
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1913
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1914
	pool_list_free(list);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1915
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1916
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1917
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1918
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1919
typedef struct list_cbdata {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1920
	boolean_t	cb_scripted;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  1921
	boolean_t	cb_first;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1922
	zprop_list_t	*cb_proplist;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1923
} list_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1924
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1925
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1926
 * Given a list of columns to display, output appropriate headers for each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1927
 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1928
static void
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1929
print_header(zprop_list_t *pl)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1930
{
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1931
	const char *header;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1932
	boolean_t first = B_TRUE;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1933
	boolean_t right_justify;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1934
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1935
	for (; pl != NULL; pl = pl->pl_next) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1936
		if (pl->pl_prop == ZPROP_INVAL)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1937
			continue;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1938
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1939
		if (!first)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1940
			(void) printf("  ");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1941
		else
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1942
			first = B_FALSE;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1943
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1944
		header = zpool_prop_column_name(pl->pl_prop);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1945
		right_justify = zpool_prop_align_right(pl->pl_prop);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1946
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1947
		if (pl->pl_next == NULL && !right_justify)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1948
			(void) printf("%s", header);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1949
		else if (right_justify)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1950
			(void) printf("%*s", pl->pl_width, header);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1951
		else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1952
			(void) printf("%-*s", pl->pl_width, header);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1953
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1954
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1955
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1956
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1957
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1958
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1959
 * Given a pool and a list of properties, print out all the properties according
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1960
 * to the described layout.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1961
 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1962
static void
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1963
print_pool(zpool_handle_t *zhp, zprop_list_t *pl, int scripted)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1964
{
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1965
	boolean_t first = B_TRUE;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1966
	char property[ZPOOL_MAXPROPLEN];
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1967
	char *propstr;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1968
	boolean_t right_justify;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1969
	int width;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1970
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1971
	for (; pl != NULL; pl = pl->pl_next) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1972
		if (!first) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1973
			if (scripted)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1974
				(void) printf("\t");
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1975
			else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1976
				(void) printf("  ");
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1977
		} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1978
			first = B_FALSE;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1979
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1980
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1981
		right_justify = B_FALSE;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1982
		if (pl->pl_prop != ZPROP_INVAL) {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1983
			if (zpool_get_prop(zhp, pl->pl_prop, property,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1984
			    sizeof (property), NULL) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1985
				propstr = "-";
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1986
			else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1987
				propstr = property;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1988
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1989
			right_justify = zpool_prop_align_right(pl->pl_prop);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1990
		} else {
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1991
			propstr = "-";
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1992
		}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1993
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1994
		width = pl->pl_width;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1995
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1996
		/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1997
		 * If this is being called in scripted mode, or if this is the
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1998
		 * last column and it is left-justified, don't include a width
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1999
		 * format specifier.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2000
		 */
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2001
		if (scripted || (pl->pl_next == NULL && !right_justify))
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2002
			(void) printf("%s", propstr);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2003
		else if (right_justify)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2004
			(void) printf("%*s", width, propstr);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2005
		else
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2006
			(void) printf("%-*s", width, propstr);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2007
	}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2008
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2009
	(void) printf("\n");
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2010
}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2011
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2012
/*
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2013
 * Generic callback function to list a pool.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2014
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2015
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2016
list_callback(zpool_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2017
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2018
	list_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2019
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2020
	if (cbp->cb_first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2021
		if (!cbp->cb_scripted)
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2022
			print_header(cbp->cb_proplist);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2023
		cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2024
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2025
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2026
	print_pool(zhp, cbp->cb_proplist, cbp->cb_scripted);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2027
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2028
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2029
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2030
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2031
/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2032
 * zpool list [-H] [-o prop[,prop]*] [pool] ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2033
 *
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2034
 *	-H	Scripted mode.  Don't display headers, and separate properties
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2035
 *		by a single tab.
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2036
 *	-o	List of properties to display.  Defaults to
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2037
 *		"name,size,used,available,capacity,health,altroot"
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2038
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2039
 * List all pools in the system, whether or not they're healthy.  Output space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2040
 * statistics for each one, as well as health status summary.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2041
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2042
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2043
zpool_do_list(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2044
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2045
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2046
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2047
	list_cbdata_t cb = { 0 };
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2048
	static char default_props[] =
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2049
	    "name,size,used,available,capacity,health,altroot";
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2050
	char *props = default_props;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2051
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2052
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2053
	while ((c = getopt(argc, argv, ":Ho:")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2054
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2055
		case 'H':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2056
			cb.cb_scripted = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2057
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2058
		case 'o':
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2059
			props = optarg;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2060
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2061
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2062
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2063
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2064
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2065
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2066
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2067
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2068
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2069
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2070
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2071
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2072
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2073
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2074
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2075
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2076
	if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2077
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2078
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2079
	cb.cb_first = B_TRUE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2080
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2081
	ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2082
	    list_callback, &cb);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2083
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2084
	zprop_free_list(cb.cb_proplist);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2085
4221
50b4bab9766b 6540584 zfs list command should not produce "no datasets available" message in script mode
mmusante
parents: 3975
diff changeset
  2086
	if (argc == 0 && cb.cb_first && !cb.cb_scripted) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2087
		(void) printf(gettext("no pools available\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2088
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2089
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2090
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2091
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2092
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2093
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2094
static nvlist_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2095
zpool_get_vdev_by_name(nvlist_t *nv, char *name)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2096
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2097
	nvlist_t **child;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2098
	uint_t c, children;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2099
	nvlist_t *match;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2100
	char *path;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2101
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2102
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2103
	    &child, &children) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2104
		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2105
		if (strncmp(name, "/dev/dsk/", 9) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2106
			name += 9;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2107
		if (strncmp(path, "/dev/dsk/", 9) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2108
			path += 9;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2109
		if (strcmp(name, path) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2110
			return (nv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2111
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2112
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2113
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2114
	for (c = 0; c < children; c++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2115
		if ((match = zpool_get_vdev_by_name(child[c], name)) != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2116
			return (match);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2117
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2118
	return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2119
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2120
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2121
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2122
zpool_do_attach_or_replace(int argc, char **argv, int replacing)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2123
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2124
	boolean_t force = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2125
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2126
	nvlist_t *nvroot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2127
	char *poolname, *old_disk, *new_disk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2128
	zpool_handle_t *zhp;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2129
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2130
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2131
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2132
	while ((c = getopt(argc, argv, "f")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2133
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2134
		case 'f':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2135
			force = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2136
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2137
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2138
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2139
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2140
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2141
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2142
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2143
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2144
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2145
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2146
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2147
	/* get pool name and check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2148
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2149
		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2150
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2151
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2152
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2153
	poolname = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2154
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2155
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2156
		(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2157
		    gettext("missing <device> specification\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2158
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2159
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2160
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2161
	old_disk = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2162
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2163
	if (argc < 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2164
		if (!replacing) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2165
			(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2166
			    gettext("missing <new_device> specification\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2167
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2168
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2169
		new_disk = old_disk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2170
		argc -= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2171
		argv += 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2172
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2173
		new_disk = argv[2];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2174
		argc -= 2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2175
		argv += 2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2176
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2177
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2178
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2179
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2180
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2181
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2182
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2183
	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2184
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2185
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 4221
diff changeset
  2186
	if (zpool_get_config(zhp, NULL) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2187
		(void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2188
		    poolname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2189
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2190
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2191
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2192
4276
c42d49ffb16f 6385547 zpool create of a mirror using a device and a slice of a device (of same size) reports an error
taylor
parents: 4221
diff changeset
  2193
	nvroot = make_root_vdev(zhp, force, B_FALSE, replacing, argc, argv);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2194
	if (nvroot == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2195
		zpool_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2196
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2197
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2198
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2199
	ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2200
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2201
	nvlist_free(nvroot);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2202
	zpool_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2203
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2204
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2205
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2206
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2207
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2208
 * zpool replace [-f] <pool> <device> <new_device>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2209
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2210
 *	-f	Force attach, even if <new_device> appears to be in use.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2211
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2212
 * Replace <device> with <new_device>.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2213
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2214
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2215
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2216
zpool_do_replace(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2217
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2218
	return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2219
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2220
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2221
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2222
 * zpool attach [-f] <pool> <device> <new_device>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2223
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2224
 *	-f	Force attach, even if <new_device> appears to be in use.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2225
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2226
 * Attach <new_device> to the mirror containing <device>.  If <device> is not
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2227
 * part of a mirror, then <device> will be transformed into a mirror of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2228
 * <device> and <new_device>.  In either case, <new_device> will begin life
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2229
 * with a DTL of [0, now], and will immediately begin to resilver itself.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2230
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2231
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2232
zpool_do_attach(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2233
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2234
	return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2235
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2236
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2237
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2238
 * zpool detach [-f] <pool> <device>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2239
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2240
 *	-f	Force detach of <device>, even if DTLs argue against it
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2241
 *		(not supported yet)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2242
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2243
 * Detach a device from a mirror.  The operation will be refused if <device>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2244
 * is the last device in the mirror, or if the DTLs indicate that this device
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2245
 * has the only valid copy of some data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2246
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2247
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2248
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2249
zpool_do_detach(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2250
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2251
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2252
	char *poolname, *path;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2253
	zpool_handle_t *zhp;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2254
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2255
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2256
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2257
	while ((c = getopt(argc, argv, "f")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2258
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2259
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2260
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2261
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2262
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2263
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2264
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2265
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2266
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2267
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2268
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2269
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2270
	/* get pool name and check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2271
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2272
		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2273
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2274
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2275
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2276
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2277
		(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2278
		    gettext("missing <device> specification\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2279
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2280
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2281
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2282
	poolname = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2283
	path = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2284
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2285
	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2286
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2287
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2288
	ret = zpool_vdev_detach(zhp, path);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2289
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2290
	zpool_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2291
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2292
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2293
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2294
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2295
/*
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  2296
 * zpool online <pool> <device> ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2297
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2298
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2299
zpool_do_online(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2300
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2301
	int c, i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2302
	char *poolname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2303
	zpool_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2304
	int ret = 0;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2305
	vdev_state_t newstate;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2306
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2307
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2308
	while ((c = getopt(argc, argv, "t")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2309
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2310
		case 't':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2311
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2312
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2313
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2314
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2315
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2316
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2317
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2318
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2319
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2320
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2321
	/* get pool name and check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2322
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2323
		(void) fprintf(stderr, gettext("missing pool name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2324
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2325
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2326
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2327
		(void) fprintf(stderr, gettext("missing device name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2328
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2329
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2330
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2331
	poolname = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2332
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2333
	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2334
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2335
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2336
	for (i = 1; i < argc; i++) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2337
		if (zpool_vdev_online(zhp, argv[i], 0, &newstate) == 0) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2338
			if (newstate != VDEV_STATE_HEALTHY) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2339
				(void) printf(gettext("warning: device '%s' "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2340
				    "onlined, but remains in faulted state\n"),
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2341
				    argv[i]);
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2342
				if (newstate == VDEV_STATE_FAULTED)
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2343
					(void) printf(gettext("use 'zpool "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2344
					    "clear' to restore a faulted "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2345
					    "device\n"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2346
				else
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2347
					(void) printf(gettext("use 'zpool "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2348
					    "replace' to replace devices "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2349
					    "that are no longer present\n"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2350
			}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2351
		} else {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2352
			ret = 1;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2353
		}
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2354
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2355
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2356
	zpool_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2357
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2358
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2359
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2360
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2361
/*
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  2362
 * zpool offline [-ft] <pool> <device> ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2363
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2364
 *	-f	Force the device into the offline state, even if doing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2365
 *		so would appear to compromise pool availability.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2366
 *		(not supported yet)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2367
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2368
 *	-t	Only take the device off-line temporarily.  The offline
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2369
 *		state will not be persistent across reboots.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2370
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2371
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2372
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2373
zpool_do_offline(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2374
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2375
	int c, i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2376
	char *poolname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2377
	zpool_handle_t *zhp;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2378
	int ret = 0;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2379
	boolean_t istmp = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2381
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2382
	while ((c = getopt(argc, argv, "ft")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2383
		switch (c) {
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  2384
		case 't':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2385
			istmp = B_TRUE;
1485
e971e58d18f6 6322005 support for persistent offline
lling
parents: 1387
diff changeset
  2386
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2387
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2388
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2389
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2390
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2391
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2392
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2393
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2394
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2395
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2396
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2397
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2398
	/* get pool name and check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2399
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2400
		(void) fprintf(stderr, gettext("missing pool name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2401
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2402
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2403
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2404
		(void) fprintf(stderr, gettext("missing device name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2405
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2406
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2407
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2408
	poolname = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2409
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2410
	if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2411
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2412
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2413
	for (i = 1; i < argc; i++) {
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2414
		if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2415
			ret = 1;
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2416
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2417
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2418
	zpool_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2419
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2420
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2421
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2422
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2423
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2424
 * zpool clear <pool> [device]
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2425
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2426
 * Clear all errors associated with a pool or a particular device.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2427
 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2428
int
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2429
zpool_do_clear(int argc, char **argv)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2430
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2431
	int ret = 0;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2432
	zpool_handle_t *zhp;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2433
	char *pool, *device;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2434
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2435
	if (argc < 2) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2436
		(void) fprintf(stderr, gettext("missing pool name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2437
		usage(B_FALSE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2438
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2439
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2440
	if (argc > 3) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2441
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2442
		usage(B_FALSE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2443
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2444
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2445
	pool = argv[1];
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2446
	device = argc == 3 ? argv[2] : NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2447
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2448
	if ((zhp = zpool_open(g_zfs, pool)) == NULL)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2449
		return (1);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2450
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2451
	if (zpool_clear(zhp, device) != 0)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2452
		ret = 1;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2453
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2454
	zpool_close(zhp);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2455
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2456
	return (ret);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2457
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2458
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2459
typedef struct scrub_cbdata {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2460
	int	cb_type;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2461
	int	cb_argc;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2462
	char	**cb_argv;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2463
} scrub_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2464
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2465
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2466
scrub_callback(zpool_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2467
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2468
	scrub_cbdata_t *cb = data;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2469
	int err;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2470
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2471
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2472
	 * Ignore faulted pools.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2473
	 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2474
	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2475
		(void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2476
		    "currently unavailable\n"), zpool_get_name(zhp));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2477
		return (1);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2478
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2479
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2480
	err = zpool_scrub(zhp, cb->cb_type);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2481
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2482
	return (err != 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2483
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2484
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2485
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2486
 * zpool scrub [-s] <pool> ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2487
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2488
 *	-s	Stop.  Stops any in-progress scrub.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2489
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2490
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2491
zpool_do_scrub(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2492
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2493
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2494
	scrub_cbdata_t cb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2495
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2496
	cb.cb_type = POOL_SCRUB_EVERYTHING;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2497
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2498
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2499
	while ((c = getopt(argc, argv, "s")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2500
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2501
		case 's':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2502
			cb.cb_type = POOL_SCRUB_NONE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2503
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2504
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2505
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2506
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2507
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2508
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2509
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2510
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2511
	cb.cb_argc = argc;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  2512
	cb.cb_argv = argv;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2513
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2514
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2515
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2516
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2517
		(void) fprintf(stderr, gettext("missing pool name argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2518
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2519
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2520
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2521
	return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2522
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2523
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2524
typedef struct status_cbdata {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2525
	int		cb_count;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2526
	boolean_t	cb_allpools;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2527
	boolean_t	cb_verbose;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2528
	boolean_t	cb_explain;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2529
	boolean_t	cb_first;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2530
} status_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2531
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2532
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2533
 * Print out detailed scrub status.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2534
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2535
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2536
print_scrub_status(nvlist_t *nvroot)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2537
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2538
	vdev_stat_t *vs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2539
	uint_t vsc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2540
	time_t start, end, now;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2541
	double fraction_done;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2542
	uint64_t examined, total, minutes_left;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2543
	char *scrub_type;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2544
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2545
	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2546
	    (uint64_t **)&vs, &vsc) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2547
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2548
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2549
	 * If there's never been a scrub, there's not much to say.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2550
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2551
	if (vs->vs_scrub_end == 0 && vs->vs_scrub_type == POOL_SCRUB_NONE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2552
		(void) printf(gettext("none requested\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2553
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2554
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2555
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2556
	scrub_type = (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2557
	    "resilver" : "scrub";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2558
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2559
	start = vs->vs_scrub_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2560
	end = vs->vs_scrub_end;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2561
	now = time(NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2562
	examined = vs->vs_scrub_examined;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2563
	total = vs->vs_alloc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2564
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2565
	if (end != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2566
		(void) printf(gettext("%s %s with %llu errors on %s"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2567
		    scrub_type, vs->vs_scrub_complete ? "completed" : "stopped",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2568
		    (u_longlong_t)vs->vs_scrub_errors, ctime(&end));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2569
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2570
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2571
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2572
	if (examined == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2573
		examined = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2574
	if (examined > total)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2575
		total = examined;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2576
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2577
	fraction_done = (double)examined / total;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2578
	minutes_left = (uint64_t)((now - start) *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2579
	    (1 - fraction_done) / fraction_done / 60);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2580
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2581
	(void) printf(gettext("%s in progress, %.2f%% done, %lluh%um to go\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2582
	    scrub_type, 100 * fraction_done,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2583
	    (u_longlong_t)(minutes_left / 60), (uint_t)(minutes_left % 60));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2584
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2585
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2586
typedef struct spare_cbdata {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2587
	uint64_t	cb_guid;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2588
	zpool_handle_t	*cb_zhp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2589
} spare_cbdata_t;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2590
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2591
static boolean_t
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2592
find_vdev(nvlist_t *nv, uint64_t search)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2593
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2594
	uint64_t guid;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2595
	nvlist_t **child;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2596
	uint_t c, children;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2597
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2598
	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2599
	    search == guid)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2600
		return (B_TRUE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2601
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2602
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2603
	    &child, &children) == 0) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2604
		for (c = 0; c < children; c++)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2605
			if (find_vdev(child[c], search))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2606
				return (B_TRUE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2607
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2608
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2609
	return (B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2610
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2611
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2612
static int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2613
find_spare(zpool_handle_t *zhp, void *data)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2614
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2615
	spare_cbdata_t *cbp = data;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2616
	nvlist_t *config, *nvroot;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2617
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2618
	config = zpool_get_config(zhp, NULL);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2619
	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2620
	    &nvroot) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2621
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2622
	if (find_vdev(nvroot, cbp->cb_guid)) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2623
		cbp->cb_zhp = zhp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2624
		return (1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2625
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2626
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2627
	zpool_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2628
	return (0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2629
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2630
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2631
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2632
 * Print out configuration state as requested by status_callback.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2633
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2634
void
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  2635
print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2636
    int namewidth, int depth, boolean_t isspare, boolean_t print_logs)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2637
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2638
	nvlist_t **child;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2639
	uint_t c, children;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2640
	vdev_stat_t *vs;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2641
	char rbuf[6], wbuf[6], cbuf[6], repaired[7];
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  2642
	char *vname;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2643
	uint64_t notpresent;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2644
	spare_cbdata_t cb;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2645
	char *state;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2646
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2647
	verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2648
	    (uint64_t **)&vs, &c) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2649
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2650
	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2651
	    &child, &children) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2652
		children = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2653
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2654
	state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2655
	if (isspare) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2656
		/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2657
		 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2658
		 * online drives.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2659
		 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2660
		if (vs->vs_aux == VDEV_AUX_SPARED)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2661
			state = "INUSE";
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2662
		else if (vs->vs_state == VDEV_STATE_HEALTHY)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2663
			state = "AVAIL";
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2664
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2665
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2666
	(void) printf("\t%*s%-*s  %-8s", depth, "", namewidth - depth,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2667
	    name, state);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2668
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2669
	if (!isspare) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2670
		zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2671
		zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2672
		zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2673
		(void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2674
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2675
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2676
	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2677
	    &notpresent) == 0) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2678
		char *path;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2679
		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1613
aca102a242d3 6396628 zpool offline does not recognize c0d0 patten recently
eschrock
parents: 1544
diff changeset
  2680
		(void) printf("  was %s", path);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2681
	} else if (vs->vs_aux != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2682
		(void) printf("  ");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2683
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2684
		switch (vs->vs_aux) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2685
		case VDEV_AUX_OPEN_FAILED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2686
			(void) printf(gettext("cannot open"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2687
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2688
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2689
		case VDEV_AUX_BAD_GUID_SUM:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2690
			(void) printf(gettext("missing device"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2691
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2692
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2693
		case VDEV_AUX_NO_REPLICAS:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2694
			(void) printf(gettext("insufficient replicas"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2695
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2696
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2697
		case VDEV_AUX_VERSION_NEWER:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2698
			(void) printf(gettext("newer version"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2699
			break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2700
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2701
		case VDEV_AUX_SPARED:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2702
			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2703
			    &cb.cb_guid) == 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2704
			if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2705
				if (strcmp(zpool_get_name(cb.cb_zhp),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2706
				    zpool_get_name(zhp)) == 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2707
					(void) printf(gettext("currently in "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2708
					    "use"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2709
				else
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2710
					(void) printf(gettext("in use by "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2711
					    "pool '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2712
					    zpool_get_name(cb.cb_zhp));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2713
				zpool_close(cb.cb_zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2714
			} else {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2715
				(void) printf(gettext("currently in use"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2716
			}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2717
			break;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2718
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2719
		case VDEV_AUX_ERR_EXCEEDED:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2720
			(void) printf(gettext("too many errors"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2721
			break;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2722
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2723
		default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2724
			(void) printf(gettext("corrupted data"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2725
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2726
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2727
	} else if (vs->vs_scrub_repaired != 0 && children == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2728
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2729
		 * Report bytes resilvered/repaired on leaf devices.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2730
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2731
		zfs_nicenum(vs->vs_scrub_repaired, repaired, sizeof (repaired));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2732
		(void) printf(gettext("  %s %s"), repaired,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2733
		    (vs->vs_scrub_type == POOL_SCRUB_RESILVER) ?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2734
		    "resilvered" : "repaired");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2735
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2736
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2737
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2738
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  2739
	for (c = 0; c < children; c++) {
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2740
		uint64_t is_log = B_FALSE;
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2741
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2742
		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2743
		    &is_log);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2744
		if ((is_log && !print_logs) || (!is_log && print_logs))
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2745
			continue;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2746
		vname = zpool_vdev_name(g_zfs, zhp, child[c]);
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  2747
		print_status_config(zhp, vname, child[c],
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2748
		    namewidth, depth + 2, isspare, B_FALSE);
1171
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  2749
		free(vname);
b501e9f31cab 6344272 re-think how whole disks are stored
eschrock
parents: 952
diff changeset
  2750
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2751
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2752
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2753
static void
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2754
print_error_log(zpool_handle_t *zhp)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2755
{
4820
acbb57436af2 6588264 zpool_get_errlog() can attempt to calloc size 0
ek110237
parents: 4787
diff changeset
  2756
	nvlist_t *nverrlist = NULL;
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2757
	nvpair_t *elem;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2758
	char *pathname;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2759
	size_t len = MAXPATHLEN * 2;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2760
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2761
	if (zpool_get_errlog(zhp, &nverrlist) != 0) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2762
		(void) printf("errors: List of errors unavailable "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2763
		    "(insufficient privileges)\n");
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2764
		return;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2765
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2766
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2767
	(void) printf("errors: Permanent errors have been "
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2768
	    "detected in the following files:\n\n");
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2769
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2770
	pathname = safe_malloc(len);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2771
	elem = NULL;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2772
	while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2773
		nvlist_t *nv;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2774
		uint64_t dsobj, obj;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2775
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2776
		verify(nvpair_value_nvlist(elem, &nv) == 0);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2777
		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2778
		    &dsobj) == 0);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2779
		verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2780
		    &obj) == 0);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2781
		zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2782
		(void) printf("%7s %s\n", "", pathname);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2783
	}
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2784
	free(pathname);
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  2785
	nvlist_free(nverrlist);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2786
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2787
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2788
static void
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2789
print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2790
    int namewidth)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2791
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2792
	uint_t i;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2793
	char *name;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2794
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2795
	if (nspares == 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2796
		return;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2797
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2798
	(void) printf(gettext("\tspares\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2799
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2800
	for (i = 0; i < nspares; i++) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2801
		name = zpool_vdev_name(g_zfs, zhp, spares[i]);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2802
		print_status_config(zhp, name, spares[i],
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  2803
		    namewidth, 2, B_TRUE, B_FALSE);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2804
		free(name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2805
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2806
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2807
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2808
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2809
 * Display a summary of pool status.  Displays a summary such as:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2810
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2811
 *        pool: tank
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2812
 *	status: DEGRADED
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2813
 *	reason: One or more devices ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2814
 *         see: http://www.sun.com/msg/ZFS-xxxx-01
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2815
 *	config:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2816
 *		mirror		DEGRADED
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2817
 *                c1t0d0	OK
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2818
 *                c2t0d0	UNAVAIL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2819
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2820
 * When given the '-v' option, we print out the complete config.  If the '-e'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2821
 * option is specified, then we print out error rate information as well.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2822
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2823
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2824
status_callback(zpool_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2825
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2826
	status_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2827
	nvlist_t *config, *nvroot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2828
	char *msgid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2829
	int reason;
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  2830
	const char *health;
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  2831
	uint_t c;
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  2832
	vdev_stat_t *vs;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2833
952
12ec54aa046e 6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents: 884
diff changeset
  2834
	config = zpool_get_config(zhp, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2835
	reason = zpool_get_status(zhp, &msgid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2836
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2837
	cbp->cb_count++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2838
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2839
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2840
	 * If we were given 'zpool status -x', only report those pools with
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2841
	 * problems.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2842
	 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2843
	if (reason == ZPOOL_STATUS_OK && cbp->cb_explain) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2844
		if (!cbp->cb_allpools) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2845
			(void) printf(gettext("pool '%s' is healthy\n"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2846
			    zpool_get_name(zhp));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2847
			if (cbp->cb_first)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2848
				cbp->cb_first = B_FALSE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2849
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2850
		return (0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  2851
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2852
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2853
	if (cbp->cb_first)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2854
		cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2855
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2856
		(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2857
3741
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  2858
	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  2859
	    &nvroot) == 0);
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  2860
	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
806e933bf124 6441384 zpool import action message is not correctly localized
mmusante
parents: 3444
diff changeset
  2861
	    (uint64_t **)&vs, &c) == 0);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2862
	health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2863
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2864
	(void) printf(gettext("  pool: %s\n"), zpool_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2865
	(void) printf(gettext(" state: %s\n"), health);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2866
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2867
	switch (reason) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2868
	case ZPOOL_STATUS_MISSING_DEV_R:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2869
		(void) printf(gettext("status: One or more devices could not "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2870
		    "be opened.  Sufficient replicas exist for\n\tthe pool to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2871
		    "continue functioning in a degraded state.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2872
		(void) printf(gettext("action: Attach the missing device and "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2873
		    "online it using 'zpool online'.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2874
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2875
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2876
	case ZPOOL_STATUS_MISSING_DEV_NR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2877
		(void) printf(gettext("status: One or more devices could not "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2878
		    "be opened.  There are insufficient\n\treplicas for the "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2879
		    "pool to continue functioning.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2880
		(void) printf(gettext("action: Attach the missing device and "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2881
		    "online it using 'zpool online'.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2882
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2883
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2884
	case ZPOOL_STATUS_CORRUPT_LABEL_R:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2885
		(void) printf(gettext("status: One or more devices could not "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2886
		    "be used because the label is missing or\n\tinvalid.  "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2887
		    "Sufficient replicas exist for the pool to continue\n\t"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2888
		    "functioning in a degraded state.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2889
		(void) printf(gettext("action: Replace the device using "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2890
		    "'zpool replace'.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2891
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2892
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2893
	case ZPOOL_STATUS_CORRUPT_LABEL_NR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2894
		(void) printf(gettext("status: One or more devices could not "
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2895
		    "be used because the label is missing \n\tor invalid.  "
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2896
		    "There are insufficient replicas for the pool to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2897
		    "continue\n\tfunctioning.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2898
		(void) printf(gettext("action: Destroy and re-create the pool "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2899
		    "from a backup source.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2900
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2901
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2902
	case ZPOOL_STATUS_FAILING_DEV:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2903
		(void) printf(gettext("status: One or more devices has "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2904
		    "experienced an unrecoverable error.  An\n\tattempt was "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2905
		    "made to correct the error.  Applications are "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2906
		    "unaffected.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2907
		(void) printf(gettext("action: Determine if the device needs "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2908
		    "to be replaced, and clear the errors\n\tusing "
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2909
		    "'zpool clear' or replace the device with 'zpool "
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2910
		    "replace'.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2911
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2912
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2913
	case ZPOOL_STATUS_OFFLINE_DEV:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2914
		(void) printf(gettext("status: One or more devices has "
3413
f4cbe0204ab7 6471255 Some ZFS error messages contain spelling errors
mmusante
parents: 3377
diff changeset
  2915
		    "been taken offline by the administrator.\n\tSufficient "
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2916
		    "replicas exist for the pool to continue functioning in "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2917
		    "a\n\tdegraded state.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2918
		(void) printf(gettext("action: Online the device using "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2919
		    "'zpool online' or replace the device with\n\t'zpool "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2920
		    "replace'.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2921
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2922
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2923
	case ZPOOL_STATUS_RESILVERING:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2924
		(void) printf(gettext("status: One or more devices is "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2925
		    "currently being resilvered.  The pool will\n\tcontinue "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2926
		    "to function, possibly in a degraded state.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2927
		(void) printf(gettext("action: Wait for the resilver to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2928
		    "complete.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2929
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2930
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2931
	case ZPOOL_STATUS_CORRUPT_DATA:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2932
		(void) printf(gettext("status: One or more devices has "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2933
		    "experienced an error resulting in data\n\tcorruption.  "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2934
		    "Applications may be affected.\n"));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2935
		(void) printf(gettext("action: Restore the file in question "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2936
		    "if possible.  Otherwise restore the\n\tentire pool from "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2937
		    "backup.\n"));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2938
		break;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2939
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2940
	case ZPOOL_STATUS_CORRUPT_POOL:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2941
		(void) printf(gettext("status: The pool metadata is corrupted "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2942
		    "and the pool cannot be opened.\n"));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2943
		(void) printf(gettext("action: Destroy and re-create the pool "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2944
		    "from a backup source.\n"));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2945
		break;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2946
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2947
	case ZPOOL_STATUS_VERSION_OLDER:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2948
		(void) printf(gettext("status: The pool is formatted using an "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2949
		    "older on-disk format.  The pool can\n\tstill be used, but "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2950
		    "some features are unavailable.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2951
		(void) printf(gettext("action: Upgrade the pool using 'zpool "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2952
		    "upgrade'.  Once this is done, the\n\tpool will no longer "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2953
		    "be accessible on older software versions.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2954
		break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2955
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2956
	case ZPOOL_STATUS_VERSION_NEWER:
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2957
		(void) printf(gettext("status: The pool has been upgraded to a "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2958
		    "newer, incompatible on-disk version.\n\tThe pool cannot "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2959
		    "be accessed on this system.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2960
		(void) printf(gettext("action: Access the pool from a system "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2961
		    "running more recent software, or\n\trestore the pool from "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2962
		    "backup.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2963
		break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  2964
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2965
	case ZPOOL_STATUS_FAULTED_DEV_R:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2966
		(void) printf(gettext("status: One or more devices are "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2967
		    "faulted in response to persistent errors.\n\tSufficient "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2968
		    "replicas exist for the pool to continue functioning "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2969
		    "in a\n\tdegraded state.\n"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2970
		(void) printf(gettext("action: Replace the faulted device, "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2971
		    "or use 'zpool clear' to mark the device\n\trepaired.\n"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2972
		break;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2973
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2974
	case ZPOOL_STATUS_FAULTED_DEV_NR:
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2975
		(void) printf(gettext("status: One or more devices are "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2976
		    "faulted in response to persistent errors.  There are "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2977
		    "insufficient replicas for the pool to\n\tcontinue "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2978
		    "functioning.\n"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2979
		(void) printf(gettext("action: Destroy and re-create the pool "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2980
		    "from a backup source.  Manually marking the device\n"
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2981
		    "\trepaired using 'zpool clear' may allow some data "
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2982
		    "to be recovered.\n"));
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2983
		break;
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4276
diff changeset
  2984
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2985
	default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2986
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2987
		 * The remaining errors can't actually be generated, yet.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2988
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2989
		assert(reason == ZPOOL_STATUS_OK);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2990
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2991
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2992
	if (msgid != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2993
		(void) printf(gettext("   see: http://www.sun.com/msg/%s\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2994
		    msgid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2995
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2996
	if (config != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2997
		int namewidth;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  2998
		uint64_t nerr;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  2999
		nvlist_t **spares;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3000
		uint_t nspares;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3001
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3002
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3003
		(void) printf(gettext(" scrub: "));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3004
		print_scrub_status(nvroot);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3005
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  3006
		namewidth = max_width(zhp, nvroot, 0, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3007
		if (namewidth < 10)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3008
			namewidth = 10;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3009
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3010
		(void) printf(gettext("config:\n\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3011
		(void) printf(gettext("\t%-*s  %-8s %5s %5s %5s\n"), namewidth,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3012
		    "NAME", "STATE", "READ", "WRITE", "CKSUM");
1354
81359ee1ee63 6362672 import gets confused about overlapping slices
eschrock
parents: 1171
diff changeset
  3013
		print_status_config(zhp, zpool_get_name(zhp), nvroot,
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3014
		    namewidth, 0, B_FALSE, B_FALSE);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3015
		if (num_logs(nvroot) > 0)
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3016
			print_status_config(zhp, "logs", nvroot, namewidth, 0,
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3017
			    B_FALSE, B_TRUE);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3018
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3019
		if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3020
		    &spares, &nspares) == 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3021
			print_spares(zhp, spares, nspares, namewidth);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3022
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3023
		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3024
		    &nerr) == 0) {
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3025
			nvlist_t *nverrlist = NULL;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3026
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3027
			/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3028
			 * If the approximate error count is small, get a
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3029
			 * precise count by fetching the entire log and
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3030
			 * uniquifying the results.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3031
			 */
4820
acbb57436af2 6588264 zpool_get_errlog() can attempt to calloc size 0
ek110237
parents: 4787
diff changeset
  3032
			if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3033
			    zpool_get_errlog(zhp, &nverrlist) == 0) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3034
				nvpair_t *elem;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3035
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3036
				elem = NULL;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3037
				nerr = 0;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3038
				while ((elem = nvlist_next_nvpair(nverrlist,
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3039
				    elem)) != NULL) {
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3040
					nerr++;
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3041
				}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3042
			}
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3413
diff changeset
  3043
			nvlist_free(nverrlist);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3044
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3045
			(void) printf("\n");
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3046
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3047
			if (nerr == 0)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3048
				(void) printf(gettext("errors: No known data "
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3049
				    "errors\n"));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3050
			else if (!cbp->cb_verbose)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  3051
				(void) printf(gettext("errors: %llu data "
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2731
diff changeset
  3052
				    "errors, use '-v' for a list\n"),
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2731
diff changeset
  3053
				    (u_longlong_t)nerr);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3054
			else
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3055
				print_error_log(zhp);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3056
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3057
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3058
		(void) printf(gettext("config: The configuration cannot be "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3059
		    "determined.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3060
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3061
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3062
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3063
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3064
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3065
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3066
 * zpool status [-vx] [pool] ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3067
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3068
 *	-v	Display complete error logs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3069
 *	-x	Display only pools with potential problems
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3070
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3071
 * Describes the health status of all pools or some subset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3072
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3073
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3074
zpool_do_status(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3075
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3076
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3077
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3078
	status_cbdata_t cb = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3079
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3080
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3081
	while ((c = getopt(argc, argv, "vx")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3082
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3083
		case 'v':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3084
			cb.cb_verbose = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3085
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3086
		case 'x':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3087
			cb.cb_explain = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3088
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3089
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3090
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3091
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3092
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3093
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3094
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3095
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3096
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3097
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3098
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3099
	cb.cb_first = B_TRUE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3100
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  3101
	if (argc == 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  3102
		cb.cb_allpools = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  3103
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3104
	ret = for_each_pool(argc, argv, B_TRUE, NULL, status_callback, &cb);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3105
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3106
	if (argc == 0 && cb.cb_count == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3107
		(void) printf(gettext("no pools available\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  3108
	else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2500
diff changeset
  3109
		(void) printf(gettext("all pools are healthy\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3110
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3111
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3112
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3113
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3114
typedef struct upgrade_cbdata {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3115
	int	cb_all;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3116
	int	cb_first;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3117
	int	cb_newer;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3118
	int	cb_argc;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3119
	uint64_t cb_version;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3120
	char	**cb_argv;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3121
} upgrade_cbdata_t;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3122
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3123
static int
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3124
upgrade_cb(zpool_handle_t *zhp, void *arg)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3125
{
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3126
	upgrade_cbdata_t *cbp = arg;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3127
	nvlist_t *config;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3128
	uint64_t version;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3129
	int ret = 0;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3130
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3131
	config = zpool_get_config(zhp, NULL);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3132
	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3133
	    &version) == 0);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3134
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  3135
	if (!cbp->cb_newer && version < SPA_VERSION) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3136
		if (!cbp->cb_all) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3137
			if (cbp->cb_first) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3138
				(void) printf(gettext("The following pools are "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3139
				    "out of date, and can be upgraded.  After "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3140
				    "being\nupgraded, these pools will no "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3141
				    "longer be accessible by older software "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3142
				    "versions.\n\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3143
				(void) printf(gettext("VER  POOL\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3144
				(void) printf(gettext("---  ------------\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3145
				cbp->cb_first = B_FALSE;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3146
			}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3147
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2731
diff changeset
  3148
			(void) printf("%2llu   %s\n", (u_longlong_t)version,
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3149
			    zpool_get_name(zhp));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3150
		} else {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3151
			cbp->cb_first = B_FALSE;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3152
			ret = zpool_upgrade(zhp, cbp->cb_version);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3153
			if (!ret) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3154
				(void) printf(gettext("Successfully upgraded "
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3155
				    "'%s'\n\n"), zpool_get_name(zhp));
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3156
			}
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3157
		}
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  3158
	} else if (cbp->cb_newer && version > SPA_VERSION) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3159
		assert(!cbp->cb_all);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3160
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3161
		if (cbp->cb_first) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3162
			(void) printf(gettext("The following pools are "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3163
			    "formatted using a newer software version and\n"
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3164
			    "cannot be accessed on the current system.\n\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3165
			(void) printf(gettext("VER  POOL\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3166
			(void) printf(gettext("---  ------------\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3167
			cbp->cb_first = B_FALSE;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3168
		}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3169
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2731
diff changeset
  3170
		(void) printf("%2llu   %s\n", (u_longlong_t)version,
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3171
		    zpool_get_name(zhp));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3172
	}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3173
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3174
	zpool_close(zhp);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3175
	return (ret);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3176
}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3177
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3178
/* ARGSUSED */
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3179
static int
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3180
upgrade_one(zpool_handle_t *zhp, void *data)
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3181
{
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3182
	upgrade_cbdata_t *cbp = data;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3183
	uint64_t cur_version;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3184
	int ret;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3185
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3186
	if (strcmp("log", zpool_get_name(zhp)) == 0) {
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3187
		(void) printf(gettext("'log' is now a reserved word\n"
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3188
		    "Pool 'log' must be renamed using export and import"
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3189
		    " to upgrade.\n"));
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3190
		return (1);
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3191
	}
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3192
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3193
	cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3194
	if (cur_version >= cbp->cb_version) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3195
		(void) printf(gettext("Pool '%s' is already formatted "
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3196
		    "using more current version '%d'.\n"), zpool_get_name(zhp),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3197
		    cur_version);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3198
		return (0);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3199
	}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3200
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3201
	ret = zpool_upgrade(zhp, cbp->cb_version);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3202
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3203
	if (!ret) {
1775
e51e26b432c0 6410698 ZFS metadata needs to be more highly replicated (ditto blocks)
billm
parents: 1760
diff changeset
  3204
		(void) printf(gettext("Successfully upgraded '%s' "
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3205
		    "from version %llu to version %llu\n\n"),
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3206
		    zpool_get_name(zhp), (u_longlong_t)cur_version,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3207
		    (u_longlong_t)cbp->cb_version);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3208
	}
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3209
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3210
	return (ret != 0);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3211
}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3212
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3213
/*
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3214
 * zpool upgrade
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3215
 * zpool upgrade -v
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3216
 * zpool upgrade [-V version] <-a | pool ...>
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3217
 *
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3218
 * With no arguments, display downrev'd ZFS pool available for upgrade.
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3219
 * Individual pools can be upgraded by specifying the pool, and '-a' will
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3220
 * upgrade all pools.
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3221
 */
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3222
int
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3223
zpool_do_upgrade(int argc, char **argv)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3224
{
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3225
	int c;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3226
	upgrade_cbdata_t cb = { 0 };
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3227
	int ret = 0;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3228
	boolean_t showversions = B_FALSE;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3229
	char *end;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3230
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3231
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3232
	/* check options */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3233
	while ((c = getopt(argc, argv, "avV:")) != -1) {
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3234
		switch (c) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3235
		case 'a':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3236
			cb.cb_all = B_TRUE;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3237
			break;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3238
		case 'v':
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3239
			showversions = B_TRUE;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3240
			break;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3241
		case 'V':
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3242
			cb.cb_version = strtoll(optarg, &end, 10);
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3243
			if (*end != '\0' || cb.cb_version > SPA_VERSION ||
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3244
			    cb.cb_version < SPA_VERSION_1) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3245
				(void) fprintf(stderr,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3246
				    gettext("invalid version '%s'\n"), optarg);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3247
				usage(B_FALSE);
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3248
			}
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3249
			break;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3250
		case '?':
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3251
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3252
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3253
			usage(B_FALSE);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3254
		}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3255
	}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3256
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3257
	cb.cb_argc = argc;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3258
	cb.cb_argv = argv;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3259
	argc -= optind;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3260
	argv += optind;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3261
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3262
	if (cb.cb_version == 0) {
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3263
		cb.cb_version = SPA_VERSION;
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3264
	} else if (!cb.cb_all && argc == 0) {
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3265
		(void) fprintf(stderr, gettext("-V option is "
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3266
		    "incompatible with other arguments\n"));
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3267
		usage(B_FALSE);
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3268
	}
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3269
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3270
	if (showversions) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3271
		if (cb.cb_all || argc != 0) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3272
			(void) fprintf(stderr, gettext("-v option is "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3273
			    "incompatible with other arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3274
			usage(B_FALSE);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3275
		}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3276
	} else if (cb.cb_all) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3277
		if (argc != 0) {
5320
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3278
			(void) fprintf(stderr, gettext("-a option should not "
43bde2b01a59 6614232 results of zpool_validate_properties() are consistently leaked
lling
parents: 5094
diff changeset
  3279
			    "be used along with a pool name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3280
			usage(B_FALSE);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3281
		}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3282
	}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3283
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  3284
	(void) printf(gettext("This system is currently running "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  3285
	    "ZFS pool version %llu.\n\n"), SPA_VERSION);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3286
	cb.cb_first = B_TRUE;
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3287
	if (showversions) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3288
		(void) printf(gettext("The following versions are "
3413
f4cbe0204ab7 6471255 Some ZFS error messages contain spelling errors
mmusante
parents: 3377
diff changeset
  3289
		    "supported:\n\n"));
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3290
		(void) printf(gettext("VER  DESCRIPTION\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3291
		(void) printf("---  -----------------------------------------"
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3292
		    "---------------\n");
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3293
		(void) printf(gettext(" 1   Initial ZFS version\n"));
1775
e51e26b432c0 6410698 ZFS metadata needs to be more highly replicated (ditto blocks)
billm
parents: 1760
diff changeset
  3294
		(void) printf(gettext(" 2   Ditto blocks "
e51e26b432c0 6410698 ZFS metadata needs to be more highly replicated (ditto blocks)
billm
parents: 1760
diff changeset
  3295
		    "(replicated metadata)\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3296
		(void) printf(gettext(" 3   Hot spares and double parity "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3297
		    "RAID-Z\n"));
3863
d56571426115 6529406 zpool history needs to bump the on-disk version
ek110237
parents: 3741
diff changeset
  3298
		(void) printf(gettext(" 4   zpool history\n"));
3886
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3863
diff changeset
  3299
		(void) printf(gettext(" 5   Compression using the gzip "
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3863
diff changeset
  3300
		    "algorithm\n"));
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3301
		(void) printf(gettext(" 6   bootfs pool property\n"));
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3302
		(void) printf(gettext(" 7   Separate intent log devices\n"));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3303
		(void) printf(gettext(" 8   Delegated administration\n"));
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5363
diff changeset
  3304
		(void) printf(gettext(" 9  refquota and refreservation "
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5363
diff changeset
  3305
		    "properties\n"));
4527
5d5b6ba91b17 PSARC 2007/171 ZFS Separate Intent Log
perrin
parents: 4451
diff changeset
  3306
		(void) printf(gettext("For more information on a particular "
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3307
		    "version, including supported releases, see:\n\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3308
		(void) printf("http://www.opensolaris.org/os/community/zfs/"
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3309
		    "version/N\n\n");
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3310
		(void) printf(gettext("Where 'N' is the version number.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3311
	} else if (argc == 0) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3312
		int notfound;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3313
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3314
		ret = zpool_iter(g_zfs, upgrade_cb, &cb);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3315
		notfound = cb.cb_first;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3316
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3317
		if (!cb.cb_all && ret == 0) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3318
			if (!cb.cb_first)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3319
				(void) printf("\n");
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3320
			cb.cb_first = B_TRUE;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3321
			cb.cb_newer = B_TRUE;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3322
			ret = zpool_iter(g_zfs, upgrade_cb, &cb);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3323
			if (!cb.cb_first) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3324
				notfound = B_FALSE;
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3325
				(void) printf("\n");
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3326
			}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3327
		}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3328
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3329
		if (ret == 0) {
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3330
			if (notfound)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3331
				(void) printf(gettext("All pools are formatted "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3332
				    "using this version.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3333
			else if (!cb.cb_all)
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3334
				(void) printf(gettext("Use 'zpool upgrade -v' "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3335
				    "for a list of available versions and "
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3336
				    "their associated\nfeatures.\n"));
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3337
		}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3338
	} else {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3339
		ret = for_each_pool(argc, argv, B_FALSE, NULL,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3340
		    upgrade_one, &cb);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3341
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3342
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3343
	return (ret);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3344
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3345
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3346
typedef struct hist_cbdata {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3347
	boolean_t first;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3348
	int longfmt;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3349
	int internal;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3350
} hist_cbdata_t;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3351
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3352
char *hist_event_table[LOG_END] = {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3353
	"invalid event",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3354
	"pool create",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3355
	"vdev add",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3356
	"pool remove",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3357
	"pool destroy",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3358
	"pool export",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3359
	"pool import",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3360
	"vdev attach",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3361
	"vdev replace",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3362
	"vdev detach",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3363
	"vdev online",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3364
	"vdev offline",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3365
	"vdev upgrade",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3366
	"pool clear",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3367
	"pool scrub",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3368
	"pool property set",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3369
	"create",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3370
	"clone",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3371
	"destroy",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3372
	"destroy_begin_sync",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3373
	"inherit",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3374
	"property set",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3375
	"quota set",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3376
	"permission update",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3377
	"permission remove",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3378
	"permission who remove",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3379
	"promote",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3380
	"receive",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3381
	"rename",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3382
	"reservation set",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3383
	"replay_inc_sync",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3384
	"replay_full_sync",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3385
	"rollback",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3386
	"snapshot",
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  3387
	"filesystem version upgrade",
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5363
diff changeset
  3388
	"refquota set",
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5363
diff changeset
  3389
	"refreservation set",
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3390
};
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3391
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3392
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3393
 * Print out the command history for a specific pool.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3394
 */
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3395
static int
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3396
get_history_one(zpool_handle_t *zhp, void *data)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3397
{
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3398
	nvlist_t *nvhis;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3399
	nvlist_t **records;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3400
	uint_t numrecords;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3401
	char *cmdstr;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3402
	char *pathstr;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3403
	uint64_t dst_time;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3404
	time_t tsec;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3405
	struct tm t;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3406
	char tbuf[30];
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3407
	int ret, i;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3408
	uint64_t who;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3409
	struct passwd *pwd;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3410
	char *hostname;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3411
	char *zonename;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3412
	char internalstr[MAXPATHLEN];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3413
	hist_cbdata_t *cb = (hist_cbdata_t *)data;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3414
	uint64_t txg;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3415
	uint64_t ievent;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3416
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3417
	cb->first = B_FALSE;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3418
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3419
	(void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3420
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3421
	if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3422
		return (ret);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3423
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3424
	verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3425
	    &records, &numrecords) == 0);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3426
	for (i = 0; i < numrecords; i++) {
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3427
		if (nvlist_lookup_uint64(records[i], ZPOOL_HIST_TIME,
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3428
		    &dst_time) != 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3429
			continue;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3430
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3431
		/* is it an internal event or a standard event? */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3432
		if (nvlist_lookup_string(records[i], ZPOOL_HIST_CMD,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3433
		    &cmdstr) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3434
			if (cb->internal == 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3435
				continue;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3436
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3437
			if (nvlist_lookup_uint64(records[i],
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3438
			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3439
				continue;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3440
			verify(nvlist_lookup_uint64(records[i],
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3441
			    ZPOOL_HIST_TXG, &txg) == 0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3442
			verify(nvlist_lookup_string(records[i],
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3443
			    ZPOOL_HIST_INT_STR, &pathstr) == 0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3444
			if (ievent > LOG_END)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3445
				continue;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3446
			(void) snprintf(internalstr,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3447
			    sizeof (internalstr),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3448
			    "[internal %s txg:%lld] %s",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3449
			    hist_event_table[ievent], txg,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3450
			    pathstr);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3451
			cmdstr = internalstr;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3452
		}
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3453
		tsec = dst_time;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3454
		(void) localtime_r(&tsec, &t);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3455
		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3456
		(void) printf("%s %s", tbuf, cmdstr);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3457
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3458
		if (!cb->longfmt) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3459
			(void) printf("\n");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3460
			continue;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3461
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3462
		(void) printf(" [");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3463
		if (nvlist_lookup_uint64(records[i],
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3464
		    ZPOOL_HIST_WHO, &who) == 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3465
			pwd = getpwuid((uid_t)who);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3466
			if (pwd)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3467
				(void) printf("user %s on",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3468
				    pwd->pw_name);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3469
			else
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3470
				(void) printf("user %d on",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3471
				    (int)who);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3472
		} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3473
			(void) printf(gettext("no info]\n"));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3474
			continue;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3475
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3476
		if (nvlist_lookup_string(records[i],
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3477
		    ZPOOL_HIST_HOST, &hostname) == 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3478
			(void) printf(" %s", hostname);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3479
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3480
		if (nvlist_lookup_string(records[i],
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3481
		    ZPOOL_HIST_ZONE, &zonename) == 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3482
			(void) printf(":%s", zonename);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3483
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3484
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3485
		(void) printf("]");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3486
		(void) printf("\n");
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3487
	}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3488
	(void) printf("\n");
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3489
	nvlist_free(nvhis);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3490
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3491
	return (ret);
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3492
}
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3493
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3494
/*
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3495
 * zpool history <pool>
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3496
 *
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3497
 * Displays the history of commands that modified pools.
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3498
 */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3499
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3500
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3501
int
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3502
zpool_do_history(int argc, char **argv)
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3503
{
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3504
	hist_cbdata_t cbdata = { 0 };
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3505
	int ret;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3506
	int c;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3507
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3508
	cbdata.first = B_TRUE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3509
	/* check options */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3510
	while ((c = getopt(argc, argv, "li")) != -1) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3511
		switch (c) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3512
		case 'l':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3513
			cbdata.longfmt = 1;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3514
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3515
		case 'i':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3516
			cbdata.internal = 1;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3517
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3518
		case '?':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3519
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3520
			    optopt);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3521
			usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3522
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3523
	}
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3524
	argc -= optind;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3525
	argv += optind;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3526
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3527
	ret = for_each_pool(argc, argv, B_FALSE,  NULL, get_history_one,
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3528
	    &cbdata);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3529
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4527
diff changeset
  3530
	if (argc == 0 && cbdata.first == B_TRUE) {
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3531
		(void) printf(gettext("no pools available\n"));
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2856
diff changeset
  3532
		return (0);
1760
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3533
	}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3534
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3535
	return (ret);
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3536
}
e1ad2821c30d PSARC 2006/206 zpool upgrade
eschrock
parents: 1631
diff changeset
  3537
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3538
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3539
get_callback(zpool_handle_t *zhp, void *data)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3540
{
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3541
	zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3542
	char value[MAXNAMELEN];
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3543
	zprop_source_t srctype;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3544
	zprop_list_t *pl;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3545
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3546
	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3547
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3548
		/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3549
		 * Skip the special fake placeholder. This will also skip
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3550
		 * over the name property when 'all' is specified.
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3551
		 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3552
		if (pl->pl_prop == ZPOOL_PROP_NAME &&
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3553
		    pl == cbp->cb_proplist)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3554
			continue;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3555
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3556
		if (zpool_get_prop(zhp, pl->pl_prop,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3557
		    value, sizeof (value), &srctype) != 0)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3558
			continue;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3559
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3560
		zprop_print_one_property(zpool_get_name(zhp), cbp,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3561
		    zpool_prop_to_name(pl->pl_prop), value, srctype, NULL);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3562
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3563
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3564
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3565
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3566
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3567
zpool_do_get(int argc, char **argv)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3568
{
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3569
	zprop_get_cbdata_t cb = { 0 };
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3570
	zprop_list_t fake_name = { 0 };
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3571
	int ret;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3572
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3573
	if (argc < 3)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3574
		usage(B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3575
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3576
	cb.cb_first = B_TRUE;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3577
	cb.cb_sources = ZPROP_SRC_ALL;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3578
	cb.cb_columns[0] = GET_COL_NAME;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3579
	cb.cb_columns[1] = GET_COL_PROPERTY;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3580
	cb.cb_columns[2] = GET_COL_VALUE;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3581
	cb.cb_columns[3] = GET_COL_SOURCE;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3582
	cb.cb_type = ZFS_TYPE_POOL;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3583
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3584
	if (zprop_get_list(g_zfs, argv[1],  &cb.cb_proplist,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3585
	    ZFS_TYPE_POOL) != 0)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3586
		usage(B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3587
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3588
	if (cb.cb_proplist != NULL) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3589
		fake_name.pl_prop = ZPOOL_PROP_NAME;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3590
		fake_name.pl_width = strlen(gettext("NAME"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3591
		fake_name.pl_next = cb.cb_proplist;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3592
		cb.cb_proplist = &fake_name;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3593
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3594
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3595
	ret = for_each_pool(argc - 2, argv + 2, B_TRUE, &cb.cb_proplist,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3596
	    get_callback, &cb);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3597
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3598
	if (cb.cb_proplist == &fake_name)
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3599
		zprop_free_list(fake_name.pl_next);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3600
	else
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  3601
		zprop_free_list(cb.cb_proplist);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3602
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3603
	return (ret);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3604
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3605
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3606
typedef struct set_cbdata {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3607
	char *cb_propname;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3608
	char *cb_value;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3609
	boolean_t cb_any_successful;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3610
} set_cbdata_t;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3611
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3612
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3613
set_callback(zpool_handle_t *zhp, void *data)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3614
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3615
	int error;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3616
	set_cbdata_t *cb = (set_cbdata_t *)data;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3617
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3618
	error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3619
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3620
	if (!error)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3621
		cb->cb_any_successful = B_TRUE;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3622
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3623
	return (error);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3624
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3625
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3626
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3627
zpool_do_set(int argc, char **argv)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3628
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3629
	set_cbdata_t cb = { 0 };
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3630
	int error;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3631
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3632
	if (argc > 1 && argv[1][0] == '-') {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3633
		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3634
		    argv[1][1]);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3635
		usage(B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3636
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3637
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3638
	if (argc < 2) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3639
		(void) fprintf(stderr, gettext("missing property=value "
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3640
		    "argument\n"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3641
		usage(B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3642
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3643
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3644
	if (argc < 3) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3645
		(void) fprintf(stderr, gettext("missing pool name\n"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3646
		usage(B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3647
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3648
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3649
	if (argc > 3) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3650
		(void) fprintf(stderr, gettext("too many pool names\n"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3651
		usage(B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3652
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3653
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3654
	cb.cb_propname = argv[1];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3655
	cb.cb_value = strchr(cb.cb_propname, '=');
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3656
	if (cb.cb_value == NULL) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3657
		(void) fprintf(stderr, gettext("missing value in "
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3658
		    "property=value argument\n"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3659
		usage(B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3660
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3661
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3662
	*(cb.cb_value) = '\0';
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3663
	cb.cb_value++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3664
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3665
	error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3666
	    set_callback, &cb);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3667
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3668
	return (error);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3669
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3670
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3671
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3672
find_command_idx(char *command, int *idx)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3673
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3674
	int i;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3675
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3676
	for (i = 0; i < NCOMMAND; i++) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3677
		if (command_table[i].name == NULL)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3678
			continue;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3679
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3680
		if (strcmp(command, command_table[i].name) == 0) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3681
			*idx = i;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3682
			return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3683
		}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3684
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3685
	return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3686
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3687
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3688
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3689
main(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3690
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3691
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3692
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3693
	char *cmdname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3694
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3695
	(void) setlocale(LC_ALL, "");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3696
	(void) textdomain(TEXT_DOMAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3697
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3698
	if ((g_zfs = libzfs_init()) == NULL) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3699
		(void) fprintf(stderr, gettext("internal error: failed to "
2731
2dd6e16860b6 6463349 error message from zpool(1M) is missing a newline
nd150628
parents: 2676
diff changeset
  3700
		    "initialize ZFS library\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3701
		return (1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3702
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3703
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3704
	libzfs_print_on_error(g_zfs, B_TRUE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3705
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3706
	opterr = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3707
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3708
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3709
	 * Make sure the user has specified some command.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3710
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3711
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3712
		(void) fprintf(stderr, gettext("missing command\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3713
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3714
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3715
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3716
	cmdname = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3717
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3718
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3719
	 * Special case '-?'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3720
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3721
	if (strcmp(cmdname, "-?") == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3722
		usage(B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3723
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4849
diff changeset
  3724
	zpool_set_history_str("zpool", argc, argv, history_str);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4849
diff changeset
  3725
	verify(zpool_stage_history(g_zfs, history_str) == 0);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4849
diff changeset
  3726
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3727
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3728
	 * Run the appropriate command.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3729
	 */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3730
	if (find_command_idx(cmdname, &i) == 0) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3731
		current_command = &command_table[i];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3732
		ret = command_table[i].func(argc - 1, argv + 1);
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3733
	} else if (strchr(cmdname, '=')) {
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3734
		verify(find_command_idx("set", &i) == 0);
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3735
		current_command = &command_table[i];
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3736
		ret = command_table[i].func(argc, argv);
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3737
	} else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3738
		/*
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3739
		 * 'freeze' is a vile debugging abomination, so we treat
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3740
		 * it as such.
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3741
		 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3742
		char buf[16384];
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1485
diff changeset
  3743
		int fd = open(ZFS_DEV, O_RDWR);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3744
		(void) strcpy((void *)buf, argv[2]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3745
		return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4715
diff changeset
  3746
	} else {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3747
		(void) fprintf(stderr, gettext("unrecognized "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3748
		    "command '%s'\n"), cmdname);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3749
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3750
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3751
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3752
	libzfs_fini(g_zfs);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1775
diff changeset
  3753
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3754
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3755
	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3756
	 * for the purposes of running ::findleaks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3757
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3758
	if (getenv("ZFS_ABORT") != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3759
		(void) printf("dumping core by request\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3760
		abort();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3761
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3762
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3763
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3764
}