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