]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
super1
[thirdparty/mdadm.git] / mdadm.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
30 #include "mdadm.h"
31 #include "md_p.h"
32 #include <ctype.h>
33
34
35 int main(int argc, char *argv[])
36 {
37 int mode = 0;
38 int opt;
39 int option_index;
40 char *help_text;
41 char *c;
42 int rv;
43 int i;
44
45 int chunk = 0;
46 int size = -1;
47 int level = UnSet;
48 int layout = UnSet;
49 int raiddisks = 0;
50 int sparedisks = 0;
51 struct mddev_ident_s ident;
52 char *configfile = NULL;
53 char *cp;
54 char *update = NULL;
55 int scan = 0;
56 char devmode = 0;
57 int runstop = 0;
58 int readonly = 0;
59 int SparcAdjust = 0;
60 mddev_dev_t devlist = NULL;
61 mddev_dev_t *devlistend = & devlist;
62 mddev_dev_t dv;
63 int devs_found = 0;
64 int verbose = 0;
65 int brief = 0;
66 int force = 0;
67 int test = 0;
68 int assume_clean = 0;
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 */
77
78 char *mailaddr = NULL;
79 char *program = NULL;
80 int delay = 0;
81 int daemonise = 0;
82 char *pidfile = NULL;
83 int oneshot = 0;
84 struct supertype *ss = NULL;
85
86 int copies;
87
88 int mdfd = -1;
89
90 ident.uuid_set=0;
91 ident.level = UnSet;
92 ident.raid_disks = UnSet;
93 ident.super_minor= UnSet;
94 ident.devices=0;
95 ident.spare_group = NULL;
96 ident.autof = 0;
97
98 while ((option_index = -1) ,
99 (opt=getopt_long(argc, argv,
100 short_options, long_options,
101 &option_index)) != -1) {
102 int newmode = mode;
103 /* firstly, so mode-independant options */
104 switch(opt) {
105 case 'h':
106 help_text = Help;
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;
118 case GROW : help_text = Help_grow; break;
119 }
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
130 case 'b': brief = 1;
131 continue;
132
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':
149 if (!mode) newmode = MANAGE;
150 break;
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;
156 case 'G': newmode = GROW; break;
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);
177 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
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);
190 }
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 }
213 if (option_index >= 0)
214 fprintf(stderr, Name ": --%s", long_options[option_index].name);
215 else
216 fprintf(stderr, Name ": -%c", opt);
217 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
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':
229 case 'G':
230 continue;
231 }
232 if (opt == 1) {
233 /* an undecorated option - must be a device name.
234 */
235 if (devs_found > 0 && mode == '@' && !devmode) {
236 fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
237 exit(2);
238 }
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 }
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
254 devs_found++;
255 continue;
256 }
257
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)) {
262 case O(CREATE,'c'):
263 case O(BUILD,'c'): /* chunk or rounding */
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;
276
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 }
284 for(i=0; !ss && superlist[i]; i++)
285 ss = superlist[i]->match_metadata_desc(optarg);
286
287 if (!ss) {
288 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
289 exit(2);
290 }
291 continue;
292
293 case O(GROW,'z'):
294 case O(CREATE,'z'): /* size */
295 if (size >= 0) {
296 fprintf(stderr, Name ": size may only be specified once. "
297 "Second value is %s.\n", optarg);
298 exit(2);
299 }
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 }
309 }
310 continue;
311
312 case O(GROW,'l'): /* hack - needed to understand layout */
313 case O(CREATE,'l'):
314 case O(BUILD,'l'): /* set raid level*/
315 if (level != UnSet) {
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);
321 if (level == UnSet) {
322 fprintf(stderr, Name ": invalid raid level: %s\n",
323 optarg);
324 exit(2);
325 }
326 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
327 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
328 optarg);
329 exit(2);
330 }
331 if (sparedisks > 0 && level < 1 && level >= -1) {
332 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
333 optarg);
334 exit(2);
335 }
336 ident.level = level;
337 continue;
338
339 case O(CREATE,'p'): /* raid5 layout */
340 case O(BUILD,'p'): /* faulty layout */
341 case O(GROW, 'p'): /* faulty reconfig */
342 if (layout != UnSet) {
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:
349 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
350 map_num(pers, level));
351 exit(2);
352 case UnSet:
353 fprintf(stderr, Name ": raid level must be given before layout.\n");
354 exit(2);
355
356 case 5:
357 case 6:
358 layout = map_name(r5layout, optarg);
359 if (layout==UnSet) {
360 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
361 optarg);
362 exit(2);
363 }
364 break;
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;
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 }
397 }
398 continue;
399
400 case O(CREATE,3):
401 case O(BUILD,3): /* assume clean */
402 assume_clean = 1;
403 continue;
404
405 case O(GROW,'n'):
406 case O(CREATE,'n'):
407 case O(BUILD,'n'): /* number of raid disks */
408 if (raiddisks) {
409 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
410 raiddisks, optarg);
411 exit(2);
412 }
413 raiddisks = strtol(optarg, &c, 10);
414 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
415 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
416 optarg);
417 exit(2);
418 }
419 if (raiddisks == 1 && !force && level != -5) {
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 }
425 ident.raid_disks = raiddisks;
426 continue;
427
428 case O(CREATE,'x'): /* number of spare (eXtra) discs */
429 if (sparedisks) {
430 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
431 sparedisks, optarg);
432 exit(2);
433 }
434 if (level != UnSet && level <= 0 && level >= -1) {
435 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
436 level);
437 exit(2);
438 }
439 sparedisks = strtol(optarg, &c, 10);
440 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
441 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
442 optarg);
443 exit(2);
444 }
445 continue;
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)
451 autof = -2;
452 else if (strcasecmp(optarg,"no")==0)
453 autof = 0;
454 else if (strcasecmp(optarg,"yes")==0)
455 autof = -2;
456 else if (strcasecmp(optarg,"md")==0)
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
485 case O(BUILD,'f'): /* force honouring '-n 1' */
486 case O(CREATE,'f'): /* force honouring of device list */
487 case O(ASSEMBLE,'f'): /* force assembly */
488 case O(MISC,'f'): /* force zero */
489 force=1;
490 continue;
491
492 /* now for the Assemble options */
493 case O(ASSEMBLE,'u'): /* uuid of array */
494 if (ident.uuid_set) {
495 fprintf(stderr, Name ": uuid cannot be set twice. "
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
507 case O(ASSEMBLE,'m'): /* super-minor for array */
508 if (ident.super_minor != UnSet) {
509 fprintf(stderr, Name ": super-minor cannot be set twice. "
510 "Second value: %s.\n", optarg);
511 exit(2);
512 }
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 }
521 }
522 continue;
523
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;
531 if (strcmp(update, "sparc2.2")==0)
532 continue;
533 if (strcmp(update, "super-minor") == 0)
534 continue;
535 if (strcmp(update, "summaries")==0)
536 continue;
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);
540 exit(2);
541
542 case O(ASSEMBLE,'c'): /* config file */
543 case O(MISC, 'c'):
544 case O(MONITOR,'c'):
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;
553 case O(ASSEMBLE,'s'): /* scan */
554 case O(MISC,'s'):
555 case O(MONITOR,'s'):
556 scan = 1;
557 continue;
558
559 case O(MONITOR,'m'): /* mail address */
560 if (mailaddr)
561 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
562 optarg);
563 else
564 mailaddr = optarg;
565 continue;
566
567 case O(MONITOR,'p'): /* alert program */
568 if (program)
569 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
570 optarg);
571 else
572 program = optarg;
573 continue;
574
575 case O(MONITOR,'d'): /* delay in seconds */
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;
588 case O(MONITOR,'f'): /* daemonise */
589 daemonise = 1;
590 continue;
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;
598 case O(MONITOR,'1'): /* oneshot */
599 oneshot = 1;
600 continue;
601 case O(MONITOR,'t'): /* test */
602 test = 1;
603 continue;
604
605 /* now the general management options. Some are applicable
606 * to other modes. None have arguments.
607 */
608 case O(GROW,'a'):
609 case O(MANAGE,'a'): /* add a drive */
610 devmode = 'a';
611 continue;
612 case O(MANAGE,'r'): /* remove a drive */
613 devmode = 'r';
614 continue;
615 case O(MANAGE,'f'): /* set faulty */
616 devmode = 'f';
617 continue;
618 case O(MANAGE,'R'):
619 case O(ASSEMBLE,'R'):
620 case O(BUILD,'R'):
621 case O(CREATE,'R'): /* Run the array */
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;
628 case O(MANAGE,'S'):
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
636 case O(MANAGE,'o'):
637 if (readonly < 0) {
638 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
639 exit(2);
640 }
641 readonly = 1;
642 continue;
643 case O(MANAGE,'w'):
644 if (readonly > 0) {
645 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
646 exit(2);
647 }
648 readonly = -1;
649 continue;
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;
667 case O(MISC,'t'):
668 test = 1;
669 continue;
670
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;
678 }
679 /* We have now processed all the valid options. Anything else is
680 * an error
681 */
682 fprintf(stderr, Name ": option %c not valid in %s mode\n",
683 opt, map_num(modes, mode));
684 exit(2);
685
686 }
687
688 if (!mode && devs_found) {
689 mode = MISC;
690 devmode = 'Q';
691 if (devlist->disposition == 0)
692 devlist->disposition = devmode;
693 }
694 if (!mode) {
695 fputs(Usage, stderr);
696 exit(2);
697 }
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 *
702 * That is mosty checked in the per-mode stuff but...
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.
706 */
707
708 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
709 (mode == ASSEMBLE && ! scan)) {
710 if (devs_found < 1) {
711 fprintf(stderr, Name ": an md device must be given in this mode\n");
712 exit(2);
713 }
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);
719 if (mdfd < 0)
720 exit(1);
721 if ((int)ident.super_minor == -2) {
722 struct stat stb;
723 fstat(mdfd, &stb);
724 ident.super_minor = minor(stb.st_rdev);
725 }
726 }
727
728 rv = 0;
729 switch(mode) {
730 case MANAGE:
731 /* readonly, add/remove, readwrite, runstop */
732 if (readonly>0)
733 rv = Manage_ro(devlist->devname, mdfd, readonly);
734 if (!rv && devs_found>1)
735 rv = Manage_subdevs(devlist->devname, mdfd,
736 devlist->next);
737 if (!rv && readonly < 0)
738 rv = Manage_ro(devlist->devname, mdfd, readonly);
739 if (!rv && runstop)
740 rv = Manage_runstop(devlist->devname, mdfd, runstop);
741 break;
742 case ASSEMBLE:
743 if (devs_found == 1 && ident.uuid_set == 0 &&
744 ident.super_minor == UnSet && !scan ) {
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);
747 if (array_ident == NULL) {
748 fprintf(stderr, Name ": %s not identified in config file.\n",
749 devlist->devname);
750 rv |= 1;
751 } else {
752 mdfd = open_mddev(devlist->devname, array_ident->autof);
753 if (mdfd < 0)
754 rv |= 1;
755 else {
756 rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile,
757 NULL,
758 readonly, runstop, update, verbose, force);
759 close(mdfd);
760 }
761 }
762 } else if (!scan)
763 rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile,
764 devlist->next,
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 }
771 for (dv = devlist ; dv ; dv=dv->next) {
772 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
773 if (array_ident == NULL) {
774 fprintf(stderr, Name ": %s not identified in config file.\n",
775 dv->devname);
776 rv |= 1;
777 continue;
778 }
779 mdfd = open_mddev(dv->devname, array_ident->autof);
780 if (mdfd < 0) {
781 rv |= 1;
782 continue;
783 }
784 rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile,
785 NULL,
786 readonly, runstop, update, verbose, force);
787 close(mdfd);
788 }
789 } else {
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) {
796 mdu_array_info_t array;
797 mdfd = open_mddev(array_list->devname, array_list->autof);
798 if (mdfd < 0) {
799 rv |= 1;
800 continue;
801 }
802 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
803 /* already assembled, skip */
804 ;
805 else
806 rv |= Assemble(ss, array_list->devname, mdfd,
807 array_list, configfile,
808 NULL,
809 readonly, runstop, NULL, verbose, force);
810 close(mdfd);
811 }
812 }
813 break;
814 case BUILD:
815 rv = Build(devlist->devname, mdfd, chunk, level, layout, raiddisks, devlist->next, assume_clean);
816 break;
817 case CREATE:
818 if (ss == NULL) {
819 for(i=0; !ss && superlist[i]; i++)
820 ss = superlist[i]->match_metadata_desc("default");
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,
828 raiddisks, sparedisks,
829 devs_found-1, devlist->next, runstop, verbose, force);
830 break;
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) {
841 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
842 exit(1);
843 }
844 rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust, ss);
845 } else {
846 if (devlist == NULL) {
847 if ((devmode == 'S' ||devmode=='D') && scan) {
848 /* apply to all devices in /proc/mdstat */
849 struct mdstat_ent *ms = mdstat_read(0);
850 struct mdstat_ent *e;
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 }
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')
870 rv |= Detail(name, !verbose, test);
871 else if (devmode=='S') {
872 mdfd = open_mddev(name, 0);
873 if (mdfd >= 0) {
874 rv |= Manage_runstop(name, mdfd, -1);
875 close(mdfd);
876 }
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':
888 rv |= Detail(dv->devname, brief, test); continue;
889 case 'K': /* Zero superblock */
890 rv |= Kill(dv->devname, force); continue;
891 case 'Q':
892 rv |= Query(dv->devname); continue;
893 }
894 mdfd = open_mddev(dv->devname, 0);
895 if (mdfd>=0) {
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 }
906 close(mdfd);
907 }
908 }
909 }
910 break;
911 case MONITOR:
912 if (!devlist && !scan) {
913 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
914 rv = 1;
915 break;
916 }
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 }
922 rv= Monitor(devlist, mailaddr, program,
923 delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile);
924 break;
925
926 case GROW:
927 if (devs_found > 1) {
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 }
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");
942 rv = 1;
943 break;
944 } else if (layout != UnSet)
945 rv = Manage_reconfig(devlist->devname, mdfd, layout);
946 else if (size >= 0 || raiddisks)
947 rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
948 else
949 fprintf(stderr, Name ": no changes to --grow\n");
950 break;
951 }
952 exit(rv);
953 }