]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
Improve opt parsing, and distinguish long from short.
[thirdparty/mdadm.git] / mdadm.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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@suse.de>
23 *
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
26 */
27
28 #include "mdadm.h"
29 #include "md_p.h"
30 #include <ctype.h>
31
32
33 int main(int argc, char *argv[])
34 {
35 int mode = 0;
36 int opt;
37 int option_index;
38 char *c;
39 int rv;
40 int i;
41
42 int chunk = 0;
43 long long size = -1;
44 long long array_size = -1;
45 int level = UnSet;
46 int layout = UnSet;
47 char *layout_str = NULL;
48 int raiddisks = 0;
49 int max_disks = MD_SB_DISKS; /* just a default */
50 int sparedisks = 0;
51 struct mddev_ident ident;
52 char *configfile = NULL;
53 char *cp;
54 char *update = NULL;
55 int scan = 0;
56 int devmode = 0;
57 int runstop = 0;
58 int readonly = 0;
59 int write_behind = 0;
60 int bitmap_fd = -1;
61 char *bitmap_file = NULL;
62 char *backup_file = NULL;
63 int bitmap_chunk = UnSet;
64 int SparcAdjust = 0;
65 struct mddev_dev *devlist = NULL;
66 struct mddev_dev **devlistend = & devlist;
67 struct mddev_dev *dv;
68 int devs_found = 0;
69 int verbose = 0;
70 int quiet = 0;
71 int brief = 0;
72 int force = 0;
73 int test = 0;
74 int export = 0;
75 int assume_clean = 0;
76 char *symlinks = NULL;
77 /* autof indicates whether and how to create device node.
78 * bottom 3 bits are style. Rest (when shifted) are number of parts
79 * 0 - unset
80 * 1 - don't create (no)
81 * 2 - if is_standard, then create (yes)
82 * 3 - create as 'md' - reject is_standard mdp (md)
83 * 4 - create as 'mdp' - reject is_standard md (mdp)
84 * 5 - default to md if not is_standard (md in config file)
85 * 6 - default to mdp if not is_standard (part, or mdp in config file)
86 */
87 int autof = 0;
88
89 char *homehost = NULL;
90 char sys_hostname[256];
91 int require_homehost = 1;
92 char *mailaddr = NULL;
93 char *program = NULL;
94 int increments = 20;
95 int delay = 0;
96 int daemonise = 0;
97 char *pidfile = NULL;
98 int oneshot = 0;
99 int spare_sharing = 1;
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 char *subarray = NULL;
108 char *remove_path = NULL;
109
110 int print_help = 0;
111 FILE *outf;
112
113 int mdfd = -1;
114
115 srandom(time(0) ^ getpid());
116
117 ident.uuid_set=0;
118 ident.level = UnSet;
119 ident.raid_disks = UnSet;
120 ident.super_minor= UnSet;
121 ident.devices=0;
122 ident.spare_group = NULL;
123 ident.autof = 0;
124 ident.st = NULL;
125 ident.bitmap_fd = -1;
126 ident.bitmap_file = NULL;
127 ident.name[0] = 0;
128 ident.container = NULL;
129 ident.member = NULL;
130
131 while ((option_index = -1) ,
132 (opt=getopt_long(argc, argv,
133 shortopt, long_options,
134 &option_index)) != -1) {
135 int newmode = mode;
136 /* firstly, some mode-independent options */
137 switch(opt) {
138 case HelpOptions:
139 print_help = 2;
140 continue;
141 case 'h':
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
157 || mode == GROW || mode == INCREMENTAL
158 || mode == MANAGE)
159 break; /* b means bitmap */
160 case Brief:
161 brief = 1;
162 continue;
163
164 case 'Y': export++;
165 continue;
166
167 case HomeHost:
168 if (strcasecmp(optarg, "<ignore>") == 0)
169 require_homehost = 0;
170 else
171 homehost = optarg;
172 continue;
173
174 case ':':
175 case '?':
176 fputs(Usage, stderr);
177 exit(2);
178 }
179 /* second, figure out the mode.
180 * Some options force the mode. Others
181 * set the mode if it isn't already
182 */
183
184 switch(opt) {
185 case ManageOpt:
186 newmode = MANAGE;
187 shortopt = short_bitmap_options;
188 break;
189 case 'a':
190 case Add:
191 case 'r':
192 case Remove:
193 case 'f':
194 case Fail:
195 case ReAdd: /* re-add */
196 if (!mode) {
197 newmode = MANAGE;
198 shortopt = short_bitmap_options;
199 }
200 break;
201
202 case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break;
203 case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break;
204 case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break;
205 case 'F': newmode = MONITOR;break;
206 case 'G': newmode = GROW;
207 shortopt = short_bitmap_options;
208 break;
209 case 'I': newmode = INCREMENTAL;
210 shortopt = short_bitmap_auto_options; break;
211 case AutoDetect:
212 newmode = AUTODETECT; break;
213
214 case MiscOpt:
215 case 'D':
216 case 'E':
217 case 'X':
218 case 'Q': newmode = MISC; break;
219 case 'R':
220 case 'S':
221 case 'o':
222 case 'w':
223 case 'W':
224 case WaitOpt:
225 case Waitclean:
226 case DetailPlatform:
227 case KillSubarray:
228 case UpdateSubarray:
229 if (opt == KillSubarray || opt == UpdateSubarray) {
230 if (subarray) {
231 fprintf(stderr, Name ": subarray can only"
232 " be specified once\n");
233 exit(2);
234 }
235 subarray = optarg;
236 }
237 case 'K': if (!mode) newmode = MISC; break;
238 case NoSharing: newmode = MONITOR; break;
239 }
240 if (mode && newmode == mode) {
241 /* everybody happy ! */
242 } else if (mode && newmode != mode) {
243 /* not allowed.. */
244 fprintf(stderr, Name ": ");
245 if (option_index >= 0)
246 fprintf(stderr, "--%s", long_options[option_index].name);
247 else
248 fprintf(stderr, "-%c", opt);
249 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
250 map_num(modes, newmode),
251 map_num(modes, mode));
252 exit(2);
253 } else if (!mode && newmode) {
254 mode = newmode;
255 } else {
256 /* special case of -c --help */
257 if ((opt == 'c' || opt == ConfigFile) &&
258 ( strncmp(optarg, "--h", 3)==0 ||
259 strncmp(optarg, "-h", 2)==0)) {
260 fputs(Help_config, stdout);
261 exit(0);
262 }
263
264 /* If first option is a device, don't force the mode yet */
265 if (opt == 1) {
266 if (devs_found == 0) {
267 dv = malloc(sizeof(*dv));
268 if (dv == NULL) {
269 fprintf(stderr, Name ": malloc failed\n");
270 exit(3);
271 }
272 dv->devname = optarg;
273 dv->disposition = devmode;
274 dv->writemostly = writemostly;
275 dv->re_add = re_add;
276 dv->used = 0;
277 dv->next = NULL;
278 *devlistend = dv;
279 devlistend = &dv->next;
280
281 devs_found++;
282 continue;
283 }
284 /* No mode yet, and this is the second device ... */
285 fprintf(stderr, Name ": An option must be given to set the mode before a second device\n"
286 " (%s) is listed\n", optarg);
287 exit(2);
288 }
289 if (option_index >= 0)
290 fprintf(stderr, Name ": --%s", long_options[option_index].name);
291 else
292 fprintf(stderr, Name ": -%c", opt);
293 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
294 exit(2);
295 }
296
297 /* if we just set the mode, then done */
298 switch(opt) {
299 case ManageOpt:
300 case MiscOpt:
301 case 'A':
302 case 'B':
303 case 'C':
304 case 'F':
305 case 'G':
306 case 'I':
307 case AutoDetect:
308 continue;
309 }
310 if (opt == 1) {
311 /* an undecorated option - must be a device name.
312 */
313 if (devs_found > 0 && mode == MANAGE && !devmode) {
314 fprintf(stderr, Name ": Must give one of -a/-r/-f"
315 " for subsequent devices at %s\n", optarg);
316 exit(2);
317 }
318 if (devs_found > 0 && mode == GROW && !devmode) {
319 fprintf(stderr, Name ": Must give -a/--add for"
320 " devices to add: %s\n", optarg);
321 exit(2);
322 }
323 dv = malloc(sizeof(*dv));
324 if (dv == NULL) {
325 fprintf(stderr, Name ": malloc failed\n");
326 exit(3);
327 }
328 dv->devname = optarg;
329 dv->disposition = devmode;
330 dv->writemostly = writemostly;
331 dv->re_add = re_add;
332 dv->used = 0;
333 dv->next = NULL;
334 *devlistend = dv;
335 devlistend = &dv->next;
336
337 devs_found++;
338 continue;
339 }
340
341 /* We've got a mode, and opt is now something else which
342 * could depend on the mode */
343 #define O(a,b) ((a<<16)|b)
344 switch (O(mode,opt)) {
345 case O(GROW,'c'):
346 case O(GROW,ChunkSize):
347 case O(CREATE,'c'):
348 case O(CREATE,ChunkSize):
349 case O(BUILD,'c'): /* chunk or rounding */
350 case O(BUILD,ChunkSize): /* chunk or rounding */
351 if (chunk) {
352 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
353 "Second value is %s.\n", optarg);
354 exit(2);
355 }
356 chunk = strtol(optarg, &c, 10);
357 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
358 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
359 optarg);
360 exit(2);
361 }
362 continue;
363
364 #if 0
365 case O(ASSEMBLE,AutoHomeHost):
366 auto_update_home = 1;
367 continue;
368 #endif
369 case O(INCREMENTAL, 'e'):
370 case O(CREATE,'e'):
371 case O(ASSEMBLE,'e'):
372 case O(MISC,'e'): /* set metadata (superblock) information */
373 if (ss) {
374 fprintf(stderr, Name ": metadata information already given\n");
375 exit(2);
376 }
377 for(i=0; !ss && superlist[i]; i++)
378 ss = superlist[i]->match_metadata_desc(optarg);
379
380 if (!ss) {
381 fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg);
382 exit(2);
383 }
384 max_disks = ss->max_devs;
385 continue;
386
387 case O(MANAGE,'W'):
388 case O(MANAGE,WriteMostly):
389 case O(BUILD,'W'):
390 case O(BUILD,WriteMostly):
391 case O(CREATE,'W'):
392 case O(CREATE,WriteMostly):
393 /* set write-mostly for following devices */
394 writemostly = 1;
395 continue;
396
397 case O(MANAGE,'w'):
398 /* clear write-mostly for following devices */
399 writemostly = 2;
400 continue;
401
402
403 case O(GROW,'z'):
404 case O(CREATE,'z'):
405 case O(BUILD,'z'): /* size */
406 if (size >= 0) {
407 fprintf(stderr, Name ": size may only be specified once. "
408 "Second value is %s.\n", optarg);
409 exit(2);
410 }
411 if (strcmp(optarg, "max")==0)
412 size = 0;
413 else {
414 size = parse_size(optarg);
415 if (size < 8) {
416 fprintf(stderr, Name ": invalid size: %s\n",
417 optarg);
418 exit(2);
419 }
420 /* convert sectors to K */
421 size /= 2;
422 }
423 continue;
424
425 case O(GROW,'Z'): /* array size */
426 if (array_size >= 0) {
427 fprintf(stderr, Name ": array-size may only be specified once. "
428 "Second value is %s.\n", optarg);
429 exit(2);
430 }
431 if (strcmp(optarg, "max") == 0)
432 array_size = 0;
433 else {
434 array_size = parse_size(optarg);
435 if (array_size <= 0) {
436 fprintf(stderr, Name ": invalid array size: %s\n",
437 optarg);
438 exit(2);
439 }
440 }
441 continue;
442
443 case O(GROW,'l'):
444 case O(CREATE,'l'):
445 case O(BUILD,'l'): /* set raid level*/
446 if (level != UnSet) {
447 fprintf(stderr, Name ": raid level may only be set once. "
448 "Second value is %s.\n", optarg);
449 exit(2);
450 }
451 level = map_name(pers, optarg);
452 if (level == UnSet) {
453 fprintf(stderr, Name ": invalid raid level: %s\n",
454 optarg);
455 exit(2);
456 }
457 if (level != 0 && level != LEVEL_LINEAR && level != 1 &&
458 level != LEVEL_MULTIPATH && level != LEVEL_FAULTY &&
459 level != 10 &&
460 mode == BUILD) {
461 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
462 optarg);
463 exit(2);
464 }
465 if (sparedisks > 0 && level < 1 && level >= -1) {
466 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
467 optarg);
468 exit(2);
469 }
470 ident.level = level;
471 continue;
472
473 case O(GROW, 'p'): /* new layout */
474 case O(GROW, Layout):
475 if (layout_str) {
476 fprintf(stderr,Name ": layout may only be sent once. "
477 "Second value was %s\n", optarg);
478 exit(2);
479 }
480 layout_str = optarg;
481 /* 'Grow' will parse the value */
482 continue;
483
484 case O(CREATE,'p'): /* raid5 layout */
485 case O(CREATE,Layout):
486 case O(BUILD,'p'): /* faulty layout */
487 case O(BUILD,Layout):
488 if (layout != UnSet) {
489 fprintf(stderr,Name ": layout may only be sent once. "
490 "Second value was %s\n", optarg);
491 exit(2);
492 }
493 switch(level) {
494 default:
495 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
496 map_num(pers, level));
497 exit(2);
498 case UnSet:
499 fprintf(stderr, Name ": raid level must be given before layout.\n");
500 exit(2);
501
502 case 5:
503 layout = map_name(r5layout, optarg);
504 if (layout==UnSet) {
505 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
506 optarg);
507 exit(2);
508 }
509 break;
510 case 6:
511 layout = map_name(r6layout, optarg);
512 if (layout==UnSet) {
513 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
514 optarg);
515 exit(2);
516 }
517 break;
518
519 case 10:
520 layout = parse_layout_10(optarg);
521 if (layout < 0) {
522 fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
523 exit(2);
524 }
525 break;
526 case LEVEL_FAULTY:
527 /* Faulty
528 * modeNNN
529 */
530 layout = parse_layout_faulty(optarg);
531 if (layout == -1) {
532 fprintf(stderr, Name ": layout %s not understood for faulty.\n",
533 optarg);
534 exit(2);
535 }
536 break;
537 }
538 continue;
539
540 case O(CREATE,AssumeClean):
541 case O(BUILD,AssumeClean): /* assume clean */
542 assume_clean = 1;
543 continue;
544
545 case O(GROW,'n'):
546 case O(CREATE,'n'):
547 case O(BUILD,'n'): /* number of raid disks */
548 if (raiddisks) {
549 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
550 raiddisks, optarg);
551 exit(2);
552 }
553 raiddisks = strtol(optarg, &c, 10);
554 if (!optarg[0] || *c || raiddisks<=0) {
555 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
556 optarg);
557 exit(2);
558 }
559 ident.raid_disks = raiddisks;
560 continue;
561
562 case O(CREATE,'x'): /* number of spare (eXtra) discs */
563 if (sparedisks) {
564 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
565 sparedisks, optarg);
566 exit(2);
567 }
568 if (level != UnSet && level <= 0 && level >= -1) {
569 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
570 level);
571 exit(2);
572 }
573 sparedisks = strtol(optarg, &c, 10);
574 if (!optarg[0] || *c || sparedisks < 0) {
575 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
576 optarg);
577 exit(2);
578 }
579 continue;
580
581 case O(CREATE,'a'):
582 case O(CREATE,Auto):
583 case O(BUILD,'a'):
584 case O(BUILD,Auto):
585 case O(INCREMENTAL,'a'):
586 case O(INCREMENTAL,Auto):
587 case O(ASSEMBLE,'a'):
588 case O(ASSEMBLE,Auto): /* auto-creation of device node */
589 autof = parse_auto(optarg, "--auto flag", 0);
590 continue;
591
592 case O(CREATE,Symlinks):
593 case O(BUILD,Symlinks):
594 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
595 symlinks = optarg;
596 continue;
597
598 case O(BUILD,'f'): /* force honouring '-n 1' */
599 case O(BUILD,Force): /* force honouring '-n 1' */
600 case O(GROW,'f'): /* ditto */
601 case O(GROW,Force): /* ditto */
602 case O(CREATE,'f'): /* force honouring of device list */
603 case O(CREATE,Force): /* force honouring of device list */
604 case O(ASSEMBLE,'f'): /* force assembly */
605 case O(ASSEMBLE,Force): /* force assembly */
606 case O(MISC,'f'): /* force zero */
607 case O(MISC,Force): /* force zero */
608 force=1;
609 continue;
610
611 /* now for the Assemble options */
612 case O(CREATE,'u'): /* uuid of array */
613 case O(ASSEMBLE,'u'): /* uuid of array */
614 if (ident.uuid_set) {
615 fprintf(stderr, Name ": uuid cannot be set twice. "
616 "Second value %s.\n", optarg);
617 exit(2);
618 }
619 if (parse_uuid(optarg, ident.uuid))
620 ident.uuid_set = 1;
621 else {
622 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
623 exit(2);
624 }
625 continue;
626
627 case O(CREATE,'N'):
628 case O(ASSEMBLE,'N'):
629 case O(MISC,'N'):
630 if (ident.name[0]) {
631 fprintf(stderr, Name ": name cannot be set twice. "
632 "Second value %s.\n", optarg);
633 exit(2);
634 }
635 if (mode == MISC && !subarray) {
636 fprintf(stderr, Name ": -N/--name only valid with --update-subarray in misc mode\n");
637 exit(2);
638 }
639 if (strlen(optarg) > 32) {
640 fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n",
641 optarg);
642 exit(2);
643 }
644 strcpy(ident.name, optarg);
645 continue;
646
647 case O(ASSEMBLE,'m'): /* super-minor for array */
648 case O(ASSEMBLE,SuperMinor):
649 if (ident.super_minor != UnSet) {
650 fprintf(stderr, Name ": super-minor cannot be set twice. "
651 "Second value: %s.\n", optarg);
652 exit(2);
653 }
654 if (strcmp(optarg, "dev")==0)
655 ident.super_minor = -2;
656 else {
657 ident.super_minor = strtoul(optarg, &cp, 10);
658 if (!optarg[0] || *cp) {
659 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
660 exit(2);
661 }
662 }
663 continue;
664
665 case O(ASSEMBLE,'U'): /* update the superblock */
666 case O(MISC,'U'):
667 if (update) {
668 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
669 update, optarg);
670 exit(2);
671 }
672 if (mode == MISC && !subarray) {
673 fprintf(stderr, Name ": Only subarrays can be updated in misc mode\n");
674 exit(2);
675 }
676 update = optarg;
677 if (strcmp(update, "sparc2.2")==0)
678 continue;
679 if (strcmp(update, "super-minor") == 0)
680 continue;
681 if (strcmp(update, "summaries")==0)
682 continue;
683 if (strcmp(update, "resync")==0)
684 continue;
685 if (strcmp(update, "uuid")==0)
686 continue;
687 if (strcmp(update, "name")==0)
688 continue;
689 if (strcmp(update, "homehost")==0)
690 continue;
691 if (strcmp(update, "devicesize")==0)
692 continue;
693 if (strcmp(update, "byteorder")==0) {
694 if (ss) {
695 fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
696 exit(2);
697 }
698 for(i=0; !ss && superlist[i]; i++)
699 ss = superlist[i]->match_metadata_desc("0.swap");
700 if (!ss) {
701 fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
702 exit(2);
703 }
704
705 continue;
706 }
707 if (strcmp(update,"?") == 0 ||
708 strcmp(update, "help") == 0) {
709 outf = stdout;
710 fprintf(outf, Name ": ");
711 } else {
712 outf = stderr;
713 fprintf(outf,
714 Name ": '--update=%s' is invalid. ",
715 update);
716 }
717 fprintf(outf, "Valid --update options are:\n"
718 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
719 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
720 exit(outf == stdout ? 0 : 2);
721
722 case O(INCREMENTAL,NoDegraded):
723 fprintf(stderr, Name ": --no-degraded is deprecated in Incremental mode\n");
724 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
725 runstop = -1; /* --stop isn't allowed for --assemble,
726 * so we overload slightly */
727 continue;
728
729 case O(ASSEMBLE,'c'):
730 case O(ASSEMBLE,ConfigFile):
731 case O(INCREMENTAL, 'c'):
732 case O(INCREMENTAL, ConfigFile):
733 case O(MISC, 'c'):
734 case O(MISC, ConfigFile):
735 case O(MONITOR,'c'):
736 case O(MONITOR,ConfigFile):
737 if (configfile) {
738 fprintf(stderr, Name ": configfile cannot be set twice. "
739 "Second value is %s.\n", optarg);
740 exit(2);
741 }
742 configfile = optarg;
743 set_conffile(configfile);
744 /* FIXME possibly check that config file exists. Even parse it */
745 continue;
746 case O(ASSEMBLE,'s'): /* scan */
747 case O(MISC,'s'):
748 case O(MONITOR,'s'):
749 case O(INCREMENTAL,'s'):
750 scan = 1;
751 continue;
752
753 case O(MONITOR,'m'): /* mail address */
754 case O(MONITOR,EMail):
755 if (mailaddr)
756 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
757 optarg);
758 else
759 mailaddr = optarg;
760 continue;
761
762 case O(MONITOR,'p'): /* alert program */
763 case O(MONITOR,ProgramOpt): /* alert program */
764 if (program)
765 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
766 optarg);
767 else
768 program = optarg;
769 continue;
770
771 case O(MONITOR,'r'): /* rebuild increments */
772 case O(MONITOR,Increment):
773 increments = atoi(optarg);
774 if (increments>99 || increments<1) {
775 fprintf(stderr, Name ": please specify positive integer between 1 and 99 as rebuild increments.\n");
776 exit(2);
777 }
778 continue;
779
780 case O(MONITOR,'d'): /* delay in seconds */
781 case O(GROW, 'd'):
782 case O(BUILD,'d'): /* delay for bitmap updates */
783 case O(CREATE,'d'):
784 if (delay)
785 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
786 optarg);
787 else {
788 delay = strtol(optarg, &c, 10);
789 if (!optarg[0] || *c || delay<1) {
790 fprintf(stderr, Name ": invalid delay: %s\n",
791 optarg);
792 exit(2);
793 }
794 }
795 continue;
796 case O(MONITOR,'f'): /* daemonise */
797 case O(MONITOR,Fork):
798 daemonise = 1;
799 continue;
800 case O(MONITOR,'i'): /* pid */
801 if (pidfile)
802 fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
803 optarg);
804 else
805 pidfile = optarg;
806 continue;
807 case O(MONITOR,'1'): /* oneshot */
808 oneshot = 1;
809 continue;
810 case O(MONITOR,'t'): /* test */
811 test = 1;
812 continue;
813 case O(MONITOR,'y'): /* log messages to syslog */
814 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
815 dosyslog = 1;
816 continue;
817 case O(MONITOR, NoSharing):
818 spare_sharing = 0;
819 continue;
820 /* now the general management options. Some are applicable
821 * to other modes. None have arguments.
822 */
823 case O(GROW,'a'):
824 case O(GROW,Add):
825 case O(MANAGE,'a'):
826 case O(MANAGE,Add): /* add a drive */
827 devmode = 'a';
828 re_add = 0;
829 continue;
830 case O(MANAGE,ReAdd):
831 devmode = 'a';
832 re_add = 1;
833 continue;
834 case O(MANAGE,'r'): /* remove a drive */
835 case O(MANAGE,Remove):
836 devmode = 'r';
837 continue;
838 case O(MANAGE,'f'): /* set faulty */
839 case O(MANAGE,Fail):
840 case O(INCREMENTAL,'f'):
841 case O(INCREMENTAL,Remove):
842 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
843 * even though we will both fail and
844 * remove the device */
845 devmode = 'f';
846 continue;
847 case O(INCREMENTAL,'R'):
848 case O(MANAGE,'R'):
849 case O(ASSEMBLE,'R'):
850 case O(BUILD,'R'):
851 case O(CREATE,'R'): /* Run the array */
852 if (runstop < 0) {
853 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
854 exit(2);
855 }
856 runstop = 1;
857 continue;
858 case O(MANAGE,'S'):
859 if (runstop > 0) {
860 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
861 exit(2);
862 }
863 runstop = -1;
864 continue;
865 case O(MANAGE,'t'):
866 test = 1;
867 continue;
868
869 case O(MISC,'Q'):
870 case O(MISC,'D'):
871 case O(MISC,'E'):
872 case O(MISC,'K'):
873 case O(MISC,'R'):
874 case O(MISC,'S'):
875 case O(MISC,'X'):
876 case O(MISC,'o'):
877 case O(MISC,'w'):
878 case O(MISC,'W'):
879 case O(MISC, WaitOpt):
880 case O(MISC, Waitclean):
881 case O(MISC, DetailPlatform):
882 case O(MISC, KillSubarray):
883 case O(MISC, UpdateSubarray):
884 if (devmode && devmode != opt &&
885 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
886 fprintf(stderr, Name ": --examine/-E cannot be given with ");
887 if (devmode == 'E') {
888 if (option_index >= 0)
889 fprintf(stderr, "--%s\n",
890 long_options[option_index].name);
891 else
892 fprintf(stderr, "-%c\n", opt);
893 } else if (isalpha(devmode))
894 fprintf(stderr, "-%c\n", devmode);
895 else
896 fprintf(stderr, "previous option\n");
897 exit(2);
898 }
899 devmode = opt;
900 continue;
901 case O(MISC,'t'):
902 test = 1;
903 continue;
904
905 case O(MISC, Sparc22):
906 if (devmode != 'E') {
907 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
908 exit(2);
909 }
910 SparcAdjust = 1;
911 continue;
912
913 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
914 case O(ASSEMBLE,Bitmap):
915 if (!optarg) {
916 fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n");
917 exit(2);
918 }
919 if (strcmp(optarg, "internal")==0) {
920 fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
921 continue;
922 }
923 bitmap_fd = open(optarg, O_RDWR);
924 if (!*optarg || bitmap_fd < 0) {
925 fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno));
926 exit(2);
927 }
928 ident.bitmap_fd = bitmap_fd; /* for Assemble */
929 continue;
930
931 case O(ASSEMBLE, BackupFile):
932 case O(GROW, BackupFile):
933 /* Specify a file into which grow might place a backup,
934 * or from which assemble might recover a backup
935 */
936 if (backup_file) {
937 fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg);
938 exit(2);
939 }
940 backup_file = optarg;
941 continue;
942
943 case O(BUILD,'b'):
944 case O(BUILD,Bitmap):
945 case O(CREATE,'b'):
946 case O(CREATE,Bitmap): /* here we create the bitmap */
947 if (strcmp(optarg, "none") == 0) {
948 fprintf(stderr, Name ": '--bitmap none' only"
949 " support for --grow\n");
950 exit(2);
951 }
952 /* FALL THROUGH */
953 case O(GROW,'b'):
954 case O(GROW,Bitmap):
955 if (strcmp(optarg, "internal")== 0 ||
956 strcmp(optarg, "none")== 0 ||
957 strchr(optarg, '/') != NULL) {
958 bitmap_file = optarg;
959 continue;
960 }
961 /* probable typo */
962 fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n"
963 " not '%s'\n", optarg);
964 exit(2);
965
966 case O(GROW,BitmapChunk):
967 case O(BUILD,BitmapChunk):
968 case O(CREATE,BitmapChunk): /* bitmap chunksize */
969 bitmap_chunk = strtol(optarg, &c, 10);
970 if (!optarg[0] || *c || bitmap_chunk < 0 ||
971 bitmap_chunk & (bitmap_chunk - 1)) {
972 fprintf(stderr, Name ": invalid bitmap chunksize: %s\n",
973 optarg);
974 exit(2);
975 }
976 /* convert K to B, chunk of 0K means 512B */
977 bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512;
978 continue;
979
980 case O(GROW, WriteBehind):
981 case O(BUILD, WriteBehind):
982 case O(CREATE, WriteBehind): /* write-behind mode */
983 write_behind = DEFAULT_MAX_WRITE_BEHIND;
984 if (optarg) {
985 write_behind = strtol(optarg, &c, 10);
986 if (write_behind < 0 || *c ||
987 write_behind > 16383) {
988 fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
989 exit(2);
990 }
991 }
992 continue;
993
994 case O(INCREMENTAL, 'r'):
995 case O(INCREMENTAL, RebuildMapOpt):
996 rebuild_map = 1;
997 continue;
998 case O(INCREMENTAL, IncrementalPath):
999 remove_path = optarg;
1000 continue;
1001 }
1002 /* We have now processed all the valid options. Anything else is
1003 * an error
1004 */
1005 if (option_index > 0)
1006 fprintf(stderr, Name ":option --%s not valid in %s mode\n",
1007 long_options[option_index].name,
1008 map_num(modes, mode));
1009 else
1010 fprintf(stderr, Name ": option -%c not valid in %s mode\n",
1011 opt, map_num(modes, mode));
1012 exit(2);
1013
1014 }
1015
1016 if (print_help) {
1017 char *help_text = Help;
1018 if (print_help == 2)
1019 help_text = OptionHelp;
1020 else
1021 switch (mode) {
1022 case ASSEMBLE : help_text = Help_assemble; break;
1023 case BUILD : help_text = Help_build; break;
1024 case CREATE : help_text = Help_create; break;
1025 case MANAGE : help_text = Help_manage; break;
1026 case MISC : help_text = Help_misc; break;
1027 case MONITOR : help_text = Help_monitor; break;
1028 case GROW : help_text = Help_grow; break;
1029 case INCREMENTAL:help_text= Help_incr; break;
1030 }
1031 fputs(help_text,stdout);
1032 exit(0);
1033 }
1034
1035 if (!mode && devs_found) {
1036 mode = MISC;
1037 devmode = 'Q';
1038 if (devlist->disposition == 0)
1039 devlist->disposition = devmode;
1040 }
1041 if (!mode) {
1042 fputs(Usage, stderr);
1043 exit(2);
1044 }
1045
1046 if (symlinks) {
1047 struct createinfo *ci = conf_get_create_info();
1048
1049 if (strcasecmp(symlinks, "yes") == 0)
1050 ci->symlinks = 1;
1051 else if (strcasecmp(symlinks, "no") == 0)
1052 ci->symlinks = 0;
1053 else {
1054 fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n");
1055 exit(2);
1056 }
1057 }
1058 /* Ok, got the option parsing out of the way
1059 * hopefully it's mostly right but there might be some stuff
1060 * missing
1061 *
1062 * That is mosty checked in the per-mode stuff but...
1063 *
1064 * For @,B,C and A without -s, the first device listed must be an md device
1065 * we check that here and open it.
1066 */
1067
1068 if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
1069 (mode == ASSEMBLE && ! scan)) {
1070 if (devs_found < 1) {
1071 fprintf(stderr, Name ": an md device must be given in this mode\n");
1072 exit(2);
1073 }
1074 if ((int)ident.super_minor == -2 && autof) {
1075 fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
1076 exit(2);
1077 }
1078 if (mode == MANAGE || mode == GROW) {
1079 mdfd = open_mddev(devlist->devname, 1);
1080 if (mdfd < 0)
1081 exit(1);
1082 } else
1083 /* non-existent device is OK */
1084 mdfd = open_mddev(devlist->devname, 0);
1085 if (mdfd == -2) {
1086 fprintf(stderr, Name ": device %s exists but is not an "
1087 "md array.\n", devlist->devname);
1088 exit(1);
1089 }
1090 if ((int)ident.super_minor == -2) {
1091 struct stat stb;
1092 if (mdfd < 0) {
1093 fprintf(stderr, Name ": --super-minor=dev given, and "
1094 "listed device %s doesn't exist.\n",
1095 devlist->devname);
1096 exit(1);
1097 }
1098 fstat(mdfd, &stb);
1099 ident.super_minor = minor(stb.st_rdev);
1100 }
1101 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1102 /* We don't really want this open yet, we just might
1103 * have wanted to check some things
1104 */
1105 close(mdfd);
1106 mdfd = -1;
1107 }
1108 }
1109
1110 if (raiddisks) {
1111 if (raiddisks > max_disks) {
1112 fprintf(stderr, Name ": invalid number of raid devices: %d\n",
1113 raiddisks);
1114 exit(2);
1115 }
1116 if (raiddisks == 1 && !force && level != -5) {
1117 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
1118 " a mistake. If you really mean it you will need to specify --force before\n"
1119 " setting the number of drives.\n");
1120 exit(2);
1121 }
1122 }
1123 if (sparedisks) {
1124 if ( sparedisks > max_disks - raiddisks) {
1125 fprintf(stderr, Name ": invalid number of spare-devices: %d\n",
1126 sparedisks);
1127 exit(2);
1128 }
1129 }
1130
1131 if (homehost == NULL)
1132 homehost = conf_get_homehost(&require_homehost);
1133 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
1134 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1135 sys_hostname[sizeof(sys_hostname)-1] = 0;
1136 homehost = sys_hostname;
1137 }
1138 }
1139
1140 if ((mode != MISC || devmode != 'E') &&
1141 geteuid() != 0) {
1142 fprintf(stderr, Name ": must be super-user to perform this action\n");
1143 exit(1);
1144 }
1145
1146 ident.autof = autof;
1147
1148 rv = 0;
1149 switch(mode) {
1150 case MANAGE:
1151 /* readonly, add/remove, readwrite, runstop */
1152 if (readonly>0)
1153 rv = Manage_ro(devlist->devname, mdfd, readonly);
1154 if (!rv && devs_found>1)
1155 rv = Manage_subdevs(devlist->devname, mdfd,
1156 devlist->next, verbose-quiet, test);
1157 if (!rv && readonly < 0)
1158 rv = Manage_ro(devlist->devname, mdfd, readonly);
1159 if (!rv && runstop)
1160 rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet);
1161 break;
1162 case ASSEMBLE:
1163 if (devs_found == 1 && ident.uuid_set == 0 &&
1164 ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) {
1165 /* Only a device has been given, so get details from config file */
1166 struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1167 if (array_ident == NULL) {
1168 fprintf(stderr, Name ": %s not identified in config file.\n",
1169 devlist->devname);
1170 rv |= 1;
1171 if (mdfd >= 0)
1172 close(mdfd);
1173 } else {
1174 if (array_ident->autof == 0)
1175 array_ident->autof = autof;
1176 rv |= Assemble(ss, devlist->devname, array_ident,
1177 NULL, backup_file,
1178 readonly, runstop, update,
1179 homehost, require_homehost,
1180 verbose-quiet, force);
1181 }
1182 } else if (!scan)
1183 rv = Assemble(ss, devlist->devname, &ident,
1184 devlist->next, backup_file,
1185 readonly, runstop, update,
1186 homehost, require_homehost,
1187 verbose-quiet, force);
1188 else if (devs_found>0) {
1189 if (update && devs_found > 1) {
1190 fprintf(stderr, Name ": can only update a single array at a time\n");
1191 exit(1);
1192 }
1193 if (backup_file && devs_found > 1) {
1194 fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n");
1195 exit(1);
1196 }
1197 for (dv = devlist ; dv ; dv=dv->next) {
1198 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1199 if (array_ident == NULL) {
1200 fprintf(stderr, Name ": %s not identified in config file.\n",
1201 dv->devname);
1202 rv |= 1;
1203 continue;
1204 }
1205 if (array_ident->autof == 0)
1206 array_ident->autof = autof;
1207 rv |= Assemble(ss, dv->devname, array_ident,
1208 NULL, backup_file,
1209 readonly, runstop, update,
1210 homehost, require_homehost,
1211 verbose-quiet, force);
1212 }
1213 } else {
1214 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1215 struct mddev_dev *devlist = conf_get_devs();
1216 int cnt = 0;
1217 int failures, successes;
1218 if (devlist == NULL) {
1219 fprintf(stderr, Name ": No devices listed in conf file were found.\n");
1220 exit(1);
1221 }
1222 if (update) {
1223 fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n");
1224 exit(1);
1225 }
1226 if (backup_file) {
1227 fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n");
1228 exit(1);
1229 }
1230 for (a = array_list; a ; a = a->next) {
1231 a->assembled = 0;
1232 if (a->autof == 0)
1233 a->autof = autof;
1234 }
1235 do {
1236 failures = 0;
1237 successes = 0;
1238 rv = 0;
1239 for (a = array_list; a ; a = a->next) {
1240 int r;
1241 if (a->assembled)
1242 continue;
1243 if (a->devname &&
1244 strcasecmp(a->devname, "<ignore>") == 0)
1245 continue;
1246
1247 r = Assemble(ss, a->devname,
1248 a,
1249 NULL, NULL,
1250 readonly, runstop, NULL,
1251 homehost, require_homehost,
1252 verbose-quiet, force);
1253 if (r == 0) {
1254 a->assembled = 1;
1255 successes++;
1256 } else
1257 failures++;
1258 rv |= r;
1259 cnt++;
1260 }
1261 } while (failures && successes);
1262 if (homehost && cnt == 0) {
1263 /* Maybe we can auto-assemble something.
1264 * Repeatedly call Assemble in auto-assemble mode
1265 * until it fails
1266 */
1267 int rv2;
1268 int acnt;
1269 ident.autof = autof;
1270 do {
1271 struct mddev_dev *devlist = conf_get_devs();
1272 acnt = 0;
1273 do {
1274 rv2 = Assemble(ss, NULL,
1275 &ident,
1276 devlist, NULL,
1277 readonly, runstop, NULL,
1278 homehost, require_homehost,
1279 verbose-quiet, force);
1280 if (rv2==0) {
1281 cnt++;
1282 acnt++;
1283 }
1284 if (rv2 == 1)
1285 /* found something so even though assembly failed we
1286 * want to avoid auto-updates
1287 */
1288 auto_update_home = 0;
1289 } while (rv2!=2);
1290 /* Incase there are stacked devices, we need to go around again */
1291 } while (acnt);
1292 #if 0
1293 if (cnt == 0 && auto_update_home && homehost) {
1294 /* Nothing found, maybe we need to bootstrap homehost info */
1295 do {
1296 acnt = 0;
1297 do {
1298 rv2 = Assemble(ss, NULL,
1299 &ident,
1300 NULL, NULL,
1301 readonly, runstop, "homehost",
1302 homehost, require_homehost,
1303 verbose-quiet, force);
1304 if (rv2==0) {
1305 cnt++;
1306 acnt++;
1307 }
1308 } while (rv2!=2);
1309 /* Incase there are stacked devices, we need to go around again */
1310 } while (acnt);
1311 }
1312 #endif
1313 if (cnt == 0 && rv == 0) {
1314 fprintf(stderr, Name ": No arrays found in config file or automatically\n");
1315 rv = 1;
1316 } else if (cnt)
1317 rv = 0;
1318 } else if (cnt == 0 && rv == 0) {
1319 fprintf(stderr, Name ": No arrays found in config file\n");
1320 rv = 1;
1321 }
1322 }
1323 break;
1324 case BUILD:
1325 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1326 if (write_behind && !bitmap_file) {
1327 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1328 rv = 1;
1329 break;
1330 }
1331 if (raiddisks == 0) {
1332 fprintf(stderr, Name ": no raid-devices specified.\n");
1333 rv = 1;
1334 break;
1335 }
1336
1337 if (bitmap_file) {
1338 if (strcmp(bitmap_file, "internal")==0) {
1339 fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n");
1340 rv |= 1;
1341 break;
1342 }
1343 }
1344 rv = Build(devlist->devname, chunk, level, layout,
1345 raiddisks, devlist->next, assume_clean,
1346 bitmap_file, bitmap_chunk, write_behind,
1347 delay, verbose-quiet, autof, size);
1348 break;
1349 case CREATE:
1350 if (delay == 0) delay = DEFAULT_BITMAP_DELAY;
1351 if (write_behind && !bitmap_file) {
1352 fprintf(stderr, Name ": write-behind mode requires a bitmap.\n");
1353 rv = 1;
1354 break;
1355 }
1356 if (raiddisks == 0) {
1357 fprintf(stderr, Name ": no raid-devices specified.\n");
1358 rv = 1;
1359 break;
1360 }
1361
1362 rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size,
1363 raiddisks, sparedisks, ident.name, homehost,
1364 ident.uuid_set ? ident.uuid : NULL,
1365 devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean,
1366 bitmap_file, bitmap_chunk, write_behind, delay, autof);
1367 break;
1368 case MISC:
1369 if (devmode == 'E') {
1370 if (devlist == NULL && !scan) {
1371 fprintf(stderr, Name ": No devices to examine\n");
1372 exit(2);
1373 }
1374 if (devlist == NULL)
1375 devlist = conf_get_devs();
1376 if (devlist == NULL) {
1377 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1378 exit(1);
1379 }
1380 if (brief && verbose)
1381 brief = 2;
1382 rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief,
1383 export, scan,
1384 SparcAdjust, ss, homehost);
1385 } else if (devmode == DetailPlatform) {
1386 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose);
1387 } else {
1388 if (devlist == NULL) {
1389 if ((devmode=='D' || devmode == Waitclean) && scan) {
1390 /* apply --detail or --wait-clean to
1391 * all devices in /proc/mdstat
1392 */
1393 struct mdstat_ent *ms = mdstat_read(0, 1);
1394 struct mdstat_ent *e;
1395 struct map_ent *map = NULL;
1396 int members;
1397 int v = verbose>1?0:verbose+1;
1398
1399 for (members = 0; members <= 1; members++) {
1400 for (e=ms ; e ; e=e->next) {
1401 char *name;
1402 struct map_ent *me;
1403 int member = e->metadata_version &&
1404 strncmp(e->metadata_version,
1405 "external:/", 10) == 0;
1406 if (members != member)
1407 continue;
1408 me = map_by_devnum(&map, e->devnum);
1409 if (me && me->path
1410 && strcmp(me->path, "/unknown") != 0)
1411 name = me->path;
1412 else
1413 name = get_md_name(e->devnum);
1414
1415 if (!name) {
1416 fprintf(stderr, Name ": cannot find device file for %s\n",
1417 e->dev);
1418 continue;
1419 }
1420 if (devmode == 'D')
1421 rv |= Detail(name, v,
1422 export, test,
1423 homehost);
1424 else
1425 rv |= WaitClean(name, -1, v);
1426 put_md_name(name);
1427 }
1428 }
1429 free_mdstat(ms);
1430 } else if (devmode == 'S' && scan) {
1431 /* apply --stop to all devices in /proc/mdstat */
1432 /* Due to possible stacking of devices, repeat until
1433 * nothing more can be stopped
1434 */
1435 int progress=1, err;
1436 int last = 0;
1437 do {
1438 struct mdstat_ent *ms = mdstat_read(0, 0);
1439 struct mdstat_ent *e;
1440
1441 if (!progress) last = 1;
1442 progress = 0; err = 0;
1443 for (e=ms ; e ; e=e->next) {
1444 char *name = get_md_name(e->devnum);
1445
1446 if (!name) {
1447 fprintf(stderr, Name ": cannot find device file for %s\n",
1448 e->dev);
1449 continue;
1450 }
1451 mdfd = open_mddev(name, 1);
1452 if (mdfd >= 0) {
1453 if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1))
1454 err = 1;
1455 else
1456 progress = 1;
1457 close(mdfd);
1458 }
1459
1460 put_md_name(name);
1461 }
1462 free_mdstat(ms);
1463 } while (!last && err);
1464 if (err) rv |= 1;
1465 } else {
1466 fprintf(stderr, Name ": No devices given.\n");
1467 exit(2);
1468 }
1469 }
1470 for (dv=devlist ; dv; dv=dv->next) {
1471 switch(dv->disposition) {
1472 case 'D':
1473 rv |= Detail(dv->devname,
1474 brief?1+verbose:0,
1475 export, test, homehost);
1476 continue;
1477 case 'K': /* Zero superblock */
1478 if (ss)
1479 rv |= Kill(dv->devname, ss, force, quiet,0);
1480 else {
1481 int q = quiet;
1482 do {
1483 rv |= Kill(dv->devname, NULL, force, q, 0);
1484 q = 1;
1485 } while (rv == 0);
1486 rv &= ~2;
1487 }
1488 continue;
1489 case 'Q':
1490 rv |= Query(dv->devname); continue;
1491 case 'X':
1492 rv |= ExamineBitmap(dv->devname, brief, ss); continue;
1493 case 'W':
1494 case WaitOpt:
1495 rv |= Wait(dv->devname); continue;
1496 case Waitclean:
1497 rv |= WaitClean(dv->devname, -1, verbose-quiet); continue;
1498 case KillSubarray:
1499 rv |= Kill_subarray(dv->devname, subarray, quiet);
1500 continue;
1501 case UpdateSubarray:
1502 if (update == NULL) {
1503 fprintf(stderr,
1504 Name ": -U/--update must be specified with --update-subarray\n");
1505 rv |= 1;
1506 continue;
1507 }
1508 rv |= Update_subarray(dv->devname, subarray, update, &ident, quiet);
1509 continue;
1510 }
1511 mdfd = open_mddev(dv->devname, 1);
1512 if (mdfd>=0) {
1513 switch(dv->disposition) {
1514 case 'R':
1515 rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break;
1516 case 'S':
1517 rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break;
1518 case 'o':
1519 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1520 case 'w':
1521 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1522 }
1523 close(mdfd);
1524 } else
1525 rv |= 1;
1526 }
1527 }
1528 break;
1529 case MONITOR:
1530 if (!devlist && !scan) {
1531 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
1532 rv = 1;
1533 break;
1534 }
1535 if (pidfile && !daemonise) {
1536 fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
1537 rv = 1;
1538 break;
1539 }
1540 if (delay == 0) {
1541 if (get_linux_version() > 2006016)
1542 /* mdstat responds to poll */
1543 delay = 1000;
1544 else
1545 delay = 60;
1546 }
1547 rv= Monitor(devlist, mailaddr, program,
1548 delay?delay:60, daemonise, scan, oneshot,
1549 dosyslog, test, pidfile, increments, spare_sharing);
1550 break;
1551
1552 case GROW:
1553 if (array_size >= 0) {
1554 /* alway impose array size first, independent of
1555 * anything else
1556 * Do not allow level or raid_disks changes at the
1557 * same time as that can be irreversibly destructive.
1558 */
1559 struct mdinfo sra;
1560 int err;
1561 if (raiddisks || level != UnSet) {
1562 fprintf(stderr, Name ": cannot change array size in same operation "
1563 "as changing raiddisks or level.\n"
1564 " Change size first, then check that data is still intact.\n");
1565 rv = 1;
1566 break;
1567 }
1568 sysfs_init(&sra, mdfd, 0);
1569 if (array_size == 0)
1570 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1571 else
1572 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1573 if (err < 0) {
1574 if (errno == E2BIG)
1575 fprintf(stderr, Name ": --array-size setting"
1576 " is too large.\n");
1577 else
1578 fprintf(stderr, Name ": current kernel does"
1579 " not support setting --array-size\n");
1580 rv = 1;
1581 break;
1582 }
1583 }
1584 if (devs_found > 1) {
1585
1586 /* must be '-a'. */
1587 if (size >= 0 || raiddisks || chunk || layout_str != NULL || bitmap_file) {
1588 fprintf(stderr, Name ": --add cannot be used with other geometry changes in --grow mode\n");
1589 rv = 1;
1590 break;
1591 }
1592 for (dv=devlist->next; dv ; dv=dv->next) {
1593 rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
1594 if (rv)
1595 break;
1596 }
1597 } else if (bitmap_file) {
1598 if (size >= 0 || raiddisks || chunk || layout_str != NULL) {
1599 fprintf(stderr, Name ": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1600 rv = 1;
1601 break;
1602 }
1603 if (delay == 0)
1604 delay = DEFAULT_BITMAP_DELAY;
1605 rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
1606 bitmap_chunk, delay, write_behind, force);
1607 } else if (size >= 0 || raiddisks != 0 || layout_str != NULL
1608 || chunk != 0 || level != UnSet) {
1609 rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
1610 size, level, layout_str, chunk, raiddisks);
1611 } else if (array_size < 0)
1612 fprintf(stderr, Name ": no changes to --grow\n");
1613 break;
1614 case INCREMENTAL:
1615 if (rebuild_map) {
1616 RebuildMap();
1617 }
1618 if (scan) {
1619 if (runstop <= 0) {
1620 fprintf(stderr, Name
1621 ": --incremental --scan meaningless without --run.\n");
1622 break;
1623 }
1624 if (devmode == 'f') {
1625 fprintf(stderr, Name
1626 ": --incremental --scan --fail not supported.\n");
1627 break;
1628 }
1629 rv = IncrementalScan(verbose);
1630 }
1631 if (!devlist) {
1632 if (!rebuild_map && !scan) {
1633 fprintf(stderr, Name
1634 ": --incremental requires a device.\n");
1635 rv = 1;
1636 }
1637 break;
1638 }
1639 if (devlist->next) {
1640 fprintf(stderr, Name
1641 ": --incremental can only handle one device.\n");
1642 rv = 1;
1643 break;
1644 }
1645 if (devmode == 'f')
1646 rv = IncrementalRemove(devlist->devname, remove_path,
1647 verbose-quiet);
1648 else
1649 rv = Incremental(devlist->devname, verbose-quiet,
1650 runstop, ss, homehost,
1651 require_homehost, autof);
1652 break;
1653 case AUTODETECT:
1654 autodetect();
1655 break;
1656 }
1657 exit(rv);
1658 }