]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de> | |
5 | * | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
22 | * Email: <neilb@suse.de> | |
23 | * | |
24 | * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004, | |
25 | * Paul Clements, SteelEye Technology, Inc. | |
26 | */ | |
27 | ||
28 | #include "mdadm.h" | |
29 | #include "md_p.h" | |
30 | #include <ctype.h> | |
31 | ||
32 | ||
33 | int main(int argc, char *argv[]) | |
34 | { | |
35 | int mode = 0; | |
36 | int opt; | |
37 | int option_index; | |
38 | char *c; | |
39 | int rv; | |
40 | int i; | |
41 | ||
42 | int chunk = 0; | |
43 | long long size = -1; | |
44 | long long array_size = -1; | |
45 | int level = UnSet; | |
46 | int layout = UnSet; | |
47 | char *layout_str = NULL; | |
48 | int raiddisks = 0; | |
49 | int sparedisks = 0; | |
50 | struct mddev_ident ident; | |
51 | char *configfile = NULL; | |
52 | char *cp; | |
53 | char *update = NULL; | |
54 | int scan = 0; | |
55 | int devmode = 0; | |
56 | int runstop = 0; | |
57 | int readonly = 0; | |
58 | int write_behind = 0; | |
59 | int bitmap_fd = -1; | |
60 | char *bitmap_file = NULL; | |
61 | char *backup_file = NULL; | |
62 | int invalid_backup = 0; | |
63 | int bitmap_chunk = UnSet; | |
64 | int SparcAdjust = 0; | |
65 | struct mddev_dev *devlist = NULL; | |
66 | struct mddev_dev **devlistend = & devlist; | |
67 | struct mddev_dev *dv; | |
68 | int devs_found = 0; | |
69 | int verbose = 0; | |
70 | int quiet = 0; | |
71 | int brief = 0; | |
72 | int force = 0; | |
73 | int test = 0; | |
74 | int export = 0; | |
75 | int assume_clean = 0; | |
76 | char *symlinks = NULL; | |
77 | /* autof indicates whether and how to create device node. | |
78 | * bottom 3 bits are style. Rest (when shifted) are number of parts | |
79 | * 0 - unset | |
80 | * 1 - don't create (no) | |
81 | * 2 - if is_standard, then create (yes) | |
82 | * 3 - create as 'md' - reject is_standard mdp (md) | |
83 | * 4 - create as 'mdp' - reject is_standard md (mdp) | |
84 | * 5 - default to md if not is_standard (md in config file) | |
85 | * 6 - default to mdp if not is_standard (part, or mdp in config file) | |
86 | */ | |
87 | int autof = 0; | |
88 | ||
89 | char *homehost = NULL; | |
90 | char sys_hostname[256]; | |
91 | int require_homehost = 1; | |
92 | char *mailaddr = NULL; | |
93 | char *program = NULL; | |
94 | int increments = 20; | |
95 | int delay = 0; | |
96 | int daemonise = 0; | |
97 | char *pidfile = NULL; | |
98 | int oneshot = 0; | |
99 | int spare_sharing = 1; | |
100 | struct supertype *ss = NULL; | |
101 | int writemostly = 0; | |
102 | int re_add = 0; | |
103 | char *shortopt = short_options; | |
104 | int dosyslog = 0; | |
105 | int rebuild_map = 0; | |
106 | int auto_update_home = 0; | |
107 | char *subarray = NULL; | |
108 | char *remove_path = NULL; | |
109 | ||
110 | int print_help = 0; | |
111 | FILE *outf; | |
112 | ||
113 | int mdfd = -1; | |
114 | ||
115 | srandom(time(0) ^ getpid()); | |
116 | ||
117 | ident.uuid_set=0; | |
118 | ident.level = UnSet; | |
119 | ident.raid_disks = UnSet; | |
120 | ident.super_minor= UnSet; | |
121 | ident.devices=0; | |
122 | ident.spare_group = NULL; | |
123 | ident.autof = 0; | |
124 | ident.st = NULL; | |
125 | ident.bitmap_fd = -1; | |
126 | ident.bitmap_file = NULL; | |
127 | ident.name[0] = 0; | |
128 | ident.container = NULL; | |
129 | ident.member = NULL; | |
130 | ||
131 | while ((option_index = -1) , | |
132 | (opt=getopt_long(argc, argv, | |
133 | shortopt, long_options, | |
134 | &option_index)) != -1) { | |
135 | int newmode = mode; | |
136 | /* firstly, some mode-independent options */ | |
137 | switch(opt) { | |
138 | case HelpOptions: | |
139 | print_help = 2; | |
140 | continue; | |
141 | case 'h': | |
142 | print_help = 1; | |
143 | continue; | |
144 | ||
145 | case 'V': | |
146 | fputs(Version, stderr); | |
147 | exit(0); | |
148 | ||
149 | case 'v': verbose++; | |
150 | continue; | |
151 | ||
152 | case 'q': quiet++; | |
153 | continue; | |
154 | ||
155 | case 'b': | |
156 | if (mode == ASSEMBLE || mode == BUILD || mode == CREATE | |
157 | || mode == GROW || mode == INCREMENTAL | |
158 | || mode == MANAGE) | |
159 | break; /* b means bitmap */ | |
160 | case Brief: | |
161 | brief = 1; | |
162 | continue; | |
163 | ||
164 | case 'Y': export++; | |
165 | continue; | |
166 | ||
167 | case HomeHost: | |
168 | if (strcasecmp(optarg, "<ignore>") == 0) | |
169 | require_homehost = 0; | |
170 | else | |
171 | homehost = optarg; | |
172 | continue; | |
173 | ||
174 | case ':': | |
175 | case '?': | |
176 | fputs(Usage, stderr); | |
177 | exit(2); | |
178 | } | |
179 | /* second, figure out the mode. | |
180 | * Some options force the mode. Others | |
181 | * set the mode if it isn't already | |
182 | */ | |
183 | ||
184 | switch(opt) { | |
185 | case ManageOpt: | |
186 | newmode = MANAGE; | |
187 | shortopt = short_bitmap_options; | |
188 | break; | |
189 | case 'a': | |
190 | case Add: | |
191 | case 'r': | |
192 | case Remove: | |
193 | case 'f': | |
194 | case Fail: | |
195 | case ReAdd: /* re-add */ | |
196 | if (!mode) { | |
197 | newmode = MANAGE; | |
198 | shortopt = short_bitmap_options; | |
199 | } | |
200 | break; | |
201 | ||
202 | case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break; | |
203 | case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break; | |
204 | case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break; | |
205 | case 'F': newmode = MONITOR;break; | |
206 | case 'G': newmode = GROW; | |
207 | shortopt = short_bitmap_options; | |
208 | break; | |
209 | case 'I': newmode = INCREMENTAL; | |
210 | shortopt = short_bitmap_auto_options; break; | |
211 | case AutoDetect: | |
212 | newmode = AUTODETECT; break; | |
213 | ||
214 | case MiscOpt: | |
215 | case 'D': | |
216 | case 'E': | |
217 | case 'X': | |
218 | case 'Q': newmode = MISC; break; | |
219 | case 'R': | |
220 | case 'S': | |
221 | case 'o': | |
222 | case 'w': | |
223 | case 'W': | |
224 | case WaitOpt: | |
225 | case Waitclean: | |
226 | case DetailPlatform: | |
227 | case KillSubarray: | |
228 | case UpdateSubarray: | |
229 | if (opt == KillSubarray || opt == UpdateSubarray) { | |
230 | if (subarray) { | |
231 | fprintf(stderr, Name ": subarray can only" | |
232 | " be specified once\n"); | |
233 | exit(2); | |
234 | } | |
235 | subarray = optarg; | |
236 | } | |
237 | case 'K': if (!mode) newmode = MISC; break; | |
238 | case NoSharing: newmode = MONITOR; break; | |
239 | } | |
240 | if (mode && newmode == mode) { | |
241 | /* everybody happy ! */ | |
242 | } else if (mode && newmode != mode) { | |
243 | /* not allowed.. */ | |
244 | fprintf(stderr, Name ": "); | |
245 | if (option_index >= 0) | |
246 | fprintf(stderr, "--%s", long_options[option_index].name); | |
247 | else | |
248 | fprintf(stderr, "-%c", opt); | |
249 | fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n", | |
250 | map_num(modes, newmode), | |
251 | map_num(modes, mode)); | |
252 | exit(2); | |
253 | } else if (!mode && newmode) { | |
254 | mode = newmode; | |
255 | } else { | |
256 | /* special case of -c --help */ | |
257 | if ((opt == 'c' || opt == ConfigFile) && | |
258 | ( strncmp(optarg, "--h", 3)==0 || | |
259 | strncmp(optarg, "-h", 2)==0)) { | |
260 | fputs(Help_config, stdout); | |
261 | exit(0); | |
262 | } | |
263 | ||
264 | /* If first option is a device, don't force the mode yet */ | |
265 | if (opt == 1) { | |
266 | if (devs_found == 0) { | |
267 | dv = malloc(sizeof(*dv)); | |
268 | if (dv == NULL) { | |
269 | fprintf(stderr, Name ": malloc failed\n"); | |
270 | exit(3); | |
271 | } | |
272 | dv->devname = optarg; | |
273 | dv->disposition = devmode; | |
274 | dv->writemostly = writemostly; | |
275 | dv->re_add = re_add; | |
276 | dv->used = 0; | |
277 | dv->next = NULL; | |
278 | *devlistend = dv; | |
279 | devlistend = &dv->next; | |
280 | ||
281 | devs_found++; | |
282 | continue; | |
283 | } | |
284 | /* No mode yet, and this is the second device ... */ | |
285 | fprintf(stderr, Name ": An option must be given to set the mode before a second device\n" | |
286 | " (%s) is listed\n", optarg); | |
287 | exit(2); | |
288 | } | |
289 | if (option_index >= 0) | |
290 | fprintf(stderr, Name ": --%s", long_options[option_index].name); | |
291 | else | |
292 | fprintf(stderr, Name ": -%c", opt); | |
293 | fprintf(stderr, " does not set the mode, and so cannot be the first option.\n"); | |
294 | exit(2); | |
295 | } | |
296 | ||
297 | /* if we just set the mode, then done */ | |
298 | switch(opt) { | |
299 | case ManageOpt: | |
300 | case MiscOpt: | |
301 | case 'A': | |
302 | case 'B': | |
303 | case 'C': | |
304 | case 'F': | |
305 | case 'G': | |
306 | case 'I': | |
307 | case AutoDetect: | |
308 | continue; | |
309 | } | |
310 | if (opt == 1) { | |
311 | /* an undecorated option - must be a device name. | |
312 | */ | |
313 | if (devs_found > 0 && mode == MANAGE && !devmode) { | |
314 | fprintf(stderr, Name ": Must give one of -a/-r/-f" | |
315 | " for subsequent devices at %s\n", optarg); | |
316 | exit(2); | |
317 | } | |
318 | if (devs_found > 0 && mode == GROW && !devmode) { | |
319 | fprintf(stderr, Name ": Must give -a/--add for" | |
320 | " devices to add: %s\n", optarg); | |
321 | exit(2); | |
322 | } | |
323 | dv = malloc(sizeof(*dv)); | |
324 | if (dv == NULL) { | |
325 | fprintf(stderr, Name ": malloc failed\n"); | |
326 | exit(3); | |
327 | } | |
328 | dv->devname = optarg; | |
329 | dv->disposition = devmode; | |
330 | dv->writemostly = writemostly; | |
331 | dv->re_add = re_add; | |
332 | dv->used = 0; | |
333 | dv->next = NULL; | |
334 | *devlistend = dv; | |
335 | devlistend = &dv->next; | |
336 | ||
337 | devs_found++; | |
338 | continue; | |
339 | } | |
340 | ||
341 | /* We've got a mode, and opt is now something else which | |
342 | * could depend on the mode */ | |
343 | #define O(a,b) ((a<<16)|b) | |
344 | switch (O(mode,opt)) { | |
345 | case O(GROW,'c'): | |
346 | case O(GROW,ChunkSize): | |
347 | case O(CREATE,'c'): | |
348 | case O(CREATE,ChunkSize): | |
349 | case O(BUILD,'c'): /* chunk or rounding */ | |
350 | case O(BUILD,ChunkSize): /* chunk or rounding */ | |
351 | if (chunk) { | |
352 | fprintf(stderr, Name ": chunk/rounding may only be specified once. " | |
353 | "Second value is %s.\n", optarg); | |
354 | exit(2); | |
355 | } | |
356 | chunk = parse_size(optarg); | |
357 | if (chunk < 8 || ((chunk-1)&chunk)) { | |
358 | fprintf(stderr, Name ": invalid chunk/rounding value: %s\n", | |
359 | optarg); | |
360 | exit(2); | |
361 | } | |
362 | /* Covert sectors to K */ | |
363 | chunk /= 2; | |
364 | continue; | |
365 | ||
366 | #if 0 | |
367 | case O(ASSEMBLE,AutoHomeHost): | |
368 | auto_update_home = 1; | |
369 | continue; | |
370 | #endif | |
371 | case O(INCREMENTAL, 'e'): | |
372 | case O(CREATE,'e'): | |
373 | case O(ASSEMBLE,'e'): | |
374 | case O(MISC,'e'): /* set metadata (superblock) information */ | |
375 | if (ss) { | |
376 | fprintf(stderr, Name ": metadata information already given\n"); | |
377 | exit(2); | |
378 | } | |
379 | for(i=0; !ss && superlist[i]; i++) | |
380 | ss = superlist[i]->match_metadata_desc(optarg); | |
381 | ||
382 | if (!ss) { | |
383 | fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg); | |
384 | exit(2); | |
385 | } | |
386 | continue; | |
387 | ||
388 | case O(MANAGE,'W'): | |
389 | case O(MANAGE,WriteMostly): | |
390 | case O(BUILD,'W'): | |
391 | case O(BUILD,WriteMostly): | |
392 | case O(CREATE,'W'): | |
393 | case O(CREATE,WriteMostly): | |
394 | /* set write-mostly for following devices */ | |
395 | writemostly = 1; | |
396 | continue; | |
397 | ||
398 | case O(MANAGE,'w'): | |
399 | /* clear write-mostly for following devices */ | |
400 | writemostly = 2; | |
401 | continue; | |
402 | ||
403 | ||
404 | case O(GROW,'z'): | |
405 | case O(CREATE,'z'): | |
406 | case O(BUILD,'z'): /* size */ | |
407 | if (size >= 0) { | |
408 | fprintf(stderr, Name ": size may only be specified once. " | |
409 | "Second value is %s.\n", optarg); | |
410 | exit(2); | |
411 | } | |
412 | if (strcmp(optarg, "max")==0) | |
413 | size = 0; | |
414 | else { | |
415 | size = parse_size(optarg); | |
416 | if (size < 8) { | |
417 | fprintf(stderr, Name ": invalid size: %s\n", | |
418 | optarg); | |
419 | exit(2); | |
420 | } | |
421 | /* convert sectors to K */ | |
422 | size /= 2; | |
423 | } | |
424 | continue; | |
425 | ||
426 | case O(GROW,'Z'): /* array size */ | |
427 | if (array_size >= 0) { | |
428 | fprintf(stderr, Name ": array-size may only be specified once. " | |
429 | "Second value is %s.\n", optarg); | |
430 | exit(2); | |
431 | } | |
432 | if (strcmp(optarg, "max") == 0) | |
433 | array_size = 0; | |
434 | else { | |
435 | array_size = parse_size(optarg); | |
436 | if (array_size <= 0) { | |
437 | fprintf(stderr, Name ": invalid array size: %s\n", | |
438 | optarg); | |
439 | exit(2); | |
440 | } | |
441 | } | |
442 | continue; | |
443 | ||
444 | case O(GROW,'l'): | |
445 | case O(CREATE,'l'): | |
446 | case O(BUILD,'l'): /* set raid level*/ | |
447 | if (level != UnSet) { | |
448 | fprintf(stderr, Name ": raid level may only be set once. " | |
449 | "Second value is %s.\n", optarg); | |
450 | exit(2); | |
451 | } | |
452 | level = map_name(pers, optarg); | |
453 | if (level == UnSet) { | |
454 | fprintf(stderr, Name ": invalid raid level: %s\n", | |
455 | optarg); | |
456 | exit(2); | |
457 | } | |
458 | if (level != 0 && level != LEVEL_LINEAR && level != 1 && | |
459 | level != LEVEL_MULTIPATH && level != LEVEL_FAULTY && | |
460 | level != 10 && | |
461 | mode == BUILD) { | |
462 | fprintf(stderr, Name ": Raid level %s not permitted with --build.\n", | |
463 | optarg); | |
464 | exit(2); | |
465 | } | |
466 | if (sparedisks > 0 && level < 1 && level >= -1) { | |
467 | fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n", | |
468 | optarg); | |
469 | exit(2); | |
470 | } | |
471 | ident.level = level; | |
472 | continue; | |
473 | ||
474 | case O(GROW, 'p'): /* new layout */ | |
475 | case O(GROW, Layout): | |
476 | if (layout_str) { | |
477 | fprintf(stderr,Name ": layout may only be sent once. " | |
478 | "Second value was %s\n", optarg); | |
479 | exit(2); | |
480 | } | |
481 | layout_str = optarg; | |
482 | /* 'Grow' will parse the value */ | |
483 | continue; | |
484 | ||
485 | case O(CREATE,'p'): /* raid5 layout */ | |
486 | case O(CREATE,Layout): | |
487 | case O(BUILD,'p'): /* faulty layout */ | |
488 | case O(BUILD,Layout): | |
489 | if (layout != UnSet) { | |
490 | fprintf(stderr,Name ": layout may only be sent once. " | |
491 | "Second value was %s\n", optarg); | |
492 | exit(2); | |
493 | } | |
494 | switch(level) { | |
495 | default: | |
496 | fprintf(stderr, Name ": layout not meaningful for %s arrays.\n", | |
497 | map_num(pers, level)); | |
498 | exit(2); | |
499 | case UnSet: | |
500 | fprintf(stderr, Name ": raid level must be given before layout.\n"); | |
501 | exit(2); | |
502 | ||
503 | case 5: | |
504 | layout = map_name(r5layout, optarg); | |
505 | if (layout==UnSet) { | |
506 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", | |
507 | optarg); | |
508 | exit(2); | |
509 | } | |
510 | break; | |
511 | case 6: | |
512 | layout = map_name(r6layout, optarg); | |
513 | if (layout==UnSet) { | |
514 | fprintf(stderr, Name ": layout %s not understood for raid6.\n", | |
515 | optarg); | |
516 | exit(2); | |
517 | } | |
518 | break; | |
519 | ||
520 | case 10: | |
521 | layout = parse_layout_10(optarg); | |
522 | if (layout < 0) { | |
523 | fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg); | |
524 | exit(2); | |
525 | } | |
526 | break; | |
527 | case LEVEL_FAULTY: | |
528 | /* Faulty | |
529 | * modeNNN | |
530 | */ | |
531 | layout = parse_layout_faulty(optarg); | |
532 | if (layout == -1) { | |
533 | fprintf(stderr, Name ": layout %s not understood for faulty.\n", | |
534 | optarg); | |
535 | exit(2); | |
536 | } | |
537 | break; | |
538 | } | |
539 | continue; | |
540 | ||
541 | case O(CREATE,AssumeClean): | |
542 | case O(BUILD,AssumeClean): /* assume clean */ | |
543 | assume_clean = 1; | |
544 | continue; | |
545 | ||
546 | case O(GROW,'n'): | |
547 | case O(CREATE,'n'): | |
548 | case O(BUILD,'n'): /* number of raid disks */ | |
549 | if (raiddisks) { | |
550 | fprintf(stderr, Name ": raid-devices set twice: %d and %s\n", | |
551 | raiddisks, optarg); | |
552 | exit(2); | |
553 | } | |
554 | raiddisks = strtol(optarg, &c, 10); | |
555 | if (!optarg[0] || *c || raiddisks<=0) { | |
556 | fprintf(stderr, Name ": invalid number of raid devices: %s\n", | |
557 | optarg); | |
558 | exit(2); | |
559 | } | |
560 | ident.raid_disks = raiddisks; | |
561 | continue; | |
562 | ||
563 | case O(CREATE,'x'): /* number of spare (eXtra) discs */ | |
564 | if (sparedisks) { | |
565 | fprintf(stderr,Name ": spare-devices set twice: %d and %s\n", | |
566 | sparedisks, optarg); | |
567 | exit(2); | |
568 | } | |
569 | if (level != UnSet && level <= 0 && level >= -1) { | |
570 | fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n", | |
571 | level); | |
572 | exit(2); | |
573 | } | |
574 | sparedisks = strtol(optarg, &c, 10); | |
575 | if (!optarg[0] || *c || sparedisks < 0) { | |
576 | fprintf(stderr, Name ": invalid number of spare-devices: %s\n", | |
577 | optarg); | |
578 | exit(2); | |
579 | } | |
580 | continue; | |
581 | ||
582 | case O(CREATE,'a'): | |
583 | case O(CREATE,Auto): | |
584 | case O(BUILD,'a'): | |
585 | case O(BUILD,Auto): | |
586 | case O(INCREMENTAL,'a'): | |
587 | case O(INCREMENTAL,Auto): | |
588 | case O(ASSEMBLE,'a'): | |
589 | case O(ASSEMBLE,Auto): /* auto-creation of device node */ | |
590 | autof = parse_auto(optarg, "--auto flag", 0); | |
591 | continue; | |
592 | ||
593 | case O(CREATE,Symlinks): | |
594 | case O(BUILD,Symlinks): | |
595 | case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */ | |
596 | symlinks = optarg; | |
597 | continue; | |
598 | ||
599 | case O(BUILD,'f'): /* force honouring '-n 1' */ | |
600 | case O(BUILD,Force): /* force honouring '-n 1' */ | |
601 | case O(GROW,'f'): /* ditto */ | |
602 | case O(GROW,Force): /* ditto */ | |
603 | case O(CREATE,'f'): /* force honouring of device list */ | |
604 | case O(CREATE,Force): /* force honouring of device list */ | |
605 | case O(ASSEMBLE,'f'): /* force assembly */ | |
606 | case O(ASSEMBLE,Force): /* force assembly */ | |
607 | case O(MISC,'f'): /* force zero */ | |
608 | case O(MISC,Force): /* force zero */ | |
609 | force=1; | |
610 | continue; | |
611 | ||
612 | /* now for the Assemble options */ | |
613 | case O(CREATE,'u'): /* uuid of array */ | |
614 | case O(ASSEMBLE,'u'): /* uuid of array */ | |
615 | if (ident.uuid_set) { | |
616 | fprintf(stderr, Name ": uuid cannot be set twice. " | |
617 | "Second value %s.\n", optarg); | |
618 | exit(2); | |
619 | } | |
620 | if (parse_uuid(optarg, ident.uuid)) | |
621 | ident.uuid_set = 1; | |
622 | else { | |
623 | fprintf(stderr,Name ": Bad uuid: %s\n", optarg); | |
624 | exit(2); | |
625 | } | |
626 | continue; | |
627 | ||
628 | case O(CREATE,'N'): | |
629 | case O(ASSEMBLE,'N'): | |
630 | case O(MISC,'N'): | |
631 | if (ident.name[0]) { | |
632 | fprintf(stderr, Name ": name cannot be set twice. " | |
633 | "Second value %s.\n", optarg); | |
634 | exit(2); | |
635 | } | |
636 | if (mode == MISC && !subarray) { | |
637 | fprintf(stderr, Name ": -N/--name only valid with --update-subarray in misc mode\n"); | |
638 | exit(2); | |
639 | } | |
640 | if (strlen(optarg) > 32) { | |
641 | fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n", | |
642 | optarg); | |
643 | exit(2); | |
644 | } | |
645 | strcpy(ident.name, optarg); | |
646 | continue; | |
647 | ||
648 | case O(ASSEMBLE,'m'): /* super-minor for array */ | |
649 | case O(ASSEMBLE,SuperMinor): | |
650 | if (ident.super_minor != UnSet) { | |
651 | fprintf(stderr, Name ": super-minor cannot be set twice. " | |
652 | "Second value: %s.\n", optarg); | |
653 | exit(2); | |
654 | } | |
655 | if (strcmp(optarg, "dev")==0) | |
656 | ident.super_minor = -2; | |
657 | else { | |
658 | ident.super_minor = strtoul(optarg, &cp, 10); | |
659 | if (!optarg[0] || *cp) { | |
660 | fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg); | |
661 | exit(2); | |
662 | } | |
663 | } | |
664 | continue; | |
665 | ||
666 | case O(ASSEMBLE,'U'): /* update the superblock */ | |
667 | case O(MISC,'U'): | |
668 | if (update) { | |
669 | fprintf(stderr, Name ": Can only update one aspect" | |
670 | " of superblock, both %s and %s given.\n", | |
671 | update, optarg); | |
672 | exit(2); | |
673 | } | |
674 | if (mode == MISC && !subarray) { | |
675 | fprintf(stderr, Name ": Only subarrays can be" | |
676 | " updated in misc mode\n"); | |
677 | exit(2); | |
678 | } | |
679 | update = optarg; | |
680 | if (strcmp(update, "sparc2.2")==0) | |
681 | continue; | |
682 | if (strcmp(update, "super-minor") == 0) | |
683 | continue; | |
684 | if (strcmp(update, "summaries")==0) | |
685 | continue; | |
686 | if (strcmp(update, "resync")==0) | |
687 | continue; | |
688 | if (strcmp(update, "uuid")==0) | |
689 | continue; | |
690 | if (strcmp(update, "name")==0) | |
691 | continue; | |
692 | if (strcmp(update, "homehost")==0) | |
693 | continue; | |
694 | if (strcmp(update, "devicesize")==0) | |
695 | continue; | |
696 | if (strcmp(update, "no-bitmap")==0) | |
697 | continue; | |
698 | if (strcmp(update, "byteorder")==0) { | |
699 | if (ss) { | |
700 | fprintf(stderr, | |
701 | Name ": must not set metadata" | |
702 | " type with --update=byteorder.\n"); | |
703 | exit(2); | |
704 | } | |
705 | for(i=0; !ss && superlist[i]; i++) | |
706 | ss = superlist[i]->match_metadata_desc( | |
707 | "0.swap"); | |
708 | if (!ss) { | |
709 | fprintf(stderr, Name ": INTERNAL ERROR" | |
710 | " cannot find 0.swap\n"); | |
711 | exit(2); | |
712 | } | |
713 | ||
714 | continue; | |
715 | } | |
716 | if (strcmp(update,"?") == 0 || | |
717 | strcmp(update, "help") == 0) { | |
718 | outf = stdout; | |
719 | fprintf(outf, Name ": "); | |
720 | } else { | |
721 | outf = stderr; | |
722 | fprintf(outf, | |
723 | Name ": '--update=%s' is invalid. ", | |
724 | update); | |
725 | } | |
726 | fprintf(outf, "Valid --update options are:\n" | |
727 | " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n" | |
728 | " 'summaries', 'homehost', 'byteorder', 'devicesize',\n" | |
729 | " 'no-bitmap'\n"); | |
730 | exit(outf == stdout ? 0 : 2); | |
731 | ||
732 | case O(MANAGE,'U'): | |
733 | /* update=devicesize is allowed with --re-add */ | |
734 | if (devmode != 'a' || re_add != 1) { | |
735 | fprintf(stderr, Name "--update in Manage mode only" | |
736 | " allowed with --re-add.\n"); | |
737 | exit(1); | |
738 | } | |
739 | if (update) { | |
740 | fprintf(stderr, Name ": Can only update one aspect" | |
741 | " of superblock, both %s and %s given.\n", | |
742 | update, optarg); | |
743 | exit(2); | |
744 | } | |
745 | update = optarg; | |
746 | if (strcmp(update, "devicesize") != 0) { | |
747 | fprintf(stderr, Name ": only 'devicesize' can be" | |
748 | " updated with --re-add\n"); | |
749 | exit(2); | |
750 | } | |
751 | continue; | |
752 | ||
753 | case O(INCREMENTAL,NoDegraded): | |
754 | fprintf(stderr, Name ": --no-degraded is deprecated in Incremental mode\n"); | |
755 | case O(ASSEMBLE,NoDegraded): /* --no-degraded */ | |
756 | runstop = -1; /* --stop isn't allowed for --assemble, | |
757 | * so we overload slightly */ | |
758 | continue; | |
759 | ||
760 | case O(ASSEMBLE,'c'): | |
761 | case O(ASSEMBLE,ConfigFile): | |
762 | case O(INCREMENTAL, 'c'): | |
763 | case O(INCREMENTAL, ConfigFile): | |
764 | case O(MISC, 'c'): | |
765 | case O(MISC, ConfigFile): | |
766 | case O(MONITOR,'c'): | |
767 | case O(MONITOR,ConfigFile): | |
768 | if (configfile) { | |
769 | fprintf(stderr, Name ": configfile cannot be set twice. " | |
770 | "Second value is %s.\n", optarg); | |
771 | exit(2); | |
772 | } | |
773 | configfile = optarg; | |
774 | set_conffile(configfile); | |
775 | /* FIXME possibly check that config file exists. Even parse it */ | |
776 | continue; | |
777 | case O(ASSEMBLE,'s'): /* scan */ | |
778 | case O(MISC,'s'): | |
779 | case O(MONITOR,'s'): | |
780 | case O(INCREMENTAL,'s'): | |
781 | scan = 1; | |
782 | continue; | |
783 | ||
784 | case O(MONITOR,'m'): /* mail address */ | |
785 | case O(MONITOR,EMail): | |
786 | if (mailaddr) | |
787 | fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n", | |
788 | optarg); | |
789 | else | |
790 | mailaddr = optarg; | |
791 | continue; | |
792 | ||
793 | case O(MONITOR,'p'): /* alert program */ | |
794 | case O(MONITOR,ProgramOpt): /* alert program */ | |
795 | if (program) | |
796 | fprintf(stderr, Name ": only specify one alter program. %s ignored.\n", | |
797 | optarg); | |
798 | else | |
799 | program = optarg; | |
800 | continue; | |
801 | ||
802 | case O(MONITOR,'r'): /* rebuild increments */ | |
803 | case O(MONITOR,Increment): | |
804 | increments = atoi(optarg); | |
805 | if (increments>99 || increments<1) { | |
806 | fprintf(stderr, Name ": please specify positive integer between 1 and 99 as rebuild increments.\n"); | |
807 | exit(2); | |
808 | } | |
809 | continue; | |
810 | ||
811 | case O(MONITOR,'d'): /* delay in seconds */ | |
812 | case O(GROW, 'd'): | |
813 | case O(BUILD,'d'): /* delay for bitmap updates */ | |
814 | case O(CREATE,'d'): | |
815 | if (delay) | |
816 | fprintf(stderr, Name ": only specify delay once. %s ignored.\n", | |
817 | optarg); | |
818 | else { | |
819 | delay = strtol(optarg, &c, 10); | |
820 | if (!optarg[0] || *c || delay<1) { | |
821 | fprintf(stderr, Name ": invalid delay: %s\n", | |
822 | optarg); | |
823 | exit(2); | |
824 | } | |
825 | } | |
826 | continue; | |
827 | case O(MONITOR,'f'): /* daemonise */ | |
828 | case O(MONITOR,Fork): | |
829 | daemonise = 1; | |
830 | continue; | |
831 | case O(MONITOR,'i'): /* pid */ | |
832 | if (pidfile) | |
833 | fprintf(stderr, Name ": only specify one pid file. %s ignored.\n", | |
834 | optarg); | |
835 | else | |
836 | pidfile = optarg; | |
837 | continue; | |
838 | case O(MONITOR,'1'): /* oneshot */ | |
839 | oneshot = 1; | |
840 | continue; | |
841 | case O(MONITOR,'t'): /* test */ | |
842 | test = 1; | |
843 | continue; | |
844 | case O(MONITOR,'y'): /* log messages to syslog */ | |
845 | openlog("mdadm", LOG_PID, SYSLOG_FACILITY); | |
846 | dosyslog = 1; | |
847 | continue; | |
848 | case O(MONITOR, NoSharing): | |
849 | spare_sharing = 0; | |
850 | continue; | |
851 | /* now the general management options. Some are applicable | |
852 | * to other modes. None have arguments. | |
853 | */ | |
854 | case O(GROW,'a'): | |
855 | case O(GROW,Add): | |
856 | case O(MANAGE,'a'): | |
857 | case O(MANAGE,Add): /* add a drive */ | |
858 | devmode = 'a'; | |
859 | re_add = 0; | |
860 | continue; | |
861 | case O(MANAGE,ReAdd): | |
862 | devmode = 'a'; | |
863 | re_add = 1; | |
864 | continue; | |
865 | case O(MANAGE,'r'): /* remove a drive */ | |
866 | case O(MANAGE,Remove): | |
867 | devmode = 'r'; | |
868 | continue; | |
869 | case O(MANAGE,'f'): /* set faulty */ | |
870 | case O(MANAGE,Fail): | |
871 | case O(INCREMENTAL,'f'): | |
872 | case O(INCREMENTAL,Remove): | |
873 | case O(INCREMENTAL,Fail): /* r for incremental is taken, use f | |
874 | * even though we will both fail and | |
875 | * remove the device */ | |
876 | devmode = 'f'; | |
877 | continue; | |
878 | case O(INCREMENTAL,'R'): | |
879 | case O(MANAGE,'R'): | |
880 | case O(ASSEMBLE,'R'): | |
881 | case O(BUILD,'R'): | |
882 | case O(CREATE,'R'): /* Run the array */ | |
883 | if (runstop < 0) { | |
884 | fprintf(stderr, Name ": Cannot both Stop and Run an array\n"); | |
885 | exit(2); | |
886 | } | |
887 | runstop = 1; | |
888 | continue; | |
889 | case O(MANAGE,'S'): | |
890 | if (runstop > 0) { | |
891 | fprintf(stderr, Name ": Cannot both Run and Stop an array\n"); | |
892 | exit(2); | |
893 | } | |
894 | runstop = -1; | |
895 | continue; | |
896 | case O(MANAGE,'t'): | |
897 | test = 1; | |
898 | continue; | |
899 | ||
900 | case O(MISC,'Q'): | |
901 | case O(MISC,'D'): | |
902 | case O(MISC,'E'): | |
903 | case O(MISC,'K'): | |
904 | case O(MISC,'R'): | |
905 | case O(MISC,'S'): | |
906 | case O(MISC,'X'): | |
907 | case O(MISC,'o'): | |
908 | case O(MISC,'w'): | |
909 | case O(MISC,'W'): | |
910 | case O(MISC, WaitOpt): | |
911 | case O(MISC, Waitclean): | |
912 | case O(MISC, DetailPlatform): | |
913 | case O(MISC, KillSubarray): | |
914 | case O(MISC, UpdateSubarray): | |
915 | if (devmode && devmode != opt && | |
916 | (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) { | |
917 | fprintf(stderr, Name ": --examine/-E cannot be given with "); | |
918 | if (devmode == 'E') { | |
919 | if (option_index >= 0) | |
920 | fprintf(stderr, "--%s\n", | |
921 | long_options[option_index].name); | |
922 | else | |
923 | fprintf(stderr, "-%c\n", opt); | |
924 | } else if (isalpha(devmode)) | |
925 | fprintf(stderr, "-%c\n", devmode); | |
926 | else | |
927 | fprintf(stderr, "previous option\n"); | |
928 | exit(2); | |
929 | } | |
930 | devmode = opt; | |
931 | continue; | |
932 | case O(MISC,'t'): | |
933 | test = 1; | |
934 | continue; | |
935 | ||
936 | case O(MISC, Sparc22): | |
937 | if (devmode != 'E') { | |
938 | fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n"); | |
939 | exit(2); | |
940 | } | |
941 | SparcAdjust = 1; | |
942 | continue; | |
943 | ||
944 | case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */ | |
945 | case O(ASSEMBLE,Bitmap): | |
946 | if (!optarg) { | |
947 | fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n"); | |
948 | exit(2); | |
949 | } | |
950 | if (strcmp(optarg, "internal")==0) { | |
951 | fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n"); | |
952 | continue; | |
953 | } | |
954 | bitmap_fd = open(optarg, O_RDWR); | |
955 | if (!*optarg || bitmap_fd < 0) { | |
956 | fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno)); | |
957 | exit(2); | |
958 | } | |
959 | ident.bitmap_fd = bitmap_fd; /* for Assemble */ | |
960 | continue; | |
961 | ||
962 | case O(ASSEMBLE, BackupFile): | |
963 | case O(GROW, BackupFile): | |
964 | /* Specify a file into which grow might place a backup, | |
965 | * or from which assemble might recover a backup | |
966 | */ | |
967 | if (backup_file) { | |
968 | fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg); | |
969 | exit(2); | |
970 | } | |
971 | backup_file = optarg; | |
972 | continue; | |
973 | ||
974 | case O(ASSEMBLE, InvalidBackup): | |
975 | /* Acknowledge that the backupfile is invalid, but ask | |
976 | * to continue anyway | |
977 | */ | |
978 | invalid_backup = 1; | |
979 | continue; | |
980 | ||
981 | case O(BUILD,'b'): | |
982 | case O(BUILD,Bitmap): | |
983 | case O(CREATE,'b'): | |
984 | case O(CREATE,Bitmap): /* here we create the bitmap */ | |
985 | if (strcmp(optarg, "none") == 0) { | |
986 | fprintf(stderr, Name ": '--bitmap none' only" | |
987 | " support for --grow\n"); | |
988 | exit(2); | |
989 | } | |
990 | /* FALL THROUGH */ | |
991 | case O(GROW,'b'): | |
992 | case O(GROW,Bitmap): | |
993 | if (strcmp(optarg, "internal")== 0 || | |
994 | strcmp(optarg, "none")== 0 || | |
995 | strchr(optarg, '/') != NULL) { | |
996 | bitmap_file = optarg; | |
997 | continue; | |
998 | } | |
999 | /* probable typo */ | |
1000 | fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n" | |
1001 | " not '%s'\n", optarg); | |
1002 | exit(2); | |
1003 | ||
1004 | case O(GROW,BitmapChunk): | |
1005 | case O(BUILD,BitmapChunk): | |
1006 | case O(CREATE,BitmapChunk): /* bitmap chunksize */ | |
1007 | bitmap_chunk = parse_size(optarg); | |
1008 | if (bitmap_chunk < 0 || | |
1009 | bitmap_chunk & (bitmap_chunk - 1)) { | |
1010 | fprintf(stderr, | |
1011 | Name ": invalid bitmap chunksize: %s\n", | |
1012 | optarg); | |
1013 | exit(2); | |
1014 | } | |
1015 | /* convert sectors to B, chunk of 0 means 512B */ | |
1016 | bitmap_chunk = bitmap_chunk ? bitmap_chunk * 512 : 512; | |
1017 | continue; | |
1018 | ||
1019 | case O(GROW, WriteBehind): | |
1020 | case O(BUILD, WriteBehind): | |
1021 | case O(CREATE, WriteBehind): /* write-behind mode */ | |
1022 | write_behind = DEFAULT_MAX_WRITE_BEHIND; | |
1023 | if (optarg) { | |
1024 | write_behind = strtol(optarg, &c, 10); | |
1025 | if (write_behind < 0 || *c || | |
1026 | write_behind > 16383) { | |
1027 | fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg); | |
1028 | exit(2); | |
1029 | } | |
1030 | } | |
1031 | continue; | |
1032 | ||
1033 | case O(INCREMENTAL, 'r'): | |
1034 | case O(INCREMENTAL, RebuildMapOpt): | |
1035 | rebuild_map = 1; | |
1036 | continue; | |
1037 | case O(INCREMENTAL, IncrementalPath): | |
1038 | remove_path = optarg; | |
1039 | continue; | |
1040 | } | |
1041 | /* We have now processed all the valid options. Anything else is | |
1042 | * an error | |
1043 | */ | |
1044 | if (option_index > 0) | |
1045 | fprintf(stderr, Name ":option --%s not valid in %s mode\n", | |
1046 | long_options[option_index].name, | |
1047 | map_num(modes, mode)); | |
1048 | else | |
1049 | fprintf(stderr, Name ": option -%c not valid in %s mode\n", | |
1050 | opt, map_num(modes, mode)); | |
1051 | exit(2); | |
1052 | ||
1053 | } | |
1054 | ||
1055 | if (print_help) { | |
1056 | char *help_text = Help; | |
1057 | if (print_help == 2) | |
1058 | help_text = OptionHelp; | |
1059 | else | |
1060 | switch (mode) { | |
1061 | case ASSEMBLE : help_text = Help_assemble; break; | |
1062 | case BUILD : help_text = Help_build; break; | |
1063 | case CREATE : help_text = Help_create; break; | |
1064 | case MANAGE : help_text = Help_manage; break; | |
1065 | case MISC : help_text = Help_misc; break; | |
1066 | case MONITOR : help_text = Help_monitor; break; | |
1067 | case GROW : help_text = Help_grow; break; | |
1068 | case INCREMENTAL:help_text= Help_incr; break; | |
1069 | } | |
1070 | fputs(help_text,stdout); | |
1071 | exit(0); | |
1072 | } | |
1073 | ||
1074 | if (!mode && devs_found) { | |
1075 | mode = MISC; | |
1076 | devmode = 'Q'; | |
1077 | if (devlist->disposition == 0) | |
1078 | devlist->disposition = devmode; | |
1079 | } | |
1080 | if (!mode) { | |
1081 | fputs(Usage, stderr); | |
1082 | exit(2); | |
1083 | } | |
1084 | ||
1085 | if (symlinks) { | |
1086 | struct createinfo *ci = conf_get_create_info(); | |
1087 | ||
1088 | if (strcasecmp(symlinks, "yes") == 0) | |
1089 | ci->symlinks = 1; | |
1090 | else if (strcasecmp(symlinks, "no") == 0) | |
1091 | ci->symlinks = 0; | |
1092 | else { | |
1093 | fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n"); | |
1094 | exit(2); | |
1095 | } | |
1096 | } | |
1097 | /* Ok, got the option parsing out of the way | |
1098 | * hopefully it's mostly right but there might be some stuff | |
1099 | * missing | |
1100 | * | |
1101 | * That is mosty checked in the per-mode stuff but... | |
1102 | * | |
1103 | * For @,B,C and A without -s, the first device listed must be an md device | |
1104 | * we check that here and open it. | |
1105 | */ | |
1106 | ||
1107 | if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW || | |
1108 | (mode == ASSEMBLE && ! scan)) { | |
1109 | if (devs_found < 1) { | |
1110 | fprintf(stderr, Name ": an md device must be given in this mode\n"); | |
1111 | exit(2); | |
1112 | } | |
1113 | if ((int)ident.super_minor == -2 && autof) { | |
1114 | fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n"); | |
1115 | exit(2); | |
1116 | } | |
1117 | if (mode == MANAGE || mode == GROW) { | |
1118 | mdfd = open_mddev(devlist->devname, 1); | |
1119 | if (mdfd < 0) | |
1120 | exit(1); | |
1121 | } else | |
1122 | /* non-existent device is OK */ | |
1123 | mdfd = open_mddev(devlist->devname, 0); | |
1124 | if (mdfd == -2) { | |
1125 | fprintf(stderr, Name ": device %s exists but is not an " | |
1126 | "md array.\n", devlist->devname); | |
1127 | exit(1); | |
1128 | } | |
1129 | if ((int)ident.super_minor == -2) { | |
1130 | struct stat stb; | |
1131 | if (mdfd < 0) { | |
1132 | fprintf(stderr, Name ": --super-minor=dev given, and " | |
1133 | "listed device %s doesn't exist.\n", | |
1134 | devlist->devname); | |
1135 | exit(1); | |
1136 | } | |
1137 | fstat(mdfd, &stb); | |
1138 | ident.super_minor = minor(stb.st_rdev); | |
1139 | } | |
1140 | if (mdfd >= 0 && mode != MANAGE && mode != GROW) { | |
1141 | /* We don't really want this open yet, we just might | |
1142 | * have wanted to check some things | |
1143 | */ | |
1144 | close(mdfd); | |
1145 | mdfd = -1; | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | if (raiddisks) { | |
1150 | if (raiddisks == 1 && !force && level != -5) { | |
1151 | fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n" | |
1152 | " a mistake. If you really mean it you will need to specify --force before\n" | |
1153 | " setting the number of drives.\n"); | |
1154 | exit(2); | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | if (homehost == NULL) | |
1159 | homehost = conf_get_homehost(&require_homehost); | |
1160 | if (homehost == NULL || strcmp(homehost, "<system>")==0) { | |
1161 | if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) { | |
1162 | sys_hostname[sizeof(sys_hostname)-1] = 0; | |
1163 | homehost = sys_hostname; | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | if ((mode != MISC || devmode != 'E') && | |
1168 | geteuid() != 0) { | |
1169 | fprintf(stderr, Name ": must be super-user to perform this action\n"); | |
1170 | exit(1); | |
1171 | } | |
1172 | ||
1173 | ident.autof = autof; | |
1174 | ||
1175 | rv = 0; | |
1176 | switch(mode) { | |
1177 | case MANAGE: | |
1178 | /* readonly, add/remove, readwrite, runstop */ | |
1179 | if (readonly>0) | |
1180 | rv = Manage_ro(devlist->devname, mdfd, readonly); | |
1181 | if (!rv && devs_found>1) | |
1182 | rv = Manage_subdevs(devlist->devname, mdfd, | |
1183 | devlist->next, verbose-quiet, test, | |
1184 | update); | |
1185 | if (!rv && readonly < 0) | |
1186 | rv = Manage_ro(devlist->devname, mdfd, readonly); | |
1187 | if (!rv && runstop) | |
1188 | rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet); | |
1189 | break; | |
1190 | case ASSEMBLE: | |
1191 | if (devs_found == 1 && ident.uuid_set == 0 && | |
1192 | ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) { | |
1193 | /* Only a device has been given, so get details from config file */ | |
1194 | struct mddev_ident *array_ident = conf_get_ident(devlist->devname); | |
1195 | if (array_ident == NULL) { | |
1196 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
1197 | devlist->devname); | |
1198 | rv |= 1; | |
1199 | if (mdfd >= 0) | |
1200 | close(mdfd); | |
1201 | } else { | |
1202 | if (array_ident->autof == 0) | |
1203 | array_ident->autof = autof; | |
1204 | rv |= Assemble(ss, devlist->devname, array_ident, | |
1205 | NULL, backup_file, invalid_backup, | |
1206 | readonly, runstop, update, | |
1207 | homehost, require_homehost, | |
1208 | verbose-quiet, force); | |
1209 | } | |
1210 | } else if (!scan) | |
1211 | rv = Assemble(ss, devlist->devname, &ident, | |
1212 | devlist->next, backup_file, invalid_backup, | |
1213 | readonly, runstop, update, | |
1214 | homehost, require_homehost, | |
1215 | verbose-quiet, force); | |
1216 | else if (devs_found>0) { | |
1217 | if (update && devs_found > 1) { | |
1218 | fprintf(stderr, Name ": can only update a single array at a time\n"); | |
1219 | exit(1); | |
1220 | } | |
1221 | if (backup_file && devs_found > 1) { | |
1222 | fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n"); | |
1223 | exit(1); | |
1224 | } | |
1225 | for (dv = devlist ; dv ; dv=dv->next) { | |
1226 | struct mddev_ident *array_ident = conf_get_ident(dv->devname); | |
1227 | if (array_ident == NULL) { | |
1228 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
1229 | dv->devname); | |
1230 | rv |= 1; | |
1231 | continue; | |
1232 | } | |
1233 | if (array_ident->autof == 0) | |
1234 | array_ident->autof = autof; | |
1235 | rv |= Assemble(ss, dv->devname, array_ident, | |
1236 | NULL, backup_file, invalid_backup, | |
1237 | readonly, runstop, update, | |
1238 | homehost, require_homehost, | |
1239 | verbose-quiet, force); | |
1240 | } | |
1241 | } else { | |
1242 | struct mddev_ident *a, *array_list = conf_get_ident(NULL); | |
1243 | struct mddev_dev *devlist = conf_get_devs(); | |
1244 | int cnt = 0; | |
1245 | int failures, successes; | |
1246 | if (devlist == NULL) { | |
1247 | fprintf(stderr, Name ": No devices listed in conf file were found.\n"); | |
1248 | exit(1); | |
1249 | } | |
1250 | if (update) { | |
1251 | fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n"); | |
1252 | exit(1); | |
1253 | } | |
1254 | if (backup_file) { | |
1255 | fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n"); | |
1256 | exit(1); | |
1257 | } | |
1258 | for (a = array_list; a ; a = a->next) { | |
1259 | a->assembled = 0; | |
1260 | if (a->autof == 0) | |
1261 | a->autof = autof; | |
1262 | } | |
1263 | do { | |
1264 | failures = 0; | |
1265 | successes = 0; | |
1266 | rv = 0; | |
1267 | for (a = array_list; a ; a = a->next) { | |
1268 | int r; | |
1269 | if (a->assembled) | |
1270 | continue; | |
1271 | if (a->devname && | |
1272 | strcasecmp(a->devname, "<ignore>") == 0) | |
1273 | continue; | |
1274 | ||
1275 | r = Assemble(ss, a->devname, | |
1276 | a, | |
1277 | NULL, NULL, 0, | |
1278 | readonly, runstop, NULL, | |
1279 | homehost, require_homehost, | |
1280 | verbose-quiet, force); | |
1281 | if (r == 0) { | |
1282 | a->assembled = 1; | |
1283 | successes++; | |
1284 | } else | |
1285 | failures++; | |
1286 | rv |= r; | |
1287 | cnt++; | |
1288 | } | |
1289 | } while (failures && successes); | |
1290 | if (homehost && cnt == 0) { | |
1291 | /* Maybe we can auto-assemble something. | |
1292 | * Repeatedly call Assemble in auto-assemble mode | |
1293 | * until it fails | |
1294 | */ | |
1295 | int rv2; | |
1296 | int acnt; | |
1297 | ident.autof = autof; | |
1298 | do { | |
1299 | struct mddev_dev *devlist = conf_get_devs(); | |
1300 | acnt = 0; | |
1301 | do { | |
1302 | rv2 = Assemble(ss, NULL, | |
1303 | &ident, | |
1304 | devlist, NULL, 0, | |
1305 | readonly, runstop, NULL, | |
1306 | homehost, require_homehost, | |
1307 | verbose-quiet, force); | |
1308 | if (rv2==0) { | |
1309 | cnt++; | |
1310 | acnt++; | |
1311 | } | |
1312 | if (rv2 == 1) | |
1313 | /* found something so even though assembly failed we | |
1314 | * want to avoid auto-updates | |
1315 | */ | |
1316 | auto_update_home = 0; | |
1317 | } while (rv2!=2); | |
1318 | /* Incase there are stacked devices, we need to go around again */ | |
1319 | } while (acnt); | |
1320 | #if 0 | |
1321 | if (cnt == 0 && auto_update_home && homehost) { | |
1322 | /* Nothing found, maybe we need to bootstrap homehost info */ | |
1323 | do { | |
1324 | acnt = 0; | |
1325 | do { | |
1326 | rv2 = Assemble( | |
1327 | ss, NULL, | |
1328 | &ident, | |
1329 | NULL, NULL, 0, | |
1330 | readonly, runstop, | |
1331 | "homehost", | |
1332 | homehost, | |
1333 | require_homehost, | |
1334 | verbose-quiet, force); | |
1335 | if (rv2==0) { | |
1336 | cnt++; | |
1337 | acnt++; | |
1338 | } | |
1339 | } while (rv2!=2); | |
1340 | /* Incase there are stacked devices, we need to go around again */ | |
1341 | } while (acnt); | |
1342 | } | |
1343 | #endif | |
1344 | if (cnt == 0 && rv == 0) { | |
1345 | fprintf(stderr, Name ": No arrays found in config file or automatically\n"); | |
1346 | rv = 1; | |
1347 | } else if (cnt) | |
1348 | rv = 0; | |
1349 | } else if (cnt == 0 && rv == 0) { | |
1350 | fprintf(stderr, Name ": No arrays found in config file\n"); | |
1351 | rv = 1; | |
1352 | } | |
1353 | } | |
1354 | break; | |
1355 | case BUILD: | |
1356 | if (delay == 0) delay = DEFAULT_BITMAP_DELAY; | |
1357 | if (write_behind && !bitmap_file) { | |
1358 | fprintf(stderr, Name ": write-behind mode requires a bitmap.\n"); | |
1359 | rv = 1; | |
1360 | break; | |
1361 | } | |
1362 | if (raiddisks == 0) { | |
1363 | fprintf(stderr, Name ": no raid-devices specified.\n"); | |
1364 | rv = 1; | |
1365 | break; | |
1366 | } | |
1367 | ||
1368 | if (bitmap_file) { | |
1369 | if (strcmp(bitmap_file, "internal")==0) { | |
1370 | fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n"); | |
1371 | rv |= 1; | |
1372 | break; | |
1373 | } | |
1374 | } | |
1375 | rv = Build(devlist->devname, chunk, level, layout, | |
1376 | raiddisks, devlist->next, assume_clean, | |
1377 | bitmap_file, bitmap_chunk, write_behind, | |
1378 | delay, verbose-quiet, autof, size); | |
1379 | break; | |
1380 | case CREATE: | |
1381 | if (delay == 0) delay = DEFAULT_BITMAP_DELAY; | |
1382 | if (write_behind && !bitmap_file) { | |
1383 | fprintf(stderr, Name ": write-behind mode requires a bitmap.\n"); | |
1384 | rv = 1; | |
1385 | break; | |
1386 | } | |
1387 | if (raiddisks == 0) { | |
1388 | fprintf(stderr, Name ": no raid-devices specified.\n"); | |
1389 | rv = 1; | |
1390 | break; | |
1391 | } | |
1392 | ||
1393 | rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size, | |
1394 | raiddisks, sparedisks, ident.name, homehost, | |
1395 | ident.uuid_set ? ident.uuid : NULL, | |
1396 | devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean, | |
1397 | bitmap_file, bitmap_chunk, write_behind, delay, autof); | |
1398 | break; | |
1399 | case MISC: | |
1400 | if (devmode == 'E') { | |
1401 | if (devlist == NULL && !scan) { | |
1402 | fprintf(stderr, Name ": No devices to examine\n"); | |
1403 | exit(2); | |
1404 | } | |
1405 | if (devlist == NULL) | |
1406 | devlist = conf_get_devs(); | |
1407 | if (devlist == NULL) { | |
1408 | fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile); | |
1409 | exit(1); | |
1410 | } | |
1411 | if (brief && verbose) | |
1412 | brief = 2; | |
1413 | rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, | |
1414 | export, scan, | |
1415 | SparcAdjust, ss, homehost); | |
1416 | } else if (devmode == DetailPlatform) { | |
1417 | rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose); | |
1418 | } else { | |
1419 | if (devlist == NULL) { | |
1420 | if ((devmode=='D' || devmode == Waitclean) && scan) { | |
1421 | /* apply --detail or --wait-clean to | |
1422 | * all devices in /proc/mdstat | |
1423 | */ | |
1424 | struct mdstat_ent *ms = mdstat_read(0, 1); | |
1425 | struct mdstat_ent *e; | |
1426 | struct map_ent *map = NULL; | |
1427 | int members; | |
1428 | int v = verbose>1?0:verbose+1; | |
1429 | ||
1430 | for (members = 0; members <= 1; members++) { | |
1431 | for (e=ms ; e ; e=e->next) { | |
1432 | char *name; | |
1433 | struct map_ent *me; | |
1434 | int member = e->metadata_version && | |
1435 | strncmp(e->metadata_version, | |
1436 | "external:/", 10) == 0; | |
1437 | if (members != member) | |
1438 | continue; | |
1439 | me = map_by_devnum(&map, e->devnum); | |
1440 | if (me && me->path | |
1441 | && strcmp(me->path, "/unknown") != 0) | |
1442 | name = me->path; | |
1443 | else | |
1444 | name = get_md_name(e->devnum); | |
1445 | ||
1446 | if (!name) { | |
1447 | fprintf(stderr, Name ": cannot find device file for %s\n", | |
1448 | e->dev); | |
1449 | continue; | |
1450 | } | |
1451 | if (devmode == 'D') | |
1452 | rv |= Detail(name, v, | |
1453 | export, test, | |
1454 | homehost); | |
1455 | else | |
1456 | rv |= WaitClean(name, -1, v); | |
1457 | put_md_name(name); | |
1458 | } | |
1459 | } | |
1460 | free_mdstat(ms); | |
1461 | } else if (devmode == 'S' && scan) { | |
1462 | /* apply --stop to all devices in /proc/mdstat */ | |
1463 | /* Due to possible stacking of devices, repeat until | |
1464 | * nothing more can be stopped | |
1465 | */ | |
1466 | int progress=1, err; | |
1467 | int last = 0; | |
1468 | do { | |
1469 | struct mdstat_ent *ms = mdstat_read(0, 0); | |
1470 | struct mdstat_ent *e; | |
1471 | ||
1472 | if (!progress) last = 1; | |
1473 | progress = 0; err = 0; | |
1474 | for (e=ms ; e ; e=e->next) { | |
1475 | char *name = get_md_name(e->devnum); | |
1476 | ||
1477 | if (!name) { | |
1478 | fprintf(stderr, Name ": cannot find device file for %s\n", | |
1479 | e->dev); | |
1480 | continue; | |
1481 | } | |
1482 | mdfd = open_mddev(name, 1); | |
1483 | if (mdfd >= 0) { | |
1484 | if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1)) | |
1485 | err = 1; | |
1486 | else | |
1487 | progress = 1; | |
1488 | close(mdfd); | |
1489 | } | |
1490 | ||
1491 | put_md_name(name); | |
1492 | } | |
1493 | free_mdstat(ms); | |
1494 | } while (!last && err); | |
1495 | if (err) rv |= 1; | |
1496 | } else { | |
1497 | fprintf(stderr, Name ": No devices given.\n"); | |
1498 | exit(2); | |
1499 | } | |
1500 | } | |
1501 | for (dv=devlist ; dv; dv=dv->next) { | |
1502 | switch(dv->disposition) { | |
1503 | case 'D': | |
1504 | rv |= Detail(dv->devname, | |
1505 | brief?1+verbose:0, | |
1506 | export, test, homehost); | |
1507 | continue; | |
1508 | case 'K': /* Zero superblock */ | |
1509 | if (ss) | |
1510 | rv |= Kill(dv->devname, ss, force, quiet,0); | |
1511 | else { | |
1512 | int q = quiet; | |
1513 | do { | |
1514 | rv |= Kill(dv->devname, NULL, force, q, 0); | |
1515 | q = 1; | |
1516 | } while (rv == 0); | |
1517 | rv &= ~2; | |
1518 | } | |
1519 | continue; | |
1520 | case 'Q': | |
1521 | rv |= Query(dv->devname); continue; | |
1522 | case 'X': | |
1523 | rv |= ExamineBitmap(dv->devname, brief, ss); continue; | |
1524 | case 'W': | |
1525 | case WaitOpt: | |
1526 | rv |= Wait(dv->devname); continue; | |
1527 | case Waitclean: | |
1528 | rv |= WaitClean(dv->devname, -1, verbose-quiet); continue; | |
1529 | case KillSubarray: | |
1530 | rv |= Kill_subarray(dv->devname, subarray, quiet); | |
1531 | continue; | |
1532 | case UpdateSubarray: | |
1533 | if (update == NULL) { | |
1534 | fprintf(stderr, | |
1535 | Name ": -U/--update must be specified with --update-subarray\n"); | |
1536 | rv |= 1; | |
1537 | continue; | |
1538 | } | |
1539 | rv |= Update_subarray(dv->devname, subarray, update, &ident, quiet); | |
1540 | continue; | |
1541 | } | |
1542 | mdfd = open_mddev(dv->devname, 1); | |
1543 | if (mdfd>=0) { | |
1544 | switch(dv->disposition) { | |
1545 | case 'R': | |
1546 | rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break; | |
1547 | case 'S': | |
1548 | rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break; | |
1549 | case 'o': | |
1550 | rv |= Manage_ro(dv->devname, mdfd, 1); break; | |
1551 | case 'w': | |
1552 | rv |= Manage_ro(dv->devname, mdfd, -1); break; | |
1553 | } | |
1554 | close(mdfd); | |
1555 | } else | |
1556 | rv |= 1; | |
1557 | } | |
1558 | } | |
1559 | break; | |
1560 | case MONITOR: | |
1561 | if (!devlist && !scan) { | |
1562 | fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n"); | |
1563 | rv = 1; | |
1564 | break; | |
1565 | } | |
1566 | if (pidfile && !daemonise) { | |
1567 | fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n"); | |
1568 | rv = 1; | |
1569 | break; | |
1570 | } | |
1571 | if (delay == 0) { | |
1572 | if (get_linux_version() > 2006016) | |
1573 | /* mdstat responds to poll */ | |
1574 | delay = 1000; | |
1575 | else | |
1576 | delay = 60; | |
1577 | } | |
1578 | rv= Monitor(devlist, mailaddr, program, | |
1579 | delay?delay:60, daemonise, scan, oneshot, | |
1580 | dosyslog, test, pidfile, increments, spare_sharing); | |
1581 | break; | |
1582 | ||
1583 | case GROW: | |
1584 | if (array_size >= 0) { | |
1585 | /* alway impose array size first, independent of | |
1586 | * anything else | |
1587 | * Do not allow level or raid_disks changes at the | |
1588 | * same time as that can be irreversibly destructive. | |
1589 | */ | |
1590 | struct mdinfo sra; | |
1591 | int err; | |
1592 | if (raiddisks || level != UnSet) { | |
1593 | fprintf(stderr, Name ": cannot change array size in same operation " | |
1594 | "as changing raiddisks or level.\n" | |
1595 | " Change size first, then check that data is still intact.\n"); | |
1596 | rv = 1; | |
1597 | break; | |
1598 | } | |
1599 | sysfs_init(&sra, mdfd, 0); | |
1600 | if (array_size == 0) | |
1601 | err = sysfs_set_str(&sra, NULL, "array_size", "default"); | |
1602 | else | |
1603 | err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2); | |
1604 | if (err < 0) { | |
1605 | if (errno == E2BIG) | |
1606 | fprintf(stderr, Name ": --array-size setting" | |
1607 | " is too large.\n"); | |
1608 | else | |
1609 | fprintf(stderr, Name ": current kernel does" | |
1610 | " not support setting --array-size\n"); | |
1611 | rv = 1; | |
1612 | break; | |
1613 | } | |
1614 | } | |
1615 | if (devs_found > 1) { | |
1616 | ||
1617 | /* must be '-a'. */ | |
1618 | if (size >= 0 || raiddisks || chunk || layout_str != NULL || bitmap_file) { | |
1619 | fprintf(stderr, Name ": --add cannot be used with other geometry changes in --grow mode\n"); | |
1620 | rv = 1; | |
1621 | break; | |
1622 | } | |
1623 | for (dv=devlist->next; dv ; dv=dv->next) { | |
1624 | rv = Grow_Add_device(devlist->devname, mdfd, dv->devname); | |
1625 | if (rv) | |
1626 | break; | |
1627 | } | |
1628 | } else if (bitmap_file) { | |
1629 | if (size >= 0 || raiddisks || chunk || layout_str != NULL) { | |
1630 | fprintf(stderr, Name ": --bitmap changes cannot be used with other geometry changes in --grow mode\n"); | |
1631 | rv = 1; | |
1632 | break; | |
1633 | } | |
1634 | if (delay == 0) | |
1635 | delay = DEFAULT_BITMAP_DELAY; | |
1636 | rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file, | |
1637 | bitmap_chunk, delay, write_behind, force); | |
1638 | } else if (size >= 0 || raiddisks != 0 || layout_str != NULL | |
1639 | || chunk != 0 || level != UnSet) { | |
1640 | rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file, | |
1641 | size, level, layout_str, chunk, raiddisks, | |
1642 | force); | |
1643 | } else if (array_size < 0) | |
1644 | fprintf(stderr, Name ": no changes to --grow\n"); | |
1645 | break; | |
1646 | case INCREMENTAL: | |
1647 | if (rebuild_map) { | |
1648 | RebuildMap(); | |
1649 | } | |
1650 | if (scan) { | |
1651 | if (runstop <= 0) { | |
1652 | fprintf(stderr, Name | |
1653 | ": --incremental --scan meaningless without --run.\n"); | |
1654 | break; | |
1655 | } | |
1656 | if (devmode == 'f') { | |
1657 | fprintf(stderr, Name | |
1658 | ": --incremental --scan --fail not supported.\n"); | |
1659 | break; | |
1660 | } | |
1661 | rv = IncrementalScan(verbose); | |
1662 | } | |
1663 | if (!devlist) { | |
1664 | if (!rebuild_map && !scan) { | |
1665 | fprintf(stderr, Name | |
1666 | ": --incremental requires a device.\n"); | |
1667 | rv = 1; | |
1668 | } | |
1669 | break; | |
1670 | } | |
1671 | if (devlist->next) { | |
1672 | fprintf(stderr, Name | |
1673 | ": --incremental can only handle one device.\n"); | |
1674 | rv = 1; | |
1675 | break; | |
1676 | } | |
1677 | if (devmode == 'f') | |
1678 | rv = IncrementalRemove(devlist->devname, remove_path, | |
1679 | verbose-quiet); | |
1680 | else | |
1681 | rv = Incremental(devlist->devname, verbose-quiet, | |
1682 | runstop, ss, homehost, | |
1683 | require_homehost, autof); | |
1684 | break; | |
1685 | case AUTODETECT: | |
1686 | autodetect(); | |
1687 | break; | |
1688 | } | |
1689 | exit(rv); | |
1690 | } |