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