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