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