xref: /titanic_41/usr/src/cmd/lvm/util/metarecover.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Recover metadevice configurations that have been lost by scanning
31  * media, intelligent guessing, or other means.
32  */
33 
34 #include <meta.h>
35 #include <sdssc.h>
36 
37 /*
38  * print usage message
39  */
40 static void
41 usage(
42 	mdsetname_t	*sp,
43 	int		eval
44 )
45 {
46 	(void) fprintf(stderr, gettext(
47 	    "usage: %s [-s setname] [-v] raw-device -p\n"), myname);
48 	(void) fprintf(stderr, gettext(
49 	    "       %s [-s setname] [-v] [-n] raw-device -p -d\n"), myname);
50 	(void) fprintf(stderr, gettext(
51 	    "       %s [-s setname] [-v] [-n] raw-device -p -m\n"), myname);
52 
53 	md_exit(sp, eval);
54 }
55 
56 int
57 main(
58 	int	argc,
59 	char	*argv[]
60 )
61 {
62 	char		*sname = MD_LOCAL_NAME;
63 	mdcmdopts_t	options = (MDCMD_DOIT | MDCMD_PRINT);
64 
65 	mdsetname_t	*sp = NULL;
66 	md_error_t	status = mdnullerror;
67 	md_error_t	*ep = &status;
68 	mdname_t	*namep;
69 	char		*devname;
70 	int		error;
71 	int		c;
72 
73 	/*
74 	 * Get the locale set up before calling any other routines
75 	 * with messages to ouput.  Just in case we're not in a build
76 	 * environment, make sure that TEXT_DOMAIN gets set to
77 	 * something.
78 	 */
79 #if !defined(TEXT_DOMAIN)
80 #define	TEXT_DOMAIN "SYS_TEST"
81 #endif
82 	(void) setlocale(LC_ALL, "");
83 	(void) textdomain(TEXT_DOMAIN);
84 
85 	if (sdssc_bind_library() == SDSSC_ERROR) {
86 		printf(gettext(
87 		    "%s: Interface error with libsds_sc.so\n"), argv[0]);
88 		exit(1);
89 	}
90 
91 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
92 			meta_check_root(ep) != 0) {
93 		mde_perror(ep, "");
94 		md_exit((mdsetname_t *)NULL, 1);
95 	}
96 
97 	/* parse arguments */
98 	optind = 1;
99 	opterr = 1;
100 	while ((c = getopt(argc, argv, "s:hnv?")) != -1) {
101 		switch (c) {
102 		case 's':
103 			sname = optarg;
104 			break;
105 
106 		case 'h':
107 			usage(sp, 0);
108 			break;
109 
110 		case 'v':
111 			options |= MDCMD_VERBOSE;
112 			break;
113 
114 		case 'n':
115 			options &= ~MDCMD_DOIT;
116 			break;
117 
118 		case '?':
119 			if (optopt == '?')
120 				usage(sp, 0);
121 			/*FALLTHROUGH*/
122 		default:
123 			usage(sp, 1);
124 			break;
125 		}
126 	}
127 	argc -= optind;
128 	argv += optind;
129 
130 	/* sname is MD_LOCAL_NAME if not specified on the command line */
131 	if ((sp = metasetname(sname, ep)) == NULL) {
132 		mde_perror(ep, "");
133 		md_exit(sp, 1);
134 	}
135 
136 	if ((argc == 0) || (argv[0] == NULL)) {
137 		usage(sp, 1);
138 	}
139 
140 	/* get raw device name */
141 	devname = Strdup(argv[0]);
142 	argv++;
143 	argc--;
144 
145 	if ((namep = metaname(&sp, devname, ep)) == NULL) {
146 		mde_perror(ep, "");
147 		md_exit(sp, 1);
148 	}
149 
150 	/* check for a valid component */
151 	if ((metagetsize(namep, ep) == MD_DISKADDR_ERROR)) {
152 		mde_perror(ep, "");
153 		md_exit(sp, 1);
154 	}
155 
156 	/* check for ownership */
157 	assert(sp != NULL);
158 	if (meta_check_ownership(sp, ep) != 0) {
159 		mde_perror(ep, "");
160 		md_exit(sp, 1);
161 	}
162 
163 	/*
164 	 * If the component is not a metadevice and we have a named set
165 	 * make sure that the component is part of the named set.
166 	 */
167 	if (strcmp(sp->setname, MD_LOCAL_NAME) != 0) {
168 		if (!metaismeta(namep)) {
169 			if (! meta_is_drive_in_thisset(sp, namep->drivenamep,
170 			    FALSE, ep)) {
171 				mddeverror(ep, MDE_NOT_IN_SET, namep->dev,
172 				    namep->cname);
173 				mde_perror(ep, "");
174 				md_exit(sp, 1);
175 			}
176 		}
177 	}
178 
179 	/* parse command line -- currently only soft partitions are supported */
180 	if ((argc > 0) && (*argv != NULL) && strncmp(*argv, "-p", 2) == 0) {
181 		error = meta_recover_sp(sp, namep, --argc, ++argv, options, ep);
182 	} else {
183 		usage(sp, 1);
184 	}
185 
186 	if (error < 0) {
187 		mde_perror(ep, "");
188 		md_exit(sp, 1);
189 	} else {
190 		if (meta_update_md_cf(sp, ep) != 0) {
191 			mde_perror(ep, "");
192 			md_exit(sp, 1);
193 		}
194 	}
195 
196 	md_exit(sp, 0);
197 	/*NOTREACHED*/
198 	return (0);
199 }
200