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