xref: /illumos-gate/usr/src/cmd/virtinfo/virtinfo.c (revision 5bcb2410)
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 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <libgen.h>
33 #include <libintl.h>
34 #include <libv12n.h>
35 #include <locale.h>
36 #include <zone.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <uuid/uuid.h>
40 
41 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
42 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
43 #endif
44 
45 static char *cmdname;
46 
47 char *options = "acdpstu";
48 
49 static void
virtinfo_usage()50 virtinfo_usage()
51 {
52 	(void) fprintf(stderr, gettext("usage: %s [-%s]\n"), cmdname, options);
53 	exit(1);
54 }
55 
56 static char *
virtinfo_cap_to_impl(int cap)57 virtinfo_cap_to_impl(int cap)
58 {
59 	if (cap & V12N_CAP_IMPL_LDOMS)
60 		return ("LDoms");
61 	return ("Unknown");
62 }
63 
64 
65 int
main(int argc,char * argv[])66 main(int argc, char *argv[])
67 {
68 	int cap;
69 	int roles;
70 	size_t rv;
71 	int opt;
72 	int errflg = 0;
73 	int aflg = 0, cflg = 0, dflg = 0, pflg = 0, sflg = 0, tflg = 0,
74 	    uflg = 0;
75 
76 	/* Set locale environment variables local definitions */
77 	(void) setlocale(LC_ALL, "");
78 	(void) textdomain(TEXT_DOMAIN);
79 
80 	cmdname = basename(argv[0]);
81 
82 	/* disable getopt error messages */
83 	opterr = 0;
84 
85 	while ((opt = getopt(argc, argv, options)) != EOF) {
86 
87 		switch (opt) {
88 		case 'a':
89 			aflg = 1;
90 			break;
91 		case 'c':
92 			cflg = 1;
93 			break;
94 		case 'd':
95 			dflg = 1;
96 			break;
97 		case 'p':
98 			pflg = 1;
99 			break;
100 		case 's':
101 			sflg = 1;
102 			break;
103 		case 't':
104 			tflg = 1;
105 			break;
106 		case 'u':
107 			uflg = 1;
108 			break;
109 		case '?':
110 		default:
111 			errflg = 1;
112 			break;
113 		}
114 	}
115 
116 	if (errflg || optind != argc)
117 		virtinfo_usage();
118 
119 	if (aflg) {
120 		/* aflg -> set all flags except -p */
121 		cflg = dflg = sflg = tflg = uflg = 1;
122 	} else if (cflg == 0 && dflg == 0 && sflg == 0 && tflg == 0 &&
123 	    uflg == 0) {
124 		/* no flag set, default to '-t' */
125 		tflg = 1;
126 	}
127 
128 	if (getzoneid() != GLOBAL_ZONEID) {
129 		(void) printf(gettext(
130 		    "%s can only be run from the global zone\n"), cmdname);
131 		exit(0);
132 	}
133 
134 	cap = v12n_capabilities();
135 	if ((cap & V12N_CAP_SUPPORTED) == 0) {
136 		(void) printf(gettext("Virtual machines are not supported\n"));
137 		exit(0);
138 	} else if ((cap & V12N_CAP_ENABLED) == 0) {
139 		(void) printf(gettext(
140 		    "Virtual machines (%s) are supported but not enabled\n"),
141 		    virtinfo_cap_to_impl(cap));
142 		exit(0);
143 	}
144 
145 	if (pflg) {
146 		(void) printf("VERSION 1.0\n");
147 	}
148 
149 	if (tflg) {
150 		char *impl = "", *role = "", *io = "", *service = "",
151 		    *root = "";
152 
153 		roles = v12n_domain_roles();
154 
155 		if (roles == -1 || (cap & V12N_CAP_IMPL_LDOMS) == 0) {
156 			if (pflg)
157 				impl = "impl=Unknown";
158 			else
159 				impl = "Unknown";
160 		} else if (pflg) {
161 			impl = "impl=LDoms";
162 			role = (roles & V12N_ROLE_CONTROL) ?
163 			    "|control=true" : "|control=false";
164 			io = (roles & V12N_ROLE_IO) ?
165 			    "|io=true" : "|io=false";
166 			service = (roles & V12N_ROLE_SERVICE) ?
167 			    "|service=true" : "|service=false";
168 			root = (roles & V12N_ROLE_ROOT) ?
169 			    "|root=true" : "|root=false";
170 		} else {
171 			impl = "LDoms";
172 			role = (roles & V12N_ROLE_CONTROL) ?
173 			    " control" : " guest";
174 			io = (roles & V12N_ROLE_IO) ?
175 			    " I/O" : "";
176 			service = (roles & V12N_ROLE_SERVICE) ?
177 			    " service" : "";
178 			root = (roles & V12N_ROLE_ROOT) ?
179 			    " root" : "";
180 		}
181 		(void) printf("%s%s%s%s%s%s\n", pflg ? "DOMAINROLE|" :
182 		    gettext("Domain role: "), impl, role, io, service, root);
183 	}
184 
185 	if (dflg) {
186 		char domain_name[V12N_NAME_MAX];
187 
188 		rv = v12n_domain_name(domain_name, sizeof (domain_name));
189 		if (rv == (size_t)(-1)) {
190 			(void) strcpy(domain_name, "Unknown");
191 		}
192 		(void) printf("%s%s\n", pflg ? "DOMAINNAME|name=" :
193 		    gettext("Domain name: "), domain_name);
194 	}
195 
196 	if (uflg) {
197 		uuid_t uuid;
198 		char uuid_str[UUID_PRINTABLE_STRING_LENGTH];
199 
200 		rv = v12n_domain_uuid(uuid);
201 
202 		if (rv == (size_t)(-1)) {
203 			(void) strcpy(uuid_str, "Unknown");
204 		} else {
205 			uuid_unparse(uuid, uuid_str);
206 		}
207 		(void) printf("%s%s\n", pflg ? "DOMAINUUID|uuid=" :
208 		    gettext("Domain UUID: "), uuid_str);
209 	}
210 
211 	if (cflg) {
212 		char ctrl_name[V12N_NAME_MAX];
213 
214 		rv = v12n_ctrl_domain(ctrl_name, sizeof (ctrl_name));
215 
216 		if (rv == (size_t)(-1)) {
217 			(void) strcpy(ctrl_name, "Unknown");
218 		}
219 		(void) printf("%s%s\n", pflg ? "DOMAINCONTROL|name=" :
220 		    gettext("Control domain: "), ctrl_name);
221 	}
222 
223 	if (sflg) {
224 		char serial_no[V12N_NAME_MAX];
225 
226 		rv = v12n_chassis_serialno(serial_no, sizeof (serial_no));
227 
228 		if (rv == (size_t)(-1)) {
229 			(void) strcpy(serial_no, "Unknown");
230 		}
231 		(void) printf("%s%s\n", pflg ? "DOMAINCHASSIS|serialno=" :
232 		    gettext("Chassis serial#: "), serial_no);
233 	}
234 	return (0);
235 }
236