xref: /onnv-gate/usr/src/cmd/lvm/util/metarecover.c (revision 11053:f33a1c7f3155)
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
51623Stw21770  * Common Development and Distribution License (the "License").
61623Stw21770  * 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*11053SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Recover metadevice configurations that have been lost by scanning
280Sstevel@tonic-gate  * media, intelligent guessing, or other means.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <meta.h>
320Sstevel@tonic-gate #include <sdssc.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * print usage message
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)380Sstevel@tonic-gate usage(
390Sstevel@tonic-gate 	mdsetname_t	*sp,
400Sstevel@tonic-gate 	int		eval
410Sstevel@tonic-gate )
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
440Sstevel@tonic-gate 	    "usage: %s [-s setname] [-v] raw-device -p\n"), myname);
450Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
460Sstevel@tonic-gate 	    "       %s [-s setname] [-v] [-n] raw-device -p -d\n"), myname);
470Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
480Sstevel@tonic-gate 	    "       %s [-s setname] [-v] [-n] raw-device -p -m\n"), myname);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	md_exit(sp, eval);
510Sstevel@tonic-gate }
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int
main(int argc,char * argv[])540Sstevel@tonic-gate main(
550Sstevel@tonic-gate 	int	argc,
560Sstevel@tonic-gate 	char	*argv[]
570Sstevel@tonic-gate )
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
600Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_DOIT | MDCMD_PRINT);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
630Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
640Sstevel@tonic-gate 	md_error_t	*ep = &status;
650Sstevel@tonic-gate 	mdname_t	*namep;
660Sstevel@tonic-gate 	char		*devname;
670Sstevel@tonic-gate 	int		error;
680Sstevel@tonic-gate 	int		c;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/*
710Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
720Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
730Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
740Sstevel@tonic-gate 	 * something.
750Sstevel@tonic-gate 	 */
760Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
770Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
800Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (sdssc_bind_library() == SDSSC_ERROR) {
83*11053SSurya.Prakki@Sun.COM 		(void) printf(gettext(
840Sstevel@tonic-gate 		    "%s: Interface error with libsds_sc.so\n"), argv[0]);
850Sstevel@tonic-gate 		exit(1);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
89*11053SSurya.Prakki@Sun.COM 	    meta_check_root(ep) != 0) {
900Sstevel@tonic-gate 		mde_perror(ep, "");
910Sstevel@tonic-gate 		md_exit((mdsetname_t *)NULL, 1);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/* parse arguments */
950Sstevel@tonic-gate 	optind = 1;
960Sstevel@tonic-gate 	opterr = 1;
970Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:hnv?")) != -1) {
980Sstevel@tonic-gate 		switch (c) {
990Sstevel@tonic-gate 		case 's':
1000Sstevel@tonic-gate 			sname = optarg;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		case 'h':
1040Sstevel@tonic-gate 			usage(sp, 0);
1050Sstevel@tonic-gate 			break;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		case 'v':
1080Sstevel@tonic-gate 			options |= MDCMD_VERBOSE;
1090Sstevel@tonic-gate 			break;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		case 'n':
1120Sstevel@tonic-gate 			options &= ~MDCMD_DOIT;
1130Sstevel@tonic-gate 			break;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		case '?':
1160Sstevel@tonic-gate 			if (optopt == '?')
1170Sstevel@tonic-gate 				usage(sp, 0);
1180Sstevel@tonic-gate 			/*FALLTHROUGH*/
1190Sstevel@tonic-gate 		default:
1200Sstevel@tonic-gate 			usage(sp, 1);
1210Sstevel@tonic-gate 			break;
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 	argc -= optind;
1250Sstevel@tonic-gate 	argv += optind;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/* sname is MD_LOCAL_NAME if not specified on the command line */
1280Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
1290Sstevel@tonic-gate 		mde_perror(ep, "");
1300Sstevel@tonic-gate 		md_exit(sp, 1);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if ((argc == 0) || (argv[0] == NULL)) {
1340Sstevel@tonic-gate 		usage(sp, 1);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/* get raw device name */
1380Sstevel@tonic-gate 	devname = Strdup(argv[0]);
1390Sstevel@tonic-gate 	argv++;
1400Sstevel@tonic-gate 	argc--;
1410Sstevel@tonic-gate 
1421623Stw21770 	if ((namep = metaname(&sp, devname, UNKNOWN, ep)) == NULL) {
1430Sstevel@tonic-gate 		mde_perror(ep, "");
1440Sstevel@tonic-gate 		md_exit(sp, 1);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/* check for a valid component */
1480Sstevel@tonic-gate 	if ((metagetsize(namep, ep) == MD_DISKADDR_ERROR)) {
1490Sstevel@tonic-gate 		mde_perror(ep, "");
1500Sstevel@tonic-gate 		md_exit(sp, 1);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* check for ownership */
1540Sstevel@tonic-gate 	assert(sp != NULL);
1550Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
1560Sstevel@tonic-gate 		mde_perror(ep, "");
1570Sstevel@tonic-gate 		md_exit(sp, 1);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * If the component is not a metadevice and we have a named set
1620Sstevel@tonic-gate 	 * make sure that the component is part of the named set.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	if (strcmp(sp->setname, MD_LOCAL_NAME) != 0) {
1650Sstevel@tonic-gate 		if (!metaismeta(namep)) {
1660Sstevel@tonic-gate 			if (! meta_is_drive_in_thisset(sp, namep->drivenamep,
1670Sstevel@tonic-gate 			    FALSE, ep)) {
168*11053SSurya.Prakki@Sun.COM 				(void) mddeverror(ep, MDE_NOT_IN_SET,
169*11053SSurya.Prakki@Sun.COM 				    namep->dev, namep->cname);
1700Sstevel@tonic-gate 				mde_perror(ep, "");
1710Sstevel@tonic-gate 				md_exit(sp, 1);
1720Sstevel@tonic-gate 			}
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/* parse command line -- currently only soft partitions are supported */
1770Sstevel@tonic-gate 	if ((argc > 0) && (*argv != NULL) && strncmp(*argv, "-p", 2) == 0) {
1780Sstevel@tonic-gate 		error = meta_recover_sp(sp, namep, --argc, ++argv, options, ep);
1790Sstevel@tonic-gate 	} else {
1800Sstevel@tonic-gate 		usage(sp, 1);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (error < 0) {
1840Sstevel@tonic-gate 		mde_perror(ep, "");
1850Sstevel@tonic-gate 		md_exit(sp, 1);
1860Sstevel@tonic-gate 	} else {
1870Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0) {
1880Sstevel@tonic-gate 			mde_perror(ep, "");
1890Sstevel@tonic-gate 			md_exit(sp, 1);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	md_exit(sp, 0);
1940Sstevel@tonic-gate 	/*NOTREACHED*/
1950Sstevel@tonic-gate 	return (0);
1960Sstevel@tonic-gate }
197