xref: /titanic_41/usr/src/cmd/lvm/util/metarecover.c (revision 80148899834a4078a2bd348504aa2d6de9752837)
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
5d7cd8252Stw21770  * Common Development and Distribution License (the "License").
6d7cd8252Stw21770  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*80148899SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Recover metadevice configurations that have been lost by scanning
287c478bd9Sstevel@tonic-gate  * media, intelligent guessing, or other means.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <meta.h>
327c478bd9Sstevel@tonic-gate #include <sdssc.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * print usage message
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)387c478bd9Sstevel@tonic-gate usage(
397c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
407c478bd9Sstevel@tonic-gate 	int		eval
417c478bd9Sstevel@tonic-gate )
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
447c478bd9Sstevel@tonic-gate 	    "usage: %s [-s setname] [-v] raw-device -p\n"), myname);
457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
467c478bd9Sstevel@tonic-gate 	    "       %s [-s setname] [-v] [-n] raw-device -p -d\n"), myname);
477c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
487c478bd9Sstevel@tonic-gate 	    "       %s [-s setname] [-v] [-n] raw-device -p -m\n"), myname);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	md_exit(sp, eval);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])547c478bd9Sstevel@tonic-gate main(
557c478bd9Sstevel@tonic-gate 	int	argc,
567c478bd9Sstevel@tonic-gate 	char	*argv[]
577c478bd9Sstevel@tonic-gate )
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
607c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_DOIT | MDCMD_PRINT);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
637c478bd9Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
647c478bd9Sstevel@tonic-gate 	md_error_t	*ep = &status;
657c478bd9Sstevel@tonic-gate 	mdname_t	*namep;
667c478bd9Sstevel@tonic-gate 	char		*devname;
677c478bd9Sstevel@tonic-gate 	int		error;
687c478bd9Sstevel@tonic-gate 	int		c;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	/*
717c478bd9Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
727c478bd9Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
737c478bd9Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
747c478bd9Sstevel@tonic-gate 	 * something.
757c478bd9Sstevel@tonic-gate 	 */
767c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
777c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
807c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (sdssc_bind_library() == SDSSC_ERROR) {
83*80148899SSurya Prakki 		(void) printf(gettext(
847c478bd9Sstevel@tonic-gate 		    "%s: Interface error with libsds_sc.so\n"), argv[0]);
857c478bd9Sstevel@tonic-gate 		exit(1);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
897c478bd9Sstevel@tonic-gate 	    meta_check_root(ep) != 0) {
907c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
917c478bd9Sstevel@tonic-gate 		md_exit((mdsetname_t *)NULL, 1);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/* parse arguments */
957c478bd9Sstevel@tonic-gate 	optind = 1;
967c478bd9Sstevel@tonic-gate 	opterr = 1;
977c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:hnv?")) != -1) {
987c478bd9Sstevel@tonic-gate 		switch (c) {
997c478bd9Sstevel@tonic-gate 		case 's':
1007c478bd9Sstevel@tonic-gate 			sname = optarg;
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		case 'h':
1047c478bd9Sstevel@tonic-gate 			usage(sp, 0);
1057c478bd9Sstevel@tonic-gate 			break;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		case 'v':
1087c478bd9Sstevel@tonic-gate 			options |= MDCMD_VERBOSE;
1097c478bd9Sstevel@tonic-gate 			break;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		case 'n':
1127c478bd9Sstevel@tonic-gate 			options &= ~MDCMD_DOIT;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		case '?':
1167c478bd9Sstevel@tonic-gate 			if (optopt == '?')
1177c478bd9Sstevel@tonic-gate 				usage(sp, 0);
1187c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1197c478bd9Sstevel@tonic-gate 		default:
1207c478bd9Sstevel@tonic-gate 			usage(sp, 1);
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	argc -= optind;
1257c478bd9Sstevel@tonic-gate 	argv += optind;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* sname is MD_LOCAL_NAME if not specified on the command line */
1287c478bd9Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
1297c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
1307c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if ((argc == 0) || (argv[0] == NULL)) {
1347c478bd9Sstevel@tonic-gate 		usage(sp, 1);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* get raw device name */
1387c478bd9Sstevel@tonic-gate 	devname = Strdup(argv[0]);
1397c478bd9Sstevel@tonic-gate 	argv++;
1407c478bd9Sstevel@tonic-gate 	argc--;
1417c478bd9Sstevel@tonic-gate 
142d7cd8252Stw21770 	if ((namep = metaname(&sp, devname, UNKNOWN, ep)) == NULL) {
1437c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
1447c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* check for a valid component */
1487c478bd9Sstevel@tonic-gate 	if ((metagetsize(namep, ep) == MD_DISKADDR_ERROR)) {
1497c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
1507c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* check for ownership */
1547c478bd9Sstevel@tonic-gate 	assert(sp != NULL);
1557c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
1567c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
1577c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * If the component is not a metadevice and we have a named set
1627c478bd9Sstevel@tonic-gate 	 * make sure that the component is part of the named set.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	if (strcmp(sp->setname, MD_LOCAL_NAME) != 0) {
1657c478bd9Sstevel@tonic-gate 		if (!metaismeta(namep)) {
1667c478bd9Sstevel@tonic-gate 			if (! meta_is_drive_in_thisset(sp, namep->drivenamep,
1677c478bd9Sstevel@tonic-gate 			    FALSE, ep)) {
168*80148899SSurya Prakki 				(void) mddeverror(ep, MDE_NOT_IN_SET,
169*80148899SSurya Prakki 				    namep->dev, namep->cname);
1707c478bd9Sstevel@tonic-gate 				mde_perror(ep, "");
1717c478bd9Sstevel@tonic-gate 				md_exit(sp, 1);
1727c478bd9Sstevel@tonic-gate 			}
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/* parse command line -- currently only soft partitions are supported */
1777c478bd9Sstevel@tonic-gate 	if ((argc > 0) && (*argv != NULL) && strncmp(*argv, "-p", 2) == 0) {
1787c478bd9Sstevel@tonic-gate 		error = meta_recover_sp(sp, namep, --argc, ++argv, options, ep);
1797c478bd9Sstevel@tonic-gate 	} else {
1807c478bd9Sstevel@tonic-gate 		usage(sp, 1);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (error < 0) {
1847c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
1857c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
1867c478bd9Sstevel@tonic-gate 	} else {
1877c478bd9Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0) {
1887c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
1897c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	md_exit(sp, 0);
1947c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1957c478bd9Sstevel@tonic-gate 	return (0);
1967c478bd9Sstevel@tonic-gate }
197