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