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