xref: /illumos-gate/usr/src/cmd/pools/pooladm/pooladm.c (revision ef150c2b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * pooladm - set, remove, or display active pool configurations.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/zone.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <priv.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <zone.h>
427c478bd9Sstevel@tonic-gate #include <pool.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include "utils.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
477c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static int Cflag;
517c478bd9Sstevel@tonic-gate static int Sflag;
527c478bd9Sstevel@tonic-gate static int Dflag;
537c478bd9Sstevel@tonic-gate static int Eflag;
547c478bd9Sstevel@tonic-gate static int Nflag;
557c478bd9Sstevel@tonic-gate static int Xflag;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static void
usage(void)587c478bd9Sstevel@tonic-gate usage(void)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
617c478bd9Sstevel@tonic-gate 	    gettext("Usage:\tpooladm [-n] [-s] [-c] [filename]\n"));
627c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
637c478bd9Sstevel@tonic-gate 	    gettext("Usage:\tpooladm [-n] -x\n"));
647c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
657c478bd9Sstevel@tonic-gate 	    gettext("Usage:\tpooladm -d | -e\n"));
667c478bd9Sstevel@tonic-gate 	exit(E_USAGE);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static void
config_print(pool_conf_t * conf)707c478bd9Sstevel@tonic-gate config_print(pool_conf_t *conf)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	char *buf;
737c478bd9Sstevel@tonic-gate 	pool_value_t *pv;
747c478bd9Sstevel@tonic-gate 	const char *tgt;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (pool_conf_open(conf, pool_dynamic_location(), PO_RDONLY)
777c478bd9Sstevel@tonic-gate 	    != PO_SUCCESS)
787c478bd9Sstevel@tonic-gate 		die(gettext(ERR_OPEN_DYNAMIC), get_errstr());
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if ((pv = pool_value_alloc()) == NULL ||
817c478bd9Sstevel@tonic-gate 	    pool_get_property(conf, pool_conf_to_elem(conf), "system.name",
827c478bd9Sstevel@tonic-gate 	    pv) == POC_INVAL ||
837c478bd9Sstevel@tonic-gate 	    pool_value_get_string(pv, &tgt) != PO_SUCCESS)
847c478bd9Sstevel@tonic-gate 		die(gettext(ERR_GET_ELEMENT_DETAILS),
857c478bd9Sstevel@tonic-gate 		    gettext(CONFIGURATION), "unknown", get_errstr());
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if ((buf = pool_conf_info(conf, PO_TRUE)) == NULL)
887c478bd9Sstevel@tonic-gate 		die(gettext(ERR_GET_ELEMENT_DETAILS), gettext(CONFIGURATION),
897c478bd9Sstevel@tonic-gate 		    tgt, get_errstr());
907c478bd9Sstevel@tonic-gate 	pool_value_free(pv);
917c478bd9Sstevel@tonic-gate 	(void) printf("%s", buf);
927c478bd9Sstevel@tonic-gate 	free(buf);
937c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(conf);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static void
config_destroy(pool_conf_t * conf)977c478bd9Sstevel@tonic-gate config_destroy(pool_conf_t *conf)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	if (pool_conf_open(conf, pool_dynamic_location(), PO_RDWR)
1007c478bd9Sstevel@tonic-gate 	    != PO_SUCCESS)
1017c478bd9Sstevel@tonic-gate 		die(gettext(ERR_OPEN_DYNAMIC), get_errstr());
1027c478bd9Sstevel@tonic-gate 	if (pool_conf_remove(conf) != PO_SUCCESS)
1037c478bd9Sstevel@tonic-gate 		die(gettext(ERR_REMOVE_DYNAMIC), get_errstr());
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static void
config_commit(pool_conf_t * conf,const char * static_conf_name)1077c478bd9Sstevel@tonic-gate config_commit(pool_conf_t *conf, const char *static_conf_name)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	if (pool_conf_open(conf, static_conf_name, Nflag || !Sflag ?
1107c478bd9Sstevel@tonic-gate 	    PO_RDONLY : PO_RDWR) != PO_SUCCESS)
1117c478bd9Sstevel@tonic-gate 		die(gettext(ERR_OPEN_STATIC), static_conf_name, get_errstr());
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (pool_conf_validate(conf, POV_RUNTIME) != PO_SUCCESS)
1147c478bd9Sstevel@tonic-gate 		die(gettext(ERR_VALIDATE_RUNTIME), static_conf_name);
1157c478bd9Sstevel@tonic-gate 	if (!Nflag) {
1167c478bd9Sstevel@tonic-gate 		if (pool_conf_commit(conf, PO_TRUE) != PO_SUCCESS)
1177c478bd9Sstevel@tonic-gate 			die(gettext(ERR_COMMIT_DYNAMIC), static_conf_name,
1187c478bd9Sstevel@tonic-gate 			    get_errstr());
1197c478bd9Sstevel@tonic-gate 		/*
1207c478bd9Sstevel@tonic-gate 		 * Dump the updated state to the specified location
1217c478bd9Sstevel@tonic-gate 		 */
1227c478bd9Sstevel@tonic-gate 		if (Sflag) {
1237c478bd9Sstevel@tonic-gate 			if (pool_conf_commit(conf, PO_FALSE) != PO_SUCCESS)
1247c478bd9Sstevel@tonic-gate 				die(gettext(ERR_COMMIT_STATIC),
1257c478bd9Sstevel@tonic-gate 				    static_conf_name, get_errstr());
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(conf);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1327c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1337c478bd9Sstevel@tonic-gate {
134*ef150c2bSRichard Lowe 	int c;
1357c478bd9Sstevel@tonic-gate 	pool_conf_t *conf = NULL;
1367c478bd9Sstevel@tonic-gate 	const char *static_conf_loc;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	(void) getpname(argv[0]);
1397c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1407c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "cdensx")) != EOF) {
1447c478bd9Sstevel@tonic-gate 		switch (c) {
1457c478bd9Sstevel@tonic-gate 		case 'c':	/* Create (or modify) system configuration */
1467c478bd9Sstevel@tonic-gate 			Cflag++;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case 'd':	/* Disable the pools facility */
1497c478bd9Sstevel@tonic-gate 			Dflag++;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		case 'e':	/* Enable the pools facility */
1527c478bd9Sstevel@tonic-gate 			Eflag++;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'n':	/* Don't actually do anything */
1557c478bd9Sstevel@tonic-gate 			Nflag++;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		case 's':	/* Update the submitted configuration */
1587c478bd9Sstevel@tonic-gate 			Sflag++;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		case 'x':	/* Delete current system configuration */
1617c478bd9Sstevel@tonic-gate 			Xflag++;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 		case '?':
1647c478bd9Sstevel@tonic-gate 		default:
1657c478bd9Sstevel@tonic-gate 			usage();
1667c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Not all flags can be used at the same time.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	if ((Cflag || Sflag || Dflag || Eflag) && Xflag)
1747c478bd9Sstevel@tonic-gate 		usage();
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if ((Dflag || Eflag) && (Cflag || Sflag || Xflag))
1777c478bd9Sstevel@tonic-gate 		usage();
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (Dflag && Eflag)
1807c478bd9Sstevel@tonic-gate 		usage();
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	argc -= optind;
1837c478bd9Sstevel@tonic-gate 	argv += optind;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (! (Cflag || Sflag)) {
1867c478bd9Sstevel@tonic-gate 		if (argc != 0)
1877c478bd9Sstevel@tonic-gate 			usage();
1887c478bd9Sstevel@tonic-gate 	} else {
1897c478bd9Sstevel@tonic-gate 		if (argc == 0)
1907c478bd9Sstevel@tonic-gate 			static_conf_loc = pool_static_location();
1917c478bd9Sstevel@tonic-gate 		else if (argc == 1)
1927c478bd9Sstevel@tonic-gate 			static_conf_loc = argv[0];
1937c478bd9Sstevel@tonic-gate 		else
1947c478bd9Sstevel@tonic-gate 			usage();
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (!Nflag && (Cflag + Dflag + Eflag + Xflag != 0) &&
1987c478bd9Sstevel@tonic-gate 	    !priv_ineffect(PRIV_SYS_RES_CONFIG))
1997c478bd9Sstevel@tonic-gate 		die(gettext(ERR_PERMISSIONS));
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (Dflag) {
2027c478bd9Sstevel@tonic-gate 		if (pool_set_status(POOL_DISABLED) != PO_SUCCESS)
2037c478bd9Sstevel@tonic-gate 			die(gettext(ERR_DISABLE));
2047c478bd9Sstevel@tonic-gate 	} else if (Eflag) {
2057c478bd9Sstevel@tonic-gate 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS) {
2067c478bd9Sstevel@tonic-gate 			if (errno == EEXIST)
2077c478bd9Sstevel@tonic-gate 				die(gettext(ERR_ENABLE
2087c478bd9Sstevel@tonic-gate 				    ": System has active processor sets\n"));
2097c478bd9Sstevel@tonic-gate 			else
2107c478bd9Sstevel@tonic-gate 				die(gettext(ERR_ENABLE));
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 	} else {
2137c478bd9Sstevel@tonic-gate 		if ((conf = pool_conf_alloc()) == NULL)
2147c478bd9Sstevel@tonic-gate 			die(gettext(ERR_NOMEM));
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		if (Cflag + Sflag + Xflag == 0) {
2177c478bd9Sstevel@tonic-gate 			/*
2187c478bd9Sstevel@tonic-gate 			 * No flags means print current system configuration
2197c478bd9Sstevel@tonic-gate 			 */
2207c478bd9Sstevel@tonic-gate 			config_print(conf);
2217c478bd9Sstevel@tonic-gate 		} else if (!Nflag && Xflag) {
2227c478bd9Sstevel@tonic-gate 			/*
2237c478bd9Sstevel@tonic-gate 			 * Destroy active pools configuration and
2247c478bd9Sstevel@tonic-gate 			 * remove the state file.
2257c478bd9Sstevel@tonic-gate 			 */
2267c478bd9Sstevel@tonic-gate 			config_destroy(conf);
2277c478bd9Sstevel@tonic-gate 		} else {
2287c478bd9Sstevel@tonic-gate 			/*
2297c478bd9Sstevel@tonic-gate 			 * Commit a new configuration.
2307c478bd9Sstevel@tonic-gate 			 */
2317c478bd9Sstevel@tonic-gate 			if (Cflag)
2327c478bd9Sstevel@tonic-gate 				config_commit(conf, static_conf_loc);
2337c478bd9Sstevel@tonic-gate 			else {
2347c478bd9Sstevel@tonic-gate 				/*
2357c478bd9Sstevel@tonic-gate 				 * Dump the dynamic state to the
2367c478bd9Sstevel@tonic-gate 				 * specified location
2377c478bd9Sstevel@tonic-gate 				 */
2387c478bd9Sstevel@tonic-gate 				if (!Nflag && Sflag) {
2397c478bd9Sstevel@tonic-gate 					if (pool_conf_open(conf,
2407c478bd9Sstevel@tonic-gate 					    pool_dynamic_location(), PO_RDONLY)
2417c478bd9Sstevel@tonic-gate 					!= PO_SUCCESS)
2427c478bd9Sstevel@tonic-gate 						die(gettext(ERR_OPEN_DYNAMIC),
2437c478bd9Sstevel@tonic-gate 						get_errstr());
2447c478bd9Sstevel@tonic-gate 					if (pool_conf_export(conf,
2457c478bd9Sstevel@tonic-gate 					    static_conf_loc, POX_NATIVE) !=
2467c478bd9Sstevel@tonic-gate 					    PO_SUCCESS)
2477c478bd9Sstevel@tonic-gate 						die(gettext(ERR_EXPORT_DYNAMIC),
2487c478bd9Sstevel@tonic-gate 						static_conf_loc, get_errstr());
2497c478bd9Sstevel@tonic-gate 					(void) pool_conf_close(conf);
2507c478bd9Sstevel@tonic-gate 				}
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		pool_conf_free(conf);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	return (E_PO_SUCCESS);
2567c478bd9Sstevel@tonic-gate }
257