]> git.ipfire.org Git - thirdparty/mdadm.git/blame_incremental - mdadm.c
mdadm-1.5.0
[thirdparty/mdadm.git] / mdadm.c
... / ...
CommitLineData
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30#include "mdadm.h"
31#include "md_p.h"
32
33int open_mddev(char *dev)
34{
35 int mdfd = open(dev, O_RDWR, 0);
36 if (mdfd < 0)
37 fprintf(stderr, Name ": error opening %s: %s\n",
38 dev, strerror(errno));
39 else if (md_get_version(mdfd) <= 0) {
40 fprintf(stderr, Name ": %s does not appear to be an md device\n",
41 dev);
42 close(mdfd);
43 mdfd = -1;
44 }
45 return mdfd;
46}
47
48
49
50int main(int argc, char *argv[])
51{
52 int mode = 0;
53 int opt;
54 int option_index;
55 char *help_text;
56 char *c;
57 int rv;
58
59 int chunk = 0;
60 int size = 0;
61 int level = UnSet;
62 int layout = UnSet;
63 int raiddisks = 0;
64 int sparedisks = 0;
65 struct mddev_ident_s ident;
66 char *configfile = NULL;
67 char *cp;
68 char *update = NULL;
69 int scan = 0;
70 char devmode = 0;
71 int runstop = 0;
72 int readonly = 0;
73 int SparcAdjust = 0;
74 mddev_dev_t devlist = NULL;
75 mddev_dev_t *devlistend = & devlist;
76 mddev_dev_t dv;
77 int devs_found = 0;
78 int verbose = 0;
79 int brief = 0;
80 int force = 0;
81 int test = 0;
82
83 char *mailaddr = NULL;
84 char *program = NULL;
85 int delay = 0;
86 int daemonise = 0;
87 int oneshot = 0;
88
89 int mdfd = -1;
90
91 ident.uuid_set=0;
92 ident.level = UnSet;
93 ident.raid_disks = UnSet;
94 ident.super_minor= UnSet;
95 ident.devices=0;
96
97 while ((option_index = -1) ,
98 (opt=getopt_long(argc, argv,
99 short_options, long_options,
100 &option_index)) != -1) {
101 int newmode = mode;
102 /* firstly, so mode-independant options */
103 switch(opt) {
104 case 'h':
105 help_text = Help;
106 if (option_index > 0 &&
107 strcmp(long_options[option_index].name, "help-options")==0)
108 help_text = OptionHelp;
109 else
110 switch (mode) {
111 case ASSEMBLE : help_text = Help_assemble; break;
112 case BUILD : help_text = Help_build; break;
113 case CREATE : help_text = Help_create; break;
114 case MANAGE : help_text = Help_manage; break;
115 case MISC : help_text = Help_misc; break;
116 case MONITOR : help_text = Help_monitor; break;
117 }
118 fputs(help_text,stderr);
119 exit(0);
120
121 case 'V':
122 fputs(Version, stderr);
123 exit(0);
124
125 case 'v': verbose = 1;
126 continue;
127
128 case 'b': brief = 1;
129 continue;
130
131 case ':':
132 case '?':
133 fputs(Usage, stderr);
134 exit(2);
135 }
136 /* second, figure out the mode.
137 * Some options force the mode. Others
138 * set the mode if it isn't already
139 */
140
141 switch(opt) {
142 case '@': /* just incase they say --manage */
143 newmode = MANAGE; break;
144 case 'a':
145 case 'r':
146 case 'f':
147 case 1 : if (!mode) newmode = MANAGE; break;
148
149 case 'A': newmode = ASSEMBLE; break;
150 case 'B': newmode = BUILD; break;
151 case 'C': newmode = CREATE; break;
152 case 'F': newmode = MONITOR;break;
153
154 case '#':
155 case 'D':
156 case 'E':
157 case 'Q': newmode = MISC; break;
158 case 'R':
159 case 'S':
160 case 'o':
161 case 'w':
162 case 'K': if (!mode) newmode = MISC; break;
163 }
164 if (mode && newmode == mode) {
165 /* everybody happy ! */
166 } else if (mode && newmode != mode) {
167 /* not allowed.. */
168 fprintf(stderr, Name ": ");
169 if (option_index >= 0)
170 fprintf(stderr, "--%s", long_options[option_index].name);
171 else
172 fprintf(stderr, "-%c", opt);
173 fprintf(stderr, " would set mode to %s, but it is already %s.\n",
174 map_num(modes, newmode),
175 map_num(modes, mode));
176 exit(2);
177 } else if (!mode && newmode) {
178 mode = newmode;
179 } else {
180 /* special case of -c --help */
181 if (opt == 'c' &&
182 ( strncmp(optarg, "--h", 3)==0 ||
183 strncmp(optarg, "-h", 2)==0)) {
184 fputs(Help_config, stderr);
185 exit(0);
186 }
187 if (option_index >= 0)
188 fprintf(stderr, "--%s", long_options[option_index].name);
189 else
190 fprintf(stderr, "-%c", opt);
191 fprintf(stderr, " does not set the mode, and so cannot be first.\n");
192 exit(2);
193 }
194
195 /* if we just set the mode, then done */
196 switch(opt) {
197 case '@':
198 case '#':
199 case 'A':
200 case 'B':
201 case 'C':
202 case 'F':
203 continue;
204 }
205 if (opt == 1) {
206 /* an undecorated option - must be a device name.
207 */
208 if (devs_found > 0 && mode == '@' && !devmode) {
209 fprintf(stderr, Name ": Must give on of -a/-r/-f for subsequent devices at %s\n", optarg);
210 exit(2);
211 }
212 dv = malloc(sizeof(*dv));
213 if (dv == NULL) {
214 fprintf(stderr, Name ": malloc failed\n");
215 exit(3);
216 }
217 dv->devname = optarg;
218 dv->disposition = devmode;
219 dv->next = NULL;
220 *devlistend = dv;
221 devlistend = &dv->next;
222
223 devs_found++;
224 continue;
225 }
226
227 /* We've got a mode, and opt is now something else which
228 * could depend on the mode */
229#define O(a,b) ((a<<8)|b)
230 switch (O(mode,opt)) {
231 case O(CREATE,'c'):
232 case O(BUILD,'c'): /* chunk or rounding */
233 if (chunk) {
234 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
235 "Second value is %s.\n", optarg);
236 exit(2);
237 }
238 chunk = strtol(optarg, &c, 10);
239 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
240 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
241 optarg);
242 exit(2);
243 }
244 continue;
245
246 case O(CREATE,'z'): /* size */
247 if (size) {
248 fprintf(stderr, Name ": size may only be specified once. "
249 "Second value is %s.\n", optarg);
250 exit(2);
251 }
252 size = strtol(optarg, &c, 10);
253 if (!optarg[0] || *c || size < 4) {
254 fprintf(stderr, Name ": invalid size: %s\n",
255 optarg);
256 exit(2);
257 }
258 continue;
259
260 case O(CREATE,'l'):
261 case O(BUILD,'l'): /* set raid level*/
262 if (level != UnSet) {
263 fprintf(stderr, Name ": raid level may only be set once. "
264 "Second value is %s.\n", optarg);
265 exit(2);
266 }
267 level = map_name(pers, optarg);
268 if (level == UnSet) {
269 fprintf(stderr, Name ": invalid raid level: %s\n",
270 optarg);
271 exit(2);
272 }
273 if (level != 0 && level != -1 && mode == BUILD) {
274 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
275 optarg);
276 exit(2);
277 }
278 if (sparedisks > 0 && level < 1 && level >= -1) {
279 fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
280 optarg);
281 exit(2);
282 }
283 ident.level = level;
284 continue;
285
286 case O(CREATE,'p'): /* raid5 layout */
287 if (layout >= 0) {
288 fprintf(stderr,Name ": layout may only be sent once. "
289 "Second value was %s\n", optarg);
290 exit(2);
291 }
292 switch(level) {
293 default:
294 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
295 map_num(pers, level));
296 exit(2);
297 case UnSet:
298 fprintf(stderr, Name ": raid level must be given before layout.\n");
299 exit(2);
300
301 case 5:
302 case 6:
303 layout = map_name(r5layout, optarg);
304 if (layout==UnSet) {
305 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
306 optarg);
307 exit(2);
308 }
309 break;
310 }
311 continue;
312
313 case O(CREATE,'n'):
314 case O(BUILD,'n'): /* number of raid disks */
315 if (raiddisks) {
316 fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
317 raiddisks, optarg);
318 exit(2);
319 }
320 raiddisks = strtol(optarg, &c, 10);
321 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
322 fprintf(stderr, Name ": invalid number of raid devices: %s\n",
323 optarg);
324 exit(2);
325 }
326 if (raiddisks == 1 && !force) {
327 fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
328 " a mistake. If you really mean it you will need to specify --force before\n"
329 " setting the number of drives.\n");
330 exit(2);
331 }
332 ident.raid_disks = raiddisks;
333 continue;
334
335 case O(CREATE,'x'): /* number of spare (eXtra) discs */
336 if (sparedisks) {
337 fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
338 sparedisks, optarg);
339 exit(2);
340 }
341 if (level != UnSet && level <= 0 && level >= -1) {
342 fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
343 level);
344 exit(2);
345 }
346 sparedisks = strtol(optarg, &c, 10);
347 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
348 fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
349 optarg);
350 exit(2);
351 }
352 continue;
353 case O(BUILD,'f'): /* force honouring '-n 1' */
354 case O(CREATE,'f'): /* force honouring of device list */
355 case O(ASSEMBLE,'f'): /* force assembly */
356 case O(MISC,'f'): /* force zero */
357 force=1;
358 continue;
359
360 /* now for the Assemble options */
361 case O(ASSEMBLE,'u'): /* uuid of array */
362 if (ident.uuid_set) {
363 fprintf(stderr, Name ": uuid cannot be set twice. "
364 "Second value %s.\n", optarg);
365 exit(2);
366 }
367 if (parse_uuid(optarg, ident.uuid))
368 ident.uuid_set = 1;
369 else {
370 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
371 exit(2);
372 }
373 continue;
374
375 case O(ASSEMBLE,'m'): /* super-minor for array */
376 if (ident.super_minor != UnSet) {
377 fprintf(stderr, Name ": super-minor cannot be set twice. "
378 "Second value: %s.\n", optarg);
379 exit(2);
380 }
381 if (strcmp(optarg, "dev")==0)
382 ident.super_minor = -2;
383 else {
384 ident.super_minor = strtoul(optarg, &cp, 10);
385 if (!optarg[0] || *cp) {
386 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
387 exit(2);
388 }
389 }
390 continue;
391
392 case O(ASSEMBLE,'U'): /* update the superblock */
393 if (update) {
394 fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
395 update, optarg);
396 exit(2);
397 }
398 update = optarg;
399 if (strcmp(update, "sparc2.2")==0) continue;
400 if (strcmp(update, "super-minor") == 0)
401 continue;
402 if (strcmp(update, "summaries")==0)
403 continue;
404 fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2', 'super-minor' or 'summaries' supported\n",update);
405 exit(2);
406
407 case O(ASSEMBLE,'c'): /* config file */
408 case O(MISC, 'c'):
409 case O(MONITOR,'c'):
410 if (configfile) {
411 fprintf(stderr, Name ": configfile cannot be set twice. "
412 "Second value is %s.\n", optarg);
413 exit(2);
414 }
415 configfile = optarg;
416 /* FIXME possibly check that config file exists. Even parse it */
417 continue;
418 case O(ASSEMBLE,'s'): /* scan */
419 case O(MISC,'s'):
420 case O(MONITOR,'s'):
421 scan = 1;
422 continue;
423
424 case O(MONITOR,'m'): /* mail address */
425 if (mailaddr)
426 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
427 optarg);
428 else
429 mailaddr = optarg;
430 continue;
431
432 case O(MONITOR,'p'): /* alert program */
433 if (program)
434 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
435 optarg);
436 else
437 program = optarg;
438 continue;
439
440 case O(MONITOR,'d'): /* delay in seconds */
441 if (delay)
442 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
443 optarg);
444 else {
445 delay = strtol(optarg, &c, 10);
446 if (!optarg[0] || *c || delay<1) {
447 fprintf(stderr, Name ": invalid delay: %s\n",
448 optarg);
449 exit(2);
450 }
451 }
452 continue;
453 case O(MONITOR,'f'): /* daemonise */
454 daemonise = 1;
455 continue;
456 case O(MONITOR,'1'): /* oneshot */
457 oneshot = 1;
458 continue;
459 case O(MONITOR,'t'): /* test */
460 test = 1;
461 continue;
462
463 /* now the general management options. Some are applicable
464 * to other modes. None have arguments.
465 */
466 case O(MANAGE,'a'):
467 case O(CREATE,'a'):
468 case O(BUILD,'a'):
469 case O(ASSEMBLE,'a'): /* add a drive */
470 devmode = 'a';
471 continue;
472 case O(MANAGE,'r'): /* remove a drive */
473 devmode = 'r';
474 continue;
475 case O(MANAGE,'f'): /* set faulty */
476 devmode = 'f';
477 continue;
478 case O(MANAGE,'R'):
479 case O(ASSEMBLE,'R'):
480 case O(BUILD,'R'):
481 case O(CREATE,'R'): /* Run the array */
482 if (runstop < 0) {
483 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
484 exit(2);
485 }
486 runstop = 1;
487 continue;
488 case O(MANAGE,'S'):
489 if (runstop > 0) {
490 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
491 exit(2);
492 }
493 runstop = -1;
494 continue;
495
496 case O(MANAGE,'o'):
497 if (readonly < 0) {
498 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
499 exit(2);
500 }
501 readonly = 1;
502 continue;
503 case O(MANAGE,'w'):
504 if (readonly > 0) {
505 fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
506 exit(2);
507 }
508 readonly = -1;
509 continue;
510
511 case O(MISC,'Q'):
512 case O(MISC,'D'):
513 case O(MISC,'E'):
514 case O(MISC,'K'):
515 case O(MISC,'R'):
516 case O(MISC,'S'):
517 case O(MISC,'o'):
518 case O(MISC,'w'):
519 if (devmode && devmode != opt &&
520 (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
521 fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
522 devmode =='E'?opt:devmode);
523 exit(2);
524 }
525 devmode = opt;
526 continue;
527 case O(MISC,'t'):
528 test = 1;
529 continue;
530
531 case O(MISC, 22):
532 if (devmode != 'E') {
533 fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
534 exit(2);
535 }
536 SparcAdjust = 1;
537 continue;
538 }
539 /* We have now processed all the valid options. Anything else is
540 * an error
541 */
542 fprintf(stderr, Name ": option %c not valid in %s mode\n",
543 opt, map_num(modes, mode));
544 exit(2);
545
546 }
547
548 if (!mode) {
549 fputs(Usage, stderr);
550 exit(2);
551 }
552 /* Ok, got the option parsing out of the way
553 * hopefully it's mostly right but there might be some stuff
554 * missing
555 *
556 * That is mosty checked in the per-mode stuff but...
557 *
558 * For @,B,C and A without -s, the first device listed must be an md device
559 * we check that here and open it.
560 */
561
562 if (mode==MANAGE || mode == BUILD || mode == CREATE || (mode == ASSEMBLE && ! scan)) {
563 if (devs_found < 1) {
564 fprintf(stderr, Name ": an md device must be given in this mode\n");
565 exit(2);
566 }
567 mdfd = open_mddev(devlist->devname);
568 if (mdfd < 0)
569 exit(1);
570 if ((int)ident.super_minor == -2) {
571 struct stat stb;
572 fstat(mdfd, &stb);
573 ident.super_minor = MINOR(stb.st_rdev);
574 }
575 }
576
577 rv = 0;
578 switch(mode) {
579 case MANAGE:
580 /* readonly, add/remove, readwrite, runstop */
581 if (readonly>0)
582 rv = Manage_ro(devlist->devname, mdfd, readonly);
583 if (!rv && devs_found>1)
584 rv = Manage_subdevs(devlist->devname, mdfd,
585 devlist->next);
586 if (!rv && readonly < 0)
587 rv = Manage_ro(devlist->devname, mdfd, readonly);
588 if (!rv && runstop)
589 rv = Manage_runstop(devlist->devname, mdfd, runstop);
590 break;
591 case ASSEMBLE:
592 if (devs_found == 1 && ident.uuid_set == 0 &&
593 ident.super_minor == UnSet && !scan ) {
594 /* Only a device has been given, so get details from config file */
595 mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
596 mdfd = open_mddev(devlist->devname);
597 if (mdfd < 0)
598 rv |= 1;
599 else {
600 if (array_ident == NULL) {
601 fprintf(stderr, Name ": %s not identified in config file.\n",
602 devlist->devname);
603 rv |= 1;
604 }
605 else
606 rv |= Assemble(devlist->devname, mdfd, array_ident, configfile,
607 NULL,
608 readonly, runstop, update, verbose, force);
609 }
610 } else if (!scan)
611 rv = Assemble(devlist->devname, mdfd, &ident, configfile,
612 devlist->next,
613 readonly, runstop, update, verbose, force);
614 else if (devs_found>0) {
615 if (update && devs_found > 1) {
616 fprintf(stderr, Name ": can only update a single array at a time\n");
617 exit(1);
618 }
619 for (dv = devlist ; dv ; dv=dv->next) {
620 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
621 mdfd = open_mddev(dv->devname);
622 if (mdfd < 0) {
623 rv |= 1;
624 continue;
625 }
626 if (array_ident == NULL) {
627 fprintf(stderr, Name ": %s not identified in config file.\n",
628 dv->devname);
629 rv |= 1;
630 continue;
631 }
632 rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
633 NULL,
634 readonly, runstop, update, verbose, force);
635 }
636 } else {
637 mddev_ident_t array_list = conf_get_ident(configfile, NULL);
638 if (!array_list) {
639 fprintf(stderr, Name ": No arrays found in config file\n");
640 rv = 1;
641 } else
642 for (; array_list; array_list = array_list->next) {
643 mdu_array_info_t array;
644 mdfd = open_mddev(array_list->devname);
645 if (mdfd < 0) {
646 rv |= 1;
647 continue;
648 }
649 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
650 /* already assembled, skip */
651 continue;
652 rv |= Assemble(array_list->devname, mdfd,
653 array_list, configfile,
654 NULL,
655 readonly, runstop, NULL, verbose, force);
656 }
657 }
658 break;
659 case BUILD:
660 rv = Build(devlist->devname, mdfd, chunk, level, raiddisks, devlist->next);
661 break;
662 case CREATE:
663 rv = Create(devlist->devname, mdfd, chunk, level, layout, size,
664 raiddisks, sparedisks,
665 devs_found-1, devlist->next, runstop, verbose, force);
666 break;
667 case MISC:
668
669 if (devmode == 'E') {
670 if (devlist == NULL && !scan) {
671 fprintf(stderr, Name ": No devices to examine\n");
672 exit(2);
673 }
674 if (devlist == NULL)
675 devlist = conf_get_devs(configfile);
676 if (devlist == NULL) {
677 fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
678 exit(1);
679 }
680 rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust);
681 } else {
682 if (devlist == NULL) {
683 if ((devmode == 'S' ||devmode=='D') && scan) {
684 /* apply to all devices in /proc/mdstat */
685 struct mdstat_ent *ms = mdstat_read();
686 struct mdstat_ent *e;
687 for (e=ms ; e ; e=e->next) {
688 char *name = get_md_name(e->devnum);
689
690 if (!name) {
691 fprintf(stderr, Name ": cannot find device file for %s\n",
692 e->dev);
693 continue;
694 }
695 if (devmode == 'D')
696 rv |= Detail(name, !verbose, test);
697 else if (devmode=='S') {
698 mdfd = open_mddev(name);
699 if (mdfd >= 0)
700 rv |= Manage_runstop(name, mdfd, -1);
701 }
702 put_md_name(name);
703 }
704 } else {
705 fprintf(stderr, Name ": No devices given.\n");
706 exit(2);
707 }
708 }
709 for (dv=devlist ; dv; dv=dv->next) {
710 switch(dv->disposition) {
711 case 'D':
712 rv |= Detail(dv->devname, brief, test); continue;
713 case 'K': /* Zero superblock */
714 rv |= Kill(dv->devname, force); continue;
715 case 'Q':
716 rv |= Query(dv->devname); continue;
717 }
718 mdfd = open_mddev(dv->devname);
719 if (mdfd>=0)
720 switch(dv->disposition) {
721 case 'R':
722 rv |= Manage_runstop(dv->devname, mdfd, 1); break;
723 case 'S':
724 rv |= Manage_runstop(dv->devname, mdfd, -1); break;
725 case 'o':
726 rv |= Manage_ro(dv->devname, mdfd, 1); break;
727 case 'w':
728 rv |= Manage_ro(dv->devname, mdfd, -1); break;
729 }
730 }
731 }
732 break;
733 case MONITOR:
734 if (!devlist && !scan) {
735 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
736 rv = 1;
737 break;
738 }
739 rv= Monitor(devlist, mailaddr, program,
740 delay?delay:60, daemonise, scan, oneshot, configfile, test);
741 break;
742 }
743 exit(rv);
744}