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