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