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