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