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