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