xref: /illumos-gate/usr/src/cmd/fs.d/zfs/fstyp/fstyp.c (revision eaca9bbd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <libintl.h>
34 #include <locale.h>
35 #include <string.h>
36 #include <libzfs.h>
37 #include <errno.h>
38 
39 static void
40 usage(void)
41 {
42 	(void) fprintf(stderr, gettext("Usage: fstype [-v] <device>\n"));
43 	exit(1);
44 }
45 
46 static void
47 dump_nvlist(nvlist_t *list, int indent)
48 {
49 	nvpair_t *elem = NULL;
50 
51 	while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
52 		switch (nvpair_type(elem)) {
53 		case DATA_TYPE_STRING:
54 			{
55 				char *value;
56 
57 				verify(nvpair_value_string(elem, &value) == 0);
58 				(void) printf("%*s%s='%s'\n", indent, "",
59 				    nvpair_name(elem), value);
60 			}
61 			break;
62 
63 		case DATA_TYPE_UINT64:
64 			{
65 				uint64_t value;
66 
67 				verify(nvpair_value_uint64(elem, &value) == 0);
68 				(void) printf("%*s%s=%llu\n", indent, "",
69 				    nvpair_name(elem), (u_longlong_t)value);
70 			}
71 			break;
72 
73 		case DATA_TYPE_NVLIST:
74 			{
75 				nvlist_t *value;
76 
77 				verify(nvpair_value_nvlist(elem, &value) == 0);
78 				(void) printf("%*s%s\n", indent, "",
79 				    nvpair_name(elem));
80 				dump_nvlist(value, indent + 4);
81 			}
82 			break;
83 
84 		case DATA_TYPE_NVLIST_ARRAY:
85 			{
86 				nvlist_t **value;
87 				uint_t c, count;
88 
89 				verify(nvpair_value_nvlist_array(elem, &value,
90 				    &count) == 0);
91 
92 				for (c = 0; c < count; c++) {
93 					(void) printf("%*s%s[%u]\n", indent, "",
94 					    nvpair_name(elem), c);
95 					dump_nvlist(value[c], indent + 8);
96 				}
97 			}
98 			break;
99 
100 		default:
101 
102 			(void) printf("bad config type %d for %s\n",
103 			    nvpair_type(elem), nvpair_name(elem));
104 		}
105 	}
106 }
107 
108 int
109 main(int argc, char **argv)
110 {
111 	int c, fd;
112 	int verbose = 0;
113 	nvlist_t *config;
114 	uint64_t state;
115 
116 	(void) setlocale(LC_ALL, "");
117 
118 #if !defined(TEXT_DOMAIN)
119 #define	TEXT_DOMAIN "SYS_TEST"
120 #endif
121 	(void) textdomain(TEXT_DOMAIN);
122 
123 	while ((c = getopt(argc, argv, "v")) != -1) {
124 		switch (c) {
125 		case 'v':
126 			verbose = 1;
127 			break;
128 		default:
129 			usage();
130 			break;
131 		}
132 	}
133 
134 	argv += optind;
135 	argc -= optind;
136 
137 	if (argc != 1)
138 		usage();
139 
140 	if ((fd = open64(argv[0], O_RDONLY)) < 0) {
141 		perror("open64");
142 		return (1);
143 	}
144 
145 	if ((config = zpool_read_label(fd)) == NULL)
146 		return (1);
147 
148 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
149 	    &state) != 0 || state == POOL_STATE_DESTROYED)
150 		return (1);
151 
152 	(void) printf("zfs\n");
153 
154 	if (verbose)
155 		dump_nvlist(config, 4);
156 
157 	(void) close(fd);
158 
159 	return (0);
160 }
161