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