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