1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <ctype.h>
#include <malloc.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <limits.h>
#include <meta.h>
#include <svm.h>
#include <libsvm.h>
#define MODEBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
#define ISREG(A) (((A).st_mode & S_IFMT) == S_IFREG)
#define DEFAULT_ROOTDIR "/a"
/*
* FUNCTION: svm_start
* starts SDS/SVM configuration. If root mirroring exists then the
* components of the root mirror are returned in svmpp.
*
* INPUT: mntpnt - root mount point
* svmpp - prealloced structure to return components
* repl_state_flag - SVM_CONV/SVM_DONT_CONV
*
* RETURN:
* 0 - SUCCESS
* !0 - ERROR
* if > 0 errno
*/
int
svm_start(char *mntpnt, svm_info_t **svmpp, int repl_state_flag)
{
char *rootdir, *tf;
char *mdevnamep = NULL;
char system_file[PATH_MAX];
char mdconf[PATH_MAX];
int rval = 0;
if (mntpnt == NULL)
rootdir = DEFAULT_ROOTDIR;
else
rootdir = mntpnt;
if ((rval = snprintf(system_file, PATH_MAX, "%s%s",
rootdir, SYSTEM_FILE)) < 0) {
return (RET_ERROR);
}
if ((rval = snprintf(mdconf, PATH_MAX, "%s%s",
rootdir, MD_CONF)) < 0) {
return (RET_ERROR);
}
debug_printf("svm_start(): repl_state_flag %s\n",
(repl_state_flag == SVM_DONT_CONV) ? "SVM_DONT_CONV":
"SVM_CONV");
if (copyfile(MD_CONF, MD_CONF_ORIG))
return (RET_ERROR);
switch (rval = convert_bootlist(system_file, mdconf, &tf)) {
case 0:
case -1: /* found in etc/system flag */
break;
default: /* convert bootlist failed */
debug_printf("svm_start(): convert_bootlist failed."
"rval %d\n", rval);
goto errout;
}
if (repl_state_flag == SVM_DONT_CONV) {
rval = create_in_file_prop(PROP_KEEP_REPL_STATE, tf);
if (rval != 0)
goto errout;
}
if (is_upgrade_prop(PROP_DEVID_DESTROY)) {
rval = create_in_file_prop(PROP_DEVID_DESTROY, tf);
/*
* For the idempotent behavior reset internal
* flag incase we have to return due to errors
*/
set_upgrade_prop(PROP_DEVID_DESTROY, 0);
if (rval != 0)
goto errout;
}
/*
* Since svm_start is called only after svm_check,
* we can assume that there is a valid metadb. If the mddb_bootlist
* is not found in etc/system, then it must be in md.conf which
* we copied to temporary file pointed to by tf
*/
if (copyfile(tf, MD_CONF)) {
debug_printf("svm_start(): copy of %s to %s failed\n", tf,
MD_CONF);
goto errout;
}
if ((rval = write_xlate_to_mdconf(rootdir)) != 0) {
debug_printf("svm_start(): write_xlate_to_mdconf(%s) failed\n",
rootdir);
goto errout;
}
if ((rval = write_targ_nm_table(rootdir)) != 0) {
goto errout;
}
/* run devfsadm to create the devices specified in md.conf */
if ((rval = system("/usr/sbin/devfsadm -r /tmp -p "
"/tmp/root/etc/path_to_inst -i md")) != 0) {
debug_printf("svm_start(): devfsadm -i md failed: %d\n", rval);
goto errout;
}
/*
* We have to unload md after the devfsadm run so that when metainit
* loads things it gets the right information from md.conf.
*/
if (rval = svm_stop()) {
debug_printf("svm_start(): svm_stop failed.\n");
return (RET_ERROR);
}
if ((rval = system("/usr/sbin/metainit -r")) != 0) {
debug_printf("svm_start(): metainit -r failed: %d\n", rval);
goto errout;
}
create_diskset_links();
if ((rval = system("/usr/sbin/metasync -r")) != 0) {
debug_printf("svm_start(): metasync -r failed: %d\n", rval);
goto errout;
}
/*
* We ignore failures from metadevadm, since it can fail if
* miniroot dev_t's don't match target dev_ts. But it still
* will update md.conf with device Id information which is
* why we are calling it here.
*/
(void) system("/usr/sbin/metadevadm -r");
/*
* check to see if we have a root metadevice and if so
* get its components.
*/
if ((rval = get_rootmetadevice(rootdir, &mdevnamep)) == 0) {
if (rval = get_mdcomponents(mdevnamep, svmpp)) {
debug_printf("svm_start(): get_mdcomponents(%s,..)"
"failed %d\n", mdevnamep, rval);
goto errout;
}
} else {
rval = 0; /* not a mirrored root */
debug_printf("svm_start(): get_rootmetadevice(%s,..) "
"No root mirrors! ", rootdir);
}
errout:
free(mdevnamep);
if (rval != 0) {
struct stat sbuf;
if (stat(MD_CONF_ORIG, &sbuf) == 0)
(void) copyfile(MD_CONF_ORIG, MD_CONF);
debug_printf("svm_start(): svm_start failed: %d\n", rval);
} else {
int i;
if ((*svmpp)->count > 0) {
debug_printf("svmpp: ");
debug_printf(" root_md: %s", (*svmpp)->root_md);
debug_printf(" count: %d", (*svmpp)->count);
for (i = 0; i < (*svmpp)->count; i++) {
debug_printf(" md_comps[%d]: %s", i,
(*svmpp)->md_comps[i]);
}
debug_printf(" \n");
} else {
if ((*svmpp)->count == 0)
debug_printf("svm_start(): no mirrored root\n");
}
debug_printf("svm_start(): svm_start succeeded.\n");
}
return (rval);
}
/*
* FUNCTION: copyfile
*
* INPUT: self descriptive
*
* RETURN:
* RET_SUCCESS
* RET_ERROR
*/
int
copyfile(char *from, char *to)
{
int fromfd, tofd;
char buf[1024];
ssize_t rbytes;
struct stat fromstat;
if ((fromfd = open(from, O_RDONLY | O_NDELAY)) < 0)
return (RET_ERROR);
if ((fstat(fromfd, &fromstat) < 0) || ! ISREG(fromstat)) {
(void) close(fromfd);
return (RET_ERROR);
}
if ((tofd = open(to, O_CREAT | O_WRONLY | O_TRUNC,
(fromstat.st_mode & MODEBITS))) < 0) {
(void) close(fromfd);
return (RET_ERROR);
}
/*
* in case the file exists then perm is forced by this chmod
*/
(void) fchmod(tofd, fromstat.st_mode & MODEBITS);
for (;;) {
rbytes = read(fromfd, buf, sizeof (buf));
/*
* no need to check for negative values since the file
* has been successfully stat'ed
*/
if (rbytes == 0)
break;
if (write(tofd, buf, rbytes) != rbytes) {
rbytes = -1;
break;
}
}
(void) close(fromfd);
(void) close(tofd);
if (rbytes < 0) {
(void) unlink(to);
return (RET_ERROR);
}
return (RET_SUCCESS);
}
|