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