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