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