182629e3Mike Christensen/*
282629e3Mike Christensen * CDDL HEADER START
382629e3Mike Christensen *
482629e3Mike Christensen * The contents of this file are subject to the terms of the
582629e3Mike Christensen * Common Development and Distribution License (the "License").
682629e3Mike Christensen * You may not use this file except in compliance with the License.
782629e3Mike Christensen *
882629e3Mike Christensen * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
982629e3Mike Christensen * or http://www.opensolaris.org/os/licensing.
1082629e3Mike Christensen * See the License for the specific language governing permissions
1182629e3Mike Christensen * and limitations under the License.
1282629e3Mike Christensen *
1382629e3Mike Christensen * When distributing Covered Code, include this CDDL HEADER in each
1482629e3Mike Christensen * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1582629e3Mike Christensen * If applicable, add the following below this CDDL HEADER, with the
1682629e3Mike Christensen * fields enclosed by brackets "[]" replaced with your own identifying
1782629e3Mike Christensen * information: Portions Copyright [yyyy] [name of copyright owner]
1882629e3Mike Christensen *
1982629e3Mike Christensen * CDDL HEADER END
2082629e3Mike Christensen */
2182629e3Mike Christensen
2282629e3Mike Christensen/*
2382629e3Mike Christensen * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
2482629e3Mike Christensen * Use is subject to license terms.
2582629e3Mike Christensen */
2682629e3Mike Christensen
2782629e3Mike Christensen#include <errno.h>
2882629e3Mike Christensen#include <stdio.h>
2982629e3Mike Christensen#include <stdlib.h>
3082629e3Mike Christensen#include <strings.h>
3182629e3Mike Christensen#include <unistd.h>
3282629e3Mike Christensen#include <libgen.h>
3382629e3Mike Christensen#include <libintl.h>
3482629e3Mike Christensen#include <libv12n.h>
355bcb241Mike Christensen#include <locale.h>
3682629e3Mike Christensen#include <zone.h>
3782629e3Mike Christensen#include <sys/types.h>
3882629e3Mike Christensen#include <sys/param.h>
3982629e3Mike Christensen#include <uuid/uuid.h>
4082629e3Mike Christensen
415bcb241Mike Christensen#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
425bcb241Mike Christensen#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
435bcb241Mike Christensen#endif
445bcb241Mike Christensen
4582629e3Mike Christensenstatic char *cmdname;
4682629e3Mike Christensen
4782629e3Mike Christensenchar *options = "acdpstu";
4882629e3Mike Christensen
4982629e3Mike Christensenstatic void
5082629e3Mike Christensenvirtinfo_usage()
5182629e3Mike Christensen{
5282629e3Mike Christensen	(void) fprintf(stderr, gettext("usage: %s [-%s]\n"), cmdname, options);
5382629e3Mike Christensen	exit(1);
5482629e3Mike Christensen}
5582629e3Mike Christensen
5682629e3Mike Christensenstatic char *
5782629e3Mike Christensenvirtinfo_cap_to_impl(int cap)
5882629e3Mike Christensen{
5982629e3Mike Christensen	if (cap & V12N_CAP_IMPL_LDOMS)
6082629e3Mike Christensen		return ("LDoms");
6182629e3Mike Christensen	return ("Unknown");
6282629e3Mike Christensen}
6382629e3Mike Christensen
6482629e3Mike Christensen
6582629e3Mike Christensenint
6682629e3Mike Christensenmain(int argc, char *argv[])
6782629e3Mike Christensen{
6882629e3Mike Christensen	int cap;
6982629e3Mike Christensen	int roles;
7082629e3Mike Christensen	size_t rv;
7182629e3Mike Christensen	int opt;
7282629e3Mike Christensen	int errflg = 0;
7382629e3Mike Christensen	int aflg = 0, cflg = 0, dflg = 0, pflg = 0, sflg = 0, tflg = 0,
7482629e3Mike Christensen	    uflg = 0;
7582629e3Mike Christensen
765bcb241Mike Christensen	/* Set locale environment variables local definitions */
775bcb241Mike Christensen	(void) setlocale(LC_ALL, "");
785bcb241Mike Christensen	(void) textdomain(TEXT_DOMAIN);
795bcb241Mike Christensen
8082629e3Mike Christensen	cmdname = basename(argv[0]);
8182629e3Mike Christensen
8282629e3Mike Christensen	/* disable getopt error messages */
8382629e3Mike Christensen	opterr = 0;
8482629e3Mike Christensen
8582629e3Mike Christensen	while ((opt = getopt(argc, argv, options)) != EOF) {
8682629e3Mike Christensen
8782629e3Mike Christensen		switch (opt) {
8882629e3Mike Christensen		case 'a':
8982629e3Mike Christensen			aflg = 1;
9082629e3Mike Christensen			break;
9182629e3Mike Christensen		case 'c':
9282629e3Mike Christensen			cflg = 1;
9382629e3Mike Christensen			break;
9482629e3Mike Christensen		case 'd':
9582629e3Mike Christensen			dflg = 1;
9682629e3Mike Christensen			break;
9782629e3Mike Christensen		case 'p':
9882629e3Mike Christensen			pflg = 1;
9982629e3Mike Christensen			break;
10082629e3Mike Christensen		case 's':
10182629e3Mike Christensen			sflg = 1;
10282629e3Mike Christensen			break;
10382629e3Mike Christensen		case 't':
10482629e3Mike Christensen			tflg = 1;
10582629e3Mike Christensen			break;
10682629e3Mike Christensen		case 'u':
10782629e3Mike Christensen			uflg = 1;
10882629e3Mike Christensen			break;
10982629e3Mike Christensen		case '?':
11082629e3Mike Christensen		default:
11182629e3Mike Christensen			errflg = 1;
11282629e3Mike Christensen			break;
11382629e3Mike Christensen		}
11482629e3Mike Christensen	}
11582629e3Mike Christensen
11682629e3Mike Christensen	if (errflg || optind != argc)
11782629e3Mike Christensen		virtinfo_usage();
11882629e3Mike Christensen
11982629e3Mike Christensen	if (aflg) {
12082629e3Mike Christensen		/* aflg -> set all flags except -p */
12182629e3Mike Christensen		cflg = dflg = sflg = tflg = uflg = 1;
12282629e3Mike Christensen	} else if (cflg == 0 && dflg == 0 && sflg == 0 && tflg == 0 &&
12382629e3Mike Christensen	    uflg == 0) {
12482629e3Mike Christensen		/* no flag set, default to '-t' */
12582629e3Mike Christensen		tflg = 1;
12682629e3Mike Christensen	}
12782629e3Mike Christensen
12882629e3Mike Christensen	if (getzoneid() != GLOBAL_ZONEID) {
12982629e3Mike Christensen		(void) printf(gettext(
13082629e3Mike Christensen		    "%s can only be run from the global zone\n"), cmdname);
13182629e3Mike Christensen		exit(0);
13282629e3Mike Christensen	}
13382629e3Mike Christensen
13482629e3Mike Christensen	cap = v12n_capabilities();
13582629e3Mike Christensen	if ((cap & V12N_CAP_SUPPORTED) == 0) {
13682629e3Mike Christensen		(void) printf(gettext("Virtual machines are not supported\n"));
13782629e3Mike Christensen		exit(0);
13882629e3Mike Christensen	} else if ((cap & V12N_CAP_ENABLED) == 0) {
13982629e3Mike Christensen		(void) printf(gettext(
14082629e3Mike Christensen		    "Virtual machines (%s) are supported but not enabled\n"),
14182629e3Mike Christensen		    virtinfo_cap_to_impl(cap));
14282629e3Mike Christensen		exit(0);
14382629e3Mike Christensen	}
14482629e3Mike Christensen
14582629e3Mike Christensen	if (pflg) {
14682629e3Mike Christensen		(void) printf("VERSION 1.0\n");
14782629e3Mike Christensen	}
14882629e3Mike Christensen
14982629e3Mike Christensen	if (tflg) {
15082629e3Mike Christensen		char *impl = "", *role = "", *io = "", *service = "",
15182629e3Mike Christensen		    *root = "";
15282629e3Mike Christensen
15382629e3Mike Christensen		roles = v12n_domain_roles();
15482629e3Mike Christensen
15582629e3Mike Christensen		if (roles == -1 || (cap & V12N_CAP_IMPL_LDOMS) == 0) {
15682629e3Mike Christensen			if (pflg)
15782629e3Mike Christensen				impl = "impl=Unknown";
15882629e3Mike Christensen			else
15982629e3Mike Christensen				impl = "Unknown";
16082629e3Mike Christensen		} else if (pflg) {
16182629e3Mike Christensen			impl = "impl=LDoms";
16282629e3Mike Christensen			role = (roles & V12N_ROLE_CONTROL) ?
16382629e3Mike Christensen			    "|control=true" : "|control=false";
16482629e3Mike Christensen			io = (roles & V12N_ROLE_IO) ?
16582629e3Mike Christensen			    "|io=true" : "|io=false";
16682629e3Mike Christensen			service = (roles & V12N_ROLE_SERVICE) ?
16782629e3Mike Christensen			    "|service=true" : "|service=false";
16882629e3Mike Christensen			root = (roles & V12N_ROLE_ROOT) ?
16982629e3Mike Christensen			    "|root=true" : "|root=false";
17082629e3Mike Christensen		} else {
17182629e3Mike Christensen			impl = "LDoms";
17282629e3Mike Christensen			role = (roles & V12N_ROLE_CONTROL) ?
17382629e3Mike Christensen			    " control" : " guest";
17482629e3Mike Christensen			io = (roles & V12N_ROLE_IO) ?
17582629e3Mike Christensen			    " I/O" : "";
17682629e3Mike Christensen			service = (roles & V12N_ROLE_SERVICE) ?
17782629e3Mike Christensen			    " service" : "";
17882629e3Mike Christensen			root = (roles & V12N_ROLE_ROOT) ?
17982629e3Mike Christensen			    " root" : "";
18082629e3Mike Christensen		}
18182629e3Mike Christensen		(void) printf("%s%s%s%s%s%s\n", pflg ? "DOMAINROLE|" :
18282629e3Mike Christensen		    gettext("Domain role: "), impl, role, io, service, root);
18382629e3Mike Christensen	}
18482629e3Mike Christensen
18582629e3Mike Christensen	if (dflg) {
18682629e3Mike Christensen		char domain_name[V12N_NAME_MAX];
18782629e3Mike Christensen
18882629e3Mike Christensen		rv = v12n_domain_name(domain_name, sizeof (domain_name));
18982629e3Mike Christensen		if (rv == (size_t)(-1)) {
19082629e3Mike Christensen			(void) strcpy(domain_name, "Unknown");
19182629e3Mike Christensen		}
19282629e3Mike Christensen		(void) printf("%s%s\n", pflg ? "DOMAINNAME|name=" :
19382629e3Mike Christensen		    gettext("Domain name: "), domain_name);
19482629e3Mike Christensen	}
19582629e3Mike Christensen
19682629e3Mike Christensen	if (uflg) {
19782629e3Mike Christensen		uuid_t uuid;
19882629e3Mike Christensen		char uuid_str[UUID_PRINTABLE_STRING_LENGTH];
19982629e3Mike Christensen
20082629e3Mike Christensen		rv = v12n_domain_uuid(uuid);
20182629e3Mike Christensen
20282629e3Mike Christensen		if (rv == (size_t)(-1)) {
20382629e3Mike Christensen			(void) strcpy(uuid_str, "Unknown");
20482629e3Mike Christensen		} else {
20582629e3Mike Christensen			uuid_unparse(uuid, uuid_str);
20682629e3Mike Christensen		}
20782629e3Mike Christensen		(void) printf("%s%s\n", pflg ? "DOMAINUUID|uuid=" :
20882629e3Mike Christensen		    gettext("Domain UUID: "), uuid_str);
20982629e3Mike Christensen	}
21082629e3Mike Christensen
21182629e3Mike Christensen	if (cflg) {
21282629e3Mike Christensen		char ctrl_name[V12N_NAME_MAX];
21382629e3Mike Christensen
21482629e3Mike Christensen		rv = v12n_ctrl_domain(ctrl_name, sizeof (ctrl_name));
21582629e3Mike Christensen
21682629e3Mike Christensen		if (rv == (size_t)(-1)) {
21782629e3Mike Christensen			(void) strcpy(ctrl_name, "Unknown");
21882629e3Mike Christensen		}
21982629e3Mike Christensen		(void) printf("%s%s\n", pflg ? "DOMAINCONTROL|name=" :
22082629e3Mike Christensen		    gettext("Control domain: "), ctrl_name);
22182629e3Mike Christensen	}
22282629e3Mike Christensen
22382629e3Mike Christensen	if (sflg) {
22482629e3Mike Christensen		char serial_no[V12N_NAME_MAX];
22582629e3Mike Christensen
22682629e3Mike Christensen		rv = v12n_chassis_serialno(serial_no, sizeof (serial_no));
22782629e3Mike Christensen
22882629e3Mike Christensen		if (rv == (size_t)(-1)) {
22982629e3Mike Christensen			(void) strcpy(serial_no, "Unknown");
23082629e3Mike Christensen		}
23182629e3Mike Christensen		(void) printf("%s%s\n", pflg ? "DOMAINCHASSIS|serialno=" :
23282629e3Mike Christensen		    gettext("Chassis serial#: "), serial_no);
23382629e3Mike Christensen	}
23482629e3Mike Christensen	return (0);
23582629e3Mike Christensen}