xref: /onnv-gate/usr/src/cmd/lvm/util/metareplace.c (revision 1623:7bac4a816ebe)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1623Stw21770  * Common Development and Distribution License (the "License").
6*1623Stw21770  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1623Stw21770  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * replace mirror component
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <meta.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sdssc.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * print usage message
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)400Sstevel@tonic-gate usage(
410Sstevel@tonic-gate 	mdsetname_t	*sp,
420Sstevel@tonic-gate 	int		eval
430Sstevel@tonic-gate )
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
460Sstevel@tonic-gate usage:	%s [-s setname] mirror component-old component-new\n\
470Sstevel@tonic-gate 	%s [-s setname] -e mirror component\n\
480Sstevel@tonic-gate 	%s [-s setname] [-f] RAID component-old component-new\n\
49*1623Stw21770 	%s [-s setname] [-f] -e RAID component\n"),
50*1623Stw21770 	    myname, myname, myname, myname);
510Sstevel@tonic-gate 	md_exit(sp, eval);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * online replace a physical disk in a metamirror
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate int
main(int argc,char * argv[])580Sstevel@tonic-gate main(
590Sstevel@tonic-gate 	int		argc,
600Sstevel@tonic-gate 	char		*argv[]
610Sstevel@tonic-gate )
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	char		*sname = NULL;
640Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
650Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT|MDCMD_DOIT);
660Sstevel@tonic-gate 	mdname_t	*namep;
670Sstevel@tonic-gate 	int		eflag = 0;
680Sstevel@tonic-gate 	int		c;
690Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
700Sstevel@tonic-gate 	md_error_t	*ep = &status;
710Sstevel@tonic-gate 	int		error;
720Sstevel@tonic-gate 	char		*uname = NULL;
730Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
740Sstevel@tonic-gate 	char		*cp;
750Sstevel@tonic-gate 	int		origargc = argc;
760Sstevel@tonic-gate 	char		**origargv = argv;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/*
790Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
800Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
810Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
820Sstevel@tonic-gate 	 * something.
830Sstevel@tonic-gate 	 */
840Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
850Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
880Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
920Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
930Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
940Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
950Sstevel@tonic-gate 				exit(error);
960Sstevel@tonic-gate 	} else {
970Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
980Sstevel@tonic-gate 		called_thru_rpc = TRUE;
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/* initialize */
1020Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
1030Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
1040Sstevel@tonic-gate 		mde_perror(ep, "");
1050Sstevel@tonic-gate 		md_exit(sp, 1);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	/* parse arguments */
1090Sstevel@tonic-gate 	optind = 1;
1100Sstevel@tonic-gate 	opterr = 1;
111*1623Stw21770 	while ((c = getopt(argc, argv, "hs:efn?")) != -1) {
1120Sstevel@tonic-gate 		switch (c) {
1130Sstevel@tonic-gate 		case 'h':
1140Sstevel@tonic-gate 			usage(sp, 0);
1150Sstevel@tonic-gate 			break;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		case 's':
1180Sstevel@tonic-gate 			sname = optarg;
1190Sstevel@tonic-gate 			break;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		case 'e':
1220Sstevel@tonic-gate 			++eflag;
1230Sstevel@tonic-gate 			break;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		case 'f':
1260Sstevel@tonic-gate 			options |= MDCMD_FORCE;
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		case 'n':
1300Sstevel@tonic-gate 			if (called_thru_rpc == TRUE) {
1310Sstevel@tonic-gate 				options &= ~MDCMD_DOIT;
1320Sstevel@tonic-gate 			} else {
1330Sstevel@tonic-gate 				usage(sp, 1);
1340Sstevel@tonic-gate 			}
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		case '?':
1380Sstevel@tonic-gate 			if (optopt == '?')
1390Sstevel@tonic-gate 				usage(sp, 0);
1400Sstevel@tonic-gate 			/*FALLTHROUGH*/
1410Sstevel@tonic-gate 		default:
1420Sstevel@tonic-gate 			usage(sp, 1);
1430Sstevel@tonic-gate 			break;
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	argc -= optind;
1470Sstevel@tonic-gate 	argv += optind;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (sname != NULL) {
1500Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
1510Sstevel@tonic-gate 			mde_perror(ep, "");
1520Sstevel@tonic-gate 			md_exit(sp, 1);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/* get device */
1570Sstevel@tonic-gate 	if (argc < 1)
1580Sstevel@tonic-gate 		usage(sp, 1);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	uname = argv[0];
1610Sstevel@tonic-gate 
162*1623Stw21770 	if (((namep = metaname(&sp, uname, META_DEVICE, ep)) == NULL)) {
1630Sstevel@tonic-gate 		mde_perror(ep, "");
1640Sstevel@tonic-gate 		md_exit(sp, 1);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
167*1623Stw21770 	if (metachkmeta(namep, ep) != 0) {
168*1623Stw21770 		mde_perror(ep, "");
169*1623Stw21770 		md_exit(sp, 1);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	assert(sp != NULL);
1730Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
1740Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[0], ep)) {
1750Sstevel@tonic-gate 		/*
1760Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
1770Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
1780Sstevel@tonic-gate 		 * command string to the master of the set and let it
1790Sstevel@tonic-gate 		 * deal with it.
1800Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
1810Sstevel@tonic-gate 		 * from argv[0] which is the metadevice arg
1820Sstevel@tonic-gate 		 */
1830Sstevel@tonic-gate 		int  i;
1840Sstevel@tonic-gate 		int  newargc;
1850Sstevel@tonic-gate 		int  result;
1860Sstevel@tonic-gate 		char *miscname;
1870Sstevel@tonic-gate 		char **newargv;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		if ((miscname = metagetmiscname(namep, ep)) == NULL) {
1900Sstevel@tonic-gate 			mde_perror(ep, "");
1910Sstevel@tonic-gate 			md_exit(sp, 1);
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		newargv = calloc(origargc+1, sizeof (char *));
1950Sstevel@tonic-gate 		newargv[0] = "metareplace";
1960Sstevel@tonic-gate 		newargv[1] = "-n"; /* always do "-n" first */
1970Sstevel@tonic-gate 		newargc = 2;
1980Sstevel@tonic-gate 		for (i = 1; i < origargc; i++, newargc++) {
1990Sstevel@tonic-gate 			newargv[newargc] = origargv[i];
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
2030Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_DRYRUN, NO_CONTEXT_STRING, ep);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		/* If we've found a problem don't do it for real */
2060Sstevel@tonic-gate 		if (result != 0) {
2070Sstevel@tonic-gate 			md_exit(sp, result);
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 		/*
2100Sstevel@tonic-gate 		 * Do it for real now. Remove "-n" from the arguments and
2110Sstevel@tonic-gate 		 * MD_DRYRUN from the flags. If this fails, the master must
2120Sstevel@tonic-gate 		 * panic as the mddbs may be inconsistent.
2130Sstevel@tonic-gate 		 */
2140Sstevel@tonic-gate 		newargv[1] = ""; /* this was "-n" before */
2150Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
2160Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_RETRY_BUSY | MD_PANIC_WHEN_INCONSISTENT,
2170Sstevel@tonic-gate 		    NO_CONTEXT_STRING, ep);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		free(newargv);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		/*
2220Sstevel@tonic-gate 		 * if the metareplace command succeeds for a mirror, send a
2230Sstevel@tonic-gate 		 * resync starting message for the metadevice
2240Sstevel@tonic-gate 		 */
2250Sstevel@tonic-gate 		if ((result == 0) && (strcmp(miscname, MD_MIRROR) == 0)) {
2260Sstevel@tonic-gate 			if ((result = meta_mn_send_resync_starting(namep, ep))
2270Sstevel@tonic-gate 			    != 0)
2280Sstevel@tonic-gate 				mde_perror(ep, "Unable to start resync");
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 		md_exit(sp, result);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	--argc, ++argv;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/* grab set lock */
2360Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
2370Sstevel@tonic-gate 		mde_perror(ep, "");
2380Sstevel@tonic-gate 		md_exit(sp, 1);
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* check for ownership */
2420Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
2430Sstevel@tonic-gate 		mde_perror(ep, "");
2440Sstevel@tonic-gate 		md_exit(sp, 1);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (eflag) {				/* enable component */
2480Sstevel@tonic-gate 		mdname_t	*compnp;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		if (argc != 1)
2510Sstevel@tonic-gate 			usage(sp, 1);
2520Sstevel@tonic-gate 
253*1623Stw21770 		if ((compnp = metaname(&sp, argv[0], UNKNOWN, ep)) == NULL) {
2540Sstevel@tonic-gate 			mde_perror(ep, "");
2550Sstevel@tonic-gate 			md_exit(sp, 1);
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 		if (meta_enable_byname(sp, namep, compnp, options, ep)
2580Sstevel@tonic-gate 		    != 0) {
2590Sstevel@tonic-gate 			mde_perror(ep, "");
2600Sstevel@tonic-gate 			md_exit(sp, 1);
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 	} else {				/* replace component */
2630Sstevel@tonic-gate 		mdname_t	*oldnp;
2640Sstevel@tonic-gate 		mdname_t	*newnp;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		if (argc != 2)
2670Sstevel@tonic-gate 			usage(sp, 1);
2680Sstevel@tonic-gate 
269*1623Stw21770 		if ((oldnp = metaname(&sp, argv[0], UNKNOWN, ep)) == NULL) {
2700Sstevel@tonic-gate 			mde_perror(ep, "");
2710Sstevel@tonic-gate 			md_exit(sp, 1);
2720Sstevel@tonic-gate 		}
273*1623Stw21770 		if ((newnp = metaname(&sp, argv[1], UNKNOWN, ep)) == NULL) {
274*1623Stw21770 			mde_perror(ep, "");
275*1623Stw21770 			md_exit(sp, 1);
276*1623Stw21770 		}
277*1623Stw21770 		if (meta_replace_byname(sp, namep, oldnp, newnp,
278*1623Stw21770 		    options, ep) != 0) {
279*1623Stw21770 			mde_perror(ep, "");
280*1623Stw21770 			md_exit(sp, 1);
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/* update md.cf */
2850Sstevel@tonic-gate 	if (meta_update_md_cf(sp, ep) != 0) {
2860Sstevel@tonic-gate 		mde_perror(ep, "");
2870Sstevel@tonic-gate 		md_exit(sp, 1);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	md_exit(sp, 0);
2910Sstevel@tonic-gate 	/*NOTREACHED*/
2920Sstevel@tonic-gate 	return (0);
2930Sstevel@tonic-gate }
294