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