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