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