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