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