]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Add a 'super-switch' so that different format superblocks can be used.
[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 superswitch *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; superlist[i]; i++)
285 if (superlist[i]->match_metadata_desc(optarg)) {
286 ss = superlist[i];
287 break;
288 }
289 if (!ss) {
290 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
291 exit(2);
292 }
293 continue;
294
295 case O(GROW,'z'):
296 case O(CREATE,'z'): /* size */
297 if (size >= 0) {
298 fprintf(stderr, Name ": size may only be specified once. "
299 "Second value is %s.\n", optarg);
300 exit(2);
301 }
302 if (strcmp(optarg, "max")==0)
303 size = 0;
304 else {
305 size = strtol(optarg, &c, 10);
306 if (!optarg[0] || *c || size < 4) {
307 fprintf(stderr, Name ": invalid size: %s\n",
308 optarg);
309 exit(2);
310 }
311 }
312 continue;
313
314 case O(GROW,'l'): /* hack - needed to understand layout */
315 case O(CREATE,'l'):
316 case O(BUILD,'l'): /* set raid level*/
317 if (level != UnSet) {
318 fprintf(stderr, Name ": raid level may only be set once. "
319 "Second value is %s.\n", optarg);
320 exit(2);
321 }
322 level = map_name(pers, optarg);
323 if (level == UnSet) {
324 fprintf(stderr, Name ": invalid raid level: %s\n",
325 optarg);
326 exit(2);
327 }
328 if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
329 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
330 optarg);
331 exit(2);
332 }
333 if (sparedisks > 0 && level < 1 && level >= -1) {
334 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
335 optarg);
336 exit(2);
337 }
338 ident.level = level;
339 continue;
340
341 case O(CREATE,'p'): /* raid5 layout */
342 case O(BUILD,'p'): /* faulty layout */
343 case O(GROW, 'p'): /* faulty reconfig */
344 if (layout != UnSet) {
345 fprintf(stderr,Name ": layout may only be sent once. "
346 "Second value was %s\n", optarg);
347 exit(2);
348 }
349 switch(level) {
350 default:
351 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
352 map_num(pers, level));
353 exit(2);
354 case UnSet:
355 fprintf(stderr, Name ": raid level must be given before layout.\n");
356 exit(2);
357
358 case 5:
359 case 6:
360 layout = map_name(r5layout, optarg);
361 if (layout==UnSet) {
362 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
363 optarg);
364 exit(2);
365 }
366 break;
367
368 case 10:
369 /* 'f' or 'n' followed by a number <= raid_disks */
370 if ((optarg[0] != 'n' && optarg[0] != 'f') ||
371 (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
372 copies > 200 ||
373 *cp) {
374 fprintf(stderr, Name ": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg);
375 exit(2);
376 }
377 if (optarg[0] == 'n')
378 layout = 256 + copies;
379 else
380 layout = 1 + (copies<<8);
381 break;
382 case -5: /* Faulty
383 * modeNNN
384 */
385
386 {
387 int ln = strcspn(optarg, "0123456789");
388 char *m = strdup(optarg);
389 int mode;
390 m[ln] = 0;
391 mode = map_name(faultylayout, m);
392 if (mode == UnSet) {
393 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
394 optarg);
395 exit(2);
396 }
397 layout = mode | (atoi(optarg+ln)<< ModeShift);
398 }
399 }
400 continue;
401
402 case O(CREATE,3):
403 case O(BUILD,3): /* assume clean */
404 assume_clean = 1;
405 continue;
406
407 case O(GROW,'n'):
408 case O(CREATE,'n'):
409 case O(BUILD,'n'): /* number of raid disks */
410 if (raiddisks) {
411 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
412 raiddisks, optarg);
413 exit(2);
414 }
415 raiddisks = strtol(optarg, &c, 10);
416 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
417 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
418 optarg);
419 exit(2);
420 }
421 if (raiddisks == 1 && !force && level != -5) {
422 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
423 " a mistake. If you really mean it you will need to specify --force before\n"
424 " setting the number of drives.\n");
425 exit(2);
426 }
427 ident.raid_disks = raiddisks;
428 continue;
429
430 case O(CREATE,'x'): /* number of spare (eXtra) discs */
431 if (sparedisks) {
432 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
433 sparedisks, optarg);
434 exit(2);
435 }
436 if (level != UnSet && level <= 0 && level >= -1) {
437 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
438 level);
439 exit(2);
440 }
441 sparedisks = strtol(optarg, &c, 10);
442 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
443 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
444 optarg);
445 exit(2);
446 }
447 continue;
448
449 case O(CREATE,'a'):
450 case O(BUILD,'a'):
451 case O(ASSEMBLE,'a'): /* auto-creation of device node */
452 if (optarg == NULL)
453 autof = -2;
454 else if (strcasecmp(optarg,"no")==0)
455 autof = 0;
456 else if (strcasecmp(optarg,"yes")==0)
457 autof = -2;
458 else if (strcasecmp(optarg,"md")==0)
459 autof = -1;
460 else {
461 /* There might be digits, and maybe a hypen, at the end */
462 char *e = optarg + strlen(optarg);
463 int num = 4;
464 int len;
465 while (e > optarg && isdigit(e[-1]))
466 e--;
467 if (*e) {
468 num = atoi(e);
469 if (num <= 0) num = 1;
470 }
471 if (e > optarg && e[-1] == '-')
472 e--;
473 len = e - optarg;
474 if ((len == 3 && strncasecmp(optarg,"mdp",3)==0) ||
475 (len == 1 && strncasecmp(optarg,"p",1)==0) ||
476 (len >= 4 && strncasecmp(optarg,"part",4)==0))
477 autof = num;
478 else {
479 fprintf(stderr, Name ": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
480 " optionally followed by a number.\n",
481 optarg);
482 exit(2);
483 }
484 }
485 continue;
486
487 case O(BUILD,'f'): /* force honouring '-n 1' */
488 case O(CREATE,'f'): /* force honouring of device list */
489 case O(ASSEMBLE,'f'): /* force assembly */
490 case O(MISC,'f'): /* force zero */
491 force=1;
492 continue;
493
494 /* now for the Assemble options */
495 case O(ASSEMBLE,'u'): /* uuid of array */
496 if (ident.uuid_set) {
497 fprintf(stderr, Name ": uuid cannot be set twice. "
498 "Second value %s.\n", optarg);
499 exit(2);
500 }
501 if (parse_uuid(optarg, ident.uuid))
502 ident.uuid_set = 1;
503 else {
504 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
505 exit(2);
506 }
507 continue;
508
509 case O(ASSEMBLE,'m'): /* super-minor for array */
510 if (ident.super_minor != UnSet) {
511 fprintf(stderr, Name ": super-minor cannot be set twice. "
512 "Second value: %s.\n", optarg);
513 exit(2);
514 }
515 if (strcmp(optarg, "dev")==0)
516 ident.super_minor = -2;
517 else {
518 ident.super_minor = strtoul(optarg, &cp, 10);
519 if (!optarg[0] || *cp) {
520 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
521 exit(2);
522 }
523 }
524 continue;
525
526 case O(ASSEMBLE,'U'): /* update the superblock */
527 if (update) {
528 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
529 update, optarg);
530 exit(2);
531 }
532 update = optarg;
533 if (strcmp(update, "sparc2.2")==0)
534 continue;
535 if (strcmp(update, "super-minor") == 0)
536 continue;
537 if (strcmp(update, "summaries")==0)
538 continue;
539 if (strcmp(update, "resync")==0)
540 continue;
541 fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update);
542 exit(2);
543
544 case O(ASSEMBLE,'c'): /* config file */
545 case O(MISC, 'c'):
546 case O(MONITOR,'c'):
547 if (configfile) {
548 fprintf(stderr, Name ": configfile cannot be set twice. "
549 "Second value is %s.\n", optarg);
550 exit(2);
551 }
552 configfile = optarg;
553 /* FIXME possibly check that config file exists. Even parse it */
554 continue;
555 case O(ASSEMBLE,'s'): /* scan */
556 case O(MISC,'s'):
557 case O(MONITOR,'s'):
558 scan = 1;
559 continue;
560
561 case O(MONITOR,'m'): /* mail address */
562 if (mailaddr)
563 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
564 optarg);
565 else
566 mailaddr = optarg;
567 continue;
568
569 case O(MONITOR,'p'): /* alert program */
570 if (program)
571 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
572 optarg);
573 else
574 program = optarg;
575 continue;
576
577 case O(MONITOR,'d'): /* delay in seconds */
578 if (delay)
579 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
580 optarg);
581 else {
582 delay = strtol(optarg, &c, 10);
583 if (!optarg[0] || *c || delay<1) {
584 fprintf(stderr, Name ": invalid delay: %s\n",
585 optarg);
586 exit(2);
587 }
588 }
589 continue;
590 case O(MONITOR,'f'): /* daemonise */
591 daemonise = 1;
592 continue;
593 case O(MONITOR,'i'): /* pid */
594 if (pidfile)
595 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
596 optarg);
597 else
598 pidfile = optarg;
599 continue;
600 case O(MONITOR,'1'): /* oneshot */
601 oneshot = 1;
602 continue;
603 case O(MONITOR,'t'): /* test */
604 test = 1;
605 continue;
606
607 /* now the general management options. Some are applicable
608 * to other modes. None have arguments.
609 */
610 case O(GROW,'a'):
611 case O(MANAGE,'a'): /* add a drive */
612 devmode = 'a';
613 continue;
614 case O(MANAGE,'r'): /* remove a drive */
615 devmode = 'r';
616 continue;
617 case O(MANAGE,'f'): /* set faulty */
618 devmode = 'f';
619 continue;
620 case O(MANAGE,'R'):
621 case O(ASSEMBLE,'R'):
622 case O(BUILD,'R'):
623 case O(CREATE,'R'): /* Run the array */
624 if (runstop < 0) {
625 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
626 exit(2);
627 }
628 runstop = 1;
629 continue;
630 case O(MANAGE,'S'):
631 if (runstop > 0) {
632 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
633 exit(2);
634 }
635 runstop = -1;
636 continue;
637
638 case O(MANAGE,'o'):
639 if (readonly < 0) {
640 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
641 exit(2);
642 }
643 readonly = 1;
644 continue;
645 case O(MANAGE,'w'):
646 if (readonly > 0) {
647 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
648 exit(2);
649 }
650 readonly = -1;
651 continue;
652
653 case O(MISC,'Q'):
654 case O(MISC,'D'):
655 case O(MISC,'E'):
656 case O(MISC,'K'):
657 case O(MISC,'R'):
658 case O(MISC,'S'):
659 case O(MISC,'o'):
660 case O(MISC,'w'):
661 if (devmode && devmode != opt &&
662 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
663 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
664 devmode =='E'?opt:devmode);
665 exit(2);
666 }
667 devmode = opt;
668 continue;
669 case O(MISC,'t'):
670 test = 1;
671 continue;
672
673 case O(MISC, 22):
674 if (devmode != 'E') {
675 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
676 exit(2);
677 }
678 SparcAdjust = 1;
679 continue;
680 }
681 /* We have now processed all the valid options. Anything else is
682 * an error
683 */
684 fprintf(stderr, Name ": option %c not valid in %s mode\n",
685 opt, map_num(modes, mode));
686 exit(2);
687
688 }
689
690 if (!mode && devs_found) {
691 mode = MISC;
692 devmode = 'Q';
693 if (devlist->disposition == 0)
694 devlist->disposition = devmode;
695 }
696 if (!mode) {
697 fputs(Usage, stderr);
698 exit(2);
699 }
700 /* Ok, got the option parsing out of the way
701 * hopefully it's mostly right but there might be some stuff
702 * missing
703 *
704 * That is mosty checked in the per-mode stuff but...
705 *
706 * For @,B,C and A without -s, the first device listed must be an md device
707 * we check that here and open it.
708 */
709
710 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
711 (mode == ASSEMBLE && ! scan)) {
712 if (devs_found < 1) {
713 fprintf(stderr, Name ": an md device must be given in this mode\n");
714 exit(2);
715 }
716 if ((int)ident.super_minor == -2 && autof) {
717 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
718 exit(2);
719 }
720 mdfd = open_mddev(devlist->devname, autof);
721 if (mdfd < 0)
722 exit(1);
723 if ((int)ident.super_minor == -2) {
724 struct stat stb;
725 fstat(mdfd, &stb);
726 ident.super_minor = minor(stb.st_rdev);
727 }
728 }
729
730 rv = 0;
731 switch(mode) {
732 case MANAGE:
733 /* readonly, add/remove, readwrite, runstop */
734 if (readonly>0)
735 rv = Manage_ro(devlist->devname, mdfd, readonly);
736 if (!rv && devs_found>1)
737 rv = Manage_subdevs(devlist->devname, mdfd,
738 devlist->next);
739 if (!rv && readonly < 0)
740 rv = Manage_ro(devlist->devname, mdfd, readonly);
741 if (!rv && runstop)
742 rv = Manage_runstop(devlist->devname, mdfd, runstop);
743 break;
744 case ASSEMBLE:
745 if (devs_found == 1 && ident.uuid_set == 0 &&
746 ident.super_minor == UnSet && !scan ) {
747 /* Only a device has been given, so get details from config file */
748 mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
749 if (array_ident == NULL) {
750 fprintf(stderr, Name ": %s not identified in config file.\n",
751 devlist->devname);
752 rv |= 1;
753 } else {
754 mdfd = open_mddev(devlist->devname, array_ident->autof);
755 if (mdfd < 0)
756 rv |= 1;
757 else {
758 rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile,
759 NULL,
760 readonly, runstop, update, verbose, force);
761 close(mdfd);
762 }
763 }
764 } else if (!scan)
765 rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile,
766 devlist->next,
767 readonly, runstop, update, verbose, force);
768 else if (devs_found>0) {
769 if (update && devs_found > 1) {
770 fprintf(stderr, Name ": can only update a single array at a time\n");
771 exit(1);
772 }
773 for (dv = devlist ; dv ; dv=dv->next) {
774 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
775 if (array_ident == NULL) {
776 fprintf(stderr, Name ": %s not identified in config file.\n",
777 dv->devname);
778 rv |= 1;
779 continue;
780 }
781 mdfd = open_mddev(dv->devname, array_ident->autof);
782 if (mdfd < 0) {
783 rv |= 1;
784 continue;
785 }
786 rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile,
787 NULL,
788 readonly, runstop, update, verbose, force);
789 close(mdfd);
790 }
791 } else {
792 mddev_ident_t array_list = conf_get_ident(configfile, NULL);
793 if (!array_list) {
794 fprintf(stderr, Name ": No arrays found in config file\n");
795 rv = 1;
796 } else
797 for (; array_list; array_list = array_list->next) {
798 mdu_array_info_t array;
799 mdfd = open_mddev(array_list->devname, array_list->autof);
800 if (mdfd < 0) {
801 rv |= 1;
802 continue;
803 }
804 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
805 /* already assembled, skip */
806 ;
807 else
808 rv |= Assemble(ss, array_list->devname, mdfd,
809 array_list, configfile,
810 NULL,
811 readonly, runstop, NULL, verbose, force);
812 close(mdfd);
813 }
814 }
815 break;
816 case BUILD:
817 rv = Build(devlist->devname, mdfd, chunk, level, layout, raiddisks, devlist->next, assume_clean);
818 break;
819 case CREATE:
820 if (ss == NULL) {
821 for(i=0; superlist[i]; i++)
822 if (superlist[i]->match_metadata_desc("default")) {
823 ss = superlist[i];
824 break;
825 }
826 }
827 if (!ss) {
828 fprintf(stderr, Name ": internal error - no default metadata style\n");
829 exit(2);
830 }
831
832 rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
833 raiddisks, sparedisks,
834 devs_found-1, devlist->next, runstop, verbose, force);
835 break;
836 case MISC:
837
838 if (devmode == 'E') {
839 if (devlist == NULL && !scan) {
840 fprintf(stderr, Name ": No devices to examine\n");
841 exit(2);
842 }
843 if (devlist == NULL)
844 devlist = conf_get_devs(configfile);
845 if (devlist == NULL) {
846 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
847 exit(1);
848 }
849 rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust);
850 } else {
851 if (devlist == NULL) {
852 if ((devmode == 'S' ||devmode=='D') && scan) {
853 /* apply to all devices in /proc/mdstat */
854 struct mdstat_ent *ms = mdstat_read(0);
855 struct mdstat_ent *e;
856 if (devmode == 'S') {
857 /* reverse order so that arrays made of arrays are stopped properly */
858 struct mdstat_ent *sm = NULL;
859 while ((e=ms) != NULL) {
860 ms = e->next;
861 e->next = sm;
862 sm = e;
863 }
864 ms = sm;
865 }
866 for (e=ms ; e ; e=e->next) {
867 char *name = get_md_name(e->devnum);
868
869 if (!name) {
870 fprintf(stderr, Name ": cannot find device file for %s\n",
871 e->dev);
872 continue;
873 }
874 if (devmode == 'D')
875 rv |= Detail(name, !verbose, test);
876 else if (devmode=='S') {
877 mdfd = open_mddev(name, 0);
878 if (mdfd >= 0) {
879 rv |= Manage_runstop(name, mdfd, -1);
880 close(mdfd);
881 }
882 }
883 put_md_name(name);
884 }
885 } else {
886 fprintf(stderr, Name ": No devices given.\n");
887 exit(2);
888 }
889 }
890 for (dv=devlist ; dv; dv=dv->next) {
891 switch(dv->disposition) {
892 case 'D':
893 rv |= Detail(dv->devname, brief, test); continue;
894 case 'K': /* Zero superblock */
895 rv |= Kill(dv->devname, force); continue;
896 case 'Q':
897 rv |= Query(dv->devname); continue;
898 }
899 mdfd = open_mddev(dv->devname, 0);
900 if (mdfd>=0) {
901 switch(dv->disposition) {
902 case 'R':
903 rv |= Manage_runstop(dv->devname, mdfd, 1); break;
904 case 'S':
905 rv |= Manage_runstop(dv->devname, mdfd, -1); break;
906 case 'o':
907 rv |= Manage_ro(dv->devname, mdfd, 1); break;
908 case 'w':
909 rv |= Manage_ro(dv->devname, mdfd, -1); break;
910 }
911 close(mdfd);
912 }
913 }
914 }
915 break;
916 case MONITOR:
917 if (!devlist && !scan) {
918 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
919 rv = 1;
920 break;
921 }
922 if (pidfile && !daemonise) {
923 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
924 rv = 1;
925 break;
926 }
927 rv= Monitor(devlist, mailaddr, program,
928 delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile);
929 break;
930
931 case GROW:
932 if (devs_found > 1) {
933
934 /* must be '-a'. */
935 if (size >= 0 || raiddisks) {
936 fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
937 rv = 1;
938 break;
939 }
940 for (dv=devlist->next; dv ; dv=dv->next) {
941 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
942 if (rv)
943 break;
944 }
945 } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) > 1) {
946 fprintf(stderr, Name ": can change at most one of size, raiddisks, and layout\n");
947 rv = 1;
948 break;
949 } else if (layout != UnSet)
950 rv = Manage_reconfig(devlist->devname, mdfd, layout);
951 else if (size >= 0 || raiddisks)
952 rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
953 else
954 fprintf(stderr, Name ": no changes to --grow\n");
955 break;
956 }
957 exit(rv);
958 }