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