]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
cd29a5c8 | 4 | * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au> |
64c4757e NB |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
22 | * Email: <neilb@cse.unsw.edu.au> | |
23 | * Paper: Neil Brown | |
24 | * School of Computer Science and Engineering | |
25 | * The University of New South Wales | |
26 | * Sydney, 2052 | |
27 | * Australia | |
28 | */ | |
29 | ||
9a9dab36 | 30 | #include "mdadm.h" |
64c4757e | 31 | #include "md_p.h" |
dd0781e5 | 32 | #include <ctype.h> |
64c4757e | 33 | |
dd0781e5 | 34 | |
52826846 NB |
35 | int main(int argc, char *argv[]) |
36 | { | |
e0d19036 | 37 | int mode = 0; |
52826846 | 38 | int opt; |
e0d19036 | 39 | int option_index; |
52826846 NB |
40 | char *help_text; |
41 | char *c; | |
42 | int rv; | |
f9ce90ba | 43 | int i; |
52826846 NB |
44 | |
45 | int chunk = 0; | |
dd0781e5 | 46 | int size = -1; |
98c6faba NB |
47 | int level = UnSet; |
48 | int layout = UnSet; | |
52826846 NB |
49 | int raiddisks = 0; |
50 | int sparedisks = 0; | |
51 | struct mddev_ident_s ident; | |
52 | char *configfile = NULL; | |
cd29a5c8 | 53 | char *cp; |
5787fa49 | 54 | char *update = NULL; |
52826846 NB |
55 | int scan = 0; |
56 | char devmode = 0; | |
57 | int runstop = 0; | |
58 | int readonly = 0; | |
bd526cee | 59 | int SparcAdjust = 0; |
cd29a5c8 NB |
60 | mddev_dev_t devlist = NULL; |
61 | mddev_dev_t *devlistend = & devlist; | |
62 | mddev_dev_t dv; | |
52826846 NB |
63 | int devs_found = 0; |
64 | int verbose = 0; | |
cd29a5c8 | 65 | int brief = 0; |
52826846 | 66 | int force = 0; |
feb716e9 | 67 | int test = 0; |
dd0781e5 | 68 | int assume_clean = 0; |
8d80900b NB |
69 | int autof = 0; /* -2 means create device based on name: |
70 | * if it ends mdN, then non-partitioned array N | |
71 | * if it ends dN, then partitions array N | |
72 | * -1 means create non-partitioned, choose N | |
73 | * 1 or more to create partitioned | |
74 | * If -1 or 1 and name is a 'standard' name, then | |
75 | * insist on a match of type and number. | |
76 | */ | |
52826846 NB |
77 | |
78 | char *mailaddr = NULL; | |
79 | char *program = NULL; | |
80 | int delay = 0; | |
d013a55e | 81 | int daemonise = 0; |
b5e64645 | 82 | char *pidfile = NULL; |
aa88f531 | 83 | int oneshot = 0; |
82d9eba6 | 84 | struct supertype *ss = NULL; |
52826846 | 85 | |
e5329c37 NB |
86 | int copies; |
87 | ||
52826846 NB |
88 | int mdfd = -1; |
89 | ||
90 | ident.uuid_set=0; | |
98c6faba NB |
91 | ident.level = UnSet; |
92 | ident.raid_disks = UnSet; | |
93 | ident.super_minor= UnSet; | |
52826846 | 94 | ident.devices=0; |
a75f2fbc NB |
95 | ident.spare_group = NULL; |
96 | ident.autof = 0; | |
52826846 | 97 | |
e0d19036 NB |
98 | while ((option_index = -1) , |
99 | (opt=getopt_long(argc, argv, | |
52826846 | 100 | short_options, long_options, |
e0d19036 NB |
101 | &option_index)) != -1) { |
102 | int newmode = mode; | |
103 | /* firstly, so mode-independant options */ | |
52826846 | 104 | switch(opt) { |
52826846 NB |
105 | case 'h': |
106 | help_text = Help; | |
56eedc1a NB |
107 | if (option_index > 0 && |
108 | strcmp(long_options[option_index].name, "help-options")==0) | |
109 | help_text = OptionHelp; | |
110 | else | |
111 | switch (mode) { | |
112 | case ASSEMBLE : help_text = Help_assemble; break; | |
113 | case BUILD : help_text = Help_build; break; | |
114 | case CREATE : help_text = Help_create; break; | |
115 | case MANAGE : help_text = Help_manage; break; | |
116 | case MISC : help_text = Help_misc; break; | |
117 | case MONITOR : help_text = Help_monitor; break; | |
dd0781e5 | 118 | case GROW : help_text = Help_grow; break; |
56eedc1a | 119 | } |
52826846 NB |
120 | fputs(help_text,stderr); |
121 | exit(0); | |
122 | ||
123 | case 'V': | |
124 | fputs(Version, stderr); | |
125 | exit(0); | |
126 | ||
127 | case 'v': verbose = 1; | |
128 | continue; | |
129 | ||
cd29a5c8 NB |
130 | case 'b': brief = 1; |
131 | continue; | |
132 | ||
e0d19036 NB |
133 | case ':': |
134 | case '?': | |
135 | fputs(Usage, stderr); | |
136 | exit(2); | |
137 | } | |
138 | /* second, figure out the mode. | |
139 | * Some options force the mode. Others | |
140 | * set the mode if it isn't already | |
141 | */ | |
142 | ||
143 | switch(opt) { | |
144 | case '@': /* just incase they say --manage */ | |
145 | newmode = MANAGE; break; | |
146 | case 'a': | |
147 | case 'r': | |
148 | case 'f': | |
0320ea45 NB |
149 | if (!mode) newmode = MANAGE; |
150 | break; | |
e0d19036 NB |
151 | |
152 | case 'A': newmode = ASSEMBLE; break; | |
153 | case 'B': newmode = BUILD; break; | |
154 | case 'C': newmode = CREATE; break; | |
155 | case 'F': newmode = MONITOR;break; | |
dd0781e5 | 156 | case 'G': newmode = GROW; break; |
e0d19036 NB |
157 | |
158 | case '#': | |
159 | case 'D': | |
160 | case 'E': | |
161 | case 'Q': newmode = MISC; break; | |
162 | case 'R': | |
163 | case 'S': | |
164 | case 'o': | |
165 | case 'w': | |
166 | case 'K': if (!mode) newmode = MISC; break; | |
167 | } | |
168 | if (mode && newmode == mode) { | |
169 | /* everybody happy ! */ | |
170 | } else if (mode && newmode != mode) { | |
171 | /* not allowed.. */ | |
172 | fprintf(stderr, Name ": "); | |
173 | if (option_index >= 0) | |
174 | fprintf(stderr, "--%s", long_options[option_index].name); | |
175 | else | |
176 | fprintf(stderr, "-%c", opt); | |
e6b64cd0 | 177 | fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n", |
e0d19036 NB |
178 | map_num(modes, newmode), |
179 | map_num(modes, mode)); | |
180 | exit(2); | |
181 | } else if (!mode && newmode) { | |
182 | mode = newmode; | |
183 | } else { | |
184 | /* special case of -c --help */ | |
185 | if (opt == 'c' && | |
186 | ( strncmp(optarg, "--h", 3)==0 || | |
187 | strncmp(optarg, "-h", 2)==0)) { | |
188 | fputs(Help_config, stderr); | |
189 | exit(0); | |
cd29a5c8 | 190 | } |
0320ea45 NB |
191 | |
192 | /* If first option is a device, don't force the mode yet */ | |
193 | if (opt == 1) { | |
194 | if (devs_found == 0) { | |
195 | dv = malloc(sizeof(*dv)); | |
196 | if (dv == NULL) { | |
197 | fprintf(stderr, Name ": malloc failed\n"); | |
198 | exit(3); | |
199 | } | |
200 | dv->devname = optarg; | |
201 | dv->disposition = devmode; | |
202 | dv->next = NULL; | |
203 | *devlistend = dv; | |
204 | devlistend = &dv->next; | |
205 | ||
206 | devs_found++; | |
207 | continue; | |
208 | } | |
209 | /* No mode yet, and this is the second device ... */ | |
210 | fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n"); | |
211 | exit(2); | |
212 | } | |
e0d19036 | 213 | if (option_index >= 0) |
0320ea45 | 214 | fprintf(stderr, Name ": --%s", long_options[option_index].name); |
e0d19036 | 215 | else |
0320ea45 NB |
216 | fprintf(stderr, Name ": -%c", opt); |
217 | fprintf(stderr, " does not set the mode, and so cannot be the first option.\n"); | |
e0d19036 NB |
218 | exit(2); |
219 | } | |
220 | ||
221 | /* if we just set the mode, then done */ | |
222 | switch(opt) { | |
223 | case '@': | |
224 | case '#': | |
225 | case 'A': | |
226 | case 'B': | |
227 | case 'C': | |
228 | case 'F': | |
dd0781e5 | 229 | case 'G': |
e0d19036 NB |
230 | continue; |
231 | } | |
232 | if (opt == 1) { | |
233 | /* an undecorated option - must be a device name. | |
234 | */ | |
cd29a5c8 | 235 | if (devs_found > 0 && mode == '@' && !devmode) { |
dd0781e5 | 236 | fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg); |
52826846 NB |
237 | exit(2); |
238 | } | |
e5329c37 NB |
239 | if (devs_found > 0 && mode == 'G' && !devmode) { |
240 | fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg); | |
241 | exit(2); | |
242 | } | |
cd29a5c8 NB |
243 | dv = malloc(sizeof(*dv)); |
244 | if (dv == NULL) { | |
245 | fprintf(stderr, Name ": malloc failed\n"); | |
246 | exit(3); | |
247 | } | |
248 | dv->devname = optarg; | |
249 | dv->disposition = devmode; | |
250 | dv->next = NULL; | |
251 | *devlistend = dv; | |
252 | devlistend = &dv->next; | |
253 | ||
52826846 NB |
254 | devs_found++; |
255 | continue; | |
682c7051 | 256 | } |
682c7051 | 257 | |
52826846 NB |
258 | /* We've got a mode, and opt is now something else which |
259 | * could depend on the mode */ | |
260 | #define O(a,b) ((a<<8)|b) | |
261 | switch (O(mode,opt)) { | |
e0d19036 NB |
262 | case O(CREATE,'c'): |
263 | case O(BUILD,'c'): /* chunk or rounding */ | |
52826846 NB |
264 | if (chunk) { |
265 | fprintf(stderr, Name ": chunk/rounding may only be specified once. " | |
266 | "Second value is %s.\n", optarg); | |
267 | exit(2); | |
268 | } | |
269 | chunk = strtol(optarg, &c, 10); | |
270 | if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) { | |
271 | fprintf(stderr, Name ": invalid chunk/rounding value: %s\n", | |
272 | optarg); | |
273 | exit(2); | |
274 | } | |
275 | continue; | |
64c4757e | 276 | |
f9ce90ba NB |
277 | case O(CREATE,'e'): |
278 | case O(ASSEMBLE,'e'): | |
279 | case O(MISC,'e'): /* set metadata (superblock) information */ | |
280 | if (ss) { | |
281 | fprintf(stderr, Name ": metadata information already given\n"); | |
282 | exit(2); | |
283 | } | |
82d9eba6 NB |
284 | for(i=0; !ss && superlist[i]; i++) |
285 | ss = superlist[i]->match_metadata_desc(optarg); | |
286 | ||
f9ce90ba NB |
287 | if (!ss) { |
288 | fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg); | |
289 | exit(2); | |
290 | } | |
291 | continue; | |
292 | ||
dd0781e5 | 293 | case O(GROW,'z'): |
e0d19036 | 294 | case O(CREATE,'z'): /* size */ |
dd0781e5 | 295 | if (size >= 0) { |
52826846 NB |
296 | fprintf(stderr, Name ": size may only be specified once. " |
297 | "Second value is %s.\n", optarg); | |
298 | exit(2); | |
299 | } | |
dd0781e5 NB |
300 | if (strcmp(optarg, "max")==0) |
301 | size = 0; | |
302 | else { | |
303 | size = strtol(optarg, &c, 10); | |
304 | if (!optarg[0] || *c || size < 4) { | |
305 | fprintf(stderr, Name ": invalid size: %s\n", | |
306 | optarg); | |
307 | exit(2); | |
308 | } | |
52826846 NB |
309 | } |
310 | continue; | |
64c4757e | 311 | |
b5e64645 | 312 | case O(GROW,'l'): /* hack - needed to understand layout */ |
e0d19036 NB |
313 | case O(CREATE,'l'): |
314 | case O(BUILD,'l'): /* set raid level*/ | |
98c6faba | 315 | if (level != UnSet) { |
52826846 NB |
316 | fprintf(stderr, Name ": raid level may only be set once. " |
317 | "Second value is %s.\n", optarg); | |
318 | exit(2); | |
319 | } | |
320 | level = map_name(pers, optarg); | |
98c6faba | 321 | if (level == UnSet) { |
52826846 NB |
322 | fprintf(stderr, Name ": invalid raid level: %s\n", |
323 | optarg); | |
324 | exit(2); | |
325 | } | |
b5e64645 | 326 | if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) { |
52826846 NB |
327 | fprintf(stderr, Name ": Raid level %s not permitted with --build.\n", |
328 | optarg); | |
329 | exit(2); | |
330 | } | |
e0d19036 | 331 | if (sparedisks > 0 && level < 1 && level >= -1) { |
b83d95f3 | 332 | fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n", |
52826846 NB |
333 | optarg); |
334 | exit(2); | |
335 | } | |
cd29a5c8 | 336 | ident.level = level; |
52826846 | 337 | continue; |
64c4757e | 338 | |
e0d19036 | 339 | case O(CREATE,'p'): /* raid5 layout */ |
b5e64645 NB |
340 | case O(BUILD,'p'): /* faulty layout */ |
341 | case O(GROW, 'p'): /* faulty reconfig */ | |
e5329c37 | 342 | if (layout != UnSet) { |
52826846 NB |
343 | fprintf(stderr,Name ": layout may only be sent once. " |
344 | "Second value was %s\n", optarg); | |
345 | exit(2); | |
346 | } | |
347 | switch(level) { | |
348 | default: | |
56eb10c0 | 349 | fprintf(stderr, Name ": layout not meaningful for %s arrays.\n", |
52826846 NB |
350 | map_num(pers, level)); |
351 | exit(2); | |
98c6faba | 352 | case UnSet: |
52826846 NB |
353 | fprintf(stderr, Name ": raid level must be given before layout.\n"); |
354 | exit(2); | |
355 | ||
356 | case 5: | |
98c6faba | 357 | case 6: |
52826846 | 358 | layout = map_name(r5layout, optarg); |
98c6faba | 359 | if (layout==UnSet) { |
52826846 NB |
360 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", |
361 | optarg); | |
362 | exit(2); | |
363 | } | |
364 | break; | |
e5329c37 NB |
365 | |
366 | case 10: | |
367 | /* 'f' or 'n' followed by a number <= raid_disks */ | |
368 | if ((optarg[0] != 'n' && optarg[0] != 'f') || | |
369 | (copies = strtoul(optarg+1, &cp, 10)) < 1 || | |
370 | copies > 200 || | |
371 | *cp) { | |
372 | fprintf(stderr, Name ": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg); | |
373 | exit(2); | |
374 | } | |
375 | if (optarg[0] == 'n') | |
376 | layout = 256 + copies; | |
377 | else | |
378 | layout = 1 + (copies<<8); | |
379 | break; | |
b5e64645 NB |
380 | case -5: /* Faulty |
381 | * modeNNN | |
382 | */ | |
383 | ||
384 | { | |
385 | int ln = strcspn(optarg, "0123456789"); | |
386 | char *m = strdup(optarg); | |
387 | int mode; | |
388 | m[ln] = 0; | |
389 | mode = map_name(faultylayout, m); | |
390 | if (mode == UnSet) { | |
391 | fprintf(stderr, Name ": layout %s not understood for faulty.\n", | |
392 | optarg); | |
393 | exit(2); | |
394 | } | |
395 | layout = mode | (atoi(optarg+ln)<< ModeShift); | |
396 | } | |
52826846 NB |
397 | } |
398 | continue; | |
399 | ||
dd0781e5 NB |
400 | case O(CREATE,3): |
401 | case O(BUILD,3): /* assume clean */ | |
402 | assume_clean = 1; | |
403 | continue; | |
404 | ||
405 | case O(GROW,'n'): | |
e0d19036 NB |
406 | case O(CREATE,'n'): |
407 | case O(BUILD,'n'): /* number of raid disks */ | |
52826846 | 408 | if (raiddisks) { |
b83d95f3 | 409 | fprintf(stderr, Name ": raid-devices set twice: %d and %s\n", |
52826846 NB |
410 | raiddisks, optarg); |
411 | exit(2); | |
412 | } | |
413 | raiddisks = strtol(optarg, &c, 10); | |
414 | if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) { | |
b83d95f3 | 415 | fprintf(stderr, Name ": invalid number of raid devices: %s\n", |
52826846 NB |
416 | optarg); |
417 | exit(2); | |
418 | } | |
b5e64645 | 419 | if (raiddisks == 1 && !force && level != -5) { |
aa88f531 NB |
420 | fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n" |
421 | " a mistake. If you really mean it you will need to specify --force before\n" | |
422 | " setting the number of drives.\n"); | |
423 | exit(2); | |
424 | } | |
cd29a5c8 | 425 | ident.raid_disks = raiddisks; |
52826846 NB |
426 | continue; |
427 | ||
e0d19036 | 428 | case O(CREATE,'x'): /* number of spare (eXtra) discs */ |
52826846 | 429 | if (sparedisks) { |
b83d95f3 | 430 | fprintf(stderr,Name ": spare-devices set twice: %d and %s\n", |
52826846 NB |
431 | sparedisks, optarg); |
432 | exit(2); | |
433 | } | |
98c6faba | 434 | if (level != UnSet && level <= 0 && level >= -1) { |
b83d95f3 | 435 | fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n", |
52826846 NB |
436 | level); |
437 | exit(2); | |
438 | } | |
439 | sparedisks = strtol(optarg, &c, 10); | |
440 | if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) { | |
b83d95f3 | 441 | fprintf(stderr, Name ": invalid number of spare-devices: %s\n", |
52826846 NB |
442 | optarg); |
443 | exit(2); | |
444 | } | |
445 | continue; | |
dd0781e5 NB |
446 | |
447 | case O(CREATE,'a'): | |
448 | case O(BUILD,'a'): | |
449 | case O(ASSEMBLE,'a'): /* auto-creation of device node */ | |
450 | if (optarg == NULL) | |
8d80900b | 451 | autof = -2; |
dd0781e5 NB |
452 | else if (strcasecmp(optarg,"no")==0) |
453 | autof = 0; | |
8d80900b NB |
454 | else if (strcasecmp(optarg,"yes")==0) |
455 | autof = -2; | |
456 | else if (strcasecmp(optarg,"md")==0) | |
dd0781e5 NB |
457 | autof = -1; |
458 | else { | |
459 | /* There might be digits, and maybe a hypen, at the end */ | |
460 | char *e = optarg + strlen(optarg); | |
461 | int num = 4; | |
462 | int len; | |
463 | while (e > optarg && isdigit(e[-1])) | |
464 | e--; | |
465 | if (*e) { | |
466 | num = atoi(e); | |
467 | if (num <= 0) num = 1; | |
468 | } | |
469 | if (e > optarg && e[-1] == '-') | |
470 | e--; | |
471 | len = e - optarg; | |
472 | if ((len == 3 && strncasecmp(optarg,"mdp",3)==0) || | |
473 | (len == 1 && strncasecmp(optarg,"p",1)==0) || | |
474 | (len >= 4 && strncasecmp(optarg,"part",4)==0)) | |
475 | autof = num; | |
476 | else { | |
477 | fprintf(stderr, Name ": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n" | |
478 | " optionally followed by a number.\n", | |
479 | optarg); | |
480 | exit(2); | |
481 | } | |
482 | } | |
483 | continue; | |
484 | ||
aa88f531 | 485 | case O(BUILD,'f'): /* force honouring '-n 1' */ |
e0d19036 NB |
486 | case O(CREATE,'f'): /* force honouring of device list */ |
487 | case O(ASSEMBLE,'f'): /* force assembly */ | |
488 | case O(MISC,'f'): /* force zero */ | |
52826846 NB |
489 | force=1; |
490 | continue; | |
491 | ||
492 | /* now for the Assemble options */ | |
e0d19036 | 493 | case O(ASSEMBLE,'u'): /* uuid of array */ |
52826846 | 494 | if (ident.uuid_set) { |
cd29a5c8 | 495 | fprintf(stderr, Name ": uuid cannot be set twice. " |
52826846 NB |
496 | "Second value %s.\n", optarg); |
497 | exit(2); | |
498 | } | |
499 | if (parse_uuid(optarg, ident.uuid)) | |
500 | ident.uuid_set = 1; | |
501 | else { | |
502 | fprintf(stderr,Name ": Bad uuid: %s\n", optarg); | |
503 | exit(2); | |
504 | } | |
505 | continue; | |
506 | ||
e0d19036 | 507 | case O(ASSEMBLE,'m'): /* super-minor for array */ |
98c6faba | 508 | if (ident.super_minor != UnSet) { |
cd29a5c8 NB |
509 | fprintf(stderr, Name ": super-minor cannot be set twice. " |
510 | "Second value: %s.\n", optarg); | |
511 | exit(2); | |
512 | } | |
d013a55e NB |
513 | if (strcmp(optarg, "dev")==0) |
514 | ident.super_minor = -2; | |
515 | else { | |
516 | ident.super_minor = strtoul(optarg, &cp, 10); | |
517 | if (!optarg[0] || *cp) { | |
518 | fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg); | |
519 | exit(2); | |
520 | } | |
cd29a5c8 NB |
521 | } |
522 | continue; | |
523 | ||
5787fa49 NB |
524 | case O(ASSEMBLE,'U'): /* update the superblock */ |
525 | if (update) { | |
526 | fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n", | |
527 | update, optarg); | |
528 | exit(2); | |
529 | } | |
530 | update = optarg; | |
e5329c37 NB |
531 | if (strcmp(update, "sparc2.2")==0) |
532 | continue; | |
5787fa49 NB |
533 | if (strcmp(update, "super-minor") == 0) |
534 | continue; | |
feb716e9 NB |
535 | if (strcmp(update, "summaries")==0) |
536 | continue; | |
e5329c37 NB |
537 | if (strcmp(update, "resync")==0) |
538 | continue; | |
539 | fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update); | |
5787fa49 NB |
540 | exit(2); |
541 | ||
e0d19036 | 542 | case O(ASSEMBLE,'c'): /* config file */ |
2d465520 | 543 | case O(MISC, 'c'): |
e0d19036 | 544 | case O(MONITOR,'c'): |
52826846 NB |
545 | if (configfile) { |
546 | fprintf(stderr, Name ": configfile cannot be set twice. " | |
547 | "Second value is %s.\n", optarg); | |
548 | exit(2); | |
549 | } | |
550 | configfile = optarg; | |
551 | /* FIXME possibly check that config file exists. Even parse it */ | |
552 | continue; | |
e0d19036 NB |
553 | case O(ASSEMBLE,'s'): /* scan */ |
554 | case O(MISC,'s'): | |
555 | case O(MONITOR,'s'): | |
52826846 NB |
556 | scan = 1; |
557 | continue; | |
558 | ||
e0d19036 | 559 | case O(MONITOR,'m'): /* mail address */ |
52826846 NB |
560 | if (mailaddr) |
561 | fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n", | |
562 | optarg); | |
563 | else | |
564 | mailaddr = optarg; | |
565 | continue; | |
566 | ||
e0d19036 | 567 | case O(MONITOR,'p'): /* alert program */ |
52826846 NB |
568 | if (program) |
569 | fprintf(stderr, Name ": only specify one alter program. %s ignored.\n", | |
570 | optarg); | |
571 | else | |
572 | program = optarg; | |
573 | continue; | |
64c4757e | 574 | |
e0d19036 | 575 | case O(MONITOR,'d'): /* delay in seconds */ |
52826846 NB |
576 | if (delay) |
577 | fprintf(stderr, Name ": only specify delay once. %s ignored.\n", | |
578 | optarg); | |
579 | else { | |
580 | delay = strtol(optarg, &c, 10); | |
581 | if (!optarg[0] || *c || delay<1) { | |
582 | fprintf(stderr, Name ": invalid delay: %s\n", | |
583 | optarg); | |
584 | exit(2); | |
585 | } | |
586 | } | |
587 | continue; | |
d013a55e NB |
588 | case O(MONITOR,'f'): /* daemonise */ |
589 | daemonise = 1; | |
590 | continue; | |
b5e64645 NB |
591 | case O(MONITOR,'i'): /* pid */ |
592 | if (pidfile) | |
593 | fprintf(stderr, Name ": only specify one pid file. %s ignored.\n", | |
594 | optarg); | |
595 | else | |
596 | pidfile = optarg; | |
597 | continue; | |
aa88f531 NB |
598 | case O(MONITOR,'1'): /* oneshot */ |
599 | oneshot = 1; | |
600 | continue; | |
98c6faba NB |
601 | case O(MONITOR,'t'): /* test */ |
602 | test = 1; | |
603 | continue; | |
52826846 NB |
604 | |
605 | /* now the general management options. Some are applicable | |
606 | * to other modes. None have arguments. | |
607 | */ | |
e5329c37 | 608 | case O(GROW,'a'): |
dd0781e5 | 609 | case O(MANAGE,'a'): /* add a drive */ |
52826846 NB |
610 | devmode = 'a'; |
611 | continue; | |
e0d19036 | 612 | case O(MANAGE,'r'): /* remove a drive */ |
52826846 NB |
613 | devmode = 'r'; |
614 | continue; | |
e0d19036 | 615 | case O(MANAGE,'f'): /* set faulty */ |
52826846 NB |
616 | devmode = 'f'; |
617 | continue; | |
e0d19036 NB |
618 | case O(MANAGE,'R'): |
619 | case O(ASSEMBLE,'R'): | |
620 | case O(BUILD,'R'): | |
621 | case O(CREATE,'R'): /* Run the array */ | |
52826846 NB |
622 | if (runstop < 0) { |
623 | fprintf(stderr, Name ": Cannot both Stop and Run an array\n"); | |
624 | exit(2); | |
625 | } | |
626 | runstop = 1; | |
627 | continue; | |
e0d19036 | 628 | case O(MANAGE,'S'): |
52826846 NB |
629 | if (runstop > 0) { |
630 | fprintf(stderr, Name ": Cannot both Run and Stop an array\n"); | |
631 | exit(2); | |
632 | } | |
633 | runstop = -1; | |
634 | continue; | |
635 | ||
e0d19036 | 636 | case O(MANAGE,'o'): |
52826846 NB |
637 | if (readonly < 0) { |
638 | fprintf(stderr, Name ": Cannot have both readonly and readwrite\n"); | |
639 | exit(2); | |
640 | } | |
641 | readonly = 1; | |
642 | continue; | |
e0d19036 | 643 | case O(MANAGE,'w'): |
52826846 | 644 | if (readonly > 0) { |
e0d19036 | 645 | fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n"); |
52826846 NB |
646 | exit(2); |
647 | } | |
648 | readonly = -1; | |
649 | continue; | |
e0d19036 NB |
650 | |
651 | case O(MISC,'Q'): | |
652 | case O(MISC,'D'): | |
653 | case O(MISC,'E'): | |
654 | case O(MISC,'K'): | |
655 | case O(MISC,'R'): | |
656 | case O(MISC,'S'): | |
657 | case O(MISC,'o'): | |
658 | case O(MISC,'w'): | |
659 | if (devmode && devmode != opt && | |
660 | (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) { | |
661 | fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n", | |
662 | devmode =='E'?opt:devmode); | |
663 | exit(2); | |
664 | } | |
665 | devmode = opt; | |
666 | continue; | |
feb716e9 NB |
667 | case O(MISC,'t'): |
668 | test = 1; | |
669 | continue; | |
e0d19036 | 670 | |
bd526cee NB |
671 | case O(MISC, 22): |
672 | if (devmode != 'E') { | |
673 | fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n"); | |
674 | exit(2); | |
675 | } | |
676 | SparcAdjust = 1; | |
677 | continue; | |
52826846 NB |
678 | } |
679 | /* We have now processed all the valid options. Anything else is | |
680 | * an error | |
681 | */ | |
e0d19036 NB |
682 | fprintf(stderr, Name ": option %c not valid in %s mode\n", |
683 | opt, map_num(modes, mode)); | |
64c4757e | 684 | exit(2); |
52826846 NB |
685 | |
686 | } | |
687 | ||
0320ea45 NB |
688 | if (!mode && devs_found) { |
689 | mode = MISC; | |
690 | devmode = 'Q'; | |
691 | if (devlist->disposition == 0) | |
692 | devlist->disposition = devmode; | |
693 | } | |
52826846 NB |
694 | if (!mode) { |
695 | fputs(Usage, stderr); | |
64c4757e | 696 | exit(2); |
64c4757e | 697 | } |
52826846 NB |
698 | /* Ok, got the option parsing out of the way |
699 | * hopefully it's mostly right but there might be some stuff | |
700 | * missing | |
701 | * | |
e0d19036 | 702 | * That is mosty checked in the per-mode stuff but... |
52826846 NB |
703 | * |
704 | * For @,B,C and A without -s, the first device listed must be an md device | |
705 | * we check that here and open it. | |
64c4757e | 706 | */ |
52826846 | 707 | |
dd0781e5 NB |
708 | if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW || |
709 | (mode == ASSEMBLE && ! scan)) { | |
52826846 NB |
710 | if (devs_found < 1) { |
711 | fprintf(stderr, Name ": an md device must be given in this mode\n"); | |
712 | exit(2); | |
713 | } | |
dd0781e5 NB |
714 | if ((int)ident.super_minor == -2 && autof) { |
715 | fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n"); | |
716 | exit(2); | |
717 | } | |
718 | mdfd = open_mddev(devlist->devname, autof); | |
52826846 NB |
719 | if (mdfd < 0) |
720 | exit(1); | |
98c6faba | 721 | if ((int)ident.super_minor == -2) { |
d013a55e NB |
722 | struct stat stb; |
723 | fstat(mdfd, &stb); | |
0df46c2a | 724 | ident.super_minor = minor(stb.st_rdev); |
d013a55e | 725 | } |
64c4757e | 726 | } |
52826846 | 727 | |
52826846 NB |
728 | rv = 0; |
729 | switch(mode) { | |
e0d19036 | 730 | case MANAGE: |
52826846 NB |
731 | /* readonly, add/remove, readwrite, runstop */ |
732 | if (readonly>0) | |
cd29a5c8 | 733 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 734 | if (!rv && devs_found>1) |
cd29a5c8 NB |
735 | rv = Manage_subdevs(devlist->devname, mdfd, |
736 | devlist->next); | |
52826846 | 737 | if (!rv && readonly < 0) |
cd29a5c8 | 738 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 739 | if (!rv && runstop) |
cd29a5c8 | 740 | rv = Manage_runstop(devlist->devname, mdfd, runstop); |
52826846 | 741 | break; |
e0d19036 | 742 | case ASSEMBLE: |
d013a55e | 743 | if (devs_found == 1 && ident.uuid_set == 0 && |
98c6faba | 744 | ident.super_minor == UnSet && !scan ) { |
d013a55e NB |
745 | /* Only a device has been given, so get details from config file */ |
746 | mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname); | |
b5e64645 NB |
747 | if (array_ident == NULL) { |
748 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
749 | devlist->devname); | |
d013a55e | 750 | rv |= 1; |
b5e64645 NB |
751 | } else { |
752 | mdfd = open_mddev(devlist->devname, array_ident->autof); | |
753 | if (mdfd < 0) | |
d013a55e | 754 | rv |= 1; |
b5e64645 | 755 | else { |
f9ce90ba | 756 | rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile, |
d013a55e NB |
757 | NULL, |
758 | readonly, runstop, update, verbose, force); | |
b5e64645 NB |
759 | close(mdfd); |
760 | } | |
d013a55e NB |
761 | } |
762 | } else if (!scan) | |
f9ce90ba | 763 | rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile, |
cd29a5c8 | 764 | devlist->next, |
5787fa49 NB |
765 | readonly, runstop, update, verbose, force); |
766 | else if (devs_found>0) { | |
767 | if (update && devs_found > 1) { | |
768 | fprintf(stderr, Name ": can only update a single array at a time\n"); | |
769 | exit(1); | |
770 | } | |
cd29a5c8 NB |
771 | for (dv = devlist ; dv ; dv=dv->next) { |
772 | mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname); | |
52826846 NB |
773 | if (array_ident == NULL) { |
774 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
cd29a5c8 | 775 | dv->devname); |
52826846 NB |
776 | rv |= 1; |
777 | continue; | |
778 | } | |
b5e64645 NB |
779 | mdfd = open_mddev(dv->devname, array_ident->autof); |
780 | if (mdfd < 0) { | |
781 | rv |= 1; | |
782 | continue; | |
783 | } | |
f9ce90ba | 784 | rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile, |
cd29a5c8 | 785 | NULL, |
5787fa49 | 786 | readonly, runstop, update, verbose, force); |
b5e64645 | 787 | close(mdfd); |
52826846 | 788 | } |
5787fa49 | 789 | } else { |
52826846 NB |
790 | mddev_ident_t array_list = conf_get_ident(configfile, NULL); |
791 | if (!array_list) { | |
792 | fprintf(stderr, Name ": No arrays found in config file\n"); | |
793 | rv = 1; | |
794 | } else | |
795 | for (; array_list; array_list = array_list->next) { | |
cd29a5c8 | 796 | mdu_array_info_t array; |
dd0781e5 | 797 | mdfd = open_mddev(array_list->devname, array_list->autof); |
52826846 NB |
798 | if (mdfd < 0) { |
799 | rv |= 1; | |
800 | continue; | |
801 | } | |
cd29a5c8 NB |
802 | if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0) |
803 | /* already assembled, skip */ | |
43fc1676 NB |
804 | ; |
805 | else | |
f9ce90ba | 806 | rv |= Assemble(ss, array_list->devname, mdfd, |
43fc1676 NB |
807 | array_list, configfile, |
808 | NULL, | |
809 | readonly, runstop, NULL, verbose, force); | |
810 | close(mdfd); | |
52826846 NB |
811 | } |
812 | } | |
813 | break; | |
e0d19036 | 814 | case BUILD: |
b5e64645 | 815 | rv = Build(devlist->devname, mdfd, chunk, level, layout, raiddisks, devlist->next, assume_clean); |
52826846 | 816 | break; |
e0d19036 | 817 | case CREATE: |
f9ce90ba | 818 | if (ss == NULL) { |
82d9eba6 NB |
819 | for(i=0; !ss && superlist[i]; i++) |
820 | ss = superlist[i]->match_metadata_desc("default"); | |
f9ce90ba NB |
821 | } |
822 | if (!ss) { | |
823 | fprintf(stderr, Name ": internal error - no default metadata style\n"); | |
824 | exit(2); | |
825 | } | |
826 | ||
827 | rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size, | |
52826846 | 828 | raiddisks, sparedisks, |
cd29a5c8 | 829 | devs_found-1, devlist->next, runstop, verbose, force); |
52826846 | 830 | break; |
e0d19036 NB |
831 | case MISC: |
832 | ||
833 | if (devmode == 'E') { | |
834 | if (devlist == NULL && !scan) { | |
835 | fprintf(stderr, Name ": No devices to examine\n"); | |
836 | exit(2); | |
837 | } | |
838 | if (devlist == NULL) | |
839 | devlist = conf_get_devs(configfile); | |
840 | if (devlist == NULL) { | |
c913b90e | 841 | fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile); |
e0d19036 NB |
842 | exit(1); |
843 | } | |
82d9eba6 | 844 | rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust, ss); |
e0d19036 NB |
845 | } else { |
846 | if (devlist == NULL) { | |
847 | if ((devmode == 'S' ||devmode=='D') && scan) { | |
848 | /* apply to all devices in /proc/mdstat */ | |
dd0781e5 | 849 | struct mdstat_ent *ms = mdstat_read(0); |
e0d19036 | 850 | struct mdstat_ent *e; |
d17c1f8c NB |
851 | if (devmode == 'S') { |
852 | /* reverse order so that arrays made of arrays are stopped properly */ | |
853 | struct mdstat_ent *sm = NULL; | |
854 | while ((e=ms) != NULL) { | |
855 | ms = e->next; | |
856 | e->next = sm; | |
857 | sm = e; | |
858 | } | |
859 | ms = sm; | |
860 | } | |
e0d19036 NB |
861 | for (e=ms ; e ; e=e->next) { |
862 | char *name = get_md_name(e->devnum); | |
863 | ||
864 | if (!name) { | |
865 | fprintf(stderr, Name ": cannot find device file for %s\n", | |
866 | e->dev); | |
867 | continue; | |
868 | } | |
869 | if (devmode == 'D') | |
feb716e9 | 870 | rv |= Detail(name, !verbose, test); |
e0d19036 | 871 | else if (devmode=='S') { |
dd0781e5 | 872 | mdfd = open_mddev(name, 0); |
43fc1676 | 873 | if (mdfd >= 0) { |
e0d19036 | 874 | rv |= Manage_runstop(name, mdfd, -1); |
43fc1676 NB |
875 | close(mdfd); |
876 | } | |
e0d19036 NB |
877 | } |
878 | put_md_name(name); | |
879 | } | |
880 | } else { | |
881 | fprintf(stderr, Name ": No devices given.\n"); | |
882 | exit(2); | |
883 | } | |
884 | } | |
885 | for (dv=devlist ; dv; dv=dv->next) { | |
886 | switch(dv->disposition) { | |
887 | case 'D': | |
feb716e9 | 888 | rv |= Detail(dv->devname, brief, test); continue; |
e0d19036 NB |
889 | case 'K': /* Zero superblock */ |
890 | rv |= Kill(dv->devname, force); continue; | |
891 | case 'Q': | |
892 | rv |= Query(dv->devname); continue; | |
893 | } | |
dd0781e5 | 894 | mdfd = open_mddev(dv->devname, 0); |
43fc1676 | 895 | if (mdfd>=0) { |
e0d19036 NB |
896 | switch(dv->disposition) { |
897 | case 'R': | |
898 | rv |= Manage_runstop(dv->devname, mdfd, 1); break; | |
899 | case 'S': | |
900 | rv |= Manage_runstop(dv->devname, mdfd, -1); break; | |
901 | case 'o': | |
902 | rv |= Manage_ro(dv->devname, mdfd, 1); break; | |
903 | case 'w': | |
904 | rv |= Manage_ro(dv->devname, mdfd, -1); break; | |
905 | } | |
43fc1676 NB |
906 | close(mdfd); |
907 | } | |
e0d19036 | 908 | } |
cd29a5c8 | 909 | } |
9a9dab36 | 910 | break; |
e0d19036 | 911 | case MONITOR: |
e0d19036 NB |
912 | if (!devlist && !scan) { |
913 | fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n"); | |
914 | rv = 1; | |
915 | break; | |
916 | } | |
b5e64645 NB |
917 | if (pidfile && !daemonise) { |
918 | fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n"); | |
919 | rv = 1; | |
920 | break; | |
921 | } | |
d013a55e | 922 | rv= Monitor(devlist, mailaddr, program, |
b5e64645 | 923 | delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile); |
9a9dab36 | 924 | break; |
dd0781e5 NB |
925 | |
926 | case GROW: | |
927 | if (devs_found > 1) { | |
e5329c37 NB |
928 | |
929 | /* must be '-a'. */ | |
930 | if (size >= 0 || raiddisks) { | |
931 | fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n"); | |
932 | rv = 1; | |
933 | break; | |
934 | } | |
935 | for (dv=devlist->next; dv ; dv=dv->next) { | |
936 | rv = Grow_Add_device(devlist->devname, mdfd, dv->devname); | |
937 | if (rv) | |
938 | break; | |
939 | } | |
b5e64645 NB |
940 | } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) > 1) { |
941 | fprintf(stderr, Name ": can change at most one of size, raiddisks, and layout\n"); | |
dd0781e5 NB |
942 | rv = 1; |
943 | break; | |
b5e64645 NB |
944 | } else if (layout != UnSet) |
945 | rv = Manage_reconfig(devlist->devname, mdfd, layout); | |
946 | else if (size >= 0 || raiddisks) | |
e5329c37 | 947 | rv = Manage_resize(devlist->devname, mdfd, size, raiddisks); |
b5e64645 NB |
948 | else |
949 | fprintf(stderr, Name ": no changes to --grow\n"); | |
dd0781e5 | 950 | break; |
64c4757e | 951 | } |
52826846 | 952 | exit(rv); |
64c4757e | 953 | } |