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