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