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